Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp5444370imb; Thu, 7 Mar 2019 16:13:07 -0800 (PST) X-Google-Smtp-Source: APXvYqzaQc9/n55rtdp5VnFgp05jipmmsPoX5SfJrrDTmwuq45fGCIVE5bLOKkfKsZBd1eKjCddE X-Received: by 2002:a17:902:20eb:: with SMTP id v40mr15867368plg.20.1552003987428; Thu, 07 Mar 2019 16:13:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1552003987; cv=none; d=google.com; s=arc-20160816; b=KSeI9+2w50VOfCqDErWXg/BFdud9t5uQBRR83SJkbmqzXWGwsarxMMmfwL/wL9Vz62 6VHVT2OlsRZieJYJAkR3+H1GhT0Dn39x9lksc8JRkzX2sffVb+AjJOtIePcpv/i5UzxE C04XepxwdijiKV0FGkl8xziJsj/5sWPq4REcbvUSBhXcoF8+x0HOXHFOeAuCxY0zaSlP 0cR3fWXNByCCatfZJu6TA/wtyQNVIgei+c8edVxWgMxylXXRJY8KgyitPcdSS1HfkeyL tVqhWzQRDPaVQvCcJaDc6ZWffnApE3mQ+Rk3oWIgZuZ5GnfPzUwjVu2hfaaxKJ0DaX1N uk/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=iaLKDxo0uwhel0Mf+OjeSn4ciEkuMwdbGhBvvoMu/xM=; b=QcZ8oAsCDgI68cgAA+EUPnqBWgxnYl6FBETUByDsZtHbm5yyCXBiRgQuEZKrIs/tQ8 z3ekJ57JRBA7NWAcjCHvMh49CDFlrRblldrR1m9kuZp//rjpho7fkh7NzHFaffs2XEjl 1D9InJe0PH/RLilC04uJyn9Ve+J074S6IJM3VQAnr+969F8Ss8rWhe6XakS2EZ5UxFW9 jfiqqvdA/cRkGuGnQeOgiQZAZTiBD3EpMlV7M94DxGPijfXbcd5hFqDJ+A5PVcaJ0saK G4opZlOwTSkHhLjY5wn2/rMvHSWx/KnGrgGFTmUyMh4TrEec2Felno3yuuXasiMBCBXY cpMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Tl2unvK6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f12si4951141pgf.184.2019.03.07.16.12.51; Thu, 07 Mar 2019 16:13:07 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Tl2unvK6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726298AbfCHAMH (ORCPT + 99 others); Thu, 7 Mar 2019 19:12:07 -0500 Received: from mail-it1-f195.google.com ([209.85.166.195]:35618 "EHLO mail-it1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726237AbfCHAMH (ORCPT ); Thu, 7 Mar 2019 19:12:07 -0500 Received: by mail-it1-f195.google.com with SMTP id 188so18865498itb.0 for ; Thu, 07 Mar 2019 16:12:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iaLKDxo0uwhel0Mf+OjeSn4ciEkuMwdbGhBvvoMu/xM=; b=Tl2unvK6BkzMftMiaCXmG3OGTBGFNn8XmV/28iFv5ZP3VntP3vcCEHBN9zm4YLSWZ8 MDc2wyoN3rfoFvImZ+uknUb4bzj1B/ZM8O1VI20RxvniKiUKM3Vs5MAgAbldD5677ET6 9L4Tbb8B78ioeJqcdVSP9hAvw7OG113vMxAwY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iaLKDxo0uwhel0Mf+OjeSn4ciEkuMwdbGhBvvoMu/xM=; b=PnDBHv6zLir20jKyUxGAyYCHEYUnxFaVi1FmY48om+i579O9/5gg1nYV1YNn+GwqTq OzqVWftnrWSdFLlv7FNn2W9g9z/2i9oYMKsIVsbbdYml0ABqp+I5rUHxYxDYQnihrTJu l/rfe4kzwSsdtOMjk5X2uIdWOkDeBsBfkFsA22H0E2d+UJp5NdpRds46MvGqUrQETDuF yN8WWDsLp1CMIHChqa0c+clvFu0pQ7ubvmFUgxbEGn0SbPfRUdylv7tQuUaHCzJumhpk iBGrebSkEIS6ofoGJuWqZLY2r7DwWxY/Iz1Ivb1EyAvmOeJmMrRffC3G8EiKFLB6U9TT UW/A== X-Gm-Message-State: APjAAAXVWD6an59e4iCQi/Mg3MWyuMPr5gtYAKf8BGjwSONiBa0Q0oMR 7JCrrW2hyieUBaX387JRBx36PQ== X-Received: by 2002:a24:7c0b:: with SMTP id a11mr6868462itd.161.1552003926268; Thu, 07 Mar 2019 16:12:06 -0800 (PST) Received: from ravisadineni0.mtv.corp.google.com ([2620:15c:202:1:98d2:1663:78dd:3593]) by smtp.gmail.com with ESMTPSA id 68sm3208434itk.40.2019.03.07.16.12.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 07 Mar 2019 16:12:05 -0800 (PST) From: RaviChandra Sadineni X-Google-Original-From: RaviChandra Sadineni To: ravisadineni@chromium.org Cc: Benson Leung , Enric Balletbo i Serra , Guenter Roeck , linux-kernel@vger.kernel.org Subject: [PATCH V4] cros_ec: Expose sysfile to force battery cutoff on shutdown. Date: Thu, 7 Mar 2019 16:11:59 -0800 Message-Id: <20190308001159.210832-1-ravisadineni@google.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <404dd7fe-23de-e5c4-48c2-5cd77a14e72b@collabora.com> References: <404dd7fe-23de-e5c4-48c2-5cd77a14e72b@collabora.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: RaviChandra Sadineni On chromebooks, power_manager daemon normally shuts down(S5) the device when the battery charge falls below 4% threshold. ChromeOS EC then normally spends an hour in S5 before hibernating. If the battery charge falls below critical threshold in the mean time, EC does a battery cutoff instead of hibernating. On some chromebooks, S5 is optimal enough resulting in EC hibernating without battery cutoff. This results in battery deep discharging. This is a bad user experience as battery has to trickle charge before booting when the AC is plugged in the next time. This patch exposes a sysfs file for an userland daemon to suggest EC if it has to do a battery cutoff instead of hibernating when the system enters S5 next time. Signed-off-by: RaviChandra Sadineni --- V4: Addressed comments from Enric. V3: Make battery-cutoff generic and expose 'at-shutdown' flag. V2: Use kstrtobool() instead of kstrtou8() and add documentation. .../ABI/testing/sysfs-class-chromeos | 16 +++++++ drivers/platform/chrome/cros_ec_sysfs.c | 47 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-class-chromeos b/Documentation/ABI/testing/sysfs-class-chromeos index 5819699d66ec..0927704d1629 100644 --- a/Documentation/ABI/testing/sysfs-class-chromeos +++ b/Documentation/ABI/testing/sysfs-class-chromeos @@ -30,3 +30,19 @@ Date: August 2015 KernelVersion: 4.2 Description: Show the information about the EC software and hardware. + +What: /sys/class/chromeos//battery_cuttoff +Date: February 2019 +Contact: Ravi Chandra Sadineni +Description: + cros_ec battery cuttoff configuration. Only option + currently exposed is 'at-shutdown'. + + 'at-shutdown' sends a host command to EC requesting + battery cutoff on next shutdown. If AC is plugged + in before next shutdown, EC ignores the request and + resets the flag. + + Currently EC does not expose a host command to read + the status of battery cutoff configuration. Thus this + flag is write-only. diff --git a/drivers/platform/chrome/cros_ec_sysfs.c b/drivers/platform/chrome/cros_ec_sysfs.c index fe0b7614ae1b..a35e1188f28f 100644 --- a/drivers/platform/chrome/cros_ec_sysfs.c +++ b/drivers/platform/chrome/cros_ec_sysfs.c @@ -308,14 +308,61 @@ static ssize_t kb_wake_angle_store(struct device *dev, return count; } +/* Battery cutoff control */ +static ssize_t battery_cutoff_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct ec_params_battery_cutoff *param; + struct cros_ec_command *msg; + int ret; + struct cros_ec_dev *ec = to_cros_ec_dev(dev); + char *p; + int len; + + msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL); + if (!msg) + return -ENOMEM; + + param = (struct ec_params_battery_cutoff *)msg->data; + msg->command = EC_CMD_BATTERY_CUT_OFF + ec->cmd_offset; + msg->version = 1; + msg->outsize = sizeof(*param); + msg->insize = 0; + + p = memchr(buf, '\n', count); + len = p ? p - buf : count; + + if (len == 11 && !strncmp(buf, "at-shutdown", len)) { + param->flags = EC_BATTERY_CUTOFF_FLAG_AT_SHUTDOWN; + } else { + count = -EINVAL; + goto exit; + } + + ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg); + if (ret < 0) + count = ret; +exit: + kfree(msg); + return count; +} + /* Module initialization */ static DEVICE_ATTR_RW(reboot); static DEVICE_ATTR_RO(version); static DEVICE_ATTR_RO(flashinfo); static DEVICE_ATTR_RW(kb_wake_angle); +/* + * Currently EC does not expose a host command to read the status of + * battery cutoff configuration. Also there is no requirement to read + * the flag from userland. So marking this attribute as write-only. + */ +static DEVICE_ATTR_WO(battery_cutoff); static struct attribute *__ec_attrs[] = { + &dev_attr_battery_cutoff.attr, &dev_attr_kb_wake_angle.attr, &dev_attr_reboot.attr, &dev_attr_version.attr, -- 2.20.1