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