This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / arm / mach-pxa / corgi_pm.c
1 /*
2  * Battery and Power Management code for the Sharp SL-C7xx
3  *
4  * Copyright (c) 2005 Richard Purdie
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  */
11
12 #include <linux/module.h>
13 #include <linux/stat.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/delay.h>
17 #include <linux/interrupt.h>
18 #include <linux/platform_device.h>
19 #include <asm/apm.h>
20 #include <asm/irq.h>
21 #include <asm/mach-types.h>
22 #include <asm/hardware.h>
23 #include <asm/hardware/scoop.h>
24
25 #include <asm/arch/sharpsl.h>
26 #include <asm/arch/corgi.h>
27 #include <asm/arch/pxa-regs.h>
28 #include "sharpsl.h"
29
30 static void corgi_charger_init(void)
31 {
32         pxa_gpio_mode(CORGI_GPIO_ADC_TEMP_ON | GPIO_OUT);
33         pxa_gpio_mode(CORGI_GPIO_CHRG_ON | GPIO_OUT);
34         pxa_gpio_mode(CORGI_GPIO_CHRG_UKN | GPIO_OUT);
35         pxa_gpio_mode(CORGI_GPIO_KEY_INT | GPIO_IN);
36         sharpsl_pm_pxa_init();
37 }
38
39 static void corgi_measure_temp(int on)
40 {
41         if (on)
42                 GPSR(CORGI_GPIO_ADC_TEMP_ON) = GPIO_bit(CORGI_GPIO_ADC_TEMP_ON);
43         else
44                 GPCR(CORGI_GPIO_ADC_TEMP_ON) = GPIO_bit(CORGI_GPIO_ADC_TEMP_ON);
45 }
46
47 static void corgi_charge(int on)
48 {
49         if (on) {
50                 if (machine_is_corgi() && (sharpsl_pm.flags & SHARPSL_SUSPENDED)) {
51                         GPCR(CORGI_GPIO_CHRG_ON) = GPIO_bit(CORGI_GPIO_CHRG_ON);
52                         GPSR(CORGI_GPIO_CHRG_UKN) = GPIO_bit(CORGI_GPIO_CHRG_UKN);
53                 } else {
54                         GPSR(CORGI_GPIO_CHRG_ON) = GPIO_bit(CORGI_GPIO_CHRG_ON);
55                         GPCR(CORGI_GPIO_CHRG_UKN) = GPIO_bit(CORGI_GPIO_CHRG_UKN);
56                 }
57         } else {
58                 GPCR(CORGI_GPIO_CHRG_ON) = GPIO_bit(CORGI_GPIO_CHRG_ON);
59                 GPCR(CORGI_GPIO_CHRG_UKN) = GPIO_bit(CORGI_GPIO_CHRG_UKN);
60         }
61 }
62
63 static void corgi_discharge(int on)
64 {
65         if (on)
66                 GPSR(CORGI_GPIO_DISCHARGE_ON) = GPIO_bit(CORGI_GPIO_DISCHARGE_ON);
67         else
68                 GPCR(CORGI_GPIO_DISCHARGE_ON) = GPIO_bit(CORGI_GPIO_DISCHARGE_ON);
69 }
70
71 static void corgi_presuspend(void)
72 {
73         int i;
74         unsigned long wakeup_mask;
75
76         /* charging , so CHARGE_ON bit is HIGH during OFF. */
77         if (READ_GPIO_BIT(CORGI_GPIO_CHRG_ON))
78                 PGSR1 |= GPIO_bit(CORGI_GPIO_CHRG_ON);
79         else
80                 PGSR1 &= ~GPIO_bit(CORGI_GPIO_CHRG_ON);
81
82         if (READ_GPIO_BIT(CORGI_GPIO_LED_ORANGE))
83                 PGSR0 |= GPIO_bit(CORGI_GPIO_LED_ORANGE);
84         else
85                 PGSR0 &= ~GPIO_bit(CORGI_GPIO_LED_ORANGE);
86
87         if (READ_GPIO_BIT(CORGI_GPIO_CHRG_UKN))
88                 PGSR1 |= GPIO_bit(CORGI_GPIO_CHRG_UKN);
89         else
90                 PGSR1 &= ~GPIO_bit(CORGI_GPIO_CHRG_UKN);
91
92         /* Resume on keyboard power key */
93         PGSR2 = (PGSR2 & ~CORGI_GPIO_ALL_STROBE_BIT) | CORGI_GPIO_STROBE_BIT(0);
94
95         wakeup_mask = GPIO_bit(CORGI_GPIO_KEY_INT) | GPIO_bit(CORGI_GPIO_WAKEUP) | GPIO_bit(CORGI_GPIO_AC_IN) | GPIO_bit(CORGI_GPIO_CHRG_FULL);
96
97         if (!machine_is_corgi())
98                 wakeup_mask |= GPIO_bit(CORGI_GPIO_MAIN_BAT_LOW);
99
100         PWER = wakeup_mask | PWER_RTC;
101         PRER = wakeup_mask;
102         PFER = wakeup_mask;
103
104         for (i = 0; i <=15; i++) {
105                 if (PRER & PFER & GPIO_bit(i)) {
106                         if (GPLR0 & GPIO_bit(i) )
107                                 PRER &= ~GPIO_bit(i);
108                         else
109                                 PFER &= ~GPIO_bit(i);
110                 }
111         }
112 }
113
114 static void corgi_postsuspend(void)
115 {
116 }
117
118 /*
119  * Check what brought us out of the suspend.
120  * Return: 0 to sleep, otherwise wake
121  */
122 static int corgi_should_wakeup(unsigned int resume_on_alarm)
123 {
124         int is_resume = 0;
125
126         dev_dbg(sharpsl_pm.dev, "GPLR0 = %x,%x\n", GPLR0, PEDR);
127
128         if ((PEDR & GPIO_bit(CORGI_GPIO_AC_IN))) {
129                 if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
130                         /* charge on */
131                         dev_dbg(sharpsl_pm.dev, "ac insert\n");
132                         sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
133                 } else {
134                         /* charge off */
135                         dev_dbg(sharpsl_pm.dev, "ac remove\n");
136                         sharpsl_pm_led(SHARPSL_LED_OFF);
137                         sharpsl_pm.machinfo->charge(0);
138                         sharpsl_pm.charge_mode = CHRG_OFF;
139                 }
140         }
141
142         if ((PEDR & GPIO_bit(CORGI_GPIO_CHRG_FULL)))
143                 dev_dbg(sharpsl_pm.dev, "Charge full interrupt\n");
144
145         if (PEDR & GPIO_bit(CORGI_GPIO_KEY_INT))
146                 is_resume |= GPIO_bit(CORGI_GPIO_KEY_INT);
147
148         if (PEDR & GPIO_bit(CORGI_GPIO_WAKEUP))
149                 is_resume |= GPIO_bit(CORGI_GPIO_WAKEUP);
150
151         if (resume_on_alarm && (PEDR & PWER_RTC))
152                 is_resume |= PWER_RTC;
153
154         dev_dbg(sharpsl_pm.dev, "is_resume: %x\n",is_resume);
155         return is_resume;
156 }
157
158 static unsigned long corgi_charger_wakeup(void)
159 {
160         return ~GPLR0 & ( GPIO_bit(CORGI_GPIO_AC_IN) | GPIO_bit(CORGI_GPIO_KEY_INT) | GPIO_bit(CORGI_GPIO_WAKEUP) );
161 }
162
163 unsigned long corgipm_read_devdata(int type)
164 {
165         switch(type) {
166         case SHARPSL_STATUS_ACIN:
167                 return ((GPLR(CORGI_GPIO_AC_IN) & GPIO_bit(CORGI_GPIO_AC_IN)) != 0);
168         case SHARPSL_STATUS_LOCK:
169                 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batlock);
170         case SHARPSL_STATUS_CHRGFULL:
171                 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batfull);
172         case SHARPSL_STATUS_FATAL:
173                 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_fatal);
174         case SHARPSL_ACIN_VOLT:
175                 return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT);
176         case SHARPSL_BATT_TEMP:
177                 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP);
178         case SHARPSL_BATT_VOLT:
179         default:
180                 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
181         }
182 }
183
184 static struct sharpsl_charger_machinfo corgi_pm_machinfo = {
185         .init            = corgi_charger_init,
186         .exit            = sharpsl_pm_pxa_remove,
187         .gpio_batlock    = CORGI_GPIO_BAT_COVER,
188         .gpio_acin       = CORGI_GPIO_AC_IN,
189         .gpio_batfull    = CORGI_GPIO_CHRG_FULL,
190         .discharge       = corgi_discharge,
191         .charge          = corgi_charge,
192         .measure_temp    = corgi_measure_temp,
193         .presuspend      = corgi_presuspend,
194         .postsuspend     = corgi_postsuspend,
195         .read_devdata    = corgipm_read_devdata,
196         .charger_wakeup  = corgi_charger_wakeup,
197         .should_wakeup   = corgi_should_wakeup,
198         .bat_levels      = 40,
199         .bat_levels_noac = spitz_battery_levels_noac,
200         .bat_levels_acin = spitz_battery_levels_acin,
201         .status_high_acin = 188,
202         .status_low_acin  = 178,
203         .status_high_noac = 185,
204         .status_low_noac  = 175,
205 };
206
207 static struct platform_device *corgipm_device;
208
209 static int __devinit corgipm_init(void)
210 {
211         int ret;
212
213         corgipm_device = platform_device_alloc("sharpsl-pm", -1);
214         if (!corgipm_device)
215                 return -ENOMEM;
216
217         corgipm_device->dev.platform_data = &corgi_pm_machinfo;
218         ret = platform_device_add(corgipm_device);
219
220         if (ret)
221                 platform_device_put(corgipm_device);
222
223         return ret;
224 }
225
226 static void corgipm_exit(void)
227 {
228         platform_device_unregister(corgipm_device);
229 }
230
231 module_init(corgipm_init);
232 module_exit(corgipm_exit);