gpio: generic regmap implementation
Subsystems (2)
v4 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 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()
Changes since v2:
See changelog in the former patch series.
Michael Walle (2):
gpiolib: Introduce gpiochip_irqchip_add_domain()
gpio: add a reusable generic gpio_chip using regmap
drivers/gpio/Kconfig | 4 + drivers/gpio/Makefile | 1 + drivers/gpio/gpio-regmap.c | 343 ++++++++++++++++++++++++++++++++++++ drivers/gpio/gpiolib.c | 20 +++ include/linux/gpio-regmap.h | 69 ++++++++ include/linux/gpio/driver.h | 3 + 6 files changed, 440 insertions(+) create mode 100644 drivers/gpio/gpio-regmap.c create mode 100644 include/linux/gpio-regmap.h
Signature · 1 line
2.20.1
PATCH v4 1/2 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
@@ -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
@@ -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
PATCH v4 2/2 gpio: add a reusable generic gpio_chip using regmap
In reply to Michael WalleThere 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 | 343 ++++++++++++++++++++++++++++++++++++ include/linux/gpio-regmap.h | 69 ++++++++ 4 files changed, 417 insertions(+) create mode 100644 drivers/gpio/gpio-regmap.c create mode 100644 include/linux/gpio-regmap.h
drivers/gpio/Kconfig +4 -0
@@ -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
@@ -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 +343 -0
343 lines not on this page · raw message
include/linux/gpio-regmap.h new file +69 -0
69 lines not on this page · raw message
Signature · 1 line
2.20.1
Re: [PATCH v4 2/2] gpio: add a reusable generic gpio_chip using regmap
In reply to a message not in the archiveHi,
Am 2020-05-26 19:29, schrieb Pierre-Louis Bossart:
Quoted 7 lines
Quoted 7 lines
drivers/gpio/gpio-regmap.c new file +7
-0+struct gpio_regmap { +struct device *parent; +struct regmap *regmap; +struct gpio_chip gpio_chip; + +int reg_stride; +int ngpio_per_reg;
Quoted 8 lines
drivers/gpio/gpio-regmap.c line 25
Quoted 5 lines
drivers/gpio/gpio-regmap.c new file +5
-0+unsigned int reg_dat_base; +unsigned int reg_set_base; +unsigned int reg_clr_base; +unsigned int reg_dir_in_base; +unsigned int reg_dir_out_base;I wonder if a base is enough, shouldn't there be a 'last' or something that constrains the range of regmap addresses to be used for gpios?
This should be covered on the regmap, shouldn't it?
Quoted 7 lines
related question since I couldn't figure out how to convert my PCM512x example, where there are 6 GPIOs configured with 3 regmap-visible registers [1], to this mapping.
GPIO_ENdefines if the GPIO is used or not (each bitfield is tied to a GPIO)
Quoted 6 lines
GPIO_CONTROL_1defines the level (each bitfield is tied to a GPIO)GPIO_OUTPUT_1+offset defines what signal is routed to each GPIO. I am really not sure how this part would be handled?
That's 8 registers total to deal with GPIOs.
Looks like you need a custom xlate function:
int pcm512x_gpio_regmap_xlate(struct gpio_regmap *gpio, unsigned int base,
unsigned int offset, unsigned int *reg, unsigned int *mask)
{
switch (base)
case GPIO_EN:
case GPIO_CONTROL_1:
*reg = base;
*mask = (1 << offset);
break;
case GPIO_OUTPUT_1:
*reg = base + offset;
*mask = ...
break;
}base is always one of the xx_base properties in the "struct gpio_regmap_config".
Quoted 37 lines
Quoted 37 lines
+/**
+ * structgpio_regmap_config- Description of a generic regmapgpio_chip.
+ *
+ * @parent: The parent device
+ * @regmap: The regmap used to access the registers
+ * given, the name of the device is used
+ * @label: (Optional) Descriptive name for GPIO controller.
+ * If not given, the name of the device is used.
+ * @ngpio: Number of GPIOs+ * @reg_dat_base: (Optional) (in) register base address + * @reg_set_base: (Optional) set register base address + * @reg_clr_base: (Optional) clear register base address+ * @reg_dir_in_base: (Optional) in setting register base address
+ * @reg_dir_out_base: (Optional) out setting register base
address
+ * @reg_stride: (Optional) May be set if the registers (of
the
+ * same type, dat, set, etc) are not consecutive.
+ * @ngpio_per_reg: Number of GPIOs per register
+ * @irq_domain: (Optional) IRQ domain if the controller is
+ * interrupt-capable
+ * @reg_mask_xlate: (Optional) Translates base address and GPIO+ * offset to a register/bitmask pair. If not + * given the default gpio_regmap_simple_xlate() + * is used.+ *
+ * Thereg_mask_xlatetranslates a given base address and GPIO offset to
+ * register and mask pair. The base address is one of the given reg_*_base.
+ *
+ * All base addresses may have the special valueGPIO_REGMAP_ADDR_ZERO
+ * which forces the address to the value 0.
+ */
+structgpio_regmap_config{
Quoted 8 lines
include/linux/gpio-regmap.h line 46
Quoted 5 lines
include/linux/gpio-regmap.h new file +5
-0+struct device *parent; +struct regmap *regmap; + +const char *label; +int ngpio;could we add a .names field for the
gpio_chip, I found this useful for PCM512x GPIO support, e.g.
Sure, I have the names in the device tree.
But I'd prefer that you'd do a patch on top of this (assuming it is applied soon), because you can actually test it and there might be missing more.
[snip]
-michael
pon., 25 maj 2020 o 18:07 Michael Walle <[email protected]> napisał(a):
Quoted 34 lines
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 v3:
- setreg_dat_base, that was actually broken
- fix typo
- fix swapped reg_in_dir/reg_out_dir documentation
- use "goto err" in error path ingpio_regmap_register()Changes since v2:
See changelog in the former patch series.Michael Walle (2):
gpiolib: Introducegpiochip_irqchip_add_domain()
gpio: add a reusable genericgpio_chipusing regmapdrivers/gpio/Kconfig | 4 + drivers/gpio/Makefile | 1 + drivers/gpio/gpio-regmap.c | 343 ++++++++++++++++++++++++++++++++++++ drivers/gpio/gpiolib.c | 20 +++ include/linux/gpio-regmap.h | 69 ++++++++ include/linux/gpio/driver.h | 3 + 6 files changed, 440 insertions(+) create mode 100644 drivers/gpio/gpio-regmap.c create mode 100644 include/linux/gpio-regmap.h
Quoted 3 lines
--
2.20.1
Both patches look good to me. I'd say we should apply them now and then fix any potential problems during the v5.8 release cycle. It's not like it's a user-space facing, stable interface, we don't have to be super careful when merging this kind of stuff.
Bart
Re: [PATCH v4 2/2] gpio: add a reusable generic gpio_chip using regmap
In reply to Michael WalleHi Michael,
Quoted 10 lines
Quoted 9 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+struct gpio_regmap_config { + struct device *parent; + struct regmap *regmap; + + const char *label; + int ngpio;could we add a .names field for the
gpio_chip, I found this useful for PCM512x GPIO support, e.g.
Quoted 5 lines
Sure, I have the names in the device tree.
But I'd prefer that you'd do a patch on top of this (assuming it is applied soon), because you can actually test it and there might be missing more.
I am happy to report that this gpio-regmap worked like a charm for me, after I applied the minor diff below (complete code at https://github.com/plbossart/sound/tree/fix/regmap-gpios).
I worked around my previous comments by forcing the GPIO internal routing directly in regmap, and that allowed me to only play with the _set and _dir bases. I see the LEDs and clock selected as before, quite nice indeed.
The chip->label test is probably wrong, since the gpio_chip structure is zeroed out if(!chip->label) is always true so the label is always set to the device name. I don't know what the intent was so just removed that test - maybe the correct test should be if (!config->label) ?
I added the names support as well, and btw I don't understand how one would get them through device tree?
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() */ WARN_ON(desc->gdev->chip->can_sleep);
so maybe we need an option here as well? Or use a different function?
Anyways, that gpio-regmap does simplify my code a great deal so thanks for this work, much appreciated. -Pierre
drivers/gpio/gpio-regmap.c +2 -4
@@ -251,10 +251,8 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config chip->ngpio = config->ngpio; chip->can_sleep = true; -if (!chip->label) - chip->label =dev_name (config->parent); -else - chip->label = config->label; + chip->label = config->label; + chip->names = config->names; chip->get = gpio_regmap_get;if (gpio->reg_set_base && gpio->reg_clr_base)
include/linux/gpio-regmap.h +2 -0
@@ -16,6 +16,7 @@ struct gpio_regmap;* given, the name of the device is used * @label: (Optional) Descriptive name for GPIO controller. * If not given, the name of the device is used. +* @names: (Optional) Array of names for gpios * @ngpio: Number of GPIOs * @reg_dat_base: (Optional) (in) register base address * @reg_set_base: (Optional) set register base address
@@ -43,6 +44,7 @@ struct gpio_regmap_config {struct regmap *regmap;const char *label; +const char *const *names;int ngpio;unsigned int reg_dat_base;
Re: [PATCH v4 2/2] gpio: add a reusable generic gpio_chip using regmap
Hi,
Am 2020-05-28 02:31, schrieb Pierre-Louis Bossart:
Quoted 27 lines
Hi Michael,
Quoted 10 lines
Quoted 9 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+struct gpio_regmap_config { + struct device *parent; + struct regmap *regmap; + + const char *label; + int ngpio;could we add a .names field for the
gpio_chip, I found this useful for PCM512x GPIO support, e.g.Quoted 5 lines
Sure, I have the names in the device tree.
But I'd prefer that you'd do a patch on top of this (assuming it is applied soon), because you can actually test it and there might be missing more.
I am happy to report that this gpio-regmap worked like a charm for me, after I applied the minor diff below (complete code at
https://github.com/plbossart/sound/tree/fix/regmap-gpios).I worked around my previous comments by forcing the GPIO internal routing directly in regmap, and that allowed me to only play with the
_setand_dirbases. I see the LEDs and clock selected as before, quite nice indeed.
Quoted 5 lines
The
chip->labeltest is probably wrong, since thegpio_chipstructure is zeroed outif(!chip->label)is always true so the label is always set to the device name. I don't know what the intent was so just removed that test - maybe the correct test should be if (!config->label) ?
yes, that was a typo. should have been if (!config->label).
I've send a v5 with that fix and your names property.
Quoted 2 lines
I added the names support as well, and btw I don't understand how one would get them through device tree?
gpio-line-names property, seeDocumentation/devicetree/bindings/gpio/gpio.txt.
Quoted 8 lines
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:4084gpiod_set_value+0x3f/0x50This seems to come from
/* Should be using gpiod_set_value_cansleep() */ 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 4 lines
so maybe we need an option here as well? Or use a different function?
Anyways, that gpio-regmap does simplify my code a great deal so thanks for this work, much appreciated.
Glad to see that there are more users for it ;)
-michael
Re: [PATCH v4 2/2] gpio: add a reusable generic gpio_chip using regmap
Quoted 4 lines · Michael Walle
Am 2020-05-28 02:31, schrieb Pierre-Louis Bossart:
I've send a v5 with that fix and your names property.
Did you already? Because I have a question why we have gpio-regmap.h instead of gpio/regmap.h ?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 2/2] gpio: add a reusable generic gpio_chip using regmap
Am 2020-05-28 10:37, schrieb Andy Shevchenko:
Quoted 7 lines · Michael Walle
Quoted 4 lines
Am 2020-05-28 02:31, schrieb Pierre-Louis Bossart:
I've send a v5 with that fix and your names property.
Did you already?
Yes, I forgot to add you :(
Quoted 2 lines
Because I have a question why we have
gpio-regmap.hinstead ofgpio/regmap.h?
No particular reason, I thought gpio/ was for low level gpio stuff (like the consumer.h, driver.h etc) or lets say - more integrated stuff.
I don't have a strong opinion. I can send a v6 with that change, on short notice if the maintainers prefer that location.
-michael
Re: [PATCH v4 2/2] gpio: add a reusable generic gpio_chip using regmap
Quoted 11 lines · Michael Walle
Am 2020-05-28 10:37, schrieb Andy Shevchenko:
Quoted 7 lines · Michael Walle
Quoted 4 lines
Am 2020-05-28 02:31, schrieb Pierre-Louis Bossart:
I've send a v5 with that fix and your names property.
Did you already?
Yes, I forgot to add you :(
Quoted 8 lines
Quoted 2 lines
Because I have a question why we have
gpio-regmap.hinstead ofgpio/regmap.h?No particular reason, I thought gpio/ was for low level gpio stuff (like the
consumer.h,driver.hetc) or lets say - more integrated stuff.I don't have a strong opinion. I can send a v6 with that change, on short notice if the maintainers prefer that location.
I guess it's quite close to core and belongs to GPIO family of headers, I definitely would like to see it there, but let's wait for Linus and Bart to speak up.
Signature · 2 lines
With Best Regards, Andy Shevchenko