mcp23xxxgpio_spi.c revision 1.3 1 1.3 thorpej /* $NetBSD: mcp23xxxgpio_spi.c,v 1.3 2022/01/19 05:05:45 thorpej 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 thorpej __KERNEL_RCSID(0, "$NetBSD: mcp23xxxgpio_spi.c,v 1.3 2022/01/19 05:05:45 thorpej Exp $");
34 1.1 thorpej
35 1.1 thorpej /*
36 1.1 thorpej * Driver for Microchip serial I/O expanders:
37 1.1 thorpej *
38 1.1 thorpej * MCP23S08 8-bit, SPI interface
39 1.1 thorpej * MCP23S17 16-bit, SPI interface
40 1.1 thorpej * MCP23S18 16-bit (open-drain outputs), SPI interface
41 1.1 thorpej *
42 1.1 thorpej * Data sheet:
43 1.1 thorpej *
44 1.1 thorpej * https://ww1.microchip.com/downloads/en/DeviceDoc/20001952C.pdf
45 1.1 thorpej */
46 1.1 thorpej
47 1.1 thorpej #include <sys/types.h>
48 1.1 thorpej #include <sys/bitops.h>
49 1.1 thorpej #include <sys/device.h>
50 1.1 thorpej #include <sys/kernel.h>
51 1.1 thorpej #include <sys/mutex.h>
52 1.1 thorpej
53 1.1 thorpej #include <dev/ic/mcp23xxxgpioreg.h>
54 1.1 thorpej #include <dev/ic/mcp23xxxgpiovar.h>
55 1.1 thorpej
56 1.1 thorpej #include <dev/spi/spivar.h>
57 1.1 thorpej
58 1.1 thorpej /*
59 1.1 thorpej * Multi-chip-on-select configurations appear to the upper layers like
60 1.1 thorpej * additional GPIO banks; mixing different chip types on the same chip
61 1.1 thorpej * select is not allowed.
62 1.1 thorpej *
63 1.1 thorpej * Some chips have 2 banks per chip, and we have up to 8 chips per chip
64 1.1 thorpej * select, it's a total of 16 banks per chip select / driver instance.
65 1.1 thorpej */
66 1.1 thorpej #define MCPGPIO_SPI_MAXBANKS 16
67 1.1 thorpej
68 1.1 thorpej struct mcpgpio_spi_softc {
69 1.1 thorpej struct mcpgpio_softc sc_mcpgpio;
70 1.1 thorpej
71 1.1 thorpej kmutex_t sc_mutex;
72 1.1 thorpej struct spi_handle *sc_sh;
73 1.1 thorpej uint8_t sc_ha[MCPGPIO_SPI_MAXBANKS];
74 1.1 thorpej };
75 1.1 thorpej
76 1.1 thorpej /*
77 1.1 thorpej * SPI-specific commands (the serial interface on the I2C flavor of
78 1.1 thorpej * the chip uses the I2C protocol to infer this information). Careful
79 1.1 thorpej * readers will note that this ends up being exactly the same bits
80 1.1 thorpej * on the serial interface that the I2C flavor of the chip uses.
81 1.1 thorpej *
82 1.1 thorpej * The SPI version can have up to 4 (or 8) chips per chip-select, demuxed
83 1.1 thorpej * using the hardware address (selected by tying the 2 or 3 HA pins high/low
84 1.1 thorpej * as desired).
85 1.1 thorpej */
86 1.1 thorpej #define OP_READ(ha) (0x41 | ((ha) << 1))
87 1.1 thorpej #define OP_WRITE(ha) (0x40 | ((ha) << 1))
88 1.1 thorpej
89 1.1 thorpej #define MCPGPIO_TO_SPI(sc) \
90 1.1 thorpej container_of((sc), struct mcpgpio_spi_softc, sc_mcpgpio)
91 1.1 thorpej
92 1.1 thorpej #if 0
93 1.1 thorpej static const struct mcpgpio_variant mcp23s08 = {
94 1.1 thorpej .name = "MCP23S08",
95 1.1 thorpej .type = MCPGPIO_TYPE_23x08,
96 1.1 thorpej };
97 1.1 thorpej #endif
98 1.1 thorpej
99 1.1 thorpej static const struct mcpgpio_variant mcp23s17 = {
100 1.1 thorpej .name = "MCP23S17",
101 1.1 thorpej .type = MCPGPIO_TYPE_23x17,
102 1.1 thorpej };
103 1.1 thorpej
104 1.1 thorpej #if 0
105 1.1 thorpej static const struct mcpgpio_variant mcp23s18 = {
106 1.1 thorpej .name = "MCP23S18",
107 1.1 thorpej .type = MCPGPIO_TYPE_23x18,
108 1.1 thorpej };
109 1.1 thorpej #endif
110 1.1 thorpej
111 1.1 thorpej #if 0
112 1.1 thorpej static const struct device_compatible_entry compat_data[] = {
113 1.1 thorpej { .compat = "microchip,mcp23s08", .data = &mcp23s08 },
114 1.1 thorpej { .compat = "microchip,mcp23s17", .data = &mcp23s17 },
115 1.1 thorpej { .compat = "microchip,mcp23s18", .data = &mcp23s18 },
116 1.1 thorpej DEVICE_COMPAT_EOL
117 1.1 thorpej };
118 1.1 thorpej #endif
119 1.1 thorpej
120 1.1 thorpej static int
121 1.1 thorpej mcpgpio_spi_lock(struct mcpgpio_softc *sc)
122 1.1 thorpej {
123 1.1 thorpej struct mcpgpio_spi_softc *ssc = MCPGPIO_TO_SPI(sc);
124 1.1 thorpej
125 1.1 thorpej mutex_enter(&ssc->sc_mutex);
126 1.1 thorpej return 0;
127 1.1 thorpej }
128 1.1 thorpej
129 1.1 thorpej static void
130 1.1 thorpej mcpgpio_spi_unlock(struct mcpgpio_softc *sc)
131 1.1 thorpej {
132 1.1 thorpej struct mcpgpio_spi_softc *ssc = MCPGPIO_TO_SPI(sc);
133 1.1 thorpej
134 1.1 thorpej mutex_exit(&ssc->sc_mutex);
135 1.1 thorpej }
136 1.1 thorpej
137 1.1 thorpej static int
138 1.1 thorpej mcpgpio_spi_read(struct mcpgpio_softc *sc, unsigned int bank,
139 1.1 thorpej uint8_t reg, uint8_t *valp)
140 1.1 thorpej {
141 1.1 thorpej struct mcpgpio_spi_softc *ssc = MCPGPIO_TO_SPI(sc);
142 1.1 thorpej uint8_t buf[2];
143 1.1 thorpej
144 1.1 thorpej KASSERT(bank < (sc->sc_npins >> 3));
145 1.1 thorpej
146 1.1 thorpej buf[0] = OP_READ(ssc->sc_ha[bank]);
147 1.1 thorpej buf[1] = reg;
148 1.1 thorpej
149 1.1 thorpej return spi_send_recv(ssc->sc_sh, 2, buf, 1, valp);
150 1.1 thorpej }
151 1.1 thorpej
152 1.1 thorpej static int
153 1.1 thorpej mcpgpio_spi_write(struct mcpgpio_softc *sc, unsigned int bank,
154 1.1 thorpej uint8_t reg, uint8_t val)
155 1.1 thorpej {
156 1.1 thorpej struct mcpgpio_spi_softc *ssc = MCPGPIO_TO_SPI(sc);
157 1.1 thorpej uint8_t buf[3];
158 1.1 thorpej
159 1.1 thorpej KASSERT(bank < (sc->sc_npins >> 3));
160 1.1 thorpej
161 1.1 thorpej buf[0] = OP_WRITE(ssc->sc_ha[bank]);
162 1.1 thorpej buf[1] = reg;
163 1.1 thorpej buf[2] = val;
164 1.1 thorpej
165 1.1 thorpej return spi_send(ssc->sc_sh, 3, buf);
166 1.1 thorpej }
167 1.1 thorpej
168 1.1 thorpej static const struct mcpgpio_accessops mcpgpio_spi_accessops = {
169 1.1 thorpej .lock = mcpgpio_spi_lock,
170 1.1 thorpej .unlock = mcpgpio_spi_unlock,
171 1.1 thorpej .read = mcpgpio_spi_read,
172 1.1 thorpej .write = mcpgpio_spi_write,
173 1.1 thorpej };
174 1.1 thorpej
175 1.1 thorpej static int
176 1.1 thorpej mcpgpio_spi_match(device_t parent, cfdata_t cf, void *aux)
177 1.1 thorpej {
178 1.1 thorpej
179 1.1 thorpej /* MCP23S17 has no way to detect it! */
180 1.1 thorpej
181 1.1 thorpej return 1;
182 1.1 thorpej }
183 1.1 thorpej
184 1.1 thorpej static void
185 1.1 thorpej mcpgpio_spi_attach(device_t parent, device_t self, void *aux)
186 1.1 thorpej {
187 1.1 thorpej struct mcpgpio_spi_softc *ssc = device_private(self);
188 1.1 thorpej struct mcpgpio_softc *sc = &ssc->sc_mcpgpio;
189 1.1 thorpej struct spi_attach_args *sa = aux;
190 1.1 thorpej uint32_t spi_present_mask;
191 1.1 thorpej int bank, nchips, error, ha;
192 1.1 thorpej
193 1.1 thorpej mutex_init(&ssc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
194 1.1 thorpej ssc->sc_sh = sa->sa_handle;
195 1.1 thorpej
196 1.1 thorpej sc->sc_dev = self;
197 1.1 thorpej sc->sc_variant = &mcp23s17; /* XXX */
198 1.1 thorpej sc->sc_iocon = IOCON_HAEN | IOCON_SEQOP;
199 1.1 thorpej sc->sc_npins = MCP23x17_GPIO_NPINS;
200 1.1 thorpej sc->sc_accessops = &mcpgpio_spi_accessops;
201 1.1 thorpej
202 1.1 thorpej aprint_naive("\n");
203 1.1 thorpej aprint_normal(": %s I/O Expander\n", sc->sc_variant->name);
204 1.1 thorpej
205 1.3 thorpej /* run at 10MHz */
206 1.3 thorpej error = spi_configure(sa->sa_handle, SPI_MODE_0, 10000000);
207 1.3 thorpej if (error) {
208 1.3 thorpej aprint_error_dev(self,
209 1.3 thorpej "failed to set Mode 0 @ 10MHz, error=%d\n", error);
210 1.3 thorpej return;
211 1.3 thorpej }
212 1.3 thorpej
213 1.1 thorpej /*
214 1.1 thorpej * Before we decode the topology information, ensure each
215 1.1 thorpej * chip has IOCON.HAEN set so that it will actually decode
216 1.1 thorpej * the address bits.
217 1.1 thorpej *
218 1.1 thorpej * XXX Going on blind faith that IOCON.BANK is already 0.
219 1.1 thorpej */
220 1.1 thorpej if (sc->sc_variant->type == MCPGPIO_TYPE_23x08) {
221 1.1 thorpej error = mcpgpio_spi_write(sc, 0, REG_IOCON, sc->sc_iocon);
222 1.1 thorpej } else {
223 1.1 thorpej error = mcpgpio_spi_write(sc, 0, REGADDR_BANK0(0, REG_IOCON),
224 1.1 thorpej sc->sc_iocon);
225 1.1 thorpej if (error == 0) {
226 1.1 thorpej error = mcpgpio_spi_write(sc, 1,
227 1.1 thorpej REGADDR_BANK0(1, REG_IOCON), sc->sc_iocon);
228 1.1 thorpej }
229 1.1 thorpej }
230 1.1 thorpej if (error) {
231 1.1 thorpej aprint_error_dev(self,
232 1.1 thorpej "unable to initialize IOCON, error=%d\n", error);
233 1.1 thorpej return;
234 1.1 thorpej }
235 1.1 thorpej
236 1.1 thorpej #if 0
237 1.1 thorpej /*
238 1.1 thorpej * The number of devices sharing this chip select, along
239 1.1 thorpej * with their assigned addresses, is encoded in the
240 1.1 thorpej * "microchip,spi-present-mask" property. Note that this
241 1.1 thorpej * device tree binding means that we will just have a
242 1.1 thorpej * single driver instance for however many chips are on
243 1.1 thorpej * this chip select. We treat them logically as banks.
244 1.1 thorpej */
245 1.1 thorpej if (of_getprop_uint32(phandle, "microchip,spi-present-mask",
246 1.1 thorpej &spi_present_mask) != 0 ||
247 1.1 thorpej of_getprop_uint32(phandle, "mcp,spi-present-mask",
248 1.1 thorpej &spi_present_mask) != 0) {
249 1.1 thorpej aprint_error_dev(self,
250 1.1 thorpej "missing \"microchip,spi-present-mask\" property\n");
251 1.1 thorpej return false;
252 1.1 thorpej }
253 1.1 thorpej #else
254 1.1 thorpej /*
255 1.1 thorpej * XXX Until we support decoding the DT properties that
256 1.1 thorpej * XXX give us the topology information.
257 1.1 thorpej */
258 1.1 thorpej spi_present_mask = __BIT(device_cfdata(self)->cf_flags & 0x7);
259 1.1 thorpej #endif
260 1.1 thorpej
261 1.1 thorpej /*
262 1.1 thorpej * The 23S08 has 2 address pins (4 devices per chip select),
263 1.1 thorpej * and the others have 3 (8 devices per chip select).
264 1.1 thorpej */
265 1.1 thorpej if (spi_present_mask == 0 ||
266 1.1 thorpej (sc->sc_variant->type == MCPGPIO_TYPE_23x08 &&
267 1.1 thorpej spi_present_mask >= __BIT(4)) ||
268 1.1 thorpej (sc->sc_variant->type != MCPGPIO_TYPE_23x08 &&
269 1.1 thorpej spi_present_mask >= __BIT(8))) {
270 1.1 thorpej aprint_error_dev(self,
271 1.1 thorpej "invalid \"microchip,spi-present-mask\" value: 0x%08x\n",
272 1.1 thorpej spi_present_mask);
273 1.1 thorpej return;
274 1.1 thorpej }
275 1.1 thorpej nchips = popcount32(spi_present_mask);
276 1.1 thorpej sc->sc_npins = nchips *
277 1.1 thorpej (sc->sc_variant->type == MCPGPIO_TYPE_23x08 ? MCP23x08_GPIO_NPINS
278 1.1 thorpej : MCP23x17_GPIO_NPINS);
279 1.1 thorpej
280 1.1 thorpej /* Record the hardware addresses for each logical bank of 8 pins. */
281 1.1 thorpej for (bank = 0; spi_present_mask != 0; spi_present_mask &= ~__BIT(ha)) {
282 1.1 thorpej int ha_first, ha_last;
283 1.1 thorpej
284 1.1 thorpej ha = ffs32(spi_present_mask) - 1;
285 1.1 thorpej ha_first = bank * MCPGPIO_PINS_PER_BANK;
286 1.1 thorpej ssc->sc_ha[bank++] = ha;
287 1.1 thorpej if (sc->sc_variant->type != MCPGPIO_TYPE_23x08) {
288 1.1 thorpej ssc->sc_ha[bank++] = ha;
289 1.1 thorpej }
290 1.1 thorpej ha_last = (bank * MCPGPIO_PINS_PER_BANK) - 1;
291 1.1 thorpej aprint_verbose_dev(self, "pins %d..%d at HA %d\n",
292 1.1 thorpej ha_first, ha_last, ha);
293 1.1 thorpej }
294 1.1 thorpej KASSERT((bank * MCPGPIO_PINS_PER_BANK) == sc->sc_npins);
295 1.1 thorpej
296 1.1 thorpej mcpgpio_attach(sc);
297 1.1 thorpej }
298 1.1 thorpej
299 1.1 thorpej CFATTACH_DECL_NEW(mcpgpio_spi, sizeof(struct mcpgpio_spi_softc),
300 1.1 thorpej mcpgpio_spi_match, mcpgpio_spi_attach, NULL, NULL);
301