i2c-topology 15 KB

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