linux音频子系统 - DAPM

Dynamic Audio Power Management for Portable Devices
移动设备的动态电源管理(DAPM)

1. Description

DAPM使得使用音频子系统的移动linux设备,在任何时候,都能够获得最小功耗。DAPM独立于
kernel中其他PM系统,能够和其他PM系统和谐共存。

DAPM对于用户层是透明的,电源切换在asoc-driver层完成。用户层的代码不需要改变重编。
DAPM做出电源切换的决定基于音频流的活动状态和mixer设备的配置状态。

DAPM横跨整个machine,它覆盖整个音频子系统的电源控制,包括codec电源块和machine层的电源系统。

DAPM有四个主要的电源域:

  • 1.codec电源域 - VREF,VMID(core codec和audio power)
    此域通常由probe/remove和suspend/resume来控制。
  • 2.platform/machine域 - 输入输出物理连接
    这部分是platform/machine和用户自定义来配置
  • 3.path域 - audio子系统信号路径
    用户层改变mixer和mux配置时自动设置
  • 4.stream域 - DAC和ADC
    当播放和录音开始结束时,使能和关闭

所有DAPM的电源控制由整个machine的音频链路(routing map)来决定,整个链路对于每个machine来说都是特定的,
它由每个音频单元的连接组成,所有影响功耗的音频单元我们称为widget(小部件)。

2. DAPM小部件

所有DAPM widget不外乎下面几种类型:

o Mixer - Mixes several analog signals into a single analog signal.
o Mux - An analog switch that outputs only one of many inputs.
o PGA - A programmable gain amplifier or attenuation widget.
o ADC - Analog to Digital Converter
o DAC - Digital to Analog Converter
o Switch - An analog switch
o Input - A codec input pin
o Output - A codec output pin
o Headphone - Headphone (and optional Jack)
o Mic - Mic (and optional Jack)
o Line - Line Input/Output (and optional Jack)
o Speaker - Speaker
o Supply - Power or clock supply widget used by other widgets.
o Pre - Special PRE widget (exec before all others)
o Post - Special POST widget (exec after all others)

(Widgets are defined in include/sound/soc-dapm.h)

widget通常在codec和machine驱动中增加,在文件soc-dapm.h文件中有许多宏定义可以方便快速的定义
codec widget 和 machine DAPM widget。

大部分widget有name, register, shift and invert字段,还有的widget有额外的元素,包括
stream name和kcontrol。

2.1 stream域的部件

stream的部件和stream电源域相关,只包含ADC和DAC两种部件

Stream widgets have the following format:-

SND_SOC_DAPM_DAC(name, stream name, reg, shift, invert),

NOTE: stream name必须匹配snd_soc_codec_dai的name

e.g. stream widgets for HiFi playback and capture

SND_SOC_DAPM_DAC("HiFi DAC", "HiFi Playback", REG, 3, 1),
SND_SOC_DAPM_ADC("HiFi ADC", "HiFi Capture", REG, 2, 1),

2.2 path域的部件

path域的部件主要是控制和影响audio子系统中的audio signal或者audio path,它们用下面的形式来表示:

SND_SOC_DAPM_PGA(name, reg, shift, invert, controls, num_controls)

任何部件的kcontrol设备可以由controls和num_controls成员来设置

e.g. Mixer widget (the kcontrols are declared first)

/* Output Mixer */
static const snd_kcontrol_new_t wm8731_output_mixer_controls[] = {
SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
};

SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1, wm8731_output_mixer_controls,
    ARRAY_SIZE(wm8731_output_mixer_controls)),

如果你不想在mixer元素前面加上mixer部件的前缀,那么可以使用SND_SOC_DAPM_MIXER_NAMED_CTL来替代,
这个定义和SND_SOC_DAPM_MIXER是一样的

2.3 platform/machine域部件

machine部件和codec部件不一样,它们没有相关的codec寄存器,一个machine部件就是一个audio单元,
可以独立供电,比如下面的器件:

o Speaker Amp
o Microphone Bias
o Jack connectors

一个machine部件有一个可选的调用函数

举例:当麦克风插入时,外部的麦克风接口部件需要使能偏压:
static int spitz_mic_bias(struct snd_soc_dapm_widget* w, int event)
{
    gpio_set_value(SPITZ_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
    return 0;
}

SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias),

2.4 codec域

codec电源域没有部件,它由codec DAPM event来操作.event的操作发生在codec电源状态改变的时候,
或者通过kernel PM事件来触发

2.5 虚拟部件

有时codec和machine中的部件没有相关电源控制的操作,对于此种情况,就需要创建一个虚拟的部件,如下

SND_SOC_DAPM_MIXER("AC97 Mixer", SND_SOC_DAPM_NOPM, 0, 0, NULL, 0),

这个可以在软件层面单独和入到一个信号路径上

所有的部件都定义好后,它们就可以加入到DAPM子系统中,通过接口snd_soc_dapm_new_control分别调用

3. codec部件相互连接

codec和machine的widget通过audio path来建立相互连接,每个连接必须按顺序通过
创建部件之间的path映射来定义

这个很容易建立一个codec的图表,它需要把经过audio signal路径的部件一起加入进来

以wm8731输出混合器为例:

The WM8731 output mixer has 3 inputs (sources)

  1. Line Bypass Input
  2. DAC (HiFi playback)
  3. Mic Sidetone Input

Each input in this example has a kcontrol associated with it (defined in example
above) and is connected to the output mixer via its kcontrol name. We can now
connect the destination widget (wrt audio signal) with its source widgets.

/* output mixer */
{"Output Mixer", "Line Bypass Switch", "Line Input"},
{"Output Mixer", "HiFi Playback Switch", "DAC"},
{"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},

So we have :-

Destination Widget  <=== Path Name <=== Source Widget

Or:-

Sink, Path, Source

Or :-

"Output Mixer" is connected to the "DAC" via the "HiFi Playback Switch".

当相互连接的部件没有path name时,我们用NULL来表示

连接的建立通过调用下面的函数:

snd_soc_dapm_connect_input(codec, sink, path, source);

最终,在所有的部件和连接都注册到系统后,snd_soc_dapm_new_widgets(codec)必须被调用,这样会导致
系统去扫描codec和machine,以便DAPM状态可以匹配到machine的物理状态

3.1 machine部件连接

machine部件的连接创建过程和codec一样,直接连接codec到machine层部件

举例:连接speaker输出codec引脚到内部的speaker

/* ext speaker connected to codec pins LOUT2, ROUT2  */
{"Ext Spk", NULL , "ROUT2"},
{"Ext Spk", NULL , "LOUT2"},

这样会允许DAPM对有连接的引脚和未使用的引脚来上下电

4. 端点部件

一个端点是machine包括codec音频信号的开始或者结束,包括以下几个:

  • o Headphone Jack
  • o Internal Speaker
  • o Internal Mic
  • o Mic Jack
  • o Codec Pins

当一个codec的引脚是NC时,它可以被标志为未使用,可以用下面函数来设置

snd_soc_dapm_set_endpoint(codec, "Widget Name", 0);

最后的参数0代表未活动状态,1代表活动状态,通过这种方式可以让某些部件永远不会上电,从而节省功耗

这个也适用于machine部件,比如,一个耳机连接到一个插孔,那么这个插孔需要被标志为活动的,如果一个
耳机被拔出,那么这个耳机插孔需要被标志为未活动的

5. DAPM部件事件机制

一些部件可以注册它们感兴趣的到系统的PM事件中,比如,一个带了放大器的扬声器注册为一个部件,
那么这个放大器部件只会在扬声器使用的时候上电

/* turn speaker amplifier on/off depending on use */
static int corgi_amp_event(struct snd_soc_dapm_widget *w, int event)
{
    gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
    return 0;
}

/* corgi machine dapm widgets */
static const struct snd_soc_dapm_widget wm8731_dapm_widgets =
    SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event);

对于所有支持事件的部件请看文件soc-dapm.h

5.1 事件类型

部件支持下面几种事件类型
/* dapm event types */
#define SND_SOC_DAPM_PRE_PMU 0x1 /* before widget power up */
#define SND_SOC_DAPM_POST_PMU 0x2 /* after widget power up */
#define SND_SOC_DAPM_PRE_PMD 0x4 /* before widget power down */
#define SND_SOC_DAPM_POST_PMD 0x8 /* after widget power down */
#define SND_SOC_DAPM_PRE_REG 0x10 /* before audio path setup */
#define SND_SOC_DAPM_POST_REG 0x20 /* after audio path setup */


附录

Dynamic Audio Power Management for Portable Devices

1. Description

Dynamic Audio Power Management (DAPM) is designed to allow portable
Linux devices to use the minimum amount of power within the audio
subsystem at all times. It is independent of other kernel PM and as
such, can easily co-exist with the other PM systems.

DAPM is also completely transparent to all user space applications as
all power switching is done within the ASoC core. No code changes or
recompiling are required for user space applications. DAPM makes power
switching decisions based upon any audio stream (capture/playback)
activity and audio mixer settings within the device.

DAPM spans the whole machine. It covers power control within the entire
audio subsystem, this includes internal codec power blocks and machine
level power systems.

There are 4 power domains within DAPM

  1. Codec domain - VREF, VMID (core codec and audio power)
    Usually controlled at codec probe/remove and suspend/resume, although
    can be set at stream time if power is not needed for sidetone, etc.

  2. Platform/Machine domain - physically connected inputs and outputs
    Is platform/machine and user action specific, is configured by the
    machine driver and responds to asynchronous events e.g when HP
    are inserted

  3. Path domain - audio susbsystem signal paths
    Automatically set when mixer and mux settings are changed by the user.
    e.g. alsamixer, amixer.

  4. Stream domain - DACs and ADCs.
    Enabled and disabled when stream playback/capture is started and
    stopped respectively. e.g. aplay, arecord.

All DAPM power switching decisions are made automatically by consulting an audio
routing map of the whole machine. This map is specific to each machine and
consists of the interconnections between every audio component (including
internal codec components). All audio components that effect power are called
widgets hereafter.

2. DAPM Widgets

Audio DAPM widgets fall into a number of types:-

o Mixer - Mixes several analog signals into a single analog signal.
o Mux - An analog switch that outputs only one of many inputs.
o PGA - A programmable gain amplifier or attenuation widget.
o ADC - Analog to Digital Converter
o DAC - Digital to Analog Converter
o Switch - An analog switch
o Input - A codec input pin
o Output - A codec output pin
o Headphone - Headphone (and optional Jack)
o Mic - Mic (and optional Jack)
o Line - Line Input/Output (and optional Jack)
o Speaker - Speaker
o Supply - Power or clock supply widget used by other widgets.
o Pre - Special PRE widget (exec before all others)
o Post - Special POST widget (exec after all others)

(Widgets are defined in include/sound/soc-dapm.h)

Widgets are usually added in the codec driver and the machine driver. There are
convenience macros defined in soc-dapm.h that can be used to quickly build a
list of widgets of the codecs and machines DAPM widgets.

Most widgets have a name, register, shift and invert. Some widgets have extra
parameters for stream name and kcontrols.

2.1 Stream Domain Widgets

Stream Widgets relate to the stream power domain and only consist of ADCs
(analog to digital converters) and DACs (digital to analog converters).

Stream widgets have the following format:-

SND_SOC_DAPM_DAC(name, stream name, reg, shift, invert),

NOTE: the stream name must match the corresponding stream name in your codec
snd_soc_codec_dai.

e.g. stream widgets for HiFi playback and capture

SND_SOC_DAPM_DAC(“HiFi DAC”, “HiFi Playback”, REG, 3, 1),
SND_SOC_DAPM_ADC(“HiFi ADC”, “HiFi Capture”, REG, 2, 1),

2.2 Path Domain Widgets

Path domain widgets have a ability to control or affect the audio signal or
audio paths within the audio subsystem. They have the following form:-

SND_SOC_DAPM_PGA(name, reg, shift, invert, controls, num_controls)

Any widget kcontrols can be set using the controls and num_controls members.

e.g. Mixer widget (the kcontrols are declared first)

/* Output Mixer */
static const snd_kcontrol_new_t wm8731_output_mixer_controls[] = {
SOC_DAPM_SINGLE(“Line Bypass Switch”, WM8731_APANA, 3, 1, 0),
SOC_DAPM_SINGLE(“Mic Sidetone Switch”, WM8731_APANA, 5, 1, 0),
SOC_DAPM_SINGLE(“HiFi Playback Switch”, WM8731_APANA, 4, 1, 0),
};

SND_SOC_DAPM_MIXER(“Output Mixer”, WM8731_PWR, 4, 1, wm8731_output_mixer_controls,
ARRAY_SIZE(wm8731_output_mixer_controls)),

If you dont want the mixer elements prefixed with the name of the mixer widget,
you can use SND_SOC_DAPM_MIXER_NAMED_CTL instead. the parameters are the same
as for SND_SOC_DAPM_MIXER.

2.3 Platform/Machine domain Widgets

Machine widgets are different from codec widgets in that they don’t have a
codec register bit associated with them. A machine widget is assigned to each
machine audio component (non codec) that can be independently powered. e.g.

o Speaker Amp
o Microphone Bias
o Jack connectors

A machine widget can have an optional call back.

e.g. Jack connector widget for an external Mic that enables Mic Bias
when the Mic is inserted:-

static int spitz_mic_bias(struct snd_soc_dapm_widget* w, int event)
{
gpio_set_value(SPITZ_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
return 0;
}

SND_SOC_DAPM_MIC(“Mic Jack”, spitz_mic_bias),

2.4 Codec Domain

The codec power domain has no widgets and is handled by the codecs DAPM event
handler. This handler is called when the codec powerstate is changed wrt to any
stream event or by kernel PM events.

2.5 Virtual Widgets

Sometimes widgets exist in the codec or machine audio map that don’t have any
corresponding soft power control. In this case it is necessary to create
a virtual widget - a widget with no control bits e.g.

SND_SOC_DAPM_MIXER(“AC97 Mixer”, SND_SOC_DAPM_NOPM, 0, 0, NULL, 0),

This can be used to merge to signal paths together in software.

After all the widgets have been defined, they can then be added to the DAPM
subsystem individually with a call to snd_soc_dapm_new_control().

3. Codec Widget Interconnections

Widgets are connected to each other within the codec and machine by audio paths
(called interconnections). Each interconnection must be defined in order to
create a map of all audio paths between widgets.

This is easiest with a diagram of the codec (and schematic of the machine audio
system), as it requires joining widgets together via their audio signal paths.

e.g., from the WM8731 output mixer (wm8731.c)

The WM8731 output mixer has 3 inputs (sources)

  1. Line Bypass Input
  2. DAC (HiFi playback)
  3. Mic Sidetone Input

Each input in this example has a kcontrol associated with it (defined in example
above) and is connected to the output mixer via its kcontrol name. We can now
connect the destination widget (wrt audio signal) with its source widgets.

/* output mixer */
{"Output Mixer", "Line Bypass Switch", "Line Input"},
{"Output Mixer", "HiFi Playback Switch", "DAC"},
{"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},

So we have :-

Destination Widget  <=== Path Name <=== Source Widget

Or:-

Sink, Path, Source

Or :-

"Output Mixer" is connected to the "DAC" via the "HiFi Playback Switch".

When there is no path name connecting widgets (e.g. a direct connection) we
pass NULL for the path name.

Interconnections are created with a call to:-

snd_soc_dapm_connect_input(codec, sink, path, source);

Finally, snd_soc_dapm_new_widgets(codec) must be called after all widgets and
interconnections have been registered with the core. This causes the core to
scan the codec and machine so that the internal DAPM state matches the
physical state of the machine.

3.1 Machine Widget Interconnections

Machine widget interconnections are created in the same way as codec ones and
directly connect the codec pins to machine level widgets.

e.g. connects the speaker out codec pins to the internal speaker.

/* ext speaker connected to codec pins LOUT2, ROUT2  */
{"Ext Spk", NULL , "ROUT2"},
{"Ext Spk", NULL , "LOUT2"},

This allows the DAPM to power on and off pins that are connected (and in use)
and pins that are NC respectively.

4 Endpoint Widgets

An endpoint is a start or end point (widget) of an audio signal within the
machine and includes the codec. e.g.

o Headphone Jack
o Internal Speaker
o Internal Mic
o Mic Jack
o Codec Pins

When a codec pin is NC it can be marked as not used with a call to

snd_soc_dapm_set_endpoint(codec, “Widget Name”, 0);

The last argument is 0 for inactive and 1 for active. This way the pin and its
input widget will never be powered up and consume power.

This also applies to machine widgets. e.g. if a headphone is connected to a
jack then the jack can be marked active. If the headphone is removed, then
the headphone jack can be marked inactive.

5 DAPM Widget Events

Some widgets can register their interest with the DAPM core in PM events.
e.g. A Speaker with an amplifier registers a widget so the amplifier can be
powered only when the spk is in use.

/* turn speaker amplifier on/off depending on use */
static int corgi_amp_event(struct snd_soc_dapm_widget *w, int event)
{
gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
return 0;
}

/* corgi machine dapm widgets */
static const struct snd_soc_dapm_widget wm8731_dapm_widgets =
SND_SOC_DAPM_SPK(“Ext Spk”, corgi_amp_event);

Please see soc-dapm.h for all other widgets that support events.

5.1 Event types

The following event types are supported by event widgets.

/* dapm event types */
#define SND_SOC_DAPM_PRE_PMU 0x1 /* before widget power up */
#define SND_SOC_DAPM_POST_PMU 0x2 /* after widget power up */
#define SND_SOC_DAPM_PRE_PMD 0x4 /* before widget power down */
#define SND_SOC_DAPM_POST_PMD 0x8 /* after widget power down */
#define SND_SOC_DAPM_PRE_REG 0x10 /* before audio path setup */
#define SND_SOC_DAPM_POST_REG 0x20 /* after audio path setup */

猜你喜欢

转载自blog.csdn.net/l289123557/article/details/78322496