Skip to contentSkip to conversation

gpio:pca953x: add mechanism to simulate open drain outputs

PATCH In review H. Nikolaus Schaller 3 participants 5 messages Sep 23, 2015 – Oct 2, 2015 2 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#1 lore raw

PATCH 1/1 gpio:pca953x: add mechanism to simulate open drain outputs

In reply to a message not in the archive

1. add a DT property to specify the list of GPIO pins that are to become "open drain"
2. if a pin is configured as "open drain", set value by either outputting a "0" (low level) or switching to input (high impedance)

Tested on OMAP5 uEVM with TCA6424 and some LEDs connected to 5V.

---
.../devicetree/bindings/gpio/gpio-pca953x.txt | 6 +++++
drivers/gpio/gpio-pca953x.c | 30 ++++++++++++++++++++++

2 files changed, 36 insertions(+)
Documentation/devicetree/bindings/gpio/gpio-pca953x.txt +0 -0
index b9a42f2..699b895 100644
@@ -26,6 +26,11 @@ Required properties:
	ti,tca6424
	exar,xra1202

+Optional property:
+ - open-drain-pins: list of pin numbers (starting at 0)
+ which will be simulating open-drain outputs
+ (by swithing between input and output(0)).
+
Example:

@@ -36,4 +41,5 @@ Example:

		pinctrl-0 = <&pinctrl_pca9505>;
		interrupt-parent = <&gpio3>;
		interrupts = <23 IRQ_TYPE_LEVEL_LOW>;
+		open-drain-pins = <1 3 4>;
	};
drivers/gpio/gpio-pca953x.c +0 -0
index 50caeb1..34fb9d3 100644
@@ -81,6 +81,7 @@ struct pca953x_chip {
	unsigned gpio_start;
	u8 reg_output[MAX_BANK];
	u8 reg_direction[MAX_BANK];
+	u8 reg_open_drain[MAX_BANK];
	struct mutex i2c_lock;

#ifdef CONFIG_GPIO_PCA953X_IRQ
@@ -237,6 +238,11 @@ static int pca953x_gpio_direction_output(struct gpio_chip *gc,

	u8 reg_val;
	int ret, offset = 0;

+ if (val && (chip->reg_open_drain[off / BANK_SZ] &
+ (1u << (off % BANK_SZ))))
+ /* make high impedance */
+ return pca953x_gpio_direction_input(gc, off);
+

	mutex_lock(&chip->i2c_lock);
	/* set output level */
	if (val)
@@ -315,6 +321,12 @@ static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
	u8 reg_val;
	int ret, offset = 0;

+ if ((chip->reg_open_drain[off / BANK_SZ] & (1u << (off % BANK_SZ)))) {
+ /* switch between input (1=High-Z) and output (0=Low) */
+ pca953x_gpio_direction_output(gc, off, val);
+ return;
+ }
+

	mutex_lock(&chip->i2c_lock);
	if (val)
		reg_val = chip->reg_output[off / BANK_SZ]
@@ -660,6 +672,8 @@ static int pca953x_probe(struct i2c_client *client,
	if (chip == NULL)
		return -ENOMEM;

+ memset(chip->reg_open_drain, 0, NBANK(chip));
+

	pdata = dev_get_platdata(&client->dev);
	if (pdata) {
		irq_base = pdata->irq_base;
@@ -667,8 +681,24 @@ static int pca953x_probe(struct i2c_client *client,
		invert = pdata->invert;
		chip->names = pdata->names;
	} else {
+		int size, i;
+
+		size = of_property_count_elems_of_size(client->dev.of_node,
+						"open-drain-pins", sizeof(u32));
		chip->gpio_start = -1;
		irq_base = 0;

+
+ /* negative sizes (errno) are simply ignored */
+
+ for (i = 0; i < size; i++) {
+ u32 off;
+ int r = of_property_read_u32_index(client->dev.of_node,
+ "open-drain-pins", i, &off);
+ if (r == 0 && off < (MAX_BANK * BANK_SZ)) {
+ chip->reg_open_drain[off / BANK_SZ] |=
+ (1u << (off % BANK_SZ));
+ }
+ }

	}

	chip->client = client;
Signature · 1 line
2.5.1
#2 lore raw
Quoted 8 lines · H. Nikolaus Schaller

1. add a DT property to specify the list of GPIO pins that are to become "open drain"
2. if a pin is configured as "open drain", set value by either outputting a "0" (low level) or switching to input (high impedance)

Tested on OMAP5 uEVM with TCA6424 and some LEDs connected to 5V.

I understand the approach, but this is the wrong way to do it. Instead of indicating that a pin on the pin controller should be open drain, the *consumer* specifying its gpios = <....> should do this.

I just sent a patch adding the bindings because Tony Lindgren and Grygorii Strashko approached me similar problems so let's begin with the bindings.

Implementing it in the kernel requires some elaboration and it's going to be more complicated than this local approach, but it will be more generic and reusable so that is what we need to do.

Yours,
Linus Walleij

#3 lore raw

Am 25.09.2015 um 00:11 schrieb Linus Walleij <[email protected]>:

Quoted 17 lines
Quoted 8 lines · H. Nikolaus Schaller

1. add a DT property to specify the list of GPIO pins that are to become "open drain"
2. if a pin is configured as "open drain", set value by either outputting a "0" (low level) or switching to input (high impedance)

Tested on OMAP5 uEVM with TCA6424 and some LEDs connected to 5V.

I understand the approach, but this is the wrong way to do it. Instead of indicating that a pin on the pin controller should be open drain, the *consumer* specifying its gpios = <....> should do this.

Quoted 7 lines

I just sent a patch adding the bindings because Tony Lindgren and Grygorii Strashko approached me similar problems so let's begin with the bindings.

Implementing it in the kernel requires some elaboration and it's going to be more complicated than this local approach, but it will be more generic and reusable so that is what we need to do.

Ok, I understand.

Should not be too difficult to modify the driver.

BR,
Nikolaus Schaller

#4 lore raw
In reply to Linus Walleij

Am 25.09.2015 um 00:11 schrieb Linus Walleij <[email protected]>:

Quoted 8 lines
Quoted 5 lines · H. Nikolaus Schaller

1. add a DT property to specify the list of GPIO pins that are to become "open drain"
2. if a pin is configured as "open drain", set value by either outputting a "0" (low level) or switching to input (high impedance)

Quoted 8 lines
Quoted 3 lines

Tested on OMAP5 uEVM with TCA6424 and some LEDs connected to 5V.

I understand the approach, but this is the wrong way to do it. Instead of indicating that a pin on the pin controller should be open drain, the *consumer* specifying its gpios = <....> should do this.

On a second thought I am no longer sure if that is the right approach.

A gpio is for input and output of “0” and “1” values. And a gpio consumer can control if the output should be active or inactive and input.

How the output is physically represented (totem-pole or single-ended, high power, low power etc.) is not a property of the gpio itself and there is IMHO no need for the consumer to be able to define (or change) it.

On SoC with integrated gpio controllers it is very common to have pinmux definitions completely separated from the gpio controller properties. But the gpio consumer can connect to pinctrl settings to describe that the pin should have certain physical properties.

There, it is common to be able to enable/disable pull-up/downs as well.

So turning a totem-pole output (as it is available in the tca6424) into an open-drain output is sort of turning off the pull-up transistor. I.e. comparable to a pinmux setting.

So such controllers should get the option to define pinctrl.

Some chips might have special control registers for doing that while others need the trick to switch between input and output mode but that is driver implementation detail.

But I am not sure if this has other implications.

Quoted 11 lines

I just sent a patch adding the bindings because Tony Lindgren and Grygorii Strashko approached me similar problems so let's begin with the bindings.

Implementing it in the kernel requires some elaboration and it's going to be more complicated than this local approach, but it will be more generic and reusable so that is what we need to do.

Yours,
Linus Walleij

BR,
Nikolaus Schaller--
To unsubscribe from this list: send the line "unsubscribe linux-gpio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw
In reply to a message not in the archive
Quoted 8 lines · H. Nikolaus Schaller

[Me]

Quoted 4 lines

I understand the approach, but this is the wrong way to do it. Instead of indicating that a pin on the pin controller should be open drain, the *consumer* specifying its gpios = <....> should do this.

On a second thought I am no longer sure if that is the right approach.

Quoted 7 lines

A gpio is for input and output of “0” and “1” values. And a gpio consumer can control if the output should be active or inactive and input.

How the output is physically represented (totem-pole or single-ended, high power, low power etc.) is not a property of the gpio itself and there is IMHO no need for the consumer to be able to define (or change) it.

As it is today we already have support for open drain/collector in the GPIO subsystem, as open drain pins should not be actively driven to 1 and open source pins not actively driven to 0, instead set as input for each of these use cases.
(See drivers/gpio/gpiolib.c, grep for OPEN_DRAIN)

Quoted 7 lines

On SoC with integrated gpio controllers it is very common to have pinmux definitions completely separated from the gpio controller properties. But the gpio consumer can connect to pinctrl settings to describe that the pin should have certain physical properties.

There, it is common to be able to enable/disable pull-up/downs as well.

So this is up to the driver to implement today, but last week Tony Lindgren and Grygorii Strashko approached me to add flags for this into the GPIO subsystem as well.

We already have a few GPIO drivers cross-calling
pinctrl_request_gpio()
pinctrl_free_gpio()
pinctrl_gpio_direction_input()
pinctrl_gpio_direction_output()

So we can very well add a
pinctrl_gpio_set_config() to set up pulls, open drain/source, drive strength etc if it makes sense.

Quoted 5 lines

So turning a totem-pole output (as it is available in the tca6424) into an open-drain output is sort of turning off the pull-up transistor. I.e. comparable to a pinmux setting.

So such controllers should get the option to define pinctrl.

Yes and Tony/Grygorii want to do that from the GPIO consumer side.

Quoted 5 lines

Some chips might have special control registers for doing that while others need the trick to switch between input and output mode but that is driver implementation detail.

But I am not sure if this has other implications.

We will hash it out as we go along, but I see no immediate blocker for this.

Yours,
Linus Walleij
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html