Linux-GPIO Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH v2 1/2] gpio: add GPO driver for PCA9570
@ 2020-06-25  7:58 Sungbo Eo
  2020-06-25  8:59 ` Andy Shevchenko
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Sungbo Eo @ 2020-06-25  7:58 UTC (permalink / raw)
  To: Linus Walleij, Bartosz Golaszewski, linux-kernel, linux-gpio; +Cc: 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 <mans0n@gorani.run>
---
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

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..e6b6c4e791c0
--- /dev/null
+++ b/drivers/gpio/gpio-pca9570.c
@@ -0,0 +1,148 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Driver for PCA9570 I2C GPO expander
+ *
+ * Copyright (C) 2020 Sungbo Eo <mans0n@gorani.run>
+ *
+ * Based on gpio-tpic2810.c
+ * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
+ *	Andrew F. Davis <afd@ti.com>
+ */
+
+#include <linux/gpio/driver.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+
+/**
+ * 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;
+	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,
+	.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);
+
+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);
+
+	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");
+		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,
+	},
+	.probe = pca9570_probe,
+	.remove = pca9570_remove,
+	.id_table = pca9570_id_table,
+};
+module_i2c_driver(pca9570_driver);
+
+MODULE_AUTHOR("Sungbo Eo <mans0n@gorani.run>");
+MODULE_DESCRIPTION("GPIO expander driver for PCA9570");
+MODULE_LICENSE("GPL v2");
-- 
2.27.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/2] gpio: add GPO driver for PCA9570
  2020-06-25  7:58 [PATCH v2 1/2] gpio: add GPO driver for PCA9570 Sungbo Eo
@ 2020-06-25  8:59 ` Andy Shevchenko
  2020-06-25 14:43 ` kernel test robot
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Andy Shevchenko @ 2020-06-25  8:59 UTC (permalink / raw)
  To: Sungbo Eo
  Cc: Linus Walleij, Bartosz Golaszewski, Linux Kernel Mailing List,
	open list:GPIO SUBSYSTEM

On Thu, Jun 25, 2020 at 10:59 AM Sungbo Eo <mans0n@gorani.run> 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.

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.

...

> +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);

> +       err = i2c_smbus_write_byte(gpio->client, buffer);

> +       if (!err)
> +               gpio->buffer = buffer;

I'm not sure I understand why this is under lock.

> +
> +       mutex_unlock(&gpio->lock);

Can't you simple do it here like

if (err)
  return;

... = buffer;

?

> +}

...

> +static int pca9570_probe(struct i2c_client *client,
> +                        const struct i2c_device_id *id)

Can't you use ->probe_new() instead?

> +{
> +       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...

> +       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' ?

> +               dev_err(&client->dev, "Unable to register gpiochip\n");
> +               return ret;
> +       }
> +
> +       return 0;

...simple return devm_...(...);

> +}

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/2] gpio: add GPO driver for PCA9570
  2020-06-25  7:58 [PATCH v2 1/2] gpio: add GPO driver for PCA9570 Sungbo Eo
  2020-06-25  8:59 ` Andy Shevchenko
@ 2020-06-25 14:43 ` kernel test robot
  2020-06-25 16:38 ` kernel test robot
  2020-06-30  9:18 ` Bartosz Golaszewski
  3 siblings, 0 replies; 8+ messages in thread
From: kernel test robot @ 2020-06-25 14:43 UTC (permalink / raw)
  To: Sungbo Eo, Linus Walleij, Bartosz Golaszewski, linux-kernel, linux-gpio
  Cc: kbuild-all, Sungbo Eo


[-- Attachment #1: Type: text/plain, Size: 1801 bytes --]

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
Reported-by: kernel test robot <lkp@intel.com>

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_probe

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@lists.01.org

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 67044 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/2] gpio: add GPO driver for PCA9570
  2020-06-25  7:58 [PATCH v2 1/2] gpio: add GPO driver for PCA9570 Sungbo Eo
  2020-06-25  8:59 ` Andy Shevchenko
  2020-06-25 14:43 ` kernel test robot
@ 2020-06-25 16:38 ` kernel test robot
  2020-06-30  9:18 ` Bartosz Golaszewski
  3 siblings, 0 replies; 8+ messages in thread
From: kernel test robot @ 2020-06-25 16:38 UTC (permalink / raw)
  To: Sungbo Eo, Linus Walleij, Bartosz Golaszewski, linux-kernel, linux-gpio
  Cc: kbuild-all, clang-built-linux, Sungbo Eo


[-- Attachment #1: Type: text/plain, Size: 2480 bytes --]

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
Reported-by: kernel test robot <lkp@intel.com>

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@lists.01.org

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 75307 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/2] gpio: add GPO driver for PCA9570
  2020-06-25  7:58 [PATCH v2 1/2] gpio: add GPO driver for PCA9570 Sungbo Eo
                   ` (2 preceding siblings ...)
  2020-06-25 16:38 ` kernel test robot
@ 2020-06-30  9:18 ` Bartosz Golaszewski
  2020-06-30  9:53   ` Andy Shevchenko
  3 siblings, 1 reply; 8+ messages in thread
From: Bartosz Golaszewski @ 2020-06-30  9:18 UTC (permalink / raw)
  To: Sungbo Eo; +Cc: Linus Walleij, LKML, linux-gpio

On Thu, Jun 25, 2020 at 9:58 AM Sungbo Eo <mans0n@gorani.run> 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.
>
> Signed-off-by: Sungbo Eo <mans0n@gorani.run>

Hi Sungbo,

on top of Andy's review, here are some more nits I spotted.

> ---
> 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
>
> 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..e6b6c4e791c0
> --- /dev/null
> +++ b/drivers/gpio/gpio-pca9570.c
> @@ -0,0 +1,148 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Driver for PCA9570 I2C GPO expander
> + *
> + * Copyright (C) 2020 Sungbo Eo <mans0n@gorani.run>
> + *
> + * Based on gpio-tpic2810.c
> + * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
> + *     Andrew F. Davis <afd@ti.com>
> + */
> +
> +#include <linux/gpio/driver.h>
> +#include <linux/i2c.h>
> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +
> +/**
> + * 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.

> +       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,
> +       .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?

> +
> +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);
> +
> +       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().

> +               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,
> +       },
> +       .probe = pca9570_probe,
> +       .remove = pca9570_remove,
> +       .id_table = pca9570_id_table,
> +};
> +module_i2c_driver(pca9570_driver);
> +
> +MODULE_AUTHOR("Sungbo Eo <mans0n@gorani.run>");
> +MODULE_DESCRIPTION("GPIO expander driver for PCA9570");
> +MODULE_LICENSE("GPL v2");
> --

Bart

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/2] gpio: add GPO driver for PCA9570
  2020-06-30  9:18 ` Bartosz Golaszewski
@ 2020-06-30  9:53   ` Andy Shevchenko
  2020-06-30 16:11     ` Sungbo Eo
  0 siblings, 1 reply; 8+ messages in thread
From: Andy Shevchenko @ 2020-06-30  9:53 UTC (permalink / raw)
  To: Bartosz Golaszewski; +Cc: Sungbo Eo, Linus Walleij, LKML, linux-gpio

On Tue, Jun 30, 2020 at 12:28 PM Bartosz Golaszewski
<bgolaszewski@baylibre.com> wrote:
> On Thu, Jun 25, 2020 at 9:58 AM Sungbo Eo <mans0n@gorani.run> wrote:

> > +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).

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/2] gpio: add GPO driver for PCA9570
  2020-06-30  9:53   ` Andy Shevchenko
@ 2020-06-30 16:11     ` Sungbo Eo
  2020-06-30 21:23       ` Andy Shevchenko
  0 siblings, 1 reply; 8+ messages in thread
From: Sungbo Eo @ 2020-06-30 16:11 UTC (permalink / raw)
  To: Andy Shevchenko, Bartosz Golaszewski; +Cc: Linus Walleij, LKML, linux-gpio

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.

On 2020-06-30 18:53, Andy Shevchenko wrote:
> On Tue, Jun 30, 2020 at 12:28 PM Bartosz Golaszewski
> <bgolaszewski@baylibre.com> wrote:
>> On Thu, Jun 25, 2020 at 9:58 AM Sungbo Eo <mans0n@gorani.run> wrote:
> 
>>> +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).
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/2] gpio: add GPO driver for PCA9570
  2020-06-30 16:11     ` Sungbo Eo
@ 2020-06-30 21:23       ` Andy Shevchenko
  0 siblings, 0 replies; 8+ messages in thread
From: Andy Shevchenko @ 2020-06-30 21:23 UTC (permalink / raw)
  To: Sungbo Eo; +Cc: Bartosz Golaszewski, Linus Walleij, LKML, linux-gpio

On Tue, Jun 30, 2020 at 7:11 PM Sungbo Eo <mans0n@gorani.run> wrote:
>
> 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?

Actually it would be better to return the value you set for output in ->get().


-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, back to index

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-25  7:58 [PATCH v2 1/2] gpio: add GPO driver for PCA9570 Sungbo Eo
2020-06-25  8:59 ` Andy Shevchenko
2020-06-25 14:43 ` kernel test robot
2020-06-25 16:38 ` kernel test robot
2020-06-30  9:18 ` Bartosz Golaszewski
2020-06-30  9:53   ` Andy Shevchenko
2020-06-30 16:11     ` Sungbo Eo
2020-06-30 21:23       ` Andy Shevchenko

Linux-GPIO Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-gpio/0 linux-gpio/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-gpio linux-gpio/ https://lore.kernel.org/linux-gpio \
		linux-gpio@vger.kernel.org
	public-inbox-index linux-gpio

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-gpio


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git