Merge master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6] / arch / arm / common / sharpsl_pm.c
1 /*
2  * Battery and Power Management code for the Sharp SL-C7xx and SL-Cxx00
3  * series of PDAs
4  *
5  * Copyright (c) 2004-2005 Richard Purdie
6  *
7  * Based on code written by Sharp for 2.4 kernels
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  */
14
15 #undef DEBUG
16
17 #include <linux/module.h>
18 #include <linux/timer.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/apm_bios.h>
22 #include <linux/delay.h>
23 #include <linux/interrupt.h>
24 #include <linux/platform_device.h>
25 #include <linux/leds.h>
26
27 #include <asm/hardware.h>
28 #include <asm/mach-types.h>
29 #include <asm/irq.h>
30 #include <asm/apm.h>
31 #include <asm/arch/pm.h>
32 #include <asm/arch/pxa-regs.h>
33 #include <asm/arch/sharpsl.h>
34 #include <asm/hardware/sharpsl_pm.h>
35
36 /*
37  * Constants
38  */
39 #define SHARPSL_CHARGE_ON_TIME_INTERVAL        (msecs_to_jiffies(1*60*1000))  /* 1 min */
40 #define SHARPSL_CHARGE_FINISH_TIME             (msecs_to_jiffies(10*60*1000)) /* 10 min */
41 #define SHARPSL_BATCHK_TIME                    (msecs_to_jiffies(15*1000))    /* 15 sec */
42 #define SHARPSL_BATCHK_TIME_SUSPEND            (60*10)                        /* 10 min */
43 #define SHARPSL_WAIT_CO_TIME                   15  /* 15 sec */
44 #define SHARPSL_WAIT_DISCHARGE_ON              100 /* 100 msec */
45 #define SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP   10  /* 10 msec */
46 #define SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT   10  /* 10 msec */
47 #define SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN   10  /* 10 msec */
48 #define SHARPSL_CHARGE_WAIT_TIME               15  /* 15 msec */
49 #define SHARPSL_CHARGE_CO_CHECK_TIME           5   /* 5 msec */
50 #define SHARPSL_CHARGE_RETRY_CNT               1   /* eqv. 10 min */
51
52 /*
53  * Prototypes
54  */
55 static int sharpsl_off_charge_battery(void);
56 static int sharpsl_check_battery_temp(void);
57 static int sharpsl_check_battery_voltage(void);
58 static int sharpsl_ac_check(void);
59 static int sharpsl_fatal_check(void);
60 static int sharpsl_average_value(int ad);
61 static void sharpsl_average_clear(void);
62 static void sharpsl_charge_toggle(void *private_);
63 static void sharpsl_battery_thread(void *private_);
64
65
66 /*
67  * Variables
68  */
69 struct sharpsl_pm_status sharpsl_pm;
70 DECLARE_WORK(toggle_charger, sharpsl_charge_toggle, NULL);
71 DECLARE_WORK(sharpsl_bat, sharpsl_battery_thread, NULL);
72 DEFINE_LED_TRIGGER(sharpsl_charge_led_trigger);
73
74
75 static int get_percentage(int voltage)
76 {
77         int i = sharpsl_pm.machinfo->bat_levels - 1;
78         int bl_status = sharpsl_pm.machinfo->backlight_get_status ? sharpsl_pm.machinfo->backlight_get_status() : 0;
79         struct battery_thresh *thresh;
80
81         if (sharpsl_pm.charge_mode == CHRG_ON)
82                 thresh = bl_status ? sharpsl_pm.machinfo->bat_levels_acin_bl : sharpsl_pm.machinfo->bat_levels_acin;
83         else
84                 thresh = bl_status ? sharpsl_pm.machinfo->bat_levels_noac_bl : sharpsl_pm.machinfo->bat_levels_noac;
85
86         while (i > 0 && (voltage > thresh[i].voltage))
87                 i--;
88
89         return thresh[i].percentage;
90 }
91
92 static int get_apm_status(int voltage)
93 {
94         int low_thresh, high_thresh;
95
96         if (sharpsl_pm.charge_mode == CHRG_ON) {
97                 high_thresh = sharpsl_pm.machinfo->status_high_acin;
98                 low_thresh = sharpsl_pm.machinfo->status_low_acin;
99         } else {
100                 high_thresh = sharpsl_pm.machinfo->status_high_noac;
101                 low_thresh = sharpsl_pm.machinfo->status_low_noac;
102         }
103
104         if (voltage >= high_thresh)
105                 return APM_BATTERY_STATUS_HIGH;
106         if (voltage >= low_thresh)
107                 return APM_BATTERY_STATUS_LOW;
108         return APM_BATTERY_STATUS_CRITICAL;
109 }
110
111 void sharpsl_battery_kick(void)
112 {
113         schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(125));
114 }
115 EXPORT_SYMBOL(sharpsl_battery_kick);
116
117
118 static void sharpsl_battery_thread(void *private_)
119 {
120         int voltage, percent, apm_status, i = 0;
121
122         if (!sharpsl_pm.machinfo)
123                 return;
124
125         sharpsl_pm.battstat.ac_status = (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN) ? APM_AC_ONLINE : APM_AC_OFFLINE);
126
127         /* Corgi cannot confirm when battery fully charged so periodically kick! */
128         if (!sharpsl_pm.machinfo->batfull_irq && (sharpsl_pm.charge_mode == CHRG_ON)
129                         && time_after(jiffies, sharpsl_pm.charge_start_time +  SHARPSL_CHARGE_ON_TIME_INTERVAL))
130                 schedule_work(&toggle_charger);
131
132         while(1) {
133                 voltage = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
134
135                 if (voltage > 0) break;
136                 if (i++ > 5) {
137                         voltage = sharpsl_pm.machinfo->bat_levels_noac[0].voltage;
138                         dev_warn(sharpsl_pm.dev, "Warning: Cannot read main battery!\n");
139                         break;
140                 }
141         }
142
143         voltage = sharpsl_average_value(voltage);
144         apm_status = get_apm_status(voltage);
145         percent = get_percentage(voltage);
146
147         /* At low battery voltages, the voltage has a tendency to start
148            creeping back up so we try to avoid this here */
149         if ((sharpsl_pm.battstat.ac_status == APM_AC_ONLINE) || (apm_status == APM_BATTERY_STATUS_HIGH) ||  percent <= sharpsl_pm.battstat.mainbat_percent) {
150                 sharpsl_pm.battstat.mainbat_voltage = voltage;
151                 sharpsl_pm.battstat.mainbat_status = apm_status;
152                 sharpsl_pm.battstat.mainbat_percent = percent;
153         }
154
155         dev_dbg(sharpsl_pm.dev, "Battery: voltage: %d, status: %d, percentage: %d, time: %d\n", voltage,
156                         sharpsl_pm.battstat.mainbat_status, sharpsl_pm.battstat.mainbat_percent, jiffies);
157
158         /* If battery is low. limit backlight intensity to save power. */
159         if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
160                         && ((sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_LOW) ||
161                         (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL))) {
162                 if (!(sharpsl_pm.flags & SHARPSL_BL_LIMIT)) {
163                         sharpsl_pm.machinfo->backlight_limit(1);
164                         sharpsl_pm.flags |= SHARPSL_BL_LIMIT;
165                 }
166         } else if (sharpsl_pm.flags & SHARPSL_BL_LIMIT) {
167                 sharpsl_pm.machinfo->backlight_limit(0);
168                 sharpsl_pm.flags &= ~SHARPSL_BL_LIMIT;
169         }
170
171         /* Suspend if critical battery level */
172         if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
173                         && (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL)
174                         && !(sharpsl_pm.flags & SHARPSL_APM_QUEUED)) {
175                 sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
176                 dev_err(sharpsl_pm.dev, "Fatal Off\n");
177                 apm_queue_event(APM_CRITICAL_SUSPEND);
178         }
179
180         schedule_delayed_work(&sharpsl_bat, SHARPSL_BATCHK_TIME);
181 }
182
183 void sharpsl_pm_led(int val)
184 {
185         if (val == SHARPSL_LED_ERROR) {
186                 dev_err(sharpsl_pm.dev, "Charging Error!\n");
187         } else if (val == SHARPSL_LED_ON) {
188                 dev_dbg(sharpsl_pm.dev, "Charge LED On\n");
189                 led_trigger_event(sharpsl_charge_led_trigger, LED_FULL);
190         } else {
191                 dev_dbg(sharpsl_pm.dev, "Charge LED Off\n");
192                 led_trigger_event(sharpsl_charge_led_trigger, LED_OFF);
193         }
194 }
195
196 static void sharpsl_charge_on(void)
197 {
198         dev_dbg(sharpsl_pm.dev, "Turning Charger On\n");
199
200         sharpsl_pm.full_count = 0;
201         sharpsl_pm.charge_mode = CHRG_ON;
202         schedule_delayed_work(&toggle_charger, msecs_to_jiffies(250));
203         schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(500));
204 }
205
206 static void sharpsl_charge_off(void)
207 {
208         dev_dbg(sharpsl_pm.dev, "Turning Charger Off\n");
209
210         sharpsl_pm.machinfo->charge(0);
211         sharpsl_pm_led(SHARPSL_LED_OFF);
212         sharpsl_pm.charge_mode = CHRG_OFF;
213
214         schedule_work(&sharpsl_bat);
215 }
216
217 static void sharpsl_charge_error(void)
218 {
219         sharpsl_pm_led(SHARPSL_LED_ERROR);
220         sharpsl_pm.machinfo->charge(0);
221         sharpsl_pm.charge_mode = CHRG_ERROR;
222 }
223
224 static void sharpsl_charge_toggle(void *private_)
225 {
226         dev_dbg(sharpsl_pm.dev, "Toogling Charger at time: %lx\n", jiffies);
227
228         if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
229                 sharpsl_charge_off();
230                 return;
231         } else if ((sharpsl_check_battery_temp() < 0) || (sharpsl_ac_check() < 0)) {
232                 sharpsl_charge_error();
233                 return;
234         }
235
236         sharpsl_pm_led(SHARPSL_LED_ON);
237         sharpsl_pm.machinfo->charge(0);
238         mdelay(SHARPSL_CHARGE_WAIT_TIME);
239         sharpsl_pm.machinfo->charge(1);
240
241         sharpsl_pm.charge_start_time = jiffies;
242 }
243
244 static void sharpsl_ac_timer(unsigned long data)
245 {
246         int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
247
248         dev_dbg(sharpsl_pm.dev, "AC Status: %d\n",acin);
249
250         sharpsl_average_clear();
251         if (acin && (sharpsl_pm.charge_mode != CHRG_ON))
252                 sharpsl_charge_on();
253         else if (sharpsl_pm.charge_mode == CHRG_ON)
254                 sharpsl_charge_off();
255
256         schedule_work(&sharpsl_bat);
257 }
258
259
260 irqreturn_t sharpsl_ac_isr(int irq, void *dev_id, struct pt_regs *fp)
261 {
262         /* Delay the event slightly to debounce */
263         /* Must be a smaller delay than the chrg_full_isr below */
264         mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
265
266         return IRQ_HANDLED;
267 }
268
269 static void sharpsl_chrg_full_timer(unsigned long data)
270 {
271         dev_dbg(sharpsl_pm.dev, "Charge Full at time: %lx\n", jiffies);
272
273         sharpsl_pm.full_count++;
274
275         if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
276                 dev_dbg(sharpsl_pm.dev, "Charge Full: AC removed - stop charging!\n");
277                 if (sharpsl_pm.charge_mode == CHRG_ON)
278                         sharpsl_charge_off();
279         } else if (sharpsl_pm.full_count < 2) {
280                 dev_dbg(sharpsl_pm.dev, "Charge Full: Count too low\n");
281                 schedule_work(&toggle_charger);
282         } else if (time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_FINISH_TIME)) {
283                 dev_dbg(sharpsl_pm.dev, "Charge Full: Interrupt generated too slowly - retry.\n");
284                 schedule_work(&toggle_charger);
285         } else {
286                 sharpsl_charge_off();
287                 sharpsl_pm.charge_mode = CHRG_DONE;
288                 dev_dbg(sharpsl_pm.dev, "Charge Full: Charging Finished\n");
289         }
290 }
291
292 /* Charging Finished Interrupt (Not present on Corgi) */
293 /* Can trigger at the same time as an AC staus change so
294    delay until after that has been processed */
295 irqreturn_t sharpsl_chrg_full_isr(int irq, void *dev_id, struct pt_regs *fp)
296 {
297         if (sharpsl_pm.flags & SHARPSL_SUSPENDED)
298                 return IRQ_HANDLED;
299
300         /* delay until after any ac interrupt */
301         mod_timer(&sharpsl_pm.chrg_full_timer, jiffies + msecs_to_jiffies(500));
302
303         return IRQ_HANDLED;
304 }
305
306 irqreturn_t sharpsl_fatal_isr(int irq, void *dev_id, struct pt_regs *fp)
307 {
308         int is_fatal = 0;
309
310         if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) {
311                 dev_err(sharpsl_pm.dev, "Battery now Unlocked! Suspending.\n");
312                 is_fatal = 1;
313         }
314
315         if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_FATAL)) {
316                 dev_err(sharpsl_pm.dev, "Fatal Batt Error! Suspending.\n");
317                 is_fatal = 1;
318         }
319
320         if (!(sharpsl_pm.flags & SHARPSL_APM_QUEUED) && is_fatal) {
321                 sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
322                 apm_queue_event(APM_CRITICAL_SUSPEND);
323         }
324
325         return IRQ_HANDLED;
326 }
327
328 /*
329  * Maintain an average of the last 10 readings
330  */
331 #define SHARPSL_CNV_VALUE_NUM    10
332 static int sharpsl_ad_index;
333
334 static void sharpsl_average_clear(void)
335 {
336         sharpsl_ad_index = 0;
337 }
338
339 static int sharpsl_average_value(int ad)
340 {
341         int i, ad_val = 0;
342         static int sharpsl_ad[SHARPSL_CNV_VALUE_NUM+1];
343
344         if (sharpsl_pm.battstat.mainbat_status != APM_BATTERY_STATUS_HIGH) {
345                 sharpsl_ad_index = 0;
346                 return ad;
347         }
348
349         sharpsl_ad[sharpsl_ad_index] = ad;
350         sharpsl_ad_index++;
351         if (sharpsl_ad_index >= SHARPSL_CNV_VALUE_NUM) {
352                 for (i=0; i < (SHARPSL_CNV_VALUE_NUM-1); i++)
353                         sharpsl_ad[i] = sharpsl_ad[i+1];
354                 sharpsl_ad_index = SHARPSL_CNV_VALUE_NUM - 1;
355         }
356         for (i=0; i < sharpsl_ad_index; i++)
357                 ad_val += sharpsl_ad[i];
358
359         return (ad_val / sharpsl_ad_index);
360 }
361
362 /*
363  * Take an array of 5 integers, remove the maximum and minimum values
364  * and return the average.
365  */
366 static int get_select_val(int *val)
367 {
368         int i, j, k, temp, sum = 0;
369
370         /* Find MAX val */
371         temp = val[0];
372         j=0;
373         for (i=1; i<5; i++) {
374                 if (temp < val[i]) {
375                         temp = val[i];
376                         j = i;
377                 }
378         }
379
380         /* Find MIN val */
381         temp = val[4];
382         k=4;
383         for (i=3; i>=0; i--) {
384                 if (temp > val[i]) {
385                         temp = val[i];
386                         k = i;
387                 }
388         }
389
390         for (i=0; i<5; i++)
391                 if (i != j && i != k )
392                         sum += val[i];
393
394         dev_dbg(sharpsl_pm.dev, "Average: %d from values: %d, %d, %d, %d, %d\n", sum/3, val[0], val[1], val[2], val[3], val[4]);
395
396         return (sum/3);
397 }
398
399 static int sharpsl_check_battery_temp(void)
400 {
401         int val, i, buff[5];
402
403         /* Check battery temperature */
404         for (i=0; i<5; i++) {
405                 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
406                 sharpsl_pm.machinfo->measure_temp(1);
407                 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
408                 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_TEMP);
409                 sharpsl_pm.machinfo->measure_temp(0);
410         }
411
412         val = get_select_val(buff);
413
414         dev_dbg(sharpsl_pm.dev, "Temperature: %d\n", val);
415         if (val > sharpsl_pm.machinfo->charge_on_temp) {
416                 printk(KERN_WARNING "Not charging: temperature out of limits.\n");
417                 return -1;
418         }
419
420         return 0;
421 }
422
423 static int sharpsl_check_battery_voltage(void)
424 {
425         int val, i, buff[5];
426
427         /* disable charge, enable discharge */
428         sharpsl_pm.machinfo->charge(0);
429         sharpsl_pm.machinfo->discharge(1);
430         mdelay(SHARPSL_WAIT_DISCHARGE_ON);
431
432         if (sharpsl_pm.machinfo->discharge1)
433                 sharpsl_pm.machinfo->discharge1(1);
434
435         /* Check battery voltage */
436         for (i=0; i<5; i++) {
437                 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
438                 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
439         }
440
441         if (sharpsl_pm.machinfo->discharge1)
442                 sharpsl_pm.machinfo->discharge1(0);
443
444         sharpsl_pm.machinfo->discharge(0);
445
446         val = get_select_val(buff);
447         dev_dbg(sharpsl_pm.dev, "Battery Voltage: %d\n", val);
448
449         if (val < sharpsl_pm.machinfo->charge_on_volt)
450                 return -1;
451
452         return 0;
453 }
454
455 static int sharpsl_ac_check(void)
456 {
457         int temp, i, buff[5];
458
459         for (i=0; i<5; i++) {
460                 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_ACIN_VOLT);
461                 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN);
462         }
463
464         temp = get_select_val(buff);
465         dev_dbg(sharpsl_pm.dev, "AC Voltage: %d\n",temp);
466
467         if ((temp > sharpsl_pm.machinfo->charge_acin_high) || (temp < sharpsl_pm.machinfo->charge_acin_low)) {
468                 dev_err(sharpsl_pm.dev, "Error: AC check failed.\n");
469                 return -1;
470         }
471
472         return 0;
473 }
474
475 #ifdef CONFIG_PM
476 static int sharpsl_pm_suspend(struct platform_device *pdev, pm_message_t state)
477 {
478         sharpsl_pm.flags |= SHARPSL_SUSPENDED;
479         flush_scheduled_work();
480
481         if (sharpsl_pm.charge_mode == CHRG_ON)
482                 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
483         else
484                 sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
485
486         return 0;
487 }
488
489 static int sharpsl_pm_resume(struct platform_device *pdev)
490 {
491         /* Clear the reset source indicators as they break the bootloader upon reboot */
492         RCSR = 0x0f;
493         sharpsl_average_clear();
494         sharpsl_pm.flags &= ~SHARPSL_APM_QUEUED;
495         sharpsl_pm.flags &= ~SHARPSL_SUSPENDED;
496
497         return 0;
498 }
499
500 static void corgi_goto_sleep(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
501 {
502         dev_dbg(sharpsl_pm.dev, "Time is: %08x\n",RCNR);
503
504         dev_dbg(sharpsl_pm.dev, "Offline Charge Activate = %d\n",sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG);
505         /* not charging and AC-IN! */
506
507         if ((sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG) && (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN))) {
508                 dev_dbg(sharpsl_pm.dev, "Activating Offline Charger...\n");
509                 sharpsl_pm.charge_mode = CHRG_OFF;
510                 sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
511                 sharpsl_off_charge_battery();
512         }
513
514         sharpsl_pm.machinfo->presuspend();
515
516         PEDR = 0xffffffff; /* clear it */
517
518         sharpsl_pm.flags &= ~SHARPSL_ALARM_ACTIVE;
519         if ((sharpsl_pm.charge_mode == CHRG_ON) && ((alarm_enable && ((alarm_time - RCNR) > (SHARPSL_BATCHK_TIME_SUSPEND + 30))) || !alarm_enable)) {
520                 RTSR &= RTSR_ALE;
521                 RTAR = RCNR + SHARPSL_BATCHK_TIME_SUSPEND;
522                 dev_dbg(sharpsl_pm.dev, "Charging alarm at: %08x\n",RTAR);
523                 sharpsl_pm.flags |= SHARPSL_ALARM_ACTIVE;
524         } else if (alarm_enable) {
525                 RTSR &= RTSR_ALE;
526                 RTAR = alarm_time;
527                 dev_dbg(sharpsl_pm.dev, "User alarm at: %08x\n",RTAR);
528         } else {
529                 dev_dbg(sharpsl_pm.dev, "No alarms set.\n");
530         }
531
532         pxa_pm_enter(state);
533
534         sharpsl_pm.machinfo->postsuspend();
535
536         dev_dbg(sharpsl_pm.dev, "Corgi woken up from suspend: %08x\n",PEDR);
537 }
538
539 static int corgi_enter_suspend(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
540 {
541         if (!sharpsl_pm.machinfo->should_wakeup(!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE) && alarm_enable) )
542         {
543                 if (!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE)) {
544                         dev_dbg(sharpsl_pm.dev, "No user triggered wakeup events and not charging. Strange. Suspend.\n");
545                         corgi_goto_sleep(alarm_time, alarm_enable, state);
546                         return 1;
547                 }
548                 if(sharpsl_off_charge_battery()) {
549                         dev_dbg(sharpsl_pm.dev, "Charging. Suspend...\n");
550                         corgi_goto_sleep(alarm_time, alarm_enable, state);
551                         return 1;
552                 }
553                 dev_dbg(sharpsl_pm.dev, "User triggered wakeup in offline charger.\n");
554         }
555
556         if ((!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) || (sharpsl_fatal_check() < 0) )
557         {
558                 dev_err(sharpsl_pm.dev, "Fatal condition. Suspend.\n");
559                 corgi_goto_sleep(alarm_time, alarm_enable, state);
560                 return 1;
561         }
562
563         return 0;
564 }
565
566 static int corgi_pxa_pm_enter(suspend_state_t state)
567 {
568         unsigned long alarm_time = RTAR;
569         unsigned int alarm_status = ((RTSR & RTSR_ALE) != 0);
570
571         dev_dbg(sharpsl_pm.dev, "SharpSL suspending for first time.\n");
572
573         corgi_goto_sleep(alarm_time, alarm_status, state);
574
575         while (corgi_enter_suspend(alarm_time,alarm_status,state))
576                 {}
577
578         dev_dbg(sharpsl_pm.dev, "SharpSL resuming...\n");
579
580         return 0;
581 }
582 #endif
583
584
585 /*
586  * Check for fatal battery errors
587  * Fatal returns -1
588  */
589 static int sharpsl_fatal_check(void)
590 {
591         int buff[5], temp, i, acin;
592
593         dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check entered\n");
594
595         /* Check AC-Adapter */
596         acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
597
598         if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) {
599                 sharpsl_pm.machinfo->charge(0);
600                 udelay(100);
601                 sharpsl_pm.machinfo->discharge(1);      /* enable discharge */
602                 mdelay(SHARPSL_WAIT_DISCHARGE_ON);
603         }
604
605         if (sharpsl_pm.machinfo->discharge1)
606                 sharpsl_pm.machinfo->discharge1(1);
607
608         /* Check battery : check inserting battery ? */
609         for (i=0; i<5; i++) {
610                 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
611                 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
612         }
613
614         if (sharpsl_pm.machinfo->discharge1)
615                 sharpsl_pm.machinfo->discharge1(0);
616
617         if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) {
618                 udelay(100);
619                 sharpsl_pm.machinfo->charge(1);
620                 sharpsl_pm.machinfo->discharge(0);
621         }
622
623         temp = get_select_val(buff);
624         dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check: acin: %d, discharge voltage: %d, no discharge: %d\n", acin, temp, sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT));
625
626         if ((acin && (temp < sharpsl_pm.machinfo->fatal_acin_volt)) ||
627                         (!acin && (temp < sharpsl_pm.machinfo->fatal_noacin_volt)))
628                 return -1;
629         return 0;
630 }
631
632 static int sharpsl_off_charge_error(void)
633 {
634         dev_err(sharpsl_pm.dev, "Offline Charger: Error occured.\n");
635         sharpsl_pm.machinfo->charge(0);
636         sharpsl_pm_led(SHARPSL_LED_ERROR);
637         sharpsl_pm.charge_mode = CHRG_ERROR;
638         return 1;
639 }
640
641 /*
642  * Charging Control while suspended
643  * Return 1 - go straight to sleep
644  * Return 0 - sleep or wakeup depending on other factors
645  */
646 static int sharpsl_off_charge_battery(void)
647 {
648         int time;
649
650         dev_dbg(sharpsl_pm.dev, "Charge Mode: %d\n", sharpsl_pm.charge_mode);
651
652         if (sharpsl_pm.charge_mode == CHRG_OFF) {
653                 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 1\n");
654
655                 /* AC Check */
656                 if ((sharpsl_ac_check() < 0) || (sharpsl_check_battery_temp() < 0))
657                         return sharpsl_off_charge_error();
658
659                 /* Start Charging */
660                 sharpsl_pm_led(SHARPSL_LED_ON);
661                 sharpsl_pm.machinfo->charge(0);
662                 mdelay(SHARPSL_CHARGE_WAIT_TIME);
663                 sharpsl_pm.machinfo->charge(1);
664
665                 sharpsl_pm.charge_mode = CHRG_ON;
666                 sharpsl_pm.full_count = 0;
667
668                 return 1;
669         } else if (sharpsl_pm.charge_mode != CHRG_ON) {
670                 return 1;
671         }
672
673         if (sharpsl_pm.full_count == 0) {
674                 int time;
675
676                 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 2\n");
677
678                 if ((sharpsl_check_battery_temp() < 0) || (sharpsl_check_battery_voltage() < 0))
679                         return sharpsl_off_charge_error();
680
681                 sharpsl_pm.machinfo->charge(0);
682                 mdelay(SHARPSL_CHARGE_WAIT_TIME);
683                 sharpsl_pm.machinfo->charge(1);
684                 sharpsl_pm.charge_mode = CHRG_ON;
685
686                 mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
687
688                 time = RCNR;
689                 while(1) {
690                         /* Check if any wakeup event had occured */
691                         if (sharpsl_pm.machinfo->charger_wakeup() != 0)
692                                 return 0;
693                         /* Check for timeout */
694                         if ((RCNR - time) > SHARPSL_WAIT_CO_TIME)
695                                 return 1;
696                         if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) {
697                                 dev_dbg(sharpsl_pm.dev, "Offline Charger: Charge full occured. Retrying to check\n");
698                                 sharpsl_pm.full_count++;
699                                 sharpsl_pm.machinfo->charge(0);
700                                 mdelay(SHARPSL_CHARGE_WAIT_TIME);
701                                 sharpsl_pm.machinfo->charge(1);
702                                 return 1;
703                         }
704                 }
705         }
706
707         dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 3\n");
708
709         mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
710
711         time = RCNR;
712         while(1) {
713                 /* Check if any wakeup event had occured */
714                 if (sharpsl_pm.machinfo->charger_wakeup() != 0)
715                         return 0;
716                 /* Check for timeout */
717                 if ((RCNR-time) > SHARPSL_WAIT_CO_TIME) {
718                         if (sharpsl_pm.full_count > SHARPSL_CHARGE_RETRY_CNT) {
719                                 dev_dbg(sharpsl_pm.dev, "Offline Charger: Not charged sufficiently. Retrying.\n");
720                                 sharpsl_pm.full_count = 0;
721                         }
722                         sharpsl_pm.full_count++;
723                         return 1;
724                 }
725                 if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) {
726                         dev_dbg(sharpsl_pm.dev, "Offline Charger: Charging complete.\n");
727                         sharpsl_pm_led(SHARPSL_LED_OFF);
728                         sharpsl_pm.machinfo->charge(0);
729                         sharpsl_pm.charge_mode = CHRG_DONE;
730                         return 1;
731                 }
732         }
733 }
734
735
736 static ssize_t battery_percentage_show(struct device *dev, struct device_attribute *attr, char *buf)
737 {
738         return sprintf(buf, "%d\n",sharpsl_pm.battstat.mainbat_percent);
739 }
740
741 static ssize_t battery_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
742 {
743         return sprintf(buf, "%d\n",sharpsl_pm.battstat.mainbat_voltage);
744 }
745
746 static DEVICE_ATTR(battery_percentage, 0444, battery_percentage_show, NULL);
747 static DEVICE_ATTR(battery_voltage, 0444, battery_voltage_show, NULL);
748
749 extern void (*apm_get_power_status)(struct apm_power_info *);
750
751 static void sharpsl_apm_get_power_status(struct apm_power_info *info)
752 {
753         info->ac_line_status = sharpsl_pm.battstat.ac_status;
754
755         if (sharpsl_pm.charge_mode == CHRG_ON)
756                 info->battery_status = APM_BATTERY_STATUS_CHARGING;
757         else
758                 info->battery_status = sharpsl_pm.battstat.mainbat_status;
759
760         info->battery_flag = (1 << info->battery_status);
761         info->battery_life = sharpsl_pm.battstat.mainbat_percent;
762 }
763
764 static struct pm_ops sharpsl_pm_ops = {
765         .pm_disk_mode   = PM_DISK_FIRMWARE,
766         .prepare        = pxa_pm_prepare,
767         .enter          = corgi_pxa_pm_enter,
768         .finish         = pxa_pm_finish,
769 };
770
771 static int __init sharpsl_pm_probe(struct platform_device *pdev)
772 {
773         if (!pdev->dev.platform_data)
774                 return -EINVAL;
775
776         sharpsl_pm.dev = &pdev->dev;
777         sharpsl_pm.machinfo = pdev->dev.platform_data;
778         sharpsl_pm.charge_mode = CHRG_OFF;
779         sharpsl_pm.flags = 0;
780
781         init_timer(&sharpsl_pm.ac_timer);
782         sharpsl_pm.ac_timer.function = sharpsl_ac_timer;
783
784         init_timer(&sharpsl_pm.chrg_full_timer);
785         sharpsl_pm.chrg_full_timer.function = sharpsl_chrg_full_timer;
786
787         led_trigger_register_simple("sharpsl-charge", &sharpsl_charge_led_trigger);
788
789         sharpsl_pm.machinfo->init();
790
791         device_create_file(&pdev->dev, &dev_attr_battery_percentage);
792         device_create_file(&pdev->dev, &dev_attr_battery_voltage);
793
794         apm_get_power_status = sharpsl_apm_get_power_status;
795
796         pm_set_ops(&sharpsl_pm_ops);
797
798         mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
799
800         return 0;
801 }
802
803 static int sharpsl_pm_remove(struct platform_device *pdev)
804 {
805         pm_set_ops(NULL);
806
807         device_remove_file(&pdev->dev, &dev_attr_battery_percentage);
808         device_remove_file(&pdev->dev, &dev_attr_battery_voltage);
809
810         led_trigger_unregister_simple(sharpsl_charge_led_trigger);
811
812         sharpsl_pm.machinfo->exit();
813
814         del_timer_sync(&sharpsl_pm.chrg_full_timer);
815         del_timer_sync(&sharpsl_pm.ac_timer);
816
817         return 0;
818 }
819
820 static struct platform_driver sharpsl_pm_driver = {
821         .probe          = sharpsl_pm_probe,
822         .remove         = sharpsl_pm_remove,
823         .suspend        = sharpsl_pm_suspend,
824         .resume         = sharpsl_pm_resume,
825         .driver         = {
826                 .name           = "sharpsl-pm",
827         },
828 };
829
830 static int __devinit sharpsl_pm_init(void)
831 {
832         return platform_driver_register(&sharpsl_pm_driver);
833 }
834
835 static void sharpsl_pm_exit(void)
836 {
837         platform_driver_unregister(&sharpsl_pm_driver);
838 }
839
840 late_initcall(sharpsl_pm_init);
841 module_exit(sharpsl_pm_exit);