Skip to contentSkip to conversation

gpio: Add device driver for GRGPIO cores and support custom accessors with gpio-generic

PATCH In review Andreas Larsson 3 participants 14 messages Mar 15, 2013 – Apr 23, 2013

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

v5 cover gpio: Add device driver for GRGPIO cores and support custom accessors with gpio-generic

Differences since v4:
- Split out changes to gpio-generic into patch 1
- Make the basic driver without any irq support into patch 2, so that things can be applied so far if more revisions needs to be done for the irq support parts.
- Change irq support to use irq domain and put it in patch 3

Andreas Larsson (3):
gpio: gpio-generic: Add 16 and 32 bit big endian byte order support
gpio: grgpio: Add device driver for GRGPIO cores
gpio: grgpio: Add irq support

 .../devicetree/bindings/gpio/gpio-grgpio.txt       |   29 ++
 drivers/gpio/Kconfig                               |    9 +
 drivers/gpio/Makefile                              |    1 +
 drivers/gpio/gpio-generic.c                        |   56 ++-
 drivers/gpio/gpio-grgpio.c                         |  489 ++++++++++++++++++++
 include/linux/basic_mmio_gpio.h                    |    1 +
 6 files changed, 576 insertions(+), 9 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/gpio/gpio-grgpio.txt
 create mode 100644 drivers/gpio/gpio-grgpio.c
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH v5 1/3 gpio: gpio-generic: Add 16 and 32 bit big endian byte order support

There is no general support for 64-bit big endian accesses, so that is left unsupported.

---
 drivers/gpio/gpio-generic.c     |   56 ++++++++++++++++++++++++++++++++-------
 include/linux/basic_mmio_gpio.h |    1 +
 2 files changed, 48 insertions(+), 9 deletions(-)
drivers/gpio/gpio-generic.c +47 -9
index 05fcc0f..42d4706 100644
@@ -104,6 +104,26 @@ static unsigned long bgpio_read64(void __iomem *reg)
 }
 #endif /* BITS_PER_LONG >= 64 */
 
+static void bgpio_write16be(void __iomem *reg, unsigned long data)
+{
+	iowrite16be(data, reg);
+}
+
+static unsigned long bgpio_read16be(void __iomem *reg)
+{
+	return ioread16be(reg);
+}
+
+static void bgpio_write32be(void __iomem *reg, unsigned long data)
+{
+	iowrite32be(data, reg);
+}
+
+static unsigned long bgpio_read32be(void __iomem *reg)
+{
+	return ioread32be(reg);
+}
+
 static unsigned long bgpio_pin2mask(struct bgpio_chip *bgc, unsigned int pin)
 {
 	return 1 << pin;
@@ -249,7 +269,8 @@ static int bgpio_dir_out_inv(struct gpio_chip *gc, unsigned int gpio, int val)
 
 static int bgpio_setup_accessors(struct device *dev,
 				 struct bgpio_chip *bgc,
-				 bool be)
+				 bool bit_be,
+				 bool byte_be)
 {
 
 	switch (bgc->bits) {
@@ -258,17 +279,33 @@ static int bgpio_setup_accessors(struct device *dev,
 		bgc->write_reg	= bgpio_write8;
 		break;
 	case 16:
-		bgc->read_reg	= bgpio_read16;
-		bgc->write_reg	= bgpio_write16;
+		if (byte_be) {
+			bgc->read_reg	= bgpio_read16be;
+			bgc->write_reg	= bgpio_write16be;
+		} else {
+			bgc->read_reg	= bgpio_read16;
+			bgc->write_reg	= bgpio_write16;
+		}
 		break;
 	case 32:
-		bgc->read_reg	= bgpio_read32;
-		bgc->write_reg	= bgpio_write32;
+		if (byte_be) {
+			bgc->read_reg	= bgpio_read32be;
+			bgc->write_reg	= bgpio_write32be;
+		} else {
+			bgc->read_reg	= bgpio_read32;
+			bgc->write_reg	= bgpio_write32;
+		}
 		break;
 #if BITS_PER_LONG >= 64
 	case 64:
-		bgc->read_reg	= bgpio_read64;
-		bgc->write_reg	= bgpio_write64;
+		if (byte_be) {
+			dev_err(dev,
+				"64 bit big endian byte order unsupported\n");
+			return -EINVAL;
+		} else {
+			bgc->read_reg	= bgpio_read64;
+			bgc->write_reg	= bgpio_write64;
+		}
 		break;
 #endif /* BITS_PER_LONG >= 64 */
 	default:
@@ -276,7 +313,7 @@ static int bgpio_setup_accessors(struct device *dev,
 		return -EINVAL;
 	}
 
-	bgc->pin2mask = be ? bgpio_pin2mask_be : bgpio_pin2mask;
+	bgc->pin2mask = bit_be ? bgpio_pin2mask_be : bgpio_pin2mask;
 
 	return 0;
 }
@@ -385,7 +422,8 @@ int bgpio_init(struct bgpio_chip *bgc, struct device *dev,
 	if (ret)
 		return ret;
 
-	ret = bgpio_setup_accessors(dev, bgc, flags & BGPIOF_BIG_ENDIAN);
+	ret = bgpio_setup_accessors(dev, bgc, flags & BGPIOF_BIG_ENDIAN,
+				    flags & BGPIOF_BIG_ENDIAN_BYTE_ORDER);
 	if (ret)
 		return ret;
 
include/linux/basic_mmio_gpio.h +1 -0
index 1c504ca..d8a97ec 100644
@@ -72,5 +72,6 @@ int bgpio_init(struct bgpio_chip *bgc, struct device *dev,
 #define BGPIOF_BIG_ENDIAN		BIT(0)
 #define BGPIOF_UNREADABLE_REG_SET	BIT(1) /* reg_set is unreadable */
 #define BGPIOF_UNREADABLE_REG_DIR	BIT(2) /* reg_dir is unreadable */
+#define BGPIOF_BIG_ENDIAN_BYTE_ORDER	BIT(3)
 
 #endif /* __BASIC_MMIO_GPIO_H */
Signature · 1 line
1.7.10.4
#3 lore raw

Re: [PATCH v5 1/3] gpio: gpio-generic: Add 16 and 32 bit big endian byte order support

Quoted 9 lines · Anton Vorontsov
Quoted 4 lines · Andreas Larsson

There is no general support for 64-bit big endian accesses, so that is left unsupported.

This looks perfect, thanks a lot!

Looks good to me as well, but since this was discussed with Grant I want his review on this before I merge it.

Yours,
Linus Walleij

#4 lore raw

Re: [PATCH v5 1/3] gpio: gpio-generic: Add 16 and 32 bit big endian byte order support

Quoted 5 lines · Linus Walleij
Quoted 4 lines · Anton Vorontsov
Quoted 3 lines · Andreas Larsson

There is no general support for 64-bit big endian accesses, so that is left unsupported.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

This looks perfect, thanks a lot!

Looks good to me as well, but since this was discussed with Grant I want his review on this before I merge it.

I take it that you are happy with the other two patches as well?

Grant, any chance of getting this into the 3.10 merge window?

Cheers,
Andreas Larsson

#5 lore raw

Re: [PATCH v5 1/3] gpio: gpio-generic: Add 16 and 32 bit big endian byte order support

Quoted 17 lines · Andreas Larson
Quoted 16 lines · Linus Walleij
Quoted 12 lines · Anton Vorontsov
Quoted 5 lines · Andreas Larsson

There is no general support for 64-bit big endian accesses, so that is left unsupported.

This looks perfect, thanks a lot!

Quoted 7 lines
Quoted 2 lines

Looks good to me as well, but since this was discussed with Grant I want his review on this before I merge it.

I take it that you are happy with the other two patches as well?

Grant, any chance of getting this into the 3.10 merge window?

Apparently Grant is busy these days, so I took a review of the patch and I like it. With Anton's ACK as well I'm certain it's good, so patch applied!

Yours,
Linus Walleij

#6 lore raw

PATCH v5 2/3 gpio: grgpio: Add device driver for GRGPIO cores

In reply to Andreas Larsson

This driver supports GRGPIO gpio cores available in the GRLIB VHDL IP core library from Aeroflex Gaisler.

---
 .../devicetree/bindings/gpio/gpio-grgpio.txt       |   24 +++
 drivers/gpio/Kconfig                               |    9 ++
 drivers/gpio/Makefile                              |    1 +
 drivers/gpio/gpio-grgpio.c                         |  164 ++++++++++++++++++++
 4 files changed, 198 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/gpio/gpio-grgpio.txt
 create mode 100644 drivers/gpio/gpio-grgpio.c
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +24 -0
new file mode 100644
index 0000000..1050dc8
@@ -0,0 +1,24 @@
+Aeroflex Gaisler GRGPIO General Purpose I/O cores.
+
+The GRGPIO GPIO core is available in the GRLIB VHDL IP core library.
+
+Note: In the ordinary environment for the GRGPIO core, a Leon SPARC system,
+these properties are built from information in the AMBA plug&play.
+
+Required properties:
+
+- name : Should be "GAISLER_GPIO" or "01_01a"
+
+- reg : Address and length of the register set for the device
+
+- interrupts : Interrupt numbers for this device
+
+Optional properties:
+
+- base : The base gpio number for the core. A dynamic base is used if not
+	present
+
+- nbits : The number of gpio lines. If not present driver assumes 32 lines.
+
+For further information look in the documentation for the GLIB IP core library:
+http://www.gaisler.com/products/grlib/grip.pdf
drivers/gpio/Kconfig +9 -0
index 93aaadf..32f068d 100644
@@ -309,6 +309,15 @@ config GPIO_LYNXPOINT
 	  driver for GPIO functionality on Intel Lynxpoint PCH chipset
 	  Requires ACPI device enumeration code to set up a platform device.
 
+config GPIO_GRGPIO
+	tristate "Aeroflex Gaisler GRGPIO support"
+	depends on OF
+	select GPIO_GENERIC
+	select IRQ_DOMAIN
+	help
+	  Select this to support Aeroflex Gaisler GRGPIO cores from the GRLIB
+	  VHDL IP core library.
+
 comment "I2C GPIO expanders:"
 
 config GPIO_ARIZONA
drivers/gpio/Makefile +1 -0
index 22e07bc..3e6be51 100644
@@ -25,6 +25,7 @@ obj-$(CONFIG_ARCH_DAVINCI)	+= gpio-davinci.o
 obj-$(CONFIG_GPIO_EM)		+= gpio-em.o
 obj-$(CONFIG_GPIO_EP93XX)	+= gpio-ep93xx.o
 obj-$(CONFIG_GPIO_GE_FPGA)	+= gpio-ge.o
+obj-$(CONFIG_GPIO_GRGPIO)	+= gpio-grgpio.o
 obj-$(CONFIG_GPIO_ICH)		+= gpio-ich.o
 obj-$(CONFIG_GPIO_IT8761E)	+= gpio-it8761e.o
 obj-$(CONFIG_GPIO_JANZ_TTL)	+= gpio-janz-ttl.o
drivers/gpio/gpio-grgpio.c new file +164 -0

164 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#7 lore raw

Re: [PATCH v5 2/3] gpio: grgpio: Add device driver for GRGPIO cores

Quoted 19 lines · Andreas Larsson

This driver supports GRGPIO gpio cores available in the GRLIB VHDL IP core library from Aeroflex Gaisler.

---
 .../devicetree/bindings/gpio/gpio-grgpio.txt       |   24 +++
 drivers/gpio/Kconfig                               |    9 ++
 drivers/gpio/Makefile                              |    1 +
 drivers/gpio/gpio-grgpio.c                         |  164 ++++++++++++++++++++
 4 files changed, 198 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/gpio/gpio-grgpio.txt
 create mode 100644 drivers/gpio/gpio-grgpio.c
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +0 -0
new file mode 100644
index 0000000..1050dc8

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt line 10

Quoted 10 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +10 -0
@@ -0,0 +1,24 @@
+Aeroflex Gaisler GRGPIO General Purpose I/O cores.
+
+The GRGPIO GPIO core is available in the GRLIB VHDL IP core library.
+
+Note: In the ordinary environment for the GRGPIO core, a Leon SPARC system,
+these properties are built from information in the AMBA plug&play.
+
+Required properties:
+
+- name : Should be "GAISLER_GPIO" or "01_01a"

What is this? Don't we usually use a .compatible string for this? Name? Que? Is that something legacy?

I would prefer:

- Add you vendor prefix to Documentation/devicetree/bindings/vendor-prefixes.txt
- Use a compatible string like this "gaisler,gpio"

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt line 19

Quoted 8 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +8 -0
+- reg : Address and length of the register set for the device
+
+- interrupts : Interrupt numbers for this device
+
+Optional properties:
+
+- base : The base gpio number for the core. A dynamic base is used if not
+       present

This has to go. We don't hardcode numbers from the global GPIO space into the device tree, beacause as you soon realize this is Linux-specific and the device tree shall be OS agnostic.

The discussion has come up a number of times, review the mailing lists for suggestions on how to get around this.

(...)

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt

Quoted 13 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +12 -0
+++ b/drivers/gpio/gpio-grgpio.c
 
+struct grgpio_regs {
+       u32 data;       /* 0x00 */
+       u32 output;     /* 0x04 */
+       u32 dir;        /* 0x08 */
+       u32 imask;      /* 0x0c */
+       u32 ipol;       /* 0x10  */
+       u32 iedge;      /* 0x14 */
+       u32 bypass;     /* 0x18 */
+       u32 __reserved; /* 0x1c */
+       u32 imap[8];    /* 0x20-0x3c */
+};

Um... Why are you doing this?

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt

Quoted 3 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +3 -0
+struct grgpio_priv {
+       struct bgpio_chip bgc;
+       struct grgpio_regs __iomem *regs;

And that's tagged as __iomem * as well, that is very unorthodox. The usual practice is to have a base pointer void __iomem *base and offset from that.

Quoted 4 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +4 -0
+       struct device *dev;
+};
+
+static inline struct grgpio_priv *grgpio_gc_to_priv(struct gpio_chip *gc)

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt

Quoted 10 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +10 -0
+{
+       struct bgpio_chip *bgc = to_bgpio_chip(gc);
+
+       return container_of(bgc, struct grgpio_priv, bgc);
+}
+
+static int grgpio_to_irq(struct gpio_chip *gc, unsigned offset)
+{
+       return -ENXIO;
+}

The gpiolib core already returns -ENXIO if this function is not assigned so just delete this function and leave that function pointer as NULL.

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt

Quoted 4 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +4 -0
+static int grgpio_probe(struct platform_device *ofdev)
+{
+       struct device_node *np = ofdev->dev.of_node;
+       struct grgpio_regs __iomem *regs;

Prefer void __iomem *base;

Quoted 9 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +9 -0
+       struct gpio_chip *gc;
+       struct bgpio_chip *bgc;
+       struct grgpio_priv *priv;
+       struct resource *res;
+       int err;
+       u32 prop;
+
+       priv = devm_kzalloc(&ofdev->dev, sizeof(*priv), GFP_KERNEL);
+       if (!priv)

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt

Quoted 10 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +10 -0
+               return -ENOMEM;
+
+       res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
+       regs = devm_ioremap_resource(&ofdev->dev, res);
+       if (IS_ERR(regs))
+               return PTR_ERR(regs);
+
+       bgc = &priv->bgc;
+       err = bgpio_init(bgc, &ofdev->dev, 4, &regs->data, &regs->output, NULL,
+                        &regs->dir, NULL, BGPIOF_BIG_ENDIAN_BYTE_ORDER);

So I would prefer if you did:

#define GRGPIO_DATA 0x00
#define GRGPIO_OUTPUT 0x04
#define GRGPIO_DIR 0x08
(...)

err = bgpio_init(bgc, &ofdev->dev, 4, base + GRGPIO_DATA, base + GRGPIO_OUTPUT, NULL,

                       base + GRGPIO_DIR, NULL, BGPIOF_BIG_ENDIAN_BYTE_ORDER);
Quoted 11 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +11 -0
+       if (err) {
+               dev_err(&ofdev->dev, "bgpio_init() failed\n");
+               return err;
+       }
+
+       priv->regs = regs;
+       priv->dev = &ofdev->dev;
+
+       gc = &bgc->gc;
+       gc->of_node = np;
+       gc->owner = THIS_MODULE;

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt

Quoted 10 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +10 -0
+       gc->to_irq = grgpio_to_irq;
+       gc->label = np->full_name;
+
+       err = of_property_read_u32(np, "base", &prop);
+       if (err) {
+               dev_dbg(&ofdev->dev, "No base property: use dynamic base\n");
+               gc->base = -1;
+       } else {
+               gc->base = prop;
+       }

Over my dead body ;-)

(...)

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt

Quoted 5 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +5 -0
+static struct of_device_id grgpio_match[] = {
+       {.name = "GAISLER_GPIO"},
+       {.name = "01_01a"},
+       {},
+};

This is very weird. Especially "01_01a" needs a real good explanation if it is to be kept.

Alas, I don't really know what the .name field in the of_device_id is for...

Yours,
Linus Walleij

#8 lore raw

Re: [PATCH v5 2/3] gpio: grgpio: Add device driver for GRGPIO cores

Quoted 26 lines · Linus Walleij
Quoted 24 lines · Andreas Larsson

This driver supports GRGPIO gpio cores available in the GRLIB VHDL IP core library from Aeroflex Gaisler.

---
  .../devicetree/bindings/gpio/gpio-grgpio.txt       |   24 +++
  drivers/gpio/Kconfig                               |    9 ++
  drivers/gpio/Makefile                              |    1 +
  drivers/gpio/gpio-grgpio.c                         |  164 ++++++++++++++++++++
  4 files changed, 198 insertions(+)

create mode 100644 Documentation/devicetree/bindings/gpio/gpio-grgpio.txt create mode 100644 drivers/gpio/gpio-grgpio.c

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +5 -0
new file mode 100644
index 0000000..1050dc8
@@ -0,0 +1,24 @@
+Aeroflex Gaisler GRGPIO General Purpose I/O cores.
+
+The GRGPIO GPIO core is available in the GRLIB VHDL IP core library.
+
+Note: In the ordinary environment for the GRGPIO core, a Leon SPARC system,
Quoted 8 lines

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt line 10

Quoted 5 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +5 -0
+these properties are built from information in the AMBA plug&play.
+
+Required properties:
+
+- name : Should be "GAISLER_GPIO" or "01_01a"

What is this? Don't we usually use a .compatible string for this? Name? Que? Is that something legacy?

Regarding using name, this is standard for SPARC. The names in the device tree originates from the PROM.

The name field is the actually the first field checked for a match in of_match_node, followed by type then compatible. See
http://lxr.free-electrons.com/source/drivers/of/base.c#L572

Examples can be found among many others in:
- drivers/watchdog/riowd.c
- drivers/watchdog/cpwd.c
- drivers/mtd/maps/sun_uflash.c
- drivers/input/misc/sparcspkr.c
- drivers/input/serio/i8042-sparcio.h
- drivers/hwmon/ultra45_env.c

As for the "01_01a", the LEON SPARC systems uses a plug&play to identify different IP cores in the system. When the PROM is unaware of the name of a certain core, the name field presented from the prom will be on this form. This is standard handling for LEON SPARC drivers.

Examples of this can be found in:
- drivers/gpio/gpio-grgpio.c
- drivers/usb/host/uhci-grlib.c
- drivers/usb/host/ehci-grlib.c
- drivers/video/grvga.c
- drivers/net/can/grcan.c
- drivers/net/ethernet/aeroflex/greth.c
- drivers/tty/serial/apbuart.c

Quoted 56 lines

I would prefer:

- Add you vendor prefix to Documentation/devicetree/bindings/vendor-prefixes.txt
- Use a compatible string like this "gaisler,gpio"

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt line 19

Quoted 8 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +8 -0
+- reg : Address and length of the register set for the device
+
+- interrupts : Interrupt numbers for this device
+
+Optional properties:
+
+- base : The base gpio number for the core. A dynamic base is used if not
+       present

This has to go. We don't hardcode numbers from the global GPIO space into the device tree, beacause as you soon realize this is Linux-specific and the device tree shall be OS agnostic.

The discussion has come up a number of times, review the mailing lists for suggestions on how to get around this.

(...)

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt

Quoted 13 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +12 -0
+++ b/drivers/gpio/gpio-grgpio.c
 
+struct grgpio_regs {
+       u32 data;       /* 0x00 */
+       u32 output;     /* 0x04 */
+       u32 dir;        /* 0x08 */
+       u32 imask;      /* 0x0c */
+       u32 ipol;       /* 0x10  */
+       u32 iedge;      /* 0x14 */
+       u32 bypass;     /* 0x18 */
+       u32 __reserved; /* 0x1c */
+       u32 imap[8];    /* 0x20-0x3c */
+};

Um... Why are you doing this?

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt

Quoted 3 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +3 -0
+struct grgpio_priv {
+       struct bgpio_chip bgc;
+       struct grgpio_regs __iomem *regs;

And that's tagged as __iomem * as well, that is very unorthodox. The usual practice is to have a base pointer void __iomem *base and offset from that.

Quoted 10 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +10 -0
+       struct device *dev;
+};
+
+static inline struct grgpio_priv *grgpio_gc_to_priv(struct gpio_chip *gc)
+{
+       struct bgpio_chip *bgc = to_bgpio_chip(gc);
+
+       return container_of(bgc, struct grgpio_priv, bgc);
+}
+
Quoted 8 lines

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt

Quoted 4 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +4 -0
+static int grgpio_to_irq(struct gpio_chip *gc, unsigned offset)
+{
+       return -ENXIO;
+}

The gpiolib core already returns -ENXIO if this function is not assigned so just delete this function and leave that function pointer as NULL.

Sure, the second patch should add it from scratch.

Quoted 29 lines

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt

Quoted 4 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +4 -0
+static int grgpio_probe(struct platform_device *ofdev)
+{
+       struct device_node *np = ofdev->dev.of_node;
+       struct grgpio_regs __iomem *regs;

Prefer void __iomem *base;

Quoted 9 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +9 -0
+       struct gpio_chip *gc;
+       struct bgpio_chip *bgc;
+       struct grgpio_priv *priv;
+       struct resource *res;
+       int err;
+       u32 prop;
+
+       priv = devm_kzalloc(&ofdev->dev, sizeof(*priv), GFP_KERNEL);
+       if (!priv)

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt

Quoted 10 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +10 -0
+               return -ENOMEM;
+
+       res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
+       regs = devm_ioremap_resource(&ofdev->dev, res);
+       if (IS_ERR(regs))
+               return PTR_ERR(regs);
+
+       bgc = &priv->bgc;
+       err = bgpio_init(bgc, &ofdev->dev, 4, &regs->data, &regs->output, NULL,
+                        &regs->dir, NULL, BGPIOF_BIG_ENDIAN_BYTE_ORDER);

So I would prefer if you did:

Quoted 4 lines

#define GRGPIO_DATA 0x00
#define GRGPIO_OUTPUT 0x04
#define GRGPIO_DIR 0x08
(...)

Sure, I'll change it, either way works for me, although I don't see a problem with the original approach.

Quoted 19 lines

err = bgpio_init(bgc, &ofdev->dev, 4, base + GRGPIO_DATA, base + GRGPIO_OUTPUT, NULL,

                        base + GRGPIO_DIR, NULL, BGPIOF_BIG_ENDIAN_BYTE_ORDER);
Quoted 15 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +15 -0
+       if (err) {
+               dev_err(&ofdev->dev, "bgpio_init() failed\n");
+               return err;
+       }
+
+       priv->regs = regs;
+       priv->dev = &ofdev->dev;
+
+       gc = &bgc->gc;
+       gc->of_node = np;
+       gc->owner = THIS_MODULE;
+       gc->to_irq = grgpio_to_irq;
+       gc->label = np->full_name;
+
+       err = of_property_read_u32(np, "base", &prop);
Quoted 8 lines

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt

Quoted 6 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +6 -0
+       if (err) {
+               dev_dbg(&ofdev->dev, "No base property: use dynamic base\n");
+               gc->base = -1;
+       } else {
+               gc->base = prop;
+       }

Over my dead body ;-)

Removed

Quoted 11 lines

(...)

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt

Quoted 5 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +5 -0
+static struct of_device_id grgpio_match[] = {
+       {.name = "GAISLER_GPIO"},
+       {.name = "01_01a"},
+       {},
+};

This is very weird. Especially "01_01a" needs a real good explanation if it is to be kept.

Alas, I don't really know what the .name field in the of_device_id is for...

Treated above.

Thanks for the feedback!

Cheers,
Andreas Larsson

#9 lore raw

Re: [PATCH v5 2/3] gpio: grgpio: Add device driver for GRGPIO cores

Quoted 16 lines · Andreas Larson
Quoted 6 lines · Linus Walleij

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt line 10

Quoted 3 lines
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt new file +3 -0
+Required properties:
+
+- name : Should be "GAISLER_GPIO" or "01_01a"

What is this? Don't we usually use a .compatible string for this? Name? Que? Is that something legacy?

Regarding using name, this is standard for SPARC. The names in the device tree originates from the PROM.

The name field is the actually the first field checked for a match in of_match_node, followed by type then compatible. See
http://lxr.free-electrons.com/source/drivers/of/base.c#L572

(...)

Quoted 4 lines

As for the "01_01a", the LEON SPARC systems uses a plug&play to identify different IP cores in the system. When the PROM is unaware of the name of a certain core, the name field presented from the prom will be on this form. This is standard handling for LEON SPARC drivers.

Thanks for detailing this! There is such a big world out there sometimes.

I'm sort of wondering why we do things the way we do in the ARM DT world sometimes. Specifically I suspect that we should be using name or type rather than compatible for the ARM PrimeCells, which are a sort of plug-n-play type devices, but I guess it's too late to change that now...

Yours,
Linus Walleij

#10 lore raw

PATCH v5 3/3 gpio: grgpio: Add irq support

In reply to Andreas Larsson

The drivers sets up an irq domain and hands out unique virqs to irq capable gpio lines regardless of which underlying irqs maps to which gpio line.

---
 .../devicetree/bindings/gpio/gpio-grgpio.txt       |    5 +
 drivers/gpio/gpio-grgpio.c                         |  333 +++++++++++++++++++-
 2 files changed, 334 insertions(+), 4 deletions(-)
Documentation/devicetree/bindings/gpio/gpio-grgpio.txt +5 -0
index 1050dc8..e28bc3c 100644
@@ -20,5 +20,10 @@ Optional properties:
 
 - nbits : The number of gpio lines. If not present driver assumes 32 lines.
 
+- irqmap : An array with an index for each gpio line. An index is either a valid
+	index into the interrupts property array, or 0xffffffff that indicates
+	no irq for that line. Driver provides no interrupt support if not
+	present.
+
 For further information look in the documentation for the GLIB IP core library:
 http://www.gaisler.com/products/grlib/grip.pdf
drivers/gpio/gpio-grgpio.c +329 -4

390 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#11 lore raw

Re: [PATCH v5 3/3] gpio: grgpio: Add irq support

Quoted 5 lines · Andreas Larsson

The drivers sets up an irq domain and hands out unique virqs to irq capable gpio lines regardless of which underlying irqs maps to which gpio line.

(...)

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt Optional properties: · line 26

Quoted 4 lines

+- irqmap : An array with an index for each gpio line. An index is either a valid

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt +3 -0
+       index into the interrupts property array, or 0xffffffff that indicates
+       no irq for that line. Driver provides no interrupt support if not
+       present.

This looks really complicated.

Can't you just do this with a flag instead?

irq-enabled-mask = <0xf0f0f0f0>;

Like 0xf0f0f0f0 would indicate that lines 0-3, 8-11, 16-19 and 24-27 have no IRQ, lines 4-7, 12-15, 20-23 and 28-31 does have IRQ.

Then supply the resulting 16 IRQs in the interrupts property.

But you should double-check this with some more DT-aware person I guess, there may be precedents. Did you get this idea from some other binding?

drivers/gpio/gpio-grgpio.c struct grgpio_regs { · line 55

Quoted 3 lines
drivers/gpio/gpio-grgpio.c +3 -0
+struct grgpio_uirq {
+       u8 refcnt; /* Reference counter to manage requesting/freeing of uirq */
+       u8 uirq; /* Underlying virq of the gpio driver */

This looks very complicated. But well, maybe there are no other ways...

If you do this you will need some spinlock to protect the refcount, or make it an atomic type. Another way is to use <linux/kref.h> if you refactor around that.

drivers/gpio/gpio-grgpio.c struct grgpio_regs { · line 60

Quoted 5 lines
drivers/gpio/gpio-grgpio.c +5 -0
+};
+
+struct grgpio_lirq {
+       s8 index; /* Index into struct grgpio_priv's uirqs, or -1 */
+       u8 virq; /* virq for the gpio line */

Just call it irq. These are Linux irqs, they are not "virtual".

drivers/gpio/gpio-grgpio.c struct grgpio_regs { · line 73

Quoted 6 lines
drivers/gpio/gpio-grgpio.c +6 -0
+       u32 imask; /* irq mask shadow register */
+
+       /* The grgpio core can have multiple irqs. Severeal gpio lines can
+        * potentially be mapped to the same irq. This driver sets up an
+        * irq domain and hands out separate virqs to each gpio line
+        */

This is very common. Most GPIO controller have something like one IRQ line per 32 GPIO lines. It's a bit odd that it can have an arbitrary number of non-irq capable GPIOs though.

Note:

/*
* I really prefer this comment style, where ther is no text on the
* first line of the comment.
*/

drivers/gpio/gpio-grgpio.c struct grgpio_regs { · line 79

Quoted 6 lines
drivers/gpio/gpio-grgpio.c +6 -0
+       struct irq_domain *domain;
+
+       /* This array contains information on each underlying irq, each
+        * irq of the grgpio core itself.
+        */
+       struct grgpio_uirq uirqs[GRGPIO_MAX_NGPIO];

So "u" is for "underlying"?

drivers/gpio/gpio-grgpio.c struct grgpio_regs { · line 87

Quoted 8 lines
drivers/gpio/gpio-grgpio.c +8 -0
+
+       /* This array contains information for each gpio line on the
+        * virtual irqs obtains from this driver. An index value of -1 for
+        * a certain gpio line indicates that the line has no
+        * irq. Otherwise the index connects the virq to the underlying
+        * irq by pointing into the uirqs array.
+        */
+       struct grgpio_lirq lirqs[GRGPIO_MAX_NGPIO];

Hm, very complex...

Quoted 4 lines
drivers/gpio/gpio-grgpio.c +4 -0
+static void grgpio_set_imask(struct grgpio_priv *priv, unsigned int offset,
+                            int val)
+{
+       struct bgpio_chip *bgc = &priv->bgc;

drivers/gpio/gpio-grgpio.c static inline struct grgpio_priv *grgpio_gc_to_priv(struct gpio_chip *gc) · line 110

Quoted 10 lines
drivers/gpio/gpio-grgpio.c +10 -0
+       unsigned long mask = bgc->pin2mask(bgc, offset);
+       unsigned long flags;
+
+       spin_lock_irqsave(&bgc->lock, flags);
+
+       if (val)
+               priv->imask |= mask;
+       else
+               priv->imask &= ~mask;
+       bgc->write_reg(&priv->regs->imask, priv->imask);

I prefer:

#define GRGPIO_IMASK 0xnn

bgc->write_reg(priv->base + GRGPIO_IMASK, priv->imask);

(...)

drivers/gpio/gpio-grgpio.c static inline struct grgpio_priv *grgpio_gc_to_priv(struct gpio_chip *gc) · line 167

Quoted 5 lines
drivers/gpio/gpio-grgpio.c +5 -0
+       ipol = priv->bgc.read_reg(&priv->regs->ipol) & ~mask;
+       iedge = priv->bgc.read_reg(&priv->regs->iedge) & ~mask;
+
+       priv->bgc.write_reg(&priv->regs->ipol, ipol | pol);
+       priv->bgc.write_reg(&priv->regs->iedge, iedge | edge);

Dito on all these.

drivers/gpio/gpio-grgpio.c static inline struct grgpio_priv *grgpio_gc_to_priv(struct gpio_chip *gc) · line 209

Quoted 13 lines
drivers/gpio/gpio-grgpio.c +13 -0
+static irqreturn_t grgpio_irq_handler(int irq, void *dev)
+{
+       struct grgpio_priv *priv = dev;
+       int ngpio = priv->bgc.gc.ngpio;
+       int i;
+
+       for (i = 0; i < ngpio; i++) {
+               struct grgpio_lirq *lirq = &priv->lirqs[i];
+
+               if (priv->imask & BIT(i) && lirq->index >= 0 &&
+                   priv->uirqs[lirq->index].uirq == irq) {
+                       generic_handle_irq(lirq->virq);
+               }

Should there not be an else clause here handling invalid interrupts or printing an error or something?

Quoted 4 lines
drivers/gpio/gpio-grgpio.c +4 -0
+       }
+
+       return IRQ_HANDLED;
+}

(...)

Quoted 22 lines
drivers/gpio/gpio-grgpio.c +22 -0
+
+/* This function will be called as a consequence of the call to
+ * irq_create_mapping in grgpio_to_irq
+ */
+int grgpio_irq_map(struct irq_domain *d, unsigned int virq,
+                  irq_hw_number_t hwirq)
+{
+       struct grgpio_priv *priv = d->host_data;
+       struct grgpio_lirq *lirq;
+       struct grgpio_uirq *uirq;
+       unsigned long flags;
+       int offset = hwirq;
+       int ret = 0;
+
+       if (!priv)
+               return -EINVAL;
+
+       lirq = &priv->lirqs[offset];
+       if (lirq->index < 0)
+               return -EINVAL;
+
+       dev_dbg(priv->dev, "Mapping virq %d for gpio line %d\n",

drivers/gpio/gpio-grgpio.c static inline struct grgpio_priv *grgpio_gc_to_priv(struct gpio_chip *gc) · line 245

Quoted 10 lines
drivers/gpio/gpio-grgpio.c +10 -0
+               virq, offset);
+
+       spin_lock_irqsave(&priv->bgc.lock, flags);
+
+       /* Request underlying irq if not already requested */
+       lirq->virq = virq;
+       uirq = &priv->uirqs[lirq->index];
+       if (uirq->refcnt == 0) {
+               ret = request_irq(uirq->uirq, grgpio_irq_handler, 0,
+                                 dev_name(priv->dev), priv);

No, please request all present uirqs in probe() before creating the irqdomain.

And use devm_request_irq() for that.

Quoted 55 lines
drivers/gpio/gpio-grgpio.c +55 -0
+               if (ret) {
+                       dev_err(priv->dev,
+                               "Could not request underlying irq %d\n",
+                               uirq->uirq);
+
+                       spin_unlock_irqrestore(&priv->bgc.lock, flags);
+
+                       return ret;
+               }
+       }
+       uirq->refcnt++;
+
+       spin_unlock_irqrestore(&priv->bgc.lock, flags);
+
+       /* Setup virq  */
+       irq_set_chip_data(virq, priv);
+       irq_set_chip_and_handler(virq, &grgpio_irq_chip,
+                                handle_simple_irq);
+       irq_clear_status_flags(virq, IRQ_NOREQUEST);
+#ifdef CONFIG_ARM
+       set_irq_flags(virq, IRQF_VALID);
+#else
+       irq_set_noprobe(virq);
+#endif
+
+       return ret;
+}
+
+void grgpio_irq_unmap(struct irq_domain *d, unsigned int virq)
+{
+       struct grgpio_priv *priv = d->host_data;
+       int index;
+       struct grgpio_lirq *lirq;
+       struct grgpio_uirq *uirq;
+       unsigned long flags;
+       int ngpio = priv->bgc.gc.ngpio;
+       int i;
+
+#ifdef CONFIG_ARM
+       set_irq_flags(virq, 0);
+#endif
+       irq_set_chip_and_handler(virq, NULL, NULL);
+       irq_set_chip_data(virq, NULL);
+
+       spin_lock_irqsave(&priv->bgc.lock, flags);
+
+       /* Free underlying irq if last user unmapped */
+       index = -1;
+       for (i = 0; i < ngpio; i++) {
+               lirq = &priv->lirqs[i];
+               if (lirq->virq == virq) {
+                       grgpio_set_imask(priv, i, 0);
+                       lirq->virq = 0;
+                       index = lirq->index;
+                       break;

drivers/gpio/gpio-grgpio.c static inline struct grgpio_priv *grgpio_gc_to_priv(struct gpio_chip *gc) · line 310

Quoted 10 lines
drivers/gpio/gpio-grgpio.c +10 -0
+               }
+       }
+       WARN_ON(index < 0);
+
+       if (index >= 0) {
+               uirq = &priv->uirqs[lirq->index];
+               uirq->refcnt--;
+               if (uirq->refcnt == 0)
+                       free_irq(uirq->uirq, priv);
+       }

Don't do that either. devm_request_irq() will free the IRQ when the driver unloads. No need for messy code trying to cover it up.

Quoted 36 lines
drivers/gpio/gpio-grgpio.c +13 -0
+       spin_unlock_irqrestore(&priv->bgc.lock, flags);
 }
 
+static struct irq_domain_ops grgpio_irq_domain_ops = {
+       .map    = grgpio_irq_map,
+       .unmap  = grgpio_irq_unmap,
+};
+
+/* ------------------------------------------------------------ */
+
 static int grgpio_probe(struct platform_device *ofdev)
 {
        struct device_node *np = ofdev->dev.of_node;
@@ -75,6 +329,9 @@ static int grgpio_probe(struct platform_device *ofdev)
        struct resource *res;
        int err;
        u32 prop;
+       s32 *irqmap;
+       int size;
+       int i;
 
        priv = devm_kzalloc(&ofdev->dev, sizeof(*priv), GFP_KERNEL);
        if (!priv)
@@ -94,6 +351,7 @@ static int grgpio_probe(struct platform_device *ofdev)
        }
 
        priv->regs = regs;
+       priv->imask = bgc->read_reg(&regs->imask);
        priv->dev = &ofdev->dev;
 
        gc = &bgc->gc;
@@ -119,6 +377,49 @@ static int grgpio_probe(struct platform_device *ofdev)
                gc->ngpio = prop;
        }
 
+       /* The irqmap contains the index values indicating which underlying irq,

drivers/gpio/gpio-grgpio.c static int grgpio_probe(struct platform_device *ofdev) · line 390

Quoted 10 lines
drivers/gpio/gpio-grgpio.c +10 -0
+        * if anyone, is connected to that line
+        */
+       irqmap = (s32 *)of_get_property(np, "irqmap", &size);
+       if (irqmap) {
+               if (size < gc->ngpio) {
+                       dev_err(&ofdev->dev,
+                               "irqmap shorter than ngpio (%d < %d)\n",
+                               size, gc->ngpio);
+                       return -EINVAL;
+               }

So devm_request all uirqs somehere like here.

drivers/gpio/gpio-grgpio.c static int grgpio_probe(struct platform_device *ofdev) · line 394

Quoted 3 lines
drivers/gpio/gpio-grgpio.c +3 -0
+               priv->domain = irq_domain_add_linear(np, gc->ngpio,
+                                                    &grgpio_irq_domain_ops,
+                                                    priv);

Then move this below the loop, so you know you have all uirqs when you create the domain.

Quoted 32 lines
drivers/gpio/gpio-grgpio.c +28 -0
+               if (!priv->domain) {
+                       dev_err(&ofdev->dev, "Could not add irq domain\n");
+                       return -EINVAL;
+               }
+
+               for (i = 0; i < gc->ngpio; i++) {
+                       struct grgpio_lirq *lirq;
+                       int ret;
+
+                       lirq = &priv->lirqs[i];
+                       lirq->index = irqmap[i];
+
+                       if (lirq->index < 0)
+                               continue;
+
+                       ret = platform_get_irq(ofdev, lirq->index);
+                       if (ret <= 0) {
+                               /* Continue without irq functionality for that
+                                * gpio line
+                                */
+                               dev_err(priv->dev,
+                                       "Failed to get irq for offset %d\n", i);
+                               continue;
+                       }
+                       priv->uirqs[lirq->index].uirq = ret;
+               }
+       }
+
        platform_set_drvdata(ofdev, priv);
 
        err = gpiochip_add(gc);

drivers/gpio/gpio-grgpio.c static int grgpio_probe(struct platform_device *ofdev) · line 435

Quoted 10 lines
drivers/gpio/gpio-grgpio.c +2 -2
@@ -127,8 +428,8 @@ static int grgpio_probe(struct platform_device *ofdev)
                return err;
        }
 
-       dev_info(&ofdev->dev, "regs=0x%p, base=%d, ngpio=%d\n",
-                priv->regs, gc->base, gc->ngpio);
+       dev_info(&ofdev->dev, "regs=0x%p, base=%d, ngpio=%d, irqs=%s\n",
+                priv->regs, gc->base, gc->ngpio, priv->domain ? "on" : "off");
 
        return 0;
 }

(...)

Yours,
Linus Walleij

#12 lore raw

Re: [PATCH v5 3/3] gpio: grgpio: Add irq support

Quoted 28 lines · Linus Walleij
Quoted 5 lines · Andreas Larsson

The drivers sets up an irq domain and hands out unique virqs to irq capable gpio lines regardless of which underlying irqs maps to which gpio line.

(...)

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt Optional properties: · line 26

Quoted 4 lines

+- irqmap : An array with an index for each gpio line. An index is either a valid

Documentation/devicetree/bindings/gpio/gpio-grgpio.txt +3 -0
+       index into the interrupts property array, or 0xffffffff that indicates
+       no irq for that line. Driver provides no interrupt support if not
+       present.

This looks really complicated.

Can't you just do this with a flag instead?

irq-enabled-mask = <0xf0f0f0f0>;

Like 0xf0f0f0f0 would indicate that lines 0-3, 8-11, 16-19 and 24-27 have no IRQ, lines 4-7, 12-15, 20-23 and 28-31 does have IRQ.

Then supply the resulting 16 IRQs in the interrupts property.

But you should double-check this with some more DT-aware person I guess, there may be precedents. Did you get this idea from some other binding?

Quoted 6 lines

drivers/gpio/gpio-grgpio.c struct grgpio_regs { · line 55

Quoted 3 lines
drivers/gpio/gpio-grgpio.c +3 -0
+struct grgpio_uirq {
+       u8 refcnt; /* Reference counter to manage requesting/freeing of uirq */
+       u8 uirq; /* Underlying virq of the gpio driver */

This looks very complicated. But well, maybe there are no other ways...

The reason for the complex handling is that any line can any one irq of the irqs of the gpio core or no irq at all - completely independenly of each other.

E.g. we can have a a system where:
- line 0 has no irq
- line 1 has irq 7
- line 2 has irq 9
- line 3 has irq 7
- line 4 has no irq
- line 5 has irq 4
- etc.

That is the reason why the "imap" of property (originating from the PROM) contains an index that maps a line to one of the irqs of the core or indicates no irq at all.

Quoted 3 lines

If you do this you will need some spinlock to protect the refcount, or make it an atomic type. Another way is to use <linux/kref.h> if you refactor around that.

Yes, I use the bgpio-lock for that but I missed using it in the irq handler - added.

Quoted 8 lines

drivers/gpio/gpio-grgpio.c struct grgpio_regs { · line 60

Quoted 6 lines
drivers/gpio/gpio-grgpio.c +5 -0
+};
+
+struct grgpio_lirq {
+       s8 index; /* Index into struct grgpio_priv's uirqs, or -1 */
+       u8 virq; /* virq for the gpio line */

Just call it irq. These are Linux irqs, they are not "virtual".

Sure

Quoted 12 lines

drivers/gpio/gpio-grgpio.c struct grgpio_regs { · line 73

Quoted 7 lines
drivers/gpio/gpio-grgpio.c +6 -0
+       u32 imask; /* irq mask shadow register */
+
+       /* The grgpio core can have multiple irqs. Severeal gpio lines can
+        * potentially be mapped to the same irq. This driver sets up an
+        * irq domain and hands out separate virqs to each gpio line
+        */

This is very common. Most GPIO controller have something like one IRQ line per 32 GPIO lines. It's a bit odd that it can have an arbitrary number of non-irq capable GPIOs though.

Quoted 6 lines

Note:

/*
* I really prefer this comment style, where ther is no text on the
* first line of the comment.
*/

Sure

Quoted 8 lines

drivers/gpio/gpio-grgpio.c struct grgpio_regs { · line 79

Quoted 6 lines
drivers/gpio/gpio-grgpio.c +6 -0
+       struct irq_domain *domain;
+
+       /* This array contains information on each underlying irq, each
+        * irq of the grgpio core itself.
+        */
+       struct grgpio_uirq uirqs[GRGPIO_MAX_NGPIO];

So "u" is for "underlying"?

Yes

Quoted 42 lines

drivers/gpio/gpio-grgpio.c struct grgpio_regs { · line 87

Quoted 8 lines
drivers/gpio/gpio-grgpio.c +8 -0
+
+       /* This array contains information for each gpio line on the
+        * virtual irqs obtains from this driver. An index value of -1 for
+        * a certain gpio line indicates that the line has no
+        * irq. Otherwise the index connects the virq to the underlying
+        * irq by pointing into the uirqs array.
+        */
+       struct grgpio_lirq lirqs[GRGPIO_MAX_NGPIO];

Hm, very complex...

Quoted 4 lines
drivers/gpio/gpio-grgpio.c +4 -0
+static void grgpio_set_imask(struct grgpio_priv *priv, unsigned int offset,
+                            int val)
+{
+       struct bgpio_chip *bgc = &priv->bgc;

drivers/gpio/gpio-grgpio.c static inline struct grgpio_priv *grgpio_gc_to_priv(struct gpio_chip *gc) · line 110

Quoted 10 lines
drivers/gpio/gpio-grgpio.c +10 -0
+       unsigned long mask = bgc->pin2mask(bgc, offset);
+       unsigned long flags;
+
+       spin_lock_irqsave(&bgc->lock, flags);
+
+       if (val)
+               priv->imask |= mask;
+       else
+               priv->imask &= ~mask;
+       bgc->write_reg(&priv->regs->imask, priv->imask);

I prefer:

#define GRGPIO_IMASK 0xnn

bgc->write_reg(priv->base + GRGPIO_IMASK, priv->imask);

(...)

drivers/gpio/gpio-grgpio.c static inline struct grgpio_priv *grgpio_gc_to_priv(struct gpio_chip *gc) · line 167

Quoted 5 lines
drivers/gpio/gpio-grgpio.c +5 -0
+       ipol = priv->bgc.read_reg(&priv->regs->ipol) & ~mask;
+       iedge = priv->bgc.read_reg(&priv->regs->iedge) & ~mask;
+
+       priv->bgc.write_reg(&priv->regs->ipol, ipol | pol);
+       priv->bgc.write_reg(&priv->regs->iedge, iedge | edge);

Dito on all these.

Quoted 16 lines

drivers/gpio/gpio-grgpio.c static inline struct grgpio_priv *grgpio_gc_to_priv(struct gpio_chip *gc) · line 209

Quoted 13 lines
drivers/gpio/gpio-grgpio.c +13 -0
+static irqreturn_t grgpio_irq_handler(int irq, void *dev)
+{
+       struct grgpio_priv *priv = dev;
+       int ngpio = priv->bgc.gc.ngpio;
+       int i;
+
+       for (i = 0; i < ngpio; i++) {
+               struct grgpio_lirq *lirq = &priv->lirqs[i];
+
+               if (priv->imask & BIT(i) && lirq->index >= 0 &&
+                   priv->uirqs[lirq->index].uirq == irq) {
+                       generic_handle_irq(lirq->virq);
+               }

Should there not be an else clause here handling invalid interrupts or printing an error or something?

Not an else clause per se, as we need to loop through all the lines and only treat the ones that should be triggered by the given underlying irq. I'll add a comment on what it is all about.

On the other hand, if no line whatsoever matches it might be good to add a warning about it. I am not sure if it could legitimately happen on an SMP system if the irq is unmapped at one CPU at the same time as another after another CPU has entered this interrupt handler but before taking the lock protecting the uirq structure.

Quoted 36 lines
Quoted 5 lines
drivers/gpio/gpio-grgpio.c +4 -0
+       }
+
+       return IRQ_HANDLED;
+}

(...)

Quoted 28 lines
drivers/gpio/gpio-grgpio.c +28 -0
+
+/* This function will be called as a consequence of the call to
+ * irq_create_mapping in grgpio_to_irq
+ */
+int grgpio_irq_map(struct irq_domain *d, unsigned int virq,
+                  irq_hw_number_t hwirq)
+{
+       struct grgpio_priv *priv = d->host_data;
+       struct grgpio_lirq *lirq;
+       struct grgpio_uirq *uirq;
+       unsigned long flags;
+       int offset = hwirq;
+       int ret = 0;
+
+       if (!priv)
+               return -EINVAL;
+
+       lirq = &priv->lirqs[offset];
+       if (lirq->index < 0)
+               return -EINVAL;
+
+       dev_dbg(priv->dev, "Mapping virq %d for gpio line %d\n",
+               virq, offset);
+
+       spin_lock_irqsave(&priv->bgc.lock, flags);
+
+       /* Request underlying irq if not already requested */
+       lirq->virq = virq;
Quoted 8 lines

drivers/gpio/gpio-grgpio.c static inline struct grgpio_priv *grgpio_gc_to_priv(struct gpio_chip *gc) · line 245

Quoted 4 lines
drivers/gpio/gpio-grgpio.c +4 -0
+       uirq = &priv->uirqs[lirq->index];
+       if (uirq->refcnt == 0) {
+               ret = request_irq(uirq->uirq, grgpio_irq_handler, 0,
+                                 dev_name(priv->dev), priv);

No, please request all present uirqs in probe() before creating the irqdomain.

The reason for doing it at irq_map and not in probe is that one typical hardware setup for this core is that it has irqs that are shared with pretty much all other cores in the system. Therefore, if this drivers requests all its irqs in the probe function, pretty much all the drivers for the other cores in the system that were not lucky enough to request their irq before this driver will fail their probes in such a hardware setup. This driver can not share irq:s so even if the other drivers can handle shared irqs they are out of luck if this driver is probed first.

That is why only the irqs that are actually used should be requested and thus why the driver does it on demand instead of in the probe.

Quoted 165 lines

And use devm_request_irq() for that.

Quoted 55 lines
drivers/gpio/gpio-grgpio.c +55 -0
+               if (ret) {
+                       dev_err(priv->dev,
+                               "Could not request underlying irq %d\n",
+                               uirq->uirq);
+
+                       spin_unlock_irqrestore(&priv->bgc.lock, flags);
+
+                       return ret;
+               }
+       }
+       uirq->refcnt++;
+
+       spin_unlock_irqrestore(&priv->bgc.lock, flags);
+
+       /* Setup virq  */
+       irq_set_chip_data(virq, priv);
+       irq_set_chip_and_handler(virq, &grgpio_irq_chip,
+                                handle_simple_irq);
+       irq_clear_status_flags(virq, IRQ_NOREQUEST);
+#ifdef CONFIG_ARM
+       set_irq_flags(virq, IRQF_VALID);
+#else
+       irq_set_noprobe(virq);
+#endif
+
+       return ret;
+}
+
+void grgpio_irq_unmap(struct irq_domain *d, unsigned int virq)
+{
+       struct grgpio_priv *priv = d->host_data;
+       int index;
+       struct grgpio_lirq *lirq;
+       struct grgpio_uirq *uirq;
+       unsigned long flags;
+       int ngpio = priv->bgc.gc.ngpio;
+       int i;
+
+#ifdef CONFIG_ARM
+       set_irq_flags(virq, 0);
+#endif
+       irq_set_chip_and_handler(virq, NULL, NULL);
+       irq_set_chip_data(virq, NULL);
+
+       spin_lock_irqsave(&priv->bgc.lock, flags);
+
+       /* Free underlying irq if last user unmapped */
+       index = -1;
+       for (i = 0; i < ngpio; i++) {
+               lirq = &priv->lirqs[i];
+               if (lirq->virq == virq) {
+                       grgpio_set_imask(priv, i, 0);
+                       lirq->virq = 0;
+                       index = lirq->index;
+                       break;

drivers/gpio/gpio-grgpio.c static inline struct grgpio_priv *grgpio_gc_to_priv(struct gpio_chip *gc) · line 310

Quoted 10 lines
drivers/gpio/gpio-grgpio.c +10 -0
+               }
+       }
+       WARN_ON(index < 0);
+
+       if (index >= 0) {
+               uirq = &priv->uirqs[lirq->index];
+               uirq->refcnt--;
+               if (uirq->refcnt == 0)
+                       free_irq(uirq->uirq, priv);
+       }

Don't do that either. devm_request_irq() will free the IRQ when the driver unloads. No need for messy code trying to cover it up.

Quoted 36 lines
drivers/gpio/gpio-grgpio.c +13 -0
+       spin_unlock_irqrestore(&priv->bgc.lock, flags);
  }
 
+static struct irq_domain_ops grgpio_irq_domain_ops = {
+       .map    = grgpio_irq_map,
+       .unmap  = grgpio_irq_unmap,
+};
+
+/* ------------------------------------------------------------ */
+
  static int grgpio_probe(struct platform_device *ofdev)
  {
         struct device_node *np = ofdev->dev.of_node;
@@ -75,6 +329,9 @@ static int grgpio_probe(struct platform_device *ofdev)
         struct resource *res;
         int err;
         u32 prop;
+       s32 *irqmap;
+       int size;
+       int i;
 
         priv = devm_kzalloc(&ofdev->dev, sizeof(*priv), GFP_KERNEL);
         if (!priv)
@@ -94,6 +351,7 @@ static int grgpio_probe(struct platform_device *ofdev)
         }
 
         priv->regs = regs;
+       priv->imask = bgc->read_reg(&regs->imask);
         priv->dev = &ofdev->dev;
 
         gc = &bgc->gc;
@@ -119,6 +377,49 @@ static int grgpio_probe(struct platform_device *ofdev)
                 gc->ngpio = prop;
         }
 
+       /* The irqmap contains the index values indicating which underlying irq,

drivers/gpio/gpio-grgpio.c static int grgpio_probe(struct platform_device *ofdev) · line 390

Quoted 10 lines
drivers/gpio/gpio-grgpio.c +10 -0
+        * if anyone, is connected to that line
+        */
+       irqmap = (s32 *)of_get_property(np, "irqmap", &size);
+       if (irqmap) {
+               if (size < gc->ngpio) {
+                       dev_err(&ofdev->dev,
+                               "irqmap shorter than ngpio (%d < %d)\n",
+                               size, gc->ngpio);
+                       return -EINVAL;
+               }

So devm_request all uirqs somehere like here.

drivers/gpio/gpio-grgpio.c static int grgpio_probe(struct platform_device *ofdev) · line 394

Quoted 3 lines
drivers/gpio/gpio-grgpio.c +3 -0
+               priv->domain = irq_domain_add_linear(np, gc->ngpio,
+                                                    &grgpio_irq_domain_ops,
+                                                    priv);

Then move this below the loop, so you know you have all uirqs when you create the domain.

Quoted 35 lines
drivers/gpio/gpio-grgpio.c +28 -0
+               if (!priv->domain) {
+                       dev_err(&ofdev->dev, "Could not add irq domain\n");
+                       return -EINVAL;
+               }
+
+               for (i = 0; i < gc->ngpio; i++) {
+                       struct grgpio_lirq *lirq;
+                       int ret;
+
+                       lirq = &priv->lirqs[i];
+                       lirq->index = irqmap[i];
+
+                       if (lirq->index < 0)
+                               continue;
+
+                       ret = platform_get_irq(ofdev, lirq->index);
+                       if (ret <= 0) {
+                               /* Continue without irq functionality for that
+                                * gpio line
+                                */
+                               dev_err(priv->dev,
+                                       "Failed to get irq for offset %d\n", i);
+                               continue;
+                       }
+                       priv->uirqs[lirq->index].uirq = ret;
+               }
+       }
+
         platform_set_drvdata(ofdev, priv);
 
         err = gpiochip_add(gc);
@@ -127,8 +428,8 @@ static int grgpio_probe(struct platform_device *ofdev)
                 return err;
         }
 
Quoted 8 lines
Quoted 7 lines
drivers/gpio/gpio-grgpio.c +2 -2
-       dev_info(&ofdev->dev, "regs=0x%p, base=%d, ngpio=%d\n",
-                priv->regs, gc->base, gc->ngpio);
+       dev_info(&ofdev->dev, "regs=0x%p, base=%d, ngpio=%d, irqs=%s\n",
+                priv->regs, gc->base, gc->ngpio, priv->domain ? "on" : "off");
 
         return 0;
  }

(...)

Thanks for the feedback!

Cheers,
Andreas Larsson

#13 lore raw

Re: [PATCH v5 3/3] gpio: grgpio: Add irq support

Quoted 2 lines · Andreas Larson
Quoted 1 line · Linus Walleij
Quoted 1 line

+int grgpio_irq_map(struct irq_domain *d, unsigned int virq,

(...)

Quoted 22 lines
Quoted 10 lines

drivers/gpio/gpio-grgpio.c static inline struct grgpio_priv *grgpio_gc_to_priv(struct gpio_chip *gc) · line 245

Quoted 6 lines
drivers/gpio/gpio-grgpio.c +6 -0
+       /* Request underlying irq if not already requested */
+       lirq->virq = virq;
+       uirq = &priv->uirqs[lirq->index];
+       if (uirq->refcnt == 0) {
+               ret = request_irq(uirq->uirq, grgpio_irq_handler, 0,
+                                 dev_name(priv->dev), priv);

No, please request all present uirqs in probe() before creating the irqdomain.

The reason for doing it at irq_map and not in probe is that one typical hardware setup for this core is that it has irqs that are shared with pretty much all other cores in the system. Therefore, if this drivers requests all its irqs in the probe function, pretty much all the drivers for the other cores in the system that were not lucky enough to request their irq before this driver will fail their probes in such a hardware setup. This driver can not share irq:s so even if the other drivers can handle shared irqs they are out of luck if this driver is probed first.

Quoted 2 lines

That is why only the irqs that are actually used should be requested and thus why the driver does it on demand instead of in the probe.

OK I buy that explanation...

Thanks,
Linus Walleij