meson_pinctrl.c revision 1.3 1 1.3 jmcneill /* $NetBSD: meson_pinctrl.c,v 1.3 2019/02/25 19:30:17 jmcneill 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.3 jmcneill __KERNEL_RCSID(0, "$NetBSD: meson_pinctrl.c,v 1.3 2019/02/25 19:30:17 jmcneill 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.1 jmcneill { NULL, 0 }
84 1.1 jmcneill };
85 1.1 jmcneill
86 1.1 jmcneill #define MUX_READ(sc, reg) \
87 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh_mux, (reg))
88 1.1 jmcneill #define MUX_WRITE(sc, reg, val) \
89 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh_mux, (reg), (val))
90 1.1 jmcneill
91 1.1 jmcneill static const struct meson_pinctrl_group *
92 1.1 jmcneill meson_pinctrl_find_group(struct meson_pinctrl_softc *sc,
93 1.1 jmcneill const char *name)
94 1.1 jmcneill {
95 1.1 jmcneill const struct meson_pinctrl_group *group;
96 1.1 jmcneill u_int n;
97 1.1 jmcneill
98 1.1 jmcneill for (n = 0; n < sc->sc_conf->ngroups; n++) {
99 1.1 jmcneill group = &sc->sc_conf->groups[n];
100 1.1 jmcneill if (strcmp(group->name, name) == 0)
101 1.1 jmcneill return group;
102 1.1 jmcneill }
103 1.1 jmcneill
104 1.1 jmcneill return NULL;
105 1.1 jmcneill }
106 1.1 jmcneill
107 1.1 jmcneill static bool
108 1.1 jmcneill meson_pinctrl_group_in_bank(struct meson_pinctrl_softc *sc,
109 1.1 jmcneill const struct meson_pinctrl_group *group, u_int bankno)
110 1.1 jmcneill {
111 1.1 jmcneill u_int n;
112 1.1 jmcneill
113 1.1 jmcneill for (n = 0; n < group->nbank; n++) {
114 1.1 jmcneill if (group->bank[n] == bankno)
115 1.1 jmcneill return true;
116 1.1 jmcneill }
117 1.1 jmcneill
118 1.1 jmcneill return false;
119 1.1 jmcneill }
120 1.1 jmcneill
121 1.1 jmcneill static void
122 1.1 jmcneill meson_pinctrl_set_group(struct meson_pinctrl_softc *sc,
123 1.1 jmcneill const struct meson_pinctrl_group *group, bool enable)
124 1.1 jmcneill {
125 1.1 jmcneill uint32_t val;
126 1.1 jmcneill
127 1.1 jmcneill val = MUX_READ(sc, group->reg);
128 1.1 jmcneill if (enable)
129 1.1 jmcneill val |= __BIT(group->bit);
130 1.1 jmcneill else
131 1.1 jmcneill val &= ~__BIT(group->bit);
132 1.1 jmcneill MUX_WRITE(sc, group->reg, val);
133 1.1 jmcneill }
134 1.1 jmcneill
135 1.1 jmcneill static void
136 1.1 jmcneill meson_pinctrl_setfunc(struct meson_pinctrl_softc *sc, const char *name)
137 1.1 jmcneill {
138 1.1 jmcneill const struct meson_pinctrl_group *group, *target_group;
139 1.1 jmcneill u_int n, bank;
140 1.1 jmcneill
141 1.1 jmcneill target_group = meson_pinctrl_find_group(sc, name);
142 1.1 jmcneill if (target_group == NULL) {
143 1.1 jmcneill aprint_error_dev(sc->sc_dev, "function '%s' not supported\n", name);
144 1.1 jmcneill return;
145 1.1 jmcneill }
146 1.1 jmcneill
147 1.1 jmcneill /* Disable conflicting groups */
148 1.1 jmcneill for (n = 0; n < sc->sc_conf->ngroups; n++) {
149 1.1 jmcneill group = &sc->sc_conf->groups[n];
150 1.1 jmcneill if (target_group == group)
151 1.1 jmcneill continue;
152 1.1 jmcneill for (bank = 0; bank < target_group->nbank; bank++) {
153 1.1 jmcneill if (meson_pinctrl_group_in_bank(sc, group, target_group->bank[bank]))
154 1.1 jmcneill meson_pinctrl_set_group(sc, group, false);
155 1.1 jmcneill }
156 1.1 jmcneill }
157 1.1 jmcneill
158 1.1 jmcneill /* Enable target group */
159 1.1 jmcneill meson_pinctrl_set_group(sc, target_group, true);
160 1.1 jmcneill }
161 1.1 jmcneill
162 1.1 jmcneill static int
163 1.1 jmcneill meson_pinctrl_set_config(device_t dev, const void *data, size_t len)
164 1.1 jmcneill {
165 1.1 jmcneill struct meson_pinctrl_softc * const sc = device_private(dev);
166 1.1 jmcneill const char *groups;
167 1.1 jmcneill int groups_len;
168 1.1 jmcneill
169 1.1 jmcneill if (len != 4)
170 1.1 jmcneill return -1;
171 1.1 jmcneill
172 1.1 jmcneill const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
173 1.1 jmcneill const int mux = of_find_firstchild_byname(phandle, "mux");
174 1.1 jmcneill if (mux == -1)
175 1.1 jmcneill return -1;
176 1.1 jmcneill
177 1.2 thorpej groups = fdtbus_pinctrl_parse_groups(mux, &groups_len);
178 1.2 thorpej if (groups == NULL)
179 1.1 jmcneill return -1;
180 1.1 jmcneill
181 1.1 jmcneill for (; groups_len > 0;
182 1.1 jmcneill groups_len -= strlen(groups) + 1, groups += strlen(groups) + 1) {
183 1.1 jmcneill meson_pinctrl_setfunc(sc, groups);
184 1.1 jmcneill }
185 1.1 jmcneill
186 1.1 jmcneill return 0;
187 1.1 jmcneill }
188 1.1 jmcneill
189 1.1 jmcneill static struct fdtbus_pinctrl_controller_func meson_pinctrl_funcs = {
190 1.1 jmcneill .set_config = meson_pinctrl_set_config,
191 1.1 jmcneill };
192 1.1 jmcneill
193 1.1 jmcneill static bus_space_handle_t
194 1.1 jmcneill meson_pinctrl_gpio_handle(struct meson_pinctrl_softc *sc,
195 1.1 jmcneill const struct meson_pinctrl_gpioreg *gpioreg)
196 1.1 jmcneill {
197 1.1 jmcneill switch (gpioreg->type) {
198 1.1 jmcneill case MESON_PINCTRL_REGTYPE_PULL:
199 1.1 jmcneill return sc->sc_bsh_pull;
200 1.1 jmcneill case MESON_PINCTRL_REGTYPE_PULL_ENABLE:
201 1.1 jmcneill return sc->sc_bsh_pull_enable;
202 1.1 jmcneill case MESON_PINCTRL_REGTYPE_GPIO:
203 1.1 jmcneill return sc->sc_bsh_gpio;
204 1.1 jmcneill default:
205 1.1 jmcneill panic("unsupported GPIO regtype %d", gpioreg->type);
206 1.1 jmcneill }
207 1.1 jmcneill }
208 1.1 jmcneill
209 1.1 jmcneill static int
210 1.1 jmcneill meson_pinctrl_pin_read(void *priv, int pin)
211 1.1 jmcneill {
212 1.1 jmcneill struct meson_pinctrl_softc * const sc = priv;
213 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def = &sc->sc_conf->gpios[pin];
214 1.1 jmcneill const struct meson_pinctrl_gpioreg *gpio_reg = &pin_def->in;
215 1.1 jmcneill bus_space_handle_t bsh;
216 1.1 jmcneill uint32_t data;
217 1.1 jmcneill int val;
218 1.1 jmcneill
219 1.1 jmcneill KASSERT(pin < sc->sc_conf->ngpios);
220 1.1 jmcneill
221 1.1 jmcneill bsh = meson_pinctrl_gpio_handle(sc, gpio_reg);
222 1.1 jmcneill data = bus_space_read_4(sc->sc_bst, bsh, gpio_reg->reg);
223 1.1 jmcneill val = __SHIFTOUT(data, gpio_reg->mask);
224 1.1 jmcneill
225 1.1 jmcneill return val;
226 1.1 jmcneill }
227 1.1 jmcneill
228 1.1 jmcneill static void
229 1.1 jmcneill meson_pinctrl_pin_write(void *priv, int pin, int val)
230 1.1 jmcneill {
231 1.1 jmcneill struct meson_pinctrl_softc * const sc = priv;
232 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def = &sc->sc_conf->gpios[pin];
233 1.1 jmcneill const struct meson_pinctrl_gpioreg *gpio_reg = &pin_def->out;
234 1.1 jmcneill bus_space_handle_t bsh;
235 1.1 jmcneill uint32_t data;
236 1.1 jmcneill
237 1.1 jmcneill KASSERT(pin < sc->sc_conf->ngpios);
238 1.1 jmcneill
239 1.1 jmcneill bsh = meson_pinctrl_gpio_handle(sc, gpio_reg);
240 1.1 jmcneill
241 1.1 jmcneill mutex_enter(&sc->sc_lock);
242 1.1 jmcneill data = bus_space_read_4(sc->sc_bst, bsh, gpio_reg->reg);
243 1.1 jmcneill if (val)
244 1.1 jmcneill data |= gpio_reg->mask;
245 1.1 jmcneill else
246 1.1 jmcneill data &= ~gpio_reg->mask;
247 1.1 jmcneill bus_space_write_4(sc->sc_bst, bsh, gpio_reg->reg, data);
248 1.1 jmcneill mutex_exit(&sc->sc_lock);
249 1.1 jmcneill }
250 1.1 jmcneill
251 1.1 jmcneill static void
252 1.1 jmcneill meson_pinctrl_pin_dir(struct meson_pinctrl_softc *sc,
253 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def, int flags)
254 1.1 jmcneill {
255 1.1 jmcneill bus_space_handle_t bsh;
256 1.1 jmcneill uint32_t data;
257 1.1 jmcneill
258 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
259 1.1 jmcneill
260 1.1 jmcneill bsh = meson_pinctrl_gpio_handle(sc, &pin_def->oen);
261 1.1 jmcneill data = bus_space_read_4(sc->sc_bst, bsh, pin_def->oen.reg);
262 1.1 jmcneill if ((flags & GPIO_PIN_INPUT) != 0)
263 1.1 jmcneill data |= pin_def->oen.mask;
264 1.1 jmcneill else
265 1.1 jmcneill data &= ~pin_def->oen.mask;
266 1.1 jmcneill bus_space_write_4(sc->sc_bst, bsh, pin_def->oen.reg, data);
267 1.1 jmcneill }
268 1.1 jmcneill
269 1.1 jmcneill static void
270 1.1 jmcneill meson_pinctrl_pin_ctl(void *priv, int pin, int flags)
271 1.1 jmcneill {
272 1.1 jmcneill struct meson_pinctrl_softc * const sc = priv;
273 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def = &sc->sc_conf->gpios[pin];
274 1.1 jmcneill bus_space_handle_t bsh;
275 1.1 jmcneill uint32_t data;
276 1.1 jmcneill
277 1.1 jmcneill KASSERT(pin < sc->sc_conf->ngpios);
278 1.1 jmcneill
279 1.1 jmcneill mutex_enter(&sc->sc_lock);
280 1.1 jmcneill
281 1.1 jmcneill if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) != 0)
282 1.1 jmcneill meson_pinctrl_pin_dir(sc, pin_def, flags);
283 1.1 jmcneill
284 1.1 jmcneill if ((flags & (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) != 0) {
285 1.1 jmcneill bsh = meson_pinctrl_gpio_handle(sc, &pin_def->pupd);
286 1.1 jmcneill data = bus_space_read_4(sc->sc_bst, bsh, pin_def->pupd.reg);
287 1.1 jmcneill if ((flags & GPIO_PIN_PULLUP) != 0)
288 1.1 jmcneill data |= pin_def->pupd.mask;
289 1.1 jmcneill else
290 1.1 jmcneill data &= ~pin_def->pupd.mask;
291 1.1 jmcneill bus_space_write_4(sc->sc_bst, bsh, pin_def->pupd.reg, data);
292 1.1 jmcneill
293 1.1 jmcneill bsh = meson_pinctrl_gpio_handle(sc, &pin_def->pupden);
294 1.1 jmcneill data = bus_space_read_4(sc->sc_bst, bsh, pin_def->pupden.reg);
295 1.1 jmcneill data |= pin_def->pupden.mask;
296 1.1 jmcneill bus_space_write_4(sc->sc_bst, bsh, pin_def->pupden.reg, data);
297 1.1 jmcneill } else {
298 1.1 jmcneill bsh = meson_pinctrl_gpio_handle(sc, &pin_def->pupden);
299 1.1 jmcneill data = bus_space_read_4(sc->sc_bst, bsh, pin_def->pupden.reg);
300 1.1 jmcneill data &= ~pin_def->pupden.mask;
301 1.1 jmcneill bus_space_write_4(sc->sc_bst, bsh, pin_def->pupden.reg, data);
302 1.1 jmcneill }
303 1.1 jmcneill
304 1.1 jmcneill mutex_exit(&sc->sc_lock);
305 1.1 jmcneill }
306 1.1 jmcneill
307 1.1 jmcneill static const struct meson_pinctrl_gpio *
308 1.1 jmcneill meson_pinctrl_gpio_lookup(struct meson_pinctrl_softc *sc, u_int id)
309 1.1 jmcneill {
310 1.1 jmcneill if (id >= sc->sc_conf->ngpios)
311 1.1 jmcneill return NULL;
312 1.1 jmcneill
313 1.1 jmcneill if (sc->sc_conf->gpios[id].name == NULL)
314 1.1 jmcneill return NULL;
315 1.1 jmcneill
316 1.1 jmcneill return &sc->sc_conf->gpios[id];
317 1.1 jmcneill }
318 1.1 jmcneill
319 1.1 jmcneill static void *
320 1.1 jmcneill meson_pinctrl_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
321 1.1 jmcneill {
322 1.1 jmcneill struct meson_pinctrl_softc * const sc = device_private(dev);
323 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def;
324 1.1 jmcneill struct meson_pinctrl_gpio_pin *gpin;
325 1.1 jmcneill const u_int *gpio = data;
326 1.1 jmcneill
327 1.1 jmcneill if (len != 12)
328 1.1 jmcneill return NULL;
329 1.1 jmcneill
330 1.1 jmcneill const u_int id = be32toh(gpio[1]);
331 1.1 jmcneill const bool actlo = be32toh(gpio[2]) & 1;
332 1.1 jmcneill
333 1.1 jmcneill pin_def = meson_pinctrl_gpio_lookup(sc, id);
334 1.1 jmcneill if (pin_def == NULL)
335 1.1 jmcneill return NULL;
336 1.1 jmcneill
337 1.1 jmcneill mutex_enter(&sc->sc_lock);
338 1.1 jmcneill meson_pinctrl_pin_dir(sc, pin_def, flags);
339 1.1 jmcneill mutex_exit(&sc->sc_lock);
340 1.1 jmcneill
341 1.1 jmcneill gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
342 1.1 jmcneill gpin->pin_sc = sc;
343 1.1 jmcneill gpin->pin_def = pin_def;
344 1.1 jmcneill gpin->pin_flags = flags;
345 1.1 jmcneill gpin->pin_actlo = actlo;
346 1.1 jmcneill
347 1.1 jmcneill return gpin;
348 1.1 jmcneill }
349 1.1 jmcneill
350 1.1 jmcneill static void
351 1.1 jmcneill meson_pinctrl_gpio_release(device_t dev, void *priv)
352 1.1 jmcneill {
353 1.1 jmcneill struct meson_pinctrl_softc * const sc = device_private(dev);
354 1.1 jmcneill struct meson_pinctrl_gpio_pin *gpin = priv;
355 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def = gpin->pin_def;
356 1.1 jmcneill
357 1.1 jmcneill KASSERT(sc == gpin->pin_sc);
358 1.1 jmcneill
359 1.1 jmcneill mutex_enter(&sc->sc_lock);
360 1.1 jmcneill meson_pinctrl_pin_dir(sc, pin_def, GPIO_PIN_INPUT);
361 1.1 jmcneill mutex_exit(&sc->sc_lock);
362 1.1 jmcneill
363 1.1 jmcneill kmem_free(gpin, sizeof(*gpin));
364 1.1 jmcneill }
365 1.1 jmcneill
366 1.1 jmcneill static int
367 1.1 jmcneill meson_pinctrl_gpio_read(device_t dev, void *priv, bool raw)
368 1.1 jmcneill {
369 1.1 jmcneill struct meson_pinctrl_softc * const sc = device_private(dev);
370 1.1 jmcneill struct meson_pinctrl_gpio_pin *gpin = priv;
371 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def = gpin->pin_def;
372 1.1 jmcneill int val;
373 1.1 jmcneill
374 1.1 jmcneill val = meson_pinctrl_pin_read(sc, pin_def->id);
375 1.1 jmcneill if (!raw && gpin->pin_actlo)
376 1.1 jmcneill val = !val;
377 1.1 jmcneill
378 1.1 jmcneill return val;
379 1.1 jmcneill }
380 1.1 jmcneill
381 1.1 jmcneill static void
382 1.1 jmcneill meson_pinctrl_gpio_write(device_t dev, void *priv, int val, bool raw)
383 1.1 jmcneill {
384 1.1 jmcneill struct meson_pinctrl_softc * const sc = device_private(dev);
385 1.1 jmcneill struct meson_pinctrl_gpio_pin *gpin = priv;
386 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def = gpin->pin_def;
387 1.1 jmcneill
388 1.1 jmcneill if (!raw && gpin->pin_actlo)
389 1.1 jmcneill val = !val;
390 1.1 jmcneill
391 1.1 jmcneill meson_pinctrl_pin_write(sc, pin_def->id, val);
392 1.1 jmcneill }
393 1.1 jmcneill
394 1.1 jmcneill static struct fdtbus_gpio_controller_func meson_pinctrl_gpio_funcs = {
395 1.1 jmcneill .acquire = meson_pinctrl_gpio_acquire,
396 1.1 jmcneill .release = meson_pinctrl_gpio_release,
397 1.1 jmcneill .read = meson_pinctrl_gpio_read,
398 1.1 jmcneill .write = meson_pinctrl_gpio_write,
399 1.1 jmcneill };
400 1.1 jmcneill
401 1.1 jmcneill static int
402 1.1 jmcneill meson_pinctrl_initres(struct meson_pinctrl_softc *sc)
403 1.1 jmcneill {
404 1.1 jmcneill bool gpio_found = false;
405 1.1 jmcneill bus_addr_t addr;
406 1.1 jmcneill bus_size_t size;
407 1.1 jmcneill int child;
408 1.1 jmcneill
409 1.1 jmcneill for (child = OF_child(sc->sc_phandle); child; child = OF_peer(child)) {
410 1.1 jmcneill if (of_hasprop(child, "gpio-controller")) {
411 1.1 jmcneill if (gpio_found)
412 1.1 jmcneill continue;
413 1.1 jmcneill gpio_found = true;
414 1.1 jmcneill
415 1.1 jmcneill if (fdtbus_get_reg_byname(child, "mux", &addr, &size) != 0 ||
416 1.1 jmcneill bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh_mux) != 0) {
417 1.1 jmcneill aprint_error(": couldn't map mux registers\n");
418 1.1 jmcneill return ENXIO;
419 1.1 jmcneill }
420 1.1 jmcneill if (fdtbus_get_reg_byname(child, "pull", &addr, &size) != 0 ||
421 1.1 jmcneill bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh_pull) != 0) {
422 1.1 jmcneill aprint_error(": couldn't map pull registers\n");
423 1.1 jmcneill return ENXIO;
424 1.1 jmcneill }
425 1.1 jmcneill if (fdtbus_get_reg_byname(child, "gpio", &addr, &size) != 0 ||
426 1.1 jmcneill bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh_gpio) != 0) {
427 1.1 jmcneill aprint_error(": couldn't map gpio registers\n");
428 1.1 jmcneill return ENXIO;
429 1.1 jmcneill }
430 1.1 jmcneill
431 1.1 jmcneill /* pull-enable register is optional */
432 1.1 jmcneill if (fdtbus_get_reg_byname(child, "pull-enable", &addr, &size) == 0) {
433 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh_pull_enable) != 0) {
434 1.1 jmcneill aprint_error(": couldn't map pull-enable registers\n");
435 1.1 jmcneill return ENXIO;
436 1.1 jmcneill }
437 1.1 jmcneill }
438 1.1 jmcneill
439 1.1 jmcneill sc->sc_phandle_gpio = child;
440 1.1 jmcneill } else if (of_find_firstchild_byname(child, "mux") != -1) {
441 1.1 jmcneill fdtbus_register_pinctrl_config(sc->sc_dev, child, &meson_pinctrl_funcs);
442 1.1 jmcneill }
443 1.1 jmcneill }
444 1.1 jmcneill
445 1.1 jmcneill if (!gpio_found) {
446 1.1 jmcneill aprint_error(": couldn't find gpio controller\n");
447 1.1 jmcneill return ENOENT;
448 1.1 jmcneill }
449 1.1 jmcneill
450 1.1 jmcneill return 0;
451 1.1 jmcneill }
452 1.1 jmcneill
453 1.1 jmcneill static void
454 1.1 jmcneill meson_pinctrl_initgpio(struct meson_pinctrl_softc *sc)
455 1.1 jmcneill {
456 1.1 jmcneill const struct meson_pinctrl_gpio *pin_def;
457 1.1 jmcneill struct gpio_chipset_tag *gp;
458 1.1 jmcneill struct gpiobus_attach_args gba;
459 1.1 jmcneill int child, len, val;
460 1.1 jmcneill u_int pin;
461 1.1 jmcneill
462 1.1 jmcneill fdtbus_register_gpio_controller(sc->sc_dev, sc->sc_phandle_gpio, &meson_pinctrl_gpio_funcs);
463 1.1 jmcneill
464 1.1 jmcneill for (child = OF_child(sc->sc_phandle_gpio); child; child = OF_peer(child)) {
465 1.1 jmcneill if (!of_hasprop(child, "gpio-hog"))
466 1.1 jmcneill continue;
467 1.1 jmcneill
468 1.1 jmcneill const char *line_name = fdtbus_get_string(child, "line-name");
469 1.1 jmcneill if (line_name == NULL)
470 1.1 jmcneill line_name = fdtbus_get_string(child, "name");
471 1.1 jmcneill
472 1.1 jmcneill const bool input = of_hasprop(child, "input");
473 1.1 jmcneill const bool output_low = of_hasprop(child, "output-low");
474 1.1 jmcneill const bool output_high = of_hasprop(child, "output-high");
475 1.1 jmcneill
476 1.1 jmcneill if (!input && !output_low && !output_high) {
477 1.1 jmcneill aprint_error_dev(sc->sc_dev, "no configuration for line %s\n", line_name);
478 1.1 jmcneill continue;
479 1.1 jmcneill }
480 1.1 jmcneill
481 1.1 jmcneill const u_int *gpio = fdtbus_get_prop(child, "gpios", &len);
482 1.1 jmcneill while (len >= 8) {
483 1.1 jmcneill const u_int id = be32toh(gpio[0]);
484 1.1 jmcneill const bool actlo = be32toh(gpio[1]) & 1;
485 1.1 jmcneill
486 1.1 jmcneill pin_def = meson_pinctrl_gpio_lookup(sc, id);
487 1.1 jmcneill if (pin_def != NULL) {
488 1.1 jmcneill if (input) {
489 1.1 jmcneill device_printf(sc->sc_dev, "%s %s set to input\n",
490 1.1 jmcneill line_name, pin_def->name);
491 1.1 jmcneill meson_pinctrl_pin_ctl(sc, pin_def->id, GPIO_PIN_INPUT);
492 1.1 jmcneill } else {
493 1.1 jmcneill val = output_high;
494 1.1 jmcneill if (actlo)
495 1.1 jmcneill val = !val;
496 1.1 jmcneill device_printf(sc->sc_dev, "%s %s set to output (%s)\n",
497 1.1 jmcneill line_name, pin_def->name, val ? "high" : "low");
498 1.1 jmcneill meson_pinctrl_pin_write(sc, pin_def->id, val);
499 1.1 jmcneill meson_pinctrl_pin_ctl(sc, pin_def->id, GPIO_PIN_OUTPUT);
500 1.1 jmcneill }
501 1.1 jmcneill } else {
502 1.1 jmcneill aprint_error_dev(sc->sc_dev, "%s: unsupported pin %d\n", line_name, id);
503 1.1 jmcneill }
504 1.1 jmcneill
505 1.1 jmcneill len -= 8;
506 1.1 jmcneill gpio += 8;
507 1.1 jmcneill }
508 1.1 jmcneill }
509 1.1 jmcneill
510 1.1 jmcneill const u_int npins = sc->sc_conf->ngpios;
511 1.1 jmcneill sc->sc_pins = kmem_zalloc(sizeof(*sc->sc_pins) * npins, KM_SLEEP);
512 1.1 jmcneill for (pin = 0; pin < npins; pin++) {
513 1.1 jmcneill pin_def = &sc->sc_conf->gpios[pin];
514 1.1 jmcneill sc->sc_pins[pin].pin_num = pin;
515 1.1 jmcneill if (pin_def->name == NULL)
516 1.1 jmcneill continue;
517 1.1 jmcneill sc->sc_pins[pin].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
518 1.1 jmcneill GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN;
519 1.1 jmcneill sc->sc_pins[pin].pin_state = meson_pinctrl_pin_read(sc, pin);
520 1.1 jmcneill strlcpy(sc->sc_pins[pin].pin_defname, pin_def->name,
521 1.1 jmcneill sizeof(sc->sc_pins[pin].pin_defname));
522 1.1 jmcneill }
523 1.1 jmcneill
524 1.1 jmcneill gp = &sc->sc_gp;
525 1.1 jmcneill gp->gp_cookie = sc;
526 1.1 jmcneill gp->gp_pin_read = meson_pinctrl_pin_read;
527 1.1 jmcneill gp->gp_pin_write = meson_pinctrl_pin_write;
528 1.1 jmcneill gp->gp_pin_ctl = meson_pinctrl_pin_ctl;
529 1.1 jmcneill
530 1.1 jmcneill memset(&gba, 0, sizeof(gba));
531 1.1 jmcneill gba.gba_gc = gp;
532 1.1 jmcneill gba.gba_pins = sc->sc_pins;
533 1.1 jmcneill gba.gba_npins = npins;
534 1.1 jmcneill config_found_ia(sc->sc_dev, "gpiobus", &gba, NULL);
535 1.1 jmcneill }
536 1.1 jmcneill
537 1.1 jmcneill static int
538 1.1 jmcneill meson_pinctrl_match(device_t parent, cfdata_t cf, void *aux)
539 1.1 jmcneill {
540 1.1 jmcneill struct fdt_attach_args * const faa = aux;
541 1.1 jmcneill
542 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
543 1.1 jmcneill }
544 1.1 jmcneill
545 1.1 jmcneill static void
546 1.1 jmcneill meson_pinctrl_attach(device_t parent, device_t self, void *aux)
547 1.1 jmcneill {
548 1.1 jmcneill struct meson_pinctrl_softc * const sc = device_private(self);
549 1.1 jmcneill struct fdt_attach_args * const faa = aux;
550 1.1 jmcneill
551 1.1 jmcneill sc->sc_dev = self;
552 1.1 jmcneill sc->sc_phandle = faa->faa_phandle;
553 1.1 jmcneill sc->sc_bst = faa->faa_bst;
554 1.1 jmcneill sc->sc_conf = (void *)of_search_compatible(sc->sc_phandle, compat_data)->data;
555 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
556 1.1 jmcneill
557 1.1 jmcneill if (meson_pinctrl_initres(sc) != 0)
558 1.1 jmcneill return;
559 1.1 jmcneill
560 1.1 jmcneill aprint_naive("\n");
561 1.1 jmcneill aprint_normal(": %s\n", sc->sc_conf->name);
562 1.1 jmcneill
563 1.1 jmcneill fdtbus_pinctrl_configure();
564 1.1 jmcneill
565 1.1 jmcneill meson_pinctrl_initgpio(sc);
566 1.1 jmcneill }
567 1.1 jmcneill
568 1.1 jmcneill CFATTACH_DECL_NEW(meson_pinctrl, sizeof(struct meson_pinctrl_softc),
569 1.1 jmcneill meson_pinctrl_match, meson_pinctrl_attach, NULL, NULL);
570