slave-interface 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_get_sync() because your device usually needs to be
  100. powered on always to be able to detect its slave address. When unregistering,
  101. do the inverse of the above.
  102. * Catch the slave interrupts and send appropriate i2c_slave_events to the backend.
  103. Note that most hardware supports being master _and_ slave on the same bus. So,
  104. if you extend a bus driver, please make sure that the driver supports that as
  105. well. In almost all cases, slave support does not need to disable the master
  106. functionality.
  107. Check the i2c-rcar driver as an example.
  108. About ACK/NACK
  109. --------------
  110. It is good behaviour to always ACK the address phase, so the master knows if a
  111. device is basically present or if it mysteriously disappeared. Using NACK to
  112. state being busy is troublesome. SMBus demands to always ACK the address phase,
  113. while the I2C specification is more loose on that. Most I2C controllers also
  114. automatically ACK when detecting their slave addresses, so there is no option
  115. to NACK them. For those reasons, this API does not support NACK in the address
  116. phase.
  117. Currently, there is no slave event to report if the master did ACK or NACK a
  118. byte when it reads from us. We could make this an optional event if the need
  119. arises. However, cases should be extremely rare because the master is expected
  120. to send STOP after that and we have an event for that. Also, keep in mind not
  121. all I2C controllers have the possibility to report that event.
  122. About buffers
  123. -------------
  124. During development of this API, the question of using buffers instead of just
  125. bytes came up. Such an extension might be possible, usefulness is unclear at
  126. this time of writing. Some points to keep in mind when using buffers:
  127. * Buffers should be opt-in and backend drivers will always have to support
  128. byte-based transactions as the ultimate fallback anyhow because this is how
  129. the majority of HW works.
  130. * For backends simulating hardware registers, buffers are largely not helpful
  131. because after each byte written an action should be immediately triggered.
  132. For reads, the data kept in the buffer might get stale if the backend just
  133. updated a register because of internal processing.
  134. * A master can send STOP at any time. For partially transferred buffers, this
  135. means additional code to handle this exception. Such code tends to be
  136. error-prone.