5

I would like to know how the i2c bus number is assigned in Linux? In one of our legacy development board we have i2c slave device connected. When I probe the device using i2cdetect, it shows /dev/i2c1 and /dev/i2c3.

What happened to i2c0 and i2c1 in /dev? How are these numbers assigned? Can someone throw some insight on this?

Note: what I observe is everytime I boot up , I get to see only 1 and 3 and not 0 and 1. That doesn't change for all the boot up's. If I would like to do a symlink of these i2c1 and i2c3 to i20 and i2c1, is it possible?

2
  • 2
    If you are asking because you want to use a particular I2C bus: You can't rely on the numbers being consistent across boots. Use udev to make a symlink, if it doesn't already does.
    – dirkt
    Commented Nov 6, 2017 at 8:04
  • No, don't make symlinks that clash with existing kernel names, you are just asking for trouble. Look at how udev makes by-path, by-id etc. symlinks for input devices, partitions etc., and copy the style. For example, /dev/i2c/by-id/my-external-bus, or whatever. And just because so far the numbers have not changed, doesn't mean they won't change in the future ... maybe after the next kernel update, when you don't expect it (and have fun bughunting if it does).
    – dirkt
    Commented Nov 8, 2017 at 11:40

1 Answer 1

5

On my system the i2c busses are devices /dev/i2c-0 and so on. These are called adaptors, and are assigned dynamically as described in the kernel Documentation file i2c/dev-interface:

You should inspect /sys/class/i2c-dev/ or run "i2cdetect -l" to decide this. Adapter numbers are assigned somewhat dynamically, so you can not assume much about them. They can even change from one boot to the next.

sudo i2cdetect -l will list which bus does what, or you can look in /sys/class/i2c-dev/:

$ sudo i2cdetect -l|sort
i2c-0   i2c             i915 gmbus ssc                 I2C adapter
i2c-1   i2c             i915 gmbus vga                 I2C adapter
i2c-2   i2c             i915 gmbus panel               I2C adapter
...

$ head /sys/class/i2c-dev/i2c-*/name
==> /sys/class/i2c-dev/i2c-0/name <==
i915 gmbus ssc

==> /sys/class/i2c-dev/i2c-1/name <==
i915 gmbus vga
...

Note: Though often i2c bus numbers are assigned dynamically, on some platforms Linux will use information from a devicetree to assign specific numbers to an i2c adaptor. Presumably, some motherboards can similarly have platform drivers that request a particular bus number, to better represent the hardware layout. The kernel call is i2c_add_numbered_adapter().

1
  • Hey meuh, Yes I too had a look at this documentation before posting. But as I said, I was confused about the i2c0 and i2c2 which was missing. Thanks
    – AKR
    Commented Nov 8, 2017 at 6:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .