Linux-GPIO Archive on lore.kernel.org
 help / color / Atom feed
From: Marco Felsch <m.felsch@pengutronix.de>
To: support.opensource@diasemi.com, lee.jones@linaro.org,
	robh+dt@kernel.org, linus.walleij@linaro.org,
	bgolaszewski@baylibre.com, joel@jms.id.au, andrew@aj.id.au,
	lgirdwood@gmail.com, broonie@kernel.org
Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-gpio@vger.kernel.org, linux-aspeed@lists.ozlabs.org,
	linux-arm-kernel@lists.infradead.org, kernel@pengutronix.de
Subject: [PATCH v3 2/6] gpio: make gpiod_to_offset() available for other users
Date: Fri, 29 Nov 2019 18:25:33 +0100
Message-ID: <20191129172537.31410-3-m.felsch@pengutronix.de> (raw)
In-Reply-To: <20191129172537.31410-1-m.felsch@pengutronix.de>

Currently gpiod_to_offset() is a gpio-subsystem private function which
is used by the gpiolib itself and by the aspeed gpio driver. The time
has shown that there are other drivers as well which need this
information in some special cases e.g. MFD drivers. The patch makes the
function public but you need to explicit add the <linux/gpio/private.h>
include. See discussion [1] for more information.

[1] https://lkml.org/lkml/2019/11/27/357

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
v3:
- former patch description was "gpio: add support to get local gpio number"
- adapt commit message and description to reflect new state
- don't add wrapper instead use the already existing gpiod_to_offset
- move gpiod_to_offset from gpiolib.h into gpiolib.c
- move declaration into linux/gpio/private.h
---
 drivers/gpio/gpio-aspeed.c   | 11 +++++------
 drivers/gpio/gpiolib-sysfs.c |  1 +
 drivers/gpio/gpiolib.c       | 22 ++++++++++++++++++++++
 drivers/gpio/gpiolib.h       |  8 --------
 include/linux/gpio/private.h | 27 +++++++++++++++++++++++++++
 5 files changed, 55 insertions(+), 14 deletions(-)
 create mode 100644 include/linux/gpio/private.h

diff --git a/drivers/gpio/gpio-aspeed.c b/drivers/gpio/gpio-aspeed.c
index b1d1d39e5174..e10ebad6853a 100644
--- a/drivers/gpio/gpio-aspeed.c
+++ b/drivers/gpio/gpio-aspeed.c
@@ -20,13 +20,12 @@
 #include <linux/string.h>
 
 /*
- * These two headers aren't meant to be used by GPIO drivers. We need
- * them in order to access gpiod_to_offset() which we need to implement
- * the aspeed specific API which allows the coprocessor to request
- * access to some GPIOs and to arbitrate between coprocessor and ARM.
+ * The header isn't meant to be used by GPIO drivers. We need it in order to
+ * access gpiod_to_offset() which we need to implement the aspeed specific API
+ * which allows the coprocessor to request access to some GPIOs and to
+ * arbitrate between coprocessor and ARM.
  */
-#include <linux/gpio/consumer.h>
-#include "gpiolib.h"
+#include <linux/gpio/private.h>
 
 struct aspeed_bank_props {
 	unsigned int bank;
diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c
index d4cab6a80928..367db78bb58c 100644
--- a/drivers/gpio/gpiolib-sysfs.c
+++ b/drivers/gpio/gpiolib-sysfs.c
@@ -5,6 +5,7 @@
 #include <linux/sysfs.h>
 #include <linux/gpio/consumer.h>
 #include <linux/gpio/driver.h>
+#include <linux/gpio/private.h>
 #include <linux/interrupt.h>
 #include <linux/kdev_t.h>
 #include <linux/slab.h>
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 548cf41c6179..197cac0e3e99 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -150,6 +150,28 @@ struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
 	return &gdev->descs[hwnum];
 }
 
+/**
+ * gpiod_to_offset - obtain the local GPIO number from a global unique
+ *		     descriptor
+ * @desc:	     gpio whose local gpio number should be returned
+ *
+ * It converts the global unique descriptor into the gpio chip local gpio
+ * number. This can be useful if you need to do further device configuration
+ * e.g. for a MFD. Use this function with caution. You will get a wrong number
+ * if you pass the wrong descriptor.
+ *
+ * Return:
+ * * the GPIO number of the passed descriptor relative to its chip
+ * * -EINVAL if desc is invalid or NULL
+ */
+int gpiod_to_offset(const struct gpio_desc *desc)
+{
+	if (IS_ERR_OR_NULL(desc))
+		return -EINVAL;
+	return desc - &desc->gdev->descs[0];
+}
+EXPORT_SYMBOL_GPL(gpiod_to_offset);
+
 /**
  * desc_to_gpio - convert a GPIO descriptor to the integer namespace
  * @desc: GPIO descriptor
diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h
index a7f93ce6e114..a586a793b084 100644
--- a/drivers/gpio/gpiolib.h
+++ b/drivers/gpio/gpiolib.h
@@ -124,14 +124,6 @@ int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
 int gpiod_hog(struct gpio_desc *desc, const char *name,
 		unsigned long lflags, enum gpiod_flags dflags);
 
-/*
- * Return the GPIO number of the passed descriptor relative to its chip
- */
-static inline int gpiod_to_offet(const struct gpio_desc *desc)
-{
-	return desc - &desc->gdev->descs[0];
-}
-
 /* With descriptor prefix */
 
 #define gpiod_emerg(desc, fmt, ...)					       \
diff --git a/include/linux/gpio/private.h b/include/linux/gpio/private.h
new file mode 100644
index 000000000000..56514bdcfac6
--- /dev/null
+++ b/include/linux/gpio/private.h
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Functions listed here should be used with caution. Mostly those functions are
+ * used by the gpiolib internally. But the time has shown that some special
+ * drivers needs access to these helpers too.
+ */
+#ifndef __LINUX_GPIO_PRIVATE_H
+#define __LINUX_GPIO_PRIVATE_H
+
+struct gpio_desc;
+
+#ifdef CONFIG_GPIOLIB
+
+int gpiod_to_offset(const struct gpio_desc *desc);
+
+#else /* CONFIG_GPIOLIB */
+
+static inline int gpiod_to_offset(const struct gpio_desc *desc)
+{
+	/* GPIO can never have been requested */
+	WARN_ON(desc);
+	return 0;
+}
+
+#endif /* CONFIG_GPIOLIB */
+
+#endif /* __LINUX_GPIO_PRIVATE_H */
-- 
2.20.1


  parent reply index

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-29 17:25 [PATCH v3 0/6] DA9062 PMIC features Marco Felsch
2019-11-29 17:25 ` [PATCH v3 1/6] gpio: treewide rename gpio_chip_hwgpio to gpiod_to_offset Marco Felsch
2019-12-02  2:59   ` Andrew Jeffery
2019-11-29 17:25 ` Marco Felsch [this message]
2019-12-02  3:00   ` [PATCH v3 2/6] gpio: make gpiod_to_offset() available for other users Andrew Jeffery
2019-11-29 17:25 ` [PATCH v3 3/6] dt-bindings: mfd: da9062: add regulator voltage selection documentation Marco Felsch
2019-12-04 13:46   ` Mark Brown
2019-12-10  9:41     ` Marco Felsch
2019-12-11 16:14       ` Adam Thomson
2019-12-11 17:09         ` Marco Felsch
2019-12-12 15:08           ` Linus Walleij
2019-12-12 15:55             ` Marco Felsch
2019-12-12 16:10           ` Mark Brown
2019-12-12 16:21             ` Marco Felsch
2019-12-12 16:51               ` Mark Brown
2019-12-16  8:55                 ` Marco Felsch
2019-12-16 11:44                   ` Mark Brown
2019-12-17  7:35                     ` Marco Felsch
2019-12-17 12:58                       ` Mark Brown
2020-01-07  8:36                         ` Marco Felsch
2020-01-07 13:09                           ` Mark Brown
2020-01-07 13:38                             ` Marco Felsch
2020-01-14 15:43                               ` Mark Brown
2019-12-16 16:32                   ` Adam Thomson
2019-12-17  9:00                     ` Marco Felsch
2019-12-17  9:12                       ` Marco Felsch
2019-12-17  9:53                       ` Adam Thomson
2019-12-17 12:31                         ` Marco Felsch
2019-12-17 13:13                           ` Adam Thomson
2019-12-16 16:32                   ` Adam Thomson
2019-12-16 12:28               ` Linus Walleij
2019-11-29 17:25 ` [PATCH v3 4/6] regulator: da9062: add voltage selection gpio support Marco Felsch
2019-11-29 17:25 ` [PATCH v3 5/6] dt-bindings: mfd: da9062: add regulator gpio enable/disable documentation Marco Felsch
2019-12-13 22:23   ` Rob Herring
2019-12-16 16:31   ` Lee Jones
2019-11-29 17:25 ` [PATCH v3 6/6] regulator: da9062: add gpio based regulator dis-/enable support Marco Felsch
2019-12-02 11:44 ` [PATCH v3 0/6] DA9062 PMIC features Linus Walleij
2019-12-02 12:04   ` Lee Jones

Reply instructions:

You may reply publically 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=20191129172537.31410-3-m.felsch@pengutronix.de \
    --to=m.felsch@pengutronix.de \
    --cc=andrew@aj.id.au \
    --cc=bgolaszewski@baylibre.com \
    --cc=broonie@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=joel@jms.id.au \
    --cc=kernel@pengutronix.de \
    --cc=lee.jones@linaro.org \
    --cc=lgirdwood@gmail.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-aspeed@lists.ozlabs.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robh+dt@kernel.org \
    --cc=support.opensource@diasemi.com \
    /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

Linux-GPIO Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-gpio/0 linux-gpio/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-gpio linux-gpio/ https://lore.kernel.org/linux-gpio \
		linux-gpio@vger.kernel.org
	public-inbox-index linux-gpio

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-gpio


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git