b5ed7e91443efe3d9970812dc479cb7a47e3fbd3
[linux-2.6.git] / drivers / video / backlight / pwm_bl.c
1 /*
2  * linux/drivers/video/backlight/pwm_bl.c
3  *
4  * Copyright (c) 2011-2013, NVIDIA CORPORATION, All rights reserved.
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * simple PWM based backlight control, board code has to setup
16  * 1) pin configuration so PWM waveforms can output
17  * 2) platform_data being correctly configured
18  *
19  */
20
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/platform_device.h>
25 #include <linux/fb.h>
26 #include <linux/backlight.h>
27 #include <linux/err.h>
28 #include <linux/gpio.h>
29 #include <linux/pwm.h>
30 #include <linux/pwm_backlight.h>
31 #include <linux/slab.h>
32
33 struct pwm_bl_data {
34         struct pwm_device       *pwm;
35         struct device           *dev;
36         unsigned int            period;
37         unsigned int            lth_brightness;
38         unsigned int            pwm_gpio;
39         int                     (*notify)(struct device *,
40                                           int brightness);
41         void                    (*notify_after)(struct device *,
42                                         int brightness);
43         int                     (*check_fb)(struct device *, struct fb_info *);
44         int (*display_init)(struct device *);
45 };
46
47 static int pwm_backlight_update_status(struct backlight_device *bl)
48 {
49         struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
50         int brightness = bl->props.brightness;
51         int max = bl->props.max_brightness;
52         if (pb->display_init && !pb->display_init(pb->dev))
53                 brightness = 0;
54
55         if (bl->props.power != FB_BLANK_UNBLANK)
56                 brightness = 0;
57
58         if (bl->props.fb_blank != FB_BLANK_UNBLANK)
59                 brightness = 0;
60
61         if (pb->notify)
62                 brightness = pb->notify(pb->dev, brightness);
63
64         if (brightness == 0) {
65                 pwm_config(pb->pwm, 0, pb->period);
66                 pwm_disable(pb->pwm);
67         } else {
68                 brightness = pb->lth_brightness +
69                         (brightness * (pb->period - pb->lth_brightness) / max);
70                 pwm_config(pb->pwm, brightness, pb->period);
71                 pwm_enable(pb->pwm);
72         }
73
74         if (pb->notify_after)
75                 pb->notify_after(pb->dev, brightness);
76
77         return 0;
78 }
79
80 static int pwm_backlight_get_brightness(struct backlight_device *bl)
81 {
82         return bl->props.brightness;
83 }
84
85 static int pwm_backlight_check_fb(struct backlight_device *bl,
86                                   struct fb_info *info)
87 {
88         struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
89
90         return !pb->check_fb || pb->check_fb(pb->dev, info);
91 }
92
93 static const struct backlight_ops pwm_backlight_ops = {
94         .update_status  = pwm_backlight_update_status,
95         .get_brightness = pwm_backlight_get_brightness,
96         .check_fb       = pwm_backlight_check_fb,
97 };
98
99 static int pwm_backlight_probe(struct platform_device *pdev)
100 {
101         struct backlight_properties props;
102         struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
103         struct backlight_device *bl;
104         struct pwm_bl_data *pb;
105         int ret;
106
107         if (!data) {
108                 dev_err(&pdev->dev, "failed to find platform data\n");
109                 return -EINVAL;
110         }
111
112         if (data->init) {
113                 ret = data->init(&pdev->dev);
114                 if (ret < 0)
115                         return ret;
116         }
117
118         pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
119         if (!pb) {
120                 dev_err(&pdev->dev, "no memory for state\n");
121                 ret = -ENOMEM;
122                 goto err_alloc;
123         }
124
125         pb->period = data->pwm_period_ns;
126         pb->notify = data->notify;
127         pb->notify_after = data->notify_after;
128         pb->check_fb = data->check_fb;
129         pb->lth_brightness = data->lth_brightness *
130                 (data->pwm_period_ns / data->max_brightness);
131         pb->dev = &pdev->dev;
132         pb->display_init = data->init;
133         pb->pwm_gpio = data->pwm_gpio;
134
135         pb->pwm = pwm_request(data->pwm_id, "backlight");
136         if (IS_ERR(pb->pwm)) {
137                 dev_err(&pdev->dev, "unable to request PWM for backlight\n");
138                 ret = PTR_ERR(pb->pwm);
139                 goto err_alloc;
140         } else
141                 dev_dbg(&pdev->dev, "got pwm for backlight\n");
142
143         memset(&props, 0, sizeof(struct backlight_properties));
144         props.type = BACKLIGHT_RAW;
145         props.max_brightness = data->max_brightness;
146
147         if (gpio_is_valid(pb->pwm_gpio)) {
148                 ret = gpio_request(pb->pwm_gpio, "disp_bl");
149                 if (ret)
150                         dev_err(&pdev->dev, "backlight gpio request failed\n");
151         }
152
153         bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
154                                        &pwm_backlight_ops, &props);
155         if (IS_ERR(bl)) {
156                 dev_err(&pdev->dev, "failed to register backlight\n");
157                 ret = PTR_ERR(bl);
158                 goto err_bl;
159         }
160
161         bl->props.brightness = data->dft_brightness;
162         backlight_update_status(bl);
163
164         if (gpio_is_valid(pb->pwm_gpio))
165                 gpio_free(pb->pwm_gpio);
166
167         platform_set_drvdata(pdev, bl);
168         return 0;
169
170 err_bl:
171         pwm_free(pb->pwm);
172 err_alloc:
173         if (data->exit)
174                 data->exit(&pdev->dev);
175         return ret;
176 }
177
178 static int pwm_backlight_remove(struct platform_device *pdev)
179 {
180         struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
181         struct backlight_device *bl = platform_get_drvdata(pdev);
182         struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
183
184         backlight_device_unregister(bl);
185         pwm_config(pb->pwm, 0, pb->period);
186         pwm_disable(pb->pwm);
187         pwm_free(pb->pwm);
188         if (data->exit)
189                 data->exit(&pdev->dev);
190         return 0;
191 }
192
193 #ifdef CONFIG_PM
194 static int pwm_backlight_suspend(struct device *dev)
195 {
196         struct backlight_device *bl = dev_get_drvdata(dev);
197         struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
198
199         if (pb->notify)
200                 pb->notify(pb->dev, 0);
201         pwm_config(pb->pwm, 0, pb->period);
202         pwm_disable(pb->pwm);
203         if (pb->notify_after)
204                 pb->notify_after(pb->dev, 0);
205         return 0;
206 }
207
208 static int pwm_backlight_resume(struct device *dev)
209 {
210         struct backlight_device *bl = dev_get_drvdata(dev);
211
212         backlight_update_status(bl);
213         return 0;
214 }
215
216 static SIMPLE_DEV_PM_OPS(pwm_backlight_pm_ops, pwm_backlight_suspend,
217                          pwm_backlight_resume);
218
219 #endif
220
221 static struct platform_driver pwm_backlight_driver = {
222         .driver         = {
223                 .name   = "pwm-backlight",
224                 .owner  = THIS_MODULE,
225 #ifdef CONFIG_PM
226                 .pm     = &pwm_backlight_pm_ops,
227 #endif
228         },
229         .probe          = pwm_backlight_probe,
230         .remove         = pwm_backlight_remove,
231 };
232
233 module_platform_driver(pwm_backlight_driver);
234
235 MODULE_DESCRIPTION("PWM based Backlight Driver");
236 MODULE_LICENSE("GPL");
237 MODULE_ALIAS("platform:pwm-backlight");
238