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