Skip to contentSkip to conversation

DA9058 GPIO driver

PATCH In review Anthony Olech 3 participants 5 messages Aug 6, 2012 – Aug 15, 2012

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

PATCH v2 5/7 DA9058 GPIO driver

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 DA9058 MFD driver.

---
 drivers/gpio/Kconfig       |   12 ++
 drivers/gpio/Makefile      |    1 +
 drivers/gpio/gpio-da9058.c |  376 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 389 insertions(+), 0 deletions(-)
 create mode 100644 drivers/gpio/gpio-da9058.c
drivers/gpio/Kconfig +12 -0
index 542f0c0..63b574a 100644
@@ -86,6 +86,18 @@ config GPIO_DA9052
 	help
 	  Say yes here to enable the GPIO driver for the DA9052 chip.
 
+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 0f55662..209224a 100644
@@ -14,6 +14,7 @@ obj-$(CONFIG_GPIO_ADP5520)	+= gpio-adp5520.o
 obj-$(CONFIG_GPIO_ADP5588)	+= gpio-adp5588.o
 obj-$(CONFIG_GPIO_BT8XX)	+= gpio-bt8xx.o
 obj-$(CONFIG_GPIO_CS5535)	+= gpio-cs5535.o
+obj-$(CONFIG_GPIO_DA9058)	+= gpio-da9058.o
 obj-$(CONFIG_GPIO_DA9052)	+= gpio-da9052.o
 obj-$(CONFIG_ARCH_DAVINCI)	+= gpio-davinci.o
 obj-$(CONFIG_GPIO_EM)		+= gpio-em.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 V2
#1 lore raw

Hi Anthony, sorry for delayed reply...

Quoted 3 lines · Anthony Olech

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 DA9058 MFD driver.

OK

drivers/gpio/Kconfig config GPIO_DA9052 · line 91

Quoted 3 lines
drivers/gpio/Kconfig +3 -0
+config GPIO_DA9058
+       tristate "Dialog DA9058 GPIO"
+       depends on MFD_DA9058

select IRQ_DOMAIN, you're going to want to use it...

Quoted 2 lines
drivers/gpio/Makefile +0 -0
index 0f55662..209224a 100644

(...)

drivers/gpio/Makefile

Quoted 2 lines
drivers/gpio/Makefile +2 -0
+#include <linux/module.h>
+#include <linux/fs.h>

Really?

Quoted 1 line

+#include <linux/uaccess.h>

Really?

drivers/gpio/Makefile

Quoted 3 lines
drivers/gpio/Makefile +3 -0
+#include <linux/platform_device.h>
+#include <linux/gpio.h>
+#include <linux/syscalls.h>

Really?

drivers/gpio/Makefile

Quoted 5 lines
drivers/gpio/Makefile +5 -0
+#include <linux/seq_file.h>
+#include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/mfd/core.h>
+#include <linux/regmap.h>

If you're using regmap you better select it in Kconfig too, but it appears you don't. You should be using regmap in the main MFD driver in this case (I haven't looked at it though.)

This header set just looks like it was copied from some other file and never really proofread, so please go over it in detail.

drivers/gpio/Makefile

Quoted 6 lines
drivers/gpio/Makefile +6 -0
+#include <linux/mfd/da9058/version.h>
+#include <linux/mfd/da9058/registers.h>
+#include <linux/mfd/da9058/core.h>
+#include <linux/mfd/da9058/gpio.h>
+#include <linux/mfd/da9058/irq.h>
+#include <linux/mfd/da9058/pdata.h>

Samuel will have to comment on this organization of headers, it seems a little much. DO you really need all of them?

drivers/gpio/Makefile

Quoted 13 lines
drivers/gpio/Makefile +13 -0
+struct da9058_gpio {
+       struct da9058 *da9058;
+       struct platform_device *pdev;
+       struct gpio_chip gp;
+       struct mutex lock;
+       u8 inp_config;
+       u8 out_config;
+};
+
+static struct da9058_gpio *gpio_chip_to_da9058_gpio(struct gpio_chip *chip)
+{
+       return container_of(chip, struct da9058_gpio, gp);
+}

static inline, or a #define, but the compile will probably optimize-inline it anyway.

drivers/gpio/Makefile

Quoted 9 lines
drivers/gpio/Makefile +9 -0
+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 there are two GPIO pins, 0 and 1? That seems odd, but OK.

Quoted 31 lines
drivers/gpio/Makefile +31 -0
+
+       mutex_lock(&gpio->lock);
+       ret = da9058_reg_read(da9058, DA9058_STATUSC_REG, &gpio_level);
+       mutex_unlock(&gpio->lock);
+       if (ret < 0)
+               return ret;
+
+       if (offset) {
+               if (gpio_level & DA9058_STATUSC_GPI1)
+                       return 1;
+               else
+                       return 0;
+       } else {
+               if (gpio_level & DA9058_STATUSC_GPI0)
+                       return 1;
+               else
+                       return 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);

drivers/gpio/Makefile

Quoted 10 lines
drivers/gpio/Makefile +10 -0
+               return;
+       }
+
+       mutex_lock(&gpio->lock);
+
+       ret = da9058_reg_read(da9058, DA9058_GPIO0001_REG, &gpio_cntrl);
+       if (ret)
+               goto exit;
+
+       if (offset) {

So this is for GPIO 1

Quoted 1 line

+ u8 value_bits = value ? 0x80 : 0x00;

These "value_bits" are just confusing. Just delete this and use the direct value below.

drivers/gpio/Makefile

Quoted 2 lines
drivers/gpio/Makefile +2 -0
+
+               gpio->out_config &= ~0x80;

A better way of writing &= ~0x80; is &= 0x7F

Quoted 1 line

+ gpio->out_config |= value_bits;

gpio->out_config = value ? 0x80 : 0x00;

So, less confusing.

drivers/gpio/Makefile

Quoted 2 lines
drivers/gpio/Makefile +2 -0
+               if (!(gpio_cntrl & 0x20))
+                       goto exit;

Please insert a comment explaining what this bit is doing and why you're just exiting if it's not set. I don't understand one thing.

Maybe this would be better if you didn't use so many magic values, what about:

#include <linux/bitops.h>

#define FOO_FLAG BIT(3) /* This is a flag for foo */

drivers/gpio/Makefile

Quoted 2 lines
drivers/gpio/Makefile +2 -0
+
+               gpio_cntrl &= ~0xF0;

A better way to write &= ~F0 is to write &= 0x0F;

If you don't #define the constants this way of negating numbers just get confusing.

So this is OK:

  foo &= ~FOO_FLAG;
  foo |= set ? FOO_FLAG : 0;

This is just hard to read:

  foo &= ~0x55;
  foo |= set ? 0x55 : 0;

And is better off

   foo &= 0xAA;
   foo |= set ? 0x55 : 0;

I prefer that you #define the registers but it's your pick.

drivers/gpio/Makefile

Quoted 5 lines
drivers/gpio/Makefile +5 -0
+               gpio_cntrl |= 0xF0 & gpio->out_config;
+
+               ret = da9058_reg_write(da9058, DA9058_GPIO0001_REG, gpio_cntrl);
+       } else {
+               u8 value_bits = value ? 0x08 : 0x00;

Same here, delete this.

Quoted 1 line

+ gpio->out_config &= ~0x08;

&= 0xF7;

or use some <linux/bitops.h> and #defines ...

Quoted 1 line

+ gpio->out_config |= value_bits;

Just
gpio->out_config |= value ? 0x08 : 0x00;

drivers/gpio/Makefile

Quoted 2 lines
drivers/gpio/Makefile +2 -0
+               if (!(gpio_cntrl & 0x02))
+                       goto exit;

Same thing, explain that flag.

drivers/gpio/Makefile

Quoted 2 lines
drivers/gpio/Makefile +2 -0
+
+               gpio_cntrl &= ~0x0F;

Just &= 0xF0;

drivers/gpio/Makefile

Quoted 3 lines
drivers/gpio/Makefile +3 -0
+               gpio_cntrl |= 0x0F & gpio->out_config;
+
+               ret = da9058_reg_write(da9058, DA9058_GPIO0001_REG, gpio_cntrl);

Further, if you're checking that flag just in order to avoid doing this write if it's not necessary, it's the wrong solution. The right solution is to implement regmap in the MFD driver so it quickly sees that the right value is already in the register and bounces off.

Quoted 18 lines
drivers/gpio/Makefile +18 -0
+       }
+exit:
+       mutex_unlock(&gpio->lock);
+       if (ret)
+               dev_err(da9058->dev,
+                       "Failed to set GPIO%d output=%d error=%d\n",
+                       offset, value, ret);
+       return;
+}
+
+static int da9058_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
+{
+       struct da9058_gpio *gpio = gpio_chip_to_da9058_gpio(gc);
+       struct da9058 *da9058 = gpio->da9058;
+       unsigned int gpio_cntrl;
+       int ret;
+
+       if (offset > 1)

drivers/gpio/Makefile

Quoted 10 lines
drivers/gpio/Makefile +10 -0
+               return -EINVAL;
+
+       mutex_lock(&gpio->lock);
+
+       ret = da9058_reg_read(da9058, DA9058_GPIO0001_REG, &gpio_cntrl);
+       if (ret)
+               goto exit;
+
+       if (offset) {
+               gpio_cntrl &= ~0xF0;

&= 0x0F; or #define the flag... FOO_MASK.

drivers/gpio/Makefile

Quoted 5 lines
drivers/gpio/Makefile +5 -0
+               gpio_cntrl |= 0xF0 & gpio->inp_config;
+
+               ret = da9058_reg_write(da9058, DA9058_GPIO0001_REG, gpio_cntrl);
+       } else {
+               gpio_cntrl &= ~0x0F;

&= 0xF0;

Quoted 39 lines
drivers/gpio/Makefile +39 -0
+               gpio_cntrl |= 0x0F & gpio->inp_config;
+
+               ret = da9058_reg_write(da9058, DA9058_GPIO0001_REG, gpio_cntrl);
+       }
+exit:
+       mutex_unlock(&gpio->lock);
+       if (ret)
+               dev_err(da9058->dev,
+                       "Failed to set GPIO%d to output error=%d\n",
+                       offset, ret);
+       return ret;
+}
+
+static int da9058_gpio_direction_output(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)
+               return -EINVAL;
+
+       mutex_lock(&gpio->lock);
+
+       ret = da9058_reg_read(da9058, DA9058_GPIO0001_REG, &gpio_cntrl);
+       if (ret)
+               goto exit;
+
+       if (offset) {
+               gpio_cntrl &= ~0xF0;
+               gpio_cntrl |= 0xF0 & gpio->out_config;
+
+               ret = da9058_reg_write(da9058, DA9058_GPIO0001_REG, gpio_cntrl);
+       } else {
+               gpio_cntrl &= ~0x0F;
+               gpio_cntrl |= 0x0F & gpio->out_config;
+

drivers/gpio/Makefile

Quoted 10 lines
drivers/gpio/Makefile +10 -0
+               ret = da9058_reg_write(da9058, DA9058_GPIO0001_REG, gpio_cntrl);
+       }
+exit:
+       mutex_unlock(&gpio->lock);
+       if (ret)
+               dev_err(da9058->dev,
+                       "Failed to set GPIO%d to input error=%d\n",
+                       offset, ret);
+       return ret;
+}

Same comments as above, and you need a blank line here.

Quoted 9 lines
drivers/gpio/Makefile +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/Makefile

Quoted 10 lines
drivers/gpio/Makefile +10 -0
+       struct da9058 *da9058 = gpio->da9058;
+
+       if (offset > 1)
+               return -EINVAL;
+
+       if (offset)
+               return da9058_to_virt_irq_num(da9058, DA9058_IRQ_EGPI1);
+       else
+               return da9058_to_virt_irq_num(da9058, DA9058_IRQ_EGPI0);
+}

Lee Jones and Mark Brown discussed these virtual IRQ mapping functions recently, and I think the outcome was to patch irqdomain to do the work and not sprinkle these custom interfaces to fetch virtual IRQs all over the place.

Quoted 42 lines
drivers/gpio/Makefile +42 -0
+static int da9058_gpio_set_debounce(struct gpio_chip *gc, unsigned offset,
+                               unsigned debounce)
+{
+       struct da9058_gpio *gpio = gpio_chip_to_da9058_gpio(gc);
+       struct da9058 *da9058 = gpio->da9058;
+       int ret;
+       unsigned int gpio_cntrl;
+
+       if (offset > 1)
+               return -EINVAL;
+
+       mutex_lock(&gpio->lock);
+
+       ret = da9058_reg_read(da9058, DA9058_GPIO0001_REG, &gpio_cntrl);
+       if (ret)
+               goto exit;
+
+       if (offset) {
+               u8 debounce_bits = debounce ? 0x80 : 0x00;
+
+               gpio->inp_config &= ~0x80;
+               gpio->inp_config |= debounce_bits;
+
+               if (gpio_cntrl & 0x20)
+                       goto exit;
+
+               gpio_cntrl &= ~0xF0;
+               gpio_cntrl |= 0xF0 & gpio->inp_config;
+
+               ret = da9058_reg_write(da9058, DA9058_GPIO0001_REG, gpio_cntrl);
+       } else {
+               u8 debounce_bits = debounce ? 0x08 : 0x00;
+
+               gpio->inp_config &= ~0x08;
+               gpio->inp_config |= debounce_bits;
+
+               if (gpio_cntrl & 0x02)
+                       goto exit;
+
+               gpio_cntrl &= ~0x0F;
+               gpio_cntrl |= 0x0F & gpio->inp_config;
+

drivers/gpio/Makefile

Quoted 10 lines
drivers/gpio/Makefile +10 -0
+               ret = da9058_reg_write(da9058, DA9058_GPIO0001_REG, gpio_cntrl);
+       }
+exit:
+       mutex_unlock(&gpio->lock);
+       if (ret)
+               dev_err(da9058->dev,
+                       "Failed to set GPIO%d bounce=%d error=%d\n",
+                       offset, debounce, ret);
+       return ret;
+ }

Apart from the same comments as above, debounce is actually pin control territory and not part of <linux/gpio.h>. Now this is a very small interface, but I still want you to consider using pin control for these portions.

The good thing about doing it with pin control is that you can have it set up on boot using hogs and need to think anymor about the pin config business.

The rest looks nice!

Yours,
Linus Walleij

#2 lore raw
Quoted 7 lines · Linus Walleij

On Sun, Aug 5, 2012 at 10:43 PM, Anthony Olech

Quoted 1 line

+#include <linux/regmap.h>

If you're using regmap you better select it in Kconfig too, but it appears you don't. You should be using regmap in the main MFD driver in this case (I haven't looked at it though.)

For MFDs the MFD core should already be ensuring that regmap is selected.

Quoted 8 lines

drivers/gpio/gpio-da9058.c line 125

Quoted 3 lines
drivers/gpio/gpio-da9058.c new file +2 -0
+               gpio_cntrl |= 0x0F & gpio->out_config;
 
+               ret = da9058_reg_write(da9058, DA9058_GPIO0001_REG, gpio_cntrl);

Further, if you're checking that flag just in order to avoid doing this write if it's not necessary, it's the wrong solution. The right solution is to implement regmap in the MFD driver so it quickly sees that the right value is already in the register and bounces off.

Just using regmap_update_bits() will do the right thing.

Quoted 10 lines

drivers/gpio/gpio-da9058.c line 226

Quoted 5 lines
drivers/gpio/gpio-da9058.c new file +5 -0
+       if (offset)
+               return da9058_to_virt_irq_num(da9058, DA9058_IRQ_EGPI1);
+       else
+               return da9058_to_virt_irq_num(da9058, DA9058_IRQ_EGPI0);
+}

Lee Jones and Mark Brown discussed these virtual IRQ mapping functions recently, and I think the outcome was to patch irqdomain to do the work and not sprinkle these custom interfaces to fetch virtual IRQs all over the place.

The easiest thing to do would be to pick the VIRQ numbers so that you can just do

	da9058_to_virt_irq_num(da9058, DA9058_IRQ_EGPI0 + offset);
#3 lore raw
In reply to Linus Walleij
Quoted 30 lines

-----Original Message-----
From: Linus Walleij [mailto:[email protected]]
Sent: 13 August 2012 14:10
To: Opensource [Anthony Olech]
Cc: Grant Likely; Linus Walleij; Mark Brown; LKML; David Dajun Chen; Samuel Ortiz; Lee Jones
Subject: Re: [NEW DRIVER V2 5/7] DA9058 GPIO driver
Hi Anthony, sorry for delayed reply...

Quoted 3 lines · Anthony Olech

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 DA9058 MFD driver.

OK

drivers/gpio/Kconfig config GPIO_DA9052 · line 91

Quoted 3 lines
drivers/gpio/Kconfig +3 -0
+config GPIO_DA9058
+       tristate "Dialog DA9058 GPIO"
+       depends on MFD_DA9058

select IRQ_DOMAIN, you're going to want to use it...

Quoted 2 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +0 -0

0f55662..209224a 100644

(...)

drivers/gpio/Makefile index

Quoted 2 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +2 -0
+#include <linux/module.h>
+#include <linux/fs.h>

Really?

Quoted 1 line

+#include <linux/uaccess.h>

Really?

drivers/gpio/Makefile index

Quoted 3 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +3 -0
+#include <linux/platform_device.h>
+#include <linux/gpio.h>
+#include <linux/syscalls.h>

Really?

Quoted 10 lines

drivers/gpio/Makefile index

Quoted 5 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +5 -0
+#include <linux/seq_file.h>
+#include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/mfd/core.h>
+#include <linux/regmap.h>

If you're using regmap you better select it in Kconfig too, but it appears you don't. You should be using regmap in the main MFD driver in this case (I haven't looked at it though.)
This header set just looks like it was copied from some other file and never really proofread, so please go over it in detail.

for some reason against 2.6.2x some of those were required, I just totally forgot to prune out the unwanted ones when rebasing forwards to 3.5. Good that you spotted it! Sorry I will try to prune the includes in the future.

Quoted 6 lines

drivers/gpio/Makefile index

Quoted 4 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +4 -0
+#include <linux/mfd/da9058/version.h> #include
+<linux/mfd/da9058/registers.h> #include <linux/mfd/da9058/core.h>
+#include <linux/mfd/da9058/gpio.h> #include <linux/mfd/da9058/irq.h>
+#include <linux/mfd/da9058/pdata.h>

Samuel will have to comment on this organization of headers, it seems a little much. DO you really need all of them?

One of them should have been stripped out by my submit script, but as for the others you must bear in mind that the DA9058 PMIC is a multifunction device, and thus some header files are common and some are specific to various component drivers. The very reason that you picked up on non-relevant include files surely has implications on the structure of the header files for the DA9058, in particular struct's and define's that only apply to one component driver should be in separate header files.

Quoted 6 lines
Quoted 6 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +6 -0
+struct da9058_gpio {
+       struct da9058 *da9058;
+       struct platform_device *pdev;
+       struct gpio_chip gp;
+       struct mutex lock;
+       u8 inp_config;
Quoted 8 lines

drivers/gpio/Makefile index

Quoted 6 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +6 -0
+       u8 out_config;
+};
+
+static struct da9058_gpio *gpio_chip_to_da9058_gpio(struct gpio_chip
+*chip) {
+       return container_of(chip, struct da9058_gpio, gp); }

static inline, or a #define, but the compile will probably optimize-inline it anyway.

The compiler should optimize it to in-line, but I will change it anyway.

Quoted 9 lines

drivers/gpio/Makefile index

Quoted 8 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +8 -0
+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 there are two GPIO pins, 0 and 1? That seems odd, but OK.

That is a feature of the hardware. I believe that calling them "0" and "1" is the correct thing to do. Correct me if they should have been called "1" and "2", or something else.

Quoted 2 lines
Quoted 1 line

+ if (offset) {

So this is for GPIO 1

Yes, it seemed the obvious thing to do.

Quoted 4 lines
Quoted 1 line

+ u8 value_bits = value ? 0x80 : 0x00;

These "value_bits" are just confusing. Just delete this and use the direct value below.

Will do. It was done for diagnostics that have since been stripped out.

Quoted 6 lines

drivers/gpio/Makefile index

Quoted 2 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +2 -0
+
+               gpio->out_config &= ~0x80;

A better way of writing &= ~0x80; is &= 0x7F

Quoted 1 line

+ gpio->out_config |= value_bits;

gpio->out_config = value ? 0x80 : 0x00;
So, less confusing.

see HANDLING NIBBLES below

Quoted 4 lines

drivers/gpio/Makefile index

Quoted 2 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +2 -0
+               if (!(gpio_cntrl & 0x20))
+                       goto exit;

Please insert a comment explaining what this bit is doing and why you're just exiting if it's not set. I don't understand one thing.

I have explained why in the driver source in the next submission attempt

Quoted 21 lines

Maybe this would be better if you didn't use so many magic values, what about:
#include <linux/bitops.h>
#define FOO_FLAG BIT(3) /* This is a flag for foo */

drivers/gpio/Makefile index

Quoted 2 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +2 -0
+
+               gpio_cntrl &= ~0xF0;

A better way to write &= ~F0 is to write &= 0x0F;
If you don't #define the constants this way of negating numbers just get confusing.
So this is OK:
foo &= ~FOO_FLAG;
foo |= set ? FOO_FLAG : 0;
This is just hard to read:
foo &= ~0x55;
foo |= set ? 0x55 : 0;
And is better off

   foo &= 0xAA;
   foo |= set ? 0x55 : 0;

I prefer that you #define the registers but it's your pick.

drivers/gpio/Makefile index

Quoted 3 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +3 -0
+               gpio_cntrl |= 0xF0 & gpio->out_config;
+
+               ret = da9058_reg_write(da9058, DA9058_GPIO0001_REG,
Quoted 7 lines

gpio_cntrl);

drivers/gpio/Makefile index

Quoted 2 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +2 -0
+       } else {
+               u8 value_bits = value ? 0x08 : 0x00;

Same here, delete this.

Quoted 1 line

+ gpio->out_config &= ~0x08;

&= 0xF7;
or use some <linux/bitops.h> and #defines ...

see HANDLING NIBBLES below

Quoted 3 lines
Quoted 1 line

+ gpio->out_config |= value_bits;

Just
gpio->out_config |= value ? 0x08 : 0x00;

done in next submission attempt

Quoted 3 lines

drivers/gpio/Makefile index

Quoted 2 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +2 -0
+               if (!(gpio_cntrl & 0x02))
+                       goto exit; 

Same thing, explain that flag.

added explanation next submission attempt

Quoted 3 lines

drivers/gpio/Makefile index

Quoted 2 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +2 -0
+
+               gpio_cntrl &= ~0x0F;

Just &= 0xF0;

see HANDLING NIBBLES below

Quoted 9 lines

drivers/gpio/Makefile index

Quoted 4 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +4 -0
+               gpio_cntrl |= 0x0F & gpio->out_config;
+
+               ret = da9058_reg_write(da9058, DA9058_GPIO0001_REG,
+ gpio_cntrl);

Further, if you're checking that flag just in order to avoid doing this write if it's not necessary, it's the wrong solution. The right solution is to implement regmap in the MFD driver so it quickly sees that the right value is already in the register and bounces off.

added explanation next submission attempt

[...]

Quoted 13 lines
Quoted 13 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +13 -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)
+               return -EINVAL;
+
Quoted 8 lines

drivers/gpio/Makefile index

Quoted 5 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +5 -0
+       if (offset)
+               return da9058_to_virt_irq_num(da9058, DA9058_IRQ_EGPI1);
+       else
+               return da9058_to_virt_irq_num(da9058,
+DA9058_IRQ_EGPI0); }

Lee Jones and Mark Brown discussed these virtual IRQ mapping functions recently, and I think the outcome was to patch irqdomain to do the work and not sprinkle these custom interfaces to fetch virtual IRQs all over the place.

The DA9058 driver does use the virtual IRQ domains

Quoted 53 lines
Quoted 19 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +19 -0
+static int da9058_gpio_set_debounce(struct gpio_chip *gc, unsigned offset,
+                               unsigned debounce) {
+       struct da9058_gpio *gpio = gpio_chip_to_da9058_gpio(gc);
+       struct da9058 *da9058 = gpio->da9058;
+       int ret;
+       unsigned int gpio_cntrl;
+
+       if (offset > 1)
+               return -EINVAL;
+
+       mutex_lock(&gpio->lock);
+
+       ret = da9058_reg_read(da9058, DA9058_GPIO0001_REG, &gpio_cntrl);
+       if (ret)
+               goto exit;
+
+       if (offset) {
+               u8 debounce_bits = debounce ? 0x80 : 0x00;
+

drivers/gpio/Makefile index

Quoted 10 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +10 -0
+               gpio->inp_config &= ~0x80;
+               gpio->inp_config |= debounce_bits;
+
+               if (gpio_cntrl & 0x20)
+                       goto exit;
+
+               gpio_cntrl &= ~0xF0;
+               gpio_cntrl |= 0xF0 & gpio->inp_config;
+
+               ret = da9058_reg_write(da9058, DA9058_GPIO0001_REG,

gpio_cntrl);

drivers/gpio/Makefile index

Quoted 13 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +13 -0
+       } else {
+               u8 debounce_bits = debounce ? 0x08 : 0x00;
+
+               gpio->inp_config &= ~0x08;
+               gpio->inp_config |= debounce_bits;
+
+               if (gpio_cntrl & 0x02)
+                       goto exit;
+
+               gpio_cntrl &= ~0x0F;
+               gpio_cntrl |= 0x0F & gpio->inp_config;
+
+               ret = da9058_reg_write(da9058, DA9058_GPIO0001_REG,

gpio_cntrl);

drivers/gpio/Makefile index

Quoted 9 lines
drivers/gpio/Makefile index renamed from drivers/gpio/Makefile +9 -0
+       }
+exit:
+       mutex_unlock(&gpio->lock);
+       if (ret)
+               dev_err(da9058->dev,
+                       "Failed to set GPIO%d bounce=%d error=%d\n",
+                       offset, debounce, ret);
+       return ret;
+ }
Quoted 5 lines

Apart from the same comments as above, debounce is actually pin control territory and not part of <linux/gpio.h>. Now this is a very small interface, but I still want you to consider using pin control for these portions.
The good thing about doing it with pin control is that you can have it set up on boot using hogs and need to think anymor about the pin config business.

PIN CONTROL ???, are there any example drivers that I could/should base it on?

Quoted 3 lines

The rest looks nice!
Yours,
Linus Walleij

Thanks for reviewing this DA9058 component driver, that just leaves the contentious point:

HANDLING NIBBLES
================

The handling of nibbles within a byte follows the rule that constants for the nibble NOT being operated on have those bits set to zero, and thus only bits being operated on may be non-zero. Thus to set, for example, the value 0xB into the MSH the operation is:

        byte &= ~0xF0
        byte |= 0xB0

it being obvious that it is the upper nibble being operated on.

It seems that you are following a different rule for handling nibbles, and I can't find any standard for doing so in the kernel, so could you send me your reference documents?

Tony Olech

#4 lore raw
Quoted 9 lines · Opensource [Anthony Olech]

[Me]

Quoted 3 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+       if (offset > 1)
+               return -EINVAL;

So there are two GPIO pins, 0 and 1? That seems odd, but OK.

That is a feature of the hardware. I believe that calling them "0" and "1" is the correct thing to do. Correct me if they should have been called "1" and "2", or something else.

It's correct, what I thought was odd was the fact that there were only two GPIO pins on this device. But some have only one even, just wanted to verify...

Quoted 7 lines

HANDLING NIBBLES
================

The handling of nibbles within a byte follows the rule that constants for the nibble NOT being operated on have those bits set to zero, and thus only bits being operated on may be non-zero. Thus to set, for example, the value 0xB into the MSH the operation is:

Quoted 6 lines
        byte &= ~0xF0
        byte |= 0xB0

it being obvious that it is the upper nibble being operated on. It seems that you are following a different rule for handling nibbles, and I can't find any standard for doing so in the kernel, so could you send me your reference documents?

In this case as stated elsewhere, I'm happy that you do things this way, if you #define the magic values you're using in your bytes and nibbles, because else it's just hard to read.

#define FOO_MASK 0xF0
#define BAR_FEATURE 0xB0

byte &= ~FOO_MASK;
byte |= BAR_FEATURE;

It's more readble.

Yours,
Linus Walleij