pinctrl: baytrail: show output gpio state correctly on Intel Baytrail
diff mbox

Message ID 20141013191405.GB29810@saruman
State Not Applicable
Headers show

Commit Message

Felipe Balbi Oct. 13, 2014, 7:14 p.m. UTC
On Mon, Oct 13, 2014 at 11:23:59AM -0700, David Cohen wrote:
> Even if a gpio pin is set to output, we still need to set INPUT_EN bit

here you say you're setting that bit.

> to be able to read its value. Without this change, we'll always read low
> level state.
> 
> Cc: <stable@vger.kernel.org> # v3.14+
> Signed-off-by: David Cohen <david.a.cohen@linux.intel.com>
> ---
> 
> Hi,
> 
> I'm resending same v1 patch but now copying linux stable and linux gpio.
> This patch is meant for all linux stable trees >= v3.14.
> 
> Br, David
> ---
> 
>  drivers/pinctrl/pinctrl-baytrail.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/pinctrl/pinctrl-baytrail.c b/drivers/pinctrl/pinctrl-baytrail.c
> index e12e5b07f6d7..c23d8ded936d 100644
> --- a/drivers/pinctrl/pinctrl-baytrail.c
> +++ b/drivers/pinctrl/pinctrl-baytrail.c
> @@ -318,7 +318,7 @@ static int byt_gpio_direction_output(struct gpio_chip *chip,
>  		"Potential Error: Setting GPIO with direct_irq_en to output");
>  
>  	reg_val = readl(reg) | BYT_DIR_MASK;
> -	reg_val &= ~BYT_OUTPUT_EN;
> +	reg_val &= ~(BYT_OUTPUT_EN | BYT_INPUT_EN);

but code is clearing it. Also, you're not patching byt_gpio_get(), so
you can't be 'reading its value' either. Quite frankly, this looks very
fishy to me. Comments on BYT_OUTPUT_EN and BYT_INPUT_EN states that
those bits are active low, meaning that clearing them enables that
particular feature.

How can you enable a pin to be both OUTPUT and INPUT ? Look like what
you really want is:


completely untested however. Care to further explain why clearing both
bits - thus enabling both INPUT and OUTPUT - is correct ?

Comments

David Cohen Oct. 13, 2014, 7:24 p.m. UTC | #1
On Mon, Oct 13, 2014 at 02:14:05PM -0500, Felipe Balbi wrote:
> On Mon, Oct 13, 2014 at 11:23:59AM -0700, David Cohen wrote:
> > Even if a gpio pin is set to output, we still need to set INPUT_EN bit
> 
> here you say you're setting that bit.
> 
> > to be able to read its value. Without this change, we'll always read low
> > level state.
> > 
> > Cc: <stable@vger.kernel.org> # v3.14+
> > Signed-off-by: David Cohen <david.a.cohen@linux.intel.com>
> > ---
> > 
> > Hi,
> > 
> > I'm resending same v1 patch but now copying linux stable and linux gpio.
> > This patch is meant for all linux stable trees >= v3.14.
> > 
> > Br, David
> > ---
> > 
> >  drivers/pinctrl/pinctrl-baytrail.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/pinctrl/pinctrl-baytrail.c b/drivers/pinctrl/pinctrl-baytrail.c
> > index e12e5b07f6d7..c23d8ded936d 100644
> > --- a/drivers/pinctrl/pinctrl-baytrail.c
> > +++ b/drivers/pinctrl/pinctrl-baytrail.c
> > @@ -318,7 +318,7 @@ static int byt_gpio_direction_output(struct gpio_chip *chip,
> >  		"Potential Error: Setting GPIO with direct_irq_en to output");
> >  
> >  	reg_val = readl(reg) | BYT_DIR_MASK;
> > -	reg_val &= ~BYT_OUTPUT_EN;
> > +	reg_val &= ~(BYT_OUTPUT_EN | BYT_INPUT_EN);
> 
> but code is clearing it. Also, you're not patching byt_gpio_get(), so
> you can't be 'reading its value' either. Quite frankly, this looks very
> fishy to me. Comments on BYT_OUTPUT_EN and BYT_INPUT_EN states that
> those bits are active low, meaning that clearing them enables that
> particular feature.
> 
> How can you enable a pin to be both OUTPUT and INPUT ? Look like what
> you really want is:
> 
> diff --git a/drivers/pinctrl/pinctrl-baytrail.c b/drivers/pinctrl/pinctrl-baytrail.c
> index e12e5b0..69f882d 100644
> --- a/drivers/pinctrl/pinctrl-baytrail.c
> +++ b/drivers/pinctrl/pinctrl-baytrail.c
> @@ -290,6 +290,7 @@ static int byt_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
>  
>  	value = readl(reg) | BYT_DIR_MASK;
>  	value &= ~BYT_INPUT_EN;		/* active low */
> +	value |= BYT_OUTPUT_EN;		/* disable output */
>  	writel(value, reg);
>  
>  	spin_unlock_irqrestore(&vg->lock, flags);
> @@ -319,6 +320,7 @@ static int byt_gpio_direction_output(struct gpio_chip *chip,
>  
>  	reg_val = readl(reg) | BYT_DIR_MASK;
>  	reg_val &= ~BYT_OUTPUT_EN;
> +	value |= BYT_INPUT_EN;		/* disable input */
>  
>  	if (value)
>  		writel(reg_val | BYT_LEVEL, reg);
> 
> completely untested however. Care to further explain why clearing both
> bits - thus enabling both INPUT and OUTPUT - is correct ?

Actually I really meant what I sent, but perhaps I misspoke during
explanation. We need to clear the bit to enable the functionality.

!BYT_INPUT_EN allows us to read the value. !BYT_OUTPUT_EN allows us to
set a value. If we want to set a value (acting as output) and still be
able to read via sysfs, we need to set both functionality (i.e. clear
both bits).

I'll resend the patch with a better comment.

Br, David

> 
> -- 
> balbi


--
To unsubscribe from this list: send the line "unsubscribe linux-gpio" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Felipe Balbi Oct. 13, 2014, 7:26 p.m. UTC | #2
Hi,

On Mon, Oct 13, 2014 at 12:24:04PM -0700, David Cohen wrote:
> On Mon, Oct 13, 2014 at 02:14:05PM -0500, Felipe Balbi wrote:
> > On Mon, Oct 13, 2014 at 11:23:59AM -0700, David Cohen wrote:
> > > Even if a gpio pin is set to output, we still need to set INPUT_EN bit
> > 
> > here you say you're setting that bit.
> > 
> > > to be able to read its value. Without this change, we'll always read low
> > > level state.
> > > 
> > > Cc: <stable@vger.kernel.org> # v3.14+
> > > Signed-off-by: David Cohen <david.a.cohen@linux.intel.com>
> > > ---
> > > 
> > > Hi,
> > > 
> > > I'm resending same v1 patch but now copying linux stable and linux gpio.
> > > This patch is meant for all linux stable trees >= v3.14.
> > > 
> > > Br, David
> > > ---
> > > 
> > >  drivers/pinctrl/pinctrl-baytrail.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > 
> > > diff --git a/drivers/pinctrl/pinctrl-baytrail.c b/drivers/pinctrl/pinctrl-baytrail.c
> > > index e12e5b07f6d7..c23d8ded936d 100644
> > > --- a/drivers/pinctrl/pinctrl-baytrail.c
> > > +++ b/drivers/pinctrl/pinctrl-baytrail.c
> > > @@ -318,7 +318,7 @@ static int byt_gpio_direction_output(struct gpio_chip *chip,
> > >  		"Potential Error: Setting GPIO with direct_irq_en to output");
> > >  
> > >  	reg_val = readl(reg) | BYT_DIR_MASK;
> > > -	reg_val &= ~BYT_OUTPUT_EN;
> > > +	reg_val &= ~(BYT_OUTPUT_EN | BYT_INPUT_EN);
> > 
> > but code is clearing it. Also, you're not patching byt_gpio_get(), so
> > you can't be 'reading its value' either. Quite frankly, this looks very
> > fishy to me. Comments on BYT_OUTPUT_EN and BYT_INPUT_EN states that
> > those bits are active low, meaning that clearing them enables that
> > particular feature.
> > 
> > How can you enable a pin to be both OUTPUT and INPUT ? Look like what
> > you really want is:
> > 
> > diff --git a/drivers/pinctrl/pinctrl-baytrail.c b/drivers/pinctrl/pinctrl-baytrail.c
> > index e12e5b0..69f882d 100644
> > --- a/drivers/pinctrl/pinctrl-baytrail.c
> > +++ b/drivers/pinctrl/pinctrl-baytrail.c
> > @@ -290,6 +290,7 @@ static int byt_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
> >  
> >  	value = readl(reg) | BYT_DIR_MASK;
> >  	value &= ~BYT_INPUT_EN;		/* active low */
> > +	value |= BYT_OUTPUT_EN;		/* disable output */
> >  	writel(value, reg);
> >  
> >  	spin_unlock_irqrestore(&vg->lock, flags);
> > @@ -319,6 +320,7 @@ static int byt_gpio_direction_output(struct gpio_chip *chip,
> >  
> >  	reg_val = readl(reg) | BYT_DIR_MASK;
> >  	reg_val &= ~BYT_OUTPUT_EN;
> > +	value |= BYT_INPUT_EN;		/* disable input */
> >  
> >  	if (value)
> >  		writel(reg_val | BYT_LEVEL, reg);
> > 
> > completely untested however. Care to further explain why clearing both
> > bits - thus enabling both INPUT and OUTPUT - is correct ?
> 
> Actually I really meant what I sent, but perhaps I misspoke during
> explanation. We need to clear the bit to enable the functionality.
> 
> !BYT_INPUT_EN allows us to read the value. !BYT_OUTPUT_EN allows us to
> set a value. If we want to set a value (acting as output) and still be
> able to read via sysfs, we need to set both functionality (i.e. clear
> both bits).
> 
> I'll resend the patch with a better comment.

Alright, just make sure that exposing a pin through sysfs and making it
output high, then later switching it to input works. It looks like
byt_gpio_direction_input() still needs to make sure OUTPUT is set.
David Cohen Oct. 13, 2014, 8:16 p.m. UTC | #3
On Mon, Oct 13, 2014 at 02:26:32PM -0500, Felipe Balbi wrote:
> Hi,
> 
> On Mon, Oct 13, 2014 at 12:24:04PM -0700, David Cohen wrote:
> > On Mon, Oct 13, 2014 at 02:14:05PM -0500, Felipe Balbi wrote:
> > > On Mon, Oct 13, 2014 at 11:23:59AM -0700, David Cohen wrote:
> > > > Even if a gpio pin is set to output, we still need to set INPUT_EN bit
> > > 
> > > here you say you're setting that bit.
> > > 
> > > > to be able to read its value. Without this change, we'll always read low
> > > > level state.
> > > > 
> > > > Cc: <stable@vger.kernel.org> # v3.14+
> > > > Signed-off-by: David Cohen <david.a.cohen@linux.intel.com>
> > > > ---
> > > > 
> > > > Hi,
> > > > 
> > > > I'm resending same v1 patch but now copying linux stable and linux gpio.
> > > > This patch is meant for all linux stable trees >= v3.14.
> > > > 
> > > > Br, David
> > > > ---
> > > > 
> > > >  drivers/pinctrl/pinctrl-baytrail.c | 2 +-
> > > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > > 
> > > > diff --git a/drivers/pinctrl/pinctrl-baytrail.c b/drivers/pinctrl/pinctrl-baytrail.c
> > > > index e12e5b07f6d7..c23d8ded936d 100644
> > > > --- a/drivers/pinctrl/pinctrl-baytrail.c
> > > > +++ b/drivers/pinctrl/pinctrl-baytrail.c
> > > > @@ -318,7 +318,7 @@ static int byt_gpio_direction_output(struct gpio_chip *chip,
> > > >  		"Potential Error: Setting GPIO with direct_irq_en to output");
> > > >  
> > > >  	reg_val = readl(reg) | BYT_DIR_MASK;
> > > > -	reg_val &= ~BYT_OUTPUT_EN;
> > > > +	reg_val &= ~(BYT_OUTPUT_EN | BYT_INPUT_EN);
> > > 
> > > but code is clearing it. Also, you're not patching byt_gpio_get(), so
> > > you can't be 'reading its value' either. Quite frankly, this looks very
> > > fishy to me. Comments on BYT_OUTPUT_EN and BYT_INPUT_EN states that
> > > those bits are active low, meaning that clearing them enables that
> > > particular feature.
> > > 
> > > How can you enable a pin to be both OUTPUT and INPUT ? Look like what
> > > you really want is:
> > > 
> > > diff --git a/drivers/pinctrl/pinctrl-baytrail.c b/drivers/pinctrl/pinctrl-baytrail.c
> > > index e12e5b0..69f882d 100644
> > > --- a/drivers/pinctrl/pinctrl-baytrail.c
> > > +++ b/drivers/pinctrl/pinctrl-baytrail.c
> > > @@ -290,6 +290,7 @@ static int byt_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
> > >  
> > >  	value = readl(reg) | BYT_DIR_MASK;
> > >  	value &= ~BYT_INPUT_EN;		/* active low */
> > > +	value |= BYT_OUTPUT_EN;		/* disable output */
> > >  	writel(value, reg);
> > >  
> > >  	spin_unlock_irqrestore(&vg->lock, flags);
> > > @@ -319,6 +320,7 @@ static int byt_gpio_direction_output(struct gpio_chip *chip,
> > >  
> > >  	reg_val = readl(reg) | BYT_DIR_MASK;
> > >  	reg_val &= ~BYT_OUTPUT_EN;
> > > +	value |= BYT_INPUT_EN;		/* disable input */
> > >  
> > >  	if (value)
> > >  		writel(reg_val | BYT_LEVEL, reg);
> > > 
> > > completely untested however. Care to further explain why clearing both
> > > bits - thus enabling both INPUT and OUTPUT - is correct ?
> > 
> > Actually I really meant what I sent, but perhaps I misspoke during
> > explanation. We need to clear the bit to enable the functionality.
> > 
> > !BYT_INPUT_EN allows us to read the value. !BYT_OUTPUT_EN allows us to
> > set a value. If we want to set a value (acting as output) and still be
> > able to read via sysfs, we need to set both functionality (i.e. clear
> > both bits).
> > 
> > I'll resend the patch with a better comment.
> 
> Alright, just make sure that exposing a pin through sysfs and making it
> output high, then later switching it to input works. It looks like

It does. I'm sending this patch based on internal fix I did to read the
value via sysfs. It's already tested and validated :)

> byt_gpio_direction_input() still needs to make sure OUTPUT is set.

That's handled by BYT_DIR_MASK here:

        value = readl(reg) | BYT_DIR_MASK;
        value &= ~BYT_INPUT_EN;         /* active low */

Br, David
--
To unsubscribe from this list: send the line "unsubscribe linux-gpio" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch
diff mbox

diff --git a/drivers/pinctrl/pinctrl-baytrail.c b/drivers/pinctrl/pinctrl-baytrail.c
index e12e5b0..69f882d 100644
--- a/drivers/pinctrl/pinctrl-baytrail.c
+++ b/drivers/pinctrl/pinctrl-baytrail.c
@@ -290,6 +290,7 @@  static int byt_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
 
 	value = readl(reg) | BYT_DIR_MASK;
 	value &= ~BYT_INPUT_EN;		/* active low */
+	value |= BYT_OUTPUT_EN;		/* disable output */
 	writel(value, reg);
 
 	spin_unlock_irqrestore(&vg->lock, flags);
@@ -319,6 +320,7 @@  static int byt_gpio_direction_output(struct gpio_chip *chip,
 
 	reg_val = readl(reg) | BYT_DIR_MASK;
 	reg_val &= ~BYT_OUTPUT_EN;
+	value |= BYT_INPUT_EN;		/* disable input */
 
 	if (value)
 		writel(reg_val | BYT_LEVEL, reg);