2 * Battery and Power Management code for the Sharp SL-Cxx00
4 * Copyright (c) 2005 Richard Purdie
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.
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-emulation.h>
21 #include <asm/mach-types.h>
22 #include <asm/hardware.h>
23 #include <asm/hardware/scoop.h>
25 #include <asm/arch/sharpsl.h>
26 #include <asm/arch/spitz.h>
27 #include <asm/arch/pxa-regs.h>
30 #define SHARPSL_CHARGE_ON_VOLT 0x99 /* 2.9V */
31 #define SHARPSL_CHARGE_ON_TEMP 0xe0 /* 2.9V */
32 #define SHARPSL_CHARGE_ON_ACIN_HIGH 0x9b /* 6V */
33 #define SHARPSL_CHARGE_ON_ACIN_LOW 0x34 /* 2V */
34 #define SHARPSL_FATAL_ACIN_VOLT 182 /* 3.45V */
35 #define SHARPSL_FATAL_NOACIN_VOLT 170 /* 3.40V */
37 static int spitz_last_ac_status;
39 static void spitz_charger_init(void)
41 pxa_gpio_mode(SPITZ_GPIO_KEY_INT | GPIO_IN);
42 pxa_gpio_mode(SPITZ_GPIO_SYNC | GPIO_IN);
43 sharpsl_pm_pxa_init();
46 static void spitz_measure_temp(int on)
49 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_ADC_TEMP_ON);
51 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_ADC_TEMP_ON);
54 static void spitz_charge(int on)
57 if (sharpsl_pm.flags & SHARPSL_SUSPENDED) {
58 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_B);
59 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_CHRG_ON);
61 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_B);
62 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_CHRG_ON);
65 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_B);
66 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_CHRG_ON);
70 static void spitz_discharge(int on)
73 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_A);
75 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_A);
78 /* HACK - For unknown reasons, accurate voltage readings are only made with a load
79 on the power bus which the green led on spitz provides */
80 static void spitz_discharge1(int on)
83 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_LED_GREEN);
85 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_LED_GREEN);
88 static void spitz_presuspend(void)
90 spitz_last_ac_status = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
92 /* GPIO Sleep Register */
95 if (machine_is_akita()) {
103 PGSR0 &= ~SPITZ_GPIO_G0_STROBE_BIT;
104 PGSR1 &= ~SPITZ_GPIO_G1_STROBE_BIT;
105 PGSR2 &= ~SPITZ_GPIO_G2_STROBE_BIT;
106 PGSR3 &= ~SPITZ_GPIO_G3_STROBE_BIT;
107 PGSR2 |= GPIO_bit(SPITZ_GPIO_KEY_STROBE0);
109 pxa_gpio_mode(GPIO18_RDY|GPIO_OUT | GPIO_DFLT_HIGH);
111 PRER = GPIO_bit(SPITZ_GPIO_KEY_INT);
112 PFER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET);
113 PWER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET) | PWER_RTC;
114 PKWR = GPIO_bit(SPITZ_GPIO_SYNC) | GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET);
115 PKSR = 0xffffffff; // clear
117 /* nRESET_OUT Disable */
120 /* Clear reset status */
121 RCSR = RCSR_HWR | RCSR_WDR | RCSR_SMR | RCSR_GPR;
123 /* Stop 3.6MHz and drive HIGH to PCMCIA and CS */
124 PCFR = PCFR_GPR_EN | PCFR_OPDE;
127 static void spitz_postsuspend(void)
129 pxa_gpio_mode(GPIO18_RDY_MD);
130 pxa_gpio_mode(10 | GPIO_IN);
133 static int spitz_should_wakeup(unsigned int resume_on_alarm)
136 int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
138 if (spitz_last_ac_status != acin) {
141 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
142 dev_dbg(sharpsl_pm.dev, "AC Inserted\n");
145 dev_dbg(sharpsl_pm.dev, "AC Removed\n");
146 sharpsl_pm_led(SHARPSL_LED_OFF);
147 sharpsl_pm.machinfo->charge(0);
148 sharpsl_pm.charge_mode = CHRG_OFF;
150 spitz_last_ac_status = acin;
151 /* Return to suspend as this must be what we were woken for */
155 if (PEDR & GPIO_bit(SPITZ_GPIO_KEY_INT))
156 is_resume |= GPIO_bit(SPITZ_GPIO_KEY_INT);
158 if (PKSR & GPIO_bit(SPITZ_GPIO_SYNC))
159 is_resume |= GPIO_bit(SPITZ_GPIO_SYNC);
161 if (resume_on_alarm && (PEDR & PWER_RTC))
162 is_resume |= PWER_RTC;
164 dev_dbg(sharpsl_pm.dev, "is_resume: %x\n",is_resume);
168 static unsigned long spitz_charger_wakeup(void)
170 return (~GPLR0 & GPIO_bit(SPITZ_GPIO_KEY_INT)) | (GPLR0 & GPIO_bit(SPITZ_GPIO_SYNC));
173 unsigned long spitzpm_read_devdata(int type)
176 case SHARPSL_STATUS_ACIN:
177 return (((~GPLR(SPITZ_GPIO_AC_IN)) & GPIO_bit(SPITZ_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:
190 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
194 struct sharpsl_charger_machinfo spitz_pm_machinfo = {
195 .init = spitz_charger_init,
196 .exit = sharpsl_pm_pxa_remove,
197 .gpio_batlock = SPITZ_GPIO_BAT_COVER,
198 .gpio_acin = SPITZ_GPIO_AC_IN,
199 .gpio_batfull = SPITZ_GPIO_CHRG_FULL,
201 .gpio_fatal = SPITZ_GPIO_FATAL_BAT,
202 .discharge = spitz_discharge,
203 .discharge1 = spitz_discharge1,
204 .charge = spitz_charge,
205 .measure_temp = spitz_measure_temp,
206 .presuspend = spitz_presuspend,
207 .postsuspend = spitz_postsuspend,
208 .read_devdata = spitzpm_read_devdata,
209 .charger_wakeup = spitz_charger_wakeup,
210 .should_wakeup = spitz_should_wakeup,
211 .backlight_limit = corgibl_limit_intensity,
212 .charge_on_volt = SHARPSL_CHARGE_ON_VOLT,
213 .charge_on_temp = SHARPSL_CHARGE_ON_TEMP,
214 .charge_acin_high = SHARPSL_CHARGE_ON_ACIN_HIGH,
215 .charge_acin_low = SHARPSL_CHARGE_ON_ACIN_LOW,
216 .fatal_acin_volt = SHARPSL_FATAL_ACIN_VOLT,
217 .fatal_noacin_volt= SHARPSL_FATAL_NOACIN_VOLT,
219 .bat_levels_noac = spitz_battery_levels_noac,
220 .bat_levels_acin = spitz_battery_levels_acin,
221 .status_high_acin = 188,
222 .status_low_acin = 178,
223 .status_high_noac = 185,
224 .status_low_noac = 175,
227 static struct platform_device *spitzpm_device;
229 static int __devinit spitzpm_init(void)
233 spitzpm_device = platform_device_alloc("sharpsl-pm", -1);
237 spitzpm_device->dev.platform_data = &spitz_pm_machinfo;
238 ret = platform_device_add(spitzpm_device);
241 platform_device_put(spitzpm_device);
246 static void spitzpm_exit(void)
248 platform_device_unregister(spitzpm_device);
251 module_init(spitzpm_init);
252 module_exit(spitzpm_exit);