dapm.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. ===================================================
  2. Dynamic Audio Power Management for Portable Devices
  3. ===================================================
  4. Description
  5. ===========
  6. Dynamic Audio Power Management (DAPM) is designed to allow portable
  7. Linux devices to use the minimum amount of power within the audio
  8. subsystem at all times. It is independent of other kernel power
  9. management frameworks and, as such, can easily co-exist with them.
  10. DAPM is also completely transparent to all user space applications as
  11. all power switching is done within the ASoC core. No code changes or
  12. recompiling are required for user space applications. DAPM makes power
  13. switching decisions based upon any audio stream (capture/playback)
  14. activity and audio mixer settings within the device.
  15. DAPM is based on two basic elements, called widgets and routes:
  16. * a **widget** is every part of the audio hardware that can be enabled by
  17. software when in use and disabled to save power when not in use
  18. * a **route** is an interconnection between widgets that exists when sound
  19. can flow from one widget to the other
  20. All DAPM power switching decisions are made automatically by consulting an
  21. audio routing graph. This graph is specific to each sound card and spans
  22. the whole sound card, so some DAPM routes connect two widgets belonging to
  23. different components (e.g. the LINE OUT pin of a CODEC and the input pin of
  24. an amplifier).
  25. The graph for the STM32MP1-DK1 sound card is shown in picture:
  26. .. kernel-figure:: dapm-graph.svg
  27. :alt: Example DAPM graph
  28. :align: center
  29. You can also generate compatible graph for your sound card using
  30. `tools/sound/dapm-graph` utility.
  31. DAPM power domains
  32. ==================
  33. There are 4 power domains within DAPM:
  34. Codec bias domain
  35. VREF, VMID (core codec and audio power)
  36. Usually controlled at codec probe/remove and suspend/resume, although
  37. can be set at stream time if power is not needed for sidetone, etc.
  38. Platform/Machine domain
  39. physically connected inputs and outputs
  40. Is platform/machine and user action specific, is configured by the
  41. machine driver and responds to asynchronous events e.g when HP
  42. are inserted
  43. Path domain
  44. audio subsystem signal paths
  45. Automatically set when mixer and mux settings are changed by the user.
  46. e.g. alsamixer, amixer.
  47. Stream domain
  48. DACs and ADCs.
  49. Enabled and disabled when stream playback/capture is started and
  50. stopped respectively. e.g. aplay, arecord.
  51. DAPM Widgets
  52. ============
  53. Audio DAPM widgets fall into a number of types:
  54. Mixer
  55. Mixes several analog signals into a single analog signal.
  56. Mux
  57. An analog switch that outputs only one of many inputs.
  58. PGA
  59. A programmable gain amplifier or attenuation widget.
  60. ADC
  61. Analog to Digital Converter
  62. DAC
  63. Digital to Analog Converter
  64. Switch
  65. An analog switch
  66. Input
  67. A codec input pin
  68. Output
  69. A codec output pin
  70. Headphone
  71. Headphone (and optional Jack)
  72. Mic
  73. Mic (and optional Jack)
  74. Line
  75. Line Input/Output (and optional Jack)
  76. Speaker
  77. Speaker
  78. Supply
  79. Power or clock supply widget used by other widgets.
  80. Regulator
  81. External regulator that supplies power to audio components.
  82. Clock
  83. External clock that supplies clock to audio components.
  84. AIF IN
  85. Audio Interface Input (with TDM slot mask).
  86. AIF OUT
  87. Audio Interface Output (with TDM slot mask).
  88. Siggen
  89. Signal Generator.
  90. DAI IN
  91. Digital Audio Interface Input.
  92. DAI OUT
  93. Digital Audio Interface Output.
  94. DAI Link
  95. DAI Link between two DAI structures
  96. Pre
  97. Special PRE widget (exec before all others)
  98. Post
  99. Special POST widget (exec after all others)
  100. Buffer
  101. Inter widget audio data buffer within a DSP.
  102. Scheduler
  103. DSP internal scheduler that schedules component/pipeline processing
  104. work.
  105. Effect
  106. Widget that performs an audio processing effect.
  107. SRC
  108. Sample Rate Converter within DSP or CODEC
  109. ASRC
  110. Asynchronous Sample Rate Converter within DSP or CODEC
  111. Encoder
  112. Widget that encodes audio data from one format (usually PCM) to another
  113. usually more compressed format.
  114. Decoder
  115. Widget that decodes audio data from a compressed format to an
  116. uncompressed format like PCM.
  117. (Widgets are defined in include/sound/soc-dapm.h)
  118. Widgets can be added to the sound card by any of the component driver types.
  119. There are convenience macros defined in soc-dapm.h that can be used to quickly
  120. build a list of widgets of the codecs and machines DAPM widgets.
  121. Most widgets have a name, register, shift and invert. Some widgets have extra
  122. parameters for stream name and kcontrols.
  123. Stream Domain Widgets
  124. ---------------------
  125. Stream Widgets relate to the stream power domain and only consist of ADCs
  126. (analog to digital converters), DACs (digital to analog converters),
  127. AIF IN and AIF OUT.
  128. Stream widgets have the following format:
  129. ::
  130. SND_SOC_DAPM_DAC(name, stream name, reg, shift, invert),
  131. SND_SOC_DAPM_AIF_IN(name, stream, slot, reg, shift, invert)
  132. NOTE: the stream name must match the corresponding stream name in your codec
  133. snd_soc_dai_driver.
  134. e.g. stream widgets for HiFi playback and capture
  135. ::
  136. SND_SOC_DAPM_DAC("HiFi DAC", "HiFi Playback", REG, 3, 1),
  137. SND_SOC_DAPM_ADC("HiFi ADC", "HiFi Capture", REG, 2, 1),
  138. e.g. stream widgets for AIF
  139. ::
  140. SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
  141. SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
  142. Path Domain Widgets
  143. -------------------
  144. Path domain widgets have a ability to control or affect the audio signal or
  145. audio paths within the audio subsystem. They have the following form:
  146. ::
  147. SND_SOC_DAPM_PGA(name, reg, shift, invert, controls, num_controls)
  148. Any widget kcontrols can be set using the controls and num_controls members.
  149. e.g. Mixer widget (the kcontrols are declared first)
  150. ::
  151. /* Output Mixer */
  152. static const snd_kcontrol_new_t wm8731_output_mixer_controls[] = {
  153. SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
  154. SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
  155. SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
  156. };
  157. SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1, wm8731_output_mixer_controls,
  158. ARRAY_SIZE(wm8731_output_mixer_controls)),
  159. If you don't want the mixer elements prefixed with the name of the mixer widget,
  160. you can use SND_SOC_DAPM_MIXER_NAMED_CTL instead. the parameters are the same
  161. as for SND_SOC_DAPM_MIXER.
  162. Machine domain Widgets
  163. ----------------------
  164. Machine widgets are different from codec widgets in that they don't have a
  165. codec register bit associated with them. A machine widget is assigned to each
  166. machine audio component (non codec or DSP) that can be independently
  167. powered. e.g.
  168. * Speaker Amp
  169. * Microphone Bias
  170. * Jack connectors
  171. A machine widget can have an optional call back.
  172. e.g. Jack connector widget for an external Mic that enables Mic Bias
  173. when the Mic is inserted::
  174. static int spitz_mic_bias(struct snd_soc_dapm_widget* w, int event)
  175. {
  176. gpio_set_value(SPITZ_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
  177. return 0;
  178. }
  179. SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias),
  180. Codec (BIAS) Domain
  181. -------------------
  182. The codec bias power domain has no widgets and is handled by the codec DAPM
  183. event handler. This handler is called when the codec powerstate is changed wrt
  184. to any stream event or by kernel PM events.
  185. Virtual Widgets
  186. ---------------
  187. Sometimes widgets exist in the codec or machine audio graph that don't have any
  188. corresponding soft power control. In this case it is necessary to create
  189. a virtual widget - a widget with no control bits e.g.
  190. ::
  191. SND_SOC_DAPM_MIXER("AC97 Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  192. This can be used to merge two signal paths together in software.
  193. Registering DAPM controls
  194. =========================
  195. In many cases the DAPM widgets are implemented statically in a ``static
  196. const struct snd_soc_dapm_widget`` array in a codec driver, and simply
  197. declared via the ``dapm_widgets`` and ``num_dapm_widgets`` fields of the
  198. ``struct snd_soc_component_driver``.
  199. Similarly, routes connecting them are implemented statically in a ``static
  200. const struct snd_soc_dapm_route`` array and declared via the
  201. ``dapm_routes`` and ``num_dapm_routes`` fields of the same struct.
  202. With the above declared, the driver registration will take care of
  203. populating them::
  204. static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = {
  205. SND_SOC_DAPM_OUTPUT("SPKN"),
  206. SND_SOC_DAPM_OUTPUT("SPKP"),
  207. ...
  208. };
  209. /* Target, Path, Source */
  210. static const struct snd_soc_dapm_route wm2000_audio_map[] = {
  211. { "SPKN", NULL, "ANC Engine" },
  212. { "SPKP", NULL, "ANC Engine" },
  213. ...
  214. };
  215. static const struct snd_soc_component_driver soc_component_dev_wm2000 = {
  216. ...
  217. .dapm_widgets = wm2000_dapm_widgets,
  218. .num_dapm_widgets = ARRAY_SIZE(wm2000_dapm_widgets),
  219. .dapm_routes = wm2000_audio_map,
  220. .num_dapm_routes = ARRAY_SIZE(wm2000_audio_map),
  221. ...
  222. };
  223. In more complex cases the list of DAPM widgets and/or routes can be only
  224. known at probe time. This happens for example when a driver supports
  225. different models having a different set of features. In those cases
  226. separate widgets and routes arrays implementing the case-specific features
  227. can be registered programmatically by calling snd_soc_dapm_new_controls()
  228. and snd_soc_dapm_add_routes().
  229. Codec/DSP Widget Interconnections
  230. =================================
  231. Widgets are connected to each other within the codec, platform and machine by
  232. audio paths (called interconnections). Each interconnection must be defined in
  233. order to create a graph of all audio paths between widgets.
  234. This is easiest with a diagram of the codec or DSP (and schematic of the machine
  235. audio system), as it requires joining widgets together via their audio signal
  236. paths.
  237. For example the WM8731 output mixer (wm8731.c) has 3 inputs (sources):
  238. 1. Line Bypass Input
  239. 2. DAC (HiFi playback)
  240. 3. Mic Sidetone Input
  241. Each input in this example has a kcontrol associated with it (defined in
  242. the example above) and is connected to the output mixer via its kcontrol
  243. name. We can now connect the destination widget (wrt audio signal) with its
  244. source widgets. ::
  245. /* output mixer */
  246. {"Output Mixer", "Line Bypass Switch", "Line Input"},
  247. {"Output Mixer", "HiFi Playback Switch", "DAC"},
  248. {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
  249. So we have:
  250. * Destination Widget <=== Path Name <=== Source Widget, or
  251. * Sink, Path, Source, or
  252. * ``Output Mixer`` is connected to the ``DAC`` via the ``HiFi Playback Switch``.
  253. When there is no path name connecting widgets (e.g. a direct connection) we
  254. pass NULL for the path name.
  255. Interconnections are created with a call to::
  256. snd_soc_dapm_connect_input(codec, sink, path, source);
  257. Finally, snd_soc_dapm_new_widgets() must be called after all widgets and
  258. interconnections have been registered with the core. This causes the core to
  259. scan the codec and machine so that the internal DAPM state matches the
  260. physical state of the machine.
  261. Machine Widget Interconnections
  262. -------------------------------
  263. Machine widget interconnections are created in the same way as codec ones and
  264. directly connect the codec pins to machine level widgets.
  265. e.g. connects the speaker out codec pins to the internal speaker.
  266. ::
  267. /* ext speaker connected to codec pins LOUT2, ROUT2 */
  268. {"Ext Spk", NULL , "ROUT2"},
  269. {"Ext Spk", NULL , "LOUT2"},
  270. This allows the DAPM to power on and off pins that are connected (and in use)
  271. and pins that are NC respectively.
  272. Endpoint Widgets
  273. ================
  274. An endpoint is a start or end point (widget) of an audio signal within the
  275. machine and includes the codec. e.g.
  276. * Headphone Jack
  277. * Internal Speaker
  278. * Internal Mic
  279. * Mic Jack
  280. * Codec Pins
  281. Endpoints are added to the DAPM graph so that their usage can be determined in
  282. order to save power. e.g. NC codecs pins will be switched OFF, unconnected
  283. jacks can also be switched OFF.
  284. DAPM Widget Events
  285. ==================
  286. Widgets needing to implement a more complex behaviour than what DAPM can do
  287. can set a custom "event handler" by setting a function pointer. An example
  288. is a power supply needing to enable a GPIO::
  289. static int sof_es8316_speaker_power_event(struct snd_soc_dapm_widget *w,
  290. struct snd_kcontrol *kcontrol, int event)
  291. {
  292. if (SND_SOC_DAPM_EVENT_ON(event))
  293. gpiod_set_value_cansleep(gpio_pa, true);
  294. else
  295. gpiod_set_value_cansleep(gpio_pa, false);
  296. return 0;
  297. }
  298. static const struct snd_soc_dapm_widget st_widgets[] = {
  299. ...
  300. SND_SOC_DAPM_SUPPLY("Speaker Power", SND_SOC_NOPM, 0, 0,
  301. sof_es8316_speaker_power_event,
  302. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
  303. };
  304. See soc-dapm.h for all other widgets that support events.
  305. Event types
  306. -----------
  307. The following event types are supported by event widgets::
  308. /* dapm event types */
  309. #define SND_SOC_DAPM_PRE_PMU 0x1 /* before widget power up */
  310. #define SND_SOC_DAPM_POST_PMU 0x2 /* after widget power up */
  311. #define SND_SOC_DAPM_PRE_PMD 0x4 /* before widget power down */
  312. #define SND_SOC_DAPM_POST_PMD 0x8 /* after widget power down */
  313. #define SND_SOC_DAPM_PRE_REG 0x10 /* before audio path setup */
  314. #define SND_SOC_DAPM_POST_REG 0x20 /* after audio path setup */
  315. #define SND_SOC_DAPM_WILL_PMU 0x40 /* called at start of sequence */
  316. #define SND_SOC_DAPM_WILL_PMD 0x80 /* called at start of sequence */
  317. #define SND_SOC_DAPM_PRE_POST_PMD (SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD)
  318. #define SND_SOC_DAPM_PRE_POST_PMU (SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU)