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");
60 gpiod_set_value_cansleep(pb->enable_gpio, 1);
65 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
71 gpiod_set_value_cansleep(pb->enable_gpio, 0);
73 pwm_config(pb->pwm, 0, pb->period);
76 regulator_disable(pb->power_supply);
80 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
82 unsigned int lth = pb->lth_brightness;
86 duty_cycle = pb->levels[brightness];
88 duty_cycle = brightness;
90 duty_cycle *= pb->period - lth;
91 do_div(duty_cycle, pb->scale);
93 return duty_cycle + lth;
96 static int pwm_backlight_update_status(struct backlight_device *bl)
98 struct pwm_bl_data *pb = bl_get_data(bl);
99 int brightness = bl->props.brightness;
102 if (bl->props.power != FB_BLANK_UNBLANK ||
103 bl->props.fb_blank != FB_BLANK_UNBLANK ||
104 bl->props.state & BL_CORE_FBBLANK)
108 brightness = pb->notify(pb->dev, brightness);
110 if (brightness > 0) {
111 duty_cycle = compute_duty_cycle(pb, brightness);
112 pwm_config(pb->pwm, duty_cycle, pb->period);
113 pwm_backlight_power_on(pb, brightness);
115 pwm_backlight_power_off(pb);
117 if (pb->notify_after)
118 pb->notify_after(pb->dev, brightness);
123 static int pwm_backlight_check_fb(struct backlight_device *bl,
124 struct fb_info *info)
126 struct pwm_bl_data *pb = bl_get_data(bl);
128 return !pb->check_fb || pb->check_fb(pb->dev, info);
131 static const struct backlight_ops pwm_backlight_ops = {
132 .update_status = pwm_backlight_update_status,
133 .check_fb = pwm_backlight_check_fb,
137 static int pwm_backlight_parse_dt(struct device *dev,
138 struct platform_pwm_backlight_data *data)
140 struct device_node *node = dev->of_node;
141 struct property *prop;
149 memset(data, 0, sizeof(*data));
151 /* determine the number of brightness levels */
152 prop = of_find_property(node, "brightness-levels", &length);
156 data->max_brightness = length / sizeof(u32);
158 /* read brightness levels from DT property */
159 if (data->max_brightness > 0) {
160 size_t size = sizeof(*data->levels) * data->max_brightness;
162 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
166 ret = of_property_read_u32_array(node, "brightness-levels",
168 data->max_brightness);
172 ret = of_property_read_u32(node, "default-brightness-level",
177 data->dft_brightness = value;
178 data->max_brightness--;
181 data->enable_gpio = -EINVAL;
185 static const struct of_device_id pwm_backlight_of_match[] = {
186 { .compatible = "pwm-backlight" },
190 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
192 static int pwm_backlight_parse_dt(struct device *dev,
193 struct platform_pwm_backlight_data *data)
199 static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
201 struct device_node *node = pb->dev->of_node;
205 * If the enable GPIO is present, observable (either as input
206 * or output) and off then the backlight is not currently active.
208 if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0)
211 if (!regulator_is_enabled(pb->power_supply))
214 if (!pwm_is_enabled(pb->pwm))
218 * Synchronize the enable_gpio with the observed state of the
222 gpiod_direction_output(pb->enable_gpio, active);
225 * Do not change pb->enabled here! pb->enabled essentially
226 * tells us if we own one of the regulator's use counts and
227 * right now we do not.
230 /* Not booted with device tree or no phandle link to the node */
231 if (!node || !node->phandle)
232 return FB_BLANK_UNBLANK;
235 * If the driver is probed from the device tree and there is a
236 * phandle link pointing to the backlight node, it is safe to
237 * assume that another driver will enable the backlight at the
238 * appropriate time. Therefore, if it is disabled, keep it so.
240 return active ? FB_BLANK_UNBLANK: FB_BLANK_POWERDOWN;
243 static int pwm_backlight_probe(struct platform_device *pdev)
245 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
246 struct platform_pwm_backlight_data defdata;
247 struct backlight_properties props;
248 struct backlight_device *bl;
249 struct device_node *node = pdev->dev.of_node;
250 struct pwm_bl_data *pb;
251 struct pwm_args pargs;
255 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
257 dev_err(&pdev->dev, "failed to find platform data\n");
265 ret = data->init(&pdev->dev);
270 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
279 for (i = 0; i <= data->max_brightness; i++)
280 if (data->levels[i] > pb->scale)
281 pb->scale = data->levels[i];
283 pb->levels = data->levels;
285 pb->scale = data->max_brightness;
287 pb->notify = data->notify;
288 pb->notify_after = data->notify_after;
289 pb->check_fb = data->check_fb;
290 pb->exit = data->exit;
291 pb->dev = &pdev->dev;
294 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
296 if (IS_ERR(pb->enable_gpio)) {
297 ret = PTR_ERR(pb->enable_gpio);
302 * Compatibility fallback for drivers still using the integer GPIO
303 * platform data. Must go away soon.
305 if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
306 ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
307 GPIOF_OUT_INIT_HIGH, "enable");
309 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
310 data->enable_gpio, ret);
314 pb->enable_gpio = gpio_to_desc(data->enable_gpio);
317 pb->power_supply = devm_regulator_get(&pdev->dev, "power");
318 if (IS_ERR(pb->power_supply)) {
319 ret = PTR_ERR(pb->power_supply);
323 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
324 if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
325 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
327 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
330 if (IS_ERR(pb->pwm)) {
331 ret = PTR_ERR(pb->pwm);
332 if (ret != -EPROBE_DEFER)
333 dev_err(&pdev->dev, "unable to request PWM\n");
337 dev_dbg(&pdev->dev, "got pwm for backlight\n");
340 * FIXME: pwm_apply_args() should be removed when switching to
341 * the atomic PWM API.
343 pwm_apply_args(pb->pwm);
346 * The DT case will set the pwm_period_ns field to 0 and store the
347 * period, parsed from the DT, in the PWM device. For the non-DT case,
348 * set the period from platform data if it has not already been set
349 * via the PWM lookup table.
351 pwm_get_args(pb->pwm, &pargs);
352 pb->period = pargs.period;
353 if (!pb->period && (data->pwm_period_ns > 0))
354 pb->period = data->pwm_period_ns;
356 pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
358 memset(&props, 0, sizeof(struct backlight_properties));
359 props.type = BACKLIGHT_RAW;
360 props.max_brightness = data->max_brightness;
361 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
362 &pwm_backlight_ops, &props);
364 dev_err(&pdev->dev, "failed to register backlight\n");
371 if (data->dft_brightness > data->max_brightness) {
373 "invalid default brightness level: %u, using %u\n",
374 data->dft_brightness, data->max_brightness);
375 data->dft_brightness = data->max_brightness;
378 bl->props.brightness = data->dft_brightness;
379 bl->props.power = pwm_backlight_initial_power_state(pb);
380 backlight_update_status(bl);
382 platform_set_drvdata(pdev, bl);
387 data->exit(&pdev->dev);
391 static int pwm_backlight_remove(struct platform_device *pdev)
393 struct backlight_device *bl = platform_get_drvdata(pdev);
394 struct pwm_bl_data *pb = bl_get_data(bl);
396 backlight_device_unregister(bl);
397 pwm_backlight_power_off(pb);
400 pb->exit(&pdev->dev);
407 static void pwm_backlight_shutdown(struct platform_device *pdev)
409 struct backlight_device *bl = platform_get_drvdata(pdev);
410 struct pwm_bl_data *pb = bl_get_data(bl);
412 pwm_backlight_power_off(pb);
415 #ifdef CONFIG_PM_SLEEP
416 static int pwm_backlight_suspend(struct device *dev)
418 struct backlight_device *bl = dev_get_drvdata(dev);
419 struct pwm_bl_data *pb = bl_get_data(bl);
422 pb->notify(pb->dev, 0);
424 pwm_backlight_power_off(pb);
426 if (pb->notify_after)
427 pb->notify_after(pb->dev, 0);
432 static int pwm_backlight_resume(struct device *dev)
434 struct backlight_device *bl = dev_get_drvdata(dev);
436 backlight_update_status(bl);
442 static const struct dev_pm_ops pwm_backlight_pm_ops = {
443 #ifdef CONFIG_PM_SLEEP
444 .suspend = pwm_backlight_suspend,
445 .resume = pwm_backlight_resume,
446 .poweroff = pwm_backlight_suspend,
447 .restore = pwm_backlight_resume,
451 static struct platform_driver pwm_backlight_driver = {
453 .name = "pwm-backlight",
454 .pm = &pwm_backlight_pm_ops,
455 .of_match_table = of_match_ptr(pwm_backlight_of_match),
457 .probe = pwm_backlight_probe,
458 .remove = pwm_backlight_remove,
459 .shutdown = pwm_backlight_shutdown,
462 module_platform_driver(pwm_backlight_driver);
464 MODULE_DESCRIPTION("PWM based Backlight Driver");
465 MODULE_LICENSE("GPL");
466 MODULE_ALIAS("platform:pwm-backlight");