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