Re: [PATCH v2 2/2] pinctrl: nuvoton: add NPCM7xx pinctrl and GPIO driver

From: Tomer Maimon
Date: Sun Jul 29 2018 - 19:24:06 EST


Hi Linus,

On 30 July 2018 at 00:59, Linus Walleij <linus.walleij@xxxxxxxxxx> wrote:
On Thu, Jul 26, 2018 at 2:01 AM Tomer Maimon <tmaimon77@xxxxxxxxx> wrote:

> I initialize bgpio as follow:
>
>Â Â Â Â Â Â Â Â Â Â Â Â Âret = bgpio_init(&pctrl->gpio_bank[id].gc,
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pctrl->dev, 4,
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pctrl->gpio_bank[id].base +
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â NPCM7XX_GP_N_DIN,
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pctrl->gpio_bank[id].base +
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â NPCM7XX_GP_N_DOUT,
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â NULL,
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â NULL,
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pctrl->gpio_bank[id].base +
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â NPCM7XX_GP_N_IEM,
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â BGPIOF_READ_OUTPUT_REG_SET);

(...)
> The problem occur when reading the GPIO value from bgpio_get_set function,
> because the directions value are inverse it reading the wrong I/O registers
>
> For direction out it reading dat register (instead of set register)
>
> For direction in it calling set register (instead of dat register)

Hm I don't quite get it... sorry. Maybe if you show your fix and what
you expect to happen I can understand better?
Â
Of course, in the last patch sentÂthree days a go (V3 - https://patchwork.ozlabs.org/patch/949942/) I did as follow to workaround the issue:

   Âint (*get)(struct gpio_chip *chip, unsigned offset);
   Âint (*get_multiple)(struct gpio_chip *chip, unsigned long *mask,
             Âunsigned long *bits);

......

static int npcmgpio_get_value(struct gpio_chip *chip, unsigned int offset)
{
   Âstruct npcm7xx_gpio *bank = gpiochip_get_data(chip);
   Âunsigned long tmp_bgpio_dir = bank->gc.bgpio_dir;
   Âint val;

   Â/*
    * sets bgpio_dir parameter value to the opposite value
    * for calling the right registers in bgpio_get_set
    * function
    */
   Âbank->gc.bgpio_dir = ~bank->gc.bgpio_dir;
   Âval = bank->get(chip, offset);
   Âbank->gc.bgpio_dir = tmp_bgpio_dir;

   Âreturn val;
}

static int npcmgpio_get_multiple_value(struct gpio_chip *chip,
                   unsigned long *mask, unsigned long *bits)
{
   Âstruct npcm7xx_gpio *bank = gpiochip_get_data(chip);
   Âunsigned long tmp_bgpio_dir = bank->gc.bgpio_dir;
   Âint val;

   Â/*
    * sets bgpio_dir parameter value to the opposite value
    * for calling the right registers in bgpio_get_set_multiple
    * function
    */
   Âbank->gc.bgpio_dir = ~bank->gc.bgpio_dir;
   Âval = bank->get_multiple(chip, mask, bits);
   Âbank->gc.bgpio_dir = tmp_bgpio_dir;

   Âreturn val;
}
Â
......

   Âpctrl->gpio_bank[id].get = pctrl->gpio_bank[id].gc.get;
   Âpctrl->gpio_bank[id].gc.get = npcmgpio_get_value;
   Âpctrl->gpio_bank[id].get_multiple = ptrl->gpio_bank[id].gc.get_multiple;
   Âpctrl->gpio_bank[id].gc.get_multiple =Ânpcmgpio_get_multiple_value;


but it is not that good solution, because the bold commands are not atomic (locked) operations.
Â

Do you mean that because you write the inverse value to
IEM this happens, and the BGPIO code assumes that
you always write 1 to set a line as input and 0 to set it
as output?

yes, because of it the bgpio_get_set and bgpio_get_set_multiple setting the opposite data registers .
ÂÂ

I would say if this causes the problem we should just add
a new BGPIOF_INVERTED_REG_DIR with comment in
include/linux/gpio/driver.h and make the necessary fix to
respect this flag in the gpio-mmio.c core so it works right.

If you do this as a separate patch I would be grateful :)

Sure, I will send a separate patchÂlater on to overcome it.

Yours,
Linus Walleij

Thanks,

Tomer