All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/4] usb: gadget: udc: renesas_usb3: add USB3.0 DRD support
@ 2017-03-28 11:53 Yoshihiro Shimoda
  2017-03-28 11:53 ` [PATCH 1/4] usb: gadget: udc: renesas_usb3: add sysfs "role" to set "b-device" mode Yoshihiro Shimoda
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: Yoshihiro Shimoda @ 2017-03-28 11:53 UTC (permalink / raw)
  To: balbi; +Cc: gregkh, linux-usb, linux-renesas-soc, Yoshihiro Shimoda

This patch set is based on the latest Felipe's usb.bit / testing/next branch
(commit id = 29986993f67341493988b6c5d68e0653061975b2).

The USB3.0 DRD controller on R-Car Gen3 can change the role via DRD_CON
register in the periperal side sadly, so this patch adds support
for usb role swap feature in this udc driver.
Furtunately, the default setting of the DRD is host side, if a user want
to use host side only, we can handle it easily just to disable the udc driver.

Yoshihiro Shimoda (4):
  usb: gadget: udc: renesas_usb3: add sysfs "role" to set "b-device"
    mode
  usb: gadget: udc: add USB ID signal monitoring
  usb: gadget: udc: renesas_usb3: add extcon support
  usb: gadget: udc: renesas_usb3: add support for usb role swap

 .../ABI/testing/sysfs-platform-renesas_usb3        |  17 ++
 drivers/usb/gadget/udc/Kconfig                     |   1 +
 drivers/usb/gadget/udc/renesas_usb3.c              | 201 ++++++++++++++++++++-
 3 files changed, 214 insertions(+), 5 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-platform-renesas_usb3

-- 
1.9.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 1/4] usb: gadget: udc: renesas_usb3: add sysfs "role" to set "b-device" mode
  2017-03-28 11:53 [PATCH 0/4] usb: gadget: udc: renesas_usb3: add USB3.0 DRD support Yoshihiro Shimoda
@ 2017-03-28 11:53 ` Yoshihiro Shimoda
  2017-03-28 12:06   ` Felipe Balbi
  2017-03-28 11:53 ` [PATCH 2/4] usb: gadget: udc: add USB ID signal monitoring Yoshihiro Shimoda
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 11+ messages in thread
From: Yoshihiro Shimoda @ 2017-03-28 11:53 UTC (permalink / raw)
  To: balbi; +Cc: gregkh, linux-usb, linux-renesas-soc, Yoshihiro Shimoda

Sadly, to change the role ("host" and "peripheral") of USB3.0 DRD
controller on R-Car Gen3, software has to set the DRD_CON register
which is included in a usb3.0 peripheral controler's register.
To simply implementation, the previous code always set peripheral mode
as hardcoded. However, to support usb role swap in the future,
the hardcoded is not good. So, this patch adds sysfs "role" to set
the mode by a user.
After applied this patch, since the DRD controller will act as host
mode after probed, a user needs to change the mode via the sysfs.

Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
---
 .../ABI/testing/sysfs-platform-renesas_usb3        | 15 +++++
 drivers/usb/gadget/udc/renesas_usb3.c              | 74 ++++++++++++++++++++--
 2 files changed, 85 insertions(+), 4 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-platform-renesas_usb3

diff --git a/Documentation/ABI/testing/sysfs-platform-renesas_usb3 b/Documentation/ABI/testing/sysfs-platform-renesas_usb3
new file mode 100644
index 0000000..f5dace7
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-platform-renesas_usb3
@@ -0,0 +1,15 @@
+What:		/sys/devices/platform/<udc-name>/role
+Date:		March 2017
+KernelVersion:	4.13
+Contact:	Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
+Description:
+		This file can be read and write.
+		The file can show/change the drd mode of usb.
+
+		Write the following string to change the mode:
+		 "b-device" - switching mode as forced b-device mode.
+
+		Read the file, then it shows the following strings:
+		 "host" - The mode is host now.
+		 "peripheral" - The mode is peripheral now.
+		 "b-device" - The mode is forced b-device (b-peripheral) now.
diff --git a/drivers/usb/gadget/udc/renesas_usb3.c b/drivers/usb/gadget/udc/renesas_usb3.c
index 2218f91..4029593 100644
--- a/drivers/usb/gadget/udc/renesas_usb3.c
+++ b/drivers/usb/gadget/udc/renesas_usb3.c
@@ -269,6 +269,7 @@ struct renesas_usb3 {
 	u8 ep0_buf[USB3_EP0_BUF_SIZE];
 	bool softconnect;
 	bool workaround_for_vbus;
+	bool forced_b_device;
 };
 
 #define gadget_to_renesas_usb3(_gadget)	\
@@ -352,6 +353,11 @@ static void usb3_disable_pipe_irq(struct renesas_usb3 *usb3, int num)
 	usb3_clear_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2);
 }
 
+static bool usb3_is_host(struct renesas_usb3 *usb3)
+{
+	return !(usb3_read(usb3, USB3_DRD_CON) & DRD_CON_PERI_CON);
+}
+
 static void usb3_init_axi_bridge(struct renesas_usb3 *usb3)
 {
 	/* Set AXI_INT */
@@ -362,10 +368,6 @@ static void usb3_init_axi_bridge(struct renesas_usb3 *usb3)
 
 static void usb3_init_epc_registers(struct renesas_usb3 *usb3)
 {
-	/* FIXME: How to change host / peripheral mode as well? */
-	usb3_set_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
-	usb3_clear_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON);
-
 	usb3_write(usb3, ~0, USB3_USB_INT_STA_1);
 	usb3_enable_irq_1(usb3, USB_INT_1_VBUS_CNG);
 }
@@ -538,6 +540,12 @@ static void usb3_check_vbus(struct renesas_usb3 *usb3)
 	}
 }
 
+static void usb3_mode_b_peri(struct renesas_usb3 *usb3)
+{
+	usb3_set_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
+	usb3_clear_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON);
+}
+
 static void renesas_usb3_init_controller(struct renesas_usb3 *usb3)
 {
 	usb3_init_axi_bridge(usb3);
@@ -556,6 +564,16 @@ static void renesas_usb3_stop_controller(struct renesas_usb3 *usb3)
 	usb3_write(usb3, 0, USB3_AXI_INT_ENA);
 }
 
+static void renesas_usb3_init_forced_b_device(struct renesas_usb3 *usb3)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&usb3->lock, flags);
+	usb3_mode_b_peri(usb3);
+	renesas_usb3_init_controller(usb3);
+	spin_unlock_irqrestore(&usb3->lock, flags);
+}
+
 static void usb3_irq_epc_int_1_pll_wakeup(struct renesas_usb3 *usb3)
 {
 	usb3_disable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
@@ -1756,6 +1774,47 @@ static int renesas_usb3_set_selfpowered(struct usb_gadget *gadget, int is_self)
 	.set_selfpowered	= renesas_usb3_set_selfpowered,
 };
 
+static const char *usb3_get_role_string(struct renesas_usb3 *usb3)
+{
+	if (usb3->forced_b_device)
+		return "b-device";
+	else if (usb3_is_host(usb3))
+		return "host";
+	else
+		return "peripheral";
+}
+
+static ssize_t role_store(struct device *dev, struct device_attribute *attr,
+			  const char *buf, size_t count)
+{
+	struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
+
+	if (!usb3->driver)
+		return -ENODEV;
+
+	if (!strncmp(buf, "b-device", strlen("b-device"))) {
+		usb3->forced_b_device = true;
+		renesas_usb3_init_forced_b_device(usb3);
+	} else {
+		usb3->forced_b_device = false;
+		return -EINVAL;
+	}
+
+	return count;
+}
+
+static ssize_t role_show(struct device *dev, struct device_attribute *attr,
+			 char *buf)
+{
+	struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
+
+	if (!usb3->driver)
+		return -ENODEV;
+
+	return sprintf(buf, "%s\n", usb3_get_role_string(usb3));
+}
+static DEVICE_ATTR_RW(role);
+
 /*------- platform_driver ------------------------------------------------*/
 static int renesas_usb3_remove(struct platform_device *pdev)
 {
@@ -1946,6 +2005,10 @@ static int renesas_usb3_probe(struct platform_device *pdev)
 	if (ret < 0)
 		goto err_add_udc;
 
+	ret = device_create_file(&pdev->dev, &dev_attr_role);
+	if (ret < 0)
+		goto err_dev_create;
+
 	usb3->workaround_for_vbus = priv->workaround_for_vbus;
 
 	pm_runtime_enable(&pdev->dev);
@@ -1955,6 +2018,9 @@ static int renesas_usb3_probe(struct platform_device *pdev)
 
 	return 0;
 
+err_dev_create:
+	usb_del_gadget_udc(&usb3->gadget);
+
 err_add_udc:
 	__renesas_usb3_ep_free_request(usb3->ep0_req);
 
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 2/4] usb: gadget: udc: add USB ID signal monitoring
  2017-03-28 11:53 [PATCH 0/4] usb: gadget: udc: renesas_usb3: add USB3.0 DRD support Yoshihiro Shimoda
  2017-03-28 11:53 ` [PATCH 1/4] usb: gadget: udc: renesas_usb3: add sysfs "role" to set "b-device" mode Yoshihiro Shimoda
@ 2017-03-28 11:53 ` Yoshihiro Shimoda
  2017-03-28 11:53 ` [PATCH 3/4] usb: gadget: udc: renesas_usb3: add extcon support Yoshihiro Shimoda
  2017-03-28 11:53 ` [PATCH 4/4] usb: gadget: udc: renesas_usb3: add support for usb role swap Yoshihiro Shimoda
  3 siblings, 0 replies; 11+ messages in thread
From: Yoshihiro Shimoda @ 2017-03-28 11:53 UTC (permalink / raw)
  To: balbi; +Cc: gregkh, linux-usb, linux-renesas-soc, Yoshihiro Shimoda

This usb 3.0 peripheral controller has a register (USB_OTG_STA) to monitor
the USB ID signal. So, this patch adds the ID signal monitoring to change
the mode to host (A-Device) or peripheral (B-Device).

Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
---
 drivers/usb/gadget/udc/renesas_usb3.c | 47 +++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/drivers/usb/gadget/udc/renesas_usb3.c b/drivers/usb/gadget/udc/renesas_usb3.c
index 4029593..d81953b 100644
--- a/drivers/usb/gadget/udc/renesas_usb3.c
+++ b/drivers/usb/gadget/udc/renesas_usb3.c
@@ -37,6 +37,9 @@
 #define USB3_USB_INT_ENA_2	0x22c
 #define USB3_STUP_DAT_0		0x230
 #define USB3_STUP_DAT_1		0x234
+#define USB3_USB_OTG_STA	0x268
+#define USB3_USB_OTG_INT_STA	0x26c
+#define USB3_USB_OTG_INT_ENA	0x270
 #define USB3_P0_MOD		0x280
 #define USB3_P0_CON		0x288
 #define USB3_P0_STA		0x28c
@@ -124,6 +127,9 @@
 /* USB_INT_ENA_2 and USB_INT_STA_2 */
 #define USB_INT_2_PIPE(n)	BIT(n)
 
+/* USB_OTG_STA, USB_OTG_INT_STA and USB_OTG_INT_ENA */
+#define USB_OTG_IDMON		BIT(4)
+
 /* P0_MOD */
 #define P0_MOD_DIR		BIT(6)
 
@@ -540,17 +546,39 @@ static void usb3_check_vbus(struct renesas_usb3 *usb3)
 	}
 }
 
+static void usb3_mode_a_host(struct renesas_usb3 *usb3)
+{
+	usb3_clear_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
+	usb3_set_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON);
+}
+
 static void usb3_mode_b_peri(struct renesas_usb3 *usb3)
 {
 	usb3_set_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
 	usb3_clear_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON);
 }
 
+static bool usb3_is_a_device(struct renesas_usb3 *usb3)
+{
+	return !(usb3_read(usb3, USB3_USB_OTG_STA) & USB_OTG_IDMON);
+}
+
+static void usb3_check_id(struct renesas_usb3 *usb3)
+{
+	if (usb3_is_a_device(usb3) && !usb3->forced_b_device)
+		usb3_mode_a_host(usb3);
+	else
+		usb3_mode_b_peri(usb3);
+}
+
 static void renesas_usb3_init_controller(struct renesas_usb3 *usb3)
 {
 	usb3_init_axi_bridge(usb3);
 	usb3_init_epc_registers(usb3);
+	usb3_write(usb3, USB_OTG_IDMON, USB3_USB_OTG_INT_STA);
+	usb3_write(usb3, USB_OTG_IDMON, USB3_USB_OTG_INT_ENA);
 
+	usb3_check_id(usb3);
 	usb3_check_vbus(usb3);
 }
 
@@ -559,6 +587,7 @@ static void renesas_usb3_stop_controller(struct renesas_usb3 *usb3)
 	usb3_disconnect(usb3);
 	usb3_write(usb3, 0, USB3_P0_INT_ENA);
 	usb3_write(usb3, 0, USB3_PN_INT_ENA);
+	usb3_write(usb3, 0, USB3_USB_OTG_INT_ENA);
 	usb3_write(usb3, 0, USB3_USB_INT_ENA_1);
 	usb3_write(usb3, 0, USB3_USB_INT_ENA_2);
 	usb3_write(usb3, 0, USB3_AXI_INT_ENA);
@@ -1492,10 +1521,22 @@ static void usb3_irq_epc_int_2(struct renesas_usb3 *usb3, u32 int_sta_2)
 	}
 }
 
+static void usb3_irq_idmon_change(struct renesas_usb3 *usb3)
+{
+	usb3_check_id(usb3);
+}
+
+static void usb3_irq_otg_int(struct renesas_usb3 *usb3, u32 otg_int_sta)
+{
+	if (otg_int_sta & USB_OTG_IDMON)
+		usb3_irq_idmon_change(usb3);
+}
+
 static void usb3_irq_epc(struct renesas_usb3 *usb3)
 {
 	u32 int_sta_1 = usb3_read(usb3, USB3_USB_INT_STA_1);
 	u32 int_sta_2 = usb3_read(usb3, USB3_USB_INT_STA_2);
+	u32 otg_int_sta = usb3_read(usb3, USB3_USB_OTG_INT_STA);
 
 	int_sta_1 &= usb3_read(usb3, USB3_USB_INT_ENA_1);
 	if (int_sta_1) {
@@ -1506,6 +1547,12 @@ static void usb3_irq_epc(struct renesas_usb3 *usb3)
 	int_sta_2 &= usb3_read(usb3, USB3_USB_INT_ENA_2);
 	if (int_sta_2)
 		usb3_irq_epc_int_2(usb3, int_sta_2);
+
+	otg_int_sta &= usb3_read(usb3, USB3_USB_OTG_INT_ENA);
+	if (otg_int_sta) {
+		usb3_write(usb3, otg_int_sta, USB3_USB_OTG_INT_STA);
+		usb3_irq_otg_int(usb3, otg_int_sta);
+	}
 }
 
 static irqreturn_t renesas_usb3_irq(int irq, void *_usb3)
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 3/4] usb: gadget: udc: renesas_usb3: add extcon support
  2017-03-28 11:53 [PATCH 0/4] usb: gadget: udc: renesas_usb3: add USB3.0 DRD support Yoshihiro Shimoda
  2017-03-28 11:53 ` [PATCH 1/4] usb: gadget: udc: renesas_usb3: add sysfs "role" to set "b-device" mode Yoshihiro Shimoda
  2017-03-28 11:53 ` [PATCH 2/4] usb: gadget: udc: add USB ID signal monitoring Yoshihiro Shimoda
@ 2017-03-28 11:53 ` Yoshihiro Shimoda
  2017-03-28 11:53 ` [PATCH 4/4] usb: gadget: udc: renesas_usb3: add support for usb role swap Yoshihiro Shimoda
  3 siblings, 0 replies; 11+ messages in thread
From: Yoshihiro Shimoda @ 2017-03-28 11:53 UTC (permalink / raw)
  To: balbi; +Cc: gregkh, linux-usb, linux-renesas-soc, Yoshihiro Shimoda

This patch adds extcon support to see VBUS/ID signal states.

Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
---
 drivers/usb/gadget/udc/Kconfig        |  1 +
 drivers/usb/gadget/udc/renesas_usb3.c | 43 +++++++++++++++++++++++++++++++++--
 2 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/gadget/udc/Kconfig b/drivers/usb/gadget/udc/Kconfig
index 4b69f28..66cb00b 100644
--- a/drivers/usb/gadget/udc/Kconfig
+++ b/drivers/usb/gadget/udc/Kconfig
@@ -191,6 +191,7 @@ config USB_RENESAS_USBHS_UDC
 config USB_RENESAS_USB3
 	tristate 'Renesas USB3.0 Peripheral controller'
 	depends on ARCH_RENESAS || COMPILE_TEST
+	depends on EXTCON
 	help
 	   Renesas USB3.0 Peripheral controller is a USB peripheral controller
 	   that supports super, high, and full speed USB 3.0 data transfers.
diff --git a/drivers/usb/gadget/udc/renesas_usb3.c b/drivers/usb/gadget/udc/renesas_usb3.c
index d81953b..34ac03c 100644
--- a/drivers/usb/gadget/udc/renesas_usb3.c
+++ b/drivers/usb/gadget/udc/renesas_usb3.c
@@ -10,6 +10,7 @@
 
 #include <linux/delay.h>
 #include <linux/err.h>
+#include <linux/extcon.h>
 #include <linux/interrupt.h>
 #include <linux/io.h>
 #include <linux/module.h>
@@ -263,6 +264,8 @@ struct renesas_usb3 {
 
 	struct usb_gadget gadget;
 	struct usb_gadget_driver *driver;
+	struct extcon_dev *extcon;
+	struct work_struct extcon_work;
 
 	struct renesas_usb3_ep *usb3_ep;
 	int num_usb3_eps;
@@ -276,6 +279,8 @@ struct renesas_usb3 {
 	bool softconnect;
 	bool workaround_for_vbus;
 	bool forced_b_device;
+	bool extcon_host;	/* check id  and set EXTCON_USB_HOST */
+	bool extcon_usb;	/* check vbus and set EXTCON_USB */
 };
 
 #define gadget_to_renesas_usb3(_gadget)	\
@@ -339,6 +344,15 @@ static int usb3_wait(struct renesas_usb3 *usb3, u32 reg, u32 mask,
 	return -EBUSY;
 }
 
+static void renesas_usb3_extcon_work(struct work_struct *work)
+{
+	struct renesas_usb3 *usb3 = container_of(work, struct renesas_usb3,
+						 extcon_work);
+
+	extcon_set_state_sync(usb3->extcon, EXTCON_USB_HOST, usb3->extcon_host);
+	extcon_set_state_sync(usb3->extcon, EXTCON_USB, usb3->extcon_usb);
+}
+
 static void usb3_enable_irq_1(struct renesas_usb3 *usb3, u32 bits)
 {
 	usb3_set_bit(usb3, bits, USB3_USB_INT_ENA_1);
@@ -539,10 +553,14 @@ static void usb3_check_vbus(struct renesas_usb3 *usb3)
 	if (usb3->workaround_for_vbus) {
 		usb3_connect(usb3);
 	} else {
-		if (usb3_read(usb3, USB3_USB_STA) & USB_STA_VBUS_STA)
+		usb3->extcon_usb = !!(usb3_read(usb3, USB3_USB_STA) &
+							USB_STA_VBUS_STA);
+		if (usb3->extcon_usb)
 			usb3_connect(usb3);
 		else
 			usb3_disconnect(usb3);
+
+		schedule_work(&usb3->extcon_work);
 	}
 }
 
@@ -565,10 +583,14 @@ static bool usb3_is_a_device(struct renesas_usb3 *usb3)
 
 static void usb3_check_id(struct renesas_usb3 *usb3)
 {
-	if (usb3_is_a_device(usb3) && !usb3->forced_b_device)
+	usb3->extcon_host = usb3_is_a_device(usb3);
+
+	if (usb3->extcon_host && !usb3->forced_b_device)
 		usb3_mode_a_host(usb3);
 	else
 		usb3_mode_b_peri(usb3);
+
+	schedule_work(&usb3->extcon_work);
 }
 
 static void renesas_usb3_init_controller(struct renesas_usb3 *usb3)
@@ -2000,6 +2022,12 @@ static void renesas_usb3_init_ram(struct renesas_usb3 *usb3, struct device *dev,
 };
 MODULE_DEVICE_TABLE(of, usb3_of_match);
 
+static const unsigned int renesas_usb3_cable[] = {
+	EXTCON_USB,
+	EXTCON_USB_HOST,
+	EXTCON_NONE,
+};
+
 static int renesas_usb3_probe(struct platform_device *pdev)
 {
 	struct renesas_usb3 *usb3;
@@ -2043,6 +2071,17 @@ static int renesas_usb3_probe(struct platform_device *pdev)
 	if (ret < 0)
 		return ret;
 
+	INIT_WORK(&usb3->extcon_work, renesas_usb3_extcon_work);
+	usb3->extcon = devm_extcon_dev_allocate(&pdev->dev, renesas_usb3_cable);
+	if (IS_ERR(usb3->extcon))
+		return PTR_ERR(usb3->extcon);
+
+	ret = devm_extcon_dev_register(&pdev->dev, usb3->extcon);
+	if (ret < 0) {
+		dev_err(&pdev->dev, "Failed to register extcon\n");
+		return ret;
+	}
+
 	/* for ep0 handling */
 	usb3->ep0_req = __renesas_usb3_ep_alloc_request(GFP_KERNEL);
 	if (!usb3->ep0_req)
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 4/4] usb: gadget: udc: renesas_usb3: add support for usb role swap
  2017-03-28 11:53 [PATCH 0/4] usb: gadget: udc: renesas_usb3: add USB3.0 DRD support Yoshihiro Shimoda
                   ` (2 preceding siblings ...)
  2017-03-28 11:53 ` [PATCH 3/4] usb: gadget: udc: renesas_usb3: add extcon support Yoshihiro Shimoda
@ 2017-03-28 11:53 ` Yoshihiro Shimoda
  3 siblings, 0 replies; 11+ messages in thread
From: Yoshihiro Shimoda @ 2017-03-28 11:53 UTC (permalink / raw)
  To: balbi; +Cc: gregkh, linux-usb, linux-renesas-soc, Yoshihiro Shimoda

This patch adds support for usb role swap via sysfs "role".

For example:
 1) Connect a usb cable using 2 Salvator-X boards.
  - For A-Device, the cable is connected to CN11 (USB3.0 ch0).
  - For B-Device, the cable is connected to CN9 (USB2.0 ch0).
 2) On A-Device, you input the following command:
  # echo peripheral > /sys/devices/platform/soc/ee020000.usb/role
 3) On B-Device, you input the following command:
  # echo host > /sys/devices/platform/soc/ee080200.usb-phy/role

Then, the A-Device acts as a peripheral and the B-Device acts as
a host. Please note that A-Device must input the following command
if you want the board to act as a host again.
 # echo host > /sys/devices/platform/soc/ee020000.usb/role

Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
---
 .../ABI/testing/sysfs-platform-renesas_usb3        |  2 ++
 drivers/usb/gadget/udc/renesas_usb3.c              | 41 +++++++++++++++++++++-
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/Documentation/ABI/testing/sysfs-platform-renesas_usb3 b/Documentation/ABI/testing/sysfs-platform-renesas_usb3
index f5dace7..1ab919c 100644
--- a/Documentation/ABI/testing/sysfs-platform-renesas_usb3
+++ b/Documentation/ABI/testing/sysfs-platform-renesas_usb3
@@ -7,6 +7,8 @@ Description:
 		The file can show/change the drd mode of usb.
 
 		Write the following string to change the mode:
+		 "host" - switching mode from peripheral to host.
+		 "peripheral" - switching mode from host to peripheral.
 		 "b-device" - switching mode as forced b-device mode.
 
 		Read the file, then it shows the following strings:
diff --git a/drivers/usb/gadget/udc/renesas_usb3.c b/drivers/usb/gadget/udc/renesas_usb3.c
index 34ac03c..b552243 100644
--- a/drivers/usb/gadget/udc/renesas_usb3.c
+++ b/drivers/usb/gadget/udc/renesas_usb3.c
@@ -570,12 +570,29 @@ static void usb3_mode_a_host(struct renesas_usb3 *usb3)
 	usb3_set_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON);
 }
 
+static void usb3_mode_a_peri(struct renesas_usb3 *usb3)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&usb3->lock, flags);
+	usb3_set_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
+	usb3_set_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON);
+	usb3_check_vbus(usb3);
+	spin_unlock_irqrestore(&usb3->lock, flags);
+}
+
 static void usb3_mode_b_peri(struct renesas_usb3 *usb3)
 {
 	usb3_set_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
 	usb3_clear_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON);
 }
 
+static void usb3_mode_b_host(struct renesas_usb3 *usb3)
+{
+	usb3_clear_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
+	usb3_clear_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON);
+}
+
 static bool usb3_is_a_device(struct renesas_usb3 *usb3)
 {
 	return !(usb3_read(usb3, USB3_USB_OTG_STA) & USB_OTG_IDMON);
@@ -1865,8 +1882,30 @@ static ssize_t role_store(struct device *dev, struct device_attribute *attr,
 		usb3->forced_b_device = true;
 		renesas_usb3_init_forced_b_device(usb3);
 	} else {
+		bool new_mode_is_host;
+
 		usb3->forced_b_device = false;
-		return -EINVAL;
+		if (!strncmp(buf, "host", strlen("host")))
+			new_mode_is_host = true;
+		else if (!strncmp(buf, "peripheral", strlen("peripheral")))
+			new_mode_is_host = false;
+		else
+			return -EINVAL;
+
+		if (new_mode_is_host == usb3_is_host(usb3))
+			return -EINVAL;
+
+		if (new_mode_is_host) {
+			if (usb3_is_a_device(usb3))
+				usb3_mode_a_host(usb3);
+			else
+				usb3_mode_b_host(usb3);
+		} else {
+			if (usb3_is_a_device(usb3))
+				usb3_mode_a_peri(usb3);
+			else
+				usb3_mode_b_peri(usb3);
+		}
 	}
 
 	return count;
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/4] usb: gadget: udc: renesas_usb3: add sysfs "role" to set "b-device" mode
  2017-03-28 11:53 ` [PATCH 1/4] usb: gadget: udc: renesas_usb3: add sysfs "role" to set "b-device" mode Yoshihiro Shimoda
@ 2017-03-28 12:06   ` Felipe Balbi
  2017-03-29  2:00     ` Yoshihiro Shimoda
  0 siblings, 1 reply; 11+ messages in thread
From: Felipe Balbi @ 2017-03-28 12:06 UTC (permalink / raw)
  To: Yoshihiro Shimoda; +Cc: gregkh, linux-usb, linux-renesas-soc, Yoshihiro Shimoda


Hi,

Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> writes:
> Sadly, to change the role ("host" and "peripheral") of USB3.0 DRD
> controller on R-Car Gen3, software has to set the DRD_CON register
> which is included in a usb3.0 peripheral controler's register.
> To simply implementation, the previous code always set peripheral mode
> as hardcoded. However, to support usb role swap in the future,
> the hardcoded is not good. So, this patch adds sysfs "role" to set
> the mode by a user.
> After applied this patch, since the DRD controller will act as host
> mode after probed, a user needs to change the mode via the sysfs.
>
> Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>

in patch 3, you add extcon to monitor ID and VBUS pins. Do you really
need this sysfs file at all?

-- 
balbi

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: [PATCH 1/4] usb: gadget: udc: renesas_usb3: add sysfs "role" to set "b-device" mode
  2017-03-28 12:06   ` Felipe Balbi
@ 2017-03-29  2:00     ` Yoshihiro Shimoda
  2017-03-29  5:52       ` Felipe Balbi
  0 siblings, 1 reply; 11+ messages in thread
From: Yoshihiro Shimoda @ 2017-03-29  2:00 UTC (permalink / raw)
  To: Felipe Balbi; +Cc: gregkh, linux-usb, linux-renesas-soc

Hi Felipe-san,

> From: Felipe Balbi, Sent: Tuesday, March 28, 2017 9:06 PM
> 
> Hi,
> 
> Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> writes:
> > Sadly, to change the role ("host" and "peripheral") of USB3.0 DRD
> > controller on R-Car Gen3, software has to set the DRD_CON register
> > which is included in a usb3.0 peripheral controler's register.
> > To simply implementation, the previous code always set peripheral mode
> > as hardcoded. However, to support usb role swap in the future,
> > the hardcoded is not good. So, this patch adds sysfs "role" to set
> > the mode by a user.
> > After applied this patch, since the DRD controller will act as host
> > mode after probed, a user needs to change the mode via the sysfs.
> >
> > Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
> 
> in patch 3, you add extcon to monitor ID and VBUS pins. Do you really
> need this sysfs file at all?

Yes. This is because this "role" sysfs file cannot monitor the ID and VBUS pins.
The "role" only monitors the mode which is "host" or "peripheral".

Best regards,
Yoshihiro Shimoda

> --
> balbi

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: [PATCH 1/4] usb: gadget: udc: renesas_usb3: add sysfs "role" to set "b-device" mode
  2017-03-29  2:00     ` Yoshihiro Shimoda
@ 2017-03-29  5:52       ` Felipe Balbi
  2017-03-29  7:24         ` Yoshihiro Shimoda
  0 siblings, 1 reply; 11+ messages in thread
From: Felipe Balbi @ 2017-03-29  5:52 UTC (permalink / raw)
  To: Yoshihiro Shimoda; +Cc: gregkh, linux-usb, linux-renesas-soc



Hi,

Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> writes:
> Hi Felipe-san,
>
>> From: Felipe Balbi, Sent: Tuesday, March 28, 2017 9:06 PM
>> 
>> Hi,
>> 
>> Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> writes:
>> > Sadly, to change the role ("host" and "peripheral") of USB3.0 DRD
>> > controller on R-Car Gen3, software has to set the DRD_CON register
>> > which is included in a usb3.0 peripheral controler's register.
>> > To simply implementation, the previous code always set peripheral mode
>> > as hardcoded. However, to support usb role swap in the future,
>> > the hardcoded is not good. So, this patch adds sysfs "role" to set
>> > the mode by a user.
>> > After applied this patch, since the DRD controller will act as host
>> > mode after probed, a user needs to change the mode via the sysfs.
>> >
>> > Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
>> 
>> in patch 3, you add extcon to monitor ID and VBUS pins. Do you really
>> need this sysfs file at all?
>
> Yes. This is because this "role" sysfs file cannot monitor the ID and VBUS pins.
> The "role" only monitors the mode which is "host" or "peripheral".

Right, I understand it can't monitor the state of ID/VBUS, but do you
have a usecase for that file that's not covered by the other patches in
the series? :-)

If you want to force renesas controller to peripheral mode, then
shouldn't you use dr-mode device_property() instead?

-- 
balbi

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: [PATCH 1/4] usb: gadget: udc: renesas_usb3: add sysfs "role" to set "b-device" mode
  2017-03-29  5:52       ` Felipe Balbi
@ 2017-03-29  7:24         ` Yoshihiro Shimoda
  2017-03-29  8:03           ` Felipe Balbi
  0 siblings, 1 reply; 11+ messages in thread
From: Yoshihiro Shimoda @ 2017-03-29  7:24 UTC (permalink / raw)
  To: Felipe Balbi; +Cc: gregkh, linux-usb, linux-renesas-soc

Hi,

> -----Original Message-----
> From: Felipe Balbi, Sent: Wednesday, March 29, 2017 2:52 PM
> 
> Hi,
> 
> Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> writes:
> > Hi Felipe-san,
> >
> >> From: Felipe Balbi, Sent: Tuesday, March 28, 2017 9:06 PM
> >>
> >> Hi,
> >>
> >> Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> writes:
> >> > Sadly, to change the role ("host" and "peripheral") of USB3.0 DRD
> >> > controller on R-Car Gen3, software has to set the DRD_CON register
> >> > which is included in a usb3.0 peripheral controler's register.
> >> > To simply implementation, the previous code always set peripheral mode
> >> > as hardcoded. However, to support usb role swap in the future,
> >> > the hardcoded is not good. So, this patch adds sysfs "role" to set
> >> > the mode by a user.
> >> > After applied this patch, since the DRD controller will act as host
> >> > mode after probed, a user needs to change the mode via the sysfs.
> >> >
> >> > Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
> >>
> >> in patch 3, you add extcon to monitor ID and VBUS pins. Do you really
> >> need this sysfs file at all?
> >
> > Yes. This is because this "role" sysfs file cannot monitor the ID and VBUS pins.
> > The "role" only monitors the mode which is "host" or "peripheral".
> 
> Right, I understand it can't monitor the state of ID/VBUS, but do you
> have a usecase for that file that's not covered by the other patches in
> the series? :-)

Thank you for the comment. Since my English skill is not good,
I don't understand your question.
But,

> If you want to force renesas controller to peripheral mode, then
> shouldn't you use dr-mode device_property() instead?

I agree with you. I will use the dr-mode property on this driver,
instead of this sysfs' "b-device" argument.

Best regards,
Yoshihiro Shimoda

> --
> balbi

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: [PATCH 1/4] usb: gadget: udc: renesas_usb3: add sysfs "role" to set "b-device" mode
  2017-03-29  7:24         ` Yoshihiro Shimoda
@ 2017-03-29  8:03           ` Felipe Balbi
  2017-03-29  8:39             ` Yoshihiro Shimoda
  0 siblings, 1 reply; 11+ messages in thread
From: Felipe Balbi @ 2017-03-29  8:03 UTC (permalink / raw)
  To: Yoshihiro Shimoda; +Cc: gregkh, linux-usb, linux-renesas-soc

[-- Attachment #1: Type: text/plain, Size: 1889 bytes --]


Yoshihiro-san

Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> writes:
>> >> Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> writes:
>> >> > Sadly, to change the role ("host" and "peripheral") of USB3.0 DRD
>> >> > controller on R-Car Gen3, software has to set the DRD_CON register
>> >> > which is included in a usb3.0 peripheral controler's register.
>> >> > To simply implementation, the previous code always set peripheral mode
>> >> > as hardcoded. However, to support usb role swap in the future,
>> >> > the hardcoded is not good. So, this patch adds sysfs "role" to set
>> >> > the mode by a user.
>> >> > After applied this patch, since the DRD controller will act as host
>> >> > mode after probed, a user needs to change the mode via the sysfs.
>> >> >
>> >> > Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
>> >>
>> >> in patch 3, you add extcon to monitor ID and VBUS pins. Do you really
>> >> need this sysfs file at all?
>> >
>> > Yes. This is because this "role" sysfs file cannot monitor the ID and VBUS pins.
>> > The "role" only monitors the mode which is "host" or "peripheral".
>> 
>> Right, I understand it can't monitor the state of ID/VBUS, but do you
>> have a usecase for that file that's not covered by the other patches in
>> the series? :-)
>
> Thank you for the comment. Since my English skill is not good,
> I don't understand your question.

oh, that's alright. I'll try to explain:

I'm asking if you have any situation where you need userspace to change
the role manually. I understand it's a very nice feature to have during
development because it frees us from searching for "the right cable";
but if the only application of this sysfs file is durinb development,
then perhaps it should be exposed using debugfs instead ;-)

Hopefully that's easier to understand now.

-- 
balbi

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: [PATCH 1/4] usb: gadget: udc: renesas_usb3: add sysfs "role" to set "b-device" mode
  2017-03-29  8:03           ` Felipe Balbi
@ 2017-03-29  8:39             ` Yoshihiro Shimoda
  0 siblings, 0 replies; 11+ messages in thread
From: Yoshihiro Shimoda @ 2017-03-29  8:39 UTC (permalink / raw)
  To: Felipe Balbi; +Cc: gregkh, linux-usb, linux-renesas-soc

Hi Felipe-san,

> From: Felipe Balbi, Sent: Wednesday, March 29, 2017 5:03 PM
> 
> Yoshihiro-san
> 
> Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> writes:
> >> >> Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> writes:
> >> >> > Sadly, to change the role ("host" and "peripheral") of USB3.0 DRD
> >> >> > controller on R-Car Gen3, software has to set the DRD_CON register
> >> >> > which is included in a usb3.0 peripheral controler's register.
> >> >> > To simply implementation, the previous code always set peripheral mode
> >> >> > as hardcoded. However, to support usb role swap in the future,
> >> >> > the hardcoded is not good. So, this patch adds sysfs "role" to set
> >> >> > the mode by a user.
> >> >> > After applied this patch, since the DRD controller will act as host
> >> >> > mode after probed, a user needs to change the mode via the sysfs.
> >> >> >
> >> >> > Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
> >> >>
> >> >> in patch 3, you add extcon to monitor ID and VBUS pins. Do you really
> >> >> need this sysfs file at all?
> >> >
> >> > Yes. This is because this "role" sysfs file cannot monitor the ID and VBUS pins.
> >> > The "role" only monitors the mode which is "host" or "peripheral".
> >>
> >> Right, I understand it can't monitor the state of ID/VBUS, but do you
> >> have a usecase for that file that's not covered by the other patches in
> >> the series? :-)
> >
> > Thank you for the comment. Since my English skill is not good,
> > I don't understand your question.
> 
> oh, that's alright. I'll try to explain:
> 
> I'm asking if you have any situation where you need userspace to change
> the role manually. I understand it's a very nice feature to have during
> development because it frees us from searching for "the right cable";
> but if the only application of this sysfs file is durinb development,
> then perhaps it should be exposed using debugfs instead ;-)
> 
> Hopefully that's easier to understand now.

Thank you for the explanation! I understood this :)

This "b-device" mode is totally for development purpose.
 - The old version SoC has a restriction (cannot monitor ID pin!),
   So even if a board has B connector, I should turn the mode to peripheral.
 - I would like to develop this driver using A connector on a board that is related
   to SoC and board environment specification :)

So, I think this code is low priority thing for now.
Thus, I will remove this patch 1 and rebase other patches and resubmit later today.

Best regards,
Yoshihiro Shimoda

> --
> balbi

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2017-03-29  8:39 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-28 11:53 [PATCH 0/4] usb: gadget: udc: renesas_usb3: add USB3.0 DRD support Yoshihiro Shimoda
2017-03-28 11:53 ` [PATCH 1/4] usb: gadget: udc: renesas_usb3: add sysfs "role" to set "b-device" mode Yoshihiro Shimoda
2017-03-28 12:06   ` Felipe Balbi
2017-03-29  2:00     ` Yoshihiro Shimoda
2017-03-29  5:52       ` Felipe Balbi
2017-03-29  7:24         ` Yoshihiro Shimoda
2017-03-29  8:03           ` Felipe Balbi
2017-03-29  8:39             ` Yoshihiro Shimoda
2017-03-28 11:53 ` [PATCH 2/4] usb: gadget: udc: add USB ID signal monitoring Yoshihiro Shimoda
2017-03-28 11:53 ` [PATCH 3/4] usb: gadget: udc: renesas_usb3: add extcon support Yoshihiro Shimoda
2017-03-28 11:53 ` [PATCH 4/4] usb: gadget: udc: renesas_usb3: add support for usb role swap Yoshihiro Shimoda

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.