調試Audio CODEC時,有時候需要讀codec寄存器的值以確認是否配置寄存器成功。
記得之前調試wm8978時,發現這個CODEC的寄存器不能讀,每次讀的結果都是0xff.
後來在linux上調試wm8978時,用snd_soc_read()又能讀到所配置的值,感覺很詫異。當時也沒有細究。
這兩天有空,在linux核心中看了一下相關代碼,發現關於通過I2C讀寫CODEC的代碼在 sound/soc/soc-cache.c中。
在這個.c檔案中定義了不少類似 snd_soc_X_Y_read() 和 snd_soc_X_Y_write()的函數,X代表地址位元,Y代碼
資料位元數,例如snd_soc_7_9_read()用於地址是7位,資料是9位的CODEC,wolfson的CODEC大多數是這種情況。
另外有個用於具體的CODEC驅動去設定X,Y值的介面,以便選擇對應的讀寫函數,這個介面是:
int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
int addr_bits, int data_bits,
enum snd_soc_control_type control)
例如wm8960.c中有如下配置調用:
ret =snd_soc_codec_set_cache_io(codec,7,9,control);
現在說一下snd_soc_X_Y_write()這類函數,例如:
static unsigned int snd_soc_7_9_read(struct snd_soc_codec *codec,
unsigned int reg)
{
u16 *cache = codec->reg_cache;
if (reg >= codec->reg_cache_size)
return -1;
return cache[reg];
}
一看代碼,原來這裡的讀,竟然不是從CODEC的寄存器中讀出來的,而是從一個cache緩衝中讀出來的。
再看一下對應的write函數就一清二楚了,原來在write時將所寫的寄存器的值同時緩衝到cache中,read時
直接從cache中讀出。
static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
unsigned int value)
{
u16 *cache = codec->reg_cache;
u8 data[2];
int ret;
BUG_ON(codec->volatile_register);
data[0] = (reg << 1) | ((value >> 8) & 0x0001);
data[1] = value & 0x00ff;
if (reg < codec->reg_cache_size)
cache[reg] = value;
if (codec->cache_only) {
codec->cache_sync = 1;
return 0;
}
dev_dbg(codec->dev, "0x%x = 0x%x\n", reg, value);
ret = codec->hw_write(codec->control_data, data, 2);
if (ret == 2)
return 0;
if (ret < 0)
return ret;
else
return -EIO;
}
這樣,近期一個奇怪的問題又有好解釋了:
通過I2C配置sensor後,通過aplay播放音樂無動靜(無聲音),但通過snd_soc_read()讀CODEC(wm8960)各寄存器的值
又是正常的(和能正常播放時的配置一樣),用示波器量了一下CODEC的LRCK管腳,沒有任何東西(本應該有clock輸出的,因為
CODEC被配置為master模式)。原來snd_soc_read()讀到的值根本就是之前寫進去的緩衝,並不是真正CODEC中已經配置的值,
顯然CODEC沒有正常配置,所以沒有LRCK訊號輸出。