420 lines · plain
1===================================================2Dynamic Audio Power Management for Portable Devices3===================================================4 5Description6===========7 8Dynamic Audio Power Management (DAPM) is designed to allow portable9Linux devices to use the minimum amount of power within the audio10subsystem at all times. It is independent of other kernel power11management frameworks and, as such, can easily co-exist with them.12 13DAPM is also completely transparent to all user space applications as14all power switching is done within the ASoC core. No code changes or15recompiling are required for user space applications. DAPM makes power16switching decisions based upon any audio stream (capture/playback)17activity and audio mixer settings within the device.18 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 by22 software when in use and disabled to save power when not in use23 * a **route** is an interconnection between widgets that exists when sound24 can flow from one widget to the other25 26All DAPM power switching decisions are made automatically by consulting an27audio routing graph. This graph is specific to each sound card and spans28the whole sound card, so some DAPM routes connect two widgets belonging to29different components (e.g. the LINE OUT pin of a CODEC and the input pin of30an amplifier).31 32The graph for the STM32MP1-DK1 sound card is shown in picture:33 34.. kernel-figure:: dapm-graph.svg35 :alt: Example DAPM graph36 :align: center37 38DAPM power domains39==================40 41There are 4 power domains within DAPM:42 43Codec bias domain44 VREF, VMID (core codec and audio power)45 46 Usually controlled at codec probe/remove and suspend/resume, although47 can be set at stream time if power is not needed for sidetone, etc.48 49Platform/Machine domain50 physically connected inputs and outputs51 52 Is platform/machine and user action specific, is configured by the53 machine driver and responds to asynchronous events e.g when HP54 are inserted55 56Path domain57 audio subsystem signal paths58 59 Automatically set when mixer and mux settings are changed by the user.60 e.g. alsamixer, amixer.61 62Stream domain63 DACs and ADCs.64 65 Enabled and disabled when stream playback/capture is started and66 stopped respectively. e.g. aplay, arecord.67 68 69DAPM Widgets70============71 72Audio DAPM widgets fall into a number of types:73 74Mixer75 Mixes several analog signals into a single analog signal.76Mux77 An analog switch that outputs only one of many inputs.78PGA79 A programmable gain amplifier or attenuation widget.80ADC81 Analog to Digital Converter82DAC83 Digital to Analog Converter84Switch85 An analog switch86Input87 A codec input pin88Output89 A codec output pin90Headphone91 Headphone (and optional Jack)92Mic93 Mic (and optional Jack)94Line95 Line Input/Output (and optional Jack)96Speaker97 Speaker98Supply99 Power or clock supply widget used by other widgets.100Regulator101 External regulator that supplies power to audio components.102Clock103 External clock that supplies clock to audio components.104AIF IN105 Audio Interface Input (with TDM slot mask).106AIF OUT107 Audio Interface Output (with TDM slot mask).108Siggen109 Signal Generator.110DAI IN111 Digital Audio Interface Input.112DAI OUT113 Digital Audio Interface Output.114DAI Link115 DAI Link between two DAI structures116Pre117 Special PRE widget (exec before all others)118Post119 Special POST widget (exec after all others)120Buffer121 Inter widget audio data buffer within a DSP.122Scheduler123 DSP internal scheduler that schedules component/pipeline processing124 work.125Effect126 Widget that performs an audio processing effect.127SRC128 Sample Rate Converter within DSP or CODEC129ASRC130 Asynchronous Sample Rate Converter within DSP or CODEC131Encoder132 Widget that encodes audio data from one format (usually PCM) to another133 usually more compressed format.134Decoder135 Widget that decodes audio data from a compressed format to an136 uncompressed format like PCM.137 138 139(Widgets are defined in include/sound/soc-dapm.h)140 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 quickly143build a list of widgets of the codecs and machines DAPM widgets.144 145Most widgets have a name, register, shift and invert. Some widgets have extra146parameters for stream name and kcontrols.147 148 149Stream Domain Widgets150---------------------151 152Stream Widgets relate to the stream power domain and only consist of ADCs153(analog to digital converters), DACs (digital to analog converters),154AIF IN and AIF OUT.155 156Stream widgets have the following format:157::158 159 SND_SOC_DAPM_DAC(name, stream name, reg, shift, invert),160 SND_SOC_DAPM_AIF_IN(name, stream, slot, reg, shift, invert)161 162NOTE: the stream name must match the corresponding stream name in your codec163snd_soc_dai_driver.164 165e.g. stream widgets for HiFi playback and capture166::167 168 SND_SOC_DAPM_DAC("HiFi DAC", "HiFi Playback", REG, 3, 1),169 SND_SOC_DAPM_ADC("HiFi ADC", "HiFi Capture", REG, 2, 1),170 171e.g. stream widgets for AIF172::173 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),176 177 178Path Domain Widgets179-------------------180 181Path domain widgets have a ability to control or affect the audio signal or182audio paths within the audio subsystem. They have the following form:183::184 185 SND_SOC_DAPM_PGA(name, reg, shift, invert, controls, num_controls)186 187Any widget kcontrols can be set using the controls and num_controls members.188 189e.g. Mixer widget (the kcontrols are declared first)190::191 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 };198 199 SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1, wm8731_output_mixer_controls,200 ARRAY_SIZE(wm8731_output_mixer_controls)),201 202If you don't want the mixer elements prefixed with the name of the mixer widget,203you can use SND_SOC_DAPM_MIXER_NAMED_CTL instead. the parameters are the same204as for SND_SOC_DAPM_MIXER.205 206 207Machine domain Widgets208----------------------209 210Machine widgets are different from codec widgets in that they don't have a211codec register bit associated with them. A machine widget is assigned to each212machine audio component (non codec or DSP) that can be independently213powered. e.g.214 215* Speaker Amp216* Microphone Bias217* Jack connectors218 219A machine widget can have an optional call back.220 221e.g. Jack connector widget for an external Mic that enables Mic Bias222when the Mic is inserted::223 224 static int spitz_mic_bias(struct snd_soc_dapm_widget* w, int event)225 {226 gpio_set_value(SPITZ_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));227 return 0;228 }229 230 SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias),231 232 233Codec (BIAS) Domain234-------------------235 236The codec bias power domain has no widgets and is handled by the codec DAPM237event handler. This handler is called when the codec powerstate is changed wrt238to any stream event or by kernel PM events.239 240 241Virtual Widgets242---------------243 244Sometimes widgets exist in the codec or machine audio graph that don't have any245corresponding soft power control. In this case it is necessary to create246a virtual widget - a widget with no control bits e.g.247::248 249 SND_SOC_DAPM_MIXER("AC97 Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),250 251This can be used to merge two signal paths together in software.252 253Registering DAPM controls254=========================255 256In many cases the DAPM widgets are implemented statically in a ``static257const struct snd_soc_dapm_widget`` array in a codec driver, and simply258declared via the ``dapm_widgets`` and ``num_dapm_widgets`` fields of the259``struct snd_soc_component_driver``.260 261Similarly, routes connecting them are implemented statically in a ``static262const struct snd_soc_dapm_route`` array and declared via the263``dapm_routes`` and ``num_dapm_routes`` fields of the same struct.264 265With the above declared, the driver registration will take care of266populating them::267 268 static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = {269 SND_SOC_DAPM_OUTPUT("SPKN"),270 SND_SOC_DAPM_OUTPUT("SPKP"),271 ...272 };273 274 /* Target, Path, Source */275 static const struct snd_soc_dapm_route wm2000_audio_map[] = {276 { "SPKN", NULL, "ANC Engine" },277 { "SPKP", NULL, "ANC Engine" },278 ...279 };280 281 static const struct snd_soc_component_driver soc_component_dev_wm2000 = {282 ...283 .dapm_widgets = wm2000_dapm_widgets,284 .num_dapm_widgets = ARRAY_SIZE(wm2000_dapm_widgets),285 .dapm_routes = wm2000_audio_map,286 .num_dapm_routes = ARRAY_SIZE(wm2000_audio_map),287 ...288 };289 290In more complex cases the list of DAPM widgets and/or routes can be only291known at probe time. This happens for example when a driver supports292different models having a different set of features. In those cases293separate widgets and routes arrays implementing the case-specific features294can be registered programmatically by calling snd_soc_dapm_new_controls()295and snd_soc_dapm_add_routes().296 297 298Codec/DSP Widget Interconnections299=================================300 301Widgets are connected to each other within the codec, platform and machine by302audio paths (called interconnections). Each interconnection must be defined in303order to create a graph of all audio paths between widgets.304 305This is easiest with a diagram of the codec or DSP (and schematic of the machine306audio system), as it requires joining widgets together via their audio signal307paths.308 309For example the WM8731 output mixer (wm8731.c) has 3 inputs (sources):310 3111. Line Bypass Input3122. DAC (HiFi playback)3133. Mic Sidetone Input314 315Each input in this example has a kcontrol associated with it (defined in316the example above) and is connected to the output mixer via its kcontrol317name. We can now connect the destination widget (wrt audio signal) with its318source widgets. ::319 320 /* output mixer */321 {"Output Mixer", "Line Bypass Switch", "Line Input"},322 {"Output Mixer", "HiFi Playback Switch", "DAC"},323 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},324 325So we have:326 327* Destination Widget <=== Path Name <=== Source Widget, or328* Sink, Path, Source, or329* ``Output Mixer`` is connected to the ``DAC`` via the ``HiFi Playback Switch``.330 331When there is no path name connecting widgets (e.g. a direct connection) we332pass NULL for the path name.333 334Interconnections are created with a call to::335 336 snd_soc_dapm_connect_input(codec, sink, path, source);337 338Finally, snd_soc_dapm_new_widgets() must be called after all widgets and339interconnections have been registered with the core. This causes the core to340scan the codec and machine so that the internal DAPM state matches the341physical state of the machine.342 343 344Machine Widget Interconnections345-------------------------------346Machine widget interconnections are created in the same way as codec ones and347directly connect the codec pins to machine level widgets.348 349e.g. connects the speaker out codec pins to the internal speaker.350::351 352 /* ext speaker connected to codec pins LOUT2, ROUT2 */353 {"Ext Spk", NULL , "ROUT2"},354 {"Ext Spk", NULL , "LOUT2"},355 356This allows the DAPM to power on and off pins that are connected (and in use)357and pins that are NC respectively.358 359 360Endpoint Widgets361================362An endpoint is a start or end point (widget) of an audio signal within the363machine and includes the codec. e.g.364 365* Headphone Jack366* Internal Speaker367* Internal Mic368* Mic Jack369* Codec Pins370 371Endpoints are added to the DAPM graph so that their usage can be determined in372order to save power. e.g. NC codecs pins will be switched OFF, unconnected373jacks can also be switched OFF.374 375 376DAPM Widget Events377==================378 379Widgets needing to implement a more complex behaviour than what DAPM can do380can set a custom "event handler" by setting a function pointer. An example381is a power supply needing to enable a GPIO::382 383 static int sof_es8316_speaker_power_event(struct snd_soc_dapm_widget *w,384 struct snd_kcontrol *kcontrol, int event)385 {386 if (SND_SOC_DAPM_EVENT_ON(event))387 gpiod_set_value_cansleep(gpio_pa, true);388 else389 gpiod_set_value_cansleep(gpio_pa, false);390 391 return 0;392 }393 394 static const struct snd_soc_dapm_widget st_widgets[] = {395 ...396 SND_SOC_DAPM_SUPPLY("Speaker Power", SND_SOC_NOPM, 0, 0,397 sof_es8316_speaker_power_event,398 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),399 };400 401See soc-dapm.h for all other widgets that support events.402 403 404Event types405-----------406 407The following event types are supported by event widgets::408 409 /* dapm event types */410 #define SND_SOC_DAPM_PRE_PMU 0x1 /* before widget power up */411 #define SND_SOC_DAPM_POST_PMU 0x2 /* after widget power up */412 #define SND_SOC_DAPM_PRE_PMD 0x4 /* before widget power down */413 #define SND_SOC_DAPM_POST_PMD 0x8 /* after widget power down */414 #define SND_SOC_DAPM_PRE_REG 0x10 /* before audio path setup */415 #define SND_SOC_DAPM_POST_REG 0x20 /* after audio path setup */416 #define SND_SOC_DAPM_WILL_PMU 0x40 /* called at start of sequence */417 #define SND_SOC_DAPM_WILL_PMD 0x80 /* called at start of sequence */418 #define SND_SOC_DAPM_PRE_POST_PMD (SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD)419 #define SND_SOC_DAPM_PRE_POST_PMU (SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU)420