smartbat.c revision 1.9 1 /* $NetBSD: smartbat.c,v 1.9 2012/09/05 21:23:31 macallan Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Michael Lorenz
5 * 2008 Magnus Henoch
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: smartbat.c,v 1.9 2012/09/05 21:23:31 macallan Exp $");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/device.h>
37 #include <sys/proc.h>
38
39 #include <dev/sysmon/sysmonvar.h>
40 #include <dev/sysmon/sysmon_taskq.h>
41
42 #include <macppc/dev/pmuvar.h>
43 #include <macppc/dev/batteryvar.h>
44 #include <sys/bus.h>
45 #include "opt_battery.h"
46
47 #ifdef SMARTBAT_DEBUG
48 #define DPRINTF printf
49 #define static /* static */
50 #else
51 #define DPRINTF while (0) printf
52 #endif
53
54 #define BAT_AC_PRESENT 0
55
56 #define BAT_PRESENT 0
57 #define BAT_VOLTAGE 1
58 #define BAT_CURRENT 2
59 #define BAT_MAX_CHARGE 3
60 #define BAT_CHARGE 4
61 #define BAT_CHARGING 5
62 #define BAT_FULL 6
63 #define BAT_NSENSORS 7 /* number of sensors */
64
65 struct smartbat_softc {
66 device_t sc_dev;
67 struct pmu_ops *sc_pmu_ops;
68 int sc_num;
69
70 /* envsys stuff */
71 struct sysmon_envsys *sc_bat_sme;
72 envsys_data_t sc_bat_sensor[BAT_NSENSORS];
73 struct sysmon_envsys *sc_ac_sme;
74 envsys_data_t sc_ac_sensor[1];
75 struct sysmon_pswitch sc_sm_acpower;
76 int sc_have_ac;
77
78 /* battery status */
79 int sc_flags;
80 int sc_oflags;
81 int sc_voltage;
82 int sc_charge;
83 int sc_max_charge;
84 int sc_draw;
85 int sc_time;
86 uint32_t sc_timestamp;
87 };
88
89 static void smartbat_attach(device_t, device_t, void *);
90 static int smartbat_match(device_t, cfdata_t, void *);
91 static void smartbat_setup_envsys(struct smartbat_softc *);
92 static void smartbat_refresh(struct sysmon_envsys *, envsys_data_t *);
93 static void smartbat_refresh_ac(struct sysmon_envsys *, envsys_data_t *);
94 static void smartbat_poll(void *);
95 static int smartbat_update(struct smartbat_softc *, int);
96
97 CFATTACH_DECL_NEW(smartbat, sizeof(struct smartbat_softc),
98 smartbat_match, smartbat_attach, NULL, NULL);
99
100 static int
101 smartbat_match(device_t parent, cfdata_t cf, void *aux)
102 {
103 struct battery_attach_args *baa = aux;
104
105 if (baa->baa_type == BATTERY_TYPE_SMART)
106 return 1;
107
108 return 0;
109 }
110
111 static void
112 smartbat_attach(device_t parent, device_t self, void *aux)
113 {
114 struct battery_attach_args *baa = aux;
115 struct smartbat_softc *sc = device_private(self);
116
117 sc->sc_dev = self;
118 sc->sc_pmu_ops = baa->baa_pmu_ops;
119 sc->sc_num = baa->baa_num;
120
121 /*
122 * we can have more than one instance but only the first one needs
123 * to report AC status
124 */
125 sc->sc_have_ac = (device_unit(self) == 0);
126 printf(" addr %d: smart battery\n", sc->sc_num);
127
128 smartbat_update(sc, 1);
129 /* trigger a status update */
130 sc->sc_oflags = ~sc->sc_flags;
131
132 smartbat_setup_envsys(sc);
133 sc->sc_pmu_ops->register_callback(sc->sc_pmu_ops->cookie,
134 smartbat_poll, sc);
135
136 if (sc->sc_have_ac) {
137 memset(&sc->sc_sm_acpower, 0, sizeof(struct sysmon_pswitch));
138 sc->sc_sm_acpower.smpsw_name = "AC Power";
139 sc->sc_sm_acpower.smpsw_type = PSWITCH_TYPE_ACADAPTER;
140 if (sysmon_pswitch_register(&sc->sc_sm_acpower) != 0)
141 printf("%s: unable to register AC power status with " \
142 "sysmon\n",
143 device_xname(sc->sc_dev));
144 }
145 }
146
147
148 static void
149 smartbat_setup_envsys(struct smartbat_softc *sc)
150 {
151 int i;
152
153 if (sc->sc_have_ac) {
154
155 #define INITDATA(index, unit, string) \
156 sc->sc_ac_sensor[index].units = unit; \
157 sc->sc_ac_sensor[index].state = ENVSYS_SINVALID; \
158 snprintf(sc->sc_ac_sensor[index].desc, \
159 sizeof(sc->sc_ac_sensor[index].desc), "%s", string);
160
161 INITDATA(BAT_AC_PRESENT, ENVSYS_INDICATOR, "connected");
162 #undef INITDATA
163
164 sc->sc_ac_sme = sysmon_envsys_create();
165
166 if (sysmon_envsys_sensor_attach(sc->sc_ac_sme,
167 &sc->sc_ac_sensor[0])) {
168 sysmon_envsys_destroy(sc->sc_ac_sme);
169 return;
170 }
171
172 sc->sc_ac_sme->sme_name = "AC Adaptor";
173 sc->sc_ac_sme->sme_cookie = sc;
174 sc->sc_ac_sme->sme_refresh = smartbat_refresh_ac;
175 sc->sc_ac_sme->sme_class = SME_CLASS_ACADAPTER;
176
177 if (sysmon_envsys_register(sc->sc_ac_sme)) {
178 aprint_error("%s: unable to register AC with sysmon\n",
179 device_xname(sc->sc_dev));
180 sysmon_envsys_destroy(sc->sc_ac_sme);
181 }
182 }
183
184 sc->sc_bat_sme = sysmon_envsys_create();
185
186 #define INITDATA(index, unit, string) \
187 sc->sc_bat_sensor[index].units = unit; \
188 sc->sc_bat_sensor[index].state = ENVSYS_SINVALID; \
189 snprintf(sc->sc_bat_sensor[index].desc, \
190 sizeof(sc->sc_bat_sensor[index].desc), "%s", string);
191
192 INITDATA(BAT_PRESENT, ENVSYS_INDICATOR, "Battery present");
193 INITDATA(BAT_VOLTAGE, ENVSYS_SVOLTS_DC, "Battery voltage");
194 INITDATA(BAT_CURRENT, ENVSYS_SAMPS, "Battery current");
195 INITDATA(BAT_MAX_CHARGE, ENVSYS_SAMPHOUR, "Battery design cap");
196 INITDATA(BAT_CHARGE, ENVSYS_SAMPHOUR, "Battery charge");
197 INITDATA(BAT_CHARGING, ENVSYS_BATTERY_CHARGE, "Battery charging");
198 #if 0
199 INITDATA(BAT_CHARGE_STATE, ENVSYS_BATTERY_CAPACITY,
200 "Battery charge state");
201 #endif
202 INITDATA(BAT_FULL, ENVSYS_INDICATOR, "Battery full");
203 #undef INITDATA
204 for (i = 0; i < BAT_NSENSORS; i++) {
205 sc->sc_bat_sensor[i].flags = ENVSYS_FMONNOTSUPP;
206 if (sysmon_envsys_sensor_attach(sc->sc_bat_sme,
207 &sc->sc_bat_sensor[i])) {
208 sysmon_envsys_destroy(sc->sc_bat_sme);
209 return;
210 }
211 }
212
213 sc->sc_bat_sme->sme_name = device_xname(sc->sc_dev);
214 sc->sc_bat_sme->sme_cookie = sc;
215 sc->sc_bat_sme->sme_refresh = smartbat_refresh;
216 sc->sc_bat_sme->sme_class = SME_CLASS_BATTERY;
217
218 if (sysmon_envsys_register(sc->sc_bat_sme)) {
219 aprint_error("%s: unable to register with sysmon\n",
220 device_xname(sc->sc_dev));
221 sysmon_envsys_destroy(sc->sc_bat_sme);
222 }
223 }
224
225 static void
226 smartbat_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
227 {
228 struct smartbat_softc *sc = sme->sme_cookie;
229 int which = edata->sensor, present;
230
231 smartbat_update(sc, 0);
232 present = (sc->sc_flags & PMU_PWR_BATT_PRESENT) != 0;
233
234 if (present) {
235 switch (which) {
236 case BAT_PRESENT:
237 edata->value_cur = present;
238 break;
239 case BAT_VOLTAGE:
240 edata->value_cur = sc->sc_voltage * 1000;
241 break;
242 case BAT_CURRENT:
243 edata->value_cur = sc->sc_draw * 1000;
244 break;
245 case BAT_MAX_CHARGE:
246 edata->value_cur = sc->sc_max_charge * 1000;
247 break;
248 case BAT_CHARGE:
249 edata->value_cur = sc->sc_charge * 1000;
250 break;
251 case BAT_CHARGING:
252 if ((sc->sc_flags & PMU_PWR_BATT_CHARGING) &&
253 (sc->sc_flags & PMU_PWR_AC_PRESENT))
254 edata->value_cur = 1;
255 else
256 edata->value_cur = 0;
257
258 break;
259 case BAT_FULL:
260 edata->value_cur = (sc->sc_flags & PMU_PWR_BATT_FULL);
261 break;
262 }
263 edata->state = ENVSYS_SVALID;
264 } else {
265 /* battery isn't there */
266 switch (which) {
267 case BAT_PRESENT:
268 edata->value_cur = present;
269 edata->state = ENVSYS_SVALID;
270 break;
271 default:
272 edata->state = ENVSYS_SINVALID;
273 edata->value_cur = 0;
274 }
275 }
276 }
277
278 static void
279 smartbat_refresh_ac(struct sysmon_envsys *sme, envsys_data_t *edata)
280 {
281 struct smartbat_softc *sc = sme->sme_cookie;
282 int which = edata->sensor;
283
284 smartbat_update(sc, 0);
285 switch (which) {
286 case BAT_AC_PRESENT:
287 edata->value_cur = (sc->sc_flags & PMU_PWR_AC_PRESENT);
288 edata->state = ENVSYS_SVALID;
289 break;
290 }
291 }
292
293 /*
294 * Thanks to Paul Mackerras and Fabio Riccardi's Linux implementation
295 * for a clear description of the PMU results.
296 */
297 static int
298 smartbat_update(struct smartbat_softc *sc, int out)
299 {
300 int len;
301 uint8_t buf[16];
302 uint8_t battery_number;
303
304 if (sc->sc_timestamp == time_second)
305 return 0;
306 sc->sc_timestamp = time_second;
307
308 /* sc_num starts from 0, but we need to start from 1 */
309 battery_number = sc->sc_num + 1;
310 len = sc->sc_pmu_ops->do_command(sc->sc_pmu_ops->cookie,
311 PMU_SMART_BATTERY_STATE,
312 1, &battery_number,
313 16, buf);
314
315 if (len < 0) {
316 DPRINTF("%s: couldn't get battery data\n",
317 device_xname(sc->sc_dev));
318 /* XXX: the return value is never checked */
319 return -1;
320 }
321
322 /* Now, buf[0] is the command number, which we already know.
323 That's why all indexes are off by one compared to
324 pm_battery_info_smart in pm_direct.c.
325 */
326 sc->sc_flags = buf[2];
327
328 /* XXX: are these all valid for smart batteries? */
329 if (out) {
330 printf(" flags: %x", buf[2]);
331 if (buf[2] & PMU_PWR_AC_PRESENT)
332 printf(" AC");
333 if (buf[2] & PMU_PWR_BATT_CHARGING)
334 printf(" charging");
335 if (buf[2] & PMU_PWR_BATT_PRESENT)
336 printf(" present");
337 if (buf[2] & PMU_PWR_BATT_FULL)
338 printf(" full");
339 printf("\n");
340 }
341
342 switch(buf[1]) {
343 case 3:
344 case 4:
345 sc->sc_charge = buf[3];
346 sc->sc_max_charge = buf[4];
347 sc->sc_draw = *((signed char *)&buf[5]);
348 sc->sc_voltage = buf[6];
349 break;
350 case 5:
351 sc->sc_charge = ((buf[3] << 8) | (buf[4]));
352 sc->sc_max_charge = ((buf[5] << 8) | (buf[6]));
353 sc->sc_draw = *((signed short *)&buf[7]);
354 sc->sc_voltage = ((buf[9] << 8) | (buf[8]));
355 break;
356 default:
357 /* XXX - Error condition */
358 DPRINTF("%s: why is buf[1] %x?\n", device_xname(sc->sc_dev),
359 buf[1]);
360 sc->sc_charge = 0;
361 sc->sc_max_charge = 0;
362 sc->sc_draw = 0;
363 sc->sc_voltage = 0;
364 break;
365 }
366
367 return 1;
368 }
369
370 static void
371 smartbat_poll(void *cookie)
372 {
373 struct smartbat_softc *sc = cookie;
374
375 smartbat_update(sc, 0);
376 if ((sc->sc_flags & PMU_PWR_AC_PRESENT) == sc->sc_oflags)
377 return;
378
379 sc->sc_oflags = sc->sc_flags & PMU_PWR_AC_PRESENT;
380
381 sysmon_pswitch_event(&sc->sc_sm_acpower,
382 sc->sc_oflags ? PSWITCH_EVENT_PRESSED :
383 PSWITCH_EVENT_RELEASED);
384 }
385