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: 9393309 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 5A9DE60762 for ; Mon, 24 Oct 2016 21:23:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4CB6229126 for ; Mon, 24 Oct 2016 21:23:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4160329140; Mon, 24 Oct 2016 21:23:47 +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 EEB5129126 for ; Mon, 24 Oct 2016 21:23:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757366AbcJXVWz (ORCPT ); Mon, 24 Oct 2016 17:22:55 -0400 Received: from atrey.karlin.mff.cuni.cz ([195.113.26.193]:36865 "EHLO atrey.karlin.mff.cuni.cz" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757329AbcJXVWz (ORCPT ); Mon, 24 Oct 2016 17:22:55 -0400 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 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 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); }