xref: /linux/sound/soc/stm/stm32_sai.h (revision 1802d0beecafe581ad584634ba92f8a471d8a63a)
1*1802d0beSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
23e086edfSolivier moysan /*
33e086edfSolivier moysan  * STM32 ALSA SoC Digital Audio Interface (SAI) driver.
43e086edfSolivier moysan  *
53e086edfSolivier moysan  * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
63e086edfSolivier moysan  * Author(s): Olivier Moysan <olivier.moysan@st.com> for STMicroelectronics.
73e086edfSolivier moysan  */
83e086edfSolivier moysan 
95914d285SOlivier Moysan #include <linux/bitfield.h>
105914d285SOlivier Moysan 
113e086edfSolivier moysan /******************** SAI Register Map **************************************/
123e086edfSolivier moysan 
135914d285SOlivier Moysan /* Global configuration register */
143e086edfSolivier moysan #define STM_SAI_GCR		0x00
153e086edfSolivier moysan 
163e086edfSolivier moysan /* Sub-block A&B registers offsets, relative to A&B sub-block addresses */
173e086edfSolivier moysan #define STM_SAI_CR1_REGX	0x00	/* A offset: 0x04. B offset: 0x24 */
183e086edfSolivier moysan #define STM_SAI_CR2_REGX	0x04
193e086edfSolivier moysan #define STM_SAI_FRCR_REGX	0x08
203e086edfSolivier moysan #define STM_SAI_SLOTR_REGX	0x0C
213e086edfSolivier moysan #define STM_SAI_IMR_REGX	0x10
223e086edfSolivier moysan #define STM_SAI_SR_REGX		0x14
233e086edfSolivier moysan #define STM_SAI_CLRFR_REGX	0x18
243e086edfSolivier moysan #define STM_SAI_DR_REGX		0x1C
253e086edfSolivier moysan 
2603e78a24Solivier moysan /* Sub-block A registers, relative to sub-block A address */
2703e78a24Solivier moysan #define STM_SAI_PDMCR_REGX	0x40
2803e78a24Solivier moysan #define STM_SAI_PDMLY_REGX	0x44
2903e78a24Solivier moysan 
303e086edfSolivier moysan /******************** Bit definition for SAI_GCR register *******************/
313e086edfSolivier moysan #define SAI_GCR_SYNCIN_SHIFT	0
325914d285SOlivier Moysan #define SAI_GCR_SYNCIN_WDTH	2
333e086edfSolivier moysan #define SAI_GCR_SYNCIN_MASK	GENMASK(1, SAI_GCR_SYNCIN_SHIFT)
345914d285SOlivier Moysan #define SAI_GCR_SYNCIN_MAX	FIELD_GET(SAI_GCR_SYNCIN_MASK,\
355914d285SOlivier Moysan 				SAI_GCR_SYNCIN_MASK)
363e086edfSolivier moysan 
373e086edfSolivier moysan #define SAI_GCR_SYNCOUT_SHIFT	4
383e086edfSolivier moysan #define SAI_GCR_SYNCOUT_MASK	GENMASK(5, SAI_GCR_SYNCOUT_SHIFT)
393e086edfSolivier moysan 
403e086edfSolivier moysan /******************* Bit definition for SAI_XCR1 register *******************/
413e086edfSolivier moysan #define SAI_XCR1_RX_TX_SHIFT	0
423e086edfSolivier moysan #define SAI_XCR1_RX_TX		BIT(SAI_XCR1_RX_TX_SHIFT)
433e086edfSolivier moysan #define SAI_XCR1_SLAVE_SHIFT	1
443e086edfSolivier moysan #define SAI_XCR1_SLAVE		BIT(SAI_XCR1_SLAVE_SHIFT)
453e086edfSolivier moysan 
463e086edfSolivier moysan #define SAI_XCR1_PRTCFG_SHIFT	2
473e086edfSolivier moysan #define SAI_XCR1_PRTCFG_MASK	GENMASK(3, SAI_XCR1_PRTCFG_SHIFT)
483e086edfSolivier moysan #define SAI_XCR1_PRTCFG_SET(x)	((x) << SAI_XCR1_PRTCFG_SHIFT)
493e086edfSolivier moysan 
503e086edfSolivier moysan #define SAI_XCR1_DS_SHIFT	5
513e086edfSolivier moysan #define SAI_XCR1_DS_MASK	GENMASK(7, SAI_XCR1_DS_SHIFT)
523e086edfSolivier moysan #define SAI_XCR1_DS_SET(x)	((x) << SAI_XCR1_DS_SHIFT)
533e086edfSolivier moysan 
543e086edfSolivier moysan #define SAI_XCR1_LSBFIRST_SHIFT	8
553e086edfSolivier moysan #define SAI_XCR1_LSBFIRST	BIT(SAI_XCR1_LSBFIRST_SHIFT)
563e086edfSolivier moysan #define SAI_XCR1_CKSTR_SHIFT	9
573e086edfSolivier moysan #define SAI_XCR1_CKSTR		BIT(SAI_XCR1_CKSTR_SHIFT)
583e086edfSolivier moysan 
593e086edfSolivier moysan #define SAI_XCR1_SYNCEN_SHIFT	10
603e086edfSolivier moysan #define SAI_XCR1_SYNCEN_MASK	GENMASK(11, SAI_XCR1_SYNCEN_SHIFT)
613e086edfSolivier moysan #define SAI_XCR1_SYNCEN_SET(x)	((x) << SAI_XCR1_SYNCEN_SHIFT)
623e086edfSolivier moysan 
633e086edfSolivier moysan #define SAI_XCR1_MONO_SHIFT	12
643e086edfSolivier moysan #define SAI_XCR1_MONO		BIT(SAI_XCR1_MONO_SHIFT)
653e086edfSolivier moysan #define SAI_XCR1_OUTDRIV_SHIFT	13
663e086edfSolivier moysan #define SAI_XCR1_OUTDRIV	BIT(SAI_XCR1_OUTDRIV_SHIFT)
673e086edfSolivier moysan #define SAI_XCR1_SAIEN_SHIFT	16
683e086edfSolivier moysan #define SAI_XCR1_SAIEN		BIT(SAI_XCR1_SAIEN_SHIFT)
693e086edfSolivier moysan #define SAI_XCR1_DMAEN_SHIFT	17
703e086edfSolivier moysan #define SAI_XCR1_DMAEN		BIT(SAI_XCR1_DMAEN_SHIFT)
713e086edfSolivier moysan #define SAI_XCR1_NODIV_SHIFT	19
723e086edfSolivier moysan #define SAI_XCR1_NODIV		BIT(SAI_XCR1_NODIV_SHIFT)
733e086edfSolivier moysan 
743e086edfSolivier moysan #define SAI_XCR1_MCKDIV_SHIFT	20
7503e78a24Solivier moysan #define SAI_XCR1_MCKDIV_WIDTH(x)	(((x) == SAI_STM32F4) ? 4 : 6)
7603e78a24Solivier moysan #define SAI_XCR1_MCKDIV_MASK(x) GENMASK((SAI_XCR1_MCKDIV_SHIFT + (x) - 1),\
7703e78a24Solivier moysan 				SAI_XCR1_MCKDIV_SHIFT)
783e086edfSolivier moysan #define SAI_XCR1_MCKDIV_SET(x)	((x) << SAI_XCR1_MCKDIV_SHIFT)
7903e78a24Solivier moysan #define SAI_XCR1_MCKDIV_MAX(x)	((1 << SAI_XCR1_MCKDIV_WIDTH(x)) - 1)
803e086edfSolivier moysan 
813e086edfSolivier moysan #define SAI_XCR1_OSR_SHIFT	26
823e086edfSolivier moysan #define SAI_XCR1_OSR		BIT(SAI_XCR1_OSR_SHIFT)
833e086edfSolivier moysan 
848307b2afSOlivier Moysan #define SAI_XCR1_MCKEN_SHIFT	27
858307b2afSOlivier Moysan #define SAI_XCR1_MCKEN		BIT(SAI_XCR1_MCKEN_SHIFT)
868307b2afSOlivier Moysan 
873e086edfSolivier moysan /******************* Bit definition for SAI_XCR2 register *******************/
883e086edfSolivier moysan #define SAI_XCR2_FTH_SHIFT	0
893e086edfSolivier moysan #define SAI_XCR2_FTH_MASK	GENMASK(2, SAI_XCR2_FTH_SHIFT)
903e086edfSolivier moysan #define SAI_XCR2_FTH_SET(x)	((x) << SAI_XCR2_FTH_SHIFT)
913e086edfSolivier moysan 
923e086edfSolivier moysan #define SAI_XCR2_FFLUSH_SHIFT	3
933e086edfSolivier moysan #define SAI_XCR2_FFLUSH		BIT(SAI_XCR2_FFLUSH_SHIFT)
943e086edfSolivier moysan #define SAI_XCR2_TRIS_SHIFT	4
953e086edfSolivier moysan #define SAI_XCR2_TRIS		BIT(SAI_XCR2_TRIS_SHIFT)
963e086edfSolivier moysan #define SAI_XCR2_MUTE_SHIFT	5
973e086edfSolivier moysan #define SAI_XCR2_MUTE		BIT(SAI_XCR2_MUTE_SHIFT)
983e086edfSolivier moysan #define SAI_XCR2_MUTEVAL_SHIFT	6
993e086edfSolivier moysan #define SAI_XCR2_MUTEVAL	BIT(SAI_XCR2_MUTEVAL_SHIFT)
1003e086edfSolivier moysan 
1013e086edfSolivier moysan #define SAI_XCR2_MUTECNT_SHIFT	7
1023e086edfSolivier moysan #define SAI_XCR2_MUTECNT_MASK	GENMASK(12, SAI_XCR2_MUTECNT_SHIFT)
1033e086edfSolivier moysan #define SAI_XCR2_MUTECNT_SET(x)	((x) << SAI_XCR2_MUTECNT_SHIFT)
1043e086edfSolivier moysan 
1053e086edfSolivier moysan #define SAI_XCR2_CPL_SHIFT	13
1063e086edfSolivier moysan #define SAI_XCR2_CPL		BIT(SAI_XCR2_CPL_SHIFT)
1073e086edfSolivier moysan 
1083e086edfSolivier moysan #define SAI_XCR2_COMP_SHIFT	14
1093e086edfSolivier moysan #define SAI_XCR2_COMP_MASK	GENMASK(15, SAI_XCR2_COMP_SHIFT)
1103e086edfSolivier moysan #define SAI_XCR2_COMP_SET(x)	((x) << SAI_XCR2_COMP_SHIFT)
1113e086edfSolivier moysan 
1123e086edfSolivier moysan /****************** Bit definition for SAI_XFRCR register *******************/
1133e086edfSolivier moysan #define SAI_XFRCR_FRL_SHIFT	0
1143e086edfSolivier moysan #define SAI_XFRCR_FRL_MASK	GENMASK(7, SAI_XFRCR_FRL_SHIFT)
1153e086edfSolivier moysan #define SAI_XFRCR_FRL_SET(x)	((x) << SAI_XFRCR_FRL_SHIFT)
1163e086edfSolivier moysan 
1173e086edfSolivier moysan #define SAI_XFRCR_FSALL_SHIFT	8
1183e086edfSolivier moysan #define SAI_XFRCR_FSALL_MASK	GENMASK(14, SAI_XFRCR_FSALL_SHIFT)
1193e086edfSolivier moysan #define SAI_XFRCR_FSALL_SET(x)	((x) << SAI_XFRCR_FSALL_SHIFT)
1203e086edfSolivier moysan 
1213e086edfSolivier moysan #define SAI_XFRCR_FSDEF_SHIFT	16
1223e086edfSolivier moysan #define SAI_XFRCR_FSDEF		BIT(SAI_XFRCR_FSDEF_SHIFT)
1233e086edfSolivier moysan #define SAI_XFRCR_FSPOL_SHIFT	17
1243e086edfSolivier moysan #define SAI_XFRCR_FSPOL		BIT(SAI_XFRCR_FSPOL_SHIFT)
1253e086edfSolivier moysan #define SAI_XFRCR_FSOFF_SHIFT	18
1263e086edfSolivier moysan #define SAI_XFRCR_FSOFF		BIT(SAI_XFRCR_FSOFF_SHIFT)
1273e086edfSolivier moysan 
1283e086edfSolivier moysan /****************** Bit definition for SAI_XSLOTR register ******************/
1293e086edfSolivier moysan #define SAI_XSLOTR_FBOFF_SHIFT	0
1303e086edfSolivier moysan #define SAI_XSLOTR_FBOFF_MASK	GENMASK(4, SAI_XSLOTR_FBOFF_SHIFT)
1313e086edfSolivier moysan #define SAI_XSLOTR_FBOFF_SET(x)	((x) << SAI_XSLOTR_FBOFF_SHIFT)
1323e086edfSolivier moysan 
1333e086edfSolivier moysan #define SAI_XSLOTR_SLOTSZ_SHIFT	6
1343e086edfSolivier moysan #define SAI_XSLOTR_SLOTSZ_MASK	GENMASK(7, SAI_XSLOTR_SLOTSZ_SHIFT)
1353e086edfSolivier moysan #define SAI_XSLOTR_SLOTSZ_SET(x)	((x) << SAI_XSLOTR_SLOTSZ_SHIFT)
1363e086edfSolivier moysan 
1373e086edfSolivier moysan #define SAI_XSLOTR_NBSLOT_SHIFT 8
1383e086edfSolivier moysan #define SAI_XSLOTR_NBSLOT_MASK	GENMASK(11, SAI_XSLOTR_NBSLOT_SHIFT)
1393e086edfSolivier moysan #define SAI_XSLOTR_NBSLOT_SET(x) ((x) << SAI_XSLOTR_NBSLOT_SHIFT)
1403e086edfSolivier moysan 
1413e086edfSolivier moysan #define SAI_XSLOTR_SLOTEN_SHIFT	16
1423e086edfSolivier moysan #define SAI_XSLOTR_SLOTEN_WIDTH	16
1433e086edfSolivier moysan #define SAI_XSLOTR_SLOTEN_MASK	GENMASK(31, SAI_XSLOTR_SLOTEN_SHIFT)
1443e086edfSolivier moysan #define SAI_XSLOTR_SLOTEN_SET(x) ((x) << SAI_XSLOTR_SLOTEN_SHIFT)
1453e086edfSolivier moysan 
1463e086edfSolivier moysan /******************* Bit definition for SAI_XIMR register *******************/
1473e086edfSolivier moysan #define SAI_XIMR_OVRUDRIE	BIT(0)
1483e086edfSolivier moysan #define SAI_XIMR_MUTEDETIE	BIT(1)
1493e086edfSolivier moysan #define SAI_XIMR_WCKCFGIE	BIT(2)
1503e086edfSolivier moysan #define SAI_XIMR_FREQIE		BIT(3)
1513e086edfSolivier moysan #define SAI_XIMR_CNRDYIE	BIT(4)
1523e086edfSolivier moysan #define SAI_XIMR_AFSDETIE	BIT(5)
1533e086edfSolivier moysan #define SAI_XIMR_LFSDETIE	BIT(6)
1543e086edfSolivier moysan 
1553e086edfSolivier moysan #define SAI_XIMR_SHIFT	0
1563e086edfSolivier moysan #define SAI_XIMR_MASK		GENMASK(6, SAI_XIMR_SHIFT)
1573e086edfSolivier moysan 
1583e086edfSolivier moysan /******************** Bit definition for SAI_XSR register *******************/
1593e086edfSolivier moysan #define SAI_XSR_OVRUDR		BIT(0)
1603e086edfSolivier moysan #define SAI_XSR_MUTEDET		BIT(1)
1613e086edfSolivier moysan #define SAI_XSR_WCKCFG		BIT(2)
1623e086edfSolivier moysan #define SAI_XSR_FREQ		BIT(3)
1633e086edfSolivier moysan #define SAI_XSR_CNRDY		BIT(4)
1643e086edfSolivier moysan #define SAI_XSR_AFSDET		BIT(5)
1653e086edfSolivier moysan #define SAI_XSR_LFSDET		BIT(6)
1663e086edfSolivier moysan 
1673e086edfSolivier moysan #define SAI_XSR_SHIFT	0
1683e086edfSolivier moysan #define SAI_XSR_MASK		GENMASK(6, SAI_XSR_SHIFT)
1693e086edfSolivier moysan 
1703e086edfSolivier moysan /****************** Bit definition for SAI_XCLRFR register ******************/
1713e086edfSolivier moysan #define SAI_XCLRFR_COVRUDR	BIT(0)
1723e086edfSolivier moysan #define SAI_XCLRFR_CMUTEDET	BIT(1)
1733e086edfSolivier moysan #define SAI_XCLRFR_CWCKCFG	BIT(2)
1743e086edfSolivier moysan #define SAI_XCLRFR_CFREQ	BIT(3)
1753e086edfSolivier moysan #define SAI_XCLRFR_CCNRDY	BIT(4)
1763e086edfSolivier moysan #define SAI_XCLRFR_CAFSDET	BIT(5)
1773e086edfSolivier moysan #define SAI_XCLRFR_CLFSDET	BIT(6)
1783e086edfSolivier moysan 
1793e086edfSolivier moysan #define SAI_XCLRFR_SHIFT	0
1803e086edfSolivier moysan #define SAI_XCLRFR_MASK		GENMASK(6, SAI_XCLRFR_SHIFT)
1813e086edfSolivier moysan 
18203e78a24Solivier moysan /****************** Bit definition for SAI_PDMCR register ******************/
18303e78a24Solivier moysan #define SAI_PDMCR_PDMEN		BIT(0)
18403e78a24Solivier moysan 
18503e78a24Solivier moysan #define SAI_PDMCR_MICNBR_SHIFT	4
18603e78a24Solivier moysan #define SAI_PDMCR_MICNBR_MASK	GENMASK(5, SAI_PDMCR_MICNBR_SHIFT)
18703e78a24Solivier moysan #define SAI_PDMCR_MICNBR_SET(x)	((x) << SAI_PDMCR_MICNBR_SHIFT)
18803e78a24Solivier moysan 
18903e78a24Solivier moysan #define SAI_PDMCR_CKEN1		BIT(8)
19003e78a24Solivier moysan #define SAI_PDMCR_CKEN2		BIT(9)
19103e78a24Solivier moysan #define SAI_PDMCR_CKEN3		BIT(10)
19203e78a24Solivier moysan #define SAI_PDMCR_CKEN4		BIT(11)
19303e78a24Solivier moysan 
19403e78a24Solivier moysan /****************** Bit definition for (SAI_PDMDLY register ****************/
19503e78a24Solivier moysan #define SAI_PDMDLY_1L_SHIFT	0
19603e78a24Solivier moysan #define SAI_PDMDLY_1L_MASK	GENMASK(2, SAI_PDMDLY_1L_SHIFT)
19703e78a24Solivier moysan #define SAI_PDMDLY_1L_WIDTH	3
19803e78a24Solivier moysan 
19903e78a24Solivier moysan #define SAI_PDMDLY_1R_SHIFT	4
20003e78a24Solivier moysan #define SAI_PDMDLY_1R_MASK	GENMASK(6, SAI_PDMDLY_1R_SHIFT)
20103e78a24Solivier moysan #define SAI_PDMDLY_1R_WIDTH	3
20203e78a24Solivier moysan 
20303e78a24Solivier moysan #define SAI_PDMDLY_2L_SHIFT	8
20403e78a24Solivier moysan #define SAI_PDMDLY_2L_MASK	GENMASK(10, SAI_PDMDLY_2L_SHIFT)
20503e78a24Solivier moysan #define SAI_PDMDLY_2L_WIDTH	3
20603e78a24Solivier moysan 
20703e78a24Solivier moysan #define SAI_PDMDLY_2R_SHIFT	12
20803e78a24Solivier moysan #define SAI_PDMDLY_2R_MASK	GENMASK(14, SAI_PDMDLY_2R_SHIFT)
20903e78a24Solivier moysan #define SAI_PDMDLY_2R_WIDTH	3
21003e78a24Solivier moysan 
21103e78a24Solivier moysan #define SAI_PDMDLY_3L_SHIFT	16
21203e78a24Solivier moysan #define SAI_PDMDLY_3L_MASK	GENMASK(18, SAI_PDMDLY_3L_SHIFT)
21303e78a24Solivier moysan #define SAI_PDMDLY_3L_WIDTH	3
21403e78a24Solivier moysan 
21503e78a24Solivier moysan #define SAI_PDMDLY_3R_SHIFT	20
21603e78a24Solivier moysan #define SAI_PDMDLY_3R_MASK	GENMASK(22, SAI_PDMDLY_3R_SHIFT)
21703e78a24Solivier moysan #define SAI_PDMDLY_3R_WIDTH	3
21803e78a24Solivier moysan 
21903e78a24Solivier moysan #define SAI_PDMDLY_4L_SHIFT	24
22003e78a24Solivier moysan #define SAI_PDMDLY_4L_MASK	GENMASK(26, SAI_PDMDLY_4L_SHIFT)
22103e78a24Solivier moysan #define SAI_PDMDLY_4L_WIDTH	3
22203e78a24Solivier moysan 
22303e78a24Solivier moysan #define SAI_PDMDLY_4R_SHIFT	28
22403e78a24Solivier moysan #define SAI_PDMDLY_4R_MASK	GENMASK(30, SAI_PDMDLY_4R_SHIFT)
22503e78a24Solivier moysan #define SAI_PDMDLY_4R_WIDTH	3
22603e78a24Solivier moysan 
22703e78a24Solivier moysan #define STM_SAI_IS_F4(ip)	((ip)->conf->version == SAI_STM32F4)
22803e78a24Solivier moysan #define STM_SAI_IS_H7(ip)	((ip)->conf->version == SAI_STM32H7)
22903e78a24Solivier moysan 
2305914d285SOlivier Moysan enum stm32_sai_syncout {
2315914d285SOlivier Moysan 	STM_SAI_SYNC_OUT_NONE,
2325914d285SOlivier Moysan 	STM_SAI_SYNC_OUT_A,
2335914d285SOlivier Moysan 	STM_SAI_SYNC_OUT_B,
2345914d285SOlivier Moysan };
2355914d285SOlivier Moysan 
2363e086edfSolivier moysan enum stm32_sai_version {
23703e78a24Solivier moysan 	SAI_STM32F4,
23803e78a24Solivier moysan 	SAI_STM32H7
23903e78a24Solivier moysan };
24003e78a24Solivier moysan 
24103e78a24Solivier moysan /**
24203e78a24Solivier moysan  * struct stm32_sai_conf - SAI configuration
24303e78a24Solivier moysan  * @version: SAI version
2446eb17d70SOlivier Moysan  * @has_spdif: SAI S/PDIF support flag
24503e78a24Solivier moysan  */
24603e78a24Solivier moysan struct stm32_sai_conf {
24703e78a24Solivier moysan 	int version;
2486eb17d70SOlivier Moysan 	bool has_spdif;
2493e086edfSolivier moysan };
2503e086edfSolivier moysan 
2513e086edfSolivier moysan /**
2523e086edfSolivier moysan  * struct stm32_sai_data - private data of SAI instance driver
2533e086edfSolivier moysan  * @pdev: device data pointer
2545914d285SOlivier Moysan  * @base: common register bank virtual base address
2555914d285SOlivier Moysan  * @pclk: SAI bus clock
2563e086edfSolivier moysan  * @clk_x8k: SAI parent clock for sampling frequencies multiple of 8kHz
2573e086edfSolivier moysan  * @clk_x11k: SAI parent clock for sampling frequencies multiple of 11kHz
2583e086edfSolivier moysan  * @version: SOC version
2593e086edfSolivier moysan  * @irq: SAI interrupt line
2605914d285SOlivier Moysan  * @set_sync: pointer to synchro mode configuration callback
261cf881773SOlivier Moysan  * @gcr: SAI Global Configuration Register
2623e086edfSolivier moysan  */
2633e086edfSolivier moysan struct stm32_sai_data {
2643e086edfSolivier moysan 	struct platform_device *pdev;
2655914d285SOlivier Moysan 	void __iomem *base;
2665914d285SOlivier Moysan 	struct clk *pclk;
2673e086edfSolivier moysan 	struct clk *clk_x8k;
2683e086edfSolivier moysan 	struct clk *clk_x11k;
26903e78a24Solivier moysan 	struct stm32_sai_conf *conf;
2703e086edfSolivier moysan 	int irq;
2715914d285SOlivier Moysan 	int (*set_sync)(struct stm32_sai_data *sai,
2725914d285SOlivier Moysan 			struct device_node *np_provider, int synco, int synci);
273cf881773SOlivier Moysan 	u32 gcr;
2743e086edfSolivier moysan };
275