Skip to contentSkip to conversation

extcon: ptn5150: add usb role class support

PATCH In review Li Jun 4 participants 14 messages Apr 6, 2022 – Apr 19, 2022

Subsystems (2)
lore
Share & tools
lore

Li Jun

author
#0 lore raw

PATCH 1/1 extcon: ptn5150: add usb role class support

Add support of usb role class consumer to do role switch.

---
 drivers/extcon/Kconfig          |  1 +
 drivers/extcon/extcon-ptn5150.c | 39 +++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
drivers/extcon/Kconfig +1 -0
index 0d42e49105dd..9828ade787a8 100644
@@ -132,6 +132,7 @@ config EXTCON_PTN5150
 	tristate "NXP PTN5150 CC LOGIC USB EXTCON support"
 	depends on I2C && (GPIOLIB || COMPILE_TEST)
 	select REGMAP_I2C
+	select USB_ROLE_SWITCH
 	help
 	  Say Y here to enable support for USB peripheral and USB host
 	  detection by NXP PTN5150 CC (Configuration Channel) logic chip.
drivers/extcon/extcon-ptn5150.c +39 -0
index 5b9a3cf8df26..821b0f390308 100644
@@ -17,6 +17,7 @@
 #include <linux/slab.h>
 #include <linux/extcon-provider.h>
 #include <linux/gpio/consumer.h>
+#include <linux/usb/role.h>
 
 /* PTN5150 registers */
 #define PTN5150_REG_DEVICE_ID			0x01
@@ -52,6 +53,7 @@ struct ptn5150_info {
 	int irq;
 	struct work_struct irq_work;
 	struct mutex mutex;
+	struct usb_role_switch *role_sw;
 };
 
 /* List of detectable cables */
@@ -85,6 +87,12 @@ static void ptn5150_check_state(struct ptn5150_info *info)
 		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false);
 		gpiod_set_value_cansleep(info->vbus_gpiod, 0);
 		extcon_set_state_sync(info->edev, EXTCON_USB, true);
+
+		ret = usb_role_switch_set_role(info->role_sw, USB_ROLE_DEVICE);
+		if (ret)
+			dev_err(info->dev, "failed to set device role: %d\n",
+				ret);
+
 		break;
 	case PTN5150_UFP_ATTACHED:
 		extcon_set_state_sync(info->edev, EXTCON_USB, false);
@@ -95,6 +103,12 @@ static void ptn5150_check_state(struct ptn5150_info *info)
 			gpiod_set_value_cansleep(info->vbus_gpiod, 1);
 
 		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, true);
+
+		ret = usb_role_switch_set_role(info->role_sw, USB_ROLE_HOST);
+		if (ret)
+			dev_err(info->dev, "failed to set host role: %d\n",
+				ret);
+
 		break;
 	default:
 		break;
@@ -133,6 +147,13 @@ static void ptn5150_irq_work(struct work_struct *work)
 			extcon_set_state_sync(info->edev,
 					EXTCON_USB, false);
 			gpiod_set_value_cansleep(info->vbus_gpiod, 0);
+
+			ret = usb_role_switch_set_role(info->role_sw,
+						       USB_ROLE_NONE);
+			if (ret)
+				dev_err(info->dev,
+					"failed to set none role: %d\n",
+					ret);
 		}
 	}
 
@@ -194,6 +215,14 @@ static int ptn5150_init_dev_type(struct ptn5150_info *info)
 	return 0;
 }
 
+static void ptn5150_put_role_sw(void *data)
+{
+	struct ptn5150_info *info = data;
+
+	cancel_work_sync(&info->irq_work);
+	usb_role_switch_put(info->role_sw);
+}
+
 static int ptn5150_i2c_probe(struct i2c_client *i2c)
 {
 	struct device *dev = &i2c->dev;
@@ -284,6 +313,16 @@ static int ptn5150_i2c_probe(struct i2c_client *i2c)
 	if (ret)
 		return -EINVAL;
 
+	info->role_sw = usb_role_switch_get(info->dev);
+	if (IS_ERR(info->role_sw))
+		return PTR_ERR(info->role_sw);
+
+	if (info->role_sw) {
+		ret = devm_add_action_or_reset(dev, ptn5150_put_role_sw, info);
+		if (ret)
+			return ret;
+	}
+
 	/*
 	 * Update current extcon state if for example OTG connection was there
 	 * before the probe
Signature · 1 line
2.25.1
#1 lore raw
Quoted 1 line · Li Jun

Add support of usb role class consumer to do role switch.

Please mention also why you are doing this.

Quoted 12 lines
---
 drivers/extcon/Kconfig          |  1 +
 drivers/extcon/extcon-ptn5150.c | 39 +++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
drivers/extcon/Kconfig +0 -0
index 0d42e49105dd..9828ade787a8 100644

drivers/extcon/Kconfig config EXTCON_PTN5150 · line 135

Quoted 4 lines
drivers/extcon/Kconfig +1 -0
@@ -132,6 +132,7 @@ config EXTCON_PTN5150
 	tristate "NXP PTN5150 CC LOGIC USB EXTCON support"
 	depends on I2C && (GPIOLIB || COMPILE_TEST)
 	select REGMAP_I2C
+	select USB_ROLE_SWITCH

You do not need to select it. Driver will work without role switch, won't it? If it works, then probably it should be just imply.

Quoted 37 lines
drivers/extcon/Kconfig +0 -0
 	help
 	  Say Y here to enable support for USB peripheral and USB host
 	  detection by NXP PTN5150 CC (Configuration Channel) logic chip.
drivers/extcon/extcon-ptn5150.c +8 -0
index 5b9a3cf8df26..821b0f390308 100644
@@ -17,6 +17,7 @@
 #include <linux/slab.h>
 #include <linux/extcon-provider.h>
 #include <linux/gpio/consumer.h>
+#include <linux/usb/role.h>
 
 /* PTN5150 registers */
 #define PTN5150_REG_DEVICE_ID			0x01
@@ -52,6 +53,7 @@ struct ptn5150_info {
 	int irq;
 	struct work_struct irq_work;
 	struct mutex mutex;
+	struct usb_role_switch *role_sw;
 };
 
 /* List of detectable cables */
@@ -85,6 +87,12 @@ static void ptn5150_check_state(struct ptn5150_info *info)
 		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false);
 		gpiod_set_value_cansleep(info->vbus_gpiod, 0);
 		extcon_set_state_sync(info->edev, EXTCON_USB, true);
+
+		ret = usb_role_switch_set_role(info->role_sw, USB_ROLE_DEVICE);
+		if (ret)
+			dev_err(info->dev, "failed to set device role: %d\n",
+				ret);
+
 		break;
 	case PTN5150_UFP_ATTACHED:
 		extcon_set_state_sync(info->edev, EXTCON_USB, false);

drivers/extcon/extcon-ptn5150.c static void ptn5150_check_state(struct ptn5150_info *info) · line 111

Quoted 9 lines
drivers/extcon/extcon-ptn5150.c +6 -0
@@ -95,6 +103,12 @@ static void ptn5150_check_state(struct ptn5150_info *info)
 			gpiod_set_value_cansleep(info->vbus_gpiod, 1);
 
 		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, true);
+
+		ret = usb_role_switch_set_role(info->role_sw, USB_ROLE_HOST);
+		if (ret)
+			dev_err(info->dev, "failed to set host role: %d\n",
+				ret);
+

Instead of having usb_role_switch_set_role() in two places, how about:
1. setting local variable to USB_ROLE_HOST/USB_ROLE_NONE
2. return on default
3. one call to usb_role_switch_set_role() outside of the case?

Should create less code.

Quoted 18 lines
drivers/extcon/extcon-ptn5150.c +7 -0
 		break;
 	default:
 		break;
@@ -133,6 +147,13 @@ static void ptn5150_irq_work(struct work_struct *work)
 			extcon_set_state_sync(info->edev,
 					EXTCON_USB, false);
 			gpiod_set_value_cansleep(info->vbus_gpiod, 0);
+
+			ret = usb_role_switch_set_role(info->role_sw,
+						       USB_ROLE_NONE);
+			if (ret)
+				dev_err(info->dev,
+					"failed to set none role: %d\n",
+					ret);
 		}
 	}
 

drivers/extcon/extcon-ptn5150.c static int ptn5150_init_dev_type(struct ptn5150_info *info) · line 222

Quoted 8 lines
drivers/extcon/extcon-ptn5150.c +5 -0
@@ -194,6 +215,14 @@ static int ptn5150_init_dev_type(struct ptn5150_info *info)
 	return 0;
 }
 
+static void ptn5150_put_role_sw(void *data)
+{
+	struct ptn5150_info *info = data;
+
+	cancel_work_sync(&info->irq_work);

This looks independent and should be executed always - even if getting role_sw in probe does not succeed.

drivers/extcon/extcon-ptn5150.c static int ptn5150_init_dev_type(struct ptn5150_info *info) · line 225

Quoted 3 lines
drivers/extcon/extcon-ptn5150.c +3 -0
+	usb_role_switch_put(info->role_sw);
+}
+

Best regards,
Krzysztof

author
#2 lore raw
Quoted 11 lines

-----Original Message-----
From: Krzysztof Kozlowski <[email protected]>
Sent: Wednesday, April 6, 2022 6:16 PM
To: Jun Li <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; Frank Li <[email protected]>; Xu Yang <[email protected]>
Subject: Re: [PATCH] extcon: ptn5150: add usb role class support

Quoted 4 lines
Quoted 1 line · Li Jun

Add support of usb role class consumer to do role switch.

Please mention also why you are doing this.

Will improve in v2.

Quoted 8 lines
Quoted 8 lines
---
 drivers/extcon/Kconfig          |  1 +
 drivers/extcon/extcon-ptn5150.c | 39 +++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
Quoted 12 lines

drivers/extcon/Kconfig config EXTCON_PTN5150 · line 135

Quoted 9 lines
drivers/extcon/Kconfig +1 -0
index 0d42e49105dd..9828ade787a8 100644
@@ -132,6 +132,7 @@ config EXTCON_PTN5150
 	tristate "NXP PTN5150 CC LOGIC USB EXTCON support"
 	depends on I2C && (GPIOLIB || COMPILE_TEST)
 	select REGMAP_I2C
+	select USB_ROLE_SWITCH

You do not need to select it. Driver will work without role switch, won't it? If it works, then probably it should be just imply.

Okay, usb role class provider should enable this for me, will drop it.

Quoted 57 lines

Review

Quoted 5 lines
drivers/extcon/Kconfig +0 -0
 	help
 	  Say Y here to enable support for USB peripheral and USB host
 	  detection by NXP PTN5150 CC (Configuration Channel) logic chip.
(quoted diff) +0 -0

b/drivers/extcon/extcon-ptn5150.c

drivers/extcon/extcon-ptn5150.c static void ptn5150_check_state(struct ptn5150_info

Quoted 20 lines

index 5b9a3cf8df26..821b0f390308 100644

drivers/extcon/extcon-ptn5150.c +2 -0
@@ -17,6 +17,7 @@
 #include <linux/slab.h>
 #include <linux/extcon-provider.h>
 #include <linux/gpio/consumer.h>
+#include <linux/usb/role.h>
 
 /* PTN5150 registers */
 #define PTN5150_REG_DEVICE_ID			0x01
@@ -52,6 +53,7 @@ struct ptn5150_info {
 	int irq;
 	struct work_struct irq_work;
 	struct mutex mutex;
+	struct usb_role_switch *role_sw;
 };
 
 /* List of detectable cables */
@@ -85,6 +87,12 @@ static void ptn5150_check_state(struct ptn5150_info

*info)

drivers/extcon/extcon-ptn5150.c static void ptn5150_check_state(struct ptn5150_info

Quoted 13 lines
drivers/extcon/extcon-ptn5150.c +6 -0
 		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false);
 		gpiod_set_value_cansleep(info->vbus_gpiod, 0);
 		extcon_set_state_sync(info->edev, EXTCON_USB, true);
+
+		ret = usb_role_switch_set_role(info->role_sw, USB_ROLE_DEVICE);
+		if (ret)
+			dev_err(info->dev, "failed to set device role: %d\n",
+				ret);
+
 		break;
 	case PTN5150_UFP_ATTACHED:
 		extcon_set_state_sync(info->edev, EXTCON_USB, false);
@@ -95,6 +103,12 @@ static void ptn5150_check_state(struct ptn5150_info

*info)

drivers/extcon/extcon-ptn5150.c static void ptn5150_check_state(struct ptn5150_info *info) · line 111

Quoted 9 lines
drivers/extcon/extcon-ptn5150.c +6 -0
 			gpiod_set_value_cansleep(info->vbus_gpiod, 1);
 
 		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, true);
+
+		ret = usb_role_switch_set_role(info->role_sw, USB_ROLE_HOST);
+		if (ret)
+			dev_err(info->dev, "failed to set host role: %d\n",
+				ret);
+

Instead of having usb_role_switch_set_role() in two places, how about:
1. setting local variable to USB_ROLE_HOST/USB_ROLE_NONE
2. return on default
3. one call to usb_role_switch_set_role() outside of the case?

Quoted 1 line

Should create less code.

Yes, this will be better, I will update in v2.

Quoted 24 lines

drivers/extcon/extcon-ptn5150.c static void ptn5150_irq_work(struct work_struct

Quoted 5 lines
drivers/extcon/extcon-ptn5150.c +0 -0
 		break;
 	default:
 		break;
@@ -133,6 +147,13 @@ static void ptn5150_irq_work(struct work_struct

*work)

drivers/extcon/extcon-ptn5150.c static int ptn5150_init_dev_type(struct ptn5150_info

Quoted 14 lines
drivers/extcon/extcon-ptn5150.c +7 -0
 			extcon_set_state_sync(info->edev,
 					EXTCON_USB, false);
 			gpiod_set_value_cansleep(info->vbus_gpiod, 0);
+
+			ret = usb_role_switch_set_role(info->role_sw,
+						       USB_ROLE_NONE);
+			if (ret)
+				dev_err(info->dev,
+					"failed to set none role: %d\n",
+					ret);
 		}
 	}
 
@@ -194,6 +215,14 @@ static int ptn5150_init_dev_type(struct ptn5150_info

*info)

Quoted 3 lines
drivers/extcon/extcon-ptn5150.c +0 -0
 	return 0;
 }
 
Quoted 8 lines

drivers/extcon/extcon-ptn5150.c static int ptn5150_init_dev_type(struct ptn5150_info *info) · line 222

Quoted 5 lines
drivers/extcon/extcon-ptn5150.c +5 -0
+static void ptn5150_put_role_sw(void *data)
+{
+	struct ptn5150_info *info = data;
+
+	cancel_work_sync(&info->irq_work);

This looks independent and should be executed always - even if getting role_sw in probe does not succeed.

Agreed, I will add one patch to make this action taken in all cases.

Thanks
Li Jun

Quoted 9 lines

drivers/extcon/extcon-ptn5150.c static int ptn5150_init_dev_type(struct ptn5150_info *info) · line 225

Quoted 4 lines
drivers/extcon/extcon-ptn5150.c +3 -0
+	usb_role_switch_put(info->role_sw);
+}
+

Best regards,
Krzysztof

author
#3 lore raw
Quoted 51 lines

-----Original Message-----
From: Jun Li
Sent: Wednesday, April 6, 2022 7:49 PM
To: Krzysztof Kozlowski <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; Frank Li <[email protected]>; Xu Yang <[email protected]>
Subject: RE: [PATCH] extcon: ptn5150: add usb role class support

Quoted 9 lines

-----Original Message-----
From: Krzysztof Kozlowski <[email protected]>
Sent: Wednesday, April 6, 2022 6:16 PM
To: Jun Li <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; Frank Li <[email protected]>; Xu Yang <[email protected]>
Subject: Re: [PATCH] extcon: ptn5150: add usb role class support

Quoted 4 lines
Quoted 1 line · Li Jun

Add support of usb role class consumer to do role switch.

Please mention also why you are doing this.

Will improve in v2.

Quoted 9 lines
Quoted 9 lines
---
 drivers/extcon/Kconfig          |  1 +
 drivers/extcon/extcon-ptn5150.c | 39
+++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
Quoted 12 lines

drivers/extcon/Kconfig config EXTCON_PTN5150 · line 135

Quoted 9 lines
drivers/extcon/Kconfig index renamed from drivers/extcon/Kconfig +0 -0

0d42e49105dd..9828ade787a8 100644

drivers/extcon/Kconfig +1 -0
@@ -132,6 +132,7 @@ config EXTCON_PTN5150
 	tristate "NXP PTN5150 CC LOGIC USB EXTCON support"
 	depends on I2C && (GPIOLIB || COMPILE_TEST)
 	select REGMAP_I2C
+	select USB_ROLE_SWITCH

You do not need to select it. Driver will work without role switch, won't it? If it works, then probably it should be just imply.

Quoted 1 line

Okay, usb role class provider should enable this for me, will drop it.

A second check on this and I think I still need this, there maybe some usb controller driver without usb role switch
+ ptn5150 via extcon, so no need USB_ROLE_SWITCH, I need select it to avoid build break.

Li Jun

#4 lore raw
Quoted 9 lines · Jun Li
Quoted 9 lines
Quoted 8 lines

drivers/extcon/Kconfig config EXTCON_PTN5150 · line 135

Quoted 5 lines
drivers/extcon/Kconfig +1 -0
@@ -132,6 +132,7 @@ config EXTCON_PTN5150
  tristate "NXP PTN5150 CC LOGIC USB EXTCON support"
  depends on I2C && (GPIOLIB || COMPILE_TEST)
  select REGMAP_I2C
+ select USB_ROLE_SWITCH

You do not need to select it. Driver will work without role switch, won't it? If it works, then probably it should be just imply.

Quoted 6 lines
Quoted 1 line

Okay, usb role class provider should enable this for me, will drop it.

A second check on this and I think I still need this, there maybe some usb controller driver without usb role switch
+ ptn5150 via extcon, so no need USB_ROLE_SWITCH, I need select it to avoid build break.

What build problem exactly? Aren't there stubs for !USB_ROLE_SWITCH case?

Best regards,
Krzysztof

author
#5 lore raw
Quoted 21 lines

-----Original Message-----
From: Krzysztof Kozlowski <[email protected]>
Sent: Tuesday, April 19, 2022 3:57 PM
To: Jun Li <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Frank Li <[email protected]>; Xu Yang <[email protected]>
Subject: Re: [PATCH] extcon: ptn5150: add usb role class support

Quoted 9 lines · Jun Li
Quoted 9 lines
Quoted 8 lines

drivers/extcon/Kconfig config EXTCON_PTN5150 · line 135

Quoted 5 lines
drivers/extcon/Kconfig +1 -0
@@ -132,6 +132,7 @@ config EXTCON_PTN5150
  tristate "NXP PTN5150 CC LOGIC USB EXTCON support"
  depends on I2C && (GPIOLIB || COMPILE_TEST)
  select REGMAP_I2C
+ select USB_ROLE_SWITCH

You do not need to select it. Driver will work without role switch, won't it? If it works, then probably it should be just imply.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Okay, usb role class provider should enable this for me, will drop it.

A second check on this and I think I still need this, there maybe some usb controller driver without usb role switch
+ ptn5150 via extcon, so no need USB_ROLE_SWITCH, I need select it to avoid build break.

What build problem exactly? Aren't there stubs for !USB_ROLE_SWITCH case?

Mostly cases USB_ROLE_SWITCH is enabled, but I cannot 100% ensure that, at least I can via make menuconfig change by removing some controllers drivers to create a config to generate a build break:

/opt/fsl-imx-internal-xwayland/5.15-honister/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux/aarch64-poky-linux-ld: drivers/extcon/extcon-ptn5150.o: in function `ptn5150_work_sync_and_put':
extcon-ptn5150.c:(.text+0x20): undefined reference to usb_role_switch_put' /opt/fsl-imx-internal-xwayland/5.15-honister/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux/aarch64-poky-linux-ld: drivers/extcon/extcon-ptn5150.o: in function ptn5150_check_state':
extcon-ptn5150.c:(.text+0xc8): undefined reference to `usb_role_switch_set_role' /opt/fsl-imx-internal-xwayland/5.15-honister/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux/aarch64-poky-linux-ld: extcon-ptn5150.c:(.text+0x138): undefined reference to `usb_role_switch_set_role' /opt/fsl-imx-internal-xwayland/5.15-honister/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux/aarch64-poky-linux-ld: extcon-ptn5150.c:(.text+0x14c): undefined reference to usb_role_string' /opt/fsl-imx-internal-xwayland/5.15-honister/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux/aarch64-poky-linux-ld: drivers/extcon/extcon-ptn5150.o: in function ptn5150_irq_work':
extcon-ptn5150.c:(.text+0x290): undefined reference to usb_role_switch_set_role' /opt/fsl-imx-internal-xwayland/5.15-honister/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux/aarch64-poky-linux-ld: drivers/extcon/extcon-ptn5150.o: in function ptn5150_i2c_probe':
extcon-ptn5150.c:(.text+0x4f4): undefined reference to `usb_role_switch_get' /opt/fsl-imx-internal-xwayland/5.15-honister/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux/aarch64-poky-linux-ld: extcon-ptn5150.c:(.text+0x530): undefined reference to `usb_role_switch_put'
make: *** [Makefile:1247: vmlinux] Error 1

thanks
Li Jun

Quoted 3 lines

Best regards,
Krzysztof

#6 lore raw
Quoted 28 lines · Jun Li
Quoted 28 lines

-----Original Message-----
From: Krzysztof Kozlowski <[email protected]>
Sent: Tuesday, April 19, 2022 3:57 PM
To: Jun Li <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Frank Li <[email protected]>; Xu Yang <[email protected]>
Subject: Re: [PATCH] extcon: ptn5150: add usb role class support

Quoted 15 lines · Jun Li
Quoted 10 lines
Quoted 8 lines

drivers/extcon/Kconfig config EXTCON_PTN5150 · line 135

Quoted 5 lines
drivers/extcon/Kconfig +1 -0
@@ -132,6 +132,7 @@ config EXTCON_PTN5150
  tristate "NXP PTN5150 CC LOGIC USB EXTCON support"
  depends on I2C && (GPIOLIB || COMPILE_TEST)
  select REGMAP_I2C
+ select USB_ROLE_SWITCH

You do not need to select it. Driver will work without role switch, won't it? If it works, then probably it should be just imply.

Okay, usb role class provider should enable this for me, will drop it.

A second check on this and I think I still need this, there maybe some usb controller driver without usb role switch
+ ptn5150 via extcon, so no need USB_ROLE_SWITCH, I need select it to avoid build break.

Quoted 8 lines
Quoted 1 line

What build problem exactly? Aren't there stubs for !USB_ROLE_SWITCH case?

Mostly cases USB_ROLE_SWITCH is enabled, but I cannot 100% ensure that, at least I can via make menuconfig change by removing some controllers drivers to create a config to generate a build break:

/opt/fsl-imx-internal-xwayland/5.15-honister/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux/aarch64-poky-linux-ld: drivers/extcon/extcon-ptn5150.o: in function `ptn5150_work_sync_and_put':
extcon-ptn5150.c:(.text+0x20): undefined reference to `usb_role_switch_put'

This is some old kernel - v5.15. Please test your changes on mainline. I don't see such issue can happen on a recent kernel - there is an EXPORT and a stub. Maybe your driver misses proper headers?

Best regards,
Krzysztof

author
#7 lore raw
Quoted 50 lines

-----Original Message-----
From: Krzysztof Kozlowski <[email protected]>
Sent: Tuesday, April 19, 2022 4:30 PM
To: Jun Li <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Frank Li <[email protected]>; Xu Yang <[email protected]>
Subject: Re: [PATCH] extcon: ptn5150: add usb role class support

Quoted 28 lines · Jun Li
Quoted 28 lines

-----Original Message-----
From: Krzysztof Kozlowski <[email protected]>
Sent: Tuesday, April 19, 2022 3:57 PM
To: Jun Li <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Frank Li <[email protected]>; Xu Yang <[email protected]>
Subject: Re: [PATCH] extcon: ptn5150: add usb role class support

Quoted 15 lines · Jun Li
Quoted 10 lines
Quoted 8 lines

Review config EXTCON_PTN5150 · line 135

Quoted 5 lines
(quoted diff) +1 -0
@@ -132,6 +132,7 @@ config EXTCON_PTN5150
  tristate "NXP PTN5150 CC LOGIC USB EXTCON support"
  depends on I2C && (GPIOLIB || COMPILE_TEST)
  select REGMAP_I2C
+ select USB_ROLE_SWITCH

You do not need to select it. Driver will work without role switch, won't it? If it works, then probably it should be just imply.

Okay, usb role class provider should enable this for me, will drop it.

A second check on this and I think I still need this, there maybe some usb controller driver without usb role switch
+ ptn5150 via extcon, so no need USB_ROLE_SWITCH, I need select it to avoid build break.

Quoted 7 lines
Quoted 1 line

What build problem exactly? Aren't there stubs for !USB_ROLE_SWITCH case?

Mostly cases USB_ROLE_SWITCH is enabled, but I cannot 100% ensure that, at least I can via make menuconfig change by removing some controllers drivers to create a config to generate a build break:

/opt/fsl-imx-internal-xwayland/5.15-honister/sysroots/x86_64-pokysdk-li nux/usr/bin/aarch64-poky-linux/aarch64-poky-linux-ld:
drivers/extcon/extcon-ptn5150.o: in function `ptn5150_work_sync_and_put':

Quoted 6 lines
Quoted 1 line

extcon-ptn5150.c:(.text+0x20): undefined reference to

`usb_role_switch_put'

This is some old kernel - v5.15. Please test your changes on mainline. I don't see such issue can happen on a recent kernel - there is an EXPORT and a stub. Maybe your driver misses proper headers?

I am working on Linux-next 20220414

commit 40354149f4d738dc3492d9998e45b3f02950369a (tag: next-20220414, origin/master, origin/HEAD)

Author: Stephen Rothwell <[email protected]>
Date:   Thu Apr 14 15:22:31 2022 +1000

Add linux-next specific files for 20220414

My test config is making USB_ROLE_SWITCH=m, but PTN5150=y

So with below header file:

#if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
void usb_role_switch_put(struct usb_role_switch *sw);
#else
static inline void usb_role_switch_put(struct usb_role_switch *sw) { } #endif

Will have link error.

Thanks
Li Jun

Quoted 3 lines

Best regards,
Krzysztof

#8 lore raw
Quoted 11 lines · Jun Li

My test config is making USB_ROLE_SWITCH=m, but PTN5150=y

So with below header file:

#if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
void usb_role_switch_put(struct usb_role_switch *sw);
#else
static inline void usb_role_switch_put(struct usb_role_switch *sw) { } #endif

Will have link error.

Yep, true. I cannot remember the solution for that... With the select you cannot disable USB_ROLE_SWITCH. With "depends on X || depends on !X", one still cannot disable USB_ROLE_SWITCH. However this is a common problem and I am pretty sure people were working on this. :)

Best regards,
Krzysztof

#9 lore raw
Quoted 11 lines · Krzysztof Kozlowski
Quoted 10 lines · Jun Li

My test config is making USB_ROLE_SWITCH=m, but PTN5150=y

So with below header file:

#if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
void usb_role_switch_put(struct usb_role_switch *sw);
#else
static inline void usb_role_switch_put(struct usb_role_switch *sw) { } #endif

Quoted 6 lines
Quoted 1 line

Will have link error.

Yep, true. I cannot remember the solution for that... With the select you cannot disable USB_ROLE_SWITCH. With "depends on X || depends on !X", one still cannot disable USB_ROLE_SWITCH. However this is a common problem and I am pretty sure people were working on this. :)

Use:
depends on USB_ROLE_SWITCH || !USB_ROLE_SWITCH'

this should solve the problem.

Best regards,
Krzysztof

author
#10 lore raw
Quoted 29 lines

-----Original Message-----
From: Krzysztof Kozlowski <[email protected]>
Sent: Tuesday, April 19, 2022 5:52 PM
To: Jun Li <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Frank Li <[email protected]>; Xu Yang <[email protected]>
Subject: Re: [PATCH] extcon: ptn5150: add usb role class support

Quoted 10 lines · Krzysztof Kozlowski
Quoted 9 lines · Jun Li

My test config is making USB_ROLE_SWITCH=m, but PTN5150=y

So with below header file:

#if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
void usb_role_switch_put(struct usb_role_switch *sw); #else static inline void usb_role_switch_put(struct usb_role_switch *sw) { } #endif

Quoted 6 lines
Quoted 1 line

Will have link error.

Yep, true. I cannot remember the solution for that... With the select you cannot disable USB_ROLE_SWITCH. With "depends on X || depends on !X", one still cannot disable USB_ROLE_SWITCH. However this is a common problem and I am pretty sure people were working on this. :)

Quoted 4 lines

Use:
depends on USB_ROLE_SWITCH || !USB_ROLE_SWITCH'

this should solve the problem.

This works, thanks for the review and suggestion, I will send out v3.

Li Jun

Quoted 3 lines

Best regards,
Krzysztof

#11 lore raw
In reply to Li Jun
Thank you for the patch! Yet something to improve:

Hi Li,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on chanwoo-extcon/extcon-next]
[also build test ERROR on v5.18-rc1 next-20220406]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Li-Jun/extcon-ptn5150-add-usb-role-class-support/20220406-212038
base:   https://git.kernel.org/pub/scm/linux/kernel/git/chanwoo/extcon.git extcon-next
config: parisc-randconfig-r031-20220406 (https://download.01.org/0day-ci/archive/20220407/[email protected]/config)
compiler: hppa-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/beb6b76258bb1ae1e371486cd4fe97fee393ac83
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Li-Jun/extcon-ptn5150-add-usb-role-class-support/20220406-212038 git checkout beb6b76258bb1ae1e371486cd4fe97fee393ac83
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=parisc SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

hppa-linux-ld: drivers/extcon/extcon-ptn5150.o: in function `ptn5150_put_role_sw':

>> (.text+0x20): undefined reference to `usb_role_switch_put'
   hppa-linux-ld: drivers/extcon/extcon-ptn5150.o: in function `ptn5150_check_state':
>> (.text+0xc8): undefined reference to `usb_role_switch_set_role'
>> hppa-linux-ld: (.text+0x11c): undefined reference to `usb_role_switch_set_role'
   hppa-linux-ld: drivers/extcon/extcon-ptn5150.o: in function `ptn5150_irq_work':
   (.text+0x1f4): undefined reference to `usb_role_switch_set_role'
   hppa-linux-ld: drivers/extcon/extcon-ptn5150.o: in function `ptn5150_i2c_probe':
>> (.text+0x4fc): undefined reference to `usb_role_switch_get'
>> hppa-linux-ld: (.text+0x53c): undefined reference to `usb_role_switch_put'

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for USB_ROLE_SWITCH Depends on USB_SUPPORT
Selected by
- EXTCON_PTN5150 && EXTCON && I2C && (GPIOLIB || COMPILE_TEST

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
maintainer
#12 lore raw
In reply to Li Jun

Hi,

Quoted 97 lines · Li Jun

Add support of usb role class consumer to do role switch.

---
 drivers/extcon/Kconfig          |  1 +
 drivers/extcon/extcon-ptn5150.c | 39 +++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
drivers/extcon/Kconfig +1 -0
index 0d42e49105dd..9828ade787a8 100644
@@ -132,6 +132,7 @@ config EXTCON_PTN5150
 	tristate "NXP PTN5150 CC LOGIC USB EXTCON support"
 	depends on I2C && (GPIOLIB || COMPILE_TEST)
 	select REGMAP_I2C
+	select USB_ROLE_SWITCH
 	help
 	  Say Y here to enable support for USB peripheral and USB host
 	  detection by NXP PTN5150 CC (Configuration Channel) logic chip.
drivers/extcon/extcon-ptn5150.c +8 -0
index 5b9a3cf8df26..821b0f390308 100644
@@ -17,6 +17,7 @@
 #include <linux/slab.h>
 #include <linux/extcon-provider.h>
 #include <linux/gpio/consumer.h>
+#include <linux/usb/role.h>
 
 /* PTN5150 registers */
 #define PTN5150_REG_DEVICE_ID			0x01
@@ -52,6 +53,7 @@ struct ptn5150_info {
 	int irq;
 	struct work_struct irq_work;
 	struct mutex mutex;
+	struct usb_role_switch *role_sw;
 };
 
 /* List of detectable cables */
@@ -85,6 +87,12 @@ static void ptn5150_check_state(struct ptn5150_info *info)
 		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false);
 		gpiod_set_value_cansleep(info->vbus_gpiod, 0);
 		extcon_set_state_sync(info->edev, EXTCON_USB, true);
+
+		ret = usb_role_switch_set_role(info->role_sw, USB_ROLE_DEVICE);
+		if (ret)
+			dev_err(info->dev, "failed to set device role: %d\n",
+				ret);
 
+
 		break;
 	case PTN5150_UFP_ATTACHED:
drivers/extcon/extcon-ptn5150.c +21 -0
 		extcon_set_state_sync(info->edev, EXTCON_USB, false);
@@ -95,6 +103,12 @@ static void ptn5150_check_state(struct ptn5150_info *info)
 			gpiod_set_value_cansleep(info->vbus_gpiod, 1);
 
 		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, true);
+
+		ret = usb_role_switch_set_role(info->role_sw, USB_ROLE_HOST);
+		if (ret)
+			dev_err(info->dev, "failed to set host role: %d\n",
+				ret);
+
 		break;
 	default:
 		break;
@@ -133,6 +147,13 @@ static void ptn5150_irq_work(struct work_struct *work)
 			extcon_set_state_sync(info->edev,
 					EXTCON_USB, false);
 			gpiod_set_value_cansleep(info->vbus_gpiod, 0);
+
+			ret = usb_role_switch_set_role(info->role_sw,
+						       USB_ROLE_NONE);
+			if (ret)
+				dev_err(info->dev,
+					"failed to set none role: %d\n",
+					ret);
 		}
 	}
 
@@ -194,6 +215,14 @@ static int ptn5150_init_dev_type(struct ptn5150_info *info)
 	return 0;
 }
 
+static void ptn5150_put_role_sw(void *data)
+{
+	struct ptn5150_info *info = data;
+
+	cancel_work_sync(&info->irq_work);
+	usb_role_switch_put(info->role_sw);
+}
+
 static int ptn5150_i2c_probe(struct i2c_client *i2c)
 {
 	struct device *dev = &i2c->dev;

drivers/extcon/extcon-ptn5150.c static int ptn5150_i2c_probe(struct i2c_client *i2c) · line 318

Quoted 6 lines
drivers/extcon/extcon-ptn5150.c +3 -0
@@ -284,6 +313,16 @@ static int ptn5150_i2c_probe(struct i2c_client *i2c)
 	if (ret)
 		return -EINVAL;
 
+	info->role_sw = usb_role_switch_get(info->dev);
+	if (IS_ERR(info->role_sw))
+		return PTR_ERR(info->role_sw);

If usb_role_switch is optional, you don't need to return error
when usb_role_switch_get returns error. Because 'usb_role_switch_set_role' on this patch doesn't break the sequence. Just print the error log if error case.

Quoted 11 lines
drivers/extcon/extcon-ptn5150.c +7 -0
+
+	if (info->role_sw) {
+		ret = devm_add_action_or_reset(dev, ptn5150_put_role_sw, info);
+		if (ret)
+			return ret;
+	}
+
 	/*
 	 * Update current extcon state if for example OTG connection was there
 	 * before the probe
Signature · 3 lines
Best Regards,
Chanwoo Choi
Samsung Electronics
author
#13 lore raw

Hi

Quoted 114 lines

-----Original Message-----
From: Chanwoo Choi <[email protected]>
Sent: Thursday, April 7, 2022 10:18 AM
To: Jun Li <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; Frank Li <[email protected]>; Xu Yang <[email protected]>
Subject: Re: [PATCH] extcon: ptn5150: add usb role class support

Hi,

drivers/extcon/extcon-ptn5150.c static void ptn5150_check_state(struct ptn5150_info

Quoted 43 lines · Li Jun

Add support of usb role class consumer to do role switch.

---
 drivers/extcon/Kconfig          |  1 +
 drivers/extcon/extcon-ptn5150.c | 39
+++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
drivers/extcon/Kconfig index renamed from drivers/extcon/Kconfig +0 -0

0d42e49105dd..9828ade787a8 100644

drivers/extcon/Kconfig +1 -0
@@ -132,6 +132,7 @@ config EXTCON_PTN5150
 	tristate "NXP PTN5150 CC LOGIC USB EXTCON support"
 	depends on I2C && (GPIOLIB || COMPILE_TEST)
 	select REGMAP_I2C
+	select USB_ROLE_SWITCH
 	help
 	  Say Y here to enable support for USB peripheral and USB host
 	  detection by NXP PTN5150 CC (Configuration Channel) logic chip.
(quoted diff) +0 -0

b/drivers/extcon/extcon-ptn5150.c index 5b9a3cf8df26..821b0f390308 100644

drivers/extcon/extcon-ptn5150.c +2 -0
@@ -17,6 +17,7 @@
 #include <linux/slab.h>
 #include <linux/extcon-provider.h>
 #include <linux/gpio/consumer.h>
+#include <linux/usb/role.h>
 
 /* PTN5150 registers */
 #define PTN5150_REG_DEVICE_ID			0x01
@@ -52,6 +53,7 @@ struct ptn5150_info {
 	int irq;
 	struct work_struct irq_work;
 	struct mutex mutex;
+	struct usb_role_switch *role_sw;
 };
 
 /* List of detectable cables */
@@ -85,6 +87,12 @@ static void ptn5150_check_state(struct ptn5150_info

*info)

drivers/extcon/extcon-ptn5150.c static void ptn5150_irq_work(struct work_struct

Quoted 27 lines
drivers/extcon/extcon-ptn5150.c +6 -0
 		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false);
 		gpiod_set_value_cansleep(info->vbus_gpiod, 0);
 		extcon_set_state_sync(info->edev, EXTCON_USB, true);
+
+		ret = usb_role_switch_set_role(info->role_sw, USB_ROLE_DEVICE);
+		if (ret)
+			dev_err(info->dev, "failed to set device role: %d\n",
+				ret);
 
+
 		break;
 	case PTN5150_UFP_ATTACHED:
drivers/extcon/extcon-ptn5150.c +7 -0
 		extcon_set_state_sync(info->edev, EXTCON_USB, false); @@ -95,6
+103,12 @@ static void ptn5150_check_state(struct ptn5150_info *info)
 			gpiod_set_value_cansleep(info->vbus_gpiod, 1);
 
 		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, true);
+
+		ret = usb_role_switch_set_role(info->role_sw, USB_ROLE_HOST);
+		if (ret)
+			dev_err(info->dev, "failed to set host role: %d\n",
+				ret);
+
 		break;
 	default:
 		break;
@@ -133,6 +147,13 @@ static void ptn5150_irq_work(struct work_struct

*work)

drivers/extcon/extcon-ptn5150.c static int ptn5150_init_dev_type(struct ptn5150_info

Quoted 14 lines
drivers/extcon/extcon-ptn5150.c +7 -0
 			extcon_set_state_sync(info->edev,
 					EXTCON_USB, false);
 			gpiod_set_value_cansleep(info->vbus_gpiod, 0);
+
+			ret = usb_role_switch_set_role(info->role_sw,
+						       USB_ROLE_NONE);
+			if (ret)
+				dev_err(info->dev,
+					"failed to set none role: %d\n",
+					ret);
 		}
 	}
 
@@ -194,6 +215,14 @@ static int ptn5150_init_dev_type(struct ptn5150_info

*info)

Quoted 16 lines
drivers/extcon/extcon-ptn5150.c +7 -0
 	return 0;
 }
 
+static void ptn5150_put_role_sw(void *data) {
+	struct ptn5150_info *info = data;
+
+	cancel_work_sync(&info->irq_work);
+	usb_role_switch_put(info->role_sw);
+}
+
 static int ptn5150_i2c_probe(struct i2c_client *i2c)  {
 	struct device *dev = &i2c->dev;
@@ -284,6 +313,16 @@ static int ptn5150_i2c_probe(struct i2c_client *i2c)
 	if (ret)
 		return -EINVAL;
 
Quoted 8 lines

drivers/extcon/extcon-ptn5150.c static int ptn5150_i2c_probe(struct i2c_client *i2c) · line 318

Quoted 3 lines
drivers/extcon/extcon-ptn5150.c +3 -0
+	info->role_sw = usb_role_switch_get(info->dev);
+	if (IS_ERR(info->role_sw))
+		return PTR_ERR(info->role_sw);

If usb_role_switch is optional, you don't need to return error when usb_role_switch_get returns error. Because 'usb_role_switch_set_role' on this patch doesn't break the sequence. Just print the error log if error case.

Yes, it's optional, if the usb role switch is not provided, then usb_role_switch_get() will return NULL, then usb role switch will do nothing;
if the usb role switch is provided(intended to use) but there is something wrong to get it, we must return error to force a failure in probe(including probe defer).

Li Jun

Quoted 18 lines

drivers/extcon/extcon-ptn5150.c static int ptn5150_i2c_probe(struct i2c_client *i2c) · line 328

Quoted 12 lines
drivers/extcon/extcon-ptn5150.c +7 -0
+
+	if (info->role_sw) {
+		ret = devm_add_action_or_reset(dev, ptn5150_put_role_sw, info);
+		if (ret)
+			return ret;
+	}
+
 	/*
 	 * Update current extcon state if for example OTG connection was there
 	 * before the probe

--
Best Regards,
Chanwoo Choi
Samsung Electronics