From: Johan Hovold <johan@kernel.org>
To: Karoly Pados <pados@pados.hu>
Cc: Johan Hovold <johan@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
Loic Poulain <loic.poulain@linaro.org>
Subject: [v7] USB: serial: ftdi_sio: implement GPIO support for FT-X devices
Date: Wed, 26 Sep 2018 12:42:50 +0200 [thread overview]
Message-ID: <20180926104250.GJ3332@localhost> (raw)
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@pados.hu>
> ---
> 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
next reply other threads:[~2018-09-26 10:42 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-09-26 10:42 Johan Hovold [this message]
-- strict thread matches above, loose matches on Subject: below --
2018-09-25 13:59 [v7] USB: serial: ftdi_sio: implement GPIO support for FT-X devices Karoly Pados
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180926104250.GJ3332@localhost \
--to=johan@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=loic.poulain@linaro.org \
--cc=pados@pados.hu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).