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