xref: /netbsd/sys/dev/acpi/acpi_bat.c (revision c4a72b64)
1 /*	$NetBSD: acpi_bat.c,v 1.7 2002/10/02 16:33:36 thorpej Exp $	*/
2 
3 /*
4  * Copyright 2001 Bill Sommerfeld.
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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed for the NetBSD Project by
18  *	Wasabi Systems, Inc.
19  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
20  *    or promote products derived from this software without specific prior
21  *    written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #define ACPI_BAT_DEBUG
37 
38 /*
39  * ACPI Battery Driver.
40  *
41  * ACPI defines two different battery device interfaces: "Control
42  * Method" batteries, in which AML methods are defined in order to get
43  * battery status and set battery alarm thresholds, and a "Smart
44  * Battery" device, which is an SMbus device accessed through the ACPI
45  * Embedded Controller device.
46  *
47  * This driver is for the "Control Method"-style battery only.
48  */
49 
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: acpi_bat.c,v 1.7 2002/10/02 16:33:36 thorpej Exp $");
52 
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>		/* for hz */
56 #include <sys/device.h>
57 #include <sys/callout.h>
58 
59 #include <dev/acpi/acpica.h>
60 #include <dev/acpi/acpireg.h>
61 #include <dev/acpi/acpivar.h>
62 
63 #define	BAT_WORDS	13
64 
65 struct acpibat_softc {
66 	struct device sc_dev;		/* base device glue */
67 	struct acpi_devnode *sc_node;	/* our ACPI devnode */
68 	int sc_flags;			/* see below */
69 	struct callout sc_callout; 	/* XXX temporary polling */
70 	int sc_status;			/* power status */
71 	int sc_rate;			/* current drain rate */
72 	int sc_capacity;		/* current capacity */
73 	int sc_mv;			/* current potential in mV */
74 	int sc_design_capacity;		/* design capacity */
75 	int sc_pred_capacity;		/* estimated current max */
76 	int sc_warn_capacity;		/* warning level */
77 	int sc_low_capacity;		/* low level */
78 
79 	ACPI_OBJECT sc_Ret[BAT_WORDS];	/* Return Buffer */
80 };
81 
82 #define	ABAT_F_VERBOSE		0x01	/* verbose events */
83 #define ABAT_F_PWRUNIT_MA	0x02 	/* mA instead of mW */
84 
85 #define ACM_RATEUNIT(sc) (((sc)->sc_flags & ABAT_F_PWRUNIT_MA)?"A":"W")
86 #define ACM_CAPUNIT(sc) (((sc)->sc_flags & ABAT_F_PWRUNIT_MA)?"Ah":"Wh")
87 #define ACM_SCALE(x)	((x) / 1000), ((x) % 1000)
88 
89 int	acpibat_match(struct device *, struct cfdata *, void *);
90 void	acpibat_attach(struct device *, struct device *, void *);
91 
92 CFATTACH_DECL(acpibat, sizeof(struct acpibat_softc),
93     acpibat_match, acpibat_attach, NULL, NULL);
94 
95 static void acpibat_get_status(void *);
96 static void acpibat_get_info(void *);
97 void	acpibat_notify_handler(ACPI_HANDLE, UINT32, void *context);
98 static void acpibat_tick(void *);
99 
100 /*
101  * acpibat_match:
102  *
103  *	Autoconfiguration `match' routine.
104  */
105 int
106 acpibat_match(struct device *parent, struct cfdata *match, void *aux)
107 {
108 	struct acpi_attach_args *aa = aux;
109 
110 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
111 		return (0);
112 
113 	if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0A") == 0)
114 		return (1);
115 
116 	return (0);
117 }
118 
119 /*
120  * acpibat_attach:
121  *
122  *	Autoconfiguration `attach' routine.
123  */
124 void
125 acpibat_attach(struct device *parent, struct device *self, void *aux)
126 {
127 	struct acpibat_softc *sc = (void *) self;
128 	struct acpi_attach_args *aa = aux;
129 	ACPI_STATUS rv;
130 
131 	printf(": ACPI Battery\n");
132 
133 	sc->sc_node = aa->aa_node;
134 
135 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
136 	    ACPI_DEVICE_NOTIFY, acpibat_notify_handler, sc);
137 	if (rv != AE_OK) {
138 		printf("%s: unable to register DEVICE NOTIFY handler: %d\n",
139 		    sc->sc_dev.dv_xname, rv);
140 		return;
141 	}
142 
143 	/* XXX See acpibat_notify_handler() */
144 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
145 	    ACPI_SYSTEM_NOTIFY, acpibat_notify_handler, sc);
146 	if (rv != AE_OK) {
147 		printf("%s: unable to register SYSTEM NOTIFY handler: %d\n",
148 		    sc->sc_dev.dv_xname, rv);
149 		return;
150 	}
151 	/*
152 	 * XXX poll battery in the driver for now.
153 	 * in the future, when we have an API, let userland do this polling
154 	 */
155 	callout_init(&sc->sc_callout);
156 	callout_reset(&sc->sc_callout, 60*hz, acpibat_tick, sc);
157 
158 	/* Display the current state. */
159 	sc->sc_flags = ABAT_F_VERBOSE;
160 	acpibat_get_info(sc);
161 	acpibat_get_status(sc);
162 
163 	/*
164 	 * XXX Hook into sysmon here.
165 	 */
166 }
167 
168 static void
169 acpibat_tick(void *arg)
170 {
171 	struct acpibat_softc *sc = arg;
172 	callout_reset(&sc->sc_callout, 60*hz, acpibat_tick, arg);
173 	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpibat_get_status, sc);
174 }
175 
176 
177 /*
178  * acpibat_get_info
179  *
180  * 	Get, and possibly display, the battery info.
181  */
182 
183 static void
184 acpibat_get_info(void *arg)
185 {
186 	struct acpibat_softc *sc = arg;
187 	ACPI_OBJECT *p1, *p2;
188 	ACPI_STATUS rv;
189 	ACPI_BUFFER buf;
190 
191 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "_BIF", &buf);
192 	if (rv != AE_OK) {
193 		printf("%s: failed to evaluate _BIF: %x\n",
194 		    sc->sc_dev.dv_xname, rv);
195 		return;
196 	}
197 	p1 = (ACPI_OBJECT *)buf.Pointer;
198 	if (p1->Type != ACPI_TYPE_PACKAGE) {
199 		printf("%s: expected PACKAGE, got %d\n", sc->sc_dev.dv_xname,
200 		    p1->Type);
201 		goto out;
202 	}
203 	if (p1->Package.Count < 13)
204 		printf("%s: expected 13 elts, got %d\n",
205 		    sc->sc_dev.dv_xname, p1->Package.Count);
206 
207 	p2 = p1->Package.Elements;
208 	if (p2[0].Integer.Value == 1)
209 		sc->sc_flags |= ABAT_F_PWRUNIT_MA;
210 
211 	sc->sc_design_capacity = p2[1].Integer.Value;
212 	sc->sc_pred_capacity = p2[2].Integer.Value;
213 	sc->sc_warn_capacity = p2[6].Integer.Value;
214 	sc->sc_low_capacity = p2[7].Integer.Value;
215 
216 	printf("%s: %s %s %s %s\n",
217 	    sc->sc_dev.dv_xname,
218 	    p2[12].String.Pointer, p2[11].String.Pointer,
219 	    p2[9].String.Pointer, p2[10].String.Pointer);
220 
221 	printf("%s: Design %d.%03d%s, Predicted %d.%03d%s Warn %d.%03d%s Low %d.%03d%s\n",
222 	       sc->sc_dev.dv_xname,
223 	       ACM_SCALE(sc->sc_design_capacity), ACM_CAPUNIT(sc),
224 	       ACM_SCALE(sc->sc_pred_capacity), ACM_CAPUNIT(sc),
225 	       ACM_SCALE(sc->sc_warn_capacity), ACM_CAPUNIT(sc),
226 	       ACM_SCALE(sc->sc_low_capacity), ACM_CAPUNIT(sc));
227 out:
228 	AcpiOsFree(buf.Pointer);
229 }
230 
231 /*
232  * acpibat_get_status:
233  *
234  *	Get, and possibly display, the current battery line status.
235  */
236 static void
237 acpibat_get_status(void *arg)
238 {
239 	struct acpibat_softc *sc = arg;
240 	ACPI_OBJECT *p1, *p2;
241 	ACPI_STATUS rv;
242 	ACPI_BUFFER buf;
243 
244 	buf.Pointer = sc->sc_Ret;
245 	buf.Length = sizeof(sc->sc_Ret);
246 
247 	rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "_BST", NULL, &buf);
248 	if (rv != AE_OK) {
249 		printf("bat: failed to evaluate _BST: %x\n", rv);
250 		return;
251 	}
252 	p1 = (ACPI_OBJECT *)buf.Pointer;
253 
254 	if (p1->Type != ACPI_TYPE_PACKAGE) {
255 		printf("bat: expected PACKAGE, got %d\n", p1->Type);
256 		return;
257 	}
258 	if (p1->Package.Count < 4)
259 		printf("bat: expected 4 elts, got %d\n", p1->Package.Count);
260 	p2 = p1->Package.Elements;
261 
262 	sc->sc_status = p2[0].Integer.Value;
263 	sc->sc_rate = p2[1].Integer.Value;
264 	sc->sc_capacity = p2[2].Integer.Value;
265 	sc->sc_mv = p2[3].Integer.Value;
266 
267 	if (sc->sc_flags & ABAT_F_VERBOSE) {
268 		printf("%s: %s%s: %d.%03dV cap %d.%03d%s (%d%%) rate %d.%03d%s\n",
269 		       sc->sc_dev.dv_xname,
270 		       (sc->sc_status&4) ? "CRITICAL ":"",
271 		       (sc->sc_status&1) ? "discharging" : (
272 		               (sc->sc_status & 2) ? "charging" : "idle"),
273 		       ACM_SCALE(sc->sc_mv),
274 		       ACM_SCALE(sc->sc_capacity), ACM_CAPUNIT(sc),
275 		       (sc->sc_design_capacity == 0) ? 0 :
276 			    (sc->sc_capacity * 100) / sc->sc_design_capacity,
277 		       ACM_SCALE(sc->sc_rate), ACM_RATEUNIT(sc));
278 	}
279 }
280 
281 /*
282  * acpibat_notify_handler:
283  *
284  *	Callback from ACPI interrupt handler to notify us of an event.
285  */
286 void
287 acpibat_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
288 {
289 	struct acpibat_softc *sc = context;
290 	int rv;
291 
292 	switch (notify) {
293 	case ACPI_NOTIFY_BusCheck:
294 	case ACPI_NOTIFY_BatteryStatusChanged:
295 	case ACPI_NOTIFY_BatteryInformationChanged:
296 #ifdef ACPI_BAT_DEBUG
297 		printf("%s: received notify message: 0x%x\n",
298 		    sc->sc_dev.dv_xname, notify);
299 #endif
300 		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
301 		    acpibat_get_status, sc);
302 		if (rv != AE_OK)
303 			printf("%s: unable to queue status check: %d\n",
304 			    sc->sc_dev.dv_xname, rv);
305 		break;
306 	default:
307 		printf("%s: received unknown notify message: 0x%x\n",
308 		    sc->sc_dev.dv_xname, notify);
309 	}
310 }
311