[ARM] 3096/1: Add SharpSL Zaurus power and battery management core driver
[linux-2.6.git] / drivers / video / backlight / corgi_bl.c
1 /*
2  *  Backlight Driver for Sharp Corgi
3  *
4  *  Copyright (c) 2004-2005 Richard Purdie
5  *
6  *  Based on Sharp's 2.4 Backlight Driver
7  *
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
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/platform_device.h>
18 #include <linux/spinlock.h>
19 #include <linux/fb.h>
20 #include <linux/backlight.h>
21
22 #include <asm/arch/sharpsl.h>
23
24 #define CORGI_DEFAULT_INTENSITY         0x1f
25 #define CORGI_LIMIT_MASK                0x0b
26
27 static int corgibl_powermode = FB_BLANK_UNBLANK;
28 static int current_intensity = 0;
29 static int corgibl_limit = 0;
30 static void (*corgibl_mach_set_intensity)(int intensity);
31 static spinlock_t bl_lock = SPIN_LOCK_UNLOCKED;
32 static struct backlight_properties corgibl_data;
33
34 static void corgibl_send_intensity(int intensity)
35 {
36         unsigned long flags;
37         void (*corgi_kick_batt)(void);
38
39         if (corgibl_powermode != FB_BLANK_UNBLANK) {
40                 intensity = 0;
41         } else {
42                 if (corgibl_limit)
43                         intensity &= CORGI_LIMIT_MASK;
44         }
45
46         spin_lock_irqsave(&bl_lock, flags);
47
48         corgibl_mach_set_intensity(intensity);
49
50         spin_unlock_irqrestore(&bl_lock, flags);
51
52         corgi_kick_batt = symbol_get(sharpsl_battery_kick);
53         if (corgi_kick_batt) {
54                 corgi_kick_batt();
55                 symbol_put(sharpsl_battery_kick);
56         }
57 }
58
59 static void corgibl_blank(int blank)
60 {
61         switch(blank) {
62
63         case FB_BLANK_NORMAL:
64         case FB_BLANK_VSYNC_SUSPEND:
65         case FB_BLANK_HSYNC_SUSPEND:
66         case FB_BLANK_POWERDOWN:
67                 if (corgibl_powermode == FB_BLANK_UNBLANK) {
68                         corgibl_send_intensity(0);
69                         corgibl_powermode = blank;
70                 }
71                 break;
72         case FB_BLANK_UNBLANK:
73                 if (corgibl_powermode != FB_BLANK_UNBLANK) {
74                         corgibl_powermode = blank;
75                         corgibl_send_intensity(current_intensity);
76                 }
77                 break;
78         }
79 }
80
81 #ifdef CONFIG_PM
82 static int corgibl_suspend(struct device *dev, pm_message_t state)
83 {
84         corgibl_blank(FB_BLANK_POWERDOWN);
85         return 0;
86 }
87
88 static int corgibl_resume(struct device *dev)
89 {
90         corgibl_blank(FB_BLANK_UNBLANK);
91         return 0;
92 }
93 #else
94 #define corgibl_suspend NULL
95 #define corgibl_resume  NULL
96 #endif
97
98
99 static int corgibl_set_power(struct backlight_device *bd, int state)
100 {
101         corgibl_blank(state);
102         return 0;
103 }
104
105 static int corgibl_get_power(struct backlight_device *bd)
106 {
107         return corgibl_powermode;
108 }
109
110 static int corgibl_set_intensity(struct backlight_device *bd, int intensity)
111 {
112         if (intensity > corgibl_data.max_brightness)
113                 intensity = corgibl_data.max_brightness;
114         corgibl_send_intensity(intensity);
115         current_intensity=intensity;
116         return 0;
117 }
118
119 static int corgibl_get_intensity(struct backlight_device *bd)
120 {
121         return current_intensity;
122 }
123
124 /*
125  * Called when the battery is low to limit the backlight intensity.
126  * If limit==0 clear any limit, otherwise limit the intensity
127  */
128 void corgibl_limit_intensity(int limit)
129 {
130         corgibl_limit = (limit ? 1 : 0);
131         corgibl_send_intensity(current_intensity);
132 }
133 EXPORT_SYMBOL(corgibl_limit_intensity);
134
135
136 static struct backlight_properties corgibl_data = {
137         .owner          = THIS_MODULE,
138         .get_power      = corgibl_get_power,
139         .set_power      = corgibl_set_power,
140         .get_brightness = corgibl_get_intensity,
141         .set_brightness = corgibl_set_intensity,
142 };
143
144 static struct backlight_device *corgi_backlight_device;
145
146 static int __init corgibl_probe(struct device *dev)
147 {
148         struct corgibl_machinfo *machinfo = dev->platform_data;
149
150         corgibl_data.max_brightness = machinfo->max_intensity;
151         corgibl_mach_set_intensity = machinfo->set_bl_intensity;
152
153         corgi_backlight_device = backlight_device_register ("corgi-bl",
154                 NULL, &corgibl_data);
155         if (IS_ERR (corgi_backlight_device))
156                 return PTR_ERR (corgi_backlight_device);
157
158         corgibl_set_intensity(NULL, CORGI_DEFAULT_INTENSITY);
159         corgibl_limit_intensity(0);
160
161         printk("Corgi Backlight Driver Initialized.\n");
162         return 0;
163 }
164
165 static int corgibl_remove(struct device *dev)
166 {
167         backlight_device_unregister(corgi_backlight_device);
168
169         corgibl_set_intensity(NULL, 0);
170
171         printk("Corgi Backlight Driver Unloaded\n");
172         return 0;
173 }
174
175 static struct device_driver corgibl_driver = {
176         .name           = "corgi-bl",
177         .bus            = &platform_bus_type,
178         .probe          = corgibl_probe,
179         .remove         = corgibl_remove,
180         .suspend        = corgibl_suspend,
181         .resume         = corgibl_resume,
182 };
183
184 static int __init corgibl_init(void)
185 {
186         return driver_register(&corgibl_driver);
187 }
188
189 static void __exit corgibl_exit(void)
190 {
191         driver_unregister(&corgibl_driver);
192 }
193
194 module_init(corgibl_init);
195 module_exit(corgibl_exit);
196
197 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
198 MODULE_DESCRIPTION("Corgi Backlight Driver");
199 MODULE_LICENSE("GPLv2");