diff --git a/docs/FINDINGS.md b/docs/FINDINGS.md index ee0b6959..15ec8f43 100644 --- a/docs/FINDINGS.md +++ b/docs/FINDINGS.md @@ -2341,7 +2341,39 @@ first build: - `sc27xx-fgu/capacity` read 575. The FGU's always-on user area keeps the last capacity across a reboot, and Android keeps it in 0.1 %. Until then the status had been Full, and the calibration that runs whenever the battery is not charging forced anything above 100 back to 100. Patch 0011 reads and saves that value in 0.1 % on - the UMP9620, and clamps the reported capacity to 0-100. + the UMP9620, and clamps the reported capacity to 0-100. (Its format was wrong: see 2026-10-07 below.) + +2026-10-07, U30 Air under 7.2.9 with 0009-0011: charged from about 60 % (Android said 65 % at 4.04 V under 1.3 A) for +about 1.5 h at 1.1-1.3 A. Then bq256xx-charger said Full (charge_type Trickle), and the battery rested at 4.146 V with ++2 mA, so it was physically full. `sc27xx-fgu` said status Full and capacity 58, and over the whole charge the capacity +had read 57.5, 59 and 58. Two faults in the driver, and a third that is still open: + +- The user-area format. Unisoc's 5.4 driver (`sc27xx_fgu_save_last_cap()`/`read_last_cap()`, `FCC_PERCENT` 1000) + keeps the capacity as whole percent in bits 7:0 and tenths in bits 11:8. That makes 575 (0x23f) 63.2 %, close to + Android's 65 %, not 57.5 %. Android's own capacity is in 0.1 % internally (its `sc27xx-fgu` capacity read 615 when `battery` said 65), but that is not + what it stores. The 0.1 % that 0011 wrote back would read as something else under Android (580 = 0x244 = 68.2 %). + Mainline's arithmetic is in whole percent throughout (`init_cap`, `ocv2cap`, the delta from the coulomb count over + `total_cap`), and 0011 had converted at the user area, so the units inside the driver agreed. The value read was + the wrong one. +- No calibration at the end of a charge. Mainline sets 100 % only when the OCV is above the top of the OCV table. + The charger ends at VREG 4.208 V, below the top of the table (4.3 V and up for these cells), so with Full and 4.146 V + nothing moved the 58. Mainline also writes the user area only on a first power-on, so every boot started again + from the value Android had left. +- Open: the coulomb count should have added about 40 % (1.6-1.8 Ah of a 4050 mAh cell). Its arithmetic matches the + vendor's (`clbcnt * 10 / 72 / cur_1000ma_adc` mAh, the same registers, 2 Hz) and does not overflow at these values, + and nothing calibrates while the status is Charging. A reboot during the charge would explain the 59 -> 58 step + (each boot restarted from the stale saved value), but not the small rise. `charge_now` (uAh) is the raw counter: + it should rise by about 1.2 Ah per hour at 1.2 A. If it does not, the UMP9620's counter needs something the + driver does not do. + +Patch 0011 is replaced (`0011-power-sc27xx-fuel-gauge-UMP9620-saved-capacity-and-full.patch`). On UMP9620 it reads +the saved value in Android's format and writes whole percent, which is the same number in both formats. A value that +is not a capacity in that format goes to the OCV. So does a multiple of 10 with tenths, which the first 0011 may have +written: the OCV table decides it right away with the battery at rest (under 50 mA), and under load the value is taken +in Android's format. Either way dmesg says `boot capacity N % (saved 0x...)` or `... from the OCV`. The capacity is +written to the user area whenever its whole percent changes. A Full from a charger driver (not 0006's guess from the +current) sets 100 % while the OCV table puts the battery at 70 % or more. The reported value stays within 0-100. Built +for 6.18.55 and 7.2.9. Not yet tested on the device. ### 33e. USB host on the U30 Air, and a trial guard that outlived its trial diff --git a/tests/test_static.py b/tests/test_static.py index f3490fb0..7e44e1b9 100644 --- a/tests/test_static.py +++ b/tests/test_static.py @@ -312,11 +312,24 @@ class Rules(unittest.TestCase): self.assertIn(r"#define SPRD_UMP9620_CHG_DET\t\t0x239c", port) self.assertIn(r"'\t.charger_det = SPRD_SC2730_CHG_DET,\n};\n', UMP9620_DATA)", port) self.assertIn("'.charger_det = SPRD_UMP9620_CHG_DET,'", port) - # Android's last capacity in the FGU's user area is in 0.1 % (575 read as 575 %), and capacity stays 0-100 - p11 = (TOP / 'upstream' / 'patches' / '0011-power-sc27xx-fuel-gauge-UMP9620-capacity-in-tenths.patch').read_text() - for s in ('+\t\tcap = clamp(cap, 0, 100) * 10;', '+\t\t*cap = min(DIV_ROUND_CLOSEST(*cap, 10), 100);', + # Android's last capacity in the FGU's user area is whole percent in bits 7:0 and tenths in bits 11:8 (575 = + # 0x23f = 63.2 %), not 0.1 %; mainline writes whole percent; an ambiguous or invalid value goes to the OCV; the + # charger's Full (from a charger driver) means 100 %; the capacity is saved as it changes and stays 0-100 + patches = TOP / 'upstream' / 'patches' + self.assertFalse((patches / '0011-power-sc27xx-fuel-gauge-UMP9620-capacity-in-tenths.patch').exists()) + p11 = (patches / '0011-power-sc27xx-fuel-gauge-UMP9620-saved-capacity-and-full.patch').read_text() + for s in ('+#define UMP9620_FGU_CAP_INTEGER_MASK\tGENMASK(7, 0)', '+#define UMP9620_FGU_CAP_DECIMAL_MASK\tGENMASK(11, 8)', + '+\t\tcap = clamp(cap, 0, 100);', '+\tvalid = whole <= 100 && tenths <= 9 && !(whole == 100 && tenths);', + '+\tambiguous = tenths && value <= 1000 && !(value % 10);', + '+\tif (valid && (!ambiguous || abs(cur) >= UMP9620_FGU_RELAXED_MA)) {', + '+\t*cap = power_supply_ocv2cap_simple(data->cap_table, data->table_len, ocv);', + '+\tif (!is_first_poweron && data->var == &ump9620_info)', + '+\tif (data->var == &ump9620_info && chg_sts == POWER_SUPPLY_STATUS_FULL && cap != 100 &&', + '+\t sc27xx_fgu_has_charger() &&', '+\t\tsc27xx_fgu_adjust_cap(data, 100);', + '+\t\tif (*cap != data->saved_cap && sc27xx_fgu_save_last_cap(data, *cap))', '+\t\tval->intval = clamp(value, 0, 100);'): self.assertIn(s, p11) + self.assertNotIn('* 10;', p11) # mu300-usb reaches the chip past the driver and leaves its watchdog off then usb = (BIN / 'mu300-usb').read_text() self.assertIn('[ -e "$R/sys/bus/i2c/devices/$BUS-006b/driver" ] && FORCE=-f', usb) diff --git a/upstream/patches/0011-power-sc27xx-fuel-gauge-UMP9620-capacity-in-tenths.patch b/upstream/patches/0011-power-sc27xx-fuel-gauge-UMP9620-capacity-in-tenths.patch deleted file mode 100644 index 1af21a7c..00000000 --- a/upstream/patches/0011-power-sc27xx-fuel-gauge-UMP9620-capacity-in-tenths.patch +++ /dev/null @@ -1,41 +0,0 @@ -power: supply: sc27xx_fuel_gauge: UMP9620's saved capacity is in 0.1 %, and capacity stays within 0-100 - -The FGU's always-on user area keeps the last capacity across a reboot, and the boot after Android reads it back. -Android keeps it there in 0.1 %: on the U30 Air the first mainline boot with a charger driver (patch 0009) read -575 and reported capacity 575. Until then the status was Full (0006's fallback), and the calibration that runs -outside charging forced anything above 100 back to 100, so nobody saw it. On UMP9620 the value is now read and -saved in 0.1 %, and the reported capacity is clamped to 0-100: while charging nothing calibrates the coulomb count. - ---- a/drivers/power/supply/sc27xx_fuel_gauge.c -+++ b/drivers/power/supply/sc27xx_fuel_gauge.c -@@ -275,6 +275,10 @@ - */ - udelay(200); - -+ /* UMP9620: Android keeps the capacity here in 0.1 % (575 at 57.5 %); so does this driver */ -+ if (data->var == &ump9620_info) -+ cap = clamp(cap, 0, 100) * 10; -+ - ret = regmap_update_bits(data->regmap, - data->base + SC27XX_FGU_USER_AREA_SET, - SC27XX_FGU_CAP_AREA_MASK, cap); -@@ -309,6 +313,9 @@ - return ret; - - *cap = value & SC27XX_FGU_CAP_AREA_MASK; -+ /* UMP9620: in 0.1 % (see sc27xx_fgu_save_last_cap()) */ -+ if (data->var == &ump9620_info) -+ *cap = min(DIV_ROUND_CLOSEST(*cap, 10), 100); - return 0; - } - -@@ -702,7 +709,8 @@ - if (ret) - goto error; - -- val->intval = value; -+ /* the coulomb count can run past either end while charging, when nothing calibrates it */ -+ val->intval = clamp(value, 0, 100); - break; - - case POWER_SUPPLY_PROP_VOLTAGE_AVG: diff --git a/upstream/patches/0011-power-sc27xx-fuel-gauge-UMP9620-saved-capacity-and-full.patch b/upstream/patches/0011-power-sc27xx-fuel-gauge-UMP9620-saved-capacity-and-full.patch new file mode 100644 index 00000000..45a6aaca --- /dev/null +++ b/upstream/patches/0011-power-sc27xx-fuel-gauge-UMP9620-saved-capacity-and-full.patch @@ -0,0 +1,249 @@ +power: supply: sc27xx_fuel_gauge: UMP9620's saved capacity in Android's format, 100 % on the charger's Full + +The FGU's always-on user area keeps the last capacity across a reboot. Unisoc's 5.4 driver (Android on the U30 Air) +keeps it as whole percent in bits 7:0 and tenths in bits 11:8 (sc27xx_fgu_save_last_cap()/read_last_cap() there): +the 575 (0x23f) that the first mainline boot with a charger driver read as 575 % is 63.2 %, with Android at 65 %. +The first version of this patch took it for 0.1 % (57.5 %) and wrote 0.1 % back, which Android reads wrongly +(580 = 0x244 = 68.2 %). On UMP9620 now: + +- a saved value is read in Android's format, and written as whole percent (which is the same number in both + formats). One that is no capacity in that format, or one that the first version may have written (a multiple of + 10 with tenths), is decided by the open-circuit voltage from the battery node's OCV table: right away with the + battery at rest (under 50 mA); under load an ambiguous value is taken in Android's format +- the capacity is written to the user area whenever its whole percent changes, as Android does: mainline wrote it + at a first power-on only, so every boot restarted from the value Android had left +- the charger's Full (from a charger driver, not 0006's guess from the current) sets the capacity to 100 % while + the OCV table puts the battery at 70 % or more: the U30 Air's charger ends a charge at 4.208 V, below the top of + the OCV table, so mainline's calibration (OCV above the table's top) never did. After a 1.5 h charge at 1.2 A the + battery rested at 4.146 V with Full from bq256xx, and the capacity still said 58 +- the reported capacity stays within 0-100: while charging nothing calibrates the coulomb count + +--- a/drivers/power/supply/sc27xx_fuel_gauge.c ++++ b/drivers/power/supply/sc27xx_fuel_gauge.c +@@ -67,6 +67,19 @@ + #define SC27XX_FGU_DEFAULT_CAP GENMASK(11, 0) + #define SC27XX_FGU_NORMAIL_POWERTON 0x5 + ++/* ++ * UMP9620: Unisoc's 5.4 driver (Android on the U30 Air) keeps the capacity in the user area as whole percent in ++ * bits 7:0 and tenths in bits 11:8: 0x23f is 63.2 %. A whole percent with no tenths is the plain number, as ++ * mainline writes it. ++ */ ++#define UMP9620_FGU_CAP_INTEGER_MASK GENMASK(7, 0) ++#define UMP9620_FGU_CAP_DECIMAL_MASK GENMASK(11, 8) ++#define UMP9620_FGU_CAP_DECIMAL_SHIFT 8 ++/* below this current (mA) the battery is at rest, and its voltage is its open-circuit voltage */ ++#define UMP9620_FGU_RELAXED_MA 50 ++/* the charger's Full sets 100 % only with the OCV table at this capacity or more */ ++#define UMP9620_FGU_FULL_MIN_CAP 70 ++ + #define SC27XX_FGU_CUR_BASIC_ADC 8192 + #define SC27XX_FGU_SAMPLE_HZ 2 + /* micro Ohms */ +@@ -151,6 +164,8 @@ + int cur_1000ma_adc; + int vol_1000mv_adc; + int calib_resist; ++ /* UMP9620: the whole percent last written to the user area, -1 for none */ ++ int saved_cap; + struct power_supply_battery_ocv_table *cap_table; + struct power_supply_resistance_temp_table *resist_table; + }; +@@ -160,6 +175,8 @@ + int cap, bool int_mode); + static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data *data, int cap); + static int sc27xx_fgu_get_temp(struct sc27xx_fgu_data *data, int *temp); ++static int sc27xx_fgu_get_current(struct sc27xx_fgu_data *data, int *val); ++static int sc27xx_fgu_get_vbat_ocv(struct sc27xx_fgu_data *data, int *val); + + static const char * const sc27xx_charger_supply_name[] = { + "sc2731_charger", +@@ -275,6 +292,10 @@ + */ + udelay(200); + ++ /* UMP9620: whole percent (no tenths), which Android reads as it reads its own */ ++ if (data->var == &ump9620_info) ++ cap = clamp(cap, 0, 100); ++ + ret = regmap_update_bits(data->regmap, + data->base + SC27XX_FGU_USER_AREA_SET, + SC27XX_FGU_CAP_AREA_MASK, cap); +@@ -294,9 +315,12 @@ + * make the user area data available, otherwise we can not save the user + * area data. + */ +- return regmap_update_bits(data->regmap, +- data->base + SC27XX_FGU_USER_AREA_CLEAR, +- SC27XX_FGU_CAP_AREA_MASK, 0); ++ ret = regmap_update_bits(data->regmap, ++ data->base + SC27XX_FGU_USER_AREA_CLEAR, ++ SC27XX_FGU_CAP_AREA_MASK, 0); ++ if (!ret) ++ data->saved_cap = cap; ++ return ret; + } + + static int sc27xx_fgu_read_last_cap(struct sc27xx_fgu_data *data, int *cap) +@@ -318,6 +342,58 @@ + * calculate the battery open circuit voltage, and get current battery + * capacity according to the capacity table. + */ ++/* ++ * UMP9620, not a first power-on: the capacity Android (or this driver) saved, in Android's format. The first ++ * version of patch 0011 saved it in 0.1 % (580 for 58 %), which can read as a valid value in that format too ++ * (0x244 = 68.2 %): such a value, or one that is no capacity at all, is decided by the open-circuit voltage ++ * instead, from the battery node's OCV table - right away when the battery is at rest (under 50 mA). An ambiguous ++ * value with the battery under load is taken in Android's format. ++ */ ++static int ump9620_fgu_get_boot_capacity(struct sc27xx_fgu_data *data, int *cap) ++{ ++ int ret, value, whole, tenths, cur = 0, ocv; ++ bool valid, ambiguous; ++ ++ ret = regmap_read(data->regmap, ++ data->base + SC27XX_FGU_USER_AREA_STATUS, &value); ++ if (ret) ++ return ret; ++ ++ value &= SC27XX_FGU_CAP_AREA_MASK; ++ whole = value & UMP9620_FGU_CAP_INTEGER_MASK; ++ tenths = (value & UMP9620_FGU_CAP_DECIMAL_MASK) >> UMP9620_FGU_CAP_DECIMAL_SHIFT; ++ valid = whole <= 100 && tenths <= 9 && !(whole == 100 && tenths); ++ ambiguous = tenths && value <= 1000 && !(value % 10); ++ ++ if (ambiguous) { ++ ret = sc27xx_fgu_get_current(data, &cur); ++ if (ret) ++ return ret; ++ } ++ ++ if (valid && (!ambiguous || abs(cur) >= UMP9620_FGU_RELAXED_MA)) { ++ *cap = DIV_ROUND_CLOSEST(whole * 10 + tenths, 10); ++ data->saved_cap = *cap; ++ dev_info(data->dev, "boot capacity %d %% (saved 0x%03x)\n", *cap, value); ++ return sc27xx_fgu_save_boot_mode(data, SC27XX_FGU_NORMAIL_POWERTON); ++ } ++ ++ ret = sc27xx_fgu_get_vbat_ocv(data, &ocv); ++ if (ret) ++ return ret; ++ ++ data->boot_volt = ocv; ++ *cap = power_supply_ocv2cap_simple(data->cap_table, data->table_len, ocv); ++ dev_info(data->dev, "boot capacity %d %% from the OCV (%d uV), saved 0x%03x is %s\n", *cap, ocv, value, ++ valid ? "ambiguous" : "no capacity"); ++ ++ ret = sc27xx_fgu_save_last_cap(data, *cap); ++ if (ret) ++ return ret; ++ ++ return sc27xx_fgu_save_boot_mode(data, SC27XX_FGU_NORMAIL_POWERTON); ++} ++ + static int sc27xx_fgu_get_boot_capacity(struct sc27xx_fgu_data *data, int *cap) + { + int volt, cur, oci, ocv, ret; +@@ -328,6 +404,9 @@ + * battery capacity as the initial battery capacity. Otherwise we should + * re-calculate the initial battery capacity. + */ ++ if (!is_first_poweron && data->var == &ump9620_info) ++ return ump9620_fgu_get_boot_capacity(data, cap); ++ + if (!is_first_poweron) { + ret = sc27xx_fgu_read_last_cap(data, cap); + if (ret) +@@ -458,7 +537,7 @@ + + static int sc27xx_fgu_get_capacity(struct sc27xx_fgu_data *data, int *cap) + { +- int ret, cur_clbcnt, delta_clbcnt, delta_cap, temp; ++ int ret, cur_clbcnt, delta_clbcnt, delta_cap, temp, init_clbcnt; + + /* Get current coulomb counters firstly */ + ret = sc27xx_fgu_get_clbcnt(data, &cur_clbcnt); +@@ -482,7 +561,21 @@ + *cap = delta_cap + data->init_cap; + + /* Calibrate the battery capacity in a normal range. */ ++ init_clbcnt = data->init_clbcnt; + sc27xx_fgu_capacity_calibration(data, *cap, false); ++ ++ /* ++ * UMP9620: a calibration counts from now (the charger's Full: 100), and the user area keeps the capacity, as ++ * Android's does, for the next boot of either system; mainline wrote it at a first power-on only, so every boot ++ * started from the capacity Android had left. ++ */ ++ if (data->var == &ump9620_info) { ++ if (data->init_clbcnt != init_clbcnt) ++ *cap = data->init_cap; ++ *cap = clamp(*cap, 0, 100); ++ if (*cap != data->saved_cap && sc27xx_fgu_save_last_cap(data, *cap)) ++ dev_err(data->dev, "failed to save battery capacity\n"); ++ } + + return 0; + } +@@ -702,7 +795,8 @@ + if (ret) + goto error; + +- val->intval = value; ++ /* the coulomb count can run past either end while charging, when nothing calibrates it */ ++ val->intval = clamp(value, 0, 100); + break; + + case POWER_SUPPLY_PROP_VOLTAGE_AVG: +@@ -866,6 +960,22 @@ + dev_err(data->dev, "failed to get init coulomb counter\n"); + } + ++/* whether one of the chargers is there to give the status (0006 guesses it from the current otherwise) */ ++static bool sc27xx_fgu_has_charger(void) ++{ ++ struct power_supply *psy; ++ int i; ++ ++ for (i = 0; i < ARRAY_SIZE(sc27xx_charger_supply_name); i++) { ++ psy = power_supply_get_by_name(sc27xx_charger_supply_name[i]); ++ if (psy) { ++ power_supply_put(psy); ++ return true; ++ } ++ } ++ return false; ++} ++ + static void sc27xx_fgu_capacity_calibration(struct sc27xx_fgu_data *data, + int cap, bool int_mode) + { +@@ -888,7 +998,20 @@ + * lower capacity. + */ + if (chg_sts == POWER_SUPPLY_STATUS_CHARGING) ++ return; ++ ++ /* ++ * UMP9620: the U30 Air's charger ends a charge at its 4.208 V, below the top of the battery's OCV table, so ++ * the OCV never reaches 100 % below. The charger's Full does (not 0006's guess from the current: the status ++ * comes from a charger driver), unless the OCV says the battery is far from full. ++ */ ++ if (data->var == &ump9620_info && chg_sts == POWER_SUPPLY_STATUS_FULL && cap != 100 && ++ sc27xx_fgu_has_charger() && ++ power_supply_ocv2cap_simple(data->cap_table, data->table_len, ocv) >= UMP9620_FGU_FULL_MIN_CAP) { ++ dev_info(data->dev, "charge full: capacity %d %% -> 100 %%\n", cap); ++ sc27xx_fgu_adjust_cap(data, 100); + return; ++ } + + if ((ocv > data->cap_table[0].ocv && cap < 100) || cap > 100) { + /* +@@ -1298,6 +1421,7 @@ + } + + data->bat_present = !!ret; ++ data->saved_cap = -1; + mutex_init(&data->lock); + data->dev = dev; + platform_set_drvdata(pdev, data);