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