Skip to contentSkip to conversation

USB: serial: ftdi_sio: add support for FT232H CBUS gpios

PATCH In review Matthew Michilot 3 participants 5 messages Aug 8, 2019 – Aug 28, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 USB: serial: ftdi_sio: add support for FT232H CBUS gpios

Enable support for cbus gpios on FT232H. The cbus configuration is stored in one word in the EEPROM at byte-offset 0x1a with the mux config for ACBUS5, ACBUS6, ACBUS8 and ACBUS9 (only pins that can be configured as I/O mode).

Tested using FT232H by configuring one ACBUS pin at a time.

---
 drivers/usb/serial/ftdi_sio.c | 43 +++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
drivers/usb/serial/ftdi_sio.c +43 -0
index 4b3a049561f3..c8d35faa8f61 100644
@@ -2023,6 +2023,46 @@ static int ftdi_read_eeprom(struct usb_serial *serial, void *dst, u16 addr,
 	return 0;
 }
 
+static int ftdi_gpio_init_ft232h(struct usb_serial_port *port)
+{
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+	u8 *buf;
+	u16 cbus_config;
+	int ret;
+	int i;
+
+	buf = kmalloc(2, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	ret = ftdi_read_eeprom(port->serial, buf, 0x1A, 4);
+	if (ret < 0)
+		goto out_free;
+
+	/*
+	 * FT232H CBUS Memory Map
+	 *
+	 * 0x1A: 8X (upper nibble -> AC5)
+	 * 0x1B: X8 (lower nibble -> AC6)
+	 * 0x1C: 88 (upper nibble -> AC8 | lower nibble -> AC9)
+	 */
+	cbus_config = (((buf[0] & 0xf0) | (buf[1] & 0xf)) << 8 | buf[2]);
+
+	priv->gc.ngpio = 4;
+	priv->gpio_altfunc = 0xff;
+
+	for (i = 0; i < priv->gc.ngpio; ++i) {
+		if ((cbus_config & 0xf) == FTDI_FTX_CBUS_MUX_GPIO)
+			priv->gpio_altfunc &= ~BIT(i);
+		cbus_config >>= 4;
+	}
+
+out_free:
+	kfree(buf);
+
+	return ret;
+}
+
 static int ftdi_gpio_init_ft232r(struct usb_serial_port *port)
 {
 	struct ftdi_private *priv = usb_get_serial_port_data(port);
@@ -2104,6 +2144,9 @@ static int ftdi_gpio_init(struct usb_serial_port *port)
 	case FTX:
 		result = ftdi_gpio_init_ftx(port);
 		break;
+	case FT232H:
+		result = ftdi_gpio_init_ft232h(port);
+		break;
 	default:
 		return 0;
 	}
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 2 lines · Matthew Michilot

Enable support for cbus gpios on FT232H. The cbus configuration is stored in one word in the EEPROM at byte-offset 0x1a with the mux

It seems to be stored in two words.

Quoted 6 lines

config for ACBUS5, ACBUS6, ACBUS8 and ACBUS9 (only pins that can be configured as I/O mode).

Tested using FT232H by configuring one ACBUS pin at a time.

typo: Reviewed-by

Quoted 16 lines
---
 drivers/usb/serial/ftdi_sio.c | 43 +++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
drivers/usb/serial/ftdi_sio.c +3 -0
index 4b3a049561f3..c8d35faa8f61 100644
@@ -2023,6 +2023,46 @@ static int ftdi_read_eeprom(struct usb_serial *serial, void *dst, u16 addr,
 	return 0;
 }
 
+static int ftdi_gpio_init_ft232h(struct usb_serial_port *port)
+{
+	struct ftdi_private *priv = usb_get_serial_port_data(port);

drivers/usb/serial/ftdi_sio.c static int ftdi_read_eeprom(struct usb_serial *serial, void *dst, u16 addr, · line 2038

Quoted 10 lines
drivers/usb/serial/ftdi_sio.c +10 -0
+	u8 *buf;
+	u16 cbus_config;
+	int ret;
+	int i;
+
+	buf = kmalloc(2, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	ret = ftdi_read_eeprom(port->serial, buf, 0x1A, 4);

You allocate 2 byte above, but write 4 bytes into buf here.

Please also use lower-case hex notation consistently throughout.

drivers/usb/serial/ftdi_sio.c static int ftdi_read_eeprom(struct usb_serial *serial, void *dst, u16 addr, · line 2049

Quoted 11 lines
drivers/usb/serial/ftdi_sio.c +11 -0
+	if (ret < 0)
+		goto out_free;
+
+	/*
+	 * FT232H CBUS Memory Map
+	 *
+	 * 0x1A: 8X (upper nibble -> AC5)
+	 * 0x1B: X8 (lower nibble -> AC6)
+	 * 0x1C: 88 (upper nibble -> AC8 | lower nibble -> AC9)
+	 */
+	cbus_config = (((buf[0] & 0xf0) | (buf[1] & 0xf)) << 8 | buf[2]);

Have you verified the order you use here by enabling one gpio at a time and toggling it? The reason I ask is that the above would give a mapping of

	gpio0 -> AC9
	gpio1 -> AC8
	gpio2 -> AC6
	gpio4 -> AC5

which looks backwards but may be correct.

Also please drop the outer parentheses in the above expression.

Quoted 20 lines
drivers/usb/serial/ftdi_sio.c +16 -0
+
+	priv->gc.ngpio = 4;
+	priv->gpio_altfunc = 0xff;
+
+	for (i = 0; i < priv->gc.ngpio; ++i) {
+		if ((cbus_config & 0xf) == FTDI_FTX_CBUS_MUX_GPIO)
+			priv->gpio_altfunc &= ~BIT(i);
+		cbus_config >>= 4;
+	}
+
+out_free:
+	kfree(buf);
+
+	return ret;
+}
+
 static int ftdi_gpio_init_ft232r(struct usb_serial_port *port)
 {
 	struct ftdi_private *priv = usb_get_serial_port_data(port);

drivers/usb/serial/ftdi_sio.c static int ftdi_gpio_init(struct usb_serial_port *port) · line 2149

Quoted 6 lines
drivers/usb/serial/ftdi_sio.c +3 -0
@@ -2104,6 +2144,9 @@ static int ftdi_gpio_init(struct usb_serial_port *port)
 	case FTX:
 		result = ftdi_gpio_init_ftx(port);
 		break;
+	case FT232H:
+		result = ftdi_gpio_init_ft232h(port);
+		break;

Please keep the case labels sorted alphabetically.

drivers/usb/serial/ftdi_sio.c static int ftdi_gpio_init(struct usb_serial_port *port) · line 2152

Quoted 3 lines
drivers/usb/serial/ftdi_sio.c +0 -0
 	default:
 		return 0;
 	}

Johan

maintainer
#2 lore raw
In reply to Matthew Michilot
Quoted 9 lines · Matthew Michilot

Enable support for cbus gpios on FT232H. The cbus configuration is stored in one word in the EEPROM at byte-offset 0x1a with the mux config for ACBUS5, ACBUS6, ACBUS8 and ACBUS9 (only pins that can be configured as I/O mode).

Tested using FT232H by configuring one ACBUS pin at a time.

Also make sure your SoB matches the From line.

Johan

#3 lore raw

PATCH v2 1/1 USB: serial: ftdi_sio: add support for FT232H CBUS gpios

In reply to Matthew Michilot

Enable support for cbus gpios on FT232H. The cbus configuration is stored in one word in the EEPROM at byte-offset 0x1a with the mux config for ACBUS5, ACBUS6, ACBUS8 and ACBUS9 (only pins that can be configured as I/O mode).

Tested using FT232H by configuring one ACBUS pin at a time.

---
V2:
- made hex notation all lowercase for consistency
- allocated 4 bytes instead of 2 bytes for buffer
- improved documentation on FT232H CBUS memory mapping
- fixed CBUS pins being incorrectly mapped to the wrong gpios
- sorted case labels alphabetically

---
 drivers/usb/serial/ftdi_sio.c | 43 +++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
drivers/usb/serial/ftdi_sio.c +43 -0
index 4b3a049561f3..01e4ecccd079 100644
@@ -2023,6 +2023,46 @@ static int ftdi_read_eeprom(struct usb_serial *serial, void *dst, u16 addr,
 	return 0;
 }
 
+static int ftdi_gpio_init_ft232h(struct usb_serial_port *port)
+{
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+	u8 *buf;
+	u16 cbus_config;
+	int ret;
+	int i;
+
+	buf = kmalloc(4, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	ret = ftdi_read_eeprom(port->serial, buf, 0x1a, 4);
+	if (ret < 0)
+		goto out_free;
+
+	/*
+	 * FT232H CBUS Memory Map
+	 *
+	 * 0x1a: X- (upper nibble -> AC5)
+	 * 0x1b: -X (lower nibble -> AC6)
+	 * 0x1c: XX (upper nibble -> AC9 | lower nibble -> AC8)
+	 */
+	cbus_config = buf[2] << 8 | (buf[1] & 0xf) << 4 | (buf[0] & 0xf0) >> 4;
+
+	priv->gc.ngpio = 4;
+	priv->gpio_altfunc = 0xff;
+
+	for (i = 0; i < priv->gc.ngpio; ++i) {
+		if ((cbus_config & 0xf) == FTDI_FTX_CBUS_MUX_GPIO)
+			priv->gpio_altfunc &= ~BIT(i);
+		cbus_config >>= 4;
+	}
+
+out_free:
+	kfree(buf);
+
+	return ret;
+}
+
 static int ftdi_gpio_init_ft232r(struct usb_serial_port *port)
 {
 	struct ftdi_private *priv = usb_get_serial_port_data(port);
@@ -2098,6 +2138,9 @@ static int ftdi_gpio_init(struct usb_serial_port *port)
 	int result;
 
 	switch (priv->chip_type) {
+	case FT232H:
+		result = ftdi_gpio_init_ft232h(port);
+		break;
 	case FT232RL:
 		result = ftdi_gpio_init_ft232r(port);
 		break;
Signature · 1 line
2.17.1
maintainer
#4 lore raw
Quoted 2 lines · Matthew Michilot

Enable support for cbus gpios on FT232H. The cbus configuration is stored in one word in the EEPROM at byte-offset 0x1a with the mux

You forgot to fix the copy-paste error here; it should be "two words" as I mentioned before.

Quoted 7 lines

config for ACBUS5, ACBUS6, ACBUS8 and ACBUS9 (only pins that can be configured as I/O mode).

Tested using FT232H by configuring one ACBUS pin at a time.

Quoted 7 lines

---
V2:
- made hex notation all lowercase for consistency
- allocated 4 bytes instead of 2 bytes for buffer
- improved documentation on FT232H CBUS memory mapping
- fixed CBUS pins being incorrectly mapped to the wrong gpios
- sorted case labels alphabetically

Now applied after amending the commit message.

Thanks,
Johan