slave-interface 6.9 KB

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