[PATCH v2 7/9] power: supply: max77693: Add support for detecting and enabling OTG

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---
Changes in v2:
- Added CHGIN OTG current limit value
- Squashed MFD header register changes into this commit
---
 drivers/power/supply/max77693_charger.c | 103 +++++++++++++++++++++++++++-----
 include/linux/mfd/max77693-private.h    |   5 ++
 2 files changed, 94 insertions(+), 14 deletions(-)

diff --git a/drivers/power/supply/max77693_charger.c b/drivers/power/supply/max77693_charger.c
index 2dc273dd96ee..34d68b1050d4 100644
--- a/drivers/power/supply/max77693_charger.c
+++ b/drivers/power/supply/max77693_charger.c
@@ -669,6 +669,16 @@ static int max77693_reg_init(struct max77693_charger *chg)
 	if (ret)
 		return ret;
 
+	/* Set OTG current limit to 900 mA */
+	data = (0x1 << CHG_CNFG_02_OTG_ILIM_SHIFT);
+	ret = regmap_update_bits(chg->max77693->regmap,
+				MAX77693_CHG_REG_CHG_CNFG_02,
+				CHG_CNFG_02_OTG_ILIM_MASK, data);
+	if (ret) {
+		dev_err(chg->dev, "Error setting OTG current limit: %d\n", ret);
+		return ret;
+	}
+
 	return max77693_set_charge_input_threshold_volt(chg,
 			chg->charge_input_threshold_volt);
 }
@@ -690,11 +700,42 @@ 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;
+	unsigned int current_limit;
 	int connector, state;
 	int ret;
 
@@ -707,31 +748,61 @@ static void max77693_charger_extcon_work(struct work_struct *work)
 
 	switch (connector) {
 	case EXTCON_CHG_USB_SDP:
-	case EXTCON_CHG_USB_DCP:
 	case EXTCON_CHG_USB_CDP:
+	case EXTCON_CHG_USB_SLOW:
+		current_limit = 500000; /* 500 mA */
+		set_charging = true;
+		set_otg = false;
+
+		dev_info(chg->dev, "slow charging. connector type: %d\n",
+			 connector);
+		break;
+	case EXTCON_CHG_USB_DCP:
 	case EXTCON_CHG_USB_ACA:
 	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;
-		}
-		dev_info(chg->dev, "charging. connector type: %d\n",
+		current_limit = chg->fast_charge_current;
+		set_charging = true;
+		set_otg = false;
+
+		dev_info(chg->dev, "fast charging. connector type: %d\n",
+			 connector);
+		break;
+	case EXTCON_USB_HOST:
+		current_limit = 500000; /* 500 mA */
+		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",
+		current_limit = 500000; /* 500 mA */
+		set_charging = false;
+		set_otg = false;
+
+		dev_info(chg->dev, "disconnected. connector type: %d\n",
 			 connector);
 		break;
 	}
 
+	ret = max77693_set_current_limit(chg, current_limit);
+	if (ret) {
+		dev_err(chg->dev, "failed to set current limit (%d)\n", ret);
+		goto out;
+	}
+
+	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);
 }
 
@@ -793,6 +864,10 @@ static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
 			&chg->batttery_overcurrent))
 		chg->batttery_overcurrent = DEFAULT_BATTERY_OVERCURRENT;
 
+	if (of_property_read_u32(np, "maxim,fast-charge-current-microamp",
+			&chg->fast_charge_current))
+		chg->fast_charge_current = DEFAULT_FAST_CHARGE_CURRENT;
+
 	if (of_property_read_u32(np, "maxim,charge-input-threshold-microvolt",
 			&chg->charge_input_threshold_volt))
 		chg->charge_input_threshold_volt =
diff --git a/include/linux/mfd/max77693-private.h b/include/linux/mfd/max77693-private.h
index 4570646e2f33..313fcc3173f9 100644
--- a/include/linux/mfd/max77693-private.h
+++ b/include/linux/mfd/max77693-private.h
@@ -209,7 +209,10 @@ enum max77693_charger_battery_state {
 
 /* MAX77693 CHG_CNFG_00 register */
 #define CHG_CNFG_00_CHG_MASK		0x1
+#define CHG_CNFG_00_OTG_MASK		0x2
 #define CHG_CNFG_00_BUCK_MASK		0x4
+#define CHG_CNFG_00_BOOST_MASK		0x8
+#define CHG_CNFG_00_DIS_MUIC_CTRL_MASK	0x20
 
 /* MAX77693_CHG_REG_CHG_CNFG_01 register */
 #define CHG_CNFG_01_FCHGTIME_SHIFT	0
@@ -222,6 +225,8 @@ enum max77693_charger_battery_state {
 /* MAX77693_CHG_REG_CHG_CNFG_02 register */
 #define CHG_CNFG_02_CC_SHIFT		0
 #define CHG_CNFG_02_CC_MASK		0x3F
+#define CHG_CNFG_02_OTG_ILIM_SHIFT	7
+#define CHG_CNFG_02_OTG_ILIM_MASK	0x80
 
 /* MAX77693_CHG_REG_CHG_CNFG_03 register */
 #define CHG_CNFG_03_TOITH_SHIFT		0

-- 
2.45.2





[Index of Archives]     [Linux SoC Development]     [Linux Rockchip Development]     [Linux for Synopsys ARC Processors]    
  • [Linux on Unisoc (RDA Micro) SoCs]     [Linux Actions SoC]     [Linux USB Development]     [Video for Linux]     [Linux Audio Users]     [Linux SCSI]     [Yosemite News]

  •   Powered by Linux