Skip to contentSkip to conversation

Add support for Kontron sl28cpld

PATCH Superseded Michael Walle 5 participants 38 messages Apr 2, 2020 – Apr 21, 2020

Subsystems (13)

v2 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add support for Kontron sl28cpld

The Kontron sl28cpld is a board management chip providing gpio, pwm, fan monitoring and an interrupt controller. For now this controller is used on the Kontron SMARC-sAL28 board. But because of its flexible nature, it might also be used on other boards in the future. The individual blocks (like gpio, pwm, etc) are kept intentionally small. The MFD core driver then instantiates different (or multiple of the same) blocks. It also provides the register layout so it might be updated in the future without a device tree change; and support other boards with a different layout or functionalities.

See also [1] for more information.

This is my first take of a MFD driver. I don't know whether the subsystem maintainers should only be CCed on the patches which affect the subsystem or on all patches for this series. I've chosen the latter so you can get a more complete picture.

[1] https://lore.kernel.org/linux-devicetree/[email protected]/

Changes since v1:
- use of_match_table in all drivers, needed for automatic module loading, when using OF_MFD_CELL()
- add new gpio-regmap.c which adds a generic regmap gpio_chip implemention
- new patch for reqmap_irq, so we can reuse its implementation
- remove almost any code from gpio-sl28cpld.c, instead use gpio-regmap and regmap-irq
- change the handling of the mfd core vs device tree nodes; add a new property "of_reg" to the mfd_cell struct which, when set, is matched to the unit-address of the device tree nodes.
- fix sl28cpld watchdog when it is not initialized by the bootloader. Explicitly set the operation mode.
- also add support for kontron,assert-wdt-timeout-pin in sl28cpld-wdt.

As suggested by Bartosz Golaszewski:
- define registers as hex
- make gpio enum uppercase
- move parent regmap check before memory allocation
- use device_property_read_bool() instead of the of_ version
- mention the gpio flavors in the bindings documentation

As suggested by Guenter Roeck:
- cleanup #includes and sort them
- use devm_watchdog_register_device()
- use watchdog_stop_on_reboot()
- provide a Documentation/hwmon/sl28cpld.rst
- cleaned up the weird tristate->bool and I2C=y issue. Instead mention that the MFD driver is bool because of the following intc patch
- removed the SL28CPLD_IRQ typo

As suggested by Rob Herring:
- combine all dt bindings docs into one patch
- change the node name for all gpio flavors to "gpio"
- removed the interrupts-extended rule
- cleaned up the unit-address space, see above

Michael Walle (16):
include/linux/ioport.h: add helper to define REG resource constructs
mfd: mfd-core: Don't overwrite the dma_mask of the child device
mfd: mfd-core: match device tree node against reg property
regmap-irq: make it possible to add irq_chip do a specific device node
dt-bindings: mfd: Add bindings for sl28cpld
mfd: Add support for Kontron sl28cpld management controller
irqchip: add sl28cpld interrupt controller support
watchdog: add support for sl28cpld watchdog
pwm: add support for sl28cpld PWM controller
gpio: add a reusable generic gpio_chip using regmap
gpio: add support for the sl28cpld GPIO controller
hwmon: add support for the sl28cpld hardware monitoring controller
arm64: dts: freescale: sl28: enable sl28cpld
arm64: dts: freescale: sl28: map GPIOs to input events
arm64: dts: freescale: sl28: enable LED support
arm64: dts: freescale: sl28: enable fan support

 .../bindings/gpio/kontron,sl28cpld-gpio.yaml  |  51 +++
 .../hwmon/kontron,sl28cpld-hwmon.yaml         |  27 ++
 .../bindings/mfd/kontron,sl28cpld.yaml        | 162 +++++++++
 .../bindings/pwm/kontron,sl28cpld-pwm.yaml    |  35 ++
 .../watchdog/kontron,sl28cpld-wdt.yaml        |  35 ++
 Documentation/hwmon/sl28cpld.rst              |  36 ++
 .../fsl-ls1028a-kontron-kbox-a-230-ls.dts     |  14 +
 .../fsl-ls1028a-kontron-sl28-var3-ads2.dts    |   9 +
 .../freescale/fsl-ls1028a-kontron-sl28.dts    | 124 +++++++
 drivers/base/regmap/regmap-irq.c              |  84 ++++-
 drivers/gpio/Kconfig                          |  15 +
 drivers/gpio/Makefile                         |   2 +
 drivers/gpio/gpio-regmap.c                    | 321 ++++++++++++++++++
 drivers/gpio/gpio-sl28cpld.c                  | 187 ++++++++++
 drivers/hwmon/Kconfig                         |  10 +
 drivers/hwmon/Makefile                        |   1 +
 drivers/hwmon/sl28cpld-hwmon.c                | 152 +++++++++
 drivers/irqchip/Kconfig                       |   3 +
 drivers/irqchip/Makefile                      |   1 +
 drivers/irqchip/irq-sl28cpld.c                |  99 ++++++
 drivers/mfd/Kconfig                           |  21 ++
 drivers/mfd/Makefile                          |   2 +
 drivers/mfd/mfd-core.c                        |  31 +-
 drivers/mfd/sl28cpld.c                        | 154 +++++++++
 drivers/pwm/Kconfig                           |  10 +
 drivers/pwm/Makefile                          |   1 +
 drivers/pwm/pwm-sl28cpld.c                    | 204 +++++++++++
 drivers/watchdog/Kconfig                      |  11 +
 drivers/watchdog/Makefile                     |   1 +
 drivers/watchdog/sl28cpld_wdt.c               | 242 +++++++++++++
 include/linux/gpio-regmap.h                   |  88 +++++
 include/linux/ioport.h                        |   5 +
 include/linux/mfd/core.h                      |  26 +-
 include/linux/regmap.h                        |  10 +
 34 files changed, 2142 insertions(+), 32 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/gpio/kontron,sl28cpld-gpio.yaml
 create mode 100644 Documentation/devicetree/bindings/hwmon/kontron,sl28cpld-hwmon.yaml
 create mode 100644 Documentation/devicetree/bindings/mfd/kontron,sl28cpld.yaml
 create mode 100644 Documentation/devicetree/bindings/pwm/kontron,sl28cpld-pwm.yaml
 create mode 100644 Documentation/devicetree/bindings/watchdog/kontron,sl28cpld-wdt.yaml
 create mode 100644 Documentation/hwmon/sl28cpld.rst
 create mode 100644 drivers/gpio/gpio-regmap.c
 create mode 100644 drivers/gpio/gpio-sl28cpld.c
 create mode 100644 drivers/hwmon/sl28cpld-hwmon.c
 create mode 100644 drivers/irqchip/irq-sl28cpld.c
 create mode 100644 drivers/mfd/sl28cpld.c
 create mode 100644 drivers/pwm/pwm-sl28cpld.c
 create mode 100644 drivers/watchdog/sl28cpld_wdt.c
 create mode 100644 include/linux/gpio-regmap.h
Signature · 1 line
2.20.1
#1 lore raw

PATCH v2 1/16 include/linux/ioport.h: add helper to define REG resource constructs

Similar to the existing helpers, add one for IORESOURCE_REG which comes in handy for most common resource declarations.

---
 include/linux/ioport.h | 5 +++++
 1 file changed, 5 insertions(+)
include/linux/ioport.h +5 -0
index a9b9170b5dd2..cdcceeec0f86 100644
@@ -165,6 +165,11 @@ enum {
 #define DEFINE_RES_MEM(_start, _size)					\
 	DEFINE_RES_MEM_NAMED((_start), (_size), NULL)
 
+#define DEFINE_RES_REG_NAMED(_start, _size, _name)			\
+	DEFINE_RES_NAMED((_start), (_size), (_name), IORESOURCE_REG)
+#define DEFINE_RES_REG(_start, _size)					\
+	DEFINE_RES_REG_NAMED((_start), (_size), NULL)
+
 #define DEFINE_RES_IRQ_NAMED(_irq, _name)				\
 	DEFINE_RES_NAMED((_irq), 1, (_name), IORESOURCE_IRQ)
 #define DEFINE_RES_IRQ(_irq)						\
Signature · 1 line
2.20.1
#2 lore raw

PATCH v2 2/16 mfd: mfd-core: Don't overwrite the dma_mask of the child device

In reply to Michael Walle

Commit cdfee5623290 ("driver core: initialize a default DMA mask for platform device") initialize the DMA of a platform device. But if the parent doesn't have a dma_mask set, for example if it's an I2C device, the dma_mask of the child platform device will be set to zero again. Which leads to many "DMA mask not set" warnings, if the MFD cell has the of_compatible property set.

[    1.877937] sl28cpld-pwm sl28cpld-pwm: DMA mask not set
[    1.883282] sl28cpld-pwm sl28cpld-pwm.0: DMA mask not set
[    1.888795] sl28cpld-gpio sl28cpld-gpio: DMA mask not set

Thus don't overwrite the dma_mask of the children. Instead set the dma_mask of the platform device.

---
 drivers/mfd/mfd-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mfd/mfd-core.c +1 -1
index f5a73af60dd4..e735565969b3 100644
@@ -138,7 +138,7 @@ static int mfd_add_device(struct device *parent, int id,
 
 	pdev->dev.parent = parent;
 	pdev->dev.type = &mfd_dev_type;
-	pdev->dev.dma_mask = parent->dma_mask;
+	pdev->platform_dma_mask = parent->dma_mask ? *parent->dma_mask : 0;
 	pdev->dev.dma_parms = parent->dma_parms;
 	pdev->dev.coherent_dma_mask = parent->coherent_dma_mask;
 
Signature · 1 line
2.20.1
#3 lore raw

PATCH v2 3/16 mfd: mfd-core: match device tree node against reg property

In reply to Michael Walle

There might be multiple children with the device tree compatible, for example if a MFD has multiple instances of the same function. In this case only the first is matched and the other children get a wrong of_node reference.
Add a new option to match also against the unit address of the child node. Additonally, a new helper OF_MFD_CELL_REG is added.

---
 drivers/mfd/mfd-core.c   | 29 ++++++++++++++++++++---------
 include/linux/mfd/core.h | 26 ++++++++++++++++++++------
 2 files changed, 40 insertions(+), 15 deletions(-)
drivers/mfd/mfd-core.c +20 -9
index e735565969b3..4ecb376338f7 100644
@@ -117,6 +117,7 @@ static int mfd_add_device(struct device *parent, int id,
 	struct device_node *np = NULL;
 	int ret = -ENOMEM;
 	int platform_id;
+	u32 of_reg;
 	int r;
 
 	if (id == PLATFORM_DEVID_AUTO)
@@ -151,16 +152,26 @@ static int mfd_add_device(struct device *parent, int id,
 
 	if (parent->of_node && cell->of_compatible) {
 		for_each_child_of_node(parent->of_node, np) {
-			if (of_device_is_compatible(np, cell->of_compatible)) {
-				if (!of_device_is_available(np)) {
-					/* Ignore disabled devices error free */
-					ret = 0;
-					goto fail_alias;
-				}
-				pdev->dev.of_node = np;
-				pdev->dev.fwnode = &np->fwnode;
-				break;
+			if (!of_device_is_compatible(np, cell->of_compatible))
+				continue;
+
+			/* also match the unit address if set */
+			if (cell->of_reg & MFD_OF_REG_VALID) {
+				if (of_property_read_u32(np, "reg", &of_reg))
+					continue;
+				if ((cell->of_reg & MFD_OF_REG_MASK) != of_reg)
+					continue;
 			}
+
+			if (!of_device_is_available(np)) {
+				/* Ignore disabled devices error free */
+				ret = 0;
+				goto fail_alias;
+			}
+
+			pdev->dev.of_node = np;
+			pdev->dev.fwnode = &np->fwnode;
+			break;
 		}
 	}
 
include/linux/mfd/core.h +20 -6
index d01d1299e49d..c2c0ad6b14f3 100644
@@ -13,8 +13,11 @@
 #include <linux/platform_device.h>
 
 #define MFD_RES_SIZE(arr) (sizeof(arr) / sizeof(struct resource))
+#define MFD_OF_REG_VALID	BIT(31)
+#define MFD_OF_REG_MASK		GENMASK(30, 0)
 
-#define MFD_CELL_ALL(_name, _res, _pdata, _pdsize, _id, _compat, _match)\
+#define MFD_CELL_ALL(_name, _res, _pdata, _pdsize, _id, _compat,	\
+		     _of_reg, _match)					\
 	{								\
 		.name = (_name),					\
 		.resources = (_res),					\
@@ -22,24 +25,32 @@
 		.platform_data = (_pdata),				\
 		.pdata_size = (_pdsize),				\
 		.of_compatible = (_compat),				\
+		.of_reg = (_of_reg),					\
 		.acpi_match = (_match),					\
 		.id = (_id),						\
 	}
 
+#define OF_MFD_CELL_REG(_name, _res, _pdata, _pdsize, _id, _compat,	\
+			_of_reg)					\
+	MFD_CELL_ALL(_name, _res, _pdata, _pdsize, _id, _compat,	\
+		     ((_of_reg) | MFD_OF_REG_VALID), NULL)		\
+
 #define OF_MFD_CELL(_name, _res, _pdata, _pdsize,_id, _compat)		\
-	MFD_CELL_ALL(_name, _res, _pdata, _pdsize, _id, _compat, NULL)	\
+	MFD_CELL_ALL(_name, _res, _pdata, _pdsize, _id, _compat,	\
+		     0, NULL)						\
 
 #define ACPI_MFD_CELL(_name, _res, _pdata, _pdsize, _id, _match)	\
-	MFD_CELL_ALL(_name, _res, _pdata, _pdsize, _id, NULL, _match)	\
+	MFD_CELL_ALL(_name, _res, _pdata, _pdsize, _id, NULL, 0,	\
+		     _match)						\
 
 #define MFD_CELL_BASIC(_name, _res, _pdata, _pdsize, _id)		\
-	MFD_CELL_ALL(_name, _res, _pdata, _pdsize, _id, NULL, NULL)	\
+	MFD_CELL_ALL(_name, _res, _pdata, _pdsize, _id, NULL, 0, NULL) \
 
 #define MFD_CELL_RES(_name, _res)					\
-	MFD_CELL_ALL(_name, _res, NULL, 0, 0, NULL, NULL)		\
+	MFD_CELL_ALL(_name, _res, NULL, 0, 0, NULL, 0, NULL)		\
 
 #define MFD_CELL_NAME(_name)						\
-	MFD_CELL_ALL(_name, NULL, NULL, 0, 0, NULL, NULL)		\
+	MFD_CELL_ALL(_name, NULL, NULL, 0, 0, NULL, 0, NULL)		\
 
 struct irq_domain;
 struct property_entry;
@@ -78,6 +89,9 @@ struct mfd_cell {
 	 */
 	const char		*of_compatible;
 
+	/* matching the reg property if set */
+	unsigned int		of_reg;
+
 	/* Matches ACPI */
 	const struct mfd_cell_acpi_match	*acpi_match;
 
Signature · 1 line
2.20.1
#4 lore raw

PATCH v2 4/16 regmap-irq: make it possible to add irq_chip do a specific device node

In reply to Michael Walle

Add a new function regmap_add_irq_chip_np() with its corresponding devm_regmap_add_irq_chip_np() variant. Sometimes one want to register the IRQ domain on a different device node that the one of the regmap node. For example when using a MFD where there are different interrupt controllers and particularly for the generic regmap gpio_chip/irq_chip driver. In this case it is not desireable to have the IRQ domain on the parent node.

---
 drivers/base/regmap/regmap-irq.c | 84 ++++++++++++++++++++++++++------
 include/linux/regmap.h           | 10 ++++
 2 files changed, 78 insertions(+), 16 deletions(-)
drivers/base/regmap/regmap-irq.c +68 -16
index 3d64c9331a82..4340e1d268b6 100644
@@ -541,8 +541,9 @@ static const struct irq_domain_ops regmap_domain_ops = {
 };
 
 /**
- * regmap_add_irq_chip() - Use standard regmap IRQ controller handling
+ * regmap_add_irq_chip_np() - Use standard regmap IRQ controller handling
  *
+ * @np: The device_node where the IRQ domain should be added to.
  * @map: The regmap for the device.
  * @irq: The IRQ the device uses to signal interrupts.
  * @irq_flags: The IRQF_ flags to use for the primary interrupt.
@@ -556,9 +557,10 @@ static const struct irq_domain_ops regmap_domain_ops = {
  * register cache.  The chip driver is responsible for restoring the
  * register values used by the IRQ controller over suspend and resume.
  */
-int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
-			int irq_base, const struct regmap_irq_chip *chip,
-			struct regmap_irq_chip_data **data)
+int regmap_add_irq_chip_np(struct device_node *np, struct regmap *map, int irq,
+			   int irq_flags, int irq_base,
+			   const struct regmap_irq_chip *chip,
+			   struct regmap_irq_chip_data **data)
 {
 	struct regmap_irq_chip_data *d;
 	int i;
@@ -769,12 +771,10 @@ int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
 	}
 
 	if (irq_base)
-		d->domain = irq_domain_add_legacy(map->dev->of_node,
-						  chip->num_irqs, irq_base, 0,
-						  &regmap_domain_ops, d);
+		d->domain = irq_domain_add_legacy(np, chip->num_irqs, irq_base,
+						  0, &regmap_domain_ops, d);
 	else
-		d->domain = irq_domain_add_linear(map->dev->of_node,
-						  chip->num_irqs,
+		d->domain = irq_domain_add_linear(np, chip->num_irqs,
 						  &regmap_domain_ops, d);
 	if (!d->domain) {
 		dev_err(map->dev, "Failed to create IRQ domain\n");
@@ -808,6 +808,30 @@ int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
 	kfree(d);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(regmap_add_irq_chip_np);
+
+/**
+ * regmap_add_irq_chip() - Use standard regmap IRQ controller handling
+ *
+ * @map: The regmap for the device.
+ * @irq: The IRQ the device uses to signal interrupts.
+ * @irq_flags: The IRQF_ flags to use for the primary interrupt.
+ * @irq_base: Allocate at specific IRQ number if irq_base > 0.
+ * @chip: Configuration for the interrupt controller.
+ * @data: Runtime data structure for the controller, allocated on success.
+ *
+ * Returns 0 on success or an errno on failure.
+ *
+ * This is the same as regmap_add_irq_chip_np, except that the device
+ * node of the regmap is used.
+ */
+int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
+			int irq_base, const struct regmap_irq_chip *chip,
+			struct regmap_irq_chip_data **data)
+{
+	return regmap_add_irq_chip_np(map->dev->of_node, map, irq, irq_flags,
+				      irq_base, chip, data);
+}
 EXPORT_SYMBOL_GPL(regmap_add_irq_chip);
 
 /**
@@ -875,9 +899,10 @@ static int devm_regmap_irq_chip_match(struct device *dev, void *res, void *data)
 }
 
 /**
- * devm_regmap_add_irq_chip() - Resource manager regmap_add_irq_chip()
+ * devm_regmap_add_irq_chip_np() - Resource manager regmap_add_irq_chip_np()
  *
  * @dev: The device pointer on which irq_chip belongs to.
+ * @np: The device_node where the IRQ domain should be added to.
  * @map: The regmap for the device.
  * @irq: The IRQ the device uses to signal interrupts
  * @irq_flags: The IRQF_ flags to use for the primary interrupt.
@@ -890,10 +915,11 @@ static int devm_regmap_irq_chip_match(struct device *dev, void *res, void *data)
  * The &regmap_irq_chip_data will be automatically released when the device is
  * unbound.
  */
-int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
-			     int irq_flags, int irq_base,
-			     const struct regmap_irq_chip *chip,
-			     struct regmap_irq_chip_data **data)
+int devm_regmap_add_irq_chip_np(struct device *dev, struct device_node *np,
+				struct regmap *map, int irq, int irq_flags,
+				int irq_base,
+				const struct regmap_irq_chip *chip,
+				struct regmap_irq_chip_data **data)
 {
 	struct regmap_irq_chip_data **ptr, *d;
 	int ret;
@@ -903,8 +929,8 @@ int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
 	if (!ptr)
 		return -ENOMEM;
 
-	ret = regmap_add_irq_chip(map, irq, irq_flags, irq_base,
-				  chip, &d);
+	ret = regmap_add_irq_chip_np(np, map, irq, irq_flags, irq_base,
+				     chip, &d);
 	if (ret < 0) {
 		devres_free(ptr);
 		return ret;
@@ -915,6 +941,32 @@ int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
 	*data = d;
 	return 0;
 }
+EXPORT_SYMBOL_GPL(devm_regmap_add_irq_chip_np);
+
+/**
+ * devm_regmap_add_irq_chip() - Resource manager regmap_add_irq_chip()
+ *
+ * @dev: The device pointer on which irq_chip belongs to.
+ * @map: The regmap for the device.
+ * @irq: The IRQ the device uses to signal interrupts
+ * @irq_flags: The IRQF_ flags to use for the primary interrupt.
+ * @irq_base: Allocate at specific IRQ number if irq_base > 0.
+ * @chip: Configuration for the interrupt controller.
+ * @data: Runtime data structure for the controller, allocated on success
+ *
+ * Returns 0 on success or an errno on failure.
+ *
+ * The &regmap_irq_chip_data will be automatically released when the device is
+ * unbound.
+ */
+int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
+			     int irq_flags, int irq_base,
+			     const struct regmap_irq_chip *chip,
+			     struct regmap_irq_chip_data **data)
+{
+	return devm_regmap_add_irq_chip_np(dev, map->dev->of_node, map, irq,
+					   irq_flags, irq_base, chip, data);
+}
 EXPORT_SYMBOL_GPL(devm_regmap_add_irq_chip);
 
 /**
include/linux/regmap.h +10 -0
index 40b07168fd8e..ae5034b2d7c3 100644
@@ -21,6 +21,7 @@
 struct module;
 struct clk;
 struct device;
+struct device_node;
 struct i2c_client;
 struct i3c_device;
 struct irq_domain;
@@ -1310,12 +1311,21 @@ struct regmap_irq_chip_data;
 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
 			int irq_base, const struct regmap_irq_chip *chip,
 			struct regmap_irq_chip_data **data);
+int regmap_add_irq_chip_np(struct device_node *np, struct regmap *map, int irq,
+			   int irq_flags, int irq_base,
+			   const struct regmap_irq_chip *chip,
+			   struct regmap_irq_chip_data **data);
 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
 
 int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
 			     int irq_flags, int irq_base,
 			     const struct regmap_irq_chip *chip,
 			     struct regmap_irq_chip_data **data);
+int devm_regmap_add_irq_chip_np(struct device *dev, struct device_node *np,
+				struct regmap *map, int irq, int irq_flags,
+				int irq_base,
+				const struct regmap_irq_chip *chip,
+				struct regmap_irq_chip_data **data);
 void devm_regmap_del_irq_chip(struct device *dev, int irq,
 			      struct regmap_irq_chip_data *data);
 
Signature · 1 line
2.20.1
maintainer
#5 lore raw

Applied "regmap-irq: make it possible to add irq_chip do a specific device node" to the regmap tree

The patch

regmap-irq: make it possible to add irq_chip do a specific device node

has been applied to the regmap tree at

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap.git

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark

From 12479382877dcf6623af4676caa8d3c647469a1b Mon Sep 17 00:00:00 2001
From: Michael Walle <[email protected]>
Date: Thu, 2 Apr 2020 22:36:44 +0200
Subject: [PATCH] regmap-irq: make it possible to add irq_chip do a specific
device node

Add a new function regmap_add_irq_chip_np() with its corresponding devm_regmap_add_irq_chip_np() variant. Sometimes one want to register the IRQ domain on a different device node that the one of the regmap node. For example when using a MFD where there are different interrupt controllers and particularly for the generic regmap gpio_chip/irq_chip driver. In this case it is not desireable to have the IRQ domain on the parent node.

---
 drivers/base/regmap/regmap-irq.c | 84 ++++++++++++++++++++++++++------
 include/linux/regmap.h           | 10 ++++
 2 files changed, 78 insertions(+), 16 deletions(-)
drivers/base/regmap/regmap-irq.c +68 -16
index 3d64c9331a82..4340e1d268b6 100644
@@ -541,8 +541,9 @@ static const struct irq_domain_ops regmap_domain_ops = {
 };
 
 /**
- * regmap_add_irq_chip() - Use standard regmap IRQ controller handling
+ * regmap_add_irq_chip_np() - Use standard regmap IRQ controller handling
  *
+ * @np: The device_node where the IRQ domain should be added to.
  * @map: The regmap for the device.
  * @irq: The IRQ the device uses to signal interrupts.
  * @irq_flags: The IRQF_ flags to use for the primary interrupt.
@@ -556,9 +557,10 @@ static const struct irq_domain_ops regmap_domain_ops = {
  * register cache.  The chip driver is responsible for restoring the
  * register values used by the IRQ controller over suspend and resume.
  */
-int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
-			int irq_base, const struct regmap_irq_chip *chip,
-			struct regmap_irq_chip_data **data)
+int regmap_add_irq_chip_np(struct device_node *np, struct regmap *map, int irq,
+			   int irq_flags, int irq_base,
+			   const struct regmap_irq_chip *chip,
+			   struct regmap_irq_chip_data **data)
 {
 	struct regmap_irq_chip_data *d;
 	int i;
@@ -769,12 +771,10 @@ int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
 	}
 
 	if (irq_base)
-		d->domain = irq_domain_add_legacy(map->dev->of_node,
-						  chip->num_irqs, irq_base, 0,
-						  &regmap_domain_ops, d);
+		d->domain = irq_domain_add_legacy(np, chip->num_irqs, irq_base,
+						  0, &regmap_domain_ops, d);
 	else
-		d->domain = irq_domain_add_linear(map->dev->of_node,
-						  chip->num_irqs,
+		d->domain = irq_domain_add_linear(np, chip->num_irqs,
 						  &regmap_domain_ops, d);
 	if (!d->domain) {
 		dev_err(map->dev, "Failed to create IRQ domain\n");
@@ -808,6 +808,30 @@ int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
 	kfree(d);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(regmap_add_irq_chip_np);
+
+/**
+ * regmap_add_irq_chip() - Use standard regmap IRQ controller handling
+ *
+ * @map: The regmap for the device.
+ * @irq: The IRQ the device uses to signal interrupts.
+ * @irq_flags: The IRQF_ flags to use for the primary interrupt.
+ * @irq_base: Allocate at specific IRQ number if irq_base > 0.
+ * @chip: Configuration for the interrupt controller.
+ * @data: Runtime data structure for the controller, allocated on success.
+ *
+ * Returns 0 on success or an errno on failure.
+ *
+ * This is the same as regmap_add_irq_chip_np, except that the device
+ * node of the regmap is used.
+ */
+int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
+			int irq_base, const struct regmap_irq_chip *chip,
+			struct regmap_irq_chip_data **data)
+{
+	return regmap_add_irq_chip_np(map->dev->of_node, map, irq, irq_flags,
+				      irq_base, chip, data);
+}
 EXPORT_SYMBOL_GPL(regmap_add_irq_chip);
 
 /**
@@ -875,9 +899,10 @@ static int devm_regmap_irq_chip_match(struct device *dev, void *res, void *data)
 }
 
 /**
- * devm_regmap_add_irq_chip() - Resource manager regmap_add_irq_chip()
+ * devm_regmap_add_irq_chip_np() - Resource manager regmap_add_irq_chip_np()
  *
  * @dev: The device pointer on which irq_chip belongs to.
+ * @np: The device_node where the IRQ domain should be added to.
  * @map: The regmap for the device.
  * @irq: The IRQ the device uses to signal interrupts
  * @irq_flags: The IRQF_ flags to use for the primary interrupt.
@@ -890,10 +915,11 @@ static int devm_regmap_irq_chip_match(struct device *dev, void *res, void *data)
  * The &regmap_irq_chip_data will be automatically released when the device is
  * unbound.
  */
-int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
-			     int irq_flags, int irq_base,
-			     const struct regmap_irq_chip *chip,
-			     struct regmap_irq_chip_data **data)
+int devm_regmap_add_irq_chip_np(struct device *dev, struct device_node *np,
+				struct regmap *map, int irq, int irq_flags,
+				int irq_base,
+				const struct regmap_irq_chip *chip,
+				struct regmap_irq_chip_data **data)
 {
 	struct regmap_irq_chip_data **ptr, *d;
 	int ret;
@@ -903,8 +929,8 @@ int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
 	if (!ptr)
 		return -ENOMEM;
 
-	ret = regmap_add_irq_chip(map, irq, irq_flags, irq_base,
-				  chip, &d);
+	ret = regmap_add_irq_chip_np(np, map, irq, irq_flags, irq_base,
+				     chip, &d);
 	if (ret < 0) {
 		devres_free(ptr);
 		return ret;
@@ -915,6 +941,32 @@ int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
 	*data = d;
 	return 0;
 }
+EXPORT_SYMBOL_GPL(devm_regmap_add_irq_chip_np);
+
+/**
+ * devm_regmap_add_irq_chip() - Resource manager regmap_add_irq_chip()
+ *
+ * @dev: The device pointer on which irq_chip belongs to.
+ * @map: The regmap for the device.
+ * @irq: The IRQ the device uses to signal interrupts
+ * @irq_flags: The IRQF_ flags to use for the primary interrupt.
+ * @irq_base: Allocate at specific IRQ number if irq_base > 0.
+ * @chip: Configuration for the interrupt controller.
+ * @data: Runtime data structure for the controller, allocated on success
+ *
+ * Returns 0 on success or an errno on failure.
+ *
+ * The &regmap_irq_chip_data will be automatically released when the device is
+ * unbound.
+ */
+int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
+			     int irq_flags, int irq_base,
+			     const struct regmap_irq_chip *chip,
+			     struct regmap_irq_chip_data **data)
+{
+	return devm_regmap_add_irq_chip_np(dev, map->dev->of_node, map, irq,
+					   irq_flags, irq_base, chip, data);
+}
 EXPORT_SYMBOL_GPL(devm_regmap_add_irq_chip);
 
 /**
include/linux/regmap.h +10 -0
index 40b07168fd8e..ae5034b2d7c3 100644
@@ -21,6 +21,7 @@
 struct module;
 struct clk;
 struct device;
+struct device_node;
 struct i2c_client;
 struct i3c_device;
 struct irq_domain;
@@ -1310,12 +1311,21 @@ struct regmap_irq_chip_data;
 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
 			int irq_base, const struct regmap_irq_chip *chip,
 			struct regmap_irq_chip_data **data);
+int regmap_add_irq_chip_np(struct device_node *np, struct regmap *map, int irq,
+			   int irq_flags, int irq_base,
+			   const struct regmap_irq_chip *chip,
+			   struct regmap_irq_chip_data **data);
 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
 
 int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
 			     int irq_flags, int irq_base,
 			     const struct regmap_irq_chip *chip,
 			     struct regmap_irq_chip_data **data);
+int devm_regmap_add_irq_chip_np(struct device *dev, struct device_node *np,
+				struct regmap *map, int irq, int irq_flags,
+				int irq_base,
+				const struct regmap_irq_chip *chip,
+				struct regmap_irq_chip_data **data);
 void devm_regmap_del_irq_chip(struct device *dev, int irq,
 			      struct regmap_irq_chip_data *data);
 
Signature · 1 line
2.20.1
maintainer
#6 lore raw

Re: [PATCH v2 04/16] regmap-irq: make it possible to add irq_chip do a specific device node

In reply to Michael Walle
Quoted 3 lines · Michael Walle

Add a new function regmap_add_irq_chip_np() with its corresponding devm_regmap_add_irq_chip_np() variant. Sometimes one want to register the IRQ domain on a different device node that the one of the regmap

The following changes since commit 8f3d9f354286745c751374f5f1fcafee6b3f3136:

Linux 5.7-rc1 (2020-04-12 12:35:55 -0700)

are available in the Git repository at:

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap.git tags/regmap-irq-np

for you to fetch changes up to 12479382877dcf6623af4676caa8d3c647469a1b:

regmap-irq: make it possible to add irq_chip do a specific device node (2020-04-14 16:21:37 +0100)

----------------------------------------------------------------
regmap: Allow an irqchip to be created for a specific DT node

----------------------------------------------------------------
Michael Walle (1):
regmap-irq: make it possible to add irq_chip do a specific device node

 drivers/base/regmap/regmap-irq.c | 84 ++++++++++++++++++++++++++++++++--------
 include/linux/regmap.h           | 10 +++++
 2 files changed, 78 insertions(+), 16 deletions(-)
#7 lore raw

PATCH v2 5/16 dt-bindings: mfd: Add bindings for sl28cpld

In reply to Michael Walle

This adds device tree bindings for the board management controller found on the Kontron SMARC-sAL28 board.

---
 .../bindings/gpio/kontron,sl28cpld-gpio.yaml  |  51 ++++++
 .../hwmon/kontron,sl28cpld-hwmon.yaml         |  27 +++
 .../bindings/mfd/kontron,sl28cpld.yaml        | 162 ++++++++++++++++++
 .../bindings/pwm/kontron,sl28cpld-pwm.yaml    |  35 ++++
 .../watchdog/kontron,sl28cpld-wdt.yaml        |  35 ++++
 5 files changed, 310 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/gpio/kontron,sl28cpld-gpio.yaml
 create mode 100644 Documentation/devicetree/bindings/hwmon/kontron,sl28cpld-hwmon.yaml
 create mode 100644 Documentation/devicetree/bindings/mfd/kontron,sl28cpld.yaml
 create mode 100644 Documentation/devicetree/bindings/pwm/kontron,sl28cpld-pwm.yaml
 create mode 100644 Documentation/devicetree/bindings/watchdog/kontron,sl28cpld-wdt.yaml
Documentation/devicetree/bindings/gpio/kontron,sl28cpld-gpio.yaml new file +51 -0
new file mode 100644
index 000000000000..291ca116743d
@@ -0,0 +1,51 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/gpio/kontron,sl28cpld-gpio.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: GPIO driver for the sl28cpld board management controller
+
+maintainers:
+  - Michael Walle <[email protected]>
+
+description: |
+  This module is part of the sl28cpld multi-function device. For more
+  details see Documentation/devicetree/bindings/mfd/kontron,sl28cpld.yaml.
+
+  There are three flavors of the GPIO controller, one full featured
+  input/output with interrupt support (kontron,sl28cpld-gpio), one
+  output-only (kontron,sl28-gpo) and one input-only (kontron,sl28-gpi).
+
+  Each controller supports 8 GPIO lines.
+
+properties:
+  compatible:
+    enum:
+      - kontron,sl28cpld-gpio
+      - kontron,sl28cpld-gpi
+      - kontron,sl28cpld-gpo
+
+  reg:
+    maxItems: 1
+
+  "#interrupt-cells":
+    const: 2
+
+  interrupt-controller: true
+
+  "#gpio-cells":
+    const: 2
+
+  gpio-controller: true
+
+  gpio-line-names:
+      minItems: 1
+      maxItems: 8
+
+required:
+  - compatible
+  - "#gpio-cells"
+  - gpio-controller
+
+additionalProperties: false
Documentation/devicetree/bindings/hwmon/kontron,sl28cpld-hwmon.yaml new file +27 -0
new file mode 100644
index 000000000000..1cebd61c6c32
@@ -0,0 +1,27 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hwmon/kontron,sl28cpld-hwmon.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Hardware monitoring driver for the sl28cpld board management controller
+
+maintainers:
+  - Michael Walle <[email protected]>
+
+description: |
+  This module is part of the sl28cpld multi-function device. For more
+  details see Documentation/devicetree/bindings/mfd/kontron,sl28cpld.yaml.
+
+properties:
+  compatible:
+    enum:
+      - kontron,sl28cpld-fan
+
+  reg:
+    maxItems: 1
+
+required:
+  - compatible
+
+additionalProperties: false
Documentation/devicetree/bindings/mfd/kontron,sl28cpld.yaml new file +162 -0

162 lines not on this page · raw message

Documentation/devicetree/bindings/pwm/kontron,sl28cpld-pwm.yaml new file +35 -0
new file mode 100644
index 000000000000..02fe88c30233
@@ -0,0 +1,35 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/pwm/kontron,sl28cpld-pwm.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: PWM driver for the sl28cpld board management controller
+
+maintainers:
+  - Michael Walle <[email protected]>
+
+description: |
+  This module is part of the sl28cpld multi-function device. For more
+  details see Documentation/devicetree/bindings/mfd/kontron,sl28cpld.yaml.
+
+  The controller supports one PWM channel and supports only four distinct
+  frequencies (250Hz, 500Hz, 1kHz, 2kHz).
+
+allOf:
+  - $ref: pwm.yaml#
+
+properties:
+  compatible:
+    const: kontron,sl28cpld-pwm
+
+  reg:
+    maxItems: 1
+
+  "#pwm-cells":
+    const: 2
+
+required:
+  - compatible
+
+additionalProperties: false
Documentation/devicetree/bindings/watchdog/kontron,sl28cpld-wdt.yaml new file +35 -0
new file mode 100644
index 000000000000..dd6559f2973a
@@ -0,0 +1,35 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/watchdog/kontron,sl28cpld-wdt.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Watchdog driver for the sl28cpld board management controller
+
+maintainers:
+  - Michael Walle <[email protected]>
+
+description: |
+  This module is part of the sl28cpld multi-function device. For more
+  details see Documentation/devicetree/bindings/mfd/kontron,sl28cpld.yaml.
+
+allOf:
+  - $ref: watchdog.yaml#
+
+properties:
+  compatible:
+    const: kontron,sl28cpld-wdt
+
+  reg:
+    maxItems: 1
+
+  kontron,assert-wdt-timeout-pin:
+    description: The SMARC standard defines a WDT_TIME_OUT# pin. If this
+      property is set, this output will be pulsed when the watchdog bites
+      and the system resets.
+    type: boolean
+
+required:
+  - compatible
+
+additionalProperties: false
Signature · 1 line
2.20.1
#8 lore raw

PATCH v2 6/16 mfd: Add support for Kontron sl28cpld management controller

In reply to Michael Walle

This patch adds core support for the board management controller found on the SMARC-sAL28 board. It consists of the following functions:
- watchdog
- GPIO controller
- PWM controller
- fan sensor
- interrupt controller

At the moment, this controller is used on the Kontron SMARC-sAL28 board.

Please note that the MFD driver is defined as bool in the Kconfig because the next patch will add interrupt support.

---
 drivers/mfd/Kconfig    |  19 +++++
 drivers/mfd/Makefile   |   2 +
 drivers/mfd/sl28cpld.c | 153 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 174 insertions(+)
 create mode 100644 drivers/mfd/sl28cpld.c
drivers/mfd/Kconfig +19 -0
index 3c547ed575e6..7c6761161f7a 100644
@@ -2059,5 +2059,24 @@ config SGI_MFD_IOC3
 	  If you have an SGI Origin, Octane, or a PCI IOC3 card,
 	  then say Y. Otherwise say N.
 
+config MFD_SL28CPLD
+	bool "Kontron sl28 core driver"
+	depends on I2C=y
+	depends on OF
+	select REGMAP_I2C
+	select MFD_CORE
+	help
+	  This option enables support for the board management controller
+	  found on the Kontron sl28 CPLD. You have to select individual
+	  functions, such as watchdog, GPIO, etc, under the corresponding menus
+	  in order to enable them.
+
+	  Currently supported boards are:
+
+		Kontron SMARC-sAL28
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called sl28cpld.
+
 endmenu
 endif
drivers/mfd/Makefile +2 -0
index f935d10cbf0f..9bc38863b9c7 100644
@@ -259,3 +259,5 @@ obj-$(CONFIG_MFD_ROHM_BD718XX)	+= rohm-bd718x7.o
 obj-$(CONFIG_MFD_STMFX) 	+= stmfx.o
 
 obj-$(CONFIG_SGI_MFD_IOC3)	+= ioc3.o
+
+obj-$(CONFIG_MFD_SL28CPLD)	+= sl28cpld.o
drivers/mfd/sl28cpld.c new file +153 -0

153 lines not on this page · raw message

Signature · 1 line
2.20.1
#9 lore raw

PATCH v2 7/16 irqchip: add sl28cpld interrupt controller support

In reply to Michael Walle

This patch adds support for the interrupt controller inside the sl28 CPLD management controller.

---
 drivers/irqchip/Kconfig        |  3 ++
 drivers/irqchip/Makefile       |  1 +
 drivers/irqchip/irq-sl28cpld.c | 97 ++++++++++++++++++++++++++++++++++
 drivers/mfd/Kconfig            |  2 +
 4 files changed, 103 insertions(+)
 create mode 100644 drivers/irqchip/irq-sl28cpld.c
drivers/irqchip/Kconfig +3 -0
index a85aada04a64..234db932e7cf 100644
@@ -246,6 +246,9 @@ config RENESAS_RZA1_IRQC
 	  Enable support for the Renesas RZ/A1 Interrupt Controller, to use up
 	  to 8 external interrupts with configurable sense select.
 
+config SL28CPLD_INTC
+	bool
+
 config ST_IRQCHIP
 	bool
 	select REGMAP
drivers/irqchip/Makefile +1 -0
index 37bbe39bf909..e3c6b94f7b0a 100644
@@ -107,3 +107,4 @@ obj-$(CONFIG_TI_SCI_INTR_IRQCHIP)	+= irq-ti-sci-intr.o
 obj-$(CONFIG_TI_SCI_INTA_IRQCHIP)	+= irq-ti-sci-inta.o
 obj-$(CONFIG_LOONGSON_LIOINTC)		+= irq-loongson-liointc.o
 obj-$(CONFIG_LOONGSON_HTPIC)		+= irq-loongson-htpic.o
+obj-$(CONFIG_SL28CPLD_INTC)		+= irq-sl28cpld.o
drivers/irqchip/irq-sl28cpld.c new file +97 -0

97 lines not on this page · raw message

drivers/mfd/Kconfig +2 -0
index 7c6761161f7a..4f741d640705 100644
@@ -2064,6 +2064,8 @@ config MFD_SL28CPLD
 	depends on I2C=y
 	depends on OF
 	select REGMAP_I2C
+	select REGMAP_IRQ
+	select SL28CPLD_INTC
 	select MFD_CORE
 	help
 	  This option enables support for the board management controller
Signature · 1 line
2.20.1
#10 lore raw

PATCH v2 8/16 watchdog: add support for sl28cpld watchdog

In reply to Michael Walle

This adds support for the watchdog of the sl28cpld board management controller. This is part of a multi-function device driver.

---
 drivers/watchdog/Kconfig        |  11 ++
 drivers/watchdog/Makefile       |   1 +
 drivers/watchdog/sl28cpld_wdt.c | 242 ++++++++++++++++++++++++++++++++
 3 files changed, 254 insertions(+)
 create mode 100644 drivers/watchdog/sl28cpld_wdt.c
drivers/watchdog/Kconfig +11 -0
index 0663c604bd64..6c53c1d0f348 100644
@@ -340,6 +340,17 @@ config MLX_WDT
 	  To compile this driver as a module, choose M here: the
 	  module will be called mlx-wdt.
 
+config SL28CPLD_WATCHDOG
+	tristate "Kontron sl28 watchdog"
+	depends on MFD_SL28CPLD
+	select WATCHDOG_CORE
+	help
+	  Say Y here to include support for the watchdog timer
+	  on the Kontron sl28 CPLD.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called sl28cpld_wdt.
+
 # ALPHA Architecture
 
 # ARM Architecture
drivers/watchdog/Makefile +1 -0
index 6de2e4ceef19..b9ecdf2d7347 100644
@@ -224,3 +224,4 @@ obj-$(CONFIG_MENF21BMC_WATCHDOG) += menf21bmc_wdt.o
 obj-$(CONFIG_MENZ069_WATCHDOG) += menz69_wdt.o
 obj-$(CONFIG_RAVE_SP_WATCHDOG) += rave-sp-wdt.o
 obj-$(CONFIG_STPMIC1_WATCHDOG) += stpmic1_wdt.o
+obj-$(CONFIG_SL28CPLD_WATCHDOG) += sl28cpld_wdt.o
drivers/watchdog/sl28cpld_wdt.c new file +242 -0

242 lines not on this page · raw message

Signature · 1 line
2.20.1
maintainer
#11 lore raw

Re: [PATCH v2 08/16] watchdog: add support for sl28cpld watchdog

Quoted 124 lines · Michael Walle

This adds support for the watchdog of the sl28cpld board management controller. This is part of a multi-function device driver.

---
 drivers/watchdog/Kconfig        |  11 ++
 drivers/watchdog/Makefile       |   1 +
 drivers/watchdog/sl28cpld_wdt.c | 242 ++++++++++++++++++++++++++++++++
 3 files changed, 254 insertions(+)
 create mode 100644 drivers/watchdog/sl28cpld_wdt.c
drivers/watchdog/Kconfig +11 -0
index 0663c604bd64..6c53c1d0f348 100644
@@ -340,6 +340,17 @@ config MLX_WDT
 	  To compile this driver as a module, choose M here: the
 	  module will be called mlx-wdt.
 
+config SL28CPLD_WATCHDOG
+	tristate "Kontron sl28 watchdog"
+	depends on MFD_SL28CPLD
+	select WATCHDOG_CORE
+	help
+	  Say Y here to include support for the watchdog timer
+	  on the Kontron sl28 CPLD.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called sl28cpld_wdt.
+
 # ALPHA Architecture
 
 # ARM Architecture
drivers/watchdog/Makefile +1 -0
index 6de2e4ceef19..b9ecdf2d7347 100644
@@ -224,3 +224,4 @@ obj-$(CONFIG_MENF21BMC_WATCHDOG) += menf21bmc_wdt.o
 obj-$(CONFIG_MENZ069_WATCHDOG) += menz69_wdt.o
 obj-$(CONFIG_RAVE_SP_WATCHDOG) += rave-sp-wdt.o
 obj-$(CONFIG_STPMIC1_WATCHDOG) += stpmic1_wdt.o
+obj-$(CONFIG_SL28CPLD_WATCHDOG) += sl28cpld_wdt.o
drivers/watchdog/sl28cpld_wdt.c new file +76 -0
new file mode 100644
index 000000000000..79a7e36217a6
@@ -0,0 +1,242 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * sl28cpld watchdog driver.
+ *
+ * Copyright 2019 Kontron Europe GmbH
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/watchdog.h>
+
+/*
+ * Watchdog timer block registers.
+ */
+#define WDT_CTRL			0x00
+#define  WDT_CTRL_EN			BIT(0)
+#define  WDT_CTRL_LOCK			BIT(2)
+#define  WDT_CTRL_ASSERT_SYS_RESET	BIT(6)
+#define  WDT_CTRL_ASSERT_WDT_TIMEOUT	BIT(7)
+#define WDT_TIMEOUT			0x01
+#define WDT_KICK			0x02
+#define  WDT_KICK_VALUE			0x6b
+#define WDT_COUNT			0x03
+
+static bool nowayout = WATCHDOG_NOWAYOUT;
+module_param(nowayout, bool, 0);
+MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
+				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
+
+static int timeout;
+module_param(timeout, int, 0);
+MODULE_PARM_DESC(timeout, "Initial watchdog timeout in seconds");
+
+struct sl28cpld_wdt {
+	struct watchdog_device wdd;
+	struct regmap *regmap;
+	u32 offset;
+	bool assert_wdt_timeout;
+};
+
+static int sl28cpld_wdt_ping(struct watchdog_device *wdd)
+{
+	struct sl28cpld_wdt *wdt = watchdog_get_drvdata(wdd);
+
+	return regmap_write(wdt->regmap, wdt->offset + WDT_KICK,
+			    WDT_KICK_VALUE);
+}
+
+static int sl28cpld_wdt_start(struct watchdog_device *wdd)
+{
+	struct sl28cpld_wdt *wdt = watchdog_get_drvdata(wdd);
+	unsigned int val;
+
+	val = WDT_CTRL_EN | WDT_CTRL_ASSERT_SYS_RESET;
+	if (wdt->assert_wdt_timeout)
+		val |= WDT_CTRL_ASSERT_WDT_TIMEOUT;
+	if (nowayout)
+		val |= WDT_CTRL_LOCK;
+
+	return regmap_update_bits(wdt->regmap, wdt->offset + WDT_CTRL,
+				  val, val);
+}
+
+static int sl28cpld_wdt_stop(struct watchdog_device *wdd)
+{
+	struct sl28cpld_wdt *wdt = watchdog_get_drvdata(wdd);
+
+	return regmap_update_bits(wdt->regmap, wdt->offset + WDT_CTRL,
+				  WDT_CTRL_EN, 0);
+}
+
+static unsigned int sl28cpld_wdt_status(struct watchdog_device *wdd)
+{

drivers/watchdog/sl28cpld_wdt.c line 86

Quoted 10 lines
drivers/watchdog/sl28cpld_wdt.c new file +10 -0
+	struct sl28cpld_wdt *wdt = watchdog_get_drvdata(wdd);
+	unsigned int status;
+	int ret;
+
+	ret = regmap_read(wdt->regmap, wdt->offset + WDT_CTRL, &status);
+	if (ret < 0)
+		return 0;
+
+	/* is the watchdog timer running? */
+	return (status & WDT_CTRL_EN) << WDOG_ACTIVE;

This is really bad coding style. It uses the fact that WDT_CTRL_EN is at bit position 0 and sets WDOG_ACTIVE accordingly.

But that it is wrong, not even considering the coding style problem. The status function is supposed to return WDIOF_ bits. What it returns if the watchdog is running is WDOG_ACTIVE, or BIT(0), which is then reported to userspace as WDIOF_OVERHEAT.

Quoted 10 lines
drivers/watchdog/sl28cpld_wdt.c new file +10 -0
+}
+
+static unsigned int sl28cpld_wdt_get_timeleft(struct watchdog_device *wdd)
+{
+	struct sl28cpld_wdt *wdt = watchdog_get_drvdata(wdd);
+	int ret;
+	unsigned int val;
+
+	ret = regmap_read(wdt->regmap, wdt->offset + WDT_COUNT, &val);
+	if (ret < 0)

drivers/watchdog/sl28cpld_wdt.c line 106

Quoted 10 lines
drivers/watchdog/sl28cpld_wdt.c new file +10 -0
+		return 0;
+
+	return val;
+}
+
+static int sl28cpld_wdt_set_timeout(struct watchdog_device *wdd,
+				  unsigned int timeout)
+{
+	int ret;
+	struct sl28cpld_wdt *wdt = watchdog_get_drvdata(wdd);

Nit: Reverse christmas tree order looks a bit nicer.

drivers/watchdog/sl28cpld_wdt.c line 109

Quoted 3 lines
drivers/watchdog/sl28cpld_wdt.c new file +3 -0
+
+	ret = regmap_write(wdt->regmap, wdt->offset + WDT_TIMEOUT, timeout);
+	if (ret == 0)

Please run checkpatch --strict and fix this and the reported alignment problem.

Quoted 49 lines
drivers/watchdog/sl28cpld_wdt.c new file +49 -0
+		wdd->timeout = timeout;
+
+	return ret;
+}
+
+static const struct watchdog_info sl28cpld_wdt_info = {
+	.options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
+	.identity = "SMARC-sAL28 CPLD watchdog",
+};
+
+static struct watchdog_ops sl28cpld_wdt_ops = {
+	.owner = THIS_MODULE,
+	.start = sl28cpld_wdt_start,
+	.stop = sl28cpld_wdt_stop,
+	.status = sl28cpld_wdt_status,
+	.ping = sl28cpld_wdt_ping,
+	.set_timeout = sl28cpld_wdt_set_timeout,
+	.get_timeleft = sl28cpld_wdt_get_timeleft,
+};
+
+static int sl28cpld_wdt_locked(struct sl28cpld_wdt *wdt)
+{
+	unsigned int val;
+	int ret;
+
+	ret = regmap_read(wdt->regmap, wdt->offset + WDT_CTRL, &val);
+	if (ret < 0)
+		return ret;
+
+	return val & WDT_CTRL_LOCK;
+}
+
+static int sl28cpld_wdt_probe(struct platform_device *pdev)
+{
+	struct sl28cpld_wdt *wdt;
+	struct watchdog_device *wdd;
+	struct resource *res;
+	unsigned int val;
+	int ret;
+
+	if (!pdev->dev.parent)
+		return -ENODEV;
+
+	wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
+	if (!wdt)
+		return -ENOMEM;
+
+	wdt->regmap = dev_get_regmap(pdev->dev.parent, NULL);
+	if (!wdt->regmap)

drivers/watchdog/sl28cpld_wdt.c line 168

Quoted 10 lines
drivers/watchdog/sl28cpld_wdt.c new file +10 -0
+		return -ENODEV;
+
+	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
+	if (res == NULL)
+		return -EINVAL;
+	wdt->offset = res->start;
+
+	if (device_property_read_bool(&pdev->dev,
+				      "kontron,assert-wdt-timeout-pin"))
+		wdt->assert_wdt_timeout = true;

This might be simpler written as

	wdt->assert_wdt_timeout = device_property_read_bool(...);
Quoted 22 lines
drivers/watchdog/sl28cpld_wdt.c new file +22 -0
+
+	/* initialize struct watchdog_device */
+	wdd = &wdt->wdd;
+	wdd->parent = &pdev->dev;
+	wdd->info = &sl28cpld_wdt_info;
+	wdd->ops = &sl28cpld_wdt_ops;
+	wdd->min_timeout = 1;
+	wdd->max_timeout = 255;
+
+	watchdog_set_drvdata(wdd, wdt);
+
+	/* if the watchdog is locked, we set nowayout to true */
+	ret = sl28cpld_wdt_locked(wdt);
+	if (ret < 0)
+		return ret;
+	if (ret)
+		nowayout = true;
+	watchdog_set_nowayout(wdd, nowayout);
+
+	/*
+	 * Initial timeout value, can either be set by kernel parameter or by
+	 * the device tree. If both are not given the current value is used.

drivers/watchdog/sl28cpld_wdt.c line 200

Quoted 10 lines
drivers/watchdog/sl28cpld_wdt.c new file +10 -0
+	 */
+	watchdog_init_timeout(wdd, timeout, &pdev->dev);
+	if (wdd->timeout) {
+		sl28cpld_wdt_set_timeout(wdd, wdd->timeout);
+	} else {
+		ret = regmap_read(wdt->regmap, wdt->offset + WDT_TIMEOUT,
+				  &val);
+		if (ret < 0)
+			return ret;
+		wdd->timeout = val;

Oddly enough that can result in a timeout of 0 if that is what the chip reports. Are you sure that is acceptable ?

drivers/watchdog/sl28cpld_wdt.c line 204

Quoted 4 lines
drivers/watchdog/sl28cpld_wdt.c new file +4 -0
+	}
+
+	watchdog_stop_on_reboot(wdd);
+	ret = devm_watchdog_register_device(&pdev->dev, wdd);

This does not inform the watchdog core if the watchdog is already active, even though that is clearly supported. You might want to consider setting WDOG_HW_RUNNING in that case.

drivers/watchdog/sl28cpld_wdt.c line 211

Quoted 7 lines
drivers/watchdog/sl28cpld_wdt.c new file +7 -0
+	if (ret < 0) {
+		dev_err(&pdev->dev, "failed to register watchdog device\n");
+		return ret;
+	}
+
+	platform_set_drvdata(pdev, wdt);
+

I don't see where this is used.

Quoted 31 lines
drivers/watchdog/sl28cpld_wdt.c new file +31 -0
+	dev_info(&pdev->dev, "CPLD watchdog: initial timeout %d sec%s\n",
+		wdd->timeout, nowayout ? ", nowayout" : "");
+
+	return 0;
+}
+
+static const struct of_device_id sl28cpld_wdt_of_match[] = {
+	{ .compatible = "kontron,sl28cpld-wdt" },
+	{},
+};
+MODULE_DEVICE_TABLE(of, sl28cpld_wdt_of_match);
+
+static const struct platform_device_id sl28cpld_wdt_id_table[] = {
+	{ "sl28cpld-wdt" },
+	{},
+};
+MODULE_DEVICE_TABLE(platform, sl28cpld_wdt_id_table);
+
+static struct platform_driver sl28cpld_wdt_driver = {
+	.probe = sl28cpld_wdt_probe,
+	.id_table = sl28cpld_wdt_id_table,
+	.driver = {
+		.name = KBUILD_MODNAME,
+		.of_match_table = sl28cpld_wdt_of_match,
+	},
+};
+module_platform_driver(sl28cpld_wdt_driver);
+
+MODULE_DESCRIPTION("sl28cpld Watchdog Driver");
+MODULE_AUTHOR("Michael Walle <[email protected]>");
+MODULE_LICENSE("GPL");
#12 lore raw

PATCH v2 9/16 pwm: add support for sl28cpld PWM controller

In reply to Michael Walle

This adds support for the PWM controller of the sl28cpld board management controller. This is part of a multi-function device driver.

---
 drivers/pwm/Kconfig        |  10 ++
 drivers/pwm/Makefile       |   1 +
 drivers/pwm/pwm-sl28cpld.c | 203 +++++++++++++++++++++++++++++++++++++
 3 files changed, 214 insertions(+)
 create mode 100644 drivers/pwm/pwm-sl28cpld.c
drivers/pwm/Kconfig +10 -0
index eebbc917ac97..5673e5e8b0c3 100644
@@ -427,6 +427,16 @@ config PWM_SIFIVE
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-sifive.
 
+config PWM_SL28CPLD
+	tristate "Kontron sl28 PWM support"
+	depends on MFD_SL28CPLD
+	help
+	  Generic PWM framework driver for board management controller
+	  found on the Kontron sl28 CPLD.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-sl28cpld.
+
 config PWM_SPEAR
 	tristate "STMicroelectronics SPEAr PWM support"
 	depends on PLAT_SPEAR || COMPILE_TEST
drivers/pwm/Makefile +1 -0
index 9a475073dafc..2c2b569dcde9 100644
@@ -40,6 +40,7 @@ obj-$(CONFIG_PWM_RENESAS_TPU)	+= pwm-renesas-tpu.o
 obj-$(CONFIG_PWM_ROCKCHIP)	+= pwm-rockchip.o
 obj-$(CONFIG_PWM_SAMSUNG)	+= pwm-samsung.o
 obj-$(CONFIG_PWM_SIFIVE)	+= pwm-sifive.o
+obj-$(CONFIG_PWM_SL28CPLD)	+= pwm-sl28cpld.o
 obj-$(CONFIG_PWM_SPEAR)		+= pwm-spear.o
 obj-$(CONFIG_PWM_SPRD)		+= pwm-sprd.o
 obj-$(CONFIG_PWM_STI)		+= pwm-sti.o
drivers/pwm/pwm-sl28cpld.c new file +203 -0

203 lines not on this page · raw message

Signature · 1 line
2.20.1
#13 lore raw

PATCH v2 10/16 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 instanciated 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  | 320 ++++++++++++++++++++++++++++++++++++
 include/linux/gpio-regmap.h |  88 ++++++++++
 4 files changed, 413 insertions(+)
 create mode 100644 drivers/gpio/gpio-regmap.c
 create mode 100644 include/linux/gpio-regmap.h
drivers/gpio/Kconfig +4 -0
index 1b96169d84f7..a8e148f4b2e0 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 b2cfc21a97f3..93e139fdfa57 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 +320 -0

320 lines not on this page · raw message

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

88 lines not on this page · raw message

Signature · 1 line
2.20.1
#14 lore raw

Re: [PATCH v2 10/16] gpio: add a reusable generic gpio_chip using regmap

czw., 2 kwi 2020 o 22:37 Michael Walle <[email protected]> napisał(a):

Quoted 11 lines

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 instanciated in the parent driver and its irq domain will be associate to this driver.

Quoted 8 lines

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().

Hi Michael,

Thanks for doing this! When looking at other generic drivers:
gpio-mmio and gpio-reg I can see there are some corner-cases and more specific configuration options we could add but it's not a blocker, we'll probably be extending this one as we convert more drivers to using it. Personally I'd love to see gpio-mmio and gpio-reg removed and replaced by a single, generic regmap interface eventually.

Quoted 68 lines
---
 drivers/gpio/Kconfig        |   4 +
 drivers/gpio/Makefile       |   1 +
 drivers/gpio/gpio-regmap.c  | 320 ++++++++++++++++++++++++++++++++++++
 include/linux/gpio-regmap.h |  88 ++++++++++
 4 files changed, 413 insertions(+)
 create mode 100644 drivers/gpio/gpio-regmap.c
 create mode 100644 include/linux/gpio-regmap.h
drivers/gpio/Kconfig +4 -0
index 1b96169d84f7..a8e148f4b2e0 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 b2cfc21a97f3..93e139fdfa57 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 +26 -0
new file mode 100644
index 000000000000..cc4437dc0521
@@ -0,0 +1,320 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * regmap based generic GPIO driver
+ *
+ * Copyright 2019 Michael Walle <[email protected]>
+ */
+
+#include <linux/gpio/driver.h>
+#include <linux/gpio-regmap.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/regmap.h>
+
+struct gpio_regmap_data {
+       struct gpio_chip gpio_chip;
+       struct gpio_regmap *gpio;
+};
+
+/**
+ * gpio_regmap_simple_xlate() - translate base/offset to reg/mask
+ *
+ * Use a simple linear mapping to translate the offset to the bitmask.
+ */
+int gpio_regmap_simple_xlate(struct gpio_regmap *gpio, unsigned int base,
+                            unsigned int offset,
+                            unsigned int *reg, unsigned int *mask)

drivers/gpio/gpio-regmap.c line 36

Quoted 10 lines
drivers/gpio/gpio-regmap.c new file +10 -0
+{
+       unsigned int line = offset % gpio->ngpio_per_reg;
+       unsigned int stride = offset / gpio->ngpio_per_reg;
+
+       *reg = base + stride * gpio->reg_stride;
+       *mask = BIT(line);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(gpio_regmap_simple_xlate);

Why does this need to be exported?

drivers/gpio/gpio-regmap.c line 43

Quoted 7 lines
drivers/gpio/gpio-regmap.c new file +7 -0
+
+static int gpio_regmap_get(struct gpio_chip *chip, unsigned int offset)
+{
+       struct gpio_regmap_data *data = gpiochip_get_data(chip);
+       struct gpio_regmap *gpio = data->gpio;
+       unsigned int base;
+       unsigned int val, reg, mask;

This can fit on a single line with base. Same elsewhere.

Quoted 283 lines
drivers/gpio/gpio-regmap.c new file +277 -0
+       int ret;
+
+       /* we might not have an output register if we are input only */
+       if (gpio->reg_dat_base.valid)
+               base = gpio->reg_dat_base.addr;
+       else
+               base = gpio->reg_set_base.addr;
+
+       ret = gpio->reg_mask_xlate(gpio, base, offset, &reg, &mask);
+       if (ret)
+               return ret;
+
+       ret = regmap_read(gpio->regmap, reg, &val);
+       if (ret)
+               return ret;
+
+       return (val & mask) ? 1 : 0;
+}
+
+static void gpio_regmap_set(struct gpio_chip *chip, unsigned int offset,
+                           int val)
+{
+       struct gpio_regmap_data *data = gpiochip_get_data(chip);
+       struct gpio_regmap *gpio = data->gpio;
+       unsigned int base = gpio->reg_set_base.addr;
+       unsigned int reg, mask;
+
+       gpio->reg_mask_xlate(gpio, base, offset, &reg, &mask);
+       if (val)
+               regmap_update_bits(gpio->regmap, reg, mask, mask);
+       else
+               regmap_update_bits(gpio->regmap, reg, mask, 0);
+}
+
+static void gpio_regmap_set_with_clear(struct gpio_chip *chip,
+                                      unsigned int offset, int val)
+{
+       struct gpio_regmap_data *data = gpiochip_get_data(chip);
+       struct gpio_regmap *gpio = data->gpio;
+       unsigned int base;
+       unsigned int reg, mask;
+
+       if (val)
+               base = gpio->reg_set_base.addr;
+       else
+               base = gpio->reg_clr_base.addr;
+
+       gpio->reg_mask_xlate(gpio, base, offset, &reg, &mask);
+       regmap_write(gpio->regmap, reg, mask);
+}
+
+static int gpio_regmap_get_direction(struct gpio_chip *chip,
+                                    unsigned int offset)
+{
+       struct gpio_regmap_data *data = gpiochip_get_data(chip);
+       struct gpio_regmap *gpio = data->gpio;
+       unsigned int val, reg, mask;
+       unsigned int base;
+       int invert;
+       int ret;
+
+       if (gpio->reg_dir_out_base.valid) {
+               base = gpio->reg_dir_out_base.addr;
+               invert = 0;
+       } else if (gpio->reg_dir_in_base.valid) {
+               base = gpio->reg_dir_in_base.addr;
+               invert = 1;
+       } else {
+               return GPIO_LINE_DIRECTION_IN;
+       }
+
+       ret = gpio->reg_mask_xlate(gpio, base, offset, &reg, &mask);
+       if (ret)
+               return ret;
+
+       ret = regmap_read(gpio->regmap, reg, &val);
+       if (ret)
+               return ret;
+
+       if (!!(val & mask) ^ invert)
+               return GPIO_LINE_DIRECTION_OUT;
+       else
+               return GPIO_LINE_DIRECTION_IN;
+}
+
+static int gpio_regmap_set_direction(struct gpio_chip *chip,
+                                    unsigned int offset, bool output)
+{
+       struct gpio_regmap_data *data = gpiochip_get_data(chip);
+       struct gpio_regmap *gpio = data->gpio;
+       unsigned int val, reg, mask;
+       unsigned int base;
+       int invert;
+       int ret;
+
+       if (gpio->reg_dir_out_base.valid) {
+               base = gpio->reg_dir_out_base.addr;
+               invert = 0;
+       } else if (gpio->reg_dir_in_base.valid) {
+               base = gpio->reg_dir_in_base.addr;
+               invert = 1;
+       } else {
+               return 0;
+       }
+
+       ret = gpio->reg_mask_xlate(gpio, base, offset, &reg, &mask);
+       if (ret)
+               return ret;
+
+       if (!invert)
+               val = (output) ? mask : 0;
+       else
+               val = (output) ? 0 : mask;
+
+       return regmap_update_bits(gpio->regmap, reg, mask, val);
+}
+
+static int gpio_regmap_direction_input(struct gpio_chip *chip,
+                                      unsigned int offset)
+{
+       return gpio_regmap_set_direction(chip, offset, false);
+}
+
+static int gpio_regmap_direction_output(struct gpio_chip *chip,
+                                       unsigned int offset, int value)
+{
+       gpio_regmap_set(chip, offset, value);
+       return gpio_regmap_set_direction(chip, offset, true);
+}
+
+static int gpio_regmap_to_irq(struct gpio_chip *chip, unsigned int offset)
+{
+       struct gpio_regmap_data *data = gpiochip_get_data(chip);
+       struct gpio_regmap *gpio = data->gpio;
+
+       /* the user might have its own .to_irq callback */
+       if (gpio->to_irq)
+               return gpio->to_irq(gpio, offset);
+
+       return irq_create_mapping(gpio->irq_domain, offset);
+}
+
+/**
+ * gpio_regmap_register() - Register a generic regmap GPIO controller
+ *
+ * @gpio: gpio_regmap device to register
+ *
+ * Returns 0 on success or an errno on failure.
+ */
+int gpio_regmap_register(struct gpio_regmap *gpio)
+{
+       struct gpio_regmap_data *d;
+       struct gpio_chip *chip;
+       int ret;
+
+       if (!gpio->parent)
+               return -EINVAL;
+
+       if (!gpio->ngpio)
+               return -EINVAL;
+
+       /* we need at least one */
+       if (!gpio->reg_dat_base.valid && !gpio->reg_set_base.valid)
+               return -EINVAL;
+
+       /* we don't support having both registers simulaniously for now */
+       if (gpio->reg_dir_out_base.valid && gpio->reg_dir_in_base.valid)
+               return -EINVAL;
+
+       /* if not set, assume they are consecutive */
+       if (!gpio->reg_stride)
+               gpio->reg_stride = 1;
+
+       /* if not set, assume there is only one register */
+       if (!gpio->ngpio_per_reg)
+               gpio->ngpio_per_reg = gpio->ngpio;
+
+       if (!gpio->reg_mask_xlate)
+               gpio->reg_mask_xlate = gpio_regmap_simple_xlate;
+
+       d = kzalloc(sizeof(*d), GFP_KERNEL);
+       if (!d)
+               return -ENOMEM;
+
+       gpio->data = d;
+       d->gpio = gpio;
+
+       chip = &d->gpio_chip;
+       chip->parent = gpio->parent;
+       chip->label = gpio->label;
+       chip->base = -1;
+       chip->ngpio = gpio->ngpio;
+       chip->can_sleep = true;
+       chip->get = gpio_regmap_get;
+
+       if (!chip->label)
+               chip->label = dev_name(gpio->parent);
+
+       if (gpio->reg_set_base.valid && gpio->reg_clr_base.valid)
+               chip->set = gpio_regmap_set_with_clear;
+       else if (gpio->reg_set_base.valid)
+               chip->set = gpio_regmap_set;
+
+       if (gpio->reg_dir_in_base.valid || gpio->reg_dir_out_base.valid) {
+               chip->get_direction = gpio_regmap_get_direction;
+               chip->direction_input = gpio_regmap_direction_input;
+               chip->direction_output = gpio_regmap_direction_output;
+       }
+
+       if (gpio->irq_domain)
+               chip->to_irq = gpio_regmap_to_irq;
+
+       ret = gpiochip_add_data(chip, d);
+       if (ret < 0)
+               goto err_alloc;
+
+       return 0;
+
+err_alloc:
+       kfree(d);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(gpio_regmap_register);
+
+/**
+ * gpio_regmap_unregister() - Unregister a generic regmap GPIO controller
+ *
+ * @gpio: gpio_regmap device to unregister
+ */
+void gpio_regmap_unregister(struct gpio_regmap *gpio)
+{
+       gpiochip_remove(&gpio->data->gpio_chip);
+       kfree(gpio->data);
+}
+EXPORT_SYMBOL_GPL(gpio_regmap_unregister);
+
+static void devm_gpio_regmap_unregister(struct device *dev, void *res)
+{
+       gpio_regmap_unregister(*(struct gpio_regmap **)res);
+}
+
+/**
+ * devm_gpio_regmap_register() - resource managed gpio_regmap_register()
+ *
+ * @dev: device that is registering this GPIO device
+ * @gpio: gpio_regmap device to register
+ *
+ * Managed gpio_regmap_register(). For generic regmap GPIO device registered by
+ * this function, gpio_regmap_unregister() is automatically called on driver
+ * detach. See gpio_regmap_register() for more information.
+ */
+int devm_gpio_regmap_register(struct device *dev, struct gpio_regmap *gpio)
+{
+       struct gpio_regmap **ptr;
+       int ret;
+
+       ptr = devres_alloc(devm_gpio_regmap_unregister, sizeof(*ptr),
+                          GFP_KERNEL);
+       if (!ptr)
+               return -ENOMEM;
+
+       ret = gpio_regmap_register(gpio);
+       if (ret) {
+               devres_free(ptr);
+               return ret;
+       }
+
+       *ptr = gpio;
+       devres_add(dev, ptr);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(devm_gpio_regmap_register);
+
+MODULE_AUTHOR("Michael Walle <[email protected]>");
+MODULE_DESCRIPTION("GPIO generic regmap driver core");
+MODULE_LICENSE("GPL");
include/linux/gpio-regmap.h new file +0 -0
new file mode 100644
index 000000000000..ad63955e0e43

include/linux/gpio-regmap.h line 9

Quoted 9 lines
include/linux/gpio-regmap.h new file +9 -0
@@ -0,0 +1,88 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef _LINUX_GPIO_REGMAP_H
+#define _LINUX_GPIO_REGMAP_H
+
+struct gpio_regmap_addr {
+       unsigned int addr;
+       bool valid;
+};

I'm not quite sure what the meaning behind the valid field here is. When would we potentially set it to false?

include/linux/gpio-regmap.h line 17

Quoted 8 lines
include/linux/gpio-regmap.h new file +8 -0
+#define GPIO_REGMAP_ADDR(_addr) \
+       ((struct gpio_regmap_addr) { .addr = _addr, .valid = true })
+
+/**
+ * struct gpio_regmap - Description of a generic regmap gpio_chip.
+ *
+ * @parent:            The parent device
+ * @regmap:            The regmap use to access the registers

s/use/used/

Quoted 18 lines
include/linux/gpio-regmap.h new file +18 -0
+ *                     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) out setting register base address
+ * @reg_dir_out_base:  (Optional) in 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.

include/linux/gpio-regmap.h line 45

Quoted 10 lines
include/linux/gpio-regmap.h new file +10 -0
+ * @to_irq:            (Optional) Maps GPIO offset to a irq number.
+ *                     By default assumes a linear mapping of the
+ *                     given irq_domain.
+ * @driver_data:       Pointer to the drivers private data. Not used by
+ *                     gpio-regmap.
+ *
+ * The reg_mask_xlate translates a given base address and GPIO offset to
+ * register and mask pair. The base address is one of the given reg_*_base.
+ */
+struct gpio_regmap {

I'd prefer to follow a pattern seen in other such APIs of calling this structure gpio_regmap_config and creating another private structure called gpio_regmap used in callbacks that would only contain necessary fields.

Quoted 44 lines
include/linux/gpio-regmap.h new file +43 -1
+       struct device *parent;
+       struct regmap *regmap;
+       struct gpio_regmap_data *data;
+
+       const char *label;
+       int ngpio;
+
+       struct gpio_regmap_addr reg_dat_base;
+       struct gpio_regmap_addr reg_set_base;
+       struct gpio_regmap_addr reg_clr_base;
+       struct gpio_regmap_addr reg_dir_in_base;
+       struct gpio_regmap_addr reg_dir_out_base;
+       int reg_stride;
+       int ngpio_per_reg;
+       struct irq_domain *irq_domain;
+
+       int (*reg_mask_xlate)(struct gpio_regmap *gpio, unsigned int base,
+                             unsigned int offset, unsigned int *reg,
+                             unsigned int *mask);
+       int (*to_irq)(struct gpio_regmap *gpio, unsigned int offset);
+
+       void *driver_data;
+};
+
+static inline void gpio_regmap_set_drvdata(struct gpio_regmap *gpio,
+                                          void *data)
+{
+       gpio->driver_data = data;
+}
+
+static inline void *gpio_regmap_get_drvdata(struct gpio_regmap *gpio)
+{
+       return gpio->driver_data;
+}
+
+int gpio_regmap_register(struct gpio_regmap *gpio);
+void gpio_regmap_unregister(struct gpio_regmap *gpio);
+int devm_gpio_regmap_register(struct device *dev, struct gpio_regmap *gpio);
+int gpio_regmap_simple_xlate(struct gpio_regmap *gpio, unsigned int base,
+                            unsigned int offset,
+                            unsigned int *reg, unsigned int *mask);
+
+#endif /* _LINUX_GPIO_REGMAP_H */
--
Quoted 2 lines

2.20.1

Overall looks really nice. I'm happy we'll have it in v5.8.

Best regards,
Bartosz Golaszewski

#15 lore raw

Re: [PATCH v2 10/16] gpio: add a reusable generic gpio_chip using regmap

Hi Bartosz, Hi Mark Brown,

Am 2020-04-06 09:47, schrieb Bartosz Golaszewski:

Quoted 19 lines

czw., 2 kwi 2020 o 22:37 Michael Walle <[email protected]> napisał(a):

Quoted 18 lines

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 instanciated 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().

Quoted 7 lines
Quoted 1 line

Hi Michael,

Thanks for doing this! When looking at other generic drivers:
gpio-mmio and gpio-reg I can see there are some corner-cases and more specific configuration options we could add

I didn't want to copy every bit without being able to test it.

Quoted 3 lines

but it's not a blocker,
we'll probably be extending this one as we convert more drivers to using it.

correct, that was also my plan.

Quoted 2 lines

Personally I'd love to see gpio-mmio and gpio-reg removed and replaced by a single, generic regmap interface eventually.

agreed.

Quoted 75 lines
Quoted 75 lines
---
 drivers/gpio/Kconfig        |   4 +
 drivers/gpio/Makefile       |   1 +
 drivers/gpio/gpio-regmap.c  | 320 
++++++++++++++++++++++++++++++++++++
 include/linux/gpio-regmap.h |  88 ++++++++++
 4 files changed, 413 insertions(+)
 create mode 100644 drivers/gpio/gpio-regmap.c
 create mode 100644 include/linux/gpio-regmap.h
drivers/gpio/Kconfig +4 -0
index 1b96169d84f7..a8e148f4b2e0 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 b2cfc21a97f3..93e139fdfa57 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 +22 -0
new file mode 100644
index 000000000000..cc4437dc0521
@@ -0,0 +1,320 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * regmap based generic GPIO driver
+ *
+ * Copyright 2019 Michael Walle <[email protected]>
+ */
+
+#include <linux/gpio/driver.h>
+#include <linux/gpio-regmap.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/regmap.h>
+
+struct gpio_regmap_data {
+       struct gpio_chip gpio_chip;
+       struct gpio_regmap *gpio;
+};
+
+/**
+ * gpio_regmap_simple_xlate() - translate base/offset to reg/mask
+ *
+ * Use a simple linear mapping to translate the offset to the 

bitmask.
+ */
+int gpio_regmap_simple_xlate(struct gpio_regmap *gpio, unsigned int base,

drivers/gpio/gpio-regmap.c new file +6 -0
+                            unsigned int offset,
+                            unsigned int *reg, unsigned int *mask)
+{
+       unsigned int line = offset % gpio->ngpio_per_reg;
+       unsigned int stride = offset / gpio->ngpio_per_reg;
+
Quoted 8 lines

drivers/gpio/gpio-regmap.c line 36

Quoted 6 lines
drivers/gpio/gpio-regmap.c new file +6 -0
+       *reg = base + stride * gpio->reg_stride;
+       *mask = BIT(line);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(gpio_regmap_simple_xlate);

Why does this need to be exported?

Mh, the idea was that a user could also set this xlate() by himself (for whatever reason). But since it is the default, it is not really necessary. That being said, I don't care if its only local to this module.

Quoted 311 lines

drivers/gpio/gpio-regmap.c line 43

Quoted 8 lines
drivers/gpio/gpio-regmap.c new file +2 -0
+
+static int gpio_regmap_get(struct gpio_chip *chip, unsigned int 

offset)

drivers/gpio/gpio-regmap.c new file +5 -0
+{
+       struct gpio_regmap_data *data = gpiochip_get_data(chip);
+       struct gpio_regmap *gpio = data->gpio;
+       unsigned int base;
+       unsigned int val, reg, mask;

This can fit on a single line with base. Same elsewhere.

Quoted 300 lines
drivers/gpio/gpio-regmap.c new file +3 -0
+       int ret;
+
+       /* we might not have an output register if we are input only 

*/

drivers/gpio/gpio-regmap.c new file +17 -0
+       if (gpio->reg_dat_base.valid)
+               base = gpio->reg_dat_base.addr;
+       else
+               base = gpio->reg_set_base.addr;
+
+       ret = gpio->reg_mask_xlate(gpio, base, offset, &reg, &mask);
+       if (ret)
+               return ret;
+
+       ret = regmap_read(gpio->regmap, reg, &val);
+       if (ret)
+               return ret;
+
+       return (val & mask) ? 1 : 0;
+}
+
+static void gpio_regmap_set(struct gpio_chip *chip, unsigned int 

offset,

drivers/gpio/gpio-regmap.c new file +105 -0
+                           int val)
+{
+       struct gpio_regmap_data *data = gpiochip_get_data(chip);
+       struct gpio_regmap *gpio = data->gpio;
+       unsigned int base = gpio->reg_set_base.addr;
+       unsigned int reg, mask;
+
+       gpio->reg_mask_xlate(gpio, base, offset, &reg, &mask);
+       if (val)
+               regmap_update_bits(gpio->regmap, reg, mask, mask);
+       else
+               regmap_update_bits(gpio->regmap, reg, mask, 0);
+}
+
+static void gpio_regmap_set_with_clear(struct gpio_chip *chip,
+                                      unsigned int offset, int val)
+{
+       struct gpio_regmap_data *data = gpiochip_get_data(chip);
+       struct gpio_regmap *gpio = data->gpio;
+       unsigned int base;
+       unsigned int reg, mask;
+
+       if (val)
+               base = gpio->reg_set_base.addr;
+       else
+               base = gpio->reg_clr_base.addr;
+
+       gpio->reg_mask_xlate(gpio, base, offset, &reg, &mask);
+       regmap_write(gpio->regmap, reg, mask);
+}
+
+static int gpio_regmap_get_direction(struct gpio_chip *chip,
+                                    unsigned int offset)
+{
+       struct gpio_regmap_data *data = gpiochip_get_data(chip);
+       struct gpio_regmap *gpio = data->gpio;
+       unsigned int val, reg, mask;
+       unsigned int base;
+       int invert;
+       int ret;
+
+       if (gpio->reg_dir_out_base.valid) {
+               base = gpio->reg_dir_out_base.addr;
+               invert = 0;
+       } else if (gpio->reg_dir_in_base.valid) {
+               base = gpio->reg_dir_in_base.addr;
+               invert = 1;
+       } else {
+               return GPIO_LINE_DIRECTION_IN;
+       }
+
+       ret = gpio->reg_mask_xlate(gpio, base, offset, &reg, &mask);
+       if (ret)
+               return ret;
+
+       ret = regmap_read(gpio->regmap, reg, &val);
+       if (ret)
+               return ret;
+
+       if (!!(val & mask) ^ invert)
+               return GPIO_LINE_DIRECTION_OUT;
+       else
+               return GPIO_LINE_DIRECTION_IN;
+}
+
+static int gpio_regmap_set_direction(struct gpio_chip *chip,
+                                    unsigned int offset, bool output)
+{
+       struct gpio_regmap_data *data = gpiochip_get_data(chip);
+       struct gpio_regmap *gpio = data->gpio;
+       unsigned int val, reg, mask;
+       unsigned int base;
+       int invert;
+       int ret;
+
+       if (gpio->reg_dir_out_base.valid) {
+               base = gpio->reg_dir_out_base.addr;
+               invert = 0;
+       } else if (gpio->reg_dir_in_base.valid) {
+               base = gpio->reg_dir_in_base.addr;
+               invert = 1;
+       } else {
+               return 0;
+       }
+
+       ret = gpio->reg_mask_xlate(gpio, base, offset, &reg, &mask);
+       if (ret)
+               return ret;
+
+       if (!invert)
+               val = (output) ? mask : 0;
+       else
+               val = (output) ? 0 : mask;
+
+       return regmap_update_bits(gpio->regmap, reg, mask, val);
+}
+
+static int gpio_regmap_direction_input(struct gpio_chip *chip,
+                                      unsigned int offset)
+{
+       return gpio_regmap_set_direction(chip, offset, false);
+}
+
+static int gpio_regmap_direction_output(struct gpio_chip *chip,
+                                       unsigned int offset, int 

value)

drivers/gpio/gpio-regmap.c new file +6 -0
+{
+       gpio_regmap_set(chip, offset, value);
+       return gpio_regmap_set_direction(chip, offset, true);
+}
+
+static int gpio_regmap_to_irq(struct gpio_chip *chip, unsigned int 

offset)

drivers/gpio/gpio-regmap.c new file +35 -0
+{
+       struct gpio_regmap_data *data = gpiochip_get_data(chip);
+       struct gpio_regmap *gpio = data->gpio;
+
+       /* the user might have its own .to_irq callback */
+       if (gpio->to_irq)
+               return gpio->to_irq(gpio, offset);
+
+       return irq_create_mapping(gpio->irq_domain, offset);
+}
+
+/**
+ * gpio_regmap_register() - Register a generic regmap GPIO controller
+ *
+ * @gpio: gpio_regmap device to register
+ *
+ * Returns 0 on success or an errno on failure.
+ */
+int gpio_regmap_register(struct gpio_regmap *gpio)
+{
+       struct gpio_regmap_data *d;
+       struct gpio_chip *chip;
+       int ret;
+
+       if (!gpio->parent)
+               return -EINVAL;
+
+       if (!gpio->ngpio)
+               return -EINVAL;
+
+       /* we need at least one */
+       if (!gpio->reg_dat_base.valid && !gpio->reg_set_base.valid)
+               return -EINVAL;
+
+       /* we don't support having both registers simulaniously for 

now */
+ if (gpio->reg_dir_out_base.valid &&
gpio->reg_dir_in_base.valid)

drivers/gpio/gpio-regmap.c new file +37 -0
+               return -EINVAL;
+
+       /* if not set, assume they are consecutive */
+       if (!gpio->reg_stride)
+               gpio->reg_stride = 1;
+
+       /* if not set, assume there is only one register */
+       if (!gpio->ngpio_per_reg)
+               gpio->ngpio_per_reg = gpio->ngpio;
+
+       if (!gpio->reg_mask_xlate)
+               gpio->reg_mask_xlate = gpio_regmap_simple_xlate;
+
+       d = kzalloc(sizeof(*d), GFP_KERNEL);
+       if (!d)
+               return -ENOMEM;
+
+       gpio->data = d;
+       d->gpio = gpio;
+
+       chip = &d->gpio_chip;
+       chip->parent = gpio->parent;
+       chip->label = gpio->label;
+       chip->base = -1;
+       chip->ngpio = gpio->ngpio;
+       chip->can_sleep = true;
+       chip->get = gpio_regmap_get;
+
+       if (!chip->label)
+               chip->label = dev_name(gpio->parent);
+
+       if (gpio->reg_set_base.valid && gpio->reg_clr_base.valid)
+               chip->set = gpio_regmap_set_with_clear;
+       else if (gpio->reg_set_base.valid)
+               chip->set = gpio_regmap_set;
+
+       if (gpio->reg_dir_in_base.valid || 

gpio->reg_dir_out_base.valid) {

drivers/gpio/gpio-regmap.c new file +22 -0
+               chip->get_direction = gpio_regmap_get_direction;
+               chip->direction_input = gpio_regmap_direction_input;
+               chip->direction_output = gpio_regmap_direction_output;
+       }
+
+       if (gpio->irq_domain)
+               chip->to_irq = gpio_regmap_to_irq;
+
+       ret = gpiochip_add_data(chip, d);
+       if (ret < 0)
+               goto err_alloc;
+
+       return 0;
+
+err_alloc:
+       kfree(d);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(gpio_regmap_register);
+
+/**
+ * gpio_regmap_unregister() - Unregister a generic regmap GPIO 

controller
+ *
+ * @gpio: gpio_regmap device to unregister
+ */

drivers/gpio/gpio-regmap.c new file +8 -0
+void gpio_regmap_unregister(struct gpio_regmap *gpio)
+{
+       gpiochip_remove(&gpio->data->gpio_chip);
+       kfree(gpio->data);
+}
+EXPORT_SYMBOL_GPL(gpio_regmap_unregister);
+
+static void devm_gpio_regmap_unregister(struct device *dev, void 

*res)

drivers/gpio/gpio-regmap.c new file +6 -0
+{
+       gpio_regmap_unregister(*(struct gpio_regmap **)res);
+}
+
+/**
+ * devm_gpio_regmap_register() - resource managed 

gpio_regmap_register()
+ *
+ * @dev: device that is registering this GPIO device
+ * @gpio: gpio_regmap device to register
+ *
+ * Managed gpio_regmap_register(). For generic regmap GPIO device registered by
+ * this function, gpio_regmap_unregister() is automatically called on driver
+ * detach. See gpio_regmap_register() for more information.
+ */
+int devm_gpio_regmap_register(struct device *dev, struct gpio_regmap *gpio)

drivers/gpio/gpio-regmap.c new file +25 -0
+{
+       struct gpio_regmap **ptr;
+       int ret;
+
+       ptr = devres_alloc(devm_gpio_regmap_unregister, sizeof(*ptr),
+                          GFP_KERNEL);
+       if (!ptr)
+               return -ENOMEM;
+
+       ret = gpio_regmap_register(gpio);
+       if (ret) {
+               devres_free(ptr);
+               return ret;
+       }
+
+       *ptr = gpio;
+       devres_add(dev, ptr);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(devm_gpio_regmap_register);
+
+MODULE_AUTHOR("Michael Walle <[email protected]>");
+MODULE_DESCRIPTION("GPIO generic regmap driver core");
+MODULE_LICENSE("GPL");
include/linux/gpio-regmap.h new file +4 -0
new file mode 100644
index 000000000000..ad63955e0e43
@@ -0,0 +1,88 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef _LINUX_GPIO_REGMAP_H
+#define _LINUX_GPIO_REGMAP_H
Quoted 8 lines

include/linux/gpio-regmap.h line 9

Quoted 5 lines
include/linux/gpio-regmap.h new file +5 -0
+
+struct gpio_regmap_addr {
+       unsigned int addr;
+       bool valid;
+};

I'm not quite sure what the meaning behind the valid field here is. When would we potentially set it to false?

Some base addresses are optional, but on the other hand, a base address of 0 could also be valid. So I cannot use 0 as an indicator whether a base address is set or not. The generic mmio driver has some special case for the ack base, where there is a use_ack flag which forces to use the ack register even if its zero. So I've had a look at the kernel if there is a better idiom for that, but I haven't found anything.

So the best from a user perspective I've could come up with was:

  ->base_reg = GPIO_REGMAP_ADDR(addr);

I'm open for suggestions.

Quoted 47 lines

include/linux/gpio-regmap.h line 17

Quoted 9 lines
include/linux/gpio-regmap.h new file +8 -0
+#define GPIO_REGMAP_ADDR(_addr) \
+       ((struct gpio_regmap_addr) { .addr = _addr, .valid = true })
+
+/**
+ * struct gpio_regmap - Description of a generic regmap gpio_chip.
+ *
+ * @parent:            The parent device
+ * @regmap:            The regmap use to access the registers

s/use/used/

Quoted 25 lines
include/linux/gpio-regmap.h new file +2 -0
+ *                     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) out setting register base address
+ * @reg_dir_out_base:  (Optional) in 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.
+ * @to_irq:            (Optional) Maps GPIO offset to a irq number.
+ *                     By default assumes a linear mapping of the
+ *                     given irq_domain.
+ * @driver_data:       Pointer to the drivers private data. Not used 
Quoted 9 lines

by
+ * gpio-regmap.
+ *
+ * The reg_mask_xlate translates a given base address and GPIO offset to
+ * register and mask pair. The base address is one of the given reg_*_base.
+ */
+struct gpio_regmap {

Quoted 4 lines

I'd prefer to follow a pattern seen in other such APIs of calling this structure gpio_regmap_config and creating another private structure called gpio_regmap used in callbacks that would only contain necessary fields.

something like the following?

struct gpio_regmap *gpio_regmap_register(struct gpio_regmap_config *)

but if that structure is private, how can a callback access individual elements? Or do you mean private in "local to the gpio drivers"?

Also I was unsure about the naming, eg. some use stuff_register()/stuff_unregister() and some stuff_add()/stuff_remove().

Quoted 44 lines
Quoted 44 lines
include/linux/gpio-regmap.h new file +17 -0
+       struct device *parent;
+       struct regmap *regmap;
+       struct gpio_regmap_data *data;
+
+       const char *label;
+       int ngpio;
+
+       struct gpio_regmap_addr reg_dat_base;
+       struct gpio_regmap_addr reg_set_base;
+       struct gpio_regmap_addr reg_clr_base;
+       struct gpio_regmap_addr reg_dir_in_base;
+       struct gpio_regmap_addr reg_dir_out_base;
+       int reg_stride;
+       int ngpio_per_reg;
+       struct irq_domain *irq_domain;
+
+       int (*reg_mask_xlate)(struct gpio_regmap *gpio, unsigned int 

base,

include/linux/gpio-regmap.h new file +21 -0
+                             unsigned int offset, unsigned int *reg,
+                             unsigned int *mask);
+       int (*to_irq)(struct gpio_regmap *gpio, unsigned int offset);
+
+       void *driver_data;
+};
+
+static inline void gpio_regmap_set_drvdata(struct gpio_regmap *gpio,
+                                          void *data)
+{
+       gpio->driver_data = data;
+}
+
+static inline void *gpio_regmap_get_drvdata(struct gpio_regmap *gpio)
+{
+       return gpio->driver_data;
+}
+
+int gpio_regmap_register(struct gpio_regmap *gpio);
+void gpio_regmap_unregister(struct gpio_regmap *gpio);
+int devm_gpio_regmap_register(struct device *dev, struct gpio_regmap 

*gpio);
+int gpio_regmap_simple_xlate(struct gpio_regmap *gpio, unsigned int base,
+ unsigned int offset,

Quoted 8 lines
Quoted 6 lines
include/linux/gpio-regmap.h new file +3 -1
+                            unsigned int *reg, unsigned int *mask);
+
+#endif /* _LINUX_GPIO_REGMAP_H */
--

2.20.1

Overall looks really nice. I'm happy we'll have it in v5.8.

Thanks, one thing I'm uncertain about is the regmap_irq change and if that is acceptable. So Mark would need to comment on that.

-michael

#16 lore raw

Re: [PATCH v2 10/16] gpio: add a reusable generic gpio_chip using regmap

pon., 6 kwi 2020 o 12:10 Michael Walle <[email protected]> napisał(a):

Quoted 29 lines

Hi Bartosz, Hi Mark Brown,

Am 2020-04-06 09:47, schrieb Bartosz Golaszewski:

Quoted 24 lines

czw., 2 kwi 2020 o 22:37 Michael Walle <[email protected]> napisał(a):

Quoted 22 lines

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 instanciated 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().

Quoted 8 lines
Quoted 5 lines

Hi Michael,

Thanks for doing this! When looking at other generic drivers:
gpio-mmio and gpio-reg I can see there are some corner-cases and more specific configuration options we could add

I didn't want to copy every bit without being able to test it.

Sure, I didn't mean we need to do it now - just set it as the future goal.

Quoted 4 lines
Quoted 3 lines

but it's not a blocker,
we'll probably be extending this one as we convert more drivers to using it.

Quoted 8 lines

correct, that was also my plan.

Quoted 2 lines

Personally I'd love to see gpio-mmio and gpio-reg removed and replaced by a single, generic regmap interface eventually.

agreed.

[snip!]

Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
drivers/gpio/gpio-regmap.c new file +5 -0
+
+/**
+ * gpio_regmap_simple_xlate() - translate base/offset to reg/mask
+ *
+ * Use a simple linear mapping to translate the offset to the

bitmask.
+ */
+int gpio_regmap_simple_xlate(struct gpio_regmap *gpio, unsigned int base,

drivers/gpio/gpio-regmap.c new file +6 -0
+                            unsigned int offset,
+                            unsigned int *reg, unsigned int *mask)
+{
+       unsigned int line = offset % gpio->ngpio_per_reg;
+       unsigned int stride = offset / gpio->ngpio_per_reg;
+
Quoted 8 lines

drivers/gpio/gpio-regmap.c line 36

Quoted 6 lines
drivers/gpio/gpio-regmap.c new file +6 -0
+       *reg = base + stride * gpio->reg_stride;
+       *mask = BIT(line);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(gpio_regmap_simple_xlate);

Why does this need to be exported?

Quoted 5 lines

Mh, the idea was that a user could also set this xlate() by himself (for whatever reason). But since it is the default, it is not really necessary.
That being said, I don't care if its only local to this module.

Let's only export symbols that have external users then.

[snip!]

Quoted 30 lines
Quoted 14 lines
Quoted 14 lines
drivers/gpio/gpio-regmap.c new file +4 -0
+
+MODULE_AUTHOR("Michael Walle <[email protected]>");
+MODULE_DESCRIPTION("GPIO generic regmap driver core");
+MODULE_LICENSE("GPL");
include/linux/gpio-regmap.h new file +4 -0
new file mode 100644
index 000000000000..ad63955e0e43
@@ -0,0 +1,88 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef _LINUX_GPIO_REGMAP_H
+#define _LINUX_GPIO_REGMAP_H
Quoted 8 lines

include/linux/gpio-regmap.h line 9

Quoted 5 lines
include/linux/gpio-regmap.h new file +5 -0
+
+struct gpio_regmap_addr {
+       unsigned int addr;
+       bool valid;
+};

I'm not quite sure what the meaning behind the valid field here is. When would we potentially set it to false?

Some base addresses are optional, but on the other hand, a base address of 0 could also be valid. So I cannot use 0 as an indicator whether a base address is set or not. The generic mmio driver has some special case for the ack base, where there is a use_ack flag which forces to use the ack register even if its zero. So I've had a look at the kernel if there is a better idiom for that, but I haven't found anything.

Quoted 6 lines

So the best from a user perspective I've could come up with was:

   ->base_reg = GPIO_REGMAP_ADDR(addr);

I'm open for suggestions.

Maybe setting the pointer to ERR_PTR(-ENOENT) which will result in IS_ERR() returning true?

Quoted 52 lines
Quoted 47 lines

include/linux/gpio-regmap.h line 17

Quoted 9 lines
include/linux/gpio-regmap.h new file +8 -0
+#define GPIO_REGMAP_ADDR(_addr) \
+       ((struct gpio_regmap_addr) { .addr = _addr, .valid = true })
+
+/**
+ * struct gpio_regmap - Description of a generic regmap gpio_chip.
+ *
+ * @parent:            The parent device
+ * @regmap:            The regmap use to access the registers

s/use/used/

Quoted 25 lines
include/linux/gpio-regmap.h new file +2 -0
+ *                     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) out setting register base address
+ * @reg_dir_out_base:  (Optional) in 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.
+ * @to_irq:            (Optional) Maps GPIO offset to a irq number.
+ *                     By default assumes a linear mapping of the
+ *                     given irq_domain.
+ * @driver_data:       Pointer to the drivers private data. Not used
Quoted 9 lines

by
+ * gpio-regmap.
+ *
+ * The reg_mask_xlate translates a given base address and GPIO offset to
+ * register and mask pair. The base address is one of the given reg_*_base.
+ */
+struct gpio_regmap {

Quoted 4 lines

I'd prefer to follow a pattern seen in other such APIs of calling this structure gpio_regmap_config and creating another private structure called gpio_regmap used in callbacks that would only contain necessary fields.

Quoted 7 lines

something like the following?

struct gpio_regmap *gpio_regmap_register(struct gpio_regmap_config *)

but if that structure is private, how can a callback access individual elements? Or do you mean private in "local to the gpio drivers"?

Either making the structure local to drivers/gpio or making it entirely opaque and providing accessor functions. Depending on how much of the structure one may want to access.

Quoted 3 lines

Also I was unsure about the naming, eg. some use stuff_register()/stuff_unregister() and some stuff_add()/stuff_remove().

register/unregister is fine with me.

Bart

#17 lore raw

Re: [PATCH v2 10/16] gpio: add a reusable generic gpio_chip using regmap

Am 2020-04-14 11:50, schrieb Bartosz Golaszewski:

Quoted 122 lines

pon., 6 kwi 2020 o 12:10 Michael Walle <[email protected]> napisał(a):

Quoted 29 lines

Hi Bartosz, Hi Mark Brown,

Am 2020-04-06 09:47, schrieb Bartosz Golaszewski:

Quoted 24 lines

czw., 2 kwi 2020 o 22:37 Michael Walle <[email protected]> napisał(a):

Quoted 22 lines

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 instanciated 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().

Quoted 8 lines
Quoted 5 lines

Hi Michael,

Thanks for doing this! When looking at other generic drivers:
gpio-mmio and gpio-reg I can see there are some corner-cases and more specific configuration options we could add

I didn't want to copy every bit without being able to test it.

Sure, I didn't mean we need to do it now - just set it as the future goal.

Quoted 4 lines
Quoted 3 lines

but it's not a blocker,
we'll probably be extending this one as we convert more drivers to using it.

Quoted 8 lines

correct, that was also my plan.

Quoted 2 lines

Personally I'd love to see gpio-mmio and gpio-reg removed and replaced by a single, generic regmap interface eventually.

agreed.

[snip!]

Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
drivers/gpio/gpio-regmap.c new file +5 -0
+
+/**
+ * gpio_regmap_simple_xlate() - translate base/offset to reg/mask
+ *
+ * Use a simple linear mapping to translate the offset to the

bitmask.
+ */
+int gpio_regmap_simple_xlate(struct gpio_regmap *gpio, unsigned int base,

drivers/gpio/gpio-regmap.c new file +6 -0
+                            unsigned int offset,
+                            unsigned int *reg, unsigned int *mask)
+{
+       unsigned int line = offset % gpio->ngpio_per_reg;
+       unsigned int stride = offset / gpio->ngpio_per_reg;
+
Quoted 8 lines

drivers/gpio/gpio-regmap.c line 36

Quoted 6 lines
drivers/gpio/gpio-regmap.c new file +6 -0
+       *reg = base + stride * gpio->reg_stride;
+       *mask = BIT(line);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(gpio_regmap_simple_xlate);

Why does this need to be exported?

Quoted 5 lines

Mh, the idea was that a user could also set this xlate() by himself (for whatever reason). But since it is the default, it is not really necessary.
That being said, I don't care if its only local to this module.

Let's only export symbols that have external users then.

[snip!]

Quoted 32 lines
Quoted 14 lines
Quoted 14 lines
drivers/gpio/gpio-regmap.c new file +4 -0
+
+MODULE_AUTHOR("Michael Walle <[email protected]>");
+MODULE_DESCRIPTION("GPIO generic regmap driver core");
+MODULE_LICENSE("GPL");
include/linux/gpio-regmap.h new file +4 -0
new file mode 100644
index 000000000000..ad63955e0e43
@@ -0,0 +1,88 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef _LINUX_GPIO_REGMAP_H
+#define _LINUX_GPIO_REGMAP_H
Quoted 8 lines

include/linux/gpio-regmap.h line 9

Quoted 5 lines
include/linux/gpio-regmap.h new file +5 -0
+
+struct gpio_regmap_addr {
+       unsigned int addr;
+       bool valid;
+};

I'm not quite sure what the meaning behind the valid field here is. When would we potentially set it to false?

Some base addresses are optional, but on the other hand, a base address of 0 could also be valid. So I cannot use 0 as an indicator whether a base address is set or not. The generic mmio driver has some special case for the ack base, where there is a use_ack flag which forces to use the ack register even if its zero. So I've had a look at the kernel if there is a better idiom for that, but I haven't found anything.

So the best from a user perspective I've could come up with was:

Quoted 7 lines
Quoted 4 lines
   ->base_reg = GPIO_REGMAP_ADDR(addr);

I'm open for suggestions.

Maybe setting the pointer to ERR_PTR(-ENOENT) which will result in IS_ERR() returning true?

Unfortunatly, its not a pointer, but only a regular unsigned int (ie the type the regmap API has for its "reg" property). It could be a pointer of course but then the user would have to allocate additional memory.

-michael

Quoted 72 lines
Quoted 53 lines
Quoted 48 lines

include/linux/gpio-regmap.h line 17

Quoted 10 lines
include/linux/gpio-regmap.h new file +8 -0
+#define GPIO_REGMAP_ADDR(_addr) \
+       ((struct gpio_regmap_addr) { .addr = _addr, .valid = true })
+
+/**
+ * struct gpio_regmap - Description of a generic regmap gpio_chip.
+ *
+ * @parent:            The parent device
+ * @regmap:            The regmap use to access the registers

s/use/used/

Quoted 25 lines
include/linux/gpio-regmap.h new file +2 -0
+ *                     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) out setting register base address
+ * @reg_dir_out_base:  (Optional) in 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.
+ * @to_irq:            (Optional) Maps GPIO offset to a irq number.
+ *                     By default assumes a linear mapping of the
+ *                     given irq_domain.
+ * @driver_data:       Pointer to the drivers private data. Not used
Quoted 9 lines

by
+ * gpio-regmap.
+ *
+ * The reg_mask_xlate translates a given base address and GPIO offset to
+ * register and mask pair. The base address is one of the given reg_*_base.
+ */
+struct gpio_regmap {

Quoted 4 lines

I'd prefer to follow a pattern seen in other such APIs of calling this structure gpio_regmap_config and creating another private structure called gpio_regmap used in callbacks that would only contain necessary fields.

Quoted 7 lines

something like the following?

struct gpio_regmap *gpio_regmap_register(struct gpio_regmap_config *)

but if that structure is private, how can a callback access individual elements? Or do you mean private in "local to the gpio drivers"?

Either making the structure local to drivers/gpio or making it entirely opaque and providing accessor functions. Depending on how much of the structure one may want to access.

Quoted 3 lines

Also I was unsure about the naming, eg. some use stuff_register()/stuff_unregister() and some stuff_add()/stuff_remove().

register/unregister is fine with me.

Bart

#18 lore raw

Re: [PATCH v2 10/16] gpio: add a reusable generic gpio_chip using regmap

wt., 14 kwi 2020 o 12:07 Michael Walle <[email protected]> napisał(a):

Quoted 11 lines
Quoted 10 lines
Quoted 7 lines

So the best from a user perspective I've could come up with was:

   ->base_reg = GPIO_REGMAP_ADDR(addr);

I'm open for suggestions.

Maybe setting the pointer to ERR_PTR(-ENOENT) which will result in IS_ERR() returning true?

Quoted 7 lines

Unfortunatly, its not a pointer, but only a regular unsigned int (ie the type the regmap API has for its "reg" property). It could be a pointer of course but then the user would have to allocate additional memory.

-michael

Eek, of course it's not a pointer. If possible I'd like to avoid this GPIO_REGMAP_ADDR() macro, so how about having some separate field for invalid offsets making every offset 'valid' by default?

Linus: do you have a better idea?

Bart

#19 lore raw

Re: [PATCH v2 10/16] gpio: add a reusable generic gpio_chip using regmap

Am 2020-04-14 19:00, schrieb Bartosz Golaszewski:

Quoted 17 lines

wt., 14 kwi 2020 o 12:07 Michael Walle <[email protected]> napisał(a):

Quoted 16 lines
Quoted 10 lines
Quoted 7 lines

So the best from a user perspective I've could come up with was:

   ->base_reg = GPIO_REGMAP_ADDR(addr);

I'm open for suggestions.

Maybe setting the pointer to ERR_PTR(-ENOENT) which will result in IS_ERR() returning true?

Unfortunatly, its not a pointer, but only a regular unsigned int (ie the type the regmap API has for its "reg" property). It could be a pointer of course but then the user would have to allocate additional memory.

Quoted 6 lines
Quoted 2 lines

-michael

Eek, of course it's not a pointer. If possible I'd like to avoid this GPIO_REGMAP_ADDR() macro, so how about having some separate field for invalid offsets making every offset 'valid' by default?

IMHO this has the same problems as mentioned in the response to Mark's idea. Normally, the user sets only some addresses, thus he has to mark all other as invalid. And if you add another address, you have to touch all the drivers to mark it as invalid.

We could add some force bits like the "use_ack" flag in the bgpio driver, where you can force the use of the value 0. But I'd really like to find a better way..

-michael

Quoted 4 lines

Linus: do you have a better idea?

Bart

#20 lore raw

Re: [PATCH v2 10/16] gpio: add a reusable generic gpio_chip using regmap

Hi Mark, Hi Bartosz, Hi Linus,

Am 2020-04-14 20:41, schrieb Michael Walle:

Quoted 25 lines

Am 2020-04-14 19:00, schrieb Bartosz Golaszewski:

Quoted 17 lines

wt., 14 kwi 2020 o 12:07 Michael Walle <[email protected]> napisał(a):

Quoted 16 lines
Quoted 10 lines
Quoted 7 lines

So the best from a user perspective I've could come up with was:

   ->base_reg = GPIO_REGMAP_ADDR(addr);

I'm open for suggestions.

Maybe setting the pointer to ERR_PTR(-ENOENT) which will result in IS_ERR() returning true?

Unfortunatly, its not a pointer, but only a regular unsigned int (ie the type the regmap API has for its "reg" property). It could be a pointer of course but then the user would have to allocate additional memory.

Quoted 6 lines
Quoted 2 lines

-michael

Eek, of course it's not a pointer. If possible I'd like to avoid this GPIO_REGMAP_ADDR() macro, so how about having some separate field for invalid offsets making every offset 'valid' by default?

Quoted 8 lines

IMHO this has the same problems as mentioned in the response to Mark's idea. Normally, the user sets only some addresses, thus he has to mark all other as invalid. And if you add another address, you have to touch all the drivers to mark it as invalid.

We could add some force bits like the "use_ack" flag in the bgpio driver, where you can force the use of the value 0. But I'd really like to find a better way..

So what about the following:

#define GPIO_REGMAP_ADDR_ZERO (unsigned int)(-1)

So this way the user might assign the base addresses the normal way except when he wants to use zero, in that case he has to use

  ->base_adr = GPIO_REGMAP_ADDR_ZERO;

gpio-regmap.c could use then:

if (base_addr)
something_useful(gpio_regmap_addr(base_addr));

unsigned int gpio_regmap_addr(unsigned int addr) {
return (addr == GPIO_REGMAP_ADDR_ZERO) ? 0 : addr;
}

-michael

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#21 lore raw

Re: [PATCH v2 10/16] gpio: add a reusable generic gpio_chip using regmap

Quoted 3 lines · Michael Walle

So what about the following:

#define GPIO_REGMAP_ADDR_ZERO (unsigned int)(-1)

Yeah with regmap explicitly using int I guess we can't use S32_MAX, so that is fair.

Quoted 7 lines

So this way the user might assign the base addresses the normal way except when he wants to use zero, in that case he has to use

   ->base_adr = GPIO_REGMAP_ADDR_ZERO;

gpio-regmap.c could use then:

Quoted 7 lines

if (base_addr)
something_useful(gpio_regmap_addr(base_addr));

unsigned int gpio_regmap_addr(unsigned int addr)

{
   return (addr == GPIO_REGMAP_ADDR_ZERO) ? 0 : addr;
}

That's reasonably clean.

Yours,
Linus Walleij

#22 lore raw

Re: [PATCH v2 10/16] gpio: add a reusable generic gpio_chip using regmap

Am 2020-04-16 11:20, schrieb Linus Walleij:

Quoted 19 lines · Michael Walle
Quoted 4 lines

So what about the following:

#define GPIO_REGMAP_ADDR_ZERO (unsigned int)(-1)

Yeah with regmap explicitly using int I guess we can't use S32_MAX, so that is fair.

Quoted 10 lines

So this way the user might assign the base addresses the normal way except when he wants to use zero, in that case he has to use

   ->base_adr = GPIO_REGMAP_ADDR_ZERO;

gpio-regmap.c could use then:

if (base_addr)
something_useful(gpio_regmap_addr(base_addr));

Quoted 6 lines
Quoted 4 lines

unsigned int gpio_regmap_addr(unsigned int addr)

{
   return (addr == GPIO_REGMAP_ADDR_ZERO) ? 0 : addr;
}

That's reasonably clean.

Ok, at least on that side. For my sl28 gpio driver I then have the problem that depending on 'base' I might have to use GPIO_REGMAP_ADDR_ZERO:

  #define GPIO_REG_DIR 0
  config.reg_dir_out_base = base + GPIO_REG_DIR;

So there is still a convenience macro:
#define GPIO_REGMAP_ADDR(addr) ((addr) ? addr : GPIO_REGMAP_ADDR_ZERO)

which you can use if you can't be sure that the address is not non-zero. So the code in my sl28 gpio driver looks like:

config.reg_dir_out_base = GPIO_REGMAP_ADDR(base + GPIO_REG_DIR);

I'll respin the patch with the current remarks.

-michael

maintainer
#23 lore raw

Re: [PATCH v2 10/16] gpio: add a reusable generic gpio_chip using regmap

In reply to Michael Walle
Quoted 9 lines · Michael Walle

Am 2020-04-14 11:50, schrieb Bartosz Golaszewski:

Quoted 2 lines

Maybe setting the pointer to ERR_PTR(-ENOENT) which will result in IS_ERR() returning true?

Unfortunatly, its not a pointer, but only a regular unsigned int (ie the type the regmap API has for its "reg" property). It could be a pointer of course but then the user would have to allocate additional memory.

You could define REGMAP_INVALID_ADDR to be (unsigned int)(-1) or some other suitably implausible address and use that as a value. It's possible that there might be a collision with a real address on some device but it should be sufficiently unlikely to be useful, especially if it's not something regmap in general goes and evaluates. For extra safety we could have an API for allowing users to query the register validity information regmap has (or can be given) and gpiolib could then use that to figure out if the value was actually a dummy value but that's probably overdoing it.

#24 lore raw

Re: [PATCH v2 10/16] gpio: add a reusable generic gpio_chip using regmap

Am 2020-04-14 19:21, schrieb Mark Brown:

Quoted 11 lines · Michael Walle
Quoted 9 lines

Am 2020-04-14 11:50, schrieb Bartosz Golaszewski:

Quoted 2 lines

Maybe setting the pointer to ERR_PTR(-ENOENT) which will result in IS_ERR() returning true?

Unfortunatly, its not a pointer, but only a regular unsigned int (ie the type the regmap API has for its "reg" property). It could be a pointer of course but then the user would have to allocate additional memory.

Quoted 9 lines

You could define REGMAP_INVALID_ADDR to be (unsigned int)(-1) or some other suitably implausible address and use that as a value. It's possible that there might be a collision with a real address on some device but it should be sufficiently unlikely to be useful, especially if it's not something regmap in general goes and evaluates. For extra safety we could have an API for allowing users to query the register validity information regmap has (or can be given) and gpiolib could then use that to figure out if the value was actually a dummy value but that's probably overdoing it.

If possible, I'd like to have the opposite logic. That is, if it is not set it should be invalid. If we have a magic macro like REGMAP_INVALID_ADDR, we must assign it to all the unused addresses. Thus every driver would have to assign all addresses and if in the future there will be some added, we'd have to touch all the drivers which use gpio_regmap.

-michael

maintainer
#25 lore raw

Re: [PATCH v2 10/16] gpio: add a reusable generic gpio_chip using regmap

Quoted 12 lines · Michael Walle

Am 2020-04-14 19:21, schrieb Mark Brown:

Quoted 9 lines

You could define REGMAP_INVALID_ADDR to be (unsigned int)(-1) or some other suitably implausible address and use that as a value. It's possible that there might be a collision with a real address on some device but it should be sufficiently unlikely to be useful, especially if it's not something regmap in general goes and evaluates. For extra safety we could have an API for allowing users to query the register validity information regmap has (or can be given) and gpiolib could then use that to figure out if the value was actually a dummy value but that's probably overdoing it.

Quoted 6 lines

If possible, I'd like to have the opposite logic. That is, if it is not set it should be invalid. If we have a magic macro like REGMAP_INVALID_ADDR, we must assign it to all the unused addresses. Thus every driver would have to assign all addresses and if in the future there will be some added, we'd have to touch all the drivers which use gpio_regmap.

Sure, for that you'd need a separate flag since zero is such a commonly valid address.

#26 lore raw

Re: [PATCH v2 10/16] gpio: add a reusable generic gpio_chip using regmap

In reply to Michael Walle
Quoted 10 lines · 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 instanciated in the parent driver and its irq domain will be associate to this driver.

Quoted 8 lines

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().

Overall I really like this driver and I think we should merge is as soon as it is in reasonable shape and then improve on top so we can start migrating drivers to it.

drivers/gpio/gpio-regmap.c line 183

Quoted 10 lines
drivers/gpio/gpio-regmap.c new file +10 -0
+static int gpio_regmap_to_irq(struct gpio_chip *chip, unsigned int offset)
+{
+       struct gpio_regmap_data *data = gpiochip_get_data(chip);
+       struct gpio_regmap *gpio = data->gpio;
+
+       /* the user might have its own .to_irq callback */
+       if (gpio->to_irq)
+               return gpio->to_irq(gpio, offset);
+
+       return irq_create_mapping(gpio->irq_domain, offset);

I think that should at least be irq_find_mapping(), the mapping should definately not be created by the .to_irq() callback since that is just a convenience function.

drivers/gpio/gpio-regmap.c line 254

Quoted 2 lines
drivers/gpio/gpio-regmap.c new file +2 -0
+       if (gpio->irq_domain)
+               chip->to_irq = gpio_regmap_to_irq;

I don't know about this.
(...)

Quoted 2 lines

+ * @irq_domain: (Optional) IRQ domain if the controller is
+ * interrupt-capable

(...)

Quoted 1 line

+ struct irq_domain *irq_domain;

I don't think this is a good storage place for the irqdomain, we already have gpio_irq_chip inside gpio_chip and that has an irqdomain, we should strive to reuse that infrastructure also for regmap GPIO I think, for now I would just leave .to_irq() out of this and let the driver deal with any irqs.

Yours,
Linus Walleij

#27 lore raw

Re: [PATCH v2 10/16] gpio: add a reusable generic gpio_chip using regmap

Hi Linus,

Am 2020-04-16 11:27, schrieb Linus Walleij:

Quoted 25 lines · Michael Walle
Quoted 11 lines

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 instanciated in the parent driver and its irq domain will be associate to this driver.

Quoted 8 lines

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().

Overall I really like this driver and I think we should merge is as soon as it is in reasonable shape and then improve on top so we can start migrating drivers to it.

Quoted 15 lines

drivers/gpio/gpio-regmap.c line 183

Quoted 11 lines

+static int gpio_regmap_to_irq(struct gpio_chip *chip, unsigned int offset)

drivers/gpio/gpio-regmap.c new file +9 -0
+{
+       struct gpio_regmap_data *data = gpiochip_get_data(chip);
+       struct gpio_regmap *gpio = data->gpio;
+
+       /* the user might have its own .to_irq callback */
+       if (gpio->to_irq)
+               return gpio->to_irq(gpio, offset);
+
+       return irq_create_mapping(gpio->irq_domain, offset);

I think that should at least be irq_find_mapping(), the mapping should definately not be created by the .to_irq() callback since that is just a convenience function.

what do you mean by conenience function? are there other ways? if you use irq_find_mapping() who will create the mappings? most gpio drivers use a similar function like gpio_regmap_to_irq().

Quoted 9 lines

drivers/gpio/gpio-regmap.c line 254

Quoted 3 lines
drivers/gpio/gpio-regmap.c new file +2 -0
+       if (gpio->irq_domain)
+               chip->to_irq = gpio_regmap_to_irq;

I don't know about this.
(...)

Quoted 3 lines

+ * @irq_domain: (Optional) IRQ domain if the
controller is
+ * interrupt-capable

Quoted 8 lines

(...)

Quoted 1 line

+ struct irq_domain *irq_domain;

I don't think this is a good storage place for the irqdomain, we already have gpio_irq_chip inside gpio_chip and that has an irqdomain, we should strive to reuse that infrastructure also for regmap GPIO I think, for now I would just leave .to_irq() out of this and let the driver deal with any irqs.

How would a driver attach the to_irq callback then? At the moment, the gpio_regmap doesn't expose the gpio_chip. So either we have to do that or the config still have to have a .to_irq property.

-michael

#28 lore raw

Re: [PATCH v2 10/16] gpio: add a reusable generic gpio_chip using regmap

Hi Linus,

Am 2020-04-17 08:34, schrieb Michael Walle:

Quoted 66 lines

Hi Linus,

Am 2020-04-16 11:27, schrieb Linus Walleij:

Quoted 25 lines · Michael Walle
Quoted 11 lines

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 instanciated in the parent driver and its irq domain will be associate to this driver.

Quoted 8 lines

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().

Overall I really like this driver and I think we should merge is as soon as it is in reasonable shape and then improve on top so we can start migrating drivers to it.

Quoted 15 lines

drivers/gpio/gpio-regmap.c line 183

Quoted 11 lines

+static int gpio_regmap_to_irq(struct gpio_chip *chip, unsigned int offset)

drivers/gpio/gpio-regmap.c new file +9 -0
+{
+       struct gpio_regmap_data *data = gpiochip_get_data(chip);
+       struct gpio_regmap *gpio = data->gpio;
+
+       /* the user might have its own .to_irq callback */
+       if (gpio->to_irq)
+               return gpio->to_irq(gpio, offset);
+
+       return irq_create_mapping(gpio->irq_domain, offset);

I think that should at least be irq_find_mapping(), the mapping should definately not be created by the .to_irq() callback since that is just a convenience function.

what do you mean by conenience function? are there other ways? if you use irq_find_mapping() who will create the mappings? most gpio drivers use a similar function like gpio_regmap_to_irq().

Quoted 9 lines

drivers/gpio/gpio-regmap.c line 254

Quoted 3 lines
drivers/gpio/gpio-regmap.c new file +2 -0
+       if (gpio->irq_domain)
+               chip->to_irq = gpio_regmap_to_irq;

I don't know about this.
(...)

Quoted 3 lines

+ * @irq_domain: (Optional) IRQ domain if the
controller is
+ * interrupt-capable

Quoted 8 lines

(...)

Quoted 1 line

+ struct irq_domain *irq_domain;

I don't think this is a good storage place for the irqdomain, we already have gpio_irq_chip inside gpio_chip and that has an irqdomain, we should strive to reuse that infrastructure also for regmap GPIO I think, for now I would just leave .to_irq() out of this and let the driver deal with any irqs.

Quoted 3 lines

How would a driver attach the to_irq callback then? At the moment, the gpio_regmap doesn't expose the gpio_chip. So either we have to do that or the config still have to have a .to_irq property.

Also, if I move the interrupt hanling completely out of the gpio-regmap, the driver would have to deal with "struct gpio_chip" which I would like to avoid if possible and keep it private to gpio-regmap.

Unfortunately, I don't have much experience how a good API for the interrupt handling and the gpio-regmap might look like. And there seems to be some overlap between regmap-irq and the interrupt stuff in gpiolib. For example, both provide and set the irq_domain_ops. Thus handing the domain over to gpio-regmap looked like a good idea to me. I get you point, that there is already a irqdomain in gpiolib and also a _to_irq() which is the same as the current implementation in gpio-regmap. Maybe it makes sense to just have a new function

int gpiolib_add_irqdomain(struct gpio_chip *gc, struct irq_domain domain) {
gc->irq.domain = domain;
gc->to_irq = gpiochip_to_irq;
}

which is called by gpio_regmap_register() if a config->irq_domain is given.

-michael

#29 lore raw

PATCH v2 11/16 gpio: add support for the sl28cpld GPIO controller

In reply to Michael Walle

This adds support for the GPIO controller of the sl28 board management controller. This driver is part of a multi-function device.

---
 drivers/gpio/Kconfig         |  11 +++
 drivers/gpio/Makefile        |   1 +
 drivers/gpio/gpio-sl28cpld.c | 186 +++++++++++++++++++++++++++++++++++
 3 files changed, 198 insertions(+)
 create mode 100644 drivers/gpio/gpio-sl28cpld.c
drivers/gpio/Kconfig +11 -0
index a8e148f4b2e0..d6d54cb9558e 100644
@@ -1215,6 +1215,17 @@ config GPIO_RC5T583
 	  This driver provides the support for driving/reading the gpio pins
 	  of RC5T583 device through standard gpio library.
 
+config GPIO_SL28CPLD
+	tristate "Kontron sl28 GPIO"
+	depends on MFD_SL28CPLD
+	select GPIO_REGMAP
+	select GPIOLIB_IRQCHIP
+	help
+	  This enables support for the GPIOs found on the Kontron sl28 CPLD.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called gpio-sl28cpld.
+
 config GPIO_STMPE
 	bool "STMPE GPIOs"
 	depends on MFD_STMPE
drivers/gpio/Makefile +1 -0
index 93e139fdfa57..f50ccf13f5eb 100644
@@ -129,6 +129,7 @@ obj-$(CONFIG_GPIO_SCH311X)		+= gpio-sch311x.o
 obj-$(CONFIG_GPIO_SCH)			+= gpio-sch.o
 obj-$(CONFIG_GPIO_SIFIVE)		+= gpio-sifive.o
 obj-$(CONFIG_GPIO_SIOX)			+= gpio-siox.o
+obj-$(CONFIG_GPIO_SL28CPLD)		+= gpio-sl28cpld.o
 obj-$(CONFIG_GPIO_SODAVILLE)		+= gpio-sodaville.o
 obj-$(CONFIG_GPIO_SPEAR_SPICS)		+= gpio-spear-spics.o
 obj-$(CONFIG_GPIO_SPRD)			+= gpio-sprd.o
drivers/gpio/gpio-sl28cpld.c new file +186 -0

186 lines not on this page · raw message

Signature · 1 line
2.20.1
#30 lore raw

Re: [PATCH v2 11/16] gpio: add support for the sl28cpld GPIO controller

Hi Michael,

this is looking good provided we can get the generic GPIO regmap helper reviewed and merged. Thanks!

Quoted 6 lines · Michael Walle

This adds support for the GPIO controller of the sl28 board management controller. This driver is part of a multi-function device.

+ depends on MFD_SL28CPLD

Apart from this depends it seems the patch is compile-time independent of the other patches so I'd suggest we just merge the generic regmap driver and this driver to the GPIO tree once we feel finished with them, optimistically assuming that the MFD driver will land and that we will not need any fundamental changes in the GPIO driver.

Worst case we have to revert the driver and that is no disaster.

Yours,
Linus Walleij

#31 lore raw

Re: [PATCH v2 11/16] gpio: add support for the sl28cpld GPIO controller

Hi Linus,

Am 2020-04-16 10:34, schrieb Linus Walleij:

Quoted 10 lines

Hi Michael,

this is looking good provided we can get the generic GPIO regmap helper reviewed and merged. Thanks!

Quoted 3 lines · Michael Walle

This adds support for the GPIO controller of the sl28 board management controller. This driver is part of a multi-function device.

Quoted 6 lines
Quoted 3 lines

+ depends on MFD_SL28CPLD

Apart from this depends it seems the patch is compile-time independent of the other patches

correct. There are no common mfd headers or something like that.

Quoted 7 lines

so I'd suggest we just merge
the generic regmap driver and this driver to the GPIO tree once we feel finished with them, optimistically assuming that the MFD driver will land and that we will not need any fundamental changes in the GPIO driver.

Worst case we have to revert the driver and that is no disaster.

Sure. One major thing I'm waiting for is the decision/new ideas on how to handle the "register is not set or zero" problem, see the other thread on the generic regmap gpio. Then I'd respin an update of this whole series.

-michael

#32 lore raw

PATCH v2 12/16 hwmon: add support for the sl28cpld hardware monitoring controller

In reply to Michael Walle

This adds support for the hardware monitoring controller of the sl28cpld board management controller. This driver is part of a multi-function device.

---
 Documentation/hwmon/sl28cpld.rst |  36 ++++++++
 drivers/hwmon/Kconfig            |  10 ++
 drivers/hwmon/Makefile           |   1 +
 drivers/hwmon/sl28cpld-hwmon.c   | 151 +++++++++++++++++++++++++++++++
 4 files changed, 198 insertions(+)
 create mode 100644 Documentation/hwmon/sl28cpld.rst
 create mode 100644 drivers/hwmon/sl28cpld-hwmon.c
Documentation/hwmon/sl28cpld.rst new file +36 -0
new file mode 100644
index 000000000000..7ed65f78250c
@@ -0,0 +1,36 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+Kernel driver sl28cpld
+======================
+
+Supported chips:
+
+   * Kontron sl28cpld
+
+     Prefix: 'sl28cpld'
+
+     Datasheet: not available
+
+Authors: Michael Walle <[email protected]>
+
+Description
+-----------
+
+The sl28cpld is a board management controller which also exposes a hardware
+monitoring controller. At the moment this controller supports a single fan
+supervisor. In the future there might be other flavours and additional
+hardware monitoring might be supported.
+
+The fan supervisor has a 7 bit counter register and a counter period of 1
+second. If the 7 bit counter overflows, the supervisor will automatically
+switch to x8 mode to support a wider input range at the loss of
+granularity.
+
+Sysfs entries
+-------------
+
+The following attributes are supported.
+
+======================= ========================================================
+fan1_input		Fan RPM. Assuming 2 pulses per revolution.
+======================= ========================================================
drivers/hwmon/Kconfig +10 -0
index 05a30832c6ba..c98716f78cfa 100644
@@ -1412,6 +1412,16 @@ config SENSORS_RASPBERRYPI_HWMON
 	  This driver can also be built as a module. If so, the module
 	  will be called raspberrypi-hwmon.
 
+config SENSORS_SL28CPLD
+	tristate "Kontron's SMARC-sAL28 hardware monitoring driver"
+	depends on MFD_SL28CPLD
+	help
+	  If you say yes here you get support for a fan connected to the
+	  input of the SMARC connector of Kontron's SMARC-sAL28 module.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called sl28cpld-hwmon.
+
 config SENSORS_SHT15
 	tristate "Sensiron humidity and temperature sensors. SHT15 and compat."
 	depends on GPIOLIB || COMPILE_TEST
drivers/hwmon/Makefile +1 -0
index b0b9c8e57176..dfb0f8cda2dd 100644
@@ -155,6 +155,7 @@ obj-$(CONFIG_SENSORS_S3C)	+= s3c-hwmon.o
 obj-$(CONFIG_SENSORS_SCH56XX_COMMON)+= sch56xx-common.o
 obj-$(CONFIG_SENSORS_SCH5627)	+= sch5627.o
 obj-$(CONFIG_SENSORS_SCH5636)	+= sch5636.o
+obj-$(CONFIG_SENSORS_SL28CPLD)	+= sl28cpld-hwmon.o
 obj-$(CONFIG_SENSORS_SHT15)	+= sht15.o
 obj-$(CONFIG_SENSORS_SHT21)	+= sht21.o
 obj-$(CONFIG_SENSORS_SHT3x)	+= sht3x.o
drivers/hwmon/sl28cpld-hwmon.c new file +151 -0

151 lines not on this page · raw message

Signature · 1 line
2.20.1
maintainer Acked-by on v2 12/16
#33 lore raw

Re: [PATCH v2 12/16] hwmon: add support for the sl28cpld hardware monitoring controller

Quoted 7 lines · Michael Walle

This adds support for the hardware monitoring controller of the sl28cpld board management controller. This driver is part of a multi-function device.

---
 Documentation/hwmon/sl28cpld.rst |  36 ++++++++

Needs to be added to Documentation.hwmon/index.rst. Otherwise looks good. With that fixed, please feel free to add

in the next version.

Guenter

Quoted 239 lines
 drivers/hwmon/Kconfig            |  10 ++
 drivers/hwmon/Makefile           |   1 +
 drivers/hwmon/sl28cpld-hwmon.c   | 151 +++++++++++++++++++++++++++++++
 4 files changed, 198 insertions(+)
 create mode 100644 Documentation/hwmon/sl28cpld.rst
 create mode 100644 drivers/hwmon/sl28cpld-hwmon.c
Documentation/hwmon/sl28cpld.rst new file +36 -0
new file mode 100644
index 000000000000..7ed65f78250c
@@ -0,0 +1,36 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+Kernel driver sl28cpld
+======================
+
+Supported chips:
+
+   * Kontron sl28cpld
+
+     Prefix: 'sl28cpld'
+
+     Datasheet: not available
+
+Authors: Michael Walle <[email protected]>
+
+Description
+-----------
+
+The sl28cpld is a board management controller which also exposes a hardware
+monitoring controller. At the moment this controller supports a single fan
+supervisor. In the future there might be other flavours and additional
+hardware monitoring might be supported.
+
+The fan supervisor has a 7 bit counter register and a counter period of 1
+second. If the 7 bit counter overflows, the supervisor will automatically
+switch to x8 mode to support a wider input range at the loss of
+granularity.
+
+Sysfs entries
+-------------
+
+The following attributes are supported.
+
+======================= ========================================================
+fan1_input		Fan RPM. Assuming 2 pulses per revolution.
+======================= ========================================================
drivers/hwmon/Kconfig +10 -0
index 05a30832c6ba..c98716f78cfa 100644
@@ -1412,6 +1412,16 @@ config SENSORS_RASPBERRYPI_HWMON
 	  This driver can also be built as a module. If so, the module
 	  will be called raspberrypi-hwmon.
 
+config SENSORS_SL28CPLD
+	tristate "Kontron's SMARC-sAL28 hardware monitoring driver"
+	depends on MFD_SL28CPLD
+	help
+	  If you say yes here you get support for a fan connected to the
+	  input of the SMARC connector of Kontron's SMARC-sAL28 module.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called sl28cpld-hwmon.
+
 config SENSORS_SHT15
 	tristate "Sensiron humidity and temperature sensors. SHT15 and compat."
 	depends on GPIOLIB || COMPILE_TEST
drivers/hwmon/Makefile +1 -0
index b0b9c8e57176..dfb0f8cda2dd 100644
@@ -155,6 +155,7 @@ obj-$(CONFIG_SENSORS_S3C)	+= s3c-hwmon.o
 obj-$(CONFIG_SENSORS_SCH56XX_COMMON)+= sch56xx-common.o
 obj-$(CONFIG_SENSORS_SCH5627)	+= sch5627.o
 obj-$(CONFIG_SENSORS_SCH5636)	+= sch5636.o
+obj-$(CONFIG_SENSORS_SL28CPLD)	+= sl28cpld-hwmon.o
 obj-$(CONFIG_SENSORS_SHT15)	+= sht15.o
 obj-$(CONFIG_SENSORS_SHT21)	+= sht21.o
 obj-$(CONFIG_SENSORS_SHT3x)	+= sht3x.o
drivers/hwmon/sl28cpld-hwmon.c new file +151 -0
new file mode 100644
index 000000000000..c79bdfed8332
@@ -0,0 +1,151 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * sl28cpld hardware monitoring driver.
+ *
+ * Copyright 2019 Kontron Europe GmbH
+ */
+
+#include <linux/bitfield.h>
+#include <linux/hwmon.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+
+#define FAN_INPUT		0x00
+#define   FAN_SCALE_X8		BIT(7)
+#define   FAN_VALUE_MASK	GENMASK(6, 0)
+
+struct sl28cpld_hwmon {
+	struct regmap *regmap;
+	u32 offset;
+};
+
+static umode_t sl28cpld_hwmon_is_visible(const void *data,
+					 enum hwmon_sensor_types type,
+					 u32 attr, int channel)
+{
+	return 0444;
+}
+
+static int sl28cpld_hwmon_read(struct device *dev,
+			       enum hwmon_sensor_types type, u32 attr,
+			       int channel, long *input)
+{
+	struct sl28cpld_hwmon *hwmon = dev_get_drvdata(dev);
+	unsigned int value;
+	int ret;
+
+	switch (attr) {
+	case hwmon_fan_input:
+		ret = regmap_read(hwmon->regmap, hwmon->offset + FAN_INPUT,
+				  &value);
+		if (ret)
+			return ret;
+		/*
+		 * The register has a 7 bit value and 1 bit which indicates the
+		 * scale. If the MSB is set, then the lower 7 bit has to be
+		 * multiplied by 8, to get the correct reading.
+		 */
+		if (value & FAN_SCALE_X8)
+			value = FIELD_GET(FAN_VALUE_MASK, value) << 3;
+
+		/*
+		 * The counter period is 1000ms and the sysfs specification
+		 * says we should asssume 2 pulses per revolution.
+		 */
+		value *= 60 / 2;
+
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	*input = value;
+	return 0;
+}
+
+static const u32 sl28cpld_hwmon_fan_config[] = {
+	HWMON_F_INPUT,
+	0
+};
+
+static const struct hwmon_channel_info sl28cpld_hwmon_fan = {
+	.type = hwmon_fan,
+	.config = sl28cpld_hwmon_fan_config,
+};
+
+static const struct hwmon_channel_info *sl28cpld_hwmon_info[] = {
+	&sl28cpld_hwmon_fan,
+	NULL
+};
+
+static const struct hwmon_ops sl28cpld_hwmon_ops = {
+	.is_visible = sl28cpld_hwmon_is_visible,
+	.read = sl28cpld_hwmon_read,
+};
+
+static const struct hwmon_chip_info sl28cpld_hwmon_chip_info = {
+	.ops = &sl28cpld_hwmon_ops,
+	.info = sl28cpld_hwmon_info,
+};
+
+static int sl28cpld_hwmon_probe(struct platform_device *pdev)
+{
+	struct device *hwmon_dev;
+	struct sl28cpld_hwmon *hwmon;
+	struct resource *res;
+
+	if (!pdev->dev.parent)
+		return -ENODEV;
+
+	hwmon = devm_kzalloc(&pdev->dev, sizeof(*hwmon), GFP_KERNEL);
+	if (!hwmon)
+		return -ENOMEM;
+
+	hwmon->regmap = dev_get_regmap(pdev->dev.parent, NULL);
+	if (!hwmon->regmap)
+		return -ENODEV;
+
+	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
+	if (!res)
+		return -EINVAL;
+	hwmon->offset = res->start;
+
+	hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
+				"sl28cpld_hwmon", hwmon,
+				&sl28cpld_hwmon_chip_info, NULL);
+	if (IS_ERR(hwmon_dev)) {
+		dev_err(&pdev->dev, "failed to register as hwmon device");
+		return PTR_ERR(hwmon_dev);
+	}
+
+	return 0;
+}
+
+static const struct of_device_id sl28cpld_hwmon_of_match[] = {
+	{ .compatible = "kontron,sl28cpld-fan" },
+	{},
+};
+MODULE_DEVICE_TABLE(of, sl28cpld_hwmon_of_match);
+
+static const struct platform_device_id sl28cpld_hwmon_id_table[] = {
+	{ "sl28cpld-fan" },
+	{}
+};
+MODULE_DEVICE_TABLE(platform, sl28cpld_hwmon_id_table);
+
+static struct platform_driver sl28cpld_hwmon_driver = {
+	.probe = sl28cpld_hwmon_probe,
+	.id_table = sl28cpld_hwmon_id_table,
+	.driver = {
+		.name = KBUILD_MODNAME,
+		.of_match_table = sl28cpld_hwmon_of_match,
+	},
+};
+module_platform_driver(sl28cpld_hwmon_driver);
+
+MODULE_DESCRIPTION("sl28cpld Hardware Monitoring Driver");
+MODULE_AUTHOR("Michael Walle <[email protected]>");
+MODULE_LICENSE("GPL");
#34 lore raw

PATCH v2 13/16 arm64: dts: freescale: sl28: enable sl28cpld

In reply to Michael Walle

Add the board management controller node.

---
 .../freescale/fsl-ls1028a-kontron-sl28.dts    | 92 +++++++++++++++++++
 1 file changed, 92 insertions(+)
arch/arm64/boot/dts/freescale/fsl-ls1028a-kontron-sl28.dts +92 -0
index 41ba38adc906..b73794d57db4 100644
@@ -8,6 +8,7 @@
 
 /dts-v1/;
 #include "fsl-ls1028a.dtsi"
+#include <dt-bindings/interrupt-controller/irq.h>
 
 / {
 	model = "Kontron SMARC-sAL28";
@@ -174,6 +175,97 @@
 		reg = <0x32>;
 	};
 
+	sl28cpld: sl28cpld@4a {
+		#address-cells = <1>;
+		#size-cells = <0>;
+		compatible = "kontron,sl28cpld";
+		reg = <0x4a>;
+		interrupts-extended = <&gpio2 6 IRQ_TYPE_EDGE_FALLING>;
+
+		interrupt-controller;
+		#interrupt-cells = <2>;
+
+		watchdog@0 {
+			compatible = "kontron,sl28cpld-wdt";
+			reg = <0>;
+			kontron,assert-wdt-timeout-pin;
+		};
+
+		hwmon@4 {
+			compatible = "kontron,sl28cpld-fan";
+			reg = <1>;
+		};
+
+		sl28cpld_pwm0: pwm@5 {
+			#pwm-cells = <2>;
+			compatible = "kontron,sl28cpld-pwm";
+			reg = <2>;
+		};
+
+		sl28cpld_pwm1: pwm@6 {
+			#pwm-cells = <2>;
+			compatible = "kontron,sl28cpld-pwm";
+			reg = <3>;
+		};
+
+		sl28cpld_gpio0: gpio@0 {
+			compatible = "kontron,sl28cpld-gpio";
+			reg = <4>;
+
+			gpio-controller;
+			#gpio-cells = <2>;
+			gpio-line-names =
+				"GPIO0_CAM0_PWR_N", "GPIO1_CAM1_PWR_N",
+				"GPIO2_CAM0_RST_N", "GPIO3_CAM1_RST_N",
+				"GPIO4_HDA_RST_N", "GPIO5_PWM_OUT",
+				"GPIO6_TACHIN", "GPIO7";
+
+			interrupt-controller;
+			#interrupt-cells = <2>;
+		};
+
+		sl28cpld_gpio1: gpio@1 {
+			compatible = "kontron,sl28cpld-gpio";
+			reg = <5>;
+
+			gpio-controller;
+			#gpio-cells = <2>;
+			gpio-line-names =
+				"GPIO8", "GPIO9", "GPIO10", "GPIO11",
+				"", "", "", "";
+
+			interrupt-controller;
+			#interrupt-cells = <2>;
+		};
+
+		sl28cpld_gpio2: gpio@2 {
+			compatible = "kontron,sl28cpld-gpo";
+			reg = <6>;
+
+			gpio-controller;
+			#gpio-cells = <2>;
+			gpio-line-names =
+				"LCD0 voltage enable",
+				"LCD0 backlight enable",
+				"eMMC reset", "LVDS bridge reset",
+				"LVDS bridge power-down",
+				"SDIO power enable",
+				"", "";
+		};
+
+		sl28cpld_gpio3: gpio@3 {
+			compatible = "kontron,sl28cpld-gpi";
+			reg = <7>;
+
+			gpio-controller;
+			#gpio-cells = <2>;
+			gpio-line-names =
+				"Power button", "Force recovery", "Sleep",
+				"Battery low", "Lid state", "Charging",
+				"Charger present", "";
+		};
+	};
+
 	eeprom@50 {
 		compatible = "atmel,24c32";
 		reg = <0x50>;
Signature · 1 line
2.20.1
#35 lore raw

PATCH v2 14/16 arm64: dts: freescale: sl28: map GPIOs to input events

In reply to Michael Walle

Now that we have support for GPIO lines of the SMARC connector, map the sleep, power and lid switch signals to the corresponding keys using the gpio-keys and gpio-keys-polled drivers. The power and sleep signals have dedicated interrupts, thus we use these ones. The lid switch is just mapped to a GPIO input and needs polling.

---
 .../freescale/fsl-ls1028a-kontron-sl28.dts    | 32 +++++++++++++++++++
 1 file changed, 32 insertions(+)
arch/arm64/boot/dts/freescale/fsl-ls1028a-kontron-sl28.dts +32 -0
index b73794d57db4..263ce50b0b79 100644
@@ -9,6 +9,8 @@
 /dts-v1/;
 #include "fsl-ls1028a.dtsi"
 #include <dt-bindings/interrupt-controller/irq.h>
+#include <dt-bindings/gpio/gpio.h>
+#include <dt-bindings/input/input.h>
 
 / {
 	model = "Kontron SMARC-sAL28";
@@ -23,6 +25,36 @@
 		spi1 = &dspi2;
 	};
 
+	buttons0 {
+		compatible = "gpio-keys";
+
+		power-button {
+			interrupts-extended = <&sl28cpld
+					       4 IRQ_TYPE_EDGE_BOTH>;
+			linux,code = <KEY_POWER>;
+			label = "Power";
+		};
+
+		sleep-button {
+			interrupts-extended = <&sl28cpld
+					       5 IRQ_TYPE_EDGE_BOTH>;
+			linux,code = <KEY_SLEEP>;
+			label = "Sleep";
+		};
+	};
+
+	buttons1 {
+		compatible = "gpio-keys-polled";
+		poll-interval = <200>;
+
+		lid-switch {
+			linux,input-type = <EV_SW>;
+			linux,code = <SW_LID>;
+			gpios = <&sl28cpld_gpio3 4 GPIO_ACTIVE_LOW>;
+			label = "Lid";
+		};
+	};
+
 	chosen {
 		stdout-path = "serial0:115200n8";
 	};
Signature · 1 line
2.20.1
#36 lore raw

PATCH v2 15/16 arm64: dts: freescale: sl28: enable LED support

In reply to Michael Walle

Now that we have support for GPIO lines of the SMARC connector, enable LED support on the KBox A-230-LS. There are two LEDs without fixed functions, one is yellow and one is green. Unfortunately, it is just one multi-color LED, thus while it is possible to enable both at the same time it is hard to tell the difference between "yellow only" and "yellow and green".

---
 .../fsl-ls1028a-kontron-kbox-a-230-ls.dts          | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/arm64/boot/dts/freescale/fsl-ls1028a-kontron-kbox-a-230-ls.dts +14 -0
index 4b4cc6a1573d..49cf4fe05c80 100644
@@ -16,6 +16,20 @@
 	model = "Kontron KBox A-230-LS";
 	compatible = "kontron,kbox-a-230-ls", "kontron,sl28-var4",
 		     "kontron,sl28", "fsl,ls1028a";
+
+	leds {
+		compatible = "gpio-leds";
+
+		user_yellow {
+			label = "s1914:yellow:user";
+			gpios = <&sl28cpld_gpio0 0 0>;
+		};
+
+		user_green {
+			label = "s1914:green:user";
+			gpios = <&sl28cpld_gpio1 3 0>;
+		};
+	};
 };
 
 &enetc_mdio_pf3 {
Signature · 1 line
2.20.1
#37 lore raw

PATCH v2 16/16 arm64: dts: freescale: sl28: enable fan support

In reply to Michael Walle

Add a pwm-fan mapped to the PWM channel 0 which is connected to the fan connector of the carrier.

---
 .../dts/freescale/fsl-ls1028a-kontron-sl28-var3-ads2.dts | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/arm64/boot/dts/freescale/fsl-ls1028a-kontron-sl28-var3-ads2.dts +9 -0
index 0973a6a45217..c45d7b40e374 100644
@@ -15,6 +15,15 @@
 	compatible = "kontron,sl28-var3-ads2", "kontron,sl28-var3",
 		     "kontron,sl28", "fsl,ls1028a";
 
+	pwm-fan {
+		compatible = "pwm-fan";
+		cooling-min-state = <0>;
+		cooling-max-state = <3>;
+		#cooling-cells = <2>;
+		pwms = <&sl28cpld_pwm0 0 4000000>;
+		cooling-levels = <1 128 192 255>;
+	};
+
 	sound {
 		#address-cells = <1>;
 		#size-cells = <0>;
Signature · 1 line
2.20.1