xref: /linux/drivers/platform/chrome/cros_ec_rpmsg.c (revision 034dbec179e5d2820480f477c43acbc50245e56d)
12de89fd9SPi-Hsun Shih // SPDX-License-Identifier: GPL-2.0
22de89fd9SPi-Hsun Shih //
32de89fd9SPi-Hsun Shih // Copyright 2018 Google LLC.
42de89fd9SPi-Hsun Shih 
52de89fd9SPi-Hsun Shih #include <linux/completion.h>
62de89fd9SPi-Hsun Shih #include <linux/delay.h>
72de89fd9SPi-Hsun Shih #include <linux/kernel.h>
82de89fd9SPi-Hsun Shih #include <linux/module.h>
92de89fd9SPi-Hsun Shih #include <linux/of.h>
10840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_commands.h>
11840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_proto.h>
122de89fd9SPi-Hsun Shih #include <linux/platform_device.h>
132de89fd9SPi-Hsun Shih #include <linux/rpmsg.h>
142de89fd9SPi-Hsun Shih #include <linux/slab.h>
152de89fd9SPi-Hsun Shih 
16*034dbec1SEnric Balletbo i Serra #include "cros_ec.h"
17*034dbec1SEnric Balletbo i Serra 
182de89fd9SPi-Hsun Shih #define EC_MSG_TIMEOUT_MS	200
192de89fd9SPi-Hsun Shih #define HOST_COMMAND_MARK	1
202de89fd9SPi-Hsun Shih #define HOST_EVENT_MARK		2
212de89fd9SPi-Hsun Shih 
222de89fd9SPi-Hsun Shih /**
232de89fd9SPi-Hsun Shih  * struct cros_ec_rpmsg_response - rpmsg message format from from EC.
242de89fd9SPi-Hsun Shih  *
252de89fd9SPi-Hsun Shih  * @type:	The type of message, should be either HOST_COMMAND_MARK or
262de89fd9SPi-Hsun Shih  *		HOST_EVENT_MARK, representing that the message is a response to
272de89fd9SPi-Hsun Shih  *		host command, or a host event.
282de89fd9SPi-Hsun Shih  * @data:	ec_host_response for host command.
292de89fd9SPi-Hsun Shih  */
302de89fd9SPi-Hsun Shih struct cros_ec_rpmsg_response {
312de89fd9SPi-Hsun Shih 	u8 type;
322de89fd9SPi-Hsun Shih 	u8 data[] __aligned(4);
332de89fd9SPi-Hsun Shih };
342de89fd9SPi-Hsun Shih 
352de89fd9SPi-Hsun Shih /**
362de89fd9SPi-Hsun Shih  * struct cros_ec_rpmsg - information about a EC over rpmsg.
372de89fd9SPi-Hsun Shih  *
382de89fd9SPi-Hsun Shih  * @rpdev:	rpmsg device we are connected to
392de89fd9SPi-Hsun Shih  * @xfer_ack:	completion for host command transfer.
402de89fd9SPi-Hsun Shih  * @host_event_work:	Work struct for pending host event.
412de89fd9SPi-Hsun Shih  */
422de89fd9SPi-Hsun Shih struct cros_ec_rpmsg {
432de89fd9SPi-Hsun Shih 	struct rpmsg_device *rpdev;
442de89fd9SPi-Hsun Shih 	struct completion xfer_ack;
452de89fd9SPi-Hsun Shih 	struct work_struct host_event_work;
4671cddb70SPi-Hsun Shih 	struct rpmsg_endpoint *ept;
472de89fd9SPi-Hsun Shih };
482de89fd9SPi-Hsun Shih 
492de89fd9SPi-Hsun Shih /**
502de89fd9SPi-Hsun Shih  * cros_ec_cmd_xfer_rpmsg - Transfer a message over rpmsg and receive the reply
512de89fd9SPi-Hsun Shih  *
522de89fd9SPi-Hsun Shih  * @ec_dev: ChromeOS EC device
532de89fd9SPi-Hsun Shih  * @ec_msg: Message to transfer
542de89fd9SPi-Hsun Shih  *
552de89fd9SPi-Hsun Shih  * This is only used for old EC proto version, and is not supported for this
562de89fd9SPi-Hsun Shih  * driver.
572de89fd9SPi-Hsun Shih  *
582de89fd9SPi-Hsun Shih  * Return: -EINVAL
592de89fd9SPi-Hsun Shih  */
602de89fd9SPi-Hsun Shih static int cros_ec_cmd_xfer_rpmsg(struct cros_ec_device *ec_dev,
612de89fd9SPi-Hsun Shih 				  struct cros_ec_command *ec_msg)
622de89fd9SPi-Hsun Shih {
632de89fd9SPi-Hsun Shih 	return -EINVAL;
642de89fd9SPi-Hsun Shih }
652de89fd9SPi-Hsun Shih 
662de89fd9SPi-Hsun Shih /**
672de89fd9SPi-Hsun Shih  * cros_ec_pkt_xfer_rpmsg - Transfer a packet over rpmsg and receive the reply
682de89fd9SPi-Hsun Shih  *
692de89fd9SPi-Hsun Shih  * @ec_dev: ChromeOS EC device
702de89fd9SPi-Hsun Shih  * @ec_msg: Message to transfer
712de89fd9SPi-Hsun Shih  *
722de89fd9SPi-Hsun Shih  * Return: number of bytes of the reply on success or negative error code.
732de89fd9SPi-Hsun Shih  */
742de89fd9SPi-Hsun Shih static int cros_ec_pkt_xfer_rpmsg(struct cros_ec_device *ec_dev,
752de89fd9SPi-Hsun Shih 				  struct cros_ec_command *ec_msg)
762de89fd9SPi-Hsun Shih {
772de89fd9SPi-Hsun Shih 	struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv;
782de89fd9SPi-Hsun Shih 	struct ec_host_response *response;
792de89fd9SPi-Hsun Shih 	unsigned long timeout;
802de89fd9SPi-Hsun Shih 	int len;
812de89fd9SPi-Hsun Shih 	int ret;
822de89fd9SPi-Hsun Shih 	u8 sum;
832de89fd9SPi-Hsun Shih 	int i;
842de89fd9SPi-Hsun Shih 
852de89fd9SPi-Hsun Shih 	ec_msg->result = 0;
862de89fd9SPi-Hsun Shih 	len = cros_ec_prepare_tx(ec_dev, ec_msg);
872de89fd9SPi-Hsun Shih 	dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
882de89fd9SPi-Hsun Shih 
892de89fd9SPi-Hsun Shih 	reinit_completion(&ec_rpmsg->xfer_ack);
9071cddb70SPi-Hsun Shih 	ret = rpmsg_send(ec_rpmsg->ept, ec_dev->dout, len);
912de89fd9SPi-Hsun Shih 	if (ret) {
922de89fd9SPi-Hsun Shih 		dev_err(ec_dev->dev, "rpmsg send failed\n");
932de89fd9SPi-Hsun Shih 		return ret;
942de89fd9SPi-Hsun Shih 	}
952de89fd9SPi-Hsun Shih 
962de89fd9SPi-Hsun Shih 	timeout = msecs_to_jiffies(EC_MSG_TIMEOUT_MS);
972de89fd9SPi-Hsun Shih 	ret = wait_for_completion_timeout(&ec_rpmsg->xfer_ack, timeout);
982de89fd9SPi-Hsun Shih 	if (!ret) {
992de89fd9SPi-Hsun Shih 		dev_err(ec_dev->dev, "rpmsg send timeout\n");
1002de89fd9SPi-Hsun Shih 		return -EIO;
1012de89fd9SPi-Hsun Shih 	}
1022de89fd9SPi-Hsun Shih 
1032de89fd9SPi-Hsun Shih 	/* check response error code */
1042de89fd9SPi-Hsun Shih 	response = (struct ec_host_response *)ec_dev->din;
1052de89fd9SPi-Hsun Shih 	ec_msg->result = response->result;
1062de89fd9SPi-Hsun Shih 
1072de89fd9SPi-Hsun Shih 	ret = cros_ec_check_result(ec_dev, ec_msg);
1082de89fd9SPi-Hsun Shih 	if (ret)
1092de89fd9SPi-Hsun Shih 		goto exit;
1102de89fd9SPi-Hsun Shih 
1112de89fd9SPi-Hsun Shih 	if (response->data_len > ec_msg->insize) {
1122de89fd9SPi-Hsun Shih 		dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
1132de89fd9SPi-Hsun Shih 			response->data_len, ec_msg->insize);
1142de89fd9SPi-Hsun Shih 		ret = -EMSGSIZE;
1152de89fd9SPi-Hsun Shih 		goto exit;
1162de89fd9SPi-Hsun Shih 	}
1172de89fd9SPi-Hsun Shih 
1182de89fd9SPi-Hsun Shih 	/* copy response packet payload and compute checksum */
1192de89fd9SPi-Hsun Shih 	memcpy(ec_msg->data, ec_dev->din + sizeof(*response),
1202de89fd9SPi-Hsun Shih 	       response->data_len);
1212de89fd9SPi-Hsun Shih 
1222de89fd9SPi-Hsun Shih 	sum = 0;
1232de89fd9SPi-Hsun Shih 	for (i = 0; i < sizeof(*response) + response->data_len; i++)
1242de89fd9SPi-Hsun Shih 		sum += ec_dev->din[i];
1252de89fd9SPi-Hsun Shih 
1262de89fd9SPi-Hsun Shih 	if (sum) {
1272de89fd9SPi-Hsun Shih 		dev_err(ec_dev->dev, "bad packet checksum, calculated %x\n",
1282de89fd9SPi-Hsun Shih 			sum);
1292de89fd9SPi-Hsun Shih 		ret = -EBADMSG;
1302de89fd9SPi-Hsun Shih 		goto exit;
1312de89fd9SPi-Hsun Shih 	}
1322de89fd9SPi-Hsun Shih 
1332de89fd9SPi-Hsun Shih 	ret = response->data_len;
1342de89fd9SPi-Hsun Shih exit:
1352de89fd9SPi-Hsun Shih 	if (ec_msg->command == EC_CMD_REBOOT_EC)
1362de89fd9SPi-Hsun Shih 		msleep(EC_REBOOT_DELAY_MS);
1372de89fd9SPi-Hsun Shih 
1382de89fd9SPi-Hsun Shih 	return ret;
1392de89fd9SPi-Hsun Shih }
1402de89fd9SPi-Hsun Shih 
1412de89fd9SPi-Hsun Shih static void
1422de89fd9SPi-Hsun Shih cros_ec_rpmsg_host_event_function(struct work_struct *host_event_work)
1432de89fd9SPi-Hsun Shih {
1442de89fd9SPi-Hsun Shih 	struct cros_ec_rpmsg *ec_rpmsg = container_of(host_event_work,
1452de89fd9SPi-Hsun Shih 						      struct cros_ec_rpmsg,
1462de89fd9SPi-Hsun Shih 						      host_event_work);
1472de89fd9SPi-Hsun Shih 	struct cros_ec_device *ec_dev = dev_get_drvdata(&ec_rpmsg->rpdev->dev);
1483300fdd6SEnrico Granata 	bool ec_has_more_events;
1492de89fd9SPi-Hsun Shih 
1503300fdd6SEnrico Granata 	do {
1513300fdd6SEnrico Granata 		ec_has_more_events = cros_ec_handle_event(ec_dev);
1523300fdd6SEnrico Granata 	} while (ec_has_more_events);
1532de89fd9SPi-Hsun Shih }
1542de89fd9SPi-Hsun Shih 
1552de89fd9SPi-Hsun Shih static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
1562de89fd9SPi-Hsun Shih 				  int len, void *priv, u32 src)
1572de89fd9SPi-Hsun Shih {
1582de89fd9SPi-Hsun Shih 	struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev);
1592de89fd9SPi-Hsun Shih 	struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv;
1602de89fd9SPi-Hsun Shih 	struct cros_ec_rpmsg_response *resp;
1612de89fd9SPi-Hsun Shih 
1622de89fd9SPi-Hsun Shih 	if (!len) {
1632de89fd9SPi-Hsun Shih 		dev_warn(ec_dev->dev, "rpmsg received empty response");
1642de89fd9SPi-Hsun Shih 		return -EINVAL;
1652de89fd9SPi-Hsun Shih 	}
1662de89fd9SPi-Hsun Shih 
1672de89fd9SPi-Hsun Shih 	resp = data;
1682de89fd9SPi-Hsun Shih 	len -= offsetof(struct cros_ec_rpmsg_response, data);
1692de89fd9SPi-Hsun Shih 	if (resp->type == HOST_COMMAND_MARK) {
1702de89fd9SPi-Hsun Shih 		if (len > ec_dev->din_size) {
1712de89fd9SPi-Hsun Shih 			dev_warn(ec_dev->dev,
1722de89fd9SPi-Hsun Shih 				 "received length %d > din_size %d, truncating",
1732de89fd9SPi-Hsun Shih 				 len, ec_dev->din_size);
1742de89fd9SPi-Hsun Shih 			len = ec_dev->din_size;
1752de89fd9SPi-Hsun Shih 		}
1762de89fd9SPi-Hsun Shih 
1772de89fd9SPi-Hsun Shih 		memcpy(ec_dev->din, resp->data, len);
1782de89fd9SPi-Hsun Shih 		complete(&ec_rpmsg->xfer_ack);
1792de89fd9SPi-Hsun Shih 	} else if (resp->type == HOST_EVENT_MARK) {
1802de89fd9SPi-Hsun Shih 		schedule_work(&ec_rpmsg->host_event_work);
1812de89fd9SPi-Hsun Shih 	} else {
1822de89fd9SPi-Hsun Shih 		dev_warn(ec_dev->dev, "rpmsg received invalid type = %d",
1832de89fd9SPi-Hsun Shih 			 resp->type);
1842de89fd9SPi-Hsun Shih 		return -EINVAL;
1852de89fd9SPi-Hsun Shih 	}
1862de89fd9SPi-Hsun Shih 
1872de89fd9SPi-Hsun Shih 	return 0;
1882de89fd9SPi-Hsun Shih }
1892de89fd9SPi-Hsun Shih 
19071cddb70SPi-Hsun Shih static struct rpmsg_endpoint *
19171cddb70SPi-Hsun Shih cros_ec_rpmsg_create_ept(struct rpmsg_device *rpdev)
19271cddb70SPi-Hsun Shih {
19371cddb70SPi-Hsun Shih 	struct rpmsg_channel_info chinfo = {};
19471cddb70SPi-Hsun Shih 
19571cddb70SPi-Hsun Shih 	strscpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
19671cddb70SPi-Hsun Shih 	chinfo.src = rpdev->src;
19771cddb70SPi-Hsun Shih 	chinfo.dst = RPMSG_ADDR_ANY;
19871cddb70SPi-Hsun Shih 
19971cddb70SPi-Hsun Shih 	return rpmsg_create_ept(rpdev, cros_ec_rpmsg_callback, NULL, chinfo);
20071cddb70SPi-Hsun Shih }
20171cddb70SPi-Hsun Shih 
2022de89fd9SPi-Hsun Shih static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev)
2032de89fd9SPi-Hsun Shih {
2042de89fd9SPi-Hsun Shih 	struct device *dev = &rpdev->dev;
2052de89fd9SPi-Hsun Shih 	struct cros_ec_rpmsg *ec_rpmsg;
2062de89fd9SPi-Hsun Shih 	struct cros_ec_device *ec_dev;
20771cddb70SPi-Hsun Shih 	int ret;
2082de89fd9SPi-Hsun Shih 
2092de89fd9SPi-Hsun Shih 	ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
2102de89fd9SPi-Hsun Shih 	if (!ec_dev)
2112de89fd9SPi-Hsun Shih 		return -ENOMEM;
2122de89fd9SPi-Hsun Shih 
2132de89fd9SPi-Hsun Shih 	ec_rpmsg = devm_kzalloc(dev, sizeof(*ec_rpmsg), GFP_KERNEL);
2142de89fd9SPi-Hsun Shih 	if (!ec_rpmsg)
2152de89fd9SPi-Hsun Shih 		return -ENOMEM;
2162de89fd9SPi-Hsun Shih 
2172de89fd9SPi-Hsun Shih 	ec_dev->dev = dev;
2182de89fd9SPi-Hsun Shih 	ec_dev->priv = ec_rpmsg;
2192de89fd9SPi-Hsun Shih 	ec_dev->cmd_xfer = cros_ec_cmd_xfer_rpmsg;
2202de89fd9SPi-Hsun Shih 	ec_dev->pkt_xfer = cros_ec_pkt_xfer_rpmsg;
2212de89fd9SPi-Hsun Shih 	ec_dev->phys_name = dev_name(&rpdev->dev);
2222de89fd9SPi-Hsun Shih 	ec_dev->din_size = sizeof(struct ec_host_response) +
2232de89fd9SPi-Hsun Shih 			   sizeof(struct ec_response_get_protocol_info);
2242de89fd9SPi-Hsun Shih 	ec_dev->dout_size = sizeof(struct ec_host_request);
2252de89fd9SPi-Hsun Shih 	dev_set_drvdata(dev, ec_dev);
2262de89fd9SPi-Hsun Shih 
2272de89fd9SPi-Hsun Shih 	ec_rpmsg->rpdev = rpdev;
2282de89fd9SPi-Hsun Shih 	init_completion(&ec_rpmsg->xfer_ack);
2292de89fd9SPi-Hsun Shih 	INIT_WORK(&ec_rpmsg->host_event_work,
2302de89fd9SPi-Hsun Shih 		  cros_ec_rpmsg_host_event_function);
2312de89fd9SPi-Hsun Shih 
23271cddb70SPi-Hsun Shih 	ec_rpmsg->ept = cros_ec_rpmsg_create_ept(rpdev);
23371cddb70SPi-Hsun Shih 	if (!ec_rpmsg->ept)
23471cddb70SPi-Hsun Shih 		return -ENOMEM;
23571cddb70SPi-Hsun Shih 
23671cddb70SPi-Hsun Shih 	ret = cros_ec_register(ec_dev);
23771cddb70SPi-Hsun Shih 	if (ret < 0) {
23871cddb70SPi-Hsun Shih 		rpmsg_destroy_ept(ec_rpmsg->ept);
23971cddb70SPi-Hsun Shih 		cancel_work_sync(&ec_rpmsg->host_event_work);
24071cddb70SPi-Hsun Shih 		return ret;
24171cddb70SPi-Hsun Shih 	}
24271cddb70SPi-Hsun Shih 
24371cddb70SPi-Hsun Shih 	return 0;
2442de89fd9SPi-Hsun Shih }
2452de89fd9SPi-Hsun Shih 
2462de89fd9SPi-Hsun Shih static void cros_ec_rpmsg_remove(struct rpmsg_device *rpdev)
2472de89fd9SPi-Hsun Shih {
2482de89fd9SPi-Hsun Shih 	struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev);
2492de89fd9SPi-Hsun Shih 	struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv;
2502de89fd9SPi-Hsun Shih 
2517aa703bbSEnric Balletbo i Serra 	cros_ec_unregister(ec_dev);
25271cddb70SPi-Hsun Shih 	rpmsg_destroy_ept(ec_rpmsg->ept);
2532de89fd9SPi-Hsun Shih 	cancel_work_sync(&ec_rpmsg->host_event_work);
2542de89fd9SPi-Hsun Shih }
2552de89fd9SPi-Hsun Shih 
256058bf149SYilun Lin #ifdef CONFIG_PM_SLEEP
257058bf149SYilun Lin static int cros_ec_rpmsg_suspend(struct device *dev)
258058bf149SYilun Lin {
259058bf149SYilun Lin 	struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
260058bf149SYilun Lin 
261058bf149SYilun Lin 	return cros_ec_suspend(ec_dev);
262058bf149SYilun Lin }
263058bf149SYilun Lin 
264058bf149SYilun Lin static int cros_ec_rpmsg_resume(struct device *dev)
265058bf149SYilun Lin {
266058bf149SYilun Lin 	struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
267058bf149SYilun Lin 
268058bf149SYilun Lin 	return cros_ec_resume(ec_dev);
269058bf149SYilun Lin }
270058bf149SYilun Lin #endif
271058bf149SYilun Lin 
272058bf149SYilun Lin static SIMPLE_DEV_PM_OPS(cros_ec_rpmsg_pm_ops, cros_ec_rpmsg_suspend,
273058bf149SYilun Lin 			 cros_ec_rpmsg_resume);
274058bf149SYilun Lin 
2752de89fd9SPi-Hsun Shih static const struct of_device_id cros_ec_rpmsg_of_match[] = {
2762de89fd9SPi-Hsun Shih 	{ .compatible = "google,cros-ec-rpmsg", },
2772de89fd9SPi-Hsun Shih 	{ }
2782de89fd9SPi-Hsun Shih };
2792de89fd9SPi-Hsun Shih MODULE_DEVICE_TABLE(of, cros_ec_rpmsg_of_match);
2802de89fd9SPi-Hsun Shih 
2812de89fd9SPi-Hsun Shih static struct rpmsg_driver cros_ec_driver_rpmsg = {
2822de89fd9SPi-Hsun Shih 	.drv = {
2832de89fd9SPi-Hsun Shih 		.name   = "cros-ec-rpmsg",
2842de89fd9SPi-Hsun Shih 		.of_match_table = cros_ec_rpmsg_of_match,
285058bf149SYilun Lin 		.pm	= &cros_ec_rpmsg_pm_ops,
2862de89fd9SPi-Hsun Shih 	},
2872de89fd9SPi-Hsun Shih 	.probe		= cros_ec_rpmsg_probe,
2882de89fd9SPi-Hsun Shih 	.remove		= cros_ec_rpmsg_remove,
2892de89fd9SPi-Hsun Shih };
2902de89fd9SPi-Hsun Shih 
2912de89fd9SPi-Hsun Shih module_rpmsg_driver(cros_ec_driver_rpmsg);
2922de89fd9SPi-Hsun Shih 
2932de89fd9SPi-Hsun Shih MODULE_LICENSE("GPL v2");
2942de89fd9SPi-Hsun Shih MODULE_DESCRIPTION("ChromeOS EC multi function device (rpmsg)");
295