i2c-cros-ec-tunnel.c 7.8 KB

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