[ARM] 3228/1: SharpSL: Move PM code to arch/arm/common
[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/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 #include <asm/hardware/sharpsl_pm.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         corgi_kick_batt = symbol_get(sharpsl_battery_kick);
54         if (corgi_kick_batt) {
55                 corgi_kick_batt();
56                 symbol_put(sharpsl_battery_kick);
57         }
58 }
59
60 static void corgibl_blank(int blank)
61 {
62         switch(blank) {
63
64         case FB_BLANK_NORMAL:
65         case FB_BLANK_VSYNC_SUSPEND:
66         case FB_BLANK_HSYNC_SUSPEND:
67         case FB_BLANK_POWERDOWN:
68                 if (corgibl_powermode == FB_BLANK_UNBLANK) {
69                         corgibl_send_intensity(0);
70                         corgibl_powermode = blank;
71                 }
72                 break;
73         case FB_BLANK_UNBLANK:
74                 if (corgibl_powermode != FB_BLANK_UNBLANK) {
75                         corgibl_powermode = blank;
76                         corgibl_send_intensity(current_intensity);
77                 }
78                 break;
79         }
80 }
81
82 #ifdef CONFIG_PM
83 static int corgibl_suspend(struct platform_device *dev, pm_message_t state)
84 {
85         corgibl_blank(FB_BLANK_POWERDOWN);
86         return 0;
87 }
88
89 static int corgibl_resume(struct platform_device *dev)
90 {
91         corgibl_blank(FB_BLANK_UNBLANK);
92         return 0;
93 }
94 #else
95 #define corgibl_suspend NULL
96 #define corgibl_resume  NULL
97 #endif
98
99
100 static int corgibl_set_power(struct backlight_device *bd, int state)
101 {
102         corgibl_blank(state);
103         return 0;
104 }
105
106 static int corgibl_get_power(struct backlight_device *bd)
107 {
108         return corgibl_powermode;
109 }
110
111 static int corgibl_set_intensity(struct backlight_device *bd, int intensity)
112 {
113         if (intensity > corgibl_data.max_brightness)
114                 intensity = corgibl_data.max_brightness;
115         corgibl_send_intensity(intensity);
116         current_intensity=intensity;
117         return 0;
118 }
119
120 static int corgibl_get_intensity(struct backlight_device *bd)
121 {
122         return current_intensity;
123 }
124
125 /*
126  * Called when the battery is low to limit the backlight intensity.
127  * If limit==0 clear any limit, otherwise limit the intensity
128  */
129 void corgibl_limit_intensity(int limit)
130 {
131         corgibl_limit = (limit ? 1 : 0);
132         corgibl_send_intensity(current_intensity);
133 }
134 EXPORT_SYMBOL(corgibl_limit_intensity);
135
136
137 static struct backlight_properties corgibl_data = {
138         .owner          = THIS_MODULE,
139         .get_power      = corgibl_get_power,
140         .set_power      = corgibl_set_power,
141         .get_brightness = corgibl_get_intensity,
142         .set_brightness = corgibl_set_intensity,
143 };
144
145 static struct backlight_device *corgi_backlight_device;
146
147 static int __init corgibl_probe(struct platform_device *pdev)
148 {
149         struct corgibl_machinfo *machinfo = pdev->dev.platform_data;
150
151         corgibl_data.max_brightness = machinfo->max_intensity;
152         corgibl_mach_set_intensity = machinfo->set_bl_intensity;
153
154         corgi_backlight_device = backlight_device_register ("corgi-bl",
155                 NULL, &corgibl_data);
156         if (IS_ERR (corgi_backlight_device))
157                 return PTR_ERR (corgi_backlight_device);
158
159         corgibl_set_intensity(NULL, CORGI_DEFAULT_INTENSITY);
160         corgibl_limit_intensity(0);
161
162         printk("Corgi Backlight Driver Initialized.\n");
163         return 0;
164 }
165
166 static int corgibl_remove(struct platform_device *dev)
167 {
168         backlight_device_unregister(corgi_backlight_device);
169
170         corgibl_set_intensity(NULL, 0);
171
172         printk("Corgi Backlight Driver Unloaded\n");
173         return 0;
174 }
175
176 static struct platform_driver corgibl_driver = {
177         .probe          = corgibl_probe,
178         .remove         = corgibl_remove,
179         .suspend        = corgibl_suspend,
180         .resume         = corgibl_resume,
181         .driver         = {
182                 .name   = "corgi-bl",
183         },
184 };
185
186 static int __init corgibl_init(void)
187 {
188         return platform_driver_register(&corgibl_driver);
189 }
190
191 static void __exit corgibl_exit(void)
192 {
193         platform_driver_unregister(&corgibl_driver);
194 }
195
196 module_init(corgibl_init);
197 module_exit(corgibl_exit);
198
199 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
200 MODULE_DESCRIPTION("Corgi Backlight Driver");
201 MODULE_LICENSE("GPLv2");