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 266eb17d70SOlivier Moysan #include <sound/asoundef.h> 273e086edfSolivier moysan #include <sound/core.h> 283e086edfSolivier moysan #include <sound/dmaengine_pcm.h> 293e086edfSolivier moysan #include <sound/pcm_params.h> 303e086edfSolivier moysan 313e086edfSolivier moysan #include "stm32_sai.h" 323e086edfSolivier moysan 333e086edfSolivier moysan #define SAI_FREE_PROTOCOL 0x0 346eb17d70SOlivier Moysan #define SAI_SPDIF_PROTOCOL 0x1 353e086edfSolivier moysan 363e086edfSolivier moysan #define SAI_SLOT_SIZE_AUTO 0x0 373e086edfSolivier moysan #define SAI_SLOT_SIZE_16 0x1 383e086edfSolivier moysan #define SAI_SLOT_SIZE_32 0x2 393e086edfSolivier moysan 403e086edfSolivier moysan #define SAI_DATASIZE_8 0x2 413e086edfSolivier moysan #define SAI_DATASIZE_10 0x3 423e086edfSolivier moysan #define SAI_DATASIZE_16 0x4 433e086edfSolivier moysan #define SAI_DATASIZE_20 0x5 443e086edfSolivier moysan #define SAI_DATASIZE_24 0x6 453e086edfSolivier moysan #define SAI_DATASIZE_32 0x7 463e086edfSolivier moysan 473e086edfSolivier moysan #define STM_SAI_FIFO_SIZE 8 483e086edfSolivier moysan #define STM_SAI_DAI_NAME_SIZE 15 493e086edfSolivier moysan 503e086edfSolivier moysan #define STM_SAI_IS_PLAYBACK(ip) ((ip)->dir == SNDRV_PCM_STREAM_PLAYBACK) 513e086edfSolivier moysan #define STM_SAI_IS_CAPTURE(ip) ((ip)->dir == SNDRV_PCM_STREAM_CAPTURE) 523e086edfSolivier moysan 533e086edfSolivier moysan #define STM_SAI_A_ID 0x0 543e086edfSolivier moysan #define STM_SAI_B_ID 0x1 553e086edfSolivier moysan 5603e78a24Solivier moysan #define STM_SAI_IS_SUB_A(x) ((x)->id == STM_SAI_A_ID) 5703e78a24Solivier moysan #define STM_SAI_IS_SUB_B(x) ((x)->id == STM_SAI_B_ID) 583e086edfSolivier moysan #define STM_SAI_BLOCK_NAME(x) (((x)->id == STM_SAI_A_ID) ? "A" : "B") 593e086edfSolivier moysan 605914d285SOlivier Moysan #define SAI_SYNC_NONE 0x0 615914d285SOlivier Moysan #define SAI_SYNC_INTERNAL 0x1 625914d285SOlivier Moysan #define SAI_SYNC_EXTERNAL 0x2 635914d285SOlivier Moysan 646eb17d70SOlivier Moysan #define STM_SAI_PROTOCOL_IS_SPDIF(ip) ((ip)->spdif) 656eb17d70SOlivier Moysan #define STM_SAI_HAS_SPDIF(x) ((x)->pdata->conf->has_spdif) 665914d285SOlivier Moysan #define STM_SAI_HAS_EXT_SYNC(x) (!STM_SAI_IS_F4(sai->pdata)) 675914d285SOlivier Moysan 686eb17d70SOlivier Moysan #define SAI_IEC60958_BLOCK_FRAMES 192 696eb17d70SOlivier Moysan #define SAI_IEC60958_STATUS_BYTES 24 706eb17d70SOlivier Moysan 713e086edfSolivier moysan /** 723e086edfSolivier moysan * struct stm32_sai_sub_data - private data of SAI sub block (block A or B) 733e086edfSolivier moysan * @pdev: device data pointer 743e086edfSolivier moysan * @regmap: SAI register map pointer 7503e78a24Solivier moysan * @regmap_config: SAI sub block register map configuration pointer 763e086edfSolivier moysan * @dma_params: dma configuration data for rx or tx channel 773e086edfSolivier moysan * @cpu_dai_drv: DAI driver data pointer 783e086edfSolivier moysan * @cpu_dai: DAI runtime data pointer 793e086edfSolivier moysan * @substream: PCM substream data pointer 803e086edfSolivier moysan * @pdata: SAI block parent data pointer 815914d285SOlivier Moysan * @np_sync_provider: synchronization provider node 823e086edfSolivier moysan * @sai_ck: kernel clock feeding the SAI clock generator 833e086edfSolivier moysan * @phys_addr: SAI registers physical base address 843e086edfSolivier moysan * @mclk_rate: SAI block master clock frequency (Hz). set at init 853e086edfSolivier moysan * @id: SAI sub block id corresponding to sub-block A or B 863e086edfSolivier moysan * @dir: SAI block direction (playback or capture). set at init 873e086edfSolivier moysan * @master: SAI block mode flag. (true=master, false=slave) set at init 886eb17d70SOlivier Moysan * @spdif: SAI S/PDIF iec60958 mode flag. set at init 893e086edfSolivier moysan * @fmt: SAI block format. relevant only for custom protocols. set at init 903e086edfSolivier moysan * @sync: SAI block synchronization mode. (none, internal or external) 915914d285SOlivier Moysan * @synco: SAI block ext sync source (provider setting). (none, sub-block A/B) 925914d285SOlivier Moysan * @synci: SAI block ext sync source (client setting). (SAI sync provider index) 933e086edfSolivier moysan * @fs_length: frame synchronization length. depends on protocol settings 943e086edfSolivier moysan * @slots: rx or tx slot number 953e086edfSolivier moysan * @slot_width: rx or tx slot width in bits 963e086edfSolivier moysan * @slot_mask: rx or tx active slots mask. set at init or at runtime 973e086edfSolivier moysan * @data_size: PCM data width. corresponds to PCM substream width. 986eb17d70SOlivier Moysan * @spdif_frm_cnt: S/PDIF playback frame counter 99*187e01d0Solivier moysan * @snd_aes_iec958: iec958 data 100*187e01d0Solivier moysan * @ctrl_lock: control lock 1013e086edfSolivier moysan */ 1023e086edfSolivier moysan struct stm32_sai_sub_data { 1033e086edfSolivier moysan struct platform_device *pdev; 1043e086edfSolivier moysan struct regmap *regmap; 10503e78a24Solivier moysan const struct regmap_config *regmap_config; 1063e086edfSolivier moysan struct snd_dmaengine_dai_dma_data dma_params; 1073e086edfSolivier moysan struct snd_soc_dai_driver *cpu_dai_drv; 1083e086edfSolivier moysan struct snd_soc_dai *cpu_dai; 1093e086edfSolivier moysan struct snd_pcm_substream *substream; 1103e086edfSolivier moysan struct stm32_sai_data *pdata; 1115914d285SOlivier Moysan struct device_node *np_sync_provider; 1123e086edfSolivier moysan struct clk *sai_ck; 1133e086edfSolivier moysan dma_addr_t phys_addr; 1143e086edfSolivier moysan unsigned int mclk_rate; 1153e086edfSolivier moysan unsigned int id; 1163e086edfSolivier moysan int dir; 1173e086edfSolivier moysan bool master; 1186eb17d70SOlivier Moysan bool spdif; 1193e086edfSolivier moysan int fmt; 1203e086edfSolivier moysan int sync; 1215914d285SOlivier Moysan int synco; 1225914d285SOlivier Moysan int synci; 1233e086edfSolivier moysan int fs_length; 1243e086edfSolivier moysan int slots; 1253e086edfSolivier moysan int slot_width; 1263e086edfSolivier moysan int slot_mask; 1273e086edfSolivier moysan int data_size; 1286eb17d70SOlivier Moysan unsigned int spdif_frm_cnt; 129*187e01d0Solivier moysan struct snd_aes_iec958 iec958; 130*187e01d0Solivier moysan struct mutex ctrl_lock; /* protect resources accessed by controls */ 1313e086edfSolivier moysan }; 1323e086edfSolivier moysan 1333e086edfSolivier moysan enum stm32_sai_fifo_th { 1343e086edfSolivier moysan STM_SAI_FIFO_TH_EMPTY, 1353e086edfSolivier moysan STM_SAI_FIFO_TH_QUARTER, 1363e086edfSolivier moysan STM_SAI_FIFO_TH_HALF, 1373e086edfSolivier moysan STM_SAI_FIFO_TH_3_QUARTER, 1383e086edfSolivier moysan STM_SAI_FIFO_TH_FULL, 1393e086edfSolivier moysan }; 1403e086edfSolivier moysan 1413e086edfSolivier moysan static bool stm32_sai_sub_readable_reg(struct device *dev, unsigned int reg) 1423e086edfSolivier moysan { 1433e086edfSolivier moysan switch (reg) { 1443e086edfSolivier moysan case STM_SAI_CR1_REGX: 1453e086edfSolivier moysan case STM_SAI_CR2_REGX: 1463e086edfSolivier moysan case STM_SAI_FRCR_REGX: 1473e086edfSolivier moysan case STM_SAI_SLOTR_REGX: 1483e086edfSolivier moysan case STM_SAI_IMR_REGX: 1493e086edfSolivier moysan case STM_SAI_SR_REGX: 1503e086edfSolivier moysan case STM_SAI_CLRFR_REGX: 1513e086edfSolivier moysan case STM_SAI_DR_REGX: 15203e78a24Solivier moysan case STM_SAI_PDMCR_REGX: 15303e78a24Solivier moysan case STM_SAI_PDMLY_REGX: 1543e086edfSolivier moysan return true; 1553e086edfSolivier moysan default: 1563e086edfSolivier moysan return false; 1573e086edfSolivier moysan } 1583e086edfSolivier moysan } 1593e086edfSolivier moysan 1603e086edfSolivier moysan static bool stm32_sai_sub_volatile_reg(struct device *dev, unsigned int reg) 1613e086edfSolivier moysan { 1623e086edfSolivier moysan switch (reg) { 1633e086edfSolivier moysan case STM_SAI_DR_REGX: 1643e086edfSolivier moysan return true; 1653e086edfSolivier moysan default: 1663e086edfSolivier moysan return false; 1673e086edfSolivier moysan } 1683e086edfSolivier moysan } 1693e086edfSolivier moysan 1703e086edfSolivier moysan static bool stm32_sai_sub_writeable_reg(struct device *dev, unsigned int reg) 1713e086edfSolivier moysan { 1723e086edfSolivier moysan switch (reg) { 1733e086edfSolivier moysan case STM_SAI_CR1_REGX: 1743e086edfSolivier moysan case STM_SAI_CR2_REGX: 1753e086edfSolivier moysan case STM_SAI_FRCR_REGX: 1763e086edfSolivier moysan case STM_SAI_SLOTR_REGX: 1773e086edfSolivier moysan case STM_SAI_IMR_REGX: 1783e086edfSolivier moysan case STM_SAI_SR_REGX: 1793e086edfSolivier moysan case STM_SAI_CLRFR_REGX: 1803e086edfSolivier moysan case STM_SAI_DR_REGX: 18103e78a24Solivier moysan case STM_SAI_PDMCR_REGX: 18203e78a24Solivier moysan case STM_SAI_PDMLY_REGX: 1833e086edfSolivier moysan return true; 1843e086edfSolivier moysan default: 1853e086edfSolivier moysan return false; 1863e086edfSolivier moysan } 1873e086edfSolivier moysan } 1883e086edfSolivier moysan 18903e78a24Solivier moysan static const struct regmap_config stm32_sai_sub_regmap_config_f4 = { 1903e086edfSolivier moysan .reg_bits = 32, 1913e086edfSolivier moysan .reg_stride = 4, 1923e086edfSolivier moysan .val_bits = 32, 1933e086edfSolivier moysan .max_register = STM_SAI_DR_REGX, 1943e086edfSolivier moysan .readable_reg = stm32_sai_sub_readable_reg, 1953e086edfSolivier moysan .volatile_reg = stm32_sai_sub_volatile_reg, 1963e086edfSolivier moysan .writeable_reg = stm32_sai_sub_writeable_reg, 1973e086edfSolivier moysan .fast_io = true, 1983e086edfSolivier moysan }; 1993e086edfSolivier moysan 20003e78a24Solivier moysan static const struct regmap_config stm32_sai_sub_regmap_config_h7 = { 20103e78a24Solivier moysan .reg_bits = 32, 20203e78a24Solivier moysan .reg_stride = 4, 20303e78a24Solivier moysan .val_bits = 32, 20403e78a24Solivier moysan .max_register = STM_SAI_PDMLY_REGX, 20503e78a24Solivier moysan .readable_reg = stm32_sai_sub_readable_reg, 20603e78a24Solivier moysan .volatile_reg = stm32_sai_sub_volatile_reg, 20703e78a24Solivier moysan .writeable_reg = stm32_sai_sub_writeable_reg, 20803e78a24Solivier moysan .fast_io = true, 20903e78a24Solivier moysan }; 21003e78a24Solivier moysan 211*187e01d0Solivier moysan static int snd_pcm_iec958_info(struct snd_kcontrol *kcontrol, 212*187e01d0Solivier moysan struct snd_ctl_elem_info *uinfo) 213*187e01d0Solivier moysan { 214*187e01d0Solivier moysan uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; 215*187e01d0Solivier moysan uinfo->count = 1; 216*187e01d0Solivier moysan 217*187e01d0Solivier moysan return 0; 218*187e01d0Solivier moysan } 219*187e01d0Solivier moysan 220*187e01d0Solivier moysan static int snd_pcm_iec958_get(struct snd_kcontrol *kcontrol, 221*187e01d0Solivier moysan struct snd_ctl_elem_value *uctl) 222*187e01d0Solivier moysan { 223*187e01d0Solivier moysan struct stm32_sai_sub_data *sai = snd_kcontrol_chip(kcontrol); 224*187e01d0Solivier moysan 225*187e01d0Solivier moysan mutex_lock(&sai->ctrl_lock); 226*187e01d0Solivier moysan memcpy(uctl->value.iec958.status, sai->iec958.status, 4); 227*187e01d0Solivier moysan mutex_unlock(&sai->ctrl_lock); 228*187e01d0Solivier moysan 229*187e01d0Solivier moysan return 0; 230*187e01d0Solivier moysan } 231*187e01d0Solivier moysan 232*187e01d0Solivier moysan static int snd_pcm_iec958_put(struct snd_kcontrol *kcontrol, 233*187e01d0Solivier moysan struct snd_ctl_elem_value *uctl) 234*187e01d0Solivier moysan { 235*187e01d0Solivier moysan struct stm32_sai_sub_data *sai = snd_kcontrol_chip(kcontrol); 236*187e01d0Solivier moysan 237*187e01d0Solivier moysan mutex_lock(&sai->ctrl_lock); 238*187e01d0Solivier moysan memcpy(sai->iec958.status, uctl->value.iec958.status, 4); 239*187e01d0Solivier moysan mutex_unlock(&sai->ctrl_lock); 240*187e01d0Solivier moysan 241*187e01d0Solivier moysan return 0; 242*187e01d0Solivier moysan } 243*187e01d0Solivier moysan 244*187e01d0Solivier moysan static const struct snd_kcontrol_new iec958_ctls = { 245*187e01d0Solivier moysan .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE | 246*187e01d0Solivier moysan SNDRV_CTL_ELEM_ACCESS_VOLATILE), 247*187e01d0Solivier moysan .iface = SNDRV_CTL_ELEM_IFACE_PCM, 248*187e01d0Solivier moysan .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), 249*187e01d0Solivier moysan .info = snd_pcm_iec958_info, 250*187e01d0Solivier moysan .get = snd_pcm_iec958_get, 251*187e01d0Solivier moysan .put = snd_pcm_iec958_put, 252*187e01d0Solivier moysan }; 253*187e01d0Solivier moysan 2543e086edfSolivier moysan static irqreturn_t stm32_sai_isr(int irq, void *devid) 2553e086edfSolivier moysan { 2563e086edfSolivier moysan struct stm32_sai_sub_data *sai = (struct stm32_sai_sub_data *)devid; 2573e086edfSolivier moysan struct platform_device *pdev = sai->pdev; 2583e086edfSolivier moysan unsigned int sr, imr, flags; 2593e086edfSolivier moysan snd_pcm_state_t status = SNDRV_PCM_STATE_RUNNING; 2603e086edfSolivier moysan 2613e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_IMR_REGX, &imr); 2623e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_SR_REGX, &sr); 2633e086edfSolivier moysan 2643e086edfSolivier moysan flags = sr & imr; 2653e086edfSolivier moysan if (!flags) 2663e086edfSolivier moysan return IRQ_NONE; 2673e086edfSolivier moysan 2683e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX, SAI_XCLRFR_MASK, 2693e086edfSolivier moysan SAI_XCLRFR_MASK); 2703e086edfSolivier moysan 271d807cdfbSOlivier Moysan if (!sai->substream) { 272d807cdfbSOlivier Moysan dev_err(&pdev->dev, "Device stopped. Spurious IRQ 0x%x\n", sr); 273d807cdfbSOlivier Moysan return IRQ_NONE; 274d807cdfbSOlivier Moysan } 275d807cdfbSOlivier Moysan 2763e086edfSolivier moysan if (flags & SAI_XIMR_OVRUDRIE) { 277602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ %s\n", 2783e086edfSolivier moysan STM_SAI_IS_PLAYBACK(sai) ? "underrun" : "overrun"); 2793e086edfSolivier moysan status = SNDRV_PCM_STATE_XRUN; 2803e086edfSolivier moysan } 2813e086edfSolivier moysan 2823e086edfSolivier moysan if (flags & SAI_XIMR_MUTEDETIE) 283602fdadcSolivier moysan dev_dbg(&pdev->dev, "IRQ mute detected\n"); 2843e086edfSolivier moysan 2853e086edfSolivier moysan if (flags & SAI_XIMR_WCKCFGIE) { 286602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ wrong clock configuration\n"); 2873e086edfSolivier moysan status = SNDRV_PCM_STATE_DISCONNECTED; 2883e086edfSolivier moysan } 2893e086edfSolivier moysan 2903e086edfSolivier moysan if (flags & SAI_XIMR_CNRDYIE) 291602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ Codec not ready\n"); 2923e086edfSolivier moysan 2933e086edfSolivier moysan if (flags & SAI_XIMR_AFSDETIE) { 294602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ Anticipated frame synchro\n"); 2953e086edfSolivier moysan status = SNDRV_PCM_STATE_XRUN; 2963e086edfSolivier moysan } 2973e086edfSolivier moysan 2983e086edfSolivier moysan if (flags & SAI_XIMR_LFSDETIE) { 299602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ Late frame synchro\n"); 3003e086edfSolivier moysan status = SNDRV_PCM_STATE_XRUN; 3013e086edfSolivier moysan } 3023e086edfSolivier moysan 3033e086edfSolivier moysan if (status != SNDRV_PCM_STATE_RUNNING) { 304d807cdfbSOlivier Moysan snd_pcm_stream_lock(sai->substream); 305d807cdfbSOlivier Moysan snd_pcm_stop(sai->substream, SNDRV_PCM_STATE_XRUN); 306d807cdfbSOlivier Moysan snd_pcm_stream_unlock(sai->substream); 3073e086edfSolivier moysan } 3083e086edfSolivier moysan 3093e086edfSolivier moysan return IRQ_HANDLED; 3103e086edfSolivier moysan } 3113e086edfSolivier moysan 3123e086edfSolivier moysan static int stm32_sai_set_sysclk(struct snd_soc_dai *cpu_dai, 3133e086edfSolivier moysan int clk_id, unsigned int freq, int dir) 3143e086edfSolivier moysan { 3153e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 316701a6ec3Solivier moysan int ret; 3173e086edfSolivier moysan 3183e086edfSolivier moysan if ((dir == SND_SOC_CLOCK_OUT) && sai->master) { 319701a6ec3Solivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 320701a6ec3Solivier moysan SAI_XCR1_NODIV, 321701a6ec3Solivier moysan (unsigned int)~SAI_XCR1_NODIV); 322701a6ec3Solivier moysan if (ret < 0) 323701a6ec3Solivier moysan return ret; 324701a6ec3Solivier moysan 3253e086edfSolivier moysan sai->mclk_rate = freq; 3263e086edfSolivier moysan dev_dbg(cpu_dai->dev, "SAI MCLK frequency is %uHz\n", freq); 3273e086edfSolivier moysan } 3283e086edfSolivier moysan 3293e086edfSolivier moysan return 0; 3303e086edfSolivier moysan } 3313e086edfSolivier moysan 3323e086edfSolivier moysan static int stm32_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask, 3333e086edfSolivier moysan u32 rx_mask, int slots, int slot_width) 3343e086edfSolivier moysan { 3353e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 3363e086edfSolivier moysan int slotr, slotr_mask, slot_size; 3373e086edfSolivier moysan 3386eb17d70SOlivier Moysan if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) { 3396eb17d70SOlivier Moysan dev_warn(cpu_dai->dev, "Slot setting relevant only for TDM\n"); 3406eb17d70SOlivier Moysan return 0; 3416eb17d70SOlivier Moysan } 3426eb17d70SOlivier Moysan 343602fdadcSolivier moysan dev_dbg(cpu_dai->dev, "Masks tx/rx:%#x/%#x, slots:%d, width:%d\n", 3443e086edfSolivier moysan tx_mask, rx_mask, slots, slot_width); 3453e086edfSolivier moysan 3463e086edfSolivier moysan switch (slot_width) { 3473e086edfSolivier moysan case 16: 3483e086edfSolivier moysan slot_size = SAI_SLOT_SIZE_16; 3493e086edfSolivier moysan break; 3503e086edfSolivier moysan case 32: 3513e086edfSolivier moysan slot_size = SAI_SLOT_SIZE_32; 3523e086edfSolivier moysan break; 3533e086edfSolivier moysan default: 3543e086edfSolivier moysan slot_size = SAI_SLOT_SIZE_AUTO; 3553e086edfSolivier moysan break; 3563e086edfSolivier moysan } 3573e086edfSolivier moysan 3583e086edfSolivier moysan slotr = SAI_XSLOTR_SLOTSZ_SET(slot_size) | 3593e086edfSolivier moysan SAI_XSLOTR_NBSLOT_SET(slots - 1); 3603e086edfSolivier moysan slotr_mask = SAI_XSLOTR_SLOTSZ_MASK | SAI_XSLOTR_NBSLOT_MASK; 3613e086edfSolivier moysan 3623e086edfSolivier moysan /* tx/rx mask set in machine init, if slot number defined in DT */ 3633e086edfSolivier moysan if (STM_SAI_IS_PLAYBACK(sai)) { 3643e086edfSolivier moysan sai->slot_mask = tx_mask; 3653e086edfSolivier moysan slotr |= SAI_XSLOTR_SLOTEN_SET(tx_mask); 3663e086edfSolivier moysan } 3673e086edfSolivier moysan 3683e086edfSolivier moysan if (STM_SAI_IS_CAPTURE(sai)) { 3693e086edfSolivier moysan sai->slot_mask = rx_mask; 3703e086edfSolivier moysan slotr |= SAI_XSLOTR_SLOTEN_SET(rx_mask); 3713e086edfSolivier moysan } 3723e086edfSolivier moysan 3733e086edfSolivier moysan slotr_mask |= SAI_XSLOTR_SLOTEN_MASK; 3743e086edfSolivier moysan 3753e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, slotr_mask, slotr); 3763e086edfSolivier moysan 3773e086edfSolivier moysan sai->slot_width = slot_width; 3783e086edfSolivier moysan sai->slots = slots; 3793e086edfSolivier moysan 3803e086edfSolivier moysan return 0; 3813e086edfSolivier moysan } 3823e086edfSolivier moysan 3833e086edfSolivier moysan static int stm32_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt) 3843e086edfSolivier moysan { 3853e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 38661fb4ff7SOlivier Moysan int cr1, frcr = 0; 38761fb4ff7SOlivier Moysan int cr1_mask, frcr_mask = 0; 3883e086edfSolivier moysan int ret; 3893e086edfSolivier moysan 3903e086edfSolivier moysan dev_dbg(cpu_dai->dev, "fmt %x\n", fmt); 3913e086edfSolivier moysan 3926eb17d70SOlivier Moysan /* Do not generate master by default */ 3936eb17d70SOlivier Moysan cr1 = SAI_XCR1_NODIV; 3946eb17d70SOlivier Moysan cr1_mask = SAI_XCR1_NODIV; 3956eb17d70SOlivier Moysan 3966eb17d70SOlivier Moysan cr1_mask |= SAI_XCR1_PRTCFG_MASK; 3976eb17d70SOlivier Moysan if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) { 3986eb17d70SOlivier Moysan cr1 |= SAI_XCR1_PRTCFG_SET(SAI_SPDIF_PROTOCOL); 3996eb17d70SOlivier Moysan goto conf_update; 4006eb17d70SOlivier Moysan } 4016eb17d70SOlivier Moysan 4026eb17d70SOlivier Moysan cr1 |= SAI_XCR1_PRTCFG_SET(SAI_FREE_PROTOCOL); 40361fb4ff7SOlivier Moysan 4043e086edfSolivier moysan switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 4053e086edfSolivier moysan /* SCK active high for all protocols */ 4063e086edfSolivier moysan case SND_SOC_DAIFMT_I2S: 4073e086edfSolivier moysan cr1 |= SAI_XCR1_CKSTR; 4083e086edfSolivier moysan frcr |= SAI_XFRCR_FSOFF | SAI_XFRCR_FSDEF; 4093e086edfSolivier moysan break; 4103e086edfSolivier moysan /* Left justified */ 4113e086edfSolivier moysan case SND_SOC_DAIFMT_MSB: 4123e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF; 4133e086edfSolivier moysan break; 4143e086edfSolivier moysan /* Right justified */ 4153e086edfSolivier moysan case SND_SOC_DAIFMT_LSB: 4163e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF; 4173e086edfSolivier moysan break; 4183e086edfSolivier moysan case SND_SOC_DAIFMT_DSP_A: 4193e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF; 4203e086edfSolivier moysan break; 4213e086edfSolivier moysan case SND_SOC_DAIFMT_DSP_B: 4223e086edfSolivier moysan frcr |= SAI_XFRCR_FSPOL; 4233e086edfSolivier moysan break; 4243e086edfSolivier moysan default: 4253e086edfSolivier moysan dev_err(cpu_dai->dev, "Unsupported protocol %#x\n", 4263e086edfSolivier moysan fmt & SND_SOC_DAIFMT_FORMAT_MASK); 4273e086edfSolivier moysan return -EINVAL; 4283e086edfSolivier moysan } 4293e086edfSolivier moysan 43061fb4ff7SOlivier Moysan cr1_mask |= SAI_XCR1_CKSTR; 4313e086edfSolivier moysan frcr_mask |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF | 4323e086edfSolivier moysan SAI_XFRCR_FSDEF; 4333e086edfSolivier moysan 4343e086edfSolivier moysan /* DAI clock strobing. Invert setting previously set */ 4353e086edfSolivier moysan switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 4363e086edfSolivier moysan case SND_SOC_DAIFMT_NB_NF: 4373e086edfSolivier moysan break; 4383e086edfSolivier moysan case SND_SOC_DAIFMT_IB_NF: 4393e086edfSolivier moysan cr1 ^= SAI_XCR1_CKSTR; 4403e086edfSolivier moysan break; 4413e086edfSolivier moysan case SND_SOC_DAIFMT_NB_IF: 4423e086edfSolivier moysan frcr ^= SAI_XFRCR_FSPOL; 4433e086edfSolivier moysan break; 4443e086edfSolivier moysan case SND_SOC_DAIFMT_IB_IF: 4453e086edfSolivier moysan /* Invert fs & sck */ 4463e086edfSolivier moysan cr1 ^= SAI_XCR1_CKSTR; 4473e086edfSolivier moysan frcr ^= SAI_XFRCR_FSPOL; 4483e086edfSolivier moysan break; 4493e086edfSolivier moysan default: 4503e086edfSolivier moysan dev_err(cpu_dai->dev, "Unsupported strobing %#x\n", 4513e086edfSolivier moysan fmt & SND_SOC_DAIFMT_INV_MASK); 4523e086edfSolivier moysan return -EINVAL; 4533e086edfSolivier moysan } 4543e086edfSolivier moysan cr1_mask |= SAI_XCR1_CKSTR; 4553e086edfSolivier moysan frcr_mask |= SAI_XFRCR_FSPOL; 4563e086edfSolivier moysan 4573e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr); 4583e086edfSolivier moysan 4593e086edfSolivier moysan /* DAI clock master masks */ 4603e086edfSolivier moysan switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 4613e086edfSolivier moysan case SND_SOC_DAIFMT_CBM_CFM: 4623e086edfSolivier moysan /* codec is master */ 4633e086edfSolivier moysan cr1 |= SAI_XCR1_SLAVE; 4643e086edfSolivier moysan sai->master = false; 4653e086edfSolivier moysan break; 4663e086edfSolivier moysan case SND_SOC_DAIFMT_CBS_CFS: 4673e086edfSolivier moysan sai->master = true; 4683e086edfSolivier moysan break; 4693e086edfSolivier moysan default: 4703e086edfSolivier moysan dev_err(cpu_dai->dev, "Unsupported mode %#x\n", 4713e086edfSolivier moysan fmt & SND_SOC_DAIFMT_MASTER_MASK); 4723e086edfSolivier moysan return -EINVAL; 4733e086edfSolivier moysan } 4745914d285SOlivier Moysan 4755914d285SOlivier Moysan /* Set slave mode if sub-block is synchronized with another SAI */ 4765914d285SOlivier Moysan if (sai->sync) { 4775914d285SOlivier Moysan dev_dbg(cpu_dai->dev, "Synchronized SAI configured as slave\n"); 4785914d285SOlivier Moysan cr1 |= SAI_XCR1_SLAVE; 4795914d285SOlivier Moysan sai->master = false; 4805914d285SOlivier Moysan } 4815914d285SOlivier Moysan 4823e086edfSolivier moysan cr1_mask |= SAI_XCR1_SLAVE; 4833e086edfSolivier moysan 4846eb17d70SOlivier Moysan conf_update: 4853e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1); 4863e086edfSolivier moysan if (ret < 0) { 4873e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 4883e086edfSolivier moysan return ret; 4893e086edfSolivier moysan } 4903e086edfSolivier moysan 4913e086edfSolivier moysan sai->fmt = fmt; 4923e086edfSolivier moysan 4933e086edfSolivier moysan return 0; 4943e086edfSolivier moysan } 4953e086edfSolivier moysan 4963e086edfSolivier moysan static int stm32_sai_startup(struct snd_pcm_substream *substream, 4973e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 4983e086edfSolivier moysan { 4993e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 5003e086edfSolivier moysan int imr, cr2, ret; 5013e086edfSolivier moysan 5023e086edfSolivier moysan sai->substream = substream; 5033e086edfSolivier moysan 5043e086edfSolivier moysan ret = clk_prepare_enable(sai->sai_ck); 5053e086edfSolivier moysan if (ret < 0) { 506602fdadcSolivier moysan dev_err(cpu_dai->dev, "Failed to enable clock: %d\n", ret); 5073e086edfSolivier moysan return ret; 5083e086edfSolivier moysan } 5093e086edfSolivier moysan 5103e086edfSolivier moysan /* Enable ITs */ 5113e086edfSolivier moysan 5123e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX, 5133e086edfSolivier moysan SAI_XCLRFR_MASK, SAI_XCLRFR_MASK); 5143e086edfSolivier moysan 5153e086edfSolivier moysan imr = SAI_XIMR_OVRUDRIE; 5163e086edfSolivier moysan if (STM_SAI_IS_CAPTURE(sai)) { 5173e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_CR2_REGX, &cr2); 5183e086edfSolivier moysan if (cr2 & SAI_XCR2_MUTECNT_MASK) 5193e086edfSolivier moysan imr |= SAI_XIMR_MUTEDETIE; 5203e086edfSolivier moysan } 5213e086edfSolivier moysan 5223e086edfSolivier moysan if (sai->master) 5233e086edfSolivier moysan imr |= SAI_XIMR_WCKCFGIE; 5243e086edfSolivier moysan else 5253e086edfSolivier moysan imr |= SAI_XIMR_AFSDETIE | SAI_XIMR_LFSDETIE; 5263e086edfSolivier moysan 5273e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, 5283e086edfSolivier moysan SAI_XIMR_MASK, imr); 5293e086edfSolivier moysan 5303e086edfSolivier moysan return 0; 5313e086edfSolivier moysan } 5323e086edfSolivier moysan 5333e086edfSolivier moysan static int stm32_sai_set_config(struct snd_soc_dai *cpu_dai, 5343e086edfSolivier moysan struct snd_pcm_substream *substream, 5353e086edfSolivier moysan struct snd_pcm_hw_params *params) 5363e086edfSolivier moysan { 5373e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 5383e086edfSolivier moysan int cr1, cr1_mask, ret; 5393e086edfSolivier moysan 540a4529d2bSOlivier Moysan /* 541a4529d2bSOlivier Moysan * DMA bursts increment is set to 4 words. 542a4529d2bSOlivier Moysan * SAI fifo threshold is set to half fifo, to keep enough space 543a4529d2bSOlivier Moysan * for DMA incoming bursts. 544a4529d2bSOlivier Moysan */ 5453e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR2_REGX, 5463e086edfSolivier moysan SAI_XCR2_FFLUSH | SAI_XCR2_FTH_MASK, 547a4529d2bSOlivier Moysan SAI_XCR2_FFLUSH | 548a4529d2bSOlivier Moysan SAI_XCR2_FTH_SET(STM_SAI_FIFO_TH_HALF)); 5493e086edfSolivier moysan 5506eb17d70SOlivier Moysan /* DS bits in CR1 not set for SPDIF (size forced to 24 bits).*/ 5516eb17d70SOlivier Moysan if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) { 5526eb17d70SOlivier Moysan sai->spdif_frm_cnt = 0; 5536eb17d70SOlivier Moysan return 0; 5546eb17d70SOlivier Moysan } 5556eb17d70SOlivier Moysan 5563e086edfSolivier moysan /* Mode, data format and channel config */ 55761fb4ff7SOlivier Moysan cr1_mask = SAI_XCR1_DS_MASK; 5583e086edfSolivier moysan switch (params_format(params)) { 5593e086edfSolivier moysan case SNDRV_PCM_FORMAT_S8: 5607e751e37Solivier moysan cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_8); 5613e086edfSolivier moysan break; 5623e086edfSolivier moysan case SNDRV_PCM_FORMAT_S16_LE: 5637e751e37Solivier moysan cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_16); 5643e086edfSolivier moysan break; 5653e086edfSolivier moysan case SNDRV_PCM_FORMAT_S32_LE: 5667e751e37Solivier moysan cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_32); 5673e086edfSolivier moysan break; 5683e086edfSolivier moysan default: 5693e086edfSolivier moysan dev_err(cpu_dai->dev, "Data format not supported"); 5703e086edfSolivier moysan return -EINVAL; 5713e086edfSolivier moysan } 5723e086edfSolivier moysan 5733e086edfSolivier moysan cr1_mask |= SAI_XCR1_MONO; 5743e086edfSolivier moysan if ((sai->slots == 2) && (params_channels(params) == 1)) 5753e086edfSolivier moysan cr1 |= SAI_XCR1_MONO; 5763e086edfSolivier moysan 5773e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1); 5783e086edfSolivier moysan if (ret < 0) { 5793e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 5803e086edfSolivier moysan return ret; 5813e086edfSolivier moysan } 5823e086edfSolivier moysan 5833e086edfSolivier moysan return 0; 5843e086edfSolivier moysan } 5853e086edfSolivier moysan 5863e086edfSolivier moysan static int stm32_sai_set_slots(struct snd_soc_dai *cpu_dai) 5873e086edfSolivier moysan { 5883e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 5893e086edfSolivier moysan int slotr, slot_sz; 5903e086edfSolivier moysan 5913e086edfSolivier moysan regmap_read(sai->regmap, STM_SAI_SLOTR_REGX, &slotr); 5923e086edfSolivier moysan 5933e086edfSolivier moysan /* 5943e086edfSolivier moysan * If SLOTSZ is set to auto in SLOTR, align slot width on data size 5953e086edfSolivier moysan * By default slot width = data size, if not forced from DT 5963e086edfSolivier moysan */ 5973e086edfSolivier moysan slot_sz = slotr & SAI_XSLOTR_SLOTSZ_MASK; 5983e086edfSolivier moysan if (slot_sz == SAI_XSLOTR_SLOTSZ_SET(SAI_SLOT_SIZE_AUTO)) 5993e086edfSolivier moysan sai->slot_width = sai->data_size; 6003e086edfSolivier moysan 6013e086edfSolivier moysan if (sai->slot_width < sai->data_size) { 6023e086edfSolivier moysan dev_err(cpu_dai->dev, 6033e086edfSolivier moysan "Data size %d larger than slot width\n", 6043e086edfSolivier moysan sai->data_size); 6053e086edfSolivier moysan return -EINVAL; 6063e086edfSolivier moysan } 6073e086edfSolivier moysan 6083e086edfSolivier moysan /* Slot number is set to 2, if not specified in DT */ 6093e086edfSolivier moysan if (!sai->slots) 6103e086edfSolivier moysan sai->slots = 2; 6113e086edfSolivier moysan 6123e086edfSolivier moysan /* The number of slots in the audio frame is equal to NBSLOT[3:0] + 1*/ 6133e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, 6143e086edfSolivier moysan SAI_XSLOTR_NBSLOT_MASK, 6153e086edfSolivier moysan SAI_XSLOTR_NBSLOT_SET((sai->slots - 1))); 6163e086edfSolivier moysan 6173e086edfSolivier moysan /* Set default slots mask if not already set from DT */ 6183e086edfSolivier moysan if (!(slotr & SAI_XSLOTR_SLOTEN_MASK)) { 6193e086edfSolivier moysan sai->slot_mask = (1 << sai->slots) - 1; 6203e086edfSolivier moysan regmap_update_bits(sai->regmap, 6213e086edfSolivier moysan STM_SAI_SLOTR_REGX, SAI_XSLOTR_SLOTEN_MASK, 6223e086edfSolivier moysan SAI_XSLOTR_SLOTEN_SET(sai->slot_mask)); 6233e086edfSolivier moysan } 6243e086edfSolivier moysan 625602fdadcSolivier moysan dev_dbg(cpu_dai->dev, "Slots %d, slot width %d\n", 6263e086edfSolivier moysan sai->slots, sai->slot_width); 6273e086edfSolivier moysan 6283e086edfSolivier moysan return 0; 6293e086edfSolivier moysan } 6303e086edfSolivier moysan 6313e086edfSolivier moysan static void stm32_sai_set_frame(struct snd_soc_dai *cpu_dai) 6323e086edfSolivier moysan { 6333e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 6343e086edfSolivier moysan int fs_active, offset, format; 6353e086edfSolivier moysan int frcr, frcr_mask; 6363e086edfSolivier moysan 6373e086edfSolivier moysan format = sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK; 6383e086edfSolivier moysan sai->fs_length = sai->slot_width * sai->slots; 6393e086edfSolivier moysan 6403e086edfSolivier moysan fs_active = sai->fs_length / 2; 6413e086edfSolivier moysan if ((format == SND_SOC_DAIFMT_DSP_A) || 6423e086edfSolivier moysan (format == SND_SOC_DAIFMT_DSP_B)) 6433e086edfSolivier moysan fs_active = 1; 6443e086edfSolivier moysan 6453e086edfSolivier moysan frcr = SAI_XFRCR_FRL_SET((sai->fs_length - 1)); 6463e086edfSolivier moysan frcr |= SAI_XFRCR_FSALL_SET((fs_active - 1)); 6473e086edfSolivier moysan frcr_mask = SAI_XFRCR_FRL_MASK | SAI_XFRCR_FSALL_MASK; 6483e086edfSolivier moysan 649602fdadcSolivier moysan dev_dbg(cpu_dai->dev, "Frame length %d, frame active %d\n", 6503e086edfSolivier moysan sai->fs_length, fs_active); 6513e086edfSolivier moysan 6523e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr); 6533e086edfSolivier moysan 6543e086edfSolivier moysan if ((sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_LSB) { 6553e086edfSolivier moysan offset = sai->slot_width - sai->data_size; 6563e086edfSolivier moysan 6573e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, 6583e086edfSolivier moysan SAI_XSLOTR_FBOFF_MASK, 6593e086edfSolivier moysan SAI_XSLOTR_FBOFF_SET(offset)); 6603e086edfSolivier moysan } 6613e086edfSolivier moysan } 6623e086edfSolivier moysan 663*187e01d0Solivier moysan static void stm32_sai_init_iec958_status(struct stm32_sai_sub_data *sai) 664*187e01d0Solivier moysan { 665*187e01d0Solivier moysan unsigned char *cs = sai->iec958.status; 666*187e01d0Solivier moysan 667*187e01d0Solivier moysan cs[0] = IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS_NONE; 668*187e01d0Solivier moysan cs[1] = IEC958_AES1_CON_GENERAL; 669*187e01d0Solivier moysan cs[2] = IEC958_AES2_CON_SOURCE_UNSPEC | IEC958_AES2_CON_CHANNEL_UNSPEC; 670*187e01d0Solivier moysan cs[3] = IEC958_AES3_CON_CLOCK_1000PPM | IEC958_AES3_CON_FS_NOTID; 671*187e01d0Solivier moysan } 672*187e01d0Solivier moysan 673*187e01d0Solivier moysan static void stm32_sai_set_iec958_status(struct stm32_sai_sub_data *sai, 674*187e01d0Solivier moysan struct snd_pcm_runtime *runtime) 675*187e01d0Solivier moysan { 676*187e01d0Solivier moysan if (!runtime) 677*187e01d0Solivier moysan return; 678*187e01d0Solivier moysan 679*187e01d0Solivier moysan /* Force the sample rate according to runtime rate */ 680*187e01d0Solivier moysan mutex_lock(&sai->ctrl_lock); 681*187e01d0Solivier moysan switch (runtime->rate) { 682*187e01d0Solivier moysan case 22050: 683*187e01d0Solivier moysan sai->iec958.status[3] = IEC958_AES3_CON_FS_22050; 684*187e01d0Solivier moysan break; 685*187e01d0Solivier moysan case 44100: 686*187e01d0Solivier moysan sai->iec958.status[3] = IEC958_AES3_CON_FS_44100; 687*187e01d0Solivier moysan break; 688*187e01d0Solivier moysan case 88200: 689*187e01d0Solivier moysan sai->iec958.status[3] = IEC958_AES3_CON_FS_88200; 690*187e01d0Solivier moysan break; 691*187e01d0Solivier moysan case 176400: 692*187e01d0Solivier moysan sai->iec958.status[3] = IEC958_AES3_CON_FS_176400; 693*187e01d0Solivier moysan break; 694*187e01d0Solivier moysan case 24000: 695*187e01d0Solivier moysan sai->iec958.status[3] = IEC958_AES3_CON_FS_24000; 696*187e01d0Solivier moysan break; 697*187e01d0Solivier moysan case 48000: 698*187e01d0Solivier moysan sai->iec958.status[3] = IEC958_AES3_CON_FS_48000; 699*187e01d0Solivier moysan break; 700*187e01d0Solivier moysan case 96000: 701*187e01d0Solivier moysan sai->iec958.status[3] = IEC958_AES3_CON_FS_96000; 702*187e01d0Solivier moysan break; 703*187e01d0Solivier moysan case 192000: 704*187e01d0Solivier moysan sai->iec958.status[3] = IEC958_AES3_CON_FS_192000; 705*187e01d0Solivier moysan break; 706*187e01d0Solivier moysan case 32000: 707*187e01d0Solivier moysan sai->iec958.status[3] = IEC958_AES3_CON_FS_32000; 708*187e01d0Solivier moysan break; 709*187e01d0Solivier moysan default: 710*187e01d0Solivier moysan sai->iec958.status[3] = IEC958_AES3_CON_FS_NOTID; 711*187e01d0Solivier moysan break; 712*187e01d0Solivier moysan } 713*187e01d0Solivier moysan mutex_unlock(&sai->ctrl_lock); 714*187e01d0Solivier moysan } 715*187e01d0Solivier moysan 7163e086edfSolivier moysan static int stm32_sai_configure_clock(struct snd_soc_dai *cpu_dai, 7173e086edfSolivier moysan struct snd_pcm_hw_params *params) 7183e086edfSolivier moysan { 7193e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 7203e086edfSolivier moysan int cr1, mask, div = 0; 72103e78a24Solivier moysan int sai_clk_rate, mclk_ratio, den, ret; 72203e78a24Solivier moysan int version = sai->pdata->conf->version; 7236eb17d70SOlivier Moysan unsigned int rate = params_rate(params); 7243e086edfSolivier moysan 7253e086edfSolivier moysan if (!sai->mclk_rate) { 7263e086edfSolivier moysan dev_err(cpu_dai->dev, "Mclk rate is null\n"); 7273e086edfSolivier moysan return -EINVAL; 7283e086edfSolivier moysan } 7293e086edfSolivier moysan 7306eb17d70SOlivier Moysan if (!(rate % 11025)) 7313e086edfSolivier moysan clk_set_parent(sai->sai_ck, sai->pdata->clk_x11k); 7323e086edfSolivier moysan else 7333e086edfSolivier moysan clk_set_parent(sai->sai_ck, sai->pdata->clk_x8k); 7343e086edfSolivier moysan sai_clk_rate = clk_get_rate(sai->sai_ck); 7353e086edfSolivier moysan 73603e78a24Solivier moysan if (STM_SAI_IS_F4(sai->pdata)) { 7373e086edfSolivier moysan /* 7383e086edfSolivier moysan * mclk_rate = 256 * fs 7393e086edfSolivier moysan * MCKDIV = 0 if sai_ck < 3/2 * mclk_rate 7403e086edfSolivier moysan * MCKDIV = sai_ck / (2 * mclk_rate) otherwise 7413e086edfSolivier moysan */ 7423e086edfSolivier moysan if (2 * sai_clk_rate >= 3 * sai->mclk_rate) 74303e78a24Solivier moysan div = DIV_ROUND_CLOSEST(sai_clk_rate, 74403e78a24Solivier moysan 2 * sai->mclk_rate); 74503e78a24Solivier moysan } else { 74603e78a24Solivier moysan /* 74703e78a24Solivier moysan * TDM mode : 74803e78a24Solivier moysan * mclk on 74903e78a24Solivier moysan * MCKDIV = sai_ck / (ws x 256) (NOMCK=0. OSR=0) 75003e78a24Solivier moysan * MCKDIV = sai_ck / (ws x 512) (NOMCK=0. OSR=1) 75103e78a24Solivier moysan * mclk off 75203e78a24Solivier moysan * MCKDIV = sai_ck / (frl x ws) (NOMCK=1) 75303e78a24Solivier moysan * Note: NOMCK/NODIV correspond to same bit. 75403e78a24Solivier moysan */ 7556eb17d70SOlivier Moysan if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) { 7566eb17d70SOlivier Moysan div = DIV_ROUND_CLOSEST(sai_clk_rate, 7576eb17d70SOlivier Moysan (params_rate(params) * 128)); 7586eb17d70SOlivier Moysan } else { 75903e78a24Solivier moysan if (sai->mclk_rate) { 7606eb17d70SOlivier Moysan mclk_ratio = sai->mclk_rate / rate; 76103e78a24Solivier moysan if (mclk_ratio == 512) { 76203e78a24Solivier moysan mask = SAI_XCR1_OSR; 76303e78a24Solivier moysan cr1 = SAI_XCR1_OSR; 7646eb17d70SOlivier Moysan } else if (mclk_ratio != 256) { 76503e78a24Solivier moysan dev_err(cpu_dai->dev, 76603e78a24Solivier moysan "Wrong mclk ratio %d\n", 76703e78a24Solivier moysan mclk_ratio); 76803e78a24Solivier moysan return -EINVAL; 76903e78a24Solivier moysan } 7706eb17d70SOlivier Moysan div = DIV_ROUND_CLOSEST(sai_clk_rate, 7716eb17d70SOlivier Moysan sai->mclk_rate); 77203e78a24Solivier moysan } else { 7736eb17d70SOlivier Moysan /* mclk-fs not set, master clock not active */ 77403e78a24Solivier moysan den = sai->fs_length * params_rate(params); 77503e78a24Solivier moysan div = DIV_ROUND_CLOSEST(sai_clk_rate, den); 77603e78a24Solivier moysan } 77703e78a24Solivier moysan } 7786eb17d70SOlivier Moysan } 7793e086edfSolivier moysan 78003e78a24Solivier moysan if (div > SAI_XCR1_MCKDIV_MAX(version)) { 7813e086edfSolivier moysan dev_err(cpu_dai->dev, "Divider %d out of range\n", div); 7823e086edfSolivier moysan return -EINVAL; 7833e086edfSolivier moysan } 7843e086edfSolivier moysan dev_dbg(cpu_dai->dev, "SAI clock %d, divider %d\n", sai_clk_rate, div); 7853e086edfSolivier moysan 78603e78a24Solivier moysan mask = SAI_XCR1_MCKDIV_MASK(SAI_XCR1_MCKDIV_WIDTH(version)); 7873e086edfSolivier moysan cr1 = SAI_XCR1_MCKDIV_SET(div); 7883e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, mask, cr1); 7893e086edfSolivier moysan if (ret < 0) { 7903e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 7913e086edfSolivier moysan return ret; 7923e086edfSolivier moysan } 7933e086edfSolivier moysan 7943e086edfSolivier moysan return 0; 7953e086edfSolivier moysan } 7963e086edfSolivier moysan 7973e086edfSolivier moysan static int stm32_sai_hw_params(struct snd_pcm_substream *substream, 7983e086edfSolivier moysan struct snd_pcm_hw_params *params, 7993e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 8003e086edfSolivier moysan { 8013e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 8023e086edfSolivier moysan int ret; 8033e086edfSolivier moysan 8043e086edfSolivier moysan sai->data_size = params_width(params); 8053e086edfSolivier moysan 806*187e01d0Solivier moysan if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) { 807*187e01d0Solivier moysan /* Rate not already set in runtime structure */ 808*187e01d0Solivier moysan substream->runtime->rate = params_rate(params); 809*187e01d0Solivier moysan stm32_sai_set_iec958_status(sai, substream->runtime); 810*187e01d0Solivier moysan } else { 8113e086edfSolivier moysan ret = stm32_sai_set_slots(cpu_dai); 8123e086edfSolivier moysan if (ret < 0) 8133e086edfSolivier moysan return ret; 8143e086edfSolivier moysan stm32_sai_set_frame(cpu_dai); 8156eb17d70SOlivier Moysan } 8163e086edfSolivier moysan 8173e086edfSolivier moysan ret = stm32_sai_set_config(cpu_dai, substream, params); 8183e086edfSolivier moysan if (ret) 8193e086edfSolivier moysan return ret; 8203e086edfSolivier moysan 8213e086edfSolivier moysan if (sai->master) 8223e086edfSolivier moysan ret = stm32_sai_configure_clock(cpu_dai, params); 8233e086edfSolivier moysan 8243e086edfSolivier moysan return ret; 8253e086edfSolivier moysan } 8263e086edfSolivier moysan 8273e086edfSolivier moysan static int stm32_sai_trigger(struct snd_pcm_substream *substream, int cmd, 8283e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 8293e086edfSolivier moysan { 8303e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 8313e086edfSolivier moysan int ret; 8323e086edfSolivier moysan 8333e086edfSolivier moysan switch (cmd) { 8343e086edfSolivier moysan case SNDRV_PCM_TRIGGER_START: 8353e086edfSolivier moysan case SNDRV_PCM_TRIGGER_RESUME: 8363e086edfSolivier moysan case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 8373e086edfSolivier moysan dev_dbg(cpu_dai->dev, "Enable DMA and SAI\n"); 8383e086edfSolivier moysan 8393e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 8403e086edfSolivier moysan SAI_XCR1_DMAEN, SAI_XCR1_DMAEN); 8413e086edfSolivier moysan 8423e086edfSolivier moysan /* Enable SAI */ 8433e086edfSolivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 8443e086edfSolivier moysan SAI_XCR1_SAIEN, SAI_XCR1_SAIEN); 8453e086edfSolivier moysan if (ret < 0) 8463e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 8473e086edfSolivier moysan break; 8483e086edfSolivier moysan case SNDRV_PCM_TRIGGER_SUSPEND: 8493e086edfSolivier moysan case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 8503e086edfSolivier moysan case SNDRV_PCM_TRIGGER_STOP: 8513e086edfSolivier moysan dev_dbg(cpu_dai->dev, "Disable DMA and SAI\n"); 8523e086edfSolivier moysan 85347a8907dSOlivier Moysan regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, 85447a8907dSOlivier Moysan SAI_XIMR_MASK, 0); 85547a8907dSOlivier Moysan 8563e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 8573e086edfSolivier moysan SAI_XCR1_SAIEN, 8583e086edfSolivier moysan (unsigned int)~SAI_XCR1_SAIEN); 8594fa17938Solivier moysan 8604fa17938Solivier moysan ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, 8614fa17938Solivier moysan SAI_XCR1_DMAEN, 8624fa17938Solivier moysan (unsigned int)~SAI_XCR1_DMAEN); 8633e086edfSolivier moysan if (ret < 0) 8643e086edfSolivier moysan dev_err(cpu_dai->dev, "Failed to update CR1 register\n"); 8656eb17d70SOlivier Moysan 8666eb17d70SOlivier Moysan if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) 8676eb17d70SOlivier Moysan sai->spdif_frm_cnt = 0; 8683e086edfSolivier moysan break; 8693e086edfSolivier moysan default: 8703e086edfSolivier moysan return -EINVAL; 8713e086edfSolivier moysan } 8723e086edfSolivier moysan 8733e086edfSolivier moysan return ret; 8743e086edfSolivier moysan } 8753e086edfSolivier moysan 8763e086edfSolivier moysan static void stm32_sai_shutdown(struct snd_pcm_substream *substream, 8773e086edfSolivier moysan struct snd_soc_dai *cpu_dai) 8783e086edfSolivier moysan { 8793e086edfSolivier moysan struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai); 8803e086edfSolivier moysan 8813e086edfSolivier moysan regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, SAI_XIMR_MASK, 0); 8823e086edfSolivier moysan 883701a6ec3Solivier moysan regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, SAI_XCR1_NODIV, 884701a6ec3Solivier moysan SAI_XCR1_NODIV); 885701a6ec3Solivier moysan 8863e086edfSolivier moysan clk_disable_unprepare(sai->sai_ck); 8873e086edfSolivier moysan sai->substream = NULL; 8883e086edfSolivier moysan } 8893e086edfSolivier moysan 890*187e01d0Solivier moysan static int stm32_sai_pcm_new(struct snd_soc_pcm_runtime *rtd, 891*187e01d0Solivier moysan struct snd_soc_dai *cpu_dai) 892*187e01d0Solivier moysan { 893*187e01d0Solivier moysan struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev); 894*187e01d0Solivier moysan 895*187e01d0Solivier moysan if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) { 896*187e01d0Solivier moysan dev_dbg(&sai->pdev->dev, "%s: register iec controls", __func__); 897*187e01d0Solivier moysan return snd_ctl_add(rtd->pcm->card, 898*187e01d0Solivier moysan snd_ctl_new1(&iec958_ctls, sai)); 899*187e01d0Solivier moysan } 900*187e01d0Solivier moysan 901*187e01d0Solivier moysan return 0; 902*187e01d0Solivier moysan } 903*187e01d0Solivier moysan 9043e086edfSolivier moysan static int stm32_sai_dai_probe(struct snd_soc_dai *cpu_dai) 9053e086edfSolivier moysan { 9063e086edfSolivier moysan struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev); 90761fb4ff7SOlivier Moysan int cr1 = 0, cr1_mask; 9083e086edfSolivier moysan 9093e086edfSolivier moysan sai->dma_params.addr = (dma_addr_t)(sai->phys_addr + STM_SAI_DR_REGX); 910a4529d2bSOlivier Moysan /* 911a4529d2bSOlivier Moysan * DMA supports 4, 8 or 16 burst sizes. Burst size 4 is the best choice, 912a4529d2bSOlivier Moysan * as it allows bytes, half-word and words transfers. (See DMA fifos 913a4529d2bSOlivier Moysan * constraints). 914a4529d2bSOlivier Moysan */ 915a4529d2bSOlivier Moysan sai->dma_params.maxburst = 4; 9163e086edfSolivier moysan /* Buswidth will be set by framework at runtime */ 9173e086edfSolivier moysan sai->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED; 9183e086edfSolivier moysan 9193e086edfSolivier moysan if (STM_SAI_IS_PLAYBACK(sai)) 9203e086edfSolivier moysan snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params, NULL); 9213e086edfSolivier moysan else 9223e086edfSolivier moysan snd_soc_dai_init_dma_data(cpu_dai, NULL, &sai->dma_params); 9233e086edfSolivier moysan 924*187e01d0Solivier moysan /* Next settings are not relevant for spdif mode */ 925*187e01d0Solivier moysan if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) 926*187e01d0Solivier moysan return 0; 927*187e01d0Solivier moysan 92861fb4ff7SOlivier Moysan cr1_mask = SAI_XCR1_RX_TX; 92961fb4ff7SOlivier Moysan if (STM_SAI_IS_CAPTURE(sai)) 93061fb4ff7SOlivier Moysan cr1 |= SAI_XCR1_RX_TX; 93161fb4ff7SOlivier Moysan 9325914d285SOlivier Moysan /* Configure synchronization */ 9335914d285SOlivier Moysan if (sai->sync == SAI_SYNC_EXTERNAL) { 9345914d285SOlivier Moysan /* Configure synchro client and provider */ 9355914d285SOlivier Moysan sai->pdata->set_sync(sai->pdata, sai->np_sync_provider, 9365914d285SOlivier Moysan sai->synco, sai->synci); 9375914d285SOlivier Moysan } 9385914d285SOlivier Moysan 9395914d285SOlivier Moysan cr1_mask |= SAI_XCR1_SYNCEN_MASK; 9405914d285SOlivier Moysan cr1 |= SAI_XCR1_SYNCEN_SET(sai->sync); 9415914d285SOlivier Moysan 94261fb4ff7SOlivier Moysan return regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1); 9433e086edfSolivier moysan } 9443e086edfSolivier moysan 9453e086edfSolivier moysan static const struct snd_soc_dai_ops stm32_sai_pcm_dai_ops = { 9463e086edfSolivier moysan .set_sysclk = stm32_sai_set_sysclk, 9473e086edfSolivier moysan .set_fmt = stm32_sai_set_dai_fmt, 9483e086edfSolivier moysan .set_tdm_slot = stm32_sai_set_dai_tdm_slot, 9493e086edfSolivier moysan .startup = stm32_sai_startup, 9503e086edfSolivier moysan .hw_params = stm32_sai_hw_params, 9513e086edfSolivier moysan .trigger = stm32_sai_trigger, 9523e086edfSolivier moysan .shutdown = stm32_sai_shutdown, 9533e086edfSolivier moysan }; 9543e086edfSolivier moysan 9556eb17d70SOlivier Moysan static int stm32_sai_pcm_process_spdif(struct snd_pcm_substream *substream, 9566eb17d70SOlivier Moysan int channel, unsigned long hwoff, 9576eb17d70SOlivier Moysan void *buf, unsigned long bytes) 9586eb17d70SOlivier Moysan { 9596eb17d70SOlivier Moysan struct snd_pcm_runtime *runtime = substream->runtime; 9606eb17d70SOlivier Moysan struct snd_soc_pcm_runtime *rtd = substream->private_data; 9616eb17d70SOlivier Moysan struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 9626eb17d70SOlivier Moysan struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev); 9636eb17d70SOlivier Moysan int *ptr = (int *)(runtime->dma_area + hwoff + 9646eb17d70SOlivier Moysan channel * (runtime->dma_bytes / runtime->channels)); 9656eb17d70SOlivier Moysan ssize_t cnt = bytes_to_samples(runtime, bytes); 9666eb17d70SOlivier Moysan unsigned int frm_cnt = sai->spdif_frm_cnt; 9676eb17d70SOlivier Moysan unsigned int byte; 9686eb17d70SOlivier Moysan unsigned int mask; 9696eb17d70SOlivier Moysan 9706eb17d70SOlivier Moysan do { 9716eb17d70SOlivier Moysan *ptr = ((*ptr >> 8) & 0x00ffffff); 9726eb17d70SOlivier Moysan 9736eb17d70SOlivier Moysan /* Set channel status bit */ 9746eb17d70SOlivier Moysan byte = frm_cnt >> 3; 9756eb17d70SOlivier Moysan mask = 1 << (frm_cnt - (byte << 3)); 976*187e01d0Solivier moysan if (sai->iec958.status[byte] & mask) 9776eb17d70SOlivier Moysan *ptr |= 0x04000000; 9786eb17d70SOlivier Moysan ptr++; 9796eb17d70SOlivier Moysan 9806eb17d70SOlivier Moysan if (!(cnt % 2)) 9816eb17d70SOlivier Moysan frm_cnt++; 9826eb17d70SOlivier Moysan 9836eb17d70SOlivier Moysan if (frm_cnt == SAI_IEC60958_BLOCK_FRAMES) 9846eb17d70SOlivier Moysan frm_cnt = 0; 9856eb17d70SOlivier Moysan } while (--cnt); 9866eb17d70SOlivier Moysan sai->spdif_frm_cnt = frm_cnt; 9876eb17d70SOlivier Moysan 9886eb17d70SOlivier Moysan return 0; 9896eb17d70SOlivier Moysan } 9906eb17d70SOlivier Moysan 9913e086edfSolivier moysan static const struct snd_pcm_hardware stm32_sai_pcm_hw = { 9923e086edfSolivier moysan .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP, 9933e086edfSolivier moysan .buffer_bytes_max = 8 * PAGE_SIZE, 9943e086edfSolivier moysan .period_bytes_min = 1024, /* 5ms at 48kHz */ 9953e086edfSolivier moysan .period_bytes_max = PAGE_SIZE, 9963e086edfSolivier moysan .periods_min = 2, 9973e086edfSolivier moysan .periods_max = 8, 9983e086edfSolivier moysan }; 9993e086edfSolivier moysan 10003e086edfSolivier moysan static struct snd_soc_dai_driver stm32_sai_playback_dai[] = { 10013e086edfSolivier moysan { 10023e086edfSolivier moysan .probe = stm32_sai_dai_probe, 1003*187e01d0Solivier moysan .pcm_new = stm32_sai_pcm_new, 10043e086edfSolivier moysan .id = 1, /* avoid call to fmt_single_name() */ 10053e086edfSolivier moysan .playback = { 10063e086edfSolivier moysan .channels_min = 1, 10073e086edfSolivier moysan .channels_max = 2, 10083e086edfSolivier moysan .rate_min = 8000, 10093e086edfSolivier moysan .rate_max = 192000, 10103e086edfSolivier moysan .rates = SNDRV_PCM_RATE_CONTINUOUS, 10113e086edfSolivier moysan /* DMA does not support 24 bits transfers */ 10123e086edfSolivier moysan .formats = 10133e086edfSolivier moysan SNDRV_PCM_FMTBIT_S8 | 10143e086edfSolivier moysan SNDRV_PCM_FMTBIT_S16_LE | 10153e086edfSolivier moysan SNDRV_PCM_FMTBIT_S32_LE, 10163e086edfSolivier moysan }, 10173e086edfSolivier moysan .ops = &stm32_sai_pcm_dai_ops, 10183e086edfSolivier moysan } 10193e086edfSolivier moysan }; 10203e086edfSolivier moysan 10213e086edfSolivier moysan static struct snd_soc_dai_driver stm32_sai_capture_dai[] = { 10223e086edfSolivier moysan { 10233e086edfSolivier moysan .probe = stm32_sai_dai_probe, 10243e086edfSolivier moysan .id = 1, /* avoid call to fmt_single_name() */ 10253e086edfSolivier moysan .capture = { 10263e086edfSolivier moysan .channels_min = 1, 10273e086edfSolivier moysan .channels_max = 2, 10283e086edfSolivier moysan .rate_min = 8000, 10293e086edfSolivier moysan .rate_max = 192000, 10303e086edfSolivier moysan .rates = SNDRV_PCM_RATE_CONTINUOUS, 10313e086edfSolivier moysan /* DMA does not support 24 bits transfers */ 10323e086edfSolivier moysan .formats = 10333e086edfSolivier moysan SNDRV_PCM_FMTBIT_S8 | 10343e086edfSolivier moysan SNDRV_PCM_FMTBIT_S16_LE | 10353e086edfSolivier moysan SNDRV_PCM_FMTBIT_S32_LE, 10363e086edfSolivier moysan }, 10373e086edfSolivier moysan .ops = &stm32_sai_pcm_dai_ops, 10383e086edfSolivier moysan } 10393e086edfSolivier moysan }; 10403e086edfSolivier moysan 10413e086edfSolivier moysan static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config = { 10423e086edfSolivier moysan .pcm_hardware = &stm32_sai_pcm_hw, 10433e086edfSolivier moysan .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config, 10443e086edfSolivier moysan }; 10453e086edfSolivier moysan 10466eb17d70SOlivier Moysan static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config_spdif = { 10476eb17d70SOlivier Moysan .pcm_hardware = &stm32_sai_pcm_hw, 10486eb17d70SOlivier Moysan .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config, 10496eb17d70SOlivier Moysan .process = stm32_sai_pcm_process_spdif, 10506eb17d70SOlivier Moysan }; 10516eb17d70SOlivier Moysan 10523e086edfSolivier moysan static const struct snd_soc_component_driver stm32_component = { 10533e086edfSolivier moysan .name = "stm32-sai", 10543e086edfSolivier moysan }; 10553e086edfSolivier moysan 10563e086edfSolivier moysan static const struct of_device_id stm32_sai_sub_ids[] = { 10573e086edfSolivier moysan { .compatible = "st,stm32-sai-sub-a", 10583e086edfSolivier moysan .data = (void *)STM_SAI_A_ID}, 10593e086edfSolivier moysan { .compatible = "st,stm32-sai-sub-b", 10603e086edfSolivier moysan .data = (void *)STM_SAI_B_ID}, 10613e086edfSolivier moysan {} 10623e086edfSolivier moysan }; 10633e086edfSolivier moysan MODULE_DEVICE_TABLE(of, stm32_sai_sub_ids); 10643e086edfSolivier moysan 10653e086edfSolivier moysan static int stm32_sai_sub_parse_of(struct platform_device *pdev, 10663e086edfSolivier moysan struct stm32_sai_sub_data *sai) 10673e086edfSolivier moysan { 10683e086edfSolivier moysan struct device_node *np = pdev->dev.of_node; 10693e086edfSolivier moysan struct resource *res; 10703e086edfSolivier moysan void __iomem *base; 10715914d285SOlivier Moysan struct of_phandle_args args; 10725914d285SOlivier Moysan int ret; 10733e086edfSolivier moysan 10743e086edfSolivier moysan if (!np) 10753e086edfSolivier moysan return -ENODEV; 10763e086edfSolivier moysan 10773e086edfSolivier moysan res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 10783e086edfSolivier moysan base = devm_ioremap_resource(&pdev->dev, res); 10793e086edfSolivier moysan if (IS_ERR(base)) 10803e086edfSolivier moysan return PTR_ERR(base); 10813e086edfSolivier moysan 10823e086edfSolivier moysan sai->phys_addr = res->start; 108303e78a24Solivier moysan 108403e78a24Solivier moysan sai->regmap_config = &stm32_sai_sub_regmap_config_f4; 108503e78a24Solivier moysan /* Note: PDM registers not available for H7 sub-block B */ 108603e78a24Solivier moysan if (STM_SAI_IS_H7(sai->pdata) && STM_SAI_IS_SUB_A(sai)) 108703e78a24Solivier moysan sai->regmap_config = &stm32_sai_sub_regmap_config_h7; 108803e78a24Solivier moysan 108903e78a24Solivier moysan sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "sai_ck", 109003e78a24Solivier moysan base, sai->regmap_config); 109103e78a24Solivier moysan if (IS_ERR(sai->regmap)) { 109203e78a24Solivier moysan dev_err(&pdev->dev, "Failed to initialize MMIO\n"); 109303e78a24Solivier moysan return PTR_ERR(sai->regmap); 109403e78a24Solivier moysan } 10953e086edfSolivier moysan 10963e086edfSolivier moysan /* Get direction property */ 10973e086edfSolivier moysan if (of_property_match_string(np, "dma-names", "tx") >= 0) { 10983e086edfSolivier moysan sai->dir = SNDRV_PCM_STREAM_PLAYBACK; 10993e086edfSolivier moysan } else if (of_property_match_string(np, "dma-names", "rx") >= 0) { 11003e086edfSolivier moysan sai->dir = SNDRV_PCM_STREAM_CAPTURE; 11013e086edfSolivier moysan } else { 11023e086edfSolivier moysan dev_err(&pdev->dev, "Unsupported direction\n"); 11033e086edfSolivier moysan return -EINVAL; 11043e086edfSolivier moysan } 11053e086edfSolivier moysan 11066eb17d70SOlivier Moysan /* Get spdif iec60958 property */ 11076eb17d70SOlivier Moysan sai->spdif = false; 11086eb17d70SOlivier Moysan if (of_get_property(np, "st,iec60958", NULL)) { 11096eb17d70SOlivier Moysan if (!STM_SAI_HAS_SPDIF(sai) || 11106eb17d70SOlivier Moysan sai->dir == SNDRV_PCM_STREAM_CAPTURE) { 11116eb17d70SOlivier Moysan dev_err(&pdev->dev, "S/PDIF IEC60958 not supported\n"); 11126eb17d70SOlivier Moysan return -EINVAL; 11136eb17d70SOlivier Moysan } 1114*187e01d0Solivier moysan stm32_sai_init_iec958_status(sai); 11156eb17d70SOlivier Moysan sai->spdif = true; 11166eb17d70SOlivier Moysan sai->master = true; 11176eb17d70SOlivier Moysan } 11186eb17d70SOlivier Moysan 11195914d285SOlivier Moysan /* Get synchronization property */ 11205914d285SOlivier Moysan args.np = NULL; 11215914d285SOlivier Moysan ret = of_parse_phandle_with_fixed_args(np, "st,sync", 1, 0, &args); 11225914d285SOlivier Moysan if (ret < 0 && ret != -ENOENT) { 11235914d285SOlivier Moysan dev_err(&pdev->dev, "Failed to get st,sync property\n"); 11245914d285SOlivier Moysan return ret; 11255914d285SOlivier Moysan } 11265914d285SOlivier Moysan 11275914d285SOlivier Moysan sai->sync = SAI_SYNC_NONE; 11285914d285SOlivier Moysan if (args.np) { 11295914d285SOlivier Moysan if (args.np == np) { 11305914d285SOlivier Moysan dev_err(&pdev->dev, "%s sync own reference\n", 11315914d285SOlivier Moysan np->name); 11325914d285SOlivier Moysan of_node_put(args.np); 11335914d285SOlivier Moysan return -EINVAL; 11345914d285SOlivier Moysan } 11355914d285SOlivier Moysan 11365914d285SOlivier Moysan sai->np_sync_provider = of_get_parent(args.np); 11375914d285SOlivier Moysan if (!sai->np_sync_provider) { 11385914d285SOlivier Moysan dev_err(&pdev->dev, "%s parent node not found\n", 11395914d285SOlivier Moysan np->name); 11405914d285SOlivier Moysan of_node_put(args.np); 11415914d285SOlivier Moysan return -ENODEV; 11425914d285SOlivier Moysan } 11435914d285SOlivier Moysan 11445914d285SOlivier Moysan sai->sync = SAI_SYNC_INTERNAL; 11455914d285SOlivier Moysan if (sai->np_sync_provider != sai->pdata->pdev->dev.of_node) { 11465914d285SOlivier Moysan if (!STM_SAI_HAS_EXT_SYNC(sai)) { 11475914d285SOlivier Moysan dev_err(&pdev->dev, 11485914d285SOlivier Moysan "External synchro not supported\n"); 11495914d285SOlivier Moysan of_node_put(args.np); 11505914d285SOlivier Moysan return -EINVAL; 11515914d285SOlivier Moysan } 11525914d285SOlivier Moysan sai->sync = SAI_SYNC_EXTERNAL; 11535914d285SOlivier Moysan 11545914d285SOlivier Moysan sai->synci = args.args[0]; 11555914d285SOlivier Moysan if (sai->synci < 1 || 11565914d285SOlivier Moysan (sai->synci > (SAI_GCR_SYNCIN_MAX + 1))) { 11575914d285SOlivier Moysan dev_err(&pdev->dev, "Wrong SAI index\n"); 11585914d285SOlivier Moysan of_node_put(args.np); 11595914d285SOlivier Moysan return -EINVAL; 11605914d285SOlivier Moysan } 11615914d285SOlivier Moysan 11625914d285SOlivier Moysan if (of_property_match_string(args.np, "compatible", 11635914d285SOlivier Moysan "st,stm32-sai-sub-a") >= 0) 11645914d285SOlivier Moysan sai->synco = STM_SAI_SYNC_OUT_A; 11655914d285SOlivier Moysan 11665914d285SOlivier Moysan if (of_property_match_string(args.np, "compatible", 11675914d285SOlivier Moysan "st,stm32-sai-sub-b") >= 0) 11685914d285SOlivier Moysan sai->synco = STM_SAI_SYNC_OUT_B; 11695914d285SOlivier Moysan 11705914d285SOlivier Moysan if (!sai->synco) { 11715914d285SOlivier Moysan dev_err(&pdev->dev, "Unknown SAI sub-block\n"); 11725914d285SOlivier Moysan of_node_put(args.np); 11735914d285SOlivier Moysan return -EINVAL; 11745914d285SOlivier Moysan } 11755914d285SOlivier Moysan } 11765914d285SOlivier Moysan 11775914d285SOlivier Moysan dev_dbg(&pdev->dev, "%s synchronized with %s\n", 11785914d285SOlivier Moysan pdev->name, args.np->full_name); 11795914d285SOlivier Moysan } 11805914d285SOlivier Moysan 11815914d285SOlivier Moysan of_node_put(args.np); 11823e086edfSolivier moysan sai->sai_ck = devm_clk_get(&pdev->dev, "sai_ck"); 11833e086edfSolivier moysan if (IS_ERR(sai->sai_ck)) { 1184602fdadcSolivier moysan dev_err(&pdev->dev, "Missing kernel clock sai_ck\n"); 11853e086edfSolivier moysan return PTR_ERR(sai->sai_ck); 11863e086edfSolivier moysan } 11873e086edfSolivier moysan 11883e086edfSolivier moysan return 0; 11893e086edfSolivier moysan } 11903e086edfSolivier moysan 11913e086edfSolivier moysan static int stm32_sai_sub_dais_init(struct platform_device *pdev, 11923e086edfSolivier moysan struct stm32_sai_sub_data *sai) 11933e086edfSolivier moysan { 11943e086edfSolivier moysan sai->cpu_dai_drv = devm_kzalloc(&pdev->dev, 11953e086edfSolivier moysan sizeof(struct snd_soc_dai_driver), 11963e086edfSolivier moysan GFP_KERNEL); 11973e086edfSolivier moysan if (!sai->cpu_dai_drv) 11983e086edfSolivier moysan return -ENOMEM; 11993e086edfSolivier moysan 12003e086edfSolivier moysan sai->cpu_dai_drv->name = dev_name(&pdev->dev); 12013e086edfSolivier moysan if (STM_SAI_IS_PLAYBACK(sai)) { 12023e086edfSolivier moysan memcpy(sai->cpu_dai_drv, &stm32_sai_playback_dai, 12033e086edfSolivier moysan sizeof(stm32_sai_playback_dai)); 12043e086edfSolivier moysan sai->cpu_dai_drv->playback.stream_name = sai->cpu_dai_drv->name; 12053e086edfSolivier moysan } else { 12063e086edfSolivier moysan memcpy(sai->cpu_dai_drv, &stm32_sai_capture_dai, 12073e086edfSolivier moysan sizeof(stm32_sai_capture_dai)); 12083e086edfSolivier moysan sai->cpu_dai_drv->capture.stream_name = sai->cpu_dai_drv->name; 12093e086edfSolivier moysan } 12103e086edfSolivier moysan 12113e086edfSolivier moysan return 0; 12123e086edfSolivier moysan } 12133e086edfSolivier moysan 12143e086edfSolivier moysan static int stm32_sai_sub_probe(struct platform_device *pdev) 12153e086edfSolivier moysan { 12163e086edfSolivier moysan struct stm32_sai_sub_data *sai; 12173e086edfSolivier moysan const struct of_device_id *of_id; 12186eb17d70SOlivier Moysan const struct snd_dmaengine_pcm_config *conf = &stm32_sai_pcm_config; 12193e086edfSolivier moysan int ret; 12203e086edfSolivier moysan 12213e086edfSolivier moysan sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL); 12223e086edfSolivier moysan if (!sai) 12233e086edfSolivier moysan return -ENOMEM; 12243e086edfSolivier moysan 12253e086edfSolivier moysan of_id = of_match_device(stm32_sai_sub_ids, &pdev->dev); 12263e086edfSolivier moysan if (!of_id) 12273e086edfSolivier moysan return -EINVAL; 12283e086edfSolivier moysan sai->id = (uintptr_t)of_id->data; 12293e086edfSolivier moysan 12303e086edfSolivier moysan sai->pdev = pdev; 1231*187e01d0Solivier moysan mutex_init(&sai->ctrl_lock); 12323e086edfSolivier moysan platform_set_drvdata(pdev, sai); 12333e086edfSolivier moysan 12343e086edfSolivier moysan sai->pdata = dev_get_drvdata(pdev->dev.parent); 12353e086edfSolivier moysan if (!sai->pdata) { 12363e086edfSolivier moysan dev_err(&pdev->dev, "Parent device data not available\n"); 12373e086edfSolivier moysan return -EINVAL; 12383e086edfSolivier moysan } 12393e086edfSolivier moysan 12403e086edfSolivier moysan ret = stm32_sai_sub_parse_of(pdev, sai); 12413e086edfSolivier moysan if (ret) 12423e086edfSolivier moysan return ret; 12433e086edfSolivier moysan 12443e086edfSolivier moysan ret = stm32_sai_sub_dais_init(pdev, sai); 12453e086edfSolivier moysan if (ret) 12463e086edfSolivier moysan return ret; 12473e086edfSolivier moysan 12483e086edfSolivier moysan ret = devm_request_irq(&pdev->dev, sai->pdata->irq, stm32_sai_isr, 12493e086edfSolivier moysan IRQF_SHARED, dev_name(&pdev->dev), sai); 12503e086edfSolivier moysan if (ret) { 1251602fdadcSolivier moysan dev_err(&pdev->dev, "IRQ request returned %d\n", ret); 12523e086edfSolivier moysan return ret; 12533e086edfSolivier moysan } 12543e086edfSolivier moysan 12553e086edfSolivier moysan ret = devm_snd_soc_register_component(&pdev->dev, &stm32_component, 12563e086edfSolivier moysan sai->cpu_dai_drv, 1); 12573e086edfSolivier moysan if (ret) 12583e086edfSolivier moysan return ret; 12593e086edfSolivier moysan 12606eb17d70SOlivier Moysan if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) 12616eb17d70SOlivier Moysan conf = &stm32_sai_pcm_config_spdif; 12626eb17d70SOlivier Moysan 12636eb17d70SOlivier Moysan ret = devm_snd_dmaengine_pcm_register(&pdev->dev, conf, 0); 12643e086edfSolivier moysan if (ret) { 1265602fdadcSolivier moysan dev_err(&pdev->dev, "Could not register pcm dma\n"); 12663e086edfSolivier moysan return ret; 12673e086edfSolivier moysan } 12683e086edfSolivier moysan 12693e086edfSolivier moysan return 0; 12703e086edfSolivier moysan } 12713e086edfSolivier moysan 12723e086edfSolivier moysan static struct platform_driver stm32_sai_sub_driver = { 12733e086edfSolivier moysan .driver = { 12743e086edfSolivier moysan .name = "st,stm32-sai-sub", 12753e086edfSolivier moysan .of_match_table = stm32_sai_sub_ids, 12763e086edfSolivier moysan }, 12773e086edfSolivier moysan .probe = stm32_sai_sub_probe, 12783e086edfSolivier moysan }; 12793e086edfSolivier moysan 12803e086edfSolivier moysan module_platform_driver(stm32_sai_sub_driver); 12813e086edfSolivier moysan 12823e086edfSolivier moysan MODULE_DESCRIPTION("STM32 Soc SAI sub-block Interface"); 1283602fdadcSolivier moysan MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>"); 12843e086edfSolivier moysan MODULE_ALIAS("platform:st,stm32-sai-sub"); 12853e086edfSolivier moysan MODULE_LICENSE("GPL v2"); 1286