13e086edfSolivier moysan /* 23e086edfSolivier moysan * STM32 ALSA SoC Digital Audio Interface (SAI) driver. 33e086edfSolivier moysan * 43e086edfSolivier moysan * Copyright (C) 2016, STMicroelectronics - All Rights Reserved 53e086edfSolivier moysan * Author(s): Olivier Moysan <olivier.moysan@st.com> for STMicroelectronics. 63e086edfSolivier moysan * 73e086edfSolivier moysan * License terms: GPL V2.0. 83e086edfSolivier moysan * 93e086edfSolivier moysan * This program is free software; you can redistribute it and/or modify it 103e086edfSolivier moysan * under the terms of the GNU General Public License version 2 as published by 113e086edfSolivier moysan * the Free Software Foundation. 123e086edfSolivier moysan * 133e086edfSolivier moysan * This program is distributed in the hope that it will be useful, but 143e086edfSolivier moysan * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 153e086edfSolivier moysan * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 163e086edfSolivier moysan * details. 173e086edfSolivier moysan */ 183e086edfSolivier moysan 19*5914d285SOlivier Moysan #include <linux/bitfield.h> 20*5914d285SOlivier Moysan 213e086edfSolivier moysan /******************** SAI Register Map **************************************/ 223e086edfSolivier moysan 23*5914d285SOlivier Moysan /* Global configuration register */ 243e086edfSolivier moysan #define STM_SAI_GCR 0x00 253e086edfSolivier moysan 263e086edfSolivier moysan /* Sub-block A&B registers offsets, relative to A&B sub-block addresses */ 273e086edfSolivier moysan #define STM_SAI_CR1_REGX 0x00 /* A offset: 0x04. B offset: 0x24 */ 283e086edfSolivier moysan #define STM_SAI_CR2_REGX 0x04 293e086edfSolivier moysan #define STM_SAI_FRCR_REGX 0x08 303e086edfSolivier moysan #define STM_SAI_SLOTR_REGX 0x0C 313e086edfSolivier moysan #define STM_SAI_IMR_REGX 0x10 323e086edfSolivier moysan #define STM_SAI_SR_REGX 0x14 333e086edfSolivier moysan #define STM_SAI_CLRFR_REGX 0x18 343e086edfSolivier moysan #define STM_SAI_DR_REGX 0x1C 353e086edfSolivier moysan 3603e78a24Solivier moysan /* Sub-block A registers, relative to sub-block A address */ 3703e78a24Solivier moysan #define STM_SAI_PDMCR_REGX 0x40 3803e78a24Solivier moysan #define STM_SAI_PDMLY_REGX 0x44 3903e78a24Solivier moysan 403e086edfSolivier moysan /******************** Bit definition for SAI_GCR register *******************/ 413e086edfSolivier moysan #define SAI_GCR_SYNCIN_SHIFT 0 42*5914d285SOlivier Moysan #define SAI_GCR_SYNCIN_WDTH 2 433e086edfSolivier moysan #define SAI_GCR_SYNCIN_MASK GENMASK(1, SAI_GCR_SYNCIN_SHIFT) 44*5914d285SOlivier Moysan #define SAI_GCR_SYNCIN_MAX FIELD_GET(SAI_GCR_SYNCIN_MASK,\ 45*5914d285SOlivier Moysan SAI_GCR_SYNCIN_MASK) 463e086edfSolivier moysan 473e086edfSolivier moysan #define SAI_GCR_SYNCOUT_SHIFT 4 483e086edfSolivier moysan #define SAI_GCR_SYNCOUT_MASK GENMASK(5, SAI_GCR_SYNCOUT_SHIFT) 493e086edfSolivier moysan 503e086edfSolivier moysan /******************* Bit definition for SAI_XCR1 register *******************/ 513e086edfSolivier moysan #define SAI_XCR1_RX_TX_SHIFT 0 523e086edfSolivier moysan #define SAI_XCR1_RX_TX BIT(SAI_XCR1_RX_TX_SHIFT) 533e086edfSolivier moysan #define SAI_XCR1_SLAVE_SHIFT 1 543e086edfSolivier moysan #define SAI_XCR1_SLAVE BIT(SAI_XCR1_SLAVE_SHIFT) 553e086edfSolivier moysan 563e086edfSolivier moysan #define SAI_XCR1_PRTCFG_SHIFT 2 573e086edfSolivier moysan #define SAI_XCR1_PRTCFG_MASK GENMASK(3, SAI_XCR1_PRTCFG_SHIFT) 583e086edfSolivier moysan #define SAI_XCR1_PRTCFG_SET(x) ((x) << SAI_XCR1_PRTCFG_SHIFT) 593e086edfSolivier moysan 603e086edfSolivier moysan #define SAI_XCR1_DS_SHIFT 5 613e086edfSolivier moysan #define SAI_XCR1_DS_MASK GENMASK(7, SAI_XCR1_DS_SHIFT) 623e086edfSolivier moysan #define SAI_XCR1_DS_SET(x) ((x) << SAI_XCR1_DS_SHIFT) 633e086edfSolivier moysan 643e086edfSolivier moysan #define SAI_XCR1_LSBFIRST_SHIFT 8 653e086edfSolivier moysan #define SAI_XCR1_LSBFIRST BIT(SAI_XCR1_LSBFIRST_SHIFT) 663e086edfSolivier moysan #define SAI_XCR1_CKSTR_SHIFT 9 673e086edfSolivier moysan #define SAI_XCR1_CKSTR BIT(SAI_XCR1_CKSTR_SHIFT) 683e086edfSolivier moysan 693e086edfSolivier moysan #define SAI_XCR1_SYNCEN_SHIFT 10 703e086edfSolivier moysan #define SAI_XCR1_SYNCEN_MASK GENMASK(11, SAI_XCR1_SYNCEN_SHIFT) 713e086edfSolivier moysan #define SAI_XCR1_SYNCEN_SET(x) ((x) << SAI_XCR1_SYNCEN_SHIFT) 723e086edfSolivier moysan 733e086edfSolivier moysan #define SAI_XCR1_MONO_SHIFT 12 743e086edfSolivier moysan #define SAI_XCR1_MONO BIT(SAI_XCR1_MONO_SHIFT) 753e086edfSolivier moysan #define SAI_XCR1_OUTDRIV_SHIFT 13 763e086edfSolivier moysan #define SAI_XCR1_OUTDRIV BIT(SAI_XCR1_OUTDRIV_SHIFT) 773e086edfSolivier moysan #define SAI_XCR1_SAIEN_SHIFT 16 783e086edfSolivier moysan #define SAI_XCR1_SAIEN BIT(SAI_XCR1_SAIEN_SHIFT) 793e086edfSolivier moysan #define SAI_XCR1_DMAEN_SHIFT 17 803e086edfSolivier moysan #define SAI_XCR1_DMAEN BIT(SAI_XCR1_DMAEN_SHIFT) 813e086edfSolivier moysan #define SAI_XCR1_NODIV_SHIFT 19 823e086edfSolivier moysan #define SAI_XCR1_NODIV BIT(SAI_XCR1_NODIV_SHIFT) 833e086edfSolivier moysan 843e086edfSolivier moysan #define SAI_XCR1_MCKDIV_SHIFT 20 8503e78a24Solivier moysan #define SAI_XCR1_MCKDIV_WIDTH(x) (((x) == SAI_STM32F4) ? 4 : 6) 8603e78a24Solivier moysan #define SAI_XCR1_MCKDIV_MASK(x) GENMASK((SAI_XCR1_MCKDIV_SHIFT + (x) - 1),\ 8703e78a24Solivier moysan SAI_XCR1_MCKDIV_SHIFT) 883e086edfSolivier moysan #define SAI_XCR1_MCKDIV_SET(x) ((x) << SAI_XCR1_MCKDIV_SHIFT) 8903e78a24Solivier moysan #define SAI_XCR1_MCKDIV_MAX(x) ((1 << SAI_XCR1_MCKDIV_WIDTH(x)) - 1) 903e086edfSolivier moysan 913e086edfSolivier moysan #define SAI_XCR1_OSR_SHIFT 26 923e086edfSolivier moysan #define SAI_XCR1_OSR BIT(SAI_XCR1_OSR_SHIFT) 933e086edfSolivier moysan 943e086edfSolivier moysan /******************* Bit definition for SAI_XCR2 register *******************/ 953e086edfSolivier moysan #define SAI_XCR2_FTH_SHIFT 0 963e086edfSolivier moysan #define SAI_XCR2_FTH_MASK GENMASK(2, SAI_XCR2_FTH_SHIFT) 973e086edfSolivier moysan #define SAI_XCR2_FTH_SET(x) ((x) << SAI_XCR2_FTH_SHIFT) 983e086edfSolivier moysan 993e086edfSolivier moysan #define SAI_XCR2_FFLUSH_SHIFT 3 1003e086edfSolivier moysan #define SAI_XCR2_FFLUSH BIT(SAI_XCR2_FFLUSH_SHIFT) 1013e086edfSolivier moysan #define SAI_XCR2_TRIS_SHIFT 4 1023e086edfSolivier moysan #define SAI_XCR2_TRIS BIT(SAI_XCR2_TRIS_SHIFT) 1033e086edfSolivier moysan #define SAI_XCR2_MUTE_SHIFT 5 1043e086edfSolivier moysan #define SAI_XCR2_MUTE BIT(SAI_XCR2_MUTE_SHIFT) 1053e086edfSolivier moysan #define SAI_XCR2_MUTEVAL_SHIFT 6 1063e086edfSolivier moysan #define SAI_XCR2_MUTEVAL BIT(SAI_XCR2_MUTEVAL_SHIFT) 1073e086edfSolivier moysan 1083e086edfSolivier moysan #define SAI_XCR2_MUTECNT_SHIFT 7 1093e086edfSolivier moysan #define SAI_XCR2_MUTECNT_MASK GENMASK(12, SAI_XCR2_MUTECNT_SHIFT) 1103e086edfSolivier moysan #define SAI_XCR2_MUTECNT_SET(x) ((x) << SAI_XCR2_MUTECNT_SHIFT) 1113e086edfSolivier moysan 1123e086edfSolivier moysan #define SAI_XCR2_CPL_SHIFT 13 1133e086edfSolivier moysan #define SAI_XCR2_CPL BIT(SAI_XCR2_CPL_SHIFT) 1143e086edfSolivier moysan 1153e086edfSolivier moysan #define SAI_XCR2_COMP_SHIFT 14 1163e086edfSolivier moysan #define SAI_XCR2_COMP_MASK GENMASK(15, SAI_XCR2_COMP_SHIFT) 1173e086edfSolivier moysan #define SAI_XCR2_COMP_SET(x) ((x) << SAI_XCR2_COMP_SHIFT) 1183e086edfSolivier moysan 1193e086edfSolivier moysan /****************** Bit definition for SAI_XFRCR register *******************/ 1203e086edfSolivier moysan #define SAI_XFRCR_FRL_SHIFT 0 1213e086edfSolivier moysan #define SAI_XFRCR_FRL_MASK GENMASK(7, SAI_XFRCR_FRL_SHIFT) 1223e086edfSolivier moysan #define SAI_XFRCR_FRL_SET(x) ((x) << SAI_XFRCR_FRL_SHIFT) 1233e086edfSolivier moysan 1243e086edfSolivier moysan #define SAI_XFRCR_FSALL_SHIFT 8 1253e086edfSolivier moysan #define SAI_XFRCR_FSALL_MASK GENMASK(14, SAI_XFRCR_FSALL_SHIFT) 1263e086edfSolivier moysan #define SAI_XFRCR_FSALL_SET(x) ((x) << SAI_XFRCR_FSALL_SHIFT) 1273e086edfSolivier moysan 1283e086edfSolivier moysan #define SAI_XFRCR_FSDEF_SHIFT 16 1293e086edfSolivier moysan #define SAI_XFRCR_FSDEF BIT(SAI_XFRCR_FSDEF_SHIFT) 1303e086edfSolivier moysan #define SAI_XFRCR_FSPOL_SHIFT 17 1313e086edfSolivier moysan #define SAI_XFRCR_FSPOL BIT(SAI_XFRCR_FSPOL_SHIFT) 1323e086edfSolivier moysan #define SAI_XFRCR_FSOFF_SHIFT 18 1333e086edfSolivier moysan #define SAI_XFRCR_FSOFF BIT(SAI_XFRCR_FSOFF_SHIFT) 1343e086edfSolivier moysan 1353e086edfSolivier moysan /****************** Bit definition for SAI_XSLOTR register ******************/ 1363e086edfSolivier moysan #define SAI_XSLOTR_FBOFF_SHIFT 0 1373e086edfSolivier moysan #define SAI_XSLOTR_FBOFF_MASK GENMASK(4, SAI_XSLOTR_FBOFF_SHIFT) 1383e086edfSolivier moysan #define SAI_XSLOTR_FBOFF_SET(x) ((x) << SAI_XSLOTR_FBOFF_SHIFT) 1393e086edfSolivier moysan 1403e086edfSolivier moysan #define SAI_XSLOTR_SLOTSZ_SHIFT 6 1413e086edfSolivier moysan #define SAI_XSLOTR_SLOTSZ_MASK GENMASK(7, SAI_XSLOTR_SLOTSZ_SHIFT) 1423e086edfSolivier moysan #define SAI_XSLOTR_SLOTSZ_SET(x) ((x) << SAI_XSLOTR_SLOTSZ_SHIFT) 1433e086edfSolivier moysan 1443e086edfSolivier moysan #define SAI_XSLOTR_NBSLOT_SHIFT 8 1453e086edfSolivier moysan #define SAI_XSLOTR_NBSLOT_MASK GENMASK(11, SAI_XSLOTR_NBSLOT_SHIFT) 1463e086edfSolivier moysan #define SAI_XSLOTR_NBSLOT_SET(x) ((x) << SAI_XSLOTR_NBSLOT_SHIFT) 1473e086edfSolivier moysan 1483e086edfSolivier moysan #define SAI_XSLOTR_SLOTEN_SHIFT 16 1493e086edfSolivier moysan #define SAI_XSLOTR_SLOTEN_WIDTH 16 1503e086edfSolivier moysan #define SAI_XSLOTR_SLOTEN_MASK GENMASK(31, SAI_XSLOTR_SLOTEN_SHIFT) 1513e086edfSolivier moysan #define SAI_XSLOTR_SLOTEN_SET(x) ((x) << SAI_XSLOTR_SLOTEN_SHIFT) 1523e086edfSolivier moysan 1533e086edfSolivier moysan /******************* Bit definition for SAI_XIMR register *******************/ 1543e086edfSolivier moysan #define SAI_XIMR_OVRUDRIE BIT(0) 1553e086edfSolivier moysan #define SAI_XIMR_MUTEDETIE BIT(1) 1563e086edfSolivier moysan #define SAI_XIMR_WCKCFGIE BIT(2) 1573e086edfSolivier moysan #define SAI_XIMR_FREQIE BIT(3) 1583e086edfSolivier moysan #define SAI_XIMR_CNRDYIE BIT(4) 1593e086edfSolivier moysan #define SAI_XIMR_AFSDETIE BIT(5) 1603e086edfSolivier moysan #define SAI_XIMR_LFSDETIE BIT(6) 1613e086edfSolivier moysan 1623e086edfSolivier moysan #define SAI_XIMR_SHIFT 0 1633e086edfSolivier moysan #define SAI_XIMR_MASK GENMASK(6, SAI_XIMR_SHIFT) 1643e086edfSolivier moysan 1653e086edfSolivier moysan /******************** Bit definition for SAI_XSR register *******************/ 1663e086edfSolivier moysan #define SAI_XSR_OVRUDR BIT(0) 1673e086edfSolivier moysan #define SAI_XSR_MUTEDET BIT(1) 1683e086edfSolivier moysan #define SAI_XSR_WCKCFG BIT(2) 1693e086edfSolivier moysan #define SAI_XSR_FREQ BIT(3) 1703e086edfSolivier moysan #define SAI_XSR_CNRDY BIT(4) 1713e086edfSolivier moysan #define SAI_XSR_AFSDET BIT(5) 1723e086edfSolivier moysan #define SAI_XSR_LFSDET BIT(6) 1733e086edfSolivier moysan 1743e086edfSolivier moysan #define SAI_XSR_SHIFT 0 1753e086edfSolivier moysan #define SAI_XSR_MASK GENMASK(6, SAI_XSR_SHIFT) 1763e086edfSolivier moysan 1773e086edfSolivier moysan /****************** Bit definition for SAI_XCLRFR register ******************/ 1783e086edfSolivier moysan #define SAI_XCLRFR_COVRUDR BIT(0) 1793e086edfSolivier moysan #define SAI_XCLRFR_CMUTEDET BIT(1) 1803e086edfSolivier moysan #define SAI_XCLRFR_CWCKCFG BIT(2) 1813e086edfSolivier moysan #define SAI_XCLRFR_CFREQ BIT(3) 1823e086edfSolivier moysan #define SAI_XCLRFR_CCNRDY BIT(4) 1833e086edfSolivier moysan #define SAI_XCLRFR_CAFSDET BIT(5) 1843e086edfSolivier moysan #define SAI_XCLRFR_CLFSDET BIT(6) 1853e086edfSolivier moysan 1863e086edfSolivier moysan #define SAI_XCLRFR_SHIFT 0 1873e086edfSolivier moysan #define SAI_XCLRFR_MASK GENMASK(6, SAI_XCLRFR_SHIFT) 1883e086edfSolivier moysan 18903e78a24Solivier moysan /****************** Bit definition for SAI_PDMCR register ******************/ 19003e78a24Solivier moysan #define SAI_PDMCR_PDMEN BIT(0) 19103e78a24Solivier moysan 19203e78a24Solivier moysan #define SAI_PDMCR_MICNBR_SHIFT 4 19303e78a24Solivier moysan #define SAI_PDMCR_MICNBR_MASK GENMASK(5, SAI_PDMCR_MICNBR_SHIFT) 19403e78a24Solivier moysan #define SAI_PDMCR_MICNBR_SET(x) ((x) << SAI_PDMCR_MICNBR_SHIFT) 19503e78a24Solivier moysan 19603e78a24Solivier moysan #define SAI_PDMCR_CKEN1 BIT(8) 19703e78a24Solivier moysan #define SAI_PDMCR_CKEN2 BIT(9) 19803e78a24Solivier moysan #define SAI_PDMCR_CKEN3 BIT(10) 19903e78a24Solivier moysan #define SAI_PDMCR_CKEN4 BIT(11) 20003e78a24Solivier moysan 20103e78a24Solivier moysan /****************** Bit definition for (SAI_PDMDLY register ****************/ 20203e78a24Solivier moysan #define SAI_PDMDLY_1L_SHIFT 0 20303e78a24Solivier moysan #define SAI_PDMDLY_1L_MASK GENMASK(2, SAI_PDMDLY_1L_SHIFT) 20403e78a24Solivier moysan #define SAI_PDMDLY_1L_WIDTH 3 20503e78a24Solivier moysan 20603e78a24Solivier moysan #define SAI_PDMDLY_1R_SHIFT 4 20703e78a24Solivier moysan #define SAI_PDMDLY_1R_MASK GENMASK(6, SAI_PDMDLY_1R_SHIFT) 20803e78a24Solivier moysan #define SAI_PDMDLY_1R_WIDTH 3 20903e78a24Solivier moysan 21003e78a24Solivier moysan #define SAI_PDMDLY_2L_SHIFT 8 21103e78a24Solivier moysan #define SAI_PDMDLY_2L_MASK GENMASK(10, SAI_PDMDLY_2L_SHIFT) 21203e78a24Solivier moysan #define SAI_PDMDLY_2L_WIDTH 3 21303e78a24Solivier moysan 21403e78a24Solivier moysan #define SAI_PDMDLY_2R_SHIFT 12 21503e78a24Solivier moysan #define SAI_PDMDLY_2R_MASK GENMASK(14, SAI_PDMDLY_2R_SHIFT) 21603e78a24Solivier moysan #define SAI_PDMDLY_2R_WIDTH 3 21703e78a24Solivier moysan 21803e78a24Solivier moysan #define SAI_PDMDLY_3L_SHIFT 16 21903e78a24Solivier moysan #define SAI_PDMDLY_3L_MASK GENMASK(18, SAI_PDMDLY_3L_SHIFT) 22003e78a24Solivier moysan #define SAI_PDMDLY_3L_WIDTH 3 22103e78a24Solivier moysan 22203e78a24Solivier moysan #define SAI_PDMDLY_3R_SHIFT 20 22303e78a24Solivier moysan #define SAI_PDMDLY_3R_MASK GENMASK(22, SAI_PDMDLY_3R_SHIFT) 22403e78a24Solivier moysan #define SAI_PDMDLY_3R_WIDTH 3 22503e78a24Solivier moysan 22603e78a24Solivier moysan #define SAI_PDMDLY_4L_SHIFT 24 22703e78a24Solivier moysan #define SAI_PDMDLY_4L_MASK GENMASK(26, SAI_PDMDLY_4L_SHIFT) 22803e78a24Solivier moysan #define SAI_PDMDLY_4L_WIDTH 3 22903e78a24Solivier moysan 23003e78a24Solivier moysan #define SAI_PDMDLY_4R_SHIFT 28 23103e78a24Solivier moysan #define SAI_PDMDLY_4R_MASK GENMASK(30, SAI_PDMDLY_4R_SHIFT) 23203e78a24Solivier moysan #define SAI_PDMDLY_4R_WIDTH 3 23303e78a24Solivier moysan 23403e78a24Solivier moysan #define STM_SAI_IS_F4(ip) ((ip)->conf->version == SAI_STM32F4) 23503e78a24Solivier moysan #define STM_SAI_IS_H7(ip) ((ip)->conf->version == SAI_STM32H7) 23603e78a24Solivier moysan 237*5914d285SOlivier Moysan enum stm32_sai_syncout { 238*5914d285SOlivier Moysan STM_SAI_SYNC_OUT_NONE, 239*5914d285SOlivier Moysan STM_SAI_SYNC_OUT_A, 240*5914d285SOlivier Moysan STM_SAI_SYNC_OUT_B, 241*5914d285SOlivier Moysan }; 242*5914d285SOlivier Moysan 2433e086edfSolivier moysan enum stm32_sai_version { 24403e78a24Solivier moysan SAI_STM32F4, 24503e78a24Solivier moysan SAI_STM32H7 24603e78a24Solivier moysan }; 24703e78a24Solivier moysan 24803e78a24Solivier moysan /** 24903e78a24Solivier moysan * struct stm32_sai_conf - SAI configuration 25003e78a24Solivier moysan * @version: SAI version 25103e78a24Solivier moysan */ 25203e78a24Solivier moysan struct stm32_sai_conf { 25303e78a24Solivier moysan int version; 2543e086edfSolivier moysan }; 2553e086edfSolivier moysan 2563e086edfSolivier moysan /** 2573e086edfSolivier moysan * struct stm32_sai_data - private data of SAI instance driver 2583e086edfSolivier moysan * @pdev: device data pointer 259*5914d285SOlivier Moysan * @base: common register bank virtual base address 260*5914d285SOlivier Moysan * @pclk: SAI bus clock 2613e086edfSolivier moysan * @clk_x8k: SAI parent clock for sampling frequencies multiple of 8kHz 2623e086edfSolivier moysan * @clk_x11k: SAI parent clock for sampling frequencies multiple of 11kHz 2633e086edfSolivier moysan * @version: SOC version 2643e086edfSolivier moysan * @irq: SAI interrupt line 265*5914d285SOlivier Moysan * @set_sync: pointer to synchro mode configuration callback 2663e086edfSolivier moysan */ 2673e086edfSolivier moysan struct stm32_sai_data { 2683e086edfSolivier moysan struct platform_device *pdev; 269*5914d285SOlivier Moysan void __iomem *base; 270*5914d285SOlivier Moysan struct clk *pclk; 2713e086edfSolivier moysan struct clk *clk_x8k; 2723e086edfSolivier moysan struct clk *clk_x11k; 27303e78a24Solivier moysan struct stm32_sai_conf *conf; 2743e086edfSolivier moysan int irq; 275*5914d285SOlivier Moysan int (*set_sync)(struct stm32_sai_data *sai, 276*5914d285SOlivier Moysan struct device_node *np_provider, int synco, int synci); 2773e086edfSolivier moysan }; 278