Skip to contentSkip to conversation

gpio: add GPO driver for PCA9570

PATCH Superseded Sungbo Eo 2 participants 5 messages Jul 2, 2020 – Jul 4, 2020 GPIO

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Sungbo Eo

author
#0 lore raw

PATCH v4 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.

---
v4:
* removed ->direction_input() and ->direction_output() (Seems unnecessary to me)
* removed ->set_multiple()
(I'm not sure this implementation is really correct)
* added ->get()
(DS says we can read the status from the device)
* read current status during probe

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 | 138 ++++++++++++++++++++++++++++++++++++
 3 files changed, 147 insertions(+)
 create mode 100644 drivers/gpio/gpio-pca9570.c
drivers/gpio/Kconfig +8 -0
index c6b5c65c8405..d10dcb81b841 100644
@@ -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
drivers/gpio/Makefile +1 -0
index 1e4894e0bf0f..33cb40c28a61 100644
@@ -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
drivers/gpio/gpio-pca9570.c new file +138 -0

138 lines not on this page · raw message

Signature · 1 line
2.27.0
#1 lore raw
Quoted 2 lines · Sungbo Eo

NXP PCA9570 is 4-bit I2C GPO expander without interrupt functionality.

is a 4-bit

Quoted 4 lines

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.

Can we have a Datasheet: tag with URL attached to it?

...

drivers/gpio/gpio-pca9570.c line 36

Quoted 10 lines
drivers/gpio/gpio-pca9570.c new file +9 -0
+static int pca9570_read(struct pca9570 *gpio, u8 *value)
+{
+       s32 ret;
+
+       ret = i2c_smbus_read_byte(gpio->client);
+       if (ret < 0)
+               return ret;
+
 
+       *value = (u8)ret;

I doubt casting is needed.

Quoted 2 lines
(quoted diff) +2 -0
+       return 0;
+}

...

drivers/gpio/gpio-pca9570.c line 100

Quoted 9 lines
drivers/gpio/gpio-pca9570.c new file +8 -0
+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" },

This driver data should also have 4.

Quoted 2 lines
(quoted diff) +2 -0
+       { /* sentinel */ }
+};

...

Quoted 1 line

+ gpio->chip.ngpio = i2c_match_id(pca9570_id_table, client)->driver_data;

Oh, avoid direct access to the table like this. And you may simply use device_get_match_data().
...

drivers/gpio/gpio-pca9570.c line 119

Quoted 2 lines
drivers/gpio/gpio-pca9570.c new file +2 -0
+       /* Read the current output level */
+       (void) pca9570_read(gpio, &gpio->out);

(void) casting is not needed. And I'm not sure hiding an error is a good idea. But the latter is up to you.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 3 lines · Andy Shevchenko
Quoted 1 line · Sungbo Eo

+ s32 ret;

And why not simple int ret; ?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#3 lore raw
In reply to Andy Shevchenko
Quoted 4 lines · Andy Shevchenko
Quoted 1 line

+ gpio->chip.ngpio = i2c_match_id(pca9570_id_table, client)->driver_data;

Oh, avoid direct access to the table like this. And you may simply use device_get_match_data().

I'm not sure if it really does the same thing, but I'll try following your suggestion.

Quoted 8 lines

...

drivers/gpio/gpio-pca9570.c line 119

Quoted 2 lines
drivers/gpio/gpio-pca9570.c new file +2 -0
+       /* Read the current output level */
+       (void) pca9570_read(gpio, &gpio->out);

(void) casting is not needed. And I'm not sure hiding an error is a good idea. But the latter is up to you.

If it returns an error then it might be because the chip could not be detected on the bus at that time. But I think aborting probe for that is too much. (void) casting was to indicate that I want to ignore the error, but I'll remove it as you said.

Thanks.

#4 lore raw
Quoted 8 lines · Sungbo Eo
Quoted 4 lines · Andy Shevchenko
Quoted 1 line

+ gpio->chip.ngpio = i2c_match_id(pca9570_id_table, client)->driver_data;

Oh, avoid direct access to the table like this. And you may simply use device_get_match_data().

I'm not sure if it really does the same thing, but I'll try following your suggestion.

It's an agnostic way to get it from fwnode enumerated devices. Since you are using ->probe_new() the driver should rely on fwnode, that's why the OF table has to have driver data be defined.

Signature · 2 lines
With Best Regards,
Andy Shevchenko