From patchwork Tue Oct 25 11:27:57 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Machek X-Patchwork-Id: 9394363 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 C7BD860762 for ; Tue, 25 Oct 2016 11:28:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B397A2952F for ; Tue, 25 Oct 2016 11:28:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A7A6029531; Tue, 25 Oct 2016 11:28:19 +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 53A7A2952F for ; Tue, 25 Oct 2016 11:28:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S941505AbcJYL2C (ORCPT ); Tue, 25 Oct 2016 07:28:02 -0400 Received: from atrey.karlin.mff.cuni.cz ([195.113.26.193]:51256 "EHLO atrey.karlin.mff.cuni.cz" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755604AbcJYL2C (ORCPT ); Tue, 25 Oct 2016 07:28:02 -0400 Received: by atrey.karlin.mff.cuni.cz (Postfix, from userid 512) id 880EA823AD; Tue, 25 Oct 2016 13:27:58 +0200 (CEST) Date: Tue, 25 Oct 2016 13:27:57 +0200 From: Pavel Machek To: Tony Lindgren Cc: pali.rohar@gmail.com, sre@kernel.org, kernel list , linux-arm-kernel , linux-omap@vger.kernel.org, khilman@kernel.org, aaro.koskinen@iki.fi, ivo.g.dimitrov.75@gmail.com, patrikbachan@gmail.com, serge@hallyn.com, abcloriens@gmail.com Subject: Re: [RFC] shutdown machine when li-ion battery goes below 3V Message-ID: <20161025112757.GC25855@amd> References: <20161024212250.GA31336@amd> <20161024212932.uhjz752z2cy5hohl@atomide.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20161024212932.uhjz752z2cy5hohl@atomide.com> 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 On Mon 2016-10-24 14:29:33, Tony Lindgren wrote: > * Pavel Machek [161024 14:24]: > > Hi! > > > > What about something like this? N900 will drain the battery down to > > system crash, which is quite uncool. > > Can't we make that generic and configurable for the voltage somehow? > > Also, the shutdown voltage can depend on external devices connected. > It could be for example 3.3V depending on eMMC on some devices while > devices with no eMMC could have it at 3.0V. Actually, do we need to make it configurable? It looks like we should respect hardware telling us battery is dead, and only use (low) hardcoded voltages as a fallback. Currently patch looks like this. generic_protect() should work for other batteries drivers, too. Pavel diff --git a/drivers/power/supply/bq27xxx_battery.c b/drivers/power/supply/bq27xxx_battery.c index 0fe278b..04094ad 100644 --- a/drivers/power/supply/bq27xxx_battery.c +++ b/drivers/power/supply/bq27xxx_battery.c @@ -46,6 +46,7 @@ #include #include #include +#include #include #include @@ -679,10 +680,10 @@ static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di) /* Unlikely but important to return first */ if (unlikely(bq27xxx_battery_overtemp(di, flags))) return POWER_SUPPLY_HEALTH_OVERHEAT; - if (unlikely(bq27xxx_battery_undertemp(di, flags))) - return POWER_SUPPLY_HEALTH_COLD; if (unlikely(bq27xxx_battery_dead(di, flags))) return POWER_SUPPLY_HEALTH_DEAD; + if (unlikely(bq27xxx_battery_undertemp(di, flags))) + return POWER_SUPPLY_HEALTH_COLD; return POWER_SUPPLY_HEALTH_GOOD; } @@ -740,6 +741,49 @@ void bq27xxx_battery_update(struct bq27xxx_device_info *di) } EXPORT_SYMBOL_GPL(bq27xxx_battery_update); +static void shutdown(char *reason) +{ + pr_alert("%s Forcing shutdown\n", reason); + orderly_poweroff(true); +} + +static int generic_protect(struct power_supply *psy) +{ + union power_supply_propval val; + int res; + int mV, mA, mOhm = 430, mVadj = 0; + + 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 < 2950) + shutdown("Battery below 2.95V."); + + 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 < 3150) + shutdown("Battery internal voltage below 3.15."); + + printk(KERN_INFO "Main battery %d mV, internal voltage %d mV\n", + mV, mVadj); + return 0; +} + static void bq27xxx_battery_poll(struct work_struct *work) { struct bq27xxx_device_info *di = @@ -747,6 +791,7 @@ static void bq27xxx_battery_poll(struct work_struct *work) work.work); bq27xxx_battery_update(di); + generic_protect(di->bat); if (poll_interval > 0) schedule_delayed_work(&di->work, poll_interval * HZ);