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