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>
24 #include <asm/hardware/sharpsl_pm.h>
26 #include <asm/arch/sharpsl.h>
27 #include <asm/arch/corgi.h>
28 #include <asm/arch/pxa-regs.h>
31 static void corgi_charger_init(void)
33 pxa_gpio_mode(CORGI_GPIO_ADC_TEMP_ON | GPIO_OUT);
34 pxa_gpio_mode(CORGI_GPIO_CHRG_ON | GPIO_OUT);
35 pxa_gpio_mode(CORGI_GPIO_CHRG_UKN | GPIO_OUT);
36 pxa_gpio_mode(CORGI_GPIO_KEY_INT | GPIO_IN);
37 sharpsl_pm_pxa_init();
40 static void corgi_measure_temp(int on)
43 GPSR(CORGI_GPIO_ADC_TEMP_ON) = GPIO_bit(CORGI_GPIO_ADC_TEMP_ON);
45 GPCR(CORGI_GPIO_ADC_TEMP_ON) = GPIO_bit(CORGI_GPIO_ADC_TEMP_ON);
48 static void corgi_charge(int on)
51 if (machine_is_corgi() && (sharpsl_pm.flags & SHARPSL_SUSPENDED)) {
52 GPCR(CORGI_GPIO_CHRG_ON) = GPIO_bit(CORGI_GPIO_CHRG_ON);
53 GPSR(CORGI_GPIO_CHRG_UKN) = GPIO_bit(CORGI_GPIO_CHRG_UKN);
55 GPSR(CORGI_GPIO_CHRG_ON) = GPIO_bit(CORGI_GPIO_CHRG_ON);
56 GPCR(CORGI_GPIO_CHRG_UKN) = GPIO_bit(CORGI_GPIO_CHRG_UKN);
59 GPCR(CORGI_GPIO_CHRG_ON) = GPIO_bit(CORGI_GPIO_CHRG_ON);
60 GPCR(CORGI_GPIO_CHRG_UKN) = GPIO_bit(CORGI_GPIO_CHRG_UKN);
64 static void corgi_discharge(int on)
67 GPSR(CORGI_GPIO_DISCHARGE_ON) = GPIO_bit(CORGI_GPIO_DISCHARGE_ON);
69 GPCR(CORGI_GPIO_DISCHARGE_ON) = GPIO_bit(CORGI_GPIO_DISCHARGE_ON);
72 static void corgi_presuspend(void)
75 unsigned long wakeup_mask;
77 /* charging , so CHARGE_ON bit is HIGH during OFF. */
78 if (READ_GPIO_BIT(CORGI_GPIO_CHRG_ON))
79 PGSR1 |= GPIO_bit(CORGI_GPIO_CHRG_ON);
81 PGSR1 &= ~GPIO_bit(CORGI_GPIO_CHRG_ON);
83 if (READ_GPIO_BIT(CORGI_GPIO_LED_ORANGE))
84 PGSR0 |= GPIO_bit(CORGI_GPIO_LED_ORANGE);
86 PGSR0 &= ~GPIO_bit(CORGI_GPIO_LED_ORANGE);
88 if (READ_GPIO_BIT(CORGI_GPIO_CHRG_UKN))
89 PGSR1 |= GPIO_bit(CORGI_GPIO_CHRG_UKN);
91 PGSR1 &= ~GPIO_bit(CORGI_GPIO_CHRG_UKN);
93 /* Resume on keyboard power key */
94 PGSR2 = (PGSR2 & ~CORGI_GPIO_ALL_STROBE_BIT) | CORGI_GPIO_STROBE_BIT(0);
96 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);
98 if (!machine_is_corgi())
99 wakeup_mask |= GPIO_bit(CORGI_GPIO_MAIN_BAT_LOW);
101 PWER = wakeup_mask | PWER_RTC;
105 for (i = 0; i <=15; i++) {
106 if (PRER & PFER & GPIO_bit(i)) {
107 if (GPLR0 & GPIO_bit(i) )
108 PRER &= ~GPIO_bit(i);
110 PFER &= ~GPIO_bit(i);
115 static void corgi_postsuspend(void)
120 * Check what brought us out of the suspend.
121 * Return: 0 to sleep, otherwise wake
123 static int corgi_should_wakeup(unsigned int resume_on_alarm)
127 dev_dbg(sharpsl_pm.dev, "GPLR0 = %x,%x\n", GPLR0, PEDR);
129 if ((PEDR & GPIO_bit(CORGI_GPIO_AC_IN))) {
130 if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
132 dev_dbg(sharpsl_pm.dev, "ac insert\n");
133 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
136 dev_dbg(sharpsl_pm.dev, "ac remove\n");
137 sharpsl_pm_led(SHARPSL_LED_OFF);
138 sharpsl_pm.machinfo->charge(0);
139 sharpsl_pm.charge_mode = CHRG_OFF;
143 if ((PEDR & GPIO_bit(CORGI_GPIO_CHRG_FULL)))
144 dev_dbg(sharpsl_pm.dev, "Charge full interrupt\n");
146 if (PEDR & GPIO_bit(CORGI_GPIO_KEY_INT))
147 is_resume |= GPIO_bit(CORGI_GPIO_KEY_INT);
149 if (PEDR & GPIO_bit(CORGI_GPIO_WAKEUP))
150 is_resume |= GPIO_bit(CORGI_GPIO_WAKEUP);
152 if (resume_on_alarm && (PEDR & PWER_RTC))
153 is_resume |= PWER_RTC;
155 dev_dbg(sharpsl_pm.dev, "is_resume: %x\n",is_resume);
159 static unsigned long corgi_charger_wakeup(void)
161 return ~GPLR0 & ( GPIO_bit(CORGI_GPIO_AC_IN) | GPIO_bit(CORGI_GPIO_KEY_INT) | GPIO_bit(CORGI_GPIO_WAKEUP) );
164 unsigned long corgipm_read_devdata(int type)
167 case SHARPSL_STATUS_ACIN:
168 return ((GPLR(CORGI_GPIO_AC_IN) & GPIO_bit(CORGI_GPIO_AC_IN)) != 0);
169 case SHARPSL_STATUS_LOCK:
170 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batlock);
171 case SHARPSL_STATUS_CHRGFULL:
172 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batfull);
173 case SHARPSL_STATUS_FATAL:
174 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_fatal);
175 case SHARPSL_ACIN_VOLT:
176 return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT);
177 case SHARPSL_BATT_TEMP:
178 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP);
179 case SHARPSL_BATT_VOLT:
181 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
185 static struct sharpsl_charger_machinfo corgi_pm_machinfo = {
186 .init = corgi_charger_init,
187 .exit = sharpsl_pm_pxa_remove,
188 .gpio_batlock = CORGI_GPIO_BAT_COVER,
189 .gpio_acin = CORGI_GPIO_AC_IN,
190 .gpio_batfull = CORGI_GPIO_CHRG_FULL,
191 .discharge = corgi_discharge,
192 .charge = corgi_charge,
193 .measure_temp = corgi_measure_temp,
194 .presuspend = corgi_presuspend,
195 .postsuspend = corgi_postsuspend,
196 .read_devdata = corgipm_read_devdata,
197 .charger_wakeup = corgi_charger_wakeup,
198 .should_wakeup = corgi_should_wakeup,
200 .bat_levels_noac = spitz_battery_levels_noac,
201 .bat_levels_acin = spitz_battery_levels_acin,
202 .status_high_acin = 188,
203 .status_low_acin = 178,
204 .status_high_noac = 185,
205 .status_low_noac = 175,
208 static struct platform_device *corgipm_device;
210 static int __devinit corgipm_init(void)
214 corgipm_device = platform_device_alloc("sharpsl-pm", -1);
218 corgipm_device->dev.platform_data = &corgi_pm_machinfo;
219 ret = platform_device_add(corgipm_device);
222 platform_device_put(corgipm_device);
227 static void corgipm_exit(void)
229 platform_device_unregister(corgipm_device);
232 module_init(corgipm_init);
233 module_exit(corgipm_exit);