blob: 1c2289ddd555a64a418f29edeaf8e4b5643430b9 [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;
Vladimir Zapolskiyedf387b2014-10-11 16:46:26 +030037 bool legacy;
Ben Dookscfc38992009-11-10 17:20:40 +000038 int (*notify)(struct device *,
39 int brightness);
Dilan Leecc7993f2011-08-25 15:59:17 -070040 void (*notify_after)(struct device *,
41 int brightness);
Robert Morellef0a5e82011-03-22 16:30:31 -070042 int (*check_fb)(struct device *, struct fb_info *);
Thierry Reding3e3ed6c2011-12-16 21:25:29 +010043 void (*exit)(struct device *);
eric miao42796d32008-04-14 09:35:08 +010044};
45
Mike Dunn8f43e182013-09-22 09:59:56 -070046static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
Thierry Reding62b744a2013-10-07 11:32:02 +020047{
Thierry Reding73d4e2b2013-10-22 09:37:05 +020048 int err;
Thierry Reding62b744a2013-10-07 11:32:02 +020049
Thierry Reding97c38432013-10-02 18:01:02 +020050 if (pb->enabled)
51 return;
52
Thierry Reding22ceeee2013-08-30 12:38:34 +020053 err = regulator_enable(pb->power_supply);
54 if (err < 0)
55 dev_err(pb->dev, "failed to enable power supply\n");
56
Alexandre Courbot257462d2014-02-27 14:53:34 +090057 if (pb->enable_gpio)
Maxime Ripard0c9501f2016-08-31 10:18:12 +020058 gpiod_set_value_cansleep(pb->enable_gpio, 1);
Thierry Reding8265b2e2013-08-30 12:32:18 +020059
Thierry Reding62b744a2013-10-07 11:32:02 +020060 pwm_enable(pb->pwm);
Thierry Reding97c38432013-10-02 18:01:02 +020061 pb->enabled = true;
Thierry Reding62b744a2013-10-07 11:32:02 +020062}
63
64static void pwm_backlight_power_off(struct pwm_bl_data *pb)
65{
Thierry Reding97c38432013-10-02 18:01:02 +020066 if (!pb->enabled)
67 return;
68
Thierry Reding62b744a2013-10-07 11:32:02 +020069 pwm_config(pb->pwm, 0, pb->period);
70 pwm_disable(pb->pwm);
Thierry Reding97c38432013-10-02 18:01:02 +020071
Alexandre Courbot257462d2014-02-27 14:53:34 +090072 if (pb->enable_gpio)
Maxime Ripard0c9501f2016-08-31 10:18:12 +020073 gpiod_set_value_cansleep(pb->enable_gpio, 0);
Thierry Reding8265b2e2013-08-30 12:32:18 +020074
Thierry Reding22ceeee2013-08-30 12:38:34 +020075 regulator_disable(pb->power_supply);
Thierry Reding97c38432013-10-02 18:01:02 +020076 pb->enabled = false;
Thierry Reding62b744a2013-10-07 11:32:02 +020077}
78
Thierry Redinge4bfeda2013-10-18 10:46:24 +020079static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
80{
81 unsigned int lth = pb->lth_brightness;
Derek Basehore5d0c49a2017-08-29 13:34:34 -070082 u64 duty_cycle;
Thierry Redinge4bfeda2013-10-18 10:46:24 +020083
84 if (pb->levels)
85 duty_cycle = pb->levels[brightness];
86 else
87 duty_cycle = brightness;
88
Derek Basehore5d0c49a2017-08-29 13:34:34 -070089 duty_cycle *= pb->period - lth;
90 do_div(duty_cycle, pb->scale);
91
92 return duty_cycle + lth;
Thierry Redinge4bfeda2013-10-18 10:46:24 +020093}
94
eric miao42796d32008-04-14 09:35:08 +010095static int pwm_backlight_update_status(struct backlight_device *bl)
96{
Jingoo Hane6e3dbf2013-02-21 16:43:46 -080097 struct pwm_bl_data *pb = bl_get_data(bl);
eric miao42796d32008-04-14 09:35:08 +010098 int brightness = bl->props.brightness;
Thierry Redinge4bfeda2013-10-18 10:46:24 +020099 int duty_cycle;
eric miao42796d32008-04-14 09:35:08 +0100100
Alexandre Courbot01322672013-01-30 15:19:15 +0900101 if (bl->props.power != FB_BLANK_UNBLANK ||
102 bl->props.fb_blank != FB_BLANK_UNBLANK ||
103 bl->props.state & BL_CORE_FBBLANK)
eric miao42796d32008-04-14 09:35:08 +0100104 brightness = 0;
105
Philipp Zabel3b731252008-05-22 14:18:40 +0100106 if (pb->notify)
Ben Dookscfc38992009-11-10 17:20:40 +0000107 brightness = pb->notify(pb->dev, brightness);
Philipp Zabel3b731252008-05-22 14:18:40 +0100108
Thierry Redinge4bfeda2013-10-18 10:46:24 +0200109 if (brightness > 0) {
110 duty_cycle = compute_duty_cycle(pb, brightness);
Alexandre Courbot9fb978b2012-07-09 15:04:23 +0900111 pwm_config(pb->pwm, duty_cycle, pb->period);
Mike Dunn8f43e182013-09-22 09:59:56 -0700112 pwm_backlight_power_on(pb, brightness);
Thierry Redinge4bfeda2013-10-18 10:46:24 +0200113 } else
Thierry Reding62b744a2013-10-07 11:32:02 +0200114 pwm_backlight_power_off(pb);
Dilan Leecc7993f2011-08-25 15:59:17 -0700115
116 if (pb->notify_after)
117 pb->notify_after(pb->dev, brightness);
118
eric miao42796d32008-04-14 09:35:08 +0100119 return 0;
120}
121
Robert Morellef0a5e82011-03-22 16:30:31 -0700122static int pwm_backlight_check_fb(struct backlight_device *bl,
123 struct fb_info *info)
124{
Jingoo Hane6e3dbf2013-02-21 16:43:46 -0800125 struct pwm_bl_data *pb = bl_get_data(bl);
Robert Morellef0a5e82011-03-22 16:30:31 -0700126
127 return !pb->check_fb || pb->check_fb(pb->dev, info);
128}
129
Emese Revfy9905a432009-12-14 00:58:57 +0100130static const struct backlight_ops pwm_backlight_ops = {
eric miao42796d32008-04-14 09:35:08 +0100131 .update_status = pwm_backlight_update_status,
Robert Morellef0a5e82011-03-22 16:30:31 -0700132 .check_fb = pwm_backlight_check_fb,
eric miao42796d32008-04-14 09:35:08 +0100133};
134
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100135#ifdef CONFIG_OF
136static int pwm_backlight_parse_dt(struct device *dev,
137 struct platform_pwm_backlight_data *data)
138{
139 struct device_node *node = dev->of_node;
140 struct property *prop;
141 int length;
142 u32 value;
143 int ret;
144
145 if (!node)
146 return -ENODEV;
147
148 memset(data, 0, sizeof(*data));
149
150 /* determine the number of brightness levels */
151 prop = of_find_property(node, "brightness-levels", &length);
152 if (!prop)
153 return -EINVAL;
154
155 data->max_brightness = length / sizeof(u32);
156
157 /* read brightness levels from DT property */
158 if (data->max_brightness > 0) {
159 size_t size = sizeof(*data->levels) * data->max_brightness;
160
161 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
162 if (!data->levels)
163 return -ENOMEM;
164
165 ret = of_property_read_u32_array(node, "brightness-levels",
166 data->levels,
167 data->max_brightness);
168 if (ret < 0)
169 return ret;
170
171 ret = of_property_read_u32(node, "default-brightness-level",
172 &value);
173 if (ret < 0)
174 return ret;
175
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100176 data->dft_brightness = value;
177 data->max_brightness--;
178 }
179
Lothar Waßmann937222c2014-08-20 08:38:36 +0200180 data->enable_gpio = -EINVAL;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100181 return 0;
182}
183
Arvind Yadav62cdfe62017-06-20 13:22:15 +0530184static const struct of_device_id pwm_backlight_of_match[] = {
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100185 { .compatible = "pwm-backlight" },
186 { }
187};
188
189MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
190#else
191static int pwm_backlight_parse_dt(struct device *dev,
192 struct platform_pwm_backlight_data *data)
193{
194 return -ENODEV;
195}
196#endif
197
Peter Ujfalusi7613c922016-11-22 15:41:22 +0200198static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
199{
200 struct device_node *node = pb->dev->of_node;
201
202 /* Not booted with device tree or no phandle link to the node */
203 if (!node || !node->phandle)
204 return FB_BLANK_UNBLANK;
205
206 /*
207 * If the driver is probed from the device tree and there is a
208 * phandle link pointing to the backlight node, it is safe to
209 * assume that another driver will enable the backlight at the
210 * appropriate time. Therefore, if it is disabled, keep it so.
211 */
212
213 /* if the enable GPIO is disabled, do not enable the backlight */
214 if (pb->enable_gpio && gpiod_get_value(pb->enable_gpio) == 0)
215 return FB_BLANK_POWERDOWN;
216
217 /* The regulator is disabled, do not enable the backlight */
218 if (!regulator_is_enabled(pb->power_supply))
219 return FB_BLANK_POWERDOWN;
220
Peter Ujfalusid1b81292016-11-22 15:41:23 +0200221 /* The PWM is disabled, keep it like this */
222 if (!pwm_is_enabled(pb->pwm))
223 return FB_BLANK_POWERDOWN;
224
Peter Ujfalusi7613c922016-11-22 15:41:22 +0200225 return FB_BLANK_UNBLANK;
226}
227
eric miao42796d32008-04-14 09:35:08 +0100228static int pwm_backlight_probe(struct platform_device *pdev)
229{
Jingoo Hanc512794c2013-11-12 15:09:04 -0800230 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100231 struct platform_pwm_backlight_data defdata;
232 struct backlight_properties props;
eric miao42796d32008-04-14 09:35:08 +0100233 struct backlight_device *bl;
Philipp Zabel87770782015-12-10 10:09:06 +0100234 struct device_node *node = pdev->dev.of_node;
eric miao42796d32008-04-14 09:35:08 +0100235 struct pwm_bl_data *pb;
Boris Brezillon6cb96442016-04-14 21:17:29 +0200236 struct pwm_args pargs;
Philipp Zabel3b731252008-05-22 14:18:40 +0100237 int ret;
eric miao42796d32008-04-14 09:35:08 +0100238
Ben Dooks14563a42008-08-05 13:01:22 -0700239 if (!data) {
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100240 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
241 if (ret < 0) {
242 dev_err(&pdev->dev, "failed to find platform data\n");
243 return ret;
244 }
245
246 data = &defdata;
Ben Dooks14563a42008-08-05 13:01:22 -0700247 }
eric miao42796d32008-04-14 09:35:08 +0100248
Philipp Zabel3b731252008-05-22 14:18:40 +0100249 if (data->init) {
250 ret = data->init(&pdev->dev);
251 if (ret < 0)
252 return ret;
253 }
254
Julia Lawallce969222012-03-23 15:02:00 -0700255 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
Philipp Zabel3b731252008-05-22 14:18:40 +0100256 if (!pb) {
257 ret = -ENOMEM;
258 goto err_alloc;
259 }
eric miao42796d32008-04-14 09:35:08 +0100260
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100261 if (data->levels) {
Mike Dunn8f43e182013-09-22 09:59:56 -0700262 unsigned int i;
263
264 for (i = 0; i <= data->max_brightness; i++)
265 if (data->levels[i] > pb->scale)
266 pb->scale = data->levels[i];
267
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100268 pb->levels = data->levels;
269 } else
Mike Dunn8f43e182013-09-22 09:59:56 -0700270 pb->scale = data->max_brightness;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100271
Philipp Zabel3b731252008-05-22 14:18:40 +0100272 pb->notify = data->notify;
Dilan Leecc7993f2011-08-25 15:59:17 -0700273 pb->notify_after = data->notify_after;
Robert Morellef0a5e82011-03-22 16:30:31 -0700274 pb->check_fb = data->check_fb;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100275 pb->exit = data->exit;
Ben Dookscfc38992009-11-10 17:20:40 +0000276 pb->dev = &pdev->dev;
Thierry Reding97c38432013-10-02 18:01:02 +0200277 pb->enabled = false;
eric miao42796d32008-04-14 09:35:08 +0100278
Axel Lincdaefcc2015-05-16 22:08:10 +0800279 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
Philipp Zabel3698d7e2015-11-18 18:12:25 +0100280 GPIOD_ASIS);
Alexandre Courbot257462d2014-02-27 14:53:34 +0900281 if (IS_ERR(pb->enable_gpio)) {
282 ret = PTR_ERR(pb->enable_gpio);
Alexandre Courbotff9c5422014-06-25 18:18:18 +0900283 goto err_alloc;
Alexandre Courbot257462d2014-02-27 14:53:34 +0900284 }
Thierry Reding8265b2e2013-08-30 12:32:18 +0200285
Alexandre Courbot257462d2014-02-27 14:53:34 +0900286 /*
287 * Compatibility fallback for drivers still using the integer GPIO
288 * platform data. Must go away soon.
289 */
290 if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
291 ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
292 GPIOF_OUT_INIT_HIGH, "enable");
Thierry Reding8265b2e2013-08-30 12:32:18 +0200293 if (ret < 0) {
294 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
Alexandre Courbot257462d2014-02-27 14:53:34 +0900295 data->enable_gpio, ret);
Thierry Reding8265b2e2013-08-30 12:32:18 +0200296 goto err_alloc;
297 }
Alexandre Courbot257462d2014-02-27 14:53:34 +0900298
299 pb->enable_gpio = gpio_to_desc(data->enable_gpio);
Thierry Reding8265b2e2013-08-30 12:32:18 +0200300 }
301
Peter Ujfalusi7613c922016-11-22 15:41:22 +0200302 /*
Geert Uytterhoeven892c7782017-04-04 12:54:35 +0200303 * If the GPIO is not known to be already configured as output, that
304 * is, if gpiod_get_direction returns either GPIOF_DIR_IN or -EINVAL,
305 * change the direction to output and set the GPIO as active.
Peter Ujfalusi7613c922016-11-22 15:41:22 +0200306 * Do not force the GPIO to active when it was already output as it
307 * could cause backlight flickering or we would enable the backlight too
308 * early. Leave the decision of the initial backlight state for later.
309 */
310 if (pb->enable_gpio &&
Geert Uytterhoeven892c7782017-04-04 12:54:35 +0200311 gpiod_get_direction(pb->enable_gpio) != GPIOF_DIR_OUT)
Peter Ujfalusi7613c922016-11-22 15:41:22 +0200312 gpiod_direction_output(pb->enable_gpio, 1);
Philipp Zabel3698d7e2015-11-18 18:12:25 +0100313
Thierry Reding22ceeee2013-08-30 12:38:34 +0200314 pb->power_supply = devm_regulator_get(&pdev->dev, "power");
315 if (IS_ERR(pb->power_supply)) {
316 ret = PTR_ERR(pb->power_supply);
Alexandre Courbot257462d2014-02-27 14:53:34 +0900317 goto err_alloc;
Thierry Reding22ceeee2013-08-30 12:38:34 +0200318 }
eric miao42796d32008-04-14 09:35:08 +0100319
Sachin Kamat60ce7022012-09-17 11:50:47 +0530320 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
Philipp Zabel87770782015-12-10 10:09:06 +0100321 if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100322 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
Vladimir Zapolskiyedf387b2014-10-11 16:46:26 +0300323 pb->legacy = true;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100324 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
Vladimir Zapolskiydc881122015-10-12 15:29:03 +0300325 }
326
327 if (IS_ERR(pb->pwm)) {
328 ret = PTR_ERR(pb->pwm);
329 if (ret != -EPROBE_DEFER)
330 dev_err(&pdev->dev, "unable to request PWM\n");
331 goto err_alloc;
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100332 }
333
334 dev_dbg(&pdev->dev, "got pwm for backlight\n");
335
336 /*
Boris Brezillon6cb96442016-04-14 21:17:29 +0200337 * FIXME: pwm_apply_args() should be removed when switching to
338 * the atomic PWM API.
339 */
340 pwm_apply_args(pb->pwm);
341
342 /*
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100343 * The DT case will set the pwm_period_ns field to 0 and store the
344 * period, parsed from the DT, in the PWM device. For the non-DT case,
Alexandre Belloni16fc3352014-05-19 22:42:42 +0200345 * set the period from platform data if it has not already been set
346 * via the PWM lookup table.
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100347 */
Boris Brezillon6cb96442016-04-14 21:17:29 +0200348 pwm_get_args(pb->pwm, &pargs);
349 pb->period = pargs.period;
Boris BREZILLON7f044b02016-03-30 22:03:25 +0200350 if (!pb->period && (data->pwm_period_ns > 0))
Alexandre Belloni16fc3352014-05-19 22:42:42 +0200351 pb->period = data->pwm_period_ns;
Alexandre Belloni16fc3352014-05-19 22:42:42 +0200352
Mike Dunn8f43e182013-09-22 09:59:56 -0700353 pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
eric miao42796d32008-04-14 09:35:08 +0100354
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500355 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700356 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500357 props.max_brightness = data->max_brightness;
358 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
359 &pwm_backlight_ops, &props);
eric miao42796d32008-04-14 09:35:08 +0100360 if (IS_ERR(bl)) {
361 dev_err(&pdev->dev, "failed to register backlight\n");
Philipp Zabel3b731252008-05-22 14:18:40 +0100362 ret = PTR_ERR(bl);
Vladimir Zapolskiy60d613d2015-06-14 17:32:14 +0300363 if (pb->legacy)
364 pwm_free(pb->pwm);
Alexandre Courbot257462d2014-02-27 14:53:34 +0900365 goto err_alloc;
eric miao42796d32008-04-14 09:35:08 +0100366 }
367
Peter Ujfalusi83cfd722013-01-22 14:39:54 +0100368 if (data->dft_brightness > data->max_brightness) {
369 dev_warn(&pdev->dev,
370 "invalid default brightness level: %u, using %u\n",
371 data->dft_brightness, data->max_brightness);
372 data->dft_brightness = data->max_brightness;
373 }
374
eric miao42796d32008-04-14 09:35:08 +0100375 bl->props.brightness = data->dft_brightness;
Peter Ujfalusi7613c922016-11-22 15:41:22 +0200376 bl->props.power = pwm_backlight_initial_power_state(pb);
eric miao42796d32008-04-14 09:35:08 +0100377 backlight_update_status(bl);
378
379 platform_set_drvdata(pdev, bl);
380 return 0;
Philipp Zabel3b731252008-05-22 14:18:40 +0100381
Philipp Zabel3b731252008-05-22 14:18:40 +0100382err_alloc:
383 if (data->exit)
384 data->exit(&pdev->dev);
385 return ret;
eric miao42796d32008-04-14 09:35:08 +0100386}
387
388static int pwm_backlight_remove(struct platform_device *pdev)
389{
390 struct backlight_device *bl = platform_get_drvdata(pdev);
Jingoo Hane6e3dbf2013-02-21 16:43:46 -0800391 struct pwm_bl_data *pb = bl_get_data(bl);
eric miao42796d32008-04-14 09:35:08 +0100392
393 backlight_device_unregister(bl);
Thierry Reding62b744a2013-10-07 11:32:02 +0200394 pwm_backlight_power_off(pb);
Thierry Reding668e63c2013-10-07 11:30:50 +0200395
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100396 if (pb->exit)
397 pb->exit(&pdev->dev);
Vladimir Zapolskiyedf387b2014-10-11 16:46:26 +0300398 if (pb->legacy)
399 pwm_free(pb->pwm);
Thierry Reding668e63c2013-10-07 11:30:50 +0200400
eric miao42796d32008-04-14 09:35:08 +0100401 return 0;
402}
403
Thierry Reding5f33b892014-04-29 17:28:59 +0200404static void pwm_backlight_shutdown(struct platform_device *pdev)
405{
406 struct backlight_device *bl = platform_get_drvdata(pdev);
407 struct pwm_bl_data *pb = bl_get_data(bl);
408
409 pwm_backlight_power_off(pb);
410}
411
Jingoo Hanc7911262013-02-25 17:15:47 +0900412#ifdef CONFIG_PM_SLEEP
Mark Browne2c17bc2012-01-10 15:09:22 -0800413static int pwm_backlight_suspend(struct device *dev)
eric miao42796d32008-04-14 09:35:08 +0100414{
Mark Browne2c17bc2012-01-10 15:09:22 -0800415 struct backlight_device *bl = dev_get_drvdata(dev);
Jingoo Hane6e3dbf2013-02-21 16:43:46 -0800416 struct pwm_bl_data *pb = bl_get_data(bl);
eric miao42796d32008-04-14 09:35:08 +0100417
Marc Zyngier82e8b542009-06-19 10:50:54 +0200418 if (pb->notify)
Ben Dookscfc38992009-11-10 17:20:40 +0000419 pb->notify(pb->dev, 0);
Thierry Reding668e63c2013-10-07 11:30:50 +0200420
Thierry Reding62b744a2013-10-07 11:32:02 +0200421 pwm_backlight_power_off(pb);
Thierry Reding668e63c2013-10-07 11:30:50 +0200422
Dilan Leecc7993f2011-08-25 15:59:17 -0700423 if (pb->notify_after)
424 pb->notify_after(pb->dev, 0);
Thierry Reding668e63c2013-10-07 11:30:50 +0200425
eric miao42796d32008-04-14 09:35:08 +0100426 return 0;
427}
428
Mark Browne2c17bc2012-01-10 15:09:22 -0800429static int pwm_backlight_resume(struct device *dev)
eric miao42796d32008-04-14 09:35:08 +0100430{
Mark Browne2c17bc2012-01-10 15:09:22 -0800431 struct backlight_device *bl = dev_get_drvdata(dev);
eric miao42796d32008-04-14 09:35:08 +0100432
433 backlight_update_status(bl);
Thierry Reding668e63c2013-10-07 11:30:50 +0200434
eric miao42796d32008-04-14 09:35:08 +0100435 return 0;
436}
Jingoo Hanc7911262013-02-25 17:15:47 +0900437#endif
Mark Browne2c17bc2012-01-10 15:09:22 -0800438
Huayi Li1dea1fd2013-10-09 10:33:02 +0800439static const struct dev_pm_ops pwm_backlight_pm_ops = {
440#ifdef CONFIG_PM_SLEEP
441 .suspend = pwm_backlight_suspend,
442 .resume = pwm_backlight_resume,
443 .poweroff = pwm_backlight_suspend,
444 .restore = pwm_backlight_resume,
445#endif
446};
Mark Browne2c17bc2012-01-10 15:09:22 -0800447
eric miao42796d32008-04-14 09:35:08 +0100448static struct platform_driver pwm_backlight_driver = {
449 .driver = {
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100450 .name = "pwm-backlight",
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100451 .pm = &pwm_backlight_pm_ops,
Thierry Reding3e3ed6c2011-12-16 21:25:29 +0100452 .of_match_table = of_match_ptr(pwm_backlight_of_match),
eric miao42796d32008-04-14 09:35:08 +0100453 },
454 .probe = pwm_backlight_probe,
455 .remove = pwm_backlight_remove,
Thierry Reding5f33b892014-04-29 17:28:59 +0200456 .shutdown = pwm_backlight_shutdown,
eric miao42796d32008-04-14 09:35:08 +0100457};
458
Axel Lin81178e02012-01-10 15:09:11 -0800459module_platform_driver(pwm_backlight_driver);
eric miao42796d32008-04-14 09:35:08 +0100460
461MODULE_DESCRIPTION("PWM based Backlight Driver");
462MODULE_LICENSE("GPL");
Ben Dooks8cd68192008-08-05 13:01:24 -0700463MODULE_ALIAS("platform:pwm-backlight");