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