mcp23xxxgpio_i2c.c revision 1.1 1 1.1 thorpej /* $NetBSD: mcp23xxxgpio_i2c.c,v 1.1 2022/01/17 16:31:23 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 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 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.1 thorpej __KERNEL_RCSID(0, "$NetBSD: mcp23xxxgpio_i2c.c,v 1.1 2022/01/17 16:31:23 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 * MCP23008 8-bit, I2C interface
39 1.1 thorpej * MCP23017 16-bit, I2C interface
40 1.1 thorpej * MCP23018 16-bit (open-drain outputs), I2C 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/device.h>
49 1.1 thorpej #include <sys/kernel.h>
50 1.1 thorpej
51 1.1 thorpej #include <dev/ic/mcp23xxxgpioreg.h>
52 1.1 thorpej #include <dev/ic/mcp23xxxgpiovar.h>
53 1.1 thorpej
54 1.1 thorpej #include <dev/i2c/i2cvar.h>
55 1.1 thorpej
56 1.1 thorpej struct mcpgpio_i2c_softc {
57 1.1 thorpej struct mcpgpio_softc sc_mcpgpio;
58 1.1 thorpej
59 1.1 thorpej i2c_tag_t sc_i2c;
60 1.1 thorpej i2c_addr_t sc_addr;
61 1.1 thorpej };
62 1.1 thorpej
63 1.1 thorpej #define MCPGPIO_TO_I2C(sc) \
64 1.1 thorpej container_of((sc), struct mcpgpio_i2c_softc, sc_mcpgpio)
65 1.1 thorpej
66 1.1 thorpej static const struct mcpgpio_variant mcp23008 = {
67 1.1 thorpej .name = "MCP23008",
68 1.1 thorpej .type = MCPGPIO_TYPE_23x08,
69 1.1 thorpej };
70 1.1 thorpej
71 1.1 thorpej static const struct mcpgpio_variant mcp23017 = {
72 1.1 thorpej .name = "MCP23017",
73 1.1 thorpej .type = MCPGPIO_TYPE_23x17,
74 1.1 thorpej };
75 1.1 thorpej
76 1.1 thorpej static const struct mcpgpio_variant mcp23018 = {
77 1.1 thorpej .name = "MCP23018",
78 1.1 thorpej .type = MCPGPIO_TYPE_23x18,
79 1.1 thorpej };
80 1.1 thorpej
81 1.1 thorpej static const struct device_compatible_entry compat_data[] = {
82 1.1 thorpej { .compat = "microchip,mcp23008", .data = &mcp23008 },
83 1.1 thorpej { .compat = "microchip,mcp23017", .data = &mcp23017 },
84 1.1 thorpej { .compat = "microchip,mcp23018", .data = &mcp23018 },
85 1.1 thorpej DEVICE_COMPAT_EOL
86 1.1 thorpej };
87 1.1 thorpej
88 1.1 thorpej static int
89 1.1 thorpej mcpgpio_i2c_lock(struct mcpgpio_softc *sc)
90 1.1 thorpej {
91 1.1 thorpej struct mcpgpio_i2c_softc *isc = MCPGPIO_TO_I2C(sc);
92 1.1 thorpej
93 1.1 thorpej return iic_acquire_bus(isc->sc_i2c, 0);
94 1.1 thorpej }
95 1.1 thorpej
96 1.1 thorpej static void
97 1.1 thorpej mcpgpio_i2c_unlock(struct mcpgpio_softc *sc)
98 1.1 thorpej {
99 1.1 thorpej struct mcpgpio_i2c_softc *isc = MCPGPIO_TO_I2C(sc);
100 1.1 thorpej
101 1.1 thorpej iic_release_bus(isc->sc_i2c, 0);
102 1.1 thorpej }
103 1.1 thorpej
104 1.1 thorpej static int
105 1.1 thorpej mcpgpio_i2c_read(struct mcpgpio_softc *sc, unsigned int bank,
106 1.1 thorpej uint8_t reg, uint8_t *valp)
107 1.1 thorpej {
108 1.1 thorpej struct mcpgpio_i2c_softc *isc = MCPGPIO_TO_I2C(sc);
109 1.1 thorpej
110 1.1 thorpej /* Only one chip per hardware address in i2c. */
111 1.1 thorpej KASSERT((bank & ~1U) == 0);
112 1.1 thorpej
113 1.1 thorpej return iic_exec(isc->sc_i2c, I2C_OP_READ_WITH_STOP, isc->sc_addr,
114 1.1 thorpej ®, 1, valp, 1, 0);
115 1.1 thorpej }
116 1.1 thorpej
117 1.1 thorpej static int
118 1.1 thorpej mcpgpio_i2c_write(struct mcpgpio_softc *sc, unsigned int bank,
119 1.1 thorpej uint8_t reg, uint8_t val)
120 1.1 thorpej {
121 1.1 thorpej struct mcpgpio_i2c_softc *isc = MCPGPIO_TO_I2C(sc);
122 1.1 thorpej
123 1.1 thorpej /* Only one chip per hardware address in i2c. */
124 1.1 thorpej KASSERT((bank & ~1U) == 0);
125 1.1 thorpej
126 1.1 thorpej return iic_exec(isc->sc_i2c, I2C_OP_WRITE_WITH_STOP, isc->sc_addr,
127 1.1 thorpej ®, 1, &val, 1, 0);
128 1.1 thorpej }
129 1.1 thorpej
130 1.1 thorpej static const struct mcpgpio_accessops mcpgpio_i2c_accessops = {
131 1.1 thorpej .lock = mcpgpio_i2c_lock,
132 1.1 thorpej .unlock = mcpgpio_i2c_unlock,
133 1.1 thorpej .read = mcpgpio_i2c_read,
134 1.1 thorpej .write = mcpgpio_i2c_write,
135 1.1 thorpej };
136 1.1 thorpej
137 1.1 thorpej static int
138 1.1 thorpej mcpgpio_i2c_match(device_t parent, cfdata_t match, void *aux)
139 1.1 thorpej {
140 1.1 thorpej struct i2c_attach_args *ia = aux;
141 1.1 thorpej int match_result;
142 1.1 thorpej
143 1.1 thorpej if (iic_use_direct_match(ia, match, compat_data, &match_result)) {
144 1.1 thorpej return match_result;
145 1.1 thorpej }
146 1.1 thorpej return 0;
147 1.1 thorpej }
148 1.1 thorpej
149 1.1 thorpej static void
150 1.1 thorpej mcpgpio_i2c_attach(device_t parent, device_t self, void *aux)
151 1.1 thorpej {
152 1.1 thorpej struct mcpgpio_i2c_softc *isc = device_private(self);
153 1.1 thorpej struct mcpgpio_softc *sc = &isc->sc_mcpgpio;
154 1.1 thorpej struct i2c_attach_args *ia = aux;
155 1.1 thorpej const struct device_compatible_entry *dce;
156 1.1 thorpej
157 1.1 thorpej isc->sc_i2c = ia->ia_tag;
158 1.1 thorpej isc->sc_addr = ia->ia_addr;
159 1.1 thorpej
160 1.1 thorpej dce = iic_compatible_lookup(ia, compat_data);
161 1.1 thorpej KASSERT(dce != NULL);
162 1.1 thorpej
163 1.1 thorpej sc->sc_dev = self;
164 1.1 thorpej sc->sc_variant = dce->data;
165 1.1 thorpej sc->sc_iocon = IOCON_SEQOP;
166 1.1 thorpej sc->sc_phandle = ia->ia_cookie;
167 1.1 thorpej sc->sc_accessops = &mcpgpio_i2c_accessops;
168 1.1 thorpej
169 1.1 thorpej aprint_naive("\n");
170 1.1 thorpej aprint_normal(": %s I/O Expander\n", sc->sc_variant->name);
171 1.1 thorpej
172 1.1 thorpej mcpgpio_attach(sc);
173 1.1 thorpej }
174 1.1 thorpej
175 1.1 thorpej CFATTACH_DECL_NEW(mcpgpio_i2c, sizeof(struct mcpgpio_i2c_softc),
176 1.1 thorpej mcpgpio_i2c_match, mcpgpio_i2c_attach, NULL, NULL);
177