Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 
0003 ===============
0004 Linux I2C Sysfs
0005 ===============
0006 
0007 Overview
0008 ========
0009 
0010 I2C topology can be complex because of the existence of I2C MUX
0011 (I2C Multiplexer). The Linux
0012 kernel abstracts the MUX channels into logical I2C bus numbers. However, there
0013 is a gap of knowledge to map from the I2C bus physical number and MUX topology
0014 to logical I2C bus number. This doc is aimed to fill in this gap, so the
0015 audience (hardware engineers and new software developers for example) can learn
0016 the concept of logical I2C buses in the kernel, by knowing the physical I2C
0017 topology and navigating through the I2C sysfs in Linux shell. This knowledge is
0018 useful and essential to use ``i2c-tools`` for the purpose of development and
0019 debugging.
0020 
0021 Target audience
0022 ---------------
0023 
0024 People who need to use Linux shell to interact with I2C subsystem on a system
0025 which the Linux is running on.
0026 
0027 Prerequisites
0028 -------------
0029 
0030 1.  Knowledge of general Linux shell file system commands and operations.
0031 
0032 2.  General knowledge of I2C, I2C MUX and I2C topology.
0033 
0034 Location of I2C Sysfs
0035 =====================
0036 
0037 Typically, the Linux Sysfs filesystem is mounted at the ``/sys`` directory,
0038 so you can find the I2C Sysfs under ``/sys/bus/i2c/devices``
0039 where you can directly ``cd`` to it.
0040 There is a list of symbolic links under that directory. The links that
0041 start with ``i2c-`` are I2C buses, which may be either physical or logical. The
0042 other links that begin with numbers and end with numbers are I2C devices, where
0043 the first number is I2C bus number, and the second number is I2C address.
0044 
0045 Google Pixel 3 phone for example::
0046 
0047   blueline:/sys/bus/i2c/devices $ ls
0048   0-0008  0-0061  1-0028  3-0043  4-0036  4-0041  i2c-1  i2c-3
0049   0-000c  0-0066  2-0049  4-000b  4-0040  i2c-0   i2c-2  i2c-4
0050 
0051 ``i2c-2`` is an I2C bus whose number is 2, and ``2-0049`` is an I2C device
0052 on bus 2 address 0x49 bound with a kernel driver.
0053 
0054 Terminology
0055 ===========
0056 
0057 First, let us define some terms to avoid confusion in later sections.
0058 
0059 (Physical) I2C Bus Controller
0060 -----------------------------
0061 
0062 The hardware system that the Linux kernel is running on may have multiple
0063 physical I2C bus controllers. The controllers are hardware and physical, and the
0064 system may define multiple registers in the memory space to manipulate the
0065 controllers. Linux kernel has I2C bus drivers under source directory
0066 ``drivers/i2c/busses`` to translate kernel I2C API into register
0067 operations for different systems. This terminology is not limited to Linux
0068 kernel only.
0069 
0070 I2C Bus Physical Number
0071 -----------------------
0072 
0073 For each physical I2C bus controller, the system vendor may assign a physical
0074 number to each controller. For example, the first I2C bus controller which has
0075 the lowest register addresses may be called ``I2C-0``.
0076 
0077 Logical I2C Bus
0078 ---------------
0079 
0080 Every I2C bus number you see in Linux I2C Sysfs is a logical I2C bus with a
0081 number assigned. This is similar to the fact that software code is usually
0082 written upon virtual memory space, instead of physical memory space.
0083 
0084 Each logical I2C bus may be an abstraction of a physical I2C bus controller, or
0085 an abstraction of a channel behind an I2C MUX. In case it is an abstraction of a
0086 MUX channel, whenever we access an I2C device via a such logical bus, the kernel
0087 will switch the I2C MUX for you to the proper channel as part of the
0088 abstraction.
0089 
0090 Physical I2C Bus
0091 ----------------
0092 
0093 If the logical I2C bus is a direct abstraction of a physical I2C bus controller,
0094 let us call it a physical I2C bus.
0095 
0096 Caveat
0097 ------
0098 
0099 This may be a confusing part for people who only know about the physical I2C
0100 design of a board. It is actually possible to rename the I2C bus physical number
0101 to a different number in logical I2C bus level in Device Tree Source (DTS) under
0102 section ``aliases``. See ``arch/arm/boot/dts/nuvoton-npcm730-gsj.dts``
0103 for an example of DTS file.
0104 
0105 Best Practice: **(To kernel software developers)** It is better to keep the I2C
0106 bus physical number the same as their corresponding logical I2C bus number,
0107 instead of renaming or mapping them, so that it may be less confusing to other
0108 users. These physical I2C buses can be served as good starting points for I2C
0109 MUX fanouts. For the following examples, we will assume that the physical I2C
0110 bus has a number same as their I2C bus physical number.
0111 
0112 Walk through Logical I2C Bus
0113 ============================
0114 
0115 For the following content, we will use a more complex I2C topology as an
0116 example. Here is a brief graph for the I2C topology. If you do not understand
0117 this graph at first glance, do not be afraid to continue reading this doc
0118 and review it when you finish reading.
0119 
0120 ::
0121 
0122   i2c-7 (physical I2C bus controller 7)
0123   `-- 7-0071 (4-channel I2C MUX at 0x71)
0124       |-- i2c-60 (channel-0)
0125       |-- i2c-73 (channel-1)
0126       |   |-- 73-0040 (I2C sensor device with hwmon directory)
0127       |   |-- 73-0070 (I2C MUX at 0x70, exists in DTS, but failed to probe)
0128       |   `-- 73-0072 (8-channel I2C MUX at 0x72)
0129       |       |-- i2c-78 (channel-0)
0130       |       |-- ... (channel-1...6, i2c-79...i2c-84)
0131       |       `-- i2c-85 (channel-7)
0132       |-- i2c-86 (channel-2)
0133       `-- i2c-203 (channel-3)
0134 
0135 Distinguish Physical and Logical I2C Bus
0136 ----------------------------------------
0137 
0138 One simple way to distinguish between a physical I2C bus and a logical I2C bus,
0139 is to read the symbolic link ``device`` under the I2C bus directory by using
0140 command ``ls -l`` or ``readlink``.
0141 
0142 An alternative symbolic link to check is ``mux_device``. This link only exists
0143 in logical I2C bus directory which is fanned out from another I2C bus.
0144 Reading this link will also tell you which I2C MUX device created
0145 this logical I2C bus.
0146 
0147 If the symbolic link points to a directory ending with ``.i2c``, it should be a
0148 physical I2C bus, directly abstracting a physical I2C bus controller. For
0149 example::
0150 
0151   $ readlink /sys/bus/i2c/devices/i2c-7/device
0152   ../../f0087000.i2c
0153   $ ls /sys/bus/i2c/devices/i2c-7/mux_device
0154   ls: /sys/bus/i2c/devices/i2c-7/mux_device: No such file or directory
0155 
0156 In this case, ``i2c-7`` is a physical I2C bus, so it does not have the symbolic
0157 link ``mux_device`` under its directory. And if the kernel software developer
0158 follows the common practice by not renaming physical I2C buses, this should also
0159 mean the physical I2C bus controller 7 of the system.
0160 
0161 On the other hand, if the symbolic link points to another I2C bus, the I2C bus
0162 presented by the current directory has to be a logical bus. The I2C bus pointed
0163 by the link is the parent bus which may be either a physical I2C bus or a
0164 logical one. In this case, the I2C bus presented by the current directory
0165 abstracts an I2C MUX channel under the parent bus.
0166 
0167 For example::
0168 
0169   $ readlink /sys/bus/i2c/devices/i2c-73/device
0170   ../../i2c-7
0171   $ readlink /sys/bus/i2c/devices/i2c-73/mux_device
0172   ../7-0071
0173 
0174 ``i2c-73`` is a logical bus fanout by an I2C MUX under ``i2c-7``
0175 whose I2C address is 0x71.
0176 Whenever we access an I2C device with bus 73, the kernel will always
0177 switch the I2C MUX addressed 0x71 to the proper channel for you as part of the
0178 abstraction.
0179 
0180 Finding out Logical I2C Bus Number
0181 ----------------------------------
0182 
0183 In this section, we will describe how to find out the logical I2C bus number
0184 representing certain I2C MUX channels based on the knowledge of physical
0185 hardware I2C topology.
0186 
0187 In this example, we have a system which has a physical I2C bus 7 and not renamed
0188 in DTS. There is a 4-channel MUX at address 0x71 on that bus. There is another
0189 8-channel MUX at address 0x72 behind the channel 1 of the 0x71 MUX. Let us
0190 navigate through Sysfs and find out the logical I2C bus number of the channel 3
0191 of the 0x72 MUX.
0192 
0193 First of all, let us go to the directory of ``i2c-7``::
0194 
0195   ~$ cd /sys/bus/i2c/devices/i2c-7
0196   /sys/bus/i2c/devices/i2c-7$ ls
0197   7-0071         i2c-60         name           subsystem
0198   delete_device  i2c-73         new_device     uevent
0199   device         i2c-86         of_node
0200   i2c-203        i2c-dev        power
0201 
0202 There, we see the 0x71 MUX as ``7-0071``. Go inside it::
0203 
0204   /sys/bus/i2c/devices/i2c-7$ cd 7-0071/
0205   /sys/bus/i2c/devices/i2c-7/7-0071$ ls -l
0206   channel-0   channel-3   modalias    power
0207   channel-1   driver      name        subsystem
0208   channel-2   idle_state  of_node     uevent
0209 
0210 Read the link ``channel-1`` using ``readlink`` or ``ls -l``::
0211 
0212   /sys/bus/i2c/devices/i2c-7/7-0071$ readlink channel-1
0213   ../i2c-73
0214 
0215 We find out that the channel 1 of 0x71 MUX on ``i2c-7`` is assigned
0216 with a logical I2C bus number of 73.
0217 Let us continue the journey to directory ``i2c-73`` in either ways::
0218 
0219   # cd to i2c-73 under I2C Sysfs root
0220   /sys/bus/i2c/devices/i2c-7/7-0071$ cd /sys/bus/i2c/devices/i2c-73
0221   /sys/bus/i2c/devices/i2c-73$
0222 
0223   # cd the channel symbolic link
0224   /sys/bus/i2c/devices/i2c-7/7-0071$ cd channel-1
0225   /sys/bus/i2c/devices/i2c-7/7-0071/channel-1$
0226 
0227   # cd the link content
0228   /sys/bus/i2c/devices/i2c-7/7-0071$ cd ../i2c-73
0229   /sys/bus/i2c/devices/i2c-7/i2c-73$
0230 
0231 Either ways, you will end up in the directory of ``i2c-73``. Similar to above,
0232 we can now find the 0x72 MUX and what logical I2C bus numbers
0233 that its channels are assigned::
0234 
0235   /sys/bus/i2c/devices/i2c-73$ ls
0236   73-0040        device         i2c-83         new_device
0237   73-004e        i2c-78         i2c-84         of_node
0238   73-0050        i2c-79         i2c-85         power
0239   73-0070        i2c-80         i2c-dev        subsystem
0240   73-0072        i2c-81         mux_device     uevent
0241   delete_device  i2c-82         name
0242   /sys/bus/i2c/devices/i2c-73$ cd 73-0072
0243   /sys/bus/i2c/devices/i2c-73/73-0072$ ls
0244   channel-0   channel-4   driver      of_node
0245   channel-1   channel-5   idle_state  power
0246   channel-2   channel-6   modalias    subsystem
0247   channel-3   channel-7   name        uevent
0248   /sys/bus/i2c/devices/i2c-73/73-0072$ readlink channel-3
0249   ../i2c-81
0250 
0251 There, we find out the logical I2C bus number of the channel 3 of the 0x72 MUX
0252 is 81. We can later use this number to switch to its own I2C Sysfs directory or
0253 issue ``i2c-tools`` commands.
0254 
0255 Tip: Once you understand the I2C topology with MUX, command
0256 `i2cdetect -l
0257 <https://manpages.debian.org/unstable/i2c-tools/i2cdetect.8.en.html>`_
0258 in
0259 `I2C Tools
0260 <https://i2c.wiki.kernel.org/index.php/I2C_Tools>`_
0261 can give you
0262 an overview of the I2C topology easily, if it is available on your system. For
0263 example::
0264 
0265   $ i2cdetect -l | grep -e '\-73' -e _7 | sort -V
0266   i2c-7   i2c             npcm_i2c_7                              I2C adapter
0267   i2c-73  i2c             i2c-7-mux (chan_id 1)                   I2C adapter
0268   i2c-78  i2c             i2c-73-mux (chan_id 0)                  I2C adapter
0269   i2c-79  i2c             i2c-73-mux (chan_id 1)                  I2C adapter
0270   i2c-80  i2c             i2c-73-mux (chan_id 2)                  I2C adapter
0271   i2c-81  i2c             i2c-73-mux (chan_id 3)                  I2C adapter
0272   i2c-82  i2c             i2c-73-mux (chan_id 4)                  I2C adapter
0273   i2c-83  i2c             i2c-73-mux (chan_id 5)                  I2C adapter
0274   i2c-84  i2c             i2c-73-mux (chan_id 6)                  I2C adapter
0275   i2c-85  i2c             i2c-73-mux (chan_id 7)                  I2C adapter
0276 
0277 Pinned Logical I2C Bus Number
0278 -----------------------------
0279 
0280 If not specified in DTS, when an I2C MUX driver is applied and the MUX device is
0281 successfully probed, the kernel will assign the MUX channels with a logical bus
0282 number based on the current biggest logical bus number incrementally. For
0283 example, if the system has ``i2c-15`` as the highest logical bus number, and a
0284 4-channel MUX is applied successfully, we will have ``i2c-16`` for the
0285 MUX channel 0, and all the way to ``i2c-19`` for the MUX channel 3.
0286 
0287 The kernel software developer is able to pin the fanout MUX channels to a static
0288 logical I2C bus number in the DTS. This doc will not go through the details on
0289 how to implement this in DTS, but we can see an example in:
0290 ``arch/arm/boot/dts/aspeed-bmc-facebook-wedge400.dts``
0291 
0292 In the above example, there is an 8-channel I2C MUX at address 0x70 on physical
0293 I2C bus 2. The channel 2 of the MUX is defined as ``imux18`` in DTS,
0294 and pinned to logical I2C bus number 18 with the line of ``i2c18 = &imux18;``
0295 in section ``aliases``.
0296 
0297 Take it further, it is possible to design a logical I2C bus number schema that
0298 can be easily remembered by humans or calculated arithmetically. For example, we
0299 can pin the fanout channels of a MUX on bus 3 to start at 30. So 30 will be the
0300 logical bus number of the channel 0 of the MUX on bus 3, and 37 will be the
0301 logical bus number of the channel 7 of the MUX on bus 3.
0302 
0303 I2C Devices
0304 ===========
0305 
0306 In previous sections, we mostly covered the I2C bus. In this section, let us see
0307 what we can learn from the I2C device directory whose link name is in the format
0308 of ``${bus}-${addr}``. The ``${bus}`` part in the name is a logical I2C bus
0309 decimal number, while the ``${addr}`` part is a hex number of the I2C address
0310 of each device.
0311 
0312 I2C Device Directory Content
0313 ----------------------------
0314 
0315 Inside each I2C device directory, there is a file named ``name``.
0316 This file tells what device name it was used for the kernel driver to
0317 probe this device. Use command ``cat`` to read its content. For example::
0318 
0319   /sys/bus/i2c/devices/i2c-73$ cat 73-0040/name
0320   ina230
0321   /sys/bus/i2c/devices/i2c-73$ cat 73-0070/name
0322   pca9546
0323   /sys/bus/i2c/devices/i2c-73$ cat 73-0072/name
0324   pca9547
0325 
0326 There is a symbolic link named ``driver`` to tell what Linux kernel driver was
0327 used to probe this device::
0328 
0329   /sys/bus/i2c/devices/i2c-73$ readlink -f 73-0040/driver
0330   /sys/bus/i2c/drivers/ina2xx
0331   /sys/bus/i2c/devices/i2c-73$ readlink -f 73-0072/driver
0332   /sys/bus/i2c/drivers/pca954x
0333 
0334 But if the link ``driver`` does not exist at the first place,
0335 it may mean that the kernel driver failed to probe this device due to
0336 some errors. The error may be found in ``dmesg``::
0337 
0338   /sys/bus/i2c/devices/i2c-73$ ls 73-0070/driver
0339   ls: 73-0070/driver: No such file or directory
0340   /sys/bus/i2c/devices/i2c-73$ dmesg | grep 73-0070
0341   pca954x 73-0070: probe failed
0342   pca954x 73-0070: probe failed
0343 
0344 Depending on what the I2C device is and what kernel driver was used to probe the
0345 device, we may have different content in the device directory.
0346 
0347 I2C MUX Device
0348 --------------
0349 
0350 While you may be already aware of this in previous sections, an I2C MUX device
0351 will have symbolic link ``channel-*`` inside its device directory.
0352 These symbolic links point to their logical I2C bus directories::
0353 
0354   /sys/bus/i2c/devices/i2c-73$ ls -l 73-0072/channel-*
0355   lrwxrwxrwx ... 73-0072/channel-0 -> ../i2c-78
0356   lrwxrwxrwx ... 73-0072/channel-1 -> ../i2c-79
0357   lrwxrwxrwx ... 73-0072/channel-2 -> ../i2c-80
0358   lrwxrwxrwx ... 73-0072/channel-3 -> ../i2c-81
0359   lrwxrwxrwx ... 73-0072/channel-4 -> ../i2c-82
0360   lrwxrwxrwx ... 73-0072/channel-5 -> ../i2c-83
0361   lrwxrwxrwx ... 73-0072/channel-6 -> ../i2c-84
0362   lrwxrwxrwx ... 73-0072/channel-7 -> ../i2c-85
0363 
0364 I2C Sensor Device / Hwmon
0365 -------------------------
0366 
0367 I2C sensor device is also common to see. If they are bound by a kernel hwmon
0368 (Hardware Monitoring) driver successfully, you will see a ``hwmon`` directory
0369 inside the I2C device directory. Keep digging into it, you will find the Hwmon
0370 Sysfs for the I2C sensor device::
0371 
0372   /sys/bus/i2c/devices/i2c-73/73-0040/hwmon/hwmon17$ ls
0373   curr1_input        in0_lcrit_alarm    name               subsystem
0374   device             in1_crit           power              uevent
0375   in0_crit           in1_crit_alarm     power1_crit        update_interval
0376   in0_crit_alarm     in1_input          power1_crit_alarm
0377   in0_input          in1_lcrit          power1_input
0378   in0_lcrit          in1_lcrit_alarm    shunt_resistor
0379 
0380 For more info on the Hwmon Sysfs, refer to the doc:
0381 
0382 ../hwmon/sysfs-interface.rst
0383 
0384 Instantiate I2C Devices in I2C Sysfs
0385 ------------------------------------
0386 
0387 Refer to section "Method 4: Instantiate from user-space" of instantiating-devices.rst