From patchwork Wed Nov 14 09:07:08 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: 151057 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp5441768ljp; Wed, 14 Nov 2018 01:08:03 -0800 (PST) X-Google-Smtp-Source: AJdET5c+ZO++/4PkBtXeh4fkn4xkyW5AZ5GGQFQfqIMKF1YAlLu1iz/l8R/TrYvMtZ950/HqzSm7 X-Received: by 2002:a17:902:a81:: with SMTP id 1-v6mr1128476plp.20.1542186483671; Wed, 14 Nov 2018 01:08:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542186483; cv=none; d=google.com; s=arc-20160816; b=qzjS3wYHr6HtyDBC43bu0uixvJRnQXixgkjOhk4UB5UPiUn066Mb3ayHQ7vb94xTLK tlVE7nBTupxZK/1uWm3IPnD8BqUCuyysbX3dF0WFCVm8Na9cUiVlplOvpj0hy99rklsj NSYiDCpRZXYelSL37vP3TxCXwMBROMqMWSKb2whAFv6lyYfsAHP1/xM/Wpf7EqW//P1h oDdIu2WzaWcZbMuJooAsqroTlU3a9yAzTJ0wTClH2BrzynjLIx7Pa9aSDnN6H6GXR2OR 8HIT+0Utl/u+vWCpWMrzHAXcAevMDLaUUBSyaHMTyIOSIw747pWSgRwA227Q9zNiimHw IEbw== 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=IRUXU/GFwnJl002dQVlSIACjSnNwZF6k9T9koevdIfU=; b=JegjQGLdfNm5qF3a8Y+nTcwfi0RiZQT42rk7Ikmew3/FsJ6HJYhneN2v8kqyKiffjW wlCD3/h3Kn3XNWjYz6ylm3/jdteF6BVYP4jYicbbwkMdCmzsLprxw+7wDczbacHbQfgc eDVuBuhuNfCuFGkwvDtU9n7jU2nQxIl8TxNUhBlDlPSXYsyY8ezVOHM0P323u6CC7Uos k6lHE5vdgdp7V8qpJk/tszb4HtdNGIhmqilJvamlfINzpR48x5XV6B7MGJgatTXPYmG5 Io2ojgQnQ+sAY9PRRynUt5OLnfqQA8pyjTte4JLEn9/I+RXN5LTpZ6gj/1rFIJbrFO5H m2+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MgP2KIyE; 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 133-v6si23684172pfb.41.2018.11.14.01.08.03; Wed, 14 Nov 2018 01:08:03 -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=MgP2KIyE; 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 S1732554AbeKNTKX (ORCPT + 32 others); Wed, 14 Nov 2018 14:10:23 -0500 Received: from mail-pg1-f196.google.com ([209.85.215.196]:35575 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727833AbeKNTKW (ORCPT ); Wed, 14 Nov 2018 14:10:22 -0500 Received: by mail-pg1-f196.google.com with SMTP id 32-v6so7081429pgu.2 for ; Wed, 14 Nov 2018 01:08:00 -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=IRUXU/GFwnJl002dQVlSIACjSnNwZF6k9T9koevdIfU=; b=MgP2KIyE/Z5xkWXYph0HjfOigzpkQYVms1EkRMG4ehWPeNBeBF56HsZYWpv/crCUwL vj9Nr1LnZKKUpmBLIYImVHwp0VweT4krV8Jaj8tvmvEcR9zAZ/ZbNHiS4fAZdf90X8tG D1MLf6kMvinSd5hIqHSHPvxJDQ0e4s6RFrWB0= 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=IRUXU/GFwnJl002dQVlSIACjSnNwZF6k9T9koevdIfU=; b=XsQUZhMCedu3AkQX5ETOR7mbTV7yX3V2y4YOYeHmfOC7VaxkuNIK1eEDnSHYij2m4B jZVl/GvL/IrkGBNVdp7A57h14CvgVOxHTNuqc3M6VgYbf7Kion3WrejYFR83VBltO2cV XVmQP3hhBdnQlVArrsVZNZlyttNiLObOgz8t4PEIlTKCSBU9K2mIxRuibuzffCEiKYht eyv4ZCWfm2mtL6RIHGt4c7pY3XKJh3QlGVpNbMcZRpSjtXjv9DovNFfYTLmcWaZuKpk0 5sGY0OntN0UyXCeucqofG4APDCEg31yFN5vXwJD5+fbqcyagENP8C7Ezx+nnrn5WvCMK J/WQ== X-Gm-Message-State: AGRZ1gIFV4sOJfUv+cCojKe35G+2exi0G5DXE4YPd+mzS9blvd78YeWa E7dJPL4OH+AXXSL2px6owTllKw== X-Received: by 2002:a63:f710:: with SMTP id x16mr1005124pgh.322.1542186480492; Wed, 14 Nov 2018 01:08:00 -0800 (PST) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.102]) by smtp.gmail.com with ESMTPSA id 127-v6sm25048814pfx.91.2018.11.14.01.07.57 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 14 Nov 2018 01:07:59 -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 Subject: [PATCH 5/5] power: supply: sc27xx: Save last battery capacity Date: Wed, 14 Nov 2018 17:07:08 +0800 Message-Id: X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yuanjiang Yu Our charger manager can optimize the battery capacity periodically, so we can save last battery capacity into registers. Then next system power-on, we can read the last saved battery capacity as the initial battery capacity, which can make the battery capacity more accurate. Signed-off-by: Yuanjiang Yu Signed-off-by: Baolin Wang --- drivers/power/supply/sc27xx_fuel_gauge.c | 143 +++++++++++++++++++++++++++++- 1 file changed, 142 insertions(+), 1 deletion(-) -- 1.7.9.5 diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c index 8c52e29..181a8f7 100644 --- a/drivers/power/supply/sc27xx_fuel_gauge.c +++ b/drivers/power/supply/sc27xx_fuel_gauge.c @@ -39,6 +39,9 @@ #define SC27XX_FGU_CLBCNT_VALH 0x68 #define SC27XX_FGU_CLBCNT_VALL 0x6c #define SC27XX_FGU_CLBCNT_QMAXL 0x74 +#define SC27XX_FGU_USER_AREA_SET 0xa0 +#define SC27XX_FGU_USER_AREA_CLEAR 0xa4 +#define SC27XX_FGU_USER_AREA_STATUS 0xa8 #define SC27XX_WRITE_SELCLB_EN BIT(0) #define SC27XX_FGU_CLBCNT_MASK GENMASK(15, 0) @@ -49,6 +52,14 @@ #define SC27XX_FGU_LOW_OVERLOAD_INT BIT(0) #define SC27XX_FGU_CLBCNT_DELTA_INT BIT(2) +#define SC27XX_FGU_MODE_AREA_MASK GENMASK(15, 12) +#define SC27XX_FGU_CAP_AREA_MASK GENMASK(11, 0) +#define SC27XX_FGU_MODE_AREA_SHIFT 12 + +#define SC27XX_FGU_FIRST_POWERTON GENMASK(3, 0) +#define SC27XX_FGU_DEFAULT_CAP GENMASK(11, 0) +#define SC27XX_FGU_NORMAIL_POWERTON 0x5 + #define SC27XX_FGU_CUR_BASIC_ADC 8192 #define SC27XX_FGU_SAMPLE_HZ 2 @@ -119,6 +130,80 @@ static int sc27xx_fgu_voltage_to_adc(struct sc27xx_fgu_data *data, int vol) return DIV_ROUND_CLOSEST(vol * data->vol_1000mv_adc, 1000); } +static bool sc27xx_fgu_is_first_poweron(struct sc27xx_fgu_data *data) +{ + int ret, status, cap, mode; + + ret = regmap_read(data->regmap, + data->base + SC27XX_FGU_USER_AREA_STATUS, &status); + if (ret) + return false; + + /* + * We use low 4 bits to save the last battery capacity and high 12 bits + * to save the system boot mode. + */ + mode = (status & SC27XX_FGU_MODE_AREA_MASK) >> SC27XX_FGU_MODE_AREA_SHIFT; + cap = status & SC27XX_FGU_CAP_AREA_MASK; + + /* + * When FGU has been powered down, the user area registers became + * default value (0xffff), which can be used to valid if the system is + * first power on or not. + */ + if (mode == SC27XX_FGU_FIRST_POWERTON || cap == SC27XX_FGU_DEFAULT_CAP) + return true; + + return false; +} + +static int sc27xx_fgu_save_boot_mode(struct sc27xx_fgu_data *data, + int boot_mode) +{ + int ret; + + ret = regmap_update_bits(data->regmap, + data->base + SC27XX_FGU_USER_AREA_CLEAR, + SC27XX_FGU_MODE_AREA_MASK, + SC27XX_FGU_MODE_AREA_MASK); + if (ret) + return ret; + + return regmap_update_bits(data->regmap, + data->base + SC27XX_FGU_USER_AREA_SET, + SC27XX_FGU_MODE_AREA_MASK, + boot_mode << SC27XX_FGU_MODE_AREA_SHIFT); +} + +static int sc27xx_fgu_save_last_cap(struct sc27xx_fgu_data *data, int cap) +{ + int ret; + + ret = regmap_update_bits(data->regmap, + data->base + SC27XX_FGU_USER_AREA_CLEAR, + SC27XX_FGU_CAP_AREA_MASK, + SC27XX_FGU_CAP_AREA_MASK); + if (ret) + return ret; + + return regmap_update_bits(data->regmap, + data->base + SC27XX_FGU_USER_AREA_SET, + SC27XX_FGU_CAP_AREA_MASK, cap); +} + +static int sc27xx_fgu_read_last_cap(struct sc27xx_fgu_data *data, int *cap) +{ + int ret, value; + + ret = regmap_read(data->regmap, + data->base + SC27XX_FGU_USER_AREA_STATUS, &value); + if (ret) + return ret; + + *cap = value & SC27XX_FGU_CAP_AREA_MASK; + return 0; +} + /* * When system boots on, we can not read battery capacity from coulomb * registers, since now the coulomb registers are invalid. So we should @@ -128,6 +213,20 @@ static int sc27xx_fgu_voltage_to_adc(struct sc27xx_fgu_data *data, int vol) static int sc27xx_fgu_get_boot_capacity(struct sc27xx_fgu_data *data, int *cap) { int volt, cur, oci, ocv, ret; + bool is_first_poweron = sc27xx_fgu_is_first_poweron(data); + + /* + * If system is not the first power on, we should use the last saved + * battery capacity as the initial battery capacity. Otherwise we should + * re-calculate the initial battery capacity. + */ + if (!is_first_poweron) { + ret = sc27xx_fgu_read_last_cap(data, cap); + if (ret) + return ret; + + return sc27xx_fgu_save_boot_mode(data, SC27XX_FGU_NORMAIL_POWERTON); + } /* * After system booting on, the SC27XX_FGU_CLBCNT_QMAXL register saved @@ -160,7 +259,11 @@ static int sc27xx_fgu_get_boot_capacity(struct sc27xx_fgu_data *data, int *cap) *cap = power_supply_ocv2cap_simple(data->cap_table, data->table_len, ocv); - return 0; + ret = sc27xx_fgu_save_last_cap(data, *cap); + if (ret) + return ret; + + return sc27xx_fgu_save_boot_mode(data, SC27XX_FGU_NORMAIL_POWERTON); } static int sc27xx_fgu_set_clbcnt(struct sc27xx_fgu_data *data, int clbcnt) @@ -418,6 +521,30 @@ static int sc27xx_fgu_get_property(struct power_supply *psy, return ret; } +static int sc27xx_fgu_set_property(struct power_supply *psy, + enum power_supply_property psp, + const union power_supply_propval *val) +{ + struct sc27xx_fgu_data *data = power_supply_get_drvdata(psy); + int ret; + + mutex_lock(&data->lock); + + switch (psp) { + case POWER_SUPPLY_PROP_CAPACITY: + ret = sc27xx_fgu_save_last_cap(data, val->intval); + if (ret < 0) + dev_err(data->dev, "failed to save battery capacity\n"); + break; + + default: + ret = -EINVAL; + } + + 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); @@ -425,6 +552,18 @@ static void sc27xx_fgu_external_power_changed(struct power_supply *psy) power_supply_changed(data->battery); } +static int sc27xx_fgu_property_is_writeable(struct power_supply *psy, + enum power_supply_property psp) +{ + switch (psp) { + case POWER_SUPPLY_PROP_CAPACITY: + return 1; + + default: + return 0; + } +} + static enum power_supply_property sc27xx_fgu_props[] = { POWER_SUPPLY_PROP_STATUS, POWER_SUPPLY_PROP_HEALTH, @@ -444,7 +583,9 @@ static void sc27xx_fgu_external_power_changed(struct power_supply *psy) .properties = sc27xx_fgu_props, .num_properties = ARRAY_SIZE(sc27xx_fgu_props), .get_property = sc27xx_fgu_get_property, + .set_property = sc27xx_fgu_set_property, .external_power_changed = sc27xx_fgu_external_power_changed, + .property_is_writeable = sc27xx_fgu_property_is_writeable, }; static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data *data, int cap)