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> 10*840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_commands.h> 11*840d9f13SEnric 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 162de89fd9SPi-Hsun Shih #define EC_MSG_TIMEOUT_MS 200 172de89fd9SPi-Hsun Shih #define HOST_COMMAND_MARK 1 182de89fd9SPi-Hsun Shih #define HOST_EVENT_MARK 2 192de89fd9SPi-Hsun Shih 202de89fd9SPi-Hsun Shih /** 212de89fd9SPi-Hsun Shih * struct cros_ec_rpmsg_response - rpmsg message format from from EC. 222de89fd9SPi-Hsun Shih * 232de89fd9SPi-Hsun Shih * @type: The type of message, should be either HOST_COMMAND_MARK or 242de89fd9SPi-Hsun Shih * HOST_EVENT_MARK, representing that the message is a response to 252de89fd9SPi-Hsun Shih * host command, or a host event. 262de89fd9SPi-Hsun Shih * @data: ec_host_response for host command. 272de89fd9SPi-Hsun Shih */ 282de89fd9SPi-Hsun Shih struct cros_ec_rpmsg_response { 292de89fd9SPi-Hsun Shih u8 type; 302de89fd9SPi-Hsun Shih u8 data[] __aligned(4); 312de89fd9SPi-Hsun Shih }; 322de89fd9SPi-Hsun Shih 332de89fd9SPi-Hsun Shih /** 342de89fd9SPi-Hsun Shih * struct cros_ec_rpmsg - information about a EC over rpmsg. 352de89fd9SPi-Hsun Shih * 362de89fd9SPi-Hsun Shih * @rpdev: rpmsg device we are connected to 372de89fd9SPi-Hsun Shih * @xfer_ack: completion for host command transfer. 382de89fd9SPi-Hsun Shih * @host_event_work: Work struct for pending host event. 392de89fd9SPi-Hsun Shih */ 402de89fd9SPi-Hsun Shih struct cros_ec_rpmsg { 412de89fd9SPi-Hsun Shih struct rpmsg_device *rpdev; 422de89fd9SPi-Hsun Shih struct completion xfer_ack; 432de89fd9SPi-Hsun Shih struct work_struct host_event_work; 442de89fd9SPi-Hsun Shih }; 452de89fd9SPi-Hsun Shih 462de89fd9SPi-Hsun Shih /** 472de89fd9SPi-Hsun Shih * cros_ec_cmd_xfer_rpmsg - Transfer a message over rpmsg and receive the reply 482de89fd9SPi-Hsun Shih * 492de89fd9SPi-Hsun Shih * @ec_dev: ChromeOS EC device 502de89fd9SPi-Hsun Shih * @ec_msg: Message to transfer 512de89fd9SPi-Hsun Shih * 522de89fd9SPi-Hsun Shih * This is only used for old EC proto version, and is not supported for this 532de89fd9SPi-Hsun Shih * driver. 542de89fd9SPi-Hsun Shih * 552de89fd9SPi-Hsun Shih * Return: -EINVAL 562de89fd9SPi-Hsun Shih */ 572de89fd9SPi-Hsun Shih static int cros_ec_cmd_xfer_rpmsg(struct cros_ec_device *ec_dev, 582de89fd9SPi-Hsun Shih struct cros_ec_command *ec_msg) 592de89fd9SPi-Hsun Shih { 602de89fd9SPi-Hsun Shih return -EINVAL; 612de89fd9SPi-Hsun Shih } 622de89fd9SPi-Hsun Shih 632de89fd9SPi-Hsun Shih /** 642de89fd9SPi-Hsun Shih * cros_ec_pkt_xfer_rpmsg - Transfer a packet over rpmsg and receive the reply 652de89fd9SPi-Hsun Shih * 662de89fd9SPi-Hsun Shih * @ec_dev: ChromeOS EC device 672de89fd9SPi-Hsun Shih * @ec_msg: Message to transfer 682de89fd9SPi-Hsun Shih * 692de89fd9SPi-Hsun Shih * Return: number of bytes of the reply on success or negative error code. 702de89fd9SPi-Hsun Shih */ 712de89fd9SPi-Hsun Shih static int cros_ec_pkt_xfer_rpmsg(struct cros_ec_device *ec_dev, 722de89fd9SPi-Hsun Shih struct cros_ec_command *ec_msg) 732de89fd9SPi-Hsun Shih { 742de89fd9SPi-Hsun Shih struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv; 752de89fd9SPi-Hsun Shih struct rpmsg_device *rpdev = ec_rpmsg->rpdev; 762de89fd9SPi-Hsun Shih struct ec_host_response *response; 772de89fd9SPi-Hsun Shih unsigned long timeout; 782de89fd9SPi-Hsun Shih int len; 792de89fd9SPi-Hsun Shih int ret; 802de89fd9SPi-Hsun Shih u8 sum; 812de89fd9SPi-Hsun Shih int i; 822de89fd9SPi-Hsun Shih 832de89fd9SPi-Hsun Shih ec_msg->result = 0; 842de89fd9SPi-Hsun Shih len = cros_ec_prepare_tx(ec_dev, ec_msg); 852de89fd9SPi-Hsun Shih dev_dbg(ec_dev->dev, "prepared, len=%d\n", len); 862de89fd9SPi-Hsun Shih 872de89fd9SPi-Hsun Shih reinit_completion(&ec_rpmsg->xfer_ack); 882de89fd9SPi-Hsun Shih ret = rpmsg_send(rpdev->ept, ec_dev->dout, len); 892de89fd9SPi-Hsun Shih if (ret) { 902de89fd9SPi-Hsun Shih dev_err(ec_dev->dev, "rpmsg send failed\n"); 912de89fd9SPi-Hsun Shih return ret; 922de89fd9SPi-Hsun Shih } 932de89fd9SPi-Hsun Shih 942de89fd9SPi-Hsun Shih timeout = msecs_to_jiffies(EC_MSG_TIMEOUT_MS); 952de89fd9SPi-Hsun Shih ret = wait_for_completion_timeout(&ec_rpmsg->xfer_ack, timeout); 962de89fd9SPi-Hsun Shih if (!ret) { 972de89fd9SPi-Hsun Shih dev_err(ec_dev->dev, "rpmsg send timeout\n"); 982de89fd9SPi-Hsun Shih return -EIO; 992de89fd9SPi-Hsun Shih } 1002de89fd9SPi-Hsun Shih 1012de89fd9SPi-Hsun Shih /* check response error code */ 1022de89fd9SPi-Hsun Shih response = (struct ec_host_response *)ec_dev->din; 1032de89fd9SPi-Hsun Shih ec_msg->result = response->result; 1042de89fd9SPi-Hsun Shih 1052de89fd9SPi-Hsun Shih ret = cros_ec_check_result(ec_dev, ec_msg); 1062de89fd9SPi-Hsun Shih if (ret) 1072de89fd9SPi-Hsun Shih goto exit; 1082de89fd9SPi-Hsun Shih 1092de89fd9SPi-Hsun Shih if (response->data_len > ec_msg->insize) { 1102de89fd9SPi-Hsun Shih dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)", 1112de89fd9SPi-Hsun Shih response->data_len, ec_msg->insize); 1122de89fd9SPi-Hsun Shih ret = -EMSGSIZE; 1132de89fd9SPi-Hsun Shih goto exit; 1142de89fd9SPi-Hsun Shih } 1152de89fd9SPi-Hsun Shih 1162de89fd9SPi-Hsun Shih /* copy response packet payload and compute checksum */ 1172de89fd9SPi-Hsun Shih memcpy(ec_msg->data, ec_dev->din + sizeof(*response), 1182de89fd9SPi-Hsun Shih response->data_len); 1192de89fd9SPi-Hsun Shih 1202de89fd9SPi-Hsun Shih sum = 0; 1212de89fd9SPi-Hsun Shih for (i = 0; i < sizeof(*response) + response->data_len; i++) 1222de89fd9SPi-Hsun Shih sum += ec_dev->din[i]; 1232de89fd9SPi-Hsun Shih 1242de89fd9SPi-Hsun Shih if (sum) { 1252de89fd9SPi-Hsun Shih dev_err(ec_dev->dev, "bad packet checksum, calculated %x\n", 1262de89fd9SPi-Hsun Shih sum); 1272de89fd9SPi-Hsun Shih ret = -EBADMSG; 1282de89fd9SPi-Hsun Shih goto exit; 1292de89fd9SPi-Hsun Shih } 1302de89fd9SPi-Hsun Shih 1312de89fd9SPi-Hsun Shih ret = response->data_len; 1322de89fd9SPi-Hsun Shih exit: 1332de89fd9SPi-Hsun Shih if (ec_msg->command == EC_CMD_REBOOT_EC) 1342de89fd9SPi-Hsun Shih msleep(EC_REBOOT_DELAY_MS); 1352de89fd9SPi-Hsun Shih 1362de89fd9SPi-Hsun Shih return ret; 1372de89fd9SPi-Hsun Shih } 1382de89fd9SPi-Hsun Shih 1392de89fd9SPi-Hsun Shih static void 1402de89fd9SPi-Hsun Shih cros_ec_rpmsg_host_event_function(struct work_struct *host_event_work) 1412de89fd9SPi-Hsun Shih { 1422de89fd9SPi-Hsun Shih struct cros_ec_rpmsg *ec_rpmsg = container_of(host_event_work, 1432de89fd9SPi-Hsun Shih struct cros_ec_rpmsg, 1442de89fd9SPi-Hsun Shih host_event_work); 1452de89fd9SPi-Hsun Shih struct cros_ec_device *ec_dev = dev_get_drvdata(&ec_rpmsg->rpdev->dev); 1462de89fd9SPi-Hsun Shih bool wake_event = true; 1472de89fd9SPi-Hsun Shih int ret; 1482de89fd9SPi-Hsun Shih 1492de89fd9SPi-Hsun Shih ret = cros_ec_get_next_event(ec_dev, &wake_event); 1502de89fd9SPi-Hsun Shih 1512de89fd9SPi-Hsun Shih /* 1522de89fd9SPi-Hsun Shih * Signal only if wake host events or any interrupt if 1532de89fd9SPi-Hsun Shih * cros_ec_get_next_event() returned an error (default value for 1542de89fd9SPi-Hsun Shih * wake_event is true) 1552de89fd9SPi-Hsun Shih */ 1562de89fd9SPi-Hsun Shih if (wake_event && device_may_wakeup(ec_dev->dev)) 1572de89fd9SPi-Hsun Shih pm_wakeup_event(ec_dev->dev, 0); 1582de89fd9SPi-Hsun Shih 1592de89fd9SPi-Hsun Shih if (ret > 0) 1602de89fd9SPi-Hsun Shih blocking_notifier_call_chain(&ec_dev->event_notifier, 1612de89fd9SPi-Hsun Shih 0, ec_dev); 1622de89fd9SPi-Hsun Shih } 1632de89fd9SPi-Hsun Shih 1642de89fd9SPi-Hsun Shih static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data, 1652de89fd9SPi-Hsun Shih int len, void *priv, u32 src) 1662de89fd9SPi-Hsun Shih { 1672de89fd9SPi-Hsun Shih struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev); 1682de89fd9SPi-Hsun Shih struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv; 1692de89fd9SPi-Hsun Shih struct cros_ec_rpmsg_response *resp; 1702de89fd9SPi-Hsun Shih 1712de89fd9SPi-Hsun Shih if (!len) { 1722de89fd9SPi-Hsun Shih dev_warn(ec_dev->dev, "rpmsg received empty response"); 1732de89fd9SPi-Hsun Shih return -EINVAL; 1742de89fd9SPi-Hsun Shih } 1752de89fd9SPi-Hsun Shih 1762de89fd9SPi-Hsun Shih resp = data; 1772de89fd9SPi-Hsun Shih len -= offsetof(struct cros_ec_rpmsg_response, data); 1782de89fd9SPi-Hsun Shih if (resp->type == HOST_COMMAND_MARK) { 1792de89fd9SPi-Hsun Shih if (len > ec_dev->din_size) { 1802de89fd9SPi-Hsun Shih dev_warn(ec_dev->dev, 1812de89fd9SPi-Hsun Shih "received length %d > din_size %d, truncating", 1822de89fd9SPi-Hsun Shih len, ec_dev->din_size); 1832de89fd9SPi-Hsun Shih len = ec_dev->din_size; 1842de89fd9SPi-Hsun Shih } 1852de89fd9SPi-Hsun Shih 1862de89fd9SPi-Hsun Shih memcpy(ec_dev->din, resp->data, len); 1872de89fd9SPi-Hsun Shih complete(&ec_rpmsg->xfer_ack); 1882de89fd9SPi-Hsun Shih } else if (resp->type == HOST_EVENT_MARK) { 1892de89fd9SPi-Hsun Shih schedule_work(&ec_rpmsg->host_event_work); 1902de89fd9SPi-Hsun Shih } else { 1912de89fd9SPi-Hsun Shih dev_warn(ec_dev->dev, "rpmsg received invalid type = %d", 1922de89fd9SPi-Hsun Shih resp->type); 1932de89fd9SPi-Hsun Shih return -EINVAL; 1942de89fd9SPi-Hsun Shih } 1952de89fd9SPi-Hsun Shih 1962de89fd9SPi-Hsun Shih return 0; 1972de89fd9SPi-Hsun Shih } 1982de89fd9SPi-Hsun Shih 1992de89fd9SPi-Hsun Shih static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev) 2002de89fd9SPi-Hsun Shih { 2012de89fd9SPi-Hsun Shih struct device *dev = &rpdev->dev; 2022de89fd9SPi-Hsun Shih struct cros_ec_rpmsg *ec_rpmsg; 2032de89fd9SPi-Hsun Shih struct cros_ec_device *ec_dev; 2042de89fd9SPi-Hsun Shih 2052de89fd9SPi-Hsun Shih ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL); 2062de89fd9SPi-Hsun Shih if (!ec_dev) 2072de89fd9SPi-Hsun Shih return -ENOMEM; 2082de89fd9SPi-Hsun Shih 2092de89fd9SPi-Hsun Shih ec_rpmsg = devm_kzalloc(dev, sizeof(*ec_rpmsg), GFP_KERNEL); 2102de89fd9SPi-Hsun Shih if (!ec_rpmsg) 2112de89fd9SPi-Hsun Shih return -ENOMEM; 2122de89fd9SPi-Hsun Shih 2132de89fd9SPi-Hsun Shih ec_dev->dev = dev; 2142de89fd9SPi-Hsun Shih ec_dev->priv = ec_rpmsg; 2152de89fd9SPi-Hsun Shih ec_dev->cmd_xfer = cros_ec_cmd_xfer_rpmsg; 2162de89fd9SPi-Hsun Shih ec_dev->pkt_xfer = cros_ec_pkt_xfer_rpmsg; 2172de89fd9SPi-Hsun Shih ec_dev->phys_name = dev_name(&rpdev->dev); 2182de89fd9SPi-Hsun Shih ec_dev->din_size = sizeof(struct ec_host_response) + 2192de89fd9SPi-Hsun Shih sizeof(struct ec_response_get_protocol_info); 2202de89fd9SPi-Hsun Shih ec_dev->dout_size = sizeof(struct ec_host_request); 2212de89fd9SPi-Hsun Shih dev_set_drvdata(dev, ec_dev); 2222de89fd9SPi-Hsun Shih 2232de89fd9SPi-Hsun Shih ec_rpmsg->rpdev = rpdev; 2242de89fd9SPi-Hsun Shih init_completion(&ec_rpmsg->xfer_ack); 2252de89fd9SPi-Hsun Shih INIT_WORK(&ec_rpmsg->host_event_work, 2262de89fd9SPi-Hsun Shih cros_ec_rpmsg_host_event_function); 2272de89fd9SPi-Hsun Shih 2282de89fd9SPi-Hsun Shih return cros_ec_register(ec_dev); 2292de89fd9SPi-Hsun Shih } 2302de89fd9SPi-Hsun Shih 2312de89fd9SPi-Hsun Shih static void cros_ec_rpmsg_remove(struct rpmsg_device *rpdev) 2322de89fd9SPi-Hsun Shih { 2332de89fd9SPi-Hsun Shih struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev); 2342de89fd9SPi-Hsun Shih struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv; 2352de89fd9SPi-Hsun Shih 2367aa703bbSEnric Balletbo i Serra cros_ec_unregister(ec_dev); 2377aa703bbSEnric Balletbo i Serra 2382de89fd9SPi-Hsun Shih cancel_work_sync(&ec_rpmsg->host_event_work); 2392de89fd9SPi-Hsun Shih } 2402de89fd9SPi-Hsun Shih 2412de89fd9SPi-Hsun Shih static const struct of_device_id cros_ec_rpmsg_of_match[] = { 2422de89fd9SPi-Hsun Shih { .compatible = "google,cros-ec-rpmsg", }, 2432de89fd9SPi-Hsun Shih { } 2442de89fd9SPi-Hsun Shih }; 2452de89fd9SPi-Hsun Shih MODULE_DEVICE_TABLE(of, cros_ec_rpmsg_of_match); 2462de89fd9SPi-Hsun Shih 2472de89fd9SPi-Hsun Shih static struct rpmsg_driver cros_ec_driver_rpmsg = { 2482de89fd9SPi-Hsun Shih .drv = { 2492de89fd9SPi-Hsun Shih .name = "cros-ec-rpmsg", 2502de89fd9SPi-Hsun Shih .of_match_table = cros_ec_rpmsg_of_match, 2512de89fd9SPi-Hsun Shih }, 2522de89fd9SPi-Hsun Shih .probe = cros_ec_rpmsg_probe, 2532de89fd9SPi-Hsun Shih .remove = cros_ec_rpmsg_remove, 2542de89fd9SPi-Hsun Shih .callback = cros_ec_rpmsg_callback, 2552de89fd9SPi-Hsun Shih }; 2562de89fd9SPi-Hsun Shih 2572de89fd9SPi-Hsun Shih module_rpmsg_driver(cros_ec_driver_rpmsg); 2582de89fd9SPi-Hsun Shih 2592de89fd9SPi-Hsun Shih MODULE_LICENSE("GPL v2"); 2602de89fd9SPi-Hsun Shih MODULE_DESCRIPTION("ChromeOS EC multi function device (rpmsg)"); 261