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