2 * Battery and Power Management code for the Sharp SL-C7xx
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/corgi.h>
27 #include <asm/arch/pxa-regs.h>
30 static void corgi_charger_init(void)
32 pxa_gpio_mode(CORGI_GPIO_ADC_TEMP_ON | GPIO_OUT);
33 pxa_gpio_mode(CORGI_GPIO_CHRG_ON | GPIO_OUT);
34 pxa_gpio_mode(CORGI_GPIO_CHRG_UKN | GPIO_OUT);
35 pxa_gpio_mode(CORGI_GPIO_KEY_INT | GPIO_IN);
36 sharpsl_pm_pxa_init();
39 static void corgi_measure_temp(int on)
42 GPSR(CORGI_GPIO_ADC_TEMP_ON) = GPIO_bit(CORGI_GPIO_ADC_TEMP_ON);
44 GPCR(CORGI_GPIO_ADC_TEMP_ON) = GPIO_bit(CORGI_GPIO_ADC_TEMP_ON);
47 static void corgi_charge(int on)
50 if (machine_is_corgi() && (sharpsl_pm.flags & SHARPSL_SUSPENDED)) {
51 GPCR(CORGI_GPIO_CHRG_ON) = GPIO_bit(CORGI_GPIO_CHRG_ON);
52 GPSR(CORGI_GPIO_CHRG_UKN) = GPIO_bit(CORGI_GPIO_CHRG_UKN);
54 GPSR(CORGI_GPIO_CHRG_ON) = GPIO_bit(CORGI_GPIO_CHRG_ON);
55 GPCR(CORGI_GPIO_CHRG_UKN) = GPIO_bit(CORGI_GPIO_CHRG_UKN);
58 GPCR(CORGI_GPIO_CHRG_ON) = GPIO_bit(CORGI_GPIO_CHRG_ON);
59 GPCR(CORGI_GPIO_CHRG_UKN) = GPIO_bit(CORGI_GPIO_CHRG_UKN);
63 static void corgi_discharge(int on)
66 GPSR(CORGI_GPIO_DISCHARGE_ON) = GPIO_bit(CORGI_GPIO_DISCHARGE_ON);
68 GPCR(CORGI_GPIO_DISCHARGE_ON) = GPIO_bit(CORGI_GPIO_DISCHARGE_ON);
71 static void corgi_presuspend(void)
74 unsigned long wakeup_mask;
76 /* charging , so CHARGE_ON bit is HIGH during OFF. */
77 if (READ_GPIO_BIT(CORGI_GPIO_CHRG_ON))
78 PGSR1 |= GPIO_bit(CORGI_GPIO_CHRG_ON);
80 PGSR1 &= ~GPIO_bit(CORGI_GPIO_CHRG_ON);
82 if (READ_GPIO_BIT(CORGI_GPIO_LED_ORANGE))
83 PGSR0 |= GPIO_bit(CORGI_GPIO_LED_ORANGE);
85 PGSR0 &= ~GPIO_bit(CORGI_GPIO_LED_ORANGE);
87 if (READ_GPIO_BIT(CORGI_GPIO_CHRG_UKN))
88 PGSR1 |= GPIO_bit(CORGI_GPIO_CHRG_UKN);
90 PGSR1 &= ~GPIO_bit(CORGI_GPIO_CHRG_UKN);
92 /* Resume on keyboard power key */
93 PGSR2 = (PGSR2 & ~CORGI_GPIO_ALL_STROBE_BIT) | CORGI_GPIO_STROBE_BIT(0);
95 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);
97 if (!machine_is_corgi())
98 wakeup_mask |= GPIO_bit(CORGI_GPIO_MAIN_BAT_LOW);
100 PWER = wakeup_mask | PWER_RTC;
104 for (i = 0; i <=15; i++) {
105 if (PRER & PFER & GPIO_bit(i)) {
106 if (GPLR0 & GPIO_bit(i) )
107 PRER &= ~GPIO_bit(i);
109 PFER &= ~GPIO_bit(i);
114 static void corgi_postsuspend(void)
119 * Check what brought us out of the suspend.
120 * Return: 0 to sleep, otherwise wake
122 static int corgi_should_wakeup(unsigned int resume_on_alarm)
126 dev_dbg(sharpsl_pm.dev, "GPLR0 = %x,%x\n", GPLR0, PEDR);
128 if ((PEDR & GPIO_bit(CORGI_GPIO_AC_IN))) {
129 if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
131 dev_dbg(sharpsl_pm.dev, "ac insert\n");
132 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
135 dev_dbg(sharpsl_pm.dev, "ac remove\n");
136 sharpsl_pm_led(SHARPSL_LED_OFF);
137 sharpsl_pm.machinfo->charge(0);
138 sharpsl_pm.charge_mode = CHRG_OFF;
142 if ((PEDR & GPIO_bit(CORGI_GPIO_CHRG_FULL)))
143 dev_dbg(sharpsl_pm.dev, "Charge full interrupt\n");
145 if (PEDR & GPIO_bit(CORGI_GPIO_KEY_INT))
146 is_resume |= GPIO_bit(CORGI_GPIO_KEY_INT);
148 if (PEDR & GPIO_bit(CORGI_GPIO_WAKEUP))
149 is_resume |= GPIO_bit(CORGI_GPIO_WAKEUP);
151 if (resume_on_alarm && (PEDR & PWER_RTC))
152 is_resume |= PWER_RTC;
154 dev_dbg(sharpsl_pm.dev, "is_resume: %x\n",is_resume);
158 static unsigned long corgi_charger_wakeup(void)
160 return ~GPLR0 & ( GPIO_bit(CORGI_GPIO_AC_IN) | GPIO_bit(CORGI_GPIO_KEY_INT) | GPIO_bit(CORGI_GPIO_WAKEUP) );
163 unsigned long corgipm_read_devdata(int type)
166 case SHARPSL_STATUS_ACIN:
167 return ((GPLR(CORGI_GPIO_AC_IN) & GPIO_bit(CORGI_GPIO_AC_IN)) != 0);
168 case SHARPSL_STATUS_LOCK:
169 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batlock);
170 case SHARPSL_STATUS_CHRGFULL:
171 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batfull);
172 case SHARPSL_STATUS_FATAL:
173 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_fatal);
174 case SHARPSL_ACIN_VOLT:
175 return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT);
176 case SHARPSL_BATT_TEMP:
177 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP);
178 case SHARPSL_BATT_VOLT:
180 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
184 static struct sharpsl_charger_machinfo corgi_pm_machinfo = {
185 .init = corgi_charger_init,
186 .exit = sharpsl_pm_pxa_remove,
187 .gpio_batlock = CORGI_GPIO_BAT_COVER,
188 .gpio_acin = CORGI_GPIO_AC_IN,
189 .gpio_batfull = CORGI_GPIO_CHRG_FULL,
190 .discharge = corgi_discharge,
191 .charge = corgi_charge,
192 .measure_temp = corgi_measure_temp,
193 .presuspend = corgi_presuspend,
194 .postsuspend = corgi_postsuspend,
195 .read_devdata = corgipm_read_devdata,
196 .charger_wakeup = corgi_charger_wakeup,
197 .should_wakeup = corgi_should_wakeup,
199 .bat_levels_noac = spitz_battery_levels_noac,
200 .bat_levels_acin = spitz_battery_levels_acin,
201 .status_high_acin = 188,
202 .status_low_acin = 178,
203 .status_high_noac = 185,
204 .status_low_noac = 175,
207 static struct platform_device *corgipm_device;
209 static int __devinit corgipm_init(void)
213 corgipm_device = platform_device_alloc("sharpsl-pm", -1);
217 corgipm_device->dev.platform_data = &corgi_pm_machinfo;
218 ret = platform_device_add(corgipm_device);
221 platform_device_put(corgipm_device);
226 static void corgipm_exit(void)
228 platform_device_unregister(corgipm_device);
231 module_init(corgipm_init);
232 module_exit(corgipm_exit);