Commit | Line | Data |
---|---|---|
d72f25b0 RP |
1 | /* |
2 | * Battery and Power Management code for the Sharp SL-C7xx | |
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> | |
e63f591a | 17 | #include <linux/gpio.h> |
6f7c0471 | 18 | #include <linux/gpio-pxa.h> |
d72f25b0 RP |
19 | #include <linux/interrupt.h> |
20 | #include <linux/platform_device.h> | |
14fca61a RK |
21 | #include <linux/apm-emulation.h> |
22 | ||
d72f25b0 RP |
23 | #include <asm/irq.h> |
24 | #include <asm/mach-types.h> | |
a09e64fb | 25 | #include <mach/hardware.h> |
d72f25b0 | 26 | |
a09e64fb | 27 | #include <mach/corgi.h> |
a09e64fb | 28 | #include <mach/pxa2xx-regs.h> |
04e4ad23 | 29 | #include <mach/sharpsl_pm.h> |
e63f591a EM |
30 | |
31 | #include "generic.h" | |
d72f25b0 | 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 | ||
e63f591a EM |
40 | static struct gpio charger_gpios[] = { |
41 | { CORGI_GPIO_ADC_TEMP_ON, GPIOF_OUT_INIT_LOW, "ADC Temp On" }, | |
42 | { CORGI_GPIO_CHRG_ON, GPIOF_OUT_INIT_LOW, "Charger On" }, | |
43 | { CORGI_GPIO_CHRG_UKN, GPIOF_OUT_INIT_LOW, "Charger Unknown" }, | |
9bf448c6 | 44 | { CORGI_GPIO_AC_IN, GPIOF_IN, "Charger Detection" }, |
e63f591a | 45 | { CORGI_GPIO_KEY_INT, GPIOF_IN, "Key Interrupt" }, |
9bf448c6 | 46 | { CORGI_GPIO_WAKEUP, GPIOF_IN, "System wakeup notification" }, |
e63f591a EM |
47 | }; |
48 | ||
d72f25b0 RP |
49 | static void corgi_charger_init(void) |
50 | { | |
e63f591a | 51 | gpio_request_array(ARRAY_AND_SIZE(charger_gpios)); |
d72f25b0 RP |
52 | } |
53 | ||
54 | static void corgi_measure_temp(int on) | |
55 | { | |
e63f591a | 56 | gpio_set_value(CORGI_GPIO_ADC_TEMP_ON, on); |
d72f25b0 RP |
57 | } |
58 | ||
59 | static void corgi_charge(int on) | |
60 | { | |
61 | if (on) { | |
62 | if (machine_is_corgi() && (sharpsl_pm.flags & SHARPSL_SUSPENDED)) { | |
e63f591a EM |
63 | gpio_set_value(CORGI_GPIO_CHRG_ON, 0); |
64 | gpio_set_value(CORGI_GPIO_CHRG_UKN, 1); | |
d72f25b0 | 65 | } else { |
e63f591a EM |
66 | gpio_set_value(CORGI_GPIO_CHRG_ON, 1); |
67 | gpio_set_value(CORGI_GPIO_CHRG_UKN, 0); | |
d72f25b0 RP |
68 | } |
69 | } else { | |
e63f591a EM |
70 | gpio_set_value(CORGI_GPIO_CHRG_ON, 0); |
71 | gpio_set_value(CORGI_GPIO_CHRG_UKN, 0); | |
d72f25b0 RP |
72 | } |
73 | } | |
74 | ||
75 | static void corgi_discharge(int on) | |
76 | { | |
e63f591a | 77 | gpio_set_value(CORGI_GPIO_DISCHARGE_ON, on); |
d72f25b0 RP |
78 | } |
79 | ||
80 | static void corgi_presuspend(void) | |
81 | { | |
d72f25b0 RP |
82 | } |
83 | ||
84 | static void corgi_postsuspend(void) | |
85 | { | |
86 | } | |
87 | ||
88 | /* | |
89 | * Check what brought us out of the suspend. | |
90 | * Return: 0 to sleep, otherwise wake | |
91 | */ | |
92 | static int corgi_should_wakeup(unsigned int resume_on_alarm) | |
93 | { | |
94 | int is_resume = 0; | |
95 | ||
9bf448c6 HZ |
96 | dev_dbg(sharpsl_pm.dev, "PEDR = %x, GPIO_AC_IN = %d, " |
97 | "GPIO_CHRG_FULL = %d, GPIO_KEY_INT = %d, GPIO_WAKEUP = %d\n", | |
98 | PEDR, gpio_get_value(CORGI_GPIO_AC_IN), | |
99 | gpio_get_value(CORGI_GPIO_CHRG_FULL), | |
100 | gpio_get_value(CORGI_GPIO_KEY_INT), | |
101 | gpio_get_value(CORGI_GPIO_WAKEUP)); | |
d72f25b0 RP |
102 | |
103 | if ((PEDR & GPIO_bit(CORGI_GPIO_AC_IN))) { | |
b7557de4 | 104 | if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) { |
d72f25b0 RP |
105 | /* charge on */ |
106 | dev_dbg(sharpsl_pm.dev, "ac insert\n"); | |
107 | sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG; | |
108 | } else { | |
109 | /* charge off */ | |
110 | dev_dbg(sharpsl_pm.dev, "ac remove\n"); | |
b7557de4 RP |
111 | sharpsl_pm_led(SHARPSL_LED_OFF); |
112 | sharpsl_pm.machinfo->charge(0); | |
d72f25b0 RP |
113 | sharpsl_pm.charge_mode = CHRG_OFF; |
114 | } | |
115 | } | |
116 | ||
117 | if ((PEDR & GPIO_bit(CORGI_GPIO_CHRG_FULL))) | |
118 | dev_dbg(sharpsl_pm.dev, "Charge full interrupt\n"); | |
119 | ||
120 | if (PEDR & GPIO_bit(CORGI_GPIO_KEY_INT)) | |
121 | is_resume |= GPIO_bit(CORGI_GPIO_KEY_INT); | |
122 | ||
123 | if (PEDR & GPIO_bit(CORGI_GPIO_WAKEUP)) | |
124 | is_resume |= GPIO_bit(CORGI_GPIO_WAKEUP); | |
125 | ||
126 | if (resume_on_alarm && (PEDR & PWER_RTC)) | |
127 | is_resume |= PWER_RTC; | |
128 | ||
129 | dev_dbg(sharpsl_pm.dev, "is_resume: %x\n",is_resume); | |
130 | return is_resume; | |
131 | } | |
132 | ||
133 | static unsigned long corgi_charger_wakeup(void) | |
134 | { | |
9bf448c6 HZ |
135 | unsigned long ret; |
136 | ||
137 | ret = (!gpio_get_value(CORGI_GPIO_AC_IN) << GPIO_bit(CORGI_GPIO_AC_IN)) | |
138 | | (!gpio_get_value(CORGI_GPIO_KEY_INT) | |
139 | << GPIO_bit(CORGI_GPIO_KEY_INT)) | |
140 | | (!gpio_get_value(CORGI_GPIO_WAKEUP) | |
141 | << GPIO_bit(CORGI_GPIO_WAKEUP)); | |
142 | return ret; | |
d72f25b0 RP |
143 | } |
144 | ||
b7557de4 | 145 | unsigned long corgipm_read_devdata(int type) |
d72f25b0 | 146 | { |
b7557de4 RP |
147 | switch(type) { |
148 | case SHARPSL_STATUS_ACIN: | |
9bf448c6 | 149 | return !gpio_get_value(CORGI_GPIO_AC_IN); |
b7557de4 | 150 | case SHARPSL_STATUS_LOCK: |
05732d7e | 151 | return gpio_get_value(sharpsl_pm.machinfo->gpio_batlock); |
b7557de4 | 152 | case SHARPSL_STATUS_CHRGFULL: |
05732d7e | 153 | return gpio_get_value(sharpsl_pm.machinfo->gpio_batfull); |
b7557de4 | 154 | case SHARPSL_STATUS_FATAL: |
05732d7e | 155 | return gpio_get_value(sharpsl_pm.machinfo->gpio_fatal); |
b7557de4 RP |
156 | case SHARPSL_ACIN_VOLT: |
157 | return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT); | |
158 | case SHARPSL_BATT_TEMP: | |
159 | return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP); | |
160 | case SHARPSL_BATT_VOLT: | |
161 | default: | |
162 | return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT); | |
163 | } | |
d72f25b0 RP |
164 | } |
165 | ||
166 | static struct sharpsl_charger_machinfo corgi_pm_machinfo = { | |
167 | .init = corgi_charger_init, | |
d48898a3 | 168 | .exit = NULL, |
d72f25b0 RP |
169 | .gpio_batlock = CORGI_GPIO_BAT_COVER, |
170 | .gpio_acin = CORGI_GPIO_AC_IN, | |
171 | .gpio_batfull = CORGI_GPIO_CHRG_FULL, | |
d72f25b0 RP |
172 | .discharge = corgi_discharge, |
173 | .charge = corgi_charge, | |
d72f25b0 RP |
174 | .measure_temp = corgi_measure_temp, |
175 | .presuspend = corgi_presuspend, | |
176 | .postsuspend = corgi_postsuspend, | |
b7557de4 | 177 | .read_devdata = corgipm_read_devdata, |
d72f25b0 RP |
178 | .charger_wakeup = corgi_charger_wakeup, |
179 | .should_wakeup = corgi_should_wakeup, | |
5cbff960 DB |
180 | #if defined(CONFIG_LCD_CORGI) |
181 | .backlight_limit = corgi_lcd_limit_intensity, | |
02a8e769 | 182 | #endif |
f8703dc8 RP |
183 | .charge_on_volt = SHARPSL_CHARGE_ON_VOLT, |
184 | .charge_on_temp = SHARPSL_CHARGE_ON_TEMP, | |
185 | .charge_acin_high = SHARPSL_CHARGE_ON_ACIN_HIGH, | |
186 | .charge_acin_low = SHARPSL_CHARGE_ON_ACIN_LOW, | |
187 | .fatal_acin_volt = SHARPSL_FATAL_ACIN_VOLT, | |
188 | .fatal_noacin_volt= SHARPSL_FATAL_NOACIN_VOLT, | |
189 | .bat_levels = 40, | |
0ba01ebc PM |
190 | .bat_levels_noac = sharpsl_battery_levels_noac, |
191 | .bat_levels_acin = sharpsl_battery_levels_acin, | |
d72f25b0 RP |
192 | .status_high_acin = 188, |
193 | .status_low_acin = 178, | |
194 | .status_high_noac = 185, | |
195 | .status_low_noac = 175, | |
196 | }; | |
197 | ||
198 | static struct platform_device *corgipm_device; | |
199 | ||
200 | static int __devinit corgipm_init(void) | |
201 | { | |
202 | int ret; | |
203 | ||
043fbc09 DB |
204 | if (!machine_is_corgi() && !machine_is_shepherd() |
205 | && !machine_is_husky()) | |
206 | return -ENODEV; | |
207 | ||
d72f25b0 RP |
208 | corgipm_device = platform_device_alloc("sharpsl-pm", -1); |
209 | if (!corgipm_device) | |
210 | return -ENOMEM; | |
211 | ||
f8703dc8 RP |
212 | if (!machine_is_corgi()) |
213 | corgi_pm_machinfo.batfull_irq = 1; | |
214 | ||
d72f25b0 RP |
215 | corgipm_device->dev.platform_data = &corgi_pm_machinfo; |
216 | ret = platform_device_add(corgipm_device); | |
217 | ||
218 | if (ret) | |
219 | platform_device_put(corgipm_device); | |
220 | ||
221 | return ret; | |
222 | } | |
223 | ||
224 | static void corgipm_exit(void) | |
225 | { | |
226 | platform_device_unregister(corgipm_device); | |
227 | } | |
228 | ||
229 | module_init(corgipm_init); | |
230 | module_exit(corgipm_exit); |