platform/chrome: wilco_ec: Add Boot on AC support
authorNick Crews <ncrews@chromium.org>
Wed, 17 Apr 2019 01:20:47 +0000 (19:20 -0600)
committerEnric Balletbo i Serra <enric.balletbo@collabora.com>
Mon, 20 May 2019 08:18:09 +0000 (10:18 +0200)
Boot on AC is a policy which makes the device boot from S5 when AC
power is connected. This is useful for users who want to run their
device headless or with a dock.

Signed-off-by: Nick Crews <ncrews@chromium.org>
Signed-off-by: Enric Balletbo i Serra <enric.balletbo@collabora.com>
Documentation/ABI/testing/sysfs-platform-wilco-ec [new file with mode: 0644]
drivers/platform/chrome/wilco_ec/Makefile
drivers/platform/chrome/wilco_ec/core.c
drivers/platform/chrome/wilco_ec/sysfs.c [new file with mode: 0644]
include/linux/platform_data/wilco-ec.h

diff --git a/Documentation/ABI/testing/sysfs-platform-wilco-ec b/Documentation/ABI/testing/sysfs-platform-wilco-ec
new file mode 100644 (file)
index 0000000..8e5d6ee
--- /dev/null
@@ -0,0 +1,9 @@
+What:          /sys/bus/platform/devices/GOOG000C\:00/boot_on_ac
+Date:          April 2019
+KernelVersion: 5.3
+Description:
+               Boot on AC is a policy which makes the device boot from S5
+               when AC power is connected. This is useful for users who
+               want to run their device headless or with a dock.
+
+               Input should be parseable by kstrtou8() to 0 or 1.
index 29b7341..72df9b5 100644 (file)
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 
-wilco_ec-objs                          := core.o mailbox.o properties.o
+wilco_ec-objs                          := core.o mailbox.o properties.o sysfs.o
 obj-$(CONFIG_WILCO_EC)                 += wilco_ec.o
 wilco_ec_debugfs-objs                  := debugfs.o
 obj-$(CONFIG_WILCO_EC_DEBUGFS)         += wilco_ec_debugfs.o
index 05e1e2b..abd15d0 100644 (file)
@@ -89,8 +89,16 @@ static int wilco_ec_probe(struct platform_device *pdev)
                goto unregister_debugfs;
        }
 
+       ret = wilco_ec_add_sysfs(ec);
+       if (ret < 0) {
+               dev_err(dev, "Failed to create sysfs entries: %d", ret);
+               goto unregister_rtc;
+       }
+
        return 0;
 
+unregister_rtc:
+       platform_device_unregister(ec->rtc_pdev);
 unregister_debugfs:
        if (ec->debugfs_pdev)
                platform_device_unregister(ec->debugfs_pdev);
@@ -102,6 +110,7 @@ static int wilco_ec_remove(struct platform_device *pdev)
 {
        struct wilco_ec_device *ec = platform_get_drvdata(pdev);
 
+       wilco_ec_remove_sysfs(ec);
        platform_device_unregister(ec->rtc_pdev);
        if (ec->debugfs_pdev)
                platform_device_unregister(ec->debugfs_pdev);
diff --git a/drivers/platform/chrome/wilco_ec/sysfs.c b/drivers/platform/chrome/wilco_ec/sysfs.c
new file mode 100644 (file)
index 0000000..f84f048
--- /dev/null
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Sysfs properties to view and modify EC-controlled features on Wilco devices.
+ * The entries will appear under /sys/bus/platform/devices/GOOG000C:00/
+ *
+ * See Documentation/ABI/testing/sysfs-platform-wilco-ec for more information.
+ */
+
+#include <linux/platform_data/wilco-ec.h>
+#include <linux/sysfs.h>
+
+#define CMD_KB_CMOS                    0x7C
+#define SUB_CMD_KB_CMOS_AUTO_ON                0x03
+
+struct boot_on_ac_request {
+       u8 cmd;                 /* Always CMD_KB_CMOS */
+       u8 reserved1;
+       u8 sub_cmd;             /* Always SUB_CMD_KB_CMOS_AUTO_ON */
+       u8 reserved3to5[3];
+       u8 val;                 /* Either 0 or 1 */
+       u8 reserved7;
+} __packed;
+
+static ssize_t boot_on_ac_store(struct device *dev,
+                               struct device_attribute *attr,
+                               const char *buf, size_t count)
+{
+       struct wilco_ec_device *ec = dev_get_drvdata(dev);
+       struct boot_on_ac_request rq;
+       struct wilco_ec_message msg;
+       int ret;
+       u8 val;
+
+       ret = kstrtou8(buf, 10, &val);
+       if (ret < 0)
+               return ret;
+       if (val > 1)
+               return -EINVAL;
+
+       memset(&rq, 0, sizeof(rq));
+       rq.cmd = CMD_KB_CMOS;
+       rq.sub_cmd = SUB_CMD_KB_CMOS_AUTO_ON;
+       rq.val = val;
+
+       memset(&msg, 0, sizeof(msg));
+       msg.type = WILCO_EC_MSG_LEGACY;
+       msg.request_data = &rq;
+       msg.request_size = sizeof(rq);
+       ret = wilco_ec_mailbox(ec, &msg);
+       if (ret < 0)
+               return ret;
+
+       return count;
+}
+
+static DEVICE_ATTR_WO(boot_on_ac);
+
+static struct attribute *wilco_dev_attrs[] = {
+       &dev_attr_boot_on_ac.attr,
+       NULL,
+};
+
+static struct attribute_group wilco_dev_attr_group = {
+       .attrs = wilco_dev_attrs,
+};
+
+int wilco_ec_add_sysfs(struct wilco_ec_device *ec)
+{
+       return sysfs_create_group(&ec->dev->kobj, &wilco_dev_attr_group);
+}
+
+void wilco_ec_remove_sysfs(struct wilco_ec_device *ec)
+{
+       sysfs_remove_group(&ec->dev->kobj, &wilco_dev_attr_group);
+}
index 50a21bd..af68fc0 100644 (file)
@@ -194,4 +194,16 @@ int wilco_ec_get_byte_property(struct wilco_ec_device *ec, u32 property_id,
 int wilco_ec_set_byte_property(struct wilco_ec_device *ec, u32 property_id,
                               u8 val);
 
+/**
+ * wilco_ec_add_sysfs() - Create sysfs entries
+ * @ec: Wilco EC device
+ *
+ * wilco_ec_remove_sysfs() needs to be called afterwards
+ * to perform the necessary cleanup.
+ *
+ * Return: 0 on success or negative error code on failure.
+ */
+int wilco_ec_add_sysfs(struct wilco_ec_device *ec);
+void wilco_ec_remove_sysfs(struct wilco_ec_device *ec);
+
 #endif /* WILCO_EC_H */