1. 程式人生 > >音訊電源動態管理--概述

音訊電源動態管理--概述

DAPM--Dynamic Audio Power Management,對應結構體是snd_soc_dapm_widget和snd_soc_dapm_route,對應的操作函式是snd_soc_dapm_new_controls()、snd_soc_dapm_add_routes()和snd_soc_dapm_new_widgets()。在我看來,DAPM是音訊驅動初接觸者的噩夢。從何處來,到何處去?它字面上的意義是音訊電源動態管理,但是往往困惑於它是怎麼被觸發的?而最鬱悶的是:這方面的資料是最少的,我涉獵多日,卻戰果寥寥,看來看去還是核心文件dapm.txt最有參考性。先把這個文件貼上:

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 it's kcontrol name. We can now
connect the destination widget (wrt audio signal) with it's 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 */

參考文章連結 http://blog.chinaunix.net/uid-9185047-id-3228644.html

直接來自核心的\Documentation\sound\alsa\soc中dapm.txt

1.描述

音訊電源動態管理(DAPM)的目的是為了在音訊子系統中允許行動式的linux裝置一直使用最小的電量。它獨立於其它核心電源管理,因此可以很方便的和其它電源管理系統共存。

DAPM對於所有使用者空間的程式也是完全透明的,因為所有的電源開關都是由ASoC core來切換的。使用者空間的程式 不需要改變程式碼和重新編譯 。DAPM使得電源開關的切換基於裝置的任何的音訊流(採集或回放)活動和音訊混音器設定之上。

DAPM貫穿於整個machine。它覆蓋整個音訊子系統的電源控制,包括內部的codec電源模組和machine等級的電源系統。

DAPM中有4個電源相關的域:

1.Codec域--VREF(參考電壓),VMID(core codec和音訊電源)

通常在codec probe/remove和suspend/resume時被控制,因此可以在側音不需要電源時在stream time被設定。

2.Platform/Machine 域  --   是platform/machine和user action相關的輸入輸出物理連線,由machine driver配置並且響應一不事件。比如HP插入時

3.Path 域 --- 音訊子系統的訊號路徑

當mixer和mux設定被使用者改變時自動設定,比如alsamixer,amixer

4.Stream 域 -- DACs和ADCs

當回放和採集流開始或者結束時被相應的使能和禁止,比如aplay,arecord

所有DAPM電源開關的策略由整個machine的一個音訊線路圖自動控制。這個線路圖是和每一個machine相關的,並且由所有音訊部件(包括內部的codec部件)的互聯絡統構成。以後,所有影響電源的音訊部件被叫做widget

2.DAPM 部件

DAPM音訊部件分為一系列的型別:

Mixer--將一些模擬訊號混合為一個單一的模擬訊號

Mux--一個控制多路輸入但只有一個輸出的模擬開關

PGA--一個可程式設計的增益放大器或者衰減部件

ADC--模擬訊號到數字訊號轉換器

DAC--數字訊號到模擬訊號轉換器

Switch--一個模擬開關

Input--codec的一個輸出介面

Output--codec的一個輸入介面

Headphone--耳機(和可選的Jack)

Mic--Mic(和可選的Jack)

Line--線路輸入/輸出(和可選的Jack)

Speaker--揚聲器

Supply--被其它部件使用的電源和時鐘供應部件

Pre--特殊的前置部件(在所有其它部件前執行)

Post--特殊的後置部件(在所有其它部件後執行)

(Widgets 定義在include/sound/soc-dapm.h)

widgets經常在codec的driver和machine的driver中被新增.在soc-dapm.h中有一些很方便的巨集可以用來快速的構建codecs和machines 的一系列的DAPM widgets。

大多數的widgets有一個name,register,shift,和invert。一些widgets有一些用於stream名字和kcontrols的多餘的引數。

2.1 Stream Domain Widgets

-------------------------------------

Stream Widgets 和stream power domain相關並且包含ADCs和DACs。

Stream Widgets有如下的格式:

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

注意:stream的名字必須和相應的你snd_soc_codec_dai中codec的stream name 一致

例如。用於HiFi回放和採集的stream widegets

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 wideget 有控制和影響音訊子系統中音訊訊號或音訊路徑的能力。他們有如下的格式:

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

所有widget kcontrols可以用controls和num_controls 成員設定。

比如.mixer widget(首先定義kcontrols)

/*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 widget的name作為mixer元素的名字字首,你可以使用SND_SOC_DAPM_MIXER_NAMED_CTL巨集代替,引數和使用SND_SOC_DAPM_MIXER一樣

2.3 Platform/Machine domain Widgets

--------------------------------------------------

Machine widgets和codec widgets不同的是,它們沒有codec 暫存器位。一個machine widget和每一個可以被獨立的供電的machine音訊控制元件相關(非codec),例如

 --揚聲器放大器

 --Microphone Bias

 --Jack 聯結器

一個machine widget可以有一個可選的回撥函式。

例如,對於一個外部的Mic,Jack connector widget在mic插入的時候使能Mic Bias

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

------------------------

codec power domain 沒有widgets,它被codec DAPM時間處理函式處理。這個處理函式在codec 的powerstate被任何stream event或核心PM 事件改變時呼叫。

2.5 Virtual Widgets

------------------------

一些widgets存在於沒有任何相應軟體電源控制的codec或者machine音訊點陣圖。這種情況下需要建立一個虛擬的widget---一個沒有控制bits的widget。例如

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

這可以被用於在軟體上合併signal paths在一起。

在所有的widget被定義之後,他們可以通過呼叫snd_soc_dapm_new_control()被獨立的新增到DAPM子系統。

3.Codec Widget Interconnections(相互連線)

=================================

Widgets在codec和machine上通過audio paths相互連線。每一個連線必須被定義以便在widgets間建立一個所有

音訊路徑的map

建立codec或者machine音訊系統的是很簡單的,這是因為它需要通過他們的音訊訊號路徑 把widget組合在一起

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

WM8731輸出mixer有3個輸入(源)

 1. Line Bypass Input--------------------線路旁路輸入
 2. DAC (HiFi playback)-----------------DAC(HiFi 回放)
 3. Mic Sidetone Input-------------------Mic 側音輸入
這個例子中每一個輸入都有一個與之相關的kcontrol(在上面定義)並且通過kcontrol name和輸出mixer相連。我們現在可以和用它們的源widgets和destination widget(wrt 音訊訊號)來連線 /* output mixer */
{"Output Mixer", "Line Bypass Switch", "Line Input"},
{"Output Mixer", "HiFi Playback Switch", "DAC"},
{"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},

因此我們有: Destination Widget  <=== Path Name <=== Source Widget

或者: Sink, Path, Source

或者: "Output Mixer" 通過"HiFi Playback Switch"和"DAC"連線

當沒有path name和widgets(比如一個直接的連線)相連線時,我們使用NULL作為path name 通過下列函式來建立連線 snd_soc_dapm_connect_input(codec, sink, path, source);

最後,當所有的widgets和連線已經唄註冊進核心時我們應當呼叫snd_soc_dapm_new_widgets(codec) 這會使得核心遍歷codec和machine,因此內部的DAPM狀態會和物理的machine狀態匹配

3.1 Machine Widget Interconnections
-------------------------------------------------

machine widget的連線建立方式和codec一樣,並且直接將codec的pins連線到machine level 的 widgets 例如。連線speaker 輸出編碼pins到內部的speaker 

/*ext speaker 連線到codec pins LOUT2,ROUT2*/ 

{"Ext Spk", NULL , "ROUT2"},
{"Ext Spk", NULL , "LOUT2"},

 這樣會讓DAPM為已經連線的pin和獨立NC的pin上電和關閉

4 Endpoint Widgets

 ===================

一個endpoint是machine內部一個音訊訊號的開始或者結束點(widget)並且包含codec。例如 

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

當一個codec pin 為NC時,它可以通過如下的函式來標記為沒有使用 snd_soc_dapm_set_endpoint(codec, "Widget Name", 0); 最後一個引數是0時為inactive,是1時為active.這樣這個pin和它的輸入widget不會上電 也不會耗費電量

5 DAPM Widget Events

 ====================

一些widgets可以在PM事件的DAPM core中註冊他們的偏好。 比如.一個有放大器的Speaker註冊一個widget這樣amplifier可以在speaker使用 的時候才上電
/*開啟和關閉amplifier 使用如下的函式*/ 

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;

 }
/*相應的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中檢視支援events的所有widgets

5.1 Event types
---------------------

evevt widgets支援如下的event 型別

/* dapm event types */ 

 #define SND_SOC_DAPM_PRE_PMU        0x1 /* before widget power up */

 #define SND_SOC_DAPM_POST_PMU0x2/* after widget power up */

 #define SND_SOC_DAPM_PRE_PMD        0x4 /* before widget power down */ 

 #define SND_SOC_DAPM_POST_PMD0x8/* after widget power down */ 

 #define SND_SOC_DAPM_PRE_REG   0x10/* before audio path setup */  

 #define SND_SOC_DAPM_POST_REG0x20/* after audio path setup */ 


http://blog.csdn.net/bingqingsuimeng/article/details/8365628