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