Home | History | Annotate | Line # | Download | only in ic
      1  1.3   andvar /*      $NetBSD: mcp23xxxgpio.c,v 1.3 2025/07/09 21:55:44 andvar Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 2014, 2022 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 Frank Kardel, and 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.3   andvar __KERNEL_RCSID(0, "$NetBSD: mcp23xxxgpio.c,v 1.3 2025/07/09 21:55:44 andvar Exp $");
     34  1.1  thorpej 
     35  1.1  thorpej /*
     36  1.1  thorpej  * Driver for Microchip serial I/O expansers:
     37  1.1  thorpej  *
     38  1.1  thorpej  *	MCP23008	8-bit, I2C interface
     39  1.1  thorpej  *	MCP23S08	8-bit, SPI interface
     40  1.1  thorpej  *	MCP23017	16-bit, I2C interface
     41  1.1  thorpej  *	MCP23S17	16-bit, SPI interface
     42  1.1  thorpej  *	MCP23018	16-bit (open-drain outputs), I2C interface
     43  1.1  thorpej  *	MCP23S18	16-bit (open-drain outputs), SPI interface
     44  1.1  thorpej  *
     45  1.1  thorpej  * Data sheet:
     46  1.1  thorpej  *
     47  1.1  thorpej  *	https://ww1.microchip.com/downloads/en/DeviceDoc/20001952C.pdf
     48  1.1  thorpej  */
     49  1.1  thorpej 
     50  1.1  thorpej #include "gpio.h"
     51  1.1  thorpej 
     52  1.1  thorpej #include <sys/types.h>
     53  1.1  thorpej #include <sys/systm.h>
     54  1.1  thorpej #include <sys/device.h>
     55  1.1  thorpej #include <sys/kernel.h>
     56  1.1  thorpej #include <sys/kmem.h>
     57  1.1  thorpej 
     58  1.1  thorpej #include <dev/ic/mcp23xxxgpioreg.h>
     59  1.1  thorpej #include <dev/ic/mcp23xxxgpiovar.h>
     60  1.1  thorpej 
     61  1.1  thorpej #define	PIN_BANK(p)	((p) / MCPGPIO_PINS_PER_BANK)
     62  1.1  thorpej #define	PIN_EPIN(p)	((p) % MCPGPIO_PINS_PER_BANK)
     63  1.1  thorpej 
     64  1.1  thorpej static uint8_t
     65  1.1  thorpej mcpgpio_regaddr(struct mcpgpio_softc *sc, uint8_t bank, uint8_t reg)
     66  1.1  thorpej {
     67  1.1  thorpej 
     68  1.1  thorpej 	if (sc->sc_variant->type == MCPGPIO_TYPE_23x08) {
     69  1.1  thorpej 		return reg;
     70  1.1  thorpej 	}
     71  1.1  thorpej 	if (sc->sc_iocon & IOCON_BANK) {
     72  1.1  thorpej 		return REGADDR_BANK1(bank & 1, reg);
     73  1.1  thorpej 	}
     74  1.1  thorpej 	return REGADDR_BANK0(bank & 1, reg);
     75  1.1  thorpej }
     76  1.1  thorpej 
     77  1.1  thorpej static const char *
     78  1.1  thorpej mcpgpio_regname(uint8_t reg)
     79  1.1  thorpej {
     80  1.1  thorpej 	static const char * const regnames[] = {
     81  1.1  thorpej 		[REG_IODIR]	=	"IODIR",
     82  1.1  thorpej 		[REG_IPOL]	=	"IPOL",
     83  1.1  thorpej 		[REG_GPINTEN]	=	"GPINTEN",
     84  1.1  thorpej 		[REG_DEFVAL]	=	"DEFVAL",
     85  1.1  thorpej 		[REG_INTCON]	=	"INTCON",
     86  1.1  thorpej 		[REG_IOCON]	=	"IOCON",
     87  1.1  thorpej 		[REG_GPPU]	=	"GPPU",
     88  1.1  thorpej 		[REG_INTF]	=	"INTF",
     89  1.1  thorpej 		[REG_INTCAP]	=	"INTCAP",
     90  1.1  thorpej 		[REG_GPIO]	=	"GPIO",
     91  1.1  thorpej 		[REG_OLAT]	=	"OLAT",
     92  1.1  thorpej 	};
     93  1.1  thorpej 	KASSERT(reg <= REG_OLAT);
     94  1.1  thorpej 	return regnames[reg];
     95  1.1  thorpej }
     96  1.1  thorpej 
     97  1.1  thorpej static const char *
     98  1.1  thorpej mcpgpio_bankname(struct mcpgpio_softc *sc, uint8_t bank)
     99  1.1  thorpej {
    100  1.1  thorpej 	static const char * const banknames[] = { "A", "B" };
    101  1.1  thorpej 
    102  1.1  thorpej 	if (sc->sc_variant->type == MCPGPIO_TYPE_23x08) {
    103  1.1  thorpej 		return "";
    104  1.1  thorpej 	}
    105  1.1  thorpej 	return banknames[bank & 1];
    106  1.1  thorpej }
    107  1.1  thorpej 
    108  1.1  thorpej static int
    109  1.1  thorpej mcpgpio__lock(struct mcpgpio_softc *sc, const char *fn)
    110  1.1  thorpej {
    111  1.1  thorpej 	int error;
    112  1.1  thorpej 
    113  1.1  thorpej 	error = sc->sc_accessops->lock(sc);
    114  1.1  thorpej 	if (__predict_false(error != 0)) {
    115  1.1  thorpej 		aprint_error_dev(sc->sc_dev,
    116  1.1  thorpej 		    "%s: unable to lock device, error=%d\n", fn, error);
    117  1.1  thorpej 	}
    118  1.1  thorpej 	return error;
    119  1.1  thorpej }
    120  1.1  thorpej 
    121  1.1  thorpej #define	mcpgpio_lock(sc)						\
    122  1.1  thorpej 	mcpgpio__lock((sc), __func__)
    123  1.1  thorpej 
    124  1.1  thorpej static void
    125  1.1  thorpej mcpgpio_unlock(struct mcpgpio_softc *sc)
    126  1.1  thorpej {
    127  1.1  thorpej 	sc->sc_accessops->unlock(sc);
    128  1.1  thorpej }
    129  1.1  thorpej 
    130  1.1  thorpej static int
    131  1.1  thorpej mcpgpio__read(struct mcpgpio_softc *sc, const char *fn,
    132  1.1  thorpej     uint8_t bank, uint8_t reg, uint8_t *valp)
    133  1.1  thorpej {
    134  1.1  thorpej 	int error;
    135  1.1  thorpej 	uint8_t regaddr = mcpgpio_regaddr(sc, bank, reg);
    136  1.1  thorpej 
    137  1.1  thorpej 	error = sc->sc_accessops->read(sc, bank, regaddr, valp);
    138  1.1  thorpej 	if (__predict_false(error != 0)) {
    139  1.1  thorpej 		aprint_error_dev(sc->sc_dev,
    140  1.1  thorpej 		    "%s: unable to read %s%s[0x%02x], error=%d\n", fn,
    141  1.1  thorpej 		    mcpgpio_regname(reg), mcpgpio_bankname(sc, bank),
    142  1.1  thorpej 		    regaddr, error);
    143  1.1  thorpej 	}
    144  1.1  thorpej 	return error;
    145  1.1  thorpej }
    146  1.1  thorpej 
    147  1.1  thorpej #define	mcpgpio_read(sc, b, r, v)					\
    148  1.1  thorpej 	mcpgpio__read((sc), __func__, (b), (r), (v))
    149  1.1  thorpej 
    150  1.1  thorpej static int
    151  1.1  thorpej mcpgpio__write(struct mcpgpio_softc *sc, const char *fn,
    152  1.1  thorpej     uint8_t bank, uint8_t reg, uint8_t val)
    153  1.1  thorpej {
    154  1.1  thorpej 	int error;
    155  1.1  thorpej 	uint8_t regaddr = mcpgpio_regaddr(sc, bank, reg);
    156  1.1  thorpej 
    157  1.1  thorpej 	error = sc->sc_accessops->write(sc, bank, regaddr, val);
    158  1.1  thorpej 	if (__predict_false(error != 0)) {
    159  1.1  thorpej 		aprint_error_dev(sc->sc_dev,
    160  1.1  thorpej 		    "%s: unable to write %s%s[0x%02x], error=%d\n", fn,
    161  1.1  thorpej 		    mcpgpio_regname(reg), mcpgpio_bankname(sc, bank),
    162  1.1  thorpej 		    regaddr, error);
    163  1.1  thorpej 	}
    164  1.1  thorpej 	return error;
    165  1.1  thorpej }
    166  1.1  thorpej 
    167  1.1  thorpej #define	mcpgpio_write(sc, b, r, v)					\
    168  1.1  thorpej 	mcpgpio__write((sc), __func__, (b), (r), (v))
    169  1.1  thorpej 
    170  1.1  thorpej #if NGPIO > 0
    171  1.1  thorpej /* GPIO support functions */
    172  1.1  thorpej static int
    173  1.1  thorpej mcpgpio_gpio_pin_read(void *arg, int pin)
    174  1.1  thorpej {
    175  1.1  thorpej 	struct mcpgpio_softc *sc = arg;
    176  1.1  thorpej 	uint8_t data;
    177  1.1  thorpej 	int val;
    178  1.1  thorpej 	int error;
    179  1.1  thorpej 
    180  1.1  thorpej 	KASSERT(pin >= 0 && pin < sc->sc_npins);
    181  1.1  thorpej 
    182  1.1  thorpej 	const uint8_t bank = PIN_BANK(pin);
    183  1.1  thorpej 	const uint8_t epin = PIN_EPIN(pin);
    184  1.1  thorpej 
    185  1.1  thorpej 	error = mcpgpio_lock(sc);
    186  1.1  thorpej 	if (__predict_false(error != 0)) {
    187  1.1  thorpej 		return GPIO_PIN_LOW;
    188  1.1  thorpej 	}
    189  1.1  thorpej 	error = mcpgpio_read(sc, bank, REG_GPIO, &data);
    190  1.1  thorpej 	if (error) {
    191  1.1  thorpej 		data = 0;
    192  1.1  thorpej 	}
    193  1.1  thorpej 	mcpgpio_unlock(sc);
    194  1.1  thorpej 
    195  1.1  thorpej 	val = data & __BIT(epin) ? GPIO_PIN_HIGH : GPIO_PIN_LOW;
    196  1.1  thorpej 
    197  1.1  thorpej 	return val;
    198  1.1  thorpej }
    199  1.1  thorpej 
    200  1.1  thorpej static void
    201  1.1  thorpej mcpgpio_gpio_pin_write(void *arg, int pin, int value)
    202  1.1  thorpej {
    203  1.1  thorpej 	struct mcpgpio_softc *sc = arg;
    204  1.1  thorpej 	uint8_t data;
    205  1.1  thorpej 	int error;
    206  1.1  thorpej 
    207  1.1  thorpej 	KASSERT(pin >= 0 && pin < sc->sc_npins);
    208  1.1  thorpej 
    209  1.1  thorpej 	const uint8_t bank = PIN_BANK(pin);
    210  1.1  thorpej 	const uint8_t epin = PIN_EPIN(pin);
    211  1.1  thorpej 
    212  1.1  thorpej 	error = mcpgpio_lock(sc);
    213  1.1  thorpej 	if (__predict_false(error != 0)) {
    214  1.1  thorpej 		return;
    215  1.1  thorpej 	}
    216  1.1  thorpej 
    217  1.1  thorpej 	error = mcpgpio_read(sc, bank, REG_OLAT, &data);
    218  1.1  thorpej 	if (__predict_true(error == 0)) {
    219  1.1  thorpej 		if (value == GPIO_PIN_HIGH) {
    220  1.1  thorpej 			data |= __BIT(epin);
    221  1.1  thorpej 		} else {
    222  1.1  thorpej 			data &= ~__BIT(epin);
    223  1.1  thorpej 		}
    224  1.1  thorpej 		(void) mcpgpio_write(sc, bank, REG_OLAT, data);
    225  1.1  thorpej 	}
    226  1.1  thorpej 
    227  1.1  thorpej 	mcpgpio_unlock(sc);
    228  1.1  thorpej }
    229  1.1  thorpej 
    230  1.1  thorpej static void
    231  1.1  thorpej mcpgpio_gpio_pin_ctl(void *arg, int pin, int flags)
    232  1.1  thorpej {
    233  1.1  thorpej 	struct mcpgpio_softc *sc = arg;
    234  1.1  thorpej 	uint8_t iodir, ipol, gppu;
    235  1.1  thorpej 	int error;
    236  1.1  thorpej 
    237  1.1  thorpej 	KASSERT(pin >= 0 && pin < sc->sc_npins);
    238  1.1  thorpej 
    239  1.1  thorpej 	const uint8_t bank = PIN_BANK(pin);
    240  1.1  thorpej 	const uint8_t epin = PIN_EPIN(pin);
    241  1.1  thorpej 	const uint8_t bit = __BIT(epin);
    242  1.1  thorpej 
    243  1.1  thorpej 	error = mcpgpio_lock(sc);
    244  1.1  thorpej 	if (__predict_false(error != 0)) {
    245  1.1  thorpej 		return;
    246  1.1  thorpej 	}
    247  1.1  thorpej 
    248  1.1  thorpej 	if ((error = mcpgpio_read(sc, bank, REG_IODIR, &iodir)) != 0 ||
    249  1.1  thorpej 	    (error = mcpgpio_read(sc, bank, REG_IPOL, &ipol)) != 0 ||
    250  1.1  thorpej 	    (error = mcpgpio_read(sc, bank, REG_GPPU, &gppu)) != 0) {
    251  1.1  thorpej 		return;
    252  1.1  thorpej 	}
    253  1.1  thorpej 
    254  1.1  thorpej 	if (flags & (GPIO_PIN_OUTPUT|GPIO_PIN_INPUT)) {
    255  1.1  thorpej 		if ((flags & GPIO_PIN_INPUT) || !(flags & GPIO_PIN_OUTPUT)) {
    256  1.1  thorpej 			/* for safety INPUT will override output */
    257  1.1  thorpej 			iodir |= bit;
    258  1.1  thorpej 		} else {
    259  1.1  thorpej 			iodir &= ~bit;
    260  1.1  thorpej 		}
    261  1.1  thorpej 	}
    262  1.1  thorpej 
    263  1.1  thorpej 	if (flags & GPIO_PIN_INVIN) {
    264  1.1  thorpej 		ipol |= bit;
    265  1.1  thorpej 	} else {
    266  1.1  thorpej 		ipol &= ~bit;
    267  1.1  thorpej 	}
    268  1.1  thorpej 
    269  1.1  thorpej 	if (flags & GPIO_PIN_PULLUP) {
    270  1.1  thorpej 		gppu |= bit;
    271  1.1  thorpej 	} else {
    272  1.1  thorpej 		gppu &= ~bit;
    273  1.1  thorpej 	}
    274  1.1  thorpej 
    275  1.1  thorpej 	(void) mcpgpio_write(sc, bank, REG_IODIR, iodir);
    276  1.1  thorpej 	(void) mcpgpio_write(sc, bank, REG_IPOL, ipol);
    277  1.1  thorpej 	(void) mcpgpio_write(sc, bank, REG_GPPU, gppu);
    278  1.1  thorpej 
    279  1.1  thorpej 	mcpgpio_unlock(sc);
    280  1.1  thorpej }
    281  1.1  thorpej #endif /* NGPIO > 0 */
    282  1.1  thorpej 
    283  1.1  thorpej void
    284  1.1  thorpej mcpgpio_attach(struct mcpgpio_softc *sc)
    285  1.1  thorpej {
    286  1.1  thorpej 	int error;
    287  1.1  thorpej 
    288  1.1  thorpej 	KASSERT(sc->sc_variant != NULL);
    289  1.1  thorpej 
    290  1.1  thorpej 	/*
    291  1.1  thorpej 	 * The SPI front-end provides the logical pin count to
    292  1.3   andvar 	 * deal with multiple chips on one chip select.
    293  1.1  thorpej 	 */
    294  1.1  thorpej 	if (sc->sc_npins == 0) {
    295  1.1  thorpej 		sc->sc_npins = sc->sc_variant->type == MCPGPIO_TYPE_23x08
    296  1.2  thorpej 		    ? MCP23x08_GPIO_NPINS : MCP23x17_GPIO_NPINS;
    297  1.1  thorpej 	}
    298  1.1  thorpej 	sc->sc_gpio_pins =
    299  1.1  thorpej 	    kmem_zalloc(sc->sc_npins * sizeof(*sc->sc_gpio_pins), KM_SLEEP);
    300  1.1  thorpej 
    301  1.1  thorpej 	/*
    302  1.1  thorpej 	 * Perform the basic setup of the device.  We program the IOCON
    303  1.1  thorpej 	 * register once for each bank, even though the data sheet is
    304  1.1  thorpej 	 * not clear that this is strictly necessary.
    305  1.1  thorpej 	 */
    306  1.1  thorpej 	if (mcpgpio_lock(sc) != 0) {
    307  1.1  thorpej 		return;
    308  1.1  thorpej 	}
    309  1.1  thorpej 	error = mcpgpio_write(sc, 0, REG_IOCON, sc->sc_iocon);
    310  1.1  thorpej 	if (error == 0 && sc->sc_variant->type != MCPGPIO_TYPE_23x08) {
    311  1.1  thorpej 		error = mcpgpio_write(sc, 1, REG_IOCON, sc->sc_iocon);
    312  1.1  thorpej 	}
    313  1.1  thorpej 	mcpgpio_unlock(sc);
    314  1.1  thorpej 	if (error) {
    315  1.1  thorpej 		return;
    316  1.1  thorpej 	}
    317  1.1  thorpej 
    318  1.1  thorpej 	/* XXX FDT glue. */
    319  1.1  thorpej 
    320  1.1  thorpej #if NGPIO > 0
    321  1.1  thorpej 	struct gpiobus_attach_args gba;
    322  1.1  thorpej 	int pin_output_caps;
    323  1.1  thorpej 	int i;
    324  1.1  thorpej 
    325  1.1  thorpej 	pin_output_caps = sc->sc_variant->type == MCPGPIO_TYPE_23x18
    326  1.1  thorpej 	    ? GPIO_PIN_OPENDRAIN : GPIO_PIN_PUSHPULL;
    327  1.1  thorpej 
    328  1.1  thorpej 	for (i = 0; i < sc->sc_npins; i++) {
    329  1.1  thorpej 		sc->sc_gpio_pins[i].pin_num = i;
    330  1.1  thorpej 		sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
    331  1.1  thorpej 			GPIO_PIN_OUTPUT |
    332  1.1  thorpej 			pin_output_caps |
    333  1.1  thorpej 			GPIO_PIN_PULLUP |
    334  1.1  thorpej 			GPIO_PIN_INVIN;
    335  1.1  thorpej 
    336  1.1  thorpej 		/* read initial state */
    337  1.1  thorpej 		sc->sc_gpio_pins[i].pin_state =
    338  1.1  thorpej 			mcpgpio_gpio_pin_read(sc, i);
    339  1.1  thorpej 	}
    340  1.1  thorpej 
    341  1.1  thorpej 	/* create controller tag */
    342  1.1  thorpej 	sc->sc_gpio_gc.gp_cookie = sc;
    343  1.1  thorpej 	sc->sc_gpio_gc.gp_pin_read = mcpgpio_gpio_pin_read;
    344  1.1  thorpej 	sc->sc_gpio_gc.gp_pin_write = mcpgpio_gpio_pin_write;
    345  1.1  thorpej 	sc->sc_gpio_gc.gp_pin_ctl = mcpgpio_gpio_pin_ctl;
    346  1.1  thorpej 
    347  1.1  thorpej 	gba.gba_gc = &sc->sc_gpio_gc;
    348  1.1  thorpej 	gba.gba_pins = sc->sc_gpio_pins;
    349  1.1  thorpej 	gba.gba_npins = sc->sc_npins;
    350  1.1  thorpej 
    351  1.2  thorpej 	config_found(sc->sc_dev, &gba, gpiobus_print,
    352  1.2  thorpej 	    CFARGS(.devhandle = device_handle(sc->sc_dev)));
    353  1.1  thorpej #else
    354  1.1  thorpej 	aprint_normal_dev(sc->sc_dev, "no GPIO configured in kernel");
    355  1.1  thorpej #endif
    356  1.1  thorpej }
    357