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