2 * Copyright (c) 1998, 2001 Nicolas Souchu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Generic I2C bit-banging code
41 * From Linux I2C generic interface
42 * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/systm.h>
49 #include <sys/module.h>
54 #include <dev/iicbus/iiconf.h>
55 #include <dev/iicbus/iicbus.h>
57 #include <dev/smbus/smbconf.h>
59 #include "iicbus_if.h"
64 int udelay; /* signal toggle delay in usec */
67 static int iicbb_attach(device_t);
68 static void iicbb_child_detached(device_t, device_t);
69 static int iicbb_detach(device_t);
70 static int iicbb_print_child(device_t, device_t);
71 static int iicbb_probe(device_t);
73 static int iicbb_callback(device_t, int, caddr_t);
74 static int iicbb_start(device_t, u_char, int);
75 static int iicbb_stop(device_t);
76 static int iicbb_write(device_t, const char *, int, int *, int);
77 static int iicbb_read(device_t, char *, int, int *, int, int);
78 static int iicbb_reset(device_t, u_char, u_char, u_char *);
79 static int iicbb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs);
81 static device_method_t iicbb_methods[] = {
82 /* device interface */
83 DEVMETHOD(device_probe, iicbb_probe),
84 DEVMETHOD(device_attach, iicbb_attach),
85 DEVMETHOD(device_detach, iicbb_detach),
88 DEVMETHOD(bus_child_detached, iicbb_child_detached),
89 DEVMETHOD(bus_print_child, iicbb_print_child),
91 /* iicbus interface */
92 DEVMETHOD(iicbus_callback, iicbb_callback),
93 DEVMETHOD(iicbus_start, iicbb_start),
94 DEVMETHOD(iicbus_repeated_start, iicbb_start),
95 DEVMETHOD(iicbus_stop, iicbb_stop),
96 DEVMETHOD(iicbus_write, iicbb_write),
97 DEVMETHOD(iicbus_read, iicbb_read),
98 DEVMETHOD(iicbus_reset, iicbb_reset),
99 DEVMETHOD(iicbus_transfer, iicbb_transfer),
104 driver_t iicbb_driver = {
107 sizeof(struct iicbb_softc),
110 devclass_t iicbb_devclass;
113 iicbb_probe(device_t dev)
115 device_set_desc(dev, "I2C bit-banging driver");
121 iicbb_attach(device_t dev)
123 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
125 sc->iicbus = device_add_child(dev, "iicbus", -1);
128 sc->udelay = 10; /* 10 uS default */
129 bus_generic_attach(dev);
135 iicbb_detach(device_t dev)
137 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
141 * We need to save child because the detach indirectly causes
142 * sc->iicbus to be zeroed. Since we added the device
143 * unconditionally in iicbb_attach, we need to make sure we
144 * delete it here. See iicbb_child_detached. We need that
145 * callback in case newbus detached our children w/o detaching
146 * us (say iicbus is a module and unloaded w/o iicbb being
150 bus_generic_detach(dev);
152 device_delete_child(dev, child);
158 iicbb_child_detached( device_t dev, device_t child )
160 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
162 if (child == sc->iicbus)
167 iicbb_print_child(device_t bus, device_t dev)
173 retval += bus_print_child_header(bus, dev);
174 /* retrieve the interface I2C address */
175 error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr);
176 if (error == IIC_ENOADDR) {
177 retval += printf(" on %s master-only\n",
178 device_get_nameunit(bus));
180 /* restore the address */
181 IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL);
183 retval += printf(" on %s addr 0x%x\n",
184 device_get_nameunit(bus), oldaddr & 0xff);
190 #define I2C_SETSDA(sc,dev,val) do { \
191 IICBB_SETSDA(device_get_parent(dev), val); \
195 #define I2C_SETSCL(dev,val) do { \
196 iicbb_setscl(dev, val, 100); \
199 #define I2C_SET(sc,dev,ctrl,data) do { \
200 I2C_SETSCL(dev, ctrl); \
201 I2C_SETSDA(sc, dev, data); \
204 #define I2C_GETSDA(dev) (IICBB_GETSDA(device_get_parent(dev)))
206 #define I2C_GETSCL(dev) (IICBB_GETSCL(device_get_parent(dev)))
208 static int i2c_debug = 0;
209 #define I2C_DEBUG(x) do { \
210 if (i2c_debug) (x); \
213 #define I2C_LOG(format,args...) do { \
214 printf(format, args); \
218 iicbb_setscl(device_t dev, int val, int timeout)
220 struct iicbb_softc *sc = device_get_softc(dev);
223 IICBB_SETSCL(device_get_parent(dev), val);
226 while (val && !I2C_GETSCL(dev) && k++ < timeout) {
227 IICBB_SETSCL(device_get_parent(dev), val);
235 iicbb_one(device_t dev, int timeout)
237 struct iicbb_softc *sc = device_get_softc(dev);
246 iicbb_zero(device_t dev, int timeout)
248 struct iicbb_softc *sc = device_get_softc(dev);
257 * Waiting for ACKNOWLEDGE.
259 * When a chip is being addressed or has received data it will issue an
260 * ACKNOWLEDGE pulse. Therefore the MASTER must release the DATA line
261 * (set it to high level) and then release the CLOCK line.
262 * Now it must wait for the SLAVE to pull the DATA line low.
263 * Actually on the bus this looks like a START condition so nothing happens
264 * because of the fact that the IC's that have not been addressed are doing
267 * When the SLAVE has pulled this line low the MASTER will take the CLOCK
268 * line low and then the SLAVE will release the SDA (data) line.
271 iicbb_ack(device_t dev, int timeout)
273 struct iicbb_softc *sc = device_get_softc(dev);
280 noack = I2C_GETSDA(dev);
285 } while (k < timeout);
288 I2C_DEBUG(printf("%c ",noack?'-':'+'));
294 iicbb_sendbyte(device_t dev, u_char data, int timeout)
298 for (i=7; i>=0; i--) {
300 iicbb_one(dev, timeout);
302 iicbb_zero(dev, timeout);
305 I2C_DEBUG(printf("w%02x",(int)data));
310 iicbb_readbyte(device_t dev, int last, int timeout)
312 struct iicbb_softc *sc = device_get_softc(dev);
314 unsigned char data=0;
325 iicbb_one(dev, timeout);
327 iicbb_zero(dev, timeout);
329 I2C_DEBUG(printf("r%02x%c ",(int)data,last?'-':'+'));
334 iicbb_callback(device_t dev, int index, caddr_t data)
336 return (IICBB_CALLBACK(device_get_parent(dev), index, data));
340 iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
342 return (IICBB_RESET(device_get_parent(dev), speed, addr, oldaddr));
346 iicbb_start(device_t dev, u_char slave, int timeout)
348 struct iicbb_softc *sc = device_get_softc(dev);
351 I2C_DEBUG(printf("<"));
358 iicbb_sendbyte(dev, slave, timeout);
361 if (iicbb_ack(dev, timeout)) {
374 iicbb_stop(device_t dev)
376 struct iicbb_softc *sc = device_get_softc(dev);
381 I2C_DEBUG(printf(">"));
382 I2C_DEBUG(printf("\n"));
387 iicbb_write(device_t dev, const char *buf, int len, int *sent, int timeout)
389 int bytes, error = 0;
394 iicbb_sendbyte(dev,(u_char)*buf++, timeout);
397 if (iicbb_ack(dev, timeout)) {
411 iicbb_read(device_t dev, char * buf, int len, int *read, int last, int delay)
417 /* XXX should insert delay here */
418 *buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0, delay);
429 iicbb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
433 error = IICBB_PRE_XFER(device_get_parent(dev));
437 error = iicbus_transfer_gen(dev, msgs, nmsgs);
439 IICBB_POST_XFER(device_get_parent(dev));
443 DRIVER_MODULE(iicbus, iicbb, iicbus_driver, iicbus_devclass, 0, 0);
445 MODULE_DEPEND(iicbb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
446 MODULE_VERSION(iicbb, IICBB_MODVER);