i2c-virtio.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Virtio I2C Bus Driver
  4. *
  5. * The Virtio I2C Specification:
  6. * https://raw.githubusercontent.com/oasis-tcs/virtio-spec/master/virtio-i2c.tex
  7. *
  8. * Copyright (c) 2021 Intel Corporation. All rights reserved.
  9. */
  10. #include <linux/acpi.h>
  11. #include <linux/completion.h>
  12. #include <linux/err.h>
  13. #include <linux/i2c.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/virtio.h>
  17. #include <linux/virtio_ids.h>
  18. #include <linux/virtio_config.h>
  19. #include <linux/virtio_i2c.h>
  20. /**
  21. * struct virtio_i2c - virtio I2C data
  22. * @vdev: virtio device for this controller
  23. * @adap: I2C adapter for this controller
  24. * @vq: the virtio virtqueue for communication
  25. */
  26. struct virtio_i2c {
  27. struct virtio_device *vdev;
  28. struct i2c_adapter adap;
  29. struct virtqueue *vq;
  30. };
  31. /**
  32. * struct virtio_i2c_req - the virtio I2C request structure
  33. * @completion: completion of virtio I2C message
  34. * @out_hdr: the OUT header of the virtio I2C message
  35. * @buf: the buffer into which data is read, or from which it's written
  36. * @in_hdr: the IN header of the virtio I2C message
  37. */
  38. struct virtio_i2c_req {
  39. struct completion completion;
  40. struct virtio_i2c_out_hdr out_hdr ____cacheline_aligned;
  41. uint8_t *buf ____cacheline_aligned;
  42. struct virtio_i2c_in_hdr in_hdr ____cacheline_aligned;
  43. };
  44. static void virtio_i2c_msg_done(struct virtqueue *vq)
  45. {
  46. struct virtio_i2c_req *req;
  47. unsigned int len;
  48. while ((req = virtqueue_get_buf(vq, &len)))
  49. complete(&req->completion);
  50. }
  51. static int virtio_i2c_prepare_reqs(struct virtqueue *vq,
  52. struct virtio_i2c_req *reqs,
  53. struct i2c_msg *msgs, int num)
  54. {
  55. struct scatterlist *sgs[3], out_hdr, msg_buf, in_hdr;
  56. int i;
  57. for (i = 0; i < num; i++) {
  58. int outcnt = 0, incnt = 0;
  59. init_completion(&reqs[i].completion);
  60. /*
  61. * Only 7-bit mode supported for this moment. For the address
  62. * format, Please check the Virtio I2C Specification.
  63. */
  64. reqs[i].out_hdr.addr = cpu_to_le16(msgs[i].addr << 1);
  65. if (msgs[i].flags & I2C_M_RD)
  66. reqs[i].out_hdr.flags |= cpu_to_le32(VIRTIO_I2C_FLAGS_M_RD);
  67. if (i != num - 1)
  68. reqs[i].out_hdr.flags |= cpu_to_le32(VIRTIO_I2C_FLAGS_FAIL_NEXT);
  69. sg_init_one(&out_hdr, &reqs[i].out_hdr, sizeof(reqs[i].out_hdr));
  70. sgs[outcnt++] = &out_hdr;
  71. if (msgs[i].len) {
  72. reqs[i].buf = i2c_get_dma_safe_msg_buf(&msgs[i], 1);
  73. if (!reqs[i].buf)
  74. break;
  75. sg_init_one(&msg_buf, reqs[i].buf, msgs[i].len);
  76. if (msgs[i].flags & I2C_M_RD)
  77. sgs[outcnt + incnt++] = &msg_buf;
  78. else
  79. sgs[outcnt++] = &msg_buf;
  80. }
  81. sg_init_one(&in_hdr, &reqs[i].in_hdr, sizeof(reqs[i].in_hdr));
  82. sgs[outcnt + incnt++] = &in_hdr;
  83. if (virtqueue_add_sgs(vq, sgs, outcnt, incnt, &reqs[i], GFP_KERNEL)) {
  84. i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], false);
  85. break;
  86. }
  87. }
  88. return i;
  89. }
  90. static int virtio_i2c_complete_reqs(struct virtqueue *vq,
  91. struct virtio_i2c_req *reqs,
  92. struct i2c_msg *msgs, int num)
  93. {
  94. bool failed = false;
  95. int i, j = 0;
  96. for (i = 0; i < num; i++) {
  97. struct virtio_i2c_req *req = &reqs[i];
  98. wait_for_completion(&req->completion);
  99. if (!failed && req->in_hdr.status != VIRTIO_I2C_MSG_OK)
  100. failed = true;
  101. i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], !failed);
  102. if (!failed)
  103. j++;
  104. }
  105. return j;
  106. }
  107. static int virtio_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  108. int num)
  109. {
  110. struct virtio_i2c *vi = i2c_get_adapdata(adap);
  111. struct virtqueue *vq = vi->vq;
  112. struct virtio_i2c_req *reqs;
  113. int count;
  114. reqs = kcalloc(num, sizeof(*reqs), GFP_KERNEL);
  115. if (!reqs)
  116. return -ENOMEM;
  117. count = virtio_i2c_prepare_reqs(vq, reqs, msgs, num);
  118. if (!count)
  119. goto err_free;
  120. /*
  121. * For the case where count < num, i.e. we weren't able to queue all the
  122. * msgs, ideally we should abort right away and return early, but some
  123. * of the messages are already sent to the remote I2C controller and the
  124. * virtqueue will be left in undefined state in that case. We kick the
  125. * remote here to clear the virtqueue, so we can try another set of
  126. * messages later on.
  127. */
  128. virtqueue_kick(vq);
  129. count = virtio_i2c_complete_reqs(vq, reqs, msgs, count);
  130. err_free:
  131. kfree(reqs);
  132. return count;
  133. }
  134. static void virtio_i2c_del_vqs(struct virtio_device *vdev)
  135. {
  136. virtio_reset_device(vdev);
  137. vdev->config->del_vqs(vdev);
  138. }
  139. static int virtio_i2c_setup_vqs(struct virtio_i2c *vi)
  140. {
  141. struct virtio_device *vdev = vi->vdev;
  142. vi->vq = virtio_find_single_vq(vdev, virtio_i2c_msg_done, "msg");
  143. return PTR_ERR_OR_ZERO(vi->vq);
  144. }
  145. static u32 virtio_i2c_func(struct i2c_adapter *adap)
  146. {
  147. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  148. }
  149. static struct i2c_algorithm virtio_algorithm = {
  150. .master_xfer = virtio_i2c_xfer,
  151. .functionality = virtio_i2c_func,
  152. };
  153. static int virtio_i2c_probe(struct virtio_device *vdev)
  154. {
  155. struct virtio_i2c *vi;
  156. int ret;
  157. if (!virtio_has_feature(vdev, VIRTIO_I2C_F_ZERO_LENGTH_REQUEST)) {
  158. dev_err(&vdev->dev, "Zero-length request feature is mandatory\n");
  159. return -EINVAL;
  160. }
  161. vi = devm_kzalloc(&vdev->dev, sizeof(*vi), GFP_KERNEL);
  162. if (!vi)
  163. return -ENOMEM;
  164. vdev->priv = vi;
  165. vi->vdev = vdev;
  166. ret = virtio_i2c_setup_vqs(vi);
  167. if (ret)
  168. return ret;
  169. vi->adap.owner = THIS_MODULE;
  170. snprintf(vi->adap.name, sizeof(vi->adap.name),
  171. "i2c_virtio at virtio bus %d", vdev->index);
  172. vi->adap.algo = &virtio_algorithm;
  173. vi->adap.dev.parent = &vdev->dev;
  174. vi->adap.dev.of_node = vdev->dev.of_node;
  175. i2c_set_adapdata(&vi->adap, vi);
  176. /*
  177. * Setup ACPI node for controlled devices which will be probed through
  178. * ACPI.
  179. */
  180. ACPI_COMPANION_SET(&vi->adap.dev, ACPI_COMPANION(vdev->dev.parent));
  181. ret = i2c_add_adapter(&vi->adap);
  182. if (ret)
  183. virtio_i2c_del_vqs(vdev);
  184. return ret;
  185. }
  186. static void virtio_i2c_remove(struct virtio_device *vdev)
  187. {
  188. struct virtio_i2c *vi = vdev->priv;
  189. i2c_del_adapter(&vi->adap);
  190. virtio_i2c_del_vqs(vdev);
  191. }
  192. static struct virtio_device_id id_table[] = {
  193. { VIRTIO_ID_I2C_ADAPTER, VIRTIO_DEV_ANY_ID },
  194. {}
  195. };
  196. MODULE_DEVICE_TABLE(virtio, id_table);
  197. #ifdef CONFIG_PM_SLEEP
  198. static int virtio_i2c_freeze(struct virtio_device *vdev)
  199. {
  200. virtio_i2c_del_vqs(vdev);
  201. return 0;
  202. }
  203. static int virtio_i2c_restore(struct virtio_device *vdev)
  204. {
  205. return virtio_i2c_setup_vqs(vdev->priv);
  206. }
  207. #endif
  208. static const unsigned int features[] = {
  209. VIRTIO_I2C_F_ZERO_LENGTH_REQUEST,
  210. };
  211. static struct virtio_driver virtio_i2c_driver = {
  212. .feature_table = features,
  213. .feature_table_size = ARRAY_SIZE(features),
  214. .id_table = id_table,
  215. .probe = virtio_i2c_probe,
  216. .remove = virtio_i2c_remove,
  217. .driver = {
  218. .name = "i2c_virtio",
  219. },
  220. #ifdef CONFIG_PM_SLEEP
  221. .freeze = virtio_i2c_freeze,
  222. .restore = virtio_i2c_restore,
  223. #endif
  224. };
  225. module_virtio_driver(virtio_i2c_driver);
  226. MODULE_AUTHOR("Jie Deng <[email protected]>");
  227. MODULE_AUTHOR("Conghui Chen <[email protected]>");
  228. MODULE_DESCRIPTION("Virtio i2c bus driver");
  229. MODULE_LICENSE("GPL");