高通Audio中ASOC的codec驱动(二)
原文:https://www.tinymind.net.cn/articles/8c917351355016
继上一篇文章:高通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
",
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
",
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
");
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
", __func__);
41 } else {
42 dev_dbg(&spmi->dev,
43 "%s: irq initialization passed
", __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
",
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
",
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
",
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
", __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
",
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
",
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
",
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
",
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 }
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
", 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. 定义structsnd_soc_codec_driver结构,设置,初始化。
3. 定义structsnd_soc_dai_driver结构,设置,初始化。
4. 调用snd_soc_register_codec函数注册codec。