2 * Battery and Power Management code for the Sharp SL-C7xx and SL-Cxx00
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.
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/device.h>
26 #include <asm/hardware.h>
27 #include <asm/hardware/scoop.h>
28 #include <asm/mach-types.h>
32 #include <asm/arch/pm.h>
33 #include <asm/arch/pxa-regs.h>
34 #include <asm/arch/sharpsl.h>
40 #define SHARPSL_CHARGE_ON_TIME_INTERVAL (msecs_to_jiffies(1*60*1000)) /* 1 min */
41 #define SHARPSL_CHARGE_FINISH_TIME (msecs_to_jiffies(10*60*1000)) /* 10 min */
42 #define SHARPSL_BATCHK_TIME (msecs_to_jiffies(15*1000)) /* 15 sec */
43 #define SHARPSL_BATCHK_TIME_SUSPEND (60*10) /* 10 min */
44 #define SHARPSL_WAIT_CO_TIME 15 /* 15 sec */
45 #define SHARPSL_WAIT_DISCHARGE_ON 100 /* 100 msec */
46 #define SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP 10 /* 10 msec */
47 #define SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT 10 /* 10 msec */
48 #define SHARPSL_CHECK_BATTERY_WAIT_TIME_JKVAD 10 /* 10 msec */
49 #define SHARPSL_CHARGE_WAIT_TIME 15 /* 15 msec */
50 #define SHARPSL_CHARGE_CO_CHECK_TIME 5 /* 5 msec */
51 #define SHARPSL_CHARGE_RETRY_CNT 1 /* eqv. 10 min */
53 #define SHARPSL_CHARGE_ON_VOLT 0x99 /* 2.9V */
54 #define SHARPSL_CHARGE_ON_TEMP 0xe0 /* 2.9V */
55 #define SHARPSL_CHARGE_ON_JKVAD_HIGH 0x9b /* 6V */
56 #define SHARPSL_CHARGE_ON_JKVAD_LOW 0x34 /* 2V */
57 #define SHARPSL_FATAL_ACIN_VOLT 182 /* 3.45V */
58 #define SHARPSL_FATAL_NOACIN_VOLT 170 /* 3.40V */
60 struct battery_thresh spitz_battery_levels_acin[] = {
103 struct battery_thresh spitz_battery_levels_noac[] = {
146 /* MAX1111 Commands */
147 #define MAXCTRL_PD0 1u << 0
148 #define MAXCTRL_PD1 1u << 1
149 #define MAXCTRL_SGL 1u << 2
150 #define MAXCTRL_UNI 1u << 3
151 #define MAXCTRL_SEL_SH 4
152 #define MAXCTRL_STR 1u << 7
154 /* MAX1111 Channel Definitions */
163 static int sharpsl_read_MainBattery(void);
164 static int sharpsl_off_charge_battery(void);
165 static int sharpsl_check_battery(int mode);
166 static int sharpsl_ac_check(void);
167 static int sharpsl_fatal_check(void);
168 static int sharpsl_average_value(int ad);
169 static void sharpsl_average_clear(void);
170 static void sharpsl_charge_toggle(void *private_);
171 static void sharpsl_battery_thread(void *private_);
177 struct sharpsl_pm_status sharpsl_pm;
178 DECLARE_WORK(toggle_charger, sharpsl_charge_toggle, NULL);
179 DECLARE_WORK(sharpsl_bat, sharpsl_battery_thread, NULL);
182 static int get_percentage(int voltage)
184 int i = sharpsl_pm.machinfo->bat_levels - 1;
185 struct battery_thresh *thresh;
187 if (sharpsl_pm.charge_mode == CHRG_ON)
188 thresh=sharpsl_pm.machinfo->bat_levels_acin;
190 thresh=sharpsl_pm.machinfo->bat_levels_noac;
192 while (i > 0 && (voltage > thresh[i].voltage))
195 return thresh[i].percentage;
198 static int get_apm_status(int voltage)
200 int low_thresh, high_thresh;
202 if (sharpsl_pm.charge_mode == CHRG_ON) {
203 high_thresh = sharpsl_pm.machinfo->status_high_acin;
204 low_thresh = sharpsl_pm.machinfo->status_low_acin;
206 high_thresh = sharpsl_pm.machinfo->status_high_noac;
207 low_thresh = sharpsl_pm.machinfo->status_low_noac;
210 if (voltage >= high_thresh)
211 return APM_BATTERY_STATUS_HIGH;
212 if (voltage >= low_thresh)
213 return APM_BATTERY_STATUS_LOW;
214 return APM_BATTERY_STATUS_CRITICAL;
217 void sharpsl_battery_kick(void)
219 schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(125));
221 EXPORT_SYMBOL(sharpsl_battery_kick);
224 static void sharpsl_battery_thread(void *private_)
226 int voltage, percent, apm_status, i = 0;
228 if (!sharpsl_pm.machinfo)
231 sharpsl_pm.battstat.ac_status = (!(STATUS_AC_IN) ? APM_AC_OFFLINE : APM_AC_ONLINE);
233 /* Corgi cannot confirm when battery fully charged so periodically kick! */
234 if (machine_is_corgi() && (sharpsl_pm.charge_mode == CHRG_ON)
235 && time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_ON_TIME_INTERVAL))
236 schedule_work(&toggle_charger);
239 voltage = sharpsl_read_MainBattery();
240 if (voltage > 0) break;
242 voltage = sharpsl_pm.machinfo->bat_levels_noac[0].voltage;
243 dev_warn(sharpsl_pm.dev, "Warning: Cannot read main battery!\n");
248 voltage = sharpsl_average_value(voltage);
249 apm_status = get_apm_status(voltage);
250 percent = get_percentage(voltage);
252 /* At low battery voltages, the voltage has a tendency to start
253 creeping back up so we try to avoid this here */
254 if ((sharpsl_pm.battstat.ac_status == APM_AC_ONLINE) || (apm_status == APM_BATTERY_STATUS_HIGH) || percent <= sharpsl_pm.battstat.mainbat_percent) {
255 sharpsl_pm.battstat.mainbat_voltage = voltage;
256 sharpsl_pm.battstat.mainbat_status = apm_status;
257 sharpsl_pm.battstat.mainbat_percent = percent;
260 dev_dbg(sharpsl_pm.dev, "Battery: voltage: %d, status: %d, percentage: %d, time: %d\n", voltage,
261 sharpsl_pm.battstat.mainbat_status, sharpsl_pm.battstat.mainbat_percent, jiffies);
263 /* If battery is low. limit backlight intensity to save power. */
264 if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
265 && ((sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_LOW) ||
266 (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL))) {
267 if (!(sharpsl_pm.flags & SHARPSL_BL_LIMIT)) {
268 corgibl_limit_intensity(1);
269 sharpsl_pm.flags |= SHARPSL_BL_LIMIT;
271 } else if (sharpsl_pm.flags & SHARPSL_BL_LIMIT) {
272 corgibl_limit_intensity(0);
273 sharpsl_pm.flags &= ~SHARPSL_BL_LIMIT;
276 /* Suspend if critical battery level */
277 if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
278 && (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL)
279 && !(sharpsl_pm.flags & SHARPSL_APM_QUEUED)) {
280 sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
281 dev_err(sharpsl_pm.dev, "Fatal Off\n");
282 apm_queue_event(APM_CRITICAL_SUSPEND);
285 schedule_delayed_work(&sharpsl_bat, SHARPSL_BATCHK_TIME);
288 static void sharpsl_charge_on(void)
290 dev_dbg(sharpsl_pm.dev, "Turning Charger On\n");
292 sharpsl_pm.full_count = 0;
293 sharpsl_pm.charge_mode = CHRG_ON;
294 schedule_delayed_work(&toggle_charger, msecs_to_jiffies(250));
295 schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(500));
298 static void sharpsl_charge_off(void)
300 dev_dbg(sharpsl_pm.dev, "Turning Charger Off\n");
304 sharpsl_pm.charge_mode = CHRG_OFF;
306 schedule_work(&sharpsl_bat);
309 static void sharpsl_charge_error(void)
313 sharpsl_pm.charge_mode = CHRG_ERROR;
316 static void sharpsl_charge_toggle(void *private_)
318 dev_dbg(sharpsl_pm.dev, "Toogling Charger at time: %lx\n", jiffies);
320 if (STATUS_AC_IN == 0) {
321 sharpsl_charge_off();
323 } else if ((sharpsl_check_battery(1) < 0) || (sharpsl_ac_check() < 0)) {
324 sharpsl_charge_error();
330 mdelay(SHARPSL_CHARGE_WAIT_TIME);
333 sharpsl_pm.charge_start_time = jiffies;
336 static void sharpsl_ac_timer(unsigned long data)
338 int acin = STATUS_AC_IN;
340 dev_dbg(sharpsl_pm.dev, "AC Status: %d\n",acin);
342 sharpsl_average_clear();
343 if (acin && (sharpsl_pm.charge_mode != CHRG_ON))
345 else if (sharpsl_pm.charge_mode == CHRG_ON)
346 sharpsl_charge_off();
348 schedule_work(&sharpsl_bat);
352 static irqreturn_t sharpsl_ac_isr(int irq, void *dev_id, struct pt_regs *fp)
354 /* Delay the event slightly to debounce */
355 /* Must be a smaller delay than the chrg_full_isr below */
356 mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
361 static void sharpsl_chrg_full_timer(unsigned long data)
363 dev_dbg(sharpsl_pm.dev, "Charge Full at time: %lx\n", jiffies);
365 sharpsl_pm.full_count++;
367 if (STATUS_AC_IN == 0) {
368 dev_dbg(sharpsl_pm.dev, "Charge Full: AC removed - stop charging!\n");
369 if (sharpsl_pm.charge_mode == CHRG_ON)
370 sharpsl_charge_off();
371 } else if (sharpsl_pm.full_count < 2) {
372 dev_dbg(sharpsl_pm.dev, "Charge Full: Count too low\n");
373 schedule_work(&toggle_charger);
374 } else if (time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_FINISH_TIME)) {
375 dev_dbg(sharpsl_pm.dev, "Charge Full: Interrupt generated too slowly - retry.\n");
376 schedule_work(&toggle_charger);
378 sharpsl_charge_off();
379 sharpsl_pm.charge_mode = CHRG_DONE;
380 dev_dbg(sharpsl_pm.dev, "Charge Full: Charging Finished\n");
384 /* Charging Finished Interrupt (Not present on Corgi) */
385 /* Can trigger at the same time as an AC staus change so
386 delay until after that has been processed */
387 static irqreturn_t sharpsl_chrg_full_isr(int irq, void *dev_id, struct pt_regs *fp)
389 if (sharpsl_pm.flags & SHARPSL_SUSPENDED)
392 /* delay until after any ac interrupt */
393 mod_timer(&sharpsl_pm.chrg_full_timer, jiffies + msecs_to_jiffies(500));
398 static irqreturn_t sharpsl_fatal_isr(int irq, void *dev_id, struct pt_regs *fp)
402 if (STATUS_BATT_LOCKED == 0) {
403 dev_err(sharpsl_pm.dev, "Battery now Unlocked! Suspending.\n");
407 if (sharpsl_pm.machinfo->gpio_fatal && (STATUS_FATAL == 0)) {
408 dev_err(sharpsl_pm.dev, "Fatal Batt Error! Suspending.\n");
412 if (!(sharpsl_pm.flags & SHARPSL_APM_QUEUED) && is_fatal) {
413 sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
414 apm_queue_event(APM_CRITICAL_SUSPEND);
421 * Maintain an average of the last 10 readings
423 #define SHARPSL_CNV_VALUE_NUM 10
424 static int sharpsl_ad_index;
426 static void sharpsl_average_clear(void)
428 sharpsl_ad_index = 0;
431 static int sharpsl_average_value(int ad)
434 static int sharpsl_ad[SHARPSL_CNV_VALUE_NUM+1];
436 if (sharpsl_pm.battstat.mainbat_status != APM_BATTERY_STATUS_HIGH) {
437 sharpsl_ad_index = 0;
441 sharpsl_ad[sharpsl_ad_index] = ad;
443 if (sharpsl_ad_index >= SHARPSL_CNV_VALUE_NUM) {
444 for (i=0; i < (SHARPSL_CNV_VALUE_NUM-1); i++)
445 sharpsl_ad[i] = sharpsl_ad[i+1];
446 sharpsl_ad_index = SHARPSL_CNV_VALUE_NUM - 1;
448 for (i=0; i < sharpsl_ad_index; i++)
449 ad_val += sharpsl_ad[i];
451 return (ad_val / sharpsl_ad_index);
458 static int read_max1111(int channel)
460 return corgi_ssp_max1111_get((channel << MAXCTRL_SEL_SH) | MAXCTRL_PD0 | MAXCTRL_PD1
461 | MAXCTRL_SGL | MAXCTRL_UNI | MAXCTRL_STR);
464 static int sharpsl_read_MainBattery(void)
466 return read_max1111(BATT_AD);
469 static int sharpsl_read_Temp(void)
473 sharpsl_pm.machinfo->measure_temp(1);
475 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
476 temp = read_max1111(BATT_THM);
478 sharpsl_pm.machinfo->measure_temp(0);
483 static int sharpsl_read_jkvad(void)
485 return read_max1111(JK_VAD);
489 * Take an array of 5 integers, remove the maximum and minimum values
490 * and return the average.
492 static int get_select_val(int *val)
494 int i, j, k, temp, sum = 0;
499 for (i=1; i<5; i++) {
509 for (i=3; i>=0; i--) {
517 if (i != j && i != k )
520 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]);
525 /* mode 0 - Check temperature and voltage
526 * 1 - Check temperature only */
527 static int sharpsl_check_battery(int mode)
531 /* Check battery temperature */
532 for (i=0; i<5; i++) {
533 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
534 buff[i] = sharpsl_read_Temp();
537 val = get_select_val(buff);
539 dev_dbg(sharpsl_pm.dev, "Temperature: %d\n", val);
540 if (val > SHARPSL_CHARGE_ON_TEMP)
545 /* disable charge, enable discharge */
548 mdelay(SHARPSL_WAIT_DISCHARGE_ON);
550 if (sharpsl_pm.machinfo->discharge1)
551 sharpsl_pm.machinfo->discharge1(1);
553 /* Check battery voltage */
554 for (i=0; i<5; i++) {
555 buff[i] = sharpsl_read_MainBattery();
556 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
559 if (sharpsl_pm.machinfo->discharge1)
560 sharpsl_pm.machinfo->discharge1(0);
564 val = get_select_val(buff);
565 dev_dbg(sharpsl_pm.dev, "Battery Voltage: %d\n", val);
567 if (val < SHARPSL_CHARGE_ON_VOLT)
573 static int sharpsl_ac_check(void)
575 int temp, i, buff[5];
577 for (i=0; i<5; i++) {
578 buff[i] = sharpsl_read_jkvad();
579 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_JKVAD);
582 temp = get_select_val(buff);
583 dev_dbg(sharpsl_pm.dev, "AC Voltage: %d\n",temp);
585 if ((temp > SHARPSL_CHARGE_ON_JKVAD_HIGH) || (temp < SHARPSL_CHARGE_ON_JKVAD_LOW)) {
586 dev_err(sharpsl_pm.dev, "Error: AC check failed.\n");
594 static int sharpsl_pm_suspend(struct device *dev, pm_message_t state)
596 sharpsl_pm.flags |= SHARPSL_SUSPENDED;
597 flush_scheduled_work();
599 if (sharpsl_pm.charge_mode == CHRG_ON)
600 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
602 sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
607 static int sharpsl_pm_resume(struct device *dev)
609 /* Clear the reset source indicators as they break the bootloader upon reboot */
611 sharpsl_average_clear();
612 sharpsl_pm.flags &= ~SHARPSL_APM_QUEUED;
613 sharpsl_pm.flags &= ~SHARPSL_SUSPENDED;
618 static void corgi_goto_sleep(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
620 dev_dbg(sharpsl_pm.dev, "Time is: %08x\n",RCNR);
622 dev_dbg(sharpsl_pm.dev, "Offline Charge Activate = %d\n",sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG);
623 /* not charging and AC-IN! */
625 if ((sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG) && (STATUS_AC_IN != 0)) {
626 dev_dbg(sharpsl_pm.dev, "Activating Offline Charger...\n");
627 sharpsl_pm.charge_mode = CHRG_OFF;
628 sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
629 sharpsl_off_charge_battery();
632 sharpsl_pm.machinfo->presuspend();
634 PEDR = 0xffffffff; /* clear it */
636 sharpsl_pm.flags &= ~SHARPSL_ALARM_ACTIVE;
637 if ((sharpsl_pm.charge_mode == CHRG_ON) && ((alarm_enable && ((alarm_time - RCNR) > (SHARPSL_BATCHK_TIME_SUSPEND + 30))) || !alarm_enable)) {
639 RTAR = RCNR + SHARPSL_BATCHK_TIME_SUSPEND;
640 dev_dbg(sharpsl_pm.dev, "Charging alarm at: %08x\n",RTAR);
641 sharpsl_pm.flags |= SHARPSL_ALARM_ACTIVE;
642 } else if (alarm_enable) {
645 dev_dbg(sharpsl_pm.dev, "User alarm at: %08x\n",RTAR);
647 dev_dbg(sharpsl_pm.dev, "No alarms set.\n");
652 sharpsl_pm.machinfo->postsuspend();
654 dev_dbg(sharpsl_pm.dev, "Corgi woken up from suspend: %08x\n",PEDR);
657 static int corgi_enter_suspend(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
659 if (!sharpsl_pm.machinfo->should_wakeup(!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE) && alarm_enable) )
661 if (!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE)) {
662 dev_dbg(sharpsl_pm.dev, "No user triggered wakeup events and not charging. Strange. Suspend.\n");
663 corgi_goto_sleep(alarm_time, alarm_enable, state);
666 if(sharpsl_off_charge_battery()) {
667 dev_dbg(sharpsl_pm.dev, "Charging. Suspend...\n");
668 corgi_goto_sleep(alarm_time, alarm_enable, state);
671 dev_dbg(sharpsl_pm.dev, "User triggered wakeup in offline charger.\n");
674 if ((STATUS_BATT_LOCKED == 0) || (sharpsl_fatal_check() < 0) )
676 dev_err(sharpsl_pm.dev, "Fatal condition. Suspend.\n");
677 corgi_goto_sleep(alarm_time, alarm_enable, state);
684 static int corgi_pxa_pm_enter(suspend_state_t state)
686 unsigned long alarm_time = RTAR;
687 unsigned int alarm_status = ((RTSR & RTSR_ALE) != 0);
689 dev_dbg(sharpsl_pm.dev, "SharpSL suspending for first time.\n");
691 corgi_goto_sleep(alarm_time, alarm_status, state);
693 while (corgi_enter_suspend(alarm_time,alarm_status,state))
696 dev_dbg(sharpsl_pm.dev, "SharpSL resuming...\n");
704 * Check for fatal battery errors
707 static int sharpsl_fatal_check(void)
709 int buff[5], temp, i, acin;
711 dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check entered\n");
713 /* Check AC-Adapter */
716 if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) {
719 DISCHARGE_ON(); /* enable discharge */
720 mdelay(SHARPSL_WAIT_DISCHARGE_ON);
723 if (sharpsl_pm.machinfo->discharge1)
724 sharpsl_pm.machinfo->discharge1(1);
726 /* Check battery : check inserting battery ? */
727 for (i=0; i<5; i++) {
728 buff[i] = sharpsl_read_MainBattery();
729 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
732 if (sharpsl_pm.machinfo->discharge1)
733 sharpsl_pm.machinfo->discharge1(0);
735 if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) {
741 temp = get_select_val(buff);
742 dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check: acin: %d, discharge voltage: %d, no discharge: %d\n", acin, temp, sharpsl_read_MainBattery());
744 if ((acin && (temp < SHARPSL_FATAL_ACIN_VOLT)) ||
745 (!acin && (temp < SHARPSL_FATAL_NOACIN_VOLT)))
750 static int sharpsl_off_charge_error(void)
752 dev_err(sharpsl_pm.dev, "Offline Charger: Error occured.\n");
755 sharpsl_pm.charge_mode = CHRG_ERROR;
760 * Charging Control while suspended
761 * Return 1 - go straight to sleep
762 * Return 0 - sleep or wakeup depending on other factors
764 static int sharpsl_off_charge_battery(void)
768 dev_dbg(sharpsl_pm.dev, "Charge Mode: %d\n", sharpsl_pm.charge_mode);
770 if (sharpsl_pm.charge_mode == CHRG_OFF) {
771 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 1\n");
774 if ((sharpsl_ac_check() < 0) || (sharpsl_check_battery(1) < 0))
775 return sharpsl_off_charge_error();
780 mdelay(SHARPSL_CHARGE_WAIT_TIME);
783 sharpsl_pm.charge_mode = CHRG_ON;
784 sharpsl_pm.full_count = 0;
787 } else if (sharpsl_pm.charge_mode != CHRG_ON) {
791 if (sharpsl_pm.full_count == 0) {
794 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 2\n");
796 if (sharpsl_check_battery(0) < 0)
797 return sharpsl_off_charge_error();
800 mdelay(SHARPSL_CHARGE_WAIT_TIME);
802 sharpsl_pm.charge_mode = CHRG_ON;
804 mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
808 /* Check if any wakeup event had occured */
809 if (sharpsl_pm.machinfo->charger_wakeup() != 0)
811 /* Check for timeout */
812 if ((RCNR - time) > SHARPSL_WAIT_CO_TIME)
814 if (STATUS_CHRG_FULL) {
815 dev_dbg(sharpsl_pm.dev, "Offline Charger: Charge full occured. Retrying to check\n");
816 sharpsl_pm.full_count++;
818 mdelay(SHARPSL_CHARGE_WAIT_TIME);
825 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 3\n");
827 mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
831 /* Check if any wakeup event had occured */
832 if (sharpsl_pm.machinfo->charger_wakeup() != 0)
834 /* Check for timeout */
835 if ((RCNR-time) > SHARPSL_WAIT_CO_TIME) {
836 if (sharpsl_pm.full_count > SHARPSL_CHARGE_RETRY_CNT) {
837 dev_dbg(sharpsl_pm.dev, "Offline Charger: Not charged sufficiently. Retrying.\n");
838 sharpsl_pm.full_count = 0;
840 sharpsl_pm.full_count++;
843 if (STATUS_CHRG_FULL) {
844 dev_dbg(sharpsl_pm.dev, "Offline Charger: Charging complete.\n");
847 sharpsl_pm.charge_mode = CHRG_DONE;
854 static ssize_t battery_percentage_show(struct device *dev, struct device_attribute *attr, char *buf)
856 return sprintf(buf, "%d\n",sharpsl_pm.battstat.mainbat_percent);
859 static ssize_t battery_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
861 return sprintf(buf, "%d\n",sharpsl_pm.battstat.mainbat_voltage);
864 static DEVICE_ATTR(battery_percentage, 0444, battery_percentage_show, NULL);
865 static DEVICE_ATTR(battery_voltage, 0444, battery_voltage_show, NULL);
867 extern void (*apm_get_power_status)(struct apm_power_info *);
869 static void sharpsl_apm_get_power_status(struct apm_power_info *info)
871 info->ac_line_status = sharpsl_pm.battstat.ac_status;
873 if (sharpsl_pm.charge_mode == CHRG_ON)
874 info->battery_status = APM_BATTERY_STATUS_CHARGING;
876 info->battery_status = sharpsl_pm.battstat.mainbat_status;
878 info->battery_flag = (1 << info->battery_status);
879 info->battery_life = sharpsl_pm.battstat.mainbat_percent;
882 static struct pm_ops sharpsl_pm_ops = {
883 .pm_disk_mode = PM_DISK_FIRMWARE,
884 .prepare = pxa_pm_prepare,
885 .enter = corgi_pxa_pm_enter,
886 .finish = pxa_pm_finish,
889 static int __init sharpsl_pm_probe(struct device *dev)
891 if (!dev->platform_data)
894 sharpsl_pm.dev = dev;
895 sharpsl_pm.machinfo = dev->platform_data;
896 sharpsl_pm.charge_mode = CHRG_OFF;
897 sharpsl_pm.flags = 0;
899 sharpsl_pm.machinfo->init();
901 init_timer(&sharpsl_pm.ac_timer);
902 sharpsl_pm.ac_timer.function = sharpsl_ac_timer;
904 init_timer(&sharpsl_pm.chrg_full_timer);
905 sharpsl_pm.chrg_full_timer.function = sharpsl_chrg_full_timer;
907 pxa_gpio_mode(sharpsl_pm.machinfo->gpio_acin | GPIO_IN);
908 pxa_gpio_mode(sharpsl_pm.machinfo->gpio_batfull | GPIO_IN);
909 pxa_gpio_mode(sharpsl_pm.machinfo->gpio_batlock | GPIO_IN);
911 /* Register interrupt handlers */
912 if (request_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_acin), sharpsl_ac_isr, SA_INTERRUPT, "AC Input Detect", sharpsl_ac_isr)) {
913 dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", IRQ_GPIO(sharpsl_pm.machinfo->gpio_acin));
915 else set_irq_type(IRQ_GPIO(sharpsl_pm.machinfo->gpio_acin),IRQT_BOTHEDGE);
917 if (request_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batlock), sharpsl_fatal_isr, SA_INTERRUPT, "Battery Cover", sharpsl_fatal_isr)) {
918 dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", IRQ_GPIO(sharpsl_pm.machinfo->gpio_batlock));
920 else set_irq_type(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batlock),IRQT_FALLING);
922 if (sharpsl_pm.machinfo->gpio_fatal) {
923 if (request_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_fatal), sharpsl_fatal_isr, SA_INTERRUPT, "Fatal Battery", sharpsl_fatal_isr)) {
924 dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", IRQ_GPIO(sharpsl_pm.machinfo->gpio_fatal));
926 else set_irq_type(IRQ_GPIO(sharpsl_pm.machinfo->gpio_fatal),IRQT_FALLING);
929 if (!machine_is_corgi())
931 /* Register interrupt handler. */
932 if (request_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull), sharpsl_chrg_full_isr, SA_INTERRUPT, "CO", sharpsl_chrg_full_isr)) {
933 dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull));
935 else set_irq_type(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull),IRQT_RISING);
938 device_create_file(dev, &dev_attr_battery_percentage);
939 device_create_file(dev, &dev_attr_battery_voltage);
941 apm_get_power_status = sharpsl_apm_get_power_status;
943 pm_set_ops(&sharpsl_pm_ops);
945 mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
950 static int sharpsl_pm_remove(struct device *dev)
954 device_remove_file(dev, &dev_attr_battery_percentage);
955 device_remove_file(dev, &dev_attr_battery_voltage);
957 free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_acin), sharpsl_ac_isr);
958 free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batlock), sharpsl_fatal_isr);
960 if (sharpsl_pm.machinfo->gpio_fatal)
961 free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_fatal), sharpsl_fatal_isr);
963 if (!machine_is_corgi())
964 free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull), sharpsl_chrg_full_isr);
966 del_timer_sync(&sharpsl_pm.chrg_full_timer);
967 del_timer_sync(&sharpsl_pm.ac_timer);
972 static struct device_driver sharpsl_pm_driver = {
973 .name = "sharpsl-pm",
974 .bus = &platform_bus_type,
975 .probe = sharpsl_pm_probe,
976 .remove = sharpsl_pm_remove,
977 .suspend = sharpsl_pm_suspend,
978 .resume = sharpsl_pm_resume,
981 static int __devinit sharpsl_pm_init(void)
983 return driver_register(&sharpsl_pm_driver);
986 static void sharpsl_pm_exit(void)
988 driver_unregister(&sharpsl_pm_driver);
991 late_initcall(sharpsl_pm_init);
992 module_exit(sharpsl_pm_exit);