From patchwork Sun Dec 10 00:23:40 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 121306 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp1204623qgn; Sat, 9 Dec 2017 16:25:50 -0800 (PST) X-Google-Smtp-Source: AGs4zMYVw6NNgB9W4i/jk3lbv/Q0HNI/z5WFdwvMv5wyww4ouzGVa9QHQLxBUOktio9Iekii9Nny X-Received: by 10.159.198.70 with SMTP id y6mr35334178plt.334.1512865550111; Sat, 09 Dec 2017 16:25:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1512865550; cv=none; d=google.com; s=arc-20160816; b=KY8fGv0ehOx8KcXMInefpwY3n5BeLjZRw1EN2Dqugf1zVdcKEyDgsh/vIBT0PB+wgH 0AfNON9qmG7bkT0OmN9XQfiKNIcm66OmzwlHX2cg3EwPJc/jwnksgoF+l9Jd+WDzacq3 V3ActT2Zlv0az332Zb06lIr0VX11qVnZ6rp8fLDKS7siWUIKOAUUSdvDj1CT5gJsugO4 80jn2HNWcSqfFFnpYkplXGQaJzEcXQPTJMVLZAfAi2s1Zb+kewL1Zzd9vCx8xfzSeROM nbvj8EB1Bv/Pzy+Km5Va07XhRc2qsAbA5jzD8jcfAPVPfNfmNCqhJHAr7X1BFfYVZgTT qIbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=Nc1oMhpseKvbuJaV5CS3VXGP8m7u/ohgGWuFzpVdZbU=; b=px88xM8+12R8CuZv2r69MxgymrL2Y7NR20g1QZkxv7A6U2vHW0EMLJ0SkKa0cR0Knx TVA6Jm+LvtkTPknMJmtcp1DWxMurc80PXXNtnpLeVFKbQw/KEgKXVq9Js4AHY97O4YyI w5Y8wVG515rFmXAL020jTWy9TQZubZDPnEbbUnfoAZ3phypw4XolQ8pYOpMj6OajSaqv i9AbXWoDoMlz8Qti8o0d7DAoQdteOZdR5CmAoEluLu+Cgp2Cf38IZiWAyLKa07wbFLu0 xebbhax7q1A4L8G3AFR2BiQ2ZGN/iaNgZPkorrgxYacIUL8oxADXhBwVqgnHESGwqn7X Y4MA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=EPGiZio5; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l4si7707265pgp.735.2017.12.09.16.25.49; Sat, 09 Dec 2017 16:25:50 -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; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=EPGiZio5; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751282AbdLJAZt (ORCPT + 6 others); Sat, 9 Dec 2017 19:25:49 -0500 Received: from mail-lf0-f65.google.com ([209.85.215.65]:40405 "EHLO mail-lf0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751002AbdLJAZs (ORCPT ); Sat, 9 Dec 2017 19:25:48 -0500 Received: by mail-lf0-f65.google.com with SMTP id c140so8645887lfg.7 for ; Sat, 09 Dec 2017 16:25:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=t+nF2YJSnzrJj+gB56fTZx/cjw2L1Zkcjf3JjAPWii8=; b=EPGiZio5lfh0G0pTxX3XsJaCa48r1NW2+jFgOYgphIYUeEelU5Z9guSpw27qTZ/amZ Vj/XTAI2IvKnxIVyia25OhOXqDEpHWW6pVGoR8uRoBoZ2SswDnCSR6lKCysrmPya1IGn sdPtHL+6704lTeOpcvWTWCpspwZrVy36wpg68= 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; bh=t+nF2YJSnzrJj+gB56fTZx/cjw2L1Zkcjf3JjAPWii8=; b=eqJxLNq/3DNoaz6r/owGvBYHL65TbHy5266KGlrQnumXPT0I3+8FJLDZyBXDevQ3ve gjpgu8pdfQwo+JQr0Pww35ERp+7ZzSW9HyOftZExmXPiB8RbMnXUqiV9R9Apc9AfQs4Z PaQYZCe6IYe/CtLsWjPcAkJooY7sWvvUa1vaQtkI85VphVT0yvwL31EtANiYARGaxJqw KPLZeMVaGG2oyVO1wwuEYWKdwPvkB0AJg5OQ75AeWInTyioKJSTrudDudGnrgYyT/qt/ Uu76/MMkffOENHTw4g2mwMPiAQgJW/PZ5Nrs0rahdOXZOXflOcqjZd3q4X678MI7DGgZ VG1Q== X-Gm-Message-State: AJaThX5hfAAZZppy3n+jCkGnu/IaYXFB+xxh9CF6BisC6mj3lJ/hLxp9 XuXlnjdvmQBcJCXy+lGHAEtuuTOwIDw= X-Received: by 10.25.20.167 with SMTP id 39mr15069336lfu.67.1512865546682; Sat, 09 Dec 2017 16:25:46 -0800 (PST) Received: from localhost.localdomain (c-cb7471d5.014-348-6c756e10.cust.bredbandsbolaget.se. [213.113.116.203]) by smtp.gmail.com with ESMTPSA id g83sm2136175ljb.48.2017.12.09.16.25.45 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 09 Dec 2017 16:25:45 -0800 (PST) From: Linus Walleij To: linux-gpio@vger.kernel.org Cc: Linus Walleij , Wolfram Sang Subject: [PATCH] gpio: Add helpers to determin direction of gpiods Date: Sun, 10 Dec 2017 01:23:40 +0100 Message-Id: <20171210002340.18460-1-linus.walleij@linaro.org> X-Mailer: git-send-email 2.14.3 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org The gpiod_get_direction() returns 1 for input and 0 for output but it's pretty hard to remember which one is which and generally unintuitive and messy to provide #defines so let's simply add two static inlines to do the job. Cc: Wolfram Sang Suggested-by: Wolfram Sang Signed-off-by: Linus Walleij --- Documentation/gpio/consumer.txt | 14 +++++++++++--- include/linux/gpio/consumer.h | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) -- 2.14.3 -- 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/Documentation/gpio/consumer.txt b/Documentation/gpio/consumer.txt index 63e1bd1d88e3..c4b8e3d8c29f 100644 --- a/Documentation/gpio/consumer.txt +++ b/Documentation/gpio/consumer.txt @@ -162,8 +162,8 @@ The device-managed variants are, unsurprisingly: Using GPIOs =========== -Setting Direction ------------------ +Setting and Getting Direction +----------------------------- The first thing a driver must do with a GPIO is setting its direction. If no direction-setting flags have been given to gpiod_get*(), this is done by invoking one of the gpiod_direction_*() functions: @@ -184,7 +184,15 @@ A driver can also query the current direction of a GPIO: int gpiod_get_direction(const struct gpio_desc *desc) -This function will return either GPIOF_DIR_IN or GPIOF_DIR_OUT. +This function will return either 1 (input) or 0 (output). There are also +these convenience helpers: + + bool gpiod_is_input(const struct gpio_desc *desc) + bool gpiod_is_output(const struct gpio_desc *desc) + +These will not provide the same level of error fallbacks: if they fail to +obtain the direction, they will print an error and report as input since this +is usually safest. Be aware that there is no default direction for GPIOs. Therefore, **using a GPIO without setting its direction first is illegal and will result in undefined diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h index 7447d85dbe2f..4e5ee3ec1913 100644 --- a/include/linux/gpio/consumer.h +++ b/include/linux/gpio/consumer.h @@ -487,6 +487,31 @@ struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev, #endif /* CONFIG_GPIOLIB */ +/* + * Helpers that quickly tell whether a line is input or output. + */ +static inline bool gpiod_is_input(struct gpio_desc *desc) +{ + int ret = gpiod_get_direction(desc); + if (ret < 0) { + pr_err("GPIO: failed to get direction\n"); + /* It is usually safest to assume we are input */ + return true; + } + return !!ret; +} + +static inline bool gpiod_is_output(struct gpio_desc *desc) +{ + int ret = gpiod_get_direction(desc); + if (ret < 0) { + pr_err("GPIO: failed to get direction\n"); + /* It is usually safest to assume we are input */ + return false; + } + return !ret; +} + static inline struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev, const char *con_id,