Linux alsa驅動中的CODEC寄存器配置問題__Linux

來源:互聯網
上載者:User

 調試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訊號輸出。
      

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.