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>
19 #include <linux/apm-emulation.h>
22 #include <asm/mach-types.h>
23 #include <mach/hardware.h>
25 #include <mach/sharpsl.h>
26 #include <mach/corgi.h>
27 #include <mach/pxa-regs.h>
28 #include <mach/pxa2xx-regs.h>
29 #include <mach/pxa2xx-gpio.h>
32 #define SHARPSL_CHARGE_ON_VOLT 0x99 /* 2.9V */
33 #define SHARPSL_CHARGE_ON_TEMP 0xe0 /* 2.9V */
34 #define SHARPSL_CHARGE_ON_ACIN_HIGH 0x9b /* 6V */
35 #define SHARPSL_CHARGE_ON_ACIN_LOW 0x34 /* 2V */
36 #define SHARPSL_FATAL_ACIN_VOLT 182 /* 3.45V */
37 #define SHARPSL_FATAL_NOACIN_VOLT 170 /* 3.40V */
39 static void corgi_charger_init(void)
41 pxa_gpio_mode(CORGI_GPIO_ADC_TEMP_ON
| GPIO_OUT
);
42 pxa_gpio_mode(CORGI_GPIO_CHRG_ON
| GPIO_OUT
);
43 pxa_gpio_mode(CORGI_GPIO_CHRG_UKN
| GPIO_OUT
);
44 pxa_gpio_mode(CORGI_GPIO_KEY_INT
| GPIO_IN
);
45 sharpsl_pm_pxa_init();
48 static void corgi_measure_temp(int on
)
51 GPSR(CORGI_GPIO_ADC_TEMP_ON
) = GPIO_bit(CORGI_GPIO_ADC_TEMP_ON
);
53 GPCR(CORGI_GPIO_ADC_TEMP_ON
) = GPIO_bit(CORGI_GPIO_ADC_TEMP_ON
);
56 static void corgi_charge(int on
)
59 if (machine_is_corgi() && (sharpsl_pm
.flags
& SHARPSL_SUSPENDED
)) {
60 GPCR(CORGI_GPIO_CHRG_ON
) = GPIO_bit(CORGI_GPIO_CHRG_ON
);
61 GPSR(CORGI_GPIO_CHRG_UKN
) = GPIO_bit(CORGI_GPIO_CHRG_UKN
);
63 GPSR(CORGI_GPIO_CHRG_ON
) = GPIO_bit(CORGI_GPIO_CHRG_ON
);
64 GPCR(CORGI_GPIO_CHRG_UKN
) = GPIO_bit(CORGI_GPIO_CHRG_UKN
);
67 GPCR(CORGI_GPIO_CHRG_ON
) = GPIO_bit(CORGI_GPIO_CHRG_ON
);
68 GPCR(CORGI_GPIO_CHRG_UKN
) = GPIO_bit(CORGI_GPIO_CHRG_UKN
);
72 static void corgi_discharge(int on
)
75 GPSR(CORGI_GPIO_DISCHARGE_ON
) = GPIO_bit(CORGI_GPIO_DISCHARGE_ON
);
77 GPCR(CORGI_GPIO_DISCHARGE_ON
) = GPIO_bit(CORGI_GPIO_DISCHARGE_ON
);
80 static void corgi_presuspend(void)
83 unsigned long wakeup_mask
;
85 /* charging , so CHARGE_ON bit is HIGH during OFF. */
86 if (READ_GPIO_BIT(CORGI_GPIO_CHRG_ON
))
87 PGSR1
|= GPIO_bit(CORGI_GPIO_CHRG_ON
);
89 PGSR1
&= ~GPIO_bit(CORGI_GPIO_CHRG_ON
);
91 if (READ_GPIO_BIT(CORGI_GPIO_LED_ORANGE
))
92 PGSR0
|= GPIO_bit(CORGI_GPIO_LED_ORANGE
);
94 PGSR0
&= ~GPIO_bit(CORGI_GPIO_LED_ORANGE
);
96 if (READ_GPIO_BIT(CORGI_GPIO_CHRG_UKN
))
97 PGSR1
|= GPIO_bit(CORGI_GPIO_CHRG_UKN
);
99 PGSR1
&= ~GPIO_bit(CORGI_GPIO_CHRG_UKN
);
101 /* Resume on keyboard power key */
102 PGSR2
= (PGSR2
& ~CORGI_GPIO_ALL_STROBE_BIT
) | CORGI_GPIO_STROBE_BIT(0);
104 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
);
106 if (!machine_is_corgi())
107 wakeup_mask
|= GPIO_bit(CORGI_GPIO_MAIN_BAT_LOW
);
109 PWER
= wakeup_mask
| PWER_RTC
;
113 for (i
= 0; i
<=15; i
++) {
114 if (PRER
& PFER
& GPIO_bit(i
)) {
115 if (GPLR0
& GPIO_bit(i
) )
116 PRER
&= ~GPIO_bit(i
);
118 PFER
&= ~GPIO_bit(i
);
123 static void corgi_postsuspend(void)
128 * Check what brought us out of the suspend.
129 * Return: 0 to sleep, otherwise wake
131 static int corgi_should_wakeup(unsigned int resume_on_alarm
)
135 dev_dbg(sharpsl_pm
.dev
, "GPLR0 = %x,%x\n", GPLR0
, PEDR
);
137 if ((PEDR
& GPIO_bit(CORGI_GPIO_AC_IN
))) {
138 if (sharpsl_pm
.machinfo
->read_devdata(SHARPSL_STATUS_ACIN
)) {
140 dev_dbg(sharpsl_pm
.dev
, "ac insert\n");
141 sharpsl_pm
.flags
|= SHARPSL_DO_OFFLINE_CHRG
;
144 dev_dbg(sharpsl_pm
.dev
, "ac remove\n");
145 sharpsl_pm_led(SHARPSL_LED_OFF
);
146 sharpsl_pm
.machinfo
->charge(0);
147 sharpsl_pm
.charge_mode
= CHRG_OFF
;
151 if ((PEDR
& GPIO_bit(CORGI_GPIO_CHRG_FULL
)))
152 dev_dbg(sharpsl_pm
.dev
, "Charge full interrupt\n");
154 if (PEDR
& GPIO_bit(CORGI_GPIO_KEY_INT
))
155 is_resume
|= GPIO_bit(CORGI_GPIO_KEY_INT
);
157 if (PEDR
& GPIO_bit(CORGI_GPIO_WAKEUP
))
158 is_resume
|= GPIO_bit(CORGI_GPIO_WAKEUP
);
160 if (resume_on_alarm
&& (PEDR
& PWER_RTC
))
161 is_resume
|= PWER_RTC
;
163 dev_dbg(sharpsl_pm
.dev
, "is_resume: %x\n",is_resume
);
167 static unsigned long corgi_charger_wakeup(void)
169 return ~GPLR0
& ( GPIO_bit(CORGI_GPIO_AC_IN
) | GPIO_bit(CORGI_GPIO_KEY_INT
) | GPIO_bit(CORGI_GPIO_WAKEUP
) );
172 unsigned long corgipm_read_devdata(int type
)
175 case SHARPSL_STATUS_ACIN
:
176 return ((GPLR(CORGI_GPIO_AC_IN
) & GPIO_bit(CORGI_GPIO_AC_IN
)) != 0);
177 case SHARPSL_STATUS_LOCK
:
178 return READ_GPIO_BIT(sharpsl_pm
.machinfo
->gpio_batlock
);
179 case SHARPSL_STATUS_CHRGFULL
:
180 return READ_GPIO_BIT(sharpsl_pm
.machinfo
->gpio_batfull
);
181 case SHARPSL_STATUS_FATAL
:
182 return READ_GPIO_BIT(sharpsl_pm
.machinfo
->gpio_fatal
);
183 case SHARPSL_ACIN_VOLT
:
184 return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT
);
185 case SHARPSL_BATT_TEMP
:
186 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP
);
187 case SHARPSL_BATT_VOLT
:
189 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT
);
193 static struct sharpsl_charger_machinfo corgi_pm_machinfo
= {
194 .init
= corgi_charger_init
,
195 .exit
= sharpsl_pm_pxa_remove
,
196 .gpio_batlock
= CORGI_GPIO_BAT_COVER
,
197 .gpio_acin
= CORGI_GPIO_AC_IN
,
198 .gpio_batfull
= CORGI_GPIO_CHRG_FULL
,
199 .discharge
= corgi_discharge
,
200 .charge
= corgi_charge
,
201 .measure_temp
= corgi_measure_temp
,
202 .presuspend
= corgi_presuspend
,
203 .postsuspend
= corgi_postsuspend
,
204 .read_devdata
= corgipm_read_devdata
,
205 .charger_wakeup
= corgi_charger_wakeup
,
206 .should_wakeup
= corgi_should_wakeup
,
207 #if defined(CONFIG_LCD_CORGI)
208 .backlight_limit
= corgi_lcd_limit_intensity
,
209 #elif defined(CONFIG_BACKLIGHT_CORGI)
210 .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
*corgipm_device
;
229 static int __devinit
corgipm_init(void)
233 if (!machine_is_corgi() && !machine_is_shepherd()
234 && !machine_is_husky())
237 corgipm_device
= platform_device_alloc("sharpsl-pm", -1);
241 if (!machine_is_corgi())
242 corgi_pm_machinfo
.batfull_irq
= 1;
244 corgipm_device
->dev
.platform_data
= &corgi_pm_machinfo
;
245 ret
= platform_device_add(corgipm_device
);
248 platform_device_put(corgipm_device
);
253 static void corgipm_exit(void)
255 platform_device_unregister(corgipm_device
);
258 module_init(corgipm_init
);
259 module_exit(corgipm_exit
);