上篇说到音频子系统2.6.x版本的入口在现在4.x版本已经发生了变化,现在就来谈谈4.x版的真正入口。
额,以下都是我自己的一些理解,主要是以DroidPhone的专栏 – Linux音频子系统为参考,如果大家觉得哪里说得有问题,欢迎指正,以期共同进步。
1, 内核版本和Codec型号
Linux 4.9.123 可从以下地址获得
https://mirrors.edge.kernel.org/pub/linux/kernel/v4.x/
本文Codec基于wm8524。
2, 设备树
设备树是音频子系统的入口吗?暂且认为它是吧,这样理解起来方便一些,我们从设备树里找到音频的设置:
wm8524: wm8524 {
compatible = "wlf,wm8524";
clocks = <&clk IMX8MM_CLK_SAI3_ROOT>;
clock-names = "mclk";
wlf,mute-gpios = <&gpio5 21 GPIO_ACTIVE_LOW>;
};
sound-wm8524 {
compatible = "fsl,imx-audio-wm8524";
model = "wm8524-audio";
audio-cpu = <&sai3>;
audio-codec = <&wm8524>;
audio-routing =
"Line Out Jack", "LINEVOUTL",
"Line Out Jack", "LINEVOUTR";
};
里面主要有两部分内容,一部分是Codec(wm8524),另一部分就叫Mechine(sound-wm8524)吧.
从这里面我们了看到几个已经定义好的属性,从Machine入手,
compatible
: 用于匹配设备树对应的驱动。
model
:设备模型,暂时不是很清楚作用。
audio-cpu
: 音频控制单元
audio-codec
: 这个其实就是指上面的Codec
audio-routing
: 音频路径,这个也比较重要,涉及到dapm
从audio-codec
跳到Codec,
compatible
: 匹配codec用的
clocks
: 用到的时钟
clock-names
: 时钟名字
wlf,mute-gpios
: 专用的静音用的gpio定义
设备树就到这里了,要注意上面说到的每一个属性,下面都会再次见到。
3, Machine驱动
搜索sound-wm8524的compatible——“fsl,imx-audio-wm8524”,跳转到文件sound/soc/fsl/imx-wm8524.c
,
static const struct of_device_id imx_wm8524_dt_ids[] = {
{ .compatible = "fsl,imx-audio-wm8524", },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, imx_wm8524_dt_ids);
static struct platform_driver imx_wm8524_driver = {
.driver = {
.name = "imx-wm8524",
.pm = &snd_soc_pm_ops,
.of_match_table = imx_wm8524_dt_ids,
},
.probe = imx_wm8524_probe,
};
module_platform_driver(imx_wm8524_driver);
我们可以清楚地看到,compatible
->imx_wm8524_dt_ids
->imx_wm8524_probe
,最终是触发了imx_wm8524_probe,
static int imx_wm8524_probe(struct platform_device *pdev)
{
struct device_node *cpu_np, *codec_np = NULL;
struct platform_device *cpu_pdev;
struct imx_priv *priv;
struct platform_device *codec_pdev = NULL;
int ret;
struct i2c_client * codec_client = NULL;
const char *dma_name;
priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
{ //根据设备树来注册一些属性,让Linux能在想用的时候找到
priv->pdev = pdev;
cpu_np = of_parse_phandle(pdev->dev.of_node, "audio-cpu", 0);
if (!cpu_np) {
dev_err(&pdev->dev, "cpu dai phandle missing or invalid\n");
ret = -EINVAL;
goto fail;
}
codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0);
if (!codec_np) {
dev_err(&pdev->dev, "phandle missing or invalid\n");
ret = -EINVAL;
goto fail;
}
cpu_pdev = of_find_device_by_node(cpu_np);
if (!cpu_pdev) {
dev_err(&pdev->dev, "failed to find SAI platform device\n");
ret = -EINVAL;
goto fail;
}
if (of_property_read_string_index(cpu_np, "dmas", 0, &dma_name))
{
dev_err(&pdev->dev, "failed to find SDMA name\n");
ret = -EINVAL;
goto fail;
}
codec_pdev = of_find_device_by_node(codec_np);
if (!codec_pdev || !codec_pdev->dev.driver) {
dev_err(&pdev->dev, "failed to find codec platform device\n");
ret = -EINVAL;
goto fail;
}
priv->codec_clk = devm_clk_get(&codec_pdev->dev, "mclk");
if (IS_ERR(priv->codec_clk)) {
ret = PTR_ERR(priv->codec_clk);
dev_err(&pdev->dev, "failed to get codec clk: %d\n", ret);
goto fail;
}
}//end of register
{ //给dai(Dynamic Audio Interface)的指针指定位置,后面改变这个地址的内存可以直接反映到这个指针的位置,imx_wm8524_dai的定义见3.0
priv->card.dai_link = imx_wm8524_dai;
imx_wm8524_dai[0].codec_of_node = codec_np;
imx_wm8524_dai[0].cpu_dai_name = dev_name(&cpu_pdev->dev);
imx_wm8524_dai[0].platform_of_node = cpu_np;
imx_wm8524_dai[0].playback_only = false;
imx_wm8524_dai[0].capture_only= false;
}
{ // 这里是大头,实现了很多功能
priv->card.late_probe = imx_wm8524_late_probe;
priv->card.num_links = 1;
priv->card.dev = &pdev->dev;
priv->card.owner = THIS_MODULE;
priv->card.dapm_widgets = imx_wm8524_dapm_widgets;
priv->card.num_dapm_widgets = ARRAY_SIZE(imx_wm8524_dapm_widgets);
}
// 解析sound card name,这里用到的是DTS里的·model·属性
ret = snd_soc_of_parse_card_name(&priv->card, "model");
if (ret)
goto fail;
// 解析audio_routing,这里用到的是DTS里的·audio-routing·属性,这个涉及到dapm
// 的widget和routes部分
ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
if (ret)
goto fail;
// 设置drvdata,把priv设到priv->card里
snd_soc_card_set_drvdata(&priv->card, priv);
// 注册sound_card
ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
if (ret) {
dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
goto fail;
}
}
ret = 0;
fail:
if (cpu_np)
of_node_put(cpu_np);
if (codec_np)
of_node_put(codec_np);
return ret;
}
上面有注释的地方需要注意
终于写完了这个流程了,读者应该也发现了,里面出现了几个东西不太好理解
- dapm: Dynamic audio power management 动态音频电源关系,和功耗相关的。
- widgets: dapm的一个组件。
- routes: dapm的一个组件。
- kcontrol: dapm的一个组件。
- dai_link: Digital Audio Interfaces 数字音频接口。
今后的几篇会详细的分门别类的来介绍这几个部分。