xref: /openbmc/linux/drivers/i2c/busses/i2c-virtio.c (revision e2eb96ae)
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)))
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(&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(&msgs[i], 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, outcnt, incnt, &reqs[i], GFP_KERNEL)) {
101 			i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], 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 		wait_for_completion(&req->completion);
120 
121 		if (!failed && req->in_hdr.status != VIRTIO_I2C_MSG_OK)
122 			failed = true;
123 
124 		i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], !failed);
125 
126 		if (!failed)
127 			j++;
128 	}
129 
130 	return j;
131 }
132 
133 static int virtio_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
134 			   int num)
135 {
136 	struct virtio_i2c *vi = i2c_get_adapdata(adap);
137 	struct virtqueue *vq = vi->vq;
138 	struct virtio_i2c_req *reqs;
139 	int count;
140 
141 	reqs = kcalloc(num, sizeof(*reqs), GFP_KERNEL);
142 	if (!reqs)
143 		return -ENOMEM;
144 
145 	count = virtio_i2c_prepare_reqs(vq, reqs, msgs, num);
146 	if (!count)
147 		goto err_free;
148 
149 	/*
150 	 * For the case where count < num, i.e. we weren't able to queue all the
151 	 * msgs, ideally we should abort right away and return early, but some
152 	 * of the messages are already sent to the remote I2C controller and the
153 	 * virtqueue will be left in undefined state in that case. We kick the
154 	 * remote here to clear the virtqueue, so we can try another set of
155 	 * messages later on.
156 	 */
157 	virtqueue_kick(vq);
158 
159 	count = virtio_i2c_complete_reqs(vq, reqs, msgs, count);
160 
161 err_free:
162 	kfree(reqs);
163 	return count;
164 }
165 
166 static void virtio_i2c_del_vqs(struct virtio_device *vdev)
167 {
168 	virtio_reset_device(vdev);
169 	vdev->config->del_vqs(vdev);
170 }
171 
172 static int virtio_i2c_setup_vqs(struct virtio_i2c *vi)
173 {
174 	struct virtio_device *vdev = vi->vdev;
175 
176 	vi->vq = virtio_find_single_vq(vdev, virtio_i2c_msg_done, "msg");
177 	return PTR_ERR_OR_ZERO(vi->vq);
178 }
179 
180 static u32 virtio_i2c_func(struct i2c_adapter *adap)
181 {
182 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
183 }
184 
185 static struct i2c_algorithm virtio_algorithm = {
186 	.master_xfer = virtio_i2c_xfer,
187 	.functionality = virtio_i2c_func,
188 };
189 
190 static int virtio_i2c_probe(struct virtio_device *vdev)
191 {
192 	struct virtio_i2c *vi;
193 	int ret;
194 
195 	if (!virtio_has_feature(vdev, VIRTIO_I2C_F_ZERO_LENGTH_REQUEST)) {
196 		dev_err(&vdev->dev, "Zero-length request feature is mandatory\n");
197 		return -EINVAL;
198 	}
199 
200 	vi = devm_kzalloc(&vdev->dev, 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(vi->adap.name, sizeof(vi->adap.name),
213 		 "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(&vi->adap, 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(&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(&vi->adap);
237 	virtio_i2c_del_vqs(vdev);
238 }
239 
240 static 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(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