- **[Audio Driver Development Procedure](#section3200)**
- [Development on the Existing Platform](#section3221)
- [Development on an Adapted Platform](#section3221)
- [Development on a New Platform](#section3222)
-**[Audio Driver Development Examples](#section4000)**
-[Codec Driver Development Example](#section4100)
-[Filling in Codec Data Structures](#section4111)
-[Initializing codecDevice and codecDai Devices](#section4112)
-[Initializing codecDevice and codecDaiDevice](#section4112)
-[Implementing the Codec Operation Function Set](#section4113)
-[Registering and Binding Codec to HDF](#section4114)
-[Configuring HCS](#section4115)
-[Accessory Driver Development Example](#section4200)
-[Filling in Accessory Data Structures](#section4221)
-[Initializing accessoryDevice and accessoryDai Devices](#section4222)
-[Initializing accessoryDevice and accessoryDaiDevice](#section4222)
-[Implementing the Accessory Operation Function Set](#section4223)
-[Registering and Binding Accessory to HDF](#section4224)
-[Configuring HCS](#section4225)
-[Platform Driver Development Example](#section4300)
-[Filling in Platform Data Structures](#section4331)
-[Initializing the dmaDevice Device](#section4332)
-[Initializing dmaDevice](#section4332)
-[Implementing the DMA Operation Function Set](#section4333)
-[Registering and Binding Platform to HDF](#sectionsection4334)
-[Configuring HCS](#section4335)
-[DAI Driver Development Example](#section4400)
-[Filling in DAI Data Structures](#section4441)
-[Initializing the daiDevice Device](#section4442)
-[Initializing daiDevice](#section4442)
-[Implementing the DAI Operation Function Set](#section4443)
-[Registering and Binding DAI to HDF](#section4444)
-[Configuring HCS](#section4445)
...
...
@@ -51,9 +51,9 @@
# Audio Driver Overview<a name="section1000"></a>
A multimedia system is an indispensable part in Internet of Things (IoT) devices. Audio is an important module of the multimedia system, and building an audio driver model is particularly important in development.
A multimedia system is an indispensable part in Internet of Things (IoT) devices. Audio is an important module of the multimedia system, and building an audio driver model is particularly important in device development.
This document describes the audio driver architecture and functional components and how to develop the audio driver based on the Hardware Driver Foundation (HDF). Chip vendors can develop their own drivers and Hardware abstraction layer (HAL) API invocation based on the driver architecture.
This document describes the audio driver architecture and functional modules and how to develop audio drivers based on the Hardware Driver Foundation (HDF). Chip vendors can develop their own drivers and invocation of Hardware Abstraction Layer (HAL) APIs based on the driver architecture.
...
...
@@ -63,36 +63,36 @@ The audio driver architecture is implemented based on the [HDF](https://device.h

The driver architecture consists of the following:
- Hardware Device Interface (HDI) adapter: implements the audio HAL driver (HDI adaptation) and provides hardware driver capability interfaces for the audio service (frameworks). The HDI adapter provides interface objects such as Audio Manager, Audio Adapter, Audio Control, Audio Capture and Audio Render.
- Audio interface lib: works with the audio driver model in the kernel to control audio hardware, read recording data, and write playback data. It contains **Stream\_ctrl\_common**, which is used to interact with the audio HDI adapter layer.
- Audio Driver Model (ADM): serves the multimedia audio subsystem and enables system developers to develop applications based on scenarios. With ADM, codec and DSP device vendors can adapt their driver code based on the unified interfaces provided by the ADM and implement quick development and easy adaptation to the OpenHarmony system.
- Audio Control Dispatch: receives control instructions from the library layer and distributes the control instructions to the driver layer.
- Audio Stream Dispatch: receives data from the library layer and distributes the data to the driver layer.
- Hardware Device Interface (HDI) adapter: implements the audio HAL driver (HDI adaptation) and provides hardware driver capability interfaces for the audio service (frameworks). The HDI adapter provides interface objects such as Audio Manager, Audio Adapter, Audio Control, Audio Capture, and Audio Render.
- Audio Interface Lib: works with the Audio Driver Model (ADM) in the kernel to control audio hardware, read recording data, and write playback data. The **Stream_ctrl_common** in the Audio Interface Lib interacts with the audio HDI adapter layer.
- ADM: helps system developers to develop scenario-specific applications for the multimedia audio subsystem. With the ADM, codec and DSP device vendors can adapt their driver code based on the unified interfaces provided by the ADM and implement quick development and easy adaptation to the OpenHarmony system.
- Audio Control Dispatch: dispatches the control instructions from the Audio Interface Lib to the driver layer.
- Audio Stream Dispatch: dispatches the data from the Audio Interface Lib to the driver layer.
- Card Manager: performs management of multiple audio cards. Each audio adapter consists of the digital audio interface (DAI), Platform, Codec, Accessory, DSP and Smart Audio Power Manager (SAPM) modules.
- Platform Driver: servers as the driver adaptation layer.
- Smart Audio Power Manager (SAPM): optimizes the power consumption policy of the ADM.
- Card Manager: manages multiple audio adapters. Each audio adapter consists of the digital audio interface (DAI), platform, codec, accessory, DSP, and Smart Audio Power Manager (SAPM) modules.
- Platform Drivers: implement driver adaptation.
- SAPM: optimizes the power consumption policy of the ADM.
The audio driver provides the **hdf\_audio\_render**, **hdf\_audio\_capture** and **hdf\_audio\_control** services for the HDI layer. The driver service nodes in the **dev** directory of the development board are as follows:
The audio driver provides the **hdf_audio_render**, **hdf_audio_capture**, and **hdf_audio_control** services for the HDI layer. The driver service nodes in the **dev** directory of the development board are as follows:
```c
# ls -l hdf_audio*
crw-rw----1systemsystem248,61970-01-0100:00hdf_audio_capture// Audio data recording streaming service.
crw-rw----1systemsystem248,41970-01-0100:00hdf_audio_codec_dev0// Name of audio adapter device 0.
crw-rw----1systemsystem248,41970-01-0100:00hdf_audio_codec_dev1// Name of audio adapter device 1.
crw-rw----1systemsystem248,51970-01-0100:00hdf_audio_control// Audio control streaming service.
crw-rw----1systemsystem248,71970-01-0100:00hdf_audio_render// Audio data playback streaming service.
The audio adapter devices have the following driver services:
The audio adapters have the following driver services:
hdf\_audio\_codec\_dev0
-**dma\_service\_0**: DMA service
-**dma\_service\_0**: direct memory access (DMA) service
-**dai_service**: CPU DAI service
-**codec\_service\_0**: codec service (built-in codec)
-**dsp\_service\_0**: DSP service (optional)
...
...
@@ -107,48 +107,48 @@ hdf\_audio\_codec\_dev1

1. When the system starts, the Platform, Codec, Accessory, DSP and DAI drivers of the audio module are loaded first. Each driver obtains the configuration information from its configuration file and saves the obtained information to the data structures.
1. When the system starts, the platform, codec, accessory, DSP, and DAI drivers of the audio module are loaded first. Each driver obtains the configuration information from its configuration file and saves the obtained information to the data structures.
2. Each driver module calls the ADM registration interface to add itself to the linked list of the driver module.
3. The ADM reads the hdf\_audio\_driver\_0 and hdf\_audio\_driver\_1 configuration and loads the devices of each module.
3. The ADM obtains the hdf_audio_driver_0 and hdf_audio_driver_1 configuration and loads the devices of each module.
4. The ADM module initializes each module device by calling the initialization API of each module.
4. The ADM module initializes each module device by calling the initialization API of the respective module.
5.Add the initialized audio devices to the cardManager linked list.
5.The initialized audio devices are added to the cardManager linked list.
### Playback Process<a name="section3112"></a>

1. The Interface Lib dispatches the **Render Open** instruction through the service launched by the driver for handling the playback streaming (referred to as driver service hereinafter). Upon receiving the instruction, the Stream Dispatch service calls the API of each module to deliver the instruction.
1. The Audio Interface Lib sends the **Render Open** instruction to the Audio Stream Dispatch service. The Audio Stream Dispatch service calls the API of each module to deliver the instruction.
2. The Interface Lib dispatches a path select instruction through the control service. Upon receiving the instruction, the Control Dispatch service calls the DAI API to set the path.
2. The Audio Interface Lib sends a path select instruction to the Control Dispatch service. The Control Dispatch service calls the DAI API to set the path.
3. The Interface Lib dispatches hardware parameters through the driver service. Upon receiving the parameters, the Stream Dispatch service calls the API of each module to set hardware parameters.
3. The Audio Interface Lib sends hardware parameters to the Audio Stream Dispatch service. The Audio Stream Dispatch service calls the API of each module to set the hardware parameters.
4. The Interface Lib dispatches the start playing instruction through the driver service. Upon receiving the instruction, the Stream Dispatch service calls the API of each module to perform related settings for each module.
4. The Audio Interface Lib sends the start playing instruction to the Audio Stream Dispatch service. The Audio Stream Dispatch service calls the API of each module to perform related settings for each module.
5. The Interface Lib dispatches audio data through the driver service. Upon receiving the data, the Stream Dispatch service calls the **Platform AudioPcmWrite** API to send the audio data to direct memory access (DMA).
5. The Audio Interface Lib sends audio data to the Audio Stream Dispatch service. The Audio Stream Dispatch service calls the **Platform AudioPcmWrite** API to send the audio data to DMA.
6. The Interface Lib dispatches the stop playing instruction through the driver service. Upon receiving the instruction, the Stream Dispatch service calls the stop API of each module to perform related settings for each module.
6. The Audio Interface Lib sends the stop playing instruction to the Audio Stream Dispatch service. The Audio Stream Dispatch service calls the stop API of each module to perform related stop settings for each module.
7. The Interface Lib dispatches the **Render Close** instruction through the driver service. Upon receiving the instruction, the Stream Dispatch service calls the **Platform AudioRenderClose** API to release resources.
7. The Audio Interface Lib sends the **Render Close** instruction to the Audio Stream Dispatch service. The Audio Stream Dispatch service calls the **Platform AudioRenderClose** API to release resources.
### Control Process<a name="section3113"></a>

1. When the volume needs to be adjusted, the Interface Lib dispatches the instruction for obtaining the volume range through the control service. Upon receiving the instruction, the Control Dispatch service parses the instruction and calls **get()** of the Codec module to obtain the volume range.
2. The Interface Lib dispatches the instruction for setting the volume through the control service. Upon receiving the instruction, the Control Dispatch service parses the instruction and calls **Set()** of the Codec module to set the volume.
1. When the volume needs to be adjusted, the Audio Interface Lib sends an instruction for obtaining the volume range to the Control Dispatch service. The Control Dispatch service parses the instruction and calls **get()** of the codec module to obtain the volume range.
2. The Audio Interface Lib sends an instruction for setting the volume to the Control Dispatch service. The Control Dispatch service parses the instruction and calls **Set()** of the codec module to set the volume.
## Audio Driver Development Procedure<a name="section3200"></a>
### Development on the Existing Platform<a name="section3221"></a>
### Development on an Adapted Platform<a name="section3221"></a>
The following figure shows the driver development process for adapting the ADM to the codec or accessory (SmartPA) of the existing platform (Hi3516D V300).
The following figure shows the process for developing the codec or accessory (SmartPA) driver on a chip platform (Hi3516D V300) to which the ADM has adapted.

- Add register information to the private HDF configuration source (HCS) of codec or smartPA based on the chip description.
- Add register information to the private HDF configuration source (HCS) of the codec or SmartPA based on the chip description.
- If the workflow of the newly added codec or SmartPA is the same as that of the existing codec or SmartPA, you do not need to implement the operation function set or configure the compilation file for the newly added codec or SmartPA.
...
...
@@ -157,13 +157,13 @@ The following figure shows the driver development process for adapting the ADM t
### Development on a New Platform<a name="section3222"></a>
The following figure shows the driver development process of the ADM on a new platform.
The following figure shows the driver development process if the ADM has not adapted to the platform.

The audio-related drivers codec (optional), DAI, DMA, DSP (optional), and SmartPA (optional) need to be adapted to the new platform.
The codec (optional), DAI, DMA, DSP (optional), and SmartPA (optional) modules of the audio adapter need to be adapted to the new platform.
- Add register information of each module driver to the private configuration file of each module according to the chip description.
- Add register information of each module driver to the private configuration file of the respective module according to the chip description.
- Implement the operation function set of each module.
...
...
@@ -177,7 +177,7 @@ The audio-related drivers codec (optional), DAI, DMA, DSP (optional), and SmartP
Code path: **drivers/peripheral/audio**
The following uses Hi3516D V300 as an example to describe how to develop the audio codec driver, accessory driver, DAI driver, and platform driver.
The following uses Hi3516D V300 as an example to describe how to develop the audio Codec driver, Accessory driver, DAI driver, and Platform driver.
## Codec Driver Development Example<a name="section4100"></a>
.DaiInit=CodecDaiDeviceInit,// Initialize the codecDai device (need to be implemented for a new platform).
.ops=&g_codecDaiDeviceOps,// codecDai operation function set.
};
```
### Initializing CodecDevice and CodecDai Devices<a name="section4112"></a>
### Initializing codecDevice and codecDaiDevice<a name="section4112"></a>
**CODECDeviceInit** sets audio input/audio output (AIAO), initializes registers, inserts **g_audioControls** into the controller linked list, initializes the power management, and selects a path.
The codec module is encapsulated with the **read()** and **write()** functions of the read and write registers at the operating system abstraction layer (OSAL).
If the new platform cannot use the OSAL read and write functions to operate registers, the developer should implement the **read()** and **write()**.
If the new platform cannot use the OSAL **read()** and **write()** functions to operate registers, you should implement them.
This process depends on the driver implementation mode of the HDF. For details, see [HDF](https://gitee.com/openharmony/docs/blob/master/en/device-dev/driver/driver-hdf.md).
Fill in the **g_codecDriverEntry** structure. Ensure that the value of **moduleName** is the same as that in **device_info.hcs**. Implement the pointers to the **Bind**, **Init**, and **Release** functions.
Fill in the **g_codecDriverEntry** structure. Ensure that the value of **moduleName** is the same as that in **device_info.hcs**. Implement the pointers to the **Bind**, **Init**, and **Release** functions.
**Configuring Codec Device Information in device_info.hcs**
...
...
@@ -397,8 +397,8 @@ The code snippet is as follows:
audio::host{
device_codec::device{
device0::deviceNode{
policy=1;// The codec module provides services only for the kernel.
priority=50;// The codec module must be loaded before the load of the HDF_AUDIO module.
policy=1;// The codec module provides services only for the kernel.
priority=50;// The codec module must be loaded before the load of the HDF_AUDIO module.
preload=0;
permission=0666;
moduleName="CODEC_HI3516";// The value must be the same as moduleName in HdfDriverEntry.
...
...
@@ -410,7 +410,7 @@ The code snippet is as follows:
**Configuring Dependencies in audio_config.hcs**
Configure dependencies between the codec, platform, DAI, DSP, and accessory for the audio_card device.
Configure dependencies between the codec, platform, DAI, DSP, and accessory for the audio adapters.
The code snippet is as follows:
...
...
@@ -427,7 +427,7 @@ root {
cpuDaiName="dai_service";// CPU DAI service.
accessoryDaiName="accessory_dai";// External DAI.
dspName="dsp_service_0";// DSP service name.
dspDaiName="dsp_dai";// DSP DAI
dspDaiName="dsp_dai";// DSP DAI.
}
}
}
...
...
@@ -437,11 +437,11 @@ root {
The configuration matches **deviceMatchAttr** of the codec configured in **device_info.hcs**. It includes the register configuration.
Binding the control function configuration is to configure the control functions and their register parameters in the .hcs file according to unified structure specifications. The configuration can be obtained and parsed, and added to the controller linked list.
Binding the control functionality configuration is to configure the control functionalities and their register parameters in the .hcs file according to unified structure specifications. The configuration can be obtained and parsed, and added to the controller linked list.
-**regConfig**: register and control function configuration
-**regConfig**: register and control functionality configuration
-**ctrlParamsSeqConfig**: control function register configuration
-**ctrlParamsSeqConfig**: control functionality register configuration
-**daiStartupSeqConfig**: DAI startup configuration
...
...
@@ -451,7 +451,7 @@ Binding the control function configuration is to configure the control functions
-**initSeqConfig**: initialization process register configuration
-**controlsConfig**: control function configuration. The **array index** (specific service scenario) and **iface** (same as the HAL) are of fixed values.
-**controlsConfig**: control functionality configuration. The **array index** (specific service scenario) and **iface** (same as the HAL) are of fixed values.
```
array index
...
...
@@ -492,12 +492,12 @@ iface
/* Base address of the Hi3516 register*/
idInfo{
chipName="hi3516";// Codec name
chipIdRegister=0x113c0000;// Codec base address
chipName="hi3516";// Codec name
chipIdRegister=0x113c0000;// Codec base address
chipIdSize=0x1000;// Codec address offset
}
/* Register configuration, including configuration of registers*/
/* Register configuration, including configuration of registers*/
regConfig{
/* reg: register address
rreg: register address
...
...
@@ -525,7 +525,7 @@ iface
0x14,0x04000002
];
/* control function config
/* Control functionality configuration
array index, iface, enable*/
controlsConfig=[
0,0,0,
...
...
@@ -537,16 +537,16 @@ iface
8,6,0,
9,6,0,
];
/* control function register config
/* Control functionality register configuration
reg, rreg, shift, rshift, min, max, mask, invert, value */
/* After the upper layer delivers parameters, write audio-related data to registers.
...
...
@@ -670,7 +670,7 @@ SmartPA is a type of accessory driver. The SmartPA development procedure is simi
Fill in the following data structures for the accessory module:
-**g_tfa9879Data**: operation function set of the accessory device. It contains the configuration in the .hcs file, and defines and maps the methods for initializing the accessory device and reading and writing registers.
-**g_tfa9879Data**: operation function set of the accessory device. It contains the configuration in the .hcs file, and defines and maps the functions for initializing the accessory device and reading and writing registers.
-**g_tfa9879DaiDeviceOps**: data set of the DAI of the accessory device. It defines and maps the driver name, initialization, and operation set of the data access interface of the accessory device.
### Initializing accessoryDevice and accessoryDai Devices<a name="section4222"></a>
### Initializing accessoryDevice and accessoryDaiDevice<a name="section4222"></a>
As the entry function for device initialization, **Tfa9879DeviceInit** sets the address of the SmartPA I2C device, obtains configuration data, initializes (including resets) the device registers, and adds the control function to the controller linked list. The current demo also includes the initialization of the registers related to the Hi3516D V300 device, such as initialization of GPIO pins.
As the entry function for device initialization, **Tfa9879DeviceInit** sets the address of the SmartPA I2C device, obtains configuration data, initializes (including resets) the device registers, and adds the control functionality to the controller linked list. The current demo also includes the initialization of the registers related to the Hi3516D V300 device, such as initialization of GPIO pins.
This process depends on the driver implementation mode of the HDF. For details, see [HDF](https://gitee.com/openharmony/docs/blob/master/en/device-dev/driver/driver-hdf.md).
Fill in the **g_tfa9879DriverEntry** structure. Ensure that the value of **moduleName** is the same as that in **device_info.hcs**. Implement the pointers to the **Bind**, **Init**, and **Release** functions.
Fill in the **g_tfa9879DriverEntry** structure. Ensure that the value of **moduleName** is the same as that in **device_info.hcs**. Implement the pointers to the **Bind**, **Init**, and **Release** functions.
In audio driver development, platform is configured to adapt to the DMA driver. The major steps for developing the platform driver are as follows:
In audio driver development, the Platform module is configured to adapt to the DMA driver. The major steps for developing the platform driver are as follows:
1. Define and fill in a platform instance.
2. Implement callbacks for the platform instance.
3. Register and bind the platform instance to the HDF.
...
...
@@ -890,7 +890,7 @@ Fill in the following structures for the platform module:
```c
structAudioDmaOpsg_dmaDeviceOps={
.DmaBufAlloc=Hi3516DmaBufAlloc,// Apply for memory for the DMA device.
.DmaBufFree=Hi3516DmaBufFree,// Releases the memory of the DMA device.
.DmaBufFree=Hi3516DmaBufFree,// Release the memory of the DMA device.
.DmaRequestChannel=Hi3516DmaRequestChannel,// Request a DMA channel.
.DmaConfigChannel=Hi3516DmaConfigChannel,// Configure the DMA channel.
This process depends on the driver implementation mode of the HDF. For details, see [HDF](https://gitee.com/openharmony/docs/blob/master/en/device-dev/driver/driver-hdf.md).
- Fill in the **g_platformDriverEntry** structure.
- Fill in the **g_platformDriverEntry** structure.
- Ensure that the value of **moduleName** is the same as that in **device_info.hcs**.
- Implement the pointers to the **Bind**, **Init**, and **Release** functions.
1. Call **GetAudioManagerFuncs()** to obtain functions.
2. Call **GetAllAdapters()** to obtain information about the supported audio adapters and call **LoadAdapter()** load the corresponding audio adapter.
2. Call **GetAllAdapters()** to obtain information about the supported audio adapters and call **LoadAdapter()**to load the corresponding audio adapter.
3. Create an audio player class by calling **CreateRender()** or create a recorder class and deliver audio attributes.
4. Call the methods mounted to the created audio player class to call **render->control.Start()** and **render->RenderFrame()** to dispatch the start instruction and deliver audio data cyclically.
5. During the playback, call **render->control.Pause()**, **render->control.Resume()** or **render->volume.SetVolume()** to control the audio player service, for example, pausing the playback, resume the playback, and adjusting the volume.
5. During the playback, call **render->control.Pause()**, **render->control.Resume()**, or **render->volume.SetVolume()** to control the audio player service, for example, pausing the playback, resuming the playback, and adjusting the volume.
6. After the audio player service is complete, stop the playback, destroy the audio player class, and unload the audio adapter.