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/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>
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
;
35 struct gpio_desc
*enable_gpio
;
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");
58 gpiod_set_value_cansleep(pb
->enable_gpio
, 1);
64 static void pwm_backlight_power_off(struct pwm_bl_data
*pb
)
69 pwm_config(pb
->pwm
, 0, pb
->period
);
73 gpiod_set_value_cansleep(pb
->enable_gpio
, 0);
75 regulator_disable(pb
->power_supply
);
79 static int compute_duty_cycle(struct pwm_bl_data
*pb
, int brightness
)
81 unsigned int lth
= pb
->lth_brightness
;
85 duty_cycle
= pb
->levels
[brightness
];
87 duty_cycle
= brightness
;
89 return (duty_cycle
* (pb
->period
- lth
) / pb
->scale
) + lth
;
92 static int pwm_backlight_update_status(struct backlight_device
*bl
)
94 struct pwm_bl_data
*pb
= bl_get_data(bl
);
95 int brightness
= bl
->props
.brightness
;
98 if (bl
->props
.power
!= FB_BLANK_UNBLANK
||
99 bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
||
100 bl
->props
.state
& BL_CORE_FBBLANK
)
104 brightness
= pb
->notify(pb
->dev
, brightness
);
106 if (brightness
> 0) {
107 duty_cycle
= compute_duty_cycle(pb
, brightness
);
108 pwm_config(pb
->pwm
, duty_cycle
, pb
->period
);
109 pwm_backlight_power_on(pb
, brightness
);
111 pwm_backlight_power_off(pb
);
113 if (pb
->notify_after
)
114 pb
->notify_after(pb
->dev
, brightness
);
119 static int pwm_backlight_check_fb(struct backlight_device
*bl
,
120 struct fb_info
*info
)
122 struct pwm_bl_data
*pb
= bl_get_data(bl
);
124 return !pb
->check_fb
|| pb
->check_fb(pb
->dev
, info
);
127 static const struct backlight_ops pwm_backlight_ops
= {
128 .update_status
= pwm_backlight_update_status
,
129 .check_fb
= pwm_backlight_check_fb
,
133 static int pwm_backlight_parse_dt(struct device
*dev
,
134 struct platform_pwm_backlight_data
*data
)
136 struct device_node
*node
= dev
->of_node
;
137 struct property
*prop
;
145 memset(data
, 0, sizeof(*data
));
147 /* determine the number of brightness levels */
148 prop
= of_find_property(node
, "brightness-levels", &length
);
152 data
->max_brightness
= length
/ sizeof(u32
);
154 /* read brightness levels from DT property */
155 if (data
->max_brightness
> 0) {
156 size_t size
= sizeof(*data
->levels
) * data
->max_brightness
;
158 data
->levels
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
162 ret
= of_property_read_u32_array(node
, "brightness-levels",
164 data
->max_brightness
);
168 ret
= of_property_read_u32(node
, "default-brightness-level",
173 data
->dft_brightness
= value
;
174 data
->max_brightness
--;
177 data
->enable_gpio
= -EINVAL
;
181 static struct of_device_id pwm_backlight_of_match
[] = {
182 { .compatible
= "pwm-backlight" },
186 MODULE_DEVICE_TABLE(of
, pwm_backlight_of_match
);
188 static int pwm_backlight_parse_dt(struct device
*dev
,
189 struct platform_pwm_backlight_data
*data
)
195 static int pwm_backlight_probe(struct platform_device
*pdev
)
197 struct platform_pwm_backlight_data
*data
= dev_get_platdata(&pdev
->dev
);
198 struct platform_pwm_backlight_data defdata
;
199 struct backlight_properties props
;
200 struct backlight_device
*bl
;
201 struct device_node
*node
= pdev
->dev
.of_node
;
202 struct pwm_bl_data
*pb
;
203 int initial_blank
= FB_BLANK_UNBLANK
;
204 struct pwm_args pargs
;
208 ret
= pwm_backlight_parse_dt(&pdev
->dev
, &defdata
);
210 dev_err(&pdev
->dev
, "failed to find platform data\n");
218 ret
= data
->init(&pdev
->dev
);
223 pb
= devm_kzalloc(&pdev
->dev
, sizeof(*pb
), GFP_KERNEL
);
232 for (i
= 0; i
<= data
->max_brightness
; i
++)
233 if (data
->levels
[i
] > pb
->scale
)
234 pb
->scale
= data
->levels
[i
];
236 pb
->levels
= data
->levels
;
238 pb
->scale
= data
->max_brightness
;
240 pb
->notify
= data
->notify
;
241 pb
->notify_after
= data
->notify_after
;
242 pb
->check_fb
= data
->check_fb
;
243 pb
->exit
= data
->exit
;
244 pb
->dev
= &pdev
->dev
;
247 pb
->enable_gpio
= devm_gpiod_get_optional(&pdev
->dev
, "enable",
249 if (IS_ERR(pb
->enable_gpio
)) {
250 ret
= PTR_ERR(pb
->enable_gpio
);
255 * Compatibility fallback for drivers still using the integer GPIO
256 * platform data. Must go away soon.
258 if (!pb
->enable_gpio
&& gpio_is_valid(data
->enable_gpio
)) {
259 ret
= devm_gpio_request_one(&pdev
->dev
, data
->enable_gpio
,
260 GPIOF_OUT_INIT_HIGH
, "enable");
262 dev_err(&pdev
->dev
, "failed to request GPIO#%d: %d\n",
263 data
->enable_gpio
, ret
);
267 pb
->enable_gpio
= gpio_to_desc(data
->enable_gpio
);
270 if (pb
->enable_gpio
) {
272 * If the driver is probed from the device tree and there is a
273 * phandle link pointing to the backlight node, it is safe to
274 * assume that another driver will enable the backlight at the
275 * appropriate time. Therefore, if it is disabled, keep it so.
277 if (node
&& node
->phandle
&&
278 gpiod_get_direction(pb
->enable_gpio
) == GPIOF_DIR_OUT
&&
279 gpiod_get_value(pb
->enable_gpio
) == 0)
280 initial_blank
= FB_BLANK_POWERDOWN
;
282 gpiod_direction_output(pb
->enable_gpio
, 1);
285 pb
->power_supply
= devm_regulator_get(&pdev
->dev
, "power");
286 if (IS_ERR(pb
->power_supply
)) {
287 ret
= PTR_ERR(pb
->power_supply
);
291 if (node
&& node
->phandle
&& !regulator_is_enabled(pb
->power_supply
))
292 initial_blank
= FB_BLANK_POWERDOWN
;
294 pb
->pwm
= devm_pwm_get(&pdev
->dev
, NULL
);
295 if (IS_ERR(pb
->pwm
) && PTR_ERR(pb
->pwm
) != -EPROBE_DEFER
&& !node
) {
296 dev_err(&pdev
->dev
, "unable to request PWM, trying legacy API\n");
298 pb
->pwm
= pwm_request(data
->pwm_id
, "pwm-backlight");
301 if (IS_ERR(pb
->pwm
)) {
302 ret
= PTR_ERR(pb
->pwm
);
303 if (ret
!= -EPROBE_DEFER
)
304 dev_err(&pdev
->dev
, "unable to request PWM\n");
308 dev_dbg(&pdev
->dev
, "got pwm for backlight\n");
311 * FIXME: pwm_apply_args() should be removed when switching to
312 * the atomic PWM API.
314 pwm_apply_args(pb
->pwm
);
317 * The DT case will set the pwm_period_ns field to 0 and store the
318 * period, parsed from the DT, in the PWM device. For the non-DT case,
319 * set the period from platform data if it has not already been set
320 * via the PWM lookup table.
322 pwm_get_args(pb
->pwm
, &pargs
);
323 pb
->period
= pargs
.period
;
324 if (!pb
->period
&& (data
->pwm_period_ns
> 0))
325 pb
->period
= data
->pwm_period_ns
;
327 pb
->lth_brightness
= data
->lth_brightness
* (pb
->period
/ pb
->scale
);
329 memset(&props
, 0, sizeof(struct backlight_properties
));
330 props
.type
= BACKLIGHT_RAW
;
331 props
.max_brightness
= data
->max_brightness
;
332 bl
= backlight_device_register(dev_name(&pdev
->dev
), &pdev
->dev
, pb
,
333 &pwm_backlight_ops
, &props
);
335 dev_err(&pdev
->dev
, "failed to register backlight\n");
342 if (data
->dft_brightness
> data
->max_brightness
) {
344 "invalid default brightness level: %u, using %u\n",
345 data
->dft_brightness
, data
->max_brightness
);
346 data
->dft_brightness
= data
->max_brightness
;
349 bl
->props
.brightness
= data
->dft_brightness
;
350 bl
->props
.power
= initial_blank
;
351 backlight_update_status(bl
);
353 platform_set_drvdata(pdev
, bl
);
358 data
->exit(&pdev
->dev
);
362 static int pwm_backlight_remove(struct platform_device
*pdev
)
364 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
365 struct pwm_bl_data
*pb
= bl_get_data(bl
);
367 backlight_device_unregister(bl
);
368 pwm_backlight_power_off(pb
);
371 pb
->exit(&pdev
->dev
);
378 static void pwm_backlight_shutdown(struct platform_device
*pdev
)
380 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
381 struct pwm_bl_data
*pb
= bl_get_data(bl
);
383 pwm_backlight_power_off(pb
);
386 #ifdef CONFIG_PM_SLEEP
387 static int pwm_backlight_suspend(struct device
*dev
)
389 struct backlight_device
*bl
= dev_get_drvdata(dev
);
390 struct pwm_bl_data
*pb
= bl_get_data(bl
);
393 pb
->notify(pb
->dev
, 0);
395 pwm_backlight_power_off(pb
);
397 if (pb
->notify_after
)
398 pb
->notify_after(pb
->dev
, 0);
403 static int pwm_backlight_resume(struct device
*dev
)
405 struct backlight_device
*bl
= dev_get_drvdata(dev
);
407 backlight_update_status(bl
);
413 static const struct dev_pm_ops pwm_backlight_pm_ops
= {
414 #ifdef CONFIG_PM_SLEEP
415 .suspend
= pwm_backlight_suspend
,
416 .resume
= pwm_backlight_resume
,
417 .poweroff
= pwm_backlight_suspend
,
418 .restore
= pwm_backlight_resume
,
422 static struct platform_driver pwm_backlight_driver
= {
424 .name
= "pwm-backlight",
425 .pm
= &pwm_backlight_pm_ops
,
426 .of_match_table
= of_match_ptr(pwm_backlight_of_match
),
428 .probe
= pwm_backlight_probe
,
429 .remove
= pwm_backlight_remove
,
430 .shutdown
= pwm_backlight_shutdown
,
433 module_platform_driver(pwm_backlight_driver
);
435 MODULE_DESCRIPTION("PWM based Backlight Driver");
436 MODULE_LICENSE("GPL");
437 MODULE_ALIAS("platform:pwm-backlight");