blob: 4af6d1302f174285cb2dda686a11a2bcaa3af390 [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
Alexandre Courbot01322672013-01-30 15:19:15 +090044 if (bl->props.power != FB_BLANK_UNBLANK ||
45 bl->props.fb_blank != FB_BLANK_UNBLANK ||
46 bl->props.state & BL_CORE_FBBLANK)
eric miao42796d32008-04-14 09:35:08 +010047 brightness = 0;
48
Philipp Zabel3b731252008-05-22 14:18:40 +010049 if (pb->notify)
Ben Dookscfc38992009-11-10 17:20:40 +000050 brightness = pb->notify(pb->dev, brightness);
Philipp Zabel3b731252008-05-22 14:18:40 +010051
eric miao42796d32008-04-14 09:35:08 +010052 if (brightness == 0) {
53 pwm_config(pb->pwm, 0, pb->period);
54 pwm_disable(pb->pwm);
55 } else {
Alexandre Courbot9fb978b2012-07-09 15:04:23 +090056 int duty_cycle;
57
Thierry Reding3e3ed6c2011-12-16 21:25:29 +010058 if (pb->levels) {
Alexandre Courbot9fb978b2012-07-09 15:04:23 +090059 duty_cycle = pb->levels[brightness];
Thierry Reding3e3ed6c2011-12-16 21:25:29 +010060 max = pb->levels[max];
Alexandre Courbot9fb978b2012-07-09 15:04:23 +090061 } else {
62 duty_cycle = brightness;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +010063 }
64
Alexandre Courbot9fb978b2012-07-09 15:04:23 +090065 duty_cycle = pb->lth_brightness +
66 (duty_cycle * (pb->period - pb->lth_brightness) / max);
67 pwm_config(pb->pwm, duty_cycle, pb->period);
eric miao42796d32008-04-14 09:35:08 +010068 pwm_enable(pb->pwm);
69 }
Dilan Leecc7993f2011-08-25 15:59:17 -070070
71 if (pb->notify_after)
72 pb->notify_after(pb->dev, brightness);
73
eric miao42796d32008-04-14 09:35:08 +010074 return 0;
75}
76
77static int pwm_backlight_get_brightness(struct backlight_device *bl)
78{
79 return bl->props.brightness;
80}
81
Robert Morellef0a5e82011-03-22 16:30:31 -070082static int pwm_backlight_check_fb(struct backlight_device *bl,
83 struct fb_info *info)
84{
85 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
86
87 return !pb->check_fb || pb->check_fb(pb->dev, info);
88}
89
Emese Revfy9905a432009-12-14 00:58:57 +010090static const struct backlight_ops pwm_backlight_ops = {
eric miao42796d32008-04-14 09:35:08 +010091 .update_status = pwm_backlight_update_status,
92 .get_brightness = pwm_backlight_get_brightness,
Robert Morellef0a5e82011-03-22 16:30:31 -070093 .check_fb = pwm_backlight_check_fb,
eric miao42796d32008-04-14 09:35:08 +010094};
95
Thierry Reding3e3ed6c2011-12-16 21:25:29 +010096#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
137 if (value >= data->max_brightness) {
138 dev_warn(dev, "invalid default brightness level: %u, using %u\n",
139 value, data->max_brightness - 1);
140 value = data->max_brightness - 1;
141 }
142
143 data->dft_brightness = value;
144 data->max_brightness--;
145 }
146
147 /*
148 * TODO: Most users of this driver use a number of GPIOs to control
149 * backlight power. Support for specifying these needs to be
150 * added.
151 */
152
153 return 0;
154}
155
156static struct of_device_id pwm_backlight_of_match[] = {
157 { .compatible = "pwm-backlight" },
158 { }
159};
160
161MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
162#else
163static int pwm_backlight_parse_dt(struct device *dev,
164 struct platform_pwm_backlight_data *data)
165{
166 return -ENODEV;
167}
168#endif
169
eric miao42796d32008-04-14 09:35:08 +0100170static int pwm_backlight_probe(struct platform_device *pdev)
171{
172 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100173 struct platform_pwm_backlight_data defdata;
174 struct backlight_properties props;
eric miao42796d32008-04-14 09:35:08 +0100175 struct backlight_device *bl;
176 struct pwm_bl_data *pb;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100177 unsigned int max;
Philipp Zabel3b731252008-05-22 14:18:40 +0100178 int ret;
eric miao42796d32008-04-14 09:35:08 +0100179
Ben Dooks14563a42008-08-05 13:01:22 -0700180 if (!data) {
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100181 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
182 if (ret < 0) {
183 dev_err(&pdev->dev, "failed to find platform data\n");
184 return ret;
185 }
186
187 data = &defdata;
Ben Dooks14563a42008-08-05 13:01:22 -0700188 }
eric miao42796d32008-04-14 09:35:08 +0100189
Philipp Zabel3b731252008-05-22 14:18:40 +0100190 if (data->init) {
191 ret = data->init(&pdev->dev);
192 if (ret < 0)
193 return ret;
194 }
195
Julia Lawallce969222012-03-23 15:02:00 -0700196 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
Philipp Zabel3b731252008-05-22 14:18:40 +0100197 if (!pb) {
Ben Dooks14563a42008-08-05 13:01:22 -0700198 dev_err(&pdev->dev, "no memory for state\n");
Philipp Zabel3b731252008-05-22 14:18:40 +0100199 ret = -ENOMEM;
200 goto err_alloc;
201 }
eric miao42796d32008-04-14 09:35:08 +0100202
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100203 if (data->levels) {
204 max = data->levels[data->max_brightness];
205 pb->levels = data->levels;
206 } else
207 max = data->max_brightness;
208
Philipp Zabel3b731252008-05-22 14:18:40 +0100209 pb->notify = data->notify;
Dilan Leecc7993f2011-08-25 15:59:17 -0700210 pb->notify_after = data->notify_after;
Robert Morellef0a5e82011-03-22 16:30:31 -0700211 pb->check_fb = data->check_fb;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100212 pb->exit = data->exit;
Ben Dookscfc38992009-11-10 17:20:40 +0000213 pb->dev = &pdev->dev;
eric miao42796d32008-04-14 09:35:08 +0100214
Sachin Kamat60ce7022012-09-17 11:50:47 +0530215 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
Ben Dooks43bda1a2008-07-01 14:18:27 +0100216 if (IS_ERR(pb->pwm)) {
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100217 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
218
219 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
220 if (IS_ERR(pb->pwm)) {
221 dev_err(&pdev->dev, "unable to request legacy PWM\n");
222 ret = PTR_ERR(pb->pwm);
223 goto err_alloc;
224 }
225 }
226
227 dev_dbg(&pdev->dev, "got pwm for backlight\n");
228
229 /*
230 * The DT case will set the pwm_period_ns field to 0 and store the
231 * period, parsed from the DT, in the PWM device. For the non-DT case,
232 * set the period from platform data.
233 */
234 if (data->pwm_period_ns > 0)
235 pwm_set_period(pb->pwm, data->pwm_period_ns);
236
237 pb->period = pwm_get_period(pb->pwm);
238 pb->lth_brightness = data->lth_brightness * (pb->period / max);
eric miao42796d32008-04-14 09:35:08 +0100239
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500240 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700241 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500242 props.max_brightness = data->max_brightness;
243 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
244 &pwm_backlight_ops, &props);
eric miao42796d32008-04-14 09:35:08 +0100245 if (IS_ERR(bl)) {
246 dev_err(&pdev->dev, "failed to register backlight\n");
Philipp Zabel3b731252008-05-22 14:18:40 +0100247 ret = PTR_ERR(bl);
Sachin Kamat60ce7022012-09-17 11:50:47 +0530248 goto err_alloc;
eric miao42796d32008-04-14 09:35:08 +0100249 }
250
eric miao42796d32008-04-14 09:35:08 +0100251 bl->props.brightness = data->dft_brightness;
252 backlight_update_status(bl);
253
254 platform_set_drvdata(pdev, bl);
255 return 0;
Philipp Zabel3b731252008-05-22 14:18:40 +0100256
Philipp Zabel3b731252008-05-22 14:18:40 +0100257err_alloc:
258 if (data->exit)
259 data->exit(&pdev->dev);
260 return ret;
eric miao42796d32008-04-14 09:35:08 +0100261}
262
263static int pwm_backlight_remove(struct platform_device *pdev)
264{
265 struct backlight_device *bl = platform_get_drvdata(pdev);
266 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
267
268 backlight_device_unregister(bl);
269 pwm_config(pb->pwm, 0, pb->period);
270 pwm_disable(pb->pwm);
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100271 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