of/platform: Initialise default DMA masks
[linux/fpc-iii.git] / drivers / video / backlight / pwm_bl.c
blob44ac5bde4e9d1ad6a39e32c57d3350ffe0bdd8ab
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/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/gpio.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/platform_device.h>
20 #include <linux/fb.h>
21 #include <linux/backlight.h>
22 #include <linux/err.h>
23 #include <linux/pwm.h>
24 #include <linux/pwm_backlight.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/slab.h>
28 struct pwm_bl_data {
29 struct pwm_device *pwm;
30 struct device *dev;
31 unsigned int period;
32 unsigned int lth_brightness;
33 unsigned int *levels;
34 bool enabled;
35 struct regulator *power_supply;
36 struct gpio_desc *enable_gpio;
37 unsigned int scale;
38 bool legacy;
39 unsigned int post_pwm_on_delay;
40 unsigned int pwm_off_delay;
41 int (*notify)(struct device *,
42 int brightness);
43 void (*notify_after)(struct device *,
44 int brightness);
45 int (*check_fb)(struct device *, struct fb_info *);
46 void (*exit)(struct device *);
49 static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
51 int err;
53 if (pb->enabled)
54 return;
56 err = regulator_enable(pb->power_supply);
57 if (err < 0)
58 dev_err(pb->dev, "failed to enable power supply\n");
60 pwm_enable(pb->pwm);
62 if (pb->post_pwm_on_delay)
63 msleep(pb->post_pwm_on_delay);
65 if (pb->enable_gpio)
66 gpiod_set_value_cansleep(pb->enable_gpio, 1);
68 pb->enabled = true;
71 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
73 if (!pb->enabled)
74 return;
76 if (pb->enable_gpio)
77 gpiod_set_value_cansleep(pb->enable_gpio, 0);
79 if (pb->pwm_off_delay)
80 msleep(pb->pwm_off_delay);
82 pwm_config(pb->pwm, 0, pb->period);
83 pwm_disable(pb->pwm);
85 regulator_disable(pb->power_supply);
86 pb->enabled = false;
89 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
91 unsigned int lth = pb->lth_brightness;
92 u64 duty_cycle;
94 if (pb->levels)
95 duty_cycle = pb->levels[brightness];
96 else
97 duty_cycle = brightness;
99 duty_cycle *= pb->period - lth;
100 do_div(duty_cycle, pb->scale);
102 return duty_cycle + lth;
105 static int pwm_backlight_update_status(struct backlight_device *bl)
107 struct pwm_bl_data *pb = bl_get_data(bl);
108 int brightness = bl->props.brightness;
109 int duty_cycle;
111 if (bl->props.power != FB_BLANK_UNBLANK ||
112 bl->props.fb_blank != FB_BLANK_UNBLANK ||
113 bl->props.state & BL_CORE_FBBLANK)
114 brightness = 0;
116 if (pb->notify)
117 brightness = pb->notify(pb->dev, brightness);
119 if (brightness > 0) {
120 duty_cycle = compute_duty_cycle(pb, brightness);
121 pwm_config(pb->pwm, duty_cycle, pb->period);
122 pwm_backlight_power_on(pb, brightness);
123 } else
124 pwm_backlight_power_off(pb);
126 if (pb->notify_after)
127 pb->notify_after(pb->dev, brightness);
129 return 0;
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 .check_fb = pwm_backlight_check_fb,
145 #ifdef CONFIG_OF
146 static int pwm_backlight_parse_dt(struct device *dev,
147 struct platform_pwm_backlight_data *data)
149 struct device_node *node = dev->of_node;
150 struct property *prop;
151 int length;
152 u32 value;
153 int ret;
155 if (!node)
156 return -ENODEV;
158 memset(data, 0, sizeof(*data));
160 /* determine the number of brightness levels */
161 prop = of_find_property(node, "brightness-levels", &length);
162 if (!prop)
163 return -EINVAL;
165 data->max_brightness = length / sizeof(u32);
167 /* read brightness levels from DT property */
168 if (data->max_brightness > 0) {
169 size_t size = sizeof(*data->levels) * data->max_brightness;
171 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
172 if (!data->levels)
173 return -ENOMEM;
175 ret = of_property_read_u32_array(node, "brightness-levels",
176 data->levels,
177 data->max_brightness);
178 if (ret < 0)
179 return ret;
181 ret = of_property_read_u32(node, "default-brightness-level",
182 &value);
183 if (ret < 0)
184 return ret;
186 data->dft_brightness = value;
187 data->max_brightness--;
191 * These values are optional and set as 0 by default, the out values
192 * are modified only if a valid u32 value can be decoded.
194 of_property_read_u32(node, "post-pwm-on-delay-ms",
195 &data->post_pwm_on_delay);
196 of_property_read_u32(node, "pwm-off-delay-ms", &data->pwm_off_delay);
198 data->enable_gpio = -EINVAL;
199 return 0;
202 static const struct of_device_id pwm_backlight_of_match[] = {
203 { .compatible = "pwm-backlight" },
207 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
208 #else
209 static int pwm_backlight_parse_dt(struct device *dev,
210 struct platform_pwm_backlight_data *data)
212 return -ENODEV;
214 #endif
216 static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
218 struct device_node *node = pb->dev->of_node;
220 /* Not booted with device tree or no phandle link to the node */
221 if (!node || !node->phandle)
222 return FB_BLANK_UNBLANK;
225 * If the driver is probed from the device tree and there is a
226 * phandle link pointing to the backlight node, it is safe to
227 * assume that another driver will enable the backlight at the
228 * appropriate time. Therefore, if it is disabled, keep it so.
231 /* if the enable GPIO is disabled, do not enable the backlight */
232 if (pb->enable_gpio && gpiod_get_value(pb->enable_gpio) == 0)
233 return FB_BLANK_POWERDOWN;
235 /* The regulator is disabled, do not enable the backlight */
236 if (!regulator_is_enabled(pb->power_supply))
237 return FB_BLANK_POWERDOWN;
239 /* The PWM is disabled, keep it like this */
240 if (!pwm_is_enabled(pb->pwm))
241 return FB_BLANK_POWERDOWN;
243 return FB_BLANK_UNBLANK;
246 static int pwm_backlight_probe(struct platform_device *pdev)
248 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
249 struct platform_pwm_backlight_data defdata;
250 struct backlight_properties props;
251 struct backlight_device *bl;
252 struct device_node *node = pdev->dev.of_node;
253 struct pwm_bl_data *pb;
254 struct pwm_args pargs;
255 int ret;
257 if (!data) {
258 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
259 if (ret < 0) {
260 dev_err(&pdev->dev, "failed to find platform data\n");
261 return ret;
264 data = &defdata;
267 if (data->init) {
268 ret = data->init(&pdev->dev);
269 if (ret < 0)
270 return ret;
273 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
274 if (!pb) {
275 ret = -ENOMEM;
276 goto err_alloc;
279 if (data->levels) {
280 unsigned int i;
282 for (i = 0; i <= data->max_brightness; i++)
283 if (data->levels[i] > pb->scale)
284 pb->scale = data->levels[i];
286 pb->levels = data->levels;
287 } else
288 pb->scale = data->max_brightness;
290 pb->notify = data->notify;
291 pb->notify_after = data->notify_after;
292 pb->check_fb = data->check_fb;
293 pb->exit = data->exit;
294 pb->dev = &pdev->dev;
295 pb->enabled = false;
296 pb->post_pwm_on_delay = data->post_pwm_on_delay;
297 pb->pwm_off_delay = data->pwm_off_delay;
299 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
300 GPIOD_ASIS);
301 if (IS_ERR(pb->enable_gpio)) {
302 ret = PTR_ERR(pb->enable_gpio);
303 goto err_alloc;
307 * Compatibility fallback for drivers still using the integer GPIO
308 * platform data. Must go away soon.
310 if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
311 ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
312 GPIOF_OUT_INIT_HIGH, "enable");
313 if (ret < 0) {
314 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
315 data->enable_gpio, ret);
316 goto err_alloc;
319 pb->enable_gpio = gpio_to_desc(data->enable_gpio);
323 * If the GPIO is not known to be already configured as output, that
324 * is, if gpiod_get_direction returns either 1 or -EINVAL, change the
325 * direction to output and set the GPIO as active.
326 * Do not force the GPIO to active when it was already output as it
327 * could cause backlight flickering or we would enable the backlight too
328 * early. Leave the decision of the initial backlight state for later.
330 if (pb->enable_gpio &&
331 gpiod_get_direction(pb->enable_gpio) != 0)
332 gpiod_direction_output(pb->enable_gpio, 1);
334 pb->power_supply = devm_regulator_get(&pdev->dev, "power");
335 if (IS_ERR(pb->power_supply)) {
336 ret = PTR_ERR(pb->power_supply);
337 goto err_alloc;
340 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
341 if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
342 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
343 pb->legacy = true;
344 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
347 if (IS_ERR(pb->pwm)) {
348 ret = PTR_ERR(pb->pwm);
349 if (ret != -EPROBE_DEFER)
350 dev_err(&pdev->dev, "unable to request PWM\n");
351 goto err_alloc;
354 dev_dbg(&pdev->dev, "got pwm for backlight\n");
357 * FIXME: pwm_apply_args() should be removed when switching to
358 * the atomic PWM API.
360 pwm_apply_args(pb->pwm);
363 * The DT case will set the pwm_period_ns field to 0 and store the
364 * period, parsed from the DT, in the PWM device. For the non-DT case,
365 * set the period from platform data if it has not already been set
366 * via the PWM lookup table.
368 pwm_get_args(pb->pwm, &pargs);
369 pb->period = pargs.period;
370 if (!pb->period && (data->pwm_period_ns > 0))
371 pb->period = data->pwm_period_ns;
373 pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
375 memset(&props, 0, sizeof(struct backlight_properties));
376 props.type = BACKLIGHT_RAW;
377 props.max_brightness = data->max_brightness;
378 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
379 &pwm_backlight_ops, &props);
380 if (IS_ERR(bl)) {
381 dev_err(&pdev->dev, "failed to register backlight\n");
382 ret = PTR_ERR(bl);
383 if (pb->legacy)
384 pwm_free(pb->pwm);
385 goto err_alloc;
388 if (data->dft_brightness > data->max_brightness) {
389 dev_warn(&pdev->dev,
390 "invalid default brightness level: %u, using %u\n",
391 data->dft_brightness, data->max_brightness);
392 data->dft_brightness = data->max_brightness;
395 bl->props.brightness = data->dft_brightness;
396 bl->props.power = pwm_backlight_initial_power_state(pb);
397 backlight_update_status(bl);
399 platform_set_drvdata(pdev, bl);
400 return 0;
402 err_alloc:
403 if (data->exit)
404 data->exit(&pdev->dev);
405 return ret;
408 static int pwm_backlight_remove(struct platform_device *pdev)
410 struct backlight_device *bl = platform_get_drvdata(pdev);
411 struct pwm_bl_data *pb = bl_get_data(bl);
413 backlight_device_unregister(bl);
414 pwm_backlight_power_off(pb);
416 if (pb->exit)
417 pb->exit(&pdev->dev);
418 if (pb->legacy)
419 pwm_free(pb->pwm);
421 return 0;
424 static void pwm_backlight_shutdown(struct platform_device *pdev)
426 struct backlight_device *bl = platform_get_drvdata(pdev);
427 struct pwm_bl_data *pb = bl_get_data(bl);
429 pwm_backlight_power_off(pb);
432 #ifdef CONFIG_PM_SLEEP
433 static int pwm_backlight_suspend(struct device *dev)
435 struct backlight_device *bl = dev_get_drvdata(dev);
436 struct pwm_bl_data *pb = bl_get_data(bl);
438 if (pb->notify)
439 pb->notify(pb->dev, 0);
441 pwm_backlight_power_off(pb);
443 if (pb->notify_after)
444 pb->notify_after(pb->dev, 0);
446 return 0;
449 static int pwm_backlight_resume(struct device *dev)
451 struct backlight_device *bl = dev_get_drvdata(dev);
453 backlight_update_status(bl);
455 return 0;
457 #endif
459 static const struct dev_pm_ops pwm_backlight_pm_ops = {
460 #ifdef CONFIG_PM_SLEEP
461 .suspend = pwm_backlight_suspend,
462 .resume = pwm_backlight_resume,
463 .poweroff = pwm_backlight_suspend,
464 .restore = pwm_backlight_resume,
465 #endif
468 static struct platform_driver pwm_backlight_driver = {
469 .driver = {
470 .name = "pwm-backlight",
471 .pm = &pwm_backlight_pm_ops,
472 .of_match_table = of_match_ptr(pwm_backlight_of_match),
474 .probe = pwm_backlight_probe,
475 .remove = pwm_backlight_remove,
476 .shutdown = pwm_backlight_shutdown,
479 module_platform_driver(pwm_backlight_driver);
481 MODULE_DESCRIPTION("PWM based Backlight Driver");
482 MODULE_LICENSE("GPL");
483 MODULE_ALIAS("platform:pwm-backlight");