Fork me on GitHub

高通Audio中ASOC的codec驱动(二)

继上一篇文章:高通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结构。
 
 

1、重要的数据结构:

所有简单来说,Codec侧有四个重要的数据结构:
 
struct snd_soc_dai,struct snd_soc_dai_driver,struct snd_soc_codec,struct snd_soc_codec_driver
 
snd_soc_dai:
 1 /*
 2  * Digital Audio Interface runtime data.
 3  *
 4  * Holds runtime data for a DAI.
 5  */
 6 struct snd_soc_dai {
 7     const char *name;  /* dai的名字 */
 8     struct device *dev;  /* 设备指针 */
 9 
10     /* driver ops */
11     struct snd_soc_dai_driver *driver;  /* 指向dai驱动结构的指针 */
12 
13     /* DAI runtime info */
14     unsigned int capture_active:1;        /* stream is in use */
15     unsigned int playback_active:1;        /* stream is in use */
16 
17     /* DAI DMA data */
18     void *playback_dma_data;  /* 用于管理playback dma */
19     void *capture_dma_data;  /* 用于管理capture dma */
20 
21     /* parent platform/codec */
22     union {
23         struct snd_soc_platform *platform;  /* 如果是cpu dai,指向所绑定的平台 */
24         struct snd_soc_codec *codec;  /* 如果是codec dai指向所绑定的codec */
25     };
26     struct snd_soc_card *card;  /* 指向Machine驱动中的crad实例 */
27 };

snd_soc_dai_driver:

 1 /*
 2  * Digital Audio Interface Driver.
 3  *
 4  * Describes the Digital Audio Interface in terms of its ALSA, DAI and AC97
 5  * operations and capabilities. Codec and platform drivers will register this
 6  * structure for every DAI they have.
 7  *
 8  * This structure covers the clocking, formating and ALSA operations for each
 9  * interface.
10  */
11 struct snd_soc_dai_driver {
12     /* DAI description */
13     const char *name;  /* dai驱动名字 */
14 
15     /* DAI driver callbacks */
16     int (*probe)(struct snd_soc_dai *dai);  /* dai驱动的probe函数,由snd_soc_instantiate_card回调 */
17     int (*remove)(struct snd_soc_dai *dai);  
18     int (*suspend)(struct snd_soc_dai *dai);  /* 电源管理 */
19     int (*resume)(struct snd_soc_dai *dai);  
20 
21     /* ops */
22     const struct snd_soc_dai_ops *ops;  /* 指向本dai的snd_soc_dai_ops结构 */
23 
24     /* DAI capabilities */
25     struct snd_soc_pcm_stream capture;  /* 描述capture的能力 */
26     struct snd_soc_pcm_stream playback;  /* 描述playback的能力 */
27 };
snd_soc_codec
 1 /* SoC Audio Codec device */
 2 struct snd_soc_codec {
 3     const char *name;  /* Codec的名字*/
 4     struct device *dev; /* 指向Codec设备的指针 */
 5     const struct snd_soc_codec_driver *driver; /* 指向该codec的驱动的指针 */
 6     struct snd_soc_card *card;    /* 指向Machine驱动的card实例 */
 7     int num_dai; /* 该Codec数字接口的个数,目前越来越多的Codec带有多个I2S或者是PCM接口 */
 8     int (*volatile_register)(...);  /* 用于判定某一寄存器是否是volatile */
 9     int (*readable_register)(...);  /* 用于判定某一寄存器是否可读 */
10     int (*writable_register)(...);  /* 用于判定某一寄存器是否可写 */
11 
12     /* runtime */
13     ......
14     /* codec IO */
15     void *control_data; /* 该指针指向的结构用于对codec的控制,通常和read,write字段联合使用 */
16     enum snd_soc_control_type control_type;/* 可以是SND_SOC_SPI,SND_SOC_I2C,SND_SOC_REGMAP中的一种 */
17     unsigned int (*read)(struct snd_soc_codec *, unsigned int);  /* 读取Codec寄存器的函数 */
18     int (*write)(struct snd_soc_codec *, unsigned int, unsigned int);  /* 写入Codec寄存器的函数 */
19     /* dapm */
20     struct snd_soc_dapm_context dapm;  /* 用于DAPM控件 */
21 };

snd_soc_codec_driver:

 1 /* codec driver */
 2 struct snd_soc_codec_driver {
 3     /* driver ops */
 4     int (*probe)(struct snd_soc_codec *);  /* codec驱动的probe函数,由snd_soc_instantiate_card回调 */
 5     int (*remove)(struct snd_soc_codec *);  
 6     int (*suspend)(struct snd_soc_codec *);  /* 电源管理 */
 7     int (*resume)(struct snd_soc_codec *);  /* 电源管理 */
 8 
 9     /* Default control and setup, added after probe() is run */
10     const struct snd_kcontrol_new *controls;  /* 音频控件指针 */
11     const struct snd_soc_dapm_widget *dapm_widgets;  /* dapm部件指针 */
12     const struct snd_soc_dapm_route *dapm_routes;  /* dapm路由指针 */
13 
14     /* codec wide operations */
15     int (*set_sysclk)(...);  /* 时钟配置函数 */
16     int (*set_pll)(...);  /* 锁相环配置函数 */
17 
18     /* codec IO */
19     unsigned int (*read)(...);  /* 读取codec寄存器函数 */
20     int (*write)(...);  /* 写入codec寄存器函数 */
21     int (*volatile_register)(...);  /* 用于判定某一寄存器是否是volatile */
22     int (*readable_register)(...);  /* 用于判定某一寄存器是否可读 */
23     int (*writable_register)(...);  /* 用于判定某一寄存器是否可写 */
24 
25     /* codec bias level */
26     int (*set_bias_level)(...);  /* 偏置电压配置函数 */
27 
28 };

 

2、Codec代码分析:

2.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这个文件;

 

 

3、查看codec的probe函数:

因为Codec驱动的代码要做到平台无关性,要使得Machine驱动能够使用该Codec,Codec驱动的首要任务就是确定snd_soc_codec和snd_soc_dai的实例,并把它们注册到系统中,注册后的codec和dai才能为Machine驱动所用。
  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_driversnd_soc_dai_driver结构。

 

 1 static struct snd_soc_codec_driver soc_codec_dev_msm8x16_wcd = {
 2     .probe    = msm8x16_wcd_codec_probe,  /*codec驱动的probe函数,由snd_soc_instantiate_card回调*/
 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 };

 

4、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);

 并且调用snd_soc_instantiate_cards对machine驱动进行一次匹配绑定的操作;

 

 

 至此,codec的注册就分析完毕。

 

关于codec侧驱动总结:
1.   分配名字为"codec_name"的平台驱动,注册。
2.   定义struct snd_soc_codec_driver结构,设置,初始化。
3.   定义struct snd_soc_dai_driver结构,设置,初始化。
4.   调用snd_soc_register_codec函数注册codec。
 
 
posted @ 2018-03-02 13:06  yooooooo  阅读(5487)  评论(0编辑  收藏  举报