linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Ramuthevar,Vadivel MuruganX"  <vadivel.muruganx.ramuthevar@linux.intel.com>
To: linux-kernel@vger.kernel.org, myungjoo.ham@samsung.com,
	cw00.choi@samsung.com
Cc: andriy.shevchenko@intel.com, thomas.langer@intel.com,
	cheol.yong.kim@intel.com, qi-ming.wu@intel.com,
	yin1.li@intel.com,
	Ramuthevar Vadivel Murugan 
	<vadivel.muruganx.ramuthevar@linux.intel.com>
Subject: [PATCH v1 6/9] extcon: extcon-ptn5150: Set and get the VBUS and POLARITY property state
Date: Tue, 18 Aug 2020 14:57:24 +0800	[thread overview]
Message-ID: <20200818065727.50520-7-vadivel.muruganx.ramuthevar@linux.intel.com> (raw)
In-Reply-To: <20200818065727.50520-1-vadivel.muruganx.ramuthevar@linux.intel.com>

From: Ramuthevar Vadivel Murugan <vadivel.muruganx.ramuthevar@linux.intel.com>

Set and get the VBUS and POLARITY property state.

Signed-off-by: Ramuthevar Vadivel Murugan <vadivel.muruganx.ramuthevar@linux.intel.com>
---
 drivers/extcon/extcon-ptn5150.c | 75 ++++++++++++++++++++++++++++++-----------
 1 file changed, 55 insertions(+), 20 deletions(-)

diff --git a/drivers/extcon/extcon-ptn5150.c b/drivers/extcon/extcon-ptn5150.c
index 31b7cbf1551d..5612dc0ef2af 100644
--- a/drivers/extcon/extcon-ptn5150.c
+++ b/drivers/extcon/extcon-ptn5150.c
@@ -64,6 +64,12 @@ static const unsigned int ptn5150_extcon_cable[] = {
 	EXTCON_NONE,
 };
 
+enum {
+	POLARITY_NC,
+	POLARITY_CC1,
+	POLARITY_CC2,
+};
+
 static const struct regmap_config ptn5150_regmap_config = {
 	.reg_bits	= 8,
 	.val_bits	= 8,
@@ -74,9 +80,14 @@ static void ptn5150_irq_work(struct work_struct *work)
 {
 	struct ptn5150_info *info = container_of(work,
 			struct ptn5150_info, irq_work);
-	int ret = 0;
+	int ret = 0, x = 0;
 	unsigned int reg_data;
 	unsigned int int_status;
+	union extcon_property_value vbus_detected;
+	union extcon_property_value flipped;
+	bool host_vbus = false;
+	bool host = false;
+	bool dev = false;
 
 	if (!info->edev)
 		return;
@@ -90,6 +101,11 @@ static void ptn5150_irq_work(struct work_struct *work)
 		return;
 	}
 
+	dev_dbg(info->dev, "CC POLARITY status:%x\n", reg_data);
+	x = FIELD_GET(PTN5150_REG_INT_CABLE_ATTACH_MASK, reg_data);
+	flipped.intval = (x == POLARITY_CC2);
+	vbus_detected.intval = !!(reg_data & PTN5150_REG_CC_VBUS_DETECTION);
+
 	/* Clear interrupt. Read would clear the register */
 	ret = regmap_read(info->regmap, PTN5150_REG_INT_STATUS, &int_status);
 	if (ret) {
@@ -111,6 +127,7 @@ static void ptn5150_irq_work(struct work_struct *work)
 
 			switch (port_status) {
 			case PTN5150_DFP_ATTACHED:
+				dev = true;
 				extcon_set_state_sync(info->edev,
 						EXTCON_USB_HOST, false);
 				gpiod_set_value(info->vbus_gpiod, 0);
@@ -118,6 +135,8 @@ static void ptn5150_irq_work(struct work_struct *work)
 						true);
 				break;
 			case PTN5150_UFP_ATTACHED:
+				host_vbus = !vbus_detected.intval;
+				host = true;
 				extcon_set_state_sync(info->edev, EXTCON_USB,
 						false);
 				vbus = FIELD_GET(PTN5150_REG_CC_VBUS_DETECTION,
@@ -127,6 +146,9 @@ static void ptn5150_irq_work(struct work_struct *work)
 				else
 					gpiod_set_value(info->vbus_gpiod, 1);
 
+				extcon_set_property(info->edev, EXTCON_USB_HOST,
+						    EXTCON_PROP_USB_TYPEC_POLARITY,
+						    flipped);
 				extcon_set_state_sync(info->edev,
 						EXTCON_USB_HOST, true);
 				break;
@@ -145,6 +167,8 @@ static void ptn5150_irq_work(struct work_struct *work)
 		}
 	}
 
+	if (host_vbus)
+		gpiod_set_value(info->vbus_gpiod, host_vbus);
 	/* Clear interrupt. Read would clear the register */
 	ret = regmap_read(info->regmap, PTN5150_REG_INT_REG_STATUS,
 			&int_status);
@@ -155,6 +179,11 @@ static void ptn5150_irq_work(struct work_struct *work)
 		return;
 	}
 
+	extcon_set_property(info->edev, host ? EXTCON_USB_HOST : EXTCON_USB,
+			    EXTCON_PROP_USB_VBUS, vbus_detected);
+	extcon_set_state_sync(info->edev, EXTCON_USB_HOST, host);
+	extcon_set_state_sync(info->edev, EXTCON_USB, dev);
+
 	mutex_unlock(&info->mutex);
 }
 
@@ -227,7 +256,7 @@ static int ptn5150_i2c_probe(struct i2c_client *i2c,
 		dev_err(dev, "failed to get INT GPIO\n");
 		return PTR_ERR(info->int_gpiod);
 	}
-	info->vbus_gpiod = devm_gpiod_get(&i2c->dev, "vbus", GPIOD_IN);
+	info->vbus_gpiod = devm_gpiod_get_optional(&i2c->dev, "vbus", GPIOD_IN);
 	if (IS_ERR(info->vbus_gpiod)) {
 		dev_err(dev, "failed to get VBUS GPIO\n");
 		return PTR_ERR(info->vbus_gpiod);
@@ -250,24 +279,6 @@ static int ptn5150_i2c_probe(struct i2c_client *i2c,
 		return ret;
 	}
 
-	if (info->int_gpiod) {
-		info->irq = gpiod_to_irq(info->int_gpiod);
-		if (info->irq < 0) {
-			dev_err(dev, "failed to get INTB IRQ\n");
-			return info->irq;
-		}
-
-		ret = devm_request_threaded_irq(dev, info->irq, NULL,
-						ptn5150_irq_handler,
-						IRQF_TRIGGER_FALLING |
-						IRQF_ONESHOT,
-						i2c->name, info);
-		if (ret < 0) {
-			dev_err(dev, "failed to request handler for INTB IRQ\n");
-			return ret;
-		}
-	}
-
 	/* Allocate extcon device */
 	info->edev = devm_extcon_dev_allocate(info->dev, ptn5150_extcon_cable);
 	if (IS_ERR(info->edev)) {
@@ -282,6 +293,30 @@ static int ptn5150_i2c_probe(struct i2c_client *i2c,
 		return ret;
 	}
 
+	extcon_set_property_capability(info->edev, EXTCON_USB,
+				       EXTCON_PROP_USB_VBUS);
+	extcon_set_property_capability(info->edev, EXTCON_USB_HOST,
+				       EXTCON_PROP_USB_VBUS);
+	extcon_set_property_capability(info->edev, EXTCON_USB_HOST,
+				       EXTCON_PROP_USB_TYPEC_POLARITY);
+
+	info->irq = gpiod_to_irq(info->int_gpiod);
+	if (info->irq < 0) {
+		dev_err(dev, "failed to get INTB IRQ\n");
+		return info->irq;
+	}
+
+	ret = devm_request_threaded_irq(dev, info->irq, NULL, ptn5150_irq_handler,
+					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
+					i2c->name, info);
+	if (ret < 0) {
+		dev_err(dev, "failed to request handler for INTB IRQ\n");
+		return ret;
+	}
+
+	/* PNT5150 interrupt is level based, read result once in case we */
+	schedule_work(&info->irq_work);
+
 	/* Initialize PTN5150 device and print vendor id and version id */
 	ret = ptn5150_init_dev_type(info);
 	if (ret)
-- 
2.11.0


  parent reply	other threads:[~2020-08-18  6:57 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-18  6:57 [PATCH v1 0/9] extcon: extcon-ptn5150: Add the USB external connector support Ramuthevar,Vadivel MuruganX
2020-08-18  6:57 ` [PATCH v1 1/9] extcon: extcon-ptn5150: Switch to GENMASK() for vendor and device ID's Ramuthevar,Vadivel MuruganX
2020-08-26  6:57   ` Krzysztof Kozlowski
2020-08-18  6:57 ` [PATCH v1 2/9] extcon: extcon-ptn5150: Switch to GENMASK() for VBUS detection macro Ramuthevar,Vadivel MuruganX
2020-08-26  6:59   ` Krzysztof Kozlowski
2020-08-26  7:33     ` Ramuthevar, Vadivel MuruganX
2020-08-18  6:57 ` [PATCH v1 3/9] extcon: extcon-ptn5150: Switch to BIT() macro for cable attach Ramuthevar,Vadivel MuruganX
2020-08-26  6:59   ` Krzysztof Kozlowski
2020-08-26  7:50     ` Ramuthevar, Vadivel MuruganX
2020-08-18  6:57 ` [PATCH v1 4/9] extcon: extcon-ptn5150: Switch to BIT() for cable detach macro Ramuthevar,Vadivel MuruganX
2020-08-26  7:00   ` Krzysztof Kozlowski
2020-08-18  6:57 ` [PATCH v1 5/9] extcon: extcon-ptn5150: Switch to GENMASK() for port attachment macro Ramuthevar,Vadivel MuruganX
2020-08-26  7:00   ` Krzysztof Kozlowski
2020-08-18  6:57 ` Ramuthevar,Vadivel MuruganX [this message]
2020-08-18  6:57 ` [PATCH v1 7/9] extcon: extcon-ptn5150: Add USB debug accessory support Ramuthevar,Vadivel MuruganX
2020-08-18  6:57 ` [PATCH v1 8/9] extcon: extcon-ptn5150: Add USB analog audio " Ramuthevar,Vadivel MuruganX
2020-08-18  6:57 ` [PATCH v1 9/9] extcon: extcon-ptn5150: Remove unused variable and extra space Ramuthevar,Vadivel MuruganX
2020-08-18  8:40 ` [PATCH v1 0/9] extcon: extcon-ptn5150: Add the USB external connector support Andy Shevchenko
2020-08-19  5:35   ` Ramuthevar, Vadivel MuruganX
2020-08-19  7:55     ` Andy Shevchenko
2020-08-19  8:45       ` Ramuthevar, Vadivel MuruganX
2020-08-25  8:19         ` Heikki Krogerus
2020-08-26  2:51           ` Ramuthevar, Vadivel MuruganX
2020-08-26  9:18             ` Andy Shevchenko
2020-08-26  9:43               ` Ramuthevar, Vadivel MuruganX
2020-08-26  9:50               ` Krzysztof Kozlowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200818065727.50520-7-vadivel.muruganx.ramuthevar@linux.intel.com \
    --to=vadivel.muruganx.ramuthevar@linux.intel.com \
    --cc=andriy.shevchenko@intel.com \
    --cc=cheol.yong.kim@intel.com \
    --cc=cw00.choi@samsung.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=myungjoo.ham@samsung.com \
    --cc=qi-ming.wu@intel.com \
    --cc=thomas.langer@intel.com \
    --cc=yin1.li@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).