[PATCH v3 4/7] power: supply: max8997_charger: Set CHARGER current limit

From: Timon Baetz
Date: Tue Dec 22 2020 - 02:32:46 EST


Register for extcon notification and set charging current depending on
the detected cable type. Current values are taken from vendor kernel,
where most charger types end up setting 650mA [0].

Also enable and disable the CHARGER regulator based on extcon events.

[0] https://github.com/krzk/linux-vendor-backup/blob/samsung/galaxy-s2-epic-4g-touch-sph-d710-exynos4210-dump/drivers/misc/max8997-muic.c#L1675-L1678

Signed-off-by: Timon Baetz <timon.baetz@xxxxxxxxxxxxxx>
---
drivers/power/supply/max8997_charger.c | 94 ++++++++++++++++++++++++++
1 file changed, 94 insertions(+)

diff --git a/drivers/power/supply/max8997_charger.c b/drivers/power/supply/max8997_charger.c
index 1947af25879a..673ffff14ae0 100644
--- a/drivers/power/supply/max8997_charger.c
+++ b/drivers/power/supply/max8997_charger.c
@@ -6,12 +6,14 @@
// MyungJoo Ham <myungjoo.ham@xxxxxxxxxxx>

#include <linux/err.h>
+#include <linux/extcon.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/mfd/max8997.h>
#include <linux/mfd/max8997-private.h>
+#include <linux/regulator/consumer.h>

/* MAX8997_REG_STATUS4 */
#define DCINOK_SHIFT 1
@@ -31,6 +33,10 @@ struct charger_data {
struct device *dev;
struct max8997_dev *iodev;
struct power_supply *battery;
+ struct regulator *reg;
+ struct extcon_dev *edev;
+ struct notifier_block extcon_nb;
+ struct work_struct extcon_work;
};

static enum power_supply_property max8997_battery_props[] = {
@@ -88,6 +94,67 @@ static int max8997_battery_get_property(struct power_supply *psy,
return 0;
}

+static void max8997_battery_extcon_evt_stop_work(void *data)
+{
+ struct charger_data *charger = data;
+
+ cancel_work_sync(&charger->extcon_work);
+}
+
+static void max8997_battery_extcon_evt_worker(struct work_struct *work)
+{
+ struct charger_data *charger =
+ container_of(work, struct charger_data, extcon_work);
+ struct extcon_dev *edev = charger->edev;
+ int current_limit;
+
+ if (extcon_get_state(edev, EXTCON_CHG_USB_SDP) > 0) {
+ dev_dbg(charger->dev, "USB SDP charger is connected\n");
+ current_limit = 450000;
+ } else if (extcon_get_state(edev, EXTCON_CHG_USB_DCP) > 0) {
+ dev_dbg(charger->dev, "USB DCP charger is connected\n");
+ current_limit = 650000;
+ } else if (extcon_get_state(edev, EXTCON_CHG_USB_FAST) > 0) {
+ dev_dbg(charger->dev, "USB FAST charger is connected\n");
+ current_limit = 650000;
+ } else if (extcon_get_state(edev, EXTCON_CHG_USB_SLOW) > 0) {
+ dev_dbg(charger->dev, "USB SLOW charger is connected\n");
+ current_limit = 650000;
+ } else if (extcon_get_state(edev, EXTCON_CHG_USB_CDP) > 0) {
+ dev_dbg(charger->dev, "USB CDP charger is connected\n");
+ current_limit = 650000;
+ } else {
+ dev_dbg(charger->dev, "USB charger is diconnected\n");
+ current_limit = -1;
+ }
+
+ if (current_limit > 0) {
+ int ret = regulator_set_current_limit(charger->reg, current_limit, current_limit);
+
+ if (ret) {
+ dev_err(charger->dev, "failed to set current limit: %d\n", ret);
+ return;
+ }
+ ret = regulator_enable(charger->reg);
+ if (ret)
+ dev_err(charger->dev, "failed to enable regulator: %d\n", ret);
+ } else {
+ int ret = regulator_disable(charger->reg);
+
+ if (ret)
+ dev_err(charger->dev, "failed to disable regulator: %d\n", ret);
+ }
+}
+
+static int max8997_battery_extcon_evt(struct notifier_block *nb,
+ unsigned long event, void *param)
+{
+ struct charger_data *charger =
+ container_of(nb, struct charger_data, extcon_nb);
+ schedule_work(&charger->extcon_work);
+ return NOTIFY_OK;
+}
+
static const struct power_supply_desc max8997_battery_desc = {
.name = "max8997_pmic",
.type = POWER_SUPPLY_TYPE_BATTERY,
@@ -170,6 +237,33 @@ static int max8997_battery_probe(struct platform_device *pdev)
return PTR_ERR(charger->battery);
}

+ charger->reg = devm_regulator_get(&pdev->dev, "charger");
+ if (IS_ERR(charger->reg)) {
+ dev_err(&pdev->dev, "couldn't get charger regulator\n");
+ return PTR_ERR(charger->reg);
+ }
+
+ charger->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
+ if (IS_ERR(charger->edev)) {
+ if (PTR_ERR(charger->edev) != -EPROBE_DEFER)
+ dev_err(charger->dev, "couldn't get extcon device\n");
+ return PTR_ERR(charger->edev);
+ }
+
+ INIT_WORK(&charger->extcon_work, max8997_battery_extcon_evt_worker);
+ ret = devm_add_action(&pdev->dev, max8997_battery_extcon_evt_stop_work, charger);
+ if (ret) {
+ dev_err(&pdev->dev, "failed to add extcon evt stop action: %d\n", ret);
+ return ret;
+ }
+ charger->extcon_nb.notifier_call = max8997_battery_extcon_evt;
+ ret = devm_extcon_register_notifier_all(&pdev->dev, charger->edev,
+ &charger->extcon_nb);
+ if (ret) {
+ dev_err(&pdev->dev, "failed to register extcon notifier\n");
+ return ret;
+ };
+
return 0;
}

--
2.25.1