]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/iicbus/ad7418.c
Upgrade to OpenSSH 7.8p1.
[FreeBSD/FreeBSD.git] / sys / dev / iicbus / ad7418.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
29 /*
30  * Analog Devices AD7418 chip sitting on the I2C bus.
31  */
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/resource.h>
39 #include <sys/rman.h>
40 #include <sys/sysctl.h>
41 #include <sys/sx.h>
42
43 #include <machine/bus.h>
44 #include <machine/cpu.h>
45 #include <machine/cpufunc.h>
46 #include <machine/frame.h>
47 #include <machine/resource.h>
48 #include <machine/intr.h>
49
50 #include <dev/iicbus/iiconf.h>
51
52 #include "iicbus_if.h"
53
54 #define IIC_M_WR        0       /* write operation */
55
56 #define AD7418_ADDR     0x50    /* slave address */
57
58 #define AD7418_TEMP     0       /* Temperature Value (r/o) */
59 #define AD7418_CONF     1       /* Config Register (r/w) */
60 #define AD7418_CONF_SHUTDOWN    0x01
61 #define AD7418_CONF_CHAN        0xe0    /* channel select mask */
62 #define AD7418_CHAN_TEMP        0x00    /* temperature channel */
63 #define AD7418_CHAN_VOLT        0x80    /* voltage channel */
64 #define AD7418_THYST    2       /* Thyst Setpoint (r/o) */
65 #define AD7418_TOTI     3       /* Toti Setpoint */
66 #define AD7418_VOLT     4       /* ADC aka Voltage (r/o) */
67 #define AD7418_CONF2    5       /* Config2 Register (r/w) */
68
69 struct ad7418_softc {
70         device_t        sc_dev;
71         struct sx       sc_lock;
72         int             sc_curchan;     /* current channel */
73         int             sc_curtemp;
74         int             sc_curvolt;
75         int             sc_lastupdate;  /* in ticks */
76 };
77
78 static void ad7418_update(struct ad7418_softc *);
79 static int ad7418_read_1(device_t dev, int reg);
80 static int ad7418_write_1(device_t dev, int reg, int v);
81
82 static int
83 ad7418_probe(device_t dev)
84 {
85         /* XXX really probe? */
86         device_set_desc(dev, "Analog Devices AD7418 ADC");
87         return (BUS_PROBE_NOWILDCARD);
88 }
89
90 static int
91 ad7418_sysctl_temp(SYSCTL_HANDLER_ARGS)
92 {
93         struct ad7418_softc *sc = arg1;
94         int temp;
95
96         sx_xlock(&sc->sc_lock);
97         ad7418_update(sc);
98         temp = (sc->sc_curtemp / 64) * 25;
99         sx_xunlock(&sc->sc_lock);
100         return sysctl_handle_int(oidp, &temp, 0, req);
101 }
102
103 static int
104 ad7418_sysctl_voltage(SYSCTL_HANDLER_ARGS)
105 {
106         struct ad7418_softc *sc = arg1;
107         int volt;
108
109         sx_xlock(&sc->sc_lock);
110         ad7418_update(sc);
111         volt = (sc->sc_curvolt >> 6) * 564 / 10;
112         sx_xunlock(&sc->sc_lock);
113         return sysctl_handle_int(oidp, &volt, 0, req);
114 }
115
116 static int
117 ad7418_attach(device_t dev)
118 {
119         struct ad7418_softc *sc = device_get_softc(dev);
120         struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
121         struct sysctl_oid *tree = device_get_sysctl_tree(dev);
122         int conf;
123
124         sc->sc_dev = dev;
125         sc->sc_lastupdate = ticks - hz;
126
127         sx_init(&sc->sc_lock, "ad7418");
128
129         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
130                 "temp", CTLTYPE_INT | CTLFLAG_RD, sc, 0,
131                 ad7418_sysctl_temp, "I", "operating temperature");
132         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
133                 "volt", CTLTYPE_INT | CTLFLAG_RD, sc, 0,
134                 ad7418_sysctl_voltage, "I", "input voltage");
135
136         /* enable chip if configured in shutdown mode */
137         conf = ad7418_read_1(dev, AD7418_CONF);
138         if (conf >= 0 && (conf & AD7418_CONF_SHUTDOWN))
139                 ad7418_write_1(dev, AD7418_CONF, conf &~ AD7418_CONF_SHUTDOWN);
140
141         return (0);
142 }
143
144 static int
145 ad7418_read_1(device_t dev, int reg) 
146 {
147         uint8_t addr = reg;
148         uint8_t data[1];
149         struct iic_msg msgs[2] = {
150              { AD7418_ADDR, IIC_M_WR, 1, &addr },
151              { AD7418_ADDR, IIC_M_RD, 1, data },
152         };
153         return iicbus_transfer(dev, msgs, 2) != 0 ? -1 : data[0];
154 }
155
156 static int
157 ad7418_write_1(device_t dev, int reg, int v) 
158 {
159         /* NB: register pointer precedes actual data */
160         uint8_t data[2];
161         struct iic_msg msgs[1] = {
162              { AD7418_ADDR, IIC_M_WR, 2, data },
163         };
164         data[0] = reg;
165         data[1] = v & 0xff;
166         return iicbus_transfer(dev, msgs, 1);
167 }
168
169 static void
170 ad7418_set_channel(struct ad7418_softc *sc, int chan)
171 {
172         if (sc->sc_curchan == chan)
173                 return;
174         ad7418_write_1(sc->sc_dev, AD7418_CONF, 
175             (ad7418_read_1(sc->sc_dev, AD7418_CONF) &~ AD7418_CONF_CHAN)|chan);
176         sc->sc_curchan = chan;
177 #if 0
178         /*
179          * NB: Linux driver delays here but chip data sheet
180          *     says nothing and things appear to work fine w/o
181          *     a delay on channel change.
182          */
183         /* let channel change settle, 1 tick should be 'nuf (need ~1ms) */
184         tsleep(sc, 0, "ad7418", hz/1000);
185 #endif
186 }
187
188 static int
189 ad7418_read_2(device_t dev, int reg) 
190 {
191         uint8_t addr = reg;
192         uint8_t data[2];
193         struct iic_msg msgs[2] = {
194              { AD7418_ADDR, IIC_M_WR, 1, &addr },
195              { AD7418_ADDR, IIC_M_RD, 2, data },
196         };
197         /* NB: D15..D8 precede D7..D0 per data sheet (Fig 12) */
198         return iicbus_transfer(dev, msgs, 2) != 0 ?
199                 -1 : ((data[0] << 8) | data[1]);
200 }
201
202 static void
203 ad7418_update(struct ad7418_softc *sc)
204 {
205         int v;
206
207         sx_assert(&sc->sc_lock, SA_XLOCKED);
208         /* NB: no point in updating any faster than the chip */
209         if (ticks - sc->sc_lastupdate > hz) {
210                 ad7418_set_channel(sc, AD7418_CHAN_TEMP);
211                 v = ad7418_read_2(sc->sc_dev, AD7418_TEMP);
212                 if (v >= 0)
213                         sc->sc_curtemp = v;
214                 ad7418_set_channel(sc, AD7418_CHAN_VOLT);
215                 v = ad7418_read_2(sc->sc_dev, AD7418_VOLT);
216                 if (v >= 0)
217                         sc->sc_curvolt = v;
218                 sc->sc_lastupdate = ticks;
219         }
220 }
221
222 static device_method_t ad7418_methods[] = {
223         DEVMETHOD(device_probe,         ad7418_probe),
224         DEVMETHOD(device_attach,        ad7418_attach),
225
226         DEVMETHOD_END
227 };
228
229 static driver_t ad7418_driver = {
230         "ad7418",
231         ad7418_methods,
232         sizeof(struct ad7418_softc),
233 };
234 static devclass_t ad7418_devclass;
235
236 DRIVER_MODULE(ad7418, iicbus, ad7418_driver, ad7418_devclass, 0, 0);
237 MODULE_VERSION(ad7418, 1);
238 MODULE_DEPEND(ad7418, iicbus, 1, 1, 1);