xref: /openbmc/linux/drivers/gpio/gpio-virtio.c (revision 9b656879)
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/spinlock.h>
20 #include <linux/virtio_config.h>
21 #include <uapi/linux/virtio_gpio.h>
22 #include <uapi/linux/virtio_ids.h>
23 
24 struct virtio_gpio_line {
25 	struct mutex lock; /* Protects line operation */
26 	struct completion completion;
27 	struct virtio_gpio_request req ____cacheline_aligned;
28 	struct virtio_gpio_response res ____cacheline_aligned;
29 	unsigned int rxlen;
30 };
31 
32 struct vgpio_irq_line {
33 	u8 type;
34 	bool disabled;
35 	bool masked;
36 	bool queued;
37 	bool update_pending;
38 	bool queue_pending;
39 
40 	struct virtio_gpio_irq_request ireq ____cacheline_aligned;
41 	struct virtio_gpio_irq_response ires ____cacheline_aligned;
42 };
43 
44 struct virtio_gpio {
45 	struct virtio_device *vdev;
46 	struct mutex lock; /* Protects virtqueue operation */
47 	struct gpio_chip gc;
48 	struct virtio_gpio_line *lines;
49 	struct virtqueue *request_vq;
50 
51 	/* irq support */
52 	struct virtqueue *event_vq;
53 	struct mutex irq_lock; /* Protects irq operation */
54 	raw_spinlock_t eventq_lock; /* Protects queuing of the buffer */
55 	struct vgpio_irq_line *irq_lines;
56 };
57 
58 static int _virtio_gpio_req(struct virtio_gpio *vgpio, u16 type, u16 gpio,
59 			    u8 txvalue, u8 *rxvalue, void *response, u32 rxlen)
60 {
61 	struct virtio_gpio_line *line = &vgpio->lines[gpio];
62 	struct virtio_gpio_request *req = &line->req;
63 	struct virtio_gpio_response *res = response;
64 	struct scatterlist *sgs[2], req_sg, res_sg;
65 	struct device *dev = &vgpio->vdev->dev;
66 	int ret;
67 
68 	/*
69 	 * Prevent concurrent requests for the same line since we have
70 	 * pre-allocated request/response buffers for each GPIO line. Moreover
71 	 * Linux always accesses a GPIO line sequentially, so this locking shall
72 	 * always go through without any delays.
73 	 */
74 	mutex_lock(&line->lock);
75 
76 	req->type = cpu_to_le16(type);
77 	req->gpio = cpu_to_le16(gpio);
78 	req->value = cpu_to_le32(txvalue);
79 
80 	sg_init_one(&req_sg, req, sizeof(*req));
81 	sg_init_one(&res_sg, res, rxlen);
82 	sgs[0] = &req_sg;
83 	sgs[1] = &res_sg;
84 
85 	line->rxlen = 0;
86 	reinit_completion(&line->completion);
87 
88 	/*
89 	 * Virtqueue callers need to ensure they don't call its APIs with other
90 	 * virtqueue operations at the same time.
91 	 */
92 	mutex_lock(&vgpio->lock);
93 	ret = virtqueue_add_sgs(vgpio->request_vq, sgs, 1, 1, line, GFP_KERNEL);
94 	if (ret) {
95 		dev_err(dev, "failed to add request to vq\n");
96 		mutex_unlock(&vgpio->lock);
97 		goto out;
98 	}
99 
100 	virtqueue_kick(vgpio->request_vq);
101 	mutex_unlock(&vgpio->lock);
102 
103 	wait_for_completion(&line->completion);
104 
105 	if (unlikely(res->status != VIRTIO_GPIO_STATUS_OK)) {
106 		dev_err(dev, "GPIO request failed: %d\n", gpio);
107 		ret = -EINVAL;
108 		goto out;
109 	}
110 
111 	if (unlikely(line->rxlen != rxlen)) {
112 		dev_err(dev, "GPIO operation returned incorrect len (%u : %u)\n",
113 			rxlen, line->rxlen);
114 		ret = -EINVAL;
115 		goto out;
116 	}
117 
118 	if (rxvalue)
119 		*rxvalue = res->value;
120 
121 out:
122 	mutex_unlock(&line->lock);
123 	return ret;
124 }
125 
126 static int virtio_gpio_req(struct virtio_gpio *vgpio, u16 type, u16 gpio,
127 			   u8 txvalue, u8 *rxvalue)
128 {
129 	struct virtio_gpio_line *line = &vgpio->lines[gpio];
130 	struct virtio_gpio_response *res = &line->res;
131 
132 	return _virtio_gpio_req(vgpio, type, gpio, txvalue, rxvalue, res,
133 				sizeof(*res));
134 }
135 
136 static void virtio_gpio_free(struct gpio_chip *gc, unsigned int gpio)
137 {
138 	struct virtio_gpio *vgpio = gpiochip_get_data(gc);
139 
140 	virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_DIRECTION, gpio,
141 			VIRTIO_GPIO_DIRECTION_NONE, NULL);
142 }
143 
144 static int virtio_gpio_get_direction(struct gpio_chip *gc, unsigned int gpio)
145 {
146 	struct virtio_gpio *vgpio = gpiochip_get_data(gc);
147 	u8 direction;
148 	int ret;
149 
150 	ret = virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_GET_DIRECTION, gpio, 0,
151 			      &direction);
152 	if (ret)
153 		return ret;
154 
155 	switch (direction) {
156 	case VIRTIO_GPIO_DIRECTION_IN:
157 		return GPIO_LINE_DIRECTION_IN;
158 	case VIRTIO_GPIO_DIRECTION_OUT:
159 		return GPIO_LINE_DIRECTION_OUT;
160 	default:
161 		return -EINVAL;
162 	}
163 }
164 
165 static int virtio_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
166 {
167 	struct virtio_gpio *vgpio = gpiochip_get_data(gc);
168 
169 	return virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_DIRECTION, gpio,
170 			       VIRTIO_GPIO_DIRECTION_IN, NULL);
171 }
172 
173 static int virtio_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio,
174 					int value)
175 {
176 	struct virtio_gpio *vgpio = gpiochip_get_data(gc);
177 	int ret;
178 
179 	ret = virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_VALUE, gpio, value, NULL);
180 	if (ret)
181 		return ret;
182 
183 	return virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_DIRECTION, gpio,
184 			       VIRTIO_GPIO_DIRECTION_OUT, NULL);
185 }
186 
187 static int virtio_gpio_get(struct gpio_chip *gc, unsigned int gpio)
188 {
189 	struct virtio_gpio *vgpio = gpiochip_get_data(gc);
190 	u8 value;
191 	int ret;
192 
193 	ret = virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_GET_VALUE, gpio, 0, &value);
194 	return ret ? ret : value;
195 }
196 
197 static void virtio_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value)
198 {
199 	struct virtio_gpio *vgpio = gpiochip_get_data(gc);
200 
201 	virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_VALUE, gpio, value, NULL);
202 }
203 
204 /* Interrupt handling */
205 static void virtio_gpio_irq_prepare(struct virtio_gpio *vgpio, u16 gpio)
206 {
207 	struct vgpio_irq_line *irq_line = &vgpio->irq_lines[gpio];
208 	struct virtio_gpio_irq_request *ireq = &irq_line->ireq;
209 	struct virtio_gpio_irq_response *ires = &irq_line->ires;
210 	struct scatterlist *sgs[2], req_sg, res_sg;
211 	int ret;
212 
213 	if (WARN_ON(irq_line->queued || irq_line->masked || irq_line->disabled))
214 		return;
215 
216 	ireq->gpio = cpu_to_le16(gpio);
217 	sg_init_one(&req_sg, ireq, sizeof(*ireq));
218 	sg_init_one(&res_sg, ires, sizeof(*ires));
219 	sgs[0] = &req_sg;
220 	sgs[1] = &res_sg;
221 
222 	ret = virtqueue_add_sgs(vgpio->event_vq, sgs, 1, 1, irq_line, GFP_ATOMIC);
223 	if (ret) {
224 		dev_err(&vgpio->vdev->dev, "failed to add request to eventq\n");
225 		return;
226 	}
227 
228 	irq_line->queued = true;
229 	virtqueue_kick(vgpio->event_vq);
230 }
231 
232 static void virtio_gpio_irq_enable(struct irq_data *d)
233 {
234 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
235 	struct virtio_gpio *vgpio = gpiochip_get_data(gc);
236 	struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
237 
238 	raw_spin_lock(&vgpio->eventq_lock);
239 	irq_line->disabled = false;
240 	irq_line->masked = false;
241 	irq_line->queue_pending = true;
242 	raw_spin_unlock(&vgpio->eventq_lock);
243 
244 	irq_line->update_pending = true;
245 }
246 
247 static void virtio_gpio_irq_disable(struct irq_data *d)
248 {
249 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
250 	struct virtio_gpio *vgpio = gpiochip_get_data(gc);
251 	struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
252 
253 	raw_spin_lock(&vgpio->eventq_lock);
254 	irq_line->disabled = true;
255 	irq_line->masked = true;
256 	irq_line->queue_pending = false;
257 	raw_spin_unlock(&vgpio->eventq_lock);
258 
259 	irq_line->update_pending = true;
260 }
261 
262 static void virtio_gpio_irq_mask(struct irq_data *d)
263 {
264 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
265 	struct virtio_gpio *vgpio = gpiochip_get_data(gc);
266 	struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
267 
268 	raw_spin_lock(&vgpio->eventq_lock);
269 	irq_line->masked = true;
270 	raw_spin_unlock(&vgpio->eventq_lock);
271 }
272 
273 static void virtio_gpio_irq_unmask(struct irq_data *d)
274 {
275 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
276 	struct virtio_gpio *vgpio = gpiochip_get_data(gc);
277 	struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
278 
279 	raw_spin_lock(&vgpio->eventq_lock);
280 	irq_line->masked = false;
281 
282 	/* Queue the buffer unconditionally on unmask */
283 	virtio_gpio_irq_prepare(vgpio, d->hwirq);
284 	raw_spin_unlock(&vgpio->eventq_lock);
285 }
286 
287 static int virtio_gpio_irq_set_type(struct irq_data *d, unsigned int type)
288 {
289 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
290 	struct virtio_gpio *vgpio = gpiochip_get_data(gc);
291 	struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
292 
293 	switch (type) {
294 	case IRQ_TYPE_EDGE_RISING:
295 		type = VIRTIO_GPIO_IRQ_TYPE_EDGE_RISING;
296 		break;
297 	case IRQ_TYPE_EDGE_FALLING:
298 		type = VIRTIO_GPIO_IRQ_TYPE_EDGE_FALLING;
299 		break;
300 	case IRQ_TYPE_EDGE_BOTH:
301 		type = VIRTIO_GPIO_IRQ_TYPE_EDGE_BOTH;
302 		break;
303 	case IRQ_TYPE_LEVEL_LOW:
304 		type = VIRTIO_GPIO_IRQ_TYPE_LEVEL_LOW;
305 		break;
306 	case IRQ_TYPE_LEVEL_HIGH:
307 		type = VIRTIO_GPIO_IRQ_TYPE_LEVEL_HIGH;
308 		break;
309 	default:
310 		dev_err(&vgpio->vdev->dev, "unsupported irq type: %u\n", type);
311 		return -EINVAL;
312 	}
313 
314 	irq_line->type = type;
315 	irq_line->update_pending = true;
316 
317 	return 0;
318 }
319 
320 static void virtio_gpio_irq_bus_lock(struct irq_data *d)
321 {
322 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
323 	struct virtio_gpio *vgpio = gpiochip_get_data(gc);
324 
325 	mutex_lock(&vgpio->irq_lock);
326 }
327 
328 static void virtio_gpio_irq_bus_sync_unlock(struct irq_data *d)
329 {
330 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
331 	struct virtio_gpio *vgpio = gpiochip_get_data(gc);
332 	struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
333 	u8 type = irq_line->disabled ? VIRTIO_GPIO_IRQ_TYPE_NONE : irq_line->type;
334 	unsigned long flags;
335 
336 	if (irq_line->update_pending) {
337 		irq_line->update_pending = false;
338 		virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_IRQ_TYPE, d->hwirq, type,
339 				NULL);
340 
341 		/* Queue the buffer only after interrupt is enabled */
342 		raw_spin_lock_irqsave(&vgpio->eventq_lock, flags);
343 		if (irq_line->queue_pending) {
344 			irq_line->queue_pending = false;
345 			virtio_gpio_irq_prepare(vgpio, d->hwirq);
346 		}
347 		raw_spin_unlock_irqrestore(&vgpio->eventq_lock, flags);
348 	}
349 
350 	mutex_unlock(&vgpio->irq_lock);
351 }
352 
353 static struct irq_chip vgpio_irq_chip = {
354 	.name			= "virtio-gpio",
355 	.irq_enable		= virtio_gpio_irq_enable,
356 	.irq_disable		= virtio_gpio_irq_disable,
357 	.irq_mask		= virtio_gpio_irq_mask,
358 	.irq_unmask		= virtio_gpio_irq_unmask,
359 	.irq_set_type		= virtio_gpio_irq_set_type,
360 
361 	/* These are required to implement irqchip for slow busses */
362 	.irq_bus_lock		= virtio_gpio_irq_bus_lock,
363 	.irq_bus_sync_unlock	= virtio_gpio_irq_bus_sync_unlock,
364 };
365 
366 static bool ignore_irq(struct virtio_gpio *vgpio, int gpio,
367 		       struct vgpio_irq_line *irq_line)
368 {
369 	bool ignore = false;
370 
371 	raw_spin_lock(&vgpio->eventq_lock);
372 	irq_line->queued = false;
373 
374 	/* Interrupt is disabled currently */
375 	if (irq_line->masked || irq_line->disabled) {
376 		ignore = true;
377 		goto unlock;
378 	}
379 
380 	/*
381 	 * Buffer is returned as the interrupt was disabled earlier, but is
382 	 * enabled again now. Requeue the buffers.
383 	 */
384 	if (irq_line->ires.status == VIRTIO_GPIO_IRQ_STATUS_INVALID) {
385 		virtio_gpio_irq_prepare(vgpio, gpio);
386 		ignore = true;
387 		goto unlock;
388 	}
389 
390 	if (WARN_ON(irq_line->ires.status != VIRTIO_GPIO_IRQ_STATUS_VALID))
391 		ignore = true;
392 
393 unlock:
394 	raw_spin_unlock(&vgpio->eventq_lock);
395 
396 	return ignore;
397 }
398 
399 static void virtio_gpio_event_vq(struct virtqueue *vq)
400 {
401 	struct virtio_gpio *vgpio = vq->vdev->priv;
402 	struct device *dev = &vgpio->vdev->dev;
403 	struct vgpio_irq_line *irq_line;
404 	int gpio, ret;
405 	unsigned int len;
406 
407 	while (true) {
408 		irq_line = virtqueue_get_buf(vgpio->event_vq, &len);
409 		if (!irq_line)
410 			break;
411 
412 		if (len != sizeof(irq_line->ires)) {
413 			dev_err(dev, "irq with incorrect length (%u : %u)\n",
414 				len, (unsigned int)sizeof(irq_line->ires));
415 			continue;
416 		}
417 
418 		/*
419 		 * Find GPIO line number from the offset of irq_line within the
420 		 * irq_lines block. We can also get GPIO number from
421 		 * irq-request, but better not to rely on a buffer returned by
422 		 * remote.
423 		 */
424 		gpio = irq_line - vgpio->irq_lines;
425 		WARN_ON(gpio >= vgpio->gc.ngpio);
426 
427 		if (unlikely(ignore_irq(vgpio, gpio, irq_line)))
428 			continue;
429 
430 		ret = generic_handle_domain_irq(vgpio->gc.irq.domain, gpio);
431 		if (ret)
432 			dev_err(dev, "failed to handle interrupt: %d\n", ret);
433 	}
434 }
435 
436 static void virtio_gpio_request_vq(struct virtqueue *vq)
437 {
438 	struct virtio_gpio_line *line;
439 	unsigned int len;
440 
441 	do {
442 		line = virtqueue_get_buf(vq, &len);
443 		if (!line)
444 			return;
445 
446 		line->rxlen = len;
447 		complete(&line->completion);
448 	} while (1);
449 }
450 
451 static void virtio_gpio_free_vqs(struct virtio_device *vdev)
452 {
453 	virtio_reset_device(vdev);
454 	vdev->config->del_vqs(vdev);
455 }
456 
457 static int virtio_gpio_alloc_vqs(struct virtio_gpio *vgpio,
458 				 struct virtio_device *vdev)
459 {
460 	const char * const names[] = { "requestq", "eventq" };
461 	vq_callback_t *cbs[] = {
462 		virtio_gpio_request_vq,
463 		virtio_gpio_event_vq,
464 	};
465 	struct virtqueue *vqs[2] = { NULL, NULL };
466 	int ret;
467 
468 	ret = virtio_find_vqs(vdev, vgpio->irq_lines ? 2 : 1, vqs, cbs, names, NULL);
469 	if (ret) {
470 		dev_err(&vdev->dev, "failed to find vqs: %d\n", ret);
471 		return ret;
472 	}
473 
474 	if (!vqs[0]) {
475 		dev_err(&vdev->dev, "failed to find requestq vq\n");
476 		goto out;
477 	}
478 	vgpio->request_vq = vqs[0];
479 
480 	if (vgpio->irq_lines && !vqs[1]) {
481 		dev_err(&vdev->dev, "failed to find eventq vq\n");
482 		goto out;
483 	}
484 	vgpio->event_vq = vqs[1];
485 
486 	return 0;
487 
488 out:
489 	if (vqs[0] || vqs[1])
490 		virtio_gpio_free_vqs(vdev);
491 
492 	return -ENODEV;
493 }
494 
495 static const char **virtio_gpio_get_names(struct virtio_gpio *vgpio,
496 					  u32 gpio_names_size, u16 ngpio)
497 {
498 	struct virtio_gpio_response_get_names *res;
499 	struct device *dev = &vgpio->vdev->dev;
500 	u8 *gpio_names, *str;
501 	const char **names;
502 	int i, ret, len;
503 
504 	if (!gpio_names_size)
505 		return NULL;
506 
507 	len = sizeof(*res) + gpio_names_size;
508 	res = devm_kzalloc(dev, len, GFP_KERNEL);
509 	if (!res)
510 		return NULL;
511 	gpio_names = res->value;
512 
513 	ret = _virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_GET_NAMES, 0, 0, NULL,
514 			       res, len);
515 	if (ret) {
516 		dev_err(dev, "Failed to get GPIO names: %d\n", ret);
517 		return NULL;
518 	}
519 
520 	names = devm_kcalloc(dev, ngpio, sizeof(*names), GFP_KERNEL);
521 	if (!names)
522 		return NULL;
523 
524 	/* NULL terminate the string instead of checking it */
525 	gpio_names[gpio_names_size - 1] = '\0';
526 
527 	for (i = 0, str = gpio_names; i < ngpio; i++) {
528 		names[i] = str;
529 		str += strlen(str) + 1; /* zero-length strings are allowed */
530 
531 		if (str > gpio_names + gpio_names_size) {
532 			dev_err(dev, "gpio_names block is too short (%d)\n", i);
533 			return NULL;
534 		}
535 	}
536 
537 	return names;
538 }
539 
540 static int virtio_gpio_probe(struct virtio_device *vdev)
541 {
542 	struct virtio_gpio_config config;
543 	struct device *dev = &vdev->dev;
544 	struct virtio_gpio *vgpio;
545 	u32 gpio_names_size;
546 	u16 ngpio;
547 	int ret, i;
548 
549 	vgpio = devm_kzalloc(dev, sizeof(*vgpio), GFP_KERNEL);
550 	if (!vgpio)
551 		return -ENOMEM;
552 
553 	/* Read configuration */
554 	virtio_cread_bytes(vdev, 0, &config, sizeof(config));
555 	gpio_names_size = le32_to_cpu(config.gpio_names_size);
556 	ngpio = le16_to_cpu(config.ngpio);
557 	if (!ngpio) {
558 		dev_err(dev, "Number of GPIOs can't be zero\n");
559 		return -EINVAL;
560 	}
561 
562 	vgpio->lines = devm_kcalloc(dev, ngpio, sizeof(*vgpio->lines), GFP_KERNEL);
563 	if (!vgpio->lines)
564 		return -ENOMEM;
565 
566 	for (i = 0; i < ngpio; i++) {
567 		mutex_init(&vgpio->lines[i].lock);
568 		init_completion(&vgpio->lines[i].completion);
569 	}
570 
571 	mutex_init(&vgpio->lock);
572 	vdev->priv = vgpio;
573 
574 	vgpio->vdev			= vdev;
575 	vgpio->gc.free			= virtio_gpio_free;
576 	vgpio->gc.get_direction		= virtio_gpio_get_direction;
577 	vgpio->gc.direction_input	= virtio_gpio_direction_input;
578 	vgpio->gc.direction_output	= virtio_gpio_direction_output;
579 	vgpio->gc.get			= virtio_gpio_get;
580 	vgpio->gc.set			= virtio_gpio_set;
581 	vgpio->gc.ngpio			= ngpio;
582 	vgpio->gc.base			= -1; /* Allocate base dynamically */
583 	vgpio->gc.label			= dev_name(dev);
584 	vgpio->gc.parent		= dev;
585 	vgpio->gc.owner			= THIS_MODULE;
586 	vgpio->gc.can_sleep		= true;
587 
588 	/* Interrupt support */
589 	if (virtio_has_feature(vdev, VIRTIO_GPIO_F_IRQ)) {
590 		vgpio->irq_lines = devm_kcalloc(dev, ngpio, sizeof(*vgpio->irq_lines), GFP_KERNEL);
591 		if (!vgpio->irq_lines)
592 			return -ENOMEM;
593 
594 		/* The event comes from the outside so no parent handler */
595 		vgpio->gc.irq.parent_handler	= NULL;
596 		vgpio->gc.irq.num_parents	= 0;
597 		vgpio->gc.irq.parents		= NULL;
598 		vgpio->gc.irq.default_type	= IRQ_TYPE_NONE;
599 		vgpio->gc.irq.handler		= handle_level_irq;
600 		vgpio->gc.irq.chip		= &vgpio_irq_chip;
601 
602 		for (i = 0; i < ngpio; i++) {
603 			vgpio->irq_lines[i].type = VIRTIO_GPIO_IRQ_TYPE_NONE;
604 			vgpio->irq_lines[i].disabled = true;
605 			vgpio->irq_lines[i].masked = true;
606 		}
607 
608 		mutex_init(&vgpio->irq_lock);
609 		raw_spin_lock_init(&vgpio->eventq_lock);
610 	}
611 
612 	ret = virtio_gpio_alloc_vqs(vgpio, vdev);
613 	if (ret)
614 		return ret;
615 
616 	/* Mark the device ready to perform operations from within probe() */
617 	virtio_device_ready(vdev);
618 
619 	vgpio->gc.names = virtio_gpio_get_names(vgpio, gpio_names_size, ngpio);
620 
621 	ret = gpiochip_add_data(&vgpio->gc, vgpio);
622 	if (ret) {
623 		virtio_gpio_free_vqs(vdev);
624 		dev_err(dev, "Failed to add virtio-gpio controller\n");
625 	}
626 
627 	return ret;
628 }
629 
630 static void virtio_gpio_remove(struct virtio_device *vdev)
631 {
632 	struct virtio_gpio *vgpio = vdev->priv;
633 
634 	gpiochip_remove(&vgpio->gc);
635 	virtio_gpio_free_vqs(vdev);
636 }
637 
638 static const struct virtio_device_id id_table[] = {
639 	{ VIRTIO_ID_GPIO, VIRTIO_DEV_ANY_ID },
640 	{},
641 };
642 MODULE_DEVICE_TABLE(virtio, id_table);
643 
644 static const unsigned int features[] = {
645 	VIRTIO_GPIO_F_IRQ,
646 };
647 
648 static struct virtio_driver virtio_gpio_driver = {
649 	.feature_table		= features,
650 	.feature_table_size	= ARRAY_SIZE(features),
651 	.id_table		= id_table,
652 	.probe			= virtio_gpio_probe,
653 	.remove			= virtio_gpio_remove,
654 	.driver			= {
655 		.name		= KBUILD_MODNAME,
656 		.owner		= THIS_MODULE,
657 	},
658 };
659 module_virtio_driver(virtio_gpio_driver);
660 
661 MODULE_AUTHOR("Enrico Weigelt, metux IT consult <info@metux.net>");
662 MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
663 MODULE_DESCRIPTION("VirtIO GPIO driver");
664 MODULE_LICENSE("GPL");
665