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 543e086edfSolivier moysan #define STM_SAI_BLOCK_NAME(x) (((x)->id == STM_SAI_A_ID) ? "A" : "B") 553e086edfSolivier moysan 563e086edfSolivier moysan /** 573e086edfSolivier moysan * struct stm32_sai_sub_data - private data of SAI sub block (block A or B) 583e086edfSolivier moysan * @pdev: device data pointer 593e086edfSolivier moysan * @regmap: SAI register map pointer 603e086edfSolivier moysan * @dma_params: dma configuration data for rx or tx channel 613e086edfSolivier moysan * @cpu_dai_drv: DAI driver data pointer 623e086edfSolivier moysan * @cpu_dai: DAI runtime data pointer 633e086edfSolivier moysan * @substream: PCM substream data pointer 643e086edfSolivier moysan * @pdata: SAI block parent data pointer 653e086edfSolivier moysan * @sai_ck: kernel clock feeding the SAI clock generator 663e086edfSolivier moysan * @phys_addr: SAI registers physical base address 673e086edfSolivier moysan * @mclk_rate: SAI block master clock frequency (Hz). set at init 683e086edfSolivier moysan * @id: SAI sub block id corresponding to sub-block A or B 693e086edfSolivier moysan * @dir: SAI block direction (playback or capture). set at init 703e086edfSolivier moysan * @master: SAI block mode flag. (true=master, false=slave) set at init 713e086edfSolivier moysan * @fmt: SAI block format. relevant only for custom protocols. set at init 723e086edfSolivier moysan * @sync: SAI block synchronization mode. (none, internal or external) 733e086edfSolivier moysan * @fs_length: frame synchronization length. depends on protocol settings 743e086edfSolivier moysan * @slots: rx or tx slot number 753e086edfSolivier moysan * @slot_width: rx or tx slot width in bits 763e086edfSolivier moysan * @slot_mask: rx or tx active slots mask. set at init or at runtime 773e086edfSolivier moysan * @data_size: PCM data width. corresponds to PCM substream width. 783e086edfSolivier moysan */ 793e086edfSolivier moysan struct stm32_sai_sub_data { 803e086edfSolivier moysan struct platform_device *pdev; 813e086edfSolivier moysan struct regmap *regmap; 823e086edfSolivier moysan struct snd_dmaengine_dai_dma_data dma_params; 833e086edfSolivier moysan struct snd_soc_dai_driver *cpu_dai_drv; 843e086edfSolivier moysan struct snd_soc_dai *cpu_dai; 853e086edfSolivier moysan struct snd_pcm_substream *substream; 863e086edfSolivier moysan struct stm32_sai_data *pdata; 873e086edfSolivier moysan struct clk *sai_ck; 883e086edfSolivier moysan dma_addr_t phys_addr; 893e086edfSolivier moysan unsigned int mclk_rate; 903e086edfSolivier moysan unsigned int id; 913e086edfSolivier moysan int dir; 923e086edfSolivier moysan bool master; 933e086edfSolivier moysan int fmt; 943e086edfSolivier moysan int sync; 953e086edfSolivier moysan int fs_length; 963e086edfSolivier moysan int slots; 973e086edfSolivier moysan int slot_width; 983e086edfSolivier moysan int slot_mask; 993e086edfSolivier moysan int data_size; 1003e086edfSolivier moysan }; 1013e086edfSolivier moysan 1023e086edfSolivier moysan enum stm32_sai_fifo_th { 1033e086edfSolivier moysan STM_SAI_FIFO_TH_EMPTY, 1043e086edfSolivier moysan STM_SAI_FIFO_TH_QUARTER, 1053e086edfSolivier moysan STM_SAI_FIFO_TH_HALF, 1063e086edfSolivier moysan STM_SAI_FIFO_TH_3_QUARTER, 1073e086edfSolivier moysan STM_SAI_FIFO_TH_FULL, 1083e086edfSolivier moysan }; 1093e086edfSolivier moysan 1103e086edfSolivier moysan static bool stm32_sai_sub_readable_reg(struct device *dev, unsigned int reg) 1113e086edfSolivier moysan { 1123e086edfSolivier moysan switch (reg) { 1133e086edfSolivier moysan case STM_SAI_CR1_REGX: 1143e086edfSolivier moysan case STM_SAI_CR2_REGX: 1153e086edfSolivier moysan case STM_SAI_FRCR_REGX: 1163e086edfSolivier moysan case STM_SAI_SLOTR_REGX: 1173e086edfSolivier moysan case STM_SAI_IMR_REGX: 1183e086edfSolivier moysan case STM_SAI_SR_REGX: 1193e086edfSolivier moysan case STM_SAI_CLRFR_REGX: 1203e086edfSolivier moysan case STM_SAI_DR_REGX: 1213e086edfSolivier moysan return true; 1223e086edfSolivier moysan default: 1233e086edfSolivier moysan return false; 1243e086edfSolivier moysan } 1253e086edfSolivier moysan } 1263e086edfSolivier moysan 1273e086edfSolivier moysan static bool stm32_sai_sub_volatile_reg(struct device *dev, unsigned int reg) 1283e086edfSolivier moysan { 1293e086edfSolivier moysan switch (reg) { 1303e086edfSolivier moysan case STM_SAI_DR_REGX: 1313e086edfSolivier moysan return true; 1323e086edfSolivier moysan default: 1333e086edfSolivier moysan return false; 1343e086edfSolivier moysan } 1353e086edfSolivier moysan } 1363e086edfSolivier moysan 1373e086edfSolivier moysan static bool stm32_sai_sub_writeable_reg(struct device *dev, unsigned int reg) 1383e086edfSolivier moysan { 1393e086edfSolivier moysan switch (reg) { 1403e086edfSolivier moysan case STM_SAI_CR1_REGX: 1413e086edfSolivier moysan case STM_SAI_CR2_REGX: 1423e086edfSolivier moysan case STM_SAI_FRCR_REGX: 1433e086edfSolivier moysan case STM_SAI_SLOTR_REGX: 1443e086edfSolivier moysan case STM_SAI_IMR_REGX: 1453e086edfSolivier moysan case STM_SAI_SR_REGX: 1463e086edfSolivier moysan case STM_SAI_CLRFR_REGX: 1473e086edfSolivier moysan case STM_SAI_DR_REGX: 1483e086edfSolivier moysan return true; 1493e086edfSolivier moysan default: 1503e086edfSolivier moysan return false; 1513e086edfSolivier moysan } 1523e086edfSolivier moysan } 1533e086edfSolivier moysan 1543e086edfSolivier moysan static const struct regmap_config stm32_sai_sub_regmap_config = { 1553e086edfSolivier moysan .reg_bits = 32, 1563e086edfSolivier moysan .reg_stride = 4, 1573e086edfSolivier moysan .val_bits = 32, 1583e086edfSolivier moysan .max_register = STM_SAI_DR_REGX, 1593e086edfSolivier moysan .readable_reg = stm32_sai_sub_readable_reg, 1603e086edfSolivier moysan .volatile_reg = stm32_sai_sub_volatile_reg, 1613e086edfSolivier moysan .writeable_reg = stm32_sai_sub_writeable_reg, 1623e086edfSolivier moysan .fast_io = true, 1633e086edfSolivier moysan }; 1643e086edfSolivier moysan 1653e086edfSolivier moysan static irqreturn_t stm32_sai_isr(int irq, void *devid) 1663e086edfSolivier moysan { 1673e086edfSolivier moysan struct stm32_sai_sub_data *sai = (struct stm32_sai_sub_data *)devid; 1683e086edfSolivier moysan struct snd_pcm_substream *substream = sai->substream; 1693e086edfSolivier moysan struct platform_device *pdev = sai->pdev; 1703e086edfSolivier moysan unsigned int sr, imr, flags; 1713e086edfSolivier moysan snd_pcm_state_t status = SNDRV_PCM_STATE_RUNNING; 1723e086edfSolivier moysan 1733e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_IMR_REGX, &imr); 1743e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_SR_REGX, &sr); 1753e086edfSolivier moysan 1763e086edfSolivier moysan flags = sr & imr; 1773e086edfSolivier moysan if (!flags) 1783e086edfSolivier moysan return IRQ_NONE; 1793e086edfSolivier moysan 1803e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX, SAI_XCLRFR_MASK, 1813e086edfSolivier moysan SAI_XCLRFR_MASK); 1823e086edfSolivier moysan 1833e086edfSolivier moysan if (flags & SAI_XIMR_OVRUDRIE) { 184*602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ %s\n", 1853e086edfSolivier moysan STM_SAI_IS_PLAYBACK(sai) ? "underrun" : "overrun"); 1863e086edfSolivier moysan status = SNDRV_PCM_STATE_XRUN; 1873e086edfSolivier moysan } 1883e086edfSolivier moysan 1893e086edfSolivier moysan if (flags & SAI_XIMR_MUTEDETIE) 190*602fdadcSolivier moysan dev_dbg(&pdev->dev, "IRQ mute detected\n"); 1913e086edfSolivier moysan 1923e086edfSolivier moysan if (flags & SAI_XIMR_WCKCFGIE) { 193*602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ wrong clock configuration\n"); 1943e086edfSolivier moysan status = SNDRV_PCM_STATE_DISCONNECTED; 1953e086edfSolivier moysan } 1963e086edfSolivier moysan 1973e086edfSolivier moysan if (flags & SAI_XIMR_CNRDYIE) 198*602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ Codec not ready\n"); 1993e086edfSolivier moysan 2003e086edfSolivier moysan if (flags & SAI_XIMR_AFSDETIE) { 201*602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ Anticipated frame synchro\n"); 2023e086edfSolivier moysan status = SNDRV_PCM_STATE_XRUN; 2033e086edfSolivier moysan } 2043e086edfSolivier moysan 2053e086edfSolivier moysan if (flags & SAI_XIMR_LFSDETIE) { 206*602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ Late frame synchro\n"); 2073e086edfSolivier moysan status = SNDRV_PCM_STATE_XRUN; 2083e086edfSolivier moysan } 2093e086edfSolivier moysan 2103e086edfSolivier moysan if (status != SNDRV_PCM_STATE_RUNNING) { 2113e086edfSolivier moysan snd_pcm_stream_lock(substream); 2123e086edfSolivier moysan snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); 2133e086edfSolivier moysan snd_pcm_stream_unlock(substream); 2143e086edfSolivier moysan } 2153e086edfSolivier moysan 2163e086edfSolivier moysan return IRQ_HANDLED; 2173e086edfSolivier moysan } 2183e086edfSolivier moysan 2193e086edfSolivier moysan static int stm32_sai_set_sysclk(struct snd_soc_dai *cpu_dai, 2203e086edfSolivier moysan int clk_id, unsigned int freq, int dir) 2213e086edfSolivier moysan { 2223e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 2233e086edfSolivier moysan 2243e086edfSolivier moysan if ((dir == SND_SOC_CLOCK_OUT) && sai->master) { 2253e086edfSolivier moysan sai->mclk_rate = freq; 2263e086edfSolivier moysan dev_dbg(cpu_dai->dev, "SAI MCLK frequency is %uHz\n", freq); 2273e086edfSolivier moysan } 2283e086edfSolivier moysan 2293e086edfSolivier moysan return 0; 2303e086edfSolivier moysan } 2313e086edfSolivier moysan 2323e086edfSolivier moysan static int stm32_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask, 2333e086edfSolivier moysan u32 rx_mask, int slots, int slot_width) 2343e086edfSolivier moysan { 2353e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 2363e086edfSolivier moysan int slotr, slotr_mask, slot_size; 2373e086edfSolivier moysan 238*602fdadcSolivier moysan dev_dbg(cpu_dai->dev, "Masks tx/rx:%#x/%#x, slots:%d, width:%d\n", 2393e086edfSolivier moysan tx_mask, rx_mask, slots, slot_width); 2403e086edfSolivier moysan 2413e086edfSolivier moysan switch (slot_width) { 2423e086edfSolivier moysan case 16: 2433e086edfSolivier moysan slot_size = SAI_SLOT_SIZE_16; 2443e086edfSolivier moysan break; 2453e086edfSolivier moysan case 32: 2463e086edfSolivier moysan slot_size = SAI_SLOT_SIZE_32; 2473e086edfSolivier moysan break; 2483e086edfSolivier moysan default: 2493e086edfSolivier moysan slot_size = SAI_SLOT_SIZE_AUTO; 2503e086edfSolivier moysan break; 2513e086edfSolivier moysan } 2523e086edfSolivier moysan 2533e086edfSolivier moysan slotr = SAI_XSLOTR_SLOTSZ_SET(slot_size) | 2543e086edfSolivier moysan SAI_XSLOTR_NBSLOT_SET(slots - 1); 2553e086edfSolivier moysan slotr_mask = SAI_XSLOTR_SLOTSZ_MASK | SAI_XSLOTR_NBSLOT_MASK; 2563e086edfSolivier moysan 2573e086edfSolivier moysan /* tx/rx mask set in machine init, if slot number defined in DT */ 2583e086edfSolivier moysan if (STM_SAI_IS_PLAYBACK(sai)) { 2593e086edfSolivier moysan sai->slot_mask = tx_mask; 2603e086edfSolivier moysan slotr |= SAI_XSLOTR_SLOTEN_SET(tx_mask); 2613e086edfSolivier moysan } 2623e086edfSolivier moysan 2633e086edfSolivier moysan if (STM_SAI_IS_CAPTURE(sai)) { 2643e086edfSolivier moysan sai->slot_mask = rx_mask; 2653e086edfSolivier moysan slotr |= SAI_XSLOTR_SLOTEN_SET(rx_mask); 2663e086edfSolivier moysan } 2673e086edfSolivier moysan 2683e086edfSolivier moysan slotr_mask |= SAI_XSLOTR_SLOTEN_MASK; 2693e086edfSolivier moysan 2703e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, slotr_mask, slotr); 2713e086edfSolivier moysan 2723e086edfSolivier moysan sai->slot_width = slot_width; 2733e086edfSolivier moysan sai->slots = slots; 2743e086edfSolivier moysan 2753e086edfSolivier moysan return 0; 2763e086edfSolivier moysan } 2773e086edfSolivier moysan 2783e086edfSolivier moysan static int stm32_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt) 2793e086edfSolivier moysan { 2803e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 2813e086edfSolivier moysan int cr1 = 0, frcr = 0; 2823e086edfSolivier moysan int cr1_mask = 0, frcr_mask = 0; 2833e086edfSolivier moysan int ret; 2843e086edfSolivier moysan 2853e086edfSolivier moysan dev_dbg(cpu_dai->dev, "fmt %x\n", fmt); 2863e086edfSolivier moysan 2873e086edfSolivier moysan switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 2883e086edfSolivier moysan /* SCK active high for all protocols */ 2893e086edfSolivier moysan case SND_SOC_DAIFMT_I2S: 2903e086edfSolivier moysan cr1 |= SAI_XCR1_CKSTR; 2913e086edfSolivier moysan frcr |= SAI_XFRCR_FSOFF | SAI_XFRCR_FSDEF; 2923e086edfSolivier moysan break; 2933e086edfSolivier moysan /* Left justified */ 2943e086edfSolivier moysan case SND_SOC_DAIFMT_MSB: 2953e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF; 2963e086edfSolivier moysan break; 2973e086edfSolivier moysan /* Right justified */ 2983e086edfSolivier moysan case SND_SOC_DAIFMT_LSB: 2993e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF; 3003e086edfSolivier moysan break; 3013e086edfSolivier moysan case SND_SOC_DAIFMT_DSP_A: 3023e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF; 3033e086edfSolivier moysan break; 3043e086edfSolivier moysan case SND_SOC_DAIFMT_DSP_B: 3053e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL; 3063e086edfSolivier moysan break; 3073e086edfSolivier moysan default: 3083e086edfSolivier moysan dev_err(cpu_dai->dev, "Unsupported protocol %#x\n", 3093e086edfSolivier moysan fmt & SND_SOC_DAIFMT_FORMAT_MASK); 3103e086edfSolivier moysan return -EINVAL; 3113e086edfSolivier moysan } 3123e086edfSolivier moysan 3133e086edfSolivier moysan cr1_mask |= SAI_XCR1_PRTCFG_MASK | SAI_XCR1_CKSTR; 3143e086edfSolivier moysan frcr_mask |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF | 3153e086edfSolivier moysan SAI_XFRCR_FSDEF; 3163e086edfSolivier moysan 3173e086edfSolivier moysan /* DAI clock strobing. Invert setting previously set */ 3183e086edfSolivier moysan switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 3193e086edfSolivier moysan case SND_SOC_DAIFMT_NB_NF: 3203e086edfSolivier moysan break; 3213e086edfSolivier moysan case SND_SOC_DAIFMT_IB_NF: 3223e086edfSolivier moysan cr1 ^= SAI_XCR1_CKSTR; 3233e086edfSolivier moysan break; 3243e086edfSolivier moysan case SND_SOC_DAIFMT_NB_IF: 3253e086edfSolivier moysan frcr ^= SAI_XFRCR_FSPOL; 3263e086edfSolivier moysan break; 3273e086edfSolivier moysan case SND_SOC_DAIFMT_IB_IF: 3283e086edfSolivier moysan /* Invert fs & sck */ 3293e086edfSolivier moysan cr1 ^= SAI_XCR1_CKSTR; 3303e086edfSolivier moysan frcr ^= SAI_XFRCR_FSPOL; 3313e086edfSolivier moysan break; 3323e086edfSolivier moysan default: 3333e086edfSolivier moysan dev_err(cpu_dai->dev, "Unsupported strobing %#x\n", 3343e086edfSolivier moysan fmt & SND_SOC_DAIFMT_INV_MASK); 3353e086edfSolivier moysan return -EINVAL; 3363e086edfSolivier moysan } 3373e086edfSolivier moysan cr1_mask |= SAI_XCR1_CKSTR; 3383e086edfSolivier moysan frcr_mask |= SAI_XFRCR_FSPOL; 3393e086edfSolivier moysan 3403e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr); 3413e086edfSolivier moysan 3423e086edfSolivier moysan /* DAI clock master masks */ 3433e086edfSolivier moysan switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 3443e086edfSolivier moysan case SND_SOC_DAIFMT_CBM_CFM: 3453e086edfSolivier moysan /* codec is master */ 3463e086edfSolivier moysan cr1 |= SAI_XCR1_SLAVE; 3473e086edfSolivier moysan sai->master = false; 3483e086edfSolivier moysan break; 3493e086edfSolivier moysan case SND_SOC_DAIFMT_CBS_CFS: 3503e086edfSolivier moysan sai->master = true; 3513e086edfSolivier moysan break; 3523e086edfSolivier moysan default: 3533e086edfSolivier moysan dev_err(cpu_dai->dev, "Unsupported mode %#x\n", 3543e086edfSolivier moysan fmt & SND_SOC_DAIFMT_MASTER_MASK); 3553e086edfSolivier moysan return -EINVAL; 3563e086edfSolivier moysan } 3573e086edfSolivier moysan cr1_mask |= SAI_XCR1_SLAVE; 3583e086edfSolivier moysan 3593e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1); 3603e086edfSolivier moysan if (ret < 0) { 3613e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 3623e086edfSolivier moysan return ret; 3633e086edfSolivier moysan } 3643e086edfSolivier moysan 3653e086edfSolivier moysan sai->fmt = fmt; 3663e086edfSolivier moysan 3673e086edfSolivier moysan return 0; 3683e086edfSolivier moysan } 3693e086edfSolivier moysan 3703e086edfSolivier moysan static int stm32_sai_startup(struct snd_pcm_substream *substream, 3713e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 3723e086edfSolivier moysan { 3733e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 3743e086edfSolivier moysan int imr, cr2, ret; 3753e086edfSolivier moysan 3763e086edfSolivier moysan sai->substream = substream; 3773e086edfSolivier moysan 3783e086edfSolivier moysan ret = clk_prepare_enable(sai->sai_ck); 3793e086edfSolivier moysan if (ret < 0) { 380*602fdadcSolivier moysan dev_err(cpu_dai->dev, "Failed to enable clock: %d\n", ret); 3813e086edfSolivier moysan return ret; 3823e086edfSolivier moysan } 3833e086edfSolivier moysan 3843e086edfSolivier moysan /* Enable ITs */ 3853e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_SR_REGX, 3863e086edfSolivier moysan SAI_XSR_MASK, (unsigned int)~SAI_XSR_MASK); 3873e086edfSolivier moysan 3883e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX, 3893e086edfSolivier moysan SAI_XCLRFR_MASK, SAI_XCLRFR_MASK); 3903e086edfSolivier moysan 3913e086edfSolivier moysan imr = SAI_XIMR_OVRUDRIE; 3923e086edfSolivier moysan if (STM_SAI_IS_CAPTURE(sai)) { 3933e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_CR2_REGX, &cr2); 3943e086edfSolivier moysan if (cr2 & SAI_XCR2_MUTECNT_MASK) 3953e086edfSolivier moysan imr |= SAI_XIMR_MUTEDETIE; 3963e086edfSolivier moysan } 3973e086edfSolivier moysan 3983e086edfSolivier moysan if (sai->master) 3993e086edfSolivier moysan imr |= SAI_XIMR_WCKCFGIE; 4003e086edfSolivier moysan else 4013e086edfSolivier moysan imr |= SAI_XIMR_AFSDETIE | SAI_XIMR_LFSDETIE; 4023e086edfSolivier moysan 4033e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, 4043e086edfSolivier moysan SAI_XIMR_MASK, imr); 4053e086edfSolivier moysan 4063e086edfSolivier moysan return 0; 4073e086edfSolivier moysan } 4083e086edfSolivier moysan 4093e086edfSolivier moysan static int stm32_sai_set_config(struct snd_soc_dai *cpu_dai, 4103e086edfSolivier moysan struct snd_pcm_substream *substream, 4113e086edfSolivier moysan struct snd_pcm_hw_params *params) 4123e086edfSolivier moysan { 4133e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 4143e086edfSolivier moysan int cr1, cr1_mask, ret; 4153e086edfSolivier moysan int fth = STM_SAI_FIFO_TH_HALF; 4163e086edfSolivier moysan 4173e086edfSolivier moysan /* FIFO config */ 4183e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR2_REGX, 4193e086edfSolivier moysan SAI_XCR2_FFLUSH | SAI_XCR2_FTH_MASK, 4203e086edfSolivier moysan SAI_XCR2_FFLUSH | SAI_XCR2_FTH_SET(fth)); 4213e086edfSolivier moysan 4223e086edfSolivier moysan /* Mode, data format and channel config */ 4233e086edfSolivier moysan cr1 = SAI_XCR1_PRTCFG_SET(SAI_FREE_PROTOCOL); 4243e086edfSolivier moysan switch (params_format(params)) { 4253e086edfSolivier moysan case SNDRV_PCM_FORMAT_S8: 4263e086edfSolivier moysan cr1 |= SAI_XCR1_DS_SET(SAI_DATASIZE_8); 4273e086edfSolivier moysan break; 4283e086edfSolivier moysan case SNDRV_PCM_FORMAT_S16_LE: 4293e086edfSolivier moysan cr1 |= SAI_XCR1_DS_SET(SAI_DATASIZE_16); 4303e086edfSolivier moysan break; 4313e086edfSolivier moysan case SNDRV_PCM_FORMAT_S32_LE: 4323e086edfSolivier moysan cr1 |= SAI_XCR1_DS_SET(SAI_DATASIZE_32); 4333e086edfSolivier moysan break; 4343e086edfSolivier moysan default: 4353e086edfSolivier moysan dev_err(cpu_dai->dev, "Data format not supported"); 4363e086edfSolivier moysan return -EINVAL; 4373e086edfSolivier moysan } 4383e086edfSolivier moysan cr1_mask = SAI_XCR1_DS_MASK | SAI_XCR1_PRTCFG_MASK; 4393e086edfSolivier moysan 4403e086edfSolivier moysan cr1_mask |= SAI_XCR1_RX_TX; 4413e086edfSolivier moysan if (STM_SAI_IS_CAPTURE(sai)) 4423e086edfSolivier moysan cr1 |= SAI_XCR1_RX_TX; 4433e086edfSolivier moysan 4443e086edfSolivier moysan cr1_mask |= SAI_XCR1_MONO; 4453e086edfSolivier moysan if ((sai->slots == 2) && (params_channels(params) == 1)) 4463e086edfSolivier moysan cr1 |= SAI_XCR1_MONO; 4473e086edfSolivier moysan 4483e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1); 4493e086edfSolivier moysan if (ret < 0) { 4503e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 4513e086edfSolivier moysan return ret; 4523e086edfSolivier moysan } 4533e086edfSolivier moysan 4543e086edfSolivier moysan /* DMA config */ 4553e086edfSolivier moysan sai->dma_params.maxburst = STM_SAI_FIFO_SIZE * fth / sizeof(u32); 4563e086edfSolivier moysan snd_soc_dai_set_dma_data(cpu_dai, substream, (void *)&sai->dma_params); 4573e086edfSolivier moysan 4583e086edfSolivier moysan return 0; 4593e086edfSolivier moysan } 4603e086edfSolivier moysan 4613e086edfSolivier moysan static int stm32_sai_set_slots(struct snd_soc_dai *cpu_dai) 4623e086edfSolivier moysan { 4633e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 4643e086edfSolivier moysan int slotr, slot_sz; 4653e086edfSolivier moysan 4663e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_SLOTR_REGX, &slotr); 4673e086edfSolivier moysan 4683e086edfSolivier moysan /* 4693e086edfSolivier moysan * If SLOTSZ is set to auto in SLOTR, align slot width on data size 4703e086edfSolivier moysan * By default slot width = data size, if not forced from DT 4713e086edfSolivier moysan */ 4723e086edfSolivier moysan slot_sz = slotr & SAI_XSLOTR_SLOTSZ_MASK; 4733e086edfSolivier moysan if (slot_sz == SAI_XSLOTR_SLOTSZ_SET(SAI_SLOT_SIZE_AUTO)) 4743e086edfSolivier moysan sai->slot_width = sai->data_size; 4753e086edfSolivier moysan 4763e086edfSolivier moysan if (sai->slot_width < sai->data_size) { 4773e086edfSolivier moysan dev_err(cpu_dai->dev, 4783e086edfSolivier moysan "Data size %d larger than slot width\n", 4793e086edfSolivier moysan sai->data_size); 4803e086edfSolivier moysan return -EINVAL; 4813e086edfSolivier moysan } 4823e086edfSolivier moysan 4833e086edfSolivier moysan /* Slot number is set to 2, if not specified in DT */ 4843e086edfSolivier moysan if (!sai->slots) 4853e086edfSolivier moysan sai->slots = 2; 4863e086edfSolivier moysan 4873e086edfSolivier moysan /* The number of slots in the audio frame is equal to NBSLOT[3:0] + 1*/ 4883e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, 4893e086edfSolivier moysan SAI_XSLOTR_NBSLOT_MASK, 4903e086edfSolivier moysan SAI_XSLOTR_NBSLOT_SET((sai->slots - 1))); 4913e086edfSolivier moysan 4923e086edfSolivier moysan /* Set default slots mask if not already set from DT */ 4933e086edfSolivier moysan if (!(slotr & SAI_XSLOTR_SLOTEN_MASK)) { 4943e086edfSolivier moysan sai->slot_mask = (1 << sai->slots) - 1; 4953e086edfSolivier moysan regmap_update_bits(sai->regmap, 4963e086edfSolivier moysan STM_SAI_SLOTR_REGX, SAI_XSLOTR_SLOTEN_MASK, 4973e086edfSolivier moysan SAI_XSLOTR_SLOTEN_SET(sai->slot_mask)); 4983e086edfSolivier moysan } 4993e086edfSolivier moysan 500*602fdadcSolivier moysan dev_dbg(cpu_dai->dev, "Slots %d, slot width %d\n", 5013e086edfSolivier moysan sai->slots, sai->slot_width); 5023e086edfSolivier moysan 5033e086edfSolivier moysan return 0; 5043e086edfSolivier moysan } 5053e086edfSolivier moysan 5063e086edfSolivier moysan static void stm32_sai_set_frame(struct snd_soc_dai *cpu_dai) 5073e086edfSolivier moysan { 5083e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 5093e086edfSolivier moysan int fs_active, offset, format; 5103e086edfSolivier moysan int frcr, frcr_mask; 5113e086edfSolivier moysan 5123e086edfSolivier moysan format = sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK; 5133e086edfSolivier moysan sai->fs_length = sai->slot_width * sai->slots; 5143e086edfSolivier moysan 5153e086edfSolivier moysan fs_active = sai->fs_length / 2; 5163e086edfSolivier moysan if ((format == SND_SOC_DAIFMT_DSP_A) || 5173e086edfSolivier moysan (format == SND_SOC_DAIFMT_DSP_B)) 5183e086edfSolivier moysan fs_active = 1; 5193e086edfSolivier moysan 5203e086edfSolivier moysan frcr = SAI_XFRCR_FRL_SET((sai->fs_length - 1)); 5213e086edfSolivier moysan frcr |= SAI_XFRCR_FSALL_SET((fs_active - 1)); 5223e086edfSolivier moysan frcr_mask = SAI_XFRCR_FRL_MASK | SAI_XFRCR_FSALL_MASK; 5233e086edfSolivier moysan 524*602fdadcSolivier moysan dev_dbg(cpu_dai->dev, "Frame length %d, frame active %d\n", 5253e086edfSolivier moysan sai->fs_length, fs_active); 5263e086edfSolivier moysan 5273e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr); 5283e086edfSolivier moysan 5293e086edfSolivier moysan if ((sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_LSB) { 5303e086edfSolivier moysan offset = sai->slot_width - sai->data_size; 5313e086edfSolivier moysan 5323e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, 5333e086edfSolivier moysan SAI_XSLOTR_FBOFF_MASK, 5343e086edfSolivier moysan SAI_XSLOTR_FBOFF_SET(offset)); 5353e086edfSolivier moysan } 5363e086edfSolivier moysan } 5373e086edfSolivier moysan 5383e086edfSolivier moysan static int stm32_sai_configure_clock(struct snd_soc_dai *cpu_dai, 5393e086edfSolivier moysan struct snd_pcm_hw_params *params) 5403e086edfSolivier moysan { 5413e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 5423e086edfSolivier moysan int cr1, mask, div = 0; 5433e086edfSolivier moysan int sai_clk_rate, ret; 5443e086edfSolivier moysan 5453e086edfSolivier moysan if (!sai->mclk_rate) { 5463e086edfSolivier moysan dev_err(cpu_dai->dev, "Mclk rate is null\n"); 5473e086edfSolivier moysan return -EINVAL; 5483e086edfSolivier moysan } 5493e086edfSolivier moysan 5503e086edfSolivier moysan if (!(params_rate(params) % 11025)) 5513e086edfSolivier moysan clk_set_parent(sai->sai_ck, sai->pdata->clk_x11k); 5523e086edfSolivier moysan else 5533e086edfSolivier moysan clk_set_parent(sai->sai_ck, sai->pdata->clk_x8k); 5543e086edfSolivier moysan sai_clk_rate = clk_get_rate(sai->sai_ck); 5553e086edfSolivier moysan 5563e086edfSolivier moysan /* 5573e086edfSolivier moysan * mclk_rate = 256 * fs 5583e086edfSolivier moysan * MCKDIV = 0 if sai_ck < 3/2 * mclk_rate 5593e086edfSolivier moysan * MCKDIV = sai_ck / (2 * mclk_rate) otherwise 5603e086edfSolivier moysan */ 5613e086edfSolivier moysan if (2 * sai_clk_rate >= 3 * sai->mclk_rate) 5623e086edfSolivier moysan div = DIV_ROUND_CLOSEST(sai_clk_rate, 2 * sai->mclk_rate); 5633e086edfSolivier moysan 5643e086edfSolivier moysan if (div > SAI_XCR1_MCKDIV_MAX) { 5653e086edfSolivier moysan dev_err(cpu_dai->dev, "Divider %d out of range\n", div); 5663e086edfSolivier moysan return -EINVAL; 5673e086edfSolivier moysan } 5683e086edfSolivier moysan dev_dbg(cpu_dai->dev, "SAI clock %d, divider %d\n", sai_clk_rate, div); 5693e086edfSolivier moysan 5703e086edfSolivier moysan mask = SAI_XCR1_MCKDIV_MASK; 5713e086edfSolivier moysan cr1 = SAI_XCR1_MCKDIV_SET(div); 5723e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, mask, cr1); 5733e086edfSolivier moysan if (ret < 0) { 5743e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 5753e086edfSolivier moysan return ret; 5763e086edfSolivier moysan } 5773e086edfSolivier moysan 5783e086edfSolivier moysan return 0; 5793e086edfSolivier moysan } 5803e086edfSolivier moysan 5813e086edfSolivier moysan static int stm32_sai_hw_params(struct snd_pcm_substream *substream, 5823e086edfSolivier moysan struct snd_pcm_hw_params *params, 5833e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 5843e086edfSolivier moysan { 5853e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 5863e086edfSolivier moysan int ret; 5873e086edfSolivier moysan 5883e086edfSolivier moysan sai->data_size = params_width(params); 5893e086edfSolivier moysan 5903e086edfSolivier moysan ret = stm32_sai_set_slots(cpu_dai); 5913e086edfSolivier moysan if (ret < 0) 5923e086edfSolivier moysan return ret; 5933e086edfSolivier moysan stm32_sai_set_frame(cpu_dai); 5943e086edfSolivier moysan 5953e086edfSolivier moysan ret = stm32_sai_set_config(cpu_dai, substream, params); 5963e086edfSolivier moysan if (ret) 5973e086edfSolivier moysan return ret; 5983e086edfSolivier moysan 5993e086edfSolivier moysan if (sai->master) 6003e086edfSolivier moysan ret = stm32_sai_configure_clock(cpu_dai, params); 6013e086edfSolivier moysan 6023e086edfSolivier moysan return ret; 6033e086edfSolivier moysan } 6043e086edfSolivier moysan 6053e086edfSolivier moysan static int stm32_sai_trigger(struct snd_pcm_substream *substream, int cmd, 6063e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 6073e086edfSolivier moysan { 6083e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 6093e086edfSolivier moysan int ret; 6103e086edfSolivier moysan 6113e086edfSolivier moysan switch (cmd) { 6123e086edfSolivier moysan case SNDRV_PCM_TRIGGER_START: 6133e086edfSolivier moysan case SNDRV_PCM_TRIGGER_RESUME: 6143e086edfSolivier moysan case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 6153e086edfSolivier moysan dev_dbg(cpu_dai->dev, "Enable DMA and SAI\n"); 6163e086edfSolivier moysan 6173e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 6183e086edfSolivier moysan SAI_XCR1_DMAEN, SAI_XCR1_DMAEN); 6193e086edfSolivier moysan 6203e086edfSolivier moysan /* Enable SAI */ 6213e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 6223e086edfSolivier moysan SAI_XCR1_SAIEN, SAI_XCR1_SAIEN); 6233e086edfSolivier moysan if (ret < 0) 6243e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 6253e086edfSolivier moysan break; 6263e086edfSolivier moysan case SNDRV_PCM_TRIGGER_SUSPEND: 6273e086edfSolivier moysan case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 6283e086edfSolivier moysan case SNDRV_PCM_TRIGGER_STOP: 6293e086edfSolivier moysan dev_dbg(cpu_dai->dev, "Disable DMA and SAI\n"); 6303e086edfSolivier moysan 6313e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 6323e086edfSolivier moysan SAI_XCR1_DMAEN, 6333e086edfSolivier moysan (unsigned int)~SAI_XCR1_DMAEN); 6343e086edfSolivier moysan 6353e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 6363e086edfSolivier moysan SAI_XCR1_SAIEN, 6373e086edfSolivier moysan (unsigned int)~SAI_XCR1_SAIEN); 6383e086edfSolivier moysan if (ret < 0) 6393e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 6403e086edfSolivier moysan break; 6413e086edfSolivier moysan default: 6423e086edfSolivier moysan return -EINVAL; 6433e086edfSolivier moysan } 6443e086edfSolivier moysan 6453e086edfSolivier moysan return ret; 6463e086edfSolivier moysan } 6473e086edfSolivier moysan 6483e086edfSolivier moysan static void stm32_sai_shutdown(struct snd_pcm_substream *substream, 6493e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 6503e086edfSolivier moysan { 6513e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 6523e086edfSolivier moysan 6533e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, SAI_XIMR_MASK, 0); 6543e086edfSolivier moysan 6553e086edfSolivier moysan clk_disable_unprepare(sai->sai_ck); 6563e086edfSolivier moysan sai->substream = NULL; 6573e086edfSolivier moysan } 6583e086edfSolivier moysan 6593e086edfSolivier moysan static int stm32_sai_dai_probe(struct snd_soc_dai *cpu_dai) 6603e086edfSolivier moysan { 6613e086edfSolivier moysan struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev); 6623e086edfSolivier moysan 6633e086edfSolivier moysan sai->dma_params.addr = (dma_addr_t)(sai->phys_addr + STM_SAI_DR_REGX); 6643e086edfSolivier moysan sai->dma_params.maxburst = 1; 6653e086edfSolivier moysan /* Buswidth will be set by framework at runtime */ 6663e086edfSolivier moysan sai->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED; 6673e086edfSolivier moysan 6683e086edfSolivier moysan if (STM_SAI_IS_PLAYBACK(sai)) 6693e086edfSolivier moysan snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params, NULL); 6703e086edfSolivier moysan else 6713e086edfSolivier moysan snd_soc_dai_init_dma_data(cpu_dai, NULL, &sai->dma_params); 6723e086edfSolivier moysan 6733e086edfSolivier moysan return 0; 6743e086edfSolivier moysan } 6753e086edfSolivier moysan 6763e086edfSolivier moysan static const struct snd_soc_dai_ops stm32_sai_pcm_dai_ops = { 6773e086edfSolivier moysan .set_sysclk = stm32_sai_set_sysclk, 6783e086edfSolivier moysan .set_fmt = stm32_sai_set_dai_fmt, 6793e086edfSolivier moysan .set_tdm_slot = stm32_sai_set_dai_tdm_slot, 6803e086edfSolivier moysan .startup = stm32_sai_startup, 6813e086edfSolivier moysan .hw_params = stm32_sai_hw_params, 6823e086edfSolivier moysan .trigger = stm32_sai_trigger, 6833e086edfSolivier moysan .shutdown = stm32_sai_shutdown, 6843e086edfSolivier moysan }; 6853e086edfSolivier moysan 6863e086edfSolivier moysan static const struct snd_pcm_hardware stm32_sai_pcm_hw = { 6873e086edfSolivier moysan .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP, 6883e086edfSolivier moysan .buffer_bytes_max = 8 * PAGE_SIZE, 6893e086edfSolivier moysan .period_bytes_min = 1024, /* 5ms at 48kHz */ 6903e086edfSolivier moysan .period_bytes_max = PAGE_SIZE, 6913e086edfSolivier moysan .periods_min = 2, 6923e086edfSolivier moysan .periods_max = 8, 6933e086edfSolivier moysan }; 6943e086edfSolivier moysan 6953e086edfSolivier moysan static struct snd_soc_dai_driver stm32_sai_playback_dai[] = { 6963e086edfSolivier moysan { 6973e086edfSolivier moysan .probe = stm32_sai_dai_probe, 6983e086edfSolivier moysan .id = 1, /* avoid call to fmt_single_name() */ 6993e086edfSolivier moysan .playback = { 7003e086edfSolivier moysan .channels_min = 1, 7013e086edfSolivier moysan .channels_max = 2, 7023e086edfSolivier moysan .rate_min = 8000, 7033e086edfSolivier moysan .rate_max = 192000, 7043e086edfSolivier moysan .rates = SNDRV_PCM_RATE_CONTINUOUS, 7053e086edfSolivier moysan /* DMA does not support 24 bits transfers */ 7063e086edfSolivier moysan .formats = 7073e086edfSolivier moysan SNDRV_PCM_FMTBIT_S8 | 7083e086edfSolivier moysan SNDRV_PCM_FMTBIT_S16_LE | 7093e086edfSolivier moysan SNDRV_PCM_FMTBIT_S32_LE, 7103e086edfSolivier moysan }, 7113e086edfSolivier moysan .ops = &stm32_sai_pcm_dai_ops, 7123e086edfSolivier moysan } 7133e086edfSolivier moysan }; 7143e086edfSolivier moysan 7153e086edfSolivier moysan static struct snd_soc_dai_driver stm32_sai_capture_dai[] = { 7163e086edfSolivier moysan { 7173e086edfSolivier moysan .probe = stm32_sai_dai_probe, 7183e086edfSolivier moysan .id = 1, /* avoid call to fmt_single_name() */ 7193e086edfSolivier moysan .capture = { 7203e086edfSolivier moysan .channels_min = 1, 7213e086edfSolivier moysan .channels_max = 2, 7223e086edfSolivier moysan .rate_min = 8000, 7233e086edfSolivier moysan .rate_max = 192000, 7243e086edfSolivier moysan .rates = SNDRV_PCM_RATE_CONTINUOUS, 7253e086edfSolivier moysan /* DMA does not support 24 bits transfers */ 7263e086edfSolivier moysan .formats = 7273e086edfSolivier moysan SNDRV_PCM_FMTBIT_S8 | 7283e086edfSolivier moysan SNDRV_PCM_FMTBIT_S16_LE | 7293e086edfSolivier moysan SNDRV_PCM_FMTBIT_S32_LE, 7303e086edfSolivier moysan }, 7313e086edfSolivier moysan .ops = &stm32_sai_pcm_dai_ops, 7323e086edfSolivier moysan } 7333e086edfSolivier moysan }; 7343e086edfSolivier moysan 7353e086edfSolivier moysan static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config = { 7363e086edfSolivier moysan .pcm_hardware = &stm32_sai_pcm_hw, 7373e086edfSolivier moysan .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config, 7383e086edfSolivier moysan }; 7393e086edfSolivier moysan 7403e086edfSolivier moysan static const struct snd_soc_component_driver stm32_component = { 7413e086edfSolivier moysan .name = "stm32-sai", 7423e086edfSolivier moysan }; 7433e086edfSolivier moysan 7443e086edfSolivier moysan static const struct of_device_id stm32_sai_sub_ids[] = { 7453e086edfSolivier moysan { .compatible = "st,stm32-sai-sub-a", 7463e086edfSolivier moysan .data = (void *)STM_SAI_A_ID}, 7473e086edfSolivier moysan { .compatible = "st,stm32-sai-sub-b", 7483e086edfSolivier moysan .data = (void *)STM_SAI_B_ID}, 7493e086edfSolivier moysan {} 7503e086edfSolivier moysan }; 7513e086edfSolivier moysan MODULE_DEVICE_TABLE(of, stm32_sai_sub_ids); 7523e086edfSolivier moysan 7533e086edfSolivier moysan static int stm32_sai_sub_parse_of(struct platform_device *pdev, 7543e086edfSolivier moysan struct stm32_sai_sub_data *sai) 7553e086edfSolivier moysan { 7563e086edfSolivier moysan struct device_node *np = pdev->dev.of_node; 7573e086edfSolivier moysan struct resource *res; 7583e086edfSolivier moysan void __iomem *base; 7593e086edfSolivier moysan 7603e086edfSolivier moysan if (!np) 7613e086edfSolivier moysan return -ENODEV; 7623e086edfSolivier moysan 7633e086edfSolivier moysan res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 7643e086edfSolivier moysan 7653e086edfSolivier moysan dev_err(&pdev->dev, "res %pr\n", res); 7663e086edfSolivier moysan 7673e086edfSolivier moysan base = devm_ioremap_resource(&pdev->dev, res); 7683e086edfSolivier moysan if (IS_ERR(base)) 7693e086edfSolivier moysan return PTR_ERR(base); 7703e086edfSolivier moysan 7713e086edfSolivier moysan sai->phys_addr = res->start; 7723e086edfSolivier moysan sai->regmap = devm_regmap_init_mmio(&pdev->dev, base, 7733e086edfSolivier moysan &stm32_sai_sub_regmap_config); 7743e086edfSolivier moysan 7753e086edfSolivier moysan /* Get direction property */ 7763e086edfSolivier moysan if (of_property_match_string(np, "dma-names", "tx") >= 0) { 7773e086edfSolivier moysan sai->dir = SNDRV_PCM_STREAM_PLAYBACK; 7783e086edfSolivier moysan } else if (of_property_match_string(np, "dma-names", "rx") >= 0) { 7793e086edfSolivier moysan sai->dir = SNDRV_PCM_STREAM_CAPTURE; 7803e086edfSolivier moysan } else { 7813e086edfSolivier moysan dev_err(&pdev->dev, "Unsupported direction\n"); 7823e086edfSolivier moysan return -EINVAL; 7833e086edfSolivier moysan } 7843e086edfSolivier moysan 7853e086edfSolivier moysan sai->sai_ck = devm_clk_get(&pdev->dev, "sai_ck"); 7863e086edfSolivier moysan if (IS_ERR(sai->sai_ck)) { 787*602fdadcSolivier moysan dev_err(&pdev->dev, "Missing kernel clock sai_ck\n"); 7883e086edfSolivier moysan return PTR_ERR(sai->sai_ck); 7893e086edfSolivier moysan } 7903e086edfSolivier moysan 7913e086edfSolivier moysan return 0; 7923e086edfSolivier moysan } 7933e086edfSolivier moysan 7943e086edfSolivier moysan static int stm32_sai_sub_dais_init(struct platform_device *pdev, 7953e086edfSolivier moysan struct stm32_sai_sub_data *sai) 7963e086edfSolivier moysan { 7973e086edfSolivier moysan sai->cpu_dai_drv = devm_kzalloc(&pdev->dev, 7983e086edfSolivier moysan sizeof(struct snd_soc_dai_driver), 7993e086edfSolivier moysan GFP_KERNEL); 8003e086edfSolivier moysan if (!sai->cpu_dai_drv) 8013e086edfSolivier moysan return -ENOMEM; 8023e086edfSolivier moysan 8033e086edfSolivier moysan sai->cpu_dai_drv->name = dev_name(&pdev->dev); 8043e086edfSolivier moysan if (STM_SAI_IS_PLAYBACK(sai)) { 8053e086edfSolivier moysan memcpy(sai->cpu_dai_drv, &stm32_sai_playback_dai, 8063e086edfSolivier moysan sizeof(stm32_sai_playback_dai)); 8073e086edfSolivier moysan sai->cpu_dai_drv->playback.stream_name = sai->cpu_dai_drv->name; 8083e086edfSolivier moysan } else { 8093e086edfSolivier moysan memcpy(sai->cpu_dai_drv, &stm32_sai_capture_dai, 8103e086edfSolivier moysan sizeof(stm32_sai_capture_dai)); 8113e086edfSolivier moysan sai->cpu_dai_drv->capture.stream_name = sai->cpu_dai_drv->name; 8123e086edfSolivier moysan } 8133e086edfSolivier moysan 8143e086edfSolivier moysan return 0; 8153e086edfSolivier moysan } 8163e086edfSolivier moysan 8173e086edfSolivier moysan static int stm32_sai_sub_probe(struct platform_device *pdev) 8183e086edfSolivier moysan { 8193e086edfSolivier moysan struct stm32_sai_sub_data *sai; 8203e086edfSolivier moysan const struct of_device_id *of_id; 8213e086edfSolivier moysan int ret; 8223e086edfSolivier moysan 8233e086edfSolivier moysan sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL); 8243e086edfSolivier moysan if (!sai) 8253e086edfSolivier moysan return -ENOMEM; 8263e086edfSolivier moysan 8273e086edfSolivier moysan of_id = of_match_device(stm32_sai_sub_ids, &pdev->dev); 8283e086edfSolivier moysan if (!of_id) 8293e086edfSolivier moysan return -EINVAL; 8303e086edfSolivier moysan sai->id = (uintptr_t)of_id->data; 8313e086edfSolivier moysan 8323e086edfSolivier moysan sai->pdev = pdev; 8333e086edfSolivier moysan platform_set_drvdata(pdev, sai); 8343e086edfSolivier moysan 8353e086edfSolivier moysan sai->pdata = dev_get_drvdata(pdev->dev.parent); 8363e086edfSolivier moysan if (!sai->pdata) { 8373e086edfSolivier moysan dev_err(&pdev->dev, "Parent device data not available\n"); 8383e086edfSolivier moysan return -EINVAL; 8393e086edfSolivier moysan } 8403e086edfSolivier moysan 8413e086edfSolivier moysan ret = stm32_sai_sub_parse_of(pdev, sai); 8423e086edfSolivier moysan if (ret) 8433e086edfSolivier moysan return ret; 8443e086edfSolivier moysan 8453e086edfSolivier moysan ret = stm32_sai_sub_dais_init(pdev, sai); 8463e086edfSolivier moysan if (ret) 8473e086edfSolivier moysan return ret; 8483e086edfSolivier moysan 8493e086edfSolivier moysan ret = devm_request_irq(&pdev->dev, sai->pdata->irq, stm32_sai_isr, 8503e086edfSolivier moysan IRQF_SHARED, dev_name(&pdev->dev), sai); 8513e086edfSolivier moysan if (ret) { 852*602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ request returned %d\n", ret); 8533e086edfSolivier moysan return ret; 8543e086edfSolivier moysan } 8553e086edfSolivier moysan 8563e086edfSolivier moysan ret = devm_snd_soc_register_component(&pdev->dev, &stm32_component, 8573e086edfSolivier moysan sai->cpu_dai_drv, 1); 8583e086edfSolivier moysan if (ret) 8593e086edfSolivier moysan return ret; 8603e086edfSolivier moysan 8613e086edfSolivier moysan ret = devm_snd_dmaengine_pcm_register(&pdev->dev, 8623e086edfSolivier moysan &stm32_sai_pcm_config, 0); 8633e086edfSolivier moysan if (ret) { 864*602fdadcSolivier moysan dev_err(&pdev->dev, "Could not register pcm dma\n"); 8653e086edfSolivier moysan return ret; 8663e086edfSolivier moysan } 8673e086edfSolivier moysan 8683e086edfSolivier moysan return 0; 8693e086edfSolivier moysan } 8703e086edfSolivier moysan 8713e086edfSolivier moysan static struct platform_driver stm32_sai_sub_driver = { 8723e086edfSolivier moysan .driver = { 8733e086edfSolivier moysan .name = "st,stm32-sai-sub", 8743e086edfSolivier moysan .of_match_table = stm32_sai_sub_ids, 8753e086edfSolivier moysan }, 8763e086edfSolivier moysan .probe = stm32_sai_sub_probe, 8773e086edfSolivier moysan }; 8783e086edfSolivier moysan 8793e086edfSolivier moysan module_platform_driver(stm32_sai_sub_driver); 8803e086edfSolivier moysan 8813e086edfSolivier moysan MODULE_DESCRIPTION("STM32 Soc SAI sub-block Interface"); 882*602fdadcSolivier moysan MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>"); 8833e086edfSolivier moysan MODULE_ALIAS("platform:st,stm32-sai-sub"); 8843e086edfSolivier moysan MODULE_LICENSE("GPL v2"); 885