Linux 2.6.31.8
[linux/fpc-iii.git] / arch / arm / mach-pxa / sharpsl_pm.c
blob629e05d1196ef7d24a5382d91b767c3bca04ff4c
1 /*
2 * Battery and Power Management code for the Sharp SL-C7xx and SL-Cxx00
3 * series of PDAs
5 * Copyright (c) 2004-2005 Richard Purdie
7 * Based on code written by Sharp for 2.4 kernels
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.
15 #undef DEBUG
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <linux/apm-emulation.h>
22 #include <linux/timer.h>
23 #include <linux/delay.h>
24 #include <linux/leds.h>
25 #include <linux/suspend.h>
26 #include <linux/gpio.h>
28 #include <asm/mach-types.h>
29 #include <mach/pm.h>
30 #include <mach/pxa2xx-regs.h>
31 #include <mach/pxa2xx-gpio.h>
32 #include <mach/regs-rtc.h>
33 #include <mach/sharpsl.h>
34 #include <mach/sharpsl_pm.h>
36 #include "sharpsl.h"
39 * Constants
41 #define SHARPSL_CHARGE_ON_TIME_INTERVAL (msecs_to_jiffies(1*60*1000)) /* 1 min */
42 #define SHARPSL_CHARGE_FINISH_TIME (msecs_to_jiffies(10*60*1000)) /* 10 min */
43 #define SHARPSL_BATCHK_TIME (msecs_to_jiffies(15*1000)) /* 15 sec */
44 #define SHARPSL_BATCHK_TIME_SUSPEND (60*10) /* 10 min */
46 #define SHARPSL_WAIT_CO_TIME 15 /* 15 sec */
47 #define SHARPSL_WAIT_DISCHARGE_ON 100 /* 100 msec */
48 #define SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP 10 /* 10 msec */
49 #define SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT 10 /* 10 msec */
50 #define SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN 10 /* 10 msec */
51 #define SHARPSL_CHARGE_WAIT_TIME 15 /* 15 msec */
52 #define SHARPSL_CHARGE_CO_CHECK_TIME 5 /* 5 msec */
53 #define SHARPSL_CHARGE_RETRY_CNT 1 /* eqv. 10 min */
56 * Prototypes
58 #ifdef CONFIG_PM
59 static int sharpsl_off_charge_battery(void);
60 static int sharpsl_check_battery_voltage(void);
61 static int sharpsl_fatal_check(void);
62 #endif
63 static int sharpsl_check_battery_temp(void);
64 static int sharpsl_ac_check(void);
65 static int sharpsl_average_value(int ad);
66 static void sharpsl_average_clear(void);
67 static void sharpsl_charge_toggle(struct work_struct *private_);
68 static void sharpsl_battery_thread(struct work_struct *private_);
72 * Variables
74 struct sharpsl_pm_status sharpsl_pm;
75 static DECLARE_DELAYED_WORK(toggle_charger, sharpsl_charge_toggle);
76 static DECLARE_DELAYED_WORK(sharpsl_bat, sharpsl_battery_thread);
77 DEFINE_LED_TRIGGER(sharpsl_charge_led_trigger);
81 struct battery_thresh spitz_battery_levels_acin[] = {
82 { 213, 100},
83 { 212, 98},
84 { 211, 95},
85 { 210, 93},
86 { 209, 90},
87 { 208, 88},
88 { 207, 85},
89 { 206, 83},
90 { 205, 80},
91 { 204, 78},
92 { 203, 75},
93 { 202, 73},
94 { 201, 70},
95 { 200, 68},
96 { 199, 65},
97 { 198, 63},
98 { 197, 60},
99 { 196, 58},
100 { 195, 55},
101 { 194, 53},
102 { 193, 50},
103 { 192, 48},
104 { 192, 45},
105 { 191, 43},
106 { 191, 40},
107 { 190, 38},
108 { 190, 35},
109 { 189, 33},
110 { 188, 30},
111 { 187, 28},
112 { 186, 25},
113 { 185, 23},
114 { 184, 20},
115 { 183, 18},
116 { 182, 15},
117 { 181, 13},
118 { 180, 10},
119 { 179, 8},
120 { 178, 5},
121 { 0, 0},
124 struct battery_thresh spitz_battery_levels_noac[] = {
125 { 213, 100},
126 { 212, 98},
127 { 211, 95},
128 { 210, 93},
129 { 209, 90},
130 { 208, 88},
131 { 207, 85},
132 { 206, 83},
133 { 205, 80},
134 { 204, 78},
135 { 203, 75},
136 { 202, 73},
137 { 201, 70},
138 { 200, 68},
139 { 199, 65},
140 { 198, 63},
141 { 197, 60},
142 { 196, 58},
143 { 195, 55},
144 { 194, 53},
145 { 193, 50},
146 { 192, 48},
147 { 191, 45},
148 { 190, 43},
149 { 189, 40},
150 { 188, 38},
151 { 187, 35},
152 { 186, 33},
153 { 185, 30},
154 { 184, 28},
155 { 183, 25},
156 { 182, 23},
157 { 181, 20},
158 { 180, 18},
159 { 179, 15},
160 { 178, 13},
161 { 177, 10},
162 { 176, 8},
163 { 175, 5},
164 { 0, 0},
167 /* MAX1111 Commands */
168 #define MAXCTRL_PD0 1u << 0
169 #define MAXCTRL_PD1 1u << 1
170 #define MAXCTRL_SGL 1u << 2
171 #define MAXCTRL_UNI 1u << 3
172 #define MAXCTRL_SEL_SH 4
173 #define MAXCTRL_STR 1u << 7
176 * Read MAX1111 ADC
178 int sharpsl_pm_pxa_read_max1111(int channel)
180 if (machine_is_tosa()) // Ugly, better move this function into another module
181 return 0;
183 #ifdef CONFIG_CORGI_SSP_DEPRECATED
184 return corgi_ssp_max1111_get((channel << MAXCTRL_SEL_SH) | MAXCTRL_PD0 | MAXCTRL_PD1
185 | MAXCTRL_SGL | MAXCTRL_UNI | MAXCTRL_STR);
186 #else
187 extern int max1111_read_channel(int);
189 /* max1111 accepts channels from 0-3, however,
190 * it is encoded from 0-7 here in the code.
192 return max1111_read_channel(channel >> 1);
193 #endif
196 static int get_percentage(int voltage)
198 int i = sharpsl_pm.machinfo->bat_levels - 1;
199 int bl_status = sharpsl_pm.machinfo->backlight_get_status ? sharpsl_pm.machinfo->backlight_get_status() : 0;
200 struct battery_thresh *thresh;
202 if (sharpsl_pm.charge_mode == CHRG_ON)
203 thresh = bl_status ? sharpsl_pm.machinfo->bat_levels_acin_bl : sharpsl_pm.machinfo->bat_levels_acin;
204 else
205 thresh = bl_status ? sharpsl_pm.machinfo->bat_levels_noac_bl : sharpsl_pm.machinfo->bat_levels_noac;
207 while (i > 0 && (voltage > thresh[i].voltage))
208 i--;
210 return thresh[i].percentage;
213 static int get_apm_status(int voltage)
215 int low_thresh, high_thresh;
217 if (sharpsl_pm.charge_mode == CHRG_ON) {
218 high_thresh = sharpsl_pm.machinfo->status_high_acin;
219 low_thresh = sharpsl_pm.machinfo->status_low_acin;
220 } else {
221 high_thresh = sharpsl_pm.machinfo->status_high_noac;
222 low_thresh = sharpsl_pm.machinfo->status_low_noac;
225 if (voltage >= high_thresh)
226 return APM_BATTERY_STATUS_HIGH;
227 if (voltage >= low_thresh)
228 return APM_BATTERY_STATUS_LOW;
229 return APM_BATTERY_STATUS_CRITICAL;
232 void sharpsl_battery_kick(void)
234 schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(125));
236 EXPORT_SYMBOL(sharpsl_battery_kick);
239 static void sharpsl_battery_thread(struct work_struct *private_)
241 int voltage, percent, apm_status, i = 0;
243 if (!sharpsl_pm.machinfo)
244 return;
246 sharpsl_pm.battstat.ac_status = (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN) ? APM_AC_ONLINE : APM_AC_OFFLINE);
248 /* Corgi cannot confirm when battery fully charged so periodically kick! */
249 if (!sharpsl_pm.machinfo->batfull_irq && (sharpsl_pm.charge_mode == CHRG_ON)
250 && time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_ON_TIME_INTERVAL))
251 schedule_delayed_work(&toggle_charger, 0);
253 while(1) {
254 voltage = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
256 if (voltage > 0) break;
257 if (i++ > 5) {
258 voltage = sharpsl_pm.machinfo->bat_levels_noac[0].voltage;
259 dev_warn(sharpsl_pm.dev, "Warning: Cannot read main battery!\n");
260 break;
264 voltage = sharpsl_average_value(voltage);
265 apm_status = get_apm_status(voltage);
266 percent = get_percentage(voltage);
268 /* At low battery voltages, the voltage has a tendency to start
269 creeping back up so we try to avoid this here */
270 if ((sharpsl_pm.battstat.ac_status == APM_AC_ONLINE) || (apm_status == APM_BATTERY_STATUS_HIGH) || percent <= sharpsl_pm.battstat.mainbat_percent) {
271 sharpsl_pm.battstat.mainbat_voltage = voltage;
272 sharpsl_pm.battstat.mainbat_status = apm_status;
273 sharpsl_pm.battstat.mainbat_percent = percent;
276 dev_dbg(sharpsl_pm.dev, "Battery: voltage: %d, status: %d, percentage: %d, time: %ld\n", voltage,
277 sharpsl_pm.battstat.mainbat_status, sharpsl_pm.battstat.mainbat_percent, jiffies);
279 #ifdef CONFIG_BACKLIGHT_CORGI
280 /* If battery is low. limit backlight intensity to save power. */
281 if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
282 && ((sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_LOW) ||
283 (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL))) {
284 if (!(sharpsl_pm.flags & SHARPSL_BL_LIMIT)) {
285 sharpsl_pm.machinfo->backlight_limit(1);
286 sharpsl_pm.flags |= SHARPSL_BL_LIMIT;
288 } else if (sharpsl_pm.flags & SHARPSL_BL_LIMIT) {
289 sharpsl_pm.machinfo->backlight_limit(0);
290 sharpsl_pm.flags &= ~SHARPSL_BL_LIMIT;
292 #endif
294 /* Suspend if critical battery level */
295 if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
296 && (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL)
297 && !(sharpsl_pm.flags & SHARPSL_APM_QUEUED)) {
298 sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
299 dev_err(sharpsl_pm.dev, "Fatal Off\n");
300 apm_queue_event(APM_CRITICAL_SUSPEND);
303 schedule_delayed_work(&sharpsl_bat, SHARPSL_BATCHK_TIME);
306 void sharpsl_pm_led(int val)
308 if (val == SHARPSL_LED_ERROR) {
309 dev_err(sharpsl_pm.dev, "Charging Error!\n");
310 } else if (val == SHARPSL_LED_ON) {
311 dev_dbg(sharpsl_pm.dev, "Charge LED On\n");
312 led_trigger_event(sharpsl_charge_led_trigger, LED_FULL);
313 } else {
314 dev_dbg(sharpsl_pm.dev, "Charge LED Off\n");
315 led_trigger_event(sharpsl_charge_led_trigger, LED_OFF);
319 static void sharpsl_charge_on(void)
321 dev_dbg(sharpsl_pm.dev, "Turning Charger On\n");
323 sharpsl_pm.full_count = 0;
324 sharpsl_pm.charge_mode = CHRG_ON;
325 schedule_delayed_work(&toggle_charger, msecs_to_jiffies(250));
326 schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(500));
329 static void sharpsl_charge_off(void)
331 dev_dbg(sharpsl_pm.dev, "Turning Charger Off\n");
333 sharpsl_pm.machinfo->charge(0);
334 sharpsl_pm_led(SHARPSL_LED_OFF);
335 sharpsl_pm.charge_mode = CHRG_OFF;
337 schedule_delayed_work(&sharpsl_bat, 0);
340 static void sharpsl_charge_error(void)
342 sharpsl_pm_led(SHARPSL_LED_ERROR);
343 sharpsl_pm.machinfo->charge(0);
344 sharpsl_pm.charge_mode = CHRG_ERROR;
347 static void sharpsl_charge_toggle(struct work_struct *private_)
349 dev_dbg(sharpsl_pm.dev, "Toogling Charger at time: %lx\n", jiffies);
351 if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
352 sharpsl_charge_off();
353 return;
354 } else if ((sharpsl_check_battery_temp() < 0) || (sharpsl_ac_check() < 0)) {
355 sharpsl_charge_error();
356 return;
359 sharpsl_pm_led(SHARPSL_LED_ON);
360 sharpsl_pm.machinfo->charge(0);
361 mdelay(SHARPSL_CHARGE_WAIT_TIME);
362 sharpsl_pm.machinfo->charge(1);
364 sharpsl_pm.charge_start_time = jiffies;
367 static void sharpsl_ac_timer(unsigned long data)
369 int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
371 dev_dbg(sharpsl_pm.dev, "AC Status: %d\n",acin);
373 sharpsl_average_clear();
374 if (acin && (sharpsl_pm.charge_mode != CHRG_ON))
375 sharpsl_charge_on();
376 else if (sharpsl_pm.charge_mode == CHRG_ON)
377 sharpsl_charge_off();
379 schedule_delayed_work(&sharpsl_bat, 0);
383 static irqreturn_t sharpsl_ac_isr(int irq, void *dev_id)
385 /* Delay the event slightly to debounce */
386 /* Must be a smaller delay than the chrg_full_isr below */
387 mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
389 return IRQ_HANDLED;
392 static void sharpsl_chrg_full_timer(unsigned long data)
394 dev_dbg(sharpsl_pm.dev, "Charge Full at time: %lx\n", jiffies);
396 sharpsl_pm.full_count++;
398 if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
399 dev_dbg(sharpsl_pm.dev, "Charge Full: AC removed - stop charging!\n");
400 if (sharpsl_pm.charge_mode == CHRG_ON)
401 sharpsl_charge_off();
402 } else if (sharpsl_pm.full_count < 2) {
403 dev_dbg(sharpsl_pm.dev, "Charge Full: Count too low\n");
404 schedule_delayed_work(&toggle_charger, 0);
405 } else if (time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_FINISH_TIME)) {
406 dev_dbg(sharpsl_pm.dev, "Charge Full: Interrupt generated too slowly - retry.\n");
407 schedule_delayed_work(&toggle_charger, 0);
408 } else {
409 sharpsl_charge_off();
410 sharpsl_pm.charge_mode = CHRG_DONE;
411 dev_dbg(sharpsl_pm.dev, "Charge Full: Charging Finished\n");
415 /* Charging Finished Interrupt (Not present on Corgi) */
416 /* Can trigger at the same time as an AC status change so
417 delay until after that has been processed */
418 static irqreturn_t sharpsl_chrg_full_isr(int irq, void *dev_id)
420 if (sharpsl_pm.flags & SHARPSL_SUSPENDED)
421 return IRQ_HANDLED;
423 /* delay until after any ac interrupt */
424 mod_timer(&sharpsl_pm.chrg_full_timer, jiffies + msecs_to_jiffies(500));
426 return IRQ_HANDLED;
429 static irqreturn_t sharpsl_fatal_isr(int irq, void *dev_id)
431 int is_fatal = 0;
433 if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) {
434 dev_err(sharpsl_pm.dev, "Battery now Unlocked! Suspending.\n");
435 is_fatal = 1;
438 if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_FATAL)) {
439 dev_err(sharpsl_pm.dev, "Fatal Batt Error! Suspending.\n");
440 is_fatal = 1;
443 if (!(sharpsl_pm.flags & SHARPSL_APM_QUEUED) && is_fatal) {
444 sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
445 apm_queue_event(APM_CRITICAL_SUSPEND);
448 return IRQ_HANDLED;
452 * Maintain an average of the last 10 readings
454 #define SHARPSL_CNV_VALUE_NUM 10
455 static int sharpsl_ad_index;
457 static void sharpsl_average_clear(void)
459 sharpsl_ad_index = 0;
462 static int sharpsl_average_value(int ad)
464 int i, ad_val = 0;
465 static int sharpsl_ad[SHARPSL_CNV_VALUE_NUM+1];
467 if (sharpsl_pm.battstat.mainbat_status != APM_BATTERY_STATUS_HIGH) {
468 sharpsl_ad_index = 0;
469 return ad;
472 sharpsl_ad[sharpsl_ad_index] = ad;
473 sharpsl_ad_index++;
474 if (sharpsl_ad_index >= SHARPSL_CNV_VALUE_NUM) {
475 for (i=0; i < (SHARPSL_CNV_VALUE_NUM-1); i++)
476 sharpsl_ad[i] = sharpsl_ad[i+1];
477 sharpsl_ad_index = SHARPSL_CNV_VALUE_NUM - 1;
479 for (i=0; i < sharpsl_ad_index; i++)
480 ad_val += sharpsl_ad[i];
482 return (ad_val / sharpsl_ad_index);
486 * Take an array of 5 integers, remove the maximum and minimum values
487 * and return the average.
489 static int get_select_val(int *val)
491 int i, j, k, temp, sum = 0;
493 /* Find MAX val */
494 temp = val[0];
495 j=0;
496 for (i=1; i<5; i++) {
497 if (temp < val[i]) {
498 temp = val[i];
499 j = i;
503 /* Find MIN val */
504 temp = val[4];
505 k=4;
506 for (i=3; i>=0; i--) {
507 if (temp > val[i]) {
508 temp = val[i];
509 k = i;
513 for (i=0; i<5; i++)
514 if (i != j && i != k )
515 sum += val[i];
517 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]);
519 return (sum/3);
522 static int sharpsl_check_battery_temp(void)
524 int val, i, buff[5];
526 /* Check battery temperature */
527 for (i=0; i<5; i++) {
528 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
529 sharpsl_pm.machinfo->measure_temp(1);
530 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
531 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_TEMP);
532 sharpsl_pm.machinfo->measure_temp(0);
535 val = get_select_val(buff);
537 dev_dbg(sharpsl_pm.dev, "Temperature: %d\n", val);
538 if (val > sharpsl_pm.machinfo->charge_on_temp) {
539 printk(KERN_WARNING "Not charging: temperature out of limits.\n");
540 return -1;
543 return 0;
546 #ifdef CONFIG_PM
547 static int sharpsl_check_battery_voltage(void)
549 int val, i, buff[5];
551 /* disable charge, enable discharge */
552 sharpsl_pm.machinfo->charge(0);
553 sharpsl_pm.machinfo->discharge(1);
554 mdelay(SHARPSL_WAIT_DISCHARGE_ON);
556 if (sharpsl_pm.machinfo->discharge1)
557 sharpsl_pm.machinfo->discharge1(1);
559 /* Check battery voltage */
560 for (i=0; i<5; i++) {
561 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
562 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
565 if (sharpsl_pm.machinfo->discharge1)
566 sharpsl_pm.machinfo->discharge1(0);
568 sharpsl_pm.machinfo->discharge(0);
570 val = get_select_val(buff);
571 dev_dbg(sharpsl_pm.dev, "Battery Voltage: %d\n", val);
573 if (val < sharpsl_pm.machinfo->charge_on_volt)
574 return -1;
576 return 0;
578 #endif
580 static int sharpsl_ac_check(void)
582 int temp, i, buff[5];
584 for (i=0; i<5; i++) {
585 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_ACIN_VOLT);
586 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN);
589 temp = get_select_val(buff);
590 dev_dbg(sharpsl_pm.dev, "AC Voltage: %d\n",temp);
592 if ((temp > sharpsl_pm.machinfo->charge_acin_high) || (temp < sharpsl_pm.machinfo->charge_acin_low)) {
593 dev_err(sharpsl_pm.dev, "Error: AC check failed.\n");
594 return -1;
597 return 0;
600 #ifdef CONFIG_PM
601 static int sharpsl_pm_suspend(struct platform_device *pdev, pm_message_t state)
603 sharpsl_pm.flags |= SHARPSL_SUSPENDED;
604 flush_scheduled_work();
606 if (sharpsl_pm.charge_mode == CHRG_ON)
607 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
608 else
609 sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
611 return 0;
614 static int sharpsl_pm_resume(struct platform_device *pdev)
616 /* Clear the reset source indicators as they break the bootloader upon reboot */
617 RCSR = 0x0f;
618 sharpsl_average_clear();
619 sharpsl_pm.flags &= ~SHARPSL_APM_QUEUED;
620 sharpsl_pm.flags &= ~SHARPSL_SUSPENDED;
622 return 0;
625 static void corgi_goto_sleep(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
627 dev_dbg(sharpsl_pm.dev, "Time is: %08x\n",RCNR);
629 dev_dbg(sharpsl_pm.dev, "Offline Charge Activate = %d\n",sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG);
630 /* not charging and AC-IN! */
632 if ((sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG) && (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN))) {
633 dev_dbg(sharpsl_pm.dev, "Activating Offline Charger...\n");
634 sharpsl_pm.charge_mode = CHRG_OFF;
635 sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
636 sharpsl_off_charge_battery();
639 sharpsl_pm.machinfo->presuspend();
641 PEDR = 0xffffffff; /* clear it */
643 sharpsl_pm.flags &= ~SHARPSL_ALARM_ACTIVE;
644 if ((sharpsl_pm.charge_mode == CHRG_ON) && ((alarm_enable && ((alarm_time - RCNR) > (SHARPSL_BATCHK_TIME_SUSPEND + 30))) || !alarm_enable)) {
645 RTSR &= RTSR_ALE;
646 RTAR = RCNR + SHARPSL_BATCHK_TIME_SUSPEND;
647 dev_dbg(sharpsl_pm.dev, "Charging alarm at: %08x\n",RTAR);
648 sharpsl_pm.flags |= SHARPSL_ALARM_ACTIVE;
649 } else if (alarm_enable) {
650 RTSR &= RTSR_ALE;
651 RTAR = alarm_time;
652 dev_dbg(sharpsl_pm.dev, "User alarm at: %08x\n",RTAR);
653 } else {
654 dev_dbg(sharpsl_pm.dev, "No alarms set.\n");
657 pxa_pm_enter(state);
659 sharpsl_pm.machinfo->postsuspend();
661 dev_dbg(sharpsl_pm.dev, "Corgi woken up from suspend: %08x\n",PEDR);
664 static int corgi_enter_suspend(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
666 if (!sharpsl_pm.machinfo->should_wakeup(!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE) && alarm_enable) )
668 if (!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE)) {
669 dev_dbg(sharpsl_pm.dev, "No user triggered wakeup events and not charging. Strange. Suspend.\n");
670 corgi_goto_sleep(alarm_time, alarm_enable, state);
671 return 1;
673 if(sharpsl_off_charge_battery()) {
674 dev_dbg(sharpsl_pm.dev, "Charging. Suspend...\n");
675 corgi_goto_sleep(alarm_time, alarm_enable, state);
676 return 1;
678 dev_dbg(sharpsl_pm.dev, "User triggered wakeup in offline charger.\n");
681 if ((!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) ||
682 (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_FATAL))) {
683 dev_err(sharpsl_pm.dev, "Fatal condition. Suspend.\n");
684 corgi_goto_sleep(alarm_time, alarm_enable, state);
685 return 1;
688 return 0;
691 static int corgi_pxa_pm_enter(suspend_state_t state)
693 unsigned long alarm_time = RTAR;
694 unsigned int alarm_status = ((RTSR & RTSR_ALE) != 0);
696 dev_dbg(sharpsl_pm.dev, "SharpSL suspending for first time.\n");
698 corgi_goto_sleep(alarm_time, alarm_status, state);
700 while (corgi_enter_suspend(alarm_time,alarm_status,state))
703 if (sharpsl_pm.machinfo->earlyresume)
704 sharpsl_pm.machinfo->earlyresume();
706 dev_dbg(sharpsl_pm.dev, "SharpSL resuming...\n");
708 return 0;
712 * Check for fatal battery errors
713 * Fatal returns -1
715 static int sharpsl_fatal_check(void)
717 int buff[5], temp, i, acin;
719 dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check entered\n");
721 /* Check AC-Adapter */
722 acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
724 if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) {
725 sharpsl_pm.machinfo->charge(0);
726 udelay(100);
727 sharpsl_pm.machinfo->discharge(1); /* enable discharge */
728 mdelay(SHARPSL_WAIT_DISCHARGE_ON);
731 if (sharpsl_pm.machinfo->discharge1)
732 sharpsl_pm.machinfo->discharge1(1);
734 /* Check battery : check inserting battery ? */
735 for (i=0; i<5; i++) {
736 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
737 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
740 if (sharpsl_pm.machinfo->discharge1)
741 sharpsl_pm.machinfo->discharge1(0);
743 if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) {
744 udelay(100);
745 sharpsl_pm.machinfo->charge(1);
746 sharpsl_pm.machinfo->discharge(0);
749 temp = get_select_val(buff);
750 dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check: acin: %d, discharge voltage: %d, no discharge: %ld\n", acin, temp, sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT));
752 if ((acin && (temp < sharpsl_pm.machinfo->fatal_acin_volt)) ||
753 (!acin && (temp < sharpsl_pm.machinfo->fatal_noacin_volt)))
754 return -1;
755 return 0;
758 static int sharpsl_off_charge_error(void)
760 dev_err(sharpsl_pm.dev, "Offline Charger: Error occurred.\n");
761 sharpsl_pm.machinfo->charge(0);
762 sharpsl_pm_led(SHARPSL_LED_ERROR);
763 sharpsl_pm.charge_mode = CHRG_ERROR;
764 return 1;
768 * Charging Control while suspended
769 * Return 1 - go straight to sleep
770 * Return 0 - sleep or wakeup depending on other factors
772 static int sharpsl_off_charge_battery(void)
774 int time;
776 dev_dbg(sharpsl_pm.dev, "Charge Mode: %d\n", sharpsl_pm.charge_mode);
778 if (sharpsl_pm.charge_mode == CHRG_OFF) {
779 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 1\n");
781 /* AC Check */
782 if ((sharpsl_ac_check() < 0) || (sharpsl_check_battery_temp() < 0))
783 return sharpsl_off_charge_error();
785 /* Start Charging */
786 sharpsl_pm_led(SHARPSL_LED_ON);
787 sharpsl_pm.machinfo->charge(0);
788 mdelay(SHARPSL_CHARGE_WAIT_TIME);
789 sharpsl_pm.machinfo->charge(1);
791 sharpsl_pm.charge_mode = CHRG_ON;
792 sharpsl_pm.full_count = 0;
794 return 1;
795 } else if (sharpsl_pm.charge_mode != CHRG_ON) {
796 return 1;
799 if (sharpsl_pm.full_count == 0) {
800 int time;
802 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 2\n");
804 if ((sharpsl_check_battery_temp() < 0) || (sharpsl_check_battery_voltage() < 0))
805 return sharpsl_off_charge_error();
807 sharpsl_pm.machinfo->charge(0);
808 mdelay(SHARPSL_CHARGE_WAIT_TIME);
809 sharpsl_pm.machinfo->charge(1);
810 sharpsl_pm.charge_mode = CHRG_ON;
812 mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
814 time = RCNR;
815 while(1) {
816 /* Check if any wakeup event had occurred */
817 if (sharpsl_pm.machinfo->charger_wakeup() != 0)
818 return 0;
819 /* Check for timeout */
820 if ((RCNR - time) > SHARPSL_WAIT_CO_TIME)
821 return 1;
822 if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) {
823 dev_dbg(sharpsl_pm.dev, "Offline Charger: Charge full occurred. Retrying to check\n");
824 sharpsl_pm.full_count++;
825 sharpsl_pm.machinfo->charge(0);
826 mdelay(SHARPSL_CHARGE_WAIT_TIME);
827 sharpsl_pm.machinfo->charge(1);
828 return 1;
833 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 3\n");
835 mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
837 time = RCNR;
838 while(1) {
839 /* Check if any wakeup event had occurred */
840 if (sharpsl_pm.machinfo->charger_wakeup() != 0)
841 return 0;
842 /* Check for timeout */
843 if ((RCNR-time) > SHARPSL_WAIT_CO_TIME) {
844 if (sharpsl_pm.full_count > SHARPSL_CHARGE_RETRY_CNT) {
845 dev_dbg(sharpsl_pm.dev, "Offline Charger: Not charged sufficiently. Retrying.\n");
846 sharpsl_pm.full_count = 0;
848 sharpsl_pm.full_count++;
849 return 1;
851 if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) {
852 dev_dbg(sharpsl_pm.dev, "Offline Charger: Charging complete.\n");
853 sharpsl_pm_led(SHARPSL_LED_OFF);
854 sharpsl_pm.machinfo->charge(0);
855 sharpsl_pm.charge_mode = CHRG_DONE;
856 return 1;
860 #else
861 #define sharpsl_pm_suspend NULL
862 #define sharpsl_pm_resume NULL
863 #endif
865 static ssize_t battery_percentage_show(struct device *dev, struct device_attribute *attr, char *buf)
867 return sprintf(buf, "%d\n",sharpsl_pm.battstat.mainbat_percent);
870 static ssize_t battery_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
872 return sprintf(buf, "%d\n",sharpsl_pm.battstat.mainbat_voltage);
875 static DEVICE_ATTR(battery_percentage, 0444, battery_percentage_show, NULL);
876 static DEVICE_ATTR(battery_voltage, 0444, battery_voltage_show, NULL);
878 extern void (*apm_get_power_status)(struct apm_power_info *);
880 static void sharpsl_apm_get_power_status(struct apm_power_info *info)
882 info->ac_line_status = sharpsl_pm.battstat.ac_status;
884 if (sharpsl_pm.charge_mode == CHRG_ON)
885 info->battery_status = APM_BATTERY_STATUS_CHARGING;
886 else
887 info->battery_status = sharpsl_pm.battstat.mainbat_status;
889 info->battery_flag = (1 << info->battery_status);
890 info->battery_life = sharpsl_pm.battstat.mainbat_percent;
893 #ifdef CONFIG_PM
894 static struct platform_suspend_ops sharpsl_pm_ops = {
895 .prepare = pxa_pm_prepare,
896 .finish = pxa_pm_finish,
897 .enter = corgi_pxa_pm_enter,
898 .valid = suspend_valid_only_mem,
900 #endif
902 static int __init sharpsl_pm_probe(struct platform_device *pdev)
904 int ret;
906 if (!pdev->dev.platform_data)
907 return -EINVAL;
909 sharpsl_pm.dev = &pdev->dev;
910 sharpsl_pm.machinfo = pdev->dev.platform_data;
911 sharpsl_pm.charge_mode = CHRG_OFF;
912 sharpsl_pm.flags = 0;
914 init_timer(&sharpsl_pm.ac_timer);
915 sharpsl_pm.ac_timer.function = sharpsl_ac_timer;
917 init_timer(&sharpsl_pm.chrg_full_timer);
918 sharpsl_pm.chrg_full_timer.function = sharpsl_chrg_full_timer;
920 led_trigger_register_simple("sharpsl-charge", &sharpsl_charge_led_trigger);
922 sharpsl_pm.machinfo->init();
924 gpio_request(sharpsl_pm.machinfo->gpio_acin, "AC IN");
925 gpio_direction_input(sharpsl_pm.machinfo->gpio_acin);
926 gpio_request(sharpsl_pm.machinfo->gpio_batfull, "Battery Full");
927 gpio_direction_input(sharpsl_pm.machinfo->gpio_batfull);
928 gpio_request(sharpsl_pm.machinfo->gpio_batlock, "Battery Lock");
929 gpio_direction_input(sharpsl_pm.machinfo->gpio_batlock);
931 /* Register interrupt handlers */
932 if (request_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_acin), sharpsl_ac_isr, IRQF_DISABLED | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "AC Input Detect", sharpsl_ac_isr)) {
933 dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", IRQ_GPIO(sharpsl_pm.machinfo->gpio_acin));
936 if (request_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batlock), sharpsl_fatal_isr, IRQF_DISABLED | IRQF_TRIGGER_FALLING, "Battery Cover", sharpsl_fatal_isr)) {
937 dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", IRQ_GPIO(sharpsl_pm.machinfo->gpio_batlock));
940 if (sharpsl_pm.machinfo->gpio_fatal) {
941 if (request_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_fatal), sharpsl_fatal_isr, IRQF_DISABLED | IRQF_TRIGGER_FALLING, "Fatal Battery", sharpsl_fatal_isr)) {
942 dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", IRQ_GPIO(sharpsl_pm.machinfo->gpio_fatal));
946 if (sharpsl_pm.machinfo->batfull_irq)
948 /* Register interrupt handler. */
949 if (request_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull), sharpsl_chrg_full_isr, IRQF_DISABLED | IRQF_TRIGGER_RISING, "CO", sharpsl_chrg_full_isr)) {
950 dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull));
954 ret = device_create_file(&pdev->dev, &dev_attr_battery_percentage);
955 ret |= device_create_file(&pdev->dev, &dev_attr_battery_voltage);
956 if (ret != 0)
957 dev_warn(&pdev->dev, "Failed to register attributes (%d)\n", ret);
959 apm_get_power_status = sharpsl_apm_get_power_status;
961 #ifdef CONFIG_PM
962 suspend_set_ops(&sharpsl_pm_ops);
963 #endif
965 mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
967 return 0;
970 static int sharpsl_pm_remove(struct platform_device *pdev)
972 suspend_set_ops(NULL);
974 device_remove_file(&pdev->dev, &dev_attr_battery_percentage);
975 device_remove_file(&pdev->dev, &dev_attr_battery_voltage);
977 led_trigger_unregister_simple(sharpsl_charge_led_trigger);
979 free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_acin), sharpsl_ac_isr);
980 free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batlock), sharpsl_fatal_isr);
982 if (sharpsl_pm.machinfo->gpio_fatal)
983 free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_fatal), sharpsl_fatal_isr);
985 if (sharpsl_pm.machinfo->batfull_irq)
986 free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull), sharpsl_chrg_full_isr);
988 gpio_free(sharpsl_pm.machinfo->gpio_batlock);
989 gpio_free(sharpsl_pm.machinfo->gpio_batfull);
990 gpio_free(sharpsl_pm.machinfo->gpio_acin);
992 if (sharpsl_pm.machinfo->exit)
993 sharpsl_pm.machinfo->exit();
995 del_timer_sync(&sharpsl_pm.chrg_full_timer);
996 del_timer_sync(&sharpsl_pm.ac_timer);
998 return 0;
1001 static struct platform_driver sharpsl_pm_driver = {
1002 .probe = sharpsl_pm_probe,
1003 .remove = sharpsl_pm_remove,
1004 .suspend = sharpsl_pm_suspend,
1005 .resume = sharpsl_pm_resume,
1006 .driver = {
1007 .name = "sharpsl-pm",
1011 static int __devinit sharpsl_pm_init(void)
1013 return platform_driver_register(&sharpsl_pm_driver);
1016 static void sharpsl_pm_exit(void)
1018 platform_driver_unregister(&sharpsl_pm_driver);
1021 late_initcall(sharpsl_pm_init);
1022 module_exit(sharpsl_pm_exit);