block: move down direct IO plugging
[linux/fpc-iii.git] / drivers / video / backlight / pwm_bl.c
blob995f0164c9b082c7da2836123adfcb2b7f10a6c7
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 = dev_get_drvdata(&bl->dev);
41 int brightness = bl->props.brightness;
42 int max = bl->props.max_brightness;
44 if (bl->props.power != FB_BLANK_UNBLANK)
45 brightness = 0;
47 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
48 brightness = 0;
50 if (pb->notify)
51 brightness = pb->notify(pb->dev, brightness);
53 if (brightness == 0) {
54 pwm_config(pb->pwm, 0, pb->period);
55 pwm_disable(pb->pwm);
56 } else {
57 int duty_cycle;
59 if (pb->levels) {
60 duty_cycle = pb->levels[brightness];
61 max = pb->levels[max];
62 } else {
63 duty_cycle = brightness;
66 duty_cycle = pb->lth_brightness +
67 (duty_cycle * (pb->period - pb->lth_brightness) / max);
68 pwm_config(pb->pwm, duty_cycle, pb->period);
69 pwm_enable(pb->pwm);
72 if (pb->notify_after)
73 pb->notify_after(pb->dev, brightness);
75 return 0;
78 static int pwm_backlight_get_brightness(struct backlight_device *bl)
80 return bl->props.brightness;
83 static int pwm_backlight_check_fb(struct backlight_device *bl,
84 struct fb_info *info)
86 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
88 return !pb->check_fb || pb->check_fb(pb->dev, info);
91 static const struct backlight_ops pwm_backlight_ops = {
92 .update_status = pwm_backlight_update_status,
93 .get_brightness = pwm_backlight_get_brightness,
94 .check_fb = pwm_backlight_check_fb,
97 #ifdef CONFIG_OF
98 static int pwm_backlight_parse_dt(struct device *dev,
99 struct platform_pwm_backlight_data *data)
101 struct device_node *node = dev->of_node;
102 struct property *prop;
103 int length;
104 u32 value;
105 int ret;
107 if (!node)
108 return -ENODEV;
110 memset(data, 0, sizeof(*data));
112 /* determine the number of brightness levels */
113 prop = of_find_property(node, "brightness-levels", &length);
114 if (!prop)
115 return -EINVAL;
117 data->max_brightness = length / sizeof(u32);
119 /* read brightness levels from DT property */
120 if (data->max_brightness > 0) {
121 size_t size = sizeof(*data->levels) * data->max_brightness;
123 data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
124 if (!data->levels)
125 return -ENOMEM;
127 ret = of_property_read_u32_array(node, "brightness-levels",
128 data->levels,
129 data->max_brightness);
130 if (ret < 0)
131 return ret;
133 ret = of_property_read_u32(node, "default-brightness-level",
134 &value);
135 if (ret < 0)
136 return ret;
138 if (value >= data->max_brightness) {
139 dev_warn(dev, "invalid default brightness level: %u, using %u\n",
140 value, data->max_brightness - 1);
141 value = data->max_brightness - 1;
144 data->dft_brightness = value;
145 data->max_brightness--;
149 * TODO: Most users of this driver use a number of GPIOs to control
150 * backlight power. Support for specifying these needs to be
151 * added.
154 return 0;
157 static struct of_device_id pwm_backlight_of_match[] = {
158 { .compatible = "pwm-backlight" },
162 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
163 #else
164 static int pwm_backlight_parse_dt(struct device *dev,
165 struct platform_pwm_backlight_data *data)
167 return -ENODEV;
169 #endif
171 static int pwm_backlight_probe(struct platform_device *pdev)
173 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
174 struct platform_pwm_backlight_data defdata;
175 struct backlight_properties props;
176 struct backlight_device *bl;
177 struct pwm_bl_data *pb;
178 unsigned int max;
179 int ret;
181 if (!data) {
182 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
183 if (ret < 0) {
184 dev_err(&pdev->dev, "failed to find platform data\n");
185 return ret;
188 data = &defdata;
191 if (data->init) {
192 ret = data->init(&pdev->dev);
193 if (ret < 0)
194 return ret;
197 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
198 if (!pb) {
199 dev_err(&pdev->dev, "no memory for state\n");
200 ret = -ENOMEM;
201 goto err_alloc;
204 if (data->levels) {
205 max = data->levels[data->max_brightness];
206 pb->levels = data->levels;
207 } else
208 max = data->max_brightness;
210 pb->notify = data->notify;
211 pb->notify_after = data->notify_after;
212 pb->check_fb = data->check_fb;
213 pb->exit = data->exit;
214 pb->dev = &pdev->dev;
216 pb->pwm = pwm_get(&pdev->dev, NULL);
217 if (IS_ERR(pb->pwm)) {
218 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
220 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
221 if (IS_ERR(pb->pwm)) {
222 dev_err(&pdev->dev, "unable to request legacy PWM\n");
223 ret = PTR_ERR(pb->pwm);
224 goto err_alloc;
228 dev_dbg(&pdev->dev, "got pwm for backlight\n");
231 * The DT case will set the pwm_period_ns field to 0 and store the
232 * period, parsed from the DT, in the PWM device. For the non-DT case,
233 * set the period from platform data.
235 if (data->pwm_period_ns > 0)
236 pwm_set_period(pb->pwm, data->pwm_period_ns);
238 pb->period = pwm_get_period(pb->pwm);
239 pb->lth_brightness = data->lth_brightness * (pb->period / max);
241 memset(&props, 0, sizeof(struct backlight_properties));
242 props.type = BACKLIGHT_RAW;
243 props.max_brightness = data->max_brightness;
244 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
245 &pwm_backlight_ops, &props);
246 if (IS_ERR(bl)) {
247 dev_err(&pdev->dev, "failed to register backlight\n");
248 ret = PTR_ERR(bl);
249 goto err_bl;
252 bl->props.brightness = data->dft_brightness;
253 backlight_update_status(bl);
255 platform_set_drvdata(pdev, bl);
256 return 0;
258 err_bl:
259 pwm_put(pb->pwm);
260 err_alloc:
261 if (data->exit)
262 data->exit(&pdev->dev);
263 return ret;
266 static int pwm_backlight_remove(struct platform_device *pdev)
268 struct backlight_device *bl = platform_get_drvdata(pdev);
269 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
271 backlight_device_unregister(bl);
272 pwm_config(pb->pwm, 0, pb->period);
273 pwm_disable(pb->pwm);
274 pwm_put(pb->pwm);
275 if (pb->exit)
276 pb->exit(&pdev->dev);
277 return 0;
280 #ifdef CONFIG_PM
281 static int pwm_backlight_suspend(struct device *dev)
283 struct backlight_device *bl = dev_get_drvdata(dev);
284 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
286 if (pb->notify)
287 pb->notify(pb->dev, 0);
288 pwm_config(pb->pwm, 0, pb->period);
289 pwm_disable(pb->pwm);
290 if (pb->notify_after)
291 pb->notify_after(pb->dev, 0);
292 return 0;
295 static int pwm_backlight_resume(struct device *dev)
297 struct backlight_device *bl = dev_get_drvdata(dev);
299 backlight_update_status(bl);
300 return 0;
303 static SIMPLE_DEV_PM_OPS(pwm_backlight_pm_ops, pwm_backlight_suspend,
304 pwm_backlight_resume);
306 #endif
308 static struct platform_driver pwm_backlight_driver = {
309 .driver = {
310 .name = "pwm-backlight",
311 .owner = THIS_MODULE,
312 #ifdef CONFIG_PM
313 .pm = &pwm_backlight_pm_ops,
314 #endif
315 .of_match_table = of_match_ptr(pwm_backlight_of_match),
317 .probe = pwm_backlight_probe,
318 .remove = pwm_backlight_remove,
321 module_platform_driver(pwm_backlight_driver);
323 MODULE_DESCRIPTION("PWM based Backlight Driver");
324 MODULE_LICENSE("GPL");
325 MODULE_ALIAS("platform:pwm-backlight");