Skip to contentSkip to conversation

gpio: add GPO driver for PCA9570

PATCH Superseded Sungbo Eo 3 participants 7 messages Jul 5, 2020 – Jul 8, 2020 GPIO

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Sungbo Eo

author
#0 lore raw

PATCH v5 1/2 gpio: add GPO driver for PCA9570

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

Datasheet: https://www.nxp.com/docs/en/data-sheet/PCA9570.pdf

---
v5:
* amended the commit message
* removed unnecessary castings
* added data to of_match_table

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 22 lines · Sungbo Eo

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

Datasheet: https://www.nxp.com/docs/en/data-sheet/PCA9570.pdf

---
v5:
* amended the commit message
* removed unnecessary castings
* added data to of_match_table

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

Quoted 7 lines

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)

This driver looks nice now but why did you remove the mutex in v3? I think when Andy commented on that, he meant not understanding why the error check is protected, not the i2c operations.

Are you sure you don't need this lock?

Bartosz

#2 lore raw

On Mon, Jul 6, 2020 at 2:21 PM Bartosz Golaszewski <[email protected]> wrote:>

Quoted 8 lines · Sungbo Eo
Quoted 7 lines

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

Datasheet: https://www.nxp.com/docs/en/data-sheet/PCA9570.pdf

No blank line here.

Quoted 5 lines
Quoted 1 line

This driver looks nice now but why did you remove the mutex in v3? I think when Andy commented on that, he meant not understanding why the error check is protected, not the i2c operations.

Right.

Quoted 1 line

Are you sure you don't need this lock?

It's a good point!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#3 lore raw
Quoted 13 lines · Andy Shevchenko

On Mon, Jul 6, 2020 at 2:21 PM Bartosz Golaszewski <[email protected]> wrote:>

Quoted 8 lines · Sungbo Eo
Quoted 7 lines

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

Datasheet: https://www.nxp.com/docs/en/data-sheet/PCA9570.pdf

No blank line here.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

This driver looks nice now but why did you remove the mutex in v3? I think when Andy commented on that, he meant not understanding why the error check is protected, not the i2c operations.

Right.

Oh, probably I misunderstood the comment... :(

But I don't really understand what mutex does here. The driver does not need consecutive commands, it only sends/receives only one byte at a time. And AFAIK each i2c_smbus function is already protected by a mutex. So what should be exactly inside the lock? Should we protect the output buffer as well? I'm not an expert on this so please enlighten me.

Thanks for your kind reviews, as always. :)

Quoted 5 lines
Quoted 2 lines

Are you sure you don't need this lock?

It's a good point!

#4 lore raw
Quoted 1 line · Sungbo Eo

On 20. 7. 6. 오후 9:00, Andy Shevchenko wrote:

...

Quoted 5 lines

But I don't really understand what mutex does here. The driver does not need consecutive commands, it only sends/receives only one byte at a time. And AFAIK each i2c_smbus function is already protected by a mutex. So what should be exactly inside the lock? Should we protect the output buffer as well? I'm not an expert on this so please enlighten me.

There are questions, answering them will give you a solution:
- Since we have two functions doing i2c communications, can they clash? If so, does the i2c framework guarantee the serialisation?
- Since we have a shared resource (buf), can accessors clash? How do we guarantee serialization?

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

Thanks, it made me think about it deeper...

Quoted 11 lines · Andy Shevchenko
Quoted 1 line · Sungbo Eo

On 20. 7. 6. 오후 9:00, Andy Shevchenko wrote:

...

Quoted 5 lines

But I don't really understand what mutex does here. The driver does not need consecutive commands, it only sends/receives only one byte at a time. And AFAIK each i2c_smbus function is already protected by a mutex. So what should be exactly inside the lock? Should we protect the output buffer as well? I'm not an expert on this so please enlighten me.

Quoted 3 lines

There are questions, answering them will give you a solution:
- Since we have two functions doing i2c communications, can they clash? If so, does the i2c framework guarantee the serialisation?

I think it does.

Quoted 3 lines

- Since we have a shared resource (buf), can accessors clash? How do we guarantee serialization?

But the output buffer should be tied to the i2c operations. So I guess it requires a mutex here.

pca9570_get() does not access gpio->out so it does not need to be locked.

On the other hand, the whole pca9570_set() function should be protected, from reading gpio->out to rewriting to gpio->out. So pca9570_write() error check should be inside the lock as well. Am I right?

Thanks.

#6 lore raw
In reply to Sungbo Eo
Quoted 3 lines · Sungbo Eo

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

Thank you for an update, my comments (besides what Bart has) below.

Quoted 3 lines

Datasheet: https://www.nxp.com/docs/en/data-sheet/PCA9570.pdf

No blank line in between.

...

drivers/gpio/gpio-pca9570.c line 14

Quoted 3 lines
drivers/gpio/gpio-pca9570.c new file +3 -0
+#include <linux/gpio/driver.h>
+#include <linux/i2c.h>
+#include <linux/module.h>

It also needs property.h.

...

drivers/gpio/gpio-pca9570.c line 24

Quoted 4 lines
drivers/gpio/gpio-pca9570.c new file +3 -0
+struct pca9570 {
+       struct gpio_chip chip;
 
+       struct i2c_client *client;

This basically a duplication of reference to a parent device of GPIO chip. See below.

drivers/gpio/gpio-pca9570.c line 29

Quoted 5 lines
drivers/gpio/gpio-pca9570.c new file +5 -0
+       u8 out;
+};
+
+static int pca9570_read(struct pca9570 *gpio, u8 *value)
+{

struct i2c_client *client = to_i2c_client(gpio->chip.parent);

Quoted 9 lines
drivers/gpio/gpio-pca9570.c new file +9 -0
+       int ret;
+
+       ret = i2c_smbus_read_byte(gpio->client);
+       if (ret < 0)
+               return ret;
+
+       *value = ret;
+       return 0;
+}

...

drivers/gpio/gpio-pca9570.c line 103

Quoted 11 lines
drivers/gpio/gpio-pca9570.c new file +11 -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", .data = (void *)4 },
+       { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, pca9570_of_match_table);

These structures now can be located closer to the end of the driver (exactly before first use).

...

Quoted 1 line

+ gpio->chip = template_chip;

I'm not sure why we need an additional structure to just memcpy() to the chip. But here I have no strong opinion (perhaps the compiler even optimizes this).

Signature · 2 lines
With Best Regards,
Andy Shevchenko