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/irq.h>
13 #include <linux/irqdomain.h>
14 #include <linux/gpio/driver.h>
15 #include <linux/mutex.h>
16 #include <linux/greybus.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 mutex irq_lock
;
44 #define gpio_chip_to_gb_gpio_controller(chip) \
45 container_of(chip, struct gb_gpio_controller, chip)
46 #define irq_data_to_gpio_chip(d) (d->domain->host_data)
48 static int gb_gpio_line_count_operation(struct gb_gpio_controller
*ggc
)
50 struct gb_gpio_line_count_response response
;
53 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_LINE_COUNT
,
54 NULL
, 0, &response
, sizeof(response
));
56 ggc
->line_max
= response
.count
;
60 static int gb_gpio_activate_operation(struct gb_gpio_controller
*ggc
, u8 which
)
62 struct gb_gpio_activate_request request
;
63 struct gbphy_device
*gbphy_dev
= ggc
->gbphy_dev
;
66 ret
= gbphy_runtime_get_sync(gbphy_dev
);
70 request
.which
= which
;
71 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_ACTIVATE
,
72 &request
, sizeof(request
), NULL
, 0);
74 gbphy_runtime_put_autosuspend(gbphy_dev
);
78 ggc
->lines
[which
].active
= true;
83 static void gb_gpio_deactivate_operation(struct gb_gpio_controller
*ggc
,
86 struct gbphy_device
*gbphy_dev
= ggc
->gbphy_dev
;
87 struct device
*dev
= &gbphy_dev
->dev
;
88 struct gb_gpio_deactivate_request request
;
91 request
.which
= which
;
92 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_DEACTIVATE
,
93 &request
, sizeof(request
), NULL
, 0);
95 dev_err(dev
, "failed to deactivate gpio %u\n", which
);
99 ggc
->lines
[which
].active
= false;
102 gbphy_runtime_put_autosuspend(gbphy_dev
);
105 static int gb_gpio_get_direction_operation(struct gb_gpio_controller
*ggc
,
108 struct device
*dev
= &ggc
->gbphy_dev
->dev
;
109 struct gb_gpio_get_direction_request request
;
110 struct gb_gpio_get_direction_response response
;
114 request
.which
= which
;
115 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_GET_DIRECTION
,
116 &request
, sizeof(request
),
117 &response
, sizeof(response
));
121 direction
= response
.direction
;
122 if (direction
&& direction
!= 1) {
123 dev_warn(dev
, "gpio %u direction was %u (should be 0 or 1)\n",
126 ggc
->lines
[which
].direction
= direction
? 1 : 0;
130 static int gb_gpio_direction_in_operation(struct gb_gpio_controller
*ggc
,
133 struct gb_gpio_direction_in_request request
;
136 request
.which
= which
;
137 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_DIRECTION_IN
,
138 &request
, sizeof(request
), NULL
, 0);
140 ggc
->lines
[which
].direction
= 1;
144 static int gb_gpio_direction_out_operation(struct gb_gpio_controller
*ggc
,
145 u8 which
, bool value_high
)
147 struct gb_gpio_direction_out_request request
;
150 request
.which
= which
;
151 request
.value
= value_high
? 1 : 0;
152 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_DIRECTION_OUT
,
153 &request
, sizeof(request
), NULL
, 0);
155 ggc
->lines
[which
].direction
= 0;
159 static int gb_gpio_get_value_operation(struct gb_gpio_controller
*ggc
,
162 struct device
*dev
= &ggc
->gbphy_dev
->dev
;
163 struct gb_gpio_get_value_request request
;
164 struct gb_gpio_get_value_response response
;
168 request
.which
= which
;
169 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_GET_VALUE
,
170 &request
, sizeof(request
),
171 &response
, sizeof(response
));
173 dev_err(dev
, "failed to get value of gpio %u\n", which
);
177 value
= response
.value
;
178 if (value
&& value
!= 1) {
179 dev_warn(dev
, "gpio %u value was %u (should be 0 or 1)\n",
182 ggc
->lines
[which
].value
= value
? 1 : 0;
186 static void gb_gpio_set_value_operation(struct gb_gpio_controller
*ggc
,
187 u8 which
, bool value_high
)
189 struct device
*dev
= &ggc
->gbphy_dev
->dev
;
190 struct gb_gpio_set_value_request request
;
193 if (ggc
->lines
[which
].direction
== 1) {
194 dev_warn(dev
, "refusing to set value of input gpio %u\n",
199 request
.which
= which
;
200 request
.value
= value_high
? 1 : 0;
201 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_SET_VALUE
,
202 &request
, sizeof(request
), NULL
, 0);
204 dev_err(dev
, "failed to set value of gpio %u\n", which
);
208 ggc
->lines
[which
].value
= request
.value
;
211 static int gb_gpio_set_debounce_operation(struct gb_gpio_controller
*ggc
,
212 u8 which
, u16 debounce_usec
)
214 struct gb_gpio_set_debounce_request request
;
217 request
.which
= which
;
218 request
.usec
= cpu_to_le16(debounce_usec
);
219 ret
= gb_operation_sync(ggc
->connection
, GB_GPIO_TYPE_SET_DEBOUNCE
,
220 &request
, sizeof(request
), NULL
, 0);
222 ggc
->lines
[which
].debounce_usec
= debounce_usec
;
226 static void _gb_gpio_irq_mask(struct gb_gpio_controller
*ggc
, u8 hwirq
)
228 struct device
*dev
= &ggc
->gbphy_dev
->dev
;
229 struct gb_gpio_irq_mask_request request
;
232 request
.which
= hwirq
;
233 ret
= gb_operation_sync(ggc
->connection
,
234 GB_GPIO_TYPE_IRQ_MASK
,
235 &request
, sizeof(request
), NULL
, 0);
237 dev_err(dev
, "failed to mask irq: %d\n", ret
);
240 static void _gb_gpio_irq_unmask(struct gb_gpio_controller
*ggc
, u8 hwirq
)
242 struct device
*dev
= &ggc
->gbphy_dev
->dev
;
243 struct gb_gpio_irq_unmask_request request
;
246 request
.which
= hwirq
;
247 ret
= gb_operation_sync(ggc
->connection
,
248 GB_GPIO_TYPE_IRQ_UNMASK
,
249 &request
, sizeof(request
), NULL
, 0);
251 dev_err(dev
, "failed to unmask irq: %d\n", ret
);
254 static void _gb_gpio_irq_set_type(struct gb_gpio_controller
*ggc
,
257 struct device
*dev
= &ggc
->gbphy_dev
->dev
;
258 struct gb_gpio_irq_type_request request
;
261 request
.which
= hwirq
;
264 ret
= gb_operation_sync(ggc
->connection
,
265 GB_GPIO_TYPE_IRQ_TYPE
,
266 &request
, sizeof(request
), NULL
, 0);
268 dev_err(dev
, "failed to set irq type: %d\n", ret
);
271 static void gb_gpio_irq_mask(struct irq_data
*d
)
273 struct gpio_chip
*chip
= irq_data_to_gpio_chip(d
);
274 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
275 struct gb_gpio_line
*line
= &ggc
->lines
[d
->hwirq
];
278 line
->masked_pending
= true;
281 static void gb_gpio_irq_unmask(struct irq_data
*d
)
283 struct gpio_chip
*chip
= irq_data_to_gpio_chip(d
);
284 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
285 struct gb_gpio_line
*line
= &ggc
->lines
[d
->hwirq
];
287 line
->masked
= false;
288 line
->masked_pending
= true;
291 static int gb_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
293 struct gpio_chip
*chip
= irq_data_to_gpio_chip(d
);
294 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
295 struct gb_gpio_line
*line
= &ggc
->lines
[d
->hwirq
];
296 struct device
*dev
= &ggc
->gbphy_dev
->dev
;
301 irq_type
= GB_GPIO_IRQ_TYPE_NONE
;
303 case IRQ_TYPE_EDGE_RISING
:
304 irq_type
= GB_GPIO_IRQ_TYPE_EDGE_RISING
;
306 case IRQ_TYPE_EDGE_FALLING
:
307 irq_type
= GB_GPIO_IRQ_TYPE_EDGE_FALLING
;
309 case IRQ_TYPE_EDGE_BOTH
:
310 irq_type
= GB_GPIO_IRQ_TYPE_EDGE_BOTH
;
312 case IRQ_TYPE_LEVEL_LOW
:
313 irq_type
= GB_GPIO_IRQ_TYPE_LEVEL_LOW
;
315 case IRQ_TYPE_LEVEL_HIGH
:
316 irq_type
= GB_GPIO_IRQ_TYPE_LEVEL_HIGH
;
319 dev_err(dev
, "unsupported irq type: %u\n", type
);
323 line
->irq_type
= irq_type
;
324 line
->irq_type_pending
= true;
329 static void gb_gpio_irq_bus_lock(struct irq_data
*d
)
331 struct gpio_chip
*chip
= irq_data_to_gpio_chip(d
);
332 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
334 mutex_lock(&ggc
->irq_lock
);
337 static void gb_gpio_irq_bus_sync_unlock(struct irq_data
*d
)
339 struct gpio_chip
*chip
= irq_data_to_gpio_chip(d
);
340 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
341 struct gb_gpio_line
*line
= &ggc
->lines
[d
->hwirq
];
343 if (line
->irq_type_pending
) {
344 _gb_gpio_irq_set_type(ggc
, d
->hwirq
, line
->irq_type
);
345 line
->irq_type_pending
= false;
348 if (line
->masked_pending
) {
350 _gb_gpio_irq_mask(ggc
, d
->hwirq
);
352 _gb_gpio_irq_unmask(ggc
, d
->hwirq
);
353 line
->masked_pending
= false;
356 mutex_unlock(&ggc
->irq_lock
);
359 static int gb_gpio_request_handler(struct gb_operation
*op
)
361 struct gb_connection
*connection
= op
->connection
;
362 struct gb_gpio_controller
*ggc
= gb_connection_get_data(connection
);
363 struct device
*dev
= &ggc
->gbphy_dev
->dev
;
364 struct gb_message
*request
;
365 struct gb_gpio_irq_event_request
*event
;
369 if (type
!= GB_GPIO_TYPE_IRQ_EVENT
) {
370 dev_err(dev
, "unsupported unsolicited request: %u\n", type
);
374 request
= op
->request
;
376 if (request
->payload_size
< sizeof(*event
)) {
377 dev_err(dev
, "short event received (%zu < %zu)\n",
378 request
->payload_size
, sizeof(*event
));
382 event
= request
->payload
;
383 if (event
->which
> ggc
->line_max
) {
384 dev_err(dev
, "invalid hw irq: %d\n", event
->which
);
388 irq
= irq_find_mapping(ggc
->chip
.irq
.domain
, event
->which
);
390 dev_err(dev
, "failed to find IRQ\n");
395 ret
= generic_handle_irq(irq
);
399 dev_err(dev
, "failed to invoke irq handler\n");
404 static int gb_gpio_request(struct gpio_chip
*chip
, unsigned int offset
)
406 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
408 return gb_gpio_activate_operation(ggc
, (u8
)offset
);
411 static void gb_gpio_free(struct gpio_chip
*chip
, unsigned int offset
)
413 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
415 gb_gpio_deactivate_operation(ggc
, (u8
)offset
);
418 static int gb_gpio_get_direction(struct gpio_chip
*chip
, unsigned int offset
)
420 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
425 ret
= gb_gpio_get_direction_operation(ggc
, which
);
429 return ggc
->lines
[which
].direction
? 1 : 0;
432 static int gb_gpio_direction_input(struct gpio_chip
*chip
, unsigned int offset
)
434 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
436 return gb_gpio_direction_in_operation(ggc
, (u8
)offset
);
439 static int gb_gpio_direction_output(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_out_operation(ggc
, (u8
)offset
, !!value
);
447 static int gb_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
449 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
454 ret
= gb_gpio_get_value_operation(ggc
, which
);
458 return ggc
->lines
[which
].value
;
461 static void gb_gpio_set(struct gpio_chip
*chip
, unsigned int offset
, int value
)
463 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
465 gb_gpio_set_value_operation(ggc
, (u8
)offset
, !!value
);
468 static int gb_gpio_set_config(struct gpio_chip
*chip
, unsigned int offset
,
469 unsigned long config
)
471 struct gb_gpio_controller
*ggc
= gpio_chip_to_gb_gpio_controller(chip
);
474 if (pinconf_to_config_param(config
) != PIN_CONFIG_INPUT_DEBOUNCE
)
477 debounce
= pinconf_to_config_argument(config
);
478 if (debounce
> U16_MAX
)
481 return gb_gpio_set_debounce_operation(ggc
, (u8
)offset
, (u16
)debounce
);
484 static int gb_gpio_controller_setup(struct gb_gpio_controller
*ggc
)
488 /* Now find out how many lines there are */
489 ret
= gb_gpio_line_count_operation(ggc
);
493 ggc
->lines
= kcalloc(ggc
->line_max
+ 1, sizeof(*ggc
->lines
),
501 static int gb_gpio_probe(struct gbphy_device
*gbphy_dev
,
502 const struct gbphy_device_id
*id
)
504 struct gb_connection
*connection
;
505 struct gb_gpio_controller
*ggc
;
506 struct gpio_chip
*gpio
;
507 struct irq_chip
*irqc
;
510 ggc
= kzalloc(sizeof(*ggc
), GFP_KERNEL
);
515 gb_connection_create(gbphy_dev
->bundle
,
516 le16_to_cpu(gbphy_dev
->cport_desc
->id
),
517 gb_gpio_request_handler
);
518 if (IS_ERR(connection
)) {
519 ret
= PTR_ERR(connection
);
523 ggc
->connection
= connection
;
524 gb_connection_set_data(connection
, ggc
);
525 ggc
->gbphy_dev
= gbphy_dev
;
526 gb_gbphy_set_data(gbphy_dev
, ggc
);
528 ret
= gb_connection_enable_tx(connection
);
530 goto exit_connection_destroy
;
532 ret
= gb_gpio_controller_setup(ggc
);
534 goto exit_connection_disable
;
537 irqc
->irq_mask
= gb_gpio_irq_mask
;
538 irqc
->irq_unmask
= gb_gpio_irq_unmask
;
539 irqc
->irq_set_type
= gb_gpio_irq_set_type
;
540 irqc
->irq_bus_lock
= gb_gpio_irq_bus_lock
;
541 irqc
->irq_bus_sync_unlock
= gb_gpio_irq_bus_sync_unlock
;
542 irqc
->name
= "greybus_gpio";
544 mutex_init(&ggc
->irq_lock
);
548 gpio
->label
= "greybus_gpio";
549 gpio
->parent
= &gbphy_dev
->dev
;
550 gpio
->owner
= THIS_MODULE
;
552 gpio
->request
= gb_gpio_request
;
553 gpio
->free
= gb_gpio_free
;
554 gpio
->get_direction
= gb_gpio_get_direction
;
555 gpio
->direction_input
= gb_gpio_direction_input
;
556 gpio
->direction_output
= gb_gpio_direction_output
;
557 gpio
->get
= gb_gpio_get
;
558 gpio
->set
= gb_gpio_set
;
559 gpio
->set_config
= gb_gpio_set_config
;
560 gpio
->base
= -1; /* Allocate base dynamically */
561 gpio
->ngpio
= ggc
->line_max
+ 1;
562 gpio
->can_sleep
= true;
564 ret
= gb_connection_enable(connection
);
568 ret
= gpiochip_add(gpio
);
570 dev_err(&gbphy_dev
->dev
, "failed to add gpio chip: %d\n", ret
);
574 ret
= gpiochip_irqchip_add(gpio
, irqc
, 0, handle_level_irq
,
577 dev_err(&gbphy_dev
->dev
, "failed to add irq chip: %d\n", ret
);
578 goto exit_gpiochip_remove
;
581 gbphy_runtime_put_autosuspend(gbphy_dev
);
584 exit_gpiochip_remove
:
585 gpiochip_remove(gpio
);
588 exit_connection_disable
:
589 gb_connection_disable(connection
);
590 exit_connection_destroy
:
591 gb_connection_destroy(connection
);
597 static void gb_gpio_remove(struct gbphy_device
*gbphy_dev
)
599 struct gb_gpio_controller
*ggc
= gb_gbphy_get_data(gbphy_dev
);
600 struct gb_connection
*connection
= ggc
->connection
;
603 ret
= gbphy_runtime_get_sync(gbphy_dev
);
605 gbphy_runtime_get_noresume(gbphy_dev
);
607 gb_connection_disable_rx(connection
);
608 gpiochip_remove(&ggc
->chip
);
609 gb_connection_disable(connection
);
610 gb_connection_destroy(connection
);
615 static const struct gbphy_device_id gb_gpio_id_table
[] = {
616 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_GPIO
) },
619 MODULE_DEVICE_TABLE(gbphy
, gb_gpio_id_table
);
621 static struct gbphy_driver gpio_driver
= {
623 .probe
= gb_gpio_probe
,
624 .remove
= gb_gpio_remove
,
625 .id_table
= gb_gpio_id_table
,
628 module_gbphy_driver(gpio_driver
);
629 MODULE_LICENSE("GPL v2");