Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 49dc9172 authored by Linux Build Service Account's avatar Linux Build Service Account Committed by Gerrit - the friendly Code Review server
Browse files

Merge "of_batterydata: Improve the selection of battery profile"

parents b14e5f4b f0cbe745
Loading
Loading
Loading
Loading
+30 −18
Original line number Diff line number Diff line
@@ -316,7 +316,8 @@ struct device_node *of_batterydata_get_best_profile(
	const char *battery_type = NULL;
	union power_supply_propval ret = {0, };
	int delta = 0, best_delta = 0, best_id_kohm = 0, id_range_pct,
		batt_id_kohm = 0, i = 0, rc = 0;
		batt_id_kohm = 0, i = 0, rc = 0, limit = 0;
	bool in_range = false;

	psy = power_supply_get_by_name(psy_name);
	if (!psy) {
@@ -332,6 +333,19 @@ struct device_node *of_batterydata_get_best_profile(

	batt_id_kohm = ret.intval / 1000;

	/* read battery id range percentage for best profile */
	rc = of_property_read_u32(batterydata_container_node,
			"qcom,batt-id-range-pct", &id_range_pct);

	if (rc) {
		if (rc == -EINVAL) {
			id_range_pct = 0;
		} else {
			pr_err("failed to read battery id range\n");
			return ERR_PTR(-ENXIO);
		}
	}

	/*
	 * Find the battery data with a battery id resistor closest to this one
	 */
@@ -352,7 +366,15 @@ struct device_node *of_batterydata_get_best_profile(
				continue;
			for (i = 0; i < batt_ids.num; i++) {
				delta = abs(batt_ids.kohm[i] - batt_id_kohm);
				if (delta < best_delta || !best_node) {
				limit = (batt_ids.kohm[i] * id_range_pct) / 100;
				in_range = (delta <= limit);
				/*
				 * Check if the delta is the lowest one
				 * and also if the limits are in range
				 * before selecting the best node.
				 */
				if ((delta < best_delta || !best_node)
					&& in_range) {
					best_node = node;
					best_delta = delta;
					best_id_kohm = batt_ids.kohm[i];
@@ -366,10 +388,6 @@ struct device_node *of_batterydata_get_best_profile(
		return best_node;
	}

	/* read battery id value for best profile */
	rc = of_property_read_u32(batterydata_container_node,
			"qcom,batt-id-range-pct", &id_range_pct);
	if (!rc) {
	/* check that profile id is in range of the measured batt_id */
	if (abs(best_id_kohm - batt_id_kohm) >
			((best_id_kohm * id_range_pct) / 100)) {
@@ -377,12 +395,6 @@ struct device_node *of_batterydata_get_best_profile(
			best_id_kohm, batt_id_kohm, id_range_pct);
		return NULL;
	}
	} else if (rc == -EINVAL) {
		rc = 0;
	} else {
		pr_err("failed to read battery id range\n");
		return ERR_PTR(-ENXIO);
	}

	rc = of_property_read_string(best_node, "qcom,battery-type",
							&battery_type);