i2c-topology 15 KB

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