Skip to contentSkip to conversation

gpio: generic regmap implementation

PATCH In review Michael Walle 4 participants 8 messages May 28, 2020 – Jun 3, 2020 1 message not in the archive

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

v6 cover gpio: generic regmap implementation

This series is a split off of the sl28cpld series:
https://lore.kernel.org/linux-gpio/[email protected]/

I wasn't sure if I should also include the gpiochip_irqchip_add_domain() patch here. So feel free to skip it. OTOH if you use interrupts with gpio-regmap it is quite handy.

For an actual user see the patch 11/16 ("gpio: add support for the sl28cpld GPIO controller") of the series above.

Changes since v5:
- add names property
- addressed Alex' remarks:
- moved header from i/l/gpio-regmap.h to i/l/gpio/regmap.h
- fixed kernel doc
- described rules how to use the register base offsets
- missing forward declarations
- style changes
- return -EOPNOTSUPP, in code path we should never reach
- added check to have input and output base regs if there is a direction register
- added MAINTAINERS patch

Changes since v4:
- add comment about can_sleep
- fix config->label typo
- add config->names property

Changes since v3:
- set reg_dat_base, that was actually broken
- fix typo
- fix swapped reg_in_dir/reg_out_dir documentation
- use "goto err" in error path in gpio_regmap_register()

Michael Walle (3):
gpiolib: Introduce gpiochip_irqchip_add_domain()
gpio: add a reusable generic gpio_chip using regmap
MAINTAINERS: Add gpio regmap section

 MAINTAINERS                 |   6 +
 drivers/gpio/Kconfig        |   4 +
 drivers/gpio/Makefile       |   1 +
 drivers/gpio/gpio-regmap.c  | 349 ++++++++++++++++++++++++++++++++++++
 drivers/gpio/gpiolib.c      |  20 +++
 include/linux/gpio/driver.h |   3 +
 include/linux/gpio/regmap.h |  86 +++++++++
 7 files changed, 469 insertions(+)
 create mode 100644 drivers/gpio/gpio-regmap.c
 create mode 100644 include/linux/gpio/regmap.h
Signature · 1 line
2.20.1
#1 lore raw

PATCH v6 1/3 gpiolib: Introduce gpiochip_irqchip_add_domain()

The function connects an IRQ domain to a gpiochip and reuses gpiochip_to_irq() which is provided by gpiolib.

gpiochip_irqchip_* and regmap_irq partially provide the same functionality. This function will help to connect just the minimal functionality of the gpiochip_irqchip which is needed to work together with regmap-irq.

---
 drivers/gpio/gpiolib.c      | 20 ++++++++++++++++++++
 include/linux/gpio/driver.h |  3 +++
 2 files changed, 23 insertions(+)
drivers/gpio/gpiolib.c +20 -0
index eaa0e209188d..d07f763c9c0b 100644
@@ -2756,6 +2756,26 @@ int gpiochip_irqchip_add_key(struct gpio_chip *gc,
 }
 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
 
+/**
+ * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
+ * @gc: the gpiochip to add the irqchip to
+ * @domain: the irqdomain to add to the gpiochip
+ *
+ * This function adds an IRQ domain to the gpiochip.
+ */
+int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
+				struct irq_domain *domain)
+{
+	if (!domain)
+		return -EINVAL;
+
+	gc->to_irq = gpiochip_to_irq;
+	gc->irq.domain = domain;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
+
 #else /* CONFIG_GPIOLIB_IRQCHIP */
 
 static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
include/linux/gpio/driver.h +3 -0
index 8c41ae41b6bb..ee30065b6f61 100644
@@ -599,6 +599,9 @@ int gpiochip_irqchip_add_key(struct gpio_chip *gc,
 bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
 				unsigned int offset);
 
+int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
+				struct irq_domain *domain);
+
 #ifdef CONFIG_LOCKDEP
 
 /*
Signature · 1 line
2.20.1
#2 lore raw

PATCH v6 2/3 gpio: add a reusable generic gpio_chip using regmap

In reply to Michael Walle

There are quite a lot simple GPIO controller which are using regmap to access the hardware. This driver tries to be a base to unify existing code into one place. This won't cover everything but it should be a good starting point.

It does not implement its own irq_chip because there is already a generic one for regmap based devices. Instead, the irq_chip will be instantiated in the parent driver and its irq domain will be associate to this driver.

For now it consists of the usual registers, like set (and an optional clear) data register, an input register and direction registers. Out-of-the-box, it supports consecutive register mappings and mappings where the registers have gaps between them with a linear mapping between GPIO offset and bit position. For weirder mappings the user can register its own .xlate().

---
 drivers/gpio/Kconfig        |   4 +
 drivers/gpio/Makefile       |   1 +
 drivers/gpio/gpio-regmap.c  | 349 ++++++++++++++++++++++++++++++++++++
 include/linux/gpio/regmap.h |  86 +++++++++
 4 files changed, 440 insertions(+)
 create mode 100644 drivers/gpio/gpio-regmap.c
 create mode 100644 include/linux/gpio/regmap.h
drivers/gpio/Kconfig +4 -0
index 7d077be10a0f..bcacd9c74aa8 100644
@@ -73,6 +73,10 @@ config GPIO_GENERIC
 	depends on HAS_IOMEM # Only for IOMEM drivers
 	tristate
 
+config GPIO_REGMAP
+	depends on REGMAP
+	tristate
+
 # put drivers in the right section, in alphabetical order
 
 # This symbol is selected by both I2C and SPI expanders
drivers/gpio/Makefile +1 -0
index 65bf3940e33c..1e4894e0bf0f 100644
@@ -12,6 +12,7 @@ obj-$(CONFIG_GPIO_SYSFS)	+= gpiolib-sysfs.o
 obj-$(CONFIG_GPIO_ACPI)		+= gpiolib-acpi.o
 
 # Device drivers. Generally keep list sorted alphabetically
+obj-$(CONFIG_GPIO_REGMAP)	+= gpio-regmap.o
 obj-$(CONFIG_GPIO_GENERIC)	+= gpio-generic.o
 
 # directly supported by gpio-generic
drivers/gpio/gpio-regmap.c new file +349 -0

349 lines not on this page · raw message

include/linux/gpio/regmap.h new file +86 -0

86 lines not on this page · raw message

Signature · 1 line
2.20.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

Re: [PATCH v6 2/3] gpio: add a reusable generic gpio_chip using regmap

Am 2020-05-28 17:55, schrieb Pierre-Louis Bossart:

Quoted 9 lines

drivers/gpio/gpio-regmap.c line 218

Quoted 5 lines

+ /* if we have a direction register we need both input and
output */

drivers/gpio/gpio-regmap.c new file +3 -0
+    if ((config->reg_dir_out_base || config->reg_dir_in_base) &&
+        (!config->reg_dat_base || !config->reg_set_base))
+        return ERR_PTR(-EINVAL);

This failed for me since I didn't have the 'dat' base assigned. I
still can't figure out what 'dat' stands for...I just assigned it to the same offset as the 'set' base but really don't understand what this is supposed to do.

DAT is the data register, aka input register, if the GPIO is in input mode.

If I read the datasheet correctly you should use the following:

PCM512x_GPIO_EN should be reg_dir_out_base
PCM512x_GPIO_CONTROL_1 should be reg_set_base
PCM512x_GPIN should be reg_dat_base

no custom xlate necessary. GPIN looks a bit fishy in that datasheet:
http://www.ti.com/lit/ds/symlink/pcm5121.pdf?ts=1590684141147

PCM512x_GPIO_OUTPUT_1..6 is pinmux control and shouldn't be part of the gpio-regmap. Your driver needs to take care of that.

Quoted 8 lines

drivers/gpio/gpio-regmap.c line 222

Quoted 5 lines
(quoted diff) +2 -0
+
+    /* we don't support having both registers simultaneously for

now */

drivers/gpio/gpio-regmap.c new file +2 -0
+    if (config->reg_dir_out_base && config->reg_dir_in_base)
+        return ERR_PTR(-EINVAL);

and this second test seems to contradict the notion of 'both input and output' above?

dir_out_base is used if the register is high active to select an output. dir_in_base is used for a low active register. Thus both bases are used to switch a GPIO between input and output.

Quoted 12 lines

re-adding comment from previous series:

Quoted 1 line
Quoted 1 line

I still have a series of odd warnings I didn't have before: >> >>

[  101.400263] WARNING: CPU: 3 PID: 1129 at >>

drivers/gpio/gpiolib.c:4084 gpiod_set_value+0x3f/0x50 >> >> This seems
to come from >> /* Should be using gpiod_set_value_cansleep() */

Quoted 1 line
Quoted 1 line
    WARN_ON(desc->gdev->chip->can_sleep); > > Right now,

gpio-regmap hardcodes can_sleep to true. But the only regmap > which don't sleep is regmap-mmio. The PCM512x seems to be either I2C or > SPI, which can both sleep. So this warning is actually correct and > wherever this gpio is set should do it by calling the _cansleep() > version.

Quoted 3 lines

I still have the warnings with this version, not sure if you wanted to fix it in the v6 or is this needs to be fixed in another piece of code/patch. How would we go about removing this warning?

There is no fix in gpio-regmap. wherever this GPIO is connected to must not call gpiod_set_value() but have to use gpiod_set_value_cansleep().

-michael

#5 lore raw

Re: [PATCH v6 2/3] gpio: add a reusable generic gpio_chip using regmap

Quoted 15 lines
Quoted 11 lines
Quoted 7 lines

+    /* if we have a direction register we need both input and output */
+    if ((config->reg_dir_out_base || config->reg_dir_in_base) && +        (!config->reg_dat_base || !config->reg_set_base)) +        return ERR_PTR(-EINVAL);

 This failed for me since I didn't have the 'dat' base assigned. I still can't figure out what 'dat' stands for...I just assigned it to the same offset as the 'set' base but really don't understand what this is supposed to do.

DAT is the data register, aka input register, if the GPIO is in input mode.

Quoted 8 lines

If I read the datasheet correctly you should use the following:

PCM512x_GPIO_EN should be reg_dir_out_base
PCM512x_GPIO_CONTROL_1 should be reg_set_base
PCM512x_GPIN should be reg_dat_base

no custom xlate necessary. GPIN looks a bit fishy in that datasheet:
 http://www.ti.com/lit/ds/symlink/pcm5121.pdf?ts=1590684141147

ok, I updated that part - can't test it though with the boards I have, only output is supported.

Quoted 2 lines

PCM512x_GPIO_OUTPUT_1..6 is pinmux control and shouldn't be part of the gpio-regmap. Your driver needs to take care of that.

yes, that's what I did.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

+
+    /* we don't support having both registers simultaneously for now */
+    if (config->reg_dir_out_base && config->reg_dir_in_base) +        return ERR_PTR(-EINVAL);

Quoted 6 lines
Quoted 2 lines

and this second test seems to contradict the notion of 'both input and output' above?

dir_out_base is used if the register is high active to select an output. dir_in_base is used for a low active register. Thus both bases are used to switch a GPIO between input and output.

ok, makes sense now, thanks for the explanations.

Quoted 12 lines
Quoted 12 lines

re-adding comment from previous series:
 >> I still have a series of odd warnings I didn't have before: >> >> [  101.400263] WARNING: CPU: 3 PID: 1129 at >> drivers/gpio/gpiolib.c:4084 gpiod_set_value+0x3f/0x50 >> >> This seems to come from >>     /* Should be using gpiod_set_value_cansleep() */

Quoted 1 line
Quoted 1 line

    WARN_ON(desc->gdev->chip->can_sleep); > > Right now,

gpio-regmap hardcodes can_sleep to true. But the only regmap > which don't sleep is regmap-mmio. The PCM512x seems to be either I2C or > SPI, which can both sleep. So this warning is actually correct and > wherever this gpio is set should do it by calling the _cansleep() > version.

Quoted 6 lines
Quoted 3 lines

I still have the warnings with this version, not sure if you wanted to fix it in the v6 or is this needs to be fixed in another piece of code/patch. How would we go about removing this warning?

There is no fix in gpio-regmap. wherever this GPIO is connected to must not call gpiod_set_value() but have to use gpiod_set_value_cansleep().

yes, I figured this out and corrected the drivers, works just fine now. My cleaned-up code is at https://github.com/thesofproject/linux/pull/1945, I'll resubmit the v2 based on these patches when they are merged and available in Mark Brown's tree (and I need to address the clock-related feedback).

Thanks for your work Michael, really nice and useful, feel free to add the following tag:

#6 lore raw

PATCH v6 3/3 MAINTAINERS: Add gpio regmap section

In reply to Michael Walle

Add myself as a reviewer for the gpio regmap.

---
 MAINTAINERS | 6 ++++++
 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
index 3a003f310574..f3458debb0c9 100644
@@ -7340,6 +7340,12 @@ S:	Maintained
 F:	drivers/gpio/gpio-mockup.c
 F:	tools/testing/selftests/gpio/
 
+GPIO REGMAP
+R:	Michael Walle <[email protected]>
+S:	Maintained
+F:	drivers/gpio/gpio-regmap.c
+F:	include/linux/gpio/regmap.h
+
 GPIO SUBSYSTEM
 M:	Linus Walleij <[email protected]>
 M:	Bartosz Golaszewski <[email protected]>
Signature · 1 line
2.20.1
Reviewed-by on the v6 series
#7 lore raw
In reply to Michael Walle
Quoted 11 lines · Michael Walle

This series is a split off of the sl28cpld series:
https://lore.kernel.org/linux-gpio/[email protected]/

I wasn't sure if I should also include the gpiochip_irqchip_add_domain() patch here. So feel free to skip it. OTOH if you use interrupts with gpio-regmap it is quite handy.

For an actual user see the patch 11/16 ("gpio: add support for the sl28cpld GPIO controller") of the series above.

v6 looks pretty much good to me, thanks!

Quoted 43 lines

Changes since v5:
- add names property
- addressed Alex' remarks:
- moved header from i/l/gpio-regmap.h to i/l/gpio/regmap.h
- fixed kernel doc
- described rules how to use the register base offsets
- missing forward declarations
- style changes
- return -EOPNOTSUPP, in code path we should never reach
- added check to have input and output base regs if there is a direction register
- added MAINTAINERS patch

Changes since v4:
- add comment about can_sleep
- fix config->label typo
- add config->names property

Changes since v3:
- set reg_dat_base, that was actually broken
- fix typo
- fix swapped reg_in_dir/reg_out_dir documentation
- use "goto err" in error path in gpio_regmap_register()

Michael Walle (3):
gpiolib: Introduce gpiochip_irqchip_add_domain()
gpio: add a reusable generic gpio_chip using regmap
MAINTAINERS: Add gpio regmap section

 MAINTAINERS                 |   6 +
 drivers/gpio/Kconfig        |   4 +
 drivers/gpio/Makefile       |   1 +
 drivers/gpio/gpio-regmap.c  | 349 ++++++++++++++++++++++++++++++++++++
 drivers/gpio/gpiolib.c      |  20 +++
 include/linux/gpio/driver.h |   3 +
 include/linux/gpio/regmap.h |  86 +++++++++
 7 files changed, 469 insertions(+)
 create mode 100644 drivers/gpio/gpio-regmap.c
 create mode 100644 include/linux/gpio/regmap.h

--
2.20.1

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw
In reply to Michael Walle
Quoted 9 lines · Michael Walle

This series is a split off of the sl28cpld series:
https://lore.kernel.org/linux-gpio/[email protected]/

I wasn't sure if I should also include the gpiochip_irqchip_add_domain() patch here. So feel free to skip it. OTOH if you use interrupts with gpio-regmap it is quite handy.

For an actual user see the patch 11/16 ("gpio: add support for the sl28cpld GPIO controller") of the series above.

Patches applied as some of the last stuff before sending my pull requests :)

Let's work with this as a base, and thanks a *LOT* for your work so far!

Yours,
Linus Walleij