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