Author: ian
Date: Sat Sep 13 19:03:32 2014
New Revision: 271546
URL: http://svnweb.freebsd.org/changeset/base/271546

Log:
  Create an interface and support routines for drivers that handle IO pin
  multiplexing and configuration based on FDT data.
  
  Reviewed by:  imp

Added:
  head/sys/dev/fdt/fdt_pinctrl.c   (contents, props changed)
  head/sys/dev/fdt/fdt_pinctrl.h   (contents, props changed)
  head/sys/dev/fdt/fdt_pinctrl_if.m   (contents, props changed)
Modified:
  head/sys/conf/files

Modified: head/sys/conf/files
==============================================================================
--- head/sys/conf/files Sat Sep 13 18:54:15 2014        (r271545)
+++ head/sys/conf/files Sat Sep 13 19:03:32 2014        (r271546)
@@ -1377,9 +1377,11 @@ dev/fatm/if_fatm.c               optional fatm pci
 dev/fb/fbd.c                   optional fbd | vt
 dev/fb/fb_if.m                 standard
 dev/fb/splash.c                        optional sc splash
-dev/fdt/fdt_clock.c            optional fdt_clock
-dev/fdt/fdt_clock_if.m         optional fdt_clock
+dev/fdt/fdt_clock.c            optional fdt fdt_clock
+dev/fdt/fdt_clock_if.m         optional fdt fdt_clock
 dev/fdt/fdt_common.c           optional fdt
+dev/fdt/fdt_pinctrl.c          optional fdt fdt_pinctrl
+dev/fdt/fdt_pinctrl_if.m       optional fdt fdt_pinctrl
 dev/fdt/fdt_slicer.c           optional fdt cfi | fdt nand
 dev/fdt/fdt_static_dtb.S       optional fdt fdt_dtb_static \
        dependency      "$S/boot/fdt/dts/${MACHINE}/${FDT_DTS_FILE}"

Added: head/sys/dev/fdt/fdt_pinctrl.c
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/dev/fdt/fdt_pinctrl.c      Sat Sep 13 19:03:32 2014        
(r271546)
@@ -0,0 +1,151 @@
+/*-
+ * Copyright (c) 2014 Ian Lepore <i...@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)));
+}
+

Added: head/sys/dev/fdt/fdt_pinctrl.h
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/dev/fdt/fdt_pinctrl.h      Sat Sep 13 19:03:32 2014        
(r271546)
@@ -0,0 +1,58 @@
+/*-
+ * Copyright (c) 2014 Ian Lepore <i...@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 */
+

Added: head/sys/dev/fdt/fdt_pinctrl_if.m
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/dev/fdt/fdt_pinctrl_if.m   Sat Sep 13 19:03:32 2014        
(r271546)
@@ -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;
+};
+
_______________________________________________
svn-src-head@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to