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