diff mbox series

[RESEND,v6,3/3] extcon: qcom-spmi: Add support for VBUS detection

Message ID 683693bdfaa14a72550f466da8b26a6126317c4d.1611621365.git.gurus@codeaurora.org
State New
Headers show
Series Add support for VBUS detection | expand

Commit Message

Guru Das Srinagesh Jan. 26, 2021, 12:38 a.m. UTC
From: Anirudh Ghayal <aghayal@codeaurora.org>

VBUS can be detected via a dedicated PMIC pin. Add support
for reporting the VBUS status.

Signed-off-by: Anirudh Ghayal <aghayal@codeaurora.org>
Signed-off-by: Kavya Nunna <knunna@codeaurora.org>
Signed-off-by: Guru Das Srinagesh <gurus@codeaurora.org>
---
 drivers/extcon/extcon-qcom-spmi-misc.c | 99 +++++++++++++++++++++++++++-------
 1 file changed, 80 insertions(+), 19 deletions(-)

Comments

Chanwoo Choi Feb. 20, 2021, 6:34 a.m. UTC | #1
Hi,

Please use get_maintainer.pl script when you send the patches.
I didn't receive the patches.  I'll review.

Thanks,
Chanwoo Choi

On 21. 1. 26. 오전 9:38, Guru Das Srinagesh wrote:
> From: Anirudh Ghayal <aghayal@codeaurora.org>
> 
> VBUS can be detected via a dedicated PMIC pin. Add support
> for reporting the VBUS status.
> 
> Signed-off-by: Anirudh Ghayal <aghayal@codeaurora.org>
> Signed-off-by: Kavya Nunna <knunna@codeaurora.org>
> Signed-off-by: Guru Das Srinagesh <gurus@codeaurora.org>
> ---
>   drivers/extcon/extcon-qcom-spmi-misc.c | 99 +++++++++++++++++++++++++++-------
>   1 file changed, 80 insertions(+), 19 deletions(-)
> 
> diff --git a/drivers/extcon/extcon-qcom-spmi-misc.c b/drivers/extcon/extcon-qcom-spmi-misc.c
> index 6b836ae..9e8ccfb 100644
> --- a/drivers/extcon/extcon-qcom-spmi-misc.c
> +++ b/drivers/extcon/extcon-qcom-spmi-misc.c
> @@ -1,7 +1,7 @@
>   // SPDX-License-Identifier: GPL-2.0-only
>   /**
>    * extcon-qcom-spmi-misc.c - Qualcomm USB extcon driver to support USB ID
> - *				detection based on extcon-usb-gpio.c.
> + *			and VBUS detection based on extcon-usb-gpio.c.
>    *
>    * Copyright (C) 2016 Linaro, Ltd.
>    * Stephen Boyd <stephen.boyd@linaro.org>
> @@ -21,30 +21,56 @@
>   
>   struct qcom_usb_extcon_info {
>   	struct extcon_dev *edev;
> -	int irq;
> +	int id_irq;
> +	int vbus_irq;
>   	struct delayed_work wq_detcable;
>   	unsigned long debounce_jiffies;
>   };
>   
>   static const unsigned int qcom_usb_extcon_cable[] = {
> +	EXTCON_USB,
>   	EXTCON_USB_HOST,
>   	EXTCON_NONE,
>   };
>   
>   static void qcom_usb_extcon_detect_cable(struct work_struct *work)
>   {
> -	bool id;
> +	bool state = false;
>   	int ret;
> +	union extcon_property_value val;
>   	struct qcom_usb_extcon_info *info = container_of(to_delayed_work(work),
>   						    struct qcom_usb_extcon_info,
>   						    wq_detcable);
>   
> -	/* check ID and update cable state */
> -	ret = irq_get_irqchip_state(info->irq, IRQCHIP_STATE_LINE_LEVEL, &id);
> -	if (ret)
> -		return;
> +	if (info->id_irq > 0) {
> +		/* check ID and update cable state */
> +		ret = irq_get_irqchip_state(info->id_irq,
> +				IRQCHIP_STATE_LINE_LEVEL, &state);
> +		if (ret)
> +			return;
> +
> +		if (!state) {
> +			val.intval = true;
> +			extcon_set_property(info->edev, EXTCON_USB_HOST,
> +						EXTCON_PROP_USB_SS, val);
> +		}
> +		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, !state);
> +	}
>   
> -	extcon_set_state_sync(info->edev, EXTCON_USB_HOST, !id);
> +	if (info->vbus_irq > 0) {
> +		/* check VBUS and update cable state */
> +		ret = irq_get_irqchip_state(info->vbus_irq,
> +				IRQCHIP_STATE_LINE_LEVEL, &state);
> +		if (ret)
> +			return;
> +
> +		if (state) {
> +			val.intval = true;
> +			extcon_set_property(info->edev, EXTCON_USB,
> +						EXTCON_PROP_USB_SS, val);
> +		}
> +		extcon_set_state_sync(info->edev, EXTCON_USB, state);
> +	}
>   }
>   
>   static irqreturn_t qcom_usb_irq_handler(int irq, void *dev_id)
> @@ -79,21 +105,48 @@ static int qcom_usb_extcon_probe(struct platform_device *pdev)
>   		return ret;
>   	}
>   
> +	ret = extcon_set_property_capability(info->edev,
> +			EXTCON_USB, EXTCON_PROP_USB_SS);
> +	ret |= extcon_set_property_capability(info->edev,
> +			EXTCON_USB_HOST, EXTCON_PROP_USB_SS);
> +	if (ret) {
> +		dev_err(dev, "failed to register extcon props rc=%d\n",
> +						ret);
> +		return ret;
> +	}
> +
>   	info->debounce_jiffies = msecs_to_jiffies(USB_ID_DEBOUNCE_MS);
>   	INIT_DELAYED_WORK(&info->wq_detcable, qcom_usb_extcon_detect_cable);
>   
> -	info->irq = platform_get_irq_byname(pdev, "usb_id");
> -	if (info->irq < 0)
> -		return info->irq;
> +	info->id_irq = platform_get_irq_byname(pdev, "usb_id");
> +	if (info->id_irq > 0) {
> +		ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
> +					qcom_usb_irq_handler,
> +					IRQF_TRIGGER_RISING |
> +					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
> +					pdev->name, info);
> +		if (ret < 0) {
> +			dev_err(dev, "failed to request handler for ID IRQ\n");
> +			return ret;
> +		}
> +	}
>   
> -	ret = devm_request_threaded_irq(dev, info->irq, NULL,
> +	info->vbus_irq = platform_get_irq_byname(pdev, "usb_vbus");
> +	if (info->vbus_irq > 0) {
> +		ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL,
>   					qcom_usb_irq_handler,
>   					IRQF_TRIGGER_RISING |
>   					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
>   					pdev->name, info);
> -	if (ret < 0) {
> -		dev_err(dev, "failed to request handler for ID IRQ\n");
> -		return ret;
> +		if (ret < 0) {
> +			dev_err(dev, "failed to request handler for VBUS IRQ\n");
> +			return ret;
> +		}
> +	}
> +
> +	if (info->id_irq < 0 && info->vbus_irq < 0) {
> +		dev_err(dev, "ID and VBUS IRQ not found\n");
> +		return -EINVAL;
>   	}
>   
>   	platform_set_drvdata(pdev, info);
> @@ -120,8 +173,12 @@ static int qcom_usb_extcon_suspend(struct device *dev)
>   	struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);
>   	int ret = 0;
>   
> -	if (device_may_wakeup(dev))
> -		ret = enable_irq_wake(info->irq);
> +	if (device_may_wakeup(dev)) {
> +		if (info->id_irq > 0)
> +			ret = enable_irq_wake(info->id_irq);
> +		if (info->vbus_irq > 0)
> +			ret = enable_irq_wake(info->vbus_irq);
> +	}
>   
>   	return ret;
>   }
> @@ -131,8 +188,12 @@ static int qcom_usb_extcon_resume(struct device *dev)
>   	struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);
>   	int ret = 0;
>   
> -	if (device_may_wakeup(dev))
> -		ret = disable_irq_wake(info->irq);
> +	if (device_may_wakeup(dev)) {
> +		if (info->id_irq > 0)
> +			ret = disable_irq_wake(info->id_irq);
> +		if (info->vbus_irq > 0)
> +			ret = disable_irq_wake(info->vbus_irq);
> +	}
>   
>   	return ret;
>   }
>
Chanwoo Choi Feb. 24, 2021, 4:54 p.m. UTC | #2
Hi,

On 21. 1. 26. 오전 9:38, Guru Das Srinagesh wrote:
> From: Anirudh Ghayal <aghayal@codeaurora.org>

> 

> VBUS can be detected via a dedicated PMIC pin. Add support

> for reporting the VBUS status.

> 

> Signed-off-by: Anirudh Ghayal <aghayal@codeaurora.org>

> Signed-off-by: Kavya Nunna <knunna@codeaurora.org>

> Signed-off-by: Guru Das Srinagesh <gurus@codeaurora.org>

> ---

>   drivers/extcon/extcon-qcom-spmi-misc.c | 99 +++++++++++++++++++++++++++-------

>   1 file changed, 80 insertions(+), 19 deletions(-)

> 

> diff --git a/drivers/extcon/extcon-qcom-spmi-misc.c b/drivers/extcon/extcon-qcom-spmi-misc.c

> index 6b836ae..9e8ccfb 100644

> --- a/drivers/extcon/extcon-qcom-spmi-misc.c

> +++ b/drivers/extcon/extcon-qcom-spmi-misc.c

> @@ -1,7 +1,7 @@

>   // SPDX-License-Identifier: GPL-2.0-only

>   /**

>    * extcon-qcom-spmi-misc.c - Qualcomm USB extcon driver to support USB ID

> - *				detection based on extcon-usb-gpio.c.

> + *			and VBUS detection based on extcon-usb-gpio.c.

>    *

>    * Copyright (C) 2016 Linaro, Ltd.

>    * Stephen Boyd <stephen.boyd@linaro.org>

> @@ -21,30 +21,56 @@

>   

>   struct qcom_usb_extcon_info {

>   	struct extcon_dev *edev;

> -	int irq;

> +	int id_irq;

> +	int vbus_irq;

>   	struct delayed_work wq_detcable;

>   	unsigned long debounce_jiffies;

>   };

>   

>   static const unsigned int qcom_usb_extcon_cable[] = {

> +	EXTCON_USB,

>   	EXTCON_USB_HOST,

>   	EXTCON_NONE,

>   };

>   

>   static void qcom_usb_extcon_detect_cable(struct work_struct *work)

>   {

> -	bool id;

> +	bool state = false;

>   	int ret;

> +	union extcon_property_value val;

>   	struct qcom_usb_extcon_info *info = container_of(to_delayed_work(work),

>   						    struct qcom_usb_extcon_info,

>   						    wq_detcable);

>   

> -	/* check ID and update cable state */

> -	ret = irq_get_irqchip_state(info->irq, IRQCHIP_STATE_LINE_LEVEL, &id);

> -	if (ret)

> -		return;

> +	if (info->id_irq > 0) {

> +		/* check ID and update cable state */

> +		ret = irq_get_irqchip_state(info->id_irq,

> +				IRQCHIP_STATE_LINE_LEVEL, &state);

> +		if (ret)

> +			return;

> +

> +		if (!state) {

> +			val.intval = true;

> +			extcon_set_property(info->edev, EXTCON_USB_HOST,

> +						EXTCON_PROP_USB_SS, val);

> +		}

> +		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, !state);

> +	}

>   

> -	extcon_set_state_sync(info->edev, EXTCON_USB_HOST, !id);

> +	if (info->vbus_irq > 0) {

> +		/* check VBUS and update cable state */

> +		ret = irq_get_irqchip_state(info->vbus_irq,

> +				IRQCHIP_STATE_LINE_LEVEL, &state);

> +		if (ret)

> +			return;

> +

> +		if (state) {

> +			val.intval = true;

> +			extcon_set_property(info->edev, EXTCON_USB,

> +						EXTCON_PROP_USB_SS, val);

> +		}

> +		extcon_set_state_sync(info->edev, EXTCON_USB, state);

> +	}

>   }

>   

>   static irqreturn_t qcom_usb_irq_handler(int irq, void *dev_id)

> @@ -79,21 +105,48 @@ static int qcom_usb_extcon_probe(struct platform_device *pdev)

>   		return ret;

>   	}

>   

> +	ret = extcon_set_property_capability(info->edev,

> +			EXTCON_USB, EXTCON_PROP_USB_SS);

> +	ret |= extcon_set_property_capability(info->edev,

> +			EXTCON_USB_HOST, EXTCON_PROP_USB_SS);

> +	if (ret) {

> +		dev_err(dev, "failed to register extcon props rc=%d\n",

> +						ret);

> +		return ret;

> +	}

> +

>   	info->debounce_jiffies = msecs_to_jiffies(USB_ID_DEBOUNCE_MS);

>   	INIT_DELAYED_WORK(&info->wq_detcable, qcom_usb_extcon_detect_cable);

>   

> -	info->irq = platform_get_irq_byname(pdev, "usb_id");

> -	if (info->irq < 0)

> -		return info->irq;

> +	info->id_irq = platform_get_irq_byname(pdev, "usb_id");

> +	if (info->id_irq > 0) {

> +		ret = devm_request_threaded_irq(dev, info->id_irq, NULL,

> +					qcom_usb_irq_handler,

> +					IRQF_TRIGGER_RISING |

> +					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,

> +					pdev->name, info);

> +		if (ret < 0) {

> +			dev_err(dev, "failed to request handler for ID IRQ\n");

> +			return ret;

> +		}

> +	}

>   

> -	ret = devm_request_threaded_irq(dev, info->irq, NULL,

> +	info->vbus_irq = platform_get_irq_byname(pdev, "usb_vbus");

> +	if (info->vbus_irq > 0) {

> +		ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL,

>   					qcom_usb_irq_handler,

>   					IRQF_TRIGGER_RISING |

>   					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,

>   					pdev->name, info);

> -	if (ret < 0) {

> -		dev_err(dev, "failed to request handler for ID IRQ\n");

> -		return ret;

> +		if (ret < 0) {

> +			dev_err(dev, "failed to request handler for VBUS IRQ\n");

> +			return ret;

> +		}

> +	}

> +

> +	if (info->id_irq < 0 && info->vbus_irq < 0) {

> +		dev_err(dev, "ID and VBUS IRQ not found\n");

> +		return -EINVAL;

>   	}

>   

>   	platform_set_drvdata(pdev, info);

> @@ -120,8 +173,12 @@ static int qcom_usb_extcon_suspend(struct device *dev)

>   	struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);

>   	int ret = 0;

>   

> -	if (device_may_wakeup(dev))

> -		ret = enable_irq_wake(info->irq);

> +	if (device_may_wakeup(dev)) {

> +		if (info->id_irq > 0)

> +			ret = enable_irq_wake(info->id_irq);

> +		if (info->vbus_irq > 0)

> +			ret = enable_irq_wake(info->vbus_irq);

> +	}

>   

>   	return ret;

>   }

> @@ -131,8 +188,12 @@ static int qcom_usb_extcon_resume(struct device *dev)

>   	struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);

>   	int ret = 0;

>   

> -	if (device_may_wakeup(dev))

> -		ret = disable_irq_wake(info->irq);

> +	if (device_may_wakeup(dev)) {

> +		if (info->id_irq > 0)

> +			ret = disable_irq_wake(info->id_irq);

> +		if (info->vbus_irq > 0)

> +			ret = disable_irq_wake(info->vbus_irq);

> +	}

>   

>   	return ret;

>   }

> 



Looks good to me. Apply it. for v5.13-rc1.

Thanks,
Chanwoo Choi
Guru Das Srinagesh Feb. 24, 2021, 7:50 p.m. UTC | #3
On Sat, Feb 20, 2021 at 03:34:52PM +0900, Chanwoo Choi wrote:
> Hi,
> 
> Please use get_maintainer.pl script when you send the patches.
> I didn't receive the patches.  I'll review.
> 
> Thanks,
> Chanwoo Choi

Thanks for reviewing and accepting the patches, Chanwoo. Sorry, I missed
out on using the get_maintainer.pl script - instead, I sent it to those
listed under "ARM/QUALCOMM SUPPORT" in the MAINTAINERS file as
drivers/extcon/extcon-qcom* is listed in that section.

Thank you.

Guru Das.
diff mbox series

Patch

diff --git a/drivers/extcon/extcon-qcom-spmi-misc.c b/drivers/extcon/extcon-qcom-spmi-misc.c
index 6b836ae..9e8ccfb 100644
--- a/drivers/extcon/extcon-qcom-spmi-misc.c
+++ b/drivers/extcon/extcon-qcom-spmi-misc.c
@@ -1,7 +1,7 @@ 
 // SPDX-License-Identifier: GPL-2.0-only
 /**
  * extcon-qcom-spmi-misc.c - Qualcomm USB extcon driver to support USB ID
- *				detection based on extcon-usb-gpio.c.
+ *			and VBUS detection based on extcon-usb-gpio.c.
  *
  * Copyright (C) 2016 Linaro, Ltd.
  * Stephen Boyd <stephen.boyd@linaro.org>
@@ -21,30 +21,56 @@ 
 
 struct qcom_usb_extcon_info {
 	struct extcon_dev *edev;
-	int irq;
+	int id_irq;
+	int vbus_irq;
 	struct delayed_work wq_detcable;
 	unsigned long debounce_jiffies;
 };
 
 static const unsigned int qcom_usb_extcon_cable[] = {
+	EXTCON_USB,
 	EXTCON_USB_HOST,
 	EXTCON_NONE,
 };
 
 static void qcom_usb_extcon_detect_cable(struct work_struct *work)
 {
-	bool id;
+	bool state = false;
 	int ret;
+	union extcon_property_value val;
 	struct qcom_usb_extcon_info *info = container_of(to_delayed_work(work),
 						    struct qcom_usb_extcon_info,
 						    wq_detcable);
 
-	/* check ID and update cable state */
-	ret = irq_get_irqchip_state(info->irq, IRQCHIP_STATE_LINE_LEVEL, &id);
-	if (ret)
-		return;
+	if (info->id_irq > 0) {
+		/* check ID and update cable state */
+		ret = irq_get_irqchip_state(info->id_irq,
+				IRQCHIP_STATE_LINE_LEVEL, &state);
+		if (ret)
+			return;
+
+		if (!state) {
+			val.intval = true;
+			extcon_set_property(info->edev, EXTCON_USB_HOST,
+						EXTCON_PROP_USB_SS, val);
+		}
+		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, !state);
+	}
 
-	extcon_set_state_sync(info->edev, EXTCON_USB_HOST, !id);
+	if (info->vbus_irq > 0) {
+		/* check VBUS and update cable state */
+		ret = irq_get_irqchip_state(info->vbus_irq,
+				IRQCHIP_STATE_LINE_LEVEL, &state);
+		if (ret)
+			return;
+
+		if (state) {
+			val.intval = true;
+			extcon_set_property(info->edev, EXTCON_USB,
+						EXTCON_PROP_USB_SS, val);
+		}
+		extcon_set_state_sync(info->edev, EXTCON_USB, state);
+	}
 }
 
 static irqreturn_t qcom_usb_irq_handler(int irq, void *dev_id)
@@ -79,21 +105,48 @@  static int qcom_usb_extcon_probe(struct platform_device *pdev)
 		return ret;
 	}
 
+	ret = extcon_set_property_capability(info->edev,
+			EXTCON_USB, EXTCON_PROP_USB_SS);
+	ret |= extcon_set_property_capability(info->edev,
+			EXTCON_USB_HOST, EXTCON_PROP_USB_SS);
+	if (ret) {
+		dev_err(dev, "failed to register extcon props rc=%d\n",
+						ret);
+		return ret;
+	}
+
 	info->debounce_jiffies = msecs_to_jiffies(USB_ID_DEBOUNCE_MS);
 	INIT_DELAYED_WORK(&info->wq_detcable, qcom_usb_extcon_detect_cable);
 
-	info->irq = platform_get_irq_byname(pdev, "usb_id");
-	if (info->irq < 0)
-		return info->irq;
+	info->id_irq = platform_get_irq_byname(pdev, "usb_id");
+	if (info->id_irq > 0) {
+		ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
+					qcom_usb_irq_handler,
+					IRQF_TRIGGER_RISING |
+					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
+					pdev->name, info);
+		if (ret < 0) {
+			dev_err(dev, "failed to request handler for ID IRQ\n");
+			return ret;
+		}
+	}
 
-	ret = devm_request_threaded_irq(dev, info->irq, NULL,
+	info->vbus_irq = platform_get_irq_byname(pdev, "usb_vbus");
+	if (info->vbus_irq > 0) {
+		ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL,
 					qcom_usb_irq_handler,
 					IRQF_TRIGGER_RISING |
 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
 					pdev->name, info);
-	if (ret < 0) {
-		dev_err(dev, "failed to request handler for ID IRQ\n");
-		return ret;
+		if (ret < 0) {
+			dev_err(dev, "failed to request handler for VBUS IRQ\n");
+			return ret;
+		}
+	}
+
+	if (info->id_irq < 0 && info->vbus_irq < 0) {
+		dev_err(dev, "ID and VBUS IRQ not found\n");
+		return -EINVAL;
 	}
 
 	platform_set_drvdata(pdev, info);
@@ -120,8 +173,12 @@  static int qcom_usb_extcon_suspend(struct device *dev)
 	struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);
 	int ret = 0;
 
-	if (device_may_wakeup(dev))
-		ret = enable_irq_wake(info->irq);
+	if (device_may_wakeup(dev)) {
+		if (info->id_irq > 0)
+			ret = enable_irq_wake(info->id_irq);
+		if (info->vbus_irq > 0)
+			ret = enable_irq_wake(info->vbus_irq);
+	}
 
 	return ret;
 }
@@ -131,8 +188,12 @@  static int qcom_usb_extcon_resume(struct device *dev)
 	struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);
 	int ret = 0;
 
-	if (device_may_wakeup(dev))
-		ret = disable_irq_wake(info->irq);
+	if (device_may_wakeup(dev)) {
+		if (info->id_irq > 0)
+			ret = disable_irq_wake(info->id_irq);
+		if (info->vbus_irq > 0)
+			ret = disable_irq_wake(info->vbus_irq);
+	}
 
 	return ret;
 }