2020-06-30 16:09:14

by Sungbo Eo

[permalink] [raw]
Subject: [PATCH v3 1/2] gpio: add GPO driver for PCA9570

NXP PCA9570 is 4-bit I2C GPO expander without interrupt functionality.
Its ports are controlled only by a data byte without register address.

As there is no other driver similar enough to be adapted for it, a new
driver is introduced here.

Signed-off-by: Sungbo Eo <[email protected]>
---
v3:
* remove mutex
* rename buffer to out
* simplify return statements
* replace ->probe() to ->probe_new()
* move ngpio to driver_data
(PCA9571 is 8-bit so I thought making ngpio configurable is a good idea)

v2:
* move the direction functions below the set functions
* use devm_gpiochip_add_data() and remove the remove callback

v1:
Tested in kernel 5.4 on an ipq40xx platform.

This is my first time submitting a whole driver patch, and I'm not really
familiar with this PCA expander series.
Please let me know how I can improve this patch further.

FYI there's an unmerged patch for this chip.
http://driverdev.linuxdriverproject.org/pipermail/driverdev-devel/2017-May/105602.html
I don't have PCA9571 either so I didn't add support for it.
---
drivers/gpio/Kconfig | 8 +++
drivers/gpio/Makefile | 1 +
drivers/gpio/gpio-pca9570.c | 131 ++++++++++++++++++++++++++++++++++++
3 files changed, 140 insertions(+)
create mode 100644 drivers/gpio/gpio-pca9570.c

diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index c6b5c65c8405..d10dcb81b841 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -962,6 +962,14 @@ config GPIO_PCA953X_IRQ
Say yes here to enable the pca953x to be used as an interrupt
controller. It requires the driver to be built in the kernel.

+config GPIO_PCA9570
+ tristate "PCA9570 4-Bit I2C GPO expander"
+ help
+ Say yes here to enable the GPO driver for the NXP PCA9570 chip.
+
+ To compile this driver as a module, choose M here: the module will
+ be called gpio-pca9570.
+
config GPIO_PCF857X
tristate "PCF857x, PCA{85,96}7x, and MAX732[89] I2C GPIO expanders"
select GPIOLIB_IRQCHIP
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index 1e4894e0bf0f..33cb40c28a61 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -110,6 +110,7 @@ obj-$(CONFIG_GPIO_OCTEON) += gpio-octeon.o
obj-$(CONFIG_GPIO_OMAP) += gpio-omap.o
obj-$(CONFIG_GPIO_PALMAS) += gpio-palmas.o
obj-$(CONFIG_GPIO_PCA953X) += gpio-pca953x.o
+obj-$(CONFIG_GPIO_PCA9570) += gpio-pca9570.o
obj-$(CONFIG_GPIO_PCF857X) += gpio-pcf857x.o
obj-$(CONFIG_GPIO_PCH) += gpio-pch.o
obj-$(CONFIG_GPIO_PCIE_IDIO_24) += gpio-pcie-idio-24.o
diff --git a/drivers/gpio/gpio-pca9570.c b/drivers/gpio/gpio-pca9570.c
new file mode 100644
index 000000000000..3898d1c6f407
--- /dev/null
+++ b/drivers/gpio/gpio-pca9570.c
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Driver for PCA9570 I2C GPO expander
+ *
+ * Copyright (C) 2020 Sungbo Eo <[email protected]>
+ *
+ * Based on gpio-tpic2810.c
+ * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
+ * Andrew F. Davis <[email protected]>
+ */
+
+#include <linux/gpio/driver.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+
+/**
+ * struct pca9570 - GPIO driver data
+ * @chip: GPIO controller chip
+ * @client: I2C device pointer
+ * @out: Buffer for device register
+ */
+struct pca9570 {
+ struct gpio_chip chip;
+ struct i2c_client *client;
+ u8 out;
+};
+
+static void pca9570_set_mask_bits(struct gpio_chip *chip, u8 mask, u8 bits)
+{
+ struct pca9570 *gpio = gpiochip_get_data(chip);
+ u8 buffer;
+ int err;
+
+ buffer = (gpio->out & ~mask) | (bits & mask);
+
+ err = i2c_smbus_write_byte(gpio->client, buffer);
+ if (err)
+ return;
+
+ gpio->out = buffer;
+}
+
+static void pca9570_set(struct gpio_chip *chip, unsigned offset, int value)
+{
+ pca9570_set_mask_bits(chip, BIT(offset), value ? BIT(offset) : 0);
+}
+
+static void pca9570_set_multiple(struct gpio_chip *chip, unsigned long *mask,
+ unsigned long *bits)
+{
+ pca9570_set_mask_bits(chip, *mask, *bits);
+}
+
+static int pca9570_get_direction(struct gpio_chip *chip,
+ unsigned offset)
+{
+ /* This device always output */
+ return GPIO_LINE_DIRECTION_OUT;
+}
+
+static int pca9570_direction_input(struct gpio_chip *chip,
+ unsigned offset)
+{
+ /* This device is output only */
+ return -EINVAL;
+}
+
+static int pca9570_direction_output(struct gpio_chip *chip,
+ unsigned offset, int value)
+{
+ /* This device always output */
+ pca9570_set(chip, offset, value);
+ return 0;
+}
+
+static const struct gpio_chip template_chip = {
+ .label = "pca9570",
+ .owner = THIS_MODULE,
+ .get_direction = pca9570_get_direction,
+ .direction_input = pca9570_direction_input,
+ .direction_output = pca9570_direction_output,
+ .set = pca9570_set,
+ .set_multiple = pca9570_set_multiple,
+ .base = -1,
+ .can_sleep = true,
+};
+
+static const struct i2c_device_id pca9570_id_table[] = {
+ { "pca9570", 4 },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(i2c, pca9570_id_table);
+
+static const struct of_device_id pca9570_of_match_table[] = {
+ { .compatible = "nxp,pca9570" },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, pca9570_of_match_table);
+
+static int pca9570_probe(struct i2c_client *client)
+{
+ struct pca9570 *gpio;
+
+ gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
+ if (!gpio)
+ return -ENOMEM;
+
+ gpio->chip = template_chip;
+ gpio->chip.parent = &client->dev;
+ gpio->chip.ngpio = i2c_match_id(pca9570_id_table, client)->driver_data;
+
+ gpio->client = client;
+
+ i2c_set_clientdata(client, gpio);
+
+ return devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio);
+}
+
+static struct i2c_driver pca9570_driver = {
+ .driver = {
+ .name = "pca9570",
+ .of_match_table = pca9570_of_match_table,
+ },
+ .probe_new = pca9570_probe,
+ .id_table = pca9570_id_table,
+};
+module_i2c_driver(pca9570_driver);
+
+MODULE_AUTHOR("Sungbo Eo <[email protected]>");
+MODULE_DESCRIPTION("GPIO expander driver for PCA9570");
+MODULE_LICENSE("GPL v2");
--
2.27.0


2020-06-30 16:11:53

by Sungbo Eo

[permalink] [raw]
Subject: [PATCH v3 2/2] dt-bindings: gpio: Add bindings for NXP PCA9570

This patch adds device tree bindings for the NXP PCA9570,
a 4-bit I2C GPO expander.

Signed-off-by: Sungbo Eo <[email protected]>
---
v3:
* fixed dt_binding_check error

v2:
I don't feel I can really maintain this driver, but it seems all yaml docs
have a maintainers field so I just added it...
---
.../bindings/gpio/gpio-pca9570.yaml | 47 +++++++++++++++++++
1 file changed, 47 insertions(+)
create mode 100644 Documentation/devicetree/bindings/gpio/gpio-pca9570.yaml

diff --git a/Documentation/devicetree/bindings/gpio/gpio-pca9570.yaml b/Documentation/devicetree/bindings/gpio/gpio-pca9570.yaml
new file mode 100644
index 000000000000..338c5312a106
--- /dev/null
+++ b/Documentation/devicetree/bindings/gpio/gpio-pca9570.yaml
@@ -0,0 +1,47 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/gpio/gpio-pca9570.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: PCA9570 I2C GPO expander
+
+maintainers:
+ - Sungbo Eo <[email protected]>
+
+properties:
+ compatible:
+ enum:
+ - nxp,pca9570
+
+ reg:
+ maxItems: 1
+
+ gpio-controller: true
+
+ '#gpio-cells':
+ const: 2
+
+required:
+ - compatible
+ - reg
+ - gpio-controller
+ - "#gpio-cells"
+
+additionalProperties: false
+
+examples:
+ - |
+ i2c0 {
+ #address-cells = <1>;
+ #size-cells = <0>;
+
+ gpio@24 {
+ compatible = "nxp,pca9570";
+ reg = <0x24>;
+ gpio-controller;
+ #gpio-cells = <2>;
+ };
+ };
+
+...
--
2.27.0

2020-06-30 22:11:30

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v3 1/2] gpio: add GPO driver for PCA9570

On Tue, Jun 30, 2020 at 7:08 PM Sungbo Eo <[email protected]> wrote:
>
> NXP PCA9570 is 4-bit I2C GPO expander without interrupt functionality.
> Its ports are controlled only by a data byte without register address.
>
> As there is no other driver similar enough to be adapted for it, a new
> driver is introduced here.
>

It looks good, but I would add ->get() to return buffered value.

> Signed-off-by: Sungbo Eo <[email protected]>
> ---
> v3:
> * remove mutex
> * rename buffer to out
> * simplify return statements
> * replace ->probe() to ->probe_new()
> * move ngpio to driver_data
> (PCA9571 is 8-bit so I thought making ngpio configurable is a good idea)
>
> v2:
> * move the direction functions below the set functions
> * use devm_gpiochip_add_data() and remove the remove callback
>
> v1:
> Tested in kernel 5.4 on an ipq40xx platform.
>
> This is my first time submitting a whole driver patch, and I'm not really
> familiar with this PCA expander series.
> Please let me know how I can improve this patch further.
>
> FYI there's an unmerged patch for this chip.
> http://driverdev.linuxdriverproject.org/pipermail/driverdev-devel/2017-May/105602.html
> I don't have PCA9571 either so I didn't add support for it.
> ---
> drivers/gpio/Kconfig | 8 +++
> drivers/gpio/Makefile | 1 +
> drivers/gpio/gpio-pca9570.c | 131 ++++++++++++++++++++++++++++++++++++
> 3 files changed, 140 insertions(+)
> create mode 100644 drivers/gpio/gpio-pca9570.c
>
> diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
> index c6b5c65c8405..d10dcb81b841 100644
> --- a/drivers/gpio/Kconfig
> +++ b/drivers/gpio/Kconfig
> @@ -962,6 +962,14 @@ config GPIO_PCA953X_IRQ
> Say yes here to enable the pca953x to be used as an interrupt
> controller. It requires the driver to be built in the kernel.
>
> +config GPIO_PCA9570
> + tristate "PCA9570 4-Bit I2C GPO expander"
> + help
> + Say yes here to enable the GPO driver for the NXP PCA9570 chip.
> +
> + To compile this driver as a module, choose M here: the module will
> + be called gpio-pca9570.
> +
> config GPIO_PCF857X
> tristate "PCF857x, PCA{85,96}7x, and MAX732[89] I2C GPIO expanders"
> select GPIOLIB_IRQCHIP
> diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
> index 1e4894e0bf0f..33cb40c28a61 100644
> --- a/drivers/gpio/Makefile
> +++ b/drivers/gpio/Makefile
> @@ -110,6 +110,7 @@ obj-$(CONFIG_GPIO_OCTEON) += gpio-octeon.o
> obj-$(CONFIG_GPIO_OMAP) += gpio-omap.o
> obj-$(CONFIG_GPIO_PALMAS) += gpio-palmas.o
> obj-$(CONFIG_GPIO_PCA953X) += gpio-pca953x.o
> +obj-$(CONFIG_GPIO_PCA9570) += gpio-pca9570.o
> obj-$(CONFIG_GPIO_PCF857X) += gpio-pcf857x.o
> obj-$(CONFIG_GPIO_PCH) += gpio-pch.o
> obj-$(CONFIG_GPIO_PCIE_IDIO_24) += gpio-pcie-idio-24.o
> diff --git a/drivers/gpio/gpio-pca9570.c b/drivers/gpio/gpio-pca9570.c
> new file mode 100644
> index 000000000000..3898d1c6f407
> --- /dev/null
> +++ b/drivers/gpio/gpio-pca9570.c
> @@ -0,0 +1,131 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Driver for PCA9570 I2C GPO expander
> + *
> + * Copyright (C) 2020 Sungbo Eo <[email protected]>
> + *
> + * Based on gpio-tpic2810.c
> + * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
> + * Andrew F. Davis <[email protected]>
> + */
> +
> +#include <linux/gpio/driver.h>
> +#include <linux/i2c.h>
> +#include <linux/module.h>
> +
> +/**
> + * struct pca9570 - GPIO driver data
> + * @chip: GPIO controller chip
> + * @client: I2C device pointer
> + * @out: Buffer for device register
> + */
> +struct pca9570 {
> + struct gpio_chip chip;
> + struct i2c_client *client;
> + u8 out;
> +};
> +
> +static void pca9570_set_mask_bits(struct gpio_chip *chip, u8 mask, u8 bits)
> +{
> + struct pca9570 *gpio = gpiochip_get_data(chip);
> + u8 buffer;
> + int err;
> +
> + buffer = (gpio->out & ~mask) | (bits & mask);
> +
> + err = i2c_smbus_write_byte(gpio->client, buffer);
> + if (err)
> + return;
> +
> + gpio->out = buffer;
> +}
> +
> +static void pca9570_set(struct gpio_chip *chip, unsigned offset, int value)
> +{
> + pca9570_set_mask_bits(chip, BIT(offset), value ? BIT(offset) : 0);
> +}
> +
> +static void pca9570_set_multiple(struct gpio_chip *chip, unsigned long *mask,
> + unsigned long *bits)
> +{
> + pca9570_set_mask_bits(chip, *mask, *bits);
> +}
> +
> +static int pca9570_get_direction(struct gpio_chip *chip,
> + unsigned offset)
> +{
> + /* This device always output */
> + return GPIO_LINE_DIRECTION_OUT;
> +}
> +
> +static int pca9570_direction_input(struct gpio_chip *chip,
> + unsigned offset)
> +{
> + /* This device is output only */
> + return -EINVAL;
> +}
> +
> +static int pca9570_direction_output(struct gpio_chip *chip,
> + unsigned offset, int value)
> +{
> + /* This device always output */
> + pca9570_set(chip, offset, value);
> + return 0;
> +}
> +
> +static const struct gpio_chip template_chip = {
> + .label = "pca9570",
> + .owner = THIS_MODULE,
> + .get_direction = pca9570_get_direction,
> + .direction_input = pca9570_direction_input,
> + .direction_output = pca9570_direction_output,
> + .set = pca9570_set,
> + .set_multiple = pca9570_set_multiple,
> + .base = -1,
> + .can_sleep = true,
> +};
> +
> +static const struct i2c_device_id pca9570_id_table[] = {
> + { "pca9570", 4 },
> + { /* sentinel */ }
> +};
> +MODULE_DEVICE_TABLE(i2c, pca9570_id_table);
> +
> +static const struct of_device_id pca9570_of_match_table[] = {
> + { .compatible = "nxp,pca9570" },
> + { /* sentinel */ }
> +};
> +MODULE_DEVICE_TABLE(of, pca9570_of_match_table);
> +
> +static int pca9570_probe(struct i2c_client *client)
> +{
> + struct pca9570 *gpio;
> +
> + gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
> + if (!gpio)
> + return -ENOMEM;
> +
> + gpio->chip = template_chip;
> + gpio->chip.parent = &client->dev;
> + gpio->chip.ngpio = i2c_match_id(pca9570_id_table, client)->driver_data;
> +
> + gpio->client = client;
> +
> + i2c_set_clientdata(client, gpio);
> +
> + return devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio);
> +}
> +
> +static struct i2c_driver pca9570_driver = {
> + .driver = {
> + .name = "pca9570",
> + .of_match_table = pca9570_of_match_table,
> + },
> + .probe_new = pca9570_probe,
> + .id_table = pca9570_id_table,
> +};
> +module_i2c_driver(pca9570_driver);
> +
> +MODULE_AUTHOR("Sungbo Eo <[email protected]>");
> +MODULE_DESCRIPTION("GPIO expander driver for PCA9570");
> +MODULE_LICENSE("GPL v2");
> --
> 2.27.0
>


--
With Best Regards,
Andy Shevchenko

2020-07-02 12:19:50

by Sungbo Eo

[permalink] [raw]
Subject: Re: [PATCH v3 1/2] gpio: add GPO driver for PCA9570

On 2020-07-01 06:30, Andy Shevchenko wrote:
> On Tue, Jun 30, 2020 at 7:08 PM Sungbo Eo <[email protected]> wrote:
>>
>> NXP PCA9570 is 4-bit I2C GPO expander without interrupt functionality.
>> Its ports are controlled only by a data byte without register address.
>>
>> As there is no other driver similar enough to be adapted for it, a new
>> driver is introduced here.
>>
>
> It looks good, but I would add ->get() to return buffered value.
>

Thanks, your suggestion was indeed valuable. I reworked the driver,
please have a look.

2020-07-13 19:08:09

by Rob Herring (Arm)

[permalink] [raw]
Subject: Re: [PATCH v3 2/2] dt-bindings: gpio: Add bindings for NXP PCA9570

On Wed, 01 Jul 2020 01:09:34 +0900, Sungbo Eo wrote:
> This patch adds device tree bindings for the NXP PCA9570,
> a 4-bit I2C GPO expander.
>
> Signed-off-by: Sungbo Eo <[email protected]>
> ---
> v3:
> * fixed dt_binding_check error
>
> v2:
> I don't feel I can really maintain this driver, but it seems all yaml docs
> have a maintainers field so I just added it...
> ---
> .../bindings/gpio/gpio-pca9570.yaml | 47 +++++++++++++++++++
> 1 file changed, 47 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/gpio/gpio-pca9570.yaml
>

Reviewed-by: Rob Herring <[email protected]>

2020-07-16 12:38:48

by Linus Walleij

[permalink] [raw]
Subject: Re: [PATCH v3 2/2] dt-bindings: gpio: Add bindings for NXP PCA9570

On Tue, Jun 30, 2020 at 6:10 PM Sungbo Eo <[email protected]> wrote:

> This patch adds device tree bindings for the NXP PCA9570,
> a 4-bit I2C GPO expander.
>
> Signed-off-by: Sungbo Eo <[email protected]>
> ---
> v3:

This v3 patch applied, thanks!

Yours,
Linus Walleij