Skip to contentSkip to conversation

Add Polarfire SoC GPIO support

PATCH Superseded [email protected] 3 participants 12 messages Jul 13, 2022 – Jul 15, 2022

Subsystems (3)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add Polarfire SoC GPIO support

From: Lewis Hanly <[email protected]>

Add a driver to support the Polarfire SoC gpio controller. Tested with 5.19-rc5

MPFS gpio interrupts can be configured as direct or
non direct connections to the PLIC (Platform Level Interrupt Controller). GPIO_INTERRUPT_FAB_CR(31:0) system register will enable GPIO2(31:0) corresponding interrupt on PLIC. e.g. If GPIO_INTERRUPT_FAB_CR bit0 is set then GPIO2 bit0 interrupt is available on the direct input pin on the PLIC.

Changes in v2:
Use raw_spinlock.
Use __assign_bit() to assign bit, added a bool variable for value. Remove unnecessary checking gpio_index.
Remove default from switch statement.
Use const for irq_chip, name updated and use mask/unmask. Use latest kernel api irq set_chip.
Implemented hierarchical interrupt chip support, although suggested to use chained interrupt flow I believe this fits better.

Lewis Hanly (1):
gpio: mpfs: add polarfire soc gpio support

 drivers/gpio/Kconfig     |   9 +
 drivers/gpio/Makefile    |   1 +
 drivers/gpio/gpio-mpfs.c | 379 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 389 insertions(+)
 create mode 100644 drivers/gpio/gpio-mpfs.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/1 gpio: mpfs: add polarfire soc gpio support

From: Lewis Hanly <[email protected]>

Add a driver to support the Polarfire SoC gpio controller.

---
 drivers/gpio/Kconfig     |   9 +
 drivers/gpio/Makefile    |   1 +
 drivers/gpio/gpio-mpfs.c | 379 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 389 insertions(+)
 create mode 100644 drivers/gpio/gpio-mpfs.c
drivers/gpio/Kconfig +9 -0
index b01961999ced..004b377b73f6 100644
@@ -490,6 +490,15 @@ config GPIO_PMIC_EIC_SPRD
 	help
 	  Say yes here to support Spreadtrum PMIC EIC device.
 
+config GPIO_POLARFIRE_SOC
+	bool "Microchip FPGA GPIO support"
+	depends on OF_GPIO
+	select GPIOLIB_IRQCHIP
+	select IRQ_DOMAIN_HIERARCHY
+	select GPIO_GENERIC
+	help
+	  Say yes here to support the GPIO device on Microchip FPGAs.
+
 config GPIO_PXA
 	bool "PXA GPIO support"
 	depends on ARCH_PXA || ARCH_MMP || COMPILE_TEST
drivers/gpio/Makefile +1 -0
index 14352f6dfe8e..3b8b6703e593 100644
@@ -119,6 +119,7 @@ obj-$(CONFIG_GPIO_PCI_IDIO_16)		+= gpio-pci-idio-16.o
 obj-$(CONFIG_GPIO_PISOSR)		+= gpio-pisosr.o
 obj-$(CONFIG_GPIO_PL061)		+= gpio-pl061.o
 obj-$(CONFIG_GPIO_PMIC_EIC_SPRD)	+= gpio-pmic-eic-sprd.o
+obj-$(CONFIG_GPIO_POLARFIRE_SOC)	+= gpio-mpfs.o
 obj-$(CONFIG_GPIO_PXA)			+= gpio-pxa.o
 obj-$(CONFIG_GPIO_RASPBERRYPI_EXP)	+= gpio-raspberrypi-exp.o
 obj-$(CONFIG_GPIO_RC5T583)		+= gpio-rc5t583.o
drivers/gpio/gpio-mpfs.c new file +379 -0

379 lines not on this page · raw message

Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v2 1/1] gpio: mpfs: add polarfire soc gpio support

Hey Lewis,
Couple comments.

Quoted 42 lines · On 13/07/2022 11:59, [email protected] wrote:

From: Lewis Hanly <[email protected]>

Add a driver to support the Polarfire SoC gpio controller.

---
  drivers/gpio/Kconfig     |   9 +
  drivers/gpio/Makefile    |   1 +
  drivers/gpio/gpio-mpfs.c | 379 +++++++++++++++++++++++++++++++++++++++
  3 files changed, 389 insertions(+)

create mode 100644 drivers/gpio/gpio-mpfs.c

drivers/gpio/gpio-mpfs.c new file +28 -0
+
+static int mpfs_gpio_probe(struct platform_device *pdev)
+{
+	struct clk *clk;
+	struct device *dev = &pdev->dev;
+	struct device_node *node = pdev->dev.of_node;
+	struct device_node *irq_parent;
+	struct gpio_irq_chip *girq;
+	struct irq_domain *parent;
+	struct mpfs_gpio_chip *mpfs_gpio;
+	int i, ret, ngpio;
+
+	mpfs_gpio = devm_kzalloc(dev, sizeof(*mpfs_gpio), GFP_KERNEL);
+	if (!mpfs_gpio)
+		return -ENOMEM;
+
+	mpfs_gpio->base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(mpfs_gpio->base))
+		return PTR_ERR(mpfs_gpio->base);
+
+	clk = devm_clk_get(&pdev->dev, NULL);
+	if (IS_ERR(clk)) {
+		dev_err(&pdev->dev, "devm_clk_get failed\n");
+		return PTR_ERR(clk);
+	}
+
+	ret = clk_prepare_enable(clk);
+	if (ret) {

drivers/gpio/gpio-mpfs.c line 292

Quoted 10 lines
drivers/gpio/gpio-mpfs.c new file +10 -0
+		dev_err(&pdev->dev, "failed to enable clock\n");
+		return ret;
+	}
+
+	mpfs_gpio->clk = clk;
+
+	ngpio = of_irq_count(node);
+	if (ngpio > NUM_GPIO) {
+		dev_err(dev, "Too many GPIO interrupts (max=%d)\n",
+			NUM_GPIO);

This should fit on one line

Quoted 8 lines
drivers/gpio/gpio-mpfs.c new file +8 -0
+		ret = -ENXIO;
+		goto cleanup_clock;
+	}
+
+	irq_parent = of_irq_find_parent(node);
+	if (!irq_parent) {
+		dev_err(dev, "no IRQ parent node\n");
+		ret = -ENODEV;

drivers/gpio/gpio-mpfs.c line 310

Quoted 10 lines
drivers/gpio/gpio-mpfs.c new file +10 -0
+		goto cleanup_clock;
+	}
+	parent = irq_find_host(irq_parent);
+	if (!parent) {
+		dev_err(dev, "no IRQ parent domain\n");
+		ret = -ENODEV;
+		goto cleanup_clock;
+	}
+
+	/* Get the interrupt numbers.

netdev style comment

drivers/gpio/gpio-mpfs.c line 314

Quoted 4 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+	 * Clear/Disable All interrupts before enabling parent interrupts.
+	 */
+	for (i = 0; i < ngpio; i++) {
+		mpfs_gpio->irq_number[i] = platform_get_irq(pdev, i);

You need to handle the case where platform_get_irq returns an error right?

Quoted 9 lines
drivers/gpio/gpio-mpfs.c new file +9 -0
+		mpfs_gpio_assign_bit(mpfs_gpio->base + MPFS_IRQ_REG, i, 1);
+		mpfs_gpio_assign_bit(mpfs_gpio->base + (i * BYTE_BOUNDARY),
+				     MPFS_GPIO_EN_INT, 0);
+	}
+
+	raw_spin_lock_init(&mpfs_gpio->lock);
+
+	mpfs_gpio->gc.direction_input = mpfs_gpio_direction_input;
+	mpfs_gpio->gc.direction_output = mpfs_gpio_direction_output;

drivers/gpio/gpio-mpfs.c line 333

Quoted 10 lines
drivers/gpio/gpio-mpfs.c new file +10 -0
+	mpfs_gpio->gc.get_direction = mpfs_gpio_get_direction;
+	mpfs_gpio->gc.get = mpfs_gpio_get;
+	mpfs_gpio->gc.set = mpfs_gpio_set;
+	mpfs_gpio->gc.base = -1;
+	mpfs_gpio->gc.ngpio = ngpio;
+	mpfs_gpio->gc.label = dev_name(dev);
+	mpfs_gpio->gc.parent = dev;
+	mpfs_gpio->gc.owner = THIS_MODULE;
+
+	/* Get a pointer to the gpio_irq_chip */

I doubt this comment is needed

drivers/gpio/gpio-mpfs.c line 342

Quoted 9 lines
drivers/gpio/gpio-mpfs.c new file +9 -0
+	girq = &mpfs_gpio->gc.irq;
+	gpio_irq_chip_set_chip(girq, &mpfs_gpio_irqchip);
+	girq->fwnode = of_node_to_fwnode(node);
+	girq->parent_domain = parent;
+	girq->child_to_parent_hwirq = mpfs_gpio_child_to_parent_hwirq;
+	girq->handler = handle_bad_irq;
+	girq->default_type = IRQ_TYPE_NONE;
+
+	ret = gpiochip_add_data(&mpfs_gpio->gc, mpfs_gpio);

Can we use a devm_ variant instead here...

Quoted 8 lines
drivers/gpio/gpio-mpfs.c new file +8 -0
+	if (ret)
+		goto cleanup_clock;
+
+	platform_set_drvdata(pdev, mpfs_gpio);
+	dev_info(dev, "Microchip MPFS GPIO registered, ngpio=%d\n", ngpio);
+
+	return 0;
+

drivers/gpio/gpio-mpfs.c line 360

Quoted 10 lines
drivers/gpio/gpio-mpfs.c new file +10 -0
+cleanup_clock:
+	clk_disable_unprepare(mpfs_gpio->clk);
+	return ret;
+}
+
+static int mpfs_gpio_remove(struct platform_device *pdev)
+{
+	struct mpfs_gpio_chip *mpfs_gpio = platform_get_drvdata(pdev);
+
+	gpiochip_remove(&mpfs_gpio->gc);

... and drop this?

drivers/gpio/gpio-mpfs.c line 368

Quoted 8 lines
drivers/gpio/gpio-mpfs.c new file +8 -0
+	clk_disable_unprepare(mpfs_gpio->clk);
+
+	return 0;
+}
+
+static const struct of_device_id mpfs_gpio_match[] = {
+	{ .compatible = "microchip,mpfs-gpio", },
+	{ /* end of list */ },

Don't need a comma after the sentinel

drivers/gpio/gpio-mpfs.c line 379

Quoted 11 lines
drivers/gpio/gpio-mpfs.c new file +11 -0
+};
+
+static struct platform_driver mpfs_gpio_driver = {
+	.probe = mpfs_gpio_probe,
+	.driver = {
+		.name = "microchip,mpfs-gpio",
+		.of_match_table = of_match_ptr(mpfs_gpio_match),
+	},
+	.remove = mpfs_gpio_remove,
+};
+builtin_platform_driver(mpfs_gpio_driver)

Mising semicolon?

Thanks,
Conor.

#3 lore raw

Re: [PATCH v2 1/1] gpio: mpfs: add polarfire soc gpio support

Thanks Conor.

Quoted 49 lines · On Wed, 2022-07-13 at 11:26 +0000, Conor Dooley - M52691 wrote:

Hey Lewis,
Couple comments.

Quoted 45 lines · On 13/07/2022 11:59, [email protected] wrote:

From: Lewis Hanly <[email protected]>

Add a driver to support the Polarfire SoC gpio controller.

---
  drivers/gpio/Kconfig     |   9 +
  drivers/gpio/Makefile    |   1 +
  drivers/gpio/gpio-mpfs.c | 379
+++++++++++++++++++++++++++++++++++++++
  3 files changed, 389 insertions(+)

create mode 100644 drivers/gpio/gpio-mpfs.c

drivers/gpio/gpio-mpfs.c new file +32 -0
+
+static int mpfs_gpio_probe(struct platform_device *pdev)
+{
+	struct clk *clk;
+	struct device *dev = &pdev->dev;
+	struct device_node *node = pdev->dev.of_node;
+	struct device_node *irq_parent;
+	struct gpio_irq_chip *girq;
+	struct irq_domain *parent;
+	struct mpfs_gpio_chip *mpfs_gpio;
+	int i, ret, ngpio;
+
+	mpfs_gpio = devm_kzalloc(dev, sizeof(*mpfs_gpio), GFP_KERNEL);
+	if (!mpfs_gpio)
+		return -ENOMEM;
+
+	mpfs_gpio->base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(mpfs_gpio->base))
+		return PTR_ERR(mpfs_gpio->base);
+
+	clk = devm_clk_get(&pdev->dev, NULL);
+	if (IS_ERR(clk)) {
+		dev_err(&pdev->dev, "devm_clk_get failed\n");
+		return PTR_ERR(clk);
+	}
+
+	ret = clk_prepare_enable(clk);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to enable clock\n");
+		return ret;
+	}
+
Quoted 8 lines

drivers/gpio/gpio-mpfs.c line 292

Quoted 6 lines
drivers/gpio/gpio-mpfs.c new file +6 -0
+	mpfs_gpio->clk = clk;
+
+	ngpio = of_irq_count(node);
+	if (ngpio > NUM_GPIO) {
+		dev_err(dev, "Too many GPIO interrupts (max=%d)\n",
+			NUM_GPIO);

This should fit on one line

Can do.

Quoted 22 lines
Quoted 9 lines
drivers/gpio/gpio-mpfs.c new file +8 -0
+		ret = -ENXIO;
+		goto cleanup_clock;
+	}
+
+	irq_parent = of_irq_find_parent(node);
+	if (!irq_parent) {
+		dev_err(dev, "no IRQ parent node\n");
+		ret = -ENODEV;

drivers/gpio/gpio-mpfs.c line 310

Quoted 10 lines
drivers/gpio/gpio-mpfs.c new file +10 -0
+		goto cleanup_clock;
+	}
+	parent = irq_find_host(irq_parent);
+	if (!parent) {
+		dev_err(dev, "no IRQ parent domain\n");
+		ret = -ENODEV;
+		goto cleanup_clock;
+	}
+
+	/* Get the interrupt numbers.

netdev style comment

Quoted 8 lines

drivers/gpio/gpio-mpfs.c line 314

Quoted 5 lines
(quoted diff) +1 -0
+	 * Clear/Disable All interrupts before enabling parent

interrupts.

drivers/gpio/gpio-mpfs.c new file +3 -0
+	 */
+	for (i = 0; i < ngpio; i++) {
+		mpfs_gpio->irq_number[i] = platform_get_irq(pdev, i);

You need to handle the case where platform_get_irq returns an error right?

Was not going to as I don't think it breaks if error. I will do a little more checking if required.

Quoted 16 lines
Quoted 16 lines
(quoted diff) +1 -0
+		mpfs_gpio_assign_bit(mpfs_gpio->base + MPFS_IRQ_REG, i,

1);

(quoted diff) +1 -0
+		mpfs_gpio_assign_bit(mpfs_gpio->base + (i *

BYTE_BOUNDARY),

drivers/gpio/gpio-mpfs.c new file +11 -0
+				     MPFS_GPIO_EN_INT, 0);
+	}
+
+	raw_spin_lock_init(&mpfs_gpio->lock);
+
+	mpfs_gpio->gc.direction_input = mpfs_gpio_direction_input;
+	mpfs_gpio->gc.direction_output = mpfs_gpio_direction_output;
+	mpfs_gpio->gc.get_direction = mpfs_gpio_get_direction;
+	mpfs_gpio->gc.get = mpfs_gpio_get;
+	mpfs_gpio->gc.set = mpfs_gpio_set;
+	mpfs_gpio->gc.base = -1;
Quoted 8 lines

drivers/gpio/gpio-mpfs.c line 333

Quoted 6 lines
drivers/gpio/gpio-mpfs.c new file +6 -0
+	mpfs_gpio->gc.ngpio = ngpio;
+	mpfs_gpio->gc.label = dev_name(dev);
+	mpfs_gpio->gc.parent = dev;
+	mpfs_gpio->gc.owner = THIS_MODULE;
+
+	/* Get a pointer to the gpio_irq_chip */

I doubt this comment is needed

OK.

Quoted 12 lines

drivers/gpio/gpio-mpfs.c line 342

Quoted 10 lines
drivers/gpio/gpio-mpfs.c new file +9 -0
+	girq = &mpfs_gpio->gc.irq;
+	gpio_irq_chip_set_chip(girq, &mpfs_gpio_irqchip);
+	girq->fwnode = of_node_to_fwnode(node);
+	girq->parent_domain = parent;
+	girq->child_to_parent_hwirq = mpfs_gpio_child_to_parent_hwirq;
+	girq->handler = handle_bad_irq;
+	girq->default_type = IRQ_TYPE_NONE;
+
+	ret = gpiochip_add_data(&mpfs_gpio->gc, mpfs_gpio);

Can we use a devm_ variant instead here...

Maybe, will check.

Quoted 14 lines
Quoted 14 lines
drivers/gpio/gpio-mpfs.c new file +5 -0
+	if (ret)
+		goto cleanup_clock;
+
+	platform_set_drvdata(pdev, mpfs_gpio);
+	dev_info(dev, "Microchip MPFS GPIO registered, ngpio=%d\n",

ngpio);

drivers/gpio/gpio-mpfs.c new file +7 -0
+
+	return 0;
+
+cleanup_clock:
+	clk_disable_unprepare(mpfs_gpio->clk);
+	return ret;
+}
Quoted 8 lines

drivers/gpio/gpio-mpfs.c line 360

Quoted 6 lines
drivers/gpio/gpio-mpfs.c new file +6 -0
+
+static int mpfs_gpio_remove(struct platform_device *pdev)
+{
+	struct mpfs_gpio_chip *mpfs_gpio = platform_get_drvdata(pdev);
+
+	gpiochip_remove(&mpfs_gpio->gc);

... and drop this?

eh, OK.

Quoted 11 lines

drivers/gpio/gpio-mpfs.c line 368

Quoted 9 lines
drivers/gpio/gpio-mpfs.c new file +8 -0
+	clk_disable_unprepare(mpfs_gpio->clk);
+
+	return 0;
+}
+
+static const struct of_device_id mpfs_gpio_match[] = {
+	{ .compatible = "microchip,mpfs-gpio", },
+	{ /* end of list */ },

Don't need a comma after the sentinel

OK.

Quoted 14 lines

drivers/gpio/gpio-mpfs.c line 379

Quoted 12 lines
drivers/gpio/gpio-mpfs.c new file +11 -0
+};
+
+static struct platform_driver mpfs_gpio_driver = {
+	.probe = mpfs_gpio_probe,
+	.driver = {
+		.name = "microchip,mpfs-gpio",
+		.of_match_table = of_match_ptr(mpfs_gpio_match),
+	},
+	.remove = mpfs_gpio_remove,
+};
+builtin_platform_driver(mpfs_gpio_driver)

Mising semicolon?

Can add..

Quoted 3 lines

Thanks,
Conor.

#4 lore raw

Re: [PATCH v2 1/1] gpio: mpfs: add polarfire soc gpio support

In reply to [email protected]
Quoted 4 lines · On Wed, Jul 13, 2022 at 1:00 PM <[email protected]> wrote:

From: Lewis Hanly <[email protected]>

Add a driver to support the Polarfire SoC gpio controller.

GPIO

...

Below my comments, I have tried hard not to duplicate what Conor already mentioned. So consider this as additional part.

...

drivers/gpio/Kconfig config GPIO_PMIC_EIC_SPRD · line 494

Quoted 2 lines
drivers/gpio/Kconfig +2 -0
+config GPIO_POLARFIRE_SOC
+       bool "Microchip FPGA GPIO support"

Why not tristate?

Quoted 1 line

+ depends on OF_GPIO

Why?

drivers/gpio/Kconfig config GPIO_PMIC_EIC_SPRD · line 497

Quoted 2 lines
drivers/gpio/Kconfig +2 -0
+       select GPIOLIB_IRQCHIP
+       select IRQ_DOMAIN_HIERARCHY

More naturally this line looks if put before GPIOLB_IRQCHIP one.

drivers/gpio/Kconfig config GPIO_PMIC_EIC_SPRD · line 500

Quoted 3 lines
drivers/gpio/Kconfig +3 -0
+       select GPIO_GENERIC
+       help
+         Say yes here to support the GPIO device on Microchip FPGAs.

When allowing it to be a module, add a text that tells how the driver will be called.

...

Quoted 8 lines

+/*
+ * Microchip PolarFire SoC (MPFS) GPIO controller driver
+ *
+ * Copyright (c) 2018-2022 Microchip Technology Inc. and its subsidiaries
+ *
+ * Author: Lewis Hanly <[email protected]>

+ *

This line is not needed.

Quoted 1 line

+ */

...

drivers/gpio/gpio-mpfs.c line 20

Quoted 2 lines
drivers/gpio/gpio-mpfs.c new file +2 -0
+#include <linux/of.h>
+#include <linux/of_irq.h>

Why?

Also don't forget mod_devicetable.h.

...

drivers/gpio/gpio-mpfs.c line 25

Quoted 2 lines
drivers/gpio/gpio-mpfs.c new file +2 -0
+#define NUM_GPIO                       32
+#define BYTE_BOUNDARY                  0x04

Without namespace?

...

drivers/gpio/gpio-mpfs.c line 69

Quoted 3 lines
drivers/gpio/gpio-mpfs.c new file +2 -0
+       gpio_cfg = readl(mpfs_gpio->base + (gpio_index * BYTE_BOUNDARY));
 
+

Unneeded line.

drivers/gpio/gpio-mpfs.c line 106

Quoted 4 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+       if (gpio_cfg & MPFS_GPIO_EN_IN)
+               return 1;
+
+       return 0;

Don't we have specific definitions for the directions?

...

drivers/gpio/gpio-mpfs.c line 135

Quoted 5 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+       struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
+       int gpio_index = irqd_to_hwirq(data);
+       u32 interrupt_type;
 
+       struct mpfs_gpio_chip *mpfs_gpio = gpiochip_get_data(gc);

This line naturally looks better before interrupt_type definition. Try to keep the "longest line first" everywhere in the driver.

Quoted 2 lines
(quoted diff) +2 -0
+       u32 gpio_cfg;
+       unsigned long flags;

...

drivers/gpio/gpio-mpfs.c line 143

Quoted 6 lines
drivers/gpio/gpio-mpfs.c new file +5 -0
+       switch (type) {
+       case IRQ_TYPE_EDGE_BOTH:
+               interrupt_type = MPFS_GPIO_TYPE_INT_EDGE_BOTH;
+               break;
 
+

Unneeded line here and everywhere in the similar cases in the entire code.

Quoted 16 lines
drivers/gpio/gpio-mpfs.c new file +16 -0
+       case IRQ_TYPE_EDGE_FALLING:
+               interrupt_type = MPFS_GPIO_TYPE_INT_EDGE_NEG;
+               break;
+
+       case IRQ_TYPE_EDGE_RISING:
+               interrupt_type = MPFS_GPIO_TYPE_INT_EDGE_POS;
+               break;
+
+       case IRQ_TYPE_LEVEL_HIGH:
+               interrupt_type = MPFS_GPIO_TYPE_INT_LEVEL_HIGH;
+               break;
+
+       case IRQ_TYPE_LEVEL_LOW:
+               interrupt_type = MPFS_GPIO_TYPE_INT_LEVEL_LOW;
+               break;
+       }

...

drivers/gpio/gpio-mpfs.c line 186

Quoted 2 lines
drivers/gpio/gpio-mpfs.c new file +2 -0
+       mpfs_gpio_assign_bit(mpfs_gpio->base + (gpio_index * BYTE_BOUNDARY),
+                            MPFS_GPIO_EN_INT, 1);

Too many parentheses.

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+       mpfs_gpio_assign_bit(mpfs_gpio->base + (gpio_index * BYTE_BOUNDARY),
+                            MPFS_GPIO_EN_INT, 0);

Ditto.

...

drivers/gpio/gpio-mpfs.c line 227

Quoted 11 lines
drivers/gpio/gpio-mpfs.c new file +9 -0
+static int mpfs_gpio_irq_set_affinity(struct irq_data *data,
+                                     const struct cpumask *dest,
+                                     bool force)
+{
 
+       if (data->parent_data)
 
+               return irq_chip_set_affinity_parent(data, dest, force);
+
+       return -EINVAL;
+}

Why do you need this? Isn't it taken care of by the IRQ chip core?

...

drivers/gpio/gpio-mpfs.c line 261

Quoted 5 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+       struct clk *clk;
+       struct device *dev = &pdev->dev;
 
+       struct device_node *node = pdev->dev.of_node;
+       struct device_node *irq_parent;

Why do you need these?

Quoted 4 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+       struct gpio_irq_chip *girq;
+       struct irq_domain *parent;
+       struct mpfs_gpio_chip *mpfs_gpio;
+       int i, ret, ngpio;

...

drivers/gpio/gpio-mpfs.c line 279

Quoted 5 lines
drivers/gpio/gpio-mpfs.c new file +5 -0
+       clk = devm_clk_get(&pdev->dev, NULL);
+       if (IS_ERR(clk)) {
+               dev_err(&pdev->dev, "devm_clk_get failed\n");
+               return PTR_ERR(clk);
+       }

return dev_err_probe(...);

It's not only convenient, but here it fixes a bug.

drivers/gpio/gpio-mpfs.c line 284

Quoted 4 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+       ret = clk_prepare_enable(clk);
+       if (ret) {
+               dev_err(&pdev->dev, "failed to enable clock\n");
+               return ret;

return dev_err_probe(...);

Quoted 1 line

+ }

Make it managed as well in addition to gpiochip_add_data(), otherwise you will have wrong ordering.

...

drivers/gpio/gpio-mpfs.c line 294

Quoted 7 lines
drivers/gpio/gpio-mpfs.c new file +6 -0
+       ngpio = of_irq_count(node);
+       if (ngpio > NUM_GPIO) {
 
+               dev_err(dev, "Too many GPIO interrupts (max=%d)\n",
+                       NUM_GPIO);
+               ret = -ENXIO;
+               goto cleanup_clock;

return dev_err_probe(...);

drivers/gpio/gpio-mpfs.c line 301

Quoted 7 lines
drivers/gpio/gpio-mpfs.c new file +7 -0
+       }
+
+       irq_parent = of_irq_find_parent(node);
+       if (!irq_parent) {
+               dev_err(dev, "no IRQ parent node\n");
+               ret = -ENODEV;
+               goto cleanup_clock;

Ditto.

drivers/gpio/gpio-mpfs.c line 307

Quoted 6 lines
drivers/gpio/gpio-mpfs.c new file +6 -0
+       }
+       parent = irq_find_host(irq_parent);
+       if (!parent) {
+               dev_err(dev, "no IRQ parent domain\n");
+               ret = -ENODEV;
+               goto cleanup_clock;

Ditto.

Quoted 1 line

+ }

Why do you need all these? Seems a copy'n'paste from gpio-sifive, which is the only GPIO driver using this pattern.

...

drivers/gpio/gpio-mpfs.c line 317

Quoted 2 lines
drivers/gpio/gpio-mpfs.c new file +2 -0
+               mpfs_gpio_assign_bit(mpfs_gpio->base + (i * BYTE_BOUNDARY),
+                                    MPFS_GPIO_EN_INT, 0);

Too many parentheses.

Quoted 1 line

+ girq->fwnode = of_node_to_fwnode(node);

This is an interesting way of

    ...->fwnode = dev_fwnode(dev);

...

Quoted 1 line

+ dev_info(dev, "Microchip MPFS GPIO registered, ngpio=%d\n", ngpio);

Noise.

...

Quoted 1 line

+ .of_match_table = of_match_ptr(mpfs_gpio_match),

Please, avoid using of_match_ptr(). It brings more harm than usefulness.

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

Re: [PATCH v2 1/1] gpio: mpfs: add polarfire soc gpio support

Quoted 11 lines · Andy Shevchenko

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Below my comments, I have tried hard not to duplicate what Conor already mentioned. So consider this as additional part.

drivers/gpio/gpio-mpfs.c line 25

Quoted 2 lines
drivers/gpio/gpio-mpfs.c new file +2 -0
+#define NUM_GPIO                       32
+#define BYTE_BOUNDARY                  0x04

Without namespace?

Does byte_boundary even need to be defined?
is incrementing an address by 0x4 not kinda obvious on its own as to what it is doing?

Quoted 6 lines

drivers/gpio/gpio-mpfs.c line 106

Quoted 4 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+       if (gpio_cfg & MPFS_GPIO_EN_IN)
+               return 1;
+
+       return 0;

Don't we have specific definitions for the directions?

FWIW Lewis, they're GPIO_LINE_DIRECTION_IN & GPIO_LINE_DIRECTION_OUT I thought something like this would surely exist but wasn't sure. Thanks for pointing it out Andy.

Conor.

#6 lore raw

Re: [PATCH v2 1/1] gpio: mpfs: add polarfire soc gpio support

Quoted 2 lines · On Wed, Jul 13, 2022 at 7:44 PM <[email protected]> wrote:
Quoted 1 line · Andy Shevchenko

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

...

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

+#define BYTE_BOUNDARY 0x04

Without namespace?

Does byte_boundary even need to be defined?
is incrementing an address by 0x4 not kinda obvious on its own as to what it is doing?

The less magic is the better.

Btw, have you considered gpio-regmap? Can it be utilized here?

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

Re: [PATCH v2 1/1] gpio: mpfs: add polarfire soc gpio support

Quoted 14 lines · Andy Shevchenko

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 3 lines · On Wed, Jul 13, 2022 at 7:44 PM <[email protected]> wrote:
Quoted 2 lines · Andy Shevchenko

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

...

Quoted 4 lines
Quoted 3 lines
Quoted 1 line

+#define BYTE_BOUNDARY 0x04

Without namespace?

Quoted 7 lines
Quoted 3 lines

Does byte_boundary even need to be defined?
is incrementing an address by 0x4 not kinda obvious on its own as to what it is doing?

The less magic is the better.

Btw, have you considered gpio-regmap? Can it be utilized here?

Yes I have considered regmap, our register map is not mapped out to fully utilize regmap. We could use for one/two registers but not fully.

Quoted 4 lines

--
With Best Regards,
Andy Shevchenko

#8 lore raw

Re: [PATCH v2 1/1] gpio: mpfs: add polarfire soc gpio support

In reply to Andy Shevchenko

Thanks Andy for the feedback.
Points noted and updates will be in next revision.

Quoted 15 lines · Andy Shevchenko

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 3 lines · On Wed, Jul 13, 2022 at 1:00 PM <[email protected]> wrote:

From: Lewis Hanly <[email protected]>

Add a driver to support the Polarfire SoC gpio controller.

GPIO

...

Below my comments, I have tried hard not to duplicate what Conor already mentioned. So consider this as additional part.

Quoted 6 lines

...

drivers/gpio/Kconfig config GPIO_PMIC_EIC_SPRD · line 494

Quoted 2 lines
drivers/gpio/Kconfig +2 -0
+config GPIO_POLARFIRE_SOC
+       bool "Microchip FPGA GPIO support"

Why not tristate?

OK.

Quoted 9 lines
Quoted 2 lines

+ depends on OF_GPIO

Why?

drivers/gpio/Kconfig config GPIO_PMIC_EIC_SPRD · line 497

Quoted 2 lines
drivers/gpio/Kconfig +2 -0
+       select GPIOLIB_IRQCHIP
+       select IRQ_DOMAIN_HIERARCHY

More naturally this line looks if put before GPIOLB_IRQCHIP one.

Yes

Quoted 8 lines
Quoted 5 lines
drivers/gpio/Kconfig +3 -0
+       select GPIO_GENERIC
+       help
+         Say yes here to support the GPIO device on Microchip

FPGAs.

When allowing it to be a module, add a text that tells how the driver will be called.

Not loading as a module.

Quoted 12 lines

...

Quoted 8 lines

+/*
+ * Microchip PolarFire SoC (MPFS) GPIO controller driver
+ *
+ * Copyright (c) 2018-2022 Microchip Technology Inc. and its subsidiaries
+ *
+ * Author: Lewis Hanly <[email protected]>
+ *

Quoted 1 line

This line is not needed.

OK

Quoted 9 lines
Quoted 2 lines

+ */

...

drivers/gpio/gpio-mpfs.c line 20

Quoted 2 lines
drivers/gpio/gpio-mpfs.c new file +2 -0
+#include <linux/of.h>
+#include <linux/of_irq.h>

Why?

Not sure, will check again.

Quoted 2 lines

Also don't forget mod_devicetable.h.

Can't see why I need this header.

Quoted 14 lines

...

drivers/gpio/gpio-mpfs.c line 25

Quoted 2 lines
drivers/gpio/gpio-mpfs.c new file +2 -0
+#define NUM_GPIO                       32
+#define BYTE_BOUNDARY                  0x04

Without namespace?

...

Quoted 3 lines

+ gpio_cfg = readl(mpfs_gpio->base + (gpio_index *
BYTE_BOUNDARY));
+

Quoted 8 lines

Unneeded line.

drivers/gpio/gpio-mpfs.c line 106

Quoted 4 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+       if (gpio_cfg & MPFS_GPIO_EN_IN)
+               return 1;
+
+       return 0;

Don't we have specific definitions for the directions?

No defines in file.

Quoted 10 lines

...

drivers/gpio/gpio-mpfs.c line 135

Quoted 4 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+       struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
+       int gpio_index = irqd_to_hwirq(data);
+       u32 interrupt_type;
+       struct mpfs_gpio_chip *mpfs_gpio = gpiochip_get_data(gc);

This line naturally looks better before interrupt_type definition. Try to keep the "longest line first" everywhere in the driver.

OK

Quoted 6 lines
Quoted 3 lines
(quoted diff) +2 -0
+       u32 gpio_cfg;
+       unsigned long flags;

...

Quoted 8 lines

drivers/gpio/gpio-mpfs.c line 143

Quoted 5 lines
drivers/gpio/gpio-mpfs.c new file +5 -0
+       switch (type) {
+       case IRQ_TYPE_EDGE_BOTH:
+               interrupt_type = MPFS_GPIO_TYPE_INT_EDGE_BOTH;
+               break;
+

Unneeded line here and everywhere in the similar cases in the entire code.

OK

Quoted 20 lines
Quoted 17 lines
drivers/gpio/gpio-mpfs.c new file +16 -0
+       case IRQ_TYPE_EDGE_FALLING:
+               interrupt_type = MPFS_GPIO_TYPE_INT_EDGE_NEG;
+               break;
+
+       case IRQ_TYPE_EDGE_RISING:
+               interrupt_type = MPFS_GPIO_TYPE_INT_EDGE_POS;
+               break;
+
+       case IRQ_TYPE_LEVEL_HIGH:
+               interrupt_type = MPFS_GPIO_TYPE_INT_LEVEL_HIGH;
+               break;
+
+       case IRQ_TYPE_LEVEL_LOW:
+               interrupt_type = MPFS_GPIO_TYPE_INT_LEVEL_LOW;
+               break;
+       }

...

Quoted 5 lines
Quoted 3 lines

+ mpfs_gpio_assign_bit(mpfs_gpio->base + (gpio_index *
BYTE_BOUNDARY),
+ MPFS_GPIO_EN_INT, 1);

Too many parentheses.

I do think it makes reading easier.

Quoted 11 lines

...

Quoted 3 lines

+ mpfs_gpio_assign_bit(mpfs_gpio->base + (gpio_index *
BYTE_BOUNDARY),
+ MPFS_GPIO_EN_INT, 0);

Ditto.

...

Quoted 12 lines

drivers/gpio/gpio-mpfs.c line 227

Quoted 10 lines
drivers/gpio/gpio-mpfs.c new file +6 -0
+static int mpfs_gpio_irq_set_affinity(struct irq_data *data,
+                                     const struct cpumask *dest,
+                                     bool force)
+{
+       if (data->parent_data)
+               return irq_chip_set_affinity_parent(data, dest,

force);

drivers/gpio/gpio-mpfs.c new file +3 -0
+
+       return -EINVAL;
+}

Why do you need this? Isn't it taken care of by the IRQ chip core?

Yes I believe we do/should, data->parent_data is used in irq_chip_set_affinity_parent(..) without checking so hence checked before calling.

Quoted 9 lines

...

drivers/gpio/gpio-mpfs.c line 261

Quoted 4 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+       struct clk *clk;
+       struct device *dev = &pdev->dev;
+       struct device_node *node = pdev->dev.of_node;
+       struct device_node *irq_parent;

Why do you need these?

Yes they are needed. Both of the same type but used in different places. I don't think I can reuse.

Quoted 14 lines
Quoted 5 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+       struct gpio_irq_chip *girq;
+       struct irq_domain *parent;
+       struct mpfs_gpio_chip *mpfs_gpio;
+       int i, ret, ngpio;

...

drivers/gpio/gpio-mpfs.c line 279

Quoted 5 lines
drivers/gpio/gpio-mpfs.c new file +5 -0
+       clk = devm_clk_get(&pdev->dev, NULL);
+       if (IS_ERR(clk)) {
+               dev_err(&pdev->dev, "devm_clk_get failed\n");
+               return PTR_ERR(clk);
+       }
Quoted 3 lines

return dev_err_probe(...);

It's not only convenient, but here it fixes a bug.

Will use return dev_err_probe.

Quoted 7 lines

drivers/gpio/gpio-mpfs.c line 284

Quoted 5 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+       ret = clk_prepare_enable(clk);
+       if (ret) {
+               dev_err(&pdev->dev, "failed to enable clock\n");
+               return ret;

return dev_err_probe(...);

Yes

Quoted 8 lines
Quoted 2 lines

+ }

Make it managed as well in addition to gpiochip_add_data(), otherwise you will have wrong ordering.

...

Quoted 8 lines

drivers/gpio/gpio-mpfs.c line 294

Quoted 6 lines
drivers/gpio/gpio-mpfs.c new file +6 -0
+       ngpio = of_irq_count(node);
+       if (ngpio > NUM_GPIO) {
+               dev_err(dev, "Too many GPIO interrupts (max=%d)\n",
+                       NUM_GPIO);
+               ret = -ENXIO;
+               goto cleanup_clock;

return dev_err_probe(...);

I need to cleanup clock before returning, will use dev_err_probe.

Quoted 18 lines

drivers/gpio/gpio-mpfs.c line 301

Quoted 8 lines
drivers/gpio/gpio-mpfs.c new file +7 -0
+       }
+
+       irq_parent = of_irq_find_parent(node);
+       if (!irq_parent) {
+               dev_err(dev, "no IRQ parent node\n");
+               ret = -ENODEV;
+               goto cleanup_clock;

Ditto.

drivers/gpio/gpio-mpfs.c line 307

Quoted 6 lines
drivers/gpio/gpio-mpfs.c new file +6 -0
+       }
+       parent = irq_find_host(irq_parent);
+       if (!parent) {
+               dev_err(dev, "no IRQ parent domain\n");
+               ret = -ENODEV;
+               goto cleanup_clock;
Quoted 6 lines

Ditto.

Quoted 1 line

+ }

Why do you need all these? Seems a copy'n'paste from gpio-sifive, which is the only GPIO driver using this pattern.

Yes I believe we do need all this information. Yes it is similiar implementation to sifive. Not the only driver using this pattern, check gpio-ixp4xxx.c

Quoted 17 lines

...

Quoted 3 lines

+ mpfs_gpio_assign_bit(mpfs_gpio->base + (i *
BYTE_BOUNDARY),
+ MPFS_GPIO_EN_INT, 0);

Too many parentheses.

Quoted 1 line

+ girq->fwnode = of_node_to_fwnode(node);

This is an interesting way of

    ...->fwnode = dev_fwnode(dev);
Quoted 6 lines

...

Quoted 2 lines

+ dev_info(dev, "Microchip MPFS GPIO registered, ngpio=%d\n",
ngpio);

Noise.

Maybe, but useful information to know the ngpio.

Quoted 7 lines

...

Quoted 1 line

+ .of_match_table = of_match_ptr(mpfs_gpio_match),

Please, avoid using of_match_ptr(). It brings more harm than usefulness.

OK

Quoted 4 lines

--
With Best Regards,
Andy Shevchenko

#9 lore raw

Re: [PATCH v2 1/1] gpio: mpfs: add polarfire soc gpio support

Quoted 2 lines · On Wed, Jul 13, 2022 at 10:44 PM <[email protected]> wrote:
Quoted 1 line · Andy Shevchenko

On Wed, Jul 13, 2022 at 1:00 PM <[email protected]> wrote:

...

Quoted 5 lines
Quoted 4 lines

drivers/gpio/Kconfig config GPIO_PMIC_EIC_SPRD · line 494

Quoted 2 lines
drivers/gpio/Kconfig +2 -0
+config GPIO_POLARFIRE_SOC
+       bool "Microchip FPGA GPIO support"

Why not tristate?

OK.

(1)

...

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
(quoted diff) +2 -0
+       help
+         Say yes here to support the GPIO device on Microchip

FPGAs.

When allowing it to be a module, add a text that tells how the driver will be called.

Not loading as a module.

I didn't get this. Together with (1) it makes nonsense. What did you mean by (1) _or_ by this?

...

Quoted 2 lines
Quoted 1 line

Also don't forget mod_devicetable.h.

Can't see why I need this header.

Because you are using data types from it.

...

Quoted 7 lines
Quoted 6 lines

drivers/gpio/gpio-mpfs.c line 106

Quoted 4 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+       if (gpio_cfg & MPFS_GPIO_EN_IN)
+               return 1;
+
+       return 0;

Don't we have specific definitions for the directions?

No defines in file.

We have. Please, check again.

...

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

+ mpfs_gpio_assign_bit(mpfs_gpio->base + (gpio_index *
BYTE_BOUNDARY),
+ MPFS_GPIO_EN_INT, 1);

Too many parentheses.

I do think it makes reading easier.

You can multiply inside mpfs_gpio_assign_bit(), no?

...

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

+ mpfs_gpio_assign_bit(mpfs_gpio->base + (gpio_index *
BYTE_BOUNDARY),
+ MPFS_GPIO_EN_INT, 0);

Ditto.

...

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+static int mpfs_gpio_irq_set_affinity(struct irq_data *data,
+                                     const struct cpumask *dest,
+                                     bool force)
+{
Quoted 8 lines

drivers/gpio/gpio-mpfs.c line 227

Quoted 6 lines
(quoted diff) +2 -0
+       if (data->parent_data)
+               return irq_chip_set_affinity_parent(data, dest,

force);

drivers/gpio/gpio-mpfs.c new file +3 -0
+
+       return -EINVAL;
+}

Why do you need this? Isn't it taken care of by the IRQ chip core?

Quoted 3 lines

Yes I believe we do/should, data->parent_data is used in irq_chip_set_affinity_parent(..) without checking so hence checked before calling.

I mean the entire function. Isn't it the default in the IRQ chip core? Or can it be made as a default with some flags set?

...

Quoted 6 lines
Quoted 4 lines

drivers/gpio/gpio-mpfs.c line 261

Quoted 2 lines
drivers/gpio/gpio-mpfs.c new file +2 -0
+       struct device_node *node = pdev->dev.of_node;
+       struct device_node *irq_parent;

Why do you need these?

Yes they are needed. Both of the same type but used in different places. I don't think I can reuse.

This is related to the pattern of how you are enumerating IRQs. If the pattern is changed, it would be not needed anymore.

...

Quoted 8 lines
Quoted 7 lines

drivers/gpio/gpio-mpfs.c line 294

Quoted 5 lines
drivers/gpio/gpio-mpfs.c new file +5 -0
+       if (ngpio > NUM_GPIO) {
+               dev_err(dev, "Too many GPIO interrupts (max=%d)\n",
+                       NUM_GPIO);
+               ret = -ENXIO;
+               goto cleanup_clock;

return dev_err_probe(...);

I need to cleanup clock before returning, will use dev_err_probe.

Have you read what I wrote above?
I wrote that you need to wrap the clk_disable_unprepare() into devm, so you may use as I pointed out, i.e. return dev_err_probe() everywhere in the ->probe().

Quoted 1 line
Quoted 1 line
Quoted 1 line

+ }

...

Quoted 5 lines
Quoted 2 lines

Why do you need all these? Seems a copy'n'paste from gpio-sifive, which is the only GPIO driver using this pattern.

Yes I believe we do need all this information. Yes it is similiar implementation to sifive. Not the only driver using this pattern, check gpio-ixp4xxx.c

My question: why do you need this? What is so special about this driver?

...

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

+ mpfs_gpio_assign_bit(mpfs_gpio->base + (i *
BYTE_BOUNDARY),
+ MPFS_GPIO_EN_INT, 0);

Too many parentheses.

As per above.

...

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

+ dev_info(dev, "Microchip MPFS GPIO registered, ngpio=%d\n",
ngpio);

Noise.

Maybe, but useful information to know the ngpio.

Nope. Use sysfs / debugfs / etc. No need to noise the log.

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

Re: [PATCH v2 1/1] gpio: mpfs: add polarfire soc gpio support

Quoted 10 lines · Andy Shevchenko

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 7 lines · On Wed, Jul 13, 2022 at 10:44 PM <[email protected]> wrote:
Quoted 6 lines · Andy Shevchenko
Quoted 4 lines · On Wed, Jul 13, 2022 at 1:00 PM <[email protected]> wrote:
drivers/gpio/gpio-mpfs.c new file +4 -0
+       if (gpio_cfg & MPFS_GPIO_EN_IN)
+               return 1;
+
+       return 0;
Quoted 4 lines
Quoted 2 lines
Quoted 1 line

Don't we have specific definitions for the directions?

No defines in file.

We have. Please, check again.

I said what they were on another reply Lewis:
https://elixir.bootlin.com/linux/latest/source/include/linux/gpio/driver.h#L25

#11 lore raw

Re: [PATCH v2 1/1] gpio: mpfs: add polarfire soc gpio support

In reply to Andy Shevchenko

OK, I have gone through all comments and taken on board your review comments. Version 3 will follow later today.

Quoted 15 lines · Andy Shevchenko

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 3 lines · On Wed, Jul 13, 2022 at 1:00 PM <[email protected]> wrote:

From: Lewis Hanly <[email protected]>

Add a driver to support the Polarfire SoC gpio controller.

GPIO

...

Below my comments, I have tried hard not to duplicate what Conor already mentioned. So consider this as additional part.

Quoted 6 lines

...

drivers/gpio/Kconfig config GPIO_PMIC_EIC_SPRD · line 494

Quoted 2 lines
drivers/gpio/Kconfig +2 -0
+config GPIO_POLARFIRE_SOC
+       bool "Microchip FPGA GPIO support"

Why not tristate?

Not a module, compile time kernel driver allways for Polarfire SoC

Quoted 9 lines
Quoted 2 lines

+ depends on OF_GPIO

Why?

drivers/gpio/Kconfig config GPIO_PMIC_EIC_SPRD · line 497

Quoted 2 lines
drivers/gpio/Kconfig +2 -0
+       select GPIOLIB_IRQCHIP
+       select IRQ_DOMAIN_HIERARCHY

More naturally this line looks if put before GPIOLB_IRQCHIP one.

OK

Quoted 20 lines
Quoted 5 lines
drivers/gpio/Kconfig +3 -0
+       select GPIO_GENERIC
+       help
+         Say yes here to support the GPIO device on Microchip

FPGAs.

When allowing it to be a module, add a text that tells how the driver will be called.

...

Quoted 8 lines

+/*
+ * Microchip PolarFire SoC (MPFS) GPIO controller driver
+ *
+ * Copyright (c) 2018-2022 Microchip Technology Inc. and its subsidiaries
+ *
+ * Author: Lewis Hanly <[email protected]>
+ *

Quoted 1 line

This line is not needed.

OK

Quoted 9 lines
Quoted 2 lines

+ */

...

drivers/gpio/gpio-mpfs.c line 20

Quoted 2 lines
drivers/gpio/gpio-mpfs.c new file +2 -0
+#include <linux/of.h>
+#include <linux/of_irq.h>

Why?

I am using data defined in these headers.

Quoted 2 lines

Also don't forget mod_devicetable.h.

OK

Quoted 66 lines

...

drivers/gpio/gpio-mpfs.c line 25

Quoted 2 lines
drivers/gpio/gpio-mpfs.c new file +2 -0
+#define NUM_GPIO                       32
+#define BYTE_BOUNDARY                  0x04

Without namespace?

...

Quoted 3 lines

+ gpio_cfg = readl(mpfs_gpio->base + (gpio_index *
BYTE_BOUNDARY));
+

Unneeded line.

drivers/gpio/gpio-mpfs.c line 106

Quoted 4 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+       if (gpio_cfg & MPFS_GPIO_EN_IN)
+               return 1;
+
+       return 0;

Don't we have specific definitions for the directions?

...

drivers/gpio/gpio-mpfs.c line 135

Quoted 4 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+       struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
+       int gpio_index = irqd_to_hwirq(data);
+       u32 interrupt_type;
+       struct mpfs_gpio_chip *mpfs_gpio = gpiochip_get_data(gc);

This line naturally looks better before interrupt_type definition. Try to keep the "longest line first" everywhere in the driver.

Quoted 2 lines
(quoted diff) +2 -0
+       u32 gpio_cfg;
+       unsigned long flags;

...

drivers/gpio/gpio-mpfs.c line 143

Quoted 5 lines
drivers/gpio/gpio-mpfs.c new file +5 -0
+       switch (type) {
+       case IRQ_TYPE_EDGE_BOTH:
+               interrupt_type = MPFS_GPIO_TYPE_INT_EDGE_BOTH;
+               break;
+

Unneeded line here and everywhere in the similar cases in the entire code.

Quoted 16 lines
drivers/gpio/gpio-mpfs.c new file +16 -0
+       case IRQ_TYPE_EDGE_FALLING:
+               interrupt_type = MPFS_GPIO_TYPE_INT_EDGE_NEG;
+               break;
+
+       case IRQ_TYPE_EDGE_RISING:
+               interrupt_type = MPFS_GPIO_TYPE_INT_EDGE_POS;
+               break;
+
+       case IRQ_TYPE_LEVEL_HIGH:
+               interrupt_type = MPFS_GPIO_TYPE_INT_LEVEL_HIGH;
+               break;
+
+       case IRQ_TYPE_LEVEL_LOW:
+               interrupt_type = MPFS_GPIO_TYPE_INT_LEVEL_LOW;
+               break;
+       }

...

Quoted 5 lines
Quoted 3 lines

+ mpfs_gpio_assign_bit(mpfs_gpio->base + (gpio_index *
BYTE_BOUNDARY),
+ MPFS_GPIO_EN_INT, 1);

Too many parentheses.

Yes updated in next revision, using macro rather than * BYTE_BOUNDARY.

Quoted 11 lines

...

Quoted 3 lines

+ mpfs_gpio_assign_bit(mpfs_gpio->base + (gpio_index *
BYTE_BOUNDARY),
+ MPFS_GPIO_EN_INT, 0);

Ditto.

...

Quoted 12 lines

drivers/gpio/gpio-mpfs.c line 227

Quoted 10 lines
drivers/gpio/gpio-mpfs.c new file +6 -0
+static int mpfs_gpio_irq_set_affinity(struct irq_data *data,
+                                     const struct cpumask *dest,
+                                     bool force)
+{
+       if (data->parent_data)
+               return irq_chip_set_affinity_parent(data, dest,

force);

drivers/gpio/gpio-mpfs.c new file +3 -0
+
+       return -EINVAL;
+}

Why do you need this? Isn't it taken care of by the IRQ chip core?

irq_chip_set_affinity could be setup directly at irq_chip strcuture initialization, but I am checking parent_data before calling. So I would say yes I do need this.

Quoted 9 lines

...

drivers/gpio/gpio-mpfs.c line 261

Quoted 4 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+       struct clk *clk;
+       struct device *dev = &pdev->dev;
+       struct device_node *node = pdev->dev.of_node;
+       struct device_node *irq_parent;

Why do you need these?

Yes I, for setting up the hierarchical interrupt controller.

Quoted 14 lines
Quoted 5 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+       struct gpio_irq_chip *girq;
+       struct irq_domain *parent;
+       struct mpfs_gpio_chip *mpfs_gpio;
+       int i, ret, ngpio;

...

drivers/gpio/gpio-mpfs.c line 279

Quoted 5 lines
drivers/gpio/gpio-mpfs.c new file +5 -0
+       clk = devm_clk_get(&pdev->dev, NULL);
+       if (IS_ERR(clk)) {
+               dev_err(&pdev->dev, "devm_clk_get failed\n");
+               return PTR_ERR(clk);
+       }
Quoted 3 lines

return dev_err_probe(...);

It's not only convenient, but here it fixes a bug.

Using dev_err_probe instead of dev_err.

Quoted 6 lines

drivers/gpio/gpio-mpfs.c line 284

Quoted 5 lines
drivers/gpio/gpio-mpfs.c new file +4 -0
+       ret = clk_prepare_enable(clk);
+       if (ret) {
+               dev_err(&pdev->dev, "failed to enable clock\n");
+               return ret;
Quoted 6 lines

return dev_err_probe(...);

Quoted 1 line

+ }

Make it managed as well in addition to gpiochip_add_data(), otherwise you will have wrong ordering.

OK.

Quoted 29 lines

...

drivers/gpio/gpio-mpfs.c line 294

Quoted 6 lines
drivers/gpio/gpio-mpfs.c new file +6 -0
+       ngpio = of_irq_count(node);
+       if (ngpio > NUM_GPIO) {
+               dev_err(dev, "Too many GPIO interrupts (max=%d)\n",
+                       NUM_GPIO);
+               ret = -ENXIO;
+               goto cleanup_clock;

return dev_err_probe(...);

drivers/gpio/gpio-mpfs.c line 301

Quoted 7 lines
drivers/gpio/gpio-mpfs.c new file +7 -0
+       }
+
+       irq_parent = of_irq_find_parent(node);
+       if (!irq_parent) {
+               dev_err(dev, "no IRQ parent node\n");
+               ret = -ENODEV;
+               goto cleanup_clock;

Ditto.

drivers/gpio/gpio-mpfs.c line 307

Quoted 6 lines
drivers/gpio/gpio-mpfs.c new file +6 -0
+       }
+       parent = irq_find_host(irq_parent);
+       if (!parent) {
+               dev_err(dev, "no IRQ parent domain\n");
+               ret = -ENODEV;
+               goto cleanup_clock;
Quoted 6 lines

Ditto.

Quoted 1 line

+ }

Why do you need all these? Seems a copy'n'paste from gpio-sifive, which is the only GPIO driver using this pattern.

As above setup of hierarchical interrupt controller, very similiar to the sifive architecture.

Quoted 17 lines

...

Quoted 3 lines

+ mpfs_gpio_assign_bit(mpfs_gpio->base + (i *
BYTE_BOUNDARY),
+ MPFS_GPIO_EN_INT, 0);

Too many parentheses.

Quoted 1 line

+ girq->fwnode = of_node_to_fwnode(node);

This is an interesting way of

    ...->fwnode = dev_fwnode(dev);
Quoted 6 lines

...

Quoted 2 lines

+ dev_info(dev, "Microchip MPFS GPIO registered, ngpio=%d\n",
ngpio);

Noise.

Noise removed.

Quoted 7 lines

...

Quoted 1 line

+ .of_match_table = of_match_ptr(mpfs_gpio_match),

Please, avoid using of_match_ptr(). It brings more harm than usefulness.

OK

Quoted 4 lines

--
With Best Regards,
Andy Shevchenko