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

Commit f0cbe745 authored by Subbaraman Narayanamurthy's avatar Subbaraman Narayanamurthy
Browse files

of_batterydata: Improve the selection of battery profile



Currently, the best battery profile is selected only based on the
profile which has the lowest delta from the battery id. For
example, if there are two profiles and if the second one has
lowest delta with the battery id, it would get selected as best
profile. However, the selection may not go through as that
profile can be outside the range. First profile whose battery id
delta is greater than the second profile but still in the valid
range will not be selected because the second profile was given
the precedence.

Fix this problem by validating whether the battery id of the
profile is within range and update the best profile based on
that.

CRs-Fixed: 755238
Change-Id: Ife0ad0e4d68d8e096328e1e1e6a3abfd73ab7408
Signed-off-by: default avatarSubbaraman Narayanamurthy <subbaram@codeaurora.org>
parent 47aaacc8
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);