[PATCH] extcon: usbc-tusb320: Expose the charger type

From: Surendranath Parimi
Date: Mon Aug 15 2022 - 05:05:23 EST


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@xxxxxxxx>
---
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