Skip to contentSkip to conversation

extcon: otg_gpio: add driver for USB OTG port controlled by GPIO(s)

RFC/PATCH In review David Cohen 8 participants 41 messages Dec 22, 2014 – Mar 19, 2015 External connector subsystem (extcon)

lore
Share & tools
lore
#0 lore raw

Some platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to GPIO

Optionally:
(2) VBUS is enabled by a GPIO (when ID is grounded)
(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:
- If yes, OTG port is configured for host role
- If no, by standard, the OTG port is configured for device role

---

Hi,

Some Intel Bay Trail boards have an unusual way to handle the USB OTG port:
- The USB ID pin is connected directly to GPIO on SoC
- When in host role, VBUS is provided by enabling a GPIO
- Device and host roles are supported by 2 independent controllers with D+/- pins from port switched between different phys according a GPIO level.

The ACPI table describes this USB port as a (virtual) device with all the necessary GPIOs. This driver implements support to this virtual device as an extcon class driver. All drivers that depend on the USB OTG port state (USB phy, PMIC, charge detection) will listen to extcon events.

Comments are welcome.

Br, David
---

 drivers/extcon/Kconfig           |   8 ++
 drivers/extcon/Makefile          |   1 +
 drivers/extcon/extcon-otg_gpio.c | 200 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 209 insertions(+)
 create mode 100644 drivers/extcon/extcon-otg_gpio.c
drivers/extcon/Kconfig +8 -0
index 6a1f7de6fa54..e8010cda4642 100644
@@ -93,4 +93,12 @@ config EXTCON_SM5502
 	  Silicon Mitus SM5502. The SM5502 is a USB port accessory
 	  detector and switch.
 
+config EXTCON_OTG_GPIO
+	tristate "VIRTUAL USB OTG PORT support"
+	depends on GPIOLIB
+	help
+	  Say Y here to enable support for virtual USB OTG port device
+	  controlled by GPIOs. This driver can be used when at least USB ID pin
+	  is connected directly to GPIO.
+
 endif # MULTISTATE_SWITCH
drivers/extcon/Makefile +1 -0
index 0370b42e5a27..9e81088c6584 100644
@@ -12,3 +12,4 @@ obj-$(CONFIG_EXTCON_MAX8997)	+= extcon-max8997.o
 obj-$(CONFIG_EXTCON_PALMAS)	+= extcon-palmas.o
 obj-$(CONFIG_EXTCON_RT8973A)	+= extcon-rt8973a.o
 obj-$(CONFIG_EXTCON_SM5502)	+= extcon-sm5502.o
+obj-$(CONFIG_EXTCON_OTG_GPIO) += extcon-otg_gpio.o
drivers/extcon/extcon-otg_gpio.c new file +200 -0

200 lines not on this page · raw message

Signature · 1 line
2.1.1
#1 lore raw
Quoted 10 lines · David Cohen

Some platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to GPIO

Optionally:
(2) VBUS is enabled by a GPIO (when ID is grounded)
(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

Would you explain how it works? Choosing controller runtime?

Quoted 3 lines

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:

You mean Micro-AB cable, right?

Quoted 114 lines

- If yes, OTG port is configured for host role
- If no, by standard, the OTG port is configured for device role

---

Hi,

Some Intel Bay Trail boards have an unusual way to handle the USB OTG port:
- The USB ID pin is connected directly to GPIO on SoC
- When in host role, VBUS is provided by enabling a GPIO
- Device and host roles are supported by 2 independent controllers with D+/- pins from port switched between different phys according a GPIO level.

The ACPI table describes this USB port as a (virtual) device with all the necessary GPIOs. This driver implements support to this virtual device as an extcon class driver. All drivers that depend on the USB OTG port state (USB phy, PMIC, charge detection) will listen to extcon events.

Comments are welcome.

Br, David
---

 drivers/extcon/Kconfig           |   8 ++
 drivers/extcon/Makefile          |   1 +
 drivers/extcon/extcon-otg_gpio.c | 200 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 209 insertions(+)
 create mode 100644 drivers/extcon/extcon-otg_gpio.c
drivers/extcon/Kconfig +8 -0
index 6a1f7de6fa54..e8010cda4642 100644
@@ -93,4 +93,12 @@ config EXTCON_SM5502
 	  Silicon Mitus SM5502. The SM5502 is a USB port accessory
 	  detector and switch.
 
+config EXTCON_OTG_GPIO
+	tristate "VIRTUAL USB OTG PORT support"
+	depends on GPIOLIB
+	help
+	  Say Y here to enable support for virtual USB OTG port device
+	  controlled by GPIOs. This driver can be used when at least USB ID pin
+	  is connected directly to GPIO.
+
 endif # MULTISTATE_SWITCH
drivers/extcon/Makefile +1 -0
index 0370b42e5a27..9e81088c6584 100644
@@ -12,3 +12,4 @@ obj-$(CONFIG_EXTCON_MAX8997)	+= extcon-max8997.o
 obj-$(CONFIG_EXTCON_PALMAS)	+= extcon-palmas.o
 obj-$(CONFIG_EXTCON_RT8973A)	+= extcon-rt8973a.o
 obj-$(CONFIG_EXTCON_SM5502)	+= extcon-sm5502.o
+obj-$(CONFIG_EXTCON_OTG_GPIO) += extcon-otg_gpio.o
drivers/extcon/extcon-otg_gpio.c new file +52 -0
new file mode 100644
index 000000000000..c5ee765a5f4f
@@ -0,0 +1,200 @@
+/*
+ * Virtual USB OTG Port driver controlled by gpios
+ *
+ * Copyright (c) 2014, Intel Corporation.
+ * Author: David Cohen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/acpi.h>
+#include <linux/extcon.h>
+#include <linux/gpio.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+
+#define DRV_NAME	"usb_otg_port"
+
+struct vuport {
+	struct device *dev;
+	struct gpio_desc *gpio_vbus_en;
+	struct gpio_desc *gpio_usb_id;
+	struct gpio_desc *gpio_usb_mux;
+
+	struct extcon_dev edev;
+};
+
+static const char *const vuport_extcon_cable[] = {
+	[0] = "USB-Host",
+	NULL,
+};
+
+/*
+ * If id == 1, USB port should be set to peripheral
+ * if id == 0, USB port should be set to host
+ *
+ * Peripheral: set USB mux to peripheral and disable VBUS
+ * Host: set USB mux to host and enable VBUS
+ */
+static void vuport_set_port(struct vuport *vup, int id)
+{
+	int mux_val = id;
+	int vbus_val = !id;
+
+	if (!IS_ERR(vup->gpio_usb_mux))
+		gpiod_direction_output(vup->gpio_usb_mux, mux_val);

drivers/extcon/extcon-otg_gpio.c line 62

Quoted 10 lines
drivers/extcon/extcon-otg_gpio.c new file +10 -0
+
+	if (!IS_ERR(vup->gpio_vbus_en))
+		gpiod_direction_output(vup->gpio_vbus_en, vbus_val);
+}
+
+static void vuport_do_usb_id(struct vuport *vup)
+{
+	int id = gpiod_get_value(vup->gpio_usb_id);
+
+	dev_info(vup->dev, "USB PORT ID: %s\n", id ? "PERIPHERAL" : "HOST");

dev_dbg

Quoted 56 lines
drivers/extcon/extcon-otg_gpio.c new file +56 -0
+
+	/*
+	 * id == 1: PERIPHERAL
+	 * id == 0: HOST
+	 */
+	vuport_set_port(vup, id);
+
+	/*
+	 * id == 0: HOST connected
+	 * id == 1: Host disconnected
+	 */
+	extcon_set_cable_state(&vup->edev, "USB-Host", !id);
+}
+
+static irqreturn_t vuport_thread_isr(int irq, void *priv)
+{
+	struct vuport *vup = priv;
+
+	vuport_do_usb_id(vup);
+	return IRQ_HANDLED;
+}
+
+static irqreturn_t vuport_isr(int irq, void *priv)
+{
+	return IRQ_WAKE_THREAD;
+}
+
+#define VUPORT_GPIO_USB_ID	0
+#define VUPORT_GPIO_VBUS_EN	1
+#define VUPORT_GPIO_USB_MUX	2
+static int vuport_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct vuport *vup;
+	int ret;
+
+	vup = devm_kzalloc(dev, sizeof(*vup), GFP_KERNEL);
+	if (!vup) {
+		dev_err(dev, "cannot allocate private data\n");
+		return -ENOMEM;
+	}
+	vup->dev = dev;
+
+	vup->gpio_usb_id = devm_gpiod_get_index(dev, "id", VUPORT_GPIO_USB_ID);
+	if (IS_ERR(vup->gpio_usb_id)) {
+		dev_err(dev, "cannot request USB ID GPIO: ret = %ld\n",
+			PTR_ERR(vup->gpio_usb_id));
+		return PTR_ERR(vup->gpio_usb_id);
+	}
+
+	ret = gpiod_direction_input(vup->gpio_usb_id);
+	if (ret < 0) {
+		dev_err(dev, "cannot set input direction to USB ID GPIO: ret = %d\n",
+			ret);
+		return ret;
+	}

drivers/extcon/extcon-otg_gpio.c line 128

Quoted 10 lines
drivers/extcon/extcon-otg_gpio.c new file +10 -0
+
+	vup->gpio_vbus_en = devm_gpiod_get_index(dev, "vbus en",
+						 VUPORT_GPIO_VBUS_EN);
+	if (IS_ERR(vup->gpio_vbus_en))
+		dev_info(dev, "cannot request VBUS EN GPIO, skipping it.\n");
+
+	vup->gpio_usb_mux = devm_gpiod_get_index(dev, "usb mux",
+						 VUPORT_GPIO_USB_MUX);
+	if (IS_ERR(vup->gpio_usb_mux))
+		dev_info(dev, "cannot request USB USB MUX, skipping it.\n");

Using dev_err

Quoted 75 lines
drivers/extcon/extcon-otg_gpio.c new file +72 -0
+
+	/* register extcon device */
+	vup->edev.dev.parent = dev;
+	vup->edev.supported_cable = vuport_extcon_cable;
+	ret = extcon_dev_register(&vup->edev);
+	if (ret < 0) {
+		dev_err(dev, "failed to register extcon device: ret = %d\n",
+			ret);
+		return ret;
+	}
+
+	ret = devm_request_threaded_irq(dev, gpiod_to_irq(vup->gpio_usb_id),
+					vuport_isr, vuport_thread_isr,
+					IRQF_SHARED | IRQF_TRIGGER_RISING |
+					IRQF_TRIGGER_FALLING,
+					dev_name(dev), vup);
+	if (ret < 0) {
+		dev_err(dev, "cannot request IRQ for USB ID GPIO: ret = %d\n",
+			ret);
+		goto irq_err;
+	}
+	vuport_do_usb_id(vup);
+
+	platform_set_drvdata(pdev, vup);
+
+	dev_info(dev, "driver successfully probed\n");
+
+	return 0;
+
+irq_err:
+	extcon_dev_unregister(&vup->edev);
+
+	return ret;
+}
+
+static int vuport_remove(struct platform_device *pdev)
+{
+	struct vuport *vup = platform_get_drvdata(pdev);
+
+	extcon_dev_unregister(&vup->edev);
+	return 0;
+}
+
+static struct acpi_device_id vuport_acpi_match[] = {
+	{ "INT3496" },
+	{ }
+};
+MODULE_DEVICE_TABLE(acpi, vuport_acpi_match);
+
+static struct platform_driver vuport_driver = {
+	.driver = {
+		.name = DRV_NAME,
+		.acpi_match_table = ACPI_PTR(vuport_acpi_match),
+	},
+	.probe = vuport_probe,
+	.remove = vuport_remove,
+};
+
+static int __init vuport_init(void)
+{
+	return platform_driver_register(&vuport_driver);
+}
+subsys_initcall(vuport_init);
+
+static void __exit vuport_exit(void)
+{
+	platform_driver_unregister(&vuport_driver);
+}
+module_exit(vuport_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("David Cohen <[email protected]>");
Signature · 1 line
2.1.1
Signature · 2 lines
Best Regards,
Peter Chen
#2 lore raw

Hi Peter,

Thanks for the review.

Quoted 6 lines · Peter Chen
Quoted 5 lines · David Cohen

Some platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to GPIO

Quoted 7 lines
Quoted 5 lines

Optionally:
(2) VBUS is enabled by a GPIO (when ID is grounded)
(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

Would you explain how it works? Choosing controller runtime?

Both controllers are (indirectly) connected to the same micro B port. The D+/- goes from the port to a switch operated by a GPIO. From the switch, D+/- may go to Host controller's phy or Device controller's phy. Depends on the GPIO level.

Quoted 6 lines
Quoted 4 lines

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:

You mean Micro-AB cable, right?

In my case I'd say micro B. But USB-Host would be any cable or combination of cables where USB ID is grounded.

Quoted 119 lines
Quoted 119 lines

- If yes, OTG port is configured for host role
- If no, by standard, the OTG port is configured for device role

---

Hi,

Some Intel Bay Trail boards have an unusual way to handle the USB OTG port:
- The USB ID pin is connected directly to GPIO on SoC
- When in host role, VBUS is provided by enabling a GPIO
- Device and host roles are supported by 2 independent controllers with D+/- pins from port switched between different phys according a GPIO level.

The ACPI table describes this USB port as a (virtual) device with all the necessary GPIOs. This driver implements support to this virtual device as an extcon class driver. All drivers that depend on the USB OTG port state (USB phy, PMIC, charge detection) will listen to extcon events.

Comments are welcome.

Br, David
---

 drivers/extcon/Kconfig           |   8 ++
 drivers/extcon/Makefile          |   1 +
 drivers/extcon/extcon-otg_gpio.c | 200 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 209 insertions(+)
 create mode 100644 drivers/extcon/extcon-otg_gpio.c
drivers/extcon/Kconfig +8 -0
index 6a1f7de6fa54..e8010cda4642 100644
@@ -93,4 +93,12 @@ config EXTCON_SM5502
 	  Silicon Mitus SM5502. The SM5502 is a USB port accessory
 	  detector and switch.
 
+config EXTCON_OTG_GPIO
+	tristate "VIRTUAL USB OTG PORT support"
+	depends on GPIOLIB
+	help
+	  Say Y here to enable support for virtual USB OTG port device
+	  controlled by GPIOs. This driver can be used when at least USB ID pin
+	  is connected directly to GPIO.
+
 endif # MULTISTATE_SWITCH
drivers/extcon/Makefile +1 -0
index 0370b42e5a27..9e81088c6584 100644
@@ -12,3 +12,4 @@ obj-$(CONFIG_EXTCON_MAX8997)	+= extcon-max8997.o
 obj-$(CONFIG_EXTCON_PALMAS)	+= extcon-palmas.o
 obj-$(CONFIG_EXTCON_RT8973A)	+= extcon-rt8973a.o
 obj-$(CONFIG_EXTCON_SM5502)	+= extcon-sm5502.o
+obj-$(CONFIG_EXTCON_OTG_GPIO) += extcon-otg_gpio.o
drivers/extcon/extcon-otg_gpio.c new file +56 -0
new file mode 100644
index 000000000000..c5ee765a5f4f
@@ -0,0 +1,200 @@
+/*
+ * Virtual USB OTG Port driver controlled by gpios
+ *
+ * Copyright (c) 2014, Intel Corporation.
+ * Author: David Cohen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/acpi.h>
+#include <linux/extcon.h>
+#include <linux/gpio.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+
+#define DRV_NAME	"usb_otg_port"
+
+struct vuport {
+	struct device *dev;
+	struct gpio_desc *gpio_vbus_en;
+	struct gpio_desc *gpio_usb_id;
+	struct gpio_desc *gpio_usb_mux;
+
+	struct extcon_dev edev;
+};
+
+static const char *const vuport_extcon_cable[] = {
+	[0] = "USB-Host",
+	NULL,
+};
+
+/*
+ * If id == 1, USB port should be set to peripheral
+ * if id == 0, USB port should be set to host
+ *
+ * Peripheral: set USB mux to peripheral and disable VBUS
+ * Host: set USB mux to host and enable VBUS
+ */
+static void vuport_set_port(struct vuport *vup, int id)
+{
+	int mux_val = id;
+	int vbus_val = !id;
+
+	if (!IS_ERR(vup->gpio_usb_mux))
+		gpiod_direction_output(vup->gpio_usb_mux, mux_val);
+
+	if (!IS_ERR(vup->gpio_vbus_en))
+		gpiod_direction_output(vup->gpio_vbus_en, vbus_val);
+}
Quoted 8 lines

drivers/extcon/extcon-otg_gpio.c line 62

Quoted 6 lines
drivers/extcon/extcon-otg_gpio.c new file +6 -0
+
+static void vuport_do_usb_id(struct vuport *vup)
+{
+	int id = gpiod_get_value(vup->gpio_usb_id);
+
+	dev_info(vup->dev, "USB PORT ID: %s\n", id ? "PERIPHERAL" : "HOST");

dev_dbg

Agreed.

Quoted 61 lines
Quoted 61 lines
drivers/extcon/extcon-otg_gpio.c new file +60 -0
+
+	/*
+	 * id == 1: PERIPHERAL
+	 * id == 0: HOST
+	 */
+	vuport_set_port(vup, id);
+
+	/*
+	 * id == 0: HOST connected
+	 * id == 1: Host disconnected
+	 */
+	extcon_set_cable_state(&vup->edev, "USB-Host", !id);
+}
+
+static irqreturn_t vuport_thread_isr(int irq, void *priv)
+{
+	struct vuport *vup = priv;
+
+	vuport_do_usb_id(vup);
+	return IRQ_HANDLED;
+}
+
+static irqreturn_t vuport_isr(int irq, void *priv)
+{
+	return IRQ_WAKE_THREAD;
+}
+
+#define VUPORT_GPIO_USB_ID	0
+#define VUPORT_GPIO_VBUS_EN	1
+#define VUPORT_GPIO_USB_MUX	2
+static int vuport_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct vuport *vup;
+	int ret;
+
+	vup = devm_kzalloc(dev, sizeof(*vup), GFP_KERNEL);
+	if (!vup) {
+		dev_err(dev, "cannot allocate private data\n");
+		return -ENOMEM;
+	}
+	vup->dev = dev;
+
+	vup->gpio_usb_id = devm_gpiod_get_index(dev, "id", VUPORT_GPIO_USB_ID);
+	if (IS_ERR(vup->gpio_usb_id)) {
+		dev_err(dev, "cannot request USB ID GPIO: ret = %ld\n",
+			PTR_ERR(vup->gpio_usb_id));
+		return PTR_ERR(vup->gpio_usb_id);
+	}
+
+	ret = gpiod_direction_input(vup->gpio_usb_id);
+	if (ret < 0) {
+		dev_err(dev, "cannot set input direction to USB ID GPIO: ret = %d\n",
+			ret);
+		return ret;
+	}
+
+	vup->gpio_vbus_en = devm_gpiod_get_index(dev, "vbus en",
+						 VUPORT_GPIO_VBUS_EN);
+	if (IS_ERR(vup->gpio_vbus_en))
Quoted 8 lines

drivers/extcon/extcon-otg_gpio.c line 128

Quoted 6 lines
drivers/extcon/extcon-otg_gpio.c new file +6 -0
+		dev_info(dev, "cannot request VBUS EN GPIO, skipping it.\n");
+
+	vup->gpio_usb_mux = devm_gpiod_get_index(dev, "usb mux",
+						 VUPORT_GPIO_USB_MUX);
+	if (IS_ERR(vup->gpio_usb_mux))
+		dev_info(dev, "cannot request USB USB MUX, skipping it.\n");

Using dev_err

That's not really an error, although the IS_ERR() suggests otherwise. The driver works well if a board doesn't need this mux (I'll add a comment to state that clear). IMHO either keep dev_info or use dev_dgb instead?

Br, David

Quoted 81 lines
Quoted 76 lines
drivers/extcon/extcon-otg_gpio.c new file +72 -0
+
+	/* register extcon device */
+	vup->edev.dev.parent = dev;
+	vup->edev.supported_cable = vuport_extcon_cable;
+	ret = extcon_dev_register(&vup->edev);
+	if (ret < 0) {
+		dev_err(dev, "failed to register extcon device: ret = %d\n",
+			ret);
+		return ret;
+	}
+
+	ret = devm_request_threaded_irq(dev, gpiod_to_irq(vup->gpio_usb_id),
+					vuport_isr, vuport_thread_isr,
+					IRQF_SHARED | IRQF_TRIGGER_RISING |
+					IRQF_TRIGGER_FALLING,
+					dev_name(dev), vup);
+	if (ret < 0) {
+		dev_err(dev, "cannot request IRQ for USB ID GPIO: ret = %d\n",
+			ret);
+		goto irq_err;
+	}
+	vuport_do_usb_id(vup);
+
+	platform_set_drvdata(pdev, vup);
+
+	dev_info(dev, "driver successfully probed\n");
+
+	return 0;
+
+irq_err:
+	extcon_dev_unregister(&vup->edev);
+
+	return ret;
+}
+
+static int vuport_remove(struct platform_device *pdev)
+{
+	struct vuport *vup = platform_get_drvdata(pdev);
+
+	extcon_dev_unregister(&vup->edev);
+	return 0;
+}
+
+static struct acpi_device_id vuport_acpi_match[] = {
+	{ "INT3496" },
+	{ }
+};
+MODULE_DEVICE_TABLE(acpi, vuport_acpi_match);
+
+static struct platform_driver vuport_driver = {
+	.driver = {
+		.name = DRV_NAME,
+		.acpi_match_table = ACPI_PTR(vuport_acpi_match),
+	},
+	.probe = vuport_probe,
+	.remove = vuport_remove,
+};
+
+static int __init vuport_init(void)
+{
+	return platform_driver_register(&vuport_driver);
+}
+subsys_initcall(vuport_init);
+
+static void __exit vuport_exit(void)
+{
+	platform_driver_unregister(&vuport_driver);
+}
+module_exit(vuport_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("David Cohen <[email protected]>");
Signature · 1 line
2.1.1
Signature · 2 lines
Best Regards,
Peter Chen
#3 lore raw
Quoted 17 lines · David Cohen

Hi Peter,

Thanks for the review.

Quoted 12 lines · Peter Chen
Quoted 10 lines · David Cohen

Some platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to GPIO

Optionally:
(2) VBUS is enabled by a GPIO (when ID is grounded)
(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

Quoted 7 lines
Quoted 1 line

Would you explain how it works? Choosing controller runtime?

Both controllers are (indirectly) connected to the same micro B port. The D+/- goes from the port to a switch operated by a GPIO. From the switch, D+/- may go to Host controller's phy or Device controller's phy. Depends on the GPIO level.

Get it, why the design like that? If your controller supports both roles, the software can do role switch by ID pin (through gpio in your case).

Quoted 15 lines
Quoted 7 lines
Quoted 4 lines

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:

You mean Micro-AB cable, right?

Quoted 7 lines

drivers/extcon/extcon-otg_gpio.c line 128

Quoted 5 lines
drivers/extcon/extcon-otg_gpio.c new file +5 -0
+
+	vup->gpio_usb_mux = devm_gpiod_get_index(dev, "usb mux",
+						 VUPORT_GPIO_USB_MUX);
+	if (IS_ERR(vup->gpio_usb_mux))
+		dev_info(dev, "cannot request USB USB MUX, skipping it.\n");

Using dev_err

Quoted 5 lines

That's not really an error, although the IS_ERR() suggests otherwise. The driver works well if a board doesn't need this mux (I'll add a comment to state that clear). IMHO either keep dev_info or use dev_dgb instead?

If that, dev_dbg may be suitable.

Signature · 2 lines
Best Regards,
Peter Chen
#4 lore raw
Quoted 26 lines · Peter Chen
Quoted 17 lines · David Cohen

Hi Peter,

Thanks for the review.

Quoted 12 lines · Peter Chen
Quoted 10 lines · David Cohen

Some platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to GPIO

Optionally:
(2) VBUS is enabled by a GPIO (when ID is grounded)
(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

Quoted 7 lines
Quoted 1 line

Would you explain how it works? Choosing controller runtime?

Both controllers are (indirectly) connected to the same micro B port. The D+/- goes from the port to a switch operated by a GPIO. From the switch, D+/- may go to Host controller's phy or Device controller's phy. Depends on the GPIO level.

Quoted 3 lines

Get it, why the design like that? If your controller supports both roles, the software can do role switch by ID pin (through gpio in your case).

It's really 2 controllers: 1 for each role. And the ID pin from port is connected to GPIO only.

Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 5 lines

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:

You mean Micro-AB cable, right?

drivers/extcon/extcon-otg_gpio.c line 128

Quoted 5 lines
drivers/extcon/extcon-otg_gpio.c new file +5 -0
+
+	vup->gpio_usb_mux = devm_gpiod_get_index(dev, "usb mux",
+						 VUPORT_GPIO_USB_MUX);
+	if (IS_ERR(vup->gpio_usb_mux))
+		dev_info(dev, "cannot request USB USB MUX, skipping it.\n");

Using dev_err

Quoted 7 lines
Quoted 5 lines

That's not really an error, although the IS_ERR() suggests otherwise. The driver works well if a board doesn't need this mux (I'll add a comment to state that clear). IMHO either keep dev_info or use dev_dgb instead?

If that, dev_dbg may be suitable.

Ack.

Br, David

Quoted 6 lines
Signature · 4 lines
-- 

Best Regards,
Peter Chen
#5 lore raw
In reply to David Cohen

Hello.

Quoted 7 lines · David Cohen

Some platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to GPIO

Optionally:
(2) VBUS is enabled by a GPIO (when ID is grounded)

Can't the host driver still control Vbus?

Quoted 20 lines

(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:
- If yes, OTG port is configured for host role
- If no, by standard, the OTG port is configured for device role

---

Hi,

Some Intel Bay Trail boards have an unusual way to handle the USB OTG port:
- The USB ID pin is connected directly to GPIO on SoC
- When in host role, VBUS is provided by enabling a GPIO
- Device and host roles are supported by 2 independent controllers with D+/- pins from port switched between different phys according a GPIO level.

Quoted 4 lines

The ACPI table describes this USB port as a (virtual) device with all the necessary GPIOs. This driver implements support to this virtual device as an extcon class driver. All drivers that depend on the USB OTG port state (USB phy, PMIC, charge detection) will listen to extcon events.

It's very close to my setup on R-Car R8A7791 based boards. :-)
I have already submitted Maxim MAX3355 OTG chip GPIO-based driver.

Quoted 17 lines

Comments are welcome.

Br, David
---

  drivers/extcon/Kconfig           |   8 ++
  drivers/extcon/Makefile          |   1 +
  drivers/extcon/extcon-otg_gpio.c | 200 +++++++++++++++++++++++++++++++++++++++
  3 files changed, 209 insertions(+)

create mode 100644 drivers/extcon/extcon-otg_gpio.c

drivers/extcon/Kconfig +0 -0
index 6a1f7de6fa54..e8010cda4642 100644
@@ -93,4 +93,12 @@ config EXTCON_SM5502
  	  Silicon Mitus SM5502. The SM5502 is a USB port accessory

drivers/extcon/Kconfig config EXTCON_SM5502 · line 103

Quoted 10 lines
drivers/extcon/Kconfig +8 -0
  	  detector and switch.
 
+config EXTCON_OTG_GPIO
+	tristate "VIRTUAL USB OTG PORT support"
+	depends on GPIOLIB
+	help
+	  Say Y here to enable support for virtual USB OTG port device
+	  controlled by GPIOs. This driver can be used when at least USB ID pin
+	  is connected directly to GPIO.
+

The entries in this file seem alphabetically sorted.

drivers/extcon/Makefile obj-$(CONFIG_EXTCON_MAX8997) += extcon-max8997.o · line 15

Quoted 10 lines
drivers/extcon/Kconfig +0 -0
  endif # MULTISTATE_SWITCH
drivers/extcon/Makefile +1 -0
index 0370b42e5a27..9e81088c6584 100644
@@ -12,3 +12,4 @@ obj-$(CONFIG_EXTCON_MAX8997)	+= extcon-max8997.o
  obj-$(CONFIG_EXTCON_PALMAS)	+= extcon-palmas.o
  obj-$(CONFIG_EXTCON_RT8973A)	+= extcon-rt8973a.o
  obj-$(CONFIG_EXTCON_SM5502)	+= extcon-sm5502.o
+obj-$(CONFIG_EXTCON_OTG_GPIO) += extcon-otg_gpio.o

The lines here are sorted too.

Quoted 6 lines
drivers/extcon/extcon-otg_gpio.c new file +0 -0
new file mode 100644
index 000000000000..c5ee765a5f4f
@@ -0,0 +1,200 @@

[...]

drivers/extcon/extcon-otg_gpio.c line 88

Quoted 4 lines
drivers/extcon/extcon-otg_gpio.c new file +4 -0
+static irqreturn_t vuport_isr(int irq, void *priv)
+{
+	return IRQ_WAKE_THREAD;
+}

It's the same as the default IRQ handler...

drivers/extcon/extcon-otg_gpio.c line 141

Quoted 2 lines
drivers/extcon/extcon-otg_gpio.c new file +2 -0
+	ret = devm_request_threaded_irq(dev, gpiod_to_irq(vup->gpio_usb_id),
+					vuport_isr, vuport_thread_isr,

... so you probably can just pass NULL instead of vuport_isr, no?

[...]

drivers/extcon/extcon-otg_gpio.c line 191

Quoted 5 lines
drivers/extcon/extcon-otg_gpio.c new file +5 -0
+static int __init vuport_init(void)
+{
+	return platform_driver_register(&vuport_driver);
+}
+subsys_initcall(vuport_init);

Hm, why?

WBR, Sergei

#6 lore raw

Hi Sergei,

Quoted 7 lines · Sergei Shtylyov

Hello.

Quoted 2 lines · David Cohen

Some platforms have an USB OTG port fully (or partially) controlled by GPIOs:

Quoted 6 lines
Quoted 4 lines

(1) USB ID is connected directly to GPIO

Optionally:
(2) VBUS is enabled by a GPIO (when ID is grounded)

Can't the host driver still control Vbus?

I can't a clean way for host driver to control VBUS considering it depends on USB ID.

Quoted 21 lines
Quoted 20 lines

(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:
- If yes, OTG port is configured for host role
- If no, by standard, the OTG port is configured for device role

---

Hi,

Some Intel Bay Trail boards have an unusual way to handle the USB OTG port:
- The USB ID pin is connected directly to GPIO on SoC
- When in host role, VBUS is provided by enabling a GPIO
- Device and host roles are supported by 2 independent controllers with D+/- pins from port switched between different phys according a GPIO level.

Quoted 7 lines
Quoted 4 lines

The ACPI table describes this USB port as a (virtual) device with all the necessary GPIOs. This driver implements support to this virtual device as an extcon class driver. All drivers that depend on the USB OTG port state (USB phy, PMIC, charge detection) will listen to extcon events.

It's very close to my setup on R-Car R8A7791 based boards. :-)
I have already submitted Maxim MAX3355 OTG chip GPIO-based driver.

Hm. I'll look for it. Thanks for pointing.

Quoted 20 lines
Quoted 20 lines

Comments are welcome.

Br, David
---

 drivers/extcon/Kconfig           |   8 ++
 drivers/extcon/Makefile          |   1 +
 drivers/extcon/extcon-otg_gpio.c | 200 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 209 insertions(+)
 create mode 100644 drivers/extcon/extcon-otg_gpio.c
drivers/extcon/Kconfig +0 -0
index 6a1f7de6fa54..e8010cda4642 100644
@@ -93,4 +93,12 @@ config EXTCON_SM5502
 	  Silicon Mitus SM5502. The SM5502 is a USB port accessory
 	  detector and switch.
 
Quoted 10 lines

drivers/extcon/Kconfig config EXTCON_SM5502 · line 103

Quoted 8 lines
drivers/extcon/Kconfig +8 -0
+config EXTCON_OTG_GPIO
+	tristate "VIRTUAL USB OTG PORT support"
+	depends on GPIOLIB
+	help
+	  Say Y here to enable support for virtual USB OTG port device
+	  controlled by GPIOs. This driver can be used when at least USB ID pin
+	  is connected directly to GPIO.
+

The entries in this file seem alphabetically sorted.

I'll fix it.

Quoted 13 lines

drivers/extcon/Makefile obj-$(CONFIG_EXTCON_MAX8997) += extcon-max8997.o · line 15

Quoted 11 lines
drivers/extcon/Kconfig +0 -0
 endif # MULTISTATE_SWITCH
drivers/extcon/Makefile +1 -0
index 0370b42e5a27..9e81088c6584 100644
@@ -12,3 +12,4 @@ obj-$(CONFIG_EXTCON_MAX8997)	+= extcon-max8997.o
 obj-$(CONFIG_EXTCON_PALMAS)	+= extcon-palmas.o
 obj-$(CONFIG_EXTCON_RT8973A)	+= extcon-rt8973a.o
 obj-$(CONFIG_EXTCON_SM5502)	+= extcon-sm5502.o
+obj-$(CONFIG_EXTCON_OTG_GPIO) += extcon-otg_gpio.o

The lines here are sorted too.

Ditto.

Quoted 15 lines
Quoted 7 lines
drivers/extcon/extcon-otg_gpio.c new file +0 -0
new file mode 100644
index 000000000000..c5ee765a5f4f
@@ -0,0 +1,200 @@

[...]

drivers/extcon/extcon-otg_gpio.c line 88

Quoted 4 lines
drivers/extcon/extcon-otg_gpio.c new file +4 -0
+static irqreturn_t vuport_isr(int irq, void *priv)
+{
+	return IRQ_WAKE_THREAD;
+}

It's the same as the default IRQ handler...

Quoted 4 lines

drivers/extcon/extcon-otg_gpio.c line 141

Quoted 2 lines
drivers/extcon/extcon-otg_gpio.c new file +2 -0
+	ret = devm_request_threaded_irq(dev, gpiod_to_irq(vup->gpio_usb_id),
+					vuport_isr, vuport_thread_isr,

... so you probably can just pass NULL instead of vuport_isr, no?

I'll review that.

Quoted 10 lines

[...]

drivers/extcon/extcon-otg_gpio.c line 191

Quoted 5 lines
drivers/extcon/extcon-otg_gpio.c new file +5 -0
+static int __init vuport_init(void)
+{
+	return platform_driver_register(&vuport_driver);
+}
+subsys_initcall(vuport_init);

Hm, why?

We have drivers that depend on this one during their probe.

Br, David

Quoted 3 lines

WBR, Sergei

#7 lore raw

Hello.

Quoted 5 lines · David Cohen
Quoted 4 lines
Quoted 4 lines

Some platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to GPIO

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Optionally:
(2) VBUS is enabled by a GPIO (when ID is grounded)

Can't the host driver still control Vbus?

I can't a clean way for host driver to control VBUS considering it depends on USB ID.

You're using the cable state notifiers, why not control Vbus from there?
You need some way of passing the GPIO to host driver though... I assume you're not using the device tree, and your host controllers live on PCI, so the platform data is out of question. You may be right then...

Quoted 25 lines
Quoted 24 lines
Quoted 24 lines

(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:
- If yes, OTG port is configured for host role
- If no, by standard, the OTG port is configured for device role

---

Hi,

Some Intel Bay Trail boards have an unusual way to handle the USB OTG port:
- The USB ID pin is connected directly to GPIO on SoC
- When in host role, VBUS is provided by enabling a GPIO
- Device and host roles are supported by 2 independent controllers with D+/- pins from port switched between different phys according a GPIO level.

The ACPI table describes this USB port as a (virtual) device with all the necessary GPIOs. This driver implements support to this virtual device as an extcon class driver. All drivers that depend on the USB OTG port state (USB phy, PMIC, charge detection) will listen to extcon events.

Quoted 4 lines
Quoted 2 lines

It's very close to my setup on R-Car R8A7791 based boards. :-)
I have already submitted Maxim MAX3355 OTG chip GPIO-based driver.

Hm. I'll look for it. Thanks for pointing.

http://marc.info/?l=linux-usb&m=141825413802370
In my case, Vbus is not controlled via GPIO though. I would have probably
used the generic GPIO extcon driver if I didn't have to drive MAX3355's SHDN# pin high...
There were also some other patches for this issue, the one probably
interesting to you is there:

http://marc.info/?l=linux-usb&m=141877180912359

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Comments are welcome.

Br, David

[...]

Quoted 9 lines
Quoted 7 lines

drivers/extcon/extcon-otg_gpio.c line 191

Quoted 5 lines
drivers/extcon/extcon-otg_gpio.c new file +5 -0
+static int __init vuport_init(void)
+{
+	return platform_driver_register(&vuport_driver);
+}
+subsys_initcall(vuport_init);

Hm, why?

We have drivers that depend on this one during their probe.

What about deferred probing? With EPROBE_DEFER we don't need to play the
initcall games any more AFAIU.

Quoted 1 line

Br, David

WBR, Sergei

#8 lore raw
Quoted 14 lines · Sergei Shtylyov

Hello.

Quoted 9 lines · David Cohen
Quoted 9 lines
Quoted 7 lines

Some platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to GPIO

Optionally:
(2) VBUS is enabled by a GPIO (when ID is grounded)

Can't the host driver still control Vbus?

Quoted 7 lines
Quoted 2 lines

I can't a clean way for host driver to control VBUS considering it depends on USB ID.

You're using the cable state notifiers, why not control Vbus from there?
You need some way of passing the GPIO to host driver though... I assume you're not using the device tree, and your host controllers live on PCI, so the platform data is out of question. You may be right then...

Yes to all questions :)

Quoted 29 lines
Quoted 28 lines
Quoted 28 lines
Quoted 21 lines

(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:
- If yes, OTG port is configured for host role
- If no, by standard, the OTG port is configured for device role

---

Hi,

Some Intel Bay Trail boards have an unusual way to handle the USB OTG port:
- The USB ID pin is connected directly to GPIO on SoC
- When in host role, VBUS is provided by enabling a GPIO
- Device and host roles are supported by 2 independent controllers with D+/- pins from port switched between different phys according a GPIO level.

Quoted 4 lines

The ACPI table describes this USB port as a (virtual) device with all the necessary GPIOs. This driver implements support to this virtual device as an extcon class driver. All drivers that depend on the USB OTG port state (USB phy, PMIC, charge detection) will listen to extcon events.

It's very close to my setup on R-Car R8A7791 based boards. :-)
I have already submitted Maxim MAX3355 OTG chip GPIO-based driver.

Quoted 6 lines
Quoted 1 line

Hm. I'll look for it. Thanks for pointing.

http://marc.info/?l=linux-usb&m=141825413802370
In my case, Vbus is not controlled via GPIO though. I would have probably
used the generic GPIO extcon driver if I didn't have to drive MAX3355's SHDN# pin high...

Besides the USB ID, I need to control VBUS and the D+/- switch. We have a new use case coming soon that may need to add a second switch control.

Quoted 4 lines

There were also some other patches for this issue, the one probably
interesting to you is there:

   http://marc.info/?l=linux-usb&m=141877180912359

This one is interesting, but I'm restricted to ACPI and to the DSDTs already released.
E.g. http://www.androidauthority.com/trekstor-xintron-lollipop-564364/

Br, David

Quoted 24 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Comments are welcome.

Br, David

[...]

Quoted 9 lines
Quoted 7 lines

drivers/extcon/extcon-otg_gpio.c line 191

Quoted 5 lines
drivers/extcon/extcon-otg_gpio.c new file +5 -0
+static int __init vuport_init(void)
+{
+	return platform_driver_register(&vuport_driver);
+}
+subsys_initcall(vuport_init);

Hm, why?

We have drivers that depend on this one during their probe.

What about deferred probing? With EPROBE_DEFER we don't need to play the
initcall games any more AFAIU.

Quoted 1 line

Br, David

WBR, Sergei

#9 lore raw
In reply to David Cohen

Hi,

Quoted 7 lines · David Cohen

Some platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to GPIO

Optionally:
(2) VBUS is enabled by a GPIO (when ID is grounded)

ok, so a fixed regulator with a GPIO enable pin.

Quoted 3 lines

(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

so you have discrete mux with a GPIO select signal, like below ?

 .-------.----------------.  .--------.
 |       |                |  |        |      D+
 |       |                |  |        |<-------------.
 |       |                |  |        |              |
 |       |    USB Host    -->|    P   |              |
 |       |                |  |    H   |              |
 |       |                |  |    Y   |    D-        |
 |       '----------------'  |    0   |<--------.    |
 |                       |   |        |         |    |
 |                       |   '--------'      .--------.  D+
 |                       |                   |        |------>
 |       SOC        GPIO | ----------------->|        |
 |                       |                   |   MUX  |
 |                       |                   |        |------>
 |                       |   .--------.      '--------'  D-
 |       .----------------.  |        |   D-  |      |
 |       |                |  |    P   |<------'      |
 |       |                |  |    H   |              |
 |       |                |  |    Y   |              |
 |       |   USB Device   -->|    1   |              |
 |       |                |  |        |      D+      |
 |       |                |  |        |<-------------'
 |       |                |  |        |
 '-------'----------------'  '--------'

I have been on and off about writing a pinctrl-gpio.c driver which would allow us to hide this detail from users. It shouldn't really matter which modes are available behind the mux, but we should be able to tell the mux to go into mode 0 or mode 1 by toggling its select signal. And it shouldn't really matter that we have a GPIO pin. The problem is: I don't really know if pinctrl would be able to handle discrete muxes.

Adding Linus W to ask. Linus, what do you think ? Should we have a pinctrl-gpio.c for such cases ? In TI we too have quite a few boards which different modes hidden behind discrete muxes.

Quoted 4 lines

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:
- If yes, OTG port is configured for host role
- If no, by standard, the OTG port is configured for device role

correct, this default-B is mandated by OTG spec anyway.

Quoted 11 lines

---

Hi,

Some Intel Bay Trail boards have an unusual way to handle the USB OTG port:
- The USB ID pin is connected directly to GPIO on SoC
- When in host role, VBUS is provided by enabling a GPIO
- Device and host roles are supported by 2 independent controllers with D+/- pins from port switched between different phys according a GPIO level.

Quoted 4 lines

The ACPI table describes this USB port as a (virtual) device with all the necessary GPIOs. This driver implements support to this virtual device as an extcon class driver. All drivers that depend on the USB OTG port state (USB phy, PMIC, charge detection) will listen to extcon events.

Right I think you're almost there, but I still think that this needs to be a bit broken down. First, we need some generic DRD library under drivers/usb/common, and that should be the one listening to extcon cable events. But your extcon driver should be doing only that: checking which cable was attached, it shouldn't be doing the switch by itself. That should be part of the DRD library.

That DRD library would also be the one enabling the (optional) VBUS regulator.

George Cherian tried to implement a generic DRD library but I think there are still too many changes happening on usbcore and udc-core. Most of the pieces are already there (usb_del_hcd() and usb_del_gadget_udc() know how to properly unload an hcd/udc), but there are details missing, no doubt.

If we can find a way to broadcast (probably not the best term, but whatever) a "Hey ID pin was just grounded" message, we can get things working.

That message, btw, shouldn't really depend on extcon-gpio alone because other platforms might use non-gpio methods to verify ID pin level. In any case, we need to have generic ID_PIN_LOW and ID_PIN_HIGH messages that a generic DRD library can listen to and load/unload the correct drivers by means of usb_{add,del}_{hcd,gadget_udc}().

With that in mind, I think you can use extcon-gpio.c for your purposes if the other pieces can be fullfilled by regulator and pinctrl.

Quoted 60 lines
drivers/extcon/Makefile +1 -0
index 0370b42e5a27..9e81088c6584 100644
@@ -12,3 +12,4 @@ obj-$(CONFIG_EXTCON_MAX8997)	+= extcon-max8997.o
 obj-$(CONFIG_EXTCON_PALMAS)	+= extcon-palmas.o
 obj-$(CONFIG_EXTCON_RT8973A)	+= extcon-rt8973a.o
 obj-$(CONFIG_EXTCON_SM5502)	+= extcon-sm5502.o
+obj-$(CONFIG_EXTCON_OTG_GPIO) += extcon-otg_gpio.o
drivers/extcon/extcon-otg_gpio.c new file +45 -0
new file mode 100644
index 000000000000..c5ee765a5f4f
@@ -0,0 +1,200 @@
+/*
+ * Virtual USB OTG Port driver controlled by gpios
+ *
+ * Copyright (c) 2014, Intel Corporation.
+ * Author: David Cohen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/acpi.h>
+#include <linux/extcon.h>
+#include <linux/gpio.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+
+#define DRV_NAME	"usb_otg_port"
+
+struct vuport {
+	struct device *dev;
+	struct gpio_desc *gpio_vbus_en;
+	struct gpio_desc *gpio_usb_id;
+	struct gpio_desc *gpio_usb_mux;
+
+	struct extcon_dev edev;
+};
+
+static const char *const vuport_extcon_cable[] = {
+	[0] = "USB-Host",
+	NULL,
+};
+
+/*
+ * If id == 1, USB port should be set to peripheral
+ * if id == 0, USB port should be set to host
+ *
+ * Peripheral: set USB mux to peripheral and disable VBUS
+ * Host: set USB mux to host and enable VBUS
+ */

drivers/extcon/extcon-otg_gpio.c line 55

Quoted 10 lines
drivers/extcon/extcon-otg_gpio.c new file +10 -0
+static void vuport_set_port(struct vuport *vup, int id)
+{
+	int mux_val = id;
+	int vbus_val = !id;
+
+	if (!IS_ERR(vup->gpio_usb_mux))
+		gpiod_direction_output(vup->gpio_usb_mux, mux_val);
+
+	if (!IS_ERR(vup->gpio_vbus_en))
+		gpiod_direction_output(vup->gpio_vbus_en, vbus_val);

not all SoCs will allow for direction to be set all the time. This can be glitchy in some cases. What you want here is to set direction during probe and just set value here.

drivers/extcon/extcon-otg_gpio.c line 62

Quoted 5 lines
drivers/extcon/extcon-otg_gpio.c new file +5 -0
+static void vuport_do_usb_id(struct vuport *vup)
+{
+	int id = gpiod_get_value(vup->gpio_usb_id);
+
+	dev_info(vup->dev, "USB PORT ID: %s\n", id ? "PERIPHERAL" : "HOST");

info ? sounds like debug to me.

Quoted 6 lines
drivers/extcon/extcon-otg_gpio.c new file +6 -0
+
+	/*
+	 * id == 1: PERIPHERAL
+	 * id == 0: HOST
+	 */
+	vuport_set_port(vup, id);

drivers/extcon/extcon-otg_gpio.c line 78

Quoted 10 lines
drivers/extcon/extcon-otg_gpio.c new file +10 -0
+
+	/*
+	 * id == 0: HOST connected
+	 * id == 1: Host disconnected
+	 */
+	extcon_set_cable_state(&vup->edev, "USB-Host", !id);
+}
+
+static irqreturn_t vuport_thread_isr(int irq, void *priv)
+{

this is unrelated to $subject, but I always consider if we should have a generic way to figure out if the interrupt was for rising or falling edge, if we knew that, we could avoid reading the GPIO value altogether ;-)

drivers/extcon/extcon-otg_gpio.c line 88

Quoted 10 lines
drivers/extcon/extcon-otg_gpio.c new file +10 -0
+	struct vuport *vup = priv;
+
+	vuport_do_usb_id(vup);
+	return IRQ_HANDLED;
+}
+
+static irqreturn_t vuport_isr(int irq, void *priv)
+{
+	return IRQ_WAKE_THREAD;
+}

you don't need this. Set the top half handler to NULL and pass IRQF_ONESHOT (which you shoudl already have set anyway).

Quoted 33 lines
drivers/extcon/extcon-otg_gpio.c new file +33 -0
+#define VUPORT_GPIO_USB_ID	0
+#define VUPORT_GPIO_VBUS_EN	1
+#define VUPORT_GPIO_USB_MUX	2
+static int vuport_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct vuport *vup;
+	int ret;
+
+	vup = devm_kzalloc(dev, sizeof(*vup), GFP_KERNEL);
+	if (!vup) {
+		dev_err(dev, "cannot allocate private data\n");
+		return -ENOMEM;
+	}
+	vup->dev = dev;
+
+	vup->gpio_usb_id = devm_gpiod_get_index(dev, "id", VUPORT_GPIO_USB_ID);
+	if (IS_ERR(vup->gpio_usb_id)) {
+		dev_err(dev, "cannot request USB ID GPIO: ret = %ld\n",
+			PTR_ERR(vup->gpio_usb_id));
+		return PTR_ERR(vup->gpio_usb_id);
+	}
+
+	ret = gpiod_direction_input(vup->gpio_usb_id);
+	if (ret < 0) {
+		dev_err(dev, "cannot set input direction to USB ID GPIO: ret = %d\n",
+			ret);
+		return ret;
+	}
+
+	vup->gpio_vbus_en = devm_gpiod_get_index(dev, "vbus en",
+						 VUPORT_GPIO_VBUS_EN);
+	if (IS_ERR(vup->gpio_vbus_en))

drivers/extcon/extcon-otg_gpio.c line 132

Quoted 10 lines
drivers/extcon/extcon-otg_gpio.c new file +10 -0
+		dev_info(dev, "cannot request VBUS EN GPIO, skipping it.\n");
+
+	vup->gpio_usb_mux = devm_gpiod_get_index(dev, "usb mux",
+						 VUPORT_GPIO_USB_MUX);
+	if (IS_ERR(vup->gpio_usb_mux))
+		dev_info(dev, "cannot request USB USB MUX, skipping it.\n");
+
+	/* register extcon device */
+	vup->edev.dev.parent = dev;
+	vup->edev.supported_cable = vuport_extcon_cable;

IIRC, edev should be allocated from now on. Have a look at devm_extcon_dev_allocate() and devm_extcon_dev_register().

Quoted 12 lines
drivers/extcon/extcon-otg_gpio.c new file +12 -0
+	ret = extcon_dev_register(&vup->edev);
+	if (ret < 0) {
+		dev_err(dev, "failed to register extcon device: ret = %d\n",
+			ret);
+		return ret;
+	}
+
+	ret = devm_request_threaded_irq(dev, gpiod_to_irq(vup->gpio_usb_id),
+					vuport_isr, vuport_thread_isr,
+					IRQF_SHARED | IRQF_TRIGGER_RISING |
+					IRQF_TRIGGER_FALLING,
+					dev_name(dev), vup);

drivers/extcon/extcon-otg_gpio.c line 154

Quoted 10 lines
drivers/extcon/extcon-otg_gpio.c new file +10 -0
+	if (ret < 0) {
+		dev_err(dev, "cannot request IRQ for USB ID GPIO: ret = %d\n",
+			ret);
+		goto irq_err;
+	}
+	vuport_do_usb_id(vup);
+
+	platform_set_drvdata(pdev, vup);
+
+	dev_info(dev, "driver successfully probed\n");

this will just make boot noisier, make it dev_dbg() ? Or even dev_vdbg() ?

drivers/extcon/extcon-otg_gpio.c line 199

Quoted 1 line
drivers/extcon/extcon-otg_gpio.c new file +1 -0
+MODULE_LICENSE("GPL");

you header on the top of this C file states gpl 2 only, but this says GPL 2+.

cheers

Signature · 1 line
balbi
#10 lore raw

Hi Felipe,

Thanks replying.

Quoted 6 lines · Felipe Balbi

Hi,

Quoted 3 lines · David Cohen

Some platforms have an USB OTG port fully (or partially) controlled by GPIOs:

Quoted 6 lines
Quoted 4 lines

(1) USB ID is connected directly to GPIO

Optionally:
(2) VBUS is enabled by a GPIO (when ID is grounded)

ok, so a fixed regulator with a GPIO enable pin.

Pretty much yes.

Quoted 24 lines
Quoted 4 lines

(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

so you have discrete mux with a GPIO select signal, like below ?

 .-------.----------------.  .--------.
 |       |                |  |        |      D+
 |       |                |  |        |<-------------.
 |       |                |  |        |              |
 |       |    USB Host    -->|    P   |              |
 |       |                |  |    H   |              |
 |       |                |  |    Y   |    D-        |
 |       '----------------'  |    0   |<--------.    |
 |                       |   |        |         |    |
 |                       |   '--------'      .--------.  D+
 |                       |                   |        |------>
 |       SOC        GPIO | ----------------->|        |
 |                       |                   |   MUX  |
 |                       |                   |        |------>
 |                       |   .--------.      '--------'  D-
 |       .----------------.  |        |   D-  |      |
Quoted 8 lines
 |       |                |  |    P   |<------'      |
 |       |                |  |    H   |              |
 |       |                |  |    Y   |              |
 |       |   USB Device   -->|    1   |              |
 |       |                |  |        |      D+      |
 |       |                |  |        |<-------------'
 |       |                |  |        |
 '-------'----------------'  '--------'

Nice ASCII pic :)
Yes, that's the case.

Quoted 11 lines

I have been on and off about writing a pinctrl-gpio.c driver which would allow us to hide this detail from users. It shouldn't really matter which modes are available behind the mux, but we should be able to tell the mux to go into mode 0 or mode 1 by toggling its select signal. And it shouldn't really matter that we have a GPIO pin. The problem is: I don't really know if pinctrl would be able to handle discrete muxes.

Adding Linus W to ask. Linus, what do you think ? Should we have a pinctrl-gpio.c for such cases ? In TI we too have quite a few boards which different modes hidden behind discrete muxes.

An input from Linus would fine in this case.

Quoted 44 lines
Quoted 5 lines

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:
- If yes, OTG port is configured for host role
- If no, by standard, the OTG port is configured for device role

correct, this default-B is mandated by OTG spec anyway.

Quoted 11 lines

---

Hi,

Some Intel Bay Trail boards have an unusual way to handle the USB OTG port:
- The USB ID pin is connected directly to GPIO on SoC
- When in host role, VBUS is provided by enabling a GPIO
- Device and host roles are supported by 2 independent controllers with D+/- pins from port switched between different phys according a GPIO level.

Quoted 4 lines

The ACPI table describes this USB port as a (virtual) device with all the necessary GPIOs. This driver implements support to this virtual device as an extcon class driver. All drivers that depend on the USB OTG port state (USB phy, PMIC, charge detection) will listen to extcon events.

Right I think you're almost there, but I still think that this needs to be a bit broken down. First, we need some generic DRD library under drivers/usb/common, and that should be the one listening to extcon cable events. But your extcon driver should be doing only that: checking which cable was attached, it shouldn't be doing the switch by itself. That should be part of the DRD library.

That DRD library would also be the one enabling the (optional) VBUS regulator.

George Cherian tried to implement a generic DRD library but I think there are still too many changes happening on usbcore and udc-core. Most of the pieces are already there (usb_del_hcd() and usb_del_gadget_udc() know how to properly unload an hcd/udc), but there are details missing, no doubt.

If we can find a way to broadcast (probably not the best term, but whatever) a "Hey ID pin was just grounded" message, we can get things working.

Quoted 5 lines

That message, btw, shouldn't really depend on extcon-gpio alone because other platforms might use non-gpio methods to verify ID pin level. In any case, we need to have generic ID_PIN_LOW and ID_PIN_HIGH messages that a generic DRD library can listen to and load/unload the correct drivers by means of usb_{add,del}_{hcd,gadget_udc}().

IMHO extcon is the correct way to broadcast it, as long as we define a standard for the cable names. E.g. DRD library could listen to "USB-HOST" cable state. Then it doesn't matter how ID pin is grounded, it just matters that whoever is controlling it broadcast via this cable.

Quoted 3 lines

With that in mind, I think you can use extcon-gpio.c for your purposes if the other pieces can be fullfilled by regulator and pinctrl.

In my case we have all gpios listed in a single ACPI device. In order to be backwards compatible with products already on market, we'd need something like a single mfd to register platform devices for this smaller pieces (extcon gpio, possible pintrl gpio, maybe vbus as regulator??).

Quoted 67 lines
Quoted 67 lines
drivers/extcon/Makefile +1 -0
index 0370b42e5a27..9e81088c6584 100644
@@ -12,3 +12,4 @@ obj-$(CONFIG_EXTCON_MAX8997)	+= extcon-max8997.o
 obj-$(CONFIG_EXTCON_PALMAS)	+= extcon-palmas.o
 obj-$(CONFIG_EXTCON_RT8973A)	+= extcon-rt8973a.o
 obj-$(CONFIG_EXTCON_SM5502)	+= extcon-sm5502.o
+obj-$(CONFIG_EXTCON_OTG_GPIO) += extcon-otg_gpio.o
drivers/extcon/extcon-otg_gpio.c new file +51 -0
new file mode 100644
index 000000000000..c5ee765a5f4f
@@ -0,0 +1,200 @@
+/*
+ * Virtual USB OTG Port driver controlled by gpios
+ *
+ * Copyright (c) 2014, Intel Corporation.
+ * Author: David Cohen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/acpi.h>
+#include <linux/extcon.h>
+#include <linux/gpio.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+
+#define DRV_NAME	"usb_otg_port"
+
+struct vuport {
+	struct device *dev;
+	struct gpio_desc *gpio_vbus_en;
+	struct gpio_desc *gpio_usb_id;
+	struct gpio_desc *gpio_usb_mux;
+
+	struct extcon_dev edev;
+};
+
+static const char *const vuport_extcon_cable[] = {
+	[0] = "USB-Host",
+	NULL,
+};
+
+/*
+ * If id == 1, USB port should be set to peripheral
+ * if id == 0, USB port should be set to host
+ *
+ * Peripheral: set USB mux to peripheral and disable VBUS
+ * Host: set USB mux to host and enable VBUS
+ */
+static void vuport_set_port(struct vuport *vup, int id)
+{
+	int mux_val = id;
+	int vbus_val = !id;
+
+	if (!IS_ERR(vup->gpio_usb_mux))
Quoted 8 lines

drivers/extcon/extcon-otg_gpio.c line 55

Quoted 4 lines
drivers/extcon/extcon-otg_gpio.c new file +4 -0
+		gpiod_direction_output(vup->gpio_usb_mux, mux_val);
+
+	if (!IS_ERR(vup->gpio_vbus_en))
+		gpiod_direction_output(vup->gpio_vbus_en, vbus_val);

not all SoCs will allow for direction to be set all the time. This can be glitchy in some cases. What you want here is to set direction during probe and just set value here.

It makes sense, I'll change it.

Quoted 8 lines

drivers/extcon/extcon-otg_gpio.c line 62

Quoted 6 lines
drivers/extcon/extcon-otg_gpio.c new file +5 -0
+static void vuport_do_usb_id(struct vuport *vup)
+{
+	int id = gpiod_get_value(vup->gpio_usb_id);
+
+	dev_info(vup->dev, "USB PORT ID: %s\n", id ? "PERIPHERAL" : "HOST");

info ? sounds like debug to me.

Ack.

Quoted 14 lines
Quoted 14 lines
drivers/extcon/extcon-otg_gpio.c new file +13 -0
+
+	/*
+	 * id == 1: PERIPHERAL
+	 * id == 0: HOST
+	 */
+	vuport_set_port(vup, id);
+
+	/*
+	 * id == 0: HOST connected
+	 * id == 1: Host disconnected
+	 */
+	extcon_set_cable_state(&vup->edev, "USB-Host", !id);
+}
Quoted 8 lines

drivers/extcon/extcon-otg_gpio.c line 78

Quoted 3 lines
drivers/extcon/extcon-otg_gpio.c new file +3 -0
+
+static irqreturn_t vuport_thread_isr(int irq, void *priv)
+{

this is unrelated to $subject, but I always consider if we should have a generic way to figure out if the interrupt was for rising or falling edge, if we knew that, we could avoid reading the GPIO value altogether ;-)

Yeah, that would be nice.

Quoted 14 lines

drivers/extcon/extcon-otg_gpio.c line 88

Quoted 11 lines
drivers/extcon/extcon-otg_gpio.c new file +10 -0
+	struct vuport *vup = priv;
+
+	vuport_do_usb_id(vup);
+	return IRQ_HANDLED;
+}
+
+static irqreturn_t vuport_isr(int irq, void *priv)
+{
+	return IRQ_WAKE_THREAD;
+}

you don't need this. Set the top half handler to NULL and pass IRQF_ONESHOT (which you shoudl already have set anyway).

Ack.

Quoted 39 lines
Quoted 39 lines
drivers/extcon/extcon-otg_gpio.c new file +38 -0
+#define VUPORT_GPIO_USB_ID	0
+#define VUPORT_GPIO_VBUS_EN	1
+#define VUPORT_GPIO_USB_MUX	2
+static int vuport_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct vuport *vup;
+	int ret;
+
+	vup = devm_kzalloc(dev, sizeof(*vup), GFP_KERNEL);
+	if (!vup) {
+		dev_err(dev, "cannot allocate private data\n");
+		return -ENOMEM;
+	}
+	vup->dev = dev;
+
+	vup->gpio_usb_id = devm_gpiod_get_index(dev, "id", VUPORT_GPIO_USB_ID);
+	if (IS_ERR(vup->gpio_usb_id)) {
+		dev_err(dev, "cannot request USB ID GPIO: ret = %ld\n",
+			PTR_ERR(vup->gpio_usb_id));
+		return PTR_ERR(vup->gpio_usb_id);
+	}
+
+	ret = gpiod_direction_input(vup->gpio_usb_id);
+	if (ret < 0) {
+		dev_err(dev, "cannot set input direction to USB ID GPIO: ret = %d\n",
+			ret);
+		return ret;
+	}
+
+	vup->gpio_vbus_en = devm_gpiod_get_index(dev, "vbus en",
+						 VUPORT_GPIO_VBUS_EN);
+	if (IS_ERR(vup->gpio_vbus_en))
+		dev_info(dev, "cannot request VBUS EN GPIO, skipping it.\n");
+
+	vup->gpio_usb_mux = devm_gpiod_get_index(dev, "usb mux",
+						 VUPORT_GPIO_USB_MUX);
+	if (IS_ERR(vup->gpio_usb_mux))
Quoted 8 lines

drivers/extcon/extcon-otg_gpio.c line 132

Quoted 5 lines
drivers/extcon/extcon-otg_gpio.c new file +5 -0
+		dev_info(dev, "cannot request USB USB MUX, skipping it.\n");
+
+	/* register extcon device */
+	vup->edev.dev.parent = dev;
+	vup->edev.supported_cable = vuport_extcon_cable;

IIRC, edev should be allocated from now on. Have a look at devm_extcon_dev_allocate() and devm_extcon_dev_register().

Thanks, I'll check.

Quoted 18 lines
Quoted 18 lines
drivers/extcon/extcon-otg_gpio.c new file +17 -0
+	ret = extcon_dev_register(&vup->edev);
+	if (ret < 0) {
+		dev_err(dev, "failed to register extcon device: ret = %d\n",
+			ret);
+		return ret;
+	}
+
+	ret = devm_request_threaded_irq(dev, gpiod_to_irq(vup->gpio_usb_id),
+					vuport_isr, vuport_thread_isr,
+					IRQF_SHARED | IRQF_TRIGGER_RISING |
+					IRQF_TRIGGER_FALLING,
+					dev_name(dev), vup);
+	if (ret < 0) {
+		dev_err(dev, "cannot request IRQ for USB ID GPIO: ret = %d\n",
+			ret);
+		goto irq_err;
+	}
Quoted 8 lines

drivers/extcon/extcon-otg_gpio.c line 154

Quoted 5 lines
drivers/extcon/extcon-otg_gpio.c new file +5 -0
+	vuport_do_usb_id(vup);
+
+	platform_set_drvdata(pdev, vup);
+
+	dev_info(dev, "driver successfully probed\n");

this will just make boot noisier, make it dev_dbg() ? Or even dev_vdbg() ?

dev_dgb() perhaps.

Quoted 5 lines

drivers/extcon/extcon-otg_gpio.c line 199

Quoted 2 lines
drivers/extcon/extcon-otg_gpio.c new file +1 -0
+MODULE_LICENSE("GPL");

you header on the top of this C file states gpl 2 only, but this says GPL 2+.

I'll fix it.

Thanks,

David

Quoted 5 lines

cheers

Signature · 1 line
balbi
#11 lore raw

Hi,

Quoted 11 lines · David Cohen

Hi Felipe,

Thanks replying.

Quoted 6 lines · Felipe Balbi

Hi,

Quoted 3 lines · David Cohen

Some platforms have an USB OTG port fully (or partially) controlled by GPIOs:

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

(1) USB ID is connected directly to GPIO

Optionally:
(2) VBUS is enabled by a GPIO (when ID is grounded)

ok, so a fixed regulator with a GPIO enable pin.

Pretty much yes.

ok

Quoted 32 lines
Quoted 23 lines
Quoted 3 lines

(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

so you have discrete mux with a GPIO select signal, like below ?

 .-------.----------------.  .--------.
 |       |                |  |        |      D+
 |       |                |  |        |<-------------.
 |       |                |  |        |              |
 |       |    USB Host    -->|    P   |              |
 |       |                |  |    H   |              |
 |       |                |  |    Y   |    D-        |
 |       '----------------'  |    0   |<--------.    |
 |                       |   |        |         |    |
 |                       |   '--------'      .--------.  D+
 |                       |                   |        |------>
 |       SOC        GPIO | ----------------->|        |
 |                       |                   |   MUX  |
 |                       |                   |        |------>
 |                       |   .--------.      '--------'  D-
 |       .----------------.  |        |   D-  |      |
Quoted 8 lines
 |       |                |  |    P   |<------'      |
 |       |                |  |    H   |              |
 |       |                |  |    Y   |              |
 |       |   USB Device   -->|    1   |              |
 |       |                |  |        |      D+      |
 |       |                |  |        |<-------------'
 |       |                |  |        |
 '-------'----------------'  '--------'
Quoted 1 line

Nice ASCII pic :)

asciio ftw \o/

Quoted 1 line

Yes, that's the case.

alright

Quoted 63 lines
Quoted 10 lines

I have been on and off about writing a pinctrl-gpio.c driver which would allow us to hide this detail from users. It shouldn't really matter which modes are available behind the mux, but we should be able to tell the mux to go into mode 0 or mode 1 by toggling its select signal. And it shouldn't really matter that we have a GPIO pin. The problem is: I don't really know if pinctrl would be able to handle discrete muxes.

Adding Linus W to ask. Linus, what do you think ? Should we have a pinctrl-gpio.c for such cases ? In TI we too have quite a few boards which different modes hidden behind discrete muxes.

An input from Linus would fine in this case.

Quoted 44 lines
Quoted 5 lines

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:
- If yes, OTG port is configured for host role
- If no, by standard, the OTG port is configured for device role

correct, this default-B is mandated by OTG spec anyway.

Quoted 11 lines

---

Hi,

Some Intel Bay Trail boards have an unusual way to handle the USB OTG port:
- The USB ID pin is connected directly to GPIO on SoC
- When in host role, VBUS is provided by enabling a GPIO
- Device and host roles are supported by 2 independent controllers with D+/- pins from port switched between different phys according a GPIO level.

Quoted 4 lines

The ACPI table describes this USB port as a (virtual) device with all the necessary GPIOs. This driver implements support to this virtual device as an extcon class driver. All drivers that depend on the USB OTG port state (USB phy, PMIC, charge detection) will listen to extcon events.

Right I think you're almost there, but I still think that this needs to be a bit broken down. First, we need some generic DRD library under drivers/usb/common, and that should be the one listening to extcon cable events. But your extcon driver should be doing only that: checking which cable was attached, it shouldn't be doing the switch by itself. That should be part of the DRD library.

That DRD library would also be the one enabling the (optional) VBUS regulator.

George Cherian tried to implement a generic DRD library but I think there are still too many changes happening on usbcore and udc-core. Most of the pieces are already there (usb_del_hcd() and usb_del_gadget_udc() know how to properly unload an hcd/udc), but there are details missing, no doubt.

If we can find a way to broadcast (probably not the best term, but whatever) a "Hey ID pin was just grounded" message, we can get things working.

Quoted 5 lines

That message, btw, shouldn't really depend on extcon-gpio alone because other platforms might use non-gpio methods to verify ID pin level. In any case, we need to have generic ID_PIN_LOW and ID_PIN_HIGH messages that a generic DRD library can listen to and load/unload the correct drivers by means of usb_{add,del}_{hcd,gadget_udc}().

Quoted 4 lines

IMHO extcon is the correct way to broadcast it, as long as we define a standard for the cable names. E.g. DRD library could listen to "USB-HOST" cable state. Then it doesn't matter how ID pin is grounded, it just matters that whoever is controlling it broadcast via this cable.

right, the likelyhood that someone would not use a GPIO is also quite minimal and for such cases, the controller would likely switch roles automatically (like with MUSB).

Quoted 7 lines
Quoted 2 lines

With that in mind, I think you can use extcon-gpio.c for your purposes if the other pieces can be fullfilled by regulator and pinctrl.

In my case we have all gpios listed in a single ACPI device. In order to be backwards compatible with products already on market, we'd need something like a single mfd to register platform devices for this smaller pieces (extcon gpio, possible pintrl gpio, maybe vbus as regulator??).

correct.

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
drivers/extcon/extcon-otg_gpio.c new file +10 -0
+	ret = devm_request_threaded_irq(dev, gpiod_to_irq(vup->gpio_usb_id),
+					vuport_isr, vuport_thread_isr,
+					IRQF_SHARED | IRQF_TRIGGER_RISING |
+					IRQF_TRIGGER_FALLING,
+					dev_name(dev), vup);
+	if (ret < 0) {
+		dev_err(dev, "cannot request IRQ for USB ID GPIO: ret = %d\n",
+			ret);
+		goto irq_err;
+	}
Quoted 8 lines

drivers/extcon/extcon-otg_gpio.c line 154

Quoted 5 lines
drivers/extcon/extcon-otg_gpio.c new file +5 -0
+	vuport_do_usb_id(vup);
+
+	platform_set_drvdata(pdev, vup);
+
+	dev_info(dev, "driver successfully probed\n");

this will just make boot noisier, make it dev_dbg() ? Or even dev_vdbg() ?

Quoted 1 line

dev_dgb() perhaps.

sure, why not :-)

Signature · 1 line
balbi
#12 lore raw

Hi Felipe and Linus,

Quoted 140 lines · Felipe Balbi

Hi,

Quoted 11 lines · David Cohen

Hi Felipe,

Thanks replying.

Quoted 6 lines · Felipe Balbi

Hi,

Quoted 3 lines · David Cohen

Some platforms have an USB OTG port fully (or partially) controlled by GPIOs:

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

(1) USB ID is connected directly to GPIO

Optionally:
(2) VBUS is enabled by a GPIO (when ID is grounded)

ok, so a fixed regulator with a GPIO enable pin.

Pretty much yes.

ok

Quoted 32 lines
Quoted 23 lines
Quoted 3 lines

(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

so you have discrete mux with a GPIO select signal, like below ?

 .-------.----------------.  .--------.
 |       |                |  |        |      D+
 |       |                |  |        |<-------------.
 |       |                |  |        |              |
 |       |    USB Host    -->|    P   |              |
 |       |                |  |    H   |              |
 |       |                |  |    Y   |    D-        |
 |       '----------------'  |    0   |<--------.    |
 |                       |   |        |         |    |
 |                       |   '--------'      .--------.  D+
 |                       |                   |        |------>
 |       SOC        GPIO | ----------------->|        |
 |                       |                   |   MUX  |
 |                       |                   |        |------>
 |                       |   .--------.      '--------'  D-
 |       .----------------.  |        |   D-  |      |
Quoted 8 lines
 |       |                |  |    P   |<------'      |
 |       |                |  |    H   |              |
 |       |                |  |    Y   |              |
 |       |   USB Device   -->|    1   |              |
 |       |                |  |        |      D+      |
 |       |                |  |        |<-------------'
 |       |                |  |        |
 '-------'----------------'  '--------'
Quoted 1 line

Nice ASCII pic :)

asciio ftw \o/

Quoted 1 line

Yes, that's the case.

alright

Quoted 63 lines
Quoted 10 lines

I have been on and off about writing a pinctrl-gpio.c driver which would allow us to hide this detail from users. It shouldn't really matter which modes are available behind the mux, but we should be able to tell the mux to go into mode 0 or mode 1 by toggling its select signal. And it shouldn't really matter that we have a GPIO pin. The problem is: I don't really know if pinctrl would be able to handle discrete muxes.

Adding Linus W to ask. Linus, what do you think ? Should we have a pinctrl-gpio.c for such cases ? In TI we too have quite a few boards which different modes hidden behind discrete muxes.

An input from Linus would fine in this case.

Quoted 44 lines
Quoted 5 lines

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:
- If yes, OTG port is configured for host role
- If no, by standard, the OTG port is configured for device role

correct, this default-B is mandated by OTG spec anyway.

Quoted 11 lines

---

Hi,

Some Intel Bay Trail boards have an unusual way to handle the USB OTG port:
- The USB ID pin is connected directly to GPIO on SoC
- When in host role, VBUS is provided by enabling a GPIO
- Device and host roles are supported by 2 independent controllers with D+/- pins from port switched between different phys according a GPIO level.

Quoted 4 lines

The ACPI table describes this USB port as a (virtual) device with all the necessary GPIOs. This driver implements support to this virtual device as an extcon class driver. All drivers that depend on the USB OTG port state (USB phy, PMIC, charge detection) will listen to extcon events.

Right I think you're almost there, but I still think that this needs to be a bit broken down. First, we need some generic DRD library under drivers/usb/common, and that should be the one listening to extcon cable events. But your extcon driver should be doing only that: checking which cable was attached, it shouldn't be doing the switch by itself. That should be part of the DRD library.

That DRD library would also be the one enabling the (optional) VBUS regulator.

George Cherian tried to implement a generic DRD library but I think there are still too many changes happening on usbcore and udc-core. Most of the pieces are already there (usb_del_hcd() and usb_del_gadget_udc() know how to properly unload an hcd/udc), but there are details missing, no doubt.

If we can find a way to broadcast (probably not the best term, but whatever) a "Hey ID pin was just grounded" message, we can get things working.

Quoted 5 lines

That message, btw, shouldn't really depend on extcon-gpio alone because other platforms might use non-gpio methods to verify ID pin level. In any case, we need to have generic ID_PIN_LOW and ID_PIN_HIGH messages that a generic DRD library can listen to and load/unload the correct drivers by means of usb_{add,del}_{hcd,gadget_udc}().

Quoted 4 lines

IMHO extcon is the correct way to broadcast it, as long as we define a standard for the cable names. E.g. DRD library could listen to "USB-HOST" cable state. Then it doesn't matter how ID pin is grounded, it just matters that whoever is controlling it broadcast via this cable.

right, the likelyhood that someone would not use a GPIO is also quite minimal and for such cases, the controller would likely switch roles automatically (like with MUSB).

Quoted 3 lines
Quoted 2 lines

With that in mind, I think you can use extcon-gpio.c for your purposes if the other pieces can be fullfilled by regulator and pinctrl.

Quoted 6 lines
Quoted 4 lines

In my case we have all gpios listed in a single ACPI device. In order to be backwards compatible with products already on market, we'd need something like a single mfd to register platform devices for this smaller pieces (extcon gpio, possible pintrl gpio, maybe vbus as regulator??).

correct.

Getting back to this case :)
Guess I need to get back my words.

extcon-gpio.c cannot work out-of-the-box with my case. There is no clean way to get the GPIO given to this device via ACPI and refer it to another device (i.e. extcon-gpio).

Here's my scenario:
This platform has only one ACPI device representing the USB port with 3 gpios controlling it. As GPIO consumer, there is no clean interface where I could get a GPIO descriptor via ACPI without requesting it. After request it, I cannot give it to extcon-gpio.c. Same would happen for a possible pinctrl gpio and regulator controller by gpio.

So my choices:
1) request GPIO locally, give it to other drivers and somehow inform them they should not request, but just to handle it (ugly)

2) implement a way to pass this GPIO resource to another device without requesting locally

3) stick with this driver fully handling the GPIOs which control this virtual "USB OTG port" device

Any thoughts?

Br, David

#13 lore raw

Hi,

Quoted 120 lines · David Cohen
Quoted 120 lines
Quoted 32 lines
Quoted 23 lines
Quoted 3 lines

(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

so you have discrete mux with a GPIO select signal, like below ?

 .-------.----------------.  .--------.
 |       |                |  |        |      D+
 |       |                |  |        |<-------------.
 |       |                |  |        |              |
 |       |    USB Host    -->|    P   |              |
 |       |                |  |    H   |              |
 |       |                |  |    Y   |    D-        |
 |       '----------------'  |    0   |<--------.    |
 |                       |   |        |         |    |
 |                       |   '--------'      .--------.  D+
 |                       |                   |        |------>
 |       SOC        GPIO | ----------------->|        |
 |                       |                   |   MUX  |
 |                       |                   |        |------>
 |                       |   .--------.      '--------'  D-
 |       .----------------.  |        |   D-  |      |
Quoted 8 lines
 |       |                |  |    P   |<------'      |
 |       |                |  |    H   |              |
 |       |                |  |    Y   |              |
 |       |   USB Device   -->|    1   |              |
 |       |                |  |        |      D+      |
 |       |                |  |        |<-------------'
 |       |                |  |        |
 '-------'----------------'  '--------'
Quoted 1 line

Nice ASCII pic :)

asciio ftw \o/

Quoted 1 line

Yes, that's the case.

alright

Quoted 63 lines
Quoted 10 lines

I have been on and off about writing a pinctrl-gpio.c driver which would allow us to hide this detail from users. It shouldn't really matter which modes are available behind the mux, but we should be able to tell the mux to go into mode 0 or mode 1 by toggling its select signal. And it shouldn't really matter that we have a GPIO pin. The problem is: I don't really know if pinctrl would be able to handle discrete muxes.

Adding Linus W to ask. Linus, what do you think ? Should we have a pinctrl-gpio.c for such cases ? In TI we too have quite a few boards which different modes hidden behind discrete muxes.

An input from Linus would fine in this case.

Quoted 44 lines
Quoted 5 lines

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:
- If yes, OTG port is configured for host role
- If no, by standard, the OTG port is configured for device role

correct, this default-B is mandated by OTG spec anyway.

Quoted 11 lines

---

Hi,

Some Intel Bay Trail boards have an unusual way to handle the USB OTG port:
- The USB ID pin is connected directly to GPIO on SoC
- When in host role, VBUS is provided by enabling a GPIO
- Device and host roles are supported by 2 independent controllers with D+/- pins from port switched between different phys according a GPIO level.

Quoted 4 lines

The ACPI table describes this USB port as a (virtual) device with all the necessary GPIOs. This driver implements support to this virtual device as an extcon class driver. All drivers that depend on the USB OTG port state (USB phy, PMIC, charge detection) will listen to extcon events.

Right I think you're almost there, but I still think that this needs to be a bit broken down. First, we need some generic DRD library under drivers/usb/common, and that should be the one listening to extcon cable events. But your extcon driver should be doing only that: checking which cable was attached, it shouldn't be doing the switch by itself. That should be part of the DRD library.

That DRD library would also be the one enabling the (optional) VBUS regulator.

George Cherian tried to implement a generic DRD library but I think there are still too many changes happening on usbcore and udc-core. Most of the pieces are already there (usb_del_hcd() and usb_del_gadget_udc() know how to properly unload an hcd/udc), but there are details missing, no doubt.

If we can find a way to broadcast (probably not the best term, but whatever) a "Hey ID pin was just grounded" message, we can get things working.

Quoted 5 lines

That message, btw, shouldn't really depend on extcon-gpio alone because other platforms might use non-gpio methods to verify ID pin level. In any case, we need to have generic ID_PIN_LOW and ID_PIN_HIGH messages that a generic DRD library can listen to and load/unload the correct drivers by means of usb_{add,del}_{hcd,gadget_udc}().

Quoted 4 lines

IMHO extcon is the correct way to broadcast it, as long as we define a standard for the cable names. E.g. DRD library could listen to "USB-HOST" cable state. Then it doesn't matter how ID pin is grounded, it just matters that whoever is controlling it broadcast via this cable.

right, the likelyhood that someone would not use a GPIO is also quite minimal and for such cases, the controller would likely switch roles automatically (like with MUSB).

Quoted 7 lines
Quoted 2 lines

With that in mind, I think you can use extcon-gpio.c for your purposes if the other pieces can be fullfilled by regulator and pinctrl.

In my case we have all gpios listed in a single ACPI device. In order to be backwards compatible with products already on market, we'd need something like a single mfd to register platform devices for this smaller pieces (extcon gpio, possible pintrl gpio, maybe vbus as regulator??).

Quoted 8 lines
Quoted 1 line

correct.

Getting back to this case :)
Guess I need to get back my words.

extcon-gpio.c cannot work out-of-the-box with my case. There is no clean way to get the GPIO given to this device via ACPI and refer it to another device (i.e. extcon-gpio).

add what's missing ?

Quoted 11 lines

Here's my scenario:
This platform has only one ACPI device representing the USB port with 3 gpios controlling it. As GPIO consumer, there is no clean interface where I could get a GPIO descriptor via ACPI without requesting it. After request it, I cannot give it to extcon-gpio.c. Same would happen for a possible pinctrl gpio and regulator controller by gpio.

So my choices:
1) request GPIO locally, give it to other drivers and somehow inform them they should not request, but just to handle it (ugly)

Quoted 5 lines

2) implement a way to pass this GPIO resource to another device without requesting locally

3) stick with this driver fully handling the GPIOs which control this virtual "USB OTG port" device

4) grab gpio via ACPI, gpio_free() it, pass to this driver. Would that work ?

Signature · 1 line
balbi
#14 lore raw

Hi,

Adding Mika.

Quoted 145 lines · Felipe Balbi

Hi,

Quoted 120 lines · David Cohen
Quoted 120 lines
Quoted 32 lines
Quoted 23 lines
Quoted 3 lines

(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

so you have discrete mux with a GPIO select signal, like below ?

 .-------.----------------.  .--------.
 |       |                |  |        |      D+
 |       |                |  |        |<-------------.
 |       |                |  |        |              |
 |       |    USB Host    -->|    P   |              |
 |       |                |  |    H   |              |
 |       |                |  |    Y   |    D-        |
 |       '----------------'  |    0   |<--------.    |
 |                       |   |        |         |    |
 |                       |   '--------'      .--------.  D+
 |                       |                   |        |------>
 |       SOC        GPIO | ----------------->|        |
 |                       |                   |   MUX  |
 |                       |                   |        |------>
 |                       |   .--------.      '--------'  D-
 |       .----------------.  |        |   D-  |      |
Quoted 8 lines
 |       |                |  |    P   |<------'      |
 |       |                |  |    H   |              |
 |       |                |  |    Y   |              |
 |       |   USB Device   -->|    1   |              |
 |       |                |  |        |      D+      |
 |       |                |  |        |<-------------'
 |       |                |  |        |
 '-------'----------------'  '--------'
Quoted 1 line

Nice ASCII pic :)

asciio ftw \o/

Quoted 1 line

Yes, that's the case.

alright

Quoted 63 lines
Quoted 10 lines

I have been on and off about writing a pinctrl-gpio.c driver which would allow us to hide this detail from users. It shouldn't really matter which modes are available behind the mux, but we should be able to tell the mux to go into mode 0 or mode 1 by toggling its select signal. And it shouldn't really matter that we have a GPIO pin. The problem is: I don't really know if pinctrl would be able to handle discrete muxes.

Adding Linus W to ask. Linus, what do you think ? Should we have a pinctrl-gpio.c for such cases ? In TI we too have quite a few boards which different modes hidden behind discrete muxes.

An input from Linus would fine in this case.

Quoted 44 lines
Quoted 5 lines

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:
- If yes, OTG port is configured for host role
- If no, by standard, the OTG port is configured for device role

correct, this default-B is mandated by OTG spec anyway.

Quoted 11 lines

---

Hi,

Some Intel Bay Trail boards have an unusual way to handle the USB OTG port:
- The USB ID pin is connected directly to GPIO on SoC
- When in host role, VBUS is provided by enabling a GPIO
- Device and host roles are supported by 2 independent controllers with D+/- pins from port switched between different phys according a GPIO level.

Quoted 4 lines

The ACPI table describes this USB port as a (virtual) device with all the necessary GPIOs. This driver implements support to this virtual device as an extcon class driver. All drivers that depend on the USB OTG port state (USB phy, PMIC, charge detection) will listen to extcon events.

Right I think you're almost there, but I still think that this needs to be a bit broken down. First, we need some generic DRD library under drivers/usb/common, and that should be the one listening to extcon cable events. But your extcon driver should be doing only that: checking which cable was attached, it shouldn't be doing the switch by itself. That should be part of the DRD library.

That DRD library would also be the one enabling the (optional) VBUS regulator.

George Cherian tried to implement a generic DRD library but I think there are still too many changes happening on usbcore and udc-core. Most of the pieces are already there (usb_del_hcd() and usb_del_gadget_udc() know how to properly unload an hcd/udc), but there are details missing, no doubt.

If we can find a way to broadcast (probably not the best term, but whatever) a "Hey ID pin was just grounded" message, we can get things working.

Quoted 5 lines

That message, btw, shouldn't really depend on extcon-gpio alone because other platforms might use non-gpio methods to verify ID pin level. In any case, we need to have generic ID_PIN_LOW and ID_PIN_HIGH messages that a generic DRD library can listen to and load/unload the correct drivers by means of usb_{add,del}_{hcd,gadget_udc}().

Quoted 4 lines

IMHO extcon is the correct way to broadcast it, as long as we define a standard for the cable names. E.g. DRD library could listen to "USB-HOST" cable state. Then it doesn't matter how ID pin is grounded, it just matters that whoever is controlling it broadcast via this cable.

right, the likelyhood that someone would not use a GPIO is also quite minimal and for such cases, the controller would likely switch roles automatically (like with MUSB).

Quoted 7 lines
Quoted 2 lines

With that in mind, I think you can use extcon-gpio.c for your purposes if the other pieces can be fullfilled by regulator and pinctrl.

In my case we have all gpios listed in a single ACPI device. In order to be backwards compatible with products already on market, we'd need something like a single mfd to register platform devices for this smaller pieces (extcon gpio, possible pintrl gpio, maybe vbus as regulator??).

Quoted 8 lines
Quoted 1 line

correct.

Getting back to this case :)
Guess I need to get back my words.

extcon-gpio.c cannot work out-of-the-box with my case. There is no clean way to get the GPIO given to this device via ACPI and refer it to another device (i.e. extcon-gpio).

add what's missing ?

Quoted 11 lines

Here's my scenario:
This platform has only one ACPI device representing the USB port with 3 gpios controlling it. As GPIO consumer, there is no clean interface where I could get a GPIO descriptor via ACPI without requesting it. After request it, I cannot give it to extcon-gpio.c. Same would happen for a possible pinctrl gpio and regulator controller by gpio.

So my choices:
1) request GPIO locally, give it to other drivers and somehow inform them they should not request, but just to handle it (ugly)

Quoted 8 lines
Quoted 5 lines

2) implement a way to pass this GPIO resource to another device without requesting locally

3) stick with this driver fully handling the GPIOs which control this virtual "USB OTG port" device

4) grab gpio via ACPI, gpio_free() it, pass to this driver. Would that work ?

That works. But I feel it'd be same as 2. In case we don't want to implement 2, the same reasons would apply to not implement 4.

Linus, Mika, would you have any thoughts about case 4?

Br, David

#15 lore raw
Quoted 156 lines · David Cohen

Hi,

Adding Mika.

Quoted 151 lines · Felipe Balbi

Hi,

Quoted 120 lines · David Cohen
Quoted 120 lines
Quoted 32 lines
Quoted 23 lines
Quoted 3 lines

(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

so you have discrete mux with a GPIO select signal, like below ?

 .-------.----------------.  .--------.
 |       |                |  |        |      D+
 |       |                |  |        |<-------------.
 |       |                |  |        |              |
 |       |    USB Host    -->|    P   |              |
 |       |                |  |    H   |              |
 |       |                |  |    Y   |    D-        |
 |       '----------------'  |    0   |<--------.    |
 |                       |   |        |         |    |
 |                       |   '--------'      .--------.  D+
 |                       |                   |        |------>
 |       SOC        GPIO | ----------------->|        |
 |                       |                   |   MUX  |
 |                       |                   |        |------>
 |                       |   .--------.      '--------'  D-
 |       .----------------.  |        |   D-  |      |
Quoted 8 lines
 |       |                |  |    P   |<------'      |
 |       |                |  |    H   |              |
 |       |                |  |    Y   |              |
 |       |   USB Device   -->|    1   |              |
 |       |                |  |        |      D+      |
 |       |                |  |        |<-------------'
 |       |                |  |        |
 '-------'----------------'  '--------'
Quoted 1 line

Nice ASCII pic :)

asciio ftw \o/

Quoted 1 line

Yes, that's the case.

alright

Quoted 63 lines
Quoted 10 lines

I have been on and off about writing a pinctrl-gpio.c driver which would allow us to hide this detail from users. It shouldn't really matter which modes are available behind the mux, but we should be able to tell the mux to go into mode 0 or mode 1 by toggling its select signal. And it shouldn't really matter that we have a GPIO pin. The problem is: I don't really know if pinctrl would be able to handle discrete muxes.

Adding Linus W to ask. Linus, what do you think ? Should we have a pinctrl-gpio.c for such cases ? In TI we too have quite a few boards which different modes hidden behind discrete muxes.

An input from Linus would fine in this case.

Quoted 44 lines
Quoted 5 lines

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:
- If yes, OTG port is configured for host role
- If no, by standard, the OTG port is configured for device role

correct, this default-B is mandated by OTG spec anyway.

Quoted 11 lines

---

Hi,

Some Intel Bay Trail boards have an unusual way to handle the USB OTG port:
- The USB ID pin is connected directly to GPIO on SoC
- When in host role, VBUS is provided by enabling a GPIO
- Device and host roles are supported by 2 independent controllers with D+/- pins from port switched between different phys according a GPIO level.

Quoted 4 lines

The ACPI table describes this USB port as a (virtual) device with all the necessary GPIOs. This driver implements support to this virtual device as an extcon class driver. All drivers that depend on the USB OTG port state (USB phy, PMIC, charge detection) will listen to extcon events.

Right I think you're almost there, but I still think that this needs to be a bit broken down. First, we need some generic DRD library under drivers/usb/common, and that should be the one listening to extcon cable events. But your extcon driver should be doing only that: checking which cable was attached, it shouldn't be doing the switch by itself. That should be part of the DRD library.

That DRD library would also be the one enabling the (optional) VBUS regulator.

George Cherian tried to implement a generic DRD library but I think there are still too many changes happening on usbcore and udc-core. Most of the pieces are already there (usb_del_hcd() and usb_del_gadget_udc() know how to properly unload an hcd/udc), but there are details missing, no doubt.

If we can find a way to broadcast (probably not the best term, but whatever) a "Hey ID pin was just grounded" message, we can get things working.

Quoted 5 lines

That message, btw, shouldn't really depend on extcon-gpio alone because other platforms might use non-gpio methods to verify ID pin level. In any case, we need to have generic ID_PIN_LOW and ID_PIN_HIGH messages that a generic DRD library can listen to and load/unload the correct drivers by means of usb_{add,del}_{hcd,gadget_udc}().

Quoted 4 lines

IMHO extcon is the correct way to broadcast it, as long as we define a standard for the cable names. E.g. DRD library could listen to "USB-HOST" cable state. Then it doesn't matter how ID pin is grounded, it just matters that whoever is controlling it broadcast via this cable.

right, the likelyhood that someone would not use a GPIO is also quite minimal and for such cases, the controller would likely switch roles automatically (like with MUSB).

Quoted 7 lines
Quoted 2 lines

With that in mind, I think you can use extcon-gpio.c for your purposes if the other pieces can be fullfilled by regulator and pinctrl.

In my case we have all gpios listed in a single ACPI device. In order to be backwards compatible with products already on market, we'd need something like a single mfd to register platform devices for this smaller pieces (extcon gpio, possible pintrl gpio, maybe vbus as regulator??).

Quoted 8 lines
Quoted 1 line

correct.

Getting back to this case :)
Guess I need to get back my words.

extcon-gpio.c cannot work out-of-the-box with my case. There is no clean way to get the GPIO given to this device via ACPI and refer it to another device (i.e. extcon-gpio).

add what's missing ?

Quoted 16 lines

Here's my scenario:
This platform has only one ACPI device representing the USB port with 3 gpios controlling it. As GPIO consumer, there is no clean interface where I could get a GPIO descriptor via ACPI without requesting it. After request it, I cannot give it to extcon-gpio.c. Same would happen for a possible pinctrl gpio and regulator controller by gpio.

So my choices:
1) request GPIO locally, give it to other drivers and somehow inform them they should not request, but just to handle it (ugly)

2) implement a way to pass this GPIO resource to another device without requesting locally

3) stick with this driver fully handling the GPIOs which control this virtual "USB OTG port" device

Quoted 7 lines
Quoted 2 lines

4) grab gpio via ACPI, gpio_free() it, pass to this driver. Would that work ?

That works. But I feel it'd be same as 2. In case we don't want to implement 2, the same reasons would apply to not implement 4.

Linus, Mika, would you have any thoughts about case 4?

Isn't this exactly what Heikki suggested in his GPIO forwarding series here:

https://lkml.org/lkml/2014/12/18/82

#16 lore raw

Hi,

Quoted 163 lines · Mika Westerberg
Quoted 162 lines · David Cohen

Hi,

Adding Mika.

Quoted 145 lines · Felipe Balbi

Hi,

Quoted 120 lines · David Cohen
Quoted 120 lines
Quoted 32 lines
Quoted 23 lines
Quoted 3 lines

(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

so you have discrete mux with a GPIO select signal, like below ?

 .-------.----------------.  .--------.
 |       |                |  |        |      D+
 |       |                |  |        |<-------------.
 |       |                |  |        |              |
 |       |    USB Host    -->|    P   |              |
 |       |                |  |    H   |              |
 |       |                |  |    Y   |    D-        |
 |       '----------------'  |    0   |<--------.    |
 |                       |   |        |         |    |
 |                       |   '--------'      .--------.  D+
 |                       |                   |        |------>
 |       SOC        GPIO | ----------------->|        |
 |                       |                   |   MUX  |
 |                       |                   |        |------>
 |                       |   .--------.      '--------'  D-
 |       .----------------.  |        |   D-  |      |
Quoted 8 lines
 |       |                |  |    P   |<------'      |
 |       |                |  |    H   |              |
 |       |                |  |    Y   |              |
 |       |   USB Device   -->|    1   |              |
 |       |                |  |        |      D+      |
 |       |                |  |        |<-------------'
 |       |                |  |        |
 '-------'----------------'  '--------'
Quoted 1 line

Nice ASCII pic :)

asciio ftw \o/

Quoted 1 line

Yes, that's the case.

alright

Quoted 63 lines
Quoted 10 lines

I have been on and off about writing a pinctrl-gpio.c driver which would allow us to hide this detail from users. It shouldn't really matter which modes are available behind the mux, but we should be able to tell the mux to go into mode 0 or mode 1 by toggling its select signal. And it shouldn't really matter that we have a GPIO pin. The problem is: I don't really know if pinctrl would be able to handle discrete muxes.

Adding Linus W to ask. Linus, what do you think ? Should we have a pinctrl-gpio.c for such cases ? In TI we too have quite a few boards which different modes hidden behind discrete muxes.

An input from Linus would fine in this case.

Quoted 44 lines
Quoted 5 lines

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:
- If yes, OTG port is configured for host role
- If no, by standard, the OTG port is configured for device role

correct, this default-B is mandated by OTG spec anyway.

Quoted 11 lines

---

Hi,

Some Intel Bay Trail boards have an unusual way to handle the USB OTG port:
- The USB ID pin is connected directly to GPIO on SoC
- When in host role, VBUS is provided by enabling a GPIO
- Device and host roles are supported by 2 independent controllers with D+/- pins from port switched between different phys according a GPIO level.

Quoted 4 lines

The ACPI table describes this USB port as a (virtual) device with all the necessary GPIOs. This driver implements support to this virtual device as an extcon class driver. All drivers that depend on the USB OTG port state (USB phy, PMIC, charge detection) will listen to extcon events.

Right I think you're almost there, but I still think that this needs to be a bit broken down. First, we need some generic DRD library under drivers/usb/common, and that should be the one listening to extcon cable events. But your extcon driver should be doing only that: checking which cable was attached, it shouldn't be doing the switch by itself. That should be part of the DRD library.

That DRD library would also be the one enabling the (optional) VBUS regulator.

George Cherian tried to implement a generic DRD library but I think there are still too many changes happening on usbcore and udc-core. Most of the pieces are already there (usb_del_hcd() and usb_del_gadget_udc() know how to properly unload an hcd/udc), but there are details missing, no doubt.

If we can find a way to broadcast (probably not the best term, but whatever) a "Hey ID pin was just grounded" message, we can get things working.

Quoted 5 lines

That message, btw, shouldn't really depend on extcon-gpio alone because other platforms might use non-gpio methods to verify ID pin level. In any case, we need to have generic ID_PIN_LOW and ID_PIN_HIGH messages that a generic DRD library can listen to and load/unload the correct drivers by means of usb_{add,del}_{hcd,gadget_udc}().

Quoted 4 lines

IMHO extcon is the correct way to broadcast it, as long as we define a standard for the cable names. E.g. DRD library could listen to "USB-HOST" cable state. Then it doesn't matter how ID pin is grounded, it just matters that whoever is controlling it broadcast via this cable.

right, the likelyhood that someone would not use a GPIO is also quite minimal and for such cases, the controller would likely switch roles automatically (like with MUSB).

Quoted 7 lines
Quoted 2 lines

With that in mind, I think you can use extcon-gpio.c for your purposes if the other pieces can be fullfilled by regulator and pinctrl.

In my case we have all gpios listed in a single ACPI device. In order to be backwards compatible with products already on market, we'd need something like a single mfd to register platform devices for this smaller pieces (extcon gpio, possible pintrl gpio, maybe vbus as regulator??).

Quoted 8 lines
Quoted 1 line

correct.

Getting back to this case :)
Guess I need to get back my words.

extcon-gpio.c cannot work out-of-the-box with my case. There is no clean way to get the GPIO given to this device via ACPI and refer it to another device (i.e. extcon-gpio).

add what's missing ?

Quoted 11 lines

Here's my scenario:
This platform has only one ACPI device representing the USB port with 3 gpios controlling it. As GPIO consumer, there is no clean interface where I could get a GPIO descriptor via ACPI without requesting it. After request it, I cannot give it to extcon-gpio.c. Same would happen for a possible pinctrl gpio and regulator controller by gpio.

So my choices:
1) request GPIO locally, give it to other drivers and somehow inform them they should not request, but just to handle it (ugly)

Quoted 8 lines
Quoted 5 lines

2) implement a way to pass this GPIO resource to another device without requesting locally

3) stick with this driver fully handling the GPIOs which control this virtual "USB OTG port" device

4) grab gpio via ACPI, gpio_free() it, pass to this driver. Would that work ?

That works. But I feel it'd be same as 2. In case we don't want to implement 2, the same reasons would apply to not implement 4.

Quoted 6 lines
Quoted 1 line

Linus, Mika, would you have any thoughts about case 4?

Isn't this exactly what Heikki suggested in his GPIO forwarding series here:

https://lkml.org/lkml/2014/12/18/82

Yes, that's pretty much the case. It's a dead end for case 2 (and 4). If we split this driver into smaller pieces, we'll have to force kernel to hack GPIO ownership.

I get back to the original proposal in this case. I can send a second version addressing all comments I received.

Comments? :)

BR, David

#17 lore raw
In reply to David Cohen
Quoted 11 lines · David Cohen

Some platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to GPIO

Optionally:
(2) VBUS is enabled by a GPIO (when ID is grounded)
(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

Quoted 6 lines

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:
- If yes, OTG port is configured for host role
- If no, by standard, the OTG port is configured for device role

Pretty interesting! I don't understand the USB stuff so commenting from a GPIO side of things only.

drivers/extcon/Kconfig config EXTCON_SM5502 · line 98

Quoted 3 lines
drivers/extcon/Kconfig +3 -0
+config EXTCON_OTG_GPIO
+       tristate "VIRTUAL USB OTG PORT support"
+       depends on GPIOLIB

Isn't it dependent on ACPI? This was mentioned in the commit message.

Quoted 9 lines
drivers/extcon/extcon-otg_gpio.c new file +9 -0
+/*
+ * Virtual USB OTG Port driver controlled by gpios
+ *
+ * Copyright (c) 2014, Intel Corporation.
+ * Author: David Cohen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.

drivers/extcon/extcon-otg_gpio.c line 19

Quoted 10 lines
drivers/extcon/extcon-otg_gpio.c new file +10 -0
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/acpi.h>
+#include <linux/extcon.h>
+#include <linux/gpio.h>

You should include <linux/gpio/consumer.h>

And nothing else. (I think it'll just work.)

drivers/extcon/extcon-otg_gpio.c line 191

Quoted 5 lines
drivers/extcon/extcon-otg_gpio.c new file +5 -0
+static int __init vuport_init(void)
+{
+       return platform_driver_register(&vuport_driver);
+}
+subsys_initcall(vuport_init);

Usually we try to avoid this kind of early initcalls. Doesn't deferred probe work as intended?

Yours,
Linus Walleij

#18 lore raw

Hi Linus,

Thanks for reviewing.

Quoted 21 lines · Linus Walleij
Quoted 11 lines · David Cohen

Some platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to GPIO

Optionally:
(2) VBUS is enabled by a GPIO (when ID is grounded)
(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys by GPIO.

Quoted 6 lines

As per initial version, this driver has the duty to control whether USB-Host cable is plugged in or not:
- If yes, OTG port is configured for host role
- If no, by standard, the OTG port is configured for device role

Quoted 8 lines

Pretty interesting! I don't understand the USB stuff so commenting from a GPIO side of things only.

drivers/extcon/Kconfig config EXTCON_SM5502 · line 98

Quoted 3 lines
drivers/extcon/Kconfig +3 -0
+config EXTCON_OTG_GPIO
+       tristate "VIRTUAL USB OTG PORT support"
+       depends on GPIOLIB

Isn't it dependent on ACPI? This was mentioned in the commit message.

Yep, I'll add it :)

Quoted 23 lines
Quoted 10 lines
drivers/extcon/extcon-otg_gpio.c new file +9 -0
+/*
+ * Virtual USB OTG Port driver controlled by gpios
+ *
+ * Copyright (c) 2014, Intel Corporation.
+ * Author: David Cohen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.

drivers/extcon/extcon-otg_gpio.c line 19

Quoted 10 lines
drivers/extcon/extcon-otg_gpio.c new file +10 -0
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/acpi.h>
+#include <linux/extcon.h>
+#include <linux/gpio.h>

You should include <linux/gpio/consumer.h>

Quoted 1 line

And nothing else. (I think it'll just work.)

It should work. I'll fix it.

Quoted 9 lines

drivers/extcon/extcon-otg_gpio.c line 191

Quoted 6 lines
drivers/extcon/extcon-otg_gpio.c new file +5 -0
+static int __init vuport_init(void)
+{
+       return platform_driver_register(&vuport_driver);
+}
+subsys_initcall(vuport_init);

Usually we try to avoid this kind of early initcalls. Doesn't deferred probe work as intended?

Yeah, deferred probe is a better thing to try here.

Br, David

Quoted 3 lines

Yours,
Linus Walleij

#19 lore raw

PATCH v2 1/1 extcon: otg_gpio: add driver for USB OTG port controlled by GPIO(s)

In reply to David Cohen

Some Intel platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to a pulled up GPIO.

Optionally:
(2) VBUS is enabled/disabled by a GPIO
(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys. according to this GPIO level.

This driver configures USB OTG port for device or host role according to USB ID value.
- If USB ID's GPIO level is low, OTG port is configured for host role by sourcing VBUS and switching D+/- to host phy.
- If USB ID's GPIO level is high, by standard, the OTG port is configured for device role by not sourcing VBUS and switching D+/- to device controller.

---

Hi,

Since splitting this driver into smaller pieces would result in ugly fixes WRT ACPI, I'm resending the same approach.
This time I addressed all comments I got from RFC version.

As always, comments are welcome.

Br, David
---

 drivers/extcon/Kconfig           |   8 ++
 drivers/extcon/Makefile          |   1 +
 drivers/extcon/extcon-otg_gpio.c | 186 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 195 insertions(+)
 create mode 100644 drivers/extcon/extcon-otg_gpio.c
drivers/extcon/Kconfig +8 -0
index 6a1f7de6fa54..986ca7da9c1b 100644
@@ -63,6 +63,14 @@ config EXTCON_MAX8997
 	  Maxim MAX8997 PMIC. The MAX8997 MUIC is a USB port accessory
 	  detector and switch.
 
+config EXTCON_OTG_GPIO
+	tristate "VIRTUAL USB OTG PORT support"
+	depends on GPIOLIB && ACPI
+	help
+	  Say Y here to enable support for virtual USB OTG port device
+	  controlled by GPIOs. This driver can be used when at least USB ID pin
+	  is connected directly to GPIO.
+
 config EXTCON_PALMAS
 	tristate "Palmas USB EXTCON support"
 	depends on MFD_PALMAS
drivers/extcon/Makefile +1 -0
index 0370b42e5a27..0d5b152b51ea 100644
@@ -9,6 +9,7 @@ obj-$(CONFIG_EXTCON_GPIO)	+= extcon-gpio.o
 obj-$(CONFIG_EXTCON_MAX14577)	+= extcon-max14577.o
 obj-$(CONFIG_EXTCON_MAX77693)	+= extcon-max77693.o
 obj-$(CONFIG_EXTCON_MAX8997)	+= extcon-max8997.o
+obj-$(CONFIG_EXTCON_OTG_GPIO)	+= extcon-otg_gpio.o
 obj-$(CONFIG_EXTCON_PALMAS)	+= extcon-palmas.o
 obj-$(CONFIG_EXTCON_RT8973A)	+= extcon-rt8973a.o
 obj-$(CONFIG_EXTCON_SM5502)	+= extcon-sm5502.o
drivers/extcon/extcon-otg_gpio.c new file +186 -0

186 lines not on this page · raw message

Signature · 1 line
2.1.1
#20 lore raw
Quoted 1 line · David Cohen

As always, comments are welcome.

Are nits welcome too?

Quoted 1 line

+MODULE_LICENSE("GPLv2");

You probably meant
MODULE_LICENSE("GPL v2")

Didn't that trigger a warning or error?

Paul Bolle

#21 lore raw

Hi,

Quoted 11 lines · Paul Bolle
Quoted 1 line · David Cohen

As always, comments are welcome.

Are nits welcome too?

Quoted 1 line

+MODULE_LICENSE("GPLv2");

You probably meant
MODULE_LICENSE("GPL v2")

Didn't that trigger a warning or error?

checkpatch showed no warning about that, not even with --strict option. I believe both ways are fine. But I can add the space.

Br, David

Quoted 4 lines

Paul Bolle

#22 lore raw
Quoted 10 lines · David Cohen

Hi,

Quoted 7 lines · Paul Bolle
Quoted 1 line · David Cohen

As always, comments are welcome.

Are nits welcome too?

Quoted 1 line

+MODULE_LICENSE("GPLv2");

Quoted 7 lines
Quoted 4 lines

You probably meant
MODULE_LICENSE("GPL v2")

Didn't that trigger a warning or error?

checkpatch showed no warning about that, not even with --strict option. I believe both ways are fine. But I can add the space.

Documentation says it should be with space:

/*
* The following license idents are currently accepted as indicating free
* software modules
*
* "GPL" [GNU Public License v2 or later]
* "GPL v2" [GNU Public License v2]
* "GPL and additional rights" [GNU Public License v2 rights and more]
* "Dual BSD/GPL" [GNU Public License v2
* or BSD license choice]
* "Dual MIT/GPL" [GNU Public License v2
* or MIT license choice]
* "Dual MPL/GPL" [GNU Public License v2
* or Mozilla license choice]
*
* The following other idents are available
*
* "Proprietary" [Non free products]
*
* There are dual licensed components, but when running with Linux it is the
* GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
* is a GPL combined work.
*
* This exists for several reasons
* 1. So modinfo can show license info for users wanting to vet their setup
* is free
* 2. So the community can ignore bug reports including proprietary modules
* 3. So vendors can do likewise based on their own policies
*/
#define MODULE_LICENSE(_license) MODULE_INFO(license, _license)

Signature · 1 line
balbi
#23 lore raw
Quoted 19 lines · Felipe Balbi
Quoted 10 lines · David Cohen

Hi,

Quoted 7 lines · Paul Bolle
Quoted 1 line · David Cohen

As always, comments are welcome.

Are nits welcome too?

Quoted 1 line

+MODULE_LICENSE("GPLv2");

Quoted 7 lines
Quoted 4 lines

You probably meant
MODULE_LICENSE("GPL v2")

Didn't that trigger a warning or error?

checkpatch showed no warning about that, not even with --strict option. I believe both ways are fine. But I can add the space.

Quoted 8 lines

Documentation says it should be with space:

/*
* The following license idents are currently accepted as indicating free
* software modules
*
* "GPL" [GNU Public License v2 or later]
* "GPL v2" [GNU Public License v2]

Thanks :)
I'll add it.

Br, David

#24 lore raw
In reply to David Cohen
Quoted 8 lines · David Cohen
Quoted 7 lines · Paul Bolle
Quoted 1 line · David Cohen

As always, comments are welcome.

Are nits welcome too?

Quoted 1 line

+MODULE_LICENSE("GPLv2");

Quoted 7 lines
Quoted 4 lines

You probably meant
MODULE_LICENSE("GPL v2")

Didn't that trigger a warning or error?

checkpatch showed no warning about that, not even with --strict option. I believe both ways are fine. But I can add the space.

"GPLv2" is not mentioned in include/linux/license.h so, as I remember, it would taint the kernel, and a warning will be printed when you load that module.

Thanks,

Paul Bolle

#25 lore raw
Quoted 12 lines · Paul Bolle
Quoted 11 lines · David Cohen
Quoted 10 lines · Paul Bolle
Quoted 1 line · David Cohen

As always, comments are welcome.

Are nits welcome too?

Quoted 1 line

+MODULE_LICENSE("GPLv2");

You probably meant
MODULE_LICENSE("GPL v2")

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Didn't that trigger a warning or error?

checkpatch showed no warning about that, not even with --strict option. I believe both ways are fine. But I can add the space.

"GPLv2" is not mentioned in include/linux/license.h so, as I remember, it would taint the kernel, and a warning will be printed when you load that module.

Ack :)
I'll add a space on next version.

Br, David

Quoted 6 lines

Thanks,

Paul Bolle

#26 lore raw
In reply to David Cohen

Hi David,

Quoted 11 lines · David Cohen

Some Intel platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to a pulled up GPIO.

Optionally:
(2) VBUS is enabled/disabled by a GPIO
(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys. according to this GPIO level.

Quoted 7 lines

This driver configures USB OTG port for device or host role according to USB ID value.
- If USB ID's GPIO level is low, OTG port is configured for host role by sourcing VBUS and switching D+/- to host phy.
- If USB ID's GPIO level is high, by standard, the OTG port is configured for device role by not sourcing VBUS and switching D+/- to device controller.

IMO it's not very elegant to handle VBUS power on/off in extcon driver. Creating fixed regulator would allow to make VBUS handling more generic. Then extcon client could handle VBUS regulator. Also D+/- routing would be separated but I don't see better place for it.

Odroid platforms also uses GPIO for USB cable detection so your driver would be useful in that case. I created some simple driver for it but I see I could use your driver after adding VBUS detection.

BTW don't you have VBUS detection on your board? It would allow to distinguish between USB, USB-Host and disconnected states.

--
Robert

Quoted 245 lines

---

Hi,

Since splitting this driver into smaller pieces would result in ugly fixes WRT ACPI, I'm resending the same approach.
This time I addressed all comments I got from RFC version.

As always, comments are welcome.

Br, David
---

 drivers/extcon/Kconfig           |   8 ++
 drivers/extcon/Makefile          |   1 +
 drivers/extcon/extcon-otg_gpio.c | 186 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 195 insertions(+)
 create mode 100644 drivers/extcon/extcon-otg_gpio.c
drivers/extcon/Kconfig +8 -0
index 6a1f7de6fa54..986ca7da9c1b 100644
@@ -63,6 +63,14 @@ config EXTCON_MAX8997
 	  Maxim MAX8997 PMIC. The MAX8997 MUIC is a USB port accessory
 	  detector and switch.
 
+config EXTCON_OTG_GPIO
+	tristate "VIRTUAL USB OTG PORT support"
+	depends on GPIOLIB && ACPI
+	help
+	  Say Y here to enable support for virtual USB OTG port device
+	  controlled by GPIOs. This driver can be used when at least USB ID pin
+	  is connected directly to GPIO.
+
 config EXTCON_PALMAS
 	tristate "Palmas USB EXTCON support"
 	depends on MFD_PALMAS
drivers/extcon/Makefile +1 -0
index 0370b42e5a27..0d5b152b51ea 100644
@@ -9,6 +9,7 @@ obj-$(CONFIG_EXTCON_GPIO)	+= extcon-gpio.o
 obj-$(CONFIG_EXTCON_MAX14577)	+= extcon-max14577.o
 obj-$(CONFIG_EXTCON_MAX77693)	+= extcon-max77693.o
 obj-$(CONFIG_EXTCON_MAX8997)	+= extcon-max8997.o
+obj-$(CONFIG_EXTCON_OTG_GPIO)	+= extcon-otg_gpio.o
 obj-$(CONFIG_EXTCON_PALMAS)	+= extcon-palmas.o
 obj-$(CONFIG_EXTCON_RT8973A)	+= extcon-rt8973a.o
 obj-$(CONFIG_EXTCON_SM5502)	+= extcon-sm5502.o
drivers/extcon/extcon-otg_gpio.c new file +186 -0
new file mode 100644
index 000000000000..7dfa2c15b562
@@ -0,0 +1,186 @@
+/*
+ * Virtual USB OTG Port driver controlled by gpios
+ *
+ * Copyright (c) 2014, Intel Corporation.
+ * Author: David Cohen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/acpi.h>
+#include <linux/extcon.h>
+#include <linux/gpio/consumer.h>
+#include <linux/interrupt.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+
+#define DRV_NAME	"usb_otg_port"
+
+struct vuport {
+	struct device *dev;
+	struct gpio_desc *gpio_vbus_en;
+	struct gpio_desc *gpio_usb_id;
+	struct gpio_desc *gpio_usb_mux;
+
+	struct extcon_dev edev;
+};
+
+static const char *vuport_extcon_cable[] = {
+	[0] = "USB-Host",
+	NULL,
+};
+
+/*
+ * If id == 1, USB port should be set to peripheral
+ * if id == 0, USB port should be set to host
+ *
+ * Peripheral: set USB mux to peripheral and disable VBUS
+ * Host: set USB mux to host and enable VBUS
+ */
+static void vuport_set_port(struct vuport *vup, int id)
+{
+	int mux_val = id;
+	int vbus_val = !id;
+
+	if (!IS_ERR(vup->gpio_usb_mux))
+		gpiod_direction_output(vup->gpio_usb_mux, mux_val);
+
+	if (!IS_ERR(vup->gpio_vbus_en))
+		gpiod_direction_output(vup->gpio_vbus_en, vbus_val);
+}
+
+static void vuport_do_usb_id(struct vuport *vup)
+{
+	int id = gpiod_get_value(vup->gpio_usb_id);
+
+	dev_info(vup->dev, "USB PORT ID: %s\n", id ? "PERIPHERAL" : "HOST");
+
+	/*
+	 * id == 1: PERIPHERAL
+	 * id == 0: HOST
+	 */
+	vuport_set_port(vup, id);
+
+	/*
+	 * id == 0: HOST connected
+	 * id == 1: Host disconnected
+	 */
+	extcon_set_cable_state(&vup->edev, "USB-Host", !id);
+}
+
+static irqreturn_t vuport_thread_isr(int irq, void *priv)
+{
+	struct vuport *vup = priv;
+
+	vuport_do_usb_id(vup);
+	return IRQ_HANDLED;
+}
+
+#define VUPORT_GPIO_USB_ID	0
+#define VUPORT_GPIO_VBUS_EN	1
+#define VUPORT_GPIO_USB_MUX	2
+static int vuport_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct vuport *vup;
+	int ret;
+
+	vup = devm_kzalloc(dev, sizeof(*vup), GFP_KERNEL);
+	if (!vup) {
+		dev_err(dev, "cannot allocate private data\n");
+		return -ENOMEM;
+	}
+	vup->dev = dev;
+
+	vup->gpio_usb_id = devm_gpiod_get_index(dev, "id", VUPORT_GPIO_USB_ID);
+	if (IS_ERR(vup->gpio_usb_id)) {
+		dev_err(dev, "cannot request USB ID GPIO: ret = %ld\n",
+			PTR_ERR(vup->gpio_usb_id));
+		return PTR_ERR(vup->gpio_usb_id);
+	}
+
+	ret = gpiod_direction_input(vup->gpio_usb_id);
+	if (ret < 0) {
+		dev_err(dev, "cannot set input direction to USB ID GPIO: ret = %d\n",
+			ret);
+		return ret;
+	}
+
+	vup->gpio_vbus_en = devm_gpiod_get_index(dev, "vbus en",
+						 VUPORT_GPIO_VBUS_EN);
+	if (IS_ERR(vup->gpio_vbus_en))
+		dev_dbg(dev, "cannot request VBUS EN GPIO, skipping it.\n");
+
+	vup->gpio_usb_mux = devm_gpiod_get_index(dev, "usb mux",
+						 VUPORT_GPIO_USB_MUX);
+	if (IS_ERR(vup->gpio_usb_mux))
+		dev_dbg(dev, "cannot request USB USB MUX, skipping it.\n");
+
+	/* register extcon device */
+	vup->edev.dev.parent = dev;
+	vup->edev.supported_cable = vuport_extcon_cable;
+	ret = extcon_dev_register(&vup->edev);
+	if (ret < 0) {
+		dev_err(dev, "failed to register extcon device: ret = %d\n",
+			ret);
+		return ret;
+	}
+
+	ret = devm_request_threaded_irq(dev, gpiod_to_irq(vup->gpio_usb_id),
+					NULL, vuport_thread_isr,
+					IRQF_SHARED | IRQF_TRIGGER_RISING |
+					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
+					dev_name(dev), vup);
+	if (ret < 0) {
+		dev_err(dev, "cannot request IRQ for USB ID GPIO: ret = %d\n",
+			ret);
+		goto irq_err;
+	}
+	vuport_do_usb_id(vup);
+
+	platform_set_drvdata(pdev, vup);
+
+	dev_dbg(dev, "driver successfully probed\n");
+
+	return 0;
+
+irq_err:
+	extcon_dev_unregister(&vup->edev);
+
+	return ret;
+}
+
+static int vuport_remove(struct platform_device *pdev)
+{
+	struct vuport *vup = platform_get_drvdata(pdev);
+
+	extcon_dev_unregister(&vup->edev);
+	return 0;
+}
+
+static struct acpi_device_id vuport_acpi_match[] = {
+	{ "INT3496" },
+	{ }
+};
+MODULE_DEVICE_TABLE(acpi, vuport_acpi_match);
+
+static struct platform_driver vuport_driver = {
+	.driver = {
+		.name = DRV_NAME,
+		.acpi_match_table = ACPI_PTR(vuport_acpi_match),
+	},
+	.probe = vuport_probe,
+	.remove = vuport_remove,
+};
+
+module_platform_driver(vuport_driver);
+
+MODULE_LICENSE("GPLv2");
+MODULE_AUTHOR("David Cohen <[email protected]>");
#27 lore raw
Quoted 22 lines · Robert Baldyga

Hi David,

Quoted 11 lines · David Cohen

Some Intel platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to a pulled up GPIO.

Optionally:
(2) VBUS is enabled/disabled by a GPIO
(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys. according to this GPIO level.

Quoted 7 lines

This driver configures USB OTG port for device or host role according to USB ID value.
- If USB ID's GPIO level is low, OTG port is configured for host role by sourcing VBUS and switching D+/- to host phy.
- If USB ID's GPIO level is high, by standard, the OTG port is configured for device role by not sourcing VBUS and switching D+/- to device controller.

Quoted 2 lines

IMO it's not very elegant to handle VBUS power on/off in extcon driver. Creating fixed regulator would allow to make VBUS handling more generic.

IMHO it's just layers of abstraction piled on top of each other here.

I would put this adjacent to the phy driver somewhere in drivers/usb/* and make the actual USB-driver thing handle its GPIOs directly. But I guess David and Felipe have already discussed that as we're seeing this patch?

Yours,
Linus Walleij

#28 lore raw

Hi Linus and Robert,

CC'ing Heikki as it involves a RFC from him.

Quoted 23 lines · Linus Walleij
Quoted 22 lines · Robert Baldyga

Hi David,

Quoted 18 lines · David Cohen

Some Intel platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to a pulled up GPIO.

Optionally:
(2) VBUS is enabled/disabled by a GPIO
(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys. according to this GPIO level.

This driver configures USB OTG port for device or host role according to USB ID value.
- If USB ID's GPIO level is low, OTG port is configured for host role by sourcing VBUS and switching D+/- to host phy.
- If USB ID's GPIO level is high, by standard, the OTG port is configured for device role by not sourcing VBUS and switching D+/- to device controller.

Quoted 2 lines
Quoted 2 lines

IMO it's not very elegant to handle VBUS power on/off in extcon driver. Creating fixed regulator would allow to make VBUS handling more generic.

I agree. But please, see below.

Quoted 7 lines

IMHO it's just layers of abstraction piled on top of each other here.

I would put this adjacent to the phy driver somewhere in drivers/usb/* and make the actual USB-driver thing handle its GPIOs directly. But I guess David and Felipe have already discussed that as we're seeing this patch?

Felipe suggested to "divide to conquer" instead of having a single extcon driver to handle all these functions:

- The mux functions would be controlled by a possible new pinctrl-gpio driver (Linus, your input here would be nice :)
- The VBUS would be a fixed regulator
- The USB ID would make usage of existent extcon-gpio

But the on fw side, this is a single ACPI device representing a virtual device for USB OTG port, which is nothing but a bunch of independent GPIOs.

I could make a mfd driver to register devices for those simpler and more generic drivers, but according to [1] community recognized it as a hack with ACPI since I'd need to give them the GPIO without requesting on mfd.

I'm open for suggestions :)

Br, David

[1] https://lkml.org/lkml/2014/12/18/82

Quoted 3 lines

Yours,
Linus Walleij

#29 lore raw
Quoted 53 lines · David Cohen

Hi Linus and Robert,

CC'ing Heikki as it involves a RFC from him.

Quoted 23 lines · Linus Walleij
Quoted 22 lines · Robert Baldyga

Hi David,

Quoted 18 lines · David Cohen

Some Intel platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to a pulled up GPIO.

Optionally:
(2) VBUS is enabled/disabled by a GPIO
(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys. according to this GPIO level.

This driver configures USB OTG port for device or host role according to USB ID value.
- If USB ID's GPIO level is low, OTG port is configured for host role by sourcing VBUS and switching D+/- to host phy.
- If USB ID's GPIO level is high, by standard, the OTG port is configured for device role by not sourcing VBUS and switching D+/- to device controller.

Quoted 2 lines
Quoted 2 lines

IMO it's not very elegant to handle VBUS power on/off in extcon driver. Creating fixed regulator would allow to make VBUS handling more generic.

I agree. But please, see below.

Quoted 7 lines

IMHO it's just layers of abstraction piled on top of each other here.

I would put this adjacent to the phy driver somewhere in drivers/usb/* and make the actual USB-driver thing handle its GPIOs directly. But I guess David and Felipe have already discussed that as we're seeing this patch?

Felipe suggested to "divide to conquer" instead of having a single extcon driver to handle all these functions:

- The mux functions would be controlled by a possible new pinctrl-gpio driver (Linus, your input here would be nice :)
- The VBUS would be a fixed regulator
- The USB ID would make usage of existent extcon-gpio

But the on fw side, this is a single ACPI device representing a virtual device for USB OTG port, which is nothing but a bunch of independent GPIOs.

Quoted 6 lines

I could make a mfd driver to register devices for those simpler and more generic drivers, but according to [1] community recognized it as a hack with ACPI since I'd need to give them the GPIO without requesting on mfd.

I'm open for suggestions :)

use MFD to create children devices and pass the required data to each one ?

Signature · 1 line
balbi
#30 lore raw
Quoted 59 lines · Felipe Balbi
Quoted 58 lines · David Cohen

Hi Linus and Robert,

CC'ing Heikki as it involves a RFC from him.

Quoted 23 lines · Linus Walleij
Quoted 22 lines · Robert Baldyga

Hi David,

Quoted 18 lines · David Cohen

Some Intel platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to a pulled up GPIO.

Optionally:
(2) VBUS is enabled/disabled by a GPIO
(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys. according to this GPIO level.

This driver configures USB OTG port for device or host role according to USB ID value.
- If USB ID's GPIO level is low, OTG port is configured for host role by sourcing VBUS and switching D+/- to host phy.
- If USB ID's GPIO level is high, by standard, the OTG port is configured for device role by not sourcing VBUS and switching D+/- to device controller.

Quoted 2 lines
Quoted 2 lines

IMO it's not very elegant to handle VBUS power on/off in extcon driver. Creating fixed regulator would allow to make VBUS handling more generic.

I agree. But please, see below.

Quoted 7 lines

IMHO it's just layers of abstraction piled on top of each other here.

I would put this adjacent to the phy driver somewhere in drivers/usb/* and make the actual USB-driver thing handle its GPIOs directly. But I guess David and Felipe have already discussed that as we're seeing this patch?

Felipe suggested to "divide to conquer" instead of having a single extcon driver to handle all these functions:

- The mux functions would be controlled by a possible new pinctrl-gpio driver (Linus, your input here would be nice :)
- The VBUS would be a fixed regulator
- The USB ID would make usage of existent extcon-gpio

But the on fw side, this is a single ACPI device representing a virtual device for USB OTG port, which is nothing but a bunch of independent GPIOs.

I could make a mfd driver to register devices for those simpler and more generic drivers, but according to [1] community recognized it as a hack with ACPI since I'd need to give them the GPIO without requesting on mfd.

Quoted 4 lines
Quoted 1 line

I'm open for suggestions :)

use MFD to create children devices and pass the required data to each one ?

I'd need to lookup GPIOs via ACPI without requesting them on mfd driver and then give them to children devices.
Heikki proposed a way to do that on [1], but it got nack'ed by community.

Br, David

Quoted 3 lines
Signature · 2 lines
-- 
balbi
#31 lore raw
Quoted 60 lines · David Cohen
Quoted 59 lines · Felipe Balbi
Quoted 58 lines · David Cohen

Hi Linus and Robert,

CC'ing Heikki as it involves a RFC from him.

Quoted 23 lines · Linus Walleij
Quoted 22 lines · Robert Baldyga

Hi David,

Quoted 18 lines · David Cohen

Some Intel platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to a pulled up GPIO.

Optionally:
(2) VBUS is enabled/disabled by a GPIO
(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys. according to this GPIO level.

This driver configures USB OTG port for device or host role according to USB ID value.
- If USB ID's GPIO level is low, OTG port is configured for host role by sourcing VBUS and switching D+/- to host phy.
- If USB ID's GPIO level is high, by standard, the OTG port is configured for device role by not sourcing VBUS and switching D+/- to device controller.

Quoted 2 lines
Quoted 2 lines

IMO it's not very elegant to handle VBUS power on/off in extcon driver. Creating fixed regulator would allow to make VBUS handling more generic.

I agree. But please, see below.

Quoted 7 lines

IMHO it's just layers of abstraction piled on top of each other here.

I would put this adjacent to the phy driver somewhere in drivers/usb/* and make the actual USB-driver thing handle its GPIOs directly. But I guess David and Felipe have already discussed that as we're seeing this patch?

Felipe suggested to "divide to conquer" instead of having a single extcon driver to handle all these functions:

- The mux functions would be controlled by a possible new pinctrl-gpio driver (Linus, your input here would be nice :)
- The VBUS would be a fixed regulator
- The USB ID would make usage of existent extcon-gpio

But the on fw side, this is a single ACPI device representing a virtual device for USB OTG port, which is nothing but a bunch of independent GPIOs.

I could make a mfd driver to register devices for those simpler and more generic drivers, but according to [1] community recognized it as a hack with ACPI since I'd need to give them the GPIO without requesting on mfd.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

I'm open for suggestions :)

use MFD to create children devices and pass the required data to each one ?

I'd need to lookup GPIOs via ACPI without requesting them on mfd driver and then give them to children devices.
Heikki proposed a way to do that on [1], but it got nack'ed by community.

you missed [1] :-)

Signature · 1 line
balbi
#32 lore raw
Quoted 63 lines · Felipe Balbi
Quoted 62 lines · David Cohen
Quoted 61 lines · Felipe Balbi
Quoted 59 lines · David Cohen

Hi Linus and Robert,

CC'ing Heikki as it involves a RFC from him.

Quoted 23 lines · Linus Walleij
Quoted 22 lines · Robert Baldyga

Hi David,

Quoted 18 lines · David Cohen

Some Intel platforms have an USB OTG port fully (or partially) controlled by GPIOs:

(1) USB ID is connected directly to a pulled up GPIO.

Optionally:
(2) VBUS is enabled/disabled by a GPIO
(3) Platform has 2 USB controllers connected to same port: one for device and one for host role. D+/- are switched between phys. according to this GPIO level.

This driver configures USB OTG port for device or host role according to USB ID value.
- If USB ID's GPIO level is low, OTG port is configured for host role by sourcing VBUS and switching D+/- to host phy.
- If USB ID's GPIO level is high, by standard, the OTG port is configured for device role by not sourcing VBUS and switching D+/- to device controller.

Quoted 2 lines
Quoted 2 lines

IMO it's not very elegant to handle VBUS power on/off in extcon driver. Creating fixed regulator would allow to make VBUS handling more generic.

I agree. But please, see below.

Quoted 7 lines

IMHO it's just layers of abstraction piled on top of each other here.

I would put this adjacent to the phy driver somewhere in drivers/usb/* and make the actual USB-driver thing handle its GPIOs directly. But I guess David and Felipe have already discussed that as we're seeing this patch?

Felipe suggested to "divide to conquer" instead of having a single extcon driver to handle all these functions:

- The mux functions would be controlled by a possible new pinctrl-gpio driver (Linus, your input here would be nice :)
- The VBUS would be a fixed regulator
- The USB ID would make usage of existent extcon-gpio

But the on fw side, this is a single ACPI device representing a virtual device for USB OTG port, which is nothing but a bunch of independent GPIOs.

I could make a mfd driver to register devices for those simpler and more generic drivers, but according to [1] community recognized it as a hack with ACPI since I'd need to give them the GPIO without requesting on mfd.

I'm open for suggestions :)

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

use MFD to create children devices and pass the required data to each one ?

I'd need to lookup GPIOs via ACPI without requesting them on mfd driver and then give them to children devices.
Heikki proposed a way to do that on [1], but it got nack'ed by community.

you missed [1] :-)

Oops. Looks like it went away during your past reply :)

[1] https://lkml.org/lkml/2014/12/18/82

Br, David

Quoted 3 lines
Signature · 2 lines
-- 
balbi
#33 lore raw
Quoted 7 lines · David Cohen
Quoted 6 lines · Felipe Balbi
Quoted 3 lines · David Cohen

I'd need to lookup GPIOs via ACPI without requesting them on mfd driver and then give them to children devices.
Heikki proposed a way to do that on [1], but it got nack'ed by community.

Quoted 5 lines
Quoted 1 line

you missed [1] :-)

Oops. Looks like it went away during your past reply :)

[1] https://lkml.org/lkml/2014/12/18/82

We don't want to do that in generic code since it's an ACPI problem, not a generic GPIO problem.

So the solution must be confined to the GPIO ACPI portions of the GPIO code, not altering central mechanisms.

Yours,
Linus Walleij

#34 lore raw
In reply to David Cohen

Hi,

[snip]

Quoted 8 lines

Felipe suggested to "divide to conquer" instead of having a single extcon driver to handle all these functions:

- The mux functions would be controlled by a possible new pinctrl-gpio driver (Linus, your input here would be nice :)
- The VBUS would be a fixed regulator
- The USB ID would make usage of existent extcon-gpio

Quoted 8 lines

But the on fw side, this is a single ACPI device representing a virtual device for USB OTG port, which is nothing but a bunch of independent GPIOs.

I could make a mfd driver to register devices for those simpler and more generic drivers, but according to [1] community recognized it as a hack with ACPI since I'd need to give them the GPIO without requesting on mfd.

I believe this case could be resumed in:

Would be [1] acceptable for mfd drivers?
- If yes, I can split this driver into more generic ones
- If no, I see no other option but having this driver fully controlling the USB OTG port.

BR, David

[1] https://lkml.org/lkml/2014/12/18/82

#35 lore raw
In reply to David Cohen
Quoted 9 lines · David Cohen
Quoted 4 lines · Linus Walleij

I would put this adjacent to the phy driver somewhere in drivers/usb/* and make the actual USB-driver thing handle its GPIOs directly. But I guess David and Felipe have already discussed that as we're seeing this patch?

- The mux functions would be controlled by a possible new pinctrl-gpio driver (Linus, your input here would be nice :)

I don't understand what this means, does it mean a pin control function somewhere else controlled by a GPIO pin?

Or do you mean a new combined pin control and GPIO driver (we have plenty of these).

If you elaborate on what you need to do in that driver I might understand it better.

Yours,
Linus Walleij

#36 lore raw
Quoted 12 lines · Linus Walleij
Quoted 9 lines · David Cohen
Quoted 4 lines · Linus Walleij

I would put this adjacent to the phy driver somewhere in drivers/usb/* and make the actual USB-driver thing handle its GPIOs directly. But I guess David and Felipe have already discussed that as we're seeing this patch?

- The mux functions would be controlled by a possible new pinctrl-gpio driver (Linus, your input here would be nice :)

Quoted 8 lines

I don't understand what this means, does it mean a pin control function somewhere else controlled by a GPIO pin?

Or do you mean a new combined pin control and GPIO driver (we have plenty of these).

If you elaborate on what you need to do in that driver I might understand it better.

there's a discrete mux (not something integrated in the SoC) whose select signal is tied to a GPIO (in some cases, more than one, but usually people use 2-state muxes).

Signature · 1 line
balbi
#37 lore raw

Hi Linus,

Quoted 13 lines · Felipe Balbi
Quoted 12 lines · Linus Walleij
Quoted 9 lines · David Cohen
Quoted 4 lines · Linus Walleij

I would put this adjacent to the phy driver somewhere in drivers/usb/* and make the actual USB-driver thing handle its GPIOs directly. But I guess David and Felipe have already discussed that as we're seeing this patch?

- The mux functions would be controlled by a possible new pinctrl-gpio driver (Linus, your input here would be nice :)

Quoted 8 lines
Quoted 8 lines

I don't understand what this means, does it mean a pin control function somewhere else controlled by a GPIO pin?

Or do you mean a new combined pin control and GPIO driver (we have plenty of these).

If you elaborate on what you need to do in that driver I might understand it better.

This is a "virtual" ACPI device. Long story short we've 3 GPIOs controlling the state of an USB OTG port. Neither of the hw components controlled by these GPIOs are connected to a bus.
The ACPI table was implemented in such way that it considers this USB port as a single "device" giving all 3 GPIOs to it.

When upstreaming this driver, the feedback I got is to split this driver into simpler and more generic ones. FWIW ACPI tables are constructed considering a valid Linux API during its implementation and are quite difficult to change after product is released. The solution would be to use MFD subsystem: this driver would create simpler children platform devices.

But at least on ACPI case, GPIO API blocks the ability to create children platform devices that require GPIO as platform data, despite we've many drivers on upstream that expect this behavior: e.g. extcon-gpio.c

Are we considering those driver deprecated from the GPIO point of view? If yes, we need to provide an update for them (we can't let upstreamed drivers broken). If no, we need to provide a way to move forward the GPIO to children devices.

BR, David

Quoted 7 lines

there's a discrete mux (not something integrated in the SoC) whose select signal is tied to a GPIO (in some cases, more than one, but usually people use 2-state muxes).

Signature · 1 line
balbi
#38 lore raw

Adding Mika to CC list.

Br, David

Quoted 58 lines · David Cohen

Hi Linus,

Quoted 13 lines · Felipe Balbi
Quoted 12 lines · Linus Walleij
Quoted 9 lines · David Cohen
Quoted 4 lines · Linus Walleij

I would put this adjacent to the phy driver somewhere in drivers/usb/* and make the actual USB-driver thing handle its GPIOs directly. But I guess David and Felipe have already discussed that as we're seeing this patch?

- The mux functions would be controlled by a possible new pinctrl-gpio driver (Linus, your input here would be nice :)

Quoted 8 lines
Quoted 8 lines

I don't understand what this means, does it mean a pin control function somewhere else controlled by a GPIO pin?

Or do you mean a new combined pin control and GPIO driver (we have plenty of these).

If you elaborate on what you need to do in that driver I might understand it better.

This is a "virtual" ACPI device. Long story short we've 3 GPIOs controlling the state of an USB OTG port. Neither of the hw components controlled by these GPIOs are connected to a bus.
The ACPI table was implemented in such way that it considers this USB port as a single "device" giving all 3 GPIOs to it.

When upstreaming this driver, the feedback I got is to split this driver into simpler and more generic ones. FWIW ACPI tables are constructed considering a valid Linux API during its implementation and are quite difficult to change after product is released. The solution would be to use MFD subsystem: this driver would create simpler children platform devices.

But at least on ACPI case, GPIO API blocks the ability to create children platform devices that require GPIO as platform data, despite we've many drivers on upstream that expect this behavior: e.g. extcon-gpio.c

Are we considering those driver deprecated from the GPIO point of view? If yes, we need to provide an update for them (we can't let upstreamed drivers broken). If no, we need to provide a way to move forward the GPIO to children devices.

BR, David

Quoted 7 lines

there's a discrete mux (not something integrated in the SoC) whose select signal is tied to a GPIO (in some cases, more than one, but usually people use 2-state muxes).

Signature · 1 line
balbi
#39 lore raw
Quoted 1 line · David Cohen

Adding Mika to CC list.

Grrr :(
Adding for real now.

Quoted 62 lines

Br, David

Quoted 58 lines · David Cohen

Hi Linus,

Quoted 13 lines · Felipe Balbi
Quoted 12 lines · Linus Walleij
Quoted 9 lines · David Cohen
Quoted 4 lines · Linus Walleij

I would put this adjacent to the phy driver somewhere in drivers/usb/* and make the actual USB-driver thing handle its GPIOs directly. But I guess David and Felipe have already discussed that as we're seeing this patch?

- The mux functions would be controlled by a possible new pinctrl-gpio driver (Linus, your input here would be nice :)

Quoted 8 lines
Quoted 8 lines

I don't understand what this means, does it mean a pin control function somewhere else controlled by a GPIO pin?

Or do you mean a new combined pin control and GPIO driver (we have plenty of these).

If you elaborate on what you need to do in that driver I might understand it better.

This is a "virtual" ACPI device. Long story short we've 3 GPIOs controlling the state of an USB OTG port. Neither of the hw components controlled by these GPIOs are connected to a bus.
The ACPI table was implemented in such way that it considers this USB port as a single "device" giving all 3 GPIOs to it.

When upstreaming this driver, the feedback I got is to split this driver into simpler and more generic ones. FWIW ACPI tables are constructed considering a valid Linux API during its implementation and are quite difficult to change after product is released. The solution would be to use MFD subsystem: this driver would create simpler children platform devices.

But at least on ACPI case, GPIO API blocks the ability to create children platform devices that require GPIO as platform data, despite we've many drivers on upstream that expect this behavior: e.g. extcon-gpio.c

Are we considering those driver deprecated from the GPIO point of view? If yes, we need to provide an update for them (we can't let upstreamed drivers broken). If no, we need to provide a way to move forward the GPIO to children devices.

BR, David

Quoted 7 lines

there's a discrete mux (not something integrated in the SoC) whose select signal is tied to a GPIO (in some cases, more than one, but usually people use 2-state muxes).

Signature · 1 line
balbi
#40 lore raw
In reply to David Cohen
Quoted 8 lines · David Cohen

But at least on ACPI case, GPIO API blocks the ability to create children platform devices that require GPIO as platform data, despite we've many drivers on upstream that expect this behavior: e.g. extcon-gpio.c

Are we considering those driver deprecated from the GPIO point of view? If yes, we need to provide an update for them (we can't let upstreamed drivers broken). If no, we need to provide a way to move forward the GPIO to children devices.

Aha OK... Let me paste this code from arch/arm/mach-integrator/impd1.c - this is not an MFD (it would be if we implemented it today) but you can see the idea I deploy:

    for (i = 0; i < ARRAY_SIZE(impd1_devs); i++) {
        struct impd1_device *idev = impd1_devs + i;
        struct amba_device *d;
        unsigned long pc_base;
        char devname[32];
        int irq1 = idev->irq[0];
        int irq2 = idev->irq[1];

        /* Translate IRQs to IM-PD1 local numberspace */
        if (irq1)
            irq1 += irq_base;
        if (irq2)
            irq2 += irq_base;

        pc_base = dev->resource.start + idev->offset;
        snprintf(devname, 32, "lm%x:%5.5lx", dev->id, idev->offset >> 12);

        /* Add GPIO descriptor lookup table for the PL061 block */
        if (idev->offset == 0x00400000) {
            struct gpiod_lookup_table *lookup;
            char *chipname;
            char *mmciname;

            lookup = devm_kzalloc(&dev->dev,
                          sizeof(*lookup) + 3 * sizeof(struct gpiod_lookup),
                          GFP_KERNEL);
            chipname = devm_kstrdup(&dev->dev, devname, GFP_KERNEL);
            mmciname = kasprintf(GFP_KERNEL, "lm%x:00700", dev->id);
            lookup->dev_id = mmciname;
            /*
             * Offsets on GPIO block 1:
             * 3 = MMC WP (write protect)
             * 4 = MMC CD (card detect)
             *
             * Offsets on GPIO block 2:
             * 0 = Up key
             * 1 = Down key
             * 2 = Left key
             * 3 = Right key
             * 4 = Key lower left
             * 5 = Key lower right
             */
            /* We need the two MMCI GPIO entries */
            lookup->table[0].chip_label = chipname;
            lookup->table[0].chip_hwnum = 3;
            lookup->table[0].con_id = "wp";
            lookup->table[1].chip_label = chipname;
            lookup->table[1].chip_hwnum = 4;
            lookup->table[1].con_id = "cd";
            lookup->table[1].flags = GPIO_ACTIVE_LOW;
            gpiod_add_lookup_table(lookup);
        }

        d = amba_ahb_device_add_res(&dev->dev, devname, pc_base, SZ_4K,
                        irq1, irq2,
                        idev->platform_data, idev->id,
                        &dev->resource);
        if (IS_ERR(d)) {
            dev_err(&dev->dev, "unable to register device: %ld\n", PTR_ERR(d));
            continue;
        }
    }

Notice how I build a dynamic lookup table.

I don't see any problem for an MFD nexus doing this too, just need some quirks I guess.

But I guess it's not that simple, i.e. the GPIOs are not just "appearing" in the system like here (the devices we add has a GPIO block going with it) but it's external GPIOs.

So I presume we are talking about the case of GPIO forwarding that was mentioned earlier, so that GPIOs are defined for the nexus (MFD) device and need to be forwarded to the clients so they can issue gpiod_get() on their own struct device*?

Yours,
Linus Walleij