aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/fdt
diff options
context:
space:
mode:
authorIan Lepore <ian@FreeBSD.org>2014-09-13 19:03:32 +0000
committerIan Lepore <ian@FreeBSD.org>2014-09-13 19:03:32 +0000
commitb8ed20dbc4f35b4273220c99faa8041ac653f346 (patch)
tree272e4805362307ec39e895718261daa033aa279d /sys/dev/fdt
parent363a68722a23a6efdf1c27c5eb167f900acc66ec (diff)
downloadsrc-b8ed20dbc4f35b4273220c99faa8041ac653f346.tar.gz
src-b8ed20dbc4f35b4273220c99faa8041ac653f346.zip
Create an interface and support routines for drivers that handle IO pin
multiplexing and configuration based on FDT data. Reviewed by: imp
Notes
Notes: svn path=/head/; revision=271546
Diffstat (limited to 'sys/dev/fdt')
-rw-r--r--sys/dev/fdt/fdt_pinctrl.c151
-rw-r--r--sys/dev/fdt/fdt_pinctrl.h58
-rw-r--r--sys/dev/fdt/fdt_pinctrl_if.m47
3 files changed, 256 insertions, 0 deletions
diff --git a/sys/dev/fdt/fdt_pinctrl.c b/sys/dev/fdt/fdt_pinctrl.c
new file mode 100644
index 000000000000..474fb00e22a9
--- /dev/null
+++ b/sys/dev/fdt/fdt_pinctrl.c
@@ -0,0 +1,151 @@
+/*-
+ * Copyright (c) 2014 Ian Lepore <ian@freebsd.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+#include <sys/param.h>
+
+#include <dev/ofw/ofw_bus.h>
+#include <dev/ofw/ofw_bus_subr.h>
+
+#include "fdt_pinctrl_if.h"
+
+#include <dev/fdt/fdt_common.h>
+#include <dev/fdt/fdt_pinctrl.h>
+
+int
+fdt_pinctrl_configure(device_t client, u_int index)
+{
+ device_t pinctrl;
+ phandle_t *configs;
+ int i, nconfigs;
+ char name[16];
+
+ snprintf(name, sizeof(name), "pinctrl-%u", index);
+ nconfigs = OF_getprop_alloc(ofw_bus_get_node(client), name,
+ sizeof(*configs), (void **)&configs);
+ if (nconfigs < 0)
+ return (ENOENT);
+ if (nconfigs == 0)
+ return (0); /* Empty property is documented as valid. */
+ for (i = 0; i < nconfigs; i++) {
+ if ((pinctrl = OF_device_from_xref(configs[i])) != NULL)
+ FDT_PINCTRL_CONFIGURE(pinctrl, configs[i]);
+ }
+ free(configs, M_OFWPROP);
+ return (0);
+}
+
+int
+fdt_pinctrl_configure_by_name(device_t client, const char * name)
+{
+ char * names;
+ int i, offset, nameslen;
+
+ nameslen = OF_getprop_alloc(ofw_bus_get_node(client), "pinctrl-names",
+ sizeof(*names), (void **)&names);
+ if (nameslen <= 0)
+ return (ENOENT);
+ for (i = 0, offset = 0; offset < nameslen; i++) {
+ if (strcmp(name, &names[offset]) == 0)
+ break;
+ offset += strlen(&names[offset]) + 1;
+ }
+ free(names, M_OFWPROP);
+ if (offset < nameslen)
+ return (fdt_pinctrl_configure(client, i));
+ else
+ return (ENOENT);
+}
+
+static int
+pinctrl_register_children(device_t pinctrl, phandle_t parent,
+ const char *pinprop)
+{
+ phandle_t node;
+
+ /*
+ * Recursively descend from parent, looking for nodes that have the
+ * given property, and associate the pinctrl device_t with each one.
+ */
+ for (node = OF_child(parent); node != 0; node = OF_peer(node)) {
+ pinctrl_register_children(pinctrl, node, pinprop);
+ if (pinprop == NULL || OF_hasprop(node, pinprop)) {
+ OF_device_register_xref(OF_xref_from_node(node),
+ pinctrl);
+ }
+ }
+ return (0);
+}
+
+int
+fdt_pinctrl_register(device_t pinctrl, const char *pinprop)
+{
+ phandle_t node;
+
+ node = ofw_bus_get_node(pinctrl);
+ OF_device_register_xref(OF_xref_from_node(node), pinctrl);
+ return (pinctrl_register_children(pinctrl, node, pinprop));
+}
+
+static int
+pinctrl_configure_children(device_t pinctrl, phandle_t parent)
+{
+ phandle_t node, *configs;
+ int i, nconfigs;
+
+ for (node = OF_child(parent); node != 0; node = OF_peer(node)) {
+ if (!fdt_is_enabled(node))
+ continue;
+ pinctrl_configure_children(pinctrl, node);
+ nconfigs = OF_getencprop_alloc(node, "pinctrl-0",
+ sizeof(*configs), (void **)&configs);
+#ifdef DEBUG
+ {
+ char name[32];
+ OF_getprop(node, "name", &name, sizeof(name));
+ printf("%d items in pinctrl-0 for %s\n", nconfigs, name);
+ }
+#endif
+ if (nconfigs <= 0)
+ continue;
+ for (i = 0; i < nconfigs; i++) {
+ if (OF_device_from_xref(configs[i]) == pinctrl)
+ FDT_PINCTRL_CONFIGURE(pinctrl, configs[i]);
+ }
+ free(configs, M_OFWPROP);
+ }
+ return (0);
+}
+
+int
+fdt_pinctrl_configure_tree(device_t pinctrl)
+{
+
+ return (pinctrl_configure_children(pinctrl, OF_peer(0)));
+}
+
diff --git a/sys/dev/fdt/fdt_pinctrl.h b/sys/dev/fdt/fdt_pinctrl.h
new file mode 100644
index 000000000000..6ac7d60a52eb
--- /dev/null
+++ b/sys/dev/fdt/fdt_pinctrl.h
@@ -0,0 +1,58 @@
+/*-
+ * Copyright (c) 2014 Ian Lepore <ian@freebsd.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef DEV_FDT_PINCTRL_H
+#define DEV_FDT_PINCTRL_H
+
+#include "fdt_pinctrl_if.h"
+
+/*
+ * Configure pins by name or index. This looks up the pinctrl-N property in
+ * client's fdt data by index or name, and passes each handle in it to the
+ * pinctrl driver for configuration.
+ */
+int fdt_pinctrl_configure(device_t client, u_int index);
+int fdt_pinctrl_configure_by_name(device_t client, const char * name);
+
+/*
+ * Register a pinctrl driver so that it can be used by other devices which call
+ * fdt_pinctrl_configure(). The pinprop argument is the name of a property that
+ * identifies each descendent of the pinctrl node which is a pin configuration
+ * node whose xref phandle can be passed to FDT_PINCTRL_CONFIGURE(). If this is
+ * NULL, every descendant node is registered.
+ */
+int fdt_pinctrl_register(device_t pinctrl, const char *pinprop);
+
+/*
+ * Walk the device tree and configure pins for each enabled device whose
+ * pinctrl-0 property contains references to nodes which are children of the
+ * given pinctrl device. This helper routine is for use by pinctrl drivers.
+ */
+int fdt_pinctrl_configure_tree(device_t pinctrl);
+
+#endif /* DEV_FDT_PINCTRL_H */
+
diff --git a/sys/dev/fdt/fdt_pinctrl_if.m b/sys/dev/fdt/fdt_pinctrl_if.m
new file mode 100644
index 000000000000..c499cdf3cb81
--- /dev/null
+++ b/sys/dev/fdt/fdt_pinctrl_if.m
@@ -0,0 +1,47 @@
+#-
+# Copyright (c) 2014 Ian Lepore
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+# $FreeBSD$
+#
+
+#include <sys/types.h>
+#include <dev/ofw/openfirm.h>
+
+#
+# This is the interface that fdt_pinctrl drivers provide to other drivers.
+#
+
+INTERFACE fdt_pinctrl;
+
+#
+# Set pins to the specified configuration. The cfgxref arg is an xref phandle
+# to a descendent node (child, grandchild, ...) of the pinctrl device node.
+# Returns 0 on success or a standard errno value.
+#
+METHOD int configure {
+ device_t pinctrl;
+ phandle_t cfgxref;
+};
+