1. 程式人生 > >DPCM (Dynamic PCM)系列之一:概述

DPCM (Dynamic PCM)系列之一:概述

http://blog.csdn.net/azloong/article/details/50162241

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