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