2022-08-15 09:29:15

by Surendranath Parimi

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

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 <[email protected]>
---
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


2022-08-23 22:55:19

by Chanwoo Choi

[permalink] [raw]
Subject: Re: [PATCH] extcon: usbc-tusb320: Expose the charger type

Hi Surendranath,

I applied the other patches[1] supporting usb type-c.
[1] https://git.kernel.org/pub/scm/linux/kernel/git/chanwoo/extcon.git/log/?h=extcon-next

And then tried to apply this patch. But the merger conflict occurs.
Please rework your patch based on latest extcon-next branch.

Thanks,
Chanwoo Choi

On 22. 8. 15. 18:04, Surendranath Parimi wrote:
> 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 <[email protected]>
> ---
> 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;
>