1 // SPDX-License-Identifier: GPL-2.0
2 // I2C interface for ChromeOS Embedded Controller
3 //
4 // Copyright (C) 2012 Google, Inc
5 
6 #include <linux/acpi.h>
7 #include <linux/delay.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/platform_data/cros_ec_commands.h>
13 #include <linux/platform_data/cros_ec_proto.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 
17 #include "cros_ec.h"
18 
19 /*
20  * Request format for protocol v3
21  * byte 0	0xda (EC_COMMAND_PROTOCOL_3)
22  * byte 1-8	struct ec_host_request
23  * byte 10-	response data
24  */
25 struct ec_host_request_i2c {
26 	/* Always 0xda to backward compatible with v2 struct */
27 	uint8_t  command_protocol;
28 	struct ec_host_request ec_request;
29 } __packed;
30 
31 
32 /*
33  * Response format for protocol v3
34  * byte 0	result code
35  * byte 1	packet_length
36  * byte 2-9	struct ec_host_response
37  * byte 10-	response data
38  */
39 struct ec_host_response_i2c {
40 	uint8_t result;
41 	uint8_t packet_length;
42 	struct ec_host_response ec_response;
43 } __packed;
44 
45 static inline struct cros_ec_device *to_ec_dev(struct device *dev)
46 {
47 	struct i2c_client *client = to_i2c_client(dev);
48 
49 	return i2c_get_clientdata(client);
50 }
51 
52 static int cros_ec_pkt_xfer_i2c(struct cros_ec_device *ec_dev,
53 				struct cros_ec_command *msg)
54 {
55 	struct i2c_client *client = ec_dev->priv;
56 	int ret = -ENOMEM;
57 	int i;
58 	int packet_len;
59 	u8 *out_buf = NULL;
60 	u8 *in_buf = NULL;
61 	u8 sum;
62 	struct i2c_msg i2c_msg[2];
63 	struct ec_host_response *ec_response;
64 	struct ec_host_request_i2c *ec_request_i2c;
65 	struct ec_host_response_i2c *ec_response_i2c;
66 	int request_header_size = sizeof(struct ec_host_request_i2c);
67 	int response_header_size = sizeof(struct ec_host_response_i2c);
68 
69 	i2c_msg[0].addr = client->addr;
70 	i2c_msg[0].flags = 0;
71 	i2c_msg[1].addr = client->addr;
72 	i2c_msg[1].flags = I2C_M_RD;
73 
74 	packet_len = msg->insize + response_header_size;
75 	BUG_ON(packet_len > ec_dev->din_size);
76 	in_buf = ec_dev->din;
77 	i2c_msg[1].len = packet_len;
78 	i2c_msg[1].buf = (char *) in_buf;
79 
80 	packet_len = msg->outsize + request_header_size;
81 	BUG_ON(packet_len > ec_dev->dout_size);
82 	out_buf = ec_dev->dout;
83 	i2c_msg[0].len = packet_len;
84 	i2c_msg[0].buf = (char *) out_buf;
85 
86 	/* create request data */
87 	ec_request_i2c = (struct ec_host_request_i2c *) out_buf;
88 	ec_request_i2c->command_protocol = EC_COMMAND_PROTOCOL_3;
89 
90 	ec_dev->dout++;
91 	ret = cros_ec_prepare_tx(ec_dev, msg);
92 	ec_dev->dout--;
93 
94 	/* send command to EC and read answer */
95 	ret = i2c_transfer(client->adapter, i2c_msg, 2);
96 	if (ret < 0) {
97 		dev_dbg(ec_dev->dev, "i2c transfer failed: %d\n", ret);
98 		goto done;
99 	} else if (ret != 2) {
100 		dev_err(ec_dev->dev, "failed to get response: %d\n", ret);
101 		ret = -EIO;
102 		goto done;
103 	}
104 
105 	ec_response_i2c = (struct ec_host_response_i2c *) in_buf;
106 	msg->result = ec_response_i2c->result;
107 	ec_response = &ec_response_i2c->ec_response;
108 
109 	switch (msg->result) {
110 	case EC_RES_SUCCESS:
111 		break;
112 	case EC_RES_IN_PROGRESS:
113 		ret = -EAGAIN;
114 		dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
115 			msg->command);
116 		goto done;
117 
118 	default:
119 		dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
120 			msg->command, msg->result);
121 		/*
122 		 * When we send v3 request to v2 ec, ec won't recognize the
123 		 * 0xda (EC_COMMAND_PROTOCOL_3) and will return with status
124 		 * EC_RES_INVALID_COMMAND with zero data length.
125 		 *
126 		 * In case of invalid command for v3 protocol the data length
127 		 * will be at least sizeof(struct ec_host_response)
128 		 */
129 		if (ec_response_i2c->result == EC_RES_INVALID_COMMAND &&
130 		    ec_response_i2c->packet_length == 0) {
131 			ret = -EPROTONOSUPPORT;
132 			goto done;
133 		}
134 	}
135 
136 	if (ec_response_i2c->packet_length < sizeof(struct ec_host_response)) {
137 		dev_err(ec_dev->dev,
138 			"response of %u bytes too short; not a full header\n",
139 			ec_response_i2c->packet_length);
140 		ret = -EBADMSG;
141 		goto done;
142 	}
143 
144 	if (msg->insize < ec_response->data_len) {
145 		dev_err(ec_dev->dev,
146 			"response data size is too large: expected %u, got %u\n",
147 			msg->insize,
148 			ec_response->data_len);
149 		ret = -EMSGSIZE;
150 		goto done;
151 	}
152 
153 	/* copy response packet payload and compute checksum */
154 	sum = 0;
155 	for (i = 0; i < sizeof(struct ec_host_response); i++)
156 		sum += ((u8 *)ec_response)[i];
157 
158 	memcpy(msg->data,
159 	       in_buf + response_header_size,
160 	       ec_response->data_len);
161 	for (i = 0; i < ec_response->data_len; i++)
162 		sum += msg->data[i];
163 
164 	/* All bytes should sum to zero */
165 	if (sum) {
166 		dev_err(ec_dev->dev, "bad packet checksum\n");
167 		ret = -EBADMSG;
168 		goto done;
169 	}
170 
171 	ret = ec_response->data_len;
172 
173 done:
174 	if (msg->command == EC_CMD_REBOOT_EC)
175 		msleep(EC_REBOOT_DELAY_MS);
176 
177 	return ret;
178 }
179 
180 static int cros_ec_cmd_xfer_i2c(struct cros_ec_device *ec_dev,
181 				struct cros_ec_command *msg)
182 {
183 	struct i2c_client *client = ec_dev->priv;
184 	int ret = -ENOMEM;
185 	int i;
186 	int len;
187 	int packet_len;
188 	u8 *out_buf = NULL;
189 	u8 *in_buf = NULL;
190 	u8 sum;
191 	struct i2c_msg i2c_msg[2];
192 
193 	i2c_msg[0].addr = client->addr;
194 	i2c_msg[0].flags = 0;
195 	i2c_msg[1].addr = client->addr;
196 	i2c_msg[1].flags = I2C_M_RD;
197 
198 	/*
199 	 * allocate larger packet (one byte for checksum, one byte for
200 	 * length, and one for result code)
201 	 */
202 	packet_len = msg->insize + 3;
203 	in_buf = kzalloc(packet_len, GFP_KERNEL);
204 	if (!in_buf)
205 		goto done;
206 	i2c_msg[1].len = packet_len;
207 	i2c_msg[1].buf = (char *)in_buf;
208 
209 	/*
210 	 * allocate larger packet (one byte for checksum, one for
211 	 * command code, one for length, and one for command version)
212 	 */
213 	packet_len = msg->outsize + 4;
214 	out_buf = kzalloc(packet_len, GFP_KERNEL);
215 	if (!out_buf)
216 		goto done;
217 	i2c_msg[0].len = packet_len;
218 	i2c_msg[0].buf = (char *)out_buf;
219 
220 	out_buf[0] = EC_CMD_VERSION0 + msg->version;
221 	out_buf[1] = msg->command;
222 	out_buf[2] = msg->outsize;
223 
224 	/* copy message payload and compute checksum */
225 	sum = out_buf[0] + out_buf[1] + out_buf[2];
226 	for (i = 0; i < msg->outsize; i++) {
227 		out_buf[3 + i] = msg->data[i];
228 		sum += out_buf[3 + i];
229 	}
230 	out_buf[3 + msg->outsize] = sum;
231 
232 	/* send command to EC and read answer */
233 	ret = i2c_transfer(client->adapter, i2c_msg, 2);
234 	if (ret < 0) {
235 		dev_err(ec_dev->dev, "i2c transfer failed: %d\n", ret);
236 		goto done;
237 	} else if (ret != 2) {
238 		dev_err(ec_dev->dev, "failed to get response: %d\n", ret);
239 		ret = -EIO;
240 		goto done;
241 	}
242 
243 	/* check response error code */
244 	msg->result = i2c_msg[1].buf[0];
245 	ret = cros_ec_check_result(ec_dev, msg);
246 	if (ret)
247 		goto done;
248 
249 	len = in_buf[1];
250 	if (len > msg->insize) {
251 		dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
252 			len, msg->insize);
253 		ret = -ENOSPC;
254 		goto done;
255 	}
256 
257 	/* copy response packet payload and compute checksum */
258 	sum = in_buf[0] + in_buf[1];
259 	for (i = 0; i < len; i++) {
260 		msg->data[i] = in_buf[2 + i];
261 		sum += in_buf[2 + i];
262 	}
263 	dev_dbg(ec_dev->dev, "packet: %*ph, sum = %02x\n",
264 		i2c_msg[1].len, in_buf, sum);
265 	if (sum != in_buf[2 + len]) {
266 		dev_err(ec_dev->dev, "bad packet checksum\n");
267 		ret = -EBADMSG;
268 		goto done;
269 	}
270 
271 	ret = len;
272 done:
273 	kfree(in_buf);
274 	kfree(out_buf);
275 	if (msg->command == EC_CMD_REBOOT_EC)
276 		msleep(EC_REBOOT_DELAY_MS);
277 
278 	return ret;
279 }
280 
281 static int cros_ec_i2c_probe(struct i2c_client *client,
282 			     const struct i2c_device_id *dev_id)
283 {
284 	struct device *dev = &client->dev;
285 	struct cros_ec_device *ec_dev = NULL;
286 	int err;
287 
288 	ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
289 	if (!ec_dev)
290 		return -ENOMEM;
291 
292 	i2c_set_clientdata(client, ec_dev);
293 	ec_dev->dev = dev;
294 	ec_dev->priv = client;
295 	ec_dev->irq = client->irq;
296 	ec_dev->cmd_xfer = cros_ec_cmd_xfer_i2c;
297 	ec_dev->pkt_xfer = cros_ec_pkt_xfer_i2c;
298 	ec_dev->phys_name = client->adapter->name;
299 	ec_dev->din_size = sizeof(struct ec_host_response_i2c) +
300 			   sizeof(struct ec_response_get_protocol_info);
301 	ec_dev->dout_size = sizeof(struct ec_host_request_i2c);
302 
303 	err = cros_ec_register(ec_dev);
304 	if (err) {
305 		dev_err(dev, "cannot register EC\n");
306 		return err;
307 	}
308 
309 	return 0;
310 }
311 
312 static int cros_ec_i2c_remove(struct i2c_client *client)
313 {
314 	struct cros_ec_device *ec_dev = i2c_get_clientdata(client);
315 
316 	cros_ec_unregister(ec_dev);
317 
318 	return 0;
319 }
320 
321 #ifdef CONFIG_PM_SLEEP
322 static int cros_ec_i2c_suspend(struct device *dev)
323 {
324 	struct cros_ec_device *ec_dev = to_ec_dev(dev);
325 
326 	return cros_ec_suspend(ec_dev);
327 }
328 
329 static int cros_ec_i2c_resume(struct device *dev)
330 {
331 	struct cros_ec_device *ec_dev = to_ec_dev(dev);
332 
333 	return cros_ec_resume(ec_dev);
334 }
335 #endif
336 
337 static const struct dev_pm_ops cros_ec_i2c_pm_ops = {
338 	SET_LATE_SYSTEM_SLEEP_PM_OPS(cros_ec_i2c_suspend, cros_ec_i2c_resume)
339 };
340 
341 #ifdef CONFIG_OF
342 static const struct of_device_id cros_ec_i2c_of_match[] = {
343 	{ .compatible = "google,cros-ec-i2c", },
344 	{ /* sentinel */ },
345 };
346 MODULE_DEVICE_TABLE(of, cros_ec_i2c_of_match);
347 #endif
348 
349 static const struct i2c_device_id cros_ec_i2c_id[] = {
350 	{ "cros-ec-i2c", 0 },
351 	{ }
352 };
353 MODULE_DEVICE_TABLE(i2c, cros_ec_i2c_id);
354 
355 #ifdef CONFIG_ACPI
356 static const struct acpi_device_id cros_ec_i2c_acpi_id[] = {
357 	{ "GOOG0008", 0 },
358 	{ /* sentinel */ }
359 };
360 MODULE_DEVICE_TABLE(acpi, cros_ec_i2c_acpi_id);
361 #endif
362 
363 static struct i2c_driver cros_ec_driver = {
364 	.driver	= {
365 		.name	= "cros-ec-i2c",
366 		.acpi_match_table = ACPI_PTR(cros_ec_i2c_acpi_id),
367 		.of_match_table = of_match_ptr(cros_ec_i2c_of_match),
368 		.pm	= &cros_ec_i2c_pm_ops,
369 	},
370 	.probe		= cros_ec_i2c_probe,
371 	.remove		= cros_ec_i2c_remove,
372 	.id_table	= cros_ec_i2c_id,
373 };
374 
375 module_i2c_driver(cros_ec_driver);
376 
377 MODULE_LICENSE("GPL v2");
378 MODULE_DESCRIPTION("I2C interface for ChromeOS Embedded Controller");
379