battery.c revision 1.6.2.2 1 /* $NetBSD: battery.c,v 1.6.2.2 2007/12/27 00:43:13 mjf Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Michael Lorenz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The NetBSD Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: battery.c,v 1.6.2.2 2007/12/27 00:43:13 mjf Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/proc.h>
40
41 #include <dev/sysmon/sysmonvar.h>
42 #include <dev/sysmon/sysmon_taskq.h>
43
44 #include <macppc/dev/pmuvar.h>
45 #include <macppc/dev/batteryvar.h>
46 #include <machine/bus.h>
47 #include <machine/pio.h>
48 #include "opt_battery.h"
49
50 #ifdef BATTERY_DEBUG
51 #define DPRINTF printf
52 #else
53 #define DPRINTF while (0) printf
54 #endif
55
56 #define BTYPE_COMET 1
57 #define BTYPE_HOOPER 2
58
59 #define BAT_CPU_TEMPERATURE 0
60 #define BAT_AC_PRESENT 1
61 #define BAT_PRESENT 2
62 #define BAT_VOLTAGE 3
63 #define BAT_CURRENT 4
64 #define BAT_MAX_CHARGE 5
65 #define BAT_CHARGE 6
66 #define BAT_CHARGING 7
67 #define BAT_FULL 8
68 #define BAT_TEMPERATURE 9
69 #define BAT_NSENSORS 10 /* number of sensors */
70
71 struct battery_softc {
72 struct device sc_dev;
73 struct pmu_ops *sc_pmu_ops;
74 int sc_type;
75
76 /* envsys stuff */
77 struct sysmon_envsys *sc_sme;
78 envsys_data_t sc_sensor[BAT_NSENSORS];
79 struct sysmon_pswitch sc_sm_acpower;
80
81 /* battery status */
82 int sc_flags;
83 int sc_oflags;
84 int sc_voltage;
85 int sc_charge;
86 int sc_current;
87 int sc_time;
88 int sc_cpu_temp;
89 int sc_bat_temp;
90 int sc_vmax_charged;
91 int sc_vmax_charging;
92 uint32_t sc_timestamp;
93 };
94
95 static void battery_attach(struct device *, struct device *, void *);
96 static int battery_match(struct device *, struct cfdata *, void *);
97 static int battery_update(struct battery_softc *, int);
98 static void battery_setup_envsys(struct battery_softc *);
99 static void battery_refresh(struct sysmon_envsys *, envsys_data_t *);
100 static void battery_poll(void *);
101
102 CFATTACH_DECL(battery, sizeof(struct battery_softc),
103 battery_match, battery_attach, NULL, NULL);
104
105 static int
106 battery_match(struct device *parent, struct cfdata *cf, void *aux)
107 {
108 struct battery_attach_args *baa = aux;
109
110 if (baa->baa_type == BATTERY_TYPE_LEGACY)
111 return 1;
112
113 return 0;
114 }
115
116 static void
117 battery_attach(struct device *parent, struct device *self, void *aux)
118 {
119 struct battery_attach_args *baa = aux;
120 struct battery_softc *sc = (struct battery_softc *)self;
121 uint32_t reg;
122
123 sc->sc_pmu_ops = baa->baa_pmu_ops;
124 printf(": legacy battery ");
125
126 reg = in32rb(0xf3000034);
127 DPRINTF("reg: %08x\n", reg);
128 if (reg & 0x20000000) {
129 sc->sc_type = BTYPE_HOOPER;
130 sc->sc_vmax_charged = 330;
131 sc->sc_vmax_charging = 365;
132 printf("[hooper]\n");
133 } else {
134 sc->sc_type = BTYPE_COMET;
135 sc->sc_vmax_charged = 189;
136 sc->sc_vmax_charging = 213;
137 printf("[comet]\n");
138 }
139 battery_update(sc, 1);
140 /* trigger a status update */
141 sc->sc_oflags = ~sc->sc_flags;
142
143 battery_setup_envsys(sc);
144 sc->sc_pmu_ops->register_callback(sc->sc_pmu_ops->cookie, battery_poll,
145 sc);
146
147 memset(&sc->sc_sm_acpower, 0, sizeof(struct sysmon_pswitch));
148 sc->sc_sm_acpower.smpsw_name = "AC Power";
149 sc->sc_sm_acpower.smpsw_type = PSWITCH_TYPE_ACADAPTER;
150 if (sysmon_pswitch_register(&sc->sc_sm_acpower) != 0)
151 printf("%s: unable to register AC power status with sysmon\n",
152 sc->sc_dev.dv_xname);
153
154 }
155
156 static int
157 battery_update(struct battery_softc *sc, int out)
158 {
159 int len, vmax, pcharge, vb;
160 uint8_t buf[16];
161
162 if (sc->sc_timestamp == time_second)
163 return 0;
164 sc->sc_timestamp = time_second;
165
166 len = sc->sc_pmu_ops->do_command(sc->sc_pmu_ops->cookie,
167 PMU_BATTERY_STATE, 0, NULL, 16, buf);
168 if (len != 9)
169 return -1;
170
171 sc->sc_flags = buf[1];
172
173 if (out) {
174 if (buf[1] & PMU_PWR_AC_PRESENT)
175 printf(" AC");
176 if (buf[1] & PMU_PWR_BATT_CHARGING)
177 printf(" charging");
178 if (buf[1] & PMU_PWR_BATT_PRESENT)
179 printf(" present");
180 if (buf[1] & PMU_PWR_BATT_FULL)
181 printf(" full");
182 printf("\n");
183 }
184
185 sc->sc_cpu_temp = buf[4];
186
187 if ((sc->sc_flags & PMU_PWR_BATT_PRESENT) == 0) {
188 sc->sc_voltage = 0;
189 sc->sc_current = 0;
190 sc->sc_bat_temp = 0;
191 sc->sc_charge = 0;
192 sc->sc_time = 0;
193 return 0;
194 }
195
196 vmax = sc->sc_vmax_charged;
197 vb = (buf[2] << 8) | buf[3];
198 sc->sc_voltage = (vb * 265 + 72665) / 10;
199 sc->sc_current = buf[6];
200 if ((sc->sc_flags & PMU_PWR_AC_PRESENT) == 0) {
201 if (sc->sc_current > 200)
202 vb += ((sc->sc_current - 200) * 15) / 100;
203 } else {
204 vmax = sc->sc_vmax_charging;
205 }
206 sc->sc_charge = (100 * vb) / vmax;
207 if (sc->sc_flags & PMU_PWR_PCHARGE_RESET) {
208 pcharge = (buf[7] << 8) | buf[8];
209 if (pcharge > 6500)
210 pcharge = 6500;
211 pcharge = 100 - pcharge * 100 / 6500;
212 if (pcharge < sc->sc_charge)
213 sc->sc_charge = pcharge;
214 }
215 if (sc->sc_current > 0) {
216 sc->sc_time = (sc->sc_charge * 16440) / sc->sc_current;
217 } else
218 sc->sc_time = 0;
219
220 sc->sc_bat_temp = buf[5];
221
222 if (out) {
223 printf("voltage: %d.%03d\n", sc->sc_voltage / 1000,
224 sc->sc_voltage % 1000);
225 printf("charge: %d%%\n", sc->sc_charge);
226 if (sc->sc_time > 0)
227 printf("time: %d:%02d\n", sc->sc_time / 60,
228 sc->sc_time % 60);
229 }
230
231 return 0;
232 }
233
234 #define INITDATA(index, unit, string) \
235 sc->sc_sensor[index].units = unit; \
236 snprintf(sc->sc_sensor[index].desc, \
237 sizeof(sc->sc_sensor[index].desc), "%s", string);
238
239 static void
240 battery_setup_envsys(struct battery_softc *sc)
241 {
242 int i;
243
244 sc->sc_sme = sysmon_envsys_create();
245
246 INITDATA(BAT_CPU_TEMPERATURE, ENVSYS_STEMP, "CPU temperature");
247 INITDATA(BAT_AC_PRESENT, ENVSYS_INDICATOR, "AC present");
248 INITDATA(BAT_PRESENT, ENVSYS_INDICATOR, "Battery present");
249 INITDATA(BAT_VOLTAGE, ENVSYS_SVOLTS_DC, "Battery voltage");
250 INITDATA(BAT_CHARGE, ENVSYS_INTEGER, "Battery charge");
251 INITDATA(BAT_MAX_CHARGE, ENVSYS_INTEGER, "Battery design cap");
252 INITDATA(BAT_CURRENT, ENVSYS_SAMPS, "Battery current");
253 INITDATA(BAT_TEMPERATURE, ENVSYS_STEMP, "Battery temperature");
254 INITDATA(BAT_CHARGING, ENVSYS_INDICATOR, "Battery charging");
255 INITDATA(BAT_FULL, ENVSYS_INDICATOR, "Battery full");
256 #undef INITDATA
257
258 for (i = 0; i < BAT_NSENSORS; i++) {
259 if (sysmon_envsys_sensor_attach(sc->sc_sme,
260 &sc->sc_sensor[i])) {
261 sysmon_envsys_destroy(sc->sc_sme);
262 return;
263 }
264 }
265
266 sc->sc_sme->sme_name = sc->sc_dev.dv_xname;
267 sc->sc_sme->sme_cookie = sc;
268 sc->sc_sme->sme_refresh = battery_refresh;
269
270 if (sysmon_envsys_register(sc->sc_sme)) {
271 aprint_error("%s: unable to register with sysmon\n",
272 sc->sc_dev.dv_xname);
273 sysmon_envsys_destroy(sc->sc_sme);
274 }
275 }
276
277 static void
278 battery_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
279 {
280 struct battery_softc *sc = sme->sme_cookie;
281 int which = edata->sensor;
282
283 battery_update(sc, 0);
284
285 switch (which) {
286 case BAT_CPU_TEMPERATURE:
287 edata->value_cur = sc->sc_cpu_temp * 1000000 + 273150000;
288 break;
289 case BAT_AC_PRESENT:
290 edata->value_cur = (sc->sc_flags & PMU_PWR_AC_PRESENT);
291 break;
292 case BAT_VOLTAGE:
293 edata->value_cur = sc->sc_voltage * 1000;
294 break;
295 case BAT_CURRENT:
296 edata->value_cur = sc->sc_current * 1000;
297 break;
298 case BAT_CHARGE:
299 edata->value_cur = sc->sc_charge;
300 break;
301 case BAT_MAX_CHARGE:
302 edata->value_cur = 100;
303 break;
304 case BAT_TEMPERATURE:
305 edata->value_cur = sc->sc_bat_temp * 1000000 + 273150000;
306 break;
307 case BAT_CHARGING:
308 if ((sc->sc_flags & PMU_PWR_BATT_CHARGING) &&
309 (sc->sc_flags & PMU_PWR_AC_PRESENT))
310 edata->value_cur = 1;
311 else
312 edata->value_cur = 0;
313
314 break;
315 case BAT_FULL:
316 edata->value_cur = (sc->sc_flags & PMU_PWR_BATT_FULL);
317 break;
318 }
319
320 edata->state = ENVSYS_SVALID;
321 }
322
323 static void
324 battery_poll(void *cookie)
325 {
326 struct battery_softc *sc = cookie;
327
328
329 battery_update(sc, 0);
330 if ((sc->sc_flags & PMU_PWR_AC_PRESENT) == sc->sc_oflags)
331 return;
332
333 sc->sc_oflags = sc->sc_flags & PMU_PWR_AC_PRESENT;
334
335 sysmon_pswitch_event(&sc->sc_sm_acpower,
336 sc->sc_oflags ? PSWITCH_EVENT_PRESSED :
337 PSWITCH_EVENT_RELEASED);
338 }
339