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