]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/greybus/gpio.c
greybus: gpio: handle api change in generic_handle_irq_desc()
[karo-tx-linux.git] / drivers / staging / greybus / gpio.c
1 /*
2  * GPIO Greybus driver.
3  *
4  * Copyright 2014 Google Inc.
5  * Copyright 2014 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/gpio.h>
14 #include <linux/irq.h>
15 #include <linux/irqdomain.h>
16 #include <linux/mutex.h>
17
18 #include "greybus.h"
19
20 struct gb_gpio_line {
21         /* The following has to be an array of line_max entries */
22         /* --> make them just a flags field */
23         u8                      active:    1,
24                                 direction: 1,   /* 0 = output, 1 = input */
25                                 value:     1;   /* 0 = low, 1 = high */
26         u16                     debounce_usec;
27
28         u8                      irq_type;
29         bool                    irq_type_pending;
30         bool                    masked;
31         bool                    masked_pending;
32 };
33
34 struct gb_gpio_controller {
35         struct gb_connection    *connection;
36         u8                      line_max;       /* max line number */
37         struct gb_gpio_line     *lines;
38
39         struct gpio_chip        chip;
40         struct irq_chip         irqc;
41         struct irq_chip         *irqchip;
42         struct irq_domain       *irqdomain;
43         unsigned int            irq_base;
44         irq_flow_handler_t      irq_handler;
45         unsigned int            irq_default_type;
46         struct mutex            irq_lock;
47 };
48 #define gpio_chip_to_gb_gpio_controller(chip) \
49         container_of(chip, struct gb_gpio_controller, chip)
50 #define irq_data_to_gpio_chip(d) (d->domain->host_data)
51
52 static int gb_gpio_line_count_operation(struct gb_gpio_controller *ggc)
53 {
54         struct gb_gpio_line_count_response response;
55         int ret;
56
57         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_LINE_COUNT,
58                                 NULL, 0, &response, sizeof(response));
59         if (!ret)
60                 ggc->line_max = response.count;
61         return ret;
62 }
63
64 static int gb_gpio_activate_operation(struct gb_gpio_controller *ggc, u8 which)
65 {
66         struct gb_gpio_activate_request request;
67         int ret;
68
69         request.which = which;
70         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_ACTIVATE,
71                                  &request, sizeof(request), NULL, 0);
72         if (!ret)
73                 ggc->lines[which].active = true;
74         return ret;
75 }
76
77 static void gb_gpio_deactivate_operation(struct gb_gpio_controller *ggc,
78                                         u8 which)
79 {
80         struct gb_gpio_deactivate_request request;
81         int ret;
82
83         request.which = which;
84         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DEACTIVATE,
85                                  &request, sizeof(request), NULL, 0);
86         if (ret) {
87                 dev_err(ggc->chip.dev, "failed to deactivate gpio %u\n",
88                         which);
89                 return;
90         }
91
92         ggc->lines[which].active = false;
93 }
94
95 static int gb_gpio_get_direction_operation(struct gb_gpio_controller *ggc,
96                                         u8 which)
97 {
98         struct gb_gpio_get_direction_request request;
99         struct gb_gpio_get_direction_response response;
100         int ret;
101         u8 direction;
102
103         request.which = which;
104         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_DIRECTION,
105                                 &request, sizeof(request),
106                                 &response, sizeof(response));
107         if (ret)
108                 return ret;
109
110         direction = response.direction;
111         if (direction && direction != 1) {
112                 dev_warn(ggc->chip.dev,
113                          "gpio %u direction was %u (should be 0 or 1)\n",
114                          which, direction);
115         }
116         ggc->lines[which].direction = direction ? 1 : 0;
117         return 0;
118 }
119
120 static int gb_gpio_direction_in_operation(struct gb_gpio_controller *ggc,
121                                         u8 which)
122 {
123         struct gb_gpio_direction_in_request request;
124         int ret;
125
126         request.which = which;
127         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_IN,
128                                 &request, sizeof(request), NULL, 0);
129         if (!ret)
130                 ggc->lines[which].direction = 1;
131         return ret;
132 }
133
134 static int gb_gpio_direction_out_operation(struct gb_gpio_controller *ggc,
135                                         u8 which, bool value_high)
136 {
137         struct gb_gpio_direction_out_request request;
138         int ret;
139
140         request.which = which;
141         request.value = value_high ? 1 : 0;
142         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_OUT,
143                                 &request, sizeof(request), NULL, 0);
144         if (!ret)
145                 ggc->lines[which].direction = 0;
146         return ret;
147 }
148
149 static int gb_gpio_get_value_operation(struct gb_gpio_controller *ggc,
150                                         u8 which)
151 {
152         struct gb_gpio_get_value_request request;
153         struct gb_gpio_get_value_response response;
154         int ret;
155         u8 value;
156
157         request.which = which;
158         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_VALUE,
159                                 &request, sizeof(request),
160                                 &response, sizeof(response));
161         if (ret) {
162                 dev_err(ggc->chip.dev, "failed to get value of gpio %u\n",
163                         which);
164                 return ret;
165         }
166
167         value = response.value;
168         if (value && value != 1) {
169                 dev_warn(ggc->chip.dev,
170                          "gpio %u value was %u (should be 0 or 1)\n",
171                          which, value);
172         }
173         ggc->lines[which].value = value ? 1 : 0;
174         return 0;
175 }
176
177 static void gb_gpio_set_value_operation(struct gb_gpio_controller *ggc,
178                                         u8 which, bool value_high)
179 {
180         struct gb_gpio_set_value_request request;
181         int ret;
182
183         if (ggc->lines[which].direction == 1) {
184                 dev_warn(ggc->chip.dev,
185                          "refusing to set value of input gpio %u\n", which);
186                 return;
187         }
188
189         request.which = which;
190         request.value = value_high ? 1 : 0;
191         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_VALUE,
192                                 &request, sizeof(request), NULL, 0);
193         if (ret) {
194                 dev_err(ggc->chip.dev, "failed to set value of gpio %u\n",
195                         which);
196                 return;
197         }
198
199         ggc->lines[which].value = request.value;
200 }
201
202 static int gb_gpio_set_debounce_operation(struct gb_gpio_controller *ggc,
203                                         u8 which, u16 debounce_usec)
204 {
205         struct gb_gpio_set_debounce_request request;
206         int ret;
207
208         request.which = which;
209         request.usec = cpu_to_le16(debounce_usec);
210         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_DEBOUNCE,
211                                 &request, sizeof(request), NULL, 0);
212         if (!ret)
213                 ggc->lines[which].debounce_usec = debounce_usec;
214         return ret;
215 }
216
217 static void _gb_gpio_irq_mask(struct gb_gpio_controller *ggc, u8 hwirq)
218 {
219         struct gb_gpio_irq_mask_request request;
220         int ret;
221
222         request.which = hwirq;
223         ret = gb_operation_sync(ggc->connection,
224                                 GB_GPIO_TYPE_IRQ_MASK,
225                                 &request, sizeof(request), NULL, 0);
226         if (ret)
227                 dev_err(ggc->chip.dev, "failed to mask irq: %d\n", ret);
228 }
229
230 static void _gb_gpio_irq_unmask(struct gb_gpio_controller *ggc, u8 hwirq)
231 {
232         struct gb_gpio_irq_unmask_request request;
233         int ret;
234
235         request.which = hwirq;
236         ret = gb_operation_sync(ggc->connection,
237                                 GB_GPIO_TYPE_IRQ_UNMASK,
238                                 &request, sizeof(request), NULL, 0);
239         if (ret)
240                 dev_err(ggc->chip.dev, "failed to unmask irq: %d\n", ret);
241 }
242
243 static void _gb_gpio_irq_set_type(struct gb_gpio_controller *ggc,
244                                         u8 hwirq, u8 type)
245 {
246         struct gb_gpio_irq_type_request request;
247         int ret;
248
249         request.which = hwirq;
250         request.type = type;
251
252         ret = gb_operation_sync(ggc->connection,
253                                 GB_GPIO_TYPE_IRQ_TYPE,
254                                 &request, sizeof(request), NULL, 0);
255         if (ret)
256                 dev_err(ggc->chip.dev, "failed to set irq type: %d\n", ret);
257 }
258
259 static void gb_gpio_irq_mask(struct irq_data *d)
260 {
261         struct gpio_chip *chip = irq_data_to_gpio_chip(d);
262         struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
263         struct gb_gpio_line *line = &ggc->lines[d->hwirq];
264
265         line->masked = true;
266         line->masked_pending = true;
267 }
268
269 static void gb_gpio_irq_unmask(struct irq_data *d)
270 {
271         struct gpio_chip *chip = irq_data_to_gpio_chip(d);
272         struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
273         struct gb_gpio_line *line = &ggc->lines[d->hwirq];
274
275         line->masked = false;
276         line->masked_pending = true;
277 }
278
279 static int gb_gpio_irq_set_type(struct irq_data *d, unsigned int type)
280 {
281         struct gpio_chip *chip = irq_data_to_gpio_chip(d);
282         struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
283         struct gb_gpio_line *line = &ggc->lines[d->hwirq];
284         u8 irq_type;
285
286         switch (type) {
287         case IRQ_TYPE_NONE:
288                 irq_type = GB_GPIO_IRQ_TYPE_NONE;
289                 break;
290         case IRQ_TYPE_EDGE_RISING:
291                 irq_type = GB_GPIO_IRQ_TYPE_EDGE_RISING;
292                 break;
293         case IRQ_TYPE_EDGE_FALLING:
294                 irq_type = GB_GPIO_IRQ_TYPE_EDGE_FALLING;
295                 break;
296         case IRQ_TYPE_EDGE_BOTH:
297                 irq_type = GB_GPIO_IRQ_TYPE_EDGE_BOTH;
298                 break;
299         case IRQ_TYPE_LEVEL_LOW:
300                 irq_type = GB_GPIO_IRQ_TYPE_LEVEL_LOW;
301                 break;
302         case IRQ_TYPE_LEVEL_HIGH:
303                 irq_type = GB_GPIO_IRQ_TYPE_LEVEL_HIGH;
304                 break;
305         default:
306                 dev_err(chip->dev, "unsupported irq type: %u\n", type);
307                 return -EINVAL;
308         }
309
310         line->irq_type = irq_type;
311         line->irq_type_pending = true;
312
313         return 0;
314 }
315
316 static void gb_gpio_irq_bus_lock(struct irq_data *d)
317 {
318         struct gpio_chip *chip = irq_data_to_gpio_chip(d);
319         struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
320
321         mutex_lock(&ggc->irq_lock);
322 }
323
324 static void gb_gpio_irq_bus_sync_unlock(struct irq_data *d)
325 {
326         struct gpio_chip *chip = irq_data_to_gpio_chip(d);
327         struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
328         struct gb_gpio_line *line = &ggc->lines[d->hwirq];
329
330         if (line->irq_type_pending) {
331                 _gb_gpio_irq_set_type(ggc, d->hwirq, line->irq_type);
332                 line->irq_type_pending = false;
333         }
334
335         if (line->masked_pending) {
336                 if (line->masked)
337                         _gb_gpio_irq_mask(ggc, d->hwirq);
338                 else
339                         _gb_gpio_irq_unmask(ggc, d->hwirq);
340                 line->masked_pending = false;
341         }
342
343         mutex_unlock(&ggc->irq_lock);
344 }
345
346 static int gb_gpio_request_recv(u8 type, struct gb_operation *op)
347 {
348         struct gb_connection *connection = op->connection;
349         struct gb_gpio_controller *ggc = connection->private;
350         struct gb_message *request;
351         struct gb_gpio_irq_event_request *event;
352         int irq;
353         struct irq_desc *desc;
354
355         if (type != GB_GPIO_TYPE_IRQ_EVENT) {
356                 dev_err(&connection->dev,
357                         "unsupported unsolicited request: %u\n", type);
358                 return -EINVAL;
359         }
360
361         request = op->request;
362
363         if (request->payload_size < sizeof(*event)) {
364                 dev_err(ggc->chip.dev, "short event received (%zu < %zu)\n",
365                         request->payload_size, sizeof(*event));
366                 return -EINVAL;
367         }
368
369         event = request->payload;
370         if (event->which > ggc->line_max) {
371                 dev_err(ggc->chip.dev, "invalid hw irq: %d\n", event->which);
372                 return -EINVAL;
373         }
374
375         irq = irq_find_mapping(ggc->irqdomain, event->which);
376         if (!irq) {
377                 dev_err(ggc->chip.dev, "failed to find IRQ\n");
378                 return -EINVAL;
379         }
380         desc = irq_to_desc(irq);
381         if (!desc) {
382                 dev_err(ggc->chip.dev, "failed to look up irq\n");
383                 return -EINVAL;
384         }
385
386         local_irq_disable();
387 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 2, 0)
388         generic_handle_irq_desc(irq, desc);
389 #else
390         generic_handle_irq_desc(desc);
391 #endif
392         local_irq_enable();
393
394         return 0;
395 }
396
397 static int gb_gpio_request(struct gpio_chip *chip, unsigned offset)
398 {
399         struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
400
401         return gb_gpio_activate_operation(ggc, (u8)offset);
402 }
403
404 static void gb_gpio_free(struct gpio_chip *chip, unsigned offset)
405 {
406         struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
407
408         gb_gpio_deactivate_operation(ggc, (u8)offset);
409 }
410
411 static int gb_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
412 {
413         struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
414         u8 which;
415         int ret;
416
417         which = (u8)offset;
418         ret = gb_gpio_get_direction_operation(ggc, which);
419         if (ret)
420                 return ret;
421
422         return ggc->lines[which].direction ? 1 : 0;
423 }
424
425 static int gb_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
426 {
427         struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
428
429         return gb_gpio_direction_in_operation(ggc, (u8)offset);
430 }
431
432 static int gb_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
433                                         int value)
434 {
435         struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
436
437         return gb_gpio_direction_out_operation(ggc, (u8)offset, !!value);
438 }
439
440 static int gb_gpio_get(struct gpio_chip *chip, unsigned offset)
441 {
442         struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
443         u8 which;
444         int ret;
445
446         which = (u8)offset;
447         ret = gb_gpio_get_value_operation(ggc, which);
448         if (ret)
449                 return ret;
450
451         return ggc->lines[which].value;
452 }
453
454 static void gb_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
455 {
456         struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
457
458         gb_gpio_set_value_operation(ggc, (u8)offset, !!value);
459 }
460
461 static int gb_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
462                                         unsigned debounce)
463 {
464         struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
465         u16 usec;
466
467         if (debounce > U16_MAX)
468                 return -EINVAL;
469         usec = (u16)debounce;
470
471         return gb_gpio_set_debounce_operation(ggc, (u8)offset, usec);
472 }
473
474 static int gb_gpio_controller_setup(struct gb_gpio_controller *ggc)
475 {
476         int ret;
477
478         /* Now find out how many lines there are */
479         ret = gb_gpio_line_count_operation(ggc);
480         if (ret)
481                 return ret;
482
483         ggc->lines = kcalloc(ggc->line_max + 1, sizeof(*ggc->lines),
484                              GFP_KERNEL);
485         if (!ggc->lines)
486                 return -ENOMEM;
487
488         return ret;
489 }
490
491 /**
492  * gb_gpio_irq_map() - maps an IRQ into a GB gpio irqchip
493  * @d: the irqdomain used by this irqchip
494  * @irq: the global irq number used by this GB gpio irqchip irq
495  * @hwirq: the local IRQ/GPIO line offset on this GB gpio
496  *
497  * This function will set up the mapping for a certain IRQ line on a
498  * GB gpio by assigning the GB gpio as chip data, and using the irqchip
499  * stored inside the GB gpio.
500  */
501 static int gb_gpio_irq_map(struct irq_domain *domain, unsigned int irq,
502                            irq_hw_number_t hwirq)
503 {
504         struct gpio_chip *chip = domain->host_data;
505         struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
506
507         irq_set_chip_data(irq, ggc);
508         irq_set_chip_and_handler(irq, ggc->irqchip, ggc->irq_handler);
509 #ifdef CONFIG_ARM
510         set_irq_flags(irq, IRQF_VALID);
511 #else
512         irq_set_noprobe(irq);
513 #endif
514         /*
515          * No set-up of the hardware will happen if IRQ_TYPE_NONE
516          * is passed as default type.
517          */
518         if (ggc->irq_default_type != IRQ_TYPE_NONE)
519                 irq_set_irq_type(irq, ggc->irq_default_type);
520
521         return 0;
522 }
523
524 static void gb_gpio_irq_unmap(struct irq_domain *d, unsigned int irq)
525 {
526 #ifdef CONFIG_ARM
527         set_irq_flags(irq, 0);
528 #endif
529         irq_set_chip_and_handler(irq, NULL, NULL);
530         irq_set_chip_data(irq, NULL);
531 }
532
533 static const struct irq_domain_ops gb_gpio_domain_ops = {
534         .map    = gb_gpio_irq_map,
535         .unmap  = gb_gpio_irq_unmap,
536 };
537
538 /**
539  * gb_gpio_irqchip_remove() - removes an irqchip added to a gb_gpio_controller
540  * @ggc: the gb_gpio_controller to remove the irqchip from
541  *
542  * This is called only from gb_gpio_remove()
543  */
544 static void gb_gpio_irqchip_remove(struct gb_gpio_controller *ggc)
545 {
546         unsigned int offset;
547
548         /* Remove all IRQ mappings and delete the domain */
549         if (ggc->irqdomain) {
550                 for (offset = 0; offset < (ggc->line_max + 1); offset++)
551                         irq_dispose_mapping(irq_find_mapping(ggc->irqdomain, offset));
552                 irq_domain_remove(ggc->irqdomain);
553         }
554
555         if (ggc->irqchip) {
556                 ggc->irqchip = NULL;
557         }
558 }
559
560
561 /**
562  * gb_gpio_irqchip_add() - adds an irqchip to a gpio chip
563  * @chip: the gpio chip to add the irqchip to
564  * @irqchip: the irqchip to add to the adapter
565  * @first_irq: if not dynamically assigned, the base (first) IRQ to
566  * allocate gpio irqs from
567  * @handler: the irq handler to use (often a predefined irq core function)
568  * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
569  * to have the core avoid setting up any default type in the hardware.
570  *
571  * This function closely associates a certain irqchip with a certain
572  * gpio chip, providing an irq domain to translate the local IRQs to
573  * global irqs, and making sure that the gpio chip
574  * is passed as chip data to all related functions. Driver callbacks
575  * need to use container_of() to get their local state containers back
576  * from the gpio chip passed as chip data. An irqdomain will be stored
577  * in the gpio chip that shall be used by the driver to handle IRQ number
578  * translation. The gpio chip will need to be initialized and registered
579  * before calling this function.
580  */
581 static int gb_gpio_irqchip_add(struct gpio_chip *chip,
582                          struct irq_chip *irqchip,
583                          unsigned int first_irq,
584                          irq_flow_handler_t handler,
585                          unsigned int type)
586 {
587         struct gb_gpio_controller *ggc;
588         unsigned int offset;
589         unsigned irq_base;
590
591         if (!chip || !irqchip)
592                 return -EINVAL;
593
594         ggc = gpio_chip_to_gb_gpio_controller(chip);
595
596         ggc->irqchip = irqchip;
597         ggc->irq_handler = handler;
598         ggc->irq_default_type = type;
599         ggc->irqdomain = irq_domain_add_simple(NULL,
600                                         ggc->line_max + 1, first_irq,
601                                         &gb_gpio_domain_ops, chip);
602         if (!ggc->irqdomain) {
603                 ggc->irqchip = NULL;
604                 return -EINVAL;
605         }
606
607         /*
608          * Prepare the mapping since the irqchip shall be orthogonal to
609          * any gpio calls. If the first_irq was zero, this is
610          * necessary to allocate descriptors for all IRQs.
611          */
612         for (offset = 0; offset < (ggc->line_max + 1); offset++) {
613                 irq_base = irq_create_mapping(ggc->irqdomain, offset);
614                 if (offset == 0)
615                         ggc->irq_base = irq_base;
616         }
617
618         return 0;
619 }
620
621 static int gb_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
622 {
623         struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
624
625         return irq_find_mapping(ggc->irqdomain, offset);
626 }
627
628 static int gb_gpio_connection_init(struct gb_connection *connection)
629 {
630         struct gb_gpio_controller *ggc;
631         struct gpio_chip *gpio;
632         struct irq_chip *irqc;
633         int ret;
634
635         ggc = kzalloc(sizeof(*ggc), GFP_KERNEL);
636         if (!ggc)
637                 return -ENOMEM;
638         ggc->connection = connection;
639         connection->private = ggc;
640
641         ret = gb_gpio_controller_setup(ggc);
642         if (ret)
643                 goto err_free_controller;
644
645         irqc = &ggc->irqc;
646         irqc->irq_mask = gb_gpio_irq_mask;
647         irqc->irq_unmask = gb_gpio_irq_unmask;
648         irqc->irq_set_type = gb_gpio_irq_set_type;
649         irqc->irq_bus_lock = gb_gpio_irq_bus_lock;
650         irqc->irq_bus_sync_unlock = gb_gpio_irq_bus_sync_unlock;
651         irqc->name = "greybus_gpio";
652
653         mutex_init(&ggc->irq_lock);
654
655         gpio = &ggc->chip;
656
657         gpio->label = "greybus_gpio";
658         gpio->dev = &connection->dev;
659         gpio->owner = THIS_MODULE;
660
661         gpio->request = gb_gpio_request;
662         gpio->free = gb_gpio_free;
663         gpio->get_direction = gb_gpio_get_direction;
664         gpio->direction_input = gb_gpio_direction_input;
665         gpio->direction_output = gb_gpio_direction_output;
666         gpio->get = gb_gpio_get;
667         gpio->set = gb_gpio_set;
668         gpio->set_debounce = gb_gpio_set_debounce;
669         gpio->to_irq = gb_gpio_to_irq;
670         gpio->base = -1;                /* Allocate base dynamically */
671         gpio->ngpio = ggc->line_max + 1;
672         gpio->can_sleep = true;
673
674         ret = gpiochip_add(gpio);
675         if (ret) {
676                 dev_err(&connection->dev, "failed to add gpio chip: %d\n",
677                         ret);
678                 goto err_free_lines;
679         }
680
681         ret = gb_gpio_irqchip_add(gpio, irqc, 0,
682                                    handle_level_irq, IRQ_TYPE_NONE);
683         if (ret) {
684                 dev_err(&connection->dev, "failed to add irq chip: %d\n", ret);
685                 goto irqchip_err;
686         }
687
688         return 0;
689
690 irqchip_err:
691         gb_gpiochip_remove(gpio);
692 err_free_lines:
693         kfree(ggc->lines);
694 err_free_controller:
695         kfree(ggc);
696         return ret;
697 }
698
699 static void gb_gpio_connection_exit(struct gb_connection *connection)
700 {
701         struct gb_gpio_controller *ggc = connection->private;
702
703         if (!ggc)
704                 return;
705
706         gb_gpio_irqchip_remove(ggc);
707         gb_gpiochip_remove(&ggc->chip);
708         /* kref_put(ggc->connection) */
709         kfree(ggc->lines);
710         kfree(ggc);
711 }
712
713 static struct gb_protocol gpio_protocol = {
714         .name                   = "gpio",
715         .id                     = GREYBUS_PROTOCOL_GPIO,
716         .major                  = GB_GPIO_VERSION_MAJOR,
717         .minor                  = GB_GPIO_VERSION_MINOR,
718         .connection_init        = gb_gpio_connection_init,
719         .connection_exit        = gb_gpio_connection_exit,
720         .request_recv           = gb_gpio_request_recv,
721 };
722
723 gb_builtin_protocol_driver(gpio_protocol);