From patchwork Mon Nov 5 07:39:07 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: 150144 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2286264ljp; Sun, 4 Nov 2018 23:39:36 -0800 (PST) X-Google-Smtp-Source: AJdET5dC5k+bpwfIhdOuCRKYMdTpeKodsgXZfzIW+orcZgtk39TWEyC+4a9GBZreLau3HyNh0Qqc X-Received: by 2002:a17:902:584:: with SMTP id f4-v6mr20919227plf.132.1541403576546; Sun, 04 Nov 2018 23:39:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541403576; cv=none; d=google.com; s=arc-20160816; b=BIDx1ZrXRzTq8+Tu/M1I8KwHZ4lvghfRvPCHTpXK608c1tntCS7gpcmQ2ruhx2aK2i FGaxC2SnA/ZGgbs6pV85as8ADsXrSp45+UbYpQp5XkiaAN6ZtAx2UmkZquCiYJ9Yz7sH DEZ2DNWVhcchaQj+pGE34ZgkUbIz5/gbWWg1eMLJxQT0KEuxfMLP1tdgVEoZBi8MGS/8 JDKqG9fBkTs52KN2gf4N+2AdkbubPlIPc4Enkj3vz4CvZgTbeLDzet75JxAtqJX6vHuf xPwz2GPjjr8xa54Oo0uV8JS6IYO2lC9GcoeQpBV+zVjo7utSTDwT3Ty1yIpNwUNjl6xx KY9w== 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=j2tmUgh6tRu6QqzVSQLrFCxkt/YAU9SuinNlw7cTUGE=; b=hhzKXxn+MO/c4Ef5Mp2A70fr2rLJVy1PjKFjvxYL36oSgSnQlVPO6PI/p1hHfzaWpp RkUqLqSADRGz0DEbDNAva4lz7rzXktNHPT2REfxVisM4dBFTfD4WP7EV7pPzrcY3fUdd +XGY0KIA5e5RCG9TK2c44wW3RE+wUMIlN03mnFwnhNtURPfqPv7r1KYIasHs1HIzDIWh XkIk+EinZKjz7oz7SZra5rsCPLmtVfZBWuHAxJztuvxIyvYZ6SHskf6LOnbE8KVN/csP rB2UJJePx9DN1qv24KxB3k04T0hlX79UBGrK5LczAtIi2zJQfz4+Dg7/0o2tSh0EChL3 Ltrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=UhZiNlry; 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 o31si11791561pgb.273.2018.11.04.23.39.36; Sun, 04 Nov 2018 23:39:36 -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=UhZiNlry; 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 S1729676AbeKEQ5y (ORCPT + 32 others); Mon, 5 Nov 2018 11:57:54 -0500 Received: from mail-pl1-f196.google.com ([209.85.214.196]:35703 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729496AbeKEQ5y (ORCPT ); Mon, 5 Nov 2018 11:57:54 -0500 Received: by mail-pl1-f196.google.com with SMTP id n4-v6so4035452plp.2 for ; Sun, 04 Nov 2018 23:39:34 -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=j2tmUgh6tRu6QqzVSQLrFCxkt/YAU9SuinNlw7cTUGE=; b=UhZiNlry/KXodTpr9qhU/8IkmbDzfvjCqlQWKTrGVJvs7F5XvczSTRFtdQ/ujwo8LS VaBB7GJ1eRxdJ57vsgCM7FXu/O3AgyvspchwxTXvBdDEZvq9oTIBfV7BHoYaRBkxX02a TEVC5b7IHUf52qwp96YzKDKpukJdQLWBUS3hM= 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=j2tmUgh6tRu6QqzVSQLrFCxkt/YAU9SuinNlw7cTUGE=; b=K8dRW43cE7mprA6S6IWMKJchp+EnYX7FrY59GU4n5p2hH0Syd3GPSPIgVAT45XJOko SJYfnv+LDRETHM18IcvX8o14Zh2YlQnp+TR60xVmF8ELwY19JV7bK8YT8flHMFWMZERr kySAA1wP+2+AG4+QxAC9gnbG72eFcuOWbwDUIWN8J2/H3oxsGYZzGcGEyWhY5u7Nix2Y QvcbVD81j/Csb/623r39VSJ/yMQEdggtLW9k8Y9S1EDGnaQOJDFGP5dLECV/DxEn6yCr 8TFxyQGY8zy1QFUMj+6nmn04GW5r4ynPvBaNTXx+gO0Rc3y5MMqXipGayt0/M9BWcvAW ouIA== X-Gm-Message-State: AGRZ1gIlYuNFPEtGLWC7pDJl4rrdl4zoURRDeg43A1Jmf+Tg2xkAw4YJ U0cNcN6QnIk2yeuC9fvS6fkA1w== X-Received: by 2002:a17:902:32a4:: with SMTP id z33-v6mr21057060plb.85.1541403574125; Sun, 04 Nov 2018 23:39:34 -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.30 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 04 Nov 2018 23:39:33 -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 2/6] power: supply: core: Add one field to present the battery internal resistance Date: Mon, 5 Nov 2018 15:39:07 +0800 Message-Id: <5253c12da08295914dda82d47a130916d0ed0407.1541393063.git.baolin.wang@linaro.org> 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 Add one field for 'struct power_supply_battery_info' to present the battery factory internal resistance. Signed-off-by: Baolin Wang Reviewed-by: Linus Walleij --- Changes from v6: - None. Changes from v5: - None. Changes from v4: - None. Changes from v3: - New patch in v3, which splits code into one separate patch. - Add Linusw reviewed tag. --- drivers/power/supply/power_supply_core.c | 3 +++ include/linux/power_supply.h | 1 + 2 files changed, 4 insertions(+) -- 1.7.9.5 diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c index e853618..307e0995 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -579,6 +579,7 @@ int power_supply_get_battery_info(struct power_supply *psy, info->charge_term_current_ua = -EINVAL; info->constant_charge_current_max_ua = -EINVAL; info->constant_charge_voltage_max_uv = -EINVAL; + info->factory_internal_resistance_uohm = -EINVAL; if (!psy->of_node) { dev_warn(&psy->dev, "%s currently only supports devicetree\n", @@ -616,6 +617,8 @@ int power_supply_get_battery_info(struct power_supply *psy, &info->constant_charge_current_max_ua); of_property_read_u32(battery_np, "constant_charge_voltage_max_microvolt", &info->constant_charge_voltage_max_uv); + of_property_read_u32(battery_np, "factory-internal-resistance-micro-ohms", + &info->factory_internal_resistance_uohm); return 0; } diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index f807691..d089566 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -326,6 +326,7 @@ struct power_supply_battery_info { int charge_term_current_ua; /* microAmps */ int constant_charge_current_max_ua; /* microAmps */ int constant_charge_voltage_max_uv; /* microVolts */ + int factory_internal_resistance_uohm; /* microOhms */ }; extern struct atomic_notifier_head power_supply_notifier; 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( From patchwork Mon Nov 5 07:39:11 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: 150148 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2286449ljp; Sun, 4 Nov 2018 23:39:53 -0800 (PST) X-Google-Smtp-Source: AJdET5dV4rmF5W8fh1/luQWWw0eMVP5lsAqOvd2iv9KrRf0IG09u2P3k8jDXcQK7ljS5Xt9SchFs X-Received: by 2002:a63:f515:: with SMTP id w21mr177781pgh.220.1541403593633; Sun, 04 Nov 2018 23:39:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541403593; cv=none; d=google.com; s=arc-20160816; b=iaLQiudtB8oUuFHnld1qi2aCQG/37Ko06G8XneCD6x3ihYouJ3xSwWjVShAUc+pF+8 asVI2nUd4BUPxogMfy19BROG4kNhURd90mcrhCNUUnkiCZNZPSxF6jfuq6RXUWiBN43W RXMZ52xzkAwHVnOpgh8HL30s80jnzc/RsKpK5vFIddBJwcUZtrldsJpuAvT0sYaW0zRZ q0urPg1ij9psxnI3AaBioTo6ZGa9P4+GrzOrg0UyGSkKh/l0xe1RItUmedTqKrA4caJ/ XAkIdZkCZQ+xGH9dmt2jsgyMqVymTTg3lndhz9iezZHiDD6D5bWCktbZzv+iYhhOh8kD BBeg== 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=Ig5rQfT9WOa4mOV+iNotyT/d4GB2W/ZB3O1oQVmn5+Y=; b=JsozYVIPU24O13TbJmJwUd/PWrWxNYqq/UXGzKLP2P89qmOHDLxOGQAaeCN+8/pnC6 gH0zmoMpNEFgH2FJ+5J33GYJ8Vx9KeKN4XlKHi1C1n7H1bgJ8rtRKWNEY18O14UmHRHD MTD/ChBmzBEsVIzRE96LNwNSu216OsA/c1RQdJ4n9C8EGw7pDrDsgZoLeKXfbwiigfK6 SRLFJEDdhwmpX9IXNep9abkw6BM7WYyAMTxttqSrc3UQ5efW4VnSnphmNx7CDANSqdul w18vWw/Q04euDTNM1Pbs+5eSqx9Y2OZAt4JG9GfTNH1f/zok4UtO7hOV6dWx7S1jNVr1 t41w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MH+lxk7L; 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 3-v6si11831839plm.136.2018.11.04.23.39.53; Sun, 04 Nov 2018 23:39:53 -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=MH+lxk7L; 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 S1729771AbeKEQ6M (ORCPT + 32 others); Mon, 5 Nov 2018 11:58:12 -0500 Received: from mail-pf1-f195.google.com ([209.85.210.195]:38253 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729299AbeKEQ6L (ORCPT ); Mon, 5 Nov 2018 11:58:11 -0500 Received: by mail-pf1-f195.google.com with SMTP id b11-v6so4003161pfi.5 for ; Sun, 04 Nov 2018 23:39:51 -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=Ig5rQfT9WOa4mOV+iNotyT/d4GB2W/ZB3O1oQVmn5+Y=; b=MH+lxk7LZ8D4mr/uunV1SseSgKiWaArbssJajVmeq4MovrYZ+cSamdaIlbQz/aj/j5 7W2y4kaCviwEDAtQQSJy2sVqmxei7NIxfdXqHqUflaB+1GKOpUebg2VgQ4cqF3l/utEn KVMRgb75KuD1b9G8TtY/EUYjTrfJyU+vEHX9A= 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=Ig5rQfT9WOa4mOV+iNotyT/d4GB2W/ZB3O1oQVmn5+Y=; b=rl8A+iftiq0Lb9PJC3N7BYQxZVR2/rAPlBrfdZVJ9tg6YDrFrXKU03KAfXZXdaQ8jG on/zPgVcHvFIZF2nFKZgDiMA5Zzj2wPHs3Gr61mp9MtEWnfzxFmooWhSpxltuwAA5Jlp vfSMjuuX8sJzx6MHfY8I2iikUB8K2Ubkc/30c1OYCNF35FmSpiTl70MmkRRwxxfjtaAQ hq2r4NwmbafKl2jooNkIUhy/sW8JX/gdLH2HRaHTIgluN84+KKyLXZAQAY8Lb/+cMQfR zI6qimCcEPa2IOEcci+zyBeNIxkwOeGuw8E1LRaJKL96iLihSIHnGG7+cG2vkpEXUIHG 6FNQ== X-Gm-Message-State: AGRZ1gKgyJQ9BjFRY2mhWPdCzWdtXL7PU5tWb0pRw8sq8WmPtO+YAr+T /iHSP95wxuO73MLePk91pxjAvg== X-Received: by 2002:a63:e156:: with SMTP id h22mr19271666pgk.255.1541403590763; Sun, 04 Nov 2018 23:39:50 -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.46 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 04 Nov 2018 23:39:49 -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 6/6] power: supply: Add Spreadtrum SC27XX fuel gauge unit driver Date: Mon, 5 Nov 2018 15:39:11 +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 This patch adds the Spreadtrum SC27XX serial PMICs fuel gauge support, which is used to calculate the battery capacity. Original-by: Yuanjiang Yu Signed-off-by: Baolin Wang Acked-by: Linus Walleij --- Changes from v6: - Use DIV_ROUND_CLOSEST macro to simplify code. Changes from v5: - Save the OCV values in micro volts for OCV capacity table. - Use devm_kmemdup() instead of devm_kzalloc() in sc27xx_fgu_hw_init() Changes from v4: - None. Changes from v3: - None. Changes from v2: - Use core helper functions to look up OCV capacity table. - Use device_property_read_u32() instead of of_property_read_u32(). - Add acked tag from Linus. Changes from v1: - Use battery standard properties to get internal resistance and ocv table. - Change devm_gpiod_get_optional() to devm_gpiod_get(). - Add power_supply_changed() when detecting battery present change. - Return micro volts for sc27xx_fgu_get_vbat_ocv(). --- drivers/power/supply/Kconfig | 7 + drivers/power/supply/Makefile | 1 + drivers/power/supply/sc27xx_fuel_gauge.c | 656 ++++++++++++++++++++++++++++++ 3 files changed, 664 insertions(+) create mode 100644 drivers/power/supply/sc27xx_fuel_gauge.c -- 1.7.9.5 diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig index f27cf07..917f4b7 100644 --- a/drivers/power/supply/Kconfig +++ b/drivers/power/supply/Kconfig @@ -652,4 +652,11 @@ config CHARGER_SC2731 Say Y here to enable support for battery charging with SC2731 PMIC chips. +config FUEL_GAUGE_SC27XX + tristate "Spreadtrum SC27XX fuel gauge driver" + depends on MFD_SC27XX_PMIC || COMPILE_TEST + help + Say Y here to enable support for fuel gauge with SC27XX + PMIC chips. + endif # POWER_SUPPLY diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile index 767105b..b731c2a 100644 --- a/drivers/power/supply/Makefile +++ b/drivers/power/supply/Makefile @@ -86,3 +86,4 @@ obj-$(CONFIG_AXP288_FUEL_GAUGE) += axp288_fuel_gauge.o obj-$(CONFIG_AXP288_CHARGER) += axp288_charger.o obj-$(CONFIG_CHARGER_CROS_USBPD) += cros_usbpd-charger.o obj-$(CONFIG_CHARGER_SC2731) += sc2731_charger.o +obj-$(CONFIG_FUEL_GAUGE_SC27XX) += sc27xx_fuel_gauge.o diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c new file mode 100644 index 0000000..8613584 --- /dev/null +++ b/drivers/power/supply/sc27xx_fuel_gauge.c @@ -0,0 +1,656 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2018 Spreadtrum Communications Inc. + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* PMIC global control registers definition */ +#define SC27XX_MODULE_EN0 0xc08 +#define SC27XX_CLK_EN0 0xc18 +#define SC27XX_FGU_EN BIT(7) +#define SC27XX_FGU_RTC_EN BIT(6) + +/* FGU registers definition */ +#define SC27XX_FGU_START 0x0 +#define SC27XX_FGU_CONFIG 0x4 +#define SC27XX_FGU_ADC_CONFIG 0x8 +#define SC27XX_FGU_STATUS 0xc +#define SC27XX_FGU_INT_EN 0x10 +#define SC27XX_FGU_INT_CLR 0x14 +#define SC27XX_FGU_INT_STS 0x1c +#define SC27XX_FGU_VOLTAGE 0x20 +#define SC27XX_FGU_OCV 0x24 +#define SC27XX_FGU_POCV 0x28 +#define SC27XX_FGU_CURRENT 0x2c +#define SC27XX_FGU_CLBCNT_SETH 0x50 +#define SC27XX_FGU_CLBCNT_SETL 0x54 +#define SC27XX_FGU_CLBCNT_VALH 0x68 +#define SC27XX_FGU_CLBCNT_VALL 0x6c +#define SC27XX_FGU_CLBCNT_QMAXL 0x74 + +#define SC27XX_WRITE_SELCLB_EN BIT(0) +#define SC27XX_FGU_CLBCNT_MASK GENMASK(15, 0) +#define SC27XX_FGU_CLBCNT_SHIFT 16 + +#define SC27XX_FGU_1000MV_ADC 686 +#define SC27XX_FGU_1000MA_ADC 1372 +#define SC27XX_FGU_CUR_BASIC_ADC 8192 +#define SC27XX_FGU_SAMPLE_HZ 2 + +/* + * struct sc27xx_fgu_data: describe the FGU device + * @regmap: regmap for register access + * @dev: platform device + * @battery: battery power supply + * @base: the base offset for the controller + * @lock: protect the structure + * @gpiod: GPIO for battery detection + * @channel: IIO channel to get battery temperature + * @internal_resist: the battery internal resistance in mOhm + * @total_cap: the total capacity of the battery in mAh + * @init_cap: the initial capacity of the battery in mAh + * @init_clbcnt: the initial coulomb counter + * @max_volt: the maximum constant input voltage in millivolt + * @table_len: the capacity table length + * @cap_table: capacity table with corresponding ocv + */ +struct sc27xx_fgu_data { + struct regmap *regmap; + struct device *dev; + struct power_supply *battery; + u32 base; + struct mutex lock; + struct gpio_desc *gpiod; + struct iio_channel *channel; + bool bat_present; + int internal_resist; + int total_cap; + int init_cap; + int init_clbcnt; + int max_volt; + int table_len; + struct power_supply_battery_ocv_table *cap_table; +}; + +static const char * const sc27xx_charger_supply_name[] = { + "sc2731_charger", + "sc2720_charger", + "sc2721_charger", + "sc2723_charger", +}; + +static int sc27xx_fgu_adc_to_current(int adc) +{ + return DIV_ROUND_CLOSEST(adc * 1000, SC27XX_FGU_1000MA_ADC); +} + +static int sc27xx_fgu_adc_to_voltage(int adc) +{ + return DIV_ROUND_CLOSEST(adc * 1000, SC27XX_FGU_1000MV_ADC); +} + +/* + * When system boots on, we can not read battery capacity from coulomb + * registers, since now the coulomb registers are invalid. So we should + * calculate the battery open circuit voltage, and get current battery + * capacity according to the capacity table. + */ +static int sc27xx_fgu_get_boot_capacity(struct sc27xx_fgu_data *data, int *cap) +{ + int volt, cur, oci, ocv, ret; + + /* + * After system booting on, the SC27XX_FGU_CLBCNT_QMAXL register saved + * the first sampled open circuit current. + */ + ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CLBCNT_QMAXL, + &cur); + if (ret) + return ret; + + cur <<= 1; + oci = sc27xx_fgu_adc_to_current(cur - SC27XX_FGU_CUR_BASIC_ADC); + + /* + * Should get the OCV from SC27XX_FGU_POCV register at the system + * beginning. It is ADC values reading from registers which need to + * convert the corresponding voltage. + */ + ret = regmap_read(data->regmap, data->base + SC27XX_FGU_POCV, &volt); + if (ret) + return ret; + + volt = sc27xx_fgu_adc_to_voltage(volt); + ocv = volt * 1000 - oci * data->internal_resist; + + /* + * Parse the capacity table to look up the correct capacity percent + * according to current battery's corresponding OCV values. + */ + *cap = power_supply_ocv2cap_simple(data->cap_table, data->table_len, + ocv); + + return 0; +} + +static int sc27xx_fgu_set_clbcnt(struct sc27xx_fgu_data *data, int clbcnt) +{ + int ret; + + clbcnt *= SC27XX_FGU_SAMPLE_HZ; + + ret = regmap_update_bits(data->regmap, + data->base + SC27XX_FGU_CLBCNT_SETL, + SC27XX_FGU_CLBCNT_MASK, clbcnt); + if (ret) + return ret; + + ret = regmap_update_bits(data->regmap, + data->base + SC27XX_FGU_CLBCNT_SETH, + SC27XX_FGU_CLBCNT_MASK, + clbcnt >> SC27XX_FGU_CLBCNT_SHIFT); + if (ret) + return ret; + + return regmap_update_bits(data->regmap, data->base + SC27XX_FGU_START, + SC27XX_WRITE_SELCLB_EN, + SC27XX_WRITE_SELCLB_EN); +} + +static int sc27xx_fgu_get_clbcnt(struct sc27xx_fgu_data *data, int *clb_cnt) +{ + int ccl, cch, ret; + + ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CLBCNT_VALL, + &ccl); + if (ret) + return ret; + + ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CLBCNT_VALH, + &cch); + if (ret) + return ret; + + *clb_cnt = ccl & SC27XX_FGU_CLBCNT_MASK; + *clb_cnt |= (cch & SC27XX_FGU_CLBCNT_MASK) << SC27XX_FGU_CLBCNT_SHIFT; + *clb_cnt /= SC27XX_FGU_SAMPLE_HZ; + + return 0; +} + +static int sc27xx_fgu_get_capacity(struct sc27xx_fgu_data *data, int *cap) +{ + int ret, cur_clbcnt, delta_clbcnt, delta_cap, temp; + + /* Get current coulomb counters firstly */ + ret = sc27xx_fgu_get_clbcnt(data, &cur_clbcnt); + if (ret) + return ret; + + delta_clbcnt = cur_clbcnt - data->init_clbcnt; + + /* + * Convert coulomb counter to delta capacity (mAh), and set multiplier + * as 100 to improve the precision. + */ + temp = DIV_ROUND_CLOSEST(delta_clbcnt, 360); + temp = sc27xx_fgu_adc_to_current(temp); + + /* + * Convert to capacity percent of the battery total capacity, + * and multiplier is 100 too. + */ + delta_cap = DIV_ROUND_CLOSEST(temp * 100, data->total_cap); + *cap = delta_cap + data->init_cap; + + return 0; +} + +static int sc27xx_fgu_get_vbat_vol(struct sc27xx_fgu_data *data, int *val) +{ + int ret, vol; + + ret = regmap_read(data->regmap, data->base + SC27XX_FGU_VOLTAGE, &vol); + if (ret) + return ret; + + /* + * It is ADC values reading from registers which need to convert to + * corresponding voltage values. + */ + *val = sc27xx_fgu_adc_to_voltage(vol); + + return 0; +} + +static int sc27xx_fgu_get_current(struct sc27xx_fgu_data *data, int *val) +{ + int ret, cur; + + ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CURRENT, &cur); + if (ret) + return ret; + + /* + * It is ADC values reading from registers which need to convert to + * corresponding current values. + */ + *val = sc27xx_fgu_adc_to_current(cur - SC27XX_FGU_CUR_BASIC_ADC); + + return 0; +} + +static int sc27xx_fgu_get_vbat_ocv(struct sc27xx_fgu_data *data, int *val) +{ + int vol, cur, ret; + + ret = sc27xx_fgu_get_vbat_vol(data, &vol); + if (ret) + return ret; + + ret = sc27xx_fgu_get_current(data, &cur); + if (ret) + return ret; + + /* Return the battery OCV in micro volts. */ + *val = vol * 1000 - cur * data->internal_resist; + + return 0; +} + +static int sc27xx_fgu_get_temp(struct sc27xx_fgu_data *data, int *temp) +{ + return iio_read_channel_processed(data->channel, temp); +} + +static int sc27xx_fgu_get_health(struct sc27xx_fgu_data *data, int *health) +{ + int ret, vol; + + ret = sc27xx_fgu_get_vbat_vol(data, &vol); + if (ret) + return ret; + + if (vol > data->max_volt) + *health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; + else + *health = POWER_SUPPLY_HEALTH_GOOD; + + return 0; +} + +static int sc27xx_fgu_get_status(struct sc27xx_fgu_data *data, int *status) +{ + union power_supply_propval val; + struct power_supply *psy; + int i, ret = -EINVAL; + + for (i = 0; i < ARRAY_SIZE(sc27xx_charger_supply_name); i++) { + psy = power_supply_get_by_name(sc27xx_charger_supply_name[i]); + if (!psy) + continue; + + ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_STATUS, + &val); + power_supply_put(psy); + if (ret) + return ret; + + *status = val.intval; + } + + return ret; +} + +static int sc27xx_fgu_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct sc27xx_fgu_data *data = power_supply_get_drvdata(psy); + int ret = 0; + int value; + + mutex_lock(&data->lock); + + switch (psp) { + case POWER_SUPPLY_PROP_STATUS: + ret = sc27xx_fgu_get_status(data, &value); + if (ret) + goto error; + + val->intval = value; + break; + + case POWER_SUPPLY_PROP_HEALTH: + ret = sc27xx_fgu_get_health(data, &value); + if (ret) + goto error; + + val->intval = value; + break; + + case POWER_SUPPLY_PROP_PRESENT: + val->intval = data->bat_present; + break; + + case POWER_SUPPLY_PROP_TEMP: + ret = sc27xx_fgu_get_temp(data, &value); + if (ret) + goto error; + + val->intval = value; + break; + + case POWER_SUPPLY_PROP_TECHNOLOGY: + val->intval = POWER_SUPPLY_TECHNOLOGY_LION; + break; + + case POWER_SUPPLY_PROP_CAPACITY: + ret = sc27xx_fgu_get_capacity(data, &value); + if (ret) + goto error; + + val->intval = value; + break; + + case POWER_SUPPLY_PROP_VOLTAGE_NOW: + ret = sc27xx_fgu_get_vbat_vol(data, &value); + if (ret) + goto error; + + val->intval = value * 1000; + break; + + case POWER_SUPPLY_PROP_VOLTAGE_OCV: + ret = sc27xx_fgu_get_vbat_ocv(data, &value); + if (ret) + goto error; + + val->intval = value; + break; + + case POWER_SUPPLY_PROP_CURRENT_NOW: + case POWER_SUPPLY_PROP_CURRENT_AVG: + ret = sc27xx_fgu_get_current(data, &value); + if (ret) + goto error; + + val->intval = value * 1000; + break; + + default: + ret = -EINVAL; + break; + } + +error: + mutex_unlock(&data->lock); + return ret; +} + +static void sc27xx_fgu_external_power_changed(struct power_supply *psy) +{ + struct sc27xx_fgu_data *data = power_supply_get_drvdata(psy); + + power_supply_changed(data->battery); +} + +static enum power_supply_property sc27xx_fgu_props[] = { + POWER_SUPPLY_PROP_STATUS, + POWER_SUPPLY_PROP_HEALTH, + POWER_SUPPLY_PROP_PRESENT, + POWER_SUPPLY_PROP_TEMP, + POWER_SUPPLY_PROP_TECHNOLOGY, + POWER_SUPPLY_PROP_CAPACITY, + POWER_SUPPLY_PROP_VOLTAGE_NOW, + POWER_SUPPLY_PROP_VOLTAGE_OCV, + POWER_SUPPLY_PROP_CURRENT_NOW, + POWER_SUPPLY_PROP_CURRENT_AVG, +}; + +static const struct power_supply_desc sc27xx_fgu_desc = { + .name = "sc27xx-fgu", + .type = POWER_SUPPLY_TYPE_BATTERY, + .properties = sc27xx_fgu_props, + .num_properties = ARRAY_SIZE(sc27xx_fgu_props), + .get_property = sc27xx_fgu_get_property, + .external_power_changed = sc27xx_fgu_external_power_changed, +}; + +static irqreturn_t sc27xx_fgu_bat_detection(int irq, void *dev_id) +{ + struct sc27xx_fgu_data *data = dev_id; + int state; + + mutex_lock(&data->lock); + + state = gpiod_get_value_cansleep(data->gpiod); + if (state < 0) { + dev_err(data->dev, "failed to get gpio state\n"); + mutex_unlock(&data->lock); + return IRQ_RETVAL(state); + } + + data->bat_present = !!state; + + mutex_unlock(&data->lock); + + power_supply_changed(data->battery); + return IRQ_HANDLED; +} + +static void sc27xx_fgu_disable(void *_data) +{ + struct sc27xx_fgu_data *data = _data; + + regmap_update_bits(data->regmap, SC27XX_CLK_EN0, SC27XX_FGU_RTC_EN, 0); + regmap_update_bits(data->regmap, SC27XX_MODULE_EN0, SC27XX_FGU_EN, 0); +} + +static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data *data, int capacity) +{ + /* + * Get current capacity (mAh) = battery total capacity (mAh) * + * current capacity percent (capacity / 100). + */ + int cur_cap = DIV_ROUND_CLOSEST(data->total_cap * capacity, 100); + + /* + * Convert current capacity (mAh) to coulomb counter according to the + * formula: 1 mAh =3.6 coulomb. + */ + return DIV_ROUND_CLOSEST(cur_cap * 36, 10); +} + +static int sc27xx_fgu_hw_init(struct sc27xx_fgu_data *data) +{ + struct power_supply_battery_info info = { }; + struct power_supply_battery_ocv_table *table; + int ret; + + ret = power_supply_get_battery_info(data->battery, &info); + if (ret) { + dev_err(data->dev, "failed to get battery information\n"); + return ret; + } + + data->total_cap = info.charge_full_design_uah / 1000; + data->max_volt = info.constant_charge_voltage_max_uv / 1000; + data->internal_resist = info.factory_internal_resistance_uohm / 1000; + + /* + * For SC27XX fuel gauge device, we only use one ocv-capacity + * table in normal temperature 20 Celsius. + */ + table = power_supply_find_ocv2cap_table(&info, 20, &data->table_len); + if (!table) + return -EINVAL; + + data->cap_table = devm_kmemdup(data->dev, table, + data->table_len * sizeof(*table), + GFP_KERNEL); + if (!data->cap_table) { + power_supply_put_battery_info(data->battery, &info); + return -ENOMEM; + } + + power_supply_put_battery_info(data->battery, &info); + + /* Enable the FGU module */ + ret = regmap_update_bits(data->regmap, SC27XX_MODULE_EN0, + SC27XX_FGU_EN, SC27XX_FGU_EN); + if (ret) { + dev_err(data->dev, "failed to enable fgu\n"); + return ret; + } + + /* Enable the FGU RTC clock to make it work */ + ret = regmap_update_bits(data->regmap, SC27XX_CLK_EN0, + SC27XX_FGU_RTC_EN, SC27XX_FGU_RTC_EN); + if (ret) { + dev_err(data->dev, "failed to enable fgu RTC clock\n"); + goto disable_fgu; + } + + /* + * Get the boot battery capacity when system powers on, which is used to + * initialize the coulomb counter. After that, we can read the coulomb + * counter to measure the battery capacity. + */ + ret = sc27xx_fgu_get_boot_capacity(data, &data->init_cap); + if (ret) { + dev_err(data->dev, "failed to get boot capacity\n"); + goto disable_clk; + } + + /* + * Convert battery capacity to the corresponding initial coulomb counter + * and set into coulomb counter registers. + */ + data->init_clbcnt = sc27xx_fgu_cap_to_clbcnt(data, data->init_cap); + ret = sc27xx_fgu_set_clbcnt(data, data->init_clbcnt); + if (ret) { + dev_err(data->dev, "failed to initialize coulomb counter\n"); + goto disable_clk; + } + + return 0; + +disable_clk: + regmap_update_bits(data->regmap, SC27XX_CLK_EN0, SC27XX_FGU_RTC_EN, 0); +disable_fgu: + regmap_update_bits(data->regmap, SC27XX_MODULE_EN0, SC27XX_FGU_EN, 0); + + return ret; +} + +static int sc27xx_fgu_probe(struct platform_device *pdev) +{ + struct device_node *np = pdev->dev.of_node; + struct power_supply_config fgu_cfg = { }; + struct sc27xx_fgu_data *data; + int ret, irq; + + data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + data->regmap = dev_get_regmap(pdev->dev.parent, NULL); + if (!data->regmap) { + dev_err(&pdev->dev, "failed to get regmap\n"); + return -ENODEV; + } + + ret = device_property_read_u32(&pdev->dev, "reg", &data->base); + if (ret) { + dev_err(&pdev->dev, "failed to get fgu address\n"); + return ret; + } + + data->channel = devm_iio_channel_get(&pdev->dev, "bat-temp"); + if (IS_ERR(data->channel)) { + dev_err(&pdev->dev, "failed to get IIO channel\n"); + return PTR_ERR(data->channel); + } + + data->gpiod = devm_gpiod_get(&pdev->dev, "bat-detect", GPIOD_IN); + if (IS_ERR(data->gpiod)) { + dev_err(&pdev->dev, "failed to get battery detection GPIO\n"); + return PTR_ERR(data->gpiod); + } + + ret = gpiod_get_value_cansleep(data->gpiod); + if (ret < 0) { + dev_err(&pdev->dev, "failed to get gpio state\n"); + return ret; + } + + data->bat_present = !!ret; + mutex_init(&data->lock); + data->dev = &pdev->dev; + + fgu_cfg.drv_data = data; + fgu_cfg.of_node = np; + data->battery = devm_power_supply_register(&pdev->dev, &sc27xx_fgu_desc, + &fgu_cfg); + if (IS_ERR(data->battery)) { + dev_err(&pdev->dev, "failed to register power supply\n"); + return PTR_ERR(data->battery); + } + + ret = sc27xx_fgu_hw_init(data); + if (ret) { + dev_err(&pdev->dev, "failed to initialize fgu hardware\n"); + return ret; + } + + ret = devm_add_action(&pdev->dev, sc27xx_fgu_disable, data); + if (ret) { + sc27xx_fgu_disable(data); + dev_err(&pdev->dev, "failed to add fgu disable action\n"); + return ret; + } + + irq = gpiod_to_irq(data->gpiod); + if (irq < 0) { + dev_err(&pdev->dev, "failed to translate GPIO to IRQ\n"); + return irq; + } + + ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, + sc27xx_fgu_bat_detection, + IRQF_ONESHOT | IRQF_TRIGGER_RISING | + IRQF_TRIGGER_FALLING, + pdev->name, data); + if (ret) { + dev_err(&pdev->dev, "failed to request IRQ\n"); + return ret; + } + + return 0; +} + +static const struct of_device_id sc27xx_fgu_of_match[] = { + { .compatible = "sprd,sc2731-fgu", }, + { } +}; + +static struct platform_driver sc27xx_fgu_driver = { + .probe = sc27xx_fgu_probe, + .driver = { + .name = "sc27xx-fgu", + .of_match_table = sc27xx_fgu_of_match, + } +}; + +module_platform_driver(sc27xx_fgu_driver); + +MODULE_DESCRIPTION("Spreadtrum SC27XX PMICs Fual Gauge Unit Driver"); +MODULE_LICENSE("GPL v2");