From patchwork Mon Apr 15 01:26:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matheus Castello X-Patchwork-Id: 10900059 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 D2C84139A for ; Mon, 15 Apr 2019 01:50:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B511428823 for ; Mon, 15 Apr 2019 01:50:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A6FC22880A; Mon, 15 Apr 2019 01:50:43 +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=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,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 289A02880A for ; Mon, 15 Apr 2019 01:50:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725972AbfDOBum (ORCPT ); Sun, 14 Apr 2019 21:50:42 -0400 Received: from gateway30.websitewelcome.com ([192.185.197.25]:43965 "EHLO gateway30.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725775AbfDOBum (ORCPT ); Sun, 14 Apr 2019 21:50:42 -0400 X-Greylist: delayed 1412 seconds by postgrey-1.27 at vger.kernel.org; Sun, 14 Apr 2019 21:50:41 EDT Received: from cm11.websitewelcome.com (cm11.websitewelcome.com [100.42.49.5]) by gateway30.websitewelcome.com (Postfix) with ESMTP id 6B015433C for ; Sun, 14 Apr 2019 20:27:08 -0500 (CDT) Received: from br164.hostgator.com.br ([192.185.176.180]) by cmsmtp with SMTP id FqOqhav0zdnCeFqOqhO6M5; Sun, 14 Apr 2019 20:27:08 -0500 X-Authority-Reason: nr=8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=castello.eng.br; s=default; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=HbUN1dkFsSC6BI7XO52/1sf3MtLPus2m67WaPbKxaeM=; b=r+jxGqJ74k69+sNIaFym6CTH8 r8SWqWm4a6ZchcL/ZFLH+xYQdxHTv7sguflHZz6firABom7LMFUetp5UvwCSq2jiVQh/1N3VcNsSx jiT1b7Q4pYNQXwM2D81sGfkKYU40h5E+Hp+Lh69sP3xOHMecRsd6Ah2VfBYYBT3LSCCAf5vVUMqLL 8AmKUxA3hMSjOs83WToLbuhCmh5RnfUuFd0wOPd4l8ikdDB4cjNLLdVVth2Seip4FE3Ex8rFaR7Ig nfyKpXmhQ3G/5PzLyi2lrAhYLGobLMqtW48g/RICFRcpTbWvYe0VfPZpzYswwH4bDdVH/G2e8Ug+S DeyCbMhdQ==; Received: from [191.191.240.251] (port=33232 helo=localhost.localdomain) by br164.hostgator.com.br with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.91) (envelope-from ) id 1hFqOp-000Uj7-OV; Sun, 14 Apr 2019 22:27:08 -0300 From: Matheus Castello To: sre@kernel.org, krzk@kernel.org, robh+dt@kernel.org Cc: mark.rutland@arm.com, cw00.choi@samsung.com, b.zolnierkie@samsung.com, lee.jones@linaro.org, linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Matheus Castello Subject: [PATCH v2 1/4] power: supply: max17040: Add IRQ handler for low SOC alert Date: Sun, 14 Apr 2019 22:26:32 -0300 Message-Id: <20190415012635.6369-2-matheus@castello.eng.br> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20190415012635.6369-1-matheus@castello.eng.br> References: <20190415012635.6369-1-matheus@castello.eng.br> X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - br164.hostgator.com.br X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - castello.eng.br X-BWhitelist: no X-Source-IP: 191.191.240.251 X-Source-L: No X-Exim-ID: 1hFqOp-000Uj7-OV X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (localhost.localdomain) [191.191.240.251]:33232 X-Source-Auth: matheus@castello.eng.br X-Email-Count: 24 X-Source-Cap: Y2FzdGUyNDg7Y2FzdGUyNDg7YnIxNjQuaG9zdGdhdG9yLmNvbS5icg== X-Local-Domain: yes 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 According datasheet max17040 has a pin for alert host for low SOC. This pin can be used as external interrupt, so we need to check for interrupts assigned for device and handle it. In hadler we are checking and storing fuel gauge registers values and send an uevent to notificate user space, so user space can decide save work or turn off since the alert demonstrate that the battery may no have the power to keep the system turned on for much longer. Signed-off-by: Matheus Castello --- drivers/power/supply/max17040_battery.c | 69 +++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 5 deletions(-) diff --git a/drivers/power/supply/max17040_battery.c b/drivers/power/supply/max17040_battery.c index 91cafc7bed30..8d2f8ed3f44c 100644 --- a/drivers/power/supply/max17040_battery.c +++ b/drivers/power/supply/max17040_battery.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -160,21 +161,40 @@ static void max17040_get_status(struct i2c_client *client) chip->status = POWER_SUPPLY_STATUS_FULL; } +static void max17040_check_changes(struct i2c_client *client) +{ + max17040_get_vcell(client); + max17040_get_soc(client); + max17040_get_online(client); + max17040_get_status(client); +} + static void max17040_work(struct work_struct *work) { struct max17040_chip *chip; chip = container_of(work, struct max17040_chip, work.work); - - max17040_get_vcell(chip->client); - max17040_get_soc(chip->client); - max17040_get_online(chip->client); - max17040_get_status(chip->client); + max17040_check_changes(chip->client); queue_delayed_work(system_power_efficient_wq, &chip->work, MAX17040_DELAY); } +static irqreturn_t max17040_thread_handler(int id, void *dev) +{ + struct max17040_chip *chip = dev; + struct i2c_client *client = chip->client; + + dev_warn(&client->dev, "IRQ: Alert battery low level"); + /* read registers */ + max17040_check_changes(chip->client); + + /* send uevent */ + power_supply_changed(chip->battery); + + return IRQ_HANDLED; +} + static enum power_supply_property max17040_battery_props[] = { POWER_SUPPLY_PROP_STATUS, POWER_SUPPLY_PROP_ONLINE, @@ -217,6 +237,27 @@ static int max17040_probe(struct i2c_client *client, return PTR_ERR(chip->battery); } + /* check interrupt */ + if (client->irq) { + int ret; + unsigned int flags; + + dev_info(&client->dev, "IRQ: enabled\n"); + flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT; + + ret = devm_request_threaded_irq(&client->dev, client->irq, NULL, + max17040_thread_handler, flags, + chip->battery->desc->name, + chip); + + if (ret) { + client->irq = 0; + if (ret != -EBUSY) + dev_warn(&client->dev, + "Failed to get IRQ err %d\n", ret); + } + } + max17040_reset(client); max17040_get_version(client); @@ -224,6 +265,8 @@ static int max17040_probe(struct i2c_client *client, queue_delayed_work(system_power_efficient_wq, &chip->work, MAX17040_DELAY); + device_init_wakeup(&client->dev, 1); + return 0; } @@ -244,6 +287,14 @@ static int max17040_suspend(struct device *dev) struct max17040_chip *chip = i2c_get_clientdata(client); cancel_delayed_work(&chip->work); + + if (client->irq) { + if (device_may_wakeup(dev)) + enable_irq_wake(client->irq); + else + disable_irq_wake(client->irq); + } + return 0; } @@ -254,6 +305,14 @@ static int max17040_resume(struct device *dev) queue_delayed_work(system_power_efficient_wq, &chip->work, MAX17040_DELAY); + + if (client->irq) { + if (device_may_wakeup(dev)) + disable_irq_wake(client->irq); + else + enable_irq_wake(client->irq); + } + return 0; } From patchwork Mon Apr 15 01:26:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matheus Castello X-Patchwork-Id: 10900061 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 062551800 for ; Mon, 15 Apr 2019 01:50:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E023E2880A for ; Mon, 15 Apr 2019 01:50:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D3F4B28826; Mon, 15 Apr 2019 01:50:43 +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=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,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 7147028820 for ; Mon, 15 Apr 2019 01:50:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725775AbfDOBum (ORCPT ); Sun, 14 Apr 2019 21:50:42 -0400 Received: from gateway30.websitewelcome.com ([192.185.197.25]:34729 "EHLO gateway30.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725789AbfDOBum (ORCPT ); Sun, 14 Apr 2019 21:50:42 -0400 Received: from cm17.websitewelcome.com (cm17.websitewelcome.com [100.42.49.20]) by gateway30.websitewelcome.com (Postfix) with ESMTP id CD83613F0D for ; Sun, 14 Apr 2019 20:27:14 -0500 (CDT) Received: from br164.hostgator.com.br ([192.185.176.180]) by cmsmtp with SMTP id FqOwhI3uy90onFqOwhTXas; Sun, 14 Apr 2019 20:27:14 -0500 X-Authority-Reason: nr=8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=castello.eng.br; s=default; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=j38GHo+STQz5ACyf1XHDDr2+jK7GF1V7iahohtBK4yo=; b=f2ebh7haEwMegcCIxVR7xjO1n EB+j6us50YAAMRY/bpcC4V+/aoBVELl912zIRYy8Z28OvSh/Lk4CNyBqTGoZpqDs+656N8QcjNEnQ u9ARYsbfbC0d5cJtH10SmcM2sxbEBVorclnQkyaFYOt4CeVVxknh8dZnQbWLU4ZYTotf6A4WFHz8k B4k1UctAdereqXwVwMwu/KKfGdlLV6VyFAIXAFx+Usreog9e4IXBW+5B6XDT1mVWZo2Mmsh3tiSwW uBYws8Pqfqz4SHzy/p6JYaVI4fIy1sNCi2huuLNNckkJBIcwjvxs+SnbNmqUNMlwL8XTQR9qa00zv 1oaEftFPA==; Received: from [191.191.240.251] (port=33232 helo=localhost.localdomain) by br164.hostgator.com.br with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.91) (envelope-from ) id 1hFqOw-000Uj7-0G; Sun, 14 Apr 2019 22:27:14 -0300 From: Matheus Castello To: sre@kernel.org, krzk@kernel.org, robh+dt@kernel.org Cc: mark.rutland@arm.com, cw00.choi@samsung.com, b.zolnierkie@samsung.com, lee.jones@linaro.org, linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Matheus Castello Subject: [PATCH v2 2/4] dt-bindings: power: supply: Max17040: Add low level SOC alert threshold Date: Sun, 14 Apr 2019 22:26:33 -0300 Message-Id: <20190415012635.6369-3-matheus@castello.eng.br> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20190415012635.6369-1-matheus@castello.eng.br> References: <20190415012635.6369-1-matheus@castello.eng.br> X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - br164.hostgator.com.br X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - castello.eng.br X-BWhitelist: no X-Source-IP: 191.191.240.251 X-Source-L: No X-Exim-ID: 1hFqOw-000Uj7-0G X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (localhost.localdomain) [191.191.240.251]:33232 X-Source-Auth: matheus@castello.eng.br X-Email-Count: 35 X-Source-Cap: Y2FzdGUyNDg7Y2FzdGUyNDg7YnIxNjQuaG9zdGdhdG9yLmNvbS5icg== X-Local-Domain: yes 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 For configure low level state of charge threshold alert signaled from max17040 we add "maxim,alert-soc-level" property. Signed-off-by: Matheus Castello --- .../power/supply/max17040_battery.txt | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Documentation/devicetree/bindings/power/supply/max17040_battery.txt diff --git a/Documentation/devicetree/bindings/power/supply/max17040_battery.txt b/Documentation/devicetree/bindings/power/supply/max17040_battery.txt new file mode 100644 index 000000000000..9b2cc67d556f --- /dev/null +++ b/Documentation/devicetree/bindings/power/supply/max17040_battery.txt @@ -0,0 +1,24 @@ +max17040_battery +~~~~~~~~~~~~~~~~ + +Required properties : + - compatible : "maxim,max17040" or "maxim,max77836-battery" + +Optional properties : +- maxim,alert-soc-level : The alert threshold that sets the state of + charge level where an interrupt is generated. + Can be configured from 1 up to 32. If skipped + the power up default value of 4 will be used. +- interrupt-parent : The GPIO bank from the interrupt line. +- interrupts : Interrupt line see Documentation/devicetree/ + bindings/interrupt-controller/interrupts.txt + +Example: + + battery-charger@36 { + compatible = "maxim,max17040"; + reg = <0x36>; + maxim,alert-soc-level = <10>; + interrupt-parent = <&gpio7>; + interrupts = <2 IRQ_TYPE_EDGE_FALLING>; + }; From patchwork Mon Apr 15 01:26:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matheus Castello X-Patchwork-Id: 10900065 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 DC20C1800 for ; Mon, 15 Apr 2019 01:51:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C277028820 for ; Mon, 15 Apr 2019 01:51:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B678B28826; Mon, 15 Apr 2019 01:51:56 +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=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,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 4849B28823 for ; Mon, 15 Apr 2019 01:51:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725775AbfDOBvz (ORCPT ); Sun, 14 Apr 2019 21:51:55 -0400 Received: from gateway21.websitewelcome.com ([192.185.45.155]:31513 "EHLO gateway21.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726111AbfDOBvz (ORCPT ); Sun, 14 Apr 2019 21:51:55 -0400 Received: from cm13.websitewelcome.com (cm13.websitewelcome.com [100.42.49.6]) by gateway21.websitewelcome.com (Postfix) with ESMTP id DBB24400C5811 for ; Sun, 14 Apr 2019 20:27:19 -0500 (CDT) Received: from br164.hostgator.com.br ([192.185.176.180]) by cmsmtp with SMTP id FqP1hXpQ4YTGMFqP1hffDP; Sun, 14 Apr 2019 20:27:19 -0500 X-Authority-Reason: nr=8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=castello.eng.br; s=default; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=R1H/zb0qpfuLZ6IzHqM2mtOCJc/6Ct/oVxfuSRZenXs=; b=ZPIF36RhX8vVvv1zPrlaLQIT1 MCQxb2Mc49otqH+84aJe3M8m0M0GzdybYyuvINnt18tiLAbM3rVXVvtl8rKJrhJaAfy7oN06HKyey 4jwDJDEc2CibPOtR7Hy1036cBkkCKs3PrwoHQ5EQqKdvoG61WwOSp1xLoj2jccfHOO0TSao0eimPR PQB9VVQpNQ3KCio9fmB8NSWnRd6yn+/l4823VWvKR3HkUHo6b5LCaIebaGSELUUzHVHZjIZlncDo5 ENZd1QFxPg9Xm2nQfLBLlE3ICTkdGklVL+yxGQ7+sEJ7S4USVoF4d1Kdb2VtO+hQ7yrf0Vp0C1AhL FFPTCIwVg==; Received: from [191.191.240.251] (port=33232 helo=localhost.localdomain) by br164.hostgator.com.br with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.91) (envelope-from ) id 1hFqP1-000Uj7-1F; Sun, 14 Apr 2019 22:27:19 -0300 From: Matheus Castello To: sre@kernel.org, krzk@kernel.org, robh+dt@kernel.org Cc: mark.rutland@arm.com, cw00.choi@samsung.com, b.zolnierkie@samsung.com, lee.jones@linaro.org, linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Matheus Castello Subject: [PATCH v2 3/4] power: supply: max17040: Config alert SOC low level threshold from FDT Date: Sun, 14 Apr 2019 22:26:34 -0300 Message-Id: <20190415012635.6369-4-matheus@castello.eng.br> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20190415012635.6369-1-matheus@castello.eng.br> References: <20190415012635.6369-1-matheus@castello.eng.br> X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - br164.hostgator.com.br X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - castello.eng.br X-BWhitelist: no X-Source-IP: 191.191.240.251 X-Source-L: No X-Exim-ID: 1hFqP1-000Uj7-1F X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (localhost.localdomain) [191.191.240.251]:33232 X-Source-Auth: matheus@castello.eng.br X-Email-Count: 46 X-Source-Cap: Y2FzdGUyNDg7Y2FzdGUyNDg7YnIxNjQuaG9zdGdhdG9yLmNvbS5icg== X-Local-Domain: yes 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 For configuration of fuel gauge alert for a low level state of charge interrupt we add a function to config level threshold and a device tree binding property to set it in flatned device tree node. Now we can use "maxim,alert-soc-level" property with the values from 1 up to 32 to configure alert interrupt threshold. Signed-off-by: Matheus Castello --- drivers/power/supply/max17040_battery.c | 56 ++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/drivers/power/supply/max17040_battery.c b/drivers/power/supply/max17040_battery.c index 8d2f8ed3f44c..f036f272d52f 100644 --- a/drivers/power/supply/max17040_battery.c +++ b/drivers/power/supply/max17040_battery.c @@ -29,6 +29,9 @@ #define MAX17040_DELAY 1000 #define MAX17040_BATTERY_FULL 95 +#define MAX17040_ATHD_MASK 0xFFE0 +#define MAX17040_ATHD_DEFAULT_POWER_UP 4 + struct max17040_chip { struct i2c_client *client; struct delayed_work work; @@ -43,6 +46,8 @@ struct max17040_chip { int soc; /* State Of Charge */ int status; + /* Alert threshold from 32% to 1% of the State of Charge */ + u32 alert_threshold; }; static int max17040_get_property(struct power_supply *psy, @@ -119,6 +124,27 @@ static void max17040_get_soc(struct i2c_client *client) chip->soc = (soc >> 8); } +static int max17040_set_soc_threshold(struct i2c_client *client, u32 level) +{ + int ret; + u16 data; + + /* check if level is between 1% and 32% */ + if (level > 0 && level < 33) { + /* alert threshold use LSb 5 bits from RCOMP */ + level = 32 - level; + data = max17040_read_reg(client, MAX17040_RCOMP); + data &= MAX17040_ATHD_MASK; + data |= level; + max17040_write_reg(client, MAX17040_RCOMP, data); + ret = 0; + } else { + ret = -EINVAL; + } + + return ret; +} + static void max17040_get_version(struct i2c_client *client) { u16 version; @@ -161,6 +187,16 @@ static void max17040_get_status(struct i2c_client *client) chip->status = POWER_SUPPLY_STATUS_FULL; } +static void max17040_get_of_data(struct max17040_chip *chip) +{ + struct device *dev = &chip->client->dev; + struct device_node *np = dev->of_node; + + if (of_property_read_u32(np, "maxim,alert-soc-level", + &chip->alert_threshold)) + chip->alert_threshold = MAX17040_ATHD_DEFAULT_POWER_UP; +} + static void max17040_check_changes(struct i2c_client *client) { max17040_get_vcell(client); @@ -226,6 +262,7 @@ static int max17040_probe(struct i2c_client *client, chip->client = client; chip->pdata = client->dev.platform_data; + max17040_get_of_data(chip); i2c_set_clientdata(client, chip); psy_cfg.drv_data = chip; @@ -237,16 +274,26 @@ static int max17040_probe(struct i2c_client *client, return PTR_ERR(chip->battery); } + max17040_reset(client); + max17040_get_version(client); + /* check interrupt */ if (client->irq) { int ret; - unsigned int flags; + + ret = max17040_set_soc_threshold(client, chip->alert_threshold); + if (ret) { + dev_err(&client->dev, + "Failed to set SOC alert threshold: err %d\n", + ret); + return ret; + } dev_info(&client->dev, "IRQ: enabled\n"); - flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT; ret = devm_request_threaded_irq(&client->dev, client->irq, NULL, - max17040_thread_handler, flags, + max17040_thread_handler, + (client->flags | IRQF_ONESHOT), chip->battery->desc->name, chip); @@ -258,9 +305,6 @@ static int max17040_probe(struct i2c_client *client, } } - max17040_reset(client); - max17040_get_version(client); - INIT_DEFERRABLE_WORK(&chip->work, max17040_work); queue_delayed_work(system_power_efficient_wq, &chip->work, MAX17040_DELAY); From patchwork Mon Apr 15 01:26:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matheus Castello X-Patchwork-Id: 10900063 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 A7F421669 for ; Mon, 15 Apr 2019 01:51:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8FC5028820 for ; Mon, 15 Apr 2019 01:51:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 826A728842; Mon, 15 Apr 2019 01:51:56 +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=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,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 26F0B28820 for ; Mon, 15 Apr 2019 01:51:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725789AbfDOBvz (ORCPT ); Sun, 14 Apr 2019 21:51:55 -0400 Received: from gateway21.websitewelcome.com ([192.185.45.155]:26566 "EHLO gateway21.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726201AbfDOBvz (ORCPT ); Sun, 14 Apr 2019 21:51:55 -0400 Received: from cm14.websitewelcome.com (cm14.websitewelcome.com [100.42.49.7]) by gateway21.websitewelcome.com (Postfix) with ESMTP id DCEBA400C52D1 for ; Sun, 14 Apr 2019 20:27:28 -0500 (CDT) Received: from br164.hostgator.com.br ([192.185.176.180]) by cmsmtp with SMTP id FqPAhJkje2qH7FqPAhfxXT; Sun, 14 Apr 2019 20:27:28 -0500 X-Authority-Reason: nr=8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=castello.eng.br; s=default; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=nli8XspRAniu+hZMp6iB5II++QseN+g9eYpJ/egI1YI=; b=htsbbGEN8M1y4WkfxhcRtS26n 7SIG8Y/yr7EJPBo2tatX7ijBZ2s0oPBRPwo2Xj145M0c1og/RyMZ4aGiif0ZKATDA8JSX1Onjm4UI opyT4H4akgPqiBcPudphUJmr1pPN8eqXYTZxwI2m9Z3AG5Fqt7wYm4VUaMrrznFffaqCC609bkCPx B+hDpqksLPLkl/TKOkAVjyYze4cVkVQK+CjUgBfgxxpAxjQExnAnVTXhQshN3HnFdrH/o4O0BjTdS IktnUmZo40VWRVTS86q7r83PmQZt+IgkCE0ZL+kVtOXHEAoRJoqAcofTXu60KZDUANaUuqodwk4hC gO/A/cLTw==; Received: from [191.191.240.251] (port=33232 helo=localhost.localdomain) by br164.hostgator.com.br with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.91) (envelope-from ) id 1hFqP7-000Uj7-Tc; Sun, 14 Apr 2019 22:27:28 -0300 From: Matheus Castello To: sre@kernel.org, krzk@kernel.org, robh+dt@kernel.org Cc: mark.rutland@arm.com, cw00.choi@samsung.com, b.zolnierkie@samsung.com, lee.jones@linaro.org, linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Matheus Castello Subject: [PATCH v2 4/4] power: supply: max17040: Send uevent in SOC changes Date: Sun, 14 Apr 2019 22:26:35 -0300 Message-Id: <20190415012635.6369-5-matheus@castello.eng.br> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20190415012635.6369-1-matheus@castello.eng.br> References: <20190415012635.6369-1-matheus@castello.eng.br> X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - br164.hostgator.com.br X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - castello.eng.br X-BWhitelist: no X-Source-IP: 191.191.240.251 X-Source-L: No X-Exim-ID: 1hFqP7-000Uj7-Tc X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (localhost.localdomain) [191.191.240.251]:33232 X-Source-Auth: matheus@castello.eng.br X-Email-Count: 57 X-Source-Cap: Y2FzdGUyNDg7Y2FzdGUyNDg7YnIxNjQuaG9zdGdhdG9yLmNvbS5icg== X-Local-Domain: yes 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 Notify core through power_supply_changed() in case of changes in state of charge. This is useful for user-space to efficiently update current battery level. Signed-off-by: Matheus Castello --- drivers/power/supply/max17040_battery.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/power/supply/max17040_battery.c b/drivers/power/supply/max17040_battery.c index f036f272d52f..db901ebf495d 100644 --- a/drivers/power/supply/max17040_battery.c +++ b/drivers/power/supply/max17040_battery.c @@ -208,10 +208,17 @@ static void max17040_check_changes(struct i2c_client *client) static void max17040_work(struct work_struct *work) { struct max17040_chip *chip; + int last_soc; chip = container_of(work, struct max17040_chip, work.work); + /* store SOC for check change */ + last_soc = chip->soc; max17040_check_changes(chip->client); + /* check changes and send uevent */ + if (last_soc != chip->soc) + power_supply_changed(chip->battery); + queue_delayed_work(system_power_efficient_wq, &chip->work, MAX17040_DELAY); }