Skip to content
systemcoding.dev
Kernels

Read a register with i2c_smbus_read_byte_data

i2c_smbus_read_byte_data() reads one 8-bit register from an I2C client in a single call and returns the value or a negative errno (Linux 6.12).

2 min read
Summarize in AI

For a sensor with one-byte registers, the SMBus helpers are the shortest path. i2c_smbus_read_byte_data() sends the register index, issues a repeated start and reads one byte back, all as one transaction. The return type is the trap: it is an s32, with the byte in the low bits and a negative errno on failure, so a u8 variable throws the error away. This sample is for Linux 6.12.

c
#define DEMO_WHO_AM_I	0x0f

static int demo_check_id(struct i2c_client *client)
{
	int id = i2c_smbus_read_byte_data(client, DEMO_WHO_AM_I);

	if (id < 0)
		return dev_err_probe(&client->dev, id, "who-am-i read failed\n");

	dev_info(&client->dev, "chip id 0x%02x\n", id);
	return 0;
}
Text
demo 1-0048: chip id 0x33

The chip id shown is an example for a made-up part. Check the value against the datasheet before treating a mismatch as fatal, because boards ship with compatible revisions.

The helper works on adapters that only implement the SMBus subset of I2C, which is why it is preferred over raw i2c_transfer() for simple register access. If the adapter cannot do the byte-data command at all, the call returns -EOPNOTSUPP, and i2c_check_functionality() lets you test for that at probe. Use regmap once a driver has more than a handful of registers. See the SMBus protocol notes.

Also inEmbedded
Share

More snippets

Get new posts by email

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

Unsubscribe in one click. No spam, ever.