Skip to content
systemcoding.dev
Embedded Linux

Describe an I2C Sensor in a Device Tree Node

Add a TMP102 temperature sensor to an I2C bus in a device tree with compatible, reg and status, then check that Linux bound the driver and exposes a reading.

6 min read
Summarize in AI
Flat illustration of a single-board computer with its SoC, memory, header and ports, with the Linux, Raspberry Pi and Debian logos.

Wire a temperature sensor to two pins of a board, boot Linux, and nothing happens. The kernel does not scan I2C buses for devices, because probing random addresses can confuse chips that share a bus, so it will not find the sensor unless the device tree says it exists. The description is five lines: which bus, which address, which chip. This post adds a TMP102 temperature sensor, a common 12-bit digital sensor, to an I2C bus in a board's device tree for Linux 6.12, explains each property, shows how to verify the result from a shell and lists the reasons it silently fails to appear.

What you need to know about the hardware

Before writing a line, collect three facts from the board schematic and the sensor datasheet:

  1. Which I2C controller the sensor is wired to. The SoC has several, named in its device tree i2c0, i2c1 and so on, or by a label from the SoC's .dtsi file. Look in the board's schematic for the pin names SDA and SCL and map them to a controller in the SoC manual.

  2. The device's 7-bit address. A TMP102 has four possible addresses from 0x48 to 0x4b, chosen by where its ADD0 pin is tied. Tied to ground gives 0x48.

  3. The compatible string. It is in the driver's binding. For this chip the kernel binding is ti,tmp102, and its driver is the lm75 family driver named tmp102 in drivers/hwmon/tmp102.c.

Most mistakes in this job are the first two. The address is the 7-bit form without the read or write bit, so a datasheet that lists 0x90 as the address byte means 0x48: shift right by one.

The node

Device tree sources of a SoC define the I2C controller and leave it disabled. A board file enables it and adds the devices wired to it. It does so by referring to the controller's label:

arch/arm64/boot/dts/acme/acme-demo.dtsdts
&i2c1 {
	status = "okay";
	clock-frequency = <400000>;
	#address-cells = <1>;
	#size-cells = <0>;

	temperature-sensor@48 {
		compatible = "ti,tmp102";
		reg = <0x48>;
	};
};

Go through it:

  • &i2c1 is a reference to the node labeled i2c1 in the SoC .dtsi. Properties written in this block are merged into that node. It is how a board customizes the SoC without copying it.

  • status = "okay" turns the controller on. In the SoC file it is disabled so that boards that do not use the bus pay nothing.

  • clock-frequency = <400000> sets the bus speed to 400 kHz, I2C fast mode. The TMP102 supports it. Omit the property and the controller driver uses its default, usually 100 kHz.

  • #address-cells = <1> and #size-cells = <0> tell the children how to read reg: one cell for the address and none for a size, because an I2C device has an address and no length. The SoC file normally sets these already, and repeating them is harmless, but check, since a missing pair is a common warning source.

  • temperature-sensor@48 is the node name. The name before the @ is a generic role, from the list in the specification, and the number after it is the address in hex, written the same as in reg.

  • compatible = "ti,tmp102" selects the driver.

  • reg = <0x48> is the 7-bit I2C address.

If the sensor has an alert pin wired to an interrupt, add the interrupt parent and number. The binding for ti,tmp102 documents the optional properties; do not add what the hardware does not use.

Build and boot it

Build the board's tree with the kernel build and copy the result to the boot partition:

Shell
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- acme/acme-demo.dtb

Reboot, and check that the I2C bus, the device and the driver all agree. The i2cdetect tool from i2c-tools scans a bus, and a device already claimed by a driver shows as UU:

Shell
i2cdetect -y 1
Text
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         

UU at row 40, column 8 is address 0x48. It means the kernel owns that address. A 48 printed there instead would mean the chip answered but no driver bound, and -- would mean no answer at all. Now confirm the driver and read a value:

Shell
cat /sys/bus/i2c/devices/1-0048/name
cat /sys/class/hwmon/hwmon*/temp1_input
Text
tmp102
24562

The number is millidegrees Celsius, so 24562 is 24.562 degrees. Your reading will be different. The directory name 1-0048 is <bus number>-<address as four hex digits>, which is how you find the device in sysfs. The driver creates a hardware monitoring class device, and hwmon* finds it whatever its index is.

When the sensor does not appear

Work down this list in order.

The bus is not enabled. If i2cdetect -l does not list your bus, the status = "okay" line did not take effect, or the controller driver did not probe. Check dmesg | grep -i i2c.

The device exists but no driver bound. ls /sys/bus/i2c/devices/ shows 1-0048, but there is no driver link inside it. The compatible is wrong, or the driver is not in the kernel. Check CONFIG_SENSORS_TMP102 and read modinfo tmp102 if it is a module. A module must be loadable: the kernel loads it from the of: alias, so it should appear on its own.

The pull-up resistors are missing. I2C needs a pull-up on each line. A board without them reads all -- or nothing from the bus, and no device tree edit fixes it. Measure with a scope or meter, not with the kernel.

The pins are not muxed to I2C. On SoCs where a pin can serve several functions, the bus needs a pinctrl-0 that selects the I2C function. That is a separate post, and it is the usual cause of an enabled bus that sees nothing.

The address is wrong. If i2cdetect shows the chip at 0x49 and your node says 0x48, the ADD0 strap differs from your reading of the schematic. Trust the scan, then fix the schematic or the node.

i2cdetect sends probe transactions that can disturb some devices, and the -r and -q modes use different methods. Do not run it blind on a bus with unknown chips, such as a PMIC; read the datasheet first.

Where the node goes

Put sensors in the board's .dts, not in the SoC's .dtsi, because they are properties of the board. If many boards use the same module, such as a system-on-module with its own sensors, put them in the module's .dtsi, included by each carrier. Keep the node inside the controller block: a device node placed at the root with a reg of 0x48 is not an I2C device, it is a memory mapped one at that address, and the kernel will not touch the bus for it.

Also inHardware
Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, C and GNU logos.
Kernels

Write a Pin Controller Driver With pinctrl

Write a Linux 6.12 pin controller driver: pinctrl_desc, pins, groups and functions, the set_mux callback, and the device tree states that consumers select.

8 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.