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>
22 /* The following has to be an array of line_max entries */
23 /* --> make them just a flags field */
25 direction
: 1, /* 0 = output, 1 = input */
26 value
: 1; /* 0 = low, 1 = high */
30 bool irq_type_pending
;
35 struct gb_gpio_controller
{
36 struct gbphy_device
*gbphy_dev
;
37 struct gb_connection
*connection
;
38 u8 line_max
; /* max line number */
39 struct gb_gpio_line
*lines
;
41 struct gpio_chip chip
;
43 struct irq_chip
*irqchip
;
44 struct irq_domain
*irqdomain
;
45 unsigned int irq_base
;
46 irq_flow_handler_t irq_handler
;
47 unsigned int irq_default_type
;
48 struct mutex irq_lock
;
50 #define gpio_chip_to_gb_gpio_controller(chip) \
51 container_of(chip, struct gb_gpio_controller, chip)
52 #define irq_data_to_gpio_chip(d) (d->domain->host_data)
54 static int gb_gpio_line_count_operation(struct gb_gpio_controller
*ggc
)
56 struct gb_gpio_line_count_response response
;
59 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_LINE_COUNT
,
60 NULL
, 0, &response
, sizeof(response
));
62 ggc
->line_max
= response
.count
;
66 static int gb_gpio_activate_operation(struct gb_gpio_controller
*ggc
, u8 which
)
68 struct gb_gpio_activate_request request
;
69 struct gbphy_device
*gbphy_dev
= ggc
->gbphy_dev
;
72 ret
= gbphy_runtime_get_sync(gbphy_dev
);
76 request
.which
= which
;
77 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_ACTIVATE
,
78 &request
, sizeof(request
), NULL
, 0);
80 gbphy_runtime_put_autosuspend(gbphy_dev
);
84 ggc
->lines
[which
].active
= true;
89 static void gb_gpio_deactivate_operation(struct gb_gpio_controller
*ggc
,
92 struct gbphy_device
*gbphy_dev
= ggc
->gbphy_dev
;
93 struct device
*dev
= &gbphy_dev
->dev
;
94 struct gb_gpio_deactivate_request request
;
97 request
.which
= which
;
98 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_DEACTIVATE
,
99 &request
, sizeof(request
), NULL
, 0);
101 dev_err(dev
, "failed to deactivate gpio %u\n", which
);
105 ggc
->lines
[which
].active
= false;
108 gbphy_runtime_put_autosuspend(gbphy_dev
);
111 static int gb_gpio_get_direction_operation(struct gb_gpio_controller
*ggc
,
114 struct device
*dev
= &ggc
->gbphy_dev
->dev
;
115 struct gb_gpio_get_direction_request request
;
116 struct gb_gpio_get_direction_response response
;
120 request
.which
= which
;
121 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_GET_DIRECTION
,
122 &request
, sizeof(request
),
123 &response
, sizeof(response
));
127 direction
= response
.direction
;
128 if (direction
&& direction
!= 1) {
129 dev_warn(dev
, "gpio %u direction was %u (should be 0 or 1)\n",
132 ggc
->lines
[which
].direction
= direction
? 1 : 0;
136 static int gb_gpio_direction_in_operation(struct gb_gpio_controller
*ggc
,
139 struct gb_gpio_direction_in_request request
;
142 request
.which
= which
;
143 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_DIRECTION_IN
,
144 &request
, sizeof(request
), NULL
, 0);
146 ggc
->lines
[which
].direction
= 1;
150 static int gb_gpio_direction_out_operation(struct gb_gpio_controller
*ggc
,
151 u8 which
, bool value_high
)
153 struct gb_gpio_direction_out_request request
;
156 request
.which
= which
;
157 request
.value
= value_high
? 1 : 0;
158 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_DIRECTION_OUT
,
159 &request
, sizeof(request
), NULL
, 0);
161 ggc
->lines
[which
].direction
= 0;
165 static int gb_gpio_get_value_operation(struct gb_gpio_controller
*ggc
,
168 struct device
*dev
= &ggc
->gbphy_dev
->dev
;
169 struct gb_gpio_get_value_request request
;
170 struct gb_gpio_get_value_response response
;
174 request
.which
= which
;
175 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_GET_VALUE
,
176 &request
, sizeof(request
),
177 &response
, sizeof(response
));
179 dev_err(dev
, "failed to get value of gpio %u\n", which
);
183 value
= response
.value
;
184 if (value
&& value
!= 1) {
185 dev_warn(dev
, "gpio %u value was %u (should be 0 or 1)\n",
188 ggc
->lines
[which
].value
= value
? 1 : 0;
192 static void gb_gpio_set_value_operation(struct gb_gpio_controller
*ggc
,
193 u8 which
, bool value_high
)
195 struct device
*dev
= &ggc
->gbphy_dev
->dev
;
196 struct gb_gpio_set_value_request request
;
199 if (ggc
->lines
[which
].direction
== 1) {
200 dev_warn(dev
, "refusing to set value of input gpio %u\n",
205 request
.which
= which
;
206 request
.value
= value_high
? 1 : 0;
207 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_SET_VALUE
,
208 &request
, sizeof(request
), NULL
, 0);
210 dev_err(dev
, "failed to set value of gpio %u\n", which
);
214 ggc
->lines
[which
].value
= request
.value
;
217 static int gb_gpio_set_debounce_operation(struct gb_gpio_controller
*ggc
,
218 u8 which
, u16 debounce_usec
)
220 struct gb_gpio_set_debounce_request request
;
223 request
.which
= which
;
224 request
.usec
= cpu_to_le16(debounce_usec
);
225 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_SET_DEBOUNCE
,
226 &request
, sizeof(request
), NULL
, 0);
228 ggc
->lines
[which
].debounce_usec
= debounce_usec
;
232 static void _gb_gpio_irq_mask(struct gb_gpio_controller
*ggc
, u8 hwirq
)
234 struct device
*dev
= &ggc
->gbphy_dev
->dev
;
235 struct gb_gpio_irq_mask_request request
;
238 request
.which
= hwirq
;
239 ret
= gb_operation_sync(ggc
->connection
,
240 GB_GPIO_TYPE_IRQ_MASK
,
241 &request
, sizeof(request
), NULL
, 0);
243 dev_err(dev
, "failed to mask irq: %d\n", ret
);
246 static void _gb_gpio_irq_unmask(struct gb_gpio_controller
*ggc
, u8 hwirq
)
248 struct device
*dev
= &ggc
->gbphy_dev
->dev
;
249 struct gb_gpio_irq_unmask_request request
;
252 request
.which
= hwirq
;
253 ret
= gb_operation_sync(ggc
->connection
,
254 GB_GPIO_TYPE_IRQ_UNMASK
,
255 &request
, sizeof(request
), NULL
, 0);
257 dev_err(dev
, "failed to unmask irq: %d\n", ret
);
260 static void _gb_gpio_irq_set_type(struct gb_gpio_controller
*ggc
,
263 struct device
*dev
= &ggc
->gbphy_dev
->dev
;
264 struct gb_gpio_irq_type_request request
;
267 request
.which
= hwirq
;
270 ret
= gb_operation_sync(ggc
->connection
,
271 GB_GPIO_TYPE_IRQ_TYPE
,
272 &request
, sizeof(request
), NULL
, 0);
274 dev_err(dev
, "failed to set irq type: %d\n", ret
);
277 static void gb_gpio_irq_mask(struct irq_data
*d
)
279 struct gpio_chip
*chip
= irq_data_to_gpio_chip(d
);
280 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
281 struct gb_gpio_line
*line
= &ggc
->lines
[d
->hwirq
];
284 line
->masked_pending
= true;
287 static void gb_gpio_irq_unmask(struct irq_data
*d
)
289 struct gpio_chip
*chip
= irq_data_to_gpio_chip(d
);
290 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
291 struct gb_gpio_line
*line
= &ggc
->lines
[d
->hwirq
];
293 line
->masked
= false;
294 line
->masked_pending
= true;
297 static int gb_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
299 struct gpio_chip
*chip
= irq_data_to_gpio_chip(d
);
300 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
301 struct gb_gpio_line
*line
= &ggc
->lines
[d
->hwirq
];
302 struct device
*dev
= &ggc
->gbphy_dev
->dev
;
307 irq_type
= GB_GPIO_IRQ_TYPE_NONE
;
309 case IRQ_TYPE_EDGE_RISING
:
310 irq_type
= GB_GPIO_IRQ_TYPE_EDGE_RISING
;
312 case IRQ_TYPE_EDGE_FALLING
:
313 irq_type
= GB_GPIO_IRQ_TYPE_EDGE_FALLING
;
315 case IRQ_TYPE_EDGE_BOTH
:
316 irq_type
= GB_GPIO_IRQ_TYPE_EDGE_BOTH
;
318 case IRQ_TYPE_LEVEL_LOW
:
319 irq_type
= GB_GPIO_IRQ_TYPE_LEVEL_LOW
;
321 case IRQ_TYPE_LEVEL_HIGH
:
322 irq_type
= GB_GPIO_IRQ_TYPE_LEVEL_HIGH
;
325 dev_err(dev
, "unsupported irq type: %u\n", type
);
329 line
->irq_type
= irq_type
;
330 line
->irq_type_pending
= true;
335 static void gb_gpio_irq_bus_lock(struct irq_data
*d
)
337 struct gpio_chip
*chip
= irq_data_to_gpio_chip(d
);
338 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
340 mutex_lock(&ggc
->irq_lock
);
343 static void gb_gpio_irq_bus_sync_unlock(struct irq_data
*d
)
345 struct gpio_chip
*chip
= irq_data_to_gpio_chip(d
);
346 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
347 struct gb_gpio_line
*line
= &ggc
->lines
[d
->hwirq
];
349 if (line
->irq_type_pending
) {
350 _gb_gpio_irq_set_type(ggc
, d
->hwirq
, line
->irq_type
);
351 line
->irq_type_pending
= false;
354 if (line
->masked_pending
) {
356 _gb_gpio_irq_mask(ggc
, d
->hwirq
);
358 _gb_gpio_irq_unmask(ggc
, d
->hwirq
);
359 line
->masked_pending
= false;
362 mutex_unlock(&ggc
->irq_lock
);
365 static int gb_gpio_request_handler(struct gb_operation
*op
)
367 struct gb_connection
*connection
= op
->connection
;
368 struct gb_gpio_controller
*ggc
= gb_connection_get_data(connection
);
369 struct device
*dev
= &ggc
->gbphy_dev
->dev
;
370 struct gb_message
*request
;
371 struct gb_gpio_irq_event_request
*event
;
374 struct irq_desc
*desc
;
376 if (type
!= GB_GPIO_TYPE_IRQ_EVENT
) {
377 dev_err(dev
, "unsupported unsolicited request: %u\n", type
);
381 request
= op
->request
;
383 if (request
->payload_size
< sizeof(*event
)) {
384 dev_err(dev
, "short event received (%zu < %zu)\n",
385 request
->payload_size
, sizeof(*event
));
389 event
= request
->payload
;
390 if (event
->which
> ggc
->line_max
) {
391 dev_err(dev
, "invalid hw irq: %d\n", event
->which
);
395 irq
= irq_find_mapping(ggc
->irqdomain
, event
->which
);
397 dev_err(dev
, "failed to find IRQ\n");
400 desc
= irq_to_desc(irq
);
402 dev_err(dev
, "failed to look up irq\n");
407 generic_handle_irq_desc(desc
);
413 static int gb_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
415 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
417 return gb_gpio_activate_operation(ggc
, (u8
)offset
);
420 static void gb_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
422 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
424 gb_gpio_deactivate_operation(ggc
, (u8
)offset
);
427 static int gb_gpio_get_direction(struct gpio_chip
*chip
, unsigned offset
)
429 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
434 ret
= gb_gpio_get_direction_operation(ggc
, which
);
438 return ggc
->lines
[which
].direction
? 1 : 0;
441 static int gb_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
443 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
445 return gb_gpio_direction_in_operation(ggc
, (u8
)offset
);
448 static int gb_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
451 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
453 return gb_gpio_direction_out_operation(ggc
, (u8
)offset
, !!value
);
456 static int gb_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
458 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
463 ret
= gb_gpio_get_value_operation(ggc
, which
);
467 return ggc
->lines
[which
].value
;
470 static void gb_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
472 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
474 gb_gpio_set_value_operation(ggc
, (u8
)offset
, !!value
);
477 static int gb_gpio_set_debounce(struct gpio_chip
*chip
, unsigned offset
,
480 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
483 if (debounce
> U16_MAX
)
485 usec
= (u16
)debounce
;
487 return gb_gpio_set_debounce_operation(ggc
, (u8
)offset
, usec
);
490 static int gb_gpio_controller_setup(struct gb_gpio_controller
*ggc
)
494 /* Now find out how many lines there are */
495 ret
= gb_gpio_line_count_operation(ggc
);
499 ggc
->lines
= kcalloc(ggc
->line_max
+ 1, sizeof(*ggc
->lines
),
508 * gb_gpio_irq_map() - maps an IRQ into a GB gpio irqchip
509 * @d: the irqdomain used by this irqchip
510 * @irq: the global irq number used by this GB gpio irqchip irq
511 * @hwirq: the local IRQ/GPIO line offset on this GB gpio
513 * This function will set up the mapping for a certain IRQ line on a
514 * GB gpio by assigning the GB gpio as chip data, and using the irqchip
515 * stored inside the GB gpio.
517 static int gb_gpio_irq_map(struct irq_domain
*domain
, unsigned int irq
,
518 irq_hw_number_t hwirq
)
520 struct gpio_chip
*chip
= domain
->host_data
;
521 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
523 irq_set_chip_data(irq
, ggc
);
524 irq_set_chip_and_handler(irq
, ggc
->irqchip
, ggc
->irq_handler
);
525 irq_set_noprobe(irq
);
527 * No set-up of the hardware will happen if IRQ_TYPE_NONE
528 * is passed as default type.
530 if (ggc
->irq_default_type
!= IRQ_TYPE_NONE
)
531 irq_set_irq_type(irq
, ggc
->irq_default_type
);
536 static void gb_gpio_irq_unmap(struct irq_domain
*d
, unsigned int irq
)
538 irq_set_chip_and_handler(irq
, NULL
, NULL
);
539 irq_set_chip_data(irq
, NULL
);
542 static const struct irq_domain_ops gb_gpio_domain_ops
= {
543 .map
= gb_gpio_irq_map
,
544 .unmap
= gb_gpio_irq_unmap
,
548 * gb_gpio_irqchip_remove() - removes an irqchip added to a gb_gpio_controller
549 * @ggc: the gb_gpio_controller to remove the irqchip from
551 * This is called only from gb_gpio_remove()
553 static void gb_gpio_irqchip_remove(struct gb_gpio_controller
*ggc
)
557 /* Remove all IRQ mappings and delete the domain */
558 if (ggc
->irqdomain
) {
559 for (offset
= 0; offset
< (ggc
->line_max
+ 1); offset
++)
560 irq_dispose_mapping(irq_find_mapping(ggc
->irqdomain
, offset
));
561 irq_domain_remove(ggc
->irqdomain
);
569 * gb_gpio_irqchip_add() - adds an irqchip to a gpio chip
570 * @chip: the gpio chip to add the irqchip to
571 * @irqchip: the irqchip to add to the adapter
572 * @first_irq: if not dynamically assigned, the base (first) IRQ to
573 * allocate gpio irqs from
574 * @handler: the irq handler to use (often a predefined irq core function)
575 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
576 * to have the core avoid setting up any default type in the hardware.
578 * This function closely associates a certain irqchip with a certain
579 * gpio chip, providing an irq domain to translate the local IRQs to
580 * global irqs, and making sure that the gpio chip
581 * is passed as chip data to all related functions. Driver callbacks
582 * need to use container_of() to get their local state containers back
583 * from the gpio chip passed as chip data. An irqdomain will be stored
584 * in the gpio chip that shall be used by the driver to handle IRQ number
585 * translation. The gpio chip will need to be initialized and registered
586 * before calling this function.
588 static int gb_gpio_irqchip_add(struct gpio_chip
*chip
,
589 struct irq_chip
*irqchip
,
590 unsigned int first_irq
,
591 irq_flow_handler_t handler
,
594 struct gb_gpio_controller
*ggc
;
598 if (!chip
|| !irqchip
)
601 ggc
= gpio_chip_to_gb_gpio_controller(chip
);
603 ggc
->irqchip
= irqchip
;
604 ggc
->irq_handler
= handler
;
605 ggc
->irq_default_type
= type
;
606 ggc
->irqdomain
= irq_domain_add_simple(NULL
,
607 ggc
->line_max
+ 1, first_irq
,
608 &gb_gpio_domain_ops
, chip
);
609 if (!ggc
->irqdomain
) {
615 * Prepare the mapping since the irqchip shall be orthogonal to
616 * any gpio calls. If the first_irq was zero, this is
617 * necessary to allocate descriptors for all IRQs.
619 for (offset
= 0; offset
< (ggc
->line_max
+ 1); offset
++) {
620 irq_base
= irq_create_mapping(ggc
->irqdomain
, offset
);
622 ggc
->irq_base
= irq_base
;
628 static int gb_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
630 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
632 return irq_find_mapping(ggc
->irqdomain
, offset
);
635 static int gb_gpio_probe(struct gbphy_device
*gbphy_dev
,
636 const struct gbphy_device_id
*id
)
638 struct gb_connection
*connection
;
639 struct gb_gpio_controller
*ggc
;
640 struct gpio_chip
*gpio
;
641 struct irq_chip
*irqc
;
644 ggc
= kzalloc(sizeof(*ggc
), GFP_KERNEL
);
648 connection
= gb_connection_create(gbphy_dev
->bundle
,
649 le16_to_cpu(gbphy_dev
->cport_desc
->id
),
650 gb_gpio_request_handler
);
651 if (IS_ERR(connection
)) {
652 ret
= PTR_ERR(connection
);
656 ggc
->connection
= connection
;
657 gb_connection_set_data(connection
, ggc
);
658 ggc
->gbphy_dev
= gbphy_dev
;
659 gb_gbphy_set_data(gbphy_dev
, ggc
);
661 ret
= gb_connection_enable_tx(connection
);
663 goto exit_connection_destroy
;
665 ret
= gb_gpio_controller_setup(ggc
);
667 goto exit_connection_disable
;
670 irqc
->irq_mask
= gb_gpio_irq_mask
;
671 irqc
->irq_unmask
= gb_gpio_irq_unmask
;
672 irqc
->irq_set_type
= gb_gpio_irq_set_type
;
673 irqc
->irq_bus_lock
= gb_gpio_irq_bus_lock
;
674 irqc
->irq_bus_sync_unlock
= gb_gpio_irq_bus_sync_unlock
;
675 irqc
->name
= "greybus_gpio";
677 mutex_init(&ggc
->irq_lock
);
681 gpio
->label
= "greybus_gpio";
682 gpio
->parent
= &gbphy_dev
->dev
;
683 gpio
->owner
= THIS_MODULE
;
685 gpio
->request
= gb_gpio_request
;
686 gpio
->free
= gb_gpio_free
;
687 gpio
->get_direction
= gb_gpio_get_direction
;
688 gpio
->direction_input
= gb_gpio_direction_input
;
689 gpio
->direction_output
= gb_gpio_direction_output
;
690 gpio
->get
= gb_gpio_get
;
691 gpio
->set
= gb_gpio_set
;
692 gpio
->set_debounce
= gb_gpio_set_debounce
;
693 gpio
->to_irq
= gb_gpio_to_irq
;
694 gpio
->base
= -1; /* Allocate base dynamically */
695 gpio
->ngpio
= ggc
->line_max
+ 1;
696 gpio
->can_sleep
= true;
698 ret
= gb_connection_enable(connection
);
702 ret
= gb_gpio_irqchip_add(gpio
, irqc
, 0,
703 handle_level_irq
, IRQ_TYPE_NONE
);
705 dev_err(&gbphy_dev
->dev
, "failed to add irq chip: %d\n", ret
);
709 ret
= gpiochip_add(gpio
);
711 dev_err(&gbphy_dev
->dev
, "failed to add gpio chip: %d\n", ret
);
712 goto exit_gpio_irqchip_remove
;
715 gbphy_runtime_put_autosuspend(gbphy_dev
);
718 exit_gpio_irqchip_remove
:
719 gb_gpio_irqchip_remove(ggc
);
722 exit_connection_disable
:
723 gb_connection_disable(connection
);
724 exit_connection_destroy
:
725 gb_connection_destroy(connection
);
731 static void gb_gpio_remove(struct gbphy_device
*gbphy_dev
)
733 struct gb_gpio_controller
*ggc
= gb_gbphy_get_data(gbphy_dev
);
734 struct gb_connection
*connection
= ggc
->connection
;
737 ret
= gbphy_runtime_get_sync(gbphy_dev
);
739 gbphy_runtime_get_noresume(gbphy_dev
);
741 gb_connection_disable_rx(connection
);
742 gpiochip_remove(&ggc
->chip
);
743 gb_gpio_irqchip_remove(ggc
);
744 gb_connection_disable(connection
);
745 gb_connection_destroy(connection
);
750 static const struct gbphy_device_id gb_gpio_id_table
[] = {
751 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_GPIO
) },
754 MODULE_DEVICE_TABLE(gbphy
, gb_gpio_id_table
);
756 static struct gbphy_driver gpio_driver
= {
758 .probe
= gb_gpio_probe
,
759 .remove
= gb_gpio_remove
,
760 .id_table
= gb_gpio_id_table
,
763 module_gbphy_driver(gpio_driver
);
764 MODULE_LICENSE("GPL v2");