* [PATCH] gpio: support native single-ended hardware drivers
@ 2016-03-22 9:57 Linus Walleij
2016-03-23 10:10 ` Michael Hennerich
0 siblings, 1 reply; 2+ messages in thread
From: Linus Walleij @ 2016-03-22 9:57 UTC (permalink / raw)
To: linux-gpio, Alexandre Courbot, Michael Hennerich; +Cc: Linus Walleij
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>
---
Michael, please have a look at this and consider testing it with
your hardware and adding a .set_single_ended() callback in your
driver to see if it does what you want. I'm ready to merge this
if at least one driver make use of it.
---
drivers/gpio/gpiolib.c | 52 ++++++++++++++++++++++++++++++++-------------
include/linux/gpio/driver.h | 25 +++++++++++++++++++++-
2 files changed, 61 insertions(+), 16 deletions(-)
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 72065532c1c7..1edc830a1b51 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -1509,8 +1509,8 @@ EXPORT_SYMBOL_GPL(gpiod_direction_input);
static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
{
- struct gpio_chip *chip;
- int status = -EINVAL;
+ struct gpio_chip *gc = desc->gdev->chip;
+ int ret;
/* GPIOs used for IRQs shall not be set as output */
if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
@@ -1520,28 +1520,50 @@ static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
return -EIO;
}
- /* Open drain pin should not be driven to 1 */
- if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
- return gpiod_direction_input(desc);
-
- /* Open source pin should not be driven to 0 */
- if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
- return gpiod_direction_input(desc);
+ if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
+ /* First see if we can enable open drain in hardware */
+ if (gc->set_single_ended) {
+ ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
+ LINE_MODE_OPEN_DRAIN);
+ if (!ret)
+ goto set_output_value;
+ }
+ /* Emulate open drain by not actively driving the line high */
+ if (value)
+ return gpiod_direction_input(desc);
+ }
+ else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
+ if (gc->set_single_ended) {
+ ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
+ LINE_MODE_OPEN_SOURCE);
+ if (!ret)
+ goto set_output_value;
+ }
+ /* Emulate open source by not actively driving the line low */
+ if (!value)
+ return gpiod_direction_input(desc);
+ } else {
+ /* Make sure to disable open drain/source hardware, if any */
+ if (gc->set_single_ended)
+ gc->set_single_ended(gc,
+ gpio_chip_hwgpio(desc),
+ LINE_MODE_PUSH_PULL);
+ }
- chip = desc->gdev->chip;
- if (!chip->set || !chip->direction_output) {
+set_output_value:
+ if (!gc->set || !gc->direction_output) {
gpiod_warn(desc,
"%s: missing set() or direction_output() operations\n",
__func__);
return -EIO;
}
- status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
- if (status == 0)
+ ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), value);
+ if (!ret)
set_bit(FLAG_IS_OUT, &desc->flags);
trace_gpio_value(desc_to_gpio(desc), 0, value);
- trace_gpio_direction(desc_to_gpio(desc), 0, status);
- return status;
+ trace_gpio_direction(desc_to_gpio(desc), 0, ret);
+ return ret;
}
/**
diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h
index bee976f82788..50882e09289b 100644
--- a/include/linux/gpio/driver.h
+++ b/include/linux/gpio/driver.h
@@ -20,6 +20,18 @@ struct gpio_device;
#ifdef CONFIG_GPIOLIB
/**
+ * enum single_ended_mode - mode for single ended operation
+ * @LINE_MODE_PUSH_PULL: normal mode for a GPIO line, drive actively high/low
+ * @LINE_MODE_OPEN_DRAIN: set line to be open drain
+ * @LINE_MODE_OPEN_SOURCE: set line to be open source
+ */
+enum single_ended_mode {
+ LINE_MODE_PUSH_PULL,
+ LINE_MODE_OPEN_DRAIN,
+ LINE_MODE_OPEN_SOURCE,
+};
+
+/**
* struct gpio_chip - abstract a GPIO controller
* @label: a functional name for the GPIO device, such as a part
* number or the name of the SoC IP-block implementing it.
@@ -38,7 +50,15 @@ struct gpio_device;
* @set: assigns output value for signal "offset"
* @set_multiple: assigns output values for multiple signals defined by "mask"
* @set_debounce: optional hook for setting debounce time for specified gpio in
- * interrupt triggered gpio chips
+ * interrupt triggered gpio chips
+ * @set_single_ended: optional hook for setting a line as open drain, open
+ * source, or non-single ended (restore from open drain/source to normal
+ * push-pull mode) this should be implemented if the hardware supports
+ * open drain or open source settings. The GPIOlib will otherwise try
+ * to emulate open drain/source by not actively driving lines high/low
+ * if a consumer request this. The driver may return -ENOTSUPP if e.g.
+ * it supports just open drain but not open source and is called
+ * with LINE_MODE_OPEN_SOURCE as mode argument.
* @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
* implementation may not sleep
* @dbg_show: optional routine to show contents in debugfs; default code
@@ -130,6 +150,9 @@ struct gpio_chip {
int (*set_debounce)(struct gpio_chip *chip,
unsigned offset,
unsigned debounce);
+ int (*set_single_ended)(struct gpio_chip *chip,
+ unsigned offset,
+ enum single_ended_mode mode);
int (*to_irq)(struct gpio_chip *chip,
unsigned offset);
--
2.4.3
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] gpio: support native single-ended hardware drivers
2016-03-22 9:57 [PATCH] gpio: support native single-ended hardware drivers Linus Walleij
@ 2016-03-23 10:10 ` Michael Hennerich
0 siblings, 0 replies; 2+ messages in thread
From: Michael Hennerich @ 2016-03-23 10:10 UTC (permalink / raw)
To: Linus Walleij, linux-gpio, Alexandre Courbot
On 03/22/2016 10:57 AM, Linus Walleij wrote:
> 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>
> ---
> Michael, please have a look at this and consider testing it with
> your hardware and adding a .set_single_ended() callback in your
> driver to see if it does what you want. I'm ready to merge this
> if at least one driver make use of it.
Hi Linus,
I think the patch looks good.
Unfortunately it only applies against your gpio/for-next branch.
for some reason on that 4.5 kernel the SDHCI interface on my test board
doesn't work any more.
So I can't really fully test it at the moment.
diff --git a/drivers/iio/dac/ad5592r-base.c b/drivers/iio/dac/ad5592r-base.c
index 4019e44..efb7442 100644
--- a/drivers/iio/dac/ad5592r-base.c
+++ b/drivers/iio/dac/ad5592r-base.c
@@ -127,6 +127,38 @@ static int ad5592r_gpio_request(struct gpio_chip
*chip, unsigned offset)
return 0;
}
+static int ad5592r_gpio_set_single_ended(struct gpio_chip *chip,
+ unsigned offset,
+ enum single_ended_mode mode)
+{
+ struct ad5592r_state *st = gpiochip_get_data(chip);
+ int ret = 0;
+
+ mutex_lock(&st->gpio_lock);
+
+ switch (mode) {
+ case LINE_MODE_PUSH_PULL:
+ st->gpio_opendrain &= ~BIT(offset);
+ break;
+
+ case LINE_MODE_OPEN_DRAIN:
+ st->gpio_opendrain |= BIT(offset);
+ break;
+
+ case LINE_MODE_OPEN_SOURCE:
+ default:
+ ret = -ENOTSUPP;
+ }
+
+ mutex_unlock(&st->gpio_lock);
+
+ if (ret)
+ return ret;
+
+ return st->ops->reg_write(st, AD5592R_REG_OPEN_DRAIN,
+ st->gpio_opendrain);
+}
+
static int ad5592r_gpio_init(struct ad5592r_state *st)
{
if (!st->gpio_map)
@@ -139,6 +171,7 @@ static int ad5592r_gpio_init(struct ad5592r_state *st)
st->gpiochip.can_sleep = true;
st->gpiochip.direction_input = ad5592r_gpio_direction_input;
st->gpiochip.direction_output = ad5592r_gpio_direction_output;
+ st->gpiochip.set_single_ended = ad5592r_gpio_set_single_ended;
st->gpiochip.get = ad5592r_gpio_get;
st->gpiochip.set = ad5592r_gpio_set;
st->gpiochip.request = ad5592r_gpio_request;
diff --git a/drivers/iio/dac/ad5592r-base.h b/drivers/iio/dac/ad5592r-base.h
index 2753385..09d545b 100644
--- a/drivers/iio/dac/ad5592r-base.h
+++ b/drivers/iio/dac/ad5592r-base.h
@@ -66,6 +66,7 @@ struct ad5592r_state {
u8 gpio_out;
u8 gpio_in;
u8 gpio_val;
+ u8 gpio_opendrain;
__be16 spi_msg ____cacheline_aligned;
__be16 spi_msg_nop;
--
Greetings,
Michael
--
Analog Devices GmbH Wilhelm-Wagenfeld-Str. 6 80807 Muenchen
Sitz der Gesellschaft: Muenchen; Registergericht: Muenchen HRB 40368;
Geschaeftsfuehrer:Dr.Carsten Suckrow, Thomas Wessel, William A. Martin,
Margaret Seif
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2016-03-23 10:17 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-22 9:57 [PATCH] gpio: support native single-ended hardware drivers Linus Walleij
2016-03-23 10:10 ` Michael Hennerich
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.