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.19 2008/08/23 07:38:00 imp Exp $
27 * $DragonFly: src/sys/bus/iicbus/iiconf.c,v 1.5 2005/06/02 20:40:36 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
35 #include <sys/thread2.h>
37 #include <bus/iicbus/iiconf.h>
38 #include <bus/iicbus/iicbus.h>
39 #include "iicbus_if.h"
45 iicbus_intr(device_t bus
, int event
, char *buf
)
47 struct iicbus_softc
*sc
= (struct iicbus_softc
*)device_get_softc(bus
);
49 /* call owner's intr routine */
51 IICBUS_INTR(sc
->owner
, event
, buf
);
57 iicbus_poll(struct iicbus_softc
*sc
, int how
)
62 case IIC_WAIT
| IIC_INTR
:
63 error
= tsleep(sc
, PCATCH
, "iicreq", 0);
66 case IIC_WAIT
| IIC_NOINTR
:
67 error
= tsleep(sc
, 0, "iicreq", 0);
79 * iicbus_request_bus()
81 * Allocate the device to perform transfers.
83 * how : IIC_WAIT or IIC_DONTWAIT
86 iicbus_request_bus(device_t bus
, device_t dev
, int how
)
88 struct iicbus_softc
*sc
= (struct iicbus_softc
*)device_get_softc(bus
);
91 /* first, ask the underlying layers if the request is ok */
93 error
= IICBUS_CALLBACK(device_get_parent(bus
),
94 IIC_REQUEST_BUS
, (caddr_t
)&how
);
96 error
= iicbus_poll(sc
, how
);
97 } while (error
== EWOULDBLOCK
);
101 if (sc
->owner
&& sc
->owner
!= dev
) {
103 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
) {
144 /* wakeup waiting processes */
153 * Test if the iicbus is started by the controller
156 iicbus_started(device_t bus
)
158 struct iicbus_softc
*sc
= (struct iicbus_softc
*)device_get_softc(bus
);
160 return (sc
->started
);
166 * Send start condition to the slave addressed by 'slave'
169 iicbus_start(device_t bus
, u_char slave
, int timeout
)
171 struct iicbus_softc
*sc
= (struct iicbus_softc
*)device_get_softc(bus
);
175 return (EINVAL
); /* bus already started */
177 if (!(error
= IICBUS_START(device_get_parent(bus
), slave
, timeout
)))
186 * iicbus_repeated_start()
188 * Send start condition to the slave addressed by 'slave'
191 iicbus_repeated_start(device_t bus
, u_char slave
, int timeout
)
193 struct iicbus_softc
*sc
= (struct iicbus_softc
*)device_get_softc(bus
);
197 return (EINVAL
); /* bus should have been already started */
199 if (!(error
= IICBUS_REPEATED_START(device_get_parent(bus
), slave
, timeout
)))
210 * Send stop condition to the bus
213 iicbus_stop(device_t bus
)
215 struct iicbus_softc
*sc
= (struct iicbus_softc
*)device_get_softc(bus
);
219 return (EINVAL
); /* bus not started */
221 error
= IICBUS_STOP(device_get_parent(bus
));
223 /* refuse any further access */
232 * Write a block of data to the slave previously started by
233 * iicbus_start() call
236 iicbus_write(device_t bus
, const char *buf
, int len
, int *sent
, int timeout
)
238 struct iicbus_softc
*sc
= (struct iicbus_softc
*)device_get_softc(bus
);
240 /* a slave must have been started with the appropriate address */
241 if (!sc
->started
|| (sc
->started
& LSB
))
244 return (IICBUS_WRITE(device_get_parent(bus
), buf
, len
, sent
, timeout
));
250 * Read a block of data from the slave previously started by
254 iicbus_read(device_t bus
, char *buf
, int len
, int *read
, int last
, int delay
)
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_READ(device_get_parent(bus
), buf
, len
, read
, last
, delay
));
266 * iicbus_write_byte()
268 * Write a byte to the slave previously started by iicbus_start() call
271 iicbus_write_byte(device_t bus
, char byte
, int timeout
)
276 return (iicbus_write(bus
, &data
, 1, &sent
, timeout
));
282 * Read a byte from the slave previously started by iicbus_start() call
285 iicbus_read_byte(device_t bus
, char *byte
, int timeout
)
289 return (iicbus_read(bus
, byte
, 1, &read
, IIC_LAST_READ
, timeout
));
293 * iicbus_block_write()
295 * Write a block of data to slave ; start/stop protocol managed
298 iicbus_block_write(device_t bus
, u_char slave
, char *buf
, int len
, int *sent
)
300 u_char addr
= slave
& ~LSB
;
303 if ((error
= iicbus_start(bus
, addr
, 0)))
306 error
= iicbus_write(bus
, buf
, len
, sent
, 0);
314 * iicbus_block_read()
316 * Read a block of data from slave ; start/stop protocol managed
319 iicbus_block_read(device_t bus
, u_char slave
, char *buf
, int len
, int *read
)
321 u_char addr
= slave
| LSB
;
324 if ((error
= iicbus_start(bus
, addr
, 0)))
327 error
= iicbus_read(bus
, buf
, len
, read
, IIC_LAST_READ
, 0);
337 * Do an aribtrary number of transfers on the iicbus. We pass these
338 * raw requests to the bridge driver. If the bridge driver supports
339 * them directly, then it manages all the details. If not, it can use
340 * the helper function iicbus_transfer_gen() which will do the
341 * transfers at a low level.
343 * Pointers passed in as part of iic_msg must be kernel pointers.
344 * Callers that have user addresses to manage must do so on their own.
347 iicbus_transfer(device_t bus
, struct iic_msg
*msgs
, uint32_t nmsgs
)
349 return (IICBUS_TRANSFER(device_get_parent(bus
), msgs
, nmsgs
));
353 * Generic version of iicbus_transfer that calls the appropriate
354 * routines to accomplish this. See note above about acceptable
358 iicbus_transfer_gen(device_t dev
, struct iic_msg
*msgs
, uint32_t nmsgs
)
360 int i
, error
, lenread
, lenwrote
, nkid
;
361 device_t
*children
, bus
;
363 if ((error
= device_get_children(dev
, &children
, &nkid
)) != 0)
366 kfree(children
, M_TEMP
);
370 kfree(children
, M_TEMP
);
371 for (i
= 0, error
= 0; i
< nmsgs
&& error
== 0; i
++) {
372 if (msgs
[i
].flags
& IIC_M_RD
)
373 error
= iicbus_block_read(bus
, msgs
[i
].slave
,
374 msgs
[i
].buf
, msgs
[i
].len
, &lenread
);
376 error
= iicbus_block_write(bus
, msgs
[i
].slave
,
377 msgs
[i
].buf
, msgs
[i
].len
, &lenwrote
);