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 201*d807cdfbSOlivier Moysan if (!sai->substream) { 202*d807cdfbSOlivier Moysan dev_err(&pdev->dev, "Device stopped. Spurious IRQ 0x%x\n", sr); 203*d807cdfbSOlivier Moysan return IRQ_NONE; 204*d807cdfbSOlivier Moysan } 205*d807cdfbSOlivier 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) { 234*d807cdfbSOlivier Moysan snd_pcm_stream_lock(sai->substream); 235*d807cdfbSOlivier Moysan snd_pcm_stop(sai->substream, SNDRV_PCM_STATE_XRUN); 236*d807cdfbSOlivier 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); 3113e086edfSolivier moysan int cr1 = 0, frcr = 0; 3123e086edfSolivier moysan int cr1_mask = 0, frcr_mask = 0; 3133e086edfSolivier moysan int ret; 3143e086edfSolivier moysan 3153e086edfSolivier moysan dev_dbg(cpu_dai->dev, "fmt %x\n", fmt); 3163e086edfSolivier moysan 3173e086edfSolivier moysan switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 3183e086edfSolivier moysan /* SCK active high for all protocols */ 3193e086edfSolivier moysan case SND_SOC_DAIFMT_I2S: 3203e086edfSolivier moysan cr1 |= SAI_XCR1_CKSTR; 3213e086edfSolivier moysan frcr |= SAI_XFRCR_FSOFF | SAI_XFRCR_FSDEF; 3223e086edfSolivier moysan break; 3233e086edfSolivier moysan /* Left justified */ 3243e086edfSolivier moysan case SND_SOC_DAIFMT_MSB: 3253e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF; 3263e086edfSolivier moysan break; 3273e086edfSolivier moysan /* Right justified */ 3283e086edfSolivier moysan case SND_SOC_DAIFMT_LSB: 3293e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF; 3303e086edfSolivier moysan break; 3313e086edfSolivier moysan case SND_SOC_DAIFMT_DSP_A: 3323e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF; 3333e086edfSolivier moysan break; 3343e086edfSolivier moysan case SND_SOC_DAIFMT_DSP_B: 3353e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL; 3363e086edfSolivier moysan break; 3373e086edfSolivier moysan default: 3383e086edfSolivier moysan dev_err(cpu_dai->dev, "Unsupported protocol %#x\n", 3393e086edfSolivier moysan fmt & SND_SOC_DAIFMT_FORMAT_MASK); 3403e086edfSolivier moysan return -EINVAL; 3413e086edfSolivier moysan } 3423e086edfSolivier moysan 3433e086edfSolivier moysan cr1_mask |= SAI_XCR1_PRTCFG_MASK | SAI_XCR1_CKSTR; 3443e086edfSolivier moysan frcr_mask |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF | 3453e086edfSolivier moysan SAI_XFRCR_FSDEF; 3463e086edfSolivier moysan 3473e086edfSolivier moysan /* DAI clock strobing. Invert setting previously set */ 3483e086edfSolivier moysan switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 3493e086edfSolivier moysan case SND_SOC_DAIFMT_NB_NF: 3503e086edfSolivier moysan break; 3513e086edfSolivier moysan case SND_SOC_DAIFMT_IB_NF: 3523e086edfSolivier moysan cr1 ^= SAI_XCR1_CKSTR; 3533e086edfSolivier moysan break; 3543e086edfSolivier moysan case SND_SOC_DAIFMT_NB_IF: 3553e086edfSolivier moysan frcr ^= SAI_XFRCR_FSPOL; 3563e086edfSolivier moysan break; 3573e086edfSolivier moysan case SND_SOC_DAIFMT_IB_IF: 3583e086edfSolivier moysan /* Invert fs & sck */ 3593e086edfSolivier moysan cr1 ^= SAI_XCR1_CKSTR; 3603e086edfSolivier moysan frcr ^= SAI_XFRCR_FSPOL; 3613e086edfSolivier moysan break; 3623e086edfSolivier moysan default: 3633e086edfSolivier moysan dev_err(cpu_dai->dev, "Unsupported strobing %#x\n", 3643e086edfSolivier moysan fmt & SND_SOC_DAIFMT_INV_MASK); 3653e086edfSolivier moysan return -EINVAL; 3663e086edfSolivier moysan } 3673e086edfSolivier moysan cr1_mask |= SAI_XCR1_CKSTR; 3683e086edfSolivier moysan frcr_mask |= SAI_XFRCR_FSPOL; 3693e086edfSolivier moysan 3703e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr); 3713e086edfSolivier moysan 3723e086edfSolivier moysan /* DAI clock master masks */ 3733e086edfSolivier moysan switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 3743e086edfSolivier moysan case SND_SOC_DAIFMT_CBM_CFM: 3753e086edfSolivier moysan /* codec is master */ 3763e086edfSolivier moysan cr1 |= SAI_XCR1_SLAVE; 3773e086edfSolivier moysan sai->master = false; 3783e086edfSolivier moysan break; 3793e086edfSolivier moysan case SND_SOC_DAIFMT_CBS_CFS: 3803e086edfSolivier moysan sai->master = true; 3813e086edfSolivier moysan break; 3823e086edfSolivier moysan default: 3833e086edfSolivier moysan dev_err(cpu_dai->dev, "Unsupported mode %#x\n", 3843e086edfSolivier moysan fmt & SND_SOC_DAIFMT_MASTER_MASK); 3853e086edfSolivier moysan return -EINVAL; 3863e086edfSolivier moysan } 3873e086edfSolivier moysan cr1_mask |= SAI_XCR1_SLAVE; 3883e086edfSolivier moysan 389701a6ec3Solivier moysan /* do not generate master by default */ 390701a6ec3Solivier moysan cr1 |= SAI_XCR1_NODIV; 391701a6ec3Solivier moysan cr1_mask |= SAI_XCR1_NODIV; 392701a6ec3Solivier moysan 3933e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1); 3943e086edfSolivier moysan if (ret < 0) { 3953e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 3963e086edfSolivier moysan return ret; 3973e086edfSolivier moysan } 3983e086edfSolivier moysan 3993e086edfSolivier moysan sai->fmt = fmt; 4003e086edfSolivier moysan 4013e086edfSolivier moysan return 0; 4023e086edfSolivier moysan } 4033e086edfSolivier moysan 4043e086edfSolivier moysan static int stm32_sai_startup(struct snd_pcm_substream *substream, 4053e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 4063e086edfSolivier moysan { 4073e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 4083e086edfSolivier moysan int imr, cr2, ret; 4093e086edfSolivier moysan 4103e086edfSolivier moysan sai->substream = substream; 4113e086edfSolivier moysan 4123e086edfSolivier moysan ret = clk_prepare_enable(sai->sai_ck); 4133e086edfSolivier moysan if (ret < 0) { 414602fdadcSolivier moysan dev_err(cpu_dai->dev, "Failed to enable clock: %d\n", ret); 4153e086edfSolivier moysan return ret; 4163e086edfSolivier moysan } 4173e086edfSolivier moysan 4183e086edfSolivier moysan /* Enable ITs */ 4193e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_SR_REGX, 4203e086edfSolivier moysan SAI_XSR_MASK, (unsigned int)~SAI_XSR_MASK); 4213e086edfSolivier moysan 4223e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX, 4233e086edfSolivier moysan SAI_XCLRFR_MASK, SAI_XCLRFR_MASK); 4243e086edfSolivier moysan 4253e086edfSolivier moysan imr = SAI_XIMR_OVRUDRIE; 4263e086edfSolivier moysan if (STM_SAI_IS_CAPTURE(sai)) { 4273e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_CR2_REGX, &cr2); 4283e086edfSolivier moysan if (cr2 & SAI_XCR2_MUTECNT_MASK) 4293e086edfSolivier moysan imr |= SAI_XIMR_MUTEDETIE; 4303e086edfSolivier moysan } 4313e086edfSolivier moysan 4323e086edfSolivier moysan if (sai->master) 4333e086edfSolivier moysan imr |= SAI_XIMR_WCKCFGIE; 4343e086edfSolivier moysan else 4353e086edfSolivier moysan imr |= SAI_XIMR_AFSDETIE | SAI_XIMR_LFSDETIE; 4363e086edfSolivier moysan 4373e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, 4383e086edfSolivier moysan SAI_XIMR_MASK, imr); 4393e086edfSolivier moysan 4403e086edfSolivier moysan return 0; 4413e086edfSolivier moysan } 4423e086edfSolivier moysan 4433e086edfSolivier moysan static int stm32_sai_set_config(struct snd_soc_dai *cpu_dai, 4443e086edfSolivier moysan struct snd_pcm_substream *substream, 4453e086edfSolivier moysan struct snd_pcm_hw_params *params) 4463e086edfSolivier moysan { 4473e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 4483e086edfSolivier moysan int cr1, cr1_mask, ret; 4493e086edfSolivier moysan int fth = STM_SAI_FIFO_TH_HALF; 4503e086edfSolivier moysan 4513e086edfSolivier moysan /* FIFO config */ 4523e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR2_REGX, 4533e086edfSolivier moysan SAI_XCR2_FFLUSH | SAI_XCR2_FTH_MASK, 4543e086edfSolivier moysan SAI_XCR2_FFLUSH | SAI_XCR2_FTH_SET(fth)); 4553e086edfSolivier moysan 4563e086edfSolivier moysan /* Mode, data format and channel config */ 4573e086edfSolivier moysan cr1 = SAI_XCR1_PRTCFG_SET(SAI_FREE_PROTOCOL); 4583e086edfSolivier moysan switch (params_format(params)) { 4593e086edfSolivier moysan case SNDRV_PCM_FORMAT_S8: 4603e086edfSolivier moysan cr1 |= SAI_XCR1_DS_SET(SAI_DATASIZE_8); 4613e086edfSolivier moysan break; 4623e086edfSolivier moysan case SNDRV_PCM_FORMAT_S16_LE: 4633e086edfSolivier moysan cr1 |= SAI_XCR1_DS_SET(SAI_DATASIZE_16); 4643e086edfSolivier moysan break; 4653e086edfSolivier moysan case SNDRV_PCM_FORMAT_S32_LE: 4663e086edfSolivier moysan cr1 |= SAI_XCR1_DS_SET(SAI_DATASIZE_32); 4673e086edfSolivier moysan break; 4683e086edfSolivier moysan default: 4693e086edfSolivier moysan dev_err(cpu_dai->dev, "Data format not supported"); 4703e086edfSolivier moysan return -EINVAL; 4713e086edfSolivier moysan } 4723e086edfSolivier moysan cr1_mask = SAI_XCR1_DS_MASK | SAI_XCR1_PRTCFG_MASK; 4733e086edfSolivier moysan 4743e086edfSolivier moysan cr1_mask |= SAI_XCR1_RX_TX; 4753e086edfSolivier moysan if (STM_SAI_IS_CAPTURE(sai)) 4763e086edfSolivier moysan cr1 |= SAI_XCR1_RX_TX; 4773e086edfSolivier moysan 4783e086edfSolivier moysan cr1_mask |= SAI_XCR1_MONO; 4793e086edfSolivier moysan if ((sai->slots == 2) && (params_channels(params) == 1)) 4803e086edfSolivier moysan cr1 |= SAI_XCR1_MONO; 4813e086edfSolivier moysan 4823e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1); 4833e086edfSolivier moysan if (ret < 0) { 4843e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 4853e086edfSolivier moysan return ret; 4863e086edfSolivier moysan } 4873e086edfSolivier moysan 4883e086edfSolivier moysan /* DMA config */ 4893e086edfSolivier moysan sai->dma_params.maxburst = STM_SAI_FIFO_SIZE * fth / sizeof(u32); 4903e086edfSolivier moysan snd_soc_dai_set_dma_data(cpu_dai, substream, (void *)&sai->dma_params); 4913e086edfSolivier moysan 4923e086edfSolivier moysan return 0; 4933e086edfSolivier moysan } 4943e086edfSolivier moysan 4953e086edfSolivier moysan static int stm32_sai_set_slots(struct snd_soc_dai *cpu_dai) 4963e086edfSolivier moysan { 4973e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 4983e086edfSolivier moysan int slotr, slot_sz; 4993e086edfSolivier moysan 5003e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_SLOTR_REGX, &slotr); 5013e086edfSolivier moysan 5023e086edfSolivier moysan /* 5033e086edfSolivier moysan * If SLOTSZ is set to auto in SLOTR, align slot width on data size 5043e086edfSolivier moysan * By default slot width = data size, if not forced from DT 5053e086edfSolivier moysan */ 5063e086edfSolivier moysan slot_sz = slotr & SAI_XSLOTR_SLOTSZ_MASK; 5073e086edfSolivier moysan if (slot_sz == SAI_XSLOTR_SLOTSZ_SET(SAI_SLOT_SIZE_AUTO)) 5083e086edfSolivier moysan sai->slot_width = sai->data_size; 5093e086edfSolivier moysan 5103e086edfSolivier moysan if (sai->slot_width < sai->data_size) { 5113e086edfSolivier moysan dev_err(cpu_dai->dev, 5123e086edfSolivier moysan "Data size %d larger than slot width\n", 5133e086edfSolivier moysan sai->data_size); 5143e086edfSolivier moysan return -EINVAL; 5153e086edfSolivier moysan } 5163e086edfSolivier moysan 5173e086edfSolivier moysan /* Slot number is set to 2, if not specified in DT */ 5183e086edfSolivier moysan if (!sai->slots) 5193e086edfSolivier moysan sai->slots = 2; 5203e086edfSolivier moysan 5213e086edfSolivier moysan /* The number of slots in the audio frame is equal to NBSLOT[3:0] + 1*/ 5223e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, 5233e086edfSolivier moysan SAI_XSLOTR_NBSLOT_MASK, 5243e086edfSolivier moysan SAI_XSLOTR_NBSLOT_SET((sai->slots - 1))); 5253e086edfSolivier moysan 5263e086edfSolivier moysan /* Set default slots mask if not already set from DT */ 5273e086edfSolivier moysan if (!(slotr & SAI_XSLOTR_SLOTEN_MASK)) { 5283e086edfSolivier moysan sai->slot_mask = (1 << sai->slots) - 1; 5293e086edfSolivier moysan regmap_update_bits(sai->regmap, 5303e086edfSolivier moysan STM_SAI_SLOTR_REGX, SAI_XSLOTR_SLOTEN_MASK, 5313e086edfSolivier moysan SAI_XSLOTR_SLOTEN_SET(sai->slot_mask)); 5323e086edfSolivier moysan } 5333e086edfSolivier moysan 534602fdadcSolivier moysan dev_dbg(cpu_dai->dev, "Slots %d, slot width %d\n", 5353e086edfSolivier moysan sai->slots, sai->slot_width); 5363e086edfSolivier moysan 5373e086edfSolivier moysan return 0; 5383e086edfSolivier moysan } 5393e086edfSolivier moysan 5403e086edfSolivier moysan static void stm32_sai_set_frame(struct snd_soc_dai *cpu_dai) 5413e086edfSolivier moysan { 5423e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 5433e086edfSolivier moysan int fs_active, offset, format; 5443e086edfSolivier moysan int frcr, frcr_mask; 5453e086edfSolivier moysan 5463e086edfSolivier moysan format = sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK; 5473e086edfSolivier moysan sai->fs_length = sai->slot_width * sai->slots; 5483e086edfSolivier moysan 5493e086edfSolivier moysan fs_active = sai->fs_length / 2; 5503e086edfSolivier moysan if ((format == SND_SOC_DAIFMT_DSP_A) || 5513e086edfSolivier moysan (format == SND_SOC_DAIFMT_DSP_B)) 5523e086edfSolivier moysan fs_active = 1; 5533e086edfSolivier moysan 5543e086edfSolivier moysan frcr = SAI_XFRCR_FRL_SET((sai->fs_length - 1)); 5553e086edfSolivier moysan frcr |= SAI_XFRCR_FSALL_SET((fs_active - 1)); 5563e086edfSolivier moysan frcr_mask = SAI_XFRCR_FRL_MASK | SAI_XFRCR_FSALL_MASK; 5573e086edfSolivier moysan 558602fdadcSolivier moysan dev_dbg(cpu_dai->dev, "Frame length %d, frame active %d\n", 5593e086edfSolivier moysan sai->fs_length, fs_active); 5603e086edfSolivier moysan 5613e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr); 5623e086edfSolivier moysan 5633e086edfSolivier moysan if ((sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_LSB) { 5643e086edfSolivier moysan offset = sai->slot_width - sai->data_size; 5653e086edfSolivier moysan 5663e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, 5673e086edfSolivier moysan SAI_XSLOTR_FBOFF_MASK, 5683e086edfSolivier moysan SAI_XSLOTR_FBOFF_SET(offset)); 5693e086edfSolivier moysan } 5703e086edfSolivier moysan } 5713e086edfSolivier moysan 5723e086edfSolivier moysan static int stm32_sai_configure_clock(struct snd_soc_dai *cpu_dai, 5733e086edfSolivier moysan struct snd_pcm_hw_params *params) 5743e086edfSolivier moysan { 5753e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 5763e086edfSolivier moysan int cr1, mask, div = 0; 57703e78a24Solivier moysan int sai_clk_rate, mclk_ratio, den, ret; 57803e78a24Solivier moysan int version = sai->pdata->conf->version; 5793e086edfSolivier moysan 5803e086edfSolivier moysan if (!sai->mclk_rate) { 5813e086edfSolivier moysan dev_err(cpu_dai->dev, "Mclk rate is null\n"); 5823e086edfSolivier moysan return -EINVAL; 5833e086edfSolivier moysan } 5843e086edfSolivier moysan 5853e086edfSolivier moysan if (!(params_rate(params) % 11025)) 5863e086edfSolivier moysan clk_set_parent(sai->sai_ck, sai->pdata->clk_x11k); 5873e086edfSolivier moysan else 5883e086edfSolivier moysan clk_set_parent(sai->sai_ck, sai->pdata->clk_x8k); 5893e086edfSolivier moysan sai_clk_rate = clk_get_rate(sai->sai_ck); 5903e086edfSolivier moysan 59103e78a24Solivier moysan if (STM_SAI_IS_F4(sai->pdata)) { 5923e086edfSolivier moysan /* 5933e086edfSolivier moysan * mclk_rate = 256 * fs 5943e086edfSolivier moysan * MCKDIV = 0 if sai_ck < 3/2 * mclk_rate 5953e086edfSolivier moysan * MCKDIV = sai_ck / (2 * mclk_rate) otherwise 5963e086edfSolivier moysan */ 5973e086edfSolivier moysan if (2 * sai_clk_rate >= 3 * sai->mclk_rate) 59803e78a24Solivier moysan div = DIV_ROUND_CLOSEST(sai_clk_rate, 59903e78a24Solivier moysan 2 * sai->mclk_rate); 60003e78a24Solivier moysan } else { 60103e78a24Solivier moysan /* 60203e78a24Solivier moysan * TDM mode : 60303e78a24Solivier moysan * mclk on 60403e78a24Solivier moysan * MCKDIV = sai_ck / (ws x 256) (NOMCK=0. OSR=0) 60503e78a24Solivier moysan * MCKDIV = sai_ck / (ws x 512) (NOMCK=0. OSR=1) 60603e78a24Solivier moysan * mclk off 60703e78a24Solivier moysan * MCKDIV = sai_ck / (frl x ws) (NOMCK=1) 60803e78a24Solivier moysan * Note: NOMCK/NODIV correspond to same bit. 60903e78a24Solivier moysan */ 61003e78a24Solivier moysan if (sai->mclk_rate) { 61103e78a24Solivier moysan mclk_ratio = sai->mclk_rate / params_rate(params); 61203e78a24Solivier moysan if (mclk_ratio != 256) { 61303e78a24Solivier moysan if (mclk_ratio == 512) { 61403e78a24Solivier moysan mask = SAI_XCR1_OSR; 61503e78a24Solivier moysan cr1 = SAI_XCR1_OSR; 61603e78a24Solivier moysan } else { 61703e78a24Solivier moysan dev_err(cpu_dai->dev, 61803e78a24Solivier moysan "Wrong mclk ratio %d\n", 61903e78a24Solivier moysan mclk_ratio); 62003e78a24Solivier moysan return -EINVAL; 62103e78a24Solivier moysan } 62203e78a24Solivier moysan } 62303e78a24Solivier moysan div = DIV_ROUND_CLOSEST(sai_clk_rate, sai->mclk_rate); 62403e78a24Solivier moysan } else { 62503e78a24Solivier moysan /* mclk-fs not set, master clock not active. NOMCK=1 */ 62603e78a24Solivier moysan den = sai->fs_length * params_rate(params); 62703e78a24Solivier moysan div = DIV_ROUND_CLOSEST(sai_clk_rate, den); 62803e78a24Solivier moysan } 62903e78a24Solivier moysan } 6303e086edfSolivier moysan 63103e78a24Solivier moysan if (div > SAI_XCR1_MCKDIV_MAX(version)) { 6323e086edfSolivier moysan dev_err(cpu_dai->dev, "Divider %d out of range\n", div); 6333e086edfSolivier moysan return -EINVAL; 6343e086edfSolivier moysan } 6353e086edfSolivier moysan dev_dbg(cpu_dai->dev, "SAI clock %d, divider %d\n", sai_clk_rate, div); 6363e086edfSolivier moysan 63703e78a24Solivier moysan mask = SAI_XCR1_MCKDIV_MASK(SAI_XCR1_MCKDIV_WIDTH(version)); 6383e086edfSolivier moysan cr1 = SAI_XCR1_MCKDIV_SET(div); 6393e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, mask, cr1); 6403e086edfSolivier moysan if (ret < 0) { 6413e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 6423e086edfSolivier moysan return ret; 6433e086edfSolivier moysan } 6443e086edfSolivier moysan 6453e086edfSolivier moysan return 0; 6463e086edfSolivier moysan } 6473e086edfSolivier moysan 6483e086edfSolivier moysan static int stm32_sai_hw_params(struct snd_pcm_substream *substream, 6493e086edfSolivier moysan struct snd_pcm_hw_params *params, 6503e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 6513e086edfSolivier moysan { 6523e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 6533e086edfSolivier moysan int ret; 6543e086edfSolivier moysan 6553e086edfSolivier moysan sai->data_size = params_width(params); 6563e086edfSolivier moysan 6573e086edfSolivier moysan ret = stm32_sai_set_slots(cpu_dai); 6583e086edfSolivier moysan if (ret < 0) 6593e086edfSolivier moysan return ret; 6603e086edfSolivier moysan stm32_sai_set_frame(cpu_dai); 6613e086edfSolivier moysan 6623e086edfSolivier moysan ret = stm32_sai_set_config(cpu_dai, substream, params); 6633e086edfSolivier moysan if (ret) 6643e086edfSolivier moysan return ret; 6653e086edfSolivier moysan 6663e086edfSolivier moysan if (sai->master) 6673e086edfSolivier moysan ret = stm32_sai_configure_clock(cpu_dai, params); 6683e086edfSolivier moysan 6693e086edfSolivier moysan return ret; 6703e086edfSolivier moysan } 6713e086edfSolivier moysan 6723e086edfSolivier moysan static int stm32_sai_trigger(struct snd_pcm_substream *substream, int cmd, 6733e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 6743e086edfSolivier moysan { 6753e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 6763e086edfSolivier moysan int ret; 6773e086edfSolivier moysan 6783e086edfSolivier moysan switch (cmd) { 6793e086edfSolivier moysan case SNDRV_PCM_TRIGGER_START: 6803e086edfSolivier moysan case SNDRV_PCM_TRIGGER_RESUME: 6813e086edfSolivier moysan case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 6823e086edfSolivier moysan dev_dbg(cpu_dai->dev, "Enable DMA and SAI\n"); 6833e086edfSolivier moysan 6843e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 6853e086edfSolivier moysan SAI_XCR1_DMAEN, SAI_XCR1_DMAEN); 6863e086edfSolivier moysan 6873e086edfSolivier moysan /* Enable SAI */ 6883e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 6893e086edfSolivier moysan SAI_XCR1_SAIEN, SAI_XCR1_SAIEN); 6903e086edfSolivier moysan if (ret < 0) 6913e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 6923e086edfSolivier moysan break; 6933e086edfSolivier moysan case SNDRV_PCM_TRIGGER_SUSPEND: 6943e086edfSolivier moysan case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 6953e086edfSolivier moysan case SNDRV_PCM_TRIGGER_STOP: 6963e086edfSolivier moysan dev_dbg(cpu_dai->dev, "Disable DMA and SAI\n"); 6973e086edfSolivier moysan 6983e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 6993e086edfSolivier moysan SAI_XCR1_SAIEN, 7003e086edfSolivier moysan (unsigned int)~SAI_XCR1_SAIEN); 7014fa17938Solivier moysan 7024fa17938Solivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 7034fa17938Solivier moysan SAI_XCR1_DMAEN, 7044fa17938Solivier moysan (unsigned int)~SAI_XCR1_DMAEN); 7053e086edfSolivier moysan if (ret < 0) 7063e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 7073e086edfSolivier moysan break; 7083e086edfSolivier moysan default: 7093e086edfSolivier moysan return -EINVAL; 7103e086edfSolivier moysan } 7113e086edfSolivier moysan 7123e086edfSolivier moysan return ret; 7133e086edfSolivier moysan } 7143e086edfSolivier moysan 7153e086edfSolivier moysan static void stm32_sai_shutdown(struct snd_pcm_substream *substream, 7163e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 7173e086edfSolivier moysan { 7183e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 7193e086edfSolivier moysan 7203e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, SAI_XIMR_MASK, 0); 7213e086edfSolivier moysan 722701a6ec3Solivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, SAI_XCR1_NODIV, 723701a6ec3Solivier moysan SAI_XCR1_NODIV); 724701a6ec3Solivier moysan 7253e086edfSolivier moysan clk_disable_unprepare(sai->sai_ck); 7263e086edfSolivier moysan sai->substream = NULL; 7273e086edfSolivier moysan } 7283e086edfSolivier moysan 7293e086edfSolivier moysan static int stm32_sai_dai_probe(struct snd_soc_dai *cpu_dai) 7303e086edfSolivier moysan { 7313e086edfSolivier moysan struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev); 7323e086edfSolivier moysan 7333e086edfSolivier moysan sai->dma_params.addr = (dma_addr_t)(sai->phys_addr + STM_SAI_DR_REGX); 7343e086edfSolivier moysan sai->dma_params.maxburst = 1; 7353e086edfSolivier moysan /* Buswidth will be set by framework at runtime */ 7363e086edfSolivier moysan sai->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED; 7373e086edfSolivier moysan 7383e086edfSolivier moysan if (STM_SAI_IS_PLAYBACK(sai)) 7393e086edfSolivier moysan snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params, NULL); 7403e086edfSolivier moysan else 7413e086edfSolivier moysan snd_soc_dai_init_dma_data(cpu_dai, NULL, &sai->dma_params); 7423e086edfSolivier moysan 7433e086edfSolivier moysan return 0; 7443e086edfSolivier moysan } 7453e086edfSolivier moysan 7463e086edfSolivier moysan static const struct snd_soc_dai_ops stm32_sai_pcm_dai_ops = { 7473e086edfSolivier moysan .set_sysclk = stm32_sai_set_sysclk, 7483e086edfSolivier moysan .set_fmt = stm32_sai_set_dai_fmt, 7493e086edfSolivier moysan .set_tdm_slot = stm32_sai_set_dai_tdm_slot, 7503e086edfSolivier moysan .startup = stm32_sai_startup, 7513e086edfSolivier moysan .hw_params = stm32_sai_hw_params, 7523e086edfSolivier moysan .trigger = stm32_sai_trigger, 7533e086edfSolivier moysan .shutdown = stm32_sai_shutdown, 7543e086edfSolivier moysan }; 7553e086edfSolivier moysan 7563e086edfSolivier moysan static const struct snd_pcm_hardware stm32_sai_pcm_hw = { 7573e086edfSolivier moysan .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP, 7583e086edfSolivier moysan .buffer_bytes_max = 8 * PAGE_SIZE, 7593e086edfSolivier moysan .period_bytes_min = 1024, /* 5ms at 48kHz */ 7603e086edfSolivier moysan .period_bytes_max = PAGE_SIZE, 7613e086edfSolivier moysan .periods_min = 2, 7623e086edfSolivier moysan .periods_max = 8, 7633e086edfSolivier moysan }; 7643e086edfSolivier moysan 7653e086edfSolivier moysan static struct snd_soc_dai_driver stm32_sai_playback_dai[] = { 7663e086edfSolivier moysan { 7673e086edfSolivier moysan .probe = stm32_sai_dai_probe, 7683e086edfSolivier moysan .id = 1, /* avoid call to fmt_single_name() */ 7693e086edfSolivier moysan .playback = { 7703e086edfSolivier moysan .channels_min = 1, 7713e086edfSolivier moysan .channels_max = 2, 7723e086edfSolivier moysan .rate_min = 8000, 7733e086edfSolivier moysan .rate_max = 192000, 7743e086edfSolivier moysan .rates = SNDRV_PCM_RATE_CONTINUOUS, 7753e086edfSolivier moysan /* DMA does not support 24 bits transfers */ 7763e086edfSolivier moysan .formats = 7773e086edfSolivier moysan SNDRV_PCM_FMTBIT_S8 | 7783e086edfSolivier moysan SNDRV_PCM_FMTBIT_S16_LE | 7793e086edfSolivier moysan SNDRV_PCM_FMTBIT_S32_LE, 7803e086edfSolivier moysan }, 7813e086edfSolivier moysan .ops = &stm32_sai_pcm_dai_ops, 7823e086edfSolivier moysan } 7833e086edfSolivier moysan }; 7843e086edfSolivier moysan 7853e086edfSolivier moysan static struct snd_soc_dai_driver stm32_sai_capture_dai[] = { 7863e086edfSolivier moysan { 7873e086edfSolivier moysan .probe = stm32_sai_dai_probe, 7883e086edfSolivier moysan .id = 1, /* avoid call to fmt_single_name() */ 7893e086edfSolivier moysan .capture = { 7903e086edfSolivier moysan .channels_min = 1, 7913e086edfSolivier moysan .channels_max = 2, 7923e086edfSolivier moysan .rate_min = 8000, 7933e086edfSolivier moysan .rate_max = 192000, 7943e086edfSolivier moysan .rates = SNDRV_PCM_RATE_CONTINUOUS, 7953e086edfSolivier moysan /* DMA does not support 24 bits transfers */ 7963e086edfSolivier moysan .formats = 7973e086edfSolivier moysan SNDRV_PCM_FMTBIT_S8 | 7983e086edfSolivier moysan SNDRV_PCM_FMTBIT_S16_LE | 7993e086edfSolivier moysan SNDRV_PCM_FMTBIT_S32_LE, 8003e086edfSolivier moysan }, 8013e086edfSolivier moysan .ops = &stm32_sai_pcm_dai_ops, 8023e086edfSolivier moysan } 8033e086edfSolivier moysan }; 8043e086edfSolivier moysan 8053e086edfSolivier moysan static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config = { 8063e086edfSolivier moysan .pcm_hardware = &stm32_sai_pcm_hw, 8073e086edfSolivier moysan .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config, 8083e086edfSolivier moysan }; 8093e086edfSolivier moysan 8103e086edfSolivier moysan static const struct snd_soc_component_driver stm32_component = { 8113e086edfSolivier moysan .name = "stm32-sai", 8123e086edfSolivier moysan }; 8133e086edfSolivier moysan 8143e086edfSolivier moysan static const struct of_device_id stm32_sai_sub_ids[] = { 8153e086edfSolivier moysan { .compatible = "st,stm32-sai-sub-a", 8163e086edfSolivier moysan .data = (void *)STM_SAI_A_ID}, 8173e086edfSolivier moysan { .compatible = "st,stm32-sai-sub-b", 8183e086edfSolivier moysan .data = (void *)STM_SAI_B_ID}, 8193e086edfSolivier moysan {} 8203e086edfSolivier moysan }; 8213e086edfSolivier moysan MODULE_DEVICE_TABLE(of, stm32_sai_sub_ids); 8223e086edfSolivier moysan 8233e086edfSolivier moysan static int stm32_sai_sub_parse_of(struct platform_device *pdev, 8243e086edfSolivier moysan struct stm32_sai_sub_data *sai) 8253e086edfSolivier moysan { 8263e086edfSolivier moysan struct device_node *np = pdev->dev.of_node; 8273e086edfSolivier moysan struct resource *res; 8283e086edfSolivier moysan void __iomem *base; 8293e086edfSolivier moysan 8303e086edfSolivier moysan if (!np) 8313e086edfSolivier moysan return -ENODEV; 8323e086edfSolivier moysan 8333e086edfSolivier moysan res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 8343e086edfSolivier moysan base = devm_ioremap_resource(&pdev->dev, res); 8353e086edfSolivier moysan if (IS_ERR(base)) 8363e086edfSolivier moysan return PTR_ERR(base); 8373e086edfSolivier moysan 8383e086edfSolivier moysan sai->phys_addr = res->start; 83903e78a24Solivier moysan 84003e78a24Solivier moysan sai->regmap_config = &stm32_sai_sub_regmap_config_f4; 84103e78a24Solivier moysan /* Note: PDM registers not available for H7 sub-block B */ 84203e78a24Solivier moysan if (STM_SAI_IS_H7(sai->pdata) && STM_SAI_IS_SUB_A(sai)) 84303e78a24Solivier moysan sai->regmap_config = &stm32_sai_sub_regmap_config_h7; 84403e78a24Solivier moysan 84503e78a24Solivier moysan sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "sai_ck", 84603e78a24Solivier moysan base, sai->regmap_config); 84703e78a24Solivier moysan if (IS_ERR(sai->regmap)) { 84803e78a24Solivier moysan dev_err(&pdev->dev, "Failed to initialize MMIO\n"); 84903e78a24Solivier moysan return PTR_ERR(sai->regmap); 85003e78a24Solivier moysan } 8513e086edfSolivier moysan 8523e086edfSolivier moysan /* Get direction property */ 8533e086edfSolivier moysan if (of_property_match_string(np, "dma-names", "tx") >= 0) { 8543e086edfSolivier moysan sai->dir = SNDRV_PCM_STREAM_PLAYBACK; 8553e086edfSolivier moysan } else if (of_property_match_string(np, "dma-names", "rx") >= 0) { 8563e086edfSolivier moysan sai->dir = SNDRV_PCM_STREAM_CAPTURE; 8573e086edfSolivier moysan } else { 8583e086edfSolivier moysan dev_err(&pdev->dev, "Unsupported direction\n"); 8593e086edfSolivier moysan return -EINVAL; 8603e086edfSolivier moysan } 8613e086edfSolivier moysan 8623e086edfSolivier moysan sai->sai_ck = devm_clk_get(&pdev->dev, "sai_ck"); 8633e086edfSolivier moysan if (IS_ERR(sai->sai_ck)) { 864602fdadcSolivier moysan dev_err(&pdev->dev, "Missing kernel clock sai_ck\n"); 8653e086edfSolivier moysan return PTR_ERR(sai->sai_ck); 8663e086edfSolivier moysan } 8673e086edfSolivier moysan 8683e086edfSolivier moysan return 0; 8693e086edfSolivier moysan } 8703e086edfSolivier moysan 8713e086edfSolivier moysan static int stm32_sai_sub_dais_init(struct platform_device *pdev, 8723e086edfSolivier moysan struct stm32_sai_sub_data *sai) 8733e086edfSolivier moysan { 8743e086edfSolivier moysan sai->cpu_dai_drv = devm_kzalloc(&pdev->dev, 8753e086edfSolivier moysan sizeof(struct snd_soc_dai_driver), 8763e086edfSolivier moysan GFP_KERNEL); 8773e086edfSolivier moysan if (!sai->cpu_dai_drv) 8783e086edfSolivier moysan return -ENOMEM; 8793e086edfSolivier moysan 8803e086edfSolivier moysan sai->cpu_dai_drv->name = dev_name(&pdev->dev); 8813e086edfSolivier moysan if (STM_SAI_IS_PLAYBACK(sai)) { 8823e086edfSolivier moysan memcpy(sai->cpu_dai_drv, &stm32_sai_playback_dai, 8833e086edfSolivier moysan sizeof(stm32_sai_playback_dai)); 8843e086edfSolivier moysan sai->cpu_dai_drv->playback.stream_name = sai->cpu_dai_drv->name; 8853e086edfSolivier moysan } else { 8863e086edfSolivier moysan memcpy(sai->cpu_dai_drv, &stm32_sai_capture_dai, 8873e086edfSolivier moysan sizeof(stm32_sai_capture_dai)); 8883e086edfSolivier moysan sai->cpu_dai_drv->capture.stream_name = sai->cpu_dai_drv->name; 8893e086edfSolivier moysan } 8903e086edfSolivier moysan 8913e086edfSolivier moysan return 0; 8923e086edfSolivier moysan } 8933e086edfSolivier moysan 8943e086edfSolivier moysan static int stm32_sai_sub_probe(struct platform_device *pdev) 8953e086edfSolivier moysan { 8963e086edfSolivier moysan struct stm32_sai_sub_data *sai; 8973e086edfSolivier moysan const struct of_device_id *of_id; 8983e086edfSolivier moysan int ret; 8993e086edfSolivier moysan 9003e086edfSolivier moysan sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL); 9013e086edfSolivier moysan if (!sai) 9023e086edfSolivier moysan return -ENOMEM; 9033e086edfSolivier moysan 9043e086edfSolivier moysan of_id = of_match_device(stm32_sai_sub_ids, &pdev->dev); 9053e086edfSolivier moysan if (!of_id) 9063e086edfSolivier moysan return -EINVAL; 9073e086edfSolivier moysan sai->id = (uintptr_t)of_id->data; 9083e086edfSolivier moysan 9093e086edfSolivier moysan sai->pdev = pdev; 9103e086edfSolivier moysan platform_set_drvdata(pdev, sai); 9113e086edfSolivier moysan 9123e086edfSolivier moysan sai->pdata = dev_get_drvdata(pdev->dev.parent); 9133e086edfSolivier moysan if (!sai->pdata) { 9143e086edfSolivier moysan dev_err(&pdev->dev, "Parent device data not available\n"); 9153e086edfSolivier moysan return -EINVAL; 9163e086edfSolivier moysan } 9173e086edfSolivier moysan 9183e086edfSolivier moysan ret = stm32_sai_sub_parse_of(pdev, sai); 9193e086edfSolivier moysan if (ret) 9203e086edfSolivier moysan return ret; 9213e086edfSolivier moysan 9223e086edfSolivier moysan ret = stm32_sai_sub_dais_init(pdev, sai); 9233e086edfSolivier moysan if (ret) 9243e086edfSolivier moysan return ret; 9253e086edfSolivier moysan 9263e086edfSolivier moysan ret = devm_request_irq(&pdev->dev, sai->pdata->irq, stm32_sai_isr, 9273e086edfSolivier moysan IRQF_SHARED, dev_name(&pdev->dev), sai); 9283e086edfSolivier moysan if (ret) { 929602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ request returned %d\n", ret); 9303e086edfSolivier moysan return ret; 9313e086edfSolivier moysan } 9323e086edfSolivier moysan 9333e086edfSolivier moysan ret = devm_snd_soc_register_component(&pdev->dev, &stm32_component, 9343e086edfSolivier moysan sai->cpu_dai_drv, 1); 9353e086edfSolivier moysan if (ret) 9363e086edfSolivier moysan return ret; 9373e086edfSolivier moysan 9383e086edfSolivier moysan ret = devm_snd_dmaengine_pcm_register(&pdev->dev, 9393e086edfSolivier moysan &stm32_sai_pcm_config, 0); 9403e086edfSolivier moysan if (ret) { 941602fdadcSolivier moysan dev_err(&pdev->dev, "Could not register pcm dma\n"); 9423e086edfSolivier moysan return ret; 9433e086edfSolivier moysan } 9443e086edfSolivier moysan 9453e086edfSolivier moysan return 0; 9463e086edfSolivier moysan } 9473e086edfSolivier moysan 9483e086edfSolivier moysan static struct platform_driver stm32_sai_sub_driver = { 9493e086edfSolivier moysan .driver = { 9503e086edfSolivier moysan .name = "st,stm32-sai-sub", 9513e086edfSolivier moysan .of_match_table = stm32_sai_sub_ids, 9523e086edfSolivier moysan }, 9533e086edfSolivier moysan .probe = stm32_sai_sub_probe, 9543e086edfSolivier moysan }; 9553e086edfSolivier moysan 9563e086edfSolivier moysan module_platform_driver(stm32_sai_sub_driver); 9573e086edfSolivier moysan 9583e086edfSolivier moysan MODULE_DESCRIPTION("STM32 Soc SAI sub-block Interface"); 959602fdadcSolivier moysan MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>"); 9603e086edfSolivier moysan MODULE_ALIAS("platform:st,stm32-sai-sub"); 9613e086edfSolivier moysan MODULE_LICENSE("GPL v2"); 962