Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / video / backlight / pwm_bl.c
blob64f9e1b8655f4b29f64f95c6cf9b3a0e9cc04c6d
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/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>
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 struct gpio_desc *enable_gpio;
36 unsigned int scale;
37 bool legacy;
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 (pb->enable_gpio)
58 gpiod_set_value(pb->enable_gpio, 1);
60 pwm_enable(pb->pwm);
61 pb->enabled = true;
64 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
66 if (!pb->enabled)
67 return;
69 pwm_config(pb->pwm, 0, pb->period);
70 pwm_disable(pb->pwm);
72 if (pb->enable_gpio)
73 gpiod_set_value(pb->enable_gpio, 0);
75 regulator_disable(pb->power_supply);
76 pb->enabled = false;
79 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
81 unsigned int lth = pb->lth_brightness;
82 int duty_cycle;
84 if (pb->levels)
85 duty_cycle = pb->levels[brightness];
86 else
87 duty_cycle = brightness;
89 return (duty_cycle * (pb->period - lth) / pb->scale) + lth;
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 duty_cycle;
98 if (bl->props.power != FB_BLANK_UNBLANK ||
99 bl->props.fb_blank != FB_BLANK_UNBLANK ||
100 bl->props.state & BL_CORE_FBBLANK)
101 brightness = 0;
103 if (pb->notify)
104 brightness = pb->notify(pb->dev, brightness);
106 if (brightness > 0) {
107 duty_cycle = compute_duty_cycle(pb, brightness);
108 pwm_config(pb->pwm, duty_cycle, pb->period);
109 pwm_backlight_power_on(pb, brightness);
110 } else
111 pwm_backlight_power_off(pb);
113 if (pb->notify_after)
114 pb->notify_after(pb->dev, brightness);
116 return 0;
119 static int pwm_backlight_check_fb(struct backlight_device *bl,
120 struct fb_info *info)
122 struct pwm_bl_data *pb = bl_get_data(bl);
124 return !pb->check_fb || pb->check_fb(pb->dev, info);
127 static const struct backlight_ops pwm_backlight_ops = {
128 .update_status = pwm_backlight_update_status,
129 .check_fb = pwm_backlight_check_fb,
132 #ifdef CONFIG_OF
133 static int pwm_backlight_parse_dt(struct device *dev,
134 struct platform_pwm_backlight_data *data)
136 struct device_node *node = dev->of_node;
137 struct property *prop;
138 int length;
139 u32 value;
140 int ret;
142 if (!node)
143 return -ENODEV;
145 memset(data, 0, sizeof(*data));
147 /* determine the number of brightness levels */
148 prop = of_find_property(node, "brightness-levels", &length);
149 if (!prop)
150 return -EINVAL;
152 data->max_brightness = length / sizeof(u32);
154 /* read brightness levels from DT property */
155 if (data->max_brightness > 0) {
156 size_t size = sizeof(*data->levels) * data->max_brightness;
158 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
159 if (!data->levels)
160 return -ENOMEM;
162 ret = of_property_read_u32_array(node, "brightness-levels",
163 data->levels,
164 data->max_brightness);
165 if (ret < 0)
166 return ret;
168 ret = of_property_read_u32(node, "default-brightness-level",
169 &value);
170 if (ret < 0)
171 return ret;
173 data->dft_brightness = value;
174 data->max_brightness--;
177 data->enable_gpio = -EINVAL;
178 return 0;
181 static struct of_device_id pwm_backlight_of_match[] = {
182 { .compatible = "pwm-backlight" },
186 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
187 #else
188 static int pwm_backlight_parse_dt(struct device *dev,
189 struct platform_pwm_backlight_data *data)
191 return -ENODEV;
193 #endif
195 static int pwm_backlight_probe(struct platform_device *pdev)
197 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
198 struct platform_pwm_backlight_data defdata;
199 struct backlight_properties props;
200 struct backlight_device *bl;
201 struct device_node *node = pdev->dev.of_node;
202 struct pwm_bl_data *pb;
203 int initial_blank = FB_BLANK_UNBLANK;
204 int ret;
206 if (!data) {
207 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
208 if (ret < 0) {
209 dev_err(&pdev->dev, "failed to find platform data\n");
210 return ret;
213 data = &defdata;
216 if (data->init) {
217 ret = data->init(&pdev->dev);
218 if (ret < 0)
219 return ret;
222 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
223 if (!pb) {
224 ret = -ENOMEM;
225 goto err_alloc;
228 if (data->levels) {
229 unsigned int i;
231 for (i = 0; i <= data->max_brightness; i++)
232 if (data->levels[i] > pb->scale)
233 pb->scale = data->levels[i];
235 pb->levels = data->levels;
236 } else
237 pb->scale = data->max_brightness;
239 pb->notify = data->notify;
240 pb->notify_after = data->notify_after;
241 pb->check_fb = data->check_fb;
242 pb->exit = data->exit;
243 pb->dev = &pdev->dev;
244 pb->enabled = false;
246 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
247 GPIOD_ASIS);
248 if (IS_ERR(pb->enable_gpio)) {
249 ret = PTR_ERR(pb->enable_gpio);
250 goto err_alloc;
254 * Compatibility fallback for drivers still using the integer GPIO
255 * platform data. Must go away soon.
257 if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
258 ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
259 GPIOF_OUT_INIT_HIGH, "enable");
260 if (ret < 0) {
261 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
262 data->enable_gpio, ret);
263 goto err_alloc;
266 pb->enable_gpio = gpio_to_desc(data->enable_gpio);
269 if (pb->enable_gpio) {
271 * If the driver is probed from the device tree and there is a
272 * phandle link pointing to the backlight node, it is safe to
273 * assume that another driver will enable the backlight at the
274 * appropriate time. Therefore, if it is disabled, keep it so.
276 if (node && node->phandle &&
277 gpiod_get_direction(pb->enable_gpio) == GPIOF_DIR_OUT &&
278 gpiod_get_value(pb->enable_gpio) == 0)
279 initial_blank = FB_BLANK_POWERDOWN;
280 else
281 gpiod_direction_output(pb->enable_gpio, 1);
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_alloc;
290 if (node && node->phandle && !regulator_is_enabled(pb->power_supply))
291 initial_blank = FB_BLANK_POWERDOWN;
293 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
294 if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
295 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
296 pb->legacy = true;
297 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
300 if (IS_ERR(pb->pwm)) {
301 ret = PTR_ERR(pb->pwm);
302 if (ret != -EPROBE_DEFER)
303 dev_err(&pdev->dev, "unable to request PWM\n");
304 goto err_alloc;
307 dev_dbg(&pdev->dev, "got pwm for backlight\n");
310 * The DT case will set the pwm_period_ns field to 0 and store the
311 * period, parsed from the DT, in the PWM device. For the non-DT case,
312 * set the period from platform data if it has not already been set
313 * via the PWM lookup table.
315 pb->period = pwm_get_period(pb->pwm);
316 if (!pb->period && (data->pwm_period_ns > 0)) {
317 pb->period = data->pwm_period_ns;
318 pwm_set_period(pb->pwm, data->pwm_period_ns);
321 pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
323 memset(&props, 0, sizeof(struct backlight_properties));
324 props.type = BACKLIGHT_RAW;
325 props.max_brightness = data->max_brightness;
326 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
327 &pwm_backlight_ops, &props);
328 if (IS_ERR(bl)) {
329 dev_err(&pdev->dev, "failed to register backlight\n");
330 ret = PTR_ERR(bl);
331 if (pb->legacy)
332 pwm_free(pb->pwm);
333 goto err_alloc;
336 if (data->dft_brightness > data->max_brightness) {
337 dev_warn(&pdev->dev,
338 "invalid default brightness level: %u, using %u\n",
339 data->dft_brightness, data->max_brightness);
340 data->dft_brightness = data->max_brightness;
343 bl->props.brightness = data->dft_brightness;
344 bl->props.power = initial_blank;
345 backlight_update_status(bl);
347 platform_set_drvdata(pdev, bl);
348 return 0;
350 err_alloc:
351 if (data->exit)
352 data->exit(&pdev->dev);
353 return ret;
356 static int pwm_backlight_remove(struct platform_device *pdev)
358 struct backlight_device *bl = platform_get_drvdata(pdev);
359 struct pwm_bl_data *pb = bl_get_data(bl);
361 backlight_device_unregister(bl);
362 pwm_backlight_power_off(pb);
364 if (pb->exit)
365 pb->exit(&pdev->dev);
366 if (pb->legacy)
367 pwm_free(pb->pwm);
369 return 0;
372 static void pwm_backlight_shutdown(struct platform_device *pdev)
374 struct backlight_device *bl = platform_get_drvdata(pdev);
375 struct pwm_bl_data *pb = bl_get_data(bl);
377 pwm_backlight_power_off(pb);
380 #ifdef CONFIG_PM_SLEEP
381 static int pwm_backlight_suspend(struct device *dev)
383 struct backlight_device *bl = dev_get_drvdata(dev);
384 struct pwm_bl_data *pb = bl_get_data(bl);
386 if (pb->notify)
387 pb->notify(pb->dev, 0);
389 pwm_backlight_power_off(pb);
391 if (pb->notify_after)
392 pb->notify_after(pb->dev, 0);
394 return 0;
397 static int pwm_backlight_resume(struct device *dev)
399 struct backlight_device *bl = dev_get_drvdata(dev);
401 backlight_update_status(bl);
403 return 0;
405 #endif
407 static const struct dev_pm_ops pwm_backlight_pm_ops = {
408 #ifdef CONFIG_PM_SLEEP
409 .suspend = pwm_backlight_suspend,
410 .resume = pwm_backlight_resume,
411 .poweroff = pwm_backlight_suspend,
412 .restore = pwm_backlight_resume,
413 #endif
416 static struct platform_driver pwm_backlight_driver = {
417 .driver = {
418 .name = "pwm-backlight",
419 .pm = &pwm_backlight_pm_ops,
420 .of_match_table = of_match_ptr(pwm_backlight_of_match),
422 .probe = pwm_backlight_probe,
423 .remove = pwm_backlight_remove,
424 .shutdown = pwm_backlight_shutdown,
427 module_platform_driver(pwm_backlight_driver);
429 MODULE_DESCRIPTION("PWM based Backlight Driver");
430 MODULE_LICENSE("GPL");
431 MODULE_ALIAS("platform:pwm-backlight");