xref: /netbsd-src/sys/arch/hpcarm/dev/j720pwr.c (revision eb961d0e02b7a46a9acfa877b02df48df6637278)
1 /*	$NetBSD: j720pwr.c,v 1.1 2006/03/04 14:09:36 peter Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002, 2006 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Emmanuel Dreyfus and Peter Postma.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /* Jornada 720 power management. */
40 
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: j720pwr.c,v 1.1 2006/03/04 14:09:36 peter Exp $");
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/proc.h>
48 #include <sys/device.h>
49 
50 #include <dev/apm/apmbios.h>
51 
52 #include <machine/config_hook.h>
53 #include <machine/platid.h>
54 #include <machine/platid_mask.h>
55 
56 #include <arm/sa11x0/sa11x0_var.h>
57 #include <arm/sa11x0/sa11x0_gpioreg.h>
58 #include <arm/sa11x0/sa11x0_ppcreg.h>
59 #include <arm/sa11x0/sa11x0_sspreg.h>
60 
61 #include <hpcarm/dev/j720sspvar.h>
62 
63 #ifdef DEBUG
64 #define DPRINTF(arg)	printf arg
65 #else
66 #define DPRINTF(arg)	/* nothing */
67 #endif
68 
69 #define arraysize(ary)	(sizeof(ary) / sizeof(ary[0]))
70 
71 struct j720pwr_softc {
72 	struct device		sc_dev;
73 
74 	struct j720ssp_softc	*sc_ssp;
75 };
76 
77 static int	j720pwr_match(struct device *, struct cfdata *, void *);
78 static void	j720pwr_attach(struct device *, struct device *, void *);
79 
80 static int	j720pwr_apm_getpower_hook(void *, int, long, void *);
81 static int	j720pwr_get_battery(struct j720pwr_softc *);
82 static int	j720pwr_get_ac_status(struct j720pwr_softc *);
83 static int	j720pwr_get_charge_status(struct j720pwr_softc *);
84 
85 static const struct {
86 	int	percent;
87 	int	value;
88 	int	state;
89 } battery_table[] = {
90 	{ 100,	670,	APM_BATT_FLAG_HIGH	},
91 	{  90,	660,	APM_BATT_FLAG_HIGH	},
92 	{  80,	650,	APM_BATT_FLAG_HIGH	},
93 	{  70,	640,	APM_BATT_FLAG_HIGH	},
94 	{  60,	630,	APM_BATT_FLAG_HIGH	},
95 	{  50,	620,	APM_BATT_FLAG_HIGH	},
96 	{  40,	605,	APM_BATT_FLAG_LOW	},
97 	{  30,	580,	APM_BATT_FLAG_LOW	},
98 	{  20,	545,	APM_BATT_FLAG_LOW	},
99 	{  10,	500,	APM_BATT_FLAG_CRITICAL	},
100 	{   0,  430,	APM_BATT_FLAG_CRITICAL	},
101 };
102 
103 CFATTACH_DECL(j720pwr, sizeof(struct j720pwr_softc),
104     j720pwr_match, j720pwr_attach, NULL, NULL);
105 
106 
107 static int
108 j720pwr_match(struct device *parent, struct cfdata *cf, void *aux)
109 {
110 
111 	if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_7XX))
112 		return 0;
113 	if (strcmp(cf->cf_name, "j720pwr") != 0)
114 		return 0;
115 
116 	return 1;
117 }
118 
119 static void
120 j720pwr_attach(struct device *parent, struct device *self, void *aux)
121 {
122 	struct j720pwr_softc *sc = (struct j720pwr_softc *)self;
123 
124 	printf("\n");
125 
126 	sc->sc_ssp = (struct j720ssp_softc *)parent;
127 
128 	/* Battery status query hook. */
129 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BATTERYVAL,
130 		    CONFIG_HOOK_EXCLUSIVE, j720pwr_apm_getpower_hook, sc);
131 
132 	/* Battery charge status query hook. */
133 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CHARGE,
134 		    CONFIG_HOOK_EXCLUSIVE, j720pwr_apm_getpower_hook, sc);
135 
136 	/* AC status query hook. */
137 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_ACADAPTER,
138 		    CONFIG_HOOK_EXCLUSIVE, j720pwr_apm_getpower_hook, sc);
139 
140 	/* Attach hpcapm. */
141 	config_found_ia(self, "hpcapmif", NULL, NULL);
142 }
143 
144 static int
145 j720pwr_apm_getpower_hook(void *ctx, int type, long id, void *msg)
146 {
147 	int * const pval = msg;
148 	int val, tmp, i, state = 0;
149 
150 	if (type != CONFIG_HOOK_GET)
151 		return EINVAL;
152 
153 	switch (id) {
154 	case CONFIG_HOOK_BATTERYVAL:
155 		val = j720pwr_get_battery(ctx);
156 
157 		if (val != -1) {
158 			for (i = 0; i < arraysize(battery_table); i++)
159 				if (val > battery_table[i].value)
160 					break;
161 			if (i == 0) {
162 				/* Battery charge status is at maximum. */
163 				*pval = 100;
164 			} else {
165 				/*
166 				 * Use linear interpolation to calculate
167 				 * the estimated charge status.
168 				 */
169 				tmp = ((val - battery_table[i].value) * 100) /
170 				    (battery_table[i - 1].value -
171 				     battery_table[i].value);
172 				*pval = battery_table[i].percent +
173 				    ((battery_table[i - 1].percent -
174 				      battery_table[i].percent) * tmp) / 100;
175 			}
176 		} else {
177 			/* Battery is absent. */
178 			*pval = 0;
179 		}
180 
181 		return 0;
182 
183 	case CONFIG_HOOK_CHARGE:
184 		val = j720pwr_get_battery(ctx);
185 
186 		if (val != -1) {
187 			for (i = 1; i < arraysize(battery_table); i++) {
188 				if (val > battery_table[i].value) {
189 					state = battery_table[i - 1].state;
190 					break;
191 				}
192 			}
193 
194 			if (j720pwr_get_charge_status(ctx) == 0)
195 				state |= APM_BATT_FLAG_CHARGING;
196 		} else {
197 			state = APM_BATT_FLAG_NO_SYSTEM_BATTERY;
198 		}
199 
200 		*pval = state;
201 		return 0;
202 
203 	case CONFIG_HOOK_ACADAPTER:
204 		*pval = j720pwr_get_ac_status(ctx) ? APM_AC_OFF : APM_AC_ON;
205 
206 		return 0;
207 	}
208 
209 	return EINVAL;
210 }
211 
212 static int
213 j720pwr_get_battery(struct j720pwr_softc *sc)
214 {
215 	struct j720ssp_softc *ssp = sc->sc_ssp;
216 	int data, i, pmdata[3];
217 
218 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PCR, 0x2000000);
219 
220 	if (j720ssp_readwrite(ssp, 1, 0x300, &data, 500) < 0 || data != 0x88) {
221 		DPRINTF(("j720pwr_get_battery: no dummy received\n"));
222 		goto out;
223 	}
224 
225 	for (i = 0; i < 3; i++) {
226 		if (j720ssp_readwrite(ssp, 0, 0x8800, &pmdata[i], 100) < 0)
227 			goto out;
228 		J720SSP_INVERT(pmdata[i]);
229 	}
230 
231 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
232 
233 	pmdata[0] |= (pmdata[2] & 0x3) << 8;	/* Main battery. */
234 	pmdata[1] |= (pmdata[2] & 0xc) << 6;	/* Backup battery (unused). */
235 
236 	DPRINTF(("j720pwr_get_battery: data[0]=%d data[1]=%d data[2]=%d\n",
237 	    pmdata[0], pmdata[1], pmdata[2]));
238 
239 	/* If bit 0 and 1 are both set, the main battery is absent. */
240 	if ((pmdata[2] & 3) == 3)
241 		return -1;
242 
243 	return pmdata[0];
244 
245 out:
246 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
247 
248 	/* reset SSP */
249 	bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x307);
250 	delay(100);
251 	bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x387);
252 
253 	DPRINTF(("j720pwr_get_battery: error %x\n", data));
254 	return 0;
255 }
256 
257 static int
258 j720pwr_get_ac_status(struct j720pwr_softc *sc)
259 {
260 	struct j720ssp_softc *ssp = sc->sc_ssp;
261 	uint32_t status;
262 
263 	status = bus_space_read_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PLR);
264 
265 	return status & (1 << 4);
266 }
267 
268 static int
269 j720pwr_get_charge_status(struct j720pwr_softc *sc)
270 {
271 	struct j720ssp_softc *ssp = sc->sc_ssp;
272 	uint32_t status;
273 
274 	status = bus_space_read_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PLR);
275 
276 	return status & (1 << 26);
277 }
278