Patchwork [U-Boot,06/22] ARM sunxi: Basic GPIO driver

login
register
mail settings
Submitter Henrik Nordström
Date Nov. 25, 2012, 11:40 a.m.
Message ID <1353843623.17518.17.camel@home.hno.se>
Download mbox | patch
Permalink /patch/201522/
State Deferred
Delegated to: Mike Frysinger
Headers show

Comments

Henrik Nordström - Nov. 25, 2012, 11:40 a.m.
GPIO driver for Allwinner sun4i/sun5i family of SoCs

GPIO Pins are named by their symbolic pin names P<g><#>
such as PH19 or H19.

Note: This do not perform any validation if the pin is in use
for some other I/O function. Use with care.
---
 arch/arm/include/asm/arch-sunxi/gpio.h |    2 +
 drivers/gpio/Makefile                  |    1 +
 drivers/gpio/sunxi_gpio.c              |  116 ++++++++++++++++++++++++++++++++
 include/configs/sunxi-common.h         |    4 +
 4 files changed, 123 insertions(+), 0 deletions(-)
 create mode 100644 drivers/gpio/sunxi_gpio.c
Luka Perkov - Nov. 25, 2012, 2:52 p.m.
Hi Henrik,

On Sun, Nov 25, 2012 at 12:40:23PM +0100, Henrik Nordström wrote:
> GPIO driver for Allwinner sun4i/sun5i family of SoCs
> 
> GPIO Pins are named by their symbolic pin names P<g><#>
> such as PH19 or H19.
> 
> Note: This do not perform any validation if the pin is in use
> for some other I/O function. Use with care.
> ---
>  arch/arm/include/asm/arch-sunxi/gpio.h |    2 +
>  drivers/gpio/Makefile                  |    1 +
>  drivers/gpio/sunxi_gpio.c              |  116 ++++++++++++++++++++++++++++++++
>  include/configs/sunxi-common.h         |    4 +
>  4 files changed, 123 insertions(+), 0 deletions(-)
>  create mode 100644 drivers/gpio/sunxi_gpio.c
> 
> diff --git a/arch/arm/include/asm/arch-sunxi/gpio.h b/arch/arm/include/asm/arch-sunxi/gpio.h
> index fceee6b..a3f8a74 100644
> --- a/arch/arm/include/asm/arch-sunxi/gpio.h
> +++ b/arch/arm/include/asm/arch-sunxi/gpio.h
> @@ -160,5 +160,7 @@ enum sunxi_gpio_number {
>  
>  int sunxi_gpio_set_cfgpin(u32 pin, u32 val);
>  int sunxi_gpio_get_cfgpin(u32 pin);
> +int name_to_gpio(const char *name);
> +#define name_to_gpio	name_to_gpio

Are you sure we need this define ?

>  
>  #endif /* _SUNXI_GPIO_H */
> diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
> index d50ac3b..6d692e6 100644
> --- a/drivers/gpio/Makefile
> +++ b/drivers/gpio/Makefile
> @@ -45,6 +45,7 @@ COBJS-$(CONFIG_OMAP_GPIO)	+= omap_gpio.o
>  COBJS-$(CONFIG_DB8500_GPIO)	+= db8500_gpio.o
>  COBJS-$(CONFIG_BCM2835_GPIO)	+= bcm2835_gpio.o
>  COBJS-$(CONFIG_S3C2440_GPIO)	+= s3c2440_gpio.o
> +COBJS-$(CONFIG_SUNXI_GPIO)	+= sunxi_gpio.o
>  
>  COBJS	:= $(COBJS-y)
>  SRCS 	:= $(COBJS:.o=.c)
> diff --git a/drivers/gpio/sunxi_gpio.c b/drivers/gpio/sunxi_gpio.c
> new file mode 100644
> index 0000000..d99071e
> --- /dev/null
> +++ b/drivers/gpio/sunxi_gpio.c
> @@ -0,0 +1,116 @@
> +/*
> + * (C) Copyright 2007-2011
> + * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
> + * Tom Cubie <tangliang@allwinnertech.com>
> + *
> + * See file CREDITS for list of people who contributed to this
> + * project.
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License as
> + * published by the Free Software Foundation; either version 2 of
> + * the License, or (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write to the Free Software
> + * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
> + * MA 02111-1307 USA
> + */
> +
> +#include <common.h>
> +#include <asm/io.h>
> +#include <asm/gpio.h>
> +
> +static int sunxi_gpio_output(u32 pin, u32 val)
> +{
> +	u32 dat;
> +	u32 bank = GPIO_BANK(pin);
> +	u32 num = GPIO_NUM(pin);
> +	struct sunxi_gpio *pio =
> +	    &((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank];
> +
> +	dat = readl(&pio->dat);
> +	if (val)
> +		dat |= 1 << num;
> +	else
> +		dat &= ~(1 << num);
> +
> +	writel(dat, &pio->dat);
> +
> +	return 0;
> +}
> +
> +static int sunxi_gpio_input(u32 pin)
> +{
> +	u32 dat;
> +	u32 bank = GPIO_BANK(pin);
> +	u32 num = GPIO_NUM(pin);
> +	struct sunxi_gpio *pio =
> +	    &((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank];
> +
> +	dat = readl(&pio->dat);
> +	dat >>= num;
> +
> +	return dat & 0x1;
> +}
> +
> +int gpio_request(unsigned gpio, const char *label)
> +{
> +	return 0;
> +}
> +
> +int gpio_free(unsigned gpio)
> +{
> +	return 0;
> +}
> +
> +int gpio_direction_input(unsigned gpio)
> +{
> +	sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_INPUT);
> +
> +	return sunxi_gpio_input(gpio);
> +}
> +
> +int gpio_direction_output(unsigned gpio, int value)
> +{
> +	sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_OUTPUT);
> +
> +	return sunxi_gpio_output(gpio, value);
> +}
> +
> +int gpio_get_value(unsigned gpio)
> +{
> +	return sunxi_gpio_input(gpio);
> +}
> +
> +int gpio_set_value(unsigned gpio, int value)
> +{
> +	return sunxi_gpio_output(gpio, value);
> +}
> +
> +int name_to_gpio(const char *name)
> +{
> +	int group = 0;
> +	int groupsize = 9 * 32;
> +	long pin;
> +	const char *eptr;
> +	if (*name == 'P' || *name == 'p')
> +		name++;
> +	if (*name >= 'A') {
> +		group = *name - (*name > 'a' ? 'a' : 'A');
> +		groupsize = 32;
> +		name++;
> +	}
> +
> +	pin = simple_strtol(name, &eptr, 10);
> +	if (!*name || *eptr)
> +		return -1;
> +	if (pin < 0 || pin > groupsize || group >= 9)
> +		return -1;
> +	return group * 32 + pin;
> +}
> diff --git a/include/configs/sunxi-common.h b/include/configs/sunxi-common.h
> index 5bf8eea..bc1f200 100644
> --- a/include/configs/sunxi-common.h
> +++ b/include/configs/sunxi-common.h
> @@ -206,4 +206,8 @@
>  /* #define CONFIG_WATCHDOG */
>  /* #define CONFIG_SUNXI_WATCHDOG */
>  
> +/* GPIO */
> +#define CONFIG_SUNXI_GPIO
> +#define CONFIG_CMD_GPIO

Why don't you use:

#ifdef CONFIG_CMD_GPIO
#define CONFIG_SUNXI_GPIO
#endif /* CONFIG_CMD_GPIO */

> +
>  #endif /* __CONFIG_H */
> -- 
> 1.7.7.6

Luka
Marek Vasut - Nov. 25, 2012, 6:14 p.m.
Dear Henrik Nordström,

> GPIO driver for Allwinner sun4i/sun5i family of SoCs
> 
> GPIO Pins are named by their symbolic pin names P<g><#>
> such as PH19 or H19.
> 
> Note: This do not perform any validation if the pin is in use
> for some other I/O function. Use with care.
> ---
>  arch/arm/include/asm/arch-sunxi/gpio.h |    2 +
>  drivers/gpio/Makefile                  |    1 +
>  drivers/gpio/sunxi_gpio.c              |  116
> ++++++++++++++++++++++++++++++++ include/configs/sunxi-common.h         | 
>   4 +
>  4 files changed, 123 insertions(+), 0 deletions(-)
>  create mode 100644 drivers/gpio/sunxi_gpio.c
> 
> diff --git a/arch/arm/include/asm/arch-sunxi/gpio.h
> b/arch/arm/include/asm/arch-sunxi/gpio.h index fceee6b..a3f8a74 100644
> --- a/arch/arm/include/asm/arch-sunxi/gpio.h
> +++ b/arch/arm/include/asm/arch-sunxi/gpio.h
> @@ -160,5 +160,7 @@ enum sunxi_gpio_number {
> 
>  int sunxi_gpio_set_cfgpin(u32 pin, u32 val);
>  int sunxi_gpio_get_cfgpin(u32 pin);
> +int name_to_gpio(const char *name);
> +#define name_to_gpio	name_to_gpio
> 
>  #endif /* _SUNXI_GPIO_H */
> diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
> index d50ac3b..6d692e6 100644
> --- a/drivers/gpio/Makefile
> +++ b/drivers/gpio/Makefile
> @@ -45,6 +45,7 @@ COBJS-$(CONFIG_OMAP_GPIO)	+= omap_gpio.o
>  COBJS-$(CONFIG_DB8500_GPIO)	+= db8500_gpio.o
>  COBJS-$(CONFIG_BCM2835_GPIO)	+= bcm2835_gpio.o
>  COBJS-$(CONFIG_S3C2440_GPIO)	+= s3c2440_gpio.o
> +COBJS-$(CONFIG_SUNXI_GPIO)	+= sunxi_gpio.o
> 
>  COBJS	:= $(COBJS-y)
>  SRCS 	:= $(COBJS:.o=.c)
> diff --git a/drivers/gpio/sunxi_gpio.c b/drivers/gpio/sunxi_gpio.c
> new file mode 100644
> index 0000000..d99071e
> --- /dev/null
> +++ b/drivers/gpio/sunxi_gpio.c
> @@ -0,0 +1,116 @@
> +/*
> + * (C) Copyright 2007-2011
> + * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
> + * Tom Cubie <tangliang@allwinnertech.com>
> + *
> + * See file CREDITS for list of people who contributed to this
> + * project.
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License as
> + * published by the Free Software Foundation; either version 2 of
> + * the License, or (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write to the Free Software
> + * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
> + * MA 02111-1307 USA
> + */
> +
> +#include <common.h>
> +#include <asm/io.h>
> +#include <asm/gpio.h>
> +
> +static int sunxi_gpio_output(u32 pin, u32 val)
> +{
> +	u32 dat;
> +	u32 bank = GPIO_BANK(pin);
> +	u32 num = GPIO_NUM(pin);
> +	struct sunxi_gpio *pio =
> +	    &((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank];
> +
> +	dat = readl(&pio->dat);
> +	if (val)
> +		dat |= 1 << num;
> +	else
> +		dat &= ~(1 << num);


clrbits_le32() / setbits_le32() ...

> +	writel(dat, &pio->dat);
> +
> +	return 0;
> +}
[...]

Best regards,
Marek Vasut
Wolfgang Denk - Nov. 25, 2012, 7:50 p.m.
Dear Henrik Nordström,

In message <1353843623.17518.17.camel@home.hno.se> you wrote:
> GPIO driver for Allwinner sun4i/sun5i family of SoCs
> 
> GPIO Pins are named by their symbolic pin names P<g><#>
> such as PH19 or H19.
> 
> Note: This do not perform any validation if the pin is in use
> for some other I/O function. Use with care.

SoB missing.


Um.... are these patches actually bisectable???  It appears not; for
example, you reference gpio_direction_input() in ealier patches, but
define it only now.

This patch series need a full rework.

Review stops here.

Best regards,

Wolfgang Denk
Marek Vasut - Nov. 25, 2012, 9:47 p.m.
Dear Wolfgang Denk,

> Dear Henrik Nordström,
> 
> In message <1353843623.17518.17.camel@home.hno.se> you wrote:
> > GPIO driver for Allwinner sun4i/sun5i family of SoCs
> > 
> > GPIO Pins are named by their symbolic pin names P<g><#>
> > such as PH19 or H19.
> > 
> > Note: This do not perform any validation if the pin is in use
> > for some other I/O function. Use with care.
> 
> SoB missing.
> 
> 
> Um.... are these patches actually bisectable???  It appears not; for
> example, you reference gpio_direction_input() in ealier patches, but
> define it only now.
> 
> This patch series need a full rework.

It needs a bit of polishing ;-)

Best regards,
Marek Vasut
Henrik Nordström - Nov. 25, 2012, 11:41 p.m.
sön 2012-11-25 klockan 20:50 +0100 skrev Wolfgang Denk:
> Dear Henrik Nordström,
> 
> In message <1353843623.17518.17.camel@home.hno.se> you wrote:
> > GPIO driver for Allwinner sun4i/sun5i family of SoCs
> > 
> > GPIO Pins are named by their symbolic pin names P<g><#>
> > such as PH19 or H19.
> > 
> > Note: This do not perform any validation if the pin is in use
> > for some other I/O function. Use with care.
> 
> SoB missing.

Sorry about that.

> Um.... are these patches actually bisectable???  It appears not; for
> example, you reference gpio_direction_input() in ealier patches, but
> define it only now.

Um.. right, on sun4i_sdcon with console redirected to the SD pins the
uart code uses that as a shorthand for sunxi_gpio_set_cfgpin(). Fixing.
Thanks for noticing.

And sorry for not doing a full matrix bisect validation, only did bisect
test on the basic targets when splitting the patch series. Will run a
full bisect matrix test for next round.

Regards
Henrik

Patch

diff --git a/arch/arm/include/asm/arch-sunxi/gpio.h b/arch/arm/include/asm/arch-sunxi/gpio.h
index fceee6b..a3f8a74 100644
--- a/arch/arm/include/asm/arch-sunxi/gpio.h
+++ b/arch/arm/include/asm/arch-sunxi/gpio.h
@@ -160,5 +160,7 @@  enum sunxi_gpio_number {
 
 int sunxi_gpio_set_cfgpin(u32 pin, u32 val);
 int sunxi_gpio_get_cfgpin(u32 pin);
+int name_to_gpio(const char *name);
+#define name_to_gpio	name_to_gpio
 
 #endif /* _SUNXI_GPIO_H */
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index d50ac3b..6d692e6 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -45,6 +45,7 @@  COBJS-$(CONFIG_OMAP_GPIO)	+= omap_gpio.o
 COBJS-$(CONFIG_DB8500_GPIO)	+= db8500_gpio.o
 COBJS-$(CONFIG_BCM2835_GPIO)	+= bcm2835_gpio.o
 COBJS-$(CONFIG_S3C2440_GPIO)	+= s3c2440_gpio.o
+COBJS-$(CONFIG_SUNXI_GPIO)	+= sunxi_gpio.o
 
 COBJS	:= $(COBJS-y)
 SRCS 	:= $(COBJS:.o=.c)
diff --git a/drivers/gpio/sunxi_gpio.c b/drivers/gpio/sunxi_gpio.c
new file mode 100644
index 0000000..d99071e
--- /dev/null
+++ b/drivers/gpio/sunxi_gpio.c
@@ -0,0 +1,116 @@ 
+/*
+ * (C) Copyright 2007-2011
+ * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
+ * Tom Cubie <tangliang@allwinnertech.com>
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <common.h>
+#include <asm/io.h>
+#include <asm/gpio.h>
+
+static int sunxi_gpio_output(u32 pin, u32 val)
+{
+	u32 dat;
+	u32 bank = GPIO_BANK(pin);
+	u32 num = GPIO_NUM(pin);
+	struct sunxi_gpio *pio =
+	    &((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank];
+
+	dat = readl(&pio->dat);
+	if (val)
+		dat |= 1 << num;
+	else
+		dat &= ~(1 << num);
+
+	writel(dat, &pio->dat);
+
+	return 0;
+}
+
+static int sunxi_gpio_input(u32 pin)
+{
+	u32 dat;
+	u32 bank = GPIO_BANK(pin);
+	u32 num = GPIO_NUM(pin);
+	struct sunxi_gpio *pio =
+	    &((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank];
+
+	dat = readl(&pio->dat);
+	dat >>= num;
+
+	return dat & 0x1;
+}
+
+int gpio_request(unsigned gpio, const char *label)
+{
+	return 0;
+}
+
+int gpio_free(unsigned gpio)
+{
+	return 0;
+}
+
+int gpio_direction_input(unsigned gpio)
+{
+	sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_INPUT);
+
+	return sunxi_gpio_input(gpio);
+}
+
+int gpio_direction_output(unsigned gpio, int value)
+{
+	sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_OUTPUT);
+
+	return sunxi_gpio_output(gpio, value);
+}
+
+int gpio_get_value(unsigned gpio)
+{
+	return sunxi_gpio_input(gpio);
+}
+
+int gpio_set_value(unsigned gpio, int value)
+{
+	return sunxi_gpio_output(gpio, value);
+}
+
+int name_to_gpio(const char *name)
+{
+	int group = 0;
+	int groupsize = 9 * 32;
+	long pin;
+	const char *eptr;
+	if (*name == 'P' || *name == 'p')
+		name++;
+	if (*name >= 'A') {
+		group = *name - (*name > 'a' ? 'a' : 'A');
+		groupsize = 32;
+		name++;
+	}
+
+	pin = simple_strtol(name, &eptr, 10);
+	if (!*name || *eptr)
+		return -1;
+	if (pin < 0 || pin > groupsize || group >= 9)
+		return -1;
+	return group * 32 + pin;
+}
diff --git a/include/configs/sunxi-common.h b/include/configs/sunxi-common.h
index 5bf8eea..bc1f200 100644
--- a/include/configs/sunxi-common.h
+++ b/include/configs/sunxi-common.h
@@ -206,4 +206,8 @@ 
 /* #define CONFIG_WATCHDOG */
 /* #define CONFIG_SUNXI_WATCHDOG */
 
+/* GPIO */
+#define CONFIG_SUNXI_GPIO
+#define CONFIG_CMD_GPIO
+
 #endif /* __CONFIG_H */