gpio: add GPO driver for PCA9570
PATCH v2 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.
---
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 | 148 ++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 drivers/gpio/gpio-pca9570.c
drivers/gpio/Kconfig +8 -0
@@ -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
@@ -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 +148 -0
148 lines not on this page · raw message
Signature · 1 line
2.27.0
Quoted 6 lines · Sungbo Eo
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.
Thanks for an update. I'll look at them later, so please defer the next version a bit (perhaps for one week).
My comments below.
...
drivers/gpio/gpio-pca9570.c line 40
Quoted 10 lines
drivers/gpio/gpio-pca9570.c new file +9
-0+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; + +mutex_lock (&gpio->lock); + buffer = gpio->buffer & ~mask; + buffer |= (mask & bits);
Usual pattern is to put this on one line
buffer = (gpio->buffer & ~mask) | (bits & mask);
drivers/gpio/gpio-pca9570.c line 44
Quoted 4 lines
drivers/gpio/gpio-pca9570.c new file +3
-0+ err =i2c_smbus_write_byte (gpio->client, buffer); +if (!err) + gpio->buffer = buffer;
I'm not sure I understand why this is under lock.
Review
Quoted 2 lines
(quoted diff) +2
-0+ + mutex_unlock(&gpio->lock);
Can't you simple do it here like
if (err)
return;
... = buffer;
?
Quoted 1 line
+}
...
drivers/gpio/gpio-pca9570.c line 102
Quoted 2 lines
drivers/gpio/gpio-pca9570.c new file +2
-0+static int pca9570_probe (struct i2c_client *client, +const struct i2c_device_id *id)
Can't you use ->probe_new() instead?
drivers/gpio/gpio-pca9570.c line 111
Quoted 10 lines
drivers/gpio/gpio-pca9570.c new file +9
-0+{ +struct pca9570 *gpio; +int ret; + + gpio =devm_kzalloc (&client->dev,sizeof (*gpio),GFP_KERNEL ); +if (!gpio) +return -ENOMEM ; + +i2c_set_clientdata (client, gpio);
Either move this before return 0; or...
drivers/gpio/gpio-pca9570.c line 121
Quoted 10 lines
drivers/gpio/gpio-pca9570.c new file +9
-0+ gpio->chip = template_chip; + gpio->chip.parent = &client->dev; + + gpio->client = client; + +mutex_init (&gpio->lock); + + ret =devm_gpiochip_add_data (&client->dev, &gpio->chip, gpio); +if (ret <0 ) {
(What is the meaning of ' < 0' ?
drivers/gpio/gpio-pca9570.c line 126
Quoted 5 lines
drivers/gpio/gpio-pca9570.c new file +5
-0+dev_err (&client->dev,"Unable to register gpiochip\n" ); +return ret; + } + +return 0 ;
...simple return devm_...(...);
Quoted 1 line
+}
Signature · 2 lines
With Best Regards, Andy Shevchenko
kernel test robot
build botThank you for the patch! Yet something to improve:
Hi Sungbo,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on gpio/for-next]
[also build test ERROR on v5.8-rc2 next-20200625]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Sungbo-Eo/gpio-add-GPO-driver-for-PCA9570/20200625-160356 base: https://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio.git for-next config: mips-allyesconfig (attached as .config) compiler: mips-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=mips
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
>> drivers/gpio/gpio-pca9570.c:141:12: error: 'pca9570_remove' undeclared here (not in a function); did you mean 'pca9570_probe'?
141 | .remove = pca9570_remove,
| ^~~~~~~~~~~~~~
| pca9570_probevim +141 drivers/gpio/gpio-pca9570.c
134
135 static struct i2c_driver pca9570_driver = {
136 .driver = {
137 .name = "pca9570",
138 .of_match_table = pca9570_of_match_table,
139 },
140 .probe = pca9570_probe,
> 141 .remove = pca9570_remove,
142 .id_table = pca9570_id_table,
143 };
144 module_i2c_driver(pca9570_driver);
145
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botThank you for the patch! Yet something to improve:
Hi Sungbo,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on gpio/for-next]
[also build test ERROR on v5.8-rc2 next-20200625]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Sungbo-Eo/gpio-add-GPO-driver-for-PCA9570/20200625-160356 base: https://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio.git for-next config: x86_64-allyesconfig (attached as .config) compiler: clang version 11.0.0 (https://github.com/llvm/llvm-project 8911a35180c6777188fefe0954a2451a2b91deaf)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
>> drivers/gpio/gpio-pca9570.c:141:12: error: use of undeclared identifier 'pca9570_remove'; did you mean 'pca9570_probe'?
.remove = pca9570_remove,
^~~~~~~~~~~~~~
pca9570_probe
drivers/gpio/gpio-pca9570.c:101:12: note: 'pca9570_probe' declared here
static int pca9570_probe(struct i2c_client *client,
^
>> drivers/gpio/gpio-pca9570.c:141:12: error: incompatible function pointer types initializing 'int (*)(struct i2c_client *)' with an expression of type 'int (struct i2c_client *, const struct i2c_device_id *)' [-Werror,-Wincompatible-function-pointer-types]
.remove = pca9570_remove,
^~~~~~~~~~~~~~2 errors generated.
vim +141 drivers/gpio/gpio-pca9570.c
134
135 static struct i2c_driver pca9570_driver = {
136 .driver = {
137 .name = "pca9570",
138 .of_match_table = pca9570_of_match_table,
139 },
140 .probe = pca9570_probe,
> 141 .remove = pca9570_remove,
142 .id_table = pca9570_id_table,
143 };
144 module_i2c_driver(pca9570_driver);
145
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Quoted 8 lines · Sungbo Eo
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]>
Hi Sungbo,
on top of Andy's review, here are some more nits I spotted.
Quoted 75 lines
---
v2:
* move the direction functions below the set functions
* usedevm_gpiochip_add_data()and remove the remove callbackv1:
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 | 148 ++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 drivers/gpio/gpio-pca9570.cdrivers/gpio/Kconfig +8
-0@@ -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_IRQCHIPdrivers/gpio/Makefile +1
-0@@ -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.odrivers/gpio/gpio-pca9570.c new file +17
-0@@ -0,0 +1,148 @@ +// 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> +#include <linux/mutex.h> + +/**
drivers/gpio/gpio-pca9570.c line 27
Quoted 10 lines
drivers/gpio/gpio-pca9570.c new file +10
-0+* struct pca9570 - GPIO driver data +* @chip: GPIO controller chip +* @client: I2C device pointer +* @buffer: Buffer for device register +* @lock: Protects write sequences +*/ +struct pca9570 { +struct gpio_chip chip; +struct i2c_client *client; +u8 buffer;
Could you rename it to reg or something else more obvious? A buffer can be for anything.
Quoted 62 lines
drivers/gpio/gpio-pca9570.c new file +62
-0+struct mutex lock; +}; + +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; + +mutex_lock (&gpio->lock); + + buffer = gpio->buffer & ~mask; + buffer |= (mask & bits); + + err =i2c_smbus_write_byte (gpio->client, buffer); +if (!err) + gpio->buffer = buffer; + +mutex_unlock (&gpio->lock); +} + +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,
drivers/gpio/gpio-pca9570.c line 99
Quoted 10 lines
drivers/gpio/gpio-pca9570.c new file +10
-0+ .base = -1, + .ngpio = 4, + .can_sleep = true, +}; + +static const struct of_device_id pca9570_of_match_table[] = { + { .compatible ="nxp,pca9570" }, + {/* sentinel */ } +}; +MODULE_DEVICE_TABLE (of, pca9570_of_match_table);
If you're not using it in probe than maybe move it next to the I2C device table?
Quoted 13 lines
drivers/gpio/gpio-pca9570.c new file +13
-0+ +static int pca9570_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +struct pca9570 *gpio; +int ret; + + gpio =devm_kzalloc (&client->dev,sizeof (*gpio),GFP_KERNEL ); +if (!gpio) +return -ENOMEM ; + +i2c_set_clientdata (client, gpio); +
drivers/gpio/gpio-pca9570.c line 122
Quoted 10 lines
drivers/gpio/gpio-pca9570.c new file +10
-0+ gpio->chip = template_chip; + gpio->chip.parent = &client->dev; + + gpio->client = client; + +mutex_init (&gpio->lock); + + ret =devm_gpiochip_add_data (&client->dev, &gpio->chip, gpio); +if (ret <0 ) { +dev_err (&client->dev,"Unable to register gpiochip\n" );
You don't need this message, the core library will print something for you. Just do return devm_gpiochip_add_data().
Quoted 17 lines
drivers/gpio/gpio-pca9570.c new file +17
-0+return ret; + } + +return 0 ; +} + +static const struct i2c_device_id pca9570_id_table[] = { + {"pca9570" , }, + {/* sentinel */ } +}; +MODULE_DEVICE_TABLE (i2c, pca9570_id_table); + +static struct i2c_driver pca9570_driver = { + .driver = { + .name ="pca9570" , + .of_match_table = pca9570_of_match_table, + },
drivers/gpio/gpio-pca9570.c
Quoted 10 lines
drivers/gpio/gpio-pca9570.c new file +9
-1+ .probe = pca9570_probe, + .remove = pca9570_remove, + .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" ); --
Bart
Quoted 9 lines · Bartosz Golaszewski
drivers/gpio/gpio-pca9570.c line 99
Quoted 5 lines · Sungbo Eo
drivers/gpio/gpio-pca9570.c new file +5
-0+static const struct of_device_id pca9570_of_match_table[] = { + { .compatible ="nxp,pca9570" }, + {/* sentinel */ } +}; +MODULE_DEVICE_TABLE (of, pca9570_of_match_table);If you're not using it in probe than maybe move it next to the I2C device table?
(Side note)
...and even if so it can be assessed via a struct device pointer:dev->driver->id_table (don't remember by heart, but you have an idea).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Thanks for all the reviews! I've updated the patch, please have a look.
And I have something to ask.
# echo 1 > gpio408/value # cat gpio408/value cat: read error: I/O error # cat gpio408/direction out # echo out > gpio408/direction # echo in > gpio408/direction [ 91.006691] gpio-408 (sysfs): gpiod_direction_input: missing get() but have direction_input() ash: write error: I/O error
I've never dealt with GPO expander before, so this seems a bit odd to me. Is it perfectly okay to leave get() and direction_input() unimplemented?
Thanks.
Quoted 16 lines · Andy Shevchenko
Quoted 9 lines · Bartosz Golaszewski
drivers/gpio/gpio-pca9570.c line 99
Quoted 5 lines · Sungbo Eo
drivers/gpio/gpio-pca9570.c new file +5
-0+static const struct of_device_id pca9570_of_match_table[] = { + { .compatible ="nxp,pca9570" }, + {/* sentinel */ } +}; +MODULE_DEVICE_TABLE (of, pca9570_of_match_table);If you're not using it in probe than maybe move it next to the I2C device table?
(Side note)
...and even if so it can be assessed via a struct device pointer:dev->driver->id_table(don't remember by heart, but you have an idea).
Quoted 16 lines · Sungbo Eo
Thanks for all the reviews! I've updated the patch, please have a look.
And I have something to ask.
# echo 1 > gpio408/value # cat gpio408/value cat: read error: I/O error # cat gpio408/direction out # echo out > gpio408/direction # echo in > gpio408/direction [ 91.006691] gpio-408 (sysfs): gpiod_direction_input: missing get() but have direction_input() ash: write error: I/O error
Quoted 2 lines
I've never dealt with GPO expander before, so this seems a bit odd to me. Is it perfectly okay to leave
get()anddirection_input()unimplemented?
Actually it would be better to return the value you set for output in ->get().
Signature · 2 lines
With Best Regards, Andy Shevchenko