ds2482ow.c revision 1.1 1 1.1 brad /* $NetBSD: ds2482ow.c,v 1.1 2024/11/04 20:43:38 brad 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.1 brad __KERNEL_RCSID(0, "$NetBSD: ds2482ow.c,v 1.1 2024/11/04 20:43:38 brad Exp $");
21 1.1 brad
22 1.1 brad /*
23 1.1 brad Driver for the DS2482-100 and DS2482-800 I2C to Onewire bridge
24 1.1 brad */
25 1.1 brad
26 1.1 brad #include <sys/param.h>
27 1.1 brad #include <sys/systm.h>
28 1.1 brad #include <sys/kernel.h>
29 1.1 brad #include <sys/device.h>
30 1.1 brad #include <sys/module.h>
31 1.1 brad #include <sys/sysctl.h>
32 1.1 brad #include <sys/mutex.h>
33 1.1 brad
34 1.1 brad #include <dev/i2c/i2cvar.h>
35 1.1 brad #include <dev/onewire/onewirevar.h>
36 1.1 brad #include <dev/i2c/ds2482owreg.h>
37 1.1 brad #include <dev/i2c/ds2482owvar.h>
38 1.1 brad
39 1.1 brad #define DS2482_ONEWIRE_SINGLE_BIT_READ 0xF7 /* Artifical */
40 1.1 brad #define DS2482_ONEWIRE_SINGLE_BIT_WRITE 0xF8 /* Artifical */
41 1.1 brad
42 1.1 brad static int ds2482_poke(i2c_tag_t, i2c_addr_t, bool);
43 1.1 brad static int ds2482_match(device_t, cfdata_t, void *);
44 1.1 brad static void ds2482_attach(device_t, device_t, void *);
45 1.1 brad static int ds2482_detach(device_t, int);
46 1.1 brad static int ds2482_verify_sysctl(SYSCTLFN_ARGS);
47 1.1 brad
48 1.1 brad static int ds2482_ow_reset(void *);
49 1.1 brad static int ds2482_ow_read_bit(void *);
50 1.1 brad static void ds2482_ow_write_bit(void *, int);
51 1.1 brad static int ds2482_ow_read_byte(void *);
52 1.1 brad static void ds2482_ow_write_byte(void *, int);
53 1.1 brad static int ds2482_ow_triplet(void *, int);
54 1.1 brad
55 1.1 brad #define DS2482_DEBUG
56 1.1 brad
57 1.1 brad #ifdef DS2482_DEBUG
58 1.1 brad #define DPRINTF(s, l, x) \
59 1.1 brad do { \
60 1.1 brad if (l <= s->sc_ds2482debug) \
61 1.1 brad aprint_normal x; \
62 1.1 brad } while (/*CONSTCOND*/0)
63 1.1 brad #else
64 1.1 brad #define DPRINTF(s, l, x)
65 1.1 brad #endif
66 1.1 brad
67 1.1 brad #ifdef DS2482_DEBUG
68 1.1 brad #define DPRINTF2(dl, l, x) \
69 1.1 brad do { \
70 1.1 brad if (l <= dl) \
71 1.1 brad aprint_normal x; \
72 1.1 brad } while (/*CONSTCOND*/0)
73 1.1 brad #else
74 1.1 brad #define DPRINTF2(dl, l, x)
75 1.1 brad #endif
76 1.1 brad
77 1.1 brad CFATTACH_DECL_NEW(ds2482ow, sizeof(struct ds2482ow_sc),
78 1.1 brad ds2482_match, ds2482_attach, ds2482_detach, NULL);
79 1.1 brad
80 1.1 brad
81 1.1 brad #define DS2482_QUICK_DELAY 18
82 1.1 brad #define DS2482_SLOW_DELAY 35
83 1.1 brad
84 1.1 brad int
85 1.1 brad ds2482_verify_sysctl(SYSCTLFN_ARGS)
86 1.1 brad {
87 1.1 brad int error, t;
88 1.1 brad struct sysctlnode node;
89 1.1 brad
90 1.1 brad node = *rnode;
91 1.1 brad t = *(int *)rnode->sysctl_data;
92 1.1 brad node.sysctl_data = &t;
93 1.1 brad error = sysctl_lookup(SYSCTLFN_CALL(&node));
94 1.1 brad if (error || newp == NULL)
95 1.1 brad return error;
96 1.1 brad
97 1.1 brad if (t < 0)
98 1.1 brad return EINVAL;
99 1.1 brad
100 1.1 brad *(int *)rnode->sysctl_data = t;
101 1.1 brad
102 1.1 brad return 0;
103 1.1 brad }
104 1.1 brad
105 1.1 brad static int
106 1.1 brad ds2482_set_pullup(i2c_tag_t tag, i2c_addr_t addr, bool activepullup,
107 1.1 brad bool strongpullup, int debuglevel)
108 1.1 brad {
109 1.1 brad int error;
110 1.1 brad uint8_t cmd = DS2482_WRITE_CONFIG;
111 1.1 brad uint8_t pu = 0;
112 1.1 brad uint8_t pux;
113 1.1 brad
114 1.1 brad if (activepullup == true)
115 1.1 brad pu = pu | DS2482_CONFIG_APU;
116 1.1 brad if (strongpullup == true)
117 1.1 brad pu = pu | DS2482_CONFIG_SPU;
118 1.1 brad
119 1.1 brad /* The Write Config command wants the top bits of the config buffer to be
120 1.1 brad * the ones complement of the lower bits.
121 1.1 brad */
122 1.1 brad
123 1.1 brad pux = ~(pu << 4);
124 1.1 brad pux = pux & 0xf0;
125 1.1 brad pu = pu | pux;
126 1.1 brad
127 1.1 brad error = iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &cmd, 1, &pu, 1, 0);
128 1.1 brad
129 1.1 brad DPRINTF2(debuglevel, 4, ("ds2482_set_pullup: pu: %02x ; error: %x %d\n", pu, error, error));
130 1.1 brad
131 1.1 brad return error;
132 1.1 brad }
133 1.1 brad
134 1.1 brad static int
135 1.1 brad ds2482_wait_with_status(i2c_tag_t tag, i2c_addr_t addr, uint8_t *status,
136 1.1 brad unsigned int d, bool set_pointer, int debuglevel)
137 1.1 brad {
138 1.1 brad int error = 0;
139 1.1 brad uint8_t xcmd, xbuf;
140 1.1 brad
141 1.1 brad DPRINTF2(debuglevel, 5, ("ds2482_wait_with_status: start\n"));
142 1.1 brad
143 1.1 brad xcmd = DS2482_SET_READ_POINTER;
144 1.1 brad xbuf = DS2482_REGISTER_STATUS;
145 1.1 brad if (set_pointer == true)
146 1.1 brad error = iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &xcmd, 1, &xbuf, 1, 0);
147 1.1 brad if (! error) {
148 1.1 brad error = iic_exec(tag, I2C_OP_READ, addr, NULL, 0, status, 1, 0);
149 1.1 brad if ((*status & DS2482_STATUS_1WB) && (! error)) {
150 1.1 brad do {
151 1.1 brad delay(d);
152 1.1 brad error = iic_exec(tag, I2C_OP_READ, addr, NULL, 0, status, 1, 0);
153 1.1 brad } while ((*status & DS2482_STATUS_1WB) &&
154 1.1 brad (! error));
155 1.1 brad }
156 1.1 brad }
157 1.1 brad
158 1.1 brad DPRINTF2(debuglevel, 5, ("ds2482_wait_with_status: end ; status: %02x %d ; error: %x %d\n", *status, *status, error, error));
159 1.1 brad
160 1.1 brad return error;
161 1.1 brad }
162 1.1 brad
163 1.1 brad static int
164 1.1 brad ds2482_cmd(i2c_tag_t tag, i2c_addr_t addr, uint8_t *cmd,
165 1.1 brad uint8_t *cmdarg, uint8_t *obuf, size_t obuflen, bool activepullup,
166 1.1 brad bool strongpullup, int debuglevel)
167 1.1 brad {
168 1.1 brad int error;
169 1.1 brad uint8_t xcmd;
170 1.1 brad uint8_t xbuf;
171 1.1 brad
172 1.1 brad switch (*cmd) {
173 1.1 brad /* The datasheet says that none of these are effected by what sort of pullup
174 1.1 brad * is set and only the Write Config command needs to happen when idle.
175 1.1 brad */
176 1.1 brad case DS2482_SET_READ_POINTER:
177 1.1 brad case DS2482_WRITE_CONFIG:
178 1.1 brad case DS2482_SELECT_CHANNEL:
179 1.1 brad KASSERT(cmdarg != NULL);
180 1.1 brad
181 1.1 brad error = 0;
182 1.1 brad
183 1.1 brad if (*cmd == DS2482_WRITE_CONFIG)
184 1.1 brad error = ds2482_wait_with_status(tag, addr, &xbuf, DS2482_QUICK_DELAY, true, debuglevel);
185 1.1 brad
186 1.1 brad if (! error)
187 1.1 brad error = iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, cmd, 1, cmdarg, 1, 0);
188 1.1 brad
189 1.1 brad DPRINTF2(debuglevel, 4, ("ds2482_cmd: cmd: %02x ; error: %x %d\n", *cmd, error, error));
190 1.1 brad
191 1.1 brad break;
192 1.1 brad
193 1.1 brad
194 1.1 brad case DS2482_DEVICE_RESET:
195 1.1 brad case DS2482_ONEWIRE_RESET:
196 1.1 brad /* Device reset resets everything, including pullup
197 1.1 brad * configuration settings, but that doesn't matter as we will
198 1.1 brad * always set the config before doing anything that actions on
199 1.1 brad * the 1-Wire bus.
200 1.1 brad *
201 1.1 brad * The data sheet warns about using the strong pull up feature
202 1.1 brad * with a 1-Wire reset, so we will simply not allow that
203 1.1 brad * combination.
204 1.1 brad *
205 1.1 brad * The data sheet does not mention if the 1-Wire reset effects
206 1.1 brad * just a single channel all channels. It seems likely that it
207 1.1 brad * is the currently active channel, and the driver works on that
208 1.1 brad * assumption.
209 1.1 brad */
210 1.1 brad
211 1.1 brad error = 0;
212 1.1 brad if (*cmd == DS2482_ONEWIRE_RESET) {
213 1.1 brad error = ds2482_wait_with_status(tag, addr, &xbuf, DS2482_QUICK_DELAY, true, debuglevel);
214 1.1 brad if (! error)
215 1.1 brad error = ds2482_set_pullup(tag, addr, activepullup, false, debuglevel);
216 1.1 brad }
217 1.1 brad
218 1.1 brad if (! error)
219 1.1 brad error = iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, cmd, 1, NULL, 0, 0);
220 1.1 brad
221 1.1 brad DPRINTF2(debuglevel, 4, ("ds2482_cmd: cmd: %02x ; error: %x %d\n", *cmd, error, error));
222 1.1 brad
223 1.1 brad if (*cmd == DS2482_DEVICE_RESET)
224 1.1 brad delay(1);
225 1.1 brad if (*cmd == DS2482_ONEWIRE_RESET)
226 1.1 brad delay(1300);
227 1.1 brad
228 1.1 brad break;
229 1.1 brad
230 1.1 brad case DS2482_ONEWIRE_SINGLE_BIT_WRITE:
231 1.1 brad KASSERT(cmdarg != NULL);
232 1.1 brad
233 1.1 brad DPRINTF2(debuglevel, 4, ("ds2482_cmd: DS2482_ONEWIRE_SINGLE_BIT_WRITE: cmdarg: %02x %d\n", *cmdarg, *cmdarg));
234 1.1 brad
235 1.1 brad xcmd = DS2482_SET_READ_POINTER;
236 1.1 brad xbuf = DS2482_REGISTER_STATUS;
237 1.1 brad error = iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &xcmd, 1, &xbuf, 1, 0);
238 1.1 brad if (! error)
239 1.1 brad error = ds2482_wait_with_status(tag, addr, &xbuf, DS2482_QUICK_DELAY, false, debuglevel);
240 1.1 brad
241 1.1 brad if (! error) {
242 1.1 brad xcmd = DS2482_ONEWIRE_SINGLE_BIT;
243 1.1 brad xbuf = DS2482_ONEWIRE_BIT_ZERO;
244 1.1 brad if (*cmdarg & 0x01)
245 1.1 brad xbuf = DS2482_ONEWIRE_BIT_ONE;
246 1.1 brad error = ds2482_set_pullup(tag, addr, activepullup, strongpullup, debuglevel);
247 1.1 brad if (! error)
248 1.1 brad error = iic_exec(tag, I2C_OP_WRITE, addr, &xcmd, 1, &xbuf, 1, 0);
249 1.1 brad if (! error) {
250 1.1 brad xbuf = 0xff;
251 1.1 brad error = ds2482_wait_with_status(tag, addr, &xbuf, DS2482_SLOW_DELAY, false, debuglevel);
252 1.1 brad }
253 1.1 brad }
254 1.1 brad break;
255 1.1 brad
256 1.1 brad case DS2482_ONEWIRE_SINGLE_BIT_READ:
257 1.1 brad KASSERT(obuf != NULL);
258 1.1 brad KASSERT(obuflen == 1);
259 1.1 brad
260 1.1 brad DPRINTF2(debuglevel, 4, ("ds2482_cmd: DS2482_ONEWIRE_SINGLE_BIT_READ\n"));
261 1.1 brad
262 1.1 brad xcmd = DS2482_SET_READ_POINTER;
263 1.1 brad xbuf = DS2482_REGISTER_STATUS;
264 1.1 brad error = iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &xcmd, 1, &xbuf, 1, 0);
265 1.1 brad if (! error)
266 1.1 brad error = ds2482_wait_with_status(tag, addr, &xbuf, DS2482_QUICK_DELAY, false, debuglevel);
267 1.1 brad
268 1.1 brad if (! error) {
269 1.1 brad xcmd = DS2482_ONEWIRE_SINGLE_BIT;
270 1.1 brad xbuf = DS2482_ONEWIRE_BIT_ONE;
271 1.1 brad error = ds2482_set_pullup(tag, addr, activepullup, strongpullup, debuglevel);
272 1.1 brad if (! error)
273 1.1 brad error = iic_exec(tag, I2C_OP_WRITE, addr, &xcmd, 1, &xbuf, 1, 0);
274 1.1 brad if (! error) {
275 1.1 brad xbuf = 0xff;
276 1.1 brad error = ds2482_wait_with_status(tag, addr, &xbuf, DS2482_SLOW_DELAY, false, debuglevel);
277 1.1 brad if (! error) {
278 1.1 brad *obuf = (xbuf & DS2482_STATUS_SBR) >> DS2482_STATUS_SBR_SHIFT;
279 1.1 brad }
280 1.1 brad }
281 1.1 brad }
282 1.1 brad break;
283 1.1 brad
284 1.1 brad case DS2482_ONEWIRE_WRITE_BYTE:
285 1.1 brad KASSERT(cmdarg != NULL);
286 1.1 brad
287 1.1 brad DPRINTF2(debuglevel, 4, ("ds2482_cmd: DS2482_ONEWIRE_WRITE_BYTE: cmdarg: %02x %d\n", *cmdarg, *cmdarg));
288 1.1 brad
289 1.1 brad xcmd = DS2482_SET_READ_POINTER;
290 1.1 brad xbuf = DS2482_REGISTER_STATUS;
291 1.1 brad error = iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &xcmd, 1, &xbuf, 1, 0);
292 1.1 brad if (! error)
293 1.1 brad error = ds2482_wait_with_status(tag, addr, &xbuf, DS2482_QUICK_DELAY, false, debuglevel);
294 1.1 brad
295 1.1 brad if (! error) {
296 1.1 brad error = ds2482_set_pullup(tag, addr, activepullup, strongpullup, debuglevel);
297 1.1 brad if (! error)
298 1.1 brad error = iic_exec(tag, I2C_OP_WRITE, addr, cmd, 1, cmdarg, 1, 0);
299 1.1 brad if (! error) {
300 1.1 brad xbuf = 0xff;
301 1.1 brad error = ds2482_wait_with_status(tag, addr, &xbuf, DS2482_SLOW_DELAY, false, debuglevel);
302 1.1 brad }
303 1.1 brad }
304 1.1 brad break;
305 1.1 brad
306 1.1 brad case DS2482_ONEWIRE_READ_BYTE:
307 1.1 brad KASSERT(obuf != NULL);
308 1.1 brad KASSERT(obuflen == 1);
309 1.1 brad
310 1.1 brad DPRINTF2(debuglevel, 4, ("ds2482_cmd: DS2482_ONEWIRE_READ_BYTE\n"));
311 1.1 brad
312 1.1 brad xcmd = DS2482_SET_READ_POINTER;
313 1.1 brad xbuf = DS2482_REGISTER_STATUS;
314 1.1 brad error = iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &xcmd, 1, &xbuf, 1, 0);
315 1.1 brad if (! error)
316 1.1 brad error = ds2482_wait_with_status(tag, addr, &xbuf, DS2482_QUICK_DELAY, false, debuglevel);
317 1.1 brad
318 1.1 brad if (! error) {
319 1.1 brad error = ds2482_set_pullup(tag, addr, activepullup, strongpullup, debuglevel);
320 1.1 brad if (! error)
321 1.1 brad error = iic_exec(tag, I2C_OP_WRITE, addr, cmd, 1, NULL, 0, 0);
322 1.1 brad if (! error) {
323 1.1 brad xbuf = 0xff;
324 1.1 brad error = ds2482_wait_with_status(tag, addr, &xbuf, DS2482_SLOW_DELAY, false, debuglevel);
325 1.1 brad if (! error) {
326 1.1 brad xcmd = DS2482_SET_READ_POINTER;
327 1.1 brad xbuf = DS2482_REGISTER_DATA;
328 1.1 brad error = iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &xcmd, 1, &xbuf, 1, 0);
329 1.1 brad if (! error) {
330 1.1 brad xbuf = 0xff;
331 1.1 brad error = iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, NULL, 0, &xbuf, 1, 0);
332 1.1 brad if (! error) {
333 1.1 brad *obuf = xbuf;
334 1.1 brad }
335 1.1 brad }
336 1.1 brad }
337 1.1 brad }
338 1.1 brad }
339 1.1 brad break;
340 1.1 brad
341 1.1 brad case DS2482_ONEWIRE_TRIPLET:
342 1.1 brad KASSERT(cmdarg != NULL);
343 1.1 brad KASSERT(obuf != NULL);
344 1.1 brad KASSERT(obuflen == 1);
345 1.1 brad
346 1.1 brad DPRINTF2(debuglevel, 4, ("ds2482_cmd: DS2482_ONEWIRE_TRIPLET: cmdarg: %02x %d\n", *cmdarg, *cmdarg));
347 1.1 brad
348 1.1 brad xcmd = DS2482_SET_READ_POINTER;
349 1.1 brad xbuf = DS2482_REGISTER_STATUS;
350 1.1 brad error = iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &xcmd, 1, &xbuf, 1, 0);
351 1.1 brad if (! error)
352 1.1 brad error = ds2482_wait_with_status(tag, addr, &xbuf, DS2482_QUICK_DELAY, false, debuglevel);
353 1.1 brad
354 1.1 brad if (! error) {
355 1.1 brad xbuf = DS2482_TRIPLET_DIR_ZERO;
356 1.1 brad if (*cmdarg & 0x01) {
357 1.1 brad xbuf = DS2482_TRIPLET_DIR_ONE;
358 1.1 brad }
359 1.1 brad error = ds2482_set_pullup(tag, addr, activepullup, strongpullup, debuglevel);
360 1.1 brad if (! error)
361 1.1 brad error = iic_exec(tag, I2C_OP_WRITE, addr, cmd, 1, &xbuf, 1, 0);
362 1.1 brad if (! error) {
363 1.1 brad xbuf = 0xff;
364 1.1 brad error = ds2482_wait_with_status(tag, addr, &xbuf, DS2482_SLOW_DELAY, false, debuglevel);
365 1.1 brad if (! error) {
366 1.1 brad /* This is undocumented anywhere I could find, but what has to be returned is
367 1.1 brad * 0x01 is the triplet path was taken, 0x02 is the Not-triplet path was taken,
368 1.1 brad * and 0x00 is neither was taken. The DIR bit in the status of the DS2482 may
369 1.1 brad * help with this some, but what is below seems to work.
370 1.1 brad */
371 1.1 brad *obuf = 0;
372 1.1 brad if (xbuf & DS2482_STATUS_TSB) {
373 1.1 brad *obuf = 0x01;
374 1.1 brad } else {
375 1.1 brad if (xbuf & DS2482_STATUS_SBR) {
376 1.1 brad *obuf = 0x02;
377 1.1 brad }
378 1.1 brad }
379 1.1 brad }
380 1.1 brad }
381 1.1 brad }
382 1.1 brad
383 1.1 brad break;
384 1.1 brad
385 1.1 brad default:
386 1.1 brad error = EINVAL;
387 1.1 brad break;
388 1.1 brad }
389 1.1 brad
390 1.1 brad return error;
391 1.1 brad }
392 1.1 brad
393 1.1 brad static int
394 1.1 brad ds2482_cmdr(struct ds2482ow_sc *sc, uint8_t cmd, uint8_t cmdarg, uint8_t *buf, size_t blen)
395 1.1 brad {
396 1.1 brad DPRINTF(sc, 3, ("%s: ds2482_cmdr: cmd: %02x\n",
397 1.1 brad device_xname(sc->sc_dev), cmd));
398 1.1 brad return ds2482_cmd(sc->sc_tag, sc->sc_addr, &cmd, &cmdarg, buf, blen, sc->sc_activepullup, sc->sc_strongpullup, sc->sc_ds2482debug);
399 1.1 brad }
400 1.1 brad
401 1.1 brad static const uint8_t ds2482_channels[] = {
402 1.1 brad DS2482_CHANNEL_IO0,
403 1.1 brad DS2482_CHANNEL_IO1,
404 1.1 brad DS2482_CHANNEL_IO2,
405 1.1 brad DS2482_CHANNEL_IO3,
406 1.1 brad DS2482_CHANNEL_IO4,
407 1.1 brad DS2482_CHANNEL_IO5,
408 1.1 brad DS2482_CHANNEL_IO6,
409 1.1 brad DS2482_CHANNEL_IO7
410 1.1 brad };
411 1.1 brad
412 1.1 brad static int
413 1.1 brad ds2482_set_channel(struct ds2482ow_sc *sc, int channel)
414 1.1 brad {
415 1.1 brad int error = 0;
416 1.1 brad
417 1.1 brad KASSERT(channel >= 0 && channel < DS2482_NUM_INSTANCES);
418 1.1 brad
419 1.1 brad if (sc->sc_is_800 == true)
420 1.1 brad error = ds2482_cmdr(sc, DS2482_SELECT_CHANNEL, ds2482_channels[channel], NULL, 0);
421 1.1 brad
422 1.1 brad return error;
423 1.1 brad }
424 1.1 brad
425 1.1 brad static int
426 1.1 brad ds2482_poke(i2c_tag_t tag, i2c_addr_t addr, bool matchdebug)
427 1.1 brad {
428 1.1 brad uint8_t reg = DS2482_SET_READ_POINTER;
429 1.1 brad uint8_t rbuf = DS2482_REGISTER_STATUS;
430 1.1 brad uint8_t obuf;
431 1.1 brad int error;
432 1.1 brad
433 1.1 brad error = ds2482_cmd(tag, addr, ®, &rbuf, &obuf, 1, false, false, 0);
434 1.1 brad if (matchdebug) {
435 1.1 brad printf("poke X 1: %d\n", error);
436 1.1 brad }
437 1.1 brad return error;
438 1.1 brad }
439 1.1 brad
440 1.1 brad static int
441 1.1 brad ds2482_match(device_t parent, cfdata_t match, void *aux)
442 1.1 brad {
443 1.1 brad struct i2c_attach_args *ia = aux;
444 1.1 brad int error, match_result;
445 1.1 brad const bool matchdebug = false;
446 1.1 brad
447 1.1 brad if (iic_use_direct_match(ia, match, NULL, &match_result))
448 1.1 brad return match_result;
449 1.1 brad
450 1.1 brad /* indirect config - check for configured address */
451 1.1 brad if (!(ia->ia_addr >= DS2482_LOWEST_ADDR &&
452 1.1 brad ia->ia_addr <= DS2482_HIGHEST_ADDR))
453 1.1 brad return 0;
454 1.1 brad
455 1.1 brad /*
456 1.1 brad * Check to see if something is really at this i2c address. This will
457 1.1 brad * keep phantom devices from appearing
458 1.1 brad */
459 1.1 brad if (iic_acquire_bus(ia->ia_tag, 0) != 0) {
460 1.1 brad if (matchdebug)
461 1.1 brad printf("in match acquire bus failed\n");
462 1.1 brad return 0;
463 1.1 brad }
464 1.1 brad
465 1.1 brad error = ds2482_poke(ia->ia_tag, ia->ia_addr, matchdebug);
466 1.1 brad iic_release_bus(ia->ia_tag, 0);
467 1.1 brad
468 1.1 brad return error == 0 ? I2C_MATCH_ADDRESS_AND_PROBE : 0;
469 1.1 brad }
470 1.1 brad
471 1.1 brad static void
472 1.1 brad ds2482_attach(device_t parent, device_t self, void *aux)
473 1.1 brad {
474 1.1 brad struct ds2482ow_sc *sc;
475 1.1 brad struct i2c_attach_args *ia;
476 1.1 brad int error, i, num_channels = 1;
477 1.1 brad struct onewirebus_attach_args oba;
478 1.1 brad const struct sysctlnode *cnode;
479 1.1 brad int sysctlroot_num, pullup_num;
480 1.1 brad
481 1.1 brad ia = aux;
482 1.1 brad sc = device_private(self);
483 1.1 brad
484 1.1 brad sc->sc_dev = self;
485 1.1 brad sc->sc_tag = ia->ia_tag;
486 1.1 brad sc->sc_addr = ia->ia_addr;
487 1.1 brad sc->sc_ds2482debug = 0;
488 1.1 brad sc->sc_activepullup = false;
489 1.1 brad sc->sc_strongpullup = false;
490 1.1 brad sc->sc_is_800 = false;
491 1.1 brad
492 1.1 brad aprint_normal("\n");
493 1.1 brad
494 1.1 brad mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
495 1.1 brad
496 1.1 brad if ((error = sysctl_createv(&sc->sc_ds2482log, 0, NULL, &cnode,
497 1.1 brad 0, CTLTYPE_NODE, device_xname(sc->sc_dev),
498 1.1 brad SYSCTL_DESCR("DS2482 controls"), NULL, 0, NULL, 0, CTL_HW,
499 1.1 brad CTL_CREATE, CTL_EOL)) != 0)
500 1.1 brad goto out;
501 1.1 brad
502 1.1 brad sysctlroot_num = cnode->sysctl_num;
503 1.1 brad
504 1.1 brad #ifdef DS2482_DEBUG
505 1.1 brad if ((error = sysctl_createv(&sc->sc_ds2482log, 0, NULL, &cnode,
506 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "debug",
507 1.1 brad SYSCTL_DESCR("Debug level"), ds2482_verify_sysctl, 0,
508 1.1 brad &sc->sc_ds2482debug, 0, CTL_HW, sysctlroot_num, CTL_CREATE,
509 1.1 brad CTL_EOL)) != 0)
510 1.1 brad goto out;
511 1.1 brad #endif
512 1.1 brad
513 1.1 brad if ((error = sysctl_createv(&sc->sc_ds2482log, 0, NULL, &cnode,
514 1.1 brad 0, CTLTYPE_NODE, "pullup",
515 1.1 brad SYSCTL_DESCR("Pullup controls"), NULL, 0, NULL, 0, CTL_HW,
516 1.1 brad sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
517 1.1 brad goto out;
518 1.1 brad
519 1.1 brad pullup_num = cnode->sysctl_num;
520 1.1 brad
521 1.1 brad if ((error = sysctl_createv(&sc->sc_ds2482log, 0, NULL, &cnode,
522 1.1 brad CTLFLAG_READWRITE, CTLTYPE_BOOL, "active",
523 1.1 brad SYSCTL_DESCR("Active pullup"), NULL, 0, &sc->sc_activepullup,
524 1.1 brad 0, CTL_HW, sysctlroot_num, pullup_num, CTL_CREATE, CTL_EOL)) != 0)
525 1.1 brad goto out;
526 1.1 brad
527 1.1 brad if ((error = sysctl_createv(&sc->sc_ds2482log, 0, NULL, &cnode,
528 1.1 brad CTLFLAG_READWRITE, CTLTYPE_BOOL, "strong",
529 1.1 brad SYSCTL_DESCR("Strong pullup"), NULL, 0, &sc->sc_strongpullup,
530 1.1 brad 0, CTL_HW, sysctlroot_num, pullup_num, CTL_CREATE, CTL_EOL)) != 0)
531 1.1 brad goto out;
532 1.1 brad
533 1.1 brad error = iic_acquire_bus(sc->sc_tag, 0);
534 1.1 brad if (error) {
535 1.1 brad aprint_error_dev(self, "Could not acquire iic bus: %d\n",
536 1.1 brad error);
537 1.1 brad goto out;
538 1.1 brad }
539 1.1 brad
540 1.1 brad error = ds2482_cmdr(sc, DS2482_DEVICE_RESET, 0, NULL, 0);
541 1.1 brad if (error != 0)
542 1.1 brad aprint_error_dev(self, "Reset failed: %d\n", error);
543 1.1 brad
544 1.1 brad if (! error) {
545 1.1 brad int xerror;
546 1.1 brad xerror = ds2482_cmdr(sc, DS2482_SELECT_CHANNEL, DS2482_CHANNEL_IO0, NULL, 0);
547 1.1 brad if (! xerror)
548 1.1 brad sc->sc_is_800 = true;
549 1.1 brad }
550 1.1 brad
551 1.1 brad iic_release_bus(sc->sc_tag, 0);
552 1.1 brad
553 1.1 brad if (error != 0) {
554 1.1 brad aprint_error_dev(self, "Unable to setup device\n");
555 1.1 brad goto out;
556 1.1 brad }
557 1.1 brad
558 1.1 brad if (sc->sc_is_800 == true) {
559 1.1 brad num_channels = DS2482_NUM_INSTANCES;
560 1.1 brad }
561 1.1 brad
562 1.1 brad aprint_normal_dev(self, "Maxim DS2482-%s I2C to 1-Wire bridge, Channels available: %d\n",
563 1.1 brad (sc->sc_is_800 == true) ? "800" : "100",
564 1.1 brad num_channels);
565 1.1 brad
566 1.1 brad for(i = 0;i < num_channels;i++) {
567 1.1 brad sc->sc_instances[i].sc_i_channel = i;
568 1.1 brad sc->sc_instances[i].sc = sc;
569 1.1 brad sc->sc_instances[i].sc_i_ow_bus.bus_cookie = &sc->sc_instances[i];
570 1.1 brad sc->sc_instances[i].sc_i_ow_bus.bus_reset = ds2482_ow_reset;
571 1.1 brad sc->sc_instances[i].sc_i_ow_bus.bus_read_bit = ds2482_ow_read_bit;
572 1.1 brad sc->sc_instances[i].sc_i_ow_bus.bus_write_bit = ds2482_ow_write_bit;
573 1.1 brad sc->sc_instances[i].sc_i_ow_bus.bus_read_byte = ds2482_ow_read_byte;
574 1.1 brad sc->sc_instances[i].sc_i_ow_bus.bus_write_byte = ds2482_ow_write_byte;
575 1.1 brad sc->sc_instances[i].sc_i_ow_bus.bus_triplet = ds2482_ow_triplet;
576 1.1 brad
577 1.1 brad memset(&oba, 0, sizeof(oba));
578 1.1 brad oba.oba_bus = &sc->sc_instances[i].sc_i_ow_bus;
579 1.1 brad sc->sc_instances[i].sc_i_ow_dev = config_found(self, &oba, onewirebus_print, CFARGS_NONE);
580 1.1 brad }
581 1.1 brad
582 1.1 brad out:
583 1.1 brad return;
584 1.1 brad }
585 1.1 brad
586 1.1 brad /* Hmmm... except in the case of reset, there really doesn't seem to be any
587 1.1 brad * way with the onewire(4) API to indicate an error condition.
588 1.1 brad */
589 1.1 brad
590 1.1 brad static int
591 1.1 brad ds2482_generic_action(struct ds2482_instance *sci, uint8_t cmd, uint8_t cmdarg, uint8_t *buf, size_t blen)
592 1.1 brad {
593 1.1 brad struct ds2482ow_sc *sc = sci->sc;
594 1.1 brad int rv;
595 1.1 brad
596 1.1 brad mutex_enter(&sc->sc_mutex);
597 1.1 brad rv = iic_acquire_bus(sc->sc_tag, 0);
598 1.1 brad if (!rv) {
599 1.1 brad rv = ds2482_set_channel(sc, sci->sc_i_channel);
600 1.1 brad if (!rv)
601 1.1 brad rv = ds2482_cmdr(sc, cmd, cmdarg, buf, blen);
602 1.1 brad }
603 1.1 brad iic_release_bus(sc->sc_tag, 0);
604 1.1 brad mutex_exit(&sc->sc_mutex);
605 1.1 brad
606 1.1 brad return rv;
607 1.1 brad }
608 1.1 brad
609 1.1 brad static int
610 1.1 brad ds2482_ow_reset(void *arg)
611 1.1 brad {
612 1.1 brad struct ds2482_instance *sci = arg;
613 1.1 brad struct ds2482ow_sc *sc = sci->sc;
614 1.1 brad int rv;
615 1.1 brad
616 1.1 brad rv = ds2482_generic_action(sci, DS2482_ONEWIRE_RESET, 0, NULL, 0);
617 1.1 brad
618 1.1 brad DPRINTF(sc, 3, ("%s: ds2482_ow_reset: channel: %d ; rv: %x %d\n",
619 1.1 brad device_xname(sc->sc_dev), sci->sc_i_channel, rv, rv));
620 1.1 brad
621 1.1 brad return rv;
622 1.1 brad }
623 1.1 brad
624 1.1 brad static int
625 1.1 brad ds2482_ow_read_bit(void *arg)
626 1.1 brad {
627 1.1 brad struct ds2482_instance *sci = arg;
628 1.1 brad struct ds2482ow_sc *sc = sci->sc;
629 1.1 brad int rv;
630 1.1 brad uint8_t buf = 0x55;
631 1.1 brad
632 1.1 brad rv = ds2482_generic_action(sci, DS2482_ONEWIRE_SINGLE_BIT_READ, 0, &buf, 1);
633 1.1 brad
634 1.1 brad DPRINTF(sc, 3, ("%s: ds2482_read_bit: channel: %d ; rv: %x %d ; buf: %02x %d\n",
635 1.1 brad device_xname(sc->sc_dev), sci->sc_i_channel, rv, rv, buf, buf));
636 1.1 brad
637 1.1 brad return (int)buf;
638 1.1 brad }
639 1.1 brad
640 1.1 brad static void
641 1.1 brad ds2482_ow_write_bit(void *arg, int value)
642 1.1 brad {
643 1.1 brad struct ds2482_instance *sci = arg;
644 1.1 brad struct ds2482ow_sc *sc = sci->sc;
645 1.1 brad int rv;
646 1.1 brad
647 1.1 brad rv = ds2482_generic_action(sci, DS2482_ONEWIRE_SINGLE_BIT_WRITE, (uint8_t)value, NULL, 0);
648 1.1 brad
649 1.1 brad DPRINTF(sc, 3, ("%s: ds2482_write_bit: channel: %d ; rv: %x %d ; value: %02x %d\n",
650 1.1 brad device_xname(sc->sc_dev), sci->sc_i_channel, rv, rv, (uint8_t)value, (uint8_t)value));
651 1.1 brad
652 1.1 brad return;
653 1.1 brad }
654 1.1 brad
655 1.1 brad static int
656 1.1 brad ds2482_ow_read_byte(void *arg)
657 1.1 brad {
658 1.1 brad struct ds2482_instance *sci = arg;
659 1.1 brad uint8_t buf = 0x55;
660 1.1 brad struct ds2482ow_sc *sc = sci->sc;
661 1.1 brad int rv;
662 1.1 brad
663 1.1 brad rv = ds2482_generic_action(sci, DS2482_ONEWIRE_READ_BYTE, 0, &buf, 1);
664 1.1 brad
665 1.1 brad DPRINTF(sc, 3, ("%s: ds2482_read_byte: channel: %d ; rv: %x %d ; buf: %02x %d\n",
666 1.1 brad device_xname(sc->sc_dev), sci->sc_i_channel, rv, rv, buf, buf));
667 1.1 brad
668 1.1 brad return (int)buf;
669 1.1 brad }
670 1.1 brad
671 1.1 brad static void
672 1.1 brad ds2482_ow_write_byte(void *arg, int value)
673 1.1 brad {
674 1.1 brad struct ds2482_instance *sci = arg;
675 1.1 brad struct ds2482ow_sc *sc = sci->sc;
676 1.1 brad int rv;
677 1.1 brad
678 1.1 brad rv = ds2482_generic_action(sci, DS2482_ONEWIRE_WRITE_BYTE, (uint8_t)value, NULL, 0);
679 1.1 brad
680 1.1 brad DPRINTF(sc, 3, ("%s: ds2482_write_byte: channel: %d ; rv: %x %d ; value: %02x %d\n",
681 1.1 brad device_xname(sc->sc_dev), sci->sc_i_channel, rv, rv, (uint8_t)value, (uint8_t)value));
682 1.1 brad
683 1.1 brad return;
684 1.1 brad }
685 1.1 brad
686 1.1 brad static int
687 1.1 brad ds2482_ow_triplet(void *arg, int dir)
688 1.1 brad {
689 1.1 brad struct ds2482_instance *sci = arg;
690 1.1 brad uint8_t buf = 0x55;
691 1.1 brad struct ds2482ow_sc *sc = sci->sc;
692 1.1 brad int rv;
693 1.1 brad
694 1.1 brad rv = ds2482_generic_action(sci, DS2482_ONEWIRE_TRIPLET, (uint8_t) dir, &buf, 1);
695 1.1 brad
696 1.1 brad DPRINTF(sc, 3, ("%s: ds2482_triplet: channel: %d ; rv: %x %d ; dir: %x %d ; buf: %02x %d\n",
697 1.1 brad device_xname(sc->sc_dev), sci->sc_i_channel, rv, rv, dir, dir, (uint8_t)buf, (uint8_t)buf));
698 1.1 brad
699 1.1 brad return (int)buf;
700 1.1 brad }
701 1.1 brad
702 1.1 brad static int
703 1.1 brad ds2482_detach(device_t self, int flags)
704 1.1 brad {
705 1.1 brad struct ds2482ow_sc *sc;
706 1.1 brad
707 1.1 brad sc = device_private(self);
708 1.1 brad
709 1.1 brad /* Remove the sysctl tree */
710 1.1 brad sysctl_teardown(&sc->sc_ds2482log);
711 1.1 brad
712 1.1 brad /* Remove the mutex */
713 1.1 brad mutex_destroy(&sc->sc_mutex);
714 1.1 brad
715 1.1 brad return 0;
716 1.1 brad }
717 1.1 brad
718 1.1 brad MODULE(MODULE_CLASS_DRIVER, ds2482ow, "iic,onewire");
719 1.1 brad
720 1.1 brad #ifdef _MODULE
721 1.1 brad #include "ioconf.c"
722 1.1 brad #endif
723 1.1 brad
724 1.1 brad static int
725 1.1 brad ds2482ow_modcmd(modcmd_t cmd, void *opaque)
726 1.1 brad {
727 1.1 brad
728 1.1 brad switch (cmd) {
729 1.1 brad case MODULE_CMD_INIT:
730 1.1 brad #ifdef _MODULE
731 1.1 brad return config_init_component(cfdriver_ioconf_ds2482ow,
732 1.1 brad cfattach_ioconf_ds2482ow, cfdata_ioconf_ds2482ow);
733 1.1 brad #else
734 1.1 brad return 0;
735 1.1 brad #endif
736 1.1 brad case MODULE_CMD_FINI:
737 1.1 brad #ifdef _MODULE
738 1.1 brad return config_fini_component(cfdriver_ioconf_ds2482ow,
739 1.1 brad cfattach_ioconf_ds2482ow, cfdata_ioconf_ds2482ow);
740 1.1 brad #else
741 1.1 brad return 0;
742 1.1 brad #endif
743 1.1 brad default:
744 1.1 brad return ENOTTY;
745 1.1 brad }
746 1.1 brad }
747