dpcm.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. ===========
  2. Dynamic PCM
  3. ===========
  4. Description
  5. ===========
  6. Dynamic PCM allows an ALSA PCM device to digitally route its PCM audio to
  7. various digital endpoints during the PCM stream runtime. e.g. PCM0 can route
  8. digital audio to I2S DAI0, I2S DAI1 or PDM DAI2. This is useful for on SoC DSP
  9. drivers that expose several ALSA PCMs and can route to multiple DAIs.
  10. The DPCM runtime routing is determined by the ALSA mixer settings in the same
  11. way as the analog signal is routed in an ASoC codec driver. DPCM uses a DAPM
  12. graph representing the DSP internal audio paths and uses the mixer settings to
  13. determine the path used by each ALSA PCM.
  14. DPCM re-uses all the existing component codec, platform and DAI drivers without
  15. any modifications.
  16. Phone Audio System with SoC based DSP
  17. -------------------------------------
  18. Consider the following phone audio subsystem. This will be used in this
  19. document for all examples :-
  20. ::
  21. | Front End PCMs | SoC DSP | Back End DAIs | Audio devices |
  22. *************
  23. PCM0 <------------> * * <----DAI0-----> Codec Headset
  24. * *
  25. PCM1 <------------> * * <----DAI1-----> Codec Speakers
  26. * DSP *
  27. PCM2 <------------> * * <----DAI2-----> MODEM
  28. * *
  29. PCM3 <------------> * * <----DAI3-----> BT
  30. * *
  31. * * <----DAI4-----> DMIC
  32. * *
  33. * * <----DAI5-----> FM
  34. *************
  35. This diagram shows a simple smart phone audio subsystem. It supports Bluetooth,
  36. FM digital radio, Speakers, Headset Jack, digital microphones and cellular
  37. modem. This sound card exposes 4 DSP front end (FE) ALSA PCM devices and
  38. supports 6 back end (BE) DAIs. Each FE PCM can digitally route audio data to any
  39. of the BE DAIs. The FE PCM devices can also route audio to more than 1 BE DAI.
  40. Example - DPCM Switching playback from DAI0 to DAI1
  41. ---------------------------------------------------
  42. Audio is being played to the Headset. After a while the user removes the headset
  43. and audio continues playing on the speakers.
  44. Playback on PCM0 to Headset would look like :-
  45. ::
  46. *************
  47. PCM0 <============> * * <====DAI0=====> Codec Headset
  48. * *
  49. PCM1 <------------> * * <----DAI1-----> Codec Speakers
  50. * DSP *
  51. PCM2 <------------> * * <----DAI2-----> MODEM
  52. * *
  53. PCM3 <------------> * * <----DAI3-----> BT
  54. * *
  55. * * <----DAI4-----> DMIC
  56. * *
  57. * * <----DAI5-----> FM
  58. *************
  59. The headset is removed from the jack by user so the speakers must now be used :-
  60. ::
  61. *************
  62. PCM0 <============> * * <----DAI0-----> Codec Headset
  63. * *
  64. PCM1 <------------> * * <====DAI1=====> Codec Speakers
  65. * DSP *
  66. PCM2 <------------> * * <----DAI2-----> MODEM
  67. * *
  68. PCM3 <------------> * * <----DAI3-----> BT
  69. * *
  70. * * <----DAI4-----> DMIC
  71. * *
  72. * * <----DAI5-----> FM
  73. *************
  74. The audio driver processes this as follows :-
  75. 1. Machine driver receives Jack removal event.
  76. 2. Machine driver OR audio HAL disables the Headset path.
  77. 3. DPCM runs the PCM trigger(stop), hw_free(), shutdown() operations on DAI0
  78. for headset since the path is now disabled.
  79. 4. Machine driver or audio HAL enables the speaker path.
  80. 5. DPCM runs the PCM ops for startup(), hw_params(), prepare() and
  81. trigger(start) for DAI1 Speakers since the path is enabled.
  82. In this example, the machine driver or userspace audio HAL can alter the routing
  83. and then DPCM will take care of managing the DAI PCM operations to either bring
  84. the link up or down. Audio playback does not stop during this transition.
  85. DPCM machine driver
  86. ===================
  87. The DPCM enabled ASoC machine driver is similar to normal machine drivers
  88. except that we also have to :-
  89. 1. Define the FE and BE DAI links.
  90. 2. Define any FE/BE PCM operations.
  91. 3. Define widget graph connections.
  92. FE and BE DAI links
  93. -------------------
  94. ::
  95. | Front End PCMs | SoC DSP | Back End DAIs | Audio devices |
  96. *************
  97. PCM0 <------------> * * <----DAI0-----> Codec Headset
  98. * *
  99. PCM1 <------------> * * <----DAI1-----> Codec Speakers
  100. * DSP *
  101. PCM2 <------------> * * <----DAI2-----> MODEM
  102. * *
  103. PCM3 <------------> * * <----DAI3-----> BT
  104. * *
  105. * * <----DAI4-----> DMIC
  106. * *
  107. * * <----DAI5-----> FM
  108. *************
  109. For the example above we have to define 4 FE DAI links and 6 BE DAI links. The
  110. FE DAI links are defined as follows :-
  111. ::
  112. static struct snd_soc_dai_link machine_dais[] = {
  113. {
  114. .name = "PCM0 System",
  115. .stream_name = "System Playback",
  116. .cpu_dai_name = "System Pin",
  117. .platform_name = "dsp-audio",
  118. .codec_name = "snd-soc-dummy",
  119. .codec_dai_name = "snd-soc-dummy-dai",
  120. .dynamic = 1,
  121. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  122. .dpcm_playback = 1,
  123. },
  124. .....< other FE and BE DAI links here >
  125. };
  126. This FE DAI link is pretty similar to a regular DAI link except that we also
  127. set the DAI link to a DPCM FE with the ``dynamic = 1``. The supported FE stream
  128. directions should also be set with the ``dpcm_playback`` and ``dpcm_capture``
  129. flags. There is also an option to specify the ordering of the trigger call for
  130. each FE. This allows the ASoC core to trigger the DSP before or after the other
  131. components (as some DSPs have strong requirements for the ordering DAI/DSP
  132. start and stop sequences).
  133. The FE DAI above sets the codec and code DAIs to dummy devices since the BE is
  134. dynamic and will change depending on runtime config.
  135. The BE DAIs are configured as follows :-
  136. ::
  137. static struct snd_soc_dai_link machine_dais[] = {
  138. .....< FE DAI links here >
  139. {
  140. .name = "Codec Headset",
  141. .cpu_dai_name = "ssp-dai.0",
  142. .platform_name = "snd-soc-dummy",
  143. .no_pcm = 1,
  144. .codec_name = "rt5640.0-001c",
  145. .codec_dai_name = "rt5640-aif1",
  146. .ignore_suspend = 1,
  147. .ignore_pmdown_time = 1,
  148. .be_hw_params_fixup = hswult_ssp0_fixup,
  149. .ops = &haswell_ops,
  150. .dpcm_playback = 1,
  151. .dpcm_capture = 1,
  152. },
  153. .....< other BE DAI links here >
  154. };
  155. This BE DAI link connects DAI0 to the codec (in this case RT5460 AIF1). It sets
  156. the ``no_pcm`` flag to mark it has a BE and sets flags for supported stream
  157. directions using ``dpcm_playback`` and ``dpcm_capture`` above.
  158. The BE has also flags set for ignoring suspend and PM down time. This allows
  159. the BE to work in a hostless mode where the host CPU is not transferring data
  160. like a BT phone call :-
  161. ::
  162. *************
  163. PCM0 <------------> * * <----DAI0-----> Codec Headset
  164. * *
  165. PCM1 <------------> * * <----DAI1-----> Codec Speakers
  166. * DSP *
  167. PCM2 <------------> * * <====DAI2=====> MODEM
  168. * *
  169. PCM3 <------------> * * <====DAI3=====> BT
  170. * *
  171. * * <----DAI4-----> DMIC
  172. * *
  173. * * <----DAI5-----> FM
  174. *************
  175. This allows the host CPU to sleep while the DSP, MODEM DAI and the BT DAI are
  176. still in operation.
  177. A BE DAI link can also set the codec to a dummy device if the codec is a device
  178. that is managed externally.
  179. Likewise a BE DAI can also set a dummy cpu DAI if the CPU DAI is managed by the
  180. DSP firmware.
  181. FE/BE PCM operations
  182. --------------------
  183. The BE above also exports some PCM operations and a ``fixup`` callback. The fixup
  184. callback is used by the machine driver to (re)configure the DAI based upon the
  185. FE hw params. i.e. the DSP may perform SRC or ASRC from the FE to BE.
  186. e.g. DSP converts all FE hw params to run at fixed rate of 48k, 16bit, stereo for
  187. DAI0. This means all FE hw_params have to be fixed in the machine driver for
  188. DAI0 so that the DAI is running at desired configuration regardless of the FE
  189. configuration.
  190. ::
  191. static int dai0_fixup(struct snd_soc_pcm_runtime *rtd,
  192. struct snd_pcm_hw_params *params)
  193. {
  194. struct snd_interval *rate = hw_param_interval(params,
  195. SNDRV_PCM_HW_PARAM_RATE);
  196. struct snd_interval *channels = hw_param_interval(params,
  197. SNDRV_PCM_HW_PARAM_CHANNELS);
  198. /* The DSP will convert the FE rate to 48k, stereo */
  199. rate->min = rate->max = 48000;
  200. channels->min = channels->max = 2;
  201. /* set DAI0 to 16 bit */
  202. params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
  203. return 0;
  204. }
  205. The other PCM operation are the same as for regular DAI links. Use as necessary.
  206. Widget graph connections
  207. ------------------------
  208. The BE DAI links will normally be connected to the graph at initialisation time
  209. by the ASoC DAPM core. However, if the BE codec or BE DAI is a dummy then this
  210. has to be set explicitly in the driver :-
  211. ::
  212. /* BE for codec Headset - DAI0 is dummy and managed by DSP FW */
  213. {"DAI0 CODEC IN", NULL, "AIF1 Capture"},
  214. {"AIF1 Playback", NULL, "DAI0 CODEC OUT"},
  215. Writing a DPCM DSP driver
  216. =========================
  217. The DPCM DSP driver looks much like a standard platform class ASoC driver
  218. combined with elements from a codec class driver. A DSP platform driver must
  219. implement :-
  220. 1. Front End PCM DAIs - i.e. struct snd_soc_dai_driver.
  221. 2. DAPM graph showing DSP audio routing from FE DAIs to BEs.
  222. 3. DAPM widgets from DSP graph.
  223. 4. Mixers for gains, routing, etc.
  224. 5. DMA configuration.
  225. 6. BE AIF widgets.
  226. Items 6 is important for routing the audio outside of the DSP. AIF need to be
  227. defined for each BE and each stream direction. e.g for BE DAI0 above we would
  228. have :-
  229. ::
  230. SND_SOC_DAPM_AIF_IN("DAI0 RX", NULL, 0, SND_SOC_NOPM, 0, 0),
  231. SND_SOC_DAPM_AIF_OUT("DAI0 TX", NULL, 0, SND_SOC_NOPM, 0, 0),
  232. The BE AIF are used to connect the DSP graph to the graphs for the other
  233. component drivers (e.g. codec graph).
  234. Hostless PCM streams
  235. ====================
  236. A hostless PCM stream is a stream that is not routed through the host CPU. An
  237. example of this would be a phone call from handset to modem.
  238. ::
  239. *************
  240. PCM0 <------------> * * <----DAI0-----> Codec Headset
  241. * *
  242. PCM1 <------------> * * <====DAI1=====> Codec Speakers/Mic
  243. * DSP *
  244. PCM2 <------------> * * <====DAI2=====> MODEM
  245. * *
  246. PCM3 <------------> * * <----DAI3-----> BT
  247. * *
  248. * * <----DAI4-----> DMIC
  249. * *
  250. * * <----DAI5-----> FM
  251. *************
  252. In this case the PCM data is routed via the DSP. The host CPU in this use case
  253. is only used for control and can sleep during the runtime of the stream.
  254. The host can control the hostless link either by :-
  255. 1. Configuring the link as a CODEC <-> CODEC style link. In this case the link
  256. is enabled or disabled by the state of the DAPM graph. This usually means
  257. there is a mixer control that can be used to connect or disconnect the path
  258. between both DAIs.
  259. 2. Hostless FE. This FE has a virtual connection to the BE DAI links on the DAPM
  260. graph. Control is then carried out by the FE as regular PCM operations.
  261. This method gives more control over the DAI links, but requires much more
  262. userspace code to control the link. Its recommended to use CODEC<->CODEC
  263. unless your HW needs more fine grained sequencing of the PCM ops.
  264. CODEC <-> CODEC link
  265. --------------------
  266. This DAI link is enabled when DAPM detects a valid path within the DAPM graph.
  267. The machine driver sets some additional parameters to the DAI link i.e.
  268. ::
  269. static const struct snd_soc_pcm_stream dai_params = {
  270. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  271. .rate_min = 8000,
  272. .rate_max = 8000,
  273. .channels_min = 2,
  274. .channels_max = 2,
  275. };
  276. static struct snd_soc_dai_link dais[] = {
  277. < ... more DAI links above ... >
  278. {
  279. .name = "MODEM",
  280. .stream_name = "MODEM",
  281. .cpu_dai_name = "dai2",
  282. .codec_dai_name = "modem-aif1",
  283. .codec_name = "modem",
  284. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  285. | SND_SOC_DAIFMT_CBM_CFM,
  286. .params = &dai_params,
  287. }
  288. < ... more DAI links here ... >
  289. These parameters are used to configure the DAI hw_params() when DAPM detects a
  290. valid path and then calls the PCM operations to start the link. DAPM will also
  291. call the appropriate PCM operations to disable the DAI when the path is no
  292. longer valid.
  293. Hostless FE
  294. -----------
  295. The DAI link(s) are enabled by a FE that does not read or write any PCM data.
  296. This means creating a new FE that is connected with a virtual path to both
  297. DAI links. The DAI links will be started when the FE PCM is started and stopped
  298. when the FE PCM is stopped. Note that the FE PCM cannot read or write data in
  299. this configuration.