0001 ===========
0002 Dynamic PCM
0003 ===========
0004
0005 Description
0006 ===========
0007
0008 Dynamic PCM allows an ALSA PCM device to digitally route its PCM audio to
0009 various digital endpoints during the PCM stream runtime. e.g. PCM0 can route
0010 digital audio to I2S DAI0, I2S DAI1 or PDM DAI2. This is useful for on SoC DSP
0011 drivers that expose several ALSA PCMs and can route to multiple DAIs.
0012
0013 The DPCM runtime routing is determined by the ALSA mixer settings in the same
0014 way as the analog signal is routed in an ASoC codec driver. DPCM uses a DAPM
0015 graph representing the DSP internal audio paths and uses the mixer settings to
0016 determine the path used by each ALSA PCM.
0017
0018 DPCM re-uses all the existing component codec, platform and DAI drivers without
0019 any modifications.
0020
0021
0022 Phone Audio System with SoC based DSP
0023 -------------------------------------
0024
0025 Consider the following phone audio subsystem. This will be used in this
0026 document for all examples :-
0027 ::
0028
0029 | Front End PCMs | SoC DSP | Back End DAIs | Audio devices |
0030
0031 *************
0032 PCM0 <------------> * * <----DAI0-----> Codec Headset
0033 * *
0034 PCM1 <------------> * * <----DAI1-----> Codec Speakers
0035 * DSP *
0036 PCM2 <------------> * * <----DAI2-----> MODEM
0037 * *
0038 PCM3 <------------> * * <----DAI3-----> BT
0039 * *
0040 * * <----DAI4-----> DMIC
0041 * *
0042 * * <----DAI5-----> FM
0043 *************
0044
0045 This diagram shows a simple smart phone audio subsystem. It supports Bluetooth,
0046 FM digital radio, Speakers, Headset Jack, digital microphones and cellular
0047 modem. This sound card exposes 4 DSP front end (FE) ALSA PCM devices and
0048 supports 6 back end (BE) DAIs. Each FE PCM can digitally route audio data to any
0049 of the BE DAIs. The FE PCM devices can also route audio to more than 1 BE DAI.
0050
0051
0052
0053 Example - DPCM Switching playback from DAI0 to DAI1
0054 ---------------------------------------------------
0055
0056 Audio is being played to the Headset. After a while the user removes the headset
0057 and audio continues playing on the speakers.
0058
0059 Playback on PCM0 to Headset would look like :-
0060 ::
0061
0062 *************
0063 PCM0 <============> * * <====DAI0=====> Codec Headset
0064 * *
0065 PCM1 <------------> * * <----DAI1-----> Codec Speakers
0066 * DSP *
0067 PCM2 <------------> * * <----DAI2-----> MODEM
0068 * *
0069 PCM3 <------------> * * <----DAI3-----> BT
0070 * *
0071 * * <----DAI4-----> DMIC
0072 * *
0073 * * <----DAI5-----> FM
0074 *************
0075
0076 The headset is removed from the jack by user so the speakers must now be used :-
0077 ::
0078
0079 *************
0080 PCM0 <============> * * <----DAI0-----> Codec Headset
0081 * *
0082 PCM1 <------------> * * <====DAI1=====> Codec Speakers
0083 * DSP *
0084 PCM2 <------------> * * <----DAI2-----> MODEM
0085 * *
0086 PCM3 <------------> * * <----DAI3-----> BT
0087 * *
0088 * * <----DAI4-----> DMIC
0089 * *
0090 * * <----DAI5-----> FM
0091 *************
0092
0093 The audio driver processes this as follows :-
0094
0095 1. Machine driver receives Jack removal event.
0096
0097 2. Machine driver OR audio HAL disables the Headset path.
0098
0099 3. DPCM runs the PCM trigger(stop), hw_free(), shutdown() operations on DAI0
0100 for headset since the path is now disabled.
0101
0102 4. Machine driver or audio HAL enables the speaker path.
0103
0104 5. DPCM runs the PCM ops for startup(), hw_params(), prepare() and
0105 trigger(start) for DAI1 Speakers since the path is enabled.
0106
0107 In this example, the machine driver or userspace audio HAL can alter the routing
0108 and then DPCM will take care of managing the DAI PCM operations to either bring
0109 the link up or down. Audio playback does not stop during this transition.
0110
0111
0112
0113 DPCM machine driver
0114 ===================
0115
0116 The DPCM enabled ASoC machine driver is similar to normal machine drivers
0117 except that we also have to :-
0118
0119 1. Define the FE and BE DAI links.
0120
0121 2. Define any FE/BE PCM operations.
0122
0123 3. Define widget graph connections.
0124
0125
0126 FE and BE DAI links
0127 -------------------
0128 ::
0129
0130 | Front End PCMs | SoC DSP | Back End DAIs | Audio devices |
0131
0132 *************
0133 PCM0 <------------> * * <----DAI0-----> Codec Headset
0134 * *
0135 PCM1 <------------> * * <----DAI1-----> Codec Speakers
0136 * DSP *
0137 PCM2 <------------> * * <----DAI2-----> MODEM
0138 * *
0139 PCM3 <------------> * * <----DAI3-----> BT
0140 * *
0141 * * <----DAI4-----> DMIC
0142 * *
0143 * * <----DAI5-----> FM
0144 *************
0145
0146 For the example above we have to define 4 FE DAI links and 6 BE DAI links. The
0147 FE DAI links are defined as follows :-
0148 ::
0149
0150 static struct snd_soc_dai_link machine_dais[] = {
0151 {
0152 .name = "PCM0 System",
0153 .stream_name = "System Playback",
0154 .cpu_dai_name = "System Pin",
0155 .platform_name = "dsp-audio",
0156 .codec_name = "snd-soc-dummy",
0157 .codec_dai_name = "snd-soc-dummy-dai",
0158 .dynamic = 1,
0159 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
0160 .dpcm_playback = 1,
0161 },
0162 .....< other FE and BE DAI links here >
0163 };
0164
0165 This FE DAI link is pretty similar to a regular DAI link except that we also
0166 set the DAI link to a DPCM FE with the ``dynamic = 1``. The supported FE stream
0167 directions should also be set with the ``dpcm_playback`` and ``dpcm_capture``
0168 flags. There is also an option to specify the ordering of the trigger call for
0169 each FE. This allows the ASoC core to trigger the DSP before or after the other
0170 components (as some DSPs have strong requirements for the ordering DAI/DSP
0171 start and stop sequences).
0172
0173 The FE DAI above sets the codec and code DAIs to dummy devices since the BE is
0174 dynamic and will change depending on runtime config.
0175
0176 The BE DAIs are configured as follows :-
0177 ::
0178
0179 static struct snd_soc_dai_link machine_dais[] = {
0180 .....< FE DAI links here >
0181 {
0182 .name = "Codec Headset",
0183 .cpu_dai_name = "ssp-dai.0",
0184 .platform_name = "snd-soc-dummy",
0185 .no_pcm = 1,
0186 .codec_name = "rt5640.0-001c",
0187 .codec_dai_name = "rt5640-aif1",
0188 .ignore_suspend = 1,
0189 .ignore_pmdown_time = 1,
0190 .be_hw_params_fixup = hswult_ssp0_fixup,
0191 .ops = &haswell_ops,
0192 .dpcm_playback = 1,
0193 .dpcm_capture = 1,
0194 },
0195 .....< other BE DAI links here >
0196 };
0197
0198 This BE DAI link connects DAI0 to the codec (in this case RT5460 AIF1). It sets
0199 the ``no_pcm`` flag to mark it has a BE and sets flags for supported stream
0200 directions using ``dpcm_playback`` and ``dpcm_capture`` above.
0201
0202 The BE has also flags set for ignoring suspend and PM down time. This allows
0203 the BE to work in a hostless mode where the host CPU is not transferring data
0204 like a BT phone call :-
0205 ::
0206
0207 *************
0208 PCM0 <------------> * * <----DAI0-----> Codec Headset
0209 * *
0210 PCM1 <------------> * * <----DAI1-----> Codec Speakers
0211 * DSP *
0212 PCM2 <------------> * * <====DAI2=====> MODEM
0213 * *
0214 PCM3 <------------> * * <====DAI3=====> BT
0215 * *
0216 * * <----DAI4-----> DMIC
0217 * *
0218 * * <----DAI5-----> FM
0219 *************
0220
0221 This allows the host CPU to sleep while the DSP, MODEM DAI and the BT DAI are
0222 still in operation.
0223
0224 A BE DAI link can also set the codec to a dummy device if the codec is a device
0225 that is managed externally.
0226
0227 Likewise a BE DAI can also set a dummy cpu DAI if the CPU DAI is managed by the
0228 DSP firmware.
0229
0230
0231 FE/BE PCM operations
0232 --------------------
0233
0234 The BE above also exports some PCM operations and a ``fixup`` callback. The fixup
0235 callback is used by the machine driver to (re)configure the DAI based upon the
0236 FE hw params. i.e. the DSP may perform SRC or ASRC from the FE to BE.
0237
0238 e.g. DSP converts all FE hw params to run at fixed rate of 48k, 16bit, stereo for
0239 DAI0. This means all FE hw_params have to be fixed in the machine driver for
0240 DAI0 so that the DAI is running at desired configuration regardless of the FE
0241 configuration.
0242 ::
0243
0244 static int dai0_fixup(struct snd_soc_pcm_runtime *rtd,
0245 struct snd_pcm_hw_params *params)
0246 {
0247 struct snd_interval *rate = hw_param_interval(params,
0248 SNDRV_PCM_HW_PARAM_RATE);
0249 struct snd_interval *channels = hw_param_interval(params,
0250 SNDRV_PCM_HW_PARAM_CHANNELS);
0251
0252 /* The DSP will convert the FE rate to 48k, stereo */
0253 rate->min = rate->max = 48000;
0254 channels->min = channels->max = 2;
0255
0256 /* set DAI0 to 16 bit */
0257 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
0258 return 0;
0259 }
0260
0261 The other PCM operation are the same as for regular DAI links. Use as necessary.
0262
0263
0264 Widget graph connections
0265 ------------------------
0266
0267 The BE DAI links will normally be connected to the graph at initialisation time
0268 by the ASoC DAPM core. However, if the BE codec or BE DAI is a dummy then this
0269 has to be set explicitly in the driver :-
0270 ::
0271
0272 /* BE for codec Headset - DAI0 is dummy and managed by DSP FW */
0273 {"DAI0 CODEC IN", NULL, "AIF1 Capture"},
0274 {"AIF1 Playback", NULL, "DAI0 CODEC OUT"},
0275
0276
0277 Writing a DPCM DSP driver
0278 =========================
0279
0280 The DPCM DSP driver looks much like a standard platform class ASoC driver
0281 combined with elements from a codec class driver. A DSP platform driver must
0282 implement :-
0283
0284 1. Front End PCM DAIs - i.e. struct snd_soc_dai_driver.
0285
0286 2. DAPM graph showing DSP audio routing from FE DAIs to BEs.
0287
0288 3. DAPM widgets from DSP graph.
0289
0290 4. Mixers for gains, routing, etc.
0291
0292 5. DMA configuration.
0293
0294 6. BE AIF widgets.
0295
0296 Items 6 is important for routing the audio outside of the DSP. AIF need to be
0297 defined for each BE and each stream direction. e.g for BE DAI0 above we would
0298 have :-
0299 ::
0300
0301 SND_SOC_DAPM_AIF_IN("DAI0 RX", NULL, 0, SND_SOC_NOPM, 0, 0),
0302 SND_SOC_DAPM_AIF_OUT("DAI0 TX", NULL, 0, SND_SOC_NOPM, 0, 0),
0303
0304 The BE AIF are used to connect the DSP graph to the graphs for the other
0305 component drivers (e.g. codec graph).
0306
0307
0308 Hostless PCM streams
0309 ====================
0310
0311 A hostless PCM stream is a stream that is not routed through the host CPU. An
0312 example of this would be a phone call from handset to modem.
0313 ::
0314
0315 *************
0316 PCM0 <------------> * * <----DAI0-----> Codec Headset
0317 * *
0318 PCM1 <------------> * * <====DAI1=====> Codec Speakers/Mic
0319 * DSP *
0320 PCM2 <------------> * * <====DAI2=====> MODEM
0321 * *
0322 PCM3 <------------> * * <----DAI3-----> BT
0323 * *
0324 * * <----DAI4-----> DMIC
0325 * *
0326 * * <----DAI5-----> FM
0327 *************
0328
0329 In this case the PCM data is routed via the DSP. The host CPU in this use case
0330 is only used for control and can sleep during the runtime of the stream.
0331
0332 The host can control the hostless link either by :-
0333
0334 1. Configuring the link as a CODEC <-> CODEC style link. In this case the link
0335 is enabled or disabled by the state of the DAPM graph. This usually means
0336 there is a mixer control that can be used to connect or disconnect the path
0337 between both DAIs.
0338
0339 2. Hostless FE. This FE has a virtual connection to the BE DAI links on the DAPM
0340 graph. Control is then carried out by the FE as regular PCM operations.
0341 This method gives more control over the DAI links, but requires much more
0342 userspace code to control the link. Its recommended to use CODEC<->CODEC
0343 unless your HW needs more fine grained sequencing of the PCM ops.
0344
0345
0346 CODEC <-> CODEC link
0347 --------------------
0348
0349 This DAI link is enabled when DAPM detects a valid path within the DAPM graph.
0350 The machine driver sets some additional parameters to the DAI link i.e.
0351 ::
0352
0353 static const struct snd_soc_pcm_stream dai_params = {
0354 .formats = SNDRV_PCM_FMTBIT_S32_LE,
0355 .rate_min = 8000,
0356 .rate_max = 8000,
0357 .channels_min = 2,
0358 .channels_max = 2,
0359 };
0360
0361 static struct snd_soc_dai_link dais[] = {
0362 < ... more DAI links above ... >
0363 {
0364 .name = "MODEM",
0365 .stream_name = "MODEM",
0366 .cpu_dai_name = "dai2",
0367 .codec_dai_name = "modem-aif1",
0368 .codec_name = "modem",
0369 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
0370 | SND_SOC_DAIFMT_CBM_CFM,
0371 .params = &dai_params,
0372 }
0373 < ... more DAI links here ... >
0374
0375 These parameters are used to configure the DAI hw_params() when DAPM detects a
0376 valid path and then calls the PCM operations to start the link. DAPM will also
0377 call the appropriate PCM operations to disable the DAI when the path is no
0378 longer valid.
0379
0380
0381 Hostless FE
0382 -----------
0383
0384 The DAI link(s) are enabled by a FE that does not read or write any PCM data.
0385 This means creating a new FE that is connected with a virtual path to both
0386 DAI links. The DAI links will be started when the FE PCM is started and stopped
0387 when the FE PCM is stopped. Note that the FE PCM cannot read or write data in
0388 this configuration.