1*7349a5e9Svisa.\" $OpenBSD: iic.9,v 1.10 2022/02/09 07:58:24 visa Exp $ 203992476Sgrange.\" 303992476Sgrange.\" Copyright (c) 2003 Wasabi Systems, Inc. 403992476Sgrange.\" All rights reserved. 503992476Sgrange.\" 603992476Sgrange.\" Written by Jason R. Thorpe for Wasabi Systems, Inc. 703992476Sgrange.\" 803992476Sgrange.\" Redistribution and use in source and binary forms, with or without 903992476Sgrange.\" modification, are permitted provided that the following conditions 1003992476Sgrange.\" are met: 1103992476Sgrange.\" 1. Redistributions of source code must retain the above copyright 1203992476Sgrange.\" notice, this list of conditions and the following disclaimer. 1303992476Sgrange.\" 2. Redistributions in binary form must reproduce the above copyright 1403992476Sgrange.\" notice, this list of conditions and the following disclaimer in the 1503992476Sgrange.\" documentation and/or other materials provided with the distribution. 1603992476Sgrange.\" 3. All advertising materials mentioning features or use of this software 1703992476Sgrange.\" must display the following acknowledgement: 1803992476Sgrange.\" This product includes software developed for the NetBSD Project by 1903992476Sgrange.\" Wasabi Systems, Inc. 2003992476Sgrange.\" 4. The name of Wasabi Systems, Inc. may not be used to endorse 2103992476Sgrange.\" or promote products derived from this software without specific prior 2203992476Sgrange.\" written permission. 2303992476Sgrange.\" 2403992476Sgrange.\" THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 2503992476Sgrange.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2603992476Sgrange.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2703992476Sgrange.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 2803992476Sgrange.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2903992476Sgrange.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3003992476Sgrange.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3103992476Sgrange.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3203992476Sgrange.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3303992476Sgrange.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3403992476Sgrange.\" POSSIBILITY OF SUCH DAMAGE. 3503992476Sgrange.\" 36*7349a5e9Svisa.Dd $Mdocdate: February 9 2022 $ 3733378d91Sjmc.Dt IIC_ACQUIRE_BUS 9 3803992476Sgrange.Os 3903992476Sgrange.Sh NAME 4003992476Sgrange.Nm iic_acquire_bus , 4103992476Sgrange.Nm iic_release_bus , 4203992476Sgrange.Nm iic_exec , 4303992476Sgrange.Nm iic_smbus_write_byte , 4403992476Sgrange.Nm iic_smbus_read_byte , 45*7349a5e9Svisa.Nm iic_smbus_receive_byte , 46*7349a5e9Svisa.Nm iic_is_compatible 4703992476Sgrange.Nd Inter IC (I2C) bus 4803992476Sgrange.Sh SYNOPSIS 49dddd2645Sschwarze.In dev/i2c/i2cvar.h 5003992476Sgrange.Ft int 5103992476Sgrange.Fo iic_acquire_bus 5203992476Sgrange.Fa "i2c_tag_t ic" 5303992476Sgrange.Fa "int flags" 5403992476Sgrange.Fc 5503992476Sgrange.Ft int 5603992476Sgrange.Fo iic_release_bus 5703992476Sgrange.Fa "i2c_tag_t ic" 5803992476Sgrange.Fa "int flags" 5903992476Sgrange.Fc 6003992476Sgrange.Ft int 6103992476Sgrange.Fo iic_exec 6203992476Sgrange.Fa "i2c_tag_t ic" 6303992476Sgrange.Fa "i2c_op_t op" 6403992476Sgrange.Fa "i2c_addr_t addr" 6503992476Sgrange.Fa "const void *cmdbuf" 6603992476Sgrange.Fa "size_t cmdlen" 6703992476Sgrange.Fa "void *buf" 6803992476Sgrange.Fa "size_t len" 6903992476Sgrange.Fa "int flags" 7003992476Sgrange.Fc 7103992476Sgrange.Ft int 7203992476Sgrange.Fo iic_smbus_write_byte 7303992476Sgrange.Fa "i2c_tag_t ic" 7403992476Sgrange.Fa "i2c_addr_t addr" 7503992476Sgrange.Fa "uint8_t cmd" 7603992476Sgrange.Fa "uint8_t data" 7703992476Sgrange.Fa "int flags" 7803992476Sgrange.Fc 7903992476Sgrange.Ft int 8003992476Sgrange.Fo iic_smbus_read_byte 8103992476Sgrange.Fa "i2c_tag_t ic" 8203992476Sgrange.Fa "i2c_addr_t addr" 8303992476Sgrange.Fa "uint8_t cmd" 8403992476Sgrange.Fa "uint8_t *datap" 8503992476Sgrange.Fa "int flags" 8603992476Sgrange.Fc 8703992476Sgrange.Ft int 8803992476Sgrange.Fo iic_smbus_receive_byte 8903992476Sgrange.Fa "i2c_tag_t ic" 9003992476Sgrange.Fa "i2c_addr_t addr" 9103992476Sgrange.Fa "uint8_t *datap" 9203992476Sgrange.Fa "int flags" 9303992476Sgrange.Fc 94*7349a5e9Svisa.Ft int 95*7349a5e9Svisa.Fo iic_is_compatible 96*7349a5e9Svisa.Fa "const struct i2c_attach_args *ia" 97*7349a5e9Svisa.Fa "const char *name" 98*7349a5e9Svisa.Fc 9903992476Sgrange.Sh DESCRIPTION 10003992476SgrangeI2C is a two-wire bus developed by Philips used for connecting 10103992476Sgrangeintegrated circuits. 10203992476SgrangeIt is commonly used for connecting devices such as EEPROMs, 10303992476Sgrangetemperature sensors, fan controllers, real-time clocks, tuners, 10403992476Sgrangeand other types of integrated circuits. 10503992476SgrangeThe 10603992476Sgrange.Nm iic 107f617753eSjmcinterface provides a means of communicating with I2C-connected devices. 10803992476SgrangeThe System Management Bus, or SMBus, is a variant of the I2C bus with 10903992476Sgrangea simplified command protocol and some electrical differences. 11003992476Sgrange.Sh DATA TYPES 11103992476SgrangeDrivers for devices attached to the I2C bus will make use of the 11203992476Sgrangefollowing data types: 11303992476Sgrange.Bl -tag -width i2c_addr_t 11403992476Sgrange.It Fa i2c_tag_t 11503992476SgrangeController tag for the I2C bus. 11603992476SgrangeThis is a pointer to a 11703992476Sgrange.Fa struct i2c_controller , 11803992476Sgrangeconsisting of function pointers filled in by the I2C 11903992476Sgrangecontroller driver. 12003992476Sgrange.It Fa i2c_op_t 12103992476SgrangeI2C bus operation. 12203992476SgrangeThe following I2C bus operations are defined: 12303992476Sgrange.Bl -tag -width XXXX 12403992476Sgrange.It I2C_OP_READ 12503992476SgrangePerform a read operation. 12603992476Sgrange.It I2C_OP_READ_WITH_STOP 12703992476SgrangePerform a read operation and send a STOP condition on the I2C bus at 12803992476Sgrangethe conclusion of the read. 12903992476Sgrange.It I2C_OP_WRITE 13003992476SgrangePerform a write operation. 13103992476Sgrange.It I2C_OP_WRITE_WITH_STOP 13203992476SgrangePerform a write operation and send a STOP condition on the I2C bus at 13303992476Sgrangethe conclusion of the write. 13403992476Sgrange.El 13503992476Sgrange.It Fa i2c_addr_t 13603992476SgrangeI2C device address. 13703992476Sgrange.It Fa struct i2c_attach_args 13803992476SgrangeDevices are attached to an I2C bus using this structure. 13903992476SgrangeThe structure is defined as follows: 14003992476Sgrange.Bd -literal 14103992476Sgrangestruct i2c_attach_args { 14203992476Sgrange i2c_tag_t ia_tag; /* controller */ 14303992476Sgrange i2c_addr_t ia_addr; /* address of device */ 14403992476Sgrange int ia_size; /* size (for EEPROMs) */ 14526b88e6eSdlg char *ia_name; /* chip name */ 146*7349a5e9Svisa size_t ia_namelen /* length of name concatenation */ 14726b88e6eSdlg void *ia_cookie; /* pass extra info from 14826b88e6eSdlg bus to dev */ 149*7349a5e9Svisa void *ia_intr /* interrupt info */ 150*7349a5e9Svisa int ia_poll; /* to force polling */ 15103992476Sgrange}; 15203992476Sgrange.Ed 15303992476Sgrange.El 15403992476Sgrange.Sh FUNCTIONS 15503992476SgrangeThe following functions comprise the API provided to drivers of 15603992476SgrangeI2C-connected devices: 15703992476Sgrange.Bl -tag -width iic_exec 15803992476Sgrange.It Fn iic_acquire_bus "ic" "flags" 15903992476SgrangeAcquire an exclusive lock on the I2C bus. 16003992476SgrangeThis is required since only one device may communicate on the 16103992476SgrangeI2C bus at a time. 16203992476SgrangeDrivers should acquire the bus lock, perform the I2C bus operations 16303992476Sgrangenecessary, and then release the bus lock. 16403992476SgrangePassing the 16503992476Sgrange.Fa I2C_F_POLL 16603992476Sgrangeflag indicates to 16703992476Sgrange.Fn iic_acquire_bus 16803992476Sgrangethat sleeping is not permitted. 16903992476Sgrange.It Fn iic_release_bus "ic" "flags" 17003992476SgrangeRelease an exclusive lock on the I2C bus. 17103992476SgrangeIf the 17203992476Sgrange.Fa I2C_F_POLL 17303992476Sgrangeflag was passed to 17403992476Sgrange.Fn iic_acquire_bus , 17503992476Sgrangeit must also be passed to 17603992476Sgrange.Fn iic_release_bus . 1770a715f02Sray.It Xo 1780a715f02Sray.Fo iic_exec 1790a715f02Sray.Fa "ic" 1800a715f02Sray.Fa "op" 1810a715f02Sray.Fa "addr" 1820a715f02Sray.Fa "cmdbuf" 1830a715f02Sray.Fa "cmdlen" 1840a715f02Sray.Fa "buf" 1850a715f02Sray.Fa "len" 1860a715f02Sray.Fa "flags" 1870a715f02Sray.Fc 1880a715f02Sray.Xc 189937e1738SdavidPerform a series of I2C transactions on the bus. 19003992476Sgrange.Fn iic_exec 19103992476Sgrangeinitiates the operation by sending a START condition on the I2C 19203992476Sgrangebus and then transmitting the address of the target device along 19303992476Sgrangewith the transaction type. 19403992476SgrangeIf 19503992476Sgrange.Fa cmdlen 19603992476Sgrangeis non-zero, the command pointed to by 19703992476Sgrange.Fa cmdbuf 198f617753eSjmcis then sent to the device. 19903992476SgrangeIf 20003992476Sgrange.Fa buflen 20103992476Sgrangeis non-zero, 20203992476Sgrange.Fn iic_exec 20303992476Sgrangewill then transmit or receive the data, as indicated by 20403992476Sgrange.Fa op . 20503992476SgrangeIf 20603992476Sgrange.Fa op 20703992476Sgrangeindicates a read operation, 20803992476Sgrange.Fn iic_exec 20903992476Sgrangewill send a REPEATED START before transferring the data. 21003992476SgrangeIf 21103992476Sgrange.Fa op 21203992476Sgrangeso indicates, a STOP condition will be sent on the I2C 21303992476Sgrangebus at the conclusion of the operation. 21403992476SgrangePassing the 21503992476Sgrange.Fa I2C_F_POLL 21603992476Sgrangeflag indicates to 21703992476Sgrange.Fn iic_exec 21803992476Sgrangethat sleeping is not permitted. 21903992476Sgrange.It Fn iic_smbus_write_byte "ic" "addr" "cmd" "data" "flags" 220f617753eSjmcPerform an SMBus WRITE BYTE operation. 221f617753eSjmcThis is equivalent to 22203992476SgrangeI2C_OP_WRITE_WITH_STOP with 22303992476Sgrange.Fa cmdlen 22403992476Sgrangeof 1 and 22503992476Sgrange.Fa len 22603992476Sgrangeof 1. 22703992476Sgrange.It Fn iic_smbus_read_byte "ic" "addr" "cmd" "datap" "flags" 228f617753eSjmcPerform an SMBus READ BYTE operation. 229f617753eSjmcThis is equivalent to 23003992476SgrangeI2C_OP_READ_WITH_STOP with 23103992476Sgrange.Fa cmdlen 23203992476Sgrangeof 1 and 23303992476Sgrange.Fa len 23403992476Sgrangeof 1. 23503992476Sgrange.It Fn iic_smbus_receive_byte "ic" "addr" "datap" "flags" 236f617753eSjmcPerform an SMBus RECEIVE BYTE operation. 237f617753eSjmcThis is equivalent to 23803992476SgrangeI2C_OP_READ_WITH_STOP with 23903992476Sgrange.Fa cmdlen 24003992476Sgrangeof 0 and 24103992476Sgrange.Fa len 24203992476Sgrangeof 1. 243*7349a5e9Svisa.It Fn iic_is_compatible "ia" "name" 244*7349a5e9SvisaTest if the device is compatible with 245*7349a5e9Svisa.Fa name . 24603992476Sgrange.El 24703992476Sgrange.Sh CONTROLLER INTERFACE 24803992476SgrangeThe I2C controller driver must fill in the function pointers of 24903992476Sgrangean 25003992476Sgrange.Fa i2c_controller 25103992476Sgrangestructure, which is defined as follows: 25203992476Sgrange.Bd -literal 25303992476Sgrangestruct i2c_controller { 25403992476Sgrange void *ic_cookie; /* controller private */ 25503992476Sgrange 25603992476Sgrange int (*ic_acquire_bus)(void *, int); 25703992476Sgrange void (*ic_release_bus)(void *, int); 25803992476Sgrange 25903992476Sgrange int (*ic_exec)(void *, i2c_op_t, i2c_addr_t, 26003992476Sgrange const void *, size_t, void *, size_t, int); 26103992476Sgrange 26203992476Sgrange int (*ic_send_start)(void *, int); 26303992476Sgrange int (*ic_send_stop)(void *, int); 26403992476Sgrange int (*ic_initiate_xfer)(void *, i2c_addr_t, int); 26503992476Sgrange int (*ic_read_byte)(void *, uint8_t *, int); 26603992476Sgrange int (*ic_write_byte)(void *, uint8_t, int); 26703992476Sgrange}; 26803992476Sgrange.Ed 26903992476Sgrange.Pp 27003992476SgrangeThe 27103992476Sgrange.Fn (*ic_acquire_bus) 27203992476Sgrangeand 27303992476Sgrange.Fn (*ic_release_bus) 27403992476Sgrangefunctions must always be provided. 27503992476Sgrange.Pp 27603992476SgrangeThe controller driver may elect to provide an 27703992476Sgrange.Fn (*ic_exec) 27803992476Sgrangefunction. 27903992476SgrangeThis function is intended for use by automated controllers 28003992476Sgrangethat do not provide manual control over I2C bus conditions 28103992476Sgrangesuch as START and STOP. 28203992476Sgrange.Pp 28303992476SgrangeIf the 28403992476Sgrange.Fn (*ic_exec) 28503992476Sgrangefunction is not provided, the following 5 functions will be 28603992476Sgrangeused by 28703992476Sgrange.Fn iic_exec 28803992476Sgrangein order to execute the I2C bus operation: 28903992476Sgrange.Bl -tag -width XXXX 29003992476Sgrange.It Fn (*ic_send_start) "cookie" "flags" 29103992476SgrangeSend a START condition on the I2C bus. 29203992476SgrangeThe 29303992476Sgrange.Fa I2C_F_POLL 29403992476Sgrangeflag indicates that sleeping is not permitted. 29503992476Sgrange.It Fn (*ic_send_stop) "cookie" "flags" 29603992476SgrangeSend a STOP condition on the I2C bus. 29703992476SgrangeThe 29803992476Sgrange.Fa I2C_F_POLL 29903992476Sgrangeflag indicates that sleeping is not permitted. 30003992476Sgrange.It Fn (*ic_initiate_xfer) "cookie" "addr" "flags" 30103992476SgrangeInitiate a transfer on the I2C bus by sending a START condition and 30203992476Sgrangethen transmitting the I2C device address and transfer type. 30303992476SgrangeThe 30403992476Sgrange.Fa I2C_F_READ 30503992476Sgrangeflag indicates a read transfer; the lack of this flag indicates a 30603992476Sgrangewrite transfer. 30703992476SgrangeThe 30803992476Sgrange.Fa I2C_F_POLL 30903992476Sgrangeflag indicates that sleeping is not permitted. 31003992476SgrangeThe error code 31103992476Sgrange.Dv ETIMEDOUT 31203992476Sgrangeshould be returned if a timeout that would indicate that the 31303992476Sgrangedevice is not present occurs. 31403992476Sgrange.It Fn (*ic_read_byte) "cookie" "datap" "flags" 31503992476SgrangeRead a byte from the I2C bus into the memory location referenced by 31603992476Sgrange.Fa datap . 31703992476SgrangeThe 31803992476Sgrange.Fa I2C_F_LAST 31903992476Sgrangeflag indicates that this is the final byte of the transfer, and that 32003992476Sgrangea NACK condition should be sent on the I2C bus following the transfer 32103992476Sgrangeof the byte. 32203992476SgrangeThe 32303992476Sgrange.Fa I2C_F_STOP 32403992476Sgrangeflag indicates that a STOP condition should be sent on the I2C bus following 32503992476Sgrangethe transfer of the byte. 32603992476SgrangeThe 32703992476Sgrange.Fa I2C_F_POLL 32803992476Sgrangeflag indicates that sleeping is not permitted. 32903992476Sgrange.It Fn (*ic_write_byte) "cookie" "data" "flags" 33003992476SgrangeWrite the byte contained in 33103992476Sgrange.Fa data 33203992476Sgrangeto the I2C bus. 33303992476SgrangeThe 33403992476Sgrange.Fa I2C_F_STOP 33503992476Sgrangeflag indicates that a STOP condition should be sent on the I2C bus following 33603992476Sgrangethe transfer of the byte. 33703992476SgrangeThe 33803992476Sgrange.Fa I2C_F_POLL 33903992476Sgrangeflag indicates that sleeping is not permitted. 34003992476Sgrange.El 34103992476Sgrange.Sh SEE ALSO 34203992476Sgrange.Xr iic 4 34303992476Sgrange.Sh HISTORY 34403992476SgrangeThe 34503992476Sgrange.Nm iic 34603992476SgrangeAPI first appeared in 34703992476Sgrange.Nx 2.0 . 34803992476Sgrange.Ox 34903992476Sgrangesupport was added in 35003992476Sgrange.Ox 3.6 . 35103992476Sgrange.Sh AUTHORS 35203992476SgrangeThe 35303992476Sgrange.Nm iic 35403992476SgrangeAPI was written by 35503992476SgrangeSteve C. Woodford and Jason R. Thorpe for 35603992476Sgrange.Nx 35703992476Sgrangeand then ported to 35803992476Sgrange.Ox 35903992476Sgrangeby 360f0641c22Sschwarze.An Alexander Yurchenko Aq Mt grange@openbsd.org . 361