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.h>
14 #include <linux/of_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;
36 unsigned long enable_gpio_flags;
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");
57 if (gpio_is_valid(pb->enable_gpio)) {
58 if (pb->enable_gpio_flags & PWM_BACKLIGHT_GPIO_ACTIVE_LOW)
59 gpio_set_value(pb->enable_gpio, 0);
61 gpio_set_value(pb->enable_gpio, 1);
68 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
73 pwm_config(pb->pwm, 0, pb->period);
76 if (gpio_is_valid(pb->enable_gpio)) {
77 if (pb->enable_gpio_flags & PWM_BACKLIGHT_GPIO_ACTIVE_LOW)
78 gpio_set_value(pb->enable_gpio, 1);
80 gpio_set_value(pb->enable_gpio, 0);
83 regulator_disable(pb->power_supply);
87 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
89 unsigned int lth = pb->lth_brightness;
93 duty_cycle = pb->levels[brightness];
95 duty_cycle = brightness;
97 return (duty_cycle * (pb->period - lth) / pb->scale) + lth;
100 static int pwm_backlight_update_status(struct backlight_device *bl)
102 struct pwm_bl_data *pb = bl_get_data(bl);
103 int brightness = bl->props.brightness;
106 if (bl->props.power != FB_BLANK_UNBLANK ||
107 bl->props.fb_blank != FB_BLANK_UNBLANK ||
108 bl->props.state & BL_CORE_FBBLANK)
112 brightness = pb->notify(pb->dev, brightness);
114 if (brightness > 0) {
115 duty_cycle = compute_duty_cycle(pb, brightness);
116 pwm_config(pb->pwm, duty_cycle, pb->period);
117 pwm_backlight_power_on(pb, brightness);
119 pwm_backlight_power_off(pb);
121 if (pb->notify_after)
122 pb->notify_after(pb->dev, brightness);
127 static int pwm_backlight_get_brightness(struct backlight_device *bl)
129 return bl->props.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 .get_brightness = pwm_backlight_get_brightness,
143 .check_fb = pwm_backlight_check_fb,
147 static int pwm_backlight_parse_dt(struct device *dev,
148 struct platform_pwm_backlight_data *data)
150 struct device_node *node = dev->of_node;
151 enum of_gpio_flags flags;
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 = of_get_named_gpio_flags(node, "enable-gpios", 0,
194 if (data->enable_gpio == -EPROBE_DEFER)
195 return -EPROBE_DEFER;
197 if (gpio_is_valid(data->enable_gpio) && (flags & OF_GPIO_ACTIVE_LOW))
198 data->enable_gpio_flags |= PWM_BACKLIGHT_GPIO_ACTIVE_LOW;
203 static struct of_device_id pwm_backlight_of_match[] = {
204 { .compatible = "pwm-backlight" },
208 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
210 static int pwm_backlight_parse_dt(struct device *dev,
211 struct platform_pwm_backlight_data *data)
217 static int pwm_backlight_probe(struct platform_device *pdev)
219 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
220 struct platform_pwm_backlight_data defdata;
221 struct backlight_properties props;
222 struct backlight_device *bl;
223 struct pwm_bl_data *pb;
227 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
229 dev_err(&pdev->dev, "failed to find platform data\n");
237 ret = data->init(&pdev->dev);
242 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
244 dev_err(&pdev->dev, "no memory for state\n");
252 for (i = 0; i <= data->max_brightness; i++)
253 if (data->levels[i] > pb->scale)
254 pb->scale = data->levels[i];
256 pb->levels = data->levels;
258 pb->scale = data->max_brightness;
260 pb->enable_gpio = data->enable_gpio;
261 pb->enable_gpio_flags = data->enable_gpio_flags;
262 pb->notify = data->notify;
263 pb->notify_after = data->notify_after;
264 pb->check_fb = data->check_fb;
265 pb->exit = data->exit;
266 pb->dev = &pdev->dev;
269 if (gpio_is_valid(pb->enable_gpio)) {
272 if (pb->enable_gpio_flags & PWM_BACKLIGHT_GPIO_ACTIVE_LOW)
273 flags = GPIOF_OUT_INIT_HIGH;
275 flags = GPIOF_OUT_INIT_LOW;
277 ret = gpio_request_one(pb->enable_gpio, flags, "enable");
279 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
280 pb->enable_gpio, ret);
285 pb->power_supply = devm_regulator_get(&pdev->dev, "power");
286 if (IS_ERR(pb->power_supply)) {
287 ret = PTR_ERR(pb->power_supply);
291 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
292 if (IS_ERR(pb->pwm)) {
293 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
295 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
296 if (IS_ERR(pb->pwm)) {
297 dev_err(&pdev->dev, "unable to request legacy PWM\n");
298 ret = PTR_ERR(pb->pwm);
303 dev_dbg(&pdev->dev, "got pwm for backlight\n");
306 * The DT case will set the pwm_period_ns field to 0 and store the
307 * period, parsed from the DT, in the PWM device. For the non-DT case,
308 * set the period from platform data.
310 if (data->pwm_period_ns > 0)
311 pwm_set_period(pb->pwm, data->pwm_period_ns);
313 pb->period = pwm_get_period(pb->pwm);
314 pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
316 memset(&props, 0, sizeof(struct backlight_properties));
317 props.type = BACKLIGHT_RAW;
318 props.max_brightness = data->max_brightness;
319 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
320 &pwm_backlight_ops, &props);
322 dev_err(&pdev->dev, "failed to register backlight\n");
327 if (data->dft_brightness > data->max_brightness) {
329 "invalid default brightness level: %u, using %u\n",
330 data->dft_brightness, data->max_brightness);
331 data->dft_brightness = data->max_brightness;
334 bl->props.brightness = data->dft_brightness;
335 backlight_update_status(bl);
337 platform_set_drvdata(pdev, bl);
341 if (gpio_is_valid(pb->enable_gpio))
342 gpio_free(pb->enable_gpio);
345 data->exit(&pdev->dev);
349 static int pwm_backlight_remove(struct platform_device *pdev)
351 struct backlight_device *bl = platform_get_drvdata(pdev);
352 struct pwm_bl_data *pb = bl_get_data(bl);
354 backlight_device_unregister(bl);
355 pwm_backlight_power_off(pb);
358 pb->exit(&pdev->dev);
363 #ifdef CONFIG_PM_SLEEP
364 static int pwm_backlight_suspend(struct device *dev)
366 struct backlight_device *bl = dev_get_drvdata(dev);
367 struct pwm_bl_data *pb = bl_get_data(bl);
370 pb->notify(pb->dev, 0);
372 pwm_backlight_power_off(pb);
374 if (pb->notify_after)
375 pb->notify_after(pb->dev, 0);
380 static int pwm_backlight_resume(struct device *dev)
382 struct backlight_device *bl = dev_get_drvdata(dev);
384 backlight_update_status(bl);
390 static const struct dev_pm_ops pwm_backlight_pm_ops = {
391 #ifdef CONFIG_PM_SLEEP
392 .suspend = pwm_backlight_suspend,
393 .resume = pwm_backlight_resume,
394 .poweroff = pwm_backlight_suspend,
395 .restore = pwm_backlight_resume,
399 static struct platform_driver pwm_backlight_driver = {
401 .name = "pwm-backlight",
402 .owner = THIS_MODULE,
403 .pm = &pwm_backlight_pm_ops,
404 .of_match_table = of_match_ptr(pwm_backlight_of_match),
406 .probe = pwm_backlight_probe,
407 .remove = pwm_backlight_remove,
410 module_platform_driver(pwm_backlight_driver);
412 MODULE_DESCRIPTION("PWM based Backlight Driver");
413 MODULE_LICENSE("GPL");
414 MODULE_ALIAS("platform:pwm-backlight");