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