···11+I2C topology22+============33+44+There are a couple of reasons for building more complex i2c topologies55+than a straight-forward i2c bus with one adapter and one or more devices.66+77+1. A mux may be needed on the bus to prevent address collisions.88+99+2. The bus may be accessible from some external bus master, and arbitration1010+ may be needed to determine if it is ok to access the bus.1111+1212+3. A device (particularly RF tuners) may want to avoid the digital noise1313+ from the i2c bus, at least most of the time, and sits behind a gate1414+ that has to be operated before the device can be accessed.1515+1616+Etc1717+1818+These constructs are represented as i2c adapter trees by Linux, where1919+each adapter has a parent adapter (except the root adapter) and zero or2020+more child adapters. The root adapter is the actual adapter that issues2121+i2c transfers, and all adapters with a parent are part of an "i2c-mux"2222+object (quoted, since it can also be an arbitrator or a gate).2323+2424+Depending of the particular mux driver, something happens when there is2525+an i2c transfer on one of its child adapters. The mux driver can2626+obviously operate a mux, but it can also do arbitration with an external2727+bus master or open a gate. The mux driver has two operations for this,2828+select and deselect. select is called before the transfer and (the2929+optional) deselect is called after the transfer.3030+3131+3232+Locking3333+=======3434+3535+There are two variants of locking available to i2c muxes, they can be3636+mux-locked or parent-locked muxes. As is evident from below, it can be3737+useful to know if a mux is mux-locked or if it is parent-locked. The3838+following list was correct at the time of writing:3939+4040+In drivers/i2c/muxes/4141+i2c-arb-gpio-challenge Parent-locked4242+i2c-mux-gpio Normally parent-locked, mux-locked iff4343+ all involved gpio pins are controlled by the4444+ same i2c root adapter that they mux.4545+i2c-mux-pca9541 Parent-locked4646+i2c-mux-pca954x Parent-locked4747+i2c-mux-pinctrl Normally parent-locked, mux-locked iff4848+ all involved pinctrl devices are controlled4949+ by the same i2c root adapter that they mux.5050+i2c-mux-reg Parent-locked5151+5252+In drivers/iio/5353+imu/inv_mpu6050/ Parent-locked5454+5555+In drivers/media/5656+dvb-frontends/m88ds3103 Parent-locked5757+dvb-frontends/rtl2830 Parent-locked5858+dvb-frontends/rtl2832 Parent-locked5959+dvb-frontends/si2168 Parent-locked6060+usb/cx231xx/ Parent-locked6161+6262+6363+Mux-locked muxes6464+----------------6565+6666+Mux-locked muxes does not lock the entire parent adapter during the6767+full select-transfer-deselect transaction, only the muxes on the parent6868+adapter are locked. Mux-locked muxes are mostly interesting if the6969+select and/or deselect operations must use i2c transfers to complete7070+their tasks. Since the parent adapter is not fully locked during the7171+full transaction, unrelated i2c transfers may interleave the different7272+stages of the transaction. This has the benefit that the mux driver7373+may be easier and cleaner to implement, but it has some caveats.7474+7575+ML1. If you build a topology with a mux-locked mux being the parent7676+ of a parent-locked mux, this might break the expectation from the7777+ parent-locked mux that the root adapter is locked during the7878+ transaction.7979+8080+ML2. It is not safe to build arbitrary topologies with two (or more)8181+ mux-locked muxes that are not siblings, when there are address8282+ collisions between the devices on the child adapters of these8383+ non-sibling muxes.8484+8585+ I.e. the select-transfer-deselect transaction targeting e.g. device8686+ address 0x42 behind mux-one may be interleaved with a similar8787+ operation targeting device address 0x42 behind mux-two. The8888+ intension with such a topology would in this hypothetical example8989+ be that mux-one and mux-two should not be selected simultaneously,9090+ but mux-locked muxes do not guarantee that in all topologies.9191+9292+ML3. A mux-locked mux cannot be used by a driver for auto-closing9393+ gates/muxes, i.e. something that closes automatically after a given9494+ number (one, in most cases) of i2c transfers. Unrelated i2c transfers9595+ may creep in and close prematurely.9696+9797+ML4. If any non-i2c operation in the mux driver changes the i2c mux state,9898+ the driver has to lock the root adapter during that operation.9999+ Otherwise garbage may appear on the bus as seen from devices100100+ behind the mux, when an unrelated i2c transfer is in flight during101101+ the non-i2c mux-changing operation.102102+103103+104104+Mux-locked Example105105+------------------106106+107107+ .----------. .--------.108108+ .--------. | mux- |-----| dev D1 |109109+ | root |--+--| locked | '--------'110110+ '--------' | | mux M1 |--. .--------.111111+ | '----------' '--| dev D2 |112112+ | .--------. '--------'113113+ '--| dev D3 |114114+ '--------'115115+116116+When there is an access to D1, this happens:117117+118118+ 1. Someone issues an i2c-transfer to D1.119119+ 2. M1 locks muxes on its parent (the root adapter in this case).120120+ 3. M1 calls ->select to ready the mux.121121+ 4. M1 (presumably) does some i2c-transfers as part of its select.122122+ These transfers are normal i2c-transfers that locks the parent123123+ adapter.124124+ 5. M1 feeds the i2c-transfer from step 1 to its parent adapter as a125125+ normal i2c-transfer that locks the parent adapter.126126+ 6. M1 calls ->deselect, if it has one.127127+ 7. Same rules as in step 4, but for ->deselect.128128+ 8. M1 unlocks muxes on its parent.129129+130130+This means that accesses to D2 are lockout out for the full duration131131+of the entire operation. But accesses to D3 are possibly interleaved132132+at any point.133133+134134+135135+Parent-locked muxes136136+-------------------137137+138138+Parent-locked muxes lock the parent adapter during the full select-139139+transfer-deselect transaction. The implication is that the mux driver140140+has to ensure that any and all i2c transfers through that parent141141+adapter during the transaction are unlocked i2c transfers (using e.g.142142+__i2c_transfer), or a deadlock will follow. There are a couple of143143+caveats.144144+145145+PL1. If you build a topology with a parent-locked mux being the child146146+ of another mux, this might break a possible assumption from the147147+ child mux that the root adapter is unused between its select op148148+ and the actual transfer (e.g. if the child mux is auto-closing149149+ and the parent mux issus i2c-transfers as part of its select).150150+ This is especially the case if the parent mux is mux-locked, but151151+ it may also happen if the parent mux is parent-locked.152152+153153+PL2. If select/deselect calls out to other subsystems such as gpio,154154+ pinctrl, regmap or iio, it is essential that any i2c transfers155155+ caused by these subsystems are unlocked. This can be convoluted to156156+ accomplish, maybe even impossible if an acceptably clean solution157157+ is sought.158158+159159+160160+Parent-locked Example161161+---------------------162162+163163+ .----------. .--------.164164+ .--------. | parent- |-----| dev D1 |165165+ | root |--+--| locked | '--------'166166+ '--------' | | mux M1 |--. .--------.167167+ | '----------' '--| dev D2 |168168+ | .--------. '--------'169169+ '--| dev D3 |170170+ '--------'171171+172172+When there is an access to D1, this happens:173173+174174+ 1. Someone issues an i2c-transfer to D1.175175+ 2. M1 locks muxes on its parent (the root adapter in this case).176176+ 3. M1 locks its parent adapter.177177+ 4. M1 calls ->select to ready the mux.178178+ 5. If M1 does any i2c-transfers (on this root adapter) as part of179179+ its select, those transfers must be unlocked i2c-transfers so180180+ that they do not deadlock the root adapter.181181+ 6. M1 feeds the i2c-transfer from step 1 to the root adapter as an182182+ unlocked i2c-transfer, so that it does not deadlock the parent183183+ adapter.184184+ 7. M1 calls ->deselect, if it has one.185185+ 8. Same rules as in step 5, but for ->deselect.186186+ 9. M1 unlocks its parent adapter.187187+10. M1 unlocks muxes on its parent.188188+189189+190190+This means that accesses to both D2 and D3 are locked out for the full191191+duration of the entire operation.192192+193193+194194+Complex Examples195195+================196196+197197+Parent-locked mux as parent of parent-locked mux198198+------------------------------------------------199199+200200+This is a useful topology, but it can be bad.201201+202202+ .----------. .----------. .--------.203203+ .--------. | parent- |-----| parent- |-----| dev D1 |204204+ | root |--+--| locked | | locked | '--------'205205+ '--------' | | mux M1 |--. | mux M2 |--. .--------.206206+ | '----------' | '----------' '--| dev D2 |207207+ | .--------. | .--------. '--------'208208+ '--| dev D4 | '--| dev D3 |209209+ '--------' '--------'210210+211211+When any device is accessed, all other devices are locked out for212212+the full duration of the operation (both muxes lock their parent,213213+and specifically when M2 requests its parent to lock, M1 passes214214+the buck to the root adapter).215215+216216+This topology is bad if M2 is an auto-closing mux and M1->select217217+issues any unlocked i2c transfers on the root adapter that may leak218218+through and be seen by the M2 adapter, thus closing M2 prematurely.219219+220220+221221+Mux-locked mux as parent of mux-locked mux222222+------------------------------------------223223+224224+This is a good topology.225225+226226+ .----------. .----------. .--------.227227+ .--------. | mux- |-----| mux- |-----| dev D1 |228228+ | root |--+--| locked | | locked | '--------'229229+ '--------' | | mux M1 |--. | mux M2 |--. .--------.230230+ | '----------' | '----------' '--| dev D2 |231231+ | .--------. | .--------. '--------'232232+ '--| dev D4 | '--| dev D3 |233233+ '--------' '--------'234234+235235+When device D1 is accessed, accesses to D2 are locked out for the236236+full duration of the operation (muxes on the top child adapter of M1237237+are locked). But accesses to D3 and D4 are possibly interleaved at238238+any point. Accesses to D3 locks out D1 and D2, but accesses to D4239239+are still possibly interleaved.240240+241241+242242+Mux-locked mux as parent of parent-locked mux243243+---------------------------------------------244244+245245+This is probably a bad topology.246246+247247+ .----------. .----------. .--------.248248+ .--------. | mux- |-----| parent- |-----| dev D1 |249249+ | root |--+--| locked | | locked | '--------'250250+ '--------' | | mux M1 |--. | mux M2 |--. .--------.251251+ | '----------' | '----------' '--| dev D2 |252252+ | .--------. | .--------. '--------'253253+ '--| dev D4 | '--| dev D3 |254254+ '--------' '--------'255255+256256+When device D1 is accessed, accesses to D2 and D3 are locked out257257+for the full duration of the operation (M1 locks child muxes on the258258+root adapter). But accesses to D4 are possibly interleaved at any259259+point.260260+261261+This kind of topology is generally not suitable and should probably262262+be avoided. The reason is that M2 probably assumes that there will263263+be no i2c transfers during its calls to ->select and ->deselect, and264264+if there are, any such transfers might appear on the slave side of M2265265+as partial i2c transfers, i.e. garbage or worse. This might cause266266+device lockups and/or other problems.267267+268268+The topology is especially troublesome if M2 is an auto-closing269269+mux. In that case, any interleaved accesses to D4 might close M2270270+prematurely, as might any i2c-transfers part of M1->select.271271+272272+But if M2 is not making the above stated assumption, and if M2 is not273273+auto-closing, the topology is fine.274274+275275+276276+Parent-locked mux as parent of mux-locked mux277277+---------------------------------------------278278+279279+This is a good topology.280280+281281+ .----------. .----------. .--------.282282+ .--------. | parent- |-----| mux- |-----| dev D1 |283283+ | root |--+--| locked | | locked | '--------'284284+ '--------' | | mux M1 |--. | mux M2 |--. .--------.285285+ | '----------' | '----------' '--| dev D2 |286286+ | .--------. | .--------. '--------'287287+ '--| dev D4 | '--| dev D3 |288288+ '--------' '--------'289289+290290+When D1 is accessed, accesses to D2 are locked out for the full291291+duration of the operation (muxes on the top child adapter of M1292292+are locked). Accesses to D3 and D4 are possibly interleaved at293293+any point, just as is expected for mux-locked muxes.294294+295295+When D3 or D4 are accessed, everything else is locked out. For D3296296+accesses, M1 locks the root adapter. For D4 accesses, the root297297+adapter is locked directly.298298+299299+300300+Two mux-locked sibling muxes301301+----------------------------302302+303303+This is a good topology.304304+305305+ .--------.306306+ .----------. .--| dev D1 |307307+ | mux- |--' '--------'308308+ .--| locked | .--------.309309+ | | mux M1 |-----| dev D2 |310310+ | '----------' '--------'311311+ | .----------. .--------.312312+ .--------. | | mux- |-----| dev D3 |313313+ | root |--+--| locked | '--------'314314+ '--------' | | mux M2 |--. .--------.315315+ | '----------' '--| dev D4 |316316+ | .--------. '--------'317317+ '--| dev D5 |318318+ '--------'319319+320320+When D1 is accessed, accesses to D2, D3 and D4 are locked out. But321321+accesses to D5 may be interleaved at any time.322322+323323+324324+Two parent-locked sibling muxes325325+-------------------------------326326+327327+This is a good topology.328328+329329+ .--------.330330+ .----------. .--| dev D1 |331331+ | parent- |--' '--------'332332+ .--| locked | .--------.333333+ | | mux M1 |-----| dev D2 |334334+ | '----------' '--------'335335+ | .----------. .--------.336336+ .--------. | | parent- |-----| dev D3 |337337+ | root |--+--| locked | '--------'338338+ '--------' | | mux M2 |--. .--------.339339+ | '----------' '--| dev D4 |340340+ | .--------. '--------'341341+ '--| dev D5 |342342+ '--------'343343+344344+When any device is accessed, accesses to all other devices are locked345345+out.346346+347347+348348+Mux-locked and parent-locked sibling muxes349349+------------------------------------------350350+351351+This is a good topology.352352+353353+ .--------.354354+ .----------. .--| dev D1 |355355+ | mux- |--' '--------'356356+ .--| locked | .--------.357357+ | | mux M1 |-----| dev D2 |358358+ | '----------' '--------'359359+ | .----------. .--------.360360+ .--------. | | parent- |-----| dev D3 |361361+ | root |--+--| locked | '--------'362362+ '--------' | | mux M2 |--. .--------.363363+ | '----------' '--| dev D4 |364364+ | .--------. '--------'365365+ '--| dev D5 |366366+ '--------'367367+368368+When D1 or D2 are accessed, accesses to D3 and D4 are locked out while369369+accesses to D5 may interleave. When D3 or D4 are accessed, accesses to370370+all other devices are locked out.
+1
MAINTAINERS
···52755275M: Peter Rosin <peda@axentia.se>52765276L: linux-i2c@vger.kernel.org52775277S: Maintained52785278+F: Documentation/i2c/i2c-topology52785279F: Documentation/i2c/muxes/52795280F: Documentation/devicetree/bindings/i2c/i2c-mux*52805281F: drivers/i2c/i2c-mux.c