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: 9969421 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 3980860365 for ; Mon, 25 Sep 2017 08:18:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2F21228B67 for ; Mon, 25 Sep 2017 08:18:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 240A528C1E; Mon, 25 Sep 2017 08:18:36 +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=-6.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_TVD_MIME_EPI autolearn=unavailable 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 9E88528B67 for ; Mon, 25 Sep 2017 08:18:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934055AbdIYISP (ORCPT ); Mon, 25 Sep 2017 04:18:15 -0400 Received: from atrey.karlin.mff.cuni.cz ([195.113.26.193]:60686 "EHLO atrey.karlin.mff.cuni.cz" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933299AbdIYISN (ORCPT ); Mon, 25 Sep 2017 04:18:13 -0400 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 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-omap-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-omap@vger.kernel.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;