i2c-sysfs.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===============
  3. Linux I2C Sysfs
  4. ===============
  5. Overview
  6. ========
  7. I2C topology can be complex because of the existence of I2C MUX
  8. (I2C Multiplexer). The Linux
  9. kernel abstracts the MUX channels into logical I2C bus numbers. However, there
  10. is a gap of knowledge to map from the I2C bus physical number and MUX topology
  11. to logical I2C bus number. This doc is aimed to fill in this gap, so the
  12. audience (hardware engineers and new software developers for example) can learn
  13. the concept of logical I2C buses in the kernel, by knowing the physical I2C
  14. topology and navigating through the I2C sysfs in Linux shell. This knowledge is
  15. useful and essential to use ``i2c-tools`` for the purpose of development and
  16. debugging.
  17. Target audience
  18. ---------------
  19. People who need to use Linux shell to interact with I2C subsystem on a system
  20. which the Linux is running on.
  21. Prerequisites
  22. -------------
  23. 1. Knowledge of general Linux shell file system commands and operations.
  24. 2. General knowledge of I2C, I2C MUX and I2C topology.
  25. Location of I2C Sysfs
  26. =====================
  27. Typically, the Linux Sysfs filesystem is mounted at the ``/sys`` directory,
  28. so you can find the I2C Sysfs under ``/sys/bus/i2c/devices``
  29. where you can directly ``cd`` to it.
  30. There is a list of symbolic links under that directory. The links that
  31. start with ``i2c-`` are I2C buses, which may be either physical or logical. The
  32. other links that begin with numbers and end with numbers are I2C devices, where
  33. the first number is I2C bus number, and the second number is I2C address.
  34. Google Pixel 3 phone for example::
  35. blueline:/sys/bus/i2c/devices $ ls
  36. 0-0008 0-0061 1-0028 3-0043 4-0036 4-0041 i2c-1 i2c-3
  37. 0-000c 0-0066 2-0049 4-000b 4-0040 i2c-0 i2c-2 i2c-4
  38. ``i2c-2`` is an I2C bus whose number is 2, and ``2-0049`` is an I2C device
  39. on bus 2 address 0x49 bound with a kernel driver.
  40. Terminology
  41. ===========
  42. First, let us define some terms to avoid confusion in later sections.
  43. (Physical) I2C Bus Controller
  44. -----------------------------
  45. The hardware system that the Linux kernel is running on may have multiple
  46. physical I2C bus controllers. The controllers are hardware and physical, and the
  47. system may define multiple registers in the memory space to manipulate the
  48. controllers. Linux kernel has I2C bus drivers under source directory
  49. ``drivers/i2c/busses`` to translate kernel I2C API into register
  50. operations for different systems. This terminology is not limited to Linux
  51. kernel only.
  52. I2C Bus Physical Number
  53. -----------------------
  54. For each physical I2C bus controller, the system vendor may assign a physical
  55. number to each controller. For example, the first I2C bus controller which has
  56. the lowest register addresses may be called ``I2C-0``.
  57. Logical I2C Bus
  58. ---------------
  59. Every I2C bus number you see in Linux I2C Sysfs is a logical I2C bus with a
  60. number assigned. This is similar to the fact that software code is usually
  61. written upon virtual memory space, instead of physical memory space.
  62. Each logical I2C bus may be an abstraction of a physical I2C bus controller, or
  63. an abstraction of a channel behind an I2C MUX. In case it is an abstraction of a
  64. MUX channel, whenever we access an I2C device via a such logical bus, the kernel
  65. will switch the I2C MUX for you to the proper channel as part of the
  66. abstraction.
  67. Physical I2C Bus
  68. ----------------
  69. If the logical I2C bus is a direct abstraction of a physical I2C bus controller,
  70. let us call it a physical I2C bus.
  71. Caveat
  72. ------
  73. This may be a confusing part for people who only know about the physical I2C
  74. design of a board. It is actually possible to rename the I2C bus physical number
  75. to a different number in logical I2C bus level in Device Tree Source (DTS) under
  76. section ``aliases``. See ``arch/arm/boot/dts/nuvoton-npcm730-gsj.dts``
  77. for an example of DTS file.
  78. Best Practice: **(To kernel software developers)** It is better to keep the I2C
  79. bus physical number the same as their corresponding logical I2C bus number,
  80. instead of renaming or mapping them, so that it may be less confusing to other
  81. users. These physical I2C buses can be served as good starting points for I2C
  82. MUX fanouts. For the following examples, we will assume that the physical I2C
  83. bus has a number same as their I2C bus physical number.
  84. Walk through Logical I2C Bus
  85. ============================
  86. For the following content, we will use a more complex I2C topology as an
  87. example. Here is a brief graph for the I2C topology. If you do not understand
  88. this graph at first glance, do not be afraid to continue reading this doc
  89. and review it when you finish reading.
  90. ::
  91. i2c-7 (physical I2C bus controller 7)
  92. `-- 7-0071 (4-channel I2C MUX at 0x71)
  93. |-- i2c-60 (channel-0)
  94. |-- i2c-73 (channel-1)
  95. | |-- 73-0040 (I2C sensor device with hwmon directory)
  96. | |-- 73-0070 (I2C MUX at 0x70, exists in DTS, but failed to probe)
  97. | `-- 73-0072 (8-channel I2C MUX at 0x72)
  98. | |-- i2c-78 (channel-0)
  99. | |-- ... (channel-1...6, i2c-79...i2c-84)
  100. | `-- i2c-85 (channel-7)
  101. |-- i2c-86 (channel-2)
  102. `-- i2c-203 (channel-3)
  103. Distinguish Physical and Logical I2C Bus
  104. ----------------------------------------
  105. One simple way to distinguish between a physical I2C bus and a logical I2C bus,
  106. is to read the symbolic link ``device`` under the I2C bus directory by using
  107. command ``ls -l`` or ``readlink``.
  108. An alternative symbolic link to check is ``mux_device``. This link only exists
  109. in logical I2C bus directory which is fanned out from another I2C bus.
  110. Reading this link will also tell you which I2C MUX device created
  111. this logical I2C bus.
  112. If the symbolic link points to a directory ending with ``.i2c``, it should be a
  113. physical I2C bus, directly abstracting a physical I2C bus controller. For
  114. example::
  115. $ readlink /sys/bus/i2c/devices/i2c-7/device
  116. ../../f0087000.i2c
  117. $ ls /sys/bus/i2c/devices/i2c-7/mux_device
  118. ls: /sys/bus/i2c/devices/i2c-7/mux_device: No such file or directory
  119. In this case, ``i2c-7`` is a physical I2C bus, so it does not have the symbolic
  120. link ``mux_device`` under its directory. And if the kernel software developer
  121. follows the common practice by not renaming physical I2C buses, this should also
  122. mean the physical I2C bus controller 7 of the system.
  123. On the other hand, if the symbolic link points to another I2C bus, the I2C bus
  124. presented by the current directory has to be a logical bus. The I2C bus pointed
  125. by the link is the parent bus which may be either a physical I2C bus or a
  126. logical one. In this case, the I2C bus presented by the current directory
  127. abstracts an I2C MUX channel under the parent bus.
  128. For example::
  129. $ readlink /sys/bus/i2c/devices/i2c-73/device
  130. ../../i2c-7
  131. $ readlink /sys/bus/i2c/devices/i2c-73/mux_device
  132. ../7-0071
  133. ``i2c-73`` is a logical bus fanout by an I2C MUX under ``i2c-7``
  134. whose I2C address is 0x71.
  135. Whenever we access an I2C device with bus 73, the kernel will always
  136. switch the I2C MUX addressed 0x71 to the proper channel for you as part of the
  137. abstraction.
  138. Finding out Logical I2C Bus Number
  139. ----------------------------------
  140. In this section, we will describe how to find out the logical I2C bus number
  141. representing certain I2C MUX channels based on the knowledge of physical
  142. hardware I2C topology.
  143. In this example, we have a system which has a physical I2C bus 7 and not renamed
  144. in DTS. There is a 4-channel MUX at address 0x71 on that bus. There is another
  145. 8-channel MUX at address 0x72 behind the channel 1 of the 0x71 MUX. Let us
  146. navigate through Sysfs and find out the logical I2C bus number of the channel 3
  147. of the 0x72 MUX.
  148. First of all, let us go to the directory of ``i2c-7``::
  149. ~$ cd /sys/bus/i2c/devices/i2c-7
  150. /sys/bus/i2c/devices/i2c-7$ ls
  151. 7-0071 i2c-60 name subsystem
  152. delete_device i2c-73 new_device uevent
  153. device i2c-86 of_node
  154. i2c-203 i2c-dev power
  155. There, we see the 0x71 MUX as ``7-0071``. Go inside it::
  156. /sys/bus/i2c/devices/i2c-7$ cd 7-0071/
  157. /sys/bus/i2c/devices/i2c-7/7-0071$ ls -l
  158. channel-0 channel-3 modalias power
  159. channel-1 driver name subsystem
  160. channel-2 idle_state of_node uevent
  161. Read the link ``channel-1`` using ``readlink`` or ``ls -l``::
  162. /sys/bus/i2c/devices/i2c-7/7-0071$ readlink channel-1
  163. ../i2c-73
  164. We find out that the channel 1 of 0x71 MUX on ``i2c-7`` is assigned
  165. with a logical I2C bus number of 73.
  166. Let us continue the journey to directory ``i2c-73`` in either ways::
  167. # cd to i2c-73 under I2C Sysfs root
  168. /sys/bus/i2c/devices/i2c-7/7-0071$ cd /sys/bus/i2c/devices/i2c-73
  169. /sys/bus/i2c/devices/i2c-73$
  170. # cd the channel symbolic link
  171. /sys/bus/i2c/devices/i2c-7/7-0071$ cd channel-1
  172. /sys/bus/i2c/devices/i2c-7/7-0071/channel-1$
  173. # cd the link content
  174. /sys/bus/i2c/devices/i2c-7/7-0071$ cd ../i2c-73
  175. /sys/bus/i2c/devices/i2c-7/i2c-73$
  176. Either ways, you will end up in the directory of ``i2c-73``. Similar to above,
  177. we can now find the 0x72 MUX and what logical I2C bus numbers
  178. that its channels are assigned::
  179. /sys/bus/i2c/devices/i2c-73$ ls
  180. 73-0040 device i2c-83 new_device
  181. 73-004e i2c-78 i2c-84 of_node
  182. 73-0050 i2c-79 i2c-85 power
  183. 73-0070 i2c-80 i2c-dev subsystem
  184. 73-0072 i2c-81 mux_device uevent
  185. delete_device i2c-82 name
  186. /sys/bus/i2c/devices/i2c-73$ cd 73-0072
  187. /sys/bus/i2c/devices/i2c-73/73-0072$ ls
  188. channel-0 channel-4 driver of_node
  189. channel-1 channel-5 idle_state power
  190. channel-2 channel-6 modalias subsystem
  191. channel-3 channel-7 name uevent
  192. /sys/bus/i2c/devices/i2c-73/73-0072$ readlink channel-3
  193. ../i2c-81
  194. There, we find out the logical I2C bus number of the channel 3 of the 0x72 MUX
  195. is 81. We can later use this number to switch to its own I2C Sysfs directory or
  196. issue ``i2c-tools`` commands.
  197. Tip: Once you understand the I2C topology with MUX, command
  198. `i2cdetect -l
  199. <https://manpages.debian.org/unstable/i2c-tools/i2cdetect.8.en.html>`_
  200. in
  201. `I2C Tools
  202. <https://i2c.wiki.kernel.org/index.php/I2C_Tools>`_
  203. can give you
  204. an overview of the I2C topology easily, if it is available on your system. For
  205. example::
  206. $ i2cdetect -l | grep -e '\-73' -e _7 | sort -V
  207. i2c-7 i2c npcm_i2c_7 I2C adapter
  208. i2c-73 i2c i2c-7-mux (chan_id 1) I2C adapter
  209. i2c-78 i2c i2c-73-mux (chan_id 0) I2C adapter
  210. i2c-79 i2c i2c-73-mux (chan_id 1) I2C adapter
  211. i2c-80 i2c i2c-73-mux (chan_id 2) I2C adapter
  212. i2c-81 i2c i2c-73-mux (chan_id 3) I2C adapter
  213. i2c-82 i2c i2c-73-mux (chan_id 4) I2C adapter
  214. i2c-83 i2c i2c-73-mux (chan_id 5) I2C adapter
  215. i2c-84 i2c i2c-73-mux (chan_id 6) I2C adapter
  216. i2c-85 i2c i2c-73-mux (chan_id 7) I2C adapter
  217. Pinned Logical I2C Bus Number
  218. -----------------------------
  219. If not specified in DTS, when an I2C MUX driver is applied and the MUX device is
  220. successfully probed, the kernel will assign the MUX channels with a logical bus
  221. number based on the current biggest logical bus number incrementally. For
  222. example, if the system has ``i2c-15`` as the highest logical bus number, and a
  223. 4-channel MUX is applied successfully, we will have ``i2c-16`` for the
  224. MUX channel 0, and all the way to ``i2c-19`` for the MUX channel 3.
  225. The kernel software developer is able to pin the fanout MUX channels to a static
  226. logical I2C bus number in the DTS. This doc will not go through the details on
  227. how to implement this in DTS, but we can see an example in:
  228. ``arch/arm/boot/dts/aspeed-bmc-facebook-wedge400.dts``
  229. In the above example, there is an 8-channel I2C MUX at address 0x70 on physical
  230. I2C bus 2. The channel 2 of the MUX is defined as ``imux18`` in DTS,
  231. and pinned to logical I2C bus number 18 with the line of ``i2c18 = &imux18;``
  232. in section ``aliases``.
  233. Take it further, it is possible to design a logical I2C bus number schema that
  234. can be easily remembered by humans or calculated arithmetically. For example, we
  235. can pin the fanout channels of a MUX on bus 3 to start at 30. So 30 will be the
  236. logical bus number of the channel 0 of the MUX on bus 3, and 37 will be the
  237. logical bus number of the channel 7 of the MUX on bus 3.
  238. I2C Devices
  239. ===========
  240. In previous sections, we mostly covered the I2C bus. In this section, let us see
  241. what we can learn from the I2C device directory whose link name is in the format
  242. of ``${bus}-${addr}``. The ``${bus}`` part in the name is a logical I2C bus
  243. decimal number, while the ``${addr}`` part is a hex number of the I2C address
  244. of each device.
  245. I2C Device Directory Content
  246. ----------------------------
  247. Inside each I2C device directory, there is a file named ``name``.
  248. This file tells what device name it was used for the kernel driver to
  249. probe this device. Use command ``cat`` to read its content. For example::
  250. /sys/bus/i2c/devices/i2c-73$ cat 73-0040/name
  251. ina230
  252. /sys/bus/i2c/devices/i2c-73$ cat 73-0070/name
  253. pca9546
  254. /sys/bus/i2c/devices/i2c-73$ cat 73-0072/name
  255. pca9547
  256. There is a symbolic link named ``driver`` to tell what Linux kernel driver was
  257. used to probe this device::
  258. /sys/bus/i2c/devices/i2c-73$ readlink -f 73-0040/driver
  259. /sys/bus/i2c/drivers/ina2xx
  260. /sys/bus/i2c/devices/i2c-73$ readlink -f 73-0072/driver
  261. /sys/bus/i2c/drivers/pca954x
  262. But if the link ``driver`` does not exist at the first place,
  263. it may mean that the kernel driver failed to probe this device due to
  264. some errors. The error may be found in ``dmesg``::
  265. /sys/bus/i2c/devices/i2c-73$ ls 73-0070/driver
  266. ls: 73-0070/driver: No such file or directory
  267. /sys/bus/i2c/devices/i2c-73$ dmesg | grep 73-0070
  268. pca954x 73-0070: probe failed
  269. pca954x 73-0070: probe failed
  270. Depending on what the I2C device is and what kernel driver was used to probe the
  271. device, we may have different content in the device directory.
  272. I2C MUX Device
  273. --------------
  274. While you may be already aware of this in previous sections, an I2C MUX device
  275. will have symbolic link ``channel-*`` inside its device directory.
  276. These symbolic links point to their logical I2C bus directories::
  277. /sys/bus/i2c/devices/i2c-73$ ls -l 73-0072/channel-*
  278. lrwxrwxrwx ... 73-0072/channel-0 -> ../i2c-78
  279. lrwxrwxrwx ... 73-0072/channel-1 -> ../i2c-79
  280. lrwxrwxrwx ... 73-0072/channel-2 -> ../i2c-80
  281. lrwxrwxrwx ... 73-0072/channel-3 -> ../i2c-81
  282. lrwxrwxrwx ... 73-0072/channel-4 -> ../i2c-82
  283. lrwxrwxrwx ... 73-0072/channel-5 -> ../i2c-83
  284. lrwxrwxrwx ... 73-0072/channel-6 -> ../i2c-84
  285. lrwxrwxrwx ... 73-0072/channel-7 -> ../i2c-85
  286. I2C Sensor Device / Hwmon
  287. -------------------------
  288. I2C sensor device is also common to see. If they are bound by a kernel hwmon
  289. (Hardware Monitoring) driver successfully, you will see a ``hwmon`` directory
  290. inside the I2C device directory. Keep digging into it, you will find the Hwmon
  291. Sysfs for the I2C sensor device::
  292. /sys/bus/i2c/devices/i2c-73/73-0040/hwmon/hwmon17$ ls
  293. curr1_input in0_lcrit_alarm name subsystem
  294. device in1_crit power uevent
  295. in0_crit in1_crit_alarm power1_crit update_interval
  296. in0_crit_alarm in1_input power1_crit_alarm
  297. in0_input in1_lcrit power1_input
  298. in0_lcrit in1_lcrit_alarm shunt_resistor
  299. For more info on the Hwmon Sysfs, refer to the doc:
  300. ../hwmon/sysfs-interface.rst
  301. Instantiate I2C Devices in I2C Sysfs
  302. ------------------------------------
  303. Refer to section "Method 4: Instantiate from user-space" of instantiating-devices.rst