前面的六篇文章,我们已经讨论了dapm关于动态电源管理的有关知识,包括widget的创建和初始化,widget之间的连接以及widget的上下电顺序等等。本章我们准备讨论dapm框架中的另一个机制:事件机制。通过dapm事件机制,widget可以对它所关心的dapm事件做出反应,这种机制对于扩充widget的能力非常有用,例如,对于那些位于codec之外的widget,好像喇叭功放、外部的前置放大器等等,由于不是使用codec内部的寄存器进行电源控制,我们就必须利用dapm的事件机制,获得相应的上下电事件,从而可以定制widget自身的电源控制功能。
/*****************************************************************************************************/
声明:本博内容均由http://blog.csdn.net/droidphone原创,转载请注明出处,谢谢!
/*****************************************************************************************************/
dapm event的种类
dapm目前为我们定义了9种dapm event,他们分别是:
事件类型 | 说明 |
---|---|
SND_SOC_DAPM_PRE_PMU | widget要上电前发出的事件 |
SND_SOC_DAPM_POST_PMU | widget要上电后发出的事件 |
SND_SOC_DAPM_PRE_PMD | widget要下电前发出的事件 |
SND_SOC_DAPM_POST_PMD | widget要下电后发出的事件 |
SND_SOC_DAPM_PRE_REG | 音频路径设置之前发出的事件 |
SND_SOC_DAPM_POST_REG | 音频路径设置之后发出的事件 |
SND_SOC_DAPM_WILL_PMU | 在处理up_list链表之前发出的事件 |
SND_SOC_DAPM_WILL_PMD | 在处理down_list链表之前发出的事件 |
SND_SOC_DAPM_PRE_POST_PMD | SND_SOC_DAPM_PRE_PMD和 SND_SOC_DAPM_POST_PMD的合并 |
widget的event回调函数
ALSA声卡驱动中的DAPM详解之二:widget-具备路径和电源管理信息的kcontrol中,我们已经介绍过代表widget的snd_soc_widget结构,在这个结构体中,有一个event字段用于保存该widget的事件回调函数,同时,event_flags字段用于保存该widget需要关心的dapm事件种类,只有event_flags字段中相应的事件位被设置了的事件才会发到event回调函数中进行处理。
我们知道,dapm为我们提供了常用widget的定义辅助宏,使用以下这几种辅助宏定义widget时,默认需要我们提供dapm event回调函数
- SND_SOC_DAPM_MIC
- SND_SOC_DAPM_HP
- SND_SOC_DAPM_SPK
- SND_SOC_DAPM_LINE
- /* 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);
- SND_SOC_DAPM_PGA_E
- SND_SOC_DAPM_OUT_DRV_E
- SND_SOC_DAPM_MIXER_E
- SND_SOC_DAPM_MIXER_NAMED_CTL_E
- SND_SOC_DAPM_SWITCH_E
- SND_SOC_DAPM_MUX_E
- SND_SOC_DAPM_VIRT_MUX_E
触发dapm event
我们已经定义好了带有event回调的widget,那么,在那里触发这些dapm event?答案是:在dapm_power_widgets函数的处理过程中,dapm_power_widgets函数我们已经在ALSA声卡驱动中的DAPM详解之六:精髓所在,牵一发而动全身中做了详细的分析,其中,在所有需要处理电源变化的widget被分别放入up_list和down_list链表后,会相应地发出各种dapm事件:
- static int dapm_power_widgets(struct snd_soc_card *card, int event)
- {
- ......
- list_for_each_entry(w, &down_list, power_list) {
- dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
- }
- list_for_each_entry(w, &up_list, power_list) {
- dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
- }
- /* Power down widgets first; try to avoid amplifying pops. */
- dapm_seq_run(card, &down_list, event, false);
- dapm_widget_update(card);
- /* Now power up. */
- dapm_seq_run(card, &up_list, event, true);
- ......
- }
- static void dapm_seq_run_coalesced(struct snd_soc_card *card,
- struct list_head *pending)
- {
- ......
- list_for_each_entry(w, pending, power_list) {
- ......
- /* Check for events */
- dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
- dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
- }
- if (reg >= 0) {
- ......
- pop_wait(card->pop_time);
- soc_widget_update_bits_locked(w, reg, mask, value);
- }
- list_for_each_entry(w, pending, power_list) {
- dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
- dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
- }
- }
- static void dapm_widget_update(struct snd_soc_card *card)
- {
- struct snd_soc_dapm_update *update = card->update;
- struct snd_soc_dapm_widget_list *wlist;
- struct snd_soc_dapm_widget *w = NULL;
- unsigned int wi;
- int ret;
- if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
- return;
- wlist = dapm_kcontrol_get_wlist(update->kcontrol);
- for (wi = 0; wi < wlist->num_widgets; wi++) {
- w = wlist->widgets[wi];
- if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
- ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
- ......
- }
- }
- ......
- /* 更新kcontrol的值,改变音频路径 */
- ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
- update->val);
- ......
- for (wi = 0; wi < wlist->num_widgets; wi++) {
- w = wlist->widgets[wi];
- if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
- ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
- ......
- }
- }
- }
dai widget与stream widget
dai widget 在ALSA声卡驱动中的DAPM详解之四:在驱动程序中初始化并注册widget和route一文中,我们已经讨论过dai widget,dai widget又分为cpu dai widget和codec dai widget,它们在machine驱动分别匹配上相应的codec和platform后,由soc_probe_platform和soc_probe_codec这两个函数通过调用dapm的api函数:
- snd_soc_dapm_new_dai_widgets
- snd_soc_dapm_dai_in 对应playback dai
- snd_soc_dapm_dai_out 对应capture dai
- snd_soc_dapm_aif_in 用SND_SOC_DAPM_AIF_IN辅助宏定义
- snd_soc_dapm_aif_out 用SND_SOC_DAPM_AIF_OUT辅助宏定义
- snd_soc_dapm_dac 用SND_SOC_DAPM_AIF_DAC辅助宏定义
- snd_soc_dapm_adc 用SND_SOC_DAPM_AIF_ADC辅助宏定义
连接dai widget和stream widget
- static int snd_soc_instantiate_card(struct snd_soc_card *card)
- {
- ......
- /* card bind complete so register a sound card */
- ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
- card->owner, 0, &card->snd_card);
- ......
- if (card->dapm_widgets)
- snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
- card->num_dapm_widgets);
- /* 建立dai widget和stream widget之间的连接关系 */
- snd_soc_dapm_link_dai_widgets(card);
- ......
- if (card->controls)
- snd_soc_add_card_controls(card, card->controls, card->num_controls);
- ......
- if (card->dapm_routes)
- snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
- card->num_dapm_routes);
- ......
- if (card->fully_routed)
- list_for_each_entry(codec, &card->codec_dev_list, card_list)
- snd_soc_dapm_auto_nc_codec_pins(codec);
- snd_soc_dapm_new_widgets(card);
- ret = snd_card_register(card->snd_card);
- ......
- return 0;
- }
- int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
- {
- struct snd_soc_dapm_widget *dai_w, *w;
- struct snd_soc_dai *dai;
- /* For each DAI widget... */
- list_for_each_entry(dai_w, &card->widgets, list) {
- switch (dai_w->id) {
- case snd_soc_dapm_dai_in:
- case snd_soc_dapm_dai_out:
- break;
- default:
- continue;
- }
- dai = dai_w->priv;
- /* ...find all widgets with the same stream and link them */
- list_for_each_entry(w, &card->widgets, list) {
- if (w->dapm != dai_w->dapm)
- continue;
- switch (w->id) {
- case snd_soc_dapm_dai_in:
- case snd_soc_dapm_dai_out:
- continue;
- default:
- break;
- }
- if (!w->sname || !strstr(w->sname, dai_w->name))
- continue;
- if (dai->driver->playback.stream_name &&
- strstr(w->sname,
- dai->driver->playback.stream_name)) {
- dev_dbg(dai->dev, "%s -> %s\n",
- dai->playback_widget->name, w->name);
- snd_soc_dapm_add_path(w->dapm,
- dai->playback_widget, w, NULL, NULL);
- }
- if (dai->driver->capture.stream_name &&
- strstr(w->sname,
- dai->driver->capture.stream_name)) {
- dev_dbg(dai->dev, "%s -> %s\n",
- w->name, dai->capture_widget->name);
- snd_soc_dapm_add_path(w->dapm, w,
- dai->capture_widget, NULL, NULL);
- }
- }
- }
- return 0;
- SND_SOC_DAPM_AIF_OUT("AIFOUTL", "Capture", 0, SND_SOC_NOPM, 0, 0),
- SND_SOC_DAPM_AIF_OUT("AIFOUTR", "Capture", 1, SND_SOC_NOPM, 0, 0),
- SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
- SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
- static struct snd_soc_dai_driver wm8993_dai = {
- .name = "wm8993-hifi",
- .playback = {
- .stream_name = "Playback",
- .channels_min = 1,
- .channels_max = 2,
- .rates = WM8993_RATES,
- .formats = WM8993_FORMATS,
- .sig_bits = 24,
- },
- .capture = {
- .stream_name = "Capture",
- .channels_min = 1,
- .channels_max = 2,
- .rates = WM8993_RATES,
- .formats = WM8993_FORMATS,
- .sig_bits = 24,
- },
- .ops = &wm8993_ops,
- .symmetric_rates = 1,
- };
- SND_SOC_DAPM_ADC("ADCL", NULL, WM8993_POWER_MANAGEMENT_2, 1, 0),
- SND_SOC_DAPM_ADC("ADCR", NULL, WM8993_POWER_MANAGEMENT_2, 0, 0),
- SND_SOC_DAPM_DAC("DACL", NULL, WM8993_POWER_MANAGEMENT_3, 1, 0),
- SND_SOC_DAPM_DAC("DACR", NULL, WM8993_POWER_MANAGEMENT_3, 0, 0),
stream event
把dai widget和stream widget连接在一起,就是为了能把ASoc中的pcm处理部分和dapm进行关联,pcm的处理过程中,会通过发出stream event来通知dapm系统,重新扫描并调整音频路径上各个widget的电源状态,目前dapm提供了以下几种stream event:
- /* dapm stream operations */
- #define SND_SOC_DAPM_STREAM_NOP 0x0
- #define SND_SOC_DAPM_STREAM_START 0x1
- #define SND_SOC_DAPM_STREAM_STOP 0x2
- #define SND_SOC_DAPM_STREAM_SUSPEND 0x4
- #define SND_SOC_DAPM_STREAM_RESUME 0x8
- #define SND_SOC_DAPM_STREAM_PAUSE_PUSH 0x10
- #define SND_SOC_DAPM_STREAM_PAUSE_RELEASE 0x20
- snd_soc_dapm_stream_event(rtd, substream->stream,
- SND_SOC_DAPM_STREAM_START);
- snd_soc_dapm_stream_event(rtd,
- SNDRV_PCM_STREAM_PLAYBACK,
- SND_SOC_DAPM_STREAM_STOP);
- static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
- int event)
- {
- struct snd_soc_dapm_widget *w_cpu, *w_codec;
- struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
- struct snd_soc_dai *codec_dai = rtd->codec_dai;
- if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
- w_cpu = cpu_dai->playback_widget;
- w_codec = codec_dai->playback_widget;
- } else {
- w_cpu = cpu_dai->capture_widget;
- w_codec = codec_dai->capture_widget;
- }
- if (w_cpu) {
- dapm_mark_dirty(w_cpu, "stream event");
- switch (event) {
- case SND_SOC_DAPM_STREAM_START:
- w_cpu->active = 1;
- break;
- case SND_SOC_DAPM_STREAM_STOP:
- w_cpu->active = 0;
- break;
- case SND_SOC_DAPM_STREAM_SUSPEND:
- case SND_SOC_DAPM_STREAM_RESUME:
- case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
- case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
- break;
- }
- }
- if (w_codec) {
- dapm_mark_dirty(w_codec, "stream event");
- switch (event) {
- case SND_SOC_DAPM_STREAM_START:
- w_codec->active = 1;
- break;
- case SND_SOC_DAPM_STREAM_STOP:
- w_codec->active = 0;
- break;
- case SND_SOC_DAPM_STREAM_SUSPEND:
- case SND_SOC_DAPM_STREAM_RESUME:
- case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
- case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
- break;
- }
- }
- dapm_power_widgets(rtd->card, event);