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