summaryrefslogtreecommitdiff
path: root/share/man/man9/iic.9
blob: 59f91ff1448194185567db850f93cf2f9f3d91b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
.\"	$OpenBSD: iic.9,v 1.3 2004/08/24 05:48:22 david Exp $
.\"
.\" Copyright (c) 2003 Wasabi Systems, Inc.
.\" All rights reserved.
.\"
.\" Written by Jason R. Thorpe for Wasabi Systems, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\"    must display the following acknowledgement:
.\"	This product includes software developed for the NetBSD Project by
.\"	Wasabi Systems, Inc.
.\" 4. The name of Wasabi Systems, Inc. may not be used to endorse
.\"    or promote products derived from this software without specific prior
.\"    written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd July 29, 2003
.Dt IIC 9
.Os
.Sh NAME
.Nm iic_acquire_bus ,
.Nm iic_release_bus ,
.Nm iic_exec ,
.Nm iic_smbus_write_byte ,
.Nm iic_smbus_read_byte ,
.Nm iic_smbus_receive_byte
.Nd Inter IC (I2C) bus
.Sh SYNOPSIS
.Fd #include <dev/i2c/i2cvar.h>
.Ft int
.Fo iic_acquire_bus
.Fa "i2c_tag_t ic"
.Fa "int flags"
.Fc
.Ft int
.Fo iic_release_bus
.Fa "i2c_tag_t ic"
.Fa "int flags"
.Fc
.Ft int
.Fo iic_exec
.Fa "i2c_tag_t ic"
.Fa "i2c_op_t op"
.Fa "i2c_addr_t addr"
.Fa "const void *cmdbuf"
.Fa "size_t cmdlen"
.Fa "void *buf"
.Fa "size_t len"
.Fa "int flags"
.Fc
.Ft int
.Fo iic_smbus_write_byte
.Fa "i2c_tag_t ic"
.Fa "i2c_addr_t addr"
.Fa "uint8_t cmd"
.Fa "uint8_t data"
.Fa "int flags"
.Fc
.Ft int
.Fo iic_smbus_read_byte
.Fa "i2c_tag_t ic"
.Fa "i2c_addr_t addr"
.Fa "uint8_t cmd"
.Fa "uint8_t *datap"
.Fa "int flags"
.Fc
.Ft int
.Fo iic_smbus_receive_byte
.Fa "i2c_tag_t ic"
.Fa "i2c_addr_t addr"
.Fa "uint8_t *datap"
.Fa "int flags"
.Fc
.Sh DESCRIPTION
I2C is a two-wire bus developed by Philips used for connecting
integrated circuits.
It is commonly used for connecting devices such as EEPROMs,
temperature sensors, fan controllers, real-time clocks, tuners,
and other types of integrated circuits.
The
.Nm iic
interface provides a means of communicating with I2C-connected devices.
The System Management Bus, or SMBus, is a variant of the I2C bus with
a simplified command protocol and some electrical differences.
.Sh DATA TYPES
Drivers for devices attached to the I2C bus will make use of the
following data types:
.Bl -tag -width i2c_addr_t
.It Fa i2c_tag_t
Controller tag for the I2C bus.
This is a pointer to a
.Fa struct i2c_controller ,
consisting of function pointers filled in by the I2C
controller driver.
.It Fa i2c_op_t
I2C bus operation.
The following I2C bus operations are defined:
.Bl -tag -width XXXX
.It I2C_OP_READ
Perform a read operation.
.It I2C_OP_READ_WITH_STOP
Perform a read operation and send a STOP condition on the I2C bus at
the conclusion of the read.
.It I2C_OP_WRITE
Perform a write operation.
.It I2C_OP_WRITE_WITH_STOP
Perform a write operation and send a STOP condition on the I2C bus at
the conclusion of the write.
.El
.It Fa i2c_addr_t
I2C device address.
.It Fa struct i2c_attach_args
Devices are attached to an I2C bus using this structure.
The structure is defined as follows:
.Bd -literal
struct i2c_attach_args {
	i2c_tag_t ia_tag;	/* controller */
	i2c_addr_t ia_addr;	/* address of device */
	int ia_size;		/* size (for EEPROMs) */
};
.Ed
.El
.Sh FUNCTIONS
The following functions comprise the API provided to drivers of
I2C-connected devices:
.Bl -tag -width iic_exec
.It Fn iic_acquire_bus "ic" "flags"
Acquire an exclusive lock on the I2C bus.
This is required since only one device may communicate on the
I2C bus at a time.
Drivers should acquire the bus lock, perform the I2C bus operations
necessary, and then release the bus lock.
Passing the
.Fa I2C_F_POLL
flag indicates to
.Fn iic_acquire_bus
that sleeping is not permitted.
.It Fn iic_release_bus "ic" "flags"
Release an exclusive lock on the I2C bus.
If the
.Fa I2C_F_POLL
flag was passed to
.Fn iic_acquire_bus ,
it must also be passed to
.Fn iic_release_bus .
.\" XXXJRT Someone help me with the mark-up here!
.\" .It Fn iic_exec "ic" "op" "addr" "cmdbuf" "cmdlen" "buf" "len" "flags"
.It Fn iic_exec "ic" "op" "addr" "cmdbuf" "cmdlen" "buf" "len"
Perform a series of I2C transactions on the bus.
.Fn iic_exec
initiates the operation by sending a START condition on the I2C
bus and then transmitting the address of the target device along
with the transaction type.
If
.Fa cmdlen
is non-zero, the command pointed to by
.Fa cmdbuf
is then sent to the device.
If
.Fa buflen
is non-zero,
.Fn iic_exec
will then transmit or receive the data, as indicated by
.Fa op .
If
.Fa op
indicates a read operation,
.Fn iic_exec
will send a REPEATED START before transferring the data.
If
.Fa op
so indicates, a STOP condition will be sent on the I2C
bus at the conclusion of the operation.
Passing the
.Fa I2C_F_POLL
flag indicates to
.Fn iic_exec
that sleeping is not permitted.
.It Fn iic_smbus_write_byte "ic" "addr" "cmd" "data" "flags"
Perform an SMBus WRITE BYTE operation.
This is equivalent to
I2C_OP_WRITE_WITH_STOP with
.Fa cmdlen
of 1 and
.Fa len
of 1.
.It Fn iic_smbus_read_byte "ic" "addr" "cmd" "datap" "flags"
Perform an SMBus READ BYTE operation.
This is equivalent to
I2C_OP_READ_WITH_STOP with
.Fa cmdlen
of 1 and
.Fa len
of 1.
.It Fn iic_smbus_receive_byte "ic" "addr" "datap" "flags"
Perform an SMBus RECEIVE BYTE operation.
This is equivalent to
I2C_OP_READ_WITH_STOP with
.Fa cmdlen
of 0 and
.Fa len
of 1.
.El
.Sh CONTROLLER INTERFACE
The I2C controller driver must fill in the function pointers of
an
.Fa i2c_controller
structure, which is defined as follows:
.Bd -literal
struct i2c_controller {
	void	*ic_cookie;	/* controller private */

	int	(*ic_acquire_bus)(void *, int);
	void	(*ic_release_bus)(void *, int);

	int	(*ic_exec)(void *, i2c_op_t, i2c_addr_t,
		   const void *, size_t, void *, size_t, int);

	int	(*ic_send_start)(void *, int);
	int	(*ic_send_stop)(void *, int);
	int	(*ic_initiate_xfer)(void *, i2c_addr_t, int);
	int	(*ic_read_byte)(void *, uint8_t *, int);
	int	(*ic_write_byte)(void *, uint8_t, int);
};
.Ed
.Pp
The
.Fn (*ic_acquire_bus)
and
.Fn (*ic_release_bus)
functions must always be provided.
.Pp
The controller driver may elect to provide an
.Fn (*ic_exec)
function.
This function is intended for use by automated controllers
that do not provide manual control over I2C bus conditions
such as START and STOP.
.Pp
If the
.Fn (*ic_exec)
function is not provided, the following 5 functions will be
used by
.Fn iic_exec
in order to execute the I2C bus operation:
.Bl -tag -width XXXX
.It Fn (*ic_send_start) "cookie" "flags"
Send a START condition on the I2C bus.
The
.Fa I2C_F_POLL
flag indicates that sleeping is not permitted.
.It Fn (*ic_send_stop) "cookie" "flags"
Send a STOP condition on the I2C bus.
The
.Fa I2C_F_POLL
flag indicates that sleeping is not permitted.
.It Fn (*ic_initiate_xfer) "cookie" "addr" "flags"
Initiate a transfer on the I2C bus by sending a START condition and
then transmitting the I2C device address and transfer type.
The
.Fa I2C_F_READ
flag indicates a read transfer; the lack of this flag indicates a
write transfer.
The
.Fa I2C_F_POLL
flag indicates that sleeping is not permitted.
The error code
.Dv ETIMEDOUT
should be returned if a timeout that would indicate that the
device is not present occurs.
.It Fn (*ic_read_byte) "cookie" "datap" "flags"
Read a byte from the I2C bus into the memory location referenced by
.Fa datap .
The
.Fa I2C_F_LAST
flag indicates that this is the final byte of the transfer, and that
a NACK condition should be sent on the I2C bus following the transfer
of the byte.
The
.Fa I2C_F_STOP
flag indicates that a STOP condition should be sent on the I2C bus following
the transfer of the byte.
The
.Fa I2C_F_POLL
flag indicates that sleeping is not permitted.
.It Fn (*ic_write_byte) "cookie" "data" "flags"
Write the byte contained in
.Fa data
to the I2C bus.
The
.Fa I2C_F_STOP
flag indicates that a STOP condition should be sent on the I2C bus following
the transfer of the byte.
The
.Fa I2C_F_POLL
flag indicates that sleeping is not permitted.
.El
.Sh SEE ALSO
.Xr iic 4
.Sh HISTORY
The
.Nm iic
API first appeared in
.Nx 2.0 .
.Ox
support was added in
.Ox 3.6 .
.Sh AUTHORS
The
.Nm iic
API was written by
Steve C. Woodford and Jason R. Thorpe for
.Nx
and then ported to
.Ox
by
.An Alexander Yurchenko Aq grange@openbsd.org .