Linux kernel audio driver

This article uses the I2S interface as an example to introduce knowledge related to Linux kernel audio.

1. Introduction to nouns

The following are common abbreviations used in audio debugging.

1. AEC (Acoustic Echo Cancellor): echo cancellation.

2. AGC (Automatic Gain Control): automatic gain compensation, adjust the MIC receiving volume.

3. ALSA (Advanced Linux Sound Architecture): Advanced Linux Sound Architecture.

4. ANS (Automatic Noise Suppression): Background noise suppression. ANS can detect background fixed frequency noise and eliminate background noise.

5. BCK (Bit Clock Line): bit clock, corresponding to each bit of digital audio data. The standard is called SCK (Serial Clock), serial clock. SCK=2xsampling frequencyxsampling bits

6. DAI (Digital Audio Interface): digital audio interface.

7. DAPM (Dynamic Audio Power Management): Dynamic power management, DAPM can make the audio subsystem on Linux-based mobile devices work in the minimum power consumption state at all times.

8. DRC (Dynamic Range Control): Dynamic compression, controlling audio output within a certain range.

9. EQ (Equaliser): An equalizer that adjusts the timbre by gaining or attenuating one or more frequency bands of the sound.

10. I2S (Inter-IC Sound): An interface standard for transmitting digital audio data between ICs. It uses a serial method to transmit 2 sets of data (left and right channels).

11. LRCK (Left-Right Clock): Frame clock, used to switch left and right channel data, 0: left channel; 1: right channel. The standard is called WS (World Select), channel selection; or FS (Frame Sync), frame synchronization; the frequency of LRCK = sampling frequency.

12. MCLK (Master Clock): Master clock, generally MCLK=256*LRCK. Not part of the I2S standard, it is mainly used to synchronize the internal operation of analog/digital converters.

13. Mono: mono.

14. OSS (Open Sound System): Open Sound System.

15. PCM (Pulse Code Modulation): Pulse code modulation, I2S is a subset of PCM.

16. Ramp: Gradually increase or decrease the volume level to avoid rapid changes in sound. Used to pause or resume music.

17. Stereo: two-channel

18. TDM (Time Division Multiplexing): Time Division Multiplexing. I2S can only transmit up to 2 channels of data, and TDM supports up to 16 channels.

2. I2S interface

I2S is a digital audio transmission standard defined by Philips and is used for the transmission of digital audio data between internal devices in the system.

I2S is a branch of PCM with the same interface definition. The sampling rate of I2S is generally 44.1/48KHZ, and the sampling frequency of PCM is generally 8/16KHZ, etc.

The I2S interface has 4 sets of signals: SCK (bit clock), LRCK (frame clock), SDI/SDO (data).

On the I2S bus, only one master device and a sending device can exist at the same time. The master device can be a sending device or a receiving device. The common I2S block diagram is as follows:

3. I2S protocol

Common protocol modes of the I2S interface include: I2S normal mode, I2S left-aligned mode, I2S right-aligned mode, etc.

1. I2S normal mode

I2S normal mode is a special case of I2S left alignment, also called Philips mode. The picture below is the waveform of I2S normal mode.

The LRCK (i2s_LRCK_rx/i2s_LRCK_tx) signal becomes low to indicate the left channel and becomes high to indicate the right channel.

The SD (i2s_sdo, i2s_sdi) signal transmits the MSB or LSB first, and sends the first bit in one SCLK clock cycle after LRCK changes.

SD signal width ranges from 16 to 32 bits.

2. I2S left-aligned mode

The figure below is the waveform of I2S left-aligned mode.

The LRCK (i2s_LRCK_rx/i2s_LRCK_tx) signal becomes high to indicate the left channel and becomes low to indicate the right channel.

The SD (i2s_sdo, i2s_sdi) signal transmits the MSB or LSB first, and sends the first bit at the same time as LRCK changes.

SD signal width ranges from 16 to 32 bits.

3. I2S right-aligned mode

The figure below is the waveform of I2S right-aligned mode.

The LRCK (i2s_LRCK_rx/i2s_LRCK_tx) signal goes high to indicate the left channel and goes low to indicate the right channel.

The SD (i2s_sdo, i2s_sdi) signal transmits MSB or LSB first, unlike I2S normal or left-justified mode, its data is aligned with the last bit at the edge of the LRCK signal.

SD signal width ranges from 16 to 32 bits.

Other protocol formats include: PCM early mode, PCM late1 mode and PCM late2 mode, etc.

4. ALSA framework

After Linux kernel 2.6, ALSA replaced OSS and became part of the Linux kernel audio subsystem.

The ALSA system includes:

1. alsa-driver: alsa system driver.

2. alsa-lib: alsa library, user space call, and kernel space interaction.

3. alsa-utils: command line tool.

4, alsa-plugin: alsa review.

5. alsa-tools: alsa tools.

The ALSA framework consists of the following:

The relevant functions in alsa-driver are as follows (taking the rockchip 4a board as an example):

1. Codec: Common parts of audio chips, including codec initialization function, control interface, register cache, controls, dapm components, audio routing, bias voltage setting function and other description information.

Rockchip 4A single board, Codec uses ES8316 chip, the dts configuration of this chip is as follows:

2. Codec DAI: Audio interface driver description on codec, including: clock configuration, format configuration, capability description, etc.

Codec DAI related implementations are as follows:

3. CPU DAI: refers to the I2S and PCM bus controller of the SoC, which is responsible for transferring audio data from the I2S tx FIFO to the codec.

The dts related configuration of RK3399 CPU DAI is as follows:

4. DAI Link: audio data link, which specifies the codec, codec_dai, cpu_dai and platform used in the link.

The Linux 4.4 kernel supports two ways to create a sound card: one is the general simple-card framework; the other is the traditional creation of a custom machine driver.

Simple card is a simple and versatile machine driver. If the simple-card framework is enough to meet your needs, you can choose the simple card framework first.

The dts related configuration of DAI Link is as follows:

The relevant implementation of DAI Link is as follows:

5. DAPM: Dynamic power management is based on the corresponding improved framework of kcontrol and adds corresponding power management mechanisms. Widget is the basic unit of DAPM.

The relevant implementations of kcontrol, dapm widget and dapm routes in Codec (es8316) are as follows:

6. DMA: Responsible for transferring the audio data in the dma buffer to the I2S tx FIFO.

5. Debugging commands

ALSA is the basic interface for Linux to process audio, but ALSA only provides basic interfaces, and the operation is relatively complicated. Under normal circumstances, you can directly use the utils tool set provided with it. The utils tool set is some encapsulated functional modules. You can directly use the command Provided in a way, users only need to type in relevant commands and parameters to realize the audio operation function.

1. Check the sound card information

root@xiaotianbsp:~# cat /proc/asound/cards
 0 [rockchipes8316c]: rockchip_es8316 - rockchip,es8316-codec
                      rockchip,es8316-codec
 1 [HDMICODEC      ]: HDMI-CODEC - HDMI-CODEC
                      HDMI-CODEC
root@xiaotianbsp:~# ls -l /dev/snd/
total 0
drwxr-xr-x  2 root root       80 Aug 16 14:43 by-path
crw-rw----+ 1 root audio 116,  2 Aug 16 14:43 controlC0
crw-rw----+ 1 root audio 116,  5 Aug 16 14:43 controlC1
crw-rw----+ 1 root audio 116,  4 Aug 16 14:43 pcmC0D0c
crw-rw----+ 1 root audio 116,  3 Aug 16 14:43 pcmC0D0p
crw-rw----+ 1 root audio 116,  6 Aug 16 14:43 pcmC1D0p
crw-rw----+ 1 root audio 116,  1 Aug 16 14:43 seq
crw-rw----+ 1 root audio 116, 33 Aug 16 14:43 timer

controlCx: control interface, providing a flexible way to manage registered sound cards and query existing sound cards.

pcmCxDxc: PCM interface, corresponding to recording equipment.

pcmCxDxp: PCM interface, corresponding to the playback device.

timer: Supports sound synchronization events to provide timers on the sound card.

seq: sequencer interface, a high-level interface for MIDI programming and sound synchronization that is more advanced than the original MIDI interface.

2. Check the sound card collection and playback PCM information

root@xiaotianbsp:~# cat /proc/asound/pcm
00-00: ff880000.i2s-ES8316 HiFi ES8316 HiFi-0 :  : playback 1 : capture 1
01-00: ff8a0000.i2s-i2s-hifi i2s-hifi-0 :  : playback 1

3. Check the ALSA driver version

root@xiaotianbsp:~# cat /proc/asound/version
Advanced Linux Sound Architecture Driver Version k4.4.154-90-rockchip-ga14f6502e045.

4. Check the information of sound card 0

root@xiaotianbsp:~# cat /proc/asound/card0/pcm0p/sub0/status
closed

5. View registers

## regmap名称
root@xiaotianbsp:~# cat /sys/kernel/debug/regmap/1-0011/name
es8316
root@xiaotianbsp:~# cat /sys/kernel/debug/regmap/ff880000.i2s/name
rockchip-i2s

## rk3399 i2s0控制器寄存器
root@xiaotianbsp:~# cat /sys/kernel/debug/regmap/ff880000.i2s/registers
00: 0000000f
04: 0000000f
08: 00033f3f
0c: 00000000
10: 000f0010
14: 01f00000
18: XXXXXXXX
1c: 00000000
20: XXXXXXXX
28: 00000000

## es8316寄存器
root@xiaotianbsp:~# cat /sys/kernel/debug/regmap/1-0011/registers
00: c0
01: f3
02: 08
03: 20
04: 11
05: 00
06: 11
07: 00
08: 00
09: 04
0a: 0c
0b: 0c
0c: ff
0d: 3f
0e: ff
0f: ff
10: 11
11: fc
12: 28
13: 00
14: 00
15: 33
16: 00
17: 00
18: 00

6. Check the clock

## 采样率44.1KHZ,mclk = 256 * sample = 256*44.1*1000 = 11289600 HZ
root@xiaotianbsp:~# cat /sys/kernel/debug/clk/clk_summary | grep i2s0
          clk_i2s0_div                    1            1   800000000          0 0
             clk_i2s0_frac                1            1    11289600          0 0
                clk_i2s0_mux              1            1    11289600          0 0
                   clk_i2s0               1            2    11289600          0 0
                hclk_i2s0                 1            2   100000000          0 0

7. amixer command

root@xiaotianbsp:~# amixer -h
Usage: amixer <options> [command]

Available options:
  -h,--help       this help
  -c,--card N     select the card
  -D,--device N   select the device, default 'default'
  -d,--debug      debug mode
  -n,--nocheck    do not perform range checking
  -v,--version    print version of this program
  -q,--quiet      be quiet
  -i,--inactive   show also inactive controls
  -a,--abstract L select abstraction level (none or basic)
  -s,--stdin      Read and execute commands from stdin sequentially
  -R,--raw-volume Use the raw value (default)
  -M,--mapped-volume Use the mapped volume

Available commands:
  scontrols       show all mixer simple controls
  scontents       show contents of all mixer simple controls (default command)
  sset sID P      set contents for one mixer simple control
  sget sID        get contents for one mixer simple control
  controls        show all controls for given card
  contents        show contents of all controls for given card
  cset cID P      set control contents for one control
  cget cID        get control contents for one control

7.1 View card0 content

root@xiaotianbsp:~# amixer -c 0 contents
numid=16,iface=MIXER,name='ADC Double FS Mode'
  ; type=BOOLEAN,access=rw------,values=1
  : values=off
numid=14,iface=MIXER,name='ADC Soft Ramp'
  ; type=BOOLEAN,access=rw------,values=1
  : values=on
numid=13,iface=MIXER,name='ADC Capture Volume'
  ; type=INTEGER,access=rw---R--,values=1,min=0,max=192,step=0
  : values=192
  | dBscale-min=-96.00dB,step=0.50dB,mute=1
numid=15,iface=MIXER,name='Capture Polarity'
  ; type=ENUMERATED,access=rw------,values=1,items=2
  ; Item #0 'Normal'
  ; Item #1 'Invert'
  : values=0

7.2 View and configure a certain numid command

## 查看
root@xiaotianbsp:~# amixer -c 0 cget numid=16
numid=16,iface=MIXER,name='ADC Double FS Mode'
  ; type=BOOLEAN,access=rw------,values=1
  : values=off
  
## 配置
root@xiaotianbsp:~# amixer -c 0 cset numid=16 on
numid=16,iface=MIXER,name='ADC Double FS Mode'
  ; type=BOOLEAN,access=rw------,values=1
  : values=on

8. Device name

root@xiaotianbsp:~# aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: rockchipes8316c [rockchip,es8316-codec], device 0: ff880000.i2s-ES8316 HiFi ES8316 HiFi-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: HDMICODEC [HDMI-CODEC], device 0: ff8a0000.i2s-i2s-hifi i2s-hifi-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0
root@linaro-alip:~# arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: rockchipes8316c [rockchip,es8316-codec], device 0: ff880000.i2s-ES8316 HiFi ES8316 HiFi-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0

9, sound emission

aplay -D hw:0,0 -r 44100 -c 2 -f s16_le play.wav

10. Recording

arecord -D hw:0,0 -r 16000 -c 1 -f s16_le record.wav

-D: Playback device, the device name listed by aplay -l or arecord -l

-r: sampling rate

-c: audio file channel

-f: Sampling format, commonly used ones are S16_LE, S24_LE, S32_LE, cd (can be ignored)

You can also refer to the usage of tinyalsa commands, such as tinymix, tinypcminfo, tinyplay and tinycap.

11. Convert mp3 to wav

lame test.mp3 test.wav --decode
Tang Xiaoou, founder of SenseTime, passed away at the age of 55 In 2023, PHP stagnated Wi-Fi 7 will be fully available in early 2024 Debut, 5 times faster than Wi-Fi 6 Hongmeng system is about to become independent, and many universities have set up “Hongmeng classes” Zhihui Jun’s startup company refinances , the amount exceeds 600 million yuan, and the pre-money valuation is 3.5 billion yuan Quark Browser PC version starts internal testing AI code assistant is popular, and programming language rankings are all There's nothing you can do Mate 60 Pro's 5G modem and radio frequency technology are far ahead MariaDB splits SkySQL and is established as an independent company Xiaomi responds to Yu Chengdong’s “keel pivot” plagiarism statement from Huawei
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4702401/blog/10322015