project
A real Linux kernel driver for the TMP102
The sensor layer of a larger product driver — kernel-space rather than a user-space i2c-dev loop, for lower per-read cost and direct control of the bus. hwmon integration and a device tree binding mean lm_sensors finds it with no configuration.
This is the temperature-sensing piece of a larger kernel driver for a product. It lives in
kernel space rather than as a user-space i2c-dev loop for two reasons: speed and control.
A user-space poll pays two syscalls and a pair of context switches per reading, and it
competes with every other process for the bus. In the kernel the read is a direct SMBus
transaction, the cache and the bus access sit behind the same mutex, and the sensor becomes
available to other in-kernel consumers — thermal zones, other drivers in the same stack —
rather than only to the one process holding the file descriptor.
The driver handles I2C over SMBus, converts the TMP102’s 12-bit two’s complement output to
millidegrees Celsius with integer-only arithmetic, caches readings under a mutex to avoid
hammering the bus, and exposes the standard sysfs interface at
/sys/class/hwmon/hwmonX/temp1_input. A device tree overlay binds it automatically on the
Pi’s I2C1 bus, so the sensor is live from boot. Every allocation goes through devm_*, so
there are no leaks on driver removal.
Design notes
Registering with hwmon rather than exposing a custom sysfs node was the decision with the longest tail. It costs a little up front, and in return every standard monitoring tool — lm_sensors, collectd, Grafana agents — finds the sensor with no configuration, including tools nobody has written yet.
The device tree binding is what makes the driver drop into the product image without board-specific glue. Probe order, bus, and address all come from DTS, so the same module works across hardware revisions that move the sensor.
Fixed-point conversion is done with shift-multiply-divide and no floating point, which is non-negotiable in kernel space and recurs in nearly every sensor driver in the stack — IMUs, pressure, humidity.