[PATCH RFC 08/11] power: supply: max77693: Add support for detecting and enabling OTG

Artur Weber
2024-05-30T08:56:38Z

Building upon the newly added extcon detection support, add detection
for USB OTG cables (EXTCON_USB_HOST type), and enable/disable the OTG
bits as needed.

Signed-off-by: Artur Weber <aweber.kernel@gmail.com>
---
Downstream also sets "CHGIN output current limit in OTG mode" to 900mA
by writing (1 << 7) to the CHG_CNFG_02 register; while I would try to
add this here, I do not know which exact bits control the current limit
and how their value affects it (downstream has no docs other than what
I just mentioned), so it's impossible for me to know what the mask is.
---
drivers/power/supply/max77693_charger.c | 70 +++++++++++++++++++++++++++------
1 file changed, 59 insertions(+), 11 deletions(-)

diff --git a/drivers/power/supply/max77693_charger.c b/drivers/power/supply/max77693_charger.c
index c2e8ae367381..e548fd420e78 100644
--- a/drivers/power/supply/max77693_charger.c
+++ b/drivers/power/supply/max77693_charger.c
@@ -737,11 +737,41 @@ static int max77693_set_charging(struct max77693_charger *chg, bool enable)
return ret;
}

+static int max77693_set_otg(struct max77693_charger *chg, bool enable)
+{
+ struct regmap *regmap = chg->max77693->regmap;
+ unsigned int data;
+ bool is_enabled;
+ int ret;
+
+ ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_CNFG_00, &data);
+ if (ret)
+ return ret;
+
+ is_enabled = !!(data & CHG_CNFG_00_OTG_MASK);
+
+ if (enable && !is_enabled) {
+ /* OTG on, boost on, DIS_MUIC_CTRL on */
+ data |= CHG_CNFG_00_OTG_MASK | CHG_CNFG_00_BOOST_MASK \
+ | CHG_CNFG_00_DIS_MUIC_CTRL_MASK;
+
+ } else if (!enable && is_enabled) {
+ /* OTG off, boost off, DIS_MUIC_CTRL off */
+ data &= ~(CHG_CNFG_00_OTG_MASK | CHG_CNFG_00_BOOST_MASK \
+ | CHG_CNFG_00_DIS_MUIC_CTRL_MASK);
+ }
+
+ return regmap_write(chg->max77693->regmap,
+ MAX77693_CHG_REG_CHG_CNFG_00,
+ data);
+}
+
static void max77693_charger_extcon_work(struct work_struct *work)
{
struct max77693_charger *chg = container_of(work, struct max77693_charger,
cable.work);
struct extcon_dev *edev = chg->cable.edev;
+ bool set_charging, set_otg;
int connector, state;
int ret;

@@ -760,25 +790,43 @@ static void max77693_charger_extcon_work(struct work_struct *work)
case EXTCON_CHG_USB_FAST:
case EXTCON_CHG_USB_SLOW:
case EXTCON_CHG_USB_PD:
- ret = max77693_set_charging(chg, true);
- if (ret) {
- dev_err(chg->dev, "failed to enable charging\n");
- break;
- }
+ set_charging = true;
+ set_otg = false;
+
dev_info(chg->dev, "charging. connector type: %d\n",
connector);
break;
+ case EXTCON_USB_HOST:
+ set_charging = false;
+ set_otg = true;
+
+ dev_info(chg->dev, "USB host. connector type: %d\n",
+ connector);
+ break;
default:
- ret = max77693_set_charging(chg, false);
- if (ret) {
- dev_err(chg->dev, "failed to disable charging\n");
- break;
- }
- dev_info(chg->dev, "charging. connector type: %d\n",
+ set_charging = false;
+ set_otg = false;
+
+ dev_info(chg->dev, "disconnected. connector type: %d\n",
connector);
break;
}

+ /*
+ * The functions below already check if the change is necessary,
+ * so we don't need to do so here.
+ */
+ ret = max77693_set_charging(chg, set_charging);
+ if (ret) {
+ dev_err(chg->dev, "failed to set charging (%d)\n", ret);
+ goto out;
+ }
+
+ ret = max77693_set_otg(chg, set_otg);
+ if (ret)
+ dev_err(chg->dev, "failed to set OTG (%d)\n", ret);
+
+out:
power_supply_changed(chg->charger);
}

--
2.45.1