i2c-cros-ec-tunnel.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. request_len = ec_i2c_count_message(i2c_msgs, num);
  161. if (request_len < 0) {
  162. dev_warn(dev, "Error constructing message %d\n", request_len);
  163. result = request_len;
  164. goto exit;
  165. }
  166. response_len = ec_i2c_count_response(i2c_msgs, num);
  167. if (response_len < 0) {
  168. /* Unexpected; no errors should come when NULL response */
  169. dev_warn(dev, "Error preparing response %d\n", response_len);
  170. result = response_len;
  171. goto exit;
  172. }
  173. if (request_len <= ARRAY_SIZE(bus->request_buf)) {
  174. request = bus->request_buf;
  175. } else {
  176. request = kzalloc(request_len, GFP_KERNEL);
  177. if (request == NULL) {
  178. result = -ENOMEM;
  179. goto exit;
  180. }
  181. }
  182. if (response_len <= ARRAY_SIZE(bus->response_buf)) {
  183. response = bus->response_buf;
  184. } else {
  185. response = kzalloc(response_len, GFP_KERNEL);
  186. if (response == NULL) {
  187. result = -ENOMEM;
  188. goto exit;
  189. }
  190. }
  191. ec_i2c_construct_message(request, i2c_msgs, num, bus_num);
  192. result = bus->ec->command_sendrecv(bus->ec, EC_CMD_I2C_PASSTHRU,
  193. request, request_len,
  194. response, response_len);
  195. if (result)
  196. goto exit;
  197. result = ec_i2c_parse_response(response, i2c_msgs, &num);
  198. if (result < 0)
  199. goto exit;
  200. /* Indicate success by saying how many messages were sent */
  201. result = num;
  202. exit:
  203. if (request != bus->request_buf)
  204. kfree(request);
  205. if (response != bus->response_buf)
  206. kfree(response);
  207. return result;
  208. }
  209. static u32 ec_i2c_functionality(struct i2c_adapter *adap)
  210. {
  211. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  212. }
  213. static const struct i2c_algorithm ec_i2c_algorithm = {
  214. .master_xfer = ec_i2c_xfer,
  215. .functionality = ec_i2c_functionality,
  216. };
  217. static int ec_i2c_probe(struct platform_device *pdev)
  218. {
  219. struct device_node *np = pdev->dev.of_node;
  220. struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
  221. struct device *dev = &pdev->dev;
  222. struct ec_i2c_device *bus = NULL;
  223. u32 remote_bus;
  224. int err;
  225. if (!ec->command_sendrecv) {
  226. dev_err(dev, "Missing sendrecv\n");
  227. return -EINVAL;
  228. }
  229. bus = devm_kzalloc(dev, sizeof(*bus), GFP_KERNEL);
  230. if (bus == NULL)
  231. return -ENOMEM;
  232. err = of_property_read_u32(np, "google,remote-bus", &remote_bus);
  233. if (err) {
  234. dev_err(dev, "Couldn't read remote-bus property\n");
  235. return err;
  236. }
  237. bus->remote_bus = remote_bus;
  238. bus->ec = ec;
  239. bus->dev = dev;
  240. bus->adap.owner = THIS_MODULE;
  241. strlcpy(bus->adap.name, "cros-ec-i2c-tunnel", sizeof(bus->adap.name));
  242. bus->adap.algo = &ec_i2c_algorithm;
  243. bus->adap.algo_data = bus;
  244. bus->adap.dev.parent = &pdev->dev;
  245. bus->adap.dev.of_node = np;
  246. err = i2c_add_adapter(&bus->adap);
  247. if (err) {
  248. dev_err(dev, "cannot register i2c adapter\n");
  249. return err;
  250. }
  251. platform_set_drvdata(pdev, bus);
  252. return err;
  253. }
  254. static int ec_i2c_remove(struct platform_device *dev)
  255. {
  256. struct ec_i2c_device *bus = platform_get_drvdata(dev);
  257. i2c_del_adapter(&bus->adap);
  258. return 0;
  259. }
  260. static struct platform_driver ec_i2c_tunnel_driver = {
  261. .probe = ec_i2c_probe,
  262. .remove = ec_i2c_remove,
  263. .driver = {
  264. .name = "cros-ec-i2c-tunnel",
  265. },
  266. };
  267. module_platform_driver(ec_i2c_tunnel_driver);
  268. MODULE_LICENSE("GPL");
  269. MODULE_DESCRIPTION("EC I2C tunnel driver");
  270. MODULE_ALIAS("platform:cros-ec-i2c-tunnel");