Skip to contentSkip to conversation

gpio: Add support for Andes ATCGPIO100 GPIO controller

PATCH In review CL Wang 4 participants 8 messages Nov 16 – Nov 24

Subsystems (2)
lore
Share & tools
lore

CL Wang

author
#0 lore raw

cover gpio: Add support for Andes ATCGPIO100 GPIO controller

This series adds support for the Andes ATCGPIO100 GPIO controller, an IP integrated in the Andes AE350 platform and in SoCs such as Qilai.

The controller provides up to 32 lines with optional pull, interrupt and debounce features, all reported by its configuration register. The driver builds the data path on the generic MMIO GPIO library and adds:
- Pull-up and pull-down bias
- Edge (rising, falling, both) and level (high, low) interrupts
- Hardware input debounce, clocked from pclk or an optional extclk
- Wakeup through the parent interrupt
- System suspend and resume

CL Wang (3):
dt-bindings: gpio: Add support for ATCGPIO100 GPIO controller
gpio: atcgpio100: Add ATCGPIO100 GPIO driver
MAINTAINERS: Add entry for Andes ATCGPIO100 GPIO

 .../bindings/gpio/andestech,ae350-gpio.yaml   |  92 +++
 MAINTAINERS                                   |   6 +
 drivers/gpio/Kconfig                          |  16 +
 drivers/gpio/Makefile                         |   1 +
 drivers/gpio/gpio-atcgpio100.c                | 641 ++++++++++++++++++
 5 files changed, 756 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/gpio/andestech,ae350-gpio.yaml
 create mode 100644 drivers/gpio/gpio-atcgpio100.c

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb

Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/3 dt-bindings: gpio: Add support for ATCGPIO100 GPIO controller

Document devicetree bindings for the Andes ATCGPIO100 GPIO controller.

ATCGPIO100 is the name of the IP, which is integrated in the Andes AE350 platform and in SoCs such as Qilai. As in the ATCSPI200 SPI controller binding, the compatibles follow the platform and SoC rather than the IP
name: andestech,ae350-gpio is the fallback, and an SoC that integrates the IP adds its own compatible, such as andestech,qilai-gpio, in front of it. Drivers can then match on the fallback and still add SoC-specific handling later.

The controller has up to 32 lines, and ngpios gives the number of lines implemented. The debounce logic can run from either of two clocks: pclk, the APB bus clock, which is required, or extclk, an optional external clock that allows longer debounce periods. The interrupt is optional, as the controller also works as a plain GPIO controller without it.

---
 .../bindings/gpio/andestech,ae350-gpio.yaml   | 92 +++++++++++++++++++
 1 file changed, 92 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/gpio/andestech,ae350-gpio.yaml
Documentation/devicetree/bindings/gpio/andestech,ae350-gpio.yaml new file +92 -0
new file mode 100644
index 000000000000..01badd2a1342
@@ -0,0 +1,92 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/gpio/andestech,ae350-gpio.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Andes ATCGPIO100 GPIO controller on AE350 platform
+
+maintainers:
+  - CL Wang <[email protected]>
+
+properties:
+  compatible:
+    oneOf:
+      - items:
+          - enum:
+              - andestech,qilai-gpio
+          - const: andestech,ae350-gpio
+      - const: andestech,ae350-gpio
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  interrupt-controller: true
+
+  "#interrupt-cells":
+    const: 2
+
+  gpio-controller: true
+
+  "#gpio-cells":
+    const: 2
+
+  ngpios:
+    minimum: 1
+    maximum: 32
+
+  gpio-line-names:
+    minItems: 1
+    maxItems: 32
+
+  clocks:
+    minItems: 1
+    items:
+      - description: APB bus clock
+      - description: External clock for debounce
+
+  clock-names:
+    minItems: 1
+    items:
+      - const: pclk
+      - const: extclk
+
+patternProperties:
+  "^.+-hog(-[0-9]+)?$":
+    type: object
+    required:
+      - gpio-hog
+
+required:
+  - compatible
+  - reg
+  - gpio-controller
+  - "#gpio-cells"
+  - clocks
+  - clock-names
+
+additionalProperties: false
+
+dependencies:
+  interrupt-controller: [ interrupts, "#interrupt-cells" ]
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    gpio@f0700000 {
+        compatible = "andestech,ae350-gpio";
+        reg = <0xf0700000 0x100>;
+        interrupts = <7 IRQ_TYPE_LEVEL_HIGH>;
+        interrupt-parent = <&plic>;
+        interrupt-controller;
+        #interrupt-cells = <2>;
+        gpio-controller;
+        #gpio-cells = <2>;
+        ngpios = <32>;
+        clocks = <&pclk>, <&extclk>;
+        clock-names = "pclk", "extclk";
+    };
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/3] dt-bindings: gpio: Add support for ATCGPIO100 GPIO controller

Quoted 9 lines · CL Wang

Document devicetree bindings for the Andes ATCGPIO100 GPIO controller.

ATCGPIO100 is the name of the IP, which is integrated in the Andes AE350 platform and in SoCs such as Qilai. As in the ATCSPI200 SPI controller binding, the compatibles follow the platform and SoC rather than the IP
name: andestech,ae350-gpio is the fallback, and an SoC that integrates the IP adds its own compatible, such as andestech,qilai-gpio, in front of it. Drivers can then match on the fallback and still add SoC-specific handling later.

Do not paste here LLM. LLM should not be used for writing commit msgs, because this is a test if you understood what you did here. You explain heer basics of DT and driver, which is exactly what LLM would do.

Quoted 3 lines

The controller has up to 32 lines, and ngpios gives the number of lines implemented. The debounce logic can run from either of two clocks: pclk,

Are you saying that SoC has multiple controllers with different number of GPIOs? Where is the DTS, can you link it?

Quoted 3 lines

the APB bus clock, which is required, or extclk, an optional external clock that allows longer debounce periods. The interrupt is optional, as the controller also works as a plain GPIO controller without it.

SoC routing is fixed, no?

Quoted 49 lines
---
 .../bindings/gpio/andestech,ae350-gpio.yaml   | 92 +++++++++++++++++++
 1 file changed, 92 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/gpio/andestech,ae350-gpio.yaml
Documentation/devicetree/bindings/gpio/andestech,ae350-gpio.yaml new file +36 -0
new file mode 100644
index 000000000000..01badd2a1342
@@ -0,0 +1,92 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/gpio/andestech,ae350-gpio.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Andes ATCGPIO100 GPIO controller on AE350 platform
+
+maintainers:
+  - CL Wang <[email protected]>
+
+properties:
+  compatible:
+    oneOf:
+      - items:
+          - enum:
+              - andestech,qilai-gpio
+          - const: andestech,ae350-gpio
+      - const: andestech,ae350-gpio
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  interrupt-controller: true
+
+  "#interrupt-cells":
+    const: 2
+
+  gpio-controller: true
+
+  "#gpio-cells":
+    const: 2
+

Documentation/devicetree/bindings/gpio/andestech,ae350-gpio.yaml line 46

Quoted 10 lines
Documentation/devicetree/bindings/gpio/andestech,ae350-gpio.yaml new file +10 -0
+  ngpios:
+    minimum: 1
+    maximum: 32
+
+  gpio-line-names:
+    minItems: 1
+    maxItems: 32
+
+  clocks:
+    minItems: 1

Why is this flexible?

Documentation/devicetree/bindings/gpio/andestech,ae350-gpio.yaml line 49

Quoted 3 lines
Documentation/devicetree/bindings/gpio/andestech,ae350-gpio.yaml new file +3 -0
+    items:
+      - description: APB bus clock
+      - description: External clock for debounce

Best regards,
Krzysztof

author
#3 lore raw

PATCH 2/3 gpio: atcgpio100: Add ATCGPIO100 GPIO driver

In reply to CL Wang

Add a driver for the Andes ATCGPIO100 GPIO controller, found on AndesCore-based platforms such as AE350.

The controller provides up to 32 lines. The data path is built on the generic MMIO GPIO library (gpio-mmio), and the number of lines and the optional pull, interrupt and debounce features are read from the configuration register at probe time.

The driver supports:
- Input and output, reading the output latch back for output lines
- Pull-up and pull-down bias
- Edge (rising, falling, both) and level (high, low) interrupts
- Hardware input debounce with a period shared by all lines
- Wakeup through the parent interrupt for lines that their consumers mark as wakeup sources
- System suspend and resume

---
 drivers/gpio/Kconfig           |  16 +
 drivers/gpio/Makefile          |   1 +
 drivers/gpio/gpio-atcgpio100.c | 641 +++++++++++++++++++++++++++++++++
 3 files changed, 658 insertions(+)
 create mode 100644 drivers/gpio/gpio-atcgpio100.c
drivers/gpio/Kconfig +16 -0
index 55e4280029d8..ad768976ff6c 100644
@@ -194,6 +194,22 @@ config GPIO_ASPEED_SGPIO
 	help
 	  Say Y here to support Aspeed AST2500 SGPIO functionality.
 
+config GPIO_ATCGPIO100
+	tristate "Andes ATCGPIO100 GPIO support"
+	depends on ARCH_ANDES || COMPILE_TEST
+	select GPIOLIB_IRQCHIP
+	select GPIO_GENERIC
+	help
+	  Say Y or M here to build support for the Andes ATCGPIO100 GPIO
+	  controller.
+
+	  This IP block provides up to 32 general-purpose I/O pins with
+	  configurable direction, pull-up and pull-down bias, edge and level
+	  triggered interrupts, hardware input debounce and wakeup support.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called gpio-atcgpio100.
+
 config GPIO_ATH79
 	tristate "Atheros AR71XX/AR724X/AR913X GPIO support"
 	default y if ATH79
drivers/gpio/Makefile +1 -0
index 88f748846e60..5a0abff4c91b 100644
@@ -40,6 +40,7 @@ obj-$(CONFIG_GPIO_AMDPT)		+= gpio-amdpt.o
 obj-$(CONFIG_GPIO_ARIZONA)		+= gpio-arizona.o
 obj-$(CONFIG_GPIO_ASPEED)		+= gpio-aspeed.o
 obj-$(CONFIG_GPIO_ASPEED_SGPIO)		+= gpio-aspeed-sgpio.o
+obj-$(CONFIG_GPIO_ATCGPIO100)		+= gpio-atcgpio100.o
 obj-$(CONFIG_GPIO_ATH79)		+= gpio-ath79.o
 obj-$(CONFIG_GPIO_AXIADO_SGPIO)		+= gpio-axiado-sgpio.o
 obj-$(CONFIG_GPIO_BCM_KONA)		+= gpio-bcm-kona.o
drivers/gpio/gpio-atcgpio100.c new file +641 -0

641 lines not on this page · raw message

Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH 2/3] gpio: atcgpio100: Add ATCGPIO100 GPIO driver

Quoted 19 lines · CL Wang

Add a driver for the Andes ATCGPIO100 GPIO controller, found on AndesCore-based platforms such as AE350.

The controller provides up to 32 lines. The data path is built on the generic MMIO GPIO library (gpio-mmio), and the number of lines and the optional pull, interrupt and debounce features are read from the configuration register at probe time.

The driver supports:
- Input and output, reading the output latch back for output lines
- Pull-up and pull-down bias
- Edge (rising, falling, both) and level (high, low) interrupts
- Hardware input debounce with a period shared by all lines
- Wakeup through the parent interrupt for lines that their consumers mark as wakeup sources
- System suspend and resume

---

...

Quoted 6 lines
drivers/gpio/gpio-atcgpio100.c new file +6 -0
+
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/clk.h>
+#include <linux/dev_printk.h>
+#include <linux/err.h>

drivers/gpio/gpio-atcgpio100.c line 22

Quoted 10 lines
drivers/gpio/gpio-atcgpio100.c new file +10 -0
+#include <linux/find.h>
+#include <linux/gpio/driver.h>
+#include <linux/gpio/generic.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/irqchip/chained_irq.h>
+#include <linux/math.h>
+#include <linux/minmax.h>
+#include <linux/mod_devicetable.h>

Uwe would probably say you should only pull in the individual headers that you require from include/linux/device-id/.

...

drivers/gpio/gpio-atcgpio100.c line 461

Quoted 9 lines
drivers/gpio/gpio-atcgpio100.c new file +9 -0
+
+	config = (struct gpio_generic_chip_config) {
+		.dev = &priv->pdev->dev,
+		.sz = 4,
+		.dat = priv->base + REG_DATA_IN,
+		.set = priv->base + REG_DATA_OUT,
+		.dirout = priv->base + REG_CHAN_DIR,
+		.flags = GPIO_GENERIC_READ_OUTPUT_REG_SET,
+	};

I think you could save even more code by using the gpio-regmap helper here instead. Could you see if that's possible here?

Quoted 10 lines
drivers/gpio/gpio-atcgpio100.c new file +10 -0
+
+	ret = gpio_generic_chip_init(&priv->chip, &config);
+	if (ret)
+		return dev_err_probe(&priv->pdev->dev, ret,
+				     "Failed to init generic GPIO chip\n");
+
+	return 0;
+}
+
+static void atcgpio_init_controller(struct atcgpio_priv *priv)

drivers/gpio/gpio-atcgpio100.c line 481

Quoted 10 lines
drivers/gpio/gpio-atcgpio100.c new file +10 -0
+{
+	u32 cfg;
+
+	cfg = atcgpio_read(priv, REG_CONFIG);
+	priv->pull_option = !!(cfg & CONFIG_PULL_OPTION);
+	priv->int_option = !!(cfg & CONFIG_INT_OPTION);
+	priv->debounce_option = !!(cfg & CONFIG_DEBOUNCE_OPTION);
+	priv->chan_num = CONFIG_CHAN_NUM_GET(cfg);
+	if (priv->chan_num > ATCGPIO_CHAN_NUM_MAX || !priv->chan_num) {
+		dev_warn(&priv->pdev->dev,

Use a local variable to store the adress of dev for better readability.

Quoted 24 lines
drivers/gpio/gpio-atcgpio100.c new file +24 -0
+			 "Invalid hardware GPIO number %d, forcing to %d\n",
+			 priv->chan_num,
+			 ATCGPIO_CHAN_NUM_MAX);
+		priv->chan_num = ATCGPIO_CHAN_NUM_MAX;
+	}
+
+	if (!priv->extclk_rate && !priv->pclk_rate) {
+		dev_warn(&priv->pdev->dev,
+			 "Disabling debounce: missing reference clock\n");
+		priv->debounce_option = 0;
+	}
+
+	if (priv->debounce_option)
+		atcgpio_write(priv, REG_DEBOUNCE_EN, 0);
+
+	if (priv->int_option) {
+		atcgpio_write(priv, REG_CHAN_INT_EN, 0);
+		atcgpio_write(priv, REG_INT_MODE0, 0);
+		atcgpio_write(priv, REG_INT_MODE1, 0);
+		atcgpio_write(priv, REG_INT_MODE2, 0);
+		atcgpio_write(priv, REG_INT_MODE3, 0);
+		atcgpio_write(priv, REG_CHAN_INT_STATE, GENMASK(31, 0));
+	}
+}

drivers/gpio/gpio-atcgpio100.c line 515

Quoted 10 lines
drivers/gpio/gpio-atcgpio100.c new file +10 -0
+
+static int atcgpio_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct gpio_irq_chip *gc_irq;
+	struct atcgpio_priv *priv;
+	struct gpio_chip *gc;
+	int ret;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);

Use devm_kzalloc_obj(). Same elsewhere.

Bart

author
#5 lore raw

Re: [PATCH 2/3] gpio: atcgpio100: Add ATCGPIO100 GPIO driver

Hi Bartosz,

Thanks for the review.

Quoted 4 lines · Bartosz Golaszewski
Quoted 1 line

+#include <linux/mod_devicetable.h>

Uwe would probably say you should only pull in the individual headers that you require from include/linux/device-id/.

As Uwe pointed out, <linux/platform_device.h> already provides of_device_id, so I will drop <linux/mod_devicetable.h> in v2.

Quoted 11 lines

drivers/gpio/gpio-atcgpio100.c line 461

Quoted 8 lines
drivers/gpio/gpio-atcgpio100.c new file +8 -0
+	config = (struct gpio_generic_chip_config) {
+		.dev = &priv->pdev->dev,
+		.sz = 4,
+		.dat = priv->base + REG_DATA_IN,
+		.set = priv->base + REG_DATA_OUT,
+		.dirout = priv->base + REG_CHAN_DIR,
+		.flags = GPIO_GENERIC_READ_OUTPUT_REG_SET,
+	};

I think you could save even more code by using the gpio-regmap helper here instead. Could you see if that's possible here?

I tried it. The conversion is doable, but the driver grows from 641 to 721 lines. The extra code comes from:

- IRQ handling: gpio-regmap only accepts an external irq_domain, so the driver has to create the domain and duplicate what GPIOLIB_IRQCHIP provides today (map/xlate ops, lockdep classes, resource request/release callbacks, and removing the chained handler and disposing of the mappings on unbind). This accounts for most of the growth. Using regmap-irq instead would turn the chained hardirq handler into a threaded one, and the 3-bit trigger mode fields (one per line, spread across four registers) would still need a custom set_type_config() callback.

- Output readback: the datasheet documents DATA_IN for input lines only, so output lines are read back from DATA_OUT. With gpio-mmio this is handled by GPIO_GENERIC_READ_OUTPUT_REG_SET; with gpio-regmap it requires a reg_mask_xlate() callback that reads the direction register on every get.

- Shared debounce cleanup: the debounce period is shared by all lines, and the driver clears a line's debounce enable bit in .free() so that a released line does not block a different period on another line. gpio-regmap does not let the driver provide .free(), so the enable bit stays set after release and the conflict check has to skip lines that are no longer requested.

Given that, I would prefer to keep gpio-mmio for v2. If you still prefer gpio-regmap, I can send that version instead.

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (priv->chan_num > ATCGPIO_CHAN_NUM_MAX || !priv->chan_num) {
+		dev_warn(&priv->pdev->dev,

Use a local variable to store the adress of dev for better readability.

Will do in v2, both here in atcgpio_init_controller() and in atcgpio_init_resources().

Quoted 3 lines

drivers/gpio/gpio-atcgpio100.c line 515

Quoted 1 line
drivers/gpio/gpio-atcgpio100.c new file +1 -0
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);

Use devm_kzalloc_obj(). Same elsewhere.

Will do in v2. I will also convert the parents array to devm_kzalloc_objs().

Thanks,
CL

#6 lore raw

Re: [PATCH 2/3] gpio: atcgpio100: Add ATCGPIO100 GPIO driver

In reply to CL Wang

drivers/gpio/gpio-atcgpio100.c line 24

Quoted 3 lines · CL Wang
drivers/gpio/gpio-atcgpio100.c new file +2 -0
+#include <linux/mod_devicetable.h>
[...]
+#include <linux/platform_device.h>

<linux/platform_device.h> already provides of_device_id and <linux/mod_devicetable.h> adds a lot of unneeded build dependencies. So please drop <linux/mod_devicetable.h>.

Best regards
Uwe

author
#7 lore raw

PATCH 3/3 MAINTAINERS: Add entry for Andes ATCGPIO100 GPIO

In reply to CL Wang

Add MAINTAINERS entry for the Andes ATCGPIO100 GPIO controller driver.

---
 MAINTAINERS | 6 ++++++
 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
index 222b7fc2c3aa..ba1930a37596 100644
@@ -2042,6 +2042,12 @@ S:	Supported
 F:	drivers/clk/analogbits/*
 F:	include/linux/clk/analogbits*
 
+ANDES ATCGPIO100 GPIO DRIVER
+M:	CL Wang <[email protected]>
+S:	Supported
+F:	Documentation/devicetree/bindings/gpio/andestech,ae350-gpio.yaml
+F:	drivers/gpio/gpio-atcgpio100.c
+
 ANDES ATCSPI200 SPI DRIVER
 M:	CL Wang <[email protected]>
 S:	Supported
Signature · 1 line
2.34.1