stream.rst 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. =========================
  2. Audio Stream in SoundWire
  3. =========================
  4. An audio stream is a logical or virtual connection created between
  5. (1) System memory buffer(s) and Codec(s)
  6. (2) DSP memory buffer(s) and Codec(s)
  7. (3) FIFO(s) and Codec(s)
  8. (4) Codec(s) and Codec(s)
  9. which is typically driven by a DMA(s) channel through the data link. An
  10. audio stream contains one or more channels of data. All channels within
  11. stream must have same sample rate and same sample size.
  12. Assume a stream with two channels (Left & Right) is opened using SoundWire
  13. interface. Below are some ways a stream can be represented in SoundWire.
  14. Stream Sample in memory (System memory, DSP memory or FIFOs) ::
  15. -------------------------
  16. | L | R | L | R | L | R |
  17. -------------------------
  18. Example 1: Stereo Stream with L and R channels is rendered from Master to
  19. Slave. Both Master and Slave is using single port. ::
  20. +---------------+ Clock Signal +---------------+
  21. | Master +----------------------------------+ Slave |
  22. | Interface | | Interface |
  23. | | | 1 |
  24. | | Data Signal | |
  25. | L + R +----------------------------------+ L + R |
  26. | (Data) | Data Direction | (Data) |
  27. +---------------+ +-----------------------> +---------------+
  28. Example 2: Stereo Stream with L and R channels is captured from Slave to
  29. Master. Both Master and Slave is using single port. ::
  30. +---------------+ Clock Signal +---------------+
  31. | Master +----------------------------------+ Slave |
  32. | Interface | | Interface |
  33. | | | 1 |
  34. | | Data Signal | |
  35. | L + R +----------------------------------+ L + R |
  36. | (Data) | Data Direction | (Data) |
  37. +---------------+ <-----------------------+ +---------------+
  38. Example 3: Stereo Stream with L and R channels is rendered by Master. Each
  39. of the L and R channel is received by two different Slaves. Master and both
  40. Slaves are using single port. ::
  41. +---------------+ Clock Signal +---------------+
  42. | Master +---------+------------------------+ Slave |
  43. | Interface | | | Interface |
  44. | | | | 1 |
  45. | | | Data Signal | |
  46. | L + R +---+------------------------------+ L |
  47. | (Data) | | | Data Direction | (Data) |
  48. +---------------+ | | +-------------> +---------------+
  49. | |
  50. | |
  51. | | +---------------+
  52. | +----------------------> | Slave |
  53. | | Interface |
  54. | | 2 |
  55. | | |
  56. +----------------------------> | R |
  57. | (Data) |
  58. +---------------+
  59. Example 4: Stereo Stream with L and R channels is rendered by
  60. Master. Both of the L and R channels are received by two different
  61. Slaves. Master and both Slaves are using single port handling
  62. L+R. Each Slave device processes the L + R data locally, typically
  63. based on static configuration or dynamic orientation, and may drive
  64. one or more speakers. ::
  65. +---------------+ Clock Signal +---------------+
  66. | Master +---------+------------------------+ Slave |
  67. | Interface | | | Interface |
  68. | | | | 1 |
  69. | | | Data Signal | |
  70. | L + R +---+------------------------------+ L + R |
  71. | (Data) | | | Data Direction | (Data) |
  72. +---------------+ | | +-------------> +---------------+
  73. | |
  74. | |
  75. | | +---------------+
  76. | +----------------------> | Slave |
  77. | | Interface |
  78. | | 2 |
  79. | | |
  80. +----------------------------> | L + R |
  81. | (Data) |
  82. +---------------+
  83. Example 5: Stereo Stream with L and R channel is rendered by two different
  84. Ports of the Master and is received by only single Port of the Slave
  85. interface. ::
  86. +--------------------+
  87. | |
  88. | +--------------+ +----------------+
  89. | | || | |
  90. | | Data Port || L Channel | |
  91. | | 1 |------------+ | |
  92. | | L Channel || | +-----+----+ |
  93. | | (Data) || | L + R Channel || Data | |
  94. | Master +----------+ | +---+---------> || Port | |
  95. | Interface | | || 1 | |
  96. | +--------------+ | || | |
  97. | | || | +----------+ |
  98. | | Data Port |------------+ | |
  99. | | 2 || R Channel | Slave |
  100. | | R Channel || | Interface |
  101. | | (Data) || | 1 |
  102. | +--------------+ Clock Signal | L + R |
  103. | +---------------------------> | (Data) |
  104. +--------------------+ | |
  105. +----------------+
  106. Example 6: Stereo Stream with L and R channel is rendered by 2 Masters, each
  107. rendering one channel, and is received by two different Slaves, each
  108. receiving one channel. Both Masters and both Slaves are using single port. ::
  109. +---------------+ Clock Signal +---------------+
  110. | Master +----------------------------------+ Slave |
  111. | Interface | | Interface |
  112. | 1 | | 1 |
  113. | | Data Signal | |
  114. | L +----------------------------------+ L |
  115. | (Data) | Data Direction | (Data) |
  116. +---------------+ +-----------------------> +---------------+
  117. +---------------+ Clock Signal +---------------+
  118. | Master +----------------------------------+ Slave |
  119. | Interface | | Interface |
  120. | 2 | | 2 |
  121. | | Data Signal | |
  122. | R +----------------------------------+ R |
  123. | (Data) | Data Direction | (Data) |
  124. +---------------+ +-----------------------> +---------------+
  125. Example 7: Stereo Stream with L and R channel is rendered by 2
  126. Masters, each rendering both channels. Each Slave receives L + R. This
  127. is the same application as Example 4 but with Slaves placed on
  128. separate links. ::
  129. +---------------+ Clock Signal +---------------+
  130. | Master +----------------------------------+ Slave |
  131. | Interface | | Interface |
  132. | 1 | | 1 |
  133. | | Data Signal | |
  134. | L + R +----------------------------------+ L + R |
  135. | (Data) | Data Direction | (Data) |
  136. +---------------+ +-----------------------> +---------------+
  137. +---------------+ Clock Signal +---------------+
  138. | Master +----------------------------------+ Slave |
  139. | Interface | | Interface |
  140. | 2 | | 2 |
  141. | | Data Signal | |
  142. | L + R +----------------------------------+ L + R |
  143. | (Data) | Data Direction | (Data) |
  144. +---------------+ +-----------------------> +---------------+
  145. Example 8: 4-channel Stream is rendered by 2 Masters, each rendering a
  146. 2 channels. Each Slave receives 2 channels. ::
  147. +---------------+ Clock Signal +---------------+
  148. | Master +----------------------------------+ Slave |
  149. | Interface | | Interface |
  150. | 1 | | 1 |
  151. | | Data Signal | |
  152. | L1 + R1 +----------------------------------+ L1 + R1 |
  153. | (Data) | Data Direction | (Data) |
  154. +---------------+ +-----------------------> +---------------+
  155. +---------------+ Clock Signal +---------------+
  156. | Master +----------------------------------+ Slave |
  157. | Interface | | Interface |
  158. | 2 | | 2 |
  159. | | Data Signal | |
  160. | L2 + R2 +----------------------------------+ L2 + R2 |
  161. | (Data) | Data Direction | (Data) |
  162. +---------------+ +-----------------------> +---------------+
  163. Note1: In multi-link cases like above, to lock, one would acquire a global
  164. lock and then go on locking bus instances. But, in this case the caller
  165. framework(ASoC DPCM) guarantees that stream operations on a card are
  166. always serialized. So, there is no race condition and hence no need for
  167. global lock.
  168. Note2: A Slave device may be configured to receive all channels
  169. transmitted on a link for a given Stream (Example 4) or just a subset
  170. of the data (Example 3). The configuration of the Slave device is not
  171. handled by a SoundWire subsystem API, but instead by the
  172. snd_soc_dai_set_tdm_slot() API. The platform or machine driver will
  173. typically configure which of the slots are used. For Example 4, the
  174. same slots would be used by all Devices, while for Example 3 the Slave
  175. Device1 would use e.g. Slot 0 and Slave device2 slot 1.
  176. Note3: Multiple Sink ports can extract the same information for the
  177. same bitSlots in the SoundWire frame, however multiple Source ports
  178. shall be configured with different bitSlot configurations. This is the
  179. same limitation as with I2S/PCM TDM usages.
  180. SoundWire Stream Management flow
  181. ================================
  182. Stream definitions
  183. ------------------
  184. (1) Current stream: This is classified as the stream on which operation has
  185. to be performed like prepare, enable, disable, de-prepare etc.
  186. (2) Active stream: This is classified as the stream which is already active
  187. on Bus other than current stream. There can be multiple active streams
  188. on the Bus.
  189. SoundWire Bus manages stream operations for each stream getting
  190. rendered/captured on the SoundWire Bus. This section explains Bus operations
  191. done for each of the stream allocated/released on Bus. Following are the
  192. stream states maintained by the Bus for each of the audio stream.
  193. SoundWire stream states
  194. -----------------------
  195. Below shows the SoundWire stream states and state transition diagram. ::
  196. +-----------+ +------------+ +----------+ +----------+
  197. | ALLOCATED +---->| CONFIGURED +---->| PREPARED +---->| ENABLED |
  198. | STATE | | STATE | | STATE | | STATE |
  199. +-----------+ +------------+ +---+--+---+ +----+-----+
  200. ^ ^ ^
  201. | | |
  202. __| |___________ |
  203. | | |
  204. v | v
  205. +----------+ +-----+------+ +-+--+-----+
  206. | RELEASED |<----------+ DEPREPARED |<-------+ DISABLED |
  207. | STATE | | STATE | | STATE |
  208. +----------+ +------------+ +----------+
  209. NOTE: State transitions between ``SDW_STREAM_ENABLED`` and
  210. ``SDW_STREAM_DISABLED`` are only relevant when then INFO_PAUSE flag is
  211. supported at the ALSA/ASoC level. Likewise the transition between
  212. ``SDW_DISABLED_STATE`` and ``SDW_PREPARED_STATE`` depends on the
  213. INFO_RESUME flag.
  214. NOTE2: The framework implements basic state transition checks, but
  215. does not e.g. check if a transition from DISABLED to ENABLED is valid
  216. on a specific platform. Such tests need to be added at the ALSA/ASoC
  217. level.
  218. Stream State Operations
  219. -----------------------
  220. Below section explains the operations done by the Bus on Master(s) and
  221. Slave(s) as part of stream state transitions.
  222. SDW_STREAM_ALLOCATED
  223. ~~~~~~~~~~~~~~~~~~~~
  224. Allocation state for stream. This is the entry state
  225. of the stream. Operations performed before entering in this state:
  226. (1) A stream runtime is allocated for the stream. This stream
  227. runtime is used as a reference for all the operations performed
  228. on the stream.
  229. (2) The resources required for holding stream runtime information are
  230. allocated and initialized. This holds all stream related information
  231. such as stream type (PCM/PDM) and parameters, Master and Slave
  232. interface associated with the stream, stream state etc.
  233. After all above operations are successful, stream state is set to
  234. ``SDW_STREAM_ALLOCATED``.
  235. Bus implements below API for allocate a stream which needs to be called once
  236. per stream. From ASoC DPCM framework, this stream state maybe linked to
  237. .startup() operation.
  238. .. code-block:: c
  239. int sdw_alloc_stream(char * stream_name);
  240. The SoundWire core provides a sdw_startup_stream() helper function,
  241. typically called during a dailink .startup() callback, which performs
  242. stream allocation and sets the stream pointer for all DAIs
  243. connected to a stream.
  244. SDW_STREAM_CONFIGURED
  245. ~~~~~~~~~~~~~~~~~~~~~
  246. Configuration state of stream. Operations performed before entering in
  247. this state:
  248. (1) The resources allocated for stream information in SDW_STREAM_ALLOCATED
  249. state are updated here. This includes stream parameters, Master(s)
  250. and Slave(s) runtime information associated with current stream.
  251. (2) All the Master(s) and Slave(s) associated with current stream provide
  252. the port information to Bus which includes port numbers allocated by
  253. Master(s) and Slave(s) for current stream and their channel mask.
  254. After all above operations are successful, stream state is set to
  255. ``SDW_STREAM_CONFIGURED``.
  256. Bus implements below APIs for CONFIG state which needs to be called by
  257. the respective Master(s) and Slave(s) associated with stream. These APIs can
  258. only be invoked once by respective Master(s) and Slave(s). From ASoC DPCM
  259. framework, this stream state is linked to .hw_params() operation.
  260. .. code-block:: c
  261. int sdw_stream_add_master(struct sdw_bus * bus,
  262. struct sdw_stream_config * stream_config,
  263. struct sdw_ports_config * ports_config,
  264. struct sdw_stream_runtime * stream);
  265. int sdw_stream_add_slave(struct sdw_slave * slave,
  266. struct sdw_stream_config * stream_config,
  267. struct sdw_ports_config * ports_config,
  268. struct sdw_stream_runtime * stream);
  269. SDW_STREAM_PREPARED
  270. ~~~~~~~~~~~~~~~~~~~
  271. Prepare state of stream. Operations performed before entering in this state:
  272. (0) Steps 1 and 2 are omitted in the case of a resume operation,
  273. where the bus bandwidth is known.
  274. (1) Bus parameters such as bandwidth, frame shape, clock frequency,
  275. are computed based on current stream as well as already active
  276. stream(s) on Bus. Re-computation is required to accommodate current
  277. stream on the Bus.
  278. (2) Transport and port parameters of all Master(s) and Slave(s) port(s) are
  279. computed for the current as well as already active stream based on frame
  280. shape and clock frequency computed in step 1.
  281. (3) Computed Bus and transport parameters are programmed in Master(s) and
  282. Slave(s) registers. The banked registers programming is done on the
  283. alternate bank (bank currently unused). Port(s) are enabled for the
  284. already active stream(s) on the alternate bank (bank currently unused).
  285. This is done in order to not disrupt already active stream(s).
  286. (4) Once all the values are programmed, Bus initiates switch to alternate
  287. bank where all new values programmed gets into effect.
  288. (5) Ports of Master(s) and Slave(s) for current stream are prepared by
  289. programming PrepareCtrl register.
  290. After all above operations are successful, stream state is set to
  291. ``SDW_STREAM_PREPARED``.
  292. Bus implements below API for PREPARE state which needs to be called
  293. once per stream. From ASoC DPCM framework, this stream state is linked
  294. to .prepare() operation. Since the .trigger() operations may not
  295. follow the .prepare(), a direct transition from
  296. ``SDW_STREAM_PREPARED`` to ``SDW_STREAM_DEPREPARED`` is allowed.
  297. .. code-block:: c
  298. int sdw_prepare_stream(struct sdw_stream_runtime * stream);
  299. SDW_STREAM_ENABLED
  300. ~~~~~~~~~~~~~~~~~~
  301. Enable state of stream. The data port(s) are enabled upon entering this state.
  302. Operations performed before entering in this state:
  303. (1) All the values computed in SDW_STREAM_PREPARED state are programmed
  304. in alternate bank (bank currently unused). It includes programming of
  305. already active stream(s) as well.
  306. (2) All the Master(s) and Slave(s) port(s) for the current stream are
  307. enabled on alternate bank (bank currently unused) by programming
  308. ChannelEn register.
  309. (3) Once all the values are programmed, Bus initiates switch to alternate
  310. bank where all new values programmed gets into effect and port(s)
  311. associated with current stream are enabled.
  312. After all above operations are successful, stream state is set to
  313. ``SDW_STREAM_ENABLED``.
  314. Bus implements below API for ENABLE state which needs to be called once per
  315. stream. From ASoC DPCM framework, this stream state is linked to
  316. .trigger() start operation.
  317. .. code-block:: c
  318. int sdw_enable_stream(struct sdw_stream_runtime * stream);
  319. SDW_STREAM_DISABLED
  320. ~~~~~~~~~~~~~~~~~~~
  321. Disable state of stream. The data port(s) are disabled upon exiting this state.
  322. Operations performed before entering in this state:
  323. (1) All the Master(s) and Slave(s) port(s) for the current stream are
  324. disabled on alternate bank (bank currently unused) by programming
  325. ChannelEn register.
  326. (2) All the current configuration of Bus and active stream(s) are programmed
  327. into alternate bank (bank currently unused).
  328. (3) Once all the values are programmed, Bus initiates switch to alternate
  329. bank where all new values programmed gets into effect and port(s) associated
  330. with current stream are disabled.
  331. After all above operations are successful, stream state is set to
  332. ``SDW_STREAM_DISABLED``.
  333. Bus implements below API for DISABLED state which needs to be called once
  334. per stream. From ASoC DPCM framework, this stream state is linked to
  335. .trigger() stop operation.
  336. When the INFO_PAUSE flag is supported, a direct transition to
  337. ``SDW_STREAM_ENABLED`` is allowed.
  338. For resume operations where ASoC will use the .prepare() callback, the
  339. stream can transition from ``SDW_STREAM_DISABLED`` to
  340. ``SDW_STREAM_PREPARED``, with all required settings restored but
  341. without updating the bandwidth and bit allocation.
  342. .. code-block:: c
  343. int sdw_disable_stream(struct sdw_stream_runtime * stream);
  344. SDW_STREAM_DEPREPARED
  345. ~~~~~~~~~~~~~~~~~~~~~
  346. De-prepare state of stream. Operations performed before entering in this
  347. state:
  348. (1) All the port(s) of Master(s) and Slave(s) for current stream are
  349. de-prepared by programming PrepareCtrl register.
  350. (2) The payload bandwidth of current stream is reduced from the total
  351. bandwidth requirement of bus and new parameters calculated and
  352. applied by performing bank switch etc.
  353. After all above operations are successful, stream state is set to
  354. ``SDW_STREAM_DEPREPARED``.
  355. Bus implements below API for DEPREPARED state which needs to be called
  356. once per stream. ALSA/ASoC do not have a concept of 'deprepare', and
  357. the mapping from this stream state to ALSA/ASoC operation may be
  358. implementation specific.
  359. When the INFO_PAUSE flag is supported, the stream state is linked to
  360. the .hw_free() operation - the stream is not deprepared on a
  361. TRIGGER_STOP.
  362. Other implementations may transition to the ``SDW_STREAM_DEPREPARED``
  363. state on TRIGGER_STOP, should they require a transition through the
  364. ``SDW_STREAM_PREPARED`` state.
  365. .. code-block:: c
  366. int sdw_deprepare_stream(struct sdw_stream_runtime * stream);
  367. SDW_STREAM_RELEASED
  368. ~~~~~~~~~~~~~~~~~~~
  369. Release state of stream. Operations performed before entering in this state:
  370. (1) Release port resources for all Master(s) and Slave(s) port(s)
  371. associated with current stream.
  372. (2) Release Master(s) and Slave(s) runtime resources associated with
  373. current stream.
  374. (3) Release stream runtime resources associated with current stream.
  375. After all above operations are successful, stream state is set to
  376. ``SDW_STREAM_RELEASED``.
  377. Bus implements below APIs for RELEASE state which needs to be called by
  378. all the Master(s) and Slave(s) associated with stream. From ASoC DPCM
  379. framework, this stream state is linked to .hw_free() operation.
  380. .. code-block:: c
  381. int sdw_stream_remove_master(struct sdw_bus * bus,
  382. struct sdw_stream_runtime * stream);
  383. int sdw_stream_remove_slave(struct sdw_slave * slave,
  384. struct sdw_stream_runtime * stream);
  385. The .shutdown() ASoC DPCM operation calls below Bus API to release
  386. stream assigned as part of ALLOCATED state.
  387. In .shutdown() the data structure maintaining stream state are freed up.
  388. .. code-block:: c
  389. void sdw_release_stream(struct sdw_stream_runtime * stream);
  390. The SoundWire core provides a sdw_shutdown_stream() helper function,
  391. typically called during a dailink .shutdown() callback, which clears
  392. the stream pointer for all DAIS connected to a stream and releases the
  393. memory allocated for the stream.
  394. Not Supported
  395. =============
  396. 1. A single port with multiple channels supported cannot be used between two
  397. streams or across stream. For example a port with 4 channels cannot be used
  398. to handle 2 independent stereo streams even though it's possible in theory
  399. in SoundWire.