dummy codec
1. 什么情况下需要用到虚拟codec
一般在linux 或者 android 系统中,没有外挂codec,主控上也没内置codec,但是外挂了一个i2s、spdif的接口的芯片;
比如说lt9611 ,芯片内置i2s 接口,用于接收主控的i2s 音频信号,等案例
这个时候就需要一个dummy codec ,挂到平台上,注册声卡出来。
当播放音频时:主控播放*.wav ----平台i2s rx ------设备lt9611 i2s —>输出
2. 虚拟codec 的注册到平台的原理
实际上,就是模拟出一个codec,只有codec驱动的框架,在框架里添加codec DAI
codec dai 和平台dai 在machine 注册匹配的时候,能够link上,就会注册声卡出来。
3.参考代码
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <sound/core.h>
#include <sound/tlv.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/initval.h>
#include <linux/of.h>
#include <sound/soc.h>
struct dummy_codec_private {
struct platform_device *dummy_codec;
};
#define DUMMY_CODEC_RATES (SNDRV_PCM_RATE_8000_192000)
#define DUMMY_CODEC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
static int dummy_codec_pcm_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params,
struct snd_soc_dai *dai)
{
return 0;
}
static int dummy_codec_set_dai_fmt(struct snd_soc_dai *codec_dai,
unsigned int fmt)
{
return 0;
}
static int dummy_codec_mute(struct snd_soc_dai *dai, int mute)
{
return 0;
}
#ifdef DUMMY_CODEC_ZYY
static const struct snd_soc_dapm_widget dummy_codec_dapm_widgets[] = {
/* Output Side */
/* DACs */
SND_SOC_DAPM_DAC("Left DAC", "HIFI Playback",
SND_SOC_NOPM, 0, 0),
SND_SOC_DAPM_DAC("Right DAC", "HIFI Playback",
SND_SOC_NOPM, 7, 0),
/* Output Lines */
SND_SOC_DAPM_OUTPUT("LOUTL"),
SND_SOC_DAPM_OUTPUT("LOUTR"),
};
static const struct snd_soc_dapm_route dummy_codec_dapm_routes[] = {
{"LOUTL", NULL, "Left DAC"},
{"LOUTR", NULL, "Right DAC"},
};
#endif
static struct snd_soc_dai_ops dummy_codec_ops = {
.hw_params = dummy_codec_pcm_hw_params,
.set_fmt = dummy_codec_set_dai_fmt,
.digital_mute = dummy_codec_mute,
};
struct snd_soc_dai_driver dummy_codec_dai = {
.name = "dummy",
//.id = 1,
.playback = {
.stream_name = "HIFI Playback",
.channels_min = 1,
.channels_max = 8,
.rates = DUMMY_CODEC_RATES,
.formats = DUMMY_CODEC_FORMATS,
},
.capture = {
.stream_name = "HIFI Capture",
.channels_min = 1,
.channels_max = 2,
.rates = DUMMY_CODEC_RATES,
.formats = DUMMY_CODEC_FORMATS,
},
.ops = &dummy_codec_ops,
};
static int dummy_codec_probe(struct snd_soc_component *component)
{
struct dummy_codec_private *dummy_codec;
dummy_codec = snd_soc_component_get_drvdata(component);
if(dummy_codec == NULL){
pr_err("%s, codec is not registered\n", __func__);
printk("zyy.........................%s..................%d",__func__,__LINE__);
return 0;
}
return 0;
}
static void dummy_codec_remove(struct snd_soc_component *component)
{
if(component == NULL)
printk("zyy.........................%s..................%d",__func__,__LINE__);
return;
};
struct snd_soc_component_driver soc_codec_dev_dummy_codec = {
.probe = dummy_codec_probe,
.remove = dummy_codec_remove,
#ifdef DUMMY_CODEC_ZYY
.dapm_widgets = dummy_codec_dapm_widgets,
.num_dapm_widgets = ARRAY_SIZE(dummy_codec_dapm_widgets),
.dapm_routes = dummy_codec_dapm_routes,
.num_dapm_routes = ARRAY_SIZE(dummy_codec_dapm_routes),
#endif
};
static const struct of_device_id dummy_codec_dt_match[] = {
{.compatible = "dummy_codec",
},
{},
};
static int dummy_codec_platform_probe(struct platform_device *pdev)
{
struct dummy_codec_private *dummy_codec;
int ret;
printk("zyy.........................%s..................%d",__func__,__LINE__);
dummy_codec = kzalloc(sizeof(struct dummy_codec_private), GFP_KERNEL);
if (dummy_codec == NULL)
return -ENOMEM;
ret = devm_snd_soc_register_component(&pdev->dev, &soc_codec_dev_dummy_codec,
&dummy_codec_dai, 1);
if (ret < 0)
printk("zyy.........................%s..................%d",__func__,__LINE__);
return ret;
}
static int dummy_codec_platform_remove(struct platform_device *pdev)
{
snd_soc_unregister_component(&pdev->dev);
return 0;
}
static struct platform_driver dummy_codec_platform_driver = {
.driver = {
.name = "dummy",
.owner = THIS_MODULE,
.of_match_table = dummy_codec_dt_match,
},
.probe = dummy_codec_platform_probe,
.remove = dummy_codec_platform_remove,
};
static int __init dummy_codec_init(void)
{
return platform_driver_register(&dummy_codec_platform_driver);
}
static void __exit dummy_codec_exit(void)
{
platform_driver_unregister(&dummy_codec_platform_driver);
}
module_init(dummy_codec_init);
module_exit(dummy_codec_exit);
MODULE_AUTHOR("ZYY, Inc.");
MODULE_DESCRIPTION("ASoC dummy_codec driver");
MODULE_LICENSE("GPL");