1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * GPIO driver for virtio-based virtual GPIO controllers
4 *
5 * Copyright (C) 2021 metux IT consult
6 * Enrico Weigelt, metux IT consult <info@metux.net>
7 *
8 * Copyright (C) 2021 Linaro.
9 * Viresh Kumar <viresh.kumar@linaro.org>
10 */
11
12 #include <linux/completion.h>
13 #include <linux/err.h>
14 #include <linux/gpio/driver.h>
15 #include <linux/io.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/virtio_config.h>
20 #include <uapi/linux/virtio_gpio.h>
21 #include <uapi/linux/virtio_ids.h>
22
23 struct virtio_gpio_line {
24 struct mutex lock; /* Protects line operation */
25 struct completion completion;
26 struct virtio_gpio_request req ____cacheline_aligned;
27 struct virtio_gpio_response res ____cacheline_aligned;
28 unsigned int rxlen;
29 };
30
31 struct virtio_gpio {
32 struct virtio_device *vdev;
33 struct mutex lock; /* Protects virtqueue operation */
34 struct gpio_chip gc;
35 struct virtio_gpio_line *lines;
36 struct virtqueue *request_vq;
37 };
38
_virtio_gpio_req(struct virtio_gpio * vgpio,u16 type,u16 gpio,u8 txvalue,u8 * rxvalue,void * response,u32 rxlen)39 static int _virtio_gpio_req(struct virtio_gpio *vgpio, u16 type, u16 gpio,
40 u8 txvalue, u8 *rxvalue, void *response, u32 rxlen)
41 {
42 struct virtio_gpio_line *line = &vgpio->lines[gpio];
43 struct virtio_gpio_request *req = &line->req;
44 struct virtio_gpio_response *res = response;
45 struct scatterlist *sgs[2], req_sg, res_sg;
46 struct device *dev = &vgpio->vdev->dev;
47 int ret;
48
49 /*
50 * Prevent concurrent requests for the same line since we have
51 * pre-allocated request/response buffers for each GPIO line. Moreover
52 * Linux always accesses a GPIO line sequentially, so this locking shall
53 * always go through without any delays.
54 */
55 mutex_lock(&line->lock);
56
57 req->type = cpu_to_le16(type);
58 req->gpio = cpu_to_le16(gpio);
59 req->value = cpu_to_le32(txvalue);
60
61 sg_init_one(&req_sg, req, sizeof(*req));
62 sg_init_one(&res_sg, res, rxlen);
63 sgs[0] = &req_sg;
64 sgs[1] = &res_sg;
65
66 line->rxlen = 0;
67 reinit_completion(&line->completion);
68
69 /*
70 * Virtqueue callers need to ensure they don't call its APIs with other
71 * virtqueue operations at the same time.
72 */
73 mutex_lock(&vgpio->lock);
74 ret = virtqueue_add_sgs(vgpio->request_vq, sgs, 1, 1, line, GFP_KERNEL);
75 if (ret) {
76 dev_err(dev, "failed to add request to vq\n");
77 mutex_unlock(&vgpio->lock);
78 goto out;
79 }
80
81 virtqueue_kick(vgpio->request_vq);
82 mutex_unlock(&vgpio->lock);
83
84 wait_for_completion(&line->completion);
85
86 if (unlikely(res->status != VIRTIO_GPIO_STATUS_OK)) {
87 dev_err(dev, "GPIO request failed: %d\n", gpio);
88 ret = -EINVAL;
89 goto out;
90 }
91
92 if (unlikely(line->rxlen != rxlen)) {
93 dev_err(dev, "GPIO operation returned incorrect len (%u : %u)\n",
94 rxlen, line->rxlen);
95 ret = -EINVAL;
96 goto out;
97 }
98
99 if (rxvalue)
100 *rxvalue = res->value;
101
102 out:
103 mutex_unlock(&line->lock);
104 return ret;
105 }
106
virtio_gpio_req(struct virtio_gpio * vgpio,u16 type,u16 gpio,u8 txvalue,u8 * rxvalue)107 static int virtio_gpio_req(struct virtio_gpio *vgpio, u16 type, u16 gpio,
108 u8 txvalue, u8 *rxvalue)
109 {
110 struct virtio_gpio_line *line = &vgpio->lines[gpio];
111 struct virtio_gpio_response *res = &line->res;
112
113 return _virtio_gpio_req(vgpio, type, gpio, txvalue, rxvalue, res,
114 sizeof(*res));
115 }
116
virtio_gpio_free(struct gpio_chip * gc,unsigned int gpio)117 static void virtio_gpio_free(struct gpio_chip *gc, unsigned int gpio)
118 {
119 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
120
121 virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_DIRECTION, gpio,
122 VIRTIO_GPIO_DIRECTION_NONE, NULL);
123 }
124
virtio_gpio_get_direction(struct gpio_chip * gc,unsigned int gpio)125 static int virtio_gpio_get_direction(struct gpio_chip *gc, unsigned int gpio)
126 {
127 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
128 u8 direction;
129 int ret;
130
131 ret = virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_GET_DIRECTION, gpio, 0,
132 &direction);
133 if (ret)
134 return ret;
135
136 switch (direction) {
137 case VIRTIO_GPIO_DIRECTION_IN:
138 return GPIO_LINE_DIRECTION_IN;
139 case VIRTIO_GPIO_DIRECTION_OUT:
140 return GPIO_LINE_DIRECTION_OUT;
141 default:
142 return -EINVAL;
143 }
144 }
145
virtio_gpio_direction_input(struct gpio_chip * gc,unsigned int gpio)146 static int virtio_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
147 {
148 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
149
150 return virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_DIRECTION, gpio,
151 VIRTIO_GPIO_DIRECTION_IN, NULL);
152 }
153
virtio_gpio_direction_output(struct gpio_chip * gc,unsigned int gpio,int value)154 static int virtio_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio,
155 int value)
156 {
157 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
158 int ret;
159
160 ret = virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_VALUE, gpio, value, NULL);
161 if (ret)
162 return ret;
163
164 return virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_DIRECTION, gpio,
165 VIRTIO_GPIO_DIRECTION_OUT, NULL);
166 }
167
virtio_gpio_get(struct gpio_chip * gc,unsigned int gpio)168 static int virtio_gpio_get(struct gpio_chip *gc, unsigned int gpio)
169 {
170 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
171 u8 value;
172 int ret;
173
174 ret = virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_GET_VALUE, gpio, 0, &value);
175 return ret ? ret : value;
176 }
177
virtio_gpio_set(struct gpio_chip * gc,unsigned int gpio,int value)178 static void virtio_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value)
179 {
180 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
181
182 virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_VALUE, gpio, value, NULL);
183 }
184
virtio_gpio_request_vq(struct virtqueue * vq)185 static void virtio_gpio_request_vq(struct virtqueue *vq)
186 {
187 struct virtio_gpio_line *line;
188 unsigned int len;
189
190 do {
191 line = virtqueue_get_buf(vq, &len);
192 if (!line)
193 return;
194
195 line->rxlen = len;
196 complete(&line->completion);
197 } while (1);
198 }
199
virtio_gpio_free_vqs(struct virtio_device * vdev)200 static void virtio_gpio_free_vqs(struct virtio_device *vdev)
201 {
202 vdev->config->reset(vdev);
203 vdev->config->del_vqs(vdev);
204 }
205
virtio_gpio_alloc_vqs(struct virtio_gpio * vgpio,struct virtio_device * vdev)206 static int virtio_gpio_alloc_vqs(struct virtio_gpio *vgpio,
207 struct virtio_device *vdev)
208 {
209 const char * const names[] = { "requestq" };
210 vq_callback_t *cbs[] = {
211 virtio_gpio_request_vq,
212 };
213 struct virtqueue *vqs[1] = { NULL };
214 int ret;
215
216 ret = virtio_find_vqs(vdev, 1, vqs, cbs, names, NULL);
217 if (ret) {
218 dev_err(&vdev->dev, "failed to find vqs: %d\n", ret);
219 return ret;
220 }
221
222 if (!vqs[0]) {
223 dev_err(&vdev->dev, "failed to find requestq vq\n");
224 return -ENODEV;
225 }
226 vgpio->request_vq = vqs[0];
227
228 return 0;
229 }
230
virtio_gpio_get_names(struct virtio_gpio * vgpio,u32 gpio_names_size,u16 ngpio)231 static const char **virtio_gpio_get_names(struct virtio_gpio *vgpio,
232 u32 gpio_names_size, u16 ngpio)
233 {
234 struct virtio_gpio_response_get_names *res;
235 struct device *dev = &vgpio->vdev->dev;
236 u8 *gpio_names, *str;
237 const char **names;
238 int i, ret, len;
239
240 if (!gpio_names_size)
241 return NULL;
242
243 len = sizeof(*res) + gpio_names_size;
244 res = devm_kzalloc(dev, len, GFP_KERNEL);
245 if (!res)
246 return NULL;
247 gpio_names = res->value;
248
249 ret = _virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_GET_NAMES, 0, 0, NULL,
250 res, len);
251 if (ret) {
252 dev_err(dev, "Failed to get GPIO names: %d\n", ret);
253 return NULL;
254 }
255
256 names = devm_kcalloc(dev, ngpio, sizeof(*names), GFP_KERNEL);
257 if (!names)
258 return NULL;
259
260 /* NULL terminate the string instead of checking it */
261 gpio_names[gpio_names_size - 1] = '\0';
262
263 for (i = 0, str = gpio_names; i < ngpio; i++) {
264 names[i] = str;
265 str += strlen(str) + 1; /* zero-length strings are allowed */
266
267 if (str > gpio_names + gpio_names_size) {
268 dev_err(dev, "gpio_names block is too short (%d)\n", i);
269 return NULL;
270 }
271 }
272
273 return names;
274 }
275
virtio_gpio_probe(struct virtio_device * vdev)276 static int virtio_gpio_probe(struct virtio_device *vdev)
277 {
278 struct virtio_gpio_config config;
279 struct device *dev = &vdev->dev;
280 struct virtio_gpio *vgpio;
281 u32 gpio_names_size;
282 u16 ngpio;
283 int ret, i;
284
285 vgpio = devm_kzalloc(dev, sizeof(*vgpio), GFP_KERNEL);
286 if (!vgpio)
287 return -ENOMEM;
288
289 /* Read configuration */
290 virtio_cread_bytes(vdev, 0, &config, sizeof(config));
291 gpio_names_size = le32_to_cpu(config.gpio_names_size);
292 ngpio = le16_to_cpu(config.ngpio);
293 if (!ngpio) {
294 dev_err(dev, "Number of GPIOs can't be zero\n");
295 return -EINVAL;
296 }
297
298 vgpio->lines = devm_kcalloc(dev, ngpio, sizeof(*vgpio->lines), GFP_KERNEL);
299 if (!vgpio->lines)
300 return -ENOMEM;
301
302 for (i = 0; i < ngpio; i++) {
303 mutex_init(&vgpio->lines[i].lock);
304 init_completion(&vgpio->lines[i].completion);
305 }
306
307 mutex_init(&vgpio->lock);
308 vdev->priv = vgpio;
309
310 vgpio->vdev = vdev;
311 vgpio->gc.free = virtio_gpio_free;
312 vgpio->gc.get_direction = virtio_gpio_get_direction;
313 vgpio->gc.direction_input = virtio_gpio_direction_input;
314 vgpio->gc.direction_output = virtio_gpio_direction_output;
315 vgpio->gc.get = virtio_gpio_get;
316 vgpio->gc.set = virtio_gpio_set;
317 vgpio->gc.ngpio = ngpio;
318 vgpio->gc.base = -1; /* Allocate base dynamically */
319 vgpio->gc.label = dev_name(dev);
320 vgpio->gc.parent = dev;
321 vgpio->gc.owner = THIS_MODULE;
322 vgpio->gc.can_sleep = true;
323
324 ret = virtio_gpio_alloc_vqs(vgpio, vdev);
325 if (ret)
326 return ret;
327
328 /* Mark the device ready to perform operations from within probe() */
329 virtio_device_ready(vdev);
330
331 vgpio->gc.names = virtio_gpio_get_names(vgpio, gpio_names_size, ngpio);
332
333 ret = gpiochip_add_data(&vgpio->gc, vgpio);
334 if (ret) {
335 virtio_gpio_free_vqs(vdev);
336 dev_err(dev, "Failed to add virtio-gpio controller\n");
337 }
338
339 return ret;
340 }
341
virtio_gpio_remove(struct virtio_device * vdev)342 static void virtio_gpio_remove(struct virtio_device *vdev)
343 {
344 struct virtio_gpio *vgpio = vdev->priv;
345
346 gpiochip_remove(&vgpio->gc);
347 virtio_gpio_free_vqs(vdev);
348 }
349
350 static const struct virtio_device_id id_table[] = {
351 { VIRTIO_ID_GPIO, VIRTIO_DEV_ANY_ID },
352 {},
353 };
354 MODULE_DEVICE_TABLE(virtio, id_table);
355
356 static struct virtio_driver virtio_gpio_driver = {
357 .id_table = id_table,
358 .probe = virtio_gpio_probe,
359 .remove = virtio_gpio_remove,
360 .driver = {
361 .name = KBUILD_MODNAME,
362 .owner = THIS_MODULE,
363 },
364 };
365 module_virtio_driver(virtio_gpio_driver);
366
367 MODULE_AUTHOR("Enrico Weigelt, metux IT consult <info@metux.net>");
368 MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
369 MODULE_DESCRIPTION("VirtIO GPIO driver");
370 MODULE_LICENSE("GPL");
371