Home | History | Annotate | Line # | Download | only in fdt
i2cmux_fdt.c revision 1.10.4.1
      1 /*	$NetBSD: i2cmux_fdt.c,v 1.10.4.1 2021/05/08 02:44:22 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.4.1 2021/05/08 02:44:22 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 	const int phandle = devhandle_to_of(device_handle(sc->sc_dev));
     63 	int i;
     64 
     65 	mux_data = kmem_zalloc(sizeof(*mux_data), KM_SLEEP);
     66 
     67 	mux_data->npins = fdtbus_gpio_count(phandle, "mux-gpios");
     68 	if (mux_data->npins == 0) {
     69 		aprint_error_dev(sc->sc_dev,
     70 		    "unable to get mux-gpios property\n");
     71 		goto bad;
     72 	}
     73 
     74 	mux_data->pins =
     75 	    kmem_zalloc(sizeof(*mux_data->pins) * mux_data->npins, KM_SLEEP);
     76 	for (i = 0; i < mux_data->npins; i++) {
     77 		mux_data->pins[i] = fdtbus_gpio_acquire_index(phandle,
     78 		    "mux-gpios", i, GPIO_PIN_OUTPUT);
     79 		if (mux_data->pins[i] == NULL) {
     80 			aprint_error_dev(sc->sc_dev,
     81 			    "unable to acquire gpio #%d\n", i);
     82 			goto bad;
     83 		}
     84 	}
     85 
     86 	mux_data->has_idle_value =
     87 	    of_getprop_uint32(phandle, "idle-state",
     88 			      &mux_data->idle_value) == 0;
     89 
     90 	return mux_data;
     91 
     92  bad:
     93 	for (i = 0; i < mux_data->npins; i++) {
     94 		if (mux_data->pins[i] != NULL) {
     95 			fdtbus_gpio_release(mux_data->pins[i]);
     96 		}
     97 	}
     98 	kmem_free(mux_data, sizeof(*mux_data));
     99 	return NULL;
    100 }
    101 
    102 static void *
    103 iicmux_gpio_get_bus_info(struct iicmux_bus * const bus)
    104 {
    105 	struct iicmux_softc * const sc = bus->mux;
    106 	const int phandle = devhandle_to_of(bus->devhandle);
    107 	struct bus_info_gpio *bus_info;
    108 	int error;
    109 
    110 	bus_info = kmem_zalloc(sizeof(*bus_info), KM_SLEEP);
    111 
    112 	error = fdtbus_get_reg(phandle, 0, &bus_info->value, NULL);
    113 	if (error) {
    114 		aprint_error_dev(sc->sc_dev,
    115 		    "unable to get reg property for bus %d\n", bus->busidx);
    116 		kmem_free(bus_info, sizeof(*bus_info));
    117 		return NULL;
    118 	}
    119 
    120 	return bus_info;
    121 }
    122 
    123 static void
    124 iicmux_gpio_set_value(struct iicmux_softc * const sc, bus_addr_t value)
    125 {
    126 	struct mux_info_gpio * const mux_info = sc->sc_mux_data;
    127 	int i;
    128 
    129 	for (i = 0; i < mux_info->npins; i++, value >>= 1) {
    130 		fdtbus_gpio_write(mux_info->pins[i], value & 1);
    131 	}
    132 }
    133 
    134 static int
    135 iicmux_gpio_acquire_bus(struct iicmux_bus * const bus, int const flags __unused)
    136 {
    137 	struct bus_info_gpio * const bus_info = bus->bus_data;
    138 
    139 	iicmux_gpio_set_value(bus->mux, bus_info->value);
    140 
    141 	return 0;
    142 }
    143 
    144 static void
    145 iicmux_gpio_release_bus(struct iicmux_bus * const bus, int const flags __unused)
    146 {
    147 	struct iicmux_softc * const sc = bus->mux;
    148 	struct mux_info_gpio * const mux_info = sc->sc_mux_data;
    149 
    150 	if (mux_info->has_idle_value) {
    151 		iicmux_gpio_set_value(sc, mux_info->idle_value);
    152 	}
    153 }
    154 
    155 static const struct iicmux_config iicmux_gpio_config = {
    156 	.desc = "GPIO",
    157 	.get_mux_info = iicmux_gpio_get_mux_info,
    158 	.get_bus_info = iicmux_gpio_get_bus_info,
    159 	.acquire_bus = iicmux_gpio_acquire_bus,
    160 	.release_bus = iicmux_gpio_release_bus,
    161 };
    162 
    163 /*****************************************************************************/
    164 
    165 struct mux_info_pinctrl {
    166 	u_int idle_idx;
    167 	bool has_idle_idx;
    168 } sc_pinctrl;
    169 
    170 struct bus_info_pinctrl {
    171 	bus_addr_t idx;
    172 };
    173 
    174 static void *
    175 iicmux_pinctrl_get_mux_info(struct iicmux_softc * const sc)
    176 {
    177 	const int phandle = devhandle_to_of(device_handle(sc->sc_dev));
    178 	struct mux_info_pinctrl *mux_info;
    179 
    180 	mux_info = kmem_alloc(sizeof(*mux_info), KM_SLEEP);
    181 
    182 	mux_info->has_idle_idx =
    183 	    fdtbus_get_index(phandle, "pinctrl-names", "idle",
    184 			     &mux_info->idle_idx) == 0;
    185 
    186 	return mux_info;
    187 }
    188 
    189 static void *
    190 iicmux_pinctrl_get_bus_info(struct iicmux_bus * const bus)
    191 {
    192 	struct iicmux_softc * const sc = bus->mux;
    193 	const int phandle = devhandle_to_of(bus->devhandle);
    194 	struct bus_info_pinctrl *bus_info;
    195 	int error;
    196 
    197 	bus_info = kmem_alloc(sizeof(*bus_info), KM_SLEEP);
    198 
    199 	error = fdtbus_get_reg(phandle, 0, &bus_info->idx, NULL);
    200 	if (error) {
    201 		aprint_error_dev(sc->sc_dev,
    202 		    "unable to get reg property for bus %d\n", bus->busidx);
    203 		kmem_free(bus_info, sizeof(*bus_info));
    204 		return NULL;
    205 	}
    206 
    207 	return bus_info;
    208 }
    209 
    210 static int
    211 iicmux_pinctrl_acquire_bus(struct iicmux_bus * const bus,
    212     int const flags __unused)
    213 {
    214 	struct iicmux_softc * const sc = bus->mux;
    215 	const int phandle = devhandle_to_of(device_handle(sc->sc_dev));
    216 	struct bus_info_pinctrl * const bus_info = bus->bus_data;
    217 
    218 	return fdtbus_pinctrl_set_config_index(phandle, bus_info->idx);
    219 }
    220 
    221 static void
    222 iicmux_pinctrl_release_bus(struct iicmux_bus * const bus,
    223     int const flags __unused)
    224 {
    225 	struct iicmux_softc * const sc = bus->mux;
    226 	const int phandle = devhandle_to_of(device_handle(sc->sc_dev));
    227 	struct mux_info_pinctrl * const mux_info = sc->sc_mux_data;
    228 
    229 	if (mux_info->has_idle_idx) {
    230 		(void) fdtbus_pinctrl_set_config_index(phandle,
    231 		    mux_info->idle_idx);
    232 	}
    233 }
    234 
    235 static const struct iicmux_config iicmux_pinctrl_config = {
    236 	.desc = "PinMux",
    237 	.get_mux_info = iicmux_pinctrl_get_mux_info,
    238 	.get_bus_info = iicmux_pinctrl_get_bus_info,
    239 	.acquire_bus = iicmux_pinctrl_acquire_bus,
    240 	.release_bus = iicmux_pinctrl_release_bus,
    241 };
    242 
    243 /*****************************************************************************/
    244 
    245 static const struct device_compatible_entry compat_data[] = {
    246 	{ .compat = "i2c-mux-gpio",
    247 	  .data = &iicmux_gpio_config },
    248 
    249 	{ .compat = "i2c-mux-pinctrl",
    250 	  .data = &iicmux_pinctrl_config },
    251 
    252 	DEVICE_COMPAT_EOL
    253 };
    254 
    255 static int
    256 iicmux_fdt_match(device_t const parent, cfdata_t const match, void * const aux)
    257 {
    258 	struct fdt_attach_args * const faa = aux;
    259 
    260 	return of_compatible_match(faa->faa_phandle, compat_data);
    261 }
    262 
    263 static void
    264 iicmux_fdt_attach(device_t const parent, device_t const self, void * const aux)
    265 {
    266 	struct iicmux_softc * const sc = device_private(self);
    267 	struct fdt_attach_args * const faa = aux;
    268 
    269 	sc->sc_dev = self;
    270 	sc->sc_config =
    271 	    of_compatible_lookup(faa->faa_phandle, compat_data)->data;
    272 
    273 	aprint_naive("\n");
    274 	aprint_normal(": %s I2C mux\n", sc->sc_config->desc);
    275 
    276 	sc->sc_i2c_parent = fdtbus_i2c_acquire(faa->faa_phandle, "i2c-parent");
    277 	if (sc->sc_i2c_parent == NULL) {
    278 		aprint_error_dev(sc->sc_dev, "unable to acquire i2c-parent\n");
    279 		return;
    280 	}
    281 
    282 	iicmux_attach(sc);
    283 }
    284 
    285 CFATTACH_DECL_NEW(iicmux_fdt, sizeof(struct iicmux_softc),
    286     iicmux_fdt_match, iicmux_fdt_attach, NULL, NULL);
    287