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)
153 bus_generic_detach(dev);
154 device_delete_children(dev);
161 iicbb_get_node(device_t bus, device_t dev)
164 /* We only have one child, the I2C bus, which needs our own node. */
165 return (ofw_bus_get_node(bus));
170 iicbb_child_detached( device_t dev, device_t child )
172 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
174 if (child == sc->iicbus)
179 iicbb_print_child(device_t bus, device_t dev)
185 retval += bus_print_child_header(bus, dev);
186 /* retrieve the interface I2C address */
187 error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr);
188 if (error == IIC_ENOADDR) {
189 retval += printf(" on %s master-only\n",
190 device_get_nameunit(bus));
192 /* restore the address */
193 IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL);
195 retval += printf(" on %s addr 0x%x\n",
196 device_get_nameunit(bus), oldaddr & 0xff);
202 #define I2C_SETSDA(sc,dev,val) do { \
203 IICBB_SETSDA(device_get_parent(dev), val); \
207 #define I2C_SETSCL(dev,val) do { \
208 iicbb_setscl(dev, val, 100); \
211 #define I2C_SET(sc,dev,ctrl,data) do { \
212 I2C_SETSCL(dev, ctrl); \
213 I2C_SETSDA(sc, dev, data); \
216 #define I2C_GETSDA(dev) (IICBB_GETSDA(device_get_parent(dev)))
218 #define I2C_GETSCL(dev) (IICBB_GETSCL(device_get_parent(dev)))
220 static int i2c_debug = 0;
221 #define I2C_DEBUG(x) do { \
222 if (i2c_debug) (x); \
225 #define I2C_LOG(format,args...) do { \
226 printf(format, args); \
230 iicbb_setscl(device_t dev, int val, int timeout)
232 struct iicbb_softc *sc = device_get_softc(dev);
235 IICBB_SETSCL(device_get_parent(dev), val);
238 while (val && !I2C_GETSCL(dev) && k++ < timeout) {
239 IICBB_SETSCL(device_get_parent(dev), val);
247 iicbb_one(device_t dev, int timeout)
249 struct iicbb_softc *sc = device_get_softc(dev);
258 iicbb_zero(device_t dev, int timeout)
260 struct iicbb_softc *sc = device_get_softc(dev);
269 * Waiting for ACKNOWLEDGE.
271 * When a chip is being addressed or has received data it will issue an
272 * ACKNOWLEDGE pulse. Therefore the MASTER must release the DATA line
273 * (set it to high level) and then release the CLOCK line.
274 * Now it must wait for the SLAVE to pull the DATA line low.
275 * Actually on the bus this looks like a START condition so nothing happens
276 * because of the fact that the IC's that have not been addressed are doing
279 * When the SLAVE has pulled this line low the MASTER will take the CLOCK
280 * line low and then the SLAVE will release the SDA (data) line.
283 iicbb_ack(device_t dev, int timeout)
285 struct iicbb_softc *sc = device_get_softc(dev);
292 noack = I2C_GETSDA(dev);
297 } while (k < timeout);
300 I2C_DEBUG(printf("%c ",noack?'-':'+'));
306 iicbb_sendbyte(device_t dev, u_char data, int timeout)
310 for (i=7; i>=0; i--) {
312 iicbb_one(dev, timeout);
314 iicbb_zero(dev, timeout);
317 I2C_DEBUG(printf("w%02x",(int)data));
322 iicbb_readbyte(device_t dev, int last, int timeout)
324 struct iicbb_softc *sc = device_get_softc(dev);
326 unsigned char data=0;
337 iicbb_one(dev, timeout);
339 iicbb_zero(dev, timeout);
341 I2C_DEBUG(printf("r%02x%c ",(int)data,last?'-':'+'));
346 iicbb_callback(device_t dev, int index, caddr_t data)
348 return (IICBB_CALLBACK(device_get_parent(dev), index, data));
352 iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
354 return (IICBB_RESET(device_get_parent(dev), speed, addr, oldaddr));
358 iicbb_start(device_t dev, u_char slave, int timeout)
360 struct iicbb_softc *sc = device_get_softc(dev);
363 I2C_DEBUG(printf("<"));
370 iicbb_sendbyte(dev, slave, timeout);
373 if (iicbb_ack(dev, timeout)) {
386 iicbb_stop(device_t dev)
388 struct iicbb_softc *sc = device_get_softc(dev);
393 I2C_DEBUG(printf(">"));
394 I2C_DEBUG(printf("\n"));
399 iicbb_write(device_t dev, const char *buf, int len, int *sent, int timeout)
401 int bytes, error = 0;
406 iicbb_sendbyte(dev,(u_char)*buf++, timeout);
409 if (iicbb_ack(dev, timeout)) {
423 iicbb_read(device_t dev, char * buf, int len, int *read, int last, int delay)
429 /* XXX should insert delay here */
430 *buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0, delay);
441 iicbb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
445 error = IICBB_PRE_XFER(device_get_parent(dev));
449 error = iicbus_transfer_gen(dev, msgs, nmsgs);
451 IICBB_POST_XFER(device_get_parent(dev));
455 DRIVER_MODULE(iicbus, iicbb, iicbus_driver, iicbus_devclass, 0, 0);
457 MODULE_DEPEND(iicbb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
458 MODULE_VERSION(iicbb, IICBB_MODVER);