gpiolib: Fix gpio_direction_* for single direction GPIOs
PATCH 1/1 gpiolib: Fix gpio_direction_* for single direction GPIOs
GPIOs with no programmable direction are not required to implement direction_output nor direction_input.
If we try to set an output direction on an output-only GPIO or input direction on an input-only GPIO simply return 0.
This allows this single direction GPIO to be used by libgpiod.
--- drivers/gpio/gpiolib.c | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-)
drivers/gpio/gpiolib.c +28 -8
@@ -2512,19 +2512,27 @@ EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);int gpiod_direction_input (struct gpio_desc *desc) {struct gpio_chip *chip; -int status = -EINVAL ; +int status =0 ;VALIDATE_DESC (desc); chip = desc->gdev->chip; -if (!chip->get || !chip->direction_input) { +if (!chip->get && chip->direction_input) {gpiod_warn (desc, -"%s: missing get() or direction_input() operations\n" , +"%s: missing get() and direction_input() operations\n" , __func__);return -EIO ; } - status = chip->direction_input (chip,gpio_chip_hwgpio (desc)); +if (chip->direction_input) { + status = chip->direction_input (chip,gpio_chip_hwgpio (desc)); + }else if (chip->get_direction && + (chip->get_direction (chip,gpio_chip_hwgpio (desc)) !=1 )) { +gpiod_warn (desc, +"%s: missing direction_input() operation\n" , + __func__); +return -EIO ; + }if (status ==0 )clear_bit (FLAG_IS_OUT , &desc->flags);
@@ -2546,16 +2554,28 @@ static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) {struct gpio_chip *gc = desc->gdev->chip;int val = !!value; -int ret; +int ret =0 ; -if (!gc->set || !gc->direction_output) { +if (!gc->set && !gc->direction_output) {gpiod_warn (desc, -"%s: missing set() or direction_output() operations\n" , +"%s: missing set() and direction_output() operations\n" , __func__);return -EIO ; } - ret = gc->direction_output (gc,gpio_chip_hwgpio (desc), val); +if (gc->direction_output) { + ret = gc->direction_output (gc,gpio_chip_hwgpio (desc), val); + }else { +if (gc->get_direction && + gc->get_direction (gc,gpio_chip_hwgpio (desc))) { +gpiod_warn (desc, +"%s: missing direction_output() operation\n" , + __func__); +return -EIO ; + } + gc->set (gc,gpio_chip_hwgpio (desc), val); + } +if (!ret)set_bit (FLAG_IS_OUT , &desc->flags);trace_gpio_value (desc_to_gpio (desc),0 , val);
Signature · 1 line
2.18.0
PATCH v2 1/1 gpiolib: Show correct direction from the beginning
Current code assumes that the direction is input if direction_input function is set.
This might not be the case on GPIOs with programmable direction.
--- drivers/gpio/gpiolib.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpio/gpiolib.c +6 -1
@@ -1352,7 +1352,12 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,* it does, and in case chip->get_direction is not set, we may * expose the wrong direction in sysfs. */ - desc->flags = !chip->direction_input ? (1 <<FLAG_IS_OUT ) :0 ; +if (chip->get_direction &&gpiochip_line_is_valid (chip, i)) + desc->flags = !chip->get_direction (chip, i) ? + (1 <<FLAG_IS_OUT ) :0 ; +else + desc->flags = !chip->direction_input ? + (1 <<FLAG_IS_OUT ) :0 ; }#ifdef CONFIG_PINCTRL
Signature · 1 line
2.18.0
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
Jeff, can you test these two patches on Amberwing to make sure that they don't cause an XPU violation on boot?
The call to gpiochip_line_is_valid() should return false on any GPIOs that aren't listed in the ACPI table.
My concern is that this patch might be calling gpiochip_line_is_valid() too early, before all the arrays have been set up.
Thanks.
Quoted 28 lines · Ricardo Ribalda Delgado
Current code assumes that the direction is input if
direction_inputfunction is set.
This might not be the case on GPIOs with programmable direction.Signed-off-by Ricardo Ribalda Delgado <[email protected]>--- drivers/gpio/gpiolib.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)drivers/gpio/gpiolib.c +6
-1@@ -1352,7 +1352,12 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,* it does, and in case chip->get_direction is not set, we may * expose the wrong direction in sysfs. */ - desc->flags = !chip->direction_input ? (1 <<FLAG_IS_OUT ) :0 ; +if (chip->get_direction &&gpiochip_line_is_valid (chip, i)) + desc->flags = !chip->get_direction (chip, i) ? + (1 <<FLAG_IS_OUT ) :0 ; +else + desc->flags = !chip->direction_input ? + (1 <<FLAG_IS_OUT ) :0 ; }#ifdef CONFIG_PINCTRL
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
In reply to Ricardo Ribalda DelgadoQuoting Ricardo Ribalda Delgado (2018-09-21 03:36:04)
Quoted 6 lines
Current code assumes that the direction is input if
direction_inputfunction is set.
This might not be the case on GPIOs with programmable direction.Signed-off-by Ricardo Ribalda Delgado <[email protected]>---
Looks OK to me visually. I haven't tested it because I don't have access to the locked down hardware anymore.
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
Quoted 2 lines · Stephen Boyd
Looks OK to me visually. I haven't tested it because I don't have access to the locked down hardware anymore.
Same here. Please wait for Jeff Hugo to test it before applying. Thanks.
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
Quoted 5 lines · Timur Tabi
Quoted 2 lines · Stephen Boyd
Looks OK to me visually. I haven't tested it because I don't have access to the locked down hardware anymore.
Same here. Please wait for Jeff Hugo to test it before applying. Thanks.
I guess its lucky I saw this then.
I should be able to test within a week.
Signature · 4 lines
Jeffrey Hugo Qualcomm Datacenter Technologies as an affiliate of Qualcomm Technologies, Inc. Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
Quoted 2 lines · Jeffrey Hugo
I guess its lucky I saw this then.
Did you not get this email: https://lore.kernel.org/patchwork/patch/989545/#1173771
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
Quoted 5 lines · Timur Tabi
Quoted 2 lines · Jeffrey Hugo
I guess its lucky I saw this then.
Did you not get this email: https://lore.kernel.org/patchwork/patch/989545/#1173771
Apparently I did. I found it in my deleted items. I must have accidentally done that when sorting through my backlog after Linaro Connect. Sorry about that.
Signature · 4 lines
Jeffrey Hugo Qualcomm Datacenter Technologies as an affiliate of Qualcomm Technologies, Inc. Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
In reply to Jeffrey HugoQuoted 11 lines · Jeffrey Hugo
Quoted 5 lines · Timur Tabi
Quoted 2 lines · Stephen Boyd
Looks OK to me visually. I haven't tested it because I don't have access to the locked down hardware anymore.
Same here. Please wait for Jeff Hugo to test it before applying. Thanks.
I guess its lucky I saw this then.
I should be able to test within a week.
Nack. Causes a XPU violation to the GPIO config registers.
Signature · 4 lines
Jeffrey Hugo Qualcomm Datacenter Technologies as an affiliate of Qualcomm Technologies, Inc. Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
Quoted 1 line · Jeffrey Hugo
Nack. Causes a XPU violation to the GPIO config registers.
That doesn't surprise me at all.
I believe that the problem is that gpiochip_line_is_valid() is being called before gpiochip_irqchip_init_valid_mask() is called, which means that gpiochip_line_is_valid() always returns true.
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
Hi Timur
In fact gpiochip_init_valid_mask is called some lines after in the same function. We could reorder the function. Would that work for you?
The driver breaking is upstream? Is it possible to access the hardware?
Thanks
[Sorry for the two html mails in a row, I did not try to work with my phone before :) ]
Quoted 9 lines · Timur Tabi
Quoted 1 line · Jeffrey Hugo
Nack. Causes a XPU violation to the GPIO config registers.
That doesn't surprise me at all.
I believe that the problem is that
gpiochip_line_is_valid()is being called beforegpiochip_irqchip_init_valid_mask()is called, which means thatgpiochip_line_is_valid()always returns true.
Signature · 1 line
Ricardo Ribalda
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
Quoted 2 lines · Ricardo Ribalda Delgado
In fact
gpiochip_init_valid_maskis called some lines after in the same function. We could reorder the function. Would that work for you?
It might. It might break something else, though.
Quoted 1 line
The driver breaking is upstream?
Yes.
Quoted 1 line
Is it possible to access the hardware?
Linaro and some Linux OSVs should still have systems, but I usually just ask Jeff to test code for me.
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
Quoted 4 lines · Timur Tabi
Quoted 2 lines
Is it possible to access the hardware?
Linaro and some Linux OSVs should still have systems, but I usually just ask Jeff to test code for me.
Alternatively, you can just add valid_mask support to your driver, and add a check to your get_direction() function to see if it ever is asked to access an invalid GPIO.
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
Quoted 2 lines · Ricardo Ribalda Delgado
How do we proceed from here? Can you fix your driver somehow to init the valid mask before enabling the gpio?
Just include a hunk to the qcom driver reordering this call at the same time. No need to make it separate patches, it need to be tested together anyways.
I guess just switch the order of these two:
ret = gpiochip_add_data(&pctrl->chip, pctrl);
if (ret) {
dev_err(pctrl->dev, "Failed register gpiochip\n");
return ret;
}
ret = msm_gpio_init_valid_mask(chip, pctrl);
if (ret) {
dev_err(pctrl->dev, "Failed to setup irq valid bits\n");
gpiochip_remove(&pctrl->chip);
return ret;
}Quoted 1 line
Do we need to make more severe changes on the core?
Don't think so.
Yours,
Linus Walleij
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
Hi Linus
Quoted 13 lines · Linus Walleij
Quoted 2 lines · Ricardo Ribalda Delgado
How do we proceed from here? Can you fix your driver somehow to init the valid mask before enabling the gpio?
Just include a hunk to the qcom driver reordering this call at the same time. No need to make it separate patches, it need to be tested together anyways.
I guess just switch the order of these two:
Quoted 13 lines
ret = gpiochip_add_data(&pctrl->chip, pctrl);
if (ret) {
dev_err(pctrl->dev, "Failed register gpiochip\n");
return ret;
}
ret = msm_gpio_init_valid_mask(chip, pctrl);
if (ret) {
dev_err(pctrl->dev, "Failed to setup irq valid bits\n");
gpiochip_remove(&pctrl->chip);
return ret;
}the problem is that valid_mask is not a long/integer, is a struct that needs to be malloced, and is malloc at gpiochip_add_data :(
Maybe we need a callback from the driver to init that mask just after the allocation?
A fast grep shows that the only driver using need_valid_mask (not for irq) is msm:
ricardo@neopili:~/curro/kernel-upstream$ git grep "need_valid_mask =" | grep -v irq drivers/gpio/gpiolib.c: gpiochip->need_valid_mask = true; drivers/pinctrl/intel/pinctrl-cherryview.c: bool need_valid_mask = !dmi_check_system(chv_no_valid_mask); drivers/pinctrl/qcom/pinctrl-msm.c: chip->need_valid_mask = msm_gpio_needs_valid_mask(pctrl);
so hacking something in the driver might not be a terrible idea.
Also
Quoted 6 lines
Quoted 1 line
Do we need to make more severe changes on the core?
Don't think so.
Yours,
Linus Walleij
Signature · 1 line
Ricardo Ribalda
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
Quoted 46 lines · Ricardo Ribalda Delgado
Hi Linus
Quoted 13 lines · Linus Walleij
Quoted 2 lines · Ricardo Ribalda Delgado
How do we proceed from here? Can you fix your driver somehow to init the valid mask before enabling the gpio?
Just include a hunk to the qcom driver reordering this call at the same time. No need to make it separate patches, it need to be tested together anyways.
I guess just switch the order of these two:
Quoted 13 lines
ret = gpiochip_add_data(&pctrl->chip, pctrl); if (ret) { dev_err(pctrl->dev, "Failed register gpiochip\n"); return ret; } ret = msm_gpio_init_valid_mask(chip, pctrl); if (ret) { dev_err(pctrl->dev, "Failed to setup irq valid bits\n"); gpiochip_remove(&pctrl->chip); return ret; }the problem is that
valid_maskis not a long/integer, is a struct that needs to be malloced, and is malloc atgpiochip_add_data:(Maybe we need a callback from the driver to init that mask just after the allocation?
A fast grep shows that the only driver using
need_valid_mask(not for irq) is msm:ricardo@neopili:~/curro/kernel-upstream$ git grep "need_valid_mask =" | grep -v irq drivers/gpio/gpiolib.c: gpiochip->need_valid_mask = true; drivers/pinctrl/intel/pinctrl-cherryview.c: bool need_valid_mask = !dmi_check_system(chv_no_valid_mask); drivers/pinctrl/qcom/pinctrl-msm.c: chip->need_valid_mask = msm_gpio_needs_valid_mask(pctrl);
Quoted 2 lines
so hacking something in the driver might not be a terrible idea.
IMO, hack up the driver and I'll test it. We can figure out what is needed to work, then determine what is the proper solution for that.
Signature · 4 lines
Jeffrey Hugo Qualcomm Datacenter Technologies as an affiliate of Qualcomm Technologies, Inc. Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
In reply to Ricardo Ribalda DelgadoQuoted 27 lines · Ricardo Ribalda Delgado
Quoted 12 lines · Linus Walleij
Quoted 2 lines · Ricardo Ribalda Delgado
How do we proceed from here? Can you fix your driver somehow to init the valid mask before enabling the gpio?
Just include a hunk to the qcom driver reordering this call at the same time. No need to make it separate patches, it need to be tested together anyways.
I guess just switch the order of these two:
Quoted 13 lines
ret = gpiochip_add_data(&pctrl->chip, pctrl); if (ret) { dev_err(pctrl->dev, "Failed register gpiochip\n"); return ret; } ret = msm_gpio_init_valid_mask(chip, pctrl); if (ret) { dev_err(pctrl->dev, "Failed to setup irq valid bits\n"); gpiochip_remove(&pctrl->chip); return ret; }
Quoted 2 lines
the problem is that
valid_maskis not a long/integer, is a struct that needs to be malloced, and is malloc atgpiochip_add_data:(
I don't get it, but maybe I'm not smart enough.
gpiochip_add_data() doesn't allocate anything, it just adds a already allocated (or static!) gpio_chip to the gpiolib subsystem.
In fact I think it is wrong to set up the mask after calling gpiolob_add_data(), because of exactly the type of problem you're seeing.
Don't get confused by the &pctrl->chip
vs just chip variables, it's just some sloppiness.
Yours,
Linus Walleij
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
Hi
Quoted 30 lines · Linus Walleij
Quoted 27 lines · Ricardo Ribalda Delgado
Quoted 25 lines · Linus Walleij
Quoted 2 lines · Ricardo Ribalda Delgado
How do we proceed from here? Can you fix your driver somehow to init the valid mask before enabling the gpio?
Just include a hunk to the qcom driver reordering this call at the same time. No need to make it separate patches, it need to be tested together anyways.
I guess just switch the order of these two:
ret = gpiochip_add_data(&pctrl->chip, pctrl); if (ret) { dev_err(pctrl->dev, "Failed register gpiochip\n"); return ret; } ret = msm_gpio_init_valid_mask(chip, pctrl); if (ret) { dev_err(pctrl->dev, "Failed to setup irq valid bits\n"); gpiochip_remove(&pctrl->chip); return ret; }
Quoted 4 lines
Quoted 2 lines
the problem is that
valid_maskis not a long/integer, is a struct that needs to be malloced, and is malloc atgpiochip_add_data:(I don't get it, but maybe I'm not smart enough.
Dont take my job, I am the not smart of the conversation :P
Quoted 5 lines
gpiochip_add_data()doesn't allocate anything, it just adds a already allocated (or static!)gpio_chipto the gpiolib subsystem.
Take a look to gpiochip_add_data_with_key()
->gpiochip_init_valid_mask()
-> gpiochip->valid_mask = gpiochip_allocate_mask(gpiochip);
Quoted 3 lines
In fact I think it is wrong to set up the mask after calling
gpiolob_add_data(), because of exactly the type of problem you're seeing.
I agree, and I believe that the cleaner way would be to add a function pointer that replaces:
gpiochip_allocate_mask()
bitmap_fill(p, chip->ngpio);
with a proper initalization from the driver
But as today the only driver that seems to be using valid_mask is msm, so perhaps a hack is something better and then when we have a second driver that requires it we figure out the real requirements. But it is definately your decision ;)
Quoted 6 lines
Don't get confused by the
&pctrl->chip
vs just chip variables, it's just some sloppiness.Yours,
Linus Walleij
Thanks!
Signature · 1 line
Ricardo Ribalda
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
Quoted 9 lines · Ricardo Ribalda Delgado
Quoted 3 lines · Linus Walleij
gpiochip_add_data()doesn't allocate anything, it just adds a already allocated (or static!)gpio_chipto the gpiolib subsystem.Take a look to
gpiochip_add_data_with_key()
->gpiochip_init_valid_mask()-> gpiochip->valid_mask = gpiochip_allocate_mask(gpiochip);
Aha I see...
Quoted 11 lines
Quoted 3 lines
In fact I think it is wrong to set up the mask after calling
gpiolob_add_data(), because of exactly the type of problem you're seeing.I agree, and I believe that the cleaner way would be to add a function pointer that replaces:
gpiochip_allocate_mask()
bitmap_fill(p, chip->ngpio);with a proper initalization from the driver
OK
Quoted 4 lines
But as today the only driver that seems to be using
valid_maskis msm, so perhaps a hack is something better and then when we have a second driver that requires it we figure out the real requirements. But it is definately your decision ;)
I would just add some exported function to gpiolib to do what you need so you can set up the valid_mask before calling gpiochip_add*.
Yours,
Linus Walleij
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
Quoted 4 lines · Linus Walleij
Quoted 4 lines
But as today the only driver that seems to be using
valid_maskis msm, so perhaps a hack is something better and then when we have a second driver that requires it we figure out the real requirements. But it is definately your decision;)
Please note that MSM is supposed to be the *first* driver, not the only, driver that needs valid_mask. So let's not make any code changes that limit this feature to the MSM driver.
Quoted 3 lines
I would just add some exported function to gpiolib to do what you need so you can set up the
valid_maskbefore calling gpiochip_add*.
I think that should be okay. Drivers should know pretty early whether they need valid_mask or not.
Re: [PATCH v2] gpiolib: Show correct direction from the beginning
Quoted 9 lines · Timur Tabi
Quoted 4 lines · Linus Walleij
Quoted 4 lines
But as today the only driver that seems to be using
valid_maskis msm, so perhaps a hack is something better and then when we have a second driver that requires it we figure out the real requirements. But it is definately your decision;)Please note that MSM is supposed to be the *first* driver, not the only, driver that needs
valid_mask. So let's not make any code changes that limit this feature to the MSM driver.
I just recently encouraged Thierry to reuse this for the Tegra186 driver:
https://marc.info/?l=linux-gpio&m=153787164826821&w=2
Yours,
Linus Walleij
Hi Ricardo,
thanks for the patch and sorry for taking time before responding.
Quoted 9 lines · Ricardo Ribalda Delgado
GPIOs with no programmable direction are not required to implement
direction_outputnordirection_input.If we try to set an output direction on an output-only GPIO or input direction on an input-only GPIO simply return 0.
This allows this single direction GPIO to be used by libgpiod.
Signed-off-by Ricardo Ribalda Delgado <[email protected]>
It makes perfect sense, patch applied.
I'll go in and add some comments to the code so I understand it right as well in the future.
Yours,
Linus Walleij