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