全志F1C100S声卡驱动探究
如果有问题,请加QQ群 891339868 进行交流
最近刚上手全志的F1C100S这个片子,听一下音乐,本来想着挺简单,内核配置一下驱动就OK,谁知道上来就一闷棍,主线内核不支持内置声卡,经过多方面的研究,终于把这个声卡驱动搞定,今天记录一下。
首先来看一下f1c100s的内部声卡的框图:
从图上可以看得出来,模拟部分是4输入,2输出,一个输入混音器,两个输出混音器,还有与之相对应的开关,搞清楚这些东西很重要,因为这里面的每一个组件或者开关,在声卡驱动里面都有对应的组件描述。
硬件结构有一定的了解以后,再来看一下和该声卡有关联的寄存器:
上图中就是和f1c100s的内部codec相关的所有的寄存器,在这里先不做详细的介绍,下面用到后再说。
和其他声卡驱动一样,f1c100s的声卡驱动也是给予ASOC框架做的,也是分为platform部分,codec部分,machine部分,他们又分别包含codec驱动和DAI驱动,我的理解codec驱动是提供资源,DAI驱动是规定怎么传输和使用这些资源
一、先来看一下codec部分:
首先看一这两个比较重要的数据结构:
struct suniv_codec_quirks {
const struct regmap_config *regmap_config;
const struct snd_soc_codec_driver *codec;
struct snd_soc_card * (*create_card)(struct device *dev);
struct reg_field reg_adc_fifoc; /* used for regmap_field */
unsigned int reg_dac_txdata; /* TX FIFO offset for DMA config */
unsigned int reg_adc_rxdata; /* RX FIFO offset for DMA config */
bool has_reset;
u32 dma_max_burst;
};
static const struct suniv_codec_quirks suniv_codec_quirks = {
.regmap_config = &suniv_codec_regmap_config,
.codec = &suniv_codec_codec,
.create_card = suniv_codec_create_card,
.reg_adc_fifoc = REG_FIELD(SUNIV_CODEC_ADC_FIFOC, 0, 31),
.reg_dac_txdata = SUNIV_CODEC_DAC_TXDATA,
.reg_adc_rxdata = SUNIV_CODEC_ADC_RXDATA,
.has_reset = true,//
.dma_max_burst = SUNIV_DMA_MAX_BURST,//
};
第一个定义的结构体是和f1c100s的codec相关的数据的一个集合,第二个是对其进行数据填充。
(一)、struct regmap_config:寄存器地址的相关配置
static const struct regmap_config suniv_codec_regmap_config = {
.reg_bits = 32,
.reg_stride = 4,
.val_bits = 32,
.max_register = SUNIV_CODEC_ADC_DAP_ORT_REG,
};
1、reg_bits:寄存器地址的长度,32位的,所以配置为32
2、reg_stride:寄存器的跨度,32位的,所以配置位8
3、val_bits:寄存器值的长度,32位的,所以配置位32
4、max_register:寄存器最大的偏移地址,可以配置为和声卡相关的最大的寄存器地址
相对来说,上面这些配置比较简单也比较格式化,到这里就介绍完了。
(二)、struct snd_soc_codec_driver:驱动的相关配置
static const struct snd_soc_codec_driver suniv_codec_codec = {
.component_driver = {
.controls = suniv_codec_controls,//音频控件
.num_controls = ARRAY_SIZE(suniv_codec_controls),
.dapm_widgets = suniv_codec_codec_dapm_widgets,//dapm控件
.num_dapm_widgets = ARRAY_SIZE(suniv_codec_codec_dapm_widgets),
.dapm_routes = suniv_codec_codec_dapm_routes,//dapm路由
.num_dapm_routes = ARRAY_SIZE(suniv_codec_codec_dapm_routes),
},
};
在这里面主要配置和DAPM(动态电源管理)组件相关的东西:
1、struct snd_kcontrol_new *controls:声卡的音频控件,其实就是各个通路上的通断开关和音量调节开关之类的东西,下面来看一下该声卡的具体配置:
static const struct snd_kcontrol_new suniv_codec_controls[] = {
SOC_SINGLE_TLV("Headphone Playback Volume", SUNIV_CODEC_DAC_ACTL,
SUNIV_CODEC_DAC_ACTL_HP_VOL, 0x3f, 1,
suniv_codec_hp_volume_scale),
SOC_SINGLE_TLV("DAC Playback Volume", SUNIV_CODEC_DAC_DPC,
SUNIV_CODEC_DAC_DPC_DVOL, 0x3f, 1,
suniv_codec_dvol_scale),
SOC_DOUBLE("Headphone Playback Switch", SUNIV_CODEC_DAC_ACTL,
SUNIV_CODEC_DAC_ACTL_LHPPAMUTE_MUTE,
SUNIV_CODEC_DAC_ACTL_RHPPAMUTE_MUTE, 1, 0),
SOC_SINGLE("MIC AMPEN Switch", SUNIV_CODEC_ADC_ACTL,
SUNIV_CODEC_ADC_ACTL_MIC_AMPEN, 1, 0),
};
在这里其实是定义了四个控制开关,这几个开关在系统启动后,可以使用amixer 工具进行设置和查看的,在配置时,使用了SOC_SINGLE_TLV和SOC_DOUBLE这两个宏,具体这两个宏怎么定义的,可以具体百度,区别就是第一个是一次设置一个寄存器,第二个是一次设置两个寄存器。
2、struct snd_soc_dapm_widget *dapm_widgets:动态电源控制控件,其实就是对应硬件框图里面的混音器、输入、输出之类的东西,下面来看一下 具体的配置:
static const struct snd_soc_dapm_widget suniv_codec_codec_dapm_widgets[] = {
/* Microphone inputs */
SND_SOC_DAPM_INPUT("MIC"),
/* Mic input path */
SND_SOC_DAPM_PGA("Mic Amplifier", SUNIV_CODEC_ADC_ACTL,
SUNIV_CODEC_ADC_ACTL_MIC_AMPEN, 0, NULL, 0),
/* Line In */
SND_SOC_DAPM_INPUT("LINEIN"),
/* FM In */
SND_SOC_DAPM_INPUT("FMINR"),
SND_SOC_DAPM_INPUT("FMINL"),
/* Digital parts of the ADCs */
SND_SOC_DAPM_SUPPLY("ADC Enable", SUNIV_CODEC_ADC_FIFOC,
SUNIV_CODEC_ADC_FIFOC_EN_AD, 0, NULL, 0),
/* Digital parts of the DACs */
SND_SOC_DAPM_SUPPLY("DAC Enable", SUNIV_CODEC_DAC_DPC,
SUNIV_CODEC_DAC_DPC_EN_DA, 0, NULL, 0),
/* Analog parts of the ADCs */
SND_SOC_DAPM_ADC("ADC", "Codec Capture", SUNIV_CODEC_ADC_ACTL,
SUNIV_CODEC_ADC_ACTL_ADC_EN, 0),
/* Analog parts of the DACs */
SND_SOC_DAPM_DAC("Left DAC", "Codec Playback", SUNIV_CODEC_DAC_ACTL,
SUNIV_CODEC_DAC_ACTL_DACAENL, 0),
SND_SOC_DAPM_DAC("Right DAC", "Codec Playback", SUNIV_CODEC_DAC_ACTL,
SUNIV_CODEC_DAC_ACTL_DACAENR, 0),
/* Mixers */
SOC_MIXER_ARRAY("Left Mixer", SUNIV_CODEC_DAC_ACTL,
SUNIV_CODEC_DAC_ACTL_LMIXEN, 0,
suniv_codec_out_mixer_controls),
SOC_MIXER_ARRAY("Right Mixer", SUNIV_CODEC_DAC_ACTL,
SUNIV_CODEC_DAC_ACTL_RMIXEN, 0,
suniv_codec_out_mixer_controls),
SOC_MIXER_ARRAY("ADC Mixer", SUNIV_CODEC_ADC_ACTL,
SND_SOC_NOPM, 0,
suniv_codec_adc_mixer_controls),
/* Headphone outpu path*/
SND_SOC_DAPM_MUX("Headphone Source Playback Route",
SND_SOC_NOPM, 0, 0, suniv_codec_hp_src),
SND_SOC_DAPM_OUT_DRV("Headphone Amp", SUNIV_CODEC_DAC_ACTL,
SUNIV_CODEC_DAC_ACTL_HPPAEN, 0, NULL, 0),
SND_SOC_DAPM_SUPPLY("HPCOM Protection", SUNIV_CODEC_DAC_ACTL,
SUNIV_CODEC_DAC_ACTL_HPCOMPEN, 0, NULL, 0),
SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPCOM", SUNIV_CODEC_DAC_ACTL,
SUNIV_CODEC_DAC_ACTL_HPCOM_FC, 0x3, 0x3, 0),
SND_SOC_DAPM_OUTPUT("HP"),
};
从上面的代码可以看出来,这里面是根据codec的内部硬件框图对各个接口进行抽象定义,也是根据内核中的ASOC框架提供的各种宏,对相关的寄存器进行初始化。
3、struct snd_soc_dapm_route *dapm_routes:动态路由控制,其实就是配置codec各个线路怎么连接,信号怎么走,具体的配置如下:
static const struct snd_soc_dapm_route suniv_codec_codec_dapm_routes[] = {
/* ADC Routes */
{ "ADC", NULL, "ADC Enable" },
/* Microphone Routes */
{ "Mic Amplifier", NULL, "MIC" },
/* DAC Routes */
{ "Left DAC", NULL, "DAC Enable" },
{ "Right DAC", NULL, "DAC Enable" },
/* Left Mixer Routes */
{ "Left Mixer", "Left DAC To Mixer Switch", "Left DAC" },
{ "Left Mixer", "Right DAC To Mixer Switch", "Right DAC" },
{ "Left Mixer", "MICIN To Out Mixer Switch", "Mic Amplifier" },
{ "Left Mixer", "LINEIN To Out Mixer Switch", "LINEIN" },
{ "Left Mixer", "FMINL To Out Mixer Switch", "FMINL" },
/* Right Mixer Routes */
{ "Right Mixer", "Left DAC To Mixer Switch", "Left DAC" },
{ "Right Mixer", "Right DAC To Mixer Switch", "Right DAC" },
{ "Right Mixer", "MICIN To Out Mixer Switch", "Mic Amplifier" },
{ "Right Mixer", "LINEIN To Out Mixer Switch", "LINEIN" },
{ "Right Mixer", "FMINL To Out Mixer Switch", "FMINL" },
/* ADC Mixer Routes */
{"ADC Mixer", "MIC To ADC Mixer Switch" ,"Mic Amplifier" },
{"ADC Mixer", "LINEIN To ADC Mixer Switch" ,"LINEIN" },
{"ADC Mixer", "FMINR To ADC Mixer Switch" ,"FMINR" },
{"ADC Mixer", "FMINL To ADC Mixer Switch" ,"FMINL" },
{"ADC Mixer", "LEFT Out To ADC Mixer Switch" ,"Left Mixer" },
{"ADC Mixer", "RIGHT Out To ADC Mixer Switch" ,"Right Mixer" },
{ "ADC" , NULL ,"ADC Mixer" },
/* Headphone Routes */
{ "Headphone Source Playback Route", "DAC", "Left DAC" },
{ "Headphone Source Playback Route", "DAC", "Right DAC" },
{ "Headphone Source Playback Route", "Mixer", "Left Mixer" },
{ "Headphone Source Playback Route", "Mixer", "Right Mixer" },
{ "Headphone Amp", NULL, "Headphone Source Playback Route" },
{ "HP", NULL, "Headphone Amp" },
{ "HPCOM", NULL, "HPCOM Protection" },
};
上面的结构体数组中,每一个成员代表一个路由线路,里面都是包含三个字符串,第一个是“目的组件”,第三个是“源组件”,中间的那个是连接目的和源的控件,如果是NULL,说明目的和源直连,这里面的控件和组件,都是与上面定义的相对应的。
说到这里,和codec driver相关的数据结构基本上就介绍完了。
(三)、struct snd_soc_card * (*create_card)(struct device *dev) ,这个接口是在machine层创建一个声卡设备,需要结合codec部分和platform部分的数据,这个最后再做介绍
(四)、剩下的几个字段是和DMA寄存器相关的描述
struct reg_field reg_adc_fifoc; /* used for regmap_field */
unsigned int reg_dac_txdata; /* TX FIFO offset for DMA config */
unsigned int reg_adc_rxdata; /* RX FIFO offset for DMA config */
bool has_reset;
u32 dma_max_burst;
下面再来说一下codec部分的DAI驱动,主要是看一下这个数据结构:
static struct snd_soc_dai_driver suniv_codec_dai = {
.name = "Codec",
.ops = &suniv_codec_dai_ops,
.playback = {
.stream_name = "Codec Playback",
.channels_min = 1,
.channels_max = 2,
.rate_min = 8000,
.rate_max = 192000,
.rates = SNDRV_PCM_RATE_CONTINUOUS,
.formats = SNDRV_PCM_FMTBIT_S16_LE |
SNDRV_PCM_FMTBIT_S32_LE,
.sig_bits = 24,
},
.capture = {
.stream_name = "Codec Capture",
.channels_min = 1,
.channels_max = 2,
.rate_min = 8000,
.rate_max = 48000,
.rates = SNDRV_PCM_RATE_CONTINUOUS,
.formats = SNDRV_PCM_FMTBIT_S16_LE |
SNDRV_PCM_FMTBIT_S32_LE,
.sig_bits = 24,
},
};
这里面详细描述了和录音、放音相关的各种参数设置和各种操作接口函数,所有声卡都是这样的套路,不做详细的描述了。
二、下面介绍platform相关的内容,因为在这个ASOC的驱动框架中,大部分工作都是在codec部分来做,而且现在使用的是f1c100s的内置声卡,所以platform部分的配置相对较少。
(一)、先看下组件driver部分,看下这个数据:
static const struct snd_soc_component_driver suniv_codec_component = {
.name = "suniv-codec",
};
因为这是一个内置声卡,codec部分的内部寄存器配置已经包含了platform部分的寄存器配置,所以不需要再做其他的配置
(二)、再看一下DAI的driver部分,看下这个数据:
static struct snd_soc_dai_driver dummy_cpu_dai = {
.name = "suniv-codec-cpu-dai",
.probe = suniv_codec_dai_probe,
.playback = {
.stream_name = "Playback",
.channels_min = 1,
.channels_max = 2,
.rates = SUNIV_CODEC_RATES,
.formats = SUNIV_CODEC_FORMATS,
.sig_bits = 24,
},
.capture = {
.stream_name = "Capture",
.channels_min = 1,
.channels_max = 2,
.rates = SUNIV_CODEC_RATES,
.formats = SUNIV_CODEC_FORMATS,
.sig_bits = 24,
},
};
和codec部分的DAI配置如出一辙。
到这里codec和platform部分基本上已经配置完了,下面需要重点研究一下驱动的probe函数
三、f1c100s的声卡驱动的注册函数,详细的代码如下:
static int suniv_codec_probe(struct platform_device *pdev)
{
struct snd_soc_card *card;
struct suniv_codec *scodec;
const struct suniv_codec_quirks *quirks;
struct resource *res;
void __iomem *base;
int ret;
scodec = devm_kzalloc(&pdev->dev, sizeof(*scodec), GFP_KERNEL);
if (!scodec)
return -ENOMEM;
scodec->dev = &pdev->dev;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
base = devm_ioremap_resource(&pdev->dev, res);
if (IS_ERR(base)) {
dev_err(&pdev->dev, "Failed to map the registers\n");
return PTR_ERR(base);
}
quirks = of_device_get_match_data(&pdev->dev);
if (quirks == NULL) {
dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
return -ENODEV;
}
scodec->regmap = devm_regmap_init_mmio(&pdev->dev, base,
quirks->regmap_config);
if (IS_ERR(scodec->regmap)) {
dev_err(&pdev->dev, "Failed to create our regmap\n");
return PTR_ERR(scodec->regmap);
}
/* Get the clocks from the DT */
scodec->clk_apb = devm_clk_get(&pdev->dev, "apb");
if (IS_ERR(scodec->clk_apb)) {
dev_err(&pdev->dev, "Failed to get the APB clock\n");
return PTR_ERR(scodec->clk_apb);
}
scodec->clk_module = devm_clk_get(&pdev->dev, "codec");
if (IS_ERR(scodec->clk_module)) {
dev_err(&pdev->dev, "Failed to get the module clock\n");
return PTR_ERR(scodec->clk_module);
}
if (quirks->has_reset) {
scodec->rst = devm_reset_control_get_exclusive(&pdev->dev,
NULL);
if (IS_ERR(scodec->rst)) {
dev_err(&pdev->dev, "Failed to get reset control\n");
return PTR_ERR(scodec->rst);
}
}
scodec->gpio_pa = devm_gpiod_get_optional(&pdev->dev, "allwinner,pa",
GPIOD_OUT_LOW);
if (IS_ERR(scodec->gpio_pa)) {
ret = PTR_ERR(scodec->gpio_pa);
if (ret != -EPROBE_DEFER)
dev_err(&pdev->dev, "Failed to get pa gpio: %d\n", ret);
return ret;
}
/* reg_field setup */
scodec->reg_adc_fifoc = devm_regmap_field_alloc(&pdev->dev,
scodec->regmap,
quirks->reg_adc_fifoc);
if (IS_ERR(scodec->reg_adc_fifoc)) {
ret = PTR_ERR(scodec->reg_adc_fifoc);
dev_err(&pdev->dev, "Failed to create regmap fields: %d\n",
ret);
return ret;
}
/* Enable the bus clock */
if (clk_prepare_enable(scodec->clk_apb)) {
dev_err(&pdev->dev, "Failed to enable the APB clock\n");
return -EINVAL;
}
/* Deassert the reset control */
if (scodec->rst) {
ret = reset_control_deassert(scodec->rst);
if (ret) {
dev_err(&pdev->dev,
"Failed to deassert the reset control\n");
goto err_clk_disable;
}
}
/* DMA configuration for TX FIFO */
scodec->playback_dma_data.addr = res->start + quirks->reg_dac_txdata;
scodec->playback_dma_data.maxburst = quirks->dma_max_burst;;
scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
/* DMA configuration for RX FIFO */
scodec->capture_dma_data.addr = res->start + quirks->reg_adc_rxdata;
scodec->capture_dma_data.maxburst = quirks->dma_max_burst;;
scodec->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
ret = snd_soc_register_codec(&pdev->dev, quirks->codec,
&suniv_codec_dai, 1);
if (ret) {
dev_err(&pdev->dev, "Failed to register our codec\n");
goto err_assert_reset;
}
ret = devm_snd_soc_register_component(&pdev->dev,
&suniv_codec_component,
&dummy_cpu_dai, 1);
if (ret) {
dev_err(&pdev->dev, "Failed to register our DAI\n");
goto err_unregister_codec;
}
ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
if (ret) {
dev_err(&pdev->dev, "Failed to register against DMAEngine\n");
goto err_unregister_codec;
}
card = quirks->create_card(&pdev->dev);
if (IS_ERR(card)) {
ret = PTR_ERR(card);
dev_err(&pdev->dev, "Failed to create our card\n");
goto err_unregister_codec;
}
snd_soc_card_set_drvdata(card, scodec);
ret = snd_soc_register_card(card);
if (ret) {
dev_err(&pdev->dev, "Failed to register our card\n");
goto err_unregister_codec;
}
return 0;
err_unregister_codec:
snd_soc_unregister_codec(&pdev->dev);
err_assert_reset:
if (scodec->rst)
reset_control_assert(scodec->rst);
err_clk_disable:
clk_disable_unprepare(scodec->clk_apb);
return ret;
}
这个注册函数主要分为以下几个功能:
1、为驱动从内存中获取资源
2、为驱动从设备树中获取相关的硬件配置参数
3、根据获取的资源分别对platform和codec部分进行注册
4、在machine层注册一个声卡
下面具体分析一下这个注册函数,主要有以下函数:
//获取内存
scodec = devm_kzalloc(&pdev->dev, sizeof(*scodec), GFP_KERNEL)
//获取硬件的资源
res = platform_get_resource(pdev, IORESOURCE_MEM, 0)
//将获取的codec的物理地址做一下映射,获得虚拟地址的基地址
base = devm_ioremap_resource(&pdev->dev, res)
//获取特定平台的数据
quirks = of_device_get_match_data(&pdev->dev)
//从设备树获取总线时钟
scodec->clk_apb = devm_clk_get(&pdev->dev, "apb")
//从设备树获取模块时钟
scodec->clk_module = devm_clk_get(&pdev->dev, "codec")
//注册codec部分
ret = snd_soc_register_codec(&pdev->dev, quirks->codec, &suniv_codec_dai, 1);
//注册platform部分
ret = devm_snd_soc_register_component(&pdev->dev, &suniv_codec_component, &dummy_cpu_dai, 1);
//注册DMA
ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0)
//注册声卡
snd_soc_card_set_drvdata(card, scodec);
//over!
到这里基本上就可以了
将驱动编译成模块,插入到系统,就可以愉快的使用声卡了
先听首歌,看看效果
上一篇: Web应用程序1.4——创建用户账户
下一篇: Django(二)Django 项目创建
推荐阅读