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>
18 #define EC_MSG_TIMEOUT_MS 200
19 #define HOST_COMMAND_MARK 1
20 #define HOST_EVENT_MARK 2
23 * struct cros_ec_rpmsg_response - rpmsg message format from from EC.
25 * @type: The type of message, should be either HOST_COMMAND_MARK or
26 * HOST_EVENT_MARK, representing that the message is a response to
27 * host command, or a host event.
28 * @data: ec_host_response for host command.
30 struct cros_ec_rpmsg_response
{
32 u8 data
[] __aligned(4);
36 * struct cros_ec_rpmsg - information about a EC over rpmsg.
38 * @rpdev: rpmsg device we are connected to
39 * @xfer_ack: completion for host command transfer.
40 * @host_event_work: Work struct for pending host event.
41 * @ept: The rpmsg endpoint of this channel.
42 * @has_pending_host_event: Boolean used to check if there is a pending event.
43 * @probe_done: Flag to indicate that probe is done.
45 struct cros_ec_rpmsg
{
46 struct rpmsg_device
*rpdev
;
47 struct completion xfer_ack
;
48 struct work_struct host_event_work
;
49 struct rpmsg_endpoint
*ept
;
50 bool has_pending_host_event
;
55 * cros_ec_cmd_xfer_rpmsg - Transfer a message over rpmsg and receive the reply
57 * @ec_dev: ChromeOS EC device
58 * @ec_msg: Message to transfer
60 * This is only used for old EC proto version, and is not supported for this
65 static int cros_ec_cmd_xfer_rpmsg(struct cros_ec_device
*ec_dev
,
66 struct cros_ec_command
*ec_msg
)
72 * cros_ec_pkt_xfer_rpmsg - Transfer a packet over rpmsg and receive the reply
74 * @ec_dev: ChromeOS EC device
75 * @ec_msg: Message to transfer
77 * Return: number of bytes of the reply on success or negative error code.
79 static int cros_ec_pkt_xfer_rpmsg(struct cros_ec_device
*ec_dev
,
80 struct cros_ec_command
*ec_msg
)
82 struct cros_ec_rpmsg
*ec_rpmsg
= ec_dev
->priv
;
83 struct ec_host_response
*response
;
84 unsigned long timeout
;
91 len
= cros_ec_prepare_tx(ec_dev
, ec_msg
);
92 dev_dbg(ec_dev
->dev
, "prepared, len=%d\n", len
);
94 reinit_completion(&ec_rpmsg
->xfer_ack
);
95 ret
= rpmsg_send(ec_rpmsg
->ept
, ec_dev
->dout
, len
);
97 dev_err(ec_dev
->dev
, "rpmsg send failed\n");
101 timeout
= msecs_to_jiffies(EC_MSG_TIMEOUT_MS
);
102 ret
= wait_for_completion_timeout(&ec_rpmsg
->xfer_ack
, timeout
);
104 dev_err(ec_dev
->dev
, "rpmsg send timeout\n");
108 /* check response error code */
109 response
= (struct ec_host_response
*)ec_dev
->din
;
110 ec_msg
->result
= response
->result
;
112 ret
= cros_ec_check_result(ec_dev
, ec_msg
);
116 if (response
->data_len
> ec_msg
->insize
) {
117 dev_err(ec_dev
->dev
, "packet too long (%d bytes, expected %d)",
118 response
->data_len
, ec_msg
->insize
);
123 /* copy response packet payload and compute checksum */
124 memcpy(ec_msg
->data
, ec_dev
->din
+ sizeof(*response
),
128 for (i
= 0; i
< sizeof(*response
) + response
->data_len
; i
++)
129 sum
+= ec_dev
->din
[i
];
132 dev_err(ec_dev
->dev
, "bad packet checksum, calculated %x\n",
138 ret
= response
->data_len
;
140 if (ec_msg
->command
== EC_CMD_REBOOT_EC
)
141 msleep(EC_REBOOT_DELAY_MS
);
147 cros_ec_rpmsg_host_event_function(struct work_struct
*host_event_work
)
149 struct cros_ec_rpmsg
*ec_rpmsg
= container_of(host_event_work
,
150 struct cros_ec_rpmsg
,
152 struct cros_ec_device
*ec_dev
= dev_get_drvdata(&ec_rpmsg
->rpdev
->dev
);
153 bool ec_has_more_events
;
156 ec_has_more_events
= cros_ec_handle_event(ec_dev
);
157 } while (ec_has_more_events
);
160 static int cros_ec_rpmsg_callback(struct rpmsg_device
*rpdev
, void *data
,
161 int len
, void *priv
, u32 src
)
163 struct cros_ec_device
*ec_dev
= dev_get_drvdata(&rpdev
->dev
);
164 struct cros_ec_rpmsg
*ec_rpmsg
= ec_dev
->priv
;
165 struct cros_ec_rpmsg_response
*resp
;
168 dev_warn(ec_dev
->dev
, "rpmsg received empty response");
173 len
-= offsetof(struct cros_ec_rpmsg_response
, data
);
174 if (resp
->type
== HOST_COMMAND_MARK
) {
175 if (len
> ec_dev
->din_size
) {
176 dev_warn(ec_dev
->dev
,
177 "received length %d > din_size %d, truncating",
178 len
, ec_dev
->din_size
);
179 len
= ec_dev
->din_size
;
182 memcpy(ec_dev
->din
, resp
->data
, len
);
183 complete(&ec_rpmsg
->xfer_ack
);
184 } else if (resp
->type
== HOST_EVENT_MARK
) {
186 * If the host event is sent before cros_ec_register is
187 * finished, queue the host event.
189 if (ec_rpmsg
->probe_done
)
190 schedule_work(&ec_rpmsg
->host_event_work
);
192 ec_rpmsg
->has_pending_host_event
= true;
194 dev_warn(ec_dev
->dev
, "rpmsg received invalid type = %d",
202 static struct rpmsg_endpoint
*
203 cros_ec_rpmsg_create_ept(struct rpmsg_device
*rpdev
)
205 struct rpmsg_channel_info chinfo
= {};
207 strscpy(chinfo
.name
, rpdev
->id
.name
, RPMSG_NAME_SIZE
);
208 chinfo
.src
= rpdev
->src
;
209 chinfo
.dst
= RPMSG_ADDR_ANY
;
211 return rpmsg_create_ept(rpdev
, cros_ec_rpmsg_callback
, NULL
, chinfo
);
214 static int cros_ec_rpmsg_probe(struct rpmsg_device
*rpdev
)
216 struct device
*dev
= &rpdev
->dev
;
217 struct cros_ec_rpmsg
*ec_rpmsg
;
218 struct cros_ec_device
*ec_dev
;
221 ec_dev
= devm_kzalloc(dev
, sizeof(*ec_dev
), GFP_KERNEL
);
225 ec_rpmsg
= devm_kzalloc(dev
, sizeof(*ec_rpmsg
), GFP_KERNEL
);
230 ec_dev
->priv
= ec_rpmsg
;
231 ec_dev
->cmd_xfer
= cros_ec_cmd_xfer_rpmsg
;
232 ec_dev
->pkt_xfer
= cros_ec_pkt_xfer_rpmsg
;
233 ec_dev
->phys_name
= dev_name(&rpdev
->dev
);
234 ec_dev
->din_size
= sizeof(struct ec_host_response
) +
235 sizeof(struct ec_response_get_protocol_info
);
236 ec_dev
->dout_size
= sizeof(struct ec_host_request
);
237 dev_set_drvdata(dev
, ec_dev
);
239 ec_rpmsg
->rpdev
= rpdev
;
240 init_completion(&ec_rpmsg
->xfer_ack
);
241 INIT_WORK(&ec_rpmsg
->host_event_work
,
242 cros_ec_rpmsg_host_event_function
);
244 ec_rpmsg
->ept
= cros_ec_rpmsg_create_ept(rpdev
);
248 ret
= cros_ec_register(ec_dev
);
250 rpmsg_destroy_ept(ec_rpmsg
->ept
);
251 cancel_work_sync(&ec_rpmsg
->host_event_work
);
255 ec_rpmsg
->probe_done
= true;
257 if (ec_rpmsg
->has_pending_host_event
)
258 schedule_work(&ec_rpmsg
->host_event_work
);
263 static void cros_ec_rpmsg_remove(struct rpmsg_device
*rpdev
)
265 struct cros_ec_device
*ec_dev
= dev_get_drvdata(&rpdev
->dev
);
266 struct cros_ec_rpmsg
*ec_rpmsg
= ec_dev
->priv
;
268 cros_ec_unregister(ec_dev
);
269 rpmsg_destroy_ept(ec_rpmsg
->ept
);
270 cancel_work_sync(&ec_rpmsg
->host_event_work
);
273 #ifdef CONFIG_PM_SLEEP
274 static int cros_ec_rpmsg_suspend(struct device
*dev
)
276 struct cros_ec_device
*ec_dev
= dev_get_drvdata(dev
);
278 return cros_ec_suspend(ec_dev
);
281 static int cros_ec_rpmsg_resume(struct device
*dev
)
283 struct cros_ec_device
*ec_dev
= dev_get_drvdata(dev
);
285 return cros_ec_resume(ec_dev
);
289 static SIMPLE_DEV_PM_OPS(cros_ec_rpmsg_pm_ops
, cros_ec_rpmsg_suspend
,
290 cros_ec_rpmsg_resume
);
292 static const struct of_device_id cros_ec_rpmsg_of_match
[] = {
293 { .compatible
= "google,cros-ec-rpmsg", },
296 MODULE_DEVICE_TABLE(of
, cros_ec_rpmsg_of_match
);
298 static struct rpmsg_driver cros_ec_driver_rpmsg
= {
300 .name
= "cros-ec-rpmsg",
301 .of_match_table
= cros_ec_rpmsg_of_match
,
302 .pm
= &cros_ec_rpmsg_pm_ops
,
304 .probe
= cros_ec_rpmsg_probe
,
305 .remove
= cros_ec_rpmsg_remove
,
308 module_rpmsg_driver(cros_ec_driver_rpmsg
);
310 MODULE_LICENSE("GPL v2");
311 MODULE_DESCRIPTION("ChromeOS EC multi function device (rpmsg)");