[ARM] pxa/spitz: fix spi cs on spitz
[deliverable/linux.git] / arch / arm / mach-pxa / corgi_pm.c
CommitLineData
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>
17#include <linux/interrupt.h>
18#include <linux/platform_device.h>
14fca61a
RK
19#include <linux/apm-emulation.h>
20
d72f25b0
RP
21#include <asm/irq.h>
22#include <asm/mach-types.h>
a09e64fb 23#include <mach/hardware.h>
d72f25b0 24
a09e64fb
RK
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>
d72f25b0
RP
30#include "sharpsl.h"
31
f8703dc8
RP
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 */
38
d72f25b0
RP
39static 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);
b7557de4 45 sharpsl_pm_pxa_init();
d72f25b0
RP
46}
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))) {
b7557de4 138 if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
d72f25b0
RP
139 /* 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");
b7557de4
RP
145 sharpsl_pm_led(SHARPSL_LED_OFF);
146 sharpsl_pm.machinfo->charge(0);
d72f25b0
RP
147 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
b7557de4 172unsigned long corgipm_read_devdata(int type)
d72f25b0 173{
b7557de4
RP
174 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 }
d72f25b0
RP
191}
192
193static struct sharpsl_charger_machinfo corgi_pm_machinfo = {
194 .init = corgi_charger_init,
b7557de4 195 .exit = sharpsl_pm_pxa_remove,
d72f25b0
RP
196 .gpio_batlock = CORGI_GPIO_BAT_COVER,
197 .gpio_acin = CORGI_GPIO_AC_IN,
198 .gpio_batfull = CORGI_GPIO_CHRG_FULL,
d72f25b0
RP
199 .discharge = corgi_discharge,
200 .charge = corgi_charge,
d72f25b0
RP
201 .measure_temp = corgi_measure_temp,
202 .presuspend = corgi_presuspend,
203 .postsuspend = corgi_postsuspend,
b7557de4 204 .read_devdata = corgipm_read_devdata,
d72f25b0
RP
205 .charger_wakeup = corgi_charger_wakeup,
206 .should_wakeup = corgi_should_wakeup,
02a8e769 207#ifdef CONFIG_BACKLIGHT_CORGI
f8703dc8 208 .backlight_limit = corgibl_limit_intensity,
02a8e769 209#endif
f8703dc8
RP
210 .charge_on_volt = SHARPSL_CHARGE_ON_VOLT,
211 .charge_on_temp = SHARPSL_CHARGE_ON_TEMP,
212 .charge_acin_high = SHARPSL_CHARGE_ON_ACIN_HIGH,
213 .charge_acin_low = SHARPSL_CHARGE_ON_ACIN_LOW,
214 .fatal_acin_volt = SHARPSL_FATAL_ACIN_VOLT,
215 .fatal_noacin_volt= SHARPSL_FATAL_NOACIN_VOLT,
216 .bat_levels = 40,
217 .bat_levels_noac = spitz_battery_levels_noac,
218 .bat_levels_acin = spitz_battery_levels_acin,
d72f25b0
RP
219 .status_high_acin = 188,
220 .status_low_acin = 178,
221 .status_high_noac = 185,
222 .status_low_noac = 175,
223};
224
225static struct platform_device *corgipm_device;
226
227static int __devinit corgipm_init(void)
228{
229 int ret;
230
043fbc09
DB
231 if (!machine_is_corgi() && !machine_is_shepherd()
232 && !machine_is_husky())
233 return -ENODEV;
234
d72f25b0
RP
235 corgipm_device = platform_device_alloc("sharpsl-pm", -1);
236 if (!corgipm_device)
237 return -ENOMEM;
238
f8703dc8
RP
239 if (!machine_is_corgi())
240 corgi_pm_machinfo.batfull_irq = 1;
241
d72f25b0
RP
242 corgipm_device->dev.platform_data = &corgi_pm_machinfo;
243 ret = platform_device_add(corgipm_device);
244
245 if (ret)
246 platform_device_put(corgipm_device);
247
248 return ret;
249}
250
251static void corgipm_exit(void)
252{
253 platform_device_unregister(corgipm_device);
254}
255
256module_init(corgipm_init);
257module_exit(corgipm_exit);
This page took 0.324819 seconds and 5 git commands to generate.