Skip to contentSkip to conversation

Migrate the PCIe-IDIO-24 and WS16C48 GPIO drivers to the regmap API

PATCH Superseded William Breathitt Gray 3 participants 14 messages Mar 6, 2023 – Mar 9, 2023

Subsystems (5)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover Migrate the PCIe-IDIO-24 and WS16C48 GPIO drivers to the regmap API

Changes in v4:
- Allocate idio24gpio before using it in idio_24_probe()
Changes in v3:
- Drop map from set_type_config() parameter list; regmap can be passed by irq_drv_data instead
- Adjust idio_24_set_type_config() for parameter list
- Add mutex to prevent clobbering the COS_ENABLE register when masking IRQ and setting their type configuration
Changes in v2:
- Simplify PCIe-IDIO-24 register offset defines to remove superfluous arithmetic
- Check for NULL pointer after chip->irq_drv_data allocation
- Set gpio_regmap drvdata and use gpio_regmap_get_drvdata() to get the regmap in idio_24_reg_map_xlate()

The regmap API supports IO port accessors so we can take advantage of regmap abstractions rather than handling access to the device registers directly in the driver.

A patch to pass irq_drv_data as a parameter for struct regmap_irq_chip set_type_config() is included. This is needed by the idio_24_set_type_config() and ws16c48_set_type_config() callbacks in order to update the type configuration on their respective devices.

A patch to migrate the WS16C48 GPIO driver to the regmap API is included in this series due to its dependence on the struct regmap_irq_chip set_type_config() change.

William Breathitt Gray (3):
regmap: Pass irq_drv_data as a parameter for set_type_config()
gpio: pcie-idio-24: Migrate to the regmap API
gpio: ws16c48: Migrate to the regmap API

 drivers/base/regmap/regmap-irq.c |   8 +-
 drivers/gpio/Kconfig             |   6 +
 drivers/gpio/gpio-pcie-idio-24.c | 710 ++++++++++++-------------------
 drivers/gpio/gpio-ws16c48.c      | 566 +++++++++---------------
 include/linux/regmap.h           |   6 +-
 5 files changed, 497 insertions(+), 799 deletions(-)

base-commit: 0d8b4049bb4792da225e2c908282bb9ed1024ac7

Signature · 1 line
2.39.2
#1 lore raw

PATCH v4 1/3 regmap: Pass irq_drv_data as a parameter for set_type_config()

Allow the struct regmap_irq_chip set_type_config() callback to access irq_drv_data by passing it as a parameter.

---
Changes in v4: none
Changes in v3:
- Drop map from set_type_config() parameter list; regmap can be passed by irq_drv_data instead
Changes in v2: none

 drivers/base/regmap/regmap-irq.c | 8 +++++---
 include/linux/regmap.h           | 6 ++++--
 2 files changed, 9 insertions(+), 5 deletions(-)
drivers/base/regmap/regmap-irq.c +5 -3
index 8c903b8c9714..e489b235b36b 100644
@@ -329,8 +329,8 @@ static int regmap_irq_set_type(struct irq_data *data, unsigned int type)
 	}
 
 	if (d->chip->set_type_config) {
-		ret = d->chip->set_type_config(d->config_buf, type,
-					       irq_data, reg);
+		ret = d->chip->set_type_config(d->config_buf, type, irq_data,
+					       reg, d->chip->irq_drv_data);
 		if (ret)
 			return ret;
 	}
@@ -651,13 +651,15 @@ EXPORT_SYMBOL_GPL(regmap_irq_get_irq_reg_linear);
  * @type: The requested IRQ type.
  * @irq_data: The IRQ being configured.
  * @idx: Index of the irq's config registers within each array `buf[i]`
+ * @irq_drv_data: Driver specific IRQ data
  *
  * This is a &struct regmap_irq_chip->set_type_config callback suitable for
  * chips with one config register. Register values are updated according to
  * the &struct regmap_irq_type data associated with an IRQ.
  */
 int regmap_irq_set_type_config_simple(unsigned int **buf, unsigned int type,
-				      const struct regmap_irq *irq_data, int idx)
+				      const struct regmap_irq *irq_data,
+				      int idx, void *irq_drv_data)
 {
 	const struct regmap_irq_type *t = &irq_data->type;
 
include/linux/regmap.h +4 -2
index 4d10790adeb0..fb92d0ac13af 100644
@@ -1650,7 +1650,8 @@ struct regmap_irq_chip {
 	int (*set_type_virt)(unsigned int **buf, unsigned int type,
 			     unsigned long hwirq, int reg);
 	int (*set_type_config)(unsigned int **buf, unsigned int type,
-			       const struct regmap_irq *irq_data, int idx);
+			       const struct regmap_irq *irq_data, int idx,
+			       void *irq_drv_data);
 	unsigned int (*get_irq_reg)(struct regmap_irq_chip_data *data,
 				    unsigned int base, int index);
 	void *irq_drv_data;
@@ -1659,7 +1660,8 @@ struct regmap_irq_chip {
 unsigned int regmap_irq_get_irq_reg_linear(struct regmap_irq_chip_data *data,
 					   unsigned int base, int index);
 int regmap_irq_set_type_config_simple(unsigned int **buf, unsigned int type,
-				      const struct regmap_irq *irq_data, int idx);
+				      const struct regmap_irq *irq_data,
+				      int idx, void *irq_drv_data);
 
 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
 			int irq_base, const struct regmap_irq_chip *chip,
Signature · 1 line
2.39.2
#2 lore raw

PATCH v4 2/3 gpio: pcie-idio-24: Migrate to the regmap API

In reply to William Breathitt Gray

The regmap API supports IO port accessors so we can take advantage of regmap abstractions rather than handling access to the device registers directly in the driver.

For the PCIe-IDIO-24 series of devices, the following BARs are available:

    BAR[0]: memory mapped PEX8311
    BAR[1]: I/O mapped PEX8311
    BAR[2]: I/O mapped card registers

There are 24 FET Output lines, 24 Isolated Input lines, and 8 TTL/CMOS lines (which may be configured for either output or input). The GPIO lines are exposed by the following card registers:

    Base +0x0-0x2 (Read/Write): FET Outputs
    Base +0xB (Read/Write): TTL/CMOS
    Base +0x4-0x6 (Read): Isolated Inputs
    Base +0x7 (Read): TTL/CMOS

In order for the device to support interrupts, the PLX PEX8311 internal PCI wire interrupt and local interrupt input must first be enabled.

The following card registers for Change-Of-State may be used:

    Base +0x8-0xA (Read): COS Status Inputs
    Base +0x8-0xA (Write): COS Clear Inputs
    Base +0xB (Read): COS Status TTL/CMOS
    Base +0xB (Write): COS Clear TTL/CMOS
    Base +0xE (Read/Write): COS Enable

The COS Enable register is used to enable/disable interrupts and configure the interrupt levels; each bit maps to a group of eight inputs as described below:

Bit 0: IRQ EN Rising Edge IN0-7
Bit 1: IRQ EN Rising Edge IN8-15
Bit 2: IRQ EN Rising Edge IN16-23
Bit 3: IRQ EN Rising Edge TTL0-7
Bit 4: IRQ EN Falling Edge IN0-7
Bit 5: IRQ EN Falling Edge IN8-15
Bit 6: IRQ EN Falling Edge IN16-23
Bit 7: IRQ EN Falling Edge TTL0-7

An interrupt is asserted when a change-of-state matching the interrupt level configuration respective for a particular group of eight inputs with enabled COS is detected.

The COS Status registers may be read to determine which inputs have changed; if interrupts were enabled, an IRQ will be generated for the set bits in these registers. Writing the value read from the COS Status register back to the respective COS Clear register will clear just those interrupts.

Cc Arnaud de Turckheim <[email protected]>
Cc John Hentges <[email protected]>
Cc Jay Dolan <[email protected]>

---
Changes in v4:
- Allocate idio24gpio before using it in idio_24_probe()
Changes in v3:
- Adjust idio_24_set_type_config() for parameter list
- Add mutex to prevent clobbering the COS_ENABLE register when masking IRQ and setting their type configuration
Changes in v2:
- Simplify PCIe-IDIO-24 register offset defines to remove superfluous arithmetic
- Check for NULL pointer after chip->irq_drv_data allocation
- Set gpio_regmap drvdata and use gpio_regmap_get_drvdata() to get the regmap in idio_24_reg_map_xlate()

 drivers/gpio/Kconfig             |   3 +
 drivers/gpio/gpio-pcie-idio-24.c | 710 ++++++++++++-------------------
 2 files changed, 286 insertions(+), 427 deletions(-)
drivers/gpio/Kconfig +3 -0
index 5d0804d58c0a..982e818df94b 100644
@@ -1574,7 +1574,10 @@ config GPIO_PCI_IDIO_16
 
 config GPIO_PCIE_IDIO_24
 	tristate "ACCES PCIe-IDIO-24 GPIO support"
+	select REGMAP_IRQ
+	select REGMAP_MMIO
 	select GPIOLIB_IRQCHIP
+	select GPIO_REGMAP
 	help
 	  Enables GPIO support for the ACCES PCIe-IDIO-24 family (PCIe-IDIO-24,
 	  PCIe-IDI-24, PCIe-IDO-24, PCIe-IDIO-12). An interrupt is generated
drivers/gpio/gpio-pcie-idio-24.c +283 -427

784 lines not on this page · raw message

Signature · 1 line
2.39.2
#3 lore raw

Re: [PATCH v4 2/3] gpio: pcie-idio-24: Migrate to the regmap API

Quoted 53 lines · William Breathitt Gray

The regmap API supports IO port accessors so we can take advantage of regmap abstractions rather than handling access to the device registers directly in the driver.

For the PCIe-IDIO-24 series of devices, the following BARs are available:

    BAR[0]: memory mapped PEX8311
    BAR[1]: I/O mapped PEX8311
    BAR[2]: I/O mapped card registers

There are 24 FET Output lines, 24 Isolated Input lines, and 8 TTL/CMOS lines (which may be configured for either output or input). The GPIO lines are exposed by the following card registers:

    Base +0x0-0x2 (Read/Write): FET Outputs
    Base +0xB (Read/Write): TTL/CMOS
    Base +0x4-0x6 (Read): Isolated Inputs
    Base +0x7 (Read): TTL/CMOS

In order for the device to support interrupts, the PLX PEX8311 internal PCI wire interrupt and local interrupt input must first be enabled.

The following card registers for Change-Of-State may be used:

    Base +0x8-0xA (Read): COS Status Inputs
    Base +0x8-0xA (Write): COS Clear Inputs
    Base +0xB (Read): COS Status TTL/CMOS
    Base +0xB (Write): COS Clear TTL/CMOS
    Base +0xE (Read/Write): COS Enable

The COS Enable register is used to enable/disable interrupts and configure the interrupt levels; each bit maps to a group of eight inputs as described below:

Bit 0: IRQ EN Rising Edge IN0-7
Bit 1: IRQ EN Rising Edge IN8-15
Bit 2: IRQ EN Rising Edge IN16-23
Bit 3: IRQ EN Rising Edge TTL0-7
Bit 4: IRQ EN Falling Edge IN0-7
Bit 5: IRQ EN Falling Edge IN8-15
Bit 6: IRQ EN Falling Edge IN16-23
Bit 7: IRQ EN Falling Edge TTL0-7

An interrupt is asserted when a change-of-state matching the interrupt level configuration respective for a particular group of eight inputs with enabled COS is detected.

The COS Status registers may be read to determine which inputs have changed; if interrupts were enabled, an IRQ will be generated for the set bits in these registers. Writing the value read from the COS Status register back to the respective COS Clear register will clear just those interrupts.

...

Quoted 2 lines

- Add mutex to prevent clobbering the COS_ENABLE register when masking IRQ and setting their type configuration

But this doesn't explain killing the raw spin lock.

I don't understand how is it suppose to work then.

What is this lock for and how IRQ related registers can be updated with RT type of kernel?

If mutex is okay, doesn't mean we have to add 'can_sleep = true'?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Re: [PATCH v4 2/3] gpio: pcie-idio-24: Migrate to the regmap API

Quoted 60 lines · Andy Shevchenko
Quoted 53 lines · William Breathitt Gray

The regmap API supports IO port accessors so we can take advantage of regmap abstractions rather than handling access to the device registers directly in the driver.

For the PCIe-IDIO-24 series of devices, the following BARs are available:

    BAR[0]: memory mapped PEX8311
    BAR[1]: I/O mapped PEX8311
    BAR[2]: I/O mapped card registers

There are 24 FET Output lines, 24 Isolated Input lines, and 8 TTL/CMOS lines (which may be configured for either output or input). The GPIO lines are exposed by the following card registers:

    Base +0x0-0x2 (Read/Write): FET Outputs
    Base +0xB (Read/Write): TTL/CMOS
    Base +0x4-0x6 (Read): Isolated Inputs
    Base +0x7 (Read): TTL/CMOS

In order for the device to support interrupts, the PLX PEX8311 internal PCI wire interrupt and local interrupt input must first be enabled.

The following card registers for Change-Of-State may be used:

    Base +0x8-0xA (Read): COS Status Inputs
    Base +0x8-0xA (Write): COS Clear Inputs
    Base +0xB (Read): COS Status TTL/CMOS
    Base +0xB (Write): COS Clear TTL/CMOS
    Base +0xE (Read/Write): COS Enable

The COS Enable register is used to enable/disable interrupts and configure the interrupt levels; each bit maps to a group of eight inputs as described below:

Bit 0: IRQ EN Rising Edge IN0-7
Bit 1: IRQ EN Rising Edge IN8-15
Bit 2: IRQ EN Rising Edge IN16-23
Bit 3: IRQ EN Rising Edge TTL0-7
Bit 4: IRQ EN Falling Edge IN0-7
Bit 5: IRQ EN Falling Edge IN8-15
Bit 6: IRQ EN Falling Edge IN16-23
Bit 7: IRQ EN Falling Edge TTL0-7

An interrupt is asserted when a change-of-state matching the interrupt level configuration respective for a particular group of eight inputs with enabled COS is detected.

The COS Status registers may be read to determine which inputs have changed; if interrupts were enabled, an IRQ will be generated for the set bits in these registers. Writing the value read from the COS Status register back to the respective COS Clear register will clear just those interrupts.

...

Quoted 2 lines

- Add mutex to prevent clobbering the COS_ENABLE register when masking IRQ and setting their type configuration

Quoted 8 lines

But this doesn't explain killing the raw spin lock.

I don't understand how is it suppose to work then.

What is this lock for and how IRQ related registers can be updated with RT type of kernel?

If mutex is okay, doesn't mean we have to add 'can_sleep = true'?

I admit that RT locking is an area I often have trouble understanding, so perhaps you can help me make sure whether this lock type change makes sense here.

My reasoning for why we need a lock is that the COS Enable register is modified in both the idio_24_set_type_config() function and the idio_24_handle_mask_sync() function. These respectively are called by regmap_irq_set_type() and regmap_irq_sync_unlock(), which are callbacks for a struct irq_chip irq_set_type() and irq_bus_sync_unlock() members.

I think these callbacks could be executed in parrallel, but are allowed to sleep, so that is why I selected mutex as the lock type here (I probably do need to add 'can_sleep = true'). Is this reasoning correct, or have I made a mistake here?

William Breathitt Gray

Reviewed-by on v4 2/3
#5 lore raw

Re: [PATCH v4 2/3] gpio: pcie-idio-24: Migrate to the regmap API

In reply to William Breathitt Gray
Quoted 54 lines

The regmap API supports IO port accessors so we can take advantage of regmap abstractions rather than handling access to the device registers directly in the driver.

For the PCIe-IDIO-24 series of devices, the following BARs are available:

    BAR[0]: memory mapped PEX8311
    BAR[1]: I/O mapped PEX8311
    BAR[2]: I/O mapped card registers

There are 24 FET Output lines, 24 Isolated Input lines, and 8 TTL/CMOS lines (which may be configured for either output or input). The GPIO lines are exposed by the following card registers:

    Base +0x0-0x2 (Read/Write): FET Outputs
    Base +0xB (Read/Write): TTL/CMOS
    Base +0x4-0x6 (Read): Isolated Inputs
    Base +0x7 (Read): TTL/CMOS

In order for the device to support interrupts, the PLX PEX8311 internal PCI wire interrupt and local interrupt input must first be enabled.

The following card registers for Change-Of-State may be used:

    Base +0x8-0xA (Read): COS Status Inputs
    Base +0x8-0xA (Write): COS Clear Inputs
    Base +0xB (Read): COS Status TTL/CMOS
    Base +0xB (Write): COS Clear TTL/CMOS
    Base +0xE (Read/Write): COS Enable

The COS Enable register is used to enable/disable interrupts and configure the interrupt levels; each bit maps to a group of eight inputs as described below:

Bit 0: IRQ EN Rising Edge IN0-7
Bit 1: IRQ EN Rising Edge IN8-15
Bit 2: IRQ EN Rising Edge IN16-23
Bit 3: IRQ EN Rising Edge TTL0-7
Bit 4: IRQ EN Falling Edge IN0-7
Bit 5: IRQ EN Falling Edge IN8-15
Bit 6: IRQ EN Falling Edge IN16-23
Bit 7: IRQ EN Falling Edge TTL0-7

An interrupt is asserted when a change-of-state matching the interrupt level configuration respective for a particular group of eight inputs with enabled COS is detected.

The COS Status registers may be read to determine which inputs have changed; if interrupts were enabled, an IRQ will be generated for the set bits in these registers. Writing the value read from the COS Status register back to the respective COS Clear register will clear just those interrupts.

Quoted 4 lines
Cc Arnaud de Turckheim <[email protected]>
Cc John Hentges <[email protected]>
Cc Jay Dolan <[email protected]>

FWIW, the gpio-regmap part looks good:

-michael

#6 lore raw

PATCH v4 3/3 gpio: ws16c48: Migrate to the regmap API

In reply to William Breathitt Gray

The regmap API supports IO port accessors so we can take advantage of regmap abstractions rather than handling access to the device registers directly in the driver.

The WinSystems WS16C48 provides the following registers:

    Offset 0x0-0x5: Port 0-5 I/O
    Offset 0x6: Int_Pending
    Offset 0x7: Page/Lock
    Offset 0x8-0xA (Page 1): Pol_0-Pol_2
    Offset 0x8-0xA (Page 2): Enab_0-Enab_2
    Offset 0x8-0xA (Page 3): Int_ID0-Int_ID2

Port 0-5 I/O provides access to 48 lines of digital I/O across six registers, each bit position corresponding to the respective line. Writing a 1 to a respective bit position causes that output pin to sink current, while writing a 0 to the same bit position causes that output pin to go to a high-impedance state and allows it to be used an input. Reads on a port report the inverted state (0 = high, 1 = low) of an I/O pin when used in input mode. Interrupts are supported on Port 0-2.

Int_Pending is a read-only register that reports the combined state of the INT_ID0 through INT_ID2 registers; an interrupt pending is indicated when any of the low three bits are set.

The Page/Lock register provides the following bits:

    Bit 0-5: Port 0-5 I/O Lock
    Bit 6-7: Page 0-3 Selection

For Bits 0-5, writing a 1 to a respective bit position locks the output state of the corresponding I/O port. Writing the page number to Bits 6-7 selects that respective register page for use.

Pol_0-Pol_2 are accessible when Page 1 is selected. Writing a 1 to a respective bit position selects the rising edge detection interrupts for that input line, while writing a 0 to the same bit position selects the falling edge detection interrupts.

Enab_0-Enab_2 are accessible when Page 2 is selected. Writing a 1 to a respective bit position enables interrupts for that input line, while writing a 0 to that same bit position clears and disables interrupts for that input line.

Int_ID0-Int_ID2 are accessible when Page 3 is selected. A respective bit when read as a 1 indicates that an edge of the polarity set in the corresponding polarity register was detected for the corresponding input line. Writing any value to this register clears all pending interrupts for the register.

---
Changes in v4: none

 drivers/gpio/Kconfig        |   3 +
 drivers/gpio/gpio-ws16c48.c | 566 +++++++++++++-----------------------
 2 files changed, 202 insertions(+), 367 deletions(-)
drivers/gpio/Kconfig +3 -0
index 982e818df94b..e740665e11ec 100644
@@ -980,7 +980,10 @@ config GPIO_WINBOND
 config GPIO_WS16C48
 	tristate "WinSystems WS16C48 GPIO support"
 	select ISA_BUS_API
+	select REGMAP_IRQ
+	select REGMAP_MMIO
 	select GPIOLIB_IRQCHIP
+	select GPIO_REGMAP
 	help
 	  Enables GPIO support for the WinSystems WS16C48. The base port
 	  addresses for the devices may be configured via the base module
drivers/gpio/gpio-ws16c48.c +199 -367

669 lines not on this page · raw message

Signature · 1 line
2.39.2
#7 lore raw

Re: [PATCH v4 3/3] gpio: ws16c48: Migrate to the regmap API

On Mon, Mar 06, 2023 at 07:59:53AM -0500, William Breathitt Gray wrote:

...

drivers/gpio/gpio-ws16c48.c static unsigned int num_irq; · line 110

Quoted 2 lines
drivers/gpio/gpio-ws16c48.c +1 -1
-	raw_spinlock_t lock;
+	spinlock_t lock;

This is a regression.
That said, do we need a support of raw spin locks in the regmap IRQ?

...

drivers/gpio/gpio-ws16c48.c static unsigned int num_irq; · line 111

Quoted 1 line
drivers/gpio/gpio-ws16c48.c +1 -0
+	u8 irq_mask[WS16C48_NUM_IRQS / WS16C48_NGPIO_PER_REG];

Can this be a bitmap? Or is it too over engineered with it?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw

Re: [PATCH v4 3/3] gpio: ws16c48: Migrate to the regmap API

Quoted 9 lines · Andy Shevchenko

On Mon, Mar 06, 2023 at 07:59:53AM -0500, William Breathitt Gray wrote:

...

drivers/gpio/gpio-ws16c48.c static unsigned int num_irq; · line 110

Quoted 2 lines
drivers/gpio/gpio-ws16c48.c +1 -1
-	raw_spinlock_t lock;
+	spinlock_t lock;

This is a regression.
That said, do we need a support of raw spin locks in the regmap IRQ?

So this code has a similar need as the gpio-pcie-idio-24 patch: guard registers between handle_mask_sync() and set_type_config(); however, now we also need to protect registers in regmap_irq_thread(). We can't use a mutex here because regmap_irq_thread() is executed in an interrupt context so we cannot sleep.

This might be a mistake in my understanding: I chose spinlock_t here because I believed it to map out to a raw_spinlock_t anyway underneath, whereas on RT kernels it would map out to whatever the equivalent is. I suspect this is not actually the case. Would using raw_spinlock_t explicitly be the correct way to go for this particular case?

Quoted 3 lines

drivers/gpio/gpio-ws16c48.c static unsigned int num_irq; · line 111

Quoted 1 line
drivers/gpio/gpio-ws16c48.c +1 -0
+	u8 irq_mask[WS16C48_NUM_IRQS / WS16C48_NGPIO_PER_REG];

Can this be a bitmap? Or is it too over engineered with it?

I also considered a bitmap at first, but I believe it adds an unnecessary abstraction in this particular case: irq_mask is just a buffer to hold the previous mask_buf state to check if it's changed when ws16c48_handle_mask_sync() is called. Since all we do with it is save the mask_buf directly, using the bitmap API seems like overkill.

William Breathitt Gray

#9 lore raw

Re: [PATCH v4 3/3] gpio: ws16c48: Migrate to the regmap API

Quoted 2 lines · William Breathitt Gray
Quoted 1 line · Andy Shevchenko

On Mon, Mar 06, 2023 at 07:59:53AM -0500, William Breathitt Gray wrote:

...

Quoted 12 lines
Quoted 5 lines

drivers/gpio/gpio-ws16c48.c static unsigned int num_irq; · line 110

Quoted 2 lines
drivers/gpio/gpio-ws16c48.c +1 -1
-	raw_spinlock_t lock;
+	spinlock_t lock;

This is a regression.
That said, do we need a support of raw spin locks in the regmap IRQ?

So this code has a similar need as the gpio-pcie-idio-24 patch: guard registers between handle_mask_sync() and set_type_config(); however, now we also need to protect registers in regmap_irq_thread(). We can't use a mutex here because regmap_irq_thread() is executed in an interrupt context so we cannot sleep.

Quoted 5 lines

This might be a mistake in my understanding: I chose spinlock_t here because I believed it to map out to a raw_spinlock_t anyway underneath, whereas on RT kernels it would map out to whatever the equivalent is. I suspect this is not actually the case. Would using raw_spinlock_t explicitly be the correct way to go for this particular case?

You may read the commit message of the 27d9098cff6e ("pinctrl: intel:
Use raw_spinlock for locking"). TL;DR: this is only affects IRQ chips, so if your GPIO controller is _not_ an IRQ chip, you are fine.

WRT the other driver, can_sleep may reduce scope of the use of GPIOs and even make a regression if any consumer don't want that behaviour and currently works.

Quoted 9 lines
Quoted 3 lines

drivers/gpio/gpio-ws16c48.c static unsigned int num_irq; · line 111

Quoted 1 line
drivers/gpio/gpio-ws16c48.c +1 -0
+	u8 irq_mask[WS16C48_NUM_IRQS / WS16C48_NGPIO_PER_REG];

Can this be a bitmap? Or is it too over engineered with it?

I also considered a bitmap at first, but I believe it adds an unnecessary abstraction in this particular case: irq_mask is just a buffer to hold the previous mask_buf state to check if it's changed when ws16c48_handle_mask_sync() is called. Since all we do with it is save the mask_buf directly, using the bitmap API seems like overkill.

Thanks for elaboration!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw

Re: [PATCH v4 3/3] gpio: ws16c48: Migrate to the regmap API

Quoted 24 lines · Andy Shevchenko
Quoted 2 lines · William Breathitt Gray
Quoted 1 line · Andy Shevchenko

On Mon, Mar 06, 2023 at 07:59:53AM -0500, William Breathitt Gray wrote:

...

Quoted 12 lines
Quoted 5 lines

drivers/gpio/gpio-ws16c48.c static unsigned int num_irq; · line 110

Quoted 2 lines
drivers/gpio/gpio-ws16c48.c +1 -1
-	raw_spinlock_t lock;
+	spinlock_t lock;

This is a regression.
That said, do we need a support of raw spin locks in the regmap IRQ?

So this code has a similar need as the gpio-pcie-idio-24 patch: guard registers between handle_mask_sync() and set_type_config(); however, now we also need to protect registers in regmap_irq_thread(). We can't use a mutex here because regmap_irq_thread() is executed in an interrupt context so we cannot sleep.

Quoted 5 lines

This might be a mistake in my understanding: I chose spinlock_t here because I believed it to map out to a raw_spinlock_t anyway underneath, whereas on RT kernels it would map out to whatever the equivalent is. I suspect this is not actually the case. Would using raw_spinlock_t explicitly be the correct way to go for this particular case?

Quoted 7 lines

You may read the commit message of the 27d9098cff6e ("pinctrl: intel:
Use raw_spinlock for locking"). TL;DR: this is only affects IRQ chips, so if your GPIO controller is _not_ an IRQ chip, you are fine.

WRT the other driver, can_sleep may reduce scope of the use of GPIOs and even make a regression if any consumer don't want that behaviour and currently works.

Looking through kernel/irq/manage.c, I see the raw_spinlock desc->lock is taken in __setup_irq() before potentially calling __irq_set_trigger() which ultimate calls the chip->irq_set_type() callback. So it seems unsafe to sleep within at least this callback which is utilized by both drivers, so both gpio-pcie-idio-24 and gpio-ws16c48 will need the raw_spinlock lock type afterall.

I'll make the necessary changes and release a v5 of this patchset.

As an aside, I wonder if locking is not needed if we only utilize the set_type_config() callback, because the desc->lock taken by the irq subsystem will be enough to guard between regmap_irq_set_type() and regmap_irq_thread(). It's not valid for our particular case here because we also utilize a handle_mask_sync() callback (chip_bus_lock() is not protected by desc->chip) but it's something to think about.

William Breathitt Gray

Reviewed-by on v4 3/3
#11 lore raw

Re: [PATCH v4 3/3] gpio: ws16c48: Migrate to the regmap API

In reply to William Breathitt Gray
Quoted 50 lines

The regmap API supports IO port accessors so we can take advantage of regmap abstractions rather than handling access to the device registers directly in the driver.

The WinSystems WS16C48 provides the following registers:

    Offset 0x0-0x5: Port 0-5 I/O
    Offset 0x6: Int_Pending
    Offset 0x7: Page/Lock
    Offset 0x8-0xA (Page 1): Pol_0-Pol_2
    Offset 0x8-0xA (Page 2): Enab_0-Enab_2
    Offset 0x8-0xA (Page 3): Int_ID0-Int_ID2

Port 0-5 I/O provides access to 48 lines of digital I/O across six registers, each bit position corresponding to the respective line. Writing a 1 to a respective bit position causes that output pin to sink current, while writing a 0 to the same bit position causes that output pin to go to a high-impedance state and allows it to be used an input. Reads on a port report the inverted state (0 = high, 1 = low) of an I/O pin when used in input mode. Interrupts are supported on Port 0-2.

Int_Pending is a read-only register that reports the combined state of the INT_ID0 through INT_ID2 registers; an interrupt pending is indicated when any of the low three bits are set.

The Page/Lock register provides the following bits:

    Bit 0-5: Port 0-5 I/O Lock
    Bit 6-7: Page 0-3 Selection

For Bits 0-5, writing a 1 to a respective bit position locks the output state of the corresponding I/O port. Writing the page number to Bits 6-7 selects that respective register page for use.

Pol_0-Pol_2 are accessible when Page 1 is selected. Writing a 1 to a respective bit position selects the rising edge detection interrupts for that input line, while writing a 0 to the same bit position selects the falling edge detection interrupts.

Enab_0-Enab_2 are accessible when Page 2 is selected. Writing a 1 to a respective bit position enables interrupts for that input line, while writing a 0 to that same bit position clears and disables interrupts for that input line.

Int_ID0-Int_ID2 are accessible when Page 3 is selected. A respective bit when read as a 1 indicates that an edge of the polarity set in the corresponding polarity register was detected for the corresponding input line. Writing any value to this register clears all pending interrupts for the register.

Quoted 3 lines

Same here, gpio-regmap part looks good:

-michael

#12 lore raw
In reply to William Breathitt Gray
Quoted 19 lines · William Breathitt Gray

Changes in v4:
- Allocate idio24gpio before using it in idio_24_probe()
Changes in v3:
- Drop map from set_type_config() parameter list; regmap can be passed by irq_drv_data instead
- Adjust idio_24_set_type_config() for parameter list
- Add mutex to prevent clobbering the COS_ENABLE register when masking IRQ and setting their type configuration
Changes in v2:
- Simplify PCIe-IDIO-24 register offset defines to remove superfluous arithmetic
- Check for NULL pointer after chip->irq_drv_data allocation
- Set gpio_regmap drvdata and use gpio_regmap_get_drvdata() to get the regmap in idio_24_reg_map_xlate()

The regmap API supports IO port accessors so we can take advantage of regmap abstractions rather than handling access to the device registers directly in the driver.

Quoted 8 lines

A patch to pass irq_drv_data as a parameter for struct regmap_irq_chip set_type_config() is included. This is needed by the idio_24_set_type_config() and ws16c48_set_type_config() callbacks in order to update the type configuration on their respective devices.

A patch to migrate the WS16C48 GPIO driver to the regmap API is included in this series due to its dependence on the struct regmap_irq_chip set_type_config() change.

I have found nothing WRT lock type changes.
Can you shed a light on what's going on here?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#13 lore raw
Quoted 25 lines · Andy Shevchenko
Quoted 24 lines · William Breathitt Gray

Changes in v4:
- Allocate idio24gpio before using it in idio_24_probe()
Changes in v3:
- Drop map from set_type_config() parameter list; regmap can be passed by irq_drv_data instead
- Adjust idio_24_set_type_config() for parameter list
- Add mutex to prevent clobbering the COS_ENABLE register when masking IRQ and setting their type configuration
Changes in v2:
- Simplify PCIe-IDIO-24 register offset defines to remove superfluous arithmetic
- Check for NULL pointer after chip->irq_drv_data allocation
- Set gpio_regmap drvdata and use gpio_regmap_get_drvdata() to get the regmap in idio_24_reg_map_xlate()

The regmap API supports IO port accessors so we can take advantage of regmap abstractions rather than handling access to the device registers directly in the driver.

A patch to pass irq_drv_data as a parameter for struct regmap_irq_chip set_type_config() is included. This is needed by the idio_24_set_type_config() and ws16c48_set_type_config() callbacks in order to update the type configuration on their respective devices.

Quoted 6 lines
Quoted 3 lines

A patch to migrate the WS16C48 GPIO driver to the regmap API is included in this series due to its dependence on the struct regmap_irq_chip set_type_config() change.

I have found nothing WRT lock type changes.
Can you shed a light on what's going on here?

Previous versions of this patchset had removed the locks entirely. Later, I realized that some locking would be required to prevent clobbering registers when updating IRQ masks and type configurations, so I added in these new locks with types that seemed appropriate for the way they are being used now in the code. I'll explain further in my replies to each patch why I chose these particular types.

William Breathitt Gray