i2c-cros-ec-tunnel.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (C) 2013 Google, Inc
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * Expose an I2C passthrough to the ChromeOS EC.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/i2c.h>
  13. #include <linux/mfd/cros_ec.h>
  14. #include <linux/mfd/cros_ec_commands.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #define I2C_MAX_RETRIES 3
  18. /**
  19. * struct ec_i2c_device - Driver data for I2C tunnel
  20. *
  21. * @dev: Device node
  22. * @adap: I2C adapter
  23. * @ec: Pointer to EC device
  24. * @remote_bus: The EC bus number we tunnel to on the other side.
  25. * @request_buf: Buffer for transmitting data; we expect most transfers to fit.
  26. * @response_buf: Buffer for receiving data; we expect most transfers to fit.
  27. */
  28. struct ec_i2c_device {
  29. struct device *dev;
  30. struct i2c_adapter adap;
  31. struct cros_ec_device *ec;
  32. u16 remote_bus;
  33. u8 request_buf[256];
  34. u8 response_buf[256];
  35. };
  36. /**
  37. * ec_i2c_count_message - Count bytes needed for ec_i2c_construct_message
  38. *
  39. * @i2c_msgs: The i2c messages to read
  40. * @num: The number of i2c messages.
  41. *
  42. * Returns the number of bytes the messages will take up.
  43. */
  44. static int ec_i2c_count_message(const struct i2c_msg i2c_msgs[], int num)
  45. {
  46. int i;
  47. int size;
  48. size = sizeof(struct ec_params_i2c_passthru);
  49. size += num * sizeof(struct ec_params_i2c_passthru_msg);
  50. for (i = 0; i < num; i++)
  51. if (!(i2c_msgs[i].flags & I2C_M_RD))
  52. size += i2c_msgs[i].len;
  53. return size;
  54. }
  55. /**
  56. * ec_i2c_construct_message - construct a message to go to the EC
  57. *
  58. * This function effectively stuffs the standard i2c_msg format of Linux into
  59. * a format that the EC understands.
  60. *
  61. * @buf: The buffer to fill. We assume that the buffer is big enough.
  62. * @i2c_msgs: The i2c messages to read.
  63. * @num: The number of i2c messages.
  64. * @bus_num: The remote bus number we want to talk to.
  65. *
  66. * Returns 0 or a negative error number.
  67. */
  68. static int ec_i2c_construct_message(u8 *buf, const struct i2c_msg i2c_msgs[],
  69. int num, u16 bus_num)
  70. {
  71. struct ec_params_i2c_passthru *params;
  72. u8 *out_data;
  73. int i;
  74. out_data = buf + sizeof(struct ec_params_i2c_passthru) +
  75. num * sizeof(struct ec_params_i2c_passthru_msg);
  76. params = (struct ec_params_i2c_passthru *)buf;
  77. params->port = bus_num;
  78. params->num_msgs = num;
  79. for (i = 0; i < num; i++) {
  80. const struct i2c_msg *i2c_msg = &i2c_msgs[i];
  81. struct ec_params_i2c_passthru_msg *msg = &params->msg[i];
  82. msg->len = i2c_msg->len;
  83. msg->addr_flags = i2c_msg->addr;
  84. if (i2c_msg->flags & I2C_M_TEN)
  85. return -EINVAL;
  86. if (i2c_msg->flags & I2C_M_RD) {
  87. msg->addr_flags |= EC_I2C_FLAG_READ;
  88. } else {
  89. memcpy(out_data, i2c_msg->buf, msg->len);
  90. out_data += msg->len;
  91. }
  92. }
  93. return 0;
  94. }
  95. /**
  96. * ec_i2c_count_response - Count bytes needed for ec_i2c_parse_response
  97. *
  98. * @i2c_msgs: The i2c messages to to fill up.
  99. * @num: The number of i2c messages expected.
  100. *
  101. * Returns the number of response bytes expeced.
  102. */
  103. static int ec_i2c_count_response(struct i2c_msg i2c_msgs[], int num)
  104. {
  105. int size;
  106. int i;
  107. size = sizeof(struct ec_response_i2c_passthru);
  108. for (i = 0; i < num; i++)
  109. if (i2c_msgs[i].flags & I2C_M_RD)
  110. size += i2c_msgs[i].len;
  111. return size;
  112. }
  113. /**
  114. * ec_i2c_parse_response - Parse a response from the EC
  115. *
  116. * We'll take the EC's response and copy it back into msgs.
  117. *
  118. * @buf: The buffer to parse.
  119. * @i2c_msgs: The i2c messages to to fill up.
  120. * @num: The number of i2c messages; will be modified to include the actual
  121. * number received.
  122. *
  123. * Returns 0 or a negative error number.
  124. */
  125. static int ec_i2c_parse_response(const u8 *buf, struct i2c_msg i2c_msgs[],
  126. int *num)
  127. {
  128. const struct ec_response_i2c_passthru *resp;
  129. const u8 *in_data;
  130. int i;
  131. in_data = buf + sizeof(struct ec_response_i2c_passthru);
  132. resp = (const struct ec_response_i2c_passthru *)buf;
  133. if (resp->i2c_status & EC_I2C_STATUS_TIMEOUT)
  134. return -ETIMEDOUT;
  135. else if (resp->i2c_status & EC_I2C_STATUS_ERROR)
  136. return -EREMOTEIO;
  137. /* Other side could send us back fewer messages, but not more */
  138. if (resp->num_msgs > *num)
  139. return -EPROTO;
  140. *num = resp->num_msgs;
  141. for (i = 0; i < *num; i++) {
  142. struct i2c_msg *i2c_msg = &i2c_msgs[i];
  143. if (i2c_msgs[i].flags & I2C_M_RD) {
  144. memcpy(i2c_msg->buf, in_data, i2c_msg->len);
  145. in_data += i2c_msg->len;
  146. }
  147. }
  148. return 0;
  149. }
  150. static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[],
  151. int num)
  152. {
  153. struct ec_i2c_device *bus = adap->algo_data;
  154. struct device *dev = bus->dev;
  155. const u16 bus_num = bus->remote_bus;
  156. int request_len;
  157. int response_len;
  158. u8 *request = NULL;
  159. u8 *response = NULL;
  160. int result;
  161. struct cros_ec_command msg;
  162. request_len = ec_i2c_count_message(i2c_msgs, num);
  163. if (request_len < 0) {
  164. dev_warn(dev, "Error constructing message %d\n", request_len);
  165. result = request_len;
  166. goto exit;
  167. }
  168. response_len = ec_i2c_count_response(i2c_msgs, num);
  169. if (response_len < 0) {
  170. /* Unexpected; no errors should come when NULL response */
  171. dev_warn(dev, "Error preparing response %d\n", response_len);
  172. result = response_len;
  173. goto exit;
  174. }
  175. if (request_len <= ARRAY_SIZE(bus->request_buf)) {
  176. request = bus->request_buf;
  177. } else {
  178. request = kzalloc(request_len, GFP_KERNEL);
  179. if (request == NULL) {
  180. result = -ENOMEM;
  181. goto exit;
  182. }
  183. }
  184. if (response_len <= ARRAY_SIZE(bus->response_buf)) {
  185. response = bus->response_buf;
  186. } else {
  187. response = kzalloc(response_len, GFP_KERNEL);
  188. if (response == NULL) {
  189. result = -ENOMEM;
  190. goto exit;
  191. }
  192. }
  193. result = ec_i2c_construct_message(request, i2c_msgs, num, bus_num);
  194. if (result)
  195. goto exit;
  196. msg.version = 0;
  197. msg.command = EC_CMD_I2C_PASSTHRU;
  198. msg.outdata = request;
  199. msg.outsize = request_len;
  200. msg.indata = response;
  201. msg.insize = response_len;
  202. result = cros_ec_cmd_xfer(bus->ec, &msg);
  203. if (result < 0)
  204. goto exit;
  205. result = ec_i2c_parse_response(response, i2c_msgs, &num);
  206. if (result < 0)
  207. goto exit;
  208. /* Indicate success by saying how many messages were sent */
  209. result = num;
  210. exit:
  211. if (request != bus->request_buf)
  212. kfree(request);
  213. if (response != bus->response_buf)
  214. kfree(response);
  215. return result;
  216. }
  217. static u32 ec_i2c_functionality(struct i2c_adapter *adap)
  218. {
  219. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  220. }
  221. static const struct i2c_algorithm ec_i2c_algorithm = {
  222. .master_xfer = ec_i2c_xfer,
  223. .functionality = ec_i2c_functionality,
  224. };
  225. static int ec_i2c_probe(struct platform_device *pdev)
  226. {
  227. struct device_node *np = pdev->dev.of_node;
  228. struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
  229. struct device *dev = &pdev->dev;
  230. struct ec_i2c_device *bus = NULL;
  231. u32 remote_bus;
  232. int err;
  233. if (!ec->cmd_xfer) {
  234. dev_err(dev, "Missing sendrecv\n");
  235. return -EINVAL;
  236. }
  237. bus = devm_kzalloc(dev, sizeof(*bus), GFP_KERNEL);
  238. if (bus == NULL)
  239. return -ENOMEM;
  240. err = of_property_read_u32(np, "google,remote-bus", &remote_bus);
  241. if (err) {
  242. dev_err(dev, "Couldn't read remote-bus property\n");
  243. return err;
  244. }
  245. bus->remote_bus = remote_bus;
  246. bus->ec = ec;
  247. bus->dev = dev;
  248. bus->adap.owner = THIS_MODULE;
  249. strlcpy(bus->adap.name, "cros-ec-i2c-tunnel", sizeof(bus->adap.name));
  250. bus->adap.algo = &ec_i2c_algorithm;
  251. bus->adap.algo_data = bus;
  252. bus->adap.dev.parent = &pdev->dev;
  253. bus->adap.dev.of_node = np;
  254. bus->adap.retries = I2C_MAX_RETRIES;
  255. err = i2c_add_adapter(&bus->adap);
  256. if (err) {
  257. dev_err(dev, "cannot register i2c adapter\n");
  258. return err;
  259. }
  260. platform_set_drvdata(pdev, bus);
  261. return err;
  262. }
  263. static int ec_i2c_remove(struct platform_device *dev)
  264. {
  265. struct ec_i2c_device *bus = platform_get_drvdata(dev);
  266. i2c_del_adapter(&bus->adap);
  267. return 0;
  268. }
  269. #ifdef CONFIG_OF
  270. static const struct of_device_id cros_ec_i2c_of_match[] = {
  271. { .compatible = "google,cros-ec-i2c-tunnel" },
  272. {},
  273. };
  274. MODULE_DEVICE_TABLE(of, cros_ec_i2c_of_match);
  275. #endif
  276. static struct platform_driver ec_i2c_tunnel_driver = {
  277. .probe = ec_i2c_probe,
  278. .remove = ec_i2c_remove,
  279. .driver = {
  280. .name = "cros-ec-i2c-tunnel",
  281. .of_match_table = of_match_ptr(cros_ec_i2c_of_match),
  282. },
  283. };
  284. module_platform_driver(ec_i2c_tunnel_driver);
  285. MODULE_LICENSE("GPL");
  286. MODULE_DESCRIPTION("EC I2C tunnel driver");
  287. MODULE_ALIAS("platform:cros-ec-i2c-tunnel");