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