2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998 Nicolas Souchu
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
40 #include <dev/iicbus/iiconf.h>
41 #include <dev/iicbus/iicbus.h>
42 #include "iicbus_if.h"
45 * Translate IIC_Exxxxx status values to vaguely-equivelent errno values.
48 iic2errno(int iic_status)
51 case IIC_NOERR: return (0);
52 case IIC_EBUSERR: return (EALREADY);
53 case IIC_ENOACK: return (EIO);
54 case IIC_ETIMEOUT: return (ETIMEDOUT);
55 case IIC_EBUSBSY: return (EWOULDBLOCK);
56 case IIC_ESTATUS: return (EPROTO);
57 case IIC_EUNDERFLOW: return (EIO);
58 case IIC_EOVERFLOW: return (EOVERFLOW);
59 case IIC_ENOTSUPP: return (EOPNOTSUPP);
60 case IIC_ENOADDR: return (EADDRNOTAVAIL);
61 case IIC_ERESOURCE: return (ENOMEM);
62 default: return (EIO);
70 iicbus_intr(device_t bus, int event, char *buf)
72 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
74 /* call owner's intr routine */
76 IICBUS_INTR(sc->owner, event, buf);
82 iicbus_poll(struct iicbus_softc *sc, int how)
86 IICBUS_ASSERT_LOCKED(sc);
87 switch (how & IIC_INTRWAIT) {
88 case IIC_WAIT | IIC_INTR:
89 error = mtx_sleep(sc, &sc->lock, IICPRI|PCATCH, "iicreq", 0);
92 case IIC_WAIT | IIC_NOINTR:
93 error = mtx_sleep(sc, &sc->lock, IICPRI, "iicreq", 0);
104 * iicbus_request_bus()
106 * Allocate the device to perform transfers.
108 * how : IIC_WAIT or IIC_DONTWAIT
111 iicbus_request_bus(device_t bus, device_t dev, int how)
113 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
119 if (sc->owner == NULL)
121 if ((how & IIC_RECURSIVE) && sc->owner == dev)
123 if ((error = iicbus_poll(sc, how)) != 0)
129 if (sc->owner == NULL) {
132 * Drop the lock around the call to the bus driver, it
133 * should be allowed to sleep in the IIC_WAIT case.
134 * Drivers might also need to grab locks that would
135 * cause a LOR if our lock is held.
138 /* Ask the underlying layers if the request is ok */
139 error = IICBUS_CALLBACK(device_get_parent(bus),
140 IIC_REQUEST_BUS, (caddr_t)&how);
157 * iicbus_release_bus()
159 * Release the device allocated with iicbus_request_dev()
162 iicbus_release_bus(device_t bus, device_t dev)
164 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
168 if (sc->owner != dev) {
170 return (IIC_EBUSBSY);
173 if (--sc->owncount == 0) {
174 /* Drop the lock while informing the low-level driver. */
176 IICBUS_CALLBACK(device_get_parent(bus), IIC_RELEASE_BUS, NULL);
188 * Test if the iicbus is started by the controller
191 iicbus_started(device_t bus)
193 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
195 return (sc->started);
201 * Send start condition to the slave addressed by 'slave'
204 iicbus_start(device_t bus, u_char slave, int timeout)
206 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
210 return (IIC_ESTATUS); /* protocol error, bus already started */
212 if (!(error = IICBUS_START(device_get_parent(bus), slave, timeout)))
221 * iicbus_repeated_start()
223 * Send start condition to the slave addressed by 'slave'
226 iicbus_repeated_start(device_t bus, u_char slave, int timeout)
228 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
232 return (IIC_ESTATUS); /* protocol error, bus not started */
234 if (!(error = IICBUS_REPEATED_START(device_get_parent(bus), slave, timeout)))
245 * Send stop condition to the bus
248 iicbus_stop(device_t bus)
250 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
254 return (IIC_ESTATUS); /* protocol error, bus not started */
256 error = IICBUS_STOP(device_get_parent(bus));
258 /* refuse any further access */
267 * Write a block of data to the slave previously started by
268 * iicbus_start() call
271 iicbus_write(device_t bus, const char *buf, int len, int *sent, int timeout)
273 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
275 /* a slave must have been started for writing */
276 if (sc->started == 0 || (sc->strict != 0 && (sc->started & LSB) != 0))
277 return (IIC_ESTATUS);
279 return (IICBUS_WRITE(device_get_parent(bus), buf, len, sent, timeout));
285 * Read a block of data from the slave previously started by
289 iicbus_read(device_t bus, char *buf, int len, int *read, int last, int delay)
291 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
293 /* a slave must have been started for reading */
294 if (sc->started == 0 || (sc->strict != 0 && (sc->started & LSB) == 0))
295 return (IIC_ESTATUS);
297 return (IICBUS_READ(device_get_parent(bus), buf, len, read, last, delay));
301 * iicbus_write_byte()
303 * Write a byte to the slave previously started by iicbus_start() call
306 iicbus_write_byte(device_t bus, char byte, int timeout)
308 struct iicbus_softc *sc = device_get_softc(bus);
312 /* a slave must have been started for writing */
313 if (sc->started == 0 || (sc->strict != 0 && (sc->started & LSB) != 0))
314 return (IIC_ESTATUS);
316 return (iicbus_write(bus, &data, 1, &sent, timeout));
322 * Read a byte from the slave previously started by iicbus_start() call
325 iicbus_read_byte(device_t bus, char *byte, int timeout)
327 struct iicbus_softc *sc = device_get_softc(bus);
330 /* a slave must have been started for reading */
331 if (sc->started == 0 || (sc->strict != 0 && (sc->started & LSB) == 0))
332 return (IIC_ESTATUS);
334 return (iicbus_read(bus, byte, 1, &read, IIC_LAST_READ, timeout));
338 * iicbus_block_write()
340 * Write a block of data to slave ; start/stop protocol managed
343 iicbus_block_write(device_t bus, u_char slave, char *buf, int len, int *sent)
345 u_char addr = slave & ~LSB;
348 if ((error = iicbus_start(bus, addr, 0)))
351 error = iicbus_write(bus, buf, len, sent, 0);
359 * iicbus_block_read()
361 * Read a block of data from slave ; start/stop protocol managed
364 iicbus_block_read(device_t bus, u_char slave, char *buf, int len, int *read)
366 u_char addr = slave | LSB;
369 if ((error = iicbus_start(bus, addr, 0)))
372 error = iicbus_read(bus, buf, len, read, IIC_LAST_READ, 0);
382 * Do an aribtrary number of transfers on the iicbus. We pass these
383 * raw requests to the bridge driver. If the bridge driver supports
384 * them directly, then it manages all the details. If not, it can use
385 * the helper function iicbus_transfer_gen() which will do the
386 * transfers at a low level.
388 * Pointers passed in as part of iic_msg must be kernel pointers.
389 * Callers that have user addresses to manage must do so on their own.
392 iicbus_transfer(device_t bus, struct iic_msg *msgs, uint32_t nmsgs)
395 return (IICBUS_TRANSFER(device_get_parent(bus), msgs, nmsgs));
399 iicbus_transfer_excl(device_t dev, struct iic_msg *msgs, uint32_t nmsgs,
405 bus = device_get_parent(dev);
406 error = iicbus_request_bus(bus, dev, how);
408 error = IICBUS_TRANSFER(bus, msgs, nmsgs);
409 iicbus_release_bus(bus, dev);
414 * Generic version of iicbus_transfer that calls the appropriate
415 * routines to accomplish this. See note above about acceptable
419 iicbus_transfer_gen(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
421 int i, error, lenread, lenwrote, nkid, rpstart, addr;
422 device_t *children, bus;
423 bool nostop, started;
425 if ((error = device_get_children(dev, &children, &nkid)) != 0)
426 return (IIC_ERESOURCE);
428 free(children, M_TEMP);
429 return (IIC_ENOTSUPP);
433 free(children, M_TEMP);
434 nostop = iicbus_get_nostop(dev);
436 for (i = 0, error = 0; i < nmsgs && error == 0; i++) {
437 addr = msgs[i].slave;
438 if (msgs[i].flags & IIC_M_RD)
443 if (!(msgs[i].flags & IIC_M_NOSTART)) {
445 error = iicbus_repeated_start(bus, addr, 0);
447 error = iicbus_start(bus, addr, 0);
453 if (msgs[i].flags & IIC_M_RD)
454 error = iicbus_read(bus, msgs[i].buf, msgs[i].len,
455 &lenread, IIC_LAST_READ, 0);
457 error = iicbus_write(bus, msgs[i].buf, msgs[i].len,
462 if ((msgs[i].flags & IIC_M_NOSTOP) != 0 ||
463 (nostop && i + 1 < nmsgs)) {
464 rpstart = 1; /* Next message gets repeated start */
470 if (error != 0 && started)
476 iicdev_readfrom(device_t slavedev, uint8_t regaddr, void *buffer,
477 uint16_t buflen, int waithow)
479 struct iic_msg msgs[2];
483 * Two transfers back to back with a repeat-start between them; first we
484 * write the address-within-device, then we read from the device.
486 slaveaddr = iicbus_get_addr(slavedev);
488 msgs[0].slave = slaveaddr;
489 msgs[0].flags = IIC_M_WR | IIC_M_NOSTOP;
491 msgs[0].buf = ®addr;
493 msgs[1].slave = slaveaddr;
494 msgs[1].flags = IIC_M_RD;
495 msgs[1].len = buflen;
496 msgs[1].buf = buffer;
498 return (iicbus_transfer_excl(slavedev, msgs, nitems(msgs), waithow));
501 int iicdev_writeto(device_t slavedev, uint8_t regaddr, void *buffer,
502 uint16_t buflen, int waithow)
504 struct iic_msg msgs[2];
508 * Two transfers back to back with no stop or start between them; first
509 * we write the address then we write the data to that address, all in a
510 * single transfer from two scattered buffers.
512 slaveaddr = iicbus_get_addr(slavedev);
514 msgs[0].slave = slaveaddr;
515 msgs[0].flags = IIC_M_WR | IIC_M_NOSTOP;
517 msgs[0].buf = ®addr;
519 msgs[1].slave = slaveaddr;
520 msgs[1].flags = IIC_M_WR | IIC_M_NOSTART;
521 msgs[1].len = buflen;
522 msgs[1].buf = buffer;
524 return (iicbus_transfer_excl(slavedev, msgs, nitems(msgs), waithow));