blob: 4e64a140252e9ebbe3dd6de75ac6548cb92af2e9 [file] [log] [blame]
Richard Purdiee8b6f7f2005-11-13 10:07:47 +00001/*
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 Dunlap158f1e92011-06-14 17:06:02 -070017#include <linux/gpio.h>
Haojian Zhuange0bf102e2011-12-08 15:07:18 +080018#include <linux/gpio-pxa.h>
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000019#include <linux/interrupt.h>
20#include <linux/platform_device.h>
Russell King14fca612007-02-27 12:10:07 +000021#include <linux/apm-emulation.h>
22
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000023#include <asm/irq.h>
24#include <asm/mach-types.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/hardware.h>
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000026
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/spitz.h>
Arnd Bergmann4c25c5d2015-01-30 10:45:33 +010028#include "pxa27x.h"
29#include "sharpsl_pm.h"
Eric Miaob6eede12010-01-11 16:17:25 +080030
Eric Miaob6eede12010-01-11 16:17:25 +080031#include "generic.h"
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000032
Richard Purdief8703dc2006-06-19 19:58:52 +010033#define SHARPSL_CHARGE_ON_VOLT 0x99 /* 2.9V */
34#define SHARPSL_CHARGE_ON_TEMP 0xe0 /* 2.9V */
35#define SHARPSL_CHARGE_ON_ACIN_HIGH 0x9b /* 6V */
36#define SHARPSL_CHARGE_ON_ACIN_LOW 0x34 /* 2V */
37#define SHARPSL_FATAL_ACIN_VOLT 182 /* 3.45V */
38#define SHARPSL_FATAL_NOACIN_VOLT 170 /* 3.40V */
39
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000040static int spitz_last_ac_status;
41
Eric Miaob6eede12010-01-11 16:17:25 +080042static struct gpio spitz_charger_gpios[] = {
43 { SPITZ_GPIO_KEY_INT, GPIOF_IN, "Keyboard Interrupt" },
44 { SPITZ_GPIO_SYNC, GPIOF_IN, "Sync" },
Haojian Zhuang9bf448c2011-10-17 13:37:23 +080045 { SPITZ_GPIO_AC_IN, GPIOF_IN, "Charger Detection" },
Eric Miaob6eede12010-01-11 16:17:25 +080046 { SPITZ_GPIO_ADC_TEMP_ON, GPIOF_OUT_INIT_LOW, "ADC Temp On" },
47 { SPITZ_GPIO_JK_B, GPIOF_OUT_INIT_LOW, "JK B" },
48 { SPITZ_GPIO_CHRG_ON, GPIOF_OUT_INIT_LOW, "Charger On" },
49};
50
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000051static void spitz_charger_init(void)
52{
Eric Miaob6eede12010-01-11 16:17:25 +080053 gpio_request_array(ARRAY_AND_SIZE(spitz_charger_gpios));
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000054}
55
56static void spitz_measure_temp(int on)
57{
Eric Miaofff14722008-09-05 22:15:23 +080058 gpio_set_value(SPITZ_GPIO_ADC_TEMP_ON, on);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000059}
60
61static void spitz_charge(int on)
62{
63 if (on) {
64 if (sharpsl_pm.flags & SHARPSL_SUSPENDED) {
Eric Miaofff14722008-09-05 22:15:23 +080065 gpio_set_value(SPITZ_GPIO_JK_B, 1);
66 gpio_set_value(SPITZ_GPIO_CHRG_ON, 0);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000067 } else {
Eric Miaofff14722008-09-05 22:15:23 +080068 gpio_set_value(SPITZ_GPIO_JK_B, 0);
69 gpio_set_value(SPITZ_GPIO_CHRG_ON, 0);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000070 }
71 } else {
Eric Miaofff14722008-09-05 22:15:23 +080072 gpio_set_value(SPITZ_GPIO_JK_B, 0);
73 gpio_set_value(SPITZ_GPIO_CHRG_ON, 1);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000074 }
75}
76
77static void spitz_discharge(int on)
78{
Eric Miaofff14722008-09-05 22:15:23 +080079 gpio_set_value(SPITZ_GPIO_JK_A, on);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000080}
81
82/* HACK - For unknown reasons, accurate voltage readings are only made with a load
83 on the power bus which the green led on spitz provides */
84static void spitz_discharge1(int on)
85{
Eric Miaofff14722008-09-05 22:15:23 +080086 gpio_set_value(SPITZ_GPIO_LED_GREEN, on);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000087}
88
Marko Katic510fcb02012-10-25 18:51:38 +020089static unsigned long gpio18_config = GPIO18_GPIO;
Eric Miaob6eede12010-01-11 16:17:25 +080090
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000091static void spitz_presuspend(void)
92{
Richard Purdieb7557de2006-01-05 20:44:55 +000093 spitz_last_ac_status = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000094
95 /* GPIO Sleep Register */
96 PGSR0 = 0x00144018;
97 PGSR1 = 0x00EF0000;
98 if (machine_is_akita()) {
99 PGSR2 = 0x2121C000;
100 PGSR3 = 0x00600400;
101 } else {
102 PGSR2 = 0x0121C000;
103 PGSR3 = 0x00600000;
104 }
105
106 PGSR0 &= ~SPITZ_GPIO_G0_STROBE_BIT;
107 PGSR1 &= ~SPITZ_GPIO_G1_STROBE_BIT;
108 PGSR2 &= ~SPITZ_GPIO_G2_STROBE_BIT;
109 PGSR3 &= ~SPITZ_GPIO_G3_STROBE_BIT;
110 PGSR2 |= GPIO_bit(SPITZ_GPIO_KEY_STROBE0);
111
Marko Katic510fcb02012-10-25 18:51:38 +0200112 pxa2xx_mfp_config(&gpio18_config, 1);
Eric Miaob6eede12010-01-11 16:17:25 +0800113 gpio_request_one(18, GPIOF_OUT_INIT_HIGH, "Unknown");
114 gpio_free(18);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000115
116 PRER = GPIO_bit(SPITZ_GPIO_KEY_INT);
117 PFER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET);
118 PWER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET) | PWER_RTC;
119 PKWR = GPIO_bit(SPITZ_GPIO_SYNC) | GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET);
Pavel Machekb64b0b72009-10-22 21:35:33 +0200120 PKSR = 0xffffffff; /* clear */
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000121
122 /* nRESET_OUT Disable */
123 PSLR |= PSLR_SL_ROD;
124
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000125 /* Stop 3.6MHz and drive HIGH to PCMCIA and CS */
126 PCFR = PCFR_GPR_EN | PCFR_OPDE;
127}
128
129static void spitz_postsuspend(void)
130{
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000131}
132
133static int spitz_should_wakeup(unsigned int resume_on_alarm)
134{
135 int is_resume = 0;
Richard Purdieb7557de2006-01-05 20:44:55 +0000136 int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000137
138 if (spitz_last_ac_status != acin) {
139 if (acin) {
140 /* charge on */
141 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
142 dev_dbg(sharpsl_pm.dev, "AC Inserted\n");
143 } else {
144 /* charge off */
145 dev_dbg(sharpsl_pm.dev, "AC Removed\n");
Richard Purdieb7557de2006-01-05 20:44:55 +0000146 sharpsl_pm_led(SHARPSL_LED_OFF);
147 sharpsl_pm.machinfo->charge(0);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000148 sharpsl_pm.charge_mode = CHRG_OFF;
149 }
150 spitz_last_ac_status = acin;
151 /* Return to suspend as this must be what we were woken for */
152 return 0;
153 }
154
155 if (PEDR & GPIO_bit(SPITZ_GPIO_KEY_INT))
156 is_resume |= GPIO_bit(SPITZ_GPIO_KEY_INT);
157
158 if (PKSR & GPIO_bit(SPITZ_GPIO_SYNC))
159 is_resume |= GPIO_bit(SPITZ_GPIO_SYNC);
160
161 if (resume_on_alarm && (PEDR & PWER_RTC))
162 is_resume |= PWER_RTC;
163
Pavel Machekb64b0b72009-10-22 21:35:33 +0200164 dev_dbg(sharpsl_pm.dev, "is_resume: %x\n", is_resume);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000165 return is_resume;
166}
167
Robert Jarzmikca264752016-08-02 00:01:32 +0200168static bool spitz_charger_wakeup(void)
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000169{
Robert Jarzmikca264752016-08-02 00:01:32 +0200170 return !gpio_get_value(SPITZ_GPIO_KEY_INT) ||
171 gpio_get_value(SPITZ_GPIO_SYNC);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000172}
173
Richard Purdieb7557de2006-01-05 20:44:55 +0000174unsigned long spitzpm_read_devdata(int type)
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000175{
Pavel Machekb64b0b72009-10-22 21:35:33 +0200176 switch (type) {
Richard Purdieb7557de2006-01-05 20:44:55 +0000177 case SHARPSL_STATUS_ACIN:
Haojian Zhuang9bf448c2011-10-17 13:37:23 +0800178 return !gpio_get_value(SPITZ_GPIO_AC_IN);
Richard Purdieb7557de2006-01-05 20:44:55 +0000179 case SHARPSL_STATUS_LOCK:
Eric Miao05732d7e2010-07-04 23:45:36 +0800180 return gpio_get_value(sharpsl_pm.machinfo->gpio_batlock);
Richard Purdieb7557de2006-01-05 20:44:55 +0000181 case SHARPSL_STATUS_CHRGFULL:
Eric Miao05732d7e2010-07-04 23:45:36 +0800182 return gpio_get_value(sharpsl_pm.machinfo->gpio_batfull);
Richard Purdieb7557de2006-01-05 20:44:55 +0000183 case SHARPSL_STATUS_FATAL:
Eric Miao05732d7e2010-07-04 23:45:36 +0800184 return gpio_get_value(sharpsl_pm.machinfo->gpio_fatal);
Richard Purdieb7557de2006-01-05 20:44:55 +0000185 case SHARPSL_ACIN_VOLT:
186 return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT);
187 case SHARPSL_BATT_TEMP:
188 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP);
189 case SHARPSL_BATT_VOLT:
190 default:
191 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
192 }
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000193}
194
195struct sharpsl_charger_machinfo spitz_pm_machinfo = {
196 .init = spitz_charger_init,
Dmitry Eremin-Solenikovd48898a2009-03-28 18:18:52 +0300197 .exit = NULL,
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000198 .gpio_batlock = SPITZ_GPIO_BAT_COVER,
199 .gpio_acin = SPITZ_GPIO_AC_IN,
200 .gpio_batfull = SPITZ_GPIO_CHRG_FULL,
Richard Purdief8703dc2006-06-19 19:58:52 +0100201 .batfull_irq = 1,
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000202 .gpio_fatal = SPITZ_GPIO_FATAL_BAT,
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000203 .discharge = spitz_discharge,
204 .discharge1 = spitz_discharge1,
205 .charge = spitz_charge,
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000206 .measure_temp = spitz_measure_temp,
207 .presuspend = spitz_presuspend,
208 .postsuspend = spitz_postsuspend,
Richard Purdieb7557de2006-01-05 20:44:55 +0000209 .read_devdata = spitzpm_read_devdata,
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000210 .charger_wakeup = spitz_charger_wakeup,
211 .should_wakeup = spitz_should_wakeup,
Dmitry Baryshkov5cbff962008-10-28 20:26:40 +0300212#if defined(CONFIG_LCD_CORGI)
213 .backlight_limit = corgi_lcd_limit_intensity,
Russell King02a8e762008-04-20 17:15:32 +0100214#endif
Richard Purdief8703dc2006-06-19 19:58:52 +0100215 .charge_on_volt = SHARPSL_CHARGE_ON_VOLT,
216 .charge_on_temp = SHARPSL_CHARGE_ON_TEMP,
217 .charge_acin_high = SHARPSL_CHARGE_ON_ACIN_HIGH,
218 .charge_acin_low = SHARPSL_CHARGE_ON_ACIN_LOW,
219 .fatal_acin_volt = SHARPSL_FATAL_ACIN_VOLT,
220 .fatal_noacin_volt= SHARPSL_FATAL_NOACIN_VOLT,
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000221 .bat_levels = 40,
Pavel Machek0ba01eb2009-10-22 22:16:34 +0200222 .bat_levels_noac = sharpsl_battery_levels_noac,
223 .bat_levels_acin = sharpsl_battery_levels_acin,
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000224 .status_high_acin = 188,
225 .status_low_acin = 178,
226 .status_high_noac = 185,
227 .status_low_noac = 175,
228};
229
230static struct platform_device *spitzpm_device;
231
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800232static int spitzpm_init(void)
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000233{
234 int ret;
235
Dmitry Baryshkov5806d8a2008-05-31 16:17:32 +0100236 if (!machine_is_spitz() && !machine_is_akita()
237 && !machine_is_borzoi())
238 return -ENODEV;
239
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000240 spitzpm_device = platform_device_alloc("sharpsl-pm", -1);
241 if (!spitzpm_device)
242 return -ENOMEM;
243
244 spitzpm_device->dev.platform_data = &spitz_pm_machinfo;
245 ret = platform_device_add(spitzpm_device);
246
247 if (ret)
248 platform_device_put(spitzpm_device);
249
250 return ret;
251}
252
253static void spitzpm_exit(void)
254{
Pavel Machekb64b0b72009-10-22 21:35:33 +0200255 platform_device_unregister(spitzpm_device);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000256}
257
258module_init(spitzpm_init);
259module_exit(spitzpm_exit);