i2c-cros-ec-tunnel.c 7.9 KB

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