All of lore.kernel.org
 help / color / mirror / Atom feed
From: Johan Hovold <johan@kernel.org>
To: Karoly Pados <pados@pados.hu>, Loic Poulain <loic.poulain@linaro.org>
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	Johan Hovold <johan@kernel.org>
Subject: [RFC] USB: serial: ftdi_sio: add support for FT232R CBUS gpios
Date: Wed,  5 Sep 2018 12:49:04 +0200	[thread overview]
Message-ID: <20180905104904.15325-1-johan@kernel.org> (raw)

Enable support for the CBUS gpios on FT232R. The cbus configuration is
stored in one word in the EEPROM at offset 0x0a with the mux config for
CBUS1, CBUS2, CBUS3 and CBUS4 in bits 0..3, 4..7, 8..11 and 12..15,
respectively.

Tested using FT232RL with all CBUS pins in IO mode.

Applies on top of Karoly's CBUS gpio patch.

FIXME: use common eeprom_read helper
FIXME: return the number of gpios instead of setting gc.ngpio?
FIXME: verify each mux setting
Signed-off-by: Johan Hovold <johan@kernel.org>
---

This is a cleaned up version of some code I've been using to test
Karoly's patch using an FT232RL in case anyone wants to give that a try.

May also be useful to get an idea of how the current setup code can be
refactored. This one needs to be rebased to use a common eeprom helper
eventually for example.

Johan


 drivers/usb/serial/ftdi_sio.c | 49 +++++++++++++++++++++++++++++++++++
 drivers/usb/serial/ftdi_sio.h |  1 +
 2 files changed, 50 insertions(+)

diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c
index 1df9c553710b..4d63863bfb3f 100644
--- a/drivers/usb/serial/ftdi_sio.c
+++ b/drivers/usb/serial/ftdi_sio.c
@@ -43,6 +43,9 @@
 #ifdef CONFIG_GPIOLIB
 #include <linux/gpio/driver.h>
 #endif
+
+#include <asm/unaligned.h>
+
 #include "ftdi_sio.h"
 #include "ftdi_sio_ids.h"
 
@@ -1920,6 +1923,49 @@ static int ftdi_sio_gpio_direction_output(struct gpio_chip *gc,
 			  FTDI_SIO_GPIO_MODE_CBUS);
 }
 
+static int ftdi_ft232r_gpioconf_init(struct usb_serial_port *port)
+{
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+	struct usb_device *udev = port->serial->dev;
+	u16 tmp;
+	u8 *buf;
+	int res;
+	int i;
+
+	buf = kmalloc(2, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
+				FTDI_SIO_READ_EEPROM_REQUEST,
+				FTDI_SIO_READ_EEPROM_REQUEST_TYPE,
+				0, 0x0a, buf, 2,
+				WDR_TIMEOUT);
+	if (res < 2) {
+		if (res >= 0)
+			res = -EIO;
+		goto out_free;
+	}
+
+	tmp = get_unaligned_le16(buf);
+	dev_dbg(&port->dev, "cbus_config = 0x%04x\n", tmp);
+
+	priv->gc.ngpio = 4;
+	priv->gpio_altfunc = 0xff;
+
+	for (i = 0; i < priv->gc.ngpio; ++i) {
+		if ((tmp & 0xf) == FTDI_FT232R_CBUS_MUX_IOMODE)
+			priv->gpio_altfunc &= ~BIT(i);
+		tmp >>= 4;
+	}
+
+	res = 0;
+out_free:
+	kfree(buf);
+
+	return res;
+}
+
 static int ftx_gpioconf_init(struct usb_serial_port *port)
 {
 	struct ftdi_private *priv = usb_get_serial_port_data(port);
@@ -1980,6 +2026,9 @@ static int ftdi_sio_gpio_init(struct usb_serial_port *port)
 
 	/* Device-specific initializations */
 	switch (priv->chip_type) {
+	case FT232RL:
+		result = ftdi_ft232r_gpioconf_init(port);
+		break;
 	case FTX:
 		result = ftx_gpioconf_init(port);
 		break;
diff --git a/drivers/usb/serial/ftdi_sio.h b/drivers/usb/serial/ftdi_sio.h
index 5046d02fe3ac..eed72608382b 100644
--- a/drivers/usb/serial/ftdi_sio.h
+++ b/drivers/usb/serial/ftdi_sio.h
@@ -458,6 +458,7 @@ enum ftdi_sio_baudrate {
 #define FTDI_SIO_GPIO_MODE_RESET	0x00
 
 #define FTDI_SIO_CBUS_MUX_GPIO		8
+#define FTDI_FT232R_CBUS_MUX_IOMODE	0xa
 
 
 /* Descriptors returned by the device

WARNING: multiple messages have this Message-ID (diff)
From: Johan Hovold <johan@kernel.org>
To: Karoly Pados <pados@pados.hu>, Loic Poulain <loic.poulain@linaro.org>
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	Johan Hovold <johan@kernel.org>
Subject: [RFC] USB: serial: ftdi_sio: add support for FT232R CBUS gpios
Date: Wed,  5 Sep 2018 12:49:04 +0200	[thread overview]
Message-ID: <20180905104904.15325-1-johan@kernel.org> (raw)

Enable support for the CBUS gpios on FT232R. The cbus configuration is
stored in one word in the EEPROM at offset 0x0a with the mux config for
CBUS1, CBUS2, CBUS3 and CBUS4 in bits 0..3, 4..7, 8..11 and 12..15,
respectively.

Tested using FT232RL with all CBUS pins in IO mode.

Applies on top of Karoly's CBUS gpio patch.

FIXME: use common eeprom_read helper
FIXME: return the number of gpios instead of setting gc.ngpio?
FIXME: verify each mux setting
Signed-off-by: Johan Hovold <johan@kernel.org>
---

This is a cleaned up version of some code I've been using to test
Karoly's patch using an FT232RL in case anyone wants to give that a try.

May also be useful to get an idea of how the current setup code can be
refactored. This one needs to be rebased to use a common eeprom helper
eventually for example.

Johan


 drivers/usb/serial/ftdi_sio.c | 49 +++++++++++++++++++++++++++++++++++
 drivers/usb/serial/ftdi_sio.h |  1 +
 2 files changed, 50 insertions(+)

diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c
index 1df9c553710b..4d63863bfb3f 100644
--- a/drivers/usb/serial/ftdi_sio.c
+++ b/drivers/usb/serial/ftdi_sio.c
@@ -43,6 +43,9 @@
 #ifdef CONFIG_GPIOLIB
 #include <linux/gpio/driver.h>
 #endif
+
+#include <asm/unaligned.h>
+
 #include "ftdi_sio.h"
 #include "ftdi_sio_ids.h"
 
@@ -1920,6 +1923,49 @@ static int ftdi_sio_gpio_direction_output(struct gpio_chip *gc,
 			  FTDI_SIO_GPIO_MODE_CBUS);
 }
 
+static int ftdi_ft232r_gpioconf_init(struct usb_serial_port *port)
+{
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+	struct usb_device *udev = port->serial->dev;
+	u16 tmp;
+	u8 *buf;
+	int res;
+	int i;
+
+	buf = kmalloc(2, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
+				FTDI_SIO_READ_EEPROM_REQUEST,
+				FTDI_SIO_READ_EEPROM_REQUEST_TYPE,
+				0, 0x0a, buf, 2,
+				WDR_TIMEOUT);
+	if (res < 2) {
+		if (res >= 0)
+			res = -EIO;
+		goto out_free;
+	}
+
+	tmp = get_unaligned_le16(buf);
+	dev_dbg(&port->dev, "cbus_config = 0x%04x\n", tmp);
+
+	priv->gc.ngpio = 4;
+	priv->gpio_altfunc = 0xff;
+
+	for (i = 0; i < priv->gc.ngpio; ++i) {
+		if ((tmp & 0xf) == FTDI_FT232R_CBUS_MUX_IOMODE)
+			priv->gpio_altfunc &= ~BIT(i);
+		tmp >>= 4;
+	}
+
+	res = 0;
+out_free:
+	kfree(buf);
+
+	return res;
+}
+
 static int ftx_gpioconf_init(struct usb_serial_port *port)
 {
 	struct ftdi_private *priv = usb_get_serial_port_data(port);
@@ -1980,6 +2026,9 @@ static int ftdi_sio_gpio_init(struct usb_serial_port *port)
 
 	/* Device-specific initializations */
 	switch (priv->chip_type) {
+	case FT232RL:
+		result = ftdi_ft232r_gpioconf_init(port);
+		break;
 	case FTX:
 		result = ftx_gpioconf_init(port);
 		break;
diff --git a/drivers/usb/serial/ftdi_sio.h b/drivers/usb/serial/ftdi_sio.h
index 5046d02fe3ac..eed72608382b 100644
--- a/drivers/usb/serial/ftdi_sio.h
+++ b/drivers/usb/serial/ftdi_sio.h
@@ -458,6 +458,7 @@ enum ftdi_sio_baudrate {
 #define FTDI_SIO_GPIO_MODE_RESET	0x00
 
 #define FTDI_SIO_CBUS_MUX_GPIO		8
+#define FTDI_FT232R_CBUS_MUX_IOMODE	0xa
 
 
 /* Descriptors returned by the device
-- 
2.18.0


             reply	other threads:[~2018-09-05 10:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-05 10:49 Johan Hovold [this message]
2018-09-05 10:49 ` [RFC] USB: serial: ftdi_sio: add support for FT232R CBUS gpios Johan Hovold

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=20180905104904.15325-1-johan@kernel.org \
    --to=johan@kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.