mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / video / backlight / pwm_bl.c
blob1fea627394d7c419107506d2b29d4cd052ea3b59
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/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>
22 #include <linux/slab.h>
24 struct pwm_bl_data {
25 struct pwm_device *pwm;
26 struct device *dev;
27 unsigned int period;
28 unsigned int lth_brightness;
29 unsigned int *levels;
30 int (*notify)(struct device *,
31 int brightness);
32 void (*notify_after)(struct device *,
33 int brightness);
34 int (*check_fb)(struct device *, struct fb_info *);
35 void (*exit)(struct device *);
38 static int pwm_backlight_update_status(struct backlight_device *bl)
40 struct pwm_bl_data *pb = bl_get_data(bl);
41 int brightness = bl->props.brightness;
42 int max = bl->props.max_brightness;
44 if (bl->props.power != FB_BLANK_UNBLANK ||
45 bl->props.fb_blank != FB_BLANK_UNBLANK ||
46 bl->props.state & BL_CORE_FBBLANK)
47 brightness = 0;
49 if (pb->notify)
50 brightness = pb->notify(pb->dev, brightness);
52 if (brightness == 0) {
53 pwm_config(pb->pwm, 0, pb->period);
54 pwm_disable(pb->pwm);
55 } else {
56 int duty_cycle;
58 if (pb->levels) {
59 duty_cycle = pb->levels[brightness];
60 max = pb->levels[max];
61 } else {
62 duty_cycle = brightness;
65 duty_cycle = pb->lth_brightness +
66 (duty_cycle * (pb->period - pb->lth_brightness) / max);
67 pwm_config(pb->pwm, duty_cycle, pb->period);
68 pwm_enable(pb->pwm);
71 if (pb->notify_after)
72 pb->notify_after(pb->dev, brightness);
74 return 0;
77 static int pwm_backlight_get_brightness(struct backlight_device *bl)
79 return bl->props.brightness;
82 static int pwm_backlight_check_fb(struct backlight_device *bl,
83 struct fb_info *info)
85 struct pwm_bl_data *pb = bl_get_data(bl);
87 return !pb->check_fb || pb->check_fb(pb->dev, info);
90 static const struct backlight_ops pwm_backlight_ops = {
91 .update_status = pwm_backlight_update_status,
92 .get_brightness = pwm_backlight_get_brightness,
93 .check_fb = pwm_backlight_check_fb,
96 #ifdef CONFIG_OF
97 static int pwm_backlight_parse_dt(struct device *dev,
98 struct platform_pwm_backlight_data *data)
100 struct device_node *node = dev->of_node;
101 struct property *prop;
102 int length;
103 u32 value;
104 int ret;
106 if (!node)
107 return -ENODEV;
109 memset(data, 0, sizeof(*data));
111 /* determine the number of brightness levels */
112 prop = of_find_property(node, "brightness-levels", &length);
113 if (!prop)
114 return -EINVAL;
116 data->max_brightness = length / sizeof(u32);
118 /* read brightness levels from DT property */
119 if (data->max_brightness > 0) {
120 size_t size = sizeof(*data->levels) * data->max_brightness;
122 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
123 if (!data->levels)
124 return -ENOMEM;
126 ret = of_property_read_u32_array(node, "brightness-levels",
127 data->levels,
128 data->max_brightness);
129 if (ret < 0)
130 return ret;
132 ret = of_property_read_u32(node, "default-brightness-level",
133 &value);
134 if (ret < 0)
135 return ret;
137 data->dft_brightness = value;
138 data->max_brightness--;
142 * TODO: Most users of this driver use a number of GPIOs to control
143 * backlight power. Support for specifying these needs to be
144 * added.
147 return 0;
150 static struct of_device_id pwm_backlight_of_match[] = {
151 { .compatible = "pwm-backlight" },
155 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
156 #else
157 static int pwm_backlight_parse_dt(struct device *dev,
158 struct platform_pwm_backlight_data *data)
160 return -ENODEV;
162 #endif
164 static int pwm_backlight_probe(struct platform_device *pdev)
166 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
167 struct platform_pwm_backlight_data defdata;
168 struct backlight_properties props;
169 struct backlight_device *bl;
170 struct pwm_bl_data *pb;
171 unsigned int max;
172 int ret;
174 if (!data) {
175 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
176 if (ret < 0) {
177 dev_err(&pdev->dev, "failed to find platform data\n");
178 return ret;
181 data = &defdata;
184 if (data->init) {
185 ret = data->init(&pdev->dev);
186 if (ret < 0)
187 return ret;
190 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
191 if (!pb) {
192 dev_err(&pdev->dev, "no memory for state\n");
193 ret = -ENOMEM;
194 goto err_alloc;
197 if (data->levels) {
198 max = data->levels[data->max_brightness];
199 pb->levels = data->levels;
200 } else
201 max = data->max_brightness;
203 pb->notify = data->notify;
204 pb->notify_after = data->notify_after;
205 pb->check_fb = data->check_fb;
206 pb->exit = data->exit;
207 pb->dev = &pdev->dev;
209 pb->pwm = devm_pwm_get(&pdev->dev, NULL);
210 if (IS_ERR(pb->pwm)) {
211 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
213 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
214 if (IS_ERR(pb->pwm)) {
215 dev_err(&pdev->dev, "unable to request legacy PWM\n");
216 ret = PTR_ERR(pb->pwm);
217 goto err_alloc;
221 dev_dbg(&pdev->dev, "got pwm for backlight\n");
224 * The DT case will set the pwm_period_ns field to 0 and store the
225 * period, parsed from the DT, in the PWM device. For the non-DT case,
226 * set the period from platform data.
228 if (data->pwm_period_ns > 0)
229 pwm_set_period(pb->pwm, data->pwm_period_ns);
231 pb->period = pwm_get_period(pb->pwm);
232 pb->lth_brightness = data->lth_brightness * (pb->period / max);
234 memset(&props, 0, sizeof(struct backlight_properties));
235 props.type = BACKLIGHT_RAW;
236 props.max_brightness = data->max_brightness;
237 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
238 &pwm_backlight_ops, &props);
239 if (IS_ERR(bl)) {
240 dev_err(&pdev->dev, "failed to register backlight\n");
241 ret = PTR_ERR(bl);
242 goto err_alloc;
245 if (data->dft_brightness > data->max_brightness) {
246 dev_warn(&pdev->dev,
247 "invalid default brightness level: %u, using %u\n",
248 data->dft_brightness, data->max_brightness);
249 data->dft_brightness = data->max_brightness;
252 bl->props.brightness = data->dft_brightness;
253 backlight_update_status(bl);
255 platform_set_drvdata(pdev, bl);
256 return 0;
258 err_alloc:
259 if (data->exit)
260 data->exit(&pdev->dev);
261 return ret;
264 static int pwm_backlight_remove(struct platform_device *pdev)
266 struct backlight_device *bl = platform_get_drvdata(pdev);
267 struct pwm_bl_data *pb = bl_get_data(bl);
269 backlight_device_unregister(bl);
270 pwm_config(pb->pwm, 0, pb->period);
271 pwm_disable(pb->pwm);
272 if (pb->exit)
273 pb->exit(&pdev->dev);
274 return 0;
277 #ifdef CONFIG_PM_SLEEP
278 static int pwm_backlight_suspend(struct device *dev)
280 struct backlight_device *bl = dev_get_drvdata(dev);
281 struct pwm_bl_data *pb = bl_get_data(bl);
283 if (pb->notify)
284 pb->notify(pb->dev, 0);
285 pwm_config(pb->pwm, 0, pb->period);
286 pwm_disable(pb->pwm);
287 if (pb->notify_after)
288 pb->notify_after(pb->dev, 0);
289 return 0;
292 static int pwm_backlight_resume(struct device *dev)
294 struct backlight_device *bl = dev_get_drvdata(dev);
296 backlight_update_status(bl);
297 return 0;
299 #endif
301 static SIMPLE_DEV_PM_OPS(pwm_backlight_pm_ops, pwm_backlight_suspend,
302 pwm_backlight_resume);
304 static struct platform_driver pwm_backlight_driver = {
305 .driver = {
306 .name = "pwm-backlight",
307 .owner = THIS_MODULE,
308 .pm = &pwm_backlight_pm_ops,
309 .of_match_table = of_match_ptr(pwm_backlight_of_match),
311 .probe = pwm_backlight_probe,
312 .remove = pwm_backlight_remove,
315 module_platform_driver(pwm_backlight_driver);
317 MODULE_DESCRIPTION("PWM based Backlight Driver");
318 MODULE_LICENSE("GPL");
319 MODULE_ALIAS("platform:pwm-backlight");