sc16is7xxi2c.c revision 1.1 1 1.1 brad /* $NetBSD: sc16is7xxi2c.c,v 1.1 2025/10/24 23:16:11 brad Exp $ */
2 1.1 brad
3 1.1 brad /*
4 1.1 brad * Copyright (c) 2025 Brad Spencer <brad (at) anduin.eldar.org>
5 1.1 brad *
6 1.1 brad * Permission to use, copy, modify, and distribute this software for any
7 1.1 brad * purpose with or without fee is hereby granted, provided that the above
8 1.1 brad * copyright notice and this permission notice appear in all copies.
9 1.1 brad *
10 1.1 brad * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 brad * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 brad * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 brad * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 brad * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 1.1 brad * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 1.1 brad * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 brad */
18 1.1 brad
19 1.1 brad #include <sys/cdefs.h>
20 1.1 brad __KERNEL_RCSID(0, "$NetBSD: sc16is7xxi2c.c,v 1.1 2025/10/24 23:16:11 brad Exp $");
21 1.1 brad
22 1.1 brad /*
23 1.1 brad * I2C frontend driver for the SC16IS7xx UART bridge.
24 1.1 brad * The heavy lifting is done by the general sc16is7xx(4)
25 1.1 brad * driver and the com(4) backend.
26 1.1 brad */
27 1.1 brad
28 1.1 brad #include <sys/param.h>
29 1.1 brad #include <sys/systm.h>
30 1.1 brad #include <sys/kernel.h>
31 1.1 brad #include <sys/device.h>
32 1.1 brad #include <sys/module.h>
33 1.1 brad #include <sys/conf.h>
34 1.1 brad #include <sys/sysctl.h>
35 1.1 brad #include <sys/mutex.h>
36 1.1 brad #include <sys/condvar.h>
37 1.1 brad #include <sys/pool.h>
38 1.1 brad #include <sys/kmem.h>
39 1.1 brad
40 1.1 brad #include <dev/i2c/i2cvar.h>
41 1.1 brad #include <dev/spi/spivar.h>
42 1.1 brad #include <dev/ic/sc16is7xxreg.h>
43 1.1 brad #include <dev/ic/sc16is7xxvar.h>
44 1.1 brad
45 1.1 brad struct sc16is7xx_i2c_softc {
46 1.1 brad struct sc16is7xx_sc sc_sc16is7xx;
47 1.1 brad i2c_tag_t sc_tag;
48 1.1 brad i2c_addr_t sc_addr;
49 1.1 brad };
50 1.1 brad
51 1.1 brad #define SC16IS7XX_TO_I2C(sc) \
52 1.1 brad container_of((sc), struct sc16is7xx_i2c_softc, sc_sc16is7xx)
53 1.1 brad
54 1.1 brad static int sc16is7xxi2c_poke(i2c_tag_t, i2c_addr_t, bool);
55 1.1 brad static int sc16is7xxi2c_match(device_t, cfdata_t, void *);
56 1.1 brad static void sc16is7xxi2c_attach(device_t, device_t, void *);
57 1.1 brad static int sc16is7xxi2c_detach(device_t, int);
58 1.1 brad
59 1.1 brad CFATTACH_DECL_NEW(sc16is7xxi2c, sizeof(struct sc16is7xx_sc),
60 1.1 brad sc16is7xxi2c_match, sc16is7xxi2c_attach, sc16is7xxi2c_detach, NULL);
61 1.1 brad
62 1.1 brad static int
63 1.1 brad sc16is7xxi2c_read_register_direct(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg,
64 1.1 brad int channel, uint8_t *buf, size_t blen)
65 1.1 brad {
66 1.1 brad int error;
67 1.1 brad uint8_t xreg;
68 1.1 brad
69 1.1 brad xreg = (reg << 3) | (channel << 1);
70 1.1 brad
71 1.1 brad error = iic_acquire_bus(tag, 0);
72 1.1 brad if (error == 0) {
73 1.1 brad error = iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, &xreg, 1,
74 1.1 brad buf, blen, 0);
75 1.1 brad }
76 1.1 brad iic_release_bus(tag, 0);
77 1.1 brad
78 1.1 brad return error;
79 1.1 brad }
80 1.1 brad
81 1.1 brad static int
82 1.1 brad sc16is7xxi2c_write_register_direct(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg,
83 1.1 brad int channel, uint8_t *buf, size_t blen)
84 1.1 brad {
85 1.1 brad int error;
86 1.1 brad uint8_t xreg;
87 1.1 brad
88 1.1 brad xreg = (reg << 3) | (channel << 1);
89 1.1 brad
90 1.1 brad error = iic_acquire_bus(tag, 0);
91 1.1 brad if (error == 0) {
92 1.1 brad error = iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &xreg, 1,
93 1.1 brad buf, blen, 0);
94 1.1 brad }
95 1.1 brad iic_release_bus(tag, 0);
96 1.1 brad
97 1.1 brad return error;
98 1.1 brad }
99 1.1 brad /* Use these after the hand off to the general driver happens */
100 1.1 brad
101 1.1 brad static int
102 1.1 brad sc16is7xxi2c_read_register(struct sc16is7xx_sc *sc, uint8_t reg, int channel,
103 1.1 brad uint8_t *buf, size_t blen)
104 1.1 brad {
105 1.1 brad struct sc16is7xx_i2c_softc *isc = SC16IS7XX_TO_I2C(sc);
106 1.1 brad int error;
107 1.1 brad
108 1.1 brad KASSERT(blen > 0);
109 1.1 brad
110 1.1 brad error = sc16is7xxi2c_read_register_direct(isc->sc_tag, isc->sc_addr,
111 1.1 brad reg, channel, buf, blen);
112 1.1 brad
113 1.1 brad return error;
114 1.1 brad }
115 1.1 brad
116 1.1 brad static int
117 1.1 brad sc16is7xxi2c_write_register(struct sc16is7xx_sc *sc, uint8_t reg, int channel,
118 1.1 brad uint8_t *buf, size_t blen)
119 1.1 brad {
120 1.1 brad struct sc16is7xx_i2c_softc *isc = SC16IS7XX_TO_I2C(sc);
121 1.1 brad int error;
122 1.1 brad
123 1.1 brad KASSERT(blen > 0);
124 1.1 brad
125 1.1 brad error = sc16is7xxi2c_write_register_direct(isc->sc_tag, isc->sc_addr,
126 1.1 brad reg, channel, buf, blen);
127 1.1 brad
128 1.1 brad return error;
129 1.1 brad }
130 1.1 brad
131 1.1 brad static void
132 1.1 brad sc16is7xxi2c_copy_handles(struct sc16is7xx_sc *sc, struct com_regs *regs)
133 1.1 brad {
134 1.1 brad struct sc16is7xx_i2c_softc *isc = SC16IS7XX_TO_I2C(sc);
135 1.1 brad
136 1.1 brad regs->cr_tag = isc->sc_tag;
137 1.1 brad regs->cr_addr = isc->sc_addr;
138 1.1 brad }
139 1.1 brad
140 1.1 brad static const struct sc16is7xx_accessfuncs sc16is7xx_i2c_accessfuncs = {
141 1.1 brad .read_reg = sc16is7xxi2c_read_register,
142 1.1 brad .write_reg = sc16is7xxi2c_write_register,
143 1.1 brad .copy_handles = sc16is7xxi2c_copy_handles,
144 1.1 brad };
145 1.1 brad /* These will be used by dev/ic/com.c, conform to what is expected */
146 1.1 brad
147 1.1 brad static uint8_t
148 1.1 brad sc16is7xx_i2c_com_read_1(struct com_regs *regs, u_int reg)
149 1.1 brad {
150 1.1 brad uint8_t buf;
151 1.1 brad int error;
152 1.1 brad
153 1.1 brad error = sc16is7xxi2c_read_register_direct(regs->cr_tag, regs->cr_addr,
154 1.1 brad reg, regs->cr_channel, &buf, 1);
155 1.1 brad
156 1.1 brad if (!error)
157 1.1 brad return buf;
158 1.1 brad
159 1.1 brad return 0;
160 1.1 brad }
161 1.1 brad
162 1.1 brad static void
163 1.1 brad sc16is7xx_i2c_com_write_1(struct com_regs *regs, u_int reg, uint8_t val)
164 1.1 brad {
165 1.1 brad sc16is7xxi2c_write_register_direct(regs->cr_tag, regs->cr_addr,
166 1.1 brad reg, regs->cr_channel, &val, 1);
167 1.1 brad }
168 1.1 brad
169 1.1 brad static void
170 1.1 brad sc16is7xx_i2c_com_write_multi_1(struct com_regs *regs, u_int reg, const uint8_t *datap,
171 1.1 brad bus_size_t count)
172 1.1 brad {
173 1.1 brad sc16is7xxi2c_write_register_direct(regs->cr_tag, regs->cr_addr,
174 1.1 brad reg, regs->cr_channel, __UNCONST(datap), count);
175 1.1 brad }
176 1.1 brad
177 1.1 brad static const struct sc16is7xx_accessfuncs sc16is7xx_i2c_com_accessfuncs = {
178 1.1 brad .com_read_1 = sc16is7xx_i2c_com_read_1,
179 1.1 brad .com_write_1 = sc16is7xx_i2c_com_write_1,
180 1.1 brad .com_write_multi_1 = sc16is7xx_i2c_com_write_multi_1,
181 1.1 brad };
182 1.1 brad
183 1.1 brad static int
184 1.1 brad sc16is7xxi2c_poke(i2c_tag_t tag, i2c_addr_t addr, bool matchdebug)
185 1.1 brad {
186 1.1 brad uint8_t reg = SC16IS7XX_REGISTER_SPR;
187 1.1 brad uint8_t buf[1];
188 1.1 brad int error;
189 1.1 brad
190 1.1 brad error = sc16is7xxi2c_read_register_direct(tag, addr, reg, 0, buf, 1);
191 1.1 brad if (matchdebug) {
192 1.1 brad printf("poke addr=%02x, error=%d\n", addr, error);
193 1.1 brad }
194 1.1 brad return error;
195 1.1 brad }
196 1.1 brad
197 1.1 brad static int
198 1.1 brad sc16is7xxi2c_match(device_t parent, cfdata_t cf, void *aux)
199 1.1 brad {
200 1.1 brad struct i2c_attach_args *ia = aux;
201 1.1 brad int error, match_result;
202 1.1 brad const bool matchdebug = false;
203 1.1 brad bool indirect_found = false;
204 1.1 brad int i;
205 1.1 brad
206 1.1 brad if (iic_use_direct_match(ia, cf, sc16is7xx_compat_data, &match_result)) {
207 1.1 brad return match_result;
208 1.1 brad }
209 1.1 brad for (i = SC16IS7XX_LOW_I2C_ADDR; i <= SC16IS7XX_HIGH_I2C_ADDR && indirect_found == false; i++) {
210 1.1 brad if (ia->ia_addr == i) {
211 1.1 brad if (matchdebug) {
212 1.1 brad printf("sc16is7xxi2c_match possible indirect: ia_addr=%02x, i=%02x\n", ia->ia_addr, i);
213 1.1 brad }
214 1.1 brad indirect_found = true;
215 1.1 brad }
216 1.1 brad }
217 1.1 brad
218 1.1 brad if (!indirect_found)
219 1.1 brad return 0;
220 1.1 brad
221 1.1 brad /* Check to see if something is really at this i2c address. This will
222 1.1 brad * keep phantom devices from appearing */
223 1.1 brad
224 1.1 brad error = sc16is7xxi2c_poke(ia->ia_tag, ia->ia_addr, matchdebug);
225 1.1 brad
226 1.1 brad return error == 0 ? I2C_MATCH_ADDRESS_AND_PROBE : 0;
227 1.1 brad }
228 1.1 brad
229 1.1 brad static void
230 1.1 brad sc16is7xxi2c_attach(device_t parent, device_t self, void *aux)
231 1.1 brad {
232 1.1 brad struct sc16is7xx_i2c_softc *isc = device_private(self);
233 1.1 brad struct sc16is7xx_sc *sc = &isc->sc_sc16is7xx;
234 1.1 brad struct i2c_attach_args *ia = aux;
235 1.1 brad
236 1.1 brad sc->sc_dev = self;
237 1.1 brad sc->sc_funcs = &sc16is7xx_i2c_accessfuncs;
238 1.1 brad sc->sc_com_funcs = &sc16is7xx_i2c_com_accessfuncs;
239 1.1 brad
240 1.1 brad isc->sc_tag = ia->ia_tag;
241 1.1 brad isc->sc_addr = ia->ia_addr;
242 1.1 brad
243 1.1 brad sc16is7xx_attach(sc);
244 1.1 brad }
245 1.1 brad
246 1.1 brad static int
247 1.1 brad sc16is7xxi2c_detach(device_t self, int flags)
248 1.1 brad {
249 1.1 brad struct sc16is7xx_i2c_softc *isc = device_private(self);
250 1.1 brad
251 1.1 brad return sc16is7xx_detach(&isc->sc_sc16is7xx, flags);
252 1.1 brad }
253