Skip to contentSkip to conversation

drivers/gpio: DA9058 GPIO driver

PATCH In review Anthony Olech 2 participants 5 messages Apr 19, 2013 – May 3, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v6 5/7 drivers/gpio: DA9058 GPIO driver

This patch is relative to next-20130419 of linux-next

This is the GPIO component driver of the Dialog DA9058 PMIC. This driver is just one component of the whole DA9058 PMIC driver. It depends on the CORE component driver of the DA9058 MFD. The meaning of the PMIC register 21 bits 1 and 5 has been documented in the driver source.

Changes relative to V5 of this patch:
- rebased to next-20130419 in git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git
- removed redundant #include <linux/mfd/da9058/version.h>
- corrected dates on copyright statements

---
 drivers/gpio/Kconfig       |   12 ++
 drivers/gpio/Makefile      |    1 +
 drivers/gpio/gpio-da9058.c |  376 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 389 insertions(+)
 create mode 100644 drivers/gpio/gpio-da9058.c
drivers/gpio/Kconfig +12 -0
index 6899afb..96b8b78 100644
@@ -103,6 +103,18 @@ config GPIO_DA9055
 
 	  If driver is built as a module it will be called gpio-da9055.
 
+config GPIO_DA9058
+	tristate "Dialog DA9058 GPIO"
+	depends on MFD_DA9058
+	help
+	  Say yes here to enable the GPIO driver for the DA9058 chip.
+
+	  The Dialog DA9058 PMIC chip has 2 GPIO pins that can be
+	  be controller by this driver.
+
+	  If driver is built as a module it will be called da9058-gpio.
+
+
 config GPIO_MAX730X
 	tristate
 
drivers/gpio/Makefile +1 -0
index e005ad5..dc41528 100644
@@ -19,6 +19,7 @@ obj-$(CONFIG_GPIO_ARIZONA)	+= gpio-arizona.o
 obj-$(CONFIG_GPIO_BT8XX)	+= gpio-bt8xx.o
 obj-$(CONFIG_GPIO_CLPS711X)	+= gpio-clps711x.o
 obj-$(CONFIG_GPIO_CS5535)	+= gpio-cs5535.o
+obj-$(CONFIG_GPIO_DA9058)	+= gpio-da9058.o
 obj-$(CONFIG_GPIO_DA9052)	+= gpio-da9052.o
 obj-$(CONFIG_GPIO_DA9055)	+= gpio-da9055.o
 obj-$(CONFIG_ARCH_DAVINCI)	+= gpio-davinci.o
drivers/gpio/gpio-da9058.c new file +376 -0

376 lines not on this page · raw message

Signature · 1 line
end-of-patch for NEW DRIVER V6
#1 lore raw
Quoted 8 lines · Anthony Olech

This patch is relative to next-20130419 of linux-next

This is the GPIO component driver of the Dialog DA9058 PMIC. This driver is just one component of the whole DA9058 PMIC driver. It depends on the CORE component driver of the DA9058 MFD. The meaning of the PMIC register 21 bits 1 and 5 has been documented in the driver source.

Quoted 7 lines

Changes relative to V5 of this patch:
- rebased to next-20130419 in git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git
- removed redundant #include <linux/mfd/da9058/version.h>
- corrected dates on copyright statements

Sorry for slow review ...

drivers/gpio/gpio-da9058.c line 54

Quoted 7 lines
drivers/gpio/gpio-da9058.c new file +7 -0
+struct da9058_gpio {
+       struct da9058 *da9058;
+       struct platform_device *pdev;
+       struct gpio_chip gp;
+       struct mutex lock;
+       int irq_0;
+       int irq_1;

Why are you keeping thes two numbers here? Use the irqdomain to contain irq mappings, this is just confusion things. (See review commens further below.)

drivers/gpio/gpio-da9058.c line 73

Quoted 12 lines
drivers/gpio/gpio-da9058.c new file +11 -0
+       u8 inp_config;
+       u8 out_config;
 
+static int da9058_gpio_get(struct gpio_chip *gc, unsigned offset)
+{
+       struct da9058_gpio *gpio = gpio_chip_to_da9058_gpio(gc);
+       struct da9058 *da9058 = gpio->da9058;
+       unsigned int gpio_level;
+       int ret;
+
+       if (offset > 1)
+               return -EINVAL;

So we have only two pins, OK that's said in Kconfig too...

(...)

drivers/gpio/gpio-da9058.c line 104

Quoted 13 lines
drivers/gpio/gpio-da9058.c new file +13 -0
+static void da9058_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
+{
+       struct da9058_gpio *gpio = gpio_chip_to_da9058_gpio(gc);
+       struct da9058 *da9058 = gpio->da9058;
+       unsigned int gpio_cntrl;
+       int ret;
+
+       if (offset > 1) {
+               dev_err(da9058->dev,
+                       "Failed to set GPIO%d output=%d because illegal GPIO\n",
+                       offset, value);
+               return;
+       }

Here you have an error print, pls insert that on the get function too then.

(...)

Review

Quoted 2 lines
(quoted diff) +2 -0
+       if (offset) {
+

I would put a comment here like
/* pin 1 clause */

Maybe it's more logical to have if (!offset) and handle pin 0 first, then pin 1 in the else clause?

drivers/gpio/gpio-da9058.c line 117

Quoted 8 lines
drivers/gpio/gpio-da9058.c new file +8 -0
+               ret = da9058_reg_read(da9058, DA9058_GPIO0001_REG, &gpio_cntrl);
+               if (ret)
+                       goto exit;
+               gpio->out_config &= ~0x80;
+               gpio->out_config |= value ? 0x80 : 0x00;
+
+               if (!(gpio_cntrl & 0x20))       /* an INP pin */
+                       goto exit;

Please use #define to define these magic bits.
Something like this:

#include <linux/bitops.h>

#define DA9058_GPIO0_VAL BIT(3)
#define DA9058_GPIO0_INOUT BIT(1)
#define DA9058_GPIO1_VAL BIT(7)
#define DA9058_GPIO1_INOUT BIT(5)

(etc, so we see what all bits are for)

then use these defines in the code...

drivers/gpio/gpio-da9058.c line 124

Quoted 7 lines
drivers/gpio/gpio-da9058.c new file +7 -0
+
+               gpio_cntrl &= ~0xF0;
+               gpio_cntrl |= 0xF0 & gpio->out_config;
+
+               ret = da9058_reg_write(da9058, DA9058_GPIO0001_REG, gpio_cntrl);
+       } else {
+

I would put a comment here like
/* pin 0 clause */

drivers/gpio/gpio-da9058.c line 132

Quoted 8 lines
drivers/gpio/gpio-da9058.c new file +8 -0
+               ret = da9058_reg_read(da9058, DA9058_GPIO0001_REG, &gpio_cntrl);
+               if (ret)
+                       goto exit;
+               gpio->out_config &= ~0x08;
+               gpio->out_config |= value ? 0x08 : 0x00;
+
+               if (!(gpio_cntrl & 0x02))       /* an INP pin */
+                       goto exit;

Magic bits, see above.

(...)

Quoted 9 lines
drivers/gpio/gpio-da9058.c new file +9 -0
+/*
+ *  da9058_gpio_to_irq is an implementation of the GPIO Hook
+ *  @to_irq: supporting non-static gpio_to_irq() mappings
+ *  whose implementation may not sleep. This hook is called
+ *  when setting up the threaded GPIO irq handler.
+ */
+static int da9058_gpio_to_irq(struct gpio_chip *gc, u32 offset)
+{
+       struct da9058_gpio *gpio = gpio_chip_to_da9058_gpio(gc);

drivers/gpio/gpio-da9058.c line 211

Quoted 10 lines
drivers/gpio/gpio-da9058.c new file +10 -0
+       struct da9058 *da9058 = gpio->da9058;
+
+       if (offset > 1)
+               return -EINVAL;
+
+       if (offset)
+               return da9058_to_virt_irq_num(da9058, gpio->irq_1);
+       else
+               return da9058_to_virt_irq_num(da9058, gpio->irq_0);
+}

OK, if the DA9058 core is using irqdomain.

drivers/gpio/gpio-da9058.c line 227

Quoted 2 lines
drivers/gpio/gpio-da9058.c new file +2 -0
+       if (offset) {
+               u8 debounce_bits = debounce ? 0x80 : 0x00;

Is this really correct??

In earlier code you use bit 7 to set the value!

This is partly why I ask you to use #defines for the bits:
less risk to do things wrong by e.g. copy/paste bugs.

drivers/gpio/gpio-da9058.c line 249

Quoted 2 lines
drivers/gpio/gpio-da9058.c new file +2 -0
+               gpio->inp_config &= ~0x08;
+               gpio->inp_config |= debounce_bits;

Dito.

(...)

Quoted 1 line

+static int da9058_gpio_probe(struct platform_device *pdev)

(...)

drivers/gpio/gpio-da9058.c line 328

Quoted 12 lines
drivers/gpio/gpio-da9058.c new file +12 -0
+       /*
+        * INP configured pins:
+        *     9 == DebounceOn+ActiceLowPullUp
+        *
+        * OUT configured pins:
+        *     7 == PushPull+ExternalPullUpToVDDIO
+        *     3 == PushPull+InternalPullUpToVDDIO
+        *     2 == OpenDrain+InternalPullUpToVDDIO
+        */
+
+       gpio->inp_config = 0x99;
+       gpio->out_config = 0x77;

Again here you should #define the bits and | or them together instead of using comments to clarify it.

This is pinctrl basically but since you seem to hardcode it it can be in the GPIO subsystem anyway.

Yours,
Linus Walleij

#2 lore raw
Quoted 35 lines

-----Original Message-----
From: Linus Walleij [mailto:[email protected]]
Sent: 24 April 2013 14:15
To: Opensource [Anthony Olech]
Cc: Grant Likely; Linus Walleij; Mark Brown; LKML; David Dajun Chen; Lee Jones
Subject: Re: [NEW DRIVER V6 5/7] drivers/gpio: DA9058 GPIO driver

Quoted 8 lines · Anthony Olech

This patch is relative to next-20130419 of linux-next

This is the GPIO component driver of the Dialog DA9058 PMIC. This driver is just one component of the whole DA9058 PMIC driver. It depends on the CORE component driver of the DA9058 MFD. The meaning of the PMIC register 21 bits 1 and 5 has been documented in the driver source.

Quoted 8 lines

Changes relative to V5 of this patch:
- rebased to next-20130419 in
git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git
- removed redundant #include <linux/mfd/da9058/version.h>
- corrected dates on copyright statements

Sorry for slow review ...

drivers/gpio/gpio-da9058.c line 54

Quoted 7 lines
drivers/gpio/gpio-da9058.c new file +7 -0
+struct da9058_gpio {
+       struct da9058 *da9058;
+       struct platform_device *pdev;
+       struct gpio_chip gp;
+       struct mutex lock;
+       int irq_0;
+       int irq_1;
Quoted 2 lines

Why are you keeping thes two numbers here? Use the irqdomain to contain irq mappings,

this module does not use the irqdomain directly so it gets the numbers from the platform data, which I thought did not persist past the probe() function. Is the platform data permanently available? - if that is the case, I can of course re-get the values on demand from the platform data - please confirm or suggest an alternative method of extracting the irq number.

Quoted 17 lines

this is just confusion things. (See review commens further below.)

drivers/gpio/gpio-da9058.c line 73

Quoted 11 lines
drivers/gpio/gpio-da9058.c new file +10 -0
+       u8 inp_config;
+       u8 out_config;
 
+static int da9058_gpio_get(struct gpio_chip *gc, unsigned offset) {
+       struct da9058_gpio *gpio = gpio_chip_to_da9058_gpio(gc);
+       struct da9058 *da9058 = gpio->da9058;
+       unsigned int gpio_level;
+       int ret;
+
+       if (offset > 1)
+               return -EINVAL;

So we have only two pins, OK that's said in Kconfig too...

(...)

Quoted 15 lines

drivers/gpio/gpio-da9058.c line 104

Quoted 13 lines
drivers/gpio/gpio-da9058.c new file +13 -0
+static void da9058_gpio_set(struct gpio_chip *gc, unsigned offset,
+int value) {
+       struct da9058_gpio *gpio = gpio_chip_to_da9058_gpio(gc);
+       struct da9058 *da9058 = gpio->da9058;
+       unsigned int gpio_cntrl;
+       int ret;
+
+       if (offset > 1) {
+               dev_err(da9058->dev,
+                       "Failed to set GPIO%d output=%d because illegal GPIO\n",
+                       offset, value);
+               return;
+       }

Here you have an error print, pls insert that on the get function too then.

the error print was used here because there is no other way to report an error. On the get function a negative number can be returned, so therefore there is no need to report it via the error log. I can put more diagnostics back in, but is it really necessary to duplicate the error indication in some cases?

Quoted 6 lines

(...)

Review

Quoted 2 lines
(quoted diff) +2 -0
+       if (offset) {
+

I would put a comment here like
/* pin 1 clause */

yes I will add such comments

Quoted 2 lines

Maybe it's more logical to have if (!offset) and handle pin 0 first, then pin 1 in the else clause?

why is it logical for one state to be preferred over the other?

Quoted 15 lines
Quoted 1 line

+ ret = da9058_reg_read(da9058, DA9058_GPIO0001_REG,

&gpio_cntrl);

drivers/gpio/gpio-da9058.c line 117

Quoted 7 lines
drivers/gpio/gpio-da9058.c new file +7 -0
+               if (ret)
+                       goto exit;
+               gpio->out_config &= ~0x80;
+               gpio->out_config |= value ? 0x80 : 0x00;
+
+               if (!(gpio_cntrl & 0x20))       /* an INP pin */
+                       goto exit;

Please use #define to define these magic bits.
Something like this:

#include <linux/bitops.h>

Quoted 8 lines

#define DA9058_GPIO0_VAL BIT(3)
#define DA9058_GPIO0_INOUT BIT(1)
#define DA9058_GPIO1_VAL BIT(7)
#define DA9058_GPIO1_INOUT BIT(5)

(etc, so we see what all bits are for)

then use these defines in the code...

the meaning of the bits other than the INP/OUT bits are not independent, but depend on the direction. Thus each nibble contains 3 dependent bits.

Quoted 7 lines

drivers/gpio/gpio-da9058.c

Quoted 6 lines
drivers/gpio/gpio-da9058.c new file +5 -0
+
+               gpio_cntrl &= ~0xF0;
+               gpio_cntrl |= 0xF0 & gpio->out_config;
+
+               ret = da9058_reg_write(da9058, DA9058_GPIO0001_REG,

gpio_cntrl);

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+       } else {
+

I would put a comment here like
/* pin 0 clause */

yes I will add such comments

Quoted 11 lines
Quoted 1 line

+ ret = da9058_reg_read(da9058, DA9058_GPIO0001_REG,

&gpio_cntrl);

drivers/gpio/gpio-da9058.c line 132

Quoted 7 lines
drivers/gpio/gpio-da9058.c new file +7 -0
+               if (ret)
+                       goto exit;
+               gpio->out_config &= ~0x08;
+               gpio->out_config |= value ? 0x08 : 0x00;
+
+               if (!(gpio_cntrl & 0x02))       /* an INP pin */
+                       goto exit;

Magic bits, see above.

see end of e-mail

Quoted 12 lines

(...)

Quoted 11 lines
drivers/gpio/gpio-da9058.c new file +11 -0
+/*
+ *  da9058_gpio_to_irq is an implementation of the GPIO Hook
+ *  @to_irq: supporting non-static gpio_to_irq() mappings
+ *  whose implementation may not sleep. This hook is called
+ *  when setting up the threaded GPIO irq handler.
+ */
+static int da9058_gpio_to_irq(struct gpio_chip *gc, u32 offset) {
+       struct da9058_gpio *gpio = gpio_chip_to_da9058_gpio(gc);
+       struct da9058 *da9058 = gpio->da9058;
+
+       if (offset > 1)
Quoted 8 lines

drivers/gpio/gpio-da9058.c

Quoted 6 lines
drivers/gpio/gpio-da9058.c new file +6 -0
+               return -EINVAL;
+
+       if (offset)
+               return da9058_to_virt_irq_num(da9058, gpio->irq_1);
+       else
+               return da9058_to_virt_irq_num(da9058, gpio->irq_0); }

OK, if the DA9058 core is using irqdomain.

Yes the core is using irqdomain.

Quoted 6 lines

drivers/gpio/gpio-da9058.c line 227

Quoted 2 lines
drivers/gpio/gpio-da9058.c new file +2 -0
+       if (offset) {
+               u8 debounce_bits = debounce ? 0x80 : 0x00;

Is this really correct??

In earlier code you use bit 7 to set the value!

You are confusing input and output operations, the meanings of each nibble's other 3 bits is depends on the direction. That is why the INP and OUT configuration needs to be saved in a control structure.

Quoted 2 lines

This is partly why I ask you to use #defines for the bits:
less risk to do things wrong by e.g. copy/paste bugs.

The hardware definition is in terms of bit patterns in each nibble, so introducing a dual name for 3 of the 4 bits means double the number of points an error can be made.

Quoted 21 lines

drivers/gpio/gpio-da9058.c line 249

Quoted 2 lines
drivers/gpio/gpio-da9058.c new file +2 -0
+               gpio->inp_config &= ~0x08;
+               gpio->inp_config |= debounce_bits;

Dito.

(...)

Quoted 1 line

+static int da9058_gpio_probe(struct platform_device *pdev)

(...)

drivers/gpio/gpio-da9058.c line 328

Quoted 12 lines
drivers/gpio/gpio-da9058.c new file +12 -0
+       /*
+        * INP configured pins:
+        *     9 == DebounceOn+ActiceLowPullUp
+        *
+        * OUT configured pins:
+        *     7 == PushPull+ExternalPullUpToVDDIO
+        *     3 == PushPull+InternalPullUpToVDDIO
+        *     2 == OpenDrain+InternalPullUpToVDDIO
+        */
+
+       gpio->inp_config = 0x99;
+       gpio->out_config = 0x77;
Quoted 2 lines

Again here you should #define the bits and | or them together instead of using comments to clarify it.

I think that in this particular case it will be more confusing trying to name the bits due to the fact that 3 out of 4 bit in each nibble depend on the 4th bit.

Quoted 5 lines

This is pinctrl basically but since you seem to hardcode it it can be in the GPIO subsystem anyway.

Yours,
Linus Walleij

Hi Linus,

I failed previously to imagine any naming scheme that would make the meaning of the magic bits in each nibble clearer. The best I could come up with was the comment you referred to.

Tony Olech

#3 lore raw
Quoted 10 lines · Opensource [Anthony Olech]

[Me]

Quoted 8 lines
Quoted 7 lines
drivers/gpio/gpio-da9058.c new file +7 -0
+struct da9058_gpio {
+       struct da9058 *da9058;
+       struct platform_device *pdev;
+       struct gpio_chip gp;
+       struct mutex lock;
+       int irq_0;
+       int irq_1;
Quoted 4 lines
Quoted 2 lines

Why are you keeping thes two numbers here? Use the irqdomain to contain irq mappings,

this module does not use the irqdomain directly

This does not compute.

Quoted 5 lines

so it gets the numbers from
the platform data, which I thought did not persist past the probe() function. Is the platform data permanently available? - if that is the case, I can of course re-get the values on demand from the platform data - please confirm or suggest an alternative method of extracting the irq number.

It is indeed usual practice to cache such things from the platform data in the local state container, do not worry about that.

I am more after where the IRQs are tracked, in an irqdomain or as some variables somewhere.

I'm OK with this now, I think...

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
drivers/gpio/gpio-da9058.c new file +6 -0
+       if (offset > 1) {
+               dev_err(da9058->dev,
+                       "Failed to set GPIO%d output=%d because illegal GPIO\n",
+                       offset, value);
+               return;
+       }
Quoted 6 lines
Quoted 1 line

Here you have an error print, pls insert that on the get function too then.

the error print was used here because there is no other way to report an error. On the get function a negative number can be returned, so therefore there is no need to report it via the error log. I can put more diagnostics back in, but is it really necessary to duplicate the error indication in some cases?

No not necessary, OK I get it...

Quoted 4 lines
Quoted 2 lines

Maybe it's more logical to have if (!offset) and handle pin 0 first, then pin 1 in the else clause?

why is it logical for one state to be preferred over the other?

Because 0 comes before 1 in the system of numbers?

Quoted 16 lines
Quoted 5 lines

Please use #define to define these magic bits.
Something like this:

#include <linux/bitops.h>

Quoted 8 lines

#define DA9058_GPIO0_VAL BIT(3)
#define DA9058_GPIO0_INOUT BIT(1)
#define DA9058_GPIO1_VAL BIT(7)
#define DA9058_GPIO1_INOUT BIT(5)

(etc, so we see what all bits are for)

then use these defines in the code...

the meaning of the bits other than the INP/OUT bits are not independent, but depend on the direction. Thus each nibble contains 3 dependent bits.

(...)

Quoted 18 lines
Quoted 6 lines

drivers/gpio/gpio-da9058.c line 227

Quoted 2 lines
drivers/gpio/gpio-da9058.c new file +2 -0
+       if (offset) {
+               u8 debounce_bits = debounce ? 0x80 : 0x00;

Is this really correct??

In earlier code you use bit 7 to set the value!

You are confusing input and output operations, the meanings of each nibble's other 3 bits is depends on the direction. That is why the INP and OUT configuration needs to be saved in a control structure.

Quoted 2 lines

This is partly why I ask you to use #defines for the bits:
less risk to do things wrong by e.g. copy/paste bugs.

The hardware definition is in terms of bit patterns in each nibble, so introducing a dual name for 3 of the 4 bits means double the number of points an error can be made.

(...)

Quoted 8 lines
Quoted 5 lines

drivers/gpio/gpio-da9058.c line 328

Quoted 2 lines
drivers/gpio/gpio-da9058.c new file +2 -0
+       gpio->inp_config = 0x99;
+       gpio->out_config = 0x77;

Again here you should #define the bits and | or them together instead of using comments to clarify it.

I think that in this particular case it will be more confusing trying to name the bits due to the fact that 3 out of 4 bit in each nibble depend on the 4th bit.

(...)

Quoted 3 lines

I failed previously to imagine any naming scheme that would make the meaning of the magic bits in each nibble clearer. The best I could come up with was the comment you referred to.

So add #defines for all combinations, IN and OUT directions.

Also write a comment explaining that the meaning of some bits change depending on how other bits are set.

#include <linux/bitops.h>

/* This bit in each nybble detemines if the pin is input or output */ #define DA9058_GPIO0_IN 0
#define DA9058_GPIO1_IN 0
#define DA9058_GPIO0_OUT BIT(1)
#define DA9058_GPIO1_OUT BIT(5)
/* This is the meaning of bits 0, 2, 3 in the input mode */ #define DA9058_GPIO0_IN_PU BIT(0)
#define DA9058_GPIO0_IN_PU BIT(4)
#define DA9058_GPIO0_IN_DEB BIT(3)
#define DA9058_GPIO1_IN_DEB BIT(7)
(what is bit 2 in input mode?)
(...)
/* This is the meaning of bits 0, 2, 3 in the output mode */ #define DA9058_GPIO0_OUT_VAL BIT(3)
#define DA9058_GPIO0_OUT_PP_EXT BIT(2) | BIT(0)
#define DA9058_GPIO0_OUT_PP_INT BIT(0)
#define DA9058_GPIO0_OUT_OD 0
#define DA9058_GPIO1_OUT_VAL BIT(7)
#define DA9058_GPIO1_OUT_PP_EXT BIT(6) | BIT(4)
#define DA9058_GPIO1_OUT_PP_INT BIT(4)
#define DA9058_GPIO1_OUT_OD 0
(...)

Then the init in probe() becomes readable:

gpio->inp_config = DA9058_GPIO0_IN |

     DA9058_GPIO0_IN_PU |
     DA9058_GPIO0_IN_DEB |
     DA9058_GPIO1_IN |
     DA9058_GPIO0_IN_PU |
     DA9058_GPIO0_IN_DEB;
gpio->out_config = DA9058_GPIO0_OUT |
     DA9058_GPIO0_OUT_PP_EXT |
     DA9058_GPIO1_OUT |
     DA9058_GPIO1_OUT_PP_EXT;

This is way more readable than:

gpio->inp_config = 0x99;
gpio->out_config = 0x77;

After this though, I start to wonder if it's not smarter to just have:

struct da9058_gpio {
    (...)
    u8 gpio0_in_config:4;
    u8 gpio0_out_config:4;
    u8 gpio1_in_config:4;
    u8 gpio1_out_config:4;
};

Then only define one set of bits for a single nybble and use some <<4 to | together the apropriate config at runtime.

Yours,
Linus Walleij

#4 lore raw
Quoted 9 lines

-----Original Message-----
From: Linus Walleij [mailto:[email protected]]
Sent: 03 May 2013 12:59
To: Opensource [Anthony Olech]
Cc: Grant Likely; Linus Walleij; Mark Brown; LKML; Lee Jones
Subject: Re: [NEW DRIVER V6 5/7] drivers/gpio: DA9058 GPIO driver

On Tue, Apr 30, 2013 at 6:17 PM, Opensource [Anthony Olech] <[email protected]> wrote:

[...]

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Maybe it's more logical to have if (!offset) and handle pin 0 first, then pin 1 in the else clause?

why is it logical for one state to be preferred over the other?

Because 0 comes before 1 in the system of numbers?

OK, good enough reason - I will change for next submission.

Quoted 106 lines
Quoted 10 lines
Quoted 10 lines

Please use #define to define these magic bits.
Something like this:

#include <linux/bitops.h>

#define DA9058_GPIO0_VAL BIT(3)
#define DA9058_GPIO0_INOUT BIT(1)
#define DA9058_GPIO1_VAL BIT(7)
#define DA9058_GPIO1_INOUT BIT(5)

Quoted 6 lines
Quoted 3 lines

(etc, so we see what all bits are for)

then use these defines in the code...

the meaning of the bits other than the INP/OUT bits are not independent, but depend on the direction. Thus each nibble contains 3

dependent bits.

(...)

Quoted 17 lines
Quoted 6 lines

drivers/gpio/gpio-da9058.c line 227

Quoted 2 lines
drivers/gpio/gpio-da9058.c new file +2 -0
+       if (offset) {
+               u8 debounce_bits = debounce ? 0x80 : 0x00;

Is this really correct??

In earlier code you use bit 7 to set the value!

You are confusing input and output operations, the meanings of each nibble's other 3 bits is depends on the direction. That is why the INP and OUT configuration needs to be saved in a control structure.

Quoted 2 lines

This is partly why I ask you to use #defines for the bits:
less risk to do things wrong by e.g. copy/paste bugs.

The hardware definition is in terms of bit patterns in each nibble, so introducing a dual name for 3 of the 4 bits means double the number of points an error can be made.

(...)

Quoted 8 lines
Quoted 5 lines

drivers/gpio/gpio-da9058.c line 328

Quoted 2 lines
drivers/gpio/gpio-da9058.c new file +2 -0
+       gpio->inp_config = 0x99;
+       gpio->out_config = 0x77;

Again here you should #define the bits and | or them together instead of using comments to clarify it.

I think that in this particular case it will be more confusing trying to name the bits due to the fact that 3 out of 4 bit in each nibble depend on

the 4th bit.

(...)

Quoted 3 lines

I failed previously to imagine any naming scheme that would make the meaning of the magic bits in each nibble clearer. The best I could come up with was the comment you referred to.

So add #defines for all combinations, IN and OUT directions.

Also write a comment explaining that the meaning of some bits change depending on how other bits are set.

#include <linux/bitops.h>

/* This bit in each nybble detemines if the pin is input or output */ #define DA9058_GPIO0_IN 0 #define DA9058_GPIO1_IN 0 #define DA9058_GPIO0_OUT BIT(1) #define DA9058_GPIO1_OUT BIT(5)
/* This is the meaning of bits 0, 2, 3 in the input mode */ #define DA9058_GPIO0_IN_PU BIT(0) #define DA9058_GPIO0_IN_PU BIT(4) #define DA9058_GPIO0_IN_DEB BIT(3) #define DA9058_GPIO1_IN_DEB BIT(7) (what is bit 2 in input mode?)
(...)
/* This is the meaning of bits 0, 2, 3 in the output mode */ #define DA9058_GPIO0_OUT_VAL BIT(3) #define DA9058_GPIO0_OUT_PP_EXT BIT(2) | BIT(0) #define DA9058_GPIO0_OUT_PP_INT BIT(0) #define DA9058_GPIO0_OUT_OD 0 #define DA9058_GPIO1_OUT_VAL BIT(7) #define DA9058_GPIO1_OUT_PP_EXT BIT(6) | BIT(4) #define DA9058_GPIO1_OUT_PP_INT BIT(4) #define DA9058_GPIO1_OUT_OD 0 (...)

Then the init in probe() becomes readable:

gpio->inp_config = DA9058_GPIO0_IN |

     DA9058_GPIO0_IN_PU |
     DA9058_GPIO0_IN_DEB |
     DA9058_GPIO1_IN |
     DA9058_GPIO0_IN_PU |
     DA9058_GPIO0_IN_DEB;
gpio->out_config = DA9058_GPIO0_OUT |
     DA9058_GPIO0_OUT_PP_EXT |
     DA9058_GPIO1_OUT |
     DA9058_GPIO1_OUT_PP_EXT;

This is way more readable than:

gpio->inp_config = 0x99;
gpio->out_config = 0x77;

After this though, I start to wonder if it's not smarter to just have:

struct da9058_gpio {
    (...)
    u8 gpio0_in_config:4;
    u8 gpio0_out_config:4;
    u8 gpio1_in_config:4;
    u8 gpio1_out_config:4;
};
Quoted 2 lines

Then only define one set of bits for a single nybble and use some <<4 to | together the apropriate config at runtime.

Thanks for this comment - bit fields look like the best way of managing the bits. I will change the source for the next submission attempt.

Many thanks for taking the time to review this driver.

Tony Olech
Dialog Semiconductor