fdt_pinctrl.c revision 1.2.4.1 1 /* $NetBSD: fdt_pinctrl.c,v 1.2.4.1 2016/11/04 14:49:08 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Martin Fouts
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: fdt_pinctrl.c,v 1.2.4.1 2016/11/04 14:49:08 pgoyette Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kmem.h>
35
36 #include <libfdt.h>
37 #include <dev/fdt/fdtvar.h>
38
39 struct fdtbus_pinctrl_controller {
40 int pc_phandle;
41 void *pc_cookie;
42 const struct fdtbus_pinctrl_controller_func *pc_funcs;
43
44 struct fdtbus_pinctrl_controller *pc_next;
45 };
46
47 static struct fdtbus_pinctrl_controller *fdtbus_pc = NULL;
48
49 int
50 fdtbus_register_pinctrl_config(void *cookie, int phandle,
51 const struct fdtbus_pinctrl_controller_func *funcs)
52 {
53 struct fdtbus_pinctrl_controller *pc;
54
55 pc = kmem_alloc(sizeof(*pc), KM_SLEEP);
56 pc->pc_cookie = cookie;
57 pc->pc_phandle = phandle;
58 pc->pc_funcs = funcs;
59
60 pc->pc_next = fdtbus_pc;
61 fdtbus_pc = pc;
62
63 return 0;
64 }
65
66 static struct fdtbus_pinctrl_controller *
67 fdtbus_pinctrl_lookup(int phandle)
68 {
69 struct fdtbus_pinctrl_controller *pc;
70
71 for (pc = fdtbus_pc; pc; pc = pc->pc_next)
72 if (pc->pc_phandle == phandle)
73 return pc;
74
75 return NULL;
76 }
77
78 int
79 fdtbus_pinctrl_set_config_index(int phandle, u_int index)
80 {
81 char buf[80];
82 int len, handle;
83 struct fdtbus_pinctrl_controller *pc;
84
85 snprintf(buf, 80, "pinctrl-%d", index);
86
87 len = OF_getprop(phandle, buf, (char *)&handle,
88 sizeof(handle));
89 if (len != sizeof(int)) {
90 printf("%s: couldn't get %s.\n", __func__, buf);
91 return -1;
92 }
93
94 handle = fdtbus_get_phandle_from_native(be32toh(handle));
95
96 pc = fdtbus_pinctrl_lookup(handle);
97 if (!pc) {
98 printf("%s: Couldn't get handle %d for %s\n", __func__, handle,
99 buf);
100 return -1;
101 }
102
103 return pc->pc_funcs->set_config(pc->pc_cookie);
104 }
105
106 int
107 fdtbus_pinctrl_set_config(int phandle, const char *cfgname)
108 {
109 int index = 0;
110 int len;
111 char *result;
112 char *next;
113
114 len = OF_getproplen(phandle, "pinctrl-names");
115 if (len <= 0)
116 return -1;
117 result = kmem_zalloc(len, KM_SLEEP);
118 OF_getprop(phandle, "pinctrl-names", result, len);
119
120 next = result;
121 while (next - result < len) {
122 if (!strcmp(next, cfgname)) {
123 kmem_free(result, len);
124 return fdtbus_pinctrl_set_config_index(phandle, index);
125 }
126 index++;
127 while (*next)
128 next++;
129 next++;
130 }
131
132 kmem_free(result, len);
133 return -1;
134 }
135