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/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/gpio.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/platform_device.h>
21 #include <linux/backlight.h>
22 #include <linux/err.h>
23 #include <linux/pwm.h>
24 #include <linux/pwm_backlight.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/slab.h>
29 struct pwm_device
*pwm
;
31 unsigned int lth_brightness
;
33 struct regulator
*power_supply
;
34 struct gpio_desc
*enable_gpio
;
37 unsigned int post_pwm_on_delay
;
38 unsigned int pwm_off_delay
;
39 int (*notify
)(struct device
*,
41 void (*notify_after
)(struct device
*,
43 int (*check_fb
)(struct device
*, struct fb_info
*);
44 void (*exit
)(struct device
*);
47 static void pwm_backlight_power_on(struct pwm_bl_data
*pb
)
49 struct pwm_state state
;
52 pwm_get_state(pb
->pwm
, &state
);
56 err
= regulator_enable(pb
->power_supply
);
58 dev_err(pb
->dev
, "failed to enable power supply\n");
61 pwm_apply_state(pb
->pwm
, &state
);
63 if (pb
->post_pwm_on_delay
)
64 msleep(pb
->post_pwm_on_delay
);
67 gpiod_set_value_cansleep(pb
->enable_gpio
, 1);
70 static void pwm_backlight_power_off(struct pwm_bl_data
*pb
)
72 struct pwm_state state
;
74 pwm_get_state(pb
->pwm
, &state
);
79 gpiod_set_value_cansleep(pb
->enable_gpio
, 0);
81 if (pb
->pwm_off_delay
)
82 msleep(pb
->pwm_off_delay
);
84 state
.enabled
= false;
86 pwm_apply_state(pb
->pwm
, &state
);
88 regulator_disable(pb
->power_supply
);
91 static int compute_duty_cycle(struct pwm_bl_data
*pb
, int brightness
)
93 unsigned int lth
= pb
->lth_brightness
;
94 struct pwm_state state
;
97 pwm_get_state(pb
->pwm
, &state
);
100 duty_cycle
= pb
->levels
[brightness
];
102 duty_cycle
= brightness
;
104 duty_cycle
*= state
.period
- lth
;
105 do_div(duty_cycle
, pb
->scale
);
107 return duty_cycle
+ lth
;
110 static int pwm_backlight_update_status(struct backlight_device
*bl
)
112 struct pwm_bl_data
*pb
= bl_get_data(bl
);
113 int brightness
= bl
->props
.brightness
;
114 struct pwm_state state
;
116 if (bl
->props
.power
!= FB_BLANK_UNBLANK
||
117 bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
||
118 bl
->props
.state
& BL_CORE_FBBLANK
)
122 brightness
= pb
->notify(pb
->dev
, brightness
);
124 if (brightness
> 0) {
125 pwm_get_state(pb
->pwm
, &state
);
126 state
.duty_cycle
= compute_duty_cycle(pb
, brightness
);
127 pwm_apply_state(pb
->pwm
, &state
);
128 pwm_backlight_power_on(pb
);
130 pwm_backlight_power_off(pb
);
132 if (pb
->notify_after
)
133 pb
->notify_after(pb
->dev
, brightness
);
138 static int pwm_backlight_check_fb(struct backlight_device
*bl
,
139 struct fb_info
*info
)
141 struct pwm_bl_data
*pb
= bl_get_data(bl
);
143 return !pb
->check_fb
|| pb
->check_fb(pb
->dev
, info
);
146 static const struct backlight_ops pwm_backlight_ops
= {
147 .update_status
= pwm_backlight_update_status
,
148 .check_fb
= pwm_backlight_check_fb
,
152 #define PWM_LUMINANCE_SCALE 10000 /* luminance scale */
154 /* An integer based power function */
155 static u64
int_pow(u64 base
, int exp
)
170 * CIE lightness to PWM conversion.
172 * The CIE 1931 lightness formula is what actually describes how we perceive
174 * Y = (L* / 902.3) if L* ≤ 0.08856
175 * Y = ((L* + 16) / 116)^3 if L* > 0.08856
177 * Where Y is the luminance, the amount of light coming out of the screen, and
178 * is a number between 0.0 and 1.0; and L* is the lightness, how bright a human
179 * perceives the screen to be, and is a number between 0 and 100.
181 * The following function does the fixed point maths needed to implement the
184 static u64
cie1931(unsigned int lightness
, unsigned int scale
)
189 if (lightness
<= (8 * scale
)) {
190 retval
= DIV_ROUND_CLOSEST_ULL(lightness
* 10, 9023);
192 retval
= int_pow((lightness
+ (16 * scale
)) / 116, 3);
193 retval
= DIV_ROUND_CLOSEST_ULL(retval
, (scale
* scale
));
200 * Create a default correction table for PWM values to create linear brightness
201 * for LED based backlights using the CIE1931 algorithm.
204 int pwm_backlight_brightness_default(struct device
*dev
,
205 struct platform_pwm_backlight_data
*data
,
208 unsigned int counter
= 0;
213 * Count the number of bits needed to represent the period number. The
214 * number of bits is used to calculate the number of levels used for the
215 * brightness-levels table, the purpose of this calculation is have a
216 * pre-computed table with enough levels to get linear brightness
217 * perception. The period is divided by the number of bits so for a
218 * 8-bit PWM we have 255 / 8 = 32 brightness levels or for a 16-bit PWM
219 * we have 65535 / 16 = 4096 brightness levels.
221 * Note that this method is based on empirical testing on different
222 * devices with PWM of 8 and 16 bits of resolution.
230 data
->max_brightness
= DIV_ROUND_UP(period
, counter
);
231 data
->levels
= devm_kcalloc(dev
, data
->max_brightness
,
232 sizeof(*data
->levels
), GFP_KERNEL
);
236 /* Fill the table using the cie1931 algorithm */
237 for (i
= 0; i
< data
->max_brightness
; i
++) {
238 retval
= cie1931((i
* PWM_LUMINANCE_SCALE
) /
239 data
->max_brightness
, PWM_LUMINANCE_SCALE
) *
241 retval
= DIV_ROUND_CLOSEST_ULL(retval
, PWM_LUMINANCE_SCALE
);
242 if (retval
> UINT_MAX
)
244 data
->levels
[i
] = (unsigned int)retval
;
247 data
->dft_brightness
= data
->max_brightness
/ 2;
248 data
->max_brightness
--;
253 static int pwm_backlight_parse_dt(struct device
*dev
,
254 struct platform_pwm_backlight_data
*data
)
256 struct device_node
*node
= dev
->of_node
;
257 unsigned int num_levels
= 0;
258 unsigned int levels_count
;
259 unsigned int num_steps
= 0;
260 struct property
*prop
;
269 memset(data
, 0, sizeof(*data
));
272 * Determine the number of brightness levels, if this property is not
273 * set a default table of brightness levels will be used.
275 prop
= of_find_property(node
, "brightness-levels", &length
);
279 data
->max_brightness
= length
/ sizeof(u32
);
281 /* read brightness levels from DT property */
282 if (data
->max_brightness
> 0) {
283 size_t size
= sizeof(*data
->levels
) * data
->max_brightness
;
284 unsigned int i
, j
, n
= 0;
286 data
->levels
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
290 ret
= of_property_read_u32_array(node
, "brightness-levels",
292 data
->max_brightness
);
296 ret
= of_property_read_u32(node
, "default-brightness-level",
301 data
->dft_brightness
= value
;
304 * This property is optional, if is set enables linear
305 * interpolation between each of the values of brightness levels
306 * and creates a new pre-computed table.
308 of_property_read_u32(node
, "num-interpolated-steps",
312 * Make sure that there is at least two entries in the
313 * brightness-levels table, otherwise we can't interpolate
314 * between two points.
317 if (data
->max_brightness
< 2) {
318 dev_err(dev
, "can't interpolate\n");
323 * Recalculate the number of brightness levels, now
324 * taking in consideration the number of interpolated
325 * steps between two levels.
327 for (i
= 0; i
< data
->max_brightness
- 1; i
++) {
328 if ((data
->levels
[i
+ 1] - data
->levels
[i
]) /
330 num_levels
+= num_steps
;
335 dev_dbg(dev
, "new number of brightness levels: %d\n",
339 * Create a new table of brightness levels with all the
340 * interpolated steps.
342 size
= sizeof(*table
) * num_levels
;
343 table
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
347 /* Fill the interpolated table. */
349 for (i
= 0; i
< data
->max_brightness
- 1; i
++) {
350 value
= data
->levels
[i
];
351 n
= (data
->levels
[i
+ 1] - value
) / num_steps
;
353 for (j
= 0; j
< num_steps
; j
++) {
354 table
[levels_count
] = value
;
359 table
[levels_count
] = data
->levels
[i
];
363 table
[levels_count
] = data
->levels
[i
];
366 * As we use interpolation lets remove current
367 * brightness levels table and replace for the
368 * new interpolated table.
370 devm_kfree(dev
, data
->levels
);
371 data
->levels
= table
;
374 * Reassign max_brightness value to the new total number
375 * of brightness levels.
377 data
->max_brightness
= num_levels
;
380 data
->max_brightness
--;
384 * These values are optional and set as 0 by default, the out values
385 * are modified only if a valid u32 value can be decoded.
387 of_property_read_u32(node
, "post-pwm-on-delay-ms",
388 &data
->post_pwm_on_delay
);
389 of_property_read_u32(node
, "pwm-off-delay-ms", &data
->pwm_off_delay
);
391 data
->enable_gpio
= -EINVAL
;
395 static const struct of_device_id pwm_backlight_of_match
[] = {
396 { .compatible
= "pwm-backlight" },
400 MODULE_DEVICE_TABLE(of
, pwm_backlight_of_match
);
402 static int pwm_backlight_parse_dt(struct device
*dev
,
403 struct platform_pwm_backlight_data
*data
)
409 int pwm_backlight_brightness_default(struct device
*dev
,
410 struct platform_pwm_backlight_data
*data
,
417 static int pwm_backlight_initial_power_state(const struct pwm_bl_data
*pb
)
419 struct device_node
*node
= pb
->dev
->of_node
;
421 /* Not booted with device tree or no phandle link to the node */
422 if (!node
|| !node
->phandle
)
423 return FB_BLANK_UNBLANK
;
426 * If the driver is probed from the device tree and there is a
427 * phandle link pointing to the backlight node, it is safe to
428 * assume that another driver will enable the backlight at the
429 * appropriate time. Therefore, if it is disabled, keep it so.
432 /* if the enable GPIO is disabled, do not enable the backlight */
433 if (pb
->enable_gpio
&& gpiod_get_value(pb
->enable_gpio
) == 0)
434 return FB_BLANK_POWERDOWN
;
436 /* The regulator is disabled, do not enable the backlight */
437 if (!regulator_is_enabled(pb
->power_supply
))
438 return FB_BLANK_POWERDOWN
;
440 /* The PWM is disabled, keep it like this */
441 if (!pwm_is_enabled(pb
->pwm
))
442 return FB_BLANK_POWERDOWN
;
444 return FB_BLANK_UNBLANK
;
447 static int pwm_backlight_probe(struct platform_device
*pdev
)
449 struct platform_pwm_backlight_data
*data
= dev_get_platdata(&pdev
->dev
);
450 struct platform_pwm_backlight_data defdata
;
451 struct backlight_properties props
;
452 struct backlight_device
*bl
;
453 struct device_node
*node
= pdev
->dev
.of_node
;
454 struct pwm_bl_data
*pb
;
455 struct pwm_state state
;
460 ret
= pwm_backlight_parse_dt(&pdev
->dev
, &defdata
);
462 dev_err(&pdev
->dev
, "failed to find platform data\n");
470 ret
= data
->init(&pdev
->dev
);
475 pb
= devm_kzalloc(&pdev
->dev
, sizeof(*pb
), GFP_KERNEL
);
481 pb
->notify
= data
->notify
;
482 pb
->notify_after
= data
->notify_after
;
483 pb
->check_fb
= data
->check_fb
;
484 pb
->exit
= data
->exit
;
485 pb
->dev
= &pdev
->dev
;
486 pb
->post_pwm_on_delay
= data
->post_pwm_on_delay
;
487 pb
->pwm_off_delay
= data
->pwm_off_delay
;
489 pb
->enable_gpio
= devm_gpiod_get_optional(&pdev
->dev
, "enable",
491 if (IS_ERR(pb
->enable_gpio
)) {
492 ret
= PTR_ERR(pb
->enable_gpio
);
497 * Compatibility fallback for drivers still using the integer GPIO
498 * platform data. Must go away soon.
500 if (!pb
->enable_gpio
&& gpio_is_valid(data
->enable_gpio
)) {
501 ret
= devm_gpio_request_one(&pdev
->dev
, data
->enable_gpio
,
502 GPIOF_OUT_INIT_HIGH
, "enable");
504 dev_err(&pdev
->dev
, "failed to request GPIO#%d: %d\n",
505 data
->enable_gpio
, ret
);
509 pb
->enable_gpio
= gpio_to_desc(data
->enable_gpio
);
513 * If the GPIO is not known to be already configured as output, that
514 * is, if gpiod_get_direction returns either 1 or -EINVAL, change the
515 * direction to output and set the GPIO as active.
516 * Do not force the GPIO to active when it was already output as it
517 * could cause backlight flickering or we would enable the backlight too
518 * early. Leave the decision of the initial backlight state for later.
520 if (pb
->enable_gpio
&&
521 gpiod_get_direction(pb
->enable_gpio
) != 0)
522 gpiod_direction_output(pb
->enable_gpio
, 1);
524 pb
->power_supply
= devm_regulator_get(&pdev
->dev
, "power");
525 if (IS_ERR(pb
->power_supply
)) {
526 ret
= PTR_ERR(pb
->power_supply
);
530 pb
->pwm
= devm_pwm_get(&pdev
->dev
, NULL
);
531 if (IS_ERR(pb
->pwm
) && PTR_ERR(pb
->pwm
) != -EPROBE_DEFER
&& !node
) {
532 dev_err(&pdev
->dev
, "unable to request PWM, trying legacy API\n");
534 pb
->pwm
= pwm_request(data
->pwm_id
, "pwm-backlight");
537 if (IS_ERR(pb
->pwm
)) {
538 ret
= PTR_ERR(pb
->pwm
);
539 if (ret
!= -EPROBE_DEFER
)
540 dev_err(&pdev
->dev
, "unable to request PWM\n");
544 dev_dbg(&pdev
->dev
, "got pwm for backlight\n");
546 /* Sync up PWM state. */
547 pwm_init_state(pb
->pwm
, &state
);
550 * The DT case will set the pwm_period_ns field to 0 and store the
551 * period, parsed from the DT, in the PWM device. For the non-DT case,
552 * set the period from platform data if it has not already been set
553 * via the PWM lookup table.
555 if (!state
.period
&& (data
->pwm_period_ns
> 0))
556 state
.period
= data
->pwm_period_ns
;
558 ret
= pwm_apply_state(pb
->pwm
, &state
);
560 dev_err(&pdev
->dev
, "failed to apply initial PWM state: %d\n",
566 ret
= pwm_backlight_brightness_default(&pdev
->dev
, data
,
570 "failed to setup default brightness table\n");
575 for (i
= 0; i
<= data
->max_brightness
; i
++) {
576 if (data
->levels
[i
] > pb
->scale
)
577 pb
->scale
= data
->levels
[i
];
579 pb
->levels
= data
->levels
;
582 pb
->lth_brightness
= data
->lth_brightness
* (state
.period
/ pb
->scale
);
584 memset(&props
, 0, sizeof(struct backlight_properties
));
585 props
.type
= BACKLIGHT_RAW
;
586 props
.max_brightness
= data
->max_brightness
;
587 bl
= backlight_device_register(dev_name(&pdev
->dev
), &pdev
->dev
, pb
,
588 &pwm_backlight_ops
, &props
);
590 dev_err(&pdev
->dev
, "failed to register backlight\n");
597 if (data
->dft_brightness
> data
->max_brightness
) {
599 "invalid default brightness level: %u, using %u\n",
600 data
->dft_brightness
, data
->max_brightness
);
601 data
->dft_brightness
= data
->max_brightness
;
604 bl
->props
.brightness
= data
->dft_brightness
;
605 bl
->props
.power
= pwm_backlight_initial_power_state(pb
);
606 backlight_update_status(bl
);
608 platform_set_drvdata(pdev
, bl
);
613 data
->exit(&pdev
->dev
);
617 static int pwm_backlight_remove(struct platform_device
*pdev
)
619 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
620 struct pwm_bl_data
*pb
= bl_get_data(bl
);
622 backlight_device_unregister(bl
);
623 pwm_backlight_power_off(pb
);
626 pb
->exit(&pdev
->dev
);
633 static void pwm_backlight_shutdown(struct platform_device
*pdev
)
635 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
636 struct pwm_bl_data
*pb
= bl_get_data(bl
);
638 pwm_backlight_power_off(pb
);
641 #ifdef CONFIG_PM_SLEEP
642 static int pwm_backlight_suspend(struct device
*dev
)
644 struct backlight_device
*bl
= dev_get_drvdata(dev
);
645 struct pwm_bl_data
*pb
= bl_get_data(bl
);
648 pb
->notify(pb
->dev
, 0);
650 pwm_backlight_power_off(pb
);
652 if (pb
->notify_after
)
653 pb
->notify_after(pb
->dev
, 0);
658 static int pwm_backlight_resume(struct device
*dev
)
660 struct backlight_device
*bl
= dev_get_drvdata(dev
);
662 backlight_update_status(bl
);
668 static const struct dev_pm_ops pwm_backlight_pm_ops
= {
669 #ifdef CONFIG_PM_SLEEP
670 .suspend
= pwm_backlight_suspend
,
671 .resume
= pwm_backlight_resume
,
672 .poweroff
= pwm_backlight_suspend
,
673 .restore
= pwm_backlight_resume
,
677 static struct platform_driver pwm_backlight_driver
= {
679 .name
= "pwm-backlight",
680 .pm
= &pwm_backlight_pm_ops
,
681 .of_match_table
= of_match_ptr(pwm_backlight_of_match
),
683 .probe
= pwm_backlight_probe
,
684 .remove
= pwm_backlight_remove
,
685 .shutdown
= pwm_backlight_shutdown
,
688 module_platform_driver(pwm_backlight_driver
);
690 MODULE_DESCRIPTION("PWM based Backlight Driver");
691 MODULE_LICENSE("GPL");
692 MODULE_ALIAS("platform:pwm-backlight");