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
virtio_i2c_msg_done(struct virtqueue * vq)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)))
54 complete(&req->completion);
55 }
56
virtio_i2c_prepare_reqs(struct virtqueue * vq,struct virtio_i2c_req * reqs,struct i2c_msg * msgs,int num)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(&reqs[i].completion);
68
69 /*
70 * We don't support 0 length messages and so filter out
71 * 0 length transfers by using i2c_adapter_quirks.
72 */
73 if (!msgs[i].len)
74 break;
75
76 /*
77 * Only 7-bit mode supported for this moment. For the address
78 * format, Please check the Virtio I2C Specification.
79 */
80 reqs[i].out_hdr.addr = cpu_to_le16(msgs[i].addr << 1);
81
82 if (i != num - 1)
83 reqs[i].out_hdr.flags = cpu_to_le32(VIRTIO_I2C_FLAGS_FAIL_NEXT);
84
85 sg_init_one(&out_hdr, &reqs[i].out_hdr, sizeof(reqs[i].out_hdr));
86 sgs[outcnt++] = &out_hdr;
87
88 reqs[i].buf = i2c_get_dma_safe_msg_buf(&msgs[i], 1);
89 if (!reqs[i].buf)
90 break;
91
92 sg_init_one(&msg_buf, reqs[i].buf, msgs[i].len);
93
94 if (msgs[i].flags & I2C_M_RD)
95 sgs[outcnt + incnt++] = &msg_buf;
96 else
97 sgs[outcnt++] = &msg_buf;
98
99 sg_init_one(&in_hdr, &reqs[i].in_hdr, sizeof(reqs[i].in_hdr));
100 sgs[outcnt + incnt++] = &in_hdr;
101
102 if (virtqueue_add_sgs(vq, sgs, outcnt, incnt, &reqs[i], GFP_KERNEL)) {
103 i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], false);
104 break;
105 }
106 }
107
108 return i;
109 }
110
virtio_i2c_complete_reqs(struct virtqueue * vq,struct virtio_i2c_req * reqs,struct i2c_msg * msgs,int num)111 static int virtio_i2c_complete_reqs(struct virtqueue *vq,
112 struct virtio_i2c_req *reqs,
113 struct i2c_msg *msgs, int num)
114 {
115 bool failed = false;
116 int i, j = 0;
117
118 for (i = 0; i < num; i++) {
119 struct virtio_i2c_req *req = &reqs[i];
120
121 wait_for_completion(&req->completion);
122
123 if (!failed && req->in_hdr.status != VIRTIO_I2C_MSG_OK)
124 failed = true;
125
126 i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], !failed);
127
128 if (!failed)
129 j++;
130 }
131
132 return j;
133 }
134
virtio_i2c_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)135 static int virtio_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
136 int num)
137 {
138 struct virtio_i2c *vi = i2c_get_adapdata(adap);
139 struct virtqueue *vq = vi->vq;
140 struct virtio_i2c_req *reqs;
141 int count;
142
143 reqs = kcalloc(num, sizeof(*reqs), GFP_KERNEL);
144 if (!reqs)
145 return -ENOMEM;
146
147 count = virtio_i2c_prepare_reqs(vq, reqs, msgs, num);
148 if (!count)
149 goto err_free;
150
151 /*
152 * For the case where count < num, i.e. we weren't able to queue all the
153 * msgs, ideally we should abort right away and return early, but some
154 * of the messages are already sent to the remote I2C controller and the
155 * virtqueue will be left in undefined state in that case. We kick the
156 * remote here to clear the virtqueue, so we can try another set of
157 * messages later on.
158 */
159 virtqueue_kick(vq);
160
161 count = virtio_i2c_complete_reqs(vq, reqs, msgs, count);
162
163 err_free:
164 kfree(reqs);
165 return count;
166 }
167
virtio_i2c_del_vqs(struct virtio_device * vdev)168 static void virtio_i2c_del_vqs(struct virtio_device *vdev)
169 {
170 vdev->config->reset(vdev);
171 vdev->config->del_vqs(vdev);
172 }
173
virtio_i2c_setup_vqs(struct virtio_i2c * vi)174 static int virtio_i2c_setup_vqs(struct virtio_i2c *vi)
175 {
176 struct virtio_device *vdev = vi->vdev;
177
178 vi->vq = virtio_find_single_vq(vdev, virtio_i2c_msg_done, "msg");
179 return PTR_ERR_OR_ZERO(vi->vq);
180 }
181
virtio_i2c_func(struct i2c_adapter * adap)182 static u32 virtio_i2c_func(struct i2c_adapter *adap)
183 {
184 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
185 }
186
187 static struct i2c_algorithm virtio_algorithm = {
188 .master_xfer = virtio_i2c_xfer,
189 .functionality = virtio_i2c_func,
190 };
191
192 static const struct i2c_adapter_quirks virtio_i2c_quirks = {
193 .flags = I2C_AQ_NO_ZERO_LEN,
194 };
195
virtio_i2c_probe(struct virtio_device * vdev)196 static int virtio_i2c_probe(struct virtio_device *vdev)
197 {
198 struct virtio_i2c *vi;
199 int ret;
200
201 vi = devm_kzalloc(&vdev->dev, sizeof(*vi), GFP_KERNEL);
202 if (!vi)
203 return -ENOMEM;
204
205 vdev->priv = vi;
206 vi->vdev = vdev;
207
208 ret = virtio_i2c_setup_vqs(vi);
209 if (ret)
210 return ret;
211
212 vi->adap.owner = THIS_MODULE;
213 snprintf(vi->adap.name, sizeof(vi->adap.name),
214 "i2c_virtio at virtio bus %d", vdev->index);
215 vi->adap.algo = &virtio_algorithm;
216 vi->adap.quirks = &virtio_i2c_quirks;
217 vi->adap.dev.parent = &vdev->dev;
218 vi->adap.dev.of_node = vdev->dev.of_node;
219 i2c_set_adapdata(&vi->adap, vi);
220
221 /*
222 * Setup ACPI node for controlled devices which will be probed through
223 * ACPI.
224 */
225 ACPI_COMPANION_SET(&vi->adap.dev, ACPI_COMPANION(vdev->dev.parent));
226
227 ret = i2c_add_adapter(&vi->adap);
228 if (ret)
229 virtio_i2c_del_vqs(vdev);
230
231 return ret;
232 }
233
virtio_i2c_remove(struct virtio_device * vdev)234 static void virtio_i2c_remove(struct virtio_device *vdev)
235 {
236 struct virtio_i2c *vi = vdev->priv;
237
238 i2c_del_adapter(&vi->adap);
239 virtio_i2c_del_vqs(vdev);
240 }
241
242 static struct virtio_device_id id_table[] = {
243 { VIRTIO_ID_I2C_ADAPTER, VIRTIO_DEV_ANY_ID },
244 {}
245 };
246 MODULE_DEVICE_TABLE(virtio, id_table);
247
248 #ifdef CONFIG_PM_SLEEP
virtio_i2c_freeze(struct virtio_device * vdev)249 static int virtio_i2c_freeze(struct virtio_device *vdev)
250 {
251 virtio_i2c_del_vqs(vdev);
252 return 0;
253 }
254
virtio_i2c_restore(struct virtio_device * vdev)255 static int virtio_i2c_restore(struct virtio_device *vdev)
256 {
257 return virtio_i2c_setup_vqs(vdev->priv);
258 }
259 #endif
260
261 static struct virtio_driver virtio_i2c_driver = {
262 .id_table = id_table,
263 .probe = virtio_i2c_probe,
264 .remove = virtio_i2c_remove,
265 .driver = {
266 .name = "i2c_virtio",
267 },
268 #ifdef CONFIG_PM_SLEEP
269 .freeze = virtio_i2c_freeze,
270 .restore = virtio_i2c_restore,
271 #endif
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