1. 程式人生 > >Linux ALSA音效卡驅動之七:ASoC架構中的Codec

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:
/* SoC Audio Codec device */
struct snd_soc_codec {
	const char *name;  /* Codec的名字*/
	struct device *dev; /* 指向Codec裝置的指標 */
	const struct snd_soc_codec_driver *driver; /* 指向該codec的驅動的指標 */
	struct snd_soc_card *card;    /* 指向Machine驅動的card例項 */
	int num_dai; /* 該Codec數字介面的個數,目前越來越多的Codec帶有多個I2S或者是PCM介面 */
	int (*volatile_register)(...);  /* 用於判定某一暫存器是否是volatile */
	int (*readable_register)(...);  /* 用於判定某一暫存器是否可讀 */
	int (*writable_register)(...);  /* 用於判定某一暫存器是否可寫 */

	/* runtime */
	......
	/* codec IO */
	void *control_data; /* 該指標指向的結構用於對codec的控制,通常和read,write欄位聯合使用 */
	enum snd_soc_control_type control_type;/* 可以是SND_SOC_SPI,SND_SOC_I2C,SND_SOC_REGMAP中的一種 */
	unsigned int (*read)(struct snd_soc_codec *, unsigned int);  /* 讀取Codec暫存器的函式 */
	int (*write)(struct snd_soc_codec *, unsigned int, unsigned int);  /* 寫入Codec暫存器的函式 */
	/* dapm */
	struct snd_soc_dapm_context dapm;  /* 用於DAPM控制元件 */
};

snd_soc_codec_driver:
/* codec driver */
struct snd_soc_codec_driver {
	/* driver ops */
	int (*probe)(struct snd_soc_codec *);  /* codec驅動的probe函式,由snd_soc_instantiate_card回撥 */
	int (*remove)(struct snd_soc_codec *);  
	int (*suspend)(struct snd_soc_codec *);  /* 電源管理 */
	int (*resume)(struct snd_soc_codec *);  /* 電源管理 */

	/* Default control and setup, added after probe() is run */
	const struct snd_kcontrol_new *controls;  /* 音訊控制元件指標 */
	const struct snd_soc_dapm_widget *dapm_widgets;  /* dapm部件指標 */
	const struct snd_soc_dapm_route *dapm_routes;  /* dapm路由指標 */

	/* codec wide operations */
	int (*set_sysclk)(...);  /* 時鐘配置函式 */
	int (*set_pll)(...);  /* 鎖相環配置函式 */

	/* codec IO */
	unsigned int (*read)(...);  /* 讀取codec暫存器函式 */
	int (*write)(...);  /* 寫入codec暫存器函式 */
	int (*volatile_register)(...);  /* 用於判定某一暫存器是否是volatile */
	int (*readable_register)(...);  /* 用於判定某一暫存器是否可讀 */
	int (*writable_register)(...);  /* 用於判定某一暫存器是否可寫 */

	/* codec bias level */
	int (*set_bias_level)(...);  /* 偏置電壓配置函式 */

};
snd_soc_dai:
/*
 * Digital Audio Interface runtime data.
 *
 * Holds runtime data for a DAI.
 */
struct snd_soc_dai {
	const char *name;  /* dai的名字 */
	struct device *dev;  /* 裝置指標 */

	/* driver ops */
	struct snd_soc_dai_driver *driver;  /* 指向dai驅動結構的指標 */

	/* DAI runtime info */
	unsigned int capture_active:1;		/* stream is in use */
	unsigned int playback_active:1;		/* stream is in use */

	/* DAI DMA data */
	void *playback_dma_data;  /* 用於管理playback dma */
	void *capture_dma_data;  /* 用於管理capture dma */

	/* parent platform/codec */
	union {
		struct snd_soc_platform *platform;  /* 如果是cpu dai,指向所繫結的平臺 */
		struct snd_soc_codec *codec;  /* 如果是codec dai指向所繫結的codec */
	};
	struct snd_soc_card *card;  /* 指向Machine驅動中的crad例項 */
};
snd_soc_dai_driver:
/*
 * Digital Audio Interface Driver.
 *
 * Describes the Digital Audio Interface in terms of its ALSA, DAI and AC97
 * operations and capabilities. Codec and platform drivers will register this
 * structure for every DAI they have.
 *
 * This structure covers the clocking, formating and ALSA operations for each
 * interface.
 */
struct snd_soc_dai_driver {
	/* DAI description */
	const char *name;  /* dai驅動名字 */

	/* DAI driver callbacks */
	int (*probe)(struct snd_soc_dai *dai);  /* dai驅動的probe函式,由snd_soc_instantiate_card回撥 */
	int (*remove)(struct snd_soc_dai *dai);  
	int (*suspend)(struct snd_soc_dai *dai);  /* 電源管理 */
	int (*resume)(struct snd_soc_dai *dai);  

	/* ops */
	const struct snd_soc_dai_ops *ops;  /* 指向本dai的snd_soc_dai_ops結構 */

	/* DAI capabilities */
	struct snd_soc_pcm_stream capture;  /* 描述capture的能力 */
	struct snd_soc_pcm_stream playback;  /* 描述playback的能力 */
};
snd_soc_dai_ops用於實現該dai的控制盒引數配置:
struct snd_soc_dai_ops {
	/*
	 * DAI clocking configuration, all optional.
	 * Called by soc_card drivers, normally in their hw_params.
	 */
	int (*set_sysclk)(...);
	int (*set_pll)(...);
	int (*set_clkdiv)(...);
	/*
	 * DAI format configuration
	 * Called by soc_card drivers, normally in their hw_params.
	 */
	int (*set_fmt)(...);
	int (*set_tdm_slot)(...);
	int (*set_channel_map)(...);
	int (*set_tristate)(...);
	/*
	 * DAI digital mute - optional.
	 * Called by soc-core to minimise any pops.
	 */
	int (*digital_mute)(...);
	/*
	 * ALSA PCM audio operations - all optional.
	 * Called by soc-core during audio PCM operations.
	 */
	int (*startup)(...);
	void (*shutdown)(...);
	int (*hw_params)(...);
	int (*hw_free)(...);
	int (*prepare)(...);
	int (*trigger)(...);
	/*
	 * For hardware based FIFO caused delay reporting.
	 * Optional.
	 */
	snd_pcm_sframes_t (*delay)(...);
};

3.  Codec的註冊

因為Codec驅動的程式碼要做到平臺無關性,要使得Machine驅動能夠使用該Codec,Codec驅動的首要任務就是確定snd_soc_codec和snd_soc_dai的例項,並把它們註冊到系統中,註冊後的codec和dai才能為Machine驅動所用。以WM8994為例,對應的程式碼位置:/sound/soc/codecs/wm8994.c,模組的入口函式註冊了一個platform driver:
static struct platform_driver wm8994_codec_driver = {
	.driver = {
		   .name = "wm8994-codec",
		   .owner = THIS_MODULE,
		   },
	.probe = wm8994_probe,
	.remove = __devexit_p(wm8994_remove),
};

module_platform_driver(wm8994_codec_driver);
有platform driver,必定會有相應的platform device,這個platform device的來源後面再說,顯然,platform driver註冊後,probe回撥將會被呼叫,這裡是wm8994_probe函式:
static int __devinit wm8994_probe(struct platform_device *pdev)
{
	return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wm8994,
			wm8994_dai, ARRAY_SIZE(wm8994_dai));
}
其中,soc_codec_dev_wm8994和wm8994_dai的定義如下(程式碼中定義了3個dai,這裡只列出第一個):
static struct snd_soc_codec_driver soc_codec_dev_wm8994 = {
	.probe =	wm8994_codec_probe,
	.remove =	wm8994_codec_remove,
	.suspend =	wm8994_suspend,
	.resume =	wm8994_resume,
	.set_bias_level = wm8994_set_bias_level,
	.reg_cache_size	= WM8994_MAX_REGISTER,
	.volatile_register = wm8994_soc_volatile,
};
static struct snd_soc_dai_driver wm8994_dai[] = {
	{
		.name = "wm8994-aif1",
		.id = 1,
		.playback = {
			.stream_name = "AIF1 Playback",
			.channels_min = 1,
			.channels_max = 2,
			.rates = WM8994_RATES,
			.formats = WM8994_FORMATS,
		},
		.capture = {
			.stream_name = "AIF1 Capture",
			.channels_min = 1,
			.channels_max = 2,
			.rates = WM8994_RATES,
			.formats = WM8994_FORMATS,
		 },
		.ops = &wm8994_aif1_dai_ops,
	},
	......
}
可見,Codec驅動的第一個步驟就是定義snd_soc_codec_driver和snd_soc_dai_driver的例項,然後呼叫snd_soc_register_codec函式對Codec進行註冊。進入snd_soc_register_codec函式看看: 首先,它申請了一個snd_soc_codec結構的例項:
	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
確定codec的名字,這個名字很重要,Machine驅動定義的snd_soc_dai_link中會指定每個link的codec和dai的名字,進行匹配繫結時就是通過和這裡的名字比較,從而找到該Codec的!
/* create CODEC component name */
	codec->name = fmt_single_name(dev, &codec->id);
然後初始化它的各個欄位,多數字段的值來自上面定義的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->dapm.stream_event = codec_drv->stream_event;
	codec->dev = dev;
	codec->driver = codec_drv;
	codec->num_dai = num_dai;
在做了一些暫存器快取的初始化和配置工作後,通過snd_soc_register_dais函式對本Codec的dai進行註冊:
	/* register any DAIs */
	if (num_dai) {
		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
		if (ret < 0)
			goto fail;
	}
最後,它把codec例項連結到全域性連結串列codec_list中,並且呼叫snd_soc_instantiate_cards是函式觸發Machine驅動進行一次匹配繫結操作:
	list_add(&codec->list, &codec_list);
	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的註冊

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驅動:

static struct i2c_driver wm8994_i2c_driver = {
	.driver = {
		.name = "wm8994",
		.owner = THIS_MODULE,
		.pm = &wm8994_pm_ops,
		.of_match_table = wm8994_of_match,
	},
	.probe = wm8994_i2c_probe,
	.remove = wm8994_i2c_remove,
	.id_table = wm8994_i2c_id,
};

static int __init wm8994_i2c_init(void)
{
	int ret;

	ret = i2c_add_driver(&wm8994_i2c_driver);
	if (ret != 0)
		pr_err("Failed to register wm8994 I2C driver: %d\n", ret);

	return ret;
}
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子裝置:
static int wm8994_i2c_probe(struct i2c_client *i2c,
			    const struct i2c_device_id *id)
{
	struct wm8994 *wm8994;
	int ret;
	wm8994 = devm_kzalloc(&i2c->dev, sizeof(struct wm8994), GFP_KERNEL);
	i2c_set_clientdata(i2c, wm8994);
	wm8994->dev = &i2c->dev;
	wm8994->irq = i2c->irq;
	wm8994->type = id->driver_data;
	wm8994->regmap = regmap_init_i2c(i2c, &wm8994_base_regmap_config);

	return wm8994_device_init(wm8994, i2c->irq);
}
繼續進入wm8994_device_init()函式,它首先為兩個LDO新增mfd子裝置:
	/* Add the on-chip regulators first for bootstrapping */
	ret = mfd_add_devices(wm8994->dev, -1,
			      wm8994_regulator_devs,
			      ARRAY_SIZE(wm8994_regulator_devs),
			      NULL, 0);
因為WM1811,WM8994,WM8958三個晶片功能類似,因此這三個晶片都使用了WM8994的程式碼,所以wm8994_device_init()接下來根據不同的晶片型號做了一些初始化動作,這部分的程式碼就不貼了。接著,從platform_data中獲得部分配置資訊:
	if (pdata) {
		wm8994->irq_base = pdata->irq_base;
		wm8994->gpio_base = pdata->gpio_base;

		/* GPIO configuration is only applied if it's non-zero */
		......
	}
最後,初始化irq,然後新增codec子裝置和gpio子裝置:
	wm8994_irq_init(wm8994);

	ret = mfd_add_devices(wm8994->dev, -1,
			      wm8994_devs, ARRAY_SIZE(wm8994_devs),
			      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的暫存器進行讀寫了。