USB: serial: ftdi_sio: add support for FT232H CBUS gpios
authorMatthew Michilot <matthew.michilot@gmail.com>
Thu, 15 Aug 2019 17:40:54 +0000 (17:40 +0000)
committerJohan Hovold <johan@kernel.org>
Wed, 28 Aug 2019 13:35:33 +0000 (15:35 +0200)
Enable support for cbus gpios on FT232H. The cbus configuration is
stored in two words 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.

Reviewed-by: Tim Harvey <tharvey@gateworks.com>
Signed-off-by: Matthew Michilot <matthew.michilot@gmail.com>
[ johan: fix copy-paste error in commit message ]
Signed-off-by: Johan Hovold <johan@kernel.org>
drivers/usb/serial/ftdi_sio.c

index 4b3a049561f373d52958b0a0e80ec295d63eb4b6..f0688c44b04c329d7df99ebfe9e2a9a8ed7b6527 100644 (file)
@@ -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);
+       u16 cbus_config;
+       u8 *buf;
+       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;