max1720x_battery: initialize outliers state from model

initialize max m5 capacity drift code from the device tree

Bug: 171770409
Test: load model, check outlier state
Signed-off-by: AleX Pelosi <[email protected]>
Change-Id: I6007e4a741d2802947a34a219e39b102bc41a166
diff --git a/max1720x_battery.c b/max1720x_battery.c
index 16f83ae..c5e14b8 100644
--- a/max1720x_battery.c
+++ b/max1720x_battery.c
@@ -826,8 +826,8 @@
 }
 
 /*
- * force is true when changing the model via debug props
- * call holding model_lock
+ * force is true when changing the model via debug props.
+ * NOTE: call holding model_lock
  */
 static void max1720x_model_reload(struct max1720x_chip *chip, bool force)
 {
@@ -869,7 +869,7 @@
 	/* overwrite with userland, will commit at cycle count */
 	ret = max_m5_model_state_sscan(chip->model_data, buf, count);
 	if (ret == 0) {
-		/* force model state */
+		/* force model state (valid) */
 		chip->model_state_valid = true;
 		max1720x_model_reload(chip, true);
 	}
@@ -2182,30 +2182,38 @@
 	return 0;
 }
 
-static struct device_node *max1720x_find_batt_node(struct device *dev,
-						   int batt_id)
+static struct device_node *max1720x_find_batt_node(struct max1720x_chip *chip)
 {
-	int ret;
-	u32 batt_id_range = 20, batt_id_kohm;
-	struct device_node *node = dev->of_node;
+	const int batt_id = chip->batt_id;
+	const struct device *dev = chip->dev;
+	const int algo_ver = chip->drift_data.algo_ver;
 	struct device_node *config_node, *child_node;
+	u32 batt_id_range = 20, algo = 0, batt_id_kohm;
+	int ret;
 
-	ret = of_property_read_u32(node, "maxim,batt-id-range-pct",
-				   &batt_id_range);
-	if (ret && ret == -EINVAL)
-		dev_warn(dev, "failed to read maxim,batt-id-range-pct\n");
-
-	config_node = of_find_node_by_name(node, "maxim,config");
+	config_node = of_find_node_by_name(dev->of_node, "maxim,config");
 	if (!config_node) {
 		dev_warn(dev, "Failed to find maxim,config setting\n");
 		return NULL;
 	}
 
+	ret = of_property_read_u32(dev->of_node, "maxim,batt-id-range-pct",
+				   &batt_id_range);
+	if (ret && ret == -EINVAL)
+		dev_warn(dev, "failed to read maxim,batt-id-range-pct\n");
+
 	for_each_child_of_node(config_node, child_node) {
+		ret = of_property_read_u32(child_node, "maxim,fg-algo", &algo);
+		if (ret < 0)
+			algo = 0;
+		if (algo_ver != algo)
+			continue;
+
 		ret = of_property_read_u32(child_node, "maxim,batt-id-kohm",
 					   &batt_id_kohm);
 		if (ret != 0)
 			continue;
+
 		if (!batt_id_range && batt_id == batt_id_kohm)
 			return child_node;
 		if ((batt_id < (batt_id_kohm * (100 + batt_id_range) / 100)) &&
@@ -2679,8 +2687,7 @@
 
 	/* ->batt_id negative for no lookup */
 	if (chip->batt_id >= 0) {
-		chip->batt_node = max1720x_find_batt_node(chip->dev,
-							  chip->batt_id);
+		chip->batt_node = max1720x_find_batt_node(chip);
 		pr_debug("node found=%d for ID=%d\n", !!chip->batt_node,
 			 chip->batt_id);
 	}
@@ -2888,16 +2895,11 @@
 	if (IS_ERR_OR_NULL(de))
 		return -ENOENT;
 
-	debugfs_create_file("irq_none_cnt", 0644, de,
-				chip, &irq_none_cnt_fops);
-	debugfs_create_file("nvram_por", 0440, de,
-				chip, &debug_nvram_por_fops);
-	debugfs_create_file("fg_reset", 0400, de,
-				chip, &debug_fg_reset_fops);
-	debugfs_create_file("ce_start", 0400, de,
-				chip, &debug_ce_start_fops);
-	debugfs_create_file("batt_id", 0600, de,
-				chip, &debug_batt_id_fops);
+	debugfs_create_file("irq_none_cnt", 0644, de, chip, &irq_none_cnt_fops);
+	debugfs_create_file("nvram_por", 0440, de, chip, &debug_nvram_por_fops);
+	debugfs_create_file("fg_reset", 0400, de, chip, &debug_fg_reset_fops);
+	debugfs_create_file("ce_start", 0400, de, chip, &debug_ce_start_fops);
+	debugfs_create_file("batt_id", 0600, de, chip, &debug_batt_id_fops);
 
 	if (chip->regmap.reglog)
 		debugfs_create_file("regmap_writes", 0440, de,
@@ -2913,6 +2915,9 @@
 		debugfs_create_file("fg_model", 0440, de, chip,
 				    &debug_m5_custom_model_fops);
 
+	/* capacity drift fixup, one of MAX1720X_DA_VER_* */
+	debugfs_create_u32("algo_ver", 0644, de, &chip->drift_data.algo_ver);
+
 	return 0;
 }
 
@@ -3131,6 +3136,13 @@
 		return -EAGAIN;
 	}
 
+	/* fix capacity outliers algo */
+	ret = max_m5_fixup_outliers(&chip->drift_data, chip->model_data);
+	if (ret < 0)
+		dev_err(chip->dev, "%s: error fixing drift data rc=%d\n",
+			__func__, ret);
+
+
 	chip->reg_prop_capacity_raw = MAX1720X_REPSOC;
 	chip->model_state_valid = true;