1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright 2018 Google LLC.
5 #include <linux/completion.h>
6 #include <linux/delay.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
10 #include <linux/platform_data/cros_ec_commands.h>
11 #include <linux/platform_data/cros_ec_proto.h>
12 #include <linux/platform_device.h>
13 #include <linux/rpmsg.h>
14 #include <linux/slab.h>
16 #define EC_MSG_TIMEOUT_MS 200
17 #define HOST_COMMAND_MARK 1
18 #define HOST_EVENT_MARK 2
21 * struct cros_ec_rpmsg_response - rpmsg message format from from EC.
23 * @type: The type of message, should be either HOST_COMMAND_MARK or
24 * HOST_EVENT_MARK, representing that the message is a response to
25 * host command, or a host event.
26 * @data: ec_host_response for host command.
28 struct cros_ec_rpmsg_response {
30 u8 data[] __aligned(4);
34 * struct cros_ec_rpmsg - information about a EC over rpmsg.
36 * @rpdev: rpmsg device we are connected to
37 * @xfer_ack: completion for host command transfer.
38 * @host_event_work: Work struct for pending host event.
40 struct cros_ec_rpmsg {
41 struct rpmsg_device *rpdev;
42 struct completion xfer_ack;
43 struct work_struct host_event_work;
44 struct rpmsg_endpoint *ept;
48 * cros_ec_cmd_xfer_rpmsg - Transfer a message over rpmsg and receive the reply
50 * @ec_dev: ChromeOS EC device
51 * @ec_msg: Message to transfer
53 * This is only used for old EC proto version, and is not supported for this
58 static int cros_ec_cmd_xfer_rpmsg(struct cros_ec_device *ec_dev,
59 struct cros_ec_command *ec_msg)
65 * cros_ec_pkt_xfer_rpmsg - Transfer a packet over rpmsg and receive the reply
67 * @ec_dev: ChromeOS EC device
68 * @ec_msg: Message to transfer
70 * Return: number of bytes of the reply on success or negative error code.
72 static int cros_ec_pkt_xfer_rpmsg(struct cros_ec_device *ec_dev,
73 struct cros_ec_command *ec_msg)
75 struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv;
76 struct ec_host_response *response;
77 unsigned long timeout;
84 len = cros_ec_prepare_tx(ec_dev, ec_msg);
85 dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
87 reinit_completion(&ec_rpmsg->xfer_ack);
88 ret = rpmsg_send(ec_rpmsg->ept, ec_dev->dout, len);
90 dev_err(ec_dev->dev, "rpmsg send failed\n");
94 timeout = msecs_to_jiffies(EC_MSG_TIMEOUT_MS);
95 ret = wait_for_completion_timeout(&ec_rpmsg->xfer_ack, timeout);
97 dev_err(ec_dev->dev, "rpmsg send timeout\n");
101 /* check response error code */
102 response = (struct ec_host_response *)ec_dev->din;
103 ec_msg->result = response->result;
105 ret = cros_ec_check_result(ec_dev, ec_msg);
109 if (response->data_len > ec_msg->insize) {
110 dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
111 response->data_len, ec_msg->insize);
116 /* copy response packet payload and compute checksum */
117 memcpy(ec_msg->data, ec_dev->din + sizeof(*response),
121 for (i = 0; i < sizeof(*response) + response->data_len; i++)
122 sum += ec_dev->din[i];
125 dev_err(ec_dev->dev, "bad packet checksum, calculated %x\n",
131 ret = response->data_len;
133 if (ec_msg->command == EC_CMD_REBOOT_EC)
134 msleep(EC_REBOOT_DELAY_MS);
140 cros_ec_rpmsg_host_event_function(struct work_struct *host_event_work)
142 struct cros_ec_rpmsg *ec_rpmsg = container_of(host_event_work,
143 struct cros_ec_rpmsg,
145 struct cros_ec_device *ec_dev = dev_get_drvdata(&ec_rpmsg->rpdev->dev);
146 bool wake_event = true;
149 ret = cros_ec_get_next_event(ec_dev, &wake_event);
152 * Signal only if wake host events or any interrupt if
153 * cros_ec_get_next_event() returned an error (default value for
154 * wake_event is true)
156 if (wake_event && device_may_wakeup(ec_dev->dev))
157 pm_wakeup_event(ec_dev->dev, 0);
160 blocking_notifier_call_chain(&ec_dev->event_notifier,
164 static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
165 int len, void *priv, u32 src)
167 struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev);
168 struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv;
169 struct cros_ec_rpmsg_response *resp;
172 dev_warn(ec_dev->dev, "rpmsg received empty response");
177 len -= offsetof(struct cros_ec_rpmsg_response, data);
178 if (resp->type == HOST_COMMAND_MARK) {
179 if (len > ec_dev->din_size) {
180 dev_warn(ec_dev->dev,
181 "received length %d > din_size %d, truncating",
182 len, ec_dev->din_size);
183 len = ec_dev->din_size;
186 memcpy(ec_dev->din, resp->data, len);
187 complete(&ec_rpmsg->xfer_ack);
188 } else if (resp->type == HOST_EVENT_MARK) {
189 schedule_work(&ec_rpmsg->host_event_work);
191 dev_warn(ec_dev->dev, "rpmsg received invalid type = %d",
199 static struct rpmsg_endpoint *
200 cros_ec_rpmsg_create_ept(struct rpmsg_device *rpdev)
202 struct rpmsg_channel_info chinfo = {};
204 strscpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
205 chinfo.src = rpdev->src;
206 chinfo.dst = RPMSG_ADDR_ANY;
208 return rpmsg_create_ept(rpdev, cros_ec_rpmsg_callback, NULL, chinfo);
211 static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev)
213 struct device *dev = &rpdev->dev;
214 struct cros_ec_rpmsg *ec_rpmsg;
215 struct cros_ec_device *ec_dev;
218 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
222 ec_rpmsg = devm_kzalloc(dev, sizeof(*ec_rpmsg), GFP_KERNEL);
227 ec_dev->priv = ec_rpmsg;
228 ec_dev->cmd_xfer = cros_ec_cmd_xfer_rpmsg;
229 ec_dev->pkt_xfer = cros_ec_pkt_xfer_rpmsg;
230 ec_dev->phys_name = dev_name(&rpdev->dev);
231 ec_dev->din_size = sizeof(struct ec_host_response) +
232 sizeof(struct ec_response_get_protocol_info);
233 ec_dev->dout_size = sizeof(struct ec_host_request);
234 dev_set_drvdata(dev, ec_dev);
236 ec_rpmsg->rpdev = rpdev;
237 init_completion(&ec_rpmsg->xfer_ack);
238 INIT_WORK(&ec_rpmsg->host_event_work,
239 cros_ec_rpmsg_host_event_function);
241 ec_rpmsg->ept = cros_ec_rpmsg_create_ept(rpdev);
245 ret = cros_ec_register(ec_dev);
247 rpmsg_destroy_ept(ec_rpmsg->ept);
248 cancel_work_sync(&ec_rpmsg->host_event_work);
255 static void cros_ec_rpmsg_remove(struct rpmsg_device *rpdev)
257 struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev);
258 struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv;
260 cros_ec_unregister(ec_dev);
261 rpmsg_destroy_ept(ec_rpmsg->ept);
262 cancel_work_sync(&ec_rpmsg->host_event_work);
265 #ifdef CONFIG_PM_SLEEP
266 static int cros_ec_rpmsg_suspend(struct device *dev)
268 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
270 return cros_ec_suspend(ec_dev);
273 static int cros_ec_rpmsg_resume(struct device *dev)
275 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
277 return cros_ec_resume(ec_dev);
281 static SIMPLE_DEV_PM_OPS(cros_ec_rpmsg_pm_ops, cros_ec_rpmsg_suspend,
282 cros_ec_rpmsg_resume);
284 static const struct of_device_id cros_ec_rpmsg_of_match[] = {
285 { .compatible = "google,cros-ec-rpmsg", },
288 MODULE_DEVICE_TABLE(of, cros_ec_rpmsg_of_match);
290 static struct rpmsg_driver cros_ec_driver_rpmsg = {
292 .name = "cros-ec-rpmsg",
293 .of_match_table = cros_ec_rpmsg_of_match,
294 .pm = &cros_ec_rpmsg_pm_ops,
296 .probe = cros_ec_rpmsg_probe,
297 .remove = cros_ec_rpmsg_remove,
300 module_rpmsg_driver(cros_ec_driver_rpmsg);
302 MODULE_LICENSE("GPL v2");
303 MODULE_DESCRIPTION("ChromeOS EC multi function device (rpmsg)");