Linux-PM Archive mirror
 help / color / mirror / Atom feed
From: "Thomas Weißschuh" <linux@weissschuh.net>
To: Lee Jones <lee@kernel.org>, Benson Leung <bleung@chromium.org>,
	 Guenter Roeck <groeck@chromium.org>,
	Tzung-Bi Shih <tzungbi@kernel.org>
Cc: linux-kernel@vger.kernel.org, chrome-platform@lists.linux.dev,
	"Mario Limonciello" <mario.limonciello@amd.com>,
	"Dustin L. Howett" <dustin@howett.net>,
	"Sebastian Reichel" <sre@kernel.org>,
	linux-pm@vger.kernel.org,
	"Thomas Weißschuh" <linux@weissschuh.net>
Subject: [PATCH 2/2] platform/chrome: cros_ec_framework_laptop: implement battery charge thresholds
Date: Sun, 05 May 2024 22:56:35 +0200	[thread overview]
Message-ID: <20240505-cros_ec-framework-v1-2-402662d6276b@weissschuh.net> (raw)
In-Reply-To: <20240505-cros_ec-framework-v1-0-402662d6276b@weissschuh.net>

Make use of the non-standard EC command FW_EC_CMD_CHARGE_LIMIT_CONTROL
to implement the standard sysfs attribute
"charge_control_end_threshold".

Tested on a Framework 13 AMD with firmware version 3.05.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 drivers/platform/chrome/cros_ec_framework_laptop.c | 120 +++++++++++++++++++++
 1 file changed, 120 insertions(+)

diff --git a/drivers/platform/chrome/cros_ec_framework_laptop.c b/drivers/platform/chrome/cros_ec_framework_laptop.c
index 8a8bf039fa9c..2693a80df38f 100644
--- a/drivers/platform/chrome/cros_ec_framework_laptop.c
+++ b/drivers/platform/chrome/cros_ec_framework_laptop.c
@@ -4,18 +4,119 @@
  *
  *  Copyright (C) 2024 Thomas Weißschuh <linux@weissschuh.net>
  */
+#include <acpi/battery.h>
 #include <linux/kernel.h>
 #include <linux/mod_devicetable.h>
 #include <linux/module.h>
+#include <linux/platform_data/cros_ec_commands.h>
 #include <linux/platform_data/cros_ec_proto.h>
 #include <linux/platform_device.h>
 
 #define DRV_NAME	"cros-ec-framework"
 
+#define FW_EC_CMD_CHARGE_LIMIT_CONTROL	0x3E03
+
+#define FW_EC_CHG_LIMIT_DISABLE		BIT(0)
+#define FW_EC_CHG_LIMIT_SET		BIT(1)
+#define FW_EC_CHG_LIMIT_GET		BIT(3)
+#define FW_EC_CHG_LIMIT_OVERRIDE	BIT(7)
+
 struct cros_fwk_priv {
 	struct cros_ec_device *cros_ec;
+	struct acpi_battery_hook battery_hook;
+	struct device_attribute end_threshold;
+};
+
+union cros_fwk_data {
+	struct {
+		u8 modes;
+		u8 max_percentage;
+		u8 min_percentage; /* not implemented in the EC */
+	} __packed req;
+	struct {
+		u8 max_percentage;
+		u8 min_percentage;
+	} __packed resp;
 };
 
+static int cros_fwk_send_cmd(struct cros_ec_device *cros_ec, union cros_fwk_data *arg)
+{
+	int ret;
+	struct {
+		struct cros_ec_command msg;
+		union cros_fwk_data data;
+	} __packed buf = {
+		.msg = {
+			.version = 0,
+			.command = FW_EC_CMD_CHARGE_LIMIT_CONTROL,
+			.insize  = sizeof(arg->resp),
+			.outsize = sizeof(arg->req),
+		},
+		.data.req = arg->req
+	};
+
+	ret = cros_ec_cmd_xfer_status(cros_ec, &buf.msg);
+	if (ret < 0)
+		return ret;
+
+	arg->resp = buf.data.resp;
+
+	return 0;
+}
+
+static ssize_t charge_control_end_threshold_show(struct device *dev, struct device_attribute *attr,
+						 char *buf)
+{
+	struct cros_fwk_priv *priv = container_of(attr, struct cros_fwk_priv, end_threshold);
+	union cros_fwk_data arg = { };
+	int ret;
+
+	arg.req.modes = FW_EC_CHG_LIMIT_GET;
+	ret = cros_fwk_send_cmd(priv->cros_ec, &arg);
+	if (ret < 0)
+		return ret;
+
+	return sysfs_emit(buf, "%u\n", (unsigned int)arg.resp.max_percentage);
+}
+
+static ssize_t charge_control_end_threshold_store(struct device *dev, struct device_attribute *attr,
+						  const char *buf, size_t count)
+{
+	struct cros_fwk_priv *priv = container_of(attr, struct cros_fwk_priv, end_threshold);
+	union cros_fwk_data arg = { };
+	int ret, val;
+
+	ret = kstrtoint(buf, 10, &val);
+	if (ret < 0)
+		return ret;
+	if (val < 1 || val > 100)
+		return -EINVAL;
+
+	arg.req.modes = FW_EC_CHG_LIMIT_SET;
+	arg.req.max_percentage = val;
+	ret = cros_fwk_send_cmd(priv->cros_ec, &arg);
+	if (ret < 0)
+		return ret;
+
+	return count;
+}
+
+static int cros_fwk_add_battery(struct power_supply *battery, struct acpi_battery_hook *hook)
+{
+	struct cros_fwk_priv *priv = container_of(hook, struct cros_fwk_priv, battery_hook);
+
+	return device_create_file(&battery->dev, &priv->end_threshold);
+}
+
+static int cros_fwk_remove_battery(struct power_supply *battery, struct acpi_battery_hook *hook)
+{
+	struct cros_fwk_priv *priv = container_of(hook, struct cros_fwk_priv, battery_hook);
+
+	device_remove_file(&battery->dev, &priv->end_threshold);
+
+	return 0;
+}
+
 static int cros_fwk_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
@@ -28,6 +129,24 @@ static int cros_fwk_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	priv->cros_ec = cros_ec;
+	priv->end_threshold = (struct device_attribute)__ATTR_RW(charge_control_end_threshold);
+
+	priv->battery_hook.name = dev_name(dev),
+	priv->battery_hook.add_battery = cros_fwk_add_battery,
+	priv->battery_hook.remove_battery = cros_fwk_remove_battery,
+
+	battery_hook_register(&priv->battery_hook);
+
+	platform_set_drvdata(pdev, priv);
+
+	return 0;
+}
+
+static int cros_fwk_remove(struct platform_device *pdev)
+{
+	struct cros_fwk_priv *priv = platform_get_drvdata(pdev);
+
+	battery_hook_unregister(&priv->battery_hook);
 
 	platform_set_drvdata(pdev, priv);
 
@@ -42,6 +161,7 @@ static const struct platform_device_id cros_fwk_id[] = {
 static struct platform_driver cros_fwk_driver = {
 	.driver.name	= DRV_NAME,
 	.probe		= cros_fwk_probe,
+	.remove		= cros_fwk_remove,
 	.id_table	= cros_fwk_id,
 };
 

-- 
2.45.0


  parent reply	other threads:[~2024-05-05 20:57 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-05 20:56 [PATCH 0/2] platform/chrome: cros_ec_framework_laptop: new driver Thomas Weißschuh
2024-05-05 20:56 ` [PATCH 1/2] platform/chrome: cros_ec_framework_laptop: introduce driver Thomas Weißschuh
2024-05-06 13:10   ` Limonciello, Mario
2024-05-06 17:48     ` Thomas Weißschuh
2024-05-07  8:25   ` Lee Jones
2024-05-05 20:56 ` Thomas Weißschuh [this message]
2024-05-06  6:09 ` [PATCH 0/2] platform/chrome: cros_ec_framework_laptop: new driver Thomas Weißschuh
2024-05-06 13:09   ` Limonciello, Mario
2024-05-06 17:43     ` Thomas Weißschuh
2024-05-06 18:29       ` Dustin Howett
2024-05-07  6:11         ` Thomas Weißschuh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240505-cros_ec-framework-v1-2-402662d6276b@weissschuh.net \
    --to=linux@weissschuh.net \
    --cc=bleung@chromium.org \
    --cc=chrome-platform@lists.linux.dev \
    --cc=dustin@howett.net \
    --cc=groeck@chromium.org \
    --cc=lee@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=mario.limonciello@amd.com \
    --cc=sre@kernel.org \
    --cc=tzungbi@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).