mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Surendranath Parimi <surendranath.parimi@axis.com>
To: MyungJoo Ham <myungjoo.ham@samsung.com>,
	Chanwoo Choi <cw00.choi@samsung.com>
Cc: <kernel@axis.com>,
	Surendranath Parimi <surendranath.parimi@axis.com>,
	<linux-kernel@vger.kernel.org>
Subject: [PATCH] extcon: usbc-tusb320: Expose the charger type
Date: Mon, 15 Aug 2022 11:04:27 +0200	[thread overview]
Message-ID: <20220815090427.3205-1-surendranath.parimi@axis.com> (raw)

In the UFP mode of operation, knowing the charger type helps to draw the
appropriate amount of current from the charger. The charger type can be
know by reading the current_mode_detect bits of register 0x08.

Add support to expose the charger type.

Signed-off-by: Surendranath Parimi <surendranath.parimi@axis.com>
---
 drivers/extcon/extcon-usbc-tusb320.c | 37 ++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)

diff --git a/drivers/extcon/extcon-usbc-tusb320.c b/drivers/extcon/extcon-usbc-tusb320.c
index 6ba3d89b106d..43f07efa3472 100644
--- a/drivers/extcon/extcon-usbc-tusb320.c
+++ b/drivers/extcon/extcon-usbc-tusb320.c
@@ -14,6 +14,10 @@
 #include <linux/module.h>
 #include <linux/regmap.h>
 
+#define TUSB320_REG8				0x8
+#define TUSB320_REG8_CURRENT_MODE_DETECT_SHIFT	0x4
+#define TUSB320_REG8_CURRENT_MODE_DETECT_MASK	0x3
+
 #define TUSB320_REG9				0x9
 #define TUSB320_REG9_ATTACHED_STATE_SHIFT	6
 #define TUSB320_REG9_ATTACHED_STATE_MASK	0x3
@@ -42,6 +46,13 @@ enum tusb320_mode {
 	TUSB320_MODE_DRP,
 };
 
+enum tusb320_current_mode_detect {
+	TUSB320_CURRENT_MODE_DETECT_DEFAULT,
+	TUSB320_CURRENT_MODE_DETECT_MEDIUM,
+	TUSB320_CURRENT_MODE_DETECT_ACCESSORY,
+	TUSB320_CURRENT_MODE_DETECT_HIGH,
+};
+
 struct tusb320_priv;
 
 struct tusb320_ops {
@@ -67,6 +78,9 @@ static const char * const tusb_attached_states[] = {
 static const unsigned int tusb320_extcon_cable[] = {
 	EXTCON_USB,
 	EXTCON_USB_HOST,
+	EXTCON_CHG_USB_FAST,
+	EXTCON_CHG_USB_SLOW,
+	EXTCON_CHG_USB_PD,
 	EXTCON_NONE,
 };
 
@@ -187,8 +201,8 @@ static struct tusb320_ops tusb320l_ops = {
 static irqreturn_t tusb320_irq_handler(int irq, void *dev_id)
 {
 	struct tusb320_priv *priv = dev_id;
-	int state, polarity;
-	unsigned reg;
+	int state, polarity, current_mode;
+	unsigned int reg, reg8;
 
 	if (regmap_read(priv->regmap, TUSB320_REG9, &reg)) {
 		dev_err(priv->dev, "error during i2c read!\n");
@@ -205,10 +219,26 @@ static irqreturn_t tusb320_irq_handler(int irq, void *dev_id)
 	dev_dbg(priv->dev, "attached state: %s, polarity: %d\n",
 		tusb_attached_states[state], polarity);
 
+	if (regmap_read(priv->regmap, TUSB320_REG8, &reg8)) {
+		dev_err(priv->dev, "error during i2c read!\n");
+		return IRQ_NONE;
+	}
+
+	current_mode = (reg8 >> TUSB320_REG8_CURRENT_MODE_DETECT_SHIFT) &
+			TUSB320_REG8_CURRENT_MODE_DETECT_MASK;
+
+	dev_dbg(priv->dev, "current_mode:%d\n", current_mode);
+
 	extcon_set_state(priv->edev, EXTCON_USB,
 			 state == TUSB320_ATTACHED_STATE_UFP);
 	extcon_set_state(priv->edev, EXTCON_USB_HOST,
 			 state == TUSB320_ATTACHED_STATE_DFP);
+	extcon_set_state(priv->edev, EXTCON_CHG_USB_SLOW,
+			 current_mode == TUSB320_CURRENT_MODE_DETECT_ACCESSORY);
+	extcon_set_state(priv->edev, EXTCON_CHG_USB_FAST,
+			 current_mode == TUSB320_CURRENT_MODE_DETECT_MEDIUM);
+	extcon_set_state(priv->edev, EXTCON_CHG_USB_PD,
+			 current_mode == TUSB320_CURRENT_MODE_DETECT_HIGH);
 	extcon_set_property(priv->edev, EXTCON_USB,
 			    EXTCON_PROP_USB_TYPEC_POLARITY,
 			    (union extcon_property_value)polarity);
@@ -217,6 +247,9 @@ static irqreturn_t tusb320_irq_handler(int irq, void *dev_id)
 			    (union extcon_property_value)polarity);
 	extcon_sync(priv->edev, EXTCON_USB);
 	extcon_sync(priv->edev, EXTCON_USB_HOST);
+	extcon_sync(priv->edev, EXTCON_CHG_USB_PD);
+	extcon_sync(priv->edev, EXTCON_CHG_USB_FAST);
+	extcon_sync(priv->edev, EXTCON_CHG_USB_SLOW);
 
 	priv->state = state;
 
-- 
2.20.1


             reply	other threads:[~2022-08-15  9:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-15  9:04 Surendranath Parimi [this message]
2022-08-23 22:41 ` Chanwoo Choi

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=20220815090427.3205-1-surendranath.parimi@axis.com \
    --to=surendranath.parimi@axis.com \
    --cc=cw00.choi@samsung.com \
    --cc=kernel@axis.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=myungjoo.ham@samsung.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

all inboxes | Powered by JetHome®