I2C and SPI Explained for Linux Driver Developers
Learn how I2C and SPI work on the wire, what each costs in pins and speed, and how Linux splits each into controller drivers and client drivers for developers.

Sensors, displays, flash chips, ADCs and codecs rarely connect to a processor with a wide parallel bus. They use one of two small serial buses, I2C or SPI, which need only a few wires and are supported by nearly every microcontroller and SoC. If you write a Linux driver for such a chip, you need two mental models: how the bus behaves electrically and in time, and how Linux divides the software work between the code that drives the bus and the code that talks to the chip. This post gives both from the beginning, with enough detail on the wire protocols to read a logic analyzer trace and enough on the kernel structure to know which file you will write. It uses Linux 6.12 terminology and the kernel's I2C and SPI documentation.
I2C on the wire
I2C (Inter-Integrated Circuit, pronounced "eye squared see") uses two signal lines shared by every device on the bus: SDA for data and SCL for the clock. Both lines are open-drain with pull-up resistors, so any device can pull a line low and none can drive it high. That is what lets many devices share two wires without fighting.
One device is the controller (the older term is master), and it generates the clock. Every other device is a target (formerly slave) with a 7-bit address, or in rarer cases 10 bits. A transfer goes like this:
The controller sends a start condition: SDA falls while SCL is high.
It sends the 7-bit address and one bit for direction, 0 for write and 1 for read.
The addressed target pulls SDA low for one clock to acknowledge (ACK). No acknowledgment, a NACK, means nobody answered.
Data bytes follow, each followed by an ACK from the receiver.
The controller sends a stop condition: SDA rises while SCL is high.
Standard speeds are 100 kHz (standard mode), 400 kHz (fast mode) and 1 MHz (fast mode plus), with faster modes defined in the NXP I2C specification. A target can hold SCL low to make the controller wait, called clock stretching.
A typical register read in a sensor combines a write and a read joined by a repeated start: write the register number, then read the value, without a stop in between. That combination is why the I2C API in Linux works in terms of messages: a transfer is a list of struct i2c_msg, each with an address, flags, length and buffer.
SPI on the wire
SPI (Serial Peripheral Interface) uses four lines: SCLK (clock), MOSI (controller out, target in), MISO (controller in, target out) and one chip select per target. The names MOSI and MISO are being replaced by COPI and CIPO in newer documents, and Linux has updated its terminology to match, but datasheets and boards still use the old ones.
There is no addressing and no acknowledgment. The controller pulls one chip select low, and that selects the device. Then it clocks bits out on MOSI while clocking bits in on MISO at the same time, so every transfer is full duplex, even when only one direction carries useful data. Speed is whatever the target supports, commonly 1 to 50 MHz.
Because there is no standard, SPI devices differ in four ways the driver must set:
| Setting | Meaning |
|---|---|
| Clock polarity (CPOL) | Level of SCLK when idle |
| Clock phase (CPHA) | Which clock edge samples data |
| Bit order | Most significant bit first, or least significant |
| Word size | Bits per word, usually 8 |
CPOL and CPHA combine into four modes, 0 to 3, and the datasheet states which one a chip needs. Getting the mode wrong gives data shifted by a bit or garbage.
The comparison
| Aspect | I2C | SPI |
|---|---|---|
| Wires | 2, shared | 3 shared plus one chip select per device |
| Addressing | In-band, 7-bit address | Chip select pin |
| Speed | 100 kHz to a few MHz | Often tens of MHz |
| Duplex | Half | Full |
| Acknowledgment | Per byte | None |
| Pin cost for many devices | Low | Grows with device count |
| Typical devices | Sensors, EEPROMs, RTCs, PMICs | Flash, ADCs, displays, radios |
Pick I2C for slow devices when pins are scarce, and SPI when throughput matters or the device offers only SPI. Many chips offer both interfaces, selected by a pin strap.
How Linux structures it
Linux separates each bus into two kinds of driver, and the split is the same for both.
A controller driver (called an adapter in I2C, a controller or host in SPI) knows how to operate the hardware block in the SoC that generates the clock and shifts bits. These live in drivers/i2c/busses/ and drivers/spi/, and they are platform drivers, as covered earlier in this cluster. The controller driver registers itself with the bus core: i2c_add_adapter for I2C, spi_register_controller for SPI.
A client driver (also called a protocol driver) knows the chip on the other end: its registers, its commands, what the values mean. These are i2c_driver and spi_driver objects, and they live under the subsystem the chip belongs to, such as drivers/iio/ or drivers/hwmon/. A client driver never touches the controller hardware. It builds messages and asks the core to send them.
The device model ties them together. A controller creates a bus, and children of the controller become client devices. They come from firmware: a device tree node under the controller node, ACPI, or explicit instantiation from code or sysfs.
&i2c1 {
status = "okay";
clock-frequency = <400000>;
temp@48 {
compatible = "ti,tmp102";
reg = <0x48>;
};
};
&spi0 {
status = "okay";
adc@0 {
compatible = "microchip,mcp3008";
reg = <0>;
spi-max-frequency = <1350000>;
};
};For the I2C child, reg is the 7-bit address. For the SPI child, reg is the chip select number, and spi-max-frequency caps the clock. SPI mode comes from boolean properties spi-cpol and spi-cpha where needed.
The client driver API
The kernel offers a small, regular API for each bus. For I2C, the call that sends and receives messages is i2c_transfer, with convenient wrappers for the common SMBus-style register accesses:
#include <linux/i2c.h>
static int tmp_read_temp(struct i2c_client *client, int *millicelsius)
{
s32 raw = i2c_smbus_read_word_swapped(client, 0x00);
if (raw < 0)
return raw;
*millicelsius = ((s16)raw >> 4) * 625 / 10;
return 0;
}For SPI, the client builds a struct spi_transfer and sends it with spi_sync, or uses helpers such as spi_write_then_read. The later posts in this cluster write complete clients for each bus.
In both cases the client driver registers with a driver structure whose probe receives the client or device, parallel to the platform driver pattern.
Looking at them on a running system
Both buses show up in sysfs and /dev. An I2C adapter appears as /sys/bus/i2c/devices/i2c-1, and each client as 1-0048 for bus 1, address 0x48. SPI devices appear as spi0.0 for controller 0, chip select 0:
ls /sys/bus/i2c/devices /sys/bus/spi/devices/sys/bus/i2c/devices:
1-0048 i2c-1
/sys/bus/spi/devices:
spi0.0The names above are examples. If a device is wired and described but its entry is missing, the node is disabled or the parent controller failed to probe.
Boundaries
This is the minimum background. Real work adds details: I2C has multi-controller arbitration, SMBus rules for timeouts and PEC checksums, and bus recovery for a stuck SDA line. SPI has quad and octal variants for flash. Other buses, such as I3C, UART and CAN, work differently and use other subsystems. And electrical design, such as pull-up values and trace lengths, is outside what a driver can fix.


