xref: /freebsd/sys/dev/iicbus/adc/ad7418.c (revision 397e83df75e0fcd0d3fcb95ae4d794cb7600fc89)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2006 Sam Leffler.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 /*
29  * Analog Devices AD7418 chip sitting on the I2C bus.
30  */
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/sysctl.h>
38 #include <sys/sx.h>
39 
40 #include <dev/iicbus/iiconf.h>
41 
42 #include "iicbus_if.h"
43 
44 #define	IIC_M_WR	0	/* write operation */
45 
46 #define	AD7418_ADDR	0x50	/* slave address */
47 
48 #define	AD7418_TEMP	0	/* Temperature Value (r/o) */
49 #define	AD7418_CONF	1	/* Config Register (r/w) */
50 #define	AD7418_CONF_SHUTDOWN	0x01
51 #define	AD7418_CONF_CHAN	0xe0	/* channel select mask */
52 #define	AD7418_CHAN_TEMP	0x00	/* temperature channel */
53 #define	AD7418_CHAN_VOLT	0x80	/* voltage channel */
54 #define	AD7418_THYST	2	/* Thyst Setpoint (r/o) */
55 #define	AD7418_TOTI	3	/* Toti Setpoint */
56 #define	AD7418_VOLT	4	/* ADC aka Voltage (r/o) */
57 #define	AD7418_CONF2	5	/* Config2 Register (r/w) */
58 
59 struct ad7418_softc {
60 	device_t	sc_dev;
61 	struct sx	sc_lock;
62 	int		sc_curchan;	/* current channel */
63 	int		sc_curtemp;
64 	int		sc_curvolt;
65 	int		sc_lastupdate;	/* in ticks */
66 };
67 
68 static void ad7418_update(struct ad7418_softc *);
69 static int ad7418_read_1(device_t dev, int reg);
70 static int ad7418_write_1(device_t dev, int reg, int v);
71 
72 static int
73 ad7418_probe(device_t dev)
74 {
75 	/* XXX really probe? */
76 	device_set_desc(dev, "Analog Devices AD7418 ADC");
77 	return (BUS_PROBE_NOWILDCARD);
78 }
79 
80 static int
81 ad7418_sysctl_temp(SYSCTL_HANDLER_ARGS)
82 {
83 	struct ad7418_softc *sc = arg1;
84 	int temp;
85 
86 	sx_xlock(&sc->sc_lock);
87 	ad7418_update(sc);
88 	temp = (sc->sc_curtemp / 64) * 25;
89 	sx_xunlock(&sc->sc_lock);
90 	return sysctl_handle_int(oidp, &temp, 0, req);
91 }
92 
93 static int
94 ad7418_sysctl_voltage(SYSCTL_HANDLER_ARGS)
95 {
96 	struct ad7418_softc *sc = arg1;
97 	int volt;
98 
99 	sx_xlock(&sc->sc_lock);
100 	ad7418_update(sc);
101 	volt = (sc->sc_curvolt >> 6) * 564 / 10;
102 	sx_xunlock(&sc->sc_lock);
103 	return sysctl_handle_int(oidp, &volt, 0, req);
104 }
105 
106 static int
107 ad7418_attach(device_t dev)
108 {
109 	struct ad7418_softc *sc = device_get_softc(dev);
110 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
111 	struct sysctl_oid *tree = device_get_sysctl_tree(dev);
112 	int conf;
113 
114 	sc->sc_dev = dev;
115 	sc->sc_lastupdate = ticks - hz;
116 
117 	sx_init(&sc->sc_lock, "ad7418");
118 
119 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
120 	    "temp", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
121 	    ad7418_sysctl_temp, "I", "operating temperature");
122 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
123 	    "volt", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
124 	    ad7418_sysctl_voltage, "I", "input voltage");
125 
126 	/* enable chip if configured in shutdown mode */
127 	conf = ad7418_read_1(dev, AD7418_CONF);
128 	if (conf >= 0 && (conf & AD7418_CONF_SHUTDOWN))
129 		ad7418_write_1(dev, AD7418_CONF, conf &~ AD7418_CONF_SHUTDOWN);
130 
131 	return (0);
132 }
133 
134 static int
135 ad7418_read_1(device_t dev, int reg)
136 {
137 	uint8_t addr = reg;
138 	uint8_t data[1];
139 	struct iic_msg msgs[2] = {
140 	     { AD7418_ADDR, IIC_M_WR, 1, &addr },
141 	     { AD7418_ADDR, IIC_M_RD, 1, data },
142 	};
143 	return iicbus_transfer(dev, msgs, 2) != 0 ? -1 : data[0];
144 }
145 
146 static int
147 ad7418_write_1(device_t dev, int reg, int v)
148 {
149 	/* NB: register pointer precedes actual data */
150 	uint8_t data[2];
151 	struct iic_msg msgs[1] = {
152 	     { AD7418_ADDR, IIC_M_WR, 2, data },
153 	};
154 	data[0] = reg;
155 	data[1] = v & 0xff;
156 	return iicbus_transfer(dev, msgs, 1);
157 }
158 
159 static void
160 ad7418_set_channel(struct ad7418_softc *sc, int chan)
161 {
162 	if (sc->sc_curchan == chan)
163 		return;
164 	ad7418_write_1(sc->sc_dev, AD7418_CONF,
165 	    (ad7418_read_1(sc->sc_dev, AD7418_CONF) &~ AD7418_CONF_CHAN)|chan);
166 	sc->sc_curchan = chan;
167 #if 0
168 	/*
169 	 * NB: Linux driver delays here but chip data sheet
170 	 *     says nothing and things appear to work fine w/o
171 	 *     a delay on channel change.
172 	 */
173 	/* let channel change settle, 1 tick should be 'nuf (need ~1ms) */
174 	tsleep(sc, 0, "ad7418", hz/1000);
175 #endif
176 }
177 
178 static int
179 ad7418_read_2(device_t dev, int reg)
180 {
181 	uint8_t addr = reg;
182 	uint8_t data[2];
183 	struct iic_msg msgs[2] = {
184 	     { AD7418_ADDR, IIC_M_WR, 1, &addr },
185 	     { AD7418_ADDR, IIC_M_RD, 2, data },
186 	};
187 	/* NB: D15..D8 precede D7..D0 per data sheet (Fig 12) */
188 	return iicbus_transfer(dev, msgs, 2) != 0 ?
189 		-1 : ((data[0] << 8) | data[1]);
190 }
191 
192 static void
193 ad7418_update(struct ad7418_softc *sc)
194 {
195 	int v;
196 
197 	sx_assert(&sc->sc_lock, SA_XLOCKED);
198 	/* NB: no point in updating any faster than the chip */
199 	if (ticks - sc->sc_lastupdate > hz) {
200 		ad7418_set_channel(sc, AD7418_CHAN_TEMP);
201 		v = ad7418_read_2(sc->sc_dev, AD7418_TEMP);
202 		if (v >= 0)
203 			sc->sc_curtemp = v;
204 		ad7418_set_channel(sc, AD7418_CHAN_VOLT);
205 		v = ad7418_read_2(sc->sc_dev, AD7418_VOLT);
206 		if (v >= 0)
207 			sc->sc_curvolt = v;
208 		sc->sc_lastupdate = ticks;
209 	}
210 }
211 
212 static device_method_t ad7418_methods[] = {
213 	DEVMETHOD(device_probe,		ad7418_probe),
214 	DEVMETHOD(device_attach,	ad7418_attach),
215 
216 	DEVMETHOD_END
217 };
218 
219 static driver_t ad7418_driver = {
220 	"ad7418",
221 	ad7418_methods,
222 	sizeof(struct ad7418_softc),
223 };
224 
225 DRIVER_MODULE(ad7418, iicbus, ad7418_driver, 0, 0);
226 MODULE_VERSION(ad7418, 1);
227 MODULE_DEPEND(ad7418, iicbus, 1, 1, 1);
228