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