exynos_pinctrl.c revision 1.6.2.3 1 /* $NetBSD: exynos_pinctrl.c,v 1.6.2.3 2016/03/19 11:29:57 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.6.2.3 2016/03/19 11:29:57 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[] = { "samsung,exynos5420-pinctrl",
80 NULL };
81 struct fdt_attach_args * const faa = aux;
82 return of_match_compatible(faa->faa_phandle, compatible);
83 }
84
85 static bool
86 is_pinctrl(int phandle)
87 {
88 int len = OF_getproplen(phandle, "samsung,pins");
89 return len > 0;
90 }
91
92 static void
93 exynos_pinctrl_attach(device_t parent, device_t self, void *aux)
94 {
95 struct exynos_pinctrl_softc * const sc
96 = kmem_zalloc(sizeof(*sc), KM_SLEEP);
97 struct fdt_attach_args * const faa = aux;
98 bus_addr_t addr;
99 bus_size_t size;
100 int error;
101 int child;
102
103 if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
104 aprint_error(": couldn't get registers\n");
105 return;
106 }
107
108 aprint_normal(" pinctl @ 0x%08x ", (uint)addr);
109 sc->sc_dev = self;
110 sc->sc_bst = faa->faa_bst;
111 error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
112 if (error) {
113 aprint_error(": couldn't map %#llx: %d",
114 (uint64_t)addr, error);
115 return;
116 }
117
118 aprint_naive("\n");
119 aprint_normal("\n");
120
121 for (child = OF_child(faa->faa_phandle); child;
122 child = OF_peer(child)) {
123
124 if (of_getprop_bool(child, "gpio-controller")) {
125 exynos_gpio_bank_config(sc, faa, child);
126 }
127
128 if (is_pinctrl(child)) {
129 struct exynos_pinctrl_config *pc;
130 pc = kmem_alloc(sizeof(*pc), KM_SLEEP);
131 pc->pc_phandle = child;
132 pc->pc_sc = sc;
133 pc->pc_pincfg = exynos_parse_config(pc);
134 fdtbus_register_pinctrl_config(pc, child,
135 &exynos_pinctrl_controller_func);
136 }
137 }
138 }
139
140 static struct exynos_gpio_pin_cfg *
141 exynos_parse_config(struct exynos_pinctrl_config *pc)
142 {
143 struct exynos_gpio_pin_cfg *gc = kmem_zalloc(sizeof(*gc), KM_SLEEP);
144 int len;
145 int value;
146
147 len = OF_getprop(pc->pc_phandle, "samsung,pin-function",
148 &value, sizeof(value));
149 if (len > 0) {
150 gc->cfg = be32toh(value);
151 }
152
153 len = OF_getprop(pc->pc_phandle, "samsung,pin-pud", &value,
154 sizeof(&value));
155 if (len > 0) {
156 gc->pud = be32toh(value);
157 }
158
159 len = OF_getprop(pc->pc_phandle, "samsung,pin-drv", &value,
160 sizeof(&value));
161 if (len > 0) {
162 gc->drv = be32toh(value);
163 }
164
165 len = OF_getprop(pc->pc_phandle, "samsung,pin-conpwd", &value,
166 sizeof(&value));
167 if (len > 0) {
168 gc->conpwd = be32toh(value);
169 }
170
171 len = OF_getprop(pc->pc_phandle, "samsung,pin-pudpwd", &value,
172 sizeof(&value));
173 if (len > 0) {
174 gc->pudpwd = be32toh(value);
175 }
176 return gc;
177 }
178
179 static int
180 exynos_do_config(struct exynos_pinctrl_config *pc)
181 {
182 struct exynos_gpio_pin_cfg *gc = pc->pc_pincfg;
183 struct exynos_gpio_bank *bank;
184 int len;
185 char result[20];
186
187 if (gc == NULL) {
188 printf("%s: No configuration available\n", __func__);
189 return -1;
190 }
191
192 len = OF_getprop(pc->pc_phandle, "samsung,pins", result,
193 sizeof(result));
194 if (len <= 0) {
195 printf("%s: couldn't get pins. (%d)\n", __func__,
196 pc->pc_phandle);
197 return -1;
198 }
199
200 bank = exynos_gpio_bank_lookup(&result[0]);
201 if (!bank) {
202 printf("%s: Couldn't get bank \"%s\".\n", __func__, result);
203 return -1;
204 }
205
206 exynos_gpio_pin_ctl_write(bank, gc);
207 return 0;
208 }
209
210 static int
211 exynos_pinctrl_set_cfg(void *cookie)
212 {
213 struct exynos_pinctrl_config *pc = cookie;
214 return exynos_do_config(pc);
215 }
216