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