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