summaryrefslogtreecommitdiff
path: root/sys/dev/acpi/acpisbs.c
blob: 7fd85860f79b9c13fa4c08ef62d172d73a24f518 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* $OpenBSD: acpisbs.c,v 1.10 2020/06/10 22:26:40 jca Exp $ */
/*
 * Smart Battery subsystem device driver
 * ACPI 5.0 spec section 10
 *
 * Copyright (c) 2016-2017 joshua stein <jcs@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * TODO: support multiple batteries based on _SBS, make sc_battery an array and
 * poll each battery independently
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>

#include <machine/apmvar.h>

#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>
#include <dev/acpi/acpidev.h>
#include <dev/acpi/amltypes.h>
#include <dev/acpi/dsdt.h>

#include <sys/sensors.h>

/* #define ACPISBS_DEBUG */

#ifdef ACPISBS_DEBUG
#define DPRINTF(x) printf x
#else
#define DPRINTF(x)
#endif

/* how often (in seconds) to re-poll data */
#define ACPISBS_POLL_FREQ	30

/* number of polls for reading data */
#define SMBUS_TIMEOUT		50

#define CHECK(kind, cmd, val, senst, sens) { \
	SMBUS_READ_##kind, SMBATT_CMD_##cmd, \
	offsetof(struct acpisbs_battery, val), \
	(SMBUS_READ_##kind == SMBUS_READ_BLOCK ? SMBUS_DATA_SIZE : 2), \
	#val, senst, sens }

struct acpisbs_battery_check {
	uint8_t	mode;
	uint8_t command;
	size_t	offset;
	int	len;
	char	*name;
	int	sensor_type;
	char	*sensor_desc;
} acpisbs_battery_checks[] = {
	/* mode must be checked first */
	CHECK(WORD, BATTERY_MODE, mode, -1,
	    "mode flags"),
	CHECK(WORD, TEMPERATURE, temperature, SENSOR_TEMP,
	    "internal temperature"),
	CHECK(WORD, VOLTAGE, voltage, SENSOR_VOLTS_DC,
	    "voltage"),
	CHECK(WORD, CURRENT, current, SENSOR_AMPS,
	    "current being supplied"),
	CHECK(WORD, AVERAGE_CURRENT, avg_current, SENSOR_AMPS,
	    "average current supplied"),
	CHECK(WORD, RELATIVE_STATE_OF_CHARGE, rel_charge, SENSOR_PERCENT,
	    "remaining capacity"),
	CHECK(WORD, ABSOLUTE_STATE_OF_CHARGE, abs_charge, SENSOR_PERCENT,
	    "remaining of design capacity"),
	CHECK(WORD, REMAINING_CAPACITY, capacity, SENSOR_AMPHOUR,
	    "remaining capacity"),
	CHECK(WORD, FULL_CHARGE_CAPACITY, full_capacity, SENSOR_AMPHOUR,
	    "capacity when fully charged"),
	CHECK(WORD, RUN_TIME_TO_EMPTY, run_time, SENSOR_INTEGER,
	    "remaining run time minutes"),
	CHECK(WORD, AVERAGE_TIME_TO_EMPTY, avg_empty_time, SENSOR_INTEGER,
	    "avg remaining minutes"),
	CHECK(WORD, AVERAGE_TIME_TO_FULL, avg_full_time, SENSOR_INTEGER,
	    "avg minutes until full charge"),
	CHECK(WORD, CHARGING_CURRENT, charge_current, SENSOR_AMPS,
	    "desired charging rate"),
	CHECK(WORD, CHARGING_VOLTAGE, charge_voltage, SENSOR_VOLTS_DC,
	    "desired charging voltage"),
	CHECK(WORD, BATTERY_STATUS, status, -1,
	    "status"),
	CHECK(WORD, CYCLE_COUNT, cycle_count, SENSOR_INTEGER,
	    "charge and discharge cycles"),
	CHECK(WORD, DESIGN_CAPACITY, design_capacity, SENSOR_AMPHOUR,
	    "capacity of new battery"),
	CHECK(WORD, DESIGN_VOLTAGE, design_voltage, SENSOR_VOLTS_DC,
	    "voltage of new battery"),
	CHECK(WORD, SERIAL_NUMBER, serial, -1,
	    "serial number"),

	CHECK(BLOCK, MANUFACTURER_NAME, manufacturer, -1,
	    "manufacturer name"),
	CHECK(BLOCK, DEVICE_NAME, device_name, -1,
	    "battery model number"),
	CHECK(BLOCK, DEVICE_CHEMISTRY, device_chemistry, -1,
	    "battery chemistry"),
#if 0
	CHECK(WORD, SPECIFICATION_INFO, spec, -1,
	    NULL),
	CHECK(WORD, MANUFACTURE_DATE, manufacture_date, -1,
	    "date battery was manufactured"),
	CHECK(BLOCK, MANUFACTURER_DATA, oem_data, -1,
	    "manufacturer-specific data"),
#endif
};

extern void acpiec_read(struct acpiec_softc *, uint8_t, int, uint8_t *);
extern void acpiec_write(struct acpiec_softc *, uint8_t, int, uint8_t *);

int	acpisbs_match(struct device *, void *, void *);
void	acpisbs_attach(struct device *, struct device *, void *);
int	acpisbs_activate(struct device *, int);
void	acpisbs_setup_sensors(struct acpisbs_softc *);
void	acpisbs_refresh_sensors(struct acpisbs_softc *);
void	acpisbs_read(struct acpisbs_softc *);
int	acpisbs_notify(struct aml_node *, int, void *);

int	acpi_smbus_read(struct acpisbs_softc *, uint8_t, uint8_t, int, void *);

const struct cfattach acpisbs_ca = {
	sizeof(struct acpisbs_softc),
	acpisbs_match,
	acpisbs_attach,
	NULL,
	acpisbs_activate,
};

struct cfdriver acpisbs_cd = {
	NULL, "acpisbs", DV_DULL
};

const char *acpisbs_hids[] = {
	ACPI_DEV_SBS,
	NULL
};

int
acpisbs_match(struct device *parent, void *match, void *aux)
{
	struct acpi_attach_args *aa = aux;
	struct cfdata *cf = match;

	return (acpi_matchhids(aa, acpisbs_hids, cf->cf_driver->cd_name));
}

void
acpisbs_attach(struct device *parent, struct device *self, void *aux)
{
	struct acpisbs_softc *sc = (struct acpisbs_softc *)self;
	struct acpi_attach_args *aa = aux;
	int64_t sbs, val;

	sc->sc_acpi = (struct acpi_softc *)parent;
	sc->sc_devnode = aa->aaa_node;
	sc->sc_batteries_present = 0;

	memset(&sc->sc_battery, 0, sizeof(sc->sc_battery));

	getmicrouptime(&sc->sc_lastpoll);

	if (aml_evalinteger(sc->sc_acpi, sc->sc_devnode, "_SBS", 0, NULL, &sbs))
		return;

	/*
	 * The parent node of the device block containing the _HID must also
	 * have an _EC node, which contains the base address and query value.
	 */
	if (aml_evalinteger(sc->sc_acpi, sc->sc_devnode->parent, "_EC", 0,
	    NULL, &val))
		return;
	sc->sc_ec_base = (val >> 8) & 0xff;

	if (!sc->sc_acpi->sc_ec)
		return;
	sc->sc_ec = sc->sc_acpi->sc_ec;

	printf(": %s", sc->sc_devnode->name);

	if (sbs > 0)
		acpisbs_read(sc);

	if (sc->sc_batteries_present) {
		if (sc->sc_battery.device_name[0])
			printf(" model \"%s\"", sc->sc_battery.device_name);
		if (sc->sc_battery.serial)
			printf(" serial %d", sc->sc_battery.serial);
		if (sc->sc_battery.device_chemistry[0])
			printf(" type %s", sc->sc_battery.device_chemistry);
		if (sc->sc_battery.manufacturer[0])
			printf(" oem \"%s\"", sc->sc_battery.manufacturer);
	}

	printf("\n");

	acpisbs_setup_sensors(sc);
	acpisbs_refresh_sensors(sc);

	/*
	 * Request notification of SCI events on the subsystem itself, but also
	 * periodically poll as a fallback in case those events never arrive.
	 */
	aml_register_notify(sc->sc_devnode->parent, aa->aaa_dev,
	    acpisbs_notify, sc, ACPIDEV_POLL);

	sc->sc_acpi->sc_havesbs = 1;
}

void
acpisbs_read(struct acpisbs_softc *sc)
{
	int i;

	for (i = 0; i < nitems(acpisbs_battery_checks); i++) {
		struct acpisbs_battery_check check = acpisbs_battery_checks[i];
		void *p = (void *)&sc->sc_battery + check.offset;

		acpi_smbus_read(sc, check.mode, check.command, check.len, p);

		if (check.mode == SMBUS_READ_BLOCK)
			DPRINTF(("%s: %s: %s\n", sc->sc_dev.dv_xname,
			    check.name, (char *)p));
		else
			DPRINTF(("%s: %s: %u\n", sc->sc_dev.dv_xname,
			    check.name, *(uint16_t *)p));

		if (check.command == SMBATT_CMD_BATTERY_MODE) {
			uint16_t *ival = (uint16_t *)p;
			if (*ival == 0) {
				/* battery not present, skip further checks */
				sc->sc_batteries_present = 0;
				break;
			}

			sc->sc_batteries_present = 1;

			if (*ival & SMBATT_BM_CAPACITY_MODE)
				sc->sc_battery.units = ACPISBS_UNITS_MW;
			else
				sc->sc_battery.units = ACPISBS_UNITS_MA;
		}
	}
}

void
acpisbs_setup_sensors(struct acpisbs_softc *sc)
{
	int i;

	memset(&sc->sc_sensordev, 0, sizeof(sc->sc_sensordev));
	strlcpy(sc->sc_sensordev.xname, DEVNAME(sc),
	    sizeof(sc->sc_sensordev.xname));

	sc->sc_sensors = mallocarray(sizeof(struct ksensor),
	    nitems(acpisbs_battery_checks), M_DEVBUF, M_WAITOK | M_ZERO);

	for (i = 0; i < nitems(acpisbs_battery_checks); i++) {
		struct acpisbs_battery_check check = acpisbs_battery_checks[i];

		if (check.sensor_type < 0)
			continue;

		strlcpy(sc->sc_sensors[i].desc, check.sensor_desc,
		    sizeof(sc->sc_sensors[i].desc));

		if (check.sensor_type == SENSOR_AMPHOUR &&
		    sc->sc_battery.units == ACPISBS_UNITS_MW)
			/* translate to watt-hours */
			sc->sc_sensors[i].type = SENSOR_WATTHOUR;
		else
			sc->sc_sensors[i].type = check.sensor_type;

		sc->sc_sensors[i].value = 0;
		sensor_attach(&sc->sc_sensordev, &sc->sc_sensors[i]);
	}

	sensordev_install(&sc->sc_sensordev);
}

void
acpisbs_refresh_sensors(struct acpisbs_softc *sc)
{
	int i;

	for (i = 0; i < nitems(acpisbs_battery_checks); i++) {
		struct acpisbs_battery_check check = acpisbs_battery_checks[i];
		void *p = (void *)&sc->sc_battery + check.offset;
		uint16_t *ival = (uint16_t *)p;

		if (check.sensor_type < 0)
			continue;

		if (sc->sc_batteries_present) {
			sc->sc_sensors[i].flags = 0;
			sc->sc_sensors[i].status = SENSOR_S_OK;

			switch (check.sensor_type) {
			case SENSOR_AMPS:
				sc->sc_sensors[i].value = *ival * 100;
				break;

			case SENSOR_AMPHOUR:
			case SENSOR_WATTHOUR:
				sc->sc_sensors[i].value = *ival * 10000;
				break;

			case SENSOR_PERCENT:
				sc->sc_sensors[i].value = *ival * 1000;
				break;

#if 0
			case SENSOR_STRING:
				strlcpy(sc->sc_sensors[i].string, (char *)p,
				    sizeof(sc->sc_sensors[i].string));
				break;
#endif
			case SENSOR_TEMP:
				/* .1 degK */
				sc->sc_sensors[i].value = (*ival * 10000) +
				    273150000;
				break;

			case SENSOR_VOLTS_DC:
				sc->sc_sensors[i].value = *ival * 1000;
				break;

			default:
				if (*ival == ACPISBS_VALUE_UNKNOWN) {
					sc->sc_sensors[i].value = 0;
					sc->sc_sensors[i].status =
					    SENSOR_S_UNKNOWN;
					sc->sc_sensors[i].flags =
					    SENSOR_FUNKNOWN;
				} else
					sc->sc_sensors[i].value = *ival;
			}
		} else {
			sc->sc_sensors[i].value = 0;
			sc->sc_sensors[i].status = SENSOR_S_UNKNOWN;
			sc->sc_sensors[i].flags = SENSOR_FUNKNOWN;
		}
	}
}

int
acpisbs_activate(struct device *self, int act)
{
	struct acpisbs_softc *sc = (struct acpisbs_softc *)self;

	switch (act) {
	case DVACT_WAKEUP:
		acpisbs_read(sc);
		acpisbs_refresh_sensors(sc);
		break;
	}

	return 0;
}

int
acpisbs_notify(struct aml_node *node, int notify_type, void *arg)
{
	struct acpisbs_softc *sc = arg;
	struct timeval diff, now;

	DPRINTF(("%s: %s: %d\n", sc->sc_dev.dv_xname, __func__, notify_type));

	getmicrouptime(&now);

	switch (notify_type) {
	case 0x00:
		/* fallback poll */
	case 0x80:
		/*
		 * EC SCI will come for every data point, so only run once in a
		 * while
		 */
		timersub(&now, &sc->sc_lastpoll, &diff);
		if (diff.tv_sec > ACPISBS_POLL_FREQ) {
			acpisbs_read(sc);
			acpisbs_refresh_sensors(sc);
			acpi_record_event(sc->sc_acpi, APM_POWER_CHANGE);
			getmicrouptime(&sc->sc_lastpoll);
		}
		break;
	default:
		break;
	}

	return 0;
}

int
acpi_smbus_read(struct acpisbs_softc *sc, uint8_t type, uint8_t cmd, int len,
    void *buf)
{
	int j;
	uint8_t addr = SMBATT_ADDRESS;
	uint8_t val;

	acpiec_write(sc->sc_ec, sc->sc_ec_base + SMBUS_ADDR, 1, &addr);
	acpiec_write(sc->sc_ec, sc->sc_ec_base + SMBUS_CMD, 1, &cmd);
	acpiec_write(sc->sc_ec, sc->sc_ec_base + SMBUS_PRTCL, 1, &type);

	for (j = SMBUS_TIMEOUT; j > 0; j--) {
		acpiec_read(sc->sc_ec, sc->sc_ec_base + SMBUS_PRTCL, 1, &val);
		if (val == 0)
			break;
	}
	if (j == 0) {
		printf("%s: %s: timeout reading 0x%x\n", sc->sc_dev.dv_xname,
		    __func__, addr);
		return 1;
	}

	acpiec_read(sc->sc_ec, sc->sc_ec_base + SMBUS_STS, 1, &val);
	if (val & SMBUS_STS_MASK) {
		printf("%s: %s: error reading status: 0x%x\n",
		    sc->sc_dev.dv_xname, __func__, addr);
		return 1;
	}

	switch (type) {
        case SMBUS_READ_WORD: {
		uint8_t word[2];
		acpiec_read(sc->sc_ec, sc->sc_ec_base + SMBUS_DATA, 2,
		    (uint8_t *)&word);

		*(uint16_t *)buf = (word[1] << 8) | word[0];

		break;
	}
	case SMBUS_READ_BLOCK:
		bzero(buf, len);

		/* find number of bytes to read */
		acpiec_read(sc->sc_ec, sc->sc_ec_base + SMBUS_BCNT, 1, &val);
		val &= 0x1f;
		if (len > val)
			len = val;

		for (j = 0; j < len; j++) {
			acpiec_read(sc->sc_ec, sc->sc_ec_base + SMBUS_DATA + j,
			    1, &val);
			((char *)buf)[j] = val;
		}
		break;
	default:
		printf("%s: %s: unknown mode 0x%x\n", sc->sc_dev.dv_xname,
		    __func__, type);
		return 1;
	}

	return 0;
}