Skip to contentSkip to conversation

Devicetree support for Loongson-1 GPIO

PATCH Applied Keguang Zhang 2 participants 8 messages Mar 15, 2023 – Mar 22, 2023

Subsystems (4)

v3 of 12 Applied Latest is v12 →

lore
Share & tools
lore

Keguang Zhang

author maintainer
#0 lore raw

v3 cover Devicetree support for Loongson-1 GPIO

Update the driver to add DT support and dt-binding document, along with other minor changes.

Changelog
V2 -> V3: Explain the reason for changing the author name in commit message
Drop the patch "gpio: loongson1: Use readl() & writel()" Restore the calling of __raw_readl() & __raw_writel() Add Reviewed-by tag from Krzysztof Kozlowski
V1 -> V2: Keep GPLv2, just convert to SPDX identifier
Split the change of calling readl() & writel() to a separate patch Let gpiolib parse ngpios property
Remove unnecessary alias id parsing
Remove superfluous initialization done by bgpio_init() Add MODULE_DEVICE_TABLE()
Other minor fixes
Use the same consistent quotes
Delete superfluous examples

Keguang Zhang (4):
gpio: loongson1: Convert to SPDX identifier
gpio: loongson1: Introduce ls1x_gpio_chip struct
gpio: loongson1: Add DT support
dt-bindings: gpio: Add Loongson-1 GPIO

 .../bindings/gpio/loongson,ls1x-gpio.yaml     | 49 +++++++++++++
 drivers/gpio/gpio-loongson1.c                 | 71 +++++++++++--------
 2 files changed, 92 insertions(+), 28 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/gpio/loongson,ls1x-gpio.yaml

base-commit: 0c14f3aa388d3becd38923869e17f9947a5e5926

Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v3 1/4 gpio: loongson1: Convert to SPDX identifier

Use SPDX-License-Identifier instead of the license text.

The current author name is unofficial, change it to my real name.

---
V2 -> V3: Explain the reason for changing the author name in commit message V1 -> V2: Keep GPLv2, just convert to SPDX identifier

---
 drivers/gpio/gpio-loongson1.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/gpio/gpio-loongson1.c +3 -6
index 5d90b3bc5a25..8862c9ea0d41 100644
@@ -1,11 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0-only
 /*
  * GPIO Driver for Loongson 1 SoC
  *
- * Copyright (C) 2015-2016 Zhang, Keguang <[email protected]>
- *
- * This file is licensed under the terms of the GNU General Public
- * License version 2. This program is licensed "as is" without any
- * warranty of any kind, whether express or implied.
+ * Copyright (C) 2015-2023 Keguang Zhang <[email protected]>
  */
 
 #include <linux/module.h>
@@ -90,6 +87,6 @@ static struct platform_driver ls1x_gpio_driver = {
 
 module_platform_driver(ls1x_gpio_driver);
 
-MODULE_AUTHOR("Kelvin Cheung <[email protected]>");
+MODULE_AUTHOR("Keguang Zhang <[email protected]>");
 MODULE_DESCRIPTION("Loongson1 GPIO driver");
 MODULE_LICENSE("GPL");
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v3 1/4] gpio: loongson1: Convert to SPDX identifier

Quoted 40 lines · Keguang Zhang

Use SPDX-License-Identifier instead of the license text.

The current author name is unofficial, change it to my real name.

---
V2 -> V3: Explain the reason for changing the author name in commit message V1 -> V2: Keep GPLv2, just convert to SPDX identifier

---
 drivers/gpio/gpio-loongson1.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/gpio/gpio-loongson1.c +3 -7
index 5d90b3bc5a25..8862c9ea0d41 100644
@@ -1,11 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0-only
 /*
  * GPIO Driver for Loongson 1 SoC
  *
- * Copyright (C) 2015-2016 Zhang, Keguang <[email protected]>
- *
- * This file is licensed under the terms of the GNU General Public
- * License version 2. This program is licensed "as is" without any
- * warranty of any kind, whether express or implied.
+ * Copyright (C) 2015-2023 Keguang Zhang <[email protected]>
  */
 
 #include <linux/module.h>
@@ -90,6 +87,6 @@ static struct platform_driver ls1x_gpio_driver = {
 
 module_platform_driver(ls1x_gpio_driver);
 
-MODULE_AUTHOR("Kelvin Cheung <[email protected]>");
+MODULE_AUTHOR("Keguang Zhang <[email protected]>");
 MODULE_DESCRIPTION("Loongson1 GPIO driver");
 MODULE_LICENSE("GPL");
--
Quoted 2 lines

2.34.1

Applied, thanks!

Bart

author maintainer
#3 lore raw

PATCH v3 2/4 gpio: loongson1: Introduce ls1x_gpio_chip struct

In reply to Keguang Zhang

This patch introduces and allocates ls1x_gpio_chip struct containing gpio_chip and reg_base to avoid global gpio_reg_base.

---
V2 -> V3: Restore the calling of __raw_readl() & __raw_writel() V1 -> V2: Split this change to a separate patch

---
 drivers/gpio/gpio-loongson1.c | 45 +++++++++++++++++++----------------
 1 file changed, 25 insertions(+), 20 deletions(-)
drivers/gpio/gpio-loongson1.c +25 -20
index 8862c9ea0d41..dddfc71f0e10 100644
@@ -16,15 +16,19 @@
 #define GPIO_DATA		0x20
 #define GPIO_OUTPUT		0x30
 
-static void __iomem *gpio_reg_base;
+struct ls1x_gpio_chip {
+	struct gpio_chip gc;
+	void __iomem *reg_base;
+};
 
 static int ls1x_gpio_request(struct gpio_chip *gc, unsigned int offset)
 {
+	struct ls1x_gpio_chip *ls1x_gc = gpiochip_get_data(gc);
 	unsigned long flags;
 
 	raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
-	__raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) | BIT(offset),
-		     gpio_reg_base + GPIO_CFG);
+	__raw_writel(__raw_readl(ls1x_gc->reg_base + GPIO_CFG) | BIT(offset),
+		     ls1x_gc->reg_base + GPIO_CFG);
 	raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
 
 	return 0;
@@ -32,44 +36,45 @@ static int ls1x_gpio_request(struct gpio_chip *gc, unsigned int offset)
 
 static void ls1x_gpio_free(struct gpio_chip *gc, unsigned int offset)
 {
+	struct ls1x_gpio_chip *ls1x_gc = gpiochip_get_data(gc);
 	unsigned long flags;
 
 	raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
-	__raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) & ~BIT(offset),
-		     gpio_reg_base + GPIO_CFG);
+	__raw_writel(__raw_readl(ls1x_gc->reg_base + GPIO_CFG) & ~BIT(offset),
+		     ls1x_gc->reg_base + GPIO_CFG);
 	raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
 }
 
 static int ls1x_gpio_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
-	struct gpio_chip *gc;
+	struct ls1x_gpio_chip *ls1x_gc;
 	int ret;
 
-	gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
-	if (!gc)
+	ls1x_gc = devm_kzalloc(dev, sizeof(*ls1x_gc), GFP_KERNEL);
+	if (!ls1x_gc)
 		return -ENOMEM;
 
-	gpio_reg_base = devm_platform_ioremap_resource(pdev, 0);
-	if (IS_ERR(gpio_reg_base))
-		return PTR_ERR(gpio_reg_base);
+	ls1x_gc->reg_base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(ls1x_gc->reg_base))
+		return PTR_ERR(ls1x_gc->reg_base);
 
-	ret = bgpio_init(gc, dev, 4, gpio_reg_base + GPIO_DATA,
-			 gpio_reg_base + GPIO_OUTPUT, NULL,
-			 NULL, gpio_reg_base + GPIO_DIR, 0);
+	ret = bgpio_init(&ls1x_gc->gc, dev, 4, ls1x_gc->reg_base + GPIO_DATA,
+			 ls1x_gc->reg_base + GPIO_OUTPUT, NULL,
+			 NULL, ls1x_gc->reg_base + GPIO_DIR, 0);
 	if (ret)
 		goto err;
 
-	gc->owner = THIS_MODULE;
-	gc->request = ls1x_gpio_request;
-	gc->free = ls1x_gpio_free;
-	gc->base = pdev->id * 32;
+	ls1x_gc->gc.owner = THIS_MODULE;
+	ls1x_gc->gc.request = ls1x_gpio_request;
+	ls1x_gc->gc.free = ls1x_gpio_free;
+	ls1x_gc->gc.base = pdev->id * 32;
 
-	ret = devm_gpiochip_add_data(dev, gc, NULL);
+	ret = devm_gpiochip_add_data(dev, &ls1x_gc->gc, ls1x_gc);
 	if (ret)
 		goto err;
 
-	platform_set_drvdata(pdev, gc);
+	platform_set_drvdata(pdev, ls1x_gc);
 	dev_info(dev, "Loongson1 GPIO driver registered\n");
 
 	return 0;
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v3 2/4] gpio: loongson1: Introduce ls1x_gpio_chip struct

Quoted 9 lines · Keguang Zhang

This patch introduces and allocates ls1x_gpio_chip struct containing gpio_chip and reg_base to avoid global gpio_reg_base.

---
V2 -> V3: Restore the calling of __raw_readl() & __raw_writel() V1 -> V2: Split this change to a separate patch
---

Applied, thanks!

Bart

author maintainer
#5 lore raw

PATCH v3 3/4 gpio: loongson1: Add DT support

In reply to Keguang Zhang

This patch adds DT support for Loongson-1 GPIO driver.

---
V2 -> V3: None
V1 -> V2: Let gpiolib parse ngpios property
Remove unnecessary alias id parsing
Remove superfluous initialization done by bgpio_init() Add MODULE_DEVICE_TABLE()
Other minor fixes

---
 drivers/gpio/gpio-loongson1.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
drivers/gpio/gpio-loongson1.c +16 -3
index dddfc71f0e10..6ca3b969db4d 100644
@@ -68,25 +68,38 @@ static int ls1x_gpio_probe(struct platform_device *pdev)
 	ls1x_gc->gc.owner = THIS_MODULE;
 	ls1x_gc->gc.request = ls1x_gpio_request;
 	ls1x_gc->gc.free = ls1x_gpio_free;
-	ls1x_gc->gc.base = pdev->id * 32;
+	/*
+	 * Clear ngpio to let gpiolib get the correct number
+	 * by reading ngpios property
+	 */
+	ls1x_gc->gc.ngpio = 0;
 
 	ret = devm_gpiochip_add_data(dev, &ls1x_gc->gc, ls1x_gc);
 	if (ret)
 		goto err;
 
 	platform_set_drvdata(pdev, ls1x_gc);
-	dev_info(dev, "Loongson1 GPIO driver registered\n");
+
+	dev_info(dev, "GPIO controller registered with %d pins\n",
+		 ls1x_gc->gc.ngpio);
 
 	return 0;
 err:
-	dev_err(dev, "failed to register GPIO device\n");
+	dev_err(dev, "failed to register GPIO controller\n");
 	return ret;
 }
 
+static const struct of_device_id ls1x_gpio_dt_ids[] = {
+	{ .compatible = "loongson,ls1x-gpio" },
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, ls1x_gpio_dt_ids);
+
 static struct platform_driver ls1x_gpio_driver = {
 	.probe	= ls1x_gpio_probe,
 	.driver	= {
 		.name	= "ls1x-gpio",
+		.of_match_table = ls1x_gpio_dt_ids,
 	},
 };
 
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH v3 3/4] gpio: loongson1: Add DT support

Quoted 62 lines · Keguang Zhang

This patch adds DT support for Loongson-1 GPIO driver.

---
V2 -> V3: None
V1 -> V2: Let gpiolib parse ngpios property
Remove unnecessary alias id parsing
Remove superfluous initialization done by bgpio_init() Add MODULE_DEVICE_TABLE()
Other minor fixes

---
 drivers/gpio/gpio-loongson1.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
drivers/gpio/gpio-loongson1.c +16 -4
index dddfc71f0e10..6ca3b969db4d 100644
@@ -68,25 +68,38 @@ static int ls1x_gpio_probe(struct platform_device *pdev)
        ls1x_gc->gc.owner = THIS_MODULE;
        ls1x_gc->gc.request = ls1x_gpio_request;
        ls1x_gc->gc.free = ls1x_gpio_free;
-       ls1x_gc->gc.base = pdev->id * 32;
+       /*
+        * Clear ngpio to let gpiolib get the correct number
+        * by reading ngpios property
+        */
+       ls1x_gc->gc.ngpio = 0;
 
        ret = devm_gpiochip_add_data(dev, &ls1x_gc->gc, ls1x_gc);
        if (ret)
                goto err;
 
        platform_set_drvdata(pdev, ls1x_gc);
-       dev_info(dev, "Loongson1 GPIO driver registered\n");
+
+       dev_info(dev, "GPIO controller registered with %d pins\n",
+                ls1x_gc->gc.ngpio);
 
        return 0;
 err:
-       dev_err(dev, "failed to register GPIO device\n");
+       dev_err(dev, "failed to register GPIO controller\n");
        return ret;
 }
 
+static const struct of_device_id ls1x_gpio_dt_ids[] = {
+       { .compatible = "loongson,ls1x-gpio" },
+       { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, ls1x_gpio_dt_ids);
+
 static struct platform_driver ls1x_gpio_driver = {
        .probe  = ls1x_gpio_probe,
        .driver = {
                .name   = "ls1x-gpio",
+               .of_match_table = ls1x_gpio_dt_ids,
        },
 };
 
--
Quoted 2 lines

2.34.1

I applied this and the last one but switched patches 3 and 4 as it's customary to have bindings in the tree before DT support.

Bart

author maintainer
#7 lore raw

PATCH v3 4/4 dt-bindings: gpio: Add Loongson-1 GPIO

In reply to Keguang Zhang

Add devicetree binding document for Loongson-1 GPIO.

---
V2 -> V3: Add Reviewed-by tag from Krzysztof Kozlowski V1 -> V2: Use the same consistent quotes
Delete superfluous examples

---
 .../bindings/gpio/loongson,ls1x-gpio.yaml     | 49 +++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/gpio/loongson,ls1x-gpio.yaml
Documentation/devicetree/bindings/gpio/loongson,ls1x-gpio.yaml new file +49 -0
new file mode 100644
index 000000000000..1a472c05697c
@@ -0,0 +1,49 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/gpio/loongson,ls1x-gpio.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Loongson-1 GPIO controller
+
+maintainers:
+  - Keguang Zhang <[email protected]>
+
+properties:
+  compatible:
+    const: loongson,ls1x-gpio
+
+  reg:
+    maxItems: 1
+
+  gpio-controller: true
+
+  "#gpio-cells":
+    const: 2
+
+  ngpios:
+    minimum: 1
+    maximum: 32
+
+required:
+  - compatible
+  - reg
+  - gpio-controller
+  - "#gpio-cells"
+  - ngpios
+
+additionalProperties: false
+
+examples:
+  - |
+    gpio0: gpio@1fd010c0 {
+        compatible = "loongson,ls1x-gpio";
+        reg = <0x1fd010c0 0x4>;
+
+        gpio-controller;
+        #gpio-cells = <2>;
+
+        ngpios = <32>;
+    };
+
+...
Signature · 1 line
2.34.1