blob: 38ca88bc5c3ed0c199e73b8a533ac43448506bfb [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
Alexandre Courbot257462d2014-02-27 14:53:34 +090013#include <linux/gpio/consumer.h>
Thierry Reding8265b2e2013-08-30 12:32:18 +020014#include <linux/gpio.h>
eric miao42796d32008-04-14 09:35:08 +010015#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/platform_device.h>
19#include <linux/fb.h>
20#include <linux/backlight.h>
21#include <linux/err.h>
22#include <linux/pwm.h>
23#include <linux/pwm_backlight.h>
Thierry Reding22ceeee2013-08-30 12:38:34 +020024#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
eric miao42796d32008-04-14 09:35:08 +010026
27struct pwm_bl_data {
28 struct pwm_device *pwm;
Ben Dookscfc38992009-11-10 17:20:40 +000029 struct device *dev;
eric miao42796d32008-04-14 09:35:08 +010030 unsigned int period;
Arun Murthyfef77642010-11-11 14:05:28 -080031 unsigned int lth_brightness;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +010032 unsigned int *levels;
Thierry Reding97c38432013-10-02 18:01:02 +020033 bool enabled;
Thierry Reding22ceeee2013-08-30 12:38:34 +020034 struct regulator *power_supply;
Alexandre Courbot257462d2014-02-27 14:53:34 +090035 struct gpio_desc *enable_gpio;
Mike Dunn8f43e182013-09-22 09:59:56 -070036 unsigned int scale;
Ben Dookscfc38992009-11-10 17:20:40 +000037 int (*notify)(struct device *,
38 int brightness);
Dilan Leecc7993f2011-08-25 15:59:17 -070039 void (*notify_after)(struct device *,
40 int brightness);
Robert Morellef0a5e82011-03-22 16:30:31 -070041 int (*check_fb)(struct device *, struct fb_info *);
Thierry Reding3e3ed6c2011-12-16 21:25:29 +010042 void (*exit)(struct device *);
eric miao42796d32008-04-14 09:35:08 +010043};
44
Mike Dunn8f43e182013-09-22 09:59:56 -070045static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
Thierry Reding62b744a2013-10-07 11:32:02 +020046{
Thierry Reding73d4e2b2013-10-22 09:37:05 +020047 int err;
Thierry Reding62b744a2013-10-07 11:32:02 +020048
Thierry Reding97c38432013-10-02 18:01:02 +020049 if (pb->enabled)
50 return;
51
Thierry Reding22ceeee2013-08-30 12:38:34 +020052 err = regulator_enable(pb->power_supply);
53 if (err < 0)
54 dev_err(pb->dev, "failed to enable power supply\n");
55
Alexandre Courbot257462d2014-02-27 14:53:34 +090056 if (pb->enable_gpio)
57 gpiod_set_value(pb->enable_gpio, 1);
Thierry Reding8265b2e2013-08-30 12:32:18 +020058
Thierry Reding62b744a2013-10-07 11:32:02 +020059 pwm_enable(pb->pwm);
Thierry Reding97c38432013-10-02 18:01:02 +020060 pb->enabled = true;
Thierry Reding62b744a2013-10-07 11:32:02 +020061}
62
63static void pwm_backlight_power_off(struct pwm_bl_data *pb)
64{
Thierry Reding97c38432013-10-02 18:01:02 +020065 if (!pb->enabled)
66 return;
67
Thierry Reding62b744a2013-10-07 11:32:02 +020068 pwm_config(pb->pwm, 0, pb->period);
69 pwm_disable(pb->pwm);
Thierry Reding97c38432013-10-02 18:01:02 +020070
Alexandre Courbot257462d2014-02-27 14:53:34 +090071 if (pb->enable_gpio)
72 gpiod_set_value(pb->enable_gpio, 0);
Thierry Reding8265b2e2013-08-30 12:32:18 +020073
Thierry Reding22ceeee2013-08-30 12:38:34 +020074 regulator_disable(pb->power_supply);
Thierry Reding97c38432013-10-02 18:01:02 +020075 pb->enabled = false;
Thierry Reding62b744a2013-10-07 11:32:02 +020076}
77
Thierry Redinge4bfeda2013-10-18 10:46:24 +020078static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
79{
80 unsigned int lth = pb->lth_brightness;
81 int duty_cycle;
82
83 if (pb->levels)
84 duty_cycle = pb->levels[brightness];
85 else
86 duty_cycle = brightness;
87
88 return (duty_cycle * (pb->period - lth) / pb->scale) + lth;
89}
90
eric miao42796d32008-04-14 09:35:08 +010091static int pwm_backlight_update_status(struct backlight_device *bl)
92{
Jingoo Hane6e3dbf2013-02-21 16:43:46 -080093 struct pwm_bl_data *pb = bl_get_data(bl);
eric miao42796d32008-04-14 09:35:08 +010094 int brightness = bl->props.brightness;
Thierry Redinge4bfeda2013-10-18 10:46:24 +020095 int duty_cycle;
eric miao42796d32008-04-14 09:35:08 +010096
Alexandre Courbot01322672013-01-30 15:19:15 +090097 if (bl->props.power != FB_BLANK_UNBLANK ||
98 bl->props.fb_blank != FB_BLANK_UNBLANK ||
99 bl->props.state & BL_CORE_FBBLANK)
eric miao42796d32008-04-14 09:35:08 +0100100 brightness = 0;
101
Philipp Zabel3b731252008-05-22 14:18:40 +0100102 if (pb->notify)
Ben Dookscfc38992009-11-10 17:20:40 +0000103 brightness = pb->notify(pb->dev, brightness);
Philipp Zabel3b731252008-05-22 14:18:40 +0100104
Thierry Redinge4bfeda2013-10-18 10:46:24 +0200105 if (brightness > 0) {
106 duty_cycle = compute_duty_cycle(pb, brightness);
Alexandre Courbot9fb978b2012-07-09 15:04:23 +0900107 pwm_config(pb->pwm, duty_cycle, pb->period);
Mike Dunn8f43e182013-09-22 09:59:56 -0700108 pwm_backlight_power_on(pb, brightness);
Thierry Redinge4bfeda2013-10-18 10:46:24 +0200109 } else
Thierry Reding62b744a2013-10-07 11:32:02 +0200110 pwm_backlight_power_off(pb);
Dilan Leecc7993f2011-08-25 15:59:17 -0700111
112 if (pb->notify_after)
113 pb->notify_after(pb->dev, brightness);
114
eric miao42796d32008-04-14 09:35:08 +0100115 return 0;
116}
117
118static int pwm_backlight_get_brightness(struct backlight_device *bl)
119{
120 return bl->props.brightness;
121}
122
Robert Morellef0a5e82011-03-22 16:30:31 -0700123static int pwm_backlight_check_fb(struct backlight_device *bl,
124 struct fb_info *info)
125{
Jingoo Hane6e3dbf2013-02-21 16:43:46 -0800126 struct pwm_bl_data *pb = bl_get_data(bl);
Robert Morellef0a5e82011-03-22 16:30:31 -0700127
128 return !pb->check_fb || pb->check_fb(pb->dev, info);
129}
130
Emese Revfy9905a432009-12-14 00:58:57 +0100131static const struct backlight_ops pwm_backlight_ops = {
eric miao42796d32008-04-14 09:35:08 +0100132 .update_status = pwm_backlight_update_status,
133 .get_brightness = pwm_backlight_get_brightness,
Robert Morellef0a5e82011-03-22 16:30:31 -0700134 .check_fb = pwm_backlight_check_fb,
eric miao42796d32008-04-14 09:35:08 +0100135};
136
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100137#ifdef CONFIG_OF
138static int pwm_backlight_parse_dt(struct device *dev,
139 struct platform_pwm_backlight_data *data)
140{
141 struct device_node *node = dev->of_node;
142 struct property *prop;
143 int length;
144 u32 value;
145 int ret;
146
147 if (!node)
148 return -ENODEV;
149
150 memset(data, 0, sizeof(*data));
151
152 /* determine the number of brightness levels */
153 prop = of_find_property(node, "brightness-levels", &length);
154 if (!prop)
155 return -EINVAL;
156
157 data->max_brightness = length / sizeof(u32);
158
159 /* read brightness levels from DT property */
160 if (data->max_brightness > 0) {
161 size_t size = sizeof(*data->levels) * data->max_brightness;
162
163 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
164 if (!data->levels)
165 return -ENOMEM;
166
167 ret = of_property_read_u32_array(node, "brightness-levels",
168 data->levels,
169 data->max_brightness);
170 if (ret < 0)
171 return ret;
172
173 ret = of_property_read_u32(node, "default-brightness-level",
174 &value);
175 if (ret < 0)
176 return ret;
177
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100178 data->dft_brightness = value;
179 data->max_brightness--;
180 }
181
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100182 return 0;
183}
184
185static struct of_device_id pwm_backlight_of_match[] = {
186 { .compatible = "pwm-backlight" },
187 { }
188};
189
190MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
191#else
192static int pwm_backlight_parse_dt(struct device *dev,
193 struct platform_pwm_backlight_data *data)
194{
195 return -ENODEV;
196}
197#endif
198
eric miao42796d32008-04-14 09:35:08 +0100199static int pwm_backlight_probe(struct platform_device *pdev)
200{
Jingoo Hanc512794c2013-11-12 15:09:04 -0800201 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100202 struct platform_pwm_backlight_data defdata;
203 struct backlight_properties props;
eric miao42796d32008-04-14 09:35:08 +0100204 struct backlight_device *bl;
205 struct pwm_bl_data *pb;
Philipp Zabel3b731252008-05-22 14:18:40 +0100206 int ret;
eric miao42796d32008-04-14 09:35:08 +0100207
Ben Dooks14563a42008-08-05 13:01:22 -0700208 if (!data) {
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100209 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
210 if (ret < 0) {
211 dev_err(&pdev->dev, "failed to find platform data\n");
212 return ret;
213 }
214
215 data = &defdata;
Ben Dooks14563a42008-08-05 13:01:22 -0700216 }
eric miao42796d32008-04-14 09:35:08 +0100217
Philipp Zabel3b731252008-05-22 14:18:40 +0100218 if (data->init) {
219 ret = data->init(&pdev->dev);
220 if (ret < 0)
221 return ret;
222 }
223
Julia Lawallce969222012-03-23 15:02:00 -0700224 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
Philipp Zabel3b731252008-05-22 14:18:40 +0100225 if (!pb) {
226 ret = -ENOMEM;
227 goto err_alloc;
228 }
eric miao42796d32008-04-14 09:35:08 +0100229
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100230 if (data->levels) {
Mike Dunn8f43e182013-09-22 09:59:56 -0700231 unsigned int i;
232
233 for (i = 0; i <= data->max_brightness; i++)
234 if (data->levels[i] > pb->scale)
235 pb->scale = data->levels[i];
236
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100237 pb->levels = data->levels;
238 } else
Mike Dunn8f43e182013-09-22 09:59:56 -0700239 pb->scale = data->max_brightness;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100240
Philipp Zabel3b731252008-05-22 14:18:40 +0100241 pb->notify = data->notify;
Dilan Leecc7993f2011-08-25 15:59:17 -0700242 pb->notify_after = data->notify_after;
Robert Morellef0a5e82011-03-22 16:30:31 -0700243 pb->check_fb = data->check_fb;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100244 pb->exit = data->exit;
Ben Dookscfc38992009-11-10 17:20:40 +0000245 pb->dev = &pdev->dev;
Thierry Reding97c38432013-10-02 18:01:02 +0200246 pb->enabled = false;
eric miao42796d32008-04-14 09:35:08 +0100247
Alexandre Courbot257462d2014-02-27 14:53:34 +0900248 pb->enable_gpio = devm_gpiod_get(&pdev->dev, "enable");
249 if (IS_ERR(pb->enable_gpio)) {
250 ret = PTR_ERR(pb->enable_gpio);
251 if (ret == -ENOENT)
252 pb->enable_gpio = NULL;
Thierry Reding8265b2e2013-08-30 12:32:18 +0200253 else
Alexandre Courbot257462d2014-02-27 14:53:34 +0900254 goto err_alloc;
255 }
Thierry Reding8265b2e2013-08-30 12:32:18 +0200256
Alexandre Courbot257462d2014-02-27 14:53:34 +0900257 /*
258 * Compatibility fallback for drivers still using the integer GPIO
259 * platform data. Must go away soon.
260 */
261 if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
262 ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
263 GPIOF_OUT_INIT_HIGH, "enable");
Thierry Reding8265b2e2013-08-30 12:32:18 +0200264 if (ret < 0) {
265 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
Alexandre Courbot257462d2014-02-27 14:53:34 +0900266 data->enable_gpio, ret);
Thierry Reding8265b2e2013-08-30 12:32:18 +0200267 goto err_alloc;
268 }
Alexandre Courbot257462d2014-02-27 14:53:34 +0900269
270 pb->enable_gpio = gpio_to_desc(data->enable_gpio);
Thierry Reding8265b2e2013-08-30 12:32:18 +0200271 }
272
Alexandre Courbot257462d2014-02-27 14:53:34 +0900273 if (pb->enable_gpio)
274 gpiod_direction_output(pb->enable_gpio, 1);
275
Thierry Reding22ceeee2013-08-30 12:38:34 +0200276 pb->power_supply = devm_regulator_get(&pdev->dev, "power");
277 if (IS_ERR(pb->power_supply)) {
278 ret = PTR_ERR(pb->power_supply);
Alexandre Courbot257462d2014-02-27 14:53:34 +0900279 goto err_alloc;
Thierry Reding22ceeee2013-08-30 12:38:34 +0200280 }
eric miao42796d32008-04-14 09:35:08 +0100281
Sachin Kamat60ce7022012-09-17 11:50:47 +0530282 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
Ben Dooks43bda1a2008-07-01 14:18:27 +0100283 if (IS_ERR(pb->pwm)) {
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100284 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
285
286 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
287 if (IS_ERR(pb->pwm)) {
288 dev_err(&pdev->dev, "unable to request legacy PWM\n");
289 ret = PTR_ERR(pb->pwm);
Alexandre Courbot257462d2014-02-27 14:53:34 +0900290 goto err_alloc;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100291 }
292 }
293
294 dev_dbg(&pdev->dev, "got pwm for backlight\n");
295
296 /*
297 * The DT case will set the pwm_period_ns field to 0 and store the
298 * period, parsed from the DT, in the PWM device. For the non-DT case,
Alexandre Belloni16fc3352014-05-19 22:42:42 +0200299 * set the period from platform data if it has not already been set
300 * via the PWM lookup table.
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100301 */
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100302 pb->period = pwm_get_period(pb->pwm);
Alexandre Belloni16fc3352014-05-19 22:42:42 +0200303 if (!pb->period && (data->pwm_period_ns > 0)) {
304 pb->period = data->pwm_period_ns;
305 pwm_set_period(pb->pwm, data->pwm_period_ns);
306 }
307
Mike Dunn8f43e182013-09-22 09:59:56 -0700308 pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
eric miao42796d32008-04-14 09:35:08 +0100309
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500310 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700311 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500312 props.max_brightness = data->max_brightness;
313 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
314 &pwm_backlight_ops, &props);
eric miao42796d32008-04-14 09:35:08 +0100315 if (IS_ERR(bl)) {
316 dev_err(&pdev->dev, "failed to register backlight\n");
Philipp Zabel3b731252008-05-22 14:18:40 +0100317 ret = PTR_ERR(bl);
Alexandre Courbot257462d2014-02-27 14:53:34 +0900318 goto err_alloc;
eric miao42796d32008-04-14 09:35:08 +0100319 }
320
Peter Ujfalusi83cfd722013-01-22 14:39:54 +0100321 if (data->dft_brightness > data->max_brightness) {
322 dev_warn(&pdev->dev,
323 "invalid default brightness level: %u, using %u\n",
324 data->dft_brightness, data->max_brightness);
325 data->dft_brightness = data->max_brightness;
326 }
327
eric miao42796d32008-04-14 09:35:08 +0100328 bl->props.brightness = data->dft_brightness;
329 backlight_update_status(bl);
330
331 platform_set_drvdata(pdev, bl);
332 return 0;
Philipp Zabel3b731252008-05-22 14:18:40 +0100333
Philipp Zabel3b731252008-05-22 14:18:40 +0100334err_alloc:
335 if (data->exit)
336 data->exit(&pdev->dev);
337 return ret;
eric miao42796d32008-04-14 09:35:08 +0100338}
339
340static int pwm_backlight_remove(struct platform_device *pdev)
341{
342 struct backlight_device *bl = platform_get_drvdata(pdev);
Jingoo Hane6e3dbf2013-02-21 16:43:46 -0800343 struct pwm_bl_data *pb = bl_get_data(bl);
eric miao42796d32008-04-14 09:35:08 +0100344
345 backlight_device_unregister(bl);
Thierry Reding62b744a2013-10-07 11:32:02 +0200346 pwm_backlight_power_off(pb);
Thierry Reding668e63c2013-10-07 11:30:50 +0200347
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100348 if (pb->exit)
349 pb->exit(&pdev->dev);
Thierry Reding668e63c2013-10-07 11:30:50 +0200350
eric miao42796d32008-04-14 09:35:08 +0100351 return 0;
352}
353
Thierry Reding5f33b892014-04-29 17:28:59 +0200354static void pwm_backlight_shutdown(struct platform_device *pdev)
355{
356 struct backlight_device *bl = platform_get_drvdata(pdev);
357 struct pwm_bl_data *pb = bl_get_data(bl);
358
359 pwm_backlight_power_off(pb);
360}
361
Jingoo Hanc7911262013-02-25 17:15:47 +0900362#ifdef CONFIG_PM_SLEEP
Mark Browne2c17bc2012-01-10 15:09:22 -0800363static int pwm_backlight_suspend(struct device *dev)
eric miao42796d32008-04-14 09:35:08 +0100364{
Mark Browne2c17bc2012-01-10 15:09:22 -0800365 struct backlight_device *bl = dev_get_drvdata(dev);
Jingoo Hane6e3dbf2013-02-21 16:43:46 -0800366 struct pwm_bl_data *pb = bl_get_data(bl);
eric miao42796d32008-04-14 09:35:08 +0100367
Marc Zyngier82e8b542009-06-19 10:50:54 +0200368 if (pb->notify)
Ben Dookscfc38992009-11-10 17:20:40 +0000369 pb->notify(pb->dev, 0);
Thierry Reding668e63c2013-10-07 11:30:50 +0200370
Thierry Reding62b744a2013-10-07 11:32:02 +0200371 pwm_backlight_power_off(pb);
Thierry Reding668e63c2013-10-07 11:30:50 +0200372
Dilan Leecc7993f2011-08-25 15:59:17 -0700373 if (pb->notify_after)
374 pb->notify_after(pb->dev, 0);
Thierry Reding668e63c2013-10-07 11:30:50 +0200375
eric miao42796d32008-04-14 09:35:08 +0100376 return 0;
377}
378
Mark Browne2c17bc2012-01-10 15:09:22 -0800379static int pwm_backlight_resume(struct device *dev)
eric miao42796d32008-04-14 09:35:08 +0100380{
Mark Browne2c17bc2012-01-10 15:09:22 -0800381 struct backlight_device *bl = dev_get_drvdata(dev);
eric miao42796d32008-04-14 09:35:08 +0100382
383 backlight_update_status(bl);
Thierry Reding668e63c2013-10-07 11:30:50 +0200384
eric miao42796d32008-04-14 09:35:08 +0100385 return 0;
386}
Jingoo Hanc7911262013-02-25 17:15:47 +0900387#endif
Mark Browne2c17bc2012-01-10 15:09:22 -0800388
Huayi Li1dea1fd2013-10-09 10:33:02 +0800389static const struct dev_pm_ops pwm_backlight_pm_ops = {
390#ifdef CONFIG_PM_SLEEP
391 .suspend = pwm_backlight_suspend,
392 .resume = pwm_backlight_resume,
393 .poweroff = pwm_backlight_suspend,
394 .restore = pwm_backlight_resume,
395#endif
396};
Mark Browne2c17bc2012-01-10 15:09:22 -0800397
eric miao42796d32008-04-14 09:35:08 +0100398static struct platform_driver pwm_backlight_driver = {
399 .driver = {
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100400 .name = "pwm-backlight",
401 .owner = THIS_MODULE,
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100402 .pm = &pwm_backlight_pm_ops,
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100403 .of_match_table = of_match_ptr(pwm_backlight_of_match),
eric miao42796d32008-04-14 09:35:08 +0100404 },
405 .probe = pwm_backlight_probe,
406 .remove = pwm_backlight_remove,
Thierry Reding5f33b892014-04-29 17:28:59 +0200407 .shutdown = pwm_backlight_shutdown,
eric miao42796d32008-04-14 09:35:08 +0100408};
409
Axel Lin81178e02012-01-10 15:09:11 -0800410module_platform_driver(pwm_backlight_driver);
eric miao42796d32008-04-14 09:35:08 +0100411
412MODULE_DESCRIPTION("PWM based Backlight Driver");
413MODULE_LICENSE("GPL");
Ben Dooks8cd68192008-08-05 13:01:24 -0700414MODULE_ALIAS("platform:pwm-backlight");