KVM: nVMX: Fix returned value of MSR_IA32_VMX_VMCS_ENUM
[linux/fpc-iii.git] / drivers / video / backlight / pwm_bl.c
blob38ca88bc5c3ed0c199e73b8a533ac43448506bfb
1 /*
2 * linux/drivers/video/backlight/pwm_bl.c
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
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.
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>
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 *);
45 static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
47 int err;
49 if (pb->enabled)
50 return;
52 err = regulator_enable(pb->power_supply);
53 if (err < 0)
54 dev_err(pb->dev, "failed to enable power supply\n");
56 if (pb->enable_gpio)
57 gpiod_set_value(pb->enable_gpio, 1);
59 pwm_enable(pb->pwm);
60 pb->enabled = true;
63 static void pwm_backlight_power_off(struct pwm_bl_data *pb)
65 if (!pb->enabled)
66 return;
68 pwm_config(pb->pwm, 0, pb->period);
69 pwm_disable(pb->pwm);
71 if (pb->enable_gpio)
72 gpiod_set_value(pb->enable_gpio, 0);
74 regulator_disable(pb->power_supply);
75 pb->enabled = false;
78 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
80 unsigned int lth = pb->lth_brightness;
81 int duty_cycle;
83 if (pb->levels)
84 duty_cycle = pb->levels[brightness];
85 else
86 duty_cycle = brightness;
88 return (duty_cycle * (pb->period - lth) / pb->scale) + lth;
91 static int pwm_backlight_update_status(struct backlight_device *bl)
93 struct pwm_bl_data *pb = bl_get_data(bl);
94 int brightness = bl->props.brightness;
95 int duty_cycle;
97 if (bl->props.power != FB_BLANK_UNBLANK ||
98 bl->props.fb_blank != FB_BLANK_UNBLANK ||
99 bl->props.state & BL_CORE_FBBLANK)
100 brightness = 0;
102 if (pb->notify)
103 brightness = pb->notify(pb->dev, brightness);
105 if (brightness > 0) {
106 duty_cycle = compute_duty_cycle(pb, brightness);
107 pwm_config(pb->pwm, duty_cycle, pb->period);
108 pwm_backlight_power_on(pb, brightness);
109 } else
110 pwm_backlight_power_off(pb);
112 if (pb->notify_after)
113 pb->notify_after(pb->dev, brightness);
115 return 0;
118 static int pwm_backlight_get_brightness(struct backlight_device *bl)
120 return bl->props.brightness;
123 static int pwm_backlight_check_fb(struct backlight_device *bl,
124 struct fb_info *info)
126 struct pwm_bl_data *pb = bl_get_data(bl);
128 return !pb->check_fb || pb->check_fb(pb->dev, info);
131 static const struct backlight_ops pwm_backlight_ops = {
132 .update_status = pwm_backlight_update_status,
133 .get_brightness = pwm_backlight_get_brightness,
134 .check_fb = pwm_backlight_check_fb,
137 #ifdef CONFIG_OF
138 static int pwm_backlight_parse_dt(struct device *dev,
139 struct platform_pwm_backlight_data *data)
141 struct device_node *node = dev->of_node;
142 struct property *prop;
143 int length;
144 u32 value;
145 int ret;
147 if (!node)
148 return -ENODEV;
150 memset(data, 0, sizeof(*data));
152 /* determine the number of brightness levels */
153 prop = of_find_property(node, "brightness-levels", &length);
154 if (!prop)
155 return -EINVAL;
157 data->max_brightness = length / sizeof(u32);
159 /* read brightness levels from DT property */
160 if (data->max_brightness > 0) {
161 size_t size = sizeof(*data->levels) * data->max_brightness;
163 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
164 if (!data->levels)
165 return -ENOMEM;
167 ret = of_property_read_u32_array(node, "brightness-levels",
168 data->levels,
169 data->max_brightness);
170 if (ret < 0)
171 return ret;
173 ret = of_property_read_u32(node, "default-brightness-level",
174 &value);
175 if (ret < 0)
176 return ret;
178 data->dft_brightness = value;
179 data->max_brightness--;
182 return 0;
185 static struct of_device_id pwm_backlight_of_match[] = {
186 { .compatible = "pwm-backlight" },
190 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
191 #else
192 static int pwm_backlight_parse_dt(struct device *dev,
193 struct platform_pwm_backlight_data *data)
195 return -ENODEV;
197 #endif
199 static int pwm_backlight_probe(struct platform_device *pdev)
201 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
202 struct platform_pwm_backlight_data defdata;
203 struct backlight_properties props;
204 struct backlight_device *bl;
205 struct pwm_bl_data *pb;
206 int ret;
208 if (!data) {
209 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;
215 data = &defdata;
218 if (data->init) {
219 ret = data->init(&pdev->dev);
220 if (ret < 0)
221 return ret;
224 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
225 if (!pb) {
226 ret = -ENOMEM;
227 goto err_alloc;
230 if (data->levels) {
231 unsigned int i;
233 for (i = 0; i <= data->max_brightness; i++)
234 if (data->levels[i] > pb->scale)
235 pb->scale = data->levels[i];
237 pb->levels = data->levels;
238 } else
239 pb->scale = data->max_brightness;
241 pb->notify = data->notify;
242 pb->notify_after = data->notify_after;
243 pb->check_fb = data->check_fb;
244 pb->exit = data->exit;
245 pb->dev = &pdev->dev;
246 pb->enabled = false;
248 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;
253 else
254 goto err_alloc;
258 * Compatibility fallback for drivers still using the integer GPIO
259 * platform data. Must go away soon.
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");
264 if (ret < 0) {
265 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
266 data->enable_gpio, ret);
267 goto err_alloc;
270 pb->enable_gpio = gpio_to_desc(data->enable_gpio);
273 if (pb->enable_gpio)
274 gpiod_direction_output(pb->enable_gpio, 1);
276 pb->power_supply = devm_regulator_get(&pdev->dev, "power");
277 if (IS_ERR(pb->power_supply)) {
278 ret = PTR_ERR(pb->power_supply);
279 goto err_alloc;
282 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
283 if (IS_ERR(pb->pwm)) {
284 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
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);
290 goto err_alloc;
294 dev_dbg(&pdev->dev, "got pwm for backlight\n");
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,
299 * set the period from platform data if it has not already been set
300 * via the PWM lookup table.
302 pb->period = pwm_get_period(pb->pwm);
303 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);
308 pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
310 memset(&props, 0, sizeof(struct backlight_properties));
311 props.type = BACKLIGHT_RAW;
312 props.max_brightness = data->max_brightness;
313 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
314 &pwm_backlight_ops, &props);
315 if (IS_ERR(bl)) {
316 dev_err(&pdev->dev, "failed to register backlight\n");
317 ret = PTR_ERR(bl);
318 goto err_alloc;
321 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;
328 bl->props.brightness = data->dft_brightness;
329 backlight_update_status(bl);
331 platform_set_drvdata(pdev, bl);
332 return 0;
334 err_alloc:
335 if (data->exit)
336 data->exit(&pdev->dev);
337 return ret;
340 static int pwm_backlight_remove(struct platform_device *pdev)
342 struct backlight_device *bl = platform_get_drvdata(pdev);
343 struct pwm_bl_data *pb = bl_get_data(bl);
345 backlight_device_unregister(bl);
346 pwm_backlight_power_off(pb);
348 if (pb->exit)
349 pb->exit(&pdev->dev);
351 return 0;
354 static void pwm_backlight_shutdown(struct platform_device *pdev)
356 struct backlight_device *bl = platform_get_drvdata(pdev);
357 struct pwm_bl_data *pb = bl_get_data(bl);
359 pwm_backlight_power_off(pb);
362 #ifdef CONFIG_PM_SLEEP
363 static int pwm_backlight_suspend(struct device *dev)
365 struct backlight_device *bl = dev_get_drvdata(dev);
366 struct pwm_bl_data *pb = bl_get_data(bl);
368 if (pb->notify)
369 pb->notify(pb->dev, 0);
371 pwm_backlight_power_off(pb);
373 if (pb->notify_after)
374 pb->notify_after(pb->dev, 0);
376 return 0;
379 static int pwm_backlight_resume(struct device *dev)
381 struct backlight_device *bl = dev_get_drvdata(dev);
383 backlight_update_status(bl);
385 return 0;
387 #endif
389 static 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
398 static struct platform_driver pwm_backlight_driver = {
399 .driver = {
400 .name = "pwm-backlight",
401 .owner = THIS_MODULE,
402 .pm = &pwm_backlight_pm_ops,
403 .of_match_table = of_match_ptr(pwm_backlight_of_match),
405 .probe = pwm_backlight_probe,
406 .remove = pwm_backlight_remove,
407 .shutdown = pwm_backlight_shutdown,
410 module_platform_driver(pwm_backlight_driver);
412 MODULE_DESCRIPTION("PWM based Backlight Driver");
413 MODULE_LICENSE("GPL");
414 MODULE_ALIAS("platform:pwm-backlight");