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

Commit f2fb0ee7 authored by Harry Yang's avatar Harry Yang
Browse files

of_batterydata: add snapshot of of_batterydata library



Snapshot of of_batterydata library as of kernel 4.9
'commit fc9ce0e0a100 ("Merge changes  into msm-4.9")'.

Change-Id: Id6fcc2c566fc0d7a56a2dc05a6990b7eb2430b45
Signed-off-by: default avatarHarry Yang <harryy@codeaurora.org>
parent e798db80
Loading
Loading
Loading
Loading
+268 −0
Original line number Diff line number Diff line
Battery Profile Data

Battery Data is a collection of battery profile data made available to
the QPNP Charger and BMS drivers via device tree.

qcom,battery-data node required properties:
- qcom,rpull-up-kohm : The vadc pullup resistor's resistance value in kOhms.
- qcom,vref-batt-therm-uv : The vadc voltage used to make readings.
			For Qualcomm Technologies, Inc. VADCs, this should be
			1800000uV.

qcom,battery-data node optional properties:
- qcom,batt-id-range-pct : The area of variation between upper and lower bound
			for which a given battery ID resistance is valid. This
			value is expressed as a percentage of the specified kohm
			resistance provided by qcom,batt-id-kohm.

qcom,battery-data can also include any number of children nodes. These children
nodes will be treated as battery profile data nodes.

Profile data node required properties:
- qcom,fcc-mah : Full charge count of the battery in milliamp-hours
- qcom,default-rbatt-mohm : The nominal battery resistance value
- qcom,rbatt-capacitive-mohm : The capacitive resistance of the battery.
- qcom,flat-ocv-threshold-uv : The threshold under which the battery can be
			considered to be in the flat portion of the discharge
			curve.
- qcom,max-voltage-uv : The maximum rated voltage of the battery
- qcom,v-cutoff-uv : The cutoff voltage of the battery at which the device
			should shutdown gracefully.
- qcom,chg-term-ua : The termination charging current of the battery.
- qcom,batt-id-kohm : The battery id resistance of the battery. It can be
			used as an array which could support multiple IDs for one battery
			module when the ID resistance of some battery modules goes across
			several ranges.
- qcom,battery-type : A string indicating the type of battery.
- qcom,fg-profile-data : An array of hexadecimal values used to configure more
			complex fuel gauge peripherals which have a large amount
			of coefficients used in hardware state machines and thus
			influencing the final output of the state of charge read
			by software.

Profile data node optional properties:
- qcom,chg-rslow-comp-c1 : A constant for rslow compensation in the fuel gauge.
			This will be provided by the profiling tool for
			additional fuel gauge accuracy during charging.
- qcom,chg-rslow-comp-c2 : A constant for rslow compensation in the fuel gauge.
			This will be provided by the profiling tool for
			additional fuel gauge accuracy during charging.
- qcom,chg-rslow-comp-thr : A constant for rslow compensation in the fuel gauge.
			This will be provided by the profiling tool for
			additional fuel gauge accuracy during charging.
- qcom,chg-rs-to-rslow: A constant for rslow compensation in the fuel gauge.
			This will be provided by the profiling tool for
			additional fuel gauge accuracy during charging.
- qcom,fastchg-current-ma: Specifies the maximum fastcharge current.
- qcom,fg-cc-cv-threshold-mv: Voltage threshold in mV for transition from constant
			charge (CC) to constant voltage (CV). This value should
			be 10 mV less than the float voltage.
			This property should only be specified if
			"qcom,autoadjust-vfloat" property is specified in the
			charger driver to ensure a proper operation.
- qcom,thermal-coefficients: Byte array of thermal coefficients for reading
			battery thermistor. This should be exactly 6 bytes
			in length.
			Example: [01 02 03 04 05 06]
- qcom,soc-based-step-chg: A bool property to indicate if the battery will
			perform SoC (State of Charge) based step charging.
			If yes, the low and high thresholds defined in
			"qcom,step-chg-ranges" tuples should be assigned as
			SoC values in percentage.
- qcom,step-chg-ranges: Array of tuples in which a tuple describes a range
			data of step charging setting.
			A range contains following 3 integer elements:
			[0]: the low threshold of battery votlage in uV
			     or SoC (State of Charge) in percentage when
			     SoC based step charge is used;
			[1]: the high threshold of battery voltage in uV
			     or SoC in percentage when SoC based step charge
			     is used;
			[2]: the FCC (full charging current) in uA when battery
			     voltage or SoC falls between the low and high
			     thresholds.
			The threshold values in range should be in ascending
			and shouldn't overlap. It support 8 ranges at max.
- qcom,jeita-fcc-ranges: Array of tuples in which a tuple describes a range
			data of sw-jeita FCC (full charging current) setting.
			A range contains following 3 integer elements:
			[0]: the low threshold of battery temperature in deci-degree;
			[1]: the high threshold of battery temperature in deci-degree;
			[2]: the FCC in uA when battery temperature falls between
			     the low and high thresholds.
			The threshold values in range should be in ascending
			and shouldn't overlap. It support 8 ranges at max.
- qcom,jeita-fv-ranges: Array of tuples in which a tuple describes a range
			data of sw-jeita FV (float voltage) setting.
			A range contains following 3 integer elements:
			[0]: the low threshold of battery temperature in deci-degree;
			[1]: the high threshold of battery temperature in deci-degree;
			[3]: the FV in uV when battery temperature falls between
			     the low and high thresholds.
			The threshold values in range should be in ascending
			and shouldn't overlap. It support 8 ranges at max.

Profile data node required subnodes:
- qcom,fcc-temp-lut : An 1-dimensional lookup table node that encodes
			temperature to fcc lookup. The units for this lookup
			table should be degrees celsius to milliamp-hours.
- qcom,pc-temp-ocv-lut : A 2-dimensional lookup table node that encodes
			temperature and percent charge to open circuit voltage
			lookup. The units for this lookup table should be
			degrees celsius and percent to millivolts.
- qcom,rbatt-sf-lut : A 2-dimentional lookup table node that encodes
			temperature and percent charge to battery internal
			resistance lookup. The units for this lookup table
			should be degrees celsius and percent to milliohms.

Profile data node optional subnodes:
- qcom,ibat-acc-luit: A 2-dimentional lookup table that encodes temperature
			and battery current to battery ACC (apparent charge
			capacity). The units for this lookup table should be
			temperature in degrees celsius, ibat in milli-amps
			and ACC in milli-ampere-hour.

Lookup table required properties:
- qcom,lut-col-legend : An array that encodes the legend of the lookup table's
			columns. The length of this array will determine the
			lookup table's width.
- qcom,lut-data : An array that encodes the lookup table's data. The size of this
			array should be equal to the size of qcom,lut-col-legend
			multiplied by 1 if it's a 1-dimensional table, or
			the size of qcom,lut-row-legend if it's a 2-dimensional
			table. The data should be in a flattened row-major
			representation.

Lookup table optional properties:
- qcom,lut-row-legend : An array that encodes the legend of the lookup table's rows.
			If this property exists, then it is assumed that the
			lookup table is a 2-dimensional table.

Example:

In msm8974-mtp.dtsi:

mtp_batterydata: qcom,battery-data {
	qcom,rpull-up-kohm = <100>;
	qcom,vref-batt-therm-uv = <1800000>;

	/include/ "batterydata-palladium.dtsi"
	/include/ "batterydata-mtp-3000mah.dtsi"
};

&pm8941_bms {
	qcom,battery-data = <&mtp_batterydata>;
};

In batterydata-palladium.dtsi:

qcom,palladium-batterydata {
	qcom,fcc-mah = <1500>;
	qcom,default-rbatt-mohm = <236>;
	qcom,rbatt-capacitive-mohm = <50>;
	qcom,flat-ocv-threshold-uv = <3800000>;
	qcom,max-voltage-uv = <4200000>;
	qcom,v-cutoff-uv = <3400000>;
	qcom,chg-term-ua = <100000>;
	qcom,batt-id-kohm = <75>;
	qcom,step-chg-ranges = <3600000 4000000 3000000
				4001000 4200000 2800000
				4201000 4400000 2000000>;
	qcom,jeita-fcc-ranges = <0      100     600000
				 101    200     2000000
				 201    450     3000000
				 451    550     600000>;
	qcom,jeita-fv-ranges = <0      100     4200000
				101    450     4350000
				451    550     4200000>;
	qcom,battery-type = "palladium_1500mah";

	qcom,fcc-temp-lut {
		qcom,lut-col-legend = <(-20) 0 25 40 65>;
		qcom,lut-data = <1492 1492 1493 1483 1502>;
	};

	qcom,pc-temp-ocv-lut {
		qcom,lut-col-legend = <(-20) 0 25 40 65>;
		qcom,lut-row-legend = <100 95 90 85 80 75 70>,
				<65 60 55 50 45 40 35>,
				<30 25 20 15 10 9 8>,
				<7 6 5 4 3 2 1 0>;
		qcom,lut-data = <4173 4167 4163 4156 4154>,
			<4104 4107 4108 4102 4104>,
			<4057 4072 4069 4061 4060>,
			<3973 4009 4019 4016 4020>,
			<3932 3959 3981 3982 3983>,
			<3899 3928 3954 3950 3950>,
			<3868 3895 3925 3921 3920>,
			<3837 3866 3898 3894 3892>,
			<3812 3841 3853 3856 3862>,
			<3794 3818 3825 3823 3822>,
			<3780 3799 3804 3804 3803>,
			<3768 3787 3790 3788 3788>,
			<3757 3779 3778 3775 3776>,
			<3747 3772 3771 3766 3765>,
			<3736 3763 3766 3760 3746>,
			<3725 3749 3756 3747 3729>,
			<3714 3718 3734 3724 3706>,
			<3701 3703 3696 3689 3668>,
			<3675 3695 3682 3675 3662>,
			<3670 3691 3680 3673 3661>,
			<3661 3686 3679 3672 3656>,
			<3649 3680 3676 3669 3641>,
			<3633 3669 3667 3655 3606>,
			<3610 3647 3640 3620 3560>,
			<3580 3607 3596 3572 3501>,
			<3533 3548 3537 3512 3425>,
			<3457 3468 3459 3429 3324>,
			<3328 3348 3340 3297 3172>,
			<3000 3000 3000 3000 3000>;
	};

	qcom,rbatt-sf-lut {
		qcom,lut-col-legend = <(-20) 0 25 40 65>;
		qcom,lut-row-legend = <100 95 90 85 80 75 70>,
				<65 60 55 50 45 40 35>,
				<30 25 20 15 10 9 8>,
				<7 6 5 4 3 2 1 0>;
		qcom,lut-data = <357 187 100 91 91>,
			<400 208 105 94 94>,
			<390 204 106 95 96>,
			<391 201 108 98 98>,
			<391 202 110 98 100>,
			<390 200 110 99 102>,
			<389 200 110 99 102>,
			<393 202 101 93 100>,
			<407 205 99 89 94>,
			<428 208 100 91 96>,
			<455 212 102 92 98>,
			<495 220 104 93 101>,
			<561 232 107 95 102>,
			<634 245 112 98 98>,
			<714 258 114 98 98>,
			<791 266 114 97 100>,
			<871 289 108 95 97>,
			<973 340 124 108 105>,
			<489 241 109 96 99>,
			<511 246 110 96 99>,
			<534 252 111 95 98>,
			<579 263 112 96 96>,
			<636 276 111 95 97>,
			<730 294 109 96 99>,
			<868 328 112 98 104>,
			<1089 374 119 101 115>,
			<1559 457 128 105 213>,
			<12886 1026 637 422 3269>,
			<170899 127211 98968 88907 77102>;
	};

	qcom,ibat-acc-lut {
		qcom,lut-col-legend = <(-20) 0 25>;
		qcom,lut-row-legend = <0 250 500 1000>;
		qcom,lut-data = <1470 1470 1473>,
				<1406 1406 1430>,
				<1247 1247 1414>,
				<764 764 1338>;
	};
};
+5 −0
Original line number Diff line number Diff line
@@ -112,4 +112,9 @@ config OF_OVERLAY
config OF_NUMA
	bool

config OF_BATTERYDATA
	def_bool y
	help
	  OpenFirmware BatteryData accessors

endif # OF
+1 −0
Original line number Diff line number Diff line
@@ -15,5 +15,6 @@ obj-$(CONFIG_OF_RESERVED_MEM) += of_reserved_mem.o
obj-$(CONFIG_OF_RESOLVE)  += resolver.o
obj-$(CONFIG_OF_OVERLAY) += overlay.o
obj-$(CONFIG_OF_NUMA) += of_numa.o
obj-$(CONFIG_OF_BATTERYDATA) += of_batterydata.o

obj-$(CONFIG_OF_UNITTEST) += unittest-data/
+457 −0
Original line number Diff line number Diff line
/* Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#define pr_fmt(fmt)	"%s: " fmt, __func__

#include <linux/err.h>
#include <linux/of.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/batterydata-lib.h>
#include <linux/power_supply.h>

static int of_batterydata_read_lut(const struct device_node *np,
			int max_cols, int max_rows, int *ncols, int *nrows,
			int *col_legend_data, int *row_legend_data,
			int *lut_data)
{
	struct property *prop;
	const __be32 *data;
	int cols, rows, size, i, j, *out_values;

	prop = of_find_property(np, "qcom,lut-col-legend", NULL);
	if (!prop) {
		pr_err("%s: No col legend found\n", np->name);
		return -EINVAL;
	} else if (!prop->value) {
		pr_err("%s: No col legend value found, np->name\n", np->name);
		return -ENODATA;
	} else if (prop->length > max_cols * sizeof(int)) {
		pr_err("%s: Too many columns\n", np->name);
		return -EINVAL;
	}

	cols = prop->length/sizeof(int);
	*ncols = cols;
	data = prop->value;
	for (i = 0; i < cols; i++)
		*col_legend_data++ = be32_to_cpup(data++);

	rows = 0;

	prop = of_find_property(np, "qcom,lut-row-legend", NULL);
	if (!prop || row_legend_data == NULL) {
		/* single row lut */
		rows = 1;
	} else if (!prop->value) {
		pr_err("%s: No row legend value found\n", np->name);
		return -ENODATA;
	} else if (prop->length > max_rows * sizeof(int)) {
		pr_err("%s: Too many rows\n", np->name);
		return -EINVAL;
	}

	if (rows != 1) {
		rows = prop->length/sizeof(int);
		*nrows = rows;
		data = prop->value;
		for (i = 0; i < rows; i++)
			*row_legend_data++ = be32_to_cpup(data++);
	}

	prop = of_find_property(np, "qcom,lut-data", NULL);
	if (!prop) {
		pr_err("prop 'qcom,lut-data' not found\n");
		return -EINVAL;
	}
	data = prop->value;
	size = prop->length/sizeof(int);
	if (size != cols * rows) {
		pr_err("%s: data size mismatch, %dx%d != %d\n",
				np->name, cols, rows, size);
		return -EINVAL;
	}
	for (i = 0; i < rows; i++) {
		out_values = lut_data + (max_cols * i);
		for (j = 0; j < cols; j++) {
			*out_values++ = be32_to_cpup(data++);
			pr_debug("Value = %d\n", *(out_values-1));
		}
	}

	return 0;
}

static int of_batterydata_read_sf_lut(struct device_node *data_node,
				const char *name, struct sf_lut *lut)
{
	struct device_node *node = of_find_node_by_name(data_node, name);
	int rc;

	if (!lut) {
		pr_debug("No lut provided, skipping\n");
		return 0;
	} else if (!node) {
		pr_err("Couldn't find %s node.\n", name);
		return -EINVAL;
	}

	rc = of_batterydata_read_lut(node, PC_CC_COLS, PC_CC_ROWS,
			&lut->cols, &lut->rows, lut->row_entries,
			lut->percent, *lut->sf);
	if (rc) {
		pr_err("Failed to read %s node.\n", name);
		return rc;
	}

	return 0;
}

static int of_batterydata_read_pc_temp_ocv_lut(struct device_node *data_node,
				const char *name, struct pc_temp_ocv_lut *lut)
{
	struct device_node *node = of_find_node_by_name(data_node, name);
	int rc;

	if (!lut) {
		pr_debug("No lut provided, skipping\n");
		return 0;
	} else if (!node) {
		pr_err("Couldn't find %s node.\n", name);
		return -EINVAL;
	}
	rc = of_batterydata_read_lut(node, PC_TEMP_COLS, PC_TEMP_ROWS,
			&lut->cols, &lut->rows, lut->temp, lut->percent,
			*lut->ocv);
	if (rc) {
		pr_err("Failed to read %s node.\n", name);
		return rc;
	}

	return 0;
}

static int of_batterydata_read_ibat_temp_acc_lut(struct device_node *data_node,
			const char *name, struct ibat_temp_acc_lut *lut)
{
	struct device_node *node = of_find_node_by_name(data_node, name);
	int rc;

	if (!lut) {
		pr_debug("No lut provided, skipping\n");
		return 0;
	} else if (!node) {
		pr_debug("Couldn't find %s node.\n", name);
		return 0;
	}
	rc = of_batterydata_read_lut(node, ACC_TEMP_COLS, ACC_IBAT_ROWS,
			&lut->cols, &lut->rows, lut->temp, lut->ibat,
			*lut->acc);
	if (rc) {
		pr_err("Failed to read %s node.\n", name);
		return rc;
	}

	return 0;
}

static int of_batterydata_read_single_row_lut(struct device_node *data_node,
				const char *name, struct single_row_lut *lut)
{
	struct device_node *node = of_find_node_by_name(data_node, name);
	int rc;

	if (!lut) {
		pr_debug("No lut provided, skipping\n");
		return 0;
	} else if (!node) {
		pr_err("Couldn't find %s node.\n", name);
		return -EINVAL;
	}

	rc = of_batterydata_read_lut(node, MAX_SINGLE_LUT_COLS, 1,
			&lut->cols, NULL, lut->x, NULL, lut->y);
	if (rc) {
		pr_err("Failed to read %s node.\n", name);
		return rc;
	}

	return 0;
}

static int of_batterydata_read_batt_id_kohm(const struct device_node *np,
				const char *propname, struct batt_ids *batt_ids)
{
	struct property *prop;
	const __be32 *data;
	int num, i, *id_kohm = batt_ids->kohm;

	prop = of_find_property(np, "qcom,batt-id-kohm", NULL);
	if (!prop) {
		pr_err("%s: No battery id resistor found\n", np->name);
		return -EINVAL;
	} else if (!prop->value) {
		pr_err("%s: No battery id resistor value found, np->name\n",
						np->name);
		return -ENODATA;
	} else if (prop->length > MAX_BATT_ID_NUM * sizeof(__be32)) {
		pr_err("%s: Too many battery id resistors\n", np->name);
		return -EINVAL;
	}

	num = prop->length/sizeof(__be32);
	batt_ids->num = num;
	data = prop->value;
	for (i = 0; i < num; i++)
		*id_kohm++ = be32_to_cpup(data++);

	return 0;
}

#define OF_PROP_READ(property, qpnp_dt_property, node, rc, optional)	\
do {									\
	if (rc)								\
		break;							\
	rc = of_property_read_u32(node, "qcom," qpnp_dt_property,	\
					&property);			\
									\
	if ((rc == -EINVAL) && optional) {				\
		property = -EINVAL;					\
		rc = 0;							\
	} else if (rc) {						\
		pr_err("Error reading " #qpnp_dt_property		\
				" property rc = %d\n", rc);		\
	}								\
} while (0)

static int of_batterydata_load_battery_data(struct device_node *node,
				int best_id_kohm,
				struct bms_battery_data *batt_data)
{
	int rc;

	rc = of_batterydata_read_single_row_lut(node, "qcom,fcc-temp-lut",
			batt_data->fcc_temp_lut);
	if (rc)
		return rc;

	rc = of_batterydata_read_pc_temp_ocv_lut(node,
			"qcom,pc-temp-ocv-lut",
			batt_data->pc_temp_ocv_lut);
	if (rc)
		return rc;

	rc = of_batterydata_read_sf_lut(node, "qcom,rbatt-sf-lut",
			batt_data->rbatt_sf_lut);
	if (rc)
		return rc;

	rc = of_batterydata_read_ibat_temp_acc_lut(node, "qcom,ibat-acc-lut",
						batt_data->ibat_acc_lut);
	if (rc)
		return rc;

	rc = of_property_read_string(node, "qcom,battery-type",
					&batt_data->battery_type);
	if (rc) {
		pr_err("Error reading qcom,battery-type property rc=%d\n", rc);
		batt_data->battery_type = NULL;
		return rc;
	}

	OF_PROP_READ(batt_data->fcc, "fcc-mah", node, rc, false);
	OF_PROP_READ(batt_data->default_rbatt_mohm,
			"default-rbatt-mohm", node, rc, false);
	OF_PROP_READ(batt_data->rbatt_capacitive_mohm,
			"rbatt-capacitive-mohm", node, rc, false);
	OF_PROP_READ(batt_data->flat_ocv_threshold_uv,
			"flat-ocv-threshold-uv", node, rc, true);
	OF_PROP_READ(batt_data->max_voltage_uv,
			"max-voltage-uv", node, rc, true);
	OF_PROP_READ(batt_data->cutoff_uv, "v-cutoff-uv", node, rc, true);
	OF_PROP_READ(batt_data->iterm_ua, "chg-term-ua", node, rc, true);
	OF_PROP_READ(batt_data->fastchg_current_ma,
			"fastchg-current-ma", node, rc, true);
	OF_PROP_READ(batt_data->fg_cc_cv_threshold_mv,
			"fg-cc-cv-threshold-mv", node, rc, true);

	batt_data->batt_id_kohm = best_id_kohm;

	return rc;
}

static int64_t of_batterydata_convert_battery_id_kohm(int batt_id_uv,
				int rpull_up, int vadc_vdd)
{
	int64_t resistor_value_kohm, denom;

	if (batt_id_uv == 0) {
		/* vadc not correct or batt id line grounded, report 0 kohms */
		return 0;
	}
	/* calculate the battery id resistance reported via ADC */
	denom = div64_s64(vadc_vdd * 1000000LL, batt_id_uv) - 1000000LL;

	if (denom == 0) {
		/* batt id connector might be open, return 0 kohms */
		return 0;
	}
	resistor_value_kohm = div64_s64(rpull_up * 1000000LL + denom/2, denom);

	pr_debug("batt id voltage = %d, resistor value = %lld\n",
			batt_id_uv, resistor_value_kohm);

	return resistor_value_kohm;
}

struct device_node *of_batterydata_get_best_profile(
		const struct device_node *batterydata_container_node,
		int batt_id_kohm, const char *batt_type)
{
	struct batt_ids batt_ids;
	struct device_node *node, *best_node = NULL;
	const char *battery_type = NULL;
	int delta = 0, best_delta = 0, best_id_kohm = 0, id_range_pct,
		i = 0, rc = 0, limit = 0;
	bool in_range = false;

	/* 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
	 */
	for_each_child_of_node(batterydata_container_node, node) {
		if (batt_type != NULL) {
			rc = of_property_read_string(node, "qcom,battery-type",
							&battery_type);
			if (!rc && strcmp(battery_type, batt_type) == 0) {
				best_node = node;
				best_id_kohm = batt_id_kohm;
				break;
			}
		} else {
			rc = of_batterydata_read_batt_id_kohm(node,
							"qcom,batt-id-kohm",
							&batt_ids);
			if (rc)
				continue;
			for (i = 0; i < batt_ids.num; i++) {
				delta = abs(batt_ids.kohm[i] - batt_id_kohm);
				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];
				}
			}
		}
	}

	if (best_node == NULL) {
		pr_err("No battery data found\n");
		return best_node;
	}

	/* 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)) {
		pr_err("out of range: profile id %d batt id %d pct %d",
			best_id_kohm, batt_id_kohm, id_range_pct);
		return NULL;
	}

	rc = of_property_read_string(best_node, "qcom,battery-type",
							&battery_type);
	if (!rc)
		pr_info("%s found\n", battery_type);
	else
		pr_info("%s found\n", best_node->name);

	return best_node;
}

int of_batterydata_read_data(struct device_node *batterydata_container_node,
				struct bms_battery_data *batt_data,
				int batt_id_uv)
{
	struct device_node *node, *best_node;
	struct batt_ids batt_ids;
	const char *battery_type = NULL;
	int delta, best_delta, batt_id_kohm, rpull_up_kohm,
		vadc_vdd_uv, best_id_kohm, i, rc = 0;

	node = batterydata_container_node;
	OF_PROP_READ(rpull_up_kohm, "rpull-up-kohm", node, rc, false);
	OF_PROP_READ(vadc_vdd_uv, "vref-batt-therm", node, rc, false);
	if (rc)
		return rc;

	batt_id_kohm = of_batterydata_convert_battery_id_kohm(batt_id_uv,
					rpull_up_kohm, vadc_vdd_uv);
	best_node = NULL;
	best_delta = 0;
	best_id_kohm = 0;

	/*
	 * Find the battery data with a battery id resistor closest to this one
	 */
	for_each_child_of_node(batterydata_container_node, node) {
		rc = of_batterydata_read_batt_id_kohm(node,
						"qcom,batt-id-kohm",
						&batt_ids);
		if (rc)
			continue;
		for (i = 0; i < batt_ids.num; i++) {
			delta = abs(batt_ids.kohm[i] - batt_id_kohm);
			if (delta < best_delta || !best_node) {
				best_node = node;
				best_delta = delta;
				best_id_kohm = batt_ids.kohm[i];
			}
		}
	}

	if (best_node == NULL) {
		pr_err("No battery data found\n");
		return -ENODATA;
	}
	rc = of_property_read_string(best_node, "qcom,battery-type",
							&battery_type);
	if (!rc)
		pr_info("%s loaded\n", battery_type);
	else
		pr_info("%s loaded\n", best_node->name);

	return of_batterydata_load_battery_data(best_node,
					best_id_kohm, batt_data);
}

MODULE_LICENSE("GPL v2");
+218 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading