scmdspi.c revision 1.7 1 1.1 brad
2 1.7 thorpej /* $NetBSD: scmdspi.c,v 1.7 2025/09/13 14:10:44 thorpej Exp $ */
3 1.1 brad
4 1.1 brad /*
5 1.1 brad * Copyright (c) 2021 Brad Spencer <brad (at) anduin.eldar.org>
6 1.1 brad *
7 1.1 brad * Permission to use, copy, modify, and distribute this software for any
8 1.1 brad * purpose with or without fee is hereby granted, provided that the above
9 1.1 brad * copyright notice and this permission notice appear in all copies.
10 1.1 brad *
11 1.1 brad * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1 brad * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 brad * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1 brad * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 brad * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 brad * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1 brad * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 brad */
19 1.1 brad
20 1.1 brad #include <sys/cdefs.h>
21 1.7 thorpej __KERNEL_RCSID(0, "$NetBSD: scmdspi.c,v 1.7 2025/09/13 14:10:44 thorpej Exp $");
22 1.1 brad
23 1.1 brad /*
24 1.1 brad * SPI driver for the Sparkfun Serial motor controller.
25 1.1 brad * Uses the common scmd driver to do the real work.
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/scmdreg.h>
43 1.1 brad #include <dev/ic/scmdvar.h>
44 1.1 brad
45 1.6 thorpej static const struct device_compatible_entry compat_data[] = {
46 1.6 thorpej { .compat = "sparkfun,scmd-motor-driver" },
47 1.6 thorpej
48 1.6 thorpej DEVICE_COMPAT_EOL
49 1.6 thorpej };
50 1.6 thorpej
51 1.1 brad extern void scmd_attach(struct scmd_sc *);
52 1.1 brad
53 1.1 brad static int scmdspi_match(device_t, cfdata_t, void *);
54 1.1 brad static void scmdspi_attach(device_t, device_t, void *);
55 1.1 brad static int scmdspi_detach(device_t, int);
56 1.1 brad static int scmdspi_activate(device_t, enum devact);
57 1.1 brad
58 1.1 brad #define SCMD_DEBUG
59 1.1 brad #ifdef SCMD_DEBUG
60 1.1 brad #define DPRINTF(s, l, x) \
61 1.1 brad do { \
62 1.1 brad if (l <= s->sc_scmddebug) \
63 1.1 brad printf x; \
64 1.1 brad } while (/*CONSTCOND*/0)
65 1.1 brad #else
66 1.1 brad #define DPRINTF(s, l, x)
67 1.1 brad #endif
68 1.1 brad
69 1.1 brad CFATTACH_DECL_NEW(scmdspi, sizeof(struct scmd_sc),
70 1.1 brad scmdspi_match, scmdspi_attach, scmdspi_detach, scmdspi_activate);
71 1.1 brad
72 1.1 brad /* For the SPI interface on this device, the reads are done in an odd
73 1.4 gutterid * manner. The first part is normal enough, you send the register binary
74 1.1 brad * or'ed with 0x80 and then the receive the data. However, you MUST also
75 1.4 gutterid * then receive a dummy value, otherwise everything gets out of sync and
76 1.1 brad * no further reads appear to work unless you do a SPI receive all by itself.
77 1.1 brad * This is documented in the data sheet for this device.
78 1.1 brad *
79 1.1 brad * Please note that the Ardunio code does this a little differently. What is
80 1.1 brad * below works on a Raspberry PI 3 without any apparent problems.
81 1.1 brad *
82 1.1 brad * The delays are also mentioned in the datasheet as being 20us, however, the
83 1.1 brad * Ardunio code does 50us, so do likewise.
84 1.1 brad */
85 1.1 brad static int
86 1.7 thorpej scmdspi_read_reg_direct(spi_handle_t sh, uint8_t reg, uint8_t *buf)
87 1.1 brad {
88 1.1 brad int err;
89 1.1 brad uint8_t b;
90 1.1 brad uint8_t rreg = reg | 0x80;
91 1.1 brad
92 1.1 brad err = spi_send(sh, 1, &rreg);
93 1.1 brad if (err)
94 1.1 brad return err;
95 1.1 brad
96 1.1 brad delay(50);
97 1.1 brad
98 1.1 brad b = SCMD_HOLE_VALUE;
99 1.1 brad err = spi_recv(sh, 1, &b);
100 1.1 brad if (err)
101 1.1 brad return err;
102 1.1 brad
103 1.1 brad *buf = b;
104 1.1 brad
105 1.1 brad delay(50);
106 1.1 brad
107 1.1 brad b = SCMD_HOLE_VALUE;
108 1.1 brad err = spi_recv(sh, 1, &b);
109 1.1 brad delay(50);
110 1.1 brad
111 1.1 brad return err;
112 1.1 brad }
113 1.1 brad
114 1.1 brad static int
115 1.1 brad scmdspi_read_reg(struct scmd_sc *sc, uint8_t reg, uint8_t *buf)
116 1.1 brad {
117 1.1 brad return scmdspi_read_reg_direct(sc->sc_sh, reg, buf);
118 1.1 brad }
119 1.1 brad
120 1.1 brad /* SPI writes to this device are normal enough. You send the register
121 1.1 brad * you want making sure that the high bit, 0x80, is clear and then the
122 1.1 brad * data.
123 1.1 brad *
124 1.1 brad * The rule about waiting between operations appears to not apply, however.
125 1.1 brad * This does more or less what the Ardunio code does.
126 1.1 brad */
127 1.1 brad static int
128 1.7 thorpej scmdspi_write_reg_direct(spi_handle_t sh, uint8_t reg, uint8_t buf)
129 1.1 brad {
130 1.1 brad uint8_t rreg = reg & 0x7F;
131 1.1 brad int err;
132 1.1 brad
133 1.1 brad err = spi_send(sh, 1, &rreg);
134 1.1 brad if (err)
135 1.1 brad return err;
136 1.1 brad
137 1.1 brad err = spi_send(sh, 1, &buf);
138 1.1 brad if (err)
139 1.1 brad return err;
140 1.1 brad
141 1.1 brad delay(50);
142 1.1 brad
143 1.1 brad return err;
144 1.1 brad }
145 1.1 brad
146 1.1 brad static int
147 1.1 brad scmdspi_write_reg(struct scmd_sc *sc, uint8_t reg, uint8_t buf)
148 1.1 brad {
149 1.1 brad return scmdspi_write_reg_direct(sc->sc_sh, reg, buf);
150 1.1 brad }
151 1.1 brad
152 1.1 brad /* These are to satisfy the common code */
153 1.1 brad static int
154 1.1 brad scmdspi_acquire_bus(struct scmd_sc *sc)
155 1.1 brad {
156 1.1 brad return 0;
157 1.1 brad }
158 1.1 brad
159 1.1 brad static void
160 1.1 brad scmdspi_release_bus(struct scmd_sc *sc)
161 1.1 brad {
162 1.1 brad return;
163 1.1 brad }
164 1.1 brad
165 1.1 brad /* Nothing more is done here. It would be nice if the device was
166 1.1 brad * actually checked to make sure it was there, but at least on the
167 1.1 brad * Raspberry PI 3 the SPI pins were not set up in ALT0 mode yet and
168 1.1 brad * everything acts like it succeeds. No errors are ever produced while
169 1.1 brad * in that state.
170 1.1 brad */
171 1.1 brad static int
172 1.1 brad scmdspi_match(device_t parent, cfdata_t match, void *aux)
173 1.1 brad {
174 1.6 thorpej struct spi_attach_args *sa = aux;
175 1.6 thorpej int match_result;
176 1.1 brad
177 1.6 thorpej if (spi_use_direct_match(sa, compat_data, &match_result)) {
178 1.6 thorpej return match_result;
179 1.1 brad }
180 1.1 brad
181 1.6 thorpej return SPI_MATCH_DEFAULT;
182 1.1 brad }
183 1.1 brad
184 1.1 brad static void
185 1.1 brad scmdspi_attach(device_t parent, device_t self, void *aux)
186 1.1 brad {
187 1.1 brad struct scmd_sc *sc;
188 1.1 brad struct spi_attach_args *sa;
189 1.2 thorpej int error;
190 1.1 brad
191 1.1 brad sa = aux;
192 1.1 brad sc = device_private(self);
193 1.1 brad
194 1.1 brad sc->sc_dev = self;
195 1.1 brad sc->sc_sh = sa->sa_handle;
196 1.1 brad sc->sc_scmddebug = 0;
197 1.1 brad sc->sc_topaddr = 0xff;
198 1.1 brad sc->sc_opened = false;
199 1.1 brad sc->sc_dying = false;
200 1.1 brad sc->sc_func_acquire_bus = &scmdspi_acquire_bus;
201 1.1 brad sc->sc_func_release_bus = &scmdspi_release_bus;
202 1.1 brad sc->sc_func_read_register = &scmdspi_read_reg;
203 1.1 brad sc->sc_func_write_register = &scmdspi_write_reg;
204 1.1 brad
205 1.1 brad mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
206 1.1 brad mutex_init(&sc->sc_condmutex, MUTEX_DEFAULT, IPL_NONE);
207 1.1 brad mutex_init(&sc->sc_dying_mutex, MUTEX_DEFAULT, IPL_NONE);
208 1.1 brad cv_init(&sc->sc_condvar, "scmdspicv");
209 1.1 brad cv_init(&sc->sc_cond_dying, "scmdspidc");
210 1.1 brad
211 1.2 thorpej /* configure for 1MHz and SPI mode 0 according to the data sheet */
212 1.5 thorpej error = spi_configure(self, sa->sa_handle, SPI_MODE_0, SPI_FREQ_MHz(1));
213 1.2 thorpej if (error) {
214 1.2 thorpej return;
215 1.2 thorpej }
216 1.2 thorpej
217 1.1 brad /* Please note that if the pins are not set up for SPI, the attachment
218 1.1 brad * will work, but it will not figure out that there are slave modules.
219 1.1 brad * It is likely required that a re-enumeration be performed after the pins
220 1.1 brad * are set. This can be done from userland later.
221 1.1 brad */
222 1.1 brad scmd_attach(sc);
223 1.1 brad
224 1.1 brad return;
225 1.1 brad }
226 1.1 brad
227 1.1 brad /* These really do not do a whole lot, as SPI devices do not seem to work
228 1.1 brad * as modules.
229 1.1 brad */
230 1.1 brad static int
231 1.1 brad scmdspi_detach(device_t self, int flags)
232 1.1 brad {
233 1.1 brad struct scmd_sc *sc;
234 1.1 brad
235 1.1 brad sc = device_private(self);
236 1.1 brad
237 1.1 brad mutex_enter(&sc->sc_mutex);
238 1.1 brad sc->sc_dying = true;
239 1.1 brad /* If this is true we are still open, destroy the condvar */
240 1.1 brad if (sc->sc_opened) {
241 1.1 brad mutex_enter(&sc->sc_dying_mutex);
242 1.1 brad DPRINTF(sc, 2, ("%s: Will wait for anything to exit\n",
243 1.1 brad device_xname(sc->sc_dev)));
244 1.1 brad /* In the worst case this will time out after 5 seconds.
245 1.1 brad * It really should not take that long for the drain / whatever
246 1.1 brad * to happen
247 1.1 brad */
248 1.1 brad cv_timedwait_sig(&sc->sc_cond_dying,
249 1.1 brad &sc->sc_dying_mutex, mstohz(5000));
250 1.1 brad mutex_exit(&sc->sc_dying_mutex);
251 1.1 brad cv_destroy(&sc->sc_cond_dying);
252 1.1 brad }
253 1.1 brad cv_destroy(&sc->sc_condvar);
254 1.1 brad mutex_exit(&sc->sc_mutex);
255 1.1 brad
256 1.1 brad mutex_destroy(&sc->sc_mutex);
257 1.1 brad mutex_destroy(&sc->sc_condmutex);
258 1.1 brad
259 1.1 brad return 0;
260 1.1 brad }
261 1.1 brad
262 1.1 brad int
263 1.1 brad scmdspi_activate(device_t self, enum devact act)
264 1.1 brad {
265 1.1 brad struct scmd_sc *sc = device_private(self);
266 1.1 brad
267 1.1 brad switch (act) {
268 1.1 brad case DVACT_DEACTIVATE:
269 1.1 brad sc->sc_dying = true;
270 1.1 brad return 0;
271 1.1 brad default:
272 1.1 brad return EOPNOTSUPP;
273 1.1 brad }
274 1.1 brad }
275