2 * Copyright (c) 1998 Nicolas Souchu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
38 #include <dev/iicbus/iiconf.h>
39 #include <dev/iicbus/iicbus.h>
40 #include "iicbus_if.h"
43 * Translate IIC_Exxxxx status values to vaguely-equivelent errno values.
46 iic2errno(int iic_status)
49 case IIC_NOERR: return (0);
50 case IIC_EBUSERR: return (EALREADY);
51 case IIC_ENOACK: return (EIO);
52 case IIC_ETIMEOUT: return (ETIMEDOUT);
53 case IIC_EBUSBSY: return (EWOULDBLOCK);
54 case IIC_ESTATUS: return (EPROTO);
55 case IIC_EUNDERFLOW: return (EIO);
56 case IIC_EOVERFLOW: return (EOVERFLOW);
57 case IIC_ENOTSUPP: return (EOPNOTSUPP);
58 case IIC_ENOADDR: return (EADDRNOTAVAIL);
59 case IIC_ERESOURCE: return (ENOMEM);
60 default: return (EIO);
68 iicbus_intr(device_t bus, int event, char *buf)
70 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
72 /* call owner's intr routine */
74 IICBUS_INTR(sc->owner, event, buf);
80 iicbus_poll(struct iicbus_softc *sc, int how)
84 IICBUS_ASSERT_LOCKED(sc);
86 case IIC_WAIT | IIC_INTR:
87 error = mtx_sleep(sc, &sc->lock, IICPRI|PCATCH, "iicreq", 0);
90 case IIC_WAIT | IIC_NOINTR:
91 error = mtx_sleep(sc, &sc->lock, IICPRI, "iicreq", 0);
102 * iicbus_request_bus()
104 * Allocate the device to perform transfers.
106 * how : IIC_WAIT or IIC_DONTWAIT
109 iicbus_request_bus(device_t bus, device_t dev, int how)
111 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
116 while (error == 0 && sc->owner != NULL && sc->owner != dev)
117 error = iicbus_poll(sc, how);
121 if (sc->owner == NULL) {
124 * Drop the lock around the call to the bus driver, it
125 * should be allowed to sleep in the IIC_WAIT case.
126 * Drivers might also need to grab locks that would
127 * cause a LOR if our lock is held.
130 /* Ask the underlying layers if the request is ok */
131 error = IICBUS_CALLBACK(device_get_parent(bus),
132 IIC_REQUEST_BUS, (caddr_t)&how);
149 * iicbus_release_bus()
151 * Release the device allocated with iicbus_request_dev()
154 iicbus_release_bus(device_t bus, device_t dev)
156 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
160 if (sc->owner != dev) {
162 return (IIC_EBUSBSY);
165 if (--sc->owncount == 0) {
166 /* Drop the lock while informing the low-level driver. */
168 IICBUS_CALLBACK(device_get_parent(bus), IIC_RELEASE_BUS, NULL);
180 * Test if the iicbus is started by the controller
183 iicbus_started(device_t bus)
185 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
187 return (sc->started);
193 * Send start condition to the slave addressed by 'slave'
196 iicbus_start(device_t bus, u_char slave, int timeout)
198 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
202 return (IIC_ESTATUS); /* protocol error, bus already started */
204 if (!(error = IICBUS_START(device_get_parent(bus), slave, timeout)))
213 * iicbus_repeated_start()
215 * Send start condition to the slave addressed by 'slave'
218 iicbus_repeated_start(device_t bus, u_char slave, int timeout)
220 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
224 return (IIC_ESTATUS); /* protocol error, bus not started */
226 if (!(error = IICBUS_REPEATED_START(device_get_parent(bus), slave, timeout)))
237 * Send stop condition to the bus
240 iicbus_stop(device_t bus)
242 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
246 return (IIC_ESTATUS); /* protocol error, bus not started */
248 error = IICBUS_STOP(device_get_parent(bus));
250 /* refuse any further access */
259 * Write a block of data to the slave previously started by
260 * iicbus_start() call
263 iicbus_write(device_t bus, const char *buf, int len, int *sent, int timeout)
265 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
267 /* a slave must have been started for writing */
268 if (sc->started == 0 || (sc->strict != 0 && (sc->started & LSB) != 0))
269 return (IIC_ESTATUS);
271 return (IICBUS_WRITE(device_get_parent(bus), buf, len, sent, timeout));
277 * Read a block of data from the slave previously started by
281 iicbus_read(device_t bus, char *buf, int len, int *read, int last, int delay)
283 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
285 /* a slave must have been started for reading */
286 if (sc->started == 0 || (sc->strict != 0 && (sc->started & LSB) == 0))
287 return (IIC_ESTATUS);
289 return (IICBUS_READ(device_get_parent(bus), buf, len, read, last, delay));
293 * iicbus_write_byte()
295 * Write a byte to the slave previously started by iicbus_start() call
298 iicbus_write_byte(device_t bus, char byte, int timeout)
300 struct iicbus_softc *sc = device_get_softc(bus);
304 /* a slave must have been started for writing */
305 if (sc->started == 0 || (sc->strict != 0 && (sc->started & LSB) != 0))
306 return (IIC_ESTATUS);
308 return (iicbus_write(bus, &data, 1, &sent, timeout));
314 * Read a byte from the slave previously started by iicbus_start() call
317 iicbus_read_byte(device_t bus, char *byte, int timeout)
319 struct iicbus_softc *sc = device_get_softc(bus);
322 /* a slave must have been started for reading */
323 if (sc->started == 0 || (sc->strict != 0 && (sc->started & LSB) == 0))
324 return (IIC_ESTATUS);
326 return (iicbus_read(bus, byte, 1, &read, IIC_LAST_READ, timeout));
330 * iicbus_block_write()
332 * Write a block of data to slave ; start/stop protocol managed
335 iicbus_block_write(device_t bus, u_char slave, char *buf, int len, int *sent)
337 u_char addr = slave & ~LSB;
340 if ((error = iicbus_start(bus, addr, 0)))
343 error = iicbus_write(bus, buf, len, sent, 0);
351 * iicbus_block_read()
353 * Read a block of data from slave ; start/stop protocol managed
356 iicbus_block_read(device_t bus, u_char slave, char *buf, int len, int *read)
358 u_char addr = slave | LSB;
361 if ((error = iicbus_start(bus, addr, 0)))
364 error = iicbus_read(bus, buf, len, read, IIC_LAST_READ, 0);
374 * Do an aribtrary number of transfers on the iicbus. We pass these
375 * raw requests to the bridge driver. If the bridge driver supports
376 * them directly, then it manages all the details. If not, it can use
377 * the helper function iicbus_transfer_gen() which will do the
378 * transfers at a low level.
380 * Pointers passed in as part of iic_msg must be kernel pointers.
381 * Callers that have user addresses to manage must do so on their own.
384 iicbus_transfer(device_t bus, struct iic_msg *msgs, uint32_t nmsgs)
387 return (IICBUS_TRANSFER(device_get_parent(bus), msgs, nmsgs));
391 iicbus_transfer_excl(device_t dev, struct iic_msg *msgs, uint32_t nmsgs,
397 bus = device_get_parent(dev);
398 error = iicbus_request_bus(bus, dev, how);
400 error = IICBUS_TRANSFER(bus, msgs, nmsgs);
401 iicbus_release_bus(bus, dev);
406 * Generic version of iicbus_transfer that calls the appropriate
407 * routines to accomplish this. See note above about acceptable
411 iicbus_transfer_gen(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
413 int i, error, lenread, lenwrote, nkid, rpstart, addr;
414 device_t *children, bus;
415 bool nostop, started;
417 if ((error = device_get_children(dev, &children, &nkid)) != 0)
418 return (IIC_ERESOURCE);
420 free(children, M_TEMP);
421 return (IIC_ENOTSUPP);
425 free(children, M_TEMP);
426 nostop = iicbus_get_nostop(dev);
428 for (i = 0, error = 0; i < nmsgs && error == 0; i++) {
429 addr = msgs[i].slave;
430 if (msgs[i].flags & IIC_M_RD)
435 if (!(msgs[i].flags & IIC_M_NOSTART)) {
437 error = iicbus_repeated_start(bus, addr, 0);
439 error = iicbus_start(bus, addr, 0);
445 if (msgs[i].flags & IIC_M_RD)
446 error = iicbus_read(bus, msgs[i].buf, msgs[i].len,
447 &lenread, IIC_LAST_READ, 0);
449 error = iicbus_write(bus, msgs[i].buf, msgs[i].len,
454 if ((msgs[i].flags & IIC_M_NOSTOP) != 0 ||
455 (nostop && i + 1 < nmsgs)) {
456 rpstart = 1; /* Next message gets repeated start */
462 if (error != 0 && started)
468 iicdev_readfrom(device_t slavedev, uint8_t regaddr, void *buffer,
469 uint16_t buflen, int waithow)
471 struct iic_msg msgs[2];
475 * Two transfers back to back with a repeat-start between them; first we
476 * write the address-within-device, then we read from the device.
478 slaveaddr = iicbus_get_addr(slavedev);
480 msgs[0].slave = slaveaddr;
481 msgs[0].flags = IIC_M_WR | IIC_M_NOSTOP;
483 msgs[0].buf = ®addr;
485 msgs[1].slave = slaveaddr;
486 msgs[1].flags = IIC_M_RD;
487 msgs[1].len = buflen;
488 msgs[1].buf = buffer;
490 return (iicbus_transfer_excl(slavedev, msgs, nitems(msgs), waithow));
493 int iicdev_writeto(device_t slavedev, uint8_t regaddr, void *buffer,
494 uint16_t buflen, int waithow)
496 struct iic_msg msgs[2];
500 * Two transfers back to back with no stop or start between them; first
501 * we write the address then we write the data to that address, all in a
502 * single transfer from two scattered buffers.
504 slaveaddr = iicbus_get_addr(slavedev);
506 msgs[0].slave = slaveaddr;
507 msgs[0].flags = IIC_M_WR | IIC_M_NOSTOP;
509 msgs[0].buf = ®addr;
511 msgs[1].slave = slaveaddr;
512 msgs[1].flags = IIC_M_WR | IIC_M_NOSTART;
513 msgs[1].len = buflen;
514 msgs[1].buf = buffer;
516 return (iicbus_transfer_excl(slavedev, msgs, nitems(msgs), waithow));