From patchwork Mon Dec 9 03:56:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 11278401 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 C955B1864 for ; Mon, 9 Dec 2019 03:57:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A75E320722 for ; Mon, 9 Dec 2019 03:57:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="qOHIXbSa" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727077AbfLID5S (ORCPT ); Sun, 8 Dec 2019 22:57:18 -0500 Received: from mail-pg1-f196.google.com ([209.85.215.196]:46410 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726826AbfLID5R (ORCPT ); Sun, 8 Dec 2019 22:57:17 -0500 Received: by mail-pg1-f196.google.com with SMTP id z124so6398122pgb.13; Sun, 08 Dec 2019 19:57:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=3b0Ff4l5EWpR1AKOGsi1ZTAFeOOljeGZ0Ba9az61bbo=; b=qOHIXbSag1W5InPqUdbn7d/Ikj1LLoCTAWrgTp9dtA91oPhVvxOG1InnSDm8o+8Hiv vLNMNp0OOoIN3ou/EYB0tl2SrEQ/m7VqCUoH619+5mQKayOjG9aG1CFq3OvQkXcVi88j x1FjvPeENgr4uGVQUUF2ldSmIqyst1osL5idJMvSHP/oheex/g1ZGdVrtTvFbUSk5b9e x+OZSFw5xW1wRpg/GE+b0d49ByNyrzl7QnntbSC5/9vR1XAAeqeQYrJBuGmibMB8DIs1 zZrCxb/hQ38mCbLt3EydsyVl9bz60Wf0Epm9E09sSCmATZPd5v6UQvizOibFEe1JW3Mg OIeA== 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=3b0Ff4l5EWpR1AKOGsi1ZTAFeOOljeGZ0Ba9az61bbo=; b=ppXKpkqXmu63aruGGvLe6nIJEjcy8q+Gm3lpEWJe/NFJmc8lpDlc2Txefs/K67SQzA 4badDHqptkSUWoT5MjQUa4apMH8h3+gd/gj8l5AXYRhxvOuTqiLU6RVYoeKdHg71PCOi i9Z86qlOzbQ5Cp3uPY+b+UhPHHv2xyaSBpcBbjODlSs7owL6gUQUDfTz+cYmPi4qnKgR 2b79LWe/8OpjgBXbKHi7WsMX0i62xjGUZwOiySnfQnk+ZeUOEdcO+gyE0RPv877iBDym qKGNHRk9aqxdEFqp3md/RIUK8HTXJGKZUnqKDqUN1KBuJrO6Oj04pam8PUnHKongXbx1 4SAQ== X-Gm-Message-State: APjAAAWSPNjcc2Fc2smHRC87sD2aobilGCEiX14fn9Tkk9F8BWmITOzc jfuWPBZp01GcRlUMidZGJsE= X-Google-Smtp-Source: APXvYqzwJcQ6CzXEnTV64kS+M/i3elI5nznARvaPTCmJ0U1egyQPyJU5QX58hKsR7qHp1UYeBK1BoA== X-Received: by 2002:a63:5d06:: with SMTP id r6mr16310502pgb.249.1575863836705; Sun, 08 Dec 2019 19:57:16 -0800 (PST) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id b129sm24606111pfb.147.2019.12.08.19.57.13 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 08 Dec 2019 19:57:16 -0800 (PST) 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 v3 1/5] dt-bindings: power: Introduce one property to describe the battery resistance with temperature changes Date: Mon, 9 Dec 2019 11:56:21 +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: Baolin Wang Since the battery internal resistance can be changed as the temperature changes, thus add one table to describe the battery resistance percent in different temperatures to get a accurate battery internal resistance. Reviewed-by: Rob Herring Signed-off-by: Baolin Wang 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..3049cf8 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 temperature in degree Celsius + and corresponding battery internal resistance percent, which is used to look + up the resistance percent according to current temperature to get a accurate + batterty internal resistance in different temperatures. 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 Dec 9 03:56:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 11278403 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 B75D61593 for ; Mon, 9 Dec 2019 03:57:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8BFE42073D for ; Mon, 9 Dec 2019 03:57:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Z0EXXeID" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727045AbfLID5W (ORCPT ); Sun, 8 Dec 2019 22:57:22 -0500 Received: from mail-pj1-f67.google.com ([209.85.216.67]:34787 "EHLO mail-pj1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726826AbfLID5V (ORCPT ); Sun, 8 Dec 2019 22:57:21 -0500 Received: by mail-pj1-f67.google.com with SMTP id j11so4148079pjs.1; Sun, 08 Dec 2019 19:57:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=DvfOTSg0d3rIyLlMu8H3HPhkwjcsj+59P86hzMtRDSE=; b=Z0EXXeID/Ki3Q3QUCzK+JWeQ52+SZ/PurD5mak7MN/lM7sj+jtLC7TsB/+q7Eyngwl UTQywzt1lStNtcmkgDRXd/wk/JOyDkz6g02hgleuW7jSSyVgJFjz9mzu19iXZOG8syse 6fY0m3BXY/R16junqFEbM5//LSnvRHE3ybWXqA/bEPxweED/JsgA2rcKW5jUt7KeI8rp 6CItpbDl0WwbGe3DWsAWP71nrkGQdv9Typz7oVmdG7i4n+thHmE3379jvUivE3yXjVAW aO1DRCbOugtEaPdcOFlinjMyZYr3ayPSjWQ7SJ1pFfcKbLkbVVW9DD+erKvJ1ERMcV81 SMMg== 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=DvfOTSg0d3rIyLlMu8H3HPhkwjcsj+59P86hzMtRDSE=; b=jMFxFS/Gr4In0V4gsbj0kpzbIAZclHXZTsdLH2XdYTtIqA/NgAZiwxNljNei/4SikT xpTymJXIsp/U9Bew5JzlOiUIwfTIup/ZAFIJI/bXVERCGht19LRMoxQNCnWFRlUgcQQm pEVXCYfe0Ss9CfKlBUvHD6/I9LpRNHTpj4/47IH+UKTqhijHEX+4tBaIiwUy/HW+b8wz ZlGiKq3hwWni7FWaQ+RXy1nuhGG7Rxb/O9RhCKtQyVebMd89dYw+1dlaQ11lrxizHIKv Sy6YpKYROCJqf1ui7r382IP6HDbwGjRNwsN4H0MwuG0o2dDXO1afcmmych/MWHLuJrzB Qesg== X-Gm-Message-State: APjAAAUtrEYagZ9Hh24dG3S9T3U0Sf8MEMJYYYr4g1RxbtAYpO8m4UZ8 9jfvSFawHEwaZxf4UpZWh6E= X-Google-Smtp-Source: APXvYqyz8L+9MDmpwoASslmwZRqgoSTOg1Se6PsiC7u3n/RxUbGp2uPFIH9Jgrq184MqyjtVAAKqNQ== X-Received: by 2002:a17:902:6b45:: with SMTP id g5mr26842153plt.159.1575863840863; Sun, 08 Dec 2019 19:57:20 -0800 (PST) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id b129sm24606111pfb.147.2019.12.08.19.57.16 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 08 Dec 2019 19:57:20 -0800 (PST) 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 v3 2/5] power: supply: core: Add battery internal resistance temperature table support Date: Mon, 9 Dec 2019 11:56:22 +0800 Message-Id: <4a8d2ca570f8fcb33bb4a0b8d34a4b038cace083.1575863274.git.baolin.wang7@gmail.com> 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: Baolin Wang 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 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 Dec 9 03:56:23 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 11278405 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 1D18313B6 for ; Mon, 9 Dec 2019 03:57:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id F00D8207FD for ; Mon, 9 Dec 2019 03:57:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KdP+ABq+" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727132AbfLID5Z (ORCPT ); Sun, 8 Dec 2019 22:57:25 -0500 Received: from mail-pj1-f66.google.com ([209.85.216.66]:33781 "EHLO mail-pj1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726826AbfLID5Z (ORCPT ); Sun, 8 Dec 2019 22:57:25 -0500 Received: by mail-pj1-f66.google.com with SMTP id r67so5276640pjb.0; Sun, 08 Dec 2019 19:57:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=k5+zjDRRhhqO491xHEoAU6VTv+EQua9/0+Lq9coQ6dc=; b=KdP+ABq+8Z0LzLBnD+4dkYtV2EJBlYa8pgmDg/uSP/ZirM8CJ42r80GcUUEZoNdI0J AOtZE7OZRrXNuwK8D9lIRYRdXzRd6EhQqIWo5+nVBn3Ng8JTskFKXgMoNvSTot7zo3WS OBpcerZohcb/bIoo6Qd0qGFBsFc9c91y4Sq+5UJKF6xpA4VfdeGsf51c2wcXwPcxxxzZ czBV0GOUn4pXO7qEaP7IVohJ+Si/K6kKvo9shgKKuXpAa534WJrXPHSZt0Wugsp8eOJ6 3P5fn9Rd80VJpQTUFHcZcO5H6XQzDjnHk/9OkkVlfTFLYDWTHJxOIWVa4kPhaRk6G+9A batw== 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=k5+zjDRRhhqO491xHEoAU6VTv+EQua9/0+Lq9coQ6dc=; b=bdSwwvfdSDSl3GLIIkJbs8VwFg3y8UGI9iLgvSmxd53YEvvTNYoJr+S1AlE/Nisp1V CGIHHihZlDYlHiFbWrO2NHPS0FBlXMQ/7cnW0AyLboOOo9YLT9X70mUalTojxPQvHDWb QS4uu/4Herb/tBcVSI5a5eh0vcvGcrSe937ztTTXptyvatgnu5rfT3cUB1NlvEt+FEUW zOUa+TbAOlEBuk6wGOOAtPJpe5DbFP8lqjOgFFEIC3K55iMpcG6/JU2WndkkVFq3A6F5 dO6QOy9Mr/HAF2oLNxgH0vx0Md72j9/4BkTtw6e9Oeexw329kkFIH+iPcwOd1UNrdipT h+iQ== X-Gm-Message-State: APjAAAUTc9n+adPcZSfnyqU2X5OoPtlkwbgqy/XYZy8hxI8latv5g1gh UkpviqHCD46YSnWiKI4OUcE= X-Google-Smtp-Source: APXvYqy45e7awVfFtJobgsw1VBWiM2A5JEwtoJXxqxUqW+5OOGTknwQFtVNxOCQd47FfCaEUjo1Kzg== X-Received: by 2002:a17:902:12c:: with SMTP id 41mr27181156plb.224.1575863844491; Sun, 08 Dec 2019 19:57:24 -0800 (PST) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id b129sm24606111pfb.147.2019.12.08.19.57.21 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 08 Dec 2019 19:57:24 -0800 (PST) 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 v3 3/5] power: supply: sc27xx: Optimize the battery resistance with measuring temperature Date: Mon, 9 Dec 2019 11:56:23 +0800 Message-Id: <2c3b89e093dad0cfe8e8dadcc09a0a466f5c5409.1575863274.git.baolin.wang7@gmail.com> 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 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 Dec 9 03:56:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 11278407 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 75D6D13B6 for ; Mon, 9 Dec 2019 03:57:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5225320726 for ; Mon, 9 Dec 2019 03:57:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Mw2JAZco" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727151AbfLID53 (ORCPT ); Sun, 8 Dec 2019 22:57:29 -0500 Received: from mail-pg1-f196.google.com ([209.85.215.196]:46014 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727059AbfLID53 (ORCPT ); Sun, 8 Dec 2019 22:57:29 -0500 Received: by mail-pg1-f196.google.com with SMTP id b9so6014917pgk.12; Sun, 08 Dec 2019 19:57:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=WvZp1SE31U0tt/atYOk2/QylIK+izOWm2BYL5+FBcUA=; b=Mw2JAZcoEUqf6EGy1zg/hy/E3EVf3xrOZkrP18byG7HswI9cbt6V8g9qnoqnc+c2j2 r8Ob0Q95DglnMkXLXp8DFaPmxXx7jyUo/CLWykXn8k84Nj2FrF9YRVlXXDv2+bmRzD7K TL7a9zJWbEgqsOkcI9HppyabDb7x/9GgE5YFT3y7IGuYoFFa7HDzmCU9U+NRjDWeeat6 caUxSMBTs2rh7C1OFjmGAF944EwGRwMhnkLhdsAgTO+V+Yj018MtlGx8zf61e5cgyWeE pawWJEbnH3JvxDzdGSgW3m5cfT1yAuVhvoUW7XyXs2pZPeVYlcGbQCKqWKliOYfqWvCU JRbg== 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=WvZp1SE31U0tt/atYOk2/QylIK+izOWm2BYL5+FBcUA=; b=FMTNustypwCpOmS3qkynHScmLQirzolhA4mOw2WHETnzVHvUl2ATktZaQ/s6iOjnxf ZQWQou/5uZOqgs4uxX/xvrR/zZf7xyZTqBeVXZ5yodkViYejroI7tk4SVj0LCz+IhYjz lFwtYkj9PHJnhclu5krq60qZ/5HqmvqYbLJppoik2JB/aqD8JVHNBX2OyHUqskKvKr03 ndyzVN7X+I6jCHjvAGB5grwiLoxkdOoEjcQNmm/mb4Qf3W0HiZCdhjVsA9NNvrUtEYDL gd4+gtqB9WEei10K8EX7R83/vdYb87F2fqcI0026G8WT4twTjwH3R5ZdnM3d55wVkeEq /0jw== X-Gm-Message-State: APjAAAWWgeTrtHq74RWfqhhwN67XyGro7vOYZ3mvgbE62bONR4vkfYSN NCZzFG+Gxclnkhi+NoZLQV4= X-Google-Smtp-Source: APXvYqxlaM/m4kdUox5LXGauDISDAHsVCxARSeYYLIO0fv5KX44dImjVv1G8ciykxTwZoARRTE2qrw== X-Received: by 2002:a63:5062:: with SMTP id q34mr16590505pgl.378.1575863848339; Sun, 08 Dec 2019 19:57:28 -0800 (PST) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id b129sm24606111pfb.147.2019.12.08.19.57.24 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 08 Dec 2019 19:57:27 -0800 (PST) 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 v3 4/5] dt-bindings: power: sc27xx: Add a new property to describe the real resistance of coulomb counter chip Date: Mon, 9 Dec 2019 11:56:24 +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: Baolin Wang 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. Acked-by: Rob Herring Signed-off-by: Baolin Wang Signed-off-by: Baolin Wang --- .../devicetree/bindings/power/supply/sc27xx-fg.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Documentation/devicetree/bindings/power/supply/sc27xx-fg.txt b/Documentation/devicetree/bindings/power/supply/sc27xx-fg.txt index 0a5705b..b6359b5 100644 --- a/Documentation/devicetree/bindings/power/supply/sc27xx-fg.txt +++ b/Documentation/devicetree/bindings/power/supply/sc27xx-fg.txt @@ -13,6 +13,8 @@ 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-micro-ohms: 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 +54,6 @@ Example: nvmem-cells = <&fgu_calib>; nvmem-cell-names = "fgu_calib"; monitored-battery = <&bat>; + sprd,calib-resistance-micro-ohms = <21500>; }; }; From patchwork Mon Dec 9 03:56:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 11278409 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 60CE013B6 for ; Mon, 9 Dec 2019 03:57:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3EC2F2073D for ; Mon, 9 Dec 2019 03:57:39 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="sqcMknGs" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727174AbfLID5d (ORCPT ); Sun, 8 Dec 2019 22:57:33 -0500 Received: from mail-pg1-f194.google.com ([209.85.215.194]:41179 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727059AbfLID5c (ORCPT ); Sun, 8 Dec 2019 22:57:32 -0500 Received: by mail-pg1-f194.google.com with SMTP id x8so6419523pgk.8; Sun, 08 Dec 2019 19:57:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=4HDNu++S9qVPoxkV4CPwjrOYl/qB6CAWEdk7e3/cozE=; b=sqcMknGsspTBQziNyBMJMVe6M2sd44GR6m5Np1BfjWnjkWLi6GSpN0wT031lsH2esi fT2BBZbgASIS8GIYDC/9Wdvldq7pYqUWMnet3xJ9RA2cInPw8mcIibEaPXGWfBxBRhN/ TvHqdkgSyr+VWmUAefKBT0/lq+bFhSfJFlFRdhCJymD7BPRgjKCO1zwJZI1YhSBX3UOB 7bkYgUOlmY8b82OBb0RUtses0Oi8VI/amY8kM5lKbsnSxdfvFduNOpDImei3+9LxwxKG PjEbSIbE+l9jW/KSjI9yDu59zgywEXeYbVGYm0ZKLtg45qMWZPaJs+6Pw4VpMU3iVLlv VbWw== 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=4HDNu++S9qVPoxkV4CPwjrOYl/qB6CAWEdk7e3/cozE=; b=k9vad3GoWZFoophZ9Djo2svRixeC/lwxI65FOXJIn9Nh8Zb4zyQYmwLnYzJkN+TxaD WJD6VXZRcmg4u0XLQlSOu0PsGw5qR/zW5wu2l+WNb3DV/0FH6cvIYJHd2GB8lTgRn2Yi bLBL+9Xwlj4SFTIRaiNGv/GmuJLN1n6RZM/ByFXhSrDv+aSsQBjy7FHA73gjSb/iLTb+ lH0fnuSsWe3xmkgCtB4U5VdxwgZM7Vq4QCTQw31J/k4OpflZxvqbrlRxaQS/H2CW94nC N1AlZ4pFoOypuI2MkkSGzdcWjGxDHFAZKgdJ3UhbS43vunfhLTP7ojfdK3Wog9DHHJpO FDbg== X-Gm-Message-State: APjAAAVi3XriRf5ytycIKc11WKY3Q10cf8YFPRSdr8H2zUtfd5/z5Q43 rcR7vp5NWIeZQZuALYjCq4uE3p1+zmM= X-Google-Smtp-Source: APXvYqzXnP92wBKh9BZKaI8aJgmxF/EhErmpuCiKjkGGK0JSl7eQu1UiMNW64/R0p2YzvLmNZtiBgQ== X-Received: by 2002:a63:115c:: with SMTP id 28mr16448493pgr.6.1575863852166; Sun, 08 Dec 2019 19:57:32 -0800 (PST) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id b129sm24606111pfb.147.2019.12.08.19.57.28 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 08 Dec 2019 19:57:31 -0800 (PST) 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 v3 5/5] power: supply: sc27xx: Calibrate the resistance of coulomb counter Date: Mon, 9 Dec 2019 11:56:25 +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: Baolin Wang 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 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..951d5d1 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-micro-ohms", + &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");