blob: d3bc56296c8d98007321a1cce4945c7bdd7e0259 [file] [log] [blame]
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +00001/*
2 * Backlight driver for OMAP based boards.
3 *
4 * Copyright (c) 2006 Andrzej Zaborowski <balrog@zabor.org>
5 *
6 * This package is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This package is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this package; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +000028
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070030#include <plat/board.h>
31#include <plat/mux.h>
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +000032
33#define OMAPBL_MAX_INTENSITY 0xff
34
35struct omap_backlight {
36 int powermode;
37 int current_intensity;
38
39 struct device *dev;
40 struct omap_backlight_config *pdata;
41};
42
43static void inline omapbl_send_intensity(int intensity)
44{
45 omap_writeb(intensity, OMAP_PWL_ENABLE);
46}
47
48static void inline omapbl_send_enable(int enable)
49{
50 omap_writeb(enable, OMAP_PWL_CLK_ENABLE);
51}
52
53static void omapbl_blank(struct omap_backlight *bl, int mode)
54{
55 if (bl->pdata->set_power)
56 bl->pdata->set_power(bl->dev, mode);
57
58 switch (mode) {
59 case FB_BLANK_NORMAL:
60 case FB_BLANK_VSYNC_SUSPEND:
61 case FB_BLANK_HSYNC_SUSPEND:
62 case FB_BLANK_POWERDOWN:
63 omapbl_send_intensity(0);
64 omapbl_send_enable(0);
65 break;
66
67 case FB_BLANK_UNBLANK:
68 omapbl_send_intensity(bl->current_intensity);
69 omapbl_send_enable(1);
70 break;
71 }
72}
73
74#ifdef CONFIG_PM
75static int omapbl_suspend(struct platform_device *pdev, pm_message_t state)
76{
77 struct backlight_device *dev = platform_get_drvdata(pdev);
78 struct omap_backlight *bl = dev_get_drvdata(&dev->dev);
79
80 omapbl_blank(bl, FB_BLANK_POWERDOWN);
81 return 0;
82}
83
84static int omapbl_resume(struct platform_device *pdev)
85{
86 struct backlight_device *dev = platform_get_drvdata(pdev);
87 struct omap_backlight *bl = dev_get_drvdata(&dev->dev);
88
89 omapbl_blank(bl, bl->powermode);
90 return 0;
91}
92#else
93#define omapbl_suspend NULL
94#define omapbl_resume NULL
95#endif
96
97static int omapbl_set_power(struct backlight_device *dev, int state)
98{
99 struct omap_backlight *bl = dev_get_drvdata(&dev->dev);
100
101 omapbl_blank(bl, state);
102 bl->powermode = state;
103
104 return 0;
105}
106
107static int omapbl_update_status(struct backlight_device *dev)
108{
109 struct omap_backlight *bl = dev_get_drvdata(&dev->dev);
110
111 if (bl->current_intensity != dev->props.brightness) {
112 if (bl->powermode == FB_BLANK_UNBLANK)
113 omapbl_send_intensity(dev->props.brightness);
114 bl->current_intensity = dev->props.brightness;
115 }
116
117 if (dev->props.fb_blank != bl->powermode)
118 omapbl_set_power(dev, dev->props.fb_blank);
119
120 return 0;
121}
122
123static int omapbl_get_intensity(struct backlight_device *dev)
124{
125 struct omap_backlight *bl = dev_get_drvdata(&dev->dev);
126 return bl->current_intensity;
127}
128
Emese Revfy9905a432009-12-14 00:58:57 +0100129static const struct backlight_ops omapbl_ops = {
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000130 .get_brightness = omapbl_get_intensity,
131 .update_status = omapbl_update_status,
132};
133
134static int omapbl_probe(struct platform_device *pdev)
135{
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500136 struct backlight_properties props;
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000137 struct backlight_device *dev;
138 struct omap_backlight *bl;
139 struct omap_backlight_config *pdata = pdev->dev.platform_data;
140
141 if (!pdata)
142 return -ENXIO;
143
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000144 bl = kzalloc(sizeof(struct omap_backlight), GFP_KERNEL);
145 if (unlikely(!bl))
146 return -ENOMEM;
147
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500148 memset(&props, 0, sizeof(struct backlight_properties));
149 props.max_brightness = OMAPBL_MAX_INTENSITY;
150 dev = backlight_device_register("omap-bl", &pdev->dev, bl, &omapbl_ops,
151 &props);
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000152 if (IS_ERR(dev)) {
153 kfree(bl);
154 return PTR_ERR(dev);
155 }
156
157 bl->powermode = FB_BLANK_POWERDOWN;
158 bl->current_intensity = 0;
159
160 bl->pdata = pdata;
161 bl->dev = &pdev->dev;
162
163 platform_set_drvdata(pdev, dev);
164
165 omap_cfg_reg(PWL); /* Conflicts with UART3 */
166
167 dev->props.fb_blank = FB_BLANK_UNBLANK;
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000168 dev->props.brightness = pdata->default_intensity;
169 omapbl_update_status(dev);
170
171 printk(KERN_INFO "OMAP LCD backlight initialised\n");
172
173 return 0;
174}
175
176static int omapbl_remove(struct platform_device *pdev)
177{
178 struct backlight_device *dev = platform_get_drvdata(pdev);
179 struct omap_backlight *bl = dev_get_drvdata(&dev->dev);
180
181 backlight_device_unregister(dev);
182 kfree(bl);
183
184 return 0;
185}
186
187static struct platform_driver omapbl_driver = {
188 .probe = omapbl_probe,
189 .remove = omapbl_remove,
190 .suspend = omapbl_suspend,
191 .resume = omapbl_resume,
192 .driver = {
193 .name = "omap-bl",
194 },
195};
196
197static int __init omapbl_init(void)
198{
199 return platform_driver_register(&omapbl_driver);
200}
201
202static void __exit omapbl_exit(void)
203{
204 platform_driver_unregister(&omapbl_driver);
205}
206
207module_init(omapbl_init);
208module_exit(omapbl_exit);
209
210MODULE_AUTHOR("Andrzej Zaborowski <balrog@zabor.org>");
211MODULE_DESCRIPTION("OMAP LCD Backlight driver");
212MODULE_LICENSE("GPL");