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/slab.h>
27 struct pwm_device *pwm;
30 unsigned int lth_brightness;
34 unsigned long enable_gpio_flags;
35 int (*notify)(struct device *,
37 void (*notify_after)(struct device *,
39 int (*check_fb)(struct device *, struct fb_info *);
40 void (*exit)(struct device *);
43 static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness,
52 duty_cycle = pb->levels[brightness];
53 max = pb->levels[max];
55 duty_cycle = brightness;
58 duty_cycle = (duty_cycle * (pb->period - pb->lth_brightness) / max) +
61 pwm_config(pb->pwm, duty_cycle, pb->period);
63 if (gpio_is_valid(pb->enable_gpio)) {
64 if (pb->enable_gpio_flags & PWM_BACKLIGHT_GPIO_ACTIVE_LOW)
65 gpio_set_value(pb->enable_gpio, 0);
67 gpio_set_value(pb->enable_gpio, 1);
74 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
79 pwm_config(pb->pwm, 0, pb->period);
82 if (gpio_is_valid(pb->enable_gpio)) {
83 if (pb->enable_gpio_flags & PWM_BACKLIGHT_GPIO_ACTIVE_LOW)
84 gpio_set_value(pb->enable_gpio, 1);
86 gpio_set_value(pb->enable_gpio, 0);
92 static int pwm_backlight_update_status(struct backlight_device *bl)
94 struct pwm_bl_data *pb = bl_get_data(bl);
95 int brightness = bl->props.brightness;
96 int max = bl->props.max_brightness;
98 if (bl->props.power != FB_BLANK_UNBLANK ||
99 bl->props.fb_blank != FB_BLANK_UNBLANK ||
100 bl->props.state & BL_CORE_FBBLANK)
104 brightness = pb->notify(pb->dev, brightness);
107 pwm_backlight_power_on(pb, brightness, max);
109 pwm_backlight_power_off(pb);
111 if (pb->notify_after)
112 pb->notify_after(pb->dev, brightness);
117 static int pwm_backlight_get_brightness(struct backlight_device *bl)
119 return bl->props.brightness;
122 static int pwm_backlight_check_fb(struct backlight_device *bl,
123 struct fb_info *info)
125 struct pwm_bl_data *pb = bl_get_data(bl);
127 return !pb->check_fb || pb->check_fb(pb->dev, info);
130 static const struct backlight_ops pwm_backlight_ops = {
131 .update_status = pwm_backlight_update_status,
132 .get_brightness = pwm_backlight_get_brightness,
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 enum of_gpio_flags flags;
142 struct property *prop;
150 memset(data, 0, sizeof(*data));
152 /* determine the number of brightness levels */
153 prop = of_find_property(node, "brightness-levels", &length);
157 data->max_brightness = length / sizeof(u32);
159 /* read brightness levels from DT property */
160 if (data->max_brightness > 0) {
161 size_t size = sizeof(*data->levels) * data->max_brightness;
163 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
167 ret = of_property_read_u32_array(node, "brightness-levels",
169 data->max_brightness);
173 ret = of_property_read_u32(node, "default-brightness-level",
178 data->dft_brightness = value;
179 data->max_brightness--;
182 data->enable_gpio = of_get_named_gpio_flags(node, "enable-gpios", 0,
184 if (data->enable_gpio == -EPROBE_DEFER)
185 return -EPROBE_DEFER;
187 if (gpio_is_valid(data->enable_gpio) && (flags & OF_GPIO_ACTIVE_LOW))
188 data->enable_gpio_flags |= PWM_BACKLIGHT_GPIO_ACTIVE_LOW;
193 static struct of_device_id pwm_backlight_of_match[] = {
194 { .compatible = "pwm-backlight" },
198 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
200 static int pwm_backlight_parse_dt(struct device *dev,
201 struct platform_pwm_backlight_data *data)
207 static int pwm_backlight_probe(struct platform_device *pdev)
209 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
210 struct platform_pwm_backlight_data defdata;
211 struct backlight_properties props;
212 struct backlight_device *bl;
213 struct pwm_bl_data *pb;
218 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
220 dev_err(&pdev->dev, "failed to find platform data\n");
228 ret = data->init(&pdev->dev);
233 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
235 dev_err(&pdev->dev, "no memory for state\n");
241 max = data->levels[data->max_brightness];
242 pb->levels = data->levels;
244 max = data->max_brightness;
246 pb->enable_gpio = data->enable_gpio;
247 pb->enable_gpio_flags = data->enable_gpio_flags;
248 pb->notify = data->notify;
249 pb->notify_after = data->notify_after;
250 pb->check_fb = data->check_fb;
251 pb->exit = data->exit;
252 pb->dev = &pdev->dev;
255 if (gpio_is_valid(pb->enable_gpio)) {
258 if (pb->enable_gpio_flags & PWM_BACKLIGHT_GPIO_ACTIVE_LOW)
259 flags = GPIOF_OUT_INIT_HIGH;
261 flags = GPIOF_OUT_INIT_LOW;
263 ret = gpio_request_one(pb->enable_gpio, flags, "enable");
265 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
266 pb->enable_gpio, ret);
271 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
272 if (IS_ERR(pb->pwm)) {
273 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
275 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
276 if (IS_ERR(pb->pwm)) {
277 dev_err(&pdev->dev, "unable to request legacy PWM\n");
278 ret = PTR_ERR(pb->pwm);
283 dev_dbg(&pdev->dev, "got pwm for backlight\n");
286 * The DT case will set the pwm_period_ns field to 0 and store the
287 * period, parsed from the DT, in the PWM device. For the non-DT case,
288 * set the period from platform data.
290 if (data->pwm_period_ns > 0)
291 pwm_set_period(pb->pwm, data->pwm_period_ns);
293 pb->period = pwm_get_period(pb->pwm);
294 pb->lth_brightness = data->lth_brightness * (pb->period / max);
296 memset(&props, 0, sizeof(struct backlight_properties));
297 props.type = BACKLIGHT_RAW;
298 props.max_brightness = data->max_brightness;
299 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
300 &pwm_backlight_ops, &props);
302 dev_err(&pdev->dev, "failed to register backlight\n");
307 if (data->dft_brightness > data->max_brightness) {
309 "invalid default brightness level: %u, using %u\n",
310 data->dft_brightness, data->max_brightness);
311 data->dft_brightness = data->max_brightness;
314 bl->props.brightness = data->dft_brightness;
315 backlight_update_status(bl);
317 platform_set_drvdata(pdev, bl);
321 if (gpio_is_valid(pb->enable_gpio))
322 gpio_free(pb->enable_gpio);
325 data->exit(&pdev->dev);
329 static int pwm_backlight_remove(struct platform_device *pdev)
331 struct backlight_device *bl = platform_get_drvdata(pdev);
332 struct pwm_bl_data *pb = bl_get_data(bl);
334 backlight_device_unregister(bl);
335 pwm_backlight_power_off(pb);
338 pb->exit(&pdev->dev);
343 #ifdef CONFIG_PM_SLEEP
344 static int pwm_backlight_suspend(struct device *dev)
346 struct backlight_device *bl = dev_get_drvdata(dev);
347 struct pwm_bl_data *pb = bl_get_data(bl);
350 pb->notify(pb->dev, 0);
352 pwm_backlight_power_off(pb);
354 if (pb->notify_after)
355 pb->notify_after(pb->dev, 0);
360 static int pwm_backlight_resume(struct device *dev)
362 struct backlight_device *bl = dev_get_drvdata(dev);
364 backlight_update_status(bl);
370 static SIMPLE_DEV_PM_OPS(pwm_backlight_pm_ops, pwm_backlight_suspend,
371 pwm_backlight_resume);
373 static struct platform_driver pwm_backlight_driver = {
375 .name = "pwm-backlight",
376 .owner = THIS_MODULE,
377 .pm = &pwm_backlight_pm_ops,
378 .of_match_table = of_match_ptr(pwm_backlight_of_match),
380 .probe = pwm_backlight_probe,
381 .remove = pwm_backlight_remove,
384 module_platform_driver(pwm_backlight_driver);
386 MODULE_DESCRIPTION("PWM based Backlight Driver");
387 MODULE_LICENSE("GPL");
388 MODULE_ALIAS("platform:pwm-backlight");