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/gpio.h>
18 #include <linux/gpio-pxa.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <linux/apm-emulation.h>
24 #include <asm/mach-types.h>
25 #include <mach/hardware.h>
27 #include <mach/spitz.h>
29 #include "sharpsl_pm.h"
33 #define SHARPSL_CHARGE_ON_VOLT 0x99 /* 2.9V */
34 #define SHARPSL_CHARGE_ON_TEMP 0xe0 /* 2.9V */
35 #define SHARPSL_CHARGE_ON_ACIN_HIGH 0x9b /* 6V */
36 #define SHARPSL_CHARGE_ON_ACIN_LOW 0x34 /* 2V */
37 #define SHARPSL_FATAL_ACIN_VOLT 182 /* 3.45V */
38 #define SHARPSL_FATAL_NOACIN_VOLT 170 /* 3.40V */
40 static int spitz_last_ac_status
;
42 static struct gpio spitz_charger_gpios
[] = {
43 { SPITZ_GPIO_KEY_INT
, GPIOF_IN
, "Keyboard Interrupt" },
44 { SPITZ_GPIO_SYNC
, GPIOF_IN
, "Sync" },
45 { SPITZ_GPIO_AC_IN
, GPIOF_IN
, "Charger Detection" },
46 { SPITZ_GPIO_ADC_TEMP_ON
, GPIOF_OUT_INIT_LOW
, "ADC Temp On" },
47 { SPITZ_GPIO_JK_B
, GPIOF_OUT_INIT_LOW
, "JK B" },
48 { SPITZ_GPIO_CHRG_ON
, GPIOF_OUT_INIT_LOW
, "Charger On" },
51 static void spitz_charger_init(void)
53 gpio_request_array(ARRAY_AND_SIZE(spitz_charger_gpios
));
56 static void spitz_measure_temp(int on
)
58 gpio_set_value(SPITZ_GPIO_ADC_TEMP_ON
, on
);
61 static void spitz_charge(int on
)
64 if (sharpsl_pm
.flags
& SHARPSL_SUSPENDED
) {
65 gpio_set_value(SPITZ_GPIO_JK_B
, 1);
66 gpio_set_value(SPITZ_GPIO_CHRG_ON
, 0);
68 gpio_set_value(SPITZ_GPIO_JK_B
, 0);
69 gpio_set_value(SPITZ_GPIO_CHRG_ON
, 0);
72 gpio_set_value(SPITZ_GPIO_JK_B
, 0);
73 gpio_set_value(SPITZ_GPIO_CHRG_ON
, 1);
77 static void spitz_discharge(int on
)
79 gpio_set_value(SPITZ_GPIO_JK_A
, on
);
82 /* HACK - For unknown reasons, accurate voltage readings are only made with a load
83 on the power bus which the green led on spitz provides */
84 static void spitz_discharge1(int on
)
86 gpio_set_value(SPITZ_GPIO_LED_GREEN
, on
);
89 static unsigned long gpio18_config
= GPIO18_GPIO
;
91 static void spitz_presuspend(void)
93 spitz_last_ac_status
= sharpsl_pm
.machinfo
->read_devdata(SHARPSL_STATUS_ACIN
);
95 /* GPIO Sleep Register */
98 if (machine_is_akita()) {
106 PGSR0
&= ~SPITZ_GPIO_G0_STROBE_BIT
;
107 PGSR1
&= ~SPITZ_GPIO_G1_STROBE_BIT
;
108 PGSR2
&= ~SPITZ_GPIO_G2_STROBE_BIT
;
109 PGSR3
&= ~SPITZ_GPIO_G3_STROBE_BIT
;
110 PGSR2
|= GPIO_bit(SPITZ_GPIO_KEY_STROBE0
);
112 pxa2xx_mfp_config(&gpio18_config
, 1);
113 gpio_request_one(18, GPIOF_OUT_INIT_HIGH
, "Unknown");
116 PRER
= GPIO_bit(SPITZ_GPIO_KEY_INT
);
117 PFER
= GPIO_bit(SPITZ_GPIO_KEY_INT
) | GPIO_bit(SPITZ_GPIO_RESET
);
118 PWER
= GPIO_bit(SPITZ_GPIO_KEY_INT
) | GPIO_bit(SPITZ_GPIO_RESET
) | PWER_RTC
;
119 PKWR
= GPIO_bit(SPITZ_GPIO_SYNC
) | GPIO_bit(SPITZ_GPIO_KEY_INT
) | GPIO_bit(SPITZ_GPIO_RESET
);
120 PKSR
= 0xffffffff; /* clear */
122 /* nRESET_OUT Disable */
125 /* Stop 3.6MHz and drive HIGH to PCMCIA and CS */
126 PCFR
= PCFR_GPR_EN
| PCFR_OPDE
;
129 static void spitz_postsuspend(void)
133 static int spitz_should_wakeup(unsigned int resume_on_alarm
)
136 int acin
= sharpsl_pm
.machinfo
->read_devdata(SHARPSL_STATUS_ACIN
);
138 if (spitz_last_ac_status
!= acin
) {
141 sharpsl_pm
.flags
|= SHARPSL_DO_OFFLINE_CHRG
;
142 dev_dbg(sharpsl_pm
.dev
, "AC Inserted\n");
145 dev_dbg(sharpsl_pm
.dev
, "AC Removed\n");
146 sharpsl_pm_led(SHARPSL_LED_OFF
);
147 sharpsl_pm
.machinfo
->charge(0);
148 sharpsl_pm
.charge_mode
= CHRG_OFF
;
150 spitz_last_ac_status
= acin
;
151 /* Return to suspend as this must be what we were woken for */
155 if (PEDR
& GPIO_bit(SPITZ_GPIO_KEY_INT
))
156 is_resume
|= GPIO_bit(SPITZ_GPIO_KEY_INT
);
158 if (PKSR
& GPIO_bit(SPITZ_GPIO_SYNC
))
159 is_resume
|= GPIO_bit(SPITZ_GPIO_SYNC
);
161 if (resume_on_alarm
&& (PEDR
& PWER_RTC
))
162 is_resume
|= PWER_RTC
;
164 dev_dbg(sharpsl_pm
.dev
, "is_resume: %x\n", is_resume
);
168 static bool spitz_charger_wakeup(void)
170 return !gpio_get_value(SPITZ_GPIO_KEY_INT
) ||
171 gpio_get_value(SPITZ_GPIO_SYNC
);
174 unsigned long spitzpm_read_devdata(int type
)
177 case SHARPSL_STATUS_ACIN
:
178 return !gpio_get_value(SPITZ_GPIO_AC_IN
);
179 case SHARPSL_STATUS_LOCK
:
180 return gpio_get_value(sharpsl_pm
.machinfo
->gpio_batlock
);
181 case SHARPSL_STATUS_CHRGFULL
:
182 return gpio_get_value(sharpsl_pm
.machinfo
->gpio_batfull
);
183 case SHARPSL_STATUS_FATAL
:
184 return gpio_get_value(sharpsl_pm
.machinfo
->gpio_fatal
);
185 case SHARPSL_ACIN_VOLT
:
186 return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT
);
187 case SHARPSL_BATT_TEMP
:
188 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP
);
189 case SHARPSL_BATT_VOLT
:
191 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT
);
195 struct sharpsl_charger_machinfo spitz_pm_machinfo
= {
196 .init
= spitz_charger_init
,
198 .gpio_batlock
= SPITZ_GPIO_BAT_COVER
,
199 .gpio_acin
= SPITZ_GPIO_AC_IN
,
200 .gpio_batfull
= SPITZ_GPIO_CHRG_FULL
,
202 .gpio_fatal
= SPITZ_GPIO_FATAL_BAT
,
203 .discharge
= spitz_discharge
,
204 .discharge1
= spitz_discharge1
,
205 .charge
= spitz_charge
,
206 .measure_temp
= spitz_measure_temp
,
207 .presuspend
= spitz_presuspend
,
208 .postsuspend
= spitz_postsuspend
,
209 .read_devdata
= spitzpm_read_devdata
,
210 .charger_wakeup
= spitz_charger_wakeup
,
211 .should_wakeup
= spitz_should_wakeup
,
212 #if defined(CONFIG_LCD_CORGI)
213 .backlight_limit
= corgi_lcd_limit_intensity
,
215 .charge_on_volt
= SHARPSL_CHARGE_ON_VOLT
,
216 .charge_on_temp
= SHARPSL_CHARGE_ON_TEMP
,
217 .charge_acin_high
= SHARPSL_CHARGE_ON_ACIN_HIGH
,
218 .charge_acin_low
= SHARPSL_CHARGE_ON_ACIN_LOW
,
219 .fatal_acin_volt
= SHARPSL_FATAL_ACIN_VOLT
,
220 .fatal_noacin_volt
= SHARPSL_FATAL_NOACIN_VOLT
,
222 .bat_levels_noac
= sharpsl_battery_levels_noac
,
223 .bat_levels_acin
= sharpsl_battery_levels_acin
,
224 .status_high_acin
= 188,
225 .status_low_acin
= 178,
226 .status_high_noac
= 185,
227 .status_low_noac
= 175,
230 static struct platform_device
*spitzpm_device
;
232 static int spitzpm_init(void)
236 if (!machine_is_spitz() && !machine_is_akita()
237 && !machine_is_borzoi())
240 spitzpm_device
= platform_device_alloc("sharpsl-pm", -1);
244 spitzpm_device
->dev
.platform_data
= &spitz_pm_machinfo
;
245 ret
= platform_device_add(spitzpm_device
);
248 platform_device_put(spitzpm_device
);
253 static void spitzpm_exit(void)
255 platform_device_unregister(spitzpm_device
);
258 module_init(spitzpm_init
);
259 module_exit(spitzpm_exit
);