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