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/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>
29 #include <asm/mach-types.h>
31 #include <mach/pxa2xx-regs.h>
33 #include "sharpsl_pm.h"
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 */
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 */
56 static int sharpsl_off_charge_battery(void);
57 static int sharpsl_check_battery_voltage(void);
59 static int sharpsl_check_battery_temp(void);
60 static int sharpsl_ac_check(void);
61 static int sharpsl_average_value(int ad
);
62 static void sharpsl_average_clear(void);
63 static void sharpsl_charge_toggle(struct work_struct
*private_
);
64 static void sharpsl_battery_thread(struct work_struct
*private_
);
70 struct sharpsl_pm_status sharpsl_pm
;
71 static DECLARE_DELAYED_WORK(toggle_charger
, sharpsl_charge_toggle
);
72 static DECLARE_DELAYED_WORK(sharpsl_bat
, sharpsl_battery_thread
);
73 DEFINE_LED_TRIGGER(sharpsl_charge_led_trigger
);
77 struct battery_thresh sharpsl_battery_levels_acin
[] = {
120 struct battery_thresh sharpsl_battery_levels_noac
[] = {
163 /* MAX1111 Commands */
164 #define MAXCTRL_PD0 (1u << 0)
165 #define MAXCTRL_PD1 (1u << 1)
166 #define MAXCTRL_SGL (1u << 2)
167 #define MAXCTRL_UNI (1u << 3)
168 #define MAXCTRL_SEL_SH 4
169 #define MAXCTRL_STR (1u << 7)
171 extern int max1111_read_channel(int);
175 int sharpsl_pm_pxa_read_max1111(int channel
)
177 /* Ugly, better move this function into another module */
178 if (machine_is_tosa())
181 /* max1111 accepts channels from 0-3, however,
182 * it is encoded from 0-7 here in the code.
184 return max1111_read_channel(channel
>> 1);
187 static int get_percentage(int voltage
)
189 int i
= sharpsl_pm
.machinfo
->bat_levels
- 1;
190 int bl_status
= sharpsl_pm
.machinfo
->backlight_get_status
? sharpsl_pm
.machinfo
->backlight_get_status() : 0;
191 struct battery_thresh
*thresh
;
193 if (sharpsl_pm
.charge_mode
== CHRG_ON
)
194 thresh
= bl_status
? sharpsl_pm
.machinfo
->bat_levels_acin_bl
: sharpsl_pm
.machinfo
->bat_levels_acin
;
196 thresh
= bl_status
? sharpsl_pm
.machinfo
->bat_levels_noac_bl
: sharpsl_pm
.machinfo
->bat_levels_noac
;
198 while (i
> 0 && (voltage
> thresh
[i
].voltage
))
201 return thresh
[i
].percentage
;
204 static int get_apm_status(int voltage
)
206 int low_thresh
, high_thresh
;
208 if (sharpsl_pm
.charge_mode
== CHRG_ON
) {
209 high_thresh
= sharpsl_pm
.machinfo
->status_high_acin
;
210 low_thresh
= sharpsl_pm
.machinfo
->status_low_acin
;
212 high_thresh
= sharpsl_pm
.machinfo
->status_high_noac
;
213 low_thresh
= sharpsl_pm
.machinfo
->status_low_noac
;
216 if (voltage
>= high_thresh
)
217 return APM_BATTERY_STATUS_HIGH
;
218 if (voltage
>= low_thresh
)
219 return APM_BATTERY_STATUS_LOW
;
220 return APM_BATTERY_STATUS_CRITICAL
;
223 void sharpsl_battery_kick(void)
225 schedule_delayed_work(&sharpsl_bat
, msecs_to_jiffies(125));
227 EXPORT_SYMBOL(sharpsl_battery_kick
);
230 static void sharpsl_battery_thread(struct work_struct
*private_
)
232 int voltage
, percent
, apm_status
, i
;
234 if (!sharpsl_pm
.machinfo
)
237 sharpsl_pm
.battstat
.ac_status
= (sharpsl_pm
.machinfo
->read_devdata(SHARPSL_STATUS_ACIN
) ? APM_AC_ONLINE
: APM_AC_OFFLINE
);
239 /* Corgi cannot confirm when battery fully charged so periodically kick! */
240 if (!sharpsl_pm
.machinfo
->batfull_irq
&& (sharpsl_pm
.charge_mode
== CHRG_ON
)
241 && time_after(jiffies
, sharpsl_pm
.charge_start_time
+ SHARPSL_CHARGE_ON_TIME_INTERVAL
))
242 schedule_delayed_work(&toggle_charger
, 0);
244 for (i
= 0; i
< 5; i
++) {
245 voltage
= sharpsl_pm
.machinfo
->read_devdata(SHARPSL_BATT_VOLT
);
250 voltage
= sharpsl_pm
.machinfo
->bat_levels_noac
[0].voltage
;
251 dev_warn(sharpsl_pm
.dev
, "Warning: Cannot read main battery!\n");
254 voltage
= sharpsl_average_value(voltage
);
255 apm_status
= get_apm_status(voltage
);
256 percent
= get_percentage(voltage
);
258 /* At low battery voltages, the voltage has a tendency to start
259 creeping back up so we try to avoid this here */
260 if ((sharpsl_pm
.battstat
.ac_status
== APM_AC_ONLINE
)
261 || (apm_status
== APM_BATTERY_STATUS_HIGH
)
262 || percent
<= sharpsl_pm
.battstat
.mainbat_percent
) {
263 sharpsl_pm
.battstat
.mainbat_voltage
= voltage
;
264 sharpsl_pm
.battstat
.mainbat_status
= apm_status
;
265 sharpsl_pm
.battstat
.mainbat_percent
= percent
;
268 dev_dbg(sharpsl_pm
.dev
, "Battery: voltage: %d, status: %d, percentage: %d, time: %ld\n", voltage
,
269 sharpsl_pm
.battstat
.mainbat_status
, sharpsl_pm
.battstat
.mainbat_percent
, jiffies
);
271 /* Suspend if critical battery level */
272 if ((sharpsl_pm
.battstat
.ac_status
!= APM_AC_ONLINE
)
273 && (sharpsl_pm
.battstat
.mainbat_status
== APM_BATTERY_STATUS_CRITICAL
)
274 && !(sharpsl_pm
.flags
& SHARPSL_APM_QUEUED
)) {
275 sharpsl_pm
.flags
|= SHARPSL_APM_QUEUED
;
276 dev_err(sharpsl_pm
.dev
, "Fatal Off\n");
277 apm_queue_event(APM_CRITICAL_SUSPEND
);
280 schedule_delayed_work(&sharpsl_bat
, SHARPSL_BATCHK_TIME
);
283 void sharpsl_pm_led(int val
)
285 if (val
== SHARPSL_LED_ERROR
) {
286 dev_err(sharpsl_pm
.dev
, "Charging Error!\n");
287 } else if (val
== SHARPSL_LED_ON
) {
288 dev_dbg(sharpsl_pm
.dev
, "Charge LED On\n");
289 led_trigger_event(sharpsl_charge_led_trigger
, LED_FULL
);
291 dev_dbg(sharpsl_pm
.dev
, "Charge LED Off\n");
292 led_trigger_event(sharpsl_charge_led_trigger
, LED_OFF
);
296 static void sharpsl_charge_on(void)
298 dev_dbg(sharpsl_pm
.dev
, "Turning Charger On\n");
300 sharpsl_pm
.full_count
= 0;
301 sharpsl_pm
.charge_mode
= CHRG_ON
;
302 schedule_delayed_work(&toggle_charger
, msecs_to_jiffies(250));
303 schedule_delayed_work(&sharpsl_bat
, msecs_to_jiffies(500));
306 static void sharpsl_charge_off(void)
308 dev_dbg(sharpsl_pm
.dev
, "Turning Charger Off\n");
310 sharpsl_pm
.machinfo
->charge(0);
311 sharpsl_pm_led(SHARPSL_LED_OFF
);
312 sharpsl_pm
.charge_mode
= CHRG_OFF
;
314 schedule_delayed_work(&sharpsl_bat
, 0);
317 static void sharpsl_charge_error(void)
319 sharpsl_pm_led(SHARPSL_LED_ERROR
);
320 sharpsl_pm
.machinfo
->charge(0);
321 sharpsl_pm
.charge_mode
= CHRG_ERROR
;
324 static void sharpsl_charge_toggle(struct work_struct
*private_
)
326 dev_dbg(sharpsl_pm
.dev
, "Toggling Charger at time: %lx\n", jiffies
);
328 if (!sharpsl_pm
.machinfo
->read_devdata(SHARPSL_STATUS_ACIN
)) {
329 sharpsl_charge_off();
331 } else if ((sharpsl_check_battery_temp() < 0) || (sharpsl_ac_check() < 0)) {
332 sharpsl_charge_error();
336 sharpsl_pm_led(SHARPSL_LED_ON
);
337 sharpsl_pm
.machinfo
->charge(0);
338 mdelay(SHARPSL_CHARGE_WAIT_TIME
);
339 sharpsl_pm
.machinfo
->charge(1);
341 sharpsl_pm
.charge_start_time
= jiffies
;
344 static void sharpsl_ac_timer(struct timer_list
*unused
)
346 int acin
= sharpsl_pm
.machinfo
->read_devdata(SHARPSL_STATUS_ACIN
);
348 dev_dbg(sharpsl_pm
.dev
, "AC Status: %d\n", acin
);
350 sharpsl_average_clear();
351 if (acin
&& (sharpsl_pm
.charge_mode
!= CHRG_ON
))
353 else if (sharpsl_pm
.charge_mode
== CHRG_ON
)
354 sharpsl_charge_off();
356 schedule_delayed_work(&sharpsl_bat
, 0);
360 static irqreturn_t
sharpsl_ac_isr(int irq
, void *dev_id
)
362 /* Delay the event slightly to debounce */
363 /* Must be a smaller delay than the chrg_full_isr below */
364 mod_timer(&sharpsl_pm
.ac_timer
, jiffies
+ msecs_to_jiffies(250));
369 static void sharpsl_chrg_full_timer(struct timer_list
*unused
)
371 dev_dbg(sharpsl_pm
.dev
, "Charge Full at time: %lx\n", jiffies
);
373 sharpsl_pm
.full_count
++;
375 if (!sharpsl_pm
.machinfo
->read_devdata(SHARPSL_STATUS_ACIN
)) {
376 dev_dbg(sharpsl_pm
.dev
, "Charge Full: AC removed - stop charging!\n");
377 if (sharpsl_pm
.charge_mode
== CHRG_ON
)
378 sharpsl_charge_off();
379 } else if (sharpsl_pm
.full_count
< 2) {
380 dev_dbg(sharpsl_pm
.dev
, "Charge Full: Count too low\n");
381 schedule_delayed_work(&toggle_charger
, 0);
382 } else if (time_after(jiffies
, sharpsl_pm
.charge_start_time
+ SHARPSL_CHARGE_FINISH_TIME
)) {
383 dev_dbg(sharpsl_pm
.dev
, "Charge Full: Interrupt generated too slowly - retry.\n");
384 schedule_delayed_work(&toggle_charger
, 0);
386 sharpsl_charge_off();
387 sharpsl_pm
.charge_mode
= CHRG_DONE
;
388 dev_dbg(sharpsl_pm
.dev
, "Charge Full: Charging Finished\n");
392 /* Charging Finished Interrupt (Not present on Corgi) */
393 /* Can trigger at the same time as an AC status change so
394 delay until after that has been processed */
395 static irqreturn_t
sharpsl_chrg_full_isr(int irq
, void *dev_id
)
397 if (sharpsl_pm
.flags
& SHARPSL_SUSPENDED
)
400 /* delay until after any ac interrupt */
401 mod_timer(&sharpsl_pm
.chrg_full_timer
, jiffies
+ msecs_to_jiffies(500));
406 static irqreturn_t
sharpsl_fatal_isr(int irq
, void *dev_id
)
410 if (!sharpsl_pm
.machinfo
->read_devdata(SHARPSL_STATUS_LOCK
)) {
411 dev_err(sharpsl_pm
.dev
, "Battery now Unlocked! Suspending.\n");
415 if (!sharpsl_pm
.machinfo
->read_devdata(SHARPSL_STATUS_FATAL
)) {
416 dev_err(sharpsl_pm
.dev
, "Fatal Batt Error! Suspending.\n");
420 if (!(sharpsl_pm
.flags
& SHARPSL_APM_QUEUED
) && is_fatal
) {
421 sharpsl_pm
.flags
|= SHARPSL_APM_QUEUED
;
422 apm_queue_event(APM_CRITICAL_SUSPEND
);
429 * Maintain an average of the last 10 readings
431 #define SHARPSL_CNV_VALUE_NUM 10
432 static int sharpsl_ad_index
;
434 static void sharpsl_average_clear(void)
436 sharpsl_ad_index
= 0;
439 static int sharpsl_average_value(int ad
)
442 static int sharpsl_ad
[SHARPSL_CNV_VALUE_NUM
+1];
444 if (sharpsl_pm
.battstat
.mainbat_status
!= APM_BATTERY_STATUS_HIGH
) {
445 sharpsl_ad_index
= 0;
449 sharpsl_ad
[sharpsl_ad_index
] = ad
;
451 if (sharpsl_ad_index
>= SHARPSL_CNV_VALUE_NUM
) {
452 for (i
= 0; i
< (SHARPSL_CNV_VALUE_NUM
-1); i
++)
453 sharpsl_ad
[i
] = sharpsl_ad
[i
+1];
454 sharpsl_ad_index
= SHARPSL_CNV_VALUE_NUM
- 1;
456 for (i
= 0; i
< sharpsl_ad_index
; i
++)
457 ad_val
+= sharpsl_ad
[i
];
459 return ad_val
/ sharpsl_ad_index
;
463 * Take an array of 5 integers, remove the maximum and minimum values
464 * and return the average.
466 static int get_select_val(int *val
)
468 int i
, j
, k
, temp
, sum
= 0;
473 for (i
= 1; i
< 5; i
++) {
483 for (i
= 3; i
>= 0; i
--) {
490 for (i
= 0; i
< 5; i
++)
491 if (i
!= j
&& i
!= k
)
494 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]);
499 static int sharpsl_check_battery_temp(void)
503 /* Check battery temperature */
504 for (i
= 0; i
< 5; i
++) {
505 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP
);
506 sharpsl_pm
.machinfo
->measure_temp(1);
507 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP
);
508 buff
[i
] = sharpsl_pm
.machinfo
->read_devdata(SHARPSL_BATT_TEMP
);
509 sharpsl_pm
.machinfo
->measure_temp(0);
512 val
= get_select_val(buff
);
514 dev_dbg(sharpsl_pm
.dev
, "Temperature: %d\n", val
);
515 if (val
> sharpsl_pm
.machinfo
->charge_on_temp
) {
516 printk(KERN_WARNING
"Not charging: temperature out of limits.\n");
524 static int sharpsl_check_battery_voltage(void)
528 /* disable charge, enable discharge */
529 sharpsl_pm
.machinfo
->charge(0);
530 sharpsl_pm
.machinfo
->discharge(1);
531 mdelay(SHARPSL_WAIT_DISCHARGE_ON
);
533 if (sharpsl_pm
.machinfo
->discharge1
)
534 sharpsl_pm
.machinfo
->discharge1(1);
536 /* Check battery voltage */
537 for (i
= 0; i
< 5; i
++) {
538 buff
[i
] = sharpsl_pm
.machinfo
->read_devdata(SHARPSL_BATT_VOLT
);
539 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT
);
542 if (sharpsl_pm
.machinfo
->discharge1
)
543 sharpsl_pm
.machinfo
->discharge1(0);
545 sharpsl_pm
.machinfo
->discharge(0);
547 val
= get_select_val(buff
);
548 dev_dbg(sharpsl_pm
.dev
, "Battery Voltage: %d\n", val
);
550 if (val
< sharpsl_pm
.machinfo
->charge_on_volt
)
557 static int sharpsl_ac_check(void)
559 int temp
, i
, buff
[5];
561 for (i
= 0; i
< 5; i
++) {
562 buff
[i
] = sharpsl_pm
.machinfo
->read_devdata(SHARPSL_ACIN_VOLT
);
563 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN
);
566 temp
= get_select_val(buff
);
567 dev_dbg(sharpsl_pm
.dev
, "AC Voltage: %d\n", temp
);
569 if ((temp
> sharpsl_pm
.machinfo
->charge_acin_high
) || (temp
< sharpsl_pm
.machinfo
->charge_acin_low
)) {
570 dev_err(sharpsl_pm
.dev
, "Error: AC check failed: voltage %d.\n", temp
);
578 static int sharpsl_pm_suspend(struct platform_device
*pdev
, pm_message_t state
)
580 sharpsl_pm
.flags
|= SHARPSL_SUSPENDED
;
581 flush_delayed_work(&toggle_charger
);
582 flush_delayed_work(&sharpsl_bat
);
584 if (sharpsl_pm
.charge_mode
== CHRG_ON
)
585 sharpsl_pm
.flags
|= SHARPSL_DO_OFFLINE_CHRG
;
587 sharpsl_pm
.flags
&= ~SHARPSL_DO_OFFLINE_CHRG
;
592 static int sharpsl_pm_resume(struct platform_device
*pdev
)
594 /* Clear the reset source indicators as they break the bootloader upon reboot */
596 sharpsl_average_clear();
597 sharpsl_pm
.flags
&= ~SHARPSL_APM_QUEUED
;
598 sharpsl_pm
.flags
&= ~SHARPSL_SUSPENDED
;
603 static void corgi_goto_sleep(unsigned long alarm_time
, unsigned int alarm_enable
, suspend_state_t state
)
605 dev_dbg(sharpsl_pm
.dev
, "Time is: %08x\n", RCNR
);
607 dev_dbg(sharpsl_pm
.dev
, "Offline Charge Activate = %d\n", sharpsl_pm
.flags
& SHARPSL_DO_OFFLINE_CHRG
);
608 /* not charging and AC-IN! */
610 if ((sharpsl_pm
.flags
& SHARPSL_DO_OFFLINE_CHRG
) && (sharpsl_pm
.machinfo
->read_devdata(SHARPSL_STATUS_ACIN
))) {
611 dev_dbg(sharpsl_pm
.dev
, "Activating Offline Charger...\n");
612 sharpsl_pm
.charge_mode
= CHRG_OFF
;
613 sharpsl_pm
.flags
&= ~SHARPSL_DO_OFFLINE_CHRG
;
614 sharpsl_off_charge_battery();
617 sharpsl_pm
.machinfo
->presuspend();
619 PEDR
= 0xffffffff; /* clear it */
621 sharpsl_pm
.flags
&= ~SHARPSL_ALARM_ACTIVE
;
622 if ((sharpsl_pm
.charge_mode
== CHRG_ON
) && ((alarm_enable
&& ((alarm_time
- RCNR
) > (SHARPSL_BATCHK_TIME_SUSPEND
+ 30))) || !alarm_enable
)) {
624 RTAR
= RCNR
+ SHARPSL_BATCHK_TIME_SUSPEND
;
625 dev_dbg(sharpsl_pm
.dev
, "Charging alarm at: %08x\n", RTAR
);
626 sharpsl_pm
.flags
|= SHARPSL_ALARM_ACTIVE
;
627 } else if (alarm_enable
) {
630 dev_dbg(sharpsl_pm
.dev
, "User alarm at: %08x\n", RTAR
);
632 dev_dbg(sharpsl_pm
.dev
, "No alarms set.\n");
637 sharpsl_pm
.machinfo
->postsuspend();
639 dev_dbg(sharpsl_pm
.dev
, "Corgi woken up from suspend: %08x\n", PEDR
);
642 static int corgi_enter_suspend(unsigned long alarm_time
, unsigned int alarm_enable
, suspend_state_t state
)
644 if (!sharpsl_pm
.machinfo
->should_wakeup(!(sharpsl_pm
.flags
& SHARPSL_ALARM_ACTIVE
) && alarm_enable
)) {
645 if (!(sharpsl_pm
.flags
& SHARPSL_ALARM_ACTIVE
)) {
646 dev_dbg(sharpsl_pm
.dev
, "No user triggered wakeup events and not charging. Strange. Suspend.\n");
647 corgi_goto_sleep(alarm_time
, alarm_enable
, state
);
650 if (sharpsl_off_charge_battery()) {
651 dev_dbg(sharpsl_pm
.dev
, "Charging. Suspend...\n");
652 corgi_goto_sleep(alarm_time
, alarm_enable
, state
);
655 dev_dbg(sharpsl_pm
.dev
, "User triggered wakeup in offline charger.\n");
658 if ((!sharpsl_pm
.machinfo
->read_devdata(SHARPSL_STATUS_LOCK
)) ||
659 (!sharpsl_pm
.machinfo
->read_devdata(SHARPSL_STATUS_FATAL
))) {
660 dev_err(sharpsl_pm
.dev
, "Fatal condition. Suspend.\n");
661 corgi_goto_sleep(alarm_time
, alarm_enable
, state
);
668 static int corgi_pxa_pm_enter(suspend_state_t state
)
670 unsigned long alarm_time
= RTAR
;
671 unsigned int alarm_status
= ((RTSR
& RTSR_ALE
) != 0);
673 dev_dbg(sharpsl_pm
.dev
, "SharpSL suspending for first time.\n");
675 corgi_goto_sleep(alarm_time
, alarm_status
, state
);
677 while (corgi_enter_suspend(alarm_time
, alarm_status
, state
))
680 if (sharpsl_pm
.machinfo
->earlyresume
)
681 sharpsl_pm
.machinfo
->earlyresume();
683 dev_dbg(sharpsl_pm
.dev
, "SharpSL resuming...\n");
688 static int sharpsl_off_charge_error(void)
690 dev_err(sharpsl_pm
.dev
, "Offline Charger: Error occurred.\n");
691 sharpsl_pm
.machinfo
->charge(0);
692 sharpsl_pm_led(SHARPSL_LED_ERROR
);
693 sharpsl_pm
.charge_mode
= CHRG_ERROR
;
698 * Charging Control while suspended
699 * Return 1 - go straight to sleep
700 * Return 0 - sleep or wakeup depending on other factors
702 static int sharpsl_off_charge_battery(void)
706 dev_dbg(sharpsl_pm
.dev
, "Charge Mode: %d\n", sharpsl_pm
.charge_mode
);
708 if (sharpsl_pm
.charge_mode
== CHRG_OFF
) {
709 dev_dbg(sharpsl_pm
.dev
, "Offline Charger: Step 1\n");
712 if ((sharpsl_ac_check() < 0) || (sharpsl_check_battery_temp() < 0))
713 return sharpsl_off_charge_error();
716 sharpsl_pm_led(SHARPSL_LED_ON
);
717 sharpsl_pm
.machinfo
->charge(0);
718 mdelay(SHARPSL_CHARGE_WAIT_TIME
);
719 sharpsl_pm
.machinfo
->charge(1);
721 sharpsl_pm
.charge_mode
= CHRG_ON
;
722 sharpsl_pm
.full_count
= 0;
725 } else if (sharpsl_pm
.charge_mode
!= CHRG_ON
) {
729 if (sharpsl_pm
.full_count
== 0) {
732 dev_dbg(sharpsl_pm
.dev
, "Offline Charger: Step 2\n");
734 if ((sharpsl_check_battery_temp() < 0) || (sharpsl_check_battery_voltage() < 0))
735 return sharpsl_off_charge_error();
737 sharpsl_pm
.machinfo
->charge(0);
738 mdelay(SHARPSL_CHARGE_WAIT_TIME
);
739 sharpsl_pm
.machinfo
->charge(1);
740 sharpsl_pm
.charge_mode
= CHRG_ON
;
742 mdelay(SHARPSL_CHARGE_CO_CHECK_TIME
);
746 /* Check if any wakeup event had occurred */
747 if (sharpsl_pm
.machinfo
->charger_wakeup())
749 /* Check for timeout */
750 if ((RCNR
- time
) > SHARPSL_WAIT_CO_TIME
)
752 if (sharpsl_pm
.machinfo
->read_devdata(SHARPSL_STATUS_CHRGFULL
)) {
753 dev_dbg(sharpsl_pm
.dev
, "Offline Charger: Charge full occurred. Retrying to check\n");
754 sharpsl_pm
.full_count
++;
755 sharpsl_pm
.machinfo
->charge(0);
756 mdelay(SHARPSL_CHARGE_WAIT_TIME
);
757 sharpsl_pm
.machinfo
->charge(1);
763 dev_dbg(sharpsl_pm
.dev
, "Offline Charger: Step 3\n");
765 mdelay(SHARPSL_CHARGE_CO_CHECK_TIME
);
769 /* Check if any wakeup event had occurred */
770 if (sharpsl_pm
.machinfo
->charger_wakeup())
772 /* Check for timeout */
773 if ((RCNR
-time
) > SHARPSL_WAIT_CO_TIME
) {
774 if (sharpsl_pm
.full_count
> SHARPSL_CHARGE_RETRY_CNT
) {
775 dev_dbg(sharpsl_pm
.dev
, "Offline Charger: Not charged sufficiently. Retrying.\n");
776 sharpsl_pm
.full_count
= 0;
778 sharpsl_pm
.full_count
++;
781 if (sharpsl_pm
.machinfo
->read_devdata(SHARPSL_STATUS_CHRGFULL
)) {
782 dev_dbg(sharpsl_pm
.dev
, "Offline Charger: Charging complete.\n");
783 sharpsl_pm_led(SHARPSL_LED_OFF
);
784 sharpsl_pm
.machinfo
->charge(0);
785 sharpsl_pm
.charge_mode
= CHRG_DONE
;
791 #define sharpsl_pm_suspend NULL
792 #define sharpsl_pm_resume NULL
795 static ssize_t
battery_percentage_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
797 return sprintf(buf
, "%d\n", sharpsl_pm
.battstat
.mainbat_percent
);
800 static ssize_t
battery_voltage_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
802 return sprintf(buf
, "%d\n", sharpsl_pm
.battstat
.mainbat_voltage
);
805 static DEVICE_ATTR_RO(battery_percentage
);
806 static DEVICE_ATTR_RO(battery_voltage
);
808 extern void (*apm_get_power_status
)(struct apm_power_info
*);
810 static void sharpsl_apm_get_power_status(struct apm_power_info
*info
)
812 info
->ac_line_status
= sharpsl_pm
.battstat
.ac_status
;
814 if (sharpsl_pm
.charge_mode
== CHRG_ON
)
815 info
->battery_status
= APM_BATTERY_STATUS_CHARGING
;
817 info
->battery_status
= sharpsl_pm
.battstat
.mainbat_status
;
819 info
->battery_flag
= (1 << info
->battery_status
);
820 info
->battery_life
= sharpsl_pm
.battstat
.mainbat_percent
;
824 static const struct platform_suspend_ops sharpsl_pm_ops
= {
825 .prepare
= pxa_pm_prepare
,
826 .finish
= pxa_pm_finish
,
827 .enter
= corgi_pxa_pm_enter
,
828 .valid
= suspend_valid_only_mem
,
832 static int sharpsl_pm_probe(struct platform_device
*pdev
)
836 if (!pdev
->dev
.platform_data
)
839 sharpsl_pm
.dev
= &pdev
->dev
;
840 sharpsl_pm
.machinfo
= pdev
->dev
.platform_data
;
841 sharpsl_pm
.charge_mode
= CHRG_OFF
;
842 sharpsl_pm
.flags
= 0;
844 timer_setup(&sharpsl_pm
.ac_timer
, sharpsl_ac_timer
, 0);
846 timer_setup(&sharpsl_pm
.chrg_full_timer
, sharpsl_chrg_full_timer
, 0);
848 led_trigger_register_simple("sharpsl-charge", &sharpsl_charge_led_trigger
);
850 sharpsl_pm
.machinfo
->init();
852 gpio_request(sharpsl_pm
.machinfo
->gpio_acin
, "AC IN");
853 gpio_direction_input(sharpsl_pm
.machinfo
->gpio_acin
);
854 gpio_request(sharpsl_pm
.machinfo
->gpio_batfull
, "Battery Full");
855 gpio_direction_input(sharpsl_pm
.machinfo
->gpio_batfull
);
856 gpio_request(sharpsl_pm
.machinfo
->gpio_batlock
, "Battery Lock");
857 gpio_direction_input(sharpsl_pm
.machinfo
->gpio_batlock
);
859 /* Register interrupt handlers */
860 irq
= gpio_to_irq(sharpsl_pm
.machinfo
->gpio_acin
);
861 if (request_irq(irq
, sharpsl_ac_isr
, IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
, "AC Input Detect", sharpsl_ac_isr
)) {
862 dev_err(sharpsl_pm
.dev
, "Could not get irq %d.\n", irq
);
865 irq
= gpio_to_irq(sharpsl_pm
.machinfo
->gpio_batlock
);
866 if (request_irq(irq
, sharpsl_fatal_isr
, IRQF_TRIGGER_FALLING
, "Battery Cover", sharpsl_fatal_isr
)) {
867 dev_err(sharpsl_pm
.dev
, "Could not get irq %d.\n", irq
);
870 if (sharpsl_pm
.machinfo
->gpio_fatal
) {
871 irq
= gpio_to_irq(sharpsl_pm
.machinfo
->gpio_fatal
);
872 if (request_irq(irq
, sharpsl_fatal_isr
, IRQF_TRIGGER_FALLING
, "Fatal Battery", sharpsl_fatal_isr
)) {
873 dev_err(sharpsl_pm
.dev
, "Could not get irq %d.\n", irq
);
877 if (sharpsl_pm
.machinfo
->batfull_irq
) {
878 /* Register interrupt handler. */
879 irq
= gpio_to_irq(sharpsl_pm
.machinfo
->gpio_batfull
);
880 if (request_irq(irq
, sharpsl_chrg_full_isr
, IRQF_TRIGGER_RISING
, "CO", sharpsl_chrg_full_isr
)) {
881 dev_err(sharpsl_pm
.dev
, "Could not get irq %d.\n", irq
);
885 ret
= device_create_file(&pdev
->dev
, &dev_attr_battery_percentage
);
886 ret
|= device_create_file(&pdev
->dev
, &dev_attr_battery_voltage
);
888 dev_warn(&pdev
->dev
, "Failed to register attributes (%d)\n", ret
);
890 apm_get_power_status
= sharpsl_apm_get_power_status
;
893 suspend_set_ops(&sharpsl_pm_ops
);
896 mod_timer(&sharpsl_pm
.ac_timer
, jiffies
+ msecs_to_jiffies(250));
901 static int sharpsl_pm_remove(struct platform_device
*pdev
)
903 suspend_set_ops(NULL
);
905 device_remove_file(&pdev
->dev
, &dev_attr_battery_percentage
);
906 device_remove_file(&pdev
->dev
, &dev_attr_battery_voltage
);
908 led_trigger_unregister_simple(sharpsl_charge_led_trigger
);
910 free_irq(gpio_to_irq(sharpsl_pm
.machinfo
->gpio_acin
), sharpsl_ac_isr
);
911 free_irq(gpio_to_irq(sharpsl_pm
.machinfo
->gpio_batlock
), sharpsl_fatal_isr
);
913 if (sharpsl_pm
.machinfo
->gpio_fatal
)
914 free_irq(gpio_to_irq(sharpsl_pm
.machinfo
->gpio_fatal
), sharpsl_fatal_isr
);
916 if (sharpsl_pm
.machinfo
->batfull_irq
)
917 free_irq(gpio_to_irq(sharpsl_pm
.machinfo
->gpio_batfull
), sharpsl_chrg_full_isr
);
919 gpio_free(sharpsl_pm
.machinfo
->gpio_batlock
);
920 gpio_free(sharpsl_pm
.machinfo
->gpio_batfull
);
921 gpio_free(sharpsl_pm
.machinfo
->gpio_acin
);
923 if (sharpsl_pm
.machinfo
->exit
)
924 sharpsl_pm
.machinfo
->exit();
926 del_timer_sync(&sharpsl_pm
.chrg_full_timer
);
927 del_timer_sync(&sharpsl_pm
.ac_timer
);
932 static struct platform_driver sharpsl_pm_driver
= {
933 .probe
= sharpsl_pm_probe
,
934 .remove
= sharpsl_pm_remove
,
935 .suspend
= sharpsl_pm_suspend
,
936 .resume
= sharpsl_pm_resume
,
938 .name
= "sharpsl-pm",
942 static int sharpsl_pm_init(void)
944 return platform_driver_register(&sharpsl_pm_driver
);
947 static void sharpsl_pm_exit(void)
949 platform_driver_unregister(&sharpsl_pm_driver
);
952 late_initcall(sharpsl_pm_init
);
953 module_exit(sharpsl_pm_exit
);