From: Simon Glass <sjg@chromium.org>
To: U-Boot Mailing List <u-boot@lists.denx.de>
Cc: Devicetree Discuss <devicetree-discuss@lists.ozlabs.org>,
Tom Warren <twarren@nvidia.com>,
Jerry Van Baren <vanbaren@cideas.com>
Subject: [PATCH v6 03/20] fdt: Add basic support for decoding GPIO definitions
Date: Mon, 27 Feb 2012 12:52:36 -0800 [thread overview]
Message-ID: <1330375973-10681-4-git-send-email-sjg@chromium.org> (raw)
In-Reply-To: <1330375973-10681-1-git-send-email-sjg@chromium.org>
This adds some support into fdtdec for reading GPIO definitions from
the fdt. We permit up to FDT_GPIO_MAX GPIOs in the system. Each GPIO
is of the form:
gpio-function-name = <phandle gpio_num flags>;
where:
phandle is a pointer to the GPIO node
gpio_num is the number of the GPIO (0 to 223)
flags is a flag, as follows:
bit meaning
0 0=polarity normal, 1=active low (inverted)
An example is:
enable-propounder-gpios = <&gpio 43 0>;
which means that GPIO 43 is used to enable the propounder (setting the
GPIO high), or that you can detect that the propounder is enabled by
checking if the GPIO is high (the fdt does not indicate input/output).
Two main functions are provided:
fdtdec_decode_gpio() reads a GPIO property from an fdt node and decodes it
into a structure.
fdtdec_setup_gpio() sets up the GPIO by calling gpio_request for you.
Both functions can cope with the property being missing, which is taken to
mean that that GPIO function is not available or is not needed.
[For reference, from Stephen Warren <swarren@nvidia.com>. It may be that
we add this extra complexity later if needed:
The correct way to parse such a GPIO property in general is:
* Read the first cell.
* Find the node referenced by the phandle (the controller).
* Ensure property gpio-controller is present in the controller node.
* Read property #gpio-cells from the controller node.
* Extract #gpio-cells from the original property.
* Keep processing more cells from the original property; there may be
multiple GPIOs listed.
According to the binding documentation in the Linux kernel, Samsung
Exynos4 doesn't use this format, and while all other chips do have a
flags cell, about 50% of the controllers indicate the cell is unused.
]
Signed-off-by: Simon Glass <sjg@chromium.org>
---
Changes in v5:
- Fixed endian bug in fdtdec_decode_gpios()
- Make sure GPIO name is NULL if incorrectly decoded
include/fdtdec.h | 45 ++++++++++++++++++++++++++++++
lib/fdtdec.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 124 insertions(+), 0 deletions(-)
diff --git a/include/fdtdec.h b/include/fdtdec.h
index 047f603..6c0a2d1 100644
--- a/include/fdtdec.h
+++ b/include/fdtdec.h
@@ -61,6 +61,23 @@ enum fdt_compat_id {
COMPAT_COUNT,
};
+/* GPIOs are numbered from 0 */
+enum {
+ FDT_GPIO_NONE = -1U, /* an invalid GPIO used to end our list */
+
+ FDT_GPIO_ACTIVE_LOW = 1 << 0, /* input is active low (else high) */
+};
+
+/* This is the state of a GPIO pin as defined by the fdt */
+struct fdt_gpio_state {
+ const char *name; /* name of the fdt property defining this */
+ uint gpio; /* GPIO number, or FDT_GPIO_NONE if none */
+ u8 flags; /* FDT_GPIO_... flags */
+};
+
+/* This tells us whether a fdt_gpio_state record is valid or not */
+#define fdt_gpio_isvalid(x) ((x)->gpio != FDT_GPIO_NONE)
+
/**
* Find the next numbered alias for a peripheral. This is used to enumerate
* all the peripherals of a certain type.
@@ -227,3 +244,31 @@ int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
* @return 1 if the properly is present; 0 if it isn't present
*/
int fdtdec_get_bool(const void *blob, int node, const char *prop_name);
+
+/**
+ * Decode a single GPIOs from an FDT.
+ *
+ * If the property is not found, then the GPIO structure will still be
+ * initialised, with gpio set to FDT_GPIO_NONE. This makes it easy to
+ * provide optional GPIOs.
+ *
+ * @param blob FDT blob to use
+ * @param node Node to look at
+ * @param prop_name Node property name
+ * @param gpio gpio elements to fill from FDT
+ * @return 0 if ok, -FDT_ERR_NOTFOUND if the property is missing.
+ */
+int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
+ struct fdt_gpio_state *gpio);
+
+/**
+ * Set up a GPIO pin according to the provided gpio information. At present this
+ * just requests the GPIO.
+ *
+ * If the gpio is FDT_GPIO_NONE, no action is taken. This makes it easy to
+ * deal with optional GPIOs.
+ *
+ * @param gpio GPIO info to use for set up
+ * @return 0 if all ok or gpio was FDT_GPIO_NONE; -1 on error
+ */
+int fdtdec_setup_gpio(struct fdt_gpio_state *gpio);
diff --git a/lib/fdtdec.c b/lib/fdtdec.c
index 977528b..c748cac 100644
--- a/lib/fdtdec.c
+++ b/lib/fdtdec.c
@@ -24,6 +24,9 @@
#include <libfdt.h>
#include <fdtdec.h>
+/* we need the generic GPIO interface here */
+#include <asm-generic/gpio.h>
+
DECLARE_GLOBAL_DATA_PTR;
/*
@@ -336,3 +339,79 @@ int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
cell = fdt_getprop(blob, node, prop_name, &len);
return cell != NULL;
}
+
+/**
+ * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
+ * terminating item.
+ *
+ * @param blob FDT blob to use
+ * @param node Node to look at
+ * @param prop_name Node property name
+ * @param gpio Array of gpio elements to fill from FDT. This will be
+ * untouched if either 0 or an error is returned
+ * @param max_count Maximum number of elements allowed
+ * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
+ * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
+ */
+static int fdtdec_decode_gpios(const void *blob, int node,
+ const char *prop_name, struct fdt_gpio_state *gpio,
+ int max_count)
+{
+ const struct fdt_property *prop;
+ const u32 *cell;
+ const char *name;
+ int len, i;
+
+ debug("%s: %s\n", __func__, prop_name);
+ assert(max_count > 0);
+ prop = fdt_get_property(blob, node, prop_name, &len);
+ if (!prop) {
+ debug("FDT: %s: property '%s' missing\n", __func__, prop_name);
+ return -FDT_ERR_NOTFOUND;
+ }
+
+ /* We will use the name to tag the GPIO */
+ name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
+ cell = (u32 *)prop->data;
+ len /= sizeof(u32) * 3; /* 3 cells per GPIO record */
+ if (len > max_count) {
+ debug("FDT: %s: too many GPIOs / cells for "
+ "property '%s'\n", __func__, prop_name);
+ return -FDT_ERR_BADLAYOUT;
+ }
+
+ /* Read out the GPIO data from the cells */
+ for (i = 0; i < len; i++, cell += 3) {
+ gpio[i].gpio = fdt32_to_cpu(cell[1]);
+ gpio[i].flags = fdt32_to_cpu(cell[2]);
+ gpio[i].name = name;
+ }
+
+ return len;
+}
+
+int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
+ struct fdt_gpio_state *gpio)
+{
+ int err;
+
+ debug("%s: %s\n", __func__, prop_name);
+ gpio->gpio = FDT_GPIO_NONE;
+ gpio->name = NULL;
+ err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
+ return err == 1 ? 0 : err;
+}
+
+int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
+{
+ /*
+ * Return success if there is no GPIO defined. This is used for
+ * optional GPIOs)
+ */
+ if (!fdt_gpio_isvalid(gpio))
+ return 0;
+
+ if (gpio_request(gpio->gpio, gpio->name))
+ return -1;
+ return 0;
+}
--
1.7.7.3
next prev parent reply other threads:[~2012-02-27 20:52 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <1330375973-10681-1-git-send-email-sjg@chromium.org>
2012-02-27 20:52 ` [PATCH v6 01/20] fdt: Tidy up a few fdtdec problems Simon Glass
2012-02-27 20:52 ` [PATCH v6 02/20] fdt: Add functions to access phandles, arrays and bools Simon Glass
2012-02-27 20:52 ` Simon Glass [this message]
2012-02-27 20:52 ` [PATCH v6 04/20] arm: fdt: Add skeleton device tree file from kernel Simon Glass
2012-02-27 20:52 ` [PATCH v6 05/20] tegra: fdt: Add Tegra2x " Simon Glass
2012-02-27 20:52 ` [PATCH v6 07/20] fdt: Add staging area for device tree binding documentation Simon Glass
2012-02-27 20:52 ` [PATCH v6 08/20] fdt: Add tegra-usb bindings file from linux Simon Glass
[not found] ` <1330375973-10681-1-git-send-email-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
2012-02-27 20:52 ` [PATCH v6 06/20] tegra: fdt: Add device tree file for Tegra2 Seaboard from kernel Simon Glass
2012-02-27 20:52 ` [PATCH v6 09/20] tegra: fdt: Add additional USB binding Simon Glass
[not found] ` <1330375973-10681-10-git-send-email-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
2012-02-27 23:27 ` Stephen Warren
2012-02-27 20:52 ` [PATCH v6 10/20] tegra: fdt: Add clock bindings Simon Glass
2012-02-27 20:52 ` [PATCH v6 11/20] tegra: fdt: Add clock bindings for Tegra2 Seaboard Simon Glass
[not found] ` <1330375973-10681-12-git-send-email-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
2012-02-27 23:29 ` Stephen Warren
[not found] ` <4F4C11E9.1050907-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2012-02-28 17:20 ` Simon Glass
[not found] ` <CAPnjgZ0_xzn0tvETt3C=pjyRX-MXNA-JXy4fuAs9L8OdHuvLhg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-02-28 17:32 ` Stephen Warren
[not found] ` <74CDBE0F657A3D45AFBB94109FB122FF17BDDF1D6A-C7FfzLzN0UxDw2glCA4ptUEOCMrvLtNR@public.gmane.org>
2012-02-28 17:37 ` Simon Glass
2012-02-28 18:31 ` Stephen Warren
2012-02-28 18:37 ` Simon Glass
2012-02-28 18:41 ` Stephen Warren
2012-02-28 18:46 ` Simon Glass
[not found] ` <CAPnjgZ0VGRSgb92u2UbNf+_HFF-EXhLZzC4XdYUvAjVKtz1XtQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-02-28 22:16 ` [U-Boot] " Albert ARIBAUD
2012-03-03 16:26 ` Simon Glass
[not found] ` <74CDBE0F657A3D45AFBB94109FB122FF17BDDF1DB8-C7FfzLzN0UxDw2glCA4ptUEOCMrvLtNR@public.gmane.org>
2012-03-05 20:46 ` [U-Boot] " Tom Rini
2012-03-07 2:48 ` Simon Glass
2012-02-27 20:52 ` [PATCH v6 12/20] tegra: usb: fdt: Add additional device tree definitions for USB ports Simon Glass
2012-02-27 20:52 ` [PATCH v6 13/20] tegra: usb: fdt: Add USB definitions for Tegra2 Seaboard Simon Glass
2012-02-27 20:52 ` [PATCH v6 15/20] tegra: fdt: Add function to return peripheral/clock ID Simon Glass
[not found] ` <1330375973-10681-16-git-send-email-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
2012-02-27 23:41 ` Stephen Warren
[not found] ` <4F4C149E.3070505-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2012-02-28 17:46 ` Simon Glass
[not found] ` <CAPnjgZ24vhy7NKj_Dt_dzn0qJ8=rj4nF04WSsY8u9MorAanzVA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-02-28 18:37 ` Stephen Warren
[not found] ` <74CDBE0F657A3D45AFBB94109FB122FF17BDDF1DB4-C7FfzLzN0UxDw2glCA4ptUEOCMrvLtNR@public.gmane.org>
2012-02-28 18:44 ` Simon Glass
2012-02-28 18:51 ` Stephen Warren
[not found] ` <74CDBE0F657A3D45AFBB94109FB122FF17BDDF1DC8-C7FfzLzN0UxDw2glCA4ptUEOCMrvLtNR@public.gmane.org>
2012-02-28 23:50 ` Simon Glass
[not found] ` <CAPnjgZ2vwck_u1HVbpz=B4QjiCVoLeX6TcvwMi-o71Fqt_m3NQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-02-29 17:08 ` Stephen Warren
2012-02-27 20:52 ` [PATCH v6 20/20] tegra: fdt: Enable FDT support for Seaboard Simon Glass
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=1330375973-10681-4-git-send-email-sjg@chromium.org \
--to=sjg@chromium.org \
--cc=devicetree-discuss@lists.ozlabs.org \
--cc=twarren@nvidia.com \
--cc=u-boot@lists.denx.de \
--cc=vanbaren@cideas.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
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).