xref: /freebsd/sys/dev/fdt/fdt_pinctrl.c (revision f5463265955b829775bbb32e1fd0bc11dafc36ce)
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/param.h>
28 
29 #include <dev/ofw/ofw_bus.h>
30 #include <dev/ofw/ofw_bus_subr.h>
31 
32 #include "fdt_pinctrl_if.h"
33 
34 #include <dev/fdt/fdt_common.h>
35 #include <dev/fdt/fdt_pinctrl.h>
36 
37 int
38 fdt_pinctrl_configure(device_t client, u_int index)
39 {
40 	device_t pinctrl;
41 	phandle_t *configs;
42 	int i, nconfigs;
43 	char name[16];
44 
45 	snprintf(name, sizeof(name), "pinctrl-%u", index);
46 	nconfigs = OF_getencprop_alloc_multi(ofw_bus_get_node(client), name,
47 	    sizeof(*configs), (void **)&configs);
48 	if (nconfigs < 0)
49 		return (ENOENT);
50 	if (nconfigs == 0)
51 		return (0); /* Empty property is documented as valid. */
52 	for (i = 0; i < nconfigs; i++) {
53 		if ((pinctrl = OF_device_from_xref(configs[i])) != NULL)
54 			FDT_PINCTRL_CONFIGURE(pinctrl, configs[i]);
55 	}
56 	OF_prop_free(configs);
57 	return (0);
58 }
59 
60 int
61 fdt_pinctrl_configure_by_name(device_t client, const char * name)
62 {
63 	char * names;
64 	int i, offset, nameslen;
65 
66 	nameslen = OF_getprop_alloc(ofw_bus_get_node(client), "pinctrl-names",
67 	    (void **)&names);
68 	if (nameslen <= 0)
69 		return (ENOENT);
70 	for (i = 0, offset = 0; offset < nameslen; i++) {
71 		if (strcmp(name, &names[offset]) == 0)
72 			break;
73 		offset += strlen(&names[offset]) + 1;
74 	}
75 	OF_prop_free(names);
76 	if (offset < nameslen)
77 		return (fdt_pinctrl_configure(client, i));
78 	else
79 		return (ENOENT);
80 }
81 
82 static int
83 pinctrl_register_children(device_t pinctrl, phandle_t parent,
84     const char *pinprop)
85 {
86 	phandle_t node;
87 
88 	/*
89 	 * Recursively descend from parent, looking for nodes that have the
90 	 * given property, and associate the pinctrl device_t with each one.
91 	 */
92 	for (node = OF_child(parent); node != 0; node = OF_peer(node)) {
93 		pinctrl_register_children(pinctrl, node, pinprop);
94 		if (pinprop == NULL || OF_hasprop(node, pinprop)) {
95 			OF_device_register_xref(OF_xref_from_node(node),
96 			    pinctrl);
97 		}
98 	}
99 	return (0);
100 }
101 
102 int
103 fdt_pinctrl_register(device_t pinctrl, const char *pinprop)
104 {
105 	phandle_t node;
106 	int ret;
107 
108 	TSENTER();
109 	node = ofw_bus_get_node(pinctrl);
110 	OF_device_register_xref(OF_xref_from_node(node), pinctrl);
111 	ret = pinctrl_register_children(pinctrl, node, pinprop);
112 	TSEXIT();
113 
114 	return (ret);
115 }
116 
117 static int
118 pinctrl_configure_children(device_t pinctrl, phandle_t parent)
119 {
120 	phandle_t node, *configs;
121 	int i, nconfigs;
122 
123 	TSENTER();
124 
125 	for (node = OF_child(parent); node != 0; node = OF_peer(node)) {
126 		if (!ofw_bus_node_status_okay(node))
127 			continue;
128 		pinctrl_configure_children(pinctrl, node);
129 		nconfigs = OF_getencprop_alloc_multi(node, "pinctrl-0",
130 		    sizeof(*configs), (void **)&configs);
131 		if (nconfigs <= 0)
132 			continue;
133 		if (bootverbose) {
134 			char name[32];
135 			OF_getprop(node, "name", &name, sizeof(name));
136 			printf("Processing %d pin-config node(s) in pinctrl-0 for %s\n",
137 			    nconfigs, name);
138 		}
139 		for (i = 0; i < nconfigs; i++) {
140 			if (OF_device_from_xref(configs[i]) == pinctrl)
141 				FDT_PINCTRL_CONFIGURE(pinctrl, configs[i]);
142 		}
143 		OF_prop_free(configs);
144 	}
145 	TSEXIT();
146 	return (0);
147 }
148 
149 int
150 fdt_pinctrl_configure_tree(device_t pinctrl)
151 {
152 
153 	return (pinctrl_configure_children(pinctrl, OF_peer(0)));
154 }
155 
156