Timeline
Timeline
2026-01-14
init
This article introduces the basics of Linux ADC drivers, discusses the difference between analog and digital signals as well as the fundamental concepts of Analog-to-Digital Converters (ADC), and summarizes the working principles of ADC in the processes of sampling, holding, quantization, and encoding, along with its application scenarios and key parameters such as resolution.
Linux Driver Notes
| Table of Contents | Links |
|---|---|
| 1. Linux Driver Framework | |
| 2. Linux Driver Loading Logic | |
| 3. Character Device Basics | |
| 4. Concurrency and Competition | |
| 5. Advanced Character Device Progression | |
| 6. Interrupts | |
| 7. Platform Bus | |
| 8. Device Tree | |
| 9. Device Model | |
| 10. Hotplug | |
| 11. pinctrl Subsystem | |
| 12. gpio Subsystem | |
| 13. Input Subsystem | |
| 14. Single Bus | |
| 15. I2C | |
| 16. SPI | |
| 17. UART | |
| 18. PWM | |
| 19. RTC | |
| 20. Watchdog | |
| 21. CAN | |
| 22. Network Device | |
| 23. ADC | |
| 24. IIO | |
| 25. USB | |
| 26. LCD |
ADC Basics
Analog Signal and Digital Signal
Analog SignalIt is a continuously varying signal whose value can take any value within a certain range. It is continuous in both time and amplitude, and can accurately reflect changes in physical quantities in nature. For example, the voltage signal output by a temperature sensor, the sound signal picked up by a microphone, and the light intensity signal detected by a photoresistor all fall into the category of analog signals.
Digital SignalIt is a discrete signal whose value can only take specific values within a limited range, usually expressed in binary form. Its characteristics are that it is discrete in time (with fixed time intervals between sampling points) and discrete in amplitude (quantized values). For example, data stored and processed in computers, audio data in MP3 files, and pixel values in JPEG image files are all concrete manifestations of digital signals.
| Dimension | Characteristics of Analog Signal | Characteristics of Digital Signals |
|---|---|---|
| Essence | Continuous variation, arbitrary values | Discrete values, often represented in binary |
| Characteristics | Both time and amplitude are continuous | Both time (sampling interval) and amplitude (quantized value) are discrete |
| Advantages | Accurately reflects changes in natural physical quantities | Easy to store/transmit/process, strong anti-interference capability |
| Typical Scenarios | Raw sensor signals, natural acoustic/optical/electrical signals | Computer data, digital audio/video files |
ADC Concept
ADC isAnalog-to-Digital Converterabbreviation, can be translated asAnalog-to-Digital Converter. It is an electronic device or circuit module used toconvert continuously varying analog signals (such as voltage, current, etc.) into discrete digital signals. These digital signals can be processed and stored by computers, microcontrollers, or other digital systems.
The conversion of analog signals to digital signals by an ADC is typically divided intosampling、holding、quantizationandencodingfour steps. The definition and function of each step are shown in the following table:
| Step | Definition | Function | Key Parameters or Features |
|---|---|---|---|
| Sampling | The process of measuring an analog signal at fixed time intervals. | Discretizes a time-continuous analog signal, laying the foundation for subsequent digital processing. | Sampling Rate, i.e., the number of samples per second. According to the Nyquist sampling theorem, the sampling rate must be at least twice the highest frequency of the signal. |
| Holding | After sampling, the instantaneous value of the signal is held for a period of time for subsequent processing. | Ensures the signal does not distort due to rapid changes during quantization, improving conversion accuracy and stability. | No specific parameters; the main purpose is to stabilize the signal. |
| Quantization | The process of mapping the continuous amplitude values obtained from sampling to a set of discrete values. | Discretizes a signal that is continuous in amplitude, enabling it to be represented by a digital system. | Resolution, typically expressed in bits (e.g., 8-bit, 10-bit, 12-bit, etc.). Higher resolution results in more accurate signal representation. |
| Encoding | The process of converting quantized values into binary code. | Represent discrete values in a form recognizable by digital systems, facilitating storage, transmission, and processing, so that analog signals can be used by computers or other digital devices. | No specific parameters; the focus is on generating binary code suitable for use by digital systems. |
ADCs are widely used in sensor signal acquisition, audio processing, image processing, industrial automation, and medical equipment:
- Sensor signal acquisition: Many sensors output analog signals. For example, environmental data collected by temperature sensors, pressure sensors, and photosensitive sensors typically exist in analog form.
- Audio processing: Sound signals picked up by microphones are analog signals.
- Image processing: Light signals captured by cameras are essentially analog signals.
- Industrial automation: Measurement of various physical quantities (such as speed, position, pressure, etc.) typically relies on analog signals output by sensors.
- Medical equipment: Many biological signals (such as ECG, EEG, etc.) are acquired in analog form.
ADC Resolution
The resolution of an ADC refers to its ability to distinguish the smallest change in signal. In other words, it is the fineness with which the ADC quantizes an analog signal into discrete digital values. In an ADC, higher resolution means it can represent the input analog signal more accurately. When we convert a digital signal back to analog through a DAC (digital-to-analog converter), a high-resolution ADC can produce results closer to the original analog signal.
ADC resolution is usually expressed in bits, such as 8-bit, 10-bit, 12-bit, etc. The number of bits determines the number of discrete values the ADC can output; an n-bit ADC can represent 2^n different values. For example, an 8-bit ADC can represent 2^8=256 different values; a 10-bit ADC can represent 2^10=1024 different values; a 12-bit ADC can represent 2^12=4096 different values. The higher the number of bits, the higher the resolution, and the smaller the signal change that can be distinguished.
Resolution can be calculated using the following formula:
Where:
- The range refers to the maximum range of the ADC input signal (i.e., the reference voltage range);
- n is the number of bits of the ADC;
- 2^n represents the total number of discrete values that an ADC can distinguish.
Example: Assume a 3-bit ADC has a reference voltage (range) of 2V, calculate its resolution:
- Range = 2V
- Number of bits is 3, 2^n = 2^3 = 8;
Substituting into the resolution formula gives 2V/8 = 0.25V. This means the ADC can divide the input signal into 8 discrete levels, with a step size of 0.25V between each level.
Besides resolution, other parameters of the ADC are as follows
| Parameter | Definition | Function or Significance |
|---|---|---|
| Sampling Time | The time required to complete one sampling. | Determines how quickly the ADC can capture changes in the input signal. If the sampling time is too long, it may cause signal distortion or loss of detail. |
| Conversion Time | The time required to convert an analog signal to a digital signal. | Directly affects the speed and efficiency of the ADC. A shorter conversion time means the ADC can process signals faster. |
| Sampling Frequency | The number of samples collected per second, commonly expressed in ksps (thousands of samples per second) or Msps (millions of samples per second). | Determines the ADC’s ability to capture signal changes. According to the Nyquist sampling theorem, the sampling frequency must be at least twice the highest frequency of the signal to accurately reconstruct it. |
| Accuracy | The error between the digital value output by the ADC and the actual analog input value. | Reflects the reliability and measurement quality of the ADC, serving as an important indicator of its performance. |
RK3568 ADC
In the peripheral resource block diagram of the RK3568 datasheet, it can be found that the RK3568 SoC has a total of8-channel SARADC。
SARADC (Successive Approximation Register Analog-to-Digital Converter) is called in ChineseSuccessive Approximation Register ADC;
In addition to the SAR ADC, there are two TS-ADC (Temperature Sensor ADC) channels, which are used to detect the temperature of the CPU and GPU respectively. If the temperature is detected to be too high over a period of time, an interrupt signal is sent to the processor to take cooling measures; if the temperature remains too high, the entire chip will be reset, achieving a restart effect. The TS-ADC is only responsible for detecting the temperature of the CPU and GPU, so it is not brought out to the baseboard.
ADC Key
The ADC key is a SARADC peripheral. The working principle of the ADC key isto connect multiple keys to the same ADC pin, generating different voltages through different voltage divider resistors. The ADC measures the voltage and compares it with preset voltage ranges to determine which key is pressed.。

Two ADC channels are used in HW_ID and BOM_On the ID, it is used for hardware identification of different electronic devices. However, since Topeet’s peripherals are all universal, these two ADC channels are not used.


Four ADC channels are connected to a 6-pin header, as shown in the schematic below.

Summary as follows
| ADC Interface | Multiplexed Function |
|---|---|
| SARADC VIN0 | Used as button pins (Volume +, Volume -, HOME, BACK) |
| SARADC VIN1 | Used as HW ID pin |
| SARADC VIN2 | Not used |
| SARADC VIN3 | Used as BOM ID pin |
| SARADC VIN4 | Not used |
| SARADC VIN5 | Not used |
| SARADC VIN6 | Not used |
| SARADC VIN7 | Not used |
Successive approximation ADC
Successive Approximation ADC (Successive Approximation Register ADC, abbreviated as SAR ADC) is a common type of analog-to-digital converter whose operating principle is based on the successive approximation algorithm. It determines a digital value close to the input signal by progressively comparing the input analog signal with an internally generated reference voltage. The schematic diagram of the SAR ADC working principle is shown below.

From the above diagram, it can be seen that the successive approximation register analog-to-digital converter (SAR ADC) mainly consists of the following parts:
- Comparator: Used to compare the input analog signal U1 with the reference voltage U0 output by the D/A converter (DAC) and generate a feedback signal.
- N-bit D/A Converter (DAC): Converts the digital value into an analog voltage, which is compared with the input signal to progressively approach the target value.
- N-bit Successive Approximation Register (SAR): Used to store the current comparison result and adjust the value based on the comparator’s feedback, ultimately determining the digital output.
- Bit-setting Selection Logic: Controls the bit selection process of the successive approximation register, adjusting the DAC output voltage using the binary search method.
Working Process:
- First, the analog input signal U1 enters the ADC and is temporarily stored by the sampling circuit for subsequent processing.
- Then, the successive approximation register (SAR) is initialized, and under the control of the bit-setting selection logic circuit, the final digital output value is progressively determined using the binary search method.
During the conversion process, the SAR starts by assuming the most significant bit is 1 and passes the current N-bit digital value to the D/A converter. The DAC converts it into an analog voltage U0, which is then sent to the comparator to be compared with the input signal U1. If U0 is less than U1, the current bit remains 1; otherwise, the current bit is changed to 0. This process repeats, determining one bit at a time, until all N bits are completed, forming the final digital output.
- Finally, the SAR outputs the determined N-bit digital value, completing one analog-to-digital conversion.
An example is as follows:
A 5-bit successive approximation ADC with a reference voltage of 32mV and a measured voltage of 21.5mV

- Set the most significant bit: The SAR register sets the most significant bit (bit 5) to ‘1’ and the remaining bits to ‘0’, resulting in a current SAR value of 10000 (binary), which is 16 (decimal). The DAC generates a reference voltage U0 = 16 × 1mV = 16 mV based on the SAR value. The comparator compares U1 = 21.5 mV and U0 = 16 mV. Since U1 > U0, the current bit is retained as ‘1’.
- Set the next highest bit: The SAR register keeps the most significant bit as ‘1’ and sets the next highest bit (bit 4) to ‘1’, resulting in a SAR value of 11000 (binary), which is 24 (decimal). The DAC generates a reference voltage U0 = 24 × 1 mV = 24 mV. The comparator compares U1 = 21.5 mV and U0 = 24 mV. Since U1 < U0, the current bit is reset to ‘0’.
- Set the third bit: The SAR register keeps the first two bits as ‘10’ and sets the third bit (bit 3) to ‘1’, resulting in a SAR value of 10100 (binary), which is 20 (decimal). The DAC generates a reference voltage U0 = 20 × 1 mV = 20 mV. The comparator compares U1 = 21.5 mV and U0 = 20 mV. Since U1 > U0, the current bit is retained as ‘1’.
- Set the fourth bit: The SAR register keeps the first three bits as ‘101’ and sets the fourth bit (bit 2) to ‘1’, resulting in a SAR value of 10110 (binary), which is 22 (decimal). The DAC generates a reference voltage U0 = 22 × 1 mV = 22 mV. The comparator compares U1 = 21.5 mV and U0 = 22 mV. Since U1 < U0, the current bit is reset to ‘0’.
- Set the least significant bit: The SAR register keeps the first four bits as ‘1010’ and sets the least significant bit (bit 1) to ‘1’, resulting in a SAR value of 10101 (binary), which is 21 (decimal). The DAC generates a reference voltage U0 = 21 × 1 mV = 21 mV. The comparator compares U1 = 21.5 mV and U0 = 21 mV. Since U1 > U0, the current bit is retained as ‘1’.
- Result: After successive approximation, the value of the SAR register is 10101 (binary), which is 21 (decimal).
Operate the ADC
Enter the/sys/bus/iio/devices/iio:device0directory

It should be noted thatin_voltageX_rawandin_voltage_scale。
in_voltageX_rawRepresents the raw sampling value of a certain ADC channel, where X is the channel number (e.g., 0, 1, 2, …, 7). It stores the digital code after ADC conversion as an integer. Users can read this file to obtain the unscaled raw data of the corresponding channel. Note that these values have not been scaled by range and must be combined within_voltage_scalefor calculation to obtain the actual voltage value.in_voltage_scaleis used toconvert the ADC raw sampling value to the actual voltage value as a scaling factor, typically defined as 1.8/1024. It is represented as a floating-point number, and the unit can be millivolts (mV) or volts (V), depending on the hardware implementation. By multiplying the raw value ofin_voltageX_rawbyin_voltage_scale, the actual voltage value of the corresponding channel can be obtained, thus achieving the conversion from digital code to physical quantity
Here, ADC channel 7 is used as an example for demonstration. The corresponding hardware is pin 5 of J24 on the baseboard, as shown in the figure below

The raw sampling value corresponding to ADC channel 7 isin_voltage7_raw, read using the cat command, and the value read is as follows:
1 | $ cat involtage7_raw |
Here, the unscaled raw data is read, and then continue to use the cat command to read the scaling factorin_voltage_scalevalue, as shown below:
1 | $ cat in_voltage_scale |
Then multiply the obtained raw sampling value by the scaling factor to get the actual voltage value: 1017 x 1.757812500 = 1787.6953125 mV = 1.787 V
In addition to the SAR ADC mentioned above, there are also two TSADCs, which are used to query the temperatures of the CPU and GPU respectively,
Check CPU temperature:
1 | cat /sys/class/thermal/thermal_zone0/temp |
Check GPU temperature:
1 | cat /sys/class/thermal/thermal_zone1/temp |
ADC Driver
Write an ADC driver that creates an ADC device node via a miscellaneous device, through which the ADC value of the corresponding channel can be read.
API Functions
iio_channel_get()
Function Prototype
1 | struct iio_channel *iio_channel_get(const char *name, const char *channel_name); |
Core Function
From the Linux IIO subsystem, obtain a correspondingstruct iio_channelchannel structure pointer; all subsequent operations on this ADC channel depend on this pointer.
Parameter Description
name: Specifies theIIO device name/IDto operate on; if passingNULLindicates using the system default IIO device; commonly used in SARADC scenariosNULLchannel_name: Specifies the ADC channel name to open; the specific name must match the kernel driver.
Return value
- ✅ Success: returns a valid
struct iio_channel *structure pointer (channel handle); - ❌ Failure: returns a kernel error pointer
ERR_PTR(错误码), cannot be used directly as a normal pointer.
iio_channel_release()
Obtained channel handleMust be released after use, otherwise it causes kernel resource leakage. The release function is fixed as:
1 | void iio_channel_release(struct iio_channel *channel); |
- Parameter: it is the
iio_channel_getobtained channel pointer - Feature: no return value, safe to call.
struct iio_channel
1 | struct iio_channel { |
iio_read_channel_raw()
Function Prototype
1 | int iio_read_channel_raw(struct iio_channel *channel, int *val); |
Core Function
Read from an already opened IIO channel (ADC channel)raw integer data without any calibration or conversion, which is the core function for reading ADC data.
✔️ In your SARADC scenario: the read
valis the voltage collected by the ADC pin corresponding to theraw digital value(for example, a 10-bit ADC gives values from 0 to 1023).
Parameter Description
channel: Input parameter,iio_channel_getthe channel handle returned on success,must be a valid pointer;val: Output parameter, an integer pointer. After the function executes successfully, the collected ADC raw data will be written to the variable pointed to by this address.
Return Value
- ✅ Success: returns
0; - ❌ Failure: returnsnegative kernel error codes(e.g., -EINVAL invalid argument, -ENODEV device not found, etc.).
Key Notes
The readvalisraw bare data:
- No unit, no calibration, no conversion; it is just the pure digital value after ADC analog-to-digital conversion;
- The value range is determined by the ADC bit resolution (e.g., 8-bit ADC: 0~255; 10-bit ADC: 0~1023; 12-bit ADC: 0~4095);
- If you need to convert to an actual voltage value, you must write your own formula:
实际电压 = (原始值 / ADC最大量程) * ADC参考电压。
Standard Writing in Kernel Driver
1 | // Step 1: Define variables |
Device tree
Add device node:
1 | adc7: adc-test { |
The key is io-channels, meaning the 7th channel of the ADC is to be used
Driver
1 |
|
Test:
1 | #include <stdio.h> |

