Re: [PATCH v7] USB: serial: ftdi_sio: implement GPIO support for FT-X devices
From: Johan Hovold
Date: Wed Sep 26 2018 - 06:42:57 EST
On Tue, Sep 25, 2018 at 03:59:11PM +0200, Karoly Pados wrote:
> This patch allows using the CBUS pins of FT-X devices as GPIO in CBUS
> bitbanging mode. There is no conflict between the GPIO and VCP
> functionality in this mode. Tested on FT230X and FT231X.
>
> As there is no way to request the current CBUS register configuration
> from the device, all CBUS pins are set to a known state when the first
> GPIO is requested. This allows using libftdi to set the GPIO pins
> before loading this module for UART functionality, a behavior that
> existing applications might be relying upon (though no specific case
> is known to the authors of this patch).
>
> Signed-off-by: Karoly Pados <pados@xxxxxxxx>
> ---
> Changelog:
> - v2: Fix compile error when CONFIG_GPIOLIB is not defined.
> - v3: Incorporate review feedback.
> - v4: Include linux/gpio/driver.h unconditionally.
> Replace and invert gpio_input with gpio_output.
> Make ftdi_gpio_direction_get return 0/1.
> Change dev_err msg in ftdi_set_bitmode_req.
> Change formatting of error checking in ftdi_gpio_get.
> Drop dev_err in ftdi_gpio_set.
> Remove some line breaks and empty lines.
> Change error handling in ftdi_read_eeprom (and adjust caller).
> Replace SIO->FTX in FTDI_SIO_CBUS_MUX_GPIO macro name.
> - v5: Resent v4 with no changes by mistake. Please ignore.
> - v6: Read only 4 bytes from eeprom in ftx_gpioconf_init.
> Compare ftdi_read_eeprom result with 0 instead of eq. cehck.
> Reserve 4 GPIOs even for FT234X.
> Release CBUS after gpiochip deregister to avoid possible race.
> Adjust comment on FTDI_SIO_SET_BITMODE macro.
> Protect GPIO value/dir setting with mutex.
> Add support for gpiochip.get_multiple and set_multiple.
> Add names to GPIO lines.
> - v7: Move <linux/gpio/driver.h> line for better include sorting.
> Adjust comment on gpio_lock declaration.
> Space vs tab formatting changes in struct ftdi_private.
> Move ftdi_ftx_gpio_names to merge it with existing #ifdef.
> Rename function ftdi_set_bitmode_req to ftdi_set_bitmode.
> Rename rcvbuf to buf in function ftdi_read_cbus_pins.
> AND *bits and *mask in ftdi_gpio_set_multiple as gpiolib does not.
> Replace if in ftdi_gpio_direction_get with negation.
> Move priv->gc.names assignment to chip-specific ftx_gpioconf_init.
Thanks again for doing this work. I've applied the patch now with some
minor style changes (see below).
> +#if defined(CONFIG_GPIOLIB)
I changed these to plain #ifdef, which is what we commonly use.
> +static int ftdi_gpio_get_multiple(struct gpio_chip *gc,
> + unsigned long *mask, unsigned long *bits)
You had some odd indentation using spaces after tabs, but which still
didn't line up with opening parenthesis (a left over from removing the
_sio_ infix, I think) which I fixed up by using plain tabs instead.
> +static int ftx_gpioconf_init(struct usb_serial_port *port)
> +{
> + struct ftdi_private *priv = usb_get_serial_port_data(port);
> + struct usb_serial *serial = port->serial;
> + const u16 cbus_cfg_addr = 0x1a;
> + const u16 cbus_cfg_size = 4;
> + u8 *cbus_cfg_buf;
> + int result;
> + u8 i;
> +
> + priv->gc.names = ftdi_ftx_gpio_names;
I moved this to where you set ngpio below as they are related (and
need not be set if we bail out early).
> +
> + /* Read a part of device EEPROM */
> + cbus_cfg_buf = kmalloc(cbus_cfg_size, GFP_KERNEL);
> + if (!cbus_cfg_buf)
> + return -ENOMEM;
> +
> + result = ftdi_read_eeprom(serial, cbus_cfg_buf,
> + cbus_cfg_addr, cbus_cfg_size);
> + if (result < 0)
> + goto out_free;
> +
> + /* FIXME: FT234XD alone has 1 GPIO, but how to recognize this IC? */
> + priv->gc.ngpio = 4;
> +
> + /* Determine which pins are configured for CBUS bitbanging */
> + priv->gpio_altfunc = 0xff;
> + for (i = 0; i < priv->gc.ngpio; ++i) {
> + if (cbus_cfg_buf[i] == FTDI_FTX_CBUS_MUX_GPIO)
> + priv->gpio_altfunc &= ~BIT(i);
> + }
> +
> +out_free:
> + kfree(cbus_cfg_buf);
> +
> + return result;
> +}
Thanks,
Johan