summaryrefslogtreecommitdiff
path: root/sys/dev/acpi/ccpmic.c
blob: 396ac7c0255b52b60c58cacc8d36ac774ed6448e (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
/*	$OpenBSD: ccpmic.c,v 1.3 2022/04/06 18:59:27 naddy Exp $	*/
/*
 * Copyright (c) 2018 Mark Kettenis <kettenis@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.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/malloc.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 <dev/i2c/i2cvar.h>

#define CCPMIC_GPIO0P0CTLO		0x2b
#define CCPMIC_GPIO0P0CTLI		0x33
#define CCPMIC_GPIO1P0CTLO		0x3b
#define CCPMIC_GPIO1P0CTLI		0x43
#define CCPMIC_GPIOPANELCTL		0x52
#define  CCPMIC_GPIOCTLO_RVAL_2KUP	(1 << 1)
#define  CCPMIC_GPIOCTLO_DRV_REN	(1 << 3)
#define  CCPMIC_GPIOCTLO_DIR_OUT	(1 << 5)
#define  CCPMIC_GPIOCTLI_VALUE		(1 << 0)

#define CCPMIC_GPIOCTLO_INPUT \
    (CCPMIC_GPIOCTLO_DRV_REN | CCPMIC_GPIOCTLO_RVAL_2KUP)
#define CCPMIC_GPIOCTLO_OUTPUT \
    (CCPMIC_GPIOCTLO_INPUT | CCPMIC_GPIOCTLO_DIR_OUT)

#define CCPMIC_V1P8SX			0x5d
#define CCPMIC_V1P2SX			0x61
#define CCPMIC_V2P85SX			0x66
#define  CCPMIC_PWR_ON			(1 << 0)
#define  CCPMIC_PWR_SEL			(1 << 1)
#define CCPMIC_SYS2_THRM_RSLT_H		0x78
#define CCPMIC_SYS2_THRM_RSLT_L		0x79

#define CCPMIC_REGIONSPACE_THERMAL	0x8c
#define CCPMIC_REGIONSPACE_POWER	0x8d

#define CCPMIC_NPINS			16

struct acpi_lpat {
	int32_t temp;
	int32_t raw;
};

struct ccpmic_softc {
	struct device	sc_dev;
	struct acpi_softc *sc_acpi;
	struct aml_node *sc_node;
	i2c_tag_t	sc_tag;
	i2c_addr_t	sc_addr;

	struct acpi_lpat *sc_lpat;
	size_t		sc_lpat_len;

	struct acpi_gpio sc_gpio;
};

int	ccpmic_match(struct device *, void *, void *);
void	ccpmic_attach(struct device *, struct device *, void *);

const struct cfattach ccpmic_ca = {
	sizeof(struct ccpmic_softc), ccpmic_match, ccpmic_attach
};

struct cfdriver ccpmic_cd = {
	NULL, "ccpmic", DV_DULL
};

uint8_t	ccpmic_read_1(struct ccpmic_softc *, uint8_t, int);
void	ccpmic_write_1(struct ccpmic_softc *, uint8_t, uint8_t, int);
void	ccpmic_get_lpat(struct ccpmic_softc *);
int32_t	ccpmic_raw_to_temp(struct ccpmic_softc *, int32_t);
int	ccpmic_thermal_opreg_handler(void *, int, uint64_t, int, uint64_t *);
int	ccpmic_power_opreg_handler(void *, int, uint64_t, int, uint64_t *);
int	ccpmic_read_pin(void *, int);
void	ccpmic_write_pin(void *, int, int);

int
ccpmic_match(struct device *parent, void *match, void *aux)
{
	struct i2c_attach_args *ia = aux;

	return (strcmp(ia->ia_name, "INT33FD") == 0);
}

void
ccpmic_attach(struct device *parent, struct device *self, void *aux)
{
	struct ccpmic_softc *sc = (struct ccpmic_softc *)self;
	struct i2c_attach_args *ia = aux;

	sc->sc_tag = ia->ia_tag;
	sc->sc_addr = ia->ia_addr;
	sc->sc_acpi = acpi_softc;
	sc->sc_node = ia->ia_cookie;

	printf("\n");

	ccpmic_get_lpat(sc);
	if (sc->sc_lpat == NULL)
		return;

	sc->sc_gpio.cookie = sc;
	sc->sc_gpio.read_pin = ccpmic_read_pin;
	sc->sc_gpio.write_pin = ccpmic_write_pin;
	sc->sc_node->gpio = &sc->sc_gpio;
	acpi_register_gpio(sc->sc_acpi, sc->sc_node);

	/* Register OEM defined address space. */
	aml_register_regionspace(sc->sc_node, CCPMIC_REGIONSPACE_THERMAL,
	    sc, ccpmic_thermal_opreg_handler);
	aml_register_regionspace(sc->sc_node, CCPMIC_REGIONSPACE_POWER,
	    sc, ccpmic_power_opreg_handler);
}

uint8_t
ccpmic_read_1(struct ccpmic_softc *sc, uint8_t reg, int flags)
{
	uint8_t val;
	int error;

	iic_acquire_bus(sc->sc_tag, flags);
	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
	    &reg, sizeof(reg), &val, sizeof(val), flags);
	iic_release_bus(sc->sc_tag, flags);

	if (error) {
		printf("%s: can't read register 0x%02x\n",
		    sc->sc_dev.dv_xname, reg);
		val = 0xff;
	}

	return val;
}

void
ccpmic_write_1(struct ccpmic_softc *sc, uint8_t reg, uint8_t val, int flags)
{
	int error;

	iic_acquire_bus(sc->sc_tag, flags);
	error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
	    &reg, sizeof(reg), &val, sizeof(val), flags);
	iic_release_bus(sc->sc_tag, flags);

	if (error) {
		printf("%s: can't write register 0x%02x\n",
		    sc->sc_dev.dv_xname, reg);
	}
}

void
ccpmic_get_lpat(struct ccpmic_softc *sc)
{
	struct aml_value res;
	int i;

	if (aml_evalname(sc->sc_acpi, sc->sc_node, "LPAT", 0, NULL, &res))
		return;
	if (res.type != AML_OBJTYPE_PACKAGE)
		goto out;
	if (res.length < 4 || (res.length % 2) != 0)
		goto out;

	sc->sc_lpat_len = res.length / 2;
	sc->sc_lpat = mallocarray(sc->sc_lpat_len, sizeof(struct acpi_lpat),
	    M_DEVBUF, M_WAITOK);

	for (i = 0; i < sc->sc_lpat_len; i++) {
		sc->sc_lpat[i].temp = aml_val2int(res.v_package[2 * i]);
		sc->sc_lpat[i].raw = aml_val2int(res.v_package[2 * i + 1]);
	}

out:
	aml_freevalue(&res);
}

int32_t
ccpmic_raw_to_temp(struct ccpmic_softc *sc, int32_t raw)
{
	struct acpi_lpat *lpat = sc->sc_lpat;
	int32_t raw0, delta_raw;
	int32_t temp0, delta_temp;
	int i;

	for (i = 1; i < sc->sc_lpat_len; i++) {
		/* Coefficient can be positive or negative. */
		if (raw >= lpat[i - 1].raw && raw <= lpat[i].raw)
			break;
		if (raw <= lpat[i - 1].raw && raw >= lpat[i].raw)
			break;
	}
	if (i == sc->sc_lpat_len)
		return -1;

	raw0 = lpat[i - 1].raw;
	temp0 = lpat[i - 1].temp;
	delta_raw = lpat[i].raw - raw0;
	delta_temp = lpat[i].temp - temp0;

	return temp0 + (raw - raw0) * delta_temp / delta_raw;
}

struct ccpmic_regmap {
	uint8_t address;
	uint8_t hi, lo;
};

struct ccpmic_regmap ccpmic_thermal_regmap[] = {
	{ 0x18, CCPMIC_SYS2_THRM_RSLT_H, CCPMIC_SYS2_THRM_RSLT_L }, /* TMP2 */
};

int
ccpmic_thermal_opreg_handler(void *cookie, int iodir, uint64_t address,
    int size, uint64_t *value)
{
	struct ccpmic_softc *sc = cookie;
	int32_t temp;
	uint16_t raw;
	uint8_t lo, hi;
	int i;

	/* Only allow 32-bit read access. */
	if (size != 4 || iodir != ACPI_IOREAD)
		return -1;

	for (i = 0; i < nitems(ccpmic_thermal_regmap); i++) {
		if (address == ccpmic_thermal_regmap[i].address)
			break;
	}
	if (i == nitems(ccpmic_thermal_regmap)) {
		printf("%s: addr 0x%02llx\n", __func__, address);
		return -1;
	}

	lo = ccpmic_thermal_regmap[i].lo;
	hi = ccpmic_thermal_regmap[i].hi;
	raw = ccpmic_read_1(sc, lo, 0);
	raw |= (ccpmic_read_1(sc, hi, 0) & 0x03) << 8;

	temp = ccpmic_raw_to_temp(sc, raw);
	if (temp < 0)
		return -1;

	*value = temp;
	return 0;
}

struct ccpmic_regmap ccpmic_power_regmap[] = {
	{ 0x24, CCPMIC_V2P85SX },	/* X285 */
	{ 0x48, CCPMIC_V1P8SX },	/* V18X */
	{ 0x50, CCPMIC_V1P2SX },	/* V12X */
};

int
ccpmic_power_opreg_handler(void *cookie, int iodir, uint64_t address,
    int size, uint64_t *value)
{
	struct ccpmic_softc *sc = cookie;
	uint8_t reg, val;
	int i;

	/* Only allow 32-bit access. */
	if (size != 4)
		return -1;

	for (i = 0; i < nitems(ccpmic_power_regmap); i++) {
		if (address == ccpmic_power_regmap[i].address)
			break;
	}
	if (i == nitems(ccpmic_power_regmap)) {
		printf("%s: addr 0x%02llx\n", __func__, address);
		return -1;
	}

	reg = ccpmic_power_regmap[i].hi;
	val = ccpmic_read_1(sc, reg, 0);
	if (iodir == ACPI_IOREAD) {
		if ((val & CCPMIC_PWR_SEL) && (val & CCPMIC_PWR_ON))
			*value = 1;
		else
			*value = 0;
	} else {
		if (*value)
			val |= CCPMIC_PWR_ON;
		else
			val &= ~CCPMIC_PWR_ON;
		ccpmic_write_1(sc, reg, val | CCPMIC_PWR_SEL, 0);
	}

	return 0;
}

/* 
 * We have 16 real GPIOs and a bunch of virtual ones.  The virtual
 * ones are mostly there to deal with a limitation of Microsoft
 * Windows.  We only implement the "panel" control GPIO, which
 * actually maps onto a real GPIO.
 */

int
ccpmic_read_pin(void *cookie, int pin)
{
	struct ccpmic_softc *sc = cookie;
	uint8_t reg;

	if (pin >= CCPMIC_NPINS)
		return 0;

	reg = ((pin < 8) ? CCPMIC_GPIO0P0CTLO : CCPMIC_GPIO1P0CTLO) + pin % 8;
	ccpmic_write_1(sc, reg, CCPMIC_GPIOCTLO_INPUT, 0);
	reg = ((pin < 8) ? CCPMIC_GPIO0P0CTLI : CCPMIC_GPIO1P0CTLI) + pin % 8;
	return ccpmic_read_1(sc, reg, 0) & CCPMIC_GPIOCTLI_VALUE;
}

void
ccpmic_write_pin(void *cookie, int pin, int value)
{
	struct ccpmic_softc *sc = cookie;
	uint8_t reg;

	if (pin == 0x5e) {
		reg = CCPMIC_GPIOPANELCTL;
		ccpmic_write_1(sc, reg, CCPMIC_GPIOCTLO_OUTPUT | !!value, 0);
		return;
	}

	if (pin >= CCPMIC_NPINS)
		return;

	reg = ((pin < 8) ? CCPMIC_GPIO0P0CTLO : CCPMIC_GPIO1P0CTLO) + pin % 8;
	ccpmic_write_1(sc, reg, CCPMIC_GPIOCTLO_OUTPUT | !!value, 0);
}