blob: 069983ca49ff89da5d241649283e11f2699b6d9e [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 {
Alexandre Courbot9fb978b2012-07-09 15:04:23 +090057 int duty_cycle;
58
Thierry Reding3e3ed6c2011-12-16 21:25:29 +010059 if (pb->levels) {
Alexandre Courbot9fb978b2012-07-09 15:04:23 +090060 duty_cycle = pb->levels[brightness];
Thierry Reding3e3ed6c2011-12-16 21:25:29 +010061 max = pb->levels[max];
Alexandre Courbot9fb978b2012-07-09 15:04:23 +090062 } else {
63 duty_cycle = brightness;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +010064 }
65
Alexandre Courbot9fb978b2012-07-09 15:04:23 +090066 duty_cycle = pb->lth_brightness +
67 (duty_cycle * (pb->period - pb->lth_brightness) / max);
68 pwm_config(pb->pwm, duty_cycle, pb->period);
eric miao42796d32008-04-14 09:35:08 +010069 pwm_enable(pb->pwm);
70 }
Dilan Leecc7993f2011-08-25 15:59:17 -070071
72 if (pb->notify_after)
73 pb->notify_after(pb->dev, brightness);
74
eric miao42796d32008-04-14 09:35:08 +010075 return 0;
76}
77
78static int pwm_backlight_get_brightness(struct backlight_device *bl)
79{
80 return bl->props.brightness;
81}
82
Robert Morellef0a5e82011-03-22 16:30:31 -070083static int pwm_backlight_check_fb(struct backlight_device *bl,
84 struct fb_info *info)
85{
86 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
87
88 return !pb->check_fb || pb->check_fb(pb->dev, info);
89}
90
Emese Revfy9905a432009-12-14 00:58:57 +010091static const struct backlight_ops pwm_backlight_ops = {
eric miao42796d32008-04-14 09:35:08 +010092 .update_status = pwm_backlight_update_status,
93 .get_brightness = pwm_backlight_get_brightness,
Robert Morellef0a5e82011-03-22 16:30:31 -070094 .check_fb = pwm_backlight_check_fb,
eric miao42796d32008-04-14 09:35:08 +010095};
96
Thierry Reding3e3ed6c2011-12-16 21:25:29 +010097#ifdef CONFIG_OF
98static int pwm_backlight_parse_dt(struct device *dev,
99 struct platform_pwm_backlight_data *data)
100{
101 struct device_node *node = dev->of_node;
102 struct property *prop;
103 int length;
104 u32 value;
105 int ret;
106
107 if (!node)
108 return -ENODEV;
109
110 memset(data, 0, sizeof(*data));
111
112 /* determine the number of brightness levels */
113 prop = of_find_property(node, "brightness-levels", &length);
114 if (!prop)
115 return -EINVAL;
116
117 data->max_brightness = length / sizeof(u32);
118
119 /* read brightness levels from DT property */
120 if (data->max_brightness > 0) {
121 size_t size = sizeof(*data->levels) * data->max_brightness;
122
123 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
124 if (!data->levels)
125 return -ENOMEM;
126
127 ret = of_property_read_u32_array(node, "brightness-levels",
128 data->levels,
129 data->max_brightness);
130 if (ret < 0)
131 return ret;
132
133 ret = of_property_read_u32(node, "default-brightness-level",
134 &value);
135 if (ret < 0)
136 return ret;
137
138 if (value >= data->max_brightness) {
139 dev_warn(dev, "invalid default brightness level: %u, using %u\n",
140 value, data->max_brightness - 1);
141 value = data->max_brightness - 1;
142 }
143
144 data->dft_brightness = value;
145 data->max_brightness--;
146 }
147
148 /*
149 * TODO: Most users of this driver use a number of GPIOs to control
150 * backlight power. Support for specifying these needs to be
151 * added.
152 */
153
154 return 0;
155}
156
157static struct of_device_id pwm_backlight_of_match[] = {
158 { .compatible = "pwm-backlight" },
159 { }
160};
161
162MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
163#else
164static int pwm_backlight_parse_dt(struct device *dev,
165 struct platform_pwm_backlight_data *data)
166{
167 return -ENODEV;
168}
169#endif
170
eric miao42796d32008-04-14 09:35:08 +0100171static int pwm_backlight_probe(struct platform_device *pdev)
172{
173 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100174 struct platform_pwm_backlight_data defdata;
175 struct backlight_properties props;
eric miao42796d32008-04-14 09:35:08 +0100176 struct backlight_device *bl;
177 struct pwm_bl_data *pb;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100178 unsigned int max;
Philipp Zabel3b731252008-05-22 14:18:40 +0100179 int ret;
eric miao42796d32008-04-14 09:35:08 +0100180
Ben Dooks14563a42008-08-05 13:01:22 -0700181 if (!data) {
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100182 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
183 if (ret < 0) {
184 dev_err(&pdev->dev, "failed to find platform data\n");
185 return ret;
186 }
187
188 data = &defdata;
Ben Dooks14563a42008-08-05 13:01:22 -0700189 }
eric miao42796d32008-04-14 09:35:08 +0100190
Philipp Zabel3b731252008-05-22 14:18:40 +0100191 if (data->init) {
192 ret = data->init(&pdev->dev);
193 if (ret < 0)
194 return ret;
195 }
196
Julia Lawallce969222012-03-23 15:02:00 -0700197 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
Philipp Zabel3b731252008-05-22 14:18:40 +0100198 if (!pb) {
Ben Dooks14563a42008-08-05 13:01:22 -0700199 dev_err(&pdev->dev, "no memory for state\n");
Philipp Zabel3b731252008-05-22 14:18:40 +0100200 ret = -ENOMEM;
201 goto err_alloc;
202 }
eric miao42796d32008-04-14 09:35:08 +0100203
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100204 if (data->levels) {
205 max = data->levels[data->max_brightness];
206 pb->levels = data->levels;
207 } else
208 max = data->max_brightness;
209
Philipp Zabel3b731252008-05-22 14:18:40 +0100210 pb->notify = data->notify;
Dilan Leecc7993f2011-08-25 15:59:17 -0700211 pb->notify_after = data->notify_after;
Robert Morellef0a5e82011-03-22 16:30:31 -0700212 pb->check_fb = data->check_fb;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100213 pb->exit = data->exit;
Ben Dookscfc38992009-11-10 17:20:40 +0000214 pb->dev = &pdev->dev;
eric miao42796d32008-04-14 09:35:08 +0100215
Sachin Kamat60ce7022012-09-17 11:50:47 +0530216 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
Ben Dooks43bda1a2008-07-01 14:18:27 +0100217 if (IS_ERR(pb->pwm)) {
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100218 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
219
220 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
221 if (IS_ERR(pb->pwm)) {
222 dev_err(&pdev->dev, "unable to request legacy PWM\n");
223 ret = PTR_ERR(pb->pwm);
224 goto err_alloc;
225 }
226 }
227
228 dev_dbg(&pdev->dev, "got pwm for backlight\n");
229
230 /*
231 * The DT case will set the pwm_period_ns field to 0 and store the
232 * period, parsed from the DT, in the PWM device. For the non-DT case,
233 * set the period from platform data.
234 */
235 if (data->pwm_period_ns > 0)
236 pwm_set_period(pb->pwm, data->pwm_period_ns);
237
238 pb->period = pwm_get_period(pb->pwm);
239 pb->lth_brightness = data->lth_brightness * (pb->period / max);
eric miao42796d32008-04-14 09:35:08 +0100240
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500241 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700242 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500243 props.max_brightness = data->max_brightness;
244 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
245 &pwm_backlight_ops, &props);
eric miao42796d32008-04-14 09:35:08 +0100246 if (IS_ERR(bl)) {
247 dev_err(&pdev->dev, "failed to register backlight\n");
Philipp Zabel3b731252008-05-22 14:18:40 +0100248 ret = PTR_ERR(bl);
Sachin Kamat60ce7022012-09-17 11:50:47 +0530249 goto err_alloc;
eric miao42796d32008-04-14 09:35:08 +0100250 }
251
eric miao42796d32008-04-14 09:35:08 +0100252 bl->props.brightness = data->dft_brightness;
253 backlight_update_status(bl);
254
255 platform_set_drvdata(pdev, bl);
256 return 0;
Philipp Zabel3b731252008-05-22 14:18:40 +0100257
Philipp Zabel3b731252008-05-22 14:18:40 +0100258err_alloc:
259 if (data->exit)
260 data->exit(&pdev->dev);
261 return ret;
eric miao42796d32008-04-14 09:35:08 +0100262}
263
264static int pwm_backlight_remove(struct platform_device *pdev)
265{
266 struct backlight_device *bl = platform_get_drvdata(pdev);
267 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
268
269 backlight_device_unregister(bl);
270 pwm_config(pb->pwm, 0, pb->period);
271 pwm_disable(pb->pwm);
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100272 if (pb->exit)
273 pb->exit(&pdev->dev);
eric miao42796d32008-04-14 09:35:08 +0100274 return 0;
275}
276
277#ifdef CONFIG_PM
Mark Browne2c17bc2012-01-10 15:09:22 -0800278static int pwm_backlight_suspend(struct device *dev)
eric miao42796d32008-04-14 09:35:08 +0100279{
Mark Browne2c17bc2012-01-10 15:09:22 -0800280 struct backlight_device *bl = dev_get_drvdata(dev);
eric miao42796d32008-04-14 09:35:08 +0100281 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
282
Marc Zyngier82e8b542009-06-19 10:50:54 +0200283 if (pb->notify)
Ben Dookscfc38992009-11-10 17:20:40 +0000284 pb->notify(pb->dev, 0);
eric miao42796d32008-04-14 09:35:08 +0100285 pwm_config(pb->pwm, 0, pb->period);
286 pwm_disable(pb->pwm);
Dilan Leecc7993f2011-08-25 15:59:17 -0700287 if (pb->notify_after)
288 pb->notify_after(pb->dev, 0);
eric miao42796d32008-04-14 09:35:08 +0100289 return 0;
290}
291
Mark Browne2c17bc2012-01-10 15:09:22 -0800292static int pwm_backlight_resume(struct device *dev)
eric miao42796d32008-04-14 09:35:08 +0100293{
Mark Browne2c17bc2012-01-10 15:09:22 -0800294 struct backlight_device *bl = dev_get_drvdata(dev);
eric miao42796d32008-04-14 09:35:08 +0100295
296 backlight_update_status(bl);
297 return 0;
298}
Mark Browne2c17bc2012-01-10 15:09:22 -0800299
300static SIMPLE_DEV_PM_OPS(pwm_backlight_pm_ops, pwm_backlight_suspend,
301 pwm_backlight_resume);
302
eric miao42796d32008-04-14 09:35:08 +0100303#endif
304
305static struct platform_driver pwm_backlight_driver = {
306 .driver = {
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100307 .name = "pwm-backlight",
308 .owner = THIS_MODULE,
Mark Browne2c17bc2012-01-10 15:09:22 -0800309#ifdef CONFIG_PM
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100310 .pm = &pwm_backlight_pm_ops,
Mark Browne2c17bc2012-01-10 15:09:22 -0800311#endif
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100312 .of_match_table = of_match_ptr(pwm_backlight_of_match),
eric miao42796d32008-04-14 09:35:08 +0100313 },
314 .probe = pwm_backlight_probe,
315 .remove = pwm_backlight_remove,
eric miao42796d32008-04-14 09:35:08 +0100316};
317
Axel Lin81178e02012-01-10 15:09:11 -0800318module_platform_driver(pwm_backlight_driver);
eric miao42796d32008-04-14 09:35:08 +0100319
320MODULE_DESCRIPTION("PWM based Backlight Driver");
321MODULE_LICENSE("GPL");
Ben Dooks8cd68192008-08-05 13:01:24 -0700322MODULE_ALIAS("platform:pwm-backlight");
323