From patchwork Mon Oct 28 07:18:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "(Exiting) Baolin Wang" X-Patchwork-Id: 11214687 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6E6C0139A for ; Mon, 28 Oct 2019 07:19:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4A89F208C0 for ; Mon, 28 Oct 2019 07:19:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="SrgmiIPW" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732503AbfJ1HT4 (ORCPT ); Mon, 28 Oct 2019 03:19:56 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:39658 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732514AbfJ1HT4 (ORCPT ); Mon, 28 Oct 2019 03:19:56 -0400 Received: by mail-pf1-f194.google.com with SMTP id v4so6284371pff.6 for ; Mon, 28 Oct 2019 00:19:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=ssMFBj43632FcWk2CcxAhFxBDGN9BLK5UqZaapoI3EI=; b=SrgmiIPW0C51iZX8hYKQ2hSUsynSut8k+7kzklaDe3hpral131t906w1kTxvTbV5tI 3AJFcoHHfri+SAsbQwrHOkCumk9s5R6sxf0Lsy79Eu3J5KBusj0Cen9tu0V/2wqWGbOH RETMM4ndBRk1j1in/GGXg8h7nnVqMSf39k7vZ9ymw67v30mM/OouHrZs1i+tJqqJyyOF E3KORbX1/MWIMAzbqYg5QFuWzs2eufjGB9AgaqQiuXq3TcGvlFZyGGOLnDd9ObkzVP+4 sNMQvk6FIeWdhMOwKBK32SKHR1/sO5Itv7GXMlIUpon3uADMjv1Nqvuf//N/tlfER0f+ 1T/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=ssMFBj43632FcWk2CcxAhFxBDGN9BLK5UqZaapoI3EI=; b=WG/4x8SIh1Dl/qjIJpMukFcQKiSLAAEZyEtjOzUdc/5/4IxZAAMkMH523QJ1rha1cO Kb8BhNO6fjnixxi0oweiGcqDWHY/4uCEF2oqCQAclEk3hu/9RfwYd/snXwz23XHkTF6z GZcRYsTk6C+tFanxjnT5D3XdYe2xl/kW5LnTfYTcVY3/uykc4oPjCZVwqcq3ZwMrNV/L /g4GHFT8pTKdFD0yppnk/4fUrxZ8inWgMl6Z6OfUhNUY+ttr/iebxW5AX+x7neJEe5De dEPxBHQZiVaADljJlIaQbO9XvcnF3CWjCGM4o/6At2Jat5Quogu46UWYRnKxZKBYXMxv dsJA== X-Gm-Message-State: APjAAAV+LXmwgOcw0/eodyKG8g2T8OtxQ9OPDCpjchM8u70JRJdeOeAv YZzt7T2ESpbx5hb1Vo1Ogp129Q== X-Google-Smtp-Source: APXvYqyOl7sbyoI1mYNi/ewKV9zLdhmDBcQe9nL/ufV1lXHqClNmhenBTTW1DVH/LhXSpRQwKSwSQg== X-Received: by 2002:a17:90b:f10:: with SMTP id br16mr20271382pjb.111.1572247194072; Mon, 28 Oct 2019 00:19:54 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id 13sm11504703pgq.72.2019.10.28.00.19.50 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 28 Oct 2019 00:19:53 -0700 (PDT) From: Baolin Wang To: sre@kernel.org, robh+dt@kernel.org, mark.rutland@arm.com Cc: linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, yuanjiang.yu@unisoc.com, baolin.wang@linaro.org, baolin.wang7@gmail.com, zhang.lyra@gmail.com, orsonzhai@gmail.com Subject: [PATCH 1/5] dt-bindings: power: Introduce one property to describe the battery resistance with temperature changes Date: Mon, 28 Oct 2019 15:18:57 +0800 Message-Id: <44f0c19510c7317cb4ee6cac54b3adfa81c2d6d0.1572245011.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Since the battery internal resistance can be changed as the temperature changes, thus add one table to describe the battery resistance percent in different temperature to get a accurate battery internal resistance. Signed-off-by: Baolin Wang --- .../devicetree/bindings/power/supply/battery.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Documentation/devicetree/bindings/power/supply/battery.txt b/Documentation/devicetree/bindings/power/supply/battery.txt index 5c913d4c..1a6f951 100644 --- a/Documentation/devicetree/bindings/power/supply/battery.txt +++ b/Documentation/devicetree/bindings/power/supply/battery.txt @@ -35,6 +35,10 @@ Optional Properties: for each of the battery capacity lookup table. The first temperature value specifies the OCV table 0, and the second temperature value specifies the OCV table 1, and so on. + - resistance-temp-table: An array providing the resistance percent and + corresponding temperature in degree Celsius, which is used to look up the + resistance percent according to current temperature to get a accurate + batterty internal resistance. Battery properties are named, where possible, for the corresponding elements in enum power_supply_property, defined in @@ -61,6 +65,7 @@ Example: ocv-capacity-table-0 = <4185000 100>, <4113000 95>, <4066000 90>, ...; ocv-capacity-table-1 = <4200000 100>, <4185000 95>, <4113000 90>, ...; ocv-capacity-table-2 = <4250000 100>, <4200000 95>, <4185000 90>, ...; + resistance-temp-table = <20 100>, <10 90>, <0 80>, <(-10) 60>; }; charger: charger@11 { From patchwork Mon Oct 28 07:18:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "(Exiting) Baolin Wang" X-Patchwork-Id: 11214691 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2C5D3139A for ; Mon, 28 Oct 2019 07:20:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id F3C27214AF for ; Mon, 28 Oct 2019 07:20:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="fm2GAqpv" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732535AbfJ1HUB (ORCPT ); Mon, 28 Oct 2019 03:20:01 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:43038 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732514AbfJ1HUA (ORCPT ); Mon, 28 Oct 2019 03:20:00 -0400 Received: by mail-pl1-f196.google.com with SMTP id v5so5143630ply.10 for ; Mon, 28 Oct 2019 00:19:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=zQKRY2XE5oS+mi6BE44agpH5JNOXqmvzvanMV9EmHYs=; b=fm2GAqpvOV5+xOdAo3r0mmuXfnSQudliJYYK8fQmDxTzI5/A6qJcm4cFNtMt6kztOm LEZHSgqqIvCQzImsVgxaM52k5nBlExW9YWo98VQGVjqF2zzmrhFIgBsgconeYJH6Qo3C VOUk3K90fTM/4g9mYUqnSN3J9E/qTa4ERPQnuGuNs8WpJGU+wX7gRuHHnAuwqB+0XZ9u YFsSnfSzwhLA6UHhm24M9X1b3D1RELJx5tC2NgLdio/S+QPsiIfaaJKTaGFZklAVbZqk shg3n+kFoHcQ10KLL5GSbW9Xps5orYtPui3XQeo+GlHYFiS6PYd+0TGb8KMDMZeBbzr4 TzPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=zQKRY2XE5oS+mi6BE44agpH5JNOXqmvzvanMV9EmHYs=; b=Z1b+yNvaUWEU5RPiayf4h0FJlX9xSDNVGTMxrf9MQCTC9QRsWu67ssIuYwk05rwsOS lwEfYrElIw0pF+xr06MT55KyCEoWFvrdbgWWyesx8VRHN8375aMF4S/eyMY5wAPJTxmA eJF2M8nlRVXFlD93NEi5EQd/uzMQF17K/XzpzCbf/7AYqPxDterHj2vhyK4rrJzzrcxV aE0TsaX7F00YXxXsk3QvYeJD5ro/p7QubMXQe4AMko/e1SODOAjxSG5dCvYvSfJmSInZ +lvKjxmV7bsj6LdavMOb8i40GvZrCCSkK7Rhj/tANJYEy6AaXfkJyBSC4ApDst5eOGYI JIQA== X-Gm-Message-State: APjAAAVImTK7MCNPTXDPqwCe9vgLX2osAIRE2wVzd52TbMJivMvig1D3 6WOBd57e+KYti72zO1gXPy3xtQ== X-Google-Smtp-Source: APXvYqxGyPewV02f8pGwk38cG8wUAibrorZUIjhlZRWTqatHMYUo9fDPeptlwz4JNB+aK8blKxxnjg== X-Received: by 2002:a17:902:8b83:: with SMTP id ay3mr17436518plb.269.1572247198266; Mon, 28 Oct 2019 00:19:58 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id 13sm11504703pgq.72.2019.10.28.00.19.54 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 28 Oct 2019 00:19:57 -0700 (PDT) From: Baolin Wang To: sre@kernel.org, robh+dt@kernel.org, mark.rutland@arm.com Cc: linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, yuanjiang.yu@unisoc.com, baolin.wang@linaro.org, baolin.wang7@gmail.com, zhang.lyra@gmail.com, orsonzhai@gmail.com Subject: [PATCH 2/5] power: supply: core: Add battery internal resistance temperature table support Date: Mon, 28 Oct 2019 15:18:58 +0800 Message-Id: X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Since the battery internal resistance can be changed with the temperature changes, thus add a resistance temperature table support to look up the accurate battery internal resistance in a certain temperature. Signed-off-by: Baolin Wang --- drivers/power/supply/power_supply_core.c | 67 +++++++++++++++++++++++++++++- include/linux/power_supply.h | 10 +++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c index 5c36c43..1a9a9fa 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -565,9 +565,11 @@ struct power_supply *devm_power_supply_get_by_phandle(struct device *dev, int power_supply_get_battery_info(struct power_supply *psy, struct power_supply_battery_info *info) { + struct power_supply_resistance_temp_table *resist_table; struct device_node *battery_np; const char *value; int err, len, index; + const __be32 *list; info->energy_full_design_uwh = -EINVAL; info->charge_full_design_uah = -EINVAL; @@ -578,6 +580,7 @@ int power_supply_get_battery_info(struct power_supply *psy, info->constant_charge_current_max_ua = -EINVAL; info->constant_charge_voltage_max_uv = -EINVAL; info->factory_internal_resistance_uohm = -EINVAL; + info->resist_table = NULL; for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) { info->ocv_table[index] = NULL; @@ -644,7 +647,6 @@ int power_supply_get_battery_info(struct power_supply *psy, for (index = 0; index < len; index++) { struct power_supply_battery_ocv_table *table; char *propname; - const __be32 *list; int i, tab_len, size; propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index); @@ -677,6 +679,26 @@ int power_supply_get_battery_info(struct power_supply *psy, } } + list = of_get_property(battery_np, "resistance-temp-table", &len); + if (!list || !len) + goto out_put_node; + + info->resist_table_size = len / (2 * sizeof(__be32)); + resist_table = info->resist_table = devm_kcalloc(&psy->dev, + info->resist_table_size, + sizeof(*resist_table), + GFP_KERNEL); + if (!info->resist_table) { + power_supply_put_battery_info(psy, info); + err = -ENOMEM; + goto out_put_node; + } + + for (index = 0; index < info->resist_table_size; index++) { + resist_table[index].temp = be32_to_cpu(*list++); + resist_table[index].resistance = be32_to_cpu(*list++); + } + out_put_node: of_node_put(battery_np); return err; @@ -692,10 +714,53 @@ void power_supply_put_battery_info(struct power_supply *psy, if (info->ocv_table[i]) devm_kfree(&psy->dev, info->ocv_table[i]); } + + if (info->resist_table) + devm_kfree(&psy->dev, info->resist_table); } EXPORT_SYMBOL_GPL(power_supply_put_battery_info); /** + * power_supply_temp2resist_simple() - find the battery internal resistance + * percent + * @table: Pointer to battery resistance temperature table + * @table_len: The table length + * @ocv: Current temperature + * + * This helper function is used to look up battery internal resistance percent + * according to current temperature value from the resistance temperature table, + * and the table must be ordered descending. Then the actual battery internal + * resistance = the ideal battery internal resistance * percent / 100. + * + * Return: the battery internal resistance percent + */ +int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table, + int table_len, int temp) +{ + int i, resist; + + for (i = 0; i < table_len; i++) + if (temp > table[i].temp) + break; + + if (i > 0 && i < table_len) { + int tmp; + + tmp = (table[i - 1].resistance - table[i].resistance) * + (temp - table[i].temp); + tmp /= table[i - 1].temp - table[i].temp; + resist = tmp + table[i].resistance; + } else if (i == 0) { + resist = table[0].resistance; + } else { + resist = table[table_len - 1].resistance; + } + + return resist; +} +EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple); + +/** * power_supply_ocv2cap_simple() - find the battery capacity * @table: Pointer to battery OCV lookup table * @table_len: OCV table length diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index 28413f7..dcd5a71 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -325,6 +325,11 @@ struct power_supply_battery_ocv_table { int capacity; /* percent */ }; +struct power_supply_resistance_temp_table { + int temp; /* celsius */ + int resistance; /* internal resistance percent */ +}; + #define POWER_SUPPLY_OCV_TEMP_MAX 20 /* @@ -349,6 +354,8 @@ struct power_supply_battery_info { int ocv_temp[POWER_SUPPLY_OCV_TEMP_MAX];/* celsius */ struct power_supply_battery_ocv_table *ocv_table[POWER_SUPPLY_OCV_TEMP_MAX]; int ocv_table_size[POWER_SUPPLY_OCV_TEMP_MAX]; + struct power_supply_resistance_temp_table *resist_table; + int resist_table_size; }; extern struct atomic_notifier_head power_supply_notifier; @@ -381,6 +388,9 @@ extern int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *ta int temp, int *table_len); extern int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info, int ocv, int temp); +extern int +power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table, + int table_len, int temp); extern void power_supply_changed(struct power_supply *psy); extern int power_supply_am_i_supplied(struct power_supply *psy); extern int power_supply_set_input_current_limit_from_supplier( From patchwork Mon Oct 28 07:18:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "(Exiting) Baolin Wang" X-Patchwork-Id: 11214693 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6DF86139A for ; Mon, 28 Oct 2019 07:20:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 43E9421D80 for ; Mon, 28 Oct 2019 07:20:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="H7ANTgpg" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732571AbfJ1HUE (ORCPT ); Mon, 28 Oct 2019 03:20:04 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:36850 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732568AbfJ1HUE (ORCPT ); Mon, 28 Oct 2019 03:20:04 -0400 Received: by mail-pf1-f193.google.com with SMTP id v19so6295811pfm.3 for ; Mon, 28 Oct 2019 00:20:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=ipn4aXAOp87D2v3r9KdhlrkAx9KnL+jbYVkv6tX05Nc=; b=H7ANTgpgwQ2+56L1vk0+gI3rNxCVOKam39g5rW/rxFArWeEN7xShdeud1+d5tj9sYT zIQeHuJdV1Lrm6P/tHiq/LJK/fJmt6fMPFh4FcD0bMWcXf1HVdxlg5m7eSqz+oW7EB2o HaJBqfCN2qoz0tVbDYTRIEa+2kA2Lq/Qkidt+66llOixVDJ8k3YK3NVMYE4CJFAyjuFq icvCAHsQbWSsbgrczubJZTzyDXX5YRAM2GtS5FH7mxhDU/vSdQAbmz8Zy8gKhAP9W71m xVIfVf2rJsZgtJsnFsVAQHRy4M9ThVbBpi9/JPuSPrNWQJLqTZ9DIH+1BPmKuydMRADC SVeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=ipn4aXAOp87D2v3r9KdhlrkAx9KnL+jbYVkv6tX05Nc=; b=GXgz0dKmFfKtMVlHv9p2WLea7eGqFckdA4VKV1Uk+F5YNOLdFYO1xtTKvPYlhqoDu2 BnLuHugI4ExPy+Ehxl8LXITC5U/+Qvefwwax4ZjJLsOnVETuzr+srIgYqOij5o7mjv8u CMMOdJxGUISn8jgwYyoHPSdGZ4xsgLSpEpZIdcYku3sicC8+Cq+E8yPf+4YimfgWgaeQ 29ZNQYYfpd7GTd6WJ9qryQSzJxr5ke4DPDkoLQJ+nO70EHUt6oPY5bMSuxl+N/dtKXQ4 68CwfSWRET6y+koQfM+9eECqkoD5tiTaFQ9OneL3auSuMUMZm2KZNgKD64gAwhDcXwqj OrZw== X-Gm-Message-State: APjAAAU5GSwQywU9K5HSDV70NI/VUbz3K8dxRkrvjhNzzIbQCTJyYrl1 M65e1SrTABfBiMjhisAb1/Y4Kw== X-Google-Smtp-Source: APXvYqwhfpmk1l+DF5Y1sQ12XRinFJFSNZF3ttAyMH6+2ZcV2cfOiRJp48ZX1gTKmZUnmk+OgQ2Yaw== X-Received: by 2002:a63:ff54:: with SMTP id s20mr18562592pgk.398.1572247202088; Mon, 28 Oct 2019 00:20:02 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id 13sm11504703pgq.72.2019.10.28.00.19.58 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 28 Oct 2019 00:20:01 -0700 (PDT) From: Baolin Wang To: sre@kernel.org, robh+dt@kernel.org, mark.rutland@arm.com Cc: linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, yuanjiang.yu@unisoc.com, baolin.wang@linaro.org, baolin.wang7@gmail.com, zhang.lyra@gmail.com, orsonzhai@gmail.com Subject: [PATCH 3/5] power: supply: sc27xx: Optimize the battery resistance with measuring temperature Date: Mon, 28 Oct 2019 15:18:59 +0800 Message-Id: X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Yuanjiang Yu Optimize the battery internal resistance in a certain temerature to get a accurate battery internal resistance. Signed-off-by: Yuanjiang Yu Signed-off-by: Baolin Wang --- drivers/power/supply/sc27xx_fuel_gauge.c | 32 ++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c index bc8f5bd..221b6fb 100644 --- a/drivers/power/supply/sc27xx_fuel_gauge.c +++ b/drivers/power/supply/sc27xx_fuel_gauge.c @@ -81,9 +81,11 @@ * @max_volt: the maximum constant input voltage in millivolt * @min_volt: the minimum drained battery voltage in microvolt * @table_len: the capacity table length + * @resist_table_len: the resistance table length * @cur_1000ma_adc: ADC value corresponding to 1000 mA * @vol_1000mv_adc: ADC value corresponding to 1000 mV * @cap_table: capacity table with corresponding ocv + * @resist_table: resistance percent table with corresponding temperature */ struct sc27xx_fgu_data { struct regmap *regmap; @@ -103,15 +105,18 @@ struct sc27xx_fgu_data { int max_volt; int min_volt; int table_len; + int resist_table_len; int cur_1000ma_adc; int vol_1000mv_adc; struct power_supply_battery_ocv_table *cap_table; + struct power_supply_resistance_temp_table *resist_table; }; static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data *data, int capacity); static void sc27xx_fgu_capacity_calibration(struct sc27xx_fgu_data *data, int cap, bool int_mode); static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data *data, int cap); +static int sc27xx_fgu_get_temp(struct sc27xx_fgu_data *data, int *temp); static const char * const sc27xx_charger_supply_name[] = { "sc2731_charger", @@ -434,7 +439,7 @@ static int sc27xx_fgu_get_current(struct sc27xx_fgu_data *data, int *val) static int sc27xx_fgu_get_vbat_ocv(struct sc27xx_fgu_data *data, int *val) { - int vol, cur, ret; + int vol, cur, ret, temp, resistance; ret = sc27xx_fgu_get_vbat_vol(data, &vol); if (ret) @@ -444,8 +449,19 @@ static int sc27xx_fgu_get_vbat_ocv(struct sc27xx_fgu_data *data, int *val) if (ret) return ret; + resistance = data->internal_resist; + if (data->resist_table_len > 0) { + ret = sc27xx_fgu_get_temp(data, &temp); + if (ret) + return ret; + + resistance = power_supply_temp2resist_simple(data->resist_table, + data->resist_table_len, temp); + resistance = data->internal_resist * resistance / 100; + } + /* Return the battery OCV in micro volts. */ - *val = vol * 1000 - cur * data->internal_resist; + *val = vol * 1000 - cur * resistance; return 0; } @@ -929,6 +945,18 @@ static int sc27xx_fgu_hw_init(struct sc27xx_fgu_data *data) if (!data->alarm_cap) data->alarm_cap += 1; + data->resist_table_len = info.resist_table_size; + if (data->resist_table_len > 0) { + data->resist_table = devm_kmemdup(data->dev, info.resist_table, + data->resist_table_len * + sizeof(struct power_supply_resistance_temp_table), + GFP_KERNEL); + if (!data->resist_table) { + power_supply_put_battery_info(data->battery, &info); + return -ENOMEM; + } + } + power_supply_put_battery_info(data->battery, &info); ret = sc27xx_fgu_calibration(data); From patchwork Mon Oct 28 07:19:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "(Exiting) Baolin Wang" X-Patchwork-Id: 11214695 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9B69F17D5 for ; Mon, 28 Oct 2019 07:20:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7551121D80 for ; Mon, 28 Oct 2019 07:20:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="HD06jgfK" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732617AbfJ1HUJ (ORCPT ); Mon, 28 Oct 2019 03:20:09 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:37745 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732608AbfJ1HUI (ORCPT ); Mon, 28 Oct 2019 03:20:08 -0400 Received: by mail-pg1-f194.google.com with SMTP id p1so6295829pgi.4 for ; Mon, 28 Oct 2019 00:20:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=zz90kEkSWWg/YORe6tmdX1Bf7ogm6HlTvGcmmgO38JE=; b=HD06jgfKdHaybBrjNDZRaOXNwrZc0W1LGLFB2+ngCxaUP4+gDqrtFqGtDyC7MrXPwX jR87YsTHT2M2QI4PXnUjNQcWD3GGTN5fXlPkFY4Om+Z18tEZqNR4xE6Rw/t6FpVu1QGu C4CwaqrEkr96aBTVgePTF44Rfo0I6mTdcpTHjAmyHzC+uMtjxHKemtjTh1trpesbg5ts PMStpjRNp3jlevLAvILguA+lR4rfm3Fyw0/1hyFx0sMmmFtWlqMbBkcoeIEBdOhPdPgF Oy55yKn1eGz9KjE7kgmfCZbWQ4yljK0mARwq08Hmry7c4eQAktXMsEdZHmNi7z72sPZZ 7CHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=zz90kEkSWWg/YORe6tmdX1Bf7ogm6HlTvGcmmgO38JE=; b=TxJz7ST8oz+GRo/AXHJejAfIXFlR67zwP3l7KCmrHMSu6q7KOkax7q0+ZPFDbg0Ho8 IeYhYtMLG5AlFl0YYzY6ZOUdrMgusVMFcVgZWkr9Tt3hBV6MTkwAYKWqsTA2Vn7dfeX2 bwuHkZlxcFgFmDSDHflc0+5TFWSgU6mfQUUSH96MsS0z2jZBqSUWdLQLG0Kpo8qqxH/h ux3Epb/S0djhgy+u3b+tcCNkHHJabmINLr8HQh3sxkzXbkf3B2IRXDam+qZC8xSjaO51 jwr2Wz+rsxYdz2OfwyN9NCEKpjzUjrqKKHJuNsHIKLFD3ovVaYcFzSlLdWY4UlFuyqy3 jQ2A== X-Gm-Message-State: APjAAAW/q+uU3q5YxQKMw3RwuKKBBgElF24tcviDNPG1CRo0+N6mwNKJ nd1a5j4tq7cEH4huMHt7HfV+8Q== X-Google-Smtp-Source: APXvYqxkl+KuBnoBixVybAo3mY0Ffb2a1+RZ9YZ8REJNwp1TVju36c8lfb8srFqfTSXP8H6DDZVNYQ== X-Received: by 2002:a17:90a:26c1:: with SMTP id m59mr20848364pje.101.1572247206109; Mon, 28 Oct 2019 00:20:06 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id 13sm11504703pgq.72.2019.10.28.00.20.02 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 28 Oct 2019 00:20:05 -0700 (PDT) From: Baolin Wang To: sre@kernel.org, robh+dt@kernel.org, mark.rutland@arm.com Cc: linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, yuanjiang.yu@unisoc.com, baolin.wang@linaro.org, baolin.wang7@gmail.com, zhang.lyra@gmail.com, orsonzhai@gmail.com Subject: [PATCH 4/5] dt-bindings: power: sc27xx: Add a new property to describe the real resistance of coulomb counter chip Date: Mon, 28 Oct 2019 15:19:00 +0800 Message-Id: <00202f739348258555dcc40982c330542ac61863.1572245011.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Add a new property to describe the real resistance of coulomb counter chip, which is used to calibrate the accuracy of the coulomb counter chip. Signed-off-by: Baolin Wang --- .../devicetree/bindings/power/supply/sc27xx-fg.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Documentation/devicetree/bindings/power/supply/sc27xx-fg.txt b/Documentation/devicetree/bindings/power/supply/sc27xx-fg.txt index 0a5705b..fc042d0 100644 --- a/Documentation/devicetree/bindings/power/supply/sc27xx-fg.txt +++ b/Documentation/devicetree/bindings/power/supply/sc27xx-fg.txt @@ -13,6 +13,7 @@ Required properties: - io-channel-names: Should be "bat-temp" or "charge-vol". - nvmem-cells: A phandle to the calibration cells provided by eFuse device. - nvmem-cell-names: Should be "fgu_calib". +- sprd,calib-resistance: Specify the real resistance of coulomb counter chip in micro Ohms. - monitored-battery: Phandle of battery characteristics devicetree node. See Documentation/devicetree/bindings/power/supply/battery.txt @@ -52,5 +53,6 @@ Example: nvmem-cells = <&fgu_calib>; nvmem-cell-names = "fgu_calib"; monitored-battery = <&bat>; + sprd,calib-resistance = <21500>; }; }; From patchwork Mon Oct 28 07:19:01 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "(Exiting) Baolin Wang" X-Patchwork-Id: 11214697 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CB6DD1864 for ; Mon, 28 Oct 2019 07:20:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A222321D80 for ; Mon, 28 Oct 2019 07:20:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="v6uDInPr" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732641AbfJ1HUL (ORCPT ); Mon, 28 Oct 2019 03:20:11 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:41791 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732605AbfJ1HUK (ORCPT ); Mon, 28 Oct 2019 03:20:10 -0400 Received: by mail-pf1-f196.google.com with SMTP id p26so2148009pfq.8 for ; Mon, 28 Oct 2019 00:20:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=HKtGe92TapQ2KIwqzwkswK8RAIuyaGxBvNdNmUU4dN4=; b=v6uDInPrs2q1qq4SFLLdTjpdDc3eS25cnyOgpZtsm/H1e6dUBVUjCnRTrIW9Je4DLe XqJz6uv+N5YdTtXGCsY5Nwk+YABBQWVqJRGdRpgtWscX0dTeHgTPJbLMsxO2Nn6Hjz3S dbBAmtfdRa2obhR4Db360QfEdyh2kQKCBrXYwvs4DQUeecLSmuAgCNg+8vDcZ+byWbxq kvfNWHa1AXgqxiYlqzAl/rAe0f36GBnVfL8P29Oh6h2SM/31I+Xd91PIcC63SXDmcHIo jZzak31XqYgXmaToPXOP0+62BYK/AyoRWAgahLk4g/uE6jWTpMz8/SETq2QRPAsWDrvB +2AQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=HKtGe92TapQ2KIwqzwkswK8RAIuyaGxBvNdNmUU4dN4=; b=OfgSjXDeppjY9teCFBmzVlg/D7ZeZ89kyXWgUYCw3xXnSBnQG5DiNJHmn4Nc5+OTzH CADKamoG9bhnFVbVyaCIzOSjwEPkUgSJ0S2C/INXKjBh7gGcPmpBu/36LaOpbF0GnH6B rnQPdV68MW0IOVfhEnM+JMmdI+egv/r9Oc0haBks/TatXRggoljctLkZ4WxPko46nOng 8y9E7VNRIcHvFSkW1+U00zX1nIWxy5gBLfoIXWcSM9j+hHgQ1WBJ90//YTpLGTAhA32O LyeiHYRbDTQwYr3alyLHrSQCN8gSxas5crI5Ptl17r+RF1NaJziIJv5R6I2SOVtp9scf EX4w== X-Gm-Message-State: APjAAAUvkEBMD6RRjfwDpz7z3S3GuwkIjn2bT7Heb7uYTBjrSj7jyaA2 R9cHIy5xyl8c4eTUmNi9ln3Ipg== X-Google-Smtp-Source: APXvYqy3mTOff/5UucNGniK8fyS/O+Xgm14NgFfPUR1PrG8IBcUY/kuiLl/NHA1b0kfdyxfyfp644Q== X-Received: by 2002:a62:108:: with SMTP id 8mr9220601pfb.53.1572247209947; Mon, 28 Oct 2019 00:20:09 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id 13sm11504703pgq.72.2019.10.28.00.20.06 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 28 Oct 2019 00:20:09 -0700 (PDT) From: Baolin Wang To: sre@kernel.org, robh+dt@kernel.org, mark.rutland@arm.com Cc: linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, yuanjiang.yu@unisoc.com, baolin.wang@linaro.org, baolin.wang7@gmail.com, zhang.lyra@gmail.com, orsonzhai@gmail.com Subject: [PATCH 5/5] power: supply: sc27xx: Calibrate the resistance of coulomb counter Date: Mon, 28 Oct 2019 15:19:01 +0800 Message-Id: <9c25e3ab9bdba2914b22cfe7495818740437ef27.1572245011.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org There are some deviations between the real resistance and the ideal resistance of coulomb counter, which will affect the accuracy of the coulomb counter, thus calibrate the real resistance of coulomb counter to improve the accuracy. Signed-off-by: Baolin Wang --- drivers/power/supply/sc27xx_fuel_gauge.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c index 221b6fb..f363fa7 100644 --- a/drivers/power/supply/sc27xx_fuel_gauge.c +++ b/drivers/power/supply/sc27xx_fuel_gauge.c @@ -62,6 +62,8 @@ #define SC27XX_FGU_CUR_BASIC_ADC 8192 #define SC27XX_FGU_SAMPLE_HZ 2 +/* micro Ohms */ +#define SC27XX_FGU_IDEAL_RESISTANCE 20000 /* * struct sc27xx_fgu_data: describe the FGU device @@ -84,6 +86,7 @@ * @resist_table_len: the resistance table length * @cur_1000ma_adc: ADC value corresponding to 1000 mA * @vol_1000mv_adc: ADC value corresponding to 1000 mV + * @calib_resist: the real resistance of coulomb counter chip in mOhm * @cap_table: capacity table with corresponding ocv * @resist_table: resistance percent table with corresponding temperature */ @@ -108,6 +111,7 @@ struct sc27xx_fgu_data { int resist_table_len; int cur_1000ma_adc; int vol_1000mv_adc; + int calib_resist; struct power_supply_battery_ocv_table *cap_table; struct power_supply_resistance_temp_table *resist_table; }; @@ -900,7 +904,9 @@ static int sc27xx_fgu_calibration(struct sc27xx_fgu_data *data) */ cal_4200mv = (calib_data & 0x1ff) + 6963 - 4096 - 256; data->vol_1000mv_adc = DIV_ROUND_CLOSEST(cal_4200mv * 10, 42); - data->cur_1000ma_adc = data->vol_1000mv_adc * 4; + data->cur_1000ma_adc = + DIV_ROUND_CLOSEST(data->vol_1000mv_adc * 4 * data->calib_resist, + SC27XX_FGU_IDEAL_RESISTANCE); kfree(buf); return 0; @@ -1079,6 +1085,15 @@ static int sc27xx_fgu_probe(struct platform_device *pdev) return ret; } + ret = device_property_read_u32(&pdev->dev, + "sprd,calib-resistance", + &data->calib_resist); + if (ret) { + dev_err(&pdev->dev, + "failed to get fgu calibration resistance\n"); + return ret; + } + data->channel = devm_iio_channel_get(dev, "bat-temp"); if (IS_ERR(data->channel)) { dev_err(dev, "failed to get IIO channel\n");