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 193e086edfSolivier moysan #include <linux/clk.h> 203e086edfSolivier moysan #include <linux/kernel.h> 213e086edfSolivier moysan #include <linux/module.h> 223e086edfSolivier moysan #include <linux/of_irq.h> 233e086edfSolivier moysan #include <linux/of_platform.h> 243e086edfSolivier moysan #include <linux/regmap.h> 253e086edfSolivier moysan 263e086edfSolivier moysan #include <sound/core.h> 273e086edfSolivier moysan #include <sound/dmaengine_pcm.h> 283e086edfSolivier moysan #include <sound/pcm_params.h> 293e086edfSolivier moysan 303e086edfSolivier moysan #include "stm32_sai.h" 313e086edfSolivier moysan 323e086edfSolivier moysan #define SAI_FREE_PROTOCOL 0x0 333e086edfSolivier moysan 343e086edfSolivier moysan #define SAI_SLOT_SIZE_AUTO 0x0 353e086edfSolivier moysan #define SAI_SLOT_SIZE_16 0x1 363e086edfSolivier moysan #define SAI_SLOT_SIZE_32 0x2 373e086edfSolivier moysan 383e086edfSolivier moysan #define SAI_DATASIZE_8 0x2 393e086edfSolivier moysan #define SAI_DATASIZE_10 0x3 403e086edfSolivier moysan #define SAI_DATASIZE_16 0x4 413e086edfSolivier moysan #define SAI_DATASIZE_20 0x5 423e086edfSolivier moysan #define SAI_DATASIZE_24 0x6 433e086edfSolivier moysan #define SAI_DATASIZE_32 0x7 443e086edfSolivier moysan 453e086edfSolivier moysan #define STM_SAI_FIFO_SIZE 8 463e086edfSolivier moysan #define STM_SAI_DAI_NAME_SIZE 15 473e086edfSolivier moysan 483e086edfSolivier moysan #define STM_SAI_IS_PLAYBACK(ip) ((ip)->dir == SNDRV_PCM_STREAM_PLAYBACK) 493e086edfSolivier moysan #define STM_SAI_IS_CAPTURE(ip) ((ip)->dir == SNDRV_PCM_STREAM_CAPTURE) 503e086edfSolivier moysan 513e086edfSolivier moysan #define STM_SAI_A_ID 0x0 523e086edfSolivier moysan #define STM_SAI_B_ID 0x1 533e086edfSolivier moysan 5403e78a24Solivier moysan #define STM_SAI_IS_SUB_A(x) ((x)->id == STM_SAI_A_ID) 5503e78a24Solivier moysan #define STM_SAI_IS_SUB_B(x) ((x)->id == STM_SAI_B_ID) 563e086edfSolivier moysan #define STM_SAI_BLOCK_NAME(x) (((x)->id == STM_SAI_A_ID) ? "A" : "B") 573e086edfSolivier moysan 585914d285SOlivier Moysan #define SAI_SYNC_NONE 0x0 595914d285SOlivier Moysan #define SAI_SYNC_INTERNAL 0x1 605914d285SOlivier Moysan #define SAI_SYNC_EXTERNAL 0x2 615914d285SOlivier Moysan 625914d285SOlivier Moysan #define STM_SAI_HAS_EXT_SYNC(x) (!STM_SAI_IS_F4(sai->pdata)) 635914d285SOlivier Moysan 643e086edfSolivier moysan /** 653e086edfSolivier moysan * struct stm32_sai_sub_data - private data of SAI sub block (block A or B) 663e086edfSolivier moysan * @pdev: device data pointer 673e086edfSolivier moysan * @regmap: SAI register map pointer 6803e78a24Solivier moysan * @regmap_config: SAI sub block register map configuration pointer 693e086edfSolivier moysan * @dma_params: dma configuration data for rx or tx channel 703e086edfSolivier moysan * @cpu_dai_drv: DAI driver data pointer 713e086edfSolivier moysan * @cpu_dai: DAI runtime data pointer 723e086edfSolivier moysan * @substream: PCM substream data pointer 733e086edfSolivier moysan * @pdata: SAI block parent data pointer 745914d285SOlivier Moysan * @np_sync_provider: synchronization provider node 753e086edfSolivier moysan * @sai_ck: kernel clock feeding the SAI clock generator 763e086edfSolivier moysan * @phys_addr: SAI registers physical base address 773e086edfSolivier moysan * @mclk_rate: SAI block master clock frequency (Hz). set at init 783e086edfSolivier moysan * @id: SAI sub block id corresponding to sub-block A or B 793e086edfSolivier moysan * @dir: SAI block direction (playback or capture). set at init 803e086edfSolivier moysan * @master: SAI block mode flag. (true=master, false=slave) set at init 813e086edfSolivier moysan * @fmt: SAI block format. relevant only for custom protocols. set at init 823e086edfSolivier moysan * @sync: SAI block synchronization mode. (none, internal or external) 835914d285SOlivier Moysan * @synco: SAI block ext sync source (provider setting). (none, sub-block A/B) 845914d285SOlivier Moysan * @synci: SAI block ext sync source (client setting). (SAI sync provider index) 853e086edfSolivier moysan * @fs_length: frame synchronization length. depends on protocol settings 863e086edfSolivier moysan * @slots: rx or tx slot number 873e086edfSolivier moysan * @slot_width: rx or tx slot width in bits 883e086edfSolivier moysan * @slot_mask: rx or tx active slots mask. set at init or at runtime 893e086edfSolivier moysan * @data_size: PCM data width. corresponds to PCM substream width. 903e086edfSolivier moysan */ 913e086edfSolivier moysan struct stm32_sai_sub_data { 923e086edfSolivier moysan struct platform_device *pdev; 933e086edfSolivier moysan struct regmap *regmap; 9403e78a24Solivier moysan const struct regmap_config *regmap_config; 953e086edfSolivier moysan struct snd_dmaengine_dai_dma_data dma_params; 963e086edfSolivier moysan struct snd_soc_dai_driver *cpu_dai_drv; 973e086edfSolivier moysan struct snd_soc_dai *cpu_dai; 983e086edfSolivier moysan struct snd_pcm_substream *substream; 993e086edfSolivier moysan struct stm32_sai_data *pdata; 1005914d285SOlivier Moysan struct device_node *np_sync_provider; 1013e086edfSolivier moysan struct clk *sai_ck; 1023e086edfSolivier moysan dma_addr_t phys_addr; 1033e086edfSolivier moysan unsigned int mclk_rate; 1043e086edfSolivier moysan unsigned int id; 1053e086edfSolivier moysan int dir; 1063e086edfSolivier moysan bool master; 1073e086edfSolivier moysan int fmt; 1083e086edfSolivier moysan int sync; 1095914d285SOlivier Moysan int synco; 1105914d285SOlivier Moysan int synci; 1113e086edfSolivier moysan int fs_length; 1123e086edfSolivier moysan int slots; 1133e086edfSolivier moysan int slot_width; 1143e086edfSolivier moysan int slot_mask; 1153e086edfSolivier moysan int data_size; 1163e086edfSolivier moysan }; 1173e086edfSolivier moysan 1183e086edfSolivier moysan enum stm32_sai_fifo_th { 1193e086edfSolivier moysan STM_SAI_FIFO_TH_EMPTY, 1203e086edfSolivier moysan STM_SAI_FIFO_TH_QUARTER, 1213e086edfSolivier moysan STM_SAI_FIFO_TH_HALF, 1223e086edfSolivier moysan STM_SAI_FIFO_TH_3_QUARTER, 1233e086edfSolivier moysan STM_SAI_FIFO_TH_FULL, 1243e086edfSolivier moysan }; 1253e086edfSolivier moysan 1263e086edfSolivier moysan static bool stm32_sai_sub_readable_reg(struct device *dev, unsigned int reg) 1273e086edfSolivier moysan { 1283e086edfSolivier moysan switch (reg) { 1293e086edfSolivier moysan case STM_SAI_CR1_REGX: 1303e086edfSolivier moysan case STM_SAI_CR2_REGX: 1313e086edfSolivier moysan case STM_SAI_FRCR_REGX: 1323e086edfSolivier moysan case STM_SAI_SLOTR_REGX: 1333e086edfSolivier moysan case STM_SAI_IMR_REGX: 1343e086edfSolivier moysan case STM_SAI_SR_REGX: 1353e086edfSolivier moysan case STM_SAI_CLRFR_REGX: 1363e086edfSolivier moysan case STM_SAI_DR_REGX: 13703e78a24Solivier moysan case STM_SAI_PDMCR_REGX: 13803e78a24Solivier moysan case STM_SAI_PDMLY_REGX: 1393e086edfSolivier moysan return true; 1403e086edfSolivier moysan default: 1413e086edfSolivier moysan return false; 1423e086edfSolivier moysan } 1433e086edfSolivier moysan } 1443e086edfSolivier moysan 1453e086edfSolivier moysan static bool stm32_sai_sub_volatile_reg(struct device *dev, unsigned int reg) 1463e086edfSolivier moysan { 1473e086edfSolivier moysan switch (reg) { 1483e086edfSolivier moysan case STM_SAI_DR_REGX: 1493e086edfSolivier moysan return true; 1503e086edfSolivier moysan default: 1513e086edfSolivier moysan return false; 1523e086edfSolivier moysan } 1533e086edfSolivier moysan } 1543e086edfSolivier moysan 1553e086edfSolivier moysan static bool stm32_sai_sub_writeable_reg(struct device *dev, unsigned int reg) 1563e086edfSolivier moysan { 1573e086edfSolivier moysan switch (reg) { 1583e086edfSolivier moysan case STM_SAI_CR1_REGX: 1593e086edfSolivier moysan case STM_SAI_CR2_REGX: 1603e086edfSolivier moysan case STM_SAI_FRCR_REGX: 1613e086edfSolivier moysan case STM_SAI_SLOTR_REGX: 1623e086edfSolivier moysan case STM_SAI_IMR_REGX: 1633e086edfSolivier moysan case STM_SAI_SR_REGX: 1643e086edfSolivier moysan case STM_SAI_CLRFR_REGX: 1653e086edfSolivier moysan case STM_SAI_DR_REGX: 16603e78a24Solivier moysan case STM_SAI_PDMCR_REGX: 16703e78a24Solivier moysan case STM_SAI_PDMLY_REGX: 1683e086edfSolivier moysan return true; 1693e086edfSolivier moysan default: 1703e086edfSolivier moysan return false; 1713e086edfSolivier moysan } 1723e086edfSolivier moysan } 1733e086edfSolivier moysan 17403e78a24Solivier moysan static const struct regmap_config stm32_sai_sub_regmap_config_f4 = { 1753e086edfSolivier moysan .reg_bits = 32, 1763e086edfSolivier moysan .reg_stride = 4, 1773e086edfSolivier moysan .val_bits = 32, 1783e086edfSolivier moysan .max_register = STM_SAI_DR_REGX, 1793e086edfSolivier moysan .readable_reg = stm32_sai_sub_readable_reg, 1803e086edfSolivier moysan .volatile_reg = stm32_sai_sub_volatile_reg, 1813e086edfSolivier moysan .writeable_reg = stm32_sai_sub_writeable_reg, 1823e086edfSolivier moysan .fast_io = true, 1833e086edfSolivier moysan }; 1843e086edfSolivier moysan 18503e78a24Solivier moysan static const struct regmap_config stm32_sai_sub_regmap_config_h7 = { 18603e78a24Solivier moysan .reg_bits = 32, 18703e78a24Solivier moysan .reg_stride = 4, 18803e78a24Solivier moysan .val_bits = 32, 18903e78a24Solivier moysan .max_register = STM_SAI_PDMLY_REGX, 19003e78a24Solivier moysan .readable_reg = stm32_sai_sub_readable_reg, 19103e78a24Solivier moysan .volatile_reg = stm32_sai_sub_volatile_reg, 19203e78a24Solivier moysan .writeable_reg = stm32_sai_sub_writeable_reg, 19303e78a24Solivier moysan .fast_io = true, 19403e78a24Solivier moysan }; 19503e78a24Solivier moysan 1963e086edfSolivier moysan static irqreturn_t stm32_sai_isr(int irq, void *devid) 1973e086edfSolivier moysan { 1983e086edfSolivier moysan struct stm32_sai_sub_data *sai = (struct stm32_sai_sub_data *)devid; 1993e086edfSolivier moysan struct platform_device *pdev = sai->pdev; 2003e086edfSolivier moysan unsigned int sr, imr, flags; 2013e086edfSolivier moysan snd_pcm_state_t status = SNDRV_PCM_STATE_RUNNING; 2023e086edfSolivier moysan 2033e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_IMR_REGX, &imr); 2043e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_SR_REGX, &sr); 2053e086edfSolivier moysan 2063e086edfSolivier moysan flags = sr & imr; 2073e086edfSolivier moysan if (!flags) 2083e086edfSolivier moysan return IRQ_NONE; 2093e086edfSolivier moysan 2103e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX, SAI_XCLRFR_MASK, 2113e086edfSolivier moysan SAI_XCLRFR_MASK); 2123e086edfSolivier moysan 213d807cdfbSOlivier Moysan if (!sai->substream) { 214d807cdfbSOlivier Moysan dev_err(&pdev->dev, "Device stopped. Spurious IRQ 0x%x\n", sr); 215d807cdfbSOlivier Moysan return IRQ_NONE; 216d807cdfbSOlivier Moysan } 217d807cdfbSOlivier Moysan 2183e086edfSolivier moysan if (flags & SAI_XIMR_OVRUDRIE) { 219602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ %s\n", 2203e086edfSolivier moysan STM_SAI_IS_PLAYBACK(sai) ? "underrun" : "overrun"); 2213e086edfSolivier moysan status = SNDRV_PCM_STATE_XRUN; 2223e086edfSolivier moysan } 2233e086edfSolivier moysan 2243e086edfSolivier moysan if (flags & SAI_XIMR_MUTEDETIE) 225602fdadcSolivier moysan dev_dbg(&pdev->dev, "IRQ mute detected\n"); 2263e086edfSolivier moysan 2273e086edfSolivier moysan if (flags & SAI_XIMR_WCKCFGIE) { 228602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ wrong clock configuration\n"); 2293e086edfSolivier moysan status = SNDRV_PCM_STATE_DISCONNECTED; 2303e086edfSolivier moysan } 2313e086edfSolivier moysan 2323e086edfSolivier moysan if (flags & SAI_XIMR_CNRDYIE) 233602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ Codec not ready\n"); 2343e086edfSolivier moysan 2353e086edfSolivier moysan if (flags & SAI_XIMR_AFSDETIE) { 236602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ Anticipated frame synchro\n"); 2373e086edfSolivier moysan status = SNDRV_PCM_STATE_XRUN; 2383e086edfSolivier moysan } 2393e086edfSolivier moysan 2403e086edfSolivier moysan if (flags & SAI_XIMR_LFSDETIE) { 241602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ Late frame synchro\n"); 2423e086edfSolivier moysan status = SNDRV_PCM_STATE_XRUN; 2433e086edfSolivier moysan } 2443e086edfSolivier moysan 2453e086edfSolivier moysan if (status != SNDRV_PCM_STATE_RUNNING) { 246d807cdfbSOlivier Moysan snd_pcm_stream_lock(sai->substream); 247d807cdfbSOlivier Moysan snd_pcm_stop(sai->substream, SNDRV_PCM_STATE_XRUN); 248d807cdfbSOlivier Moysan snd_pcm_stream_unlock(sai->substream); 2493e086edfSolivier moysan } 2503e086edfSolivier moysan 2513e086edfSolivier moysan return IRQ_HANDLED; 2523e086edfSolivier moysan } 2533e086edfSolivier moysan 2543e086edfSolivier moysan static int stm32_sai_set_sysclk(struct snd_soc_dai *cpu_dai, 2553e086edfSolivier moysan int clk_id, unsigned int freq, int dir) 2563e086edfSolivier moysan { 2573e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 258701a6ec3Solivier moysan int ret; 2593e086edfSolivier moysan 2603e086edfSolivier moysan if ((dir == SND_SOC_CLOCK_OUT) && sai->master) { 261701a6ec3Solivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 262701a6ec3Solivier moysan SAI_XCR1_NODIV, 263701a6ec3Solivier moysan (unsigned int)~SAI_XCR1_NODIV); 264701a6ec3Solivier moysan if (ret < 0) 265701a6ec3Solivier moysan return ret; 266701a6ec3Solivier moysan 2673e086edfSolivier moysan sai->mclk_rate = freq; 2683e086edfSolivier moysan dev_dbg(cpu_dai->dev, "SAI MCLK frequency is %uHz\n", freq); 2693e086edfSolivier moysan } 2703e086edfSolivier moysan 2713e086edfSolivier moysan return 0; 2723e086edfSolivier moysan } 2733e086edfSolivier moysan 2743e086edfSolivier moysan static int stm32_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask, 2753e086edfSolivier moysan u32 rx_mask, int slots, int slot_width) 2763e086edfSolivier moysan { 2773e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 2783e086edfSolivier moysan int slotr, slotr_mask, slot_size; 2793e086edfSolivier moysan 280602fdadcSolivier moysan dev_dbg(cpu_dai->dev, "Masks tx/rx:%#x/%#x, slots:%d, width:%d\n", 2813e086edfSolivier moysan tx_mask, rx_mask, slots, slot_width); 2823e086edfSolivier moysan 2833e086edfSolivier moysan switch (slot_width) { 2843e086edfSolivier moysan case 16: 2853e086edfSolivier moysan slot_size = SAI_SLOT_SIZE_16; 2863e086edfSolivier moysan break; 2873e086edfSolivier moysan case 32: 2883e086edfSolivier moysan slot_size = SAI_SLOT_SIZE_32; 2893e086edfSolivier moysan break; 2903e086edfSolivier moysan default: 2913e086edfSolivier moysan slot_size = SAI_SLOT_SIZE_AUTO; 2923e086edfSolivier moysan break; 2933e086edfSolivier moysan } 2943e086edfSolivier moysan 2953e086edfSolivier moysan slotr = SAI_XSLOTR_SLOTSZ_SET(slot_size) | 2963e086edfSolivier moysan SAI_XSLOTR_NBSLOT_SET(slots - 1); 2973e086edfSolivier moysan slotr_mask = SAI_XSLOTR_SLOTSZ_MASK | SAI_XSLOTR_NBSLOT_MASK; 2983e086edfSolivier moysan 2993e086edfSolivier moysan /* tx/rx mask set in machine init, if slot number defined in DT */ 3003e086edfSolivier moysan if (STM_SAI_IS_PLAYBACK(sai)) { 3013e086edfSolivier moysan sai->slot_mask = tx_mask; 3023e086edfSolivier moysan slotr |= SAI_XSLOTR_SLOTEN_SET(tx_mask); 3033e086edfSolivier moysan } 3043e086edfSolivier moysan 3053e086edfSolivier moysan if (STM_SAI_IS_CAPTURE(sai)) { 3063e086edfSolivier moysan sai->slot_mask = rx_mask; 3073e086edfSolivier moysan slotr |= SAI_XSLOTR_SLOTEN_SET(rx_mask); 3083e086edfSolivier moysan } 3093e086edfSolivier moysan 3103e086edfSolivier moysan slotr_mask |= SAI_XSLOTR_SLOTEN_MASK; 3113e086edfSolivier moysan 3123e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, slotr_mask, slotr); 3133e086edfSolivier moysan 3143e086edfSolivier moysan sai->slot_width = slot_width; 3153e086edfSolivier moysan sai->slots = slots; 3163e086edfSolivier moysan 3173e086edfSolivier moysan return 0; 3183e086edfSolivier moysan } 3193e086edfSolivier moysan 3203e086edfSolivier moysan static int stm32_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt) 3213e086edfSolivier moysan { 3223e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 32361fb4ff7SOlivier Moysan int cr1, frcr = 0; 32461fb4ff7SOlivier Moysan int cr1_mask, frcr_mask = 0; 3253e086edfSolivier moysan int ret; 3263e086edfSolivier moysan 3273e086edfSolivier moysan dev_dbg(cpu_dai->dev, "fmt %x\n", fmt); 3283e086edfSolivier moysan 32961fb4ff7SOlivier Moysan cr1_mask = SAI_XCR1_PRTCFG_MASK; 33061fb4ff7SOlivier Moysan cr1 = SAI_XCR1_PRTCFG_SET(SAI_FREE_PROTOCOL); 33161fb4ff7SOlivier Moysan 3323e086edfSolivier moysan switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 3333e086edfSolivier moysan /* SCK active high for all protocols */ 3343e086edfSolivier moysan case SND_SOC_DAIFMT_I2S: 3353e086edfSolivier moysan cr1 |= SAI_XCR1_CKSTR; 3363e086edfSolivier moysan frcr |= SAI_XFRCR_FSOFF | SAI_XFRCR_FSDEF; 3373e086edfSolivier moysan break; 3383e086edfSolivier moysan /* Left justified */ 3393e086edfSolivier moysan case SND_SOC_DAIFMT_MSB: 3403e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF; 3413e086edfSolivier moysan break; 3423e086edfSolivier moysan /* Right justified */ 3433e086edfSolivier moysan case SND_SOC_DAIFMT_LSB: 3443e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF; 3453e086edfSolivier moysan break; 3463e086edfSolivier moysan case SND_SOC_DAIFMT_DSP_A: 3473e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF; 3483e086edfSolivier moysan break; 3493e086edfSolivier moysan case SND_SOC_DAIFMT_DSP_B: 3503e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL; 3513e086edfSolivier moysan break; 3523e086edfSolivier moysan default: 3533e086edfSolivier moysan dev_err(cpu_dai->dev, "Unsupported protocol %#x\n", 3543e086edfSolivier moysan fmt & SND_SOC_DAIFMT_FORMAT_MASK); 3553e086edfSolivier moysan return -EINVAL; 3563e086edfSolivier moysan } 3573e086edfSolivier moysan 35861fb4ff7SOlivier Moysan cr1_mask |= SAI_XCR1_CKSTR; 3593e086edfSolivier moysan frcr_mask |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF | 3603e086edfSolivier moysan SAI_XFRCR_FSDEF; 3613e086edfSolivier moysan 3623e086edfSolivier moysan /* DAI clock strobing. Invert setting previously set */ 3633e086edfSolivier moysan switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 3643e086edfSolivier moysan case SND_SOC_DAIFMT_NB_NF: 3653e086edfSolivier moysan break; 3663e086edfSolivier moysan case SND_SOC_DAIFMT_IB_NF: 3673e086edfSolivier moysan cr1 ^= SAI_XCR1_CKSTR; 3683e086edfSolivier moysan break; 3693e086edfSolivier moysan case SND_SOC_DAIFMT_NB_IF: 3703e086edfSolivier moysan frcr ^= SAI_XFRCR_FSPOL; 3713e086edfSolivier moysan break; 3723e086edfSolivier moysan case SND_SOC_DAIFMT_IB_IF: 3733e086edfSolivier moysan /* Invert fs & sck */ 3743e086edfSolivier moysan cr1 ^= SAI_XCR1_CKSTR; 3753e086edfSolivier moysan frcr ^= SAI_XFRCR_FSPOL; 3763e086edfSolivier moysan break; 3773e086edfSolivier moysan default: 3783e086edfSolivier moysan dev_err(cpu_dai->dev, "Unsupported strobing %#x\n", 3793e086edfSolivier moysan fmt & SND_SOC_DAIFMT_INV_MASK); 3803e086edfSolivier moysan return -EINVAL; 3813e086edfSolivier moysan } 3823e086edfSolivier moysan cr1_mask |= SAI_XCR1_CKSTR; 3833e086edfSolivier moysan frcr_mask |= SAI_XFRCR_FSPOL; 3843e086edfSolivier moysan 3853e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr); 3863e086edfSolivier moysan 3873e086edfSolivier moysan /* DAI clock master masks */ 3883e086edfSolivier moysan switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 3893e086edfSolivier moysan case SND_SOC_DAIFMT_CBM_CFM: 3903e086edfSolivier moysan /* codec is master */ 3913e086edfSolivier moysan cr1 |= SAI_XCR1_SLAVE; 3923e086edfSolivier moysan sai->master = false; 3933e086edfSolivier moysan break; 3943e086edfSolivier moysan case SND_SOC_DAIFMT_CBS_CFS: 3953e086edfSolivier moysan sai->master = true; 3963e086edfSolivier moysan break; 3973e086edfSolivier moysan default: 3983e086edfSolivier moysan dev_err(cpu_dai->dev, "Unsupported mode %#x\n", 3993e086edfSolivier moysan fmt & SND_SOC_DAIFMT_MASTER_MASK); 4003e086edfSolivier moysan return -EINVAL; 4013e086edfSolivier moysan } 4025914d285SOlivier Moysan 4035914d285SOlivier Moysan /* Set slave mode if sub-block is synchronized with another SAI */ 4045914d285SOlivier Moysan if (sai->sync) { 4055914d285SOlivier Moysan dev_dbg(cpu_dai->dev, "Synchronized SAI configured as slave\n"); 4065914d285SOlivier Moysan cr1 |= SAI_XCR1_SLAVE; 4075914d285SOlivier Moysan sai->master = false; 4085914d285SOlivier Moysan } 4095914d285SOlivier Moysan 4103e086edfSolivier moysan cr1_mask |= SAI_XCR1_SLAVE; 4113e086edfSolivier moysan 412701a6ec3Solivier moysan /* do not generate master by default */ 413701a6ec3Solivier moysan cr1 |= SAI_XCR1_NODIV; 414701a6ec3Solivier moysan cr1_mask |= SAI_XCR1_NODIV; 415701a6ec3Solivier moysan 4163e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1); 4173e086edfSolivier moysan if (ret < 0) { 4183e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 4193e086edfSolivier moysan return ret; 4203e086edfSolivier moysan } 4213e086edfSolivier moysan 4223e086edfSolivier moysan sai->fmt = fmt; 4233e086edfSolivier moysan 4243e086edfSolivier moysan return 0; 4253e086edfSolivier moysan } 4263e086edfSolivier moysan 4273e086edfSolivier moysan static int stm32_sai_startup(struct snd_pcm_substream *substream, 4283e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 4293e086edfSolivier moysan { 4303e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 4313e086edfSolivier moysan int imr, cr2, ret; 4323e086edfSolivier moysan 4333e086edfSolivier moysan sai->substream = substream; 4343e086edfSolivier moysan 4353e086edfSolivier moysan ret = clk_prepare_enable(sai->sai_ck); 4363e086edfSolivier moysan if (ret < 0) { 437602fdadcSolivier moysan dev_err(cpu_dai->dev, "Failed to enable clock: %d\n", ret); 4383e086edfSolivier moysan return ret; 4393e086edfSolivier moysan } 4403e086edfSolivier moysan 4413e086edfSolivier moysan /* Enable ITs */ 4423e086edfSolivier moysan 4433e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX, 4443e086edfSolivier moysan SAI_XCLRFR_MASK, SAI_XCLRFR_MASK); 4453e086edfSolivier moysan 4463e086edfSolivier moysan imr = SAI_XIMR_OVRUDRIE; 4473e086edfSolivier moysan if (STM_SAI_IS_CAPTURE(sai)) { 4483e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_CR2_REGX, &cr2); 4493e086edfSolivier moysan if (cr2 & SAI_XCR2_MUTECNT_MASK) 4503e086edfSolivier moysan imr |= SAI_XIMR_MUTEDETIE; 4513e086edfSolivier moysan } 4523e086edfSolivier moysan 4533e086edfSolivier moysan if (sai->master) 4543e086edfSolivier moysan imr |= SAI_XIMR_WCKCFGIE; 4553e086edfSolivier moysan else 4563e086edfSolivier moysan imr |= SAI_XIMR_AFSDETIE | SAI_XIMR_LFSDETIE; 4573e086edfSolivier moysan 4583e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, 4593e086edfSolivier moysan SAI_XIMR_MASK, imr); 4603e086edfSolivier moysan 4613e086edfSolivier moysan return 0; 4623e086edfSolivier moysan } 4633e086edfSolivier moysan 4643e086edfSolivier moysan static int stm32_sai_set_config(struct snd_soc_dai *cpu_dai, 4653e086edfSolivier moysan struct snd_pcm_substream *substream, 4663e086edfSolivier moysan struct snd_pcm_hw_params *params) 4673e086edfSolivier moysan { 4683e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 4693e086edfSolivier moysan int cr1, cr1_mask, ret; 4703e086edfSolivier moysan 471a4529d2bSOlivier Moysan /* 472a4529d2bSOlivier Moysan * DMA bursts increment is set to 4 words. 473a4529d2bSOlivier Moysan * SAI fifo threshold is set to half fifo, to keep enough space 474a4529d2bSOlivier Moysan * for DMA incoming bursts. 475a4529d2bSOlivier Moysan */ 4763e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR2_REGX, 4773e086edfSolivier moysan SAI_XCR2_FFLUSH | SAI_XCR2_FTH_MASK, 478a4529d2bSOlivier Moysan SAI_XCR2_FFLUSH | 479a4529d2bSOlivier Moysan SAI_XCR2_FTH_SET(STM_SAI_FIFO_TH_HALF)); 4803e086edfSolivier moysan 4813e086edfSolivier moysan /* Mode, data format and channel config */ 48261fb4ff7SOlivier Moysan cr1_mask = SAI_XCR1_DS_MASK; 4833e086edfSolivier moysan switch (params_format(params)) { 4843e086edfSolivier moysan case SNDRV_PCM_FORMAT_S8: 485*7e751e37Solivier moysan cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_8); 4863e086edfSolivier moysan break; 4873e086edfSolivier moysan case SNDRV_PCM_FORMAT_S16_LE: 488*7e751e37Solivier moysan cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_16); 4893e086edfSolivier moysan break; 4903e086edfSolivier moysan case SNDRV_PCM_FORMAT_S32_LE: 491*7e751e37Solivier moysan cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_32); 4923e086edfSolivier moysan break; 4933e086edfSolivier moysan default: 4943e086edfSolivier moysan dev_err(cpu_dai->dev, "Data format not supported"); 4953e086edfSolivier moysan return -EINVAL; 4963e086edfSolivier moysan } 4973e086edfSolivier moysan 4983e086edfSolivier moysan cr1_mask |= SAI_XCR1_MONO; 4993e086edfSolivier moysan if ((sai->slots == 2) && (params_channels(params) == 1)) 5003e086edfSolivier moysan cr1 |= SAI_XCR1_MONO; 5013e086edfSolivier moysan 5023e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1); 5033e086edfSolivier moysan if (ret < 0) { 5043e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 5053e086edfSolivier moysan return ret; 5063e086edfSolivier moysan } 5073e086edfSolivier moysan 5083e086edfSolivier moysan return 0; 5093e086edfSolivier moysan } 5103e086edfSolivier moysan 5113e086edfSolivier moysan static int stm32_sai_set_slots(struct snd_soc_dai *cpu_dai) 5123e086edfSolivier moysan { 5133e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 5143e086edfSolivier moysan int slotr, slot_sz; 5153e086edfSolivier moysan 5163e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_SLOTR_REGX, &slotr); 5173e086edfSolivier moysan 5183e086edfSolivier moysan /* 5193e086edfSolivier moysan * If SLOTSZ is set to auto in SLOTR, align slot width on data size 5203e086edfSolivier moysan * By default slot width = data size, if not forced from DT 5213e086edfSolivier moysan */ 5223e086edfSolivier moysan slot_sz = slotr & SAI_XSLOTR_SLOTSZ_MASK; 5233e086edfSolivier moysan if (slot_sz == SAI_XSLOTR_SLOTSZ_SET(SAI_SLOT_SIZE_AUTO)) 5243e086edfSolivier moysan sai->slot_width = sai->data_size; 5253e086edfSolivier moysan 5263e086edfSolivier moysan if (sai->slot_width < sai->data_size) { 5273e086edfSolivier moysan dev_err(cpu_dai->dev, 5283e086edfSolivier moysan "Data size %d larger than slot width\n", 5293e086edfSolivier moysan sai->data_size); 5303e086edfSolivier moysan return -EINVAL; 5313e086edfSolivier moysan } 5323e086edfSolivier moysan 5333e086edfSolivier moysan /* Slot number is set to 2, if not specified in DT */ 5343e086edfSolivier moysan if (!sai->slots) 5353e086edfSolivier moysan sai->slots = 2; 5363e086edfSolivier moysan 5373e086edfSolivier moysan /* The number of slots in the audio frame is equal to NBSLOT[3:0] + 1*/ 5383e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, 5393e086edfSolivier moysan SAI_XSLOTR_NBSLOT_MASK, 5403e086edfSolivier moysan SAI_XSLOTR_NBSLOT_SET((sai->slots - 1))); 5413e086edfSolivier moysan 5423e086edfSolivier moysan /* Set default slots mask if not already set from DT */ 5433e086edfSolivier moysan if (!(slotr & SAI_XSLOTR_SLOTEN_MASK)) { 5443e086edfSolivier moysan sai->slot_mask = (1 << sai->slots) - 1; 5453e086edfSolivier moysan regmap_update_bits(sai->regmap, 5463e086edfSolivier moysan STM_SAI_SLOTR_REGX, SAI_XSLOTR_SLOTEN_MASK, 5473e086edfSolivier moysan SAI_XSLOTR_SLOTEN_SET(sai->slot_mask)); 5483e086edfSolivier moysan } 5493e086edfSolivier moysan 550602fdadcSolivier moysan dev_dbg(cpu_dai->dev, "Slots %d, slot width %d\n", 5513e086edfSolivier moysan sai->slots, sai->slot_width); 5523e086edfSolivier moysan 5533e086edfSolivier moysan return 0; 5543e086edfSolivier moysan } 5553e086edfSolivier moysan 5563e086edfSolivier moysan static void stm32_sai_set_frame(struct snd_soc_dai *cpu_dai) 5573e086edfSolivier moysan { 5583e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 5593e086edfSolivier moysan int fs_active, offset, format; 5603e086edfSolivier moysan int frcr, frcr_mask; 5613e086edfSolivier moysan 5623e086edfSolivier moysan format = sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK; 5633e086edfSolivier moysan sai->fs_length = sai->slot_width * sai->slots; 5643e086edfSolivier moysan 5653e086edfSolivier moysan fs_active = sai->fs_length / 2; 5663e086edfSolivier moysan if ((format == SND_SOC_DAIFMT_DSP_A) || 5673e086edfSolivier moysan (format == SND_SOC_DAIFMT_DSP_B)) 5683e086edfSolivier moysan fs_active = 1; 5693e086edfSolivier moysan 5703e086edfSolivier moysan frcr = SAI_XFRCR_FRL_SET((sai->fs_length - 1)); 5713e086edfSolivier moysan frcr |= SAI_XFRCR_FSALL_SET((fs_active - 1)); 5723e086edfSolivier moysan frcr_mask = SAI_XFRCR_FRL_MASK | SAI_XFRCR_FSALL_MASK; 5733e086edfSolivier moysan 574602fdadcSolivier moysan dev_dbg(cpu_dai->dev, "Frame length %d, frame active %d\n", 5753e086edfSolivier moysan sai->fs_length, fs_active); 5763e086edfSolivier moysan 5773e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr); 5783e086edfSolivier moysan 5793e086edfSolivier moysan if ((sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_LSB) { 5803e086edfSolivier moysan offset = sai->slot_width - sai->data_size; 5813e086edfSolivier moysan 5823e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, 5833e086edfSolivier moysan SAI_XSLOTR_FBOFF_MASK, 5843e086edfSolivier moysan SAI_XSLOTR_FBOFF_SET(offset)); 5853e086edfSolivier moysan } 5863e086edfSolivier moysan } 5873e086edfSolivier moysan 5883e086edfSolivier moysan static int stm32_sai_configure_clock(struct snd_soc_dai *cpu_dai, 5893e086edfSolivier moysan struct snd_pcm_hw_params *params) 5903e086edfSolivier moysan { 5913e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 5923e086edfSolivier moysan int cr1, mask, div = 0; 59303e78a24Solivier moysan int sai_clk_rate, mclk_ratio, den, ret; 59403e78a24Solivier moysan int version = sai->pdata->conf->version; 5953e086edfSolivier moysan 5963e086edfSolivier moysan if (!sai->mclk_rate) { 5973e086edfSolivier moysan dev_err(cpu_dai->dev, "Mclk rate is null\n"); 5983e086edfSolivier moysan return -EINVAL; 5993e086edfSolivier moysan } 6003e086edfSolivier moysan 6013e086edfSolivier moysan if (!(params_rate(params) % 11025)) 6023e086edfSolivier moysan clk_set_parent(sai->sai_ck, sai->pdata->clk_x11k); 6033e086edfSolivier moysan else 6043e086edfSolivier moysan clk_set_parent(sai->sai_ck, sai->pdata->clk_x8k); 6053e086edfSolivier moysan sai_clk_rate = clk_get_rate(sai->sai_ck); 6063e086edfSolivier moysan 60703e78a24Solivier moysan if (STM_SAI_IS_F4(sai->pdata)) { 6083e086edfSolivier moysan /* 6093e086edfSolivier moysan * mclk_rate = 256 * fs 6103e086edfSolivier moysan * MCKDIV = 0 if sai_ck < 3/2 * mclk_rate 6113e086edfSolivier moysan * MCKDIV = sai_ck / (2 * mclk_rate) otherwise 6123e086edfSolivier moysan */ 6133e086edfSolivier moysan if (2 * sai_clk_rate >= 3 * sai->mclk_rate) 61403e78a24Solivier moysan div = DIV_ROUND_CLOSEST(sai_clk_rate, 61503e78a24Solivier moysan 2 * sai->mclk_rate); 61603e78a24Solivier moysan } else { 61703e78a24Solivier moysan /* 61803e78a24Solivier moysan * TDM mode : 61903e78a24Solivier moysan * mclk on 62003e78a24Solivier moysan * MCKDIV = sai_ck / (ws x 256) (NOMCK=0. OSR=0) 62103e78a24Solivier moysan * MCKDIV = sai_ck / (ws x 512) (NOMCK=0. OSR=1) 62203e78a24Solivier moysan * mclk off 62303e78a24Solivier moysan * MCKDIV = sai_ck / (frl x ws) (NOMCK=1) 62403e78a24Solivier moysan * Note: NOMCK/NODIV correspond to same bit. 62503e78a24Solivier moysan */ 62603e78a24Solivier moysan if (sai->mclk_rate) { 62703e78a24Solivier moysan mclk_ratio = sai->mclk_rate / params_rate(params); 62803e78a24Solivier moysan if (mclk_ratio != 256) { 62903e78a24Solivier moysan if (mclk_ratio == 512) { 63003e78a24Solivier moysan mask = SAI_XCR1_OSR; 63103e78a24Solivier moysan cr1 = SAI_XCR1_OSR; 63203e78a24Solivier moysan } else { 63303e78a24Solivier moysan dev_err(cpu_dai->dev, 63403e78a24Solivier moysan "Wrong mclk ratio %d\n", 63503e78a24Solivier moysan mclk_ratio); 63603e78a24Solivier moysan return -EINVAL; 63703e78a24Solivier moysan } 63803e78a24Solivier moysan } 63903e78a24Solivier moysan div = DIV_ROUND_CLOSEST(sai_clk_rate, sai->mclk_rate); 64003e78a24Solivier moysan } else { 64103e78a24Solivier moysan /* mclk-fs not set, master clock not active. NOMCK=1 */ 64203e78a24Solivier moysan den = sai->fs_length * params_rate(params); 64303e78a24Solivier moysan div = DIV_ROUND_CLOSEST(sai_clk_rate, den); 64403e78a24Solivier moysan } 64503e78a24Solivier moysan } 6463e086edfSolivier moysan 64703e78a24Solivier moysan if (div > SAI_XCR1_MCKDIV_MAX(version)) { 6483e086edfSolivier moysan dev_err(cpu_dai->dev, "Divider %d out of range\n", div); 6493e086edfSolivier moysan return -EINVAL; 6503e086edfSolivier moysan } 6513e086edfSolivier moysan dev_dbg(cpu_dai->dev, "SAI clock %d, divider %d\n", sai_clk_rate, div); 6523e086edfSolivier moysan 65303e78a24Solivier moysan mask = SAI_XCR1_MCKDIV_MASK(SAI_XCR1_MCKDIV_WIDTH(version)); 6543e086edfSolivier moysan cr1 = SAI_XCR1_MCKDIV_SET(div); 6553e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, mask, cr1); 6563e086edfSolivier moysan if (ret < 0) { 6573e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 6583e086edfSolivier moysan return ret; 6593e086edfSolivier moysan } 6603e086edfSolivier moysan 6613e086edfSolivier moysan return 0; 6623e086edfSolivier moysan } 6633e086edfSolivier moysan 6643e086edfSolivier moysan static int stm32_sai_hw_params(struct snd_pcm_substream *substream, 6653e086edfSolivier moysan struct snd_pcm_hw_params *params, 6663e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 6673e086edfSolivier moysan { 6683e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 6693e086edfSolivier moysan int ret; 6703e086edfSolivier moysan 6713e086edfSolivier moysan sai->data_size = params_width(params); 6723e086edfSolivier moysan 6733e086edfSolivier moysan ret = stm32_sai_set_slots(cpu_dai); 6743e086edfSolivier moysan if (ret < 0) 6753e086edfSolivier moysan return ret; 6763e086edfSolivier moysan stm32_sai_set_frame(cpu_dai); 6773e086edfSolivier moysan 6783e086edfSolivier moysan ret = stm32_sai_set_config(cpu_dai, substream, params); 6793e086edfSolivier moysan if (ret) 6803e086edfSolivier moysan return ret; 6813e086edfSolivier moysan 6823e086edfSolivier moysan if (sai->master) 6833e086edfSolivier moysan ret = stm32_sai_configure_clock(cpu_dai, params); 6843e086edfSolivier moysan 6853e086edfSolivier moysan return ret; 6863e086edfSolivier moysan } 6873e086edfSolivier moysan 6883e086edfSolivier moysan static int stm32_sai_trigger(struct snd_pcm_substream *substream, int cmd, 6893e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 6903e086edfSolivier moysan { 6913e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 6923e086edfSolivier moysan int ret; 6933e086edfSolivier moysan 6943e086edfSolivier moysan switch (cmd) { 6953e086edfSolivier moysan case SNDRV_PCM_TRIGGER_START: 6963e086edfSolivier moysan case SNDRV_PCM_TRIGGER_RESUME: 6973e086edfSolivier moysan case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 6983e086edfSolivier moysan dev_dbg(cpu_dai->dev, "Enable DMA and SAI\n"); 6993e086edfSolivier moysan 7003e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 7013e086edfSolivier moysan SAI_XCR1_DMAEN, SAI_XCR1_DMAEN); 7023e086edfSolivier moysan 7033e086edfSolivier moysan /* Enable SAI */ 7043e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 7053e086edfSolivier moysan SAI_XCR1_SAIEN, SAI_XCR1_SAIEN); 7063e086edfSolivier moysan if (ret < 0) 7073e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 7083e086edfSolivier moysan break; 7093e086edfSolivier moysan case SNDRV_PCM_TRIGGER_SUSPEND: 7103e086edfSolivier moysan case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 7113e086edfSolivier moysan case SNDRV_PCM_TRIGGER_STOP: 7123e086edfSolivier moysan dev_dbg(cpu_dai->dev, "Disable DMA and SAI\n"); 7133e086edfSolivier moysan 71447a8907dSOlivier Moysan regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, 71547a8907dSOlivier Moysan SAI_XIMR_MASK, 0); 71647a8907dSOlivier Moysan 7173e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 7183e086edfSolivier moysan SAI_XCR1_SAIEN, 7193e086edfSolivier moysan (unsigned int)~SAI_XCR1_SAIEN); 7204fa17938Solivier moysan 7214fa17938Solivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 7224fa17938Solivier moysan SAI_XCR1_DMAEN, 7234fa17938Solivier moysan (unsigned int)~SAI_XCR1_DMAEN); 7243e086edfSolivier moysan if (ret < 0) 7253e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 7263e086edfSolivier moysan break; 7273e086edfSolivier moysan default: 7283e086edfSolivier moysan return -EINVAL; 7293e086edfSolivier moysan } 7303e086edfSolivier moysan 7313e086edfSolivier moysan return ret; 7323e086edfSolivier moysan } 7333e086edfSolivier moysan 7343e086edfSolivier moysan static void stm32_sai_shutdown(struct snd_pcm_substream *substream, 7353e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 7363e086edfSolivier moysan { 7373e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 7383e086edfSolivier moysan 7393e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, SAI_XIMR_MASK, 0); 7403e086edfSolivier moysan 741701a6ec3Solivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, SAI_XCR1_NODIV, 742701a6ec3Solivier moysan SAI_XCR1_NODIV); 743701a6ec3Solivier moysan 7443e086edfSolivier moysan clk_disable_unprepare(sai->sai_ck); 7453e086edfSolivier moysan sai->substream = NULL; 7463e086edfSolivier moysan } 7473e086edfSolivier moysan 7483e086edfSolivier moysan static int stm32_sai_dai_probe(struct snd_soc_dai *cpu_dai) 7493e086edfSolivier moysan { 7503e086edfSolivier moysan struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev); 75161fb4ff7SOlivier Moysan int cr1 = 0, cr1_mask; 7523e086edfSolivier moysan 7533e086edfSolivier moysan sai->dma_params.addr = (dma_addr_t)(sai->phys_addr + STM_SAI_DR_REGX); 754a4529d2bSOlivier Moysan /* 755a4529d2bSOlivier Moysan * DMA supports 4, 8 or 16 burst sizes. Burst size 4 is the best choice, 756a4529d2bSOlivier Moysan * as it allows bytes, half-word and words transfers. (See DMA fifos 757a4529d2bSOlivier Moysan * constraints). 758a4529d2bSOlivier Moysan */ 759a4529d2bSOlivier Moysan sai->dma_params.maxburst = 4; 7603e086edfSolivier moysan /* Buswidth will be set by framework at runtime */ 7613e086edfSolivier moysan sai->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED; 7623e086edfSolivier moysan 7633e086edfSolivier moysan if (STM_SAI_IS_PLAYBACK(sai)) 7643e086edfSolivier moysan snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params, NULL); 7653e086edfSolivier moysan else 7663e086edfSolivier moysan snd_soc_dai_init_dma_data(cpu_dai, NULL, &sai->dma_params); 7673e086edfSolivier moysan 76861fb4ff7SOlivier Moysan cr1_mask = SAI_XCR1_RX_TX; 76961fb4ff7SOlivier Moysan if (STM_SAI_IS_CAPTURE(sai)) 77061fb4ff7SOlivier Moysan cr1 |= SAI_XCR1_RX_TX; 77161fb4ff7SOlivier Moysan 7725914d285SOlivier Moysan /* Configure synchronization */ 7735914d285SOlivier Moysan if (sai->sync == SAI_SYNC_EXTERNAL) { 7745914d285SOlivier Moysan /* Configure synchro client and provider */ 7755914d285SOlivier Moysan sai->pdata->set_sync(sai->pdata, sai->np_sync_provider, 7765914d285SOlivier Moysan sai->synco, sai->synci); 7775914d285SOlivier Moysan } 7785914d285SOlivier Moysan 7795914d285SOlivier Moysan cr1_mask |= SAI_XCR1_SYNCEN_MASK; 7805914d285SOlivier Moysan cr1 |= SAI_XCR1_SYNCEN_SET(sai->sync); 7815914d285SOlivier Moysan 78261fb4ff7SOlivier Moysan return regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1); 7833e086edfSolivier moysan } 7843e086edfSolivier moysan 7853e086edfSolivier moysan static const struct snd_soc_dai_ops stm32_sai_pcm_dai_ops = { 7863e086edfSolivier moysan .set_sysclk = stm32_sai_set_sysclk, 7873e086edfSolivier moysan .set_fmt = stm32_sai_set_dai_fmt, 7883e086edfSolivier moysan .set_tdm_slot = stm32_sai_set_dai_tdm_slot, 7893e086edfSolivier moysan .startup = stm32_sai_startup, 7903e086edfSolivier moysan .hw_params = stm32_sai_hw_params, 7913e086edfSolivier moysan .trigger = stm32_sai_trigger, 7923e086edfSolivier moysan .shutdown = stm32_sai_shutdown, 7933e086edfSolivier moysan }; 7943e086edfSolivier moysan 7953e086edfSolivier moysan static const struct snd_pcm_hardware stm32_sai_pcm_hw = { 7963e086edfSolivier moysan .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP, 7973e086edfSolivier moysan .buffer_bytes_max = 8 * PAGE_SIZE, 7983e086edfSolivier moysan .period_bytes_min = 1024, /* 5ms at 48kHz */ 7993e086edfSolivier moysan .period_bytes_max = PAGE_SIZE, 8003e086edfSolivier moysan .periods_min = 2, 8013e086edfSolivier moysan .periods_max = 8, 8023e086edfSolivier moysan }; 8033e086edfSolivier moysan 8043e086edfSolivier moysan static struct snd_soc_dai_driver stm32_sai_playback_dai[] = { 8053e086edfSolivier moysan { 8063e086edfSolivier moysan .probe = stm32_sai_dai_probe, 8073e086edfSolivier moysan .id = 1, /* avoid call to fmt_single_name() */ 8083e086edfSolivier moysan .playback = { 8093e086edfSolivier moysan .channels_min = 1, 8103e086edfSolivier moysan .channels_max = 2, 8113e086edfSolivier moysan .rate_min = 8000, 8123e086edfSolivier moysan .rate_max = 192000, 8133e086edfSolivier moysan .rates = SNDRV_PCM_RATE_CONTINUOUS, 8143e086edfSolivier moysan /* DMA does not support 24 bits transfers */ 8153e086edfSolivier moysan .formats = 8163e086edfSolivier moysan SNDRV_PCM_FMTBIT_S8 | 8173e086edfSolivier moysan SNDRV_PCM_FMTBIT_S16_LE | 8183e086edfSolivier moysan SNDRV_PCM_FMTBIT_S32_LE, 8193e086edfSolivier moysan }, 8203e086edfSolivier moysan .ops = &stm32_sai_pcm_dai_ops, 8213e086edfSolivier moysan } 8223e086edfSolivier moysan }; 8233e086edfSolivier moysan 8243e086edfSolivier moysan static struct snd_soc_dai_driver stm32_sai_capture_dai[] = { 8253e086edfSolivier moysan { 8263e086edfSolivier moysan .probe = stm32_sai_dai_probe, 8273e086edfSolivier moysan .id = 1, /* avoid call to fmt_single_name() */ 8283e086edfSolivier moysan .capture = { 8293e086edfSolivier moysan .channels_min = 1, 8303e086edfSolivier moysan .channels_max = 2, 8313e086edfSolivier moysan .rate_min = 8000, 8323e086edfSolivier moysan .rate_max = 192000, 8333e086edfSolivier moysan .rates = SNDRV_PCM_RATE_CONTINUOUS, 8343e086edfSolivier moysan /* DMA does not support 24 bits transfers */ 8353e086edfSolivier moysan .formats = 8363e086edfSolivier moysan SNDRV_PCM_FMTBIT_S8 | 8373e086edfSolivier moysan SNDRV_PCM_FMTBIT_S16_LE | 8383e086edfSolivier moysan SNDRV_PCM_FMTBIT_S32_LE, 8393e086edfSolivier moysan }, 8403e086edfSolivier moysan .ops = &stm32_sai_pcm_dai_ops, 8413e086edfSolivier moysan } 8423e086edfSolivier moysan }; 8433e086edfSolivier moysan 8443e086edfSolivier moysan static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config = { 8453e086edfSolivier moysan .pcm_hardware = &stm32_sai_pcm_hw, 8463e086edfSolivier moysan .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config, 8473e086edfSolivier moysan }; 8483e086edfSolivier moysan 8493e086edfSolivier moysan static const struct snd_soc_component_driver stm32_component = { 8503e086edfSolivier moysan .name = "stm32-sai", 8513e086edfSolivier moysan }; 8523e086edfSolivier moysan 8533e086edfSolivier moysan static const struct of_device_id stm32_sai_sub_ids[] = { 8543e086edfSolivier moysan { .compatible = "st,stm32-sai-sub-a", 8553e086edfSolivier moysan .data = (void *)STM_SAI_A_ID}, 8563e086edfSolivier moysan { .compatible = "st,stm32-sai-sub-b", 8573e086edfSolivier moysan .data = (void *)STM_SAI_B_ID}, 8583e086edfSolivier moysan {} 8593e086edfSolivier moysan }; 8603e086edfSolivier moysan MODULE_DEVICE_TABLE(of, stm32_sai_sub_ids); 8613e086edfSolivier moysan 8623e086edfSolivier moysan static int stm32_sai_sub_parse_of(struct platform_device *pdev, 8633e086edfSolivier moysan struct stm32_sai_sub_data *sai) 8643e086edfSolivier moysan { 8653e086edfSolivier moysan struct device_node *np = pdev->dev.of_node; 8663e086edfSolivier moysan struct resource *res; 8673e086edfSolivier moysan void __iomem *base; 8685914d285SOlivier Moysan struct of_phandle_args args; 8695914d285SOlivier Moysan int ret; 8703e086edfSolivier moysan 8713e086edfSolivier moysan if (!np) 8723e086edfSolivier moysan return -ENODEV; 8733e086edfSolivier moysan 8743e086edfSolivier moysan res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 8753e086edfSolivier moysan base = devm_ioremap_resource(&pdev->dev, res); 8763e086edfSolivier moysan if (IS_ERR(base)) 8773e086edfSolivier moysan return PTR_ERR(base); 8783e086edfSolivier moysan 8793e086edfSolivier moysan sai->phys_addr = res->start; 88003e78a24Solivier moysan 88103e78a24Solivier moysan sai->regmap_config = &stm32_sai_sub_regmap_config_f4; 88203e78a24Solivier moysan /* Note: PDM registers not available for H7 sub-block B */ 88303e78a24Solivier moysan if (STM_SAI_IS_H7(sai->pdata) && STM_SAI_IS_SUB_A(sai)) 88403e78a24Solivier moysan sai->regmap_config = &stm32_sai_sub_regmap_config_h7; 88503e78a24Solivier moysan 88603e78a24Solivier moysan sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "sai_ck", 88703e78a24Solivier moysan base, sai->regmap_config); 88803e78a24Solivier moysan if (IS_ERR(sai->regmap)) { 88903e78a24Solivier moysan dev_err(&pdev->dev, "Failed to initialize MMIO\n"); 89003e78a24Solivier moysan return PTR_ERR(sai->regmap); 89103e78a24Solivier moysan } 8923e086edfSolivier moysan 8933e086edfSolivier moysan /* Get direction property */ 8943e086edfSolivier moysan if (of_property_match_string(np, "dma-names", "tx") >= 0) { 8953e086edfSolivier moysan sai->dir = SNDRV_PCM_STREAM_PLAYBACK; 8963e086edfSolivier moysan } else if (of_property_match_string(np, "dma-names", "rx") >= 0) { 8973e086edfSolivier moysan sai->dir = SNDRV_PCM_STREAM_CAPTURE; 8983e086edfSolivier moysan } else { 8993e086edfSolivier moysan dev_err(&pdev->dev, "Unsupported direction\n"); 9003e086edfSolivier moysan return -EINVAL; 9013e086edfSolivier moysan } 9023e086edfSolivier moysan 9035914d285SOlivier Moysan /* Get synchronization property */ 9045914d285SOlivier Moysan args.np = NULL; 9055914d285SOlivier Moysan ret = of_parse_phandle_with_fixed_args(np, "st,sync", 1, 0, &args); 9065914d285SOlivier Moysan if (ret < 0 && ret != -ENOENT) { 9075914d285SOlivier Moysan dev_err(&pdev->dev, "Failed to get st,sync property\n"); 9085914d285SOlivier Moysan return ret; 9095914d285SOlivier Moysan } 9105914d285SOlivier Moysan 9115914d285SOlivier Moysan sai->sync = SAI_SYNC_NONE; 9125914d285SOlivier Moysan if (args.np) { 9135914d285SOlivier Moysan if (args.np == np) { 9145914d285SOlivier Moysan dev_err(&pdev->dev, "%s sync own reference\n", 9155914d285SOlivier Moysan np->name); 9165914d285SOlivier Moysan of_node_put(args.np); 9175914d285SOlivier Moysan return -EINVAL; 9185914d285SOlivier Moysan } 9195914d285SOlivier Moysan 9205914d285SOlivier Moysan sai->np_sync_provider = of_get_parent(args.np); 9215914d285SOlivier Moysan if (!sai->np_sync_provider) { 9225914d285SOlivier Moysan dev_err(&pdev->dev, "%s parent node not found\n", 9235914d285SOlivier Moysan np->name); 9245914d285SOlivier Moysan of_node_put(args.np); 9255914d285SOlivier Moysan return -ENODEV; 9265914d285SOlivier Moysan } 9275914d285SOlivier Moysan 9285914d285SOlivier Moysan sai->sync = SAI_SYNC_INTERNAL; 9295914d285SOlivier Moysan if (sai->np_sync_provider != sai->pdata->pdev->dev.of_node) { 9305914d285SOlivier Moysan if (!STM_SAI_HAS_EXT_SYNC(sai)) { 9315914d285SOlivier Moysan dev_err(&pdev->dev, 9325914d285SOlivier Moysan "External synchro not supported\n"); 9335914d285SOlivier Moysan of_node_put(args.np); 9345914d285SOlivier Moysan return -EINVAL; 9355914d285SOlivier Moysan } 9365914d285SOlivier Moysan sai->sync = SAI_SYNC_EXTERNAL; 9375914d285SOlivier Moysan 9385914d285SOlivier Moysan sai->synci = args.args[0]; 9395914d285SOlivier Moysan if (sai->synci < 1 || 9405914d285SOlivier Moysan (sai->synci > (SAI_GCR_SYNCIN_MAX + 1))) { 9415914d285SOlivier Moysan dev_err(&pdev->dev, "Wrong SAI index\n"); 9425914d285SOlivier Moysan of_node_put(args.np); 9435914d285SOlivier Moysan return -EINVAL; 9445914d285SOlivier Moysan } 9455914d285SOlivier Moysan 9465914d285SOlivier Moysan if (of_property_match_string(args.np, "compatible", 9475914d285SOlivier Moysan "st,stm32-sai-sub-a") >= 0) 9485914d285SOlivier Moysan sai->synco = STM_SAI_SYNC_OUT_A; 9495914d285SOlivier Moysan 9505914d285SOlivier Moysan if (of_property_match_string(args.np, "compatible", 9515914d285SOlivier Moysan "st,stm32-sai-sub-b") >= 0) 9525914d285SOlivier Moysan sai->synco = STM_SAI_SYNC_OUT_B; 9535914d285SOlivier Moysan 9545914d285SOlivier Moysan if (!sai->synco) { 9555914d285SOlivier Moysan dev_err(&pdev->dev, "Unknown SAI sub-block\n"); 9565914d285SOlivier Moysan of_node_put(args.np); 9575914d285SOlivier Moysan return -EINVAL; 9585914d285SOlivier Moysan } 9595914d285SOlivier Moysan } 9605914d285SOlivier Moysan 9615914d285SOlivier Moysan dev_dbg(&pdev->dev, "%s synchronized with %s\n", 9625914d285SOlivier Moysan pdev->name, args.np->full_name); 9635914d285SOlivier Moysan } 9645914d285SOlivier Moysan 9655914d285SOlivier Moysan of_node_put(args.np); 9663e086edfSolivier moysan sai->sai_ck = devm_clk_get(&pdev->dev, "sai_ck"); 9673e086edfSolivier moysan if (IS_ERR(sai->sai_ck)) { 968602fdadcSolivier moysan dev_err(&pdev->dev, "Missing kernel clock sai_ck\n"); 9693e086edfSolivier moysan return PTR_ERR(sai->sai_ck); 9703e086edfSolivier moysan } 9713e086edfSolivier moysan 9723e086edfSolivier moysan return 0; 9733e086edfSolivier moysan } 9743e086edfSolivier moysan 9753e086edfSolivier moysan static int stm32_sai_sub_dais_init(struct platform_device *pdev, 9763e086edfSolivier moysan struct stm32_sai_sub_data *sai) 9773e086edfSolivier moysan { 9783e086edfSolivier moysan sai->cpu_dai_drv = devm_kzalloc(&pdev->dev, 9793e086edfSolivier moysan sizeof(struct snd_soc_dai_driver), 9803e086edfSolivier moysan GFP_KERNEL); 9813e086edfSolivier moysan if (!sai->cpu_dai_drv) 9823e086edfSolivier moysan return -ENOMEM; 9833e086edfSolivier moysan 9843e086edfSolivier moysan sai->cpu_dai_drv->name = dev_name(&pdev->dev); 9853e086edfSolivier moysan if (STM_SAI_IS_PLAYBACK(sai)) { 9863e086edfSolivier moysan memcpy(sai->cpu_dai_drv, &stm32_sai_playback_dai, 9873e086edfSolivier moysan sizeof(stm32_sai_playback_dai)); 9883e086edfSolivier moysan sai->cpu_dai_drv->playback.stream_name = sai->cpu_dai_drv->name; 9893e086edfSolivier moysan } else { 9903e086edfSolivier moysan memcpy(sai->cpu_dai_drv, &stm32_sai_capture_dai, 9913e086edfSolivier moysan sizeof(stm32_sai_capture_dai)); 9923e086edfSolivier moysan sai->cpu_dai_drv->capture.stream_name = sai->cpu_dai_drv->name; 9933e086edfSolivier moysan } 9943e086edfSolivier moysan 9953e086edfSolivier moysan return 0; 9963e086edfSolivier moysan } 9973e086edfSolivier moysan 9983e086edfSolivier moysan static int stm32_sai_sub_probe(struct platform_device *pdev) 9993e086edfSolivier moysan { 10003e086edfSolivier moysan struct stm32_sai_sub_data *sai; 10013e086edfSolivier moysan const struct of_device_id *of_id; 10023e086edfSolivier moysan int ret; 10033e086edfSolivier moysan 10043e086edfSolivier moysan sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL); 10053e086edfSolivier moysan if (!sai) 10063e086edfSolivier moysan return -ENOMEM; 10073e086edfSolivier moysan 10083e086edfSolivier moysan of_id = of_match_device(stm32_sai_sub_ids, &pdev->dev); 10093e086edfSolivier moysan if (!of_id) 10103e086edfSolivier moysan return -EINVAL; 10113e086edfSolivier moysan sai->id = (uintptr_t)of_id->data; 10123e086edfSolivier moysan 10133e086edfSolivier moysan sai->pdev = pdev; 10143e086edfSolivier moysan platform_set_drvdata(pdev, sai); 10153e086edfSolivier moysan 10163e086edfSolivier moysan sai->pdata = dev_get_drvdata(pdev->dev.parent); 10173e086edfSolivier moysan if (!sai->pdata) { 10183e086edfSolivier moysan dev_err(&pdev->dev, "Parent device data not available\n"); 10193e086edfSolivier moysan return -EINVAL; 10203e086edfSolivier moysan } 10213e086edfSolivier moysan 10223e086edfSolivier moysan ret = stm32_sai_sub_parse_of(pdev, sai); 10233e086edfSolivier moysan if (ret) 10243e086edfSolivier moysan return ret; 10253e086edfSolivier moysan 10263e086edfSolivier moysan ret = stm32_sai_sub_dais_init(pdev, sai); 10273e086edfSolivier moysan if (ret) 10283e086edfSolivier moysan return ret; 10293e086edfSolivier moysan 10303e086edfSolivier moysan ret = devm_request_irq(&pdev->dev, sai->pdata->irq, stm32_sai_isr, 10313e086edfSolivier moysan IRQF_SHARED, dev_name(&pdev->dev), sai); 10323e086edfSolivier moysan if (ret) { 1033602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ request returned %d\n", ret); 10343e086edfSolivier moysan return ret; 10353e086edfSolivier moysan } 10363e086edfSolivier moysan 10373e086edfSolivier moysan ret = devm_snd_soc_register_component(&pdev->dev, &stm32_component, 10383e086edfSolivier moysan sai->cpu_dai_drv, 1); 10393e086edfSolivier moysan if (ret) 10403e086edfSolivier moysan return ret; 10413e086edfSolivier moysan 10423e086edfSolivier moysan ret = devm_snd_dmaengine_pcm_register(&pdev->dev, 10433e086edfSolivier moysan &stm32_sai_pcm_config, 0); 10443e086edfSolivier moysan if (ret) { 1045602fdadcSolivier moysan dev_err(&pdev->dev, "Could not register pcm dma\n"); 10463e086edfSolivier moysan return ret; 10473e086edfSolivier moysan } 10483e086edfSolivier moysan 10493e086edfSolivier moysan return 0; 10503e086edfSolivier moysan } 10513e086edfSolivier moysan 10523e086edfSolivier moysan static struct platform_driver stm32_sai_sub_driver = { 10533e086edfSolivier moysan .driver = { 10543e086edfSolivier moysan .name = "st,stm32-sai-sub", 10553e086edfSolivier moysan .of_match_table = stm32_sai_sub_ids, 10563e086edfSolivier moysan }, 10573e086edfSolivier moysan .probe = stm32_sai_sub_probe, 10583e086edfSolivier moysan }; 10593e086edfSolivier moysan 10603e086edfSolivier moysan module_platform_driver(stm32_sai_sub_driver); 10613e086edfSolivier moysan 10623e086edfSolivier moysan MODULE_DESCRIPTION("STM32 Soc SAI sub-block Interface"); 1063602fdadcSolivier moysan MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>"); 10643e086edfSolivier moysan MODULE_ALIAS("platform:st,stm32-sai-sub"); 10653e086edfSolivier moysan MODULE_LICENSE("GPL v2"); 1066