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>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
36 #include <dev/iicbus/iiconf.h>
37 #include <dev/iicbus/iicbus.h>
38 #include "iicbus_if.h"
44 iicbus_intr(device_t bus, int event, char *buf)
46 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
48 /* call owner's intr routine */
50 IICBUS_INTR(sc->owner, event, buf);
56 iicbus_poll(struct iicbus_softc *sc, int how)
61 case (IIC_WAIT | IIC_INTR):
62 error = tsleep(sc, IICPRI|PCATCH, "iicreq", 0);
65 case (IIC_WAIT | IIC_NOINTR):
66 error = tsleep(sc, IICPRI, "iicreq", 0);
78 * iicbus_request_bus()
80 * Allocate the device to perform transfers.
82 * how : IIC_WAIT or IIC_DONTWAIT
85 iicbus_request_bus(device_t bus, device_t dev, int how)
87 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
90 /* first, ask the underlying layers if the request is ok */
92 error = IICBUS_CALLBACK(device_get_parent(bus),
93 IIC_REQUEST_BUS, (caddr_t)&how);
95 error = iicbus_poll(sc, how);
96 } while (error == EWOULDBLOCK);
100 if (sc->owner && sc->owner != dev) {
103 error = iicbus_poll(sc, how);
111 /* free any allocated resource */
113 IICBUS_CALLBACK(device_get_parent(bus), IIC_RELEASE_BUS,
121 * iicbus_release_bus()
123 * Release the device allocated with iicbus_request_dev()
126 iicbus_release_bus(device_t bus, device_t dev)
128 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
131 /* first, ask the underlying layers if the release is ok */
132 error = IICBUS_CALLBACK(device_get_parent(bus), IIC_RELEASE_BUS, NULL);
138 if (sc->owner != dev) {
146 /* wakeup waiting processes */
155 * Test if the iicbus is started by the controller
158 iicbus_started(device_t bus)
160 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
162 return (sc->started);
168 * Send start condition to the slave addressed by 'slave'
171 iicbus_start(device_t bus, u_char slave, int timeout)
173 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
177 return (EINVAL); /* bus already started */
179 if (!(error = IICBUS_START(device_get_parent(bus), slave, timeout)))
188 * iicbus_repeated_start()
190 * Send start condition to the slave addressed by 'slave'
193 iicbus_repeated_start(device_t bus, u_char slave, int timeout)
195 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
199 return (EINVAL); /* bus should have been already started */
201 if (!(error = IICBUS_REPEATED_START(device_get_parent(bus), slave, timeout)))
212 * Send stop condition to the bus
215 iicbus_stop(device_t bus)
217 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
221 return (EINVAL); /* bus not started */
223 error = IICBUS_STOP(device_get_parent(bus));
225 /* refuse any further access */
234 * Write a block of data to the slave previously started by
235 * iicbus_start() call
238 iicbus_write(device_t bus, const char *buf, int len, int *sent, int timeout)
240 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
242 /* a slave must have been started with the appropriate address */
243 if (!sc->started || (sc->started & LSB))
246 return (IICBUS_WRITE(device_get_parent(bus), buf, len, sent, timeout));
252 * Read a block of data from the slave previously started by
256 iicbus_read(device_t bus, char *buf, int len, int *read, int last, int delay)
258 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
260 /* a slave must have been started with the appropriate address */
261 if (!sc->started || !(sc->started & LSB))
264 return (IICBUS_READ(device_get_parent(bus), buf, len, read, last, delay));
268 * iicbus_write_byte()
270 * Write a byte to the slave previously started by iicbus_start() call
273 iicbus_write_byte(device_t bus, char byte, int timeout)
278 return (iicbus_write(bus, &data, 1, &sent, timeout));
284 * Read a byte from the slave previously started by iicbus_start() call
287 iicbus_read_byte(device_t bus, char *byte, int timeout)
291 return (iicbus_read(bus, byte, 1, &read, IIC_LAST_READ, timeout));
295 * iicbus_block_write()
297 * Write a block of data to slave ; start/stop protocol managed
300 iicbus_block_write(device_t bus, u_char slave, char *buf, int len, int *sent)
302 u_char addr = slave & ~LSB;
305 if ((error = iicbus_start(bus, addr, 0)))
308 error = iicbus_write(bus, buf, len, sent, 0);
316 * iicbus_block_read()
318 * Read a block of data from slave ; start/stop protocol managed
321 iicbus_block_read(device_t bus, u_char slave, char *buf, int len, int *read)
323 u_char addr = slave | LSB;
326 if ((error = iicbus_start(bus, addr, 0)))
329 error = iicbus_read(bus, buf, len, read, IIC_LAST_READ, 0);
339 * Do an aribtrary number of transfers on the iicbus. We pass these
340 * raw requests to the bridge driver. If the bridge driver supports
341 * them directly, then it manages all the details. If not, it can use
342 * the helper function iicbus_transfer_gen() which will do the
343 * transfers at a low level.
345 * Pointers passed in as part of iic_msg must be kernel pointers.
346 * Callers that have user addresses to manage must do so on their own.
349 iicbus_transfer(device_t bus, struct iic_msg *msgs, uint32_t nmsgs)
351 return (IICBUS_TRANSFER(device_get_parent(bus), msgs, nmsgs));
355 * Generic version of iicbus_transfer that calls the appropriate
356 * routines to accomplish this. See note above about acceptable
360 iicbus_transfer_gen(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
362 int i, error, lenread, lenwrote, nkid;
363 device_t *children, bus;
365 device_get_children(dev, &children, &nkid);
369 free(children, M_TEMP);
370 for (i = 0, error = 0; i < nmsgs && error == 0; i++) {
371 if (msgs[i].flags & IIC_M_RD)
372 error = iicbus_block_read(bus, msgs[i].slave,
373 msgs[i].buf, msgs[i].len, &lenread);
375 error = iicbus_block_write(bus, msgs[i].slave,
376 msgs[i].buf, msgs[i].len, &lenwrote);