From patchwork Mon Nov 5 07:39:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\(Exiting\) Baolin Wang" X-Patchwork-Id: 150146 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2286360ljp; Sun, 4 Nov 2018 23:39:45 -0800 (PST) X-Google-Smtp-Source: AJdET5eG6Nis3lOoiWfH8iLxqeqMdPIQKQFgH4v9x72jrnNL1cXmOz2CYp6VPNVOHTUMVQGFReIu X-Received: by 2002:a65:4b82:: with SMTP id t2mr4937802pgq.189.1541403585448; Sun, 04 Nov 2018 23:39:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541403585; cv=none; d=google.com; s=arc-20160816; b=xjICclKz52SBefgu3irPdmEtP9HlfHVhYrtgCynY0H/ZAKJoSrCRc9FRU9QVF1mTGL yUkAcXkAILpSkYK8uCjr1L9HQWnAnX5Dyyg40a2YNHruHpF/ryGbgDmEnLQOV2F/OkU1 SbF1aFKOcUaYjvyRMj2ADlS+ncmjltsolZ1T3kdbW6lR4RTrUJBf1nq1iH6fpa4tQq8f EmI1g+f9Ke7rvnXx7mBVhnQAJcptyBFWMhpGiwdowScOzrh4NmG2JRKixx5yy/xfBfDb W0dPFzCmHgv//QLO7TXeg9CzEun/fsJky/II8J2gCSyvo8txkwAV/jXwG+AJj4nUWx6u qsIw== 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=OcfqNyqejbDF0lP44ym38PLblWl+qKIlZIFOYmEvXqQ=; b=nlJaXGkLDkvs3qKAfuJNdrvYzqLJudNB6S1rTHxgXSP6t3dsYEjB3BlVkDLSV2qTau 8EgpLyiD+LF1v3fLrHT456T8XcCDVehn4Tw4T2DhDSVnV3EOEVcP4VSq97xTAmjijwh5 HUmOXX3B0gmxwCB/+Na3Am1YA/CvgqW1vuL5FLsUVtYDqfv7bT1gkRleT5cjCPLjg97v quAxxXaLofud7ynrK6CrjGzVM2RosZk4Ty4sUBK2markUjzK1WGTN9ttGqcajPWgy/bn /FsWbNMw7t1NQ6x/WiPrqcCV1aBBOiTHi5h3vr85wnsOJtZBVnOlzB+KzXYmF/UCv1fe 6J+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=O0JJ9BWz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-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 k135-v6si28370755pfd.239.2018.11.04.23.39.45; Sun, 04 Nov 2018 23:39:45 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-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=O0JJ9BWz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-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 S1729734AbeKEQ6D (ORCPT + 32 others); Mon, 5 Nov 2018 11:58:03 -0500 Received: from mail-pg1-f193.google.com ([209.85.215.193]:33023 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729458AbeKEQ6D (ORCPT ); Mon, 5 Nov 2018 11:58:03 -0500 Received: by mail-pg1-f193.google.com with SMTP id q5-v6so3835433pgv.0 for ; Sun, 04 Nov 2018 23:39:42 -0800 (PST) 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=OcfqNyqejbDF0lP44ym38PLblWl+qKIlZIFOYmEvXqQ=; b=O0JJ9BWz0qfPhkLH5mThC9jY+E+pcj9IPegDH6gKrI0gVQK0JeA6wybNjNeEQKXKw7 L6xZVhqiDq4bXf8EkGv0mPnsdi9H/neRzfHcNgXTbwzKT0boCrHsVCPjrT3688Q/pNjV 89p4/QZunZv1qtvRd0kDTyNdqUroW2MhLV+b0= 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=OcfqNyqejbDF0lP44ym38PLblWl+qKIlZIFOYmEvXqQ=; b=JUjZSanT9VnGjNohPWvSVEA9UTh5n7fgjhxqbEJGpy4e18WBewIXf6g399q9o0q773 fGeElTVzS2c4XVStE0Bfroup/gJh7rbTpjJH154o0PBwq4uhGHsiX4RduPMfwE1mic1/ 35h1v8FQoXblW1iJsausTvkigUUjTjNvaCKI/c5WEkJRsAMtEbaJbcbqjrJFe5OafF4C WskUIMuTuAzyGQxYNDgZRGbJEfjHJQCC9DYDHI3b/q4l6bD3zKgeX6Z6oRaJIRr9184F wSc54sNNx9g8SNPE3opRJUhChwuLl+Uy/uzTEqwDp3bGlhftYkBKzEt7+1k99D7AuV+s ggyQ== X-Gm-Message-State: AGRZ1gI43ZNXRvO/ohB9DjFGF/stRuxTcgBP7sm8P7deTWhAhVqB+ME3 CziYvQQadlo3R6KMWcqucrHVyQ== X-Received: by 2002:a63:3546:: with SMTP id c67mr19486004pga.284.1541403582558; Sun, 04 Nov 2018 23:39:42 -0800 (PST) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.102]) by smtp.gmail.com with ESMTPSA id d5-v6sm27632040pfo.131.2018.11.04.23.39.38 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 04 Nov 2018 23:39:41 -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, broonie@kernel.org, ctatlor97@gmail.com, linus.walleij@linaro.org, quentin.schulz@bootlin.com Subject: [PATCH v7 4/6] power: supply: core: Add some helpers to use the battery OCV capacity table Date: Mon, 5 Nov 2018 15:39:09 +0800 Message-Id: X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <5640f29e0b7f46f9ee77d80745f8c93eda355c34.1541393063.git.baolin.wang@linaro.org> References: <5640f29e0b7f46f9ee77d80745f8c93eda355c34.1541393063.git.baolin.wang@linaro.org> In-Reply-To: <5640f29e0b7f46f9ee77d80745f8c93eda355c34.1541393063.git.baolin.wang@linaro.org> References: <5640f29e0b7f46f9ee77d80745f8c93eda355c34.1541393063.git.baolin.wang@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We have introduced some battery properties to present the OCV table temperatures and OCV capacity table values. Thus this patch add OCV temperature and OCV table for battery information, as well as providing some helper functions to use the OCV capacity table for users. Signed-off-by: Baolin Wang Reviewed-by: Linus Walleij --- Changes from v6: - Use devm_kcalloc() instead of devm_kzalloc(). - Add some comments for power_supply_ocv2cap_simple(). - Use devm_kfree() instead of kfree() in power_supply_put_battery_info(). - Change to u8 for i and best_index variables in power_supply_find_ocv2cap_table(). Changes from v5: - None. Changes from v4: - None. Changes from v3: - Split core modification into one separate patch. - Rename ocv-capacity-table-temperatures to ocv-capacity-celsius. Changes from v2: - Use type __be32 to calculate the table length. - Update error messages. - Add some helper functions. Changes from v1: - New patch in v2. --- drivers/power/supply/power_supply_core.c | 137 +++++++++++++++++++++++++++++- include/linux/power_supply.h | 19 +++++ 2 files changed, 155 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 307e0995..93007cb 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -570,7 +570,7 @@ int power_supply_get_battery_info(struct power_supply *psy, { struct device_node *battery_np; const char *value; - int err; + int err, len, index; info->energy_full_design_uwh = -EINVAL; info->charge_full_design_uah = -EINVAL; @@ -581,6 +581,12 @@ int power_supply_get_battery_info(struct power_supply *psy, info->constant_charge_voltage_max_uv = -EINVAL; info->factory_internal_resistance_uohm = -EINVAL; + for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) { + info->ocv_table[index] = NULL; + info->ocv_temp[index] = -EINVAL; + info->ocv_table_size[index] = -EINVAL; + } + if (!psy->of_node) { dev_warn(&psy->dev, "%s currently only supports devicetree\n", __func__); @@ -620,10 +626,139 @@ int power_supply_get_battery_info(struct power_supply *psy, of_property_read_u32(battery_np, "factory-internal-resistance-micro-ohms", &info->factory_internal_resistance_uohm); + len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius"); + if (len < 0 && len != -EINVAL) { + return len; + } else if (len > POWER_SUPPLY_OCV_TEMP_MAX) { + dev_err(&psy->dev, "Too many temperature values\n"); + return -EINVAL; + } else if (len > 0) { + of_property_read_u32_array(battery_np, "ocv-capacity-celsius", + info->ocv_temp, len); + } + + 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); + list = of_get_property(battery_np, propname, &size); + if (!list || !size) { + dev_err(&psy->dev, "failed to get %s\n", propname); + kfree(propname); + power_supply_put_battery_info(psy, info); + return -EINVAL; + } + + kfree(propname); + tab_len = size / (2 * sizeof(__be32)); + info->ocv_table_size[index] = tab_len; + + table = info->ocv_table[index] = + devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL); + if (!info->ocv_table[index]) { + power_supply_put_battery_info(psy, info); + return -ENOMEM; + } + + for (i = 0; i < tab_len; i++) { + table[i].ocv = be32_to_cpu(*list++); + table[i].capacity = be32_to_cpu(*list++); + } + } + return 0; } EXPORT_SYMBOL_GPL(power_supply_get_battery_info); +void power_supply_put_battery_info(struct power_supply *psy, + struct power_supply_battery_info *info) +{ + int i; + + for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) { + if (info->ocv_table[i]) + devm_kfree(&psy->dev, info->ocv_table[i]); + } +} +EXPORT_SYMBOL_GPL(power_supply_put_battery_info); + +/** + * power_supply_ocv2cap_simple() - find the battery capacity + * @table: Pointer to battery OCV lookup table + * @table_len: OCV table length + * @ocv: Current OCV value + * + * This helper function is used to look up battery capacity according to + * current OCV value from one OCV table, and the OCV table must be ordered + * descending. + * + * Return: the battery capacity. + */ +int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table, + int table_len, int ocv) +{ + int i, cap, tmp; + + for (i = 0; i < table_len; i++) + if (ocv > table[i].ocv) + break; + + if (i > 0 && i < table_len) { + tmp = (table[i - 1].capacity - table[i].capacity) * + (ocv - table[i].ocv); + tmp /= table[i - 1].ocv - table[i].ocv; + cap = tmp + table[i].capacity; + } else if (i == 0) { + cap = table[0].capacity; + } else { + cap = table[table_len - 1].capacity; + } + + return cap; +} +EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple); + +struct power_supply_battery_ocv_table * +power_supply_find_ocv2cap_table(struct power_supply_battery_info *info, + int temp, int *table_len) +{ + int best_temp_diff = INT_MAX, temp_diff; + u8 i, best_index = 0; + + if (!info->ocv_table[0]) + return NULL; + + for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) { + temp_diff = abs(info->ocv_temp[i] - temp); + + if (temp_diff < best_temp_diff) { + best_temp_diff = temp_diff; + best_index = i; + } + } + + *table_len = info->ocv_table_size[best_index]; + return info->ocv_table[best_index]; +} +EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table); + +int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info, + int ocv, int temp) +{ + struct power_supply_battery_ocv_table *table; + int table_len; + + table = power_supply_find_ocv2cap_table(info, temp, &table_len); + if (!table) + return -EINVAL; + + return power_supply_ocv2cap_simple(table, table_len, ocv); +} +EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap); + int power_supply_get_property(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *val) diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index d089566..84fe93f 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -309,6 +309,13 @@ struct power_supply_info { int use_for_apm; }; +struct power_supply_battery_ocv_table { + int ocv; /* microVolts */ + int capacity; /* percent */ +}; + +#define POWER_SUPPLY_OCV_TEMP_MAX 20 + /* * This is the recommended struct to manage static battery parameters, * populated by power_supply_get_battery_info(). Most platform drivers should @@ -327,6 +334,9 @@ struct power_supply_battery_info { int constant_charge_current_max_ua; /* microAmps */ int constant_charge_voltage_max_uv; /* microVolts */ int factory_internal_resistance_uohm; /* microOhms */ + 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]; }; extern struct atomic_notifier_head power_supply_notifier; @@ -350,6 +360,15 @@ extern struct power_supply *devm_power_supply_get_by_phandle( extern int power_supply_get_battery_info(struct power_supply *psy, struct power_supply_battery_info *info); +extern void power_supply_put_battery_info(struct power_supply *psy, + struct power_supply_battery_info *info); +extern int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table, + int table_len, int ocv); +extern struct power_supply_battery_ocv_table * +power_supply_find_ocv2cap_table(struct power_supply_battery_info *info, + int temp, int *table_len); +extern int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info, + int ocv, 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(