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>
26 #include <linux/delay.h>
29 struct pwm_device *pwm;
32 unsigned int lth_brightness;
35 struct regulator *power_supply;
36 struct gpio_desc *enable_gpio;
40 int (*notify)(struct device *,
42 void (*notify_after)(struct device *,
44 int (*check_fb)(struct device *, struct fb_info *);
45 void (*exit)(struct device *);
48 static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
57 if (pb->turn_on_delay_ms > 0) {
58 dev_dbg(pb->dev, "Sleeping %u..%uµs\n",
59 pb->turn_on_delay_ms * 1000,
60 pb->turn_on_delay_ms * 1100);
61 if (pb->turn_on_delay_ms > 20)
62 msleep(pb->turn_on_delay_ms);
64 usleep_range(pb->turn_on_delay_ms * 1000,
65 pb->turn_on_delay_ms * 1100);
68 err = regulator_enable(pb->power_supply);
70 dev_err(pb->dev, "failed to enable power supply\n");
73 gpiod_set_value_cansleep(pb->enable_gpio, 1);
78 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
83 pwm_config(pb->pwm, 0, pb->period);
87 gpiod_set_value_cansleep(pb->enable_gpio, 0);
89 regulator_disable(pb->power_supply);
93 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
95 unsigned int lth = pb->lth_brightness;
99 duty_cycle = pb->levels[brightness];
101 duty_cycle = brightness;
103 return (duty_cycle * (pb->period - lth) / pb->scale) + lth;
106 static int pwm_backlight_update_status(struct backlight_device *bl)
108 struct pwm_bl_data *pb = bl_get_data(bl);
109 int brightness = bl->props.brightness;
112 if (bl->props.power != FB_BLANK_UNBLANK ||
113 bl->props.fb_blank != FB_BLANK_UNBLANK ||
114 bl->props.state & BL_CORE_FBBLANK)
118 brightness = pb->notify(pb->dev, brightness);
120 if (brightness > 0) {
121 duty_cycle = compute_duty_cycle(pb, brightness);
122 pwm_config(pb->pwm, duty_cycle, pb->period);
123 pwm_backlight_power_on(pb, brightness);
125 pwm_backlight_power_off(pb);
128 if (pb->notify_after)
129 pb->notify_after(pb->dev, brightness);
134 static int pwm_backlight_check_fb(struct backlight_device *bl,
135 struct fb_info *info)
137 struct pwm_bl_data *pb = bl_get_data(bl);
139 return !pb->check_fb || pb->check_fb(pb->dev, info);
142 static const struct backlight_ops pwm_backlight_ops = {
143 .update_status = pwm_backlight_update_status,
144 .check_fb = pwm_backlight_check_fb,
148 static int pwm_backlight_parse_dt(struct device *dev,
149 struct platform_pwm_backlight_data *data)
151 struct device_node *node = dev->of_node;
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 = -EINVAL;
196 static struct of_device_id pwm_backlight_of_match[] = {
197 { .compatible = "pwm-backlight" },
201 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
203 static int pwm_backlight_parse_dt(struct device *dev,
204 struct platform_pwm_backlight_data *data)
210 static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
212 struct device_node *node = pb->dev->of_node;
214 /* Not booted with device tree or no phandle link to the node */
215 if (!node || !node->phandle)
216 return FB_BLANK_UNBLANK;
219 * If the driver is probed from the device tree and there is a
220 * phandle link pointing to the backlight node, it is safe to
221 * assume that another driver will enable the backlight at the
222 * appropriate time. Therefore, if it is disabled, keep it so.
225 /* if the enable GPIO is disabled, do not enable the backlight */
226 if (pb->enable_gpio && gpiod_get_value(pb->enable_gpio) == 0)
227 return FB_BLANK_POWERDOWN;
229 /* The regulator is disabled, do not enable the backlight */
230 if (!regulator_is_enabled(pb->power_supply))
231 return FB_BLANK_POWERDOWN;
233 /* The PWM is disabled, keep it like this */
234 if (!pwm_is_enabled(pb->pwm))
235 return FB_BLANK_POWERDOWN;
237 return FB_BLANK_UNBLANK;
240 static int pwm_backlight_probe(struct platform_device *pdev)
242 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
243 struct platform_pwm_backlight_data defdata;
244 struct backlight_properties props;
245 struct backlight_device *bl;
246 struct device_node *node = pdev->dev.of_node;
247 struct pwm_bl_data *pb;
248 struct pwm_args pargs;
252 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
254 dev_err(&pdev->dev, "failed to find platform data\n");
262 ret = data->init(&pdev->dev);
267 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
276 for (i = 0; i <= data->max_brightness; i++)
277 if (data->levels[i] > pb->scale)
278 pb->scale = data->levels[i];
280 pb->levels = data->levels;
282 pb->scale = data->max_brightness;
284 pb->notify = data->notify;
285 pb->notify_after = data->notify_after;
286 pb->check_fb = data->check_fb;
287 pb->exit = data->exit;
288 pb->dev = &pdev->dev;
291 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
293 if (IS_ERR(pb->enable_gpio)) {
294 ret = PTR_ERR(pb->enable_gpio);
299 * Compatibility fallback for drivers still using the integer GPIO
300 * platform data. Must go away soon.
302 if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
303 ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
304 GPIOF_OUT_INIT_HIGH, "enable");
306 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
307 data->enable_gpio, ret);
311 pb->enable_gpio = gpio_to_desc(data->enable_gpio);
315 * If the GPIO is not known to be already configured as output, that
316 * is, if gpiod_get_direction returns either GPIOF_DIR_IN or -EINVAL,
317 * change the direction to output and set the GPIO as active.
318 * Do not force the GPIO to active when it was already output as it
319 * could cause backlight flickering or we would enable the backlight too
320 * early. Leave the decision of the initial backlight state for later.
322 if (pb->enable_gpio &&
323 gpiod_get_direction(pb->enable_gpio) != GPIOF_DIR_OUT)
324 gpiod_direction_output(pb->enable_gpio, 1);
326 pb->power_supply = devm_regulator_get(&pdev->dev, "power");
327 if (IS_ERR(pb->power_supply)) {
328 ret = PTR_ERR(pb->power_supply);
332 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
333 if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
334 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
336 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
339 if (IS_ERR(pb->pwm)) {
340 ret = PTR_ERR(pb->pwm);
341 if (ret != -EPROBE_DEFER)
342 dev_err(&pdev->dev, "unable to request PWM\n");
346 of_property_read_u32(node, "turn-on-delay-ms", &pb->turn_on_delay_ms);
348 dev_dbg(&pdev->dev, "got pwm for backlight\n");
351 * FIXME: pwm_apply_args() should be removed when switching to
352 * the atomic PWM API.
354 pwm_apply_args(pb->pwm);
357 * The DT case will set the pwm_period_ns field to 0 and store the
358 * period, parsed from the DT, in the PWM device. For the non-DT case,
359 * set the period from platform data if it has not already been set
360 * via the PWM lookup table.
362 pwm_get_args(pb->pwm, &pargs);
363 pb->period = pargs.period;
364 if (!pb->period && (data->pwm_period_ns > 0))
365 pb->period = data->pwm_period_ns;
367 pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
369 memset(&props, 0, sizeof(struct backlight_properties));
370 props.type = BACKLIGHT_RAW;
371 props.max_brightness = data->max_brightness;
372 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
373 &pwm_backlight_ops, &props);
375 dev_err(&pdev->dev, "failed to register backlight\n");
382 if (data->dft_brightness > data->max_brightness) {
384 "invalid default brightness level: %u, using %u\n",
385 data->dft_brightness, data->max_brightness);
386 data->dft_brightness = data->max_brightness;
389 bl->props.brightness = data->dft_brightness;
390 bl->props.power = pwm_backlight_initial_power_state(pb);
391 backlight_update_status(bl);
393 platform_set_drvdata(pdev, bl);
398 data->exit(&pdev->dev);
402 static int pwm_backlight_remove(struct platform_device *pdev)
404 struct backlight_device *bl = platform_get_drvdata(pdev);
405 struct pwm_bl_data *pb = bl_get_data(bl);
407 backlight_device_unregister(bl);
408 pwm_backlight_power_off(pb);
411 pb->exit(&pdev->dev);
418 static void pwm_backlight_shutdown(struct platform_device *pdev)
420 struct backlight_device *bl = platform_get_drvdata(pdev);
421 struct pwm_bl_data *pb = bl_get_data(bl);
423 pwm_backlight_power_off(pb);
426 #ifdef CONFIG_PM_SLEEP
427 static int pwm_backlight_suspend(struct device *dev)
429 struct backlight_device *bl = dev_get_drvdata(dev);
430 struct pwm_bl_data *pb = bl_get_data(bl);
433 pb->notify(pb->dev, 0);
435 pwm_backlight_power_off(pb);
437 if (pb->notify_after)
438 pb->notify_after(pb->dev, 0);
443 static int pwm_backlight_resume(struct device *dev)
445 struct backlight_device *bl = dev_get_drvdata(dev);
447 backlight_update_status(bl);
453 static const struct dev_pm_ops pwm_backlight_pm_ops = {
454 #ifdef CONFIG_PM_SLEEP
455 .suspend = pwm_backlight_suspend,
456 .resume = pwm_backlight_resume,
457 .poweroff = pwm_backlight_suspend,
458 .restore = pwm_backlight_resume,
462 static struct platform_driver pwm_backlight_driver = {
464 .name = "pwm-backlight",
465 .pm = &pwm_backlight_pm_ops,
466 .of_match_table = of_match_ptr(pwm_backlight_of_match),
468 .probe = pwm_backlight_probe,
469 .remove = pwm_backlight_remove,
470 .shutdown = pwm_backlight_shutdown,
473 module_platform_driver(pwm_backlight_driver);
475 MODULE_DESCRIPTION("PWM based Backlight Driver");
476 MODULE_LICENSE("GPL");
477 MODULE_ALIAS("platform:pwm-backlight");