exynos_pinctrl.c revision 1.10 1 /* $NetBSD: exynos_pinctrl.c,v 1.10 2017/06/22 06:42:38 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Marty Fouts
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "opt_exynos.h"
33 #include "opt_arm_debug.h"
34 #include "gpio.h"
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(1, "$NetBSD: exynos_pinctrl.c,v 1.10 2017/06/22 06:42:38 skrll Exp $");
38
39 #include <sys/param.h>
40 #include <sys/bus.h>
41 #include <sys/device.h>
42 #include <sys/intr.h>
43 #include <sys/systm.h>
44 #include <sys/kmem.h>
45 #include <sys/gpio.h>
46
47 #include <dev/gpio/gpiovar.h>
48
49 #include <arm/samsung/exynos_reg.h>
50 #include <arm/samsung/exynos_var.h>
51 #include <arm/samsung/exynos_intr.h>
52 #include <arm/samsung/exynos_pinctrl.h>
53
54 #include <dev/fdt/fdtvar.h>
55
56 struct exynos_pinctrl_config {
57 int pc_phandle;
58 struct exynos_gpio_pin_cfg *pc_pincfg;
59 struct exynos_pinctrl_softc *pc_sc;
60 };
61
62 static int exynos_pinctrl_match(device_t, cfdata_t, void *);
63 static void exynos_pinctrl_attach(device_t, device_t, void *);
64
65 static int exynos_pinctrl_set_cfg(void *);
66 static struct exynos_gpio_pin_cfg *
67 exynos_parse_config(struct exynos_pinctrl_config *pc);
68
69 static struct fdtbus_pinctrl_controller_func exynos_pinctrl_controller_func = {
70 .set_config = exynos_pinctrl_set_cfg
71 };
72
73 CFATTACH_DECL_NEW(exynos_pinctrl, sizeof(struct exynos_pinctrl_softc),
74 exynos_pinctrl_match, exynos_pinctrl_attach, NULL, NULL);
75
76 static int
77 exynos_pinctrl_match(device_t parent, cfdata_t cf, void *aux)
78 {
79 const char * const compatible[] = {
80 "samsung,exynos5410-pinctrl",
81 "samsung,exynos5420-pinctrl",
82 NULL };
83 struct fdt_attach_args * const faa = aux;
84 return of_match_compatible(faa->faa_phandle, compatible);
85 }
86
87 static bool
88 is_pinctrl(int phandle)
89 {
90 int len = OF_getproplen(phandle, "samsung,pins");
91 return len > 0;
92 }
93
94 static void
95 exynos_pinctrl_attach(device_t parent, device_t self, void *aux)
96 {
97 struct exynos_pinctrl_softc * const sc
98 = kmem_zalloc(sizeof(*sc), KM_SLEEP);
99 struct fdt_attach_args * const faa = aux;
100 bus_addr_t addr;
101 bus_size_t size;
102 int error;
103 int child;
104
105 if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
106 aprint_error(": couldn't get registers\n");
107 return;
108 }
109
110 aprint_normal(" pinctl @ 0x%08x ", (uint)addr);
111 sc->sc_dev = self;
112 sc->sc_bst = faa->faa_bst;
113 error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
114 if (error) {
115 aprint_error(": couldn't map %#llx: %d",
116 (uint64_t)addr, error);
117 return;
118 }
119
120 aprint_naive("\n");
121 aprint_normal("\n");
122
123 for (child = OF_child(faa->faa_phandle); child;
124 child = OF_peer(child)) {
125
126 if (of_getprop_bool(child, "gpio-controller")) {
127 exynos_gpio_bank_config(sc, faa, child);
128 }
129
130 if (is_pinctrl(child)) {
131 struct exynos_pinctrl_config *pc;
132 pc = kmem_alloc(sizeof(*pc), KM_SLEEP);
133 pc->pc_phandle = child;
134 pc->pc_sc = sc;
135 pc->pc_pincfg = exynos_parse_config(pc);
136 fdtbus_register_pinctrl_config(pc, child,
137 &exynos_pinctrl_controller_func);
138 }
139 }
140 }
141
142 static struct exynos_gpio_pin_cfg *
143 exynos_parse_config(struct exynos_pinctrl_config *pc)
144 {
145 struct exynos_gpio_pin_cfg *gc = kmem_zalloc(sizeof(*gc), KM_SLEEP);
146 int len;
147 int value;
148
149 len = OF_getprop(pc->pc_phandle, "samsung,pin-function",
150 &value, sizeof(value));
151 if (len > 0) {
152 gc->cfg = be32toh(value);
153 }
154
155 len = OF_getprop(pc->pc_phandle, "samsung,pin-pud", &value,
156 sizeof(&value));
157 if (len > 0) {
158 gc->pud = be32toh(value);
159 }
160
161 len = OF_getprop(pc->pc_phandle, "samsung,pin-drv", &value,
162 sizeof(&value));
163 if (len > 0) {
164 gc->drv = be32toh(value);
165 }
166
167 len = OF_getprop(pc->pc_phandle, "samsung,pin-conpwd", &value,
168 sizeof(&value));
169 if (len > 0) {
170 gc->conpwd = be32toh(value);
171 }
172
173 len = OF_getprop(pc->pc_phandle, "samsung,pin-pudpwd", &value,
174 sizeof(&value));
175 if (len > 0) {
176 gc->pudpwd = be32toh(value);
177 }
178 return gc;
179 }
180
181 static int
182 exynos_do_config(struct exynos_pinctrl_config *pc)
183 {
184 struct exynos_gpio_pin_cfg *gc = pc->pc_pincfg;
185 struct exynos_gpio_bank *bank;
186 int len;
187 char result[20];
188
189 if (gc == NULL) {
190 printf("%s: No configuration available\n", __func__);
191 return -1;
192 }
193
194 len = OF_getprop(pc->pc_phandle, "samsung,pins", result,
195 sizeof(result));
196 if (len <= 0) {
197 printf("%s: couldn't get pins. (%d)\n", __func__,
198 pc->pc_phandle);
199 return -1;
200 }
201
202 bank = exynos_gpio_bank_lookup(&result[0]);
203 if (!bank) {
204 printf("%s: Couldn't get bank \"%s\".\n", __func__, result);
205 return -1;
206 }
207
208 exynos_gpio_pin_ctl_write(bank, gc);
209 return 0;
210 }
211
212 static int
213 exynos_pinctrl_set_cfg(void *cookie)
214 {
215 struct exynos_pinctrl_config *pc = cookie;
216 return exynos_do_config(pc);
217 }
218