linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Clemens Gruber <clemens.gruber@pqgruber.com>
To: linux-gpio@vger.kernel.org
Cc: Linus Walleij <linus.walleij@linaro.org>,
	Alexandre Courbot <gnurou@gmail.com>,
	linux-kernel@vger.kernel.org
Subject: gpio-mxc: gpiod_get_value returns INT_MIN when the GPIO is off
Date: Sun, 29 Nov 2015 00:02:01 +0100	[thread overview]
Message-ID: <20151128230200.GA21951@archie.tuxnet.lan> (raw)

Hi,

I am writing a module which reads several GPIO input states from a
Freescale i.MX6Q. I can control the input states through hardware
buttons attached to my development board. They are all connected exactly
the same, with external pull up resistors.
I am using the current torvalds/linux master branch.

The list in my DT looks like this:
my-gpios = <&gpio1 7 GPIO_ACTIVE_LOW>,
	<&gpio1 8 GPIO_ACTIVE_LOW>,
	/* .... */
	<&gpio6 31 GPIO_ACTIVE_LOW>,
	<&gpio7 11 GPIO_ACTIVE_LOW>;

In my platform driver module, I first call gpiod_get_array(dev, "my",
GPIOD_IN);

Then I can successfully read in the input states of my GPIOs and get
"correct" values for all but one of them: GPIO6_31
Meaning: When no button is pressed, every call to
gpiod_get_value(my_gpios->desc[i]) returns 0 and if one of the buttons
is pressed, it returns 1.
Except for gpio6 31: It returns -2^31 (INT_MIN) when no button is
pressed and 1 if a button is pressed.

I double checked the pinmuxing and the hardware, the input state should
be off / 0 and not -2^31. The internal pull up is also enabled.
Muxing: fsl,pins = <MX6QDL_PAD_EIM_BCLK__GPIO6_IO31 0x1b099>;

I read through the source of gpio-generic.c and came across
bgpio_get_set. If I interpret the code correctly, it is set as the get
function of the gpio chip. (because BGPIOF_READ_OUTPUT_REG_SET is passed
as flags argument when bgpio_init is called from gpio-mxc.c)
In the case of gpio6_31, the pin2mask function would return 1 << 31 as
unsigned long, meaning 2^31 and then the unsigned long return value of
read_reg is bitmasked with 2^31 which would result in 2^31 if it is on
and 0 if it is off.
But because gpiod_get_value returns an int, the 2^31 unsigned long is
casted to a signed int, which means it returns -2^31 if it is active.
But in this case, it returns -2^31 if it is inactive and 1 if it is
active...

I can workaround by checking gpiod_get_value(desc) == 1 but I'd really
like to know what's causing this (odd?) behavior.

Am I doing something wrong here?

Thanks,
Clemens

             reply	other threads:[~2015-11-28 23:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-28 23:02 Clemens Gruber [this message]
2015-12-10 14:58 ` gpio-mxc: gpiod_get_value returns INT_MIN when the GPIO is off Linus Walleij
2015-12-10 17:18   ` Clemens Gruber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20151128230200.GA21951@archie.tuxnet.lan \
    --to=clemens.gruber@pqgruber.com \
    --cc=gnurou@gmail.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).