Merge branch 'master' into for_paulus
[linux-drm-fsl-dcu.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/interrupt.h>
18 #include <linux/platform_device.h>
19 #include <asm/apm-emulation.h>
20 #include <asm/irq.h>
21 #include <asm/mach-types.h>
22 #include <asm/hardware.h>
23 #include <asm/hardware/scoop.h>
24
25 #include <asm/arch/sharpsl.h>
26 #include <asm/arch/corgi.h>
27 #include <asm/arch/pxa-regs.h>
28 #include "sharpsl.h"
29
30 #define SHARPSL_CHARGE_ON_VOLT         0x99  /* 2.9V */
31 #define SHARPSL_CHARGE_ON_TEMP         0xe0  /* 2.9V */
32 #define SHARPSL_CHARGE_ON_ACIN_HIGH    0x9b  /* 6V */
33 #define SHARPSL_CHARGE_ON_ACIN_LOW     0x34  /* 2V */
34 #define SHARPSL_FATAL_ACIN_VOLT        182   /* 3.45V */
35 #define SHARPSL_FATAL_NOACIN_VOLT      170   /* 3.40V */
36
37 static void corgi_charger_init(void)
38 {
39         pxa_gpio_mode(CORGI_GPIO_ADC_TEMP_ON | GPIO_OUT);
40         pxa_gpio_mode(CORGI_GPIO_CHRG_ON | GPIO_OUT);
41         pxa_gpio_mode(CORGI_GPIO_CHRG_UKN | GPIO_OUT);
42         pxa_gpio_mode(CORGI_GPIO_KEY_INT | GPIO_IN);
43         sharpsl_pm_pxa_init();
44 }
45
46 static void corgi_measure_temp(int on)
47 {
48         if (on)
49                 GPSR(CORGI_GPIO_ADC_TEMP_ON) = GPIO_bit(CORGI_GPIO_ADC_TEMP_ON);
50         else
51                 GPCR(CORGI_GPIO_ADC_TEMP_ON) = GPIO_bit(CORGI_GPIO_ADC_TEMP_ON);
52 }
53
54 static void corgi_charge(int on)
55 {
56         if (on) {
57                 if (machine_is_corgi() && (sharpsl_pm.flags & SHARPSL_SUSPENDED)) {
58                         GPCR(CORGI_GPIO_CHRG_ON) = GPIO_bit(CORGI_GPIO_CHRG_ON);
59                         GPSR(CORGI_GPIO_CHRG_UKN) = GPIO_bit(CORGI_GPIO_CHRG_UKN);
60                 } else {
61                         GPSR(CORGI_GPIO_CHRG_ON) = GPIO_bit(CORGI_GPIO_CHRG_ON);
62                         GPCR(CORGI_GPIO_CHRG_UKN) = GPIO_bit(CORGI_GPIO_CHRG_UKN);
63                 }
64         } else {
65                 GPCR(CORGI_GPIO_CHRG_ON) = GPIO_bit(CORGI_GPIO_CHRG_ON);
66                 GPCR(CORGI_GPIO_CHRG_UKN) = GPIO_bit(CORGI_GPIO_CHRG_UKN);
67         }
68 }
69
70 static void corgi_discharge(int on)
71 {
72         if (on)
73                 GPSR(CORGI_GPIO_DISCHARGE_ON) = GPIO_bit(CORGI_GPIO_DISCHARGE_ON);
74         else
75                 GPCR(CORGI_GPIO_DISCHARGE_ON) = GPIO_bit(CORGI_GPIO_DISCHARGE_ON);
76 }
77
78 static void corgi_presuspend(void)
79 {
80         int i;
81         unsigned long wakeup_mask;
82
83         /* charging , so CHARGE_ON bit is HIGH during OFF. */
84         if (READ_GPIO_BIT(CORGI_GPIO_CHRG_ON))
85                 PGSR1 |= GPIO_bit(CORGI_GPIO_CHRG_ON);
86         else
87                 PGSR1 &= ~GPIO_bit(CORGI_GPIO_CHRG_ON);
88
89         if (READ_GPIO_BIT(CORGI_GPIO_LED_ORANGE))
90                 PGSR0 |= GPIO_bit(CORGI_GPIO_LED_ORANGE);
91         else
92                 PGSR0 &= ~GPIO_bit(CORGI_GPIO_LED_ORANGE);
93
94         if (READ_GPIO_BIT(CORGI_GPIO_CHRG_UKN))
95                 PGSR1 |= GPIO_bit(CORGI_GPIO_CHRG_UKN);
96         else
97                 PGSR1 &= ~GPIO_bit(CORGI_GPIO_CHRG_UKN);
98
99         /* Resume on keyboard power key */
100         PGSR2 = (PGSR2 & ~CORGI_GPIO_ALL_STROBE_BIT) | CORGI_GPIO_STROBE_BIT(0);
101
102         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);
103
104         if (!machine_is_corgi())
105                 wakeup_mask |= GPIO_bit(CORGI_GPIO_MAIN_BAT_LOW);
106
107         PWER = wakeup_mask | PWER_RTC;
108         PRER = wakeup_mask;
109         PFER = wakeup_mask;
110
111         for (i = 0; i <=15; i++) {
112                 if (PRER & PFER & GPIO_bit(i)) {
113                         if (GPLR0 & GPIO_bit(i) )
114                                 PRER &= ~GPIO_bit(i);
115                         else
116                                 PFER &= ~GPIO_bit(i);
117                 }
118         }
119 }
120
121 static void corgi_postsuspend(void)
122 {
123 }
124
125 /*
126  * Check what brought us out of the suspend.
127  * Return: 0 to sleep, otherwise wake
128  */
129 static int corgi_should_wakeup(unsigned int resume_on_alarm)
130 {
131         int is_resume = 0;
132
133         dev_dbg(sharpsl_pm.dev, "GPLR0 = %x,%x\n", GPLR0, PEDR);
134
135         if ((PEDR & GPIO_bit(CORGI_GPIO_AC_IN))) {
136                 if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
137                         /* charge on */
138                         dev_dbg(sharpsl_pm.dev, "ac insert\n");
139                         sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
140                 } else {
141                         /* charge off */
142                         dev_dbg(sharpsl_pm.dev, "ac remove\n");
143                         sharpsl_pm_led(SHARPSL_LED_OFF);
144                         sharpsl_pm.machinfo->charge(0);
145                         sharpsl_pm.charge_mode = CHRG_OFF;
146                 }
147         }
148
149         if ((PEDR & GPIO_bit(CORGI_GPIO_CHRG_FULL)))
150                 dev_dbg(sharpsl_pm.dev, "Charge full interrupt\n");
151
152         if (PEDR & GPIO_bit(CORGI_GPIO_KEY_INT))
153                 is_resume |= GPIO_bit(CORGI_GPIO_KEY_INT);
154
155         if (PEDR & GPIO_bit(CORGI_GPIO_WAKEUP))
156                 is_resume |= GPIO_bit(CORGI_GPIO_WAKEUP);
157
158         if (resume_on_alarm && (PEDR & PWER_RTC))
159                 is_resume |= PWER_RTC;
160
161         dev_dbg(sharpsl_pm.dev, "is_resume: %x\n",is_resume);
162         return is_resume;
163 }
164
165 static unsigned long corgi_charger_wakeup(void)
166 {
167         return ~GPLR0 & ( GPIO_bit(CORGI_GPIO_AC_IN) | GPIO_bit(CORGI_GPIO_KEY_INT) | GPIO_bit(CORGI_GPIO_WAKEUP) );
168 }
169
170 unsigned long corgipm_read_devdata(int type)
171 {
172         switch(type) {
173         case SHARPSL_STATUS_ACIN:
174                 return ((GPLR(CORGI_GPIO_AC_IN) & GPIO_bit(CORGI_GPIO_AC_IN)) != 0);
175         case SHARPSL_STATUS_LOCK:
176                 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batlock);
177         case SHARPSL_STATUS_CHRGFULL:
178                 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batfull);
179         case SHARPSL_STATUS_FATAL:
180                 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_fatal);
181         case SHARPSL_ACIN_VOLT:
182                 return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT);
183         case SHARPSL_BATT_TEMP:
184                 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP);
185         case SHARPSL_BATT_VOLT:
186         default:
187                 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
188         }
189 }
190
191 static struct sharpsl_charger_machinfo corgi_pm_machinfo = {
192         .init            = corgi_charger_init,
193         .exit            = sharpsl_pm_pxa_remove,
194         .gpio_batlock    = CORGI_GPIO_BAT_COVER,
195         .gpio_acin       = CORGI_GPIO_AC_IN,
196         .gpio_batfull    = CORGI_GPIO_CHRG_FULL,
197         .discharge       = corgi_discharge,
198         .charge          = corgi_charge,
199         .measure_temp    = corgi_measure_temp,
200         .presuspend      = corgi_presuspend,
201         .postsuspend     = corgi_postsuspend,
202         .read_devdata    = corgipm_read_devdata,
203         .charger_wakeup  = corgi_charger_wakeup,
204         .should_wakeup   = corgi_should_wakeup,
205         .backlight_limit = corgibl_limit_intensity,
206         .charge_on_volt   = SHARPSL_CHARGE_ON_VOLT,
207         .charge_on_temp   = SHARPSL_CHARGE_ON_TEMP,
208         .charge_acin_high = SHARPSL_CHARGE_ON_ACIN_HIGH,
209         .charge_acin_low  = SHARPSL_CHARGE_ON_ACIN_LOW,
210         .fatal_acin_volt  = SHARPSL_FATAL_ACIN_VOLT,
211         .fatal_noacin_volt= SHARPSL_FATAL_NOACIN_VOLT,
212         .bat_levels       = 40,
213         .bat_levels_noac  = spitz_battery_levels_noac,
214         .bat_levels_acin  = spitz_battery_levels_acin,
215         .status_high_acin = 188,
216         .status_low_acin  = 178,
217         .status_high_noac = 185,
218         .status_low_noac  = 175,
219 };
220
221 static struct platform_device *corgipm_device;
222
223 static int __devinit corgipm_init(void)
224 {
225         int ret;
226
227         corgipm_device = platform_device_alloc("sharpsl-pm", -1);
228         if (!corgipm_device)
229                 return -ENOMEM;
230
231         if (!machine_is_corgi())
232             corgi_pm_machinfo.batfull_irq = 1;
233
234         corgipm_device->dev.platform_data = &corgi_pm_machinfo;
235         ret = platform_device_add(corgipm_device);
236
237         if (ret)
238                 platform_device_put(corgipm_device);
239
240         return ret;
241 }
242
243 static void corgipm_exit(void)
244 {
245         platform_device_unregister(corgipm_device);
246 }
247
248 module_init(corgipm_init);
249 module_exit(corgipm_exit);