2 * linux/drivers/video/backlight/pwm_bl.c
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
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.
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>
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>
28 struct pwm_device
*pwm
;
31 unsigned int lth_brightness
;
34 struct regulator
*power_supply
;
36 unsigned long enable_gpio_flags
;
38 int (*notify
)(struct device
*,
40 void (*notify_after
)(struct device
*,
42 int (*check_fb
)(struct device
*, struct fb_info
*);
43 void (*exit
)(struct device
*);
46 static void pwm_backlight_power_on(struct pwm_bl_data
*pb
, int brightness
)
53 err
= regulator_enable(pb
->power_supply
);
55 dev_err(pb
->dev
, "failed to enable power supply\n");
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);
61 gpio_set_value(pb
->enable_gpio
, 1);
68 static void pwm_backlight_power_off(struct pwm_bl_data
*pb
)
73 pwm_config(pb
->pwm
, 0, pb
->period
);
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);
80 gpio_set_value(pb
->enable_gpio
, 0);
83 regulator_disable(pb
->power_supply
);
87 static int compute_duty_cycle(struct pwm_bl_data
*pb
, int brightness
)
89 unsigned int lth
= pb
->lth_brightness
;
93 duty_cycle
= pb
->levels
[brightness
];
95 duty_cycle
= brightness
;
97 return (duty_cycle
* (pb
->period
- lth
) / pb
->scale
) + lth
;
100 static int pwm_backlight_update_status(struct backlight_device
*bl
)
102 struct pwm_bl_data
*pb
= bl_get_data(bl
);
103 int brightness
= bl
->props
.brightness
;
106 if (bl
->props
.power
!= FB_BLANK_UNBLANK
||
107 bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
||
108 bl
->props
.state
& BL_CORE_FBBLANK
)
112 brightness
= pb
->notify(pb
->dev
, brightness
);
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
);
119 pwm_backlight_power_off(pb
);
121 if (pb
->notify_after
)
122 pb
->notify_after(pb
->dev
, brightness
);
127 static int pwm_backlight_get_brightness(struct backlight_device
*bl
)
129 return bl
->props
.brightness
;
132 static int pwm_backlight_check_fb(struct backlight_device
*bl
,
133 struct fb_info
*info
)
135 struct pwm_bl_data
*pb
= bl_get_data(bl
);
137 return !pb
->check_fb
|| pb
->check_fb(pb
->dev
, info
);
140 static 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
,
147 static int pwm_backlight_parse_dt(struct device
*dev
,
148 struct platform_pwm_backlight_data
*data
)
150 struct device_node
*node
= dev
->of_node
;
151 enum of_gpio_flags flags
;
152 struct property
*prop
;
160 memset(data
, 0, sizeof(*data
));
162 /* determine the number of brightness levels */
163 prop
= of_find_property(node
, "brightness-levels", &length
);
167 data
->max_brightness
= length
/ sizeof(u32
);
169 /* read brightness levels from DT property */
170 if (data
->max_brightness
> 0) {
171 size_t size
= sizeof(*data
->levels
) * data
->max_brightness
;
173 data
->levels
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
177 ret
= of_property_read_u32_array(node
, "brightness-levels",
179 data
->max_brightness
);
183 ret
= of_property_read_u32(node
, "default-brightness-level",
188 data
->dft_brightness
= value
;
189 data
->max_brightness
--;
192 data
->enable_gpio
= of_get_named_gpio_flags(node
, "enable-gpios", 0,
194 if (data
->enable_gpio
== -EPROBE_DEFER
)
195 return -EPROBE_DEFER
;
197 if (gpio_is_valid(data
->enable_gpio
) && (flags
& OF_GPIO_ACTIVE_LOW
))
198 data
->enable_gpio_flags
|= PWM_BACKLIGHT_GPIO_ACTIVE_LOW
;
203 static struct of_device_id pwm_backlight_of_match
[] = {
204 { .compatible
= "pwm-backlight" },
208 MODULE_DEVICE_TABLE(of
, pwm_backlight_of_match
);
210 static int pwm_backlight_parse_dt(struct device
*dev
,
211 struct platform_pwm_backlight_data
*data
)
217 static int pwm_backlight_probe(struct platform_device
*pdev
)
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
;
227 ret
= pwm_backlight_parse_dt(&pdev
->dev
, &defdata
);
229 dev_err(&pdev
->dev
, "failed to find platform data\n");
237 ret
= data
->init(&pdev
->dev
);
242 pb
= devm_kzalloc(&pdev
->dev
, sizeof(*pb
), GFP_KERNEL
);
251 for (i
= 0; i
<= data
->max_brightness
; i
++)
252 if (data
->levels
[i
] > pb
->scale
)
253 pb
->scale
= data
->levels
[i
];
255 pb
->levels
= data
->levels
;
257 pb
->scale
= data
->max_brightness
;
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
;
268 if (gpio_is_valid(pb
->enable_gpio
)) {
271 if (pb
->enable_gpio_flags
& PWM_BACKLIGHT_GPIO_ACTIVE_LOW
)
272 flags
= GPIOF_OUT_INIT_HIGH
;
274 flags
= GPIOF_OUT_INIT_LOW
;
276 ret
= gpio_request_one(pb
->enable_gpio
, flags
, "enable");
278 dev_err(&pdev
->dev
, "failed to request GPIO#%d: %d\n",
279 pb
->enable_gpio
, ret
);
284 pb
->power_supply
= devm_regulator_get(&pdev
->dev
, "power");
285 if (IS_ERR(pb
->power_supply
)) {
286 ret
= PTR_ERR(pb
->power_supply
);
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");
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
);
302 dev_dbg(&pdev
->dev
, "got pwm for backlight\n");
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.
309 if (data
->pwm_period_ns
> 0)
310 pwm_set_period(pb
->pwm
, data
->pwm_period_ns
);
312 pb
->period
= pwm_get_period(pb
->pwm
);
313 pb
->lth_brightness
= data
->lth_brightness
* (pb
->period
/ pb
->scale
);
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
);
321 dev_err(&pdev
->dev
, "failed to register backlight\n");
326 if (data
->dft_brightness
> data
->max_brightness
) {
328 "invalid default brightness level: %u, using %u\n",
329 data
->dft_brightness
, data
->max_brightness
);
330 data
->dft_brightness
= data
->max_brightness
;
333 bl
->props
.brightness
= data
->dft_brightness
;
334 backlight_update_status(bl
);
336 platform_set_drvdata(pdev
, bl
);
340 if (gpio_is_valid(pb
->enable_gpio
))
341 gpio_free(pb
->enable_gpio
);
344 data
->exit(&pdev
->dev
);
348 static int pwm_backlight_remove(struct platform_device
*pdev
)
350 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
351 struct pwm_bl_data
*pb
= bl_get_data(bl
);
353 backlight_device_unregister(bl
);
354 pwm_backlight_power_off(pb
);
357 pb
->exit(&pdev
->dev
);
362 #ifdef CONFIG_PM_SLEEP
363 static int pwm_backlight_suspend(struct device
*dev
)
365 struct backlight_device
*bl
= dev_get_drvdata(dev
);
366 struct pwm_bl_data
*pb
= bl_get_data(bl
);
369 pb
->notify(pb
->dev
, 0);
371 pwm_backlight_power_off(pb
);
373 if (pb
->notify_after
)
374 pb
->notify_after(pb
->dev
, 0);
379 static int pwm_backlight_resume(struct device
*dev
)
381 struct backlight_device
*bl
= dev_get_drvdata(dev
);
383 backlight_update_status(bl
);
389 static 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
,
398 static struct platform_driver pwm_backlight_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
),
405 .probe
= pwm_backlight_probe
,
406 .remove
= pwm_backlight_remove
,
409 module_platform_driver(pwm_backlight_driver
);
411 MODULE_DESCRIPTION("PWM based Backlight Driver");
412 MODULE_LICENSE("GPL");
413 MODULE_ALIAS("platform:pwm-backlight");