Merge branch 'locking-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[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 #include <linux/gpio.h>
26
27 #include <asm/irq.h>
28 #include <mach/hardware.h>
29 #include <asm/hardware/scoop.h>
30 #include <mach/dma.h>
31 #include <mach/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         gpio_request(COLLIE_GPIO_CHARGE_ON, "charge on");
63         gpio_direction_output(COLLIE_GPIO_CHARGE_ON, 1);
64
65         ucb1x00_io_set_dir(ucb, 0, COLLIE_TC35143_GPIO_MBAT_ON | COLLIE_TC35143_GPIO_TMP_ON |
66                                    COLLIE_TC35143_GPIO_BBAT_ON);
67         return;
68 }
69
70 static void collie_measure_temp(int on)
71 {
72         if (on)
73                 ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_TMP_ON, 0);
74         else
75                 ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_TMP_ON);
76 }
77
78 static void collie_charge(int on)
79 {
80         /* Zaurus seems to contain LTC1731; it should know when to
81          * stop charging itself, so setting charge on should be
82          * relatively harmless (as long as it is not done too often).
83          */
84         gpio_set_value(COLLIE_GPIO_CHARGE_ON, on);
85 }
86
87 static void collie_discharge(int on)
88 {
89 }
90
91 static void collie_discharge1(int on)
92 {
93 }
94
95 static void collie_presuspend(void)
96 {
97 }
98
99 static void collie_postsuspend(void)
100 {
101 }
102
103 static int collie_should_wakeup(unsigned int resume_on_alarm)
104 {
105         return 0;
106 }
107
108 static unsigned long collie_charger_wakeup(void)
109 {
110         return 0;
111 }
112
113 int collie_read_backup_battery(void)
114 {
115         int voltage;
116
117         ucb1x00_adc_enable(ucb);
118
119         ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_BBAT_ON, 0);
120         voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD1, UCB_SYNC);
121
122         ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_BBAT_ON);
123         ucb1x00_adc_disable(ucb);
124
125         printk("Backup battery = %d(%d)\n", ADCtoPower(voltage), voltage);
126
127         return ADCtoPower(voltage);
128 }
129
130 int collie_read_main_battery(void)
131 {
132         int voltage, voltage_rev, voltage_volts;
133
134         ucb1x00_adc_enable(ucb);
135         ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_BBAT_ON);
136         ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_MBAT_ON, 0);
137
138         mdelay(1);
139         voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD1, UCB_SYNC);
140
141         ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_MBAT_ON);
142         ucb1x00_adc_disable(ucb);
143
144         voltage_rev = voltage + ((ad_revise * voltage) / 652);
145         voltage_volts = ADCtoPower(voltage_rev);
146
147         printk("Main battery = %d(%d)\n", voltage_volts, voltage);
148
149         if (voltage != -1)
150                 return voltage_volts;
151         else
152                 return voltage;
153 }
154
155 int collie_read_temp(void)
156 {
157         int voltage;
158
159         /* According to Sharp, temp must be > 973, main battery must be < 465,
160            FIXME: sharpsl_pm.c has both conditions negated? FIXME: values
161            are way out of range? */
162
163         ucb1x00_adc_enable(ucb);
164         ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_TMP_ON, 0);
165         /* >1010 = battery removed, 460 = 22C ?, higher = lower temp ? */
166         voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD0, UCB_SYNC);
167         ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_TMP_ON);
168         ucb1x00_adc_disable(ucb);
169
170         printk("Battery temp = %d\n", voltage);
171         return voltage;
172 }
173
174 static unsigned long read_devdata(int which)
175 {
176         switch (which) {
177         case SHARPSL_BATT_VOLT:
178                 return collie_read_main_battery();
179         case SHARPSL_BATT_TEMP:
180                 return collie_read_temp();
181         case SHARPSL_ACIN_VOLT:
182                 return 500;
183         case SHARPSL_STATUS_ACIN: {
184                 int ret = GPLR & COLLIE_GPIO_AC_IN;
185                 printk("AC status = %d\n", ret);
186                 return ret;
187         }
188         case SHARPSL_STATUS_FATAL: {
189                 int ret = GPLR & COLLIE_GPIO_MAIN_BAT_LOW;
190                 printk("Fatal bat = %d\n", ret);
191                 return ret;
192         }
193         default:
194                 return ~0;
195         }
196 }
197
198 struct battery_thresh collie_battery_levels_acin[] = {
199         { 420, 100},
200         { 417,  95},
201         { 415,  90},
202         { 413,  80},
203         { 411,  75},
204         { 408,  70},
205         { 406,  60},
206         { 403,  50},
207         { 398,  40},
208         { 391,  25},
209         {  10,   5},
210         {   0,   0},
211 };
212
213 struct battery_thresh collie_battery_levels[] = {
214         { 394, 100},
215         { 390,  95},
216         { 380,  90},
217         { 370,  80},
218         { 368,  75},    /* From sharp code: battery high with frontlight */
219         { 366,  70},    /* 60..90 -- fake values invented by me for testing */
220         { 364,  60},
221         { 362,  50},
222         { 360,  40},
223         { 358,  25},    /* From sharp code: battery low with frontlight */
224         { 356,   5},    /* From sharp code: battery verylow with frontlight */
225         {   0,   0},
226 };
227
228 struct sharpsl_charger_machinfo collie_pm_machinfo = {
229         .init             = collie_charger_init,
230         .read_devdata     = read_devdata,
231         .discharge        = collie_discharge,
232         .discharge1       = collie_discharge1,
233         .charge           = collie_charge,
234         .measure_temp     = collie_measure_temp,
235         .presuspend       = collie_presuspend,
236         .postsuspend      = collie_postsuspend,
237         .charger_wakeup   = collie_charger_wakeup,
238         .should_wakeup    = collie_should_wakeup,
239         .bat_levels       = 12,
240         .bat_levels_noac  = collie_battery_levels,
241         .bat_levels_acin  = collie_battery_levels_acin,
242         .status_high_acin = 368,
243         .status_low_acin  = 358,
244         .status_high_noac = 368,
245         .status_low_noac  = 358,
246         .charge_on_volt   = 350,        /* spitz uses 2.90V, but lets play it safe. */
247         .charge_on_temp   = 550,
248         .charge_acin_high = 550,        /* collie does not seem to have sensor for this, anyway */
249         .charge_acin_low  = 450,        /* ignored, too */
250         .fatal_acin_volt  = 356,
251         .fatal_noacin_volt = 356,
252
253         .batfull_irq = 1,               /* We do not want periodical charge restarts */
254 };
255
256 static int __init collie_pm_ucb_add(struct ucb1x00_dev *pdev)
257 {
258         sharpsl_pm.machinfo = &collie_pm_machinfo;
259         ucb = pdev->ucb;
260         return 0;
261 }
262
263 static struct ucb1x00_driver collie_pm_ucb_driver = {
264         .add    = collie_pm_ucb_add,
265 };
266
267 static struct platform_device *collie_pm_device;
268
269 static int __init collie_pm_init(void)
270 {
271         int ret;
272
273         collie_pm_device = platform_device_alloc("sharpsl-pm", -1);
274         if (!collie_pm_device)
275                 return -ENOMEM;
276
277         collie_pm_device->dev.platform_data = &collie_pm_machinfo;
278         ret = platform_device_add(collie_pm_device);
279
280         if (ret)
281                 platform_device_put(collie_pm_device);
282
283         if (!ret)
284                 ret = ucb1x00_register_driver(&collie_pm_ucb_driver);
285
286         return ret;
287 }
288
289 static void __exit collie_pm_exit(void)
290 {
291         ucb1x00_unregister_driver(&collie_pm_ucb_driver);
292         platform_device_unregister(collie_pm_device);
293 }
294
295 module_init(collie_pm_init);
296 module_exit(collie_pm_exit);