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