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