PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / video / backlight / pwm_bl.c
blobb75201ff46f6d1bc133a5cae571b90082d11347d
1 /*
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>
19 #include <linux/fb.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>
27 struct pwm_bl_data {
28 struct pwm_device *pwm;
29 struct device *dev;
30 unsigned int period;
31 unsigned int lth_brightness;
32 unsigned int *levels;
33 bool enabled;
34 struct regulator *power_supply;
35 int enable_gpio;
36 unsigned long enable_gpio_flags;
37 unsigned int scale;
38 int (*notify)(struct device *,
39 int brightness);
40 void (*notify_after)(struct device *,
41 int brightness);
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)
48 int err;
50 if (pb->enabled)
51 return;
53 err = regulator_enable(pb->power_supply);
54 if (err < 0)
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);
60 else
61 gpio_set_value(pb->enable_gpio, 1);
64 pwm_enable(pb->pwm);
65 pb->enabled = true;
68 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
70 if (!pb->enabled)
71 return;
73 pwm_config(pb->pwm, 0, pb->period);
74 pwm_disable(pb->pwm);
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);
79 else
80 gpio_set_value(pb->enable_gpio, 0);
83 regulator_disable(pb->power_supply);
84 pb->enabled = false;
87 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
89 unsigned int lth = pb->lth_brightness;
90 int duty_cycle;
92 if (pb->levels)
93 duty_cycle = pb->levels[brightness];
94 else
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;
104 int duty_cycle;
106 if (bl->props.power != FB_BLANK_UNBLANK ||
107 bl->props.fb_blank != FB_BLANK_UNBLANK ||
108 bl->props.state & BL_CORE_FBBLANK)
109 brightness = 0;
111 if (pb->notify)
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);
118 } else
119 pwm_backlight_power_off(pb);
121 if (pb->notify_after)
122 pb->notify_after(pb->dev, brightness);
124 return 0;
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,
146 #ifdef CONFIG_OF
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;
153 int length;
154 u32 value;
155 int ret;
157 if (!node)
158 return -ENODEV;
160 memset(data, 0, sizeof(*data));
162 /* determine the number of brightness levels */
163 prop = of_find_property(node, "brightness-levels", &length);
164 if (!prop)
165 return -EINVAL;
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);
174 if (!data->levels)
175 return -ENOMEM;
177 ret = of_property_read_u32_array(node, "brightness-levels",
178 data->levels,
179 data->max_brightness);
180 if (ret < 0)
181 return ret;
183 ret = of_property_read_u32(node, "default-brightness-level",
184 &value);
185 if (ret < 0)
186 return ret;
188 data->dft_brightness = value;
189 data->max_brightness--;
192 data->enable_gpio = of_get_named_gpio_flags(node, "enable-gpios", 0,
193 &flags);
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;
200 return 0;
203 static struct of_device_id pwm_backlight_of_match[] = {
204 { .compatible = "pwm-backlight" },
208 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
209 #else
210 static int pwm_backlight_parse_dt(struct device *dev,
211 struct platform_pwm_backlight_data *data)
213 return -ENODEV;
215 #endif
217 static int pwm_backlight_probe(struct platform_device *pdev)
219 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
220 struct platform_pwm_backlight_data defdata;
221 struct backlight_properties props;
222 struct backlight_device *bl;
223 struct pwm_bl_data *pb;
224 int ret;
226 if (!data) {
227 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
228 if (ret < 0) {
229 dev_err(&pdev->dev, "failed to find platform data\n");
230 return ret;
233 data = &defdata;
236 if (data->init) {
237 ret = data->init(&pdev->dev);
238 if (ret < 0)
239 return ret;
242 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
243 if (!pb) {
244 ret = -ENOMEM;
245 goto err_alloc;
248 if (data->levels) {
249 unsigned int i;
251 for (i = 0; i <= data->max_brightness; i++)
252 if (data->levels[i] > pb->scale)
253 pb->scale = data->levels[i];
255 pb->levels = data->levels;
256 } else
257 pb->scale = data->max_brightness;
259 pb->enable_gpio = data->enable_gpio;
260 pb->enable_gpio_flags = data->enable_gpio_flags;
261 pb->notify = data->notify;
262 pb->notify_after = data->notify_after;
263 pb->check_fb = data->check_fb;
264 pb->exit = data->exit;
265 pb->dev = &pdev->dev;
266 pb->enabled = false;
268 if (gpio_is_valid(pb->enable_gpio)) {
269 unsigned long flags;
271 if (pb->enable_gpio_flags & PWM_BACKLIGHT_GPIO_ACTIVE_LOW)
272 flags = GPIOF_OUT_INIT_HIGH;
273 else
274 flags = GPIOF_OUT_INIT_LOW;
276 ret = gpio_request_one(pb->enable_gpio, flags, "enable");
277 if (ret < 0) {
278 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
279 pb->enable_gpio, ret);
280 goto err_alloc;
284 pb->power_supply = devm_regulator_get(&pdev->dev, "power");
285 if (IS_ERR(pb->power_supply)) {
286 ret = PTR_ERR(pb->power_supply);
287 goto err_gpio;
290 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
291 if (IS_ERR(pb->pwm)) {
292 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
294 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
295 if (IS_ERR(pb->pwm)) {
296 dev_err(&pdev->dev, "unable to request legacy PWM\n");
297 ret = PTR_ERR(pb->pwm);
298 goto err_gpio;
302 dev_dbg(&pdev->dev, "got pwm for backlight\n");
305 * The DT case will set the pwm_period_ns field to 0 and store the
306 * period, parsed from the DT, in the PWM device. For the non-DT case,
307 * set the period from platform data.
309 if (data->pwm_period_ns > 0)
310 pwm_set_period(pb->pwm, data->pwm_period_ns);
312 pb->period = pwm_get_period(pb->pwm);
313 pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
315 memset(&props, 0, sizeof(struct backlight_properties));
316 props.type = BACKLIGHT_RAW;
317 props.max_brightness = data->max_brightness;
318 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
319 &pwm_backlight_ops, &props);
320 if (IS_ERR(bl)) {
321 dev_err(&pdev->dev, "failed to register backlight\n");
322 ret = PTR_ERR(bl);
323 goto err_gpio;
326 if (data->dft_brightness > data->max_brightness) {
327 dev_warn(&pdev->dev,
328 "invalid default brightness level: %u, using %u\n",
329 data->dft_brightness, data->max_brightness);
330 data->dft_brightness = data->max_brightness;
333 bl->props.brightness = data->dft_brightness;
334 backlight_update_status(bl);
336 platform_set_drvdata(pdev, bl);
337 return 0;
339 err_gpio:
340 if (gpio_is_valid(pb->enable_gpio))
341 gpio_free(pb->enable_gpio);
342 err_alloc:
343 if (data->exit)
344 data->exit(&pdev->dev);
345 return ret;
348 static int pwm_backlight_remove(struct platform_device *pdev)
350 struct backlight_device *bl = platform_get_drvdata(pdev);
351 struct pwm_bl_data *pb = bl_get_data(bl);
353 backlight_device_unregister(bl);
354 pwm_backlight_power_off(pb);
356 if (pb->exit)
357 pb->exit(&pdev->dev);
359 return 0;
362 #ifdef CONFIG_PM_SLEEP
363 static int pwm_backlight_suspend(struct device *dev)
365 struct backlight_device *bl = dev_get_drvdata(dev);
366 struct pwm_bl_data *pb = bl_get_data(bl);
368 if (pb->notify)
369 pb->notify(pb->dev, 0);
371 pwm_backlight_power_off(pb);
373 if (pb->notify_after)
374 pb->notify_after(pb->dev, 0);
376 return 0;
379 static int pwm_backlight_resume(struct device *dev)
381 struct backlight_device *bl = dev_get_drvdata(dev);
383 backlight_update_status(bl);
385 return 0;
387 #endif
389 static const struct dev_pm_ops pwm_backlight_pm_ops = {
390 #ifdef CONFIG_PM_SLEEP
391 .suspend = pwm_backlight_suspend,
392 .resume = pwm_backlight_resume,
393 .poweroff = pwm_backlight_suspend,
394 .restore = pwm_backlight_resume,
395 #endif
398 static struct platform_driver pwm_backlight_driver = {
399 .driver = {
400 .name = "pwm-backlight",
401 .owner = THIS_MODULE,
402 .pm = &pwm_backlight_pm_ops,
403 .of_match_table = of_match_ptr(pwm_backlight_of_match),
405 .probe = pwm_backlight_probe,
406 .remove = pwm_backlight_remove,
409 module_platform_driver(pwm_backlight_driver);
411 MODULE_DESCRIPTION("PWM based Backlight Driver");
412 MODULE_LICENSE("GPL");
413 MODULE_ALIAS("platform:pwm-backlight");