2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998, 2001 Nicolas Souchu
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * Generic I2C bit-banging code
43 * From Linux I2C generic interface
44 * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
48 #include "opt_platform.h"
50 #include <sys/param.h>
51 #include <sys/kernel.h>
52 #include <sys/systm.h>
53 #include <sys/module.h>
58 #include <dev/ofw/ofw_bus.h>
59 #include <dev/ofw/ofw_bus_subr.h>
60 #include <dev/fdt/fdt_common.h>
63 #include <dev/iicbus/iiconf.h>
64 #include <dev/iicbus/iicbus.h>
66 #include <dev/smbus/smbconf.h>
68 #include "iicbus_if.h"
73 int udelay; /* signal toggle delay in usec */
76 static int iicbb_attach(device_t);
77 static void iicbb_child_detached(device_t, device_t);
78 static int iicbb_detach(device_t);
79 static int iicbb_print_child(device_t, device_t);
80 static int iicbb_probe(device_t);
82 static int iicbb_callback(device_t, int, caddr_t);
83 static int iicbb_start(device_t, u_char, int);
84 static int iicbb_stop(device_t);
85 static int iicbb_write(device_t, const char *, int, int *, int);
86 static int iicbb_read(device_t, char *, int, int *, int, int);
87 static int iicbb_reset(device_t, u_char, u_char, u_char *);
88 static int iicbb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs);
90 static phandle_t iicbb_get_node(device_t, device_t);
93 static device_method_t iicbb_methods[] = {
94 /* device interface */
95 DEVMETHOD(device_probe, iicbb_probe),
96 DEVMETHOD(device_attach, iicbb_attach),
97 DEVMETHOD(device_detach, iicbb_detach),
100 DEVMETHOD(bus_child_detached, iicbb_child_detached),
101 DEVMETHOD(bus_print_child, iicbb_print_child),
103 /* iicbus interface */
104 DEVMETHOD(iicbus_callback, iicbb_callback),
105 DEVMETHOD(iicbus_start, iicbb_start),
106 DEVMETHOD(iicbus_repeated_start, iicbb_start),
107 DEVMETHOD(iicbus_stop, iicbb_stop),
108 DEVMETHOD(iicbus_write, iicbb_write),
109 DEVMETHOD(iicbus_read, iicbb_read),
110 DEVMETHOD(iicbus_reset, iicbb_reset),
111 DEVMETHOD(iicbus_transfer, iicbb_transfer),
114 /* ofw_bus interface */
115 DEVMETHOD(ofw_bus_get_node, iicbb_get_node),
121 driver_t iicbb_driver = {
124 sizeof(struct iicbb_softc),
127 devclass_t iicbb_devclass;
130 iicbb_probe(device_t dev)
132 device_set_desc(dev, "I2C bit-banging driver");
138 iicbb_attach(device_t dev)
140 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
142 sc->iicbus = device_add_child(dev, "iicbus", -1);
145 sc->udelay = 10; /* 10 uS default */
146 bus_generic_attach(dev);
152 iicbb_detach(device_t dev)
155 bus_generic_detach(dev);
156 device_delete_children(dev);
163 iicbb_get_node(device_t bus, device_t dev)
166 /* We only have one child, the I2C bus, which needs our own node. */
167 return (ofw_bus_get_node(bus));
172 iicbb_child_detached( device_t dev, device_t child )
174 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
176 if (child == sc->iicbus)
181 iicbb_print_child(device_t bus, device_t dev)
187 retval += bus_print_child_header(bus, dev);
188 /* retrieve the interface I2C address */
189 error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr);
190 if (error == IIC_ENOADDR) {
191 retval += printf(" on %s master-only\n",
192 device_get_nameunit(bus));
194 /* restore the address */
195 IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL);
197 retval += printf(" on %s addr 0x%x\n",
198 device_get_nameunit(bus), oldaddr & 0xff);
204 #define I2C_SETSDA(sc,dev,val) do { \
205 IICBB_SETSDA(device_get_parent(dev), val); \
209 #define I2C_SETSCL(dev,val) do { \
210 iicbb_setscl(dev, val, 100); \
213 #define I2C_SET(sc,dev,ctrl,data) do { \
214 I2C_SETSCL(dev, ctrl); \
215 I2C_SETSDA(sc, dev, data); \
218 #define I2C_GETSDA(dev) (IICBB_GETSDA(device_get_parent(dev)))
220 #define I2C_GETSCL(dev) (IICBB_GETSCL(device_get_parent(dev)))
222 static int i2c_debug = 0;
223 #define I2C_DEBUG(x) do { \
224 if (i2c_debug) (x); \
227 #define I2C_LOG(format,args...) do { \
228 printf(format, args); \
232 iicbb_setscl(device_t dev, int val, int timeout)
234 struct iicbb_softc *sc = device_get_softc(dev);
237 IICBB_SETSCL(device_get_parent(dev), val);
240 while (val && !I2C_GETSCL(dev) && k++ < timeout) {
241 IICBB_SETSCL(device_get_parent(dev), val);
249 iicbb_one(device_t dev, int timeout)
251 struct iicbb_softc *sc = device_get_softc(dev);
260 iicbb_zero(device_t dev, int timeout)
262 struct iicbb_softc *sc = device_get_softc(dev);
271 * Waiting for ACKNOWLEDGE.
273 * When a chip is being addressed or has received data it will issue an
274 * ACKNOWLEDGE pulse. Therefore the MASTER must release the DATA line
275 * (set it to high level) and then release the CLOCK line.
276 * Now it must wait for the SLAVE to pull the DATA line low.
277 * Actually on the bus this looks like a START condition so nothing happens
278 * because of the fact that the IC's that have not been addressed are doing
281 * When the SLAVE has pulled this line low the MASTER will take the CLOCK
282 * line low and then the SLAVE will release the SDA (data) line.
285 iicbb_ack(device_t dev, int timeout)
287 struct iicbb_softc *sc = device_get_softc(dev);
294 noack = I2C_GETSDA(dev);
299 } while (k < timeout);
302 I2C_DEBUG(printf("%c ",noack?'-':'+'));
308 iicbb_sendbyte(device_t dev, u_char data, int timeout)
312 for (i=7; i>=0; i--) {
314 iicbb_one(dev, timeout);
316 iicbb_zero(dev, timeout);
319 I2C_DEBUG(printf("w%02x",(int)data));
324 iicbb_readbyte(device_t dev, int last, int timeout)
326 struct iicbb_softc *sc = device_get_softc(dev);
328 unsigned char data=0;
339 iicbb_one(dev, timeout);
341 iicbb_zero(dev, timeout);
343 I2C_DEBUG(printf("r%02x%c ",(int)data,last?'-':'+'));
348 iicbb_callback(device_t dev, int index, caddr_t data)
350 return (IICBB_CALLBACK(device_get_parent(dev), index, data));
354 iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
356 return (IICBB_RESET(device_get_parent(dev), speed, addr, oldaddr));
360 iicbb_start(device_t dev, u_char slave, int timeout)
362 struct iicbb_softc *sc = device_get_softc(dev);
365 I2C_DEBUG(printf("<"));
372 iicbb_sendbyte(dev, slave, timeout);
375 if (iicbb_ack(dev, timeout)) {
388 iicbb_stop(device_t dev)
390 struct iicbb_softc *sc = device_get_softc(dev);
395 I2C_DEBUG(printf(">"));
396 I2C_DEBUG(printf("\n"));
401 iicbb_write(device_t dev, const char *buf, int len, int *sent, int timeout)
403 int bytes, error = 0;
408 iicbb_sendbyte(dev,(u_char)*buf++, timeout);
411 if (iicbb_ack(dev, timeout)) {
425 iicbb_read(device_t dev, char * buf, int len, int *read, int last, int delay)
431 /* XXX should insert delay here */
432 *buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0, delay);
443 iicbb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
447 error = IICBB_PRE_XFER(device_get_parent(dev));
451 error = iicbus_transfer_gen(dev, msgs, nmsgs);
453 IICBB_POST_XFER(device_get_parent(dev));
457 DRIVER_MODULE(iicbus, iicbb, iicbus_driver, iicbus_devclass, 0, 0);
459 MODULE_DEPEND(iicbb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
460 MODULE_VERSION(iicbb, IICBB_MODVER);