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;
32 unsigned int lth_brightness;
35 struct regulator *power_supply;
36 struct gpio_desc *enable_gpio;
39 unsigned int post_pwm_on_delay;
40 unsigned int pwm_off_delay;
41 int (*notify)(struct device *,
43 void (*notify_after)(struct device *,
45 int (*check_fb)(struct device *, struct fb_info *);
46 void (*exit)(struct device *);
49 static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
56 err = regulator_enable(pb->power_supply);
58 dev_err(pb->dev, "failed to enable power supply\n");
62 if (pb->post_pwm_on_delay)
63 msleep(pb->post_pwm_on_delay);
66 gpiod_set_value_cansleep(pb->enable_gpio, 1);
71 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
77 gpiod_set_value_cansleep(pb->enable_gpio, 0);
79 if (pb->pwm_off_delay)
80 msleep(pb->pwm_off_delay);
82 pwm_config(pb->pwm, 0, pb->period);
85 regulator_disable(pb->power_supply);
89 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
91 unsigned int lth = pb->lth_brightness;
95 duty_cycle = pb->levels[brightness];
97 duty_cycle = brightness;
99 duty_cycle *= pb->period - lth;
100 do_div(duty_cycle, pb->scale);
102 return duty_cycle + lth;
105 static int pwm_backlight_update_status(struct backlight_device *bl)
107 struct pwm_bl_data *pb = bl_get_data(bl);
108 int brightness = bl->props.brightness;
111 if (bl->props.power != FB_BLANK_UNBLANK ||
112 bl->props.fb_blank != FB_BLANK_UNBLANK ||
113 bl->props.state & BL_CORE_FBBLANK)
117 brightness = pb->notify(pb->dev, brightness);
119 if (brightness > 0) {
120 duty_cycle = compute_duty_cycle(pb, brightness);
121 pwm_config(pb->pwm, duty_cycle, pb->period);
122 pwm_backlight_power_on(pb, brightness);
124 pwm_backlight_power_off(pb);
126 if (pb->notify_after)
127 pb->notify_after(pb->dev, 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 .check_fb = pwm_backlight_check_fb,
146 #define PWM_LUMINANCE_SCALE 10000 /* luminance scale */
148 /* An integer based power function */
149 static u64 int_pow(u64 base, int exp)
164 * CIE lightness to PWM conversion.
166 * The CIE 1931 lightness formula is what actually describes how we perceive
168 * Y = (L* / 902.3) if L* ≤ 0.08856
169 * Y = ((L* + 16) / 116)^3 if L* > 0.08856
171 * Where Y is the luminance, the amount of light coming out of the screen, and
172 * is a number between 0.0 and 1.0; and L* is the lightness, how bright a human
173 * perceives the screen to be, and is a number between 0 and 100.
175 * The following function does the fixed point maths needed to implement the
178 static u64 cie1931(unsigned int lightness, unsigned int scale)
183 if (lightness <= (8 * scale)) {
184 retval = DIV_ROUND_CLOSEST_ULL(lightness * 10, 9023);
186 retval = int_pow((lightness + (16 * scale)) / 116, 3);
187 retval = DIV_ROUND_CLOSEST_ULL(retval, (scale * scale));
194 * Create a default correction table for PWM values to create linear brightness
195 * for LED based backlights using the CIE1931 algorithm.
198 int pwm_backlight_brightness_default(struct device *dev,
199 struct platform_pwm_backlight_data *data,
206 * Once we have 4096 levels there's little point going much higher...
207 * neither interactive sliders nor animation benefits from having
208 * more values in the table.
210 data->max_brightness =
211 min((int)DIV_ROUND_UP(period, fls(period)), 4096);
213 data->levels = devm_kcalloc(dev, data->max_brightness,
214 sizeof(*data->levels), GFP_KERNEL);
218 /* Fill the table using the cie1931 algorithm */
219 for (i = 0; i < data->max_brightness; i++) {
220 retval = cie1931((i * PWM_LUMINANCE_SCALE) /
221 data->max_brightness, PWM_LUMINANCE_SCALE) *
223 retval = DIV_ROUND_CLOSEST_ULL(retval, PWM_LUMINANCE_SCALE);
224 if (retval > UINT_MAX)
226 data->levels[i] = (unsigned int)retval;
229 data->dft_brightness = data->max_brightness / 2;
230 data->max_brightness--;
235 static int pwm_backlight_parse_dt(struct device *dev,
236 struct platform_pwm_backlight_data *data)
238 struct device_node *node = dev->of_node;
239 unsigned int num_levels = 0;
240 unsigned int levels_count;
241 unsigned int num_steps = 0;
242 struct property *prop;
251 memset(data, 0, sizeof(*data));
254 * These values are optional and set as 0 by default, the out values
255 * are modified only if a valid u32 value can be decoded.
257 of_property_read_u32(node, "post-pwm-on-delay-ms",
258 &data->post_pwm_on_delay);
259 of_property_read_u32(node, "pwm-off-delay-ms", &data->pwm_off_delay);
261 data->enable_gpio = -EINVAL;
264 * Determine the number of brightness levels, if this property is not
265 * set a default table of brightness levels will be used.
267 prop = of_find_property(node, "brightness-levels", &length);
271 data->max_brightness = length / sizeof(u32);
273 /* read brightness levels from DT property */
274 if (data->max_brightness > 0) {
275 size_t size = sizeof(*data->levels) * data->max_brightness;
276 unsigned int i, j, n = 0;
278 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
282 ret = of_property_read_u32_array(node, "brightness-levels",
284 data->max_brightness);
288 ret = of_property_read_u32(node, "default-brightness-level",
293 data->dft_brightness = value;
296 * This property is optional, if is set enables linear
297 * interpolation between each of the values of brightness levels
298 * and creates a new pre-computed table.
300 of_property_read_u32(node, "num-interpolated-steps",
304 * Make sure that there is at least two entries in the
305 * brightness-levels table, otherwise we can't interpolate
306 * between two points.
309 if (data->max_brightness < 2) {
310 dev_err(dev, "can't interpolate\n");
315 * Recalculate the number of brightness levels, now
316 * taking in consideration the number of interpolated
317 * steps between two levels.
319 for (i = 0; i < data->max_brightness - 1; i++) {
320 if ((data->levels[i + 1] - data->levels[i]) /
322 num_levels += num_steps;
327 dev_dbg(dev, "new number of brightness levels: %d\n",
331 * Create a new table of brightness levels with all the
332 * interpolated steps.
334 size = sizeof(*table) * num_levels;
335 table = devm_kzalloc(dev, size, GFP_KERNEL);
339 /* Fill the interpolated table. */
341 for (i = 0; i < data->max_brightness - 1; i++) {
342 value = data->levels[i];
343 n = (data->levels[i + 1] - value) / num_steps;
345 for (j = 0; j < num_steps; j++) {
346 table[levels_count] = value;
351 table[levels_count] = data->levels[i];
355 table[levels_count] = data->levels[i];
358 * As we use interpolation lets remove current
359 * brightness levels table and replace for the
360 * new interpolated table.
362 devm_kfree(dev, data->levels);
363 data->levels = table;
366 * Reassign max_brightness value to the new total number
367 * of brightness levels.
369 data->max_brightness = num_levels;
372 data->max_brightness--;
378 static const struct of_device_id pwm_backlight_of_match[] = {
379 { .compatible = "pwm-backlight" },
383 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
385 static int pwm_backlight_parse_dt(struct device *dev,
386 struct platform_pwm_backlight_data *data)
392 int pwm_backlight_brightness_default(struct device *dev,
393 struct platform_pwm_backlight_data *data,
400 static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
402 struct device_node *node = pb->dev->of_node;
406 * If the enable GPIO is present, observable (either as input
407 * or output) and off then the backlight is not currently active.
409 if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0)
412 if (!regulator_is_enabled(pb->power_supply))
415 if (!pwm_is_enabled(pb->pwm))
419 * Synchronize the enable_gpio with the observed state of the
423 gpiod_direction_output(pb->enable_gpio, active);
426 * Do not change pb->enabled here! pb->enabled essentially
427 * tells us if we own one of the regulator's use counts and
428 * right now we do not.
431 /* Not booted with device tree or no phandle link to the node */
432 if (!node || !node->phandle)
433 return FB_BLANK_UNBLANK;
436 * If the driver is probed from the device tree and there is a
437 * phandle link pointing to the backlight node, it is safe to
438 * assume that another driver will enable the backlight at the
439 * appropriate time. Therefore, if it is disabled, keep it so.
441 return active ? FB_BLANK_UNBLANK: FB_BLANK_POWERDOWN;
444 static int pwm_backlight_probe(struct platform_device *pdev)
446 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
447 struct platform_pwm_backlight_data defdata;
448 struct backlight_properties props;
449 struct backlight_device *bl;
450 struct device_node *node = pdev->dev.of_node;
451 struct pwm_bl_data *pb;
452 struct pwm_state state;
453 struct pwm_args pargs;
458 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
460 dev_err(&pdev->dev, "failed to find platform data\n");
468 ret = data->init(&pdev->dev);
473 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
479 pb->notify = data->notify;
480 pb->notify_after = data->notify_after;
481 pb->check_fb = data->check_fb;
482 pb->exit = data->exit;
483 pb->dev = &pdev->dev;
485 pb->post_pwm_on_delay = data->post_pwm_on_delay;
486 pb->pwm_off_delay = data->pwm_off_delay;
488 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
490 if (IS_ERR(pb->enable_gpio)) {
491 ret = PTR_ERR(pb->enable_gpio);
496 * Compatibility fallback for drivers still using the integer GPIO
497 * platform data. Must go away soon.
499 if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
500 ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
501 GPIOF_OUT_INIT_HIGH, "enable");
503 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
504 data->enable_gpio, ret);
508 pb->enable_gpio = gpio_to_desc(data->enable_gpio);
511 pb->power_supply = devm_regulator_get(&pdev->dev, "power");
512 if (IS_ERR(pb->power_supply)) {
513 ret = PTR_ERR(pb->power_supply);
517 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
518 if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
519 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
521 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
524 if (IS_ERR(pb->pwm)) {
525 ret = PTR_ERR(pb->pwm);
526 if (ret != -EPROBE_DEFER)
527 dev_err(&pdev->dev, "unable to request PWM\n");
531 dev_dbg(&pdev->dev, "got pwm for backlight\n");
534 /* Get the PWM period (in nanoseconds) */
535 pwm_get_state(pb->pwm, &state);
537 ret = pwm_backlight_brightness_default(&pdev->dev, data,
541 "failed to setup default brightness table\n");
546 for (i = 0; i <= data->max_brightness; i++) {
547 if (data->levels[i] > pb->scale)
548 pb->scale = data->levels[i];
550 pb->levels = data->levels;
554 * FIXME: pwm_apply_args() should be removed when switching to
555 * the atomic PWM API.
557 pwm_apply_args(pb->pwm);
560 * The DT case will set the pwm_period_ns field to 0 and store the
561 * period, parsed from the DT, in the PWM device. For the non-DT case,
562 * set the period from platform data if it has not already been set
563 * via the PWM lookup table.
565 pwm_get_args(pb->pwm, &pargs);
566 pb->period = pargs.period;
567 if (!pb->period && (data->pwm_period_ns > 0))
568 pb->period = data->pwm_period_ns;
570 pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
572 memset(&props, 0, sizeof(struct backlight_properties));
573 props.type = BACKLIGHT_RAW;
574 props.max_brightness = data->max_brightness;
575 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
576 &pwm_backlight_ops, &props);
578 dev_err(&pdev->dev, "failed to register backlight\n");
585 if (data->dft_brightness > data->max_brightness) {
587 "invalid default brightness level: %u, using %u\n",
588 data->dft_brightness, data->max_brightness);
589 data->dft_brightness = data->max_brightness;
592 bl->props.brightness = data->dft_brightness;
593 bl->props.power = pwm_backlight_initial_power_state(pb);
594 backlight_update_status(bl);
596 platform_set_drvdata(pdev, bl);
601 data->exit(&pdev->dev);
605 static int pwm_backlight_remove(struct platform_device *pdev)
607 struct backlight_device *bl = platform_get_drvdata(pdev);
608 struct pwm_bl_data *pb = bl_get_data(bl);
610 backlight_device_unregister(bl);
611 pwm_backlight_power_off(pb);
614 pb->exit(&pdev->dev);
621 static void pwm_backlight_shutdown(struct platform_device *pdev)
623 struct backlight_device *bl = platform_get_drvdata(pdev);
624 struct pwm_bl_data *pb = bl_get_data(bl);
626 pwm_backlight_power_off(pb);
629 #ifdef CONFIG_PM_SLEEP
630 static int pwm_backlight_suspend(struct device *dev)
632 struct backlight_device *bl = dev_get_drvdata(dev);
633 struct pwm_bl_data *pb = bl_get_data(bl);
636 pb->notify(pb->dev, 0);
638 pwm_backlight_power_off(pb);
640 if (pb->notify_after)
641 pb->notify_after(pb->dev, 0);
646 static int pwm_backlight_resume(struct device *dev)
648 struct backlight_device *bl = dev_get_drvdata(dev);
650 backlight_update_status(bl);
656 static const struct dev_pm_ops pwm_backlight_pm_ops = {
657 #ifdef CONFIG_PM_SLEEP
658 .suspend = pwm_backlight_suspend,
659 .resume = pwm_backlight_resume,
660 .poweroff = pwm_backlight_suspend,
661 .restore = pwm_backlight_resume,
665 static struct platform_driver pwm_backlight_driver = {
667 .name = "pwm-backlight",
668 .pm = &pwm_backlight_pm_ops,
669 .of_match_table = of_match_ptr(pwm_backlight_of_match),
671 .probe = pwm_backlight_probe,
672 .remove = pwm_backlight_remove,
673 .shutdown = pwm_backlight_shutdown,
676 module_platform_driver(pwm_backlight_driver);
678 MODULE_DESCRIPTION("PWM based Backlight Driver");
679 MODULE_LICENSE("GPL");
680 MODULE_ALIAS("platform:pwm-backlight");