ASOC的出現是為了讓codec獨立於CPU,減少和CPU之間的耦合,這樣同一個codec驅動就無需修改就可以匹配任何一款平臺。 在Machine中已經知道,snd_soc_dai_link結構就指明瞭該Machine所使用的Platform和Codec。在Codec這邊通過codec_dai和Pl ...
繼上一篇文章:高通Audio中ASOC的machine驅動(一)
ASOC的出現是為了讓codec獨立於CPU,減少和CPU之間的耦合,這樣同一個codec驅動就無需修改就可以匹配任何一款平臺。
在Machine中已經知道,snd_soc_dai_link結構就指明瞭該Machine所使用的Platform和Codec。在Codec這邊通過codec_dai和Platform側的cpu_dai相互通信,既然相互通信,就需要遵守一定的規則,其中codec_dai和cpu_dai統一抽象為struct snd_soc_dai結構,而將dai的相關操作使用snd_soc_dai_driver抽象。同時也需要對所有的codec設備進行抽象封裝,linux使用snd_soc_codec進行所有codec設備的抽象,而將codec的驅動抽象為snd_soc_codec_driver結構。 所有簡單來說,Codec側有四個重要的數據結構: struct snd_soc_dai,struct snd_soc_dai_driver,struct snd_soc_codec,struct snd_soc_codec_driver。
1、Codec代碼分析:
1.1 找到codec的代碼:
如何找到codec的代碼呢? 答案是通過machine中的snd_soc_dai_link結構:1 { 2 .name = LPASS_BE_TERT_MI2S_TX, 3 .stream_name = "Tertiary MI2S Capture", 4 .cpu_dai_name = "msm-dai-q6-mi2s.2", 5 .platform_name = "msm-pcm-routing", 6 .codec_name = MSM8X16_CODEC_NAME, 7 .codec_dai_name = "msm8x16_wcd_i2s_tx1", 8 .no_pcm = 1, 9 .be_id = MSM_BACKEND_DAI_TERTIARY_MI2S_TX, 10 .be_hw_params_fixup = msm_tx_be_hw_params_fixup, 11 .ops = &msm8x16_mi2s_be_ops, 12 .ignore_suspend = 1, 13 },
由dai_link中codec_name,可以知道我們的codec驅動在哪。
由高通Audio中ASOC的machine驅動中的匹配並註冊相應驅動的那一章分析可知,codec驅動代碼就是msm8x16-wcd.c這個文件;
2、查看codec的probe函數:
1 static int msm8x16_wcd_spmi_probe(struct spmi_device *spmi) 2 { 3 int ret = 0; 4 struct msm8x16_wcd *msm8x16 = NULL; 5 struct msm8x16_wcd_pdata *pdata; 6 struct resource *wcd_resource; 7 int modem_state; 8 9 dev_dbg(&spmi->dev, "%s(%d):slave ID = 0x%x\n", 10 __func__, __LINE__, spmi->sid); 11 12 modem_state = apr_get_modem_state(); 13 if (modem_state != APR_SUBSYS_LOADED) { 14 dev_dbg(&spmi->dev, "Modem is not loaded yet %d\n", 15 modem_state); 16 return -EPROBE_DEFER; 17 } 18 19 wcd_resource = spmi_get_resource(spmi, NULL, IORESOURCE_MEM, 0); 20 if (!wcd_resource) { 21 dev_err(&spmi->dev, "Unable to get Tombak base address\n"); 22 return -ENXIO; 23 } 24 25 switch (wcd_resource->start) { 26 case TOMBAK_CORE_0_SPMI_ADDR: 27 msm8x16_wcd_modules[0].spmi = spmi; 28 msm8x16_wcd_modules[0].base = (spmi->sid << 16) + 29 wcd_resource->start; 30 wcd9xxx_spmi_set_dev(msm8x16_wcd_modules[0].spmi, 0); 31 device_init_wakeup(&spmi->dev, true); 32 break; 33 case TOMBAK_CORE_1_SPMI_ADDR: 34 msm8x16_wcd_modules[1].spmi = spmi; 35 msm8x16_wcd_modules[1].base = (spmi->sid << 16) + 36 wcd_resource->start; 37 wcd9xxx_spmi_set_dev(msm8x16_wcd_modules[1].spmi, 1); 38 if (wcd9xxx_spmi_irq_init()) { 39 dev_err(&spmi->dev, 40 "%s: irq initialization failed\n", __func__); 41 } else { 42 dev_dbg(&spmi->dev, 43 "%s: irq initialization passed\n", __func__); 44 } 45 goto rtn; 46 default: 47 ret = -EINVAL; 48 goto rtn; 49 } 50 51 52 dev_dbg(&spmi->dev, "%s(%d):start addr = 0x%pa\n", 53 __func__, __LINE__, &wcd_resource->start); 54 55 if (wcd_resource->start != TOMBAK_CORE_0_SPMI_ADDR) 56 goto rtn; 57 58 dev_set_name(&spmi->dev, "%s", MSM8X16_CODEC_NAME); 59 if (spmi->dev.of_node) { 60 dev_dbg(&spmi->dev, "%s:Platform data from device tree\n", 61 __func__); 62 pdata = msm8x16_wcd_populate_dt_pdata(&spmi->dev); 63 spmi->dev.platform_data = pdata; 64 } else { 65 dev_dbg(&spmi->dev, "%s:Platform data from board file\n", 66 __func__); 67 pdata = spmi->dev.platform_data; 68 } 69 70 msm8x16 = kzalloc(sizeof(struct msm8x16_wcd), GFP_KERNEL); 71 if (msm8x16 == NULL) { 72 dev_err(&spmi->dev, 73 "%s: error, allocation failed\n", __func__); 74 ret = -ENOMEM; 75 goto rtn; 76 } 77 78 msm8x16->dev = &spmi->dev; 79 msm8x16->read_dev = __msm8x16_wcd_reg_read; 80 msm8x16->write_dev = __msm8x16_wcd_reg_write; 81 ret = msm8x16_wcd_init_supplies(msm8x16, pdata); 82 if (ret) { 83 dev_err(&spmi->dev, "%s: Fail to enable Codec supplies\n", 84 __func__); 85 goto err_codec; 86 } 87 88 ret = msm8x16_wcd_enable_static_supplies(msm8x16, pdata); 89 if (ret) { 90 dev_err(&spmi->dev, 91 "%s: Fail to enable Codec pre-reset supplies\n", 92 __func__); 93 goto err_codec; 94 } 95 usleep_range(5, 6); 96 97 ret = msm8x16_wcd_device_init(msm8x16); 98 if (ret) { 99 dev_err(&spmi->dev, 100 "%s:msm8x16_wcd_device_init failed with error %d\n", 101 __func__, ret); 102 goto err_supplies; 103 } 104 dev_set_drvdata(&spmi->dev, msm8x16); 105 106 ret = snd_soc_register_codec(&spmi->dev, &soc_codec_dev_msm8x16_wcd, 107 msm8x16_wcd_i2s_dai, 108 ARRAY_SIZE(msm8x16_wcd_i2s_dai)); 109 if (ret) { 110 dev_err(&spmi->dev, 111 "%s:snd_soc_register_codec failed with error %d\n", 112 __func__, ret); 113 } else { 114 goto rtn; 115 } 116 err_supplies: 117 msm8x16_wcd_disable_supplies(msm8x16, pdata); 118 err_codec: 119 kfree(msm8x16); 120 rtn: 121 return ret; 122 }msm8x16_wcd_spmi_probe
SPMI匯流排是高通電源管理的一種規範,也就是通過PMU控制音頻(具體我也不夠瞭解,有待以後深入理解)
看看最重要的函數:
1 ret = snd_soc_register_codec(&spmi->dev, &soc_codec_dev_msm8x16_wcd, 2 msm8x16_wcd_i2s_dai, 3 ARRAY_SIZE(msm8x16_wcd_i2s_dai));
此函數通過snd_soc_register_codec函數註冊了wcd9320的codec,同時傳入了snd_soc_codec_driver和snd_soc_dai_driver結構。
1 static struct snd_soc_codec_driver soc_codec_dev_msm8x16_wcd = { 2 .probe = msm8x16_wcd_codec_probe, 3 .remove = msm8x16_wcd_codec_remove, 4 5 .read = msm8x16_wcd_read, 6 .write = msm8x16_wcd_write, 7 8 .suspend = msm8x16_wcd_suspend, 9 .resume = msm8x16_wcd_resume, 10 11 .readable_register = msm8x16_wcd_readable, 12 .volatile_register = msm8x16_wcd_volatile, 13 14 .reg_cache_size = MSM8X16_WCD_CACHE_SIZE, 15 .reg_cache_default = msm8x16_wcd_reset_reg_defaults, 16 .reg_word_size = 1, 17 18 .controls = msm8x16_wcd_snd_controls, 19 .num_controls = ARRAY_SIZE(msm8x16_wcd_snd_controls), 20 .dapm_widgets = msm8x16_wcd_dapm_widgets, 21 .num_dapm_widgets = ARRAY_SIZE(msm8x16_wcd_dapm_widgets), 22 .dapm_routes = audio_map, 23 .num_dapm_routes = ARRAY_SIZE(audio_map), 24 };
snd_soc_dai_driver結構:
1 static struct snd_soc_dai_driver msm8x16_wcd_i2s_dai[] = { 2 { 3 .name = "msm8x16_wcd_i2s_rx1", 4 .id = AIF1_PB, 5 .playback = { 6 .stream_name = "AIF1 Playback", 7 .rates = MSM8X16_WCD_RATES, 8 .formats = MSM8X16_WCD_FORMATS, 9 .rate_max = 192000, 10 .rate_min = 8000, 11 .channels_min = 1, 12 .channels_max = 3, 13 }, 14 .ops = &msm8x16_wcd_dai_ops, 15 }, 16 { 17 .name = "msm8x16_wcd_i2s_tx1", 18 .id = AIF1_CAP, 19 .capture = { 20 .stream_name = "AIF1 Capture", 21 .rates = MSM8X16_WCD_RATES, 22 .formats = MSM8X16_WCD_FORMATS, 23 .rate_max = 192000, 24 .rate_min = 8000, 25 .channels_min = 1, 26 .channels_max = 4, 27 }, 28 .ops = &msm8x16_wcd_dai_ops, 29 }, 30 };
3、snd_soc_register_codec函數分析:
首先,它申請了一個snd_soc_codec結構的實例:1 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
確定codec的名字,這個名字很重要,Machine驅動定義的snd_soc_dai_link中會指定每個link的codec和dai的名字,進行匹配綁定時就是通過和這裡的名字比較,從而找到該Codec的!
1 codec->name = fmt_single_name(dev, &codec->id);
然後初始化它的各個欄位,多數欄位的值來自上面定義的snd_soc_codec_driver的實例:
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->ignore_pmdown_time = codec_drv->ignore_pmdown_time; 7 codec->dapm.bias_level = SND_SOC_BIAS_OFF; 8 codec->dapm.dev = dev; 9 codec->dapm.codec = codec; 10 codec->dapm.seq_notifier = codec_drv->seq_notifier; 11 codec->dapm.stream_event = codec_drv->stream_event; 12 codec->dev = dev; 13 codec->driver = codec_drv; 14 codec->num_dai = num_dai;
在做了一些寄存器緩存的初始化和配置工作後,通過snd_soc_register_dais函數對本Codec的dai進行註冊:
1 /* register any DAIs */ 2 ret = snd_soc_register_dais(dev, dai_drv, num_dai); 3 if (ret < 0) { 4 dev_err(codec->dev, "ASoC: Failed to regster DAIs: %d\n", ret); 5 goto fail_codec_name; 6 }
最後,它把codec實例鏈接到全局鏈表codec_list中:
1 mutex_lock(&client_mutex); 2 list_add(&codec->list, &codec_list); 3 mutex_unlock(&client_mutex);
至此,codec的註冊就分析完畢。
關於codec側驅動總結: 1. 分配名字為"codec_name"的平臺驅動,註冊。
2. 定義struct snd_soc_codec_driver結構,設置,初始化。 3. 定義struct snd_soc_dai_driver結構,設置,初始化。 4. 調用snd_soc_register_codec函數註冊codec。