Search by Tags

ADC (Linux)


Article updated at 12 Dec 2019
Compare with Revision

Subscribe for this article updates

Depending on the module type the analogue inputs are connected via different ADCs (see ADC) therefore ADC read-out is supported by various drivers. All current drivers provide ADC access through sysfs. Access through sysfs is easy to implement since it only involves reading a regular file. Keep in mind that opening and closing a file for every ADC read-out is relatively slow and rather resource intensive. However the internal ADCs are anyway not meant to be used for high speed applications.

Apart from Colibri T20 all modules use a driver that is based on the Industrial Input/Output (IIO) framework.

More information about the IIO framework:


Colibri T20

On the Colibri T20 the WM9715 supports ADC read-out through the battery driver. Currently only AD0 and AD1 are supported.

root@colibri_t20:~# cat /sys/class/power_supply/colibri_t20-analog_inputs/voltage_now
root@colibri_t20:~# cat /sys/class/power_supply/colibri_t20-analog_inputs/temp

Apalis TK1

The ADC is implemented in the K20 MCU using its internal ADC. The TK1 driver controls and reads the values through SPI from the K20.

MXM3 K20 Input IIO Device IIO Channel
AN1_ADC0 PTB0\ADC0_SE8 iio:device0 in_voltage0_raw
AN1_ADC1 PTB1\ADC0_SE9 iio:device0 in_voltage1_raw
AN1_ADC2 PTB2\ADC0_SE12 iio:device0 in_voltage2_raw
AN1_ADC3 PTB3\ADC0_SE13 iio:device0 in_voltage3_raw
# cd /sys/bus/iio/devices/iio:device0/
# ls in*
in_voltage0_raw    in_voltage0_scale  in_voltage1_raw    in_voltage1_scale  in_voltage2_raw    in_voltage2_scale  in_voltage3_raw    in_voltage3_scale
# cat /sys/bus/iio/devices/device0/in_voltage0_raw

Apalis iMX6/Apalis T30/Colibri iMX6/Colibri T30

The STMPE-ADC driver is included since BSP V2.1 beta 2.

The STMPE811 supports up to 8 analogue inputs, however, input 1-4 are used for the resistive touchscreen interface. The inputs are 0 indexed, so the usable inputs are 4-7.

SODIMM/MXM3 STMPE811 Input IIO device IIO channel
AN1_ADC0 IN0 iio:device0 in_voltage4_raw
AN1_ADC1 IN1 iio:device0 in_voltage5_raw
AN1_ADC2 IN2 iio:device0 in_voltage6_raw
AN1_ADC3 IN3 iio:device0 in_voltage7_raw

Note that the STMPE811 pin name IN0 goes to the ADC channel 4.

The IIO framework exposes those inputs through the sysfs interface:

# cd /sys/bus/iio/devices/device0/
# ls in*
in4_raw   in5_raw   in6_raw   in7_raw   in_scale
# cat /sys/bus/iio/devices/device0/in4_raw

Note: On newer kernels the IIO framework changed its file naming scheme slightly:

root@colibri-imx6:~# cd /sys/bus/iio/devices/iio\:device0/
root@colibri-imx6:/sys/bus/iio/devices/iio:device0# ls in*
in_temp8_raw      in_voltage4_raw   in_voltage5_raw   in_voltage6_raw   in_voltage7_raw   in_voltage_scale
root@colibri-imx6:/sys/bus/iio/devices/iio:device0# cat /sys/bus/iio/devices/iio\:device0/in_voltage4_raw

Note: Since the ADC is shared with the touchscreen its not recommended to change the ADC characteristics such as sample time.

Apalis iMX8

On Apalis iMX8 the analog to digital inputs are connected directly to the SoC. The table below shows how they are connected.

SODIMM/MXM3 NXP/Freescale Name IIO device IIO channel
AN1_ADC0 ADC0_IN0 iio:device0 in_voltage0_raw
AN1_ADC1 ADC0_IN1 iio:device0 in_voltage1_raw
AN1_ADC2 ADC0_IN2 iio:device0 in_voltage2_raw
AN1_ADC3 ADC0_IN3 iio:device0 in_voltage3_raw

Apalis iMX8X

On Apalis iMX8X the analog to digital inputs are connected directly to the SoC. The table below shows how they are connected.

SODIMM/MXM3 NXP/Freescale Name IIO device IIO channel
AN1_ADC0 ADC_IN0 iio:device0 in_voltage0_raw
AN1_ADC1 ADC_IN1 iio:device0 in_voltage1_raw
AN1_ADC2 ADC_IN4 iio:device0 in_voltage4_raw
AN1_ADC3 ADC_IN5 iio:device0 in_voltage5_raw
root@apalis-imx8x:~# cd /sys/bus/iio/devices/iio:device0
root@apalis-imx8x:/sys/bus/iio/devices/iio:device0# cat in_voltage0_raw in_voltage1_raw in_voltage4_raw in_voltage5_raw
64  # AN1_ADC0
73  # AN1_ADC1
55  # AN1_ADC2
543 # AN1_ADC3

Colibri VFxx

On the Colibri VFxx modules the AD functionality is making use of the two integrated 12-Bit ADC peripherals of the Vybrid SoC.

SODIMM Vybrid ADC IIO device IIO channel
ANALOG_IN0 ADC0, Channel 8 iio:device0 in_voltage8_raw
ANALOG_IN1 ADC1, Channel 8 iio:device1 in_voltage8_raw
ANALOG_IN2 ADC0, Channel 9 iio:device0 in_voltage9_raw
ANALOG_IN3 ADC1, Channel 9 iio:device1 in_voltage9_raw

These four analogue inputs are compatible within the Colibri family and are dedicated ADC inputs. Additional some of the GPIO capable pins on the SODIMM connector feature with alternative function ADC capability. The ADC function of these pins are not compatible with other Colibri modules. It should be noted, that the noise level on these pins might be higher than on the dedicated ADC input signals, as these are routed as digital signals on the module.

For BSP versions 2.3 Beta3 and earlier, the driver is called mvf-adc and supports access through a character device only. We created a test program to show how the driver can be used, see adc-test.c.

Starting with BSP versions 2.3 Beta5, the vf610_adc driver makes use of the Industrial Input/Output (IIO) framework. The IIO framework exposes the ADC channels through the sysfs interface. The inputs are indexed through 0 to 15 with the channels 8 and 9 for ADC0 and ADC1 being usable by default. The ADC peripherals also include an internal temperature sensor. The readout of them is given by in_temp_input in degree celsius times 1000.

root@colibri-vf:~# cd /sys/bus/iio/devices/iio\:device0
root@colibri-vf:/sys/bus/iio/devices/iio:device0# ls
dev                            in_voltage5_raw
in_temp_input                  in_voltage6_raw
in_voltage0_raw                in_voltage7_raw
in_voltage10_raw               in_voltage8_raw
in_voltage11_raw               in_voltage9_raw
in_voltage12_raw               in_voltage_sampling_frequency
in_voltage13_raw               in_voltage_scale
in_voltage14_raw               name
in_voltage15_raw               power
in_voltage1_raw                sampling_frequency_available
in_voltage2_raw                subsystem
in_voltage3_raw                uevent
root@colibri-vf:/sys/bus/iio/devices/iio:device0# cat in_temp_input 
root@colibri-vf:/sys/bus/iio/devices/iio:device0# cat in_voltage8_raw 
root@colibri-vf:~# cd /sys/bus/iio/devices/iio\:device1
root@colibri-vf:/sys/bus/iio/devices/iio:device1# cat in_temp_input 
root@colibri-vf:/sys/bus/iio/devices/iio:device1# cat in_voltage9_raw 

Continuous Sampling

Vybrid provides support for continuous sampling with its ADC peripheral. Currently only software triggers are supported, they are accessible through sysfs. The necessary driver support is available with version 2.6 Beta1 and above. The 2.6 Beta1 release image is missing the sysfs trigger configuration option (CONFIG_IIO_SYSFS_TRIGGER) and hence requires a custom kernel build.

root@colibri-vf:~# cd /sys/bus/iio/devices/
root@colibri-vf:/sys/bus/iio/devices# ls
iio:device0        iio:device1        iio_sysfs_trigger
root@colibri-vf:/sys/bus/iio/devices# cd iio:device0
root@colibri-vf:/sys/bus/iio/devices/iio:device0# ls
buffer                         in_voltage13_raw               in_voltage6_raw                power
dev                            in_voltage14_raw               in_voltage7_raw                sampling_frequency_available
in_conversion_mode             in_voltage15_raw               in_voltage8_raw                scan_elements
in_temp_input                  in_voltage1_raw                in_voltage9_raw                subsystem
in_voltage0_raw                in_voltage2_raw                in_voltage_sampling_frequency  trigger
in_voltage10_raw               in_voltage3_raw                in_voltage_scale               uevent
in_voltage11_raw               in_voltage4_raw                name
in_voltage12_raw               in_voltage5_raw                of_node

The generic buffer code present in Linux kernel source tree can be used to test the continuous sampling functionality.

root@colibri-vf:~# echo 1 > /sys/bus/iio/devices/iio:device0/scan_elements/in_temp_en
root@colibri-vf:~# echo 1 > /sys/bus/iio/devices/iio:device0/scan_elements/in_timestamp_en
root@colibri-vf:~# echo 0 > /sys/bus/iio/devices/iio_sysfs_trigger/add_trigger
root@colibri-vf:~# ./generic_buffer -n 4003b000.adc -t sysfstrig0 -l 512 -c 1                                              
iio device number being used is 0
iio trigger number being used is 0
/sys/bus/iio/devices/iio:device0 sysfstrig0
835.000000 1463058211407519734 
835.000000 1463058211407532489 
834.000000 1463058211407542708 
835.000000 1463058211407556933 
835.000000 1463058211407572004 
835.000000 1463058211407587044 
836.000000 1463058211407601449 
835.000000 1463058211407615464 
835.000000 1463058211407629923 
835.000000 1463058211407644663 
835.000000 1463058211407659362 
835.000000 1463058211407674031 
836.000000 1463058211407688639 
835.000000 1463058211407703164 
835.000000 1463058211407717683 
834.000000 1463058211407731968 
root@colibri-vf:~# echo 0 > /sys/bus/iio/devices/iio:device0/scan_elements/in_timestamp_en
root@colibri-vf:~# echo 0 > /sys/bus/iio/devices/iio:device0/scan_elements/in_temp_en
root@colibri-vf:~# echo 0 > /sys/bus/iio/devices/iio_sysfs_trigger/remove_trigger

Colibri iMX6ULL

On the Colibri iMX6ULL modules the AD functionality is making use of the integrated 12-Bit ADC peripheral of the i.MX 6ULL SoC.

SODIMM i.MX 6ULL ADC IIO device IIO channel
ANALOG_IN0 ADC1_IN0 iio:device0 in_voltage0_raw
ANALOG_IN1 ADC1_IN1 iio:device0 in_voltage1_raw
ANALOG_IN2 ADC1_IN8 iio:device0 in_voltage8_raw
ANALOG_IN3 ADC1_IN9 iio:device0 in_voltage9_raw
root@colibri-imx6ull:~# cd /sys/bus/iio/devices/iio\:device0
root@colibri-imx6ull:/sys/bus/iio/devices/iio:device0# ls
dev                            in_voltage9_raw
in_voltage0_raw                in_voltage_sampling_frequency                    
in_voltage1_raw                in_voltage_scale
in_voltage2_raw                name
in_voltage3_raw                of_node
in_voltage4_raw                power
in_voltage5_raw                sampling_frequency_available
in_voltage6_raw                subsystem
in_voltage7_raw                uevent
root@colibri-imx6ull:/sys/bus/iio/devices/iio:device0# cat in_voltage0_raw in_voltage1_raw in_voltage8_raw in_voltage9_raw

Colibri iMX7

On the Colibri iMX7 modules the AD functionality is making use of the integrated 12-Bit ADC peripheral of the i.MX 7 SoC.

SODIMM i.MX 7 ADC IIO device IIO channel
ANALOG_IN0 ADC1_IN0 iio:device0 in_voltage0_raw
ANALOG_IN1 ADC1_IN1 iio:device0 in_voltage1_raw
ANALOG_IN2 ADC1_IN2 iio:device0 in_voltage2_raw
ANALOG_IN3 ADC1_IN3 iio:device0 in_voltage3_raw

Note: The ADC channels are only 1.8V capable! This is a limitation of the i.MX 7 SoC.

root@colibri-imx7:~# cd /sys/bus/iio/devices/iio\:device0/
root@colibriimx7:/sys/bus/iio/devices/iio:device0# ls
dev               in_voltage12_raw  in_voltage1_raw  in_voltage5_raw  in_voltage9_raw                of_node
in_voltage0_raw   in_voltage13_raw  in_voltage2_raw  in_voltage6_raw  in_voltage_sampling_frequency  power
in_voltage10_raw  in_voltage14_raw  in_voltage3_raw  in_voltage7_raw  in_voltage_scale               subsystem
in_voltage11_raw  in_voltage15_raw  in_voltage4_raw  in_voltage8_raw  name                           uevent
root@colibriimx7:/sys/bus/iio/devices/iio:device0# cat in_voltage0_raw in_voltage1_raw in_voltage2_raw in_voltage3_raw

Colibri iMX8X

SODIMM NXP/Freescale Name IIO device IIO channel
ANALOG_IN0 ADMA_ADC_IN1 iio:device0 in_voltage0_raw
ANALOG_IN1 ADMA_ADC_IN2 iio:device0 in_voltage1_raw
ANALOG_IN2 ADMA_ADC_IN4 iio:device0 in_voltage4_raw
ANALOG_IN3 ADMA_ADC_IN5 iio:device0 in_voltage5_raw

Verdin iMX8M Mini

The ADC is implemented with a I2C ADC.

SODIMM ADC input IIO Device IIO Channel
ADC_1 AIN0 iio:device0 in_voltage0_raw
ADC_2 AIN1 iio:device0 in_voltage1_raw
ADC_3 AIN2 iio:device0 in_voltage2_raw
ADC_4 AIN3 iio:device0 in_voltage3_raw
root@verdin-imx8mm:~# cd /sys/bus/iio/devices/iio\:device0/

root@verdin-imx8mm:/sys/bus/iio/devices/iio:device0# ls in*
in_voltage-voltage_scale  in_voltage0_raw           in_voltage1_raw           in_voltage2_raw           in_voltage3_raw
in_voltage0-voltage1_raw  in_voltage1-voltage0_raw  in_voltage2-voltage3_raw  in_voltage3-voltage2_raw  in_voltage_scale

root@verdin-imx8mm:/sys/bus/iio/devices/iio:device0# cat in_voltage0_raw