Difference between revisions of "ADC device tree configuration"

[quality revision] [checked revision]
m
m (Article purpose)
 
Applicable for STM32MP13x lines, STM32MP15x lines

1 Article purpose[edit]

The purpose of this article is to explain how to configure the analog-to-digital converter (ADC)[1] when the peripheral is assigned to Linux® OS, and in particular:

  • how to configure the ADC peripheral
  • how to configure the board, e.g. the ADC voltage reference regulator, channels, pins and sampling time.

The configuration is performed ADC internal peripheral using the device tree mechanism[2].

It is used by the ADC Linux driver that registers relevant information in IIO framework, such as IIO devices, channels and voltage scale for each ADC.

If the peripheral is assigned to another execution context, refer , relying on the bindings documentation that is the description of the required and optional device-tree properties.

The peripheral can be assigned to different contexts/software components, depending on the final product needs. Refer to How to assign an internal peripheral to a runtime context article for guidelines on peripheral assignment and this configuration.

The device tree file to configure depends on the context/component to which the ADC peripheral is assigned:

2 DT bindings documentation[edit]

U-Boot, Linux® OS: STM32 ADC device tree bindings[3] describe all the required and optional functions. : Documentation/devicetree/bindings/iio/adc/st,stm32-adc.yaml

3 DT configuration[edit]

This hardware description is a combination of the STM32 microprocessor device tree files (.dtsi extension) and board device tree files (.dts extension). See the Device tree for more explanations on an explanation of the device tree file split.

The STM32CubeMX can be used to generate the board device tree. Refer to How to configure the DT using STM32CubeMX for more details.

ADC common resources ADC common resources ADC common resources ADC private resources ADC private resources Regulator Pinctrl How to setup TIM or LPTIM triggers
ADC DT configuration

3.1 DT configuration (STM32/SoC level)[edit]

The ADC nodes are declared in stm32mp151.dtsi[4].

DT root node ('adc') describes the ADC hardware

An ADC peripheral provides one or more ADCs. Some hardware common resources are managed in the ADC peripheral, whatever the number of ADC sub-blocks. In the following chapters, the ADC peripheral is called "ADC block", while "ADC" refers to an ADC peripheral sub-block.

The ADC DT nodes are structured as follows:

  • The DT root node describes the ADC block parameters such as register areas, clocks and interrupts.
  • The DT child nodes ('adc1' and/or 'adc2') describe ADC1 and ADC2 the ADCs independently.

3.1.1 STM32MP13x lines Warning.png[edit]

The ADC nodes are declared in stm32mp131.dtsi[1] and in stm32mp133.dtsi[2].

adc_1: adc@address {
	compatible = "st,stm32mp1stm32mp13-adc-core";
	...                                      /* common resources in 'adc_1' root node. */
	adc1: adc@0 {
		compatible = "st,stm32mp1stm32mp13-adc";
		...                              /* private resources in 'adc1' child node. */
	};
};
adc_2: adc@address {
	compatible = "st,stm32mp13-adc-core";
	...                                      /* common resources in 'adc_2' root node. */
	adc2: adc@100adc@0 {
		compatible = "st,stm32mp1stm32mp13-adc";
		...                              /* private resources in 'adc2' child node. */
	};
};
Warning white.png Warning
This device tree part is related to STM32 microprocessors. It must be kept as is, without being modified by the end-user.

3.1.2 STM32MP15x lines More info.png[edit]

The ADC nodes are declared in stm32mp151.dtsi[3].

adc: adc@address {
	compatible = "st,stm32mp1-adc-core";
	...                                      /* common resources in 'adc' root node. */
	adc1: adc@0 {
		compatible = "st,stm32mp1-adc";
		...                              /* private resources in 'adc1' child node. */
	};
	adc2: adc@100 {
		compatible = "st,stm32mp1-adc";
		...                              /* private resources in 'adc2' child node. */
	};
};

3.2 DT configuration (board level)[edit]

Follow the To configure and enable the ADC on your board, follow the sequences described in the below chapters to configure and enable the ADC on your boardfollowing chapters. The objective of this chapter is to explain how to enable and configure the ADC internal peripheral DT nodes for a board. This board DT is overloaded on top of the SoC DT. It is usually composed of several DT files: at least one "board" dts file, that includes the "SoC" dtsi file, plus one or several other dts files.

3.2.1 Common resources for all ADCs[edit]

The DT root node ('adc') must be filled in:

  • Enable the ADC block by setting status = "okay".
  • Configure the pins in use via pinctrl, through pinctrl-0 and pinctrl-names.
  • Configure the analog supply voltage regulator[54] by setting vdda-supply = <&your_vdda_regulator>.
  • Configure the analog reference voltage regulator[54] by setting vref-supply = <&your_vref_regulator>.
Info white.png Information
The ADC block can use the internal VREFBUF[65] or any other external regulator[54] wired to VREF+ pin.

3.2.2 Resources dedicated to ADC1 and ADC2an ADC[edit]

The DT child nodes ('adc1' and /or 'adc2') must be filled in :

Enable 'adc1' and/or 'adc2'

with the ADC and channels properties.

3.2.2.1 ADC properties[edit]
  • Enable ADC by setting status = "okay".
  • Set ADC resolution by setting assigned-resolution-bits = <12> (optional).
3.2.2.2 Channel properties (for ecosystem release ≥ v3.1.0 )[edit]

Each channel is described by a dedicated DT sub node, filled in with properties documented in Generic IIO bindings for ADC channels[6].

  • Define channel index 'x' by setting reg = <x>.
  • Identifiy the channel by setting its name with label = "xxx" (optional).
    Reserved labels allow to identify the internal channels.
    • "vrefint", "vddcore": These internal channels are defined in the SoC DT. They can be removed in the board DT if they are not used.
    • "vbat": This internal channel can be defined in the board DT. It is not defined by default in the SoC DT to save current consumption.
  • Enable differential channel pair (<vinp vinn>) by setting diff-channels = <x y> (optional).
  • Set the minimum sampling time (optional):
    • by setting st,min-sample-time-ns = <10000>, for ecosystem release ≥ v4.0.0 More info.png
    • by setting st,min-sample-time-nsecs = <10000>, for ecosystem release v3.1.0
3.2.2.3 Legacy channel properties (for ecosystem release ≤ v3.0.0 )[edit]

These properties are deprecated, but are still supported for backward compatibility. The properties introduced in v3.1.0 should be used instead, whenever possible.

  • Enable single-ended channel(s) (<vinp...>) by setting st,adc-channels = <0 1 2...>.
  • Enable differential channel(s) pairs (<vinp vinn>, ...) by setting st,adc-diff-channels = <1 0>, <2 6>, ....
  • Set the minimum sampling time [7] for each or all channels by setting st,min-sample-time-nsecs = <10000> (optional).
  • Set the resolution by setting assigned-resolution-bits = <12> (optional).
  • 3.3 DT configuration exampleexamples[edit]

    The example below shows how to configure ADC1:

    • Input pin: use Pinctrl device tree configuration to configure PF12 as analog input.
    • Analog supply: it is provided by one of the PMIC LDO regulators.
    • Voltage reference: it is provided by the VREFBUF internal regulator.
    • Input channel: configure ADC1_IN6 (e.g on PF12).
    • Sampling time: the minimum sampling time is 10 µs.
    # part of pin-controller dt node
    adc1_in6_pins_a: adc1-in6 {
    	pins {
    		pinmux = <STM32_PINMUX('F', 12, ANALOG)>; /* configure 'PF12' as ANALOG */
    	};
    };
    

    3.3.1 For ecosystem release ≥ v3.1.0[edit]

    Warning white.png Warning
    For ecosystem release v3.1.0 use "st,min-sample-time-nsecs", instead of "st,min-sample-time-ns", to configure the sampling time.
    &adc {
    	/* ADC1ADC &block ADC2 common resources */
    	pinctrl-names = "default";
    	pinctrl-0 = <&adc1_in6_pins_a>;              /* Use PF12 pin as ANALOG */
    	vdda-supply = <&vdda>;                       /* Example to supply vdda pin by using a PMIC regulator
    	vref-supply = <&vrefbuf>;                    /* Example to use VREFBUF (It needs to be enabled as well) */
    	status = "okay";                             /* Enable ADC12ADC block */
    	adc1: adc@0 {
    	        #address-cells = <1>;
            	#size-cells = <0>;
    		/* private resources for ADC1 */
    		st,adc-channelsstatus = "okay";                     /* Enable ADC1 */
    		channel@6 {
    			reg = <6>;               /* ADC1 in6 channel is used */
    			st,min-sample-time-nsecsns = <10000>;  /* 10µs sampling time */
    		};
    	};
    };
    
    

    3.3.2 For ecosystem release ≤ v3.0.0[edit]

    &adc {
    	/* ADC common resources */
    	pinctrl-names = "default";
    	pinctrl-0 = <&adc1_in6_pins_a>;              /* Use PF12 pin as ANALOG */
    	vdda-supply = <&vdda>;                       /* Example to supply vdda pin by using a PMIC regulator
    	vref-supply = <&vrefbuf>;                    /* Example to use VREFBUF (It needs to be enabled as well) */
    	status = "okay";                             /* Enable 
    
    ADC1
    ADC block */
    	
    
    }; adc2
    adc1: 
    
    adc@100
    adc@0 {
    		/* private resources for 
    
    ADC2
    ADC1 */
    		
    
    ...
    st,adc-channels = <6>;               /* ADC1 in6 channel is used */
    		st,min-sample-time-nsecs = <10000>;  /* 10µs sampling time */
    		status = "okay";                     /* Enable ADC1 */
    	};
    };
    
    

    4 How to configure the DT using STM32CubeMX[edit]

    The STM32CubeMX tool can be used to configure the STM32MPU device and get the corresponding platform configuration device tree files.
    The STM32CubeMX may not support all the properties described in DT binding files listed in the above DT bindings documentation paragraph. If so, the tool inserts user sections in the generated device tree. These sections can then be edited to add some properties, and they are preserved from one generation to another. Refer to STM32CubeMX user manual for further information.

    5 References[edit]

    For additional information, refer to the following links:




    == <noinclude>{{ApplicableFor
    |MPUs list=STM32MP13x, STM32MP15x
    |MPUs checklist=STM32MP13x,STM32MP15x
    }}</noinclude>
    
    == Article purpose ==
    The purpose of this article is to explain how to configure the analog-to-digital converter (''ADC'')<ref name="ADC internal peripheral">[[ADC internal peripheral]]</ref> ''' when the peripheral is assigned to Linux<sup>&reg;</sup> OS''', and in particular:
    * how to configure the ADC '''peripheral'''
    * how to configure the '''board''', e.g. the ADC voltage reference regulator, channels, pins and sampling time.
    
    The configuration is performed using the '''device tree mechanism'''<ref> [[Device tree]]</ref>.
    
    It is used by the [[ADC Linux driver]] that registers relevant information in [[IIO overview|IIO framework]], such as IIO devices, channels and voltage scale for each ADC.
    
    If the peripheral is assigned to another execution context, refer to [[How to assign an internal peripheral to a runtime context]] article for guidelines on peripheral assignment and configuration.
    
    == DT bindings documentation ==
    ''[[ADC_internal_peripheral|ADC internal peripheral]] using the [[Device tree|device tree]] mechanism, relying on the bindings documentation that is the description of the required and optional device-tree properties.
    
    The peripheral can be assigned to different contexts/software components, depending on the final product needs. Refer to [[How to assign an internal peripheral to a runtime context]] for guidelines on this configuration.
    
    The device tree file to configure depends on the context/component to which the ADC peripheral is assigned:
    * U-Boot (Boot time context): refer to [[U-Boot_overview|U-Boot overview]].
    * Linux® OS (Runtime context): refer to the [[IIO overview|IIO framework]].
    
    == DT bindings documentation ==
    U-Boot, Linux® OS: STM32 ADC device tree bindings''<ref>: {{CodeSource | Linux kernel | Documentation/devicetree/bindings/iio/adc/st,stm32-adc.yaml | Documentation/devicetree/bindings/iio/adc/st,stm32-adc.yaml}}, STM32 ADC device tree bindings</ref> describe all the required and optional functions.
    
    
    == DT configuration ==
    This hardware description is a combination of the '''STM32 microprocessor and board device tree files. See [[Device tree]] for more explanations on ''' device tree files (''.dtsi'' extension) and '''board''' device tree files (''.dts'' extension). See the [[Device tree]] for an explanation of the device tree file split.
    The '''STM32CubeMX''' can be used to generate the board device tree. Refer to [[#How_to_configure_the_DT_using_STM32CubeMX|How to configure the DT using STM32CubeMX]] for more details.
    {{
    ImageMap|Image:ADC DT configuration.png {{!}} frame {{!}} center{{!}} ADC DT configuration
    rect 233 249 295 287237 230 299 265 [[#Common_resources_for_all_ADCs|ADC common resources]]
    rect 333 158 395 197341 138 403 174 [[#Common_resources_for_all_ADCs|ADC common resources]]
    rect 511 91 573 130525 68 585 105 [[#Common_resources_for_all_ADCs|ADC common resources]]
    rect 333 313 395 352341 297 403 334 [[#Private_resources_for_an_ADC|ADC private resources]]
    rect 511 283 573 323525 267 585 301 [[#Private_resources_for_an_ADC|ADC private resources]]
    rect 141 157 227 183145 136 231 159 [[Regulator overview|Regulator]]
    rect 141 269 227 293145 250 231 272 [[Pinctrl overview|Pinctrl]]
    rect 141 307 227 330145 288 231 311 [[How to use the IIO user space interface#How to set up a TIM or LPTIM trigger using the sysfs interface|How to setup TIM or LPTIM triggers]]
    }}
    
    === DT configuration (STM32/SoC level) ===TheAn ADC nodes are declared in stm32mp151.dtsi<ref name="stm32mp151_dtsi">{{CodeSource | Linux kernel | arch/arm/boot/dts/stm32mp151.dtsi | STM32MP151 device tree file}}</ref>.
    
    * '''DT root node''' (''''adc'''') describes the ADC hardware peripheral provides one or more ADCs. Some hardware common resources are managed in the ADC peripheral, whatever the number of ADC sub-blocks. In the following chapters, the ADC peripheral is called "'''ADC block'''", while "'''ADC'''" refers to an ADC peripheral sub-block.
    
    The ADC DT nodes are structured as follows:
    * The '''DT root node''' describes the ADC block parameters such as register areas, clocks and interrupts.
    * The '''DT child nodes''' (''''adc1'''' and/or ''''adc2'''') describe ADC1 and ADC2 independently.
     adc: adc@address {
     	compatible = "st,stm32mp1the ADCs independently.
    {{ReviewsComments|-- [[User:Nathalie Sangouard|Nathalie Sangouard]] ([[User talk:Nathalie Sangouard|talk]]) 15:42, 30 May 2022 (CEST)<br />I have changed the ordre MP13 /MP15 because it should be given in alphabetic order : MP13 first}}
    ===={{MicroprocessorDevice | device=13}}====
    The ADC nodes are declared in stm32mp131.dtsi<ref name="stm32mp131_dtsi">{{CodeSource | Linux kernel | arch/arm/boot/dts/stm32mp131.dtsi | STM32MP131 device tree file}}</ref> and in
    stm32mp133.dtsi<ref name="stm32mp133_dtsi">{{CodeSource | Linux kernel | arch/arm/boot/dts/stm32mp133.dtsi | STM32MP133 device tree file}}</ref>.
     adc_1: adc@address {
     	compatible = "st,stm32mp13-adc-core";
     	...                                      {{highlight|/* common resources in 'adc_1' root node. */}}
     	adc1: adc@0 {
     		compatible = "st,stm32mp1stm32mp13-adc";
     		...                              {{highlight|/* private resources in 'adc1' child node. */}}
     	};	adc2: adc@100 {
     		compatible = "st,stm32mp1};
     adc_2: adc@address {
     	compatible = "st,stm32mp13-adc-core";
     	...                                      {{highlight|/* common resources in 'adc_2' root node. */}}
     	adc2: adc@0 {
     		compatible = "st,stm32mp13-adc";
     		...                              {{highlight|/* private resources in 'adc2' child node. */}}
     	};
     };
    {{Warning|This device tree part is related to STM32 microprocessors. It must be kept as is, without being modified by the end-user.}}
    
    === ={{MicroprocessorDevice | device=15}}====
    
    The ADC nodes are declared in stm32mp151.dtsi<ref name="stm32mp151_dtsi">{{CodeSource | Linux kernel | arch/arm/boot/dts/stm32mp151.dtsi | STM32MP151 device tree file}}</ref>.
     adc: adc@address {
     	compatible = "st,stm32mp1-adc-core";
     	...                                      {{highlight|/* common resources in 'adc' root node. */}}
     	adc1: adc@0 {
     		compatible = "st,stm32mp1-adc";
     		...                              {{highlight|/* private resources in 'adc1' child node. */}}
     	};
     	adc2: adc@100 {
     		compatible = "st,stm32mp1-adc";
     		...                              {{highlight|/* private resources in 'adc2' child node. */}}
     	};
     };
    === DT configuration (board level) ===Follow the 
    
    To configure and enable the ADC on your board, follow the sequences described in the belowfollowing chapters to configure and enable the ADC on your board.
    .
    The objective of this chapter is to explain how to enable and configure the ADC internal peripheral DT nodes for a board.
    This board DT is overloaded on top of the SoC DT. It is usually composed of several DT files: at least one "board" dts file, that includes the "SoC" dtsi file, plus one or several other dts files. 
    ==== Common resources for all ADCs ====
    The '''DT root node''' (''''adc'''') must be filled in:
    * Enable the ADC block by setting '''status = "okay".'''
    * Configure the pins in use via [[Pinctrl overview|pinctrl]], through '''pinctrl-0''' and '''pinctrl-names'''.
    * Configure the analog supply voltage regulator<ref name="Regulator overview">[[Regulator overview]]</ref> by setting '''vdda-supply = <&your_vdda_regulator>'''.
    * Configure the analog reference voltage regulator<ref name="Regulator overview">[[Regulator overview]]</ref> by setting '''vref-supply = <&your_vref_regulator>'''.
    {{info|The ADC block can use the internal VREFBUF<ref name="VREFBUF internal peripheral">[[VREFBUF internal peripheral]]</ref> or any other external regulator<ref name="Regulator overview"/> wired to VREF+ pin.}}
    
    ==== Resources dedicated to ADC1 and ADC2an ADC ====
    The '''DT child nodes''' (''''adc1'''' and/or ''''adc2'''') must be filled in:
    * Enable 'adc1' and/or 'adc2' by setting '''status = "okay".'''
    * Enable  with the ADC and channels properties.
    
    ===== ADC properties =====
    * Enable ADC by setting '''status = "okay".'''
    * Set ADC resolution by setting '''assigned-resolution-bits = <12>''' (optional).
    ===== Channel properties (for {{EcosystemRelease | revision=3.1.0 | range=and after}}) =====
    Each channel is described by a dedicated '''DT sub node''', filled in with properties documented in 
    ''Generic IIO bindings for ADC channels''<ref>{{CodeSource | Linux kernel | Documentation/devicetree/bindings/iio/adc/adc.yaml | Documentation/devicetree/bindings/iio/adc/adc.yaml}}, Generic IIO bindings for ADC channels</ref>.
    * Define channel index 'x' by setting '''reg = <x>'''.
    * Identifiy the channel by setting its name with '''label = "xxx"''' (optional). <BR>Reserved labels allow to identify the internal channels.
    ** "'''vrefint'''", "'''vddcore'''": These internal channels are defined in the SoC DT. They can be removed in the board DT if they are not used.
    ** "'''vbat'''": This internal channel can be defined in the board DT. It is not defined by default in the SoC DT to save current consumption.
    * Enable differential channel pair (''<vinp vinn>'') by setting '''diff-channels = <x y>''' (optional).
    * Set the minimum sampling time (optional):
    ** by setting '''st,min-sample-time-ns = <10000>''', for {{EcosystemRelease | revision=4.0.0 | range=and after}}
    ** by setting '''st,min-sample-time-nsecs = <10000>''', for {{EcosystemRelease | revision=3.1.0}}
    <div class="mw-collapsible mw-collapsed">
    
    =====  Legacy channel properties (for {{EcosystemRelease | revision=3.0.0 | range=and before}}) =====<div class="mw-collapsible-content">
    
    These properties are deprecated, but are still supported for backward compatibility.
    The properties introduced in v3.1.0 should be used instead, whenever possible.
    * Enable single-ended channel(s) (''<vinp...>'') by setting '''st,adc-channels = <0 1 2...>.'''
    * Enable differential channel(s) pairs (''<vinp vinn>, ...'') by setting '''st,adc-diff-channels = <1 0>, <2 6>, ....'''
    * Set the minimum sampling time <ref>[http://www.st.com/content/ccc/resource/technical/document/application_note/group0/3f/4c/a4/82/bd/63/4e/92/CD00211314/files/CD00211314.pdf/jcr:content/translations/en.CD00211314.pdf How to get the best ADC accuracy in STM32], by STMicroelectronics</ref> for each or all channels by setting '''st,min-sample-time-nsecs = <10000>''' (optional).* Set the resolution by setting '''assigned-resolution-bits = <12>''' (optional).
    
    === DT configuration example</div></div>
    
    
    === DT configuration examples ===
    The example below shows how to configure ADC1:
    * Input pin: use [[Pinctrl device tree configuration]] to configure PF12 as analog input.
    * Analog supply: it is provided by one of the [[PMIC hardware components|PMIC]] LDO regulators.
    * Voltage reference: it is provided by the [[Regulator_overview#Microprocessor device internal regulators|VREFBUF internal regulator]].
    * Input channel: configure ADC1_IN6 (e.g on PF12).
    * Sampling time: the minimum sampling time is 10 µs. 
    
     # part of pin-controller dt node
     adc1_in6_pins_a: adc1-in6 {
     	pins {
     		pinmux = <STM32_PINMUX({{highlight|'F'}},{{highlight| 12}}, ANALOG)>; {{highlight|/* configure 'PF12' as ANALOG */}}
     	};
     };
     &adc {
     	/* ADC1 & ADC2 ==== For {{EcosystemRelease | revision=3.1.0 | range=and after}} ====
    
    {{warning| For {{EcosystemRelease | revision=3.1.0}} use "st,min-sample-time-{{highlight|nsecs}}", instead of "st,min-sample-time-{{highlight|ns}}", to configure the sampling time. }}
    
     &adc {
     	/* ADC block common resources */
     	pinctrl-names = "default";
     	pinctrl-0 = {{highlight|<&adc1_in6_pins_a>}};              {{highlight|/* Use PF12 pin as ANALOG */}}
     	vdda-supply ={{highlight| <&vdda>}};                       {{highlight|/* Example to supply vdda pin by using a PMIC regulator}}
     	vref-supply ={{highlight| <&vrefbuf>}};                    {{highlight|/* Example to use VREFBUF (It needs to be enabled as well) */}}
     	status = "'''okay'''";                             {{highlight|/* Enable ADC12ADC block */}}
     	adc1: adc@0 {	        #address-cells = <1>;
             	#size-cells = <0>;/* private resources for ADC1 */st,adc-channels = {{highlight|status = "'''okay'''";                     {{highlight|/* Enable ADC1 */}}
     		channel@6 {
     			reg = {{highlight|<6>}};               {{highlight|/* ADC1 in6 channel is used */}}	st,min-sample-time-nsecsns = {{highlight|<10000>}};  {{highlight|/* 10µs sampling time */}}status = "'''okay'''";                     {{highlight|/* Enable ADC1 */}}
     	};
     	adc2: adc@100 {
     		/* private resources for ADC2 */
     		...
     	};
     };
    };
     	};
     };
    <div class="mw-collapsible mw-collapsed">
    
    
    ==== For {{EcosystemRelease | revision=3.0.0 | range=and before}} ====<div class="mw-collapsible-content">
    
     &adc {
     	/* ADC common resources */
     	pinctrl-names = "default";
     	pinctrl-0 = {{highlight|<&adc1_in6_pins_a>}};              {{highlight|/* Use PF12 pin as ANALOG */}}
     	vdda-supply ={{highlight| <&vdda>}};                       {{highlight|/* Example to supply vdda pin by using a PMIC regulator}}
     	vref-supply ={{highlight| <&vrefbuf>}};                    {{highlight|/* Example to use VREFBUF (It needs to be enabled as well) */}}
     	status = "'''okay'''";                             {{highlight|/* Enable ADC block */}}
     	adc1: adc@0 {
     		/* private resources for ADC1 */
     		st,adc-channels = {{highlight|<6>}};               {{highlight|/* ADC1 in6 channel is used */}}
     		st,min-sample-time-nsecs = {{highlight|<10000>}};  {{highlight|/* 10µs sampling time */}}
     		status = "'''okay'''";                     {{highlight|/* Enable ADC1 */}}
     	};
     };</div></div>
    
    ==How to configure the DT using STM32CubeMX==
    The [[STM32CubeMX]] tool can be used to configure the STM32MPU device and get the corresponding [[Device_tree#STM32|platform configuration device tree]] files.<br />
    The STM32CubeMX may not support all the properties described in DT binding files listed in the above [[#DT bindings documentation|DT bindings documentation]] paragraph. If so, the tool inserts '''user sections''' in the generated device tree. These sections can then be edited to add some properties, and they are preserved from one generation to another. Refer to [[STM32CubeMX]] user manual for further information.
    
    ==References==
    For additional information, refer to the following links:
    <references />
    
    <noinclude>
    
    {{ArticleBasedOnModel | Peripheral or framework device tree configuration model}}
    {{PublicationRequestId | 8779 | 2018-09-13 |24109|2022-07-27 | previous : 8779 - 2018-09-13 - AnneJ}}
    
    [[Category:Device tree configuration]]
    [[Category:IIO]]
    {{ReviewsComments|JCT 1840: alignment needed with the last version of the model<br>
    
    [[Category:ToBeAlignedWithModel]]
    }}</noinclude>
    (27 intermediate revisions by 6 users not shown)
    Line 1: Line 1:
      +
    <noinclude>{{ApplicableFor
      +
    |MPUs list=STM32MP13x, STM32MP15x
      +
    |MPUs checklist=STM32MP13x,STM32MP15x
      +
    }}</noinclude>
     
    == Article purpose ==
     
    == Article purpose ==
    The purpose of this article is to explain how to configure the analog-to-digital converter (''ADC'')<ref name="ADC internal peripheral">[[ADC internal peripheral]]</ref> ''' when the peripheral is assigned to Linux<sup>&reg;</sup> OS''', and in particular:
    +
    The purpose of this article is to explain how to configure the analog-to-digital converter [[ADC_internal_peripheral|ADC internal peripheral]] using the [[Device tree|device tree]] mechanism, relying on the bindings documentation that is the description of the required and optional device-tree properties.
    * how to configure the ADC '''peripheral'''
     
    * how to configure the '''board''', e.g. the ADC voltage reference regulator, channels, pins and sampling time.
     
       
    The configuration is performed using the '''device tree mechanism'''<ref> [[Device tree]]</ref>.
    +
    The peripheral can be assigned to different contexts/software components, depending on the final product needs. Refer to [[How to assign an internal peripheral to a runtime context]] for guidelines on this configuration.
       
    It is used by the [[ADC Linux driver]] that registers relevant information in [[IIO overview|IIO framework]], such as IIO devices, channels and voltage scale for each ADC.
    +
    The device tree file to configure depends on the context/component to which the ADC peripheral is assigned:
     
    +
    * U-Boot (Boot time context): refer to [[U-Boot_overview|U-Boot overview]].
    If the peripheral is assigned to another execution context, refer to [[How to assign an internal peripheral to a runtime context]] article for guidelines on peripheral assignment and configuration.
    +
    * Linux® OS (Runtime context): refer to the [[IIO overview|IIO framework]].
       
     
    == DT bindings documentation ==
     
    == DT bindings documentation ==
    ''STM32 ADC device tree bindings''<ref>{{CodeSource | Linux kernel | Documentation/devicetree/bindings/iio/adc/st,stm32-adc.yaml | Documentation/devicetree/bindings/iio/adc/st,stm32-adc.yaml}}, STM32 ADC device tree bindings</ref> describe all the required and optional functions.
    +
    U-Boot, Linux® OS: STM32 ADC device tree bindings: {{CodeSource | Linux kernel | Documentation/devicetree/bindings/iio/adc/st,stm32-adc.yaml | Documentation/devicetree/bindings/iio/adc/st,stm32-adc.yaml}}
       
     
    == DT configuration ==
     
    == DT configuration ==
    This hardware description is a combination of STM32 microprocessor and board device tree files. See [[Device tree]] for more explanations on device tree file split.
    +
    This hardware description is a combination of the '''STM32 microprocessor''' device tree files (''.dtsi'' extension) and '''board''' device tree files (''.dts'' extension). See the [[Device tree]] for an explanation of the device tree file split.
       
    The '''STM32CubeMX''' can be used to generate the board device tree. Refer to [[#How_to_configure_the_DT_using_STM32CubeMX|How to configure the DT using STM32CubeMX]] for more details.
    +
    '''STM32CubeMX''' can be used to generate the board device tree. Refer to [[#How_to_configure_the_DT_using_STM32CubeMX|How to configure the DT using STM32CubeMX]] for more details.
     
    {{
     
    {{
     
    ImageMap|Image:ADC DT configuration.png {{!}} frame {{!}} center{{!}} ADC DT configuration
     
    ImageMap|Image:ADC DT configuration.png {{!}} frame {{!}} center{{!}} ADC DT configuration
    rect 233 249 295 287 [[#Common_resources_for_all_ADCs|ADC common resources]]
    +
    rect 237 230 299 265 [[#Common_resources_for_all_ADCs|ADC common resources]]
    rect 333 158 395 197 [[#Common_resources_for_all_ADCs|ADC common resources]]
    +
    rect 341 138 403 174 [[#Common_resources_for_all_ADCs|ADC common resources]]
    rect 511 91 573 130 [[#Common_resources_for_all_ADCs|ADC common resources]]
    +
    rect 525 68 585 105 [[#Common_resources_for_all_ADCs|ADC common resources]]
    rect 333 313 395 352 [[#Private_resources_for_an_ADC|ADC private resources]]
    +
    rect 341 297 403 334 [[#Private_resources_for_an_ADC|ADC private resources]]
    rect 511 283 573 323 [[#Private_resources_for_an_ADC|ADC private resources]]
    +
    rect 525 267 585 301 [[#Private_resources_for_an_ADC|ADC private resources]]
    rect 141 157 227 183 [[Regulator overview|Regulator]]
    +
    rect 145 136 231 159 [[Regulator overview|Regulator]]
    rect 141 269 227 293 [[Pinctrl overview|Pinctrl]]
    +
    rect 145 250 231 272 [[Pinctrl overview|Pinctrl]]
    rect 141 307 227 330 [[How to use the IIO user space interface#How to set up a TIM or LPTIM trigger using the sysfs interface|How to setup TIM or LPTIM triggers]]
    +
    rect 145 288 231 311 [[How to use the IIO user space interface#How to set up a TIM or LPTIM trigger using the sysfs interface|How to setup TIM or LPTIM triggers]]
     
    }}
     
    }}
       
    === DT configuration (STM32 level) ===
    +
    === DT configuration (STM32/SoC level) ===
      +
    An ADC peripheral provides one or more ADCs. Some hardware common resources are managed in the ADC peripheral, whatever the number of ADC sub-blocks. In the following chapters, the ADC peripheral is called "'''ADC block'''", while "'''ADC'''" refers to an ADC peripheral sub-block.
      +
     
      +
    The ADC DT nodes are structured as follows:
      +
    * The '''DT root node''' describes the ADC block parameters such as register areas, clocks and interrupts.
      +
    * The '''DT child nodes''' (''''adc1'''' and/or ''''adc2'''') describe the ADCs independently.
      +
    {{ReviewsComments|-- [[User:Nathalie Sangouard|Nathalie Sangouard]] ([[User talk:Nathalie Sangouard|talk]]) 15:42, 30 May 2022 (CEST)<br />I have changed the ordre MP13 /MP15 because it should be given in alphabetic order : MP13 first}}
      +
    ===={{MicroprocessorDevice | device=13}}====
      +
    The ADC nodes are declared in stm32mp131.dtsi<ref name="stm32mp131_dtsi">{{CodeSource | Linux kernel | arch/arm/boot/dts/stm32mp131.dtsi | STM32MP131 device tree file}}</ref> and in
      +
    stm32mp133.dtsi<ref name="stm32mp133_dtsi">{{CodeSource | Linux kernel | arch/arm/boot/dts/stm32mp133.dtsi | STM32MP133 device tree file}}</ref>.
      +
    adc_1: adc@address {
      +
    compatible = "st,stm32mp13-adc-core";
      +
    ...                                      {{highlight|/* common resources in 'adc_1' root node. */}}
      +
    adc1: adc@0 {
      +
    compatible = "st,stm32mp13-adc";
      +
    ...                              {{highlight|/* private resources in 'adc1' child node. */}}
      +
    };
      +
    };
      +
    adc_2: adc@address {
      +
    compatible = "st,stm32mp13-adc-core";
      +
    ...                                      {{highlight|/* common resources in 'adc_2' root node. */}}
      +
    adc2: adc@0 {
      +
    compatible = "st,stm32mp13-adc";
      +
    ...                              {{highlight|/* private resources in 'adc2' child node. */}}
      +
    };
      +
    };
      +
     
      +
    {{Warning|This device tree part is related to STM32 microprocessors. It must be kept as is, without being modified by the end-user.}}
      +
     
      +
    ===={{MicroprocessorDevice | device=15}}====
      +
     
     
    The ADC nodes are declared in stm32mp151.dtsi<ref name="stm32mp151_dtsi">{{CodeSource | Linux kernel | arch/arm/boot/dts/stm32mp151.dtsi | STM32MP151 device tree file}}</ref>.
     
    The ADC nodes are declared in stm32mp151.dtsi<ref name="stm32mp151_dtsi">{{CodeSource | Linux kernel | arch/arm/boot/dts/stm32mp151.dtsi | STM32MP151 device tree file}}</ref>.
     
    * '''DT root node''' (''''adc'''') describes the ADC hardware block parameters such as register areas, clocks and interrupts.
     
    * '''DT child nodes''' (''''adc1'''' and ''''adc2'''') describe ADC1 and ADC2 independently.
     
     
      adc: adc@address {
     
      adc: adc@address {
     
      compatible = "st,stm32mp1-adc-core";
     
      compatible = "st,stm32mp1-adc-core";
    Line 46: Line 75:
     
      };
     
      };
     
      };
     
      };
    {{Warning|This device tree part is related to STM32 microprocessors. It must be kept as is, without being modified by the end-user.}}
    +
    === DT configuration (board level) ===
       
    === DT configuration (board level) ===
    +
    To configure and enable the ADC on your board, follow the sequences described in the following chapters.
    Follow the sequences described in the below chapters to configure and enable the ADC on your board.
    +
    The objective of this chapter is to explain how to enable and configure the ADC internal peripheral DT nodes for a board.
      +
    This board DT is overloaded on top of the SoC DT. It is usually composed of several DT files: at least one "board" dts file, that includes the "SoC" dtsi file, plus one or several other dts files.  
       
     
    ==== Common resources for all ADCs ====
     
    ==== Common resources for all ADCs ====
    The '''DT root node''' (''''adc'''') must be filled in:
    +
     
      +
    The '''DT root node''' must be filled in:
     
    * Enable the ADC block by setting '''status = "okay".'''
     
    * Enable the ADC block by setting '''status = "okay".'''
     
    * Configure the pins in use via [[Pinctrl overview|pinctrl]], through '''pinctrl-0''' and '''pinctrl-names'''.
     
    * Configure the pins in use via [[Pinctrl overview|pinctrl]], through '''pinctrl-0''' and '''pinctrl-names'''.
     
    * Configure the analog supply voltage regulator<ref name="Regulator overview">[[Regulator overview]]</ref> by setting '''vdda-supply = <&your_vdda_regulator>'''.
     
    * Configure the analog supply voltage regulator<ref name="Regulator overview">[[Regulator overview]]</ref> by setting '''vdda-supply = <&your_vdda_regulator>'''.
     
    * Configure the analog reference voltage regulator<ref name="Regulator overview">[[Regulator overview]]</ref> by setting '''vref-supply = <&your_vref_regulator>'''.
     
    * Configure the analog reference voltage regulator<ref name="Regulator overview">[[Regulator overview]]</ref> by setting '''vref-supply = <&your_vref_regulator>'''.
    {{info|The ADC can use the internal VREFBUF<ref name="VREFBUF internal peripheral">[[VREFBUF internal peripheral]]</ref> or any other external regulator<ref name="Regulator overview"/> wired to VREF+ pin.}}
    +
    {{info|The ADC block can use the internal VREFBUF<ref name="VREFBUF internal peripheral">[[VREFBUF internal peripheral]]</ref> or any other external regulator<ref name="Regulator overview"/> wired to VREF+ pin.}}
       
    ==== Resources dedicated to ADC1 and ADC2====
    +
    ==== Resources dedicated to an ADC ====
    The '''DT child nodes''' (''''adc1'''' and/or ''''adc2'''') must be filled in:
    +
    The '''DT child nodes''' (''''adc1'''' and ''''adc2'''') must be filled in with the ADC and channels properties.
    * Enable 'adc1' and/or 'adc2' by setting '''status = "okay".'''
    +
     
      +
    ===== ADC properties =====
      +
    * Enable ADC by setting '''status = "okay".'''
      +
    * Set ADC resolution by setting '''assigned-resolution-bits = <12>''' (optional).
      +
    ===== Channel properties (for {{EcosystemRelease | revision=3.1.0 | range=and after}}) =====
      +
    Each channel is described by a dedicated '''DT sub node''', filled in with properties documented in
      +
    ''Generic IIO bindings for ADC channels''<ref>{{CodeSource | Linux kernel | Documentation/devicetree/bindings/iio/adc/adc.yaml | Documentation/devicetree/bindings/iio/adc/adc.yaml}}, Generic IIO bindings for ADC channels</ref>.
      +
    * Define channel index 'x' by setting '''reg = <x>'''.
      +
    * Identifiy the channel by setting its name with '''label = "xxx"''' (optional). <BR>Reserved labels allow to identify the internal channels.
      +
    ** "'''vrefint'''", "'''vddcore'''": These internal channels are defined in the SoC DT. They can be removed in the board DT if they are not used.
      +
    ** "'''vbat'''": This internal channel can be defined in the board DT. It is not defined by default in the SoC DT to save current consumption.
      +
    * Enable differential channel pair (''<vinp vinn>'') by setting '''diff-channels = <x y>''' (optional).
      +
    * Set the minimum sampling time (optional):
      +
    ** by setting '''st,min-sample-time-ns = <10000>''', for {{EcosystemRelease | revision=4.0.0 | range=and after}}
      +
    ** by setting '''st,min-sample-time-nsecs = <10000>''', for {{EcosystemRelease | revision=3.1.0}}
      +
     
      +
    <div class="mw-collapsible mw-collapsed">
      +
    =====  Legacy channel properties (for {{EcosystemRelease | revision=3.0.0 | range=and before}}) =====
      +
    <div class="mw-collapsible-content">
      +
    These properties are deprecated, but are still supported for backward compatibility.
      +
    The properties introduced in v3.1.0 should be used instead, whenever possible.
     
    * Enable single-ended channel(s) (''<vinp...>'') by setting '''st,adc-channels = <0 1 2...>.'''
     
    * Enable single-ended channel(s) (''<vinp...>'') by setting '''st,adc-channels = <0 1 2...>.'''
     
    * Enable differential channel(s) pairs (''<vinp vinn>, ...'') by setting '''st,adc-diff-channels = <1 0>, <2 6>, ....'''
     
    * Enable differential channel(s) pairs (''<vinp vinn>, ...'') by setting '''st,adc-diff-channels = <1 0>, <2 6>, ....'''
     
    * Set the minimum sampling time <ref>[http://www.st.com/content/ccc/resource/technical/document/application_note/group0/3f/4c/a4/82/bd/63/4e/92/CD00211314/files/CD00211314.pdf/jcr:content/translations/en.CD00211314.pdf How to get the best ADC accuracy in STM32], by STMicroelectronics</ref> for each or all channels by setting '''st,min-sample-time-nsecs = <10000>''' (optional).
     
    * Set the minimum sampling time <ref>[http://www.st.com/content/ccc/resource/technical/document/application_note/group0/3f/4c/a4/82/bd/63/4e/92/CD00211314/files/CD00211314.pdf/jcr:content/translations/en.CD00211314.pdf How to get the best ADC accuracy in STM32], by STMicroelectronics</ref> for each or all channels by setting '''st,min-sample-time-nsecs = <10000>''' (optional).
    * Set the resolution by setting '''assigned-resolution-bits = <12>''' (optional).
    +
    </div></div>
       
    === DT configuration example ===
    +
    === DT configuration examples ===
     
    The example below shows how to configure ADC1:
     
    The example below shows how to configure ADC1:
     
    * Input pin: use [[Pinctrl device tree configuration]] to configure PF12 as analog input.
     
    * Input pin: use [[Pinctrl device tree configuration]] to configure PF12 as analog input.
    Line 82: Line 133:
     
      };
     
      };
       
      +
    ==== For {{EcosystemRelease | revision=3.1.0 | range=and after}} ====
      +
      +
    {{warning| For {{EcosystemRelease | revision=3.1.0}} use "st,min-sample-time-{{highlight|nsecs}}", instead of "st,min-sample-time-{{highlight|ns}}", to configure the sampling time. }}
      +
      +
    &adc {
      +
    /* ADC block common resources */
      +
    pinctrl-names = "default";
      +
    pinctrl-0 = {{highlight|<&adc1_in6_pins_a>}};              {{highlight|/* Use PF12 pin as ANALOG */}}
      +
    vdda-supply ={{highlight| <&vdda>}};                      {{highlight|/* Example to supply vdda pin by using a PMIC regulator}}
      +
    vref-supply ={{highlight| <&vrefbuf>}};                    {{highlight|/* Example to use VREFBUF (It needs to be enabled as well) */}}
      +
    status = "'''okay'''";                            {{highlight|/* Enable ADC block */}}
      +
    adc1: adc@0 {
      +
            #address-cells = <1>;
      +
            #size-cells = <0>;
      +
    /* private resources for ADC1 */
      +
    status = "'''okay'''";                    {{highlight|/* Enable ADC1 */}}
      +
    channel@6 {
      +
    reg = {{highlight|<6>}};              {{highlight|/* ADC1 in6 channel is used */}}
      +
    st,min-sample-time-ns = {{highlight|<10000>}};  {{highlight|/* 10µs sampling time */}}
      +
    };
      +
    };
      +
    };
      +
      +
    <div class="mw-collapsible mw-collapsed">
      +
      +
    ==== For {{EcosystemRelease | revision=3.0.0 | range=and before}} ====
      +
    <div class="mw-collapsible-content">
     
      &adc {
     
      &adc {
      /* ADC1 & ADC2 common resources */
    +
      /* ADC common resources */
     
      pinctrl-names = "default";
     
      pinctrl-names = "default";
     
      pinctrl-0 = {{highlight|<&adc1_in6_pins_a>}};              {{highlight|/* Use PF12 pin as ANALOG */}}
     
      pinctrl-0 = {{highlight|<&adc1_in6_pins_a>}};              {{highlight|/* Use PF12 pin as ANALOG */}}
     
      vdda-supply ={{highlight| <&vdda>}};                      {{highlight|/* Example to supply vdda pin by using a PMIC regulator}}
     
      vdda-supply ={{highlight| <&vdda>}};                      {{highlight|/* Example to supply vdda pin by using a PMIC regulator}}
     
      vref-supply ={{highlight| <&vrefbuf>}};                    {{highlight|/* Example to use VREFBUF (It needs to be enabled as well) */}}
     
      vref-supply ={{highlight| <&vrefbuf>}};                    {{highlight|/* Example to use VREFBUF (It needs to be enabled as well) */}}
      status = "'''okay'''";                            {{highlight|/* Enable ADC12 block */}}
    +
      status = "'''okay'''";                            {{highlight|/* Enable ADC block */}}
     
      adc1: adc@0 {
     
      adc1: adc@0 {
     
      /* private resources for ADC1 */
     
      /* private resources for ADC1 */
    Line 94: Line 172:
     
      st,min-sample-time-nsecs = {{highlight|<10000>}};  {{highlight|/* 10µs sampling time */}}
     
      st,min-sample-time-nsecs = {{highlight|<10000>}};  {{highlight|/* 10µs sampling time */}}
     
      status = "'''okay'''";                    {{highlight|/* Enable ADC1 */}}
     
      status = "'''okay'''";                    {{highlight|/* Enable ADC1 */}}
    };
     
    adc2: adc@100 {
     
    /* private resources for ADC2 */
     
    ...
     
     
      };
     
      };
     
      };
     
      };
      +
    </div></div>
       
     
    ==How to configure the DT using STM32CubeMX==
     
    ==How to configure the DT using STM32CubeMX==
     
    The [[STM32CubeMX]] tool can be used to configure the STM32MPU device and get the corresponding [[Device_tree#STM32|platform configuration device tree]] files.<br />
     
    The [[STM32CubeMX]] tool can be used to configure the STM32MPU device and get the corresponding [[Device_tree#STM32|platform configuration device tree]] files.<br />
    The STM32CubeMX may not support all the properties described in the above [[#DT bindings documentation|DT bindings documentation]] paragraph. If so, the tool inserts '''user sections''' in the generated device tree. These sections can then be edited to add some properties and they are preserved from one generation to another. Refer to [[STM32CubeMX]] user manual for further information.
    +
    STM32CubeMX may not support all the properties described in DT binding files listed in the above [[#DT bindings documentation|DT bindings documentation]] paragraph. If so, the tool inserts '''user sections''' in the generated device tree. These sections can then be edited to add some properties, and they are preserved from one generation to another. Refer to [[STM32CubeMX]] user manual for further information.
       
     
    ==References==
     
    ==References==
    Line 112: Line 187:
     
    <noinclude>
     
    <noinclude>
     
    {{ArticleBasedOnModel | Peripheral or framework device tree configuration model}}
     
    {{ArticleBasedOnModel | Peripheral or framework device tree configuration model}}
    {{PublicationRequestId | 8779 | 2018-09-13 | AnneJ}}
    +
    {{PublicationRequestId | 24109|2022-07-27 | previous : 8779 - 2018-09-13 - AnneJ}}
       
     
    [[Category:Device tree configuration]]
     
    [[Category:Device tree configuration]]
     
    [[Category:IIO]]
     
    [[Category:IIO]]
       
    {{ReviewsComments|JCT 1840: alignment needed with the last version of the model<br>
     
    [[Category:ToBeAlignedWithModel]]
     
    }}
     
     
    </noinclude>
     
    </noinclude>