diff mbox series

[v7,2/6] gpio: Add Delta TN48M CPLD GPIO driver

Message ID 20211102165657.3428995-2-robert.marko@sartura.hr
State New
Headers show
Series [v7,1/6] mfd: simple-mfd-i2c: Add Delta TN48M CPLD support | expand

Commit Message

Robert Marko Nov. 2, 2021, 4:56 p.m. UTC
Delta TN48M switch has an onboard Lattice CPLD that is used as a GPIO
expander.

The CPLD provides 12 pins in total on the TN48M, but on more advanced
switch models it provides up to 192 pins, so the driver is extendable
to support more switches.

Signed-off-by: Robert Marko <robert.marko@sartura.hr>
---
Changes in v7:
* Change compatibles, reduce their number
* Rework the driver to be easily extendible to support more devices
* Use match data to populate configuration
* Drop reviews and ACK-s as the driver changed

Changes in v6:
* Drop unused header
* Return the return value of device_property_read_u32()
instead of a hardcoded return

Changes in v2:
* Rewrite to use simple I2C MFD and GPIO regmap
* Drop DT bindings for pin numbering
---
 drivers/gpio/Kconfig      |  12 +++++
 drivers/gpio/Makefile     |   1 +
 drivers/gpio/gpio-tn48m.c | 100 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 113 insertions(+)
 create mode 100644 drivers/gpio/gpio-tn48m.c

Comments

Andy Shevchenko Nov. 2, 2021, 7:27 p.m. UTC | #1
On Tue, Nov 2, 2021 at 6:57 PM Robert Marko <robert.marko@sartura.hr> wrote:
>
> Delta TN48M switch has an onboard Lattice CPLD that is used as a GPIO
> expander.
>
> The CPLD provides 12 pins in total on the TN48M, but on more advanced
> switch models it provides up to 192 pins, so the driver is extendable
> to support more switches.

...

> +static int tn48m_gpio_probe(struct platform_device *pdev)
> +{

> +       const struct tn48m_gpio_config *gpio_config = NULL;
> +       struct gpio_regmap_config config = {0};

I don't see a point in assigning these variables.

> +       struct regmap *regmap;
> +       u32 base;
> +       int ret;
> +
> +       if (!pdev->dev.parent)
> +               return -ENODEV;
> +
> +       gpio_config = device_get_match_data(&pdev->dev);
> +       if (!gpio_config)
> +               return -ENODEV;
> +
> +       ret = device_property_read_u32(&pdev->dev, "reg", &base);
> +       if (ret)
> +               return ret;
> +
> +       regmap = dev_get_regmap(pdev->dev.parent, NULL);
> +       if (!regmap)
> +               return -ENODEV;

> +}
Robert Marko Nov. 5, 2021, 11:34 a.m. UTC | #2
On Tue, Nov 2, 2021 at 8:28 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
>
> On Tue, Nov 2, 2021 at 6:57 PM Robert Marko <robert.marko@sartura.hr> wrote:
> >
> > Delta TN48M switch has an onboard Lattice CPLD that is used as a GPIO
> > expander.
> >
> > The CPLD provides 12 pins in total on the TN48M, but on more advanced
> > switch models it provides up to 192 pins, so the driver is extendable
> > to support more switches.
>
> ...
>
> > +static int tn48m_gpio_probe(struct platform_device *pdev)
> > +{
>
> > +       const struct tn48m_gpio_config *gpio_config = NULL;
> > +       struct gpio_regmap_config config = {0};
>
> I don't see a point in assigning these variables.

Hi,
The NULL one isn't required, but the regmap one is, otherwise the
devm_gpio_regmap_register call will
return -EINVAL.

It will fail here:
https://elixir.bootlin.com/linux/latest/source/drivers/gpio/gpio-regmap.c#L209

I see that Michael Walle who did the GPIO regmap uses it as well in
the Kontron CPLD driver.
Michael, can you shed some light on this?

Regards,
Robert
>
> > +       struct regmap *regmap;
> > +       u32 base;
> > +       int ret;
> > +
> > +       if (!pdev->dev.parent)
> > +               return -ENODEV;
> > +
> > +       gpio_config = device_get_match_data(&pdev->dev);
> > +       if (!gpio_config)
> > +               return -ENODEV;
> > +
> > +       ret = device_property_read_u32(&pdev->dev, "reg", &base);
> > +       if (ret)
> > +               return ret;
> > +
> > +       regmap = dev_get_regmap(pdev->dev.parent, NULL);
> > +       if (!regmap)
> > +               return -ENODEV;
>
> > +}
>
> --
> With Best Regards,
> Andy Shevchenko
Michael Walle Nov. 5, 2021, 11:19 p.m. UTC | #3
Am 2021-11-05 12:34, schrieb Robert Marko:
> On Tue, Nov 2, 2021 at 8:28 PM Andy Shevchenko
> <andy.shevchenko@gmail.com> wrote:
>> 
>> On Tue, Nov 2, 2021 at 6:57 PM Robert Marko <robert.marko@sartura.hr> 
>> wrote:
>> >
>> > Delta TN48M switch has an onboard Lattice CPLD that is used as a GPIO
>> > expander.
>> >
>> > The CPLD provides 12 pins in total on the TN48M, but on more advanced
>> > switch models it provides up to 192 pins, so the driver is extendable
>> > to support more switches.
>> 
>> ...
>> 
>> > +static int tn48m_gpio_probe(struct platform_device *pdev)
>> > +{
>> 
>> > +       const struct tn48m_gpio_config *gpio_config = NULL;
>> > +       struct gpio_regmap_config config = {0};
>> 
>> I don't see a point in assigning these variables.
> 
> Hi,
> The NULL one isn't required, but the regmap one is, otherwise the
> devm_gpio_regmap_register call will
> return -EINVAL.
> 
> It will fail here:
> https://elixir.bootlin.com/linux/latest/source/drivers/gpio/gpio-regmap.c#L209
> 
> I see that Michael Walle who did the GPIO regmap uses it as well in
> the Kontron CPLD driver.
> Michael, can you shed some light on this?

I guess this was already clarified? Because you don't set
all the properties of the config you have to initialize it.
Otherwise anything could happen.

-michael
Robert Marko Nov. 9, 2021, 11:17 a.m. UTC | #4
On Sat, Nov 6, 2021 at 12:19 AM Michael Walle <michael@walle.cc> wrote:
>
> Am 2021-11-05 12:34, schrieb Robert Marko:
> > On Tue, Nov 2, 2021 at 8:28 PM Andy Shevchenko
> > <andy.shevchenko@gmail.com> wrote:
> >>
> >> On Tue, Nov 2, 2021 at 6:57 PM Robert Marko <robert.marko@sartura.hr>
> >> wrote:
> >> >
> >> > Delta TN48M switch has an onboard Lattice CPLD that is used as a GPIO
> >> > expander.
> >> >
> >> > The CPLD provides 12 pins in total on the TN48M, but on more advanced
> >> > switch models it provides up to 192 pins, so the driver is extendable
> >> > to support more switches.
> >>
> >> ...
> >>
> >> > +static int tn48m_gpio_probe(struct platform_device *pdev)
> >> > +{
> >>
> >> > +       const struct tn48m_gpio_config *gpio_config = NULL;
> >> > +       struct gpio_regmap_config config = {0};
> >>
> >> I don't see a point in assigning these variables.
> >
> > Hi,
> > The NULL one isn't required, but the regmap one is, otherwise the
> > devm_gpio_regmap_register call will
> > return -EINVAL.
> >
> > It will fail here:
> > https://elixir.bootlin.com/linux/latest/source/drivers/gpio/gpio-regmap.c#L209
> >
> > I see that Michael Walle who did the GPIO regmap uses it as well in
> > the Kontron CPLD driver.
> > Michael, can you shed some light on this?
>
> I guess this was already clarified? Because you don't set
> all the properties of the config you have to initialize it.
> Otherwise anything could happen.

Yeah, I think it's clear now.
Thanks,
Robert
>
> -michael
diff mbox series

Patch

diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index fab571016adf..5ccdf0636fad 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -1344,6 +1344,18 @@  config GPIO_TIMBERDALE
 	help
 	Add support for the GPIO IP in the timberdale FPGA.
 
+config GPIO_TN48M_CPLD
+	tristate "Delta Networks TN48M switch CPLD GPIO driver"
+	depends on MFD_TN48M_CPLD
+	select GPIO_REGMAP
+	help
+	  This enables support for the GPIOs found on the Delta
+	  Networks TN48M switch CPLD.
+	  They are used for inputs and outputs on the SFP slots.
+
+	  This driver can also be built as a module. If so, the
+	  module will be called gpio-tn48m.
+
 config GPIO_TPS65086
 	tristate "TI TPS65086 GPO"
 	depends on MFD_TPS65086
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index 32a32659866a..93abc7461e45 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -148,6 +148,7 @@  obj-$(CONFIG_GPIO_TEGRA186)		+= gpio-tegra186.o
 obj-$(CONFIG_GPIO_TEGRA)		+= gpio-tegra.o
 obj-$(CONFIG_GPIO_THUNDERX)		+= gpio-thunderx.o
 obj-$(CONFIG_GPIO_TIMBERDALE)		+= gpio-timberdale.o
+obj-$(CONFIG_GPIO_TN48M_CPLD)		+= gpio-tn48m.o
 obj-$(CONFIG_GPIO_TPIC2810)		+= gpio-tpic2810.o
 obj-$(CONFIG_GPIO_TPS65086)		+= gpio-tps65086.o
 obj-$(CONFIG_GPIO_TPS65218)		+= gpio-tps65218.o
diff --git a/drivers/gpio/gpio-tn48m.c b/drivers/gpio/gpio-tn48m.c
new file mode 100644
index 000000000000..08909555e73d
--- /dev/null
+++ b/drivers/gpio/gpio-tn48m.c
@@ -0,0 +1,100 @@ 
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Delta TN48M CPLD GPIO driver
+ *
+ * Copyright (C) 2021 Sartura Ltd.
+ *
+ * Author: Robert Marko <robert.marko@sartura.hr>
+ */
+
+#include <linux/device.h>
+#include <linux/gpio/driver.h>
+#include <linux/gpio/regmap.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+
+enum tn48m_gpio_type {
+	TN48M_GP0 = 1,
+	TN48M_GPI,
+};
+
+struct tn48m_gpio_config {
+	int ngpio;
+	int ngpio_per_reg;
+	enum tn48m_gpio_type type;
+};
+
+static const struct tn48m_gpio_config tn48m_gpo_config = {
+	.ngpio = 4,
+	.ngpio_per_reg = 4,
+	.type = TN48M_GP0,
+};
+
+static const struct tn48m_gpio_config tn48m_gpi_config = {
+	.ngpio = 4,
+	.ngpio_per_reg = 4,
+	.type = TN48M_GPI,
+};
+
+static int tn48m_gpio_probe(struct platform_device *pdev)
+{
+	const struct tn48m_gpio_config *gpio_config = NULL;
+	struct gpio_regmap_config config = {0};
+	struct regmap *regmap;
+	u32 base;
+	int ret;
+
+	if (!pdev->dev.parent)
+		return -ENODEV;
+
+	gpio_config = device_get_match_data(&pdev->dev);
+	if (!gpio_config)
+		return -ENODEV;
+
+	ret = device_property_read_u32(&pdev->dev, "reg", &base);
+	if (ret)
+		return ret;
+
+	regmap = dev_get_regmap(pdev->dev.parent, NULL);
+	if (!regmap)
+		return -ENODEV;
+
+	config.regmap = regmap;
+	config.parent = &pdev->dev;
+	config.ngpio = gpio_config->ngpio;
+	config.ngpio_per_reg = gpio_config->ngpio_per_reg;
+	switch (gpio_config->type) {
+	case TN48M_GP0:
+		config.reg_set_base = base;
+		break;
+	case TN48M_GPI:
+		config.reg_dat_base = base;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(&pdev->dev, &config));
+}
+
+static const struct of_device_id tn48m_gpio_of_match[] = {
+	{ .compatible = "delta,tn48m-gpo", .data = &tn48m_gpo_config },
+	{ .compatible = "delta,tn48m-gpi", .data = &tn48m_gpi_config },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, tn48m_gpio_of_match);
+
+static struct platform_driver tn48m_gpio_driver = {
+	.driver = {
+		.name = "delta-tn48m-gpio",
+		.of_match_table = tn48m_gpio_of_match,
+	},
+	.probe = tn48m_gpio_probe,
+};
+module_platform_driver(tn48m_gpio_driver);
+
+MODULE_AUTHOR("Robert Marko <robert.marko@sartura.hr>");
+MODULE_DESCRIPTION("Delta TN48M CPLD GPIO driver");
+MODULE_LICENSE("GPL");