Merge branches 'release', 'asus', 'sony-laptop' and 'thinkpad' into release
[linux-2.6] / arch / arm / mach-sa1100 / collie_pm.c
1 /*
2  * Based on spitz_pm.c and sharp code.
3  *
4  * Copyright (C) 2001  SHARP
5  * Copyright 2005 Pavel Machek <pavel@suse.cz>
6  *
7  * Distribute under GPLv2.
8  *
9  * Li-ion batteries are angry beasts, and they like to explode. This driver is not finished,
10  * and sometimes charges them when it should not. If it makes angry lithium to come your way...
11  * ...well, you have been warned.
12  *
13  * Actually, this should be quite safe, it seems sharp leaves charger enabled by default,
14  * and my collie did not explode (yet).
15  */
16
17 #include <linux/module.h>
18 #include <linux/stat.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/delay.h>
22 #include <linux/interrupt.h>
23 #include <linux/device.h>
24 #include <linux/platform_device.h>
25
26 #include <asm/irq.h>
27 #include <asm/mach-types.h>
28 #include <asm/hardware.h>
29 #include <asm/hardware/scoop.h>
30 #include <asm/dma.h>
31 #include <asm/arch/collie.h>
32 #include <asm/mach/sharpsl_param.h>
33 #include <asm/hardware/sharpsl_pm.h>
34
35 #include "../drivers/mfd/ucb1x00.h"
36
37 static struct ucb1x00 *ucb;
38 static int ad_revise;
39
40 #define ADCtoPower(x)          ((330 * x * 2) / 1024)
41
42 static void collie_charger_init(void)
43 {
44         int err;
45
46         if (sharpsl_param.adadj != -1)
47                 ad_revise = sharpsl_param.adadj;
48
49         /* Register interrupt handler. */
50         if ((err = request_irq(COLLIE_IRQ_GPIO_AC_IN, sharpsl_ac_isr, IRQF_DISABLED,
51                                "ACIN", sharpsl_ac_isr))) {
52                 printk("Could not get irq %d.\n", COLLIE_IRQ_GPIO_AC_IN);
53                 return;
54         }
55         if ((err = request_irq(COLLIE_IRQ_GPIO_CO, sharpsl_chrg_full_isr, IRQF_DISABLED,
56                                "CO", sharpsl_chrg_full_isr))) {
57                 free_irq(COLLIE_IRQ_GPIO_AC_IN, sharpsl_ac_isr);
58                 printk("Could not get irq %d.\n", COLLIE_IRQ_GPIO_CO);
59                 return;
60         }
61
62         ucb1x00_io_set_dir(ucb, 0, COLLIE_TC35143_GPIO_MBAT_ON | COLLIE_TC35143_GPIO_TMP_ON |
63                                    COLLIE_TC35143_GPIO_BBAT_ON);
64         return;
65 }
66
67 static void collie_measure_temp(int on)
68 {
69         if (on)
70                 ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_TMP_ON, 0);
71         else
72                 ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_TMP_ON);
73 }
74
75 static void collie_charge(int on)
76 {
77         extern struct platform_device colliescoop_device;
78
79         /* Zaurus seems to contain LTC1731; it should know when to
80          * stop charging itself, so setting charge on should be
81          * relatively harmless (as long as it is not done too often).
82          */
83         if (on) {
84                 set_scoop_gpio(&colliescoop_device.dev, COLLIE_SCP_CHARGE_ON);
85         } else {
86                 reset_scoop_gpio(&colliescoop_device.dev, COLLIE_SCP_CHARGE_ON);
87         }
88 }
89
90 static void collie_discharge(int on)
91 {
92 }
93
94 static void collie_discharge1(int on)
95 {
96 }
97
98 static void collie_presuspend(void)
99 {
100 }
101
102 static void collie_postsuspend(void)
103 {
104 }
105
106 static int collie_should_wakeup(unsigned int resume_on_alarm)
107 {
108         return 0;
109 }
110
111 static unsigned long collie_charger_wakeup(void)
112 {
113         return 0;
114 }
115
116 int collie_read_backup_battery(void)
117 {
118         int voltage;
119
120         ucb1x00_adc_enable(ucb);
121
122         ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_BBAT_ON, 0);
123         voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD1, UCB_SYNC);
124
125         ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_BBAT_ON);
126         ucb1x00_adc_disable(ucb);
127
128         printk("Backup battery = %d(%d)\n", ADCtoPower(voltage), voltage);
129
130         return ADCtoPower(voltage);
131 }
132
133 int collie_read_main_battery(void)
134 {
135         int voltage, voltage_rev, voltage_volts;
136
137         ucb1x00_adc_enable(ucb);
138         ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_BBAT_ON);
139         ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_MBAT_ON, 0);
140
141         mdelay(1);
142         voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD1, UCB_SYNC);
143
144         ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_MBAT_ON);
145         ucb1x00_adc_disable(ucb);
146
147         voltage_rev = voltage + ((ad_revise * voltage) / 652);
148         voltage_volts = ADCtoPower(voltage_rev);
149
150         printk("Main battery = %d(%d)\n", voltage_volts, voltage);
151
152         if (voltage != -1)
153                 return voltage_volts;
154         else
155                 return voltage;
156 }
157
158 int collie_read_temp(void)
159 {
160         int voltage;
161
162         /* According to Sharp, temp must be > 973, main battery must be < 465,
163            FIXME: sharpsl_pm.c has both conditions negated? FIXME: values
164            are way out of range? */
165
166         ucb1x00_adc_enable(ucb);
167         ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_TMP_ON, 0);
168         /* >1010 = battery removed, 460 = 22C ?, higher = lower temp ? */
169         voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD0, UCB_SYNC);
170         ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_TMP_ON);
171         ucb1x00_adc_disable(ucb);
172
173         printk("Battery temp = %d\n", voltage);
174         return voltage;
175 }
176
177 static unsigned long read_devdata(int which)
178 {
179         switch (which) {
180         case SHARPSL_BATT_VOLT:
181                 return collie_read_main_battery();
182         case SHARPSL_BATT_TEMP:
183                 return collie_read_temp();
184         case SHARPSL_ACIN_VOLT:
185                 return 500;
186         case SHARPSL_STATUS_ACIN: {
187                 int ret = GPLR & COLLIE_GPIO_AC_IN;
188                 printk("AC status = %d\n", ret);
189                 return ret;
190         }
191         case SHARPSL_STATUS_FATAL: {
192                 int ret = GPLR & COLLIE_GPIO_MAIN_BAT_LOW;
193                 printk("Fatal bat = %d\n", ret);
194                 return ret;
195         }
196         default:
197                 return ~0;
198         }
199 }
200
201 struct battery_thresh collie_battery_levels_acin[] = {
202         { 420, 100},
203         { 417,  95},
204         { 415,  90},
205         { 413,  80},
206         { 411,  75},
207         { 408,  70},
208         { 406,  60},
209         { 403,  50},
210         { 398,  40},
211         { 391,  25},
212         {  10,   5},
213         {   0,   0},
214 };
215
216 struct battery_thresh collie_battery_levels[] = {
217         { 394, 100},
218         { 390,  95},
219         { 380,  90},
220         { 370,  80},
221         { 368,  75},    /* From sharp code: battery high with frontlight */
222         { 366,  70},    /* 60..90 -- fake values invented by me for testing */
223         { 364,  60},
224         { 362,  50},
225         { 360,  40},
226         { 358,  25},    /* From sharp code: battery low with frontlight */
227         { 356,   5},    /* From sharp code: battery verylow with frontlight */
228         {   0,   0},
229 };
230
231 struct sharpsl_charger_machinfo collie_pm_machinfo = {
232         .init             = collie_charger_init,
233         .read_devdata     = read_devdata,
234         .discharge        = collie_discharge,
235         .discharge1       = collie_discharge1,
236         .charge           = collie_charge,
237         .measure_temp     = collie_measure_temp,
238         .presuspend       = collie_presuspend,
239         .postsuspend      = collie_postsuspend,
240         .charger_wakeup   = collie_charger_wakeup,
241         .should_wakeup    = collie_should_wakeup,
242         .bat_levels       = 12,
243         .bat_levels_noac  = collie_battery_levels,
244         .bat_levels_acin  = collie_battery_levels_acin,
245         .status_high_acin = 368,
246         .status_low_acin  = 358,
247         .status_high_noac = 368,
248         .status_low_noac  = 358,
249         .charge_on_volt   = 350,        /* spitz uses 2.90V, but lets play it safe. */
250         .charge_on_temp   = 550,
251         .charge_acin_high = 550,        /* collie does not seem to have sensor for this, anyway */
252         .charge_acin_low  = 450,        /* ignored, too */
253         .fatal_acin_volt  = 356,
254         .fatal_noacin_volt = 356,
255
256         .batfull_irq = 1,               /* We do not want periodical charge restarts */
257 };
258
259 static int __init collie_pm_ucb_add(struct ucb1x00_dev *pdev)
260 {
261         sharpsl_pm.machinfo = &collie_pm_machinfo;
262         ucb = pdev->ucb;
263         return 0;
264 }
265
266 static struct ucb1x00_driver collie_pm_ucb_driver = {
267         .add            = collie_pm_ucb_add,
268 };
269
270 static struct platform_device *collie_pm_device;
271
272 static int __init collie_pm_init(void)
273 {
274         int ret;
275
276         collie_pm_device = platform_device_alloc("sharpsl-pm", -1);
277         if (!collie_pm_device)
278                 return -ENOMEM;
279
280         collie_pm_device->dev.platform_data = &collie_pm_machinfo;
281         ret = platform_device_add(collie_pm_device);
282
283         if (ret)
284                 platform_device_put(collie_pm_device);
285
286         if (!ret)
287                 ret = ucb1x00_register_driver(&collie_pm_ucb_driver);
288
289         return ret;
290 }
291
292 static void __exit collie_pm_exit(void)
293 {
294         ucb1x00_unregister_driver(&collie_pm_ucb_driver);
295         platform_device_unregister(collie_pm_device);
296 }
297
298 module_init(collie_pm_init);
299 module_exit(collie_pm_exit);