From 46a072a7ae48464c18a6f5c7d215141157664582 Mon Sep 17 00:00:00 2001
From: huangcm <1263938474@qq.com>
Date: Fri, 28 Mar 2025 03:34:08 +0000
Subject: [PATCH] feat(battery): fix battery boot on & cap

---
 longan/device/config/chips/a133/configs/c3/kickpi-k5c.dts       |   48 ++++++++++++------------
 longan/kernel/linux-4.9/drivers/power/supply/axp803_battery.c   |    6 ++-
 longan/device/config/chips/a133/configs/c3/sys_config.fex       |    1 
 longan/brandy/brandy-2.0/u-boot-2018/board/sunxi/power_manage.c |   58 ++++++++++++++++++++++++++++-
 4 files changed, 84 insertions(+), 29 deletions(-)

diff --git a/longan/brandy/brandy-2.0/u-boot-2018/board/sunxi/power_manage.c b/longan/brandy/brandy-2.0/u-boot-2018/board/sunxi/power_manage.c
index e24c9b8..23ba577 100644
--- a/longan/brandy/brandy-2.0/u-boot-2018/board/sunxi/power_manage.c
+++ b/longan/brandy/brandy-2.0/u-boot-2018/board/sunxi/power_manage.c
@@ -11,6 +11,8 @@
 #include <spare_head.h>
 #include <sunxi_display2.h>
 #include <console.h>
+#include <sunxi_power/pmu_axp81X.h>
+
 /*
  * Global data (for the gd->bd)
  */
@@ -411,16 +413,55 @@
 	return pmu_set_voltage(name, 0, onoff);
 }
 
+int pmu_axp707_get_poweron_source(void)
+{
+    int reg_value = 0;
+    reg_value = pmu_get_reg_value(AXP81X_OTG_STATUS);
+    printf("kickpi axp707 reason %x \n", reg_value);
+    pmu_set_reg_value(AXP81X_OTG_STATUS, 0xff);
+    printf("kickpi after clean reason %x \n", pmu_get_reg_value(AXP81X_OTG_STATUS));
+
+	if (reg_value & (1 << 0)) {
+		return AXP_BOOT_SOURCE_BUTTON;
+	} else if (reg_value & (1 << 1)) {
+		return AXP_BOOT_SOURCE_CHARGER;
+	} else if (reg_value & (1 << 2)) {
+		return AXP_BOOT_SOURCE_BATTERY;
+	}
+	return -1;
+}
+
+int runtime_tick(void);
+int pmu_axp707_get_battery_probe(void)
+{
+	int reg_value = 0;
+	int old_time = runtime_tick();
+	while ((runtime_tick() - old_time) < 2000) {
+        reg_value = pmu_get_reg_value(AXP81X_MODE_CHGSTATUS);
+	}
+
+	/*bit4 determines whether bit5 is valid*/
+	/*bit5 determines whether bat is exist*/
+	if ((reg_value & (1<<4)) && (reg_value & (1<<5)))
+		return 1;
+
+	return 0;
+}
 
 int sunxi_update_axp_info(void)
 {
 	int val = -1;
 	char bootreason[16] = {0};
 	int ret = 0, bat_exist = 0;
+    int dcin_exist = pmu_get_reg_value(AXP81X_STATUS);
 
 	ret = script_parser_fetch(FDT_PATH_POWER_SPLY, "battery_exist", &bat_exist, 1);
-	if (ret < 0)
-		bat_exist = 1;
+	if (ret < 0) {
+        bat_exist = pmu_axp707_get_battery_probe();
+        printf("kickpi detect battery_exist = %d, AXP81X_STATUS = %x\n", bat_exist, dcin_exist);
+    } else {
+        printf("force set battery_exist = %d\n", bat_exist);
+    }
 
 #ifdef CONFIG_SUNXI_BMU
 #ifdef CONFIG_AXP_LATE_INFO
@@ -429,10 +470,21 @@
 	val = gd->pmu_saved_status;
 #endif
 #endif
+    printf("kickpi pmu_get_sys_mode = %d\n", pmu_get_sys_mode());
 	if ((val == -1) && (pmu_get_sys_mode() == SUNXI_CHARGING_FLAG)) {
 		val = AXP_BOOT_SOURCE_CHARGER;
 		pmu_set_sys_mode(0);
 	}
+
+    if (bat_exist == 1) {
+        /* When the battery exists, it can only be turned on by power key */
+        if (pmu_axp707_get_poweron_source() != AXP_BOOT_SOURCE_BUTTON) {
+            val = AXP_BOOT_SOURCE_CHARGER;
+            pmu_set_sys_mode(0);
+            printf("kickpi[bat_exist] not power key up when boot on \n");
+        }
+    }
+
 	switch (val) {
 	case AXP_BOOT_SOURCE_BUTTON:
 		strncpy(bootreason, "button", sizeof("button"));
@@ -456,6 +508,8 @@
 		break;
 	}
 	env_set("bootreason", bootreason);
+
+    printf("kickpi bootreason = %s / val = %d \n", bootreason, val);
 	return 0;
 }
 
diff --git a/longan/device/config/chips/a133/configs/c3/kickpi-k5c.dts b/longan/device/config/chips/a133/configs/c3/kickpi-k5c.dts
index b3f8d69..86f742f 100755
--- a/longan/device/config/chips/a133/configs/c3/kickpi-k5c.dts
+++ b/longan/device/config/chips/a133/configs/c3/kickpi-k5c.dts
@@ -401,21 +401,21 @@
 					pmu_ac_vol = <4600>;
 					pmu_ac_cur = <3000>;
 
-					wakeup_ac_in;
-					wakeup_ac_out;
+					// wakeup_ac_in;
+					// wakeup_ac_out;
 				};
 
 				usb_power_supply: usb_power_supply {
 					compatible = "x-powers,axp803-usb-power-supply";
 					status = "okay";
 
-					pmu_usbpc_vol = <5000>;
-					pmu_usbpc_cur = <5000>;
-					pmu_usbad_vol = <5000>;
-					pmu_usbad_cur = <5000>;
+					pmu_usbpc_vol = <4600>;
+					pmu_usbpc_cur = <2500>;
+					pmu_usbad_vol = <4600>;
+					pmu_usbad_cur = <2500>;
 
-					wakeup_usb_in;
-					wakeup_usb_out;
+					// wakeup_usb_in;
+					// wakeup_usb_out;
 				};
 
 				battery_power_supply: battery-power-supply {
@@ -423,21 +423,21 @@
 					status = "okay";
 
 					pmu_chg_ic_temp = <0>;
-					pmu_battery_rdc= <93>;
-					pmu_battery_cap = <5000>;
+					pmu_battery_rdc= <50>;
+					pmu_battery_cap = <8000>;
 					pmu_runtime_chgcur = <1000>;
 					pmu_suspend_chgcur = <2000>;
 					pmu_shutdown_chgcur = <2000>;
-					pmu_init_chgvol = <4200>;
+					pmu_init_chgvol = <4280>;
 					pmu_battery_warning_level1 = <15>;
 					pmu_battery_warning_level2 = <0>;
 					pmu_chgled_func = <1>;
 					pmu_chgled_type = <0>;
 					ocv_coulumb_100 = <1>;
 					pmu_bat_para1 = <0>;
-					pmu_bat_para2 = <0>;
-					pmu_bat_para3 = <0>;
-					pmu_bat_para4 = <0>;
+					pmu_bat_para2 = <3>;
+					pmu_bat_para3 = <4>;
+					pmu_bat_para4 = <5>;
 					pmu_bat_para5 = <6>;
 					pmu_bat_para6 = <8>;
 					pmu_bat_para7 = <9>;
@@ -455,16 +455,16 @@
 					pmu_bat_para19 = <65>;
 					pmu_bat_para20 = <70>;
 					pmu_bat_para21 = <75>;
-					pmu_bat_para22 = <80>;
-					pmu_bat_para23 = <83>;
-					pmu_bat_para24 = <88>;
-					pmu_bat_para25 = <92>;
-					pmu_bat_para26 = <97>;
-					pmu_bat_para27 = <100>;
-					pmu_bat_para28 = <100>;
-					pmu_bat_para29 = <100>;
-					pmu_bat_para30 = <100>;
-					pmu_bat_para31 = <100>;
+					pmu_bat_para22 = <76>;
+					pmu_bat_para23 = <77>;
+					pmu_bat_para24 = <78>;
+					pmu_bat_para25 = <79>;
+					pmu_bat_para26 = <80>;
+					pmu_bat_para27 = <83>;
+					pmu_bat_para28 = <85>;
+					pmu_bat_para29 = <88>;
+					pmu_bat_para30 = <90>;
+					pmu_bat_para31 = <95>;
 					pmu_bat_para32 = <100>;
 
 					pmu_bat_temp_enable = <0>;
diff --git a/longan/device/config/chips/a133/configs/c3/sys_config.fex b/longan/device/config/chips/a133/configs/c3/sys_config.fex
index 96179b5..8aa58b3 100755
--- a/longan/device/config/chips/a133/configs/c3/sys_config.fex
+++ b/longan/device/config/chips/a133/configs/c3/sys_config.fex
@@ -57,7 +57,6 @@
 dcdc5_mode                 = 0
 dcdc2_mode                 = 0
 dcdc3_mode                 = 0
-battery_exist              = 0
 
 [card_boot]
 logical_start   = 40960
diff --git a/longan/kernel/linux-4.9/drivers/power/supply/axp803_battery.c b/longan/kernel/linux-4.9/drivers/power/supply/axp803_battery.c
index 04f6af3..5f49c60 100644
--- a/longan/kernel/linux-4.9/drivers/power/supply/axp803_battery.c
+++ b/longan/kernel/linux-4.9/drivers/power/supply/axp803_battery.c
@@ -671,8 +671,10 @@
 	}
 
 	regmap_read(map, AXP803_BATCAP0, &val);
-	if ((axp_config->pmu_battery_cap) && (!(val & 0x80))) {
-		cur_coulomb_counter = axp_config->pmu_battery_cap
+	//if ((axp_config->pmu_battery_cap) && (!(val & 0x80))) {
+    // always update from dts
+    if ((axp_config->pmu_battery_cap)) {
+        cur_coulomb_counter = axp_config->pmu_battery_cap
 					* 1000 / 1456;
 		regmap_write(map, AXP803_BATCAP0, ((cur_coulomb_counter >> 8) | 0x80));
 		regmap_write(map, AXP803_BATCAP1, cur_coulomb_counter & 0x00FF);

--
Gitblit v1.6.2