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