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"
66 static int iicbb_probe(device_t);
67 static int iicbb_attach(device_t);
68 static int iicbb_detach(device_t);
69 static int iicbb_print_child(device_t, device_t);
71 static int iicbb_callback(device_t, int, caddr_t);
72 static int iicbb_start(device_t, u_char, int);
73 static int iicbb_stop(device_t);
74 static int iicbb_write(device_t, char *, int, int *, int);
75 static int iicbb_read(device_t, char *, int, int *, int, int);
76 static int iicbb_reset(device_t, u_char, u_char, u_char *);
78 static device_method_t iicbb_methods[] = {
79 /* device interface */
80 DEVMETHOD(device_probe, iicbb_probe),
81 DEVMETHOD(device_attach, iicbb_attach),
82 DEVMETHOD(device_detach, iicbb_detach),
85 DEVMETHOD(bus_print_child, iicbb_print_child),
87 /* iicbus interface */
88 DEVMETHOD(iicbus_callback, iicbb_callback),
89 DEVMETHOD(iicbus_start, iicbb_start),
90 DEVMETHOD(iicbus_repeated_start, iicbb_start),
91 DEVMETHOD(iicbus_stop, iicbb_stop),
92 DEVMETHOD(iicbus_write, iicbb_write),
93 DEVMETHOD(iicbus_read, iicbb_read),
94 DEVMETHOD(iicbus_reset, iicbb_reset),
99 driver_t iicbb_driver = {
102 sizeof(struct iicbb_softc),
105 devclass_t iicbb_devclass;
107 static int iicbb_probe(device_t dev)
109 device_set_desc(dev, "I2C bit-banging driver");
114 static int iicbb_attach(device_t dev)
116 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
118 bzero(sc, sizeof(struct iicbb_softc));
120 sc->iicbus = device_add_child(dev, "iicbus", -1);
125 bus_generic_attach(dev);
130 static int iicbb_detach(device_t dev)
132 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
135 bus_generic_detach(dev);
136 device_delete_child(dev, sc->iicbus);
143 iicbb_print_child(device_t bus, device_t dev)
149 retval += bus_print_child_header(bus, dev);
150 /* retrieve the interface I2C address */
151 error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr);
152 if (error == IIC_ENOADDR) {
153 retval += printf(" on %s master-only\n",
154 device_get_nameunit(bus));
156 /* restore the address */
157 IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL);
159 retval += printf(" on %s addr 0x%x\n",
160 device_get_nameunit(bus), oldaddr & 0xff);
168 #define I2C_SETSDA(dev,val) do { \
169 IICBB_SETSDA(device_get_parent(dev), val); \
173 #define I2C_SETSCL(dev,val) do { \
174 iicbb_setscl(dev, val, 100); \
177 #define I2C_SET(dev,ctrl,data) do { \
178 I2C_SETSCL(dev, ctrl); \
179 I2C_SETSDA(dev, data); \
182 #define I2C_GETSDA(dev) (IICBB_GETSDA(device_get_parent(dev)))
184 #define I2C_GETSCL(dev) (IICBB_GETSCL(device_get_parent(dev)))
186 static int i2c_debug = 0;
187 #define I2C_DEBUG(x) do { \
188 if (i2c_debug) (x); \
191 #define I2C_LOG(format,args...) do { \
192 printf(format, args); \
195 static void iicbb_setscl(device_t dev, int val, int timeout)
199 IICBB_SETSCL(device_get_parent(dev), val);
202 while (val && !I2C_GETSCL(dev) && k++ < timeout) {
203 IICBB_SETSCL(device_get_parent(dev), val);
210 static void iicbb_one(device_t dev, int timeout)
218 static void iicbb_zero(device_t dev, int timeout)
227 * Waiting for ACKNOWLEDGE.
229 * When a chip is being addressed or has received data it will issue an
230 * ACKNOWLEDGE pulse. Therefore the MASTER must release the DATA line
231 * (set it to high level) and then release the CLOCK line.
232 * Now it must wait for the SLAVE to pull the DATA line low.
233 * Actually on the bus this looks like a START condition so nothing happens
234 * because of the fact that the IC's that have not been addressed are doing
237 * When the SLAVE has pulled this line low the MASTER will take the CLOCK
238 * line low and then the SLAVE will release the SDA (data) line.
240 static int iicbb_ack(device_t dev, int timeout)
248 noack = I2C_GETSDA(dev);
253 } while (k < timeout);
256 I2C_DEBUG(printf("%c ",noack?'-':'+'));
261 static void iicbb_sendbyte(device_t dev, u_char data, int timeout)
265 for (i=7; i>=0; i--) {
267 iicbb_one(dev, timeout);
269 iicbb_zero(dev, timeout);
272 I2C_DEBUG(printf("w%02x",(int)data));
276 static u_char iicbb_readbyte(device_t dev, int last, int timeout)
279 unsigned char data=0;
290 iicbb_one(dev, timeout);
292 iicbb_zero(dev, timeout);
294 I2C_DEBUG(printf("r%02x%c ",(int)data,last?'-':'+'));
298 static int iicbb_callback(device_t dev, int index, caddr_t data)
300 return (IICBB_CALLBACK(device_get_parent(dev), index, data));
303 static int iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
305 return (IICBB_RESET(device_get_parent(dev), speed, addr, oldaddr));
308 static int iicbb_start(device_t dev, u_char slave, int timeout)
312 I2C_DEBUG(printf("<"));
319 iicbb_sendbyte(dev, slave, timeout);
322 if (iicbb_ack(dev, timeout)) {
334 static int iicbb_stop(device_t dev)
339 I2C_DEBUG(printf(">"));
343 static int iicbb_write(device_t dev, char * buf, int len, int *sent,
346 int bytes, error = 0;
351 iicbb_sendbyte(dev,(u_char)*buf++, timeout);
354 if (iicbb_ack(dev, timeout)) {
367 static int iicbb_read(device_t dev, char * buf, int len, int *read,
374 /* XXX should insert delay here */
375 *buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0, delay);
385 DRIVER_MODULE(iicbb, bktr, iicbb_driver, iicbb_devclass, 0, 0);
386 DRIVER_MODULE(iicbb, lpbb, iicbb_driver, iicbb_devclass, 0, 0);
387 DRIVER_MODULE(iicbb, viapm, iicbb_driver, iicbb_devclass, 0, 0);
389 MODULE_DEPEND(iicbb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
390 MODULE_VERSION(iicbb, IICBB_MODVER);