ADC OpenSTLinux drivers

Revision as of 12:03, 3 March 2022 by Registered User
Applicable for STM32MP13x lines, STM32MP15x lines

1. Article purpose[edit source]

This article introduces the Linux® and U-boot drivers for the ADC[1] internal peripheral:

  • Which ADC features are supported by the driver
  • How to configure, use and debug the driver
  • What is the driver structure, and where the source code can be found.

2. Supported features[edit source]


2.1. Calibration[edit source]

The U-boot and Linux kernel drivers implement calibration support to improve the ADC accuracy. The recommendations to enhance ADC accuracy can be found in the application note AN2834[2].


The ADC[1] internal peripheral offers two kind of calibration:

  • Offset calibration

This calibration is fast and may vary over time depending on voltage and temperature variations. The offset single-ended and differential calibration is performed on each boot in U-boot driver, and each time new conversions are launched in Linux kernel driver.

  • Linear calibration ( STM32MP15x lines More info.png only)

The linear calibration is SoC dependent. This calibration is time consuming and does not change over time as it is not voltage or temperature dependent, so it can to be done only once, on first boot. This calibration has to be performed when there is a minimal activity on CPU, to minimize clock and voltage disturbances. For this reason, the linear calibration is performed in U-Boot, and corresponding linear calibration factors are retrieved on Linux kernel side whenever possible.

ADC calibration in U-Boot
ADC calibration in Linux kernel

2.2. Linux kernel[edit source]

The ADC Linux® driver (kernel space) is based on the IIO framework. It supports two modes:

  1. IIO direct mode: single capture on a channel (using interrupts)
  2. IIO triggered buffer mode: capture on one or more channels (preferably using DMA).
    It uses the hardware triggers available in IIO. See TIM Linux driver and LPTIM Linux driver.


2.3. U-boot[edit source]


The U-boot driver is based on the adc-uclass framework. It supports single conversions (using polling)


3. Configuration[edit source]

3.1. Kernel configuration[edit source]


Activate the ADC[1] Linux® driver in the kernel configuration using the Linux Menuconfig tool: Menuconfig or how to configure kernel (enable both CONFIG_STM32_ADC_CORE and CONFIG_STM32_ADC).

Device Drivers  --->
   <*> Industrial I/O support  --->
      Analog to digital converters  --->
         <*> STMicroelectronics STM32 adc core
         <*>   STMicroelectronics STM32 adc

3.2. U-boot configuration[edit source]


Activate the ADC[1] U-boot driver in the configuration menu, by using the Menuconfig tool. (enable CONFIG_STM32_ADC).

Device Drivers  --->
	[*] Enable STMicroelectronics STM32 ADC driver


3.3. Device tree[edit source]

Refer to the ADC device tree configuration article when configuring the ADC Linux kernel or U-boot driver.


4. How to use[edit source]

4.1. Linux kernel[edit source]


In "IIO direct mode", the conversion result can be read directly from sysfs (refer to How to do a simple ADC conversion using the sysfs interface).

In "IIO triggered buffer mode", the configuration must be performed using sysfs first. Then, character device (/dev/iio:deviceX) is used to read data (refer to Convert one or more channels using triggered buffer mode).

4.2. U-boot[edit source]

The Analog to Digital Converters info and data can be accessed through the ADC command line tool. (The command line interface must be enabled in U-Boot configuration.)

5. How to trace and debug[edit source]

5.1. Linux kernel[edit source]

Refer to How to trace with dynamic debug for how to enable the debug logs in the driver and in the framework.

Refer to How to debug with debugfs for how to access the ADC registers.

The ADC has system wide dependencies towards other key resources:

  • runtime power management can be disabled, for example it may be forced on via power/control sysfs entry:
 cd /sys/devices/platform/soc/48003000.adc/48003000.adc:adc@0
 cat power/autosuspend_delay_ms
2000
 cat power/control
auto                                     # kernel is allowed to automatically suspend the ADC device after autosuspend_delay_ms
 echo on > power/control         # force the kernel to resume the ADC device (e.g. keep clocks and regulators enabled)
Info white.png Information
It might be useful to disable runtime power management, in order to dump registers by any means or to check clock and regulator usage (see example below).
  • clock[3] usage can be verified by reading clk_summary:
 cat /sys/kernel/debug/clk/clk_summary | grep adc
         adc12_k                     1        1        0    24000000          0 0
                  adc12              1        1        0   196607910          0 0
  • regulator[4] tree and usage can be verified (e.g. use count, open count or regulator reference voltage) as follows:
 cat /sys/kernel/debug/regulator/regulator_summary
 regulator                      use open bypass voltage current     min     max
-------------------------------------------------------------------------------
    v3v3                          4    5      0  3300mV     0mA  3300mV  3300mV
       vdda                       1    2      0  2900mV     0mA  2900mV  2900mV 
          40017000.dac                                              0mV     0mV
          48003000.adc                                              0mV     0mV
  • pinctrl[5] usage can be verified by reading pinmux-pins:
 cd /sys/kernel/debug/pinctrl/soc\:pin-controller@50002000/
 cat pinmux-pins | grep adc
pin 92 (PF12): device 48003000.adc function analog group PF12   # check pin is assigned to ADC and is configured as "analog"
  • interrupts can be verified by reading "interrupts":
 cat /proc/interrupts
           CPU0       CPU1
 56:          2          0     dummy   0 Edge      48003000.adc:adc@0


6. Source code location[edit source]

6.1. Linux kernel[edit source]

The ADC source code is composed of:

  • stm32-adc-core driver to handle common resources such as clock (selection, prescaler), regulator used as reference voltage, interrupt and common registers.
  • stm32-adc driver to handle the resources available for each ADC such as channel configuration and buffer handling.

6.2. U-boot[edit source]

The ADC source code is composed of:

  • stm32-adc-core driver to handle common resources such as clock (selection, prescaler), regulator used as reference voltage, interrupt and common registers.
  • stm32-adc driver to handle the resources available for each ADC such as channel configuration.

7. References[edit source]