cros_ec_i2c.c 9.2 KB

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