xref: /freebsd/sys/dev/fdt/fdt_pinctrl.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * Copyright (c) 2014 Ian Lepore <ian@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include <sys/param.h>
29 
30 #include <dev/ofw/ofw_bus.h>
31 #include <dev/ofw/ofw_bus_subr.h>
32 
33 #include "fdt_pinctrl_if.h"
34 
35 #include <dev/fdt/fdt_common.h>
36 #include <dev/fdt/fdt_pinctrl.h>
37 
38 int
39 fdt_pinctrl_configure(device_t client, u_int index)
40 {
41 	device_t pinctrl;
42 	phandle_t *configs;
43 	int i, nconfigs;
44 	char name[16];
45 
46 	snprintf(name, sizeof(name), "pinctrl-%u", index);
47 	nconfigs = OF_getencprop_alloc_multi(ofw_bus_get_node(client), name,
48 	    sizeof(*configs), (void **)&configs);
49 	if (nconfigs < 0)
50 		return (ENOENT);
51 	if (nconfigs == 0)
52 		return (0); /* Empty property is documented as valid. */
53 	for (i = 0; i < nconfigs; i++) {
54 		if ((pinctrl = OF_device_from_xref(configs[i])) != NULL)
55 			FDT_PINCTRL_CONFIGURE(pinctrl, configs[i]);
56 	}
57 	OF_prop_free(configs);
58 	return (0);
59 }
60 
61 int
62 fdt_pinctrl_configure_by_name(device_t client, const char * name)
63 {
64 	char * names;
65 	int i, offset, nameslen;
66 
67 	nameslen = OF_getprop_alloc(ofw_bus_get_node(client), "pinctrl-names",
68 	    (void **)&names);
69 	if (nameslen <= 0)
70 		return (ENOENT);
71 	for (i = 0, offset = 0; offset < nameslen; i++) {
72 		if (strcmp(name, &names[offset]) == 0)
73 			break;
74 		offset += strlen(&names[offset]) + 1;
75 	}
76 	OF_prop_free(names);
77 	if (offset < nameslen)
78 		return (fdt_pinctrl_configure(client, i));
79 	else
80 		return (ENOENT);
81 }
82 
83 static int
84 pinctrl_register_children(device_t pinctrl, phandle_t parent,
85     const char *pinprop)
86 {
87 	phandle_t node;
88 
89 	/*
90 	 * Recursively descend from parent, looking for nodes that have the
91 	 * given property, and associate the pinctrl device_t with each one.
92 	 */
93 	for (node = OF_child(parent); node != 0; node = OF_peer(node)) {
94 		pinctrl_register_children(pinctrl, node, pinprop);
95 		if (pinprop == NULL || OF_hasprop(node, pinprop)) {
96 			OF_device_register_xref(OF_xref_from_node(node),
97 			    pinctrl);
98 		}
99 	}
100 	return (0);
101 }
102 
103 int
104 fdt_pinctrl_register(device_t pinctrl, const char *pinprop)
105 {
106 	phandle_t node;
107 	int ret;
108 
109 	TSENTER();
110 	node = ofw_bus_get_node(pinctrl);
111 	OF_device_register_xref(OF_xref_from_node(node), pinctrl);
112 	ret = pinctrl_register_children(pinctrl, node, pinprop);
113 	TSEXIT();
114 
115 	return (ret);
116 }
117 
118 static int
119 pinctrl_configure_children(device_t pinctrl, phandle_t parent)
120 {
121 	phandle_t node, *configs;
122 	int i, nconfigs;
123 
124 	TSENTER();
125 
126 	for (node = OF_child(parent); node != 0; node = OF_peer(node)) {
127 		if (!ofw_bus_node_status_okay(node))
128 			continue;
129 		pinctrl_configure_children(pinctrl, node);
130 		nconfigs = OF_getencprop_alloc_multi(node, "pinctrl-0",
131 		    sizeof(*configs), (void **)&configs);
132 		if (nconfigs <= 0)
133 			continue;
134 		if (bootverbose) {
135 			char name[32];
136 			OF_getprop(node, "name", &name, sizeof(name));
137 			printf("Processing %d pin-config node(s) in pinctrl-0 for %s\n",
138 			    nconfigs, name);
139 		}
140 		for (i = 0; i < nconfigs; i++) {
141 			if (OF_device_from_xref(configs[i]) == pinctrl)
142 				FDT_PINCTRL_CONFIGURE(pinctrl, configs[i]);
143 		}
144 		OF_prop_free(configs);
145 	}
146 	TSEXIT();
147 	return (0);
148 }
149 
150 int
151 fdt_pinctrl_configure_tree(device_t pinctrl)
152 {
153 
154 	return (pinctrl_configure_children(pinctrl, OF_peer(0)));
155 }
156 
157