i2c: Add FTDI FT4222H USB I2C adapter
PATCH 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 USB communication code in the driver contains several magic numbers that have been retrieved by sniffing USB traffic generated with the FTDI FT4222 userspace library.
--- .../ABI/testing/sysfs-bus-i2c-devices-ft4222 | 7 + drivers/i2c/busses/Kconfig | 11 + drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-ft4222.c | 425 ++++++++++++++++++ 4 files changed, 444 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 +425 -0
425 lines not on this page · raw message
Signature · 1 line
2.39.5
Andi Shyti
maintainerHi Francesco,
I did just a quick initial review.
Quoted 6 lines · Francesco Lavra
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 USB communication code in the driver contains several magic numbers that have been retrieved by sniffing USB traffic generated with the FTDI FT4222 userspace library.
In any case, magic numbers need to be documented and/or defined in the code.
...
drivers/i2c/busses/i2c-ft4222.c line 51
Quoted 11 lines
drivers/i2c/busses/i2c-ft4222.c new file +11
-0+enum ft4222_conf_mode { + ft4222_conf0, + ft4222_conf12, + ft4222_conf3, +}; + +enum ft4222_sys_clk { + ft4222_sys_clk_60 =0 , + ft4222_sys_clk_24, + ft4222_sys_clk_48, + ft4222_sys_clk_80,
can we make these enums capital letters? Otherwise they will look more like variables or structs.
Quoted 1 line
+};
...
drivers/i2c/busses/i2c-ft4222.c line 76
Quoted 6 lines
drivers/i2c/busses/i2c-ft4222.c new file +6
-0+static int ft4222_cmd_get (struct ft4222_i2c *ftdi,u16 cmd,u8 *val) +{ +struct usb_device *udev = ftdi->udev; +int ret =usb_control_msg (udev,usb_rcvctrlpipe (udev,0 ),0x20 ,0xC0 , + cmd,0x0001 , ftdi->ubuf,sizeof (*val), +FT4222_IO_TIMEOUT );
Please, avoid to call important functions in the declaration parts.
Give them more visibility by calling these functions in it's own line and section.
drivers/i2c/busses/i2c-ft4222.c line 87
Quoted 11 lines
drivers/i2c/busses/i2c-ft4222.c new file +11
-0+ +if (ret ==sizeof (*val)) + *val = ftdi->ubuf[0 ]; +else if (ret >=0 )/* unexpected number of bytes transferred */ + ret = -EIO ; +return ret; +} + +static int ft4222_i2c_reset (struct ft4222_i2c *ftdi) +{ +return ft4222_cmd_set (ftdi,0x51 ,1 );
what is 0x51?
what is '1'?
drivers/i2c/busses/i2c-ft4222.c line 94
Quoted 7 lines
drivers/i2c/busses/i2c-ft4222.c new file +7
-0+} + +static int ft4222_i2c_get_status (struct ft4222_i2c *ftdi) +{ +u8 status; +int retry; +const int max_retries =512 ;
why 512?
drivers/i2c/busses/i2c-ft4222.c line 105
Quoted 10 lines
drivers/i2c/busses/i2c-ft4222.c new file +10
-0+for (retry =0 ; retry < max_retries; retry++) { +int ret =ft4222_cmd_get (ftdi,0xf5b4 , &status); + +if (ret <0 ) +return ret; +if (!(status &FT4222_I2C_CTRL_BUSY )) +break ; + } +dev_dbg (&ftdi->adapter.dev,"status 0x%02x (%d retries)" , status, + retry);
whould this debug message be printed after the check below?
Quoted 13 lines
drivers/i2c/busses/i2c-ft4222.c new file +13
-0+if (retry == max_retries) { +ft4222_i2c_reset (ftdi); +return -ETIMEDOUT ; + } +if (!(status &FT4222_I2C_ERROR )) +return 0 ; +if (status &FT4222_I2C_ADDR_NACK ) +return -ENXIO ; +else if (status &FT4222_I2C_DATA_NACK ) +return -EIO ; +else +return -EBUSY ; +}
...
drivers/i2c/busses/i2c-ft4222.c line 258
Quoted 13 lines
drivers/i2c/busses/i2c-ft4222.c new file +13
-0+static int ft4222_i2c_setup (struct ft4222_i2c *ftdi,unsigned int freq) +{ +bool hi_freq = (freq >I2C_MAX_FAST_MODE_FREQ ); +const int m = hi_freq ?6 :8 ; +u8 n; +int ret; + +if ((freq <I2C_MAX_STANDARD_MODE_FREQ ) || + (freq >I2C_MAX_HIGH_SPEED_MODE_FREQ )) +return -EINVAL ; + n =DIV_ROUND_UP (ftdi->sys_clk, freq * m) -1 ; +if (hi_freq) + n |=0xc0 ;
what is 0xc0?
drivers/i2c/busses/i2c-ft4222.c line 259
Quoted 1 line
drivers/i2c/busses/i2c-ft4222.c new file +1
-0+ ret =ft4222_cmd_set (ftdi,0x52 , n);
what is 0x52?
drivers/i2c/busses/i2c-ft4222.c line 262
Quoted 3 lines
drivers/i2c/busses/i2c-ft4222.c new file +3
-0+if (ret <0 ) +return ret; + ret =ft4222_cmd_set (ftdi,0x05 ,1 );
what is 0x05?
Quoted 5 lines
drivers/i2c/busses/i2c-ft4222.c new file +5
-0+if (ret <0 ) +return ret; + ftdi->freq = freq; +return 0 ; +}
...
drivers/i2c/busses/i2c-ft4222.c line 317
Quoted 4 lines
drivers/i2c/busses/i2c-ft4222.c new file +4
-0+static int ft4222_i2c_init (struct usb_interface *interface) +{ +struct device *dev = &interface->dev; +struct ft4222_i2c *ftdi =devm_kmalloc (dev,sizeof (*ftdi),GFP_KERNEL );
Please, don't initialize here with kmalloc.
drivers/i2c/busses/i2c-ft4222.c line 321
Quoted 4 lines
drivers/i2c/busses/i2c-ft4222.c new file +4
-0+int ret; +u8 sys_clk_enum; +unsigned int sys_clk; +struct i2c_adapter *adapter;
super nitpick: sort variables by line length, from the longest to the shortest, in a reverse christmas tree order.
Quoted 1 line
+
please, initialize here with kmalloc.
Quoted 34 lines
drivers/i2c/busses/i2c-ft4222.c new file +34
-0+if (!ftdi) +return -ENOMEM ; + ftdi->udev =interface_to_usbdev (interface); + ret =ft4222_cmd_get (ftdi,0x0004 , &sys_clk_enum); +if (ret <0 ) +return ret; +switch (sys_clk_enum) { +case ft4222_sys_clk_60: + sys_clk =60000000 ; +break ; +case ft4222_sys_clk_24: + sys_clk =24000000 ; +break ; +case ft4222_sys_clk_48: + sys_clk =48000000 ; +break ; +case ft4222_sys_clk_80: + sys_clk =80000000 ; +break ; +default : +dev_err (dev,"unknown system clock setting %d" , sys_clk_enum); +return -EOPNOTSUPP ; + } + ftdi->sys_clk = sys_clk; + ret =ft4222_i2c_setup (ftdi,I2C_MAX_FAST_MODE_FREQ ); +if (ret <0 ) +return ret; + ret =ft4222_i2c_reset (ftdi); +if (ret <0 ) +return ret; + adapter = &ftdi->adapter; +memset (adapter,0 ,sizeof (*adapter)); + adapter->owner =THIS_MODULE ; + adapter->algo = &ft4222_i2c_algo;
drivers/i2c/busses/i2c-ft4222.c line 366
Quoted 10 lines
drivers/i2c/busses/i2c-ft4222.c new file +10
-0+ adapter->dev.parent = dev; + adapter->dev.groups = ft4222_attr_groups; +snprintf (adapter->name,sizeof (adapter->name), +"FT4222 USB-to-I2C %03d-%03d" , ftdi->udev->bus->busnum, + ftdi->udev->devnum); + ret =devm_i2c_add_adapter (dev, adapter); +if (ret <0 ) +return ret; +dev_dbg (&adapter->dev,"system clock frequency %d Hz" , sys_clk); +return 0 ;
I see that you don't like blank lines, but sometimes, in my opinion, separating the blocks with a blank line, might improve the readability.
Quoted 6 lines
drivers/i2c/busses/i2c-ft4222.c new file +6
-0+} + +static int ft4222_get_conf (struct usb_interface *interface, +enum ft4222_conf_mode *conf_mode) +{ +struct usb_device *udev =interface_to_usbdev (interface);
drivers/i2c/busses/i2c-ft4222.c line 382
Quoted 10 lines
drivers/i2c/busses/i2c-ft4222.c new file +10
-0+u16 dev_type = udev->descriptor.bcdDevice; + +switch (dev_type >>8 ) { +case 0x17 : + *conf_mode = ft4222_conf3; +break ; +case 0x18 : + *conf_mode = ft4222_conf0; +break ; +case 0x19 :
can we give a define to these dev_types?
Quoted 8 lines
drivers/i2c/busses/i2c-ft4222.c new file +8
-0+ *conf_mode = ft4222_conf12; +break ; +default : +return -EOPNOTSUPP ; + } +return 0 ; +} +
drivers/i2c/busses/i2c-ft4222.c line 400
Quoted 10 lines
drivers/i2c/busses/i2c-ft4222.c new file +10
-0+static int ft4222_i2c_probe (struct usb_interface *interface, +const struct usb_device_id *id) +{ +enum ft4222_conf_mode conf_mode; +int ret =ft4222_get_conf (interface, &conf_mode); +int intf = interface->cur_altsetting->desc.bInterfaceNumber; + +if (ret) +return ret; + if (((conf_mode == ft4222_conf0) || (conf_mode == ft4222_conf3)) &&
what about conf12?
drivers/i2c/busses/i2c-ft4222.c line 403
Quoted 3 lines
drivers/i2c/busses/i2c-ft4222.c new file +3
-0+ (intf == 0)) +return ft4222_i2c_init (interface); +return -ENODEV ;
I'd prefer to revert the conditions and end up in success rather than with failure:
if (((conf_mode != ft4222_conf0) && (conf_mode == ft4222_conf3)) || (intf != 0)) return -ENODEV; return ft4222_i2c_init(interface);
Quoted 1 line
+}
Thanks,
Andi
Hi Andi,
Quoted 3 lines · Andi Shyti
Hi Francesco,
I did just a quick initial review.
...
Quoted 7 lines
drivers/i2c/busses/i2c-ft4222.c line 94
Quoted 5 lines
drivers/i2c/busses/i2c-ft4222.c new file +5
-0+static int ft4222_i2c_get_status (struct ft4222_i2c *ftdi) +{ + u8 status; + int retry; + const int max_retries = 512;why 512?
These retries are needed mostly when retrieving the status after doing a write with the I2C bus operating at a low speed.
Doing various tests at 100 kHz, I saw 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 we may need more retries, for example if I disconnect the SCL line and then try to do a write, I see that up to 64 retries are needed.
So I guess 512 max_retries is too much, but we need a value > 64. Does 128 sound OK to you? Perhaps I can add a comment with the above observations.
Quoted 12 lines
Quoted 11 lines
+ for (retry = 0; retry <
max_retries; retry++) { + int ret =ft4222_cmd_get(ftdi, 0xf5b4, &status); +
+ if (ret < 0)
+ return ret;
+ if (!(status &FT4222_I2C_CTRL_BUSY))
+ break;
+ }
+ dev_dbg(&ftdi->adapter.dev, "status 0x%02x (%d retries)", status,
+ retry);
Quoted 1 line
whould this debug message be printed after the check below?
Not sure I understood your question. Are you suggesting to move the dev_dbg() call after the check below? I put it before the check so that I get a debug message even if the maximum number of retries has been reached.
Quoted 13 lines
Quoted 13 lines
+ if (retry ==
max_retries) {
+ ft4222_i2c_reset(ftdi);
+ return-ETIMEDOUT;
+ }
+ if (!(status &FT4222_I2C_ERROR))
+ return 0;
+ if (status &FT4222_I2C_ADDR_NACK)
+ return-ENXIO;
+ else if (status &FT4222_I2C_DATA_NACK)
+ return-EIO;
+ else
+ return-EBUSY;
+}
...
Quoted 5 lines
Quoted 5 lines
drivers/i2c/busses/i2c-ft4222.c new file +5
-0+static int ft4222_i2c_probe (struct usb_interface *interface, + const struct usb_device_id *id) +{ + enum ft4222_conf_mode conf_mode; + int ret = ft4222_get_conf(interface, &conf_mode);
Quoted 8 lines
Quoted 6 lines
+ int intf =
interface->cur_altsetting->desc.bInterfaceNumber; +
+ if (ret)
+ return ret;
+ if (((conf_mode==ft4222_conf0) || (conf_mode==ft4222_conf3)) &&what about conf12?
As mentioned in the commit message, the I2C functionality is available only when the chip is configured in mode 0 or 3. In modes 1 and 2, the chip implements other functionalities, e.g. SPI.
I will add a comment in this function.
Quoted 3 lines
Quoted 3 lines
+ (intf == 0))
+ returnft4222_i2c_init(interface); + return-ENODEV;
Andi Shyti
maintainerQuoted 15 lines · Andi Shyti
Quoted 3 lines
Hi Francesco,
I did just a quick initial review.
...
Quoted 7 lines
drivers/i2c/busses/i2c-ft4222.c line 94
Quoted 5 lines
drivers/i2c/busses/i2c-ft4222.c new file +5
-0+static int ft4222_i2c_get_status (struct ft4222_i2c *ftdi) +{ + u8 status; + int retry; + const int max_retries = 512;why 512?
Quoted 9 lines
These retries are needed mostly when retrieving the status after doing a write with the I2C bus operating at a low speed.
Doing various tests at 100 kHz, I saw that after a maximum-sized (512-byte) write, up to 11 retries are needed before the chip clears itsCTRL_BUSYflag. But under certain conditions we may need more retries, for example if I disconnect the SCL line and then try to do a write, I see that up to 64 retries are needed.
So I guess 512max_retriesis too much, but we need a value > 64. Does 128 sound OK to you? Perhaps I can add a comment with the above observations.
All constants need to have a comment or defined or both. A good comment would definitely help. As for the number of retries it looks a bit too much to me, but you definitely know better.
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
+ for (retry = 0; retry <
max_retries; retry++) { + int ret =ft4222_cmd_get(ftdi, 0xf5b4, &status); +
+ if (ret < 0)
+ return ret;
+ if (!(status &FT4222_I2C_CTRL_BUSY))
+ break;
+ }
+ dev_dbg(&ftdi->adapter.dev, "status 0x%02x (%d retries)", status,
+ retry);
Quoted 5 lines
Quoted 1 line
whould this debug message be printed after the check below?
Not sure I understood your question. Are you suggesting to move the
dev_dbg()call after the check below? I put it before the check so that I get a debug message even if the maximum number of retries has been reached.
It looks to me more like a spam message or a spurious printout after a debugging session, rather than an informative message.
I'd remove it, but if you think that's really useful, then you can keep it.
Quoted 30 lines
Quoted 13 lines
Quoted 13 lines
+ if (retry ==
max_retries) {
+ ft4222_i2c_reset(ftdi);
+ return-ETIMEDOUT;
+ }
+ if (!(status &FT4222_I2C_ERROR))
+ return 0;
+ if (status &FT4222_I2C_ADDR_NACK)
+ return-ENXIO;
+ else if (status &FT4222_I2C_DATA_NACK)
+ return-EIO;
+ else
+ return-EBUSY;
+}...
Quoted 5 lines
Quoted 5 lines
drivers/i2c/busses/i2c-ft4222.c new file +5
-0+static int ft4222_i2c_probe (struct usb_interface *interface, + const struct usb_device_id *id) +{ + enum ft4222_conf_mode conf_mode; + int ret = ft4222_get_conf(interface, &conf_mode);Quoted 8 lines
Quoted 6 lines
+ int intf =
interface->cur_altsetting->desc.bInterfaceNumber; +
+ if (ret)
+ return ret;
+ if (((conf_mode==ft4222_conf0) || (conf_mode==ft4222_conf3)) &&what about conf12?
Quoted 4 lines
As mentioned in the commit message, the I2C functionality is available only when the chip is configured in mode 0 or 3. In modes 1 and 2, the chip implements other functionalities, e.g. SPI.
I will add a comment in this function.
What prevents us from removing conf12 at all?
Andi
Quoted 31 lines · Andi Shyti
Quoted 15 lines · Andi Shyti
Quoted 3 lines
Hi Francesco,
I did just a quick initial review.
...
Quoted 7 lines
drivers/i2c/busses/i2c-ft4222.c line 94
Quoted 5 lines
drivers/i2c/busses/i2c-ft4222.c new file +5
-0+static int ft4222_i2c_get_status (struct ft4222_i2c *ftdi) +{ + u8 status; + int retry; + const int max_retries = 512;why 512?
Quoted 15 lines
These retries are needed mostly when retrieving the status after doing a
write with the I2C bus operating at a low speed.
Doing various tests at 100 kHz, I saw that after a maximum-sized (512- byte)
write, up to 11 retries are needed before the chip clears itsCTRL_BUSYflag. But under certain conditions we may need more retries, for example if
I disconnect the SCL line and then try to do a write, I see that up to 64
retries are needed.
So I guess 512max_retriesis too much, but we need a value > 64. Does 128
sound OK to you? Perhaps I can add a comment with the above observations.
Quoted 3 lines
All constants need to have a comment or defined or both. A good comment would definitely help. As for the number of retries it looks a bit too much to me, but you definitely know better.
OK, I added a comment and changed the constant to 70.
Quoted 20 lines
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
+ for (retry = 0; retry <
max_retries; retry++) { + int ret =ft4222_cmd_get(ftdi, 0xf5b4, &status); +
+ if (ret < 0)
+ return ret;
+ if (!(status &FT4222_I2C_CTRL_BUSY))
+ break;
+ }
+ dev_dbg(&ftdi->adapter.dev, "status 0x%02x (%d retries)", status,
+ retry);Quoted 7 lines
Quoted 1 line
whould this debug message be printed after the check below?
Not sure I understood your question. Are you suggesting to move the
dev_dbg()call after the check below? I put it before the check so that I
get a debug message even if the maximum number of retries has been reached.
Quoted 5 lines
It looks to me more like a spam message or a spurious printout after a debugging session, rather than an informative message.
I'd remove it, but if you think that's really useful, then you can keep it.
Removed.
Quoted 31 lines
Quoted 31 lines
Quoted 13 lines
Quoted 13 lines
+ if (retry ==
max_retries) {
+ ft4222_i2c_reset(ftdi);
+ return-ETIMEDOUT;
+ }
+ if (!(status &FT4222_I2C_ERROR))
+ return 0;
+ if (status &FT4222_I2C_ADDR_NACK)
+ return-ENXIO;
+ else if (status &FT4222_I2C_DATA_NACK)
+ return-EIO;
+ else
+ return-EBUSY;
+}...
Quoted 14 lines
Quoted 7 lines
drivers/i2c/busses/i2c-ft4222.c new file +6
-0+static int ft4222_i2c_probe (struct usb_interface *interface, + const struct usb_device_id *id) +{ + enum ft4222_conf_mode conf_mode; + int ret = ft4222_get_conf(interface, &conf_mode); + int intf = interface->cur_altsetting-Quoted 1 line
desc.bInterfaceNumber;
Quoted 5 lines
+
+ if (ret)
+ return ret;
+ if (((conf_mode==ft4222_conf0) || (conf_mode==ft4222_conf3)) &&what about conf12?
Quoted 7 lines
Quoted 5 lines
As mentioned in the commit message, the I2C functionality is available only
when the chip is configured in mode 0 or 3. In modes 1 and 2, the chip implements other functionalities, e.g. SPI.
I will add a comment in this function.What prevents us from removing conf12 at all?
OK, removed