blob: e641584e212e64c087cada21136b6840ff18d2e5 [file] [log] [blame]
eric miao42796d32008-04-14 09:35:08 +01001/*
2 * linux/drivers/video/backlight/pwm_bl.c
3 *
4 * simple PWM based backlight control, board code has to setup
5 * 1) pin configuration so PWM waveforms can output
Eric Miaob8cdd872009-02-10 13:30:37 +08006 * 2) platform_data being correctly configured
eric miao42796d32008-04-14 09:35:08 +01007 *
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.
11 */
12
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
23struct pwm_bl_data {
24 struct pwm_device *pwm;
25 unsigned int period;
Philipp Zabel3b731252008-05-22 14:18:40 +010026 int (*notify)(int brightness);
eric miao42796d32008-04-14 09:35:08 +010027};
28
29static int pwm_backlight_update_status(struct backlight_device *bl)
30{
31 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
32 int brightness = bl->props.brightness;
33 int max = bl->props.max_brightness;
34
35 if (bl->props.power != FB_BLANK_UNBLANK)
36 brightness = 0;
37
38 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
39 brightness = 0;
40
Philipp Zabel3b731252008-05-22 14:18:40 +010041 if (pb->notify)
42 brightness = pb->notify(brightness);
43
eric miao42796d32008-04-14 09:35:08 +010044 if (brightness == 0) {
45 pwm_config(pb->pwm, 0, pb->period);
46 pwm_disable(pb->pwm);
47 } else {
48 pwm_config(pb->pwm, brightness * pb->period / max, pb->period);
49 pwm_enable(pb->pwm);
50 }
51 return 0;
52}
53
54static int pwm_backlight_get_brightness(struct backlight_device *bl)
55{
56 return bl->props.brightness;
57}
58
59static struct backlight_ops pwm_backlight_ops = {
60 .update_status = pwm_backlight_update_status,
61 .get_brightness = pwm_backlight_get_brightness,
62};
63
64static int pwm_backlight_probe(struct platform_device *pdev)
65{
66 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
67 struct backlight_device *bl;
68 struct pwm_bl_data *pb;
Philipp Zabel3b731252008-05-22 14:18:40 +010069 int ret;
eric miao42796d32008-04-14 09:35:08 +010070
Ben Dooks14563a42008-08-05 13:01:22 -070071 if (!data) {
72 dev_err(&pdev->dev, "failed to find platform data\n");
eric miao42796d32008-04-14 09:35:08 +010073 return -EINVAL;
Ben Dooks14563a42008-08-05 13:01:22 -070074 }
eric miao42796d32008-04-14 09:35:08 +010075
Philipp Zabel3b731252008-05-22 14:18:40 +010076 if (data->init) {
77 ret = data->init(&pdev->dev);
78 if (ret < 0)
79 return ret;
80 }
81
eric miao42796d32008-04-14 09:35:08 +010082 pb = kzalloc(sizeof(*pb), GFP_KERNEL);
Philipp Zabel3b731252008-05-22 14:18:40 +010083 if (!pb) {
Ben Dooks14563a42008-08-05 13:01:22 -070084 dev_err(&pdev->dev, "no memory for state\n");
Philipp Zabel3b731252008-05-22 14:18:40 +010085 ret = -ENOMEM;
86 goto err_alloc;
87 }
eric miao42796d32008-04-14 09:35:08 +010088
89 pb->period = data->pwm_period_ns;
Philipp Zabel3b731252008-05-22 14:18:40 +010090 pb->notify = data->notify;
eric miao42796d32008-04-14 09:35:08 +010091
92 pb->pwm = pwm_request(data->pwm_id, "backlight");
Ben Dooks43bda1a2008-07-01 14:18:27 +010093 if (IS_ERR(pb->pwm)) {
eric miao42796d32008-04-14 09:35:08 +010094 dev_err(&pdev->dev, "unable to request PWM for backlight\n");
Ben Dooks43bda1a2008-07-01 14:18:27 +010095 ret = PTR_ERR(pb->pwm);
Philipp Zabel3b731252008-05-22 14:18:40 +010096 goto err_pwm;
Ben Dooks14563a42008-08-05 13:01:22 -070097 } else
98 dev_dbg(&pdev->dev, "got pwm for backlight\n");
eric miao42796d32008-04-14 09:35:08 +010099
Eric Miaob8cdd872009-02-10 13:30:37 +0800100 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev,
eric miao42796d32008-04-14 09:35:08 +0100101 pb, &pwm_backlight_ops);
102 if (IS_ERR(bl)) {
103 dev_err(&pdev->dev, "failed to register backlight\n");
Philipp Zabel3b731252008-05-22 14:18:40 +0100104 ret = PTR_ERR(bl);
105 goto err_bl;
eric miao42796d32008-04-14 09:35:08 +0100106 }
107
108 bl->props.max_brightness = data->max_brightness;
109 bl->props.brightness = data->dft_brightness;
110 backlight_update_status(bl);
111
112 platform_set_drvdata(pdev, bl);
113 return 0;
Philipp Zabel3b731252008-05-22 14:18:40 +0100114
115err_bl:
116 pwm_free(pb->pwm);
117err_pwm:
118 kfree(pb);
119err_alloc:
120 if (data->exit)
121 data->exit(&pdev->dev);
122 return ret;
eric miao42796d32008-04-14 09:35:08 +0100123}
124
125static int pwm_backlight_remove(struct platform_device *pdev)
126{
Philipp Zabel3b731252008-05-22 14:18:40 +0100127 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
eric miao42796d32008-04-14 09:35:08 +0100128 struct backlight_device *bl = platform_get_drvdata(pdev);
129 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
130
131 backlight_device_unregister(bl);
132 pwm_config(pb->pwm, 0, pb->period);
133 pwm_disable(pb->pwm);
134 pwm_free(pb->pwm);
135 kfree(pb);
Philipp Zabel3b731252008-05-22 14:18:40 +0100136 if (data->exit)
137 data->exit(&pdev->dev);
eric miao42796d32008-04-14 09:35:08 +0100138 return 0;
139}
140
141#ifdef CONFIG_PM
142static int pwm_backlight_suspend(struct platform_device *pdev,
143 pm_message_t state)
144{
145 struct backlight_device *bl = platform_get_drvdata(pdev);
146 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
147
148 pwm_config(pb->pwm, 0, pb->period);
149 pwm_disable(pb->pwm);
150 return 0;
151}
152
153static int pwm_backlight_resume(struct platform_device *pdev)
154{
155 struct backlight_device *bl = platform_get_drvdata(pdev);
156
157 backlight_update_status(bl);
158 return 0;
159}
160#else
161#define pwm_backlight_suspend NULL
162#define pwm_backlight_resume NULL
163#endif
164
165static struct platform_driver pwm_backlight_driver = {
166 .driver = {
167 .name = "pwm-backlight",
168 .owner = THIS_MODULE,
169 },
170 .probe = pwm_backlight_probe,
171 .remove = pwm_backlight_remove,
172 .suspend = pwm_backlight_suspend,
173 .resume = pwm_backlight_resume,
174};
175
176static int __init pwm_backlight_init(void)
177{
178 return platform_driver_register(&pwm_backlight_driver);
179}
180module_init(pwm_backlight_init);
181
182static void __exit pwm_backlight_exit(void)
183{
184 platform_driver_unregister(&pwm_backlight_driver);
185}
186module_exit(pwm_backlight_exit);
187
188MODULE_DESCRIPTION("PWM based Backlight Driver");
189MODULE_LICENSE("GPL");
Ben Dooks8cd68192008-08-05 13:01:24 -0700190MODULE_ALIAS("platform:pwm-backlight");
191