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