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