[ARM] 3228/1: SharpSL: Move PM code to arch/arm/common
authorRichard Purdie <rpurdie@rpsys.net>
Thu, 5 Jan 2006 20:44:55 +0000 (20:44 +0000)
committerRussell King <rmk+kernel@arm.linux.org.uk>
Thu, 5 Jan 2006 20:44:55 +0000 (20:44 +0000)
Patch from Richard Purdie

This patch moves a large chunk of the sharpsl_pm driver to
arch/arm/common so that it can be reused on other devices such as the
SL-5500 (collie). It also abstracts some functions from the core into
the machine and platform specific parts of the driver to aid reuse.

Signed-off-by: Richard Purdie <rpurdie@rpsys.net>
Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
arch/arm/common/Kconfig
arch/arm/common/Makefile
arch/arm/common/sharpsl_pm.c [new file with mode: 0644]
arch/arm/mach-pxa/Kconfig
arch/arm/mach-pxa/corgi_pm.c
arch/arm/mach-pxa/sharpsl.h
arch/arm/mach-pxa/sharpsl_pm.c
arch/arm/mach-pxa/spitz_pm.c
drivers/video/backlight/corgi_bl.c
include/asm-arm/hardware/sharpsl_pm.h [new file with mode: 0644]

index 666ba393575b8d7800b5b8a14d4e5ce1466932bc..d7509c7a3c5e651a2634ea04f203aca8a5226f4f 100644 (file)
@@ -23,5 +23,8 @@ config SHARP_LOCOMO
 config SHARP_PARAM
        bool
 
+config SHARPSL_PM
+       bool
+
 config SHARP_SCOOP
        bool
index a87886564b19e0b9c33798cdae6947114e0e5947..2685051b30131666c703f5bd607eb9878ab6fefc 100644 (file)
@@ -13,4 +13,5 @@ obj-$(CONFIG_DMABOUNCE)               += dmabounce.o
 obj-$(CONFIG_TIMER_ACORN)      += time-acorn.o
 obj-$(CONFIG_SHARP_LOCOMO)     += locomo.o
 obj-$(CONFIG_SHARP_PARAM)      += sharpsl_param.o
+obj-$(CONFIG_SHARPSL_PM)       += sharpsl_pm.o
 obj-$(CONFIG_SHARP_SCOOP)      += scoop.o
diff --git a/arch/arm/common/sharpsl_pm.c b/arch/arm/common/sharpsl_pm.c
new file mode 100644 (file)
index 0000000..978d32e
--- /dev/null
@@ -0,0 +1,839 @@
+/*
+ * Battery and Power Management code for the Sharp SL-C7xx and SL-Cxx00
+ * series of PDAs
+ *
+ * Copyright (c) 2004-2005 Richard Purdie
+ *
+ * Based on code written by Sharp for 2.4 kernels
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#undef DEBUG
+
+#include <linux/module.h>
+#include <linux/timer.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/apm_bios.h>
+#include <linux/delay.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+
+#include <asm/hardware.h>
+#include <asm/mach-types.h>
+#include <asm/irq.h>
+#include <asm/apm.h>
+#include <asm/arch/pm.h>
+#include <asm/arch/pxa-regs.h>
+#include <asm/arch/sharpsl.h>
+#include <asm/hardware/sharpsl_pm.h>
+
+/*
+ * Constants
+ */
+#define SHARPSL_CHARGE_ON_TIME_INTERVAL        (msecs_to_jiffies(1*60*1000))  /* 1 min */
+#define SHARPSL_CHARGE_FINISH_TIME             (msecs_to_jiffies(10*60*1000)) /* 10 min */
+#define SHARPSL_BATCHK_TIME                    (msecs_to_jiffies(15*1000))    /* 15 sec */
+#define SHARPSL_BATCHK_TIME_SUSPEND            (60*10)                        /* 10 min */
+#define SHARPSL_WAIT_CO_TIME                   15  /* 15 sec */
+#define SHARPSL_WAIT_DISCHARGE_ON              100 /* 100 msec */
+#define SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP   10  /* 10 msec */
+#define SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT   10  /* 10 msec */
+#define SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN   10  /* 10 msec */
+#define SHARPSL_CHARGE_WAIT_TIME               15  /* 15 msec */
+#define SHARPSL_CHARGE_CO_CHECK_TIME           5   /* 5 msec */
+#define SHARPSL_CHARGE_RETRY_CNT               1   /* eqv. 10 min */
+
+#define SHARPSL_CHARGE_ON_VOLT         0x99  /* 2.9V */
+#define SHARPSL_CHARGE_ON_TEMP         0xe0  /* 2.9V */
+#define SHARPSL_CHARGE_ON_ACIN_HIGH    0x9b  /* 6V */
+#define SHARPSL_CHARGE_ON_ACIN_LOW     0x34  /* 2V */
+#define SHARPSL_FATAL_ACIN_VOLT        182   /* 3.45V */
+#define SHARPSL_FATAL_NOACIN_VOLT      170   /* 3.40V */
+
+/*
+ * Prototypes
+ */
+static int sharpsl_off_charge_battery(void);
+static int sharpsl_check_battery_temp(void);
+static int sharpsl_check_battery_voltage(void);
+static int sharpsl_ac_check(void);
+static int sharpsl_fatal_check(void);
+static int sharpsl_average_value(int ad);
+static void sharpsl_average_clear(void);
+static void sharpsl_charge_toggle(void *private_);
+static void sharpsl_battery_thread(void *private_);
+
+
+/*
+ * Variables
+ */
+struct sharpsl_pm_status sharpsl_pm;
+DECLARE_WORK(toggle_charger, sharpsl_charge_toggle, NULL);
+DECLARE_WORK(sharpsl_bat, sharpsl_battery_thread, NULL);
+
+
+static int get_percentage(int voltage)
+{
+       int i = sharpsl_pm.machinfo->bat_levels - 1;
+       struct battery_thresh *thresh;
+
+       if (sharpsl_pm.charge_mode == CHRG_ON)
+               thresh=sharpsl_pm.machinfo->bat_levels_acin;
+       else
+               thresh=sharpsl_pm.machinfo->bat_levels_noac;
+
+       while (i > 0 && (voltage > thresh[i].voltage))
+               i--;
+
+       return thresh[i].percentage;
+}
+
+static int get_apm_status(int voltage)
+{
+       int low_thresh, high_thresh;
+
+       if (sharpsl_pm.charge_mode == CHRG_ON) {
+               high_thresh = sharpsl_pm.machinfo->status_high_acin;
+               low_thresh = sharpsl_pm.machinfo->status_low_acin;
+       } else {
+               high_thresh = sharpsl_pm.machinfo->status_high_noac;
+               low_thresh = sharpsl_pm.machinfo->status_low_noac;
+       }
+
+       if (voltage >= high_thresh)
+               return APM_BATTERY_STATUS_HIGH;
+       if (voltage >= low_thresh)
+               return APM_BATTERY_STATUS_LOW;
+       return APM_BATTERY_STATUS_CRITICAL;
+}
+
+void sharpsl_battery_kick(void)
+{
+       schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(125));
+}
+EXPORT_SYMBOL(sharpsl_battery_kick);
+
+
+static void sharpsl_battery_thread(void *private_)
+{
+       int voltage, percent, apm_status, i = 0;
+
+       if (!sharpsl_pm.machinfo)
+               return;
+
+       sharpsl_pm.battstat.ac_status = (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN) ? APM_AC_ONLINE : APM_AC_OFFLINE);
+
+       /* Corgi cannot confirm when battery fully charged so periodically kick! */
+       if (machine_is_corgi() && (sharpsl_pm.charge_mode == CHRG_ON)
+                       && time_after(jiffies, sharpsl_pm.charge_start_time +  SHARPSL_CHARGE_ON_TIME_INTERVAL))
+               schedule_work(&toggle_charger);
+
+       while(1) {
+               voltage = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
+
+               if (voltage > 0) break;
+               if (i++ > 5) {
+                       voltage = sharpsl_pm.machinfo->bat_levels_noac[0].voltage;
+                       dev_warn(sharpsl_pm.dev, "Warning: Cannot read main battery!\n");
+                       break;
+               }
+       }
+
+       voltage = sharpsl_average_value(voltage);
+       apm_status = get_apm_status(voltage);
+       percent = get_percentage(voltage);
+
+       /* At low battery voltages, the voltage has a tendency to start
+           creeping back up so we try to avoid this here */
+       if ((sharpsl_pm.battstat.ac_status == APM_AC_ONLINE) || (apm_status == APM_BATTERY_STATUS_HIGH) ||  percent <= sharpsl_pm.battstat.mainbat_percent) {
+               sharpsl_pm.battstat.mainbat_voltage = voltage;
+               sharpsl_pm.battstat.mainbat_status = apm_status;
+               sharpsl_pm.battstat.mainbat_percent = percent;
+       }
+
+       dev_dbg(sharpsl_pm.dev, "Battery: voltage: %d, status: %d, percentage: %d, time: %d\n", voltage,
+                       sharpsl_pm.battstat.mainbat_status, sharpsl_pm.battstat.mainbat_percent, jiffies);
+
+       /* If battery is low. limit backlight intensity to save power. */
+       if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
+                       && ((sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_LOW) ||
+                       (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL))) {
+               if (!(sharpsl_pm.flags & SHARPSL_BL_LIMIT)) {
+                       corgibl_limit_intensity(1);
+                       sharpsl_pm.flags |= SHARPSL_BL_LIMIT;
+               }
+       } else if (sharpsl_pm.flags & SHARPSL_BL_LIMIT) {
+               corgibl_limit_intensity(0);
+               sharpsl_pm.flags &= ~SHARPSL_BL_LIMIT;
+       }
+
+       /* Suspend if critical battery level */
+       if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
+                       && (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL)
+                       && !(sharpsl_pm.flags & SHARPSL_APM_QUEUED)) {
+               sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
+               dev_err(sharpsl_pm.dev, "Fatal Off\n");
+               apm_queue_event(APM_CRITICAL_SUSPEND);
+       }
+
+       schedule_delayed_work(&sharpsl_bat, SHARPSL_BATCHK_TIME);
+}
+
+void sharpsl_pm_led(int val)
+{
+       if (val == SHARPSL_LED_ERROR) {
+               dev_err(sharpsl_pm.dev, "Charging Error!\n");
+       } else if (val == SHARPSL_LED_ON) {
+               dev_dbg(sharpsl_pm.dev, "Charge LED On\n");
+
+       } else {
+               dev_dbg(sharpsl_pm.dev, "Charge LED Off\n");
+
+       }
+}
+
+static void sharpsl_charge_on(void)
+{
+       dev_dbg(sharpsl_pm.dev, "Turning Charger On\n");
+
+       sharpsl_pm.full_count = 0;
+       sharpsl_pm.charge_mode = CHRG_ON;
+       schedule_delayed_work(&toggle_charger, msecs_to_jiffies(250));
+       schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(500));
+}
+
+static void sharpsl_charge_off(void)
+{
+       dev_dbg(sharpsl_pm.dev, "Turning Charger Off\n");
+
+       sharpsl_pm.machinfo->charge(0);
+       sharpsl_pm_led(SHARPSL_LED_OFF);
+       sharpsl_pm.charge_mode = CHRG_OFF;
+
+       schedule_work(&sharpsl_bat);
+}
+
+static void sharpsl_charge_error(void)
+{
+       sharpsl_pm_led(SHARPSL_LED_ERROR);
+       sharpsl_pm.machinfo->charge(0);
+       sharpsl_pm.charge_mode = CHRG_ERROR;
+}
+
+static void sharpsl_charge_toggle(void *private_)
+{
+       dev_dbg(sharpsl_pm.dev, "Toogling Charger at time: %lx\n", jiffies);
+
+       if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
+               sharpsl_charge_off();
+               return;
+       } else if ((sharpsl_check_battery_temp() < 0) || (sharpsl_ac_check() < 0)) {
+               sharpsl_charge_error();
+               return;
+       }
+
+       sharpsl_pm_led(SHARPSL_LED_ON);
+       sharpsl_pm.machinfo->charge(0);
+       mdelay(SHARPSL_CHARGE_WAIT_TIME);
+       sharpsl_pm.machinfo->charge(1);
+
+       sharpsl_pm.charge_start_time = jiffies;
+}
+
+static void sharpsl_ac_timer(unsigned long data)
+{
+       int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
+
+       dev_dbg(sharpsl_pm.dev, "AC Status: %d\n",acin);
+
+       sharpsl_average_clear();
+       if (acin && (sharpsl_pm.charge_mode != CHRG_ON))
+               sharpsl_charge_on();
+       else if (sharpsl_pm.charge_mode == CHRG_ON)
+               sharpsl_charge_off();
+
+       schedule_work(&sharpsl_bat);
+}
+
+
+irqreturn_t sharpsl_ac_isr(int irq, void *dev_id, struct pt_regs *fp)
+{
+       /* Delay the event slightly to debounce */
+       /* Must be a smaller delay than the chrg_full_isr below */
+       mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
+
+       return IRQ_HANDLED;
+}
+
+static void sharpsl_chrg_full_timer(unsigned long data)
+{
+       dev_dbg(sharpsl_pm.dev, "Charge Full at time: %lx\n", jiffies);
+
+       sharpsl_pm.full_count++;
+
+       if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
+               dev_dbg(sharpsl_pm.dev, "Charge Full: AC removed - stop charging!\n");
+               if (sharpsl_pm.charge_mode == CHRG_ON)
+                       sharpsl_charge_off();
+       } else if (sharpsl_pm.full_count < 2) {
+               dev_dbg(sharpsl_pm.dev, "Charge Full: Count too low\n");
+               schedule_work(&toggle_charger);
+       } else if (time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_FINISH_TIME)) {
+               dev_dbg(sharpsl_pm.dev, "Charge Full: Interrupt generated too slowly - retry.\n");
+               schedule_work(&toggle_charger);
+       } else {
+               sharpsl_charge_off();
+               sharpsl_pm.charge_mode = CHRG_DONE;
+               dev_dbg(sharpsl_pm.dev, "Charge Full: Charging Finished\n");
+       }
+}
+
+/* Charging Finished Interrupt (Not present on Corgi) */
+/* Can trigger at the same time as an AC staus change so
+   delay until after that has been processed */
+irqreturn_t sharpsl_chrg_full_isr(int irq, void *dev_id, struct pt_regs *fp)
+{
+       if (sharpsl_pm.flags & SHARPSL_SUSPENDED)
+               return IRQ_HANDLED;
+
+       /* delay until after any ac interrupt */
+       mod_timer(&sharpsl_pm.chrg_full_timer, jiffies + msecs_to_jiffies(500));
+
+       return IRQ_HANDLED;
+}
+
+irqreturn_t sharpsl_fatal_isr(int irq, void *dev_id, struct pt_regs *fp)
+{
+       int is_fatal = 0;
+
+       if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) {
+               dev_err(sharpsl_pm.dev, "Battery now Unlocked! Suspending.\n");
+               is_fatal = 1;
+       }
+
+       if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_FATAL)) {
+               dev_err(sharpsl_pm.dev, "Fatal Batt Error! Suspending.\n");
+               is_fatal = 1;
+       }
+
+       if (!(sharpsl_pm.flags & SHARPSL_APM_QUEUED) && is_fatal) {
+               sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
+               apm_queue_event(APM_CRITICAL_SUSPEND);
+       }
+
+       return IRQ_HANDLED;
+}
+
+/*
+ * Maintain an average of the last 10 readings
+ */
+#define SHARPSL_CNV_VALUE_NUM    10
+static int sharpsl_ad_index;
+
+static void sharpsl_average_clear(void)
+{
+       sharpsl_ad_index = 0;
+}
+
+static int sharpsl_average_value(int ad)
+{
+       int i, ad_val = 0;
+       static int sharpsl_ad[SHARPSL_CNV_VALUE_NUM+1];
+
+       if (sharpsl_pm.battstat.mainbat_status != APM_BATTERY_STATUS_HIGH) {
+               sharpsl_ad_index = 0;
+               return ad;
+       }
+
+       sharpsl_ad[sharpsl_ad_index] = ad;
+       sharpsl_ad_index++;
+       if (sharpsl_ad_index >= SHARPSL_CNV_VALUE_NUM) {
+               for (i=0; i < (SHARPSL_CNV_VALUE_NUM-1); i++)
+                       sharpsl_ad[i] = sharpsl_ad[i+1];
+               sharpsl_ad_index = SHARPSL_CNV_VALUE_NUM - 1;
+       }
+       for (i=0; i < sharpsl_ad_index; i++)
+               ad_val += sharpsl_ad[i];
+
+       return (ad_val / sharpsl_ad_index);
+}
+
+/*
+ * Take an array of 5 integers, remove the maximum and minimum values
+ * and return the average.
+ */
+static int get_select_val(int *val)
+{
+       int i, j, k, temp, sum = 0;
+
+       /* Find MAX val */
+       temp = val[0];
+       j=0;
+       for (i=1; i<5; i++) {
+               if (temp < val[i]) {
+                       temp = val[i];
+                       j = i;
+               }
+       }
+
+       /* Find MIN val */
+       temp = val[4];
+       k=4;
+       for (i=3; i>=0; i--) {
+               if (temp > val[i]) {
+                       temp = val[i];
+                       k = i;
+               }
+       }
+
+       for (i=0; i<5; i++)
+               if (i != j && i != k )
+                       sum += val[i];
+
+       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]);
+
+       return (sum/3);
+}
+
+static int sharpsl_check_battery_temp(void)
+{
+       int val, i, buff[5];
+
+       /* Check battery temperature */
+       for (i=0; i<5; i++) {
+               mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
+               sharpsl_pm.machinfo->measure_temp(1);
+               mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
+               buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_TEMP);
+               sharpsl_pm.machinfo->measure_temp(0);
+       }
+
+       val = get_select_val(buff);
+
+       dev_dbg(sharpsl_pm.dev, "Temperature: %d\n", val);
+       if (val > SHARPSL_CHARGE_ON_TEMP)
+               return -1;
+
+       return 0;
+}
+
+static int sharpsl_check_battery_voltage(void)
+{
+       int val, i, buff[5];
+
+       /* disable charge, enable discharge */
+       sharpsl_pm.machinfo->charge(0);
+       sharpsl_pm.machinfo->discharge(1);
+       mdelay(SHARPSL_WAIT_DISCHARGE_ON);
+
+       if (sharpsl_pm.machinfo->discharge1)
+               sharpsl_pm.machinfo->discharge1(1);
+
+       /* Check battery voltage */
+       for (i=0; i<5; i++) {
+               buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
+               mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
+       }
+
+       if (sharpsl_pm.machinfo->discharge1)
+               sharpsl_pm.machinfo->discharge1(0);
+
+       sharpsl_pm.machinfo->discharge(0);
+
+       val = get_select_val(buff);
+       dev_dbg(sharpsl_pm.dev, "Battery Voltage: %d\n", val);
+
+       if (val < SHARPSL_CHARGE_ON_VOLT)
+               return -1;
+
+       return 0;
+}
+
+static int sharpsl_ac_check(void)
+{
+       int temp, i, buff[5];
+
+       for (i=0; i<5; i++) {
+               buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_ACIN_VOLT);
+               mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN);
+       }
+
+       temp = get_select_val(buff);
+       dev_dbg(sharpsl_pm.dev, "AC Voltage: %d\n",temp);
+
+       if ((temp > SHARPSL_CHARGE_ON_ACIN_HIGH) || (temp < SHARPSL_CHARGE_ON_ACIN_LOW)) {
+               dev_err(sharpsl_pm.dev, "Error: AC check failed.\n");
+               return -1;
+       }
+
+       return 0;
+}
+
+#ifdef CONFIG_PM
+static int sharpsl_pm_suspend(struct platform_device *pdev, pm_message_t state)
+{
+       sharpsl_pm.flags |= SHARPSL_SUSPENDED;
+       flush_scheduled_work();
+
+       if (sharpsl_pm.charge_mode == CHRG_ON)
+               sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
+       else
+               sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
+
+       return 0;
+}
+
+static int sharpsl_pm_resume(struct platform_device *pdev)
+{
+       /* Clear the reset source indicators as they break the bootloader upon reboot */
+       RCSR = 0x0f;
+       sharpsl_average_clear();
+       sharpsl_pm.flags &= ~SHARPSL_APM_QUEUED;
+       sharpsl_pm.flags &= ~SHARPSL_SUSPENDED;
+
+       return 0;
+}
+
+static void corgi_goto_sleep(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
+{
+       dev_dbg(sharpsl_pm.dev, "Time is: %08x\n",RCNR);
+
+       dev_dbg(sharpsl_pm.dev, "Offline Charge Activate = %d\n",sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG);
+       /* not charging and AC-IN! */
+
+       if ((sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG) && (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN))) {
+               dev_dbg(sharpsl_pm.dev, "Activating Offline Charger...\n");
+               sharpsl_pm.charge_mode = CHRG_OFF;
+               sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
+               sharpsl_off_charge_battery();
+       }
+
+       sharpsl_pm.machinfo->presuspend();
+
+       PEDR = 0xffffffff; /* clear it */
+
+       sharpsl_pm.flags &= ~SHARPSL_ALARM_ACTIVE;
+       if ((sharpsl_pm.charge_mode == CHRG_ON) && ((alarm_enable && ((alarm_time - RCNR) > (SHARPSL_BATCHK_TIME_SUSPEND + 30))) || !alarm_enable)) {
+               RTSR &= RTSR_ALE;
+               RTAR = RCNR + SHARPSL_BATCHK_TIME_SUSPEND;
+               dev_dbg(sharpsl_pm.dev, "Charging alarm at: %08x\n",RTAR);
+               sharpsl_pm.flags |= SHARPSL_ALARM_ACTIVE;
+       } else if (alarm_enable) {
+               RTSR &= RTSR_ALE;
+               RTAR = alarm_time;
+               dev_dbg(sharpsl_pm.dev, "User alarm at: %08x\n",RTAR);
+       } else {
+               dev_dbg(sharpsl_pm.dev, "No alarms set.\n");
+       }
+
+       pxa_pm_enter(state);
+
+       sharpsl_pm.machinfo->postsuspend();
+
+       dev_dbg(sharpsl_pm.dev, "Corgi woken up from suspend: %08x\n",PEDR);
+}
+
+static int corgi_enter_suspend(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
+{
+       if (!sharpsl_pm.machinfo->should_wakeup(!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE) && alarm_enable) )
+       {
+               if (!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE)) {
+                       dev_dbg(sharpsl_pm.dev, "No user triggered wakeup events and not charging. Strange. Suspend.\n");
+                       corgi_goto_sleep(alarm_time, alarm_enable, state);
+                       return 1;
+               }
+               if(sharpsl_off_charge_battery()) {
+                       dev_dbg(sharpsl_pm.dev, "Charging. Suspend...\n");
+                       corgi_goto_sleep(alarm_time, alarm_enable, state);
+                       return 1;
+               }
+               dev_dbg(sharpsl_pm.dev, "User triggered wakeup in offline charger.\n");
+       }
+
+       if ((!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) || (sharpsl_fatal_check() < 0) )
+       {
+               dev_err(sharpsl_pm.dev, "Fatal condition. Suspend.\n");
+               corgi_goto_sleep(alarm_time, alarm_enable, state);
+               return 1;
+       }
+
+       return 0;
+}
+
+static int corgi_pxa_pm_enter(suspend_state_t state)
+{
+       unsigned long alarm_time = RTAR;
+       unsigned int alarm_status = ((RTSR & RTSR_ALE) != 0);
+
+       dev_dbg(sharpsl_pm.dev, "SharpSL suspending for first time.\n");
+
+       corgi_goto_sleep(alarm_time, alarm_status, state);
+
+       while (corgi_enter_suspend(alarm_time,alarm_status,state))
+               {}
+
+       dev_dbg(sharpsl_pm.dev, "SharpSL resuming...\n");
+
+       return 0;
+}
+#endif
+
+
+/*
+ * Check for fatal battery errors
+ * Fatal returns -1
+ */
+static int sharpsl_fatal_check(void)
+{
+       int buff[5], temp, i, acin;
+
+       dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check entered\n");
+
+       /* Check AC-Adapter */
+       acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
+
+       if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) {
+               sharpsl_pm.machinfo->charge(0);
+               udelay(100);
+               sharpsl_pm.machinfo->discharge(1);      /* enable discharge */
+               mdelay(SHARPSL_WAIT_DISCHARGE_ON);
+       }
+
+       if (sharpsl_pm.machinfo->discharge1)
+               sharpsl_pm.machinfo->discharge1(1);
+
+       /* Check battery : check inserting battery ? */
+       for (i=0; i<5; i++) {
+               buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
+               mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
+       }
+
+       if (sharpsl_pm.machinfo->discharge1)
+               sharpsl_pm.machinfo->discharge1(0);
+
+       if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) {
+               udelay(100);
+               sharpsl_pm.machinfo->charge(1);
+               sharpsl_pm.machinfo->discharge(0);
+       }
+
+       temp = get_select_val(buff);
+       dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check: acin: %d, discharge voltage: %d, no discharge: %d\n", acin, temp, sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT));
+
+       if ((acin && (temp < SHARPSL_FATAL_ACIN_VOLT)) ||
+                       (!acin && (temp < SHARPSL_FATAL_NOACIN_VOLT)))
+               return -1;
+       return 0;
+}
+
+static int sharpsl_off_charge_error(void)
+{
+       dev_err(sharpsl_pm.dev, "Offline Charger: Error occured.\n");
+       sharpsl_pm.machinfo->charge(0);
+       sharpsl_pm_led(SHARPSL_LED_ERROR);
+       sharpsl_pm.charge_mode = CHRG_ERROR;
+       return 1;
+}
+
+/*
+ * Charging Control while suspended
+ * Return 1 - go straight to sleep
+ * Return 0 - sleep or wakeup depending on other factors
+ */
+static int sharpsl_off_charge_battery(void)
+{
+       int time;
+
+       dev_dbg(sharpsl_pm.dev, "Charge Mode: %d\n", sharpsl_pm.charge_mode);
+
+       if (sharpsl_pm.charge_mode == CHRG_OFF) {
+               dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 1\n");
+
+               /* AC Check */
+               if ((sharpsl_ac_check() < 0) || (sharpsl_check_battery_temp() < 0))
+                       return sharpsl_off_charge_error();
+
+               /* Start Charging */
+               sharpsl_pm_led(SHARPSL_LED_ON);
+               sharpsl_pm.machinfo->charge(0);
+               mdelay(SHARPSL_CHARGE_WAIT_TIME);
+               sharpsl_pm.machinfo->charge(1);
+
+               sharpsl_pm.charge_mode = CHRG_ON;
+               sharpsl_pm.full_count = 0;
+
+               return 1;
+       } else if (sharpsl_pm.charge_mode != CHRG_ON) {
+               return 1;
+       }
+
+       if (sharpsl_pm.full_count == 0) {
+               int time;
+
+               dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 2\n");
+
+               if ((sharpsl_check_battery_temp() < 0) || (sharpsl_check_battery_voltage() < 0))
+                       return sharpsl_off_charge_error();
+
+               sharpsl_pm.machinfo->charge(0);
+               mdelay(SHARPSL_CHARGE_WAIT_TIME);
+               sharpsl_pm.machinfo->charge(1);
+               sharpsl_pm.charge_mode = CHRG_ON;
+
+               mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
+
+               time = RCNR;
+               while(1) {
+                       /* Check if any wakeup event had occured */
+                       if (sharpsl_pm.machinfo->charger_wakeup() != 0)
+                               return 0;
+                       /* Check for timeout */
+                       if ((RCNR - time) > SHARPSL_WAIT_CO_TIME)
+                               return 1;
+                       if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) {
+                               dev_dbg(sharpsl_pm.dev, "Offline Charger: Charge full occured. Retrying to check\n");
+                               sharpsl_pm.full_count++;
+                               sharpsl_pm.machinfo->charge(0);
+                               mdelay(SHARPSL_CHARGE_WAIT_TIME);
+                               sharpsl_pm.machinfo->charge(1);
+                               return 1;
+                       }
+               }
+       }
+
+       dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 3\n");
+
+       mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
+
+       time = RCNR;
+       while(1) {
+               /* Check if any wakeup event had occured */
+               if (sharpsl_pm.machinfo->charger_wakeup() != 0)
+                       return 0;
+               /* Check for timeout */
+               if ((RCNR-time) > SHARPSL_WAIT_CO_TIME) {
+                       if (sharpsl_pm.full_count > SHARPSL_CHARGE_RETRY_CNT) {
+                               dev_dbg(sharpsl_pm.dev, "Offline Charger: Not charged sufficiently. Retrying.\n");
+                               sharpsl_pm.full_count = 0;
+                       }
+                       sharpsl_pm.full_count++;
+                       return 1;
+               }
+               if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) {
+                       dev_dbg(sharpsl_pm.dev, "Offline Charger: Charging complete.\n");
+                       sharpsl_pm_led(SHARPSL_LED_OFF);
+                       sharpsl_pm.machinfo->charge(0);
+                       sharpsl_pm.charge_mode = CHRG_DONE;
+                       return 1;
+               }
+       }
+}
+
+
+static ssize_t battery_percentage_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       return sprintf(buf, "%d\n",sharpsl_pm.battstat.mainbat_percent);
+}
+
+static ssize_t battery_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       return sprintf(buf, "%d\n",sharpsl_pm.battstat.mainbat_voltage);
+}
+
+static DEVICE_ATTR(battery_percentage, 0444, battery_percentage_show, NULL);
+static DEVICE_ATTR(battery_voltage, 0444, battery_voltage_show, NULL);
+
+extern void (*apm_get_power_status)(struct apm_power_info *);
+
+static void sharpsl_apm_get_power_status(struct apm_power_info *info)
+{
+       info->ac_line_status = sharpsl_pm.battstat.ac_status;
+
+       if (sharpsl_pm.charge_mode == CHRG_ON)
+               info->battery_status = APM_BATTERY_STATUS_CHARGING;
+       else
+               info->battery_status = sharpsl_pm.battstat.mainbat_status;
+
+       info->battery_flag = (1 << info->battery_status);
+       info->battery_life = sharpsl_pm.battstat.mainbat_percent;
+}
+
+static struct pm_ops sharpsl_pm_ops = {
+       .pm_disk_mode   = PM_DISK_FIRMWARE,
+       .prepare        = pxa_pm_prepare,
+       .enter          = corgi_pxa_pm_enter,
+       .finish         = pxa_pm_finish,
+};
+
+static int __init sharpsl_pm_probe(struct platform_device *pdev)
+{
+       if (!pdev->dev.platform_data)
+               return -EINVAL;
+
+       sharpsl_pm.dev = &pdev->dev;
+       sharpsl_pm.machinfo = pdev->dev.platform_data;
+       sharpsl_pm.charge_mode = CHRG_OFF;
+       sharpsl_pm.flags = 0;
+
+       init_timer(&sharpsl_pm.ac_timer);
+       sharpsl_pm.ac_timer.function = sharpsl_ac_timer;
+
+       init_timer(&sharpsl_pm.chrg_full_timer);
+       sharpsl_pm.chrg_full_timer.function = sharpsl_chrg_full_timer;
+
+       sharpsl_pm.machinfo->init();
+
+       device_create_file(&pdev->dev, &dev_attr_battery_percentage);
+       device_create_file(&pdev->dev, &dev_attr_battery_voltage);
+
+       apm_get_power_status = sharpsl_apm_get_power_status;
+
+       pm_set_ops(&sharpsl_pm_ops);
+
+       mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
+
+       return 0;
+}
+
+static int sharpsl_pm_remove(struct platform_device *pdev)
+{
+       pm_set_ops(NULL);
+
+       device_remove_file(&pdev->dev, &dev_attr_battery_percentage);
+       device_remove_file(&pdev->dev, &dev_attr_battery_voltage);
+
+       sharpsl_pm.machinfo->exit();
+
+       del_timer_sync(&sharpsl_pm.chrg_full_timer);
+       del_timer_sync(&sharpsl_pm.ac_timer);
+
+       return 0;
+}
+
+static struct platform_driver sharpsl_pm_driver = {
+       .probe          = sharpsl_pm_probe,
+       .remove         = sharpsl_pm_remove,
+       .suspend        = sharpsl_pm_suspend,
+       .resume         = sharpsl_pm_resume,
+       .driver         = {
+               .name           = "sharpsl-pm",
+       },
+};
+
+static int __devinit sharpsl_pm_init(void)
+{
+       return platform_driver_register(&sharpsl_pm_driver);
+}
+
+static void sharpsl_pm_exit(void)
+{
+       platform_driver_unregister(&sharpsl_pm_driver);
+}
+
+late_initcall(sharpsl_pm_init);
+module_exit(sharpsl_pm_exit);
index 2a58499c0968793001e7b4827cea8238091fdf0e..c1d77f5b38234da82e39b8eb7bb558ebdc174f6d 100644 (file)
@@ -112,12 +112,14 @@ config IWMMXT
 config PXA_SHARP_C7xx
        bool
        select PXA_SSP
+       select SHARPSL_PM
        help
          Enable support for all Sharp C7xx models
 
 config PXA_SHARP_Cxx00
        bool
        select PXA_SSP
+       select SHARPSL_PM
        help
          Enable common support for Sharp Cxx00 models
 
index 599be14754f938f8776ca502340b44df13b416fa..de8b2403c929125002ffeeaa821e794271994377 100644 (file)
@@ -21,6 +21,7 @@
 #include <asm/mach-types.h>
 #include <asm/hardware.h>
 #include <asm/hardware/scoop.h>
+#include <asm/hardware/sharpsl_pm.h>
 
 #include <asm/arch/sharpsl.h>
 #include <asm/arch/corgi.h>
@@ -33,19 +34,7 @@ static void corgi_charger_init(void)
        pxa_gpio_mode(CORGI_GPIO_CHRG_ON | GPIO_OUT);
        pxa_gpio_mode(CORGI_GPIO_CHRG_UKN | GPIO_OUT);
        pxa_gpio_mode(CORGI_GPIO_KEY_INT | GPIO_IN);
-}
-
-static void corgi_charge_led(int val)
-{
-       if (val == SHARPSL_LED_ERROR) {
-               dev_dbg(sharpsl_pm.dev, "Charge LED Error\n");
-       } else if (val == SHARPSL_LED_ON) {
-               dev_dbg(sharpsl_pm.dev, "Charge LED On\n");
-               GPSR0 = GPIO_bit(CORGI_GPIO_LED_ORANGE);
-       } else {
-               dev_dbg(sharpsl_pm.dev, "Charge LED Off\n");
-               GPCR0 = GPIO_bit(CORGI_GPIO_LED_ORANGE);
-       }
+       sharpsl_pm_pxa_init();
 }
 
 static void corgi_measure_temp(int on)
@@ -138,15 +127,15 @@ static int corgi_should_wakeup(unsigned int resume_on_alarm)
        dev_dbg(sharpsl_pm.dev, "GPLR0 = %x,%x\n", GPLR0, PEDR);
 
        if ((PEDR & GPIO_bit(CORGI_GPIO_AC_IN))) {
-               if (STATUS_AC_IN()) {
+               if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
                        /* charge on */
                        dev_dbg(sharpsl_pm.dev, "ac insert\n");
                        sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
                } else {
                        /* charge off */
                        dev_dbg(sharpsl_pm.dev, "ac remove\n");
-                       CHARGE_LED_OFF();
-                       CHARGE_OFF();
+                       sharpsl_pm_led(SHARPSL_LED_OFF);
+                       sharpsl_pm.machinfo->charge(0);
                        sharpsl_pm.charge_mode = CHRG_OFF;
                }
        }
@@ -172,23 +161,39 @@ static unsigned long corgi_charger_wakeup(void)
        return ~GPLR0 & ( GPIO_bit(CORGI_GPIO_AC_IN) | GPIO_bit(CORGI_GPIO_KEY_INT) | GPIO_bit(CORGI_GPIO_WAKEUP) );
 }
 
-static int corgi_acin_status(void)
+unsigned long corgipm_read_devdata(int type)
 {
-       return ((GPLR(CORGI_GPIO_AC_IN) & GPIO_bit(CORGI_GPIO_AC_IN)) != 0);
+       switch(type) {
+       case SHARPSL_STATUS_ACIN:
+               return ((GPLR(CORGI_GPIO_AC_IN) & GPIO_bit(CORGI_GPIO_AC_IN)) != 0);
+       case SHARPSL_STATUS_LOCK:
+               return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batlock);
+       case SHARPSL_STATUS_CHRGFULL:
+               return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batfull);
+       case SHARPSL_STATUS_FATAL:
+               return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_fatal);
+       case SHARPSL_ACIN_VOLT:
+               return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT);
+       case SHARPSL_BATT_TEMP:
+               return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP);
+       case SHARPSL_BATT_VOLT:
+       default:
+               return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
+       }
 }
 
 static struct sharpsl_charger_machinfo corgi_pm_machinfo = {
        .init            = corgi_charger_init,
+       .exit            = sharpsl_pm_pxa_remove,
        .gpio_batlock    = CORGI_GPIO_BAT_COVER,
        .gpio_acin       = CORGI_GPIO_AC_IN,
        .gpio_batfull    = CORGI_GPIO_CHRG_FULL,
-       .status_acin     = corgi_acin_status,
        .discharge       = corgi_discharge,
        .charge          = corgi_charge,
-       .chargeled       = corgi_charge_led,
        .measure_temp    = corgi_measure_temp,
        .presuspend      = corgi_presuspend,
        .postsuspend     = corgi_postsuspend,
+       .read_devdata    = corgipm_read_devdata,
        .charger_wakeup  = corgi_charger_wakeup,
        .should_wakeup   = corgi_should_wakeup,
        .bat_levels      = 40,
index b0c40a1d6671e17d5b5f78cd4f12e8734a8e61b4..92b79305050974f8c6ffe1875287fe4b8a573200 100644 (file)
@@ -1,7 +1,16 @@
 /*
- * SharpSL SSP Driver
+ * Copyright (c) 2004-2005 Richard Purdie
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
  */
 
+
+/*
+ * SharpSL SSP Driver
+ */
 struct corgissp_machinfo {
        int port;
        int cs_lcdcon;
@@ -14,18 +23,18 @@ struct corgissp_machinfo {
 
 void corgi_ssp_set_machinfo(struct corgissp_machinfo *machinfo);
 
+
 /*
  * SharpSL Backlight
  */
-
 void corgi_bl_set_intensity(int intensity);
 void spitz_bl_set_intensity(int intensity);
 void akita_bl_set_intensity(int intensity);
 
+
 /*
  * SharpSL Touchscreen Driver
  */
-
 unsigned long corgi_get_hsync_len(void);
 unsigned long spitz_get_hsync_len(void);
 void corgi_put_hsync(void);
@@ -33,89 +42,22 @@ void spitz_put_hsync(void);
 void corgi_wait_hsync(void);
 void spitz_wait_hsync(void);
 
+
 /*
  * SharpSL Battery/PM Driver
  */
 
-struct sharpsl_charger_machinfo {
-       void (*init)(void);
-       int gpio_acin;
-       int gpio_batfull;
-       int gpio_batlock;
-       int gpio_fatal;
-       int (*status_acin)(void);
-       void (*discharge)(int);
-       void (*discharge1)(int);
-       void (*charge)(int);
-       void (*chargeled)(int);
-       void (*measure_temp)(int);
-       void (*presuspend)(void);
-       void (*postsuspend)(void);
-       unsigned long (*charger_wakeup)(void);
-       int (*should_wakeup)(unsigned int resume_on_alarm);
-       int bat_levels;
-       struct battery_thresh *bat_levels_noac;
-       struct battery_thresh *bat_levels_acin;
-       int status_high_acin;
-       int status_low_acin;
-       int status_high_noac;
-       int status_low_noac;
-};
-
-struct battery_thresh {
-       int voltage;
-       int percentage;
-};
-
-struct battery_stat {
-       int ac_status;         /* APM AC Present/Not Present */
-       int mainbat_status;    /* APM Main Battery Status */
-       int mainbat_percent;   /* Main Battery Percentage Charge */
-       int mainbat_voltage;   /* Main Battery Voltage */
-};
-
-struct sharpsl_pm_status {
-       struct device *dev;
-       struct timer_list ac_timer;
-       struct timer_list chrg_full_timer;
-
-       int charge_mode;
-#define CHRG_ERROR    (-1)
-#define CHRG_OFF      (0)
-#define CHRG_ON       (1)
-#define CHRG_DONE     (2)
-
-       unsigned int flags;
-#define SHARPSL_SUSPENDED       (1 << 0)  /* Device is Suspended */
-#define SHARPSL_ALARM_ACTIVE    (1 << 1)  /* Alarm is for charging event (not user) */
-#define SHARPSL_BL_LIMIT        (1 << 2)  /* Backlight Intensity Limited */
-#define SHARPSL_APM_QUEUED      (1 << 3)  /* APM Event Queued */
-#define SHARPSL_DO_OFFLINE_CHRG (1 << 4)  /* Trigger the offline charger */
+#define READ_GPIO_BIT(x)    (GPLR(x) & GPIO_bit(x))
 
-       int full_count;
-       unsigned long charge_start_time;
-       struct sharpsl_charger_machinfo *machinfo;
-       struct battery_stat battstat;
-};
+/* MAX1111 Channel Definitions */
+#define MAX1111_BATT_VOLT   4u
+#define MAX1111_BATT_TEMP   2u
+#define MAX1111_ACIN_VOLT   6u
 
-extern struct sharpsl_pm_status sharpsl_pm;
 extern struct battery_thresh spitz_battery_levels_acin[];
 extern struct battery_thresh spitz_battery_levels_noac[];
+void sharpsl_pm_pxa_init(void);
+void sharpsl_pm_pxa_remove(void);
+int sharpsl_pm_pxa_read_max1111(int channel);
 
-#define READ_GPIO_BIT(x)    (GPLR(x) & GPIO_bit(x))
-
-#define SHARPSL_LED_ERROR  2
-#define SHARPSL_LED_ON     1
-#define SHARPSL_LED_OFF    0
 
-#define CHARGE_ON()         sharpsl_pm.machinfo->charge(1)
-#define CHARGE_OFF()        sharpsl_pm.machinfo->charge(0)
-#define CHARGE_LED_ON()     sharpsl_pm.machinfo->chargeled(SHARPSL_LED_ON)
-#define CHARGE_LED_OFF()    sharpsl_pm.machinfo->chargeled(SHARPSL_LED_OFF)
-#define CHARGE_LED_ERR()    sharpsl_pm.machinfo->chargeled(SHARPSL_LED_ERROR)
-#define DISCHARGE_ON()      sharpsl_pm.machinfo->discharge(1)
-#define DISCHARGE_OFF()     sharpsl_pm.machinfo->discharge(0)
-#define STATUS_AC_IN()      sharpsl_pm.machinfo->status_acin()
-#define STATUS_BATT_LOCKED()  READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batlock)
-#define STATUS_CHRG_FULL()  READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batfull)
-#define STATUS_FATAL()      READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_fatal)
index c10be00fb52671ae7e7dc6fd2e1ccc93cb8ac722..f6fefb181411e45d91a4fd735924fb407db01398 100644 (file)
 #undef DEBUG
 
 #include <linux/module.h>
-#include <linux/timer.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
-#include <linux/apm_bios.h>
-#include <linux/delay.h>
 #include <linux/interrupt.h>
 #include <linux/platform_device.h>
 
 #include <asm/hardware.h>
-#include <asm/hardware/scoop.h>
 #include <asm/mach-types.h>
 #include <asm/irq.h>
 #include <asm/apm.h>
-
 #include <asm/arch/pm.h>
 #include <asm/arch/pxa-regs.h>
 #include <asm/arch/sharpsl.h>
+#include <asm/hardware/sharpsl_pm.h>
 #include "sharpsl.h"
 
-/*
- * Constants
- */
-#define SHARPSL_CHARGE_ON_TIME_INTERVAL        (msecs_to_jiffies(1*60*1000))  /* 1 min */
-#define SHARPSL_CHARGE_FINISH_TIME             (msecs_to_jiffies(10*60*1000)) /* 10 min */
-#define SHARPSL_BATCHK_TIME                    (msecs_to_jiffies(15*1000))    /* 15 sec */
-#define SHARPSL_BATCHK_TIME_SUSPEND            (60*10)                        /* 10 min */
-#define SHARPSL_WAIT_CO_TIME                   15  /* 15 sec */
-#define SHARPSL_WAIT_DISCHARGE_ON              100 /* 100 msec */
-#define SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP   10  /* 10 msec */
-#define SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT   10  /* 10 msec */
-#define SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN   10  /* 10 msec */
-#define SHARPSL_CHARGE_WAIT_TIME               15  /* 15 msec */
-#define SHARPSL_CHARGE_CO_CHECK_TIME           5   /* 5 msec */
-#define SHARPSL_CHARGE_RETRY_CNT               1   /* eqv. 10 min */
-
-#define SHARPSL_CHARGE_ON_VOLT         0x99  /* 2.9V */
-#define SHARPSL_CHARGE_ON_TEMP         0xe0  /* 2.9V */
-#define SHARPSL_CHARGE_ON_ACIN_HIGH    0x9b  /* 6V */
-#define SHARPSL_CHARGE_ON_ACIN_LOW     0x34  /* 2V */
-#define SHARPSL_FATAL_ACIN_VOLT        182   /* 3.45V */
-#define SHARPSL_FATAL_NOACIN_VOLT      170   /* 3.40V */
-
 struct battery_thresh spitz_battery_levels_acin[] = {
        { 213, 100},
        { 212,  98},
@@ -151,763 +124,17 @@ struct battery_thresh  spitz_battery_levels_noac[] = {
 #define MAXCTRL_SEL_SH   4
 #define MAXCTRL_STR      1u << 7
 
-/* MAX1111 Channel Definitions */
-#define BATT_AD    4u
-#define BATT_THM   2u
-#define JK_VAD     6u
-
-
-/*
- * Prototypes
- */
-static int sharpsl_read_main_battery(void);
-static int sharpsl_off_charge_battery(void);
-static int sharpsl_check_battery_temp(void);
-static int sharpsl_check_battery_voltage(void);
-static int sharpsl_ac_check(void);
-static int sharpsl_fatal_check(void);
-static int sharpsl_average_value(int ad);
-static void sharpsl_average_clear(void);
-static void sharpsl_charge_toggle(void *private_);
-static void sharpsl_battery_thread(void *private_);
-
-
-/*
- * Variables
- */
-struct sharpsl_pm_status sharpsl_pm;
-DECLARE_WORK(toggle_charger, sharpsl_charge_toggle, NULL);
-DECLARE_WORK(sharpsl_bat, sharpsl_battery_thread, NULL);
-
-
-static int get_percentage(int voltage)
-{
-       int i = sharpsl_pm.machinfo->bat_levels - 1;
-       struct battery_thresh *thresh;
-
-       if (sharpsl_pm.charge_mode == CHRG_ON)
-               thresh=sharpsl_pm.machinfo->bat_levels_acin;
-       else
-               thresh=sharpsl_pm.machinfo->bat_levels_noac;
-
-       while (i > 0 && (voltage > thresh[i].voltage))
-               i--;
-
-       return thresh[i].percentage;
-}
-
-static int get_apm_status(int voltage)
-{
-       int low_thresh, high_thresh;
-
-       if (sharpsl_pm.charge_mode == CHRG_ON) {
-               high_thresh = sharpsl_pm.machinfo->status_high_acin;
-               low_thresh = sharpsl_pm.machinfo->status_low_acin;
-       } else {
-               high_thresh = sharpsl_pm.machinfo->status_high_noac;
-               low_thresh = sharpsl_pm.machinfo->status_low_noac;
-       }
-
-       if (voltage >= high_thresh)
-               return APM_BATTERY_STATUS_HIGH;
-       if (voltage >= low_thresh)
-               return APM_BATTERY_STATUS_LOW;
-       return APM_BATTERY_STATUS_CRITICAL;
-}
-
-void sharpsl_battery_kick(void)
-{
-       schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(125));
-}
-EXPORT_SYMBOL(sharpsl_battery_kick);
-
-
-static void sharpsl_battery_thread(void *private_)
-{
-       int voltage, percent, apm_status, i = 0;
-
-       if (!sharpsl_pm.machinfo)
-               return;
-
-       sharpsl_pm.battstat.ac_status = (STATUS_AC_IN() ? APM_AC_ONLINE : APM_AC_OFFLINE);
-
-       /* Corgi cannot confirm when battery fully charged so periodically kick! */
-       if (machine_is_corgi() && (sharpsl_pm.charge_mode == CHRG_ON)
-                       && time_after(jiffies, sharpsl_pm.charge_start_time +  SHARPSL_CHARGE_ON_TIME_INTERVAL))
-               schedule_work(&toggle_charger);
-
-       while(1) {
-               voltage = sharpsl_read_main_battery();
-               if (voltage > 0) break;
-               if (i++ > 5) {
-                       voltage = sharpsl_pm.machinfo->bat_levels_noac[0].voltage;
-                       dev_warn(sharpsl_pm.dev, "Warning: Cannot read main battery!\n");
-                       break;
-               }
-       }
-
-       voltage = sharpsl_average_value(voltage);
-       apm_status = get_apm_status(voltage);
-       percent = get_percentage(voltage);
-
-       /* At low battery voltages, the voltage has a tendency to start
-           creeping back up so we try to avoid this here */
-       if ((sharpsl_pm.battstat.ac_status == APM_AC_ONLINE) || (apm_status == APM_BATTERY_STATUS_HIGH) ||  percent <= sharpsl_pm.battstat.mainbat_percent) {
-               sharpsl_pm.battstat.mainbat_voltage = voltage;
-               sharpsl_pm.battstat.mainbat_status = apm_status;
-               sharpsl_pm.battstat.mainbat_percent = percent;
-       }
-
-       dev_dbg(sharpsl_pm.dev, "Battery: voltage: %d, status: %d, percentage: %d, time: %d\n", voltage,
-                       sharpsl_pm.battstat.mainbat_status, sharpsl_pm.battstat.mainbat_percent, jiffies);
-
-       /* If battery is low. limit backlight intensity to save power. */
-       if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
-                       && ((sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_LOW) ||
-                       (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL))) {
-               if (!(sharpsl_pm.flags & SHARPSL_BL_LIMIT)) {
-                       corgibl_limit_intensity(1);
-                       sharpsl_pm.flags |= SHARPSL_BL_LIMIT;
-               }
-       } else if (sharpsl_pm.flags & SHARPSL_BL_LIMIT) {
-               corgibl_limit_intensity(0);
-               sharpsl_pm.flags &= ~SHARPSL_BL_LIMIT;
-       }
-
-       /* Suspend if critical battery level */
-       if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
-                       && (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL)
-                       && !(sharpsl_pm.flags & SHARPSL_APM_QUEUED)) {
-               sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
-               dev_err(sharpsl_pm.dev, "Fatal Off\n");
-               apm_queue_event(APM_CRITICAL_SUSPEND);
-       }
-
-       schedule_delayed_work(&sharpsl_bat, SHARPSL_BATCHK_TIME);
-}
-
-static void sharpsl_charge_on(void)
-{
-       dev_dbg(sharpsl_pm.dev, "Turning Charger On\n");
-
-       sharpsl_pm.full_count = 0;
-       sharpsl_pm.charge_mode = CHRG_ON;
-       schedule_delayed_work(&toggle_charger, msecs_to_jiffies(250));
-       schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(500));
-}
-
-static void sharpsl_charge_off(void)
-{
-       dev_dbg(sharpsl_pm.dev, "Turning Charger Off\n");
-
-       CHARGE_OFF();
-       CHARGE_LED_OFF();
-       sharpsl_pm.charge_mode = CHRG_OFF;
-
-       schedule_work(&sharpsl_bat);
-}
-
-static void sharpsl_charge_error(void)
-{
-       CHARGE_LED_ERR();
-       CHARGE_OFF();
-       sharpsl_pm.charge_mode = CHRG_ERROR;
-}
-
-static void sharpsl_charge_toggle(void *private_)
-{
-       dev_dbg(sharpsl_pm.dev, "Toogling Charger at time: %lx\n", jiffies);
-
-       if (STATUS_AC_IN() == 0) {
-               sharpsl_charge_off();
-               return;
-       } else if ((sharpsl_check_battery_temp() < 0) || (sharpsl_ac_check() < 0)) {
-               sharpsl_charge_error();
-               return;
-       }
-
-       CHARGE_LED_ON();
-       CHARGE_OFF();
-       mdelay(SHARPSL_CHARGE_WAIT_TIME);
-       CHARGE_ON();
-
-       sharpsl_pm.charge_start_time = jiffies;
-}
-
-static void sharpsl_ac_timer(unsigned long data)
-{
-       int acin = STATUS_AC_IN();
-
-       dev_dbg(sharpsl_pm.dev, "AC Status: %d\n",acin);
-
-       sharpsl_average_clear();
-       if (acin && (sharpsl_pm.charge_mode != CHRG_ON))
-               sharpsl_charge_on();
-       else if (sharpsl_pm.charge_mode == CHRG_ON)
-               sharpsl_charge_off();
-
-       schedule_work(&sharpsl_bat);
-}
-
-
-static irqreturn_t sharpsl_ac_isr(int irq, void *dev_id, struct pt_regs *fp)
-{
-       /* Delay the event slightly to debounce */
-       /* Must be a smaller delay than the chrg_full_isr below */
-       mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
-
-       return IRQ_HANDLED;
-}
-
-static void sharpsl_chrg_full_timer(unsigned long data)
-{
-       dev_dbg(sharpsl_pm.dev, "Charge Full at time: %lx\n", jiffies);
-
-       sharpsl_pm.full_count++;
-
-       if (STATUS_AC_IN() == 0) {
-               dev_dbg(sharpsl_pm.dev, "Charge Full: AC removed - stop charging!\n");
-               if (sharpsl_pm.charge_mode == CHRG_ON)
-                       sharpsl_charge_off();
-       } else if (sharpsl_pm.full_count < 2) {
-               dev_dbg(sharpsl_pm.dev, "Charge Full: Count too low\n");
-               schedule_work(&toggle_charger);
-       } else if (time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_FINISH_TIME)) {
-               dev_dbg(sharpsl_pm.dev, "Charge Full: Interrupt generated too slowly - retry.\n");
-               schedule_work(&toggle_charger);
-       } else {
-               sharpsl_charge_off();
-               sharpsl_pm.charge_mode = CHRG_DONE;
-               dev_dbg(sharpsl_pm.dev, "Charge Full: Charging Finished\n");
-       }
-}
-
-/* Charging Finished Interrupt (Not present on Corgi) */
-/* Can trigger at the same time as an AC staus change so
-   delay until after that has been processed */
-static irqreturn_t sharpsl_chrg_full_isr(int irq, void *dev_id, struct pt_regs *fp)
-{
-       if (sharpsl_pm.flags & SHARPSL_SUSPENDED)
-               return IRQ_HANDLED;
-
-       /* delay until after any ac interrupt */
-       mod_timer(&sharpsl_pm.chrg_full_timer, jiffies + msecs_to_jiffies(500));
-
-       return IRQ_HANDLED;
-}
-
-static irqreturn_t sharpsl_fatal_isr(int irq, void *dev_id, struct pt_regs *fp)
-{
-       int is_fatal = 0;
-
-       if (STATUS_BATT_LOCKED() == 0) {
-               dev_err(sharpsl_pm.dev, "Battery now Unlocked! Suspending.\n");
-               is_fatal = 1;
-       }
-
-       if (sharpsl_pm.machinfo->gpio_fatal && (STATUS_FATAL() == 0)) {
-               dev_err(sharpsl_pm.dev, "Fatal Batt Error! Suspending.\n");
-               is_fatal = 1;
-       }
-
-       if (!(sharpsl_pm.flags & SHARPSL_APM_QUEUED) && is_fatal) {
-               sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
-               apm_queue_event(APM_CRITICAL_SUSPEND);
-       }
-
-       return IRQ_HANDLED;
-}
-
-/*
- * Maintain an average of the last 10 readings
- */
-#define SHARPSL_CNV_VALUE_NUM    10
-static int sharpsl_ad_index;
-
-static void sharpsl_average_clear(void)
-{
-       sharpsl_ad_index = 0;
-}
-
-static int sharpsl_average_value(int ad)
-{
-       int i, ad_val = 0;
-       static int sharpsl_ad[SHARPSL_CNV_VALUE_NUM+1];
-
-       if (sharpsl_pm.battstat.mainbat_status != APM_BATTERY_STATUS_HIGH) {
-               sharpsl_ad_index = 0;
-               return ad;
-       }
-
-       sharpsl_ad[sharpsl_ad_index] = ad;
-       sharpsl_ad_index++;
-       if (sharpsl_ad_index >= SHARPSL_CNV_VALUE_NUM) {
-               for (i=0; i < (SHARPSL_CNV_VALUE_NUM-1); i++)
-                       sharpsl_ad[i] = sharpsl_ad[i+1];
-               sharpsl_ad_index = SHARPSL_CNV_VALUE_NUM - 1;
-       }
-       for (i=0; i < sharpsl_ad_index; i++)
-               ad_val += sharpsl_ad[i];
-
-       return (ad_val / sharpsl_ad_index);
-}
-
-
 /*
  * Read MAX1111 ADC
  */
-static int read_max1111(int channel)
+int sharpsl_pm_pxa_read_max1111(int channel)
 {
        return corgi_ssp_max1111_get((channel << MAXCTRL_SEL_SH) | MAXCTRL_PD0 | MAXCTRL_PD1
                        | MAXCTRL_SGL | MAXCTRL_UNI | MAXCTRL_STR);
 }
 
-static int sharpsl_read_main_battery(void)
-{
-       return read_max1111(BATT_AD);
-}
-
-static int sharpsl_read_temp(void)
+void sharpsl_pm_pxa_init(void)
 {
-       int temp;
-
-       sharpsl_pm.machinfo->measure_temp(1);
-
-       mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
-       temp = read_max1111(BATT_THM);
-
-       sharpsl_pm.machinfo->measure_temp(0);
-
-       return temp;
-}
-
-static int sharpsl_read_acin(void)
-{
-       return read_max1111(JK_VAD);
-}
-
-/*
- * Take an array of 5 integers, remove the maximum and minimum values
- * and return the average.
- */
-static int get_select_val(int *val)
-{
-       int i, j, k, temp, sum = 0;
-
-       /* Find MAX val */
-       temp = val[0];
-       j=0;
-       for (i=1; i<5; i++) {
-               if (temp < val[i]) {
-                       temp = val[i];
-                       j = i;
-               }
-       }
-
-       /* Find MIN val */
-       temp = val[4];
-       k=4;
-       for (i=3; i>=0; i--) {
-               if (temp > val[i]) {
-                       temp = val[i];
-                       k = i;
-               }
-       }
-
-       for (i=0; i<5; i++)
-               if (i != j && i != k )
-                       sum += val[i];
-
-       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]);
-
-       return (sum/3);
-}
-
-static int sharpsl_check_battery_temp(void)
-{
-       int val, i, buff[5];
-
-       /* Check battery temperature */
-       for (i=0; i<5; i++) {
-               mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
-               buff[i] = sharpsl_read_temp();
-       }
-
-       val = get_select_val(buff);
-
-       dev_dbg(sharpsl_pm.dev, "Temperature: %d\n", val);
-       if (val > SHARPSL_CHARGE_ON_TEMP)
-               return -1;
-
-       return 0;
-}
-
-static int sharpsl_check_battery_voltage(void)
-{
-       int val, i, buff[5];
-
-       /* disable charge, enable discharge */
-       CHARGE_OFF();
-       DISCHARGE_ON();
-       mdelay(SHARPSL_WAIT_DISCHARGE_ON);
-
-       if (sharpsl_pm.machinfo->discharge1)
-               sharpsl_pm.machinfo->discharge1(1);
-
-       /* Check battery voltage */
-       for (i=0; i<5; i++) {
-               buff[i] = sharpsl_read_main_battery();
-               mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
-       }
-
-       if (sharpsl_pm.machinfo->discharge1)
-               sharpsl_pm.machinfo->discharge1(0);
-
-       DISCHARGE_OFF();
-
-       val = get_select_val(buff);
-       dev_dbg(sharpsl_pm.dev, "Battery Voltage: %d\n", val);
-
-       if (val < SHARPSL_CHARGE_ON_VOLT)
-               return -1;
-
-       return 0;
-}
-
-static int sharpsl_ac_check(void)
-{
-       int temp, i, buff[5];
-
-       for (i=0; i<5; i++) {
-               buff[i] = sharpsl_read_acin();
-               mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN);
-       }
-
-       temp = get_select_val(buff);
-       dev_dbg(sharpsl_pm.dev, "AC Voltage: %d\n",temp);
-
-       if ((temp > SHARPSL_CHARGE_ON_ACIN_HIGH) || (temp < SHARPSL_CHARGE_ON_ACIN_LOW)) {
-               dev_err(sharpsl_pm.dev, "Error: AC check failed.\n");
-               return -1;
-       }
-
-       return 0;
-}
-
-#ifdef CONFIG_PM
-static int sharpsl_pm_suspend(struct platform_device *pdev, pm_message_t state)
-{
-       sharpsl_pm.flags |= SHARPSL_SUSPENDED;
-       flush_scheduled_work();
-
-       if (sharpsl_pm.charge_mode == CHRG_ON)
-               sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
-       else
-               sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
-
-       return 0;
-}
-
-static int sharpsl_pm_resume(struct platform_device *pdev)
-{
-       /* Clear the reset source indicators as they break the bootloader upon reboot */
-       RCSR = 0x0f;
-       sharpsl_average_clear();
-       sharpsl_pm.flags &= ~SHARPSL_APM_QUEUED;
-       sharpsl_pm.flags &= ~SHARPSL_SUSPENDED;
-
-       return 0;
-}
-
-static void corgi_goto_sleep(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
-{
-       dev_dbg(sharpsl_pm.dev, "Time is: %08x\n",RCNR);
-
-       dev_dbg(sharpsl_pm.dev, "Offline Charge Activate = %d\n",sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG);
-       /* not charging and AC-IN! */
-
-       if ((sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG) && (STATUS_AC_IN() != 0)) {
-               dev_dbg(sharpsl_pm.dev, "Activating Offline Charger...\n");
-               sharpsl_pm.charge_mode = CHRG_OFF;
-               sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
-               sharpsl_off_charge_battery();
-       }
-
-       sharpsl_pm.machinfo->presuspend();
-
-       PEDR = 0xffffffff; /* clear it */
-
-       sharpsl_pm.flags &= ~SHARPSL_ALARM_ACTIVE;
-       if ((sharpsl_pm.charge_mode == CHRG_ON) && ((alarm_enable && ((alarm_time - RCNR) > (SHARPSL_BATCHK_TIME_SUSPEND + 30))) || !alarm_enable)) {
-               RTSR &= RTSR_ALE;
-               RTAR = RCNR + SHARPSL_BATCHK_TIME_SUSPEND;
-               dev_dbg(sharpsl_pm.dev, "Charging alarm at: %08x\n",RTAR);
-               sharpsl_pm.flags |= SHARPSL_ALARM_ACTIVE;
-       } else if (alarm_enable) {
-               RTSR &= RTSR_ALE;
-               RTAR = alarm_time;
-               dev_dbg(sharpsl_pm.dev, "User alarm at: %08x\n",RTAR);
-       } else {
-               dev_dbg(sharpsl_pm.dev, "No alarms set.\n");
-       }
-
-       pxa_pm_enter(state);
-
-       sharpsl_pm.machinfo->postsuspend();
-
-       dev_dbg(sharpsl_pm.dev, "Corgi woken up from suspend: %08x\n",PEDR);
-}
-
-static int corgi_enter_suspend(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
-{
-       if (!sharpsl_pm.machinfo->should_wakeup(!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE) && alarm_enable) )
-       {
-               if (!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE)) {
-                       dev_dbg(sharpsl_pm.dev, "No user triggered wakeup events and not charging. Strange. Suspend.\n");
-                       corgi_goto_sleep(alarm_time, alarm_enable, state);
-                       return 1;
-               }
-               if(sharpsl_off_charge_battery()) {
-                       dev_dbg(sharpsl_pm.dev, "Charging. Suspend...\n");
-                       corgi_goto_sleep(alarm_time, alarm_enable, state);
-                       return 1;
-               }
-               dev_dbg(sharpsl_pm.dev, "User triggered wakeup in offline charger.\n");
-       }
-
-       if ((STATUS_BATT_LOCKED() == 0) || (sharpsl_fatal_check() < 0) )
-       {
-               dev_err(sharpsl_pm.dev, "Fatal condition. Suspend.\n");
-               corgi_goto_sleep(alarm_time, alarm_enable, state);
-               return 1;
-       }
-
-       return 0;
-}
-
-static int corgi_pxa_pm_enter(suspend_state_t state)
-{
-       unsigned long alarm_time = RTAR;
-       unsigned int alarm_status = ((RTSR & RTSR_ALE) != 0);
-
-       dev_dbg(sharpsl_pm.dev, "SharpSL suspending for first time.\n");
-
-       corgi_goto_sleep(alarm_time, alarm_status, state);
-
-       while (corgi_enter_suspend(alarm_time,alarm_status,state))
-               {}
-
-       dev_dbg(sharpsl_pm.dev, "SharpSL resuming...\n");
-
-       return 0;
-}
-#endif
-
-
-/*
- * Check for fatal battery errors
- * Fatal returns -1
- */
-static int sharpsl_fatal_check(void)
-{
-       int buff[5], temp, i, acin;
-
-       dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check entered\n");
-
-       /* Check AC-Adapter */
-       acin = STATUS_AC_IN();
-
-       if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) {
-               CHARGE_OFF();
-               udelay(100);
-               DISCHARGE_ON(); /* enable discharge */
-               mdelay(SHARPSL_WAIT_DISCHARGE_ON);
-       }
-
-       if (sharpsl_pm.machinfo->discharge1)
-               sharpsl_pm.machinfo->discharge1(1);
-
-       /* Check battery : check inserting battery ? */
-       for (i=0; i<5; i++) {
-               buff[i] = sharpsl_read_main_battery();
-               mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
-       }
-
-       if (sharpsl_pm.machinfo->discharge1)
-               sharpsl_pm.machinfo->discharge1(0);
-
-       if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) {
-               udelay(100);
-               CHARGE_ON();
-               DISCHARGE_OFF();
-       }
-
-       temp = get_select_val(buff);
-       dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check: acin: %d, discharge voltage: %d, no discharge: %d\n", acin, temp, sharpsl_read_main_battery());
-
-       if ((acin && (temp < SHARPSL_FATAL_ACIN_VOLT)) ||
-                       (!acin && (temp < SHARPSL_FATAL_NOACIN_VOLT)))
-               return -1;
-       return 0;
-}
-
-static int sharpsl_off_charge_error(void)
-{
-       dev_err(sharpsl_pm.dev, "Offline Charger: Error occured.\n");
-       CHARGE_OFF();
-       CHARGE_LED_ERR();
-       sharpsl_pm.charge_mode = CHRG_ERROR;
-       return 1;
-}
-
-/*
- * Charging Control while suspended
- * Return 1 - go straight to sleep
- * Return 0 - sleep or wakeup depending on other factors
- */
-static int sharpsl_off_charge_battery(void)
-{
-       int time;
-
-       dev_dbg(sharpsl_pm.dev, "Charge Mode: %d\n", sharpsl_pm.charge_mode);
-
-       if (sharpsl_pm.charge_mode == CHRG_OFF) {
-               dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 1\n");
-
-               /* AC Check */
-               if ((sharpsl_ac_check() < 0) || (sharpsl_check_battery_temp() < 0))
-                       return sharpsl_off_charge_error();
-
-               /* Start Charging */
-               CHARGE_LED_ON();
-               CHARGE_OFF();
-               mdelay(SHARPSL_CHARGE_WAIT_TIME);
-               CHARGE_ON();
-
-               sharpsl_pm.charge_mode = CHRG_ON;
-               sharpsl_pm.full_count = 0;
-
-               return 1;
-       } else if (sharpsl_pm.charge_mode != CHRG_ON) {
-               return 1;
-       }
-
-       if (sharpsl_pm.full_count == 0) {
-               int time;
-
-               dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 2\n");
-
-               if ((sharpsl_check_battery_temp() < 0) || (sharpsl_check_battery_voltage() < 0))
-                       return sharpsl_off_charge_error();
-
-               CHARGE_OFF();
-               mdelay(SHARPSL_CHARGE_WAIT_TIME);
-               CHARGE_ON();
-               sharpsl_pm.charge_mode = CHRG_ON;
-
-               mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
-
-               time = RCNR;
-               while(1) {
-                       /* Check if any wakeup event had occured */
-                       if (sharpsl_pm.machinfo->charger_wakeup() != 0)
-                               return 0;
-                       /* Check for timeout */
-                       if ((RCNR - time) > SHARPSL_WAIT_CO_TIME)
-                               return 1;
-                       if (STATUS_CHRG_FULL()) {
-                               dev_dbg(sharpsl_pm.dev, "Offline Charger: Charge full occured. Retrying to check\n");
-                               sharpsl_pm.full_count++;
-                               CHARGE_OFF();
-                               mdelay(SHARPSL_CHARGE_WAIT_TIME);
-                               CHARGE_ON();
-                               return 1;
-                       }
-               }
-       }
-
-       dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 3\n");
-
-       mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
-
-       time = RCNR;
-       while(1) {
-               /* Check if any wakeup event had occured */
-               if (sharpsl_pm.machinfo->charger_wakeup() != 0)
-                       return 0;
-               /* Check for timeout */
-               if ((RCNR-time) > SHARPSL_WAIT_CO_TIME) {
-                       if (sharpsl_pm.full_count > SHARPSL_CHARGE_RETRY_CNT) {
-                               dev_dbg(sharpsl_pm.dev, "Offline Charger: Not charged sufficiently. Retrying.\n");
-                               sharpsl_pm.full_count = 0;
-                       }
-                       sharpsl_pm.full_count++;
-                       return 1;
-               }
-               if (STATUS_CHRG_FULL()) {
-                       dev_dbg(sharpsl_pm.dev, "Offline Charger: Charging complete.\n");
-                       CHARGE_LED_OFF();
-                       CHARGE_OFF();
-                       sharpsl_pm.charge_mode = CHRG_DONE;
-                       return 1;
-               }
-       }
-}
-
-
-static ssize_t battery_percentage_show(struct device *dev, struct device_attribute *attr, char *buf)
-{
-       return sprintf(buf, "%d\n",sharpsl_pm.battstat.mainbat_percent);
-}
-
-static ssize_t battery_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
-{
-       return sprintf(buf, "%d\n",sharpsl_pm.battstat.mainbat_voltage);
-}
-
-static DEVICE_ATTR(battery_percentage, 0444, battery_percentage_show, NULL);
-static DEVICE_ATTR(battery_voltage, 0444, battery_voltage_show, NULL);
-
-extern void (*apm_get_power_status)(struct apm_power_info *);
-
-static void sharpsl_apm_get_power_status(struct apm_power_info *info)
-{
-       info->ac_line_status = sharpsl_pm.battstat.ac_status;
-
-       if (sharpsl_pm.charge_mode == CHRG_ON)
-               info->battery_status = APM_BATTERY_STATUS_CHARGING;
-       else
-               info->battery_status = sharpsl_pm.battstat.mainbat_status;
-
-       info->battery_flag = (1 << info->battery_status);
-       info->battery_life = sharpsl_pm.battstat.mainbat_percent;
-}
-
-static struct pm_ops sharpsl_pm_ops = {
-       .pm_disk_mode   = PM_DISK_FIRMWARE,
-       .prepare        = pxa_pm_prepare,
-       .enter          = corgi_pxa_pm_enter,
-       .finish         = pxa_pm_finish,
-};
-
-static int __init sharpsl_pm_probe(struct platform_device *pdev)
-{
-       if (!pdev->dev.platform_data)
-               return -EINVAL;
-
-       sharpsl_pm.dev = &pdev->dev;
-       sharpsl_pm.machinfo = pdev->dev.platform_data;
-       sharpsl_pm.charge_mode = CHRG_OFF;
-       sharpsl_pm.flags = 0;
-
-       sharpsl_pm.machinfo->init();
-
-       init_timer(&sharpsl_pm.ac_timer);
-       sharpsl_pm.ac_timer.function = sharpsl_ac_timer;
-
-       init_timer(&sharpsl_pm.chrg_full_timer);
-       sharpsl_pm.chrg_full_timer.function = sharpsl_chrg_full_timer;
-
        pxa_gpio_mode(sharpsl_pm.machinfo->gpio_acin | GPIO_IN);
        pxa_gpio_mode(sharpsl_pm.machinfo->gpio_batfull | GPIO_IN);
        pxa_gpio_mode(sharpsl_pm.machinfo->gpio_batlock | GPIO_IN);
@@ -938,26 +165,10 @@ static int __init sharpsl_pm_probe(struct platform_device *pdev)
                }
                else set_irq_type(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull),IRQT_RISING);
        }
-
-       device_create_file(&pdev->dev, &dev_attr_battery_percentage);
-       device_create_file(&pdev->dev, &dev_attr_battery_voltage);
-
-       apm_get_power_status = sharpsl_apm_get_power_status;
-
-       pm_set_ops(&sharpsl_pm_ops);
-
-       mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
-
-       return 0;
 }
 
-static int sharpsl_pm_remove(struct platform_device *pdev)
+void sharpsl_pm_pxa_remove(void)
 {
-       pm_set_ops(NULL);
-
-       device_remove_file(&pdev->dev, &dev_attr_battery_percentage);
-       device_remove_file(&pdev->dev, &dev_attr_battery_voltage);
-
        free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_acin), sharpsl_ac_isr);
        free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batlock), sharpsl_fatal_isr);
 
@@ -966,32 +177,4 @@ static int sharpsl_pm_remove(struct platform_device *pdev)
 
        if (!machine_is_corgi())
                free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull), sharpsl_chrg_full_isr);
-
-       del_timer_sync(&sharpsl_pm.chrg_full_timer);
-       del_timer_sync(&sharpsl_pm.ac_timer);
-
-       return 0;
 }
-
-static struct platform_driver sharpsl_pm_driver = {
-       .probe          = sharpsl_pm_probe,
-       .remove         = sharpsl_pm_remove,
-       .suspend        = sharpsl_pm_suspend,
-       .resume         = sharpsl_pm_resume,
-       .driver         = {
-               .name           = "sharpsl-pm",
-       },
-};
-
-static int __devinit sharpsl_pm_init(void)
-{
-       return platform_driver_register(&sharpsl_pm_driver);
-}
-
-static void sharpsl_pm_exit(void)
-{
-       platform_driver_unregister(&sharpsl_pm_driver);
-}
-
-late_initcall(sharpsl_pm_init);
-module_exit(sharpsl_pm_exit);
index 3ce7486daa51b42c73febb736220e24103ce4227..76a5c26dea0b299fc42801a79d1cb66c1ff62cbd 100644 (file)
@@ -21,6 +21,7 @@
 #include <asm/mach-types.h>
 #include <asm/hardware.h>
 #include <asm/hardware/scoop.h>
+#include <asm/hardware/sharpsl_pm.h>
 
 #include <asm/arch/sharpsl.h>
 #include <asm/arch/spitz.h>
@@ -33,19 +34,7 @@ static void spitz_charger_init(void)
 {
        pxa_gpio_mode(SPITZ_GPIO_KEY_INT | GPIO_IN);
        pxa_gpio_mode(SPITZ_GPIO_SYNC | GPIO_IN);
-}
-
-static void spitz_charge_led(int val)
-{
-       if (val == SHARPSL_LED_ERROR) {
-               dev_dbg(sharpsl_pm.dev, "Charge LED Error\n");
-       } else if (val == SHARPSL_LED_ON) {
-               dev_dbg(sharpsl_pm.dev, "Charge LED On\n");
-               set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_LED_ORANGE);
-       } else {
-               dev_dbg(sharpsl_pm.dev, "Charge LED Off\n");
-               reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_LED_ORANGE);
-       }
+       sharpsl_pm_pxa_init();
 }
 
 static void spitz_measure_temp(int on)
@@ -92,7 +81,7 @@ static void spitz_discharge1(int on)
 
 static void spitz_presuspend(void)
 {
-       spitz_last_ac_status = STATUS_AC_IN();
+       spitz_last_ac_status = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
 
        /* GPIO Sleep Register */
        PGSR0 = 0x00144018;
@@ -138,7 +127,7 @@ static void spitz_postsuspend(void)
 static int spitz_should_wakeup(unsigned int resume_on_alarm)
 {
        int is_resume = 0;
-       int acin = STATUS_AC_IN();
+       int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
 
        if (spitz_last_ac_status != acin) {
                if (acin) {
@@ -148,8 +137,8 @@ static int spitz_should_wakeup(unsigned int resume_on_alarm)
                } else {
                        /* charge off */
                        dev_dbg(sharpsl_pm.dev, "AC Removed\n");
-                       CHARGE_LED_OFF();
-                       CHARGE_OFF();
+                       sharpsl_pm_led(SHARPSL_LED_OFF);
+                       sharpsl_pm.machinfo->charge(0);
                        sharpsl_pm.charge_mode = CHRG_OFF;
                }
                spitz_last_ac_status = acin;
@@ -175,25 +164,41 @@ static unsigned long spitz_charger_wakeup(void)
        return (~GPLR0 & GPIO_bit(SPITZ_GPIO_KEY_INT)) | (GPLR0 & GPIO_bit(SPITZ_GPIO_SYNC));
 }
 
-static int spitz_acin_status(void)
+unsigned long spitzpm_read_devdata(int type)
 {
-       return (((~GPLR(SPITZ_GPIO_AC_IN)) & GPIO_bit(SPITZ_GPIO_AC_IN)) != 0);
+       switch(type) {
+       case SHARPSL_STATUS_ACIN:
+               return (((~GPLR(SPITZ_GPIO_AC_IN)) & GPIO_bit(SPITZ_GPIO_AC_IN)) != 0);
+       case SHARPSL_STATUS_LOCK:
+               return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batlock);
+       case SHARPSL_STATUS_CHRGFULL:
+               return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batfull);
+       case SHARPSL_STATUS_FATAL:
+               return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_fatal);
+       case SHARPSL_ACIN_VOLT:
+               return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT);
+       case SHARPSL_BATT_TEMP:
+               return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP);
+       case SHARPSL_BATT_VOLT:
+       default:
+               return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
+       }
 }
 
 struct sharpsl_charger_machinfo spitz_pm_machinfo = {
        .init             = spitz_charger_init,
+       .exit             = sharpsl_pm_pxa_remove,
        .gpio_batlock     = SPITZ_GPIO_BAT_COVER,
        .gpio_acin        = SPITZ_GPIO_AC_IN,
        .gpio_batfull     = SPITZ_GPIO_CHRG_FULL,
        .gpio_fatal       = SPITZ_GPIO_FATAL_BAT,
-       .status_acin      = spitz_acin_status,
        .discharge        = spitz_discharge,
        .discharge1       = spitz_discharge1,
        .charge           = spitz_charge,
-       .chargeled        = spitz_charge_led,
        .measure_temp     = spitz_measure_temp,
        .presuspend       = spitz_presuspend,
        .postsuspend      = spitz_postsuspend,
+       .read_devdata     = spitzpm_read_devdata,
        .charger_wakeup   = spitz_charger_wakeup,
        .should_wakeup    = spitz_should_wakeup,
        .bat_levels       = 40,
index 6a219b2c77e3d10a18200c5e0c2bca0791a5235a..d0aaf450e8c7229d01d6c43c9c580d0939125a29 100644 (file)
@@ -20,6 +20,7 @@
 #include <linux/backlight.h>
 
 #include <asm/arch/sharpsl.h>
+#include <asm/hardware/sharpsl_pm.h>
 
 #define CORGI_DEFAULT_INTENSITY                0x1f
 #define CORGI_LIMIT_MASK               0x0b
diff --git a/include/asm-arm/hardware/sharpsl_pm.h b/include/asm-arm/hardware/sharpsl_pm.h
new file mode 100644 (file)
index 0000000..36983e5
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * SharpSL Battery/PM Driver
+ *
+ * Copyright (c) 2004-2005 Richard Purdie
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#include <linux/interrupt.h>
+
+struct sharpsl_charger_machinfo {
+       void (*init)(void);
+       void (*exit)(void);
+       int gpio_acin;
+       int gpio_batfull;
+       int gpio_batlock;
+       int gpio_fatal;
+       void (*discharge)(int);
+       void (*discharge1)(int);
+       void (*charge)(int);
+       void (*measure_temp)(int);
+       void (*presuspend)(void);
+       void (*postsuspend)(void);
+       unsigned long (*read_devdata)(int);
+#define SHARPSL_BATT_VOLT       1
+#define SHARPSL_BATT_TEMP       2
+#define SHARPSL_ACIN_VOLT       3
+#define SHARPSL_STATUS_ACIN     4
+#define SHARPSL_STATUS_LOCK     5
+#define SHARPSL_STATUS_CHRGFULL 6
+#define SHARPSL_STATUS_FATAL    7
+       unsigned long (*charger_wakeup)(void);
+       int (*should_wakeup)(unsigned int resume_on_alarm);
+       int bat_levels;
+       struct battery_thresh *bat_levels_noac;
+       struct battery_thresh *bat_levels_acin;
+       int status_high_acin;
+       int status_low_acin;
+       int status_high_noac;
+       int status_low_noac;
+};
+
+struct battery_thresh {
+       int voltage;
+       int percentage;
+};
+
+struct battery_stat {
+       int ac_status;         /* APM AC Present/Not Present */
+       int mainbat_status;    /* APM Main Battery Status */
+       int mainbat_percent;   /* Main Battery Percentage Charge */
+       int mainbat_voltage;   /* Main Battery Voltage */
+};
+
+struct sharpsl_pm_status {
+       struct device *dev;
+       struct timer_list ac_timer;
+       struct timer_list chrg_full_timer;
+
+       int charge_mode;
+#define CHRG_ERROR    (-1)
+#define CHRG_OFF      (0)
+#define CHRG_ON       (1)
+#define CHRG_DONE     (2)
+
+       unsigned int flags;
+#define SHARPSL_SUSPENDED       (1 << 0)  /* Device is Suspended */
+#define SHARPSL_ALARM_ACTIVE    (1 << 1)  /* Alarm is for charging event (not user) */
+#define SHARPSL_BL_LIMIT        (1 << 2)  /* Backlight Intensity Limited */
+#define SHARPSL_APM_QUEUED      (1 << 3)  /* APM Event Queued */
+#define SHARPSL_DO_OFFLINE_CHRG (1 << 4)  /* Trigger the offline charger */
+
+       int full_count;
+       unsigned long charge_start_time;
+       struct sharpsl_charger_machinfo *machinfo;
+       struct battery_stat battstat;
+};
+
+extern struct sharpsl_pm_status sharpsl_pm;
+
+
+#define SHARPSL_LED_ERROR  2
+#define SHARPSL_LED_ON     1
+#define SHARPSL_LED_OFF    0
+
+void sharpsl_battery_kick(void);
+void sharpsl_pm_led(int val);
+irqreturn_t sharpsl_ac_isr(int irq, void *dev_id, struct pt_regs *fp);
+irqreturn_t sharpsl_chrg_full_isr(int irq, void *dev_id, struct pt_regs *fp);
+irqreturn_t sharpsl_fatal_isr(int irq, void *dev_id, struct pt_regs *fp);
+