cros_ec_i2c.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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/delay.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/i2c.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/mfd/cros_ec.h>
  21. #include <linux/mfd/cros_ec_commands.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. /**
  25. * Request format for protocol v3
  26. * byte 0 0xda (EC_COMMAND_PROTOCOL_3)
  27. * byte 1-8 struct ec_host_request
  28. * byte 10- response data
  29. */
  30. struct ec_host_request_i2c {
  31. /* Always 0xda to backward compatible with v2 struct */
  32. uint8_t command_protocol;
  33. struct ec_host_request ec_request;
  34. } __packed;
  35. /*
  36. * Response format for protocol v3
  37. * byte 0 result code
  38. * byte 1 packet_length
  39. * byte 2-9 struct ec_host_response
  40. * byte 10- response data
  41. */
  42. struct ec_host_response_i2c {
  43. uint8_t result;
  44. uint8_t packet_length;
  45. struct ec_host_response ec_response;
  46. } __packed;
  47. static inline struct cros_ec_device *to_ec_dev(struct device *dev)
  48. {
  49. struct i2c_client *client = to_i2c_client(dev);
  50. return i2c_get_clientdata(client);
  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. i2c_msg[0].addr = client->addr;
  69. i2c_msg[0].flags = 0;
  70. i2c_msg[1].addr = client->addr;
  71. i2c_msg[1].flags = I2C_M_RD;
  72. packet_len = msg->insize + response_header_size;
  73. BUG_ON(packet_len > ec_dev->din_size);
  74. in_buf = ec_dev->din;
  75. i2c_msg[1].len = packet_len;
  76. i2c_msg[1].buf = (char *) in_buf;
  77. packet_len = msg->outsize + request_header_size;
  78. BUG_ON(packet_len > ec_dev->dout_size);
  79. out_buf = ec_dev->dout;
  80. i2c_msg[0].len = packet_len;
  81. i2c_msg[0].buf = (char *) out_buf;
  82. /* create request data */
  83. ec_request_i2c = (struct ec_host_request_i2c *) out_buf;
  84. ec_request_i2c->command_protocol = EC_COMMAND_PROTOCOL_3;
  85. ec_dev->dout++;
  86. ret = cros_ec_prepare_tx(ec_dev, msg);
  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. const struct i2c_device_id *dev_id)
  253. {
  254. struct device *dev = &client->dev;
  255. struct cros_ec_device *ec_dev = NULL;
  256. int err;
  257. ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
  258. if (!ec_dev)
  259. return -ENOMEM;
  260. i2c_set_clientdata(client, ec_dev);
  261. ec_dev->dev = dev;
  262. ec_dev->priv = client;
  263. ec_dev->irq = client->irq;
  264. ec_dev->cmd_xfer = cros_ec_cmd_xfer_i2c;
  265. ec_dev->pkt_xfer = cros_ec_pkt_xfer_i2c;
  266. ec_dev->phys_name = client->adapter->name;
  267. ec_dev->din_size = sizeof(struct ec_host_response_i2c) +
  268. sizeof(struct ec_response_get_protocol_info);
  269. ec_dev->dout_size = sizeof(struct ec_host_request_i2c);
  270. err = cros_ec_register(ec_dev);
  271. if (err) {
  272. dev_err(dev, "cannot register EC\n");
  273. return err;
  274. }
  275. return 0;
  276. }
  277. static int cros_ec_i2c_remove(struct i2c_client *client)
  278. {
  279. struct cros_ec_device *ec_dev = i2c_get_clientdata(client);
  280. cros_ec_remove(ec_dev);
  281. return 0;
  282. }
  283. #ifdef CONFIG_PM_SLEEP
  284. static int cros_ec_i2c_suspend(struct device *dev)
  285. {
  286. struct cros_ec_device *ec_dev = to_ec_dev(dev);
  287. return cros_ec_suspend(ec_dev);
  288. }
  289. static int cros_ec_i2c_resume(struct device *dev)
  290. {
  291. struct cros_ec_device *ec_dev = to_ec_dev(dev);
  292. return cros_ec_resume(ec_dev);
  293. }
  294. #endif
  295. static SIMPLE_DEV_PM_OPS(cros_ec_i2c_pm_ops, cros_ec_i2c_suspend,
  296. cros_ec_i2c_resume);
  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. static const struct i2c_device_id cros_ec_i2c_id[] = {
  303. { "cros-ec-i2c", 0 },
  304. { }
  305. };
  306. MODULE_DEVICE_TABLE(i2c, cros_ec_i2c_id);
  307. static struct i2c_driver cros_ec_driver = {
  308. .driver = {
  309. .name = "cros-ec-i2c",
  310. .of_match_table = of_match_ptr(cros_ec_i2c_of_match),
  311. .pm = &cros_ec_i2c_pm_ops,
  312. },
  313. .probe = cros_ec_i2c_probe,
  314. .remove = cros_ec_i2c_remove,
  315. .id_table = cros_ec_i2c_id,
  316. };
  317. module_i2c_driver(cros_ec_driver);
  318. MODULE_LICENSE("GPL");
  319. MODULE_DESCRIPTION("ChromeOS EC multi function device");