blob: e35259032813ae82543a75bd24b8a3a69f3f2fd8 [file] [log] [blame]
Richard Purdied72f25b2005-11-13 10:07:46 +00001/*
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>
17#include <linux/interrupt.h>
18#include <linux/platform_device.h>
Russell King14fca612007-02-27 12:10:07 +000019#include <linux/apm-emulation.h>
20
Richard Purdied72f25b2005-11-13 10:07:46 +000021#include <asm/irq.h>
22#include <asm/mach-types.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010023#include <mach/hardware.h>
Richard Purdied72f25b2005-11-13 10:07:46 +000024
Russell Kinga09e64f2008-08-05 16:14:15 +010025#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>
Richard Purdied72f25b2005-11-13 10:07:46 +000030#include "sharpsl.h"
31
Richard Purdief8703dc2006-06-19 19:58:52 +010032#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 */
38
Richard Purdied72f25b2005-11-13 10:07:46 +000039static void corgi_charger_init(void)
40{
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);
Richard Purdieb7557de2006-01-05 20:44:55 +000045 sharpsl_pm_pxa_init();
Richard Purdied72f25b2005-11-13 10:07:46 +000046}
47
48static void corgi_measure_temp(int on)
49{
50 if (on)
51 GPSR(CORGI_GPIO_ADC_TEMP_ON) = GPIO_bit(CORGI_GPIO_ADC_TEMP_ON);
52 else
53 GPCR(CORGI_GPIO_ADC_TEMP_ON) = GPIO_bit(CORGI_GPIO_ADC_TEMP_ON);
54}
55
56static void corgi_charge(int on)
57{
58 if (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);
62 } else {
63 GPSR(CORGI_GPIO_CHRG_ON) = GPIO_bit(CORGI_GPIO_CHRG_ON);
64 GPCR(CORGI_GPIO_CHRG_UKN) = GPIO_bit(CORGI_GPIO_CHRG_UKN);
65 }
66 } else {
67 GPCR(CORGI_GPIO_CHRG_ON) = GPIO_bit(CORGI_GPIO_CHRG_ON);
68 GPCR(CORGI_GPIO_CHRG_UKN) = GPIO_bit(CORGI_GPIO_CHRG_UKN);
69 }
70}
71
72static void corgi_discharge(int on)
73{
74 if (on)
75 GPSR(CORGI_GPIO_DISCHARGE_ON) = GPIO_bit(CORGI_GPIO_DISCHARGE_ON);
76 else
77 GPCR(CORGI_GPIO_DISCHARGE_ON) = GPIO_bit(CORGI_GPIO_DISCHARGE_ON);
78}
79
80static void corgi_presuspend(void)
81{
82 int i;
83 unsigned long wakeup_mask;
84
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);
88 else
89 PGSR1 &= ~GPIO_bit(CORGI_GPIO_CHRG_ON);
90
91 if (READ_GPIO_BIT(CORGI_GPIO_LED_ORANGE))
92 PGSR0 |= GPIO_bit(CORGI_GPIO_LED_ORANGE);
93 else
94 PGSR0 &= ~GPIO_bit(CORGI_GPIO_LED_ORANGE);
95
96 if (READ_GPIO_BIT(CORGI_GPIO_CHRG_UKN))
97 PGSR1 |= GPIO_bit(CORGI_GPIO_CHRG_UKN);
98 else
99 PGSR1 &= ~GPIO_bit(CORGI_GPIO_CHRG_UKN);
100
101 /* Resume on keyboard power key */
102 PGSR2 = (PGSR2 & ~CORGI_GPIO_ALL_STROBE_BIT) | CORGI_GPIO_STROBE_BIT(0);
103
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);
105
106 if (!machine_is_corgi())
107 wakeup_mask |= GPIO_bit(CORGI_GPIO_MAIN_BAT_LOW);
108
109 PWER = wakeup_mask | PWER_RTC;
110 PRER = wakeup_mask;
111 PFER = wakeup_mask;
112
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);
117 else
118 PFER &= ~GPIO_bit(i);
119 }
120 }
121}
122
123static void corgi_postsuspend(void)
124{
125}
126
127/*
128 * Check what brought us out of the suspend.
129 * Return: 0 to sleep, otherwise wake
130 */
131static int corgi_should_wakeup(unsigned int resume_on_alarm)
132{
133 int is_resume = 0;
134
135 dev_dbg(sharpsl_pm.dev, "GPLR0 = %x,%x\n", GPLR0, PEDR);
136
137 if ((PEDR & GPIO_bit(CORGI_GPIO_AC_IN))) {
Richard Purdieb7557de2006-01-05 20:44:55 +0000138 if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
Richard Purdied72f25b2005-11-13 10:07:46 +0000139 /* charge on */
140 dev_dbg(sharpsl_pm.dev, "ac insert\n");
141 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
142 } else {
143 /* charge off */
144 dev_dbg(sharpsl_pm.dev, "ac remove\n");
Richard Purdieb7557de2006-01-05 20:44:55 +0000145 sharpsl_pm_led(SHARPSL_LED_OFF);
146 sharpsl_pm.machinfo->charge(0);
Richard Purdied72f25b2005-11-13 10:07:46 +0000147 sharpsl_pm.charge_mode = CHRG_OFF;
148 }
149 }
150
151 if ((PEDR & GPIO_bit(CORGI_GPIO_CHRG_FULL)))
152 dev_dbg(sharpsl_pm.dev, "Charge full interrupt\n");
153
154 if (PEDR & GPIO_bit(CORGI_GPIO_KEY_INT))
155 is_resume |= GPIO_bit(CORGI_GPIO_KEY_INT);
156
157 if (PEDR & GPIO_bit(CORGI_GPIO_WAKEUP))
158 is_resume |= GPIO_bit(CORGI_GPIO_WAKEUP);
159
160 if (resume_on_alarm && (PEDR & PWER_RTC))
161 is_resume |= PWER_RTC;
162
163 dev_dbg(sharpsl_pm.dev, "is_resume: %x\n",is_resume);
164 return is_resume;
165}
166
167static unsigned long corgi_charger_wakeup(void)
168{
169 return ~GPLR0 & ( GPIO_bit(CORGI_GPIO_AC_IN) | GPIO_bit(CORGI_GPIO_KEY_INT) | GPIO_bit(CORGI_GPIO_WAKEUP) );
170}
171
Richard Purdieb7557de2006-01-05 20:44:55 +0000172unsigned long corgipm_read_devdata(int type)
Richard Purdied72f25b2005-11-13 10:07:46 +0000173{
Richard Purdieb7557de2006-01-05 20:44:55 +0000174 switch(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:
188 default:
189 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
190 }
Richard Purdied72f25b2005-11-13 10:07:46 +0000191}
192
193static struct sharpsl_charger_machinfo corgi_pm_machinfo = {
194 .init = corgi_charger_init,
Richard Purdieb7557de2006-01-05 20:44:55 +0000195 .exit = sharpsl_pm_pxa_remove,
Richard Purdied72f25b2005-11-13 10:07:46 +0000196 .gpio_batlock = CORGI_GPIO_BAT_COVER,
197 .gpio_acin = CORGI_GPIO_AC_IN,
198 .gpio_batfull = CORGI_GPIO_CHRG_FULL,
Richard Purdied72f25b2005-11-13 10:07:46 +0000199 .discharge = corgi_discharge,
200 .charge = corgi_charge,
Richard Purdied72f25b2005-11-13 10:07:46 +0000201 .measure_temp = corgi_measure_temp,
202 .presuspend = corgi_presuspend,
203 .postsuspend = corgi_postsuspend,
Richard Purdieb7557de2006-01-05 20:44:55 +0000204 .read_devdata = corgipm_read_devdata,
Richard Purdied72f25b2005-11-13 10:07:46 +0000205 .charger_wakeup = corgi_charger_wakeup,
206 .should_wakeup = corgi_should_wakeup,
Dmitry Baryshkov5cbff962008-10-28 20:26:40 +0300207#if defined(CONFIG_LCD_CORGI)
208 .backlight_limit = corgi_lcd_limit_intensity,
209#elif defined(CONFIG_BACKLIGHT_CORGI)
Richard Purdief8703dc2006-06-19 19:58:52 +0100210 .backlight_limit = corgibl_limit_intensity,
Russell King02a8e762008-04-20 17:15:32 +0100211#endif
Richard Purdief8703dc2006-06-19 19:58:52 +0100212 .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,
218 .bat_levels = 40,
219 .bat_levels_noac = spitz_battery_levels_noac,
220 .bat_levels_acin = spitz_battery_levels_acin,
Richard Purdied72f25b2005-11-13 10:07:46 +0000221 .status_high_acin = 188,
222 .status_low_acin = 178,
223 .status_high_noac = 185,
224 .status_low_noac = 175,
225};
226
227static struct platform_device *corgipm_device;
228
229static int __devinit corgipm_init(void)
230{
231 int ret;
232
Dmitry Baryshkov043fbc02008-05-31 16:16:54 +0100233 if (!machine_is_corgi() && !machine_is_shepherd()
234 && !machine_is_husky())
235 return -ENODEV;
236
Richard Purdied72f25b2005-11-13 10:07:46 +0000237 corgipm_device = platform_device_alloc("sharpsl-pm", -1);
238 if (!corgipm_device)
239 return -ENOMEM;
240
Richard Purdief8703dc2006-06-19 19:58:52 +0100241 if (!machine_is_corgi())
242 corgi_pm_machinfo.batfull_irq = 1;
243
Richard Purdied72f25b2005-11-13 10:07:46 +0000244 corgipm_device->dev.platform_data = &corgi_pm_machinfo;
245 ret = platform_device_add(corgipm_device);
246
247 if (ret)
248 platform_device_put(corgipm_device);
249
250 return ret;
251}
252
253static void corgipm_exit(void)
254{
255 platform_device_unregister(corgipm_device);
256}
257
258module_init(corgipm_init);
259module_exit(corgipm_exit);