Skip to contentSkip to conversation

Add gpio driver support for Realtek DHC SoCs

PATCH Superseded Tzuyi Chang 4 participants 11 messages Dec 22, 2023 – Dec 28, 2023

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover Add gpio driver support for Realtek DHC SoCs

These patches add the bindings and the gpio driver for Realtek DHC(Digital Home Center) RTD SoCs, including RTD1295, RTD1395, RTD1619, RTD1319, RTD1619B, RTD1319D and RTD1315E.

Change log:
v3->v4:
1. Arrange the compatible list in alphanumerical order.
2. Remove the size check for the offset array.
3. Add the debounce callback.
4. Conducted a review of the critical section, employing raw_spinlock_t for locking purposes.
5. Add gpiochip_enable_irq/gpiochip_disable_irq to fulfill the immutability requirements.
6. Use irqd_to_hwirq to get hwirq.
v2->v3:
1. Remove generic compatible and use SoC-specific compatible instead.
2. Add the missing descriptions for the rtd_gpio_info structure members.
3. Assign gpio_chip fwnode.
v1->v2:
1. Add description for DHC RTD SoCs in the bindings.
2. Revise the compatible names in the bindings.
3. Transitioned from OF API to platform_device API.
4. Use u8 for the offset array within the rtd_gpio_info structure.
5. Record the size of each array within the rtd_gpio_info structure and implement checks to prevent out-of-bounds access.
6. Use GPIOLIB_IRQCHIP helpers to register interrupts.
7. Use dynamic allocation for GPIO base.

Tzuyi Chang (2):
dt-bindings: gpio: realtek: Add realtek,rtd-gpio
Add GPIO support for Realtek DHC(Digital Home Center) RTD SoCs.

 .../bindings/gpio/realtek,rtd-gpio.yaml       |  69 ++
 drivers/gpio/Kconfig                          |   9 +
 drivers/gpio/Makefile                         |   1 +
 drivers/gpio/gpio-rtd.c                       | 616 ++++++++++++++++++
 4 files changed, 695 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/gpio/realtek,rtd-gpio.yaml
 create mode 100644 drivers/gpio/gpio-rtd.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH v4 1/2 dt-bindings: gpio: realtek: Add realtek,rtd-gpio

Add the device tree bindings for the Realtek DHC(Digital Home Center) RTD SoCs GPIO controllers.

---
v3 to v4 change:
1. Arrange the compatible list in alphanumerical order.
v2 to v3 change:
1. Remove generic compatible and use SoC-specific compatible instead.
v1 to v2 change:
1. Add description for DHC RTD SoCs.
2. Revise the compatible names.
3. Add descriptions for reg and interrupts properties.

---
 .../bindings/gpio/realtek,rtd-gpio.yaml       | 69 +++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/gpio/realtek,rtd-gpio.yaml
Documentation/devicetree/bindings/gpio/realtek,rtd-gpio.yaml new file +69 -0
new file mode 100644
index 000000000000..dd768db37a98
@@ -0,0 +1,69 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+# Copyright 2023 Realtek Semiconductor Corporation
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/gpio/realtek,rtd-gpio.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Realtek DHC GPIO controller
+
+maintainers:
+  - Tzuyi Chang <[email protected]>
+
+description:
+  The GPIO controller is designed for the Realtek DHC (Digital Home Center)
+  RTD series SoC family, which are high-definition media processor SoCs.
+
+properties:
+  compatible:
+    enum:
+      - realtek,rtd1295-misc-gpio
+      - realtek,rtd1295-iso-gpio
+      - realtek,rtd1315e-iso-gpio
+      - realtek,rtd1319-iso-gpio
+      - realtek,rtd1319d-iso-gpio
+      - realtek,rtd1395-iso-gpio
+      - realtek,rtd1619-iso-gpio
+      - realtek,rtd1619b-iso-gpio
+
+  reg:
+    items:
+      - description: GPIO controller registers
+      - description: GPIO interrupt registers
+
+  interrupts:
+    items:
+      - description: Interrupt number of the assert GPIO interrupt, which is
+                     triggered when there is a rising edge.
+      - description: Interrupt number of the deassert GPIO interrupt, which is
+                     triggered when there is a falling edge.
+
+  gpio-ranges: true
+
+  gpio-controller: true
+
+  "#gpio-cells":
+    const: 2
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - gpio-ranges
+  - gpio-controller
+  - "#gpio-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    gpio@100 {
+      compatible = "realtek,rtd1319d-iso-gpio";
+      reg = <0x100 0x100>,
+            <0x0 0xb0>;
+      interrupt-parent = <&iso_irq_mux>;
+      interrupts = <19>, <20>;
+      gpio-ranges = <&pinctrl 0 0 82>;
+      gpio-controller;
+      #gpio-cells = <2>;
+    };
Signature · 1 line
2.43.0
#3 lore raw

PATCH v4 2/2 Add GPIO support for Realtek DHC(Digital Home Center) RTD SoCs.

In reply to Tzuyi Chang

This driver enables configuration of GPIO direction, GPIO values, GPIO debounce settings and handles GPIO interrupts.

---
v3 to v4 change:
1. Remove the size check for the offset array.
2. Add the debounce callback to get values, register offsets and shifts for each chip.
3. Conducted a review of the critical section, employing raw_spinlock_t for locking purposes.
4. Use irqd_to_hwirq to get hwirq.
5. Add gpiochip_enable_irq/gpiochip_disable_irq to fulfill the immutability requirements.
v2 to v3 change:
1. Remove generic compatible and use SoC-specific compatible instead.
2. Add the missing descriptions for the rtd_gpio_info structure members.
3. Assign gpio_chip fwnode.
v1 to v2 change:
1. Remove legacy headers.
2. Transitioned from OF API to platform_device API.
3. Use u8 for the offset member within the rtd_gpio_info structure.
4. Record the size of each array within the rtd_gpio_info structure and implement checks to prevent out-of-bounds access.
5. Use GPIOLIB_IRQCHIP helpers to register interrupts.
6. Use dynamic allocation for GPIO base.

---
 drivers/gpio/Kconfig    |   9 +
 drivers/gpio/Makefile   |   1 +
 drivers/gpio/gpio-rtd.c | 616 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 626 insertions(+)
 create mode 100644 drivers/gpio/gpio-rtd.c
drivers/gpio/Kconfig +9 -0
index b3a133ed31ee..f0bdf9dbdefc 100644
@@ -553,6 +553,15 @@ config GPIO_ROCKCHIP
 	help
 	  Say yes here to support GPIO on Rockchip SoCs.
 
+config GPIO_RTD
+	tristate "Realtek DHC GPIO support"
+	depends on ARCH_REALTEK
+	default y
+	select GPIOLIB_IRQCHIP
+	help
+	  Say yes here to support GPIO on Realtek DHC(Digital Home Center)
+	  SoCs.
+
 config GPIO_SAMA5D2_PIOBU
 	tristate "SAMA5D2 PIOBU GPIO support"
 	depends on MFD_SYSCON
drivers/gpio/Makefile +1 -0
index eb73b5d633eb..16bb40717e87 100644
@@ -137,6 +137,7 @@ obj-$(CONFIG_GPIO_RDC321X)		+= gpio-rdc321x.o
 obj-$(CONFIG_GPIO_REALTEK_OTTO)		+= gpio-realtek-otto.o
 obj-$(CONFIG_GPIO_REG)			+= gpio-reg.o
 obj-$(CONFIG_GPIO_ROCKCHIP)	+= gpio-rockchip.o
+obj-$(CONFIG_GPIO_RTD)			+= gpio-rtd.o
 obj-$(CONFIG_ARCH_SA1100)		+= gpio-sa1100.o
 obj-$(CONFIG_GPIO_SAMA5D2_PIOBU)	+= gpio-sama5d2-piobu.o
 obj-$(CONFIG_GPIO_SCH311X)		+= gpio-sch311x.o
drivers/gpio/gpio-rtd.c new file +616 -0

616 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v4 2/2] Add GPIO support for Realtek DHC(Digital Home Center) RTD SoCs.

Quoted 2 lines · Tzuyi Chang

This driver enables configuration of GPIO direction, GPIO values, GPIO debounce settings and handles GPIO interrupts.

...

drivers/gpio/Kconfig config GPIO_ROCKCHIP · line 563

Quoted 3 lines
drivers/gpio/Kconfig +3 -0
+	help
+	  Say yes here to support GPIO on Realtek DHC(Digital Home Center)
+	  SoCs.

checkpatch.pl complains if it's less than 3 lines.

...

Please, follow IWYU principle.

drivers/gpio/gpio-rtd.c line 17

Quoted 11 lines
drivers/gpio/gpio-rtd.c new file +10 -0
+#include <linux/bitops.h>
+#include <linux/gpio/driver.h>
+#include <linux/interrupt.h>
+#include <linux/irqchip.h>
+#include <linux/irqchip/chained_irq.h>
+#include <linux/irqdomain.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
 
+#include <linux/spinlock.h>

+ types.h

...

Quoted 6 lines
drivers/gpio/gpio-rtd.c new file +6 -0
+struct rtd_gpio_info {
+	const char		*name;
+	unsigned int		gpio_base;
+	unsigned int		num_gpios;
+	u8			*dir_offset;
+	u8			*dato_offset;

drivers/gpio/gpio-rtd.c line 58

Quoted 10 lines
drivers/gpio/gpio-rtd.c new file +10 -0
+	u8			*dati_offset;
+	u8			*ie_offset;
+	u8			*dp_offset;
+	u8			*gpa_offset;
+	u8			*gpda_offset;
+	u8			*deb_offset;
+	u8			*deb_val;
+	u8			(*get_deb_setval)(const struct rtd_gpio_info *info,
+						   unsigned int offset, u8 *reg_offset,
+						   u8 *shift, u8 deb_index);

Basically you should group input parameters and output for better understanding.

u8 (*get_deb_setval)(const struct rtd_gpio_info *info,
unsigned int offset, u8 deb_index, u8 *reg_offset, u8 *shift);

Also indent the lines properly (besides the TABs).

Quoted 1 line

+};

Make it one TAB less in the middle.

...

drivers/gpio/gpio-rtd.c line 71

Quoted 2 lines
drivers/gpio/gpio-rtd.c new file +2 -0
+static u8 rtd_gpio_get_deb_setval(const struct rtd_gpio_info *info, unsigned int offset,
+				u8 *reg_offset, u8 *shift, u8 deb_val)

Why is it called val here and index in the other cases?
Can you come up with better naming that it can be consistent in all four places?

Quoted 21 lines
drivers/gpio/gpio-rtd.c new file +20 -0
+{
+	*reg_offset = info->deb_offset[offset / 8];
+	*shift = (offset % 8) * 4;
+	return info->deb_val[deb_val];
+}
+
+static u8 rtd1295_misc_gpio_get_deb_setval(const struct rtd_gpio_info *info, unsigned int offset,
+					 u8 *reg_offset, u8 *shift, u8 deb_index)
+{
+	*reg_offset = info->deb_offset[0];
+	*shift = (offset % 8) * 4;
+	return info->deb_val[deb_index];
+}
 
+static u8 rtd1295_iso_gpio_get_deb_setval(const struct rtd_gpio_info *info, unsigned int offset,
+					u8 *reg_offset, u8 *shift, u8 deb_index)
+{
+	*reg_offset = info->deb_offset[0];
+	*shift = 0;
+	return info->deb_val[deb_index];
+}

...

drivers/gpio/gpio-rtd.c line 209

Quoted 9 lines
drivers/gpio/gpio-rtd.c new file +9 -0
+static int rtd_gpio_gpa_offset(struct rtd_gpio *data, unsigned int offset)
+{
+	return data->info->gpa_offset[offset / 31];
+}
+
+static int rtd_gpio_gpda_offset(struct rtd_gpio *data, unsigned int offset)
+{
+	return data->info->gpda_offset[offset / 31];
+}

The / 31 so-o-o counter intuitive, please add a comment in each case to explain why [it's not 32 or other power-of-2].

...

drivers/gpio/gpio-rtd.c line 252

Quoted 3 lines
drivers/gpio/gpio-rtd.c new file +3 -0
+	raw_spin_lock_irqsave(&data->lock, flags);
+	writel_relaxed(val, data->base + reg_offset);
+	raw_spin_unlock_irqrestore(&data->lock, flags);

Convert to use cleanup.c, in particular here it becomes

	guard(raw_spinlock_irqsave)(&data->lock);

	writel_relaxed(val, data->base + reg_offset);

...

drivers/gpio/gpio-rtd.c line 312

Quoted 9 lines
drivers/gpio/gpio-rtd.c new file +8 -0
+	val = readl_relaxed(data->base + dir_reg_offset);
+	val &= BIT(offset % 32);
+	dat_reg_offset = val ?
+			 rtd_gpio_dato_offset(data, offset) : rtd_gpio_dati_offset(data, offset);
+
+	val = readl_relaxed(data->base + dat_reg_offset);
 
+	val >>= offset % 32;
+	val &= 0x1;

Replace 3 LoCs by 1:

	return !!(val & BIT(ofsset % 32));

Missed locking. How do you guarantee that you will get consistent results between the reads?

...

drivers/gpio/gpio-rtd.c line 327

Quoted 3 lines
drivers/gpio/gpio-rtd.c new file +3 -0
+	val &= BIT(offset % 32);
+
+	return val ? GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
	if (val & BIT(...))
		return _OUT;
	return _IN;

...

drivers/gpio/gpio-rtd.c line 396

Quoted 1 line
drivers/gpio/gpio-rtd.c new file +1 -0
+	for (i = 0; i < data->info->num_gpios; i += 31) {

Same, add explanation why 31.

Note, I actually prefer to see use of valid_mask instead of this weirdness. Then you will need to comment only once and use 32 (almost?) everywhere.

drivers/gpio/gpio-rtd.c line 403

Quoted 7 lines
drivers/gpio/gpio-rtd.c new file +7 -0
+		reg_offset = get_reg_offset(data, i);
+
+		status = readl_relaxed(data->irq_base + reg_offset) >> 1;
+		writel_relaxed(status << 1, data->irq_base + reg_offset);
+
+		for_each_set_bit(j, &status, 31) {
+			hwirq = i + j;

Nice, but you can do better

		/* Bit 0 is special... bla-bla-bla... */
		status = readl_relaxed(data->irq_base + reg_offset);
		status &= ~BIT(0);
		writel_relaxed(status, data->irq_base + reg_offset);

		for_each_set_bit(j, &status, 32) {
			hwirq = i + j - 1;

drivers/gpio/gpio-rtd.c line 409

Quoted 6 lines
drivers/gpio/gpio-rtd.c new file +6 -0
+			if (rtd_gpio_check_ie(data, hwirq)) {
+				int girq = irq_find_mapping(domain, hwirq);
+				u32 irq_type = irq_get_trigger_type(girq);
+
+				if ((irq == data->irqs[1]) && ((irq_type & IRQ_TYPE_SENSE_MASK) !=
+					IRQ_TYPE_EDGE_BOTH))

Do you need mask? Isn't irq_type already properly masked here?

Quoted 5 lines
drivers/gpio/gpio-rtd.c new file +5 -0
+					break;
+				generic_handle_domain_irq(domain, hwirq);
+			}
+		}
+	}

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+	u32 clr_mask = BIT(hwirq % 31) << 1;
+	u32 ie_mask = BIT(hwirq % 32);

This blows the mind. Needs a comment.

...

Quoted 43 lines
drivers/gpio/gpio-rtd.c new file +43 -0
+static int rtd_gpio_irq_set_type(struct irq_data *d, unsigned int type)
+{
+	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+	struct rtd_gpio *data = gpiochip_get_data(gc);
+	irq_hw_number_t hwirq = irqd_to_hwirq(d);
+	u32 mask = BIT(hwirq % 32);
+	unsigned long flags;
+	int dp_reg_offset;
+	bool polarity;
+	u32 val;
+
+	dp_reg_offset = rtd_gpio_dp_offset(data, hwirq);
+
+	switch (type & IRQ_TYPE_SENSE_MASK) {
+	case IRQ_TYPE_EDGE_RISING:
+		polarity = 1;
+		break;
+
+	case IRQ_TYPE_EDGE_FALLING:
+		polarity = 0;
+		break;
+
+	case IRQ_TYPE_EDGE_BOTH:
+		polarity = 1;
+		break;
+
+	default:
+		return -EINVAL;
+	}
+
+	raw_spin_lock_irqsave(&data->lock, flags);
+
+	val = readl_relaxed(data->base + dp_reg_offset);
+	if (polarity)
+		val |= mask;
+	else
+		val &= ~mask;
+	writel_relaxed(val, data->base + dp_reg_offset);
+
+	raw_spin_unlock_irqrestore(&data->lock, flags);
+
+	return 0;
+}

...

drivers/gpio/gpio-rtd.c line 569

Quoted 1 line
drivers/gpio/gpio-rtd.c new file +1 -0
+	irq_chip->handler = handle_simple_irq;

Please, apply bad handler here and lock it in the set_type callback above.

You may read eb441337c714 ("gpio: pca953x: Set IRQ type when handle Intel Galileo Gen 2") to understand the difference.

...

drivers/gpio/gpio-rtd.c line 606

Quoted 6 lines
drivers/gpio/gpio-rtd.c new file +5 -0
+static int rtd_gpio_init(void)
+{
+	return platform_driver_register(&rtd_gpio_platform_driver);
+}
 
+

Redundant blank line, but see below.

drivers/gpio/gpio-rtd.c line 613

Quoted 7 lines
drivers/gpio/gpio-rtd.c new file +7 -0
+module_init(rtd_gpio_init);
+
+static void __exit rtd_gpio_exit(void)
+{
+	platform_driver_unregister(&rtd_gpio_platform_driver);
+}
+module_exit(rtd_gpio_exit);

There is no special initcall, you may use module_platform_driver() macro instead.

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

RE: [PATCH v4 2/2] Add GPIO support for Realtek DHC(Digital Home Center) RTD SoCs.

Hi Andy,

Thank you for the review.

Quoted 12 lines · Tzuyi Chang
Quoted 2 lines

This driver enables configuration of GPIO direction, GPIO values, GPIO debounce settings and handles GPIO interrupts.

...

drivers/gpio/Kconfig config GPIO_ROCKCHIP · line 563

Quoted 3 lines
drivers/gpio/Kconfig +3 -0
+     help
+       Say yes here to support GPIO on Realtek DHC(Digital Home Center)
+       SoCs.

checkpatch.pl complains if it's less than 3 lines.

I will add more description.

Quoted 12 lines

...

Please, follow IWYU principle.

Quoted 7 lines
drivers/gpio/gpio-rtd.c new file +7 -0
+#include <linux/bitops.h>
+#include <linux/gpio/driver.h>
+#include <linux/interrupt.h>
+#include <linux/irqchip.h>
+#include <linux/irqchip/chained_irq.h> #include <linux/irqdomain.h>
+#include <linux/module.h> #include <linux/platform_device.h> #include
+<linux/property.h>
Quoted 4 lines
Quoted 1 line

+#include <linux/spinlock.h>

+ types.h

I will revise it.

Quoted 16 lines

...

Quoted 4 lines
drivers/gpio/gpio-rtd.c new file +4 -0
+struct rtd_gpio_info {
+     const char              *name;
+     unsigned int            gpio_base;
+     unsigned int            num_gpios;

drivers/gpio/gpio-rtd.c

Quoted 10 lines
drivers/gpio/gpio-rtd.c new file +10 -0
+     u8                      *dir_offset;
+     u8                      *dato_offset;
+     u8                      *dati_offset;
+     u8                      *ie_offset;
+     u8                      *dp_offset;
+     u8                      *gpa_offset;
+     u8                      *gpda_offset;
+     u8                      *deb_offset;
+     u8                      *deb_val;
+     u8                      (*get_deb_setval)(const struct
Quoted 9 lines

rtd_gpio_info *info,

Quoted 1 line

+ unsigned int

offset, u8 *reg_offset,

Review

Quoted 2 lines
(quoted diff) +2 -0
+                                                u8 *shift, u8
+deb_index);

Basically you should group input parameters and output for better understanding.

I will define a structure to hold the necessary information for the output.

Quoted 4 lines

u8 (*get_deb_setval)(const struct rtd_gpio_info
*info,
unsigned int offset, u8
deb_index,

Quoted 8 lines
                                         u8 *reg_offset, u8 *shift);

Also indent the lines properly (besides the TABs).

Quoted 1 line

+};

Make it one TAB less in the middle.

I will revise it.

Quoted 9 lines

...

Quoted 1 line

+static u8 rtd_gpio_get_deb_setval(const struct rtd_gpio_info *info, unsigned

int offset,

Quoted 1 line

+ u8 *reg_offset, u8 *shift, u8 deb_val)

Why is it called val here and index in the other cases?
Can you come up with better naming that it can be consistent in all four places?

I missed to rename it to 'deb_index'. Sorry about that.

Quoted 24 lines

drivers/gpio/gpio-rtd.c

Quoted 7 lines
drivers/gpio/gpio-rtd.c new file +7 -0
+{
+     *reg_offset = info->deb_offset[offset / 8];
+     *shift = (offset % 8) * 4;
+     return info->deb_val[deb_val];
+}
+
+static u8 rtd1295_misc_gpio_get_deb_setval(const struct rtd_gpio_info *info,

unsigned int offset,

Review

Quoted 7 lines
(quoted diff) +6 -0
+                                      u8 *reg_offset, u8 *shift, u8
+deb_index) {
+     *reg_offset = info->deb_offset[0];
+     *shift = (offset % 8) * 4;
+     return info->deb_val[deb_index]; }
 
+static u8 rtd1295_iso_gpio_get_deb_setval(const struct rtd_gpio_info *info,

unsigned int offset,

Quoted 5 lines
(quoted diff) +5 -0
+                                     u8 *reg_offset, u8 *shift, u8
+deb_index) {
+     *reg_offset = info->deb_offset[0];
+     *shift = 0;
+     return info->deb_val[deb_index]; }

...

Quoted 11 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+static int rtd_gpio_gpa_offset(struct rtd_gpio *data, unsigned int
+offset) {
+     return data->info->gpa_offset[offset / 31]; }
+
+static int rtd_gpio_gpda_offset(struct rtd_gpio *data, unsigned int
+offset) {
+     return data->info->gpda_offset[offset / 31]; }

The / 31 so-o-o counter intuitive, please add a comment in each case to explain why [it's not 32 or other power-of-2].

In our hardware design, the bit 0 of the gpda and gpa status registers does not correspond to a GPIO. If bit 0 is set to 1, the other bit can be set to 1 by writing 1.
If bit 0 is set to 0, the other bit can be clear to 0 by writing 1.

Therefore, each status register only contains the status of 31 GPIOs. I will add the comment for this.

Quoted 6 lines

...

drivers/gpio/gpio-rtd.c line 252

Quoted 3 lines
drivers/gpio/gpio-rtd.c new file +3 -0
+     raw_spin_lock_irqsave(&data->lock, flags);
+     writel_relaxed(val, data->base + reg_offset);
+     raw_spin_unlock_irqrestore(&data->lock, flags);
Quoted 6 lines

Convert to use cleanup.c, in particular here it becomes

       guard(raw_spinlock_irqsave)(&data->lock);

       writel_relaxed(val, data->base + reg_offset);

I will revise it.

Quoted 13 lines

...

drivers/gpio/gpio-rtd.c line 312

Quoted 10 lines
drivers/gpio/gpio-rtd.c new file +9 -0
+     val = readl_relaxed(data->base + dir_reg_offset);
+     val &= BIT(offset % 32);
+     dat_reg_offset = val ?
+                      rtd_gpio_dato_offset(data, offset) :
+ rtd_gpio_dati_offset(data, offset);
+
+     val = readl_relaxed(data->base + dat_reg_offset);
 
+     val >>= offset % 32;
+     val &= 0x1;
Quoted 8 lines

Replace 3 LoCs by 1:

       return !!(val & BIT(ofsset % 32));

Missed locking. How do you guarantee that you will get consistent results between the reads?

I will revise it.

Quoted 11 lines

...

drivers/gpio/gpio-rtd.c

Quoted 3 lines
drivers/gpio/gpio-rtd.c new file +3 -0
+     val &= BIT(offset % 32);
+
+     return val ? GPIO_LINE_DIRECTION_OUT :

GPIO_LINE_DIRECTION_IN;

       if (val & BIT(...))
               return _OUT;
       return _IN;
Quoted 9 lines

...

Quoted 1 line

+ for (i = 0; i < data->info->num_gpios; i += 31) {

Same, add explanation why 31.

Note, I actually prefer to see use of valid_mask instead of this weirdness. Then you will need to comment only once and use 32 (almost?) everywhere.

The reason remains consistent with the previous explanation. Each status register exclusively holds the status of 31 GPIOs.

Quoted 11 lines

drivers/gpio/gpio-rtd.c line 403

Quoted 8 lines
drivers/gpio/gpio-rtd.c new file +8 -0
+             reg_offset = get_reg_offset(data, i);
+
+             status = readl_relaxed(data->irq_base + reg_offset) >> 1;
+             writel_relaxed(status << 1, data->irq_base +
+ reg_offset);
+
+             for_each_set_bit(j, &status, 31) {
+                     hwirq = i + j;

Nice, but you can do better

Quoted 8 lines
               /* Bit 0 is special... bla-bla-bla... */
               status = readl_relaxed(data->irq_base + reg_offset);
               status &= ~BIT(0);
               writel_relaxed(status, data->irq_base + reg_offset);

               for_each_set_bit(j, &status, 32) {
                       hwirq = i + j - 1;

Given that each status register accommodates the status of only 31 GPIOs, I think utilizing the upper format and including explanatory comments would be appropriate. It can indicate the status registers only contains 31 GPIOs. Please correct me if my understanding is incorrect.

Quoted 7 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+                     if (rtd_gpio_check_ie(data, hwirq)) {
+                             int girq = irq_find_mapping(domain,

hwirq);

Review

Quoted 4 lines
(quoted diff) +4 -0
+                             u32 irq_type =
+ irq_get_trigger_type(girq);
+
+                             if ((irq == data->irqs[1]) && ((irq_type &
Quoted 12 lines

IRQ_TYPE_SENSE_MASK) !=

Quoted 1 line

+ IRQ_TYPE_EDGE_BOTH))

Do you need mask? Isn't irq_type already properly masked here?

Review

Quoted 2 lines
(quoted diff) +2 -0
+                                     break;
+                             generic_handle_domain_irq(domain,

hwirq);

Quoted 3 lines
drivers/gpio/gpio-rtd.c new file +3 -0
+                     }
+             }
+     }

It has already been masked in the irq_get_trigger_type. I will revise it.

Quoted 7 lines

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+     u32 clr_mask = BIT(hwirq % 31) << 1;
+     u32 ie_mask = BIT(hwirq % 32);

This blows the mind. Needs a comment.

The clr_mask is used to clear the gpa/gpda registers, each of which accommodates only 31 GPIOs.

Quoted 48 lines

...

Quoted 43 lines
drivers/gpio/gpio-rtd.c new file +43 -0
+static int rtd_gpio_irq_set_type(struct irq_data *d, unsigned int
+type) {
+     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+     struct rtd_gpio *data = gpiochip_get_data(gc);
+     irq_hw_number_t hwirq = irqd_to_hwirq(d);
+     u32 mask = BIT(hwirq % 32);
+     unsigned long flags;
+     int dp_reg_offset;
+     bool polarity;
+     u32 val;
+
+     dp_reg_offset = rtd_gpio_dp_offset(data, hwirq);
+
+     switch (type & IRQ_TYPE_SENSE_MASK) {
+     case IRQ_TYPE_EDGE_RISING:
+             polarity = 1;
+             break;
+
+     case IRQ_TYPE_EDGE_FALLING:
+             polarity = 0;
+             break;
+
+     case IRQ_TYPE_EDGE_BOTH:
+             polarity = 1;
+             break;
+
+     default:
+             return -EINVAL;
+     }
+
+     raw_spin_lock_irqsave(&data->lock, flags);
+
+     val = readl_relaxed(data->base + dp_reg_offset);
+     if (polarity)
+             val |= mask;
+     else
+             val &= ~mask;
+     writel_relaxed(val, data->base + dp_reg_offset);
+
+     raw_spin_unlock_irqrestore(&data->lock, flags);
+
+     return 0;
+}

...

Quoted 7 lines
Quoted 1 line

+ irq_chip->handler = handle_simple_irq;

Please, apply bad handler here and lock it in the set_type callback above.

You may read eb441337c714 ("gpio: pca953x: Set IRQ type when handle Intel Galileo Gen 2") to understand the difference.

I will revise it.

Quoted 11 lines

...

drivers/gpio/gpio-rtd.c line 606

Quoted 6 lines
drivers/gpio/gpio-rtd.c new file +5 -0
+static int rtd_gpio_init(void)
+{
+     return platform_driver_register(&rtd_gpio_platform_driver);
+}
 
+

Redundant blank line, but see below.

Quoted 10 lines

drivers/gpio/gpio-rtd.c line 613

Quoted 6 lines
drivers/gpio/gpio-rtd.c new file +6 -0
+module_init(rtd_gpio_init);
+
+static void __exit rtd_gpio_exit(void) {
+     platform_driver_unregister(&rtd_gpio_platform_driver);
+}
+module_exit(rtd_gpio_exit);

There is no special initcall, you may use module_platform_driver() macro instead.

I will revise it.

Thanks,
Tzuyi Chang

#6 lore raw

Re: [PATCH v4 2/2] Add GPIO support for Realtek DHC(Digital Home Center) RTD SoCs.

Quoted 1 line · TY_Chang[張子逸]
Quoted 1 line · Tzuyi Chang

...

Quoted 12 lines
Quoted 11 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+static int rtd_gpio_gpa_offset(struct rtd_gpio *data, unsigned int
+offset) {
+     return data->info->gpa_offset[offset / 31]; }
+
+static int rtd_gpio_gpda_offset(struct rtd_gpio *data, unsigned int
+offset) {
+     return data->info->gpda_offset[offset / 31]; }

The / 31 so-o-o counter intuitive, please add a comment in each case to explain why [it's not 32 or other power-of-2].

Quoted 5 lines

In our hardware design, the bit 0 of the gpda and gpa status registers does not correspond to a GPIO. If bit 0 is set to 1, the other bit can be set to 1 by writing 1.
If bit 0 is set to 0, the other bit can be clear to 0 by writing 1.

Therefore, each status register only contains the status of 31 GPIOs. I will add the comment for this.

Yes, please add in all places, while it's a dup, it helps understanding the point without looking around for a while.

...

Quoted 10 lines
Quoted 7 lines
Quoted 1 line

+ for (i = 0; i < data->info->num_gpios; i += 31) {

Same, add explanation why 31.

Note, I actually prefer to see use of valid_mask instead of this weirdness. Then you will need to comment only once and use 32 (almost?) everywhere.

The reason remains consistent with the previous explanation. Each status register exclusively holds the status of 31 GPIOs.

As per above, add a comment.

Quoted 16 lines
Quoted 16 lines

drivers/gpio/gpio-rtd.c line 403

Quoted 8 lines
drivers/gpio/gpio-rtd.c new file +8 -0
+             reg_offset = get_reg_offset(data, i);
+
+             status = readl_relaxed(data->irq_base + reg_offset) >> 1;
+             writel_relaxed(status << 1, data->irq_base +
+ reg_offset);
+
+             for_each_set_bit(j, &status, 31) {
+                     hwirq = i + j;

Nice, but you can do better

               /* Bit 0 is special... bla-bla-bla... */
               status = readl_relaxed(data->irq_base + reg_offset);
               status &= ~BIT(0);
               writel_relaxed(status, data->irq_base + reg_offset);
Quoted 8 lines
Quoted 3 lines
               for_each_set_bit(j, &status, 32) {
                       hwirq = i + j - 1;

Given that each status register accommodates the status of only 31 GPIOs, I think utilizing the upper format and including explanatory comments would be appropriate. It can indicate the status registers only contains 31 GPIOs. Please correct me if my understanding is incorrect.

The above is just a code hack to help bitops to optimise. 32 is power-of-2 which might be treated better by the compiler and hence produce better code.

Yet, it's an interrupt handler where we want to have the ops as shorter as possible, so even micro-optimizations are good to have here (I don't insist to follow the same idea elsewhere).

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/gpio/gpio-rtd.c new file +3 -0
+                     }
+             }
+     }
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

RE: [PATCH v4 2/2] Add GPIO support for Realtek DHC(Digital Home Center) RTD SoCs.

Hi Andy,

Quoted 68 lines · TY_Chang[張子逸]
Quoted 1 line
Quoted 1 line · Tzuyi Chang

...

Quoted 13 lines
Quoted 12 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+static int rtd_gpio_gpa_offset(struct rtd_gpio *data, unsigned int
+offset) {
+     return data->info->gpa_offset[offset / 31]; }
+
+static int rtd_gpio_gpda_offset(struct rtd_gpio *data, unsigned
+int
+offset) {
+     return data->info->gpda_offset[offset / 31]; }

The / 31 so-o-o counter intuitive, please add a comment in each case to explain why [it's not 32 or other power-of-2].

Quoted 1 line

In our hardware design, the bit 0 of the gpda and gpa status registers does not

correspond to a GPIO.

Quoted 4 lines

If bit 0 is set to 1, the other bit can be set to 1 by writing 1. If bit 0 is set to 0, the other bit can be clear to 0 by writing 1.

Therefore, each status register only contains the status of 31 GPIOs. I will add

the comment for this.

Yes, please add in all places, while it's a dup, it helps understanding the point without looking around for a while.

...

Quoted 10 lines
Quoted 7 lines
Quoted 1 line

+ for (i = 0; i < data->info->num_gpios; i += 31) {

Same, add explanation why 31.

Note, I actually prefer to see use of valid_mask instead of this weirdness. Then you will need to comment only once and use 32 (almost?) everywhere.

The reason remains consistent with the previous explanation. Each status register exclusively holds the status of 31 GPIOs.

As per above, add a comment.

Quoted 16 lines
Quoted 16 lines

drivers/gpio/gpio-rtd.c line 403

Quoted 8 lines
drivers/gpio/gpio-rtd.c new file +8 -0
+             reg_offset = get_reg_offset(data, i);
+
+             status = readl_relaxed(data->irq_base + reg_offset) >> 1;
+             writel_relaxed(status << 1, data->irq_base +
+ reg_offset);
+
+             for_each_set_bit(j, &status, 31) {
+                     hwirq = i + j;

Nice, but you can do better

               /* Bit 0 is special... bla-bla-bla... */
               status = readl_relaxed(data->irq_base + reg_offset);
               status &= ~BIT(0);
               writel_relaxed(status, data->irq_base + reg_offset);
Quoted 7 lines
Quoted 3 lines
               for_each_set_bit(j, &status, 32) {
                       hwirq = i + j - 1;

Given that each status register accommodates the status of only 31 GPIOs, I think utilizing the upper format and including explanatory comments would be appropriate. It can indicate the status registers only

contains 31 GPIOs.

Quoted 9 lines
Quoted 1 line

Please correct me if my understanding is incorrect.

The above is just a code hack to help bitops to optimise. 32 is power-of-2 which might be treated better by the compiler and hence produce better code.

Yet, it's an interrupt handler where we want to have the ops as shorter as possible, so even micro-optimizations are good to have here (I don't insist to follow the same idea elsewhere).

I understand. Thank you for explaining. I will revise it.

Quoted 4 lines
Quoted 3 lines
Quoted 3 lines

drivers/gpio/gpio-rtd.c line 92

Quoted 3 lines
drivers/gpio/gpio-rtd.c new file +3 -0
+                     }
+             }
+     }

Thanks,
Tzuyi Chang

#8 lore raw

Re: [PATCH v4 2/2] Add GPIO support for Realtek DHC(Digital Home Center) RTD SoCs.

In reply to Tzuyi Chang
Quoted 6 lines · Tzuyi Chang

This driver enables configuration of GPIO direction, GPIO values, GPIO debounce settings and handles GPIO interrupts.

---

...

Quoted 21 lines
drivers/gpio/gpio-rtd.c new file +21 -0
+static int rtd_gpio_irq_set_type(struct irq_data *d, unsigned int type)
+{
+	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+	struct rtd_gpio *data = gpiochip_get_data(gc);
+	irq_hw_number_t hwirq = irqd_to_hwirq(d);
+	u32 mask = BIT(hwirq % 32);
+	unsigned long flags;
+	int dp_reg_offset;
+	bool polarity;
+	u32 val;
+
+	dp_reg_offset = rtd_gpio_dp_offset(data, hwirq);
+
+	switch (type & IRQ_TYPE_SENSE_MASK) {
+	case IRQ_TYPE_EDGE_RISING:
+		polarity = 1;
+		break;
+
+	case IRQ_TYPE_EDGE_FALLING:
+		polarity = 0;
+		break;

drivers/gpio/gpio-rtd.c line 501

Quoted 10 lines
drivers/gpio/gpio-rtd.c new file +10 -0
+
+	case IRQ_TYPE_EDGE_BOTH:
+		polarity = 1;
+		break;
+
+	default:
+		return -EINVAL;
+	}
+
+	raw_spin_lock_irqsave(&data->lock, flags);

Why are you using raw spinlock? This question applies to entire driver.

drivers/gpio/gpio-rtd.c line 513

Quoted 12 lines
drivers/gpio/gpio-rtd.c new file +12 -0
+
+	val = readl_relaxed(data->base + dp_reg_offset);
+	if (polarity)
+		val |= mask;
+	else
+		val &= ~mask;
+	writel_relaxed(val, data->base + dp_reg_offset);
+
+	raw_spin_unlock_irqrestore(&data->lock, flags);
+
+	return 0;
+}

...

}

drivers/gpio/gpio-rtd.c line 613

Quoted 8 lines
drivers/gpio/gpio-rtd.c new file +8 -0
+
+module_init(rtd_gpio_init);
+
+static void __exit rtd_gpio_exit(void)
+{
+	platform_driver_unregister(&rtd_gpio_platform_driver);
+}
+module_exit(rtd_gpio_exit);

Why not using module_platform_driver?

Best regards,
Krzysztof

#9 lore raw

Re: [PATCH v4 2/2] Add GPIO support for Realtek DHC(Digital Home Center) RTD SoCs.

Quoted 1 line · Krzysztof Kozlowski

On 22/12/2023 08:58, Tzuyi Chang wrote:

...

Quoted 3 lines
Quoted 1 line

+ raw_spin_lock_irqsave(&data->lock, flags);

Why are you using raw spinlock? This question applies to entire driver.

If you want to have your IRQ chip to work in the RT kernel, you need a real spinlock.

Quoted 1 line
Quoted 1 line

+ raw_spin_unlock_irqrestore(&data->lock, flags);

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

RE: [PATCH v4 2/2] Add GPIO support for Realtek DHC(Digital Home Center) RTD SoCs.

In reply to Krzysztof Kozlowski

Hi Krzysztof,

Thank you for the review.

Quoted 27 lines

...

Quoted 25 lines
drivers/gpio/gpio-rtd.c new file +25 -0
+static int rtd_gpio_irq_set_type(struct irq_data *d, unsigned int
+type) {
+     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+     struct rtd_gpio *data = gpiochip_get_data(gc);
+     irq_hw_number_t hwirq = irqd_to_hwirq(d);
+     u32 mask = BIT(hwirq % 32);
+     unsigned long flags;
+     int dp_reg_offset;
+     bool polarity;
+     u32 val;
+
+     dp_reg_offset = rtd_gpio_dp_offset(data, hwirq);
+
+     switch (type & IRQ_TYPE_SENSE_MASK) {
+     case IRQ_TYPE_EDGE_RISING:
+             polarity = 1;
+             break;
+
+     case IRQ_TYPE_EDGE_FALLING:
+             polarity = 0;
+             break;
+
+     case IRQ_TYPE_EDGE_BOTH:
+             polarity = 1;
+             break;
Quoted 9 lines

drivers/gpio/gpio-rtd.c line 501

Quoted 6 lines
drivers/gpio/gpio-rtd.c new file +6 -0
+
+     default:
+             return -EINVAL;
+     }
+
+     raw_spin_lock_irqsave(&data->lock, flags);

Why are you using raw spinlock? This question applies to entire driver.

I think consumer driver may operate GPIOs within the ISR, so it needs a lock.

Quoted 16 lines

drivers/gpio/gpio-rtd.c line 513

Quoted 12 lines
drivers/gpio/gpio-rtd.c new file +12 -0
+
+     val = readl_relaxed(data->base + dp_reg_offset);
+     if (polarity)
+             val |= mask;
+     else
+             val &= ~mask;
+     writel_relaxed(val, data->base + dp_reg_offset);
+
+     raw_spin_unlock_irqrestore(&data->lock, flags);
+
+     return 0;
+}

...

}

Quoted 10 lines

drivers/gpio/gpio-rtd.c line 613

Quoted 7 lines
drivers/gpio/gpio-rtd.c new file +7 -0
+
+module_init(rtd_gpio_init);
+
+static void __exit rtd_gpio_exit(void) {
+     platform_driver_unregister(&rtd_gpio_platform_driver);
+}
+module_exit(rtd_gpio_exit);

Why not using module_platform_driver?

I will revise it.

Thanks,
Tzuyi Chang