Skip to contentSkip to conversation

i2c: Add FTDI FT4222H USB I2C adapter

PATCH Superseded Francesco Lavra 2 participants 3 messages Jan 29 – Feb 9 I2C subsystem host drivers

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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 v2 [2]:
- added delay between retries and decreased maximum retries in ft4222_i2c_get_status()

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/
[2] 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               | 454 ++++++++++++++++++
 4 files changed, 473 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
new file mode 100644
index 000000000000..fdd921f723bd
@@ -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
index bcbc6693dbe4..313613e07abe 100644
@@ -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
index e1a71830321c..bb708372ef3a 100644
@@ -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 +454 -0

454 lines not on this page · raw message

Signature · 1 line
2.39.5
maintainer
#1 lore raw

Hi Francesco,

...

drivers/i2c/busses/i2c-ft4222.c line 162

Quoted 10 lines
drivers/i2c/busses/i2c-ft4222.c new file +10 -0
+static int ft4222_i2c_write(struct ft4222_i2c *ftdi, u8 flags, u8 slave_addr,
+			    u8 *data, int len)
+{
+	struct usb_device *udev = ftdi->udev;
+	unsigned int pipe = usb_sndbulkpipe(udev, FT4222_EP_TX);
+	u8 *buf = ftdi->ubuf;
+	int written = 0;
+
+	dev_dbg(&ftdi->adapter.dev, "write to 0x%02x, flags 0x%02x, len %d",
+		slave_addr, flags, len);

Here and in every dev_dbg/dev_err, you are missing the "\n" at the end of the message.

drivers/i2c/busses/i2c-ft4222.c line 174

Quoted 12 lines
drivers/i2c/busses/i2c-ft4222.c new file +12 -0
+	buf[0] = slave_addr << 1;
+	buf[2] = buf[3] = 0;
+	do {
+		int pkt_len = min(FT4222_TX_HDRLEN + len - written,
+				  FT4222_BULK_MAXLEN);
+		bool first_pkt = (written == 0);
+		bool last_pkt = (written + pkt_len == FT4222_TX_HDRLEN + len);
+		int ret, actual_len;
+
+		buf[1] = 0;
+		if (first_pkt)
+			buf[1] |= flags & FT4222_FLAG_RESTART;

Aren't we losing the _START case here?

Andi

Quoted 19 lines
drivers/i2c/busses/i2c-ft4222.c new file +19 -0
+		if (last_pkt)
+			buf[1] |= flags & FT4222_FLAG_STOP;
+		if (buf[1] == 0)
+			buf[1] = FT4222_FLAG_NONE;
+		memcpy(buf + FT4222_TX_HDRLEN, data + written,
+		       pkt_len - FT4222_TX_HDRLEN);
+		ret = usb_bulk_msg(udev, pipe, buf, pkt_len, &actual_len,
+				   FT4222_IO_TIMEOUT);
+		if (ret < 0)
+			return ret;
+		if (actual_len < pkt_len)
+			return -EIO;
+		ret = ft4222_i2c_get_status(ftdi);
+		if (ret < 0)
+			return ret;
+		written += pkt_len - FT4222_TX_HDRLEN;
+	} while (written < len);
+	return 0;
+}
#2 lore raw

On Wed, 2026-02-04 at 23:06 +0100, Andi Shyti wrote:
...

Quoted 14 lines
Quoted 5 lines

+       buf[0] = slave_addr << 1;
+       buf[2] = buf[3] = 0;
+       do {
+               int pkt_len = min(FT4222_TX_HDRLEN + len - written, +                                 FT4222_BULK_MAXLEN);

Quoted 8 lines

+               bool first_pkt = (written == 0);
+               bool last_pkt = (written + pkt_len == FT4222_TX_HDRLEN
+ len);
+               int ret, actual_len;
+
+               buf[1] = 0;
+               if (first_pkt)
+                       buf[1] |= flags & FT4222_FLAG_RESTART;

Quoted 1 line

Aren't we losing the _START case here?

The _RESTART flag is 0x3, and the _START flag is 0x02, so by applying the _RESTART mask we are including the _START case too. But the code shouldn't depend on this detail, I will change it to flags & (FT4222_FLAG_START | FT4222_FLAG_RESTART).