From af0bfa3cdd4a19ed5da5f43da250982b2217239d Mon Sep 17 00:00:00 2001
From: Potato <nikko@faint.day>
Date: Tue, 24 Oct 2023 19:08:18 +0800
Subject: [PATCH 5/6] drivers: power: axp20x: customize PMU

This patch set some default states, add new sysfs interfaces, and register the
power off callback to kernel. Mostly harmless.
---
 drivers/mfd/axp20x.c                   | 22 +++++++++++++
 drivers/power/supply/axp20x_ac_power.c |  3 ++
 drivers/power/supply/axp20x_battery.c  | 44 ++++++++++++++++++++++++++
 3 files changed, 69 insertions(+)

diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
index 880c41fa7021..27c2000badc7 100644
--- a/drivers/mfd/axp20x.c
+++ b/drivers/mfd/axp20x.c
@@ -24,6 +24,7 @@
 #include <linux/module.h>
 #include <linux/of_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/reboot.h>
 #include <linux/regmap.h>
 #include <linux/regulator/consumer.h>
 
@@ -845,6 +846,21 @@ static void axp20x_power_off(void)
 	mdelay(500);
 }
 
+static int axp20x_power_off_handler(struct sys_off_data *data)
+{
+	struct axp20x_dev *device = data->cb_data;
+
+	if (device->variant == AXP288_ID)
+		return NOTIFY_DONE;
+
+	regmap_write(device->regmap, AXP20X_OFF_CTRL, AXP20X_OFF);
+
+	/* Give capacitors etc. time to drain to avoid kernel panic msg. */
+	mdelay(500);
+
+	return NOTIFY_DONE;
+}
+
 int axp20x_match_device(struct axp20x_dev *axp20x)
 {
 	struct device *dev = axp20x->dev;
@@ -1012,6 +1028,12 @@ int axp20x_device_probe(struct axp20x_dev *axp20x)
 	if (!pm_power_off) {
 		axp20x_pm_power_off = axp20x;
 		pm_power_off = axp20x_power_off;
+	} else {
+		/* register power off handler */
+		ret = devm_register_power_off_handler(axp20x->dev, axp20x_power_off_handler, axp20x);
+		if (ret) {
+			dev_warn(axp20x->dev, "Failed to register power off handler! (%d)", ret);
+		}
 	}
 
 	dev_info(axp20x->dev, "AXP20X driver loaded\n");
diff --git a/drivers/power/supply/axp20x_ac_power.c b/drivers/power/supply/axp20x_ac_power.c
index 57e50208d537..ce7ad453201c 100644
--- a/drivers/power/supply/axp20x_ac_power.c
+++ b/drivers/power/supply/axp20x_ac_power.c
@@ -53,6 +53,9 @@ static irqreturn_t axp20x_ac_power_irq(int irq, void *devid)
 {
 	struct axp20x_ac_power *power = devid;
 
+	/* do not limit IPSOUT current */
+	regmap_update_bits(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, 0x03, 0x03);
+
 	power_supply_changed(power->supply);
 
 	return IRQ_HANDLED;
diff --git a/drivers/power/supply/axp20x_battery.c b/drivers/power/supply/axp20x_battery.c
index 9106077c0dbb..53b53137a480 100644
--- a/drivers/power/supply/axp20x_battery.c
+++ b/drivers/power/supply/axp20x_battery.c
@@ -326,6 +326,42 @@ static int axp20x_battery_get_prop(struct power_supply *psy,
 		val->intval *= 1000;
 		break;
 
+	case POWER_SUPPLY_PROP_ENERGY_FULL:
+	case POWER_SUPPLY_PROP_ENERGY_NOW:
+		/* When no battery is present, return 0 */
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
+				  &reg);
+		if (ret)
+			return ret;
+
+		if (!(reg & AXP20X_PWR_OP_BATT_PRESENT)) {
+			val->intval = 0;
+			return 0;
+		}
+
+		if(psp == POWER_SUPPLY_PROP_ENERGY_FULL) {
+			val->intval = 8000000;
+			return 0;
+		}
+
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &reg);
+		if (ret)
+			return ret;
+
+		if (axp20x_batt->data->has_fg_valid && !(reg & AXP22X_FG_VALID))
+			return -EINVAL;
+
+		val1 = reg & AXP209_FG_PERCENT;
+		if (val1 > 90)
+			val1= 80;
+		else if (val1 < 10)
+			val1 = 0;
+		else
+			val1 -= 10;
+
+		val->intval = val1 * 100000;
+		break;
+
 	default:
 		return -EINVAL;
 	}
@@ -497,6 +533,8 @@ static enum power_supply_property axp20x_battery_props[] = {
 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
 	POWER_SUPPLY_PROP_CAPACITY,
+	POWER_SUPPLY_PROP_ENERGY_FULL,
+	POWER_SUPPLY_PROP_ENERGY_NOW,
 };
 
 static int axp20x_battery_prop_writeable(struct power_supply *psy,
@@ -642,6 +680,12 @@ static int axp20x_power_probe(struct platform_device *pdev)
 	axp20x_get_constant_charge_current(axp20x_batt,
 					   &axp20x_batt->max_ccc);
 
+	regmap_update_bits(axp20x_batt->regmap, AXP20X_VBUS_IPSOUT_MGMT, 0x03, 0x03); /* do not limit IPSOUT current */
+	regmap_update_bits(axp20x_batt->regmap, AXP20X_OFF_CTRL, 0x08, 0x08); /* set charge led controlled by charge module */
+	regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL2, 0x30, 0x20); /* full charge output, charge led function type */
+	// regmap_update_bits(axp20x_batt->regmap, AXP20X_PEK_KEY, 0x0f, 0x0b); /* configure power key, this is handled by PEK driver */
+	regmap_update_bits(axp20x_batt->regmap, AXP20X_GPIO0_CTRL, 0x07, 0x00); /* configure charging LED */
+
 	return 0;
 }
 
-- 
2.43.0

