pwm-backlight: Track enable state
[cascardo/linux.git] / drivers / video / backlight / pwm_bl.c
1 /*
2  * linux/drivers/video/backlight/pwm_bl.c
3  *
4  * simple PWM based backlight control, board code has to setup
5  * 1) pin configuration so PWM waveforms can output
6  * 2) platform_data being correctly configured
7  *
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.
11  */
12
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/platform_device.h>
17 #include <linux/fb.h>
18 #include <linux/backlight.h>
19 #include <linux/err.h>
20 #include <linux/pwm.h>
21 #include <linux/pwm_backlight.h>
22 #include <linux/slab.h>
23
24 struct pwm_bl_data {
25         struct pwm_device       *pwm;
26         struct device           *dev;
27         unsigned int            period;
28         unsigned int            lth_brightness;
29         unsigned int            *levels;
30         bool                    enabled;
31         int                     (*notify)(struct device *,
32                                           int brightness);
33         void                    (*notify_after)(struct device *,
34                                         int brightness);
35         int                     (*check_fb)(struct device *, struct fb_info *);
36         void                    (*exit)(struct device *);
37 };
38
39 static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness,
40                                    int max)
41 {
42         int duty_cycle, err;
43
44         if (pb->enabled)
45                 return;
46
47         if (pb->levels) {
48                 duty_cycle = pb->levels[brightness];
49                 max = pb->levels[max];
50         } else {
51                 duty_cycle = brightness;
52         }
53
54         duty_cycle = (duty_cycle * (pb->period - pb->lth_brightness) / max) +
55                      pb->lth_brightness;
56
57         pwm_config(pb->pwm, duty_cycle, pb->period);
58         pwm_enable(pb->pwm);
59         pb->enabled = true;
60 }
61
62 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
63 {
64         if (!pb->enabled)
65                 return;
66
67         pwm_config(pb->pwm, 0, pb->period);
68         pwm_disable(pb->pwm);
69
70         pb->enabled = false;
71 }
72
73 static int pwm_backlight_update_status(struct backlight_device *bl)
74 {
75         struct pwm_bl_data *pb = bl_get_data(bl);
76         int brightness = bl->props.brightness;
77         int max = bl->props.max_brightness;
78
79         if (bl->props.power != FB_BLANK_UNBLANK ||
80             bl->props.fb_blank != FB_BLANK_UNBLANK ||
81             bl->props.state & BL_CORE_FBBLANK)
82                 brightness = 0;
83
84         if (pb->notify)
85                 brightness = pb->notify(pb->dev, brightness);
86
87         if (brightness > 0)
88                 pwm_backlight_power_on(pb, brightness, max);
89         else
90                 pwm_backlight_power_off(pb);
91
92         if (pb->notify_after)
93                 pb->notify_after(pb->dev, brightness);
94
95         return 0;
96 }
97
98 static int pwm_backlight_get_brightness(struct backlight_device *bl)
99 {
100         return bl->props.brightness;
101 }
102
103 static int pwm_backlight_check_fb(struct backlight_device *bl,
104                                   struct fb_info *info)
105 {
106         struct pwm_bl_data *pb = bl_get_data(bl);
107
108         return !pb->check_fb || pb->check_fb(pb->dev, info);
109 }
110
111 static const struct backlight_ops pwm_backlight_ops = {
112         .update_status  = pwm_backlight_update_status,
113         .get_brightness = pwm_backlight_get_brightness,
114         .check_fb       = pwm_backlight_check_fb,
115 };
116
117 #ifdef CONFIG_OF
118 static int pwm_backlight_parse_dt(struct device *dev,
119                                   struct platform_pwm_backlight_data *data)
120 {
121         struct device_node *node = dev->of_node;
122         struct property *prop;
123         int length;
124         u32 value;
125         int ret;
126
127         if (!node)
128                 return -ENODEV;
129
130         memset(data, 0, sizeof(*data));
131
132         /* determine the number of brightness levels */
133         prop = of_find_property(node, "brightness-levels", &length);
134         if (!prop)
135                 return -EINVAL;
136
137         data->max_brightness = length / sizeof(u32);
138
139         /* read brightness levels from DT property */
140         if (data->max_brightness > 0) {
141                 size_t size = sizeof(*data->levels) * data->max_brightness;
142
143                 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
144                 if (!data->levels)
145                         return -ENOMEM;
146
147                 ret = of_property_read_u32_array(node, "brightness-levels",
148                                                  data->levels,
149                                                  data->max_brightness);
150                 if (ret < 0)
151                         return ret;
152
153                 ret = of_property_read_u32(node, "default-brightness-level",
154                                            &value);
155                 if (ret < 0)
156                         return ret;
157
158                 data->dft_brightness = value;
159                 data->max_brightness--;
160         }
161
162         /*
163          * TODO: Most users of this driver use a number of GPIOs to control
164          *       backlight power. Support for specifying these needs to be
165          *       added.
166          */
167
168         return 0;
169 }
170
171 static struct of_device_id pwm_backlight_of_match[] = {
172         { .compatible = "pwm-backlight" },
173         { }
174 };
175
176 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
177 #else
178 static int pwm_backlight_parse_dt(struct device *dev,
179                                   struct platform_pwm_backlight_data *data)
180 {
181         return -ENODEV;
182 }
183 #endif
184
185 static int pwm_backlight_probe(struct platform_device *pdev)
186 {
187         struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
188         struct platform_pwm_backlight_data defdata;
189         struct backlight_properties props;
190         struct backlight_device *bl;
191         struct pwm_bl_data *pb;
192         unsigned int max;
193         int ret;
194
195         if (!data) {
196                 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
197                 if (ret < 0) {
198                         dev_err(&pdev->dev, "failed to find platform data\n");
199                         return ret;
200                 }
201
202                 data = &defdata;
203         }
204
205         if (data->init) {
206                 ret = data->init(&pdev->dev);
207                 if (ret < 0)
208                         return ret;
209         }
210
211         pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
212         if (!pb) {
213                 dev_err(&pdev->dev, "no memory for state\n");
214                 ret = -ENOMEM;
215                 goto err_alloc;
216         }
217
218         if (data->levels) {
219                 max = data->levels[data->max_brightness];
220                 pb->levels = data->levels;
221         } else
222                 max = data->max_brightness;
223
224         pb->notify = data->notify;
225         pb->notify_after = data->notify_after;
226         pb->check_fb = data->check_fb;
227         pb->exit = data->exit;
228         pb->dev = &pdev->dev;
229         pb->enabled = false;
230
231         pb->pwm = devm_pwm_get(&pdev->dev, NULL);
232         if (IS_ERR(pb->pwm)) {
233                 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
234
235                 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
236                 if (IS_ERR(pb->pwm)) {
237                         dev_err(&pdev->dev, "unable to request legacy PWM\n");
238                         ret = PTR_ERR(pb->pwm);
239                         goto err_alloc;
240                 }
241         }
242
243         dev_dbg(&pdev->dev, "got pwm for backlight\n");
244
245         /*
246          * The DT case will set the pwm_period_ns field to 0 and store the
247          * period, parsed from the DT, in the PWM device. For the non-DT case,
248          * set the period from platform data.
249          */
250         if (data->pwm_period_ns > 0)
251                 pwm_set_period(pb->pwm, data->pwm_period_ns);
252
253         pb->period = pwm_get_period(pb->pwm);
254         pb->lth_brightness = data->lth_brightness * (pb->period / max);
255
256         memset(&props, 0, sizeof(struct backlight_properties));
257         props.type = BACKLIGHT_RAW;
258         props.max_brightness = data->max_brightness;
259         bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
260                                        &pwm_backlight_ops, &props);
261         if (IS_ERR(bl)) {
262                 dev_err(&pdev->dev, "failed to register backlight\n");
263                 ret = PTR_ERR(bl);
264                 goto err_alloc;
265         }
266
267         if (data->dft_brightness > data->max_brightness) {
268                 dev_warn(&pdev->dev,
269                          "invalid default brightness level: %u, using %u\n",
270                          data->dft_brightness, data->max_brightness);
271                 data->dft_brightness = data->max_brightness;
272         }
273
274         bl->props.brightness = data->dft_brightness;
275         backlight_update_status(bl);
276
277         platform_set_drvdata(pdev, bl);
278         return 0;
279
280 err_alloc:
281         if (data->exit)
282                 data->exit(&pdev->dev);
283         return ret;
284 }
285
286 static int pwm_backlight_remove(struct platform_device *pdev)
287 {
288         struct backlight_device *bl = platform_get_drvdata(pdev);
289         struct pwm_bl_data *pb = bl_get_data(bl);
290
291         backlight_device_unregister(bl);
292         pwm_backlight_power_off(pb);
293
294         if (pb->exit)
295                 pb->exit(&pdev->dev);
296
297         return 0;
298 }
299
300 #ifdef CONFIG_PM_SLEEP
301 static int pwm_backlight_suspend(struct device *dev)
302 {
303         struct backlight_device *bl = dev_get_drvdata(dev);
304         struct pwm_bl_data *pb = bl_get_data(bl);
305
306         if (pb->notify)
307                 pb->notify(pb->dev, 0);
308
309         pwm_backlight_power_off(pb);
310
311         if (pb->notify_after)
312                 pb->notify_after(pb->dev, 0);
313
314         return 0;
315 }
316
317 static int pwm_backlight_resume(struct device *dev)
318 {
319         struct backlight_device *bl = dev_get_drvdata(dev);
320
321         backlight_update_status(bl);
322
323         return 0;
324 }
325 #endif
326
327 static SIMPLE_DEV_PM_OPS(pwm_backlight_pm_ops, pwm_backlight_suspend,
328                          pwm_backlight_resume);
329
330 static struct platform_driver pwm_backlight_driver = {
331         .driver         = {
332                 .name           = "pwm-backlight",
333                 .owner          = THIS_MODULE,
334                 .pm             = &pwm_backlight_pm_ops,
335                 .of_match_table = of_match_ptr(pwm_backlight_of_match),
336         },
337         .probe          = pwm_backlight_probe,
338         .remove         = pwm_backlight_remove,
339 };
340
341 module_platform_driver(pwm_backlight_driver);
342
343 MODULE_DESCRIPTION("PWM based Backlight Driver");
344 MODULE_LICENSE("GPL");
345 MODULE_ALIAS("platform:pwm-backlight");