IIC(9)
π IIC(9) β the Inter IC (I2C) bus, explained by your favorite kernel gremlin π
okay bestie, sit down, grab a strawberry π β this is IIC(9), from the Kernel Developer's Manual, and it's literally the group chat between your kernel and every tiny chip on the board. no cap, it's iconic.
β¨ NAME β¨
iic_acquire_bus, iic_release_bus, iic_exec, iic_smbus_write_byte, iic_smbus_read_byte, iic_smbus_receive_byte β Inter IC (I2C) bus, aka the six functions you'll be vibing with π
πΈ SYNOPSIS πΈ
#include <dev/i2c/i2cvar.h>
int
iic_acquire_bus(i2c_tag_t ic, int flags);
void
iic_release_bus(i2c_tag_t ic, int flags);
int
iic_exec(i2c_tag_t ic, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
size_t cmdlen, void *buf, size_t buflen, int flags);
int
iic_smbus_write_byte(i2c_tag_t ic, i2c_addr_t addr, uint8_t cmd,
uint8_t data, int flags);
int
iic_smbus_read_byte(i2c_tag_t ic, i2c_addr_t addr, uint8_t cmd,
uint8_t *datap, int flags);
int
iic_smbus_receive_byte(i2c_tag_t ic, i2c_addr_t addr, uint8_t *datap,
int flags);
like, just look at those signatures. gorgeous. no notes. π«Ά
π¦ DESCRIPTION π¦
so I2C is a two-wire bus developed by Philips, used for connecting integrated circuits, and honestly? it ate. it's the main character of board-level comms.
it's commonly used for connecting devices such as EEPROMs, temperature sensors, fan controllers, real-time clocks, tuners, and other types of integrated circuits. every single one of those is lowkey doing the most on just two little wires.
the iic interface provides a means of communicating with I2C-connected devices. that's the whole vibe check: you talk, the chip talks back, everybody's happy β¨
and then there's the System Management Bus, or SMBus β a variant of the I2C bus with a simplified command protocol and some electrical differences. same energy, slightly different outfit π
π DATA TYPES π
Drivers for devices attached to the I2C bus will make use of the following data types β learn them, love them, they're your new besties:
i2c_tag_tβ Controller tag for the I2C bus. this is a pointer to astruct i2c_controller, consisting of function pointers filled in by the I2C controller driver. so it's like the controller's contact card πi2c_op_tβ I2C bus operation. the following I2C bus operations are defined:I2C_OP_READβ Perform a read operation.I2C_OP_READ_WITH_STOPβ Perform a read operation and send a STOP condition on the I2C bus at the conclusion of the read. read it and yeet the STOP, basically πI2C_OP_WRITEβ Perform a write operation.I2C_OP_WRITE_WITH_STOPβ Perform a write operation and send a STOP condition on the I2C bus at the conclusion of the write.
i2c_addr_tβ I2C device address. that's the chip's @, iykyk.struct i2c_attach_argsβ Devices are attached to an I2C bus using this structure. The structure is defined as follows:
struct i2c_attach_args {
i2c_tag_t ia_tag; /* controller */
i2c_addr_t ia_addr; /* address of device */
int ia_size; /* size (for EEPROMs) */
};
π FUNCTIONS π
The following functions comprise the API provided to drivers of I2C-connected devices. this is the good stuff, bestie, pay attention π₯
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 β it's giving "one mic, one voice" π€. Drivers should acquire the bus lock, perform the I2C bus operations necessary, and then release the bus lock. Passing theI2C_F_POLLflag indicates toiic_acquire_bus()that sleeping is not permitted. no naps allowed π΄βiic_release_bus(ic, flags)β Release an exclusive lock on the I2C bus. If theI2C_F_POLLflag was passed toiic_acquire_bus(), it must also be passed toiic_release_bus(). symmetric behavior, periodt.iic_exec(ic, op, addr, cmdbuf, cmdlen, buf, buflen, flags)β Perform a series of I2C transactions on the bus. so here's the choreography, bestie: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
cmdlenis non-zero, the command pointed to bycmdbufis then sent to the device. - If
buflenis non-zero,iic_exec()will then transmit or receive the data, as indicated byop. - If
opindicates a read operation,iic_exec()will send a REPEATED START before transferring the data. - If
opso indicates, a STOP condition will be sent on the I2C bus at the conclusion of the operation. - Passing the
I2C_F_POLLflag indicates toiic_exec()that sleeping is not permitted.
iic_smbus_write_byte(ic, addr, cmd, data, flags)β Perform an SMBus WRITE BYTE operation. This is equivalent toI2C_OP_WRITE_WITH_STOPwithcmdlenof1andbuflenof1. one byte in, one byte out, slay πiic_smbus_read_byte(ic, addr, cmd, datap, flags)β Perform an SMBus READ BYTE operation. This is equivalent toI2C_OP_READ_WITH_STOPwithcmdlenof1andbuflenof1.iic_smbus_receive_byte(ic, addr, datap, flags)β Perform an SMBus RECEIVE BYTE operation. This is equivalent toI2C_OP_READ_WITH_STOPwithcmdlenof0andbuflenof1. zero command bytes, still iconic.
π CONTROLLER INTERFACE π
now for the other side of the relationship, the controller driver's side π«Ά. The I2C controller driver must fill in the function pointers of an i2c_controller structure, which is defined as follows:
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);
};
The (*ic_acquire_bus)() and (*ic_release_bus)() functions must always be provided. mandatory. non-negotiable. the bare minimum π
The controller driver may elect to provide an (*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. fancy automated controllers get to skip the micromanagement β¨
If the (*ic_exec)() function is not provided, the following 5 functions will be used by iic_exec() in order to execute the I2C bus operation:
(*ic_send_start)(cookie, flags)β Send a START condition on the I2C bus. TheI2C_F_POLLflag indicates that sleeping is not permitted. go go go π¦(*ic_send_stop)(cookie, flags)β Send a STOP condition on the I2C bus. TheI2C_F_POLLflag indicates that sleeping is not permitted. that's a wrap, bestie π(*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. TheI2C_F_READflag indicates a read transfer; the lack of this flag indicates a write transfer. TheI2C_F_POLLflag indicates that sleeping is not permitted. The error codeETIMEDOUTshould be returned if a timeout that would indicate that the device is not present occurs. so if nobody answers... it's giving ghosted π»(*ic_read_byte)(cookie, datap, flags)β Read a byte from the I2C bus into the memory location referenced bydatap. TheI2C_F_LASTflag 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. TheI2C_F_STOPflag indicates that a STOP condition should be sent on the I2C bus following the transfer of the byte. TheI2C_F_POLLflag indicates that sleeping is not permitted.(*ic_write_byte)(cookie, data, flags)β Write the byte contained indatato the I2C bus. TheI2C_F_STOPflag indicates that a STOP condition should be sent on the I2C bus following the transfer of the byte. TheI2C_F_POLLflag indicates that sleeping is not permitted.
π§ SEE ALSO π§
iic(4)i2cscan(8)- NXP Semiconductors, I2C-bus Specification and User Manual, Revision 03, http://www.ics.nxp.com/support/documents/i2c/pdf/i2c.bus.specification.pdf, June 19, 2007.
- Duracell Inc. et. al., System Management Bus (SMBus) Specification, Version 2.0, http://smbus.org/specs/smbus20.pdf, August 3, 2000.
like, go read the specs, they're long but they're the moment πβ¨
β HISTORY β
The iic API first appeared in NetBSD 2.0. OpenBSD support was added in OpenBSD 3.6. a glow-up across two operating systems, we love to see it π
π AUTHORS π
The iic API was written by Steve C. Woodford and Jason R. Thorpe for NetBSD and then ported to OpenBSD by Alexander Yurchenko <grange@openbsd.org>. legends, all three of them πππ
De bian β October 15, 2011 β that's the source/version and date on this one, so shoutout to the Debian manpage packaging era, mwah π this page has been a whole vibe, hope I2C makes sense now, love u bye πβ¨
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 2003 Wasabi Systems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms