slave-interface 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. Linux I2C slave interface description
  2. =====================================
  3. by Wolfram Sang <wsa@sang-engineering.com> in 2014-15
  4. Linux can also be an I2C slave if the I2C controller in use has slave
  5. functionality. For that to work, one needs slave support in the bus driver plus
  6. a hardware independent software backend providing the actual functionality. An
  7. example for the latter is the slave-eeprom driver, which acts as a dual memory
  8. driver. While another I2C master on the bus can access it like a regular
  9. EEPROM, the Linux I2C slave can access the content via sysfs and handle data as
  10. needed. The backend driver and the I2C bus driver communicate via events. Here
  11. is a small graph visualizing the data flow and the means by which data is
  12. transported. The dotted line marks only one example. The backend could also
  13. use a character device, be in-kernel only, or something completely different:
  14. e.g. sysfs I2C slave events I/O registers
  15. +-----------+ v +---------+ v +--------+ v +------------+
  16. | Userspace +........+ Backend +-----------+ Driver +-----+ Controller |
  17. +-----------+ +---------+ +--------+ +------------+
  18. | |
  19. ----------------------------------------------------------------+-- I2C
  20. --------------------------------------------------------------+---- Bus
  21. Note: Technically, there is also the I2C core between the backend and the
  22. driver. However, at this time of writing, the layer is transparent.
  23. User manual
  24. ===========
  25. I2C slave backends behave like standard I2C clients. So, you can instantiate
  26. them as described in the document 'instantiating-devices'. The only difference
  27. is that i2c slave backends have their own address space. So, you have to add
  28. 0x1000 to the address you would originally request. An example for
  29. instantiating the slave-eeprom driver from userspace at the 7 bit address 0x64
  30. on bus 1:
  31. # echo slave-24c02 0x1064 > /sys/bus/i2c/devices/i2c-1/new_device
  32. Each backend should come with separate documentation to describe its specific
  33. behaviour and setup.
  34. Developer manual
  35. ================
  36. First, the events which are used by the bus driver and the backend will be
  37. described in detail. After that, some implementation hints for extending bus
  38. drivers and writing backends will be given.
  39. I2C slave events
  40. ----------------
  41. The bus driver sends an event to the backend using the following function:
  42. ret = i2c_slave_event(client, event, &val)
  43. 'client' describes the i2c slave device. 'event' is one of the special event
  44. types described hereafter. 'val' holds an u8 value for the data byte to be
  45. read/written and is thus bidirectional. The pointer to val must always be
  46. provided even if val is not used for an event, i.e. don't use NULL here. 'ret'
  47. is the return value from the backend. Mandatory events must be provided by the
  48. bus drivers and must be checked for by backend drivers.
  49. Event types:
  50. * I2C_SLAVE_WRITE_REQUESTED (mandatory)
  51. 'val': unused
  52. 'ret': always 0
  53. Another I2C master wants to write data to us. This event should be sent once
  54. our own address and the write bit was detected. The data did not arrive yet, so
  55. there is nothing to process or return. Wakeup or initialization probably needs
  56. to be done, though.
  57. * I2C_SLAVE_READ_REQUESTED (mandatory)
  58. 'val': backend returns first byte to be sent
  59. 'ret': always 0
  60. Another I2C master wants to read data from us. This event should be sent once
  61. our own address and the read bit was detected. After returning, the bus driver
  62. should transmit the first byte.
  63. * I2C_SLAVE_WRITE_RECEIVED (mandatory)
  64. 'val': bus driver delivers received byte
  65. 'ret': 0 if the byte should be acked, some errno if the byte should be nacked
  66. Another I2C master has sent a byte to us which needs to be set in 'val'. If 'ret'
  67. is zero, the bus driver should ack this byte. If 'ret' is an errno, then the byte
  68. should be nacked.
  69. * I2C_SLAVE_READ_PROCESSED (mandatory)
  70. 'val': backend returns next byte to be sent
  71. 'ret': always 0
  72. The bus driver requests the next byte to be sent to another I2C master in
  73. 'val'. Important: This does not mean that the previous byte has been acked, it
  74. only means that the previous byte is shifted out to the bus! To ensure seamless
  75. transmission, most hardware requests the next byte when the previous one is
  76. still shifted out. If the master sends NACK and stops reading after the byte
  77. currently shifted out, this byte requested here is never used. It very likely
  78. needs to be sent again on the next I2C_SLAVE_READ_REQUEST, depending a bit on
  79. your backend, though.
  80. * I2C_SLAVE_STOP (mandatory)
  81. 'val': unused
  82. 'ret': always 0
  83. A stop condition was received. This can happen anytime and the backend should
  84. reset its state machine for I2C transfers to be able to receive new requests.
  85. Software backends
  86. -----------------
  87. If you want to write a software backend:
  88. * use a standard i2c_driver and its matching mechanisms
  89. * write the slave_callback which handles the above slave events
  90. (best using a state machine)
  91. * register this callback via i2c_slave_register()
  92. Check the i2c-slave-eeprom driver as an example.
  93. Bus driver support
  94. ------------------
  95. If you want to add slave support to the bus driver:
  96. * implement calls to register/unregister the slave and add those to the
  97. struct i2c_algorithm. When registering, you probably need to set the i2c
  98. slave address and enable slave specific interrupts. If you use runtime pm, you
  99. should use pm_runtime_forbid() because your device usually needs to be powered
  100. on always to be able to detect its slave address. When unregistering, do the
  101. inverse of the above.
  102. * Catch the slave interrupts and send appropriate i2c_slave_events to the backend.
  103. Check the i2c-rcar driver as an example.
  104. About ACK/NACK
  105. --------------
  106. It is good behaviour to always ACK the address phase, so the master knows if a
  107. device is basically present or if it mysteriously disappeared. Using NACK to
  108. state being busy is troublesome. SMBus demands to always ACK the address phase,
  109. while the I2C specification is more loose on that. Most I2C controllers also
  110. automatically ACK when detecting their slave addresses, so there is no option
  111. to NACK them. For those reasons, this API does not support NACK in the address
  112. phase.
  113. Currently, there is no slave event to report if the master did ACK or NACK a
  114. byte when it reads from us. We could make this an optional event if the need
  115. arises. However, cases should be extremely rare because the master is expected
  116. to send STOP after that and we have an event for that. Also, keep in mind not
  117. all I2C controllers have the possibility to report that event.
  118. About buffers
  119. -------------
  120. During development of this API, the question of using buffers instead of just
  121. bytes came up. Such an extension might be possible, usefulness is unclear at
  122. this time of writing. Some points to keep in mind when using buffers:
  123. * Buffers should be opt-in and slave drivers will always have to support
  124. byte-based transactions as the ultimate fallback because this is how the
  125. majority of HW works.
  126. * For backends simulating hardware registers, buffers are not helpful because
  127. on writes an action should be immediately triggered. For reads, the data in
  128. the buffer might get stale.
  129. * A master can send STOP at any time. For partially transferred buffers, this
  130. means additional code to handle this exception. Such code tends to be
  131. error-prone.