2017-03-20 08:01:04

by Baolin Wang

[permalink] [raw]
Subject: [PATCH 1/2] extcon: usb-gpio: Add level trigger support

Now extcon-usb-gpio only supports for GPIO egdge trigger, but VBUS/ID
gpios' detection can be triggered by the level trigger on some platforms.
Thus intoduce one property 'extcon-gpio,level-trigger' to identify this
situation.

Signed-off-by: Baolin Wang <[email protected]>
---
.../devicetree/bindings/extcon/extcon-usb-gpio.txt | 3 +++
1 file changed, 3 insertions(+)

diff --git a/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt b/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt
index dfc14f7..191504b 100644
--- a/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt
+++ b/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt
@@ -9,6 +9,9 @@ Required properties:
Either one of id-gpio or vbus-gpio must be present. Both can be present as well.
- id-gpio: gpio for USB ID pin. See gpio binding.
- vbus-gpio: gpio for USB VBUS pin.
+- extcon-gpio,level-trigger: Boolean, set this gpio's interrupt flag to
+level trigger. If not specified defaults to false, gpio's interrupt flag
+defaults to edge trigger.

Example: Examples of extcon-usb-gpio node in dra7-evm.dts as listed below:
extcon_usb1 {
--
1.7.9.5


2017-03-20 08:01:07

by Baolin Wang

[permalink] [raw]
Subject: [PATCH 2/2] extcon: usb-gpio: Add the GPIO level trigger support

GPIOs may need level trigger to detect VBUS/ID on some platforms, thus
we should add GPIO level trigger to support this situation.

Signed-off-by: Baolin Wang <[email protected]>
---
drivers/extcon/extcon-usb-gpio.c | 81 ++++++++++++++++++++++++++++++++++++--
1 file changed, 77 insertions(+), 4 deletions(-)

diff --git a/drivers/extcon/extcon-usb-gpio.c b/drivers/extcon/extcon-usb-gpio.c
index a5e1882..cc89e25 100644
--- a/drivers/extcon/extcon-usb-gpio.c
+++ b/drivers/extcon/extcon-usb-gpio.c
@@ -39,6 +39,8 @@ struct usb_extcon_info {
struct gpio_desc *vbus_gpiod;
int id_irq;
int vbus_irq;
+ int level_trigger;
+ int trigger_irq;

unsigned long debounce_jiffies;
struct delayed_work wq_detcable;
@@ -70,6 +72,7 @@ struct usb_extcon_info {
static void usb_extcon_detect_cable(struct work_struct *work)
{
int id, vbus;
+ unsigned int trigger;
struct usb_extcon_info *info = container_of(to_delayed_work(work),
struct usb_extcon_info,
wq_detcable);
@@ -80,6 +83,53 @@ static void usb_extcon_detect_cable(struct work_struct *work)
vbus = info->vbus_gpiod ?
gpiod_get_value_cansleep(info->vbus_gpiod) : id;

+ if (info->level_trigger) {
+ if (info->trigger_irq == info->id_irq && info->id_gpiod) {
+ trigger = irqd_get_trigger_type(
+ irq_get_irq_data(info->id_irq));
+
+ /* Ignore incorrect ID trigger */
+ if (((trigger & IRQF_TRIGGER_LOW) && (id > 0)) ||
+ ((trigger & IRQF_TRIGGER_HIGH) && (id == 0))) {
+ enable_irq(info->id_irq);
+ return;
+ }
+
+ if (id) {
+ trigger &= ~IRQF_TRIGGER_HIGH;
+ trigger |= IRQF_TRIGGER_LOW;
+ } else {
+ trigger &= ~IRQF_TRIGGER_LOW;
+ trigger |= IRQF_TRIGGER_HIGH;
+ }
+
+ irq_set_irq_type(info->id_irq, trigger);
+ enable_irq(info->id_irq);
+ } else if (info->trigger_irq == info->vbus_irq &&
+ info->vbus_gpiod) {
+ trigger = irqd_get_trigger_type(
+ irq_get_irq_data(info->vbus_irq));
+
+ /* Ignore incorrect VBUS trigger */
+ if (((trigger & IRQF_TRIGGER_LOW) && (vbus > 0)) ||
+ ((trigger & IRQF_TRIGGER_HIGH) && (vbus == 0))) {
+ enable_irq(info->vbus_irq);
+ return;
+ }
+
+ if (vbus) {
+ trigger &= ~IRQF_TRIGGER_HIGH;
+ trigger |= IRQF_TRIGGER_LOW;
+ } else {
+ trigger &= ~IRQF_TRIGGER_LOW;
+ trigger |= IRQF_TRIGGER_HIGH;
+ }
+
+ irq_set_irq_type(info->vbus_irq, trigger);
+ enable_irq(info->vbus_irq);
+ }
+ }
+
/* at first we clean states which are no longer active */
if (id)
extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false);
@@ -98,6 +148,11 @@ static irqreturn_t usb_irq_handler(int irq, void *dev_id)
{
struct usb_extcon_info *info = dev_id;

+ if (info->level_trigger) {
+ info->trigger_irq = irq;
+ disable_irq_nosync(irq);
+ }
+
queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
info->debounce_jiffies);

@@ -109,7 +164,9 @@ static int usb_extcon_probe(struct platform_device *pdev)
struct device *dev = &pdev->dev;
struct device_node *np = dev->of_node;
struct usb_extcon_info *info;
+ unsigned long id_irqflags, vbus_irqflags;
int ret;
+ int id_active_low, vbus_active_low;

if (!np && !ACPI_HANDLE(dev))
return -EINVAL;
@@ -134,6 +191,14 @@ static int usb_extcon_probe(struct platform_device *pdev)
if (IS_ERR(info->vbus_gpiod))
return PTR_ERR(info->vbus_gpiod);

+ info->level_trigger = of_property_read_bool(np,
+ "extcon-gpio,level-trigger");
+ info->trigger_irq = -1;
+ id_active_low = info->id_gpiod ?
+ gpiod_is_active_low(info->id_gpiod) : 0;
+ vbus_active_low = info->vbus_gpiod ?
+ gpiod_is_active_low(info->vbus_gpiod) : 0;
+
info->edev = devm_extcon_dev_allocate(dev, usb_extcon_cable);
if (IS_ERR(info->edev)) {
dev_err(dev, "failed to allocate extcon device\n");
@@ -158,6 +223,16 @@ static int usb_extcon_probe(struct platform_device *pdev)

INIT_DELAYED_WORK(&info->wq_detcable, usb_extcon_detect_cable);

+ if (info->level_trigger) {
+ id_irqflags = id_active_low ?
+ IRQF_TRIGGER_HIGH : IRQF_TRIGGER_LOW;
+ vbus_irqflags = vbus_active_low ?
+ IRQF_TRIGGER_LOW : IRQF_TRIGGER_HIGH;
+ } else {
+ id_irqflags = vbus_irqflags = IRQF_TRIGGER_RISING |
+ IRQF_TRIGGER_FALLING;
+ }
+
if (info->id_gpiod) {
info->id_irq = gpiod_to_irq(info->id_gpiod);
if (info->id_irq < 0) {
@@ -167,8 +242,7 @@ static int usb_extcon_probe(struct platform_device *pdev)

ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
usb_irq_handler,
- IRQF_TRIGGER_RISING |
- IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
+ id_irqflags | IRQF_ONESHOT,
pdev->name, info);
if (ret < 0) {
dev_err(dev, "failed to request handler for ID IRQ\n");
@@ -185,8 +259,7 @@ static int usb_extcon_probe(struct platform_device *pdev)

ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL,
usb_irq_handler,
- IRQF_TRIGGER_RISING |
- IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
+ vbus_irqflags | IRQF_ONESHOT,
pdev->name, info);
if (ret < 0) {
dev_err(dev, "failed to request handler for VBUS IRQ\n");
--
1.7.9.5

2017-03-24 11:08:47

by Chanwoo Choi

[permalink] [raw]
Subject: Re: [PATCH 1/2] extcon: usb-gpio: Add level trigger support

Hi,

On 2017년 03월 20일 16:59, Baolin Wang wrote:
> Now extcon-usb-gpio only supports for GPIO egdge trigger, but VBUS/ID
> gpios' detection can be triggered by the level trigger on some platforms.
> Thus intoduce one property 'extcon-gpio,level-trigger' to identify this
> situation.
>
> Signed-off-by: Baolin Wang <[email protected]>
> ---
> .../devicetree/bindings/extcon/extcon-usb-gpio.txt | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt b/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt
> index dfc14f7..191504b 100644
> --- a/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt
> +++ b/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt
> @@ -9,6 +9,9 @@ Required properties:
> Either one of id-gpio or vbus-gpio must be present. Both can be present as well.
> - id-gpio: gpio for USB ID pin. See gpio binding.
> - vbus-gpio: gpio for USB VBUS pin.
> +- extcon-gpio,level-trigger: Boolean, set this gpio's interrupt flag to

It is 'extcon-usb-gpio' instead of 'extcon-gpio'.

> +level trigger. If not specified defaults to false, gpio's interrupt flag
> +defaults to edge trigger.

I understand why you need the new flag.
But, I'm not sure it is right way to add the new flag for interrupt flag.

IMHO, I think that we need to find the more proper way to get the interrupt flag
or maybe, we may need to implement the new helper api to get the interrupt
flag for gpio pin when we use the gpio as the interrupt source,

>
> Example: Examples of extcon-usb-gpio node in dra7-evm.dts as listed below:
> extcon_usb1 {
>


--
Best Regards,
Chanwoo Choi
Samsung Electronics

2017-03-24 11:56:25

by Baolin Wang

[permalink] [raw]
Subject: Re: [PATCH 1/2] extcon: usb-gpio: Add level trigger support

Hi,

On 24 March 2017 at 19:08, Chanwoo Choi <[email protected]> wrote:
> Hi,
>
> On 2017년 03월 20일 16:59, Baolin Wang wrote:
>> Now extcon-usb-gpio only supports for GPIO egdge trigger, but VBUS/ID
>> gpios' detection can be triggered by the level trigger on some platforms.
>> Thus intoduce one property 'extcon-gpio,level-trigger' to identify this
>> situation.
>>
>> Signed-off-by: Baolin Wang <[email protected]>
>> ---
>> .../devicetree/bindings/extcon/extcon-usb-gpio.txt | 3 +++
>> 1 file changed, 3 insertions(+)
>>
>> diff --git a/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt b/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt
>> index dfc14f7..191504b 100644
>> --- a/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt
>> +++ b/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt
>> @@ -9,6 +9,9 @@ Required properties:
>> Either one of id-gpio or vbus-gpio must be present. Both can be present as well.
>> - id-gpio: gpio for USB ID pin. See gpio binding.
>> - vbus-gpio: gpio for USB VBUS pin.
>> +- extcon-gpio,level-trigger: Boolean, set this gpio's interrupt flag to
>
> It is 'extcon-usb-gpio' instead of 'extcon-gpio'.

Sure.

>
>> +level trigger. If not specified defaults to false, gpio's interrupt flag
>> +defaults to edge trigger.
>
> I understand why you need the new flag.
> But, I'm not sure it is right way to add the new flag for interrupt flag.
>
> IMHO, I think that we need to find the more proper way to get the interrupt flag
> or maybe, we may need to implement the new helper api to get the interrupt
> flag for gpio pin when we use the gpio as the interrupt source,

Yes, I agree with you. We already have helper functions to get
interrupt flag from device tree, but we can not get the irq number of
GPIO to configure in device tree, then I introduce one new flag to
indicate the trigger type. But I like to change the patch if there are
any good suggestion. Thanks.

--
Baolin.wang
Best Regards

2017-03-24 12:41:44

by Rob Herring

[permalink] [raw]
Subject: Re: [PATCH 1/2] extcon: usb-gpio: Add level trigger support

On Fri, Mar 24, 2017 at 6:56 AM, Baolin Wang <[email protected]> wrote:
> Hi,
>
> On 24 March 2017 at 19:08, Chanwoo Choi <[email protected]> wrote:
>> Hi,
>>
>> On 2017년 03월 20일 16:59, Baolin Wang wrote:
>>> Now extcon-usb-gpio only supports for GPIO egdge trigger, but VBUS/ID
>>> gpios' detection can be triggered by the level trigger on some platforms.
>>> Thus intoduce one property 'extcon-gpio,level-trigger' to identify this
>>> situation.
>>>
>>> Signed-off-by: Baolin Wang <[email protected]>
>>> ---
>>> .../devicetree/bindings/extcon/extcon-usb-gpio.txt | 3 +++
>>> 1 file changed, 3 insertions(+)
>>>
>>> diff --git a/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt b/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt
>>> index dfc14f7..191504b 100644
>>> --- a/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt
>>> +++ b/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt
>>> @@ -9,6 +9,9 @@ Required properties:
>>> Either one of id-gpio or vbus-gpio must be present. Both can be present as well.
>>> - id-gpio: gpio for USB ID pin. See gpio binding.
>>> - vbus-gpio: gpio for USB VBUS pin.
>>> +- extcon-gpio,level-trigger: Boolean, set this gpio's interrupt flag to
>>
>> It is 'extcon-usb-gpio' instead of 'extcon-gpio'.
>
> Sure.

"extcon-gpio" is not a vendor, so don't make it a vendor prefix.

>>> +level trigger. If not specified defaults to false, gpio's interrupt flag
>>> +defaults to edge trigger.
>>
>> I understand why you need the new flag.
>> But, I'm not sure it is right way to add the new flag for interrupt flag.
>>
>> IMHO, I think that we need to find the more proper way to get the interrupt flag
>> or maybe, we may need to implement the new helper api to get the interrupt
>> flag for gpio pin when we use the gpio as the interrupt source,
>
> Yes, I agree with you. We already have helper functions to get
> interrupt flag from device tree, but we can not get the irq number of
> GPIO to configure in device tree, then I introduce one new flag to
> indicate the trigger type. But I like to change the patch if there are
> any good suggestion. Thanks.

The extcon binding needs an overhaul in general. I'm not going to take
extensions to a broken binding. What's needed is a USB connector
binding like we have for HDMI and other video connectors. And for
USB-C, those need to be combined.

Rob

2017-03-29 06:08:30

by Baolin Wang

[permalink] [raw]
Subject: Re: [PATCH 1/2] extcon: usb-gpio: Add level trigger support

Hi,

On 24 March 2017 at 20:41, Rob Herring <[email protected]> wrote:
> On Fri, Mar 24, 2017 at 6:56 AM, Baolin Wang <[email protected]> wrote:
>> Hi,
>>
>> On 24 March 2017 at 19:08, Chanwoo Choi <[email protected]> wrote:
>>> Hi,
>>>
>>> On 2017년 03월 20일 16:59, Baolin Wang wrote:
>>>> Now extcon-usb-gpio only supports for GPIO egdge trigger, but VBUS/ID
>>>> gpios' detection can be triggered by the level trigger on some platforms.
>>>> Thus intoduce one property 'extcon-gpio,level-trigger' to identify this
>>>> situation.
>>>>
>>>> Signed-off-by: Baolin Wang <[email protected]>
>>>> ---
>>>> .../devicetree/bindings/extcon/extcon-usb-gpio.txt | 3 +++
>>>> 1 file changed, 3 insertions(+)
>>>>
>>>> diff --git a/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt b/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt
>>>> index dfc14f7..191504b 100644
>>>> --- a/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt
>>>> +++ b/Documentation/devicetree/bindings/extcon/extcon-usb-gpio.txt
>>>> @@ -9,6 +9,9 @@ Required properties:
>>>> Either one of id-gpio or vbus-gpio must be present. Both can be present as well.
>>>> - id-gpio: gpio for USB ID pin. See gpio binding.
>>>> - vbus-gpio: gpio for USB VBUS pin.
>>>> +- extcon-gpio,level-trigger: Boolean, set this gpio's interrupt flag to
>>>
>>> It is 'extcon-usb-gpio' instead of 'extcon-gpio'.
>>
>> Sure.
>
> "extcon-gpio" is not a vendor, so don't make it a vendor prefix.

OK.

>
>>>> +level trigger. If not specified defaults to false, gpio's interrupt flag
>>>> +defaults to edge trigger.
>>>
>>> I understand why you need the new flag.
>>> But, I'm not sure it is right way to add the new flag for interrupt flag.
>>>
>>> IMHO, I think that we need to find the more proper way to get the interrupt flag
>>> or maybe, we may need to implement the new helper api to get the interrupt
>>> flag for gpio pin when we use the gpio as the interrupt source,
>>
>> Yes, I agree with you. We already have helper functions to get
>> interrupt flag from device tree, but we can not get the irq number of
>> GPIO to configure in device tree, then I introduce one new flag to
>> indicate the trigger type. But I like to change the patch if there are
>> any good suggestion. Thanks.
>
> The extcon binding needs an overhaul in general. I'm not going to take
> extensions to a broken binding. What's needed is a USB connector
> binding like we have for HDMI and other video connectors. And for
> USB-C, those need to be combined.

I am not sure I understand your points correctly, could you elaborate
on how to modify for extcon? Thanks.

--
Baolin.wang
Best Regards