Merge branch 'linus' into x86/i8259
[linux-2.6] / arch / arm / mach-pxa / spitz_pm.c
1 /*
2  * Battery and Power Management code for the Sharp SL-Cxx00
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 <asm/hardware.h>
24 #include <asm/hardware/scoop.h>
25
26 #include <asm/arch/sharpsl.h>
27 #include <asm/arch/spitz.h>
28 #include <asm/arch/pxa-regs.h>
29 #include <asm/arch/pxa2xx-gpio.h>
30 #include "sharpsl.h"
31
32 #define SHARPSL_CHARGE_ON_VOLT         0x99  /* 2.9V */
33 #define SHARPSL_CHARGE_ON_TEMP         0xe0  /* 2.9V */
34 #define SHARPSL_CHARGE_ON_ACIN_HIGH    0x9b  /* 6V */
35 #define SHARPSL_CHARGE_ON_ACIN_LOW     0x34  /* 2V */
36 #define SHARPSL_FATAL_ACIN_VOLT        182   /* 3.45V */
37 #define SHARPSL_FATAL_NOACIN_VOLT      170   /* 3.40V */
38
39 static int spitz_last_ac_status;
40
41 static void spitz_charger_init(void)
42 {
43         pxa_gpio_mode(SPITZ_GPIO_KEY_INT | GPIO_IN);
44         pxa_gpio_mode(SPITZ_GPIO_SYNC | GPIO_IN);
45         sharpsl_pm_pxa_init();
46 }
47
48 static void spitz_measure_temp(int on)
49 {
50         if (on)
51                 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_ADC_TEMP_ON);
52         else
53                 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_ADC_TEMP_ON);
54 }
55
56 static void spitz_charge(int on)
57 {
58         if (on) {
59                 if (sharpsl_pm.flags & SHARPSL_SUSPENDED) {
60                         set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_B);
61                         reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_CHRG_ON);
62                 } else {
63                         reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_B);
64                         reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_CHRG_ON);
65                 }
66         } else {
67                 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_B);
68                 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_CHRG_ON);
69         }
70 }
71
72 static void spitz_discharge(int on)
73 {
74         if (on)
75                 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_A);
76         else
77                 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_A);
78 }
79
80 /* HACK - For unknown reasons, accurate voltage readings are only made with a load
81    on the power bus which the green led on spitz provides */
82 static void spitz_discharge1(int on)
83 {
84         if (on)
85                 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_LED_GREEN);
86         else
87                 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_LED_GREEN);
88 }
89
90 static void spitz_presuspend(void)
91 {
92         spitz_last_ac_status = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
93
94         /* GPIO Sleep Register */
95         PGSR0 = 0x00144018;
96         PGSR1 = 0x00EF0000;
97         if (machine_is_akita()) {
98                 PGSR2 = 0x2121C000;
99                 PGSR3 = 0x00600400;
100         } else {
101                 PGSR2 = 0x0121C000;
102                 PGSR3 = 0x00600000;
103         }
104
105         PGSR0 &= ~SPITZ_GPIO_G0_STROBE_BIT;
106         PGSR1 &= ~SPITZ_GPIO_G1_STROBE_BIT;
107         PGSR2 &= ~SPITZ_GPIO_G2_STROBE_BIT;
108         PGSR3 &= ~SPITZ_GPIO_G3_STROBE_BIT;
109         PGSR2 |= GPIO_bit(SPITZ_GPIO_KEY_STROBE0);
110
111         pxa_gpio_mode(GPIO18_RDY|GPIO_OUT | GPIO_DFLT_HIGH);
112
113         PRER = GPIO_bit(SPITZ_GPIO_KEY_INT);
114         PFER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET);
115         PWER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET) | PWER_RTC;
116         PKWR = GPIO_bit(SPITZ_GPIO_SYNC) | GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET);
117         PKSR = 0xffffffff; // clear
118
119         /* nRESET_OUT Disable */
120         PSLR |= PSLR_SL_ROD;
121
122         /* Stop 3.6MHz and drive HIGH to PCMCIA and CS */
123         PCFR = PCFR_GPR_EN | PCFR_OPDE;
124 }
125
126 static void spitz_postsuspend(void)
127 {
128         pxa_gpio_mode(GPIO18_RDY_MD);
129         pxa_gpio_mode(10 | GPIO_IN);
130 }
131
132 static int spitz_should_wakeup(unsigned int resume_on_alarm)
133 {
134         int is_resume = 0;
135         int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
136
137         if (spitz_last_ac_status != acin) {
138                 if (acin) {
139                         /* charge on */
140                         sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
141                         dev_dbg(sharpsl_pm.dev, "AC Inserted\n");
142                 } else {
143                         /* charge off */
144                         dev_dbg(sharpsl_pm.dev, "AC Removed\n");
145                         sharpsl_pm_led(SHARPSL_LED_OFF);
146                         sharpsl_pm.machinfo->charge(0);
147                         sharpsl_pm.charge_mode = CHRG_OFF;
148                 }
149                 spitz_last_ac_status = acin;
150                 /* Return to suspend as this must be what we were woken for */
151                 return 0;
152         }
153
154         if (PEDR & GPIO_bit(SPITZ_GPIO_KEY_INT))
155                 is_resume |= GPIO_bit(SPITZ_GPIO_KEY_INT);
156
157         if (PKSR & GPIO_bit(SPITZ_GPIO_SYNC))
158                 is_resume |= GPIO_bit(SPITZ_GPIO_SYNC);
159
160         if (resume_on_alarm && (PEDR & PWER_RTC))
161                 is_resume |= PWER_RTC;
162
163         dev_dbg(sharpsl_pm.dev, "is_resume: %x\n",is_resume);
164         return is_resume;
165 }
166
167 static unsigned long spitz_charger_wakeup(void)
168 {
169         return (~GPLR0 & GPIO_bit(SPITZ_GPIO_KEY_INT)) | (GPLR0 & GPIO_bit(SPITZ_GPIO_SYNC));
170 }
171
172 unsigned long spitzpm_read_devdata(int type)
173 {
174         switch(type) {
175         case SHARPSL_STATUS_ACIN:
176                 return (((~GPLR(SPITZ_GPIO_AC_IN)) & GPIO_bit(SPITZ_GPIO_AC_IN)) != 0);
177         case SHARPSL_STATUS_LOCK:
178                 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batlock);
179         case SHARPSL_STATUS_CHRGFULL:
180                 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batfull);
181         case SHARPSL_STATUS_FATAL:
182                 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_fatal);
183         case SHARPSL_ACIN_VOLT:
184                 return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT);
185         case SHARPSL_BATT_TEMP:
186                 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP);
187         case SHARPSL_BATT_VOLT:
188         default:
189                 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
190         }
191 }
192
193 struct sharpsl_charger_machinfo spitz_pm_machinfo = {
194         .init             = spitz_charger_init,
195         .exit             = sharpsl_pm_pxa_remove,
196         .gpio_batlock     = SPITZ_GPIO_BAT_COVER,
197         .gpio_acin        = SPITZ_GPIO_AC_IN,
198         .gpio_batfull     = SPITZ_GPIO_CHRG_FULL,
199         .batfull_irq      = 1,
200         .gpio_fatal       = SPITZ_GPIO_FATAL_BAT,
201         .discharge        = spitz_discharge,
202         .discharge1       = spitz_discharge1,
203         .charge           = spitz_charge,
204         .measure_temp     = spitz_measure_temp,
205         .presuspend       = spitz_presuspend,
206         .postsuspend      = spitz_postsuspend,
207         .read_devdata     = spitzpm_read_devdata,
208         .charger_wakeup   = spitz_charger_wakeup,
209         .should_wakeup    = spitz_should_wakeup,
210         .backlight_limit  = corgibl_limit_intensity,
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 *spitzpm_device;
227
228 static int __devinit spitzpm_init(void)
229 {
230         int ret;
231
232         spitzpm_device = platform_device_alloc("sharpsl-pm", -1);
233         if (!spitzpm_device)
234                 return -ENOMEM;
235
236         spitzpm_device->dev.platform_data = &spitz_pm_machinfo;
237         ret = platform_device_add(spitzpm_device);
238
239         if (ret)
240                 platform_device_put(spitzpm_device);
241
242         return ret;
243 }
244
245 static void spitzpm_exit(void)
246 {
247         platform_device_unregister(spitzpm_device);
248 }
249
250 module_init(spitzpm_init);
251 module_exit(spitzpm_exit);