Linux 2.6.34-rc3
[pohmelfs.git] / drivers / video / backlight / pwm_bl.c
blobb89eebc3f77d357c0f68c64aa45b0b68b65f9909
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>
23 struct pwm_bl_data {
24 struct pwm_device *pwm;
25 struct device *dev;
26 unsigned int period;
27 int (*notify)(struct device *,
28 int brightness);
31 static int pwm_backlight_update_status(struct backlight_device *bl)
33 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
34 int brightness = bl->props.brightness;
35 int max = bl->props.max_brightness;
37 if (bl->props.power != FB_BLANK_UNBLANK)
38 brightness = 0;
40 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
41 brightness = 0;
43 if (pb->notify)
44 brightness = pb->notify(pb->dev, brightness);
46 if (brightness == 0) {
47 pwm_config(pb->pwm, 0, pb->period);
48 pwm_disable(pb->pwm);
49 } else {
50 pwm_config(pb->pwm, brightness * pb->period / max, pb->period);
51 pwm_enable(pb->pwm);
53 return 0;
56 static int pwm_backlight_get_brightness(struct backlight_device *bl)
58 return bl->props.brightness;
61 static const struct backlight_ops pwm_backlight_ops = {
62 .update_status = pwm_backlight_update_status,
63 .get_brightness = pwm_backlight_get_brightness,
66 static int pwm_backlight_probe(struct platform_device *pdev)
68 struct backlight_properties props;
69 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
70 struct backlight_device *bl;
71 struct pwm_bl_data *pb;
72 int ret;
74 if (!data) {
75 dev_err(&pdev->dev, "failed to find platform data\n");
76 return -EINVAL;
79 if (data->init) {
80 ret = data->init(&pdev->dev);
81 if (ret < 0)
82 return ret;
85 pb = kzalloc(sizeof(*pb), GFP_KERNEL);
86 if (!pb) {
87 dev_err(&pdev->dev, "no memory for state\n");
88 ret = -ENOMEM;
89 goto err_alloc;
92 pb->period = data->pwm_period_ns;
93 pb->notify = data->notify;
94 pb->dev = &pdev->dev;
96 pb->pwm = pwm_request(data->pwm_id, "backlight");
97 if (IS_ERR(pb->pwm)) {
98 dev_err(&pdev->dev, "unable to request PWM for backlight\n");
99 ret = PTR_ERR(pb->pwm);
100 goto err_pwm;
101 } else
102 dev_dbg(&pdev->dev, "got pwm for backlight\n");
104 memset(&props, 0, sizeof(struct backlight_properties));
105 props.max_brightness = data->max_brightness;
106 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
107 &pwm_backlight_ops, &props);
108 if (IS_ERR(bl)) {
109 dev_err(&pdev->dev, "failed to register backlight\n");
110 ret = PTR_ERR(bl);
111 goto err_bl;
114 bl->props.brightness = data->dft_brightness;
115 backlight_update_status(bl);
117 platform_set_drvdata(pdev, bl);
118 return 0;
120 err_bl:
121 pwm_free(pb->pwm);
122 err_pwm:
123 kfree(pb);
124 err_alloc:
125 if (data->exit)
126 data->exit(&pdev->dev);
127 return ret;
130 static int pwm_backlight_remove(struct platform_device *pdev)
132 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
133 struct backlight_device *bl = platform_get_drvdata(pdev);
134 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
136 backlight_device_unregister(bl);
137 pwm_config(pb->pwm, 0, pb->period);
138 pwm_disable(pb->pwm);
139 pwm_free(pb->pwm);
140 kfree(pb);
141 if (data->exit)
142 data->exit(&pdev->dev);
143 return 0;
146 #ifdef CONFIG_PM
147 static int pwm_backlight_suspend(struct platform_device *pdev,
148 pm_message_t state)
150 struct backlight_device *bl = platform_get_drvdata(pdev);
151 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
153 if (pb->notify)
154 pb->notify(pb->dev, 0);
155 pwm_config(pb->pwm, 0, pb->period);
156 pwm_disable(pb->pwm);
157 return 0;
160 static int pwm_backlight_resume(struct platform_device *pdev)
162 struct backlight_device *bl = platform_get_drvdata(pdev);
164 backlight_update_status(bl);
165 return 0;
167 #else
168 #define pwm_backlight_suspend NULL
169 #define pwm_backlight_resume NULL
170 #endif
172 static struct platform_driver pwm_backlight_driver = {
173 .driver = {
174 .name = "pwm-backlight",
175 .owner = THIS_MODULE,
177 .probe = pwm_backlight_probe,
178 .remove = pwm_backlight_remove,
179 .suspend = pwm_backlight_suspend,
180 .resume = pwm_backlight_resume,
183 static int __init pwm_backlight_init(void)
185 return platform_driver_register(&pwm_backlight_driver);
187 module_init(pwm_backlight_init);
189 static void __exit pwm_backlight_exit(void)
191 platform_driver_unregister(&pwm_backlight_driver);
193 module_exit(pwm_backlight_exit);
195 MODULE_DESCRIPTION("PWM based Backlight Driver");
196 MODULE_LICENSE("GPL");
197 MODULE_ALIAS("platform:pwm-backlight");