xref: /netbsd/sys/dev/spi/mcp23xxxgpio_spi.c (revision da38dc41)
1*da38dc41Sthorpej /*      $NetBSD: mcp23xxxgpio_spi.c,v 1.4 2022/01/19 05:21:44 thorpej Exp $ */
24ee342adSthorpej 
34ee342adSthorpej /*-
44ee342adSthorpej  * Copyright (c) 2014, 2022 The NetBSD Foundation, Inc.
54ee342adSthorpej  * All rights reserved.
64ee342adSthorpej  *
74ee342adSthorpej  * This code is derived from software contributed to The NetBSD Foundation
84ee342adSthorpej  * by Frank Kardel, and by Jason R. Thorpe.
94ee342adSthorpej  *
104ee342adSthorpej  * Redistribution and use in source and binary forms, with or without
114ee342adSthorpej  * modification, are permitted provided that the following conditions
124ee342adSthorpej  * are met:
134ee342adSthorpej  * 1. Redistributions of source code must retain the above copyright
144ee342adSthorpej  *    notice, this list of conditions and the following disclaimer.
154ee342adSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
164ee342adSthorpej  *    notice, this list of conditions and the following disclaimer in the
174ee342adSthorpej  *    documentation and/or other materials provided with the distribution.
184ee342adSthorpej  *
194ee342adSthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
204ee342adSthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
214ee342adSthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
224ee342adSthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
234ee342adSthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
244ee342adSthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
254ee342adSthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
264ee342adSthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
274ee342adSthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
284ee342adSthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
294ee342adSthorpej  * POSSIBILITY OF SUCH DAMAGE.
304ee342adSthorpej  */
314ee342adSthorpej 
324ee342adSthorpej #include <sys/cdefs.h>
33*da38dc41Sthorpej __KERNEL_RCSID(0, "$NetBSD: mcp23xxxgpio_spi.c,v 1.4 2022/01/19 05:21:44 thorpej Exp $");
344ee342adSthorpej 
354ee342adSthorpej /*
364ee342adSthorpej  * Driver for Microchip serial I/O expanders:
374ee342adSthorpej  *
384ee342adSthorpej  *	MCP23S08	8-bit, SPI interface
394ee342adSthorpej  *	MCP23S17	16-bit, SPI interface
404ee342adSthorpej  *	MCP23S18	16-bit (open-drain outputs), SPI interface
414ee342adSthorpej  *
424ee342adSthorpej  * Data sheet:
434ee342adSthorpej  *
444ee342adSthorpej  *	https://ww1.microchip.com/downloads/en/DeviceDoc/20001952C.pdf
454ee342adSthorpej  */
464ee342adSthorpej 
474ee342adSthorpej #include <sys/types.h>
484ee342adSthorpej #include <sys/bitops.h>
494ee342adSthorpej #include <sys/device.h>
504ee342adSthorpej #include <sys/kernel.h>
514ee342adSthorpej #include <sys/mutex.h>
524ee342adSthorpej 
534ee342adSthorpej #include <dev/ic/mcp23xxxgpioreg.h>
544ee342adSthorpej #include <dev/ic/mcp23xxxgpiovar.h>
554ee342adSthorpej 
564ee342adSthorpej #include <dev/spi/spivar.h>
574ee342adSthorpej 
584ee342adSthorpej /*
594ee342adSthorpej  * Multi-chip-on-select configurations appear to the upper layers like
604ee342adSthorpej  * additional GPIO banks; mixing different chip types on the same chip
614ee342adSthorpej  * select is not allowed.
624ee342adSthorpej  *
634ee342adSthorpej  * Some chips have 2 banks per chip, and we have up to 8 chips per chip
644ee342adSthorpej  * select, it's a total of 16 banks per chip select / driver instance.
654ee342adSthorpej  */
664ee342adSthorpej #define	MCPGPIO_SPI_MAXBANKS	16
674ee342adSthorpej 
684ee342adSthorpej struct mcpgpio_spi_softc {
694ee342adSthorpej 	struct mcpgpio_softc	sc_mcpgpio;
704ee342adSthorpej 
714ee342adSthorpej 	kmutex_t		sc_mutex;
724ee342adSthorpej 	struct spi_handle      *sc_sh;
734ee342adSthorpej 	uint8_t			sc_ha[MCPGPIO_SPI_MAXBANKS];
744ee342adSthorpej };
754ee342adSthorpej 
764ee342adSthorpej /*
774ee342adSthorpej  * SPI-specific commands (the serial interface on the I2C flavor of
784ee342adSthorpej  * the chip uses the I2C protocol to infer this information).  Careful
794ee342adSthorpej  * readers will note that this ends up being exactly the same bits
804ee342adSthorpej  * on the serial interface that the I2C flavor of the chip uses.
814ee342adSthorpej  *
824ee342adSthorpej  * The SPI version can have up to 4 (or 8) chips per chip-select, demuxed
834ee342adSthorpej  * using the hardware address (selected by tying the 2 or 3 HA pins high/low
844ee342adSthorpej  * as desired).
854ee342adSthorpej  */
864ee342adSthorpej #define	OP_READ(ha)	(0x41 | ((ha) << 1))
874ee342adSthorpej #define	OP_WRITE(ha)	(0x40 | ((ha) << 1))
884ee342adSthorpej 
894ee342adSthorpej #define	MCPGPIO_TO_SPI(sc)					\
904ee342adSthorpej 	container_of((sc), struct mcpgpio_spi_softc, sc_mcpgpio)
914ee342adSthorpej 
924ee342adSthorpej #if 0
934ee342adSthorpej static const struct mcpgpio_variant mcp23s08 = {
944ee342adSthorpej 	.name = "MCP23S08",
954ee342adSthorpej 	.type = MCPGPIO_TYPE_23x08,
964ee342adSthorpej };
974ee342adSthorpej #endif
984ee342adSthorpej 
994ee342adSthorpej static const struct mcpgpio_variant mcp23s17 = {
1004ee342adSthorpej 	.name = "MCP23S17",
1014ee342adSthorpej 	.type = MCPGPIO_TYPE_23x17,
1024ee342adSthorpej };
1034ee342adSthorpej 
1044ee342adSthorpej #if 0
1054ee342adSthorpej static const struct mcpgpio_variant mcp23s18 = {
1064ee342adSthorpej 	.name = "MCP23S18",
1074ee342adSthorpej 	.type = MCPGPIO_TYPE_23x18,
1084ee342adSthorpej };
1094ee342adSthorpej #endif
1104ee342adSthorpej 
1114ee342adSthorpej #if 0
1124ee342adSthorpej static const struct device_compatible_entry compat_data[] = {
1134ee342adSthorpej 	{ .compat = "microchip,mcp23s08",	.data = &mcp23s08 },
1144ee342adSthorpej 	{ .compat = "microchip,mcp23s17",	.data = &mcp23s17 },
1154ee342adSthorpej 	{ .compat = "microchip,mcp23s18",	.data = &mcp23s18 },
1164ee342adSthorpej 	DEVICE_COMPAT_EOL
1174ee342adSthorpej };
1184ee342adSthorpej #endif
1194ee342adSthorpej 
1204ee342adSthorpej static int
mcpgpio_spi_lock(struct mcpgpio_softc * sc)1214ee342adSthorpej mcpgpio_spi_lock(struct mcpgpio_softc *sc)
1224ee342adSthorpej {
1234ee342adSthorpej 	struct mcpgpio_spi_softc *ssc = MCPGPIO_TO_SPI(sc);
1244ee342adSthorpej 
1254ee342adSthorpej 	mutex_enter(&ssc->sc_mutex);
1264ee342adSthorpej 	return 0;
1274ee342adSthorpej }
1284ee342adSthorpej 
1294ee342adSthorpej static void
mcpgpio_spi_unlock(struct mcpgpio_softc * sc)1304ee342adSthorpej mcpgpio_spi_unlock(struct mcpgpio_softc *sc)
1314ee342adSthorpej {
1324ee342adSthorpej 	struct mcpgpio_spi_softc *ssc = MCPGPIO_TO_SPI(sc);
1334ee342adSthorpej 
1344ee342adSthorpej 	mutex_exit(&ssc->sc_mutex);
1354ee342adSthorpej }
1364ee342adSthorpej 
1374ee342adSthorpej static int
mcpgpio_spi_read(struct mcpgpio_softc * sc,unsigned int bank,uint8_t reg,uint8_t * valp)1384ee342adSthorpej mcpgpio_spi_read(struct mcpgpio_softc *sc, unsigned int bank,
1394ee342adSthorpej     uint8_t reg, uint8_t *valp)
1404ee342adSthorpej {
1414ee342adSthorpej 	struct mcpgpio_spi_softc *ssc = MCPGPIO_TO_SPI(sc);
1424ee342adSthorpej 	uint8_t buf[2];
1434ee342adSthorpej 
1444ee342adSthorpej 	KASSERT(bank < (sc->sc_npins >> 3));
1454ee342adSthorpej 
1464ee342adSthorpej 	buf[0] = OP_READ(ssc->sc_ha[bank]);
1474ee342adSthorpej 	buf[1] = reg;
1484ee342adSthorpej 
1494ee342adSthorpej 	return spi_send_recv(ssc->sc_sh, 2, buf, 1, valp);
1504ee342adSthorpej }
1514ee342adSthorpej 
1524ee342adSthorpej static int
mcpgpio_spi_write(struct mcpgpio_softc * sc,unsigned int bank,uint8_t reg,uint8_t val)1534ee342adSthorpej mcpgpio_spi_write(struct mcpgpio_softc *sc, unsigned int bank,
1544ee342adSthorpej     uint8_t reg, uint8_t val)
1554ee342adSthorpej {
1564ee342adSthorpej 	struct mcpgpio_spi_softc *ssc = MCPGPIO_TO_SPI(sc);
1574ee342adSthorpej 	uint8_t buf[3];
1584ee342adSthorpej 
1594ee342adSthorpej 	KASSERT(bank < (sc->sc_npins >> 3));
1604ee342adSthorpej 
1614ee342adSthorpej 	buf[0] = OP_WRITE(ssc->sc_ha[bank]);
1624ee342adSthorpej 	buf[1] = reg;
1634ee342adSthorpej 	buf[2] = val;
1644ee342adSthorpej 
1654ee342adSthorpej 	return spi_send(ssc->sc_sh, 3, buf);
1664ee342adSthorpej }
1674ee342adSthorpej 
1684ee342adSthorpej static const struct mcpgpio_accessops mcpgpio_spi_accessops = {
1694ee342adSthorpej 	.lock	=	mcpgpio_spi_lock,
1704ee342adSthorpej 	.unlock	=	mcpgpio_spi_unlock,
1714ee342adSthorpej 	.read	=	mcpgpio_spi_read,
1724ee342adSthorpej 	.write	=	mcpgpio_spi_write,
1734ee342adSthorpej };
1744ee342adSthorpej 
1754ee342adSthorpej static int
mcpgpio_spi_match(device_t parent,cfdata_t cf,void * aux)1764ee342adSthorpej mcpgpio_spi_match(device_t parent, cfdata_t cf, void *aux)
1774ee342adSthorpej {
1784ee342adSthorpej 
1794ee342adSthorpej 	/* MCP23S17 has no way to detect it! */
1804ee342adSthorpej 
1814ee342adSthorpej 	return 1;
1824ee342adSthorpej }
1834ee342adSthorpej 
1844ee342adSthorpej static void
mcpgpio_spi_attach(device_t parent,device_t self,void * aux)1854ee342adSthorpej mcpgpio_spi_attach(device_t parent, device_t self, void *aux)
1864ee342adSthorpej {
1874ee342adSthorpej 	struct mcpgpio_spi_softc *ssc = device_private(self);
1884ee342adSthorpej 	struct mcpgpio_softc *sc = &ssc->sc_mcpgpio;
1894ee342adSthorpej 	struct spi_attach_args *sa = aux;
1904ee342adSthorpej 	uint32_t spi_present_mask;
1914ee342adSthorpej 	int bank, nchips, error, ha;
1924ee342adSthorpej 
1934ee342adSthorpej 	mutex_init(&ssc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
1944ee342adSthorpej 	ssc->sc_sh = sa->sa_handle;
1954ee342adSthorpej 
1964ee342adSthorpej 	sc->sc_dev = self;
1974ee342adSthorpej 	sc->sc_variant = &mcp23s17;		/* XXX */
1984ee342adSthorpej 	sc->sc_iocon = IOCON_HAEN | IOCON_SEQOP;
1994ee342adSthorpej 	sc->sc_npins = MCP23x17_GPIO_NPINS;
2004ee342adSthorpej 	sc->sc_accessops = &mcpgpio_spi_accessops;
2014ee342adSthorpej 
2024ee342adSthorpej 	aprint_naive("\n");
2034ee342adSthorpej 	aprint_normal(": %s I/O Expander\n", sc->sc_variant->name);
2044ee342adSthorpej 
20593f31c05Sthorpej 	/* run at 10MHz */
206*da38dc41Sthorpej 	error = spi_configure(self, sa->sa_handle, SPI_MODE_0, 10000000);
20793f31c05Sthorpej 	if (error) {
20893f31c05Sthorpej 		return;
20993f31c05Sthorpej 	}
21093f31c05Sthorpej 
2114ee342adSthorpej 	/*
2124ee342adSthorpej 	 * Before we decode the topology information, ensure each
2134ee342adSthorpej 	 * chip has IOCON.HAEN set so that it will actually decode
2144ee342adSthorpej 	 * the address bits.
2154ee342adSthorpej 	 *
2164ee342adSthorpej 	 * XXX Going on blind faith that IOCON.BANK is already 0.
2174ee342adSthorpej 	 */
2184ee342adSthorpej 	if (sc->sc_variant->type == MCPGPIO_TYPE_23x08) {
2194ee342adSthorpej 		error = mcpgpio_spi_write(sc, 0, REG_IOCON, sc->sc_iocon);
2204ee342adSthorpej 	} else {
2214ee342adSthorpej 		error = mcpgpio_spi_write(sc, 0, REGADDR_BANK0(0, REG_IOCON),
2224ee342adSthorpej 		    sc->sc_iocon);
2234ee342adSthorpej 		if (error == 0) {
2244ee342adSthorpej 			error = mcpgpio_spi_write(sc, 1,
2254ee342adSthorpej 			    REGADDR_BANK0(1, REG_IOCON), sc->sc_iocon);
2264ee342adSthorpej 		}
2274ee342adSthorpej 	}
2284ee342adSthorpej 	if (error) {
2294ee342adSthorpej 		aprint_error_dev(self,
2304ee342adSthorpej 		    "unable to initialize IOCON, error=%d\n", error);
2314ee342adSthorpej 		return;
2324ee342adSthorpej 	}
2334ee342adSthorpej 
2344ee342adSthorpej #if 0
2354ee342adSthorpej 	/*
2364ee342adSthorpej 	 * The number of devices sharing this chip select, along
2374ee342adSthorpej 	 * with their assigned addresses, is encoded in the
2384ee342adSthorpej 	 * "microchip,spi-present-mask" property.  Note that this
2394ee342adSthorpej 	 * device tree binding means that we will just have a
2404ee342adSthorpej 	 * single driver instance for however many chips are on
2414ee342adSthorpej 	 * this chip select.  We treat them logically as banks.
2424ee342adSthorpej 	 */
2434ee342adSthorpej 	if (of_getprop_uint32(phandle, "microchip,spi-present-mask",
2444ee342adSthorpej 			      &spi_present_mask) != 0 ||
2454ee342adSthorpej 	    of_getprop_uint32(phandle, "mcp,spi-present-mask",
2464ee342adSthorpej 			      &spi_present_mask) != 0) {
2474ee342adSthorpej 		aprint_error_dev(self,
2484ee342adSthorpej 		    "missing \"microchip,spi-present-mask\" property\n");
2494ee342adSthorpej 		return false;
2504ee342adSthorpej 	}
2514ee342adSthorpej #else
2524ee342adSthorpej 	/*
2534ee342adSthorpej 	 * XXX Until we support decoding the DT properties that
2544ee342adSthorpej 	 * XXX give us the topology information.
2554ee342adSthorpej 	 */
2564ee342adSthorpej 	spi_present_mask = __BIT(device_cfdata(self)->cf_flags & 0x7);
2574ee342adSthorpej #endif
2584ee342adSthorpej 
2594ee342adSthorpej 	/*
2604ee342adSthorpej 	 * The 23S08 has 2 address pins (4 devices per chip select),
2614ee342adSthorpej 	 * and the others have 3 (8 devices per chip select).
2624ee342adSthorpej 	 */
2634ee342adSthorpej 	if (spi_present_mask == 0 ||
2644ee342adSthorpej 	    (sc->sc_variant->type == MCPGPIO_TYPE_23x08 &&
2654ee342adSthorpej 	     spi_present_mask >= __BIT(4)) ||
2664ee342adSthorpej 	    (sc->sc_variant->type != MCPGPIO_TYPE_23x08 &&
2674ee342adSthorpej 	     spi_present_mask >= __BIT(8))) {
2684ee342adSthorpej 		aprint_error_dev(self,
2694ee342adSthorpej 		    "invalid \"microchip,spi-present-mask\" value: 0x%08x\n",
2704ee342adSthorpej 		    spi_present_mask);
2714ee342adSthorpej 		return;
2724ee342adSthorpej 	}
2734ee342adSthorpej 	nchips = popcount32(spi_present_mask);
2744ee342adSthorpej 	sc->sc_npins = nchips *
2754ee342adSthorpej 	    (sc->sc_variant->type == MCPGPIO_TYPE_23x08 ? MCP23x08_GPIO_NPINS
2764ee342adSthorpej 							: MCP23x17_GPIO_NPINS);
2774ee342adSthorpej 
2784ee342adSthorpej 	/* Record the hardware addresses for each logical bank of 8 pins. */
2794ee342adSthorpej 	for (bank = 0; spi_present_mask != 0; spi_present_mask &= ~__BIT(ha)) {
2804ee342adSthorpej 		int ha_first, ha_last;
2814ee342adSthorpej 
2824ee342adSthorpej 		ha = ffs32(spi_present_mask) - 1;
2834ee342adSthorpej 		ha_first = bank * MCPGPIO_PINS_PER_BANK;
2844ee342adSthorpej 		ssc->sc_ha[bank++] = ha;
2854ee342adSthorpej 		if (sc->sc_variant->type != MCPGPIO_TYPE_23x08) {
2864ee342adSthorpej 			ssc->sc_ha[bank++] = ha;
2874ee342adSthorpej 		}
2884ee342adSthorpej 		ha_last = (bank * MCPGPIO_PINS_PER_BANK) - 1;
2894ee342adSthorpej 		aprint_verbose_dev(self, "pins %d..%d at HA %d\n",
2904ee342adSthorpej 		    ha_first, ha_last, ha);
2914ee342adSthorpej 	}
2924ee342adSthorpej 	KASSERT((bank * MCPGPIO_PINS_PER_BANK) == sc->sc_npins);
2934ee342adSthorpej 
2944ee342adSthorpej 	mcpgpio_attach(sc);
2954ee342adSthorpej }
2964ee342adSthorpej 
2974ee342adSthorpej CFATTACH_DECL_NEW(mcpgpio_spi, sizeof(struct mcpgpio_spi_softc),
2984ee342adSthorpej     mcpgpio_spi_match, mcpgpio_spi_attach, NULL, NULL);
299