From patchwork Mon Sep 25 08:18:11 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Pavel Machek X-Patchwork-Id: 9969433 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 8F0DF60365 for ; Mon, 25 Sep 2017 08:20:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8386728C1E for ; Mon, 25 Sep 2017 08:20:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 77F0128C22; Mon, 25 Sep 2017 08:20:38 +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=-4.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id CE78228C1E for ; Mon, 25 Sep 2017 08:20:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender:Content-Type: List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: MIME-Version:Message-ID:Subject:To:From:Date:Reply-To:Cc: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=n4nmpKQLfuARU2nSqqtaw/A51Kph9Sk4AFwMbeqelEU=; b=Ov2 mHPhCCbgrW8HQNSYb4qSFHdAW9lie3r7Hvl0g+3yXAFl1reeAv3wGr90v/Cq2rNqWmtbz8a4EWi5c bZLVNCKOCTxKyJkFUqmyIbqoCx1a6SLlXZpWixNRKNARCk5nRQV6sIVVzBZEOaeOp8iz8f++AhEPK psktKkK42wHWI4+WY646rVPDxTBbVQ8erbwghl1f+bcez3zGb/JY/LXX86kheh84d4vNy7QXHHXtp 0WrpoxZXDVKC/k3EaEcYkYUWNzp+6SVb+uXIH4CSBl3UN0Tt1MuPbQ+KAhPoSIrTdEgb14t867mVQ zum1beb+DqOMGO6uytxAgaarkAp5e3A==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dwOcv-0005PY-PB; Mon, 25 Sep 2017 08:20:29 +0000 Received: from atrey.karlin.mff.cuni.cz ([195.113.26.193]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dwOb3-0002TA-0w for linux-arm-kernel@lists.infradead.org; Mon, 25 Sep 2017 08:18:37 +0000 Received: by atrey.karlin.mff.cuni.cz (Postfix, from userid 512) id 7D97981860; Mon, 25 Sep 2017 10:18:11 +0200 (CEST) Date: Mon, 25 Sep 2017 10:18:11 +0200 From: Pavel Machek To: pali.rohar@gmail.com, sre@kernel.org, kernel list , linux-arm-kernel , linux-omap@vger.kernel.org, tony@atomide.com, khilman@kernel.org, aaro.koskinen@iki.fi, ivo.g.dimitrov.75@gmail.com, patrikbachan@gmail.com, serge@hallyn.com, abcloriens@gmail.com, clayton@craftyguy.net, martijn@brixit.nl, sakari.ailus@linux.intel.com Subject: bq27xxx: add battery protection Message-ID: <20170925081810.GA32216@amd> MIME-Version: 1.0 User-Agent: Mutt/1.5.23 (2014-03-12) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170925_011833_717413_BE7157BF X-CRM114-Status: GOOD ( 15.13 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Add battery protection for bq27xxx. Note that thresholds are very low, so this only kills system long after reasonable userspace should have reacted. Signed-off-by: Pavel Machek diff --git a/drivers/power/supply/bq27xxx_battery.c b/drivers/power/supply/bq27xxx_battery.c index 51f0961..08dc193 100644 --- a/drivers/power/supply/bq27xxx_battery.c +++ b/drivers/power/supply/bq27xxx_battery.c @@ -53,6 +53,7 @@ #include #include #include +#include #include #include @@ -1580,6 +1577,56 @@ void bq27xxx_battery_update(struct bq27xxx_device_info *di) } EXPORT_SYMBOL_GPL(bq27xxx_battery_update); +static void shutdown(char *reason) +{ + pr_alert("Forcing shutdown: %s\n", reason); + orderly_poweroff(true); +} + +static int generic_protect(struct power_supply *psy) +{ + union power_supply_propval val; + int res; + int mV, mA, mVadj = 0; + const int mOhm = 430; + const int mV_limit = 2950; + const int mV_open_limit = 3150; + + res = psy->desc->get_property(psy, POWER_SUPPLY_PROP_HEALTH, &val); + if (res) + return res; + + if (val.intval == POWER_SUPPLY_HEALTH_OVERHEAT) + shutdown("Battery overheat."); + if (val.intval == POWER_SUPPLY_HEALTH_DEAD) + shutdown("Battery dead."); + + res = psy->desc->get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW, &val); + if (res) + return res; + mV = val.intval / 1000; + + if (mV < mV_limit) { + pr_alert("Battery below %d mV.", mV_limit); + orderly_poweroff(true); + } + + res = psy->desc->get_property(psy, POWER_SUPPLY_PROP_CURRENT_NOW, &val); + if (res) + return res; + mA = val.intval / 1000; + mVadj = mV + (mA * mOhm) / 1000; + + if (mVadj < mV_open_limit) { + pr_alert("Battery open circuit voltage below %d mV.", mV_open_limit); + orderly_poweroff(true); + } + + printk(KERN_INFO "Main battery %d mV, open circuit voltage %d mV\n", + mV, mVadj); + return 0; +} + static void bq27xxx_battery_poll(struct work_struct *work) { struct bq27xxx_device_info *di = @@ -1592,6 +1639,16 @@ static void bq27xxx_battery_poll(struct work_struct *work) schedule_delayed_work(&di->work, poll_interval * HZ); } +static void bq27xxx_battery_poll_protect(struct work_struct *work) +{ + struct bq27xxx_device_info *di = + container_of(work, struct bq27xxx_device_info, + work.work); + + bq27xxx_battery_poll(work); + generic_protect(di->bat); +} + /* * Return the battery average current in µA * Note that current can be negative signed as well @@ -1684,8 +1741,8 @@ static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di, } /* - * Return the battery Voltage in millivolts - * Or < 0 if something fails. + * Set val->intval to the battery Voltage in millivolts. + * Return < 0 if something fails. */ static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di, union power_supply_propval *val) @@ -1821,7 +1878,7 @@ int bq27xxx_battery_setup(struct bq27xxx_device_info *di) .drv_data = di, }; - INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll); + INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll_protect); mutex_init(&di->lock); di->regs = bq27xxx_chip_data[di->chip].regs;