1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright 2014 Google Inc.
6 * Copyright 2014 Linaro Ltd.
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/gpio.h>
13 #include <linux/irq.h>
14 #include <linux/irqdomain.h>
15 #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 gbphy_device *gbphy_dev;
36 struct gb_connection *connection;
37 u8 line_max; /* max line number */
38 struct gb_gpio_line *lines;
40 struct gpio_chip chip;
42 struct irq_chip *irqchip;
43 struct irq_domain *irqdomain;
44 unsigned int irq_base;
45 irq_flow_handler_t irq_handler;
46 unsigned int irq_default_type;
47 struct mutex irq_lock;
49 #define gpio_chip_to_gb_gpio_controller(chip) \
50 container_of(chip, struct gb_gpio_controller, chip)
51 #define irq_data_to_gpio_chip(d) (d->domain->host_data)
53 static int gb_gpio_line_count_operation(struct gb_gpio_controller *ggc)
55 struct gb_gpio_line_count_response response;
58 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_LINE_COUNT,
59 NULL, 0, &response, sizeof(response));
61 ggc->line_max = response.count;
65 static int gb_gpio_activate_operation(struct gb_gpio_controller *ggc, u8 which)
67 struct gb_gpio_activate_request request;
68 struct gbphy_device *gbphy_dev = ggc->gbphy_dev;
71 ret = gbphy_runtime_get_sync(gbphy_dev);
75 request.which = which;
76 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_ACTIVATE,
77 &request, sizeof(request), NULL, 0);
79 gbphy_runtime_put_autosuspend(gbphy_dev);
83 ggc->lines[which].active = true;
88 static void gb_gpio_deactivate_operation(struct gb_gpio_controller *ggc,
91 struct gbphy_device *gbphy_dev = ggc->gbphy_dev;
92 struct device *dev = &gbphy_dev->dev;
93 struct gb_gpio_deactivate_request request;
96 request.which = which;
97 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DEACTIVATE,
98 &request, sizeof(request), NULL, 0);
100 dev_err(dev, "failed to deactivate gpio %u\n", which);
104 ggc->lines[which].active = false;
107 gbphy_runtime_put_autosuspend(gbphy_dev);
110 static int gb_gpio_get_direction_operation(struct gb_gpio_controller *ggc,
113 struct device *dev = &ggc->gbphy_dev->dev;
114 struct gb_gpio_get_direction_request request;
115 struct gb_gpio_get_direction_response response;
119 request.which = which;
120 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_DIRECTION,
121 &request, sizeof(request),
122 &response, sizeof(response));
126 direction = response.direction;
127 if (direction && direction != 1) {
128 dev_warn(dev, "gpio %u direction was %u (should be 0 or 1)\n",
131 ggc->lines[which].direction = direction ? 1 : 0;
135 static int gb_gpio_direction_in_operation(struct gb_gpio_controller *ggc,
138 struct gb_gpio_direction_in_request request;
141 request.which = which;
142 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_IN,
143 &request, sizeof(request), NULL, 0);
145 ggc->lines[which].direction = 1;
149 static int gb_gpio_direction_out_operation(struct gb_gpio_controller *ggc,
150 u8 which, bool value_high)
152 struct gb_gpio_direction_out_request request;
155 request.which = which;
156 request.value = value_high ? 1 : 0;
157 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_OUT,
158 &request, sizeof(request), NULL, 0);
160 ggc->lines[which].direction = 0;
164 static int gb_gpio_get_value_operation(struct gb_gpio_controller *ggc,
167 struct device *dev = &ggc->gbphy_dev->dev;
168 struct gb_gpio_get_value_request request;
169 struct gb_gpio_get_value_response response;
173 request.which = which;
174 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_VALUE,
175 &request, sizeof(request),
176 &response, sizeof(response));
178 dev_err(dev, "failed to get value of gpio %u\n", which);
182 value = response.value;
183 if (value && value != 1) {
184 dev_warn(dev, "gpio %u value was %u (should be 0 or 1)\n",
187 ggc->lines[which].value = value ? 1 : 0;
191 static void gb_gpio_set_value_operation(struct gb_gpio_controller *ggc,
192 u8 which, bool value_high)
194 struct device *dev = &ggc->gbphy_dev->dev;
195 struct gb_gpio_set_value_request request;
198 if (ggc->lines[which].direction == 1) {
199 dev_warn(dev, "refusing to set value of input gpio %u\n",
204 request.which = which;
205 request.value = value_high ? 1 : 0;
206 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_VALUE,
207 &request, sizeof(request), NULL, 0);
209 dev_err(dev, "failed to set value of gpio %u\n", which);
213 ggc->lines[which].value = request.value;
216 static int gb_gpio_set_debounce_operation(struct gb_gpio_controller *ggc,
217 u8 which, u16 debounce_usec)
219 struct gb_gpio_set_debounce_request request;
222 request.which = which;
223 request.usec = cpu_to_le16(debounce_usec);
224 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_DEBOUNCE,
225 &request, sizeof(request), NULL, 0);
227 ggc->lines[which].debounce_usec = debounce_usec;
231 static void _gb_gpio_irq_mask(struct gb_gpio_controller *ggc, u8 hwirq)
233 struct device *dev = &ggc->gbphy_dev->dev;
234 struct gb_gpio_irq_mask_request request;
237 request.which = hwirq;
238 ret = gb_operation_sync(ggc->connection,
239 GB_GPIO_TYPE_IRQ_MASK,
240 &request, sizeof(request), NULL, 0);
242 dev_err(dev, "failed to mask irq: %d\n", ret);
245 static void _gb_gpio_irq_unmask(struct gb_gpio_controller *ggc, u8 hwirq)
247 struct device *dev = &ggc->gbphy_dev->dev;
248 struct gb_gpio_irq_unmask_request request;
251 request.which = hwirq;
252 ret = gb_operation_sync(ggc->connection,
253 GB_GPIO_TYPE_IRQ_UNMASK,
254 &request, sizeof(request), NULL, 0);
256 dev_err(dev, "failed to unmask irq: %d\n", ret);
259 static void _gb_gpio_irq_set_type(struct gb_gpio_controller *ggc,
262 struct device *dev = &ggc->gbphy_dev->dev;
263 struct gb_gpio_irq_type_request request;
266 request.which = hwirq;
269 ret = gb_operation_sync(ggc->connection,
270 GB_GPIO_TYPE_IRQ_TYPE,
271 &request, sizeof(request), NULL, 0);
273 dev_err(dev, "failed to set irq type: %d\n", ret);
276 static void gb_gpio_irq_mask(struct irq_data *d)
278 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
279 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
280 struct gb_gpio_line *line = &ggc->lines[d->hwirq];
283 line->masked_pending = true;
286 static void gb_gpio_irq_unmask(struct irq_data *d)
288 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
289 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
290 struct gb_gpio_line *line = &ggc->lines[d->hwirq];
292 line->masked = false;
293 line->masked_pending = true;
296 static int gb_gpio_irq_set_type(struct irq_data *d, unsigned int type)
298 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
299 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
300 struct gb_gpio_line *line = &ggc->lines[d->hwirq];
301 struct device *dev = &ggc->gbphy_dev->dev;
306 irq_type = GB_GPIO_IRQ_TYPE_NONE;
308 case IRQ_TYPE_EDGE_RISING:
309 irq_type = GB_GPIO_IRQ_TYPE_EDGE_RISING;
311 case IRQ_TYPE_EDGE_FALLING:
312 irq_type = GB_GPIO_IRQ_TYPE_EDGE_FALLING;
314 case IRQ_TYPE_EDGE_BOTH:
315 irq_type = GB_GPIO_IRQ_TYPE_EDGE_BOTH;
317 case IRQ_TYPE_LEVEL_LOW:
318 irq_type = GB_GPIO_IRQ_TYPE_LEVEL_LOW;
320 case IRQ_TYPE_LEVEL_HIGH:
321 irq_type = GB_GPIO_IRQ_TYPE_LEVEL_HIGH;
324 dev_err(dev, "unsupported irq type: %u\n", type);
328 line->irq_type = irq_type;
329 line->irq_type_pending = true;
334 static void gb_gpio_irq_bus_lock(struct irq_data *d)
336 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
337 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
339 mutex_lock(&ggc->irq_lock);
342 static void gb_gpio_irq_bus_sync_unlock(struct irq_data *d)
344 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
345 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
346 struct gb_gpio_line *line = &ggc->lines[d->hwirq];
348 if (line->irq_type_pending) {
349 _gb_gpio_irq_set_type(ggc, d->hwirq, line->irq_type);
350 line->irq_type_pending = false;
353 if (line->masked_pending) {
355 _gb_gpio_irq_mask(ggc, d->hwirq);
357 _gb_gpio_irq_unmask(ggc, d->hwirq);
358 line->masked_pending = false;
361 mutex_unlock(&ggc->irq_lock);
364 static int gb_gpio_request_handler(struct gb_operation *op)
366 struct gb_connection *connection = op->connection;
367 struct gb_gpio_controller *ggc = gb_connection_get_data(connection);
368 struct device *dev = &ggc->gbphy_dev->dev;
369 struct gb_message *request;
370 struct gb_gpio_irq_event_request *event;
373 struct irq_desc *desc;
375 if (type != GB_GPIO_TYPE_IRQ_EVENT) {
376 dev_err(dev, "unsupported unsolicited request: %u\n", type);
380 request = op->request;
382 if (request->payload_size < sizeof(*event)) {
383 dev_err(dev, "short event received (%zu < %zu)\n",
384 request->payload_size, sizeof(*event));
388 event = request->payload;
389 if (event->which > ggc->line_max) {
390 dev_err(dev, "invalid hw irq: %d\n", event->which);
394 irq = irq_find_mapping(ggc->irqdomain, event->which);
396 dev_err(dev, "failed to find IRQ\n");
399 desc = irq_to_desc(irq);
401 dev_err(dev, "failed to look up irq\n");
406 generic_handle_irq_desc(desc);
412 static int gb_gpio_request(struct gpio_chip *chip, unsigned int offset)
414 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
416 return gb_gpio_activate_operation(ggc, (u8)offset);
419 static void gb_gpio_free(struct gpio_chip *chip, unsigned int offset)
421 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
423 gb_gpio_deactivate_operation(ggc, (u8)offset);
426 static int gb_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
428 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
433 ret = gb_gpio_get_direction_operation(ggc, which);
437 return ggc->lines[which].direction ? 1 : 0;
440 static int gb_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
442 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
444 return gb_gpio_direction_in_operation(ggc, (u8)offset);
447 static int gb_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
450 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
452 return gb_gpio_direction_out_operation(ggc, (u8)offset, !!value);
455 static int gb_gpio_get(struct gpio_chip *chip, unsigned int offset)
457 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
462 ret = gb_gpio_get_value_operation(ggc, which);
466 return ggc->lines[which].value;
469 static void gb_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
471 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
473 gb_gpio_set_value_operation(ggc, (u8)offset, !!value);
476 static int gb_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
477 unsigned long config)
479 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
482 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
485 debounce = pinconf_to_config_argument(config);
486 if (debounce > U16_MAX)
489 return gb_gpio_set_debounce_operation(ggc, (u8)offset, (u16)debounce);
492 static int gb_gpio_controller_setup(struct gb_gpio_controller *ggc)
496 /* Now find out how many lines there are */
497 ret = gb_gpio_line_count_operation(ggc);
501 ggc->lines = kcalloc(ggc->line_max + 1, sizeof(*ggc->lines),
510 * gb_gpio_irq_map() - maps an IRQ into a GB gpio irqchip
511 * @d: the irqdomain used by this irqchip
512 * @irq: the global irq number used by this GB gpio irqchip irq
513 * @hwirq: the local IRQ/GPIO line offset on this GB gpio
515 * This function will set up the mapping for a certain IRQ line on a
516 * GB gpio by assigning the GB gpio as chip data, and using the irqchip
517 * stored inside the GB gpio.
519 static int gb_gpio_irq_map(struct irq_domain *domain, unsigned int irq,
520 irq_hw_number_t hwirq)
522 struct gpio_chip *chip = domain->host_data;
523 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
525 irq_set_chip_data(irq, ggc);
526 irq_set_chip_and_handler(irq, ggc->irqchip, ggc->irq_handler);
527 irq_set_noprobe(irq);
529 * No set-up of the hardware will happen if IRQ_TYPE_NONE
530 * is passed as default type.
532 if (ggc->irq_default_type != IRQ_TYPE_NONE)
533 irq_set_irq_type(irq, ggc->irq_default_type);
538 static void gb_gpio_irq_unmap(struct irq_domain *d, unsigned int irq)
540 irq_set_chip_and_handler(irq, NULL, NULL);
541 irq_set_chip_data(irq, NULL);
544 static const struct irq_domain_ops gb_gpio_domain_ops = {
545 .map = gb_gpio_irq_map,
546 .unmap = gb_gpio_irq_unmap,
550 * gb_gpio_irqchip_remove() - removes an irqchip added to a gb_gpio_controller
551 * @ggc: the gb_gpio_controller to remove the irqchip from
553 * This is called only from gb_gpio_remove()
555 static void gb_gpio_irqchip_remove(struct gb_gpio_controller *ggc)
559 /* Remove all IRQ mappings and delete the domain */
560 if (ggc->irqdomain) {
561 for (offset = 0; offset < (ggc->line_max + 1); offset++)
562 irq_dispose_mapping(irq_find_mapping(ggc->irqdomain,
564 irq_domain_remove(ggc->irqdomain);
572 * gb_gpio_irqchip_add() - adds an irqchip to a gpio chip
573 * @chip: the gpio chip to add the irqchip to
574 * @irqchip: the irqchip to add to the adapter
575 * @first_irq: if not dynamically assigned, the base (first) IRQ to
576 * allocate gpio irqs from
577 * @handler: the irq handler to use (often a predefined irq core function)
578 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
579 * to have the core avoid setting up any default type in the hardware.
581 * This function closely associates a certain irqchip with a certain
582 * gpio chip, providing an irq domain to translate the local IRQs to
583 * global irqs, and making sure that the gpio chip
584 * is passed as chip data to all related functions. Driver callbacks
585 * need to use container_of() to get their local state containers back
586 * from the gpio chip passed as chip data. An irqdomain will be stored
587 * in the gpio chip that shall be used by the driver to handle IRQ number
588 * translation. The gpio chip will need to be initialized and registered
589 * before calling this function.
591 static int gb_gpio_irqchip_add(struct gpio_chip *chip,
592 struct irq_chip *irqchip,
593 unsigned int first_irq,
594 irq_flow_handler_t handler,
597 struct gb_gpio_controller *ggc;
599 unsigned int irq_base;
601 if (!chip || !irqchip)
604 ggc = gpio_chip_to_gb_gpio_controller(chip);
606 ggc->irqchip = irqchip;
607 ggc->irq_handler = handler;
608 ggc->irq_default_type = type;
609 ggc->irqdomain = irq_domain_add_simple(NULL,
610 ggc->line_max + 1, first_irq,
611 &gb_gpio_domain_ops, chip);
612 if (!ggc->irqdomain) {
618 * Prepare the mapping since the irqchip shall be orthogonal to
619 * any gpio calls. If the first_irq was zero, this is
620 * necessary to allocate descriptors for all IRQs.
622 for (offset = 0; offset < (ggc->line_max + 1); offset++) {
623 irq_base = irq_create_mapping(ggc->irqdomain, offset);
625 ggc->irq_base = irq_base;
631 static int gb_gpio_to_irq(struct gpio_chip *chip, unsigned int offset)
633 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
635 return irq_find_mapping(ggc->irqdomain, offset);
638 static int gb_gpio_probe(struct gbphy_device *gbphy_dev,
639 const struct gbphy_device_id *id)
641 struct gb_connection *connection;
642 struct gb_gpio_controller *ggc;
643 struct gpio_chip *gpio;
644 struct irq_chip *irqc;
647 ggc = kzalloc(sizeof(*ggc), GFP_KERNEL);
651 connection = gb_connection_create(gbphy_dev->bundle,
652 le16_to_cpu(gbphy_dev->cport_desc->id),
653 gb_gpio_request_handler);
654 if (IS_ERR(connection)) {
655 ret = PTR_ERR(connection);
659 ggc->connection = connection;
660 gb_connection_set_data(connection, ggc);
661 ggc->gbphy_dev = gbphy_dev;
662 gb_gbphy_set_data(gbphy_dev, ggc);
664 ret = gb_connection_enable_tx(connection);
666 goto exit_connection_destroy;
668 ret = gb_gpio_controller_setup(ggc);
670 goto exit_connection_disable;
673 irqc->irq_mask = gb_gpio_irq_mask;
674 irqc->irq_unmask = gb_gpio_irq_unmask;
675 irqc->irq_set_type = gb_gpio_irq_set_type;
676 irqc->irq_bus_lock = gb_gpio_irq_bus_lock;
677 irqc->irq_bus_sync_unlock = gb_gpio_irq_bus_sync_unlock;
678 irqc->name = "greybus_gpio";
680 mutex_init(&ggc->irq_lock);
684 gpio->label = "greybus_gpio";
685 gpio->parent = &gbphy_dev->dev;
686 gpio->owner = THIS_MODULE;
688 gpio->request = gb_gpio_request;
689 gpio->free = gb_gpio_free;
690 gpio->get_direction = gb_gpio_get_direction;
691 gpio->direction_input = gb_gpio_direction_input;
692 gpio->direction_output = gb_gpio_direction_output;
693 gpio->get = gb_gpio_get;
694 gpio->set = gb_gpio_set;
695 gpio->set_config = gb_gpio_set_config;
696 gpio->to_irq = gb_gpio_to_irq;
697 gpio->base = -1; /* Allocate base dynamically */
698 gpio->ngpio = ggc->line_max + 1;
699 gpio->can_sleep = true;
701 ret = gb_connection_enable(connection);
705 ret = gb_gpio_irqchip_add(gpio, irqc, 0,
706 handle_level_irq, IRQ_TYPE_NONE);
708 dev_err(&gbphy_dev->dev, "failed to add irq chip: %d\n", ret);
712 ret = gpiochip_add(gpio);
714 dev_err(&gbphy_dev->dev, "failed to add gpio chip: %d\n", ret);
715 goto exit_gpio_irqchip_remove;
718 gbphy_runtime_put_autosuspend(gbphy_dev);
721 exit_gpio_irqchip_remove:
722 gb_gpio_irqchip_remove(ggc);
725 exit_connection_disable:
726 gb_connection_disable(connection);
727 exit_connection_destroy:
728 gb_connection_destroy(connection);
734 static void gb_gpio_remove(struct gbphy_device *gbphy_dev)
736 struct gb_gpio_controller *ggc = gb_gbphy_get_data(gbphy_dev);
737 struct gb_connection *connection = ggc->connection;
740 ret = gbphy_runtime_get_sync(gbphy_dev);
742 gbphy_runtime_get_noresume(gbphy_dev);
744 gb_connection_disable_rx(connection);
745 gpiochip_remove(&ggc->chip);
746 gb_gpio_irqchip_remove(ggc);
747 gb_connection_disable(connection);
748 gb_connection_destroy(connection);
753 static const struct gbphy_device_id gb_gpio_id_table[] = {
754 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_GPIO) },
757 MODULE_DEVICE_TABLE(gbphy, gb_gpio_id_table);
759 static struct gbphy_driver gpio_driver = {
761 .probe = gb_gpio_probe,
762 .remove = gb_gpio_remove,
763 .id_table = gb_gpio_id_table,
766 module_gbphy_driver(gpio_driver);
767 MODULE_LICENSE("GPL v2");