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
);
38 static void corgi_charge_led(int val
)
40 if (val
== SHARPSL_LED_ERROR
) {
41 dev_dbg(sharpsl_pm
.dev
, "Charge LED Error\n");
42 } else if (val
== SHARPSL_LED_ON
) {
43 dev_dbg(sharpsl_pm
.dev
, "Charge LED On\n");
44 GPSR0
= GPIO_bit(CORGI_GPIO_LED_ORANGE
);
46 dev_dbg(sharpsl_pm
.dev
, "Charge LED Off\n");
47 GPCR0
= GPIO_bit(CORGI_GPIO_LED_ORANGE
);
51 static void corgi_measure_temp(int on
)
54 GPSR(CORGI_GPIO_ADC_TEMP_ON
) = GPIO_bit(CORGI_GPIO_ADC_TEMP_ON
);
56 GPCR(CORGI_GPIO_ADC_TEMP_ON
) = GPIO_bit(CORGI_GPIO_ADC_TEMP_ON
);
59 static void corgi_charge(int on
)
62 if (machine_is_corgi() && (sharpsl_pm
.flags
& SHARPSL_SUSPENDED
)) {
63 GPCR(CORGI_GPIO_CHRG_ON
) = GPIO_bit(CORGI_GPIO_CHRG_ON
);
64 GPSR(CORGI_GPIO_CHRG_UKN
) = GPIO_bit(CORGI_GPIO_CHRG_UKN
);
66 GPSR(CORGI_GPIO_CHRG_ON
) = GPIO_bit(CORGI_GPIO_CHRG_ON
);
67 GPCR(CORGI_GPIO_CHRG_UKN
) = GPIO_bit(CORGI_GPIO_CHRG_UKN
);
70 GPCR(CORGI_GPIO_CHRG_ON
) = GPIO_bit(CORGI_GPIO_CHRG_ON
);
71 GPCR(CORGI_GPIO_CHRG_UKN
) = GPIO_bit(CORGI_GPIO_CHRG_UKN
);
75 static void corgi_discharge(int on
)
78 GPSR(CORGI_GPIO_DISCHARGE_ON
) = GPIO_bit(CORGI_GPIO_DISCHARGE_ON
);
80 GPCR(CORGI_GPIO_DISCHARGE_ON
) = GPIO_bit(CORGI_GPIO_DISCHARGE_ON
);
83 static void corgi_presuspend(void)
86 unsigned long wakeup_mask
;
88 /* charging , so CHARGE_ON bit is HIGH during OFF. */
89 if (READ_GPIO_BIT(CORGI_GPIO_CHRG_ON
))
90 PGSR1
|= GPIO_bit(CORGI_GPIO_CHRG_ON
);
92 PGSR1
&= ~GPIO_bit(CORGI_GPIO_CHRG_ON
);
94 if (READ_GPIO_BIT(CORGI_GPIO_LED_ORANGE
))
95 PGSR0
|= GPIO_bit(CORGI_GPIO_LED_ORANGE
);
97 PGSR0
&= ~GPIO_bit(CORGI_GPIO_LED_ORANGE
);
99 if (READ_GPIO_BIT(CORGI_GPIO_CHRG_UKN
))
100 PGSR1
|= GPIO_bit(CORGI_GPIO_CHRG_UKN
);
102 PGSR1
&= ~GPIO_bit(CORGI_GPIO_CHRG_UKN
);
104 /* Resume on keyboard power key */
105 PGSR2
= (PGSR2
& ~CORGI_GPIO_ALL_STROBE_BIT
) | CORGI_GPIO_STROBE_BIT(0);
107 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
);
109 if (!machine_is_corgi())
110 wakeup_mask
|= GPIO_bit(CORGI_GPIO_MAIN_BAT_LOW
);
112 PWER
= wakeup_mask
| PWER_RTC
;
116 for (i
= 0; i
<=15; i
++) {
117 if (PRER
& PFER
& GPIO_bit(i
)) {
118 if (GPLR0
& GPIO_bit(i
) )
119 PRER
&= ~GPIO_bit(i
);
121 PFER
&= ~GPIO_bit(i
);
126 static void corgi_postsuspend(void)
131 * Check what brought us out of the suspend.
132 * Return: 0 to sleep, otherwise wake
134 static int corgi_should_wakeup(unsigned int resume_on_alarm
)
138 dev_dbg(sharpsl_pm
.dev
, "GPLR0 = %x,%x\n", GPLR0
, PEDR
);
140 if ((PEDR
& GPIO_bit(CORGI_GPIO_AC_IN
))) {
141 if (STATUS_AC_IN()) {
143 dev_dbg(sharpsl_pm
.dev
, "ac insert\n");
144 sharpsl_pm
.flags
|= SHARPSL_DO_OFFLINE_CHRG
;
147 dev_dbg(sharpsl_pm
.dev
, "ac remove\n");
150 sharpsl_pm
.charge_mode
= CHRG_OFF
;
154 if ((PEDR
& GPIO_bit(CORGI_GPIO_CHRG_FULL
)))
155 dev_dbg(sharpsl_pm
.dev
, "Charge full interrupt\n");
157 if (PEDR
& GPIO_bit(CORGI_GPIO_KEY_INT
))
158 is_resume
|= GPIO_bit(CORGI_GPIO_KEY_INT
);
160 if (PEDR
& GPIO_bit(CORGI_GPIO_WAKEUP
))
161 is_resume
|= GPIO_bit(CORGI_GPIO_WAKEUP
);
163 if (resume_on_alarm
&& (PEDR
& PWER_RTC
))
164 is_resume
|= PWER_RTC
;
166 dev_dbg(sharpsl_pm
.dev
, "is_resume: %x\n",is_resume
);
170 static unsigned long corgi_charger_wakeup(void)
172 return ~GPLR0
& ( GPIO_bit(CORGI_GPIO_AC_IN
) | GPIO_bit(CORGI_GPIO_KEY_INT
) | GPIO_bit(CORGI_GPIO_WAKEUP
) );
175 static int corgi_acin_status(void)
177 return ((GPLR(CORGI_GPIO_AC_IN
) & GPIO_bit(CORGI_GPIO_AC_IN
)) != 0);
180 static struct sharpsl_charger_machinfo corgi_pm_machinfo
= {
181 .init
= corgi_charger_init
,
182 .gpio_batlock
= CORGI_GPIO_BAT_COVER
,
183 .gpio_acin
= CORGI_GPIO_AC_IN
,
184 .gpio_batfull
= CORGI_GPIO_CHRG_FULL
,
185 .status_acin
= corgi_acin_status
,
186 .discharge
= corgi_discharge
,
187 .charge
= corgi_charge
,
188 .chargeled
= corgi_charge_led
,
189 .measure_temp
= corgi_measure_temp
,
190 .presuspend
= corgi_presuspend
,
191 .postsuspend
= corgi_postsuspend
,
192 .charger_wakeup
= corgi_charger_wakeup
,
193 .should_wakeup
= corgi_should_wakeup
,
195 .bat_levels_noac
= spitz_battery_levels_noac
,
196 .bat_levels_acin
= spitz_battery_levels_acin
,
197 .status_high_acin
= 188,
198 .status_low_acin
= 178,
199 .status_high_noac
= 185,
200 .status_low_noac
= 175,
203 static struct platform_device
*corgipm_device
;
205 static int __devinit
corgipm_init(void)
209 corgipm_device
= platform_device_alloc("sharpsl-pm", -1);
213 corgipm_device
->dev
.platform_data
= &corgi_pm_machinfo
;
214 ret
= platform_device_add(corgipm_device
);
217 platform_device_put(corgipm_device
);
222 static void corgipm_exit(void)
224 platform_device_unregister(corgipm_device
);
227 module_init(corgipm_init
);
228 module_exit(corgipm_exit
);