Skip to contentSkip to conversation

bcma: gpio: add own IRQ domain

PATCH Superseded Rafał Miłecki 4 participants 20 messages Nov 29, 2013 – Jan 2, 2014

Subsystems (3)
lore
Share & tools
lore

Rafał Miłecki

author maintainer
#0 lore raw

PATCH v2 1/2 bcma: gpio: add own IRQ domain

Input GPIO changes can generate interrupts, but we need kind of ACK for them by changing IRQ polarity. This is required to stop hardware from keep generating interrupts and generate another one on the next GPIO state change.
This code allows using GPIOs with standard interrupts and add for example GPIO buttons support.

---
 drivers/bcma/Kconfig                        |    1 +
 drivers/bcma/driver_gpio.c                  |   92 ++++++++++++++++++++++++++-
 include/linux/bcma/bcma_driver_chipcommon.h |    1 +
 3 files changed, 92 insertions(+), 2 deletions(-)
drivers/bcma/Kconfig +1 -0
index 7c081b3..649568e 100644
@@ -75,6 +75,7 @@ config BCMA_DRIVER_GMAC_CMN
 config BCMA_DRIVER_GPIO
 	bool "BCMA GPIO driver"
 	depends on BCMA && GPIOLIB
+	select IRQ_DOMAIN
 	help
 	  Driver to provide access to the GPIO pins of the bcma bus.
 
drivers/bcma/driver_gpio.c +90 -2

119 lines not on this page · raw message

include/linux/bcma/bcma_driver_chipcommon.h +1 -0
index c49e1a1..63d105c 100644
@@ -640,6 +640,7 @@ struct bcma_drv_cc {
 	spinlock_t gpio_lock;
 #ifdef CONFIG_BCMA_DRIVER_GPIO
 	struct gpio_chip gpio;
+	struct irq_domain *irq_domain;
 #endif
 };
 
Signature · 1 line
1.7.10.4
author maintainer
#1 lore raw

PATCH v2 2/2 MIPS: BCM47XX: Prepare support for GPIO buttons

So far this adds support for one Netgear model only, but it's designed and ready to add many more device. We could hopefully import database from OpenWrt.
Support for SSB is currently disabled, because SSB doesn't implement IRQ domain yet.

---
 arch/mips/bcm47xx/Makefile          |    2 +-
 arch/mips/bcm47xx/bcm47xx_private.h |    3 ++
 arch/mips/bcm47xx/buttons.c         |   81 +++++++++++++++++++++++++++++++++++
 arch/mips/bcm47xx/setup.c           |    1 +
 4 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 arch/mips/bcm47xx/buttons.c
arch/mips/bcm47xx/Makefile +1 -1
index 84e9aed..006a05e 100644
@@ -4,5 +4,5 @@
 #
 
 obj-y				+= irq.o nvram.o prom.o serial.o setup.o time.o sprom.o
-obj-y				+= board.o leds.o
+obj-y				+= board.o buttons.o leds.o
 obj-$(CONFIG_BCM47XX_SSB)	+= wgt634u.o
arch/mips/bcm47xx/bcm47xx_private.h +3 -0
index 1a1e600..5c94ace 100644
@@ -3,6 +3,9 @@
 
 #include <linux/kernel.h>
 
+/* buttons.c */
+int __init bcm47xx_buttons_register(void);
+
 /* leds.c */
 void __init bcm47xx_leds_register(void);
 
arch/mips/bcm47xx/buttons.c new file +81 -0

81 lines not on this page · raw message

arch/mips/bcm47xx/setup.c +1 -0
index 7e61c0b..a791124 100644
@@ -242,6 +242,7 @@ static int __init bcm47xx_register_bus_complete(void)
 #endif
 	}
 
+	bcm47xx_buttons_register();
 	bcm47xx_leds_register();
 
 	return 0;
Signature · 1 line
1.7.10.4
maintainer
#2 lore raw

Re: [PATCH V2 2/2] MIPS: BCM47XX: Prepare support for GPIO buttons

Quoted 46 lines · Rafał Miłecki

So far this adds support for one Netgear model only, but it's designed and ready to add many more device. We could hopefully import database from OpenWrt.
Support for SSB is currently disabled, because SSB doesn't implement IRQ domain yet.

---
 arch/mips/bcm47xx/Makefile          |    2 +-
 arch/mips/bcm47xx/bcm47xx_private.h |    3 ++
 arch/mips/bcm47xx/buttons.c         |   81 +++++++++++++++++++++++++++++++++++
 arch/mips/bcm47xx/setup.c           |    1 +
 4 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 arch/mips/bcm47xx/buttons.c
arch/mips/bcm47xx/Makefile +1 -1
index 84e9aed..006a05e 100644
@@ -4,5 +4,5 @@
 #
 
 obj-y				+= irq.o nvram.o prom.o serial.o setup.o time.o sprom.o
-obj-y				+= board.o leds.o
+obj-y				+= board.o buttons.o leds.o
 obj-$(CONFIG_BCM47XX_SSB)	+= wgt634u.o
arch/mips/bcm47xx/bcm47xx_private.h +3 -0
index 1a1e600..5c94ace 100644
@@ -3,6 +3,9 @@
 
 #include <linux/kernel.h>
 
+/* buttons.c */
+int __init bcm47xx_buttons_register(void);
+
 /* leds.c */
 void __init bcm47xx_leds_register(void);
 
arch/mips/bcm47xx/buttons.c new file +0 -0
new file mode 100644
index 0000000..ecd6b1b

arch/mips/bcm47xx/buttons.c line 10

Quoted 10 lines
arch/mips/bcm47xx/buttons.c new file +10 -0
@@ -0,0 +1,81 @@
+#include "bcm47xx_private.h"
+
+#include <linux/input.h>
+#include <linux/gpio_keys.h>
+#include <linux/interrupt.h>
+#include <linux/ssb/ssb_embedded.h>
+#include <bcm47xx_board.h>
+#include <bcm47xx.h>
+
+struct input_dev *input;

This is unused please remove it.

arch/mips/bcm47xx/buttons.c line 17

Quoted 7 lines
arch/mips/bcm47xx/buttons.c new file +7 -0
+
+/**************************************************
+ * Database
+ **************************************************/
+
+static struct gpio_keys_button
+bcm47xx_buttons_netgear_wndr4500_v1[] = {

Could you make this __initconst so it will be freed after the kernel booted. Then you have to make sure it gets copied into a different non init memory region if it will be used after the kernel booted. This should scale to ~100 devices so this memory gets significant.

Quoted 77 lines
arch/mips/bcm47xx/buttons.c new file +64 -0
+	{
+		.code		= KEY_WPS_BUTTON,
+		.gpio		= 4,
+		.active_low	= 1,
+	},
+	{
+		.code		= KEY_RFKILL,
+		.gpio		= 5,
+		.active_low	= 1,
+	},
+	{
+		.code		= KEY_RESTART,
+		.gpio		= 6,
+		.active_low	= 1,
+	},
+};
+
+/**************************************************
+ * Init
+ **************************************************/
+
+static struct gpio_keys_platform_data bcm47xx_button_pdata;
+
+static struct platform_device bcm47xx_buttons_gpio_keys = {
+	.name = "gpio-keys",
+	.dev = {
+		.platform_data = &bcm47xx_button_pdata,
+	}
+};
+
+#define bcm47xx_set_bdata(dev_buttons) do {				\
+	bcm47xx_button_pdata.buttons = dev_buttons;			\
+	bcm47xx_button_pdata.nbuttons = ARRAY_SIZE(dev_buttons);	\
+} while (0)
+
+int __init bcm47xx_buttons_register(void)
+{
+	enum bcm47xx_board board = bcm47xx_board_get();
+	int err;
+
+#ifdef CONFIG_BCM47XX_SSB
+	if (bcm47xx_bus_type == BCM47XX_BUS_TYPE_SSB) {
+		pr_debug("Buttons on SSB are not supported yet.\n");
+		return -ENOTSUPP;
+	}
+#endif
+
+	switch (board) {
+	case BCM47XX_BOARD_NETGEAR_WNDR4500V1:
+		bcm47xx_set_bdata(bcm47xx_buttons_netgear_wndr4500_v1);
+		break;
+	default:
+		pr_debug("No buttons configuration found for this device\n");
+		return -ENOTSUPP;
+	}
+
+	err = platform_device_register(&bcm47xx_buttons_gpio_keys);
+	if (err) {
+		pr_err("Failed to register platform device: %d\n", err);
+		return err;
+	}
+
+	return 0;
+}
arch/mips/bcm47xx/setup.c +1 -0
index 7e61c0b..a791124 100644
@@ -242,6 +242,7 @@ static int __init bcm47xx_register_bus_complete(void)
 #endif
 	}
 
+	bcm47xx_buttons_register();
 	bcm47xx_leds_register();
 
 	return 0;
author maintainer
#3 lore raw

PATCH v3 2/2 MIPS: BCM47XX: Prepare support for GPIO buttons

In reply to Rafał Miłecki

So far this adds support for one Netgear model only, but it's designed and ready to add many more device. We could hopefully import database from OpenWrt.
Support for SSB is currently disabled, because SSB doesn't implement IRQ domain yet.

---
V3: Use __initconst and copy data for detected device. This will allow
us to free some memory after init.

---
 arch/mips/bcm47xx/Makefile          |    2 +-
 arch/mips/bcm47xx/bcm47xx_private.h |    3 ++
 arch/mips/bcm47xx/buttons.c         |   95 +++++++++++++++++++++++++++++++++++
 arch/mips/bcm47xx/setup.c           |    1 +
 4 files changed, 100 insertions(+), 1 deletion(-)
 create mode 100644 arch/mips/bcm47xx/buttons.c
arch/mips/bcm47xx/Makefile +1 -1
index 84e9aed..006a05e 100644
@@ -4,5 +4,5 @@
 #
 
 obj-y				+= irq.o nvram.o prom.o serial.o setup.o time.o sprom.o
-obj-y				+= board.o leds.o
+obj-y				+= board.o buttons.o leds.o
 obj-$(CONFIG_BCM47XX_SSB)	+= wgt634u.o
arch/mips/bcm47xx/bcm47xx_private.h +3 -0
index 1a1e600..5c94ace 100644
@@ -3,6 +3,9 @@
 
 #include <linux/kernel.h>
 
+/* buttons.c */
+int __init bcm47xx_buttons_register(void);
+
 /* leds.c */
 void __init bcm47xx_leds_register(void);
 
arch/mips/bcm47xx/buttons.c new file +95 -0

95 lines not on this page · raw message

arch/mips/bcm47xx/setup.c +1 -0
index 7e61c0b..a791124 100644
@@ -242,6 +242,7 @@ static int __init bcm47xx_register_bus_complete(void)
 #endif
 	}
 
+	bcm47xx_buttons_register();
 	bcm47xx_leds_register();
 
 	return 0;
Signature · 1 line
1.7.10.4
maintainer Acked-by on v3 2/2
#4 lore raw

Re: [PATCH V3 2/2] MIPS: BCM47XX: Prepare support for GPIO buttons

Quoted 7 lines · Rafał Miłecki

So far this adds support for one Netgear model only, but it's designed and ready to add many more device. We could hopefully import database from OpenWrt.
Support for SSB is currently disabled, because SSB doesn't implement IRQ domain yet.

Quoted 150 lines

---
V3: Use __initconst and copy data for detected device. This will allow
us to free some memory after init.

---
 arch/mips/bcm47xx/Makefile          |    2 +-
 arch/mips/bcm47xx/bcm47xx_private.h |    3 ++
 arch/mips/bcm47xx/buttons.c         |   95 +++++++++++++++++++++++++++++++++++
 arch/mips/bcm47xx/setup.c           |    1 +
 4 files changed, 100 insertions(+), 1 deletion(-)
 create mode 100644 arch/mips/bcm47xx/buttons.c
arch/mips/bcm47xx/Makefile +1 -1
index 84e9aed..006a05e 100644
@@ -4,5 +4,5 @@
 #
 
 obj-y				+= irq.o nvram.o prom.o serial.o setup.o time.o sprom.o
-obj-y				+= board.o leds.o
+obj-y				+= board.o buttons.o leds.o
 obj-$(CONFIG_BCM47XX_SSB)	+= wgt634u.o
arch/mips/bcm47xx/bcm47xx_private.h +3 -0
index 1a1e600..5c94ace 100644
@@ -3,6 +3,9 @@
 
 #include <linux/kernel.h>
 
+/* buttons.c */
+int __init bcm47xx_buttons_register(void);
+
 /* leds.c */
 void __init bcm47xx_leds_register(void);
 
arch/mips/bcm47xx/buttons.c new file +95 -0
new file mode 100644
index 0000000..3138f03
@@ -0,0 +1,95 @@
+#include "bcm47xx_private.h"
+
+#include <linux/input.h>
+#include <linux/gpio_keys.h>
+#include <linux/interrupt.h>
+#include <linux/ssb/ssb_embedded.h>
+#include <bcm47xx_board.h>
+#include <bcm47xx.h>
+
+/**************************************************
+ * Database
+ **************************************************/
+
+static struct gpio_keys_button
+bcm47xx_buttons_netgear_wndr4500_v1[] __initconst = {
+	{
+		.code		= KEY_WPS_BUTTON,
+		.gpio		= 4,
+		.active_low	= 1,
+	},
+	{
+		.code		= KEY_RFKILL,
+		.gpio		= 5,
+		.active_low	= 1,
+	},
+	{
+		.code		= KEY_RESTART,
+		.gpio		= 6,
+		.active_low	= 1,
+	},
+};
+
+/**************************************************
+ * Init
+ **************************************************/
+
+static struct gpio_keys_platform_data bcm47xx_button_pdata;
+
+static struct platform_device bcm47xx_buttons_gpio_keys = {
+	.name = "gpio-keys",
+	.dev = {
+		.platform_data = &bcm47xx_button_pdata,
+	}
+};
+
+/* Copy data from __initconst */
+static int __init bcm47xx_buttons_copy(struct gpio_keys_button *buttons,
+					size_t nbuttons)
+{
+	size_t size = nbuttons * sizeof(*buttons);
+
+	bcm47xx_button_pdata.buttons = kmalloc(size, GFP_KERNEL);
+	if (!bcm47xx_button_pdata.buttons)
+		return -ENOMEM;
+	memcpy(bcm47xx_button_pdata.buttons, buttons, size);
+	bcm47xx_button_pdata.nbuttons = nbuttons;
+
+	return 0;
+}
+
+#define bcm47xx_copy_bdata(dev_buttons)					\
+	bcm47xx_buttons_copy(dev_buttons, ARRAY_SIZE(dev_buttons));
+
+int __init bcm47xx_buttons_register(void)
+{
+	enum bcm47xx_board board = bcm47xx_board_get();
+	int err;
+
+#ifdef CONFIG_BCM47XX_SSB
+	if (bcm47xx_bus_type == BCM47XX_BUS_TYPE_SSB) {
+		pr_debug("Buttons on SSB are not supported yet.\n");
+		return -ENOTSUPP;
+	}
+#endif
+
+	switch (board) {
+	case BCM47XX_BOARD_NETGEAR_WNDR4500V1:
+		err = bcm47xx_copy_bdata(bcm47xx_buttons_netgear_wndr4500_v1);
+		break;
+	default:
+		pr_debug("No buttons configuration found for this device\n");
+		return -ENOTSUPP;
+	}
+
+	if (err)
+		return -ENOMEM;
+
+	err = platform_device_register(&bcm47xx_buttons_gpio_keys);
+	if (err) {
+		pr_err("Failed to register platform device: %d\n", err);
+		return err;
+	}
+
+	return 0;
+}
arch/mips/bcm47xx/setup.c +1 -0
index 7e61c0b..a791124 100644
@@ -242,6 +242,7 @@ static int __init bcm47xx_register_bus_complete(void)
 #endif
 	}
 
+	bcm47xx_buttons_register();
 	bcm47xx_leds_register();
 
 	return 0;
author maintainer
#5 lore raw

PATCH v4 1/1 MIPS: BCM47XX: Prepare support for GPIO buttons

In reply to Rafał Miłecki

So far this adds support for one Netgear model only, but it's designed and ready to add many more device. We could hopefully import database from OpenWrt.
Support for SSB is currently disabled, because SSB doesn't implement IRQ domain yet.

---
V3: Use __initconst and copy data for detected device. This will allow
us to free some memory after init.
V4: Use const for bcm47xx_buttons_netgear_wndr4500_v1
Rebase on top of linux-john.git mips-next-3.14

---
 arch/mips/bcm47xx/Makefile          |    2 +-
 arch/mips/bcm47xx/bcm47xx_private.h |    3 ++
 arch/mips/bcm47xx/buttons.c         |   95 +++++++++++++++++++++++++++++++++++
 arch/mips/bcm47xx/setup.c           |    1 +
 4 files changed, 100 insertions(+), 1 deletion(-)
 create mode 100644 arch/mips/bcm47xx/buttons.c
arch/mips/bcm47xx/Makefile +1 -1
index 84e9aed..006a05e 100644
@@ -4,5 +4,5 @@
 #
 
 obj-y				+= irq.o nvram.o prom.o serial.o setup.o time.o sprom.o
-obj-y				+= board.o leds.o
+obj-y				+= board.o buttons.o leds.o
 obj-$(CONFIG_BCM47XX_SSB)	+= wgt634u.o
arch/mips/bcm47xx/bcm47xx_private.h +3 -0
index 1a1e600..5c94ace 100644
@@ -3,6 +3,9 @@
 
 #include <linux/kernel.h>
 
+/* buttons.c */
+int __init bcm47xx_buttons_register(void);
+
 /* leds.c */
 void __init bcm47xx_leds_register(void);
 
arch/mips/bcm47xx/buttons.c new file +95 -0

95 lines not on this page · raw message

arch/mips/bcm47xx/setup.c +1 -0
index bd84473..0bd4702 100644
@@ -265,6 +265,7 @@ static int __init bcm47xx_register_bus_complete(void)
 #endif
 	}
 
+	bcm47xx_buttons_register();
 	bcm47xx_leds_register();
 
 	fixed_phy_add(PHY_POLL, 0, &bcm47xx_fixed_phy_status);
Signature · 1 line
1.7.10.4
maintainer
#6 lore raw
In reply to Rafał Miłecki
Quoted 6 lines · Rafał Miłecki

Input GPIO changes can generate interrupts, but we need kind of ACK for them by changing IRQ polarity. This is required to stop hardware from keep generating interrupts and generate another one on the next GPIO state change.
This code allows using GPIOs with standard interrupts and add for example GPIO buttons support.

As far as I know IRQs for GPIOs are only support on SoCs and not on PCIe wifi cards like the BCM43224. I think the dependency to IRQ_DOMAIN should only be added when BCMA_HOST_SOC is set.

Hauke

Quoted 69 lines
---
 drivers/bcma/Kconfig                        |    1 +
 drivers/bcma/driver_gpio.c                  |   92 ++++++++++++++++++++++++++-
 include/linux/bcma/bcma_driver_chipcommon.h |    1 +
 3 files changed, 92 insertions(+), 2 deletions(-)
drivers/bcma/Kconfig +1 -0
index 7c081b3..649568e 100644
@@ -75,6 +75,7 @@ config BCMA_DRIVER_GMAC_CMN
 config BCMA_DRIVER_GPIO
 	bool "BCMA GPIO driver"
 	depends on BCMA && GPIOLIB
+	select IRQ_DOMAIN
 	help
 	  Driver to provide access to the GPIO pins of the bcma bus.
 
drivers/bcma/driver_gpio.c +29 -1
index 45f0996..0abf09a 100644
@@ -9,6 +9,9 @@
  */
 
 #include <linux/gpio.h>
+#include <linux/irq.h>
+#include <linux/interrupt.h>
+#include <linux/irqdomain.h>
 #include <linux/export.h>
 #include <linux/bcma/bcma.h>
 
@@ -78,15 +81,60 @@ static int bcma_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
 	struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
 
 	if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
-		return bcma_core_irq(cc->core);
+		return irq_find_mapping(cc->irq_domain, gpio);
 	else
 		return -EINVAL;
 }
 
+static void bcma_gpio_irq_unmask(struct irq_data *d)
+{
+	struct bcma_drv_cc *cc = irq_data_get_irq_chip_data(d);
+	int gpio = irqd_to_hwirq(d);
+
+	bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio));
+}
+
+static void bcma_gpio_irq_mask(struct irq_data *d)
+{
+	struct bcma_drv_cc *cc = irq_data_get_irq_chip_data(d);
+	int gpio = irqd_to_hwirq(d);
+
+	bcma_chipco_gpio_intmask(cc, BIT(gpio), 0);
+}
+
+static struct irq_chip bcma_gpio_irq_chip = {
+	.name		= "BCMA-GPIO",
+	.irq_mask	= bcma_gpio_irq_mask,
+	.irq_unmask	= bcma_gpio_irq_unmask,
+};
+
+static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id)
+{
+	struct bcma_drv_cc *cc = dev_id;

drivers/bcma/driver_gpio.c static int bcma_gpio_to_irq(struct gpio_chip *chip, unsigned gpio) · line 123

Quoted 10 lines
drivers/bcma/driver_gpio.c +10 -0
+	u32 val = bcma_cc_read32(cc, BCMA_CC_GPIOIN);
+	u32 mask = bcma_cc_read32(cc, BCMA_CC_GPIOIRQ);
+	u32 pol = bcma_cc_read32(cc, BCMA_CC_GPIOPOL);
+	u32 irqs = (val ^ pol) & mask;
+	int gpio;
+
+	for_each_set_bit(gpio, (unsigned long *)&irqs, cc->gpio.ngpio) {
+		generic_handle_irq(bcma_gpio_to_irq(&cc->gpio, gpio));
+		bcma_chipco_gpio_polarity(cc, BIT(gpio),
+					  (val & BIT(gpio)) ? BIT(gpio) : 0);

What about setting this once for all irqs at the end of this function? bcma_chipco_gpio_polarity() takes an lock.

Quoted 18 lines
drivers/bcma/driver_gpio.c +10 -0
+	}
+
+	return irqs ? IRQ_HANDLED : IRQ_NONE;
+}
+
 int bcma_gpio_init(struct bcma_drv_cc *cc)
 {
 	struct gpio_chip *chip = &cc->gpio;
+	unsigned int hwirq, gpio;
+	int err;
 
+	/*
+	 * GPIO chip
+	 */
 	chip->label		= "bcma_gpio";
 	chip->owner		= THIS_MODULE;
 	chip->request		= bcma_gpio_request;

drivers/bcma/driver_gpio.c int bcma_gpio_init(struct bcma_drv_cc *cc) · line 157

Quoted 6 lines
drivers/bcma/driver_gpio.c +3 -0
@@ -104,8 +152,48 @@ int bcma_gpio_init(struct bcma_drv_cc *cc)
 		chip->base		= 0;
 	else
 		chip->base		= -1;
+	err = gpiochip_add(chip);
+	if (err)
+		goto err_gpiochip_add;

Shouldn't the gpio chip be registered after we set up the irq handling so no one uses it before?

Quoted 7 lines
drivers/bcma/driver_gpio.c +7 -0
+
+	/*
+	 * IRQ domain
+	 */
+	cc->irq_domain = irq_domain_add_linear(NULL, chip->ngpio,
+					       &irq_domain_simple_ops, cc);
+	if (!cc->irq_domain) {

drivers/bcma/driver_gpio.c int bcma_gpio_init(struct bcma_drv_cc *cc) · line 174

Quoted 10 lines
drivers/bcma/driver_gpio.c +10 -0
+		err = -ENODEV;
+		goto err_irq_domain;
+	}
+	for (gpio = 0; gpio < chip->ngpio; gpio++) {
+		int irq = irq_create_mapping(cc->irq_domain, gpio);
+
+		irq_set_chip_data(irq, cc);
+		irq_set_chip_and_handler(irq, &bcma_gpio_irq_chip,
+					 handle_simple_irq);
+	}

Is there some method needed to free or unregister these functions? This could be build as an module when it is not used on a SoC.

Quoted 39 lines
drivers/bcma/driver_gpio.c +21 -1
+
+	/*
+	 * IRQ handler
+	 */
+	hwirq = bcma_core_irq(cc->core);
+	err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio",
+			  cc);
+	if (err)
+		goto err_req_irq;
+
+	bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO);
+
+	return 0;
 
-	return gpiochip_add(chip);
+err_req_irq:
+	irq_domain_remove(cc->irq_domain);
+err_irq_domain:
+	err = gpiochip_remove(&cc->gpio);
+	if (err)
+		pr_err("Failed to remove GPIO chip: %d\n", err);
+err_gpiochip_add:
+	return err;
 }
 
 int bcma_gpio_unregister(struct bcma_drv_cc *cc)
include/linux/bcma/bcma_driver_chipcommon.h +1 -0
index c49e1a1..63d105c 100644
@@ -640,6 +640,7 @@ struct bcma_drv_cc {
 	spinlock_t gpio_lock;
 #ifdef CONFIG_BCMA_DRIVER_GPIO
 	struct gpio_chip gpio;
+	struct irq_domain *irq_domain;
 #endif
 };
 
author maintainer
#7 lore raw

2013/11/29 Hauke Mehrtens <[email protected]>:

Quoted 11 lines · Rafał Miłecki
Quoted 6 lines

Input GPIO changes can generate interrupts, but we need kind of ACK for them by changing IRQ polarity. This is required to stop hardware from keep generating interrupts and generate another one on the next GPIO state change.
This code allows using GPIOs with standard interrupts and add for example GPIO buttons support.

As far as I know IRQs for GPIOs are only support on SoCs and not on PCIe wifi cards like the BCM43224. I think the dependency to IRQ_DOMAIN should only be added when BCMA_HOST_SOC is set.

Sounds sane.

Quoted 7 lines

drivers/bcma/driver_gpio.c static int bcma_gpio_to_irq(struct gpio_chip *chip, unsigned gpio) · line 123

Quoted 4 lines
drivers/bcma/driver_gpio.c +4 -0
+     for_each_set_bit(gpio, (unsigned long *)&irqs, cc->gpio.ngpio) {
+             generic_handle_irq(bcma_gpio_to_irq(&cc->gpio, gpio));
+             bcma_chipco_gpio_polarity(cc, BIT(gpio),
+                                       (val & BIT(gpio)) ? BIT(gpio) : 0);

What about setting this once for all irqs at the end of this function? bcma_chipco_gpio_polarity() takes an lock.

OK

Quoted 6 lines
Quoted 6 lines

int bcma_gpio_init(struct bcma_drv_cc *cc)

 {
      struct gpio_chip *chip = &cc->gpio;
drivers/bcma/driver_gpio.c +2 -0
+     unsigned int hwirq, gpio;
+     int err;
Quoted 16 lines

drivers/bcma/driver_gpio.c int bcma_gpio_init(struct bcma_drv_cc *cc) · line 157

Quoted 13 lines
drivers/bcma/driver_gpio.c +6 -0
+     /*
+      * GPIO chip
+      */
      chip->label             = "bcma_gpio";
      chip->owner             = THIS_MODULE;
      chip->request           = bcma_gpio_request;
@@ -104,8 +152,48 @@ int bcma_gpio_init(struct bcma_drv_cc *cc)
              chip->base              = 0;
      else
              chip->base              = -1;
+     err = gpiochip_add(chip);
+     if (err)
+             goto err_gpiochip_add;

Shouldn't the gpio chip be registered after we set up the irq handling so no one uses it before?

I don't know.

gpio-tegra.c calls functions in this order:
1) gpiochip_add
2) irq_create_mapping3
3) irq_set_chip_and_handler
4) irq_set_chained_handler

gpio-msm-v2:
1) gpiochip_add
2) irq_domain_add_linear
3) irq_set_chained_handler

gpio-mpc8xxx.c: like above

gpio-adnp.c on the other hand:
1) irq_domain_add_linear
2) request_threaded_irq
3) gpiochip_add

gpio-grgpio.c:
1) irq_domain_add_linear
2) gpiochip_add

So that differs pretty much between the drivers.

But I think calling "gpiochip_add" at the end makes some sense. I guess I was looking at gpio-tegra.c too much.

Quoted 10 lines

drivers/bcma/driver_gpio.c int bcma_gpio_init(struct bcma_drv_cc *cc) · line 174

Quoted 7 lines
drivers/bcma/driver_gpio.c +7 -0
+     for (gpio = 0; gpio < chip->ngpio; gpio++) {
+             int irq = irq_create_mapping(cc->irq_domain, gpio);
+
+             irq_set_chip_data(irq, cc);
+             irq_set_chip_and_handler(irq, &bcma_gpio_irq_chip,
+                                      handle_simple_irq);
+     }

Is there some method needed to free or unregister these functions? This could be build as an module when it is not used on a SoC.

Yes: irq_dispose_mapping (it calls irq_set_chip_and_handler with NULL and NULL for us). I'll use it.

Signature · 1 line
Rafał
author maintainer
#8 lore raw

PATCH v3 1/2 bcma: gpio: add own IRQ domain

In reply to Rafał Miłecki

Input GPIO changes can generate interrupts, but we need kind of ACK for them by changing IRQ polarity. This is required to stop hardware from keep generating interrupts and generate another one on the next GPIO state change.
This code allows using GPIOs with standard interrupts and add for example GPIO buttons support.

---
V3: Apply Hauke's comments.
1) Use IRQ domain for CONFIG_BCMA_HOST_SOC only
2) Optimize bcma_gpio_irq_handler
3) Register GPIO chip after doing everything else
4) Improve cleaning paths

---
 drivers/bcma/Kconfig                        |    1 +
 drivers/bcma/driver_gpio.c                  |  126 ++++++++++++++++++++++++++-
 include/linux/bcma/bcma_driver_chipcommon.h |    1 +
 3 files changed, 126 insertions(+), 2 deletions(-)
drivers/bcma/Kconfig +1 -0
index 7c081b3..0ee48be 100644
@@ -75,6 +75,7 @@ config BCMA_DRIVER_GMAC_CMN
 config BCMA_DRIVER_GPIO
 	bool "BCMA GPIO driver"
 	depends on BCMA && GPIOLIB
+	select IRQ_DOMAIN if BCMA_HOST_SOC
 	help
 	  Driver to provide access to the GPIO pins of the bcma bus.
 
drivers/bcma/driver_gpio.c +124 -2

166 lines not on this page · raw message

include/linux/bcma/bcma_driver_chipcommon.h +1 -0
index c49e1a1..63d105c 100644
@@ -640,6 +640,7 @@ struct bcma_drv_cc {
 	spinlock_t gpio_lock;
 #ifdef CONFIG_BCMA_DRIVER_GPIO
 	struct gpio_chip gpio;
+	struct irq_domain *irq_domain;
 #endif
 };
 
Signature · 1 line
1.7.10.4
maintainer
#9 lore raw
Quoted 90 lines · Rafał Miłecki

Input GPIO changes can generate interrupts, but we need kind of ACK for them by changing IRQ polarity. This is required to stop hardware from keep generating interrupts and generate another one on the next GPIO state change.
This code allows using GPIOs with standard interrupts and add for example GPIO buttons support.

---
V3: Apply Hauke's comments.
1) Use IRQ domain for CONFIG_BCMA_HOST_SOC only
2) Optimize bcma_gpio_irq_handler
3) Register GPIO chip after doing everything else
4) Improve cleaning paths

---
 drivers/bcma/Kconfig                        |    1 +
 drivers/bcma/driver_gpio.c                  |  126 ++++++++++++++++++++++++++-
 include/linux/bcma/bcma_driver_chipcommon.h |    1 +
 3 files changed, 126 insertions(+), 2 deletions(-)
drivers/bcma/Kconfig +1 -0
index 7c081b3..0ee48be 100644
@@ -75,6 +75,7 @@ config BCMA_DRIVER_GMAC_CMN
 config BCMA_DRIVER_GPIO
 	bool "BCMA GPIO driver"
 	depends on BCMA && GPIOLIB
+	select IRQ_DOMAIN if BCMA_HOST_SOC
 	help
 	  Driver to provide access to the GPIO pins of the bcma bus.
 
drivers/bcma/driver_gpio.c +33 -1
index 45f0996..fe9b1bc 100644
@@ -9,6 +9,9 @@
  */
 
 #include <linux/gpio.h>
+#include <linux/irq.h>
+#include <linux/interrupt.h>
+#include <linux/irqdomain.h>
 #include <linux/export.h>
 #include <linux/bcma/bcma.h>
 
@@ -73,19 +76,117 @@ static void bcma_gpio_free(struct gpio_chip *chip, unsigned gpio)
 	bcma_chipco_gpio_pullup(cc, 1 << gpio, 0);
 }
 
+#ifdef CONFIG_BCMA_HOST_SOC
 static int bcma_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
 {
 	struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
 
 	if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
-		return bcma_core_irq(cc->core);
+		return irq_find_mapping(cc->irq_domain, gpio);
 	else
 		return -EINVAL;
 }
 
+static void bcma_gpio_irq_unmask(struct irq_data *d)
+{
+	struct bcma_drv_cc *cc = irq_data_get_irq_chip_data(d);
+	int gpio = irqd_to_hwirq(d);
+
+	bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio));
+}
+
+static void bcma_gpio_irq_mask(struct irq_data *d)
+{
+	struct bcma_drv_cc *cc = irq_data_get_irq_chip_data(d);
+	int gpio = irqd_to_hwirq(d);
+
+	bcma_chipco_gpio_intmask(cc, BIT(gpio), 0);
+}
+
+static struct irq_chip bcma_gpio_irq_chip = {
+	.name		= "BCMA-GPIO",
+	.irq_mask	= bcma_gpio_irq_mask,
+	.irq_unmask	= bcma_gpio_irq_unmask,
+};
+
+static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id)
+{
+	struct bcma_drv_cc *cc = dev_id;
+	u32 val = bcma_cc_read32(cc, BCMA_CC_GPIOIN);
+	u32 mask = bcma_cc_read32(cc, BCMA_CC_GPIOIRQ);
+	u32 pol = bcma_cc_read32(cc, BCMA_CC_GPIOPOL);

drivers/bcma/driver_gpio.c static void bcma_gpio_free(struct gpio_chip *chip, unsigned gpio) · line 127

Quoted 10 lines
drivers/bcma/driver_gpio.c +10 -0
+	u32 irqs = (val ^ pol) & mask;
+	u32 pol_mask = 0, pol_val = 0;
+	int gpio;
+
+	for_each_set_bit(gpio, (unsigned long *)&irqs, cc->gpio.ngpio) {
+		generic_handle_irq(bcma_gpio_to_irq(&cc->gpio, gpio));
+		pol_mask |= BIT(gpio);
+		if (val & BIT(gpio))
+			pol_val |= BIT(gpio);
+	}

Instead of calculation pol_mask and pol_val bit for bit manually, you can pretty easy calculate valid value like this:
pol_mask = irqs;
pol_val = val & irqs;

Quoted 47 lines
drivers/bcma/driver_gpio.c +47 -0
+	bcma_chipco_gpio_polarity(cc, pol_mask, pol_val);
+
+	return irqs ? IRQ_HANDLED : IRQ_NONE;
+}
+
+static int bcma_gpio_irq_domain_init(struct bcma_drv_cc *cc)
+{
+	struct gpio_chip *chip = &cc->gpio;
+	int gpio, hwirq, err;
+
+	cc->irq_domain = irq_domain_add_linear(NULL, chip->ngpio,
+					       &irq_domain_simple_ops, cc);
+	if (!cc->irq_domain) {
+		err = -ENODEV;
+		goto err_irq_domain;
+	}
+	for (gpio = 0; gpio < chip->ngpio; gpio++) {
+		int irq = irq_create_mapping(cc->irq_domain, gpio);
+
+		irq_set_chip_data(irq, cc);
+		irq_set_chip_and_handler(irq, &bcma_gpio_irq_chip,
+					 handle_simple_irq);
+	}
+
+	hwirq = bcma_core_irq(cc->core);
+	err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio",
+			  cc);
+	if (err)
+		goto err_req_irq;
+
+	return 0;
+
+err_req_irq:
+	for (gpio = 0; gpio < chip->ngpio; gpio++) {
+		int irq = irq_find_mapping(cc->irq_domain, gpio);
+
+		irq_dispose_mapping(irq);
+	}
+	irq_domain_remove(cc->irq_domain);
+err_irq_domain:
+	return err;
+}
+
+static void bcma_gpio_irq_domain_exit(struct bcma_drv_cc *cc)
+{
+	struct gpio_chip *chip = &cc->gpio;
+	int gpio;

drivers/bcma/driver_gpio.c static void bcma_gpio_free(struct gpio_chip *chip, unsigned gpio) · line 184

Quoted 10 lines
drivers/bcma/driver_gpio.c +10 -0
+
+	free_irq(bcma_core_irq(cc->core), cc);
+	for (gpio = 0; gpio < chip->ngpio; gpio++) {
+		int irq = irq_find_mapping(cc->irq_domain, gpio);
+
+		irq_dispose_mapping(irq);
+	}
+	irq_domain_remove(cc->irq_domain);
+}
+#endif

I do not like all these ifdefs, why not add this and remove some of them from the code in bcma_gpio_init():

#else /* CONFIG_BCMA_HOST_SOC */

static int bcma_gpio_irq_domain_init(struct bcma_drv_cc *cc)

{
	return 0;
}
static void bcma_gpio_irq_domain_exit(struct bcma_drv_cc *cc)
{
	/* Nothing */
}

#endif /* CONFIG_BCMA_HOST_SOC */

Quoted 28 lines
drivers/bcma/driver_gpio.c +9 -1
+
 int bcma_gpio_init(struct bcma_drv_cc *cc)
 {
 	struct gpio_chip *chip = &cc->gpio;
+	int err;
 
 	chip->label		= "bcma_gpio";
 	chip->owner		= THIS_MODULE;
@@ -95,7 +196,9 @@ int bcma_gpio_init(struct bcma_drv_cc *cc)
 	chip->set		= bcma_gpio_set_value;
 	chip->direction_input	= bcma_gpio_direction_input;
 	chip->direction_output	= bcma_gpio_direction_output;
+#ifdef CONFIG_BCMA_HOST_SOC
 	chip->to_irq		= bcma_gpio_to_irq;
+#endif
 	chip->ngpio		= 16;
 	/* There is just one SoC in one device and its GPIO addresses should be
 	 * deterministic to address them more easily. The other buses could get
@@ -105,10 +208,29 @@ int bcma_gpio_init(struct bcma_drv_cc *cc)
 	else
 		chip->base		= -1;
 
-	return gpiochip_add(chip);
+#ifdef CONFIG_BCMA_HOST_SOC
+	err = bcma_gpio_irq_domain_init(cc);
+	if (err)
+		return err;
+#endif

drivers/bcma/driver_gpio.c int bcma_gpio_init(struct bcma_drv_cc *cc) · line 225

Quoted 10 lines
drivers/bcma/driver_gpio.c +10 -0
+
+	err = gpiochip_add(chip);
+	if (err) {
+#ifdef CONFIG_BCMA_HOST_SOC
+		bcma_gpio_irq_domain_exit(cc);
+#endif
+		return err;
+	}
+
+	bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO);

This should be moved into bcma_gpio_irq_domain_ini(), I do not know what happens when we activate them on a PCIe device. IRQs should get deactivated in bcma_gpio_irq_domain_exit().

Quoted 24 lines
drivers/bcma/driver_gpio.c +5 -0
+
+	return 0;
 }
 
 int bcma_gpio_unregister(struct bcma_drv_cc *cc)
 {
+#ifdef CONFIG_BCMA_HOST_SOC
+	bcma_gpio_irq_domain_exit(cc);
+#endif
 	return gpiochip_remove(&cc->gpio);
 }
include/linux/bcma/bcma_driver_chipcommon.h +1 -0
index c49e1a1..63d105c 100644
@@ -640,6 +640,7 @@ struct bcma_drv_cc {
 	spinlock_t gpio_lock;
 #ifdef CONFIG_BCMA_DRIVER_GPIO
 	struct gpio_chip gpio;
+	struct irq_domain *irq_domain;
 #endif
 };
 
author maintainer
#10 lore raw

PATCH v4 1/2 bcma: gpio: add own IRQ domain

In reply to Rafał Miłecki

Input GPIO changes can generate interrupts, but we need kind of ACK for them by changing IRQ polarity. This is required to stop hardware from keep generating interrupts and generate another one on the next GPIO state change.
This code allows using GPIOs with standard interrupts and add for example GPIO buttons support.

---
V3: Apply Hauke's comments.
1) Use IRQ domain for CONFIG_BCMA_HOST_SOC only
2) Optimize bcma_gpio_irq_handler
3) Register GPIO chip after doing everything else
4) Improve cleaning paths

V4: More fixes from Hauke's comments:
1) Less #ifdef CONFIG_BCMA_HOST_SOC
2) Move BCMA_CC_IRQMASK op (and unset on exit)
3) Optimize bcma_gpio_irq_handler

---
 drivers/bcma/Kconfig                        |    1 +
 drivers/bcma/driver_gpio.c                  |  128 ++++++++++++++++++++++++++-
 include/linux/bcma/bcma_driver_chipcommon.h |    1 +
 3 files changed, 128 insertions(+), 2 deletions(-)
drivers/bcma/Kconfig +1 -0
index 7c081b3..0ee48be 100644
@@ -75,6 +75,7 @@ config BCMA_DRIVER_GMAC_CMN
 config BCMA_DRIVER_GPIO
 	bool "BCMA GPIO driver"
 	depends on BCMA && GPIOLIB
+	select IRQ_DOMAIN if BCMA_HOST_SOC
 	help
 	  Driver to provide access to the GPIO pins of the bcma bus.
 
drivers/bcma/driver_gpio.c +126 -2

168 lines not on this page · raw message

include/linux/bcma/bcma_driver_chipcommon.h +1 -0
index c49e1a1..63d105c 100644
@@ -640,6 +640,7 @@ struct bcma_drv_cc {
 	spinlock_t gpio_lock;
 #ifdef CONFIG_BCMA_DRIVER_GPIO
 	struct gpio_chip gpio;
+	struct irq_domain *irq_domain;
 #endif
 };
 
Signature · 1 line
1.7.10.4
#11 lore raw

drivers/bcma/driver_gpio.c int bcma_gpio_init(struct bcma_drv_cc *cc) · line 212

Quoted 4 lines · Rafał Miłecki
drivers/bcma/driver_gpio.c +2 -0
+#ifdef CONFIG_BCMA_HOST_SOC
  	chip->to_irq		= bcma_gpio_to_irq;
+#endif
  	chip->ngpio		= 16;

Hi,

Should this not be

if (IS_ENABLED(CONFIG_BCMA_HOST_SOC))

	chip->to_irq = bcma_gpio_to_irq;


	John
author maintainer
#12 lore raw

2013/11/29 John Crispin <[email protected]>:

Quoted 11 lines · Rafał Miłecki

drivers/bcma/driver_gpio.c int bcma_gpio_init(struct bcma_drv_cc *cc) · line 212

Quoted 5 lines
drivers/bcma/driver_gpio.c +2 -0
+#ifdef CONFIG_BCMA_HOST_SOC
        chip->to_irq            = bcma_gpio_to_irq;
+#endif
        chip->ngpio             = 16;

Hi,

Quoted 4 lines

Should this not be

if (IS_ENABLED(CONFIG_BCMA_HOST_SOC))

        chip->to_irq = bcma_gpio_to_irq;

I can't find a proper documentation about that. It's definitely nicer to use #if IS_ENABLED(FOO)
instead of
#if defined(FOO) || defined(FOO_MODULE)

But are we supposed to use it also for a simple
#if defined(FOO)
?

I tried to Google about this but found only some minor flame-wars ;)

Is that documented anywhere?

Signature · 1 line
Rafał
#13 lore raw
Quoted 22 lines · Rafał Miłecki

2013/11/29 John Crispin<[email protected]>:

Quoted 11 lines · Rafał Miłecki

drivers/bcma/driver_gpio.c int bcma_gpio_init(struct bcma_drv_cc *cc) · line 212

Quoted 5 lines
drivers/bcma/driver_gpio.c +2 -0
+#ifdef CONFIG_BCMA_HOST_SOC
         chip->to_irq            = bcma_gpio_to_irq;
+#endif
         chip->ngpio             = 16;

Hi,

Quoted 4 lines

Should this not be

if (IS_ENABLED(CONFIG_BCMA_HOST_SOC))

         chip->to_irq = bcma_gpio_to_irq;

I can't find a proper documentation about that. It's definitely nicer to use #if IS_ENABLED(FOO)
instead of
#if defined(FOO) || defined(FOO_MODULE)

Quoted 8 lines

But are we supposed to use it also for a simple
#if defined(FOO)
?

I tried to Google about this but found only some minor flame-wars ;)

Is that documented anywhere?

The commit message has the relevant info ...

http://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/commit/?id=2a11c8ea20bf850b3a2c60db8c2e7497d28aba99

John

maintainer Acked-by
#14 lore raw
Quoted 30 lines · John Crispin
Quoted 29 lines · Rafał Miłecki

2013/11/29 John Crispin<[email protected]>:

Quoted 11 lines · Rafał Miłecki

drivers/bcma/driver_gpio.c int bcma_gpio_init(struct bcma_drv_cc *cc) · line 212

Quoted 5 lines
drivers/bcma/driver_gpio.c +2 -0
+#ifdef CONFIG_BCMA_HOST_SOC
         chip->to_irq            = bcma_gpio_to_irq;
+#endif
         chip->ngpio             = 16;

Hi,

Quoted 4 lines

Should this not be

if (IS_ENABLED(CONFIG_BCMA_HOST_SOC))

         chip->to_irq = bcma_gpio_to_irq;

I can't find a proper documentation about that. It's definitely nicer to use
#if IS_ENABLED(FOO)
instead of
#if defined(FOO) || defined(FOO_MODULE)

But are we supposed to use it also for a simple
#if defined(FOO)
?

I tried to Google about this but found only some minor flame-wars ;)

Quoted 7 lines
Quoted 2 lines

Is that documented anywhere?

The commit message has the relevant info ...

http://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/commit/?id=2a11c8ea20bf850b3a2c60db8c2e7497d28aba99

I read this and as far as I understand that when CONFIG_BCMA_HOST_SOC is bool and not tristate "#ifdef CONFIG_BCMA_HOST_SOC" and "#if IS_ENABLED(CONFIG_BCMA_HOST_SOC)" will have the same effect?

@Rafał

for the "bcma: gpio: add own IRQ domain" patch.

Hauke

#15 lore raw

Hello.

Quoted 27 lines · Hauke Mehrtens
Quoted 25 lines
Quoted 25 lines
Quoted 11 lines

drivers/bcma/driver_gpio.c int bcma_gpio_init(struct bcma_drv_cc *cc) · line 212

Quoted 4 lines
drivers/bcma/driver_gpio.c +2 -0
+#ifdef CONFIG_BCMA_HOST_SOC
          chip->to_irq            = bcma_gpio_to_irq;
+#endif
          chip->ngpio             = 16;

Hi,

Should this not be

if (IS_ENABLED(CONFIG_BCMA_HOST_SOC))

          chip->to_irq = bcma_gpio_to_irq;

I can't find a proper documentation about that. It's definitely nicer to use
#if IS_ENABLED(FOO)
instead of
#if defined(FOO) || defined(FOO_MODULE)

But are we supposed to use it also for a simple
#if defined(FOO)
?

I tried to Google about this but found only some minor flame-wars ;)

Is that documented anywhere?

Quoted 7 lines
Quoted 3 lines

The commit message has the relevant info ...

http://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/commit/?id=2a11c8ea20bf850b3a2c60db8c2e7497d28aba99

I read this and as far as I understand that when CONFIG_BCMA_HOST_SOC is bool and not tristate "#ifdef CONFIG_BCMA_HOST_SOC" and "#if IS_ENABLED(CONFIG_BCMA_HOST_SOC)" will have the same effect?

You can also use IS_BUILTIN(CONFIG_BCMA_HOST_SOC) to avoid unneeded test
for the modular case.

Quoted 1 line

Hauke

WBR, Sergei

author maintainer
#16 lore raw

PATCH v5 1/2 bcma: gpio: add own IRQ domain

In reply to Rafał Miłecki

Input GPIO changes can generate interrupts, but we need kind of ACK for them by changing IRQ polarity. This is required to stop hardware from keep generating interrupts and generate another one on the next GPIO state change.
This code allows using GPIOs with standard interrupts and add for example GPIO buttons support.

---
V3: Apply Hauke's comments.
1) Use IRQ domain for CONFIG_BCMA_HOST_SOC only
2) Optimize bcma_gpio_irq_handler
3) Register GPIO chip after doing everything else
4) Improve cleaning paths

V4: More fixes from Hauke's comments:
1) Less #ifdef CONFIG_BCMA_HOST_SOC
2) Move BCMA_CC_IRQMASK op (and unset on exit)
3) Optimize bcma_gpio_irq_handler

V5: Use IS_BUILTIN

---
 drivers/bcma/Kconfig                        |    1 +
 drivers/bcma/driver_gpio.c                  |  129 ++++++++++++++++++++++++++-
 include/linux/bcma/bcma_driver_chipcommon.h |    1 +
 3 files changed, 128 insertions(+), 3 deletions(-)
drivers/bcma/Kconfig +1 -0
index 7c081b3..0ee48be 100644
@@ -75,6 +75,7 @@ config BCMA_DRIVER_GMAC_CMN
 config BCMA_DRIVER_GPIO
 	bool "BCMA GPIO driver"
 	depends on BCMA && GPIOLIB
+	select IRQ_DOMAIN if BCMA_HOST_SOC
 	help
 	  Driver to provide access to the GPIO pins of the bcma bus.
 
drivers/bcma/driver_gpio.c +126 -3

168 lines not on this page · raw message

include/linux/bcma/bcma_driver_chipcommon.h +1 -0
index c49e1a1..63d105c 100644
@@ -640,6 +640,7 @@ struct bcma_drv_cc {
 	spinlock_t gpio_lock;
 #ifdef CONFIG_BCMA_DRIVER_GPIO
 	struct gpio_chip gpio;
+	struct irq_domain *irq_domain;
 #endif
 };
 
Signature · 1 line
1.7.10.4
maintainer
#17 lore raw
Quoted 194 lines · Rafał Miłecki

Input GPIO changes can generate interrupts, but we need kind of ACK for them by changing IRQ polarity. This is required to stop hardware from keep generating interrupts and generate another one on the next GPIO state change.
This code allows using GPIOs with standard interrupts and add for example GPIO buttons support.

---
V3: Apply Hauke's comments.
1) Use IRQ domain for CONFIG_BCMA_HOST_SOC only
2) Optimize bcma_gpio_irq_handler
3) Register GPIO chip after doing everything else
4) Improve cleaning paths

V4: More fixes from Hauke's comments:
1) Less #ifdef CONFIG_BCMA_HOST_SOC
2) Move BCMA_CC_IRQMASK op (and unset on exit)
3) Optimize bcma_gpio_irq_handler

V5: Use IS_BUILTIN

---
 drivers/bcma/Kconfig                        |    1 +
 drivers/bcma/driver_gpio.c                  |  129 ++++++++++++++++++++++++++-
 include/linux/bcma/bcma_driver_chipcommon.h |    1 +
 3 files changed, 128 insertions(+), 3 deletions(-)
drivers/bcma/Kconfig +1 -0
index 7c081b3..0ee48be 100644
@@ -75,6 +75,7 @@ config BCMA_DRIVER_GMAC_CMN
 config BCMA_DRIVER_GPIO
 	bool "BCMA GPIO driver"
 	depends on BCMA && GPIOLIB
+	select IRQ_DOMAIN if BCMA_HOST_SOC
 	help
 	  Driver to provide access to the GPIO pins of the bcma bus.
 
drivers/bcma/driver_gpio.c +114 -2
index 45f0996..d5b02d2 100644
@@ -9,6 +9,9 @@
  */
 
 #include <linux/gpio.h>
+#include <linux/irq.h>
+#include <linux/interrupt.h>
+#include <linux/irqdomain.h>
 #include <linux/export.h>
 #include <linux/bcma/bcma.h>
 
@@ -73,19 +76,127 @@ static void bcma_gpio_free(struct gpio_chip *chip, unsigned gpio)
 	bcma_chipco_gpio_pullup(cc, 1 << gpio, 0);
 }
 
+#if IS_BUILTIN(CONFIG_BCMA_HOST_SOC)
 static int bcma_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
 {
 	struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
 
 	if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
-		return bcma_core_irq(cc->core);
+		return irq_find_mapping(cc->irq_domain, gpio);
 	else
 		return -EINVAL;
 }
 
+static void bcma_gpio_irq_unmask(struct irq_data *d)
+{
+	struct bcma_drv_cc *cc = irq_data_get_irq_chip_data(d);
+	int gpio = irqd_to_hwirq(d);
+
+	bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio));
+}
+
+static void bcma_gpio_irq_mask(struct irq_data *d)
+{
+	struct bcma_drv_cc *cc = irq_data_get_irq_chip_data(d);
+	int gpio = irqd_to_hwirq(d);
+
+	bcma_chipco_gpio_intmask(cc, BIT(gpio), 0);
+}
+
+static struct irq_chip bcma_gpio_irq_chip = {
+	.name		= "BCMA-GPIO",
+	.irq_mask	= bcma_gpio_irq_mask,
+	.irq_unmask	= bcma_gpio_irq_unmask,
+};
+
+static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id)
+{
+	struct bcma_drv_cc *cc = dev_id;
+	u32 val = bcma_cc_read32(cc, BCMA_CC_GPIOIN);
+	u32 mask = bcma_cc_read32(cc, BCMA_CC_GPIOIRQ);
+	u32 pol = bcma_cc_read32(cc, BCMA_CC_GPIOPOL);
+	u32 irqs = (val ^ pol) & mask;
+	int gpio;
+
+	if (!irqs)
+		return IRQ_NONE;
+
+	for_each_set_bit(gpio, (unsigned long *)&irqs, cc->gpio.ngpio)
+		generic_handle_irq(bcma_gpio_to_irq(&cc->gpio, gpio));
+	bcma_chipco_gpio_polarity(cc, irqs, val & irqs);
+
+	return IRQ_HANDLED;
+}
+
+static int bcma_gpio_irq_domain_init(struct bcma_drv_cc *cc)
+{
+	struct gpio_chip *chip = &cc->gpio;
+	int gpio, hwirq, err;
+
+	cc->irq_domain = irq_domain_add_linear(NULL, chip->ngpio,
+					       &irq_domain_simple_ops, cc);
+	if (!cc->irq_domain) {
+		err = -ENODEV;
+		goto err_irq_domain;
+	}
+	for (gpio = 0; gpio < chip->ngpio; gpio++) {
+		int irq = irq_create_mapping(cc->irq_domain, gpio);
+
+		irq_set_chip_data(irq, cc);
+		irq_set_chip_and_handler(irq, &bcma_gpio_irq_chip,
+					 handle_simple_irq);
+	}
+
+	hwirq = bcma_core_irq(cc->core);
+	err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio",
+			  cc);
+	if (err)
+		goto err_req_irq;
+
+	bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO);
+
+	return 0;
+
+err_req_irq:
+	for (gpio = 0; gpio < chip->ngpio; gpio++) {
+		int irq = irq_find_mapping(cc->irq_domain, gpio);
+
+		irq_dispose_mapping(irq);
+	}
+	irq_domain_remove(cc->irq_domain);
+err_irq_domain:
+	return err;
+}
+
+static void bcma_gpio_irq_domain_exit(struct bcma_drv_cc *cc)
+{
+	struct gpio_chip *chip = &cc->gpio;
+	int gpio;
+
+	bcma_cc_mask32(cc, BCMA_CC_IRQMASK, ~BCMA_CC_IRQ_GPIO);
+	free_irq(bcma_core_irq(cc->core), cc);
+	for (gpio = 0; gpio < chip->ngpio; gpio++) {
+		int irq = irq_find_mapping(cc->irq_domain, gpio);
+
+		irq_dispose_mapping(irq);
+	}
+	irq_domain_remove(cc->irq_domain);
+}
+#else
+static int bcma_gpio_irq_domain_init(struct bcma_drv_cc *cc)
+{
+	return 0;
+}
+
+static void bcma_gpio_irq_domain_exit(struct bcma_drv_cc *cc)
+{
+}
+#endif
+
 int bcma_gpio_init(struct bcma_drv_cc *cc)
 {
 	struct gpio_chip *chip = &cc->gpio;
+	int err;
 
 	chip->label		= "bcma_gpio";
 	chip->owner		= THIS_MODULE;
@@ -95,7 +206,8 @@ int bcma_gpio_init(struct bcma_drv_cc *cc)
 	chip->set		= bcma_gpio_set_value;
 	chip->direction_input	= bcma_gpio_direction_input;
 	chip->direction_output	= bcma_gpio_direction_output;
-	chip->to_irq		= bcma_gpio_to_irq;
+	if (IS_BUILTIN(CONFIG_BCMA_HOST_SOC))
+		chip->to_irq		= bcma_gpio_to_irq;
 	chip->ngpio		= 16;
 	/* There is just one SoC in one device and its GPIO addresses should be
 	 * deterministic to address them more easily. The other buses could get

drivers/bcma/driver_gpio.c int bcma_gpio_init(struct bcma_drv_cc *cc) · line 222

Quoted 7 lines
drivers/bcma/driver_gpio.c +3 -1
@@ -105,10 +217,21 @@ int bcma_gpio_init(struct bcma_drv_cc *cc)
 	else
 		chip->base		= -1;
 
-	return gpiochip_add(chip);
+	err = bcma_gpio_irq_domain_init(cc);
+	if (err)
+		return err;

bcma_gpio_irq_domain_init() should only be called if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC) because bcma_gpio_to_irq() will return -EINVAL for any other type anyway and we will get the error you posted me in jabber.

I do not know how an IRQ in chipcommon on a PCIe card would work and I do not think GPIO irqs will not be used there.

In bcma_core_irq() in drivers/bcma/driver_mips.c we should check if drv_mips.core is not null to prevent any such problems in an additional patch.

Quoted 28 lines
drivers/bcma/driver_gpio.c +9 -0
+
+	err = gpiochip_add(chip);
+	if (err) {
+		bcma_gpio_irq_domain_exit(cc);
+		return err;
+	}
+
+	return 0;
 }
 
 int bcma_gpio_unregister(struct bcma_drv_cc *cc)
 {
+	bcma_gpio_irq_domain_exit(cc);
 	return gpiochip_remove(&cc->gpio);
 }
include/linux/bcma/bcma_driver_chipcommon.h +1 -0
index c49e1a1..63d105c 100644
@@ -640,6 +640,7 @@ struct bcma_drv_cc {
 	spinlock_t gpio_lock;
 #ifdef CONFIG_BCMA_DRIVER_GPIO
 	struct gpio_chip gpio;
+	struct irq_domain *irq_domain;
 #endif
 };
 
author maintainer
#18 lore raw

PATCH v6 1/2 bcma: gpio: add own IRQ domain

In reply to Rafał Miłecki

Input GPIO changes can generate interrupts, but we need kind of ACK for them by changing IRQ polarity. This is required to stop hardware from keep generating interrupts and generate another one on the next GPIO state change.
This code allows using GPIOs with standard interrupts and add for example GPIO buttons support.

---
V3: Apply Hauke's comments.
1) Use IRQ domain for CONFIG_BCMA_HOST_SOC only
2) Optimize bcma_gpio_irq_handler
3) Register GPIO chip after doing everything else
4) Improve cleaning paths

V4: More fixes from Hauke's comments:
1) Less #ifdef CONFIG_BCMA_HOST_SOC
2) Move BCMA_CC_IRQMASK op (and unset on exit)
3) Optimize bcma_gpio_irq_handler

V5: Use IS_BUILTIN

V6: Add IRQ domain on SOC only

Hauke: does it look OK to you?

---
 drivers/bcma/Kconfig                        |    1 +
 drivers/bcma/driver_gpio.c                  |  135 ++++++++++++++++++++++++++-
 include/linux/bcma/bcma_driver_chipcommon.h |    1 +
 3 files changed, 134 insertions(+), 3 deletions(-)
drivers/bcma/Kconfig +1 -0
index 7c081b3..0ee48be 100644
@@ -75,6 +75,7 @@ config BCMA_DRIVER_GMAC_CMN
 config BCMA_DRIVER_GPIO
 	bool "BCMA GPIO driver"
 	depends on BCMA && GPIOLIB
+	select IRQ_DOMAIN if BCMA_HOST_SOC
 	help
 	  Driver to provide access to the GPIO pins of the bcma bus.
 
drivers/bcma/driver_gpio.c +132 -3

174 lines not on this page · raw message

include/linux/bcma/bcma_driver_chipcommon.h +1 -0
index c49e1a1..63d105c 100644
@@ -640,6 +640,7 @@ struct bcma_drv_cc {
 	spinlock_t gpio_lock;
 #ifdef CONFIG_BCMA_DRIVER_GPIO
 	struct gpio_chip gpio;
+	struct irq_domain *irq_domain;
 #endif
 };
 
Signature · 1 line
1.7.10.4
author maintainer
#19 lore raw

PATCH v7 1/2 bcma: gpio: add own IRQ domain

Input GPIO changes can generate interrupts, but we need kind of ACK for them by changing IRQ polarity. This is required to stop hardware from keep generating interrupts and generate another one on the next GPIO state change.
This code allows using GPIOs with standard interrupts and add for example GPIO buttons support.

---
V3: Apply Hauke's comments.
1) Use IRQ domain for CONFIG_BCMA_HOST_SOC only
2) Optimize bcma_gpio_irq_handler
3) Register GPIO chip after doing everything else
4) Improve cleaning paths

V4: More fixes from Hauke's comments:
1) Less #ifdef CONFIG_BCMA_HOST_SOC
2) Move BCMA_CC_IRQMASK op (and unset on exit)
3) Optimize bcma_gpio_irq_handler

V5: Use IS_BUILTIN
V6: Add IRQ domain on SOC only
V7: Change "return 0" to "return"

Hauke: does it look OK to you?

---
 drivers/bcma/Kconfig                        |    1 +
 drivers/bcma/driver_gpio.c                  |  135 ++++++++++++++++++++++++++-
 include/linux/bcma/bcma_driver_chipcommon.h |    1 +
 3 files changed, 134 insertions(+), 3 deletions(-)
drivers/bcma/Kconfig +1 -0
index 7c081b3..0ee48be 100644
@@ -75,6 +75,7 @@ config BCMA_DRIVER_GMAC_CMN
 config BCMA_DRIVER_GPIO
 	bool "BCMA GPIO driver"
 	depends on BCMA && GPIOLIB
+	select IRQ_DOMAIN if BCMA_HOST_SOC
 	help
 	  Driver to provide access to the GPIO pins of the bcma bus.
 
drivers/bcma/driver_gpio.c +132 -3

174 lines not on this page · raw message

include/linux/bcma/bcma_driver_chipcommon.h +1 -0
index c49e1a1..63d105c 100644
@@ -640,6 +640,7 @@ struct bcma_drv_cc {
 	spinlock_t gpio_lock;
 #ifdef CONFIG_BCMA_DRIVER_GPIO
 	struct gpio_chip gpio;
+	struct irq_domain *irq_domain;
 #endif
 };
 
Signature · 1 line
1.7.10.4