blob: 057389d69a51c5ce7d473ab45b058d9e2978fc34 [file] [log] [blame]
eric miao42796d32008-04-14 09:35:08 +01001/*
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
Eric Miaob8cdd872009-02-10 13:30:37 +08006 * 2) platform_data being correctly configured
eric miao42796d32008-04-14 09:35:08 +01007 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
eric miao42796d32008-04-14 09:35:08 +010023
24struct pwm_bl_data {
25 struct pwm_device *pwm;
Ben Dookscfc38992009-11-10 17:20:40 +000026 struct device *dev;
eric miao42796d32008-04-14 09:35:08 +010027 unsigned int period;
Arun Murthyfef77642010-11-11 14:05:28 -080028 unsigned int lth_brightness;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +010029 unsigned int *levels;
Ben Dookscfc38992009-11-10 17:20:40 +000030 int (*notify)(struct device *,
31 int brightness);
Dilan Leecc7993f2011-08-25 15:59:17 -070032 void (*notify_after)(struct device *,
33 int brightness);
Robert Morellef0a5e82011-03-22 16:30:31 -070034 int (*check_fb)(struct device *, struct fb_info *);
Thierry Reding3e3ed6c2011-12-16 21:25:29 +010035 void (*exit)(struct device *);
eric miao42796d32008-04-14 09:35:08 +010036};
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
Philipp Zabel3b731252008-05-22 14:18:40 +010050 if (pb->notify)
Ben Dookscfc38992009-11-10 17:20:40 +000051 brightness = pb->notify(pb->dev, brightness);
Philipp Zabel3b731252008-05-22 14:18:40 +010052
eric miao42796d32008-04-14 09:35:08 +010053 if (brightness == 0) {
54 pwm_config(pb->pwm, 0, pb->period);
55 pwm_disable(pb->pwm);
56 } else {
Thierry Reding3e3ed6c2011-12-16 21:25:29 +010057 if (pb->levels) {
58 brightness = pb->levels[brightness];
59 max = pb->levels[max];
60 }
61
Arun Murthyfef77642010-11-11 14:05:28 -080062 brightness = pb->lth_brightness +
63 (brightness * (pb->period - pb->lth_brightness) / max);
64 pwm_config(pb->pwm, brightness, pb->period);
eric miao42796d32008-04-14 09:35:08 +010065 pwm_enable(pb->pwm);
66 }
Dilan Leecc7993f2011-08-25 15:59:17 -070067
68 if (pb->notify_after)
69 pb->notify_after(pb->dev, brightness);
70
eric miao42796d32008-04-14 09:35:08 +010071 return 0;
72}
73
74static int pwm_backlight_get_brightness(struct backlight_device *bl)
75{
76 return bl->props.brightness;
77}
78
Robert Morellef0a5e82011-03-22 16:30:31 -070079static 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
Emese Revfy9905a432009-12-14 00:58:57 +010087static const struct backlight_ops pwm_backlight_ops = {
eric miao42796d32008-04-14 09:35:08 +010088 .update_status = pwm_backlight_update_status,
89 .get_brightness = pwm_backlight_get_brightness,
Robert Morellef0a5e82011-03-22 16:30:31 -070090 .check_fb = pwm_backlight_check_fb,
eric miao42796d32008-04-14 09:35:08 +010091};
92
Thierry Reding3e3ed6c2011-12-16 21:25:29 +010093#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
eric miao42796d32008-04-14 09:35:08 +0100167static int pwm_backlight_probe(struct platform_device *pdev)
168{
169 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100170 struct platform_pwm_backlight_data defdata;
171 struct backlight_properties props;
eric miao42796d32008-04-14 09:35:08 +0100172 struct backlight_device *bl;
173 struct pwm_bl_data *pb;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100174 unsigned int max;
Philipp Zabel3b731252008-05-22 14:18:40 +0100175 int ret;
eric miao42796d32008-04-14 09:35:08 +0100176
Ben Dooks14563a42008-08-05 13:01:22 -0700177 if (!data) {
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100178 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;
Ben Dooks14563a42008-08-05 13:01:22 -0700185 }
eric miao42796d32008-04-14 09:35:08 +0100186
Philipp Zabel3b731252008-05-22 14:18:40 +0100187 if (data->init) {
188 ret = data->init(&pdev->dev);
189 if (ret < 0)
190 return ret;
191 }
192
Julia Lawallce969222012-03-23 15:02:00 -0700193 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
Philipp Zabel3b731252008-05-22 14:18:40 +0100194 if (!pb) {
Ben Dooks14563a42008-08-05 13:01:22 -0700195 dev_err(&pdev->dev, "no memory for state\n");
Philipp Zabel3b731252008-05-22 14:18:40 +0100196 ret = -ENOMEM;
197 goto err_alloc;
198 }
eric miao42796d32008-04-14 09:35:08 +0100199
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100200 if (data->levels) {
201 max = data->levels[data->max_brightness];
202 pb->levels = data->levels;
203 } else
204 max = data->max_brightness;
205
Philipp Zabel3b731252008-05-22 14:18:40 +0100206 pb->notify = data->notify;
Dilan Leecc7993f2011-08-25 15:59:17 -0700207 pb->notify_after = data->notify_after;
Robert Morellef0a5e82011-03-22 16:30:31 -0700208 pb->check_fb = data->check_fb;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100209 pb->exit = data->exit;
Ben Dookscfc38992009-11-10 17:20:40 +0000210 pb->dev = &pdev->dev;
eric miao42796d32008-04-14 09:35:08 +0100211
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100212 pb->pwm = pwm_get(&pdev->dev, NULL);
Ben Dooks43bda1a2008-07-01 14:18:27 +0100213 if (IS_ERR(pb->pwm)) {
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100214 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);
eric miao42796d32008-04-14 09:35:08 +0100236
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500237 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700238 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500239 props.max_brightness = data->max_brightness;
240 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
241 &pwm_backlight_ops, &props);
eric miao42796d32008-04-14 09:35:08 +0100242 if (IS_ERR(bl)) {
243 dev_err(&pdev->dev, "failed to register backlight\n");
Philipp Zabel3b731252008-05-22 14:18:40 +0100244 ret = PTR_ERR(bl);
245 goto err_bl;
eric miao42796d32008-04-14 09:35:08 +0100246 }
247
eric miao42796d32008-04-14 09:35:08 +0100248 bl->props.brightness = data->dft_brightness;
249 backlight_update_status(bl);
250
251 platform_set_drvdata(pdev, bl);
252 return 0;
Philipp Zabel3b731252008-05-22 14:18:40 +0100253
254err_bl:
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100255 pwm_put(pb->pwm);
Philipp Zabel3b731252008-05-22 14:18:40 +0100256err_alloc:
257 if (data->exit)
258 data->exit(&pdev->dev);
259 return ret;
eric miao42796d32008-04-14 09:35:08 +0100260}
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);
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100270 pwm_put(pb->pwm);
271 if (pb->exit)
272 pb->exit(&pdev->dev);
eric miao42796d32008-04-14 09:35:08 +0100273 return 0;
274}
275
276#ifdef CONFIG_PM
Mark Browne2c17bc2012-01-10 15:09:22 -0800277static int pwm_backlight_suspend(struct device *dev)
eric miao42796d32008-04-14 09:35:08 +0100278{
Mark Browne2c17bc2012-01-10 15:09:22 -0800279 struct backlight_device *bl = dev_get_drvdata(dev);
eric miao42796d32008-04-14 09:35:08 +0100280 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
281
Marc Zyngier82e8b542009-06-19 10:50:54 +0200282 if (pb->notify)
Ben Dookscfc38992009-11-10 17:20:40 +0000283 pb->notify(pb->dev, 0);
eric miao42796d32008-04-14 09:35:08 +0100284 pwm_config(pb->pwm, 0, pb->period);
285 pwm_disable(pb->pwm);
Dilan Leecc7993f2011-08-25 15:59:17 -0700286 if (pb->notify_after)
287 pb->notify_after(pb->dev, 0);
eric miao42796d32008-04-14 09:35:08 +0100288 return 0;
289}
290
Mark Browne2c17bc2012-01-10 15:09:22 -0800291static int pwm_backlight_resume(struct device *dev)
eric miao42796d32008-04-14 09:35:08 +0100292{
Mark Browne2c17bc2012-01-10 15:09:22 -0800293 struct backlight_device *bl = dev_get_drvdata(dev);
eric miao42796d32008-04-14 09:35:08 +0100294
295 backlight_update_status(bl);
296 return 0;
297}
Mark Browne2c17bc2012-01-10 15:09:22 -0800298
299static SIMPLE_DEV_PM_OPS(pwm_backlight_pm_ops, pwm_backlight_suspend,
300 pwm_backlight_resume);
301
eric miao42796d32008-04-14 09:35:08 +0100302#endif
303
304static struct platform_driver pwm_backlight_driver = {
305 .driver = {
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100306 .name = "pwm-backlight",
307 .owner = THIS_MODULE,
Mark Browne2c17bc2012-01-10 15:09:22 -0800308#ifdef CONFIG_PM
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100309 .pm = &pwm_backlight_pm_ops,
Mark Browne2c17bc2012-01-10 15:09:22 -0800310#endif
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100311 .of_match_table = of_match_ptr(pwm_backlight_of_match),
eric miao42796d32008-04-14 09:35:08 +0100312 },
313 .probe = pwm_backlight_probe,
314 .remove = pwm_backlight_remove,
eric miao42796d32008-04-14 09:35:08 +0100315};
316
Axel Lin81178e02012-01-10 15:09:11 -0800317module_platform_driver(pwm_backlight_driver);
eric miao42796d32008-04-14 09:35:08 +0100318
319MODULE_DESCRIPTION("PWM based Backlight Driver");
320MODULE_LICENSE("GPL");
Ben Dooks8cd68192008-08-05 13:01:24 -0700321MODULE_ALIAS("platform:pwm-backlight");
322