162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * This file is part of STM32 ADC driver
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
662306a36Sopenharmony_ci * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#ifndef __STM32_ADC_H
1162306a36Sopenharmony_ci#define __STM32_ADC_H
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci/*
1462306a36Sopenharmony_ci * STM32 - ADC global register map
1562306a36Sopenharmony_ci * ________________________________________________________
1662306a36Sopenharmony_ci * | Offset |                 Register                    |
1762306a36Sopenharmony_ci * --------------------------------------------------------
1862306a36Sopenharmony_ci * | 0x000  |                Master ADC1                  |
1962306a36Sopenharmony_ci * --------------------------------------------------------
2062306a36Sopenharmony_ci * | 0x100  |                Slave ADC2                   |
2162306a36Sopenharmony_ci * --------------------------------------------------------
2262306a36Sopenharmony_ci * | 0x200  |                Slave ADC3                   |
2362306a36Sopenharmony_ci * --------------------------------------------------------
2462306a36Sopenharmony_ci * | 0x300  |         Master & Slave common regs          |
2562306a36Sopenharmony_ci * --------------------------------------------------------
2662306a36Sopenharmony_ci */
2762306a36Sopenharmony_ci/* Maximum ADC instances number per ADC block for all supported SoCs */
2862306a36Sopenharmony_ci#define STM32_ADC_MAX_ADCS		3
2962306a36Sopenharmony_ci#define STM32_ADC_OFFSET		0x100
3062306a36Sopenharmony_ci#define STM32_ADCX_COMN_OFFSET		0x300
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci/* STM32F4 - Registers for each ADC instance */
3362306a36Sopenharmony_ci#define STM32F4_ADC_SR			0x00
3462306a36Sopenharmony_ci#define STM32F4_ADC_CR1			0x04
3562306a36Sopenharmony_ci#define STM32F4_ADC_CR2			0x08
3662306a36Sopenharmony_ci#define STM32F4_ADC_SMPR1		0x0C
3762306a36Sopenharmony_ci#define STM32F4_ADC_SMPR2		0x10
3862306a36Sopenharmony_ci#define STM32F4_ADC_HTR			0x24
3962306a36Sopenharmony_ci#define STM32F4_ADC_LTR			0x28
4062306a36Sopenharmony_ci#define STM32F4_ADC_SQR1		0x2C
4162306a36Sopenharmony_ci#define STM32F4_ADC_SQR2		0x30
4262306a36Sopenharmony_ci#define STM32F4_ADC_SQR3		0x34
4362306a36Sopenharmony_ci#define STM32F4_ADC_JSQR		0x38
4462306a36Sopenharmony_ci#define STM32F4_ADC_JDR1		0x3C
4562306a36Sopenharmony_ci#define STM32F4_ADC_JDR2		0x40
4662306a36Sopenharmony_ci#define STM32F4_ADC_JDR3		0x44
4762306a36Sopenharmony_ci#define STM32F4_ADC_JDR4		0x48
4862306a36Sopenharmony_ci#define STM32F4_ADC_DR			0x4C
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci/* STM32F4 - common registers for all ADC instances: 1, 2 & 3 */
5162306a36Sopenharmony_ci#define STM32F4_ADC_CSR			(STM32_ADCX_COMN_OFFSET + 0x00)
5262306a36Sopenharmony_ci#define STM32F4_ADC_CCR			(STM32_ADCX_COMN_OFFSET + 0x04)
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci/* STM32F4_ADC_SR - bit fields */
5562306a36Sopenharmony_ci#define STM32F4_OVR			BIT(5)
5662306a36Sopenharmony_ci#define STM32F4_STRT			BIT(4)
5762306a36Sopenharmony_ci#define STM32F4_EOC			BIT(1)
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci/* STM32F4_ADC_CR1 - bit fields */
6062306a36Sopenharmony_ci#define STM32F4_OVRIE			BIT(26)
6162306a36Sopenharmony_ci#define STM32F4_RES_SHIFT		24
6262306a36Sopenharmony_ci#define STM32F4_RES_MASK		GENMASK(25, 24)
6362306a36Sopenharmony_ci#define STM32F4_SCAN			BIT(8)
6462306a36Sopenharmony_ci#define STM32F4_EOCIE			BIT(5)
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci/* STM32F4_ADC_CR2 - bit fields */
6762306a36Sopenharmony_ci#define STM32F4_SWSTART			BIT(30)
6862306a36Sopenharmony_ci#define STM32F4_EXTEN_SHIFT		28
6962306a36Sopenharmony_ci#define STM32F4_EXTEN_MASK		GENMASK(29, 28)
7062306a36Sopenharmony_ci#define STM32F4_EXTSEL_SHIFT		24
7162306a36Sopenharmony_ci#define STM32F4_EXTSEL_MASK		GENMASK(27, 24)
7262306a36Sopenharmony_ci#define STM32F4_EOCS			BIT(10)
7362306a36Sopenharmony_ci#define STM32F4_DDS			BIT(9)
7462306a36Sopenharmony_ci#define STM32F4_DMA			BIT(8)
7562306a36Sopenharmony_ci#define STM32F4_ADON			BIT(0)
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci/* STM32F4_ADC_CSR - bit fields */
7862306a36Sopenharmony_ci#define STM32F4_OVR3			BIT(21)
7962306a36Sopenharmony_ci#define STM32F4_EOC3			BIT(17)
8062306a36Sopenharmony_ci#define STM32F4_OVR2			BIT(13)
8162306a36Sopenharmony_ci#define STM32F4_EOC2			BIT(9)
8262306a36Sopenharmony_ci#define STM32F4_OVR1			BIT(5)
8362306a36Sopenharmony_ci#define STM32F4_EOC1			BIT(1)
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci/* STM32F4_ADC_CCR - bit fields */
8662306a36Sopenharmony_ci#define STM32F4_ADC_ADCPRE_SHIFT	16
8762306a36Sopenharmony_ci#define STM32F4_ADC_ADCPRE_MASK		GENMASK(17, 16)
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci/* STM32H7 - Registers for each ADC instance */
9062306a36Sopenharmony_ci#define STM32H7_ADC_ISR			0x00
9162306a36Sopenharmony_ci#define STM32H7_ADC_IER			0x04
9262306a36Sopenharmony_ci#define STM32H7_ADC_CR			0x08
9362306a36Sopenharmony_ci#define STM32H7_ADC_CFGR		0x0C
9462306a36Sopenharmony_ci#define STM32H7_ADC_SMPR1		0x14
9562306a36Sopenharmony_ci#define STM32H7_ADC_SMPR2		0x18
9662306a36Sopenharmony_ci#define STM32H7_ADC_PCSEL		0x1C
9762306a36Sopenharmony_ci#define STM32H7_ADC_SQR1		0x30
9862306a36Sopenharmony_ci#define STM32H7_ADC_SQR2		0x34
9962306a36Sopenharmony_ci#define STM32H7_ADC_SQR3		0x38
10062306a36Sopenharmony_ci#define STM32H7_ADC_SQR4		0x3C
10162306a36Sopenharmony_ci#define STM32H7_ADC_DR			0x40
10262306a36Sopenharmony_ci#define STM32H7_ADC_DIFSEL		0xC0
10362306a36Sopenharmony_ci#define STM32H7_ADC_CALFACT		0xC4
10462306a36Sopenharmony_ci#define STM32H7_ADC_CALFACT2		0xC8
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci/* STM32MP1 - ADC2 instance option register */
10762306a36Sopenharmony_ci#define STM32MP1_ADC2_OR		0xD0
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci/* STM32MP1 - Identification registers */
11062306a36Sopenharmony_ci#define STM32MP1_ADC_HWCFGR0		0x3F0
11162306a36Sopenharmony_ci#define STM32MP1_ADC_VERR		0x3F4
11262306a36Sopenharmony_ci#define STM32MP1_ADC_IPDR		0x3F8
11362306a36Sopenharmony_ci#define STM32MP1_ADC_SIDR		0x3FC
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci/* STM32MP13 - Registers for each ADC instance */
11662306a36Sopenharmony_ci#define STM32MP13_ADC_DIFSEL		0xB0
11762306a36Sopenharmony_ci#define STM32MP13_ADC_CALFACT		0xB4
11862306a36Sopenharmony_ci#define STM32MP13_ADC2_OR		0xC8
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci/* STM32H7 - common registers for all ADC instances */
12162306a36Sopenharmony_ci#define STM32H7_ADC_CSR			(STM32_ADCX_COMN_OFFSET + 0x00)
12262306a36Sopenharmony_ci#define STM32H7_ADC_CCR			(STM32_ADCX_COMN_OFFSET + 0x08)
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci/* STM32H7_ADC_ISR - bit fields */
12562306a36Sopenharmony_ci#define STM32MP1_VREGREADY		BIT(12)
12662306a36Sopenharmony_ci#define STM32H7_OVR			BIT(4)
12762306a36Sopenharmony_ci#define STM32H7_EOC			BIT(2)
12862306a36Sopenharmony_ci#define STM32H7_ADRDY			BIT(0)
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci/* STM32H7_ADC_IER - bit fields */
13162306a36Sopenharmony_ci#define STM32H7_OVRIE			STM32H7_OVR
13262306a36Sopenharmony_ci#define STM32H7_EOCIE			STM32H7_EOC
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci/* STM32H7_ADC_CR - bit fields */
13562306a36Sopenharmony_ci#define STM32H7_ADCAL			BIT(31)
13662306a36Sopenharmony_ci#define STM32H7_ADCALDIF		BIT(30)
13762306a36Sopenharmony_ci#define STM32H7_DEEPPWD			BIT(29)
13862306a36Sopenharmony_ci#define STM32H7_ADVREGEN		BIT(28)
13962306a36Sopenharmony_ci#define STM32H7_LINCALRDYW6		BIT(27)
14062306a36Sopenharmony_ci#define STM32H7_LINCALRDYW5		BIT(26)
14162306a36Sopenharmony_ci#define STM32H7_LINCALRDYW4		BIT(25)
14262306a36Sopenharmony_ci#define STM32H7_LINCALRDYW3		BIT(24)
14362306a36Sopenharmony_ci#define STM32H7_LINCALRDYW2		BIT(23)
14462306a36Sopenharmony_ci#define STM32H7_LINCALRDYW1		BIT(22)
14562306a36Sopenharmony_ci#define STM32H7_LINCALRDYW_MASK		GENMASK(27, 22)
14662306a36Sopenharmony_ci#define STM32H7_ADCALLIN		BIT(16)
14762306a36Sopenharmony_ci#define STM32H7_BOOST			BIT(8)
14862306a36Sopenharmony_ci#define STM32H7_ADSTP			BIT(4)
14962306a36Sopenharmony_ci#define STM32H7_ADSTART			BIT(2)
15062306a36Sopenharmony_ci#define STM32H7_ADDIS			BIT(1)
15162306a36Sopenharmony_ci#define STM32H7_ADEN			BIT(0)
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci/* STM32H7_ADC_CFGR bit fields */
15462306a36Sopenharmony_ci#define STM32H7_EXTEN_SHIFT		10
15562306a36Sopenharmony_ci#define STM32H7_EXTEN_MASK		GENMASK(11, 10)
15662306a36Sopenharmony_ci#define STM32H7_EXTSEL_SHIFT		5
15762306a36Sopenharmony_ci#define STM32H7_EXTSEL_MASK		GENMASK(9, 5)
15862306a36Sopenharmony_ci#define STM32H7_RES_SHIFT		2
15962306a36Sopenharmony_ci#define STM32H7_RES_MASK		GENMASK(4, 2)
16062306a36Sopenharmony_ci#define STM32H7_DMNGT_SHIFT		0
16162306a36Sopenharmony_ci#define STM32H7_DMNGT_MASK		GENMASK(1, 0)
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_cienum stm32h7_adc_dmngt {
16462306a36Sopenharmony_ci	STM32H7_DMNGT_DR_ONLY,		/* Regular data in DR only */
16562306a36Sopenharmony_ci	STM32H7_DMNGT_DMA_ONESHOT,	/* DMA one shot mode */
16662306a36Sopenharmony_ci	STM32H7_DMNGT_DFSDM,		/* DFSDM mode */
16762306a36Sopenharmony_ci	STM32H7_DMNGT_DMA_CIRC,		/* DMA circular mode */
16862306a36Sopenharmony_ci};
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci/* STM32H7_ADC_DIFSEL - bit fields */
17162306a36Sopenharmony_ci#define STM32H7_DIFSEL_MASK		GENMASK(19, 0)
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci/* STM32H7_ADC_CALFACT - bit fields */
17462306a36Sopenharmony_ci#define STM32H7_CALFACT_D_SHIFT		16
17562306a36Sopenharmony_ci#define STM32H7_CALFACT_D_MASK		GENMASK(26, 16)
17662306a36Sopenharmony_ci#define STM32H7_CALFACT_S_SHIFT		0
17762306a36Sopenharmony_ci#define STM32H7_CALFACT_S_MASK		GENMASK(10, 0)
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci/* STM32H7_ADC_CALFACT2 - bit fields */
18062306a36Sopenharmony_ci#define STM32H7_LINCALFACT_SHIFT	0
18162306a36Sopenharmony_ci#define STM32H7_LINCALFACT_MASK		GENMASK(29, 0)
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci/* STM32H7_ADC_CSR - bit fields */
18462306a36Sopenharmony_ci#define STM32H7_OVR_SLV			BIT(20)
18562306a36Sopenharmony_ci#define STM32H7_EOC_SLV			BIT(18)
18662306a36Sopenharmony_ci#define STM32H7_OVR_MST			BIT(4)
18762306a36Sopenharmony_ci#define STM32H7_EOC_MST			BIT(2)
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci/* STM32H7_ADC_CCR - bit fields */
19062306a36Sopenharmony_ci#define STM32H7_VBATEN			BIT(24)
19162306a36Sopenharmony_ci#define STM32H7_VREFEN			BIT(22)
19262306a36Sopenharmony_ci#define STM32H7_PRESC_SHIFT		18
19362306a36Sopenharmony_ci#define STM32H7_PRESC_MASK		GENMASK(21, 18)
19462306a36Sopenharmony_ci#define STM32H7_CKMODE_SHIFT		16
19562306a36Sopenharmony_ci#define STM32H7_CKMODE_MASK		GENMASK(17, 16)
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci/* STM32MP1_ADC2_OR - bit fields */
19862306a36Sopenharmony_ci#define STM32MP1_VDDCOREEN		BIT(0)
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci/* STM32MP1_ADC_HWCFGR0 - bit fields */
20162306a36Sopenharmony_ci#define STM32MP1_ADCNUM_SHIFT		0
20262306a36Sopenharmony_ci#define STM32MP1_ADCNUM_MASK		GENMASK(3, 0)
20362306a36Sopenharmony_ci#define STM32MP1_MULPIPE_SHIFT		4
20462306a36Sopenharmony_ci#define STM32MP1_MULPIPE_MASK		GENMASK(7, 4)
20562306a36Sopenharmony_ci#define STM32MP1_OPBITS_SHIFT		8
20662306a36Sopenharmony_ci#define STM32MP1_OPBITS_MASK		GENMASK(11, 8)
20762306a36Sopenharmony_ci#define STM32MP1_IDLEVALUE_SHIFT	12
20862306a36Sopenharmony_ci#define STM32MP1_IDLEVALUE_MASK	GENMASK(15, 12)
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci/* STM32MP1_ADC_VERR - bit fields */
21162306a36Sopenharmony_ci#define STM32MP1_MINREV_SHIFT		0
21262306a36Sopenharmony_ci#define STM32MP1_MINREV_MASK		GENMASK(3, 0)
21362306a36Sopenharmony_ci#define STM32MP1_MAJREV_SHIFT		4
21462306a36Sopenharmony_ci#define STM32MP1_MAJREV_MASK		GENMASK(7, 4)
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci/* STM32MP1_ADC_IPDR - bit fields */
21762306a36Sopenharmony_ci#define STM32MP1_IPIDR_MASK		GENMASK(31, 0)
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci/* STM32MP1_ADC_SIDR - bit fields */
22062306a36Sopenharmony_ci#define STM32MP1_SIDR_MASK		GENMASK(31, 0)
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci/* STM32MP13_ADC_CFGR specific bit fields */
22362306a36Sopenharmony_ci#define STM32MP13_DMAEN			BIT(0)
22462306a36Sopenharmony_ci#define STM32MP13_DMACFG		BIT(1)
22562306a36Sopenharmony_ci#define STM32MP13_DFSDMCFG		BIT(2)
22662306a36Sopenharmony_ci#define STM32MP13_RES_SHIFT		3
22762306a36Sopenharmony_ci#define STM32MP13_RES_MASK		GENMASK(4, 3)
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci/* STM32MP13_ADC_DIFSEL - bit fields */
23062306a36Sopenharmony_ci#define STM32MP13_DIFSEL_MASK		GENMASK(18, 0)
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci/* STM32MP13_ADC_CALFACT - bit fields */
23362306a36Sopenharmony_ci#define STM32MP13_CALFACT_D_SHIFT	16
23462306a36Sopenharmony_ci#define STM32MP13_CALFACT_D_MASK	GENMASK(22, 16)
23562306a36Sopenharmony_ci#define STM32MP13_CALFACT_S_SHIFT	0
23662306a36Sopenharmony_ci#define STM32MP13_CALFACT_S_MASK	GENMASK(6, 0)
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci/* STM32MP13_ADC2_OR - bit fields */
23962306a36Sopenharmony_ci#define STM32MP13_OP2			BIT(2)
24062306a36Sopenharmony_ci#define STM32MP13_OP1			BIT(1)
24162306a36Sopenharmony_ci#define STM32MP13_OP0			BIT(0)
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci#define STM32MP15_IPIDR_NUMBER		0x00110005
24462306a36Sopenharmony_ci#define STM32MP13_IPIDR_NUMBER		0x00110006
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci/**
24762306a36Sopenharmony_ci * struct stm32_adc_common - stm32 ADC driver common data (for all instances)
24862306a36Sopenharmony_ci * @base:		control registers base cpu addr
24962306a36Sopenharmony_ci * @phys_base:		control registers base physical addr
25062306a36Sopenharmony_ci * @rate:		clock rate used for analog circuitry
25162306a36Sopenharmony_ci * @vref_mv:		vref voltage (mv)
25262306a36Sopenharmony_ci * @lock:		spinlock
25362306a36Sopenharmony_ci */
25462306a36Sopenharmony_cistruct stm32_adc_common {
25562306a36Sopenharmony_ci	void __iomem			*base;
25662306a36Sopenharmony_ci	phys_addr_t			phys_base;
25762306a36Sopenharmony_ci	unsigned long			rate;
25862306a36Sopenharmony_ci	int				vref_mv;
25962306a36Sopenharmony_ci	spinlock_t			lock;		/* lock for common register */
26062306a36Sopenharmony_ci};
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci#endif
263