exynos_pinctrl.c revision 1.9.10.1 1 /* $NetBSD: exynos_pinctrl.c,v 1.9.10.1 2017/07/18 19:13:08 snj 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.9.10.1 2017/07/18 19:13:08 snj 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(device_t, const void *, size_t);
66 static void exynos_parse_config(int, struct exynos_gpio_pin_cfg *);
67
68 static struct fdtbus_pinctrl_controller_func exynos_pinctrl_controller_func = {
69 .set_config = exynos_pinctrl_set_cfg
70 };
71
72 CFATTACH_DECL_NEW(exynos_pinctrl, sizeof(struct exynos_pinctrl_softc),
73 exynos_pinctrl_match, exynos_pinctrl_attach, NULL, NULL);
74
75 static int
76 exynos_pinctrl_match(device_t parent, cfdata_t cf, void *aux)
77 {
78 const char * const compatible[] = { "samsung,exynos5420-pinctrl",
79 NULL };
80 struct fdt_attach_args * const faa = aux;
81 return of_match_compatible(faa->faa_phandle, compatible);
82 }
83
84 static bool
85 is_pinctrl(int phandle)
86 {
87 int len = OF_getproplen(phandle, "samsung,pins");
88 return len > 0;
89 }
90
91 static void
92 exynos_pinctrl_attach(device_t parent, device_t self, void *aux)
93 {
94 struct exynos_pinctrl_softc * const sc
95 = kmem_zalloc(sizeof(*sc), KM_SLEEP);
96 struct fdt_attach_args * const faa = aux;
97 bus_addr_t addr;
98 bus_size_t size;
99 int error;
100 int child;
101
102 if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
103 aprint_error(": couldn't get registers\n");
104 return;
105 }
106
107 aprint_normal(" pinctl @ 0x%08x ", (uint)addr);
108 sc->sc_dev = self;
109 sc->sc_bst = faa->faa_bst;
110 error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
111 if (error) {
112 aprint_error(": couldn't map %#llx: %d",
113 (uint64_t)addr, error);
114 return;
115 }
116
117 aprint_naive("\n");
118 aprint_normal("\n");
119
120 for (child = OF_child(faa->faa_phandle); child;
121 child = OF_peer(child)) {
122
123 if (of_getprop_bool(child, "gpio-controller")) {
124 exynos_gpio_bank_config(sc, faa, child);
125 }
126
127 if (is_pinctrl(child)) {
128 fdtbus_register_pinctrl_config(self, child,
129 &exynos_pinctrl_controller_func);
130 }
131 }
132
133 fdtbus_pinctrl_configure();
134 }
135
136 static void
137 exynos_parse_config(int phandle, struct exynos_gpio_pin_cfg *gc)
138 {
139 of_getprop_uint32(phandle, "samsung,pin-function", &gc->cfg);
140 of_getprop_uint32(phandle, "samsung,pin-pud", &gc->pud);
141 of_getprop_uint32(phandle, "samsung,pin-drv", &gc->drv);
142 of_getprop_uint32(phandle, "samsung,pin-conpwd", &gc->conpwd);
143 of_getprop_uint32(phandle, "samsung,pin-pudpwd", &gc->pudpwd);
144 }
145
146 static int
147 exynos_do_config(struct exynos_pinctrl_config *pc)
148 {
149 struct exynos_gpio_pin_cfg *gc = &pc->pc_pincfg;
150 struct exynos_gpio_bank *bank;
151 const char *pins;
152
153 int pins_len = OF_getproplen(pc->pc_phandle, "samsung,pins");
154 if (pins_len <= 0)
155 return -1;
156
157 for (pins = fdtbus_get_string(pc->pc_phandle, "samsung,pins");
158 pins_len > 0;
159 pins_len -= strlen(pins) + 1, pins += strlen(pins) + 1) {
160 bank = exynos_gpio_bank_lookup(pins);
161 if (bank == NULL) {
162 aprint_error_dev(pc->pc_sc->sc_dev,
163 "unknown pin name '%s'\n", pins);
164 continue;
165 }
166 exynos_gpio_pin_ctl_write(bank, gc);
167 }
168
169 return 0;
170 }
171
172 static int
173 exynos_pinctrl_set_cfg(device_t dev, const void *data, size_t len)
174 {
175 struct exynos_pinctrl_config pc;
176
177 if (len != 4)
178 return -1;
179
180 pc.pc_phandle = fdtbus_get_phandle_from_native(be32dec(data));
181 pc.pc_sc = device_private(dev);
182 exynos_parse_config(pc.pc_phandle, &pc.pc_pincfg);
183
184 return exynos_do_config(&pc);
185 }
186