Skip to contentSkip to conversation

drivers gpio: Check for negativ gpio values

PATCH In review Teresa Gámez 4 participants 6 messages Sep 24, 2012

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

PATCH 1/2 drivers gpio: Check for negativ gpio values

---
 drivers/gpio/gpio.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/gpio/gpio.c +1 -1
index 6ad8d27..0966e2a 100644
@@ -10,7 +10,7 @@ static struct gpio_chip *gpio_desc[ARCH_NR_GPIOS];
 
 static int gpio_is_valid(unsigned gpio)
 {
-	if (gpio < ARCH_NR_GPIOS)
+	if (gpio < ARCH_NR_GPIOS && gpio >= 0)
 		return 1;
 	return 0;
 }
Signature · 7 lines
1.7.0.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

PATCH 2/2 ARM OMAP: switch to gpiolib support

---
 arch/arm/Kconfig                   |    1 +
 arch/arm/mach-omap/gpio.c          |  171 ++++++++++++++++-------------------
 arch/arm/mach-omap/omap3_generic.c |   19 ++++
 arch/arm/mach-omap/omap4_generic.c |   19 ++++
 4 files changed, 117 insertions(+), 93 deletions(-)
arch/arm/Kconfig +1 -0
index a54ad03..7b7b058 100644
@@ -58,6 +58,7 @@ config ARCH_NOMADIK
 config ARCH_OMAP
 	bool "TI OMAP"
 	select HAS_DEBUG_LL
+	select GPIOLIB
 
 config ARCH_PXA
 	bool "Intel/Marvell PXA based"
arch/arm/mach-omap/gpio.c +78 -93

218 lines not on this page · raw message

arch/arm/mach-omap/omap3_generic.c +19 -0
index 4ab265a..0e9cd71 100644
@@ -515,3 +515,22 @@ const struct gpmc_config omap3_nand_cfg = {
 	.base = 0x28000000,
 	.size = GPMC_SIZE_16M,
 };
+
+static int omap3_gpio_init(void)
+{
+	add_generic_device("omap-gpio", 0, NULL, 0x48310000,
+					0x100, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 1, NULL, 0x49050000,
+					0x100, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 2, NULL, 0x49052000,
+					0x100, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 3, NULL, 0x49054000,
+					0x100, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 4, NULL, 0x49056000,
+					0x100, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 5, NULL, 0x49058000,
+					0x100, IORESOURCE_MEM, NULL);
+
+	return 0;
+}
+coredevice_initcall(omap3_gpio_init);
arch/arm/mach-omap/omap4_generic.c +19 -0
index 617d786..a159dfc 100644
@@ -572,3 +572,22 @@ const struct gpmc_config omap4_nand_cfg = {
 	.base = 0x28000000,
 	.size = GPMC_SIZE_16M,
 };
+
+static int omap4_gpio_init(void)
+{
+	add_generic_device("omap-gpio", 0, NULL, 0x4a310100,
+				0x1000, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 1, NULL, 0x48055100,
+				0x1000, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 2, NULL, 0x48057100,
+				0x1000, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 3, NULL, 0x48059100,
+				0x1000, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 4, NULL, 0x4805b100,
+				0x1000, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 5, NULL, 0x4805d100,
+				0x1000, IORESOURCE_MEM, NULL);
+
+	return 0;
+}
+coredevice_initcall(omap4_gpio_init);
Signature · 7 lines
1.7.0.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw
In reply to Teresa Gámez

drivers/gpio/gpio.c static struct gpio_chip *gpio_desc[ARCH_NR_GPIOS]; · line 13

Quoted 4 lines · Teresa Gámez

static int gpio_is_valid(unsigned gpio)
{

drivers/gpio/gpio.c +1 -1
-	if (gpio < ARCH_NR_GPIOS)
+	if (gpio < ARCH_NR_GPIOS && gpio >= 0)

For an 'unsigned gpio', gpio >= 0 is always true.

Regards,
Jan Lübbe

Signature · 10 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw

Am 24.09.2012 11:16, schrieb Jan Lübbe:

Quoted 6 lines · Teresa Gámez

drivers/gpio/gpio.c static struct gpio_chip *gpio_desc[ARCH_NR_GPIOS]; · line 13

Quoted 4 lines
  static int gpio_is_valid(unsigned gpio)
  {
drivers/gpio/gpio.c +1 -1
-	if (gpio < ARCH_NR_GPIOS)
+	if (gpio < ARCH_NR_GPIOS && gpio >= 0)

For an 'unsigned gpio', gpio >= 0 is always true.

Thats true...
Ignore this one then.

Sorry, forgot to cc mailing list.

Teresa

Quoted 2 lines

Regards,
Jan Lübbe

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#5 lore raw
In reply to Jan Lübbe
Quoted 7 lines · Jan Lübbe

drivers/gpio/gpio.c static struct gpio_chip *gpio_desc[ARCH_NR_GPIOS]; · line 13

Quoted 4 lines · Teresa Gámez

static int gpio_is_valid(unsigned gpio)
{

drivers/gpio/gpio.c +1 -1
-	if (gpio < ARCH_NR_GPIOS)
+	if (gpio < ARCH_NR_GPIOS && gpio >= 0)

For an 'unsigned gpio', gpio >= 0 is always true.

unsigned is wrong type gpio need to int

Best Regards,
J.

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#2 lore raw

Re: [PATCH 2/2] ARM OMAP: switch to gpiolib support

In reply to Teresa Gámez

Hi Teresa,

Quoted 2 lines · Teresa Gámez

Nice cleanup. Applied, thanks

Sascha

Quoted 305 lines
---
 arch/arm/Kconfig                   |    1 +
 arch/arm/mach-omap/gpio.c          |  171 ++++++++++++++++-------------------
 arch/arm/mach-omap/omap3_generic.c |   19 ++++
 arch/arm/mach-omap/omap4_generic.c |   19 ++++
 4 files changed, 117 insertions(+), 93 deletions(-)
arch/arm/Kconfig +1 -0
index a54ad03..7b7b058 100644
@@ -58,6 +58,7 @@ config ARCH_NOMADIK
 config ARCH_OMAP
 	bool "TI OMAP"
 	select HAS_DEBUG_LL
+	select GPIOLIB
 
 config ARCH_PXA
 	bool "Intel/Marvell PXA based"
arch/arm/mach-omap/gpio.c +78 -93
index 142cf52..d217550 100644
@@ -36,11 +36,10 @@
  * published by the Free Software Foundation.
  */
 #include <common.h>
-#include <mach/gpio.h>
 #include <io.h>
 #include <errno.h>
-
-#ifdef CONFIG_ARCH_OMAP3
+#include <gpio.h>
+#include <init.h>
 
 #define OMAP_GPIO_OE		0x0034
 #define OMAP_GPIO_DATAIN	0x0038
@@ -48,129 +47,115 @@
 #define OMAP_GPIO_CLEARDATAOUT	0x0090
 #define OMAP_GPIO_SETDATAOUT	0x0094
 
-static void __iomem *gpio_bank[] = {
-	(void *)0x48310000,
-	(void *)0x49050000,
-	(void *)0x49052000,
-	(void *)0x49054000,
-	(void *)0x49056000,
-	(void *)0x49058000,
-};
-#endif
-
-#ifdef CONFIG_ARCH_OMAP4
-
-#define OMAP_GPIO_OE		0x0134
-#define OMAP_GPIO_DATAIN	0x0138
-#define OMAP_GPIO_DATAOUT	0x013c
-#define OMAP_GPIO_CLEARDATAOUT	0x0190
-#define OMAP_GPIO_SETDATAOUT	0x0194
-
-static void __iomem *gpio_bank[] = {
-	(void *)0x4a310000,
-	(void *)0x48055000,
-	(void *)0x48057000,
-	(void *)0x48059000,
-	(void *)0x4805b000,
-	(void *)0x4805d000,
+struct omap_gpio_chip {
+	void __iomem *base;
+	struct gpio_chip chip;
 };
-#endif
-
-static inline void __iomem *get_gpio_base(int gpio)
-{
-	return gpio_bank[gpio >> 5];
-}
 
-static inline int get_gpio_index(int gpio)
+static inline int omap_get_gpio_index(int gpio)
 {
 	return gpio & 0x1f;
 }
 
-static inline int gpio_valid(int gpio)
+static void omap_gpio_set_value(struct gpio_chip *chip,
+					unsigned gpio, int value)
 {
-	if (gpio < 0)
-		return -1;
-	if (gpio / 32 < ARRAY_SIZE(gpio_bank))
-		return 0;
-	return -1;
-}
-
-static int check_gpio(int gpio)
-{
-	if (gpio_valid(gpio) < 0) {
-		printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
-		return -1;
-	}
-	return 0;
-}
-
-void gpio_set_value(unsigned gpio, int value)
-{
-	void __iomem *reg;
+	struct omap_gpio_chip *omapgpio =
+			container_of(chip, struct omap_gpio_chip, chip);
+	void __iomem *base = omapgpio->base;
 	u32 l = 0;
 
-	if (check_gpio(gpio) < 0)
-		return;
-
-	reg = get_gpio_base(gpio);
-
 	if (value)
-		reg += OMAP_GPIO_SETDATAOUT;
+		base += OMAP_GPIO_SETDATAOUT;
 	else
-		reg += OMAP_GPIO_CLEARDATAOUT;
-	l = 1 << get_gpio_index(gpio);
+		base += OMAP_GPIO_CLEARDATAOUT;
+
+	l = 1 << omap_get_gpio_index(gpio);
 
-	__raw_writel(l, reg);
+	writel(l, base);
 }
 
-int gpio_direction_input(unsigned gpio)
+static int omap_gpio_direction_input(struct gpio_chip *chip,
+					unsigned gpio)
 {
-	void __iomem *reg;
+	struct omap_gpio_chip *omapgpio =
+			container_of(chip, struct omap_gpio_chip, chip);
+	void __iomem *base = omapgpio->base;
 	u32 val;
 
-	if (check_gpio(gpio) < 0)
-		return -EINVAL;
-
-	reg = get_gpio_base(gpio);
-
-	reg += OMAP_GPIO_OE;
+	base += OMAP_GPIO_OE;
 
-	val = __raw_readl(reg);
-	val |= 1 << get_gpio_index(gpio);
-	__raw_writel(val, reg);
+	val = readl(base);
+	val |= 1 << omap_get_gpio_index(gpio);
+	writel(val, base);
 
 	return 0;
 }
 
-int gpio_direction_output(unsigned gpio, int value)
+static int omap_gpio_direction_output(struct gpio_chip *chip,
+					unsigned gpio, int value)
 {
-	void __iomem *reg;
+	struct omap_gpio_chip *omapgpio =
+			container_of(chip, struct omap_gpio_chip, chip);
+	void __iomem *base = omapgpio->base;
 	u32 val;
 
-	if (check_gpio(gpio) < 0)
-		return -EINVAL;
-	reg = get_gpio_base(gpio);
+	omap_gpio_set_value(chip, gpio, value);
 
-	gpio_set_value(gpio, value);
+	base += OMAP_GPIO_OE;
 
-	reg += OMAP_GPIO_OE;
-
-	val = __raw_readl(reg);
-	val &= ~(1 << get_gpio_index(gpio));
-	__raw_writel(val, reg);
+	val = readl(base);
+	val &= ~(1 << omap_get_gpio_index(gpio));
+	writel(val, base);
 
 	return 0;
 }
 
-int gpio_get_value(unsigned gpio)
+static int omap_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
+{
+	struct omap_gpio_chip *omapgpio =
+			container_of(chip, struct omap_gpio_chip, chip);
+	void __iomem *base = omapgpio->base;
+
+	base  += OMAP_GPIO_DATAIN;
+
+	return (readl(base) & (1 << omap_get_gpio_index(gpio))) != 0;
+
+}
+
+static struct gpio_ops omap_gpio_ops = {
+	.direction_input = omap_gpio_direction_input,
+	.direction_output = omap_gpio_direction_output,
+	.get = omap_gpio_get_value,
+	.set = omap_gpio_set_value,
+};
+
+static int omap_gpio_probe(struct device_d *dev)
 {
-	void __iomem *reg;
+	struct omap_gpio_chip *omapgpio;
+
+	omapgpio = xzalloc(sizeof(*omapgpio));
+	omapgpio->base = dev_request_mem_region(dev, 0);
+	omapgpio->chip.ops = &omap_gpio_ops;
+	omapgpio->chip.base = -1;
+	omapgpio->chip.ngpio = 32;
+	omapgpio->chip.dev = dev;
+	gpiochip_add(&omapgpio->chip);
 
-	if (check_gpio(gpio) < 0)
-		return -EINVAL;
-	reg = get_gpio_base(gpio);
+	dev_info(dev, "probed gpiochip%d with base %d\n",
+				dev->id, omapgpio->chip.base);
+
+	return 0;
+}
 
-	reg += OMAP_GPIO_DATAIN;
+static struct driver_d omap_gpio_driver = {
+	.name = "omap-gpio",
+	.probe = omap_gpio_probe,
+};
 
-	return (__raw_readl(reg) & (1 << get_gpio_index(gpio))) != 0;
+static int omap_gpio_add(void)
+{
+	register_driver(&omap_gpio_driver);
+	return 0;
 }
+coredevice_initcall(omap_gpio_add);
arch/arm/mach-omap/omap3_generic.c +19 -0
index 4ab265a..0e9cd71 100644
@@ -515,3 +515,22 @@ const struct gpmc_config omap3_nand_cfg = {
 	.base = 0x28000000,
 	.size = GPMC_SIZE_16M,
 };
+
+static int omap3_gpio_init(void)
+{
+	add_generic_device("omap-gpio", 0, NULL, 0x48310000,
+					0x100, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 1, NULL, 0x49050000,
+					0x100, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 2, NULL, 0x49052000,
+					0x100, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 3, NULL, 0x49054000,
+					0x100, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 4, NULL, 0x49056000,
+					0x100, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 5, NULL, 0x49058000,
+					0x100, IORESOURCE_MEM, NULL);
+
+	return 0;
+}
+coredevice_initcall(omap3_gpio_init);
arch/arm/mach-omap/omap4_generic.c +19 -0
index 617d786..a159dfc 100644
@@ -572,3 +572,22 @@ const struct gpmc_config omap4_nand_cfg = {
 	.base = 0x28000000,
 	.size = GPMC_SIZE_16M,
 };
+
+static int omap4_gpio_init(void)
+{
+	add_generic_device("omap-gpio", 0, NULL, 0x4a310100,
+				0x1000, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 1, NULL, 0x48055100,
+				0x1000, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 2, NULL, 0x48057100,
+				0x1000, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 3, NULL, 0x48059100,
+				0x1000, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 4, NULL, 0x4805b100,
+				0x1000, IORESOURCE_MEM, NULL);
+	add_generic_device("omap-gpio", 5, NULL, 0x4805d100,
+				0x1000, IORESOURCE_MEM, NULL);
+
+	return 0;
+}
+coredevice_initcall(omap4_gpio_init);
Signature · 7 lines
1.7.0.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox