Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
6 * 2) platform_data being correctly configured
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
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>
26
27struct 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 *);
44};
45
46static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
47{
48 int err;
49
50 if (pb->enabled)
51 return;
52
53 err = regulator_enable(pb->power_supply);
54 if (err < 0)
55 dev_err(pb->dev, "failed to enable power supply\n");
56
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);
62 }
63
64 pwm_enable(pb->pwm);
65 pb->enabled = true;
66}
67
68static void pwm_backlight_power_off(struct pwm_bl_data *pb)
69{
70 if (!pb->enabled)
71 return;
72
73 pwm_config(pb->pwm, 0, pb->period);
74 pwm_disable(pb->pwm);
75
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);
81 }
82
83 regulator_disable(pb->power_supply);
84 pb->enabled = false;
85}
86
87static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
88{
89 unsigned int lth = pb->lth_brightness;
90 int duty_cycle;
91
92 if (pb->levels)
93 duty_cycle = pb->levels[brightness];
94 else
95 duty_cycle = brightness;
96
97 return (duty_cycle * (pb->period - lth) / pb->scale) + lth;
98}
99
100static int pwm_backlight_update_status(struct backlight_device *bl)
101{
102 struct pwm_bl_data *pb = bl_get_data(bl);
103 int brightness = bl->props.brightness;
104 int duty_cycle;
105
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;
110
111 if (pb->notify)
112 brightness = pb->notify(pb->dev, brightness);
113
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);
120
121 if (pb->notify_after)
122 pb->notify_after(pb->dev, brightness);
123
124 return 0;
125}
126
127static int pwm_backlight_get_brightness(struct backlight_device *bl)
128{
129 return bl->props.brightness;
130}
131
132static int pwm_backlight_check_fb(struct backlight_device *bl,
133 struct fb_info *info)
134{
135 struct pwm_bl_data *pb = bl_get_data(bl);
136
137 return !pb->check_fb || pb->check_fb(pb->dev, info);
138}
139
140static 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,
144};
145
146#ifdef CONFIG_OF
147static int pwm_backlight_parse_dt(struct device *dev,
148 struct platform_pwm_backlight_data *data)
149{
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;
156
157 if (!node)
158 return -ENODEV;
159
160 memset(data, 0, sizeof(*data));
161
162 /* determine the number of brightness levels */
163 prop = of_find_property(node, "brightness-levels", &length);
164 if (!prop)
165 return -EINVAL;
166
167 data->max_brightness = length / sizeof(u32);
168
169 /* read brightness levels from DT property */
170 if (data->max_brightness > 0) {
171 size_t size = sizeof(*data->levels) * data->max_brightness;
172
173 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
174 if (!data->levels)
175 return -ENOMEM;
176
177 ret = of_property_read_u32_array(node, "brightness-levels",
178 data->levels,
179 data->max_brightness);
180 if (ret < 0)
181 return ret;
182
183 ret = of_property_read_u32(node, "default-brightness-level",
184 &value);
185 if (ret < 0)
186 return ret;
187
188 data->dft_brightness = value;
189 data->max_brightness--;
190 }
191
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;
196
197 if (gpio_is_valid(data->enable_gpio) && (flags & OF_GPIO_ACTIVE_LOW))
198 data->enable_gpio_flags |= PWM_BACKLIGHT_GPIO_ACTIVE_LOW;
199
200 return 0;
201}
202
203static struct of_device_id pwm_backlight_of_match[] = {
204 { .compatible = "pwm-backlight" },
205 { }
206};
207
208MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
209#else
210static int pwm_backlight_parse_dt(struct device *dev,
211 struct platform_pwm_backlight_data *data)
212{
213 return -ENODEV;
214}
215#endif
216
217static int pwm_backlight_probe(struct platform_device *pdev)
218{
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;
225
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;
231 }
232
233 data = &defdata;
234 }
235
236 if (data->init) {
237 ret = data->init(&pdev->dev);
238 if (ret < 0)
239 return ret;
240 }
241
242 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
243 if (!pb) {
244 dev_err(&pdev->dev, "no memory for state\n");
245 ret = -ENOMEM;
246 goto err_alloc;
247 }
248
249 if (data->levels) {
250 unsigned int i;
251
252 for (i = 0; i <= data->max_brightness; i++)
253 if (data->levels[i] > pb->scale)
254 pb->scale = data->levels[i];
255
256 pb->levels = data->levels;
257 } else
258 pb->scale = data->max_brightness;
259
260 pb->enable_gpio = data->enable_gpio;
261 pb->enable_gpio_flags = data->enable_gpio_flags;
262 pb->notify = data->notify;
263 pb->notify_after = data->notify_after;
264 pb->check_fb = data->check_fb;
265 pb->exit = data->exit;
266 pb->dev = &pdev->dev;
267 pb->enabled = false;
268
269 if (gpio_is_valid(pb->enable_gpio)) {
270 unsigned long flags;
271
272 if (pb->enable_gpio_flags & PWM_BACKLIGHT_GPIO_ACTIVE_LOW)
273 flags = GPIOF_OUT_INIT_HIGH;
274 else
275 flags = GPIOF_OUT_INIT_LOW;
276
277 ret = gpio_request_one(pb->enable_gpio, flags, "enable");
278 if (ret < 0) {
279 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
280 pb->enable_gpio, ret);
281 goto err_alloc;
282 }
283 }
284
285 pb->power_supply = devm_regulator_get(&pdev->dev, "power");
286 if (IS_ERR(pb->power_supply)) {
287 ret = PTR_ERR(pb->power_supply);
288 goto err_gpio;
289 }
290
291 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
292 if (IS_ERR(pb->pwm)) {
293 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
294
295 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
296 if (IS_ERR(pb->pwm)) {
297 dev_err(&pdev->dev, "unable to request legacy PWM\n");
298 ret = PTR_ERR(pb->pwm);
299 goto err_gpio;
300 }
301 }
302
303 dev_dbg(&pdev->dev, "got pwm for backlight\n");
304
305 /*
306 * The DT case will set the pwm_period_ns field to 0 and store the
307 * period, parsed from the DT, in the PWM device. For the non-DT case,
308 * set the period from platform data.
309 */
310 if (data->pwm_period_ns > 0)
311 pwm_set_period(pb->pwm, data->pwm_period_ns);
312
313 pb->period = pwm_get_period(pb->pwm);
314 pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
315
316 memset(&props, 0, sizeof(struct backlight_properties));
317 props.type = BACKLIGHT_RAW;
318 props.max_brightness = data->max_brightness;
319 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
320 &pwm_backlight_ops, &props);
321 if (IS_ERR(bl)) {
322 dev_err(&pdev->dev, "failed to register backlight\n");
323 ret = PTR_ERR(bl);
324 goto err_gpio;
325 }
326
327 if (data->dft_brightness > data->max_brightness) {
328 dev_warn(&pdev->dev,
329 "invalid default brightness level: %u, using %u\n",
330 data->dft_brightness, data->max_brightness);
331 data->dft_brightness = data->max_brightness;
332 }
333
334 bl->props.brightness = data->dft_brightness;
335 backlight_update_status(bl);
336
337 platform_set_drvdata(pdev, bl);
338 return 0;
339
340err_gpio:
341 if (gpio_is_valid(pb->enable_gpio))
342 gpio_free(pb->enable_gpio);
343err_alloc:
344 if (data->exit)
345 data->exit(&pdev->dev);
346 return ret;
347}
348
349static int pwm_backlight_remove(struct platform_device *pdev)
350{
351 struct backlight_device *bl = platform_get_drvdata(pdev);
352 struct pwm_bl_data *pb = bl_get_data(bl);
353
354 backlight_device_unregister(bl);
355 pwm_backlight_power_off(pb);
356
357 if (pb->exit)
358 pb->exit(&pdev->dev);
359
360 return 0;
361}
362
363#ifdef CONFIG_PM_SLEEP
364static int pwm_backlight_suspend(struct device *dev)
365{
366 struct backlight_device *bl = dev_get_drvdata(dev);
367 struct pwm_bl_data *pb = bl_get_data(bl);
368
369 if (pb->notify)
370 pb->notify(pb->dev, 0);
371
372 pwm_backlight_power_off(pb);
373
374 if (pb->notify_after)
375 pb->notify_after(pb->dev, 0);
376
377 return 0;
378}
379
380static int pwm_backlight_resume(struct device *dev)
381{
382 struct backlight_device *bl = dev_get_drvdata(dev);
383
384 backlight_update_status(bl);
385
386 return 0;
387}
388#endif
389
390static const struct dev_pm_ops pwm_backlight_pm_ops = {
391#ifdef CONFIG_PM_SLEEP
392 .suspend = pwm_backlight_suspend,
393 .resume = pwm_backlight_resume,
394 .poweroff = pwm_backlight_suspend,
395 .restore = pwm_backlight_resume,
396#endif
397};
398
399static struct platform_driver pwm_backlight_driver = {
400 .driver = {
401 .name = "pwm-backlight",
402 .owner = THIS_MODULE,
403 .pm = &pwm_backlight_pm_ops,
404 .of_match_table = of_match_ptr(pwm_backlight_of_match),
405 },
406 .probe = pwm_backlight_probe,
407 .remove = pwm_backlight_remove,
408};
409
410module_platform_driver(pwm_backlight_driver);
411
412MODULE_DESCRIPTION("PWM based Backlight Driver");
413MODULE_LICENSE("GPL");
414MODULE_ALIAS("platform:pwm-backlight");