18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci// Expose the ChromeOS EC through sysfs
38c2ecf20Sopenharmony_ci//
48c2ecf20Sopenharmony_ci// Copyright (C) 2014 Google, Inc.
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/ctype.h>
78c2ecf20Sopenharmony_ci#include <linux/delay.h>
88c2ecf20Sopenharmony_ci#include <linux/device.h>
98c2ecf20Sopenharmony_ci#include <linux/fs.h>
108c2ecf20Sopenharmony_ci#include <linux/kobject.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/platform_data/cros_ec_commands.h>
138c2ecf20Sopenharmony_ci#include <linux/platform_data/cros_ec_proto.h>
148c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
158c2ecf20Sopenharmony_ci#include <linux/printk.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include <linux/stat.h>
188c2ecf20Sopenharmony_ci#include <linux/types.h>
198c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define DRV_NAME "cros-ec-sysfs"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* Accessor functions */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic ssize_t reboot_show(struct device *dev,
268c2ecf20Sopenharmony_ci			   struct device_attribute *attr, char *buf)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	int count = 0;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	count += scnprintf(buf + count, PAGE_SIZE - count,
318c2ecf20Sopenharmony_ci			   "ro|rw|cancel|cold|disable-jump|hibernate");
328c2ecf20Sopenharmony_ci	count += scnprintf(buf + count, PAGE_SIZE - count,
338c2ecf20Sopenharmony_ci			   " [at-shutdown]\n");
348c2ecf20Sopenharmony_ci	return count;
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic ssize_t reboot_store(struct device *dev,
388c2ecf20Sopenharmony_ci			    struct device_attribute *attr,
398c2ecf20Sopenharmony_ci			    const char *buf, size_t count)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	static const struct {
428c2ecf20Sopenharmony_ci		const char * const str;
438c2ecf20Sopenharmony_ci		uint8_t cmd;
448c2ecf20Sopenharmony_ci		uint8_t flags;
458c2ecf20Sopenharmony_ci	} words[] = {
468c2ecf20Sopenharmony_ci		{"cancel",       EC_REBOOT_CANCEL, 0},
478c2ecf20Sopenharmony_ci		{"ro",           EC_REBOOT_JUMP_RO, 0},
488c2ecf20Sopenharmony_ci		{"rw",           EC_REBOOT_JUMP_RW, 0},
498c2ecf20Sopenharmony_ci		{"cold",         EC_REBOOT_COLD, 0},
508c2ecf20Sopenharmony_ci		{"disable-jump", EC_REBOOT_DISABLE_JUMP, 0},
518c2ecf20Sopenharmony_ci		{"hibernate",    EC_REBOOT_HIBERNATE, 0},
528c2ecf20Sopenharmony_ci		{"at-shutdown",  -1, EC_REBOOT_FLAG_ON_AP_SHUTDOWN},
538c2ecf20Sopenharmony_ci	};
548c2ecf20Sopenharmony_ci	struct cros_ec_command *msg;
558c2ecf20Sopenharmony_ci	struct ec_params_reboot_ec *param;
568c2ecf20Sopenharmony_ci	int got_cmd = 0, offset = 0;
578c2ecf20Sopenharmony_ci	int i;
588c2ecf20Sopenharmony_ci	int ret;
598c2ecf20Sopenharmony_ci	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	msg = kmalloc(sizeof(*msg) + sizeof(*param), GFP_KERNEL);
628c2ecf20Sopenharmony_ci	if (!msg)
638c2ecf20Sopenharmony_ci		return -ENOMEM;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	param = (struct ec_params_reboot_ec *)msg->data;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	param->flags = 0;
688c2ecf20Sopenharmony_ci	while (1) {
698c2ecf20Sopenharmony_ci		/* Find word to start scanning */
708c2ecf20Sopenharmony_ci		while (buf[offset] && isspace(buf[offset]))
718c2ecf20Sopenharmony_ci			offset++;
728c2ecf20Sopenharmony_ci		if (!buf[offset])
738c2ecf20Sopenharmony_ci			break;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(words); i++) {
768c2ecf20Sopenharmony_ci			if (!strncasecmp(words[i].str, buf+offset,
778c2ecf20Sopenharmony_ci					 strlen(words[i].str))) {
788c2ecf20Sopenharmony_ci				if (words[i].flags) {
798c2ecf20Sopenharmony_ci					param->flags |= words[i].flags;
808c2ecf20Sopenharmony_ci				} else {
818c2ecf20Sopenharmony_ci					param->cmd = words[i].cmd;
828c2ecf20Sopenharmony_ci					got_cmd = 1;
838c2ecf20Sopenharmony_ci				}
848c2ecf20Sopenharmony_ci				break;
858c2ecf20Sopenharmony_ci			}
868c2ecf20Sopenharmony_ci		}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci		/* On to the next word, if any */
898c2ecf20Sopenharmony_ci		while (buf[offset] && !isspace(buf[offset]))
908c2ecf20Sopenharmony_ci			offset++;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	if (!got_cmd) {
948c2ecf20Sopenharmony_ci		count = -EINVAL;
958c2ecf20Sopenharmony_ci		goto exit;
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	msg->version = 0;
998c2ecf20Sopenharmony_ci	msg->command = EC_CMD_REBOOT_EC + ec->cmd_offset;
1008c2ecf20Sopenharmony_ci	msg->outsize = sizeof(*param);
1018c2ecf20Sopenharmony_ci	msg->insize = 0;
1028c2ecf20Sopenharmony_ci	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
1038c2ecf20Sopenharmony_ci	if (ret < 0)
1048c2ecf20Sopenharmony_ci		count = ret;
1058c2ecf20Sopenharmony_ciexit:
1068c2ecf20Sopenharmony_ci	kfree(msg);
1078c2ecf20Sopenharmony_ci	return count;
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic ssize_t version_show(struct device *dev,
1118c2ecf20Sopenharmony_ci			    struct device_attribute *attr, char *buf)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	static const char * const image_names[] = {"unknown", "RO", "RW"};
1148c2ecf20Sopenharmony_ci	struct ec_response_get_version *r_ver;
1158c2ecf20Sopenharmony_ci	struct ec_response_get_chip_info *r_chip;
1168c2ecf20Sopenharmony_ci	struct ec_response_board_version *r_board;
1178c2ecf20Sopenharmony_ci	struct cros_ec_command *msg;
1188c2ecf20Sopenharmony_ci	int ret;
1198c2ecf20Sopenharmony_ci	int count = 0;
1208c2ecf20Sopenharmony_ci	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
1238c2ecf20Sopenharmony_ci	if (!msg)
1248c2ecf20Sopenharmony_ci		return -ENOMEM;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	/* Get versions. RW may change. */
1278c2ecf20Sopenharmony_ci	msg->version = 0;
1288c2ecf20Sopenharmony_ci	msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
1298c2ecf20Sopenharmony_ci	msg->insize = sizeof(*r_ver);
1308c2ecf20Sopenharmony_ci	msg->outsize = 0;
1318c2ecf20Sopenharmony_ci	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
1328c2ecf20Sopenharmony_ci	if (ret < 0) {
1338c2ecf20Sopenharmony_ci		count = ret;
1348c2ecf20Sopenharmony_ci		goto exit;
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci	r_ver = (struct ec_response_get_version *)msg->data;
1378c2ecf20Sopenharmony_ci	/* Strings should be null-terminated, but let's be sure. */
1388c2ecf20Sopenharmony_ci	r_ver->version_string_ro[sizeof(r_ver->version_string_ro) - 1] = '\0';
1398c2ecf20Sopenharmony_ci	r_ver->version_string_rw[sizeof(r_ver->version_string_rw) - 1] = '\0';
1408c2ecf20Sopenharmony_ci	count += scnprintf(buf + count, PAGE_SIZE - count,
1418c2ecf20Sopenharmony_ci			   "RO version:    %s\n", r_ver->version_string_ro);
1428c2ecf20Sopenharmony_ci	count += scnprintf(buf + count, PAGE_SIZE - count,
1438c2ecf20Sopenharmony_ci			   "RW version:    %s\n", r_ver->version_string_rw);
1448c2ecf20Sopenharmony_ci	count += scnprintf(buf + count, PAGE_SIZE - count,
1458c2ecf20Sopenharmony_ci			   "Firmware copy: %s\n",
1468c2ecf20Sopenharmony_ci			   (r_ver->current_image < ARRAY_SIZE(image_names) ?
1478c2ecf20Sopenharmony_ci			    image_names[r_ver->current_image] : "?"));
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	/* Get build info. */
1508c2ecf20Sopenharmony_ci	msg->command = EC_CMD_GET_BUILD_INFO + ec->cmd_offset;
1518c2ecf20Sopenharmony_ci	msg->insize = EC_HOST_PARAM_SIZE;
1528c2ecf20Sopenharmony_ci	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
1538c2ecf20Sopenharmony_ci	if (ret < 0) {
1548c2ecf20Sopenharmony_ci		count += scnprintf(buf + count, PAGE_SIZE - count,
1558c2ecf20Sopenharmony_ci				   "Build info:    XFER / EC ERROR %d / %d\n",
1568c2ecf20Sopenharmony_ci				   ret, msg->result);
1578c2ecf20Sopenharmony_ci	} else {
1588c2ecf20Sopenharmony_ci		msg->data[EC_HOST_PARAM_SIZE - 1] = '\0';
1598c2ecf20Sopenharmony_ci		count += scnprintf(buf + count, PAGE_SIZE - count,
1608c2ecf20Sopenharmony_ci				   "Build info:    %s\n", msg->data);
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	/* Get chip info. */
1648c2ecf20Sopenharmony_ci	msg->command = EC_CMD_GET_CHIP_INFO + ec->cmd_offset;
1658c2ecf20Sopenharmony_ci	msg->insize = sizeof(*r_chip);
1668c2ecf20Sopenharmony_ci	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
1678c2ecf20Sopenharmony_ci	if (ret < 0) {
1688c2ecf20Sopenharmony_ci		count += scnprintf(buf + count, PAGE_SIZE - count,
1698c2ecf20Sopenharmony_ci				   "Chip info:     XFER / EC ERROR %d / %d\n",
1708c2ecf20Sopenharmony_ci				   ret, msg->result);
1718c2ecf20Sopenharmony_ci	} else {
1728c2ecf20Sopenharmony_ci		r_chip = (struct ec_response_get_chip_info *)msg->data;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci		r_chip->vendor[sizeof(r_chip->vendor) - 1] = '\0';
1758c2ecf20Sopenharmony_ci		r_chip->name[sizeof(r_chip->name) - 1] = '\0';
1768c2ecf20Sopenharmony_ci		r_chip->revision[sizeof(r_chip->revision) - 1] = '\0';
1778c2ecf20Sopenharmony_ci		count += scnprintf(buf + count, PAGE_SIZE - count,
1788c2ecf20Sopenharmony_ci				   "Chip vendor:   %s\n", r_chip->vendor);
1798c2ecf20Sopenharmony_ci		count += scnprintf(buf + count, PAGE_SIZE - count,
1808c2ecf20Sopenharmony_ci				   "Chip name:     %s\n", r_chip->name);
1818c2ecf20Sopenharmony_ci		count += scnprintf(buf + count, PAGE_SIZE - count,
1828c2ecf20Sopenharmony_ci				   "Chip revision: %s\n", r_chip->revision);
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	/* Get board version */
1868c2ecf20Sopenharmony_ci	msg->command = EC_CMD_GET_BOARD_VERSION + ec->cmd_offset;
1878c2ecf20Sopenharmony_ci	msg->insize = sizeof(*r_board);
1888c2ecf20Sopenharmony_ci	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
1898c2ecf20Sopenharmony_ci	if (ret < 0) {
1908c2ecf20Sopenharmony_ci		count += scnprintf(buf + count, PAGE_SIZE - count,
1918c2ecf20Sopenharmony_ci				   "Board version: XFER / EC ERROR %d / %d\n",
1928c2ecf20Sopenharmony_ci				   ret, msg->result);
1938c2ecf20Sopenharmony_ci	} else {
1948c2ecf20Sopenharmony_ci		r_board = (struct ec_response_board_version *)msg->data;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci		count += scnprintf(buf + count, PAGE_SIZE - count,
1978c2ecf20Sopenharmony_ci				   "Board version: %d\n",
1988c2ecf20Sopenharmony_ci				   r_board->board_version);
1998c2ecf20Sopenharmony_ci	}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ciexit:
2028c2ecf20Sopenharmony_ci	kfree(msg);
2038c2ecf20Sopenharmony_ci	return count;
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic ssize_t flashinfo_show(struct device *dev,
2078c2ecf20Sopenharmony_ci			      struct device_attribute *attr, char *buf)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	struct ec_response_flash_info *resp;
2108c2ecf20Sopenharmony_ci	struct cros_ec_command *msg;
2118c2ecf20Sopenharmony_ci	int ret;
2128c2ecf20Sopenharmony_ci	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
2158c2ecf20Sopenharmony_ci	if (!msg)
2168c2ecf20Sopenharmony_ci		return -ENOMEM;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	/* The flash info shouldn't ever change, but ask each time anyway. */
2198c2ecf20Sopenharmony_ci	msg->version = 0;
2208c2ecf20Sopenharmony_ci	msg->command = EC_CMD_FLASH_INFO + ec->cmd_offset;
2218c2ecf20Sopenharmony_ci	msg->insize = sizeof(*resp);
2228c2ecf20Sopenharmony_ci	msg->outsize = 0;
2238c2ecf20Sopenharmony_ci	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
2248c2ecf20Sopenharmony_ci	if (ret < 0)
2258c2ecf20Sopenharmony_ci		goto exit;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	resp = (struct ec_response_flash_info *)msg->data;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	ret = scnprintf(buf, PAGE_SIZE,
2308c2ecf20Sopenharmony_ci			"FlashSize %d\nWriteSize %d\n"
2318c2ecf20Sopenharmony_ci			"EraseSize %d\nProtectSize %d\n",
2328c2ecf20Sopenharmony_ci			resp->flash_size, resp->write_block_size,
2338c2ecf20Sopenharmony_ci			resp->erase_block_size, resp->protect_block_size);
2348c2ecf20Sopenharmony_ciexit:
2358c2ecf20Sopenharmony_ci	kfree(msg);
2368c2ecf20Sopenharmony_ci	return ret;
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci/* Keyboard wake angle control */
2408c2ecf20Sopenharmony_cistatic ssize_t kb_wake_angle_show(struct device *dev,
2418c2ecf20Sopenharmony_ci				  struct device_attribute *attr, char *buf)
2428c2ecf20Sopenharmony_ci{
2438c2ecf20Sopenharmony_ci	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
2448c2ecf20Sopenharmony_ci	struct ec_response_motion_sense *resp;
2458c2ecf20Sopenharmony_ci	struct ec_params_motion_sense *param;
2468c2ecf20Sopenharmony_ci	struct cros_ec_command *msg;
2478c2ecf20Sopenharmony_ci	int ret;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
2508c2ecf20Sopenharmony_ci	if (!msg)
2518c2ecf20Sopenharmony_ci		return -ENOMEM;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	param = (struct ec_params_motion_sense *)msg->data;
2548c2ecf20Sopenharmony_ci	msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
2558c2ecf20Sopenharmony_ci	msg->version = 2;
2568c2ecf20Sopenharmony_ci	param->cmd = MOTIONSENSE_CMD_KB_WAKE_ANGLE;
2578c2ecf20Sopenharmony_ci	param->kb_wake_angle.data = EC_MOTION_SENSE_NO_VALUE;
2588c2ecf20Sopenharmony_ci	msg->outsize = sizeof(*param);
2598c2ecf20Sopenharmony_ci	msg->insize = sizeof(*resp);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
2628c2ecf20Sopenharmony_ci	if (ret < 0)
2638c2ecf20Sopenharmony_ci		goto exit;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	resp = (struct ec_response_motion_sense *)msg->data;
2668c2ecf20Sopenharmony_ci	ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->kb_wake_angle.ret);
2678c2ecf20Sopenharmony_ciexit:
2688c2ecf20Sopenharmony_ci	kfree(msg);
2698c2ecf20Sopenharmony_ci	return ret;
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic ssize_t kb_wake_angle_store(struct device *dev,
2738c2ecf20Sopenharmony_ci				   struct device_attribute *attr,
2748c2ecf20Sopenharmony_ci				   const char *buf, size_t count)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
2778c2ecf20Sopenharmony_ci	struct ec_params_motion_sense *param;
2788c2ecf20Sopenharmony_ci	struct cros_ec_command *msg;
2798c2ecf20Sopenharmony_ci	u16 angle;
2808c2ecf20Sopenharmony_ci	int ret;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	ret = kstrtou16(buf, 0, &angle);
2838c2ecf20Sopenharmony_ci	if (ret)
2848c2ecf20Sopenharmony_ci		return ret;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
2878c2ecf20Sopenharmony_ci	if (!msg)
2888c2ecf20Sopenharmony_ci		return -ENOMEM;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	param = (struct ec_params_motion_sense *)msg->data;
2918c2ecf20Sopenharmony_ci	msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
2928c2ecf20Sopenharmony_ci	msg->version = 2;
2938c2ecf20Sopenharmony_ci	param->cmd = MOTIONSENSE_CMD_KB_WAKE_ANGLE;
2948c2ecf20Sopenharmony_ci	param->kb_wake_angle.data = angle;
2958c2ecf20Sopenharmony_ci	msg->outsize = sizeof(*param);
2968c2ecf20Sopenharmony_ci	msg->insize = sizeof(struct ec_response_motion_sense);
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
2998c2ecf20Sopenharmony_ci	kfree(msg);
3008c2ecf20Sopenharmony_ci	if (ret < 0)
3018c2ecf20Sopenharmony_ci		return ret;
3028c2ecf20Sopenharmony_ci	return count;
3038c2ecf20Sopenharmony_ci}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci/* Module initialization */
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(reboot);
3088c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(version);
3098c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(flashinfo);
3108c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(kb_wake_angle);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_cistatic struct attribute *__ec_attrs[] = {
3138c2ecf20Sopenharmony_ci	&dev_attr_kb_wake_angle.attr,
3148c2ecf20Sopenharmony_ci	&dev_attr_reboot.attr,
3158c2ecf20Sopenharmony_ci	&dev_attr_version.attr,
3168c2ecf20Sopenharmony_ci	&dev_attr_flashinfo.attr,
3178c2ecf20Sopenharmony_ci	NULL,
3188c2ecf20Sopenharmony_ci};
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_cistatic umode_t cros_ec_ctrl_visible(struct kobject *kobj,
3218c2ecf20Sopenharmony_ci				    struct attribute *a, int n)
3228c2ecf20Sopenharmony_ci{
3238c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
3248c2ecf20Sopenharmony_ci	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	if (a == &dev_attr_kb_wake_angle.attr && !ec->has_kb_wake_angle)
3278c2ecf20Sopenharmony_ci		return 0;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	return a->mode;
3308c2ecf20Sopenharmony_ci}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_cistatic struct attribute_group cros_ec_attr_group = {
3338c2ecf20Sopenharmony_ci	.attrs = __ec_attrs,
3348c2ecf20Sopenharmony_ci	.is_visible = cros_ec_ctrl_visible,
3358c2ecf20Sopenharmony_ci};
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_cistatic int cros_ec_sysfs_probe(struct platform_device *pd)
3388c2ecf20Sopenharmony_ci{
3398c2ecf20Sopenharmony_ci	struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
3408c2ecf20Sopenharmony_ci	struct device *dev = &pd->dev;
3418c2ecf20Sopenharmony_ci	int ret;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	ret = sysfs_create_group(&ec_dev->class_dev.kobj, &cros_ec_attr_group);
3448c2ecf20Sopenharmony_ci	if (ret < 0)
3458c2ecf20Sopenharmony_ci		dev_err(dev, "failed to create attributes. err=%d\n", ret);
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	return ret;
3488c2ecf20Sopenharmony_ci}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_cistatic int cros_ec_sysfs_remove(struct platform_device *pd)
3518c2ecf20Sopenharmony_ci{
3528c2ecf20Sopenharmony_ci	struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	sysfs_remove_group(&ec_dev->class_dev.kobj, &cros_ec_attr_group);
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	return 0;
3578c2ecf20Sopenharmony_ci}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_cistatic struct platform_driver cros_ec_sysfs_driver = {
3608c2ecf20Sopenharmony_ci	.driver = {
3618c2ecf20Sopenharmony_ci		.name = DRV_NAME,
3628c2ecf20Sopenharmony_ci	},
3638c2ecf20Sopenharmony_ci	.probe = cros_ec_sysfs_probe,
3648c2ecf20Sopenharmony_ci	.remove = cros_ec_sysfs_remove,
3658c2ecf20Sopenharmony_ci};
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_cimodule_platform_driver(cros_ec_sysfs_driver);
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
3708c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Expose the ChromeOS EC through sysfs");
3718c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:" DRV_NAME);
372