Add linux-next specific files for 20110801
[linux-2.6/next.git] / drivers / video / backlight / pwm_bl.c
blobb8f38ec6eb1898ab0b60d606ec1f3998e11fe339
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 int (*notify)(struct device *,
30 int brightness);
31 int (*check_fb)(struct device *, struct fb_info *);
34 static int pwm_backlight_update_status(struct backlight_device *bl)
36 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
37 int brightness = bl->props.brightness;
38 int max = bl->props.max_brightness;
40 if (bl->props.power != FB_BLANK_UNBLANK)
41 brightness = 0;
43 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
44 brightness = 0;
46 if (pb->notify)
47 brightness = pb->notify(pb->dev, brightness);
49 if (brightness == 0) {
50 pwm_config(pb->pwm, 0, pb->period);
51 pwm_disable(pb->pwm);
52 } else {
53 brightness = pb->lth_brightness +
54 (brightness * (pb->period - pb->lth_brightness) / max);
55 pwm_config(pb->pwm, brightness, pb->period);
56 pwm_enable(pb->pwm);
58 return 0;
61 static int pwm_backlight_get_brightness(struct backlight_device *bl)
63 return bl->props.brightness;
66 static int pwm_backlight_check_fb(struct backlight_device *bl,
67 struct fb_info *info)
69 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
71 return !pb->check_fb || pb->check_fb(pb->dev, info);
74 static const struct backlight_ops pwm_backlight_ops = {
75 .update_status = pwm_backlight_update_status,
76 .get_brightness = pwm_backlight_get_brightness,
77 .check_fb = pwm_backlight_check_fb,
80 static int pwm_backlight_probe(struct platform_device *pdev)
82 struct backlight_properties props;
83 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
84 struct backlight_device *bl;
85 struct pwm_bl_data *pb;
86 int ret;
88 if (!data) {
89 dev_err(&pdev->dev, "failed to find platform data\n");
90 return -EINVAL;
93 if (data->init) {
94 ret = data->init(&pdev->dev);
95 if (ret < 0)
96 return ret;
99 pb = kzalloc(sizeof(*pb), GFP_KERNEL);
100 if (!pb) {
101 dev_err(&pdev->dev, "no memory for state\n");
102 ret = -ENOMEM;
103 goto err_alloc;
106 pb->period = data->pwm_period_ns;
107 pb->notify = data->notify;
108 pb->check_fb = data->check_fb;
109 pb->lth_brightness = data->lth_brightness *
110 (data->pwm_period_ns / data->max_brightness);
111 pb->dev = &pdev->dev;
113 pb->pwm = pwm_request(data->pwm_id, "backlight");
114 if (IS_ERR(pb->pwm)) {
115 dev_err(&pdev->dev, "unable to request PWM for backlight\n");
116 ret = PTR_ERR(pb->pwm);
117 goto err_pwm;
118 } else
119 dev_dbg(&pdev->dev, "got pwm for backlight\n");
121 memset(&props, 0, sizeof(struct backlight_properties));
122 props.type = BACKLIGHT_RAW;
123 props.max_brightness = data->max_brightness;
124 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
125 &pwm_backlight_ops, &props);
126 if (IS_ERR(bl)) {
127 dev_err(&pdev->dev, "failed to register backlight\n");
128 ret = PTR_ERR(bl);
129 goto err_bl;
132 bl->props.brightness = data->dft_brightness;
133 backlight_update_status(bl);
135 platform_set_drvdata(pdev, bl);
136 return 0;
138 err_bl:
139 pwm_free(pb->pwm);
140 err_pwm:
141 kfree(pb);
142 err_alloc:
143 if (data->exit)
144 data->exit(&pdev->dev);
145 return ret;
148 static int pwm_backlight_remove(struct platform_device *pdev)
150 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
151 struct backlight_device *bl = platform_get_drvdata(pdev);
152 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
154 backlight_device_unregister(bl);
155 pwm_config(pb->pwm, 0, pb->period);
156 pwm_disable(pb->pwm);
157 pwm_free(pb->pwm);
158 kfree(pb);
159 if (data->exit)
160 data->exit(&pdev->dev);
161 return 0;
164 #ifdef CONFIG_PM
165 static int pwm_backlight_suspend(struct platform_device *pdev,
166 pm_message_t state)
168 struct backlight_device *bl = platform_get_drvdata(pdev);
169 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
171 if (pb->notify)
172 pb->notify(pb->dev, 0);
173 pwm_config(pb->pwm, 0, pb->period);
174 pwm_disable(pb->pwm);
175 return 0;
178 static int pwm_backlight_resume(struct platform_device *pdev)
180 struct backlight_device *bl = platform_get_drvdata(pdev);
182 backlight_update_status(bl);
183 return 0;
185 #else
186 #define pwm_backlight_suspend NULL
187 #define pwm_backlight_resume NULL
188 #endif
190 static struct platform_driver pwm_backlight_driver = {
191 .driver = {
192 .name = "pwm-backlight",
193 .owner = THIS_MODULE,
195 .probe = pwm_backlight_probe,
196 .remove = pwm_backlight_remove,
197 .suspend = pwm_backlight_suspend,
198 .resume = pwm_backlight_resume,
201 static int __init pwm_backlight_init(void)
203 return platform_driver_register(&pwm_backlight_driver);
205 module_init(pwm_backlight_init);
207 static void __exit pwm_backlight_exit(void)
209 platform_driver_unregister(&pwm_backlight_driver);
211 module_exit(pwm_backlight_exit);
213 MODULE_DESCRIPTION("PWM based Backlight Driver");
214 MODULE_LICENSE("GPL");
215 MODULE_ALIAS("platform:pwm-backlight");