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