1. Codec简介
在移动设备中,Codec的作用可以归结为4种,分别是:
- 对PCM等信号进行D/A转换,把数字的音频信号转换为模拟信号
- 对Mic、Linein或者其他输入源的模拟信号进行A/D转换,把模拟的声音信号转变CPU能够处理的数字信号
- 对音频通路进行控制,比如播放音乐,收听调频收音机,又或者接听电话时,音频信号在codec内的流通路线是不一样的
- 对音频信号做出相应的处理,例如音量控制,功率放大,EQ控制等等
ASoC对Codec的这些功能都定义好了一些列相应的接口,以方便地对Codec进行控制。ASoC对Codec驱动的一个基本要求是:驱动程序的代码必须要做到平台无关性,以方便同一个Codec的代码不经修改即可用在不同的平台上。以下的讨论基于wolfson的Codec芯片WM8994,kernel的版本3.3.x。
/*****************************************************************************************************/
声明:本博内容均由http://blog.csdn.net/droidphone原创,转载请注明出处,谢谢!
/*****************************************************************************************************/
2. ASoC中对Codec的数据抽象
描述Codec的最主要的几个数据结构分别是:snd_soc_codec,snd_soc_codec_driver,snd_soc_dai,snd_soc_dai_driver,其中的snd_soc_dai和snd_soc_dai_driver在ASoC的Platform驱动中也会使用到,Platform和Codec的DAI通过snd_soc_dai_link结构,在Machine驱动中进行绑定连接。下面我们先看看这几个结构的定义,这里我只贴出我要关注的字段,详细的定义请参照:/include/sound/soc.h。
snd_soc_codec:
- /* SoC Audio Codec device */
- struct snd_soc_codec {
- const char *name; /* Codec的名字*/
- struct device *dev; /* 指向Codec设备的指针 */
- const struct snd_soc_codec_driver *driver; /* 指向该codec的驱动的指针 */
- struct snd_soc_card *card; /* 指向Machine驱动的card实例 */
- int num_dai; /* 该Codec数字接口的个数,目前越来越多的Codec带有多个I2S或者是PCM接口 */
- int (*volatile_register)(...); /* 用于判定某一寄存器是否是volatile */
- int (*readable_register)(...); /* 用于判定某一寄存器是否可读 */
- int (*writable_register)(...); /* 用于判定某一寄存器是否可写 */
- /* runtime */
- ......
- /* codec IO */
- void *control_data; /* 该指针指向的结构用于对codec的控制,通常和read,write字段联合使用 */
- enum snd_soc_control_type control_type;/* 可以是SND_SOC_SPI,SND_SOC_I2C,SND_SOC_REGMAP中的一种 */
- unsigned int (*read)(struct snd_soc_codec *, unsigned int); /* 读取Codec寄存器的函数 */
- int (*write)(struct snd_soc_codec *, unsigned int, unsigned int); /* 写入Codec寄存器的函数 */
- /* dapm */
- struct snd_soc_dapm_context dapm; /* 用于DAPM控件 */
- };
snd_soc_codec_driver:
- /* codec driver */
- struct snd_soc_codec_driver {
- /* driver ops */
- int (*probe)(struct snd_soc_codec *); /* codec驱动的probe函数,由snd_soc_instantiate_card回调 */
- int (*remove)(struct snd_soc_codec *);
- int (*suspend)(struct snd_soc_codec *); /* 电源管理 */
- int (*resume)(struct snd_soc_codec *); /* 电源管理 */
- /* Default control and setup, added after probe() is run */
- const struct snd_kcontrol_new *controls; /* 音频控件指针 */
- const struct snd_soc_dapm_widget *dapm_widgets; /* dapm部件指针 */
- const struct snd_soc_dapm_route *dapm_routes; /* dapm路由指针 */
- /* codec wide operations */
- int (*set_sysclk)(...); /* 时钟配置函数 */
- int (*set_pll)(...); /* 锁相环配置函数 */
- /* codec IO */
- unsigned int (*read)(...); /* 读取codec寄存器函数 */
- int (*write)(...); /* 写入codec寄存器函数 */
- int (*volatile_register)(...); /* 用于判定某一寄存器是否是volatile */
- int (*readable_register)(...); /* 用于判定某一寄存器是否可读 */
- int (*writable_register)(...); /* 用于判定某一寄存器是否可写 */
- /* codec bias level */
- int (*set_bias_level)(...); /* 偏置电压配置函数 */
- };
snd_soc_dai:
- /*
- * Digital Audio Interface runtime data.
- *
- * Holds runtime data for a DAI.
- */
- struct snd_soc_dai {
- const char *name; /* dai的名字 */
- struct device *dev; /* 设备指针 */
- /* driver ops */
- struct snd_soc_dai_driver *driver; /* 指向dai驱动结构的指针 */
- /* DAI runtime info */
- unsigned int capture_active:1; /* stream is in use */
- unsigned int playback_active:1; /* stream is in use */
- /* DAI DMA data */
- void *playback_dma_data; /* 用于管理playback dma */
- void *capture_dma_data; /* 用于管理capture dma */
- /* parent platform/codec */
- union {
- struct snd_soc_platform *platform; /* 如果是cpu dai,指向所绑定的平台 */
- struct snd_soc_codec *codec; /* 如果是codec dai指向所绑定的codec */
- };
- struct snd_soc_card *card; /* 指向Machine驱动中的crad实例 */
- };
snd_soc_dai_driver:
- /*
- * Digital Audio Interface Driver.
- *
- * Describes the Digital Audio Interface in terms of its ALSA, DAI and AC97
- * operations and capabilities. Codec and platform drivers will register this
- * structure for every DAI they have.
- *
- * This structure covers the clocking, formating and ALSA operations for each
- * interface.
- */
- struct snd_soc_dai_driver {
- /* DAI description */
- const char *name; /* dai驱动名字 */
- /* DAI driver callbacks */
- int (*probe)(struct snd_soc_dai *dai); /* dai驱动的probe函数,由snd_soc_instantiate_card回调 */
- int (*remove)(struct snd_soc_dai *dai);
- int (*suspend)(struct snd_soc_dai *dai); /* 电源管理 */
- int (*resume)(struct snd_soc_dai *dai);
- /* ops */
- const struct snd_soc_dai_ops *ops; /* 指向本dai的snd_soc_dai_ops结构 */
- /* DAI capabilities */
- struct snd_soc_pcm_stream capture; /* 描述capture的能力 */
- struct snd_soc_pcm_stream playback; /* 描述playback的能力 */
- };
snd_soc_dai_ops用于实现该dai的控制盒参数配置:
- struct snd_soc_dai_ops {
- /*
- * DAI clocking configuration, all optional.
- * Called by soc_card drivers, normally in their hw_params.
- */
- int (*set_sysclk)(...);
- int (*set_pll)(...);
- int (*set_clkdiv)(...);
- /*
- * DAI format configuration
- * Called by soc_card drivers, normally in their hw_params.
- */
- int (*set_fmt)(...);
- int (*set_tdm_slot)(...);
- int (*set_channel_map)(...);
- int (*set_tristate)(...);
- /*
- * DAI digital mute - optional.
- * Called by soc-core to minimise any pops.
- */
- int (*digital_mute)(...);
- /*
- * ALSA PCM audio operations - all optional.
- * Called by soc-core during audio PCM operations.
- */
- int (*startup)(...);
- void (*shutdown)(...);