Linux ALSA聲卡驅動之七:ASoC架構中的Codec

1.  Codec簡介

在移動設備中,Codec的作用可以歸結爲4種,分別是:

  • 對PCM等信號進行D/A轉換,把數字的音頻信號轉換爲模擬信號
  • 對Mic、Linein或者其他輸入源的模擬信號進行A/D轉換,把模擬的聲音信號轉變CPU能夠處理的數字信號
  • 對音頻通路進行控制,比如播放音樂,收聽調頻收音機,又或者接聽電話時,音頻信號在codec內的流通路線是不一樣的
  • 對音頻信號做出相應的處理,例如音量控制,功率放大,EQ控制等等

ASoC對Codec的這些功能都定義好了一些列相應的接口,以方便地對Codec進行控制。ASoC對Codec驅動的一個基本要求是:驅動程序的代碼必須要做到平臺無關性,以方便同一個Codec的代碼不經修改即可用在不同的平臺上。以下的討論基於wolfson的Codec芯片WM8994,kernel的版本3.3.x。


/*****************************************************************************************************/
聲明:本博內容均由http://blog.csdn.net/droidphone原創,轉載請註明出處,謝謝!
/*****************************************************************************************************/

2.  ASoC中對Codec的數據抽象

描述Codec的最主要的幾個數據結構分別是:snd_soc_codec,snd_soc_codec_driver,snd_soc_dai,snd_soc_dai_driver,其中的snd_soc_dai和snd_soc_dai_driver在ASoC的Platform驅動中也會使用到,Platform和Codec的DAI通過snd_soc_dai_link結構,在Machine驅動中進行綁定連接。下面我們先看看這幾個結構的定義,這裏我只貼出我要關注的字段,詳細的定義請參照:/include/sound/soc.h。
snd_soc_codec:
[html] view plaincopy
  1. /* SoC Audio Codec device */  
  2. struct snd_soc_codec {  
  3.     const char *name;  /* Codec的名字*/  
  4.     struct device *dev; /* 指向Codec設備的指針 */  
  5.     const struct snd_soc_codec_driver *driver; /* 指向該codec的驅動的指針 */  
  6.     struct snd_soc_card *card;    /* 指向Machine驅動的card實例 */  
  7.     int num_dai; /* 該Codec數字接口的個數,目前越來越多的Codec帶有多個I2S或者是PCM接口 */  
  8.     int (*volatile_register)(...);  /* 用於判定某一寄存器是否是volatile */  
  9.     int (*readable_register)(...);  /* 用於判定某一寄存器是否可讀 */  
  10.     int (*writable_register)(...);  /* 用於判定某一寄存器是否可寫 */  
  11.   
  12.     /* runtime */  
  13.     ......  
  14.     /* codec IO */  
  15.     void *control_data; /* 該指針指向的結構用於對codec的控制,通常和read,write字段聯合使用 */  
  16.     enum snd_soc_control_type control_type;/* 可以是SND_SOC_SPI,SND_SOC_I2C,SND_SOC_REGMAP中的一種 */  
  17.     unsigned int (*read)(struct snd_soc_codec *, unsigned int);  /* 讀取Codec寄存器的函數 */  
  18.     int (*write)(struct snd_soc_codec *, unsigned int, unsigned int);  /* 寫入Codec寄存器的函數 */  
  19.     /* dapm */  
  20.     struct snd_soc_dapm_context dapm;  /* 用於DAPM控件 */  
  21. };  

snd_soc_codec_driver:
[html] view plaincopy
  1. /* codec driver */  
  2. struct snd_soc_codec_driver {  
  3.     /* driver ops */  
  4.     int (*probe)(struct snd_soc_codec *);  /* codec驅動的probe函數,由snd_soc_instantiate_card回調 */  
  5.     int (*remove)(struct snd_soc_codec *);    
  6.     int (*suspend)(struct snd_soc_codec *);  /* 電源管理 */  
  7.     int (*resume)(struct snd_soc_codec *);  /* 電源管理 */  
  8.   
  9.     /* Default control and setup, added after probe() is run */  
  10.     const struct snd_kcontrol_new *controls;  /* 音頻控件指針 */  
  11.     const struct snd_soc_dapm_widget *dapm_widgets;  /* dapm部件指針 */  
  12.     const struct snd_soc_dapm_route *dapm_routes;  /* dapm路由指針 */  
  13.   
  14.     /* codec wide operations */  
  15.     int (*set_sysclk)(...);  /* 時鐘配置函數 */  
  16.     int (*set_pll)(...);  /* 鎖相環配置函數 */  
  17.   
  18.     /* codec IO */  
  19.     unsigned int (*read)(...);  /* 讀取codec寄存器函數 */  
  20.     int (*write)(...);  /* 寫入codec寄存器函數 */  
  21.     int (*volatile_register)(...);  /* 用於判定某一寄存器是否是volatile */  
  22.     int (*readable_register)(...);  /* 用於判定某一寄存器是否可讀 */  
  23.     int (*writable_register)(...);  /* 用於判定某一寄存器是否可寫 */  
  24.   
  25.     /* codec bias level */  
  26.     int (*set_bias_level)(...);  /* 偏置電壓配置函數 */  
  27.   
  28. };  
snd_soc_dai:
[html] view plaincopy
  1. /*  
  2.  * Digital Audio Interface runtime data.  
  3.  *  
  4.  * Holds runtime data for a DAI.  
  5.  */  
  6. struct snd_soc_dai {  
  7.     const char *name;  /* dai的名字 */  
  8.     struct device *dev;  /* 設備指針 */  
  9.   
  10.     /* driver ops */  
  11.     struct snd_soc_dai_driver *driver;  /* 指向dai驅動結構的指針 */  
  12.   
  13.     /* DAI runtime info */  
  14.     unsigned int capture_active:1;      /* stream is in use */  
  15.     unsigned int playback_active:1;     /* stream is in use */  
  16.   
  17.     /* DAI DMA data */  
  18.     void *playback_dma_data;  /* 用於管理playback dma */  
  19.     void *capture_dma_data;  /* 用於管理capture dma */  
  20.   
  21.     /* parent platform/codec */  
  22.     union {  
  23.         struct snd_soc_platform *platform;  /* 如果是cpu dai,指向所綁定的平臺 */  
  24.         struct snd_soc_codec *codec;  /* 如果是codec dai指向所綁定的codec */  
  25.     };  
  26.     struct snd_soc_card *card;  /* 指向Machine驅動中的crad實例 */  
  27. };  
snd_soc_dai_driver:
[html] view plaincopy
  1. /*  
  2.  * Digital Audio Interface Driver.  
  3.  *  
  4.  * Describes the Digital Audio Interface in terms of its ALSA, DAI and AC97  
  5.  * operations and capabilities. Codec and platform drivers will register this  
  6.  * structure for every DAI they have.  
  7.  *  
  8.  * This structure covers the clocking, formating and ALSA operations for each  
  9.  * interface.  
  10.  */  
  11. struct snd_soc_dai_driver {  
  12.     /* DAI description */  
  13.     const char *name;  /* dai驅動名字 */  
  14.   
  15.     /* DAI driver callbacks */  
  16.     int (*probe)(struct snd_soc_dai *dai);  /* dai驅動的probe函數,由snd_soc_instantiate_card回調 */  
  17.     int (*remove)(struct snd_soc_dai *dai);    
  18.     int (*suspend)(struct snd_soc_dai *dai);  /* 電源管理 */  
  19.     int (*resume)(struct snd_soc_dai *dai);    
  20.   
  21.     /* ops */  
  22.     const struct snd_soc_dai_ops *ops;  /* 指向本dai的snd_soc_dai_ops結構 */  
  23.   
  24.     /* DAI capabilities */  
  25.     struct snd_soc_pcm_stream capture;  /* 描述capture的能力 */  
  26.     struct snd_soc_pcm_stream playback;  /* 描述playback的能力 */  
  27. };  
snd_soc_dai_ops用於實現該dai的控制盒參數配置:
[html] view plaincopy
  1. struct snd_soc_dai_ops {  
  2.     /*  
  3.      * DAI clocking configuration, all optional.  
  4.      * Called by soc_card drivers, normally in their hw_params.  
  5.      */  
  6.     int (*set_sysclk)(...);  
  7.     int (*set_pll)(...);  
  8.     int (*set_clkdiv)(...);  
  9.     /*  
  10.      * DAI format configuration  
  11.      * Called by soc_card drivers, normally in their hw_params.  
  12.      */  
  13.     int (*set_fmt)(...);  
  14.     int (*set_tdm_slot)(...);  
  15.     int (*set_channel_map)(...);  
  16.     int (*set_tristate)(...);  
  17.     /*  
  18.      * DAI digital mute - optional.  
  19.      * Called by soc-core to minimise any pops.  
  20.      */  
  21.     int (*digital_mute)(...);  
  22.     /*  
  23.      * ALSA PCM audio operations - all optional.  
  24.      * Called by soc-core during audio PCM operations.  
  25.      */  
  26.     int (*startup)(...);  
  27.     void (*shutdown)(...);  
  28.     int (*hw_params)(...);  
  29.     int (*hw_free)(...);  
  30.     int (*prepare)(...);  
  31.     int (*trigger)(...);  
  32.     /*  
  33.      * For hardware based FIFO caused delay reporting.  
  34.      * Optional.  
  35.      */  
  36.     snd_pcm_sframes_t (*delay)(...);  
  37. };  

3.  Codec的註冊

因爲Codec驅動的代碼要做到平臺無關性,要使得Machine驅動能夠使用該Codec,Codec驅動的首要任務就是確定snd_soc_codec和snd_soc_dai的實例,並把它們註冊到系統中,註冊後的codec和dai才能爲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",  
  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 device,這個platform device的來源後面再說,顯然,platform driver註冊後,probe回調將會被調用,這裏是wm8994_probe函數:
[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_wm8994和wm8994_dai的定義如下(代碼中定義了3個dai,這裏只列出第一個):
[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_driver和snd_soc_dai_driver的實例,然後調用snd_soc_register_codec函數對Codec進行註冊。進入snd_soc_register_codec函數看看:
首先,它申請了一個snd_soc_codec結構的實例:
[html] view plaincopy
  1. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);  
確定codec的名字,這個名字很重要,Machine驅動定義的snd_soc_dai_link中會指定每個link的codec和dai的名字,進行匹配綁定時就是通過和這裏的名字比較,從而找到該Codec的!
[html] view plaincopy
  1. /* create CODEC component name */  
  2.     codec->name = fmt_single_name(dev, &codec->id);  
然後初始化它的各個字段,多數字段的值來自上面定義的snd_soc_codec_driver的實例soc_codec_dev_wm8994:
[html] view plaincopy
  1. codec->write = codec_drv->write;  
  2. codec->read = codec_drv->read;  
  3. codec->volatile_register = codec_drv->volatile_register;  
  4. codec->readable_register = codec_drv->readable_register;  
  5. codec->writable_register = codec_drv->writable_register;  
  6. codec->dapm.bias_level = SND_SOC_BIAS_OFF;  
  7. codec->dapm.dev = dev;  
  8. codec->dapm.codec = codec;  
  9. codec->dapm.seq_notifier = codec_drv->seq_notifier;  
  10. codec->dapm.stream_event = codec_drv->stream_event;  
  11. codec->dev = dev;  
  12. codec->driver = codec_drv;  
  13. codec->num_dai = num_dai;  
在做了一些寄存器緩存的初始化和配置工作後,通過snd_soc_register_dais函數對本Codec的dai進行註冊:
[html] view plaincopy
  1. /* register any DAIs */  
  2. if (num_dai) {  
  3.     ret = snd_soc_register_dais(dev, dai_drv, num_dai);  
  4.     if (ret < 0)  
  5.         goto fail;  
  6. }  
最後,它把codec實例鏈接到全局鏈表codec_list中,並且調用snd_soc_instantiate_cards是函數觸發Machine驅動進行一次匹配綁定操作:
[html] view plaincopy
  1. list_add(&codec->list, &codec_list);  
  2. snd_soc_instantiate_cards();  
上面的snd_soc_register_dais函數其實也是和snd_soc_register_codec類似,顯示爲每個snd_soc_dai實例分配內存,確定dai的名字,用snd_soc_dai_driver實例的字段對它進行必要初始化,最後把該dai鏈接到全局鏈表dai_list中,和Codec一樣,最後也會調用snd_soc_instantiate_cards函數觸發一次匹配綁定的操作。

               圖3.1 dai的註冊
關於snd_soc_instantiate_cards函數,請參閱另一篇博文:Linux音頻驅動之六:ASoC架構中的Machine

4.  mfd設備

前面已經提到,codec驅動把自己註冊爲一個platform driver,那對應的platform device在哪裏定義?答案是在以下代碼文件中:/drivers/mfd/wm8994-core.c。

WM8994本身具備多種功能,除了codec外,它還有作爲LDO和GPIO使用,這幾種功能共享一些IO和中斷資源,linux爲這種設備提供了一套標準的實現方法:mfd設備。其基本思想是爲這些功能的公共部分實現一個父設備,以便共享某些系統資源和功能,然後每個子功能實現爲它的子設備,這樣既共享了資源和代碼,又能實現合理的設備層次結構,主要利用到的API就是:mfd_add_devices(),mfd_remove_devices(),mfd_cell_enable(),mfd_cell_disable(),mfd_clone_cell()。

回到wm8994-core.c中,因爲WM8994使用I2C進行內部寄存器的存取,它首先註冊了一個I2C驅動:

[html] view plaincopy
  1. static struct i2c_driver wm8994_i2c_driver = {  
  2.     .driver = {  
  3.         .name = "wm8994",  
  4.         .owner = THIS_MODULE,  
  5.         .pm = &wm8994_pm_ops,  
  6.         .of_match_table = wm8994_of_match,  
  7.     },  
  8.     .probe = wm8994_i2c_probe,  
  9.     .remove = wm8994_i2c_remove,  
  10.     .id_table = wm8994_i2c_id,  
  11. };  
  12.   
  13. static int __init wm8994_i2c_init(void)  
  14. {  
  15.     int ret;  
  16.   
  17.     ret = i2c_add_driver(&wm8994_i2c_driver);  
  18.     if (ret != 0)  
  19.         pr_err("Failed to register wm8994 I2C driver: %d\n", ret);  
  20.   
  21.     return ret;  
  22. }  
  23. module_init(wm8994_i2c_init);  
進入wm8994_i2c_probe()函數,它先申請了一個wm8994結構的變量,該變量被作爲這個I2C設備的driver_data使用,上面已經講過,codec作爲它的子設備,將會取出並使用這個driver_data。接下來,本函數利用regmap_init_i2c()初始化並獲得一個regmap結構,該結構主要用於後續基於regmap機制的寄存器I/O,關於regmap我們留在後面再講。最後,通過wm8994_device_init()來添加mfd子設備:

[html] view plaincopy
  1. static int wm8994_i2c_probe(struct i2c_client *i2c,  
  2.                 const struct i2c_device_id *id)  
  3. {  
  4.     struct wm8994 *wm8994;  
  5.     int ret;  
  6.     wm8994 = devm_kzalloc(&i2c->dev, sizeof(struct wm8994), GFP_KERNEL);  
  7.     i2c_set_clientdata(i2c, wm8994);  
  8.     wm8994->dev = &i2c->dev;  
  9.     wm8994->irq = i2c->irq;  
  10.     wm8994->type = id->driver_data;  
  11.     wm8994->regmap = regmap_init_i2c(i2c, &wm8994_base_regmap_config);  
  12.   
  13.     return wm8994_device_init(wm8994, i2c->irq);  
  14. }  
繼續進入wm8994_device_init()函數,它首先爲兩個LDO添加mfd子設備:
[html] view plaincopy
  1. /* Add the on-chip regulators first for bootstrapping */  
  2. ret = mfd_add_devices(wm8994->dev, -1,  
  3.               wm8994_regulator_devs,  
  4.               ARRAY_SIZE(wm8994_regulator_devs),  
  5.               NULL, 0);  
因爲WM1811,WM8994,WM8958三個芯片功能類似,因此這三個芯片都使用了WM8994的代碼,所以wm8994_device_init()接下來根據不同的芯片型號做了一些初始化動作,這部分的代碼就不貼了。接着,從platform_data中獲得部分配置信息:
[html] view plaincopy
  1. if (pdata) {  
  2.     wm8994->irq_base = pdata->irq_base;  
  3.     wm8994->gpio_base = pdata->gpio_base;  
  4.   
  5.     /* GPIO configuration is only applied if it's non-zero */  
  6.     ......  
  7. }  
最後,初始化irq,然後添加codec子設備和gpio子設備:
[html] view plaincopy
  1. wm8994_irq_init(wm8994);  
  2.   
  3. ret = mfd_add_devices(wm8994->dev, -1,  
  4.               wm8994_devs, ARRAY_SIZE(wm8994_devs),  
  5.               NULL, 0);  
經過以上這些處理後,作爲父設備的I2C設備已經準備就緒,它的下面掛着4個子設備:ldo-0,ldo-1,codec,gpio。其中,codec子設備的加入,它將會和前面所講codec的platform driver匹配,觸發probe回調完成下面所說的codec驅動的初始化工作。

5.  Codec初始化

Machine驅動的初始化,codec和dai的註冊,都會調用snd_soc_instantiate_cards()進行一次聲卡和codec,dai,platform的匹配綁定過程,這裏所說的綁定,正如Machine驅動一文中所描述,就是通過3個全局鏈表,按名字進行匹配,把匹配的codec,dai和platform實例賦值給聲卡每對dai的snd_soc_pcm_runtime變量中。一旦綁定成功,將會使得codec和dai驅動的probe回調被調用,codec的初始化工作就在該回調中完成。對於WM8994,該回調就是wm8994_codec_probe函數:

                                                                   圖5.1  wm8994_codec_probe

  • 取出父設備的driver_data,其實就是上一節的wm8994結構變量,取出其中的regmap字段,複製到codec的control_data字段中;
  • 申請一個wm8994_priv私有數據結構,並把它設爲codec設備的driver_data;
  • 通過snd_soc_codec_set_cache_io初始化regmap io,完成這一步後,就可以使用API:snd_soc_read(),snd_soc_write()對codec的寄存器進行讀寫了;
  • 把父設備的driver_data(struct wm8994)和platform_data保存到私有結構wm8994_priv中;
  • 因爲要同時支持3個芯片型號,這裏要根據芯片的型號做一些特定的初始化工作;
  • 申請必要的幾個中斷;
  • 設置合適的偏置電平;
  • 通過snd_soc_update_bits修改某些寄存器;
  • 根據父設備的platform_data,完成特定於平臺的初始化配置;
  • 添加必要的control,dapm部件進而dapm路由信息;

至此,codec驅動的初始化完成。

5.  regmap-io

我們知道,要想對codec進行控制,通常都是通過讀寫它的內部寄存器完成的,讀寫的接口通常是I2C或者是SPI接口,不過每個codec芯片寄存器的比特位組成都有所不同,寄存器地址的比特位也有所不同。例如WM8753的寄存器地址是7bits,數據是9bits,WM8993的寄存器地址是8bits,數據也是16bits,而WM8994的寄存器地址是16bits,數據也是16bits。在kernel3.1版本,內核引入了一套regmap機制和相關的API,這樣就可以用統一的操作來實現對這些多樣的寄存器的控制。regmap使用起來也相對簡單:
  • 爲codec定義一個regmap_config結構實例,指定codec寄存器的地址和數據位等信息;
  • 根據codec的控制總線類型,調用以下其中一個函數,得到一個指向regmap結構的指針:
    • struct regmap *regmap_init_i2c(struct i2c_client *i2c, const struct regmap_config *config);
    • struct regmap *regmap_init_spi(struct spi_device *dev, const struct regmap_config *config);
  • 把獲得的regmap結構指針賦值給codec->control_data;
  • 調用soc-io的api:snd_soc_codec_set_cache_io使得soc-io和regmap進行關聯;
完成以上步驟後,codec驅動就可以使用諸如snd_soc_read、snd_soc_write、snd_soc_update_bits等API對codec的寄存器進行讀寫了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章