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>
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 static int spitz_last_ac_status;
32 static void spitz_charger_init(void)
34 pxa_gpio_mode(SPITZ_GPIO_KEY_INT | GPIO_IN);
35 pxa_gpio_mode(SPITZ_GPIO_SYNC | GPIO_IN);
36 sharpsl_pm_pxa_init();
39 static void spitz_measure_temp(int on)
42 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_ADC_TEMP_ON);
44 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_ADC_TEMP_ON);
47 static void spitz_charge(int on)
50 if (sharpsl_pm.flags & SHARPSL_SUSPENDED) {
51 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_B);
52 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_CHRG_ON);
54 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_B);
55 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_CHRG_ON);
58 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_B);
59 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_CHRG_ON);
63 static void spitz_discharge(int on)
66 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_A);
68 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_JK_A);
71 /* HACK - For unknown reasons, accurate voltage readings are only made with a load
72 on the power bus which the green led on spitz provides */
73 static void spitz_discharge1(int on)
76 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_LED_GREEN);
78 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_LED_GREEN);
81 static void spitz_presuspend(void)
83 spitz_last_ac_status = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
85 /* GPIO Sleep Register */
88 if (machine_is_akita()) {
96 PGSR0 &= ~SPITZ_GPIO_G0_STROBE_BIT;
97 PGSR1 &= ~SPITZ_GPIO_G1_STROBE_BIT;
98 PGSR2 &= ~SPITZ_GPIO_G2_STROBE_BIT;
99 PGSR3 &= ~SPITZ_GPIO_G3_STROBE_BIT;
100 PGSR2 |= GPIO_bit(SPITZ_GPIO_KEY_STROBE0);
102 pxa_gpio_mode(GPIO18_RDY|GPIO_OUT | GPIO_DFLT_HIGH);
104 PRER = GPIO_bit(SPITZ_GPIO_KEY_INT);
105 PFER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET);
106 PWER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET) | PWER_RTC;
107 PKWR = GPIO_bit(SPITZ_GPIO_SYNC) | GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET);
108 PKSR = 0xffffffff; // clear
110 /* nRESET_OUT Disable */
113 /* Clear reset status */
114 RCSR = RCSR_HWR | RCSR_WDR | RCSR_SMR | RCSR_GPR;
116 /* Stop 3.6MHz and drive HIGH to PCMCIA and CS */
117 PCFR = PCFR_GPR_EN | PCFR_OPDE;
120 static void spitz_postsuspend(void)
122 pxa_gpio_mode(GPIO18_RDY_MD);
123 pxa_gpio_mode(10 | GPIO_IN);
126 static int spitz_should_wakeup(unsigned int resume_on_alarm)
129 int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
131 if (spitz_last_ac_status != acin) {
134 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
135 dev_dbg(sharpsl_pm.dev, "AC Inserted\n");
138 dev_dbg(sharpsl_pm.dev, "AC Removed\n");
139 sharpsl_pm_led(SHARPSL_LED_OFF);
140 sharpsl_pm.machinfo->charge(0);
141 sharpsl_pm.charge_mode = CHRG_OFF;
143 spitz_last_ac_status = acin;
144 /* Return to suspend as this must be what we were woken for */
148 if (PEDR & GPIO_bit(SPITZ_GPIO_KEY_INT))
149 is_resume |= GPIO_bit(SPITZ_GPIO_KEY_INT);
151 if (PKSR & GPIO_bit(SPITZ_GPIO_SYNC))
152 is_resume |= GPIO_bit(SPITZ_GPIO_SYNC);
154 if (resume_on_alarm && (PEDR & PWER_RTC))
155 is_resume |= PWER_RTC;
157 dev_dbg(sharpsl_pm.dev, "is_resume: %x\n",is_resume);
161 static unsigned long spitz_charger_wakeup(void)
163 return (~GPLR0 & GPIO_bit(SPITZ_GPIO_KEY_INT)) | (GPLR0 & GPIO_bit(SPITZ_GPIO_SYNC));
166 unsigned long spitzpm_read_devdata(int type)
169 case SHARPSL_STATUS_ACIN:
170 return (((~GPLR(SPITZ_GPIO_AC_IN)) & GPIO_bit(SPITZ_GPIO_AC_IN)) != 0);
171 case SHARPSL_STATUS_LOCK:
172 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batlock);
173 case SHARPSL_STATUS_CHRGFULL:
174 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batfull);
175 case SHARPSL_STATUS_FATAL:
176 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_fatal);
177 case SHARPSL_ACIN_VOLT:
178 return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT);
179 case SHARPSL_BATT_TEMP:
180 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP);
181 case SHARPSL_BATT_VOLT:
183 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
187 struct sharpsl_charger_machinfo spitz_pm_machinfo = {
188 .init = spitz_charger_init,
189 .exit = sharpsl_pm_pxa_remove,
190 .gpio_batlock = SPITZ_GPIO_BAT_COVER,
191 .gpio_acin = SPITZ_GPIO_AC_IN,
192 .gpio_batfull = SPITZ_GPIO_CHRG_FULL,
193 .gpio_fatal = SPITZ_GPIO_FATAL_BAT,
194 .discharge = spitz_discharge,
195 .discharge1 = spitz_discharge1,
196 .charge = spitz_charge,
197 .measure_temp = spitz_measure_temp,
198 .presuspend = spitz_presuspend,
199 .postsuspend = spitz_postsuspend,
200 .read_devdata = spitzpm_read_devdata,
201 .charger_wakeup = spitz_charger_wakeup,
202 .should_wakeup = spitz_should_wakeup,
204 .bat_levels_noac = spitz_battery_levels_noac,
205 .bat_levels_acin = spitz_battery_levels_acin,
206 .status_high_acin = 188,
207 .status_low_acin = 178,
208 .status_high_noac = 185,
209 .status_low_noac = 175,
212 static struct platform_device *spitzpm_device;
214 static int __devinit spitzpm_init(void)
218 spitzpm_device = platform_device_alloc("sharpsl-pm", -1);
222 spitzpm_device->dev.platform_data = &spitz_pm_machinfo;
223 ret = platform_device_add(spitzpm_device);
226 platform_device_put(spitzpm_device);
231 static void spitzpm_exit(void)
233 platform_device_unregister(spitzpm_device);
236 module_init(spitzpm_init);
237 module_exit(spitzpm_exit);