2022-04-06 15:20:29

by Jun Li

[permalink] [raw]
Subject: [PATCH] extcon: ptn5150: add usb role class support

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

Signed-off-by: Li Jun <[email protected]>
---
drivers/extcon/Kconfig | 1 +
drivers/extcon/extcon-ptn5150.c | 39 +++++++++++++++++++++++++++++++++
2 files changed, 40 insertions(+)

diff --git a/drivers/extcon/Kconfig b/drivers/extcon/Kconfig
index 0d42e49105dd..9828ade787a8 100644
--- a/drivers/extcon/Kconfig
+++ b/drivers/extcon/Kconfig
@@ -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.
diff --git a/drivers/extcon/extcon-ptn5150.c b/drivers/extcon/extcon-ptn5150.c
index 5b9a3cf8df26..821b0f390308 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 */
@@ -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
--
2.25.1


2022-04-06 15:38:49

by Jun Li

[permalink] [raw]
Subject: RE: [PATCH] extcon: ptn5150: add usb role class support



> -----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
>
> On 06/04/2022 11:42, Li Jun wrote:
> > Add support of usb role class consumer to do role switch.
>
> Please mention also why you are doing this.

Will improve in v2.

>
> >
> > Signed-off-by: Li Jun <[email protected]>
> > ---
> > drivers/extcon/Kconfig | 1 +
> > drivers/extcon/extcon-ptn5150.c | 39 +++++++++++++++++++++++++++++++++
> > 2 files changed, 40 insertions(+)
> >
> > diff --git a/drivers/extcon/Kconfig b/drivers/extcon/Kconfig
> > index 0d42e49105dd..9828ade787a8 100644
> > --- a/drivers/extcon/Kconfig
> > +++ b/drivers/extcon/Kconfig
> > @@ -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.

>
> > 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 5b9a3cf8df26..821b0f390308 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 */
> > @@ -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);
> > +
>
>
> 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.

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

>
> > 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);
>
> 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

>
> > + usb_role_switch_put(info->role_sw);
> > +}
> > +
>
>
>
> Best regards,
> Krzysztof

2022-04-06 15:53:29

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH] extcon: ptn5150: add usb role class support

On 06/04/2022 11:42, Li Jun wrote:
> Add support of usb role class consumer to do role switch.

Please mention also why you are doing this.

>
> Signed-off-by: Li Jun <[email protected]>
> ---
> drivers/extcon/Kconfig | 1 +
> drivers/extcon/extcon-ptn5150.c | 39 +++++++++++++++++++++++++++++++++
> 2 files changed, 40 insertions(+)
>
> diff --git a/drivers/extcon/Kconfig b/drivers/extcon/Kconfig
> index 0d42e49105dd..9828ade787a8 100644
> --- a/drivers/extcon/Kconfig
> +++ b/drivers/extcon/Kconfig
> @@ -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.

> 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 5b9a3cf8df26..821b0f390308 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 */
> @@ -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);
> +


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.

> 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);

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

> + usb_role_switch_put(info->role_sw);
> +}
> +



Best regards,
Krzysztof

2022-04-06 21:20:58

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] extcon: ptn5150: add usb role class support

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
Reported-by: kernel test robot <[email protected]>

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

--
0-DAY CI Kernel Test Service
https://01.org/lkp

2022-04-07 07:16:50

by Chanwoo Choi

[permalink] [raw]
Subject: Re: [PATCH] extcon: ptn5150: add usb role class support

Hi,

On 4/6/22 6:42 PM, Li Jun wrote:
> Add support of usb role class consumer to do role switch.
>
> Signed-off-by: Li Jun <[email protected]>
> ---
> drivers/extcon/Kconfig | 1 +
> drivers/extcon/extcon-ptn5150.c | 39 +++++++++++++++++++++++++++++++++
> 2 files changed, 40 insertions(+)
>
> diff --git a/drivers/extcon/Kconfig b/drivers/extcon/Kconfig
> index 0d42e49105dd..9828ade787a8 100644
> --- a/drivers/extcon/Kconfig
> +++ b/drivers/extcon/Kconfig
> @@ -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.
> diff --git a/drivers/extcon/extcon-ptn5150.c b/drivers/extcon/extcon-ptn5150.c
> index 5b9a3cf8df26..821b0f390308 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 */
> @@ -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 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.

> +
> + 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

2022-04-19 14:19:48

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH] extcon: ptn5150: add usb role class support

On Tue, 19 Apr 2022 at 09:53, Jun Li <[email protected]> wrote:
> > > > @@ -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.

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

Best regards,
Krzysztof

2022-04-19 17:58:50

by Jun Li

[permalink] [raw]
Subject: RE: [PATCH] extcon: ptn5150: add usb role class support

Hi

> -----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,
>
> On 4/6/22 6:42 PM, Li Jun wrote:
> > Add support of usb role class consumer to do role switch.
> >
> > Signed-off-by: Li Jun <[email protected]>
> > ---
> > drivers/extcon/Kconfig | 1 +
> > drivers/extcon/extcon-ptn5150.c | 39
> > +++++++++++++++++++++++++++++++++
> > 2 files changed, 40 insertions(+)
> >
> > diff --git a/drivers/extcon/Kconfig b/drivers/extcon/Kconfig index
> > 0d42e49105dd..9828ade787a8 100644
> > --- a/drivers/extcon/Kconfig
> > +++ b/drivers/extcon/Kconfig
> > @@ -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.
> > diff --git a/drivers/extcon/extcon-ptn5150.c
> > b/drivers/extcon/extcon-ptn5150.c index 5b9a3cf8df26..821b0f390308
> > 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 */
> > @@ -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 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
>
> > +
> > + 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

2022-04-19 23:56:32

by Jun Li

[permalink] [raw]
Subject: RE: [PATCH] extcon: ptn5150: add usb role class support



> -----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
>
> On 19/04/2022 10:23, Jun Li wrote:
> >
> >
> >> -----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
> >>
> >> On Tue, 19 Apr 2022 at 09:53, Jun Li <[email protected]> wrote:
> >>>>>> @@ -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.
> >>
> >> 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':
> > 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

Signed-off-by: Stephen Rothwell <[email protected]>

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
>
> Best regards,
> Krzysztof

2022-04-20 00:15:05

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH] extcon: ptn5150: add usb role class support

On 19/04/2022 10:23, Jun Li wrote:
>
>
>> -----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
>>
>> On Tue, 19 Apr 2022 at 09:53, Jun Li <[email protected]> wrote:
>>>>>> @@ -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.
>>
>> 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

2022-04-20 06:27:47

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH] extcon: ptn5150: add usb role class support

On 19/04/2022 10:51, Jun Li wrote:
> 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

2022-04-20 08:46:40

by Jun Li

[permalink] [raw]
Subject: RE: [PATCH] extcon: ptn5150: add usb role class support



> -----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
>
> On Tue, 19 Apr 2022 at 09:53, Jun Li <[email protected]> wrote:
> > > > > @@ -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.
>
> 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

>
> Best regards,
> Krzysztof

2022-04-22 07:40:59

by Jun Li

[permalink] [raw]
Subject: RE: [PATCH] extcon: ptn5150: add usb role class support



> -----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
>
> On 19/04/2022 11:12, Krzysztof Kozlowski wrote:
> > On 19/04/2022 10:51, Jun Li wrote:
> >> 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. :)
>
> 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
>
> Best regards,
> Krzysztof

2022-04-22 21:23:28

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH] extcon: ptn5150: add usb role class support

On 19/04/2022 11:12, Krzysztof Kozlowski wrote:
> On 19/04/2022 10:51, Jun Li wrote:
>> 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. :)

Use:
depends on USB_ROLE_SWITCH || !USB_ROLE_SWITCH'

this should solve the problem.

Best regards,
Krzysztof

2022-04-22 22:42:35

by Jun Li

[permalink] [raw]
Subject: RE: [PATCH] extcon: ptn5150: add usb role class support



> -----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
>
>
>
> > -----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
> >
> > On 06/04/2022 11:42, Li Jun wrote:
> > > Add support of usb role class consumer to do role switch.
> >
> > Please mention also why you are doing this.
>
> Will improve in v2.
>
> >
> > >
> > > Signed-off-by: Li Jun <[email protected]>
> > > ---
> > > drivers/extcon/Kconfig | 1 +
> > > drivers/extcon/extcon-ptn5150.c | 39
> > > +++++++++++++++++++++++++++++++++
> > > 2 files changed, 40 insertions(+)
> > >
> > > diff --git a/drivers/extcon/Kconfig b/drivers/extcon/Kconfig index
> > > 0d42e49105dd..9828ade787a8 100644
> > > --- a/drivers/extcon/Kconfig
> > > +++ b/drivers/extcon/Kconfig
> > > @@ -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.

Li Jun