From patchwork Thu Dec 6 03:20:39 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: 10715211 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 055CD1759 for ; Thu, 6 Dec 2018 03:21:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E01592DDC2 for ; Thu, 6 Dec 2018 03:21:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D42BB2DDD1; Thu, 6 Dec 2018 03:21:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A513E2DDD0 for ; Thu, 6 Dec 2018 03:21:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728144AbeLFDVE (ORCPT ); Wed, 5 Dec 2018 22:21:04 -0500 Received: from mail-pl1-f194.google.com ([209.85.214.194]:41614 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727940AbeLFDVE (ORCPT ); Wed, 5 Dec 2018 22:21:04 -0500 Received: by mail-pl1-f194.google.com with SMTP id u6so11060801plm.8 for ; Wed, 05 Dec 2018 19:21:04 -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; bh=h2KSnIAlk7pCL7epeA9wqI/dn+NWpdOYjtJjFjbxWQo=; b=XpRbVs3IwSfy9RQTl+eNF/lJWAP/7YeOnmSw84XAvkd2MWrmJKKUlbR6kuGZUj/TvC GAUG4e0bhePhZyUVCCUBVeIUKnB3CkrHs5wx3NDoD+pYAJocx0w/Ro5BcO/LpGPQ6jos OkDyRvRxndUQHno+0XK8xP2TFjEwQpdgF4bC0= 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; bh=h2KSnIAlk7pCL7epeA9wqI/dn+NWpdOYjtJjFjbxWQo=; b=UzP84gqxUqdFAfYB1N4Ge3/998JgHWEYdcK0T7lQ/lF+uxgQrKHwcGT/ZJ0sAY1XuA o+tR2TIdWJCXh8NwxSujPdC2hikKsO1X22hMTknYZSPfF1r8Ua6OFluIJCXOM35cVLyR MpiDxQ/vKq3uEtvENGJuvLpVr1/ohPvMsG8KyIJUiVhi4w0fUZvSHfI5ULF5oLWtDhJ7 lHbcAMjSA2K82N+f7ldWda07uukxmNL52zWVElp5KjCJbl19njR2jJWa9vYsk6qiEY/K A6dWgC0aBbwgGdY1IMVE5HcuT4hyE1GSW0yvYPsJrVUAGIhmAxyJ3J1Q1cg+nGJL2Tma QnBA== X-Gm-Message-State: AA+aEWbV83iaZEqMOceLJjUNi2IeP7toQMYOV0Dm9TkIlOpJN1hmgUhj lLTGKMyZqj5gVDIDeePk8x6WWQ== X-Google-Smtp-Source: AFSGD/UVB7Fkxkz13X7TagQRFwNgXXeVJWh/vRCJmBjdw3++QlShsoJvyUwawUWg7DmOmjHwKW7wbg== X-Received: by 2002:a17:902:5a86:: with SMTP id r6mr25635770pli.301.1544066463875; Wed, 05 Dec 2018 19:21:03 -0800 (PST) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.102]) by smtp.gmail.com with ESMTPSA id o189sm33258819pfg.117.2018.12.05.19.21.00 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 05 Dec 2018 19:21:02 -0800 (PST) From: Baolin Wang To: sre@kernel.org Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, yuanjiang.yu@unisoc.com, baolin.wang@linaro.org, broonie@kernel.org, pavel@ucw.cz Subject: [PATCH v2] power: supply: sc27xx: Save last battery capacity Date: Thu, 6 Dec 2018 11:20:39 +0800 Message-Id: <271ebbae85bc544cbcb6925a10de30267f8eb3ee.1544066225.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- Changes from v1: - Simplify the logics for sc27xx_fgu_set_property() and sc27xx_fgu_property_is_writeable(). --- drivers/power/supply/sc27xx_fuel_gauge.c | 135 +++++++++++++++++++++++++++++- 1 file changed, 134 insertions(+), 1 deletion(-) diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c index 8c52e29..76da189 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,28 @@ 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; + + if (psp != POWER_SUPPLY_PROP_CAPACITY) + return -EINVAL; + + mutex_lock(&data->lock); + + ret = sc27xx_fgu_save_last_cap(data, val->intval); + + mutex_unlock(&data->lock); + + if (ret < 0) + dev_err(data->dev, "failed to save battery capacity\n"); + + 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 +550,12 @@ 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) +{ + return psp == POWER_SUPPLY_PROP_CAPACITY; +} + static enum power_supply_property sc27xx_fgu_props[] = { POWER_SUPPLY_PROP_STATUS, POWER_SUPPLY_PROP_HEALTH, @@ -444,7 +575,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)