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 583e086edfSolivier moysan /** 593e086edfSolivier moysan * struct stm32_sai_sub_data - private data of SAI sub block (block A or B) 603e086edfSolivier moysan * @pdev: device data pointer 613e086edfSolivier moysan * @regmap: SAI register map pointer 6203e78a24Solivier moysan * @regmap_config: SAI sub block register map configuration pointer 633e086edfSolivier moysan * @dma_params: dma configuration data for rx or tx channel 643e086edfSolivier moysan * @cpu_dai_drv: DAI driver data pointer 653e086edfSolivier moysan * @cpu_dai: DAI runtime data pointer 663e086edfSolivier moysan * @substream: PCM substream data pointer 673e086edfSolivier moysan * @pdata: SAI block parent data pointer 683e086edfSolivier moysan * @sai_ck: kernel clock feeding the SAI clock generator 693e086edfSolivier moysan * @phys_addr: SAI registers physical base address 703e086edfSolivier moysan * @mclk_rate: SAI block master clock frequency (Hz). set at init 713e086edfSolivier moysan * @id: SAI sub block id corresponding to sub-block A or B 723e086edfSolivier moysan * @dir: SAI block direction (playback or capture). set at init 733e086edfSolivier moysan * @master: SAI block mode flag. (true=master, false=slave) set at init 743e086edfSolivier moysan * @fmt: SAI block format. relevant only for custom protocols. set at init 753e086edfSolivier moysan * @sync: SAI block synchronization mode. (none, internal or external) 763e086edfSolivier moysan * @fs_length: frame synchronization length. depends on protocol settings 773e086edfSolivier moysan * @slots: rx or tx slot number 783e086edfSolivier moysan * @slot_width: rx or tx slot width in bits 793e086edfSolivier moysan * @slot_mask: rx or tx active slots mask. set at init or at runtime 803e086edfSolivier moysan * @data_size: PCM data width. corresponds to PCM substream width. 813e086edfSolivier moysan */ 823e086edfSolivier moysan struct stm32_sai_sub_data { 833e086edfSolivier moysan struct platform_device *pdev; 843e086edfSolivier moysan struct regmap *regmap; 8503e78a24Solivier moysan const struct regmap_config *regmap_config; 863e086edfSolivier moysan struct snd_dmaengine_dai_dma_data dma_params; 873e086edfSolivier moysan struct snd_soc_dai_driver *cpu_dai_drv; 883e086edfSolivier moysan struct snd_soc_dai *cpu_dai; 893e086edfSolivier moysan struct snd_pcm_substream *substream; 903e086edfSolivier moysan struct stm32_sai_data *pdata; 913e086edfSolivier moysan struct clk *sai_ck; 923e086edfSolivier moysan dma_addr_t phys_addr; 933e086edfSolivier moysan unsigned int mclk_rate; 943e086edfSolivier moysan unsigned int id; 953e086edfSolivier moysan int dir; 963e086edfSolivier moysan bool master; 973e086edfSolivier moysan int fmt; 983e086edfSolivier moysan int sync; 993e086edfSolivier moysan int fs_length; 1003e086edfSolivier moysan int slots; 1013e086edfSolivier moysan int slot_width; 1023e086edfSolivier moysan int slot_mask; 1033e086edfSolivier moysan int data_size; 1043e086edfSolivier moysan }; 1053e086edfSolivier moysan 1063e086edfSolivier moysan enum stm32_sai_fifo_th { 1073e086edfSolivier moysan STM_SAI_FIFO_TH_EMPTY, 1083e086edfSolivier moysan STM_SAI_FIFO_TH_QUARTER, 1093e086edfSolivier moysan STM_SAI_FIFO_TH_HALF, 1103e086edfSolivier moysan STM_SAI_FIFO_TH_3_QUARTER, 1113e086edfSolivier moysan STM_SAI_FIFO_TH_FULL, 1123e086edfSolivier moysan }; 1133e086edfSolivier moysan 1143e086edfSolivier moysan static bool stm32_sai_sub_readable_reg(struct device *dev, unsigned int reg) 1153e086edfSolivier moysan { 1163e086edfSolivier moysan switch (reg) { 1173e086edfSolivier moysan case STM_SAI_CR1_REGX: 1183e086edfSolivier moysan case STM_SAI_CR2_REGX: 1193e086edfSolivier moysan case STM_SAI_FRCR_REGX: 1203e086edfSolivier moysan case STM_SAI_SLOTR_REGX: 1213e086edfSolivier moysan case STM_SAI_IMR_REGX: 1223e086edfSolivier moysan case STM_SAI_SR_REGX: 1233e086edfSolivier moysan case STM_SAI_CLRFR_REGX: 1243e086edfSolivier moysan case STM_SAI_DR_REGX: 12503e78a24Solivier moysan case STM_SAI_PDMCR_REGX: 12603e78a24Solivier moysan case STM_SAI_PDMLY_REGX: 1273e086edfSolivier moysan return true; 1283e086edfSolivier moysan default: 1293e086edfSolivier moysan return false; 1303e086edfSolivier moysan } 1313e086edfSolivier moysan } 1323e086edfSolivier moysan 1333e086edfSolivier moysan static bool stm32_sai_sub_volatile_reg(struct device *dev, unsigned int reg) 1343e086edfSolivier moysan { 1353e086edfSolivier moysan switch (reg) { 1363e086edfSolivier moysan case STM_SAI_DR_REGX: 1373e086edfSolivier moysan return true; 1383e086edfSolivier moysan default: 1393e086edfSolivier moysan return false; 1403e086edfSolivier moysan } 1413e086edfSolivier moysan } 1423e086edfSolivier moysan 1433e086edfSolivier moysan static bool stm32_sai_sub_writeable_reg(struct device *dev, unsigned int reg) 1443e086edfSolivier moysan { 1453e086edfSolivier moysan switch (reg) { 1463e086edfSolivier moysan case STM_SAI_CR1_REGX: 1473e086edfSolivier moysan case STM_SAI_CR2_REGX: 1483e086edfSolivier moysan case STM_SAI_FRCR_REGX: 1493e086edfSolivier moysan case STM_SAI_SLOTR_REGX: 1503e086edfSolivier moysan case STM_SAI_IMR_REGX: 1513e086edfSolivier moysan case STM_SAI_SR_REGX: 1523e086edfSolivier moysan case STM_SAI_CLRFR_REGX: 1533e086edfSolivier moysan case STM_SAI_DR_REGX: 15403e78a24Solivier moysan case STM_SAI_PDMCR_REGX: 15503e78a24Solivier moysan case STM_SAI_PDMLY_REGX: 1563e086edfSolivier moysan return true; 1573e086edfSolivier moysan default: 1583e086edfSolivier moysan return false; 1593e086edfSolivier moysan } 1603e086edfSolivier moysan } 1613e086edfSolivier moysan 16203e78a24Solivier moysan static const struct regmap_config stm32_sai_sub_regmap_config_f4 = { 1633e086edfSolivier moysan .reg_bits = 32, 1643e086edfSolivier moysan .reg_stride = 4, 1653e086edfSolivier moysan .val_bits = 32, 1663e086edfSolivier moysan .max_register = STM_SAI_DR_REGX, 1673e086edfSolivier moysan .readable_reg = stm32_sai_sub_readable_reg, 1683e086edfSolivier moysan .volatile_reg = stm32_sai_sub_volatile_reg, 1693e086edfSolivier moysan .writeable_reg = stm32_sai_sub_writeable_reg, 1703e086edfSolivier moysan .fast_io = true, 1713e086edfSolivier moysan }; 1723e086edfSolivier moysan 17303e78a24Solivier moysan static const struct regmap_config stm32_sai_sub_regmap_config_h7 = { 17403e78a24Solivier moysan .reg_bits = 32, 17503e78a24Solivier moysan .reg_stride = 4, 17603e78a24Solivier moysan .val_bits = 32, 17703e78a24Solivier moysan .max_register = STM_SAI_PDMLY_REGX, 17803e78a24Solivier moysan .readable_reg = stm32_sai_sub_readable_reg, 17903e78a24Solivier moysan .volatile_reg = stm32_sai_sub_volatile_reg, 18003e78a24Solivier moysan .writeable_reg = stm32_sai_sub_writeable_reg, 18103e78a24Solivier moysan .fast_io = true, 18203e78a24Solivier moysan }; 18303e78a24Solivier moysan 1843e086edfSolivier moysan static irqreturn_t stm32_sai_isr(int irq, void *devid) 1853e086edfSolivier moysan { 1863e086edfSolivier moysan struct stm32_sai_sub_data *sai = (struct stm32_sai_sub_data *)devid; 1873e086edfSolivier moysan struct platform_device *pdev = sai->pdev; 1883e086edfSolivier moysan unsigned int sr, imr, flags; 1893e086edfSolivier moysan snd_pcm_state_t status = SNDRV_PCM_STATE_RUNNING; 1903e086edfSolivier moysan 1913e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_IMR_REGX, &imr); 1923e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_SR_REGX, &sr); 1933e086edfSolivier moysan 1943e086edfSolivier moysan flags = sr & imr; 1953e086edfSolivier moysan if (!flags) 1963e086edfSolivier moysan return IRQ_NONE; 1973e086edfSolivier moysan 1983e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX, SAI_XCLRFR_MASK, 1993e086edfSolivier moysan SAI_XCLRFR_MASK); 2003e086edfSolivier moysan 201d807cdfbSOlivier Moysan if (!sai->substream) { 202d807cdfbSOlivier Moysan dev_err(&pdev->dev, "Device stopped. Spurious IRQ 0x%x\n", sr); 203d807cdfbSOlivier Moysan return IRQ_NONE; 204d807cdfbSOlivier Moysan } 205d807cdfbSOlivier Moysan 2063e086edfSolivier moysan if (flags & SAI_XIMR_OVRUDRIE) { 207602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ %s\n", 2083e086edfSolivier moysan STM_SAI_IS_PLAYBACK(sai) ? "underrun" : "overrun"); 2093e086edfSolivier moysan status = SNDRV_PCM_STATE_XRUN; 2103e086edfSolivier moysan } 2113e086edfSolivier moysan 2123e086edfSolivier moysan if (flags & SAI_XIMR_MUTEDETIE) 213602fdadcSolivier moysan dev_dbg(&pdev->dev, "IRQ mute detected\n"); 2143e086edfSolivier moysan 2153e086edfSolivier moysan if (flags & SAI_XIMR_WCKCFGIE) { 216602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ wrong clock configuration\n"); 2173e086edfSolivier moysan status = SNDRV_PCM_STATE_DISCONNECTED; 2183e086edfSolivier moysan } 2193e086edfSolivier moysan 2203e086edfSolivier moysan if (flags & SAI_XIMR_CNRDYIE) 221602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ Codec not ready\n"); 2223e086edfSolivier moysan 2233e086edfSolivier moysan if (flags & SAI_XIMR_AFSDETIE) { 224602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ Anticipated frame synchro\n"); 2253e086edfSolivier moysan status = SNDRV_PCM_STATE_XRUN; 2263e086edfSolivier moysan } 2273e086edfSolivier moysan 2283e086edfSolivier moysan if (flags & SAI_XIMR_LFSDETIE) { 229602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ Late frame synchro\n"); 2303e086edfSolivier moysan status = SNDRV_PCM_STATE_XRUN; 2313e086edfSolivier moysan } 2323e086edfSolivier moysan 2333e086edfSolivier moysan if (status != SNDRV_PCM_STATE_RUNNING) { 234d807cdfbSOlivier Moysan snd_pcm_stream_lock(sai->substream); 235d807cdfbSOlivier Moysan snd_pcm_stop(sai->substream, SNDRV_PCM_STATE_XRUN); 236d807cdfbSOlivier Moysan snd_pcm_stream_unlock(sai->substream); 2373e086edfSolivier moysan } 2383e086edfSolivier moysan 2393e086edfSolivier moysan return IRQ_HANDLED; 2403e086edfSolivier moysan } 2413e086edfSolivier moysan 2423e086edfSolivier moysan static int stm32_sai_set_sysclk(struct snd_soc_dai *cpu_dai, 2433e086edfSolivier moysan int clk_id, unsigned int freq, int dir) 2443e086edfSolivier moysan { 2453e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 246701a6ec3Solivier moysan int ret; 2473e086edfSolivier moysan 2483e086edfSolivier moysan if ((dir == SND_SOC_CLOCK_OUT) && sai->master) { 249701a6ec3Solivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 250701a6ec3Solivier moysan SAI_XCR1_NODIV, 251701a6ec3Solivier moysan (unsigned int)~SAI_XCR1_NODIV); 252701a6ec3Solivier moysan if (ret < 0) 253701a6ec3Solivier moysan return ret; 254701a6ec3Solivier moysan 2553e086edfSolivier moysan sai->mclk_rate = freq; 2563e086edfSolivier moysan dev_dbg(cpu_dai->dev, "SAI MCLK frequency is %uHz\n", freq); 2573e086edfSolivier moysan } 2583e086edfSolivier moysan 2593e086edfSolivier moysan return 0; 2603e086edfSolivier moysan } 2613e086edfSolivier moysan 2623e086edfSolivier moysan static int stm32_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask, 2633e086edfSolivier moysan u32 rx_mask, int slots, int slot_width) 2643e086edfSolivier moysan { 2653e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 2663e086edfSolivier moysan int slotr, slotr_mask, slot_size; 2673e086edfSolivier moysan 268602fdadcSolivier moysan dev_dbg(cpu_dai->dev, "Masks tx/rx:%#x/%#x, slots:%d, width:%d\n", 2693e086edfSolivier moysan tx_mask, rx_mask, slots, slot_width); 2703e086edfSolivier moysan 2713e086edfSolivier moysan switch (slot_width) { 2723e086edfSolivier moysan case 16: 2733e086edfSolivier moysan slot_size = SAI_SLOT_SIZE_16; 2743e086edfSolivier moysan break; 2753e086edfSolivier moysan case 32: 2763e086edfSolivier moysan slot_size = SAI_SLOT_SIZE_32; 2773e086edfSolivier moysan break; 2783e086edfSolivier moysan default: 2793e086edfSolivier moysan slot_size = SAI_SLOT_SIZE_AUTO; 2803e086edfSolivier moysan break; 2813e086edfSolivier moysan } 2823e086edfSolivier moysan 2833e086edfSolivier moysan slotr = SAI_XSLOTR_SLOTSZ_SET(slot_size) | 2843e086edfSolivier moysan SAI_XSLOTR_NBSLOT_SET(slots - 1); 2853e086edfSolivier moysan slotr_mask = SAI_XSLOTR_SLOTSZ_MASK | SAI_XSLOTR_NBSLOT_MASK; 2863e086edfSolivier moysan 2873e086edfSolivier moysan /* tx/rx mask set in machine init, if slot number defined in DT */ 2883e086edfSolivier moysan if (STM_SAI_IS_PLAYBACK(sai)) { 2893e086edfSolivier moysan sai->slot_mask = tx_mask; 2903e086edfSolivier moysan slotr |= SAI_XSLOTR_SLOTEN_SET(tx_mask); 2913e086edfSolivier moysan } 2923e086edfSolivier moysan 2933e086edfSolivier moysan if (STM_SAI_IS_CAPTURE(sai)) { 2943e086edfSolivier moysan sai->slot_mask = rx_mask; 2953e086edfSolivier moysan slotr |= SAI_XSLOTR_SLOTEN_SET(rx_mask); 2963e086edfSolivier moysan } 2973e086edfSolivier moysan 2983e086edfSolivier moysan slotr_mask |= SAI_XSLOTR_SLOTEN_MASK; 2993e086edfSolivier moysan 3003e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, slotr_mask, slotr); 3013e086edfSolivier moysan 3023e086edfSolivier moysan sai->slot_width = slot_width; 3033e086edfSolivier moysan sai->slots = slots; 3043e086edfSolivier moysan 3053e086edfSolivier moysan return 0; 3063e086edfSolivier moysan } 3073e086edfSolivier moysan 3083e086edfSolivier moysan static int stm32_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt) 3093e086edfSolivier moysan { 3103e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 311*61fb4ff7SOlivier Moysan int cr1, frcr = 0; 312*61fb4ff7SOlivier Moysan int cr1_mask, frcr_mask = 0; 3133e086edfSolivier moysan int ret; 3143e086edfSolivier moysan 3153e086edfSolivier moysan dev_dbg(cpu_dai->dev, "fmt %x\n", fmt); 3163e086edfSolivier moysan 317*61fb4ff7SOlivier Moysan cr1_mask = SAI_XCR1_PRTCFG_MASK; 318*61fb4ff7SOlivier Moysan cr1 = SAI_XCR1_PRTCFG_SET(SAI_FREE_PROTOCOL); 319*61fb4ff7SOlivier Moysan 3203e086edfSolivier moysan switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 3213e086edfSolivier moysan /* SCK active high for all protocols */ 3223e086edfSolivier moysan case SND_SOC_DAIFMT_I2S: 3233e086edfSolivier moysan cr1 |= SAI_XCR1_CKSTR; 3243e086edfSolivier moysan frcr |= SAI_XFRCR_FSOFF | SAI_XFRCR_FSDEF; 3253e086edfSolivier moysan break; 3263e086edfSolivier moysan /* Left justified */ 3273e086edfSolivier moysan case SND_SOC_DAIFMT_MSB: 3283e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF; 3293e086edfSolivier moysan break; 3303e086edfSolivier moysan /* Right justified */ 3313e086edfSolivier moysan case SND_SOC_DAIFMT_LSB: 3323e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF; 3333e086edfSolivier moysan break; 3343e086edfSolivier moysan case SND_SOC_DAIFMT_DSP_A: 3353e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF; 3363e086edfSolivier moysan break; 3373e086edfSolivier moysan case SND_SOC_DAIFMT_DSP_B: 3383e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL; 3393e086edfSolivier moysan break; 3403e086edfSolivier moysan default: 3413e086edfSolivier moysan dev_err(cpu_dai->dev, "Unsupported protocol %#x\n", 3423e086edfSolivier moysan fmt & SND_SOC_DAIFMT_FORMAT_MASK); 3433e086edfSolivier moysan return -EINVAL; 3443e086edfSolivier moysan } 3453e086edfSolivier moysan 346*61fb4ff7SOlivier Moysan cr1_mask |= SAI_XCR1_CKSTR; 3473e086edfSolivier moysan frcr_mask |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF | 3483e086edfSolivier moysan SAI_XFRCR_FSDEF; 3493e086edfSolivier moysan 3503e086edfSolivier moysan /* DAI clock strobing. Invert setting previously set */ 3513e086edfSolivier moysan switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 3523e086edfSolivier moysan case SND_SOC_DAIFMT_NB_NF: 3533e086edfSolivier moysan break; 3543e086edfSolivier moysan case SND_SOC_DAIFMT_IB_NF: 3553e086edfSolivier moysan cr1 ^= SAI_XCR1_CKSTR; 3563e086edfSolivier moysan break; 3573e086edfSolivier moysan case SND_SOC_DAIFMT_NB_IF: 3583e086edfSolivier moysan frcr ^= SAI_XFRCR_FSPOL; 3593e086edfSolivier moysan break; 3603e086edfSolivier moysan case SND_SOC_DAIFMT_IB_IF: 3613e086edfSolivier moysan /* Invert fs & sck */ 3623e086edfSolivier moysan cr1 ^= SAI_XCR1_CKSTR; 3633e086edfSolivier moysan frcr ^= SAI_XFRCR_FSPOL; 3643e086edfSolivier moysan break; 3653e086edfSolivier moysan default: 3663e086edfSolivier moysan dev_err(cpu_dai->dev, "Unsupported strobing %#x\n", 3673e086edfSolivier moysan fmt & SND_SOC_DAIFMT_INV_MASK); 3683e086edfSolivier moysan return -EINVAL; 3693e086edfSolivier moysan } 3703e086edfSolivier moysan cr1_mask |= SAI_XCR1_CKSTR; 3713e086edfSolivier moysan frcr_mask |= SAI_XFRCR_FSPOL; 3723e086edfSolivier moysan 3733e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr); 3743e086edfSolivier moysan 3753e086edfSolivier moysan /* DAI clock master masks */ 3763e086edfSolivier moysan switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 3773e086edfSolivier moysan case SND_SOC_DAIFMT_CBM_CFM: 3783e086edfSolivier moysan /* codec is master */ 3793e086edfSolivier moysan cr1 |= SAI_XCR1_SLAVE; 3803e086edfSolivier moysan sai->master = false; 3813e086edfSolivier moysan break; 3823e086edfSolivier moysan case SND_SOC_DAIFMT_CBS_CFS: 3833e086edfSolivier moysan sai->master = true; 3843e086edfSolivier moysan break; 3853e086edfSolivier moysan default: 3863e086edfSolivier moysan dev_err(cpu_dai->dev, "Unsupported mode %#x\n", 3873e086edfSolivier moysan fmt & SND_SOC_DAIFMT_MASTER_MASK); 3883e086edfSolivier moysan return -EINVAL; 3893e086edfSolivier moysan } 3903e086edfSolivier moysan cr1_mask |= SAI_XCR1_SLAVE; 3913e086edfSolivier moysan 392701a6ec3Solivier moysan /* do not generate master by default */ 393701a6ec3Solivier moysan cr1 |= SAI_XCR1_NODIV; 394701a6ec3Solivier moysan cr1_mask |= SAI_XCR1_NODIV; 395701a6ec3Solivier moysan 3963e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1); 3973e086edfSolivier moysan if (ret < 0) { 3983e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 3993e086edfSolivier moysan return ret; 4003e086edfSolivier moysan } 4013e086edfSolivier moysan 4023e086edfSolivier moysan sai->fmt = fmt; 4033e086edfSolivier moysan 4043e086edfSolivier moysan return 0; 4053e086edfSolivier moysan } 4063e086edfSolivier moysan 4073e086edfSolivier moysan static int stm32_sai_startup(struct snd_pcm_substream *substream, 4083e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 4093e086edfSolivier moysan { 4103e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 4113e086edfSolivier moysan int imr, cr2, ret; 4123e086edfSolivier moysan 4133e086edfSolivier moysan sai->substream = substream; 4143e086edfSolivier moysan 4153e086edfSolivier moysan ret = clk_prepare_enable(sai->sai_ck); 4163e086edfSolivier moysan if (ret < 0) { 417602fdadcSolivier moysan dev_err(cpu_dai->dev, "Failed to enable clock: %d\n", ret); 4183e086edfSolivier moysan return ret; 4193e086edfSolivier moysan } 4203e086edfSolivier moysan 4213e086edfSolivier moysan /* Enable ITs */ 4223e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_SR_REGX, 4233e086edfSolivier moysan SAI_XSR_MASK, (unsigned int)~SAI_XSR_MASK); 4243e086edfSolivier moysan 4253e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX, 4263e086edfSolivier moysan SAI_XCLRFR_MASK, SAI_XCLRFR_MASK); 4273e086edfSolivier moysan 4283e086edfSolivier moysan imr = SAI_XIMR_OVRUDRIE; 4293e086edfSolivier moysan if (STM_SAI_IS_CAPTURE(sai)) { 4303e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_CR2_REGX, &cr2); 4313e086edfSolivier moysan if (cr2 & SAI_XCR2_MUTECNT_MASK) 4323e086edfSolivier moysan imr |= SAI_XIMR_MUTEDETIE; 4333e086edfSolivier moysan } 4343e086edfSolivier moysan 4353e086edfSolivier moysan if (sai->master) 4363e086edfSolivier moysan imr |= SAI_XIMR_WCKCFGIE; 4373e086edfSolivier moysan else 4383e086edfSolivier moysan imr |= SAI_XIMR_AFSDETIE | SAI_XIMR_LFSDETIE; 4393e086edfSolivier moysan 4403e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, 4413e086edfSolivier moysan SAI_XIMR_MASK, imr); 4423e086edfSolivier moysan 4433e086edfSolivier moysan return 0; 4443e086edfSolivier moysan } 4453e086edfSolivier moysan 4463e086edfSolivier moysan static int stm32_sai_set_config(struct snd_soc_dai *cpu_dai, 4473e086edfSolivier moysan struct snd_pcm_substream *substream, 4483e086edfSolivier moysan struct snd_pcm_hw_params *params) 4493e086edfSolivier moysan { 4503e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 4513e086edfSolivier moysan int cr1, cr1_mask, ret; 4523e086edfSolivier moysan 453a4529d2bSOlivier Moysan /* 454a4529d2bSOlivier Moysan * DMA bursts increment is set to 4 words. 455a4529d2bSOlivier Moysan * SAI fifo threshold is set to half fifo, to keep enough space 456a4529d2bSOlivier Moysan * for DMA incoming bursts. 457a4529d2bSOlivier Moysan */ 4583e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR2_REGX, 4593e086edfSolivier moysan SAI_XCR2_FFLUSH | SAI_XCR2_FTH_MASK, 460a4529d2bSOlivier Moysan SAI_XCR2_FFLUSH | 461a4529d2bSOlivier Moysan SAI_XCR2_FTH_SET(STM_SAI_FIFO_TH_HALF)); 4623e086edfSolivier moysan 4633e086edfSolivier moysan /* Mode, data format and channel config */ 464*61fb4ff7SOlivier Moysan cr1_mask = SAI_XCR1_DS_MASK; 4653e086edfSolivier moysan switch (params_format(params)) { 4663e086edfSolivier moysan case SNDRV_PCM_FORMAT_S8: 4673e086edfSolivier moysan cr1 |= SAI_XCR1_DS_SET(SAI_DATASIZE_8); 4683e086edfSolivier moysan break; 4693e086edfSolivier moysan case SNDRV_PCM_FORMAT_S16_LE: 4703e086edfSolivier moysan cr1 |= SAI_XCR1_DS_SET(SAI_DATASIZE_16); 4713e086edfSolivier moysan break; 4723e086edfSolivier moysan case SNDRV_PCM_FORMAT_S32_LE: 4733e086edfSolivier moysan cr1 |= SAI_XCR1_DS_SET(SAI_DATASIZE_32); 4743e086edfSolivier moysan break; 4753e086edfSolivier moysan default: 4763e086edfSolivier moysan dev_err(cpu_dai->dev, "Data format not supported"); 4773e086edfSolivier moysan return -EINVAL; 4783e086edfSolivier moysan } 4793e086edfSolivier moysan 4803e086edfSolivier moysan cr1_mask |= SAI_XCR1_MONO; 4813e086edfSolivier moysan if ((sai->slots == 2) && (params_channels(params) == 1)) 4823e086edfSolivier moysan cr1 |= SAI_XCR1_MONO; 4833e086edfSolivier moysan 4843e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1); 4853e086edfSolivier moysan if (ret < 0) { 4863e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 4873e086edfSolivier moysan return ret; 4883e086edfSolivier moysan } 4893e086edfSolivier moysan 4903e086edfSolivier moysan return 0; 4913e086edfSolivier moysan } 4923e086edfSolivier moysan 4933e086edfSolivier moysan static int stm32_sai_set_slots(struct snd_soc_dai *cpu_dai) 4943e086edfSolivier moysan { 4953e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 4963e086edfSolivier moysan int slotr, slot_sz; 4973e086edfSolivier moysan 4983e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_SLOTR_REGX, &slotr); 4993e086edfSolivier moysan 5003e086edfSolivier moysan /* 5013e086edfSolivier moysan * If SLOTSZ is set to auto in SLOTR, align slot width on data size 5023e086edfSolivier moysan * By default slot width = data size, if not forced from DT 5033e086edfSolivier moysan */ 5043e086edfSolivier moysan slot_sz = slotr & SAI_XSLOTR_SLOTSZ_MASK; 5053e086edfSolivier moysan if (slot_sz == SAI_XSLOTR_SLOTSZ_SET(SAI_SLOT_SIZE_AUTO)) 5063e086edfSolivier moysan sai->slot_width = sai->data_size; 5073e086edfSolivier moysan 5083e086edfSolivier moysan if (sai->slot_width < sai->data_size) { 5093e086edfSolivier moysan dev_err(cpu_dai->dev, 5103e086edfSolivier moysan "Data size %d larger than slot width\n", 5113e086edfSolivier moysan sai->data_size); 5123e086edfSolivier moysan return -EINVAL; 5133e086edfSolivier moysan } 5143e086edfSolivier moysan 5153e086edfSolivier moysan /* Slot number is set to 2, if not specified in DT */ 5163e086edfSolivier moysan if (!sai->slots) 5173e086edfSolivier moysan sai->slots = 2; 5183e086edfSolivier moysan 5193e086edfSolivier moysan /* The number of slots in the audio frame is equal to NBSLOT[3:0] + 1*/ 5203e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, 5213e086edfSolivier moysan SAI_XSLOTR_NBSLOT_MASK, 5223e086edfSolivier moysan SAI_XSLOTR_NBSLOT_SET((sai->slots - 1))); 5233e086edfSolivier moysan 5243e086edfSolivier moysan /* Set default slots mask if not already set from DT */ 5253e086edfSolivier moysan if (!(slotr & SAI_XSLOTR_SLOTEN_MASK)) { 5263e086edfSolivier moysan sai->slot_mask = (1 << sai->slots) - 1; 5273e086edfSolivier moysan regmap_update_bits(sai->regmap, 5283e086edfSolivier moysan STM_SAI_SLOTR_REGX, SAI_XSLOTR_SLOTEN_MASK, 5293e086edfSolivier moysan SAI_XSLOTR_SLOTEN_SET(sai->slot_mask)); 5303e086edfSolivier moysan } 5313e086edfSolivier moysan 532602fdadcSolivier moysan dev_dbg(cpu_dai->dev, "Slots %d, slot width %d\n", 5333e086edfSolivier moysan sai->slots, sai->slot_width); 5343e086edfSolivier moysan 5353e086edfSolivier moysan return 0; 5363e086edfSolivier moysan } 5373e086edfSolivier moysan 5383e086edfSolivier moysan static void stm32_sai_set_frame(struct snd_soc_dai *cpu_dai) 5393e086edfSolivier moysan { 5403e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 5413e086edfSolivier moysan int fs_active, offset, format; 5423e086edfSolivier moysan int frcr, frcr_mask; 5433e086edfSolivier moysan 5443e086edfSolivier moysan format = sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK; 5453e086edfSolivier moysan sai->fs_length = sai->slot_width * sai->slots; 5463e086edfSolivier moysan 5473e086edfSolivier moysan fs_active = sai->fs_length / 2; 5483e086edfSolivier moysan if ((format == SND_SOC_DAIFMT_DSP_A) || 5493e086edfSolivier moysan (format == SND_SOC_DAIFMT_DSP_B)) 5503e086edfSolivier moysan fs_active = 1; 5513e086edfSolivier moysan 5523e086edfSolivier moysan frcr = SAI_XFRCR_FRL_SET((sai->fs_length - 1)); 5533e086edfSolivier moysan frcr |= SAI_XFRCR_FSALL_SET((fs_active - 1)); 5543e086edfSolivier moysan frcr_mask = SAI_XFRCR_FRL_MASK | SAI_XFRCR_FSALL_MASK; 5553e086edfSolivier moysan 556602fdadcSolivier moysan dev_dbg(cpu_dai->dev, "Frame length %d, frame active %d\n", 5573e086edfSolivier moysan sai->fs_length, fs_active); 5583e086edfSolivier moysan 5593e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr); 5603e086edfSolivier moysan 5613e086edfSolivier moysan if ((sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_LSB) { 5623e086edfSolivier moysan offset = sai->slot_width - sai->data_size; 5633e086edfSolivier moysan 5643e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, 5653e086edfSolivier moysan SAI_XSLOTR_FBOFF_MASK, 5663e086edfSolivier moysan SAI_XSLOTR_FBOFF_SET(offset)); 5673e086edfSolivier moysan } 5683e086edfSolivier moysan } 5693e086edfSolivier moysan 5703e086edfSolivier moysan static int stm32_sai_configure_clock(struct snd_soc_dai *cpu_dai, 5713e086edfSolivier moysan struct snd_pcm_hw_params *params) 5723e086edfSolivier moysan { 5733e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 5743e086edfSolivier moysan int cr1, mask, div = 0; 57503e78a24Solivier moysan int sai_clk_rate, mclk_ratio, den, ret; 57603e78a24Solivier moysan int version = sai->pdata->conf->version; 5773e086edfSolivier moysan 5783e086edfSolivier moysan if (!sai->mclk_rate) { 5793e086edfSolivier moysan dev_err(cpu_dai->dev, "Mclk rate is null\n"); 5803e086edfSolivier moysan return -EINVAL; 5813e086edfSolivier moysan } 5823e086edfSolivier moysan 5833e086edfSolivier moysan if (!(params_rate(params) % 11025)) 5843e086edfSolivier moysan clk_set_parent(sai->sai_ck, sai->pdata->clk_x11k); 5853e086edfSolivier moysan else 5863e086edfSolivier moysan clk_set_parent(sai->sai_ck, sai->pdata->clk_x8k); 5873e086edfSolivier moysan sai_clk_rate = clk_get_rate(sai->sai_ck); 5883e086edfSolivier moysan 58903e78a24Solivier moysan if (STM_SAI_IS_F4(sai->pdata)) { 5903e086edfSolivier moysan /* 5913e086edfSolivier moysan * mclk_rate = 256 * fs 5923e086edfSolivier moysan * MCKDIV = 0 if sai_ck < 3/2 * mclk_rate 5933e086edfSolivier moysan * MCKDIV = sai_ck / (2 * mclk_rate) otherwise 5943e086edfSolivier moysan */ 5953e086edfSolivier moysan if (2 * sai_clk_rate >= 3 * sai->mclk_rate) 59603e78a24Solivier moysan div = DIV_ROUND_CLOSEST(sai_clk_rate, 59703e78a24Solivier moysan 2 * sai->mclk_rate); 59803e78a24Solivier moysan } else { 59903e78a24Solivier moysan /* 60003e78a24Solivier moysan * TDM mode : 60103e78a24Solivier moysan * mclk on 60203e78a24Solivier moysan * MCKDIV = sai_ck / (ws x 256) (NOMCK=0. OSR=0) 60303e78a24Solivier moysan * MCKDIV = sai_ck / (ws x 512) (NOMCK=0. OSR=1) 60403e78a24Solivier moysan * mclk off 60503e78a24Solivier moysan * MCKDIV = sai_ck / (frl x ws) (NOMCK=1) 60603e78a24Solivier moysan * Note: NOMCK/NODIV correspond to same bit. 60703e78a24Solivier moysan */ 60803e78a24Solivier moysan if (sai->mclk_rate) { 60903e78a24Solivier moysan mclk_ratio = sai->mclk_rate / params_rate(params); 61003e78a24Solivier moysan if (mclk_ratio != 256) { 61103e78a24Solivier moysan if (mclk_ratio == 512) { 61203e78a24Solivier moysan mask = SAI_XCR1_OSR; 61303e78a24Solivier moysan cr1 = SAI_XCR1_OSR; 61403e78a24Solivier moysan } else { 61503e78a24Solivier moysan dev_err(cpu_dai->dev, 61603e78a24Solivier moysan "Wrong mclk ratio %d\n", 61703e78a24Solivier moysan mclk_ratio); 61803e78a24Solivier moysan return -EINVAL; 61903e78a24Solivier moysan } 62003e78a24Solivier moysan } 62103e78a24Solivier moysan div = DIV_ROUND_CLOSEST(sai_clk_rate, sai->mclk_rate); 62203e78a24Solivier moysan } else { 62303e78a24Solivier moysan /* mclk-fs not set, master clock not active. NOMCK=1 */ 62403e78a24Solivier moysan den = sai->fs_length * params_rate(params); 62503e78a24Solivier moysan div = DIV_ROUND_CLOSEST(sai_clk_rate, den); 62603e78a24Solivier moysan } 62703e78a24Solivier moysan } 6283e086edfSolivier moysan 62903e78a24Solivier moysan if (div > SAI_XCR1_MCKDIV_MAX(version)) { 6303e086edfSolivier moysan dev_err(cpu_dai->dev, "Divider %d out of range\n", div); 6313e086edfSolivier moysan return -EINVAL; 6323e086edfSolivier moysan } 6333e086edfSolivier moysan dev_dbg(cpu_dai->dev, "SAI clock %d, divider %d\n", sai_clk_rate, div); 6343e086edfSolivier moysan 63503e78a24Solivier moysan mask = SAI_XCR1_MCKDIV_MASK(SAI_XCR1_MCKDIV_WIDTH(version)); 6363e086edfSolivier moysan cr1 = SAI_XCR1_MCKDIV_SET(div); 6373e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, mask, cr1); 6383e086edfSolivier moysan if (ret < 0) { 6393e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 6403e086edfSolivier moysan return ret; 6413e086edfSolivier moysan } 6423e086edfSolivier moysan 6433e086edfSolivier moysan return 0; 6443e086edfSolivier moysan } 6453e086edfSolivier moysan 6463e086edfSolivier moysan static int stm32_sai_hw_params(struct snd_pcm_substream *substream, 6473e086edfSolivier moysan struct snd_pcm_hw_params *params, 6483e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 6493e086edfSolivier moysan { 6503e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 6513e086edfSolivier moysan int ret; 6523e086edfSolivier moysan 6533e086edfSolivier moysan sai->data_size = params_width(params); 6543e086edfSolivier moysan 6553e086edfSolivier moysan ret = stm32_sai_set_slots(cpu_dai); 6563e086edfSolivier moysan if (ret < 0) 6573e086edfSolivier moysan return ret; 6583e086edfSolivier moysan stm32_sai_set_frame(cpu_dai); 6593e086edfSolivier moysan 6603e086edfSolivier moysan ret = stm32_sai_set_config(cpu_dai, substream, params); 6613e086edfSolivier moysan if (ret) 6623e086edfSolivier moysan return ret; 6633e086edfSolivier moysan 6643e086edfSolivier moysan if (sai->master) 6653e086edfSolivier moysan ret = stm32_sai_configure_clock(cpu_dai, params); 6663e086edfSolivier moysan 6673e086edfSolivier moysan return ret; 6683e086edfSolivier moysan } 6693e086edfSolivier moysan 6703e086edfSolivier moysan static int stm32_sai_trigger(struct snd_pcm_substream *substream, int cmd, 6713e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 6723e086edfSolivier moysan { 6733e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 6743e086edfSolivier moysan int ret; 6753e086edfSolivier moysan 6763e086edfSolivier moysan switch (cmd) { 6773e086edfSolivier moysan case SNDRV_PCM_TRIGGER_START: 6783e086edfSolivier moysan case SNDRV_PCM_TRIGGER_RESUME: 6793e086edfSolivier moysan case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 6803e086edfSolivier moysan dev_dbg(cpu_dai->dev, "Enable DMA and SAI\n"); 6813e086edfSolivier moysan 6823e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 6833e086edfSolivier moysan SAI_XCR1_DMAEN, SAI_XCR1_DMAEN); 6843e086edfSolivier moysan 6853e086edfSolivier moysan /* Enable SAI */ 6863e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 6873e086edfSolivier moysan SAI_XCR1_SAIEN, SAI_XCR1_SAIEN); 6883e086edfSolivier moysan if (ret < 0) 6893e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 6903e086edfSolivier moysan break; 6913e086edfSolivier moysan case SNDRV_PCM_TRIGGER_SUSPEND: 6923e086edfSolivier moysan case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 6933e086edfSolivier moysan case SNDRV_PCM_TRIGGER_STOP: 6943e086edfSolivier moysan dev_dbg(cpu_dai->dev, "Disable DMA and SAI\n"); 6953e086edfSolivier moysan 6963e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 6973e086edfSolivier moysan SAI_XCR1_SAIEN, 6983e086edfSolivier moysan (unsigned int)~SAI_XCR1_SAIEN); 6994fa17938Solivier moysan 7004fa17938Solivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 7014fa17938Solivier moysan SAI_XCR1_DMAEN, 7024fa17938Solivier moysan (unsigned int)~SAI_XCR1_DMAEN); 7033e086edfSolivier moysan if (ret < 0) 7043e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 7053e086edfSolivier moysan break; 7063e086edfSolivier moysan default: 7073e086edfSolivier moysan return -EINVAL; 7083e086edfSolivier moysan } 7093e086edfSolivier moysan 7103e086edfSolivier moysan return ret; 7113e086edfSolivier moysan } 7123e086edfSolivier moysan 7133e086edfSolivier moysan static void stm32_sai_shutdown(struct snd_pcm_substream *substream, 7143e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 7153e086edfSolivier moysan { 7163e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 7173e086edfSolivier moysan 7183e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, SAI_XIMR_MASK, 0); 7193e086edfSolivier moysan 720701a6ec3Solivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, SAI_XCR1_NODIV, 721701a6ec3Solivier moysan SAI_XCR1_NODIV); 722701a6ec3Solivier moysan 7233e086edfSolivier moysan clk_disable_unprepare(sai->sai_ck); 7243e086edfSolivier moysan sai->substream = NULL; 7253e086edfSolivier moysan } 7263e086edfSolivier moysan 7273e086edfSolivier moysan static int stm32_sai_dai_probe(struct snd_soc_dai *cpu_dai) 7283e086edfSolivier moysan { 7293e086edfSolivier moysan struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev); 730*61fb4ff7SOlivier Moysan int cr1 = 0, cr1_mask; 7313e086edfSolivier moysan 7323e086edfSolivier moysan sai->dma_params.addr = (dma_addr_t)(sai->phys_addr + STM_SAI_DR_REGX); 733a4529d2bSOlivier Moysan /* 734a4529d2bSOlivier Moysan * DMA supports 4, 8 or 16 burst sizes. Burst size 4 is the best choice, 735a4529d2bSOlivier Moysan * as it allows bytes, half-word and words transfers. (See DMA fifos 736a4529d2bSOlivier Moysan * constraints). 737a4529d2bSOlivier Moysan */ 738a4529d2bSOlivier Moysan sai->dma_params.maxburst = 4; 7393e086edfSolivier moysan /* Buswidth will be set by framework at runtime */ 7403e086edfSolivier moysan sai->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED; 7413e086edfSolivier moysan 7423e086edfSolivier moysan if (STM_SAI_IS_PLAYBACK(sai)) 7433e086edfSolivier moysan snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params, NULL); 7443e086edfSolivier moysan else 7453e086edfSolivier moysan snd_soc_dai_init_dma_data(cpu_dai, NULL, &sai->dma_params); 7463e086edfSolivier moysan 747*61fb4ff7SOlivier Moysan cr1_mask = SAI_XCR1_RX_TX; 748*61fb4ff7SOlivier Moysan if (STM_SAI_IS_CAPTURE(sai)) 749*61fb4ff7SOlivier Moysan cr1 |= SAI_XCR1_RX_TX; 750*61fb4ff7SOlivier Moysan 751*61fb4ff7SOlivier Moysan return regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1); 7523e086edfSolivier moysan } 7533e086edfSolivier moysan 7543e086edfSolivier moysan static const struct snd_soc_dai_ops stm32_sai_pcm_dai_ops = { 7553e086edfSolivier moysan .set_sysclk = stm32_sai_set_sysclk, 7563e086edfSolivier moysan .set_fmt = stm32_sai_set_dai_fmt, 7573e086edfSolivier moysan .set_tdm_slot = stm32_sai_set_dai_tdm_slot, 7583e086edfSolivier moysan .startup = stm32_sai_startup, 7593e086edfSolivier moysan .hw_params = stm32_sai_hw_params, 7603e086edfSolivier moysan .trigger = stm32_sai_trigger, 7613e086edfSolivier moysan .shutdown = stm32_sai_shutdown, 7623e086edfSolivier moysan }; 7633e086edfSolivier moysan 7643e086edfSolivier moysan static const struct snd_pcm_hardware stm32_sai_pcm_hw = { 7653e086edfSolivier moysan .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP, 7663e086edfSolivier moysan .buffer_bytes_max = 8 * PAGE_SIZE, 7673e086edfSolivier moysan .period_bytes_min = 1024, /* 5ms at 48kHz */ 7683e086edfSolivier moysan .period_bytes_max = PAGE_SIZE, 7693e086edfSolivier moysan .periods_min = 2, 7703e086edfSolivier moysan .periods_max = 8, 7713e086edfSolivier moysan }; 7723e086edfSolivier moysan 7733e086edfSolivier moysan static struct snd_soc_dai_driver stm32_sai_playback_dai[] = { 7743e086edfSolivier moysan { 7753e086edfSolivier moysan .probe = stm32_sai_dai_probe, 7763e086edfSolivier moysan .id = 1, /* avoid call to fmt_single_name() */ 7773e086edfSolivier moysan .playback = { 7783e086edfSolivier moysan .channels_min = 1, 7793e086edfSolivier moysan .channels_max = 2, 7803e086edfSolivier moysan .rate_min = 8000, 7813e086edfSolivier moysan .rate_max = 192000, 7823e086edfSolivier moysan .rates = SNDRV_PCM_RATE_CONTINUOUS, 7833e086edfSolivier moysan /* DMA does not support 24 bits transfers */ 7843e086edfSolivier moysan .formats = 7853e086edfSolivier moysan SNDRV_PCM_FMTBIT_S8 | 7863e086edfSolivier moysan SNDRV_PCM_FMTBIT_S16_LE | 7873e086edfSolivier moysan SNDRV_PCM_FMTBIT_S32_LE, 7883e086edfSolivier moysan }, 7893e086edfSolivier moysan .ops = &stm32_sai_pcm_dai_ops, 7903e086edfSolivier moysan } 7913e086edfSolivier moysan }; 7923e086edfSolivier moysan 7933e086edfSolivier moysan static struct snd_soc_dai_driver stm32_sai_capture_dai[] = { 7943e086edfSolivier moysan { 7953e086edfSolivier moysan .probe = stm32_sai_dai_probe, 7963e086edfSolivier moysan .id = 1, /* avoid call to fmt_single_name() */ 7973e086edfSolivier moysan .capture = { 7983e086edfSolivier moysan .channels_min = 1, 7993e086edfSolivier moysan .channels_max = 2, 8003e086edfSolivier moysan .rate_min = 8000, 8013e086edfSolivier moysan .rate_max = 192000, 8023e086edfSolivier moysan .rates = SNDRV_PCM_RATE_CONTINUOUS, 8033e086edfSolivier moysan /* DMA does not support 24 bits transfers */ 8043e086edfSolivier moysan .formats = 8053e086edfSolivier moysan SNDRV_PCM_FMTBIT_S8 | 8063e086edfSolivier moysan SNDRV_PCM_FMTBIT_S16_LE | 8073e086edfSolivier moysan SNDRV_PCM_FMTBIT_S32_LE, 8083e086edfSolivier moysan }, 8093e086edfSolivier moysan .ops = &stm32_sai_pcm_dai_ops, 8103e086edfSolivier moysan } 8113e086edfSolivier moysan }; 8123e086edfSolivier moysan 8133e086edfSolivier moysan static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config = { 8143e086edfSolivier moysan .pcm_hardware = &stm32_sai_pcm_hw, 8153e086edfSolivier moysan .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config, 8163e086edfSolivier moysan }; 8173e086edfSolivier moysan 8183e086edfSolivier moysan static const struct snd_soc_component_driver stm32_component = { 8193e086edfSolivier moysan .name = "stm32-sai", 8203e086edfSolivier moysan }; 8213e086edfSolivier moysan 8223e086edfSolivier moysan static const struct of_device_id stm32_sai_sub_ids[] = { 8233e086edfSolivier moysan { .compatible = "st,stm32-sai-sub-a", 8243e086edfSolivier moysan .data = (void *)STM_SAI_A_ID}, 8253e086edfSolivier moysan { .compatible = "st,stm32-sai-sub-b", 8263e086edfSolivier moysan .data = (void *)STM_SAI_B_ID}, 8273e086edfSolivier moysan {} 8283e086edfSolivier moysan }; 8293e086edfSolivier moysan MODULE_DEVICE_TABLE(of, stm32_sai_sub_ids); 8303e086edfSolivier moysan 8313e086edfSolivier moysan static int stm32_sai_sub_parse_of(struct platform_device *pdev, 8323e086edfSolivier moysan struct stm32_sai_sub_data *sai) 8333e086edfSolivier moysan { 8343e086edfSolivier moysan struct device_node *np = pdev->dev.of_node; 8353e086edfSolivier moysan struct resource *res; 8363e086edfSolivier moysan void __iomem *base; 8373e086edfSolivier moysan 8383e086edfSolivier moysan if (!np) 8393e086edfSolivier moysan return -ENODEV; 8403e086edfSolivier moysan 8413e086edfSolivier moysan res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 8423e086edfSolivier moysan base = devm_ioremap_resource(&pdev->dev, res); 8433e086edfSolivier moysan if (IS_ERR(base)) 8443e086edfSolivier moysan return PTR_ERR(base); 8453e086edfSolivier moysan 8463e086edfSolivier moysan sai->phys_addr = res->start; 84703e78a24Solivier moysan 84803e78a24Solivier moysan sai->regmap_config = &stm32_sai_sub_regmap_config_f4; 84903e78a24Solivier moysan /* Note: PDM registers not available for H7 sub-block B */ 85003e78a24Solivier moysan if (STM_SAI_IS_H7(sai->pdata) && STM_SAI_IS_SUB_A(sai)) 85103e78a24Solivier moysan sai->regmap_config = &stm32_sai_sub_regmap_config_h7; 85203e78a24Solivier moysan 85303e78a24Solivier moysan sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "sai_ck", 85403e78a24Solivier moysan base, sai->regmap_config); 85503e78a24Solivier moysan if (IS_ERR(sai->regmap)) { 85603e78a24Solivier moysan dev_err(&pdev->dev, "Failed to initialize MMIO\n"); 85703e78a24Solivier moysan return PTR_ERR(sai->regmap); 85803e78a24Solivier moysan } 8593e086edfSolivier moysan 8603e086edfSolivier moysan /* Get direction property */ 8613e086edfSolivier moysan if (of_property_match_string(np, "dma-names", "tx") >= 0) { 8623e086edfSolivier moysan sai->dir = SNDRV_PCM_STREAM_PLAYBACK; 8633e086edfSolivier moysan } else if (of_property_match_string(np, "dma-names", "rx") >= 0) { 8643e086edfSolivier moysan sai->dir = SNDRV_PCM_STREAM_CAPTURE; 8653e086edfSolivier moysan } else { 8663e086edfSolivier moysan dev_err(&pdev->dev, "Unsupported direction\n"); 8673e086edfSolivier moysan return -EINVAL; 8683e086edfSolivier moysan } 8693e086edfSolivier moysan 8703e086edfSolivier moysan sai->sai_ck = devm_clk_get(&pdev->dev, "sai_ck"); 8713e086edfSolivier moysan if (IS_ERR(sai->sai_ck)) { 872602fdadcSolivier moysan dev_err(&pdev->dev, "Missing kernel clock sai_ck\n"); 8733e086edfSolivier moysan return PTR_ERR(sai->sai_ck); 8743e086edfSolivier moysan } 8753e086edfSolivier moysan 8763e086edfSolivier moysan return 0; 8773e086edfSolivier moysan } 8783e086edfSolivier moysan 8793e086edfSolivier moysan static int stm32_sai_sub_dais_init(struct platform_device *pdev, 8803e086edfSolivier moysan struct stm32_sai_sub_data *sai) 8813e086edfSolivier moysan { 8823e086edfSolivier moysan sai->cpu_dai_drv = devm_kzalloc(&pdev->dev, 8833e086edfSolivier moysan sizeof(struct snd_soc_dai_driver), 8843e086edfSolivier moysan GFP_KERNEL); 8853e086edfSolivier moysan if (!sai->cpu_dai_drv) 8863e086edfSolivier moysan return -ENOMEM; 8873e086edfSolivier moysan 8883e086edfSolivier moysan sai->cpu_dai_drv->name = dev_name(&pdev->dev); 8893e086edfSolivier moysan if (STM_SAI_IS_PLAYBACK(sai)) { 8903e086edfSolivier moysan memcpy(sai->cpu_dai_drv, &stm32_sai_playback_dai, 8913e086edfSolivier moysan sizeof(stm32_sai_playback_dai)); 8923e086edfSolivier moysan sai->cpu_dai_drv->playback.stream_name = sai->cpu_dai_drv->name; 8933e086edfSolivier moysan } else { 8943e086edfSolivier moysan memcpy(sai->cpu_dai_drv, &stm32_sai_capture_dai, 8953e086edfSolivier moysan sizeof(stm32_sai_capture_dai)); 8963e086edfSolivier moysan sai->cpu_dai_drv->capture.stream_name = sai->cpu_dai_drv->name; 8973e086edfSolivier moysan } 8983e086edfSolivier moysan 8993e086edfSolivier moysan return 0; 9003e086edfSolivier moysan } 9013e086edfSolivier moysan 9023e086edfSolivier moysan static int stm32_sai_sub_probe(struct platform_device *pdev) 9033e086edfSolivier moysan { 9043e086edfSolivier moysan struct stm32_sai_sub_data *sai; 9053e086edfSolivier moysan const struct of_device_id *of_id; 9063e086edfSolivier moysan int ret; 9073e086edfSolivier moysan 9083e086edfSolivier moysan sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL); 9093e086edfSolivier moysan if (!sai) 9103e086edfSolivier moysan return -ENOMEM; 9113e086edfSolivier moysan 9123e086edfSolivier moysan of_id = of_match_device(stm32_sai_sub_ids, &pdev->dev); 9133e086edfSolivier moysan if (!of_id) 9143e086edfSolivier moysan return -EINVAL; 9153e086edfSolivier moysan sai->id = (uintptr_t)of_id->data; 9163e086edfSolivier moysan 9173e086edfSolivier moysan sai->pdev = pdev; 9183e086edfSolivier moysan platform_set_drvdata(pdev, sai); 9193e086edfSolivier moysan 9203e086edfSolivier moysan sai->pdata = dev_get_drvdata(pdev->dev.parent); 9213e086edfSolivier moysan if (!sai->pdata) { 9223e086edfSolivier moysan dev_err(&pdev->dev, "Parent device data not available\n"); 9233e086edfSolivier moysan return -EINVAL; 9243e086edfSolivier moysan } 9253e086edfSolivier moysan 9263e086edfSolivier moysan ret = stm32_sai_sub_parse_of(pdev, sai); 9273e086edfSolivier moysan if (ret) 9283e086edfSolivier moysan return ret; 9293e086edfSolivier moysan 9303e086edfSolivier moysan ret = stm32_sai_sub_dais_init(pdev, sai); 9313e086edfSolivier moysan if (ret) 9323e086edfSolivier moysan return ret; 9333e086edfSolivier moysan 9343e086edfSolivier moysan ret = devm_request_irq(&pdev->dev, sai->pdata->irq, stm32_sai_isr, 9353e086edfSolivier moysan IRQF_SHARED, dev_name(&pdev->dev), sai); 9363e086edfSolivier moysan if (ret) { 937602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ request returned %d\n", ret); 9383e086edfSolivier moysan return ret; 9393e086edfSolivier moysan } 9403e086edfSolivier moysan 9413e086edfSolivier moysan ret = devm_snd_soc_register_component(&pdev->dev, &stm32_component, 9423e086edfSolivier moysan sai->cpu_dai_drv, 1); 9433e086edfSolivier moysan if (ret) 9443e086edfSolivier moysan return ret; 9453e086edfSolivier moysan 9463e086edfSolivier moysan ret = devm_snd_dmaengine_pcm_register(&pdev->dev, 9473e086edfSolivier moysan &stm32_sai_pcm_config, 0); 9483e086edfSolivier moysan if (ret) { 949602fdadcSolivier moysan dev_err(&pdev->dev, "Could not register pcm dma\n"); 9503e086edfSolivier moysan return ret; 9513e086edfSolivier moysan } 9523e086edfSolivier moysan 9533e086edfSolivier moysan return 0; 9543e086edfSolivier moysan } 9553e086edfSolivier moysan 9563e086edfSolivier moysan static struct platform_driver stm32_sai_sub_driver = { 9573e086edfSolivier moysan .driver = { 9583e086edfSolivier moysan .name = "st,stm32-sai-sub", 9593e086edfSolivier moysan .of_match_table = stm32_sai_sub_ids, 9603e086edfSolivier moysan }, 9613e086edfSolivier moysan .probe = stm32_sai_sub_probe, 9623e086edfSolivier moysan }; 9633e086edfSolivier moysan 9643e086edfSolivier moysan module_platform_driver(stm32_sai_sub_driver); 9653e086edfSolivier moysan 9663e086edfSolivier moysan MODULE_DESCRIPTION("STM32 Soc SAI sub-block Interface"); 967602fdadcSolivier moysan MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>"); 9683e086edfSolivier moysan MODULE_ALIAS("platform:st,stm32-sai-sub"); 9693e086edfSolivier moysan MODULE_LICENSE("GPL v2"); 970