From patchwork Mon Oct 24 21:22:50 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Machek X-Patchwork-Id: 9393311 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 EB9976077A for ; Mon, 24 Oct 2016 21:24:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DD0C129126 for ; Mon, 24 Oct 2016 21:24:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D0B8A29140; Mon, 24 Oct 2016 21:24:31 +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, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 6655729126 for ; Mon, 24 Oct 2016 21:24:31 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1bymiG-0001Sv-Pc; Mon, 24 Oct 2016 21:23:20 +0000 Received: from atrey.karlin.mff.cuni.cz ([195.113.26.193]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1bymi9-0001Nx-Oa for linux-arm-kernel@lists.infradead.org; Mon, 24 Oct 2016 21:23:14 +0000 Received: by atrey.karlin.mff.cuni.cz (Postfix, from userid 512) id CF72B823CF; Mon, 24 Oct 2016 23:22:50 +0200 (CEST) Date: Mon, 24 Oct 2016 23:22:50 +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 Subject: [RFC] shutdown machine when li-ion battery goes below 3V Message-ID: <20161024212250.GA31336@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-20161024_142314_138084_E524DD3F X-CRM114-Status: GOOD ( 14.40 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 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 Hi! What about something like this? N900 will drain the battery down to system crash, which is quite uncool. Pavel diff --git a/drivers/power/supply/bq27xxx_battery.c b/drivers/power/supply/bq27xxx_battery.c index 0fe278b..8eb2f8f 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 @@ -740,6 +741,9 @@ void bq27xxx_battery_update(struct bq27xxx_device_info *di) } EXPORT_SYMBOL_GPL(bq27xxx_battery_update); +static int bq27xxx_battery_protect(struct bq27xxx_device_info *di); + + static void bq27xxx_battery_poll(struct work_struct *work) { struct bq27xxx_device_info *di = @@ -747,6 +751,7 @@ static void bq27xxx_battery_poll(struct work_struct *work) work.work); bq27xxx_battery_update(di); + bq27xxx_battery_protect(di); if (poll_interval > 0) schedule_delayed_work(&di->work, poll_interval * HZ); @@ -958,6 +963,41 @@ static int bq27xxx_battery_get_property(struct power_supply *psy, return ret; } +static int bq27xxx_battery_protect(struct bq27xxx_device_info *di) +{ + union power_supply_propval val; + int mV, mA, mOhm = 430, mVadj; + int res; + + printk(KERN_INFO "Main battery check\n"); + + res = bq27xxx_battery_voltage(di, &val); + if (res) + return res; + + mV = val.intval / 1000; + + if (mV < 2950) { + printk(KERN_ALERT "Main battery below 2.95V, forcing shutdown.\n"); + orderly_poweroff(true); + } + + res = bq27xxx_battery_current(di, &val); + if (res) + return res; + + mA = val.intval / 1000; + mVadj = mV + (mA * mOhm) / 1000; + + if (mVadj < 3150) { + printk(KERN_ALERT "Main battery internal voltage below 3.15, shutdown.\n"); + orderly_poweroff(true); + } + printk(KERN_INFO "Main battery %d mV, internal voltage %d mV\n", + mV, mVadj); + return 0; +} + static void bq27xxx_external_power_changed(struct power_supply *psy) { struct bq27xxx_device_info *di = power_supply_get_drvdata(psy); diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 226b0b4ac..bcdc1f8 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -444,7 +444,7 @@ static void handle_critical_trips(struct thermal_zone_device *tz, if (trip_type == THERMAL_TRIP_CRITICAL) { dev_emerg(&tz->device, - "critical temperature reached(%d C),shutting down\n", + "critical temperature reached(%d C), shutting down\n", tz->temperature / 1000); orderly_poweroff(true); }