adadc.c revision 1.11
1/* $NetBSD: adadc.c,v 1.11 2025/06/30 10:16:01 macallan Exp $ */
2
3/*-
4 * Copyright (c) 2018 Michael Lorenz
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 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/*
30 * a driver for Analog Devices AD7417 temperature sensors / ADCs
31 * very much macppc only for now since we need calibaration data to make sense
32 * of the ADC inputs
33 * info on how to get these from FreeBSD and Linux
34 */
35
36#include <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: adadc.c,v 1.11 2025/06/30 10:16:01 macallan Exp $");
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/device.h>
42#include <sys/conf.h>
43#include <sys/bus.h>
44
45#include <dev/i2c/i2cvar.h>
46
47#include <dev/sysmon/sysmonvar.h>
48
49#include <dev/ofw/openfirm.h>
50
51/* commands */
52#define ADADC_TEMP	0x00	/* temperature, 16bit */
53#define ADADC_CONFIG	0x01	/* 8bit */
54#define ADADC_THYST	0x02	/* 16bit */
55#define ADADC_TOTI	0x03	/* 16bit, temperature threshold */
56#define ADADC_ADC	0x04	/* 16bit, ADC value */
57#define ADADC_CONFIG2	0x05	/* 8bit */
58
59/*
60 * registers
61 * ADADC_TEMP has signed temperature in C in first byte, left aligned fraction
62 * in 2nd byte.
63 */
64
65#define ADADC_CFG_CHANNEL_0	0x00
66#define ADADC_CFG_CHANNEL_1	0x20
67#define ADADC_CFG_CHANNEL_2	0x40
68#define ADADC_CFG_CHANNEL_3	0x60
69#define ADADC_CFG_CHANNEL_4	0x80
70#define ADADC_CFG_CHANNEL_MASK	0xe0
71#define ADADC_CFG_FAULT_MASK	0x18
72#define ADADC_CFG_OTI_POL	0x04	/* overtemp output polarity */
73#define ADADC_CFG_INTMODE	0x02	/* interrupt mode */
74#define ADADC_CFG_SHUTDOWN	0x01	/* shutdown mode */
75
76
77struct adadc_softc {
78	device_t	sc_dev;
79	i2c_tag_t	sc_i2c;
80	i2c_addr_t	sc_addr;
81
82	struct sysmon_envsys *sc_sme;
83	envsys_data_t	sc_sensors[5];
84	int		sc_nsensors;
85	int		sc_diode_offset, sc_diode_slope;
86};
87
88static int	adadc_match(device_t, cfdata_t, void *);
89static void	adadc_attach(device_t, device_t, void *);
90
91static void	adadc_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
92
93CFATTACH_DECL_NEW(adadc, sizeof(struct adadc_softc),
94    adadc_match, adadc_attach, NULL, NULL);
95
96static const struct device_compatible_entry compat_data[] = {
97	{ .compat = "ad7417" },
98	DEVICE_COMPAT_EOL
99};
100
101/* calibaration table from Darwin via Linux */
102static int slope[5] = {0, 0, 0x0320, 0x00a0, 0x1f40};
103
104static int
105adadc_match(device_t parent, cfdata_t match, void *aux)
106{
107	struct i2c_attach_args *ia = aux;
108	int match_result;
109
110	if (iic_use_direct_match(ia, match, compat_data, &match_result))
111		return match_result;
112
113	/*
114	 * XXX
115	 * this driver is pretty much useless without OF, should
116	 * probably remove this
117	 */
118	if ((ia->ia_addr & 0x2b) == 0x2b)
119		return I2C_MATCH_ADDRESS_ONLY;
120
121	return 0;
122}
123
124static void
125adadc_attach(device_t parent, device_t self, void *aux)
126{
127	struct adadc_softc *sc = device_private(self);
128	struct i2c_attach_args *ia = aux;
129	envsys_data_t *s;
130	int error, ch;
131	uint32_t eeprom[40];
132	char loc[256];
133	int which_cpu;
134
135	sc->sc_dev = self;
136	sc->sc_i2c = ia->ia_tag;
137	sc->sc_addr = ia->ia_addr;
138
139	aprint_naive("\n");
140	aprint_normal(": AD7417\n");
141
142	sc->sc_sme = sysmon_envsys_create();
143	sc->sc_sme->sme_name = device_xname(self);
144	sc->sc_sme->sme_cookie = sc;
145	sc->sc_sme->sme_refresh = adadc_sensors_refresh;
146	sc->sc_nsensors = 0;
147
148	/*
149	 * XXX
150	 * without OpenFirmware telling us how to interpret the ADC inputs we
151	 * should probably just expose the temperature and four ENVSYS_INTEGERs
152	 */
153	which_cpu = 0;
154	ch = OF_child(ia->ia_cookie);
155	if (ch == 0) {
156		/* old style info */
157		int len, idx = 0, reg = 0;
158		uint32_t ids[16];
159		char buffer[256];
160		len = OF_getprop(ia->ia_cookie, "hwsensor-id", ids, 64);
161		OF_getprop(ia->ia_cookie, "hwsensor-location", buffer, 256);
162		while (len > 0) {
163			strcpy(loc, &buffer[idx]);
164			s = &sc->sc_sensors[sc->sc_nsensors];
165			s->state = ENVSYS_SINVALID;
166			/*
167			 * this setup matches my 1.8GHz PCI-X G5, Linux and
168			 * FreeBSD hardcode these as well so we should be safe
169			 */
170			switch (reg) {
171				case 0:
172					if (strstr(loc, "CPU B") != NULL)
173						which_cpu = 1;
174					/* FALLTHROUGH */
175				case 1:
176					s->units = ENVSYS_STEMP;
177					break;
178				case 2:
179				case 4:
180					s->units = ENVSYS_SAMPS;
181					break;
182				case 3:
183					s->units = ENVSYS_SVOLTS_DC;
184					break;
185				default:
186					s->units = ENVSYS_INTEGER;
187			}
188			strncpy(s->desc, loc, sizeof(s->desc));
189			s->private = reg;
190			sysmon_envsys_sensor_attach(sc->sc_sme, s);
191			sc->sc_nsensors++;
192			len -= 4;
193			idx += strlen(loc) + 1;
194			reg++;
195		}
196	} else {
197		while (ch != 0) {
198			if (OF_getprop(ch, "location", loc, 32) > 0) {
199				int reg = 0;
200				OF_getprop(ch, "reg", &reg, sizeof(reg));
201				s = &sc->sc_sensors[sc->sc_nsensors];
202				s->state = ENVSYS_SINVALID;
203				/*
204				 * this setup matches my 2x 2.5GHz PCI-X G5, Linux and
205				 * FreeBSD hardcode these as well so we should be safe
206				 */
207				switch (reg) {
208				case 0:
209					if (strstr(loc, "CPU B") != NULL)
210						which_cpu = 1;
211					/* FALLTHROUGH */
212				case 1:
213					s->units = ENVSYS_STEMP;
214					break;
215				case 2:
216				case 4:
217					s->units = ENVSYS_SAMPS;
218					break;
219				case 3:
220					s->units = ENVSYS_SVOLTS_DC;
221					break;
222				default:
223					s->units = ENVSYS_INTEGER;
224				}
225				strncpy(s->desc, loc, sizeof(s->desc));
226				s->private = reg;
227				sysmon_envsys_sensor_attach(sc->sc_sme, s);
228				sc->sc_nsensors++;
229			}
230			ch = OF_peer(ch);
231		}
232	}
233	aprint_debug_dev(self, "monitoring CPU %d\n", which_cpu);
234	error = get_cpuid(which_cpu, (uint8_t *)eeprom);
235	if (error >= 0) {
236		sc->sc_diode_slope = eeprom[0x11] >> 16;
237		sc->sc_diode_offset = (int16_t)(eeprom[0x11] & 0xffff) << 12;
238	}
239	sysmon_envsys_register(sc->sc_sme);
240}
241
242static void
243adadc_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
244{
245	struct adadc_softc *sc = sme->sme_cookie;
246	uint8_t cmd = ADADC_CONFIG;
247	int16_t data = 0;
248	uint16_t rdata;
249	uint8_t cfg;
250	int error, ch;
251
252
253	iic_acquire_bus(sc->sc_i2c, 0);
254	if (edata->private > 0) {
255		error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
256		    sc->sc_addr, &cmd, 1, &cfg, 1, 0);
257		ch = cfg >> 5;
258		/* are we on the right channel already? */
259		if (ch != edata->private) {
260			/* nope */
261			cfg &= 0x1f;
262			cfg |= (edata->private & 7) << 5;
263			iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
264			    sc->sc_addr, &cmd, 1, &cfg, 1, 0);
265		}
266		cmd = ADADC_ADC;
267		/* now read the ADC register */
268		error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
269		    sc->sc_addr, &cmd, 1, &rdata, 2, 0);
270		rdata = be16toh(rdata) >> 6;
271		if (edata->private == 1) {
272			int temp;
273			temp = (rdata * sc->sc_diode_slope +
274				sc->sc_diode_offset) >> 2;
275			/* 16.16 fixed point */
276			edata->value_cur = (temp >> 12) * 62500 + 273150000;
277		} else {
278			/*
279			 * the input is 10bit, so converting to 8.4 fixed point
280			 * is more than enough
281			 */
282			int temp = rdata * slope[edata->private];
283			edata->value_cur = (temp >> 12) * 62500;
284		}
285	} else {
286		cmd = ADADC_TEMP;
287		/* just read the temperature register */
288		error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
289		    sc->sc_addr, &cmd, 1, &data, 2, 0);
290		/* 8.2 bit fixed point Celsius -> microkelvin */
291		edata->value_cur = ((data >> 6) * 250000) + 273150000;
292	}
293	iic_release_bus(sc->sc_i2c, 0);
294
295	if (error) {
296		edata->state = ENVSYS_SINVALID;
297	} else {
298		edata->state = ENVSYS_SVALID;
299	}
300}
301