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