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