i2c-topology.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. ================================
  2. I2C muxes and complex topologies
  3. ================================
  4. There are a couple of reasons for building more complex I2C topologies
  5. than a straight-forward I2C bus with one adapter and one or more devices.
  6. Some example use cases are:
  7. 1. A mux may be needed on the bus to prevent address collisions.
  8. 2. The bus may be accessible from some external bus master, and arbitration
  9. may be needed to determine if it is ok to access the bus.
  10. 3. A device (particularly RF tuners) may want to avoid the digital noise
  11. from the I2C bus, at least most of the time, and sits behind a gate
  12. that has to be operated before the device can be accessed.
  13. Several types of hardware components such as I2C muxes, I2C gates and I2C
  14. arbitrators allow to handle such needs.
  15. These components are represented as I2C adapter trees by Linux, where
  16. each adapter has a parent adapter (except the root adapter) and zero or
  17. more child adapters. The root adapter is the actual adapter that issues
  18. I2C transfers, and all adapters with a parent are part of an "i2c-mux"
  19. object (quoted, since it can also be an arbitrator or a gate).
  20. Depending of the particular mux driver, something happens when there is
  21. an I2C transfer on one of its child adapters. The mux driver can
  22. obviously operate a mux, but it can also do arbitration with an external
  23. bus master or open a gate. The mux driver has two operations for this,
  24. select and deselect. select is called before the transfer and (the
  25. optional) deselect is called after the transfer.
  26. Locking
  27. =======
  28. There are two variants of locking available to I2C muxes, they can be
  29. mux-locked or parent-locked muxes.
  30. Mux-locked muxes
  31. ----------------
  32. Mux-locked muxes does not lock the entire parent adapter during the
  33. full select-transfer-deselect transaction, only the muxes on the parent
  34. adapter are locked. Mux-locked muxes are mostly interesting if the
  35. select and/or deselect operations must use I2C transfers to complete
  36. their tasks. Since the parent adapter is not fully locked during the
  37. full transaction, unrelated I2C transfers may interleave the different
  38. stages of the transaction. This has the benefit that the mux driver
  39. may be easier and cleaner to implement, but it has some caveats.
  40. Mux-locked Example
  41. ~~~~~~~~~~~~~~~~~~
  42. ::
  43. .----------. .--------.
  44. .--------. | mux- |-----| dev D1 |
  45. | root |--+--| locked | '--------'
  46. '--------' | | mux M1 |--. .--------.
  47. | '----------' '--| dev D2 |
  48. | .--------. '--------'
  49. '--| dev D3 |
  50. '--------'
  51. When there is an access to D1, this happens:
  52. 1. Someone issues an I2C transfer to D1.
  53. 2. M1 locks muxes on its parent (the root adapter in this case).
  54. 3. M1 calls ->select to ready the mux.
  55. 4. M1 (presumably) does some I2C transfers as part of its select.
  56. These transfers are normal I2C transfers that locks the parent
  57. adapter.
  58. 5. M1 feeds the I2C transfer from step 1 to its parent adapter as a
  59. normal I2C transfer that locks the parent adapter.
  60. 6. M1 calls ->deselect, if it has one.
  61. 7. Same rules as in step 4, but for ->deselect.
  62. 8. M1 unlocks muxes on its parent.
  63. This means that accesses to D2 are lockout out for the full duration
  64. of the entire operation. But accesses to D3 are possibly interleaved
  65. at any point.
  66. Mux-locked caveats
  67. ~~~~~~~~~~~~~~~~~~
  68. When using a mux-locked mux, be aware of the following restrictions:
  69. [ML1]
  70. If you build a topology with a mux-locked mux being the parent
  71. of a parent-locked mux, this might break the expectation from the
  72. parent-locked mux that the root adapter is locked during the
  73. transaction.
  74. [ML2]
  75. It is not safe to build arbitrary topologies with two (or more)
  76. mux-locked muxes that are not siblings, when there are address
  77. collisions between the devices on the child adapters of these
  78. non-sibling muxes.
  79. I.e. the select-transfer-deselect transaction targeting e.g. device
  80. address 0x42 behind mux-one may be interleaved with a similar
  81. operation targeting device address 0x42 behind mux-two. The
  82. intent with such a topology would in this hypothetical example
  83. be that mux-one and mux-two should not be selected simultaneously,
  84. but mux-locked muxes do not guarantee that in all topologies.
  85. [ML3]
  86. A mux-locked mux cannot be used by a driver for auto-closing
  87. gates/muxes, i.e. something that closes automatically after a given
  88. number (one, in most cases) of I2C transfers. Unrelated I2C transfers
  89. may creep in and close prematurely.
  90. [ML4]
  91. If any non-I2C operation in the mux driver changes the I2C mux state,
  92. the driver has to lock the root adapter during that operation.
  93. Otherwise garbage may appear on the bus as seen from devices
  94. behind the mux, when an unrelated I2C transfer is in flight during
  95. the non-I2C mux-changing operation.
  96. Parent-locked muxes
  97. -------------------
  98. Parent-locked muxes lock the parent adapter during the full select-
  99. transfer-deselect transaction. The implication is that the mux driver
  100. has to ensure that any and all I2C transfers through that parent
  101. adapter during the transaction are unlocked I2C transfers (using e.g.
  102. __i2c_transfer), or a deadlock will follow.
  103. Parent-locked Example
  104. ~~~~~~~~~~~~~~~~~~~~~
  105. ::
  106. .----------. .--------.
  107. .--------. | parent- |-----| dev D1 |
  108. | root |--+--| locked | '--------'
  109. '--------' | | mux M1 |--. .--------.
  110. | '----------' '--| dev D2 |
  111. | .--------. '--------'
  112. '--| dev D3 |
  113. '--------'
  114. When there is an access to D1, this happens:
  115. 1. Someone issues an I2C transfer to D1.
  116. 2. M1 locks muxes on its parent (the root adapter in this case).
  117. 3. M1 locks its parent adapter.
  118. 4. M1 calls ->select to ready the mux.
  119. 5. If M1 does any I2C transfers (on this root adapter) as part of
  120. its select, those transfers must be unlocked I2C transfers so
  121. that they do not deadlock the root adapter.
  122. 6. M1 feeds the I2C transfer from step 1 to the root adapter as an
  123. unlocked I2C transfer, so that it does not deadlock the parent
  124. adapter.
  125. 7. M1 calls ->deselect, if it has one.
  126. 8. Same rules as in step 5, but for ->deselect.
  127. 9. M1 unlocks its parent adapter.
  128. 10. M1 unlocks muxes on its parent.
  129. This means that accesses to both D2 and D3 are locked out for the full
  130. duration of the entire operation.
  131. Parent-locked Caveats
  132. ~~~~~~~~~~~~~~~~~~~~~
  133. When using a parent-locked mux, be aware of the following restrictions:
  134. [PL1]
  135. If you build a topology with a parent-locked mux being the child
  136. of another mux, this might break a possible assumption from the
  137. child mux that the root adapter is unused between its select op
  138. and the actual transfer (e.g. if the child mux is auto-closing
  139. and the parent mux issues I2C transfers as part of its select).
  140. This is especially the case if the parent mux is mux-locked, but
  141. it may also happen if the parent mux is parent-locked.
  142. [PL2]
  143. If select/deselect calls out to other subsystems such as gpio,
  144. pinctrl, regmap or iio, it is essential that any I2C transfers
  145. caused by these subsystems are unlocked. This can be convoluted to
  146. accomplish, maybe even impossible if an acceptably clean solution
  147. is sought.
  148. Complex Examples
  149. ================
  150. Parent-locked mux as parent of parent-locked mux
  151. ------------------------------------------------
  152. This is a useful topology, but it can be bad::
  153. .----------. .----------. .--------.
  154. .--------. | parent- |-----| parent- |-----| dev D1 |
  155. | root |--+--| locked | | locked | '--------'
  156. '--------' | | mux M1 |--. | mux M2 |--. .--------.
  157. | '----------' | '----------' '--| dev D2 |
  158. | .--------. | .--------. '--------'
  159. '--| dev D4 | '--| dev D3 |
  160. '--------' '--------'
  161. When any device is accessed, all other devices are locked out for
  162. the full duration of the operation (both muxes lock their parent,
  163. and specifically when M2 requests its parent to lock, M1 passes
  164. the buck to the root adapter).
  165. This topology is bad if M2 is an auto-closing mux and M1->select
  166. issues any unlocked I2C transfers on the root adapter that may leak
  167. through and be seen by the M2 adapter, thus closing M2 prematurely.
  168. Mux-locked mux as parent of mux-locked mux
  169. ------------------------------------------
  170. This is a good topology::
  171. .----------. .----------. .--------.
  172. .--------. | mux- |-----| mux- |-----| dev D1 |
  173. | root |--+--| locked | | locked | '--------'
  174. '--------' | | mux M1 |--. | mux M2 |--. .--------.
  175. | '----------' | '----------' '--| dev D2 |
  176. | .--------. | .--------. '--------'
  177. '--| dev D4 | '--| dev D3 |
  178. '--------' '--------'
  179. When device D1 is accessed, accesses to D2 are locked out for the
  180. full duration of the operation (muxes on the top child adapter of M1
  181. are locked). But accesses to D3 and D4 are possibly interleaved at
  182. any point.
  183. Accesses to D3 locks out D1 and D2, but accesses to D4 are still possibly
  184. interleaved.
  185. Mux-locked mux as parent of parent-locked mux
  186. ---------------------------------------------
  187. This is probably a bad topology::
  188. .----------. .----------. .--------.
  189. .--------. | mux- |-----| parent- |-----| dev D1 |
  190. | root |--+--| locked | | locked | '--------'
  191. '--------' | | mux M1 |--. | mux M2 |--. .--------.
  192. | '----------' | '----------' '--| dev D2 |
  193. | .--------. | .--------. '--------'
  194. '--| dev D4 | '--| dev D3 |
  195. '--------' '--------'
  196. When device D1 is accessed, accesses to D2 and D3 are locked out
  197. for the full duration of the operation (M1 locks child muxes on the
  198. root adapter). But accesses to D4 are possibly interleaved at any
  199. point.
  200. This kind of topology is generally not suitable and should probably
  201. be avoided. The reason is that M2 probably assumes that there will
  202. be no I2C transfers during its calls to ->select and ->deselect, and
  203. if there are, any such transfers might appear on the slave side of M2
  204. as partial I2C transfers, i.e. garbage or worse. This might cause
  205. device lockups and/or other problems.
  206. The topology is especially troublesome if M2 is an auto-closing
  207. mux. In that case, any interleaved accesses to D4 might close M2
  208. prematurely, as might any I2C transfers part of M1->select.
  209. But if M2 is not making the above stated assumption, and if M2 is not
  210. auto-closing, the topology is fine.
  211. Parent-locked mux as parent of mux-locked mux
  212. ---------------------------------------------
  213. This is a good topology::
  214. .----------. .----------. .--------.
  215. .--------. | parent- |-----| mux- |-----| dev D1 |
  216. | root |--+--| locked | | locked | '--------'
  217. '--------' | | mux M1 |--. | mux M2 |--. .--------.
  218. | '----------' | '----------' '--| dev D2 |
  219. | .--------. | .--------. '--------'
  220. '--| dev D4 | '--| dev D3 |
  221. '--------' '--------'
  222. When D1 is accessed, accesses to D2 are locked out for the full
  223. duration of the operation (muxes on the top child adapter of M1
  224. are locked). Accesses to D3 and D4 are possibly interleaved at
  225. any point, just as is expected for mux-locked muxes.
  226. When D3 or D4 are accessed, everything else is locked out. For D3
  227. accesses, M1 locks the root adapter. For D4 accesses, the root
  228. adapter is locked directly.
  229. Two mux-locked sibling muxes
  230. ----------------------------
  231. This is a good topology::
  232. .--------.
  233. .----------. .--| dev D1 |
  234. | mux- |--' '--------'
  235. .--| locked | .--------.
  236. | | mux M1 |-----| dev D2 |
  237. | '----------' '--------'
  238. | .----------. .--------.
  239. .--------. | | mux- |-----| dev D3 |
  240. | root |--+--| locked | '--------'
  241. '--------' | | mux M2 |--. .--------.
  242. | '----------' '--| dev D4 |
  243. | .--------. '--------'
  244. '--| dev D5 |
  245. '--------'
  246. When D1 is accessed, accesses to D2, D3 and D4 are locked out. But
  247. accesses to D5 may be interleaved at any time.
  248. Two parent-locked sibling muxes
  249. -------------------------------
  250. This is a good topology::
  251. .--------.
  252. .----------. .--| dev D1 |
  253. | parent- |--' '--------'
  254. .--| locked | .--------.
  255. | | mux M1 |-----| dev D2 |
  256. | '----------' '--------'
  257. | .----------. .--------.
  258. .--------. | | parent- |-----| dev D3 |
  259. | root |--+--| locked | '--------'
  260. '--------' | | mux M2 |--. .--------.
  261. | '----------' '--| dev D4 |
  262. | .--------. '--------'
  263. '--| dev D5 |
  264. '--------'
  265. When any device is accessed, accesses to all other devices are locked
  266. out.
  267. Mux-locked and parent-locked sibling muxes
  268. ------------------------------------------
  269. This is a good topology::
  270. .--------.
  271. .----------. .--| dev D1 |
  272. | mux- |--' '--------'
  273. .--| locked | .--------.
  274. | | mux M1 |-----| dev D2 |
  275. | '----------' '--------'
  276. | .----------. .--------.
  277. .--------. | | parent- |-----| dev D3 |
  278. | root |--+--| locked | '--------'
  279. '--------' | | mux M2 |--. .--------.
  280. | '----------' '--| dev D4 |
  281. | .--------. '--------'
  282. '--| dev D5 |
  283. '--------'
  284. When D1 or D2 are accessed, accesses to D3 and D4 are locked out while
  285. accesses to D5 may interleave. When D3 or D4 are accessed, accesses to
  286. all other devices are locked out.
  287. Mux type of existing device drivers
  288. ===================================
  289. Whether a device is mux-locked or parent-locked depends on its
  290. implementation. The following list was correct at the time of writing:
  291. In drivers/i2c/muxes/:
  292. ====================== =============================================
  293. i2c-arb-gpio-challenge Parent-locked
  294. i2c-mux-gpio Normally parent-locked, mux-locked iff
  295. all involved gpio pins are controlled by the
  296. same I2C root adapter that they mux.
  297. i2c-mux-gpmux Normally parent-locked, mux-locked iff
  298. specified in device-tree.
  299. i2c-mux-ltc4306 Mux-locked
  300. i2c-mux-mlxcpld Parent-locked
  301. i2c-mux-pca9541 Parent-locked
  302. i2c-mux-pca954x Parent-locked
  303. i2c-mux-pinctrl Normally parent-locked, mux-locked iff
  304. all involved pinctrl devices are controlled
  305. by the same I2C root adapter that they mux.
  306. i2c-mux-reg Parent-locked
  307. ====================== =============================================
  308. In drivers/iio/:
  309. ====================== =============================================
  310. gyro/mpu3050 Mux-locked
  311. imu/inv_mpu6050/ Mux-locked
  312. ====================== =============================================
  313. In drivers/media/:
  314. ======================= =============================================
  315. dvb-frontends/lgdt3306a Mux-locked
  316. dvb-frontends/m88ds3103 Parent-locked
  317. dvb-frontends/rtl2830 Parent-locked
  318. dvb-frontends/rtl2832 Mux-locked
  319. dvb-frontends/si2168 Mux-locked
  320. usb/cx231xx/ Parent-locked
  321. ======================= =============================================