一、什么是ASOC
在嵌入式系統里面的聲卡驅動為ASOC(ALSA System on Chip) ,它是在ALSA 驅動程序上封裝的一層,分為3大部分,Machine,Platform和Codec?,三部分的關系如下圖所示:其中Machine是指我們的開發板,Platform是指Soc,而Codec是指編解碼器(如uda1341)
二、嵌入式系統聲卡注冊過程
這里我們以mini2440開發板為例?
安裝新驅動insmod alsa/driver/myalsa/platform/s3c2440_iis.ko
insmod alsa/driver/myalsa/platform/s3c2440_dma.ko
insmod alsa/driver/myalsa/codec/uda1341.ko
insmod alsa/driver/myalsa/machine/s3c2440_uda1341.ko
首先我們往內核安裝我們寫好的聲卡驅動(Machine部分的ko文件一定要放在最后面,Platform和Codec 則無特殊要求,下面會講為什么)
1.platform部分
1.1 cpu_dai
?安裝s3c2440_iis.ko的時候會注冊平臺驅動s3c24xx_iis_driver,因為內核中有名字同為"s3c24xx-iis"的平臺設備,所以調用了對應的probe函數s3c24xx_iis_dev_probe,該函數最終會把s3c24xx_i2s_dai(cpu_dai硬件操作相關)放入鏈表dai_list,并命名為"s3c24xx-iis"(后面Machine驅動根據這個名字來實例化聲卡)
1. platform:
1.1 s3c24xx-i2s.c : 把s3c24xx_i2s_dai放入鏈表dai_list, .name = "s3c24xx-iis",
s3c24xx_iis_dev_probesnd_soc_register_dai(&pdev->dev, &s3c24xx_i2s_dai);list_add(&dai->list, &dai_list);
平臺驅動
static struct platform_driver s3c24xx_iis_driver = {.probe = s3c24xx_iis_dev_probe,.remove = s3c24xx_iis_dev_remove,.driver = {.name = "s3c24xx-iis",.owner = THIS_MODULE,},
};平臺設備
struct platform_device s3c_device_iis = {.name = "s3c24xx-iis",.id = -1,.num_resources = ARRAY_SIZE(s3c_iis_resource),.resource = s3c_iis_resource,.dev = {.dma_mask = &s3c_device_iis_dmamask,.coherent_dma_mask = 0xffffffffUL}
};
static struct snd_soc_dai_ops s3c24xx_i2s_dai_ops = {.trigger = s3c24xx_i2s_trigger,.hw_params = s3c24xx_i2s_hw_params,.set_fmt = s3c24xx_i2s_set_fmt,.set_clkdiv = s3c24xx_i2s_set_clkdiv,.set_sysclk = s3c24xx_i2s_set_sysclk,
};//cpu_dai相關的硬件操作
static struct snd_soc_dai_driver s3c24xx_i2s_dai = {.probe = s3c24xx_i2s_probe,.suspend = s3c24xx_i2s_suspend,.resume = s3c24xx_i2s_resume,.playback = {.channels_min = 2,.channels_max = 2,.rates = S3C24XX_I2S_RATES,.formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},.capture = {.channels_min = 2,.channels_max = 2,.rates = S3C24XX_I2S_RATES,.formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},.ops = &s3c24xx_i2s_dai_ops,
};
1.2?platform_dma
安裝s3c2440_dma.ko時,把samsung_asoc_platform放入了鏈表platform_list,并命名為"samsung-audio"(后面Machineko驅動根據這個名字來實例化聲卡)
1.2 sound/soc/samsung/dma.c : 把samsung_asoc_platform放入了鏈表platform_list, .name = "samsung-audio",
samsung_asoc_platform_probesnd_soc_register_platform(&pdev->dev, &samsung_asoc_platform);list_add(&platform->list, &platform_list);
static struct snd_pcm_ops dma_ops = {.open = dma_open,.close = dma_close,.ioctl = snd_pcm_lib_ioctl,.hw_params = dma_hw_params,.hw_free = dma_hw_free,.prepare = dma_prepare,.trigger = dma_trigger,.pointer = dma_pointer,.mmap = dma_mmap,
};
//cpu_dam相關的硬件操作
static struct snd_soc_platform_driver samsung_asoc_platform = {.ops = &dma_ops,.pcm_new = dma_new,.pcm_free = dma_free_dma_buffers,
};
2. codec注冊部分
安裝uda1341.ko 時,把soc_codec_dev_uda134x和uda134x_dai分別放入dai_list和codec_list鏈表(用于后面的Machineko驅動實例化聲卡)
2. codec: uda134x.c
uda134x_codec_probesnd_soc_register_codec(&pdev->dev,&soc_codec_dev_uda134x, &uda134x_dai, 1);struct snd_soc_codec *codec;codec->driver = codec_drv; = &soc_codec_dev_uda134xsnd_soc_register_dais(dev, dai_drv, num_dai); // uda134x_dailist_add(&dai->list, &dai_list); : 把uda134x_dai放入了鏈表dai_listlist_add(&codec->list, &codec_list); 把soc_codec_dev_uda134x放入了鏈表codec_list
//設置codec芯片的硬件操作
static struct snd_soc_codec_driver soc_codec_dev_uda134x = {.probe = uda134x_soc_probe,.remove = uda134x_soc_remove,.suspend = uda134x_soc_suspend,.resume = uda134x_soc_resume,.reg_cache_size = sizeof(uda134x_reg),.reg_word_size = sizeof(u8),.reg_cache_default = uda134x_reg,.reg_cache_step = 1,.read = uda134x_read_reg_cache,.write = uda134x_write,.set_bias_level = uda134x_set_bias_level,
};
?
static struct snd_soc_dai_ops uda134x_dai_ops = {.startup = uda134x_startup,.shutdown = uda134x_shutdown,.hw_params = uda134x_hw_params,.digital_mute = uda134x_mute,.set_sysclk = uda134x_set_dai_sysclk,.set_fmt = uda134x_set_dai_fmt,
};//codec_dai相關的硬件操作
static struct snd_soc_dai_driver uda134x_dai = {.name = "uda134x-hifi",/* playback capabilities */.playback = {.stream_name = "Playback",.channels_min = 1,.channels_max = 2,.rates = UDA134X_RATES,.formats = UDA134X_FORMATS,},/* capture capabilities */.capture = {.stream_name = "Capture",.channels_min = 1,.channels_max = 2,.rates = UDA134X_RATES,.formats = UDA134X_FORMATS,},/* pcm operations */.ops = &uda134x_dai_ops,
};
3. machine部分
machine部分內容很多,總的調用過程如下:
s3c24xx_uda134x_probes3c24xx_uda134x_snd_device = platform_device_alloc("soc-audio", -1);platform_set_drvdata(s3c24xx_uda134x_snd_device, &snd_soc_s3c24xx_uda134x); platform_device_add(s3c24xx_uda134x_snd_device);.....soc_probesnd_soc_register_card(card); // card = &snd_soc_s3c24xx_uda134xcard->rtd = devm_kzalloc(card->dev,...card->rtd[i].dai_link = &card->dai_link[i]; // &s3c24xx_uda134x_dai_linklist_add(&card->list, &card_list);snd_soc_instantiate_cards(); // 實例化聲卡snd_soc_instantiate_card(card);3.1 /* bind DAIs */for (i = 0; i < card->num_links; i++)soc_bind_dai_link(card, i);3.1.1 /* find CPU DAI */rtd->cpu_dai = cpu_dai; = //&s3c24xx_i2s_dai3.1.2 /* find_codec */rtd->codec = codec; = // codec, codec->driver=&soc_codec_dev_uda134x3.1.3 /* find CODEC DAI */ rtd->codec_dai = codec_dai; // = &uda134x_dai3.1.4 /* find_platform */rtd->platform = platform; // = &samsung_asoc_platform3.2 /* initialize the register cache for each available codec */ret = snd_soc_init_codec_cache(codec, compress_type);3.3 snd_card_createerr = snd_ctl_create(card);static struct snd_device_ops ops = {.dev_free = snd_ctl_dev_free,.dev_register = snd_ctl_dev_register,.dev_disconnect = snd_ctl_dev_disconnect,};snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);dev->ops = ops;3.4 /* early DAI link probe */soc_probe_dai_link /* probe the cpu_dai *//* probe the CODEC *//* probe the platform *//* probe the CODEC DAI *//* create the pcm */ret = soc_new_pcm(rtd, num);struct snd_pcm_ops *soc_pcm_ops = &rtd->ops;soc_pcm_ops->open = soc_pcm_open;soc_pcm_ops->close = soc_pcm_close;soc_pcm_ops->hw_params = soc_pcm_hw_params;soc_pcm_ops->hw_free = soc_pcm_hw_free;soc_pcm_ops->prepare = soc_pcm_prepare;soc_pcm_ops->trigger = soc_pcm_trigger;soc_pcm_ops->pointer = soc_pcm_pointer;snd_pcm_newstatic struct snd_device_ops ops = {.dev_free = snd_pcm_dev_free,.dev_register = snd_pcm_dev_register,.dev_disconnect = snd_pcm_dev_disconnect,};err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)dev->ops = ops;pcm->private_data = rtd;3.5 snd_card_registersnd_device_register_allerr = dev->ops->dev_register(dev)調用前面snd_pcm_new的snd_pcm_dev_registersnd_pcm_dev_registererr = snd_register_device_for_dev(devtype, pcm->card,pcm->device,&snd_pcm_f_ops[cidx],pcm, str, dev);
安裝s3c2440_uda1341.ko時,注冊平臺驅動s3c24xx_uda134x_driver,因為內核中有同名的平臺設備mini2440_audio,所以調用了s3c24xx_uda134x_probe函數
//平臺驅動
static struct platform_driver s3c24xx_uda134x_driver = {.probe = s3c24xx_uda134x_probe,.remove = s3c24xx_uda134x_remove,.driver = {.name = "s3c24xx_uda134x",.owner = THIS_MODULE,},
};//平臺設備
static struct platform_device mini2440_audio = {.name = "s3c24xx_uda134x",.id = 0,.dev = {.platform_data = &mini2440_audio_pins,},
};
①s3c24xx_uda134x_probe函數調用platform_set_drvdata把snd_soc_card結構體snd_soc_s3c24xx_uda134x(含有dai_link:用來負責連接Platform
和Codec,實例化的時候會用到)保存在pdev(platform_device)->dev->p->driver_data中,然后platform_device_add(s3c24xx_uda134x_snd_device),因為內核中有同名的平臺驅動soc_driver,所以調用該驅動的probe函數(soc_probe函數)
s3c24xx_uda134x_snd_device = platform_device_alloc("soc-audio", -1);platform_set_drvdata(s3c24xx_uda134x_snd_device, &snd_soc_s3c24xx_uda134x); dev_set_drvdata(&pdev->dev, data);dev->p->driver_data = data; platform_device_add(s3c24xx_uda134x_snd_device);
//指定了platform和codec
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",
};static struct snd_soc_card snd_soc_s3c24xx_uda134x = {.name = "S3C24XX_UDA134X",.dai_link = &s3c24xx_uda134x_dai_link,.num_links = 1, //只有1個dai_link
};
/* 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,
};
②soc_probe函數調用platform_get_drvdata函數把第①步的snd_soc_card結構體snd_soc_s3c24xx_uda134x(含有dai_link:用來負責連接Platform和Codec,實例化的時候會用到)取出來,然后調用snd_soc_register_card函數注冊它。
soc_probestruct snd_soc_card *card = platform_get_drvdata(pdev);return dev->p->driver_data;snd_soc_register_card(card); // card = &snd_soc_s3c24xx_uda134x
③snd_soc_register_card函數取出snd_soc_card結構體的dai_link(snd_soc_s3c24xx_uda134x結構體只有一個dai_link,這里的for只執行一次),然后調用snd_soc_instantiate_cards函數實例化聲卡,其實就是根據snd_soc_s3c24xx_uda134x結構體里面的dai_link指定的名字,在dai_list、platform_list、codec_list鏈表找到我們之前platform和codec注冊部分放入隊列的那四個結構體。(soc_bind_dai_link函數逐個掃描這三個鏈表,根據card->dai_link[]中的名稱進行匹配,匹配后把相應的codec,dai和platform實例賦值到(snd_soc_card)card->rtd[]中(snd_soc_pcm_runtime)。經過這個過程后,snd_soc_pcm_runtime:(card->rtd)中保存了本Machine中使用的Codec,DAI和Platform驅動的信息)
snd_soc_register_cardcard->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *(card->num_links + card->num_aux_devs),GFP_KERNEL);for (i = 0; i < card->num_links; i++)card->rtd[i].dai_link = &card->dai_link[i];list_add(&card->list, &card_list);snd_soc_instantiate_cards();snd_soc_instantiate_card(card);soc_bind_dai_link(card, i);/* find CPU DAI */rtd->cpu_dai = cpu_dai; = //&s3c24xx_i2s_dai/* find_codec */rtd->codec = codec; = // codec, codec->driver=&soc_codec_dev_uda134x/* find CODEC DAI */ rtd->codec_dai = codec_dai; // = &uda134x_dai/* find_platform */rtd->platform = platform; // = &samsung_asoc_platform
④調用傳統ALSA聲卡驅動的API,snd_card_create函數,該函數再調用snd_ctl_create,snd_ctl_create函數調用snd_device_new把snd_device_ops結構體ops填充到snd_device的ops成員(dev->ops = ops;),然后把snd_device掛到(snd_card)card->devices鏈表中。后面第⑧步snd_card_register的時候會用到snd_device_ops結構體的dev_register成員。目前我們只是先把它掛在一個鏈表中,如下圖:
3.3 snd_card_createerr = snd_ctl_create(card);static struct snd_device_ops ops = {.dev_free = snd_ctl_dev_free,.dev_register = snd_ctl_dev_register,.dev_disconnect = snd_ctl_dev_disconnect,};snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);dev->ops = ops;
⑤soc_probe_dai_link函數先是調用前面第三步找出來的cpu_dai等的probe函數(例如:s3c24xx_i2s_dai結構體的s3c24xx_iis_dev_probe),說白了就是硬件初始化。然后調用soc_new_pcm。
soc_probe_dai_link /* probe the cpu_dai */ret = cpu_dai->driver->probe(cpu_dai);/* probe the CODEC */ret = soc_probe_codec(card, codec);/* probe the platform */ret = platform->driver->probe(platform);/* probe the CODEC DAI */ret = codec_dai->driver->probe(codec_dai);/* create the pcm */ret = soc_new_pcm(rtd, num);
⑥soc_new_pcm函數函數先是給card->rtd->ops結構體里的函數指針賦值(APP調用的程序的過程中會通過這些函數再去調用到硬件相關的函數(如前面platform的s3c24xx_i2s_dai)),然后把card->rtd->ops的地址賦給substream->ops。最后調用snd_pcm_new。
ret = soc_new_pcm(rtd, num);struct snd_pcm_ops *soc_pcm_ops = &rtd->ops;soc_pcm_ops->open = soc_pcm_open;soc_pcm_ops->close = soc_pcm_close;soc_pcm_ops->hw_params = soc_pcm_hw_params;soc_pcm_ops->hw_free = soc_pcm_hw_free;soc_pcm_ops->prepare = soc_pcm_prepare;soc_pcm_ops->trigger = soc_pcm_trigger;soc_pcm_ops->pointer = soc_pcm_pointer;snd_pcm_newsnd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, soc_pcm_ops);substream->ops = ops;
或snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, soc_pcm_ops);substream->ops = ops;
⑦snd_pcm_new是傳統的ALSA聲卡驅動API,這一步和第④步是相似的,區別在于第④步對應的是snd_control,這里是snd_pcm的。snd_pcm_new執行完又回到soc_new_pcm函數的,這里有一個(snd_pcm)pcm->private_data = rtd;后面我們APP調用程序的時候會從pcm->private_data取出rtd(rtd里有對應我們開發板的cpu_dai、codec_dai...)
snd_pcm_newstatic struct snd_device_ops ops = {.dev_free = snd_pcm_dev_free,.dev_register = snd_pcm_dev_register,.dev_disconnect = snd_pcm_dev_disconnect,};err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)dev->ops = ops;pcm->private_data = rtd;
⑧snd_card_register,該函數會調用前面第④、⑦步的snd_ctl_dev_register和snd_pcm_dev_register(通過snd_device_register_all()注冊所有掛在該聲卡下的邏輯設備,snd_device_register_all()實際上是通過snd_card的devices鏈表,遍歷所有的snd_device,并且調用snd_device的ops->dev_register()來實現各自設備的注冊的),這兩個函數也是傳統的ALSA聲卡的API,它們最后都會調用到snd_register_device_for_dev函數,將snd_ctl_f_ops、snd_pcm_f_ops作為snd_register_device_for_dev的參數被傳入,并被記錄在snd_minors[minor]中的字段f_ops中,并創建設備節點(device_create(class_create則是在sound_core.c中))。用戶程序需要打開control、pcm設備時,驅動程序通過snd_minors[]全局數組和此設備號,可以獲得snd_ctl_f_ops、snd_pcm_f_ops結構中的各個回調函數(在另外一篇文章里介紹)
3.5 snd_card_registersnd_device_register_allerr = dev->ops->dev_register(dev)調用前面snd_ctl_create的snd_ctl_dev_register和snd_pcm_new的snd_pcm_dev_registersnd_ctl_dev_registersnd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,&snd_ctl_f_ops, card, name)snd_register_device_for_devpreg->type = type;preg->card = card ? card->number : -1;preg->device = dev;preg->f_ops = f_ops;preg->private_data = private_data;snd_minors[minor] = preg;preg->dev = device_create(sound_class, device, MKDEV(major, minor),private_data, "%s", name);和snd_pcm_dev_registererr = snd_register_device_for_dev(devtype, pcm->card,pcm->device,&snd_pcm_f_ops[cidx],pcm, str, dev);
static const struct file_operations snd_ctl_f_ops =
{.owner = THIS_MODULE,.read = snd_ctl_read,.open = snd_ctl_open,.release = snd_ctl_release,.llseek = no_llseek,.poll = snd_ctl_poll,.unlocked_ioctl = snd_ctl_ioctl,.compat_ioctl = snd_ctl_ioctl_compat,.fasync = snd_ctl_fasync,
};const struct file_operations snd_pcm_f_ops[2] = {{.owner = THIS_MODULE,.write = snd_pcm_write,.aio_write = snd_pcm_aio_write,.open = snd_pcm_playback_open,.release = snd_pcm_release,.llseek = no_llseek,.poll = snd_pcm_playback_poll,.unlocked_ioctl = snd_pcm_playback_ioctl,.compat_ioctl = snd_pcm_ioctl_compat,.mmap = snd_pcm_mmap,.fasync = snd_pcm_fasync,.get_unmapped_area = snd_pcm_get_unmapped_area,},{.owner = THIS_MODULE,.read = snd_pcm_read,.aio_read = snd_pcm_aio_read,.open = snd_pcm_capture_open,.release = snd_pcm_release,.llseek = no_llseek,.poll = snd_pcm_capture_poll,.unlocked_ioctl = snd_pcm_capture_ioctl,.compat_ioctl = snd_pcm_ioctl_compat,.mmap = snd_pcm_mmap,.fasync = snd_pcm_fasync,.get_unmapped_area = snd_pcm_get_unmapped_area,}
};
?至此,machine部分的任務已經完成。
?
4.?字符設備注冊
在sound/core/sound.c中有alsa_sound_init()函數,register_chrdev中的參數major與之前創建pcm設備是device_create時的major是同一個,這樣的結果是,當應用程序open設備文件/dev/snd/pcmCxDxp時,會進入snd_fops的open回調函數,該open函數以次設備號為索引,從snd_minors全局數組中取出當初注冊conrol、pcm設備時填充的snd_minor結構,然后從snd_minor結構中取出control、pcm設備的f_ops,并且把file->f_op替換為pcm設備的f_ops,緊接著直接調用pcm設備的f_ops->open()...
#define CONFIG_SND_MAJOR 116 /* standard configuration */
static int major = CONFIG_SND_MAJOR;static const struct file_operations snd_fops =
{.owner = THIS_MODULE,.open = snd_open,.llseek = noop_llseek,
};alsa_sound_initregister_chrdev(major, "alsa", &snd_fops)
調用過程我們在下一篇文章再來詳細介紹。
?
?