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