pwm-backlight: Add rudimentary device tree support
[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
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/platform_device.h>
17#include <linux/fb.h>
18#include <linux/backlight.h>
19#include <linux/err.h>
20#include <linux/pwm.h>
21#include <linux/pwm_backlight.h>
5a0e3ad6 22#include <linux/slab.h>
42796d37 23
24struct pwm_bl_data {
25 struct pwm_device *pwm;
cfc3899f 26 struct device *dev;
42796d37 27 unsigned int period;
fef7764f 28 unsigned int lth_brightness;
3e3ed6cd 29 unsigned int *levels;
cfc3899f
BD
30 int (*notify)(struct device *,
31 int brightness);
cc7993f6
DL
32 void (*notify_after)(struct device *,
33 int brightness);
ef0a5e80 34 int (*check_fb)(struct device *, struct fb_info *);
3e3ed6cd 35 void (*exit)(struct device *);
42796d37 36};
37
38static int pwm_backlight_update_status(struct backlight_device *bl)
39{
40 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
41 int brightness = bl->props.brightness;
42 int max = bl->props.max_brightness;
43
44 if (bl->props.power != FB_BLANK_UNBLANK)
45 brightness = 0;
46
47 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
48 brightness = 0;
49
3b73125a 50 if (pb->notify)
cfc3899f 51 brightness = pb->notify(pb->dev, brightness);
3b73125a 52
42796d37 53 if (brightness == 0) {
54 pwm_config(pb->pwm, 0, pb->period);
55 pwm_disable(pb->pwm);
56 } else {
3e3ed6cd
TR
57 if (pb->levels) {
58 brightness = pb->levels[brightness];
59 max = pb->levels[max];
60 }
61
fef7764f
AM
62 brightness = pb->lth_brightness +
63 (brightness * (pb->period - pb->lth_brightness) / max);
64 pwm_config(pb->pwm, brightness, pb->period);
42796d37 65 pwm_enable(pb->pwm);
66 }
cc7993f6
DL
67
68 if (pb->notify_after)
69 pb->notify_after(pb->dev, brightness);
70
42796d37 71 return 0;
72}
73
74static int pwm_backlight_get_brightness(struct backlight_device *bl)
75{
76 return bl->props.brightness;
77}
78
ef0a5e80
RM
79static int pwm_backlight_check_fb(struct backlight_device *bl,
80 struct fb_info *info)
81{
82 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
83
84 return !pb->check_fb || pb->check_fb(pb->dev, info);
85}
86
9905a43b 87static const struct backlight_ops pwm_backlight_ops = {
42796d37 88 .update_status = pwm_backlight_update_status,
89 .get_brightness = pwm_backlight_get_brightness,
ef0a5e80 90 .check_fb = pwm_backlight_check_fb,
42796d37 91};
92
3e3ed6cd
TR
93#ifdef CONFIG_OF
94static int pwm_backlight_parse_dt(struct device *dev,
95 struct platform_pwm_backlight_data *data)
96{
97 struct device_node *node = dev->of_node;
98 struct property *prop;
99 int length;
100 u32 value;
101 int ret;
102
103 if (!node)
104 return -ENODEV;
105
106 memset(data, 0, sizeof(*data));
107
108 /* determine the number of brightness levels */
109 prop = of_find_property(node, "brightness-levels", &length);
110 if (!prop)
111 return -EINVAL;
112
113 data->max_brightness = length / sizeof(u32);
114
115 /* read brightness levels from DT property */
116 if (data->max_brightness > 0) {
117 size_t size = sizeof(*data->levels) * data->max_brightness;
118
119 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
120 if (!data->levels)
121 return -ENOMEM;
122
123 ret = of_property_read_u32_array(node, "brightness-levels",
124 data->levels,
125 data->max_brightness);
126 if (ret < 0)
127 return ret;
128
129 ret = of_property_read_u32(node, "default-brightness-level",
130 &value);
131 if (ret < 0)
132 return ret;
133
134 if (value >= data->max_brightness) {
135 dev_warn(dev, "invalid default brightness level: %u, using %u\n",
136 value, data->max_brightness - 1);
137 value = data->max_brightness - 1;
138 }
139
140 data->dft_brightness = value;
141 data->max_brightness--;
142 }
143
144 /*
145 * TODO: Most users of this driver use a number of GPIOs to control
146 * backlight power. Support for specifying these needs to be
147 * added.
148 */
149
150 return 0;
151}
152
153static struct of_device_id pwm_backlight_of_match[] = {
154 { .compatible = "pwm-backlight" },
155 { }
156};
157
158MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
159#else
160static int pwm_backlight_parse_dt(struct device *dev,
161 struct platform_pwm_backlight_data *data)
162{
163 return -ENODEV;
164}
165#endif
166
42796d37 167static int pwm_backlight_probe(struct platform_device *pdev)
168{
169 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
3e3ed6cd
TR
170 struct platform_pwm_backlight_data defdata;
171 struct backlight_properties props;
42796d37 172 struct backlight_device *bl;
173 struct pwm_bl_data *pb;
3e3ed6cd 174 unsigned int max;
3b73125a 175 int ret;
42796d37 176
14563a4e 177 if (!data) {
3e3ed6cd
TR
178 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
179 if (ret < 0) {
180 dev_err(&pdev->dev, "failed to find platform data\n");
181 return ret;
182 }
183
184 data = &defdata;
14563a4e 185 }
42796d37 186
3b73125a
PZ
187 if (data->init) {
188 ret = data->init(&pdev->dev);
189 if (ret < 0)
190 return ret;
191 }
192
ce969228 193 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
3b73125a 194 if (!pb) {
14563a4e 195 dev_err(&pdev->dev, "no memory for state\n");
3b73125a
PZ
196 ret = -ENOMEM;
197 goto err_alloc;
198 }
42796d37 199
3e3ed6cd
TR
200 if (data->levels) {
201 max = data->levels[data->max_brightness];
202 pb->levels = data->levels;
203 } else
204 max = data->max_brightness;
205
3b73125a 206 pb->notify = data->notify;
cc7993f6 207 pb->notify_after = data->notify_after;
ef0a5e80 208 pb->check_fb = data->check_fb;
3e3ed6cd 209 pb->exit = data->exit;
cfc3899f 210 pb->dev = &pdev->dev;
42796d37 211
3e3ed6cd 212 pb->pwm = pwm_get(&pdev->dev, NULL);
43bda1a6 213 if (IS_ERR(pb->pwm)) {
3e3ed6cd
TR
214 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
215
216 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
217 if (IS_ERR(pb->pwm)) {
218 dev_err(&pdev->dev, "unable to request legacy PWM\n");
219 ret = PTR_ERR(pb->pwm);
220 goto err_alloc;
221 }
222 }
223
224 dev_dbg(&pdev->dev, "got pwm for backlight\n");
225
226 /*
227 * The DT case will set the pwm_period_ns field to 0 and store the
228 * period, parsed from the DT, in the PWM device. For the non-DT case,
229 * set the period from platform data.
230 */
231 if (data->pwm_period_ns > 0)
232 pwm_set_period(pb->pwm, data->pwm_period_ns);
233
234 pb->period = pwm_get_period(pb->pwm);
235 pb->lth_brightness = data->lth_brightness * (pb->period / max);
42796d37 236
a19a6ee6 237 memset(&props, 0, sizeof(struct backlight_properties));
bb7ca747 238 props.type = BACKLIGHT_RAW;
a19a6ee6
MG
239 props.max_brightness = data->max_brightness;
240 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
241 &pwm_backlight_ops, &props);
42796d37 242 if (IS_ERR(bl)) {
243 dev_err(&pdev->dev, "failed to register backlight\n");
3b73125a
PZ
244 ret = PTR_ERR(bl);
245 goto err_bl;
42796d37 246 }
247
42796d37 248 bl->props.brightness = data->dft_brightness;
249 backlight_update_status(bl);
250
251 platform_set_drvdata(pdev, bl);
252 return 0;
3b73125a
PZ
253
254err_bl:
3e3ed6cd 255 pwm_put(pb->pwm);
3b73125a
PZ
256err_alloc:
257 if (data->exit)
258 data->exit(&pdev->dev);
259 return ret;
42796d37 260}
261
262static int pwm_backlight_remove(struct platform_device *pdev)
263{
264 struct backlight_device *bl = platform_get_drvdata(pdev);
265 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
266
267 backlight_device_unregister(bl);
268 pwm_config(pb->pwm, 0, pb->period);
269 pwm_disable(pb->pwm);
3e3ed6cd
TR
270 pwm_put(pb->pwm);
271 if (pb->exit)
272 pb->exit(&pdev->dev);
42796d37 273 return 0;
274}
275
276#ifdef CONFIG_PM
e2c17bc6 277static int pwm_backlight_suspend(struct device *dev)
42796d37 278{
e2c17bc6 279 struct backlight_device *bl = dev_get_drvdata(dev);
42796d37 280 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
281
82e8b542 282 if (pb->notify)
cfc3899f 283 pb->notify(pb->dev, 0);
42796d37 284 pwm_config(pb->pwm, 0, pb->period);
285 pwm_disable(pb->pwm);
cc7993f6
DL
286 if (pb->notify_after)
287 pb->notify_after(pb->dev, 0);
42796d37 288 return 0;
289}
290
e2c17bc6 291static int pwm_backlight_resume(struct device *dev)
42796d37 292{
e2c17bc6 293 struct backlight_device *bl = dev_get_drvdata(dev);
42796d37 294
295 backlight_update_status(bl);
296 return 0;
297}
e2c17bc6
MB
298
299static SIMPLE_DEV_PM_OPS(pwm_backlight_pm_ops, pwm_backlight_suspend,
300 pwm_backlight_resume);
301
42796d37 302#endif
303
304static struct platform_driver pwm_backlight_driver = {
305 .driver = {
3e3ed6cd
TR
306 .name = "pwm-backlight",
307 .owner = THIS_MODULE,
e2c17bc6 308#ifdef CONFIG_PM
3e3ed6cd 309 .pm = &pwm_backlight_pm_ops,
e2c17bc6 310#endif
3e3ed6cd 311 .of_match_table = of_match_ptr(pwm_backlight_of_match),
42796d37 312 },
313 .probe = pwm_backlight_probe,
314 .remove = pwm_backlight_remove,
42796d37 315};
316
81178e02 317module_platform_driver(pwm_backlight_driver);
42796d37 318
319MODULE_DESCRIPTION("PWM based Backlight Driver");
320MODULE_LICENSE("GPL");
8cd68198
BD
321MODULE_ALIAS("platform:pwm-backlight");
322
This page took 0.820682 seconds and 5 git commands to generate.