From patchwork Thu Dec 10 14:58:39 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 58223 Delivered-To: patch@linaro.org Received: by 10.112.147.194 with SMTP id tm2csp544301lbb; Thu, 10 Dec 2015 06:58:41 -0800 (PST) X-Received: by 10.66.123.72 with SMTP id ly8mr17162057pab.19.1449759521649; Thu, 10 Dec 2015 06:58:41 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h10si20792424pat.126.2015.12.10.06.58.41; Thu, 10 Dec 2015 06:58:41 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-gpio-owner@vger.kernel.org; dkim=neutral (body hash did not verify) header.i=@linaro-org.20150623.gappssmtp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753438AbbLJO6l (ORCPT + 4 others); Thu, 10 Dec 2015 09:58:41 -0500 Received: from mail-oi0-f49.google.com ([209.85.218.49]:34488 "EHLO mail-oi0-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750849AbbLJO6k (ORCPT ); Thu, 10 Dec 2015 09:58:40 -0500 Received: by oigl9 with SMTP id l9so7942822oig.1 for ; Thu, 10 Dec 2015 06:58:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro-org.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=GuhlCOWUUbtqaVHr0ffDvW8yMYIH3e1CzfjNJgLhTO4=; b=RT9Jc6i4LCwTKa+bCSRUha0O2gvwEXyBskgl8b52SeMLsi5v7VUEolNMO2egY2Wqyj tM0KPXxX1FypO+hParZ0JtaVNec9vfCHxG1+oYPo1Ed5+saRJ9jLhZhRlbL+bKA6tjln dDkZWEQjK2SNUXvvzIOxGwcIn6PJy/6S1jyVGSnNNGa1KjNr+Tka9dX2DBOTKNeUHka3 DbsE8CxpwcqQxrERGsYhxYqRibdwOZGwyDJbXdxM1NPpd4gfzkwo4RYb1vuu+uPlSlP+ FAsCHd9SBFnCtmdvxD6DvH9HiB/NQCE8gHa+cuTAXQXFYue0di4slx+pLiR9mCpxajBY gX3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=GuhlCOWUUbtqaVHr0ffDvW8yMYIH3e1CzfjNJgLhTO4=; b=byBpo+m8S3Bb5t1qi6xkvNcOsNanbaUaLtYm/ZnsR4g29WJVwrqvf7MyvGseNcGF3Q b9aWICdtbj9mDoeWK+Hb9bjEhokRw00b0erNpI55Mo5VtggOjPr/lNchJr7MR15rv3Dk gGZtQ96NuBXP0xPHkcOdgC784WdVUY3jol7yM3onUfuNvPVeiEqIX6F84gQUFkRcC/5m omzgT1HBFUQdo7s0bXxEih44W2eWNN/eMkVy4c0folEPOc82w4p9dV3c6Gm4EJEVwq48 pxDQxlhI36YlI1EPaDEveszwms+THiDpqxFiRKfrfYDjjdvVv04rLDkQbPmxr/z487NO CnTQ== X-Gm-Message-State: ALoCoQlEBym8NTxNkEugNwkEdspVBxJctT+5yCnrx24JOCsh6xJ4954W1faK8/aPwZGHR2EZChlQONAqx7JKnsktbdqBZRsiZyzmwZRHqpJ8a/KF6WZK26w= MIME-Version: 1.0 X-Received: by 10.202.195.202 with SMTP id t193mr9420914oif.94.1449759519385; Thu, 10 Dec 2015 06:58:39 -0800 (PST) Received: by 10.182.32.70 with HTTP; Thu, 10 Dec 2015 06:58:39 -0800 (PST) In-Reply-To: <20151128230200.GA21951@archie.tuxnet.lan> References: <20151128230200.GA21951@archie.tuxnet.lan> Date: Thu, 10 Dec 2015 15:58:39 +0100 Message-ID: Subject: Re: gpio-mxc: gpiod_get_value returns INT_MIN when the GPIO is off From: Linus Walleij To: Clemens Gruber , Ulises Brindis , Vladimir Zapolskiy , =?UTF-8?Q?Uwe_Kleine=2DK=C3=B6nig?= , Fabio Estevam Cc: "linux-gpio@vger.kernel.org" , Alexandre Courbot , "linux-kernel@vger.kernel.org" Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org On Sun, Nov 29, 2015 at 12:02 AM, Clemens Gruber wrote: > 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>; So IIUC this us using drivers/gpio/gpio-mxc.c, correct? > 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 = ; > > 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? No it looks like a bug to me, sadly I'm not really using generic GPIO with any hardware enough. I cooked up the following patch, can you test it (tell me if you want me to send it separately): >From 6e9a115f7a9f70fede21e63630e0d7b01661cf7c Mon Sep 17 00:00:00 2001 From: Linus Walleij Date: Thu, 10 Dec 2015 15:55:29 +0100 Subject: [PATCH] gpio: generic: clamp values from bgpio_get_set() The bgpio_get_set() call should return a value clamped to [0,1], the current code will return a negative value if reading bit 31, which gets interpreted as an error by the gpiolib core. Reported-by: Clemens Gruber Signed-off-by: Linus Walleij --- drivers/gpio/gpio-generic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) static int bgpio_get(struct gpio_chip *gc, unsigned int gpio) -- 2.4.3 Yours, Linus Walleij -- 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 diff --git a/drivers/gpio/gpio-generic.c b/drivers/gpio/gpio-generic.c index 72088028d7a9..ea581dc23d44 100644 --- a/drivers/gpio/gpio-generic.c +++ b/drivers/gpio/gpio-generic.c @@ -141,9 +141,9 @@ static int bgpio_get_set(struct gpio_chip *gc, unsigned int gpio) unsigned long pinmask = bgc->pin2mask(bgc, gpio); if (bgc->dir & pinmask) - return bgc->read_reg(bgc->reg_set) & pinmask; + return !!(bgc->read_reg(bgc->reg_set) & pinmask); else - return bgc->read_reg(bgc->reg_dat) & pinmask; + return !!(bgc->read_reg(bgc->reg_dat) & pinmask); }