pinctrl_single.c revision 1.2
1/* $NetBSD: pinctrl_single.c,v 1.2 2021/01/18 02:35:49 thorpej Exp $ */ 2 3/*- 4 * Copyright (c) 2019 Jared McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__KERNEL_RCSID(0, "$NetBSD: pinctrl_single.c,v 1.2 2021/01/18 02:35:49 thorpej Exp $"); 31 32#include <sys/param.h> 33#include <sys/bus.h> 34#include <sys/device.h> 35#include <sys/systm.h> 36#include <sys/gpio.h> 37 38#include <dev/fdt/fdtvar.h> 39 40#define PINCTRL_FLAG_PINCONF __BIT(0) /* supports generic pinconf */ 41 42struct pinctrl_single_config { 43 uint32_t flags; 44}; 45 46static const struct pinctrl_single_config pinctrl_config = { 47}; 48 49static const struct pinctrl_single_config pinconf_config = { 50 .flags = PINCTRL_FLAG_PINCONF 51}; 52 53static const struct device_compatible_entry compat_data[] = { 54 { .compat = "pinctrl-single", .data = &pinctrl_config }, 55 { .compat = "pinconf-single", .data = &pinconf_config }, 56 57 { 0 } 58}; 59 60struct pinctrl_single_softc { 61 device_t sc_dev; 62 int sc_phandle; 63 bus_space_tag_t sc_bst; 64 bus_space_handle_t sc_bsh; 65 uint32_t sc_flags; 66 u_int sc_regwidth; 67 u_int sc_funcmask; 68}; 69 70static void 71pinctrl_single_pins_write(struct pinctrl_single_softc *sc, u_int off, u_int val) 72{ 73 union { 74 uint32_t reg32; 75 uint16_t reg16; 76 uint8_t reg8; 77 } u; 78 79 aprint_debug_dev(sc->sc_dev, "writing %#x with %#x\n", off, val); 80 81 switch (sc->sc_regwidth) { 82 case 8: 83 u.reg8 = bus_space_read_1(sc->sc_bst, sc->sc_bsh, off); 84 u.reg8 &= ~sc->sc_funcmask; 85 u.reg8 |= val; 86 bus_space_write_1(sc->sc_bst, sc->sc_bsh, off, u.reg8); 87 break; 88 case 16: 89 u.reg16 = bus_space_read_2(sc->sc_bst, sc->sc_bsh, off); 90 u.reg16 &= ~sc->sc_funcmask; 91 u.reg16 |= val; 92 bus_space_write_2(sc->sc_bst, sc->sc_bsh, off, u.reg16); 93 break; 94 case 32: 95 u.reg32 = bus_space_read_4(sc->sc_bst, sc->sc_bsh, off); 96 u.reg32 &= ~sc->sc_funcmask; 97 u.reg32 |= val; 98 bus_space_write_4(sc->sc_bst, sc->sc_bsh, off, u.reg32); 99 break; 100 default: 101 device_printf(sc->sc_dev, "%s: unsupported reg width %d\n", 102 __func__, sc->sc_regwidth); 103 break; 104 } 105} 106 107static int 108pinctrl_single_pins_set_config(device_t dev, const void *data, size_t len) 109{ 110 struct pinctrl_single_softc * const sc = device_private(dev); 111 const u_int *pins; 112 int pinslen; 113 114 if (len != 4) 115 return -1; 116 117 const int phandle = fdtbus_get_phandle_from_native(be32dec(data)); 118 119 pins = fdtbus_get_prop(phandle, "pinctrl-single,pins", &pinslen); 120 if (pins == NULL) 121 return -1; 122 123 while (pinslen >= 8) { 124 const int off = be32toh(pins[0]); 125 const int val = be32toh(pins[1]); 126 127 pinctrl_single_pins_write(sc, off, val); 128 pins += 2; 129 pinslen -= 8; 130 } 131 132 return 0; 133} 134 135static struct fdtbus_pinctrl_controller_func pinctrl_single_pins_funcs = { 136 .set_config = pinctrl_single_pins_set_config, 137}; 138 139static int 140pinctrl_single_match(device_t parent, cfdata_t cf, void *aux) 141{ 142 struct fdt_attach_args * const faa = aux; 143 144 return of_match_compat_data(faa->faa_phandle, compat_data); 145} 146 147static void 148pinctrl_single_attach(device_t parent, device_t self, void *aux) 149{ 150 struct pinctrl_single_softc * const sc = device_private(self); 151 struct fdt_attach_args * const faa = aux; 152 const int phandle = faa->faa_phandle; 153 const struct pinctrl_single_config *conf; 154 bus_addr_t addr; 155 bus_size_t size; 156 int child; 157 158 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 159 aprint_error(": couldn't get registers\n"); 160 return; 161 } 162 163 conf = of_search_compatible(phandle, compat_data)->data; 164 165 sc->sc_dev = self; 166 sc->sc_phandle = phandle; 167 sc->sc_bst = faa->faa_bst; 168 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 169 aprint_error(": couldn't map registers\n"); 170 return; 171 } 172 sc->sc_flags = conf->flags; 173 174 if (of_getprop_uint32(phandle, "pinctrl-single,register-width", &sc->sc_regwidth) != 0) { 175 aprint_error(": missing 'pinctrl-single,register-width' property\n"); 176 return; 177 } 178 if (of_getprop_uint32(phandle, "pinctrl-single,function-mask", &sc->sc_funcmask) != 0) { 179 aprint_error(": missing 'pinctrl-single,function-mask' property\n"); 180 return; 181 } 182 183 switch (sc->sc_regwidth) { 184 case 8: 185 case 16: 186 case 32: 187 break; 188 default: 189 aprint_error(": register width %d not supported\n", sc->sc_regwidth); 190 return; 191 } 192 193 aprint_naive("\n"); 194 aprint_normal("\n"); 195 196 for (child = OF_child(phandle); child; child = OF_peer(child)) { 197 if (of_hasprop(child, "pinctrl-single,pins")) 198 fdtbus_register_pinctrl_config(self, child, &pinctrl_single_pins_funcs); 199 } 200 201 fdtbus_pinctrl_set_config(phandle, "default"); 202} 203 204CFATTACH_DECL_NEW(pinctrl_single, sizeof(struct pinctrl_single_softc), 205 pinctrl_single_match, pinctrl_single_attach, NULL, NULL); 206