</>EmbeddedInterview

How do you write an I2C (or SPI) client driver?

MediumLinux Kernel & Device Drivers

Short answer: Register a struct i2c_driver with an OF match table and an i2c_device_id table. In probe(struct i2c_client *client) you get the device's address and adapter; talk to it with i2c_smbus_read_byte_data()/i2c_smbus_write_byte_data(), i2c_transfer() for combined messages, or more commonly through regmap (devm_regmap_init_i2c). Then register with the right subsystem (hwmon, IIO, input...). These calls sleep, so use them only in process context (threaded IRQs, workqueues), never in a hard IRQ handler or under a spinlock.

#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/regmap.h>

#define REG_ID   0x0F
#define REG_CTRL 0x20

struct mysensor { struct regmap *map; };

static const struct regmap_config mysensor_regmap = {
    .reg_bits = 8, .val_bits = 8, .max_register = 0x7F,
};

static int mysensor_probe(struct i2c_client *client)
{
    struct mysensor *s = devm_kzalloc(&client->dev, sizeof(*s), GFP_KERNEL);
    unsigned int id;
    int ret;

    if (!s) return -ENOMEM;
    s->map = devm_regmap_init_i2c(client, &mysensor_regmap);
    if (IS_ERR(s->map)) return PTR_ERR(s->map);

    ret = regmap_read(s->map, REG_ID, &id);
    if (ret) return dev_err_probe(&client->dev, ret, "no response at 0x%02x\n", client->addr);
    if (id != 0x33) return dev_err_probe(&client->dev, -ENODEV, "unexpected id 0x%02x\n", id);

    ret = regmap_update_bits(s->map, REG_CTRL, 0x0F, 0x07);   /* enable axes, set rate */
    if (ret) return ret;

    i2c_set_clientdata(client, s);
    return 0;                                   /* then register with IIO/hwmon/input */
}

static const struct of_device_id mysensor_of_match[] = { { .compatible = "acme,mysensor" }, { } };
MODULE_DEVICE_TABLE(of, mysensor_of_match);
static const struct i2c_device_id mysensor_id[] = { { "mysensor" }, { } };
MODULE_DEVICE_TABLE(i2c, mysensor_id);

static struct i2c_driver mysensor_driver = {
    .driver   = { .name = "mysensor", .of_match_table = mysensor_of_match },
    .probe    = mysensor_probe,
    .id_table = mysensor_id,
};
module_i2c_driver(mysensor_driver);
MODULE_LICENSE("GPL");
&i2c1 {
    status = "okay";
    clock-frequency = <400000>;
    sensor@18 {
        compatible = "acme,mysensor";
        reg = <0x18>;
        interrupt-parent = <&gpio2>;
        interrupts = <5 IRQ_TYPE_EDGE_RISING>;
    };
};

SPI is almost identical: struct spi_driver, probe(struct spi_device *spi), spi_setup() for mode/speed, spi_sync()/spi_write_then_read() or devm_regmap_init_spi(), and spi-max-frequency in the device tree.

Why regmap: one register API for I2C, SPI and MMIO, a register cache (fewer bus transactions, restore after suspend), range checks, and a debugfs dump of all registers (/sys/kernel/debug/regmap/).

Testing without a driver: i2cdetect, i2cget, i2cset (with -f if a driver owns the address).