GNU Linux-libre 4.14.257-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 const 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_initial_power_state(const struct pwm_bl_data *pb)
200 {
201         struct device_node *node = pb->dev->of_node;
202         bool active = true;
203
204         /*
205          * If the enable GPIO is present, observable (either as input
206          * or output) and off then the backlight is not currently active.
207          * */
208         if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0)
209                 active = false;
210
211         if (!regulator_is_enabled(pb->power_supply))
212                 active = false;
213
214         if (!pwm_is_enabled(pb->pwm))
215                 active = false;
216
217         /*
218          * Synchronize the enable_gpio with the observed state of the
219          * hardware.
220          */
221         if (pb->enable_gpio)
222                 gpiod_direction_output(pb->enable_gpio, active);
223
224         /*
225          * Do not change pb->enabled here! pb->enabled essentially
226          * tells us if we own one of the regulator's use counts and
227          * right now we do not.
228          */
229
230         /* Not booted with device tree or no phandle link to the node */
231         if (!node || !node->phandle)
232                 return FB_BLANK_UNBLANK;
233
234         /*
235          * If the driver is probed from the device tree and there is a
236          * phandle link pointing to the backlight node, it is safe to
237          * assume that another driver will enable the backlight at the
238          * appropriate time. Therefore, if it is disabled, keep it so.
239          */
240         return active ? FB_BLANK_UNBLANK: FB_BLANK_POWERDOWN;
241 }
242
243 static int pwm_backlight_probe(struct platform_device *pdev)
244 {
245         struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
246         struct platform_pwm_backlight_data defdata;
247         struct backlight_properties props;
248         struct backlight_device *bl;
249         struct device_node *node = pdev->dev.of_node;
250         struct pwm_bl_data *pb;
251         struct pwm_args pargs;
252         int ret;
253
254         if (!data) {
255                 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
256                 if (ret < 0) {
257                         dev_err(&pdev->dev, "failed to find platform data\n");
258                         return ret;
259                 }
260
261                 data = &defdata;
262         }
263
264         if (data->init) {
265                 ret = data->init(&pdev->dev);
266                 if (ret < 0)
267                         return ret;
268         }
269
270         pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
271         if (!pb) {
272                 ret = -ENOMEM;
273                 goto err_alloc;
274         }
275
276         if (data->levels) {
277                 unsigned int i;
278
279                 for (i = 0; i <= data->max_brightness; i++)
280                         if (data->levels[i] > pb->scale)
281                                 pb->scale = data->levels[i];
282
283                 pb->levels = data->levels;
284         } else
285                 pb->scale = data->max_brightness;
286
287         pb->notify = data->notify;
288         pb->notify_after = data->notify_after;
289         pb->check_fb = data->check_fb;
290         pb->exit = data->exit;
291         pb->dev = &pdev->dev;
292         pb->enabled = false;
293
294         pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
295                                                   GPIOD_ASIS);
296         if (IS_ERR(pb->enable_gpio)) {
297                 ret = PTR_ERR(pb->enable_gpio);
298                 goto err_alloc;
299         }
300
301         /*
302          * Compatibility fallback for drivers still using the integer GPIO
303          * platform data. Must go away soon.
304          */
305         if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
306                 ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
307                                             GPIOF_OUT_INIT_HIGH, "enable");
308                 if (ret < 0) {
309                         dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
310                                 data->enable_gpio, ret);
311                         goto err_alloc;
312                 }
313
314                 pb->enable_gpio = gpio_to_desc(data->enable_gpio);
315         }
316
317         pb->power_supply = devm_regulator_get(&pdev->dev, "power");
318         if (IS_ERR(pb->power_supply)) {
319                 ret = PTR_ERR(pb->power_supply);
320                 goto err_alloc;
321         }
322
323         pb->pwm = devm_pwm_get(&pdev->dev, NULL);
324         if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
325                 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
326                 pb->legacy = true;
327                 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
328         }
329
330         if (IS_ERR(pb->pwm)) {
331                 ret = PTR_ERR(pb->pwm);
332                 if (ret != -EPROBE_DEFER)
333                         dev_err(&pdev->dev, "unable to request PWM\n");
334                 goto err_alloc;
335         }
336
337         dev_dbg(&pdev->dev, "got pwm for backlight\n");
338
339         /*
340          * FIXME: pwm_apply_args() should be removed when switching to
341          * the atomic PWM API.
342          */
343         pwm_apply_args(pb->pwm);
344
345         /*
346          * The DT case will set the pwm_period_ns field to 0 and store the
347          * period, parsed from the DT, in the PWM device. For the non-DT case,
348          * set the period from platform data if it has not already been set
349          * via the PWM lookup table.
350          */
351         pwm_get_args(pb->pwm, &pargs);
352         pb->period = pargs.period;
353         if (!pb->period && (data->pwm_period_ns > 0))
354                 pb->period = data->pwm_period_ns;
355
356         pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
357
358         memset(&props, 0, sizeof(struct backlight_properties));
359         props.type = BACKLIGHT_RAW;
360         props.max_brightness = data->max_brightness;
361         bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
362                                        &pwm_backlight_ops, &props);
363         if (IS_ERR(bl)) {
364                 dev_err(&pdev->dev, "failed to register backlight\n");
365                 ret = PTR_ERR(bl);
366                 if (pb->legacy)
367                         pwm_free(pb->pwm);
368                 goto err_alloc;
369         }
370
371         if (data->dft_brightness > data->max_brightness) {
372                 dev_warn(&pdev->dev,
373                          "invalid default brightness level: %u, using %u\n",
374                          data->dft_brightness, data->max_brightness);
375                 data->dft_brightness = data->max_brightness;
376         }
377
378         bl->props.brightness = data->dft_brightness;
379         bl->props.power = pwm_backlight_initial_power_state(pb);
380         backlight_update_status(bl);
381
382         platform_set_drvdata(pdev, bl);
383         return 0;
384
385 err_alloc:
386         if (data->exit)
387                 data->exit(&pdev->dev);
388         return ret;
389 }
390
391 static int pwm_backlight_remove(struct platform_device *pdev)
392 {
393         struct backlight_device *bl = platform_get_drvdata(pdev);
394         struct pwm_bl_data *pb = bl_get_data(bl);
395
396         backlight_device_unregister(bl);
397         pwm_backlight_power_off(pb);
398
399         if (pb->exit)
400                 pb->exit(&pdev->dev);
401         if (pb->legacy)
402                 pwm_free(pb->pwm);
403
404         return 0;
405 }
406
407 static void pwm_backlight_shutdown(struct platform_device *pdev)
408 {
409         struct backlight_device *bl = platform_get_drvdata(pdev);
410         struct pwm_bl_data *pb = bl_get_data(bl);
411
412         pwm_backlight_power_off(pb);
413 }
414
415 #ifdef CONFIG_PM_SLEEP
416 static int pwm_backlight_suspend(struct device *dev)
417 {
418         struct backlight_device *bl = dev_get_drvdata(dev);
419         struct pwm_bl_data *pb = bl_get_data(bl);
420
421         if (pb->notify)
422                 pb->notify(pb->dev, 0);
423
424         pwm_backlight_power_off(pb);
425
426         if (pb->notify_after)
427                 pb->notify_after(pb->dev, 0);
428
429         return 0;
430 }
431
432 static int pwm_backlight_resume(struct device *dev)
433 {
434         struct backlight_device *bl = dev_get_drvdata(dev);
435
436         backlight_update_status(bl);
437
438         return 0;
439 }
440 #endif
441
442 static const struct dev_pm_ops pwm_backlight_pm_ops = {
443 #ifdef CONFIG_PM_SLEEP
444         .suspend = pwm_backlight_suspend,
445         .resume = pwm_backlight_resume,
446         .poweroff = pwm_backlight_suspend,
447         .restore = pwm_backlight_resume,
448 #endif
449 };
450
451 static struct platform_driver pwm_backlight_driver = {
452         .driver         = {
453                 .name           = "pwm-backlight",
454                 .pm             = &pwm_backlight_pm_ops,
455                 .of_match_table = of_match_ptr(pwm_backlight_of_match),
456         },
457         .probe          = pwm_backlight_probe,
458         .remove         = pwm_backlight_remove,
459         .shutdown       = pwm_backlight_shutdown,
460 };
461
462 module_platform_driver(pwm_backlight_driver);
463
464 MODULE_DESCRIPTION("PWM based Backlight Driver");
465 MODULE_LICENSE("GPL");
466 MODULE_ALIAS("platform:pwm-backlight");