2 * linux/drivers/video/backlight/pwm_bl.c
4 * simple PWM based backlight control, board code has to setup
5 * 1) pin configuration so PWM waveforms can output
6 * 2) platform_data casts to the PWM id (0/1/2/3 on PXA)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/platform_device.h>
18 #include <linux/backlight.h>
19 #include <linux/err.h>
20 #include <linux/pwm.h>
21 #include <linux/pwm_backlight.h>
24 struct pwm_device *pwm;
26 int (*notify)(int brightness);
29 static int pwm_backlight_update_status(struct backlight_device *bl)
31 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
32 int brightness = bl->props.brightness;
33 int max = bl->props.max_brightness;
35 if (bl->props.power != FB_BLANK_UNBLANK)
38 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
42 brightness = pb->notify(brightness);
44 if (brightness == 0) {
45 pwm_config(pb->pwm, 0, pb->period);
48 pwm_config(pb->pwm, brightness * pb->period / max, pb->period);
54 static int pwm_backlight_get_brightness(struct backlight_device *bl)
56 return bl->props.brightness;
59 static struct backlight_ops pwm_backlight_ops = {
60 .update_status = pwm_backlight_update_status,
61 .get_brightness = pwm_backlight_get_brightness,
64 static int pwm_backlight_probe(struct platform_device *pdev)
66 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
67 struct backlight_device *bl;
68 struct pwm_bl_data *pb;
72 dev_err(&pdev->dev, "failed to find platform data\n");
77 ret = data->init(&pdev->dev);
82 pb = kzalloc(sizeof(*pb), GFP_KERNEL);
84 dev_err(&pdev->dev, "no memory for state\n");
89 pb->period = data->pwm_period_ns;
90 pb->notify = data->notify;
92 pb->pwm = pwm_request(data->pwm_id, "backlight");
93 if (IS_ERR(pb->pwm)) {
94 dev_err(&pdev->dev, "unable to request PWM for backlight\n");
95 ret = PTR_ERR(pb->pwm);
98 dev_dbg(&pdev->dev, "got pwm for backlight\n");
100 bl = backlight_device_register(pdev->name, &pdev->dev,
101 pb, &pwm_backlight_ops);
103 dev_err(&pdev->dev, "failed to register backlight\n");
108 bl->props.max_brightness = data->max_brightness;
109 bl->props.brightness = data->dft_brightness;
110 backlight_update_status(bl);
112 platform_set_drvdata(pdev, bl);
121 data->exit(&pdev->dev);
125 static int pwm_backlight_remove(struct platform_device *pdev)
127 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
128 struct backlight_device *bl = platform_get_drvdata(pdev);
129 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
131 backlight_device_unregister(bl);
132 pwm_config(pb->pwm, 0, pb->period);
133 pwm_disable(pb->pwm);
137 data->exit(&pdev->dev);
142 static int pwm_backlight_suspend(struct platform_device *pdev,
145 struct backlight_device *bl = platform_get_drvdata(pdev);
146 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
148 pwm_config(pb->pwm, 0, pb->period);
149 pwm_disable(pb->pwm);
153 static int pwm_backlight_resume(struct platform_device *pdev)
155 struct backlight_device *bl = platform_get_drvdata(pdev);
157 backlight_update_status(bl);
161 #define pwm_backlight_suspend NULL
162 #define pwm_backlight_resume NULL
165 static struct platform_driver pwm_backlight_driver = {
167 .name = "pwm-backlight",
168 .owner = THIS_MODULE,
170 .probe = pwm_backlight_probe,
171 .remove = pwm_backlight_remove,
172 .suspend = pwm_backlight_suspend,
173 .resume = pwm_backlight_resume,
176 static int __init pwm_backlight_init(void)
178 return platform_driver_register(&pwm_backlight_driver);
180 module_init(pwm_backlight_init);
182 static void __exit pwm_backlight_exit(void)
184 platform_driver_unregister(&pwm_backlight_driver);
186 module_exit(pwm_backlight_exit);
188 MODULE_DESCRIPTION("PWM based Backlight Driver");
189 MODULE_LICENSE("GPL");
190 MODULE_ALIAS("platform:pwm-backlight");