GNU Linux-libre 4.9.332-gnu1
[releases.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/gpio/consumer.h>
14 #include <linux/gpio.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/platform_device.h>
19 #include <linux/fb.h>
20 #include <linux/backlight.h>
21 #include <linux/err.h>
22 #include <linux/pwm.h>
23 #include <linux/pwm_backlight.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/slab.h>
26
27 struct pwm_bl_data {
28         struct pwm_device       *pwm;
29         struct device           *dev;
30         unsigned int            period;
31         unsigned int            lth_brightness;
32         unsigned int            *levels;
33         bool                    enabled;
34         struct regulator        *power_supply;
35         struct gpio_desc        *enable_gpio;
36         unsigned int            scale;
37         bool                    legacy;
38         int                     (*notify)(struct device *,
39                                           int brightness);
40         void                    (*notify_after)(struct device *,
41                                         int brightness);
42         int                     (*check_fb)(struct device *, struct fb_info *);
43         void                    (*exit)(struct device *);
44 };
45
46 static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
47 {
48         int err;
49
50         if (pb->enabled)
51                 return;
52
53         err = regulator_enable(pb->power_supply);
54         if (err < 0)
55                 dev_err(pb->dev, "failed to enable power supply\n");
56
57         pwm_enable(pb->pwm);
58
59         if (pb->enable_gpio)
60                 gpiod_set_value_cansleep(pb->enable_gpio, 1);
61
62         pb->enabled = true;
63 }
64
65 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
66 {
67         if (!pb->enabled)
68                 return;
69
70         if (pb->enable_gpio)
71                 gpiod_set_value_cansleep(pb->enable_gpio, 0);
72
73         pwm_config(pb->pwm, 0, pb->period);
74         pwm_disable(pb->pwm);
75
76         regulator_disable(pb->power_supply);
77         pb->enabled = false;
78 }
79
80 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
81 {
82         unsigned int lth = pb->lth_brightness;
83         u64 duty_cycle;
84
85         if (pb->levels)
86                 duty_cycle = pb->levels[brightness];
87         else
88                 duty_cycle = brightness;
89
90         duty_cycle *= pb->period - lth;
91         do_div(duty_cycle, pb->scale);
92
93         return duty_cycle + lth;
94 }
95
96 static int pwm_backlight_update_status(struct backlight_device *bl)
97 {
98         struct pwm_bl_data *pb = bl_get_data(bl);
99         int brightness = bl->props.brightness;
100         int duty_cycle;
101
102         if (bl->props.power != FB_BLANK_UNBLANK ||
103             bl->props.fb_blank != FB_BLANK_UNBLANK ||
104             bl->props.state & BL_CORE_FBBLANK)
105                 brightness = 0;
106
107         if (pb->notify)
108                 brightness = pb->notify(pb->dev, brightness);
109
110         if (brightness > 0) {
111                 duty_cycle = compute_duty_cycle(pb, brightness);
112                 pwm_config(pb->pwm, duty_cycle, pb->period);
113                 pwm_backlight_power_on(pb, brightness);
114         } else
115                 pwm_backlight_power_off(pb);
116
117         if (pb->notify_after)
118                 pb->notify_after(pb->dev, brightness);
119
120         return 0;
121 }
122
123 static int pwm_backlight_check_fb(struct backlight_device *bl,
124                                   struct fb_info *info)
125 {
126         struct pwm_bl_data *pb = bl_get_data(bl);
127
128         return !pb->check_fb || pb->check_fb(pb->dev, info);
129 }
130
131 static const struct backlight_ops pwm_backlight_ops = {
132         .update_status  = pwm_backlight_update_status,
133         .check_fb       = pwm_backlight_check_fb,
134 };
135
136 #ifdef CONFIG_OF
137 static int pwm_backlight_parse_dt(struct device *dev,
138                                   struct platform_pwm_backlight_data *data)
139 {
140         struct device_node *node = dev->of_node;
141         struct property *prop;
142         int length;
143         u32 value;
144         int ret;
145
146         if (!node)
147                 return -ENODEV;
148
149         memset(data, 0, sizeof(*data));
150
151         /* determine the number of brightness levels */
152         prop = of_find_property(node, "brightness-levels", &length);
153         if (!prop)
154                 return -EINVAL;
155
156         data->max_brightness = length / sizeof(u32);
157
158         /* read brightness levels from DT property */
159         if (data->max_brightness > 0) {
160                 size_t size = sizeof(*data->levels) * data->max_brightness;
161
162                 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
163                 if (!data->levels)
164                         return -ENOMEM;
165
166                 ret = of_property_read_u32_array(node, "brightness-levels",
167                                                  data->levels,
168                                                  data->max_brightness);
169                 if (ret < 0)
170                         return ret;
171
172                 ret = of_property_read_u32(node, "default-brightness-level",
173                                            &value);
174                 if (ret < 0)
175                         return ret;
176
177                 data->dft_brightness = value;
178                 data->max_brightness--;
179         }
180
181         data->enable_gpio = -EINVAL;
182         return 0;
183 }
184
185 static struct of_device_id pwm_backlight_of_match[] = {
186         { .compatible = "pwm-backlight" },
187         { }
188 };
189
190 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
191 #else
192 static int pwm_backlight_parse_dt(struct device *dev,
193                                   struct platform_pwm_backlight_data *data)
194 {
195         return -ENODEV;
196 }
197 #endif
198
199 static int pwm_backlight_probe(struct platform_device *pdev)
200 {
201         struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
202         struct platform_pwm_backlight_data defdata;
203         struct backlight_properties props;
204         struct backlight_device *bl;
205         struct device_node *node = pdev->dev.of_node;
206         struct pwm_bl_data *pb;
207         int initial_blank = FB_BLANK_UNBLANK;
208         struct pwm_args pargs;
209         int ret;
210
211         if (!data) {
212                 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
213                 if (ret < 0) {
214                         dev_err(&pdev->dev, "failed to find platform data\n");
215                         return ret;
216                 }
217
218                 data = &defdata;
219         }
220
221         if (data->init) {
222                 ret = data->init(&pdev->dev);
223                 if (ret < 0)
224                         return ret;
225         }
226
227         pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
228         if (!pb) {
229                 ret = -ENOMEM;
230                 goto err_alloc;
231         }
232
233         if (data->levels) {
234                 unsigned int i;
235
236                 for (i = 0; i <= data->max_brightness; i++)
237                         if (data->levels[i] > pb->scale)
238                                 pb->scale = data->levels[i];
239
240                 pb->levels = data->levels;
241         } else
242                 pb->scale = data->max_brightness;
243
244         pb->notify = data->notify;
245         pb->notify_after = data->notify_after;
246         pb->check_fb = data->check_fb;
247         pb->exit = data->exit;
248         pb->dev = &pdev->dev;
249         pb->enabled = false;
250
251         pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
252                                                   GPIOD_ASIS);
253         if (IS_ERR(pb->enable_gpio)) {
254                 ret = PTR_ERR(pb->enable_gpio);
255                 goto err_alloc;
256         }
257
258         /*
259          * Compatibility fallback for drivers still using the integer GPIO
260          * platform data. Must go away soon.
261          */
262         if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
263                 ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
264                                             GPIOF_OUT_INIT_HIGH, "enable");
265                 if (ret < 0) {
266                         dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
267                                 data->enable_gpio, ret);
268                         goto err_alloc;
269                 }
270
271                 pb->enable_gpio = gpio_to_desc(data->enable_gpio);
272         }
273
274         if (pb->enable_gpio) {
275                 /*
276                  * If the driver is probed from the device tree and there is a
277                  * phandle link pointing to the backlight node, it is safe to
278                  * assume that another driver will enable the backlight at the
279                  * appropriate time. Therefore, if it is disabled, keep it so.
280                  */
281                 if (node && node->phandle &&
282                     gpiod_get_direction(pb->enable_gpio) == GPIOF_DIR_OUT &&
283                     gpiod_get_value(pb->enable_gpio) == 0)
284                         initial_blank = FB_BLANK_POWERDOWN;
285                 else
286                         gpiod_direction_output(pb->enable_gpio, 1);
287         }
288
289         pb->power_supply = devm_regulator_get(&pdev->dev, "power");
290         if (IS_ERR(pb->power_supply)) {
291                 ret = PTR_ERR(pb->power_supply);
292                 goto err_alloc;
293         }
294
295         if (node && node->phandle && !regulator_is_enabled(pb->power_supply))
296                 initial_blank = FB_BLANK_POWERDOWN;
297
298         pb->pwm = devm_pwm_get(&pdev->dev, NULL);
299         if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
300                 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
301                 pb->legacy = true;
302                 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
303         }
304
305         if (IS_ERR(pb->pwm)) {
306                 ret = PTR_ERR(pb->pwm);
307                 if (ret != -EPROBE_DEFER)
308                         dev_err(&pdev->dev, "unable to request PWM\n");
309                 goto err_alloc;
310         }
311
312         dev_dbg(&pdev->dev, "got pwm for backlight\n");
313
314         /*
315          * FIXME: pwm_apply_args() should be removed when switching to
316          * the atomic PWM API.
317          */
318         pwm_apply_args(pb->pwm);
319
320         /*
321          * The DT case will set the pwm_period_ns field to 0 and store the
322          * period, parsed from the DT, in the PWM device. For the non-DT case,
323          * set the period from platform data if it has not already been set
324          * via the PWM lookup table.
325          */
326         pwm_get_args(pb->pwm, &pargs);
327         pb->period = pargs.period;
328         if (!pb->period && (data->pwm_period_ns > 0))
329                 pb->period = data->pwm_period_ns;
330
331         pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
332
333         memset(&props, 0, sizeof(struct backlight_properties));
334         props.type = BACKLIGHT_RAW;
335         props.max_brightness = data->max_brightness;
336         bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
337                                        &pwm_backlight_ops, &props);
338         if (IS_ERR(bl)) {
339                 dev_err(&pdev->dev, "failed to register backlight\n");
340                 ret = PTR_ERR(bl);
341                 if (pb->legacy)
342                         pwm_free(pb->pwm);
343                 goto err_alloc;
344         }
345
346         if (data->dft_brightness > data->max_brightness) {
347                 dev_warn(&pdev->dev,
348                          "invalid default brightness level: %u, using %u\n",
349                          data->dft_brightness, data->max_brightness);
350                 data->dft_brightness = data->max_brightness;
351         }
352
353         bl->props.brightness = data->dft_brightness;
354         bl->props.power = initial_blank;
355         backlight_update_status(bl);
356
357         platform_set_drvdata(pdev, bl);
358         return 0;
359
360 err_alloc:
361         if (data->exit)
362                 data->exit(&pdev->dev);
363         return ret;
364 }
365
366 static int pwm_backlight_remove(struct platform_device *pdev)
367 {
368         struct backlight_device *bl = platform_get_drvdata(pdev);
369         struct pwm_bl_data *pb = bl_get_data(bl);
370
371         backlight_device_unregister(bl);
372         pwm_backlight_power_off(pb);
373
374         if (pb->exit)
375                 pb->exit(&pdev->dev);
376         if (pb->legacy)
377                 pwm_free(pb->pwm);
378
379         return 0;
380 }
381
382 static void pwm_backlight_shutdown(struct platform_device *pdev)
383 {
384         struct backlight_device *bl = platform_get_drvdata(pdev);
385         struct pwm_bl_data *pb = bl_get_data(bl);
386
387         pwm_backlight_power_off(pb);
388 }
389
390 #ifdef CONFIG_PM_SLEEP
391 static int pwm_backlight_suspend(struct device *dev)
392 {
393         struct backlight_device *bl = dev_get_drvdata(dev);
394         struct pwm_bl_data *pb = bl_get_data(bl);
395
396         if (pb->notify)
397                 pb->notify(pb->dev, 0);
398
399         pwm_backlight_power_off(pb);
400
401         if (pb->notify_after)
402                 pb->notify_after(pb->dev, 0);
403
404         return 0;
405 }
406
407 static int pwm_backlight_resume(struct device *dev)
408 {
409         struct backlight_device *bl = dev_get_drvdata(dev);
410
411         backlight_update_status(bl);
412
413         return 0;
414 }
415 #endif
416
417 static const struct dev_pm_ops pwm_backlight_pm_ops = {
418 #ifdef CONFIG_PM_SLEEP
419         .suspend = pwm_backlight_suspend,
420         .resume = pwm_backlight_resume,
421         .poweroff = pwm_backlight_suspend,
422         .restore = pwm_backlight_resume,
423 #endif
424 };
425
426 static struct platform_driver pwm_backlight_driver = {
427         .driver         = {
428                 .name           = "pwm-backlight",
429                 .pm             = &pwm_backlight_pm_ops,
430                 .of_match_table = of_match_ptr(pwm_backlight_of_match),
431         },
432         .probe          = pwm_backlight_probe,
433         .remove         = pwm_backlight_remove,
434         .shutdown       = pwm_backlight_shutdown,
435 };
436
437 module_platform_driver(pwm_backlight_driver);
438
439 MODULE_DESCRIPTION("PWM based Backlight Driver");
440 MODULE_LICENSE("GPL");
441 MODULE_ALIAS("platform:pwm-backlight");