Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Battery and Power Management code for the Sharp SL-C7xx |
| 3 | * |
| 4 | * Copyright (c) 2005 Richard Purdie |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/stat.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/delay.h> |
Eric Miao | e63f591 | 2010-01-08 13:32:46 +0800 | [diff] [blame] | 17 | #include <linux/gpio.h> |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/platform_device.h> |
Russell King | 14fca61 | 2007-02-27 12:10:07 +0000 | [diff] [blame] | 20 | #include <linux/apm-emulation.h> |
| 21 | |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 22 | #include <asm/irq.h> |
| 23 | #include <asm/mach-types.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 24 | #include <mach/hardware.h> |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 25 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 26 | #include <mach/corgi.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 27 | #include <mach/pxa2xx-regs.h> |
Eric Miao | 04e4ad2 | 2010-07-05 00:03:34 +0800 | [diff] [blame] | 28 | #include <mach/sharpsl_pm.h> |
Eric Miao | e63f591 | 2010-01-08 13:32:46 +0800 | [diff] [blame] | 29 | |
| 30 | #include "generic.h" |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 31 | |
Richard Purdie | f8703dc | 2006-06-19 19:58:52 +0100 | [diff] [blame] | 32 | #define SHARPSL_CHARGE_ON_VOLT 0x99 /* 2.9V */ |
| 33 | #define SHARPSL_CHARGE_ON_TEMP 0xe0 /* 2.9V */ |
| 34 | #define SHARPSL_CHARGE_ON_ACIN_HIGH 0x9b /* 6V */ |
| 35 | #define SHARPSL_CHARGE_ON_ACIN_LOW 0x34 /* 2V */ |
| 36 | #define SHARPSL_FATAL_ACIN_VOLT 182 /* 3.45V */ |
| 37 | #define SHARPSL_FATAL_NOACIN_VOLT 170 /* 3.40V */ |
| 38 | |
Eric Miao | e63f591 | 2010-01-08 13:32:46 +0800 | [diff] [blame] | 39 | static struct gpio charger_gpios[] = { |
| 40 | { CORGI_GPIO_ADC_TEMP_ON, GPIOF_OUT_INIT_LOW, "ADC Temp On" }, |
| 41 | { CORGI_GPIO_CHRG_ON, GPIOF_OUT_INIT_LOW, "Charger On" }, |
| 42 | { CORGI_GPIO_CHRG_UKN, GPIOF_OUT_INIT_LOW, "Charger Unknown" }, |
| 43 | { CORGI_GPIO_KEY_INT, GPIOF_IN, "Key Interrupt" }, |
| 44 | }; |
| 45 | |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 46 | static void corgi_charger_init(void) |
| 47 | { |
Eric Miao | e63f591 | 2010-01-08 13:32:46 +0800 | [diff] [blame] | 48 | gpio_request_array(ARRAY_AND_SIZE(charger_gpios)); |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static void corgi_measure_temp(int on) |
| 52 | { |
Eric Miao | e63f591 | 2010-01-08 13:32:46 +0800 | [diff] [blame] | 53 | gpio_set_value(CORGI_GPIO_ADC_TEMP_ON, on); |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static void corgi_charge(int on) |
| 57 | { |
| 58 | if (on) { |
| 59 | if (machine_is_corgi() && (sharpsl_pm.flags & SHARPSL_SUSPENDED)) { |
Eric Miao | e63f591 | 2010-01-08 13:32:46 +0800 | [diff] [blame] | 60 | gpio_set_value(CORGI_GPIO_CHRG_ON, 0); |
| 61 | gpio_set_value(CORGI_GPIO_CHRG_UKN, 1); |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 62 | } else { |
Eric Miao | e63f591 | 2010-01-08 13:32:46 +0800 | [diff] [blame] | 63 | gpio_set_value(CORGI_GPIO_CHRG_ON, 1); |
| 64 | gpio_set_value(CORGI_GPIO_CHRG_UKN, 0); |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 65 | } |
| 66 | } else { |
Eric Miao | e63f591 | 2010-01-08 13:32:46 +0800 | [diff] [blame] | 67 | gpio_set_value(CORGI_GPIO_CHRG_ON, 0); |
| 68 | gpio_set_value(CORGI_GPIO_CHRG_UKN, 0); |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | static void corgi_discharge(int on) |
| 73 | { |
Eric Miao | e63f591 | 2010-01-08 13:32:46 +0800 | [diff] [blame] | 74 | gpio_set_value(CORGI_GPIO_DISCHARGE_ON, on); |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void corgi_presuspend(void) |
| 78 | { |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static void corgi_postsuspend(void) |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Check what brought us out of the suspend. |
| 87 | * Return: 0 to sleep, otherwise wake |
| 88 | */ |
| 89 | static int corgi_should_wakeup(unsigned int resume_on_alarm) |
| 90 | { |
| 91 | int is_resume = 0; |
| 92 | |
| 93 | dev_dbg(sharpsl_pm.dev, "GPLR0 = %x,%x\n", GPLR0, PEDR); |
| 94 | |
| 95 | if ((PEDR & GPIO_bit(CORGI_GPIO_AC_IN))) { |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 96 | if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) { |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 97 | /* charge on */ |
| 98 | dev_dbg(sharpsl_pm.dev, "ac insert\n"); |
| 99 | sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG; |
| 100 | } else { |
| 101 | /* charge off */ |
| 102 | dev_dbg(sharpsl_pm.dev, "ac remove\n"); |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 103 | sharpsl_pm_led(SHARPSL_LED_OFF); |
| 104 | sharpsl_pm.machinfo->charge(0); |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 105 | sharpsl_pm.charge_mode = CHRG_OFF; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if ((PEDR & GPIO_bit(CORGI_GPIO_CHRG_FULL))) |
| 110 | dev_dbg(sharpsl_pm.dev, "Charge full interrupt\n"); |
| 111 | |
| 112 | if (PEDR & GPIO_bit(CORGI_GPIO_KEY_INT)) |
| 113 | is_resume |= GPIO_bit(CORGI_GPIO_KEY_INT); |
| 114 | |
| 115 | if (PEDR & GPIO_bit(CORGI_GPIO_WAKEUP)) |
| 116 | is_resume |= GPIO_bit(CORGI_GPIO_WAKEUP); |
| 117 | |
| 118 | if (resume_on_alarm && (PEDR & PWER_RTC)) |
| 119 | is_resume |= PWER_RTC; |
| 120 | |
| 121 | dev_dbg(sharpsl_pm.dev, "is_resume: %x\n",is_resume); |
| 122 | return is_resume; |
| 123 | } |
| 124 | |
| 125 | static unsigned long corgi_charger_wakeup(void) |
| 126 | { |
| 127 | return ~GPLR0 & ( GPIO_bit(CORGI_GPIO_AC_IN) | GPIO_bit(CORGI_GPIO_KEY_INT) | GPIO_bit(CORGI_GPIO_WAKEUP) ); |
| 128 | } |
| 129 | |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 130 | unsigned long corgipm_read_devdata(int type) |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 131 | { |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 132 | switch(type) { |
| 133 | case SHARPSL_STATUS_ACIN: |
| 134 | return ((GPLR(CORGI_GPIO_AC_IN) & GPIO_bit(CORGI_GPIO_AC_IN)) != 0); |
| 135 | case SHARPSL_STATUS_LOCK: |
Eric Miao | 05732d7e | 2010-07-04 23:45:36 +0800 | [diff] [blame] | 136 | return gpio_get_value(sharpsl_pm.machinfo->gpio_batlock); |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 137 | case SHARPSL_STATUS_CHRGFULL: |
Eric Miao | 05732d7e | 2010-07-04 23:45:36 +0800 | [diff] [blame] | 138 | return gpio_get_value(sharpsl_pm.machinfo->gpio_batfull); |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 139 | case SHARPSL_STATUS_FATAL: |
Eric Miao | 05732d7e | 2010-07-04 23:45:36 +0800 | [diff] [blame] | 140 | return gpio_get_value(sharpsl_pm.machinfo->gpio_fatal); |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 141 | case SHARPSL_ACIN_VOLT: |
| 142 | return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT); |
| 143 | case SHARPSL_BATT_TEMP: |
| 144 | return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP); |
| 145 | case SHARPSL_BATT_VOLT: |
| 146 | default: |
| 147 | return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT); |
| 148 | } |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static struct sharpsl_charger_machinfo corgi_pm_machinfo = { |
| 152 | .init = corgi_charger_init, |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 153 | .exit = NULL, |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 154 | .gpio_batlock = CORGI_GPIO_BAT_COVER, |
| 155 | .gpio_acin = CORGI_GPIO_AC_IN, |
| 156 | .gpio_batfull = CORGI_GPIO_CHRG_FULL, |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 157 | .discharge = corgi_discharge, |
| 158 | .charge = corgi_charge, |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 159 | .measure_temp = corgi_measure_temp, |
| 160 | .presuspend = corgi_presuspend, |
| 161 | .postsuspend = corgi_postsuspend, |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 162 | .read_devdata = corgipm_read_devdata, |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 163 | .charger_wakeup = corgi_charger_wakeup, |
| 164 | .should_wakeup = corgi_should_wakeup, |
Dmitry Baryshkov | 5cbff96 | 2008-10-28 20:26:40 +0300 | [diff] [blame] | 165 | #if defined(CONFIG_LCD_CORGI) |
| 166 | .backlight_limit = corgi_lcd_limit_intensity, |
Russell King | 02a8e76 | 2008-04-20 17:15:32 +0100 | [diff] [blame] | 167 | #endif |
Richard Purdie | f8703dc | 2006-06-19 19:58:52 +0100 | [diff] [blame] | 168 | .charge_on_volt = SHARPSL_CHARGE_ON_VOLT, |
| 169 | .charge_on_temp = SHARPSL_CHARGE_ON_TEMP, |
| 170 | .charge_acin_high = SHARPSL_CHARGE_ON_ACIN_HIGH, |
| 171 | .charge_acin_low = SHARPSL_CHARGE_ON_ACIN_LOW, |
| 172 | .fatal_acin_volt = SHARPSL_FATAL_ACIN_VOLT, |
| 173 | .fatal_noacin_volt= SHARPSL_FATAL_NOACIN_VOLT, |
| 174 | .bat_levels = 40, |
Pavel Machek | 0ba01eb | 2009-10-22 22:16:34 +0200 | [diff] [blame] | 175 | .bat_levels_noac = sharpsl_battery_levels_noac, |
| 176 | .bat_levels_acin = sharpsl_battery_levels_acin, |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 177 | .status_high_acin = 188, |
| 178 | .status_low_acin = 178, |
| 179 | .status_high_noac = 185, |
| 180 | .status_low_noac = 175, |
| 181 | }; |
| 182 | |
| 183 | static struct platform_device *corgipm_device; |
| 184 | |
| 185 | static int __devinit corgipm_init(void) |
| 186 | { |
| 187 | int ret; |
| 188 | |
Dmitry Baryshkov | 043fbc0 | 2008-05-31 16:16:54 +0100 | [diff] [blame] | 189 | if (!machine_is_corgi() && !machine_is_shepherd() |
| 190 | && !machine_is_husky()) |
| 191 | return -ENODEV; |
| 192 | |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 193 | corgipm_device = platform_device_alloc("sharpsl-pm", -1); |
| 194 | if (!corgipm_device) |
| 195 | return -ENOMEM; |
| 196 | |
Richard Purdie | f8703dc | 2006-06-19 19:58:52 +0100 | [diff] [blame] | 197 | if (!machine_is_corgi()) |
| 198 | corgi_pm_machinfo.batfull_irq = 1; |
| 199 | |
Richard Purdie | d72f25b | 2005-11-13 10:07:46 +0000 | [diff] [blame] | 200 | corgipm_device->dev.platform_data = &corgi_pm_machinfo; |
| 201 | ret = platform_device_add(corgipm_device); |
| 202 | |
| 203 | if (ret) |
| 204 | platform_device_put(corgipm_device); |
| 205 | |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | static void corgipm_exit(void) |
| 210 | { |
| 211 | platform_device_unregister(corgipm_device); |
| 212 | } |
| 213 | |
| 214 | module_init(corgipm_init); |
| 215 | module_exit(corgipm_exit); |