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