meson_pinctrl.c revision 1.7 1 1.7 ryo /* $NetBSD: meson_pinctrl.c,v 1.7 2021/01/01 07:21:58 ryo Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2019 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include "opt_soc.h"
30 1.1 jmcneill
31 1.1 jmcneill #include <sys/cdefs.h>
32 1.7 ryo __KERNEL_RCSID(0, "$NetBSD: meson_pinctrl.c,v 1.7 2021/01/01 07:21:58 ryo Exp $");
33 1.1 jmcneill
34 1.1 jmcneill #include <sys/param.h>
35 1.1 jmcneill #include <sys/bus.h>
36 1.1 jmcneill #include <sys/device.h>
37 1.1 jmcneill #include <sys/systm.h>
38 1.1 jmcneill #include <sys/kernel.h>
39 1.1 jmcneill #include <sys/mutex.h>
40 1.1 jmcneill #include <sys/kmem.h>
41 1.1 jmcneill #include <sys/gpio.h>
42 1.1 jmcneill
43 1.1 jmcneill #include <dev/gpio/gpiovar.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <dev/fdt/fdtvar.h>
46 1.1 jmcneill
47 1.1 jmcneill #include <arm/amlogic/meson_pinctrl.h>
48 1.1 jmcneill
49 1.1 jmcneill struct meson_pinctrl_softc {
50 1.1 jmcneill device_t sc_dev;
51 1.1 jmcneill bus_space_tag_t sc_bst;
52 1.1 jmcneill bus_space_handle_t sc_bsh_mux;
53 1.1 jmcneill bus_space_handle_t sc_bsh_pull;
54 1.1 jmcneill bus_space_handle_t sc_bsh_pull_enable;
55 1.1 jmcneill bus_space_handle_t sc_bsh_gpio;
56 1.1 jmcneill int sc_phandle;
57 1.1 jmcneill int sc_phandle_gpio;
58 1.1 jmcneill
59 1.1 jmcneill kmutex_t sc_lock;
60 1.1 jmcneill
61 1.1 jmcneill const struct meson_pinctrl_config *sc_conf;
62 1.1 jmcneill
63 1.1 jmcneill struct gpio_chipset_tag sc_gp;
64 1.1 jmcneill gpio_pin_t *sc_pins;
65 1.1 jmcneill };
66 1.1 jmcneill
67 1.1 jmcneill struct meson_pinctrl_gpio_pin {
68 1.1 jmcneill struct meson_pinctrl_softc *pin_sc;
69 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def;
70 1.1 jmcneill int pin_flags;
71 1.1 jmcneill bool pin_actlo;
72 1.1 jmcneill };
73 1.1 jmcneill
74 1.1 jmcneill static const struct of_compat_data compat_data[] = {
75 1.1 jmcneill #ifdef SOC_MESON8B
76 1.1 jmcneill { "amlogic,meson8b-aobus-pinctrl", (uintptr_t)&meson8b_aobus_pinctrl_config },
77 1.1 jmcneill { "amlogic,meson8b-cbus-pinctrl", (uintptr_t)&meson8b_cbus_pinctrl_config },
78 1.1 jmcneill #endif
79 1.3 jmcneill #ifdef SOC_MESONGXBB
80 1.3 jmcneill { "amlogic,meson-gxbb-aobus-pinctrl", (uintptr_t)&mesongxbb_aobus_pinctrl_config },
81 1.3 jmcneill { "amlogic,meson-gxbb-periphs-pinctrl", (uintptr_t)&mesongxbb_periphs_pinctrl_config },
82 1.3 jmcneill #endif
83 1.5 jmcneill #ifdef SOC_MESONGXL
84 1.5 jmcneill { "amlogic,meson-gxl-aobus-pinctrl", (uintptr_t)&mesongxl_aobus_pinctrl_config },
85 1.5 jmcneill { "amlogic,meson-gxl-periphs-pinctrl", (uintptr_t)&mesongxl_periphs_pinctrl_config },
86 1.5 jmcneill #endif
87 1.7 ryo #ifdef SOC_MESONG12
88 1.7 ryo { "amlogic,meson-g12a-aobus-pinctrl", (uintptr_t)&mesong12a_aobus_pinctrl_config },
89 1.7 ryo { "amlogic,meson-g12a-periphs-pinctrl", (uintptr_t)&mesong12a_periphs_pinctrl_config },
90 1.7 ryo #endif
91 1.1 jmcneill { NULL, 0 }
92 1.1 jmcneill };
93 1.1 jmcneill
94 1.1 jmcneill #define MUX_READ(sc, reg) \
95 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh_mux, (reg))
96 1.1 jmcneill #define MUX_WRITE(sc, reg, val) \
97 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh_mux, (reg), (val))
98 1.1 jmcneill
99 1.1 jmcneill static const struct meson_pinctrl_group *
100 1.1 jmcneill meson_pinctrl_find_group(struct meson_pinctrl_softc *sc,
101 1.1 jmcneill const char *name)
102 1.1 jmcneill {
103 1.1 jmcneill const struct meson_pinctrl_group *group;
104 1.1 jmcneill u_int n;
105 1.1 jmcneill
106 1.1 jmcneill for (n = 0; n < sc->sc_conf->ngroups; n++) {
107 1.1 jmcneill group = &sc->sc_conf->groups[n];
108 1.1 jmcneill if (strcmp(group->name, name) == 0)
109 1.1 jmcneill return group;
110 1.1 jmcneill }
111 1.1 jmcneill
112 1.1 jmcneill return NULL;
113 1.1 jmcneill }
114 1.1 jmcneill
115 1.1 jmcneill static bool
116 1.1 jmcneill meson_pinctrl_group_in_bank(struct meson_pinctrl_softc *sc,
117 1.1 jmcneill const struct meson_pinctrl_group *group, u_int bankno)
118 1.1 jmcneill {
119 1.1 jmcneill u_int n;
120 1.1 jmcneill
121 1.1 jmcneill for (n = 0; n < group->nbank; n++) {
122 1.1 jmcneill if (group->bank[n] == bankno)
123 1.1 jmcneill return true;
124 1.1 jmcneill }
125 1.1 jmcneill
126 1.1 jmcneill return false;
127 1.1 jmcneill }
128 1.1 jmcneill
129 1.1 jmcneill static void
130 1.1 jmcneill meson_pinctrl_set_group(struct meson_pinctrl_softc *sc,
131 1.1 jmcneill const struct meson_pinctrl_group *group, bool enable)
132 1.1 jmcneill {
133 1.1 jmcneill uint32_t val;
134 1.1 jmcneill
135 1.1 jmcneill val = MUX_READ(sc, group->reg);
136 1.7 ryo if (group->mask == 0) {
137 1.7 ryo if (enable)
138 1.7 ryo val |= __BIT(group->bit);
139 1.7 ryo else
140 1.7 ryo val &= ~__BIT(group->bit);
141 1.7 ryo } else {
142 1.7 ryo val &= ~group->mask;
143 1.7 ryo if (enable)
144 1.7 ryo val |= __SHIFTIN(group->func, group->mask);
145 1.7 ryo }
146 1.1 jmcneill MUX_WRITE(sc, group->reg, val);
147 1.1 jmcneill }
148 1.1 jmcneill
149 1.1 jmcneill static void
150 1.1 jmcneill meson_pinctrl_setfunc(struct meson_pinctrl_softc *sc, const char *name)
151 1.1 jmcneill {
152 1.1 jmcneill const struct meson_pinctrl_group *group, *target_group;
153 1.1 jmcneill u_int n, bank;
154 1.1 jmcneill
155 1.1 jmcneill target_group = meson_pinctrl_find_group(sc, name);
156 1.1 jmcneill if (target_group == NULL) {
157 1.1 jmcneill aprint_error_dev(sc->sc_dev, "function '%s' not supported\n", name);
158 1.1 jmcneill return;
159 1.1 jmcneill }
160 1.1 jmcneill
161 1.1 jmcneill /* Disable conflicting groups */
162 1.1 jmcneill for (n = 0; n < sc->sc_conf->ngroups; n++) {
163 1.1 jmcneill group = &sc->sc_conf->groups[n];
164 1.1 jmcneill if (target_group == group)
165 1.1 jmcneill continue;
166 1.1 jmcneill for (bank = 0; bank < target_group->nbank; bank++) {
167 1.1 jmcneill if (meson_pinctrl_group_in_bank(sc, group, target_group->bank[bank]))
168 1.1 jmcneill meson_pinctrl_set_group(sc, group, false);
169 1.1 jmcneill }
170 1.1 jmcneill }
171 1.1 jmcneill
172 1.1 jmcneill /* Enable target group */
173 1.1 jmcneill meson_pinctrl_set_group(sc, target_group, true);
174 1.1 jmcneill }
175 1.1 jmcneill
176 1.1 jmcneill static int
177 1.1 jmcneill meson_pinctrl_set_config(device_t dev, const void *data, size_t len)
178 1.1 jmcneill {
179 1.1 jmcneill struct meson_pinctrl_softc * const sc = device_private(dev);
180 1.1 jmcneill const char *groups;
181 1.1 jmcneill int groups_len;
182 1.1 jmcneill
183 1.1 jmcneill if (len != 4)
184 1.1 jmcneill return -1;
185 1.1 jmcneill
186 1.1 jmcneill const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
187 1.1 jmcneill const int mux = of_find_firstchild_byname(phandle, "mux");
188 1.1 jmcneill if (mux == -1)
189 1.1 jmcneill return -1;
190 1.1 jmcneill
191 1.2 thorpej groups = fdtbus_pinctrl_parse_groups(mux, &groups_len);
192 1.2 thorpej if (groups == NULL)
193 1.1 jmcneill return -1;
194 1.1 jmcneill
195 1.1 jmcneill for (; groups_len > 0;
196 1.1 jmcneill groups_len -= strlen(groups) + 1, groups += strlen(groups) + 1) {
197 1.1 jmcneill meson_pinctrl_setfunc(sc, groups);
198 1.1 jmcneill }
199 1.1 jmcneill
200 1.1 jmcneill return 0;
201 1.1 jmcneill }
202 1.1 jmcneill
203 1.1 jmcneill static struct fdtbus_pinctrl_controller_func meson_pinctrl_funcs = {
204 1.1 jmcneill .set_config = meson_pinctrl_set_config,
205 1.1 jmcneill };
206 1.1 jmcneill
207 1.1 jmcneill static bus_space_handle_t
208 1.1 jmcneill meson_pinctrl_gpio_handle(struct meson_pinctrl_softc *sc,
209 1.1 jmcneill const struct meson_pinctrl_gpioreg *gpioreg)
210 1.1 jmcneill {
211 1.1 jmcneill switch (gpioreg->type) {
212 1.1 jmcneill case MESON_PINCTRL_REGTYPE_PULL:
213 1.1 jmcneill return sc->sc_bsh_pull;
214 1.1 jmcneill case MESON_PINCTRL_REGTYPE_PULL_ENABLE:
215 1.1 jmcneill return sc->sc_bsh_pull_enable;
216 1.1 jmcneill case MESON_PINCTRL_REGTYPE_GPIO:
217 1.1 jmcneill return sc->sc_bsh_gpio;
218 1.1 jmcneill default:
219 1.1 jmcneill panic("unsupported GPIO regtype %d", gpioreg->type);
220 1.1 jmcneill }
221 1.1 jmcneill }
222 1.1 jmcneill
223 1.1 jmcneill static int
224 1.1 jmcneill meson_pinctrl_pin_read(void *priv, int pin)
225 1.1 jmcneill {
226 1.1 jmcneill struct meson_pinctrl_softc * const sc = priv;
227 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def = &sc->sc_conf->gpios[pin];
228 1.1 jmcneill const struct meson_pinctrl_gpioreg *gpio_reg = &pin_def->in;
229 1.1 jmcneill bus_space_handle_t bsh;
230 1.1 jmcneill uint32_t data;
231 1.1 jmcneill int val;
232 1.1 jmcneill
233 1.1 jmcneill KASSERT(pin < sc->sc_conf->ngpios);
234 1.1 jmcneill
235 1.1 jmcneill bsh = meson_pinctrl_gpio_handle(sc, gpio_reg);
236 1.1 jmcneill data = bus_space_read_4(sc->sc_bst, bsh, gpio_reg->reg);
237 1.1 jmcneill val = __SHIFTOUT(data, gpio_reg->mask);
238 1.1 jmcneill
239 1.1 jmcneill return val;
240 1.1 jmcneill }
241 1.1 jmcneill
242 1.1 jmcneill static void
243 1.1 jmcneill meson_pinctrl_pin_write(void *priv, int pin, int val)
244 1.1 jmcneill {
245 1.1 jmcneill struct meson_pinctrl_softc * const sc = priv;
246 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def = &sc->sc_conf->gpios[pin];
247 1.1 jmcneill const struct meson_pinctrl_gpioreg *gpio_reg = &pin_def->out;
248 1.1 jmcneill bus_space_handle_t bsh;
249 1.1 jmcneill uint32_t data;
250 1.1 jmcneill
251 1.1 jmcneill KASSERT(pin < sc->sc_conf->ngpios);
252 1.1 jmcneill
253 1.1 jmcneill bsh = meson_pinctrl_gpio_handle(sc, gpio_reg);
254 1.1 jmcneill
255 1.1 jmcneill mutex_enter(&sc->sc_lock);
256 1.1 jmcneill data = bus_space_read_4(sc->sc_bst, bsh, gpio_reg->reg);
257 1.1 jmcneill if (val)
258 1.1 jmcneill data |= gpio_reg->mask;
259 1.1 jmcneill else
260 1.1 jmcneill data &= ~gpio_reg->mask;
261 1.1 jmcneill bus_space_write_4(sc->sc_bst, bsh, gpio_reg->reg, data);
262 1.1 jmcneill mutex_exit(&sc->sc_lock);
263 1.1 jmcneill }
264 1.1 jmcneill
265 1.1 jmcneill static void
266 1.1 jmcneill meson_pinctrl_pin_dir(struct meson_pinctrl_softc *sc,
267 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def, int flags)
268 1.1 jmcneill {
269 1.1 jmcneill bus_space_handle_t bsh;
270 1.1 jmcneill uint32_t data;
271 1.1 jmcneill
272 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
273 1.1 jmcneill
274 1.1 jmcneill bsh = meson_pinctrl_gpio_handle(sc, &pin_def->oen);
275 1.1 jmcneill data = bus_space_read_4(sc->sc_bst, bsh, pin_def->oen.reg);
276 1.1 jmcneill if ((flags & GPIO_PIN_INPUT) != 0)
277 1.1 jmcneill data |= pin_def->oen.mask;
278 1.1 jmcneill else
279 1.1 jmcneill data &= ~pin_def->oen.mask;
280 1.1 jmcneill bus_space_write_4(sc->sc_bst, bsh, pin_def->oen.reg, data);
281 1.1 jmcneill }
282 1.1 jmcneill
283 1.1 jmcneill static void
284 1.1 jmcneill meson_pinctrl_pin_ctl(void *priv, int pin, int flags)
285 1.1 jmcneill {
286 1.1 jmcneill struct meson_pinctrl_softc * const sc = priv;
287 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def = &sc->sc_conf->gpios[pin];
288 1.1 jmcneill bus_space_handle_t bsh;
289 1.1 jmcneill uint32_t data;
290 1.1 jmcneill
291 1.1 jmcneill KASSERT(pin < sc->sc_conf->ngpios);
292 1.1 jmcneill
293 1.1 jmcneill mutex_enter(&sc->sc_lock);
294 1.1 jmcneill
295 1.1 jmcneill if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) != 0)
296 1.1 jmcneill meson_pinctrl_pin_dir(sc, pin_def, flags);
297 1.1 jmcneill
298 1.1 jmcneill if ((flags & (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) != 0) {
299 1.1 jmcneill bsh = meson_pinctrl_gpio_handle(sc, &pin_def->pupd);
300 1.1 jmcneill data = bus_space_read_4(sc->sc_bst, bsh, pin_def->pupd.reg);
301 1.1 jmcneill if ((flags & GPIO_PIN_PULLUP) != 0)
302 1.1 jmcneill data |= pin_def->pupd.mask;
303 1.1 jmcneill else
304 1.1 jmcneill data &= ~pin_def->pupd.mask;
305 1.1 jmcneill bus_space_write_4(sc->sc_bst, bsh, pin_def->pupd.reg, data);
306 1.1 jmcneill
307 1.1 jmcneill bsh = meson_pinctrl_gpio_handle(sc, &pin_def->pupden);
308 1.1 jmcneill data = bus_space_read_4(sc->sc_bst, bsh, pin_def->pupden.reg);
309 1.1 jmcneill data |= pin_def->pupden.mask;
310 1.1 jmcneill bus_space_write_4(sc->sc_bst, bsh, pin_def->pupden.reg, data);
311 1.1 jmcneill } else {
312 1.1 jmcneill bsh = meson_pinctrl_gpio_handle(sc, &pin_def->pupden);
313 1.1 jmcneill data = bus_space_read_4(sc->sc_bst, bsh, pin_def->pupden.reg);
314 1.1 jmcneill data &= ~pin_def->pupden.mask;
315 1.1 jmcneill bus_space_write_4(sc->sc_bst, bsh, pin_def->pupden.reg, data);
316 1.1 jmcneill }
317 1.1 jmcneill
318 1.1 jmcneill mutex_exit(&sc->sc_lock);
319 1.1 jmcneill }
320 1.1 jmcneill
321 1.1 jmcneill static const struct meson_pinctrl_gpio *
322 1.1 jmcneill meson_pinctrl_gpio_lookup(struct meson_pinctrl_softc *sc, u_int id)
323 1.1 jmcneill {
324 1.1 jmcneill if (id >= sc->sc_conf->ngpios)
325 1.1 jmcneill return NULL;
326 1.1 jmcneill
327 1.1 jmcneill if (sc->sc_conf->gpios[id].name == NULL)
328 1.1 jmcneill return NULL;
329 1.1 jmcneill
330 1.1 jmcneill return &sc->sc_conf->gpios[id];
331 1.1 jmcneill }
332 1.1 jmcneill
333 1.1 jmcneill static void *
334 1.1 jmcneill meson_pinctrl_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
335 1.1 jmcneill {
336 1.1 jmcneill struct meson_pinctrl_softc * const sc = device_private(dev);
337 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def;
338 1.4 jmcneill const struct meson_pinctrl_group *group;
339 1.1 jmcneill struct meson_pinctrl_gpio_pin *gpin;
340 1.1 jmcneill const u_int *gpio = data;
341 1.4 jmcneill u_int n, bank;
342 1.1 jmcneill
343 1.1 jmcneill if (len != 12)
344 1.1 jmcneill return NULL;
345 1.1 jmcneill
346 1.1 jmcneill const u_int id = be32toh(gpio[1]);
347 1.1 jmcneill const bool actlo = be32toh(gpio[2]) & 1;
348 1.1 jmcneill
349 1.1 jmcneill pin_def = meson_pinctrl_gpio_lookup(sc, id);
350 1.1 jmcneill if (pin_def == NULL)
351 1.1 jmcneill return NULL;
352 1.1 jmcneill
353 1.4 jmcneill /* Disable conflicting groups */
354 1.4 jmcneill for (n = 0; n < sc->sc_conf->ngroups; n++) {
355 1.4 jmcneill group = &sc->sc_conf->groups[n];
356 1.4 jmcneill for (bank = 0; bank < group->nbank; bank++) {
357 1.4 jmcneill if (group->bank[bank] == pin_def->id)
358 1.4 jmcneill meson_pinctrl_set_group(sc, group, false);
359 1.4 jmcneill }
360 1.4 jmcneill }
361 1.4 jmcneill
362 1.1 jmcneill mutex_enter(&sc->sc_lock);
363 1.1 jmcneill meson_pinctrl_pin_dir(sc, pin_def, flags);
364 1.1 jmcneill mutex_exit(&sc->sc_lock);
365 1.1 jmcneill
366 1.1 jmcneill gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
367 1.1 jmcneill gpin->pin_sc = sc;
368 1.1 jmcneill gpin->pin_def = pin_def;
369 1.1 jmcneill gpin->pin_flags = flags;
370 1.1 jmcneill gpin->pin_actlo = actlo;
371 1.1 jmcneill
372 1.1 jmcneill return gpin;
373 1.1 jmcneill }
374 1.1 jmcneill
375 1.1 jmcneill static void
376 1.1 jmcneill meson_pinctrl_gpio_release(device_t dev, void *priv)
377 1.1 jmcneill {
378 1.1 jmcneill struct meson_pinctrl_softc * const sc = device_private(dev);
379 1.1 jmcneill struct meson_pinctrl_gpio_pin *gpin = priv;
380 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def = gpin->pin_def;
381 1.1 jmcneill
382 1.1 jmcneill KASSERT(sc == gpin->pin_sc);
383 1.1 jmcneill
384 1.1 jmcneill mutex_enter(&sc->sc_lock);
385 1.1 jmcneill meson_pinctrl_pin_dir(sc, pin_def, GPIO_PIN_INPUT);
386 1.1 jmcneill mutex_exit(&sc->sc_lock);
387 1.1 jmcneill
388 1.1 jmcneill kmem_free(gpin, sizeof(*gpin));
389 1.1 jmcneill }
390 1.1 jmcneill
391 1.1 jmcneill static int
392 1.1 jmcneill meson_pinctrl_gpio_read(device_t dev, void *priv, bool raw)
393 1.1 jmcneill {
394 1.1 jmcneill struct meson_pinctrl_softc * const sc = device_private(dev);
395 1.1 jmcneill struct meson_pinctrl_gpio_pin *gpin = priv;
396 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def = gpin->pin_def;
397 1.1 jmcneill int val;
398 1.1 jmcneill
399 1.1 jmcneill val = meson_pinctrl_pin_read(sc, pin_def->id);
400 1.1 jmcneill if (!raw && gpin->pin_actlo)
401 1.1 jmcneill val = !val;
402 1.1 jmcneill
403 1.1 jmcneill return val;
404 1.1 jmcneill }
405 1.1 jmcneill
406 1.1 jmcneill static void
407 1.1 jmcneill meson_pinctrl_gpio_write(device_t dev, void *priv, int val, bool raw)
408 1.1 jmcneill {
409 1.1 jmcneill struct meson_pinctrl_softc * const sc = device_private(dev);
410 1.1 jmcneill struct meson_pinctrl_gpio_pin *gpin = priv;
411 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def = gpin->pin_def;
412 1.1 jmcneill
413 1.1 jmcneill if (!raw && gpin->pin_actlo)
414 1.1 jmcneill val = !val;
415 1.1 jmcneill
416 1.1 jmcneill meson_pinctrl_pin_write(sc, pin_def->id, val);
417 1.1 jmcneill }
418 1.1 jmcneill
419 1.1 jmcneill static struct fdtbus_gpio_controller_func meson_pinctrl_gpio_funcs = {
420 1.1 jmcneill .acquire = meson_pinctrl_gpio_acquire,
421 1.1 jmcneill .release = meson_pinctrl_gpio_release,
422 1.1 jmcneill .read = meson_pinctrl_gpio_read,
423 1.1 jmcneill .write = meson_pinctrl_gpio_write,
424 1.1 jmcneill };
425 1.1 jmcneill
426 1.1 jmcneill static int
427 1.1 jmcneill meson_pinctrl_initres(struct meson_pinctrl_softc *sc)
428 1.1 jmcneill {
429 1.1 jmcneill bool gpio_found = false;
430 1.1 jmcneill bus_addr_t addr;
431 1.1 jmcneill bus_size_t size;
432 1.1 jmcneill int child;
433 1.1 jmcneill
434 1.1 jmcneill for (child = OF_child(sc->sc_phandle); child; child = OF_peer(child)) {
435 1.1 jmcneill if (of_hasprop(child, "gpio-controller")) {
436 1.1 jmcneill if (gpio_found)
437 1.1 jmcneill continue;
438 1.1 jmcneill gpio_found = true;
439 1.1 jmcneill
440 1.1 jmcneill if (fdtbus_get_reg_byname(child, "mux", &addr, &size) != 0 ||
441 1.1 jmcneill bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh_mux) != 0) {
442 1.1 jmcneill aprint_error(": couldn't map mux registers\n");
443 1.1 jmcneill return ENXIO;
444 1.1 jmcneill }
445 1.1 jmcneill if (fdtbus_get_reg_byname(child, "gpio", &addr, &size) != 0 ||
446 1.1 jmcneill bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh_gpio) != 0) {
447 1.1 jmcneill aprint_error(": couldn't map gpio registers\n");
448 1.1 jmcneill return ENXIO;
449 1.1 jmcneill }
450 1.1 jmcneill
451 1.7 ryo /* pull register is optional */
452 1.7 ryo if (fdtbus_get_reg_byname(child, "pull", &addr, &size) == 0) {
453 1.7 ryo if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh_pull) != 0) {
454 1.7 ryo aprint_error(": couldn't map pull registers\n");
455 1.7 ryo return ENXIO;
456 1.7 ryo }
457 1.7 ryo }
458 1.1 jmcneill /* pull-enable register is optional */
459 1.1 jmcneill if (fdtbus_get_reg_byname(child, "pull-enable", &addr, &size) == 0) {
460 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh_pull_enable) != 0) {
461 1.1 jmcneill aprint_error(": couldn't map pull-enable registers\n");
462 1.1 jmcneill return ENXIO;
463 1.1 jmcneill }
464 1.1 jmcneill }
465 1.1 jmcneill
466 1.1 jmcneill sc->sc_phandle_gpio = child;
467 1.1 jmcneill } else if (of_find_firstchild_byname(child, "mux") != -1) {
468 1.1 jmcneill fdtbus_register_pinctrl_config(sc->sc_dev, child, &meson_pinctrl_funcs);
469 1.1 jmcneill }
470 1.1 jmcneill }
471 1.1 jmcneill
472 1.1 jmcneill if (!gpio_found) {
473 1.1 jmcneill aprint_error(": couldn't find gpio controller\n");
474 1.1 jmcneill return ENOENT;
475 1.1 jmcneill }
476 1.1 jmcneill
477 1.1 jmcneill return 0;
478 1.1 jmcneill }
479 1.1 jmcneill
480 1.1 jmcneill static void
481 1.1 jmcneill meson_pinctrl_initgpio(struct meson_pinctrl_softc *sc)
482 1.1 jmcneill {
483 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def;
484 1.1 jmcneill struct gpio_chipset_tag *gp;
485 1.1 jmcneill struct gpiobus_attach_args gba;
486 1.1 jmcneill int child, len, val;
487 1.1 jmcneill u_int pin;
488 1.1 jmcneill
489 1.1 jmcneill fdtbus_register_gpio_controller(sc->sc_dev, sc->sc_phandle_gpio, &meson_pinctrl_gpio_funcs);
490 1.1 jmcneill
491 1.1 jmcneill for (child = OF_child(sc->sc_phandle_gpio); child; child = OF_peer(child)) {
492 1.1 jmcneill if (!of_hasprop(child, "gpio-hog"))
493 1.1 jmcneill continue;
494 1.1 jmcneill
495 1.1 jmcneill const char *line_name = fdtbus_get_string(child, "line-name");
496 1.1 jmcneill if (line_name == NULL)
497 1.1 jmcneill line_name = fdtbus_get_string(child, "name");
498 1.1 jmcneill
499 1.1 jmcneill const bool input = of_hasprop(child, "input");
500 1.1 jmcneill const bool output_low = of_hasprop(child, "output-low");
501 1.1 jmcneill const bool output_high = of_hasprop(child, "output-high");
502 1.1 jmcneill
503 1.1 jmcneill if (!input && !output_low && !output_high) {
504 1.1 jmcneill aprint_error_dev(sc->sc_dev, "no configuration for line %s\n", line_name);
505 1.1 jmcneill continue;
506 1.1 jmcneill }
507 1.1 jmcneill
508 1.1 jmcneill const u_int *gpio = fdtbus_get_prop(child, "gpios", &len);
509 1.1 jmcneill while (len >= 8) {
510 1.1 jmcneill const u_int id = be32toh(gpio[0]);
511 1.1 jmcneill const bool actlo = be32toh(gpio[1]) & 1;
512 1.1 jmcneill
513 1.1 jmcneill pin_def = meson_pinctrl_gpio_lookup(sc, id);
514 1.1 jmcneill if (pin_def != NULL) {
515 1.1 jmcneill if (input) {
516 1.1 jmcneill device_printf(sc->sc_dev, "%s %s set to input\n",
517 1.1 jmcneill line_name, pin_def->name);
518 1.1 jmcneill meson_pinctrl_pin_ctl(sc, pin_def->id, GPIO_PIN_INPUT);
519 1.1 jmcneill } else {
520 1.1 jmcneill val = output_high;
521 1.1 jmcneill if (actlo)
522 1.1 jmcneill val = !val;
523 1.1 jmcneill device_printf(sc->sc_dev, "%s %s set to output (%s)\n",
524 1.1 jmcneill line_name, pin_def->name, val ? "high" : "low");
525 1.1 jmcneill meson_pinctrl_pin_write(sc, pin_def->id, val);
526 1.1 jmcneill meson_pinctrl_pin_ctl(sc, pin_def->id, GPIO_PIN_OUTPUT);
527 1.1 jmcneill }
528 1.1 jmcneill } else {
529 1.1 jmcneill aprint_error_dev(sc->sc_dev, "%s: unsupported pin %d\n", line_name, id);
530 1.1 jmcneill }
531 1.1 jmcneill
532 1.1 jmcneill len -= 8;
533 1.1 jmcneill gpio += 8;
534 1.1 jmcneill }
535 1.1 jmcneill }
536 1.1 jmcneill
537 1.1 jmcneill const u_int npins = sc->sc_conf->ngpios;
538 1.1 jmcneill sc->sc_pins = kmem_zalloc(sizeof(*sc->sc_pins) * npins, KM_SLEEP);
539 1.1 jmcneill for (pin = 0; pin < npins; pin++) {
540 1.1 jmcneill pin_def = &sc->sc_conf->gpios[pin];
541 1.1 jmcneill sc->sc_pins[pin].pin_num = pin;
542 1.1 jmcneill if (pin_def->name == NULL)
543 1.1 jmcneill continue;
544 1.1 jmcneill sc->sc_pins[pin].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
545 1.1 jmcneill GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN;
546 1.1 jmcneill sc->sc_pins[pin].pin_state = meson_pinctrl_pin_read(sc, pin);
547 1.1 jmcneill strlcpy(sc->sc_pins[pin].pin_defname, pin_def->name,
548 1.1 jmcneill sizeof(sc->sc_pins[pin].pin_defname));
549 1.1 jmcneill }
550 1.1 jmcneill
551 1.1 jmcneill gp = &sc->sc_gp;
552 1.1 jmcneill gp->gp_cookie = sc;
553 1.1 jmcneill gp->gp_pin_read = meson_pinctrl_pin_read;
554 1.1 jmcneill gp->gp_pin_write = meson_pinctrl_pin_write;
555 1.1 jmcneill gp->gp_pin_ctl = meson_pinctrl_pin_ctl;
556 1.1 jmcneill
557 1.1 jmcneill memset(&gba, 0, sizeof(gba));
558 1.1 jmcneill gba.gba_gc = gp;
559 1.1 jmcneill gba.gba_pins = sc->sc_pins;
560 1.1 jmcneill gba.gba_npins = npins;
561 1.1 jmcneill config_found_ia(sc->sc_dev, "gpiobus", &gba, NULL);
562 1.1 jmcneill }
563 1.1 jmcneill
564 1.1 jmcneill static int
565 1.1 jmcneill meson_pinctrl_match(device_t parent, cfdata_t cf, void *aux)
566 1.1 jmcneill {
567 1.1 jmcneill struct fdt_attach_args * const faa = aux;
568 1.1 jmcneill
569 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
570 1.1 jmcneill }
571 1.1 jmcneill
572 1.1 jmcneill static void
573 1.1 jmcneill meson_pinctrl_attach(device_t parent, device_t self, void *aux)
574 1.1 jmcneill {
575 1.1 jmcneill struct meson_pinctrl_softc * const sc = device_private(self);
576 1.1 jmcneill struct fdt_attach_args * const faa = aux;
577 1.1 jmcneill
578 1.1 jmcneill sc->sc_dev = self;
579 1.1 jmcneill sc->sc_phandle = faa->faa_phandle;
580 1.1 jmcneill sc->sc_bst = faa->faa_bst;
581 1.1 jmcneill sc->sc_conf = (void *)of_search_compatible(sc->sc_phandle, compat_data)->data;
582 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
583 1.1 jmcneill
584 1.1 jmcneill if (meson_pinctrl_initres(sc) != 0)
585 1.1 jmcneill return;
586 1.1 jmcneill
587 1.1 jmcneill aprint_naive("\n");
588 1.1 jmcneill aprint_normal(": %s\n", sc->sc_conf->name);
589 1.1 jmcneill
590 1.1 jmcneill meson_pinctrl_initgpio(sc);
591 1.1 jmcneill }
592 1.1 jmcneill
593 1.1 jmcneill CFATTACH_DECL_NEW(meson_pinctrl, sizeof(struct meson_pinctrl_softc),
594 1.1 jmcneill meson_pinctrl_match, meson_pinctrl_attach, NULL, NULL);
595