smartbat.c revision 1.10 1 /* $NetBSD: smartbat.c,v 1.10 2012/09/06 05:03:59 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.10 2012/09/06 05:03:59 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_CHARGE_STATE 6
63 #define BAT_FULL 7
64 #define BAT_NSENSORS 8 /* number of sensors */
65
66 struct smartbat_softc {
67 device_t sc_dev;
68 struct pmu_ops *sc_pmu_ops;
69 int sc_num;
70
71 /* envsys stuff */
72 struct sysmon_envsys *sc_bat_sme;
73 envsys_data_t sc_bat_sensor[BAT_NSENSORS];
74 struct sysmon_envsys *sc_ac_sme;
75 envsys_data_t sc_ac_sensor[1];
76 struct sysmon_pswitch sc_sm_acpower;
77 int sc_have_ac;
78
79 /* battery status */
80 int sc_flags;
81 int sc_oflags;
82 int sc_voltage;
83 int sc_charge;
84 int sc_max_charge;
85 int sc_draw;
86 int sc_time;
87 uint32_t sc_timestamp;
88 };
89
90 static void smartbat_attach(device_t, device_t, void *);
91 static int smartbat_match(device_t, cfdata_t, void *);
92 static void smartbat_setup_envsys(struct smartbat_softc *);
93 static void smartbat_refresh(struct sysmon_envsys *, envsys_data_t *);
94 static void smartbat_refresh_ac(struct sysmon_envsys *, envsys_data_t *);
95 static void smartbat_poll(void *);
96 static int smartbat_update(struct smartbat_softc *, int);
97
98 CFATTACH_DECL_NEW(smartbat, sizeof(struct smartbat_softc),
99 smartbat_match, smartbat_attach, NULL, NULL);
100
101 static int
102 smartbat_match(device_t parent, cfdata_t cf, void *aux)
103 {
104 struct battery_attach_args *baa = aux;
105
106 if (baa->baa_type == BATTERY_TYPE_SMART)
107 return 1;
108
109 return 0;
110 }
111
112 static void
113 smartbat_attach(device_t parent, device_t self, void *aux)
114 {
115 struct battery_attach_args *baa = aux;
116 struct smartbat_softc *sc = device_private(self);
117
118 sc->sc_dev = self;
119 sc->sc_pmu_ops = baa->baa_pmu_ops;
120 sc->sc_num = baa->baa_num;
121
122 /*
123 * we can have more than one instance but only the first one needs
124 * to report AC status
125 */
126 sc->sc_have_ac = FALSE;
127 if (sc->sc_num == 0)
128 sc->sc_have_ac = TRUE;
129
130 printf(" addr %d: smart battery\n", sc->sc_num);
131
132 smartbat_update(sc, 1);
133 /* trigger a status update */
134 sc->sc_oflags = ~sc->sc_flags;
135
136 smartbat_setup_envsys(sc);
137
138 if (sc->sc_have_ac) {
139 memset(&sc->sc_sm_acpower, 0, sizeof(struct sysmon_pswitch));
140 sc->sc_sm_acpower.smpsw_name = "AC Power";
141 sc->sc_sm_acpower.smpsw_type = PSWITCH_TYPE_ACADAPTER;
142 if (sysmon_pswitch_register(&sc->sc_sm_acpower) != 0)
143 printf("%s: unable to register AC power status with " \
144 "sysmon\n",
145 device_xname(sc->sc_dev));
146 sc->sc_pmu_ops->register_callback(sc->sc_pmu_ops->cookie,
147 smartbat_poll, sc);
148 }
149 }
150
151
152 static void
153 smartbat_setup_envsys(struct smartbat_softc *sc)
154 {
155 int i;
156
157 if (sc->sc_have_ac) {
158
159 #define INITDATA(index, unit, string) \
160 sc->sc_ac_sensor[index].units = unit; \
161 sc->sc_ac_sensor[index].state = ENVSYS_SINVALID; \
162 snprintf(sc->sc_ac_sensor[index].desc, \
163 sizeof(sc->sc_ac_sensor[index].desc), "%s", string);
164
165 INITDATA(BAT_AC_PRESENT, ENVSYS_INDICATOR, "connected");
166 #undef INITDATA
167
168 sc->sc_ac_sme = sysmon_envsys_create();
169
170 if (sysmon_envsys_sensor_attach(sc->sc_ac_sme,
171 &sc->sc_ac_sensor[0])) {
172 sysmon_envsys_destroy(sc->sc_ac_sme);
173 return;
174 }
175
176 sc->sc_ac_sme->sme_name = "AC Adaptor";
177 sc->sc_ac_sme->sme_cookie = sc;
178 sc->sc_ac_sme->sme_refresh = smartbat_refresh_ac;
179 sc->sc_ac_sme->sme_class = SME_CLASS_ACADAPTER;
180
181 if (sysmon_envsys_register(sc->sc_ac_sme)) {
182 aprint_error("%s: unable to register AC with sysmon\n",
183 device_xname(sc->sc_dev));
184 sysmon_envsys_destroy(sc->sc_ac_sme);
185 }
186 }
187
188 sc->sc_bat_sme = sysmon_envsys_create();
189
190 #define INITDATA(index, unit, string) \
191 sc->sc_bat_sensor[index].units = unit; \
192 sc->sc_bat_sensor[index].state = ENVSYS_SINVALID; \
193 snprintf(sc->sc_bat_sensor[index].desc, \
194 sizeof(sc->sc_bat_sensor[index].desc), "%s", string);
195
196 INITDATA(BAT_PRESENT, ENVSYS_INDICATOR, "Battery present");
197 INITDATA(BAT_VOLTAGE, ENVSYS_SVOLTS_DC, "Battery voltage");
198 INITDATA(BAT_CURRENT, ENVSYS_SAMPS, "Battery current");
199 INITDATA(BAT_MAX_CHARGE, ENVSYS_SWATTHOUR, "Battery design cap");
200 INITDATA(BAT_CHARGE, ENVSYS_SWATTHOUR, "Battery charge");
201 INITDATA(BAT_CHARGING, ENVSYS_BATTERY_CHARGE, "Battery charging");
202 INITDATA(BAT_CHARGE_STATE, ENVSYS_BATTERY_CAPACITY,
203 "Battery charge state");
204 INITDATA(BAT_FULL, ENVSYS_INDICATOR, "Battery full");
205 #undef INITDATA
206
207 sc->sc_bat_sensor[BAT_CHARGE_STATE].value_cur = ENVSYS_BATTERY_CAPACITY_NORMAL;
208 sc->sc_bat_sensor[BAT_CHARGE_STATE].state = ENVSYS_SVALID;
209 sc->sc_bat_sensor[BAT_CHARGING].value_cur = TRUE;
210 sc->sc_bat_sensor[BAT_CHARGING].state = ENVSYS_SVALID;
211 sc->sc_bat_sensor[BAT_CHARGING].value_cur = TRUE;
212 sc->sc_bat_sensor[BAT_CHARGING].state = ENVSYS_SVALID;
213
214 for (i = 0; i < BAT_NSENSORS; i++) {
215 sc->sc_bat_sensor[i].flags = ENVSYS_FMONNOTSUPP;
216 if (sysmon_envsys_sensor_attach(sc->sc_bat_sme,
217 &sc->sc_bat_sensor[i])) {
218 sysmon_envsys_destroy(sc->sc_bat_sme);
219 return;
220 }
221 }
222
223 sc->sc_bat_sme->sme_name = device_xname(sc->sc_dev);
224 sc->sc_bat_sme->sme_cookie = sc;
225 sc->sc_bat_sme->sme_refresh = smartbat_refresh;
226 sc->sc_bat_sme->sme_class = SME_CLASS_BATTERY;
227
228 if (sysmon_envsys_register(sc->sc_bat_sme)) {
229 aprint_error("%s: unable to register with sysmon\n",
230 device_xname(sc->sc_dev));
231 sysmon_envsys_destroy(sc->sc_bat_sme);
232 }
233 }
234
235 static void
236 smartbat_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
237 {
238 struct smartbat_softc *sc = sme->sme_cookie;
239 int which = edata->sensor, present;
240
241 smartbat_update(sc, 0);
242 present = (sc->sc_flags & PMU_PWR_BATT_PRESENT) != 0;
243
244 if (present) {
245 switch (which) {
246 case BAT_PRESENT:
247 edata->value_cur = present;
248 break;
249 case BAT_VOLTAGE:
250 edata->value_cur = sc->sc_voltage * 1000;
251 break;
252 case BAT_CURRENT:
253 edata->value_cur = sc->sc_draw * 1000;
254 break;
255 case BAT_MAX_CHARGE:
256 edata->value_cur = sc->sc_max_charge * 1000;
257 break;
258 case BAT_CHARGE:
259 edata->value_cur = sc->sc_charge * 1000;
260 break;
261 case BAT_CHARGING:
262 if ((sc->sc_flags & PMU_PWR_BATT_CHARGING) &&
263 (sc->sc_flags & PMU_PWR_AC_PRESENT))
264 edata->value_cur = 1;
265 else
266 edata->value_cur = 0;
267 break;
268 case BAT_CHARGE_STATE:
269 edata->value_cur = ENVSYS_BATTERY_CAPACITY_NORMAL;
270 break;
271 case BAT_FULL:
272 edata->value_cur = (sc->sc_flags & PMU_PWR_BATT_FULL);
273 break;
274 }
275 edata->state = ENVSYS_SVALID;
276 } else {
277 /* battery isn't there */
278 switch (which) {
279 case BAT_PRESENT:
280 edata->value_cur = present;
281 edata->state = ENVSYS_SVALID;
282 break;
283 default:
284 edata->state = ENVSYS_SINVALID;
285 edata->value_cur = 0;
286 }
287 }
288 }
289
290 static void
291 smartbat_refresh_ac(struct sysmon_envsys *sme, envsys_data_t *edata)
292 {
293 struct smartbat_softc *sc = sme->sme_cookie;
294 int which = edata->sensor;
295
296 smartbat_update(sc, 0);
297 switch (which) {
298 case BAT_AC_PRESENT:
299 edata->value_cur = (sc->sc_flags & PMU_PWR_AC_PRESENT);
300 edata->state = ENVSYS_SVALID;
301 break;
302 }
303 }
304
305 /*
306 * Thanks to Paul Mackerras and Fabio Riccardi's Linux implementation
307 * for a clear description of the PMU results.
308 */
309 static int
310 smartbat_update(struct smartbat_softc *sc, int out)
311 {
312 int len;
313 uint8_t buf[16];
314 uint8_t battery_number;
315
316 if (sc->sc_timestamp == time_second)
317 return 0;
318 sc->sc_timestamp = time_second;
319
320 /* sc_num starts from 0, but we need to start from 1 */
321 battery_number = sc->sc_num + 1;
322 len = sc->sc_pmu_ops->do_command(sc->sc_pmu_ops->cookie,
323 PMU_SMART_BATTERY_STATE,
324 1, &battery_number,
325 16, buf);
326
327 if (len < 0) {
328 DPRINTF("%s: couldn't get battery data\n",
329 device_xname(sc->sc_dev));
330 /* XXX: the return value is never checked */
331 return -1;
332 }
333
334 /* Now, buf[0] is the command number, which we already know.
335 That's why all indexes are off by one compared to
336 pm_battery_info_smart in pm_direct.c.
337 */
338 sc->sc_flags = buf[2];
339
340 /* XXX: are these all valid for smart batteries? */
341 if (out) {
342 printf(" flags: %x", buf[2]);
343 if (buf[2] & PMU_PWR_AC_PRESENT)
344 printf(" AC");
345 if (buf[2] & PMU_PWR_BATT_CHARGING)
346 printf(" charging");
347 if (buf[2] & PMU_PWR_BATT_PRESENT)
348 printf(" present");
349 if (buf[2] & PMU_PWR_BATT_FULL)
350 printf(" full");
351 printf("\n");
352 }
353
354 switch(buf[1]) {
355 case 3:
356 case 4:
357 sc->sc_charge = buf[3];
358 sc->sc_max_charge = buf[4];
359 sc->sc_draw = *((signed char *)&buf[5]);
360 sc->sc_voltage = buf[6];
361 break;
362 case 5:
363 sc->sc_charge = ((buf[3] << 8) | (buf[4]));
364 sc->sc_max_charge = ((buf[5] << 8) | (buf[6]));
365 sc->sc_draw = *((signed short *)&buf[7]);
366 sc->sc_voltage = ((buf[9] << 8) | (buf[8]));
367 break;
368 default:
369 /* XXX - Error condition */
370 DPRINTF("%s: why is buf[1] %x?\n", device_xname(sc->sc_dev),
371 buf[1]);
372 sc->sc_charge = 0;
373 sc->sc_max_charge = 0;
374 sc->sc_draw = 0;
375 sc->sc_voltage = 0;
376 break;
377 }
378
379 return 1;
380 }
381
382 static void
383 smartbat_poll(void *cookie)
384 {
385 struct smartbat_softc *sc = cookie;
386
387 smartbat_update(sc, 0);
388 if ((sc->sc_flags & PMU_PWR_AC_PRESENT) == sc->sc_oflags)
389 return;
390
391 sc->sc_oflags = sc->sc_flags & PMU_PWR_AC_PRESENT;
392
393 sysmon_pswitch_event(&sc->sc_sm_acpower,
394 sc->sc_oflags ? PSWITCH_EVENT_PRESSED :
395 PSWITCH_EVENT_RELEASED);
396 }
397