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/module.h>
35 #include <dev/iicbus/iiconf.h>
36 #include <dev/iicbus/iicbus.h>
37 #include "iicbus_if.h"
43 iicbus_intr(device_t bus, int event, char *buf)
45 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
47 /* call owner's intr routine */
49 IICBUS_INTR(sc->owner, event, buf);
55 iicbus_poll(struct iicbus_softc *sc, int how)
60 case (IIC_WAIT | IIC_INTR):
61 error = tsleep(sc, IICPRI|PCATCH, "iicreq", 0);
64 case (IIC_WAIT | IIC_NOINTR):
65 error = tsleep(sc, IICPRI, "iicreq", 0);
77 * iicbus_request_bus()
79 * Allocate the device to perform transfers.
81 * how : IIC_WAIT or IIC_DONTWAIT
84 iicbus_request_bus(device_t bus, device_t dev, int how)
86 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
89 /* first, ask the underlying layers if the request is ok */
91 error = IICBUS_CALLBACK(device_get_parent(bus),
92 IIC_REQUEST_BUS, (caddr_t)&how);
94 error = iicbus_poll(sc, how);
95 } while (error == EWOULDBLOCK);
99 if (sc->owner && sc->owner != dev) {
102 error = iicbus_poll(sc, how);
110 /* free any allocated resource */
112 IICBUS_CALLBACK(device_get_parent(bus), IIC_RELEASE_BUS,
120 * iicbus_release_bus()
122 * Release the device allocated with iicbus_request_dev()
125 iicbus_release_bus(device_t bus, device_t dev)
127 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
130 /* first, ask the underlying layers if the release is ok */
131 error = IICBUS_CALLBACK(device_get_parent(bus), IIC_RELEASE_BUS, NULL);
137 if (sc->owner != dev) {
145 /* wakeup waiting processes */
154 * Test if the iicbus is started by the controller
157 iicbus_started(device_t bus)
159 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
161 return (sc->started);
167 * Send start condition to the slave addressed by 'slave'
170 iicbus_start(device_t bus, u_char slave, int timeout)
172 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
176 return (EINVAL); /* bus already started */
178 if (!(error = IICBUS_START(device_get_parent(bus), slave, timeout)))
187 * iicbus_repeated_start()
189 * Send start condition to the slave addressed by 'slave'
192 iicbus_repeated_start(device_t bus, u_char slave, int timeout)
194 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
198 return (EINVAL); /* bus should have been already started */
200 if (!(error = IICBUS_REPEATED_START(device_get_parent(bus), slave, timeout)))
211 * Send stop condition to the bus
214 iicbus_stop(device_t bus)
216 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
220 return (EINVAL); /* bus not started */
222 error = IICBUS_STOP(device_get_parent(bus));
224 /* refuse any further access */
233 * Write a block of data to the slave previously started by
234 * iicbus_start() call
237 iicbus_write(device_t bus, char *buf, int len, int *sent, int timeout)
239 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
241 /* a slave must have been started with the appropriate address */
242 if (!sc->started || (sc->started & LSB))
245 return (IICBUS_WRITE(device_get_parent(bus), buf, len, sent, timeout));
251 * Read a block of data from the slave previously started by
255 iicbus_read(device_t bus, char *buf, int len, int *read, int last, int delay)
257 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
259 /* a slave must have been started with the appropriate address */
260 if (!sc->started || !(sc->started & LSB))
263 return (IICBUS_READ(device_get_parent(bus), buf, len, read, last, delay));
267 * iicbus_write_byte()
269 * Write a byte to the slave previously started by iicbus_start() call
272 iicbus_write_byte(device_t bus, char byte, int timeout)
277 return (iicbus_write(bus, &data, 1, &sent, timeout));
283 * Read a byte from the slave previously started by iicbus_start() call
286 iicbus_read_byte(device_t bus, char *byte, int timeout)
290 return (iicbus_read(bus, byte, 1, &read, IIC_LAST_READ, timeout));
294 * iicbus_block_write()
296 * Write a block of data to slave ; start/stop protocol managed
299 iicbus_block_write(device_t bus, u_char slave, char *buf, int len, int *sent)
301 u_char addr = slave & ~LSB;
304 if ((error = iicbus_start(bus, addr, 0)))
307 error = iicbus_write(bus, buf, len, sent, 0);
315 * iicbus_block_read()
317 * Read a block of data from slave ; start/stop protocol managed
320 iicbus_block_read(device_t bus, u_char slave, char *buf, int len, int *read)
322 u_char addr = slave | LSB;
325 if ((error = iicbus_start(bus, addr, 0)))
328 error = iicbus_read(bus, buf, len, read, IIC_LAST_READ, 0);