Linux Audio (4) ASOC代码分析-基于kernel3.4.2

ASOC代码分析-基于kernel3.4.2

Linux kernel版本:3.4.2

Overview

linux ASoC音频设备驱动
ASoC是ALSA在SoC方面的发展和演变,它的本质仍然属于ALSA,但是在ALSA架构基础上对CPU相关的代码和Codec相关的代码进行了分离,其原因是采用传统ALSA架构情况下,同一型号的Codec工作于不同的CPU时,需要不同的驱动,这是不符合代码重用的要求的。

ASoC主要由3部分组成:
(1)Codec驱动,这一部分只关系Codec本身,与CPU相关的特性不由此部分操作
(2)平台驱动,这一部分只关心CPU本身,不关系Codec,它主要处理了两个问题:DMA引擎和SoC解除的PCM、IIS或AC’97数字接口控制。
(3)板驱动,这一部分将平台驱动和Codec驱动绑定在一起,描述了板一级的硬件特征

以上3部分中,1和2基本都可以仍然是通用的驱动了,即Codec驱动认为自己可以连接任意CPU,而CPU的IIS、PCM、或AC’97接口对应的平台驱动则认为自己可以连接
符号其接口类型的Codec,只有3是不通用的,由特定的电路板上具体的CPU和Codec确定,因此它很像一个插座,上面插着Codec和平台这两个插头。ASoC的用户空间编程方法与ALSA完全一致。

下面以实例来分析函数的调用过程:

machine: sound\soc\samsung\s3c24xx_uda134x.c
platform: sound\soc\samsung\s3c24xx-i2s.c
codec:   sound\soc\codecs\uda134x.c

抓住小尾巴dai_link:

static struct snd_soc_dai_link s3c24xx_uda134x_dai_link = {
	.name = "UDA134X",
	.stream_name = "UDA134X",
	.codec_name = "uda134x-codec",
	.codec_dai_name = "uda134x-hifi",
	.cpu_dai_name = "s3c24xx-iis",
	.ops = &s3c24xx_uda134x_ops,
	.platform_name	= "samsung-audio",
};

Platform

Platform分为两部分:Platform(DMA)和cpu dai(iis);
platform: sound\soc\samsung\s3c24xx-i2s.c

CPU DAI

.cpu_dai_name = "s3c24xx-iis",    // 2440(CPU)的哪个DAI接口

下面是搜到的"s3c24xx-iis"信息,有platform_driver和platform_device,匹配后会执行driver的probe函数:
.probe = s3c24xx_iis_dev_probe,在注册时会把DMA的file_operation操作函数集注册到soc-core中。

linux-3.4.2\sound\soc\samsung\s3c24xx-i2s.c

s3c24xx_iis_dev_probe(struct platform_device *pdev)
	snd_soc_register_dai(&pdev->dev, &s3c24xx_i2s_dai);//Register a DAI with the ASoC core
		list_add(&dai->list, &dai_list); //把cpu dai添加到dai_list中,在snd_soc_instantiate_cards中会去遍历dai_list

CPU DMA

.platform_name= "samsung-audio",  // 2440(CPU)的哪个DMA驱动程序

下面是搜到的"samsung-audio"信息,有platform_driver和platform_device,匹配后会执行driver的probe函数:
.probe = samsung_asoc_platform_probe,在注册时会把DMA的file_operation操作函数集注册到soc-core中。

linux-3.4.2\sound\soc\samsung\dma.c

samsung_asoc_platform_probe(struct platform_device *pdev)
	snd_soc_register_platform(&pdev->dev, &samsung_asoc_platform);
		list_add(&platform->list, &platform_list);

套路和cpu dai是一样的,在snd_soc_register_platform()函数中把platform->list加到platform_list中,然后在snd_soc_instantiate_cards()中遍历platform_list注册设备。

总结: 通过machine中的snd_soc_dai_link中的platform_name和cpu_dai_name分别查找平台的dma设备驱动和cpu侧的dai驱动。最终会将这dai保存到component->dai_list中,platform保存到platform_list当中。然后将component放入到component_list链表中。这些数据会在Machine代码的开始过程中进行匹配操作。

Codec

codec: sound\soc\codecs\uda134x.c

sound\soc\codecs\uda134x.c
uda134x_codec_probe(struct platform_device *pdev)
	snd_soc_register_codec(&pdev->dev,&soc_codec_dev_uda134x, &uda134x_dai, 1); -->snd_soc_instantiate_cards()
		list_add(&codec->list, &codec_list);
		snd_soc_register_dais(dev, dai_drv, num_dai);
			list_add(&dai->list, &dai_list);

Mechine

mechine担负着定义dai_link结构体,注册逻辑设备的神圣使命。在Asoc结构中,声卡的注册过程如下:
devm_snd_soc_register_card --> snd_soc_register_card --> ①soc_init_dai_link ②snd_soc_instantiate_card-->③snd_card_register();

当codec、dai、platform的device name和driver name匹配时,就会执行对应Driver的probe()函数。
machine: sound\soc\samsung\s3c24xx_uda134x.c中,mechine的platform_driver和platform_device名字匹配后就会执行Driver的probe()函数。

Mechine中向Linux平台设备总线注册名为“soc-audio” 的设备

该函数里的Platform 指的是Linux设备总线模型中的概念呦!
static int s3c24xx_uda134x_probe(struct platform_device *pdev)
{
	// 为snd soc card申请一个名为"soc-audio"的platform_device结构体
	s3c24xx_uda134x_snd_device = platform_device_alloc("soc-audio", -1);
	// 把snd_soc_card结构体作为私有数据填充到platform_device结构体中;
	platform_set_drvdata(s3c24xx_uda134x_snd_device, &snd_soc_s3c24xx_uda134x);
	// 把snd soc card "soc-audio"注册到platform设备总线上。
	ret = platform_device_add(s3c24xx_uda134x_snd_device);
}

对应的"soc-audio" platform_driver在soc-core.c (sound\soc)z中定义:

/* ASoC platform driver */
static struct platform_driver soc_driver = {
	.driver		= {
		.name		= "soc-audio",
		.owner		= THIS_MODULE,
		.pm		= &snd_soc_pm_ops,
	},
	.probe		= soc_probe,
	.remove		= soc_remove,
};
// 在其入口函数中将结构体注册进platform_driver中
static int __init snd_soc_init(void)
{
	return platform_driver_register(&soc_driver);
}
module_init(snd_soc_init);

当“soc-audio”的Driver和Device匹配后,会执行sound\soc\soc-core.c-->struct soc_driver.probe = soc_probe()函数。

/* probes a new socdev */
static int soc_probe(struct platform_device *pdev)
{
	// 在machine: sound\soc\samsung\s3c24xx_uda134x.c中在s3c24xx_uda134x_probe()中
	// 已经在把snd_soc_card结构体填充到了device的私有数据中,这里再取出来用来向内核注册Driver
	struct snd_soc_card *card = platform_get_drvdata(pdev); 
	/* Bodge while we unpick instantiation */
	card->dev = &pdev->dev;
	// 向Asoc core注册声卡设备
	ret = snd_soc_register_card(card); --->>> snd_soc_instantiate_card()
}

下面看下函数调用

snd_soc_register_card(struct snd_soc_card *card) 
	snd_soc_instantiate_cards(void)	
		soc_bind_dai_link(card, i);//从dai_list中遍历cpu dai,codec dai
		snd_card_create(...., card->owner, 0, &card->snd_card);
	/* 下面开始执行一些probe函数,比如codec, dai, platform(dma)等file_operation中的结构体 */	
		card->probe(card);
		soc_probe_dai_link(card, i, order);
		snd_card_register(card->snd_card);

在ALSA时,我们分析过这个函数,这样就完美的和ALSA接轨了。在snd_card_register()向内核注册逻辑设备。

/**
 *  snd_card_register - register the soundcard
 *  @card: soundcard structure
 *
 *  This function registers all the devices assigned to the soundcard.
 *  Until calling this, the ALSA control interface is blocked from the
 *  external accesses.  Thus, you should call this function at the end
 *  of the initialization of the card.
 *
 *  Returns zero otherwise a negative error code if the registration failed.
 */
int snd_card_register(struct snd_card *card)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值