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;
45 static struct gpio_chip *irq_data_to_gpio_chip(struct irq_data *d)
47 return d->domain->host_data;
50 static int gb_gpio_line_count_operation(struct gb_gpio_controller *ggc)
52 struct gb_gpio_line_count_response response;
55 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_LINE_COUNT,
56 NULL, 0, &response, sizeof(response));
58 ggc->line_max = response.count;
62 static int gb_gpio_activate_operation(struct gb_gpio_controller *ggc, u8 which)
64 struct gb_gpio_activate_request request;
65 struct gbphy_device *gbphy_dev = ggc->gbphy_dev;
68 ret = gbphy_runtime_get_sync(gbphy_dev);
72 request.which = which;
73 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_ACTIVATE,
74 &request, sizeof(request), NULL, 0);
76 gbphy_runtime_put_autosuspend(gbphy_dev);
80 ggc->lines[which].active = true;
85 static void gb_gpio_deactivate_operation(struct gb_gpio_controller *ggc,
88 struct gbphy_device *gbphy_dev = ggc->gbphy_dev;
89 struct device *dev = &gbphy_dev->dev;
90 struct gb_gpio_deactivate_request request;
93 request.which = which;
94 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DEACTIVATE,
95 &request, sizeof(request), NULL, 0);
97 dev_err(dev, "failed to deactivate gpio %u\n", which);
101 ggc->lines[which].active = false;
104 gbphy_runtime_put_autosuspend(gbphy_dev);
107 static int gb_gpio_get_direction_operation(struct gb_gpio_controller *ggc,
110 struct device *dev = &ggc->gbphy_dev->dev;
111 struct gb_gpio_get_direction_request request;
112 struct gb_gpio_get_direction_response response;
116 request.which = which;
117 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_DIRECTION,
118 &request, sizeof(request),
119 &response, sizeof(response));
123 direction = response.direction;
124 if (direction && direction != 1) {
125 dev_warn(dev, "gpio %u direction was %u (should be 0 or 1)\n",
128 ggc->lines[which].direction = direction ? 1 : 0;
132 static int gb_gpio_direction_in_operation(struct gb_gpio_controller *ggc,
135 struct gb_gpio_direction_in_request request;
138 request.which = which;
139 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_IN,
140 &request, sizeof(request), NULL, 0);
142 ggc->lines[which].direction = 1;
146 static int gb_gpio_direction_out_operation(struct gb_gpio_controller *ggc,
147 u8 which, bool value_high)
149 struct gb_gpio_direction_out_request request;
152 request.which = which;
153 request.value = value_high ? 1 : 0;
154 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_OUT,
155 &request, sizeof(request), NULL, 0);
157 ggc->lines[which].direction = 0;
161 static int gb_gpio_get_value_operation(struct gb_gpio_controller *ggc,
164 struct device *dev = &ggc->gbphy_dev->dev;
165 struct gb_gpio_get_value_request request;
166 struct gb_gpio_get_value_response response;
170 request.which = which;
171 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_VALUE,
172 &request, sizeof(request),
173 &response, sizeof(response));
175 dev_err(dev, "failed to get value of gpio %u\n", which);
179 value = response.value;
180 if (value && value != 1) {
181 dev_warn(dev, "gpio %u value was %u (should be 0 or 1)\n",
184 ggc->lines[which].value = value ? 1 : 0;
188 static void gb_gpio_set_value_operation(struct gb_gpio_controller *ggc,
189 u8 which, bool value_high)
191 struct device *dev = &ggc->gbphy_dev->dev;
192 struct gb_gpio_set_value_request request;
195 if (ggc->lines[which].direction == 1) {
196 dev_warn(dev, "refusing to set value of input gpio %u\n",
201 request.which = which;
202 request.value = value_high ? 1 : 0;
203 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_VALUE,
204 &request, sizeof(request), NULL, 0);
206 dev_err(dev, "failed to set value of gpio %u\n", which);
210 ggc->lines[which].value = request.value;
213 static int gb_gpio_set_debounce_operation(struct gb_gpio_controller *ggc,
214 u8 which, u16 debounce_usec)
216 struct gb_gpio_set_debounce_request request;
219 request.which = which;
220 request.usec = cpu_to_le16(debounce_usec);
221 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_DEBOUNCE,
222 &request, sizeof(request), NULL, 0);
224 ggc->lines[which].debounce_usec = debounce_usec;
228 static void _gb_gpio_irq_mask(struct gb_gpio_controller *ggc, u8 hwirq)
230 struct device *dev = &ggc->gbphy_dev->dev;
231 struct gb_gpio_irq_mask_request request;
234 request.which = hwirq;
235 ret = gb_operation_sync(ggc->connection,
236 GB_GPIO_TYPE_IRQ_MASK,
237 &request, sizeof(request), NULL, 0);
239 dev_err(dev, "failed to mask irq: %d\n", ret);
242 static void _gb_gpio_irq_unmask(struct gb_gpio_controller *ggc, u8 hwirq)
244 struct device *dev = &ggc->gbphy_dev->dev;
245 struct gb_gpio_irq_unmask_request request;
248 request.which = hwirq;
249 ret = gb_operation_sync(ggc->connection,
250 GB_GPIO_TYPE_IRQ_UNMASK,
251 &request, sizeof(request), NULL, 0);
253 dev_err(dev, "failed to unmask irq: %d\n", ret);
256 static void _gb_gpio_irq_set_type(struct gb_gpio_controller *ggc,
259 struct device *dev = &ggc->gbphy_dev->dev;
260 struct gb_gpio_irq_type_request request;
263 request.which = hwirq;
266 ret = gb_operation_sync(ggc->connection,
267 GB_GPIO_TYPE_IRQ_TYPE,
268 &request, sizeof(request), NULL, 0);
270 dev_err(dev, "failed to set irq type: %d\n", ret);
273 static void gb_gpio_irq_mask(struct irq_data *d)
275 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
276 struct gb_gpio_controller *ggc = gpiochip_get_data(chip);
277 struct gb_gpio_line *line = &ggc->lines[d->hwirq];
280 line->masked_pending = true;
283 static void gb_gpio_irq_unmask(struct irq_data *d)
285 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
286 struct gb_gpio_controller *ggc = gpiochip_get_data(chip);
287 struct gb_gpio_line *line = &ggc->lines[d->hwirq];
289 line->masked = false;
290 line->masked_pending = true;
293 static int gb_gpio_irq_set_type(struct irq_data *d, unsigned int type)
295 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
296 struct gb_gpio_controller *ggc = gpiochip_get_data(chip);
297 struct gb_gpio_line *line = &ggc->lines[d->hwirq];
298 struct device *dev = &ggc->gbphy_dev->dev;
303 irq_type = GB_GPIO_IRQ_TYPE_NONE;
305 case IRQ_TYPE_EDGE_RISING:
306 irq_type = GB_GPIO_IRQ_TYPE_EDGE_RISING;
308 case IRQ_TYPE_EDGE_FALLING:
309 irq_type = GB_GPIO_IRQ_TYPE_EDGE_FALLING;
311 case IRQ_TYPE_EDGE_BOTH:
312 irq_type = GB_GPIO_IRQ_TYPE_EDGE_BOTH;
314 case IRQ_TYPE_LEVEL_LOW:
315 irq_type = GB_GPIO_IRQ_TYPE_LEVEL_LOW;
317 case IRQ_TYPE_LEVEL_HIGH:
318 irq_type = GB_GPIO_IRQ_TYPE_LEVEL_HIGH;
321 dev_err(dev, "unsupported irq type: %u\n", type);
325 line->irq_type = irq_type;
326 line->irq_type_pending = true;
331 static void gb_gpio_irq_bus_lock(struct irq_data *d)
333 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
334 struct gb_gpio_controller *ggc = gpiochip_get_data(chip);
336 mutex_lock(&ggc->irq_lock);
339 static void gb_gpio_irq_bus_sync_unlock(struct irq_data *d)
341 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
342 struct gb_gpio_controller *ggc = gpiochip_get_data(chip);
343 struct gb_gpio_line *line = &ggc->lines[d->hwirq];
345 if (line->irq_type_pending) {
346 _gb_gpio_irq_set_type(ggc, d->hwirq, line->irq_type);
347 line->irq_type_pending = false;
350 if (line->masked_pending) {
352 _gb_gpio_irq_mask(ggc, d->hwirq);
354 _gb_gpio_irq_unmask(ggc, d->hwirq);
355 line->masked_pending = false;
358 mutex_unlock(&ggc->irq_lock);
361 static int gb_gpio_request_handler(struct gb_operation *op)
363 struct gb_connection *connection = op->connection;
364 struct gb_gpio_controller *ggc = gb_connection_get_data(connection);
365 struct device *dev = &ggc->gbphy_dev->dev;
366 struct gb_message *request;
367 struct gb_gpio_irq_event_request *event;
371 if (type != GB_GPIO_TYPE_IRQ_EVENT) {
372 dev_err(dev, "unsupported unsolicited request: %u\n", type);
376 request = op->request;
378 if (request->payload_size < sizeof(*event)) {
379 dev_err(dev, "short event received (%zu < %zu)\n",
380 request->payload_size, sizeof(*event));
384 event = request->payload;
385 if (event->which > ggc->line_max) {
386 dev_err(dev, "invalid hw irq: %d\n", event->which);
390 irq = irq_find_mapping(ggc->chip.irq.domain, event->which);
392 dev_err(dev, "failed to find IRQ\n");
396 ret = generic_handle_irq_safe(irq);
398 dev_err(dev, "failed to invoke irq handler\n");
403 static int gb_gpio_request(struct gpio_chip *chip, unsigned int offset)
405 struct gb_gpio_controller *ggc = gpiochip_get_data(chip);
407 return gb_gpio_activate_operation(ggc, (u8)offset);
410 static void gb_gpio_free(struct gpio_chip *chip, unsigned int offset)
412 struct gb_gpio_controller *ggc = gpiochip_get_data(chip);
414 gb_gpio_deactivate_operation(ggc, (u8)offset);
417 static int gb_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
419 struct gb_gpio_controller *ggc = gpiochip_get_data(chip);
424 ret = gb_gpio_get_direction_operation(ggc, which);
428 return ggc->lines[which].direction ? 1 : 0;
431 static int gb_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
433 struct gb_gpio_controller *ggc = gpiochip_get_data(chip);
435 return gb_gpio_direction_in_operation(ggc, (u8)offset);
438 static int gb_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
441 struct gb_gpio_controller *ggc = gpiochip_get_data(chip);
443 return gb_gpio_direction_out_operation(ggc, (u8)offset, !!value);
446 static int gb_gpio_get(struct gpio_chip *chip, unsigned int offset)
448 struct gb_gpio_controller *ggc = gpiochip_get_data(chip);
453 ret = gb_gpio_get_value_operation(ggc, which);
457 return ggc->lines[which].value;
460 static void gb_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
462 struct gb_gpio_controller *ggc = gpiochip_get_data(chip);
464 gb_gpio_set_value_operation(ggc, (u8)offset, !!value);
467 static int gb_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
468 unsigned long config)
470 struct gb_gpio_controller *ggc = gpiochip_get_data(chip);
473 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
476 debounce = pinconf_to_config_argument(config);
477 if (debounce > U16_MAX)
480 return gb_gpio_set_debounce_operation(ggc, (u8)offset, (u16)debounce);
483 static int gb_gpio_controller_setup(struct gb_gpio_controller *ggc)
487 /* Now find out how many lines there are */
488 ret = gb_gpio_line_count_operation(ggc);
492 ggc->lines = kcalloc(ggc->line_max + 1, sizeof(*ggc->lines),
500 static int gb_gpio_probe(struct gbphy_device *gbphy_dev,
501 const struct gbphy_device_id *id)
503 struct gb_connection *connection;
504 struct gb_gpio_controller *ggc;
505 struct gpio_chip *gpio;
506 struct gpio_irq_chip *girq;
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;
566 /* The event comes from the outside so no parent handler */
567 girq->parent_handler = NULL;
568 girq->num_parents = 0;
569 girq->parents = NULL;
570 girq->default_type = IRQ_TYPE_NONE;
571 girq->handler = handle_level_irq;
573 ret = gb_connection_enable(connection);
577 ret = gpiochip_add_data(gpio, ggc);
579 dev_err(&gbphy_dev->dev, "failed to add gpio chip: %d\n", ret);
583 gbphy_runtime_put_autosuspend(gbphy_dev);
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_DESCRIPTION("GPIO Greybus driver");
630 MODULE_LICENSE("GPL v2");