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;
37 int (*notify)(struct device *,
39 void (*notify_after)(struct device *,
41 int (*check_fb)(struct device *, struct fb_info *);
42 void (*exit)(struct device *);
45 static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
52 err = regulator_enable(pb->power_supply);
54 dev_err(pb->dev, "failed to enable power supply\n");
57 gpiod_set_value(pb->enable_gpio, 1);
63 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
68 pwm_config(pb->pwm, 0, pb->period);
72 gpiod_set_value(pb->enable_gpio, 0);
74 regulator_disable(pb->power_supply);
78 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
80 unsigned int lth = pb->lth_brightness;
84 duty_cycle = pb->levels[brightness];
86 duty_cycle = brightness;
88 return (duty_cycle * (pb->period - lth) / pb->scale) + lth;
91 static int pwm_backlight_update_status(struct backlight_device *bl)
93 struct pwm_bl_data *pb = bl_get_data(bl);
94 int brightness = bl->props.brightness;
97 if (bl->props.power != FB_BLANK_UNBLANK ||
98 bl->props.fb_blank != FB_BLANK_UNBLANK ||
99 bl->props.state & BL_CORE_FBBLANK)
103 brightness = pb->notify(pb->dev, brightness);
105 if (brightness > 0) {
106 duty_cycle = compute_duty_cycle(pb, brightness);
107 pwm_config(pb->pwm, duty_cycle, pb->period);
108 pwm_backlight_power_on(pb, brightness);
110 pwm_backlight_power_off(pb);
112 if (pb->notify_after)
113 pb->notify_after(pb->dev, brightness);
118 static int pwm_backlight_check_fb(struct backlight_device *bl,
119 struct fb_info *info)
121 struct pwm_bl_data *pb = bl_get_data(bl);
123 return !pb->check_fb || pb->check_fb(pb->dev, info);
126 static const struct backlight_ops pwm_backlight_ops = {
127 .update_status = pwm_backlight_update_status,
128 .check_fb = pwm_backlight_check_fb,
132 static int pwm_backlight_parse_dt(struct device *dev,
133 struct platform_pwm_backlight_data *data)
135 struct device_node *node = dev->of_node;
136 struct property *prop;
144 memset(data, 0, sizeof(*data));
146 /* determine the number of brightness levels */
147 prop = of_find_property(node, "brightness-levels", &length);
151 data->max_brightness = length / sizeof(u32);
153 /* read brightness levels from DT property */
154 if (data->max_brightness > 0) {
155 size_t size = sizeof(*data->levels) * data->max_brightness;
157 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
161 ret = of_property_read_u32_array(node, "brightness-levels",
163 data->max_brightness);
167 ret = of_property_read_u32(node, "default-brightness-level",
172 data->dft_brightness = value;
173 data->max_brightness--;
176 data->enable_gpio = -EINVAL;
180 static struct of_device_id pwm_backlight_of_match[] = {
181 { .compatible = "pwm-backlight" },
185 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
187 static int pwm_backlight_parse_dt(struct device *dev,
188 struct platform_pwm_backlight_data *data)
194 static int pwm_backlight_probe(struct platform_device *pdev)
196 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
197 struct platform_pwm_backlight_data defdata;
198 struct backlight_properties props;
199 struct backlight_device *bl;
200 struct pwm_bl_data *pb;
204 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
206 dev_err(&pdev->dev, "failed to find platform data\n");
214 ret = data->init(&pdev->dev);
219 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
228 for (i = 0; i <= data->max_brightness; i++)
229 if (data->levels[i] > pb->scale)
230 pb->scale = data->levels[i];
232 pb->levels = data->levels;
234 pb->scale = data->max_brightness;
236 pb->notify = data->notify;
237 pb->notify_after = data->notify_after;
238 pb->check_fb = data->check_fb;
239 pb->exit = data->exit;
240 pb->dev = &pdev->dev;
243 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable");
244 if (IS_ERR(pb->enable_gpio)) {
245 ret = PTR_ERR(pb->enable_gpio);
250 * Compatibility fallback for drivers still using the integer GPIO
251 * platform data. Must go away soon.
253 if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
254 ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
255 GPIOF_OUT_INIT_HIGH, "enable");
257 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
258 data->enable_gpio, ret);
262 pb->enable_gpio = gpio_to_desc(data->enable_gpio);
266 gpiod_direction_output(pb->enable_gpio, 1);
268 pb->power_supply = devm_regulator_get(&pdev->dev, "power");
269 if (IS_ERR(pb->power_supply)) {
270 ret = PTR_ERR(pb->power_supply);
274 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
275 if (IS_ERR(pb->pwm)) {
276 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
278 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
279 if (IS_ERR(pb->pwm)) {
280 dev_err(&pdev->dev, "unable to request legacy PWM\n");
281 ret = PTR_ERR(pb->pwm);
286 dev_dbg(&pdev->dev, "got pwm for backlight\n");
289 * The DT case will set the pwm_period_ns field to 0 and store the
290 * period, parsed from the DT, in the PWM device. For the non-DT case,
291 * set the period from platform data if it has not already been set
292 * via the PWM lookup table.
294 pb->period = pwm_get_period(pb->pwm);
295 if (!pb->period && (data->pwm_period_ns > 0)) {
296 pb->period = data->pwm_period_ns;
297 pwm_set_period(pb->pwm, data->pwm_period_ns);
300 pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
302 memset(&props, 0, sizeof(struct backlight_properties));
303 props.type = BACKLIGHT_RAW;
304 props.max_brightness = data->max_brightness;
305 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
306 &pwm_backlight_ops, &props);
308 dev_err(&pdev->dev, "failed to register backlight\n");
313 if (data->dft_brightness > data->max_brightness) {
315 "invalid default brightness level: %u, using %u\n",
316 data->dft_brightness, data->max_brightness);
317 data->dft_brightness = data->max_brightness;
320 bl->props.brightness = data->dft_brightness;
321 backlight_update_status(bl);
323 platform_set_drvdata(pdev, bl);
328 data->exit(&pdev->dev);
332 static int pwm_backlight_remove(struct platform_device *pdev)
334 struct backlight_device *bl = platform_get_drvdata(pdev);
335 struct pwm_bl_data *pb = bl_get_data(bl);
337 backlight_device_unregister(bl);
338 pwm_backlight_power_off(pb);
341 pb->exit(&pdev->dev);
346 static void pwm_backlight_shutdown(struct platform_device *pdev)
348 struct backlight_device *bl = platform_get_drvdata(pdev);
349 struct pwm_bl_data *pb = bl_get_data(bl);
351 pwm_backlight_power_off(pb);
354 #ifdef CONFIG_PM_SLEEP
355 static int pwm_backlight_suspend(struct device *dev)
357 struct backlight_device *bl = dev_get_drvdata(dev);
358 struct pwm_bl_data *pb = bl_get_data(bl);
361 pb->notify(pb->dev, 0);
363 pwm_backlight_power_off(pb);
365 if (pb->notify_after)
366 pb->notify_after(pb->dev, 0);
371 static int pwm_backlight_resume(struct device *dev)
373 struct backlight_device *bl = dev_get_drvdata(dev);
375 backlight_update_status(bl);
381 static const struct dev_pm_ops pwm_backlight_pm_ops = {
382 #ifdef CONFIG_PM_SLEEP
383 .suspend = pwm_backlight_suspend,
384 .resume = pwm_backlight_resume,
385 .poweroff = pwm_backlight_suspend,
386 .restore = pwm_backlight_resume,
390 static struct platform_driver pwm_backlight_driver = {
392 .name = "pwm-backlight",
393 .owner = THIS_MODULE,
394 .pm = &pwm_backlight_pm_ops,
395 .of_match_table = of_match_ptr(pwm_backlight_of_match),
397 .probe = pwm_backlight_probe,
398 .remove = pwm_backlight_remove,
399 .shutdown = pwm_backlight_shutdown,
402 module_platform_driver(pwm_backlight_driver);
404 MODULE_DESCRIPTION("PWM based Backlight Driver");
405 MODULE_LICENSE("GPL");
406 MODULE_ALIAS("platform:pwm-backlight");