smartbat.c revision 1.8 1 /* $NetBSD: smartbat.c,v 1.8 2011/07/26 08:36:02 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.8 2011/07/26 08:36:02 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 #else
50 #define DPRINTF while (0) printf
51 #endif
52
53 #define BAT_AC_PRESENT 0
54 #define BAT_PRESENT 1
55 #define BAT_VOLTAGE 2
56 #define BAT_CURRENT 3
57 #define BAT_MAX_CHARGE 4
58 #define BAT_CHARGE 5
59 #define BAT_CHARGING 6
60 #define BAT_FULL 7
61 #define BAT_NSENSORS 8 /* number of sensors */
62
63 struct smartbat_softc {
64 device_t sc_dev;
65 struct pmu_ops *sc_pmu_ops;
66 int sc_num;
67
68 /* envsys stuff */
69 struct sysmon_envsys *sc_sme;
70 envsys_data_t sc_sensor[BAT_NSENSORS];
71 struct sysmon_pswitch sc_sm_acpower;
72
73 /* battery status */
74 int sc_flags;
75 int sc_oflags;
76 int sc_voltage;
77 int sc_charge;
78 int sc_max_charge;
79 int sc_draw;
80 int sc_time;
81 uint32_t sc_timestamp;
82 };
83
84 static void smartbat_attach(device_t, device_t, void *);
85 static int smartbat_match(device_t, cfdata_t, void *);
86 static void smartbat_setup_envsys(struct smartbat_softc *);
87 static void smartbat_refresh(struct sysmon_envsys *, envsys_data_t *);
88 static void smartbat_poll(void *);
89 static int smartbat_update(struct smartbat_softc *, int);
90
91 CFATTACH_DECL_NEW(smartbat, sizeof(struct smartbat_softc),
92 smartbat_match, smartbat_attach, NULL, NULL);
93
94 static int
95 smartbat_match(device_t parent, cfdata_t cf, void *aux)
96 {
97 struct battery_attach_args *baa = aux;
98
99 if (baa->baa_type == BATTERY_TYPE_SMART)
100 return 1;
101
102 return 0;
103 }
104
105 static void
106 smartbat_attach(device_t parent, device_t self, void *aux)
107 {
108 struct battery_attach_args *baa = aux;
109 struct smartbat_softc *sc = device_private(self);
110
111 sc->sc_dev = self;
112 sc->sc_pmu_ops = baa->baa_pmu_ops;
113 sc->sc_num = baa->baa_num;
114
115 printf(" addr %d: smart battery\n", sc->sc_num);
116
117 smartbat_update(sc, 1);
118 /* trigger a status update */
119 sc->sc_oflags = ~sc->sc_flags;
120
121 smartbat_setup_envsys(sc);
122 sc->sc_pmu_ops->register_callback(sc->sc_pmu_ops->cookie, smartbat_poll,
123 sc);
124
125 memset(&sc->sc_sm_acpower, 0, sizeof(struct sysmon_pswitch));
126 sc->sc_sm_acpower.smpsw_name = "AC Power";
127 sc->sc_sm_acpower.smpsw_type = PSWITCH_TYPE_ACADAPTER;
128 if (sysmon_pswitch_register(&sc->sc_sm_acpower) != 0)
129 printf("%s: unable to register AC power status with sysmon\n",
130 device_xname(sc->sc_dev));
131 }
132
133 #define INITDATA(index, unit, string) \
134 sc->sc_sensor[index].units = unit; \
135 sc->sc_sensor[index].state = ENVSYS_SINVALID; \
136 snprintf(sc->sc_sensor[index].desc, \
137 sizeof(sc->sc_sensor[index].desc), "%s", string);
138
139 static void
140 smartbat_setup_envsys(struct smartbat_softc *sc)
141 {
142 int i;
143
144 sc->sc_sme = sysmon_envsys_create();
145
146 INITDATA(BAT_AC_PRESENT, ENVSYS_INDICATOR, "AC present");
147 INITDATA(BAT_PRESENT, ENVSYS_INDICATOR, "Battery present");
148 INITDATA(BAT_VOLTAGE, ENVSYS_SVOLTS_DC, "Battery voltage");
149 INITDATA(BAT_CURRENT, ENVSYS_SAMPS, "Battery current");
150 INITDATA(BAT_MAX_CHARGE, ENVSYS_SAMPHOUR, "Battery design cap");
151 INITDATA(BAT_CHARGE, ENVSYS_SAMPHOUR, "Battery charge");
152 INITDATA(BAT_CHARGING, ENVSYS_BATTERY_CHARGE, "Battery charging");
153 INITDATA(BAT_FULL, ENVSYS_INDICATOR, "Battery full");
154 #undef INITDATA
155
156 for (i = 0; i < BAT_NSENSORS; i++) {
157 if (sysmon_envsys_sensor_attach(sc->sc_sme,
158 &sc->sc_sensor[i])) {
159 sysmon_envsys_destroy(sc->sc_sme);
160 return;
161 }
162 }
163
164 sc->sc_sme->sme_name = device_xname(sc->sc_dev);
165 sc->sc_sme->sme_cookie = sc;
166 sc->sc_sme->sme_refresh = smartbat_refresh;
167
168 if (sysmon_envsys_register(sc->sc_sme)) {
169 aprint_error("%s: unable to register with sysmon\n",
170 device_xname(sc->sc_dev));
171 sysmon_envsys_destroy(sc->sc_sme);
172 }
173 }
174
175 static void
176 smartbat_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
177 {
178 struct smartbat_softc *sc = sme->sme_cookie;
179 int which = edata->sensor, present;
180
181 smartbat_update(sc, 0);
182 present = (sc->sc_flags & PMU_PWR_BATT_PRESENT) != 0;
183
184 if (present) {
185 switch (which) {
186 case BAT_AC_PRESENT:
187 edata->value_cur = (sc->sc_flags & PMU_PWR_AC_PRESENT);
188 break;
189 case BAT_PRESENT:
190 edata->value_cur = present;
191 break;
192 case BAT_VOLTAGE:
193 edata->value_cur = sc->sc_voltage * 1000;
194 break;
195 case BAT_CURRENT:
196 edata->value_cur = sc->sc_draw * 1000;
197 break;
198 case BAT_MAX_CHARGE:
199 edata->value_cur = sc->sc_max_charge * 1000;
200 break;
201 case BAT_CHARGE:
202 edata->value_cur = sc->sc_charge * 1000;
203 break;
204 case BAT_CHARGING:
205 if ((sc->sc_flags & PMU_PWR_BATT_CHARGING) &&
206 (sc->sc_flags & PMU_PWR_AC_PRESENT))
207 edata->value_cur = 1;
208 else
209 edata->value_cur = 0;
210
211 break;
212 case BAT_FULL:
213 edata->value_cur = (sc->sc_flags & PMU_PWR_BATT_FULL);
214 break;
215 }
216 edata->state = ENVSYS_SVALID;
217 } else {
218 /* battery isn't there */
219 switch (which) {
220 case BAT_AC_PRESENT:
221 edata->value_cur = (sc->sc_flags & PMU_PWR_AC_PRESENT);
222 edata->state = ENVSYS_SVALID;
223 break;
224 case BAT_PRESENT:
225 edata->value_cur = present;
226 edata->state = ENVSYS_SVALID;
227 break;
228 default:
229 edata->state = ENVSYS_SINVALID;
230 edata->value_cur = 0;
231 }
232 }
233 }
234
235 /*
236 * Thanks to Paul Mackerras and Fabio Riccardi's Linux implementation
237 * for a clear description of the PMU results.
238 */
239 static int
240 smartbat_update(struct smartbat_softc *sc, int out)
241 {
242 int len;
243 uint8_t buf[16];
244 uint8_t battery_number;
245
246 if (sc->sc_timestamp == time_second)
247 return 0;
248 sc->sc_timestamp = time_second;
249
250 /* sc_num starts from 0, but we need to start from 1 */
251 battery_number = sc->sc_num + 1;
252 len = sc->sc_pmu_ops->do_command(sc->sc_pmu_ops->cookie,
253 PMU_SMART_BATTERY_STATE,
254 1, &battery_number,
255 16, buf);
256
257 if (len < 0) {
258 DPRINTF("%s: couldn't get battery data\n", device_xname(sc->sc_dev));
259 /* XXX: the return value is never checked */
260 return -1;
261 }
262
263 /* Now, buf[0] is the command number, which we already know.
264 That's why all indexes are off by one compared to
265 pm_battery_info_smart in pm_direct.c.
266 */
267 sc->sc_flags = buf[2];
268
269 /* XXX: are these all valid for smart batteries? */
270 if (out) {
271 printf(" flags: %x", buf[2]);
272 if (buf[2] & PMU_PWR_AC_PRESENT)
273 printf(" AC");
274 if (buf[2] & PMU_PWR_BATT_CHARGING)
275 printf(" charging");
276 if (buf[2] & PMU_PWR_BATT_PRESENT)
277 printf(" present");
278 if (buf[2] & PMU_PWR_BATT_FULL)
279 printf(" full");
280 printf("\n");
281 }
282
283 switch(buf[1]) {
284 case 3:
285 case 4:
286 sc->sc_charge = buf[3];
287 sc->sc_max_charge = buf[4];
288 sc->sc_draw = *((signed char *)&buf[5]);
289 sc->sc_voltage = buf[6];
290 break;
291 case 5:
292 sc->sc_charge = ((buf[3] << 8) | (buf[4]));
293 sc->sc_max_charge = ((buf[5] << 8) | (buf[6]));
294 sc->sc_draw = *((signed short *)&buf[7]);
295 sc->sc_voltage = ((buf[9] << 8) | (buf[8]));
296 break;
297 default:
298 /* XXX - Error condition */
299 DPRINTF("%s: why is buf[1] %x?\n", device_xname(sc->sc_dev), buf[1]);
300 sc->sc_charge = 0;
301 sc->sc_max_charge = 0;
302 sc->sc_draw = 0;
303 sc->sc_voltage = 0;
304 break;
305 }
306
307 return 1;
308 }
309
310 static void
311 smartbat_poll(void *cookie)
312 {
313 struct smartbat_softc *sc = cookie;
314
315 smartbat_update(sc, 0);
316 if ((sc->sc_flags & PMU_PWR_AC_PRESENT) == sc->sc_oflags)
317 return;
318
319 sc->sc_oflags = sc->sc_flags & PMU_PWR_AC_PRESENT;
320
321 sysmon_pswitch_event(&sc->sc_sm_acpower,
322 sc->sc_oflags ? PSWITCH_EVENT_PRESSED :
323 PSWITCH_EVENT_RELEASED);
324 }
325