From patchwork Wed Jul 31 10:00:23 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: 11067451 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 E51A513AC for ; Wed, 31 Jul 2019 10:01:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CFCFC28511 for ; Wed, 31 Jul 2019 10:01:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C408028815; Wed, 31 Jul 2019 10:01:20 +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 71F2C28511 for ; Wed, 31 Jul 2019 10:01:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387612AbfGaKAy (ORCPT ); Wed, 31 Jul 2019 06:00:54 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:41430 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387642AbfGaKAy (ORCPT ); Wed, 31 Jul 2019 06:00:54 -0400 Received: by mail-pg1-f196.google.com with SMTP id x15so21371332pgg.8 for ; Wed, 31 Jul 2019 03:00:53 -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=MmBOwwNeN+W1dC0nmIPZG6LtIHwgJLZWSo5QrSMSqcg=; b=F2cmNKe82FILmNf5yBHDnTc0fv/W9P8ElxWyX9nD9eNEddHhrjcVcMripReHGh+S0v tSNdYbOTYj5QQBmbdLKklAfAnbsGhNRFF8O/+LB3M71Jf26oCbFHGBxBwHSsBhGdqxyA +KxTVK8klRgp4/pNwu9f2ux4WWomzu+suJyXaCfOMITmKuJ2rNjMgyJNtefNtQk/6Ced +G4XQdEfuvog8fX/p/pcnavzvF0fv3pGjJxnKPAJzsT26oHgPzE1HZaAb08cu8XimFcy Ot89rgQl1jDKVtz6fQ/61d+80sPzURiNrw0u2ctVaFEOZ6aLQg8LP6zliTaQ5zR/CcnX jBig== 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=MmBOwwNeN+W1dC0nmIPZG6LtIHwgJLZWSo5QrSMSqcg=; b=WqKcL5OG6+cWfNmCVd1WitLyuPiSA9w99/ST8kPrdTBvcFaPtKvGutBhEJE0M9BlbI fl7QerxYHXnZtft+xvXeDqFo1pNrAzr6/9vuTyca5V+V2CXSC/iHUzJFmObUL+dGS1W7 LkMvNYSh0xSMcISOPFKWOJJx6sbIun/CbpvDXxEBUQv5a0CuZhV/gk8D1BmXfDfbqacH PNM08RCe4Bur5aZWpFGPtU8D3LfYIzy8wPGhy+DJ3XDa5i2mAQDkLMgYrifEslNnQQ7B 2AJEDAjT8hZK5o07WxIAVs3RqB6+eU36+0IY9Tg7mZt6aH5xRtV5QfzbVsE/tQtLxZhw xlYw== X-Gm-Message-State: APjAAAV7Xf6ZctV0mPcQAzS/cj+7/Bg1cOZkUnJZaLeJpKEs4hFEHNNy 08/vCs2aHqd2A/Exx06lm8VbkQ== X-Google-Smtp-Source: APXvYqwtDG50+CDnx8yHTX0XWjamhJQ0wzyuOwxWwmAtlyxz5NNOSYgypMSGVytueAL/Ooa0yoUJyA== X-Received: by 2002:a63:d555:: with SMTP id v21mr90143798pgi.179.1564567253116; Wed, 31 Jul 2019 03:00:53 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id m6sm68611352pfb.151.2019.07.31.03.00.50 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 31 Jul 2019 03:00:52 -0700 (PDT) From: Baolin Wang To: sre@kernel.org Cc: orsonzhai@gmail.com, zhang.lyra@gmail.com, yuanjiang.yu@unisoc.com, baolin.wang@linaro.org, vincent.guittot@linaro.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/6] power: supply: sc27xx: Add POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN attribute Date: Wed, 31 Jul 2019 18:00:23 +0800 Message-Id: X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: 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 Add POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN attribute to provide the battery's design capacity for charger manager to calculate the charging counter. Signed-off-by: Yuanjiang Yu Signed-off-by: Baolin Wang --- drivers/power/supply/sc27xx_fuel_gauge.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c index 24895cc..2fe97ae 100644 --- a/drivers/power/supply/sc27xx_fuel_gauge.c +++ b/drivers/power/supply/sc27xx_fuel_gauge.c @@ -587,6 +587,10 @@ static int sc27xx_fgu_get_property(struct power_supply *psy, val->intval = value * 1000; break; + case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: + val->intval = data->total_cap * 1000; + break; + default: ret = -EINVAL; break; @@ -644,6 +648,7 @@ static int sc27xx_fgu_property_is_writeable(struct power_supply *psy, POWER_SUPPLY_PROP_CURRENT_NOW, POWER_SUPPLY_PROP_CURRENT_AVG, POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, + POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, }; static const struct power_supply_desc sc27xx_fgu_desc = { From patchwork Wed Jul 31 10:00:24 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: 11067449 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 0190613AC for ; Wed, 31 Jul 2019 10:01:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DB86528511 for ; Wed, 31 Jul 2019 10:01:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CFD2528815; Wed, 31 Jul 2019 10:01:17 +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 8187728511 for ; Wed, 31 Jul 2019 10:01:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387675AbfGaKA5 (ORCPT ); Wed, 31 Jul 2019 06:00:57 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:37968 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387644AbfGaKA4 (ORCPT ); Wed, 31 Jul 2019 06:00:56 -0400 Received: by mail-pl1-f194.google.com with SMTP id az7so30245622plb.5 for ; Wed, 31 Jul 2019 03:00:56 -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=i+zcdOKM4uEsoBGbIg99q0zdIYp2a4lDHZin2ZyV6Ts=; b=H77GUufatg+2unukyfaUu5mIxtf9dK45weuozY3ifgIb2dgL2u+aeUCplrl82Pq2sP b2RX+7J/p/p2zmCozRuWIf8QArtB5MJKTKSI2W5+wf5MGGRL9fquoTMXWRZQeETW5oNG 9AyOc2po9ajcww/AZ4Gk49nuuQezsh8pG3bp8pmqo6iV+urV4s2bkTAOF5lSjIztR9Cu qZOES5sOtvdtqd4j4eaANh/hLCDVHh9kULIB9iwNmy8npgfW4MWuOq18H202bivWBtxy ONuRYh2HtoahAfiLP7XBvyHSsMKGMpLWD+kd5UMYrtfkNLBu6fIixasyARHKZFxhCrrX zrUw== 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=i+zcdOKM4uEsoBGbIg99q0zdIYp2a4lDHZin2ZyV6Ts=; b=R3+/7DZBjAc2CCeWf3iNx0xCSAbBcAJ7NMjpqVxz3XhIS0O24SBz8EAY+OFnsOV0RJ HPniSJzabBjoK/8qInR70LlWeNXNA4FxJ1fh0AIrdzXPZcZi3IgGB9Y/vZMmFbsfchZy dl52i0boCRzzpkfbEaDXBnNIAVskVDaO3Vshv3zROS3wPWou+3f+DcmwBUrvU80T6XP9 dBDnCiAAnYCyYr96tMrywdcrhZk7UsNGVsM6GNuclDMQ/CVWZJg6LDAcOXb1qrrUjgCb X4x6lAfFhsAOZfomnwN1pbgf6gPpW9zDDpRTSB7jfnI8qxK6vVl0MoYUvf5LqZ7wFc2j G7Lw== X-Gm-Message-State: APjAAAUtckK+wFi6qOZdrD7JIxwsGOZI+Yi0qT3+x2Nidv8koFDfA28s BQOg398ZzEwTPsGYxOCA1BWvoQ== X-Google-Smtp-Source: APXvYqz0L0q/H6Aaq2C1hNAQC/UTqvaSD7TJO7dRD0vFgnlVqXzm0SKiN+S5lJQxC+3inAVkr70i1g== X-Received: by 2002:a17:902:4401:: with SMTP id k1mr96016005pld.193.1564567255904; Wed, 31 Jul 2019 03:00:55 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id m6sm68611352pfb.151.2019.07.31.03.00.53 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 31 Jul 2019 03:00:55 -0700 (PDT) From: Baolin Wang To: sre@kernel.org Cc: orsonzhai@gmail.com, zhang.lyra@gmail.com, yuanjiang.yu@unisoc.com, baolin.wang@linaro.org, vincent.guittot@linaro.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/6] power: supply: sc27xx: Fix conditon to enable the FGU interrupt Date: Wed, 31 Jul 2019 18:00:24 +0800 Message-Id: <84abc5a002dcc29e51bf6525aee0ca2aace8e30a.1564566425.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: 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 We should allow to enable FGU interrupt to adjust the battery capacity, when charging status is POWER_SUPPLY_STATUS_DISCHARGING. Signed-off-by: Yuanjiang Yu Signed-off-by: Baolin Wang --- drivers/power/supply/sc27xx_fuel_gauge.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c index 2fe97ae..ca7b73e 100644 --- a/drivers/power/supply/sc27xx_fuel_gauge.c +++ b/drivers/power/supply/sc27xx_fuel_gauge.c @@ -1098,7 +1098,8 @@ static int sc27xx_fgu_suspend(struct device *dev) * If we are charging, then no need to enable the FGU interrupts to * adjust the battery capacity. */ - if (status != POWER_SUPPLY_STATUS_NOT_CHARGING) + if (status != POWER_SUPPLY_STATUS_NOT_CHARGING && + status != POWER_SUPPLY_STATUS_DISCHARGING) return 0; ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_EN, From patchwork Wed Jul 31 10:00:25 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: 11067439 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 2390213A0 for ; Wed, 31 Jul 2019 10:01:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 62B06201A4 for ; Wed, 31 Jul 2019 10:01:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 53CA01FFE4; Wed, 31 Jul 2019 10:01:03 +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 A5A4628385 for ; Wed, 31 Jul 2019 10:01:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387668AbfGaKA7 (ORCPT ); Wed, 31 Jul 2019 06:00:59 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:40824 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387681AbfGaKA6 (ORCPT ); Wed, 31 Jul 2019 06:00:58 -0400 Received: by mail-pg1-f194.google.com with SMTP id w10so31724522pgj.7 for ; Wed, 31 Jul 2019 03:00:58 -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=vuwg/qLthBT40Ca2xrbvcRHyZdgOFkkkM28IonKKxd4=; b=BGrk88fawYJnzHlTqSp6jAcbVl4MvCckvfC8RgMr9Iq0piEiaZ2vyikaRxDhM9fLSn xlRwXhOZymwnuqLXEXZCVXGakJTqcBwfe9pSQxJQs9tFf3b/btbIEU6bWd7a22pHa1FX XnbKJaf5d6QeBvS6a2hJi6b7eYrlKLuT+vR5nw85UjKNJF5NAg2AvIgaN0qfUq9KW+H1 D+86lngDkbCc9sND22axjQyz0lGSDKqA/KvdbbGuRQLdSkH2iJCYhFVAMrLM5vvqeIaY 1uy4NRxiurckC0uiTQF7DYa0lIdvjQPzWAUMdCLG6DdM+/QmJSMk4+8tPDCW8GLhIrqw JCAw== 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=vuwg/qLthBT40Ca2xrbvcRHyZdgOFkkkM28IonKKxd4=; b=pNKGKu2hcGuDDX/YWo04sgAmLbIuOGllHxrtUkK53bcek9kNO10EW/YWRd73H1SebW 9rckvkX/8hA0H0Cmh7GEApaIe+8xWrx/QdQvPC5L+STq9yeotqXZ0pqtHZgbugxLk37R 7iNeiBblks75HN5yglzWHDXnY1UxD2SmTrrCgGfb9PA6zyK1oKhh2oguTOyszcZni+ot VTtVyD4twt2Dm+uYwdnFxaOQNdPTLpgNHPt1pjDV2mse73m6fuMsYfDnk0sqnpMEqA8J o51QoRrKp1drzDIjhHYnoM9MAUmaUgQtItuVtTbumS9go5a325PwnK90buGXDBFCY03P 5bhQ== X-Gm-Message-State: APjAAAUhnOtvbMdjZyGii+7IgeaynAaNOrD/tk9kzb2rHieQeqCn9yxv laNh7fboUatiI6sZeBBBm3Nm0A== X-Google-Smtp-Source: APXvYqydg9TR+BiV/Pz9CW9e98/wdHVGKxLK9oxylari74O6XN92CBconFgW3aaqYn1XtdjKzu52bg== X-Received: by 2002:a63:ee08:: with SMTP id e8mr59564051pgi.70.1564567258256; Wed, 31 Jul 2019 03:00:58 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id m6sm68611352pfb.151.2019.07.31.03.00.56 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 31 Jul 2019 03:00:57 -0700 (PDT) From: Baolin Wang To: sre@kernel.org Cc: orsonzhai@gmail.com, zhang.lyra@gmail.com, yuanjiang.yu@unisoc.com, baolin.wang@linaro.org, vincent.guittot@linaro.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/6] power: supply: sc27xx: Fix the the accuracy issue of coulomb calculation Date: Wed, 31 Jul 2019 18:00:25 +0800 Message-Id: <0e86ed1f992d2eb99fd4abb9e0c51ca2c4ccd4eb.1564566425.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: 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 The Spreadtrum fuel gauge will multiply by 2 for counting the coulomb counter to improve the accuracy, which means the value saved in fuel gauge is: coulomb counter * 2 * 1000ma_adc. Thus fix the conversion formular to improve the accuracy of calculating the battery capacity. Signed-off-by: Yuanjiang Yu Signed-off-by: Baolin Wang --- drivers/power/supply/sc27xx_fuel_gauge.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c index ca7b73e..ab3afa1 100644 --- a/drivers/power/supply/sc27xx_fuel_gauge.c +++ b/drivers/power/supply/sc27xx_fuel_gauge.c @@ -326,8 +326,6 @@ 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); @@ -362,7 +360,6 @@ static int sc27xx_fgu_get_clbcnt(struct sc27xx_fgu_data *data, int *clb_cnt) *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; } @@ -380,10 +377,10 @@ static int sc27xx_fgu_get_capacity(struct sc27xx_fgu_data *data, int *cap) /* * Convert coulomb counter to delta capacity (mAh), and set multiplier - * as 100 to improve the precision. + * as 10 to improve the precision. */ - temp = DIV_ROUND_CLOSEST(delta_clbcnt, 360); - temp = sc27xx_fgu_adc_to_current(data, temp); + temp = DIV_ROUND_CLOSEST(delta_clbcnt * 10, 36 * SC27XX_FGU_SAMPLE_HZ); + temp = sc27xx_fgu_adc_to_current(data, temp / 1000); /* * Convert to capacity percent of the battery total capacity, @@ -790,7 +787,7 @@ static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data *data, int capacity) * Convert current capacity (mAh) to coulomb counter according to the * formula: 1 mAh =3.6 coulomb. */ - return DIV_ROUND_CLOSEST(cur_cap * 36 * data->cur_1000ma_adc, 10); + return DIV_ROUND_CLOSEST(cur_cap * 36 * data->cur_1000ma_adc * SC27XX_FGU_SAMPLE_HZ, 10); } static int sc27xx_fgu_calibration(struct sc27xx_fgu_data *data) From patchwork Wed Jul 31 10:00:26 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: 11067441 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 7C12613A0 for ; Wed, 31 Jul 2019 10:01:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0D2D81FE6B for ; Wed, 31 Jul 2019 10:01:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 008D920415; Wed, 31 Jul 2019 10:01:03 +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 5CCEC1FE6B for ; Wed, 31 Jul 2019 10:01:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387705AbfGaKBB (ORCPT ); Wed, 31 Jul 2019 06:01:01 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:40405 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387692AbfGaKBB (ORCPT ); Wed, 31 Jul 2019 06:01:01 -0400 Received: by mail-pl1-f194.google.com with SMTP id a93so30212267pla.7 for ; Wed, 31 Jul 2019 03:01:01 -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=lzudkxND7QXBPzukVZ8caM1zSAxHaq1C/uuuYUCx2po=; b=JYv4Dfa5hcngN9DoTOGV4efGVdwLlRNapWP8kKxPLBBoo8QqvDyDZqRLCkSwKS1E+I NOX/xhTUok4OWxNDBqrXEDfiTWFgfk2d38fMFffnafAiCMyUQD0o800ed1YjDeoL7HT9 ZTG/w8M9Whi8WhgceM0vjdQ7K/iN5cfK8LxHF7iNXjB0Z3tnrhb2SFGsfEEi6aS4HSY2 wi38XC1RfJl7qJ+C0L3S6BCvibvLQ2CgLYoHC8JNNQwmp47MAY3X8JLS3A+p5MuWlKcB 6Efxk2uOLoWwHEtebA++1wAID8Y+ffRS3dD+hpdxRHr50yUsa2aqEOijbNN/QhwB+vSO 8zfA== 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=lzudkxND7QXBPzukVZ8caM1zSAxHaq1C/uuuYUCx2po=; b=lZXpYZUgNwF210p0yPw1asSsD9b7feF08MqqbiJDD8dnnmGfOPdnnTJKKSCuj/LqUS 6zQbZO6rsXX8EWoEVxsl1jPWCvMhdmx51RUQRecnAa0oE+aD0rRODnlPC6DLJxQdSpDV lYGxYMQP8zkaUdltiWM6PxWxbFOR9Lgs0O1d1P8ycl66/eEkHtkuYyeW/xnA9mCHUHln L1xR1YBn2To3rtATw6UUrYgEpeTyuzNiVf3BDW6Q38jr3RGivc76/0iqqFxBstjcKKZn qbnVFS1qWtuLOsmh9UV0KTaBn6MN/VsbQPL6c5XeeYZfjzBOQxgyGOoJ0uTNQbdfrCy2 yLIw== X-Gm-Message-State: APjAAAWbL6cRM0rGzWD+ZQ5TCGHZX5y/nv83HbWJMkM40K8JfpIBCuFv k4iP/Mw+xhnEmqcfrtiVBN0e0w== X-Google-Smtp-Source: APXvYqyR0e1/G2szSnTUlBQRznNPz+DXSU8mTfqTNHVZx5Nio2FgWQ4Gqlinf5EjrfrfMnJCKcFi9g== X-Received: by 2002:a17:902:2de4:: with SMTP id p91mr88832811plb.28.1564567260751; Wed, 31 Jul 2019 03:01:00 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id m6sm68611352pfb.151.2019.07.31.03.00.58 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 31 Jul 2019 03:01:00 -0700 (PDT) From: Baolin Wang To: sre@kernel.org Cc: orsonzhai@gmail.com, zhang.lyra@gmail.com, yuanjiang.yu@unisoc.com, baolin.wang@linaro.org, vincent.guittot@linaro.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/6] power: supply: sc27xx: Optimize the battery capacity calibration Date: Wed, 31 Jul 2019 18:00:26 +0800 Message-Id: <6d4155a2d95a67546c27b49ba4cd1ab2898cb570.1564566425.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: 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 This patch factors out the capacity calibration into one single function to calibrate the battery capacity, and adding more abnormal cases to calibrate the capacity when the OCV value is not matchable with current capacity. Moreover we also allow to calibrate the capacity when charger magager tries to get current capacity to make sure we give a correct capacity for userspace. Signed-off-by: Yuanjiang Yu Signed-off-by: Baolin Wang --- drivers/power/supply/sc27xx_fuel_gauge.c | 127 ++++++++++++++++++++++-------- 1 file changed, 92 insertions(+), 35 deletions(-) diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c index ab3afa1..f670131 100644 --- a/drivers/power/supply/sc27xx_fuel_gauge.c +++ b/drivers/power/supply/sc27xx_fuel_gauge.c @@ -109,6 +109,8 @@ struct sc27xx_fgu_data { }; 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, int int_mode); static const char * const sc27xx_charger_supply_name[] = { "sc2731_charger", @@ -389,6 +391,9 @@ static int sc27xx_fgu_get_capacity(struct sc27xx_fgu_data *data, int *cap) delta_cap = DIV_ROUND_CLOSEST(temp * 100, data->total_cap); *cap = delta_cap + data->init_cap; + /* Calibrate the battery capacity in a normal range. */ + sc27xx_fgu_capacity_calibration(data, *cap, false); + return 0; } @@ -661,50 +666,62 @@ static int sc27xx_fgu_property_is_writeable(struct power_supply *psy, static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data *data, int cap) { + int ret; + data->init_cap = cap; - data->init_clbcnt = sc27xx_fgu_cap_to_clbcnt(data, data->init_cap); + ret = sc27xx_fgu_get_clbcnt(data, &data->init_clbcnt); + if (ret) + dev_err(data->dev, "failed to get init coulomb counter\n"); } -static irqreturn_t sc27xx_fgu_interrupt(int irq, void *dev_id) +static void sc27xx_fgu_capacity_calibration(struct sc27xx_fgu_data *data, + int cap, int int_mode) { - struct sc27xx_fgu_data *data = dev_id; - int ret, cap, ocv, adc; - u32 status; - - mutex_lock(&data->lock); + int ret, ocv, chg_sts, adc; - ret = regmap_read(data->regmap, data->base + SC27XX_FGU_INT_STS, - &status); - if (ret) - goto out; + ret = sc27xx_fgu_get_vbat_ocv(data, &ocv); + if (ret) { + dev_err(data->dev, "get battery ocv error.\n"); + return; + } - ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_CLR, - status, status); - if (ret) - goto out; + ret = sc27xx_fgu_get_status(data, &chg_sts); + if (ret) { + dev_err(data->dev, "get charger status error.\n"); + return; + } /* - * When low overload voltage interrupt happens, we should calibrate the - * battery capacity in lower voltage stage. + * If we are in charging mode, then we do not need to calibrate the + * lower capacity. */ - if (!(status & SC27XX_FGU_LOW_OVERLOAD_INT)) - goto out; + if (chg_sts == POWER_SUPPLY_STATUS_CHARGING) + return; - ret = sc27xx_fgu_get_capacity(data, &cap); - if (ret) - goto out; - - ret = sc27xx_fgu_get_vbat_ocv(data, &ocv); - if (ret) - goto out; - - /* - * If current OCV value is less than the minimum OCV value in OCV table, - * which means now battery capacity is 0%, and we should adjust the - * inititial capacity to 0. - */ - if (ocv <= data->cap_table[data->table_len - 1].ocv) { + if ((ocv > data->cap_table[0].ocv && cap < 100) || cap > 100) { + /* + * If current OCV value is larger than the max OCV value in + * OCV table, or the current capacity is larger than 100, + * we should force the inititial capacity to 100. + */ + sc27xx_fgu_adjust_cap(data, 100); + } else if (ocv <= data->cap_table[data->table_len - 1].ocv) { + /* + * If current OCV value is leass than the minimum OCV value in + * OCV table, we should force the inititial capacity to 0. + */ sc27xx_fgu_adjust_cap(data, 0); + } else if ((ocv > data->cap_table[data->table_len - 1].ocv && cap <= 0) || + (ocv > data->min_volt && cap <= data->alarm_cap)) { + /* + * If current OCV value is not matchable with current capacity, + * we should re-calculate current capacity by looking up the + * OCV table. + */ + int cur_cap = power_supply_ocv2cap_simple(data->cap_table, + data->table_len, ocv); + + sc27xx_fgu_adjust_cap(data, cur_cap); } else if (ocv <= data->min_volt) { /* * If current OCV value is less than the low alarm voltage, but @@ -713,7 +730,7 @@ static irqreturn_t sc27xx_fgu_interrupt(int irq, void *dev_id) */ if (cap > data->alarm_cap) { sc27xx_fgu_adjust_cap(data, data->alarm_cap); - } else if (cap <= 0) { + } else { int cur_cap; /* @@ -728,15 +745,55 @@ static irqreturn_t sc27xx_fgu_interrupt(int irq, void *dev_id) sc27xx_fgu_adjust_cap(data, cur_cap); } + if (!int_mode) + return; + /* * After adjusting the battery capacity, we should set the * lowest alarm voltage instead. */ data->min_volt = data->cap_table[data->table_len - 1].ocv; + data->alarm_cap = power_supply_ocv2cap_simple(data->cap_table, + data->table_len, + data->min_volt); + adc = sc27xx_fgu_voltage_to_adc(data, data->min_volt / 1000); - regmap_update_bits(data->regmap, data->base + SC27XX_FGU_LOW_OVERLOAD, + regmap_update_bits(data->regmap, + data->base + SC27XX_FGU_LOW_OVERLOAD, SC27XX_FGU_LOW_OVERLOAD_MASK, adc); } +} + +static irqreturn_t sc27xx_fgu_interrupt(int irq, void *dev_id) +{ + struct sc27xx_fgu_data *data = dev_id; + int ret, cap; + u32 status; + + mutex_lock(&data->lock); + + ret = regmap_read(data->regmap, data->base + SC27XX_FGU_INT_STS, + &status); + if (ret) + goto out; + + ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_CLR, + status, status); + if (ret) + goto out; + + /* + * When low overload voltage interrupt happens, we should calibrate the + * battery capacity in lower voltage stage. + */ + if (!(status & SC27XX_FGU_LOW_OVERLOAD_INT)) + goto out; + + ret = sc27xx_fgu_get_capacity(data, &cap); + if (ret) + goto out; + + sc27xx_fgu_capacity_calibration(data, cap, true); out: mutex_unlock(&data->lock); From patchwork Wed Jul 31 10:00:27 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: 11067443 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 82F8A1398 for ; Wed, 31 Jul 2019 10:01:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6D5BA28815 for ; Wed, 31 Jul 2019 10:01:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6001828869; Wed, 31 Jul 2019 10:01:12 +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 0A9BB28511 for ; Wed, 31 Jul 2019 10:01:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729092AbfGaKBL (ORCPT ); Wed, 31 Jul 2019 06:01:11 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:40093 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387645AbfGaKBD (ORCPT ); Wed, 31 Jul 2019 06:01:03 -0400 Received: by mail-pf1-f195.google.com with SMTP id p184so31580005pfp.7 for ; Wed, 31 Jul 2019 03:01:03 -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=hT++od96JIv4ERj/5q43A9sdV6GV4nXuETH/GErjU/I=; b=kIXAp4MzviR01IRWqe7+ak4T/tIj/IFNOIOhRvpjmPe4Iky3tFm/wfi5mHwn+pc5DK BQr4/V2CGVXy3LsdNBMhg93+PTiKMQhTlHIm5KoA1US4OPQS9jfABuFeaTh5gnT+r2aX nIoi4PywtlKhdVqSZOj3IKK1NvCelqy/7qMhasvUnDLoaz2EMKpUZIxxfyXFzAraP4Ox iKncbdTOLXT99eGgIOaUMIJJzvIoi2zdT+fSBRWqENUmT/UezZtf8dIF2BK3IICY2HN+ D695VleypTnWA8Je1k4uXVgjgWZ8D8GJiwdlybFBgC7IWA4KSWK3FBUq7x1kMW9XG3xw eB2A== 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=hT++od96JIv4ERj/5q43A9sdV6GV4nXuETH/GErjU/I=; b=Y7fJsu9Iwkjme8lEbSEPbQ27x6VDdodlzXIgqnGzaz1WykMD26AHwPahanTx5Kot5c h7+WrcGNUwTA3zwoM2kWM86W0+gGme37Zwlzp/w5pRe1I5oCQ3IQJWZ97KxqSLFFZJ8S n5ZZKFta6326tWwcDbmAWBVxn9EfIt9D/p+BaPjn+KXDTv6ydYv9GA9wv8XnV6uYYziT 5rCN4JmGFVXO71okec/+0k0nk8hOpu0nmRfMlQ866tEpu41eSyJQSmWGPLq1R48q1Q5v aLyBsHvjkibVI+R0QwyWkBsSgIXZUSvylh+itRbFsCjgBq1ZxRFQMYMpIDfj3GuQSZ0D vAVQ== X-Gm-Message-State: APjAAAW8gza1dv83CyREZxHeGqldbRmXDdMQXyg8iSgF/3sKvvkIBI0F +fWbIEr4UrveYHIC6RGbLHiCaA== X-Google-Smtp-Source: APXvYqzXNx5w1M0CUiKp7nGMuwNt7ZCkwdDTEa9GNGjgnC2N3BvUWTewMA7HX54pOwDNkuJOSYLSgw== X-Received: by 2002:aa7:8acb:: with SMTP id b11mr44975412pfd.109.1564567263283; Wed, 31 Jul 2019 03:01:03 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id m6sm68611352pfb.151.2019.07.31.03.01.00 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 31 Jul 2019 03:01:02 -0700 (PDT) From: Baolin Wang To: sre@kernel.org Cc: orsonzhai@gmail.com, zhang.lyra@gmail.com, yuanjiang.yu@unisoc.com, baolin.wang@linaro.org, vincent.guittot@linaro.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 5/6] power: supply: sc27xx: Make sure the alarm capacity is larger than 0 Date: Wed, 31 Jul 2019 18:00:27 +0800 Message-Id: <5c3bb516c6020410a932c12eb2481fd75c34b7fe.1564566425.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: 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 We must make sure the alarm capacity is larger than 0, to help to calibrate the low battery capacity. Signed-off-by: Yuanjiang Yu Signed-off-by: Baolin Wang --- drivers/power/supply/sc27xx_fuel_gauge.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c index f670131..fa85f40 100644 --- a/drivers/power/supply/sc27xx_fuel_gauge.c +++ b/drivers/power/supply/sc27xx_fuel_gauge.c @@ -915,6 +915,8 @@ static int sc27xx_fgu_hw_init(struct sc27xx_fgu_data *data) data->alarm_cap = power_supply_ocv2cap_simple(data->cap_table, data->table_len, data->min_volt); + if (!data->alarm_cap) + data->alarm_cap += 1; power_supply_put_battery_info(data->battery, &info); From patchwork Wed Jul 31 10:00:28 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: 11067445 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 5CDCC1398 for ; Wed, 31 Jul 2019 10:01:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4711828511 for ; Wed, 31 Jul 2019 10:01:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3B55C28815; Wed, 31 Jul 2019 10:01:16 +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 0F87B28511 for ; Wed, 31 Jul 2019 10:01:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729071AbfGaKBK (ORCPT ); Wed, 31 Jul 2019 06:01:10 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:40416 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387724AbfGaKBG (ORCPT ); Wed, 31 Jul 2019 06:01:06 -0400 Received: by mail-pl1-f196.google.com with SMTP id a93so30212374pla.7 for ; Wed, 31 Jul 2019 03:01:05 -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=XF5mZ6TSVo4eSNdQwLP31itinD3L/9CCtkqFjn0djlU=; b=UHCfglolFc+DiKNqu28PP7eW3FZEIuVvD/ZAL0ARBydSmw5J1AvOi7RqECGvzy6OyG JjznumgXnMlrgA+kEdFEsntSef0+YnGvMTLpX+q4IecNPm57QuzzIsAAWnE3sBhklwoQ aYzQ3bw8+p+ZPNrOrSDamq+nkI+kJfTvuVAL4kdLH6lmo3FnsURjQaFVeGvWtuV/dx7M Oesx0zR11jotI5yl8mxyz374bO4ddYQKkHky0Sx447WMBTRl/sKaHqp16dEOPi1Tzgev 2vBnBHlc0ZsG6mTdF49WwAunGzn3pS9UeqSoQFADQK3GU6s/SIfyzE1ni8WGsDTjD2ek FFBg== 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=XF5mZ6TSVo4eSNdQwLP31itinD3L/9CCtkqFjn0djlU=; b=T4LjJ+gasMVF1GF4oNvpV3q/M7jQzaeTmi+j7pNtCi4QObLhfxz8+bvnL/CcBGHYUf rzP3AM8sJH0XVKT/HaUYimknanjyoQiR7DcRzkK+VdcryDGg4GK+0e7wX6+PftEMGzwD 8ZFb1p9UCT2yqvDq/8vqwZDrXH4BjKHsfOSS5jo03acjCB+qTjpqIecX5dxexpaiBi8o L+a4RLSOeg4r+U7torUBh/ItsTRkGuTW22pE9+gHtHnf0w1skt3LD23DPdgVpRGHwpRK a4VBaTGezzi0lS7CN+Asd/56PXCMopGUaZs7k6vi3Hg5+DEOCdvDzUZB2N0go/jGRCvj SVwA== X-Gm-Message-State: APjAAAWMg/sjjUpG2Ziqb3eMPNLu7CrD8yDUzq1aaVpq1AEAO+nzrPfv QJSUHVrcss3W4kjDHusQL62BPg== X-Google-Smtp-Source: APXvYqwNQvRYJbmUxrXwPc+d2Y+WlvIn2qkESp52+VZCGPtoCkNpZLLC2P1EOSKz+HvgKnVzuGG4XA== X-Received: by 2002:a17:902:a514:: with SMTP id s20mr113281631plq.162.1564567265611; Wed, 31 Jul 2019 03:01:05 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id m6sm68611352pfb.151.2019.07.31.03.01.03 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 31 Jul 2019 03:01:05 -0700 (PDT) From: Baolin Wang To: sre@kernel.org Cc: orsonzhai@gmail.com, zhang.lyra@gmail.com, yuanjiang.yu@unisoc.com, baolin.wang@linaro.org, vincent.guittot@linaro.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 6/6] power: supply: sc27xx: Add POWER_SUPPLY_PROP_CALIBRATE attribute Date: Wed, 31 Jul 2019 18:00:28 +0800 Message-Id: <554dd7e2ef4f3c5247322d8b185607748144b1e6.1564566425.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: 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 Add the 'POWER_SUPPLY_PROP_CALIBRATE' attribute to allow chareger manager to calibrate the battery capacity. Signed-off-by: Yuanjiang Yu Signed-off-by: Baolin Wang --- drivers/power/supply/sc27xx_fuel_gauge.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c index fa85f40..6363c48 100644 --- a/drivers/power/supply/sc27xx_fuel_gauge.c +++ b/drivers/power/supply/sc27xx_fuel_gauge.c @@ -111,6 +111,7 @@ struct sc27xx_fgu_data { 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, int int_mode); +static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data *data, int cap); static const char * const sc27xx_charger_supply_name[] = { "sc2731_charger", @@ -610,17 +611,25 @@ static int sc27xx_fgu_set_property(struct power_supply *psy, 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); + 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; - mutex_unlock(&data->lock); + case POWER_SUPPLY_PROP_CALIBRATE: + sc27xx_fgu_adjust_cap(data, val->intval); + ret = 0; + break; - if (ret < 0) - dev_err(data->dev, "failed to save battery capacity\n"); + default: + ret = -EINVAL; + } + + mutex_unlock(&data->lock); return ret; } @@ -635,7 +644,8 @@ static void sc27xx_fgu_external_power_changed(struct power_supply *psy) static int sc27xx_fgu_property_is_writeable(struct power_supply *psy, enum power_supply_property psp) { - return psp == POWER_SUPPLY_PROP_CAPACITY; + return psp == POWER_SUPPLY_PROP_CAPACITY || + psp == POWER_SUPPLY_PROP_CALIBRATE; } static enum power_supply_property sc27xx_fgu_props[] = { @@ -651,6 +661,7 @@ static int sc27xx_fgu_property_is_writeable(struct power_supply *psy, POWER_SUPPLY_PROP_CURRENT_AVG, POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, + POWER_SUPPLY_PROP_CALIBRATE, }; static const struct power_supply_desc sc27xx_fgu_desc = {