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