dapm.rst 13 KB

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