11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2e4e6ec7bSolivier moysan /* 3e4e6ec7bSolivier moysan * STM32 ALSA SoC Digital Audio Interface (I2S) driver. 4e4e6ec7bSolivier moysan * 5e4e6ec7bSolivier moysan * Copyright (C) 2017, STMicroelectronics - All Rights Reserved 6e4e6ec7bSolivier moysan * Author(s): Olivier Moysan <olivier.moysan@st.com> for STMicroelectronics. 7e4e6ec7bSolivier moysan */ 8e4e6ec7bSolivier moysan 971ed4bddSOlivier Moysan #include <linux/bitfield.h> 10e4e6ec7bSolivier moysan #include <linux/clk.h> 118a262e61SOlivier Moysan #include <linux/clk-provider.h> 12e4e6ec7bSolivier moysan #include <linux/delay.h> 13e4e6ec7bSolivier moysan #include <linux/module.h> 14e4e6ec7bSolivier moysan #include <linux/of_irq.h> 15e4e6ec7bSolivier moysan #include <linux/of_platform.h> 1632a956a1SOlivier Moysan #include <linux/pm_runtime.h> 17e4e6ec7bSolivier moysan #include <linux/regmap.h> 18e4e6ec7bSolivier moysan #include <linux/reset.h> 19e4e6ec7bSolivier moysan #include <linux/spinlock.h> 20e4e6ec7bSolivier moysan 21e4e6ec7bSolivier moysan #include <sound/dmaengine_pcm.h> 22e4e6ec7bSolivier moysan #include <sound/pcm_params.h> 23e4e6ec7bSolivier moysan 24e4e6ec7bSolivier moysan #define STM32_I2S_CR1_REG 0x0 25e4e6ec7bSolivier moysan #define STM32_I2S_CFG1_REG 0x08 26e4e6ec7bSolivier moysan #define STM32_I2S_CFG2_REG 0x0C 27e4e6ec7bSolivier moysan #define STM32_I2S_IER_REG 0x10 28e4e6ec7bSolivier moysan #define STM32_I2S_SR_REG 0x14 29e4e6ec7bSolivier moysan #define STM32_I2S_IFCR_REG 0x18 30e4e6ec7bSolivier moysan #define STM32_I2S_TXDR_REG 0X20 31e4e6ec7bSolivier moysan #define STM32_I2S_RXDR_REG 0x30 32e4e6ec7bSolivier moysan #define STM32_I2S_CGFR_REG 0X50 3371ed4bddSOlivier Moysan #define STM32_I2S_HWCFGR_REG 0x3F0 3471ed4bddSOlivier Moysan #define STM32_I2S_VERR_REG 0x3F4 3571ed4bddSOlivier Moysan #define STM32_I2S_IPIDR_REG 0x3F8 3671ed4bddSOlivier Moysan #define STM32_I2S_SIDR_REG 0x3FC 37e4e6ec7bSolivier moysan 38e4e6ec7bSolivier moysan /* Bit definition for SPI2S_CR1 register */ 39e4e6ec7bSolivier moysan #define I2S_CR1_SPE BIT(0) 40e4e6ec7bSolivier moysan #define I2S_CR1_CSTART BIT(9) 41e4e6ec7bSolivier moysan #define I2S_CR1_CSUSP BIT(10) 42e4e6ec7bSolivier moysan #define I2S_CR1_HDDIR BIT(11) 43e4e6ec7bSolivier moysan #define I2S_CR1_SSI BIT(12) 44e4e6ec7bSolivier moysan #define I2S_CR1_CRC33_17 BIT(13) 45e4e6ec7bSolivier moysan #define I2S_CR1_RCRCI BIT(14) 46e4e6ec7bSolivier moysan #define I2S_CR1_TCRCI BIT(15) 47e4e6ec7bSolivier moysan 48e4e6ec7bSolivier moysan /* Bit definition for SPI_CFG2 register */ 49e4e6ec7bSolivier moysan #define I2S_CFG2_IOSWP_SHIFT 15 50e4e6ec7bSolivier moysan #define I2S_CFG2_IOSWP BIT(I2S_CFG2_IOSWP_SHIFT) 51e4e6ec7bSolivier moysan #define I2S_CFG2_LSBFRST BIT(23) 52e4e6ec7bSolivier moysan #define I2S_CFG2_AFCNTR BIT(31) 53e4e6ec7bSolivier moysan 54e4e6ec7bSolivier moysan /* Bit definition for SPI_CFG1 register */ 55e4e6ec7bSolivier moysan #define I2S_CFG1_FTHVL_SHIFT 5 56e4e6ec7bSolivier moysan #define I2S_CFG1_FTHVL_MASK GENMASK(8, I2S_CFG1_FTHVL_SHIFT) 57e4e6ec7bSolivier moysan #define I2S_CFG1_FTHVL_SET(x) ((x) << I2S_CFG1_FTHVL_SHIFT) 58e4e6ec7bSolivier moysan 59e4e6ec7bSolivier moysan #define I2S_CFG1_TXDMAEN BIT(15) 60e4e6ec7bSolivier moysan #define I2S_CFG1_RXDMAEN BIT(14) 61e4e6ec7bSolivier moysan 62e4e6ec7bSolivier moysan /* Bit definition for SPI2S_IER register */ 63e4e6ec7bSolivier moysan #define I2S_IER_RXPIE BIT(0) 64e4e6ec7bSolivier moysan #define I2S_IER_TXPIE BIT(1) 65e4e6ec7bSolivier moysan #define I2S_IER_DPXPIE BIT(2) 66e4e6ec7bSolivier moysan #define I2S_IER_EOTIE BIT(3) 67e4e6ec7bSolivier moysan #define I2S_IER_TXTFIE BIT(4) 68e4e6ec7bSolivier moysan #define I2S_IER_UDRIE BIT(5) 69e4e6ec7bSolivier moysan #define I2S_IER_OVRIE BIT(6) 70e4e6ec7bSolivier moysan #define I2S_IER_CRCEIE BIT(7) 71e4e6ec7bSolivier moysan #define I2S_IER_TIFREIE BIT(8) 72e4e6ec7bSolivier moysan #define I2S_IER_MODFIE BIT(9) 73e4e6ec7bSolivier moysan #define I2S_IER_TSERFIE BIT(10) 74e4e6ec7bSolivier moysan 75e4e6ec7bSolivier moysan /* Bit definition for SPI2S_SR register */ 76e4e6ec7bSolivier moysan #define I2S_SR_RXP BIT(0) 77e4e6ec7bSolivier moysan #define I2S_SR_TXP BIT(1) 78e4e6ec7bSolivier moysan #define I2S_SR_DPXP BIT(2) 79e4e6ec7bSolivier moysan #define I2S_SR_EOT BIT(3) 80e4e6ec7bSolivier moysan #define I2S_SR_TXTF BIT(4) 81e4e6ec7bSolivier moysan #define I2S_SR_UDR BIT(5) 82e4e6ec7bSolivier moysan #define I2S_SR_OVR BIT(6) 83e4e6ec7bSolivier moysan #define I2S_SR_CRCERR BIT(7) 84e4e6ec7bSolivier moysan #define I2S_SR_TIFRE BIT(8) 85e4e6ec7bSolivier moysan #define I2S_SR_MODF BIT(9) 86e4e6ec7bSolivier moysan #define I2S_SR_TSERF BIT(10) 87e4e6ec7bSolivier moysan #define I2S_SR_SUSP BIT(11) 88e4e6ec7bSolivier moysan #define I2S_SR_TXC BIT(12) 89e4e6ec7bSolivier moysan #define I2S_SR_RXPLVL GENMASK(14, 13) 90e4e6ec7bSolivier moysan #define I2S_SR_RXWNE BIT(15) 91e4e6ec7bSolivier moysan 92e4e6ec7bSolivier moysan #define I2S_SR_MASK GENMASK(15, 0) 93e4e6ec7bSolivier moysan 94e4e6ec7bSolivier moysan /* Bit definition for SPI_IFCR register */ 95e4e6ec7bSolivier moysan #define I2S_IFCR_EOTC BIT(3) 96e4e6ec7bSolivier moysan #define I2S_IFCR_TXTFC BIT(4) 97e4e6ec7bSolivier moysan #define I2S_IFCR_UDRC BIT(5) 98e4e6ec7bSolivier moysan #define I2S_IFCR_OVRC BIT(6) 99e4e6ec7bSolivier moysan #define I2S_IFCR_CRCEC BIT(7) 100e4e6ec7bSolivier moysan #define I2S_IFCR_TIFREC BIT(8) 101e4e6ec7bSolivier moysan #define I2S_IFCR_MODFC BIT(9) 102e4e6ec7bSolivier moysan #define I2S_IFCR_TSERFC BIT(10) 103e4e6ec7bSolivier moysan #define I2S_IFCR_SUSPC BIT(11) 104e4e6ec7bSolivier moysan 105e4e6ec7bSolivier moysan #define I2S_IFCR_MASK GENMASK(11, 3) 106e4e6ec7bSolivier moysan 107e4e6ec7bSolivier moysan /* Bit definition for SPI_I2SCGFR register */ 108e4e6ec7bSolivier moysan #define I2S_CGFR_I2SMOD BIT(0) 109e4e6ec7bSolivier moysan 110e4e6ec7bSolivier moysan #define I2S_CGFR_I2SCFG_SHIFT 1 111e4e6ec7bSolivier moysan #define I2S_CGFR_I2SCFG_MASK GENMASK(3, I2S_CGFR_I2SCFG_SHIFT) 112e4e6ec7bSolivier moysan #define I2S_CGFR_I2SCFG_SET(x) ((x) << I2S_CGFR_I2SCFG_SHIFT) 113e4e6ec7bSolivier moysan 114e4e6ec7bSolivier moysan #define I2S_CGFR_I2SSTD_SHIFT 4 115e4e6ec7bSolivier moysan #define I2S_CGFR_I2SSTD_MASK GENMASK(5, I2S_CGFR_I2SSTD_SHIFT) 116e4e6ec7bSolivier moysan #define I2S_CGFR_I2SSTD_SET(x) ((x) << I2S_CGFR_I2SSTD_SHIFT) 117e4e6ec7bSolivier moysan 118e4e6ec7bSolivier moysan #define I2S_CGFR_PCMSYNC BIT(7) 119e4e6ec7bSolivier moysan 120e4e6ec7bSolivier moysan #define I2S_CGFR_DATLEN_SHIFT 8 121e4e6ec7bSolivier moysan #define I2S_CGFR_DATLEN_MASK GENMASK(9, I2S_CGFR_DATLEN_SHIFT) 122e4e6ec7bSolivier moysan #define I2S_CGFR_DATLEN_SET(x) ((x) << I2S_CGFR_DATLEN_SHIFT) 123e4e6ec7bSolivier moysan 124e4e6ec7bSolivier moysan #define I2S_CGFR_CHLEN_SHIFT 10 125e4e6ec7bSolivier moysan #define I2S_CGFR_CHLEN BIT(I2S_CGFR_CHLEN_SHIFT) 126e4e6ec7bSolivier moysan #define I2S_CGFR_CKPOL BIT(11) 127e4e6ec7bSolivier moysan #define I2S_CGFR_FIXCH BIT(12) 128e4e6ec7bSolivier moysan #define I2S_CGFR_WSINV BIT(13) 129e4e6ec7bSolivier moysan #define I2S_CGFR_DATFMT BIT(14) 130e4e6ec7bSolivier moysan 131e4e6ec7bSolivier moysan #define I2S_CGFR_I2SDIV_SHIFT 16 132e4e6ec7bSolivier moysan #define I2S_CGFR_I2SDIV_BIT_H 23 133e4e6ec7bSolivier moysan #define I2S_CGFR_I2SDIV_MASK GENMASK(I2S_CGFR_I2SDIV_BIT_H,\ 134e4e6ec7bSolivier moysan I2S_CGFR_I2SDIV_SHIFT) 135e4e6ec7bSolivier moysan #define I2S_CGFR_I2SDIV_SET(x) ((x) << I2S_CGFR_I2SDIV_SHIFT) 136e4e6ec7bSolivier moysan #define I2S_CGFR_I2SDIV_MAX ((1 << (I2S_CGFR_I2SDIV_BIT_H -\ 137e4e6ec7bSolivier moysan I2S_CGFR_I2SDIV_SHIFT)) - 1) 138e4e6ec7bSolivier moysan 139e4e6ec7bSolivier moysan #define I2S_CGFR_ODD_SHIFT 24 140e4e6ec7bSolivier moysan #define I2S_CGFR_ODD BIT(I2S_CGFR_ODD_SHIFT) 141e4e6ec7bSolivier moysan #define I2S_CGFR_MCKOE BIT(25) 142e4e6ec7bSolivier moysan 14371ed4bddSOlivier Moysan /* Registers below apply to I2S version 1.1 and more */ 14471ed4bddSOlivier Moysan 14571ed4bddSOlivier Moysan /* Bit definition for SPI_HWCFGR register */ 14671ed4bddSOlivier Moysan #define I2S_HWCFGR_I2S_SUPPORT_MASK GENMASK(15, 12) 14771ed4bddSOlivier Moysan 14871ed4bddSOlivier Moysan /* Bit definition for SPI_VERR register */ 14971ed4bddSOlivier Moysan #define I2S_VERR_MIN_MASK GENMASK(3, 0) 15071ed4bddSOlivier Moysan #define I2S_VERR_MAJ_MASK GENMASK(7, 4) 15171ed4bddSOlivier Moysan 15271ed4bddSOlivier Moysan /* Bit definition for SPI_IPIDR register */ 15371ed4bddSOlivier Moysan #define I2S_IPIDR_ID_MASK GENMASK(31, 0) 15471ed4bddSOlivier Moysan 15571ed4bddSOlivier Moysan /* Bit definition for SPI_SIDR register */ 15671ed4bddSOlivier Moysan #define I2S_SIDR_ID_MASK GENMASK(31, 0) 15771ed4bddSOlivier Moysan 15871ed4bddSOlivier Moysan #define I2S_IPIDR_NUMBER 0x00130022 15971ed4bddSOlivier Moysan 160e4e6ec7bSolivier moysan enum i2s_master_mode { 161e4e6ec7bSolivier moysan I2S_MS_NOT_SET, 162e4e6ec7bSolivier moysan I2S_MS_MASTER, 163e4e6ec7bSolivier moysan I2S_MS_SLAVE, 164e4e6ec7bSolivier moysan }; 165e4e6ec7bSolivier moysan 166e4e6ec7bSolivier moysan enum i2s_mode { 167e4e6ec7bSolivier moysan I2S_I2SMOD_TX_SLAVE, 168e4e6ec7bSolivier moysan I2S_I2SMOD_RX_SLAVE, 169e4e6ec7bSolivier moysan I2S_I2SMOD_TX_MASTER, 170e4e6ec7bSolivier moysan I2S_I2SMOD_RX_MASTER, 171e4e6ec7bSolivier moysan I2S_I2SMOD_FD_SLAVE, 172e4e6ec7bSolivier moysan I2S_I2SMOD_FD_MASTER, 173e4e6ec7bSolivier moysan }; 174e4e6ec7bSolivier moysan 175e4e6ec7bSolivier moysan enum i2s_fifo_th { 176e4e6ec7bSolivier moysan I2S_FIFO_TH_NONE, 177e4e6ec7bSolivier moysan I2S_FIFO_TH_ONE_QUARTER, 178e4e6ec7bSolivier moysan I2S_FIFO_TH_HALF, 179e4e6ec7bSolivier moysan I2S_FIFO_TH_THREE_QUARTER, 180e4e6ec7bSolivier moysan I2S_FIFO_TH_FULL, 181e4e6ec7bSolivier moysan }; 182e4e6ec7bSolivier moysan 183e4e6ec7bSolivier moysan enum i2s_std { 184e4e6ec7bSolivier moysan I2S_STD_I2S, 185e4e6ec7bSolivier moysan I2S_STD_LEFT_J, 186e4e6ec7bSolivier moysan I2S_STD_RIGHT_J, 187e4e6ec7bSolivier moysan I2S_STD_DSP, 188e4e6ec7bSolivier moysan }; 189e4e6ec7bSolivier moysan 190e4e6ec7bSolivier moysan enum i2s_datlen { 191e4e6ec7bSolivier moysan I2S_I2SMOD_DATLEN_16, 192e4e6ec7bSolivier moysan I2S_I2SMOD_DATLEN_24, 193e4e6ec7bSolivier moysan I2S_I2SMOD_DATLEN_32, 194e4e6ec7bSolivier moysan }; 195e4e6ec7bSolivier moysan 196e4e6ec7bSolivier moysan #define STM32_I2S_FIFO_SIZE 16 197e4e6ec7bSolivier moysan 198e4e6ec7bSolivier moysan #define STM32_I2S_IS_MASTER(x) ((x)->ms_flg == I2S_MS_MASTER) 199e4e6ec7bSolivier moysan #define STM32_I2S_IS_SLAVE(x) ((x)->ms_flg == I2S_MS_SLAVE) 200e4e6ec7bSolivier moysan 2018a262e61SOlivier Moysan #define STM32_I2S_NAME_LEN 32 2028a262e61SOlivier Moysan #define STM32_I2S_RATE_11K 11025 2038a262e61SOlivier Moysan 204e4e6ec7bSolivier moysan /** 205307cce4aSOlivier Moysan * struct stm32_i2s_data - private data of I2S 206e4e6ec7bSolivier moysan * @regmap_conf: I2S register map configuration pointer 207307cce4aSOlivier Moysan * @regmap: I2S register map pointer 208e4e6ec7bSolivier moysan * @pdev: device data pointer 209e4e6ec7bSolivier moysan * @dai_drv: DAI driver pointer 210e4e6ec7bSolivier moysan * @dma_data_tx: dma configuration data for tx channel 211e4e6ec7bSolivier moysan * @dma_data_rx: dma configuration data for tx channel 212e4e6ec7bSolivier moysan * @substream: PCM substream data pointer 213e4e6ec7bSolivier moysan * @i2sclk: kernel clock feeding the I2S clock generator 2148a262e61SOlivier Moysan * @i2smclk: master clock from I2S mclk provider 215e4e6ec7bSolivier moysan * @pclk: peripheral clock driving bus interface 216e4e6ec7bSolivier moysan * @x8kclk: I2S parent clock for sampling frequencies multiple of 8kHz 217e4e6ec7bSolivier moysan * @x11kclk: I2S parent clock for sampling frequencies multiple of 11kHz 218e4e6ec7bSolivier moysan * @base: mmio register base virtual address 219e4e6ec7bSolivier moysan * @phys_addr: I2S registers physical base address 220e4e6ec7bSolivier moysan * @lock_fd: lock to manage race conditions in full duplex mode 2213005decfSOlivier Moysan * @irq_lock: prevent race condition with IRQ 222e4e6ec7bSolivier moysan * @mclk_rate: master clock frequency (Hz) 223e4e6ec7bSolivier moysan * @fmt: DAI protocol 2248a262e61SOlivier Moysan * @divider: prescaler division ratio 2258a262e61SOlivier Moysan * @div: prescaler div field 2268a262e61SOlivier Moysan * @odd: prescaler odd field 227e4e6ec7bSolivier moysan * @refcount: keep count of opened streams on I2S 228e4e6ec7bSolivier moysan * @ms_flg: master mode flag. 229e4e6ec7bSolivier moysan */ 230e4e6ec7bSolivier moysan struct stm32_i2s_data { 231e4e6ec7bSolivier moysan const struct regmap_config *regmap_conf; 232e4e6ec7bSolivier moysan struct regmap *regmap; 233e4e6ec7bSolivier moysan struct platform_device *pdev; 234e4e6ec7bSolivier moysan struct snd_soc_dai_driver *dai_drv; 235e4e6ec7bSolivier moysan struct snd_dmaengine_dai_dma_data dma_data_tx; 236e4e6ec7bSolivier moysan struct snd_dmaengine_dai_dma_data dma_data_rx; 237e4e6ec7bSolivier moysan struct snd_pcm_substream *substream; 238e4e6ec7bSolivier moysan struct clk *i2sclk; 2398a262e61SOlivier Moysan struct clk *i2smclk; 240e4e6ec7bSolivier moysan struct clk *pclk; 241e4e6ec7bSolivier moysan struct clk *x8kclk; 242e4e6ec7bSolivier moysan struct clk *x11kclk; 243e4e6ec7bSolivier moysan void __iomem *base; 244e4e6ec7bSolivier moysan dma_addr_t phys_addr; 245e4e6ec7bSolivier moysan spinlock_t lock_fd; /* Manage race conditions for full duplex */ 2463005decfSOlivier Moysan spinlock_t irq_lock; /* used to prevent race condition with IRQ */ 247e4e6ec7bSolivier moysan unsigned int mclk_rate; 248e4e6ec7bSolivier moysan unsigned int fmt; 2498a262e61SOlivier Moysan unsigned int divider; 2508a262e61SOlivier Moysan unsigned int div; 2518a262e61SOlivier Moysan bool odd; 252e4e6ec7bSolivier moysan int refcount; 253e4e6ec7bSolivier moysan int ms_flg; 254e4e6ec7bSolivier moysan }; 255e4e6ec7bSolivier moysan 2568a262e61SOlivier Moysan struct stm32_i2smclk_data { 2578a262e61SOlivier Moysan struct clk_hw hw; 2588a262e61SOlivier Moysan unsigned long freq; 2598a262e61SOlivier Moysan struct stm32_i2s_data *i2s_data; 2608a262e61SOlivier Moysan }; 2618a262e61SOlivier Moysan 2628a262e61SOlivier Moysan #define to_mclk_data(_hw) container_of(_hw, struct stm32_i2smclk_data, hw) 2638a262e61SOlivier Moysan 2648a262e61SOlivier Moysan static int stm32_i2s_calc_clk_div(struct stm32_i2s_data *i2s, 2658a262e61SOlivier Moysan unsigned long input_rate, 2668a262e61SOlivier Moysan unsigned long output_rate) 2678a262e61SOlivier Moysan { 2688a262e61SOlivier Moysan unsigned int ratio, div, divider = 1; 2698a262e61SOlivier Moysan bool odd; 2708a262e61SOlivier Moysan 2718a262e61SOlivier Moysan ratio = DIV_ROUND_CLOSEST(input_rate, output_rate); 2728a262e61SOlivier Moysan 2738a262e61SOlivier Moysan /* Check the parity of the divider */ 2748a262e61SOlivier Moysan odd = ratio & 0x1; 2758a262e61SOlivier Moysan 2768a262e61SOlivier Moysan /* Compute the div prescaler */ 2778a262e61SOlivier Moysan div = ratio >> 1; 2788a262e61SOlivier Moysan 2798a262e61SOlivier Moysan /* If div is 0 actual divider is 1 */ 2808a262e61SOlivier Moysan if (div) { 2818a262e61SOlivier Moysan divider = ((2 * div) + odd); 2828a262e61SOlivier Moysan dev_dbg(&i2s->pdev->dev, "Divider: 2*%d(div)+%d(odd) = %d\n", 2838a262e61SOlivier Moysan div, odd, divider); 2848a262e61SOlivier Moysan } 2858a262e61SOlivier Moysan 2868a262e61SOlivier Moysan /* Division by three is not allowed by I2S prescaler */ 2878a262e61SOlivier Moysan if ((div == 1 && odd) || div > I2S_CGFR_I2SDIV_MAX) { 2888a262e61SOlivier Moysan dev_err(&i2s->pdev->dev, "Wrong divider setting\n"); 2898a262e61SOlivier Moysan return -EINVAL; 2908a262e61SOlivier Moysan } 2918a262e61SOlivier Moysan 2928a262e61SOlivier Moysan if (input_rate % divider) 2938a262e61SOlivier Moysan dev_dbg(&i2s->pdev->dev, 2948a262e61SOlivier Moysan "Rate not accurate. requested (%ld), actual (%ld)\n", 2958a262e61SOlivier Moysan output_rate, input_rate / divider); 2968a262e61SOlivier Moysan 2978a262e61SOlivier Moysan i2s->div = div; 2988a262e61SOlivier Moysan i2s->odd = odd; 2998a262e61SOlivier Moysan i2s->divider = divider; 3008a262e61SOlivier Moysan 3018a262e61SOlivier Moysan return 0; 3028a262e61SOlivier Moysan } 3038a262e61SOlivier Moysan 3048a262e61SOlivier Moysan static int stm32_i2s_set_clk_div(struct stm32_i2s_data *i2s) 3058a262e61SOlivier Moysan { 3068a262e61SOlivier Moysan u32 cgfr, cgfr_mask; 3078a262e61SOlivier Moysan 3088a262e61SOlivier Moysan cgfr = I2S_CGFR_I2SDIV_SET(i2s->div) | (i2s->odd << I2S_CGFR_ODD_SHIFT); 3098a262e61SOlivier Moysan cgfr_mask = I2S_CGFR_I2SDIV_MASK | I2S_CGFR_ODD; 3108a262e61SOlivier Moysan 3118a262e61SOlivier Moysan return regmap_update_bits(i2s->regmap, STM32_I2S_CGFR_REG, 3128a262e61SOlivier Moysan cgfr_mask, cgfr); 3138a262e61SOlivier Moysan } 3148a262e61SOlivier Moysan 3158a262e61SOlivier Moysan static int stm32_i2s_set_parent_clock(struct stm32_i2s_data *i2s, 3168a262e61SOlivier Moysan unsigned int rate) 3178a262e61SOlivier Moysan { 3188a262e61SOlivier Moysan struct platform_device *pdev = i2s->pdev; 3198a262e61SOlivier Moysan struct clk *parent_clk; 3208a262e61SOlivier Moysan int ret; 3218a262e61SOlivier Moysan 3228a262e61SOlivier Moysan if (!(rate % STM32_I2S_RATE_11K)) 3238a262e61SOlivier Moysan parent_clk = i2s->x11kclk; 3248a262e61SOlivier Moysan else 3258a262e61SOlivier Moysan parent_clk = i2s->x8kclk; 3268a262e61SOlivier Moysan 3278a262e61SOlivier Moysan ret = clk_set_parent(i2s->i2sclk, parent_clk); 3288a262e61SOlivier Moysan if (ret) 3298a262e61SOlivier Moysan dev_err(&pdev->dev, 3308a262e61SOlivier Moysan "Error %d setting i2sclk parent clock\n", ret); 3318a262e61SOlivier Moysan 3328a262e61SOlivier Moysan return ret; 3338a262e61SOlivier Moysan } 3348a262e61SOlivier Moysan 3358a262e61SOlivier Moysan static long stm32_i2smclk_round_rate(struct clk_hw *hw, unsigned long rate, 3368a262e61SOlivier Moysan unsigned long *prate) 3378a262e61SOlivier Moysan { 3388a262e61SOlivier Moysan struct stm32_i2smclk_data *mclk = to_mclk_data(hw); 3398a262e61SOlivier Moysan struct stm32_i2s_data *i2s = mclk->i2s_data; 3408a262e61SOlivier Moysan int ret; 3418a262e61SOlivier Moysan 3428a262e61SOlivier Moysan ret = stm32_i2s_calc_clk_div(i2s, *prate, rate); 3438a262e61SOlivier Moysan if (ret) 3448a262e61SOlivier Moysan return ret; 3458a262e61SOlivier Moysan 3468a262e61SOlivier Moysan mclk->freq = *prate / i2s->divider; 3478a262e61SOlivier Moysan 3488a262e61SOlivier Moysan return mclk->freq; 3498a262e61SOlivier Moysan } 3508a262e61SOlivier Moysan 3518a262e61SOlivier Moysan static unsigned long stm32_i2smclk_recalc_rate(struct clk_hw *hw, 3528a262e61SOlivier Moysan unsigned long parent_rate) 3538a262e61SOlivier Moysan { 3548a262e61SOlivier Moysan struct stm32_i2smclk_data *mclk = to_mclk_data(hw); 3558a262e61SOlivier Moysan 3568a262e61SOlivier Moysan return mclk->freq; 3578a262e61SOlivier Moysan } 3588a262e61SOlivier Moysan 3598a262e61SOlivier Moysan static int stm32_i2smclk_set_rate(struct clk_hw *hw, unsigned long rate, 3608a262e61SOlivier Moysan unsigned long parent_rate) 3618a262e61SOlivier Moysan { 3628a262e61SOlivier Moysan struct stm32_i2smclk_data *mclk = to_mclk_data(hw); 3638a262e61SOlivier Moysan struct stm32_i2s_data *i2s = mclk->i2s_data; 3648a262e61SOlivier Moysan int ret; 3658a262e61SOlivier Moysan 3668a262e61SOlivier Moysan ret = stm32_i2s_calc_clk_div(i2s, parent_rate, rate); 3678a262e61SOlivier Moysan if (ret) 3688a262e61SOlivier Moysan return ret; 3698a262e61SOlivier Moysan 3708a262e61SOlivier Moysan ret = stm32_i2s_set_clk_div(i2s); 3718a262e61SOlivier Moysan if (ret) 3728a262e61SOlivier Moysan return ret; 3738a262e61SOlivier Moysan 3748a262e61SOlivier Moysan mclk->freq = rate; 3758a262e61SOlivier Moysan 3768a262e61SOlivier Moysan return 0; 3778a262e61SOlivier Moysan } 3788a262e61SOlivier Moysan 3798a262e61SOlivier Moysan static int stm32_i2smclk_enable(struct clk_hw *hw) 3808a262e61SOlivier Moysan { 3818a262e61SOlivier Moysan struct stm32_i2smclk_data *mclk = to_mclk_data(hw); 3828a262e61SOlivier Moysan struct stm32_i2s_data *i2s = mclk->i2s_data; 3838a262e61SOlivier Moysan 3848a262e61SOlivier Moysan dev_dbg(&i2s->pdev->dev, "Enable master clock\n"); 3858a262e61SOlivier Moysan 3868a262e61SOlivier Moysan return regmap_update_bits(i2s->regmap, STM32_I2S_CGFR_REG, 3878a262e61SOlivier Moysan I2S_CGFR_MCKOE, I2S_CGFR_MCKOE); 3888a262e61SOlivier Moysan } 3898a262e61SOlivier Moysan 3908a262e61SOlivier Moysan static void stm32_i2smclk_disable(struct clk_hw *hw) 3918a262e61SOlivier Moysan { 3928a262e61SOlivier Moysan struct stm32_i2smclk_data *mclk = to_mclk_data(hw); 3938a262e61SOlivier Moysan struct stm32_i2s_data *i2s = mclk->i2s_data; 3948a262e61SOlivier Moysan 3958a262e61SOlivier Moysan dev_dbg(&i2s->pdev->dev, "Disable master clock\n"); 3968a262e61SOlivier Moysan 3978a262e61SOlivier Moysan regmap_update_bits(i2s->regmap, STM32_I2S_CGFR_REG, I2S_CGFR_MCKOE, 0); 3988a262e61SOlivier Moysan } 3998a262e61SOlivier Moysan 4008a262e61SOlivier Moysan static const struct clk_ops mclk_ops = { 4018a262e61SOlivier Moysan .enable = stm32_i2smclk_enable, 4028a262e61SOlivier Moysan .disable = stm32_i2smclk_disable, 4038a262e61SOlivier Moysan .recalc_rate = stm32_i2smclk_recalc_rate, 4048a262e61SOlivier Moysan .round_rate = stm32_i2smclk_round_rate, 4058a262e61SOlivier Moysan .set_rate = stm32_i2smclk_set_rate, 4068a262e61SOlivier Moysan }; 4078a262e61SOlivier Moysan 4088a262e61SOlivier Moysan static int stm32_i2s_add_mclk_provider(struct stm32_i2s_data *i2s) 4098a262e61SOlivier Moysan { 4108a262e61SOlivier Moysan struct clk_hw *hw; 4118a262e61SOlivier Moysan struct stm32_i2smclk_data *mclk; 4128a262e61SOlivier Moysan struct device *dev = &i2s->pdev->dev; 4138a262e61SOlivier Moysan const char *pname = __clk_get_name(i2s->i2sclk); 4148a262e61SOlivier Moysan char *mclk_name, *p, *s = (char *)pname; 4158a262e61SOlivier Moysan int ret, i = 0; 4168a262e61SOlivier Moysan 4178a262e61SOlivier Moysan mclk = devm_kzalloc(dev, sizeof(*mclk), GFP_KERNEL); 4188a262e61SOlivier Moysan if (!mclk) 4198a262e61SOlivier Moysan return -ENOMEM; 4208a262e61SOlivier Moysan 4218a262e61SOlivier Moysan mclk_name = devm_kcalloc(dev, sizeof(char), 4228a262e61SOlivier Moysan STM32_I2S_NAME_LEN, GFP_KERNEL); 4238a262e61SOlivier Moysan if (!mclk_name) 4248a262e61SOlivier Moysan return -ENOMEM; 4258a262e61SOlivier Moysan 4268a262e61SOlivier Moysan /* 4278a262e61SOlivier Moysan * Forge mclk clock name from parent clock name and suffix. 4288a262e61SOlivier Moysan * String after "_" char is stripped in parent name. 4298a262e61SOlivier Moysan */ 4308a262e61SOlivier Moysan p = mclk_name; 4318a262e61SOlivier Moysan while (*s && *s != '_' && (i < (STM32_I2S_NAME_LEN - 7))) { 4328a262e61SOlivier Moysan *p++ = *s++; 4338a262e61SOlivier Moysan i++; 4348a262e61SOlivier Moysan } 4358a262e61SOlivier Moysan strcat(p, "_mclk"); 4368a262e61SOlivier Moysan 4378a262e61SOlivier Moysan mclk->hw.init = CLK_HW_INIT(mclk_name, pname, &mclk_ops, 0); 4388a262e61SOlivier Moysan mclk->i2s_data = i2s; 4398a262e61SOlivier Moysan hw = &mclk->hw; 4408a262e61SOlivier Moysan 4418a262e61SOlivier Moysan dev_dbg(dev, "Register master clock %s\n", mclk_name); 4428a262e61SOlivier Moysan ret = devm_clk_hw_register(&i2s->pdev->dev, hw); 4438a262e61SOlivier Moysan if (ret) { 4448a262e61SOlivier Moysan dev_err(dev, "mclk register fails with error %d\n", ret); 4458a262e61SOlivier Moysan return ret; 4468a262e61SOlivier Moysan } 4478a262e61SOlivier Moysan i2s->i2smclk = hw->clk; 4488a262e61SOlivier Moysan 4498a262e61SOlivier Moysan /* register mclk provider */ 4508a262e61SOlivier Moysan return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, hw); 4518a262e61SOlivier Moysan } 4528a262e61SOlivier Moysan 453e4e6ec7bSolivier moysan static irqreturn_t stm32_i2s_isr(int irq, void *devid) 454e4e6ec7bSolivier moysan { 455e4e6ec7bSolivier moysan struct stm32_i2s_data *i2s = (struct stm32_i2s_data *)devid; 456e4e6ec7bSolivier moysan struct platform_device *pdev = i2s->pdev; 457e4e6ec7bSolivier moysan u32 sr, ier; 458e4e6ec7bSolivier moysan unsigned long flags; 459e4e6ec7bSolivier moysan int err = 0; 460e4e6ec7bSolivier moysan 461e4e6ec7bSolivier moysan regmap_read(i2s->regmap, STM32_I2S_SR_REG, &sr); 462e4e6ec7bSolivier moysan regmap_read(i2s->regmap, STM32_I2S_IER_REG, &ier); 463e4e6ec7bSolivier moysan 464e4e6ec7bSolivier moysan flags = sr & ier; 465e4e6ec7bSolivier moysan if (!flags) { 466e4e6ec7bSolivier moysan dev_dbg(&pdev->dev, "Spurious IRQ sr=0x%08x, ier=0x%08x\n", 467e4e6ec7bSolivier moysan sr, ier); 468e4e6ec7bSolivier moysan return IRQ_NONE; 469e4e6ec7bSolivier moysan } 470e4e6ec7bSolivier moysan 4718ba3c521SOlivier Moysan regmap_write_bits(i2s->regmap, STM32_I2S_IFCR_REG, 472e4e6ec7bSolivier moysan I2S_IFCR_MASK, flags); 473e4e6ec7bSolivier moysan 474e4e6ec7bSolivier moysan if (flags & I2S_SR_OVR) { 475e4e6ec7bSolivier moysan dev_dbg(&pdev->dev, "Overrun\n"); 476e4e6ec7bSolivier moysan err = 1; 477e4e6ec7bSolivier moysan } 478e4e6ec7bSolivier moysan 479e4e6ec7bSolivier moysan if (flags & I2S_SR_UDR) { 480e4e6ec7bSolivier moysan dev_dbg(&pdev->dev, "Underrun\n"); 481e4e6ec7bSolivier moysan err = 1; 482e4e6ec7bSolivier moysan } 483e4e6ec7bSolivier moysan 484e4e6ec7bSolivier moysan if (flags & I2S_SR_TIFRE) 485e4e6ec7bSolivier moysan dev_dbg(&pdev->dev, "Frame error\n"); 486e4e6ec7bSolivier moysan 4873005decfSOlivier Moysan spin_lock(&i2s->irq_lock); 4883005decfSOlivier Moysan if (err && i2s->substream) 489e4e6ec7bSolivier moysan snd_pcm_stop_xrun(i2s->substream); 4903005decfSOlivier Moysan spin_unlock(&i2s->irq_lock); 491e4e6ec7bSolivier moysan 492e4e6ec7bSolivier moysan return IRQ_HANDLED; 493e4e6ec7bSolivier moysan } 494e4e6ec7bSolivier moysan 495e4e6ec7bSolivier moysan static bool stm32_i2s_readable_reg(struct device *dev, unsigned int reg) 496e4e6ec7bSolivier moysan { 497e4e6ec7bSolivier moysan switch (reg) { 498e4e6ec7bSolivier moysan case STM32_I2S_CR1_REG: 499e4e6ec7bSolivier moysan case STM32_I2S_CFG1_REG: 500e4e6ec7bSolivier moysan case STM32_I2S_CFG2_REG: 501e4e6ec7bSolivier moysan case STM32_I2S_IER_REG: 502e4e6ec7bSolivier moysan case STM32_I2S_SR_REG: 503e4e6ec7bSolivier moysan case STM32_I2S_RXDR_REG: 504e4e6ec7bSolivier moysan case STM32_I2S_CGFR_REG: 50571ed4bddSOlivier Moysan case STM32_I2S_HWCFGR_REG: 50671ed4bddSOlivier Moysan case STM32_I2S_VERR_REG: 50771ed4bddSOlivier Moysan case STM32_I2S_IPIDR_REG: 50871ed4bddSOlivier Moysan case STM32_I2S_SIDR_REG: 509e4e6ec7bSolivier moysan return true; 510e4e6ec7bSolivier moysan default: 511e4e6ec7bSolivier moysan return false; 512e4e6ec7bSolivier moysan } 513e4e6ec7bSolivier moysan } 514e4e6ec7bSolivier moysan 515e4e6ec7bSolivier moysan static bool stm32_i2s_volatile_reg(struct device *dev, unsigned int reg) 516e4e6ec7bSolivier moysan { 517e4e6ec7bSolivier moysan switch (reg) { 518a39fe6e2SOlivier Moysan case STM32_I2S_SR_REG: 519e4e6ec7bSolivier moysan case STM32_I2S_RXDR_REG: 520e4e6ec7bSolivier moysan return true; 521e4e6ec7bSolivier moysan default: 522e4e6ec7bSolivier moysan return false; 523e4e6ec7bSolivier moysan } 524e4e6ec7bSolivier moysan } 525e4e6ec7bSolivier moysan 526e4e6ec7bSolivier moysan static bool stm32_i2s_writeable_reg(struct device *dev, unsigned int reg) 527e4e6ec7bSolivier moysan { 528e4e6ec7bSolivier moysan switch (reg) { 529e4e6ec7bSolivier moysan case STM32_I2S_CR1_REG: 530e4e6ec7bSolivier moysan case STM32_I2S_CFG1_REG: 531e4e6ec7bSolivier moysan case STM32_I2S_CFG2_REG: 532e4e6ec7bSolivier moysan case STM32_I2S_IER_REG: 533e4e6ec7bSolivier moysan case STM32_I2S_IFCR_REG: 534e4e6ec7bSolivier moysan case STM32_I2S_TXDR_REG: 535e4e6ec7bSolivier moysan case STM32_I2S_CGFR_REG: 536e4e6ec7bSolivier moysan return true; 537e4e6ec7bSolivier moysan default: 538e4e6ec7bSolivier moysan return false; 539e4e6ec7bSolivier moysan } 540e4e6ec7bSolivier moysan } 541e4e6ec7bSolivier moysan 542e4e6ec7bSolivier moysan static int stm32_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt) 543e4e6ec7bSolivier moysan { 544e4e6ec7bSolivier moysan struct stm32_i2s_data *i2s = snd_soc_dai_get_drvdata(cpu_dai); 545e4e6ec7bSolivier moysan u32 cgfr; 546e4e6ec7bSolivier moysan u32 cgfr_mask = I2S_CGFR_I2SSTD_MASK | I2S_CGFR_CKPOL | 547e4e6ec7bSolivier moysan I2S_CGFR_WSINV | I2S_CGFR_I2SCFG_MASK; 548e4e6ec7bSolivier moysan 549e4e6ec7bSolivier moysan dev_dbg(cpu_dai->dev, "fmt %x\n", fmt); 550e4e6ec7bSolivier moysan 551e4e6ec7bSolivier moysan /* 552e4e6ec7bSolivier moysan * winv = 0 : default behavior (high/low) for all standards 553e4e6ec7bSolivier moysan * ckpol = 0 for all standards. 554e4e6ec7bSolivier moysan */ 555e4e6ec7bSolivier moysan switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 556e4e6ec7bSolivier moysan case SND_SOC_DAIFMT_I2S: 557e4e6ec7bSolivier moysan cgfr = I2S_CGFR_I2SSTD_SET(I2S_STD_I2S); 558e4e6ec7bSolivier moysan break; 559e4e6ec7bSolivier moysan case SND_SOC_DAIFMT_MSB: 560e4e6ec7bSolivier moysan cgfr = I2S_CGFR_I2SSTD_SET(I2S_STD_LEFT_J); 561e4e6ec7bSolivier moysan break; 562e4e6ec7bSolivier moysan case SND_SOC_DAIFMT_LSB: 563e4e6ec7bSolivier moysan cgfr = I2S_CGFR_I2SSTD_SET(I2S_STD_RIGHT_J); 564e4e6ec7bSolivier moysan break; 565e4e6ec7bSolivier moysan case SND_SOC_DAIFMT_DSP_A: 566e4e6ec7bSolivier moysan cgfr = I2S_CGFR_I2SSTD_SET(I2S_STD_DSP); 567e4e6ec7bSolivier moysan break; 568e4e6ec7bSolivier moysan /* DSP_B not mapped on I2S PCM long format. 1 bit offset does not fit */ 569e4e6ec7bSolivier moysan default: 570e4e6ec7bSolivier moysan dev_err(cpu_dai->dev, "Unsupported protocol %#x\n", 571e4e6ec7bSolivier moysan fmt & SND_SOC_DAIFMT_FORMAT_MASK); 572e4e6ec7bSolivier moysan return -EINVAL; 573e4e6ec7bSolivier moysan } 574e4e6ec7bSolivier moysan 575e4e6ec7bSolivier moysan /* DAI clock strobing */ 576e4e6ec7bSolivier moysan switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 577e4e6ec7bSolivier moysan case SND_SOC_DAIFMT_NB_NF: 578e4e6ec7bSolivier moysan break; 579e4e6ec7bSolivier moysan case SND_SOC_DAIFMT_IB_NF: 580e4e6ec7bSolivier moysan cgfr |= I2S_CGFR_CKPOL; 581e4e6ec7bSolivier moysan break; 582e4e6ec7bSolivier moysan case SND_SOC_DAIFMT_NB_IF: 583e4e6ec7bSolivier moysan cgfr |= I2S_CGFR_WSINV; 584e4e6ec7bSolivier moysan break; 585e4e6ec7bSolivier moysan case SND_SOC_DAIFMT_IB_IF: 586e4e6ec7bSolivier moysan cgfr |= I2S_CGFR_CKPOL; 587e4e6ec7bSolivier moysan cgfr |= I2S_CGFR_WSINV; 588e4e6ec7bSolivier moysan break; 589e4e6ec7bSolivier moysan default: 590e4e6ec7bSolivier moysan dev_err(cpu_dai->dev, "Unsupported strobing %#x\n", 591e4e6ec7bSolivier moysan fmt & SND_SOC_DAIFMT_INV_MASK); 592e4e6ec7bSolivier moysan return -EINVAL; 593e4e6ec7bSolivier moysan } 594e4e6ec7bSolivier moysan 595e4e6ec7bSolivier moysan /* DAI clock master masks */ 5960092dac9SCharles Keepax switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) { 5970092dac9SCharles Keepax case SND_SOC_DAIFMT_BC_FC: 598e4e6ec7bSolivier moysan i2s->ms_flg = I2S_MS_SLAVE; 599e4e6ec7bSolivier moysan break; 6000092dac9SCharles Keepax case SND_SOC_DAIFMT_BP_FP: 601e4e6ec7bSolivier moysan i2s->ms_flg = I2S_MS_MASTER; 602e4e6ec7bSolivier moysan break; 603e4e6ec7bSolivier moysan default: 604e4e6ec7bSolivier moysan dev_err(cpu_dai->dev, "Unsupported mode %#x\n", 6050092dac9SCharles Keepax fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK); 606e4e6ec7bSolivier moysan return -EINVAL; 607e4e6ec7bSolivier moysan } 608e4e6ec7bSolivier moysan 609e4e6ec7bSolivier moysan i2s->fmt = fmt; 610e4e6ec7bSolivier moysan return regmap_update_bits(i2s->regmap, STM32_I2S_CGFR_REG, 611e4e6ec7bSolivier moysan cgfr_mask, cgfr); 612e4e6ec7bSolivier moysan } 613e4e6ec7bSolivier moysan 614e4e6ec7bSolivier moysan static int stm32_i2s_set_sysclk(struct snd_soc_dai *cpu_dai, 615e4e6ec7bSolivier moysan int clk_id, unsigned int freq, int dir) 616e4e6ec7bSolivier moysan { 617e4e6ec7bSolivier moysan struct stm32_i2s_data *i2s = snd_soc_dai_get_drvdata(cpu_dai); 6188a262e61SOlivier Moysan int ret = 0; 619e4e6ec7bSolivier moysan 6208a262e61SOlivier Moysan dev_dbg(cpu_dai->dev, "I2S MCLK frequency is %uHz. mode: %s, dir: %s\n", 6218a262e61SOlivier Moysan freq, STM32_I2S_IS_MASTER(i2s) ? "master" : "slave", 6228a262e61SOlivier Moysan dir ? "output" : "input"); 623e4e6ec7bSolivier moysan 6248a262e61SOlivier Moysan /* MCLK generation is available only in master mode */ 6258a262e61SOlivier Moysan if (dir == SND_SOC_CLOCK_OUT && STM32_I2S_IS_MASTER(i2s)) { 6268a262e61SOlivier Moysan if (!i2s->i2smclk) { 6278a262e61SOlivier Moysan dev_dbg(cpu_dai->dev, "No MCLK registered\n"); 6288a262e61SOlivier Moysan return 0; 629e4e6ec7bSolivier moysan } 630e4e6ec7bSolivier moysan 6318a262e61SOlivier Moysan /* Assume shutdown if requested frequency is 0Hz */ 6328a262e61SOlivier Moysan if (!freq) { 6338a262e61SOlivier Moysan /* Release mclk rate only if rate was actually set */ 6348a262e61SOlivier Moysan if (i2s->mclk_rate) { 6358a262e61SOlivier Moysan clk_rate_exclusive_put(i2s->i2smclk); 6368a262e61SOlivier Moysan i2s->mclk_rate = 0; 6378a262e61SOlivier Moysan } 6388a262e61SOlivier Moysan return regmap_update_bits(i2s->regmap, 6398a262e61SOlivier Moysan STM32_I2S_CGFR_REG, 6408a262e61SOlivier Moysan I2S_CGFR_MCKOE, 0); 6418a262e61SOlivier Moysan } 6428a262e61SOlivier Moysan /* If master clock is used, set parent clock now */ 6438a262e61SOlivier Moysan ret = stm32_i2s_set_parent_clock(i2s, freq); 6448a262e61SOlivier Moysan if (ret) 6458a262e61SOlivier Moysan return ret; 6468a262e61SOlivier Moysan ret = clk_set_rate_exclusive(i2s->i2smclk, freq); 6478a262e61SOlivier Moysan if (ret) { 6488a262e61SOlivier Moysan dev_err(cpu_dai->dev, "Could not set mclk rate\n"); 6498a262e61SOlivier Moysan return ret; 6508a262e61SOlivier Moysan } 6518a262e61SOlivier Moysan ret = regmap_update_bits(i2s->regmap, STM32_I2S_CGFR_REG, 6528a262e61SOlivier Moysan I2S_CGFR_MCKOE, I2S_CGFR_MCKOE); 6538a262e61SOlivier Moysan if (!ret) 6548a262e61SOlivier Moysan i2s->mclk_rate = freq; 6558a262e61SOlivier Moysan } 6568a262e61SOlivier Moysan 6578a262e61SOlivier Moysan return ret; 658e4e6ec7bSolivier moysan } 659e4e6ec7bSolivier moysan 660e4e6ec7bSolivier moysan static int stm32_i2s_configure_clock(struct snd_soc_dai *cpu_dai, 661e4e6ec7bSolivier moysan struct snd_pcm_hw_params *params) 662e4e6ec7bSolivier moysan { 663e4e6ec7bSolivier moysan struct stm32_i2s_data *i2s = snd_soc_dai_get_drvdata(cpu_dai); 664e4e6ec7bSolivier moysan unsigned long i2s_clock_rate; 6658a262e61SOlivier Moysan unsigned int nb_bits, frame_len; 666e4e6ec7bSolivier moysan unsigned int rate = params_rate(params); 6678a262e61SOlivier Moysan u32 cgfr; 668e4e6ec7bSolivier moysan int ret; 669e4e6ec7bSolivier moysan 670e4e6ec7bSolivier moysan if (!(rate % 11025)) 671e4e6ec7bSolivier moysan clk_set_parent(i2s->i2sclk, i2s->x11kclk); 672e4e6ec7bSolivier moysan else 673e4e6ec7bSolivier moysan clk_set_parent(i2s->i2sclk, i2s->x8kclk); 674e4e6ec7bSolivier moysan i2s_clock_rate = clk_get_rate(i2s->i2sclk); 675e4e6ec7bSolivier moysan 676e4e6ec7bSolivier moysan /* 677e4e6ec7bSolivier moysan * mckl = mclk_ratio x ws 678e4e6ec7bSolivier moysan * i2s mode : mclk_ratio = 256 679e4e6ec7bSolivier moysan * dsp mode : mclk_ratio = 128 680e4e6ec7bSolivier moysan * 681e4e6ec7bSolivier moysan * mclk on 682e4e6ec7bSolivier moysan * i2s mode : div = i2s_clk / (mclk_ratio * ws) 683e4e6ec7bSolivier moysan * dsp mode : div = i2s_clk / (mclk_ratio * ws) 684e4e6ec7bSolivier moysan * mclk off 685e4e6ec7bSolivier moysan * i2s mode : div = i2s_clk / (nb_bits x ws) 686e4e6ec7bSolivier moysan * dsp mode : div = i2s_clk / (nb_bits x ws) 687e4e6ec7bSolivier moysan */ 688e4e6ec7bSolivier moysan if (i2s->mclk_rate) { 6898a262e61SOlivier Moysan ret = stm32_i2s_calc_clk_div(i2s, i2s_clock_rate, 6908a262e61SOlivier Moysan i2s->mclk_rate); 6918a262e61SOlivier Moysan if (ret) 6928a262e61SOlivier Moysan return ret; 693e4e6ec7bSolivier moysan } else { 694e4e6ec7bSolivier moysan frame_len = 32; 695e4e6ec7bSolivier moysan if ((i2s->fmt & SND_SOC_DAIFMT_FORMAT_MASK) == 696e4e6ec7bSolivier moysan SND_SOC_DAIFMT_DSP_A) 697e4e6ec7bSolivier moysan frame_len = 16; 698e4e6ec7bSolivier moysan 699e4e6ec7bSolivier moysan /* master clock not enabled */ 700e4e6ec7bSolivier moysan ret = regmap_read(i2s->regmap, STM32_I2S_CGFR_REG, &cgfr); 701e4e6ec7bSolivier moysan if (ret < 0) 702e4e6ec7bSolivier moysan return ret; 703e4e6ec7bSolivier moysan 704424fe7edSOlivier Moysan nb_bits = frame_len * (FIELD_GET(I2S_CGFR_CHLEN, cgfr) + 1); 7058a262e61SOlivier Moysan ret = stm32_i2s_calc_clk_div(i2s, i2s_clock_rate, 7068a262e61SOlivier Moysan (nb_bits * rate)); 7078a262e61SOlivier Moysan if (ret) 7088a262e61SOlivier Moysan return ret; 709e4e6ec7bSolivier moysan } 710e4e6ec7bSolivier moysan 7118a262e61SOlivier Moysan ret = stm32_i2s_set_clk_div(i2s); 712e4e6ec7bSolivier moysan if (ret < 0) 713e4e6ec7bSolivier moysan return ret; 714e4e6ec7bSolivier moysan 715e4e6ec7bSolivier moysan /* Set bitclock and frameclock to their inactive state */ 716e4e6ec7bSolivier moysan return regmap_update_bits(i2s->regmap, STM32_I2S_CFG2_REG, 717e4e6ec7bSolivier moysan I2S_CFG2_AFCNTR, I2S_CFG2_AFCNTR); 718e4e6ec7bSolivier moysan } 719e4e6ec7bSolivier moysan 720e4e6ec7bSolivier moysan static int stm32_i2s_configure(struct snd_soc_dai *cpu_dai, 721e4e6ec7bSolivier moysan struct snd_pcm_hw_params *params, 722e4e6ec7bSolivier moysan struct snd_pcm_substream *substream) 723e4e6ec7bSolivier moysan { 724e4e6ec7bSolivier moysan struct stm32_i2s_data *i2s = snd_soc_dai_get_drvdata(cpu_dai); 725e4e6ec7bSolivier moysan int format = params_width(params); 7261ac2bd16SOlivier Moysan u32 cfgr, cfgr_mask, cfg1; 727e4e6ec7bSolivier moysan unsigned int fthlv; 728e4e6ec7bSolivier moysan int ret; 729e4e6ec7bSolivier moysan 730e4e6ec7bSolivier moysan switch (format) { 731e4e6ec7bSolivier moysan case 16: 732e4e6ec7bSolivier moysan cfgr = I2S_CGFR_DATLEN_SET(I2S_I2SMOD_DATLEN_16); 7330c4c68d6SOlivier Moysan cfgr_mask = I2S_CGFR_DATLEN_MASK | I2S_CGFR_CHLEN; 734e4e6ec7bSolivier moysan break; 735e4e6ec7bSolivier moysan case 32: 736e4e6ec7bSolivier moysan cfgr = I2S_CGFR_DATLEN_SET(I2S_I2SMOD_DATLEN_32) | 737e4e6ec7bSolivier moysan I2S_CGFR_CHLEN; 738e4e6ec7bSolivier moysan cfgr_mask = I2S_CGFR_DATLEN_MASK | I2S_CGFR_CHLEN; 739e4e6ec7bSolivier moysan break; 740e4e6ec7bSolivier moysan default: 741e4e6ec7bSolivier moysan dev_err(cpu_dai->dev, "Unexpected format %d", format); 742e4e6ec7bSolivier moysan return -EINVAL; 743e4e6ec7bSolivier moysan } 744e4e6ec7bSolivier moysan 745e4e6ec7bSolivier moysan if (STM32_I2S_IS_SLAVE(i2s)) { 746e7cc49b8Solivier moysan cfgr |= I2S_CGFR_I2SCFG_SET(I2S_I2SMOD_FD_SLAVE); 747e4e6ec7bSolivier moysan 748e4e6ec7bSolivier moysan /* As data length is either 16 or 32 bits, fixch always set */ 749e4e6ec7bSolivier moysan cfgr |= I2S_CGFR_FIXCH; 750e4e6ec7bSolivier moysan cfgr_mask |= I2S_CGFR_FIXCH; 751e4e6ec7bSolivier moysan } else { 752e7cc49b8Solivier moysan cfgr |= I2S_CGFR_I2SCFG_SET(I2S_I2SMOD_FD_MASTER); 753e4e6ec7bSolivier moysan } 754e4e6ec7bSolivier moysan cfgr_mask |= I2S_CGFR_I2SCFG_MASK; 755e4e6ec7bSolivier moysan 756e4e6ec7bSolivier moysan ret = regmap_update_bits(i2s->regmap, STM32_I2S_CGFR_REG, 757e4e6ec7bSolivier moysan cfgr_mask, cfgr); 758e4e6ec7bSolivier moysan if (ret < 0) 759e4e6ec7bSolivier moysan return ret; 760e4e6ec7bSolivier moysan 761e4e6ec7bSolivier moysan fthlv = STM32_I2S_FIFO_SIZE * I2S_FIFO_TH_ONE_QUARTER / 4; 7621ac2bd16SOlivier Moysan cfg1 = I2S_CFG1_FTHVL_SET(fthlv - 1); 763e4e6ec7bSolivier moysan 764e4e6ec7bSolivier moysan return regmap_update_bits(i2s->regmap, STM32_I2S_CFG1_REG, 7651ac2bd16SOlivier Moysan I2S_CFG1_FTHVL_MASK, cfg1); 766e4e6ec7bSolivier moysan } 767e4e6ec7bSolivier moysan 768e4e6ec7bSolivier moysan static int stm32_i2s_startup(struct snd_pcm_substream *substream, 769e4e6ec7bSolivier moysan struct snd_soc_dai *cpu_dai) 770e4e6ec7bSolivier moysan { 771e4e6ec7bSolivier moysan struct stm32_i2s_data *i2s = snd_soc_dai_get_drvdata(cpu_dai); 7723005decfSOlivier Moysan unsigned long flags; 7736a68eeeeSOlivier Moysan int ret; 774e4e6ec7bSolivier moysan 7753005decfSOlivier Moysan spin_lock_irqsave(&i2s->irq_lock, flags); 776e4e6ec7bSolivier moysan i2s->substream = substream; 7773005decfSOlivier Moysan spin_unlock_irqrestore(&i2s->irq_lock, flags); 778e4e6ec7bSolivier moysan 779c2dc8b2cSOlivier Moysan if ((i2s->fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_DSP_A) 780c2dc8b2cSOlivier Moysan snd_pcm_hw_constraint_single(substream->runtime, 781c2dc8b2cSOlivier Moysan SNDRV_PCM_HW_PARAM_CHANNELS, 2); 782c2dc8b2cSOlivier Moysan 7836a68eeeeSOlivier Moysan ret = clk_prepare_enable(i2s->i2sclk); 7846a68eeeeSOlivier Moysan if (ret < 0) { 7856a68eeeeSOlivier Moysan dev_err(cpu_dai->dev, "Failed to enable clock: %d\n", ret); 7866a68eeeeSOlivier Moysan return ret; 7876a68eeeeSOlivier Moysan } 7886a68eeeeSOlivier Moysan 7898ba3c521SOlivier Moysan return regmap_write_bits(i2s->regmap, STM32_I2S_IFCR_REG, 790e4e6ec7bSolivier moysan I2S_IFCR_MASK, I2S_IFCR_MASK); 791e4e6ec7bSolivier moysan } 792e4e6ec7bSolivier moysan 793e4e6ec7bSolivier moysan static int stm32_i2s_hw_params(struct snd_pcm_substream *substream, 794e4e6ec7bSolivier moysan struct snd_pcm_hw_params *params, 795e4e6ec7bSolivier moysan struct snd_soc_dai *cpu_dai) 796e4e6ec7bSolivier moysan { 797e4e6ec7bSolivier moysan struct stm32_i2s_data *i2s = snd_soc_dai_get_drvdata(cpu_dai); 798e4e6ec7bSolivier moysan int ret; 799e4e6ec7bSolivier moysan 800e4e6ec7bSolivier moysan ret = stm32_i2s_configure(cpu_dai, params, substream); 801e4e6ec7bSolivier moysan if (ret < 0) { 802e4e6ec7bSolivier moysan dev_err(cpu_dai->dev, "Configuration returned error %d\n", ret); 803e4e6ec7bSolivier moysan return ret; 804e4e6ec7bSolivier moysan } 805e4e6ec7bSolivier moysan 806e4e6ec7bSolivier moysan if (STM32_I2S_IS_MASTER(i2s)) 807e4e6ec7bSolivier moysan ret = stm32_i2s_configure_clock(cpu_dai, params); 808e4e6ec7bSolivier moysan 809e4e6ec7bSolivier moysan return ret; 810e4e6ec7bSolivier moysan } 811e4e6ec7bSolivier moysan 812e4e6ec7bSolivier moysan static int stm32_i2s_trigger(struct snd_pcm_substream *substream, int cmd, 813e4e6ec7bSolivier moysan struct snd_soc_dai *cpu_dai) 814e4e6ec7bSolivier moysan { 815e4e6ec7bSolivier moysan struct stm32_i2s_data *i2s = snd_soc_dai_get_drvdata(cpu_dai); 816e4e6ec7bSolivier moysan bool playback_flg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); 817e7cc49b8Solivier moysan u32 cfg1_mask, ier; 818e4e6ec7bSolivier moysan int ret; 819e4e6ec7bSolivier moysan 820e4e6ec7bSolivier moysan switch (cmd) { 821e4e6ec7bSolivier moysan case SNDRV_PCM_TRIGGER_START: 822e4e6ec7bSolivier moysan case SNDRV_PCM_TRIGGER_RESUME: 823e4e6ec7bSolivier moysan case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 824e4e6ec7bSolivier moysan /* Enable i2s */ 82552e7306cSOlivier Moysan dev_dbg(cpu_dai->dev, "start I2S %s\n", 82652e7306cSOlivier Moysan playback_flg ? "playback" : "capture"); 827e4e6ec7bSolivier moysan 8281ac2bd16SOlivier Moysan cfg1_mask = I2S_CFG1_RXDMAEN | I2S_CFG1_TXDMAEN; 8291ac2bd16SOlivier Moysan regmap_update_bits(i2s->regmap, STM32_I2S_CFG1_REG, 8301ac2bd16SOlivier Moysan cfg1_mask, cfg1_mask); 8311ac2bd16SOlivier Moysan 832e4e6ec7bSolivier moysan ret = regmap_update_bits(i2s->regmap, STM32_I2S_CR1_REG, 833e4e6ec7bSolivier moysan I2S_CR1_SPE, I2S_CR1_SPE); 834e4e6ec7bSolivier moysan if (ret < 0) { 835e4e6ec7bSolivier moysan dev_err(cpu_dai->dev, "Error %d enabling I2S\n", ret); 836e4e6ec7bSolivier moysan return ret; 837e4e6ec7bSolivier moysan } 838e4e6ec7bSolivier moysan 839307cce4aSOlivier Moysan ret = regmap_write_bits(i2s->regmap, STM32_I2S_CR1_REG, 840e4e6ec7bSolivier moysan I2S_CR1_CSTART, I2S_CR1_CSTART); 841e4e6ec7bSolivier moysan if (ret < 0) { 842e4e6ec7bSolivier moysan dev_err(cpu_dai->dev, "Error %d starting I2S\n", ret); 843e4e6ec7bSolivier moysan return ret; 844e4e6ec7bSolivier moysan } 845e7cc49b8Solivier moysan 8468ba3c521SOlivier Moysan regmap_write_bits(i2s->regmap, STM32_I2S_IFCR_REG, 847e7cc49b8Solivier moysan I2S_IFCR_MASK, I2S_IFCR_MASK); 848e7cc49b8Solivier moysan 849ebf629d5SOlivier Moysan spin_lock(&i2s->lock_fd); 850ebf629d5SOlivier Moysan i2s->refcount++; 851e7cc49b8Solivier moysan if (playback_flg) { 852e7cc49b8Solivier moysan ier = I2S_IER_UDRIE; 853e7cc49b8Solivier moysan } else { 854e7cc49b8Solivier moysan ier = I2S_IER_OVRIE; 855e7cc49b8Solivier moysan 8567b6b0049SOlivier Moysan if (STM32_I2S_IS_MASTER(i2s) && i2s->refcount == 1) 8577b6b0049SOlivier Moysan /* dummy write to gate bus clocks */ 858e7cc49b8Solivier moysan regmap_write(i2s->regmap, 859e7cc49b8Solivier moysan STM32_I2S_TXDR_REG, 0); 860e7cc49b8Solivier moysan } 861ebf629d5SOlivier Moysan spin_unlock(&i2s->lock_fd); 862e7cc49b8Solivier moysan 863e7cc49b8Solivier moysan if (STM32_I2S_IS_SLAVE(i2s)) 864e7cc49b8Solivier moysan ier |= I2S_IER_TIFREIE; 865e7cc49b8Solivier moysan 866e7cc49b8Solivier moysan regmap_update_bits(i2s->regmap, STM32_I2S_IER_REG, ier, ier); 867e4e6ec7bSolivier moysan break; 868e4e6ec7bSolivier moysan case SNDRV_PCM_TRIGGER_STOP: 869e4e6ec7bSolivier moysan case SNDRV_PCM_TRIGGER_SUSPEND: 870e4e6ec7bSolivier moysan case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 87152e7306cSOlivier Moysan dev_dbg(cpu_dai->dev, "stop I2S %s\n", 87252e7306cSOlivier Moysan playback_flg ? "playback" : "capture"); 87352e7306cSOlivier Moysan 874e7cc49b8Solivier moysan if (playback_flg) 875e7cc49b8Solivier moysan regmap_update_bits(i2s->regmap, STM32_I2S_IER_REG, 876e7cc49b8Solivier moysan I2S_IER_UDRIE, 877e7cc49b8Solivier moysan (unsigned int)~I2S_IER_UDRIE); 878e7cc49b8Solivier moysan else 879e7cc49b8Solivier moysan regmap_update_bits(i2s->regmap, STM32_I2S_IER_REG, 880e7cc49b8Solivier moysan I2S_IER_OVRIE, 881e7cc49b8Solivier moysan (unsigned int)~I2S_IER_OVRIE); 882e7cc49b8Solivier moysan 883e7cc49b8Solivier moysan spin_lock(&i2s->lock_fd); 884e7cc49b8Solivier moysan i2s->refcount--; 885e7cc49b8Solivier moysan if (i2s->refcount) { 886e7cc49b8Solivier moysan spin_unlock(&i2s->lock_fd); 887e7cc49b8Solivier moysan break; 888e7cc49b8Solivier moysan } 889e7cc49b8Solivier moysan 890e4e6ec7bSolivier moysan ret = regmap_update_bits(i2s->regmap, STM32_I2S_CR1_REG, 891e4e6ec7bSolivier moysan I2S_CR1_SPE, 0); 892e4e6ec7bSolivier moysan if (ret < 0) { 893e4e6ec7bSolivier moysan dev_err(cpu_dai->dev, "Error %d disabling I2S\n", ret); 894ebf629d5SOlivier Moysan spin_unlock(&i2s->lock_fd); 895e4e6ec7bSolivier moysan return ret; 896e4e6ec7bSolivier moysan } 897ebf629d5SOlivier Moysan spin_unlock(&i2s->lock_fd); 898e4e6ec7bSolivier moysan 899e7cc49b8Solivier moysan cfg1_mask = I2S_CFG1_RXDMAEN | I2S_CFG1_TXDMAEN; 900e4e6ec7bSolivier moysan regmap_update_bits(i2s->regmap, STM32_I2S_CFG1_REG, 901e4e6ec7bSolivier moysan cfg1_mask, 0); 902e4e6ec7bSolivier moysan break; 903e4e6ec7bSolivier moysan default: 904e4e6ec7bSolivier moysan return -EINVAL; 905e4e6ec7bSolivier moysan } 906e4e6ec7bSolivier moysan 907e4e6ec7bSolivier moysan return 0; 908e4e6ec7bSolivier moysan } 909e4e6ec7bSolivier moysan 910e4e6ec7bSolivier moysan static void stm32_i2s_shutdown(struct snd_pcm_substream *substream, 911e4e6ec7bSolivier moysan struct snd_soc_dai *cpu_dai) 912e4e6ec7bSolivier moysan { 913e4e6ec7bSolivier moysan struct stm32_i2s_data *i2s = snd_soc_dai_get_drvdata(cpu_dai); 9143005decfSOlivier Moysan unsigned long flags; 915e4e6ec7bSolivier moysan 9166a68eeeeSOlivier Moysan clk_disable_unprepare(i2s->i2sclk); 9173005decfSOlivier Moysan 9183005decfSOlivier Moysan spin_lock_irqsave(&i2s->irq_lock, flags); 9193005decfSOlivier Moysan i2s->substream = NULL; 9203005decfSOlivier Moysan spin_unlock_irqrestore(&i2s->irq_lock, flags); 921e4e6ec7bSolivier moysan } 922e4e6ec7bSolivier moysan 923e4e6ec7bSolivier moysan static int stm32_i2s_dai_probe(struct snd_soc_dai *cpu_dai) 924e4e6ec7bSolivier moysan { 925e4e6ec7bSolivier moysan struct stm32_i2s_data *i2s = dev_get_drvdata(cpu_dai->dev); 926e4e6ec7bSolivier moysan struct snd_dmaengine_dai_dma_data *dma_data_tx = &i2s->dma_data_tx; 927e4e6ec7bSolivier moysan struct snd_dmaengine_dai_dma_data *dma_data_rx = &i2s->dma_data_rx; 928e4e6ec7bSolivier moysan 929e4e6ec7bSolivier moysan /* Buswidth will be set by framework */ 930e4e6ec7bSolivier moysan dma_data_tx->addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED; 931e4e6ec7bSolivier moysan dma_data_tx->addr = (dma_addr_t)(i2s->phys_addr) + STM32_I2S_TXDR_REG; 932e4e6ec7bSolivier moysan dma_data_tx->maxburst = 1; 933e4e6ec7bSolivier moysan dma_data_rx->addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED; 934e4e6ec7bSolivier moysan dma_data_rx->addr = (dma_addr_t)(i2s->phys_addr) + STM32_I2S_RXDR_REG; 935e4e6ec7bSolivier moysan dma_data_rx->maxburst = 1; 936e4e6ec7bSolivier moysan 937e4e6ec7bSolivier moysan snd_soc_dai_init_dma_data(cpu_dai, dma_data_tx, dma_data_rx); 938e4e6ec7bSolivier moysan 939e4e6ec7bSolivier moysan return 0; 940e4e6ec7bSolivier moysan } 941e4e6ec7bSolivier moysan 942e4e6ec7bSolivier moysan static const struct regmap_config stm32_h7_i2s_regmap_conf = { 943e4e6ec7bSolivier moysan .reg_bits = 32, 944e4e6ec7bSolivier moysan .reg_stride = 4, 945e4e6ec7bSolivier moysan .val_bits = 32, 94671ed4bddSOlivier Moysan .max_register = STM32_I2S_SIDR_REG, 947e4e6ec7bSolivier moysan .readable_reg = stm32_i2s_readable_reg, 948e4e6ec7bSolivier moysan .volatile_reg = stm32_i2s_volatile_reg, 949e4e6ec7bSolivier moysan .writeable_reg = stm32_i2s_writeable_reg, 95071ed4bddSOlivier Moysan .num_reg_defaults_raw = STM32_I2S_SIDR_REG / sizeof(u32) + 1, 951e4e6ec7bSolivier moysan .fast_io = true, 952307cce4aSOlivier Moysan .cache_type = REGCACHE_FLAT, 953e4e6ec7bSolivier moysan }; 954e4e6ec7bSolivier moysan 955e4e6ec7bSolivier moysan static const struct snd_soc_dai_ops stm32_i2s_pcm_dai_ops = { 956*53c577baSKuninori Morimoto .probe = stm32_i2s_dai_probe, 957e4e6ec7bSolivier moysan .set_sysclk = stm32_i2s_set_sysclk, 95802ba0d96SCharles Keepax .set_fmt = stm32_i2s_set_dai_fmt, 959e4e6ec7bSolivier moysan .startup = stm32_i2s_startup, 960e4e6ec7bSolivier moysan .hw_params = stm32_i2s_hw_params, 961e4e6ec7bSolivier moysan .trigger = stm32_i2s_trigger, 962e4e6ec7bSolivier moysan .shutdown = stm32_i2s_shutdown, 963e4e6ec7bSolivier moysan }; 964e4e6ec7bSolivier moysan 965e4e6ec7bSolivier moysan static const struct snd_pcm_hardware stm32_i2s_pcm_hw = { 966e4e6ec7bSolivier moysan .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP, 967e4e6ec7bSolivier moysan .buffer_bytes_max = 8 * PAGE_SIZE, 9684fc19fffSOlivier Moysan .period_bytes_min = 1024, 9694fc19fffSOlivier Moysan .period_bytes_max = 4 * PAGE_SIZE, 970e4e6ec7bSolivier moysan .periods_min = 2, 971e4e6ec7bSolivier moysan .periods_max = 8, 972e4e6ec7bSolivier moysan }; 973e4e6ec7bSolivier moysan 974e4e6ec7bSolivier moysan static const struct snd_dmaengine_pcm_config stm32_i2s_pcm_config = { 975e4e6ec7bSolivier moysan .pcm_hardware = &stm32_i2s_pcm_hw, 976e4e6ec7bSolivier moysan .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config, 977e4e6ec7bSolivier moysan .prealloc_buffer_size = PAGE_SIZE * 8, 978e4e6ec7bSolivier moysan }; 979e4e6ec7bSolivier moysan 980e4e6ec7bSolivier moysan static const struct snd_soc_component_driver stm32_i2s_component = { 981e4e6ec7bSolivier moysan .name = "stm32-i2s", 98236f07985SCharles Keepax .legacy_dai_naming = 1, 983e4e6ec7bSolivier moysan }; 984e4e6ec7bSolivier moysan 985e4e6ec7bSolivier moysan static void stm32_i2s_dai_init(struct snd_soc_pcm_stream *stream, 986e4e6ec7bSolivier moysan char *stream_name) 987e4e6ec7bSolivier moysan { 988e4e6ec7bSolivier moysan stream->stream_name = stream_name; 989e4e6ec7bSolivier moysan stream->channels_min = 1; 990e4e6ec7bSolivier moysan stream->channels_max = 2; 991e4e6ec7bSolivier moysan stream->rates = SNDRV_PCM_RATE_8000_192000; 992e4e6ec7bSolivier moysan stream->formats = SNDRV_PCM_FMTBIT_S16_LE | 993e4e6ec7bSolivier moysan SNDRV_PCM_FMTBIT_S32_LE; 994e4e6ec7bSolivier moysan } 995e4e6ec7bSolivier moysan 996e4e6ec7bSolivier moysan static int stm32_i2s_dais_init(struct platform_device *pdev, 997e4e6ec7bSolivier moysan struct stm32_i2s_data *i2s) 998e4e6ec7bSolivier moysan { 999e4e6ec7bSolivier moysan struct snd_soc_dai_driver *dai_ptr; 1000e4e6ec7bSolivier moysan 1001e4e6ec7bSolivier moysan dai_ptr = devm_kzalloc(&pdev->dev, sizeof(struct snd_soc_dai_driver), 1002e4e6ec7bSolivier moysan GFP_KERNEL); 1003e4e6ec7bSolivier moysan if (!dai_ptr) 1004e4e6ec7bSolivier moysan return -ENOMEM; 1005e4e6ec7bSolivier moysan 1006e4e6ec7bSolivier moysan dai_ptr->ops = &stm32_i2s_pcm_dai_ops; 1007e4e6ec7bSolivier moysan dai_ptr->id = 1; 1008e4e6ec7bSolivier moysan stm32_i2s_dai_init(&dai_ptr->playback, "playback"); 1009e4e6ec7bSolivier moysan stm32_i2s_dai_init(&dai_ptr->capture, "capture"); 1010e4e6ec7bSolivier moysan i2s->dai_drv = dai_ptr; 1011e4e6ec7bSolivier moysan 1012e4e6ec7bSolivier moysan return 0; 1013e4e6ec7bSolivier moysan } 1014e4e6ec7bSolivier moysan 1015e4e6ec7bSolivier moysan static const struct of_device_id stm32_i2s_ids[] = { 1016e4e6ec7bSolivier moysan { 1017e4e6ec7bSolivier moysan .compatible = "st,stm32h7-i2s", 1018e4e6ec7bSolivier moysan .data = &stm32_h7_i2s_regmap_conf 1019e4e6ec7bSolivier moysan }, 1020e4e6ec7bSolivier moysan {}, 1021e4e6ec7bSolivier moysan }; 1022e4e6ec7bSolivier moysan 1023e4e6ec7bSolivier moysan static int stm32_i2s_parse_dt(struct platform_device *pdev, 1024e4e6ec7bSolivier moysan struct stm32_i2s_data *i2s) 1025e4e6ec7bSolivier moysan { 1026e4e6ec7bSolivier moysan struct device_node *np = pdev->dev.of_node; 1027e4e6ec7bSolivier moysan const struct of_device_id *of_id; 1028e4e6ec7bSolivier moysan struct reset_control *rst; 1029e4e6ec7bSolivier moysan struct resource *res; 1030e4e6ec7bSolivier moysan int irq, ret; 1031e4e6ec7bSolivier moysan 1032e4e6ec7bSolivier moysan if (!np) 1033e4e6ec7bSolivier moysan return -ENODEV; 1034e4e6ec7bSolivier moysan 1035e4e6ec7bSolivier moysan of_id = of_match_device(stm32_i2s_ids, &pdev->dev); 1036e4e6ec7bSolivier moysan if (of_id) 1037e4e6ec7bSolivier moysan i2s->regmap_conf = (const struct regmap_config *)of_id->data; 1038e4e6ec7bSolivier moysan else 1039e4e6ec7bSolivier moysan return -EINVAL; 1040e4e6ec7bSolivier moysan 10412f177669SYang Yingliang i2s->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 1042e4e6ec7bSolivier moysan if (IS_ERR(i2s->base)) 1043e4e6ec7bSolivier moysan return PTR_ERR(i2s->base); 1044e4e6ec7bSolivier moysan 1045e4e6ec7bSolivier moysan i2s->phys_addr = res->start; 1046e4e6ec7bSolivier moysan 1047e4e6ec7bSolivier moysan /* Get clocks */ 1048e4e6ec7bSolivier moysan i2s->pclk = devm_clk_get(&pdev->dev, "pclk"); 1049efc162cbSKuninori Morimoto if (IS_ERR(i2s->pclk)) 1050efc162cbSKuninori Morimoto return dev_err_probe(&pdev->dev, PTR_ERR(i2s->pclk), 1051efc162cbSKuninori Morimoto "Could not get pclk\n"); 1052e4e6ec7bSolivier moysan 1053e4e6ec7bSolivier moysan i2s->i2sclk = devm_clk_get(&pdev->dev, "i2sclk"); 1054efc162cbSKuninori Morimoto if (IS_ERR(i2s->i2sclk)) 1055efc162cbSKuninori Morimoto return dev_err_probe(&pdev->dev, PTR_ERR(i2s->i2sclk), 1056efc162cbSKuninori Morimoto "Could not get i2sclk\n"); 1057e4e6ec7bSolivier moysan 1058e4e6ec7bSolivier moysan i2s->x8kclk = devm_clk_get(&pdev->dev, "x8k"); 1059efc162cbSKuninori Morimoto if (IS_ERR(i2s->x8kclk)) 1060efc162cbSKuninori Morimoto return dev_err_probe(&pdev->dev, PTR_ERR(i2s->x8kclk), 1061efc162cbSKuninori Morimoto "Could not get x8k parent clock\n"); 1062e4e6ec7bSolivier moysan 1063e4e6ec7bSolivier moysan i2s->x11kclk = devm_clk_get(&pdev->dev, "x11k"); 1064efc162cbSKuninori Morimoto if (IS_ERR(i2s->x11kclk)) 1065efc162cbSKuninori Morimoto return dev_err_probe(&pdev->dev, PTR_ERR(i2s->x11kclk), 1066efc162cbSKuninori Morimoto "Could not get x11k parent clock\n"); 1067e4e6ec7bSolivier moysan 10688a262e61SOlivier Moysan /* Register mclk provider if requested */ 10691e108e60SRob Herring if (of_property_present(np, "#clock-cells")) { 10708a262e61SOlivier Moysan ret = stm32_i2s_add_mclk_provider(i2s); 10718a262e61SOlivier Moysan if (ret < 0) 10728a262e61SOlivier Moysan return ret; 10738a262e61SOlivier Moysan } 10748a262e61SOlivier Moysan 1075e4e6ec7bSolivier moysan /* Get irqs */ 1076e4e6ec7bSolivier moysan irq = platform_get_irq(pdev, 0); 1077cf9441adSStephen Boyd if (irq < 0) 10780bbf4084SFabien Dessenne return irq; 1079e4e6ec7bSolivier moysan 10807c0f8f14SOlivier Moysan ret = devm_request_irq(&pdev->dev, irq, stm32_i2s_isr, 0, 1081e4e6ec7bSolivier moysan dev_name(&pdev->dev), i2s); 1082e4e6ec7bSolivier moysan if (ret) { 1083e4e6ec7bSolivier moysan dev_err(&pdev->dev, "irq request returned %d\n", ret); 1084e4e6ec7bSolivier moysan return ret; 1085e4e6ec7bSolivier moysan } 1086e4e6ec7bSolivier moysan 1087e4e6ec7bSolivier moysan /* Reset */ 1088158ecc65SOlivier Moysan rst = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL); 1089efc162cbSKuninori Morimoto if (IS_ERR(rst)) 1090efc162cbSKuninori Morimoto return dev_err_probe(&pdev->dev, PTR_ERR(rst), 1091efc162cbSKuninori Morimoto "Reset controller error\n"); 1092efc162cbSKuninori Morimoto 1093e4e6ec7bSolivier moysan reset_control_assert(rst); 1094e4e6ec7bSolivier moysan udelay(2); 1095e4e6ec7bSolivier moysan reset_control_deassert(rst); 1096e4e6ec7bSolivier moysan 1097e4e6ec7bSolivier moysan return 0; 1098e4e6ec7bSolivier moysan } 1099e4e6ec7bSolivier moysan 1100607e4cf5SUwe Kleine-König static void stm32_i2s_remove(struct platform_device *pdev) 1101caff4ce8SOlivier Moysan { 1102caff4ce8SOlivier Moysan snd_dmaengine_pcm_unregister(&pdev->dev); 1103caff4ce8SOlivier Moysan snd_soc_unregister_component(&pdev->dev); 110432a956a1SOlivier Moysan pm_runtime_disable(&pdev->dev); 1105caff4ce8SOlivier Moysan } 1106caff4ce8SOlivier Moysan 1107e4e6ec7bSolivier moysan static int stm32_i2s_probe(struct platform_device *pdev) 1108e4e6ec7bSolivier moysan { 1109e4e6ec7bSolivier moysan struct stm32_i2s_data *i2s; 111071ed4bddSOlivier Moysan u32 val; 1111e4e6ec7bSolivier moysan int ret; 1112e4e6ec7bSolivier moysan 1113e4e6ec7bSolivier moysan i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL); 1114e4e6ec7bSolivier moysan if (!i2s) 1115e4e6ec7bSolivier moysan return -ENOMEM; 1116e4e6ec7bSolivier moysan 1117e4e6ec7bSolivier moysan i2s->pdev = pdev; 1118e4e6ec7bSolivier moysan i2s->ms_flg = I2S_MS_NOT_SET; 1119e4e6ec7bSolivier moysan spin_lock_init(&i2s->lock_fd); 11203005decfSOlivier Moysan spin_lock_init(&i2s->irq_lock); 1121e4e6ec7bSolivier moysan platform_set_drvdata(pdev, i2s); 1122e4e6ec7bSolivier moysan 11238a262e61SOlivier Moysan ret = stm32_i2s_parse_dt(pdev, i2s); 11248a262e61SOlivier Moysan if (ret) 11258a262e61SOlivier Moysan return ret; 11268a262e61SOlivier Moysan 1127e4e6ec7bSolivier moysan ret = stm32_i2s_dais_init(pdev, i2s); 1128e4e6ec7bSolivier moysan if (ret) 1129e4e6ec7bSolivier moysan return ret; 1130e4e6ec7bSolivier moysan 11316a68eeeeSOlivier Moysan i2s->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "pclk", 11326a68eeeeSOlivier Moysan i2s->base, i2s->regmap_conf); 1133efc162cbSKuninori Morimoto if (IS_ERR(i2s->regmap)) 1134efc162cbSKuninori Morimoto return dev_err_probe(&pdev->dev, PTR_ERR(i2s->regmap), 1135efc162cbSKuninori Morimoto "Regmap init error\n"); 1136e4e6ec7bSolivier moysan 1137caff4ce8SOlivier Moysan ret = snd_dmaengine_pcm_register(&pdev->dev, &stm32_i2s_pcm_config, 0); 1138efc162cbSKuninori Morimoto if (ret) 1139efc162cbSKuninori Morimoto return dev_err_probe(&pdev->dev, ret, "PCM DMA register error\n"); 1140e4e6ec7bSolivier moysan 1141caff4ce8SOlivier Moysan ret = snd_soc_register_component(&pdev->dev, &stm32_i2s_component, 1142caff4ce8SOlivier Moysan i2s->dai_drv, 1); 1143caff4ce8SOlivier Moysan if (ret) { 1144caff4ce8SOlivier Moysan snd_dmaengine_pcm_unregister(&pdev->dev); 1145caff4ce8SOlivier Moysan return ret; 1146caff4ce8SOlivier Moysan } 1147caff4ce8SOlivier Moysan 1148e4e6ec7bSolivier moysan /* Set SPI/I2S in i2s mode */ 114971ed4bddSOlivier Moysan ret = regmap_update_bits(i2s->regmap, STM32_I2S_CGFR_REG, 1150e4e6ec7bSolivier moysan I2S_CGFR_I2SMOD, I2S_CGFR_I2SMOD); 115171ed4bddSOlivier Moysan if (ret) 1152caff4ce8SOlivier Moysan goto error; 115371ed4bddSOlivier Moysan 115471ed4bddSOlivier Moysan ret = regmap_read(i2s->regmap, STM32_I2S_IPIDR_REG, &val); 115571ed4bddSOlivier Moysan if (ret) 1156caff4ce8SOlivier Moysan goto error; 115771ed4bddSOlivier Moysan 115871ed4bddSOlivier Moysan if (val == I2S_IPIDR_NUMBER) { 115971ed4bddSOlivier Moysan ret = regmap_read(i2s->regmap, STM32_I2S_HWCFGR_REG, &val); 116071ed4bddSOlivier Moysan if (ret) 1161caff4ce8SOlivier Moysan goto error; 116271ed4bddSOlivier Moysan 116371ed4bddSOlivier Moysan if (!FIELD_GET(I2S_HWCFGR_I2S_SUPPORT_MASK, val)) { 116471ed4bddSOlivier Moysan dev_err(&pdev->dev, 116571ed4bddSOlivier Moysan "Device does not support i2s mode\n"); 1166caff4ce8SOlivier Moysan ret = -EPERM; 1167caff4ce8SOlivier Moysan goto error; 116871ed4bddSOlivier Moysan } 116971ed4bddSOlivier Moysan 117071ed4bddSOlivier Moysan ret = regmap_read(i2s->regmap, STM32_I2S_VERR_REG, &val); 1171caff4ce8SOlivier Moysan if (ret) 1172caff4ce8SOlivier Moysan goto error; 117371ed4bddSOlivier Moysan 117471ed4bddSOlivier Moysan dev_dbg(&pdev->dev, "I2S version: %lu.%lu registered\n", 117571ed4bddSOlivier Moysan FIELD_GET(I2S_VERR_MAJ_MASK, val), 117671ed4bddSOlivier Moysan FIELD_GET(I2S_VERR_MIN_MASK, val)); 117771ed4bddSOlivier Moysan } 117871ed4bddSOlivier Moysan 117993618e5eSZhang Qilong pm_runtime_enable(&pdev->dev); 118093618e5eSZhang Qilong 118171ed4bddSOlivier Moysan return ret; 1182caff4ce8SOlivier Moysan 1183caff4ce8SOlivier Moysan error: 1184caff4ce8SOlivier Moysan stm32_i2s_remove(pdev); 1185caff4ce8SOlivier Moysan 1186caff4ce8SOlivier Moysan return ret; 1187e4e6ec7bSolivier moysan } 1188e4e6ec7bSolivier moysan 1189e4e6ec7bSolivier moysan MODULE_DEVICE_TABLE(of, stm32_i2s_ids); 1190e4e6ec7bSolivier moysan 1191307cce4aSOlivier Moysan #ifdef CONFIG_PM_SLEEP 1192307cce4aSOlivier Moysan static int stm32_i2s_suspend(struct device *dev) 1193307cce4aSOlivier Moysan { 1194307cce4aSOlivier Moysan struct stm32_i2s_data *i2s = dev_get_drvdata(dev); 1195307cce4aSOlivier Moysan 1196307cce4aSOlivier Moysan regcache_cache_only(i2s->regmap, true); 1197307cce4aSOlivier Moysan regcache_mark_dirty(i2s->regmap); 1198307cce4aSOlivier Moysan 1199307cce4aSOlivier Moysan return 0; 1200307cce4aSOlivier Moysan } 1201307cce4aSOlivier Moysan 1202307cce4aSOlivier Moysan static int stm32_i2s_resume(struct device *dev) 1203307cce4aSOlivier Moysan { 1204307cce4aSOlivier Moysan struct stm32_i2s_data *i2s = dev_get_drvdata(dev); 1205307cce4aSOlivier Moysan 1206307cce4aSOlivier Moysan regcache_cache_only(i2s->regmap, false); 1207307cce4aSOlivier Moysan return regcache_sync(i2s->regmap); 1208307cce4aSOlivier Moysan } 1209307cce4aSOlivier Moysan #endif /* CONFIG_PM_SLEEP */ 1210307cce4aSOlivier Moysan 1211307cce4aSOlivier Moysan static const struct dev_pm_ops stm32_i2s_pm_ops = { 1212307cce4aSOlivier Moysan SET_SYSTEM_SLEEP_PM_OPS(stm32_i2s_suspend, stm32_i2s_resume) 1213307cce4aSOlivier Moysan }; 1214307cce4aSOlivier Moysan 1215e4e6ec7bSolivier moysan static struct platform_driver stm32_i2s_driver = { 1216e4e6ec7bSolivier moysan .driver = { 1217e4e6ec7bSolivier moysan .name = "st,stm32-i2s", 1218e4e6ec7bSolivier moysan .of_match_table = stm32_i2s_ids, 1219307cce4aSOlivier Moysan .pm = &stm32_i2s_pm_ops, 1220e4e6ec7bSolivier moysan }, 1221e4e6ec7bSolivier moysan .probe = stm32_i2s_probe, 1222607e4cf5SUwe Kleine-König .remove_new = stm32_i2s_remove, 1223e4e6ec7bSolivier moysan }; 1224e4e6ec7bSolivier moysan 1225e4e6ec7bSolivier moysan module_platform_driver(stm32_i2s_driver); 1226e4e6ec7bSolivier moysan 1227e4e6ec7bSolivier moysan MODULE_DESCRIPTION("STM32 Soc i2s Interface"); 1228e4e6ec7bSolivier moysan MODULE_AUTHOR("Olivier Moysan, <olivier.moysan@st.com>"); 1229e4e6ec7bSolivier moysan MODULE_ALIAS("platform:stm32-i2s"); 1230e4e6ec7bSolivier moysan MODULE_LICENSE("GPL v2"); 1231