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