From patchwork Thu Oct 31 12:22:40 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: 178166 Delivered-To: patch@linaro.org Received: by 2002:a92:409a:0:0:0:0:0 with SMTP id d26csp2760102ill; Thu, 31 Oct 2019 05:23:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqxVCePyq/YriDb84bUQcgucQDuwfLdCjDqzjE9TejHzqZip1+Jcstol0WAHL4fZKxa+CHpQ X-Received: by 2002:aa7:d305:: with SMTP id p5mr5799713edq.80.1572524622450; Thu, 31 Oct 2019 05:23:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572524622; cv=none; d=google.com; s=arc-20160816; b=SL+AgGxoL95zZubqyJIL4JmcqOcxZeGc5nWbM3LcL8okhnht3diebK+CbZNl/9fVfq UwT4fFiE95j1bwHYxLRTYSllhv4VVg4Up9FyH2oRMIerzsaniWEsXJtOI49XOZ+hp983 KJGNNghRxQewTZ4Eaoyd+vfjLbnRTk0v9o7XZBcxAQV9BWnQZRYI983pF+2T6ccGyRSz iOxJLxw/Y8GZMQqsYiCs9JGgDUchLYpzDJ62Uc6T0lJPtbYZD2XIPfE4bA9vAK+pjUX+ d4sxn7V3E4b34DR08jGzf71pPTJ/ewhW9wQe6jXDdEc9SFl5heHvLfpqmM0Irou1vrGi 6EHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature; bh=3EOM5oayeFhI5olpubTkJZc1yJ+pE3t3ormYT5/k2xU=; b=kP+vxuSvOmj1bVSEList6BBOZ3j5oBCGF+lVHu0/Kj57SVaYCfKrnxti9aChBE0nwH XqsbupVeyYzHL1e/onvaGedFHy7ShMHHo2B7makQn+9oax50hIM5M025i5JB13l2+tIV 98iRVR00/4z2EtCvKxcULNl+gS6Ulv0rDmGOqow1jLdUs0WBpdJN8I3CLIwOAbc56e/G Wsupo818ITOC3p8q2aOKubkNS+I6zs+HQgqpcuGaYGbIh0qMaxe0UhYycAkgIrOAW95p yJZRPXpHfrQwjdOsZjgJd4GbbDi7S22pnOxoT72xzla502Pxh1iapmHAT7u1wzjZFDWP xLpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=yHGo5PEL; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y15si3461700ejq.159.2019.10.31.05.23.42; Thu, 31 Oct 2019 05:23:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=yHGo5PEL; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726952AbfJaMXl (ORCPT + 8 others); Thu, 31 Oct 2019 08:23:41 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:45113 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727054AbfJaMXl (ORCPT ); Thu, 31 Oct 2019 08:23:41 -0400 Received: by mail-pl1-f196.google.com with SMTP id y24so2637257plr.12 for ; Thu, 31 Oct 2019 05:23:40 -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=3EOM5oayeFhI5olpubTkJZc1yJ+pE3t3ormYT5/k2xU=; b=yHGo5PELukGJE9+DlmbPU9ub+QasVUdgD0N6BsJWeVaWMmn7fCQNlwxtZgV/XnQLOz Qlo5JOuq8hYX2/7b1uW0HABZKfHq4/zHXVp2aKGUEPdQpOwBU7Jy0X4iKRwplLfgg9Uy j2TnRv6GlHf5qJDXpiA2SviX4bypaq5Co22dQcrH34GYKMIQmUqatwcUCGhoCsMQR3d+ zXwrI0zu23eKq11aQOybq4T9cYaTEE+PNGNiHafUL+cwmpZGePBZXwVQE3ffFNrHJf0T vvaD8KMx34m14qG4r0mnvV1Ply8Y+jnVM2OnpQeYEwGf0nnMnkNDcGNqPUVGT/CIL9/f uegQ== 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=3EOM5oayeFhI5olpubTkJZc1yJ+pE3t3ormYT5/k2xU=; b=tH3XXfK105ZDOaW2Q6frXV47fJGMGBV8SdUbnZ/ovGRUJrxn0odIG8qhzEbaVQ61jU piP5w9vhGRmnvkTCBxdwfpAuESWANTRA4rOb1OkPrEHpOpyzmn/cp5VH1FRKmkXU2KQI 0tZRuJM2MfWpexuZJd43G0XxZJePfVr1Q0qPg920p6Gq6yhI5FTor7P2eqoAPu7rp8+3 7SNyZ0h1bqW8XfJDABSPcUVxPG7ZpnY5QSdvaq3yD/f510zOfQS4yv51y01pCak5LQoH OEvY+sSKaJoqgqCl5sNvtfRAX+iNrNGWWMUtBweb5wWB9wjGOeFxHRlObnXdAWIkb6kb Qm5w== X-Gm-Message-State: APjAAAXZMblc+49heP2FlwsenRG4P6jPCL+K2JgXQ6mzlo5fsgi8n8HX ritVIKx95WDHXcNzSWQDQMQ9zg== X-Received: by 2002:a17:902:fe89:: with SMTP id x9mr6340644plm.62.1572524620180; Thu, 31 Oct 2019 05:23:40 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id j25sm2983077pfi.113.2019.10.31.05.23.36 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 31 Oct 2019 05:23:39 -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 v2 1/5] dt-bindings: power: Introduce one property to describe the battery resistance with temperature changes Date: Thu, 31 Oct 2019 20:22:40 +0800 Message-Id: X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: devicetree-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: devicetree@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 temperatures to get a accurate battery internal resistance. Signed-off-by: Baolin Wang --- .../devicetree/bindings/power/supply/battery.txt | 5 +++++ 1 file changed, 5 insertions(+) -- 1.7.9.5 Reviewed-by: Rob Herring 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 Thu Oct 31 12:22:41 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: 178167 Delivered-To: patch@linaro.org Received: by 2002:a92:409a:0:0:0:0:0 with SMTP id d26csp2760196ill; Thu, 31 Oct 2019 05:23:46 -0700 (PDT) X-Google-Smtp-Source: APXvYqyZFSt64sQq+yTeWMrJK26c9+blxepwBaMQKo8mZhreWKvTTtr3zMrQEuXyZLx69VmDYsM4 X-Received: by 2002:a50:f096:: with SMTP id v22mr5752626edl.149.1572524626333; Thu, 31 Oct 2019 05:23:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572524626; cv=none; d=google.com; s=arc-20160816; b=TVz2ivTMwkuR9QXx5Fqb2NgjIWkS0iiRwp54Qtdi5f2dEKpyunZA3xFbYlXJQEzjlL vne7AOQTKHPhvPelui83hc+dkp/mIXkZBa6Vomv+gw424AZ49kU8Exrn0wzmG4dZ9uzx /8qKDTi6bSFLqhswh2KLdJbo2aojkmhcWIFeT0lX9OUewb46Pru2wipdwOa/ypnO+mde icyTKs4ySKVc0lMDsxaaihA+00Is129hrKvl8hmA7vJtXmwJ2H662VDmREke5FwN66+t fGl8QMHVqY0qM7xfenRpk9pUdDL2tEv4kL3jK9nL8nNrjjWzvN4vejg1uEcsdD48VhCl APPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature; bh=zQKRY2XE5oS+mi6BE44agpH5JNOXqmvzvanMV9EmHYs=; b=dj/Md32+31llzjTGA/XQ+Zo2tcRVwPNnrbPJKjJLi9kjyBKg3vOC00Q1X6o6l85Uvt +PAQvYUi35dbBI/n3g6QjaOZaV6W4rpaPzLSGxKVee6+HobEP8iCAZy1xAwHwzIcb3fl 1ub6IOuZcgpklIlkN4rTRYanrphkmjS2JWnEmQLC0I/LkeKn52yA5kpMvEZuMhMzpkvN Wup4eTsZ4tzhPbeADcvkIKrvAv9vCNTvReMDcQKruvs47SL3tN01C7PhW7D3U7uLP+/1 SjQczA/U4VBmcVxSdcZrT/APbIUMcDHX4kwT8cV4uPT89ibx/hBj24ACl0RnuaOmGOaz 9fDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=cLIQcH9d; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f54si3772755edb.255.2019.10.31.05.23.46; Thu, 31 Oct 2019 05:23:46 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=cLIQcH9d; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727315AbfJaMXp (ORCPT + 8 others); Thu, 31 Oct 2019 08:23:45 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:44912 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727310AbfJaMXp (ORCPT ); Thu, 31 Oct 2019 08:23:45 -0400 Received: by mail-pg1-f196.google.com with SMTP id e10so3942882pgd.11 for ; Thu, 31 Oct 2019 05:23:45 -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=cLIQcH9d9VBOuW5eg1WEB5cH12kHdtw3tpLXDCqui+gGLI2o+7YmxJeMpr3bTTID22 OxrVgLn7lpnpBFmynNLHesa80WLHSG1l1PQOM92u6713UOvNwXflkXZNdNY4MS2WMztn 5QvPoMfZiu9X5Kwdqy4XwZKlycdtyH3cLehgxbzfTn4P9qeLqQ60qm8/vRDXUNfi5oU6 J9fN2TF2DbSg1+UCZu3XGYnYUhlbE7r6+qHwvv+UtvcO92CHjzbwqmmzSc4MhwgKFwoY wCVaTSmQIfpf8AivSJE+7TFrbb/Qe3bg2EbtvphPWKjHzBOjKLO0MPfGhElq9/Y0VcFX Ax4g== 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=bB2mdy2YDTKuT4d6pGQp6c3IuvNR8Ngm9tJ3Rz1deyfUMN2IVJmnboRHDReoBlXUCO SiPX/oiSAAjxm3MX5xGvCJStt+CtsEZmGpS4o63qPLtKib8ZyTasuUUGhb/sfp77ct7g PvbAdxOwksBTVBiemjDuUzSSGh8KT6TITyDIo/z03wisBrFKg3Bz7ikUUq6Pf9rbp5Eh DY3CVquMm4curKN8AQjfwTK6N4hcyDDxPdXLmLxx4EOpYxBzRYE1wv8ID9iuQNkONBwY sYf9Tp8CxPzkrgvv+ATCxhTSj07rTspnuVaPoN8zC6gPTkPRKD4huVot4h72FKlGbpZu LHdg== X-Gm-Message-State: APjAAAVkbCzCFd1IqgR5+GXtPyrqntls8zzTQodOEd1sJBzXCV1Bw2jE nsdJPGp1DyLN9/ycGoZM794CXp2k5k45/g== X-Received: by 2002:a17:90a:9293:: with SMTP id n19mr6915757pjo.67.1572524624617; Thu, 31 Oct 2019 05:23:44 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id j25sm2983077pfi.113.2019.10.31.05.23.40 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 31 Oct 2019 05:23:43 -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 v2 2/5] power: supply: core: Add battery internal resistance temperature table support Date: Thu, 31 Oct 2019 20:22:41 +0800 Message-Id: X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: devicetree-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: devicetree@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(-) -- 1.7.9.5 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 Thu Oct 31 12:22:42 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: 178168 Delivered-To: patch@linaro.org Received: by 2002:a92:409a:0:0:0:0:0 with SMTP id d26csp2760818ill; Thu, 31 Oct 2019 05:24:17 -0700 (PDT) X-Google-Smtp-Source: APXvYqyi7uznvm6zL7r1Zy147ojQMs1in+dgDRNcx/2A54oletugsrk5OXoDLdmWcS7MuU7rLJwA X-Received: by 2002:a17:906:66ce:: with SMTP id k14mr3588701ejp.92.1572524657452; Thu, 31 Oct 2019 05:24:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572524657; cv=none; d=google.com; s=arc-20160816; b=XvNc/kdMsH6momd1qF2nYK42G4VqC4qUZxWyXPYdHE9CNAqwpufJjSA8V7PorgigJm 0q+bIYr+1SAe5jCzuo8ocViASJtoIH+HcFV7OBYQ5EmhIITJHOcsfLHzMP4/JrDk4TfV 9Ltapj7WeCD0u9DQ7Zsv3qUes/sCdYume2JZr0Z0WF6pBwR87Oe/Rzd/P3aijorYJwgJ 2y0Sm/vOWxfQJZGYpZ7LlT5Yf1k+IDwik2t+a7YGb6hGrgJQ4aFu1L8Fk7B4puDyRW4m OzgTJHOp3fYaK0pkPzJobqpy0zzxBDrXA04JvXRmkJJE7pJ/kuSZHvPWewyaVN7eTZ1S 68BA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature; bh=ipn4aXAOp87D2v3r9KdhlrkAx9KnL+jbYVkv6tX05Nc=; b=GXi+ce7YnMTMe5ScBH9wEkaWtGdafbe7tDP9N+FujF/PhL9oSxmnZ6TAmRMmupbeFh qD/t1aWAek0AKZDEbN0LzqndFI4SvZXaQWLtAmCKs6YvU2396pFa9EiKkPn6WPydoB4v A+GmLgbmQsHI/zYT0cAJrgQTcKcryNsy1/vItr9L3vH3sgwBBx8zuCi56YVpatesSzUI hl1PMWSss6ahzNqtGDNwx4YdzQ548uK+udLBqdgW4+VwjdcXcciXOWsuAmfapqbr4tu1 SkQh9LatJME/DLFY356fKmNRuX/J6RJqKSdw2hww37brMHatxO2SfKj/xWN8e6OfV4Wo GjLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="Ts249/eg"; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f54si3772755edb.255.2019.10.31.05.24.17; Thu, 31 Oct 2019 05:24:17 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="Ts249/eg"; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727364AbfJaMXu (ORCPT + 8 others); Thu, 31 Oct 2019 08:23:50 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:42278 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726728AbfJaMXu (ORCPT ); Thu, 31 Oct 2019 08:23:50 -0400 Received: by mail-pf1-f194.google.com with SMTP id 21so4260098pfj.9 for ; Thu, 31 Oct 2019 05:23:49 -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=Ts249/egnrhzEnpXd834uI2TsYDQGJUwwxIXs5mn5C+Q4KZiVMKkCBPTYzys4nYqgq xziLKywtITszSHspc6rib/y5uXeygD6cYUVj4k1TGPpDyI5VdC9JdZJ0SbRlx/uFMx4q yqlRwdLtYMNEnlDwSS1VfDuyr4zuEb8mmG8e7h0Wgw5lIlEcPX19EbMBKah2Bq53HI1v SpMA7AHFE02wNG4vvt+SB6TCGEo6ZUQT37b+DgdXeKv5sdSTpFjzx8+yEGB0RRSTa6IY 6+iwybqUmbH21IGe8G0TCqltT5PMjVbhBcYLhiGWYPZzW7Azs4ZcZ3XPJF+bWTIIKHEo FRIQ== 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=G7pV9/pVjEWbpwiXyHKlQzJGTsjCzaCbqZreu3UwT7/8FOht/Ombnj6r9vhWThAMYD 5hUgHd7zsGRA6q6DaFj3ofAEc/jf/HGjxxvdOh+AANdJWjlaaknR2YR9B9rU8aS8osEd G8MHsN8yPEWb2UpgSYl67s7ORpvomeQggj8+Q2OA93G6vj5sdf+DEXGIvalgCZste5iZ 70YNeL5u/xrANdqnC2cuGF9kXalcZjH7yHKNHq+c1zVJ/Adu60bhIp6bU8oEaRmBVIEI 3saoD8skLgpT7Oocyo3xp0KtxRhNxFXgjoPbCFD+Fdhv03r/KepjFtCt0ZM7QtC4CjLI gJvw== X-Gm-Message-State: APjAAAXBvtOCfCbQPbSDppjIIJ5uQmdoqnvfVq1c4S+tJsdZNW4mauQa PlJPU9K29mK5kM+8XOBNak0nxQ== X-Received: by 2002:a17:90a:8816:: with SMTP id s22mr2159357pjn.31.1572524628486; Thu, 31 Oct 2019 05:23:48 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id j25sm2983077pfi.113.2019.10.31.05.23.44 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 31 Oct 2019 05:23:47 -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 v2 3/5] power: supply: sc27xx: Optimize the battery resistance with measuring temperature Date: Thu, 31 Oct 2019 20:22:42 +0800 Message-Id: <3dbd343189aa027c00148a5d8365661aa8b4c7ff.1572523415.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: devicetree-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: devicetree@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(-) -- 1.7.9.5 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);