Linux Audio-driven Learning: (1) ASOC analysis

Source: Internet
Author: User

First, Audio architecture overview

(1)ALSA is the acronym for Advanced Linux Sound Architecture, which has become the mainstream audio architecture for Linux and wants to learn more
For information and knowledge about this open source project at ALSA, please see the following URL: http://www.alsa-project.org/.
In the kernel device driver layer, ALSA provides alsa-driver, while at the application layer, ALSA provides us with alsa-lib, as long as the application calls
Alsa-lib provides an API that enables control of the underlying audio hardware.

(2)PCM is the abbreviation of English Pulse-code modulation, the Chinese translation is pulse code modulation. We know that in real life, the sound of the human ear hears
is an analog signal, PCM is a technology to convert sound from analog to digital signal, his principle is simply to use a fixed frequency
Sampling the analog signal, the sampled signal in the waveform looks like a series of successive amplitude of the pulse, the amplitude of these pulses according to a certain fine
Are continuously exported, transmitted, processed, or recorded to the storage medium, all of which constitute the production of digital audio
The health process.

Two important indicators of PCM signal are sampling frequency and quantization accuracy, currently, CD audio sampling frequency is usually 44100Hz, quantization accuracy is
16bit. In general, when playing music, the application reads audio data from the storage media (MP3, WMA, AAC ...). ), after decoding, the final
The PCM data is sent to the audio driver, and in turn, the audio driver continuously sends the sampled PCM data back to the application during the recording process.
The application to complete the compression, storage and other tasks. So, the two core tasks of audio drivers are:
Playback How to convert PCM data from user-space applications to analog audio that can be distinguished by the human ear
Capture converts the mic to an analog signal, which is sampled, quantified, and converted to an application where the PCM signal is sent back to the user space

(3)Asoc--alsa System on Chip:
Built on the standard ALSA drive layer to better support embedded processors and mobile devices
A set of software systems for audio codec in the The kernel has some support for audio in the SOC before the ASOC appears, but there are
Some limitations:
The codec drive is too tightly coupled to the SOC CPU, which can lead to duplication of code, for example, only the wm8731 drive
Drive code for 4 different platforms in Linux.
Audio events do not have a standard way to notify users, such as headphones, microphone insertion and detection, which are very
Normal, and usually requires machine-specific code to re-configure the audio path.
When playing or recording, the driver will put the entire codec in power state, this is not a problem for the PC, but for mobile devices, this means
Waste a lot of electricity. It also does not support the purpose of saving power by changing the sampling frequency and bias current.
ASOC is a solution to all of these problems, and is now integrated into the kernel's code tree: Sound/soc. ASOC cannot be single
Alone, he's just built on a standard ALSA drive that must be combined with a standard ALSA drive frame to work.

(4) ASOC hardware architecture:

Often, like abstraction and reuse in the software world, the audio system of embedded devices can be divided into onboard hardware (machine),
Soc (Platform), codec three parts, as shown:

①machine refers to a machine, can be a device, a development board, or a smart phone, it can be seen that machines are almost non-reusable, each machine hardware implementation may not be the same, the CPU is not the same, Codec different, audio input, output devices are not the same, machine for the CPU, CODEC, input provides a carrier. The machine driver is responsible for the coupling between platform and codec, as well as part and device or board-specific code, to handle certain control and audio events that are unique to the machines (for example, when playing audio, you need to open an amplifier first) , separate platform and codec drivers are not working, and they must be combined by the machine driver to complete the audio processing of the entire device. everything from ASOC starts with machine drivers, including the registration of sound cards, bindings platform and codec drivers.

②platform generally refers to a certain SOC platform, such as pxaxxx,s3cxxxx,omapxxx and so on, and audio-related usually contains the clock in the SOC, DMA, I2S, PCM and so on, as long as the SOC is specified, Then we can assume that it will have a corresponding platform, which is only related to the SOC, not to machine, so that we can abstract platform, so that the same SOC without any changes, it can be used in different machine. In fact, it's better to understand platform as a certain soc. platform drive is to complete the audio data management, finally through the CPU Digital audio Interface (DAI) to the audio data sent to codec for processing, and eventually by the codec output driver ear machine or horn signal. In the concrete implementation, ASOC has the platform drive divided into two parts: Snd_soc_platform_driver and snd_soc_dai_ Driver The platform_driver is responsible for managing audio data and transmitting audio data to the CPU Dai via DMA or other operations. through certain channels. < EM id= "__mcedel" >snd_soc_platform_driver to interact.

③codec literally means a codec, Codec contains I2S interface, D/A, A/D, Mixer, PA (amplifier), usuallycontains multiple inputs (Mic, line-in, I2S, PCM) and multiple outputs (headphones, horn, handset, line-out), codec andplatform is a reusable part, and the same codec can be used by different machine. Embedded codec are usually used by i²c tothe internal registers are controlled.

The main functions of the following 4 kinds:

-d/A conversion of PCM and other signals to convert digital audio signal to analog signal

-A/D conversion of analog signals from MIC, LineIn, or other input sources to convert analog sound signals to digital signals that the CPU can process

-Control The audio path, such as playing music, listening to an FM radio, or when answering a phone, the audio signal is circulating in the codec The lines are not the same

-processing of audio signals, such as volume control, power amplification, EQ control, etc.

(5) ASOC software Architecture:
At the software level, ASOC also divides the audio systems of embedded devices into 3 parts, Machine,platform and codec.
An important design principle in the codec drive ASOC is that the codec driver is platform-independent and contains some audio controls
(Controls), audio interface, definition of damp (dynamic audio power management) and certain codec IO functions. To ensure hardware-independent
, any platform-specific code is moved to the platform and machine drivers. All codec drivers are provided with the following special
Of
Codec DAI and PCM configuration information;
Codec IO control mode (I2C,SPI, etc.);
Mixer and other audio controls;
Codec ALSA audio operation interface;
If necessary, you can also provide the following features:
DAPM description information;
DAPM event handlers;
DAC Digital Mute Control
Platform driver it contains the configuration and control of the audio DMA and audio interface of the SOC platform (I2S,PCM,AC97, etc.);
Nor can it contain any code related to the board or machine.
The machine driver is responsible for handling some of the control and audio events that are specific to the machines (for example, when playing audio,
The platform and codec drivers are not working, they must be combined by the machine driver to
Can complete the audio processing of the whole equipment.

Second, the Code flow analysis:

1. Platform:
1.1 S3C24XX-I2S.C: Put S3c24xx_i2s_dai into the list dai_list,. Name = "S3c24xx-iis",
S3c24xx_iis_dev_probe
Snd_soc_register_dai (&pdev->dev, &s3c24xx_i2s_dai);
List_add (&dai->list, &dai_list);

1.2 sound/soc/samsung/dma.c: Put Samsung_asoc_platform into the list platform_list,. Name = "Samsung-audio",
Samsung_asoc_platform_probe
Snd_soc_register_platform (&pdev->dev, &samsung_asoc_platform);
List_add (&platform->list, &platform_list);

2. codec:uda134x.c
Uda134x_codec_probe
Snd_soc_register_codec (&pdev->dev,&soc_codec_dev_uda134x, &uda134x_dai, 1);
struct SND_SOC_CODEC *codec;
Codec->driver = Codec_drv; = &soc_codec_dev_uda134x

Snd_soc_register_dais (Dev, dai_drv, Num_dai); Uda134x_dai
List_add (&dai->list, &dai_list); : Put Uda134x_dai into the list dai_list
List_add (&codec->list, &codec_list);

3. Machine:
S3c24xx_uda134x_probe
S3c24xx_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_probe
Snd_soc_register_card (card); Card = &snd_soc_s3c24xx_uda134x

CARD->RTD = Devm_kzalloc (Card->dev,...

Card->rtd[i].dai_link = &card->dai_link[i]; &s3c24xx_uda134x_dai_link

List_add (&card->list, &card_list);

Snd_soc_instantiate_cards (); Instantiating a sound card
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_dai
3.1.2/* Find_codec */
Rtd->codec = codec; =//Codec, codec->driver=&soc_codec_dev_uda134x
3.1.3/* Find CODEC DAI * *
Rtd->codec_dai = Codec_dai; = &uda134x_dai
3.1.4/* Find_platform */
Rtd->platform = platform; = &samsung_asoc_platform
3.2/* Initialize the register cache for each available codec */
RET = Snd_soc_init_codec_cache (codec, compress_type);

3.3 Snd_card_create

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_new
3.5 Snd_card_register



Strace Analysis: Aplay windows.wav
1./dev/snd/controlc0 corresponds to the file_operations is Snd_ctl_f_ops
Open:snd_ctl_open
Sndrv_ctl_ioctl_pversion:snd_ctl_ioctl-Put_user (sndrv_ctl_version, IP)
Sndrv_ctl_ioctl_card_info:snd_ctl_ioctl-Snd_ctl_card_info (CARD, CTL, CMD, ARGP);
Copy_to_user

Sndrv_ctl_ioctl_pcm_prefer_subdevice:snd_ctl_ioctl, Snd_pcm_control_ioctl, control->prefer_pcm_ Subdevice = val;
Close
The above three IOCTL do not involve hardware operation

2./dev/snd/pcmc0d0p corresponds to File_operations is snd_pcm_f_ops[0]
Open:snd_pcm_playback_open
Snd_pcm_open
Snd_pcm_open_file
struct Snd_pcm_substream *substream;
Snd_pcm_open_substream
Err = Snd_pcm_hw_constraints_init (Substream);
Snd_mask_any
Snd_interval_any
.....
Err = Substream->ops->open (substream)//Substream->ops:snd_pcm_ops structural body
Soc_pcm_open
Call Cpu_dai, DMA, Codec_dai, machine's Open or startup function in turn
Uda134x_startup: Snd_pcm_hw_constraint_minmax (sndrv_pcm_hw_param_rate), Snd_pcm_hw_constraint_minmax (SNDRV_PCM_ Hw_param_sample_bits)
Dma_open: Snd_pcm_hw_constraint_integer,snd_soc_set_runtime_hwparams
Runtime->hw.info = hw->info; = Sndrv_pcm_info_interleaved |
Sndrv_pcm_info_block_transfer |
Sndrv_pcm_info_mmap |
Sndrv_pcm_info_mmap_valid |
Sndrv_pcm_info_pause |
Sndrv_pcm_info_resume,
Snd_pcm_hw_constraints_complete
Pcm_file->substream = Substream;
File->private_data = Pcm_file;

Note: Substream->ops = Soc_pcm_ops in the SOC_NEW_PCM function

The following IOCTL entries are: Snd_pcm_playback_ioctl
Sndrv_pcm_ioctl_info:snd_pcm_info_user (Substream, arg);
Substream->ops->ioctl (Substream, Sndrv_pcm_ioctl1_info, INFO);
Snd_pcm_lib_ioctl

Sndrv_pcm_ioctl_pversion:put_user (sndrv_pcm_version, (int __user *) arg)
SNDRV_PCM _ioctl_ttstamp:snd_pcm_tstamp (Substream, ARG);

Sndrv_pcm_ioctl_sync_ptr:snd_pcm_sync_ptr (Substream, Arg); first, regardless of

Sndrv_pcm_ioctl_hw_refine .... : Snd_pcm_hw_refine_user (Substream, ARG);
Memdup_user
Snd_pcm_hw_refine (Substream, params), regardless of
Copy_to_user
Sndrv_pcm_ioctl_hw_params:snd_ Pcm_hw_params_user (Substream, ARG);
Snd_pcm_hw_params
Substream->ops->hw_params (substream, params);
Soc_pcm_hw_params
Call the Machine,codec_dai,cpu_dai,platform (DMA) hw_params function
Sndrv_pcm_ioctl_sync_ptr
Sndrv_pcm_ioctl_sw_ Params:snd_pcm_sw_params_user (Substream, ARG);
Snd_pcm_sw_params does not involve hardware operations

Sndrv_pcm_ioctl_sync_ptr
Sndrv_pcm_ioctl_prepare:snd_pcm_prepare ( Substream, file);
Snd_power_wait//power management related, regardless of
.... Call to prepare

Sndrv_pcm_ioctl_sync_ptr
Sndrv_pcm_ioctl_sw_params

in platform

Cycle:
Sndrv_pcm_ioctl_writei_frames:copy_from_user
Snd_pcm_lib_write
Snd_pcm_lib_write1 (Substream, (unsigned long) buf, size, Nonblock, Snd_pcm_lib_write_transfer)
Snd_pcm_lib_write_transfer
Copy_from_user
Snd_pcm_start (Substream); Start the transfer


Sndrv_pcm_ioctl_sync_ptr

Sndrv_pcm_ioctl_drain
Sndrv_pcm_ioctl_drop
Sndrv_pcm_ioctl_hw_free
Close

Strace Analysis: Amixer cset numid=1 30 (set volume)
/dev/snd/controlc0
Open
Sndrv_ctl_ioctl_card_info
Sndrv_ctl_ioctl_pversion
Sndrv_ctl_ioctl_elem_info
Sndrv_ctl_ioctl_elem_read
Sndrv_ctl_ioctl_elem_write:snd_ctl_elem_write_user
Snd_ctl_elem_write
Find a Snd_kcontrol
Kctl = snd_ctl_find_id (card, &control->id);
Call it's put
result = Kctl->put (Kctl, control);


Report:
static struct Snd_soc_card snd_soc_s3c24xx_uda134x = {
. Name = "S3c24xx_uda134x",
. Owner = This_module,
. Dai_link = &s3c24xx_uda134x_dai_link,
. num_links = 1,
};

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",
};

Linux Audio-driven Learning: (1) ASOC analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.