cros_ec_i2c.c 9.5 KB

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