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 "opt_platform.h"
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/module.h>
56 #include <dev/ofw/ofw_bus.h>
57 #include <dev/ofw/ofw_bus_subr.h>
58 #include <dev/fdt/fdt_common.h>
61 #include <dev/iicbus/iiconf.h>
62 #include <dev/iicbus/iicbus.h>
64 #include <dev/smbus/smbconf.h>
66 #include "iicbus_if.h"
71 int udelay; /* signal toggle delay in usec */
74 static int iicbb_attach(device_t);
75 static void iicbb_child_detached(device_t, device_t);
76 static int iicbb_detach(device_t);
77 static int iicbb_print_child(device_t, device_t);
78 static int iicbb_probe(device_t);
80 static int iicbb_callback(device_t, int, caddr_t);
81 static int iicbb_start(device_t, u_char, int);
82 static int iicbb_stop(device_t);
83 static int iicbb_write(device_t, const char *, int, int *, int);
84 static int iicbb_read(device_t, char *, int, int *, int, int);
85 static int iicbb_reset(device_t, u_char, u_char, u_char *);
86 static int iicbb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs);
88 static phandle_t iicbb_get_node(device_t, device_t);
91 static device_method_t iicbb_methods[] = {
92 /* device interface */
93 DEVMETHOD(device_probe, iicbb_probe),
94 DEVMETHOD(device_attach, iicbb_attach),
95 DEVMETHOD(device_detach, iicbb_detach),
98 DEVMETHOD(bus_child_detached, iicbb_child_detached),
99 DEVMETHOD(bus_print_child, iicbb_print_child),
101 /* iicbus interface */
102 DEVMETHOD(iicbus_callback, iicbb_callback),
103 DEVMETHOD(iicbus_start, iicbb_start),
104 DEVMETHOD(iicbus_repeated_start, iicbb_start),
105 DEVMETHOD(iicbus_stop, iicbb_stop),
106 DEVMETHOD(iicbus_write, iicbb_write),
107 DEVMETHOD(iicbus_read, iicbb_read),
108 DEVMETHOD(iicbus_reset, iicbb_reset),
109 DEVMETHOD(iicbus_transfer, iicbb_transfer),
112 /* ofw_bus interface */
113 DEVMETHOD(ofw_bus_get_node, iicbb_get_node),
119 driver_t iicbb_driver = {
122 sizeof(struct iicbb_softc),
125 devclass_t iicbb_devclass;
128 iicbb_probe(device_t dev)
130 device_set_desc(dev, "I2C bit-banging driver");
136 iicbb_attach(device_t dev)
138 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
140 sc->iicbus = device_add_child(dev, "iicbus", -1);
143 sc->udelay = 10; /* 10 uS default */
144 bus_generic_attach(dev);
150 iicbb_detach(device_t dev)
152 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
156 * We need to save child because the detach indirectly causes
157 * sc->iicbus to be zeroed. Since we added the device
158 * unconditionally in iicbb_attach, we need to make sure we
159 * delete it here. See iicbb_child_detached. We need that
160 * callback in case newbus detached our children w/o detaching
161 * us (say iicbus is a module and unloaded w/o iicbb being
165 bus_generic_detach(dev);
167 device_delete_child(dev, child);
174 iicbb_get_node(device_t bus, device_t dev)
177 /* We only have one child, the I2C bus, which needs our own node. */
178 return (ofw_bus_get_node(bus));
183 iicbb_child_detached( device_t dev, device_t child )
185 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
187 if (child == sc->iicbus)
192 iicbb_print_child(device_t bus, device_t dev)
198 retval += bus_print_child_header(bus, dev);
199 /* retrieve the interface I2C address */
200 error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr);
201 if (error == IIC_ENOADDR) {
202 retval += printf(" on %s master-only\n",
203 device_get_nameunit(bus));
205 /* restore the address */
206 IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL);
208 retval += printf(" on %s addr 0x%x\n",
209 device_get_nameunit(bus), oldaddr & 0xff);
215 #define I2C_SETSDA(sc,dev,val) do { \
216 IICBB_SETSDA(device_get_parent(dev), val); \
220 #define I2C_SETSCL(dev,val) do { \
221 iicbb_setscl(dev, val, 100); \
224 #define I2C_SET(sc,dev,ctrl,data) do { \
225 I2C_SETSCL(dev, ctrl); \
226 I2C_SETSDA(sc, dev, data); \
229 #define I2C_GETSDA(dev) (IICBB_GETSDA(device_get_parent(dev)))
231 #define I2C_GETSCL(dev) (IICBB_GETSCL(device_get_parent(dev)))
233 static int i2c_debug = 0;
234 #define I2C_DEBUG(x) do { \
235 if (i2c_debug) (x); \
238 #define I2C_LOG(format,args...) do { \
239 printf(format, args); \
243 iicbb_setscl(device_t dev, int val, int timeout)
245 struct iicbb_softc *sc = device_get_softc(dev);
248 IICBB_SETSCL(device_get_parent(dev), val);
251 while (val && !I2C_GETSCL(dev) && k++ < timeout) {
252 IICBB_SETSCL(device_get_parent(dev), val);
260 iicbb_one(device_t dev, int timeout)
262 struct iicbb_softc *sc = device_get_softc(dev);
271 iicbb_zero(device_t dev, int timeout)
273 struct iicbb_softc *sc = device_get_softc(dev);
282 * Waiting for ACKNOWLEDGE.
284 * When a chip is being addressed or has received data it will issue an
285 * ACKNOWLEDGE pulse. Therefore the MASTER must release the DATA line
286 * (set it to high level) and then release the CLOCK line.
287 * Now it must wait for the SLAVE to pull the DATA line low.
288 * Actually on the bus this looks like a START condition so nothing happens
289 * because of the fact that the IC's that have not been addressed are doing
292 * When the SLAVE has pulled this line low the MASTER will take the CLOCK
293 * line low and then the SLAVE will release the SDA (data) line.
296 iicbb_ack(device_t dev, int timeout)
298 struct iicbb_softc *sc = device_get_softc(dev);
305 noack = I2C_GETSDA(dev);
310 } while (k < timeout);
313 I2C_DEBUG(printf("%c ",noack?'-':'+'));
319 iicbb_sendbyte(device_t dev, u_char data, int timeout)
323 for (i=7; i>=0; i--) {
325 iicbb_one(dev, timeout);
327 iicbb_zero(dev, timeout);
330 I2C_DEBUG(printf("w%02x",(int)data));
335 iicbb_readbyte(device_t dev, int last, int timeout)
337 struct iicbb_softc *sc = device_get_softc(dev);
339 unsigned char data=0;
350 iicbb_one(dev, timeout);
352 iicbb_zero(dev, timeout);
354 I2C_DEBUG(printf("r%02x%c ",(int)data,last?'-':'+'));
359 iicbb_callback(device_t dev, int index, caddr_t data)
361 return (IICBB_CALLBACK(device_get_parent(dev), index, data));
365 iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
367 return (IICBB_RESET(device_get_parent(dev), speed, addr, oldaddr));
371 iicbb_start(device_t dev, u_char slave, int timeout)
373 struct iicbb_softc *sc = device_get_softc(dev);
376 I2C_DEBUG(printf("<"));
383 iicbb_sendbyte(dev, slave, timeout);
386 if (iicbb_ack(dev, timeout)) {
399 iicbb_stop(device_t dev)
401 struct iicbb_softc *sc = device_get_softc(dev);
406 I2C_DEBUG(printf(">"));
407 I2C_DEBUG(printf("\n"));
412 iicbb_write(device_t dev, const char *buf, int len, int *sent, int timeout)
414 int bytes, error = 0;
419 iicbb_sendbyte(dev,(u_char)*buf++, timeout);
422 if (iicbb_ack(dev, timeout)) {
436 iicbb_read(device_t dev, char * buf, int len, int *read, int last, int delay)
442 /* XXX should insert delay here */
443 *buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0, delay);
454 iicbb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
458 error = IICBB_PRE_XFER(device_get_parent(dev));
462 error = iicbus_transfer_gen(dev, msgs, nmsgs);
464 IICBB_POST_XFER(device_get_parent(dev));
468 DRIVER_MODULE(iicbus, iicbb, iicbus_driver, iicbus_devclass, 0, 0);
470 MODULE_DEPEND(iicbb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
471 MODULE_VERSION(iicbb, IICBB_MODVER);