dapm.txt 10.0 KB

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