1 /* $NetBSD: i2cmux_fdt.c,v 1.10 2021/01/27 03:10:21 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2020 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: i2cmux_fdt.c,v 1.10 2021/01/27 03:10:21 thorpej Exp $"); 34 35 #include <sys/types.h> 36 #include <sys/device.h> 37 #include <sys/kernel.h> 38 #include <sys/kmem.h> 39 #include <sys/bus.h> 40 #include <sys/gpio.h> 41 42 #include <dev/fdt/fdtvar.h> 43 #include <dev/i2c/i2cmuxvar.h> 44 45 /*****************************************************************************/ 46 47 struct mux_info_gpio { 48 struct fdtbus_gpio_pin **pins; 49 int npins; 50 uint32_t idle_value; 51 bool has_idle_value; 52 }; 53 54 struct bus_info_gpio { 55 bus_addr_t value; 56 }; 57 58 static void * 59 iicmux_gpio_get_mux_info(struct iicmux_softc * const sc) 60 { 61 struct mux_info_gpio *mux_data; 62 int i; 63 64 mux_data = kmem_zalloc(sizeof(*mux_data), KM_SLEEP); 65 66 mux_data->npins = fdtbus_gpio_count(sc->sc_handle, "mux-gpios"); 67 if (mux_data->npins == 0) { 68 aprint_error_dev(sc->sc_dev, 69 "unable to get mux-gpios property\n"); 70 goto bad; 71 } 72 73 mux_data->pins = 74 kmem_zalloc(sizeof(*mux_data->pins) * mux_data->npins, KM_SLEEP); 75 for (i = 0; i < mux_data->npins; i++) { 76 mux_data->pins[i] = fdtbus_gpio_acquire_index(sc->sc_handle, 77 "mux-gpios", i, GPIO_PIN_OUTPUT); 78 if (mux_data->pins[i] == NULL) { 79 aprint_error_dev(sc->sc_dev, 80 "unable to acquire gpio #%d\n", i); 81 goto bad; 82 } 83 } 84 85 mux_data->has_idle_value = 86 of_getprop_uint32(sc->sc_handle, "idle-state", 87 &mux_data->idle_value) == 0; 88 89 return mux_data; 90 91 bad: 92 for (i = 0; i < mux_data->npins; i++) { 93 if (mux_data->pins[i] != NULL) { 94 fdtbus_gpio_release(mux_data->pins[i]); 95 } 96 } 97 kmem_free(mux_data, sizeof(*mux_data)); 98 return NULL; 99 } 100 101 static void * 102 iicmux_gpio_get_bus_info(struct iicmux_bus * const bus) 103 { 104 struct iicmux_softc * const sc = bus->mux; 105 struct bus_info_gpio *bus_info; 106 int error; 107 108 bus_info = kmem_zalloc(sizeof(*bus_info), KM_SLEEP); 109 110 error = fdtbus_get_reg(bus->handle, 0, &bus_info->value, NULL); 111 if (error) { 112 aprint_error_dev(sc->sc_dev, 113 "unable to get reg property for bus %d\n", bus->busidx); 114 kmem_free(bus_info, sizeof(*bus_info)); 115 return NULL; 116 } 117 118 return bus_info; 119 } 120 121 static void 122 iicmux_gpio_set_value(struct iicmux_softc * const sc, bus_addr_t value) 123 { 124 struct mux_info_gpio * const mux_info = sc->sc_mux_data; 125 int i; 126 127 for (i = 0; i < mux_info->npins; i++, value >>= 1) { 128 fdtbus_gpio_write(mux_info->pins[i], value & 1); 129 } 130 } 131 132 static int 133 iicmux_gpio_acquire_bus(struct iicmux_bus * const bus, int const flags __unused) 134 { 135 struct bus_info_gpio * const bus_info = bus->bus_data; 136 137 iicmux_gpio_set_value(bus->mux, bus_info->value); 138 139 return 0; 140 } 141 142 static void 143 iicmux_gpio_release_bus(struct iicmux_bus * const bus, int const flags __unused) 144 { 145 struct iicmux_softc * const sc = bus->mux; 146 struct mux_info_gpio * const mux_info = sc->sc_mux_data; 147 148 if (mux_info->has_idle_value) { 149 iicmux_gpio_set_value(sc, mux_info->idle_value); 150 } 151 } 152 153 static const struct iicmux_config iicmux_gpio_config = { 154 .desc = "GPIO", 155 .get_mux_info = iicmux_gpio_get_mux_info, 156 .get_bus_info = iicmux_gpio_get_bus_info, 157 .acquire_bus = iicmux_gpio_acquire_bus, 158 .release_bus = iicmux_gpio_release_bus, 159 }; 160 161 /*****************************************************************************/ 162 163 struct mux_info_pinctrl { 164 u_int idle_idx; 165 bool has_idle_idx; 166 } sc_pinctrl; 167 168 struct bus_info_pinctrl { 169 bus_addr_t idx; 170 }; 171 172 static void * 173 iicmux_pinctrl_get_mux_info(struct iicmux_softc * const sc) 174 { 175 struct mux_info_pinctrl *mux_info; 176 177 mux_info = kmem_alloc(sizeof(*mux_info), KM_SLEEP); 178 179 mux_info->has_idle_idx = 180 fdtbus_get_index(sc->sc_handle, "pinctrl-names", "idle", 181 &mux_info->idle_idx) == 0; 182 183 return mux_info; 184 } 185 186 static void * 187 iicmux_pinctrl_get_bus_info(struct iicmux_bus * const bus) 188 { 189 struct iicmux_softc * const sc = bus->mux; 190 struct bus_info_pinctrl *bus_info; 191 int error; 192 193 bus_info = kmem_alloc(sizeof(*bus_info), KM_SLEEP); 194 195 error = fdtbus_get_reg(bus->handle, 0, &bus_info->idx, NULL); 196 if (error) { 197 aprint_error_dev(sc->sc_dev, 198 "unable to get reg property for bus %d\n", bus->busidx); 199 kmem_free(bus_info, sizeof(*bus_info)); 200 return NULL; 201 } 202 203 return bus_info; 204 } 205 206 static int 207 iicmux_pinctrl_acquire_bus(struct iicmux_bus * const bus, 208 int const flags __unused) 209 { 210 struct iicmux_softc * const sc = bus->mux; 211 struct bus_info_pinctrl * const bus_info = bus->bus_data; 212 213 return fdtbus_pinctrl_set_config_index(sc->sc_handle, bus_info->idx); 214 } 215 216 static void 217 iicmux_pinctrl_release_bus(struct iicmux_bus * const bus, 218 int const flags __unused) 219 { 220 struct iicmux_softc * const sc = bus->mux; 221 struct mux_info_pinctrl * const mux_info = sc->sc_mux_data; 222 223 if (mux_info->has_idle_idx) { 224 (void) fdtbus_pinctrl_set_config_index(sc->sc_handle, 225 mux_info->idle_idx); 226 } 227 } 228 229 static const struct iicmux_config iicmux_pinctrl_config = { 230 .desc = "PinMux", 231 .get_mux_info = iicmux_pinctrl_get_mux_info, 232 .get_bus_info = iicmux_pinctrl_get_bus_info, 233 .acquire_bus = iicmux_pinctrl_acquire_bus, 234 .release_bus = iicmux_pinctrl_release_bus, 235 }; 236 237 /*****************************************************************************/ 238 239 static const struct device_compatible_entry compat_data[] = { 240 { .compat = "i2c-mux-gpio", 241 .data = &iicmux_gpio_config }, 242 243 { .compat = "i2c-mux-pinctrl", 244 .data = &iicmux_pinctrl_config }, 245 246 DEVICE_COMPAT_EOL 247 }; 248 249 static int 250 iicmux_fdt_match(device_t const parent, cfdata_t const match, void * const aux) 251 { 252 struct fdt_attach_args * const faa = aux; 253 254 return of_compatible_match(faa->faa_phandle, compat_data); 255 } 256 257 static void 258 iicmux_fdt_attach(device_t const parent, device_t const self, void * const aux) 259 { 260 struct iicmux_softc * const sc = device_private(self); 261 struct fdt_attach_args * const faa = aux; 262 263 sc->sc_dev = self; 264 sc->sc_handle = faa->faa_phandle; 265 sc->sc_config = of_compatible_lookup(sc->sc_handle, compat_data)->data; 266 267 aprint_naive("\n"); 268 aprint_normal(": %s I2C mux\n", sc->sc_config->desc); 269 270 sc->sc_i2c_parent = fdtbus_i2c_acquire(sc->sc_handle, "i2c-parent"); 271 if (sc->sc_i2c_parent == NULL) { 272 aprint_error_dev(sc->sc_dev, "unable to acquire i2c-parent\n"); 273 return; 274 } 275 276 iicmux_attach(sc); 277 } 278 279 CFATTACH_DECL_NEW(iicmux_fdt, sizeof(struct iicmux_softc), 280 iicmux_fdt_match, iicmux_fdt_attach, NULL, NULL); 281