umcpmio.c revision 1.2 1 1.2 riastrad /* $NetBSD: umcpmio.c,v 1.2 2025/03/17 18:24:08 riastradh Exp $ */
2 1.1 brad
3 1.1 brad /*
4 1.1 brad * Copyright (c) 2024 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.2 riastrad __KERNEL_RCSID(0, "$NetBSD: umcpmio.c,v 1.2 2025/03/17 18:24:08 riastradh Exp $");
21 1.1 brad
22 1.1 brad /*
23 1.2 riastrad * Driver for the Microchip MCP2221 / MCP2221A USB multi-io chip
24 1.2 riastrad */
25 1.1 brad
26 1.1 brad #ifdef _KERNEL_OPT
27 1.1 brad #include "opt_usb.h"
28 1.1 brad #endif
29 1.1 brad
30 1.1 brad #include <sys/param.h>
31 1.2 riastrad #include <sys/types.h>
32 1.2 riastrad
33 1.1 brad #include <sys/conf.h>
34 1.2 riastrad #include <sys/device.h>
35 1.2 riastrad #include <sys/file.h>
36 1.2 riastrad #include <sys/gpio.h>
37 1.2 riastrad #include <sys/kauth.h>
38 1.1 brad #include <sys/kernel.h>
39 1.1 brad #include <sys/kmem.h>
40 1.2 riastrad #include <sys/lwp.h>
41 1.1 brad #include <sys/sysctl.h>
42 1.2 riastrad #include <sys/systm.h>
43 1.1 brad #include <sys/tty.h>
44 1.1 brad #include <sys/vnode.h>
45 1.1 brad
46 1.2 riastrad #include <dev/i2c/i2cvar.h>
47 1.2 riastrad
48 1.1 brad #include <dev/gpio/gpiovar.h>
49 1.1 brad
50 1.2 riastrad #include <dev/hid/hid.h>
51 1.1 brad
52 1.2 riastrad #include <dev/usb/uhidev.h>
53 1.1 brad #include <dev/usb/usb.h>
54 1.2 riastrad #include <dev/usb/usbdevs.h>
55 1.1 brad #include <dev/usb/usbdi.h>
56 1.1 brad #include <dev/usb/usbdi_util.h>
57 1.2 riastrad #include <dev/usb/usbhid.h>
58 1.1 brad
59 1.1 brad #include <dev/usb/umcpmio.h>
60 1.1 brad #include <dev/usb/umcpmio_hid_reports.h>
61 1.1 brad #include <dev/usb/umcpmio_io.h>
62 1.2 riastrad #include <dev/usb/umcpmio_subr.h>
63 1.1 brad
64 1.2 riastrad int umcpmio_send_report(struct umcpmio_softc *, uint8_t *, size_t, uint8_t *,
65 1.2 riastrad int);
66 1.1 brad
67 1.1 brad static const struct usb_devno umcpmio_devs[] = {
68 1.1 brad { USB_VENDOR_MICROCHIP, USB_PRODUCT_MICROCHIP_MCP2221 },
69 1.1 brad };
70 1.1 brad #define umcpmio_lookup(v, p) usb_lookup(umcpmio_devs, v, p)
71 1.1 brad
72 1.1 brad static int umcpmio_match(device_t, cfdata_t, void *);
73 1.1 brad static void umcpmio_attach(device_t, device_t, void *);
74 1.1 brad static int umcpmio_detach(device_t, int);
75 1.1 brad static int umcpmio_activate(device_t, enum devact);
76 1.1 brad static int umcpmio_verify_sysctl(SYSCTLFN_ARGS);
77 1.1 brad static int umcpmio_verify_dac_sysctl(SYSCTLFN_ARGS);
78 1.1 brad static int umcpmio_verify_adc_sysctl(SYSCTLFN_ARGS);
79 1.1 brad static int umcpmio_verify_gpioclock_dc_sysctl(SYSCTLFN_ARGS);
80 1.1 brad static int umcpmio_verify_gpioclock_cd_sysctl(SYSCTLFN_ARGS);
81 1.1 brad
82 1.1 brad #define UMCPMIO_DEBUG 1
83 1.1 brad #ifdef UMCPMIO_DEBUG
84 1.2 riastrad #define DPRINTF(x) do { if (umcpmiodebug) printf x; } while (0)
85 1.2 riastrad #define DPRINTFN(n, x) do { if (umcpmiodebug > (n)) printf x; } while (0)
86 1.1 brad int umcpmiodebug = 0;
87 1.1 brad #else
88 1.1 brad #define DPRINTF(x) __nothing
89 1.2 riastrad #define DPRINTFN(n, x) __nothing
90 1.1 brad #endif
91 1.1 brad
92 1.1 brad CFATTACH_DECL_NEW(umcpmio, sizeof(struct umcpmio_softc), umcpmio_match,
93 1.1 brad umcpmio_attach, umcpmio_detach, umcpmio_activate);
94 1.1 brad
95 1.2 riastrad static void
96 1.2 riastrad WAITMS(int ms)
97 1.2 riastrad {
98 1.2 riastrad if (ms > 0)
99 1.2 riastrad delay(ms * 1000);
100 1.2 riastrad }
101 1.1 brad
102 1.1 brad extern struct cfdriver umcpmio_cd;
103 1.1 brad
104 1.1 brad static dev_type_open(umcpmio_dev_open);
105 1.1 brad static dev_type_read(umcpmio_dev_read);
106 1.1 brad static dev_type_write(umcpmio_dev_write);
107 1.1 brad static dev_type_close(umcpmio_dev_close);
108 1.1 brad static dev_type_ioctl(umcpmio_dev_ioctl);
109 1.2 riastrad
110 1.1 brad const struct cdevsw umcpmio_cdevsw = {
111 1.1 brad .d_open = umcpmio_dev_open,
112 1.1 brad .d_close = umcpmio_dev_close,
113 1.1 brad .d_read = umcpmio_dev_read,
114 1.1 brad .d_write = umcpmio_dev_write,
115 1.1 brad .d_ioctl = umcpmio_dev_ioctl,
116 1.1 brad .d_stop = nostop,
117 1.1 brad .d_tty = notty,
118 1.1 brad .d_poll = nopoll,
119 1.1 brad .d_mmap = nommap,
120 1.1 brad .d_kqfilter = nokqfilter,
121 1.1 brad .d_discard = nodiscard,
122 1.1 brad .d_flag = D_OTHER
123 1.1 brad };
124 1.1 brad
125 1.1 brad static const char umcpmio_valid_vrefs[] =
126 1.1 brad "4.096V, 2.048V, 1.024V, OFF, VDD";
127 1.1 brad
128 1.1 brad static const char umcpmio_valid_dcs[] =
129 1.1 brad "75%, 50%, 25%, 0%";
130 1.1 brad
131 1.1 brad static const char umcpmio_valid_cds[] =
132 1.1 brad "375kHz, 750kHz, 1.5MHz, 3MHz, 6MHz, 12MHz, 24MHz";
133 1.1 brad
134 1.1 brad static void
135 1.1 brad umcpmio_dump_buffer(bool enabled, uint8_t *buf, u_int len, const char *name)
136 1.1 brad {
137 1.2 riastrad int i;
138 1.2 riastrad
139 1.1 brad if (enabled) {
140 1.2 riastrad DPRINTF(("%s:", name));
141 1.2 riastrad for (i = 0; i < len; i++) {
142 1.2 riastrad DPRINTF((" %02x", buf[i]));
143 1.1 brad }
144 1.1 brad DPRINTF(("\n"));
145 1.1 brad }
146 1.1 brad }
147 1.1 brad
148 1.2 riastrad /*
149 1.2 riastrad * Communication with the HID function requires sending a HID report
150 1.2 riastrad * request and then waiting for a response.
151 1.1 brad *
152 1.1 brad * The panic that occurs when trying to use the interrupt... i.e.
153 1.1 brad * attaching though this driver seems to be related to the fact that
154 1.1 brad * a spin lock is held and the USB stack wants to wait.
155 1.1 brad *
156 1.1 brad * The USB stack *IS* going to have to wait for the response from
157 1.1 brad * the device, somehow...
158 1.1 brad *
159 1.1 brad * It didn't seem possible to defer the uhidev_write to a thread.
160 1.1 brad * Attempts to yield() while spinning hard also did not work and
161 1.1 brad * not yield()ing didn't allow anything else to run.
162 1.1 brad *
163 1.1 brad */
164 1.1 brad
165 1.1 brad /*
166 1.1 brad * This is the panic you will get:
167 1.1 brad *
168 1.2 riastrad * panic: kernel diagnostic assertion "ci->ci_mtx_count == -1" failed: file "../../../../kern/kern_synch.c", line 762 mi_switch: cpu0: ci_mtx_count (-2) != -1 (block with spin-mutex held)
169 1.2 riastrad */
170 1.1 brad
171 1.1 brad static void
172 1.1 brad umcpmio_uhidev_intr(void *cookie, void *ibuf, u_int len)
173 1.1 brad {
174 1.1 brad struct umcpmio_softc *sc = cookie;
175 1.1 brad
176 1.1 brad if (sc->sc_dying)
177 1.1 brad return;
178 1.1 brad
179 1.2 riastrad DPRINTFN(30, ("umcpmio_uhidev_intr: len=%d\n", len));
180 1.1 brad
181 1.1 brad mutex_enter(&sc->sc_res_mutex);
182 1.1 brad switch(len) {
183 1.1 brad case MCP2221_RES_BUFFER_SIZE:
184 1.1 brad if (sc->sc_res_buffer != NULL) {
185 1.2 riastrad memcpy(sc->sc_res_buffer, ibuf,
186 1.2 riastrad MCP2221_RES_BUFFER_SIZE);
187 1.1 brad sc->sc_res_ready = true;
188 1.1 brad cv_signal(&sc->sc_res_cv);
189 1.1 brad } else {
190 1.2 riastrad int d = umcpmiodebug;
191 1.2 riastrad device_printf(sc->sc_dev,
192 1.2 riastrad "umcpmio_uhidev_intr: NULL sc_res_buffer:"
193 1.2 riastrad " len=%d\n",
194 1.2 riastrad len);
195 1.2 riastrad umcpmiodebug = 20;
196 1.2 riastrad umcpmio_dump_buffer(true, (uint8_t *)ibuf, len,
197 1.2 riastrad "umcpmio_uhidev_intr: ibuf");
198 1.2 riastrad umcpmiodebug = d;
199 1.1 brad }
200 1.1 brad
201 1.1 brad break;
202 1.1 brad default:
203 1.2 riastrad device_printf(sc->sc_dev,
204 1.2 riastrad "umcpmio_uhidev_intr: Unknown interrupt length: %d",
205 1.2 riastrad len);
206 1.1 brad break;
207 1.1 brad }
208 1.1 brad mutex_exit(&sc->sc_res_mutex);
209 1.1 brad }
210 1.1 brad
211 1.1 brad /* Send a HID report. This needs to be called with the action mutex held */
212 1.1 brad
213 1.1 brad int
214 1.1 brad umcpmio_send_report(struct umcpmio_softc *sc, uint8_t *sendbuf,
215 1.1 brad size_t sendlen, uint8_t *resbuf, int timeout)
216 1.1 brad {
217 1.1 brad int err = 0;
218 1.2 riastrad int err_count = 0;
219 1.1 brad
220 1.1 brad if (sc->sc_dying)
221 1.1 brad return EIO;
222 1.1 brad
223 1.1 brad KASSERT(mutex_owned(&sc->sc_action_mutex));
224 1.1 brad
225 1.1 brad if (sc->sc_res_buffer != NULL) {
226 1.2 riastrad device_printf(sc->sc_dev,
227 1.2 riastrad "umcpmio_send_report: sc->sc_res_buffer is not NULL\n");
228 1.1 brad }
229 1.1 brad sc->sc_res_buffer = resbuf;
230 1.1 brad sc->sc_res_ready = false;
231 1.1 brad
232 1.1 brad err = uhidev_write(sc->sc_hdev, sendbuf, sendlen);
233 1.1 brad
234 1.1 brad if (err) {
235 1.2 riastrad DPRINTF(("umcpmio_send_report: uhidev_write errored with:"
236 1.2 riastrad " err=%d\n", err));
237 1.1 brad goto out;
238 1.1 brad }
239 1.1 brad
240 1.2 riastrad DPRINTFN(30, ("umcpmio_send_report: about to wait on cv. err=%d\n",
241 1.2 riastrad err));
242 1.1 brad
243 1.1 brad mutex_enter(&sc->sc_res_mutex);
244 1.1 brad while (!sc->sc_res_ready) {
245 1.2 riastrad DPRINTFN(20, ("umcpmio_send_report: LOOP for response."
246 1.2 riastrad " sc_res_ready=%d, err_count=%d, timeout=%d\n",
247 1.2 riastrad sc->sc_res_ready, err_count, mstohz(timeout)));
248 1.1 brad
249 1.2 riastrad err = cv_timedwait_sig(&sc->sc_res_cv, &sc->sc_res_mutex,
250 1.2 riastrad mstohz(timeout));
251 1.1 brad
252 1.2 riastrad /*
253 1.2 riastrad * We are only going to allow this to loop on an error,
254 1.1 brad * any error at all, so many times.
255 1.1 brad */
256 1.1 brad if (err) {
257 1.2 riastrad DPRINTF(("umcpmio_send_report:"
258 1.2 riastrad " cv_timedwait_sig reported an error:"
259 1.2 riastrad " err=%d, sc->sc_res_ready=%d\n",
260 1.2 riastrad err, sc->sc_res_ready));
261 1.1 brad err_count++;
262 1.1 brad }
263 1.1 brad
264 1.2 riastrad /*
265 1.2 riastrad * The CV was interrupted, but the buffer is ready so,
266 1.2 riastrad * clear the error and break out.
267 1.1 brad */
268 1.1 brad if ((err == ERESTART) && (sc->sc_res_ready)) {
269 1.2 riastrad DPRINTF(("umcpmio_send_report:"
270 1.2 riastrad " ERESTART and buffer is ready\n"));
271 1.1 brad err = 0;
272 1.1 brad break;
273 1.1 brad }
274 1.1 brad
275 1.2 riastrad /*
276 1.2 riastrad * Too many times though the loop, just break out. Turn
277 1.1 brad * a ERESTART (interruption) into a I/O error at this point.
278 1.1 brad */
279 1.1 brad if (err_count > sc->sc_response_errcnt) {
280 1.2 riastrad DPRINTF(("umcpmio_send_report: err_count exceeded:"
281 1.2 riastrad " err=%d\n", err));
282 1.1 brad if (err == ERESTART)
283 1.1 brad err = EIO;
284 1.1 brad break;
285 1.1 brad }
286 1.1 brad
287 1.1 brad /* This is a normal timeout, without interruption, try again */
288 1.1 brad if (err == EWOULDBLOCK) {
289 1.2 riastrad DPRINTF(("umcpmio_send_report: EWOULDBLOCK:"
290 1.2 riastrad " err_count=%d\n", err_count));
291 1.1 brad continue;
292 1.1 brad }
293 1.1 brad
294 1.2 riastrad /*
295 1.2 riastrad * The CV was interrupted and the buffer wasn't filled
296 1.2 riastrad * in, so try again
297 1.2 riastrad */
298 1.1 brad if ((err == ERESTART) && (!sc->sc_res_ready)) {
299 1.2 riastrad DPRINTF(("umcpmio_send_report:"
300 1.2 riastrad " ERESTART and buffer is NOT ready."
301 1.2 riastrad " err_count=%d\n", err_count));
302 1.1 brad continue;
303 1.1 brad }
304 1.1 brad }
305 1.1 brad
306 1.1 brad sc->sc_res_buffer = NULL;
307 1.1 brad sc->sc_res_ready = false;
308 1.1 brad mutex_exit(&sc->sc_res_mutex);
309 1.1 brad
310 1.1 brad /* Turn most errors into an I/O error */
311 1.1 brad if (err &&
312 1.1 brad err != ERESTART)
313 1.1 brad err = EIO;
314 1.1 brad
315 1.1 brad out:
316 1.1 brad return err;
317 1.1 brad }
318 1.1 brad
319 1.1 brad /* These are standard gpio reads and set calls */
320 1.1 brad
321 1.1 brad static int
322 1.1 brad umcpmio_gpio_pin_read(void *arg, int pin)
323 1.1 brad {
324 1.1 brad struct umcpmio_softc *sc = arg;
325 1.1 brad int r = GPIO_PIN_LOW;
326 1.1 brad
327 1.1 brad r = umcpmio_get_gpio_value(sc, pin, true);
328 1.1 brad
329 1.2 riastrad return r;
330 1.1 brad }
331 1.1 brad
332 1.1 brad static void
333 1.1 brad umcpmio_gpio_pin_write(void *arg, int pin, int value)
334 1.1 brad {
335 1.1 brad struct umcpmio_softc *sc = arg;
336 1.1 brad
337 1.1 brad umcpmio_set_gpio_value_one(sc, pin, value, true);
338 1.1 brad }
339 1.1 brad
340 1.2 riastrad /*
341 1.2 riastrad * Internal function that does the dirty work of setting a gpio
342 1.1 brad * pin to its "type".
343 1.1 brad *
344 1.2 riastrad * There are really two ways to do some of this, one is to set the pin
345 1.2 riastrad * to input and output, or whatever, using SRAM calls, the other is to
346 1.2 riastrad * use the GPIO config calls to set input and output and SRAM for
347 1.2 riastrad * everything else. This just uses SRAM for everything.
348 1.1 brad */
349 1.1 brad
350 1.1 brad static int
351 1.1 brad umcpmio_gpio_pin_ctlctl(void *arg, int pin, int flags, bool takemutex)
352 1.1 brad {
353 1.1 brad struct umcpmio_softc *sc = arg;
354 1.1 brad struct mcp2221_set_sram_req set_sram_req;
355 1.1 brad struct mcp2221_set_sram_res set_sram_res;
356 1.1 brad struct mcp2221_get_sram_res current_sram_res;
357 1.1 brad struct mcp2221_get_gpio_cfg_res current_gpio_cfg_res;
358 1.1 brad int err = 0;
359 1.1 brad
360 1.1 brad if (sc->sc_dying)
361 1.1 brad return 0;
362 1.1 brad
363 1.1 brad if (takemutex)
364 1.1 brad mutex_enter(&sc->sc_action_mutex);
365 1.1 brad
366 1.1 brad err = umcpmio_get_sram(sc, ¤t_sram_res, false);
367 1.1 brad if (err)
368 1.1 brad goto out;
369 1.1 brad
370 1.1 brad err = umcpmio_get_gpio_cfg(sc, ¤t_gpio_cfg_res, false);
371 1.1 brad if (err)
372 1.1 brad goto out;
373 1.1 brad
374 1.2 riastrad /*
375 1.2 riastrad * You can't just set one pin, you must set all of them, so copy the
376 1.1 brad * current settings for the pin we are not messing with.
377 1.1 brad *
378 1.1 brad * And, yes, of course, if the MCP-2210 is ever supported with this
379 1.1 brad * driver, this sort of unrolling will need to be turned into
380 1.1 brad * something different, but for now, just unroll as there are only
381 1.1 brad * 4 pins to care about.
382 1.1 brad *
383 1.1 brad */
384 1.1 brad
385 1.1 brad memset(&set_sram_req, 0, MCP2221_REQ_BUFFER_SIZE);
386 1.1 brad switch (pin) {
387 1.1 brad case 0:
388 1.1 brad set_sram_req.gp1_settings = current_sram_res.gp1_settings;
389 1.1 brad set_sram_req.gp2_settings = current_sram_res.gp2_settings;
390 1.1 brad set_sram_req.gp3_settings = current_sram_res.gp3_settings;
391 1.1 brad break;
392 1.1 brad case 1:
393 1.1 brad set_sram_req.gp0_settings = current_sram_res.gp0_settings;
394 1.1 brad set_sram_req.gp2_settings = current_sram_res.gp2_settings;
395 1.1 brad set_sram_req.gp3_settings = current_sram_res.gp3_settings;
396 1.1 brad break;
397 1.1 brad case 2:
398 1.1 brad set_sram_req.gp0_settings = current_sram_res.gp0_settings;
399 1.1 brad set_sram_req.gp1_settings = current_sram_res.gp1_settings;
400 1.1 brad set_sram_req.gp3_settings = current_sram_res.gp3_settings;
401 1.1 brad break;
402 1.1 brad case 3:
403 1.1 brad set_sram_req.gp0_settings = current_sram_res.gp0_settings;
404 1.1 brad set_sram_req.gp1_settings = current_sram_res.gp1_settings;
405 1.1 brad set_sram_req.gp2_settings = current_sram_res.gp2_settings;
406 1.1 brad break;
407 1.1 brad }
408 1.1 brad umcpmio_set_gpio_designation_sram(&set_sram_req, pin, flags);
409 1.1 brad umcpmio_set_gpio_dir_sram(&set_sram_req, pin, flags);
410 1.1 brad
411 1.1 brad /*
412 1.2 riastrad * This part is unfortunate... if a pin is set to output, the
413 1.2 riastrad * value set on the pin is not mirrored by the chip into SRAM,
414 1.2 riastrad * but the chip will use the value from SRAM to set the value of
415 1.2 riastrad * the pin. What this means is that we have to learn the value
416 1.2 riastrad * from the GPIO config and make sure it is set properly when
417 1.2 riastrad * updating SRAM.
418 1.1 brad */
419 1.1 brad
420 1.1 brad if (current_gpio_cfg_res.gp0_pin_dir == MCP2221_GPIO_CFG_DIR_OUTPUT) {
421 1.2 riastrad if (current_gpio_cfg_res.gp0_pin_value == 1) {
422 1.2 riastrad set_sram_req.gp0_settings |=
423 1.2 riastrad MCP2221_SRAM_GPIO_OUTPUT_HIGH;
424 1.2 riastrad } else {
425 1.2 riastrad set_sram_req.gp0_settings &=
426 1.2 riastrad ~MCP2221_SRAM_GPIO_OUTPUT_HIGH;
427 1.2 riastrad }
428 1.1 brad }
429 1.1 brad if (current_gpio_cfg_res.gp1_pin_dir == MCP2221_GPIO_CFG_DIR_OUTPUT) {
430 1.2 riastrad if (current_gpio_cfg_res.gp1_pin_value == 1) {
431 1.2 riastrad set_sram_req.gp1_settings |=
432 1.2 riastrad MCP2221_SRAM_GPIO_OUTPUT_HIGH;
433 1.2 riastrad } else {
434 1.2 riastrad set_sram_req.gp1_settings &=
435 1.2 riastrad ~MCP2221_SRAM_GPIO_OUTPUT_HIGH;
436 1.2 riastrad }
437 1.1 brad }
438 1.1 brad if (current_gpio_cfg_res.gp2_pin_dir == MCP2221_GPIO_CFG_DIR_OUTPUT) {
439 1.2 riastrad if (current_gpio_cfg_res.gp2_pin_value == 1) {
440 1.2 riastrad set_sram_req.gp2_settings |=
441 1.2 riastrad MCP2221_SRAM_GPIO_OUTPUT_HIGH;
442 1.2 riastrad } else {
443 1.2 riastrad set_sram_req.gp2_settings &=
444 1.2 riastrad ~MCP2221_SRAM_GPIO_OUTPUT_HIGH;
445 1.2 riastrad }
446 1.1 brad }
447 1.1 brad if (current_gpio_cfg_res.gp3_pin_dir == MCP2221_GPIO_CFG_DIR_OUTPUT) {
448 1.2 riastrad if (current_gpio_cfg_res.gp3_pin_value == 1) {
449 1.2 riastrad set_sram_req.gp3_settings |=
450 1.2 riastrad MCP2221_SRAM_GPIO_OUTPUT_HIGH;
451 1.2 riastrad } else {
452 1.2 riastrad set_sram_req.gp3_settings &=
453 1.2 riastrad ~MCP2221_SRAM_GPIO_OUTPUT_HIGH;
454 1.2 riastrad }
455 1.1 brad }
456 1.1 brad
457 1.1 brad err = umcpmio_put_sram(sc, &set_sram_req, &set_sram_res, false);
458 1.1 brad if (! err) {
459 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
460 1.2 riastrad (uint8_t *)&set_sram_res, MCP2221_RES_BUFFER_SIZE,
461 1.2 riastrad "umcpmio_gpio_pin_ctlctl set sram buffer copy");
462 1.1 brad if (set_sram_res.cmd == MCP2221_CMD_SET_SRAM &&
463 1.1 brad set_sram_res.completion == MCP2221_CMD_COMPLETE_OK) {
464 1.1 brad sc->sc_gpio_pins[pin].pin_flags = flags;
465 1.1 brad } else {
466 1.2 riastrad device_printf(sc->sc_dev, "umcpmio_gpio_pin_ctlctl:"
467 1.2 riastrad " not the command desired, or error: %02x %02x\n",
468 1.1 brad set_sram_res.cmd,
469 1.1 brad set_sram_res.completion);
470 1.1 brad err = EIO;
471 1.1 brad }
472 1.1 brad }
473 1.1 brad
474 1.1 brad out:
475 1.1 brad if (takemutex)
476 1.1 brad mutex_exit(&sc->sc_action_mutex);
477 1.1 brad
478 1.1 brad return err;
479 1.1 brad }
480 1.1 brad
481 1.1 brad static void
482 1.1 brad umcpmio_gpio_pin_ctl(void *arg, int pin, int flags)
483 1.1 brad {
484 1.1 brad struct umcpmio_softc *sc = arg;
485 1.1 brad
486 1.1 brad if (sc->sc_dying)
487 1.1 brad return;
488 1.1 brad
489 1.1 brad umcpmio_gpio_pin_ctlctl(sc, pin, flags, true);
490 1.1 brad }
491 1.1 brad
492 1.1 brad /*
493 1.2 riastrad * XXX -
494 1.2 riastrad *
495 1.2 riastrad * Since testing of gpio interrupts wasn't possible, this part probably
496 1.2 riastrad * is not complete. At the very least, there is a scheduled callout
497 1.2 riastrad * that needs to exist to read the interrupt status. The chip does not
498 1.2 riastrad * send anything on its own when the interrupt happens.
499 1.1 brad */
500 1.1 brad
501 1.1 brad static void *
502 1.1 brad umcpmio_gpio_intr_establish(void *vsc, int pin, int ipl, int irqmode,
503 1.1 brad int (*func)(void *), void *arg)
504 1.1 brad {
505 1.1 brad struct umcpmio_softc *sc = vsc;
506 1.1 brad struct umcpmio_irq *irq = &sc->sc_gpio_irqs[0];
507 1.1 brad struct mcp2221_set_sram_req set_sram_req;
508 1.1 brad struct mcp2221_set_sram_res set_sram_res;
509 1.1 brad struct mcp2221_get_sram_res current_sram_res;
510 1.1 brad int err = 0;
511 1.1 brad
512 1.1 brad if (sc->sc_dying)
513 1.2 riastrad return NULL;
514 1.1 brad
515 1.1 brad irq->sc_gpio_irqfunc = func;
516 1.1 brad irq->sc_gpio_irqarg = arg;
517 1.1 brad
518 1.2 riastrad DPRINTF(("umcpmio_intr_establish: pin=%d, irqmode=%04x\n",
519 1.2 riastrad pin, irqmode));
520 1.1 brad
521 1.1 brad mutex_enter(&sc->sc_action_mutex);
522 1.1 brad
523 1.1 brad err = umcpmio_get_sram(sc, ¤t_sram_res, false);
524 1.1 brad if (err)
525 1.1 brad goto out;
526 1.1 brad
527 1.1 brad memset(&set_sram_req, 0, MCP2221_REQ_BUFFER_SIZE);
528 1.1 brad set_sram_req.gp0_settings = current_sram_res.gp0_settings;
529 1.1 brad set_sram_req.gp2_settings = current_sram_res.gp2_settings;
530 1.1 brad set_sram_req.gp3_settings = current_sram_res.gp3_settings;
531 1.1 brad umcpmio_set_gpio_irq_sram(&set_sram_req, irqmode);
532 1.1 brad err = umcpmio_put_sram(sc, &set_sram_req, &set_sram_res, false);
533 1.1 brad if (! err) {
534 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
535 1.2 riastrad (uint8_t *)&set_sram_res, MCP2221_RES_BUFFER_SIZE,
536 1.2 riastrad "umcpmio_intr_establish set sram buffer copy");
537 1.1 brad if (set_sram_res.cmd == MCP2221_CMD_SET_SRAM &&
538 1.1 brad set_sram_res.completion == MCP2221_CMD_COMPLETE_OK) {
539 1.1 brad sc->sc_gpio_pins[1].pin_flags = GPIO_PIN_ALT2;
540 1.1 brad } else {
541 1.2 riastrad device_printf(sc->sc_dev, "umcpmio_intr_establish:"
542 1.2 riastrad " not the command desired, or error: %02x %02x\n",
543 1.1 brad set_sram_res.cmd,
544 1.1 brad set_sram_res.completion);
545 1.1 brad }
546 1.1 brad } else {
547 1.2 riastrad device_printf(sc->sc_dev, "umcpmio_intr_establish:"
548 1.2 riastrad " set sram error: err=%d\n",
549 1.1 brad err);
550 1.1 brad }
551 1.1 brad
552 1.1 brad out:
553 1.1 brad mutex_exit(&sc->sc_action_mutex);
554 1.1 brad
555 1.2 riastrad return irq;
556 1.1 brad }
557 1.1 brad
558 1.1 brad static void
559 1.1 brad umcpmio_gpio_intr_disestablish(void *vsc, void *ih)
560 1.1 brad {
561 1.1 brad struct umcpmio_softc *sc = vsc;
562 1.1 brad struct mcp2221_set_sram_req set_sram_req;
563 1.1 brad struct mcp2221_set_sram_res set_sram_res;
564 1.1 brad struct mcp2221_get_sram_res current_sram_res;
565 1.1 brad int err = 0;
566 1.1 brad
567 1.1 brad if (sc->sc_dying)
568 1.1 brad return;
569 1.1 brad
570 1.1 brad DPRINTF(("umcpmio_intr_disestablish:\n"));
571 1.1 brad
572 1.1 brad mutex_enter(&sc->sc_action_mutex);
573 1.1 brad
574 1.1 brad err = umcpmio_get_sram(sc, ¤t_sram_res, false);
575 1.1 brad if (err)
576 1.1 brad goto out;
577 1.1 brad
578 1.1 brad memset(&set_sram_req, 0, MCP2221_REQ_BUFFER_SIZE);
579 1.1 brad set_sram_req.gp0_settings = current_sram_res.gp0_settings;
580 1.1 brad set_sram_req.gp2_settings = current_sram_res.gp2_settings;
581 1.1 brad set_sram_req.gp3_settings = current_sram_res.gp3_settings;
582 1.1 brad umcpmio_set_gpio_irq_sram(&set_sram_req, 0);
583 1.1 brad err = umcpmio_put_sram(sc, &set_sram_req, &set_sram_res, true);
584 1.1 brad if (! err) {
585 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
586 1.2 riastrad (uint8_t *)&set_sram_res, MCP2221_RES_BUFFER_SIZE,
587 1.2 riastrad "umcpmio_intr_disestablish set sram buffer copy");
588 1.1 brad if (set_sram_res.cmd == MCP2221_CMD_SET_SRAM &&
589 1.1 brad set_sram_res.completion == MCP2221_CMD_COMPLETE_OK) {
590 1.1 brad sc->sc_gpio_pins[1].pin_flags = GPIO_PIN_INPUT;
591 1.1 brad } else {
592 1.2 riastrad device_printf(sc->sc_dev, "umcpmio_intr_disestablish:"
593 1.2 riastrad " not the command desired, or error: %02x %02x\n",
594 1.1 brad set_sram_res.cmd,
595 1.1 brad set_sram_res.completion);
596 1.1 brad }
597 1.1 brad } else {
598 1.2 riastrad device_printf(sc->sc_dev, "umcpmio_intr_disestablish:"
599 1.2 riastrad " set sram error: err=%d\n",
600 1.1 brad err);
601 1.1 brad }
602 1.1 brad out:
603 1.1 brad mutex_exit(&sc->sc_action_mutex);
604 1.1 brad }
605 1.1 brad
606 1.1 brad static bool
607 1.1 brad umcpmio_gpio_intrstr(void *vsc, int pin, int irqmode, char *buf, size_t buflen)
608 1.1 brad {
609 1.1 brad
610 1.1 brad if (pin < 0 || pin >= MCP2221_NPINS) {
611 1.2 riastrad DPRINTF(("umcpmio_gpio_intrstr:"
612 1.2 riastrad " pin %d less than zero or too big\n",
613 1.2 riastrad pin));
614 1.2 riastrad return false;
615 1.1 brad }
616 1.1 brad
617 1.1 brad if (pin != 1) {
618 1.2 riastrad DPRINTF(("umcpmio_gpio_intrstr: pin %d was not 1\n",
619 1.2 riastrad pin));
620 1.2 riastrad return false;
621 1.1 brad }
622 1.1 brad
623 1.1 brad snprintf(buf, buflen, "GPIO %d", pin);
624 1.1 brad
625 1.2 riastrad return true;
626 1.1 brad }
627 1.1 brad
628 1.1 brad /* Clear status of the I2C engine */
629 1.1 brad
630 1.1 brad static int
631 1.1 brad umcpmio_i2c_clear(struct umcpmio_softc *sc, bool takemutex)
632 1.1 brad {
633 1.1 brad int err = 0;
634 1.1 brad struct mcp2221_status_req status_req;
635 1.1 brad struct mcp2221_status_res status_res;
636 1.1 brad
637 1.1 brad memset(&status_req, 0, MCP2221_REQ_BUFFER_SIZE);
638 1.1 brad status_req.cmd = MCP2221_CMD_STATUS;
639 1.1 brad status_req.cancel_transfer = MCP2221_I2C_DO_CANCEL;
640 1.1 brad
641 1.1 brad if (takemutex)
642 1.1 brad mutex_enter(&sc->sc_action_mutex);
643 1.2 riastrad err = umcpmio_send_report(sc,
644 1.2 riastrad (uint8_t *)&status_req, MCP2221_REQ_BUFFER_SIZE,
645 1.2 riastrad (uint8_t *)&status_res, sc->sc_cv_wait);
646 1.1 brad if (takemutex)
647 1.1 brad mutex_exit(&sc->sc_action_mutex);
648 1.1 brad
649 1.1 brad if (! err) {
650 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
651 1.2 riastrad (uint8_t *)&status_res, MCP2221_RES_BUFFER_SIZE,
652 1.2 riastrad "umcpmio_i2c_clear buffer copy");
653 1.1 brad if (status_res.cmd == MCP2221_CMD_STATUS &&
654 1.1 brad status_res.completion == MCP2221_CMD_COMPLETE_OK) {
655 1.2 riastrad umcpmio_dump_buffer(true,
656 1.2 riastrad (uint8_t *)&status_res, MCP2221_RES_BUFFER_SIZE,
657 1.2 riastrad "umcpmio_i2c_clear res buffer");
658 1.1 brad } else {
659 1.2 riastrad device_printf(sc->sc_dev, "umcpmio_i2c_clear:"
660 1.2 riastrad " cmd exec: not the command desired, or error:"
661 1.2 riastrad " %02x %02x\n",
662 1.1 brad status_res.cmd,
663 1.1 brad status_res.completion);
664 1.1 brad err = EIO;
665 1.1 brad }
666 1.1 brad } else {
667 1.2 riastrad device_printf(sc->sc_dev, "umcpmio_i2c_clear: request error:"
668 1.2 riastrad " err=%d\n", err);
669 1.1 brad err = EIO;
670 1.1 brad }
671 1.1 brad
672 1.2 riastrad return err;
673 1.1 brad }
674 1.1 brad
675 1.2 riastrad /*
676 1.2 riastrad * There isn't much required to acquire or release the I2C bus, but the man
677 1.1 brad * pages says these are needed
678 1.1 brad */
679 1.1 brad
680 1.1 brad static int
681 1.1 brad umcpmio_acquire_bus(void *v, int flags)
682 1.1 brad {
683 1.2 riastrad return 0;
684 1.1 brad }
685 1.1 brad
686 1.1 brad static void
687 1.1 brad umcpmio_release_bus(void *v, int flags)
688 1.1 brad {
689 1.1 brad return;
690 1.1 brad }
691 1.1 brad
692 1.2 riastrad /*
693 1.2 riastrad * The I2C write and I2C read functions mostly use an algorithm that Adafruit
694 1.1 brad * came up with in their Python based driver. A lot of other people have used
695 1.1 brad * this same algorithm to good effect. If changes are made to the I2C read and
696 1.1 brad * write functions, it is HIGHLY advisable that a MCP2221 or MCP2221A be on
697 1.1 brad * hand to test them.
698 1.1 brad */
699 1.1 brad
700 1.1 brad /* This is what is considered a fatal return from the engine. */
701 1.1 brad
702 1.1 brad static bool
703 1.1 brad umcpmio_i2c_fatal(uint8_t state)
704 1.1 brad {
705 1.1 brad int r = false;
706 1.1 brad
707 1.1 brad if (state == MCP2221_ENGINE_ADDRNACK ||
708 1.1 brad state == MCP2221_ENGINE_STARTTIMEOUT ||
709 1.1 brad state == MCP2221_ENGINE_REPSTARTTIMEOUT ||
710 1.1 brad state == MCP2221_ENGINE_STOPTIMEOUT ||
711 1.1 brad state == MCP2221_ENGINE_READTIMEOUT ||
712 1.1 brad state == MCP2221_ENGINE_WRITETIMEOUT ||
713 1.1 brad state == MCP2221_ENGINE_ADDRTIMEOUT)
714 1.1 brad r = true;
715 1.2 riastrad return r;
716 1.1 brad }
717 1.1 brad
718 1.1 brad static int
719 1.1 brad umcpmio_i2c_write(struct umcpmio_softc *sc, i2c_op_t op, i2c_addr_t addr,
720 1.2 riastrad const void *cmdbuf, size_t cmdlen, void *databuf, size_t datalen,
721 1.2 riastrad int flags)
722 1.1 brad {
723 1.1 brad struct mcp2221_i2c_req i2c_req;
724 1.1 brad struct mcp2221_i2c_res i2c_res;
725 1.1 brad struct mcp2221_status_res status_res;
726 1.1 brad int remaining;
727 1.1 brad int err = 0;
728 1.1 brad uint8_t cmd;
729 1.1 brad size_t totallen = 0;
730 1.1 brad int wretry = sc->sc_retry_busy_write;
731 1.1 brad int wsretry = sc->sc_retry_busy_write;
732 1.1 brad
733 1.1 brad err = umcpmio_get_status(sc, &status_res, true);
734 1.1 brad if (err)
735 1.1 brad goto out;
736 1.1 brad if (status_res.internal_i2c_state != 0) {
737 1.2 riastrad DPRINTF(("umcpmio_i2c_write: internal state not zero,"
738 1.2 riastrad " clearing. internal_i2c_state=%02x\n",
739 1.2 riastrad status_res.internal_i2c_state));
740 1.1 brad err = umcpmio_i2c_clear(sc, true);
741 1.1 brad }
742 1.1 brad if (err)
743 1.1 brad goto out;
744 1.1 brad
745 1.1 brad if (cmdbuf != NULL)
746 1.1 brad totallen += cmdlen;
747 1.1 brad if (databuf != NULL)
748 1.1 brad totallen += datalen;
749 1.1 brad
750 1.1 brad again:
751 1.1 brad memset(&i2c_req, 0, MCP2221_REQ_BUFFER_SIZE);
752 1.1 brad cmd = MCP2221_I2C_WRITE_DATA_NS;
753 1.1 brad if (I2C_OP_STOP_P(op))
754 1.1 brad cmd = MCP2221_I2C_WRITE_DATA;
755 1.1 brad i2c_req.cmd = cmd;
756 1.1 brad i2c_req.lsblen = totallen;
757 1.1 brad i2c_req.msblen = 0;
758 1.1 brad i2c_req.slaveaddr = addr << 1;
759 1.1 brad
760 1.1 brad remaining = 0;
761 1.1 brad if (cmdbuf != NULL) {
762 1.1 brad memcpy(&i2c_req.data[0], cmdbuf, cmdlen);
763 1.1 brad remaining = cmdlen;
764 1.1 brad }
765 1.1 brad if (databuf != NULL)
766 1.1 brad memcpy(&i2c_req.data[remaining], databuf, datalen);
767 1.1 brad
768 1.2 riastrad DPRINTF(("umcpmio_i2c_write: I2C WRITE: cmd: %02x\n", cmd));
769 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
770 1.2 riastrad (uint8_t *)&i2c_req, MCP2221_REQ_BUFFER_SIZE,
771 1.2 riastrad "umcpmio_i2c_write: write req buffer copy");
772 1.1 brad
773 1.1 brad mutex_enter(&sc->sc_action_mutex);
774 1.2 riastrad err = umcpmio_send_report(sc,
775 1.2 riastrad (uint8_t *)&i2c_req, MCP2221_REQ_BUFFER_SIZE,
776 1.2 riastrad (uint8_t *)&i2c_res, sc->sc_cv_wait);
777 1.1 brad mutex_exit(&sc->sc_action_mutex);
778 1.1 brad if (! err) {
779 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
780 1.2 riastrad (uint8_t *)&i2c_res, MCP2221_RES_BUFFER_SIZE,
781 1.2 riastrad "umcpmio_i2c_write: write res buffer copy");
782 1.1 brad if (i2c_res.cmd == cmd &&
783 1.1 brad i2c_res.completion == MCP2221_CMD_COMPLETE_OK) {
784 1.2 riastrad /*
785 1.2 riastrad * Adafruit does a read back of the status at
786 1.2 riastrad * this point. We choose not to do that. That
787 1.2 riastrad * is done later anyway, and it seemed to be
788 1.2 riastrad * redundent.
789 1.1 brad */
790 1.1 brad } else {
791 1.1 brad if (i2c_res.cmd == cmd &&
792 1.1 brad i2c_res.completion == MCP2221_I2C_ENGINE_BUSY) {
793 1.2 riastrad DPRINTF(("umcpmio_i2c_write:"
794 1.2 riastrad " I2C engine busy\n"));
795 1.1 brad
796 1.2 riastrad const uint8_t state =
797 1.2 riastrad i2c_res.internal_i2c_state;
798 1.2 riastrad if (umcpmio_i2c_fatal(state)) {
799 1.1 brad err = EIO;
800 1.1 brad } else {
801 1.1 brad wretry--;
802 1.1 brad if (wretry > 0) {
803 1.1 brad WAITMS(sc->sc_busy_delay);
804 1.1 brad goto again;
805 1.1 brad } else {
806 1.1 brad err = EBUSY;
807 1.1 brad }
808 1.1 brad }
809 1.1 brad } else {
810 1.2 riastrad device_printf(sc->sc_dev, "umcpmio_i2c_write:"
811 1.2 riastrad " not the command desired, or error:"
812 1.2 riastrad " %02x %02x\n",
813 1.1 brad i2c_res.cmd,
814 1.1 brad i2c_res.completion);
815 1.1 brad err = EIO;
816 1.1 brad }
817 1.1 brad }
818 1.1 brad } else {
819 1.2 riastrad device_printf(sc->sc_dev, "umcpmio_i2c_write request error:"
820 1.2 riastrad " err=%d\n", err);
821 1.1 brad err = EIO;
822 1.1 brad }
823 1.1 brad
824 1.1 brad if (! err) {
825 1.1 brad while (wsretry > 0) {
826 1.1 brad wsretry--;
827 1.1 brad
828 1.2 riastrad DPRINTF(("umcpmio_i2c_write: checking status loop:"
829 1.2 riastrad " wcretry=%d\n", wsretry));
830 1.1 brad
831 1.1 brad err = umcpmio_get_status(sc, &status_res, true);
832 1.1 brad if (! err) {
833 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
834 1.2 riastrad (uint8_t *)&status_res,
835 1.2 riastrad MCP2221_RES_BUFFER_SIZE,
836 1.2 riastrad "umcpmio_i2c_write post check status");
837 1.2 riastrad /*
838 1.2 riastrad * Since there isn't any documentation on what
839 1.1 brad * some of the internal state means, it isn't
840 1.1 brad * clear that this is any different than than
841 1.1 brad * MCP2221_ENGINE_ADDRNACK in the other state
842 1.1 brad * register.
843 1.1 brad */
844 1.1 brad
845 1.2 riastrad const uint8_t state20 =
846 1.2 riastrad status_res.internal_i2c_state20;
847 1.2 riastrad if (state20 & MCP2221_ENGINE_T1_MASK_NACK) {
848 1.2 riastrad DPRINTF(("umcpmio_i2c_write"
849 1.2 riastrad " post check:"
850 1.2 riastrad " engine internal state T1"
851 1.2 riastrad " says NACK\n"));
852 1.1 brad err = EIO;
853 1.1 brad break;
854 1.1 brad }
855 1.1 brad if (status_res.internal_i2c_state == 0) {
856 1.2 riastrad DPRINTF(("umcpmio_i2c_write"
857 1.2 riastrad " post check:"
858 1.2 riastrad " engine internal state"
859 1.2 riastrad " is ZERO\n"));
860 1.1 brad err = 0;
861 1.1 brad break;
862 1.1 brad }
863 1.2 riastrad if (status_res.internal_i2c_state ==
864 1.2 riastrad MCP2221_ENGINE_WRITINGNOSTOP &&
865 1.1 brad cmd == MCP2221_I2C_WRITE_DATA_NS) {
866 1.2 riastrad DPRINTF(("umcpmio_i2c_write"
867 1.2 riastrad " post check:"
868 1.2 riastrad " engine internal state"
869 1.2 riastrad " is WRITINGNOSTOP\n"));
870 1.1 brad err = 0;
871 1.1 brad break;
872 1.1 brad }
873 1.2 riastrad const uint8_t state =
874 1.2 riastrad status_res.internal_i2c_state;
875 1.2 riastrad if (umcpmio_i2c_fatal(state)) {
876 1.2 riastrad DPRINTF(("umcpmio_i2c_write"
877 1.2 riastrad " post check:"
878 1.2 riastrad " engine internal state"
879 1.2 riastrad " is fatal: %02x\n",
880 1.2 riastrad state));
881 1.1 brad err = EIO;
882 1.1 brad break;
883 1.1 brad }
884 1.1 brad WAITMS(sc->sc_busy_delay);
885 1.1 brad } else {
886 1.1 brad err = EIO;
887 1.1 brad break;
888 1.1 brad }
889 1.1 brad }
890 1.1 brad }
891 1.1 brad
892 1.1 brad out:
893 1.2 riastrad return err;
894 1.1 brad }
895 1.1 brad
896 1.2 riastrad /*
897 1.2 riastrad * This one deviates a bit from Adafruit in that is supports a straight
898 1.2 riastrad * read and a write + read. That is, write a register to read from and
899 1.2 riastrad * then do the read.
900 1.1 brad */
901 1.1 brad
902 1.1 brad static int
903 1.1 brad umcpmio_i2c_read(struct umcpmio_softc *sc, i2c_op_t op, i2c_addr_t addr,
904 1.2 riastrad const void *cmdbuf, size_t cmdlen, void *databuf, size_t datalen, int
905 1.2 riastrad flags)
906 1.1 brad {
907 1.1 brad struct mcp2221_i2c_req i2c_req;
908 1.1 brad struct mcp2221_i2c_res i2c_res;
909 1.1 brad struct mcp2221_i2c_fetch_req i2c_fetch_req;
910 1.1 brad struct mcp2221_i2c_fetch_res i2c_fetch_res;
911 1.1 brad struct mcp2221_status_res status_res;
912 1.1 brad int err = 0;
913 1.1 brad uint8_t cmd;
914 1.1 brad int rretry = sc->sc_retry_busy_read;
915 1.1 brad
916 1.1 brad if (cmdbuf != NULL) {
917 1.2 riastrad DPRINTF(("umcpmio_i2c_read: has a cmdbuf, doing write first:"
918 1.2 riastrad " addr=%02x\n", addr));
919 1.2 riastrad err = umcpmio_i2c_write(sc, I2C_OP_WRITE, addr, cmdbuf, cmdlen,
920 1.2 riastrad NULL, 0, flags);
921 1.1 brad }
922 1.1 brad if (err)
923 1.1 brad goto out;
924 1.1 brad
925 1.1 brad err = umcpmio_get_status(sc, &status_res, true);
926 1.1 brad if (err)
927 1.1 brad goto out;
928 1.1 brad
929 1.2 riastrad if (status_res.internal_i2c_state != 0 &&
930 1.1 brad status_res.internal_i2c_state != MCP2221_ENGINE_WRITINGNOSTOP) {
931 1.2 riastrad DPRINTF(("umcpmio_i2c_read:"
932 1.2 riastrad " internal state not zero and not WRITINGNOSTOP,"
933 1.2 riastrad " clearing. internal_i2c_state=%02x\n",
934 1.2 riastrad status_res.internal_i2c_state));
935 1.1 brad err = umcpmio_i2c_clear(sc, true);
936 1.1 brad }
937 1.1 brad if (err)
938 1.1 brad goto out;
939 1.1 brad
940 1.1 brad memset(&i2c_req, 0, MCP2221_REQ_BUFFER_SIZE);
941 1.1 brad if (cmdbuf == NULL &&
942 1.1 brad status_res.internal_i2c_state != MCP2221_ENGINE_WRITINGNOSTOP) {
943 1.1 brad cmd = MCP2221_I2C_READ_DATA;
944 1.1 brad } else {
945 1.1 brad cmd = MCP2221_I2C_READ_DATA_RS;
946 1.1 brad }
947 1.1 brad
948 1.2 riastrad /*
949 1.2 riastrad * The chip apparently can't do a READ without a STOP
950 1.2 riastrad * operation. Report that, and try treating it like a READ
951 1.2 riastrad * with a STOP. This won't work for a lot of devices.
952 1.1 brad */
953 1.1 brad
954 1.1 brad if (!I2C_OP_STOP_P(op) &&
955 1.2 riastrad sc->sc_reportreadnostop) {
956 1.2 riastrad device_printf(sc->sc_dev,
957 1.2 riastrad "umcpmio_i2c_read: ************ called with READ"
958 1.2 riastrad " without STOP ***************\n");
959 1.2 riastrad }
960 1.1 brad
961 1.1 brad i2c_req.cmd = cmd;
962 1.1 brad i2c_req.lsblen = datalen;
963 1.1 brad i2c_req.msblen = 0;
964 1.1 brad i2c_req.slaveaddr = (addr << 1) | 0x01;
965 1.1 brad
966 1.2 riastrad DPRINTF(("umcpmio_i2c_read: I2C READ normal read:"
967 1.2 riastrad " cmd=%02x, addr=%02x\n", cmd, addr));
968 1.1 brad
969 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
970 1.2 riastrad (uint8_t *)&i2c_req, MCP2221_RES_BUFFER_SIZE,
971 1.2 riastrad "umcpmio_i2c_read normal read req buffer copy");
972 1.1 brad
973 1.1 brad mutex_enter(&sc->sc_action_mutex);
974 1.2 riastrad err = umcpmio_send_report(sc,
975 1.2 riastrad (uint8_t *)&i2c_req, MCP2221_REQ_BUFFER_SIZE,
976 1.2 riastrad (uint8_t *)&i2c_res, sc->sc_cv_wait);
977 1.1 brad mutex_exit(&sc->sc_action_mutex);
978 1.1 brad
979 1.1 brad if (! err) {
980 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
981 1.2 riastrad (uint8_t *)&i2c_res, MCP2221_RES_BUFFER_SIZE,
982 1.2 riastrad "umcpmio_i2c_read read-request response buffer copy");
983 1.1 brad
984 1.1 brad while (rretry > 0) {
985 1.1 brad rretry--;
986 1.2 riastrad DPRINTF(("umcpmio_i2c_read: fetch loop: rretry=%d\n",
987 1.2 riastrad rretry));
988 1.1 brad err = 0;
989 1.1 brad memset(&i2c_fetch_req, 0, MCP2221_REQ_BUFFER_SIZE);
990 1.1 brad i2c_fetch_req.cmd = MCP2221_CMD_I2C_FETCH_READ_DATA;
991 1.1 brad mutex_enter(&sc->sc_action_mutex);
992 1.2 riastrad err = umcpmio_send_report(sc,
993 1.2 riastrad (uint8_t *)&i2c_fetch_req, MCP2221_REQ_BUFFER_SIZE,
994 1.2 riastrad (uint8_t *)&i2c_fetch_res, sc->sc_cv_wait);
995 1.1 brad mutex_exit(&sc->sc_action_mutex);
996 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
997 1.2 riastrad (uint8_t *)&i2c_fetch_req, MCP2221_RES_BUFFER_SIZE,
998 1.2 riastrad "umcpmio_i2c_read fetch res buffer copy");
999 1.2 riastrad
1000 1.2 riastrad if (i2c_fetch_res.cmd ==
1001 1.2 riastrad MCP2221_CMD_I2C_FETCH_READ_DATA) {
1002 1.2 riastrad if (i2c_fetch_res.completion ==
1003 1.2 riastrad MCP2221_FETCH_READ_PARTIALDATA ||
1004 1.2 riastrad i2c_fetch_res.fetchlen ==
1005 1.2 riastrad MCP2221_FETCH_READERROR) {
1006 1.2 riastrad DPRINTF(("umcpmio_i2c_read:"
1007 1.2 riastrad " fetch loop:"
1008 1.2 riastrad " partial data or read error:"
1009 1.2 riastrad " completion=%02x,"
1010 1.2 riastrad " fetchlen=%02x\n",
1011 1.2 riastrad i2c_fetch_res.completion,
1012 1.2 riastrad i2c_fetch_res.fetchlen));
1013 1.1 brad WAITMS(sc->sc_busy_delay);
1014 1.1 brad err = EAGAIN;
1015 1.1 brad continue;
1016 1.1 brad }
1017 1.2 riastrad if (i2c_fetch_res.internal_i2c_state ==
1018 1.2 riastrad MCP2221_ENGINE_ADDRNACK) {
1019 1.2 riastrad DPRINTF(("umcpmio_i2c_read:"
1020 1.2 riastrad " fetch loop: engine NACK\n"));
1021 1.1 brad err = EIO;
1022 1.1 brad break;
1023 1.1 brad }
1024 1.1 brad if (i2c_fetch_res.internal_i2c_state == 0 &&
1025 1.1 brad i2c_fetch_res.fetchlen == 0) {
1026 1.2 riastrad DPRINTF(("umcpmio_i2c_read:"
1027 1.2 riastrad " fetch loop:"
1028 1.2 riastrad " internal state and"
1029 1.2 riastrad " fetch len are ZERO\n"));
1030 1.1 brad err = 0;
1031 1.1 brad break;
1032 1.1 brad }
1033 1.2 riastrad if (i2c_fetch_res.internal_i2c_state ==
1034 1.2 riastrad MCP2221_ENGINE_READPARTIAL ||
1035 1.2 riastrad i2c_fetch_res.internal_i2c_state ==
1036 1.2 riastrad MCP2221_ENGINE_READCOMPLETE) {
1037 1.2 riastrad int state =
1038 1.2 riastrad i2c_fetch_res.internal_i2c_state;
1039 1.2 riastrad DPRINTF(("umcpmio_i2c_read:"
1040 1.2 riastrad " fetch loop: read partial or"
1041 1.2 riastrad " read complete:"
1042 1.2 riastrad " internal_i2c_state=%02x\n",
1043 1.2 riastrad state));
1044 1.1 brad err = 0;
1045 1.1 brad break;
1046 1.1 brad }
1047 1.1 brad } else {
1048 1.2 riastrad device_printf(sc->sc_dev, "umcpmio_i2c_read:"
1049 1.2 riastrad " fetch2: not the command desired: %02x\n",
1050 1.1 brad i2c_fetch_res.cmd);
1051 1.1 brad err = EIO;
1052 1.1 brad break;
1053 1.1 brad }
1054 1.1 brad }
1055 1.1 brad if (err == EAGAIN)
1056 1.1 brad err = ETIMEDOUT;
1057 1.1 brad
1058 1.1 brad if (! err) {
1059 1.1 brad if (databuf != NULL &&
1060 1.2 riastrad i2c_fetch_res.fetchlen !=
1061 1.2 riastrad MCP2221_FETCH_READERROR) {
1062 1.2 riastrad int size = uimin(i2c_fetch_res.fetchlen,
1063 1.2 riastrad datalen);
1064 1.2 riastrad DPRINTF(("umcpmio_i2c_read: copy data:"
1065 1.2 riastrad " size=%d, fetchlen=%d\n",
1066 1.2 riastrad size, i2c_fetch_res.fetchlen));
1067 1.2 riastrad if (size > 0) {
1068 1.2 riastrad memcpy(databuf, &i2c_fetch_res.data[0],
1069 1.2 riastrad size);
1070 1.2 riastrad }
1071 1.1 brad } else {
1072 1.2 riastrad DPRINTF(("umcpmio_i2c_read: copy data:"
1073 1.2 riastrad " databuf is NULL\n"));
1074 1.1 brad }
1075 1.1 brad }
1076 1.1 brad } else {
1077 1.2 riastrad device_printf(sc->sc_dev, "umcpmio_i2c_read request error:"
1078 1.2 riastrad " cmd=%02x, err=%d\n", cmd, err);
1079 1.1 brad err = EIO;
1080 1.1 brad }
1081 1.1 brad out:
1082 1.2 riastrad return err;
1083 1.1 brad }
1084 1.1 brad
1085 1.1 brad static int
1086 1.1 brad umcpmio_i2c_exec(void *v, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
1087 1.1 brad size_t cmdlen, void *databuf, size_t datalen, int flags)
1088 1.1 brad {
1089 1.1 brad struct umcpmio_softc *sc = v;
1090 1.1 brad size_t totallen = 0;
1091 1.1 brad int err = 0;
1092 1.1 brad
1093 1.1 brad if (addr > 0x7f)
1094 1.2 riastrad return ENOTSUP;
1095 1.1 brad
1096 1.1 brad if (cmdbuf != NULL)
1097 1.1 brad totallen += cmdlen;
1098 1.1 brad if (databuf != NULL)
1099 1.1 brad totallen += datalen;
1100 1.1 brad
1101 1.2 riastrad /*
1102 1.2 riastrad * There is a way to do a transfer that is larger than 60 bytes,
1103 1.1 brad * but it requires that your break the transfer up into pieces and
1104 1.1 brad * send them in 60 byte chunks. We just won't support that right now.
1105 1.1 brad * It would be somewhat unusual for there to be a transfer that big,
1106 1.1 brad * unless you are trying to do block transfers and that isn't natively
1107 1.1 brad * supported by the chip anyway... so those have to be broken up and
1108 1.1 brad * sent as bytes.
1109 1.1 brad */
1110 1.1 brad
1111 1.1 brad if (totallen > 60)
1112 1.2 riastrad return ENOTSUP;
1113 1.1 brad
1114 1.1 brad if (I2C_OP_WRITE_P(op)) {
1115 1.2 riastrad err = umcpmio_i2c_write(sc, op, addr, cmdbuf, cmdlen,
1116 1.2 riastrad databuf, datalen, flags);
1117 1.1 brad
1118 1.1 brad DPRINTF(("umcpmio_exec: I2C WRITE: err=%d\n", err));
1119 1.1 brad } else {
1120 1.2 riastrad err = umcpmio_i2c_read(sc, op, addr, cmdbuf, cmdlen,
1121 1.2 riastrad databuf, datalen, flags);
1122 1.1 brad
1123 1.1 brad DPRINTF(("umcpmio_exec: I2C READ: err=%d\n", err));
1124 1.1 brad }
1125 1.1 brad
1126 1.2 riastrad return err;
1127 1.1 brad }
1128 1.1 brad
1129 1.1 brad /* Accessing the ADC and DAC part of the chip */
1130 1.1 brad
1131 1.2 riastrad #define UMCPMIO_DEV_UNIT(m) ((m) & 0x80 ? ((m) & 0x7f) / 3 : (m))
1132 1.2 riastrad #define UMCPMIO_DEV_WHAT(m) ((m) & 0x80 ? (((m) & 0x7f) % 3) + 1 : CONTROL_DEV)
1133 1.1 brad
1134 1.1 brad static int
1135 1.1 brad umcpmio_dev_open(dev_t dev, int flags, int fmt, struct lwp *l)
1136 1.1 brad {
1137 1.1 brad struct umcpmio_softc *sc;
1138 1.1 brad int dunit;
1139 1.1 brad int pin = -1;
1140 1.1 brad int error = 0;
1141 1.1 brad
1142 1.1 brad sc = device_lookup_private(&umcpmio_cd, UMCPMIO_DEV_UNIT(minor(dev)));
1143 1.1 brad if (!sc)
1144 1.1 brad return ENXIO;
1145 1.1 brad
1146 1.1 brad dunit = UMCPMIO_DEV_WHAT(minor(dev));
1147 1.1 brad
1148 1.1 brad if (sc->sc_dev_open[dunit]) {
1149 1.2 riastrad DPRINTF(("umcpmio_dev_open: dunit=%d BUSY\n", dunit));
1150 1.1 brad return EBUSY;
1151 1.1 brad }
1152 1.1 brad
1153 1.2 riastrad /*
1154 1.2 riastrad * The control device only allows for ioctl calls, so pretty
1155 1.2 riastrad * much allow any sort of access. For the ADC, you perform a
1156 1.2 riastrad * strict O_RDONLY and for the DAC a strict O_WRONLY. It is an
1157 1.2 riastrad * error to try and do a O_RDWR It makes little sense to try
1158 1.2 riastrad * and support select or poll. The ADC and DAC are always
1159 1.2 riastrad * available for use.
1160 1.1 brad */
1161 1.1 brad
1162 1.1 brad if (dunit != CONTROL_DEV &&
1163 1.1 brad ((flags & FREAD) && (flags & FWRITE))) {
1164 1.2 riastrad DPRINTF(("umcpmio_dev_open: Not CONTROL device and trying to"
1165 1.2 riastrad " do READ and WRITE\n"));
1166 1.1 brad return EINVAL;
1167 1.1 brad }
1168 1.1 brad
1169 1.2 riastrad /*
1170 1.2 riastrad * Ya, this unrolling will also have to be changed if the MCP-2210 is
1171 1.1 brad * supported. There are currently only 4 pins, so don't worry too much
1172 1.1 brad * about it. The MCP-2210 has RAM, so there would be a fifth for it.
1173 1.1 brad */
1174 1.1 brad
1175 1.1 brad mutex_enter(&sc->sc_action_mutex);
1176 1.1 brad if (dunit != CONTROL_DEV) {
1177 1.1 brad switch (dunit) {
1178 1.1 brad case GP1_DEV:
1179 1.1 brad pin = 1;
1180 1.1 brad break;
1181 1.1 brad case GP2_DEV:
1182 1.1 brad pin = 2;
1183 1.1 brad break;
1184 1.1 brad case GP3_DEV:
1185 1.1 brad pin = 3;
1186 1.1 brad break;
1187 1.1 brad default:
1188 1.1 brad error = EINVAL;
1189 1.1 brad break;
1190 1.1 brad }
1191 1.1 brad if (! error) {
1192 1.2 riastrad /*
1193 1.2 riastrad * XXX - we can probably do better here... it
1194 1.2 riastrad * doesn't remember what the pin was set to and
1195 1.2 riastrad * probably should.
1196 1.1 brad */
1197 1.1 brad if (flags & FREAD) {
1198 1.2 riastrad error = umcpmio_gpio_pin_ctlctl(sc, pin,
1199 1.2 riastrad GPIO_PIN_ALT0, false);
1200 1.1 brad } else {
1201 1.1 brad if (pin == 1) {
1202 1.1 brad error = EINVAL;
1203 1.1 brad } else {
1204 1.2 riastrad error = umcpmio_gpio_pin_ctlctl(sc,
1205 1.2 riastrad pin, GPIO_PIN_ALT1, false);
1206 1.1 brad }
1207 1.1 brad }
1208 1.1 brad }
1209 1.1 brad }
1210 1.1 brad if (! error)
1211 1.1 brad sc->sc_dev_open[dunit] = true;
1212 1.1 brad mutex_exit(&sc->sc_action_mutex);
1213 1.1 brad
1214 1.2 riastrad DPRINTF(("umcpmio_dev_open: Opened dunit=%d, pin=%d, error=%d\n",
1215 1.2 riastrad dunit, pin, error));
1216 1.1 brad
1217 1.1 brad return error;
1218 1.1 brad }
1219 1.1 brad
1220 1.1 brad /* Read an ADC value */
1221 1.1 brad
1222 1.1 brad static int
1223 1.1 brad umcpmio_dev_read(dev_t dev, struct uio *uio, int flags)
1224 1.1 brad {
1225 1.1 brad struct umcpmio_softc *sc;
1226 1.1 brad struct mcp2221_status_res status_res;
1227 1.1 brad int dunit;
1228 1.1 brad int error = 0;
1229 1.1 brad uint8_t adc_lsb;
1230 1.1 brad uint8_t adc_msb;
1231 1.1 brad uint16_t buf;
1232 1.1 brad
1233 1.2 riastrad sc = device_lookup_private(&umcpmio_cd, UMCPMIO_DEV_UNIT(minor(dev)));
1234 1.2 riastrad if (sc == NULL)
1235 1.1 brad return ENXIO;
1236 1.1 brad
1237 1.1 brad dunit = UMCPMIO_DEV_WHAT(minor(dev));
1238 1.1 brad
1239 1.1 brad if (dunit != CONTROL_DEV) {
1240 1.1 brad while (uio->uio_resid &&
1241 1.1 brad !sc->sc_dying) {
1242 1.1 brad error = umcpmio_get_status(sc, &status_res, true);
1243 1.1 brad if (! error) {
1244 1.1 brad switch (dunit) {
1245 1.1 brad case GP1_DEV:
1246 1.1 brad adc_lsb = status_res.adc_channel0_lsb;
1247 1.1 brad adc_msb = status_res.adc_channel0_msb;
1248 1.1 brad break;
1249 1.1 brad case GP2_DEV:
1250 1.1 brad adc_lsb = status_res.adc_channel1_lsb;
1251 1.1 brad adc_msb = status_res.adc_channel1_msb;
1252 1.1 brad break;
1253 1.1 brad case GP3_DEV:
1254 1.1 brad adc_lsb = status_res.adc_channel2_lsb;
1255 1.1 brad adc_msb = status_res.adc_channel2_msb;
1256 1.1 brad break;
1257 1.1 brad default:
1258 1.1 brad error = EINVAL;
1259 1.1 brad break;
1260 1.1 brad }
1261 1.1 brad
1262 1.1 brad if (! error) {
1263 1.1 brad if (sc->sc_dying)
1264 1.1 brad break;
1265 1.1 brad
1266 1.1 brad buf = adc_msb << 8;
1267 1.1 brad buf |= adc_lsb;
1268 1.1 brad error = uiomove(&buf, 2, uio);
1269 1.1 brad }
1270 1.1 brad }
1271 1.1 brad }
1272 1.1 brad } else {
1273 1.1 brad error = EINVAL;
1274 1.1 brad }
1275 1.1 brad
1276 1.1 brad return error;
1277 1.1 brad }
1278 1.1 brad
1279 1.1 brad /* Write to the DAC */
1280 1.1 brad
1281 1.1 brad static int
1282 1.1 brad umcpmio_dev_write(dev_t dev, struct uio *uio, int flags)
1283 1.1 brad {
1284 1.1 brad struct umcpmio_softc *sc;
1285 1.1 brad int dunit;
1286 1.1 brad int error = 0;
1287 1.1 brad
1288 1.2 riastrad sc = device_lookup_private(&umcpmio_cd, UMCPMIO_DEV_UNIT(minor(dev)));
1289 1.2 riastrad if (sc == NULL)
1290 1.1 brad return ENXIO;
1291 1.1 brad
1292 1.1 brad dunit = UMCPMIO_DEV_WHAT(minor(dev));
1293 1.1 brad
1294 1.1 brad if (dunit != CONTROL_DEV) {
1295 1.1 brad while (uio->uio_resid &&
1296 1.1 brad !sc->sc_dying) {
1297 1.1 brad uint8_t buf;
1298 1.1 brad
1299 1.1 brad if ((error = uiomove(&buf, 1, uio)) != 0)
1300 1.1 brad break;
1301 1.1 brad
1302 1.1 brad if (sc->sc_dying)
1303 1.1 brad break;
1304 1.1 brad
1305 1.1 brad error = umcpmio_set_dac_value_one(sc, buf, true);
1306 1.1 brad if (error)
1307 1.1 brad break;
1308 1.1 brad }
1309 1.1 brad } else {
1310 1.1 brad error = EINVAL;
1311 1.1 brad }
1312 1.1 brad
1313 1.1 brad return error;
1314 1.1 brad }
1315 1.1 brad
1316 1.1 brad /* Close everything up */
1317 1.1 brad
1318 1.1 brad static int
1319 1.1 brad umcpmio_dev_close(dev_t dev, int flags, int fmt, struct lwp *l)
1320 1.1 brad {
1321 1.1 brad struct umcpmio_softc *sc;
1322 1.1 brad int dunit;
1323 1.1 brad int pin;
1324 1.1 brad int error = 0;
1325 1.1 brad
1326 1.1 brad sc = device_lookup_private(&umcpmio_cd, UMCPMIO_DEV_UNIT(minor(dev)));
1327 1.1 brad if (sc->sc_dying)
1328 1.1 brad return EIO;
1329 1.1 brad
1330 1.1 brad dunit = UMCPMIO_DEV_WHAT(minor(dev));
1331 1.1 brad
1332 1.1 brad mutex_enter(&sc->sc_action_mutex);
1333 1.1 brad if (dunit != CONTROL_DEV) {
1334 1.1 brad switch (dunit) {
1335 1.1 brad case GP1_DEV:
1336 1.1 brad pin = 1;
1337 1.1 brad break;
1338 1.1 brad case GP2_DEV:
1339 1.1 brad pin = 2;
1340 1.1 brad break;
1341 1.1 brad case GP3_DEV:
1342 1.1 brad pin = 3;
1343 1.1 brad break;
1344 1.1 brad default:
1345 1.1 brad error = EINVAL;
1346 1.1 brad break;
1347 1.1 brad }
1348 1.1 brad if (! error) {
1349 1.2 riastrad /*
1350 1.2 riastrad * XXX - Ya, this really could be done better.
1351 1.2 riastrad * Probably should read the sram config and
1352 1.2 riastrad * maybe the gpio config and save out what the
1353 1.2 riastrad * pin was set to.
1354 1.1 brad */
1355 1.1 brad
1356 1.2 riastrad error = umcpmio_gpio_pin_ctlctl(sc, pin,
1357 1.2 riastrad GPIO_PIN_INPUT, false);
1358 1.1 brad }
1359 1.1 brad }
1360 1.1 brad sc->sc_dev_open[dunit] = false;
1361 1.1 brad mutex_exit(&sc->sc_action_mutex);
1362 1.1 brad
1363 1.1 brad return error;
1364 1.1 brad }
1365 1.1 brad
1366 1.1 brad static int
1367 1.1 brad umcpmio_dev_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1368 1.1 brad {
1369 1.1 brad struct umcpmio_softc *sc;
1370 1.1 brad struct mcp2221_status_res get_status_res;
1371 1.1 brad struct mcp2221_get_sram_res get_sram_res;
1372 1.1 brad struct mcp2221_get_gpio_cfg_res get_gpio_cfg_res;
1373 1.1 brad struct mcp2221_get_flash_res get_flash_res;
1374 1.1 brad struct mcp2221_status_res *ioctl_get_status;
1375 1.1 brad struct mcp2221_get_sram_res *ioctl_get_sram;
1376 1.1 brad struct mcp2221_get_gpio_cfg_res *ioctl_get_gpio_cfg;
1377 1.1 brad struct umcpmio_ioctl_get_flash *ioctl_get_flash;
1378 1.1 brad struct umcpmio_ioctl_put_flash *ioctl_put_flash;
1379 1.1 brad struct mcp2221_put_flash_req put_flash_req;
1380 1.1 brad struct mcp2221_put_flash_res put_flash_res;
1381 1.1 brad int dunit;
1382 1.1 brad int error = 0;
1383 1.1 brad
1384 1.1 brad sc = device_lookup_private(&umcpmio_cd, UMCPMIO_DEV_UNIT(minor(dev)));
1385 1.1 brad if (sc->sc_dying)
1386 1.1 brad return EIO;
1387 1.1 brad
1388 1.1 brad dunit = UMCPMIO_DEV_WHAT(minor(dev));
1389 1.1 brad
1390 1.1 brad if (dunit != CONTROL_DEV) {
1391 1.2 riastrad /*
1392 1.2 riastrad * It actually is fine to call ioctl with a unsupported
1393 1.2 riastrad * cmd, but be a little noisy if debug is enabled.
1394 1.1 brad */
1395 1.2 riastrad DPRINTF(("umcpmio_dev_ioctl: dunit is not the CONTROL device:"
1396 1.2 riastrad " dunit=%d, cmd=%ld\n", dunit, cmd));
1397 1.1 brad return EINVAL;
1398 1.1 brad }
1399 1.1 brad
1400 1.1 brad mutex_enter(&sc->sc_action_mutex);
1401 1.1 brad
1402 1.1 brad switch (cmd) {
1403 1.2 riastrad /*
1404 1.2 riastrad * The GET calls use a shadow buffer for each type of
1405 1.2 riastrad * call. That probably isn't actually needed and the
1406 1.2 riastrad * memcpy could be avoided. but... it is only ever 64
1407 1.2 riastrad * bytes, so maybe not a big deal.
1408 1.1 brad */
1409 1.1 brad case UMCPMIO_GET_STATUS:
1410 1.1 brad ioctl_get_status = (struct mcp2221_status_res *)data;
1411 1.1 brad error = umcpmio_get_status(sc, &get_status_res, false);
1412 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
1413 1.2 riastrad (uint8_t *)&get_status_res, MCP2221_RES_BUFFER_SIZE,
1414 1.2 riastrad "umcpmio_dev_ioctl: UMCPMIO_GET_STATUS: get_status_res");
1415 1.2 riastrad DPRINTF(("umcpmio_dev_ioctl: UMCPMIO_GET_STATUS:"
1416 1.2 riastrad " umcpmio_get_status error=%d\n", error));
1417 1.2 riastrad if (! error) {
1418 1.2 riastrad memcpy(ioctl_get_status, &get_status_res,
1419 1.2 riastrad MCP2221_RES_BUFFER_SIZE);
1420 1.2 riastrad }
1421 1.1 brad break;
1422 1.1 brad
1423 1.1 brad case UMCPMIO_GET_SRAM:
1424 1.1 brad ioctl_get_sram = (struct mcp2221_get_sram_res *)data;
1425 1.1 brad error = umcpmio_get_sram(sc, &get_sram_res, false);
1426 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
1427 1.2 riastrad (uint8_t *)&get_sram_res, MCP2221_RES_BUFFER_SIZE,
1428 1.2 riastrad "umcpmio_dev_ioctl: UMCPMIO_GET_SRAM: get_sram_res");
1429 1.2 riastrad DPRINTF(("umcpmio_dev_ioctl: UMCPMIO_GET_SRAM:"
1430 1.2 riastrad " umcpmio_get_sram error=%d\n", error));
1431 1.2 riastrad if (! error) {
1432 1.2 riastrad memcpy(ioctl_get_sram, &get_sram_res,
1433 1.2 riastrad MCP2221_RES_BUFFER_SIZE);
1434 1.2 riastrad }
1435 1.1 brad break;
1436 1.1 brad
1437 1.1 brad case UMCPMIO_GET_GP_CFG:
1438 1.1 brad ioctl_get_gpio_cfg = (struct mcp2221_get_gpio_cfg_res *)data;
1439 1.1 brad error = umcpmio_get_gpio_cfg(sc, &get_gpio_cfg_res, false);
1440 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
1441 1.2 riastrad (uint8_t *)&get_gpio_cfg_res, MCP2221_RES_BUFFER_SIZE,
1442 1.2 riastrad "umcpmio_dev_ioctl: UMCPMIO_GET_GP_CFG: get_gpio_cfg_res");
1443 1.2 riastrad DPRINTF(("umcpmio_dev_ioctl: UMCPMIO_GET_GP_CFG:"
1444 1.2 riastrad " umcpmio_get_gpio_cfg error=%d\n", error));
1445 1.2 riastrad if (! error) {
1446 1.2 riastrad memcpy(ioctl_get_gpio_cfg, &get_gpio_cfg_res,
1447 1.2 riastrad MCP2221_RES_BUFFER_SIZE);
1448 1.2 riastrad }
1449 1.1 brad break;
1450 1.1 brad
1451 1.1 brad case UMCPMIO_GET_FLASH:
1452 1.1 brad ioctl_get_flash = (struct umcpmio_ioctl_get_flash *)data;
1453 1.2 riastrad error = umcpmio_get_flash(sc, ioctl_get_flash->subcode,
1454 1.2 riastrad &get_flash_res, false);
1455 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
1456 1.2 riastrad (uint8_t *)&get_flash_res, MCP2221_RES_BUFFER_SIZE,
1457 1.2 riastrad "umcpmio_dev_ioctl: UMCPMIO_GET_FLASH: get_flash_res");
1458 1.2 riastrad DPRINTF(("umcpmio_dev_ioctl: UMCPMIO_GET_FLASH:"
1459 1.2 riastrad " umcpmio_get_flash subcode=%d, error=%d\n",
1460 1.2 riastrad ioctl_get_flash->subcode, error));
1461 1.2 riastrad if (!error) {
1462 1.2 riastrad memcpy(&ioctl_get_flash->get_flash_res, &get_flash_res,
1463 1.2 riastrad MCP2221_RES_BUFFER_SIZE);
1464 1.2 riastrad }
1465 1.1 brad break;
1466 1.1 brad
1467 1.1 brad case UMCPMIO_PUT_FLASH:
1468 1.2 riastrad /*
1469 1.2 riastrad * We only allow the flash parts related to gpio to be changed.
1470 1.1 brad * Bounce any attempt to do something else. Also use a shadow
1471 1.1 brad * buffer for the put, so we get to control just literally
1472 1.1 brad * everything about the write to flash.
1473 1.1 brad */
1474 1.1 brad ioctl_put_flash = (struct umcpmio_ioctl_put_flash *)data;
1475 1.2 riastrad DPRINTF(("umcpmio_dev_ioctl: UMCPMIO_PUT_FLASH:"
1476 1.2 riastrad " umcpmio_put_flash subcode=%d\n",
1477 1.2 riastrad ioctl_put_flash->subcode));
1478 1.1 brad if (ioctl_put_flash->subcode == MCP2221_FLASH_SUBCODE_GP) {
1479 1.1 brad memset(&put_flash_req, 0, MCP2221_REQ_BUFFER_SIZE);
1480 1.1 brad put_flash_req.subcode = ioctl_put_flash->subcode;
1481 1.2 riastrad put_flash_req.u.gp.gp0_settings =
1482 1.2 riastrad ioctl_put_flash->put_flash_req.u.gp.gp0_settings;
1483 1.2 riastrad put_flash_req.u.gp.gp1_settings =
1484 1.2 riastrad ioctl_put_flash->put_flash_req.u.gp.gp1_settings;
1485 1.2 riastrad put_flash_req.u.gp.gp2_settings =
1486 1.2 riastrad ioctl_put_flash->put_flash_req.u.gp.gp2_settings;
1487 1.2 riastrad put_flash_req.u.gp.gp3_settings =
1488 1.2 riastrad ioctl_put_flash->put_flash_req.u.gp.gp3_settings;
1489 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
1490 1.2 riastrad (uint8_t *)&ioctl_put_flash->put_flash_req,
1491 1.2 riastrad MCP2221_REQ_BUFFER_SIZE,
1492 1.2 riastrad "umcpmio_dev_ioctl: UMCPMIO_PUT_FLASH:"
1493 1.2 riastrad " ioctl put_flash_req");
1494 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
1495 1.2 riastrad (uint8_t *)&put_flash_req, MCP2221_REQ_BUFFER_SIZE,
1496 1.2 riastrad "umcpmio_dev_ioctl:"
1497 1.2 riastrad " UMCPMIO_PUT_FLASH: put_flash_req");
1498 1.1 brad memset(&put_flash_res, 0, MCP2221_RES_BUFFER_SIZE);
1499 1.2 riastrad error = umcpmio_put_flash(sc, &put_flash_req,
1500 1.2 riastrad &put_flash_res, false);
1501 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
1502 1.2 riastrad (uint8_t *)&put_flash_res, MCP2221_RES_BUFFER_SIZE,
1503 1.2 riastrad "umcpmio_dev_ioctl: UMCPMIO_PUT_FLASH:"
1504 1.2 riastrad " put_flash_res");
1505 1.2 riastrad memcpy(&ioctl_put_flash->put_flash_res, &put_flash_res,
1506 1.2 riastrad MCP2221_RES_BUFFER_SIZE);
1507 1.1 brad } else {
1508 1.1 brad error = EINVAL;
1509 1.1 brad }
1510 1.1 brad break;
1511 1.1 brad default:
1512 1.1 brad error = EINVAL;
1513 1.1 brad }
1514 1.1 brad
1515 1.1 brad mutex_exit(&sc->sc_action_mutex);
1516 1.1 brad
1517 1.1 brad return error;
1518 1.1 brad }
1519 1.1 brad
1520 1.1 brad /* This is for sysctl variables that don't actually change the chip. */
1521 1.1 brad
1522 1.1 brad int
1523 1.1 brad umcpmio_verify_sysctl(SYSCTLFN_ARGS)
1524 1.1 brad {
1525 1.1 brad int error, t;
1526 1.1 brad struct sysctlnode node;
1527 1.1 brad
1528 1.1 brad node = *rnode;
1529 1.1 brad t = *(int *)rnode->sysctl_data;
1530 1.1 brad node.sysctl_data = &t;
1531 1.1 brad error = sysctl_lookup(SYSCTLFN_CALL(&node));
1532 1.1 brad if (error || newp == NULL)
1533 1.1 brad return error;
1534 1.1 brad
1535 1.1 brad if (t < 0)
1536 1.1 brad return EINVAL;
1537 1.1 brad
1538 1.1 brad *(int *)rnode->sysctl_data = t;
1539 1.1 brad
1540 1.1 brad return 0;
1541 1.1 brad }
1542 1.1 brad
1543 1.2 riastrad /*
1544 1.2 riastrad * sysctl validation for stuff that interacts with the chip needs to
1545 1.2 riastrad * happen in a transaction. The read of the current state and the
1546 1.2 riastrad * update to new state can't allow for someone to sneak in between the
1547 1.2 riastrad * two.
1548 1.1 brad *
1549 1.2 riastrad * We use text for the values of a lot of these variables so you don't
1550 1.2 riastrad * need the datasheet in front of you. You get to do that with
1551 1.2 riastrad * umcpmioctl(8).
1552 1.1 brad */
1553 1.1 brad
1554 1.1 brad static struct umcpmio_sysctl_name umcpmio_vref_names[] = {
1555 1.1 brad {
1556 1.1 brad .text = "4.096V",
1557 1.1 brad },
1558 1.1 brad {
1559 1.1 brad .text = "2.048V",
1560 1.1 brad },
1561 1.1 brad {
1562 1.1 brad .text = "1.024V",
1563 1.1 brad },
1564 1.1 brad {
1565 1.1 brad .text = "OFF",
1566 1.1 brad },
1567 1.1 brad {
1568 1.1 brad .text = "VDD",
1569 1.1 brad }
1570 1.1 brad };
1571 1.1 brad
1572 1.1 brad int
1573 1.1 brad umcpmio_verify_dac_sysctl(SYSCTLFN_ARGS)
1574 1.1 brad {
1575 1.1 brad char buf[UMCPMIO_VREF_NAME];
1576 1.1 brad char cbuf[UMCPMIO_VREF_NAME];
1577 1.1 brad struct umcpmio_softc *sc;
1578 1.1 brad struct sysctlnode node;
1579 1.1 brad int error = 0;
1580 1.1 brad int vrm;
1581 1.1 brad size_t i;
1582 1.1 brad struct mcp2221_get_sram_res sram_res;
1583 1.1 brad
1584 1.1 brad node = *rnode;
1585 1.1 brad sc = node.sysctl_data;
1586 1.1 brad
1587 1.1 brad mutex_enter(&sc->sc_action_mutex);
1588 1.1 brad
1589 1.1 brad error = umcpmio_get_sram(sc, &sram_res, false);
1590 1.1 brad if (error)
1591 1.1 brad goto out;
1592 1.1 brad
1593 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
1594 1.2 riastrad (uint8_t *)&sram_res, MCP2221_RES_BUFFER_SIZE,
1595 1.2 riastrad "umcpmio_verify_dac_sysctl SRAM res buffer");
1596 1.1 brad
1597 1.1 brad if (sram_res.dac_reference_voltage & MCP2221_SRAM_DAC_IS_VRM) {
1598 1.2 riastrad vrm = sram_res.dac_reference_voltage &
1599 1.2 riastrad MCP2221_SRAM_DAC_VRM_MASK;
1600 1.1 brad switch (vrm) {
1601 1.1 brad case MCP2221_SRAM_DAC_VRM_4096V:
1602 1.1 brad strncpy(buf, "4.096V", UMCPMIO_VREF_NAME);
1603 1.1 brad break;
1604 1.1 brad case MCP2221_SRAM_DAC_VRM_2048V:
1605 1.1 brad strncpy(buf, "2.048V", UMCPMIO_VREF_NAME);
1606 1.1 brad break;
1607 1.1 brad case MCP2221_SRAM_DAC_VRM_1024V:
1608 1.1 brad strncpy(buf, "1.024V", UMCPMIO_VREF_NAME);
1609 1.1 brad break;
1610 1.1 brad case MCP2221_SRAM_DAC_VRM_OFF:
1611 1.1 brad default:
1612 1.1 brad strncpy(buf, "OFF", UMCPMIO_VREF_NAME);
1613 1.1 brad break;
1614 1.1 brad }
1615 1.1 brad } else {
1616 1.1 brad strncpy(buf, "VDD", UMCPMIO_VREF_NAME);
1617 1.1 brad }
1618 1.1 brad strncpy(cbuf, buf, UMCPMIO_VREF_NAME);
1619 1.1 brad node.sysctl_data = buf;
1620 1.1 brad error = sysctl_lookup(SYSCTLFN_CALL(&node));
1621 1.1 brad if (error || newp == NULL)
1622 1.1 brad goto out;
1623 1.1 brad
1624 1.1 brad for (i = 0; i < __arraycount(umcpmio_vref_names); i++) {
1625 1.1 brad if (strncmp(node.sysctl_data, umcpmio_vref_names[i].text,
1626 1.1 brad UMCPMIO_VREF_NAME) == 0) {
1627 1.1 brad break;
1628 1.1 brad }
1629 1.1 brad }
1630 1.1 brad
1631 1.1 brad if (i == __arraycount(umcpmio_vref_names))
1632 1.1 brad error = EINVAL;
1633 1.1 brad
1634 1.1 brad if (! error) {
1635 1.1 brad if (strncmp(cbuf, buf, UMCPMIO_VREF_NAME) != 0) {
1636 1.2 riastrad DPRINTF(("umcpmio_verify_dac_sysctl: setting DAC vref:"
1637 1.2 riastrad " %s\n", buf));
1638 1.1 brad error = umcpmio_set_dac_vref_one(sc, buf, false);
1639 1.1 brad }
1640 1.1 brad }
1641 1.1 brad
1642 1.1 brad out:
1643 1.1 brad mutex_exit(&sc->sc_action_mutex);
1644 1.1 brad return error;
1645 1.1 brad }
1646 1.1 brad
1647 1.1 brad int
1648 1.1 brad umcpmio_verify_adc_sysctl(SYSCTLFN_ARGS)
1649 1.1 brad {
1650 1.1 brad char buf[UMCPMIO_VREF_NAME];
1651 1.1 brad char cbuf[UMCPMIO_VREF_NAME];
1652 1.1 brad struct umcpmio_softc *sc;
1653 1.1 brad struct sysctlnode node;
1654 1.1 brad int error = 0;
1655 1.1 brad int vrm;
1656 1.1 brad size_t i;
1657 1.1 brad struct mcp2221_get_sram_res sram_res;
1658 1.1 brad
1659 1.1 brad node = *rnode;
1660 1.1 brad sc = node.sysctl_data;
1661 1.1 brad
1662 1.1 brad mutex_enter(&sc->sc_action_mutex);
1663 1.1 brad
1664 1.1 brad error = umcpmio_get_sram(sc, &sram_res, false);
1665 1.1 brad if (error)
1666 1.1 brad goto out;
1667 1.1 brad
1668 1.1 brad if (sram_res.irq_adc_reference_voltage & MCP2221_SRAM_ADC_IS_VRM) {
1669 1.2 riastrad vrm = sram_res.irq_adc_reference_voltage &
1670 1.2 riastrad MCP2221_SRAM_ADC_VRM_MASK;
1671 1.1 brad switch (vrm) {
1672 1.1 brad case MCP2221_SRAM_ADC_VRM_4096V:
1673 1.1 brad strncpy(buf, "4.096V", UMCPMIO_VREF_NAME);
1674 1.1 brad break;
1675 1.1 brad case MCP2221_SRAM_ADC_VRM_2048V:
1676 1.1 brad strncpy(buf, "2.048V", UMCPMIO_VREF_NAME);
1677 1.1 brad break;
1678 1.1 brad case MCP2221_SRAM_ADC_VRM_1024V:
1679 1.1 brad strncpy(buf, "1.024V", UMCPMIO_VREF_NAME);
1680 1.1 brad break;
1681 1.1 brad case MCP2221_SRAM_ADC_VRM_OFF:
1682 1.1 brad default:
1683 1.1 brad strncpy(buf, "OFF", UMCPMIO_VREF_NAME);
1684 1.1 brad break;
1685 1.1 brad }
1686 1.1 brad } else {
1687 1.1 brad strncpy(buf, "VDD", UMCPMIO_VREF_NAME);
1688 1.1 brad }
1689 1.1 brad strncpy(cbuf, buf, UMCPMIO_VREF_NAME);
1690 1.1 brad node.sysctl_data = buf;
1691 1.1 brad error = sysctl_lookup(SYSCTLFN_CALL(&node));
1692 1.1 brad if (error || newp == NULL)
1693 1.1 brad goto out;
1694 1.1 brad
1695 1.1 brad for (i = 0; i < __arraycount(umcpmio_vref_names); i++) {
1696 1.1 brad if (strncmp(node.sysctl_data, umcpmio_vref_names[i].text,
1697 1.1 brad UMCPMIO_VREF_NAME) == 0) {
1698 1.1 brad break;
1699 1.1 brad }
1700 1.1 brad }
1701 1.1 brad
1702 1.1 brad if (i == __arraycount(umcpmio_vref_names))
1703 1.1 brad error = EINVAL;
1704 1.1 brad
1705 1.1 brad if (! error) {
1706 1.1 brad if (strncmp(cbuf, buf, UMCPMIO_VREF_NAME) != 0) {
1707 1.2 riastrad DPRINTF(("umcpmio_verify_adc_sysctl: setting ADC vref:"
1708 1.2 riastrad " %s\n", buf));
1709 1.1 brad error = umcpmio_set_adc_vref_one(sc, buf, false);
1710 1.1 brad }
1711 1.1 brad }
1712 1.1 brad
1713 1.1 brad out:
1714 1.1 brad mutex_exit(&sc->sc_action_mutex);
1715 1.1 brad return error;
1716 1.1 brad }
1717 1.1 brad
1718 1.1 brad static struct umcpmio_sysctl_name umcpmio_dc_names[] = {
1719 1.1 brad {
1720 1.1 brad .text = "75%",
1721 1.1 brad },
1722 1.1 brad {
1723 1.1 brad .text = "50%",
1724 1.1 brad },
1725 1.1 brad {
1726 1.1 brad .text = "25%",
1727 1.1 brad },
1728 1.1 brad {
1729 1.1 brad .text = "0%",
1730 1.1 brad }
1731 1.1 brad };
1732 1.1 brad
1733 1.1 brad static int
1734 1.1 brad umcpmio_verify_gpioclock_dc_sysctl(SYSCTLFN_ARGS)
1735 1.1 brad {
1736 1.1 brad char buf[UMCPMIO_VREF_NAME];
1737 1.1 brad char cbuf[UMCPMIO_VREF_NAME];
1738 1.1 brad struct umcpmio_softc *sc;
1739 1.1 brad struct sysctlnode node;
1740 1.1 brad int error = 0;
1741 1.1 brad uint8_t duty_cycle;
1742 1.1 brad size_t i;
1743 1.1 brad struct mcp2221_get_sram_res sram_res;
1744 1.1 brad
1745 1.1 brad node = *rnode;
1746 1.1 brad sc = node.sysctl_data;
1747 1.1 brad
1748 1.1 brad mutex_enter(&sc->sc_action_mutex);
1749 1.1 brad
1750 1.1 brad error = umcpmio_get_sram(sc, &sram_res, false);
1751 1.1 brad if (error)
1752 1.1 brad goto out;
1753 1.1 brad
1754 1.1 brad duty_cycle = sram_res.clock_divider & MCP2221_SRAM_GPIO_CLOCK_DC_MASK;
1755 1.2 riastrad DPRINTF(("umcpmio_verify_gpioclock_dc_sysctl: current duty cycle:"
1756 1.2 riastrad " %02x\n", duty_cycle));
1757 1.1 brad switch (duty_cycle) {
1758 1.1 brad case MCP2221_SRAM_GPIO_CLOCK_DC_75:
1759 1.1 brad strncpy(buf, "75%", UMCPMIO_DC_NAME);
1760 1.1 brad break;
1761 1.1 brad case MCP2221_SRAM_GPIO_CLOCK_DC_50:
1762 1.1 brad strncpy(buf, "50%", UMCPMIO_DC_NAME);
1763 1.1 brad break;
1764 1.1 brad case MCP2221_SRAM_GPIO_CLOCK_DC_25:
1765 1.1 brad strncpy(buf, "25%", UMCPMIO_DC_NAME);
1766 1.1 brad break;
1767 1.1 brad case MCP2221_SRAM_GPIO_CLOCK_DC_0:
1768 1.1 brad default:
1769 1.1 brad strncpy(buf, "0%", UMCPMIO_DC_NAME);
1770 1.1 brad break;
1771 1.1 brad }
1772 1.1 brad strncpy(cbuf, buf, UMCPMIO_VREF_NAME);
1773 1.1 brad node.sysctl_data = buf;
1774 1.1 brad error = sysctl_lookup(SYSCTLFN_CALL(&node));
1775 1.1 brad if (error || newp == NULL)
1776 1.1 brad goto out;
1777 1.1 brad
1778 1.1 brad for (i = 0; i < __arraycount(umcpmio_dc_names); i++) {
1779 1.1 brad if (strncmp(node.sysctl_data, umcpmio_dc_names[i].text,
1780 1.1 brad UMCPMIO_VREF_NAME) == 0) {
1781 1.1 brad break;
1782 1.1 brad }
1783 1.1 brad }
1784 1.1 brad
1785 1.1 brad if (i == __arraycount(umcpmio_dc_names))
1786 1.1 brad error = EINVAL;
1787 1.1 brad
1788 1.1 brad if (! error) {
1789 1.1 brad if (strncmp(cbuf, buf, UMCPMIO_VREF_NAME) != 0) {
1790 1.2 riastrad DPRINTF(("umcpmio_verify_gpioclock_dc_sysctl:"
1791 1.2 riastrad " setting GPIO clock duty cycle: %s\n", buf));
1792 1.1 brad error = umcpmio_set_gpioclock_dc_one(sc, buf, false);
1793 1.1 brad }
1794 1.1 brad }
1795 1.1 brad
1796 1.1 brad out:
1797 1.1 brad mutex_exit(&sc->sc_action_mutex);
1798 1.1 brad return error;
1799 1.1 brad }
1800 1.1 brad
1801 1.1 brad static struct umcpmio_sysctl_name umcpmio_cd_names[] = {
1802 1.1 brad {
1803 1.1 brad .text = "375kHz",
1804 1.1 brad },
1805 1.1 brad {
1806 1.1 brad .text = "750kHz",
1807 1.1 brad },
1808 1.1 brad {
1809 1.1 brad .text = "1.5MHz",
1810 1.1 brad },
1811 1.1 brad {
1812 1.1 brad .text = "3MHz",
1813 1.1 brad },
1814 1.1 brad {
1815 1.1 brad .text = "6MHz",
1816 1.1 brad },
1817 1.1 brad {
1818 1.1 brad .text = "12MHz",
1819 1.1 brad },
1820 1.1 brad {
1821 1.1 brad .text = "24MHz",
1822 1.1 brad }
1823 1.1 brad };
1824 1.1 brad
1825 1.1 brad static int
1826 1.1 brad umcpmio_verify_gpioclock_cd_sysctl(SYSCTLFN_ARGS)
1827 1.1 brad {
1828 1.1 brad char buf[UMCPMIO_CD_NAME];
1829 1.1 brad char cbuf[UMCPMIO_CD_NAME];
1830 1.1 brad struct umcpmio_softc *sc;
1831 1.1 brad struct sysctlnode node;
1832 1.1 brad int error = 0;
1833 1.1 brad uint8_t clock_divider;
1834 1.1 brad size_t i;
1835 1.1 brad struct mcp2221_get_sram_res sram_res;
1836 1.1 brad
1837 1.1 brad node = *rnode;
1838 1.1 brad sc = node.sysctl_data;
1839 1.1 brad
1840 1.1 brad mutex_enter(&sc->sc_action_mutex);
1841 1.1 brad
1842 1.1 brad error = umcpmio_get_sram(sc, &sram_res, false);
1843 1.1 brad if (error)
1844 1.1 brad goto out;
1845 1.1 brad
1846 1.2 riastrad clock_divider = sram_res.clock_divider &
1847 1.2 riastrad MCP2221_SRAM_GPIO_CLOCK_CD_MASK;
1848 1.2 riastrad DPRINTF(("umcpmio_verify_gpioclock_cd_sysctl: current clock divider:"
1849 1.2 riastrad " %02x\n", clock_divider));
1850 1.1 brad switch (clock_divider) {
1851 1.1 brad case MCP2221_SRAM_GPIO_CLOCK_CD_375KHZ:
1852 1.1 brad strncpy(buf, "375kHz", UMCPMIO_CD_NAME);
1853 1.1 brad break;
1854 1.1 brad case MCP2221_SRAM_GPIO_CLOCK_CD_750KHZ:
1855 1.1 brad strncpy(buf, "750kHz", UMCPMIO_CD_NAME);
1856 1.1 brad break;
1857 1.1 brad case MCP2221_SRAM_GPIO_CLOCK_CD_1P5MHZ:
1858 1.1 brad strncpy(buf, "1.5MHz", UMCPMIO_CD_NAME);
1859 1.1 brad break;
1860 1.1 brad case MCP2221_SRAM_GPIO_CLOCK_CD_3MHZ:
1861 1.1 brad strncpy(buf, "3MHz", UMCPMIO_CD_NAME);
1862 1.1 brad break;
1863 1.1 brad case MCP2221_SRAM_GPIO_CLOCK_CD_6MHZ:
1864 1.1 brad strncpy(buf, "6MHz", UMCPMIO_CD_NAME);
1865 1.1 brad break;
1866 1.1 brad case MCP2221_SRAM_GPIO_CLOCK_CD_12MHZ:
1867 1.1 brad strncpy(buf, "12MHz", UMCPMIO_CD_NAME);
1868 1.1 brad break;
1869 1.1 brad case MCP2221_SRAM_GPIO_CLOCK_CD_24MHZ:
1870 1.1 brad strncpy(buf, "24MHz", UMCPMIO_CD_NAME);
1871 1.1 brad break;
1872 1.1 brad default:
1873 1.1 brad strncpy(buf, "12MHz", UMCPMIO_CD_NAME);
1874 1.1 brad break;
1875 1.1 brad }
1876 1.1 brad strncpy(cbuf, buf, UMCPMIO_CD_NAME);
1877 1.1 brad node.sysctl_data = buf;
1878 1.1 brad error = sysctl_lookup(SYSCTLFN_CALL(&node));
1879 1.1 brad if (error || newp == NULL)
1880 1.1 brad goto out;
1881 1.1 brad
1882 1.1 brad for (i = 0; i < __arraycount(umcpmio_cd_names); i++) {
1883 1.1 brad if (strncmp(node.sysctl_data, umcpmio_cd_names[i].text,
1884 1.1 brad UMCPMIO_CD_NAME) == 0) {
1885 1.1 brad break;
1886 1.1 brad }
1887 1.1 brad }
1888 1.1 brad
1889 1.1 brad if (i == __arraycount(umcpmio_cd_names))
1890 1.1 brad error = EINVAL;
1891 1.1 brad
1892 1.1 brad if (! error) {
1893 1.1 brad if (strncmp(cbuf, buf, UMCPMIO_CD_NAME) != 0) {
1894 1.2 riastrad DPRINTF(("umcpmio_verify_gpioclock_cd_sysctl:"
1895 1.2 riastrad " setting GPIO clock clock divider: %s\n",
1896 1.2 riastrad buf));
1897 1.1 brad error = umcpmio_set_gpioclock_cd_one(sc, buf, false);
1898 1.1 brad }
1899 1.1 brad }
1900 1.1 brad
1901 1.1 brad out:
1902 1.1 brad mutex_exit(&sc->sc_action_mutex);
1903 1.1 brad return error;
1904 1.1 brad }
1905 1.1 brad
1906 1.1 brad static int
1907 1.1 brad umcpmio_sysctl_init(struct umcpmio_softc *sc)
1908 1.1 brad {
1909 1.1 brad int error;
1910 1.1 brad const struct sysctlnode *cnode;
1911 1.1 brad int sysctlroot_num, i2c_num, adc_dac_num, adc_num, dac_num, gpio_num;
1912 1.1 brad
1913 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
1914 1.1 brad 0, CTLTYPE_NODE, device_xname(sc->sc_dev),
1915 1.2 riastrad SYSCTL_DESCR("mcpmio controls"),
1916 1.2 riastrad NULL, 0, NULL, 0,
1917 1.2 riastrad CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
1918 1.1 brad return error;
1919 1.1 brad
1920 1.1 brad sysctlroot_num = cnode->sysctl_num;
1921 1.1 brad
1922 1.1 brad #ifdef UMCPMIO_DEBUG
1923 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
1924 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "debug",
1925 1.2 riastrad SYSCTL_DESCR("Debug level"),
1926 1.2 riastrad umcpmio_verify_sysctl, 0, &umcpmiodebug, 0,
1927 1.2 riastrad CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
1928 1.1 brad return error;
1929 1.1 brad
1930 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
1931 1.1 brad CTLFLAG_READWRITE, CTLTYPE_BOOL, "dump_buffers",
1932 1.2 riastrad SYSCTL_DESCR("Dump buffer when debugging"),
1933 1.2 riastrad NULL, 0, &sc->sc_dumpbuffer, 0,
1934 1.2 riastrad CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
1935 1.1 brad return error;
1936 1.1 brad #endif
1937 1.1 brad
1938 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
1939 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "response_wait",
1940 1.2 riastrad SYSCTL_DESCR("How long to wait in ms for a response"
1941 1.2 riastrad " for a HID report"),
1942 1.2 riastrad umcpmio_verify_sysctl, 0, &sc->sc_cv_wait, 0,
1943 1.2 riastrad CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
1944 1.1 brad return error;
1945 1.1 brad
1946 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
1947 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "response_errcnt",
1948 1.1 brad SYSCTL_DESCR("How many errors to allow on a response"),
1949 1.2 riastrad umcpmio_verify_sysctl, 0, &sc->sc_response_errcnt, 0,
1950 1.2 riastrad CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
1951 1.1 brad return error;
1952 1.1 brad
1953 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
1954 1.1 brad 0, CTLTYPE_NODE, "i2c",
1955 1.2 riastrad SYSCTL_DESCR("I2C controls"),
1956 1.2 riastrad NULL, 0, NULL, 0,
1957 1.2 riastrad CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
1958 1.1 brad return error;
1959 1.1 brad
1960 1.1 brad i2c_num = cnode->sysctl_num;
1961 1.1 brad
1962 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
1963 1.1 brad 0, CTLTYPE_NODE, "adcdac",
1964 1.2 riastrad SYSCTL_DESCR("ADC and DAC controls"),
1965 1.2 riastrad NULL, 0, NULL, 0,
1966 1.2 riastrad CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
1967 1.1 brad return error;
1968 1.1 brad
1969 1.1 brad adc_dac_num = cnode->sysctl_num;
1970 1.1 brad
1971 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
1972 1.1 brad 0, CTLTYPE_NODE, "adc",
1973 1.2 riastrad SYSCTL_DESCR("ADC controls"),
1974 1.2 riastrad NULL, 0, NULL, 0,
1975 1.2 riastrad CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
1976 1.1 brad return error;
1977 1.1 brad
1978 1.1 brad adc_num = cnode->sysctl_num;
1979 1.1 brad
1980 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
1981 1.1 brad 0, CTLTYPE_NODE, "dac",
1982 1.2 riastrad SYSCTL_DESCR("DAC controls"),
1983 1.2 riastrad NULL, 0, NULL, 0,
1984 1.2 riastrad CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
1985 1.1 brad return error;
1986 1.1 brad
1987 1.1 brad dac_num = cnode->sysctl_num;
1988 1.1 brad
1989 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
1990 1.1 brad 0, CTLTYPE_NODE, "gpio",
1991 1.2 riastrad SYSCTL_DESCR("GPIO controls"),
1992 1.2 riastrad NULL, 0, NULL, 0,
1993 1.2 riastrad CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
1994 1.1 brad return error;
1995 1.1 brad
1996 1.1 brad gpio_num = cnode->sysctl_num;
1997 1.1 brad
1998 1.1 brad /* I2C */
1999 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
2000 1.1 brad CTLFLAG_READWRITE, CTLTYPE_BOOL, "reportreadnostop",
2001 1.2 riastrad SYSCTL_DESCR("Report that a READ without STOP was attempted"
2002 1.2 riastrad " by a device"),
2003 1.2 riastrad NULL, 0, &sc->sc_reportreadnostop, 0,
2004 1.2 riastrad CTL_HW, sysctlroot_num, i2c_num, CTL_CREATE, CTL_EOL)) != 0)
2005 1.1 brad return error;
2006 1.1 brad
2007 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
2008 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "busy_delay",
2009 1.1 brad SYSCTL_DESCR("How long to wait in ms when the I2C engine is busy"),
2010 1.2 riastrad umcpmio_verify_sysctl, 0, &sc->sc_busy_delay, 0,
2011 1.2 riastrad CTL_HW, sysctlroot_num, i2c_num, CTL_CREATE, CTL_EOL)) != 0)
2012 1.1 brad return error;
2013 1.1 brad
2014 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
2015 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "retry_busy_read",
2016 1.1 brad SYSCTL_DESCR("How many times to retry a busy I2C read"),
2017 1.2 riastrad umcpmio_verify_sysctl, 0, &sc->sc_retry_busy_read, 0,
2018 1.2 riastrad CTL_HW, sysctlroot_num, i2c_num, CTL_CREATE, CTL_EOL)) != 0)
2019 1.1 brad return error;
2020 1.1 brad
2021 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
2022 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "retry_busy_write",
2023 1.1 brad SYSCTL_DESCR("How many times to retry a busy I2C write"),
2024 1.2 riastrad umcpmio_verify_sysctl, 0, &sc->sc_retry_busy_write, 0,
2025 1.2 riastrad CTL_HW, sysctlroot_num, i2c_num, CTL_CREATE, CTL_EOL)) != 0)
2026 1.1 brad return error;
2027 1.1 brad
2028 1.1 brad /* GPIO */
2029 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
2030 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "irq_poll",
2031 1.1 brad SYSCTL_DESCR("How often to poll for a IRQ change"),
2032 1.2 riastrad umcpmio_verify_sysctl, 0, &sc->sc_irq_poll, 0,
2033 1.2 riastrad CTL_HW, sysctlroot_num, gpio_num, CTL_CREATE, CTL_EOL)) != 0)
2034 1.1 brad return error;
2035 1.1 brad
2036 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
2037 1.1 brad CTLFLAG_READONLY, CTLTYPE_STRING, "clock_duty_cycles",
2038 1.2 riastrad SYSCTL_DESCR("Valid duty cycles for GPIO clock on"
2039 1.2 riastrad " GP1 ALT3 duty cycle"),
2040 1.2 riastrad 0, 0, __UNCONST(umcpmio_valid_dcs), sizeof(umcpmio_valid_dcs) + 1,
2041 1.2 riastrad CTL_HW, sysctlroot_num, gpio_num, CTL_CREATE, CTL_EOL)) != 0)
2042 1.1 brad return error;
2043 1.1 brad
2044 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
2045 1.1 brad CTLFLAG_READWRITE, CTLTYPE_STRING, "clock_duty_cycle",
2046 1.1 brad SYSCTL_DESCR("GPIO clock on GP1 ALT3 duty cycle"),
2047 1.2 riastrad umcpmio_verify_gpioclock_dc_sysctl, 0, (void *)sc, UMCPMIO_DC_NAME,
2048 1.2 riastrad CTL_HW, sysctlroot_num, gpio_num, CTL_CREATE, CTL_EOL)) != 0)
2049 1.1 brad return error;
2050 1.1 brad
2051 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
2052 1.1 brad CTLFLAG_READONLY, CTLTYPE_STRING, "clock_dividers",
2053 1.2 riastrad SYSCTL_DESCR("Valid clock dividers for GPIO clock on GP1"
2054 1.2 riastrad " with ALT3"),
2055 1.2 riastrad 0, 0, __UNCONST(umcpmio_valid_cds), sizeof(umcpmio_valid_cds) + 1,
2056 1.2 riastrad CTL_HW, sysctlroot_num, gpio_num, CTL_CREATE, CTL_EOL)) != 0)
2057 1.1 brad return error;
2058 1.1 brad
2059 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
2060 1.1 brad CTLFLAG_READWRITE, CTLTYPE_STRING, "clock_divider",
2061 1.1 brad SYSCTL_DESCR("GPIO clock on GP1 ALT3 clock divider"),
2062 1.2 riastrad umcpmio_verify_gpioclock_cd_sysctl, 0, (void *)sc, UMCPMIO_CD_NAME,
2063 1.2 riastrad CTL_HW, sysctlroot_num, gpio_num, CTL_CREATE, CTL_EOL)) != 0)
2064 1.1 brad return error;
2065 1.1 brad
2066 1.1 brad /* ADC and DAC */
2067 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
2068 1.1 brad CTLFLAG_READONLY, CTLTYPE_STRING, "vrefs",
2069 1.1 brad SYSCTL_DESCR("Valid vref values for ADC and DAC"),
2070 1.2 riastrad 0, 0,
2071 1.2 riastrad __UNCONST(umcpmio_valid_vrefs), sizeof(umcpmio_valid_vrefs) + 1,
2072 1.2 riastrad CTL_HW, sysctlroot_num, adc_dac_num, CTL_CREATE, CTL_EOL)) != 0)
2073 1.1 brad return error;
2074 1.1 brad
2075 1.1 brad /* ADC */
2076 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
2077 1.1 brad CTLFLAG_READWRITE, CTLTYPE_STRING, "vref",
2078 1.1 brad SYSCTL_DESCR("ADC voltage reference"),
2079 1.2 riastrad umcpmio_verify_adc_sysctl, 0, (void *)sc, UMCPMIO_VREF_NAME,
2080 1.2 riastrad CTL_HW, sysctlroot_num, adc_num, CTL_CREATE, CTL_EOL)) != 0)
2081 1.1 brad return error;
2082 1.1 brad
2083 1.1 brad /* DAC */
2084 1.1 brad if ((error = sysctl_createv(&sc->sc_umcpmiolog, 0, NULL, &cnode,
2085 1.1 brad CTLFLAG_READWRITE, CTLTYPE_STRING, "vref",
2086 1.1 brad SYSCTL_DESCR("DAC voltage reference"),
2087 1.2 riastrad umcpmio_verify_dac_sysctl, 0, (void *)sc, UMCPMIO_VREF_NAME,
2088 1.2 riastrad CTL_HW, sysctlroot_num, dac_num, CTL_CREATE, CTL_EOL)) != 0)
2089 1.1 brad return error;
2090 1.1 brad
2091 1.1 brad return 0;
2092 1.1 brad }
2093 1.1 brad
2094 1.1 brad static int
2095 1.1 brad umcpmio_match(device_t parent, cfdata_t match, void *aux)
2096 1.1 brad {
2097 1.1 brad struct uhidev_attach_arg *uha = aux;
2098 1.1 brad
2099 1.1 brad return umcpmio_lookup(uha->uiaa->uiaa_vendor, uha->uiaa->uiaa_product)
2100 1.1 brad != NULL ? UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
2101 1.1 brad }
2102 1.1 brad
2103 1.2 riastrad /*
2104 1.2 riastrad * This driver could be extended to support the MCP-2210 which is MCP's
2105 1.2 riastrad * USB to SPI / gpio chip. It also appears to be a something like the
2106 1.2 riastrad * PIC16F1455 used in the MCP2221 / MCP2221A. It is likely that a lot
2107 1.2 riastrad * of this could use tables to drive behavior.
2108 1.1 brad */
2109 1.1 brad
2110 1.1 brad static void
2111 1.1 brad umcpmio_attach(device_t parent, device_t self, void *aux)
2112 1.1 brad {
2113 1.1 brad struct umcpmio_softc *sc = device_private(self);
2114 1.1 brad struct uhidev_attach_arg *uha = aux;
2115 1.1 brad struct gpiobus_attach_args gba;
2116 1.1 brad struct i2cbus_attach_args iba;
2117 1.1 brad int err;
2118 1.1 brad struct mcp2221_status_res status_res;
2119 1.1 brad
2120 1.1 brad sc->sc_dev = self;
2121 1.1 brad sc->sc_hdev = uha->parent;
2122 1.1 brad sc->sc_udev = uha->uiaa->uiaa_device;
2123 1.1 brad
2124 1.1 brad sc->sc_umcpmiolog = NULL;
2125 1.1 brad sc->sc_dumpbuffer = false;
2126 1.1 brad
2127 1.1 brad sc->sc_reportreadnostop = true;
2128 1.1 brad sc->sc_cv_wait = 2500;
2129 1.1 brad sc->sc_response_errcnt = 5;
2130 1.1 brad sc->sc_busy_delay = 1;
2131 1.1 brad sc->sc_retry_busy_read = 50;
2132 1.1 brad sc->sc_retry_busy_write = 50;
2133 1.1 brad sc->sc_irq_poll = 10;
2134 1.2 riastrad sc->sc_dev_open[CONTROL_DEV] = false;
2135 1.2 riastrad sc->sc_dev_open[GP1_DEV] = false;
2136 1.2 riastrad sc->sc_dev_open[GP2_DEV] = false;
2137 1.2 riastrad sc->sc_dev_open[GP3_DEV] = false;
2138 1.1 brad
2139 1.1 brad aprint_normal("\n");
2140 1.1 brad
2141 1.1 brad if ((err = umcpmio_sysctl_init(sc)) != 0) {
2142 1.1 brad aprint_error_dev(self, "Can't setup sysctl tree (%d)\n", err);
2143 1.1 brad return;
2144 1.1 brad }
2145 1.1 brad
2146 1.1 brad mutex_init(&sc->sc_action_mutex, MUTEX_DEFAULT, IPL_NONE);
2147 1.1 brad cv_init(&sc->sc_res_cv, "mcpres");
2148 1.1 brad mutex_init(&sc->sc_res_mutex, MUTEX_DEFAULT, IPL_NONE);
2149 1.1 brad sc->sc_res_buffer = NULL;
2150 1.1 brad sc->sc_res_ready = false;
2151 1.1 brad
2152 1.1 brad err = uhidev_open(sc->sc_hdev, &umcpmio_uhidev_intr, sc);
2153 1.1 brad
2154 1.2 riastrad /*
2155 1.2 riastrad * It is not clear that this should be needed, but it was noted
2156 1.1 brad * that the device would sometimes not be ready if this delay
2157 1.2 riastrad * was not present. In fact, the attempts to set stuff a
2158 1.2 riastrad * little later would sometimes fail.
2159 1.1 brad */
2160 1.1 brad
2161 1.1 brad delay(1000);
2162 1.1 brad
2163 1.2 riastrad if (err) {
2164 1.2 riastrad aprint_error_dev(sc->sc_dev, "umcpmio_attach: "
2165 1.2 riastrad " open uhidev_open: err=%d\n", err);
2166 1.2 riastrad }
2167 1.1 brad
2168 1.1 brad if (!err)
2169 1.1 brad err = umcpmio_get_status(sc, &status_res, true);
2170 1.1 brad
2171 1.1 brad if (!err) {
2172 1.2 riastrad aprint_normal_dev(sc->sc_dev,
2173 1.2 riastrad "Hardware revision: %d.%d, Firmware revision: %d.%d\n",
2174 1.1 brad status_res.mcp2221_hardware_rev_major,
2175 1.1 brad status_res.mcp2221_hardware_rev_minor,
2176 1.1 brad status_res.mcp2221_firmware_rev_major,
2177 1.1 brad status_res.mcp2221_firmware_rev_minor);
2178 1.1 brad
2179 1.2 riastrad /*
2180 1.2 riastrad * The datasheet suggests that it is possble for this
2181 1.2 riastrad * to fail if the I2C port is currently being used.
2182 1.2 riastrad * However... since you just plugged in the chip, the
2183 1.2 riastrad * I2C port should not really be in use at that moment.
2184 1.2 riastrad * In any case, try hard to set this and don't make it
2185 1.2 riastrad * fatal if it did not get set.
2186 1.1 brad */
2187 1.2 riastrad int i2cspeed = 0;
2188 1.1 brad while (! err && i2cspeed < 3) {
2189 1.1 brad err = umcpmio_set_i2c_speed_one(sc, I2C_SPEED_SM, true);
2190 1.1 brad if (err) {
2191 1.2 riastrad aprint_error_dev(sc->sc_dev, "umcpmio_attach:"
2192 1.2 riastrad " set I2C speed: err=%d\n",
2193 1.1 brad err);
2194 1.1 brad delay(300);
2195 1.1 brad }
2196 1.1 brad i2cspeed++;
2197 1.1 brad }
2198 1.1 brad
2199 1.1 brad struct mcp2221_get_sram_res get_sram_res;
2200 1.1 brad err = umcpmio_get_sram(sc, &get_sram_res, true);
2201 1.1 brad
2202 1.1 brad if (! err) {
2203 1.2 riastrad umcpmio_dump_buffer(sc->sc_dumpbuffer,
2204 1.2 riastrad (uint8_t *)&get_sram_res, MCP2221_RES_BUFFER_SIZE,
2205 1.2 riastrad "umcpmio_attach get sram buffer copy");
2206 1.2 riastrad
2207 1.2 riastrad /*
2208 1.2 riastrad * There are only 4 pins right now, just unroll
2209 1.2 riastrad * any loops
2210 1.2 riastrad */
2211 1.1 brad
2212 1.1 brad sc->sc_gpio_pins[0].pin_num = 0;
2213 1.2 riastrad sc->sc_gpio_pins[0].pin_caps = GPIO_PIN_INPUT;
2214 1.2 riastrad sc->sc_gpio_pins[0].pin_caps |= GPIO_PIN_OUTPUT;
2215 1.2 riastrad sc->sc_gpio_pins[0].pin_caps |= GPIO_PIN_ALT0;
2216 1.2 riastrad sc->sc_gpio_pins[0].pin_caps |= GPIO_PIN_ALT3;
2217 1.2 riastrad sc->sc_gpio_pins[0].pin_flags =
2218 1.2 riastrad umcpmio_sram_gpio_to_flags(
2219 1.2 riastrad get_sram_res.gp0_settings);
2220 1.1 brad sc->sc_gpio_pins[0].pin_intrcaps = 0;
2221 1.1 brad snprintf(sc->sc_gpio_pins[0].pin_defname, 4, "GP0");
2222 1.1 brad
2223 1.1 brad sc->sc_gpio_pins[1].pin_num = 1;
2224 1.2 riastrad sc->sc_gpio_pins[1].pin_caps = GPIO_PIN_INPUT;
2225 1.2 riastrad sc->sc_gpio_pins[1].pin_caps |= GPIO_PIN_OUTPUT;
2226 1.2 riastrad sc->sc_gpio_pins[1].pin_caps |= GPIO_PIN_ALT0;
2227 1.2 riastrad sc->sc_gpio_pins[1].pin_caps |= GPIO_PIN_ALT1;
2228 1.2 riastrad sc->sc_gpio_pins[1].pin_caps |= GPIO_PIN_ALT2;
2229 1.2 riastrad sc->sc_gpio_pins[1].pin_caps |= GPIO_PIN_ALT3;
2230 1.2 riastrad sc->sc_gpio_pins[1].pin_flags =
2231 1.2 riastrad umcpmio_sram_gpio_to_flags(
2232 1.2 riastrad get_sram_res.gp1_settings);
2233 1.1 brad /* XXX - lets not advertise this right now... */
2234 1.1 brad #if 0
2235 1.2 riastrad sc->sc_gpio_pins[1].pin_intrcaps = GPIO_INTR_POS_EDGE;
2236 1.2 riastrad sc->sc_gpio_pins[1].pin_intrcaps |= GPIO_INTR_NEG_EDGE;
2237 1.2 riastrad sc->sc_gpio_pins[1].pin_intrcaps |=
2238 1.2 riastrad GPIO_INTR_DOUBLE_EDGE;
2239 1.2 riastrad sc->sc_gpio_pins[1].pin_intrcaps |= GPIO_INTR_MPSAFE;
2240 1.1 brad #endif
2241 1.1 brad sc->sc_gpio_pins[1].pin_intrcaps = 0;
2242 1.1 brad snprintf(sc->sc_gpio_pins[1].pin_defname, 4, "GP1");
2243 1.1 brad
2244 1.1 brad sc->sc_gpio_pins[2].pin_num = 2;
2245 1.2 riastrad sc->sc_gpio_pins[2].pin_caps = GPIO_PIN_INPUT;
2246 1.2 riastrad sc->sc_gpio_pins[2].pin_caps |= GPIO_PIN_OUTPUT;
2247 1.2 riastrad sc->sc_gpio_pins[2].pin_caps |= GPIO_PIN_ALT0;
2248 1.2 riastrad sc->sc_gpio_pins[2].pin_caps |= GPIO_PIN_ALT1;
2249 1.2 riastrad sc->sc_gpio_pins[2].pin_caps |= GPIO_PIN_ALT3;
2250 1.2 riastrad sc->sc_gpio_pins[2].pin_flags =
2251 1.2 riastrad umcpmio_sram_gpio_to_flags(
2252 1.2 riastrad get_sram_res.gp2_settings);
2253 1.1 brad sc->sc_gpio_pins[2].pin_intrcaps = 0;
2254 1.1 brad snprintf(sc->sc_gpio_pins[2].pin_defname, 4, "GP2");
2255 1.1 brad
2256 1.1 brad sc->sc_gpio_pins[3].pin_num = 3;
2257 1.2 riastrad sc->sc_gpio_pins[3].pin_caps = GPIO_PIN_INPUT;
2258 1.2 riastrad sc->sc_gpio_pins[3].pin_caps |= GPIO_PIN_OUTPUT;
2259 1.2 riastrad sc->sc_gpio_pins[3].pin_caps |= GPIO_PIN_ALT0;
2260 1.2 riastrad sc->sc_gpio_pins[3].pin_caps |= GPIO_PIN_ALT1;
2261 1.2 riastrad sc->sc_gpio_pins[3].pin_caps |= GPIO_PIN_ALT3;
2262 1.2 riastrad sc->sc_gpio_pins[3].pin_flags =
2263 1.2 riastrad umcpmio_sram_gpio_to_flags(
2264 1.2 riastrad get_sram_res.gp3_settings);
2265 1.1 brad sc->sc_gpio_pins[3].pin_intrcaps = 0;
2266 1.1 brad snprintf(sc->sc_gpio_pins[3].pin_defname, 4, "GP3");
2267 1.1 brad
2268 1.1 brad sc->sc_gpio_gc.gp_cookie = sc;
2269 1.1 brad sc->sc_gpio_gc.gp_pin_read = umcpmio_gpio_pin_read;
2270 1.1 brad sc->sc_gpio_gc.gp_pin_write = umcpmio_gpio_pin_write;
2271 1.1 brad sc->sc_gpio_gc.gp_pin_ctl = umcpmio_gpio_pin_ctl;
2272 1.1 brad
2273 1.2 riastrad sc->sc_gpio_gc.gp_intr_establish =
2274 1.2 riastrad umcpmio_gpio_intr_establish;
2275 1.2 riastrad sc->sc_gpio_gc.gp_intr_disestablish =
2276 1.2 riastrad umcpmio_gpio_intr_disestablish;
2277 1.1 brad sc->sc_gpio_gc.gp_intr_str = umcpmio_gpio_intrstr;
2278 1.1 brad
2279 1.1 brad gba.gba_gc = &sc->sc_gpio_gc;
2280 1.1 brad gba.gba_pins = sc->sc_gpio_pins;
2281 1.1 brad gba.gba_npins = MCP2221_NPINS;
2282 1.1 brad
2283 1.2 riastrad sc->sc_gpio_dev =
2284 1.2 riastrad config_found(self, &gba, gpiobus_print,
2285 1.2 riastrad CFARGS(.iattr = "gpiobus"));
2286 1.1 brad
2287 1.1 brad iic_tag_init(&sc->sc_i2c_tag);
2288 1.1 brad sc->sc_i2c_tag.ic_cookie = sc;
2289 1.1 brad sc->sc_i2c_tag.ic_acquire_bus = umcpmio_acquire_bus;
2290 1.1 brad sc->sc_i2c_tag.ic_release_bus = umcpmio_release_bus;
2291 1.1 brad sc->sc_i2c_tag.ic_exec = umcpmio_i2c_exec;
2292 1.1 brad
2293 1.1 brad memset(&iba, 0, sizeof(iba));
2294 1.1 brad iba.iba_tag = &sc->sc_i2c_tag;
2295 1.2 riastrad sc->sc_i2c_dev = config_found(self, &iba, iicbus_print,
2296 1.2 riastrad CFARGS(.iattr = "i2cbus"));
2297 1.1 brad } else {
2298 1.2 riastrad aprint_error_dev(sc->sc_dev, "umcpmio_attach:"
2299 1.2 riastrad " get sram error: err=%d\n",
2300 1.1 brad err);
2301 1.1 brad }
2302 1.1 brad } else {
2303 1.2 riastrad aprint_error_dev(sc->sc_dev, "umcpmio_attach:"
2304 1.2 riastrad " open uhidev_open: err=%d\n", err);
2305 1.1 brad }
2306 1.1 brad }
2307 1.1 brad
2308 1.1 brad static int
2309 1.1 brad umcpmio_detach(device_t self, int flags)
2310 1.1 brad {
2311 1.1 brad struct umcpmio_softc *sc = device_private(self);
2312 1.1 brad int err;
2313 1.1 brad
2314 1.1 brad DPRINTF(("umcpmio_detach: sc=%p flags=%d\n", sc, flags));
2315 1.1 brad
2316 1.1 brad mutex_enter(&sc->sc_action_mutex);
2317 1.1 brad sc->sc_dying = 1;
2318 1.1 brad
2319 1.1 brad err = config_detach_children(self, flags);
2320 1.1 brad if (err)
2321 1.1 brad return err;
2322 1.1 brad
2323 1.1 brad uhidev_close(sc->sc_hdev);
2324 1.1 brad
2325 1.1 brad mutex_destroy(&sc->sc_res_mutex);
2326 1.1 brad cv_destroy(&sc->sc_res_cv);
2327 1.1 brad
2328 1.1 brad sysctl_teardown(&sc->sc_umcpmiolog);
2329 1.1 brad
2330 1.1 brad mutex_exit(&sc->sc_action_mutex);
2331 1.1 brad mutex_destroy(&sc->sc_action_mutex);
2332 1.1 brad
2333 1.1 brad return 0;
2334 1.1 brad }
2335 1.1 brad
2336 1.1 brad static int
2337 1.1 brad umcpmio_activate(device_t self, enum devact act)
2338 1.1 brad {
2339 1.1 brad struct umcpmio_softc *sc = device_private(self);
2340 1.1 brad
2341 1.2 riastrad DPRINTFN(5, ("umcpmio_activate: %d\n", act));
2342 1.1 brad
2343 1.1 brad switch (act) {
2344 1.1 brad case DVACT_DEACTIVATE:
2345 1.1 brad sc->sc_dying = 1;
2346 1.1 brad return 0;
2347 1.1 brad default:
2348 1.1 brad return EOPNOTSUPP;
2349 1.1 brad }
2350 1.1 brad }
2351