From patchwork Sat Oct 12 01:56:23 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1175633 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="YtI3n15T"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46qnxl1J2dz9s7T for ; Sat, 12 Oct 2019 12:56:59 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727117AbfJLB46 (ORCPT ); Fri, 11 Oct 2019 21:56:58 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:45605 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727345AbfJLB46 (ORCPT ); Fri, 11 Oct 2019 21:56:58 -0400 Received: by mail-pl1-f194.google.com with SMTP id u12so5239660pls.12 for ; Fri, 11 Oct 2019 18:56:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=fGwiK1J0mM5vfviiuZRXy7lRh5kuzlTW61EA743PqAA=; b=YtI3n15TzREOvRNBbHxAVp4Ha+v14oJdS0o6RpHEAzSUK2zoX3yN6FJzVsIKLvLdJp ZgvXWqGj/JL/yF/QKWPFK7+E9hIK7e8R43N14AAkLDQIWqwAOiIO9BXniBKI4A1p7VaF EBsM16yYETP9/UdSbp+qzWQPtfMlMwIgfTggff+PuWJV7kP/NccXIihCdBuXUFOpv19/ rloryLvQKp9t1+B7CirseI8Pfj407y8CzxFk9X/IN3ljWsjhwdZhr+pT1HxBgBPSULPQ pKO0yJN/m8f4pr6xOzSa+mg84N2sLCyg7E0x8iwPiAbCHNdUwhcDGpHG0kSNKgkYDXed UB/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fGwiK1J0mM5vfviiuZRXy7lRh5kuzlTW61EA743PqAA=; b=SJDxBsIp8FAwbbxqslW/tu/KwicEPJBCcw759dFVR8kmROUysUi0taNSAXqGj1adJn Y7kzhjtzP2kQOkd6b4VZepK4xkfbH7bCvs1SZonWigR2/Bxb5f4TVXikG+U9ra/L1FzS dYSCkcZ0iRL/zPZ5/SFuhSKMU0mZs//ZkTCi7hmFdOPKEnzaTnoQ9/ApoQIjr5AsvD2f IUvk1zp2EA79Y+peldhbTqZVKXD0GOwzUT0H4bRwuehcacAiuxkb9QBIQXfiyEPCIeWE NxkvtmkCER4BFivo0hzj8mgJkgGK1wSLhsYRp1gqQGM40L3/rme3WQ7dOpHADrW+Ol2U 7PQw== X-Gm-Message-State: APjAAAX1Pp5MmXivVFQz/nNogMV9HxXxVxeomE/CqE7sI4hYUh25nyA0 b8gT2Oc1sWkffp+VWAcQIZ2tWy2ua3g/uw== X-Google-Smtp-Source: APXvYqzt2jegYH204sS4S8u4k+stbkHOxN6hs4yJTBeSQJnRxh1LFGVwra8yyY9b70EQtB4Yp7+YUQ== X-Received: by 2002:a17:902:222:: with SMTP id 31mr18434713plc.169.1570845417103; Fri, 11 Oct 2019 18:56:57 -0700 (PDT) Received: from sol.lan (220-235-84-126.dyn.iinet.net.au. [220.235.84.126]) by smtp.gmail.com with ESMTPSA id b20sm12042242pff.158.2019.10.11.18.56.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2019 18:56:56 -0700 (PDT) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl, linus.walleij@linaro.org, bamv2005@gmail.com Cc: drew@pdp7.com, Bartosz Golaszewski Subject: [PATCH v2 1/6] gpio: expose pull-up/pull-down line flags to userspace Date: Sat, 12 Oct 2019 09:56:23 +0800 Message-Id: <20191012015628.9604-2-warthog618@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191012015628.9604-1-warthog618@gmail.com> References: <20191012015628.9604-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Drew Fustini Add pull-up/pull-down flags to the gpio line get and set ioctl() calls. Use cases include a push button that does not have an external resistor. Addition use cases described by Limor Fried (ladyada) of Adafruit in this PR for Adafruit_Blinka Python lib: https://github.com/adafruit/Adafruit_Blinka/pull/59 Signed-off-by: Drew Fustini [Bartosz: added flag sanitization] Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 17 +++++++++++++++++ include/uapi/linux/gpio.h | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index bdbc1649eafa..9d2a5e2f6e77 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -421,6 +421,8 @@ struct linehandle_state { (GPIOHANDLE_REQUEST_INPUT | \ GPIOHANDLE_REQUEST_OUTPUT | \ GPIOHANDLE_REQUEST_ACTIVE_LOW | \ + GPIOHANDLE_REQUEST_PULL_UP | \ + GPIOHANDLE_REQUEST_PULL_DOWN | \ GPIOHANDLE_REQUEST_OPEN_DRAIN | \ GPIOHANDLE_REQUEST_OPEN_SOURCE) @@ -537,6 +539,11 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) (lflags & GPIOHANDLE_REQUEST_OUTPUT)) return -EINVAL; + /* Same with pull-up and pull-down. */ + if ((lflags & GPIOHANDLE_REQUEST_PULL_UP) && + (lflags & GPIOHANDLE_REQUEST_PULL_DOWN)) + return -EINVAL; + /* * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If * the hardware actually supports enabling both at the same time the @@ -592,6 +599,10 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) set_bit(FLAG_OPEN_DRAIN, &desc->flags); if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE) set_bit(FLAG_OPEN_SOURCE, &desc->flags); + if (lflags & GPIOHANDLE_REQUEST_PULL_DOWN) + set_bit(FLAG_PULL_DOWN, &desc->flags); + if (lflags & GPIOHANDLE_REQUEST_PULL_UP) + set_bit(FLAG_PULL_UP, &desc->flags); ret = gpiod_set_transitory(desc, false); if (ret < 0) @@ -1097,6 +1108,10 @@ static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) lineinfo.flags |= (GPIOLINE_FLAG_OPEN_SOURCE | GPIOLINE_FLAG_IS_OUT); + if (test_bit(FLAG_PULL_DOWN, &desc->flags)) + lineinfo.flags |= GPIOLINE_FLAG_PULL_DOWN; + if (test_bit(FLAG_PULL_UP, &desc->flags)) + lineinfo.flags |= GPIOLINE_FLAG_PULL_UP; if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) return -EFAULT; @@ -2770,6 +2785,8 @@ static bool gpiod_free_commit(struct gpio_desc *desc) clear_bit(FLAG_REQUESTED, &desc->flags); clear_bit(FLAG_OPEN_DRAIN, &desc->flags); clear_bit(FLAG_OPEN_SOURCE, &desc->flags); + clear_bit(FLAG_PULL_UP, &desc->flags); + clear_bit(FLAG_PULL_DOWN, &desc->flags); clear_bit(FLAG_IS_HOGGED, &desc->flags); ret = true; } diff --git a/include/uapi/linux/gpio.h b/include/uapi/linux/gpio.h index 4ebfe0ac6c5b..c2d1f7d908d6 100644 --- a/include/uapi/linux/gpio.h +++ b/include/uapi/linux/gpio.h @@ -33,6 +33,8 @@ struct gpiochip_info { #define GPIOLINE_FLAG_ACTIVE_LOW (1UL << 2) #define GPIOLINE_FLAG_OPEN_DRAIN (1UL << 3) #define GPIOLINE_FLAG_OPEN_SOURCE (1UL << 4) +#define GPIOLINE_FLAG_PULL_UP (1UL << 5) +#define GPIOLINE_FLAG_PULL_DOWN (1UL << 6) /** * struct gpioline_info - Information about a certain GPIO line @@ -62,6 +64,8 @@ struct gpioline_info { #define GPIOHANDLE_REQUEST_ACTIVE_LOW (1UL << 2) #define GPIOHANDLE_REQUEST_OPEN_DRAIN (1UL << 3) #define GPIOHANDLE_REQUEST_OPEN_SOURCE (1UL << 4) +#define GPIOHANDLE_REQUEST_PULL_UP (1UL << 5) +#define GPIOHANDLE_REQUEST_PULL_DOWN (1UL << 6) /** * struct gpiohandle_request - Information about a GPIO handle request From patchwork Sat Oct 12 01:56:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1175634 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="uRpmcaAy"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46qnxr0VhXz9s7T for ; Sat, 12 Oct 2019 12:57:04 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728111AbfJLB5D (ORCPT ); Fri, 11 Oct 2019 21:57:03 -0400 Received: from mail-pl1-f195.google.com ([209.85.214.195]:44460 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727345AbfJLB5D (ORCPT ); Fri, 11 Oct 2019 21:57:03 -0400 Received: by mail-pl1-f195.google.com with SMTP id q15so5249388pll.11 for ; Fri, 11 Oct 2019 18:57:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Qv1ffV1J2AHPgs2jYfDAyydiPGBjRAY5RJPIZ/rc3d4=; b=uRpmcaAyU4eDoRzSEYlaVk8PtxfJabPDZyIuZQdiojVmpeje8eh+yC4VLCjk07Qs6O Wm5Z4r49OVCHLXDkkiRtOkLeFE3nwxltdBaVzWx2iFXGponkUD6+Wk+0IzFQf26UI8uI O4Q2EQzoQFfGl744mrt4FcoepZv9Vf6y2EH4oIh1sxrM9NA4NHBqH2+CZYyOZbpCAjQ8 tO+pGia1SFe39uCDXvD1kqCztNlEIcaHtQDDKP5336yAEwqRho8vaCqQLygR2GsMKmmP krg6VFyE2I2tl+jK5sy0LZLNt6PwSS+Jiv37U9VTLYPNFUU9B3ks7+neOSvPeQxiFsT9 lvyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Qv1ffV1J2AHPgs2jYfDAyydiPGBjRAY5RJPIZ/rc3d4=; b=o8ya3HuQbC6GEQdLJwzQ8qPZnRtG8+vZ/ZOZNS2hSARaPxbOpiBLORjwFbMt33xqBq KXWS1SAbKiazbDp5zjMHEsdvajOhKC7p89BuYAPIkR6TqQBKctJyR8dbdz+eRF/OjDsG wh49Udr1MJ1OAPe3Bqzpi7G6tmRIck2OAIZDDmc/MuWZvUtQyLVDTOdL4esVR9G6U/+o bXC3U9XOh0ICrSrvnF9vPpZIKbFqYGsjoClpKOiGPbqzENu6RZwfuPbbjNSChzrBqa+T kCesTNyT51OJQuuuFDEUfbJ/ZMvZzotvh5JDodKCyVfFd0C5NKBgaHKoeyXvsvwR7A0x UyhA== X-Gm-Message-State: APjAAAVfTiHbCqOK11rv3fl+X1GTyTscxQ8AJXknW2mNN0axyzSs34Za TLRfUEHMqxYg41HFOpLI2awJ5EGzC88vHQ== X-Google-Smtp-Source: APXvYqzYznrBg5yu4H0eoJ7zNhjuwvHCOv6pYT9ko7A9B/ARd65nvkWdGONE2YfWlQG+oVo+nTq6zA== X-Received: by 2002:a17:902:bf0a:: with SMTP id bi10mr18633811plb.56.1570845420596; Fri, 11 Oct 2019 18:57:00 -0700 (PDT) Received: from sol.lan (220-235-84-126.dyn.iinet.net.au. [220.235.84.126]) by smtp.gmail.com with ESMTPSA id b20sm12042242pff.158.2019.10.11.18.56.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2019 18:57:00 -0700 (PDT) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl, linus.walleij@linaro.org, bamv2005@gmail.com Cc: drew@pdp7.com, Kent Gibson Subject: [PATCH v2 2/6] gpiolib: add support for pull up/down to lineevent_create Date: Sat, 12 Oct 2019 09:56:24 +0800 Message-Id: <20191012015628.9604-3-warthog618@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191012015628.9604-1-warthog618@gmail.com> References: <20191012015628.9604-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org This patch adds support for pull up/down to lineevent_create. Use cases include receiving asynchronous presses from a push button without an external pull up/down. Move all the flags sanitization before any memory allocation in lineevent_create() in order to remove a couple unneeded gotos. (from Bartosz Golaszewski ) Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib.c | 60 +++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 9d2a5e2f6e77..053847b6187f 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -905,6 +905,38 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) if (copy_from_user(&eventreq, ip, sizeof(eventreq))) return -EFAULT; + offset = eventreq.lineoffset; + lflags = eventreq.handleflags; + eflags = eventreq.eventflags; + + if (offset >= gdev->ngpio) + return -EINVAL; + + /* Return an error if a unknown flag is set */ + if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || + (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) + return -EINVAL; + + /* This is just wrong: we don't look for events on output lines */ + if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || + (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || + (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) + return -EINVAL; + + /* PULL_UP and PULL_DOWN flags only make sense for input mode. */ + if (!(lflags & GPIOHANDLE_REQUEST_INPUT) && + ((lflags & GPIOHANDLE_REQUEST_PULL_UP) || + (lflags & GPIOHANDLE_REQUEST_PULL_DOWN))) + return -EINVAL; + + /* + * Do not allow both pull-up and pull-down flags to be set as they + * are contradictory. + */ + if ((lflags & GPIOHANDLE_REQUEST_PULL_UP) && + (lflags & GPIOHANDLE_REQUEST_PULL_DOWN)) + return -EINVAL; + le = kzalloc(sizeof(*le), GFP_KERNEL); if (!le) return -ENOMEM; @@ -922,30 +954,6 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) } } - offset = eventreq.lineoffset; - lflags = eventreq.handleflags; - eflags = eventreq.eventflags; - - if (offset >= gdev->ngpio) { - ret = -EINVAL; - goto out_free_label; - } - - /* Return an error if a unknown flag is set */ - if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || - (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) { - ret = -EINVAL; - goto out_free_label; - } - - /* This is just wrong: we don't look for events on output lines */ - if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || - (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || - (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) { - ret = -EINVAL; - goto out_free_label; - } - desc = &gdev->descs[offset]; ret = gpiod_request(desc, le->label); if (ret) @@ -955,6 +963,10 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW) set_bit(FLAG_ACTIVE_LOW, &desc->flags); + if (lflags & GPIOHANDLE_REQUEST_PULL_DOWN) + set_bit(FLAG_PULL_DOWN, &desc->flags); + if (lflags & GPIOHANDLE_REQUEST_PULL_UP) + set_bit(FLAG_PULL_UP, &desc->flags); ret = gpiod_direction_input(desc); if (ret) From patchwork Sat Oct 12 01:56:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1175635 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="mhHSoPhJ"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46qnxs5YS8z9s7T for ; Sat, 12 Oct 2019 12:57:05 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728442AbfJLB5F (ORCPT ); Fri, 11 Oct 2019 21:57:05 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:45918 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727345AbfJLB5F (ORCPT ); Fri, 11 Oct 2019 21:57:05 -0400 Received: by mail-pf1-f196.google.com with SMTP id y72so7075422pfb.12 for ; Fri, 11 Oct 2019 18:57:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9yM+7slsNLJm2jMU2/8cO8XX3jrRvDxcj2OR+Xd2zxk=; b=mhHSoPhJ0mPyKq+x1R0ViVgGWoxCKcMPdTBMCPzYENHXbJmujHK3lWGk+0q6e/x9hh J/l2czViPPIseswxFFL3eEgWzniRDQOMJdTrqFp0Rh6ZZonagqeJAGS4eo1hFaPt3zp8 YCvcnTXepDLrpWd5PXuaxkppACj5X0j2D/I9bgqJqMFEzO/7aOqcdJ7OHx76pdUbFLXl Olqo0g4ixpzJAhXK/m+TRFU75XbeGzp4uWxhTfabjdeoZVnHhtLiq9njH329s6ACLZGQ NT5vRxbJBzy4lKVAcRWc0eLTgF/qd4U1NkyaxhY7pbmFx29i9Gt+yoOQreQQG8oc4OEk YuWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9yM+7slsNLJm2jMU2/8cO8XX3jrRvDxcj2OR+Xd2zxk=; b=bkhSiupQ4owj6h8f+stxXTYNyEIfQMlDHxhgLMi1JvUz83EB1BspDzCs/pOp12YZ1O tc/uZvgUPBNhZwGS6uFq87r0DKvygCFwPSaMhWLDhrfdT5PTgo1e+arIT1yRUE9XV5t8 BFWoli8TEpUy7Dm9k8o+bQifXTtQaeVwIlwW/ds0+lAMdNlBFq9RiJObwZ+rqvYh6jlS kxGnV4SQiWdrbfz7msaObNApqNBx7XJ8jeO4RQeH+7QPotqlF+kEGz4dJvjJEd+zF/RZ DHUWvXj5b45thgaG4cFL5xBHdNDoA2sH1SHzYZhCZkUT96mUjS6Ejik2ckANX+pn4EMD q55w== X-Gm-Message-State: APjAAAWSGaJ8lYeQCEfxN+zM+tC7NOE7hbZwS4b7uJHYhqhgywlTmm+H Wddw6jCInVOgN0r57S8Ef8qNAEAdC/JF0w== X-Google-Smtp-Source: APXvYqyo7bF1WJDVRXZa4XRiL3uVXCvH7ghxRJZ8SIcwcS4tptcf/Ec7r1pFYwrHa0h/TQcGbAxyiA== X-Received: by 2002:a17:90a:aa0a:: with SMTP id k10mr20706200pjq.13.1570845423881; Fri, 11 Oct 2019 18:57:03 -0700 (PDT) Received: from sol.lan (220-235-84-126.dyn.iinet.net.au. [220.235.84.126]) by smtp.gmail.com with ESMTPSA id b20sm12042242pff.158.2019.10.11.18.57.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2019 18:57:03 -0700 (PDT) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl, linus.walleij@linaro.org, bamv2005@gmail.com Cc: drew@pdp7.com, Kent Gibson Subject: [PATCH v2 3/6] gpio: mockup: add set_config to support pull up/down Date: Sat, 12 Oct 2019 09:56:25 +0800 Message-Id: <20191012015628.9604-4-warthog618@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191012015628.9604-1-warthog618@gmail.com> References: <20191012015628.9604-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org This patch adds support for the pull up/down state set via gpiolib line requests to be reflected in the state of the mockup. Use case is for testing of the GPIO uAPI, specifically the pull up/down flags. Signed-off-by: Kent Gibson --- drivers/gpio/gpio-mockup.c | 94 ++++++++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 34 deletions(-) diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c index 213aedc97dc2..493077229677 100644 --- a/drivers/gpio/gpio-mockup.c +++ b/drivers/gpio/gpio-mockup.c @@ -146,6 +146,61 @@ static void gpio_mockup_set_multiple(struct gpio_chip *gc, mutex_unlock(&chip->lock); } +static int gpio_mockup_apply_pull(struct gpio_mockup_chip *chip, + unsigned int offset, int value) +{ + struct gpio_desc *desc; + struct gpio_chip *gc; + struct irq_sim *sim; + int curr, irq, irq_type; + + gc = &chip->gc; + desc = &gc->gpiodev->descs[offset]; + sim = &chip->irqsim; + + mutex_lock(&chip->lock); + + if (test_bit(FLAG_REQUESTED, &desc->flags) && + !test_bit(FLAG_IS_OUT, &desc->flags)) { + curr = __gpio_mockup_get(chip, offset); + if (curr == value) + goto out; + + irq = irq_sim_irqnum(sim, offset); + irq_type = irq_get_trigger_type(irq); + + if ((value == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) || + (value == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING))) + irq_sim_fire(sim, offset); + } + + /* Change the value unless we're actively driving the line. */ + if (!test_bit(FLAG_REQUESTED, &desc->flags) || + !test_bit(FLAG_IS_OUT, &desc->flags)) + __gpio_mockup_set(chip, offset, value); + +out: + chip->lines[offset].pull = value; + mutex_unlock(&chip->lock); + return 0; +} + +static int gpio_mockup_set_config(struct gpio_chip *gc, + unsigned int offset, unsigned long config) +{ + struct gpio_mockup_chip *chip = gpiochip_get_data(gc); + + switch (pinconf_to_config_param(config)) { + case PIN_CONFIG_BIAS_PULL_UP: + return gpio_mockup_apply_pull(chip, offset, 1); + case PIN_CONFIG_BIAS_PULL_DOWN: + return gpio_mockup_apply_pull(chip, offset, 0); + default: + break; + } + return -ENOTSUPP; +} + static int gpio_mockup_dirout(struct gpio_chip *gc, unsigned int offset, int value) { @@ -226,12 +281,8 @@ static ssize_t gpio_mockup_debugfs_write(struct file *file, size_t size, loff_t *ppos) { struct gpio_mockup_dbgfs_private *priv; - int rv, val, curr, irq, irq_type; - struct gpio_mockup_chip *chip; + int rv, val; struct seq_file *sfile; - struct gpio_desc *desc; - struct gpio_chip *gc; - struct irq_sim *sim; if (*ppos != 0) return -EINVAL; @@ -244,35 +295,9 @@ static ssize_t gpio_mockup_debugfs_write(struct file *file, sfile = file->private_data; priv = sfile->private; - chip = priv->chip; - gc = &chip->gc; - desc = &gc->gpiodev->descs[priv->offset]; - sim = &chip->irqsim; - - mutex_lock(&chip->lock); - - if (test_bit(FLAG_REQUESTED, &desc->flags) && - !test_bit(FLAG_IS_OUT, &desc->flags)) { - curr = __gpio_mockup_get(chip, priv->offset); - if (curr == val) - goto out; - - irq = irq_sim_irqnum(sim, priv->offset); - irq_type = irq_get_trigger_type(irq); - - if ((val == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) || - (val == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING))) - irq_sim_fire(sim, priv->offset); - } - - /* Change the value unless we're actively driving the line. */ - if (!test_bit(FLAG_REQUESTED, &desc->flags) || - !test_bit(FLAG_IS_OUT, &desc->flags)) - __gpio_mockup_set(chip, priv->offset, val); - -out: - chip->lines[priv->offset].pull = val; - mutex_unlock(&chip->lock); + rv = gpio_mockup_apply_pull(priv->chip, priv->offset, val); + if (rv) + return rv; return size; } @@ -418,6 +443,7 @@ static int gpio_mockup_probe(struct platform_device *pdev) gc->direction_output = gpio_mockup_dirout; gc->direction_input = gpio_mockup_dirin; gc->get_direction = gpio_mockup_get_direction; + gc->set_config = gpio_mockup_set_config; gc->to_irq = gpio_mockup_to_irq; gc->free = gpio_mockup_free; From patchwork Sat Oct 12 01:56:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1175636 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="FXBe8Trl"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46qnxw5H66z9s7T for ; Sat, 12 Oct 2019 12:57:08 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728608AbfJLB5I (ORCPT ); Fri, 11 Oct 2019 21:57:08 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:33548 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727345AbfJLB5I (ORCPT ); Fri, 11 Oct 2019 21:57:08 -0400 Received: by mail-pf1-f194.google.com with SMTP id q10so7107970pfl.0 for ; Fri, 11 Oct 2019 18:57:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xpqXB5DVOgvUUCUlvpMuCi1tvRwuyVvNOfMh9KSgay0=; b=FXBe8TrlXaPSbSZFRa74CyAwD/RIiwsTRExQike+lb545vTKzaSUNRtxdVZlhIDDB8 Tnd4J/zBpYTu6Zk07hQONVpl6VSBLECmSk2DYKgwubCEcVfbff39k63yItypzrj0PlAz 8wDUHyvQBozP801GhOMTvOcvj3OZs8qhph2qz316C8GdBCj5upKm741sD5LEQe78XeGy 8kpSJFxdfncsE4VcuJvw/Jg+AE4pYhwtAhXY4j+ODY/SvLZahzN2llB/JH4kBdeQX4zj j7hEqmZxmpA88ZL1PBtXuFahki3Yl3ym4QoGe3bVz+PYuQrp4jRlJcNCGWiaUQAm5/YY 1xcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xpqXB5DVOgvUUCUlvpMuCi1tvRwuyVvNOfMh9KSgay0=; b=RKV2sEy4JiCAhIQjkqMI1jgrImVQgkSJ19xZfIXrK3yOXdi8ZZbmxkgPmPMtCM+GDY ahPNp8Tx7XQwIoibmkYVVgPQGdrBhpyDwGoycFdtEa/pPsLi3qMVAAKUpnMj/Ke+0XDl U3RUEqzhfU2O5NEwgIJ7iF5to9Kql7kKkJ7ckBE7f86XhauVmpc58M+OaGS+lUGvrSSv y7Wou4CPxufrsy/75XSr0AAkXHPM7tbggk2GWT3EkqTW2VvYMo1zILnQF3kj3fcgEMfH LVmBQ2gF8cVi0VT6FEypJhxhX/kksJCNb31de7QFmL4y4r4qh3AxYNwBnIiE/YEmIwRN LYZw== X-Gm-Message-State: APjAAAVXb6RPwXdMiPHkk9RnhEF3NqUquWmim+rhhtvWRpTfqndtG/rZ 2XlVhWzXCpq3SW7xk/T9Kv+2H/9CCX6N8A== X-Google-Smtp-Source: APXvYqw0AfFcuzmcit425jS34oNLhDguYWe8vU3poxriTG/943NPf+dEJxdoVUv1Xop3afc1iNrmrg== X-Received: by 2002:a65:5bca:: with SMTP id o10mr19688070pgr.74.1570845427148; Fri, 11 Oct 2019 18:57:07 -0700 (PDT) Received: from sol.lan (220-235-84-126.dyn.iinet.net.au. [220.235.84.126]) by smtp.gmail.com with ESMTPSA id b20sm12042242pff.158.2019.10.11.18.57.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2019 18:57:06 -0700 (PDT) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl, linus.walleij@linaro.org, bamv2005@gmail.com Cc: drew@pdp7.com, Kent Gibson Subject: [PATCH v2 4/6] gpiolib: pull requires explicit input mode Date: Sat, 12 Oct 2019 09:56:26 +0800 Message-Id: <20191012015628.9604-5-warthog618@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191012015628.9604-1-warthog618@gmail.com> References: <20191012015628.9604-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org This patch prevents pull up/down flags being applied to as-is line requests, which should be left as-is, and for output mode for which setting pulls is not currently supported. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 053847b6187f..647334f53622 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -559,6 +559,12 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))) return -EINVAL; + /* PULL_UP and PULL_DOWN flags only make sense for input mode. */ + if (!(lflags & GPIOHANDLE_REQUEST_INPUT) && + ((lflags & GPIOHANDLE_REQUEST_PULL_UP) || + (lflags & GPIOHANDLE_REQUEST_PULL_DOWN))) + return -EINVAL; + lh = kzalloc(sizeof(*lh), GFP_KERNEL); if (!lh) return -ENOMEM; From patchwork Sat Oct 12 01:56:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1175637 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Fkvq2sLv"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46qny00v2fz9s7T for ; Sat, 12 Oct 2019 12:57:12 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728171AbfJLB5L (ORCPT ); Fri, 11 Oct 2019 21:57:11 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:46401 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727345AbfJLB5L (ORCPT ); Fri, 11 Oct 2019 21:57:11 -0400 Received: by mail-pg1-f196.google.com with SMTP id b8so6749404pgm.13 for ; Fri, 11 Oct 2019 18:57:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=AOT7vFL3O4ZceBzvBdGY8FMxY/oQKy8m5KQbEXrjZS8=; b=Fkvq2sLveLjXEcRLunmQpckHPJHjl6+cMWtyAbsizQ1v6/AwNUQ3KWLQd1k/8r7Ums qKs6BKCr72BTP5dzs1G9UT8FdBGhxrp7fx0copCnVP7J9XEfvoxcicwjfhfKrijCkDfK p8K1fIIEPSSbutUveHUd1KdsmMhzeCBLoVAbrcvP5pX6xkvz0/nGlUnEbPR69Gic/n1L 9b2kQQAyUQESb8z9mwULMOC7msz+g+G0huaV3taCzQ5so+Rpk0Aam0f5HawYH2FE1/Ob bNPrOXbKQ6b4Pwood3LnkcZ5eWMgn4CT1/7HcCMnFGxJPikcDVY2FL5X9I4QZ0Rtu7Ve spCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AOT7vFL3O4ZceBzvBdGY8FMxY/oQKy8m5KQbEXrjZS8=; b=VYI8ICFipfOMH27W/ikc0QDO9JCeb8lKMhisdBMxDjOciTvewk7gFJgLi7nIDBcGRR K1Peeewx4IajAQG/CuVrEI04v/binDe3m4BoF25AkhHbGeDWXHtRahfk2WhFmVFnWuer CXiIH8xtbqYrsSmiY1DrAQWpKFryO5GTDz+qIO545D2BesuGQ3ztfnRINoKMZYE9ZdVK pERAQ7ZMShDTRsJdTAcUQsLVAQ1FGD2FJvoBCGN/a6nDyuuvLYise9RCzHrwYenWjAoc Y0c7cMqbl9qhLnlrtYWBjmS7uFu71Em286oYg2FV/s2w0KTKuHGCVzF4aMu3xEWuutDo tACA== X-Gm-Message-State: APjAAAU7Il0sQQ6nOxsT7O7xYF1oi5SUH84dN7yviNe9wz3YLXop+ipn uL0FbNuNECObiJaBwPc2y7IS61qjjhfccA== X-Google-Smtp-Source: APXvYqyR3XdtAMDR1xDLVV5NdP5i2qzMxDfH+aOOgrmpANGmncC5o9dPCVbOFRTaTxcIVu6PPrMSUA== X-Received: by 2002:a63:1b10:: with SMTP id b16mr216609pgb.235.1570845430571; Fri, 11 Oct 2019 18:57:10 -0700 (PDT) Received: from sol.lan (220-235-84-126.dyn.iinet.net.au. [220.235.84.126]) by smtp.gmail.com with ESMTPSA id b20sm12042242pff.158.2019.10.11.18.57.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2019 18:57:10 -0700 (PDT) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl, linus.walleij@linaro.org, bamv2005@gmail.com Cc: drew@pdp7.com, Kent Gibson Subject: [PATCH v2 5/6] gpiolib: disable bias on inputs when pull up/down are both set Date: Sat, 12 Oct 2019 09:56:27 +0800 Message-Id: <20191012015628.9604-6-warthog618@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191012015628.9604-1-warthog618@gmail.com> References: <20191012015628.9604-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org This patch allows pull up/down bias to be disabled, allowing the line to float or to be biased only by external circuitry. Use case is for where the bias has been applied previously, either by default or by the user, but that setting may conflict with the current use of the line. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 647334f53622..f90b20d548b9 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -539,11 +539,6 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) (lflags & GPIOHANDLE_REQUEST_OUTPUT)) return -EINVAL; - /* Same with pull-up and pull-down. */ - if ((lflags & GPIOHANDLE_REQUEST_PULL_UP) && - (lflags & GPIOHANDLE_REQUEST_PULL_DOWN)) - return -EINVAL; - /* * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If * the hardware actually supports enabling both at the same time the @@ -935,14 +930,6 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) (lflags & GPIOHANDLE_REQUEST_PULL_DOWN))) return -EINVAL; - /* - * Do not allow both pull-up and pull-down flags to be set as they - * are contradictory. - */ - if ((lflags & GPIOHANDLE_REQUEST_PULL_UP) && - (lflags & GPIOHANDLE_REQUEST_PULL_DOWN)) - return -EINVAL; - le = kzalloc(sizeof(*le), GFP_KERNEL); if (!le) return -ENOMEM; @@ -2931,6 +2918,7 @@ static int gpio_set_config(struct gpio_chip *gc, unsigned offset, unsigned arg; switch (mode) { + case PIN_CONFIG_BIAS_DISABLE: case PIN_CONFIG_BIAS_PULL_DOWN: case PIN_CONFIG_BIAS_PULL_UP: arg = 1; @@ -2991,7 +2979,11 @@ int gpiod_direction_input(struct gpio_desc *desc) if (ret == 0) clear_bit(FLAG_IS_OUT, &desc->flags); - if (test_bit(FLAG_PULL_UP, &desc->flags)) + if (test_bit(FLAG_PULL_UP, &desc->flags) && + test_bit(FLAG_PULL_DOWN, &desc->flags)) + gpio_set_config(chip, gpio_chip_hwgpio(desc), + PIN_CONFIG_BIAS_DISABLE); + else if (test_bit(FLAG_PULL_UP, &desc->flags)) gpio_set_config(chip, gpio_chip_hwgpio(desc), PIN_CONFIG_BIAS_PULL_UP); else if (test_bit(FLAG_PULL_DOWN, &desc->flags)) @@ -4462,7 +4454,7 @@ int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, if (lflags & GPIO_PULL_UP) set_bit(FLAG_PULL_UP, &desc->flags); - else if (lflags & GPIO_PULL_DOWN) + if (lflags & GPIO_PULL_DOWN) set_bit(FLAG_PULL_DOWN, &desc->flags); ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY)); From patchwork Sat Oct 12 01:56:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1175638 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="nAcUoxKF"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46qny331L1z9s7T for ; Sat, 12 Oct 2019 12:57:15 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727345AbfJLB5P (ORCPT ); Fri, 11 Oct 2019 21:57:15 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:37389 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726903AbfJLB5O (ORCPT ); Fri, 11 Oct 2019 21:57:14 -0400 Received: by mail-pg1-f194.google.com with SMTP id p1so6772483pgi.4 for ; Fri, 11 Oct 2019 18:57:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=DuhvzQhU38wgFVGeqIOao3QuYRbclUqMBiuip6aGEIU=; b=nAcUoxKFRZ9zVQzRMDnKBVz82qgdatPUx68aQEZEYV88AGgXSQtkklq4ZzoYjeFGeX RdmMKTnY7P1GzxC9kJKiwLYKq6TeF3CTo+6oIQrJtf51LTkZzaZ5fxrdKdn0zlOHryxB 0nfrpqsoMaw/n0oAk3CN3VR5m2YxE9KNUXyB8Tg6Ptwikb7IYMAGfnLrR2L58cB/frVd BYynz4OSWqTK47XPOMwr4/Gnlg7SBnI96cLFLKwom3zGXKpXkbDnDJFvNVSndHTnL2mr K2PX0Fc3664EvbTwZpWcQ1APwElupl2oMOTd0K0xxK42tdSG8RrMSIe1w8pWPU+eqEUk XNtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DuhvzQhU38wgFVGeqIOao3QuYRbclUqMBiuip6aGEIU=; b=LExYYjeOzUcccmRkN0UXAS0TIHF2t9liBwRDjXWa/R83dkVOBTYdnToBIxzrHarQV4 FdKVY5f3UsR7H2iPC68aN9IfnXAUAK+3/f91lTADbPbQ6tniMBSYLSazwXwvtm8awGik cp7HOdoF6ujXbKkkr6DhxXTHsEZ3M0AbY55rQqWuwFk8ru4E73oAyFYzlQah3+CHhzif MFmzREv5N4z2szgU+v+2E0LgM7S4e0DRFxhs4zRTblOju4F/NqY0RUJcfHOkQ1Otvo8s J3YU6erXWkInk+eqJ56S9ywzi1voTHlijthJ4rZothLn64zYEHG3NhRTGAqph4quM87S hoPg== X-Gm-Message-State: APjAAAUWA5Bq4wKACVjsJmS2n8qWlH5KXubBa60hHaYPPx2m6ZbNvIWP CMr684odqt2wPKiMEFo53TeOF0PW0BD/rg== X-Google-Smtp-Source: APXvYqwf2eouH0Y+Hyb15lA2pq6b/7Sd/6l2+0xPbNCPUUFumNjAY3AY32MQk6q3b9VNFz1hR+BGbQ== X-Received: by 2002:a62:8209:: with SMTP id w9mr19755770pfd.5.1570845433903; Fri, 11 Oct 2019 18:57:13 -0700 (PDT) Received: from sol.lan (220-235-84-126.dyn.iinet.net.au. [220.235.84.126]) by smtp.gmail.com with ESMTPSA id b20sm12042242pff.158.2019.10.11.18.57.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2019 18:57:13 -0700 (PDT) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl, linus.walleij@linaro.org, bamv2005@gmail.com Cc: drew@pdp7.com, Kent Gibson Subject: [PATCH v2 6/6] gpiolib: allow pull up/down on outputs Date: Sat, 12 Oct 2019 09:56:28 +0800 Message-Id: <20191012015628.9604-7-warthog618@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191012015628.9604-1-warthog618@gmail.com> References: <20191012015628.9604-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org This patch allows pull up/down bias to be set on outputs. Use case is for open source or open drain applications where internal pull up/down may conflict with external biasing. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index f90b20d548b9..9cc0f9077c7b 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -554,8 +554,9 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))) return -EINVAL; - /* PULL_UP and PULL_DOWN flags only make sense for input mode. */ - if (!(lflags & GPIOHANDLE_REQUEST_INPUT) && + /* PULL_UP and PULL_DOWN flags only allowed for input or output mode. */ + if (!((lflags & GPIOHANDLE_REQUEST_INPUT) || + (lflags & GPIOHANDLE_REQUEST_OUTPUT)) && ((lflags & GPIOHANDLE_REQUEST_PULL_UP) || (lflags & GPIOHANDLE_REQUEST_PULL_DOWN))) return -EINVAL; @@ -2932,6 +2933,24 @@ static int gpio_set_config(struct gpio_chip *gc, unsigned offset, return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP; } +static int gpio_set_bias(struct gpio_chip *chip, struct gpio_desc *desc) +{ + int bias = 0; + + if (test_bit(FLAG_PULL_UP, &desc->flags) && + test_bit(FLAG_PULL_DOWN, &desc->flags)) + bias = PIN_CONFIG_BIAS_DISABLE; + else if (test_bit(FLAG_PULL_UP, &desc->flags)) + bias = PIN_CONFIG_BIAS_PULL_UP; + else if (test_bit(FLAG_PULL_DOWN, &desc->flags)) + bias = PIN_CONFIG_BIAS_PULL_DOWN; + + if (bias) + return gpio_set_config(chip, gpio_chip_hwgpio(desc), bias); + + return 0; +} + /** * gpiod_direction_input - set the GPIO direction to input * @desc: GPIO to set to input @@ -2979,16 +2998,7 @@ int gpiod_direction_input(struct gpio_desc *desc) if (ret == 0) clear_bit(FLAG_IS_OUT, &desc->flags); - if (test_bit(FLAG_PULL_UP, &desc->flags) && - test_bit(FLAG_PULL_DOWN, &desc->flags)) - gpio_set_config(chip, gpio_chip_hwgpio(desc), - PIN_CONFIG_BIAS_DISABLE); - else if (test_bit(FLAG_PULL_UP, &desc->flags)) - gpio_set_config(chip, gpio_chip_hwgpio(desc), - PIN_CONFIG_BIAS_PULL_UP); - else if (test_bit(FLAG_PULL_DOWN, &desc->flags)) - gpio_set_config(chip, gpio_chip_hwgpio(desc), - PIN_CONFIG_BIAS_PULL_DOWN); + gpio_set_bias(chip, desc); trace_gpio_direction(desc_to_gpio(desc), 1, ret); @@ -3114,6 +3124,7 @@ int gpiod_direction_output(struct gpio_desc *desc, int value) } set_output_value: + gpio_set_bias(gc, desc); return gpiod_direction_output_raw_commit(desc, value); } EXPORT_SYMBOL_GPL(gpiod_direction_output);