Merge tag 'kvm-arm-for-4.8-take2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.git] / drivers / video / backlight / pwm_bl.c
CommitLineData
42796d37 1/*
2 * linux/drivers/video/backlight/pwm_bl.c
3 *
4 * simple PWM based backlight control, board code has to setup
5 * 1) pin configuration so PWM waveforms can output
b8cdd877 6 * 2) platform_data being correctly configured
42796d37 7 *
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.
11 */
12
257462db 13#include <linux/gpio/consumer.h>
8265b2e4 14#include <linux/gpio.h>
42796d37 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>
22ceeee1 24#include <linux/regulator/consumer.h>
5a0e3ad6 25#include <linux/slab.h>
42796d37 26
27struct pwm_bl_data {
28 struct pwm_device *pwm;
cfc3899f 29 struct device *dev;
42796d37 30 unsigned int period;
fef7764f 31 unsigned int lth_brightness;
3e3ed6cd 32 unsigned int *levels;
97c38437 33 bool enabled;
22ceeee1 34 struct regulator *power_supply;
257462db 35 struct gpio_desc *enable_gpio;
8f43e18e 36 unsigned int scale;
edf387b6 37 bool legacy;
cfc3899f
BD
38 int (*notify)(struct device *,
39 int brightness);
cc7993f6
DL
40 void (*notify_after)(struct device *,
41 int brightness);
ef0a5e80 42 int (*check_fb)(struct device *, struct fb_info *);
3e3ed6cd 43 void (*exit)(struct device *);
42796d37 44};
45
8f43e18e 46static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
62b744a8 47{
73d4e2b8 48 int err;
62b744a8 49
97c38437
TR
50 if (pb->enabled)
51 return;
52
22ceeee1
TR
53 err = regulator_enable(pb->power_supply);
54 if (err < 0)
55 dev_err(pb->dev, "failed to enable power supply\n");
56
257462db
AC
57 if (pb->enable_gpio)
58 gpiod_set_value(pb->enable_gpio, 1);
8265b2e4 59
62b744a8 60 pwm_enable(pb->pwm);
97c38437 61 pb->enabled = true;
62b744a8
TR
62}
63
64static void pwm_backlight_power_off(struct pwm_bl_data *pb)
65{
97c38437
TR
66 if (!pb->enabled)
67 return;
68
62b744a8
TR
69 pwm_config(pb->pwm, 0, pb->period);
70 pwm_disable(pb->pwm);
97c38437 71
257462db
AC
72 if (pb->enable_gpio)
73 gpiod_set_value(pb->enable_gpio, 0);
8265b2e4 74
22ceeee1 75 regulator_disable(pb->power_supply);
97c38437 76 pb->enabled = false;
62b744a8
TR
77}
78
e4bfeda9
TR
79static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
80{
81 unsigned int lth = pb->lth_brightness;
82 int duty_cycle;
83
84 if (pb->levels)
85 duty_cycle = pb->levels[brightness];
86 else
87 duty_cycle = brightness;
88
89 return (duty_cycle * (pb->period - lth) / pb->scale) + lth;
90}
91
42796d37 92static int pwm_backlight_update_status(struct backlight_device *bl)
93{
e6e3dbf9 94 struct pwm_bl_data *pb = bl_get_data(bl);
42796d37 95 int brightness = bl->props.brightness;
e4bfeda9 96 int duty_cycle;
42796d37 97
0132267d
AC
98 if (bl->props.power != FB_BLANK_UNBLANK ||
99 bl->props.fb_blank != FB_BLANK_UNBLANK ||
100 bl->props.state & BL_CORE_FBBLANK)
42796d37 101 brightness = 0;
102
3b73125a 103 if (pb->notify)
cfc3899f 104 brightness = pb->notify(pb->dev, brightness);
3b73125a 105
e4bfeda9
TR
106 if (brightness > 0) {
107 duty_cycle = compute_duty_cycle(pb, brightness);
9fb978b1 108 pwm_config(pb->pwm, duty_cycle, pb->period);
8f43e18e 109 pwm_backlight_power_on(pb, brightness);
e4bfeda9 110 } else
62b744a8 111 pwm_backlight_power_off(pb);
cc7993f6
DL
112
113 if (pb->notify_after)
114 pb->notify_after(pb->dev, brightness);
115
42796d37 116 return 0;
117}
118
ef0a5e80
RM
119static int pwm_backlight_check_fb(struct backlight_device *bl,
120 struct fb_info *info)
121{
e6e3dbf9 122 struct pwm_bl_data *pb = bl_get_data(bl);
ef0a5e80
RM
123
124 return !pb->check_fb || pb->check_fb(pb->dev, info);
125}
126
9905a43b 127static const struct backlight_ops pwm_backlight_ops = {
42796d37 128 .update_status = pwm_backlight_update_status,
ef0a5e80 129 .check_fb = pwm_backlight_check_fb,
42796d37 130};
131
3e3ed6cd
TR
132#ifdef CONFIG_OF
133static int pwm_backlight_parse_dt(struct device *dev,
134 struct platform_pwm_backlight_data *data)
135{
136 struct device_node *node = dev->of_node;
137 struct property *prop;
138 int length;
139 u32 value;
140 int ret;
141
142 if (!node)
143 return -ENODEV;
144
145 memset(data, 0, sizeof(*data));
146
147 /* determine the number of brightness levels */
148 prop = of_find_property(node, "brightness-levels", &length);
149 if (!prop)
150 return -EINVAL;
151
152 data->max_brightness = length / sizeof(u32);
153
154 /* read brightness levels from DT property */
155 if (data->max_brightness > 0) {
156 size_t size = sizeof(*data->levels) * data->max_brightness;
157
158 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
159 if (!data->levels)
160 return -ENOMEM;
161
162 ret = of_property_read_u32_array(node, "brightness-levels",
163 data->levels,
164 data->max_brightness);
165 if (ret < 0)
166 return ret;
167
168 ret = of_property_read_u32(node, "default-brightness-level",
169 &value);
170 if (ret < 0)
171 return ret;
172
3e3ed6cd
TR
173 data->dft_brightness = value;
174 data->max_brightness--;
175 }
176
937222c4 177 data->enable_gpio = -EINVAL;
3e3ed6cd
TR
178 return 0;
179}
180
181static struct of_device_id pwm_backlight_of_match[] = {
182 { .compatible = "pwm-backlight" },
183 { }
184};
185
186MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
187#else
188static int pwm_backlight_parse_dt(struct device *dev,
189 struct platform_pwm_backlight_data *data)
190{
191 return -ENODEV;
192}
193#endif
194
42796d37 195static int pwm_backlight_probe(struct platform_device *pdev)
196{
c512794c 197 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
3e3ed6cd
TR
198 struct platform_pwm_backlight_data defdata;
199 struct backlight_properties props;
42796d37 200 struct backlight_device *bl;
87770780 201 struct device_node *node = pdev->dev.of_node;
42796d37 202 struct pwm_bl_data *pb;
3698d7e7 203 int initial_blank = FB_BLANK_UNBLANK;
6cb9644d 204 struct pwm_args pargs;
3b73125a 205 int ret;
42796d37 206
14563a4e 207 if (!data) {
3e3ed6cd
TR
208 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
209 if (ret < 0) {
210 dev_err(&pdev->dev, "failed to find platform data\n");
211 return ret;
212 }
213
214 data = &defdata;
14563a4e 215 }
42796d37 216
3b73125a
PZ
217 if (data->init) {
218 ret = data->init(&pdev->dev);
219 if (ret < 0)
220 return ret;
221 }
222
ce969228 223 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
3b73125a
PZ
224 if (!pb) {
225 ret = -ENOMEM;
226 goto err_alloc;
227 }
42796d37 228
3e3ed6cd 229 if (data->levels) {
8f43e18e
MD
230 unsigned int i;
231
232 for (i = 0; i <= data->max_brightness; i++)
233 if (data->levels[i] > pb->scale)
234 pb->scale = data->levels[i];
235
3e3ed6cd
TR
236 pb->levels = data->levels;
237 } else
8f43e18e 238 pb->scale = data->max_brightness;
3e3ed6cd 239
3b73125a 240 pb->notify = data->notify;
cc7993f6 241 pb->notify_after = data->notify_after;
ef0a5e80 242 pb->check_fb = data->check_fb;
3e3ed6cd 243 pb->exit = data->exit;
cfc3899f 244 pb->dev = &pdev->dev;
97c38437 245 pb->enabled = false;
42796d37 246
cdaefcce 247 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
3698d7e7 248 GPIOD_ASIS);
257462db
AC
249 if (IS_ERR(pb->enable_gpio)) {
250 ret = PTR_ERR(pb->enable_gpio);
ff9c5422 251 goto err_alloc;
257462db 252 }
8265b2e4 253
257462db
AC
254 /*
255 * Compatibility fallback for drivers still using the integer GPIO
256 * platform data. Must go away soon.
257 */
258 if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
259 ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
260 GPIOF_OUT_INIT_HIGH, "enable");
8265b2e4
TR
261 if (ret < 0) {
262 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
257462db 263 data->enable_gpio, ret);
8265b2e4
TR
264 goto err_alloc;
265 }
257462db
AC
266
267 pb->enable_gpio = gpio_to_desc(data->enable_gpio);
8265b2e4
TR
268 }
269
3698d7e7
PZ
270 if (pb->enable_gpio) {
271 /*
272 * If the driver is probed from the device tree and there is a
273 * phandle link pointing to the backlight node, it is safe to
274 * assume that another driver will enable the backlight at the
275 * appropriate time. Therefore, if it is disabled, keep it so.
276 */
87770780 277 if (node && node->phandle &&
3698d7e7
PZ
278 gpiod_get_direction(pb->enable_gpio) == GPIOF_DIR_OUT &&
279 gpiod_get_value(pb->enable_gpio) == 0)
280 initial_blank = FB_BLANK_POWERDOWN;
281 else
282 gpiod_direction_output(pb->enable_gpio, 1);
283 }
284
22ceeee1
TR
285 pb->power_supply = devm_regulator_get(&pdev->dev, "power");
286 if (IS_ERR(pb->power_supply)) {
287 ret = PTR_ERR(pb->power_supply);
257462db 288 goto err_alloc;
22ceeee1 289 }
42796d37 290
87770780 291 if (node && node->phandle && !regulator_is_enabled(pb->power_supply))
3698d7e7
PZ
292 initial_blank = FB_BLANK_POWERDOWN;
293
60ce7028 294 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
87770780 295 if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
3e3ed6cd 296 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
edf387b6 297 pb->legacy = true;
3e3ed6cd 298 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
dc881123
VZ
299 }
300
301 if (IS_ERR(pb->pwm)) {
302 ret = PTR_ERR(pb->pwm);
303 if (ret != -EPROBE_DEFER)
304 dev_err(&pdev->dev, "unable to request PWM\n");
305 goto err_alloc;
3e3ed6cd
TR
306 }
307
308 dev_dbg(&pdev->dev, "got pwm for backlight\n");
309
6cb9644d
BB
310 /*
311 * FIXME: pwm_apply_args() should be removed when switching to
312 * the atomic PWM API.
313 */
314 pwm_apply_args(pb->pwm);
315
3e3ed6cd
TR
316 /*
317 * The DT case will set the pwm_period_ns field to 0 and store the
318 * period, parsed from the DT, in the PWM device. For the non-DT case,
16fc3352
AB
319 * set the period from platform data if it has not already been set
320 * via the PWM lookup table.
3e3ed6cd 321 */
6cb9644d
BB
322 pwm_get_args(pb->pwm, &pargs);
323 pb->period = pargs.period;
7f044b09 324 if (!pb->period && (data->pwm_period_ns > 0))
16fc3352 325 pb->period = data->pwm_period_ns;
3e3ed6cd 326
8f43e18e 327 pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
42796d37 328
a19a6ee6 329 memset(&props, 0, sizeof(struct backlight_properties));
bb7ca747 330 props.type = BACKLIGHT_RAW;
a19a6ee6
MG
331 props.max_brightness = data->max_brightness;
332 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
333 &pwm_backlight_ops, &props);
42796d37 334 if (IS_ERR(bl)) {
335 dev_err(&pdev->dev, "failed to register backlight\n");
3b73125a 336 ret = PTR_ERR(bl);
60d613d6
VZ
337 if (pb->legacy)
338 pwm_free(pb->pwm);
257462db 339 goto err_alloc;
42796d37 340 }
341
83cfd726
PU
342 if (data->dft_brightness > data->max_brightness) {
343 dev_warn(&pdev->dev,
344 "invalid default brightness level: %u, using %u\n",
345 data->dft_brightness, data->max_brightness);
346 data->dft_brightness = data->max_brightness;
347 }
348
42796d37 349 bl->props.brightness = data->dft_brightness;
3698d7e7 350 bl->props.power = initial_blank;
42796d37 351 backlight_update_status(bl);
352
353 platform_set_drvdata(pdev, bl);
354 return 0;
3b73125a 355
3b73125a
PZ
356err_alloc:
357 if (data->exit)
358 data->exit(&pdev->dev);
359 return ret;
42796d37 360}
361
362static int pwm_backlight_remove(struct platform_device *pdev)
363{
364 struct backlight_device *bl = platform_get_drvdata(pdev);
e6e3dbf9 365 struct pwm_bl_data *pb = bl_get_data(bl);
42796d37 366
367 backlight_device_unregister(bl);
62b744a8 368 pwm_backlight_power_off(pb);
668e63c6 369
3e3ed6cd
TR
370 if (pb->exit)
371 pb->exit(&pdev->dev);
edf387b6
VZ
372 if (pb->legacy)
373 pwm_free(pb->pwm);
668e63c6 374
42796d37 375 return 0;
376}
377
5f33b896
TR
378static void pwm_backlight_shutdown(struct platform_device *pdev)
379{
380 struct backlight_device *bl = platform_get_drvdata(pdev);
381 struct pwm_bl_data *pb = bl_get_data(bl);
382
383 pwm_backlight_power_off(pb);
384}
385
c791126b 386#ifdef CONFIG_PM_SLEEP
e2c17bc6 387static int pwm_backlight_suspend(struct device *dev)
42796d37 388{
e2c17bc6 389 struct backlight_device *bl = dev_get_drvdata(dev);
e6e3dbf9 390 struct pwm_bl_data *pb = bl_get_data(bl);
42796d37 391
82e8b542 392 if (pb->notify)
cfc3899f 393 pb->notify(pb->dev, 0);
668e63c6 394
62b744a8 395 pwm_backlight_power_off(pb);
668e63c6 396
cc7993f6
DL
397 if (pb->notify_after)
398 pb->notify_after(pb->dev, 0);
668e63c6 399
42796d37 400 return 0;
401}
402
e2c17bc6 403static int pwm_backlight_resume(struct device *dev)
42796d37 404{
e2c17bc6 405 struct backlight_device *bl = dev_get_drvdata(dev);
42796d37 406
407 backlight_update_status(bl);
668e63c6 408
42796d37 409 return 0;
410}
c791126b 411#endif
e2c17bc6 412
1dea1fd0
HL
413static const struct dev_pm_ops pwm_backlight_pm_ops = {
414#ifdef CONFIG_PM_SLEEP
415 .suspend = pwm_backlight_suspend,
416 .resume = pwm_backlight_resume,
417 .poweroff = pwm_backlight_suspend,
418 .restore = pwm_backlight_resume,
419#endif
420};
e2c17bc6 421
42796d37 422static struct platform_driver pwm_backlight_driver = {
423 .driver = {
3e3ed6cd 424 .name = "pwm-backlight",
3e3ed6cd 425 .pm = &pwm_backlight_pm_ops,
3e3ed6cd 426 .of_match_table = of_match_ptr(pwm_backlight_of_match),
42796d37 427 },
428 .probe = pwm_backlight_probe,
429 .remove = pwm_backlight_remove,
5f33b896 430 .shutdown = pwm_backlight_shutdown,
42796d37 431};
432
81178e02 433module_platform_driver(pwm_backlight_driver);
42796d37 434
435MODULE_DESCRIPTION("PWM based Backlight Driver");
436MODULE_LICENSE("GPL");
8cd68198 437MODULE_ALIAS("platform:pwm-backlight");
This page took 0.692542 seconds and 5 git commands to generate.