Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1306980
  • 博文数量: 860
  • 博客积分: 425
  • 博客等级: 下士
  • 技术积分: 1464
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-20 19:57
个人简介

对技术执着

文章分类

全部博文(860)

文章存档

2019年(16)

2018年(12)

2015年(732)

2013年(85)

2012年(15)

我的朋友

分类: LINUX

2015-03-14 15:27:07

原文地址:alsa声卡驱动分析小结 作者:nacichan

alsa声卡驱动分析总结



分析只列出部分重要代码,具体请参考linux3.0内核代码。

Alsa架构整体来说十分复杂,但对于驱动移植来说我们仅仅只需要关心ASOC就足够了。

在学习asoc之前我们先了解一些专业术语:

ASoC currently supportsthe three main Digital Audio Interfaces (DAI) found on

SoC controllers and portable audio CODECs today, namelyAC97, I2S and PCM.
ASoC现在支持如今的SoC控制器和便携音频解码器上的三个主要数字音频接口,即AC97,I2S,PCM(与pcm音频格式注意区分,前者是一种音频接口,后者是一种输入声卡的音频格式)。


AC97
AC97
====

  AC97 is a five wire interface commonly found on many PC soundcards. It is
now also popular in many portable devices. This DAI has a reset line and time
multiplexes its data on its SDATA_OUT (playback) and SDATA_IN (capture) lines.
The bit clock (BCLK) is always driven by the CODEC (usually 12.288MHz) and the
frame (FRAME) (usually 48kHz) is always driven by the controller. Each AC97
frame is 21uS long and is divided into 13 time slots.

AC97是一种个人电脑声卡上常见的五线接口。现在在很多便携设备中也很流行。这个数字音频接口有一个复位线,分时在SDATA_OUT(回放)和SDATA_IN(捕获)线上传送数据。位时钟常由解码器驱动(通常是12.288MHz).帧时钟(通常48kHz)总是由控制器驱动。每个AC97帧21uS,并分为13个时间槽。


I2S
I2S
===

I2S is a common 4 wire DAI used in HiFi, STBand portable devices. The Tx and
Rx lines are used for audio transmission,whilst the bit clock (BCLK) and
left/right clock (LRC) synchronise the link.I2S is flexible in that either the
controller or CODEC can drive (master) theBCLK and LRC clock lines. Bit clock
usually varies depending on the sample rateand the master system clock
(SYSCLK). LRCLK is the same as the samplerate. A few devices support separate
ADC and DAC LRCLKs, this allows forsimultaneous capture and playback at
different sample rates.
I2S是一个4线数字音频接口,常用于HiFiSTB便携设备。Tx  Rx信号线用于音频传输。而位时钟和左右时钟(LRC)用于同步链接。I2S具有灵活性,因为控制器和解码器都可以控制位时钟和左右时钟。位时钟因采样率和主系统时钟而有不同。LRCLK与采样率相同。少数设备支持独立的ADCDACLRCLK。这使在不同采样率情况下同步捕获和回放成为可能。

I2S has several different operating modes:-
I2S有几个不同的操作模式

o I2S - MSB is transmitted on the falling edgeof the first BCLK after LRC
        transition.
I2S模式-MSBLRC后的第一个位时钟的下降沿传送。

o Left Justified - MSB is transmitted ontransition of LRC.
左对齐模式:MSBLRC传送时传送。

o Right Justified - MSB is transmitted samplesize BCLKs before LRC
                    transition.
右对齐模式:MSB在(此句不懂)

PCM
PCM
===

PCM is another 4 wire interface, very similarto I2S, which can support a more
flexible protocol. It has bit clock (BCLK) andsync (SYNC) lines that are used
to synchronise the link whilst the Tx and Rxlines are used to transmit and
receive the audio data. Bit clock usuallyvaries depending on sample rate
whilst sync runs at the sample rate. PCM alsosupports Time Division
Multiplexing (TDM) in that several devices canuse the bus simultaneously (this
is sometimes referred to as network mode).
PCM也是一种4线制接口。与I2S非常相像,但支持一个更灵活的协议。它有位时钟(BCLK)和同步时钟SYNC)用来在TxRx在传送和接收音频数据是同步连接。位时钟通常因采样率的不同而不同,然而同步时钟(SYNC)与采样频率相同。PCM同样支持时分复用,可以几个设备同时使用总线(这有时被称为network模式)。

Common PCM operating modes:-
常用的PCM操作模式:

o Mode A - MSB is transmitted on falling edgeof first BCLK after FRAME/SYNC.
模式AMSBFRAME/SYNC后第一个BCLK的下降沿传送。

o Mode B - MSB is transmitted on rising edgeof FRAME/SYNC.
模式BMSBFRAME/SYNC的上升沿传送。

 

Codec(解码器)

各解码器驱动必须提供如下特性
1) Codec DAI and PCM configuration
2) Codec control IO - using I2C, 3 Wire(SPI)or both APIs
3) Mixers and audio controls
4) Codec audio operations
1)解码器数字音频接口和PCM配置。
2)解码器控制IO-使用I2C3总线(SPI)或两个都有。
3)混音器和音频控制。
4)解码器音频操作。


Optionally, codec drivers can also provide:-
解码器驱动可以选择性提供:

5) DAPM description.
6) DAPM event handler.
7) DAC Digital mute control.
5)动态音频电源管理描述。
6)动态音频电源管理事件控制。
7)数模转换数字消音控制。

SoC DAI Drivers
板级DAI驱动
===============

Each SoC DAI driver must provide the followingfeatures:-
每个SoC DAI驱动都必须提供如下性能:

1) Digital audio interface (DAI) description
1)数字音频接口描述
2) Digital audio interface configuration
2)数字音频接口配置
3) PCM's description
3PCM描述
4) SYSCLK configuration
4)系统时钟配置
5) Suspend and resume (optional)
5)挂起和恢复(可选的)

以上由君子翻译,本人实在没办法比他描述的更好了,所以把重要的部分提取出来直接copy。在这里对君子表示由衷感谢,赋上君子注。

君子注:
您现在所阅读的,是君子阅读Linux音频SoC驱动时,写下的文档译文。
君子写些译文,一方面是作为自己的笔记,帮助记忆,另一方面也希望能对他人有所帮助。
如果您能于君子的译文中有所收获,则吾心甚慰

 

现在我们开始分析ASOC

ASoC被分为MachinePlatformCodec三大部分。其中的Machine驱动负责PlatformCodec之间的耦合和设备或板子特定的代码。

看起来挺复杂,其实需要我们做的事情并不多,大部分内核已经完成。下面我们分析哪些是我们需要自己做的:

codec驱动:负责音频解码。这部分代码完全无平台无关,设备原厂提供,我们只需要把它加进内核编译就好了。

platform驱动:与处理器芯片相关,这部分代码在该芯片商用之前方案产商提供的demo板已完全确定了,也就是说我们只需要使用就可以了。

machine驱动:好了,到了最关键的地方了,machine驱动是耦合platformcodec驱动,同时与上层交互的代码。由于上层是标准的alsa架构,所以下层接口肯定要做了统一,所以我很负责的告诉你,这部分由machine本身的platform驱动和platform设备组成(请跟asocplatform驱动区别)platform驱动内核帮我们完成了,所以你无须过多的关心你的驱动怎么跟上层alsa怎么衍接的问题,我们只需要注册一个machineplatform设备以及完成platformcodec耦合就ok

asoc的关系图如下:(以下适应于linux3.0linux2.6会有所不同)    

                                                                        

上图把asoc架构显示的淋漓尽致,如果你分析了asoc你就会发现上图描述的结构以及函数真的一个都跑不了。

 

Machie

Machine platform device:(~/sound/soc/samsung/smdk_wm8994.c)

  1. smdk_snd_device = platform_device_alloc("soc-audio", -1);  
  2.     if (!smdk_snd_device)  
  3.         return -ENOMEM;  
  4.   
  5.     platform_set_drvdata(smdk_snd_device, &smdk);  
  6.   
  7.     ret = platform_device_add(smdk_snd_device);  

                                                   

  1. static struct snd_soc_dai_link smdk_dai[] = {  
  2.     { /* Primary DAI i/f */  
  3.         .name = "WM8994 AIF1",  
  4.         .stream_name = "Pri_Dai",  
  5.         .cpu_dai_name = "samsung-i2s.0",  
  6.         .codec_dai_name = "wm8994-aif1",  
  7.         .platform_name = "samsung-audio",  
  8.         .codec_name = "wm8994-codec",  
  9.         .init = smdk_wm8994_init_paiftx,  
  10.         .ops = &smdk_ops,  
  11.     }, { /* Sec_Fifo Playback i/f */  
  12.         .name = "Sec_FIFO TX",  
  13.         .stream_name = "Sec_Dai",  
  14.         .cpu_dai_name = "samsung-i2s.4",  
  15.         .codec_dai_name = "wm8994-aif1",  
  16.         .platform_name = "samsung-audio",  
  17.         .codec_name = "wm8994-codec",  
  18.         .ops = &smdk_ops,  
  19.     },  
  20. };  
  21.   
  22. static struct snd_soc_card smdk = {  
  23.     .name = "SMDK-I2S",  
  24.     .owner = THIS_MODULE,  
  25.     .dai_link = smdk_dai,  
  26.     .num_links = ARRAY_SIZE(smdk_dai),  
  27. };  

 

通过snd_soc_card结构,又引出了Machine驱动的另外两个个数据结构:

  • snd_soc_dai_link(实例:smdk_dai[] )
  • snd_soc_ops(实例:smdk_ops )

snd_soc_dai_link看名字就知道,很明显它是起耦合链接作用的。它指定了PlatformCodeccodec_daicpu_dai的名字,稍后Machine驱动将会利用这些名字去匹配已经在系统中注册的platformcodecdai

snd_soc_ops连接PlatformCodecdai_link对应的ops操作函数,本例就是smdk_ops,它只实现了hw_params函数:smdk_hw_params

到此为止,最主要的部分machine的平台设备注册我们完成了。

 

下面我们关注machine平台驱动部分(这部分内核不需要我们实现,但我们需要知道它是怎么工作的)

ASoCplatform_driver在以下文件中定义:sound/soc/soc-core.c

还是先从模块的入口看起:

[cpp] view plaincopy

  1. static int __init snd_soc_init(void)  
  2. {  
  3.     ......  
  4.     return platform_driver_register(&soc_driver);  
  5. }  

soc_driver的定义如下:

[cpp] view plaincopy

  1. /* ASoC platform driver */  
  2. static struct platform_driver soc_driver = {  
  3.     .driver     = {  
  4.         .name       = "soc-audio", //确保你注册machine平台设备和它保持一致
  5.         .owner      = THIS_MODULE,  
  6.         .pm     = &soc_pm_ops,  
  7.     },  
  8.     .probe      = soc_probe,  
  9.     .remove     = soc_remove,  
  10. };  

 初始化入口soc_probe()

soc_probe函数本身很简单,它先从platform_device参数中取出snd_soc_card,然后调用snd_soc_register_card,通过snd_soc_register_card,为snd_soc_pcm_runtime数组申请内存,每一个dai_link对应snd_soc_pcm_runtime数组的一个单元,然后把snd_soc_card中的dai_link配置复制到相应的snd_soc_pcm_runtime中,最后,大部分的工作都在snd_soc_instantiate_card中实现,下面就看看snd_soc_instantiate_card做了些什么:

该函数首先利用card->instantiated来判断该卡是否已经实例化,如果已经实例化则直接返回,否则遍历每一对dai_link,进行codecplatformdai的绑定工作,下只是代码的部分选节,详细的代码请直接参考完整的代码树。

static int soc_probe(struct platform_device *pdev)
{
       struct snd_soc_card *card =platform_get_drvdata(pdev);//别忘记了machineplatform_set_drvdata //取出snd_soc_card
       .............
       ret =snd_soc_register_card(card);//注册

        ............
       }

下面我们看snd_soc_register_card()函数:

int snd_soc_register_card(struct snd_soc_card *card)
{
。。。。。。。。
       card->rtd =kzalloc(sizeof(struct snd_soc_pcm_runtime) *
                        (card->num_links + card->num_aux_devs),
                        GFP_KERNEL);//snd_soc_pcm_runtime数组申请内存,每一个dai_link对应一个snd_soc_pcm_runtime数组单元
。。。。。。。。
       for (i = 0; i num_links; i++)
              card->rtd[i].dai_link= &card->dai_link[i];//snd_soc_card中的dai_link复制到相应的snd_soc_pcm_runtime

。。。。。。。。

       snd_soc_instantiate_cards();//将调用snd_soc_instantiate_card()//最为重要
}

下面我们分析snd_soc_instantiate_card()函数:

static void snd_soc_instantiate_card(struct snd_soc_card*card)
{
。。。。。。。。
       if (card->instantiated) {  //判断该卡是否已经实例化,如果是就返回
              mutex_unlock(&card->mutex);
              return;
       }
       /* bind DAIs */
       for (i = 0; i num_links; i++) //否则遍例每一对dai_link,进行codec,flatrom,dai的绑定工作
              soc_bind_dai_link(card,i);
/*******************************************************************************************************************************************************************

ASoC定义了三个全局的链表头变量:codec_listdai_listplatform_list,系统中所有的CodecDAIPlatform都在注册时连接到这三个全局链表上。soc_bind_dai_link函数逐个扫描这三个链表,根据card->dai_link[]中的名称进行匹配,匹配后把相应的codecdaiplatform实例赋值到card->rtd[]中(snd_soc_pcm_runtime)。经过这个过程后,snd_soc_pcm_runtime:(card->rtd)中保存了本Machine中使用的CodecDAIPlatform驱动的信息。

*****************************************************************************************************************************************************************/

       /* bind completed ? */
       if (card->num_rtd !=card->num_links) {
              mutex_unlock(&card->mutex);
              return;
       }
。。。。。。。。。
       /* card bind complete soregister a sound card */
       ret =snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
                     card->owner,0, &card->snd_card);

。。。。。。。。。
       card->snd_card->dev =card->dev;
       card->dapm.bias_level =SND_SOC_BIAS_OFF;
       card->dapm.dev =card->dev;
       card->dapm.card = card;
       list_add(&card->dapm.list,&card->dapm_list);//初始化codec缓存,创建声卡实例

。。。。。。。。。。。。。

//下面是最重要的probe匹配工作
       if (card->probe) {
              ret =card->probe(card);
              if (ret < 0)
                     gotocard_probe_error;
       }
 。。。。。。。。。
              ret =soc_probe_dai_link(card, i, order);//主要的耦合链接工作在此函数完成
。。。。。。。。。
              ret =soc_probe_aux_dev(card, i);
。。。。。。。。。

       if (card->late_probe) {//最后的声卡初始化工作,
              ret =card->late_probe(card);
       }
。。。。。。。。。
       ret =snd_card_register(card->snd_card);//然后调用标准的alsa驱动的声卡函数进行声卡注册

。。。。。。。。。
       }
到这里声卡已经注册好了,声卡可以正常工作了,我们再分析最后一个函数,也就是它们是怎么匹配的 soc_probe_dai_link():

static int soc_probe_dai_link(struct snd_soc_card *card,int num, int order)
{
。。。。。。。。。。
       /* probe the cpu_dai */
       if (!cpu_dai->probed&&
                     cpu_dai->driver->probe_order== order) {
              if(!try_module_get(cpu_dai->dev->driver->owner))
                     return -ENODEV;


              if(cpu_dai->driver->probe) {
                     ret =cpu_dai->driver->probe(cpu_dai);
                     if (ret < 0){
                            printk(KERN_ERR"asoc: failed to probe CPU DAI %s\n",
                                          cpu_dai->name);
                            module_put(cpu_dai->dev->driver->owner);
                            returnret;
                     }
              }
              cpu_dai->probed = 1;
              /* mark cpu_dai asprobed and add to card dai list */
              list_add(&cpu_dai->card_list,&card->dai_dev_list);
       }


       /* probe the CODEC */
       if (!codec->probed&&
                     codec->driver->probe_order== order) {
              ret =soc_probe_codec(card, codec);
              if (ret < 0)
                     return ret;
       }


       /* probe the platform */
       if (!platform->probed&&
                     platform->driver->probe_order== order) {
              ret = soc_probe_platform(card,platform);
              if (ret < 0)
                     return ret;
       }


       /* probe the CODEC DAI */
       if (!codec_dai->probed&& codec_dai->driver->probe_order == order) {
              if(codec_dai->driver->probe) {
                     ret =codec_dai->driver->probe(codec_dai);
                     if (ret < 0){
                            printk(KERN_ERR"asoc: failed to probe CODEC DAI %s\n",
                                          codec_dai->name);
                            returnret;
                     }
              }


              /* mark codec_dai asprobed and add to card dai list */
              codec_dai->probed =1;
              list_add(&codec_dai->card_list,&card->dai_dev_list);
       }


       /* complete DAI probe duringlast probe */
       if (order !=SND_SOC_COMP_ORDER_LAST)
              return 0;


。。。。。。。。。。
       /* create the pcm */
       ret = soc_new_pcm(rtd, num);//如果上面都匹配成功将创建标准的alsapcm逻辑设备
。。。。。。。。。。
       }
好了,到此为止我们最主要的部分machine部分分析完成了。

接着是codec驱动部分:

  Codec简介

在移动设备中,Codec的作用可以归结为4种,分别是:

  • 对PCM等信号进行D/A转换,把数字的音频信号转换为模拟信号
  • 对Mic、Linein或者其他输入源的模拟信号进行A/D转换,把模拟的声音信号转变CPU能够处理的数字信号
  • 对音频通路进行控制,比如播放音乐,收听调频收音机,又或者接听电话时,音频信号在codec内的流通路线是不一样的
  • 对音频信号做出相应的处理,例如音量控制,功率放大,EQ控制等等

ASoCCodec的这些功能都定义好了一些列相应的接口,以方便地对Codec进行控制。ASoCCodec驱动的一个基本要求是:驱动程序的代码必须要做到平台无关性,以方便同一个Codec的代码不经修改即可用在不同的平台上。以下的讨论基于wolfsonCodec芯片WM8994kernel的版本3.3.x

描述Codec的最主要的几个数据结构分别是:snd_soc_codecsnd_soc_codec_driversnd_soc_daisnd_soc_dai_driver,其中的snd_soc_daisnd_soc_dai_driverASoCPlatform驱动中也会使用到,PlatformCodecDAI通过snd_soc_dai_link结构,在Machine驱动中进行绑定连接。

 

  Codec的注册

因为Codec驱动的代码要做到平台无关性,要使得Machine驱动能够使用该CodecCodec驱动的首要任务就是确定snd_soc_codecsnd_soc_dai的实例,并把它们注册到系统中,注册后的codecdai才能为Machine驱动所用。以WM8994为例,对应的代码位置:/sound/soc/codecs/wm8994.c,模块的入口函数注册了一个platform driver

[html] view plaincopy

1.     static struct platform_driver wm8994_codec_driver = {  

2.         .driver = {  

3.                .name = "wm8994-codec",  //注意machine device里面和这里保持一致

4.                .owner = THIS_MODULE,  

5.                },  

6.         .probe = wm8994_probe,  

7.         .remove = __devexit_p(wm8994_remove),  

8.     };  

9.       

10.  module_platform_driver(wm8994_codec_driver);  

platform driver,必定会有相应的platform deviceplatform device其实在我们之前讲过的machine device注册时已经引入了。


[html]
 view plaincopy

  1. static int __devinit wm8994_probe(struct platform_device *pdev)  
  2. {  
  3.     return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wm8994,  
  4.             wm8994_dai, ARRAY_SIZE(wm8994_dai));  
  5. }  

其中,soc_codec_dev_wm8994wm8994_dai的定义如下(代码中定义了3dai,这里只列出第一个):

[html] view plaincopy

  1. static struct snd_soc_codec_driver soc_codec_dev_wm8994 = {  
  2.     .probe =    wm8994_codec_probe,  
  3.     .remove =   wm8994_codec_remove,  
  4.     .suspend =  wm8994_suspend,  
  5.     .resume =   wm8994_resume,  
  6.     .set_bias_level = wm8994_set_bias_level,  
  7.     .reg_cache_size = WM8994_MAX_REGISTER,  
  8.     .volatile_register = wm8994_soc_volatile,  
  9. };  

[html] view plaincopy

  1. static struct snd_soc_dai_driver wm8994_dai[] = {  
  2.     {  
  3.         .name = "wm8994-aif1",  
  4.         .id = 1,  
  5.         .playback = {  
  6.             .stream_name = "AIF1 Playback",  
  7.             .channels_min = 1,  
  8.             .channels_max = 2,  
  9.             .rates = WM8994_RATES,  
  10.             .formats = WM8994_FORMATS,  
  11.         },  
  12.         .capture = {  
  13.             .stream_name = "AIF1 Capture",  
  14.             .channels_min = 1,  
  15.             .channels_max = 2,  
  16.             .rates = WM8994_RATES,  
  17.             .formats = WM8994_FORMATS,  
  18.          },  
  19.         .ops = &wm8994_aif1_dai_ops,  
  20.     },  
  21.     ......  
  22. }  

可见,Codec驱动的第一个步骤就是定义snd_soc_codec_driversnd_soc_dai_driver的实例,然后调用snd_soc_register_codec函数对Codec进行注册。

snd_soc_register_codec()函数是machine driver提供的,只要注册成功后codec提供的操作函数就能正常提供给machinedriver使用了。

int snd_soc_register_codec(struct device *dev,
                       const struct snd_soc_codec_driver *codec_drv,
                       struct snd_soc_dai_driver *dai_drv,
                        intnum_dai)

{
       codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
       。。。。。。。。。。。
       /* create CODEC component name */
       codec->name = fmt_single_name(dev, &codec->id);/*Machine驱动定义的snd_soc_dai_link中会指定每个linkcodecdai的名字,进行匹配绑定时就是通过和这里的名字比较,从而找到该Codec*/ 
//
     然后初始化它的各个字段,多数字段的值来自上面定义的snd_soc_codec_driver的实例soc_codec_dev_wm8994
       codec->write = codec_drv->write;
       codec->read = codec_drv->read;
       codec->volatile_register = codec_drv->volatile_register;
       codec->readable_register = codec_drv->readable_register;
       codec->writable_register = codec_drv->writable_register;
       codec->dapm.bias_level = SND_SOC_BIAS_OFF;
       codec->dapm.dev = dev;
       codec->dapm.codec = codec;
       codec->dapm.seq_notifier = codec_drv->seq_notifier;
       codec->dev = dev;
       codec->driver = codec_drv;
       codec->num_dai = num_dai;
       mutex_init(&codec->mutex);


       /* allocate CODEC register cache */
       if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
              reg_size = codec_drv->reg_cache_size *codec_drv->reg_word_size;
              codec->reg_size = reg_size;
              /* it is necessary to make a copy of the default registercache
               * because in the case of using a compression type thatrequires
               * the default register cache to be marked as__devinitconst the
               * kernel might have freed the array by the time weinitialize
               * the cache.
               */
              if (codec_drv->reg_cache_default) {
                     codec->reg_def_copy =kmemdup(codec_drv->reg_cache_default,
                                                reg_size, GFP_KERNEL);
                     if (!codec->reg_def_copy) {
                            ret = -ENOMEM;
                            goto fail;
                     }
              }
       }

。。。。。。。。。。。
       /* register any DAIs */
       if (num_dai) {
              ret = snd_soc_register_dais(dev, dai_drv, num_dai);//通过snd_soc_register_dais函数对本Codecdai进行注册
              if (ret < 0)
                     goto fail;
       }


       mutex_lock(&client_mutex);
       list_add(&codec->list, &codec_list);/*最后,它把codec实例链接到全局链表codec_list中,并且调用snd_soc_instantiate_cards是函数触发Machine驱动进行一次匹配绑定操作*/ 
       snd_soc_instantiate_cards();
       mutex_unlock(&client_mutex);
。。。。。。。。。
}

 

好了,在这里我们的codec驱动也分析完了,其实这部分都是与平台无关代码,一般也不需要改动,这部分我们从设备原厂拿到代码后丢上去就可以了,只是我们在写machine device的时候要注意和这里的名字匹配。

接下来是asocplatform驱动:

Platform驱动的主要作用是完成音频数据的管理,最终通过CPU的数字音频接口(DAI)把音频数据传送给Codec进行处理,最终由Codec输出驱动耳机或者是喇叭的音信信号。在具体实现上,ASoC有把Platform驱动分为两个部分:snd_soc_platform_driversnd_soc_dai_driver。其中,platform_driver负责管理音频数据,把音频数据通过dma或其他操作传送至cpu dai中,dai_driver则主要完成cpu一侧的dai的参数配置,同时也会通过一定的途径把必要的dma等参数与snd_soc_platform_driver进行交互。

  snd_soc_platform_driver的注册

通常,ASoCsnd_soc_platform_driver注册为一个系统的platform_driver,不要被这两个想像的术语所迷惑,前者只是针对ASoC子系统的,后者是来自Linux的设备驱动模型。我们要做的就是:

  • 定义一个snd_soc_platform_driver结构的实例;
  • 在platform_driver的probe回调中利用ASoC的API:snd_soc_register_platform()注册上面定义的实例;
  • 实现snd_soc_platform_driver中的各个回调函数;

kernel3.3中的/sound/soc/samsung/dma.c为例:

[cpp] view plaincopy

1.     static struct snd_soc_platform_driver samsung_asoc_platform = {  

2.         .ops        = &dma_ops,  

3.         .pcm_new    = dma_new,  

4.         .pcm_free   = dma_free_dma_buffers,  

5.     };  

6.       

7.     static int __devinit samsung_asoc_platform_probe(struct platform_device *pdev)  

8.     {  

9.         return snd_soc_register_platform(&pdev->dev, &samsung_asoc_platform);  

10.  }  

11.    

12.  static int __devexit samsung_asoc_platform_remove(struct platform_device *pdev)  

13.  {  

14.      snd_soc_unregister_platform(&pdev->dev);  

15.      return 0;  

16.  }  

17.    

18.  static struct platform_driver asoc_dma_driver = {  

19.      .driver = {  

20.          .name = "samsung-audio",  

21.          .owner = THIS_MODULE,  

22.      },  

23.    

24.      .probe = samsung_asoc_platform_probe,  

25.      .remove = __devexit_p(samsung_asoc_platform_remove),  

26.  };  

27.    

28.  module_platform_driver(asoc_dma_driver);  

snd_soc_register_platform() 该函数用于注册一个snd_soc_platform,只有注册以后,它才可以被Machine驱动使用。它的代码已经清晰地表达了它的实现过程:

  • 为snd_soc_platform实例申请内存;
  • 从platform_device中获得它的名字,用于Machine驱动的匹配工作;
  • 初始化snd_soc_platform的字段;
  • 把snd_soc_platform实例连接到全局链表platform_list中;
  • 调用snd_soc_instantiate_cards,触发声卡的machine、platform、codec、dai等的匹配工作;

  cpusnd_soc_daidriver驱动的注册

dai驱动通常对应cpu的一个或几个I2S/PCM接口,与snd_soc_platform一样,dai驱动也是实现为一个platform driver,实现一个dai驱动大致可以分为以下几个步骤:

  • 定义一个snd_soc_dai_driver结构的实例;
  • 在对应的platform_driver中的probe回调中通过API:snd_soc_register_dai或者snd_soc_register_dais,注册snd_soc_dai实例;
  • 实现snd_soc_dai_driver结构中的probe、suspend等回调;
  • 实现snd_soc_dai_driver结构中的snd_soc_dai_ops字段中的回调函数;

snd_soc_register_dai 这个函数在上一篇介绍codec驱动的博文中已有介绍

 

具体不再分析,这个驱动也不需要用户做任务修改,所以只要知道它的作用就已经够了。就像电话机一样,我们只要知道电话怎么打就够了,至于它怎么连接我们并不太需要关心。

阅读(452) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~