@@ -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,
};
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(+)