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