Skip to contentSkip to conversation

gpio_direction_output() needs an initial value

PATCH In review David Brownell 3 participants 4 messages Mar 2, 2007

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio_direction_output() needs an initial value

It's been pointed out that output GPIOs should have an initial value, to avoid signal glitching ... among other things, it can be some time before a driver is ready. This patch corrects that oversight, fixing

- documentation
- platforms supporting the GPIO interface
- users of that call (just one for now, others are pending)

Note that most platforms are clear about the hardware letting the output value be set before the pin direction is changed, but the s3c241x docs are vague on that topic ... so those chips might not avoid the glitches.

---
Please merge for 2.6.21 ... better to get these API details sorted out now, while the relevant patch is small.

 Documentation/gpio.txt               |    5 ++++-
 arch/arm/mach-at91/gpio.c            |    3 ++-
 arch/arm/mach-sa1100/generic.c       |    3 ++-
 arch/avr32/mach-at32ap/pio.c         |    4 +++-
 drivers/spi/atmel_spi.c              |    2 +-
 include/asm-arm/arch-at91/gpio.h     |    2 +-
 include/asm-arm/arch-omap/gpio.h     |    3 ++-
 include/asm-arm/arch-pxa/gpio.h      |    4 ++--
 include/asm-arm/arch-s3c2410/gpio.h  |    4 +++-
 include/asm-arm/arch-sa1100/gpio.h   |    2 +-
 include/asm-avr32/arch-at32ap/gpio.h |    2 +-
 11 files changed, 22 insertions(+), 12 deletions(-)
Documentation/gpio.txt +4 -1
Index: g26/Documentation/gpio.txt
===================================================================
@@ -105,12 +105,15 @@ setting up a platform_device using the G
 
 	/* set as input or output, returning 0 or negative errno */
 	int gpio_direction_input(unsigned gpio);
-	int gpio_direction_output(unsigned gpio);
+	int gpio_direction_output(unsigned gpio, int value);
 
 The return value is zero for success, else a negative errno.  It should
 be checked, since the get/set calls don't have error returns and since
 misconfiguration is possible.  (These calls could sleep.)
 
+For output GPIOs, the value provided becomes the initial value.  This
+helps avoid signal glitching during system startup.
+
 Setting the direction can fail if the GPIO number is invalid, or when
 that particular GPIO can't be used in that mode.  It's generally a bad
 idea to rely on boot firmware to have set the direction correctly, since
include/asm-arm/arch-at91/gpio.h +1 -1
Index: g26/include/asm-arm/arch-at91/gpio.h
===================================================================
@@ -223,7 +223,7 @@ static inline void gpio_free(unsigned gp
 }
 
 extern int gpio_direction_input(unsigned gpio);
-extern int gpio_direction_output(unsigned gpio);
+extern int gpio_direction_output(unsigned gpio, int value);
 
 static inline int gpio_get_value(unsigned gpio)
 {
arch/arm/mach-at91/gpio.c +2 -1
Index: g26/arch/arm/mach-at91/gpio.c
===================================================================
@@ -215,13 +215,14 @@ int gpio_direction_input(unsigned pin)
 }
 EXPORT_SYMBOL(gpio_direction_input);
 
-int gpio_direction_output(unsigned pin)
+int gpio_direction_output(unsigned pin, int value)
 {
 	void __iomem	*pio = pin_to_controller(pin);
 	unsigned	mask = pin_to_mask(pin);
 
 	if (!pio || !(__raw_readl(pio + PIO_PSR) & mask))
 		return -EINVAL;
+	__raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
 	__raw_writel(mask, pio + PIO_OER);
 	return 0;
 }
include/asm-arm/arch-omap/gpio.h +2 -1
Index: g26/include/asm-arm/arch-omap/gpio.h
===================================================================
@@ -113,8 +113,9 @@ static inline int gpio_direction_input(u
 	return __gpio_set_direction(gpio, 1);
 }
 
-static inline int gpio_direction_output(unsigned gpio)
+static inline int gpio_direction_output(unsigned gpio, int value)
 {
+	omap_set_gpio_dataout(gpio, value);
 	return __gpio_set_direction(gpio, 0);
 }
 
include/asm-arm/arch-pxa/gpio.h +2 -2
Index: g26/include/asm-arm/arch-pxa/gpio.h
===================================================================
@@ -43,9 +43,9 @@ static inline int gpio_direction_input(u
 	return pxa_gpio_mode(gpio | GPIO_IN);
 }
 
-static inline int gpio_direction_output(unsigned gpio)
+static inline int gpio_direction_output(unsigned gpio, int value)
 {
-	return pxa_gpio_mode(gpio | GPIO_OUT);
+	return pxa_gpio_mode(gpio | GPIO_OUT | (value ? 0 : GPIO_DFLT_LOW));
 }
 
 static inline int __gpio_get_value(unsigned gpio)
include/asm-arm/arch-s3c2410/gpio.h +3 -1
Index: g26/include/asm-arm/arch-s3c2410/gpio.h
===================================================================
@@ -44,9 +44,11 @@ static inline int gpio_direction_input(u
 	return 0;
 }
 
-static inline int gpio_direction_output(unsigned gpio)
+static inline int gpio_direction_output(unsigned gpio, int value)
 {
 	s3c2410_gpio_cfgpin(gpio, S3C2410_GPIO_OUTPUT);
+	/* could we write the value first, to avoid glitching? */
+	s3c2410_gpio_setpin(gpio, value);
 	return 0;
 }
 
include/asm-arm/arch-sa1100/gpio.h +1 -1
Index: g26/include/asm-arm/arch-sa1100/gpio.h
===================================================================
@@ -38,7 +38,7 @@ static inline void gpio_free(unsigned gp
 }
 
 extern int gpio_direction_input(unsigned gpio);
-extern int gpio_direction_output(unsigned gpio);
+extern int gpio_direction_output(unsigned gpio, int value);
 
 
 static inline int gpio_get_value(unsigned gpio)
arch/arm/mach-sa1100/generic.c +2 -1
Index: g26/arch/arm/mach-sa1100/generic.c
===================================================================
@@ -153,7 +153,7 @@ int gpio_direction_input(unsigned gpio)
 
 EXPORT_SYMBOL(gpio_direction_input);
 
-int gpio_direction_output(unsigned gpio)
+int gpio_direction_output(unsigned gpio, int value)
 {
 	unsigned long flags;
 
@@ -161,6 +161,7 @@ int gpio_direction_output(unsigned gpio)
 		return -EINVAL;
 
 	local_irq_save(flags);
+	gpio_set_value(gpio, value);
 	GPDR |= GPIO_GPIO(gpio);
 	local_irq_restore(flags);
 	return 0;
include/asm-avr32/arch-at32ap/gpio.h +1 -1
Index: g26/include/asm-avr32/arch-at32ap/gpio.h
===================================================================
@@ -10,7 +10,7 @@ int __must_check gpio_request(unsigned i
 void gpio_free(unsigned int gpio);
 
 int gpio_direction_input(unsigned int gpio);
-int gpio_direction_output(unsigned int gpio);
+int gpio_direction_output(unsigned int gpio, int value);
 int gpio_get_value(unsigned int gpio);
 void gpio_set_value(unsigned int gpio, int value);
 
arch/avr32/mach-at32ap/pio.c +3 -1
Index: g26/arch/avr32/mach-at32ap/pio.c
===================================================================
@@ -214,7 +214,7 @@ int gpio_direction_input(unsigned int gp
 }
 EXPORT_SYMBOL(gpio_direction_input);
 
-int gpio_direction_output(unsigned int gpio)
+int gpio_direction_output(unsigned int gpio, int value)
 {
 	struct pio_device *pio;
 	unsigned int pin;
@@ -223,6 +223,8 @@ int gpio_direction_output(unsigned int g
 	if (!pio)
 		return -ENODEV;
 
+	gpio_set_value(gpio, value);
+
 	pin = gpio & 0x1f;
 	pio_writel(pio, OER, 1 << pin);
 
drivers/spi/atmel_spi.c +1 -1
Index: g26/drivers/spi/atmel_spi.c
===================================================================
@@ -425,7 +425,7 @@ static int atmel_spi_setup(struct spi_de
 		if (ret)
 			return ret;
 		spi->controller_state = (void *)npcs_pin;
-		gpio_direction_output(npcs_pin);
+		gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH));
 	}
 
 	dev_dbg(&spi->dev,
Acked-by on v1 1/1
#1 lore raw

hi David,

Quoted 3 lines

It's been pointed out that output GPIOs should have an initial value, to avoid signal glitching ... among other things, it can be some time before a driver is ready. This patch corrects that oversight, fixing

For the AT91 changes:

drivers/spi/atmel_spi.c static int atmel_spi_setup(struct spi_de · line 429

Quoted 6 lines
drivers/spi/atmel_spi.c +1 -1
 
-		gpio_direction_output(npcs_pin);
+		gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH));
 	}

As mentioned previously (by Walter Tuppa), wouldn't it be better to just change this to:

     cs_deactivate(spi);

Regards,
Andrew Victor

#2 lore raw
Quoted 13 lines · Andrew Victor

hi David,

Quoted 3 lines

It's been pointed out that output GPIOs should have an initial value, to avoid signal glitching ... among other things, it can be some time before a driver is ready. This patch corrects that oversight, fixing

For the AT91 changes:

Quoted 2 lines
drivers/spi/atmel_spi.c +0 -0
Quoted 7 lines

drivers/spi/atmel_spi.c static int atmel_spi_setup(struct spi_de · line 429

Quoted 3 lines
drivers/spi/atmel_spi.c +1 -1
-		gpio_direction_output(npcs_pin);
+		gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH));
 	}

As mentioned previously (by Walter Tuppa), wouldn't it be better to just change this to:

     cs_deactivate(spi);

That *does* deactivate the chipselect, as the initial value. Given this function signature change, some initial value must be specified, and that expression uses the inactive value...

Acked-by on v1 1/1
#3 lore raw
In reply to David Brownell
Quoted 8 lines · On Thu, 1 Mar 2007 18:25:09 -0800 David Brownell <[email protected]> wrote:

It's been pointed out that output GPIOs should have an initial value, to avoid signal glitching ... among other things, it can be some time before a driver is ready. This patch corrects that oversight, fixing

- documentation
- platforms supporting the GPIO interface
- users of that call (just one for now, others are pending)

Quoted 5 lines

Note that most platforms are clear about the hardware letting the output value be set before the pin direction is changed, but the s3c241x docs are vague on that topic ... so those chips might not avoid the glitches.

Thanks, Dave.

Haavard