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

Commit 2e01fb68 authored by qctecmdr Service's avatar qctecmdr Service Committed by Gerrit - the friendly Code Review server
Browse files

Merge "power: smb5: Add snapshot of SMB5 driver and its dependencies"

parents 8cf67e5e caceca44
Loading
Loading
Loading
Loading
+423 −0
Original line number Diff line number Diff line
Qualcomm Technologies, Inc. SMB5 Charger Specific Bindings

SMB5 Charger is an efficient programmable battery charger capable of charging a
high-capacity lithium-ion battery over micro-USB or USB Type-C ultrafast with
Quick Charge 2.0, Quick Charge 3.0, and USB Power Delivery support. Wireless
charging features full A4WP Rezence 1.2, WPC 1.2, and PMA support.

=======================
Required Node Structure
=======================

SMB5 Charger must be described in two levels of devices nodes.

===============================
First Level Node - SMB5 Charger
===============================

Charger specific properties:
- compatible
  Usage:      required
  Value type: <string>
  Definition: "qcom,qpnp-smb5".

- qcom,pmic-revid
  Usage:      required
  Value type: phandle
  Definition: Should specify the phandle of PMI's revid module. This is used to
		identify the PMI subtype.

- qcom,sec-charger-config
  Usage:      optional
  Value type: <u32>
  Definition: Specify how the secondary chargers are configured.
		0 - No secondary charger.
		1 - Charge Pump SMB1390.
		2 - SMB1355 parallel charger.
		3 - Both Charge Pump and SMB1355.
		If the value is not present, 0 is used as default.

- io-channels
- io-channel-names
  Usage:      optional
  Value type: <phandle>
  Definition: For details about IIO bindings see:
		Documentation/devicetree/bindings/iio/iio-bindings.txt

- qcom,batteryless-platform
  Usage:      optional
  Value type: <empty>
  Definition: Boolean flag which indicates that the platform does not have a
		battery, and therefore charging should be disabled. In
		addition battery properties will be faked such that the device
		assumes normal operation.

- qcom,charger-temp-max
  Usage:      optional
  Value type: <u32>
  Definition: Specifies the charger temp REG_H_THRESHOLD for PM8150B in deciDegC.
		If the value is not present, use the setting read from the device.

- qcom,smb-temp-max
  Usage:      optional
  Value type: <u32>
  Definition: Specifies the charger temp REG_H_THRESHOLD for SMB1355 in deciDegC.
		If the value is not present, use the setting read from the device.

- qcom,fcc-max-ua
  Usage:      optional
  Value type: <u32>
  Definition: Specifies the maximum fast charge current in micro-amps.
		If the value is not present, 1Amp is used as default.

- qcom,fv-max-uv
  Usage:      optional
  Value type: <u32>
  Definition: Specifies the maximum float voltage in micro-volts.
		If the value is not present, 4.35V is used as default.

- qcom,usb-icl-ua
  Usage:      optional
  Value type: <u32>
  Definition: Specifies the USB input current limit in micro-amps.
		 If the value is not present, 1.5Amps is used as default.

- qcom,usb-ocl-ua
  Usage:      optional
  Value type: <u32>
  Definition: Specifies the OTG output current limit in micro-amps.
		If the value is not present, 1.5Amps is used as default.

- qcom,dc-icl-ua
  Usage:      optional
  Value type: <u32>
  Definition: Specifies the DC input current limit in micro-amps.

- qcom,boost-threshold-ua
  Usage:      optional
  Value type: <u32>
  Definition: Specifies the boost current threshold in micro-amps.
		If the value is not present, 100mA is used as default.

- qcom,thermal-mitigation
  Usage:      optional
  Value type: Array of <u32>
  Definition: Array of fast charge current limit values for
		different system thermal mitigation levels.
		This should be a flat array that denotes the
		maximum charge current in mA for each thermal
		level.

- qcom,float-option
  Usage:      optional
  Value type: <u32>
  Definition: Configures how the charger behaves when a float charger is
	      detected by APSD.
	        1 - Treat as a DCP.
	        2 - Treat as a SDP.
	        3 - Disable charging.
	        4 - Suspend USB input.

- qcom,hvdcp-disable
  Usage:      optional
  Value type: <empty>
  Definition: Specifies if hvdcp charging is to be enabled or not.
		If this property is not specified hvdcp will be enabled.
		If this property is specified, hvdcp 2.0 detection will still
		happen but the adapter won't be asked to switch to a higher
		voltage point.

- qcom,chg-inhibit-threshold-mv
  Usage:      optional
  Value type: <u32>
  Definition: Charge inhibit threshold in milli-volts. Charging will be
		inhibited when the battery voltage is within this threshold
		from Vfloat at charger insertion. If this is not specified
		then charge inhibit will be disabled by default.
		Allowed values are: 50, 100, 200, 300.

- qcom,chg-term-src
  Usage:      optional
  Value type: <u32>
  Definition: Specify either the ADC or analog comparators to be used in order
		to set threshold values for charge termination current.
		0 - Unspecified
		1 - Select ADC comparator
		2 - Select ANALOG comparator

- qcom,chg-term-current-ma
  Usage:      optional
  Value type: <u32>
  Definition: When ADC comparator is selected as qcom,chg-term-src, this
		parameter should be set to the desired upper threshold.

- qcom,chg-term-base-current-ma
  Usage:      optional
  Value type: <u32>
  Definition: When ADC comparator is selected as qcom,chg-term-src, this
		parameter should be set to the desired lower threshold.

- qcom,auto-recharge-soc
  Usage:      optional
  Value type: <u32>
  Definition: Specifies the SOC threshold at which the charger will
		restart charging after termination. The value specified
		ranges from 0 - 100. The feature is enabled if this
		property is specified with a valid SOC value.

- qcom,auto-recharge-vbat-mv
  Usage:      optional
  Value type: <u32>
  Definition: Specifies the battery voltage threshold at which the charger
		will restart charging after termination. The value specified
		is in milli-volts.

- qcom,suspend-input-on-debug-batt
  Usage:      optional
  Value type: <empty>
  Definition: Boolean flag which when present enables input suspend for
		debug battery.

- qcom,min-freq-khz
  Usage:      optional
  Value type: <u32>
  Definition: Specifies the minimum charger buck/boost switching frequency
		in KHz. It overrides the min frequency defined for the charger.

- qcom,max-freq-khz
  Usage:      optional
  Value type: <u32>
  Definition: Specifies the maximum charger buck/boost switching frequency in
		 KHz. It overrides the max frequency defined for the charger.

- qcom,otg-deglitch-time-ms
  Usage:      optional
  Value type: <u32>
  Definition: Specifies the deglitch interval for OTG detection.
		If the value is not present, 50 msec is used as default.

- qcom,step-charging-enable
  Usage:      optional
  Value type: bool
  Definition: Boolean flag which when present enables step-charging.

- qcom,wd-bark-time-secs
  Usage:      optional
  Value type: <u32>
  Definition: WD bark-timeout in seconds. The possible values are
		16, 32, 64, 128. If not defined it defaults to 64.

- qcom,sw-jeita-enable
  Usage:      optional
  Value type: bool
  Definition: Boolean flag which when present enables sw compensation for
		jeita.

- qcom,battery-data
  Usage:      optional
  Value type: <phandle>
  Definition: Specifies the phandle of the node which contains the battery
		profiles supported on the device.

- qcom,flash-derating-soc
  Usage:      optional
  Value type: <u32>
  Definition: SOC threshold in percentage below which hardware will start
		derating flash. This is only applicable to certain PMICs like
		PMI632 which has SCHGM_FLASH peripheral.

- qcom,flash-disable-soc
  Usage:      optional
  Value type: <u32>
  Definition: SOC threshold in percentage below which hardware will disable
		flash. This is only applicable to certain PMICs like PMI632
		which has SCHGM_FLASH peripheral.

- qcom,headroom-mode
  Usage:      optional
  Value type: <u32>
  Definition: Specifies flash hardware headroom management policy. The
		possible values are:
		<0>: Fixed mode, constant 5V at flash input.
		<1>: Adaptive mode allows charger output voltage to be
		dynamically controlled by the flash module based on the
		required flash headroom.
		This is only applicable to certain PMICs like PMI632 which
		has SCHGM_FLASH peripheral.

- qcom,fcc-stepping-enable
  Usage:      optional
  Value type: bool
  Definition: Boolean flag which when present enables stepwise change in FCC.
		The default stepping rate is 100mA/sec.

=============================================
Second Level Nodes - SMB5 Charger Peripherals
=============================================

Peripheral specific properties:
- reg
  Usage:      required
  Value type: <prop-encoded-array>
  Definition: Address and size of the peripheral's register block.

- interrupts
  Usage:      required
  Value type: <prop-encoded-array>
  Definition: Peripheral interrupt specifier.

- interrupt-names
  Usage:      required
  Value type: <stringlist>
  Definition: Interrupt names.  This list must match up 1-to-1 with the
	      interrupts specified in the 'interrupts' property.

=======
Example
=======

pm8150b_charger: qcom,qpnp-smb5 {
	compatible = "qcom,qpnp-smb5";
	#address-cells = <1>;
	#size-cells = <1>;

	qcom,pmic-revid = <&pm8150b_revid>;

	dpdm-supply = <&qusb_phy0>;

	qcom,sec-charger-config = <1>;

	io-channels = <&pm8150b_vadc ADC_USB_IN_V_16>,
	              <&pm8150b_vadc ADC_USB_IN_I>,
	              <&pm8150b_vadc ADC_CHG_TEMP>;
	io-channel-names = "usb_in_voltage",
	                   "usb_in_current",
	                   "chg_temp";

	qcom,chgr@1000 {
		reg = <0x1000 0x100>;
		interrupts =	<0x2 0x10 0x0 IRQ_TYPE_NONE>,
				<0x2 0x10 0x1 IRQ_TYPE_NONE>,
				<0x2 0x10 0x2 IRQ_TYPE_NONE>,
				<0x2 0x10 0x3 IRQ_TYPE_NONE>,
				<0x2 0x10 0x4 IRQ_TYPE_NONE>;

		interrupt-names =	"chg-error",
					"chg-state-change",
					"step-chg-state-change",
					"step-chg-soc-update-fail",
					"step-chg-soc-update-request";
	};

	qcom,otg@1100 {
		reg = <0x1100 0x100>;
		interrupts =	<0x2 0x11 0x0 IRQ_TYPE_NONE>,
				<0x2 0x11 0x1 IRQ_TYPE_NONE>,
				<0x2 0x11 0x2 IRQ_TYPE_NONE>,
				<0x2 0x11 0x3 IRQ_TYPE_NONE>;

		interrupt-names =	"otg-fail",
					"otg-overcurrent",
					"otg-oc-dis-sw-sts",
					"testmode-change-detect";
	};

	qcom,bat-if@1200 {
		reg = <0x1200 0x100>;
		interrupts =	<0x2 0x12 0x0 IRQ_TYPE_NONE>,
				<0x2 0x12 0x1 IRQ_TYPE_NONE>,
				<0x2 0x12 0x2 IRQ_TYPE_NONE>,
				<0x2 0x12 0x3 IRQ_TYPE_NONE>,
				<0x2 0x12 0x4 IRQ_TYPE_NONE>,
				<0x2 0x12 0x5 IRQ_TYPE_NONE>;

		interrupt-names =	"bat-temp",
					"bat-ocp",
					"bat-ov",
					"bat-low",
					"bat-therm-or-id-missing",
					"bat-terminal-missing";
	};

	qcom,usb-chgpth@1300 {
		reg = <0x1300 0x100>;
		interrupts =	<0x2 0x13 0x0 IRQ_TYPE_NONE>,
				<0x2 0x13 0x1 IRQ_TYPE_NONE>,
				<0x2 0x13 0x2 IRQ_TYPE_NONE>,
				<0x2 0x13 0x3 IRQ_TYPE_NONE>,
				<0x2 0x13 0x4 IRQ_TYPE_NONE>,
				<0x2 0x13 0x5 IRQ_TYPE_NONE>,
				<0x2 0x13 0x6 IRQ_TYPE_NONE>,
				<0x2 0x13 0x7 IRQ_TYPE_NONE>;

		interrupt-names =	"usbin-collapse",
					"usbin-lt-3p6v",
					"usbin-uv",
					"usbin-ov",
					"usbin-plugin",
					"usbin-src-change",
					"usbin-icl-change",
					"type-c-change";
	};

	qcom,dc-chgpth@1400 {
		reg = <0x1400 0x100>;
		interrupts =	<0x2 0x14 0x0 IRQ_TYPE_NONE>,
				<0x2 0x14 0x1 IRQ_TYPE_NONE>,
				<0x2 0x14 0x2 IRQ_TYPE_NONE>,
				<0x2 0x14 0x3 IRQ_TYPE_NONE>,
				<0x2 0x14 0x4 IRQ_TYPE_NONE>,
				<0x2 0x14 0x5 IRQ_TYPE_NONE>,
				<0x2 0x14 0x6 IRQ_TYPE_NONE>;

		interrupt-names =	"dcin-collapse",
					"dcin-lt-3p6v",
					"dcin-uv",
					"dcin-ov",
					"dcin-plugin",
					"div2-en-dg",
					"dcin-icl-change";
	};

	qcom,chgr-misc@1600 {
		reg = <0x1600 0x100>;
		interrupts =	<0x2 0x16 0x0 IRQ_TYPE_NONE>,
				<0x2 0x16 0x1 IRQ_TYPE_NONE>,
				<0x2 0x16 0x2 IRQ_TYPE_NONE>,
				<0x2 0x16 0x3 IRQ_TYPE_NONE>,
				<0x2 0x16 0x4 IRQ_TYPE_NONE>,
				<0x2 0x16 0x5 IRQ_TYPE_NONE>,
				<0x2 0x16 0x6 IRQ_TYPE_NONE>,
				<0x2 0x16 0x7 IRQ_TYPE_NONE>;

		interrupt-names =	"wdog-snarl",
					"wdog-bark",
					"aicl-fail",
					"aicl-done",
					"high-duty-cycle",
					"input-current-limiting",
					"temperature-change",
					"switcher-power-ok";
	};

	qcom,schgm-flash@a600 {
		reg = <0xa600 0x100>;
		interrupts =	<0x2 0xa6 0x0 IRQ_TYPE_NONE>,
				<0x2 0xa6 0x1 IRQ_TYPE_NONE>,
				<0x2 0xa6 0x2 IRQ_TYPE_NONE>,
				<0x2 0xa6 0x3 IRQ_TYPE_NONE>,
				<0x2 0xa6 0x4 IRQ_TYPE_NONE>,
				<0x2 0xa6 0x5 IRQ_TYPE_NONE>,
				<0x2 0xa6 0x6 IRQ_TYPE_NONE>,
				<0x2 0xa6 0x7 IRQ_TYPE_NONE>;

		interrupt-names =	"flash-en",
					"torch-req",
					"flash-state-change",
					"vout-up",
					"vout-down",
					"ilim1-s1",
					"ilim2-s2",
					"vreg-ok";
	};
};
+5 −0
Original line number Diff line number Diff line
@@ -109,4 +109,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
@@ -14,5 +14,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/
+450 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
 */

#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\n",
			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");
+2 −0

File changed.

Preview size limit exceeded, changes collapsed.

Loading