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>
55 #include <sys/sysctl.h>
59 #include <dev/ofw/ofw_bus.h>
60 #include <dev/ofw/ofw_bus_subr.h>
61 #include <dev/fdt/fdt_common.h>
64 #include <dev/iicbus/iiconf.h>
65 #include <dev/iicbus/iicbus.h>
67 #include <dev/smbus/smbconf.h>
69 #include "iicbus_if.h"
72 /* Based on the SMBus specification. */
73 #define DEFAULT_SCL_LOW_TIMEOUT (25 * 1000)
77 u_int udelay; /* signal toggle delay in usec */
78 u_int scl_low_timeout;
81 static int iicbb_attach(device_t);
82 static void iicbb_child_detached(device_t, device_t);
83 static int iicbb_detach(device_t);
84 static int iicbb_print_child(device_t, device_t);
85 static int iicbb_probe(device_t);
87 static int iicbb_callback(device_t, int, caddr_t);
88 static int iicbb_start(device_t, u_char, int);
89 static int iicbb_stop(device_t);
90 static int iicbb_write(device_t, const char *, int, int *, int);
91 static int iicbb_read(device_t, char *, int, int *, int, int);
92 static int iicbb_reset(device_t, u_char, u_char, u_char *);
93 static int iicbb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs);
94 static void iicbb_set_speed(struct iicbb_softc *sc, u_char);
96 static phandle_t iicbb_get_node(device_t, device_t);
99 static device_method_t iicbb_methods[] = {
100 /* device interface */
101 DEVMETHOD(device_probe, iicbb_probe),
102 DEVMETHOD(device_attach, iicbb_attach),
103 DEVMETHOD(device_detach, iicbb_detach),
106 DEVMETHOD(bus_child_detached, iicbb_child_detached),
107 DEVMETHOD(bus_print_child, iicbb_print_child),
109 /* iicbus interface */
110 DEVMETHOD(iicbus_callback, iicbb_callback),
111 DEVMETHOD(iicbus_start, iicbb_start),
112 DEVMETHOD(iicbus_repeated_start, iicbb_start),
113 DEVMETHOD(iicbus_stop, iicbb_stop),
114 DEVMETHOD(iicbus_write, iicbb_write),
115 DEVMETHOD(iicbus_read, iicbb_read),
116 DEVMETHOD(iicbus_reset, iicbb_reset),
117 DEVMETHOD(iicbus_transfer, iicbb_transfer),
120 /* ofw_bus interface */
121 DEVMETHOD(ofw_bus_get_node, iicbb_get_node),
127 driver_t iicbb_driver = {
130 sizeof(struct iicbb_softc),
133 devclass_t iicbb_devclass;
136 iicbb_probe(device_t dev)
138 device_set_desc(dev, "I2C bit-banging driver");
144 iicbb_attach(device_t dev)
146 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
148 sc->iicbus = device_add_child(dev, "iicbus", -1);
152 sc->scl_low_timeout = DEFAULT_SCL_LOW_TIMEOUT;
154 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
155 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
156 "delay", CTLFLAG_RD, &sc->udelay,
157 0, "Signal change delay controlled by bus frequency, microseconds");
159 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
160 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
161 "scl_low_timeout", CTLFLAG_RWTUN, &sc->scl_low_timeout,
162 0, "SCL low timeout, microseconds");
163 bus_generic_attach(dev);
168 iicbb_detach(device_t dev)
171 bus_generic_detach(dev);
172 device_delete_children(dev);
179 iicbb_get_node(device_t bus, device_t dev)
182 /* We only have one child, the I2C bus, which needs our own node. */
183 return (ofw_bus_get_node(bus));
188 iicbb_child_detached( device_t dev, device_t child )
190 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
192 if (child == sc->iicbus)
197 iicbb_print_child(device_t bus, device_t dev)
203 retval += bus_print_child_header(bus, dev);
204 /* retrieve the interface I2C address */
205 error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr);
206 if (error == IIC_ENOADDR) {
207 retval += printf(" on %s master-only\n",
208 device_get_nameunit(bus));
210 /* restore the address */
211 IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL);
213 retval += printf(" on %s addr 0x%x\n",
214 device_get_nameunit(bus), oldaddr & 0xff);
220 #define I2C_GETSDA(dev) (IICBB_GETSDA(device_get_parent(dev)))
221 #define I2C_SETSDA(dev, x) (IICBB_SETSDA(device_get_parent(dev), x))
222 #define I2C_GETSCL(dev) (IICBB_GETSCL(device_get_parent(dev)))
223 #define I2C_SETSCL(dev, x) (IICBB_SETSCL(device_get_parent(dev), x))
225 #define I2C_SET(sc, dev, ctrl, val) do { \
226 iicbb_setscl(dev, ctrl); \
227 I2C_SETSDA(dev, val); \
231 static int i2c_debug = 0;
232 #define I2C_DEBUG(x) do { \
233 if (i2c_debug) (x); \
236 #define I2C_LOG(format,args...) do { \
237 printf(format, args); \
241 iicbb_setscl(device_t dev, int val)
243 struct iicbb_softc *sc = device_get_softc(dev);
247 I2C_SETSCL(dev, val);
250 /* Pulling low cannot fail. */
254 /* Use DELAY for up to 1 ms, then switch to pause. */
255 end = sbinuptime() + sc->scl_low_timeout * SBT_1US;
256 fast_timeout = MIN(sc->scl_low_timeout, 1000);
257 while (fast_timeout > 0) {
260 I2C_SETSCL(dev, 1); /* redundant ? */
262 fast_timeout -= sc->udelay;
265 while (!I2C_GETSCL(dev)) {
269 pause_sbt("iicbb-scl-low", SBT_1MS, C_PREL(8), 0);
275 iicbb_one(device_t dev, int timeout)
277 struct iicbb_softc *sc = device_get_softc(dev);
286 iicbb_zero(device_t dev, int timeout)
288 struct iicbb_softc *sc = device_get_softc(dev);
297 * Waiting for ACKNOWLEDGE.
299 * When a chip is being addressed or has received data it will issue an
300 * ACKNOWLEDGE pulse. Therefore the MASTER must release the DATA line
301 * (set it to high level) and then release the CLOCK line.
302 * Now it must wait for the SLAVE to pull the DATA line low.
303 * Actually on the bus this looks like a START condition so nothing happens
304 * because of the fact that the IC's that have not been addressed are doing
307 * When the SLAVE has pulled this line low the MASTER will take the CLOCK
308 * line low and then the SLAVE will release the SDA (data) line.
311 iicbb_ack(device_t dev, int timeout)
313 struct iicbb_softc *sc = device_get_softc(dev);
320 /* SCL must be high now. */
321 if (!I2C_GETSCL(dev))
322 return (IIC_ETIMEOUT);
325 noack = I2C_GETSDA(dev);
330 } while (k < timeout);
333 I2C_DEBUG(printf("%c ",noack?'-':'+'));
335 return (noack ? IIC_ENOACK : 0);
339 iicbb_sendbyte(device_t dev, u_char data, int timeout)
343 for (i=7; i>=0; i--) {
345 iicbb_one(dev, timeout);
347 iicbb_zero(dev, timeout);
350 I2C_DEBUG(printf("w%02x",(int)data));
355 iicbb_readbyte(device_t dev, int last, int timeout)
357 struct iicbb_softc *sc = device_get_softc(dev);
359 unsigned char data=0;
370 iicbb_one(dev, timeout);
372 iicbb_zero(dev, timeout);
374 I2C_DEBUG(printf("r%02x%c ",(int)data,last?'-':'+'));
379 iicbb_callback(device_t dev, int index, caddr_t data)
381 return (IICBB_CALLBACK(device_get_parent(dev), index, data));
385 iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
387 iicbb_set_speed(device_get_softc(dev), speed);
388 return (IICBB_RESET(device_get_parent(dev), speed, addr, oldaddr));
392 iicbb_start(device_t dev, u_char slave, int timeout)
394 struct iicbb_softc *sc = device_get_softc(dev);
397 I2C_DEBUG(printf("<"));
401 /* SCL must be high now. */
402 if (!I2C_GETSCL(dev))
403 return (IIC_ETIMEOUT);
409 iicbb_sendbyte(dev, slave, timeout);
412 error = iicbb_ack(dev, timeout);
421 iicbb_stop(device_t dev)
423 struct iicbb_softc *sc = device_get_softc(dev);
428 I2C_DEBUG(printf(">"));
429 I2C_DEBUG(printf("\n"));
431 /* SCL must be high now. */
432 if (!I2C_GETSCL(dev))
433 return (IIC_ETIMEOUT);
438 iicbb_write(device_t dev, const char *buf, int len, int *sent, int timeout)
440 int bytes, error = 0;
445 iicbb_sendbyte(dev,(u_char)*buf++, timeout);
448 error = iicbb_ack(dev, timeout);
460 iicbb_read(device_t dev, char * buf, int len, int *read, int last, int delay)
466 /* XXX should insert delay here */
467 *buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0, delay);
478 iicbb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
482 error = IICBB_PRE_XFER(device_get_parent(dev));
486 error = iicbus_transfer_gen(dev, msgs, nmsgs);
488 IICBB_POST_XFER(device_get_parent(dev));
493 iicbb_set_speed(struct iicbb_softc *sc, u_char speed)
495 u_int busfreq, period;
498 * NB: the resulting frequency will be a quarter (even less) of the
499 * configured bus frequency. This is for historic reasons. The default
500 * bus frequency is 100 kHz. And the historic default udelay is 10
501 * microseconds. The cycle of sending a bit takes four udelay-s plus
502 * SCL is kept low for extra two udelay-s. The actual I/O toggling also
505 busfreq = IICBUS_GET_FREQUENCY(sc->iicbus, speed);
506 period = 1000000 / busfreq; /* Hz -> uS */
507 sc->udelay = MAX(period, 1);
510 DRIVER_MODULE(iicbus, iicbb, iicbus_driver, iicbus_devclass, 0, 0);
512 MODULE_DEPEND(iicbb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
513 MODULE_VERSION(iicbb, IICBB_MODVER);