linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/2] extcon: ptn5150: add queue work sync before driver release
@ 2022-04-19  7:43 Li Jun
  2022-04-19  7:43 ` [PATCH v2 2/2] extcon: ptn5150: Add usb role class support Li Jun
  2022-04-19  8:32 ` [PATCH v2 1/2] extcon: ptn5150: add queue work sync before driver release Krzysztof Kozlowski
  0 siblings, 2 replies; 4+ messages in thread
From: Li Jun @ 2022-04-19  7:43 UTC (permalink / raw)
  To: myungjoo.ham, cw00.choi, krzk; +Cc: linux-kernel, frank.li, xu.yang_2

Add device managed action to sync pending queue work, otherwise
the queued work may run after the work is destroyed.

Signed-off-by: Li Jun <jun.li@nxp.com>
---
- new patch

 drivers/extcon/extcon-ptn5150.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/extcon/extcon-ptn5150.c b/drivers/extcon/extcon-ptn5150.c
index 5b9a3cf8df26..2a7874108df8 100644
--- a/drivers/extcon/extcon-ptn5150.c
+++ b/drivers/extcon/extcon-ptn5150.c
@@ -194,6 +194,13 @@ static int ptn5150_init_dev_type(struct ptn5150_info *info)
 	return 0;
 }
 
+static void ptn5150_work_sync_and_put(void *data)
+{
+	struct ptn5150_info *info = data;
+
+	cancel_work_sync(&info->irq_work);
+}
+
 static int ptn5150_i2c_probe(struct i2c_client *i2c)
 {
 	struct device *dev = &i2c->dev;
@@ -284,6 +291,10 @@ static int ptn5150_i2c_probe(struct i2c_client *i2c)
 	if (ret)
 		return -EINVAL;
 
+	ret = devm_add_action_or_reset(dev, ptn5150_work_sync_and_put, info);
+	if (ret)
+		return ret;
+
 	/*
 	 * Update current extcon state if for example OTG connection was there
 	 * before the probe
-- 
2.25.1


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

* [PATCH v2 2/2] extcon: ptn5150: Add usb role class support
  2022-04-19  7:43 [PATCH v2 1/2] extcon: ptn5150: add queue work sync before driver release Li Jun
@ 2022-04-19  7:43 ` Li Jun
  2022-04-19  8:35   ` Krzysztof Kozlowski
  2022-04-19  8:32 ` [PATCH v2 1/2] extcon: ptn5150: add queue work sync before driver release Krzysztof Kozlowski
  1 sibling, 1 reply; 4+ messages in thread
From: Li Jun @ 2022-04-19  7:43 UTC (permalink / raw)
  To: myungjoo.ham, cw00.choi, krzk; +Cc: linux-kernel, frank.li, xu.yang_2

Some usb controller drivers may not support extcon but use
usb role class as it's the preferred approach, so to support
usb dual role switch with usb role class, add usb role class
consumer support.

Signed-off-by: Li Jun <jun.li@nxp.com>
---
change for v2:
- Add one patch to sync queue work before driver remove.
- improve git commit log
- use a variable of usb_role to set target role in
  ptn5150_check_state() to simplify code.
- use dev_err_probe for usb_role_switch_get() return value.

 drivers/extcon/Kconfig          |  1 +
 drivers/extcon/extcon-ptn5150.c | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/drivers/extcon/Kconfig b/drivers/extcon/Kconfig
index c69d40ae5619..f3352a81ee7c 100644
--- a/drivers/extcon/Kconfig
+++ b/drivers/extcon/Kconfig
@@ -130,6 +130,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.
diff --git a/drivers/extcon/extcon-ptn5150.c b/drivers/extcon/extcon-ptn5150.c
index 2a7874108df8..017a07197f38 100644
--- a/drivers/extcon/extcon-ptn5150.c
+++ b/drivers/extcon/extcon-ptn5150.c
@@ -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 */
@@ -70,6 +72,7 @@ static const struct regmap_config ptn5150_regmap_config = {
 static void ptn5150_check_state(struct ptn5150_info *info)
 {
 	unsigned int port_status, reg_data, vbus;
+	enum usb_role usb_role = USB_ROLE_NONE;
 	int ret;
 
 	ret = regmap_read(info->regmap, PTN5150_REG_CC_STATUS, &reg_data);
@@ -85,6 +88,7 @@ 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);
+		usb_role = USB_ROLE_DEVICE;
 		break;
 	case PTN5150_UFP_ATTACHED:
 		extcon_set_state_sync(info->edev, EXTCON_USB, false);
@@ -95,10 +99,18 @@ 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);
+		usb_role = USB_ROLE_HOST;
 		break;
 	default:
 		break;
 	}
+
+	if (usb_role) {
+		ret = usb_role_switch_set_role(info->role_sw, usb_role);
+		if (ret)
+			dev_err(info->dev, "failed to set %s role: %d\n",
+				usb_role_string(usb_role), ret);
+	}
 }
 
 static void ptn5150_irq_work(struct work_struct *work)
@@ -133,6 +145,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);
 		}
 	}
 
@@ -199,6 +218,7 @@ static void ptn5150_work_sync_and_put(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)
@@ -291,6 +311,11 @@ 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 dev_err_probe(info->dev, PTR_ERR(info->role_sw),
+				     "failed to get role switch\n");
+
 	ret = devm_add_action_or_reset(dev, ptn5150_work_sync_and_put, info);
 	if (ret)
 		return ret;
-- 
2.25.1


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

* Re: [PATCH v2 1/2] extcon: ptn5150: add queue work sync before driver release
  2022-04-19  7:43 [PATCH v2 1/2] extcon: ptn5150: add queue work sync before driver release Li Jun
  2022-04-19  7:43 ` [PATCH v2 2/2] extcon: ptn5150: Add usb role class support Li Jun
@ 2022-04-19  8:32 ` Krzysztof Kozlowski
  1 sibling, 0 replies; 4+ messages in thread
From: Krzysztof Kozlowski @ 2022-04-19  8:32 UTC (permalink / raw)
  To: Li Jun, myungjoo.ham, cw00.choi; +Cc: linux-kernel, frank.li, xu.yang_2

On 19/04/2022 09:43, Li Jun wrote:
> Add device managed action to sync pending queue work, otherwise
> the queued work may run after the work is destroyed.
> 
> Signed-off-by: Li Jun <jun.li@nxp.com>

Fixes: 4ed754de2d66 ("extcon: Add support for ptn5150 extcon driver")


Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>


Best regards,
Krzysztof

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

* Re: [PATCH v2 2/2] extcon: ptn5150: Add usb role class support
  2022-04-19  7:43 ` [PATCH v2 2/2] extcon: ptn5150: Add usb role class support Li Jun
@ 2022-04-19  8:35   ` Krzysztof Kozlowski
  0 siblings, 0 replies; 4+ messages in thread
From: Krzysztof Kozlowski @ 2022-04-19  8:35 UTC (permalink / raw)
  To: Li Jun, myungjoo.ham, cw00.choi; +Cc: linux-kernel, frank.li, xu.yang_2

On 19/04/2022 09:43, Li Jun wrote:
> Some usb controller drivers may not support extcon but use
> usb role class as it's the preferred approach, so to support
> usb dual role switch with usb role class, add usb role class
> consumer support.
> 
> Signed-off-by: Li Jun <jun.li@nxp.com>
> ---
> change for v2:
> - Add one patch to sync queue work before driver remove.
> - improve git commit log
> - use a variable of usb_role to set target role in
>   ptn5150_check_state() to simplify code.
> - use dev_err_probe for usb_role_switch_get() return value.
> 
>  drivers/extcon/Kconfig          |  1 +
>  drivers/extcon/extcon-ptn5150.c | 25 +++++++++++++++++++++++++
>  2 files changed, 26 insertions(+)
> 
> diff --git a/drivers/extcon/Kconfig b/drivers/extcon/Kconfig
> index c69d40ae5619..f3352a81ee7c 100644
> --- a/drivers/extcon/Kconfig
> +++ b/drivers/extcon/Kconfig
> @@ -130,6 +130,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

This is still being discussed. Rest looks good.

Best regards,
Krzysztof

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

end of thread, other threads:[~2022-04-19  8:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-19  7:43 [PATCH v2 1/2] extcon: ptn5150: add queue work sync before driver release Li Jun
2022-04-19  7:43 ` [PATCH v2 2/2] extcon: ptn5150: Add usb role class support Li Jun
2022-04-19  8:35   ` Krzysztof Kozlowski
2022-04-19  8:32 ` [PATCH v2 1/2] extcon: ptn5150: add queue work sync before driver release Krzysztof Kozlowski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).