1 // SPDX-License-Identifier: GPL-2.0-only
3 * Battery and Power Management code for the Sharp SL-C7xx and SL-Cxx00
6 * Copyright (c) 2004-2005 Richard Purdie
8 * Based on code written by Sharp for 2.4 kernels
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/interrupt.h>
16 #include <linux/platform_device.h>
17 #include <linux/apm-emulation.h>
18 #include <linux/timer.h>
19 #include <linux/delay.h>
20 #include <linux/leds.h>
21 #include <linux/suspend.h>
22 #include <linux/gpio.h>
25 #include <asm/mach-types.h>
27 #include "pxa2xx-regs.h"
29 #include "sharpsl_pm.h"
34 #define SHARPSL_CHARGE_ON_TIME_INTERVAL (msecs_to_jiffies(1*60*1000)) /* 1 min */
35 #define SHARPSL_CHARGE_FINISH_TIME (msecs_to_jiffies(10*60*1000)) /* 10 min */
36 #define SHARPSL_BATCHK_TIME (msecs_to_jiffies(15*1000)) /* 15 sec */
37 #define SHARPSL_BATCHK_TIME_SUSPEND (60*10) /* 10 min */
39 #define SHARPSL_WAIT_CO_TIME 15 /* 15 sec */
40 #define SHARPSL_WAIT_DISCHARGE_ON 100 /* 100 msec */
41 #define SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP 10 /* 10 msec */
42 #define SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT 10 /* 10 msec */
43 #define SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN 10 /* 10 msec */
44 #define SHARPSL_CHARGE_WAIT_TIME 15 /* 15 msec */
45 #define SHARPSL_CHARGE_CO_CHECK_TIME 5 /* 5 msec */
46 #define SHARPSL_CHARGE_RETRY_CNT 1 /* eqv. 10 min */
52 static int sharpsl_off_charge_battery(void);
53 static int sharpsl_check_battery_voltage(void);
55 static int sharpsl_check_battery_temp(void);
56 static int sharpsl_ac_check(void);
57 static int sharpsl_average_value(int ad);
58 static void sharpsl_average_clear(void);
59 static void sharpsl_charge_toggle(struct work_struct *private_);
60 static void sharpsl_battery_thread(struct work_struct *private_);
66 struct sharpsl_pm_status sharpsl_pm;
67 static DECLARE_DELAYED_WORK(toggle_charger, sharpsl_charge_toggle);
68 static DECLARE_DELAYED_WORK(sharpsl_bat, sharpsl_battery_thread);
69 DEFINE_LED_TRIGGER(sharpsl_charge_led_trigger);
73 struct battery_thresh sharpsl_battery_levels_acin[] = {
116 struct battery_thresh sharpsl_battery_levels_noac[] = {
159 /* MAX1111 Commands */
160 #define MAXCTRL_PD0 (1u << 0)
161 #define MAXCTRL_PD1 (1u << 1)
162 #define MAXCTRL_SGL (1u << 2)
163 #define MAXCTRL_UNI (1u << 3)
164 #define MAXCTRL_SEL_SH 4
165 #define MAXCTRL_STR (1u << 7)
167 extern int max1111_read_channel(int);
171 int sharpsl_pm_pxa_read_max1111(int channel)
173 /* Ugly, better move this function into another module */
174 if (machine_is_tosa())
177 /* max1111 accepts channels from 0-3, however,
178 * it is encoded from 0-7 here in the code.
180 return max1111_read_channel(channel >> 1);
183 static int get_percentage(int voltage)
185 int i = sharpsl_pm.machinfo->bat_levels - 1;
186 int bl_status = sharpsl_pm.machinfo->backlight_get_status ? sharpsl_pm.machinfo->backlight_get_status() : 0;
187 struct battery_thresh *thresh;
189 if (sharpsl_pm.charge_mode == CHRG_ON)
190 thresh = bl_status ? sharpsl_pm.machinfo->bat_levels_acin_bl : sharpsl_pm.machinfo->bat_levels_acin;
192 thresh = bl_status ? sharpsl_pm.machinfo->bat_levels_noac_bl : sharpsl_pm.machinfo->bat_levels_noac;
194 while (i > 0 && (voltage > thresh[i].voltage))
197 return thresh[i].percentage;
200 static int get_apm_status(int voltage)
202 int low_thresh, high_thresh;
204 if (sharpsl_pm.charge_mode == CHRG_ON) {
205 high_thresh = sharpsl_pm.machinfo->status_high_acin;
206 low_thresh = sharpsl_pm.machinfo->status_low_acin;
208 high_thresh = sharpsl_pm.machinfo->status_high_noac;
209 low_thresh = sharpsl_pm.machinfo->status_low_noac;
212 if (voltage >= high_thresh)
213 return APM_BATTERY_STATUS_HIGH;
214 if (voltage >= low_thresh)
215 return APM_BATTERY_STATUS_LOW;
216 return APM_BATTERY_STATUS_CRITICAL;
219 void sharpsl_battery_kick(void)
221 schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(125));
224 static void sharpsl_battery_thread(struct work_struct *private_)
226 int voltage, percent, apm_status, i;
228 if (!sharpsl_pm.machinfo)
231 sharpsl_pm.battstat.ac_status = (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN) ? APM_AC_ONLINE : APM_AC_OFFLINE);
233 /* Corgi cannot confirm when battery fully charged so periodically kick! */
234 if (!sharpsl_pm.machinfo->batfull_irq && (sharpsl_pm.charge_mode == CHRG_ON)
235 && time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_ON_TIME_INTERVAL))
236 schedule_delayed_work(&toggle_charger, 0);
238 for (i = 0; i < 5; i++) {
239 voltage = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
244 voltage = sharpsl_pm.machinfo->bat_levels_noac[0].voltage;
245 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)
255 || (apm_status == APM_BATTERY_STATUS_HIGH)
256 || percent <= sharpsl_pm.battstat.mainbat_percent) {
257 sharpsl_pm.battstat.mainbat_voltage = voltage;
258 sharpsl_pm.battstat.mainbat_status = apm_status;
259 sharpsl_pm.battstat.mainbat_percent = percent;
262 dev_dbg(sharpsl_pm.dev, "Battery: voltage: %d, status: %d, percentage: %d, time: %ld\n", voltage,
263 sharpsl_pm.battstat.mainbat_status, sharpsl_pm.battstat.mainbat_percent, jiffies);
265 /* Suspend if critical battery level */
266 if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
267 && (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL)
268 && !(sharpsl_pm.flags & SHARPSL_APM_QUEUED)) {
269 sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
270 dev_err(sharpsl_pm.dev, "Fatal Off\n");
271 apm_queue_event(APM_CRITICAL_SUSPEND);
274 schedule_delayed_work(&sharpsl_bat, SHARPSL_BATCHK_TIME);
277 void sharpsl_pm_led(int val)
279 if (val == SHARPSL_LED_ERROR) {
280 dev_err(sharpsl_pm.dev, "Charging Error!\n");
281 } else if (val == SHARPSL_LED_ON) {
282 dev_dbg(sharpsl_pm.dev, "Charge LED On\n");
283 led_trigger_event(sharpsl_charge_led_trigger, LED_FULL);
285 dev_dbg(sharpsl_pm.dev, "Charge LED Off\n");
286 led_trigger_event(sharpsl_charge_led_trigger, LED_OFF);
290 static void sharpsl_charge_on(void)
292 dev_dbg(sharpsl_pm.dev, "Turning Charger On\n");
294 sharpsl_pm.full_count = 0;
295 sharpsl_pm.charge_mode = CHRG_ON;
296 schedule_delayed_work(&toggle_charger, msecs_to_jiffies(250));
297 schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(500));
300 static void sharpsl_charge_off(void)
302 dev_dbg(sharpsl_pm.dev, "Turning Charger Off\n");
304 sharpsl_pm.machinfo->charge(0);
305 sharpsl_pm_led(SHARPSL_LED_OFF);
306 sharpsl_pm.charge_mode = CHRG_OFF;
308 schedule_delayed_work(&sharpsl_bat, 0);
311 static void sharpsl_charge_error(void)
313 sharpsl_pm_led(SHARPSL_LED_ERROR);
314 sharpsl_pm.machinfo->charge(0);
315 sharpsl_pm.charge_mode = CHRG_ERROR;
318 static void sharpsl_charge_toggle(struct work_struct *private_)
320 dev_dbg(sharpsl_pm.dev, "Toggling Charger at time: %lx\n", jiffies);
322 if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
323 sharpsl_charge_off();
325 } else if ((sharpsl_check_battery_temp() < 0) || (sharpsl_ac_check() < 0)) {
326 sharpsl_charge_error();
330 sharpsl_pm_led(SHARPSL_LED_ON);
331 sharpsl_pm.machinfo->charge(0);
332 mdelay(SHARPSL_CHARGE_WAIT_TIME);
333 sharpsl_pm.machinfo->charge(1);
335 sharpsl_pm.charge_start_time = jiffies;
338 static void sharpsl_ac_timer(struct timer_list *unused)
340 int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
342 dev_dbg(sharpsl_pm.dev, "AC Status: %d\n", acin);
344 sharpsl_average_clear();
345 if (acin && (sharpsl_pm.charge_mode != CHRG_ON))
347 else if (sharpsl_pm.charge_mode == CHRG_ON)
348 sharpsl_charge_off();
350 schedule_delayed_work(&sharpsl_bat, 0);
354 static irqreturn_t sharpsl_ac_isr(int irq, void *dev_id)
356 /* Delay the event slightly to debounce */
357 /* Must be a smaller delay than the chrg_full_isr below */
358 mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
363 static void sharpsl_chrg_full_timer(struct timer_list *unused)
365 dev_dbg(sharpsl_pm.dev, "Charge Full at time: %lx\n", jiffies);
367 sharpsl_pm.full_count++;
369 if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
370 dev_dbg(sharpsl_pm.dev, "Charge Full: AC removed - stop charging!\n");
371 if (sharpsl_pm.charge_mode == CHRG_ON)
372 sharpsl_charge_off();
373 } else if (sharpsl_pm.full_count < 2) {
374 dev_dbg(sharpsl_pm.dev, "Charge Full: Count too low\n");
375 schedule_delayed_work(&toggle_charger, 0);
376 } else if (time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_FINISH_TIME)) {
377 dev_dbg(sharpsl_pm.dev, "Charge Full: Interrupt generated too slowly - retry.\n");
378 schedule_delayed_work(&toggle_charger, 0);
380 sharpsl_charge_off();
381 sharpsl_pm.charge_mode = CHRG_DONE;
382 dev_dbg(sharpsl_pm.dev, "Charge Full: Charging Finished\n");
386 /* Charging Finished Interrupt (Not present on Corgi) */
387 /* Can trigger at the same time as an AC status change so
388 delay until after that has been processed */
389 static irqreturn_t sharpsl_chrg_full_isr(int irq, void *dev_id)
391 if (sharpsl_pm.flags & SHARPSL_SUSPENDED)
394 /* delay until after any ac interrupt */
395 mod_timer(&sharpsl_pm.chrg_full_timer, jiffies + msecs_to_jiffies(500));
400 static irqreturn_t sharpsl_fatal_isr(int irq, void *dev_id)
404 if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) {
405 dev_err(sharpsl_pm.dev, "Battery now Unlocked! Suspending.\n");
409 if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_FATAL)) {
410 dev_err(sharpsl_pm.dev, "Fatal Batt Error! Suspending.\n");
414 if (!(sharpsl_pm.flags & SHARPSL_APM_QUEUED) && is_fatal) {
415 sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
416 apm_queue_event(APM_CRITICAL_SUSPEND);
423 * Maintain an average of the last 10 readings
425 #define SHARPSL_CNV_VALUE_NUM 10
426 static int sharpsl_ad_index;
428 static void sharpsl_average_clear(void)
430 sharpsl_ad_index = 0;
433 static int sharpsl_average_value(int ad)
436 static int sharpsl_ad[SHARPSL_CNV_VALUE_NUM+1];
438 if (sharpsl_pm.battstat.mainbat_status != APM_BATTERY_STATUS_HIGH) {
439 sharpsl_ad_index = 0;
443 sharpsl_ad[sharpsl_ad_index] = ad;
445 if (sharpsl_ad_index >= SHARPSL_CNV_VALUE_NUM) {
446 for (i = 0; i < (SHARPSL_CNV_VALUE_NUM-1); i++)
447 sharpsl_ad[i] = sharpsl_ad[i+1];
448 sharpsl_ad_index = SHARPSL_CNV_VALUE_NUM - 1;
450 for (i = 0; i < sharpsl_ad_index; i++)
451 ad_val += sharpsl_ad[i];
453 return ad_val / sharpsl_ad_index;
457 * Take an array of 5 integers, remove the maximum and minimum values
458 * and return the average.
460 static int get_select_val(int *val)
462 int i, j, k, temp, sum = 0;
467 for (i = 1; i < 5; i++) {
477 for (i = 3; i >= 0; i--) {
484 for (i = 0; i < 5; i++)
485 if (i != j && i != k)
488 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]);
493 static int sharpsl_check_battery_temp(void)
497 /* Check battery temperature */
498 for (i = 0; i < 5; i++) {
499 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
500 sharpsl_pm.machinfo->measure_temp(1);
501 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
502 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_TEMP);
503 sharpsl_pm.machinfo->measure_temp(0);
506 val = get_select_val(buff);
508 dev_dbg(sharpsl_pm.dev, "Temperature: %d\n", val);
509 if (val > sharpsl_pm.machinfo->charge_on_temp) {
510 printk(KERN_WARNING "Not charging: temperature out of limits.\n");
518 static int sharpsl_check_battery_voltage(void)
522 /* disable charge, enable discharge */
523 sharpsl_pm.machinfo->charge(0);
524 sharpsl_pm.machinfo->discharge(1);
525 mdelay(SHARPSL_WAIT_DISCHARGE_ON);
527 if (sharpsl_pm.machinfo->discharge1)
528 sharpsl_pm.machinfo->discharge1(1);
530 /* Check battery voltage */
531 for (i = 0; i < 5; i++) {
532 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
533 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
536 if (sharpsl_pm.machinfo->discharge1)
537 sharpsl_pm.machinfo->discharge1(0);
539 sharpsl_pm.machinfo->discharge(0);
541 val = get_select_val(buff);
542 dev_dbg(sharpsl_pm.dev, "Battery Voltage: %d\n", val);
544 if (val < sharpsl_pm.machinfo->charge_on_volt)
551 static int sharpsl_ac_check(void)
553 int temp, i, buff[5];
555 for (i = 0; i < 5; i++) {
556 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_ACIN_VOLT);
557 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN);
560 temp = get_select_val(buff);
561 dev_dbg(sharpsl_pm.dev, "AC Voltage: %d\n", temp);
563 if ((temp > sharpsl_pm.machinfo->charge_acin_high) || (temp < sharpsl_pm.machinfo->charge_acin_low)) {
564 dev_err(sharpsl_pm.dev, "Error: AC check failed: voltage %d.\n", temp);
572 static int sharpsl_pm_suspend(struct platform_device *pdev, pm_message_t state)
574 sharpsl_pm.flags |= SHARPSL_SUSPENDED;
575 flush_delayed_work(&toggle_charger);
576 flush_delayed_work(&sharpsl_bat);
578 if (sharpsl_pm.charge_mode == CHRG_ON)
579 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
581 sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
586 static int sharpsl_pm_resume(struct platform_device *pdev)
588 /* Clear the reset source indicators as they break the bootloader upon reboot */
590 sharpsl_average_clear();
591 sharpsl_pm.flags &= ~SHARPSL_APM_QUEUED;
592 sharpsl_pm.flags &= ~SHARPSL_SUSPENDED;
597 static void corgi_goto_sleep(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
599 dev_dbg(sharpsl_pm.dev, "Time is: %08x\n", RCNR);
601 dev_dbg(sharpsl_pm.dev, "Offline Charge Activate = %d\n", sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG);
602 /* not charging and AC-IN! */
604 if ((sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG) && (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN))) {
605 dev_dbg(sharpsl_pm.dev, "Activating Offline Charger...\n");
606 sharpsl_pm.charge_mode = CHRG_OFF;
607 sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
608 sharpsl_off_charge_battery();
611 sharpsl_pm.machinfo->presuspend();
613 PEDR = 0xffffffff; /* clear it */
615 sharpsl_pm.flags &= ~SHARPSL_ALARM_ACTIVE;
616 if ((sharpsl_pm.charge_mode == CHRG_ON) && ((alarm_enable && ((alarm_time - RCNR) > (SHARPSL_BATCHK_TIME_SUSPEND + 30))) || !alarm_enable)) {
618 RTAR = RCNR + SHARPSL_BATCHK_TIME_SUSPEND;
619 dev_dbg(sharpsl_pm.dev, "Charging alarm at: %08x\n", RTAR);
620 sharpsl_pm.flags |= SHARPSL_ALARM_ACTIVE;
621 } else if (alarm_enable) {
624 dev_dbg(sharpsl_pm.dev, "User alarm at: %08x\n", RTAR);
626 dev_dbg(sharpsl_pm.dev, "No alarms set.\n");
631 sharpsl_pm.machinfo->postsuspend();
633 dev_dbg(sharpsl_pm.dev, "Corgi woken up from suspend: %08x\n", PEDR);
636 static int corgi_enter_suspend(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
638 if (!sharpsl_pm.machinfo->should_wakeup(!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE) && alarm_enable)) {
639 if (!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE)) {
640 dev_dbg(sharpsl_pm.dev, "No user triggered wakeup events and not charging. Strange. Suspend.\n");
641 corgi_goto_sleep(alarm_time, alarm_enable, state);
644 if (sharpsl_off_charge_battery()) {
645 dev_dbg(sharpsl_pm.dev, "Charging. Suspend...\n");
646 corgi_goto_sleep(alarm_time, alarm_enable, state);
649 dev_dbg(sharpsl_pm.dev, "User triggered wakeup in offline charger.\n");
652 if ((!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) ||
653 (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_FATAL))) {
654 dev_err(sharpsl_pm.dev, "Fatal condition. Suspend.\n");
655 corgi_goto_sleep(alarm_time, alarm_enable, state);
662 static int corgi_pxa_pm_enter(suspend_state_t state)
664 unsigned long alarm_time = RTAR;
665 unsigned int alarm_status = ((RTSR & RTSR_ALE) != 0);
667 dev_dbg(sharpsl_pm.dev, "SharpSL suspending for first time.\n");
669 corgi_goto_sleep(alarm_time, alarm_status, state);
671 while (corgi_enter_suspend(alarm_time, alarm_status, state))
674 if (sharpsl_pm.machinfo->earlyresume)
675 sharpsl_pm.machinfo->earlyresume();
677 dev_dbg(sharpsl_pm.dev, "SharpSL resuming...\n");
682 static int sharpsl_off_charge_error(void)
684 dev_err(sharpsl_pm.dev, "Offline Charger: Error occurred.\n");
685 sharpsl_pm.machinfo->charge(0);
686 sharpsl_pm_led(SHARPSL_LED_ERROR);
687 sharpsl_pm.charge_mode = CHRG_ERROR;
692 * Charging Control while suspended
693 * Return 1 - go straight to sleep
694 * Return 0 - sleep or wakeup depending on other factors
696 static int sharpsl_off_charge_battery(void)
700 dev_dbg(sharpsl_pm.dev, "Charge Mode: %d\n", sharpsl_pm.charge_mode);
702 if (sharpsl_pm.charge_mode == CHRG_OFF) {
703 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 1\n");
706 if ((sharpsl_ac_check() < 0) || (sharpsl_check_battery_temp() < 0))
707 return sharpsl_off_charge_error();
710 sharpsl_pm_led(SHARPSL_LED_ON);
711 sharpsl_pm.machinfo->charge(0);
712 mdelay(SHARPSL_CHARGE_WAIT_TIME);
713 sharpsl_pm.machinfo->charge(1);
715 sharpsl_pm.charge_mode = CHRG_ON;
716 sharpsl_pm.full_count = 0;
719 } else if (sharpsl_pm.charge_mode != CHRG_ON) {
723 if (sharpsl_pm.full_count == 0) {
726 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 2\n");
728 if ((sharpsl_check_battery_temp() < 0) || (sharpsl_check_battery_voltage() < 0))
729 return sharpsl_off_charge_error();
731 sharpsl_pm.machinfo->charge(0);
732 mdelay(SHARPSL_CHARGE_WAIT_TIME);
733 sharpsl_pm.machinfo->charge(1);
734 sharpsl_pm.charge_mode = CHRG_ON;
736 mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
740 /* Check if any wakeup event had occurred */
741 if (sharpsl_pm.machinfo->charger_wakeup())
743 /* Check for timeout */
744 if ((RCNR - time) > SHARPSL_WAIT_CO_TIME)
746 if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) {
747 dev_dbg(sharpsl_pm.dev, "Offline Charger: Charge full occurred. Retrying to check\n");
748 sharpsl_pm.full_count++;
749 sharpsl_pm.machinfo->charge(0);
750 mdelay(SHARPSL_CHARGE_WAIT_TIME);
751 sharpsl_pm.machinfo->charge(1);
757 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 3\n");
759 mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
763 /* Check if any wakeup event had occurred */
764 if (sharpsl_pm.machinfo->charger_wakeup())
766 /* Check for timeout */
767 if ((RCNR-time) > SHARPSL_WAIT_CO_TIME) {
768 if (sharpsl_pm.full_count > SHARPSL_CHARGE_RETRY_CNT) {
769 dev_dbg(sharpsl_pm.dev, "Offline Charger: Not charged sufficiently. Retrying.\n");
770 sharpsl_pm.full_count = 0;
772 sharpsl_pm.full_count++;
775 if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) {
776 dev_dbg(sharpsl_pm.dev, "Offline Charger: Charging complete.\n");
777 sharpsl_pm_led(SHARPSL_LED_OFF);
778 sharpsl_pm.machinfo->charge(0);
779 sharpsl_pm.charge_mode = CHRG_DONE;
785 #define sharpsl_pm_suspend NULL
786 #define sharpsl_pm_resume NULL
789 static ssize_t battery_percentage_show(struct device *dev, struct device_attribute *attr, char *buf)
791 return sprintf(buf, "%d\n", sharpsl_pm.battstat.mainbat_percent);
794 static ssize_t battery_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
796 return sprintf(buf, "%d\n", sharpsl_pm.battstat.mainbat_voltage);
799 static DEVICE_ATTR_RO(battery_percentage);
800 static DEVICE_ATTR_RO(battery_voltage);
802 extern void (*apm_get_power_status)(struct apm_power_info *);
804 static void sharpsl_apm_get_power_status(struct apm_power_info *info)
806 info->ac_line_status = sharpsl_pm.battstat.ac_status;
808 if (sharpsl_pm.charge_mode == CHRG_ON)
809 info->battery_status = APM_BATTERY_STATUS_CHARGING;
811 info->battery_status = sharpsl_pm.battstat.mainbat_status;
813 info->battery_flag = (1 << info->battery_status);
814 info->battery_life = sharpsl_pm.battstat.mainbat_percent;
818 static const struct platform_suspend_ops sharpsl_pm_ops = {
819 .prepare = pxa_pm_prepare,
820 .finish = pxa_pm_finish,
821 .enter = corgi_pxa_pm_enter,
822 .valid = suspend_valid_only_mem,
826 static int sharpsl_pm_probe(struct platform_device *pdev)
830 if (!pdev->dev.platform_data)
833 sharpsl_pm.dev = &pdev->dev;
834 sharpsl_pm.machinfo = pdev->dev.platform_data;
835 sharpsl_pm.charge_mode = CHRG_OFF;
836 sharpsl_pm.flags = 0;
838 timer_setup(&sharpsl_pm.ac_timer, sharpsl_ac_timer, 0);
840 timer_setup(&sharpsl_pm.chrg_full_timer, sharpsl_chrg_full_timer, 0);
842 led_trigger_register_simple("sharpsl-charge", &sharpsl_charge_led_trigger);
844 sharpsl_pm.machinfo->init();
846 gpio_request(sharpsl_pm.machinfo->gpio_acin, "AC IN");
847 gpio_direction_input(sharpsl_pm.machinfo->gpio_acin);
848 gpio_request(sharpsl_pm.machinfo->gpio_batfull, "Battery Full");
849 gpio_direction_input(sharpsl_pm.machinfo->gpio_batfull);
850 gpio_request(sharpsl_pm.machinfo->gpio_batlock, "Battery Lock");
851 gpio_direction_input(sharpsl_pm.machinfo->gpio_batlock);
853 /* Register interrupt handlers */
854 irq = gpio_to_irq(sharpsl_pm.machinfo->gpio_acin);
855 if (request_irq(irq, sharpsl_ac_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "AC Input Detect", sharpsl_ac_isr)) {
856 dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", irq);
859 irq = gpio_to_irq(sharpsl_pm.machinfo->gpio_batlock);
860 if (request_irq(irq, sharpsl_fatal_isr, IRQF_TRIGGER_FALLING, "Battery Cover", sharpsl_fatal_isr)) {
861 dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", irq);
864 if (sharpsl_pm.machinfo->gpio_fatal) {
865 irq = gpio_to_irq(sharpsl_pm.machinfo->gpio_fatal);
866 if (request_irq(irq, sharpsl_fatal_isr, IRQF_TRIGGER_FALLING, "Fatal Battery", sharpsl_fatal_isr)) {
867 dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", irq);
871 if (sharpsl_pm.machinfo->batfull_irq) {
872 /* Register interrupt handler. */
873 irq = gpio_to_irq(sharpsl_pm.machinfo->gpio_batfull);
874 if (request_irq(irq, sharpsl_chrg_full_isr, IRQF_TRIGGER_RISING, "CO", sharpsl_chrg_full_isr)) {
875 dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", irq);
879 ret = device_create_file(&pdev->dev, &dev_attr_battery_percentage);
880 ret |= device_create_file(&pdev->dev, &dev_attr_battery_voltage);
882 dev_warn(&pdev->dev, "Failed to register attributes (%d)\n", ret);
884 apm_get_power_status = sharpsl_apm_get_power_status;
887 suspend_set_ops(&sharpsl_pm_ops);
890 mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
895 static int sharpsl_pm_remove(struct platform_device *pdev)
897 suspend_set_ops(NULL);
899 device_remove_file(&pdev->dev, &dev_attr_battery_percentage);
900 device_remove_file(&pdev->dev, &dev_attr_battery_voltage);
902 led_trigger_unregister_simple(sharpsl_charge_led_trigger);
904 free_irq(gpio_to_irq(sharpsl_pm.machinfo->gpio_acin), sharpsl_ac_isr);
905 free_irq(gpio_to_irq(sharpsl_pm.machinfo->gpio_batlock), sharpsl_fatal_isr);
907 if (sharpsl_pm.machinfo->gpio_fatal)
908 free_irq(gpio_to_irq(sharpsl_pm.machinfo->gpio_fatal), sharpsl_fatal_isr);
910 if (sharpsl_pm.machinfo->batfull_irq)
911 free_irq(gpio_to_irq(sharpsl_pm.machinfo->gpio_batfull), sharpsl_chrg_full_isr);
913 gpio_free(sharpsl_pm.machinfo->gpio_batlock);
914 gpio_free(sharpsl_pm.machinfo->gpio_batfull);
915 gpio_free(sharpsl_pm.machinfo->gpio_acin);
917 if (sharpsl_pm.machinfo->exit)
918 sharpsl_pm.machinfo->exit();
920 del_timer_sync(&sharpsl_pm.chrg_full_timer);
921 del_timer_sync(&sharpsl_pm.ac_timer);
926 static struct platform_driver sharpsl_pm_driver = {
927 .probe = sharpsl_pm_probe,
928 .remove = sharpsl_pm_remove,
929 .suspend = sharpsl_pm_suspend,
930 .resume = sharpsl_pm_resume,
932 .name = "sharpsl-pm",
936 static int sharpsl_pm_init(void)
938 return platform_driver_register(&sharpsl_pm_driver);
941 static void sharpsl_pm_exit(void)
943 platform_driver_unregister(&sharpsl_pm_driver);
946 late_initcall(sharpsl_pm_init);
947 module_exit(sharpsl_pm_exit);