scmdspi.c revision 1.3 1 1.1 brad
2 1.3 thorpej /* $NetBSD: scmdspi.c,v 1.3 2022/01/19 05:21: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.3 thorpej __KERNEL_RCSID(0, "$NetBSD: scmdspi.c,v 1.3 2022/01/19 05:21: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.1 brad extern void scmd_attach(struct scmd_sc *);
46 1.1 brad
47 1.1 brad static int scmdspi_match(device_t, cfdata_t, void *);
48 1.1 brad static void scmdspi_attach(device_t, device_t, void *);
49 1.1 brad static int scmdspi_detach(device_t, int);
50 1.1 brad static int scmdspi_activate(device_t, enum devact);
51 1.1 brad
52 1.1 brad #define SCMD_DEBUG
53 1.1 brad #ifdef SCMD_DEBUG
54 1.1 brad #define DPRINTF(s, l, x) \
55 1.1 brad do { \
56 1.1 brad if (l <= s->sc_scmddebug) \
57 1.1 brad printf x; \
58 1.1 brad } while (/*CONSTCOND*/0)
59 1.1 brad #else
60 1.1 brad #define DPRINTF(s, l, x)
61 1.1 brad #endif
62 1.1 brad
63 1.1 brad CFATTACH_DECL_NEW(scmdspi, sizeof(struct scmd_sc),
64 1.1 brad scmdspi_match, scmdspi_attach, scmdspi_detach, scmdspi_activate);
65 1.1 brad
66 1.1 brad /* For the SPI interface on this device, the reads are done in an odd
67 1.1 brad * manor. The first part is normal enough, you send the register binary
68 1.1 brad * or'ed with 0x80 and then the receive the data. However, you MUST also
69 1.1 brad * then receive a dummy value otherwise, everything gets out of sync and
70 1.1 brad * no further reads appear to work unless you do a SPI receive all by itself.
71 1.1 brad * This is documented in the data sheet for this device.
72 1.1 brad *
73 1.1 brad * Please note that the Ardunio code does this a little differently. What is
74 1.1 brad * below works on a Raspberry PI 3 without any apparent problems.
75 1.1 brad *
76 1.1 brad * The delays are also mentioned in the datasheet as being 20us, however, the
77 1.1 brad * Ardunio code does 50us, so do likewise.
78 1.1 brad */
79 1.1 brad static int
80 1.1 brad scmdspi_read_reg_direct(struct spi_handle *sh, uint8_t reg,
81 1.1 brad uint8_t *buf)
82 1.1 brad {
83 1.1 brad int err;
84 1.1 brad uint8_t b;
85 1.1 brad uint8_t rreg = reg | 0x80;
86 1.1 brad
87 1.1 brad err = spi_send(sh, 1, &rreg);
88 1.1 brad if (err)
89 1.1 brad return err;
90 1.1 brad
91 1.1 brad delay(50);
92 1.1 brad
93 1.1 brad b = SCMD_HOLE_VALUE;
94 1.1 brad err = spi_recv(sh, 1, &b);
95 1.1 brad if (err)
96 1.1 brad return err;
97 1.1 brad
98 1.1 brad *buf = b;
99 1.1 brad
100 1.1 brad delay(50);
101 1.1 brad
102 1.1 brad b = SCMD_HOLE_VALUE;
103 1.1 brad err = spi_recv(sh, 1, &b);
104 1.1 brad delay(50);
105 1.1 brad
106 1.1 brad return err;
107 1.1 brad }
108 1.1 brad
109 1.1 brad static int
110 1.1 brad scmdspi_read_reg(struct scmd_sc *sc, uint8_t reg, uint8_t *buf)
111 1.1 brad {
112 1.1 brad return scmdspi_read_reg_direct(sc->sc_sh, reg, buf);
113 1.1 brad }
114 1.1 brad
115 1.1 brad /* SPI writes to this device are normal enough. You send the register
116 1.1 brad * you want making sure that the high bit, 0x80, is clear and then the
117 1.1 brad * data.
118 1.1 brad *
119 1.1 brad * The rule about waiting between operations appears to not apply, however.
120 1.1 brad * This does more or less what the Ardunio code does.
121 1.1 brad */
122 1.1 brad static int
123 1.1 brad scmdspi_write_reg_direct(struct spi_handle *sh, uint8_t reg,
124 1.1 brad uint8_t buf)
125 1.1 brad {
126 1.1 brad uint8_t rreg = reg & 0x7F;
127 1.1 brad int err;
128 1.1 brad
129 1.1 brad err = spi_send(sh, 1, &rreg);
130 1.1 brad if (err)
131 1.1 brad return err;
132 1.1 brad
133 1.1 brad err = spi_send(sh, 1, &buf);
134 1.1 brad if (err)
135 1.1 brad return err;
136 1.1 brad
137 1.1 brad delay(50);
138 1.1 brad
139 1.1 brad return err;
140 1.1 brad }
141 1.1 brad
142 1.1 brad static int
143 1.1 brad scmdspi_write_reg(struct scmd_sc *sc, uint8_t reg, uint8_t buf)
144 1.1 brad {
145 1.1 brad return scmdspi_write_reg_direct(sc->sc_sh, reg, buf);
146 1.1 brad }
147 1.1 brad
148 1.1 brad /* These are to satisfy the common code */
149 1.1 brad static int
150 1.1 brad scmdspi_acquire_bus(struct scmd_sc *sc)
151 1.1 brad {
152 1.1 brad return 0;
153 1.1 brad }
154 1.1 brad
155 1.1 brad static void
156 1.1 brad scmdspi_release_bus(struct scmd_sc *sc)
157 1.1 brad {
158 1.1 brad return;
159 1.1 brad }
160 1.1 brad
161 1.1 brad /* Nothing more is done here. It would be nice if the device was
162 1.1 brad * actually checked to make sure it was there, but at least on the
163 1.1 brad * Raspberry PI 3 the SPI pins were not set up in ALT0 mode yet and
164 1.1 brad * everything acts like it succeeds. No errors are ever produced while
165 1.1 brad * in that state.
166 1.1 brad */
167 1.1 brad static int
168 1.1 brad scmdspi_match(device_t parent, cfdata_t match, void *aux)
169 1.1 brad {
170 1.1 brad struct spi_attach_args *sa = aux;
171 1.1 brad const bool matchdebug = true;
172 1.1 brad
173 1.1 brad if (matchdebug) {
174 1.1 brad printf("Trying to match\n");
175 1.1 brad }
176 1.1 brad
177 1.1 brad return 1;
178 1.1 brad }
179 1.1 brad
180 1.1 brad static void
181 1.1 brad scmdspi_attach(device_t parent, device_t self, void *aux)
182 1.1 brad {
183 1.1 brad struct scmd_sc *sc;
184 1.1 brad struct spi_attach_args *sa;
185 1.2 thorpej int error;
186 1.1 brad
187 1.1 brad sa = aux;
188 1.1 brad sc = device_private(self);
189 1.1 brad
190 1.1 brad sc->sc_dev = self;
191 1.1 brad sc->sc_sh = sa->sa_handle;
192 1.1 brad sc->sc_scmddebug = 0;
193 1.1 brad sc->sc_topaddr = 0xff;
194 1.1 brad sc->sc_opened = false;
195 1.1 brad sc->sc_dying = false;
196 1.1 brad sc->sc_func_acquire_bus = &scmdspi_acquire_bus;
197 1.1 brad sc->sc_func_release_bus = &scmdspi_release_bus;
198 1.1 brad sc->sc_func_read_register = &scmdspi_read_reg;
199 1.1 brad sc->sc_func_write_register = &scmdspi_write_reg;
200 1.1 brad
201 1.1 brad mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
202 1.1 brad mutex_init(&sc->sc_condmutex, MUTEX_DEFAULT, IPL_NONE);
203 1.1 brad mutex_init(&sc->sc_dying_mutex, MUTEX_DEFAULT, IPL_NONE);
204 1.1 brad cv_init(&sc->sc_condvar, "scmdspicv");
205 1.1 brad cv_init(&sc->sc_cond_dying, "scmdspidc");
206 1.1 brad
207 1.2 thorpej /* configure for 1MHz and SPI mode 0 according to the data sheet */
208 1.3 thorpej error = spi_configure(self, sa->sa_handle, SPI_MODE_0, 1000000);
209 1.2 thorpej if (error) {
210 1.2 thorpej return;
211 1.2 thorpej }
212 1.2 thorpej
213 1.1 brad /* Please note that if the pins are not set up for SPI, the attachment
214 1.1 brad * will work, but it will not figure out that there are slave modules.
215 1.1 brad * It is likely required that a re-enumeration be performed after the pins
216 1.1 brad * are set. This can be done from userland later.
217 1.1 brad */
218 1.1 brad scmd_attach(sc);
219 1.1 brad
220 1.1 brad return;
221 1.1 brad }
222 1.1 brad
223 1.1 brad /* These really do not do a whole lot, as SPI devices do not seem to work
224 1.1 brad * as modules.
225 1.1 brad */
226 1.1 brad static int
227 1.1 brad scmdspi_detach(device_t self, int flags)
228 1.1 brad {
229 1.1 brad struct scmd_sc *sc;
230 1.1 brad
231 1.1 brad sc = device_private(self);
232 1.1 brad
233 1.1 brad mutex_enter(&sc->sc_mutex);
234 1.1 brad sc->sc_dying = true;
235 1.1 brad /* If this is true we are still open, destroy the condvar */
236 1.1 brad if (sc->sc_opened) {
237 1.1 brad mutex_enter(&sc->sc_dying_mutex);
238 1.1 brad DPRINTF(sc, 2, ("%s: Will wait for anything to exit\n",
239 1.1 brad device_xname(sc->sc_dev)));
240 1.1 brad /* In the worst case this will time out after 5 seconds.
241 1.1 brad * It really should not take that long for the drain / whatever
242 1.1 brad * to happen
243 1.1 brad */
244 1.1 brad cv_timedwait_sig(&sc->sc_cond_dying,
245 1.1 brad &sc->sc_dying_mutex, mstohz(5000));
246 1.1 brad mutex_exit(&sc->sc_dying_mutex);
247 1.1 brad cv_destroy(&sc->sc_cond_dying);
248 1.1 brad }
249 1.1 brad cv_destroy(&sc->sc_condvar);
250 1.1 brad mutex_exit(&sc->sc_mutex);
251 1.1 brad
252 1.1 brad mutex_destroy(&sc->sc_mutex);
253 1.1 brad mutex_destroy(&sc->sc_condmutex);
254 1.1 brad
255 1.1 brad return 0;
256 1.1 brad }
257 1.1 brad
258 1.1 brad int
259 1.1 brad scmdspi_activate(device_t self, enum devact act)
260 1.1 brad {
261 1.1 brad struct scmd_sc *sc = device_private(self);
262 1.1 brad
263 1.1 brad switch (act) {
264 1.1 brad case DVACT_DEACTIVATE:
265 1.1 brad sc->sc_dying = true;
266 1.1 brad return 0;
267 1.1 brad default:
268 1.1 brad return EOPNOTSUPP;
269 1.1 brad }
270 1.1 brad }
271