Skip to contentSkip to conversation

gpio: rockchip: Update the GPIO driver

PATCH Superseded Ye Zhang 3 participants 23 messages Sep 3, 2024

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Ye Zhang

author
#0 lore raw

v3 cover gpio: rockchip: Update the GPIO driver

GPIO driver support acpi and new version, set input direction in irq_request_resources, fix division error and debounce config error.

Changes since v1:
- Split commits with multiple changes into separate commits.
- Adjust backportable fix to the forefront.
- Modify messages of some commits.

Changes since v2:
- Optimize version number comments.
- Modify the GPIO version judgment logic.
- Use devm_clk_get_enabled to simplify the code.
- Use guard instead of mutex_lock to simplify the code.
- Use irq_hw_number_t and irqd_to_hwirq() in the request irq function.
- Since list_first_entry cannot return NULL, remove the NULL check.
- Temporarily do not add support for ACPI.

Ye Zhang (12):
gpio: rockchip: avoid division by zero
gpio: rockchip: release reference to device node
gpio: rockchip: resolve overflow issues
gpio: rockchip: resolve underflow issue
gpio: rockchip: fix debounce calculate
gpio: rockchip: Update debounce config function
gpio: rockchip: support 'clock-names' from dt nodes
gpio: rockchip: explan the format of the GPIO version ID
gpio: rockchip: change the GPIO version judgment logic
gpio: rockchip: support new version gpio
gpio: rockchip: Set input direction when request irq
gpio: rockchip: replace mutex_lock() with guard()

 drivers/gpio/gpio-rockchip.c       | 151 ++++++++++++++++++-----------
 drivers/pinctrl/pinctrl-rockchip.h |   2 +
 2 files changed, 99 insertions(+), 54 deletions(-)
Signature · 7 lines
2.34.1


_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
author
#1 lore raw

PATCH v3 1/12 gpio: rockchip: avoid division by zero

If the clk_get_rate return '0', it will happen division by zero.

Fixes 3bcbd1a85b68 ("gpio/rockchip: support next version gpio controller")
---
 drivers/gpio/gpio-rockchip.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpio/gpio-rockchip.c +2 -0
index 0bd339813110..712258224eb3 100644
@@ -207,6 +207,8 @@ static int rockchip_gpio_set_debounce(struct gpio_chip *gc,
 	if (bank->gpio_type == GPIO_TYPE_V2 && !IS_ERR(bank->db_clk)) {
 		div_debounce_support = true;
 		freq = clk_get_rate(bank->db_clk);
+		if (!freq)
+			return -EINVAL;
 		max_debounce = (GENMASK(23, 0) + 1) * 2 * 1000000 / freq;
 		if (debounce > max_debounce)
 			return -EINVAL;
Signature · 7 lines
2.34.1


_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
#2 lore raw

Re: [PATCH v3 01/12] gpio: rockchip: avoid division by zero

Quoted 1 line · Ye Zhang

If the clk_get_rate return '0', it will happen division by zero.

I don't understand the circumstances when it may happen.

Quoted 1 line
Fixes 3bcbd1a85b68 ("gpio/rockchip: support next version gpio controller")

Not sure that this actually fixes anything. See below why I think so.

...

Quoted 1 line

if (bank->gpio_type == GPIO_TYPE_V2 && !IS_ERR(bank->db_clk)) {

Here you explicitly checked that the clock is provided by DT.

...

Quoted 1 line

freq = clk_get_rate(bank->db_clk);

Here you read the frequency which may be 0 in two cases:
1) in DT explicitly set to 0;
2) CCF is disabled.

So, wrong DTs have to be validated / fixed beforehand, correct?

But if the CCF is disabled, the db_clk is NULL. Moreover I don't see how the db_clk may contain error pointer as you have it filtered out at _get_bank_data(). So, maybe what you need is to have NULL check in the conditional and explaining more in the commit message why it is currently a problematic code?

Quoted 2 lines
drivers/gpio/gpio-rockchip.c +2 -0
+		if (!freq)
+			return -EINVAL;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#3 lore raw

PATCH v3 2/12 gpio: rockchip: release reference to device node

In reply to Ye Zhang

Added a call to of_node_put(pctlnp) in rockchip_gpio_probe to properly release the reference to the device node, improving memory management and preventing potential leaks.

Fixes 936ee2675eee ("gpio/rockchip: add driver for rockchip gpio")
---
 drivers/gpio/gpio-rockchip.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpio/gpio-rockchip.c +1 -0
index 712258224eb3..5f60162baaeb 100644
@@ -715,6 +715,7 @@ static int rockchip_gpio_probe(struct platform_device *pdev)
 		return -ENODEV;
 
 	pctldev = of_pinctrl_get(pctlnp);
+	of_node_put(pctlnp);
 	if (!pctldev)
 		return -EPROBE_DEFER;
 
Signature · 7 lines
2.34.1


_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
author
#4 lore raw

PATCH v3 3/12 gpio: rockchip: resolve overflow issues

In reply to Ye Zhang

Prevent overflow issues when performing debounce-related calculations.

Fixes 3bcbd1a85b68 ("gpio/rockchip: support next version gpio controller")
---
 drivers/gpio/gpio-rockchip.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpio/gpio-rockchip.c +4 -2
index 5f60162baaeb..6dcb8bb0d1b4 100644
@@ -22,6 +22,7 @@
 #include <linux/pinctrl/pinconf-generic.h>
 #include <linux/platform_device.h>
 #include <linux/regmap.h>
+#include <linux/units.h>
 
 #include "../pinctrl/core.h"
 #include "../pinctrl/pinctrl-rockchip.h"
@@ -209,11 +210,12 @@ static int rockchip_gpio_set_debounce(struct gpio_chip *gc,
 		freq = clk_get_rate(bank->db_clk);
 		if (!freq)
 			return -EINVAL;
-		max_debounce = (GENMASK(23, 0) + 1) * 2 * 1000000 / freq;
+		div = (u64)(GENMASK(23, 0) + 1) * 2 * HZ_PER_MHZ;
+		max_debounce = DIV_ROUND_CLOSEST_ULL(div, freq);
 		if (debounce > max_debounce)
 			return -EINVAL;
 
-		div = debounce * freq;
+		div = (u64)debounce * freq;
 		div_reg = DIV_ROUND_CLOSEST_ULL(div, 2 * USEC_PER_SEC) - 1;
 	} else {
 		div_debounce_support = false;
Signature · 7 lines
2.34.1


_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
author
#5 lore raw

PATCH v3 4/12 gpio: rockchip: resolve underflow issue

In reply to Ye Zhang

div_reg may be < 0 if debounce is zero, causing the unsigned int to overflow.

Fixes 3bcbd1a85b68 ("gpio/rockchip: support next version gpio controller")
---
 drivers/gpio/gpio-rockchip.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/gpio/gpio-rockchip.c +4 -3
index 6dcb8bb0d1b4..26191197cd37 100644
@@ -205,8 +205,11 @@ static int rockchip_gpio_set_debounce(struct gpio_chip *gc,
 	unsigned int cur_div_reg;
 	u64 div;
 
-	if (bank->gpio_type == GPIO_TYPE_V2 && !IS_ERR(bank->db_clk)) {
+	if ((bank->gpio_type == GPIO_TYPE_V2) && !IS_ERR(bank->db_clk))
 		div_debounce_support = true;
+	else
+		div_debounce_support = false;
+	if (debounce && div_debounce_support) {
 		freq = clk_get_rate(bank->db_clk);
 		if (!freq)
 			return -EINVAL;
@@ -217,8 +220,6 @@ static int rockchip_gpio_set_debounce(struct gpio_chip *gc,
 
 		div = (u64)debounce * freq;
 		div_reg = DIV_ROUND_CLOSEST_ULL(div, 2 * USEC_PER_SEC) - 1;
-	} else {
-		div_debounce_support = false;
 	}
 
 	raw_spin_lock_irqsave(&bank->slock, flags);
Signature · 7 lines
2.34.1


_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
#6 lore raw

Re: [PATCH v3 04/12] gpio: rockchip: resolve underflow issue

Quoted 2 lines · Ye Zhang

div_reg may be < 0 if debounce is zero, causing the unsigned int to overflow.

...

drivers/gpio/gpio-rockchip.c static int rockchip_gpio_set_debounce(struct gpio_chip *gc, · line 208

Quoted 2 lines
drivers/gpio/gpio-rockchip.c +1 -1
-	if (bank->gpio_type == GPIO_TYPE_V2 && !IS_ERR(bank->db_clk)) {
+	if ((bank->gpio_type == GPIO_TYPE_V2) && !IS_ERR(bank->db_clk))

Stray change.

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

PATCH v3 5/12 gpio: rockchip: fix debounce calculate

In reply to Ye Zhang

The previous configuration ensured that signals with a duration greater than the debounce value would always be detected, while signals with a duration less than debounce / 2 would always not be detected. After the modification, it is changed to ensure that signals with a duration greater than 2 * debounce will always be detected, while signals with a duration less than debounce/2 will still not be detected.

Fixes 3bcbd1a85b68 ("gpio/rockchip: support next version gpio controller")
---
 drivers/gpio/gpio-rockchip.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpio/gpio-rockchip.c +2 -2
index 26191197cd37..75355f799751 100644
@@ -213,13 +213,13 @@ static int rockchip_gpio_set_debounce(struct gpio_chip *gc,
 		freq = clk_get_rate(bank->db_clk);
 		if (!freq)
 			return -EINVAL;
-		div = (u64)(GENMASK(23, 0) + 1) * 2 * HZ_PER_MHZ;
+		div = (u64)(GENMASK(23, 0) + 1) * HZ_PER_MHZ;
 		max_debounce = DIV_ROUND_CLOSEST_ULL(div, freq);
 		if (debounce > max_debounce)
 			return -EINVAL;
 
 		div = (u64)debounce * freq;
-		div_reg = DIV_ROUND_CLOSEST_ULL(div, 2 * USEC_PER_SEC) - 1;
+		div_reg = DIV_ROUND_CLOSEST_ULL(div, USEC_PER_SEC) - 1;
 	}
 
 	raw_spin_lock_irqsave(&bank->slock, flags);
Signature · 7 lines
2.34.1


_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
author
#8 lore raw

PATCH v3 6/12 gpio: rockchip: Update debounce config function

In reply to Ye Zhang

In the GPIO with version number 0x01000C2B, debounce configuration is already supported.

---
 drivers/gpio/gpio-rockchip.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
drivers/gpio/gpio-rockchip.c +4 -13
index 75355f799751..2e80f28bb0c4 100644
@@ -254,6 +254,8 @@ static int rockchip_gpio_set_debounce(struct gpio_chip *gc,
 			clk_prepare_enable(bank->db_clk);
 		else
 			clk_disable_unprepare(bank->db_clk);
+	} else {
+		return -ENOTSUPP;
 	}
 
 	return 0;
@@ -282,22 +284,11 @@ static int rockchip_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
 				  unsigned long config)
 {
 	enum pin_config_param param = pinconf_to_config_param(config);
+	unsigned int debounce = pinconf_to_config_argument(config);
 
 	switch (param) {
 	case PIN_CONFIG_INPUT_DEBOUNCE:
-		rockchip_gpio_set_debounce(gc, offset, true);
-		/*
-		 * Rockchip's gpio could only support up to one period
-		 * of the debounce clock(pclk), which is far away from
-		 * satisftying the requirement, as pclk is usually near
-		 * 100MHz shared by all peripherals. So the fact is it
-		 * has crippled debounce capability could only be useful
-		 * to prevent any spurious glitches from waking up the system
-		 * if the gpio is conguired as wakeup interrupt source. Let's
-		 * still return -ENOTSUPP as before, to make sure the caller
-		 * of gpiod_set_debounce won't change its behaviour.
-		 */
-		return -ENOTSUPP;
+		return rockchip_gpio_set_debounce(gc, offset, debounce);
 	default:
 		return -ENOTSUPP;
 	}
Signature · 7 lines
2.34.1


_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
author
#9 lore raw

PATCH v3 7/12 gpio: rockchip: support 'clock-names' from dt nodes

In reply to Ye Zhang

Added support for retrieving clocks using 'clock-names' from dt nodes

---
 drivers/gpio/gpio-rockchip.c       | 54 +++++++++++++++++++++---------
 drivers/pinctrl/pinctrl-rockchip.h |  2 ++
 2 files changed, 40 insertions(+), 16 deletions(-)
drivers/gpio/gpio-rockchip.c +38 -16
index 2e80f28bb0c4..83df1632112d 100644
@@ -650,23 +650,12 @@ static int rockchip_get_bank_data(struct rockchip_pin_bank *bank)
 	if (!bank->irq)
 		return -EINVAL;
 
-	bank->clk = of_clk_get(bank->of_node, 0);
-	if (IS_ERR(bank->clk))
-		return PTR_ERR(bank->clk);
-
-	clk_prepare_enable(bank->clk);
 	id = readl(bank->reg_base + gpio_regs_v2.version_id);
 
 	/* If not gpio v2, that is default to v1. */
 	if (id == GPIO_TYPE_V2 || id == GPIO_TYPE_V2_1) {
 		bank->gpio_regs = &gpio_regs_v2;
 		bank->gpio_type = GPIO_TYPE_V2;
-		bank->db_clk = of_clk_get(bank->of_node, 1);
-		if (IS_ERR(bank->db_clk)) {
-			dev_err(bank->dev, "cannot find debounce clk\n");
-			clk_disable_unprepare(bank->clk);
-			return -EINVAL;
-		}
 	} else {
 		bank->gpio_regs = &gpio_regs_v1;
 		bank->gpio_type = GPIO_TYPE_V1;
@@ -726,9 +715,31 @@ static int rockchip_gpio_probe(struct platform_device *pdev)
 
 	raw_spin_lock_init(&bank->slock);
 
+	bank->clk = devm_clk_get_enabled(dev, "bus");
+	if (IS_ERR(bank->clk)) {
+		bank->clk = of_clk_get(dev->of_node, 0);
+		if (IS_ERR(bank->clk)) {
+			dev_err(dev, "fail to get apb clock\n");
+			return PTR_ERR(bank->clk);
+		}
+		clk_prepare_enable(bank->clk);
+		bank->manual_clk_release = true;
+	}
+
+	bank->db_clk = devm_clk_get_enabled(dev, "db");
+	if (IS_ERR(bank->db_clk)) {
+		bank->db_clk = of_clk_get(dev->of_node, 1);
+		if (IS_ERR(bank->db_clk)) {
+			bank->db_clk = NULL;
+		} else {
+			clk_prepare_enable(bank->db_clk);
+			bank->manual_dbclk_release = true;
+		}
+	}
+
 	ret = rockchip_get_bank_data(bank);
 	if (ret)
-		return ret;
+		goto err_disabled_clk;
 
 	/*
 	 * Prevent clashes with a deferred output setting
@@ -738,9 +749,8 @@ static int rockchip_gpio_probe(struct platform_device *pdev)
 
 	ret = rockchip_gpiolib_register(bank);
 	if (ret) {
-		clk_disable_unprepare(bank->clk);
-		mutex_unlock(&bank->deferred_lock);
-		return ret;
+		dev_err(bank->dev, "Failed to register gpio %d\n", ret);
+		goto err_unlock;
 	}
 
 	while (!list_empty(&bank->deferred_pins)) {
@@ -773,13 +783,25 @@ static int rockchip_gpio_probe(struct platform_device *pdev)
 	dev_info(dev, "probed %pOF\n", np);
 
 	return 0;
+err_unlock:
+	mutex_unlock(&bank->deferred_lock);
+err_disabled_clk:
+	if (bank->manual_clk_release)
+		clk_disable_unprepare(bank->clk);
+	if (bank->manual_dbclk_release)
+		clk_disable_unprepare(bank->db_clk);
+
+	return ret;
 }
 
 static void rockchip_gpio_remove(struct platform_device *pdev)
 {
 	struct rockchip_pin_bank *bank = platform_get_drvdata(pdev);
 
-	clk_disable_unprepare(bank->clk);
+	if (bank->manual_clk_release)
+		clk_disable_unprepare(bank->clk);
+	if (bank->manual_dbclk_release)
+		clk_disable_unprepare(bank->db_clk);
 	gpiochip_remove(&bank->gpio_chip);
 }
 
drivers/pinctrl/pinctrl-rockchip.h +2 -0
index 0b2b56014b17..bb6df7296508 100644
@@ -319,6 +319,8 @@ struct rockchip_pin_bank {
 	struct regmap			*regmap_pull;
 	struct clk			*clk;
 	struct clk			*db_clk;
+	bool				manual_clk_release;
+	bool				manual_dbclk_release;
 	int				irq;
 	u32				saved_masks;
 	u32				pin_base;
Signature · 7 lines
2.34.1


_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
#10 lore raw

Re: [PATCH v3 07/12] gpio: rockchip: support 'clock-names' from dt nodes

Quoted 1 line · Ye Zhang

Added support for retrieving clocks using 'clock-names' from dt nodes

...

drivers/gpio/gpio-rockchip.c static int rockchip_gpio_probe(struct platform_device *pdev) · line 718

Quoted 1 line
drivers/gpio/gpio-rockchip.c +1 -0
+	bank->clk = devm_clk_get_enabled(dev, "bus");

I would use _optional() variant.

drivers/gpio/gpio-rockchip.c static int rockchip_gpio_probe(struct platform_device *pdev)

Quoted 8 lines
drivers/gpio/gpio-rockchip.c +7 -0
+	if (IS_ERR(bank->clk)) {
+		bank->clk = of_clk_get(dev->of_node, 0);
+		if (IS_ERR(bank->clk)) {
+			dev_err(dev, "fail to get apb clock\n");
+			return PTR_ERR(bank->clk);
+		}
 
+		clk_prepare_enable(bank->clk);

Why? How was it tested?

Review

Quoted 2 lines
(quoted diff) +2 -0
+		bank->manual_clk_release = true;
+	}

It seems you need to unnest the conditionals and think through the logic.

drivers/gpio/gpio-rockchip.c static int rockchip_gpio_probe(struct platform_device *pdev) · line 735

Quoted 7 lines
drivers/gpio/gpio-rockchip.c +7 -0
+	bank->db_clk = devm_clk_get_enabled(dev, "db");
+	if (IS_ERR(bank->db_clk)) {
+		bank->db_clk = of_clk_get(dev->of_node, 1);
+		if (IS_ERR(bank->db_clk)) {
+			bank->db_clk = NULL;
+		} else {
+			clk_prepare_enable(bank->db_clk);

Ditto.

Quoted 3 lines
drivers/gpio/gpio-rockchip.c +3 -0
+			bank->manual_dbclk_release = true;
+		}
+	}

...

drivers/gpio/gpio-rockchip.c static int rockchip_gpio_probe(struct platform_device *pdev) · line 742

Quoted 4 lines
drivers/gpio/gpio-rockchip.c +1 -1
 	ret = rockchip_get_bank_data(bank);
 	if (ret)
-		return ret;
+		goto err_disabled_clk;

No, just make sure it's properly wrapped by devm.

...

drivers/pinctrl/pinctrl-rockchip.h struct rockchip_pin_bank { · line 323

Quoted 2 lines
drivers/pinctrl/pinctrl-rockchip.h +2 -0
+	bool				manual_clk_release;
+	bool				manual_dbclk_release;

Why do you need them?

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

PATCH v3 8/12 gpio: rockchip: explan the format of the GPIO version ID

In reply to Ye Zhang

Remove redundant comments and provide a detailed explanation of the GPIO version ID.

---
 drivers/gpio/gpio-rockchip.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpio/gpio-rockchip.c +8 -2
index 83df1632112d..04a24f1d77eb 100644
@@ -27,9 +27,15 @@
 #include "../pinctrl/core.h"
 #include "../pinctrl/pinctrl-rockchip.h"
 
+/*
+ * Version ID Register
+ * Bits [31:24] - Major Version
+ * Bits [23:16] - Minor Version
+ * Bits [15:0]  - SVN Number
+ */
 #define GPIO_TYPE_V1		(0)           /* GPIO Version ID reserved */
-#define GPIO_TYPE_V2		(0x01000C2B)  /* GPIO Version ID 0x01000C2B */
-#define GPIO_TYPE_V2_1		(0x0101157C)  /* GPIO Version ID 0x0101157C */
+#define GPIO_TYPE_V2		(0x01000C2B)
+#define GPIO_TYPE_V2_1		(0x0101157C)
 
 static const struct rockchip_gpio_regs gpio_regs_v1 = {
 	.port_dr = 0x00,
Signature · 7 lines
2.34.1


_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
Reviewed-by on v3 8/12
#12 lore raw

Re: [PATCH v3 08/12] gpio: rockchip: explan the format of the GPIO version ID

Quoted 2 lines · Ye Zhang

Remove redundant comments and provide a detailed explanation of the GPIO version ID.

"explain" in the Subject.

(and maybe others)

Bart, I think this one is good to go (with spelling fix). And it makes these values to be described which looks like a win to me.

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

PATCH v3 9/12 gpio: rockchip: change the GPIO version judgment logic

In reply to Ye Zhang

Have a list of valid V1 IDs and default to -ENODEV.

---
 drivers/gpio/gpio-rockchip.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/gpio/gpio-rockchip.c +12 -5
index 04a24f1d77eb..579701ad3c6f 100644
@@ -658,13 +658,20 @@ static int rockchip_get_bank_data(struct rockchip_pin_bank *bank)
 
 	id = readl(bank->reg_base + gpio_regs_v2.version_id);
 
-	/* If not gpio v2, that is default to v1. */
-	if (id == GPIO_TYPE_V2 || id == GPIO_TYPE_V2_1) {
-		bank->gpio_regs = &gpio_regs_v2;
-		bank->gpio_type = GPIO_TYPE_V2;
-	} else {
+	/* The GPIO version ID is incrementing. */
+	switch (id) {
+	case GPIO_TYPE_V1:
 		bank->gpio_regs = &gpio_regs_v1;
 		bank->gpio_type = GPIO_TYPE_V1;
+		break;
+	case GPIO_TYPE_V2:
+	case GPIO_TYPE_V2_1:
+		bank->gpio_regs = &gpio_regs_v2;
+		bank->gpio_type = GPIO_TYPE_V2;
+		break;
+	default:
+		dev_err(bank->dev, "cannot get the version ID\n");
+		return -ENODEV;
 	}
 
 	return 0;
Signature · 7 lines
2.34.1


_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
#14 lore raw

Re: [PATCH v3 09/12] gpio: rockchip: change the GPIO version judgment logic

Quoted 1 line · Ye Zhang

Have a list of valid V1 IDs and default to -ENODEV.

s/V1//

...

Quoted 3 lines

id = readl(bank->reg_base + gpio_regs_v2.version_id);

You may remove this blank line now...

drivers/gpio/gpio-rockchip.c static int rockchip_get_bank_data(struct rockchip_pin_bank *bank) · line 662

Quoted 7 lines
drivers/gpio/gpio-rockchip.c +2 -5
-	/* If not gpio v2, that is default to v1. */
-	if (id == GPIO_TYPE_V2 || id == GPIO_TYPE_V2_1) {
-		bank->gpio_regs = &gpio_regs_v2;
-		bank->gpio_type = GPIO_TYPE_V2;
-	} else {
+	/* The GPIO version ID is incrementing. */
+	switch (id) {

...basically to have

	/* The GPIO version ID is incrementing. */
	id = readl(bank->reg_base + gpio_regs_v2.version_id);
	switch (id) {
Quoted 13 lines
drivers/gpio/gpio-rockchip.c +10 -0
+	case GPIO_TYPE_V1:
 		bank->gpio_regs = &gpio_regs_v1;
 		bank->gpio_type = GPIO_TYPE_V1;
+		break;
+	case GPIO_TYPE_V2:
+	case GPIO_TYPE_V2_1:
+		bank->gpio_regs = &gpio_regs_v2;
+		bank->gpio_type = GPIO_TYPE_V2;
+		break;
+	default:
+		dev_err(bank->dev, "cannot get the version ID\n");
+		return -ENODEV;
 	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#15 lore raw

PATCH v3 10/12 gpio: rockchip: support new version gpio

In reply to Ye Zhang

The next version gpio controller on SoCs like rk3576 which support four OS operation and four interrupts

---
 drivers/gpio/gpio-rockchip.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)
drivers/gpio/gpio-rockchip.c +20 -9
index 579701ad3c6f..6deebf1dd9dd 100644
@@ -36,6 +36,7 @@
 #define GPIO_TYPE_V1		(0)           /* GPIO Version ID reserved */
 #define GPIO_TYPE_V2		(0x01000C2B)
 #define GPIO_TYPE_V2_1		(0x0101157C)
+#define GPIO_TYPE_V2_2		(0x010219C8)
 
 static const struct rockchip_gpio_regs gpio_regs_v1 = {
 	.port_dr = 0x00,
@@ -85,7 +86,7 @@ static inline void rockchip_gpio_writel(struct rockchip_pin_bank *bank,
 {
 	void __iomem *reg = bank->reg_base + offset;
 
-	if (bank->gpio_type == GPIO_TYPE_V2)
+	if (bank->gpio_type >= GPIO_TYPE_V2)
 		gpio_writel_v2(value, reg);
 	else
 		writel(value, reg);
@@ -97,7 +98,7 @@ static inline u32 rockchip_gpio_readl(struct rockchip_pin_bank *bank,
 	void __iomem *reg = bank->reg_base + offset;
 	u32 value;
 
-	if (bank->gpio_type == GPIO_TYPE_V2)
+	if (bank->gpio_type >= GPIO_TYPE_V2)
 		value = gpio_readl_v2(reg);
 	else
 		value = readl(reg);
@@ -112,7 +113,7 @@ static inline void rockchip_gpio_writel_bit(struct rockchip_pin_bank *bank,
 	void __iomem *reg = bank->reg_base + offset;
 	u32 data;
 
-	if (bank->gpio_type == GPIO_TYPE_V2) {
+	if (bank->gpio_type >= GPIO_TYPE_V2) {
 		if (value)
 			data = BIT(bit % 16) | BIT(bit % 16 + 16);
 		else
@@ -133,7 +134,7 @@ static inline u32 rockchip_gpio_readl_bit(struct rockchip_pin_bank *bank,
 	void __iomem *reg = bank->reg_base + offset;
 	u32 data;
 
-	if (bank->gpio_type == GPIO_TYPE_V2) {
+	if (bank->gpio_type >= GPIO_TYPE_V2) {
 		data = readl(bit >= 16 ? reg + 0x4 : reg);
 		data >>= bit % 16;
 	} else {
@@ -220,12 +221,18 @@ static int rockchip_gpio_set_debounce(struct gpio_chip *gc,
 		if (!freq)
 			return -EINVAL;
 		div = (u64)(GENMASK(23, 0) + 1) * HZ_PER_MHZ;
-		max_debounce = DIV_ROUND_CLOSEST_ULL(div, freq);
+		if (bank->gpio_type == GPIO_TYPE_V2)
+			max_debounce = DIV_ROUND_CLOSEST_ULL(div, freq);
+		else
+			max_debounce = DIV_ROUND_CLOSEST_ULL(div, 2 * freq);
 		if (debounce > max_debounce)
 			return -EINVAL;
 
 		div = (u64)debounce * freq;
-		div_reg = DIV_ROUND_CLOSEST_ULL(div, USEC_PER_SEC) - 1;
+		if (bank->gpio_type == GPIO_TYPE_V2)
+			div_reg = DIV_ROUND_CLOSEST_ULL(div, USEC_PER_SEC) - 1;
+		else
+			div_reg = DIV_ROUND_CLOSEST_ULL(div, USEC_PER_SEC / 2) - 1;
 	}
 
 	raw_spin_lock_irqsave(&bank->slock, flags);
@@ -411,7 +418,7 @@ static int rockchip_irq_set_type(struct irq_data *d, unsigned int type)
 	polarity = rockchip_gpio_readl(bank, bank->gpio_regs->int_polarity);
 
 	if (type == IRQ_TYPE_EDGE_BOTH) {
-		if (bank->gpio_type == GPIO_TYPE_V2) {
+		if (bank->gpio_type >= GPIO_TYPE_V2) {
 			rockchip_gpio_writel_bit(bank, d->hwirq, 1,
 						 bank->gpio_regs->int_bothedge);
 			goto out;
@@ -430,7 +437,7 @@ static int rockchip_irq_set_type(struct irq_data *d, unsigned int type)
 				polarity |= mask;
 		}
 	} else {
-		if (bank->gpio_type == GPIO_TYPE_V2) {
+		if (bank->gpio_type >= GPIO_TYPE_V2) {
 			rockchip_gpio_writel_bit(bank, d->hwirq, 0,
 						 bank->gpio_regs->int_bothedge);
 		} else {
@@ -536,7 +543,7 @@ static int rockchip_interrupts_register(struct rockchip_pin_bank *bank)
 	}
 
 	gc = irq_get_domain_generic_chip(bank->domain, 0);
-	if (bank->gpio_type == GPIO_TYPE_V2) {
+	if (bank->gpio_type >= GPIO_TYPE_V2) {
 		gc->reg_writel = gpio_writel_v2;
 		gc->reg_readl = gpio_readl_v2;
 	}
@@ -669,6 +676,10 @@ static int rockchip_get_bank_data(struct rockchip_pin_bank *bank)
 		bank->gpio_regs = &gpio_regs_v2;
 		bank->gpio_type = GPIO_TYPE_V2;
 		break;
+	case GPIO_TYPE_V2_2:
+		bank->gpio_regs = &gpio_regs_v2;
+		bank->gpio_type = GPIO_TYPE_V2_2;
+		break;
 	default:
 		dev_err(bank->dev, "cannot get the version ID\n");
 		return -ENODEV;
Signature · 7 lines
2.34.1


_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
#16 lore raw

Re: [PATCH v3 10/12] gpio: rockchip: support new version gpio

Quoted 1 line · Ye Zhang

The next version gpio controller on SoCs like rk3576 which support four

GPIO

Quoted 1 line

OS operation and four interrupts

operations ?

Also missing period at the end.

...

What does this all mean?

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

PATCH v3 11/12 gpio: rockchip: Set input direction when request irq

In reply to Ye Zhang

Since the GPIO can only generate interrupts when its direction is set to input, it is set to input before requesting the interrupt resources.

---
 drivers/gpio/gpio-rockchip.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpio/gpio-rockchip.c +5 -1
index 6deebf1dd9dd..73e57efb46fc 100644
@@ -476,10 +476,14 @@ static int rockchip_irq_set_type(struct irq_data *d, unsigned int type)
 
 static int rockchip_irq_reqres(struct irq_data *d)
 {
+	irq_hw_number_t hwirq;
 	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
 	struct rockchip_pin_bank *bank = gc->private;
 
-	return gpiochip_reqres_irq(&bank->gpio_chip, d->hwirq);
+	hwirq = irqd_to_hwirq(d);
+	rockchip_gpio_direction_input(&bank->gpio_chip, hwirq);
+
+	return gpiochip_reqres_irq(&bank->gpio_chip, hwirq);
 }
 
 static void rockchip_irq_relres(struct irq_data *d)
Signature · 7 lines
2.34.1


_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
#18 lore raw

Re: [PATCH v3 11/12] gpio: rockchip: Set input direction when request irq

Quoted 2 lines · Ye Zhang

Since the GPIO can only generate interrupts when its direction is set to input, it is set to input before requesting the interrupt resources.

...

drivers/gpio/gpio-rockchip.c static int rockchip_irq_set_type(struct irq_data *d, unsigned int type) · line 484

Quoted 10 lines

static int rockchip_irq_reqres(struct irq_data *d) {

drivers/gpio/gpio-rockchip.c +3 -1
+	irq_hw_number_t hwirq;
 	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
 	struct rockchip_pin_bank *bank = gc->private;
 
-	return gpiochip_reqres_irq(&bank->gpio_chip, d->hwirq);
+	hwirq = irqd_to_hwirq(d);
+	rockchip_gpio_direction_input(&bank->gpio_chip, hwirq);
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
	struct rockchip_pin_bank *bank = gc->private;
	irq_hw_number_t hwirq = irqd_to_hwirq(d);

	rockchip_gpio_direction_input(&bank->gpio_chip, hwirq);
Quoted 2 lines
drivers/gpio/gpio-rockchip.c +1 -0
+	return gpiochip_reqres_irq(&bank->gpio_chip, hwirq);
 }
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#19 lore raw

PATCH v3 12/12 gpio: rockchip: replace mutex_lock() with guard()

In reply to Ye Zhang

Replacing mutex_lock with guard() simplifies the code and helps avoid deadlocks.

---
 drivers/gpio/gpio-rockchip.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/gpio/gpio-rockchip.c +5 -9
index 73e57efb46fc..d5c57617fc86 100644
@@ -765,20 +765,19 @@ static int rockchip_gpio_probe(struct platform_device *pdev)
 		}
 	}
 
-	ret = rockchip_get_bank_data(bank);
-	if (ret)
-		goto err_disabled_clk;
-
 	/*
 	 * Prevent clashes with a deferred output setting
 	 * being added right at this moment.
 	 */
-	mutex_lock(&bank->deferred_lock);
+	guard(mutex)(&bank->deferred_lock);
+	ret = rockchip_get_bank_data(bank);
+	if (ret)
+		goto err_disabled_clk;
 
 	ret = rockchip_gpiolib_register(bank);
 	if (ret) {
 		dev_err(bank->dev, "Failed to register gpio %d\n", ret);
-		goto err_unlock;
+		goto err_disabled_clk;
 	}
 
 	while (!list_empty(&bank->deferred_pins)) {
@@ -805,14 +804,11 @@ static int rockchip_gpio_probe(struct platform_device *pdev)
 		kfree(cfg);
 	}
 
-	mutex_unlock(&bank->deferred_lock);
 
 	platform_set_drvdata(pdev, bank);
 	dev_info(dev, "probed %pOF\n", np);
 
 	return 0;
-err_unlock:
-	mutex_unlock(&bank->deferred_lock);
 err_disabled_clk:
 	if (bank->manual_clk_release)
 		clk_disable_unprepare(bank->clk);
Signature · 7 lines
2.34.1


_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
#20 lore raw

Re: [PATCH v3 12/12] gpio: rockchip: replace mutex_lock() with guard()

Quoted 1 line · Ye Zhang

Replacing mutex_lock with guard() simplifies the code and helps avoid

mutex_lock()

avoiding

Quoted 1 line

deadlocks.

...

drivers/gpio/gpio-rockchip.c

Quoted 2 lines
drivers/gpio/gpio-rockchip.c +0 -0

+ cleanup.h

...

drivers/gpio/gpio-rockchip.c static int rockchip_gpio_probe(struct platform_device *pdev) · line 772

Quoted 3 lines
drivers/gpio/gpio-rockchip.c +1 -1
 	}
-	mutex_lock(&bank->deferred_lock);
+	guard(mutex)(&bank->deferred_lock);

Make it surrounded by blank lines.

Quoted 3 lines
drivers/gpio/gpio-rockchip.c +3 -0
+	ret = rockchip_get_bank_data(bank);
+	if (ret)
+		goto err_disabled_clk;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#21 lore raw

Re: [PATCH v3 12/12] gpio: rockchip: replace mutex_lock() with guard()

In reply to Ye Zhang

Le 03/09/2024 à 09:36, Ye Zhang a écrit :

Quoted 17 lines

Replacing mutex_lock with guard() simplifies the code and helps avoid deadlocks.

---
  drivers/gpio/gpio-rockchip.c | 14 +++++---------
  1 file changed, 5 insertions(+), 9 deletions(-)
drivers/gpio/gpio-rockchip.c +0 -1
index 73e57efb46fc..d5c57617fc86 100644
@@ -765,20 +765,19 @@ static int rockchip_gpio_probe(struct platform_device *pdev)
  		}
  	}
 
-	ret = rockchip_get_bank_data(bank);

drivers/gpio/gpio-rockchip.c static int rockchip_gpio_probe(struct platform_device *pdev) · line 773

Quoted 10 lines
drivers/gpio/gpio-rockchip.c +2 -4
-	if (ret)
-		goto err_disabled_clk;
-
  	/*
  	 * Prevent clashes with a deferred output setting
  	 * being added right at this moment.
  	 */
-	mutex_lock(&bank->deferred_lock);
+	guard(mutex)(&bank->deferred_lock);
+	ret = rockchip_get_bank_data(bank);

rockchip_get_bank_data() was out of the lock before, now it is inside.

It looks ok, but is it on purpose? If so, maybe it could be mentioned or explained why in the changelog ?

CJ

Quoted 16 lines
drivers/gpio/gpio-rockchip.c +3 -2
+	if (ret)
+		goto err_disabled_clk;
 
  	ret = rockchip_gpiolib_register(bank);
  	if (ret) {
  		dev_err(bank->dev, "Failed to register gpio %d\n", ret);
-		goto err_unlock;
+		goto err_disabled_clk;
  	}
 
  	while (!list_empty(&bank->deferred_pins)) {
@@ -805,14 +804,11 @@ static int rockchip_gpio_probe(struct platform_device *pdev)
  		kfree(cfg);
  	}
 
-	mutex_unlock(&bank->deferred_lock);

drivers/gpio/gpio-rockchip.c static int rockchip_gpio_probe(struct platform_device *pdev) · line 814

Quoted 10 lines
drivers/gpio/gpio-rockchip.c +0 -2
 
  	platform_set_drvdata(pdev, bank);
  	dev_info(dev, "probed %pOF\n", np);
 
  	return 0;
-err_unlock:
-	mutex_unlock(&bank->deferred_lock);
  err_disabled_clk:
  	if (bank->manual_clk_release)
  		clk_disable_unprepare(bank->clk);

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

#22 lore raw
In reply to Ye Zhang
Quoted 2 lines · Ye Zhang

GPIO driver support acpi and new version, set input direction in irq_request_resources, fix division error and debounce config error.

Looking at patch 7 it seems the new feature has been barely tested. First of all, I recommend to split out the real fixes for now and send them separately while working on the rest. Second, perform better bisectability (both compile and run time) and overall test coverage in different scenarios.

Signature · 2 lines
With Best Regards,
Andy Shevchenko