[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20240530-max77693-charger-extcon-v1-8-dc2a9e5bdf30@gmail.com>
Date: Thu, 30 May 2024 10:55:58 +0200
From: Artur Weber <aweber.kernel@...il.com>
To: Krzysztof Kozlowski <krzk@...nel.org>,
Chanwoo Choi <cw00.choi@...sung.com>
Cc: Sebastian Reichel <sre@...nel.org>, Rob Herring <robh@...nel.org>,
Conor Dooley <conor+dt@...nel.org>, Lee Jones <lee@...nel.org>,
Krzysztof Kozlowski <krzk+dt@...nel.org>,
Alim Akhtar <alim.akhtar@...sung.com>, linux-pm@...r.kernel.org,
devicetree@...r.kernel.org, linux-kernel@...r.kernel.org,
linux-arm-kernel@...ts.infradead.org, linux-samsung-soc@...r.kernel.org,
~postmarketos/upstreaming@...ts.sr.ht, Henrik Grimler <henrik@...mler.se>,
Wolfgang Wiedmeyer <wolfgit@...dmeyer.de>,
Denis 'GNUtoo' Carikli <GNUtoo@...erdimension.org>,
Artur Weber <aweber.kernel@...il.com>
Subject: [PATCH RFC 08/11] power: supply: max77693: Add support for
detecting and enabling OTG
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@...il.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
Powered by blists - more mailing lists