i2c-cros-ec-tunnel.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. int alloc_size;
  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. return request_len;
  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. return response_len;
  171. }
  172. alloc_size = max(request_len, response_len);
  173. msg = kmalloc(sizeof(*msg) + alloc_size, GFP_KERNEL);
  174. if (!msg)
  175. return -ENOMEM;
  176. result = ec_i2c_construct_message(msg->data, i2c_msgs, num, bus_num);
  177. if (result) {
  178. dev_err(dev, "Error constructing EC i2c message %d\n", result);
  179. goto exit;
  180. }
  181. msg->version = 0;
  182. msg->command = EC_CMD_I2C_PASSTHRU;
  183. msg->outsize = request_len;
  184. msg->insize = response_len;
  185. result = cros_ec_cmd_xfer_status(bus->ec, msg);
  186. if (result < 0) {
  187. dev_err(dev, "Error transferring EC i2c message %d\n", result);
  188. goto exit;
  189. }
  190. result = ec_i2c_parse_response(msg->data, i2c_msgs, &num);
  191. if (result < 0) {
  192. dev_err(dev, "Error parsing EC i2c message %d\n", result);
  193. goto exit;
  194. }
  195. /* Indicate success by saying how many messages were sent */
  196. result = num;
  197. exit:
  198. kfree(msg);
  199. return result;
  200. }
  201. static u32 ec_i2c_functionality(struct i2c_adapter *adap)
  202. {
  203. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  204. }
  205. static const struct i2c_algorithm ec_i2c_algorithm = {
  206. .master_xfer = ec_i2c_xfer,
  207. .functionality = ec_i2c_functionality,
  208. };
  209. static int ec_i2c_probe(struct platform_device *pdev)
  210. {
  211. struct device_node *np = pdev->dev.of_node;
  212. struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
  213. struct device *dev = &pdev->dev;
  214. struct ec_i2c_device *bus = NULL;
  215. u32 remote_bus;
  216. int err;
  217. if (!ec->cmd_xfer) {
  218. dev_err(dev, "Missing sendrecv\n");
  219. return -EINVAL;
  220. }
  221. bus = devm_kzalloc(dev, sizeof(*bus), GFP_KERNEL);
  222. if (bus == NULL)
  223. return -ENOMEM;
  224. err = of_property_read_u32(np, "google,remote-bus", &remote_bus);
  225. if (err) {
  226. dev_err(dev, "Couldn't read remote-bus property\n");
  227. return err;
  228. }
  229. bus->remote_bus = remote_bus;
  230. bus->ec = ec;
  231. bus->dev = dev;
  232. bus->adap.owner = THIS_MODULE;
  233. strlcpy(bus->adap.name, "cros-ec-i2c-tunnel", sizeof(bus->adap.name));
  234. bus->adap.algo = &ec_i2c_algorithm;
  235. bus->adap.algo_data = bus;
  236. bus->adap.dev.parent = &pdev->dev;
  237. bus->adap.dev.of_node = np;
  238. bus->adap.retries = I2C_MAX_RETRIES;
  239. err = i2c_add_adapter(&bus->adap);
  240. if (err) {
  241. dev_err(dev, "cannot register i2c adapter\n");
  242. return err;
  243. }
  244. platform_set_drvdata(pdev, bus);
  245. return err;
  246. }
  247. static int ec_i2c_remove(struct platform_device *dev)
  248. {
  249. struct ec_i2c_device *bus = platform_get_drvdata(dev);
  250. i2c_del_adapter(&bus->adap);
  251. return 0;
  252. }
  253. #ifdef CONFIG_OF
  254. static const struct of_device_id cros_ec_i2c_of_match[] = {
  255. { .compatible = "google,cros-ec-i2c-tunnel" },
  256. {},
  257. };
  258. MODULE_DEVICE_TABLE(of, cros_ec_i2c_of_match);
  259. #endif
  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. .of_match_table = of_match_ptr(cros_ec_i2c_of_match),
  266. },
  267. };
  268. module_platform_driver(ec_i2c_tunnel_driver);
  269. MODULE_LICENSE("GPL");
  270. MODULE_DESCRIPTION("EC I2C tunnel driver");
  271. MODULE_ALIAS("platform:cros-ec-i2c-tunnel");