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
26 * $FreeBSD: src/sys/dev/iicbus/iiconf.c,v 1.10 1999/12/03 08:41:02 mdodd Exp $
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.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);
58 * Allocate a new bus connected to the given parent device
61 iicbus_alloc_bus(device_t parent)
65 /* add the bus to the parent */
66 child = device_add_child(parent, "iicbus", -1);
72 iicbus_poll(struct iicbus_softc *sc, int how)
77 case (IIC_WAIT | IIC_INTR):
78 error = tsleep(sc, IICPRI|PCATCH, "iicreq", 0);
81 case (IIC_WAIT | IIC_NOINTR):
82 error = tsleep(sc, IICPRI, "iicreq", 0);
94 * iicbus_request_bus()
96 * Allocate the device to perform transfers.
98 * how : IIC_WAIT or IIC_DONTWAIT
101 iicbus_request_bus(device_t bus, device_t dev, int how)
103 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
106 /* first, ask the underlying layers if the request is ok */
108 error = IICBUS_CALLBACK(device_get_parent(bus),
109 IIC_REQUEST_BUS, (caddr_t)&how);
111 error = iicbus_poll(sc, how);
112 } while (error == EWOULDBLOCK);
116 if (sc->owner && sc->owner != dev) {
119 error = iicbus_poll(sc, how);
127 /* free any allocated resource */
129 IICBUS_CALLBACK(device_get_parent(bus), IIC_RELEASE_BUS,
137 * iicbus_release_bus()
139 * Release the device allocated with iicbus_request_dev()
142 iicbus_release_bus(device_t bus, device_t dev)
144 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
147 /* first, ask the underlying layers if the release is ok */
148 error = IICBUS_CALLBACK(device_get_parent(bus), IIC_RELEASE_BUS, NULL);
154 if (sc->owner != dev) {
162 /* wakeup waiting processes */
171 * Test if the iicbus is started by the controller
174 iicbus_started(device_t bus)
176 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
178 return (sc->started);
184 * Send start condition to the slave addressed by 'slave'
187 iicbus_start(device_t bus, u_char slave, int timeout)
189 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
193 return (EINVAL); /* bus already started */
195 if (!(error = IICBUS_START(device_get_parent(bus), slave, timeout)))
204 * iicbus_repeated_start()
206 * Send start condition to the slave addressed by 'slave'
209 iicbus_repeated_start(device_t bus, u_char slave, int timeout)
211 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
215 return (EINVAL); /* bus should have been already started */
217 if (!(error = IICBUS_REPEATED_START(device_get_parent(bus), slave, timeout)))
228 * Send stop condition to the bus
231 iicbus_stop(device_t bus)
233 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
237 return (EINVAL); /* bus not started */
239 error = IICBUS_STOP(device_get_parent(bus));
241 /* refuse any further access */
250 * Write a block of data to the slave previously started by
251 * iicbus_start() call
254 iicbus_write(device_t bus, char *buf, int len, int *sent, int timeout)
256 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
258 /* a slave must have been started with the appropriate address */
259 if (!sc->started || (sc->started & LSB))
262 return (IICBUS_WRITE(device_get_parent(bus), buf, len, sent, timeout));
268 * Read a block of data from the slave previously started by
272 iicbus_read(device_t bus, char *buf, int len, int *read, int last, int delay)
274 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
276 /* a slave must have been started with the appropriate address */
277 if (!sc->started || !(sc->started & LSB))
280 return (IICBUS_READ(device_get_parent(bus), buf, len, read, last, delay));
284 * iicbus_write_byte()
286 * Write a byte to the slave previously started by iicbus_start() call
289 iicbus_write_byte(device_t bus, char byte, int timeout)
294 return (iicbus_write(bus, &data, 1, &sent, timeout));
300 * Read a byte from the slave previously started by iicbus_start() call
303 iicbus_read_byte(device_t bus, char *byte, int timeout)
307 return (iicbus_read(bus, byte, 1, &read, IIC_LAST_READ, timeout));
311 * iicbus_block_write()
313 * Write a block of data to slave ; start/stop protocol managed
316 iicbus_block_write(device_t bus, u_char slave, char *buf, int len, int *sent)
318 u_char addr = slave & ~LSB;
321 if ((error = iicbus_start(bus, addr, 0)))
324 error = iicbus_write(bus, buf, len, sent, 0);
332 * iicbus_block_read()
334 * Read a block of data from slave ; start/stop protocol managed
337 iicbus_block_read(device_t bus, u_char slave, char *buf, int len, int *read)
339 u_char addr = slave | LSB;
342 if ((error = iicbus_start(bus, addr, 0)))
345 error = iicbus_read(bus, buf, len, read, IIC_LAST_READ, 0);
355 * Get the I2C 7 bits address of the device
358 iicbus_get_addr(device_t dev)
361 device_t parent = device_get_parent(dev);
363 BUS_READ_IVAR(parent, dev, IICBUS_IVAR_ADDR, &addr);
365 return ((u_char)addr);