xref: /linux/drivers/platform/chrome/wilco_ec/sysfs.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
14c1ca625SNick Crews // SPDX-License-Identifier: GPL-2.0
24c1ca625SNick Crews /*
34c1ca625SNick Crews  * Copyright 2019 Google LLC
44c1ca625SNick Crews  *
54c1ca625SNick Crews  * Sysfs properties to view and modify EC-controlled features on Wilco devices.
64c1ca625SNick Crews  * The entries will appear under /sys/bus/platform/devices/GOOG000C:00/
74c1ca625SNick Crews  *
84c1ca625SNick Crews  * See Documentation/ABI/testing/sysfs-platform-wilco-ec for more information.
94c1ca625SNick Crews  */
104c1ca625SNick Crews 
118673e944SAndy Shevchenko #include <linux/device.h>
128673e944SAndy Shevchenko #include <linux/kernel.h>
134c1ca625SNick Crews #include <linux/platform_data/wilco-ec.h>
148673e944SAndy Shevchenko #include <linux/string.h>
154c1ca625SNick Crews #include <linux/sysfs.h>
168673e944SAndy Shevchenko #include <linux/types.h>
174c1ca625SNick Crews 
184c1ca625SNick Crews #define CMD_KB_CMOS			0x7C
194c1ca625SNick Crews #define SUB_CMD_KB_CMOS_AUTO_ON		0x03
204c1ca625SNick Crews 
214c1ca625SNick Crews struct boot_on_ac_request {
224c1ca625SNick Crews 	u8 cmd;			/* Always CMD_KB_CMOS */
234c1ca625SNick Crews 	u8 reserved1;
244c1ca625SNick Crews 	u8 sub_cmd;		/* Always SUB_CMD_KB_CMOS_AUTO_ON */
254c1ca625SNick Crews 	u8 reserved3to5[3];
264c1ca625SNick Crews 	u8 val;			/* Either 0 or 1 */
274c1ca625SNick Crews 	u8 reserved7;
284c1ca625SNick Crews } __packed;
294c1ca625SNick Crews 
30fdf0fe2dSDaniel Campello #define CMD_USB_CHARGE 0x39
31fdf0fe2dSDaniel Campello 
32fdf0fe2dSDaniel Campello enum usb_charge_op {
33fdf0fe2dSDaniel Campello 	USB_CHARGE_GET = 0,
34fdf0fe2dSDaniel Campello 	USB_CHARGE_SET = 1,
35fdf0fe2dSDaniel Campello };
36fdf0fe2dSDaniel Campello 
37fdf0fe2dSDaniel Campello struct usb_charge_request {
38fdf0fe2dSDaniel Campello 	u8 cmd;		/* Always CMD_USB_CHARGE */
39fdf0fe2dSDaniel Campello 	u8 reserved;
40fdf0fe2dSDaniel Campello 	u8 op;		/* One of enum usb_charge_op */
41fdf0fe2dSDaniel Campello 	u8 val;		/* When setting, either 0 or 1 */
42fdf0fe2dSDaniel Campello } __packed;
43fdf0fe2dSDaniel Campello 
44fdf0fe2dSDaniel Campello struct usb_charge_response {
45fdf0fe2dSDaniel Campello 	u8 reserved;
46fdf0fe2dSDaniel Campello 	u8 status;	/* Set by EC to 0 on success, other value on failure */
47fdf0fe2dSDaniel Campello 	u8 val;		/* When getting, set by EC to either 0 or 1 */
48fdf0fe2dSDaniel Campello } __packed;
49fdf0fe2dSDaniel Campello 
5079e3f1d3SRaul E Rangel #define CMD_EC_INFO			0x38
5179e3f1d3SRaul E Rangel enum get_ec_info_op {
5279e3f1d3SRaul E Rangel 	CMD_GET_EC_LABEL	= 0,
5379e3f1d3SRaul E Rangel 	CMD_GET_EC_REV		= 1,
5479e3f1d3SRaul E Rangel 	CMD_GET_EC_MODEL	= 2,
5579e3f1d3SRaul E Rangel 	CMD_GET_EC_BUILD_DATE	= 3,
5679e3f1d3SRaul E Rangel };
5779e3f1d3SRaul E Rangel 
5879e3f1d3SRaul E Rangel struct get_ec_info_req {
5979e3f1d3SRaul E Rangel 	u8 cmd;			/* Always CMD_EC_INFO */
6079e3f1d3SRaul E Rangel 	u8 reserved;
6179e3f1d3SRaul E Rangel 	u8 op;			/* One of enum get_ec_info_op */
6279e3f1d3SRaul E Rangel } __packed;
6379e3f1d3SRaul E Rangel 
6479e3f1d3SRaul E Rangel struct get_ec_info_resp {
6579e3f1d3SRaul E Rangel 	u8 reserved[2];
6679e3f1d3SRaul E Rangel 	char value[9]; /* __nonstring: might not be null terminated */
6779e3f1d3SRaul E Rangel } __packed;
6879e3f1d3SRaul E Rangel 
boot_on_ac_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)694c1ca625SNick Crews static ssize_t boot_on_ac_store(struct device *dev,
704c1ca625SNick Crews 				struct device_attribute *attr,
714c1ca625SNick Crews 				const char *buf, size_t count)
724c1ca625SNick Crews {
734c1ca625SNick Crews 	struct wilco_ec_device *ec = dev_get_drvdata(dev);
744c1ca625SNick Crews 	struct boot_on_ac_request rq;
754c1ca625SNick Crews 	struct wilco_ec_message msg;
764c1ca625SNick Crews 	int ret;
774c1ca625SNick Crews 	u8 val;
784c1ca625SNick Crews 
794c1ca625SNick Crews 	ret = kstrtou8(buf, 10, &val);
804c1ca625SNick Crews 	if (ret < 0)
814c1ca625SNick Crews 		return ret;
824c1ca625SNick Crews 	if (val > 1)
834c1ca625SNick Crews 		return -EINVAL;
844c1ca625SNick Crews 
854c1ca625SNick Crews 	memset(&rq, 0, sizeof(rq));
864c1ca625SNick Crews 	rq.cmd = CMD_KB_CMOS;
874c1ca625SNick Crews 	rq.sub_cmd = SUB_CMD_KB_CMOS_AUTO_ON;
884c1ca625SNick Crews 	rq.val = val;
894c1ca625SNick Crews 
904c1ca625SNick Crews 	memset(&msg, 0, sizeof(msg));
914c1ca625SNick Crews 	msg.type = WILCO_EC_MSG_LEGACY;
924c1ca625SNick Crews 	msg.request_data = &rq;
934c1ca625SNick Crews 	msg.request_size = sizeof(rq);
944c1ca625SNick Crews 	ret = wilco_ec_mailbox(ec, &msg);
954c1ca625SNick Crews 	if (ret < 0)
964c1ca625SNick Crews 		return ret;
974c1ca625SNick Crews 
984c1ca625SNick Crews 	return count;
994c1ca625SNick Crews }
1004c1ca625SNick Crews 
1014c1ca625SNick Crews static DEVICE_ATTR_WO(boot_on_ac);
1024c1ca625SNick Crews 
get_info(struct device * dev,char * buf,enum get_ec_info_op op)10379e3f1d3SRaul E Rangel static ssize_t get_info(struct device *dev, char *buf, enum get_ec_info_op op)
10479e3f1d3SRaul E Rangel {
10579e3f1d3SRaul E Rangel 	struct wilco_ec_device *ec = dev_get_drvdata(dev);
10679e3f1d3SRaul E Rangel 	struct get_ec_info_req req = { .cmd = CMD_EC_INFO, .op = op };
10779e3f1d3SRaul E Rangel 	struct get_ec_info_resp resp;
10879e3f1d3SRaul E Rangel 	int ret;
10979e3f1d3SRaul E Rangel 
11079e3f1d3SRaul E Rangel 	struct wilco_ec_message msg = {
11179e3f1d3SRaul E Rangel 		.type = WILCO_EC_MSG_LEGACY,
11279e3f1d3SRaul E Rangel 		.request_data = &req,
11379e3f1d3SRaul E Rangel 		.request_size = sizeof(req),
11479e3f1d3SRaul E Rangel 		.response_data = &resp,
11579e3f1d3SRaul E Rangel 		.response_size = sizeof(resp),
11679e3f1d3SRaul E Rangel 	};
11779e3f1d3SRaul E Rangel 
11879e3f1d3SRaul E Rangel 	ret = wilco_ec_mailbox(ec, &msg);
11979e3f1d3SRaul E Rangel 	if (ret < 0)
12079e3f1d3SRaul E Rangel 		return ret;
12179e3f1d3SRaul E Rangel 
122256b734eSye xingchen 	return sysfs_emit(buf, "%.*s\n", (int)sizeof(resp.value), (char *)&resp.value);
12379e3f1d3SRaul E Rangel }
12479e3f1d3SRaul E Rangel 
version_show(struct device * dev,struct device_attribute * attr,char * buf)12579e3f1d3SRaul E Rangel static ssize_t version_show(struct device *dev, struct device_attribute *attr,
12679e3f1d3SRaul E Rangel 			  char *buf)
12779e3f1d3SRaul E Rangel {
12879e3f1d3SRaul E Rangel 	return get_info(dev, buf, CMD_GET_EC_LABEL);
12979e3f1d3SRaul E Rangel }
13079e3f1d3SRaul E Rangel 
13179e3f1d3SRaul E Rangel static DEVICE_ATTR_RO(version);
13279e3f1d3SRaul E Rangel 
build_revision_show(struct device * dev,struct device_attribute * attr,char * buf)13379e3f1d3SRaul E Rangel static ssize_t build_revision_show(struct device *dev,
13479e3f1d3SRaul E Rangel 				   struct device_attribute *attr, char *buf)
13579e3f1d3SRaul E Rangel {
13679e3f1d3SRaul E Rangel 	return get_info(dev, buf, CMD_GET_EC_REV);
13779e3f1d3SRaul E Rangel }
13879e3f1d3SRaul E Rangel 
13979e3f1d3SRaul E Rangel static DEVICE_ATTR_RO(build_revision);
14079e3f1d3SRaul E Rangel 
build_date_show(struct device * dev,struct device_attribute * attr,char * buf)14179e3f1d3SRaul E Rangel static ssize_t build_date_show(struct device *dev,
14279e3f1d3SRaul E Rangel 			       struct device_attribute *attr, char *buf)
14379e3f1d3SRaul E Rangel {
14479e3f1d3SRaul E Rangel 	return get_info(dev, buf, CMD_GET_EC_BUILD_DATE);
14579e3f1d3SRaul E Rangel }
14679e3f1d3SRaul E Rangel 
14779e3f1d3SRaul E Rangel static DEVICE_ATTR_RO(build_date);
14879e3f1d3SRaul E Rangel 
model_number_show(struct device * dev,struct device_attribute * attr,char * buf)14979e3f1d3SRaul E Rangel static ssize_t model_number_show(struct device *dev,
15079e3f1d3SRaul E Rangel 				 struct device_attribute *attr, char *buf)
15179e3f1d3SRaul E Rangel {
15279e3f1d3SRaul E Rangel 	return get_info(dev, buf, CMD_GET_EC_MODEL);
15379e3f1d3SRaul E Rangel }
15479e3f1d3SRaul E Rangel 
15579e3f1d3SRaul E Rangel static DEVICE_ATTR_RO(model_number);
15679e3f1d3SRaul E Rangel 
send_usb_charge(struct wilco_ec_device * ec,struct usb_charge_request * rq,struct usb_charge_response * rs)157fdf0fe2dSDaniel Campello static int send_usb_charge(struct wilco_ec_device *ec,
158fdf0fe2dSDaniel Campello 				struct usb_charge_request *rq,
159fdf0fe2dSDaniel Campello 				struct usb_charge_response *rs)
160fdf0fe2dSDaniel Campello {
161fdf0fe2dSDaniel Campello 	struct wilco_ec_message msg;
162fdf0fe2dSDaniel Campello 	int ret;
163fdf0fe2dSDaniel Campello 
164fdf0fe2dSDaniel Campello 	memset(&msg, 0, sizeof(msg));
165fdf0fe2dSDaniel Campello 	msg.type = WILCO_EC_MSG_LEGACY;
166fdf0fe2dSDaniel Campello 	msg.request_data = rq;
167fdf0fe2dSDaniel Campello 	msg.request_size = sizeof(*rq);
168fdf0fe2dSDaniel Campello 	msg.response_data = rs;
169fdf0fe2dSDaniel Campello 	msg.response_size = sizeof(*rs);
170fdf0fe2dSDaniel Campello 	ret = wilco_ec_mailbox(ec, &msg);
171fdf0fe2dSDaniel Campello 	if (ret < 0)
172fdf0fe2dSDaniel Campello 		return ret;
173fdf0fe2dSDaniel Campello 	if (rs->status)
174fdf0fe2dSDaniel Campello 		return -EIO;
175fdf0fe2dSDaniel Campello 
176fdf0fe2dSDaniel Campello 	return 0;
177fdf0fe2dSDaniel Campello }
178fdf0fe2dSDaniel Campello 
usb_charge_show(struct device * dev,struct device_attribute * attr,char * buf)179fdf0fe2dSDaniel Campello static ssize_t usb_charge_show(struct device *dev,
180fdf0fe2dSDaniel Campello 				    struct device_attribute *attr, char *buf)
181fdf0fe2dSDaniel Campello {
182fdf0fe2dSDaniel Campello 	struct wilco_ec_device *ec = dev_get_drvdata(dev);
183fdf0fe2dSDaniel Campello 	struct usb_charge_request rq;
184fdf0fe2dSDaniel Campello 	struct usb_charge_response rs;
185fdf0fe2dSDaniel Campello 	int ret;
186fdf0fe2dSDaniel Campello 
187fdf0fe2dSDaniel Campello 	memset(&rq, 0, sizeof(rq));
188fdf0fe2dSDaniel Campello 	rq.cmd = CMD_USB_CHARGE;
189fdf0fe2dSDaniel Campello 	rq.op = USB_CHARGE_GET;
190fdf0fe2dSDaniel Campello 
191fdf0fe2dSDaniel Campello 	ret = send_usb_charge(ec, &rq, &rs);
192fdf0fe2dSDaniel Campello 	if (ret < 0)
193fdf0fe2dSDaniel Campello 		return ret;
194fdf0fe2dSDaniel Campello 
195*48e49af7SAi Chao 	return sysfs_emit(buf, "%d\n", rs.val);
196fdf0fe2dSDaniel Campello }
197fdf0fe2dSDaniel Campello 
usb_charge_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)198fdf0fe2dSDaniel Campello static ssize_t usb_charge_store(struct device *dev,
199fdf0fe2dSDaniel Campello 				     struct device_attribute *attr,
200fdf0fe2dSDaniel Campello 				     const char *buf, size_t count)
201fdf0fe2dSDaniel Campello {
202fdf0fe2dSDaniel Campello 	struct wilco_ec_device *ec = dev_get_drvdata(dev);
203fdf0fe2dSDaniel Campello 	struct usb_charge_request rq;
204fdf0fe2dSDaniel Campello 	struct usb_charge_response rs;
205fdf0fe2dSDaniel Campello 	int ret;
206fdf0fe2dSDaniel Campello 	u8 val;
207fdf0fe2dSDaniel Campello 
208fdf0fe2dSDaniel Campello 	ret = kstrtou8(buf, 10, &val);
209fdf0fe2dSDaniel Campello 	if (ret < 0)
210fdf0fe2dSDaniel Campello 		return ret;
211fdf0fe2dSDaniel Campello 	if (val > 1)
212fdf0fe2dSDaniel Campello 		return -EINVAL;
213fdf0fe2dSDaniel Campello 
214fdf0fe2dSDaniel Campello 	memset(&rq, 0, sizeof(rq));
215fdf0fe2dSDaniel Campello 	rq.cmd = CMD_USB_CHARGE;
216fdf0fe2dSDaniel Campello 	rq.op = USB_CHARGE_SET;
217fdf0fe2dSDaniel Campello 	rq.val = val;
218fdf0fe2dSDaniel Campello 
219fdf0fe2dSDaniel Campello 	ret = send_usb_charge(ec, &rq, &rs);
220fdf0fe2dSDaniel Campello 	if (ret < 0)
221fdf0fe2dSDaniel Campello 		return ret;
222fdf0fe2dSDaniel Campello 
223fdf0fe2dSDaniel Campello 	return count;
224fdf0fe2dSDaniel Campello }
225fdf0fe2dSDaniel Campello 
226fdf0fe2dSDaniel Campello static DEVICE_ATTR_RW(usb_charge);
22779e3f1d3SRaul E Rangel 
2284c1ca625SNick Crews static struct attribute *wilco_dev_attrs[] = {
2294c1ca625SNick Crews 	&dev_attr_boot_on_ac.attr,
23079e3f1d3SRaul E Rangel 	&dev_attr_build_date.attr,
23179e3f1d3SRaul E Rangel 	&dev_attr_build_revision.attr,
23279e3f1d3SRaul E Rangel 	&dev_attr_model_number.attr,
233fdf0fe2dSDaniel Campello 	&dev_attr_usb_charge.attr,
23479e3f1d3SRaul E Rangel 	&dev_attr_version.attr,
2354c1ca625SNick Crews 	NULL,
2364c1ca625SNick Crews };
2374c1ca625SNick Crews 
238d7c1fef7SRikard Falkeborn static const struct attribute_group wilco_dev_attr_group = {
2394c1ca625SNick Crews 	.attrs = wilco_dev_attrs,
2404c1ca625SNick Crews };
2414c1ca625SNick Crews 
wilco_ec_add_sysfs(struct wilco_ec_device * ec)2424c1ca625SNick Crews int wilco_ec_add_sysfs(struct wilco_ec_device *ec)
2434c1ca625SNick Crews {
2444c1ca625SNick Crews 	return sysfs_create_group(&ec->dev->kobj, &wilco_dev_attr_group);
2454c1ca625SNick Crews }
2464c1ca625SNick Crews 
wilco_ec_remove_sysfs(struct wilco_ec_device * ec)2474c1ca625SNick Crews void wilco_ec_remove_sysfs(struct wilco_ec_device *ec)
2484c1ca625SNick Crews {
2494c1ca625SNick Crews 	sysfs_remove_group(&ec->dev->kobj, &wilco_dev_attr_group);
2504c1ca625SNick Crews }
251