4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
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>
21 /* The following has to be an array of line_max entries */
22 /* --> make them just a flags field */
24 direction: 1, /* 0 = output, 1 = input */
25 value: 1; /* 0 = low, 1 = high */
29 bool irq_type_pending;
34 struct gb_gpio_controller {
35 struct gb_connection *connection;
36 u8 line_max; /* max line number */
37 struct gb_gpio_line *lines;
39 struct gpio_chip chip;
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;
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)
52 static int gb_gpio_line_count_operation(struct gb_gpio_controller *ggc)
54 struct gb_gpio_line_count_response response;
57 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_LINE_COUNT,
58 NULL, 0, &response, sizeof(response));
60 ggc->line_max = response.count;
64 static int gb_gpio_activate_operation(struct gb_gpio_controller *ggc, u8 which)
66 struct gb_gpio_activate_request request;
69 request.which = which;
70 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_ACTIVATE,
71 &request, sizeof(request), NULL, 0);
73 ggc->lines[which].active = true;
77 static void gb_gpio_deactivate_operation(struct gb_gpio_controller *ggc,
80 struct gb_gpio_deactivate_request request;
83 request.which = which;
84 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DEACTIVATE,
85 &request, sizeof(request), NULL, 0);
87 dev_err(ggc->chip.dev, "failed to deactivate gpio %u\n",
92 ggc->lines[which].active = false;
95 static int gb_gpio_get_direction_operation(struct gb_gpio_controller *ggc,
98 struct gb_gpio_get_direction_request request;
99 struct gb_gpio_get_direction_response response;
103 request.which = which;
104 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_DIRECTION,
105 &request, sizeof(request),
106 &response, sizeof(response));
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",
116 ggc->lines[which].direction = direction ? 1 : 0;
120 static int gb_gpio_direction_in_operation(struct gb_gpio_controller *ggc,
123 struct gb_gpio_direction_in_request request;
126 request.which = which;
127 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_IN,
128 &request, sizeof(request), NULL, 0);
130 ggc->lines[which].direction = 1;
134 static int gb_gpio_direction_out_operation(struct gb_gpio_controller *ggc,
135 u8 which, bool value_high)
137 struct gb_gpio_direction_out_request request;
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);
145 ggc->lines[which].direction = 0;
149 static int gb_gpio_get_value_operation(struct gb_gpio_controller *ggc,
152 struct gb_gpio_get_value_request request;
153 struct gb_gpio_get_value_response response;
157 request.which = which;
158 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_VALUE,
159 &request, sizeof(request),
160 &response, sizeof(response));
162 dev_err(ggc->chip.dev, "failed to get value of gpio %u\n",
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",
173 ggc->lines[which].value = value ? 1 : 0;
177 static void gb_gpio_set_value_operation(struct gb_gpio_controller *ggc,
178 u8 which, bool value_high)
180 struct gb_gpio_set_value_request request;
183 if (ggc->lines[which].direction == 1) {
184 dev_warn(ggc->chip.dev,
185 "refusing to set value of input gpio %u\n", which);
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);
194 dev_err(ggc->chip.dev, "failed to set value of gpio %u\n",
199 ggc->lines[which].value = request.value;
202 static int gb_gpio_set_debounce_operation(struct gb_gpio_controller *ggc,
203 u8 which, u16 debounce_usec)
205 struct gb_gpio_set_debounce_request request;
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);
213 ggc->lines[which].debounce_usec = debounce_usec;
217 static void _gb_gpio_irq_mask(struct gb_gpio_controller *ggc, u8 hwirq)
219 struct gb_gpio_irq_mask_request request;
222 request.which = hwirq;
223 ret = gb_operation_sync(ggc->connection,
224 GB_GPIO_TYPE_IRQ_MASK,
225 &request, sizeof(request), NULL, 0);
227 dev_err(ggc->chip.dev, "failed to mask irq: %d\n", ret);
230 static void _gb_gpio_irq_unmask(struct gb_gpio_controller *ggc, u8 hwirq)
232 struct gb_gpio_irq_unmask_request request;
235 request.which = hwirq;
236 ret = gb_operation_sync(ggc->connection,
237 GB_GPIO_TYPE_IRQ_UNMASK,
238 &request, sizeof(request), NULL, 0);
240 dev_err(ggc->chip.dev, "failed to unmask irq: %d\n", ret);
243 static void _gb_gpio_irq_set_type(struct gb_gpio_controller *ggc,
246 struct gb_gpio_irq_type_request request;
249 request.which = hwirq;
252 ret = gb_operation_sync(ggc->connection,
253 GB_GPIO_TYPE_IRQ_TYPE,
254 &request, sizeof(request), NULL, 0);
256 dev_err(ggc->chip.dev, "failed to set irq type: %d\n", ret);
259 static void gb_gpio_irq_mask(struct irq_data *d)
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];
266 line->masked_pending = true;
269 static void gb_gpio_irq_unmask(struct irq_data *d)
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];
275 line->masked = false;
276 line->masked_pending = true;
279 static int gb_gpio_irq_set_type(struct irq_data *d, unsigned int type)
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];
288 irq_type = GB_GPIO_IRQ_TYPE_NONE;
290 case IRQ_TYPE_EDGE_RISING:
291 irq_type = GB_GPIO_IRQ_TYPE_EDGE_RISING;
293 case IRQ_TYPE_EDGE_FALLING:
294 irq_type = GB_GPIO_IRQ_TYPE_EDGE_FALLING;
296 case IRQ_TYPE_EDGE_BOTH:
297 irq_type = GB_GPIO_IRQ_TYPE_EDGE_BOTH;
299 case IRQ_TYPE_LEVEL_LOW:
300 irq_type = GB_GPIO_IRQ_TYPE_LEVEL_LOW;
302 case IRQ_TYPE_LEVEL_HIGH:
303 irq_type = GB_GPIO_IRQ_TYPE_LEVEL_HIGH;
306 dev_err(chip->dev, "unsupported irq type: %u\n", type);
310 line->irq_type = irq_type;
311 line->irq_type_pending = true;
316 static void gb_gpio_irq_bus_lock(struct irq_data *d)
318 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
319 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
321 mutex_lock(&ggc->irq_lock);
324 static void gb_gpio_irq_bus_sync_unlock(struct irq_data *d)
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];
330 if (line->irq_type_pending) {
331 _gb_gpio_irq_set_type(ggc, d->hwirq, line->irq_type);
332 line->irq_type_pending = false;
335 if (line->masked_pending) {
337 _gb_gpio_irq_mask(ggc, d->hwirq);
339 _gb_gpio_irq_unmask(ggc, d->hwirq);
340 line->masked_pending = false;
343 mutex_unlock(&ggc->irq_lock);
346 static int gb_gpio_request_recv(u8 type, struct gb_operation *op)
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;
353 struct irq_desc *desc;
355 if (type != GB_GPIO_TYPE_IRQ_EVENT) {
356 dev_err(&connection->dev,
357 "unsupported unsolicited request: %u\n", type);
361 request = op->request;
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));
369 event = request->payload;
370 if (event->which > ggc->line_max) {
371 dev_err(ggc->chip.dev, "invalid hw irq: %d\n", event->which);
375 irq = irq_find_mapping(ggc->irqdomain, event->which);
377 dev_err(ggc->chip.dev, "failed to find IRQ\n");
380 desc = irq_to_desc(irq);
382 dev_err(ggc->chip.dev, "failed to look up irq\n");
387 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 2, 0)
388 generic_handle_irq_desc(irq, desc);
390 generic_handle_irq_desc(desc);
397 static int gb_gpio_request(struct gpio_chip *chip, unsigned offset)
399 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
401 return gb_gpio_activate_operation(ggc, (u8)offset);
404 static void gb_gpio_free(struct gpio_chip *chip, unsigned offset)
406 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
408 gb_gpio_deactivate_operation(ggc, (u8)offset);
411 static int gb_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
413 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
418 ret = gb_gpio_get_direction_operation(ggc, which);
422 return ggc->lines[which].direction ? 1 : 0;
425 static int gb_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
427 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
429 return gb_gpio_direction_in_operation(ggc, (u8)offset);
432 static int gb_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
435 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
437 return gb_gpio_direction_out_operation(ggc, (u8)offset, !!value);
440 static int gb_gpio_get(struct gpio_chip *chip, unsigned offset)
442 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
447 ret = gb_gpio_get_value_operation(ggc, which);
451 return ggc->lines[which].value;
454 static void gb_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
456 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
458 gb_gpio_set_value_operation(ggc, (u8)offset, !!value);
461 static int gb_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
464 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
467 if (debounce > U16_MAX)
469 usec = (u16)debounce;
471 return gb_gpio_set_debounce_operation(ggc, (u8)offset, usec);
474 static int gb_gpio_controller_setup(struct gb_gpio_controller *ggc)
478 /* Now find out how many lines there are */
479 ret = gb_gpio_line_count_operation(ggc);
483 ggc->lines = kcalloc(ggc->line_max + 1, sizeof(*ggc->lines),
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
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.
501 static int gb_gpio_irq_map(struct irq_domain *domain, unsigned int irq,
502 irq_hw_number_t hwirq)
504 struct gpio_chip *chip = domain->host_data;
505 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
507 irq_set_chip_data(irq, ggc);
508 irq_set_chip_and_handler(irq, ggc->irqchip, ggc->irq_handler);
510 set_irq_flags(irq, IRQF_VALID);
512 irq_set_noprobe(irq);
515 * No set-up of the hardware will happen if IRQ_TYPE_NONE
516 * is passed as default type.
518 if (ggc->irq_default_type != IRQ_TYPE_NONE)
519 irq_set_irq_type(irq, ggc->irq_default_type);
524 static void gb_gpio_irq_unmap(struct irq_domain *d, unsigned int irq)
527 set_irq_flags(irq, 0);
529 irq_set_chip_and_handler(irq, NULL, NULL);
530 irq_set_chip_data(irq, NULL);
533 static const struct irq_domain_ops gb_gpio_domain_ops = {
534 .map = gb_gpio_irq_map,
535 .unmap = gb_gpio_irq_unmap,
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
542 * This is called only from gb_gpio_remove()
544 static void gb_gpio_irqchip_remove(struct gb_gpio_controller *ggc)
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);
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.
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.
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,
587 struct gb_gpio_controller *ggc;
591 if (!chip || !irqchip)
594 ggc = gpio_chip_to_gb_gpio_controller(chip);
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) {
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.
612 for (offset = 0; offset < (ggc->line_max + 1); offset++) {
613 irq_base = irq_create_mapping(ggc->irqdomain, offset);
615 ggc->irq_base = irq_base;
621 static int gb_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
623 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
625 return irq_find_mapping(ggc->irqdomain, offset);
628 static int gb_gpio_connection_init(struct gb_connection *connection)
630 struct gb_gpio_controller *ggc;
631 struct gpio_chip *gpio;
632 struct irq_chip *irqc;
635 ggc = kzalloc(sizeof(*ggc), GFP_KERNEL);
638 ggc->connection = connection;
639 connection->private = ggc;
641 ret = gb_gpio_controller_setup(ggc);
643 goto err_free_controller;
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";
653 mutex_init(&ggc->irq_lock);
657 gpio->label = "greybus_gpio";
658 gpio->dev = &connection->dev;
659 gpio->owner = THIS_MODULE;
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;
674 ret = gpiochip_add(gpio);
676 dev_err(&connection->dev, "failed to add gpio chip: %d\n",
681 ret = gb_gpio_irqchip_add(gpio, irqc, 0,
682 handle_level_irq, IRQ_TYPE_NONE);
684 dev_err(&connection->dev, "failed to add irq chip: %d\n", ret);
691 gb_gpiochip_remove(gpio);
699 static void gb_gpio_connection_exit(struct gb_connection *connection)
701 struct gb_gpio_controller *ggc = connection->private;
706 gb_gpio_irqchip_remove(ggc);
707 gb_gpiochip_remove(&ggc->chip);
708 /* kref_put(ggc->connection) */
713 static struct gb_protocol gpio_protocol = {
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,
723 gb_builtin_protocol_driver(gpio_protocol);