Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gpio: support native single-ended hardware drivers

Some GPIO controllers has a special hardware bit we can flip
to support open drain / source. This means that on these hardwares
we do not need to emulate OD/OS by setting the line to input
instead of actively driving it high/low. Add an optional vtable
callback to the driver set_single_ended() so that driver can
implement this in hardware if they have it.

We may need a pinctrl_gpio_set_config() call at some point to
propagate this down to a backing pin control device on systems
with split GPIO/pin control.

Reported-by: Michael Hennerich <michael.hennerich@analog.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

+61 -16
+37 -15
drivers/gpio/gpiolib.c
··· 1509 1509 1510 1510 static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value) 1511 1511 { 1512 - struct gpio_chip *chip; 1513 - int status = -EINVAL; 1512 + struct gpio_chip *gc = desc->gdev->chip; 1513 + int ret; 1514 1514 1515 1515 /* GPIOs used for IRQs shall not be set as output */ 1516 1516 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { ··· 1520 1520 return -EIO; 1521 1521 } 1522 1522 1523 - /* Open drain pin should not be driven to 1 */ 1524 - if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 1525 - return gpiod_direction_input(desc); 1523 + if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { 1524 + /* First see if we can enable open drain in hardware */ 1525 + if (gc->set_single_ended) { 1526 + ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc), 1527 + LINE_MODE_OPEN_DRAIN); 1528 + if (!ret) 1529 + goto set_output_value; 1530 + } 1531 + /* Emulate open drain by not actively driving the line high */ 1532 + if (value) 1533 + return gpiod_direction_input(desc); 1534 + } 1535 + else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { 1536 + if (gc->set_single_ended) { 1537 + ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc), 1538 + LINE_MODE_OPEN_SOURCE); 1539 + if (!ret) 1540 + goto set_output_value; 1541 + } 1542 + /* Emulate open source by not actively driving the line low */ 1543 + if (!value) 1544 + return gpiod_direction_input(desc); 1545 + } else { 1546 + /* Make sure to disable open drain/source hardware, if any */ 1547 + if (gc->set_single_ended) 1548 + gc->set_single_ended(gc, 1549 + gpio_chip_hwgpio(desc), 1550 + LINE_MODE_PUSH_PULL); 1551 + } 1526 1552 1527 - /* Open source pin should not be driven to 0 */ 1528 - if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 1529 - return gpiod_direction_input(desc); 1530 - 1531 - chip = desc->gdev->chip; 1532 - if (!chip->set || !chip->direction_output) { 1553 + set_output_value: 1554 + if (!gc->set || !gc->direction_output) { 1533 1555 gpiod_warn(desc, 1534 1556 "%s: missing set() or direction_output() operations\n", 1535 1557 __func__); 1536 1558 return -EIO; 1537 1559 } 1538 1560 1539 - status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value); 1540 - if (status == 0) 1561 + ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), value); 1562 + if (!ret) 1541 1563 set_bit(FLAG_IS_OUT, &desc->flags); 1542 1564 trace_gpio_value(desc_to_gpio(desc), 0, value); 1543 - trace_gpio_direction(desc_to_gpio(desc), 0, status); 1544 - return status; 1565 + trace_gpio_direction(desc_to_gpio(desc), 0, ret); 1566 + return ret; 1545 1567 } 1546 1568 1547 1569 /**
+24 -1
include/linux/gpio/driver.h
··· 20 20 #ifdef CONFIG_GPIOLIB 21 21 22 22 /** 23 + * enum single_ended_mode - mode for single ended operation 24 + * @LINE_MODE_PUSH_PULL: normal mode for a GPIO line, drive actively high/low 25 + * @LINE_MODE_OPEN_DRAIN: set line to be open drain 26 + * @LINE_MODE_OPEN_SOURCE: set line to be open source 27 + */ 28 + enum single_ended_mode { 29 + LINE_MODE_PUSH_PULL, 30 + LINE_MODE_OPEN_DRAIN, 31 + LINE_MODE_OPEN_SOURCE, 32 + }; 33 + 34 + /** 23 35 * struct gpio_chip - abstract a GPIO controller 24 36 * @label: a functional name for the GPIO device, such as a part 25 37 * number or the name of the SoC IP-block implementing it. ··· 50 38 * @set: assigns output value for signal "offset" 51 39 * @set_multiple: assigns output values for multiple signals defined by "mask" 52 40 * @set_debounce: optional hook for setting debounce time for specified gpio in 53 - * interrupt triggered gpio chips 41 + * interrupt triggered gpio chips 42 + * @set_single_ended: optional hook for setting a line as open drain, open 43 + * source, or non-single ended (restore from open drain/source to normal 44 + * push-pull mode) this should be implemented if the hardware supports 45 + * open drain or open source settings. The GPIOlib will otherwise try 46 + * to emulate open drain/source by not actively driving lines high/low 47 + * if a consumer request this. The driver may return -ENOTSUPP if e.g. 48 + * it supports just open drain but not open source and is called 49 + * with LINE_MODE_OPEN_SOURCE as mode argument. 54 50 * @to_irq: optional hook supporting non-static gpio_to_irq() mappings; 55 51 * implementation may not sleep 56 52 * @dbg_show: optional routine to show contents in debugfs; default code ··· 150 130 int (*set_debounce)(struct gpio_chip *chip, 151 131 unsigned offset, 152 132 unsigned debounce); 133 + int (*set_single_ended)(struct gpio_chip *chip, 134 + unsigned offset, 135 + enum single_ended_mode mode); 153 136 154 137 int (*to_irq)(struct gpio_chip *chip, 155 138 unsigned offset);