Commit | Line | Data |
---|---|---|
e8b6f7f4 RP |
1 | /* |
2 | * Battery and Power Management code for the Sharp SL-Cxx00 | |
3 | * | |
4 | * Copyright (c) 2005 Richard Purdie | |
5 | * | |
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. | |
9 | * | |
10 | */ | |
11 | ||
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> | |
158f1e95 | 17 | #include <linux/gpio.h> |
e0bf102e | 18 | #include <linux/gpio-pxa.h> |
e8b6f7f4 RP |
19 | #include <linux/interrupt.h> |
20 | #include <linux/platform_device.h> | |
14fca61a RK |
21 | #include <linux/apm-emulation.h> |
22 | ||
e8b6f7f4 RP |
23 | #include <asm/irq.h> |
24 | #include <asm/mach-types.h> | |
a09e64fb | 25 | #include <mach/hardware.h> |
e8b6f7f4 | 26 | |
a09e64fb | 27 | #include <mach/spitz.h> |
b6eede11 | 28 | #include <mach/pxa27x.h> |
04e4ad23 | 29 | #include <mach/sharpsl_pm.h> |
b6eede11 | 30 | |
b6eede11 | 31 | #include "generic.h" |
e8b6f7f4 | 32 | |
f8703dc8 RP |
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 */ | |
39 | ||
e8b6f7f4 RP |
40 | static int spitz_last_ac_status; |
41 | ||
b6eede11 EM |
42 | static struct gpio spitz_charger_gpios[] = { |
43 | { SPITZ_GPIO_KEY_INT, GPIOF_IN, "Keyboard Interrupt" }, | |
44 | { SPITZ_GPIO_SYNC, GPIOF_IN, "Sync" }, | |
9bf448c6 | 45 | { SPITZ_GPIO_AC_IN, GPIOF_IN, "Charger Detection" }, |
b6eede11 EM |
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" }, | |
49 | }; | |
50 | ||
e8b6f7f4 RP |
51 | static void spitz_charger_init(void) |
52 | { | |
b6eede11 | 53 | gpio_request_array(ARRAY_AND_SIZE(spitz_charger_gpios)); |
e8b6f7f4 RP |
54 | } |
55 | ||
56 | static void spitz_measure_temp(int on) | |
57 | { | |
fff14720 | 58 | gpio_set_value(SPITZ_GPIO_ADC_TEMP_ON, on); |
e8b6f7f4 RP |
59 | } |
60 | ||
61 | static void spitz_charge(int on) | |
62 | { | |
63 | if (on) { | |
64 | if (sharpsl_pm.flags & SHARPSL_SUSPENDED) { | |
fff14720 EM |
65 | gpio_set_value(SPITZ_GPIO_JK_B, 1); |
66 | gpio_set_value(SPITZ_GPIO_CHRG_ON, 0); | |
e8b6f7f4 | 67 | } else { |
fff14720 EM |
68 | gpio_set_value(SPITZ_GPIO_JK_B, 0); |
69 | gpio_set_value(SPITZ_GPIO_CHRG_ON, 0); | |
e8b6f7f4 RP |
70 | } |
71 | } else { | |
fff14720 EM |
72 | gpio_set_value(SPITZ_GPIO_JK_B, 0); |
73 | gpio_set_value(SPITZ_GPIO_CHRG_ON, 1); | |
e8b6f7f4 RP |
74 | } |
75 | } | |
76 | ||
77 | static void spitz_discharge(int on) | |
78 | { | |
fff14720 | 79 | gpio_set_value(SPITZ_GPIO_JK_A, on); |
e8b6f7f4 RP |
80 | } |
81 | ||
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) | |
85 | { | |
fff14720 | 86 | gpio_set_value(SPITZ_GPIO_LED_GREEN, on); |
e8b6f7f4 RP |
87 | } |
88 | ||
510fcb0d | 89 | static unsigned long gpio18_config = GPIO18_GPIO; |
b6eede11 | 90 | |
e8b6f7f4 RP |
91 | static void spitz_presuspend(void) |
92 | { | |
b7557de4 | 93 | spitz_last_ac_status = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN); |
e8b6f7f4 RP |
94 | |
95 | /* GPIO Sleep Register */ | |
96 | PGSR0 = 0x00144018; | |
97 | PGSR1 = 0x00EF0000; | |
98 | if (machine_is_akita()) { | |
99 | PGSR2 = 0x2121C000; | |
100 | PGSR3 = 0x00600400; | |
101 | } else { | |
102 | PGSR2 = 0x0121C000; | |
103 | PGSR3 = 0x00600000; | |
104 | } | |
105 | ||
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); | |
111 | ||
510fcb0d | 112 | pxa2xx_mfp_config(&gpio18_config, 1); |
b6eede11 EM |
113 | gpio_request_one(18, GPIOF_OUT_INIT_HIGH, "Unknown"); |
114 | gpio_free(18); | |
e8b6f7f4 RP |
115 | |
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); | |
b64b0b76 | 120 | PKSR = 0xffffffff; /* clear */ |
e8b6f7f4 RP |
121 | |
122 | /* nRESET_OUT Disable */ | |
123 | PSLR |= PSLR_SL_ROD; | |
124 | ||
e8b6f7f4 RP |
125 | /* Stop 3.6MHz and drive HIGH to PCMCIA and CS */ |
126 | PCFR = PCFR_GPR_EN | PCFR_OPDE; | |
127 | } | |
128 | ||
129 | static void spitz_postsuspend(void) | |
130 | { | |
e8b6f7f4 RP |
131 | } |
132 | ||
133 | static int spitz_should_wakeup(unsigned int resume_on_alarm) | |
134 | { | |
135 | int is_resume = 0; | |
b7557de4 | 136 | int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN); |
e8b6f7f4 RP |
137 | |
138 | if (spitz_last_ac_status != acin) { | |
139 | if (acin) { | |
140 | /* charge on */ | |
141 | sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG; | |
142 | dev_dbg(sharpsl_pm.dev, "AC Inserted\n"); | |
143 | } else { | |
144 | /* charge off */ | |
145 | dev_dbg(sharpsl_pm.dev, "AC Removed\n"); | |
b7557de4 RP |
146 | sharpsl_pm_led(SHARPSL_LED_OFF); |
147 | sharpsl_pm.machinfo->charge(0); | |
e8b6f7f4 RP |
148 | sharpsl_pm.charge_mode = CHRG_OFF; |
149 | } | |
150 | spitz_last_ac_status = acin; | |
151 | /* Return to suspend as this must be what we were woken for */ | |
152 | return 0; | |
153 | } | |
154 | ||
155 | if (PEDR & GPIO_bit(SPITZ_GPIO_KEY_INT)) | |
156 | is_resume |= GPIO_bit(SPITZ_GPIO_KEY_INT); | |
157 | ||
158 | if (PKSR & GPIO_bit(SPITZ_GPIO_SYNC)) | |
159 | is_resume |= GPIO_bit(SPITZ_GPIO_SYNC); | |
160 | ||
161 | if (resume_on_alarm && (PEDR & PWER_RTC)) | |
162 | is_resume |= PWER_RTC; | |
163 | ||
b64b0b76 | 164 | dev_dbg(sharpsl_pm.dev, "is_resume: %x\n", is_resume); |
e8b6f7f4 RP |
165 | return is_resume; |
166 | } | |
167 | ||
168 | static unsigned long spitz_charger_wakeup(void) | |
169 | { | |
9bf448c6 | 170 | unsigned long ret; |
bc329ad7 | 171 | ret = ((!gpio_get_value(SPITZ_GPIO_KEY_INT) |
9bf448c6 | 172 | << GPIO_bit(SPITZ_GPIO_KEY_INT)) |
bc329ad7 | 173 | | gpio_get_value(SPITZ_GPIO_SYNC)); |
9bf448c6 | 174 | return ret; |
e8b6f7f4 RP |
175 | } |
176 | ||
b7557de4 | 177 | unsigned long spitzpm_read_devdata(int type) |
e8b6f7f4 | 178 | { |
b64b0b76 | 179 | switch (type) { |
b7557de4 | 180 | case SHARPSL_STATUS_ACIN: |
9bf448c6 | 181 | return !gpio_get_value(SPITZ_GPIO_AC_IN); |
b7557de4 | 182 | case SHARPSL_STATUS_LOCK: |
05732d7e | 183 | return gpio_get_value(sharpsl_pm.machinfo->gpio_batlock); |
b7557de4 | 184 | case SHARPSL_STATUS_CHRGFULL: |
05732d7e | 185 | return gpio_get_value(sharpsl_pm.machinfo->gpio_batfull); |
b7557de4 | 186 | case SHARPSL_STATUS_FATAL: |
05732d7e | 187 | return gpio_get_value(sharpsl_pm.machinfo->gpio_fatal); |
b7557de4 RP |
188 | case SHARPSL_ACIN_VOLT: |
189 | return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT); | |
190 | case SHARPSL_BATT_TEMP: | |
191 | return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP); | |
192 | case SHARPSL_BATT_VOLT: | |
193 | default: | |
194 | return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT); | |
195 | } | |
e8b6f7f4 RP |
196 | } |
197 | ||
198 | struct sharpsl_charger_machinfo spitz_pm_machinfo = { | |
199 | .init = spitz_charger_init, | |
d48898a3 | 200 | .exit = NULL, |
e8b6f7f4 RP |
201 | .gpio_batlock = SPITZ_GPIO_BAT_COVER, |
202 | .gpio_acin = SPITZ_GPIO_AC_IN, | |
203 | .gpio_batfull = SPITZ_GPIO_CHRG_FULL, | |
f8703dc8 | 204 | .batfull_irq = 1, |
e8b6f7f4 | 205 | .gpio_fatal = SPITZ_GPIO_FATAL_BAT, |
e8b6f7f4 RP |
206 | .discharge = spitz_discharge, |
207 | .discharge1 = spitz_discharge1, | |
208 | .charge = spitz_charge, | |
e8b6f7f4 RP |
209 | .measure_temp = spitz_measure_temp, |
210 | .presuspend = spitz_presuspend, | |
211 | .postsuspend = spitz_postsuspend, | |
b7557de4 | 212 | .read_devdata = spitzpm_read_devdata, |
e8b6f7f4 RP |
213 | .charger_wakeup = spitz_charger_wakeup, |
214 | .should_wakeup = spitz_should_wakeup, | |
5cbff960 DB |
215 | #if defined(CONFIG_LCD_CORGI) |
216 | .backlight_limit = corgi_lcd_limit_intensity, | |
02a8e769 | 217 | #endif |
f8703dc8 RP |
218 | .charge_on_volt = SHARPSL_CHARGE_ON_VOLT, |
219 | .charge_on_temp = SHARPSL_CHARGE_ON_TEMP, | |
220 | .charge_acin_high = SHARPSL_CHARGE_ON_ACIN_HIGH, | |
221 | .charge_acin_low = SHARPSL_CHARGE_ON_ACIN_LOW, | |
222 | .fatal_acin_volt = SHARPSL_FATAL_ACIN_VOLT, | |
223 | .fatal_noacin_volt= SHARPSL_FATAL_NOACIN_VOLT, | |
e8b6f7f4 | 224 | .bat_levels = 40, |
0ba01ebc PM |
225 | .bat_levels_noac = sharpsl_battery_levels_noac, |
226 | .bat_levels_acin = sharpsl_battery_levels_acin, | |
e8b6f7f4 RP |
227 | .status_high_acin = 188, |
228 | .status_low_acin = 178, | |
229 | .status_high_noac = 185, | |
230 | .status_low_noac = 175, | |
231 | }; | |
232 | ||
233 | static struct platform_device *spitzpm_device; | |
234 | ||
351a102d | 235 | static int spitzpm_init(void) |
e8b6f7f4 RP |
236 | { |
237 | int ret; | |
238 | ||
5806d8ab DB |
239 | if (!machine_is_spitz() && !machine_is_akita() |
240 | && !machine_is_borzoi()) | |
241 | return -ENODEV; | |
242 | ||
e8b6f7f4 RP |
243 | spitzpm_device = platform_device_alloc("sharpsl-pm", -1); |
244 | if (!spitzpm_device) | |
245 | return -ENOMEM; | |
246 | ||
247 | spitzpm_device->dev.platform_data = &spitz_pm_machinfo; | |
248 | ret = platform_device_add(spitzpm_device); | |
249 | ||
250 | if (ret) | |
251 | platform_device_put(spitzpm_device); | |
252 | ||
253 | return ret; | |
254 | } | |
255 | ||
256 | static void spitzpm_exit(void) | |
257 | { | |
b64b0b76 | 258 | platform_device_unregister(spitzpm_device); |
e8b6f7f4 RP |
259 | } |
260 | ||
261 | module_init(spitzpm_init); | |
262 | module_exit(spitzpm_exit); |