Skip to contentSkip to conversation

Add Nuvoton NPCM SGPIO feature

PATCH Superseded Jim Liu 3 participants 6 messages Nov 1, 2023 – Nov 2, 2023

Subsystems (3)

v7 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Jim Liu

author
#0 lore raw

v7 cover Add Nuvoton NPCM SGPIO feature

This SGPIO controller is for NUVOTON NPCM7xx and NPCM8xx SoC. Nuvoton NPCM SGPIO module is combine serial to parallel IC (HC595) and parallel to serial IC (HC165), and use APB3 clock to control it. This interface has 4 pins (D_out , D_in, S_CLK, LDSH). NPCM7xx/NPCM8xx have two sgpio module each module can support up to 64 output pins,and up to 64 input pin, the pin is only for gpi or gpo. GPIO pins have sequential, First half is gpo and second half is gpi.

Jim Liu (3):
dt-bindings: gpio: add NPCM sgpio driver bindings
arm: dts: nuvoton: npcm: Add sgpio feature
gpio: nuvoton: Add Nuvoton NPCM sgpio driver

 .../bindings/gpio/nuvoton,sgpio.yaml          |  86 +++
 .../dts/nuvoton/nuvoton-common-npcm7xx.dtsi   |  24 +
 drivers/gpio/Kconfig                          |   8 +
 drivers/gpio/Makefile                         |   1 +
 drivers/gpio/gpio-npcm-sgpio.c                | 649 ++++++++++++++++++
 5 files changed, 768 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/gpio/nuvoton,sgpio.yaml
 create mode 100644 drivers/gpio/gpio-npcm-sgpio.c
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v7 1/3 dt-bindings: gpio: add NPCM sgpio driver bindings

Add dt-bindings document for the Nuvoton NPCM7xx sgpio driver

---
Changes for v7:
- no change
Changes for v6:
- Drop quotes for $ref
- Add and drop '|' for description
- Add space after 'exposed.'
- remove status
Changes for v5:
- remove bus bus-frequency
- modify in/out description
Changes for v4:
- modify in/out property
- modify bus-frequency property
Changes for v3:
- modify description
- modify in/out property name
Changes for v2:
- modify description

---
 .../bindings/gpio/nuvoton,sgpio.yaml          | 86 +++++++++++++++++++
 1 file changed, 86 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/gpio/nuvoton,sgpio.yaml
Documentation/devicetree/bindings/gpio/nuvoton,sgpio.yaml new file +86 -0
new file mode 100644
index 000000000000..84e0dbcb066c
@@ -0,0 +1,86 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/gpio/nuvoton,sgpio.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Nuvoton SGPIO controller
+
+maintainers:
+  - Jim LIU <[email protected]>
+
+description: |
+  This SGPIO controller is for NUVOTON NPCM7xx and NPCM8xx SoC.
+  Nuvoton NPCM7xx SGPIO module is combine serial to parallel IC (HC595)
+  and parallel to serial IC (HC165), and use APB3 clock to control it.
+  This interface has 4 pins  (D_out , D_in, S_CLK, LDSH).
+  NPCM7xx/NPCM8xx have two sgpio module each module can support up
+  to 64 output pins,and up to 64 input pin, the pin is only for gpi or gpo.
+  GPIO pins have sequential, First half is gpo and second half is gpi.
+  GPIO pins can be programmed to support the following options
+  - Support interrupt option for each input port and various interrupt
+    sensitivity option (level-high, level-low, edge-high, edge-low)
+  - ngpios is number of nuvoton,input-ngpios GPIO lines and nuvoton,output-ngpios GPIO lines.
+    nuvoton,input-ngpios GPIO lines is only for gpi.
+    nuvoton,output-ngpios GPIO lines is only for gpo.
+
+properties:
+  compatible:
+    enum:
+      - nuvoton,npcm750-sgpio
+      - nuvoton,npcm845-sgpio
+
+  reg:
+    maxItems: 1
+
+  gpio-controller: true
+
+  '#gpio-cells':
+    const: 2
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  nuvoton,input-ngpios:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description:
+      The numbers of GPIO's exposed. GPIO lines is only for gpi.
+    minimum: 0
+    maximum: 64
+
+  nuvoton,output-ngpios:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description:
+      The numbers of GPIO's exposed. GPIO lines is only for gpo.
+    minimum: 0
+    maximum: 64
+
+required:
+  - compatible
+  - reg
+  - gpio-controller
+  - '#gpio-cells'
+  - interrupts
+  - nuvoton,input-ngpios
+  - nuvoton,output-ngpios
+  - clocks
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/nuvoton,npcm7xx-clock.h>
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    gpio8: gpio@101000 {
+        compatible = "nuvoton,npcm750-sgpio";
+        reg = <0x101000 0x200>;
+        clocks = <&clk NPCM7XX_CLK_APB3>;
+        interrupts = <GIC_SPI 19 IRQ_TYPE_LEVEL_HIGH>;
+        gpio-controller;
+        #gpio-cells = <2>;
+        nuvoton,input-ngpios = <64>;
+        nuvoton,output-ngpios = <64>;
+    };
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH v7 2/3 arm: dts: nuvoton: npcm: Add sgpio feature

In reply to Jim Liu

Add the SGPIO controller to the NPCM7xx devicetree

---
Changes for v7:
- no change
Changes for v6:
- remove bus-frequency
- check with dtbs_check
Changes for v5:
- remove npcm8xx node
Changes for v4:
- add npcm8xx gpio node
Changes for v3:
- modify node name
- modify in/out property name
Changes for v2:
- modify dts node

---
 .../dts/nuvoton/nuvoton-common-npcm7xx.dtsi   | 24 +++++++++++++++++++
 1 file changed, 24 insertions(+)
arch/arm/boot/dts/nuvoton/nuvoton-common-npcm7xx.dtsi +24 -0
index 868454ae6bde..df91517a4842 100644
@@ -372,6 +372,30 @@ &fanin12_pins &fanin13_pins
 				status = "disabled";
 			};
 
+			gpio8: gpio@101000 {
+				compatible = "nuvoton,npcm750-sgpio";
+				reg = <0x101000 0x200>;
+				clocks = <&clk NPCM7XX_CLK_APB3>;
+				interrupts = <GIC_SPI 19 IRQ_TYPE_LEVEL_HIGH>;
+				gpio-controller;
+				#gpio-cells = <2>;
+				nuvoton,input-ngpios = <64>;
+				nuvoton,output-ngpios = <64>;
+				status = "disabled";
+			};
+
+			gpio9: gpio@102000 {
+				compatible = "nuvoton,npcm750-sgpio";
+				reg = <0x102000 0x200>;
+				clocks = <&clk NPCM7XX_CLK_APB3>;
+				interrupts = <GIC_SPI 19 IRQ_TYPE_LEVEL_HIGH>;
+				gpio-controller;
+				#gpio-cells = <2>;
+				nuvoton,input-ngpios = <64>;
+				nuvoton,output-ngpios = <64>;
+				status = "disabled";
+			};
+
 			i2c0: i2c@80000 {
 				reg = <0x80000 0x1000>;
 				compatible = "nuvoton,npcm750-i2c";
Signature · 1 line
2.25.1
author
#3 lore raw

PATCH v7 3/3 gpio: nuvoton: Add Nuvoton NPCM sgpio driver

In reply to Jim Liu

Add Nuvoton BMC NPCM7xx/NPCM8xx sgpio driver support. Nuvoton NPCM SGPIO module is combine serial to parallel IC (HC595) and parallel to serial IC (HC165), and use APB3 clock to control it. This interface has 4 pins (D_out , D_in, S_CLK, LDSH).
BMC can use this driver to increase 64 gpi pins and 64 gpo pins to use.

---
Changes for v7:
- remove unused variable
- remove return in bank_reg function
- fix warning for const issue
Changes for v6:
- Remove bus-frequency property set
- Use GPIO_GENERIC
- Move iowrite8 out of the branches
- Add error codes in npcm_sgpio_get_direction
- Use used a ternary operator for npcm_sgpio_init_port
- change npcm_clk_cfg u8 to unsigned int
- Fix typo
Changes for v5:
- remove printk
- add descriptive for to_bank
- using "GPL" instead of "GPL v2"
Changes for v4:
- followed reviewer suggestion to modify npcm_sgpio_dir_in
- blank line in npcm_sgpio_dir_out
- use int type for dir in npcm_sgpio_get

Changes for v3:
- remove return in bank_reg function
Changes for v2:
- add prefix
- write the enum values in all capitals
- remove _init in npcm_sgpio_probe

---
 drivers/gpio/Kconfig           |   8 +
 drivers/gpio/Makefile          |   1 +
 drivers/gpio/gpio-npcm-sgpio.c | 649 +++++++++++++++++++++++++++++++++
 3 files changed, 658 insertions(+)
 create mode 100644 drivers/gpio/gpio-npcm-sgpio.c
drivers/gpio/Kconfig +8 -0
index 673bafb8be58..94c621c7618b 100644
@@ -478,6 +478,14 @@ config GPIO_MXS
 	select GPIO_GENERIC
 	select GENERIC_IRQ_CHIP
 
+config GPIO_NPCM_SGPIO
+	bool "Nuvoton SGPIO support"
+	depends on (ARCH_NPCM || COMPILE_TEST) && OF_GPIO
+	select GPIO_GENERIC
+	select GPIOLIB_IRQCHIP
+	help
+	  Say Y here to support Nuvoton NPCM7XX/NPCM8XX SGPIO functionality.
+
 config GPIO_OCTEON
 	tristate "Cavium OCTEON GPIO"
 	depends on CAVIUM_OCTEON_SOC
drivers/gpio/Makefile +1 -0
index eb73b5d633eb..373aa2943de5 100644
@@ -116,6 +116,7 @@ obj-$(CONFIG_GPIO_MT7621)		+= gpio-mt7621.o
 obj-$(CONFIG_GPIO_MVEBU)		+= gpio-mvebu.o
 obj-$(CONFIG_GPIO_MXC)			+= gpio-mxc.o
 obj-$(CONFIG_GPIO_MXS)			+= gpio-mxs.o
+obj-$(CONFIG_GPIO_NPCM_SGPIO)		+= gpio-npcm-sgpio.o
 obj-$(CONFIG_GPIO_OCTEON)		+= gpio-octeon.o
 obj-$(CONFIG_GPIO_OMAP)			+= gpio-omap.o
 obj-$(CONFIG_GPIO_PALMAS)		+= gpio-palmas.o
drivers/gpio/gpio-npcm-sgpio.c new file +649 -0

649 lines not on this page · raw message

Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH v7 3/3] gpio: nuvoton: Add Nuvoton NPCM sgpio driver

Quoted 5 lines · Jim Liu

Add Nuvoton BMC NPCM7xx/NPCM8xx sgpio driver support. Nuvoton NPCM SGPIO module is combine serial to parallel IC (HC595) and parallel to serial IC (HC165), and use APB3 clock to control it. This interface has 4 pins (D_out , D_in, S_CLK, LDSH).
BMC can use this driver to increase 64 gpi pins and 64 gpo pins to use.

GPI
GPO

...

Quoted 1 line

Can't be true. The absence of a new code may not be "reported".

...

drivers/gpio/Kconfig config GPIO_MXS · line 483

Quoted 3 lines
drivers/gpio/Kconfig +3 -0
+config GPIO_NPCM_SGPIO
+	bool "Nuvoton SGPIO support"
+	depends on (ARCH_NPCM || COMPILE_TEST) && OF_GPIO

No new driver should use OF_GPIO.
And I do not see where it's being used. Care to elaborate?

Quoted 14 lines
drivers/gpio/gpio-npcm-sgpio.c new file +13 -0
+	select GPIO_GENERIC
+	select GPIOLIB_IRQCHIP
 
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/gpio/driver.h>
+#include <linux/hashtable.h>
+#include <linux/init.h>
+#include <linux/io.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/spinlock.h>
+#include <linux/string.h>

...

Quoted 1 line

+#define NPCM_IXOEVCFG_MASK 0x3

GENMASK()

...

drivers/gpio/gpio-npcm-sgpio.c line 37

Quoted 2 lines
drivers/gpio/gpio-npcm-sgpio.c new file +2 -0
+#define  NPCM_IXOEVCFG_FALLING	0x2
+#define  NPCM_IXOEVCFG_RISING	0x1

BIT()

Quoted 1 line

+#define NPCM_IXOEVCFG_BOTH 0x3

#define NPCM_IXOEVCFG_BOTH (NPCM_IXOEVCFG_FALLING | NPCM_IXOEVCFG_RISING)

...

Quoted 1 line

+#define NPCM_CLK_TH 8000000

Hmm... What is the units here? Hz? Can you use HZ_PER_MHZ multiplier?

Quoted 2 lines
drivers/gpio/gpio-npcm-sgpio.c new file +2 -0
+#define GPIO_BANK(x)    ((x) / 8)
+#define GPIO_BIT(x)     ((x) % 8)

...

drivers/gpio/gpio-npcm-sgpio.c line 50

Quoted 3 lines
drivers/gpio/gpio-npcm-sgpio.c new file +3 -0
+struct npcm_clk_cfg {
+	const unsigned int *SFT_CLK;
+	const unsigned int *CLK_SEL;

Why capital?

Review

Quoted 2 lines
(quoted diff) +2 -0
+	const unsigned int cfg_opt;
+};

How const for all of them makes any sense here? Just mark the entire struct with const whenever you are using it.

...

drivers/gpio/gpio-npcm-sgpio.c line 58

Quoted 5 lines
drivers/gpio/gpio-npcm-sgpio.c new file +5 -0
+struct npcm_sgpio {
+	struct gpio_chip chip;
+	struct clk *pclk;
+	struct irq_chip intc;
+	spinlock_t lock; /*protect event config*/

Missing spaces inside the comment.

Quoted 8 lines
drivers/gpio/gpio-npcm-sgpio.c new file +8 -0
+	void __iomem *base;
+	int irq;
+	u8 nin_sgpio;
+	u8 nout_sgpio;
+	u8 in_port;
+	u8 out_port;
+	u8 int_type[MAX_NR_HW_SGPIO];
+};

...

drivers/gpio/gpio-npcm-sgpio.c line 136

Quoted 3 lines
drivers/gpio/gpio-npcm-sgpio.c new file +3 -0
+static void __iomem *bank_reg(struct npcm_sgpio *gpio,
+			      const struct npcm_sgpio_bank *bank,
+				const enum npcm_sgpio_reg reg)

Something wrong with indentation.

drivers/gpio/gpio-npcm-sgpio.c line 149

Quoted 13 lines
drivers/gpio/gpio-npcm-sgpio.c new file +13 -0
+{
+	switch (reg) {
+	case READ_DATA:
+		return gpio->base + bank->rdata_reg;
+	case WRITE_DATA:
+		return gpio->base + bank->wdata_reg;
+	case EVENT_CFG:
+		return gpio->base + bank->event_config;
+	case EVENT_STS:
+		return gpio->base + bank->event_status;
+	default:
+		/* actually if code runs to here, it's an error case */
+		WARN(true, "Getting here is an error condition");

You have a device pointer, why not dev_WARN()?

Quoted 2 lines
(quoted diff) +2 -0
+	}
+}

...

drivers/gpio/gpio-npcm-sgpio.c line 163

Quoted 4 lines
drivers/gpio/gpio-npcm-sgpio.c new file +4 -0
+static void irqd_to_npcm_sgpio_data(struct irq_data *d,
+				    struct npcm_sgpio **gpio,
+				    const struct npcm_sgpio_bank **bank,
+				    u8 *bit, int *offset)

offset should be of the proper type.

drivers/gpio/gpio-npcm-sgpio.c line 169

Quoted 7 lines
drivers/gpio/gpio-npcm-sgpio.c new file +6 -0
+{
+	struct npcm_sgpio *internal;
+
+	*offset = irqd_to_hwirq(d);
+	internal = irq_data_get_irq_chip_data(d);
 
+	WARN_ON(!internal);

When does this make sense? I.o.w. when the internal can be NULL?

Quoted 6 lines
drivers/gpio/gpio-npcm-sgpio.c new file +6 -0
+
+	*gpio = internal;
+	*offset -= internal->nout_sgpio;
+	*bank = offset_to_bank(*offset);
+	*bit = GPIO_BIT(*offset);
+}

...

drivers/gpio/gpio-npcm-sgpio.c line 183

Quoted 7 lines
drivers/gpio/gpio-npcm-sgpio.c new file +7 -0
+static int npcm_sgpio_init_port(struct npcm_sgpio *gpio)
+{
+	u8 in_port, out_port, set_port, reg;
+
+	in_port = gpio->nin_sgpio / 8;
+	if (gpio->nin_sgpio % 8 > 0)
+		in_port += 1;

You created macros and you don't use them?

drivers/gpio/gpio-npcm-sgpio.c line 187

Quoted 3 lines
drivers/gpio/gpio-npcm-sgpio.c new file +3 -0
+	out_port = gpio->nout_sgpio / 8;
+	if (gpio->nout_sgpio % 8 > 0)
+		out_port += 1;

Ditto.

drivers/gpio/gpio-npcm-sgpio.c line 191

Quoted 4 lines
drivers/gpio/gpio-npcm-sgpio.c new file +3 -0
+	gpio->in_port = in_port;
+	gpio->out_port = out_port;
 
+	set_port = ((out_port & 0xf) << 4) | (in_port & 0xf);

GENMASK() ?

Quoted 7 lines
drivers/gpio/gpio-npcm-sgpio.c new file +7 -0
+	iowrite8(set_port, gpio->base + NPCM_IOXCFG2);
+
+	reg = ioread8(gpio->base + NPCM_IOXCFG2);
+
+	return reg == set_port ? 0 : -EINVAL;
+
+}

...

drivers/gpio/gpio-npcm-sgpio.c line 217

Quoted 10 lines
drivers/gpio/gpio-npcm-sgpio.c new file +9 -0
+static int npcm_sgpio_dir_out(struct gpio_chip *gc, unsigned int offset, int val)
+{
+	struct npcm_sgpio *gpio = gpiochip_get_data(gc);
+
+	if (offset < gpio->nout_sgpio) {
+		gc->set(gc, offset, val);
+		return 0;
+	}
 
+	return -EINVAL;

When is this true? Same question to all these checks over the code.

Quoted 1 line

+}

...

drivers/gpio/gpio-npcm-sgpio.c line 243

Quoted 2 lines
drivers/gpio/gpio-npcm-sgpio.c new file +2 -0
+	if (val)
+		reg |= (val << GPIO_BIT(offset));

And if val == 3? See below as well.

drivers/gpio/gpio-npcm-sgpio.c line 245

Quoted 2 lines
drivers/gpio/gpio-npcm-sgpio.c new file +2 -0
+	else
+		reg &= ~(1 << GPIO_BIT(offset));

Why not BIT() macro?

...

Review

Quoted 1 line
(quoted diff) +1 -0
+		reg = (reg >> GPIO_BIT(offset)) & 0x01;
		reg = !!(reg & GPIO_BIT(...));

...

Review

Quoted 1 line
(quoted diff) +1 -0
+		reg = (reg >> GPIO_BIT(offset)) & 0x01;

Ditto.

...

drivers/gpio/gpio-npcm-sgpio.c line 478

Quoted 1 line
drivers/gpio/gpio-npcm-sgpio.c new file +1 -0
+	.flags		= IRQCHIP_IMMUTABLE | IRQCHIP_MASK_ON_SUSPEND,

Indentation issue...

Check entire code for this.

...

It's a big driver and you have a lot of different kinds of problems. One of them is RT kernel support. You have an IRQ chip implementation here, can it be used in RT?
If so, consider different type of lock.

Also use cleanup.h.

...

drivers/gpio/gpio-npcm-sgpio.c line 550

Quoted 7 lines
drivers/gpio/gpio-npcm-sgpio.c new file +6 -0
+static const struct of_device_id npcm_sgpio_of_table[] = {
+	{ .compatible = "nuvoton,npcm750-sgpio", .data = &npcm750_sgpio_pdata, },
+	{ .compatible = "nuvoton,npcm845-sgpio", .data = &npcm845_sgpio_pdata, },
+	{}
+};
 
+

Redundant blank line.

Quoted 1 line

+MODULE_DEVICE_TABLE(of, npcm_sgpio_of_table);

...

drivers/gpio/gpio-npcm-sgpio.c line 575

Quoted 4 lines
drivers/gpio/gpio-npcm-sgpio.c new file +4 -0
+	rc = device_property_read_u32(&pdev->dev, "nuvoton,input-ngpios", &nin_gpios);
+	if (rc < 0) {
+		dev_err(&pdev->dev, "Could not read ngpios property\n");
+		return -EINVAL;
		return dev_err_probe();
Quoted 1 line
(quoted diff) +1 -0
+	}

...

drivers/gpio/gpio-npcm-sgpio.c line 581

Quoted 4 lines
drivers/gpio/gpio-npcm-sgpio.c new file +4 -0
+	rc = device_property_read_u32(&pdev->dev, "nuvoton,output-ngpios", &nout_gpios);
+	if (rc < 0) {
+		dev_err(&pdev->dev, "Could not read ngpios property\n");
+		return -EINVAL;

Ditto. And so on for the entire ->probe() stage.

Quoted 1 line
(quoted diff) +1 -0
+	}

...

drivers/gpio/gpio-npcm-sgpio.c line 595

Quoted 4 lines
drivers/gpio/gpio-npcm-sgpio.c new file +4 -0
+	gpio->pclk = devm_clk_get(&pdev->dev, NULL);
+	if (IS_ERR(gpio->pclk)) {
+		dev_err(&pdev->dev, "Could not get pclk\n");
+		return PTR_ERR(gpio->pclk);

Not using dev_err_probe() will spam the log.

Quoted 1 line
(quoted diff) +1 -0
+	}

...

drivers/gpio/gpio-npcm-sgpio.c line 614

Quoted 3 lines
drivers/gpio/gpio-npcm-sgpio.c new file +3 -0
+	gpio->chip.request = NULL;
+	gpio->chip.free = NULL;
+	gpio->chip.set_config = NULL;

Redundant assignments.

...

drivers/gpio/gpio-npcm-sgpio.c line 631

Quoted 1 line
drivers/gpio/gpio-npcm-sgpio.c new file +1 -0
+	dev_info(&pdev->dev, "NPCM: SGPIO module is ready\n");

Useless noisy message.

...

Quoted 1 line

+

Unneeded blank line.

Quoted 1 line

+module_platform_driver(npcm_sgpio_driver);

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH v7 3/3] gpio: nuvoton: Add Nuvoton NPCM sgpio driver

In reply to Jim Liu

Hi Jim,

thanks for your patch!

I saw that Andy already provided some good feedback but couldn't help but to notice this:

Quoted 3 lines · Jim Liu

Changes for v6:
- Remove bus-frequency property set
- Use GPIO_GENERIC

(...)

drivers/gpio/Kconfig config GPIO_MXS · line 484

Quoted 4 lines
drivers/gpio/Kconfig +4 -0
+config GPIO_NPCM_SGPIO
+       bool "Nuvoton SGPIO support"
+       depends on (ARCH_NPCM || COMPILE_TEST) && OF_GPIO
+       select GPIO_GENERIC

You select GPIO_GENERIC but you don't actually use it. If you were using it, your code would be calling bgpio_init() and it does not.

Yours,
Linus Walleij