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