i2c: Add FTDI FT4222H USB I2C adapter
PATCH v2 1/1 i2c: Add FTDI FT4222H USB I2C adapter
FT4222H exports a USB interface to operate as I2C bus master when set up (via the DCNF pins) in configuration mode 0 or 3.
The USB communication protocol implemented by the FT4222 chip is not publicly documented, therefore the name assigned to the various defines used in the USB communication code is a semi-educated guess after sniffing USB traffic generated with the FTDI FT4222 userspace library.
---
Changes from v1 [1]:
- replaced magic numbers with symbolic constants
- changed enums to use capital letters
- decreased max retries in ft4222_i2c_get_status()
- removed not so useful dev_dbg() call
- removed handling of configuration modes 1 and 2
- miscellaneous stylistic changes
[1] https://lore.kernel.org/linux-i2c/[email protected]/T/
.../ABI/testing/sysfs-bus-i2c-devices-ft4222 | 7 + drivers/i2c/busses/Kconfig | 11 + drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-ft4222.c | 455 ++++++++++++++++++ 4 files changed, 474 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-i2c-devices-ft4222 create mode 100644 drivers/i2c/busses/i2c-ft4222.c
Documentation/ABI/testing/sysfs-bus-i2c-devices-ft4222 new file +7 -0
@@ -0,0 +1,7 @@ +What: /sys/bus/i2c/devices/i2c-<bus-id>/i2c_freq +Date: January 2026 +KernelVersion: 6.20 +Contact: Francesco Lavra <[email protected]> +Description: (RW) I2C bus clock frequency, expressed in Hz. + Accepted values range from 100000 (standard mode) to 3400000 + (high-speed mode).
drivers/i2c/busses/Kconfig +11 -0
@@ -1462,6 +1462,17 @@ config I2C_TINY_USB This driver can also be built as a module. If so, the module will be called i2c-tiny-usb. +config I2C_FT4222 + tristate "FTDI FT4222 USB to I2C interface" + depends on USB + help + Include support for interfacing with I2C devices via an FTDI + FT4222H USB device that acts as I2C master. + The I2C interface is available when the chip is set up (via the + DCNF0 and DCNF1 pins) in configuration mode 0 or 3. + + If built as a module, this driver creates a module called i2c-ft4222. + config I2C_VIPERBOARD tristate "Viperboard I2C master support" depends on MFD_VIPERBOARD && USB
drivers/i2c/busses/Makefile +1 -0
@@ -144,6 +144,7 @@ obj-$(CONFIG_I2C_PCI1XXXX) += i2c-mchp-pci1xxxx.o obj-$(CONFIG_I2C_ROBOTFUZZ_OSIF) += i2c-robotfuzz-osif.o obj-$(CONFIG_I2C_TAOS_EVM) += i2c-taos-evm.o obj-$(CONFIG_I2C_TINY_USB) += i2c-tiny-usb.o +obj-$(CONFIG_I2C_FT4222) += i2c-ft4222.o obj-$(CONFIG_I2C_VIPERBOARD) += i2c-viperboard.o # Other I2C/SMBus bus drivers
drivers/i2c/busses/i2c-ft4222.c new file +455 -0
455 lines not on this page · raw message
Signature · 1 line
2.39.5
Andi Shyti
maintainerHi Francesco,
...
Quoted 13 lines
drivers/i2c/busses/i2c-ft4222.c new file +13
-0+static int ft4222_i2c_get_status (struct ft4222_i2c *ftdi) +{ +/* +* Multiple retries are needed mostly when retrieving the controller +* status after doing a write with the I2C bus operating at a low speed. +* Empirical tests conducted at 100 kHz showed that after a +* maximum-sized (512-byte) write, up to 11 retries are needed before +* the chip clears its CTRL_BUSY flag. But under certain conditions more +* retries may be needed: for example, when trying to do a write after +* disconnecting the SCL line from the I2C slave, tests showed that up +* to 64 retries are needed. +*/ +const int max_retries =70 ;
drivers/i2c/busses/i2c-ft4222.c line 138
Quoted 10 lines
drivers/i2c/busses/i2c-ft4222.c new file +10
-0+int retry; +u8 status; + +for (retry =0 ; retry < max_retries; retry++) { +int ret =ft4222_cmd_get (ftdi,FT4222_CMD_I2C_STATUS , &status); + +if (ret <0 ) +return ret; +if (!(status &FT4222_I2C_CTRL_BUSY )) +break ;
How about adding a small delay here. Does it also help to reduce the maximum number of retries?
Quoted 1 line
(quoted diff) +1
-0+ }
...
drivers/i2c/busses/i2c-ft4222.c line 252
Quoted 10 lines
drivers/i2c/busses/i2c-ft4222.c new file +10
-0+dev_dbg (&adapter->dev,"transfer with %d message(s)" , num); +for (i =0 ; i < num; ++i) { +const u8 addr = msg[i].addr; +const u16 len = msg[i].len; +u8 *buf = msg[i].buf; +u8 flags; + + flags = ((i ==0 ) ?FT4222_FLAG_START :FT4222_FLAG_RESTART ); +if (i == num -1 ) + flags |=FT4222_FLAG_STOP ;
Are we setting here both STOP and RESTART at the same time.
The rest looks good to me.
Andi
Quoted 1 line
drivers/i2c/busses/i2c-ft4222.c new file +1
-0+if (msg[i].flags &I2C_M_RD )
Hi Andi,
Quoted 29 lines · Andi Shyti
Hi Francesco,
...
Quoted 25 lines
drivers/i2c/busses/i2c-ft4222.c new file +4
-0+static int ft4222_i2c_get_status (struct ft4222_i2c *ftdi) +{ + /* +* Multiple retries are needed mostly when retrieving the controller
+ * status after doing a write with the I2C bus operating at a low speed.
+ * Empirical tests conducted at 100 kHz showed that after a + * maximum-sized (512-byte) write, up to 11 retries are needed before
+ * the chip clears itsCTRL_BUSYflag. But under certain conditions more
+ * retries may be needed: for example, when trying to do a write after
+ * disconnecting the SCL line from the I2C slave, tests showed that up
+ * to 64 retries are needed.
+ */
+ const intmax_retries= 70;
+ int retry;
+ u8 status;
+
+ for (retry = 0; retry <max_retries; retry++) {
+ int ret =ft4222_cmd_get(ftdi, FT4222_CMD_I2C_STATUS, &status);
Quoted 8 lines
Quoted 5 lines
+
+ if (ret < 0)
+ return ret;
+ if (!(status &FT4222_I2C_CTRL_BUSY)) + break;How about adding a small delay here. Does it also help to reduce the maximum number of retries?
Yes, I tried adding a call to msleep(10) here, and with that delay I have never seen the retry variable higher than 4 at the end of the loop. So I will add msleep(10), and change the max_retries constant to something like 5.
Quoted 16 lines
Quoted 1 line
+ }
...
Quoted 11 lines
+ dev_dbg(&adapter->dev, "transfer with %d message(s)", num); + for (i = 0; i < num; ++i) {
+ const u8 addr = msg[i].addr;
+ const u16 len = msg[i].len;
+ u8 *buf = msg[i].buf;
+ u8 flags;
+
+ flags = ((i == 0) ?FT4222_FLAG_START:FT4222_FLAG_RESTART);
+ if (i == num - 1)
+ flags |=FT4222_FLAG_STOP;
Quoted 1 line
Are we setting here both STOP and RESTART at the same time.
Yes, for each I2C message the driver must set the correct flags in order for the adapter to create the appropriate I2C bus conditions. So for example in a single-message transfer, we set (FT4222_FLAG_START | FT4222_FLAG_STOP); in a 2-message transfer, we set (FT4222_FLAG_START) for the first message and (FT4222_FLAG_RESTART | FT4222_FLAG_STOP) for the second message.
Quoted 1 line
The rest looks good to me.