ki2c.c revision 1.35 1 1.35 macallan /* $NetBSD: ki2c.c,v 1.35 2025/07/05 12:58:05 macallan Exp $ */
2 1.1 grant /* Id: ki2c.c,v 1.7 2002/10/05 09:56:05 tsubai Exp */
3 1.1 grant
4 1.1 grant /*-
5 1.1 grant * Copyright (c) 2001 Tsubai Masanari. All rights reserved.
6 1.1 grant *
7 1.1 grant * Redistribution and use in source and binary forms, with or without
8 1.1 grant * modification, are permitted provided that the following conditions
9 1.1 grant * are met:
10 1.1 grant * 1. Redistributions of source code must retain the above copyright
11 1.1 grant * notice, this list of conditions and the following disclaimer.
12 1.1 grant * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 grant * notice, this list of conditions and the following disclaimer in the
14 1.1 grant * documentation and/or other materials provided with the distribution.
15 1.1 grant * 3. The name of the author may not be used to endorse or promote products
16 1.1 grant * derived from this software without specific prior written permission.
17 1.1 grant *
18 1.1 grant * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 grant * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 grant * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 grant * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 grant * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 grant * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 grant * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 grant * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 grant * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 1.1 grant * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 grant */
29 1.1 grant
30 1.1 grant #include <sys/param.h>
31 1.1 grant #include <sys/device.h>
32 1.1 grant #include <sys/systm.h>
33 1.11 ad #include <sys/mutex.h>
34 1.1 grant
35 1.1 grant #include <dev/ofw/openfirm.h>
36 1.1 grant #include <machine/autoconf.h>
37 1.1 grant
38 1.29 macallan #include "opt_ki2c.h"
39 1.3 macallan #include <macppc/dev/ki2cvar.h>
40 1.1 grant
41 1.20 macallan #ifdef KI2C_DEBUG
42 1.20 macallan #define DPRINTF printf
43 1.20 macallan #else
44 1.20 macallan #define DPRINTF while (0) printf
45 1.20 macallan #endif
46 1.20 macallan
47 1.33 mlelstv #define KI2C_EXEC_MAX_CMDLEN 32
48 1.33 mlelstv #define KI2C_EXEC_MAX_BUFLEN 32
49 1.33 mlelstv
50 1.17 matt int ki2c_match(device_t, cfdata_t, void *);
51 1.17 matt void ki2c_attach(device_t, device_t, void *);
52 1.21 macallan inline uint8_t ki2c_readreg(struct ki2c_softc *, int);
53 1.21 macallan inline void ki2c_writereg(struct ki2c_softc *, int, uint8_t);
54 1.1 grant u_int ki2c_getmode(struct ki2c_softc *);
55 1.1 grant void ki2c_setmode(struct ki2c_softc *, u_int);
56 1.1 grant u_int ki2c_getspeed(struct ki2c_softc *);
57 1.1 grant void ki2c_setspeed(struct ki2c_softc *, u_int);
58 1.35 macallan int ki2c_intr(void *);
59 1.1 grant int ki2c_poll(struct ki2c_softc *, int);
60 1.1 grant int ki2c_start(struct ki2c_softc *, int, int, void *, int);
61 1.1 grant int ki2c_read(struct ki2c_softc *, int, int, void *, int);
62 1.3 macallan int ki2c_write(struct ki2c_softc *, int, int, void *, int);
63 1.3 macallan
64 1.3 macallan /* I2C glue */
65 1.3 macallan static int ki2c_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
66 1.3 macallan void *, size_t, int);
67 1.3 macallan
68 1.1 grant
69 1.18 macallan CFATTACH_DECL_NEW(ki2c, sizeof(struct ki2c_softc), ki2c_match, ki2c_attach,
70 1.9 dogcow NULL, NULL);
71 1.1 grant
72 1.1 grant int
73 1.17 matt ki2c_match(device_t parent, cfdata_t match, void *aux)
74 1.1 grant {
75 1.1 grant struct confargs *ca = aux;
76 1.1 grant
77 1.1 grant if (strcmp(ca->ca_name, "i2c") == 0)
78 1.1 grant return 1;
79 1.1 grant
80 1.1 grant return 0;
81 1.1 grant }
82 1.1 grant
83 1.1 grant void
84 1.17 matt ki2c_attach(device_t parent, device_t self, void *aux)
85 1.1 grant {
86 1.17 matt struct ki2c_softc *sc = device_private(self);
87 1.1 grant struct confargs *ca = aux;
88 1.1 grant int node = ca->ca_node;
89 1.35 macallan uint32_t addr, channel, reg, intr[2];
90 1.28 macallan int rate, child, /*namelen,*/ i2cbus[2] = {0, 0};
91 1.3 macallan struct i2cbus_attach_args iba;
92 1.20 macallan prop_dictionary_t dict = device_properties(self);
93 1.20 macallan prop_array_t cfg;
94 1.26 macallan int devs, devc;
95 1.26 macallan char compat[256], num[8], descr[32];
96 1.22 macallan prop_dictionary_t dev;
97 1.22 macallan prop_data_t data;
98 1.35 macallan char name[32], intr_xname[32];
99 1.18 macallan
100 1.18 macallan sc->sc_dev = self;
101 1.21 macallan sc->sc_tag = ca->ca_tag;
102 1.1 grant ca->ca_reg[0] += ca->ca_baseaddr;
103 1.1 grant
104 1.1 grant if (OF_getprop(node, "AAPL,i2c-rate", &rate, 4) != 4) {
105 1.20 macallan aprint_error(": cannot get i2c-rate\n");
106 1.1 grant return;
107 1.1 grant }
108 1.20 macallan if (OF_getprop(node, "AAPL,address", &addr, 4) != 4) {
109 1.20 macallan aprint_error(": unable to find i2c address\n");
110 1.1 grant return;
111 1.1 grant }
112 1.21 macallan if (bus_space_map(sc->sc_tag, addr, PAGE_SIZE, 0, &sc->sc_bh) != 0) {
113 1.21 macallan aprint_error_dev(sc->sc_dev, "failed to map registers\n");
114 1.21 macallan return;
115 1.21 macallan }
116 1.21 macallan
117 1.1 grant if (OF_getprop(node, "AAPL,address-step", &sc->sc_regstep, 4) != 4) {
118 1.20 macallan aprint_error(": unable to find i2c address step\n");
119 1.1 grant return;
120 1.1 grant }
121 1.1 grant
122 1.35 macallan if(OF_getprop(node, "interrupts", intr, 8) != 8) {
123 1.35 macallan aprint_error(": can't find interrupt\n");
124 1.35 macallan return;
125 1.35 macallan }
126 1.35 macallan
127 1.35 macallan aprint_normal(" irq %d", intr[0]);
128 1.35 macallan
129 1.1 grant printf("\n");
130 1.1 grant
131 1.1 grant ki2c_writereg(sc, STATUS, 0);
132 1.1 grant ki2c_writereg(sc, ISR, 0);
133 1.1 grant ki2c_writereg(sc, IER, 0);
134 1.1 grant
135 1.1 grant ki2c_setmode(sc, I2C_STDSUBMODE);
136 1.1 grant ki2c_setspeed(sc, I2C_100kHz); /* XXX rate */
137 1.3 macallan
138 1.3 macallan ki2c_writereg(sc, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
139 1.3 macallan
140 1.20 macallan cfg = prop_array_create();
141 1.20 macallan prop_dictionary_set(dict, "i2c-child-devices", cfg);
142 1.20 macallan prop_object_release(cfg);
143 1.20 macallan
144 1.4 macallan /*
145 1.4 macallan * newer OF puts I2C devices under 'i2c-bus' instead of attaching them
146 1.4 macallan * directly to the ki2c node so we just check if we have a child named
147 1.25 macallan * 'i2c-bus' and if so we attach its children, not ours
148 1.25 macallan *
149 1.25 macallan * XXX
150 1.25 macallan * should probably check for multiple i2c-bus children
151 1.4 macallan */
152 1.28 macallan
153 1.28 macallan int found_busnode = 0;
154 1.25 macallan channel = 0;
155 1.4 macallan child = OF_child(node);
156 1.28 macallan while (child != 0) {
157 1.4 macallan OF_getprop(child, "name", name, sizeof(name));
158 1.25 macallan if (strcmp(name, "i2c-bus") == 0) {
159 1.25 macallan OF_getprop(child, "reg", &channel, sizeof(channel));
160 1.28 macallan i2cbus[channel] = child;
161 1.25 macallan DPRINTF("found channel %x\n", channel);
162 1.28 macallan found_busnode = 1;
163 1.25 macallan }
164 1.4 macallan child = OF_peer(child);
165 1.4 macallan }
166 1.28 macallan if (found_busnode == 0)
167 1.28 macallan i2cbus[0] = node;
168 1.22 macallan
169 1.28 macallan for (channel = 0; channel < 2; channel++) {
170 1.28 macallan devs = OF_child(i2cbus[channel]);
171 1.28 macallan while (devs != 0) {
172 1.28 macallan if (OF_getprop(devs, "name", name, 32) <= 0)
173 1.22 macallan goto skip;
174 1.28 macallan if (OF_getprop(devs, "compatible", compat, 256) <= 0) {
175 1.28 macallan /* some i2c device nodes don't have 'compatible' */
176 1.28 macallan memset(compat, 0, 256);
177 1.28 macallan strncpy(compat, name, 256);
178 1.28 macallan }
179 1.28 macallan if (OF_getprop(devs, "reg", &addr, 4) <= 0)
180 1.28 macallan if (OF_getprop(devs, "i2c-address", &addr, 4) <= 0)
181 1.28 macallan goto skip;
182 1.28 macallan addr |= channel << 8;
183 1.28 macallan addr = addr >> 1;
184 1.28 macallan DPRINTF("-> %s@%x\n", name, addr);
185 1.28 macallan dev = prop_dictionary_create();
186 1.30 macallan prop_dictionary_set_string(dev, "name", name);
187 1.30 macallan data = prop_data_create_copy(compat, strlen(compat)+1);
188 1.28 macallan prop_dictionary_set(dev, "compatible", data);
189 1.28 macallan prop_object_release(data);
190 1.28 macallan prop_dictionary_set_uint32(dev, "addr", addr);
191 1.28 macallan prop_dictionary_set_uint64(dev, "cookie", devs);
192 1.28 macallan /* look for location info for sensors */
193 1.28 macallan devc = OF_child(devs);
194 1.34 macallan if (devc == 0) {
195 1.34 macallan /* old style name info */
196 1.34 macallan uint32_t ids[4];
197 1.34 macallan int len = OF_getprop(devs, "hwsensor-id", ids, 16);
198 1.34 macallan int i = 0, idx = 0;
199 1.34 macallan char buffer[256];
200 1.34 macallan memset(buffer, 0, 256);
201 1.34 macallan OF_getprop(devs, "hwsensor-location", buffer, 256);
202 1.34 macallan while (len > 0) {
203 1.34 macallan reg = ids[i];
204 1.34 macallan strcpy(descr, &buffer[idx]);
205 1.34 macallan idx += strlen(descr) + 1;
206 1.34 macallan DPRINTF("found '%s' at %02x\n", descr, reg);
207 1.34 macallan snprintf(num, 7, "s%02x", i);
208 1.34 macallan prop_dictionary_set_string(dev, num, descr);
209 1.34 macallan i++;
210 1.34 macallan len -= 4;
211 1.34 macallan }
212 1.34 macallan } else {
213 1.34 macallan while (devc != 0) {
214 1.34 macallan if (OF_getprop(devc, "reg", ®, 4) < 4) goto nope;
215 1.34 macallan if (OF_getprop(devc, "location", descr, 32) <= 0)
216 1.34 macallan goto nope;
217 1.34 macallan DPRINTF("found '%s' at %02x\n", descr, reg);
218 1.34 macallan snprintf(num, 7, "s%02x", reg);
219 1.34 macallan prop_dictionary_set_string(dev, num, descr);
220 1.34 macallan nope:
221 1.34 macallan devc = OF_peer(devc);
222 1.35 macallan }
223 1.28 macallan }
224 1.34 macallan
225 1.28 macallan prop_array_add(cfg, dev);
226 1.28 macallan prop_object_release(dev);
227 1.28 macallan skip:
228 1.28 macallan devs = OF_peer(devs);
229 1.26 macallan }
230 1.3 macallan }
231 1.3 macallan
232 1.35 macallan cv_init(&sc->sc_todev, device_xname(self));
233 1.35 macallan mutex_init(&sc->sc_todevmtx, MUTEX_DEFAULT, IPL_NONE);
234 1.35 macallan
235 1.35 macallan snprintf(intr_xname, sizeof(intr_xname), "%s intr", device_xname(self));
236 1.35 macallan intr_establish_xname(intr[0], (intr[1] & 1) ? IST_LEVEL : IST_EDGE,
237 1.35 macallan IPL_BIO, ki2c_intr, sc, intr_xname);
238 1.35 macallan
239 1.35 macallan ki2c_writereg(sc, IER, I2C_INT_ALL);
240 1.35 macallan
241 1.22 macallan /* fill in the i2c tag */
242 1.27 thorpej iic_tag_init(&sc->sc_i2c);
243 1.22 macallan sc->sc_i2c.ic_cookie = sc;
244 1.22 macallan sc->sc_i2c.ic_exec = ki2c_i2c_exec;
245 1.3 macallan
246 1.22 macallan memset(&iba, 0, sizeof(iba));
247 1.22 macallan iba.iba_tag = &sc->sc_i2c;
248 1.32 thorpej config_found(sc->sc_dev, &iba, iicbus_print, CFARGS_NONE);
249 1.22 macallan
250 1.1 grant }
251 1.1 grant
252 1.21 macallan uint8_t
253 1.14 dsl ki2c_readreg(struct ki2c_softc *sc, int reg)
254 1.1 grant {
255 1.1 grant
256 1.21 macallan return bus_space_read_1(sc->sc_tag, sc->sc_bh, sc->sc_regstep * reg);
257 1.1 grant }
258 1.1 grant
259 1.1 grant void
260 1.21 macallan ki2c_writereg(struct ki2c_softc *sc, int reg, uint8_t val)
261 1.1 grant {
262 1.21 macallan
263 1.21 macallan bus_space_write_1(sc->sc_tag, sc->sc_bh, reg * sc->sc_regstep, val);
264 1.1 grant delay(10);
265 1.1 grant }
266 1.1 grant
267 1.1 grant u_int
268 1.14 dsl ki2c_getmode(struct ki2c_softc *sc)
269 1.1 grant {
270 1.1 grant return ki2c_readreg(sc, MODE) & I2C_MODE;
271 1.1 grant }
272 1.1 grant
273 1.1 grant void
274 1.14 dsl ki2c_setmode(struct ki2c_softc *sc, u_int mode)
275 1.1 grant {
276 1.25 macallan ki2c_writereg(sc, MODE, mode);
277 1.1 grant }
278 1.1 grant
279 1.1 grant u_int
280 1.14 dsl ki2c_getspeed(struct ki2c_softc *sc)
281 1.1 grant {
282 1.1 grant return ki2c_readreg(sc, MODE) & I2C_SPEED;
283 1.1 grant }
284 1.1 grant
285 1.1 grant void
286 1.14 dsl ki2c_setspeed(struct ki2c_softc *sc, u_int speed)
287 1.1 grant {
288 1.1 grant u_int x;
289 1.1 grant
290 1.1 grant KASSERT((speed & ~I2C_SPEED) == 0);
291 1.1 grant x = ki2c_readreg(sc, MODE);
292 1.1 grant x &= ~I2C_SPEED;
293 1.1 grant x |= speed;
294 1.1 grant ki2c_writereg(sc, MODE, x);
295 1.1 grant }
296 1.1 grant
297 1.1 grant int
298 1.35 macallan ki2c_intr(void *cookie)
299 1.1 grant {
300 1.35 macallan struct ki2c_softc *sc = cookie;
301 1.1 grant u_int isr, x;
302 1.1 grant
303 1.1 grant isr = ki2c_readreg(sc, ISR);
304 1.1 grant if (isr & I2C_INT_ADDR) {
305 1.1 grant #if 0
306 1.1 grant if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
307 1.1 grant /* No slave responded. */
308 1.1 grant sc->sc_flags |= I2C_ERROR;
309 1.1 grant goto out;
310 1.1 grant }
311 1.1 grant #endif
312 1.1 grant
313 1.1 grant if (sc->sc_flags & I2C_READING) {
314 1.1 grant if (sc->sc_resid > 1) {
315 1.1 grant x = ki2c_readreg(sc, CONTROL);
316 1.1 grant x |= I2C_CT_AAK;
317 1.1 grant ki2c_writereg(sc, CONTROL, x);
318 1.1 grant }
319 1.1 grant } else {
320 1.1 grant ki2c_writereg(sc, DATA, *sc->sc_data++);
321 1.1 grant sc->sc_resid--;
322 1.1 grant }
323 1.1 grant }
324 1.1 grant
325 1.1 grant if (isr & I2C_INT_DATA) {
326 1.1 grant if (sc->sc_flags & I2C_READING) {
327 1.1 grant *sc->sc_data++ = ki2c_readreg(sc, DATA);
328 1.1 grant sc->sc_resid--;
329 1.1 grant
330 1.1 grant if (sc->sc_resid == 0) { /* Completed */
331 1.1 grant ki2c_writereg(sc, CONTROL, 0);
332 1.35 macallan cv_signal(&sc->sc_todev);
333 1.1 grant goto out;
334 1.1 grant }
335 1.1 grant } else {
336 1.1 grant #if 0
337 1.1 grant if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
338 1.1 grant /* No slave responded. */
339 1.1 grant sc->sc_flags |= I2C_ERROR;
340 1.1 grant goto out;
341 1.1 grant }
342 1.1 grant #endif
343 1.1 grant
344 1.1 grant if (sc->sc_resid == 0) {
345 1.1 grant x = ki2c_readreg(sc, CONTROL) | I2C_CT_STOP;
346 1.1 grant ki2c_writereg(sc, CONTROL, x);
347 1.35 macallan cv_signal(&sc->sc_todev);
348 1.1 grant } else {
349 1.1 grant ki2c_writereg(sc, DATA, *sc->sc_data++);
350 1.1 grant sc->sc_resid--;
351 1.1 grant }
352 1.1 grant }
353 1.1 grant }
354 1.1 grant
355 1.1 grant out:
356 1.1 grant if (isr & I2C_INT_STOP) {
357 1.1 grant ki2c_writereg(sc, CONTROL, 0);
358 1.1 grant sc->sc_flags &= ~I2C_BUSY;
359 1.35 macallan cv_signal(&sc->sc_todev);
360 1.1 grant }
361 1.1 grant
362 1.1 grant ki2c_writereg(sc, ISR, isr);
363 1.1 grant
364 1.1 grant return 1;
365 1.1 grant }
366 1.1 grant
367 1.1 grant int
368 1.14 dsl ki2c_poll(struct ki2c_softc *sc, int timo)
369 1.1 grant {
370 1.1 grant while (sc->sc_flags & I2C_BUSY) {
371 1.35 macallan if (cold) {
372 1.35 macallan if (ki2c_readreg(sc, ISR))
373 1.35 macallan ki2c_intr(sc);
374 1.35 macallan timo -= 10;
375 1.35 macallan if (timo < 0) {
376 1.35 macallan DPRINTF("i2c_poll: timeout\n");
377 1.35 macallan return -1;
378 1.35 macallan }
379 1.35 macallan delay(10);
380 1.35 macallan } else {
381 1.35 macallan mutex_enter(&sc->sc_todevmtx);
382 1.35 macallan cv_timedwait_sig(&sc->sc_todev, &sc->sc_todevmtx, hz);
383 1.35 macallan mutex_exit(&sc->sc_todevmtx);
384 1.1 grant }
385 1.1 grant }
386 1.1 grant return 0;
387 1.1 grant }
388 1.1 grant
389 1.1 grant int
390 1.15 dsl ki2c_start(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
391 1.1 grant {
392 1.1 grant int rw = (sc->sc_flags & I2C_READING) ? 1 : 0;
393 1.1 grant int timo, x;
394 1.1 grant
395 1.1 grant KASSERT((addr & 1) == 0);
396 1.1 grant
397 1.1 grant sc->sc_data = data;
398 1.1 grant sc->sc_resid = len;
399 1.1 grant sc->sc_flags |= I2C_BUSY;
400 1.1 grant
401 1.1 grant timo = 1000 + len * 200;
402 1.1 grant
403 1.1 grant /* XXX TAS3001 sometimes takes 50ms to finish writing registers. */
404 1.1 grant /* if (addr == 0x68) */
405 1.1 grant timo += 100000;
406 1.1 grant
407 1.1 grant ki2c_writereg(sc, ADDR, addr | rw);
408 1.1 grant ki2c_writereg(sc, SUBADDR, subaddr);
409 1.1 grant
410 1.1 grant x = ki2c_readreg(sc, CONTROL) | I2C_CT_ADDR;
411 1.1 grant ki2c_writereg(sc, CONTROL, x);
412 1.1 grant
413 1.1 grant if (ki2c_poll(sc, timo))
414 1.1 grant return -1;
415 1.1 grant if (sc->sc_flags & I2C_ERROR) {
416 1.20 macallan DPRINTF("I2C_ERROR\n");
417 1.1 grant return -1;
418 1.1 grant }
419 1.1 grant return 0;
420 1.1 grant }
421 1.1 grant
422 1.1 grant int
423 1.15 dsl ki2c_read(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
424 1.1 grant {
425 1.1 grant sc->sc_flags = I2C_READING;
426 1.20 macallan DPRINTF("ki2c_read: %02x %d\n", addr, len);
427 1.1 grant return ki2c_start(sc, addr, subaddr, data, len);
428 1.1 grant }
429 1.1 grant
430 1.1 grant int
431 1.15 dsl ki2c_write(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
432 1.1 grant {
433 1.1 grant sc->sc_flags = 0;
434 1.20 macallan DPRINTF("ki2c_write: %02x %d\n",addr,len);
435 1.3 macallan return ki2c_start(sc, addr, subaddr, data, len);
436 1.3 macallan }
437 1.3 macallan
438 1.3 macallan int
439 1.3 macallan ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
440 1.3 macallan size_t cmdlen, void *vbuf, size_t buflen, int flags)
441 1.3 macallan {
442 1.3 macallan struct ki2c_softc *sc = cookie;
443 1.12 pgoyette int i;
444 1.12 pgoyette size_t w_len;
445 1.12 pgoyette uint8_t *wp;
446 1.33 mlelstv uint8_t wrbuf[KI2C_EXEC_MAX_CMDLEN + KI2C_EXEC_MAX_CMDLEN];
447 1.25 macallan uint8_t channel;
448 1.12 pgoyette
449 1.12 pgoyette /*
450 1.12 pgoyette * We don't have any idea if the ki2c controller can execute
451 1.12 pgoyette * i2c quick_{read,write} operations, so if someone tries one,
452 1.12 pgoyette * return an error.
453 1.12 pgoyette */
454 1.12 pgoyette if (cmdlen == 0 && buflen == 0)
455 1.12 pgoyette return -1;
456 1.12 pgoyette
457 1.33 mlelstv /*
458 1.33 mlelstv * Transaction could be much larger now. Bail if it exceeds our
459 1.33 mlelstv * small combining buffer, we don't expect such devices.
460 1.33 mlelstv */
461 1.33 mlelstv if (cmdlen + buflen > sizeof(wrbuf))
462 1.33 mlelstv return -1;
463 1.33 mlelstv
464 1.25 macallan channel = (addr & 0xf80) ? 0x10 : 0x00;
465 1.25 macallan addr &= 0x7f;
466 1.25 macallan
467 1.25 macallan
468 1.3 macallan /* we handle the subaddress stuff ourselves */
469 1.25 macallan ki2c_setmode(sc, channel | I2C_STDMODE);
470 1.28 macallan ki2c_setspeed(sc, I2C_50kHz);
471 1.3 macallan
472 1.12 pgoyette /* Write-buffer defaults to vcmd */
473 1.12 pgoyette wp = (uint8_t *)(__UNCONST(vcmd));
474 1.12 pgoyette w_len = cmdlen;
475 1.12 pgoyette
476 1.12 pgoyette /*
477 1.12 pgoyette * Concatenate vcmd and vbuf for write operations
478 1.12 pgoyette *
479 1.12 pgoyette * Drivers written specifically for ki2c might already do this,
480 1.12 pgoyette * but "generic" i2c drivers still provide separate arguments
481 1.12 pgoyette * for the cmd and buf parts of iic_smbus_write_{byte,word}.
482 1.12 pgoyette */
483 1.12 pgoyette if (I2C_OP_WRITE_P(op) && buflen != 0) {
484 1.12 pgoyette if (cmdlen == 0) {
485 1.12 pgoyette wp = (uint8_t *)vbuf;
486 1.12 pgoyette w_len = buflen;
487 1.12 pgoyette } else {
488 1.12 pgoyette KASSERT((cmdlen + buflen) <= sizeof(wrbuf));
489 1.12 pgoyette wp = (uint8_t *)(__UNCONST(vcmd));
490 1.12 pgoyette w_len = 0;
491 1.12 pgoyette for (i = 0; i < cmdlen; i++)
492 1.12 pgoyette wrbuf[w_len++] = *wp++;
493 1.12 pgoyette wp = (uint8_t *)vbuf;
494 1.12 pgoyette for (i = 0; i < buflen; i++)
495 1.12 pgoyette wrbuf[w_len++] = *wp++;
496 1.12 pgoyette wp = wrbuf;
497 1.12 pgoyette }
498 1.12 pgoyette }
499 1.12 pgoyette
500 1.22 macallan if (w_len > 0)
501 1.22 macallan if (ki2c_write(sc, addr << 1, 0, wp, w_len) !=0 )
502 1.22 macallan return -1;
503 1.10 garbled
504 1.10 garbled if (I2C_OP_READ_P(op)) {
505 1.20 macallan if (ki2c_read(sc, addr << 1, 0, vbuf, buflen) !=0 )
506 1.3 macallan return -1;
507 1.3 macallan }
508 1.3 macallan return 0;
509 1.1 grant }
510