dapm.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. Dynamic Audio Power Management for Portable Devices
  2. ===================================================
  3. 1. Description
  4. ==============
  5. Dynamic Audio Power Management (DAPM) is designed to allow portable
  6. Linux devices to use the minimum amount of power within the audio
  7. subsystem at all times. It is independent of other kernel PM and as
  8. such, can easily co-exist with the other PM systems.
  9. DAPM is also completely transparent to all user space applications as
  10. all power switching is done within the ASoC core. No code changes or
  11. recompiling are required for user space applications. DAPM makes power
  12. switching decisions based upon any audio stream (capture/playback)
  13. activity and audio mixer settings within the device.
  14. DAPM spans the whole machine. It covers power control within the entire
  15. audio subsystem, this includes internal codec power blocks and machine
  16. level power systems.
  17. There are 4 power domains within DAPM
  18. 1. Codec bias domain - VREF, VMID (core codec and audio power)
  19. Usually controlled at codec probe/remove and suspend/resume, although
  20. can be set at stream time if power is not needed for sidetone, etc.
  21. 2. Platform/Machine domain - physically connected inputs and outputs
  22. Is platform/machine and user action specific, is configured by the
  23. machine driver and responds to asynchronous events e.g when HP
  24. are inserted
  25. 3. Path domain - audio subsystem signal paths
  26. Automatically set when mixer and mux settings are changed by the user.
  27. e.g. alsamixer, amixer.
  28. 4. Stream domain - DACs and ADCs.
  29. Enabled and disabled when stream playback/capture is started and
  30. stopped respectively. e.g. aplay, arecord.
  31. All DAPM power switching decisions are made automatically by consulting an audio
  32. routing map of the whole machine. This map is specific to each machine and
  33. consists of the interconnections between every audio component (including
  34. internal codec components). All audio components that effect power are called
  35. widgets hereafter.
  36. 2. DAPM Widgets
  37. ===============
  38. Audio DAPM widgets fall into a number of types:-
  39. o Mixer - Mixes several analog signals into a single analog signal.
  40. o Mux - An analog switch that outputs only one of many inputs.
  41. o PGA - A programmable gain amplifier or attenuation widget.
  42. o ADC - Analog to Digital Converter
  43. o DAC - Digital to Analog Converter
  44. o Switch - An analog switch
  45. o Input - A codec input pin
  46. o Output - A codec output pin
  47. o Headphone - Headphone (and optional Jack)
  48. o Mic - Mic (and optional Jack)
  49. o Line - Line Input/Output (and optional Jack)
  50. o Speaker - Speaker
  51. o Supply - Power or clock supply widget used by other widgets.
  52. o Regulator - External regulator that supplies power to audio components.
  53. o Clock - External clock that supplies clock to audio components.
  54. o AIF IN - Audio Interface Input (with TDM slot mask).
  55. o AIF OUT - Audio Interface Output (with TDM slot mask).
  56. o Siggen - Signal Generator.
  57. o DAI IN - Digital Audio Interface Input.
  58. o DAI OUT - Digital Audio Interface Output.
  59. o DAI Link - DAI Link between two DAI structures */
  60. o Pre - Special PRE widget (exec before all others)
  61. o Post - Special POST widget (exec after all others)
  62. (Widgets are defined in include/sound/soc-dapm.h)
  63. Widgets can be added to the sound card by any of the component driver types.
  64. There are convenience macros defined in soc-dapm.h that can be used to quickly
  65. build a list of widgets of the codecs and machines DAPM widgets.
  66. Most widgets have a name, register, shift and invert. Some widgets have extra
  67. parameters for stream name and kcontrols.
  68. 2.1 Stream Domain Widgets
  69. -------------------------
  70. Stream Widgets relate to the stream power domain and only consist of ADCs
  71. (analog to digital converters), DACs (digital to analog converters),
  72. AIF IN and AIF OUT.
  73. Stream widgets have the following format:-
  74. SND_SOC_DAPM_DAC(name, stream name, reg, shift, invert),
  75. SND_SOC_DAPM_AIF_IN(name, stream, slot, reg, shift, invert)
  76. NOTE: the stream name must match the corresponding stream name in your codec
  77. snd_soc_codec_dai.
  78. e.g. stream widgets for HiFi playback and capture
  79. SND_SOC_DAPM_DAC("HiFi DAC", "HiFi Playback", REG, 3, 1),
  80. SND_SOC_DAPM_ADC("HiFi ADC", "HiFi Capture", REG, 2, 1),
  81. e.g. stream widgets for AIF
  82. SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
  83. SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
  84. 2.2 Path Domain Widgets
  85. -----------------------
  86. Path domain widgets have a ability to control or affect the audio signal or
  87. audio paths within the audio subsystem. They have the following form:-
  88. SND_SOC_DAPM_PGA(name, reg, shift, invert, controls, num_controls)
  89. Any widget kcontrols can be set using the controls and num_controls members.
  90. e.g. Mixer widget (the kcontrols are declared first)
  91. /* Output Mixer */
  92. static const snd_kcontrol_new_t wm8731_output_mixer_controls[] = {
  93. SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
  94. SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
  95. SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
  96. };
  97. SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1, wm8731_output_mixer_controls,
  98. ARRAY_SIZE(wm8731_output_mixer_controls)),
  99. If you dont want the mixer elements prefixed with the name of the mixer widget,
  100. you can use SND_SOC_DAPM_MIXER_NAMED_CTL instead. the parameters are the same
  101. as for SND_SOC_DAPM_MIXER.
  102. 2.3 Machine domain Widgets
  103. --------------------------
  104. Machine widgets are different from codec widgets in that they don't have a
  105. codec register bit associated with them. A machine widget is assigned to each
  106. machine audio component (non codec or DSP) that can be independently
  107. powered. e.g.
  108. o Speaker Amp
  109. o Microphone Bias
  110. o Jack connectors
  111. A machine widget can have an optional call back.
  112. e.g. Jack connector widget for an external Mic that enables Mic Bias
  113. when the Mic is inserted:-
  114. static int spitz_mic_bias(struct snd_soc_dapm_widget* w, int event)
  115. {
  116. gpio_set_value(SPITZ_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
  117. return 0;
  118. }
  119. SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias),
  120. 2.4 Codec (BIAS) Domain
  121. -----------------------
  122. The codec bias power domain has no widgets and is handled by the codecs DAPM
  123. event handler. This handler is called when the codec powerstate is changed wrt
  124. to any stream event or by kernel PM events.
  125. 2.5 Virtual Widgets
  126. -------------------
  127. Sometimes widgets exist in the codec or machine audio map that don't have any
  128. corresponding soft power control. In this case it is necessary to create
  129. a virtual widget - a widget with no control bits e.g.
  130. SND_SOC_DAPM_MIXER("AC97 Mixer", SND_SOC_DAPM_NOPM, 0, 0, NULL, 0),
  131. This can be used to merge to signal paths together in software.
  132. After all the widgets have been defined, they can then be added to the DAPM
  133. subsystem individually with a call to snd_soc_dapm_new_control().
  134. 3. Codec/DSP Widget Interconnections
  135. ====================================
  136. Widgets are connected to each other within the codec, platform and machine by
  137. audio paths (called interconnections). Each interconnection must be defined in
  138. order to create a map of all audio paths between widgets.
  139. This is easiest with a diagram of the codec or DSP (and schematic of the machine
  140. audio system), as it requires joining widgets together via their audio signal
  141. paths.
  142. e.g., from the WM8731 output mixer (wm8731.c)
  143. The WM8731 output mixer has 3 inputs (sources)
  144. 1. Line Bypass Input
  145. 2. DAC (HiFi playback)
  146. 3. Mic Sidetone Input
  147. Each input in this example has a kcontrol associated with it (defined in example
  148. above) and is connected to the output mixer via its kcontrol name. We can now
  149. connect the destination widget (wrt audio signal) with its source widgets.
  150. /* output mixer */
  151. {"Output Mixer", "Line Bypass Switch", "Line Input"},
  152. {"Output Mixer", "HiFi Playback Switch", "DAC"},
  153. {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
  154. So we have :-
  155. Destination Widget <=== Path Name <=== Source Widget
  156. Or:-
  157. Sink, Path, Source
  158. Or :-
  159. "Output Mixer" is connected to the "DAC" via the "HiFi Playback Switch".
  160. When there is no path name connecting widgets (e.g. a direct connection) we
  161. pass NULL for the path name.
  162. Interconnections are created with a call to:-
  163. snd_soc_dapm_connect_input(codec, sink, path, source);
  164. Finally, snd_soc_dapm_new_widgets(codec) must be called after all widgets and
  165. interconnections have been registered with the core. This causes the core to
  166. scan the codec and machine so that the internal DAPM state matches the
  167. physical state of the machine.
  168. 3.1 Machine Widget Interconnections
  169. -----------------------------------
  170. Machine widget interconnections are created in the same way as codec ones and
  171. directly connect the codec pins to machine level widgets.
  172. e.g. connects the speaker out codec pins to the internal speaker.
  173. /* ext speaker connected to codec pins LOUT2, ROUT2 */
  174. {"Ext Spk", NULL , "ROUT2"},
  175. {"Ext Spk", NULL , "LOUT2"},
  176. This allows the DAPM to power on and off pins that are connected (and in use)
  177. and pins that are NC respectively.
  178. 4 Endpoint Widgets
  179. ===================
  180. An endpoint is a start or end point (widget) of an audio signal within the
  181. machine and includes the codec. e.g.
  182. o Headphone Jack
  183. o Internal Speaker
  184. o Internal Mic
  185. o Mic Jack
  186. o Codec Pins
  187. Endpoints are added to the DAPM graph so that their usage can be determined in
  188. order to save power. e.g. NC codecs pins will be switched OFF, unconnected
  189. jacks can also be switched OFF.
  190. 5 DAPM Widget Events
  191. ====================
  192. Some widgets can register their interest with the DAPM core in PM events.
  193. e.g. A Speaker with an amplifier registers a widget so the amplifier can be
  194. powered only when the spk is in use.
  195. /* turn speaker amplifier on/off depending on use */
  196. static int corgi_amp_event(struct snd_soc_dapm_widget *w, int event)
  197. {
  198. gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
  199. return 0;
  200. }
  201. /* corgi machine dapm widgets */
  202. static const struct snd_soc_dapm_widget wm8731_dapm_widgets =
  203. SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event);
  204. Please see soc-dapm.h for all other widgets that support events.
  205. 5.1 Event types
  206. ---------------
  207. The following event types are supported by event widgets.
  208. /* dapm event types */
  209. #define SND_SOC_DAPM_PRE_PMU 0x1 /* before widget power up */
  210. #define SND_SOC_DAPM_POST_PMU 0x2 /* after widget power up */
  211. #define SND_SOC_DAPM_PRE_PMD 0x4 /* before widget power down */
  212. #define SND_SOC_DAPM_POST_PMD 0x8 /* after widget power down */
  213. #define SND_SOC_DAPM_PRE_REG 0x10 /* before audio path setup */
  214. #define SND_SOC_DAPM_POST_REG 0x20 /* after audio path setup */