battery.c revision 1.1.8.2 1 /* $NetBSD: battery.c,v 1.1.8.2 2007/05/27 12:27:42 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Michael Lorenz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The NetBSD Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: battery.c,v 1.1.8.2 2007/05/27 12:27:42 ad Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/proc.h>
40
41 #include <dev/sysmon/sysmonvar.h>
42 #include <dev/sysmon/sysmon_taskq.h>
43
44 #include <macppc/dev/pmuvar.h>
45 #include <macppc/dev/batteryvar.h>
46 #include <machine/bus.h>
47 #include "opt_battery.h"
48
49 #ifdef BATTERY_DEBUG
50 #define DPRINTF printf
51 #else
52 #define DPRINTF while (0) printf
53 #endif
54
55 #define BTYPE_COMET 1
56 #define BTYPE_HOOPER 2
57
58 #define BAT_CPU_TEMPERATURE 0
59 #define BAT_AC_PRESENT 1
60 #define BAT_PRESENT 2
61 #define BAT_VOLTAGE 3
62 #define BAT_CURRENT 4
63 #define BAT_MAX_CHARGE 5
64 #define BAT_CHARGE 6
65 #define BAT_CHARGING 7
66 #define BAT_DISCHARGING 8
67 #define BAT_FULL 9
68 #define BAT_TEMPERATURE 10
69 #define BAT_NSENSORS 11 /* number of sensors */
70
71 struct battery_softc {
72 struct device sc_dev;
73 struct pmu_ops *sc_pmu_ops;
74 int sc_type;
75
76 /* envsys stuff */
77 struct sysmon_envsys sc_sysmon;
78 struct envsys_basic_info sc_sinfo[BAT_NSENSORS];
79 struct envsys_tre_data sc_sdata[BAT_NSENSORS];
80 struct sysmon_pswitch sc_sm_acpower;
81
82 /* battery status */
83 int sc_flags;
84 int sc_oflags;
85 int sc_voltage;
86 int sc_charge;
87 int sc_current;
88 int sc_time;
89 int sc_cpu_temp;
90 int sc_bat_temp;
91 int sc_vmax_charged;
92 int sc_vmax_charging;
93 uint32_t sc_timestamp;
94 };
95
96 static void battery_attach(struct device *, struct device *, void *);
97 static int battery_match(struct device *, struct cfdata *, void *);
98 static int battery_update(struct battery_softc *, int);
99 static void battery_setup_envsys(struct battery_softc *);
100 static int battery_gtredata(struct sysmon_envsys *, struct envsys_tre_data *);
101 static int battery_streinfo(struct sysmon_envsys *,
102 struct envsys_basic_info *);
103 static void battery_poll(void *);
104
105 CFATTACH_DECL(battery, sizeof(struct battery_softc),
106 battery_match, battery_attach, NULL, NULL);
107
108 static int
109 battery_match(struct device *parent, struct cfdata *cf, void *aux)
110 {
111 struct battery_attach_args *baa = aux;
112
113 if (baa->baa_type == BATTERY_TYPE_LEGACY)
114 return 1;
115
116 return 0;
117 }
118
119 static void
120 battery_attach(struct device *parent, struct device *self, void *aux)
121 {
122 struct battery_attach_args *baa = aux;
123 struct battery_softc *sc = (struct battery_softc *)self;
124 uint32_t reg;
125
126 sc->sc_pmu_ops = baa->baa_pmu_ops;
127 printf(": legacy battery ");
128
129 reg = in32rb(0xf3000034);
130 DPRINTF("reg: %08x\n", reg);
131 if (reg & 0x20000000) {
132 sc->sc_type = BTYPE_HOOPER;
133 sc->sc_vmax_charged = 330;
134 sc->sc_vmax_charging = 365;
135 printf("[hooper]\n");
136 } else {
137 sc->sc_type = BTYPE_COMET;
138 sc->sc_vmax_charged = 189;
139 sc->sc_vmax_charging = 213;
140 printf("[comet]\n");
141 }
142 battery_update(sc, 1);
143 /* trigger a status update */
144 sc->sc_oflags = ~sc->sc_flags;
145
146 battery_setup_envsys(sc);
147 sc->sc_pmu_ops->register_callback(sc->sc_pmu_ops->cookie, battery_poll,
148 sc);
149
150 memset(&sc->sc_sm_acpower, 0, sizeof(struct sysmon_pswitch));
151 sc->sc_sm_acpower.smpsw_name = "AC Power";
152 sc->sc_sm_acpower.smpsw_type = PSWITCH_TYPE_ACADAPTER;
153 if (sysmon_pswitch_register(&sc->sc_sm_acpower) != 0)
154 printf("%s: unable to register AC power status with sysmon\n",
155 sc->sc_dev.dv_xname);
156 }
157
158 static int
159 battery_update(struct battery_softc *sc, int out)
160 {
161 int len, vmax, pcharge, vb;
162 uint8_t buf[16];
163
164 if (sc->sc_timestamp == time_second)
165 return 0;
166 sc->sc_timestamp = time_second;
167
168 len = sc->sc_pmu_ops->do_command(sc->sc_pmu_ops->cookie,
169 PMU_BATTERY_STATE, 0, NULL, 16, buf);
170 if (len != 9)
171 return -1;
172
173 sc->sc_flags = buf[1];
174
175 if (out) {
176 if (buf[1] & PMU_PWR_AC_PRESENT)
177 printf(" AC");
178 if (buf[1] & PMU_PWR_BATT_CHARGING)
179 printf(" charging");
180 if (buf[1] & PMU_PWR_BATT_PRESENT)
181 printf(" present");
182 if (buf[1] & PMU_PWR_BATT_FULL)
183 printf(" full");
184 printf("\n");
185 }
186
187 sc->sc_cpu_temp = buf[4];
188
189 if ((sc->sc_flags & PMU_PWR_BATT_PRESENT) == 0) {
190 sc->sc_voltage = 0;
191 sc->sc_current = 0;
192 sc->sc_bat_temp = 0;
193 sc->sc_charge = 0;
194 sc->sc_time = 0;
195 return 0;
196 }
197
198 vmax = sc->sc_vmax_charged;
199 vb = (buf[2] << 8) | buf[3];
200 sc->sc_voltage = (vb * 265 + 72665) / 10;
201 sc->sc_current = buf[6];
202 if ((sc->sc_flags & PMU_PWR_AC_PRESENT) == 0) {
203 if (sc->sc_current > 200)
204 vb += ((sc->sc_current - 200) * 15) / 100;
205 } else {
206 vmax = sc->sc_vmax_charging;
207 }
208 sc->sc_charge = (100 * vb) / vmax;
209 if (sc->sc_flags & PMU_PWR_PCHARGE_RESET) {
210 pcharge = (buf[7] << 8) | buf[8];
211 if (pcharge > 6500)
212 pcharge = 6500;
213 pcharge = 100 - pcharge * 100 / 6500;
214 if (pcharge < sc->sc_charge)
215 sc->sc_charge = pcharge;
216 }
217 if (sc->sc_current > 0) {
218 sc->sc_time = (sc->sc_charge * 16440) / sc->sc_current;
219 } else
220 sc->sc_time = 0;
221
222 sc->sc_bat_temp = buf[5];
223
224 if (out) {
225 printf("voltage: %d.%03d\n", sc->sc_voltage / 1000,
226 sc->sc_voltage % 1000);
227 printf("charge: %d%%\n", sc->sc_charge);
228 if (sc->sc_time > 0)
229 printf("time: %d:%02d\n", sc->sc_time / 60,
230 sc->sc_time % 60);
231 }
232
233 return 0;
234 }
235
236 #define INITDATA(index, unit, string) \
237 sc->sc_sdata[index].sensor = index; \
238 sc->sc_sdata[index].units = unit; \
239 sc->sc_sdata[index].validflags = ENVSYS_FVALID; \
240 sc->sc_sdata[index].warnflags = 0; \
241 sc->sc_sinfo[index].sensor = index; \
242 sc->sc_sinfo[index].units = unit; \
243 sc->sc_sinfo[index].rfact = 1; \
244 sc->sc_sinfo[index].validflags = ENVSYS_FVALID; \
245 snprintf(sc->sc_sinfo[index].desc, sizeof(sc->sc_sinfo[index].desc), \
246 "%s", string);
247
248 static void
249 battery_setup_envsys(struct battery_softc *sc)
250 {
251
252 INITDATA(BAT_CPU_TEMPERATURE, ENVSYS_STEMP, "CPU temperature");
253 INITDATA(BAT_AC_PRESENT, ENVSYS_INDICATOR, "AC present");
254 INITDATA(BAT_PRESENT, ENVSYS_INDICATOR, "Battery present");
255 INITDATA(BAT_VOLTAGE, ENVSYS_SVOLTS_DC, "Battery voltage");
256 INITDATA(BAT_CHARGE, ENVSYS_INTEGER, "Battery charge");
257 INITDATA(BAT_MAX_CHARGE, ENVSYS_INTEGER, "Battery design cap");
258 INITDATA(BAT_CURRENT, ENVSYS_SAMPS, "Battery current");
259 INITDATA(BAT_TEMPERATURE, ENVSYS_STEMP, "Battery temperature");
260 INITDATA(BAT_CHARGING, ENVSYS_INDICATOR, "Battery charging");
261 INITDATA(BAT_DISCHARGING, ENVSYS_INDICATOR, "Battery discharging");
262 INITDATA(BAT_FULL, ENVSYS_INDICATOR, "Battery full");
263 #undef INITDATA
264
265 sc->sc_sysmon.sme_sensor_info = sc->sc_sinfo;
266 sc->sc_sysmon.sme_sensor_data = sc->sc_sdata;
267 sc->sc_sysmon.sme_cookie = sc;
268 sc->sc_sysmon.sme_gtredata = battery_gtredata;
269 sc->sc_sysmon.sme_streinfo = battery_streinfo;
270 sc->sc_sysmon.sme_nsensors = BAT_NSENSORS;
271 sc->sc_sysmon.sme_envsys_version = 1000;
272
273 if (sysmon_envsys_register(&sc->sc_sysmon))
274 printf("%s: unable to register with sysmon\n",
275 sc->sc_dev.dv_xname);
276 }
277
278 static int
279 battery_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
280 {
281 struct battery_softc *sc = sme->sme_cookie;
282 struct envsys_tre_data *cur;
283 int which = tred->sensor;
284
285 battery_update(sc, 0);
286
287 cur = &sc->sc_sdata[BAT_CPU_TEMPERATURE];
288 cur->cur.data_s = sc->sc_cpu_temp * 1000000 + 273150000;
289 cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
290
291 cur = &sc->sc_sdata[BAT_AC_PRESENT];
292 cur->cur.data_s = (sc->sc_flags & PMU_PWR_AC_PRESENT);
293 cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
294
295 cur = &sc->sc_sdata[BAT_PRESENT];
296 cur->cur.data_s = (sc->sc_flags & PMU_PWR_BATT_PRESENT);
297 cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
298
299 cur = &sc->sc_sdata[BAT_VOLTAGE];
300 cur->cur.data_s = sc->sc_voltage * 1000;
301 cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
302
303 cur = &sc->sc_sdata[BAT_CURRENT];
304 cur->cur.data_s = sc->sc_current * 1000;
305 cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
306
307 cur = &sc->sc_sdata[BAT_CHARGE];
308 cur->cur.data_s = sc->sc_charge;
309 cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
310
311 cur = &sc->sc_sdata[BAT_MAX_CHARGE];
312 cur->cur.data_s = 100;
313 cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
314
315 cur = &sc->sc_sdata[BAT_TEMPERATURE];
316 cur->cur.data_s = sc->sc_bat_temp * 1000000 + 273150000;
317 cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
318
319 cur = &sc->sc_sdata[BAT_CHARGING];
320 cur->cur.data_s = (sc->sc_flags & PMU_PWR_BATT_CHARGING);
321 cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
322
323 cur = &sc->sc_sdata[BAT_DISCHARGING];
324 cur->cur.data_s = (!(sc->sc_flags & PMU_PWR_BATT_CHARGING)) &&
325 (!(sc->sc_flags & PMU_PWR_AC_PRESENT));
326 cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
327
328 cur = &sc->sc_sdata[BAT_FULL];
329 cur->cur.data_s = (sc->sc_flags & PMU_PWR_BATT_FULL);
330 cur->validflags = ENVSYS_FVALID | ENVSYS_FCURVALID;
331
332 memcpy(tred, &sc->sc_sdata[which], sizeof(struct envsys_tre_data));
333 return 0;
334 }
335
336 static int
337 battery_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
338 {
339
340 /* XXX Not implemented */
341 binfo->validflags = 0;
342
343 return 0;
344 }
345
346 static void
347 battery_poll(void *cookie)
348 {
349 struct battery_softc *sc = cookie;
350
351 battery_update(sc, 0);
352 if ((sc->sc_flags & PMU_PWR_AC_PRESENT) == sc->sc_oflags)
353 return;
354
355 sc->sc_oflags = sc->sc_flags & PMU_PWR_AC_PRESENT;
356
357 sysmon_pswitch_event(&sc->sc_sm_acpower,
358 sc->sc_oflags ? PSWITCH_EVENT_PRESSED :
359 PSWITCH_EVENT_RELEASED);
360 }
361