1 /*
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
6 * 2) platform_data being correctly configured
7 *
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/gpio/consumer.h>
14 #include <linux/gpio.h>
15 #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>
24 #include <linux/regulator/consumer.h>
25 #include <linux/slab.h>
26
27 struct pwm_bl_data {
28 struct pwm_device *pwm;
29 struct device *dev;
30 unsigned int period;
31 unsigned int lth_brightness;
32 unsigned int *levels;
33 bool enabled;
34 struct regulator *power_supply;
35 struct gpio_desc *enable_gpio;
36 unsigned int scale;
37 int (*notify)(struct device *,
38 int brightness);
39 void (*notify_after)(struct device *,
40 int brightness);
41 int (*check_fb)(struct device *, struct fb_info *);
42 void (*exit)(struct device *);
43 };
44
pwm_backlight_power_on(struct pwm_bl_data * pb,int brightness)45 static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
46 {
47 int err;
48
49 if (pb->enabled)
50 return;
51
52 err = regulator_enable(pb->power_supply);
53 if (err < 0)
54 dev_err(pb->dev, "failed to enable power supply\n");
55
56 if (pb->enable_gpio)
57 gpiod_set_value(pb->enable_gpio, 1);
58
59 pwm_enable(pb->pwm);
60 pb->enabled = true;
61 }
62
pwm_backlight_power_off(struct pwm_bl_data * pb)63 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
64 {
65 if (!pb->enabled)
66 return;
67
68 pwm_config(pb->pwm, 0, pb->period);
69 pwm_disable(pb->pwm);
70
71 if (pb->enable_gpio)
72 gpiod_set_value(pb->enable_gpio, 0);
73
74 regulator_disable(pb->power_supply);
75 pb->enabled = false;
76 }
77
compute_duty_cycle(struct pwm_bl_data * pb,int brightness)78 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
79 {
80 unsigned int lth = pb->lth_brightness;
81 u64 duty_cycle;
82
83 if (pb->levels)
84 duty_cycle = pb->levels[brightness];
85 else
86 duty_cycle = brightness;
87
88 duty_cycle *= pb->period - lth;
89 do_div(duty_cycle, pb->scale);
90
91 return duty_cycle + lth;
92 }
93
pwm_backlight_update_status(struct backlight_device * bl)94 static int pwm_backlight_update_status(struct backlight_device *bl)
95 {
96 struct pwm_bl_data *pb = bl_get_data(bl);
97 int brightness = bl->props.brightness;
98 int duty_cycle;
99
100 if (bl->props.power != FB_BLANK_UNBLANK ||
101 bl->props.fb_blank != FB_BLANK_UNBLANK ||
102 bl->props.state & BL_CORE_FBBLANK)
103 brightness = 0;
104
105 if (pb->notify)
106 brightness = pb->notify(pb->dev, brightness);
107
108 if (brightness > 0) {
109 duty_cycle = compute_duty_cycle(pb, brightness);
110 pwm_config(pb->pwm, duty_cycle, pb->period);
111 pwm_backlight_power_on(pb, brightness);
112 } else
113 pwm_backlight_power_off(pb);
114
115 if (pb->notify_after)
116 pb->notify_after(pb->dev, brightness);
117
118 return 0;
119 }
120
pwm_backlight_check_fb(struct backlight_device * bl,struct fb_info * info)121 static int pwm_backlight_check_fb(struct backlight_device *bl,
122 struct fb_info *info)
123 {
124 struct pwm_bl_data *pb = bl_get_data(bl);
125
126 return !pb->check_fb || pb->check_fb(pb->dev, info);
127 }
128
129 static const struct backlight_ops pwm_backlight_ops = {
130 .update_status = pwm_backlight_update_status,
131 .check_fb = pwm_backlight_check_fb,
132 };
133
134 #ifdef CONFIG_OF
pwm_backlight_parse_dt(struct device * dev,struct platform_pwm_backlight_data * data)135 static int pwm_backlight_parse_dt(struct device *dev,
136 struct platform_pwm_backlight_data *data)
137 {
138 struct device_node *node = dev->of_node;
139 struct property *prop;
140 int length;
141 u32 value;
142 int ret;
143
144 if (!node)
145 return -ENODEV;
146
147 memset(data, 0, sizeof(*data));
148
149 /* determine the number of brightness levels */
150 prop = of_find_property(node, "brightness-levels", &length);
151 if (!prop)
152 return -EINVAL;
153
154 data->max_brightness = length / sizeof(u32);
155
156 /* read brightness levels from DT property */
157 if (data->max_brightness > 0) {
158 size_t size = sizeof(*data->levels) * data->max_brightness;
159
160 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
161 if (!data->levels)
162 return -ENOMEM;
163
164 ret = of_property_read_u32_array(node, "brightness-levels",
165 data->levels,
166 data->max_brightness);
167 if (ret < 0)
168 return ret;
169
170 ret = of_property_read_u32(node, "default-brightness-level",
171 &value);
172 if (ret < 0)
173 return ret;
174
175 data->dft_brightness = value;
176 data->max_brightness--;
177 }
178
179 data->enable_gpio = -EINVAL;
180 return 0;
181 }
182
183 static struct of_device_id pwm_backlight_of_match[] = {
184 { .compatible = "pwm-backlight" },
185 { }
186 };
187
188 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
189 #else
pwm_backlight_parse_dt(struct device * dev,struct platform_pwm_backlight_data * data)190 static int pwm_backlight_parse_dt(struct device *dev,
191 struct platform_pwm_backlight_data *data)
192 {
193 return -ENODEV;
194 }
195 #endif
196
pwm_backlight_probe(struct platform_device * pdev)197 static int pwm_backlight_probe(struct platform_device *pdev)
198 {
199 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
200 struct platform_pwm_backlight_data defdata;
201 struct backlight_properties props;
202 struct backlight_device *bl;
203 struct pwm_bl_data *pb;
204 int ret;
205
206 if (!data) {
207 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
208 if (ret < 0) {
209 dev_err(&pdev->dev, "failed to find platform data\n");
210 return ret;
211 }
212
213 data = &defdata;
214 }
215
216 if (data->init) {
217 ret = data->init(&pdev->dev);
218 if (ret < 0)
219 return ret;
220 }
221
222 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
223 if (!pb) {
224 ret = -ENOMEM;
225 goto err_alloc;
226 }
227
228 if (data->levels) {
229 unsigned int i;
230
231 for (i = 0; i <= data->max_brightness; i++)
232 if (data->levels[i] > pb->scale)
233 pb->scale = data->levels[i];
234
235 pb->levels = data->levels;
236 } else
237 pb->scale = data->max_brightness;
238
239 pb->notify = data->notify;
240 pb->notify_after = data->notify_after;
241 pb->check_fb = data->check_fb;
242 pb->exit = data->exit;
243 pb->dev = &pdev->dev;
244 pb->enabled = false;
245
246 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable");
247 if (IS_ERR(pb->enable_gpio)) {
248 ret = PTR_ERR(pb->enable_gpio);
249 goto err_alloc;
250 }
251
252 /*
253 * Compatibility fallback for drivers still using the integer GPIO
254 * platform data. Must go away soon.
255 */
256 if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
257 ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
258 GPIOF_OUT_INIT_HIGH, "enable");
259 if (ret < 0) {
260 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
261 data->enable_gpio, ret);
262 goto err_alloc;
263 }
264
265 pb->enable_gpio = gpio_to_desc(data->enable_gpio);
266 }
267
268 if (pb->enable_gpio)
269 gpiod_direction_output(pb->enable_gpio, 1);
270
271 pb->power_supply = devm_regulator_get(&pdev->dev, "power");
272 if (IS_ERR(pb->power_supply)) {
273 ret = PTR_ERR(pb->power_supply);
274 goto err_alloc;
275 }
276
277 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
278 if (IS_ERR(pb->pwm)) {
279 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
280
281 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
282 if (IS_ERR(pb->pwm)) {
283 dev_err(&pdev->dev, "unable to request legacy PWM\n");
284 ret = PTR_ERR(pb->pwm);
285 goto err_alloc;
286 }
287 }
288
289 dev_dbg(&pdev->dev, "got pwm for backlight\n");
290
291 /*
292 * The DT case will set the pwm_period_ns field to 0 and store the
293 * period, parsed from the DT, in the PWM device. For the non-DT case,
294 * set the period from platform data if it has not already been set
295 * via the PWM lookup table.
296 */
297 pb->period = pwm_get_period(pb->pwm);
298 if (!pb->period && (data->pwm_period_ns > 0)) {
299 pb->period = data->pwm_period_ns;
300 pwm_set_period(pb->pwm, data->pwm_period_ns);
301 }
302
303 pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
304
305 memset(&props, 0, sizeof(struct backlight_properties));
306 props.type = BACKLIGHT_RAW;
307 props.max_brightness = data->max_brightness;
308 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
309 &pwm_backlight_ops, &props);
310 if (IS_ERR(bl)) {
311 dev_err(&pdev->dev, "failed to register backlight\n");
312 ret = PTR_ERR(bl);
313 goto err_alloc;
314 }
315
316 if (data->dft_brightness > data->max_brightness) {
317 dev_warn(&pdev->dev,
318 "invalid default brightness level: %u, using %u\n",
319 data->dft_brightness, data->max_brightness);
320 data->dft_brightness = data->max_brightness;
321 }
322
323 bl->props.brightness = data->dft_brightness;
324 backlight_update_status(bl);
325
326 platform_set_drvdata(pdev, bl);
327 return 0;
328
329 err_alloc:
330 if (data->exit)
331 data->exit(&pdev->dev);
332 return ret;
333 }
334
pwm_backlight_remove(struct platform_device * pdev)335 static int pwm_backlight_remove(struct platform_device *pdev)
336 {
337 struct backlight_device *bl = platform_get_drvdata(pdev);
338 struct pwm_bl_data *pb = bl_get_data(bl);
339
340 backlight_device_unregister(bl);
341 pwm_backlight_power_off(pb);
342
343 if (pb->exit)
344 pb->exit(&pdev->dev);
345
346 return 0;
347 }
348
pwm_backlight_shutdown(struct platform_device * pdev)349 static void pwm_backlight_shutdown(struct platform_device *pdev)
350 {
351 struct backlight_device *bl = platform_get_drvdata(pdev);
352 struct pwm_bl_data *pb = bl_get_data(bl);
353
354 pwm_backlight_power_off(pb);
355 }
356
357 #ifdef CONFIG_PM_SLEEP
pwm_backlight_suspend(struct device * dev)358 static int pwm_backlight_suspend(struct device *dev)
359 {
360 struct backlight_device *bl = dev_get_drvdata(dev);
361 struct pwm_bl_data *pb = bl_get_data(bl);
362
363 if (pb->notify)
364 pb->notify(pb->dev, 0);
365
366 pwm_backlight_power_off(pb);
367
368 if (pb->notify_after)
369 pb->notify_after(pb->dev, 0);
370
371 return 0;
372 }
373
pwm_backlight_resume(struct device * dev)374 static int pwm_backlight_resume(struct device *dev)
375 {
376 struct backlight_device *bl = dev_get_drvdata(dev);
377
378 backlight_update_status(bl);
379
380 return 0;
381 }
382 #endif
383
384 static const struct dev_pm_ops pwm_backlight_pm_ops = {
385 #ifdef CONFIG_PM_SLEEP
386 .suspend = pwm_backlight_suspend,
387 .resume = pwm_backlight_resume,
388 .poweroff = pwm_backlight_suspend,
389 .restore = pwm_backlight_resume,
390 #endif
391 };
392
393 static struct platform_driver pwm_backlight_driver = {
394 .driver = {
395 .name = "pwm-backlight",
396 .pm = &pwm_backlight_pm_ops,
397 .of_match_table = of_match_ptr(pwm_backlight_of_match),
398 },
399 .probe = pwm_backlight_probe,
400 .remove = pwm_backlight_remove,
401 .shutdown = pwm_backlight_shutdown,
402 };
403
404 module_platform_driver(pwm_backlight_driver);
405
406 MODULE_DESCRIPTION("PWM based Backlight Driver");
407 MODULE_LICENSE("GPL");
408 MODULE_ALIAS("platform:pwm-backlight");
409