dapm.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 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 susbsystem 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 Pre - Special PRE widget (exec before all others)
  53. o Post - Special POST widget (exec after all others)
  54. (Widgets are defined in include/sound/soc-dapm.h)
  55. Widgets are usually added in the codec driver and the machine driver. There are
  56. convenience macros defined in soc-dapm.h that can be used to quickly build a
  57. list of widgets of the codecs and machines DAPM widgets.
  58. Most widgets have a name, register, shift and invert. Some widgets have extra
  59. parameters for stream name and kcontrols.
  60. 2.1 Stream Domain Widgets
  61. -------------------------
  62. Stream Widgets relate to the stream power domain and only consist of ADCs
  63. (analog to digital converters) and DACs (digital to analog converters).
  64. Stream widgets have the following format:-
  65. SND_SOC_DAPM_DAC(name, stream name, reg, shift, invert),
  66. NOTE: the stream name must match the corresponding stream name in your codec
  67. snd_soc_codec_dai.
  68. e.g. stream widgets for HiFi playback and capture
  69. SND_SOC_DAPM_DAC("HiFi DAC", "HiFi Playback", REG, 3, 1),
  70. SND_SOC_DAPM_ADC("HiFi ADC", "HiFi Capture", REG, 2, 1),
  71. 2.2 Path Domain Widgets
  72. -----------------------
  73. Path domain widgets have a ability to control or affect the audio signal or
  74. audio paths within the audio subsystem. They have the following form:-
  75. SND_SOC_DAPM_PGA(name, reg, shift, invert, controls, num_controls)
  76. Any widget kcontrols can be set using the controls and num_controls members.
  77. e.g. Mixer widget (the kcontrols are declared first)
  78. /* Output Mixer */
  79. static const snd_kcontrol_new_t wm8731_output_mixer_controls[] = {
  80. SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
  81. SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
  82. SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
  83. };
  84. SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1, wm8731_output_mixer_controls,
  85. ARRAY_SIZE(wm8731_output_mixer_controls)),
  86. If you dont want the mixer elements prefixed with the name of the mixer widget,
  87. you can use SND_SOC_DAPM_MIXER_NAMED_CTL instead. the parameters are the same
  88. as for SND_SOC_DAPM_MIXER.
  89. 2.3 Platform/Machine domain Widgets
  90. -----------------------------------
  91. Machine widgets are different from codec widgets in that they don't have a
  92. codec register bit associated with them. A machine widget is assigned to each
  93. machine audio component (non codec) that can be independently powered. e.g.
  94. o Speaker Amp
  95. o Microphone Bias
  96. o Jack connectors
  97. A machine widget can have an optional call back.
  98. e.g. Jack connector widget for an external Mic that enables Mic Bias
  99. when the Mic is inserted:-
  100. static int spitz_mic_bias(struct snd_soc_dapm_widget* w, int event)
  101. {
  102. gpio_set_value(SPITZ_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
  103. return 0;
  104. }
  105. SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias),
  106. 2.4 Codec Domain
  107. ----------------
  108. The codec power domain has no widgets and is handled by the codecs DAPM event
  109. handler. This handler is called when the codec powerstate is changed wrt to any
  110. stream event or by kernel PM events.
  111. 2.5 Virtual Widgets
  112. -------------------
  113. Sometimes widgets exist in the codec or machine audio map that don't have any
  114. corresponding soft power control. In this case it is necessary to create
  115. a virtual widget - a widget with no control bits e.g.
  116. SND_SOC_DAPM_MIXER("AC97 Mixer", SND_SOC_DAPM_NOPM, 0, 0, NULL, 0),
  117. This can be used to merge to signal paths together in software.
  118. After all the widgets have been defined, they can then be added to the DAPM
  119. subsystem individually with a call to snd_soc_dapm_new_control().
  120. 3. Codec Widget Interconnections
  121. ================================
  122. Widgets are connected to each other within the codec and machine by audio paths
  123. (called interconnections). Each interconnection must be defined in order to
  124. create a map of all audio paths between widgets.
  125. This is easiest with a diagram of the codec (and schematic of the machine audio
  126. system), as it requires joining widgets together via their audio signal paths.
  127. e.g., from the WM8731 output mixer (wm8731.c)
  128. The WM8731 output mixer has 3 inputs (sources)
  129. 1. Line Bypass Input
  130. 2. DAC (HiFi playback)
  131. 3. Mic Sidetone Input
  132. Each input in this example has a kcontrol associated with it (defined in example
  133. above) and is connected to the output mixer via its kcontrol name. We can now
  134. connect the destination widget (wrt audio signal) with its source widgets.
  135. /* output mixer */
  136. {"Output Mixer", "Line Bypass Switch", "Line Input"},
  137. {"Output Mixer", "HiFi Playback Switch", "DAC"},
  138. {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
  139. So we have :-
  140. Destination Widget <=== Path Name <=== Source Widget
  141. Or:-
  142. Sink, Path, Source
  143. Or :-
  144. "Output Mixer" is connected to the "DAC" via the "HiFi Playback Switch".
  145. When there is no path name connecting widgets (e.g. a direct connection) we
  146. pass NULL for the path name.
  147. Interconnections are created with a call to:-
  148. snd_soc_dapm_connect_input(codec, sink, path, source);
  149. Finally, snd_soc_dapm_new_widgets(codec) must be called after all widgets and
  150. interconnections have been registered with the core. This causes the core to
  151. scan the codec and machine so that the internal DAPM state matches the
  152. physical state of the machine.
  153. 3.1 Machine Widget Interconnections
  154. -----------------------------------
  155. Machine widget interconnections are created in the same way as codec ones and
  156. directly connect the codec pins to machine level widgets.
  157. e.g. connects the speaker out codec pins to the internal speaker.
  158. /* ext speaker connected to codec pins LOUT2, ROUT2 */
  159. {"Ext Spk", NULL , "ROUT2"},
  160. {"Ext Spk", NULL , "LOUT2"},
  161. This allows the DAPM to power on and off pins that are connected (and in use)
  162. and pins that are NC respectively.
  163. 4 Endpoint Widgets
  164. ===================
  165. An endpoint is a start or end point (widget) of an audio signal within the
  166. machine and includes the codec. e.g.
  167. o Headphone Jack
  168. o Internal Speaker
  169. o Internal Mic
  170. o Mic Jack
  171. o Codec Pins
  172. When a codec pin is NC it can be marked as not used with a call to
  173. snd_soc_dapm_set_endpoint(codec, "Widget Name", 0);
  174. The last argument is 0 for inactive and 1 for active. This way the pin and its
  175. input widget will never be powered up and consume power.
  176. This also applies to machine widgets. e.g. if a headphone is connected to a
  177. jack then the jack can be marked active. If the headphone is removed, then
  178. the headphone jack can be marked inactive.
  179. 5 DAPM Widget Events
  180. ====================
  181. Some widgets can register their interest with the DAPM core in PM events.
  182. e.g. A Speaker with an amplifier registers a widget so the amplifier can be
  183. powered only when the spk is in use.
  184. /* turn speaker amplifier on/off depending on use */
  185. static int corgi_amp_event(struct snd_soc_dapm_widget *w, int event)
  186. {
  187. gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
  188. return 0;
  189. }
  190. /* corgi machine dapm widgets */
  191. static const struct snd_soc_dapm_widget wm8731_dapm_widgets =
  192. SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event);
  193. Please see soc-dapm.h for all other widgets that support events.
  194. 5.1 Event types
  195. ---------------
  196. The following event types are supported by event widgets.
  197. /* dapm event types */
  198. #define SND_SOC_DAPM_PRE_PMU 0x1 /* before widget power up */
  199. #define SND_SOC_DAPM_POST_PMU 0x2 /* after widget power up */
  200. #define SND_SOC_DAPM_PRE_PMD 0x4 /* before widget power down */
  201. #define SND_SOC_DAPM_POST_PMD 0x8 /* after widget power down */
  202. #define SND_SOC_DAPM_PRE_REG 0x10 /* before audio path setup */
  203. #define SND_SOC_DAPM_POST_REG 0x20 /* after audio path setup */