Skip to contentSkip to conversation

Add support for nuvoton ma35d1 pin control

PATCH Superseded Jacky Huang 3 participants 8 messages Mar 13, 2024 – Mar 29, 2024

Subsystems (5)

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v6 cover Add support for nuvoton ma35d1 pin control

From: Jacky Huang <[email protected]>

This patch series adds the pin control and GPIO driver for the nuvoton ma35d1 ARMv8 SoC. It includes DT binding documentation and the ma35d1 pin control driver.

This pin control driver has been tested on the ma35d1 som board with Linux 6.8-rc5 .

v6:
- Remove DTS from this patchset. The DTS will be submitted in another patchset.

v5:
- Update the pinctrl driver header file pinctrl-ma35.h
- Include platform_device.h to fix compile issues.

v4:
- Update the pinctrl driver Kconfig
- Add depends to CONFIG_PINCTRL_MA35D1 to prevent compilation errors.
- Update the pinctrl driver
- Utilize devm_kcalloc() instead of devm_kzalloc().
- Employ ARRAY_SIZE() instead of sizeof()/sizeof().

v3:
- Update DTS and YAML files
- Corrected the unit address of nodes gpioa ~ gpion.
- Removed the invalid "pin-default" node.
- Removed the phandle entry from "nuvoton,pins".
- Update pinctrl driver
- Fixed the Kconfig by using "depend on" instead of "if".
- Removed unused #include of header files.
- Utilized immutable irq_chip instead of dynamic irq_chip.
- Replaced ma35_dt_free_map() with pinconf_generic_dt_free_map().
- Implemented other minor fixes as suggested by the reviewer.

v2:
- Update nuvoton,ma35d1-pinctrl.yaml
- Update the 'nuvoton,pins' to follow the style of rockchip pinctrl approch.
- Use power-source to indicate the pin voltage selection which follow the realtek pinctrl approch.
- Instead of integer, use drive-strength-microamp to specify the real driving strength capability of IO pins.
- Update ma35d1 pinctrl driver
- Add I/O drive strength lookup table for translating device tree setting into control register.
- Remove ma35d1-pinfunc.h which is unused after update definition of 'nuvoton,pins'.

Jacky Huang (3):
dt-bindings: reset: Add syscon to nuvoton ma35d1 system-management
node
dt-bindings: pinctrl: Document nuvoton ma35d1 pin control
pinctrl: nuvoton: Add ma35d1 pinctrl and GPIO driver

 .../pinctrl/nuvoton,ma35d1-pinctrl.yaml       |  163 ++
 .../bindings/reset/nuvoton,ma35d1-reset.yaml  |    3 +-
 drivers/pinctrl/nuvoton/Kconfig               |   19 +
 drivers/pinctrl/nuvoton/Makefile              |    2 +
 drivers/pinctrl/nuvoton/pinctrl-ma35.c        | 1211 +++++++++++
 drivers/pinctrl/nuvoton/pinctrl-ma35.h        |   51 +
 drivers/pinctrl/nuvoton/pinctrl-ma35d1.c      | 1797 +++++++++++++++++
 7 files changed, 3245 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/pinctrl/nuvoton,ma35d1-pinctrl.yaml
 create mode 100644 drivers/pinctrl/nuvoton/pinctrl-ma35.c
 create mode 100644 drivers/pinctrl/nuvoton/pinctrl-ma35.h
 create mode 100644 drivers/pinctrl/nuvoton/pinctrl-ma35d1.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v6 1/3 dt-bindings: reset: Add syscon to nuvoton ma35d1 system-management node

From: Jacky Huang <[email protected]>

Add a compatible 'syscon' to the system management node since the system control registers are mapped by this driver. The other driver must access the system control registers through 'regmap' using a phandle that references this node.

---
 .../devicetree/bindings/reset/nuvoton,ma35d1-reset.yaml        | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/reset/nuvoton,ma35d1-reset.yaml +2 -1
index 34c5c1c08ec1..3ce7dcecd87a 100644
@@ -18,6 +18,7 @@ properties:
   compatible:
     items:
       - const: nuvoton,ma35d1-reset
+      - const: syscon
 
   reg:
     maxItems: 1
@@ -37,7 +38,7 @@ examples:
   - |
 
     system-management@40460000 {
-        compatible = "nuvoton,ma35d1-reset";
+        compatible = "nuvoton,ma35d1-reset", "syscon";
         reg = <0x40460000 0x200>;
         #reset-cells = <1>;
     };
Signature · 1 line
2.34.1
#2 lore raw

PATCH v6 2/3 dt-bindings: pinctrl: Document nuvoton ma35d1 pin control

In reply to Jacky Huang

From: Jacky Huang <[email protected]>

Add documentation to describe nuvoton ma35d1 pin control and GPIO.

---
 .../pinctrl/nuvoton,ma35d1-pinctrl.yaml       | 163 ++++++++++++++++++
 1 file changed, 163 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pinctrl/nuvoton,ma35d1-pinctrl.yaml
Documentation/devicetree/bindings/pinctrl/nuvoton,ma35d1-pinctrl.yaml new file +163 -0
new file mode 100644
index 000000000000..8b9ec263213f
@@ -0,0 +1,163 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/pinctrl/nuvoton,ma35d1-pinctrl.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Nuvoton MA35D1 pin control and GPIO
+
+maintainers:
+  - Shan-Chun Hung <[email protected]>
+  - Jacky Huang <[email protected]>
+
+allOf:
+  - $ref: pinctrl.yaml#
+
+properties:
+  compatible:
+    enum:
+      - nuvoton,ma35d1-pinctrl
+
+  '#address-cells':
+    const: 1
+
+  '#size-cells':
+    const: 1
+
+  nuvoton,sys:
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description:
+      phandle of the system-management node.
+
+  ranges: true
+
+patternProperties:
+  "^gpio@[0-9a-f]+$":
+    type: object
+    additionalProperties: false
+    properties:
+      gpio-controller: true
+
+      '#gpio-cells':
+        const: 2
+
+      reg:
+        maxItems: 1
+
+      clocks:
+        maxItems: 1
+
+      interrupt-controller: true
+
+      '#interrupt-cells':
+        const: 2
+
+      interrupts:
+        description:
+          The interrupt outputs to sysirq.
+        maxItems: 1
+
+    required:
+      - gpio-controller
+      - '#gpio-cells'
+      - reg
+      - clocks
+      - interrupt-controller
+      - '#interrupt-cells'
+      - interrupts
+
+  "^pin-[a-z0-9]+$":
+    type: object
+    description:
+      A pinctrl node should contain at least one subnodes representing the
+      pinctrl groups available on the machine. Each subnode will list the
+      pins it needs, and how they should be configured, with regard to muxer
+      configuration, pullups, drive strength, input enable/disable and input
+      schmitt.
+
+    $ref: pincfg-node.yaml#
+
+    properties:
+      power-source:
+        description: |
+          Valid arguments are described as below:
+          0: power supply of 1.8V
+          1: power supply of 3.3V
+        enum: [0, 1]
+
+      drive-strength-microamp:
+        oneOf:
+          - enum: [ 2900, 4400, 5800, 7300, 8600, 10100, 11500, 13000 ]
+            description: 1.8V I/O driving strength
+          - enum: [ 17100, 25600, 34100, 42800, 48000, 56000, 77000, 82000 ]
+            description: 3.3V I/O driving strength
+
+    unevaluatedProperties: false
+
+  "-grp$":
+    type: object
+    description:
+      Pinctrl node's client devices use subnodes for desired pin configuration.
+      Client device subnodes use below standard properties.
+    properties:
+      nuvoton,pins:
+        description:
+          Each entry consists of 4 parameters and represents the mux and config
+          setting for one pin.
+        $ref: /schemas/types.yaml#/definitions/uint32-matrix
+        minItems: 1
+        items:
+          items:
+            - minimum: 0
+              maximum: 13
+              description:
+                Pin bank.
+            - minimum: 0
+              maximum: 15
+              description:
+                Pin bank index.
+            - minimum: 0
+              maximum: 15
+              description:
+                Mux 0 means GPIO and mux 1 to 15 means the specific device function.
+
+required:
+  - compatible
+  - nuvoton,sys
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    #include <dt-bindings/gpio/gpio.h>
+    #include <dt-bindings/clock/nuvoton,ma35d1-clk.h>
+
+    pinctrl@40040000 {
+        compatible = "nuvoton,ma35d1-pinctrl";
+        #address-cells = <1>;
+        #size-cells = <1>;
+        nuvoton,sys = <&sys>;
+        ranges = <0 0x40040000 0xc00>;
+
+        gpio@0 {
+            reg = <0x0 0x40>;
+            interrupts = <GIC_SPI 14 IRQ_TYPE_LEVEL_HIGH>;
+            clocks = <&clk GPA_GATE>;
+            gpio-controller;
+            #gpio-cells = <2>;
+            interrupt-controller;
+            #interrupt-cells = <2>;
+        };
+
+        uart-grp {
+            uart11-pins {
+                nuvoton,pins = <11 0 2>,
+                               <11 1 2>,
+                               <11 2 2>,
+                               <11 3 2>;
+                bias-disable;
+                power-source = <1>;
+            };
+        };
+    };
Signature · 1 line
2.34.1
#3 lore raw

PATCH v6 3/3 pinctrl: nuvoton: Add ma35d1 pinctrl and GPIO driver

In reply to Jacky Huang

From: Jacky Huang <[email protected]>

Add common pinctrl and GPIO driver for Nuvoton MA35 series SoC, and add support for ma35d1 pinctrl.

---
 drivers/pinctrl/nuvoton/Kconfig          |   19 +
 drivers/pinctrl/nuvoton/Makefile         |    2 +
 drivers/pinctrl/nuvoton/pinctrl-ma35.c   | 1211 +++++++++++++++
 drivers/pinctrl/nuvoton/pinctrl-ma35.h   |   51 +
 drivers/pinctrl/nuvoton/pinctrl-ma35d1.c | 1797 ++++++++++++++++++++++
 5 files changed, 3080 insertions(+)
 create mode 100644 drivers/pinctrl/nuvoton/pinctrl-ma35.c
 create mode 100644 drivers/pinctrl/nuvoton/pinctrl-ma35.h
 create mode 100644 drivers/pinctrl/nuvoton/pinctrl-ma35d1.c
drivers/pinctrl/nuvoton/Kconfig +19 -0
index 2abbfcec1fae..7eadaaf48d6e 100644
@@ -45,3 +45,22 @@ config PINCTRL_NPCM8XX
 	  Say Y or M here to enable pin controller and GPIO support for
 	  the Nuvoton NPCM8XX SoC. This is strongly recommended when
 	  building a kernel that will run on this chip.
+
+config PINCTRL_MA35
+	bool
+	depends on (ARCH_MA35 || COMPILE_TEST) && OF
+	select GENERIC_PINCTRL_GROUPS
+	select GENERIC_PINMUX_FUNCTIONS
+	select GENERIC_PINCONF
+	select GPIOLIB
+	select GPIO_GENERIC
+	select GPIOLIB_IRQCHIP
+	select MFD_SYSCON
+
+config PINCTRL_MA35D1
+	bool "Pinctrl and GPIO driver for Nuvoton MA35D1"
+	depends on (ARCH_MA35 || COMPILE_TEST) && OF
+	select PINCTRL_MA35
+	help
+	  Say Y here to enable pin controller and GPIO support
+	  for Nuvoton MA35D1 SoC.
drivers/pinctrl/nuvoton/Makefile +2 -0
index 08031eab0af6..346c5082bc60 100644
@@ -4,3 +4,5 @@
 obj-$(CONFIG_PINCTRL_WPCM450)	+= pinctrl-wpcm450.o
 obj-$(CONFIG_PINCTRL_NPCM7XX)	+= pinctrl-npcm7xx.o
 obj-$(CONFIG_PINCTRL_NPCM8XX)	+= pinctrl-npcm8xx.o
+obj-$(CONFIG_PINCTRL_MA35)	+= pinctrl-ma35.o
+obj-$(CONFIG_PINCTRL_MA35D1)	+= pinctrl-ma35d1.o
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +1211 -0

1,211 lines not on this page · raw message

drivers/pinctrl/nuvoton/pinctrl-ma35.h new file +51 -0

51 lines not on this page · raw message

drivers/pinctrl/nuvoton/pinctrl-ma35d1.c new file +1797 -0

1,711 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.34.1

Part of the diff is not shown here. View the raw message.

#4 lore raw

Re: [PATCH v6 3/3] pinctrl: nuvoton: Add ma35d1 pinctrl and GPIO driver

Quoted 30 lines · Jacky Huang
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +30 -0
+static int ma35_gpiolib_register(struct platform_device *pdev, struct ma35_pinctrl *npctl)
+{
+	struct ma35_pin_ctrl *ctrl = npctl->ctrl;
+	struct ma35_pin_bank *bank = ctrl->pin_banks;
+	int ret;
+	int i;
+
+	for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
+		if (!bank->valid) {
+			dev_warn(&pdev->dev, "bank %s is not valid\n",
+				 bank->np->name);
+			continue;
+		}
+		bank->irqtype = 0;
+		bank->irqinten = 0;
+		bank->chip.label = bank->name;
+		bank->chip.of_gpio_n_cells = 2;
+		bank->chip.parent = &pdev->dev;
+		bank->chip.request = ma35_gpio_core_to_request;
+		bank->chip.direction_input = ma35_gpio_core_direction_in;
+		bank->chip.direction_output = ma35_gpio_core_direction_out;
+		bank->chip.get = ma35_gpio_core_get;
+		bank->chip.set = ma35_gpio_core_set;
+		bank->chip.base = -1;
+		bank->chip.ngpio = bank->nr_pins;
+		bank->chip.can_sleep = false;
+		spin_lock_init(&bank->lock);
+
+		if (bank->irq > 0) {
+			struct gpio_irq_chip *girq;

drivers/pinctrl/nuvoton/pinctrl-ma35.c line 545

Quoted 10 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +10 -0
+
+			girq = &bank->chip.irq;
+			gpio_irq_chip_set_chip(girq, &ma35_gpio_irqchip);
+			girq->parent_handler = ma35_irq_demux_intgroup;
+			girq->num_parents = 1;
+
+			girq->parents = devm_kcalloc(&pdev->dev, 1, sizeof(*girq->parents),
+						     GFP_KERNEL);
+			if (!girq->parents)
+				return -ENOMEM;

ret = -ENOMEM;
goto fail;

regards,
dan carpenter

Quoted 23 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +23 -0
+
+			girq->parents[0] = bank->irq;
+			girq->default_type = IRQ_TYPE_NONE;
+			girq->handler = handle_level_irq;
+		}
+
+		ret = gpiochip_add_data(&bank->chip, bank);
+		if (ret) {
+			dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
+				bank->chip.label, ret);
+			goto fail;
+		}
+	}
+	return 0;
+
+fail:
+	for (--i, --bank; i >= 0; --i, --bank) {
+		if (!bank->valid)
+			continue;
+		gpiochip_remove(&bank->chip);
+	}
+	return ret;
+}
#5 lore raw

Re: [PATCH v6 3/3] pinctrl: nuvoton: Add ma35d1 pinctrl and GPIO driver

Quoted 44 lines · Dan Carpenter
Quoted 30 lines · Jacky Huang
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +30 -0
+static int ma35_gpiolib_register(struct platform_device *pdev, struct ma35_pinctrl *npctl)
+{
+	struct ma35_pin_ctrl *ctrl = npctl->ctrl;
+	struct ma35_pin_bank *bank = ctrl->pin_banks;
+	int ret;
+	int i;
+
+	for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
+		if (!bank->valid) {
+			dev_warn(&pdev->dev, "bank %s is not valid\n",
+				 bank->np->name);
+			continue;
+		}
+		bank->irqtype = 0;
+		bank->irqinten = 0;
+		bank->chip.label = bank->name;
+		bank->chip.of_gpio_n_cells = 2;
+		bank->chip.parent = &pdev->dev;
+		bank->chip.request = ma35_gpio_core_to_request;
+		bank->chip.direction_input = ma35_gpio_core_direction_in;
+		bank->chip.direction_output = ma35_gpio_core_direction_out;
+		bank->chip.get = ma35_gpio_core_get;
+		bank->chip.set = ma35_gpio_core_set;
+		bank->chip.base = -1;
+		bank->chip.ngpio = bank->nr_pins;
+		bank->chip.can_sleep = false;
+		spin_lock_init(&bank->lock);
+
+		if (bank->irq > 0) {
+			struct gpio_irq_chip *girq;

drivers/pinctrl/nuvoton/pinctrl-ma35.c line 545

Quoted 10 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +10 -0
+
+			girq = &bank->chip.irq;
+			gpio_irq_chip_set_chip(girq, &ma35_gpio_irqchip);
+			girq->parent_handler = ma35_irq_demux_intgroup;
+			girq->num_parents = 1;
+
+			girq->parents = devm_kcalloc(&pdev->dev, 1, sizeof(*girq->parents),
+						     GFP_KERNEL);
+			if (!girq->parents)
+				return -ENOMEM;

ret = -ENOMEM;
goto fail;

Quoted 2 lines

regards,
dan carpenter

Thanks, I will fix it.

Quoted 15 lines
Quoted 15 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +15 -0
+
+			girq->parents[0] = bank->irq;
+			girq->default_type = IRQ_TYPE_NONE;
+			girq->handler = handle_level_irq;
+		}
+
+		ret = gpiochip_add_data(&bank->chip, bank);
+		if (ret) {
+			dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
+				bank->chip.label, ret);
+			goto fail;
+		}
+	}
+	return 0;
+
Quoted 8 lines

drivers/pinctrl/nuvoton/pinctrl-ma35.c line 568

Quoted 8 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +8 -0
+fail:
+	for (--i, --bank; i >= 0; --i, --bank) {
+		if (!bank->valid)
+			continue;
+		gpiochip_remove(&bank->chip);
+	}
+	return ret;
+}

Best Regards,
Jacky Huang

#6 lore raw

Re: [PATCH v6 3/3] pinctrl: nuvoton: Add ma35d1 pinctrl and GPIO driver

In reply to Jacky Huang

Hi Jacky,

overall this looks very good.

Quoted 6 lines · Jacky Huang

From: Jacky Huang <[email protected]>

Add common pinctrl and GPIO driver for Nuvoton MA35 series SoC, and add support for ma35d1 pinctrl.

(...)

Quoted 4 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +4 -0
+static int ma35_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned int selector,
+                              unsigned int group)
+{
+       struct ma35_pinctrl *npctl = pinctrl_dev_get_drvdata(pctldev);

drivers/pinctrl/nuvoton/pinctrl-ma35.c line 278

Quoted 10 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +10 -0
+       struct ma35_pin_group *grp = &npctl->groups[group];
+       struct ma35_pin_setting *setting = grp->settings;
+       u32 i, regval;
+
+       dev_dbg(npctl->dev, "enable function %s group %s\n",
+               npctl->functions[selector].name, npctl->groups[group].name);
+
+       for (i = 0; i < grp->npins; i++) {
+               regmap_read(npctl->regmap, setting->offset, &regval);
+               regval &= ~GENMASK(setting->shift + 3, setting->shift);

Add a comment explaining why you add +3

drivers/pinctrl/nuvoton/pinctrl-ma35.c line 305

Quoted 12 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +12 -0
+static int ma35_gpio_core_direction_in(struct gpio_chip *gc, unsigned int gpio)
+{
+       struct ma35_pin_bank *bank = gpiochip_get_data(gc);
+       void __iomem *reg_mode = bank->reg_base + MA35_GP_REG_MODE;
+       unsigned long flags;
+       unsigned int regval;
+
+       spin_lock_irqsave(&bank->lock, flags);
+
+       regval = readl(reg_mode);
+       regval &= ~GENMASK(gpio * 2 + 1, gpio * 2);
+       regval |= MA35_GP_MODE_INPUT << gpio * 2;

Here the first time you do this magic explain in a comment why you use *2+1 and *2 overall (I guess two bits per line).

drivers/pinctrl/nuvoton/pinctrl-ma35.c line 317

Quoted 5 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +5 -0
+static int ma35_gpio_core_get(struct gpio_chip *gc, unsigned int gpio)
+{
+       struct ma35_pin_bank *bank = gpiochip_get_data(gc);
+
+       return readl(bank->reg_base + MA35_PIN_MAP_BASE + gpio * 4);

Here add a comment explaining the *4
I guess one 32-bit register per pin?

Quoted 46 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +46 -0
+static int ma35_irq_irqtype(struct irq_data *d, unsigned int type)
+{
+       struct ma35_pin_bank *bank = gpiochip_get_data(irq_data_get_irq_chip_data(d));
+       void __iomem *reg_itype = bank->reg_base + MA35_GP_REG_INTTYPE;
+       void __iomem *reg_ien = bank->reg_base + MA35_GP_REG_INTEN;
+       unsigned int num = (d->hwirq);
+
+       if (type == IRQ_TYPE_PROBE) {
+               writel(readl(reg_itype) & ~BIT(num), reg_itype);
+               writel(readl(reg_ien) | BIT(num) | BIT(num + 16), reg_ien);
+               bank->irqtype &= ~BIT(num);
+               bank->irqinten |= BIT(num) | BIT(num + 16);
+               return 0;
+       }
+
+       if (type & IRQ_TYPE_LEVEL_MASK) {
+               writel(readl(reg_itype) | BIT(num), reg_itype);
+               writel(readl(reg_ien) & ~(BIT(num) | BIT(num + 16)), reg_ien);
+               bank->irqtype |= BIT(num);
+               bank->irqinten &= ~(BIT(num) | BIT(num + 16));
+               if (type == IRQ_TYPE_LEVEL_HIGH) {
+                       writel(readl(reg_ien) | BIT(num + 16), reg_ien);
+                       bank->irqinten |= BIT(num + 16);
+                       return 0;
+               }
+
+               if (type == IRQ_TYPE_LEVEL_LOW) {
+                       writel(readl(reg_ien) | BIT(num), reg_ien);
+                       bank->irqinten |= BIT(num);
+                       return 0;
+               }
+
+       } else {
+               writel(readl(reg_itype) & ~BIT(num), reg_itype);
+               bank->irqtype &= ~BIT(num);
+
+               if (type & IRQ_TYPE_EDGE_RISING) {
+                       writel(readl(reg_ien) | BIT(num + 16), reg_ien);
+                       bank->irqinten |= BIT(num + 16);
+
+               } else {
+                       writel(readl(reg_ien) & ~BIT(num + 16), reg_ien);
+                       bank->irqinten &= ~BIT(num + 16);
+               }
+
+               if (type & IRQ_TYPE_EDGE_FALLING) {

drivers/pinctrl/nuvoton/pinctrl-ma35.c line 470

Quoted 10 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +10 -0
+                       writel(readl(reg_ien) | BIT(num), reg_ien);
+                       bank->irqinten |= BIT(num);
+
+               } else {
+                       writel(readl(reg_ien) & ~BIT(num), reg_ien);
+                       bank->irqinten &= ~BIT(num);
+               }
+       }
+       return 0;
+}

I don't understand why you don't set the irq_handler:
irq_set_handler_locked(d, handle_edge_irq); irq_set_handler_locked(d, handle_level_irq);

It seems you are not handling IRQ_TYPE_EDGE_BOTH?
What happens if both rising and falling is specified simultaneously?

The if/else nesting is hard to read.
switch (type) {
case IRQ_TYPE_EDGE_BOTH:
(...)
case IRQ_TYPE_EDGE_RISING:
(...)

See drivers/gpio/gpio-ftgpio010.c for an example.

Have you checked that handling edge and level IRQs really work as expected?

Quoted 34 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +34 -0
+static int ma35_gpiolib_register(struct platform_device *pdev, struct ma35_pinctrl *npctl)
+{
+       struct ma35_pin_ctrl *ctrl = npctl->ctrl;
+       struct ma35_pin_bank *bank = ctrl->pin_banks;
+       int ret;
+       int i;
+
+       for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
+               if (!bank->valid) {
+                       dev_warn(&pdev->dev, "bank %s is not valid\n",
+                                bank->np->name);
+                       continue;
+               }
+               bank->irqtype = 0;
+               bank->irqinten = 0;
+               bank->chip.label = bank->name;
+               bank->chip.of_gpio_n_cells = 2;
+               bank->chip.parent = &pdev->dev;
+               bank->chip.request = ma35_gpio_core_to_request;
+               bank->chip.direction_input = ma35_gpio_core_direction_in;
+               bank->chip.direction_output = ma35_gpio_core_direction_out;
+               bank->chip.get = ma35_gpio_core_get;
+               bank->chip.set = ma35_gpio_core_set;
+               bank->chip.base = -1;
+               bank->chip.ngpio = bank->nr_pins;
+               bank->chip.can_sleep = false;
+               spin_lock_init(&bank->lock);
+
+               if (bank->irq > 0) {
+                       struct gpio_irq_chip *girq;
+
+                       girq = &bank->chip.irq;
+                       gpio_irq_chip_set_chip(girq, &ma35_gpio_irqchip);
+                       girq->parent_handler = ma35_irq_demux_intgroup;

drivers/pinctrl/nuvoton/pinctrl-ma35.c line 549

Quoted 10 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +10 -0
+                       girq->num_parents = 1;
+
+                       girq->parents = devm_kcalloc(&pdev->dev, 1, sizeof(*girq->parents),
+                                                    GFP_KERNEL);
+                       if (!girq->parents)
+                               return -ENOMEM;
+
+                       girq->parents[0] = bank->irq;
+                       girq->default_type = IRQ_TYPE_NONE;
+                       girq->handler = handle_level_irq;

Does this really work for the edge IRQs?

I recommend setting this to handle_bad_irq and assign the right handler in .set_type().

Yours,
Linus Walleij

#7 lore raw

Re: [PATCH v6 3/3] pinctrl: nuvoton: Add ma35d1 pinctrl and GPIO driver

Dear Linus,

Thanks for your review.

Quoted 14 lines · Linus Walleij

Hi Jacky,

overall this looks very good.

Quoted 6 lines · Jacky Huang

From: Jacky Huang <[email protected]>

Add common pinctrl and GPIO driver for Nuvoton MA35 series SoC, and add support for ma35d1 pinctrl.

(...)

Quoted 15 lines
Quoted 4 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +4 -0
+static int ma35_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned int selector,
+                              unsigned int group)
+{
+       struct ma35_pinctrl *npctl = pinctrl_dev_get_drvdata(pctldev);

drivers/pinctrl/nuvoton/pinctrl-ma35.c line 278

Quoted 10 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +10 -0
+       struct ma35_pin_group *grp = &npctl->groups[group];
+       struct ma35_pin_setting *setting = grp->settings;
+       u32 i, regval;
+
+       dev_dbg(npctl->dev, "enable function %s group %s\n",
+               npctl->functions[selector].name, npctl->groups[group].name);
+
+       for (i = 0; i < grp->npins; i++) {
+               regmap_read(npctl->regmap, setting->offset, &regval);
+               regval &= ~GENMASK(setting->shift + 3, setting->shift);

Add a comment explaining why you add +3

The pinmux selection is 4 bits. I will use a constant for the bitmask width instead.

Quoted 15 lines

drivers/pinctrl/nuvoton/pinctrl-ma35.c line 305

Quoted 13 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +12 -0
+static int ma35_gpio_core_direction_in(struct gpio_chip *gc, unsigned int gpio)
+{
+       struct ma35_pin_bank *bank = gpiochip_get_data(gc);
+       void __iomem *reg_mode = bank->reg_base + MA35_GP_REG_MODE;
+       unsigned long flags;
+       unsigned int regval;
+
+       spin_lock_irqsave(&bank->lock, flags);
+
+       regval = readl(reg_mode);
+       regval &= ~GENMASK(gpio * 2 + 1, gpio * 2);
+       regval |= MA35_GP_MODE_INPUT << gpio * 2;

Here the first time you do this magic explain in a comment why you use *2+1 and *2 overall (I guess two bits per line).

Yes, it is two bits per pin. I will add a comment to explain this.

Quoted 7 lines

drivers/pinctrl/nuvoton/pinctrl-ma35.c line 317

Quoted 5 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +5 -0
+static int ma35_gpio_core_get(struct gpio_chip *gc, unsigned int gpio)
+{
+       struct ma35_pin_bank *bank = gpiochip_get_data(gc);
+
+       return readl(bank->reg_base + MA35_PIN_MAP_BASE + gpio * 4);

Here add a comment explaining the *4
I guess one 32-bit register per pin?

Yes, it maps one 32-bit register to a gpio pin.
I will add a comment to explain this.

Quoted 51 lines
Quoted 51 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +51 -0
+static int ma35_irq_irqtype(struct irq_data *d, unsigned int type)
+{
+       struct ma35_pin_bank *bank = gpiochip_get_data(irq_data_get_irq_chip_data(d));
+       void __iomem *reg_itype = bank->reg_base + MA35_GP_REG_INTTYPE;
+       void __iomem *reg_ien = bank->reg_base + MA35_GP_REG_INTEN;
+       unsigned int num = (d->hwirq);
+
+       if (type == IRQ_TYPE_PROBE) {
+               writel(readl(reg_itype) & ~BIT(num), reg_itype);
+               writel(readl(reg_ien) | BIT(num) | BIT(num + 16), reg_ien);
+               bank->irqtype &= ~BIT(num);
+               bank->irqinten |= BIT(num) | BIT(num + 16);
+               return 0;
+       }
+
+       if (type & IRQ_TYPE_LEVEL_MASK) {
+               writel(readl(reg_itype) | BIT(num), reg_itype);
+               writel(readl(reg_ien) & ~(BIT(num) | BIT(num + 16)), reg_ien);
+               bank->irqtype |= BIT(num);
+               bank->irqinten &= ~(BIT(num) | BIT(num + 16));
+               if (type == IRQ_TYPE_LEVEL_HIGH) {
+                       writel(readl(reg_ien) | BIT(num + 16), reg_ien);
+                       bank->irqinten |= BIT(num + 16);
+                       return 0;
+               }
+
+               if (type == IRQ_TYPE_LEVEL_LOW) {
+                       writel(readl(reg_ien) | BIT(num), reg_ien);
+                       bank->irqinten |= BIT(num);
+                       return 0;
+               }
+
+       } else {
+               writel(readl(reg_itype) & ~BIT(num), reg_itype);
+               bank->irqtype &= ~BIT(num);
+
+               if (type & IRQ_TYPE_EDGE_RISING) {
+                       writel(readl(reg_ien) | BIT(num + 16), reg_ien);
+                       bank->irqinten |= BIT(num + 16);
+
+               } else {
+                       writel(readl(reg_ien) & ~BIT(num + 16), reg_ien);
+                       bank->irqinten &= ~BIT(num + 16);
+               }
+
+               if (type & IRQ_TYPE_EDGE_FALLING) {
+                       writel(readl(reg_ien) | BIT(num), reg_ien);
+                       bank->irqinten |= BIT(num);
+
+               } else {
+                       writel(readl(reg_ien) & ~BIT(num), reg_ien);
Quoted 8 lines

drivers/pinctrl/nuvoton/pinctrl-ma35.c line 470

Quoted 5 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +5 -0
+                       bank->irqinten &= ~BIT(num);
+               }
+       }
+       return 0;
+}

I don't understand why you don't set the irq_handler:
irq_set_handler_locked(d, handle_edge_irq); irq_set_handler_locked(d, handle_level_irq);

I will add the irq_set_handler_locked().

Quoted 11 lines

It seems you are not handling IRQ_TYPE_EDGE_BOTH?
What happens if both rising and falling is specified simultaneously?

The if/else nesting is hard to read.
switch (type) {
case IRQ_TYPE_EDGE_BOTH:
(...)
case IRQ_TYPE_EDGE_RISING:
(...)

See drivers/gpio/gpio-ftgpio010.c for an example.

We'll refer to this driver to modify our code.

Quoted 2 lines

Have you checked that handling edge and level IRQs really work as expected?

This driver works with edge or level IRQs in linux-5.10, and some modifications have been made for upstream. We'll verify if it also works in linux-6.9.

Quoted 46 lines
Quoted 34 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +34 -0
+static int ma35_gpiolib_register(struct platform_device *pdev, struct ma35_pinctrl *npctl)
+{
+       struct ma35_pin_ctrl *ctrl = npctl->ctrl;
+       struct ma35_pin_bank *bank = ctrl->pin_banks;
+       int ret;
+       int i;
+
+       for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
+               if (!bank->valid) {
+                       dev_warn(&pdev->dev, "bank %s is not valid\n",
+                                bank->np->name);
+                       continue;
+               }
+               bank->irqtype = 0;
+               bank->irqinten = 0;
+               bank->chip.label = bank->name;
+               bank->chip.of_gpio_n_cells = 2;
+               bank->chip.parent = &pdev->dev;
+               bank->chip.request = ma35_gpio_core_to_request;
+               bank->chip.direction_input = ma35_gpio_core_direction_in;
+               bank->chip.direction_output = ma35_gpio_core_direction_out;
+               bank->chip.get = ma35_gpio_core_get;
+               bank->chip.set = ma35_gpio_core_set;
+               bank->chip.base = -1;
+               bank->chip.ngpio = bank->nr_pins;
+               bank->chip.can_sleep = false;
+               spin_lock_init(&bank->lock);
+
+               if (bank->irq > 0) {
+                       struct gpio_irq_chip *girq;
+
+                       girq = &bank->chip.irq;
+                       gpio_irq_chip_set_chip(girq, &ma35_gpio_irqchip);
+                       girq->parent_handler = ma35_irq_demux_intgroup;

drivers/pinctrl/nuvoton/pinctrl-ma35.c line 549

Quoted 10 lines
drivers/pinctrl/nuvoton/pinctrl-ma35.c new file +10 -0
+                       girq->num_parents = 1;
+
+                       girq->parents = devm_kcalloc(&pdev->dev, 1, sizeof(*girq->parents),
+                                                    GFP_KERNEL);
+                       if (!girq->parents)
+                               return -ENOMEM;
+
+                       girq->parents[0] = bank->irq;
+                       girq->default_type = IRQ_TYPE_NONE;
+                       girq->handler = handle_level_irq;

Does this really work for the edge IRQs?

Quoted 5 lines

I recommend setting this to handle_bad_irq and assign the right handler in .set_type().

Yours,
Linus Walleij

OK, I will fix it.

Best Regards,
Jacky Huang