Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Battery and Power Management code for the Sharp SL-Cxx00 |
| 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> |
Randy Dunlap | 158f1e9 | 2011-06-14 17:06:02 -0700 | [diff] [blame^] | 17 | #include <linux/gpio.h> |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +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 | e8b6f7f | 2005-11-13 10:07:47 +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 | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 25 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 26 | #include <mach/spitz.h> |
Eric Miao | b6eede1 | 2010-01-11 16:17:25 +0800 | [diff] [blame] | 27 | #include <mach/pxa27x.h> |
Eric Miao | 04e4ad2 | 2010-07-05 00:03:34 +0800 | [diff] [blame] | 28 | #include <mach/sharpsl_pm.h> |
Eric Miao | b6eede1 | 2010-01-11 16:17:25 +0800 | [diff] [blame] | 29 | |
Eric Miao | b6eede1 | 2010-01-11 16:17:25 +0800 | [diff] [blame] | 30 | #include "generic.h" |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +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 | |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 39 | static int spitz_last_ac_status; |
| 40 | |
Eric Miao | b6eede1 | 2010-01-11 16:17:25 +0800 | [diff] [blame] | 41 | static struct gpio spitz_charger_gpios[] = { |
| 42 | { SPITZ_GPIO_KEY_INT, GPIOF_IN, "Keyboard Interrupt" }, |
| 43 | { SPITZ_GPIO_SYNC, GPIOF_IN, "Sync" }, |
| 44 | { SPITZ_GPIO_ADC_TEMP_ON, GPIOF_OUT_INIT_LOW, "ADC Temp On" }, |
| 45 | { SPITZ_GPIO_JK_B, GPIOF_OUT_INIT_LOW, "JK B" }, |
| 46 | { SPITZ_GPIO_CHRG_ON, GPIOF_OUT_INIT_LOW, "Charger On" }, |
| 47 | }; |
| 48 | |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 49 | static void spitz_charger_init(void) |
| 50 | { |
Eric Miao | b6eede1 | 2010-01-11 16:17:25 +0800 | [diff] [blame] | 51 | gpio_request_array(ARRAY_AND_SIZE(spitz_charger_gpios)); |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static void spitz_measure_temp(int on) |
| 55 | { |
Eric Miao | fff1472 | 2008-09-05 22:15:23 +0800 | [diff] [blame] | 56 | gpio_set_value(SPITZ_GPIO_ADC_TEMP_ON, on); |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static void spitz_charge(int on) |
| 60 | { |
| 61 | if (on) { |
| 62 | if (sharpsl_pm.flags & SHARPSL_SUSPENDED) { |
Eric Miao | fff1472 | 2008-09-05 22:15:23 +0800 | [diff] [blame] | 63 | gpio_set_value(SPITZ_GPIO_JK_B, 1); |
| 64 | gpio_set_value(SPITZ_GPIO_CHRG_ON, 0); |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 65 | } else { |
Eric Miao | fff1472 | 2008-09-05 22:15:23 +0800 | [diff] [blame] | 66 | gpio_set_value(SPITZ_GPIO_JK_B, 0); |
| 67 | gpio_set_value(SPITZ_GPIO_CHRG_ON, 0); |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 68 | } |
| 69 | } else { |
Eric Miao | fff1472 | 2008-09-05 22:15:23 +0800 | [diff] [blame] | 70 | gpio_set_value(SPITZ_GPIO_JK_B, 0); |
| 71 | gpio_set_value(SPITZ_GPIO_CHRG_ON, 1); |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | static void spitz_discharge(int on) |
| 76 | { |
Eric Miao | fff1472 | 2008-09-05 22:15:23 +0800 | [diff] [blame] | 77 | gpio_set_value(SPITZ_GPIO_JK_A, on); |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /* HACK - For unknown reasons, accurate voltage readings are only made with a load |
| 81 | on the power bus which the green led on spitz provides */ |
| 82 | static void spitz_discharge1(int on) |
| 83 | { |
Eric Miao | fff1472 | 2008-09-05 22:15:23 +0800 | [diff] [blame] | 84 | gpio_set_value(SPITZ_GPIO_LED_GREEN, on); |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Eric Miao | b6eede1 | 2010-01-11 16:17:25 +0800 | [diff] [blame] | 87 | static unsigned long gpio18_config[] = { |
| 88 | GPIO18_RDY, |
| 89 | GPIO18_GPIO, |
| 90 | }; |
| 91 | |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 92 | static void spitz_presuspend(void) |
| 93 | { |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 94 | spitz_last_ac_status = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN); |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 95 | |
| 96 | /* GPIO Sleep Register */ |
| 97 | PGSR0 = 0x00144018; |
| 98 | PGSR1 = 0x00EF0000; |
| 99 | if (machine_is_akita()) { |
| 100 | PGSR2 = 0x2121C000; |
| 101 | PGSR3 = 0x00600400; |
| 102 | } else { |
| 103 | PGSR2 = 0x0121C000; |
| 104 | PGSR3 = 0x00600000; |
| 105 | } |
| 106 | |
| 107 | PGSR0 &= ~SPITZ_GPIO_G0_STROBE_BIT; |
| 108 | PGSR1 &= ~SPITZ_GPIO_G1_STROBE_BIT; |
| 109 | PGSR2 &= ~SPITZ_GPIO_G2_STROBE_BIT; |
| 110 | PGSR3 &= ~SPITZ_GPIO_G3_STROBE_BIT; |
| 111 | PGSR2 |= GPIO_bit(SPITZ_GPIO_KEY_STROBE0); |
| 112 | |
Eric Miao | b6eede1 | 2010-01-11 16:17:25 +0800 | [diff] [blame] | 113 | pxa2xx_mfp_config(&gpio18_config[0], 1); |
| 114 | gpio_request_one(18, GPIOF_OUT_INIT_HIGH, "Unknown"); |
| 115 | gpio_free(18); |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 116 | |
| 117 | PRER = GPIO_bit(SPITZ_GPIO_KEY_INT); |
| 118 | PFER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET); |
| 119 | PWER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET) | PWER_RTC; |
| 120 | PKWR = GPIO_bit(SPITZ_GPIO_SYNC) | GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET); |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 121 | PKSR = 0xffffffff; /* clear */ |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 122 | |
| 123 | /* nRESET_OUT Disable */ |
| 124 | PSLR |= PSLR_SL_ROD; |
| 125 | |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 126 | /* Stop 3.6MHz and drive HIGH to PCMCIA and CS */ |
| 127 | PCFR = PCFR_GPR_EN | PCFR_OPDE; |
| 128 | } |
| 129 | |
| 130 | static void spitz_postsuspend(void) |
| 131 | { |
Eric Miao | b6eede1 | 2010-01-11 16:17:25 +0800 | [diff] [blame] | 132 | pxa2xx_mfp_config(&gpio18_config[1], 1); |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static int spitz_should_wakeup(unsigned int resume_on_alarm) |
| 136 | { |
| 137 | int is_resume = 0; |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 138 | int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN); |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 139 | |
| 140 | if (spitz_last_ac_status != acin) { |
| 141 | if (acin) { |
| 142 | /* charge on */ |
| 143 | sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG; |
| 144 | dev_dbg(sharpsl_pm.dev, "AC Inserted\n"); |
| 145 | } else { |
| 146 | /* charge off */ |
| 147 | dev_dbg(sharpsl_pm.dev, "AC Removed\n"); |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 148 | sharpsl_pm_led(SHARPSL_LED_OFF); |
| 149 | sharpsl_pm.machinfo->charge(0); |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 150 | sharpsl_pm.charge_mode = CHRG_OFF; |
| 151 | } |
| 152 | spitz_last_ac_status = acin; |
| 153 | /* Return to suspend as this must be what we were woken for */ |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | if (PEDR & GPIO_bit(SPITZ_GPIO_KEY_INT)) |
| 158 | is_resume |= GPIO_bit(SPITZ_GPIO_KEY_INT); |
| 159 | |
| 160 | if (PKSR & GPIO_bit(SPITZ_GPIO_SYNC)) |
| 161 | is_resume |= GPIO_bit(SPITZ_GPIO_SYNC); |
| 162 | |
| 163 | if (resume_on_alarm && (PEDR & PWER_RTC)) |
| 164 | is_resume |= PWER_RTC; |
| 165 | |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 166 | dev_dbg(sharpsl_pm.dev, "is_resume: %x\n", is_resume); |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 167 | return is_resume; |
| 168 | } |
| 169 | |
| 170 | static unsigned long spitz_charger_wakeup(void) |
| 171 | { |
| 172 | return (~GPLR0 & GPIO_bit(SPITZ_GPIO_KEY_INT)) | (GPLR0 & GPIO_bit(SPITZ_GPIO_SYNC)); |
| 173 | } |
| 174 | |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 175 | unsigned long spitzpm_read_devdata(int type) |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 176 | { |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 177 | switch (type) { |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 178 | case SHARPSL_STATUS_ACIN: |
| 179 | return (((~GPLR(SPITZ_GPIO_AC_IN)) & GPIO_bit(SPITZ_GPIO_AC_IN)) != 0); |
| 180 | case SHARPSL_STATUS_LOCK: |
Eric Miao | 05732d7e | 2010-07-04 23:45:36 +0800 | [diff] [blame] | 181 | return gpio_get_value(sharpsl_pm.machinfo->gpio_batlock); |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 182 | case SHARPSL_STATUS_CHRGFULL: |
Eric Miao | 05732d7e | 2010-07-04 23:45:36 +0800 | [diff] [blame] | 183 | return gpio_get_value(sharpsl_pm.machinfo->gpio_batfull); |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 184 | case SHARPSL_STATUS_FATAL: |
Eric Miao | 05732d7e | 2010-07-04 23:45:36 +0800 | [diff] [blame] | 185 | return gpio_get_value(sharpsl_pm.machinfo->gpio_fatal); |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 186 | case SHARPSL_ACIN_VOLT: |
| 187 | return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT); |
| 188 | case SHARPSL_BATT_TEMP: |
| 189 | return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP); |
| 190 | case SHARPSL_BATT_VOLT: |
| 191 | default: |
| 192 | return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT); |
| 193 | } |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | struct sharpsl_charger_machinfo spitz_pm_machinfo = { |
| 197 | .init = spitz_charger_init, |
Dmitry Eremin-Solenikov | d48898a | 2009-03-28 18:18:52 +0300 | [diff] [blame] | 198 | .exit = NULL, |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 199 | .gpio_batlock = SPITZ_GPIO_BAT_COVER, |
| 200 | .gpio_acin = SPITZ_GPIO_AC_IN, |
| 201 | .gpio_batfull = SPITZ_GPIO_CHRG_FULL, |
Richard Purdie | f8703dc | 2006-06-19 19:58:52 +0100 | [diff] [blame] | 202 | .batfull_irq = 1, |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 203 | .gpio_fatal = SPITZ_GPIO_FATAL_BAT, |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 204 | .discharge = spitz_discharge, |
| 205 | .discharge1 = spitz_discharge1, |
| 206 | .charge = spitz_charge, |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 207 | .measure_temp = spitz_measure_temp, |
| 208 | .presuspend = spitz_presuspend, |
| 209 | .postsuspend = spitz_postsuspend, |
Richard Purdie | b7557de | 2006-01-05 20:44:55 +0000 | [diff] [blame] | 210 | .read_devdata = spitzpm_read_devdata, |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 211 | .charger_wakeup = spitz_charger_wakeup, |
| 212 | .should_wakeup = spitz_should_wakeup, |
Dmitry Baryshkov | 5cbff96 | 2008-10-28 20:26:40 +0300 | [diff] [blame] | 213 | #if defined(CONFIG_LCD_CORGI) |
| 214 | .backlight_limit = corgi_lcd_limit_intensity, |
Russell King | 02a8e76 | 2008-04-20 17:15:32 +0100 | [diff] [blame] | 215 | #endif |
Richard Purdie | f8703dc | 2006-06-19 19:58:52 +0100 | [diff] [blame] | 216 | .charge_on_volt = SHARPSL_CHARGE_ON_VOLT, |
| 217 | .charge_on_temp = SHARPSL_CHARGE_ON_TEMP, |
| 218 | .charge_acin_high = SHARPSL_CHARGE_ON_ACIN_HIGH, |
| 219 | .charge_acin_low = SHARPSL_CHARGE_ON_ACIN_LOW, |
| 220 | .fatal_acin_volt = SHARPSL_FATAL_ACIN_VOLT, |
| 221 | .fatal_noacin_volt= SHARPSL_FATAL_NOACIN_VOLT, |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 222 | .bat_levels = 40, |
Pavel Machek | 0ba01eb | 2009-10-22 22:16:34 +0200 | [diff] [blame] | 223 | .bat_levels_noac = sharpsl_battery_levels_noac, |
| 224 | .bat_levels_acin = sharpsl_battery_levels_acin, |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 225 | .status_high_acin = 188, |
| 226 | .status_low_acin = 178, |
| 227 | .status_high_noac = 185, |
| 228 | .status_low_noac = 175, |
| 229 | }; |
| 230 | |
| 231 | static struct platform_device *spitzpm_device; |
| 232 | |
| 233 | static int __devinit spitzpm_init(void) |
| 234 | { |
| 235 | int ret; |
| 236 | |
Dmitry Baryshkov | 5806d8a | 2008-05-31 16:17:32 +0100 | [diff] [blame] | 237 | if (!machine_is_spitz() && !machine_is_akita() |
| 238 | && !machine_is_borzoi()) |
| 239 | return -ENODEV; |
| 240 | |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 241 | spitzpm_device = platform_device_alloc("sharpsl-pm", -1); |
| 242 | if (!spitzpm_device) |
| 243 | return -ENOMEM; |
| 244 | |
| 245 | spitzpm_device->dev.platform_data = &spitz_pm_machinfo; |
| 246 | ret = platform_device_add(spitzpm_device); |
| 247 | |
| 248 | if (ret) |
| 249 | platform_device_put(spitzpm_device); |
| 250 | |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | static void spitzpm_exit(void) |
| 255 | { |
Pavel Machek | b64b0b7 | 2009-10-22 21:35:33 +0200 | [diff] [blame] | 256 | platform_device_unregister(spitzpm_device); |
Richard Purdie | e8b6f7f | 2005-11-13 10:07:47 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | module_init(spitzpm_init); |
| 260 | module_exit(spitzpm_exit); |