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