xref: /linux/sound/soc/stm/stm32_sai_sub.c (revision 7fabe7fed182498cac568100d8e28d4b95f8a80e)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23e086edfSolivier moysan /*
33e086edfSolivier moysan  * STM32 ALSA SoC Digital Audio Interface (SAI) driver.
43e086edfSolivier moysan  *
53e086edfSolivier moysan  * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
63e086edfSolivier moysan  * Author(s): Olivier Moysan <olivier.moysan@st.com> for STMicroelectronics.
73e086edfSolivier moysan  */
83e086edfSolivier moysan 
93e086edfSolivier moysan #include <linux/clk.h>
108307b2afSOlivier Moysan #include <linux/clk-provider.h>
113e086edfSolivier moysan #include <linux/kernel.h>
123e086edfSolivier moysan #include <linux/module.h>
133e086edfSolivier moysan #include <linux/of_irq.h>
143e086edfSolivier moysan #include <linux/of_platform.h>
154e723e75SOlivier Moysan #include <linux/pm_runtime.h>
163e086edfSolivier moysan #include <linux/regmap.h>
173e086edfSolivier moysan 
186eb17d70SOlivier Moysan #include <sound/asoundef.h>
193e086edfSolivier moysan #include <sound/core.h>
203e086edfSolivier moysan #include <sound/dmaengine_pcm.h>
213e086edfSolivier moysan #include <sound/pcm_params.h>
223e086edfSolivier moysan 
233e086edfSolivier moysan #include "stm32_sai.h"
243e086edfSolivier moysan 
253e086edfSolivier moysan #define SAI_FREE_PROTOCOL	0x0
266eb17d70SOlivier Moysan #define SAI_SPDIF_PROTOCOL	0x1
273e086edfSolivier moysan 
283e086edfSolivier moysan #define SAI_SLOT_SIZE_AUTO	0x0
293e086edfSolivier moysan #define SAI_SLOT_SIZE_16	0x1
303e086edfSolivier moysan #define SAI_SLOT_SIZE_32	0x2
313e086edfSolivier moysan 
323e086edfSolivier moysan #define SAI_DATASIZE_8		0x2
333e086edfSolivier moysan #define SAI_DATASIZE_10		0x3
343e086edfSolivier moysan #define SAI_DATASIZE_16		0x4
353e086edfSolivier moysan #define SAI_DATASIZE_20		0x5
363e086edfSolivier moysan #define SAI_DATASIZE_24		0x6
373e086edfSolivier moysan #define SAI_DATASIZE_32		0x7
383e086edfSolivier moysan 
393e086edfSolivier moysan #define STM_SAI_DAI_NAME_SIZE	15
403e086edfSolivier moysan 
413e086edfSolivier moysan #define STM_SAI_IS_PLAYBACK(ip)	((ip)->dir == SNDRV_PCM_STREAM_PLAYBACK)
423e086edfSolivier moysan #define STM_SAI_IS_CAPTURE(ip)	((ip)->dir == SNDRV_PCM_STREAM_CAPTURE)
433e086edfSolivier moysan 
443e086edfSolivier moysan #define STM_SAI_A_ID		0x0
453e086edfSolivier moysan #define STM_SAI_B_ID		0x1
463e086edfSolivier moysan 
4703e78a24Solivier moysan #define STM_SAI_IS_SUB_A(x)	((x)->id == STM_SAI_A_ID)
4803e78a24Solivier moysan #define STM_SAI_IS_SUB_B(x)	((x)->id == STM_SAI_B_ID)
493e086edfSolivier moysan #define STM_SAI_BLOCK_NAME(x)	(((x)->id == STM_SAI_A_ID) ? "A" : "B")
503e086edfSolivier moysan 
515914d285SOlivier Moysan #define SAI_SYNC_NONE		0x0
525914d285SOlivier Moysan #define SAI_SYNC_INTERNAL	0x1
535914d285SOlivier Moysan #define SAI_SYNC_EXTERNAL	0x2
545914d285SOlivier Moysan 
556eb17d70SOlivier Moysan #define STM_SAI_PROTOCOL_IS_SPDIF(ip)	((ip)->spdif)
561d9c95c1SOlivier Moysan #define STM_SAI_HAS_SPDIF(x)	((x)->pdata->conf.has_spdif_pdm)
571d9c95c1SOlivier Moysan #define STM_SAI_HAS_PDM(x)	((x)->pdata->conf.has_spdif_pdm)
585914d285SOlivier Moysan #define STM_SAI_HAS_EXT_SYNC(x) (!STM_SAI_IS_F4(sai->pdata))
595914d285SOlivier Moysan 
606eb17d70SOlivier Moysan #define SAI_IEC60958_BLOCK_FRAMES	192
616eb17d70SOlivier Moysan #define SAI_IEC60958_STATUS_BYTES	24
626eb17d70SOlivier Moysan 
638307b2afSOlivier Moysan #define SAI_MCLK_NAME_LEN		32
64e37c2deaSOlivier Moysan #define SAI_RATE_11K			11025
658307b2afSOlivier Moysan 
663e086edfSolivier moysan /**
673e086edfSolivier moysan  * struct stm32_sai_sub_data - private data of SAI sub block (block A or B)
683e086edfSolivier moysan  * @pdev: device data pointer
693e086edfSolivier moysan  * @regmap: SAI register map pointer
7003e78a24Solivier moysan  * @regmap_config: SAI sub block register map configuration pointer
713e086edfSolivier moysan  * @dma_params: dma configuration data for rx or tx channel
723e086edfSolivier moysan  * @cpu_dai_drv: DAI driver data pointer
733e086edfSolivier moysan  * @cpu_dai: DAI runtime data pointer
743e086edfSolivier moysan  * @substream: PCM substream data pointer
753e086edfSolivier moysan  * @pdata: SAI block parent data pointer
765914d285SOlivier Moysan  * @np_sync_provider: synchronization provider node
773e086edfSolivier moysan  * @sai_ck: kernel clock feeding the SAI clock generator
788307b2afSOlivier Moysan  * @sai_mclk: master clock from SAI mclk provider
793e086edfSolivier moysan  * @phys_addr: SAI registers physical base address
803e086edfSolivier moysan  * @mclk_rate: SAI block master clock frequency (Hz). set at init
813e086edfSolivier moysan  * @id: SAI sub block id corresponding to sub-block A or B
823e086edfSolivier moysan  * @dir: SAI block direction (playback or capture). set at init
833e086edfSolivier moysan  * @master: SAI block mode flag. (true=master, false=slave) set at init
846eb17d70SOlivier Moysan  * @spdif: SAI S/PDIF iec60958 mode flag. set at init
853e086edfSolivier moysan  * @fmt: SAI block format. relevant only for custom protocols. set at init
863e086edfSolivier moysan  * @sync: SAI block synchronization mode. (none, internal or external)
875914d285SOlivier Moysan  * @synco: SAI block ext sync source (provider setting). (none, sub-block A/B)
885914d285SOlivier Moysan  * @synci: SAI block ext sync source (client setting). (SAI sync provider index)
893e086edfSolivier moysan  * @fs_length: frame synchronization length. depends on protocol settings
903e086edfSolivier moysan  * @slots: rx or tx slot number
913e086edfSolivier moysan  * @slot_width: rx or tx slot width in bits
923e086edfSolivier moysan  * @slot_mask: rx or tx active slots mask. set at init or at runtime
933e086edfSolivier moysan  * @data_size: PCM data width. corresponds to PCM substream width.
946eb17d70SOlivier Moysan  * @spdif_frm_cnt: S/PDIF playback frame counter
955f8a1000SOlivier Moysan  * @iec958: iec958 data
96187e01d0Solivier moysan  * @ctrl_lock: control lock
9726f98e82SOlivier Moysan  * @irq_lock: prevent race condition with IRQ
983e086edfSolivier moysan  */
993e086edfSolivier moysan struct stm32_sai_sub_data {
1003e086edfSolivier moysan 	struct platform_device *pdev;
1013e086edfSolivier moysan 	struct regmap *regmap;
10203e78a24Solivier moysan 	const struct regmap_config *regmap_config;
1033e086edfSolivier moysan 	struct snd_dmaengine_dai_dma_data dma_params;
1048f8a5488SArnaud Pouliquen 	struct snd_soc_dai_driver cpu_dai_drv;
1053e086edfSolivier moysan 	struct snd_soc_dai *cpu_dai;
1063e086edfSolivier moysan 	struct snd_pcm_substream *substream;
1073e086edfSolivier moysan 	struct stm32_sai_data *pdata;
1085914d285SOlivier Moysan 	struct device_node *np_sync_provider;
1093e086edfSolivier moysan 	struct clk *sai_ck;
1108307b2afSOlivier Moysan 	struct clk *sai_mclk;
1113e086edfSolivier moysan 	dma_addr_t phys_addr;
1123e086edfSolivier moysan 	unsigned int mclk_rate;
1133e086edfSolivier moysan 	unsigned int id;
1143e086edfSolivier moysan 	int dir;
1153e086edfSolivier moysan 	bool master;
1166eb17d70SOlivier Moysan 	bool spdif;
1173e086edfSolivier moysan 	int fmt;
1183e086edfSolivier moysan 	int sync;
1195914d285SOlivier Moysan 	int synco;
1205914d285SOlivier Moysan 	int synci;
1213e086edfSolivier moysan 	int fs_length;
1223e086edfSolivier moysan 	int slots;
1233e086edfSolivier moysan 	int slot_width;
1243e086edfSolivier moysan 	int slot_mask;
1253e086edfSolivier moysan 	int data_size;
1266eb17d70SOlivier Moysan 	unsigned int spdif_frm_cnt;
127187e01d0Solivier moysan 	struct snd_aes_iec958 iec958;
128187e01d0Solivier moysan 	struct mutex ctrl_lock; /* protect resources accessed by controls */
12926f98e82SOlivier Moysan 	spinlock_t irq_lock; /* used to prevent race condition with IRQ */
1303e086edfSolivier moysan };
1313e086edfSolivier moysan 
1323e086edfSolivier moysan enum stm32_sai_fifo_th {
1333e086edfSolivier moysan 	STM_SAI_FIFO_TH_EMPTY,
1343e086edfSolivier moysan 	STM_SAI_FIFO_TH_QUARTER,
1353e086edfSolivier moysan 	STM_SAI_FIFO_TH_HALF,
1363e086edfSolivier moysan 	STM_SAI_FIFO_TH_3_QUARTER,
1373e086edfSolivier moysan 	STM_SAI_FIFO_TH_FULL,
1383e086edfSolivier moysan };
1393e086edfSolivier moysan 
1403e086edfSolivier moysan static bool stm32_sai_sub_readable_reg(struct device *dev, unsigned int reg)
1413e086edfSolivier moysan {
1423e086edfSolivier moysan 	switch (reg) {
1433e086edfSolivier moysan 	case STM_SAI_CR1_REGX:
1443e086edfSolivier moysan 	case STM_SAI_CR2_REGX:
1453e086edfSolivier moysan 	case STM_SAI_FRCR_REGX:
1463e086edfSolivier moysan 	case STM_SAI_SLOTR_REGX:
1473e086edfSolivier moysan 	case STM_SAI_IMR_REGX:
1483e086edfSolivier moysan 	case STM_SAI_SR_REGX:
1493e086edfSolivier moysan 	case STM_SAI_CLRFR_REGX:
1503e086edfSolivier moysan 	case STM_SAI_DR_REGX:
15103e78a24Solivier moysan 	case STM_SAI_PDMCR_REGX:
15203e78a24Solivier moysan 	case STM_SAI_PDMLY_REGX:
1533e086edfSolivier moysan 		return true;
1543e086edfSolivier moysan 	default:
1553e086edfSolivier moysan 		return false;
1563e086edfSolivier moysan 	}
1573e086edfSolivier moysan }
1583e086edfSolivier moysan 
1593e086edfSolivier moysan static bool stm32_sai_sub_volatile_reg(struct device *dev, unsigned int reg)
1603e086edfSolivier moysan {
1613e086edfSolivier moysan 	switch (reg) {
1623e086edfSolivier moysan 	case STM_SAI_DR_REGX:
163cf881773SOlivier Moysan 	case STM_SAI_SR_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_CLRFR_REGX:
1793e086edfSolivier moysan 	case STM_SAI_DR_REGX:
18003e78a24Solivier moysan 	case STM_SAI_PDMCR_REGX:
18103e78a24Solivier moysan 	case STM_SAI_PDMLY_REGX:
1823e086edfSolivier moysan 		return true;
1833e086edfSolivier moysan 	default:
1843e086edfSolivier moysan 		return false;
1853e086edfSolivier moysan 	}
1863e086edfSolivier moysan }
1873e086edfSolivier moysan 
188a14bf98cSOlivier Moysan static int stm32_sai_sub_reg_up(struct stm32_sai_sub_data *sai,
189a14bf98cSOlivier Moysan 				unsigned int reg, unsigned int mask,
190a14bf98cSOlivier Moysan 				unsigned int val)
191a14bf98cSOlivier Moysan {
192a14bf98cSOlivier Moysan 	int ret;
193a14bf98cSOlivier Moysan 
194a14bf98cSOlivier Moysan 	ret = clk_enable(sai->pdata->pclk);
195a14bf98cSOlivier Moysan 	if (ret < 0)
196a14bf98cSOlivier Moysan 		return ret;
197a14bf98cSOlivier Moysan 
198a14bf98cSOlivier Moysan 	ret = regmap_update_bits(sai->regmap, reg, mask, val);
199a14bf98cSOlivier Moysan 
200a14bf98cSOlivier Moysan 	clk_disable(sai->pdata->pclk);
201a14bf98cSOlivier Moysan 
202a14bf98cSOlivier Moysan 	return ret;
203a14bf98cSOlivier Moysan }
204a14bf98cSOlivier Moysan 
205a14bf98cSOlivier Moysan static int stm32_sai_sub_reg_wr(struct stm32_sai_sub_data *sai,
206a14bf98cSOlivier Moysan 				unsigned int reg, unsigned int mask,
207a14bf98cSOlivier Moysan 				unsigned int val)
208a14bf98cSOlivier Moysan {
209a14bf98cSOlivier Moysan 	int ret;
210a14bf98cSOlivier Moysan 
211a14bf98cSOlivier Moysan 	ret = clk_enable(sai->pdata->pclk);
212a14bf98cSOlivier Moysan 	if (ret < 0)
213a14bf98cSOlivier Moysan 		return ret;
214a14bf98cSOlivier Moysan 
215a14bf98cSOlivier Moysan 	ret = regmap_write_bits(sai->regmap, reg, mask, val);
216a14bf98cSOlivier Moysan 
217a14bf98cSOlivier Moysan 	clk_disable(sai->pdata->pclk);
218a14bf98cSOlivier Moysan 
219a14bf98cSOlivier Moysan 	return ret;
220a14bf98cSOlivier Moysan }
221a14bf98cSOlivier Moysan 
222a14bf98cSOlivier Moysan static int stm32_sai_sub_reg_rd(struct stm32_sai_sub_data *sai,
223a14bf98cSOlivier Moysan 				unsigned int reg, unsigned int *val)
224a14bf98cSOlivier Moysan {
225a14bf98cSOlivier Moysan 	int ret;
226a14bf98cSOlivier Moysan 
227a14bf98cSOlivier Moysan 	ret = clk_enable(sai->pdata->pclk);
228a14bf98cSOlivier Moysan 	if (ret < 0)
229a14bf98cSOlivier Moysan 		return ret;
230a14bf98cSOlivier Moysan 
231a14bf98cSOlivier Moysan 	ret = regmap_read(sai->regmap, reg, val);
232a14bf98cSOlivier Moysan 
233a14bf98cSOlivier Moysan 	clk_disable(sai->pdata->pclk);
234a14bf98cSOlivier Moysan 
235a14bf98cSOlivier Moysan 	return ret;
236a14bf98cSOlivier Moysan }
237a14bf98cSOlivier Moysan 
23803e78a24Solivier moysan static const struct regmap_config stm32_sai_sub_regmap_config_f4 = {
2393e086edfSolivier moysan 	.reg_bits = 32,
2403e086edfSolivier moysan 	.reg_stride = 4,
2413e086edfSolivier moysan 	.val_bits = 32,
2423e086edfSolivier moysan 	.max_register = STM_SAI_DR_REGX,
2433e086edfSolivier moysan 	.readable_reg = stm32_sai_sub_readable_reg,
2443e086edfSolivier moysan 	.volatile_reg = stm32_sai_sub_volatile_reg,
2453e086edfSolivier moysan 	.writeable_reg = stm32_sai_sub_writeable_reg,
2463e086edfSolivier moysan 	.fast_io = true,
247cf881773SOlivier Moysan 	.cache_type = REGCACHE_FLAT,
2483e086edfSolivier moysan };
2493e086edfSolivier moysan 
25003e78a24Solivier moysan static const struct regmap_config stm32_sai_sub_regmap_config_h7 = {
25103e78a24Solivier moysan 	.reg_bits = 32,
25203e78a24Solivier moysan 	.reg_stride = 4,
25303e78a24Solivier moysan 	.val_bits = 32,
25403e78a24Solivier moysan 	.max_register = STM_SAI_PDMLY_REGX,
25503e78a24Solivier moysan 	.readable_reg = stm32_sai_sub_readable_reg,
25603e78a24Solivier moysan 	.volatile_reg = stm32_sai_sub_volatile_reg,
25703e78a24Solivier moysan 	.writeable_reg = stm32_sai_sub_writeable_reg,
25803e78a24Solivier moysan 	.fast_io = true,
259cf881773SOlivier Moysan 	.cache_type = REGCACHE_FLAT,
26003e78a24Solivier moysan };
26103e78a24Solivier moysan 
262187e01d0Solivier moysan static int snd_pcm_iec958_info(struct snd_kcontrol *kcontrol,
263187e01d0Solivier moysan 			       struct snd_ctl_elem_info *uinfo)
264187e01d0Solivier moysan {
265187e01d0Solivier moysan 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
266187e01d0Solivier moysan 	uinfo->count = 1;
267187e01d0Solivier moysan 
268187e01d0Solivier moysan 	return 0;
269187e01d0Solivier moysan }
270187e01d0Solivier moysan 
271187e01d0Solivier moysan static int snd_pcm_iec958_get(struct snd_kcontrol *kcontrol,
272187e01d0Solivier moysan 			      struct snd_ctl_elem_value *uctl)
273187e01d0Solivier moysan {
274187e01d0Solivier moysan 	struct stm32_sai_sub_data *sai = snd_kcontrol_chip(kcontrol);
275187e01d0Solivier moysan 
276187e01d0Solivier moysan 	mutex_lock(&sai->ctrl_lock);
277187e01d0Solivier moysan 	memcpy(uctl->value.iec958.status, sai->iec958.status, 4);
278187e01d0Solivier moysan 	mutex_unlock(&sai->ctrl_lock);
279187e01d0Solivier moysan 
280187e01d0Solivier moysan 	return 0;
281187e01d0Solivier moysan }
282187e01d0Solivier moysan 
283187e01d0Solivier moysan static int snd_pcm_iec958_put(struct snd_kcontrol *kcontrol,
284187e01d0Solivier moysan 			      struct snd_ctl_elem_value *uctl)
285187e01d0Solivier moysan {
286187e01d0Solivier moysan 	struct stm32_sai_sub_data *sai = snd_kcontrol_chip(kcontrol);
287187e01d0Solivier moysan 
288187e01d0Solivier moysan 	mutex_lock(&sai->ctrl_lock);
289187e01d0Solivier moysan 	memcpy(sai->iec958.status, uctl->value.iec958.status, 4);
290187e01d0Solivier moysan 	mutex_unlock(&sai->ctrl_lock);
291187e01d0Solivier moysan 
292187e01d0Solivier moysan 	return 0;
293187e01d0Solivier moysan }
294187e01d0Solivier moysan 
295187e01d0Solivier moysan static const struct snd_kcontrol_new iec958_ctls = {
296187e01d0Solivier moysan 	.access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
297187e01d0Solivier moysan 			SNDRV_CTL_ELEM_ACCESS_VOLATILE),
298187e01d0Solivier moysan 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
299187e01d0Solivier moysan 	.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
300187e01d0Solivier moysan 	.info = snd_pcm_iec958_info,
301187e01d0Solivier moysan 	.get = snd_pcm_iec958_get,
302187e01d0Solivier moysan 	.put = snd_pcm_iec958_put,
303187e01d0Solivier moysan };
304187e01d0Solivier moysan 
3058307b2afSOlivier Moysan struct stm32_sai_mclk_data {
3068307b2afSOlivier Moysan 	struct clk_hw hw;
3078307b2afSOlivier Moysan 	unsigned long freq;
3088307b2afSOlivier Moysan 	struct stm32_sai_sub_data *sai_data;
3098307b2afSOlivier Moysan };
3108307b2afSOlivier Moysan 
3118307b2afSOlivier Moysan #define to_mclk_data(_hw) container_of(_hw, struct stm32_sai_mclk_data, hw)
3128307b2afSOlivier Moysan #define STM32_SAI_MAX_CLKS 1
3138307b2afSOlivier Moysan 
3148307b2afSOlivier Moysan static int stm32_sai_get_clk_div(struct stm32_sai_sub_data *sai,
3158307b2afSOlivier Moysan 				 unsigned long input_rate,
3168307b2afSOlivier Moysan 				 unsigned long output_rate)
3178307b2afSOlivier Moysan {
3181d9c95c1SOlivier Moysan 	int version = sai->pdata->conf.version;
3198307b2afSOlivier Moysan 	int div;
3208307b2afSOlivier Moysan 
3218307b2afSOlivier Moysan 	div = DIV_ROUND_CLOSEST(input_rate, output_rate);
3228307b2afSOlivier Moysan 	if (div > SAI_XCR1_MCKDIV_MAX(version)) {
3238307b2afSOlivier Moysan 		dev_err(&sai->pdev->dev, "Divider %d out of range\n", div);
3248307b2afSOlivier Moysan 		return -EINVAL;
3258307b2afSOlivier Moysan 	}
3268307b2afSOlivier Moysan 	dev_dbg(&sai->pdev->dev, "SAI divider %d\n", div);
3278307b2afSOlivier Moysan 
3288307b2afSOlivier Moysan 	if (input_rate % div)
3298307b2afSOlivier Moysan 		dev_dbg(&sai->pdev->dev,
3308307b2afSOlivier Moysan 			"Rate not accurate. requested (%ld), actual (%ld)\n",
3318307b2afSOlivier Moysan 			output_rate, input_rate / div);
3328307b2afSOlivier Moysan 
3338307b2afSOlivier Moysan 	return div;
3348307b2afSOlivier Moysan }
3358307b2afSOlivier Moysan 
3368307b2afSOlivier Moysan static int stm32_sai_set_clk_div(struct stm32_sai_sub_data *sai,
3378307b2afSOlivier Moysan 				 unsigned int div)
3388307b2afSOlivier Moysan {
3391d9c95c1SOlivier Moysan 	int version = sai->pdata->conf.version;
3408307b2afSOlivier Moysan 	int ret, cr1, mask;
3418307b2afSOlivier Moysan 
3428307b2afSOlivier Moysan 	if (div > SAI_XCR1_MCKDIV_MAX(version)) {
3438307b2afSOlivier Moysan 		dev_err(&sai->pdev->dev, "Divider %d out of range\n", div);
3448307b2afSOlivier Moysan 		return -EINVAL;
3458307b2afSOlivier Moysan 	}
3468307b2afSOlivier Moysan 
3478307b2afSOlivier Moysan 	mask = SAI_XCR1_MCKDIV_MASK(SAI_XCR1_MCKDIV_WIDTH(version));
3488307b2afSOlivier Moysan 	cr1 = SAI_XCR1_MCKDIV_SET(div);
349a14bf98cSOlivier Moysan 	ret = stm32_sai_sub_reg_up(sai, STM_SAI_CR1_REGX, mask, cr1);
3508307b2afSOlivier Moysan 	if (ret < 0)
3518307b2afSOlivier Moysan 		dev_err(&sai->pdev->dev, "Failed to update CR1 register\n");
3528307b2afSOlivier Moysan 
3538307b2afSOlivier Moysan 	return ret;
3548307b2afSOlivier Moysan }
3558307b2afSOlivier Moysan 
356e37c2deaSOlivier Moysan static int stm32_sai_set_parent_clock(struct stm32_sai_sub_data *sai,
357e37c2deaSOlivier Moysan 				      unsigned int rate)
358e37c2deaSOlivier Moysan {
359e37c2deaSOlivier Moysan 	struct platform_device *pdev = sai->pdev;
360e37c2deaSOlivier Moysan 	struct clk *parent_clk = sai->pdata->clk_x8k;
361e37c2deaSOlivier Moysan 	int ret;
362e37c2deaSOlivier Moysan 
363e37c2deaSOlivier Moysan 	if (!(rate % SAI_RATE_11K))
364e37c2deaSOlivier Moysan 		parent_clk = sai->pdata->clk_x11k;
365e37c2deaSOlivier Moysan 
366e37c2deaSOlivier Moysan 	ret = clk_set_parent(sai->sai_ck, parent_clk);
367e37c2deaSOlivier Moysan 	if (ret)
368e37c2deaSOlivier Moysan 		dev_err(&pdev->dev, " Error %d setting sai_ck parent clock. %s",
369e37c2deaSOlivier Moysan 			ret, ret == -EBUSY ?
370e37c2deaSOlivier Moysan 			"Active stream rates conflict\n" : "\n");
371e37c2deaSOlivier Moysan 
372e37c2deaSOlivier Moysan 	return ret;
373e37c2deaSOlivier Moysan }
374e37c2deaSOlivier Moysan 
3758307b2afSOlivier Moysan static long stm32_sai_mclk_round_rate(struct clk_hw *hw, unsigned long rate,
3768307b2afSOlivier Moysan 				      unsigned long *prate)
3778307b2afSOlivier Moysan {
3788307b2afSOlivier Moysan 	struct stm32_sai_mclk_data *mclk = to_mclk_data(hw);
3798307b2afSOlivier Moysan 	struct stm32_sai_sub_data *sai = mclk->sai_data;
3808307b2afSOlivier Moysan 	int div;
3818307b2afSOlivier Moysan 
3828307b2afSOlivier Moysan 	div = stm32_sai_get_clk_div(sai, *prate, rate);
3838307b2afSOlivier Moysan 	if (div < 0)
3848307b2afSOlivier Moysan 		return div;
3858307b2afSOlivier Moysan 
3868307b2afSOlivier Moysan 	mclk->freq = *prate / div;
3878307b2afSOlivier Moysan 
3888307b2afSOlivier Moysan 	return mclk->freq;
3898307b2afSOlivier Moysan }
3908307b2afSOlivier Moysan 
3918307b2afSOlivier Moysan static unsigned long stm32_sai_mclk_recalc_rate(struct clk_hw *hw,
3928307b2afSOlivier Moysan 						unsigned long parent_rate)
3938307b2afSOlivier Moysan {
3948307b2afSOlivier Moysan 	struct stm32_sai_mclk_data *mclk = to_mclk_data(hw);
3958307b2afSOlivier Moysan 
3968307b2afSOlivier Moysan 	return mclk->freq;
3978307b2afSOlivier Moysan }
3988307b2afSOlivier Moysan 
3998307b2afSOlivier Moysan static int stm32_sai_mclk_set_rate(struct clk_hw *hw, unsigned long rate,
4008307b2afSOlivier Moysan 				   unsigned long parent_rate)
4018307b2afSOlivier Moysan {
4028307b2afSOlivier Moysan 	struct stm32_sai_mclk_data *mclk = to_mclk_data(hw);
4038307b2afSOlivier Moysan 	struct stm32_sai_sub_data *sai = mclk->sai_data;
4046b27e277SColin Ian King 	int div, ret;
4058307b2afSOlivier Moysan 
4068307b2afSOlivier Moysan 	div = stm32_sai_get_clk_div(sai, parent_rate, rate);
4078307b2afSOlivier Moysan 	if (div < 0)
4088307b2afSOlivier Moysan 		return div;
4098307b2afSOlivier Moysan 
4108307b2afSOlivier Moysan 	ret = stm32_sai_set_clk_div(sai, div);
4118307b2afSOlivier Moysan 	if (ret)
4128307b2afSOlivier Moysan 		return ret;
4138307b2afSOlivier Moysan 
4148307b2afSOlivier Moysan 	mclk->freq = rate;
4158307b2afSOlivier Moysan 
4168307b2afSOlivier Moysan 	return 0;
4178307b2afSOlivier Moysan }
4188307b2afSOlivier Moysan 
4198307b2afSOlivier Moysan static int stm32_sai_mclk_enable(struct clk_hw *hw)
4208307b2afSOlivier Moysan {
4218307b2afSOlivier Moysan 	struct stm32_sai_mclk_data *mclk = to_mclk_data(hw);
4228307b2afSOlivier Moysan 	struct stm32_sai_sub_data *sai = mclk->sai_data;
4238307b2afSOlivier Moysan 
4248307b2afSOlivier Moysan 	dev_dbg(&sai->pdev->dev, "Enable master clock\n");
4258307b2afSOlivier Moysan 
426a14bf98cSOlivier Moysan 	return stm32_sai_sub_reg_up(sai, STM_SAI_CR1_REGX,
4278307b2afSOlivier Moysan 				    SAI_XCR1_MCKEN, SAI_XCR1_MCKEN);
4288307b2afSOlivier Moysan }
4298307b2afSOlivier Moysan 
4308307b2afSOlivier Moysan static void stm32_sai_mclk_disable(struct clk_hw *hw)
4318307b2afSOlivier Moysan {
4328307b2afSOlivier Moysan 	struct stm32_sai_mclk_data *mclk = to_mclk_data(hw);
4338307b2afSOlivier Moysan 	struct stm32_sai_sub_data *sai = mclk->sai_data;
4348307b2afSOlivier Moysan 
4358307b2afSOlivier Moysan 	dev_dbg(&sai->pdev->dev, "Disable master clock\n");
4368307b2afSOlivier Moysan 
437a14bf98cSOlivier Moysan 	stm32_sai_sub_reg_up(sai, STM_SAI_CR1_REGX, SAI_XCR1_MCKEN, 0);
4388307b2afSOlivier Moysan }
4398307b2afSOlivier Moysan 
4408307b2afSOlivier Moysan static const struct clk_ops mclk_ops = {
4418307b2afSOlivier Moysan 	.enable = stm32_sai_mclk_enable,
4428307b2afSOlivier Moysan 	.disable = stm32_sai_mclk_disable,
4438307b2afSOlivier Moysan 	.recalc_rate = stm32_sai_mclk_recalc_rate,
4448307b2afSOlivier Moysan 	.round_rate = stm32_sai_mclk_round_rate,
4458307b2afSOlivier Moysan 	.set_rate = stm32_sai_mclk_set_rate,
4468307b2afSOlivier Moysan };
4478307b2afSOlivier Moysan 
4488307b2afSOlivier Moysan static int stm32_sai_add_mclk_provider(struct stm32_sai_sub_data *sai)
4498307b2afSOlivier Moysan {
4508307b2afSOlivier Moysan 	struct clk_hw *hw;
4518307b2afSOlivier Moysan 	struct stm32_sai_mclk_data *mclk;
4528307b2afSOlivier Moysan 	struct device *dev = &sai->pdev->dev;
4538307b2afSOlivier Moysan 	const char *pname = __clk_get_name(sai->sai_ck);
4548307b2afSOlivier Moysan 	char *mclk_name, *p, *s = (char *)pname;
4558307b2afSOlivier Moysan 	int ret, i = 0;
4568307b2afSOlivier Moysan 
457496fa3baSWei Yongjun 	mclk = devm_kzalloc(dev, sizeof(*mclk), GFP_KERNEL);
4588307b2afSOlivier Moysan 	if (!mclk)
4598307b2afSOlivier Moysan 		return -ENOMEM;
4608307b2afSOlivier Moysan 
4618307b2afSOlivier Moysan 	mclk_name = devm_kcalloc(dev, sizeof(char),
4628307b2afSOlivier Moysan 				 SAI_MCLK_NAME_LEN, GFP_KERNEL);
4638307b2afSOlivier Moysan 	if (!mclk_name)
4648307b2afSOlivier Moysan 		return -ENOMEM;
4658307b2afSOlivier Moysan 
4668307b2afSOlivier Moysan 	/*
4678307b2afSOlivier Moysan 	 * Forge mclk clock name from parent clock name and suffix.
4688307b2afSOlivier Moysan 	 * String after "_" char is stripped in parent name.
4698307b2afSOlivier Moysan 	 */
4708307b2afSOlivier Moysan 	p = mclk_name;
4716be0f96dSOlivier Moysan 	while (*s && *s != '_' && (i < (SAI_MCLK_NAME_LEN - 7))) {
4728307b2afSOlivier Moysan 		*p++ = *s++;
4738307b2afSOlivier Moysan 		i++;
4748307b2afSOlivier Moysan 	}
4756be0f96dSOlivier Moysan 	STM_SAI_IS_SUB_A(sai) ? strcat(p, "a_mclk") : strcat(p, "b_mclk");
4768307b2afSOlivier Moysan 
4778307b2afSOlivier Moysan 	mclk->hw.init = CLK_HW_INIT(mclk_name, pname, &mclk_ops, 0);
4788307b2afSOlivier Moysan 	mclk->sai_data = sai;
4798307b2afSOlivier Moysan 	hw = &mclk->hw;
4808307b2afSOlivier Moysan 
4818307b2afSOlivier Moysan 	dev_dbg(dev, "Register master clock %s\n", mclk_name);
4828307b2afSOlivier Moysan 	ret = devm_clk_hw_register(&sai->pdev->dev, hw);
4838307b2afSOlivier Moysan 	if (ret) {
4848307b2afSOlivier Moysan 		dev_err(dev, "mclk register returned %d\n", ret);
4858307b2afSOlivier Moysan 		return ret;
4868307b2afSOlivier Moysan 	}
487a0695853SJerome Brunet 	sai->sai_mclk = hw->clk;
4888307b2afSOlivier Moysan 
4898307b2afSOlivier Moysan 	/* register mclk provider */
4908307b2afSOlivier Moysan 	return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, hw);
4918307b2afSOlivier Moysan }
4928307b2afSOlivier Moysan 
4933e086edfSolivier moysan static irqreturn_t stm32_sai_isr(int irq, void *devid)
4943e086edfSolivier moysan {
4953e086edfSolivier moysan 	struct stm32_sai_sub_data *sai = (struct stm32_sai_sub_data *)devid;
4963e086edfSolivier moysan 	struct platform_device *pdev = sai->pdev;
4973e086edfSolivier moysan 	unsigned int sr, imr, flags;
4983e086edfSolivier moysan 	snd_pcm_state_t status = SNDRV_PCM_STATE_RUNNING;
4993e086edfSolivier moysan 
500a14bf98cSOlivier Moysan 	stm32_sai_sub_reg_rd(sai, STM_SAI_IMR_REGX, &imr);
501a14bf98cSOlivier Moysan 	stm32_sai_sub_reg_rd(sai, STM_SAI_SR_REGX, &sr);
5023e086edfSolivier moysan 
5033e086edfSolivier moysan 	flags = sr & imr;
5043e086edfSolivier moysan 	if (!flags)
5053e086edfSolivier moysan 		return IRQ_NONE;
5063e086edfSolivier moysan 
507a14bf98cSOlivier Moysan 	stm32_sai_sub_reg_wr(sai, STM_SAI_CLRFR_REGX, SAI_XCLRFR_MASK,
5083e086edfSolivier moysan 			     SAI_XCLRFR_MASK);
5093e086edfSolivier moysan 
510d807cdfbSOlivier Moysan 	if (!sai->substream) {
511d807cdfbSOlivier Moysan 		dev_err(&pdev->dev, "Device stopped. Spurious IRQ 0x%x\n", sr);
512d807cdfbSOlivier Moysan 		return IRQ_NONE;
513d807cdfbSOlivier Moysan 	}
514d807cdfbSOlivier Moysan 
5153e086edfSolivier moysan 	if (flags & SAI_XIMR_OVRUDRIE) {
516602fdadcSolivier moysan 		dev_err(&pdev->dev, "IRQ %s\n",
5173e086edfSolivier moysan 			STM_SAI_IS_PLAYBACK(sai) ? "underrun" : "overrun");
5183e086edfSolivier moysan 		status = SNDRV_PCM_STATE_XRUN;
5193e086edfSolivier moysan 	}
5203e086edfSolivier moysan 
5213e086edfSolivier moysan 	if (flags & SAI_XIMR_MUTEDETIE)
522602fdadcSolivier moysan 		dev_dbg(&pdev->dev, "IRQ mute detected\n");
5233e086edfSolivier moysan 
5243e086edfSolivier moysan 	if (flags & SAI_XIMR_WCKCFGIE) {
525602fdadcSolivier moysan 		dev_err(&pdev->dev, "IRQ wrong clock configuration\n");
5263e086edfSolivier moysan 		status = SNDRV_PCM_STATE_DISCONNECTED;
5273e086edfSolivier moysan 	}
5283e086edfSolivier moysan 
5293e086edfSolivier moysan 	if (flags & SAI_XIMR_CNRDYIE)
530602fdadcSolivier moysan 		dev_err(&pdev->dev, "IRQ Codec not ready\n");
5313e086edfSolivier moysan 
5323e086edfSolivier moysan 	if (flags & SAI_XIMR_AFSDETIE) {
533602fdadcSolivier moysan 		dev_err(&pdev->dev, "IRQ Anticipated frame synchro\n");
5343e086edfSolivier moysan 		status = SNDRV_PCM_STATE_XRUN;
5353e086edfSolivier moysan 	}
5363e086edfSolivier moysan 
5373e086edfSolivier moysan 	if (flags & SAI_XIMR_LFSDETIE) {
538602fdadcSolivier moysan 		dev_err(&pdev->dev, "IRQ Late frame synchro\n");
5393e086edfSolivier moysan 		status = SNDRV_PCM_STATE_XRUN;
5403e086edfSolivier moysan 	}
5413e086edfSolivier moysan 
54226f98e82SOlivier Moysan 	spin_lock(&sai->irq_lock);
54326f98e82SOlivier Moysan 	if (status != SNDRV_PCM_STATE_RUNNING && sai->substream)
544b1625fbbSTakashi Iwai 		snd_pcm_stop_xrun(sai->substream);
54526f98e82SOlivier Moysan 	spin_unlock(&sai->irq_lock);
5463e086edfSolivier moysan 
5473e086edfSolivier moysan 	return IRQ_HANDLED;
5483e086edfSolivier moysan }
5493e086edfSolivier moysan 
5503e086edfSolivier moysan static int stm32_sai_set_sysclk(struct snd_soc_dai *cpu_dai,
5513e086edfSolivier moysan 				int clk_id, unsigned int freq, int dir)
5523e086edfSolivier moysan {
5533e086edfSolivier moysan 	struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
554701a6ec3Solivier moysan 	int ret;
5553e086edfSolivier moysan 
556e37c2deaSOlivier Moysan 	if (dir == SND_SOC_CLOCK_OUT && sai->sai_mclk) {
557a14bf98cSOlivier Moysan 		ret = stm32_sai_sub_reg_up(sai, STM_SAI_CR1_REGX,
558701a6ec3Solivier moysan 					   SAI_XCR1_NODIV,
5599b7a7f92SOlivier Moysan 					 freq ? 0 : SAI_XCR1_NODIV);
560701a6ec3Solivier moysan 		if (ret < 0)
561701a6ec3Solivier moysan 			return ret;
562701a6ec3Solivier moysan 
5639b7a7f92SOlivier Moysan 		/* Assume shutdown if requested frequency is 0Hz */
5649b7a7f92SOlivier Moysan 		if (!freq) {
5659b7a7f92SOlivier Moysan 			/* Release mclk rate only if rate was actually set */
5669b7a7f92SOlivier Moysan 			if (sai->mclk_rate) {
5679b7a7f92SOlivier Moysan 				clk_rate_exclusive_put(sai->sai_mclk);
5689b7a7f92SOlivier Moysan 				sai->mclk_rate = 0;
5699b7a7f92SOlivier Moysan 			}
5709b7a7f92SOlivier Moysan 			return 0;
5719b7a7f92SOlivier Moysan 		}
5729b7a7f92SOlivier Moysan 
573e37c2deaSOlivier Moysan 		/* If master clock is used, set parent clock now */
574e37c2deaSOlivier Moysan 		ret = stm32_sai_set_parent_clock(sai, freq);
575e37c2deaSOlivier Moysan 		if (ret)
576e37c2deaSOlivier Moysan 			return ret;
5778307b2afSOlivier Moysan 
578e37c2deaSOlivier Moysan 		ret = clk_set_rate_exclusive(sai->sai_mclk, freq);
5798307b2afSOlivier Moysan 		if (ret) {
5808307b2afSOlivier Moysan 			dev_err(cpu_dai->dev,
581e37c2deaSOlivier Moysan 				ret == -EBUSY ?
582e37c2deaSOlivier Moysan 				"Active streams have incompatible rates" :
5838307b2afSOlivier Moysan 				"Could not set mclk rate\n");
5848307b2afSOlivier Moysan 			return ret;
5858307b2afSOlivier Moysan 		}
586e37c2deaSOlivier Moysan 
587e37c2deaSOlivier Moysan 		dev_dbg(cpu_dai->dev, "SAI MCLK frequency is %uHz\n", freq);
588e37c2deaSOlivier Moysan 		sai->mclk_rate = freq;
5893e086edfSolivier moysan 	}
5903e086edfSolivier moysan 
5913e086edfSolivier moysan 	return 0;
5923e086edfSolivier moysan }
5933e086edfSolivier moysan 
5943e086edfSolivier moysan static int stm32_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
5953e086edfSolivier moysan 				      u32 rx_mask, int slots, int slot_width)
5963e086edfSolivier moysan {
5973e086edfSolivier moysan 	struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
5983e086edfSolivier moysan 	int slotr, slotr_mask, slot_size;
5993e086edfSolivier moysan 
6006eb17d70SOlivier Moysan 	if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
6016eb17d70SOlivier Moysan 		dev_warn(cpu_dai->dev, "Slot setting relevant only for TDM\n");
6026eb17d70SOlivier Moysan 		return 0;
6036eb17d70SOlivier Moysan 	}
6046eb17d70SOlivier Moysan 
605602fdadcSolivier moysan 	dev_dbg(cpu_dai->dev, "Masks tx/rx:%#x/%#x, slots:%d, width:%d\n",
6063e086edfSolivier moysan 		tx_mask, rx_mask, slots, slot_width);
6073e086edfSolivier moysan 
6083e086edfSolivier moysan 	switch (slot_width) {
6093e086edfSolivier moysan 	case 16:
6103e086edfSolivier moysan 		slot_size = SAI_SLOT_SIZE_16;
6113e086edfSolivier moysan 		break;
6123e086edfSolivier moysan 	case 32:
6133e086edfSolivier moysan 		slot_size = SAI_SLOT_SIZE_32;
6143e086edfSolivier moysan 		break;
6153e086edfSolivier moysan 	default:
6163e086edfSolivier moysan 		slot_size = SAI_SLOT_SIZE_AUTO;
6173e086edfSolivier moysan 		break;
6183e086edfSolivier moysan 	}
6193e086edfSolivier moysan 
6203e086edfSolivier moysan 	slotr = SAI_XSLOTR_SLOTSZ_SET(slot_size) |
6213e086edfSolivier moysan 		SAI_XSLOTR_NBSLOT_SET(slots - 1);
6223e086edfSolivier moysan 	slotr_mask = SAI_XSLOTR_SLOTSZ_MASK | SAI_XSLOTR_NBSLOT_MASK;
6233e086edfSolivier moysan 
6243e086edfSolivier moysan 	/* tx/rx mask set in machine init, if slot number defined in DT */
6253e086edfSolivier moysan 	if (STM_SAI_IS_PLAYBACK(sai)) {
6263e086edfSolivier moysan 		sai->slot_mask = tx_mask;
6273e086edfSolivier moysan 		slotr |= SAI_XSLOTR_SLOTEN_SET(tx_mask);
6283e086edfSolivier moysan 	}
6293e086edfSolivier moysan 
6303e086edfSolivier moysan 	if (STM_SAI_IS_CAPTURE(sai)) {
6313e086edfSolivier moysan 		sai->slot_mask = rx_mask;
6323e086edfSolivier moysan 		slotr |= SAI_XSLOTR_SLOTEN_SET(rx_mask);
6333e086edfSolivier moysan 	}
6343e086edfSolivier moysan 
6353e086edfSolivier moysan 	slotr_mask |= SAI_XSLOTR_SLOTEN_MASK;
6363e086edfSolivier moysan 
637a14bf98cSOlivier Moysan 	stm32_sai_sub_reg_up(sai, STM_SAI_SLOTR_REGX, slotr_mask, slotr);
6383e086edfSolivier moysan 
6393e086edfSolivier moysan 	sai->slot_width = slot_width;
6403e086edfSolivier moysan 	sai->slots = slots;
6413e086edfSolivier moysan 
6423e086edfSolivier moysan 	return 0;
6433e086edfSolivier moysan }
6443e086edfSolivier moysan 
6453e086edfSolivier moysan static int stm32_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
6463e086edfSolivier moysan {
6473e086edfSolivier moysan 	struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
64861fb4ff7SOlivier Moysan 	int cr1, frcr = 0;
64961fb4ff7SOlivier Moysan 	int cr1_mask, frcr_mask = 0;
6503e086edfSolivier moysan 	int ret;
6513e086edfSolivier moysan 
6523e086edfSolivier moysan 	dev_dbg(cpu_dai->dev, "fmt %x\n", fmt);
6533e086edfSolivier moysan 
6546eb17d70SOlivier Moysan 	/* Do not generate master by default */
6556eb17d70SOlivier Moysan 	cr1 = SAI_XCR1_NODIV;
6566eb17d70SOlivier Moysan 	cr1_mask = SAI_XCR1_NODIV;
6576eb17d70SOlivier Moysan 
6586eb17d70SOlivier Moysan 	cr1_mask |= SAI_XCR1_PRTCFG_MASK;
6596eb17d70SOlivier Moysan 	if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
6606eb17d70SOlivier Moysan 		cr1 |= SAI_XCR1_PRTCFG_SET(SAI_SPDIF_PROTOCOL);
6616eb17d70SOlivier Moysan 		goto conf_update;
6626eb17d70SOlivier Moysan 	}
6636eb17d70SOlivier Moysan 
6646eb17d70SOlivier Moysan 	cr1 |= SAI_XCR1_PRTCFG_SET(SAI_FREE_PROTOCOL);
66561fb4ff7SOlivier Moysan 
6663e086edfSolivier moysan 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
6673e086edfSolivier moysan 	/* SCK active high for all protocols */
6683e086edfSolivier moysan 	case SND_SOC_DAIFMT_I2S:
6693e086edfSolivier moysan 		cr1 |= SAI_XCR1_CKSTR;
6703e086edfSolivier moysan 		frcr |= SAI_XFRCR_FSOFF | SAI_XFRCR_FSDEF;
6713e086edfSolivier moysan 		break;
6723e086edfSolivier moysan 	/* Left justified */
6733e086edfSolivier moysan 	case SND_SOC_DAIFMT_MSB:
6743e086edfSolivier moysan 		frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF;
6753e086edfSolivier moysan 		break;
6763e086edfSolivier moysan 	/* Right justified */
6773e086edfSolivier moysan 	case SND_SOC_DAIFMT_LSB:
6783e086edfSolivier moysan 		frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF;
6793e086edfSolivier moysan 		break;
6803e086edfSolivier moysan 	case SND_SOC_DAIFMT_DSP_A:
6813e086edfSolivier moysan 		frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF;
6823e086edfSolivier moysan 		break;
6833e086edfSolivier moysan 	case SND_SOC_DAIFMT_DSP_B:
6843e086edfSolivier moysan 		frcr |= SAI_XFRCR_FSPOL;
6853e086edfSolivier moysan 		break;
6863e086edfSolivier moysan 	default:
6873e086edfSolivier moysan 		dev_err(cpu_dai->dev, "Unsupported protocol %#x\n",
6883e086edfSolivier moysan 			fmt & SND_SOC_DAIFMT_FORMAT_MASK);
6893e086edfSolivier moysan 		return -EINVAL;
6903e086edfSolivier moysan 	}
6913e086edfSolivier moysan 
69261fb4ff7SOlivier Moysan 	cr1_mask |= SAI_XCR1_CKSTR;
6933e086edfSolivier moysan 	frcr_mask |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF |
6943e086edfSolivier moysan 		     SAI_XFRCR_FSDEF;
6953e086edfSolivier moysan 
6963e086edfSolivier moysan 	/* DAI clock strobing. Invert setting previously set */
6973e086edfSolivier moysan 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
6983e086edfSolivier moysan 	case SND_SOC_DAIFMT_NB_NF:
6993e086edfSolivier moysan 		break;
7003e086edfSolivier moysan 	case SND_SOC_DAIFMT_IB_NF:
7013e086edfSolivier moysan 		cr1 ^= SAI_XCR1_CKSTR;
7023e086edfSolivier moysan 		break;
7033e086edfSolivier moysan 	case SND_SOC_DAIFMT_NB_IF:
7043e086edfSolivier moysan 		frcr ^= SAI_XFRCR_FSPOL;
7053e086edfSolivier moysan 		break;
7063e086edfSolivier moysan 	case SND_SOC_DAIFMT_IB_IF:
7073e086edfSolivier moysan 		/* Invert fs & sck */
7083e086edfSolivier moysan 		cr1 ^= SAI_XCR1_CKSTR;
7093e086edfSolivier moysan 		frcr ^= SAI_XFRCR_FSPOL;
7103e086edfSolivier moysan 		break;
7113e086edfSolivier moysan 	default:
7123e086edfSolivier moysan 		dev_err(cpu_dai->dev, "Unsupported strobing %#x\n",
7133e086edfSolivier moysan 			fmt & SND_SOC_DAIFMT_INV_MASK);
7143e086edfSolivier moysan 		return -EINVAL;
7153e086edfSolivier moysan 	}
7163e086edfSolivier moysan 	cr1_mask |= SAI_XCR1_CKSTR;
7173e086edfSolivier moysan 	frcr_mask |= SAI_XFRCR_FSPOL;
7183e086edfSolivier moysan 
719a14bf98cSOlivier Moysan 	stm32_sai_sub_reg_up(sai, STM_SAI_FRCR_REGX, frcr_mask, frcr);
7203e086edfSolivier moysan 
7213e086edfSolivier moysan 	/* DAI clock master masks */
7223e086edfSolivier moysan 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
7233e086edfSolivier moysan 	case SND_SOC_DAIFMT_CBM_CFM:
7243e086edfSolivier moysan 		/* codec is master */
7253e086edfSolivier moysan 		cr1 |= SAI_XCR1_SLAVE;
7263e086edfSolivier moysan 		sai->master = false;
7273e086edfSolivier moysan 		break;
7283e086edfSolivier moysan 	case SND_SOC_DAIFMT_CBS_CFS:
7293e086edfSolivier moysan 		sai->master = true;
7303e086edfSolivier moysan 		break;
7313e086edfSolivier moysan 	default:
7323e086edfSolivier moysan 		dev_err(cpu_dai->dev, "Unsupported mode %#x\n",
7333e086edfSolivier moysan 			fmt & SND_SOC_DAIFMT_MASTER_MASK);
7343e086edfSolivier moysan 		return -EINVAL;
7353e086edfSolivier moysan 	}
7365914d285SOlivier Moysan 
7375914d285SOlivier Moysan 	/* Set slave mode if sub-block is synchronized with another SAI */
7385914d285SOlivier Moysan 	if (sai->sync) {
7395914d285SOlivier Moysan 		dev_dbg(cpu_dai->dev, "Synchronized SAI configured as slave\n");
7405914d285SOlivier Moysan 		cr1 |= SAI_XCR1_SLAVE;
7415914d285SOlivier Moysan 		sai->master = false;
7425914d285SOlivier Moysan 	}
7435914d285SOlivier Moysan 
7443e086edfSolivier moysan 	cr1_mask |= SAI_XCR1_SLAVE;
7453e086edfSolivier moysan 
7466eb17d70SOlivier Moysan conf_update:
747a14bf98cSOlivier Moysan 	ret = stm32_sai_sub_reg_up(sai, STM_SAI_CR1_REGX, cr1_mask, cr1);
7483e086edfSolivier moysan 	if (ret < 0) {
7493e086edfSolivier moysan 		dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
7503e086edfSolivier moysan 		return ret;
7513e086edfSolivier moysan 	}
7523e086edfSolivier moysan 
7533e086edfSolivier moysan 	sai->fmt = fmt;
7543e086edfSolivier moysan 
7553e086edfSolivier moysan 	return 0;
7563e086edfSolivier moysan }
7573e086edfSolivier moysan 
7583e086edfSolivier moysan static int stm32_sai_startup(struct snd_pcm_substream *substream,
7593e086edfSolivier moysan 			     struct snd_soc_dai *cpu_dai)
7603e086edfSolivier moysan {
7613e086edfSolivier moysan 	struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
7623e086edfSolivier moysan 	int imr, cr2, ret;
76326f98e82SOlivier Moysan 	unsigned long flags;
7643e086edfSolivier moysan 
76526f98e82SOlivier Moysan 	spin_lock_irqsave(&sai->irq_lock, flags);
7663e086edfSolivier moysan 	sai->substream = substream;
76726f98e82SOlivier Moysan 	spin_unlock_irqrestore(&sai->irq_lock, flags);
7683e086edfSolivier moysan 
769b8468192SOlivier Moysan 	if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
770b8468192SOlivier Moysan 		snd_pcm_hw_constraint_mask64(substream->runtime,
771b8468192SOlivier Moysan 					     SNDRV_PCM_HW_PARAM_FORMAT,
772b8468192SOlivier Moysan 					     SNDRV_PCM_FMTBIT_S32_LE);
773b8468192SOlivier Moysan 		snd_pcm_hw_constraint_single(substream->runtime,
774b8468192SOlivier Moysan 					     SNDRV_PCM_HW_PARAM_CHANNELS, 2);
775b8468192SOlivier Moysan 	}
776b8468192SOlivier Moysan 
7773e086edfSolivier moysan 	ret = clk_prepare_enable(sai->sai_ck);
7783e086edfSolivier moysan 	if (ret < 0) {
779602fdadcSolivier moysan 		dev_err(cpu_dai->dev, "Failed to enable clock: %d\n", ret);
7803e086edfSolivier moysan 		return ret;
7813e086edfSolivier moysan 	}
7823e086edfSolivier moysan 
7833e086edfSolivier moysan 	/* Enable ITs */
784a14bf98cSOlivier Moysan 	stm32_sai_sub_reg_wr(sai, STM_SAI_CLRFR_REGX,
7853e086edfSolivier moysan 			     SAI_XCLRFR_MASK, SAI_XCLRFR_MASK);
7863e086edfSolivier moysan 
7873e086edfSolivier moysan 	imr = SAI_XIMR_OVRUDRIE;
7883e086edfSolivier moysan 	if (STM_SAI_IS_CAPTURE(sai)) {
789a14bf98cSOlivier Moysan 		stm32_sai_sub_reg_rd(sai, STM_SAI_CR2_REGX, &cr2);
7903e086edfSolivier moysan 		if (cr2 & SAI_XCR2_MUTECNT_MASK)
7913e086edfSolivier moysan 			imr |= SAI_XIMR_MUTEDETIE;
7923e086edfSolivier moysan 	}
7933e086edfSolivier moysan 
7943e086edfSolivier moysan 	if (sai->master)
7953e086edfSolivier moysan 		imr |= SAI_XIMR_WCKCFGIE;
7963e086edfSolivier moysan 	else
7973e086edfSolivier moysan 		imr |= SAI_XIMR_AFSDETIE | SAI_XIMR_LFSDETIE;
7983e086edfSolivier moysan 
799a14bf98cSOlivier Moysan 	stm32_sai_sub_reg_up(sai, STM_SAI_IMR_REGX,
8003e086edfSolivier moysan 			     SAI_XIMR_MASK, imr);
8013e086edfSolivier moysan 
8023e086edfSolivier moysan 	return 0;
8033e086edfSolivier moysan }
8043e086edfSolivier moysan 
8053e086edfSolivier moysan static int stm32_sai_set_config(struct snd_soc_dai *cpu_dai,
8063e086edfSolivier moysan 				struct snd_pcm_substream *substream,
8073e086edfSolivier moysan 				struct snd_pcm_hw_params *params)
8083e086edfSolivier moysan {
8093e086edfSolivier moysan 	struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
8103e086edfSolivier moysan 	int cr1, cr1_mask, ret;
8113e086edfSolivier moysan 
812a4529d2bSOlivier Moysan 	/*
813a4529d2bSOlivier Moysan 	 * DMA bursts increment is set to 4 words.
814a4529d2bSOlivier Moysan 	 * SAI fifo threshold is set to half fifo, to keep enough space
815a4529d2bSOlivier Moysan 	 * for DMA incoming bursts.
816a4529d2bSOlivier Moysan 	 */
817a14bf98cSOlivier Moysan 	stm32_sai_sub_reg_wr(sai, STM_SAI_CR2_REGX,
8183e086edfSolivier moysan 			     SAI_XCR2_FFLUSH | SAI_XCR2_FTH_MASK,
819a4529d2bSOlivier Moysan 			     SAI_XCR2_FFLUSH |
820a4529d2bSOlivier Moysan 			     SAI_XCR2_FTH_SET(STM_SAI_FIFO_TH_HALF));
8213e086edfSolivier moysan 
8226eb17d70SOlivier Moysan 	/* DS bits in CR1 not set for SPDIF (size forced to 24 bits).*/
8236eb17d70SOlivier Moysan 	if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
8246eb17d70SOlivier Moysan 		sai->spdif_frm_cnt = 0;
8256eb17d70SOlivier Moysan 		return 0;
8266eb17d70SOlivier Moysan 	}
8276eb17d70SOlivier Moysan 
8283e086edfSolivier moysan 	/* Mode, data format and channel config */
82961fb4ff7SOlivier Moysan 	cr1_mask = SAI_XCR1_DS_MASK;
8303e086edfSolivier moysan 	switch (params_format(params)) {
8313e086edfSolivier moysan 	case SNDRV_PCM_FORMAT_S8:
8327e751e37Solivier moysan 		cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_8);
8333e086edfSolivier moysan 		break;
8343e086edfSolivier moysan 	case SNDRV_PCM_FORMAT_S16_LE:
8357e751e37Solivier moysan 		cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_16);
8363e086edfSolivier moysan 		break;
8373e086edfSolivier moysan 	case SNDRV_PCM_FORMAT_S32_LE:
8387e751e37Solivier moysan 		cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_32);
8393e086edfSolivier moysan 		break;
8403e086edfSolivier moysan 	default:
8419de300abSSebastian Fricke 		dev_err(cpu_dai->dev, "Data format not supported\n");
8423e086edfSolivier moysan 		return -EINVAL;
8433e086edfSolivier moysan 	}
8443e086edfSolivier moysan 
8453e086edfSolivier moysan 	cr1_mask |= SAI_XCR1_MONO;
8463e086edfSolivier moysan 	if ((sai->slots == 2) && (params_channels(params) == 1))
8473e086edfSolivier moysan 		cr1 |= SAI_XCR1_MONO;
8483e086edfSolivier moysan 
849a14bf98cSOlivier Moysan 	ret = stm32_sai_sub_reg_up(sai, STM_SAI_CR1_REGX, cr1_mask, cr1);
8503e086edfSolivier moysan 	if (ret < 0) {
8513e086edfSolivier moysan 		dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
8523e086edfSolivier moysan 		return ret;
8533e086edfSolivier moysan 	}
8543e086edfSolivier moysan 
8553e086edfSolivier moysan 	return 0;
8563e086edfSolivier moysan }
8573e086edfSolivier moysan 
8583e086edfSolivier moysan static int stm32_sai_set_slots(struct snd_soc_dai *cpu_dai)
8593e086edfSolivier moysan {
8603e086edfSolivier moysan 	struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
8613e086edfSolivier moysan 	int slotr, slot_sz;
8623e086edfSolivier moysan 
863a14bf98cSOlivier Moysan 	stm32_sai_sub_reg_rd(sai, STM_SAI_SLOTR_REGX, &slotr);
8643e086edfSolivier moysan 
8653e086edfSolivier moysan 	/*
8663e086edfSolivier moysan 	 * If SLOTSZ is set to auto in SLOTR, align slot width on data size
8673e086edfSolivier moysan 	 * By default slot width = data size, if not forced from DT
8683e086edfSolivier moysan 	 */
8693e086edfSolivier moysan 	slot_sz = slotr & SAI_XSLOTR_SLOTSZ_MASK;
8703e086edfSolivier moysan 	if (slot_sz == SAI_XSLOTR_SLOTSZ_SET(SAI_SLOT_SIZE_AUTO))
8713e086edfSolivier moysan 		sai->slot_width = sai->data_size;
8723e086edfSolivier moysan 
8733e086edfSolivier moysan 	if (sai->slot_width < sai->data_size) {
8743e086edfSolivier moysan 		dev_err(cpu_dai->dev,
8753e086edfSolivier moysan 			"Data size %d larger than slot width\n",
8763e086edfSolivier moysan 			sai->data_size);
8773e086edfSolivier moysan 		return -EINVAL;
8783e086edfSolivier moysan 	}
8793e086edfSolivier moysan 
8803e086edfSolivier moysan 	/* Slot number is set to 2, if not specified in DT */
8813e086edfSolivier moysan 	if (!sai->slots)
8823e086edfSolivier moysan 		sai->slots = 2;
8833e086edfSolivier moysan 
8843e086edfSolivier moysan 	/* The number of slots in the audio frame is equal to NBSLOT[3:0] + 1*/
885a14bf98cSOlivier Moysan 	stm32_sai_sub_reg_up(sai, STM_SAI_SLOTR_REGX,
8863e086edfSolivier moysan 			     SAI_XSLOTR_NBSLOT_MASK,
8873e086edfSolivier moysan 			     SAI_XSLOTR_NBSLOT_SET((sai->slots - 1)));
8883e086edfSolivier moysan 
8893e086edfSolivier moysan 	/* Set default slots mask if not already set from DT */
8903e086edfSolivier moysan 	if (!(slotr & SAI_XSLOTR_SLOTEN_MASK)) {
8913e086edfSolivier moysan 		sai->slot_mask = (1 << sai->slots) - 1;
892a14bf98cSOlivier Moysan 		stm32_sai_sub_reg_up(sai,
8933e086edfSolivier moysan 				     STM_SAI_SLOTR_REGX, SAI_XSLOTR_SLOTEN_MASK,
8943e086edfSolivier moysan 				     SAI_XSLOTR_SLOTEN_SET(sai->slot_mask));
8953e086edfSolivier moysan 	}
8963e086edfSolivier moysan 
897602fdadcSolivier moysan 	dev_dbg(cpu_dai->dev, "Slots %d, slot width %d\n",
8983e086edfSolivier moysan 		sai->slots, sai->slot_width);
8993e086edfSolivier moysan 
9003e086edfSolivier moysan 	return 0;
9013e086edfSolivier moysan }
9023e086edfSolivier moysan 
9033e086edfSolivier moysan static void stm32_sai_set_frame(struct snd_soc_dai *cpu_dai)
9043e086edfSolivier moysan {
9053e086edfSolivier moysan 	struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
9063e086edfSolivier moysan 	int fs_active, offset, format;
9073e086edfSolivier moysan 	int frcr, frcr_mask;
9083e086edfSolivier moysan 
9093e086edfSolivier moysan 	format = sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
9103e086edfSolivier moysan 	sai->fs_length = sai->slot_width * sai->slots;
9113e086edfSolivier moysan 
9123e086edfSolivier moysan 	fs_active = sai->fs_length / 2;
9133e086edfSolivier moysan 	if ((format == SND_SOC_DAIFMT_DSP_A) ||
9143e086edfSolivier moysan 	    (format == SND_SOC_DAIFMT_DSP_B))
9153e086edfSolivier moysan 		fs_active = 1;
9163e086edfSolivier moysan 
9173e086edfSolivier moysan 	frcr = SAI_XFRCR_FRL_SET((sai->fs_length - 1));
9183e086edfSolivier moysan 	frcr |= SAI_XFRCR_FSALL_SET((fs_active - 1));
9193e086edfSolivier moysan 	frcr_mask = SAI_XFRCR_FRL_MASK | SAI_XFRCR_FSALL_MASK;
9203e086edfSolivier moysan 
921602fdadcSolivier moysan 	dev_dbg(cpu_dai->dev, "Frame length %d, frame active %d\n",
9223e086edfSolivier moysan 		sai->fs_length, fs_active);
9233e086edfSolivier moysan 
924a14bf98cSOlivier Moysan 	stm32_sai_sub_reg_up(sai, STM_SAI_FRCR_REGX, frcr_mask, frcr);
9253e086edfSolivier moysan 
9263e086edfSolivier moysan 	if ((sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_LSB) {
9273e086edfSolivier moysan 		offset = sai->slot_width - sai->data_size;
9283e086edfSolivier moysan 
929a14bf98cSOlivier Moysan 		stm32_sai_sub_reg_up(sai, STM_SAI_SLOTR_REGX,
9303e086edfSolivier moysan 				     SAI_XSLOTR_FBOFF_MASK,
9313e086edfSolivier moysan 				     SAI_XSLOTR_FBOFF_SET(offset));
9323e086edfSolivier moysan 	}
9333e086edfSolivier moysan }
9343e086edfSolivier moysan 
935187e01d0Solivier moysan static void stm32_sai_init_iec958_status(struct stm32_sai_sub_data *sai)
936187e01d0Solivier moysan {
937187e01d0Solivier moysan 	unsigned char *cs = sai->iec958.status;
938187e01d0Solivier moysan 
939187e01d0Solivier moysan 	cs[0] = IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS_NONE;
940187e01d0Solivier moysan 	cs[1] = IEC958_AES1_CON_GENERAL;
941187e01d0Solivier moysan 	cs[2] = IEC958_AES2_CON_SOURCE_UNSPEC | IEC958_AES2_CON_CHANNEL_UNSPEC;
942187e01d0Solivier moysan 	cs[3] = IEC958_AES3_CON_CLOCK_1000PPM | IEC958_AES3_CON_FS_NOTID;
943187e01d0Solivier moysan }
944187e01d0Solivier moysan 
945187e01d0Solivier moysan static void stm32_sai_set_iec958_status(struct stm32_sai_sub_data *sai,
946187e01d0Solivier moysan 					struct snd_pcm_runtime *runtime)
947187e01d0Solivier moysan {
948187e01d0Solivier moysan 	if (!runtime)
949187e01d0Solivier moysan 		return;
950187e01d0Solivier moysan 
951187e01d0Solivier moysan 	/* Force the sample rate according to runtime rate */
952187e01d0Solivier moysan 	mutex_lock(&sai->ctrl_lock);
953187e01d0Solivier moysan 	switch (runtime->rate) {
954187e01d0Solivier moysan 	case 22050:
955187e01d0Solivier moysan 		sai->iec958.status[3] = IEC958_AES3_CON_FS_22050;
956187e01d0Solivier moysan 		break;
957187e01d0Solivier moysan 	case 44100:
958187e01d0Solivier moysan 		sai->iec958.status[3] = IEC958_AES3_CON_FS_44100;
959187e01d0Solivier moysan 		break;
960187e01d0Solivier moysan 	case 88200:
961187e01d0Solivier moysan 		sai->iec958.status[3] = IEC958_AES3_CON_FS_88200;
962187e01d0Solivier moysan 		break;
963187e01d0Solivier moysan 	case 176400:
964187e01d0Solivier moysan 		sai->iec958.status[3] = IEC958_AES3_CON_FS_176400;
965187e01d0Solivier moysan 		break;
966187e01d0Solivier moysan 	case 24000:
967187e01d0Solivier moysan 		sai->iec958.status[3] = IEC958_AES3_CON_FS_24000;
968187e01d0Solivier moysan 		break;
969187e01d0Solivier moysan 	case 48000:
970187e01d0Solivier moysan 		sai->iec958.status[3] = IEC958_AES3_CON_FS_48000;
971187e01d0Solivier moysan 		break;
972187e01d0Solivier moysan 	case 96000:
973187e01d0Solivier moysan 		sai->iec958.status[3] = IEC958_AES3_CON_FS_96000;
974187e01d0Solivier moysan 		break;
975187e01d0Solivier moysan 	case 192000:
976187e01d0Solivier moysan 		sai->iec958.status[3] = IEC958_AES3_CON_FS_192000;
977187e01d0Solivier moysan 		break;
978187e01d0Solivier moysan 	case 32000:
979187e01d0Solivier moysan 		sai->iec958.status[3] = IEC958_AES3_CON_FS_32000;
980187e01d0Solivier moysan 		break;
981187e01d0Solivier moysan 	default:
982187e01d0Solivier moysan 		sai->iec958.status[3] = IEC958_AES3_CON_FS_NOTID;
983187e01d0Solivier moysan 		break;
984187e01d0Solivier moysan 	}
985187e01d0Solivier moysan 	mutex_unlock(&sai->ctrl_lock);
986187e01d0Solivier moysan }
987187e01d0Solivier moysan 
9883e086edfSolivier moysan static int stm32_sai_configure_clock(struct snd_soc_dai *cpu_dai,
9893e086edfSolivier moysan 				     struct snd_pcm_hw_params *params)
9903e086edfSolivier moysan {
9913e086edfSolivier moysan 	struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
99271d9537fSOlivier Moysan 	int div = 0, cr1 = 0;
9938307b2afSOlivier Moysan 	int sai_clk_rate, mclk_ratio, den;
9946eb17d70SOlivier Moysan 	unsigned int rate = params_rate(params);
995e37c2deaSOlivier Moysan 	int ret;
9963e086edfSolivier moysan 
997e37c2deaSOlivier Moysan 	if (!sai->sai_mclk) {
998e37c2deaSOlivier Moysan 		ret = stm32_sai_set_parent_clock(sai, rate);
999e37c2deaSOlivier Moysan 		if (ret)
1000e37c2deaSOlivier Moysan 			return ret;
1001e37c2deaSOlivier Moysan 	}
10023e086edfSolivier moysan 	sai_clk_rate = clk_get_rate(sai->sai_ck);
10033e086edfSolivier moysan 
100403e78a24Solivier moysan 	if (STM_SAI_IS_F4(sai->pdata)) {
10058307b2afSOlivier Moysan 		/* mclk on (NODIV=0)
10063e086edfSolivier moysan 		 *   mclk_rate = 256 * fs
10073e086edfSolivier moysan 		 *   MCKDIV = 0 if sai_ck < 3/2 * mclk_rate
10083e086edfSolivier moysan 		 *   MCKDIV = sai_ck / (2 * mclk_rate) otherwise
10098307b2afSOlivier Moysan 		 * mclk off (NODIV=1)
10108307b2afSOlivier Moysan 		 *   MCKDIV ignored. sck = sai_ck
10113e086edfSolivier moysan 		 */
10128307b2afSOlivier Moysan 		if (!sai->mclk_rate)
10138307b2afSOlivier Moysan 			return 0;
10148307b2afSOlivier Moysan 
10158307b2afSOlivier Moysan 		if (2 * sai_clk_rate >= 3 * sai->mclk_rate) {
10168307b2afSOlivier Moysan 			div = stm32_sai_get_clk_div(sai, sai_clk_rate,
101703e78a24Solivier moysan 						    2 * sai->mclk_rate);
10188307b2afSOlivier Moysan 			if (div < 0)
10198307b2afSOlivier Moysan 				return div;
10208307b2afSOlivier Moysan 		}
102103e78a24Solivier moysan 	} else {
102203e78a24Solivier moysan 		/*
102303e78a24Solivier moysan 		 * TDM mode :
102403e78a24Solivier moysan 		 *   mclk on
102503e78a24Solivier moysan 		 *      MCKDIV = sai_ck / (ws x 256)	(NOMCK=0. OSR=0)
102603e78a24Solivier moysan 		 *      MCKDIV = sai_ck / (ws x 512)	(NOMCK=0. OSR=1)
102703e78a24Solivier moysan 		 *   mclk off
102803e78a24Solivier moysan 		 *      MCKDIV = sai_ck / (frl x ws)	(NOMCK=1)
102903e78a24Solivier moysan 		 * Note: NOMCK/NODIV correspond to same bit.
103003e78a24Solivier moysan 		 */
10316eb17d70SOlivier Moysan 		if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
10328307b2afSOlivier Moysan 			div = stm32_sai_get_clk_div(sai, sai_clk_rate,
10338307b2afSOlivier Moysan 						    rate * 128);
10348307b2afSOlivier Moysan 			if (div < 0)
10358307b2afSOlivier Moysan 				return div;
10366eb17d70SOlivier Moysan 		} else {
103703e78a24Solivier moysan 			if (sai->mclk_rate) {
10386eb17d70SOlivier Moysan 				mclk_ratio = sai->mclk_rate / rate;
103971d9537fSOlivier Moysan 				if (mclk_ratio == 512) {
104071d9537fSOlivier Moysan 					cr1 = SAI_XCR1_OSR;
104171d9537fSOlivier Moysan 				} else if (mclk_ratio != 256) {
104203e78a24Solivier moysan 					dev_err(cpu_dai->dev,
104303e78a24Solivier moysan 						"Wrong mclk ratio %d\n",
104403e78a24Solivier moysan 						mclk_ratio);
104503e78a24Solivier moysan 					return -EINVAL;
104603e78a24Solivier moysan 				}
104771d9537fSOlivier Moysan 
1048a14bf98cSOlivier Moysan 				stm32_sai_sub_reg_up(sai,
104971d9537fSOlivier Moysan 						     STM_SAI_CR1_REGX,
105071d9537fSOlivier Moysan 						     SAI_XCR1_OSR, cr1);
105171d9537fSOlivier Moysan 
10528307b2afSOlivier Moysan 				div = stm32_sai_get_clk_div(sai, sai_clk_rate,
10536eb17d70SOlivier Moysan 							    sai->mclk_rate);
10548307b2afSOlivier Moysan 				if (div < 0)
10558307b2afSOlivier Moysan 					return div;
105603e78a24Solivier moysan 			} else {
10576eb17d70SOlivier Moysan 				/* mclk-fs not set, master clock not active */
105803e78a24Solivier moysan 				den = sai->fs_length * params_rate(params);
10598307b2afSOlivier Moysan 				div = stm32_sai_get_clk_div(sai, sai_clk_rate,
10608307b2afSOlivier Moysan 							    den);
10618307b2afSOlivier Moysan 				if (div < 0)
10628307b2afSOlivier Moysan 					return div;
106303e78a24Solivier moysan 			}
106403e78a24Solivier moysan 		}
10656eb17d70SOlivier Moysan 	}
10663e086edfSolivier moysan 
10678307b2afSOlivier Moysan 	return stm32_sai_set_clk_div(sai, div);
10683e086edfSolivier moysan }
10693e086edfSolivier moysan 
10703e086edfSolivier moysan static int stm32_sai_hw_params(struct snd_pcm_substream *substream,
10713e086edfSolivier moysan 			       struct snd_pcm_hw_params *params,
10723e086edfSolivier moysan 			       struct snd_soc_dai *cpu_dai)
10733e086edfSolivier moysan {
10743e086edfSolivier moysan 	struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
10753e086edfSolivier moysan 	int ret;
10763e086edfSolivier moysan 
10773e086edfSolivier moysan 	sai->data_size = params_width(params);
10783e086edfSolivier moysan 
1079187e01d0Solivier moysan 	if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
1080187e01d0Solivier moysan 		/* Rate not already set in runtime structure */
1081187e01d0Solivier moysan 		substream->runtime->rate = params_rate(params);
1082187e01d0Solivier moysan 		stm32_sai_set_iec958_status(sai, substream->runtime);
1083187e01d0Solivier moysan 	} else {
10843e086edfSolivier moysan 		ret = stm32_sai_set_slots(cpu_dai);
10853e086edfSolivier moysan 		if (ret < 0)
10863e086edfSolivier moysan 			return ret;
10873e086edfSolivier moysan 		stm32_sai_set_frame(cpu_dai);
10886eb17d70SOlivier Moysan 	}
10893e086edfSolivier moysan 
10903e086edfSolivier moysan 	ret = stm32_sai_set_config(cpu_dai, substream, params);
10913e086edfSolivier moysan 	if (ret)
10923e086edfSolivier moysan 		return ret;
10933e086edfSolivier moysan 
10943e086edfSolivier moysan 	if (sai->master)
10953e086edfSolivier moysan 		ret = stm32_sai_configure_clock(cpu_dai, params);
10963e086edfSolivier moysan 
10973e086edfSolivier moysan 	return ret;
10983e086edfSolivier moysan }
10993e086edfSolivier moysan 
11003e086edfSolivier moysan static int stm32_sai_trigger(struct snd_pcm_substream *substream, int cmd,
11013e086edfSolivier moysan 			     struct snd_soc_dai *cpu_dai)
11023e086edfSolivier moysan {
11033e086edfSolivier moysan 	struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
11043e086edfSolivier moysan 	int ret;
11053e086edfSolivier moysan 
11063e086edfSolivier moysan 	switch (cmd) {
11073e086edfSolivier moysan 	case SNDRV_PCM_TRIGGER_START:
11083e086edfSolivier moysan 	case SNDRV_PCM_TRIGGER_RESUME:
11093e086edfSolivier moysan 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
11103e086edfSolivier moysan 		dev_dbg(cpu_dai->dev, "Enable DMA and SAI\n");
11113e086edfSolivier moysan 
1112a14bf98cSOlivier Moysan 		stm32_sai_sub_reg_up(sai, STM_SAI_CR1_REGX,
11133e086edfSolivier moysan 				     SAI_XCR1_DMAEN, SAI_XCR1_DMAEN);
11143e086edfSolivier moysan 
11153e086edfSolivier moysan 		/* Enable SAI */
1116a14bf98cSOlivier Moysan 		ret = stm32_sai_sub_reg_up(sai, STM_SAI_CR1_REGX,
11173e086edfSolivier moysan 					   SAI_XCR1_SAIEN, SAI_XCR1_SAIEN);
11183e086edfSolivier moysan 		if (ret < 0)
11193e086edfSolivier moysan 			dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
11203e086edfSolivier moysan 		break;
11213e086edfSolivier moysan 	case SNDRV_PCM_TRIGGER_SUSPEND:
11223e086edfSolivier moysan 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
11233e086edfSolivier moysan 	case SNDRV_PCM_TRIGGER_STOP:
11243e086edfSolivier moysan 		dev_dbg(cpu_dai->dev, "Disable DMA and SAI\n");
11253e086edfSolivier moysan 
1126a14bf98cSOlivier Moysan 		stm32_sai_sub_reg_up(sai, STM_SAI_IMR_REGX,
112747a8907dSOlivier Moysan 				     SAI_XIMR_MASK, 0);
112847a8907dSOlivier Moysan 
1129a14bf98cSOlivier Moysan 		stm32_sai_sub_reg_up(sai, STM_SAI_CR1_REGX,
11303e086edfSolivier moysan 				     SAI_XCR1_SAIEN,
11313e086edfSolivier moysan 				     (unsigned int)~SAI_XCR1_SAIEN);
11324fa17938Solivier moysan 
1133a14bf98cSOlivier Moysan 		ret = stm32_sai_sub_reg_up(sai, STM_SAI_CR1_REGX,
11344fa17938Solivier moysan 					   SAI_XCR1_DMAEN,
11354fa17938Solivier moysan 					   (unsigned int)~SAI_XCR1_DMAEN);
11363e086edfSolivier moysan 		if (ret < 0)
11373e086edfSolivier moysan 			dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
11386eb17d70SOlivier Moysan 
11396eb17d70SOlivier Moysan 		if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
11406eb17d70SOlivier Moysan 			sai->spdif_frm_cnt = 0;
11413e086edfSolivier moysan 		break;
11423e086edfSolivier moysan 	default:
11433e086edfSolivier moysan 		return -EINVAL;
11443e086edfSolivier moysan 	}
11453e086edfSolivier moysan 
11463e086edfSolivier moysan 	return ret;
11473e086edfSolivier moysan }
11483e086edfSolivier moysan 
11493e086edfSolivier moysan static void stm32_sai_shutdown(struct snd_pcm_substream *substream,
11503e086edfSolivier moysan 			       struct snd_soc_dai *cpu_dai)
11513e086edfSolivier moysan {
11523e086edfSolivier moysan 	struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
115326f98e82SOlivier Moysan 	unsigned long flags;
11543e086edfSolivier moysan 
1155a14bf98cSOlivier Moysan 	stm32_sai_sub_reg_up(sai, STM_SAI_IMR_REGX, SAI_XIMR_MASK, 0);
11563e086edfSolivier moysan 
1157e37c2deaSOlivier Moysan 	clk_disable_unprepare(sai->sai_ck);
11588307b2afSOlivier Moysan 
115926f98e82SOlivier Moysan 	spin_lock_irqsave(&sai->irq_lock, flags);
11603e086edfSolivier moysan 	sai->substream = NULL;
116126f98e82SOlivier Moysan 	spin_unlock_irqrestore(&sai->irq_lock, flags);
11623e086edfSolivier moysan }
11633e086edfSolivier moysan 
1164187e01d0Solivier moysan static int stm32_sai_pcm_new(struct snd_soc_pcm_runtime *rtd,
1165187e01d0Solivier moysan 			     struct snd_soc_dai *cpu_dai)
1166187e01d0Solivier moysan {
1167187e01d0Solivier moysan 	struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
11685f8a1000SOlivier Moysan 	struct snd_kcontrol_new knew = iec958_ctls;
1169187e01d0Solivier moysan 
1170187e01d0Solivier moysan 	if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
1171187e01d0Solivier moysan 		dev_dbg(&sai->pdev->dev, "%s: register iec controls", __func__);
11725f8a1000SOlivier Moysan 		knew.device = rtd->pcm->device;
11735f8a1000SOlivier Moysan 		return snd_ctl_add(rtd->pcm->card, snd_ctl_new1(&knew, sai));
1174187e01d0Solivier moysan 	}
1175187e01d0Solivier moysan 
1176187e01d0Solivier moysan 	return 0;
1177187e01d0Solivier moysan }
1178187e01d0Solivier moysan 
11793e086edfSolivier moysan static int stm32_sai_dai_probe(struct snd_soc_dai *cpu_dai)
11803e086edfSolivier moysan {
11813e086edfSolivier moysan 	struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
1182d4180b4cSOlivier Moysan 	int cr1 = 0, cr1_mask, ret;
11833e086edfSolivier moysan 
11848307b2afSOlivier Moysan 	sai->cpu_dai = cpu_dai;
11858307b2afSOlivier Moysan 
11863e086edfSolivier moysan 	sai->dma_params.addr = (dma_addr_t)(sai->phys_addr + STM_SAI_DR_REGX);
1187a4529d2bSOlivier Moysan 	/*
1188a4529d2bSOlivier Moysan 	 * DMA supports 4, 8 or 16 burst sizes. Burst size 4 is the best choice,
1189a4529d2bSOlivier Moysan 	 * as it allows bytes, half-word and words transfers. (See DMA fifos
1190a4529d2bSOlivier Moysan 	 * constraints).
1191a4529d2bSOlivier Moysan 	 */
1192a4529d2bSOlivier Moysan 	sai->dma_params.maxburst = 4;
11931d9c95c1SOlivier Moysan 	if (sai->pdata->conf.fifo_size < 8)
11941d9c95c1SOlivier Moysan 		sai->dma_params.maxburst = 1;
11953e086edfSolivier moysan 	/* Buswidth will be set by framework at runtime */
11963e086edfSolivier moysan 	sai->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
11973e086edfSolivier moysan 
11983e086edfSolivier moysan 	if (STM_SAI_IS_PLAYBACK(sai))
11993e086edfSolivier moysan 		snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params, NULL);
12003e086edfSolivier moysan 	else
12013e086edfSolivier moysan 		snd_soc_dai_init_dma_data(cpu_dai, NULL, &sai->dma_params);
12023e086edfSolivier moysan 
1203187e01d0Solivier moysan 	/* Next settings are not relevant for spdif mode */
1204187e01d0Solivier moysan 	if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
1205187e01d0Solivier moysan 		return 0;
1206187e01d0Solivier moysan 
120761fb4ff7SOlivier Moysan 	cr1_mask = SAI_XCR1_RX_TX;
120861fb4ff7SOlivier Moysan 	if (STM_SAI_IS_CAPTURE(sai))
120961fb4ff7SOlivier Moysan 		cr1 |= SAI_XCR1_RX_TX;
121061fb4ff7SOlivier Moysan 
12115914d285SOlivier Moysan 	/* Configure synchronization */
12125914d285SOlivier Moysan 	if (sai->sync == SAI_SYNC_EXTERNAL) {
12135914d285SOlivier Moysan 		/* Configure synchro client and provider */
1214d4180b4cSOlivier Moysan 		ret = sai->pdata->set_sync(sai->pdata, sai->np_sync_provider,
12155914d285SOlivier Moysan 					   sai->synco, sai->synci);
1216d4180b4cSOlivier Moysan 		if (ret)
1217d4180b4cSOlivier Moysan 			return ret;
12185914d285SOlivier Moysan 	}
12195914d285SOlivier Moysan 
12205914d285SOlivier Moysan 	cr1_mask |= SAI_XCR1_SYNCEN_MASK;
12215914d285SOlivier Moysan 	cr1 |= SAI_XCR1_SYNCEN_SET(sai->sync);
12225914d285SOlivier Moysan 
1223a14bf98cSOlivier Moysan 	return stm32_sai_sub_reg_up(sai, STM_SAI_CR1_REGX, cr1_mask, cr1);
12243e086edfSolivier moysan }
12253e086edfSolivier moysan 
12263e086edfSolivier moysan static const struct snd_soc_dai_ops stm32_sai_pcm_dai_ops = {
12273e086edfSolivier moysan 	.set_sysclk	= stm32_sai_set_sysclk,
12283e086edfSolivier moysan 	.set_fmt	= stm32_sai_set_dai_fmt,
12293e086edfSolivier moysan 	.set_tdm_slot	= stm32_sai_set_dai_tdm_slot,
12303e086edfSolivier moysan 	.startup	= stm32_sai_startup,
12313e086edfSolivier moysan 	.hw_params	= stm32_sai_hw_params,
12323e086edfSolivier moysan 	.trigger	= stm32_sai_trigger,
12333e086edfSolivier moysan 	.shutdown	= stm32_sai_shutdown,
12343e086edfSolivier moysan };
12353e086edfSolivier moysan 
12366eb17d70SOlivier Moysan static int stm32_sai_pcm_process_spdif(struct snd_pcm_substream *substream,
12376eb17d70SOlivier Moysan 				       int channel, unsigned long hwoff,
12386eb17d70SOlivier Moysan 				       void *buf, unsigned long bytes)
12396eb17d70SOlivier Moysan {
12406eb17d70SOlivier Moysan 	struct snd_pcm_runtime *runtime = substream->runtime;
1241ddb4f06dSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1242b1bee67cSKuninori Morimoto 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
12436eb17d70SOlivier Moysan 	struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
12446eb17d70SOlivier Moysan 	int *ptr = (int *)(runtime->dma_area + hwoff +
12456eb17d70SOlivier Moysan 			   channel * (runtime->dma_bytes / runtime->channels));
12466eb17d70SOlivier Moysan 	ssize_t cnt = bytes_to_samples(runtime, bytes);
12476eb17d70SOlivier Moysan 	unsigned int frm_cnt = sai->spdif_frm_cnt;
12486eb17d70SOlivier Moysan 	unsigned int byte;
12496eb17d70SOlivier Moysan 	unsigned int mask;
12506eb17d70SOlivier Moysan 
12516eb17d70SOlivier Moysan 	do {
12526eb17d70SOlivier Moysan 		*ptr = ((*ptr >> 8) & 0x00ffffff);
12536eb17d70SOlivier Moysan 
12546eb17d70SOlivier Moysan 		/* Set channel status bit */
12556eb17d70SOlivier Moysan 		byte = frm_cnt >> 3;
12566eb17d70SOlivier Moysan 		mask = 1 << (frm_cnt - (byte << 3));
1257187e01d0Solivier moysan 		if (sai->iec958.status[byte] & mask)
12586eb17d70SOlivier Moysan 			*ptr |= 0x04000000;
12596eb17d70SOlivier Moysan 		ptr++;
12606eb17d70SOlivier Moysan 
12616eb17d70SOlivier Moysan 		if (!(cnt % 2))
12626eb17d70SOlivier Moysan 			frm_cnt++;
12636eb17d70SOlivier Moysan 
12646eb17d70SOlivier Moysan 		if (frm_cnt == SAI_IEC60958_BLOCK_FRAMES)
12656eb17d70SOlivier Moysan 			frm_cnt = 0;
12666eb17d70SOlivier Moysan 	} while (--cnt);
12676eb17d70SOlivier Moysan 	sai->spdif_frm_cnt = frm_cnt;
12686eb17d70SOlivier Moysan 
12696eb17d70SOlivier Moysan 	return 0;
12706eb17d70SOlivier Moysan }
12716eb17d70SOlivier Moysan 
1272eaf072e5SOlivier Moysan /* No support of mmap in S/PDIF mode */
1273eaf072e5SOlivier Moysan static const struct snd_pcm_hardware stm32_sai_pcm_hw_spdif = {
1274eaf072e5SOlivier Moysan 	.info = SNDRV_PCM_INFO_INTERLEAVED,
1275eaf072e5SOlivier Moysan 	.buffer_bytes_max = 8 * PAGE_SIZE,
1276eaf072e5SOlivier Moysan 	.period_bytes_min = 1024,
1277eaf072e5SOlivier Moysan 	.period_bytes_max = PAGE_SIZE,
1278eaf072e5SOlivier Moysan 	.periods_min = 2,
1279eaf072e5SOlivier Moysan 	.periods_max = 8,
1280eaf072e5SOlivier Moysan };
1281eaf072e5SOlivier Moysan 
12823e086edfSolivier moysan static const struct snd_pcm_hardware stm32_sai_pcm_hw = {
12833e086edfSolivier moysan 	.info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP,
12843e086edfSolivier moysan 	.buffer_bytes_max = 8 * PAGE_SIZE,
12853e086edfSolivier moysan 	.period_bytes_min = 1024, /* 5ms at 48kHz */
12863e086edfSolivier moysan 	.period_bytes_max = PAGE_SIZE,
12873e086edfSolivier moysan 	.periods_min = 2,
12883e086edfSolivier moysan 	.periods_max = 8,
12893e086edfSolivier moysan };
12903e086edfSolivier moysan 
12918f8a5488SArnaud Pouliquen static struct snd_soc_dai_driver stm32_sai_playback_dai = {
12923e086edfSolivier moysan 		.probe = stm32_sai_dai_probe,
1293187e01d0Solivier moysan 		.pcm_new = stm32_sai_pcm_new,
12943e086edfSolivier moysan 		.id = 1, /* avoid call to fmt_single_name() */
12953e086edfSolivier moysan 		.playback = {
12963e086edfSolivier moysan 			.channels_min = 1,
1297*7fabe7feSOlivier Moysan 			.channels_max = 16,
12983e086edfSolivier moysan 			.rate_min = 8000,
12993e086edfSolivier moysan 			.rate_max = 192000,
13003e086edfSolivier moysan 			.rates = SNDRV_PCM_RATE_CONTINUOUS,
13013e086edfSolivier moysan 			/* DMA does not support 24 bits transfers */
13023e086edfSolivier moysan 			.formats =
13033e086edfSolivier moysan 				SNDRV_PCM_FMTBIT_S8 |
13043e086edfSolivier moysan 				SNDRV_PCM_FMTBIT_S16_LE |
13053e086edfSolivier moysan 				SNDRV_PCM_FMTBIT_S32_LE,
13063e086edfSolivier moysan 		},
13073e086edfSolivier moysan 		.ops = &stm32_sai_pcm_dai_ops,
13083e086edfSolivier moysan };
13093e086edfSolivier moysan 
13108f8a5488SArnaud Pouliquen static struct snd_soc_dai_driver stm32_sai_capture_dai = {
13113e086edfSolivier moysan 		.probe = stm32_sai_dai_probe,
13123e086edfSolivier moysan 		.id = 1, /* avoid call to fmt_single_name() */
13133e086edfSolivier moysan 		.capture = {
13143e086edfSolivier moysan 			.channels_min = 1,
1315*7fabe7feSOlivier Moysan 			.channels_max = 16,
13163e086edfSolivier moysan 			.rate_min = 8000,
13173e086edfSolivier moysan 			.rate_max = 192000,
13183e086edfSolivier moysan 			.rates = SNDRV_PCM_RATE_CONTINUOUS,
13193e086edfSolivier moysan 			/* DMA does not support 24 bits transfers */
13203e086edfSolivier moysan 			.formats =
13213e086edfSolivier moysan 				SNDRV_PCM_FMTBIT_S8 |
13223e086edfSolivier moysan 				SNDRV_PCM_FMTBIT_S16_LE |
13233e086edfSolivier moysan 				SNDRV_PCM_FMTBIT_S32_LE,
13243e086edfSolivier moysan 		},
13253e086edfSolivier moysan 		.ops = &stm32_sai_pcm_dai_ops,
13263e086edfSolivier moysan };
13273e086edfSolivier moysan 
13283e086edfSolivier moysan static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config = {
13293e086edfSolivier moysan 	.pcm_hardware = &stm32_sai_pcm_hw,
13303e086edfSolivier moysan 	.prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
13313e086edfSolivier moysan };
13323e086edfSolivier moysan 
13336eb17d70SOlivier Moysan static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config_spdif = {
1334eaf072e5SOlivier Moysan 	.pcm_hardware = &stm32_sai_pcm_hw_spdif,
13356eb17d70SOlivier Moysan 	.prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
13366eb17d70SOlivier Moysan 	.process = stm32_sai_pcm_process_spdif,
13376eb17d70SOlivier Moysan };
13386eb17d70SOlivier Moysan 
13393e086edfSolivier moysan static const struct snd_soc_component_driver stm32_component = {
13403e086edfSolivier moysan 	.name = "stm32-sai",
13413e086edfSolivier moysan };
13423e086edfSolivier moysan 
13433e086edfSolivier moysan static const struct of_device_id stm32_sai_sub_ids[] = {
13443e086edfSolivier moysan 	{ .compatible = "st,stm32-sai-sub-a",
13453e086edfSolivier moysan 	  .data = (void *)STM_SAI_A_ID},
13463e086edfSolivier moysan 	{ .compatible = "st,stm32-sai-sub-b",
13473e086edfSolivier moysan 	  .data = (void *)STM_SAI_B_ID},
13483e086edfSolivier moysan 	{}
13493e086edfSolivier moysan };
13503e086edfSolivier moysan MODULE_DEVICE_TABLE(of, stm32_sai_sub_ids);
13513e086edfSolivier moysan 
13523e086edfSolivier moysan static int stm32_sai_sub_parse_of(struct platform_device *pdev,
13533e086edfSolivier moysan 				  struct stm32_sai_sub_data *sai)
13543e086edfSolivier moysan {
13553e086edfSolivier moysan 	struct device_node *np = pdev->dev.of_node;
13563e086edfSolivier moysan 	struct resource *res;
13573e086edfSolivier moysan 	void __iomem *base;
13585914d285SOlivier Moysan 	struct of_phandle_args args;
13595914d285SOlivier Moysan 	int ret;
13603e086edfSolivier moysan 
13613e086edfSolivier moysan 	if (!np)
13623e086edfSolivier moysan 		return -ENODEV;
13633e086edfSolivier moysan 
1364003ee640SYang Yingliang 	base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
13653e086edfSolivier moysan 	if (IS_ERR(base))
13663e086edfSolivier moysan 		return PTR_ERR(base);
13673e086edfSolivier moysan 
13683e086edfSolivier moysan 	sai->phys_addr = res->start;
136903e78a24Solivier moysan 
137003e78a24Solivier moysan 	sai->regmap_config = &stm32_sai_sub_regmap_config_f4;
13711d9c95c1SOlivier Moysan 	/* Note: PDM registers not available for sub-block B */
13721d9c95c1SOlivier Moysan 	if (STM_SAI_HAS_PDM(sai) && STM_SAI_IS_SUB_A(sai))
137303e78a24Solivier moysan 		sai->regmap_config = &stm32_sai_sub_regmap_config_h7;
137403e78a24Solivier moysan 
1375a14bf98cSOlivier Moysan 	/*
1376a14bf98cSOlivier Moysan 	 * Do not manage peripheral clock through regmap framework as this
1377a14bf98cSOlivier Moysan 	 * can lead to circular locking issue with sai master clock provider.
1378a14bf98cSOlivier Moysan 	 * Manage peripheral clock directly in driver instead.
1379a14bf98cSOlivier Moysan 	 */
1380a14bf98cSOlivier Moysan 	sai->regmap = devm_regmap_init_mmio(&pdev->dev, base,
1381a14bf98cSOlivier Moysan 					    sai->regmap_config);
138203e78a24Solivier moysan 	if (IS_ERR(sai->regmap)) {
13835183e854SOlivier Moysan 		if (PTR_ERR(sai->regmap) != -EPROBE_DEFER)
13845183e854SOlivier Moysan 			dev_err(&pdev->dev, "Regmap init error %ld\n",
13855183e854SOlivier Moysan 				PTR_ERR(sai->regmap));
138603e78a24Solivier moysan 		return PTR_ERR(sai->regmap);
138703e78a24Solivier moysan 	}
13883e086edfSolivier moysan 
13893e086edfSolivier moysan 	/* Get direction property */
13903e086edfSolivier moysan 	if (of_property_match_string(np, "dma-names", "tx") >= 0) {
13913e086edfSolivier moysan 		sai->dir = SNDRV_PCM_STREAM_PLAYBACK;
13923e086edfSolivier moysan 	} else if (of_property_match_string(np, "dma-names", "rx") >= 0) {
13933e086edfSolivier moysan 		sai->dir = SNDRV_PCM_STREAM_CAPTURE;
13943e086edfSolivier moysan 	} else {
13953e086edfSolivier moysan 		dev_err(&pdev->dev, "Unsupported direction\n");
13963e086edfSolivier moysan 		return -EINVAL;
13973e086edfSolivier moysan 	}
13983e086edfSolivier moysan 
13996eb17d70SOlivier Moysan 	/* Get spdif iec60958 property */
14006eb17d70SOlivier Moysan 	sai->spdif = false;
14016eb17d70SOlivier Moysan 	if (of_get_property(np, "st,iec60958", NULL)) {
14026eb17d70SOlivier Moysan 		if (!STM_SAI_HAS_SPDIF(sai) ||
14036eb17d70SOlivier Moysan 		    sai->dir == SNDRV_PCM_STREAM_CAPTURE) {
14046eb17d70SOlivier Moysan 			dev_err(&pdev->dev, "S/PDIF IEC60958 not supported\n");
14056eb17d70SOlivier Moysan 			return -EINVAL;
14066eb17d70SOlivier Moysan 		}
1407187e01d0Solivier moysan 		stm32_sai_init_iec958_status(sai);
14086eb17d70SOlivier Moysan 		sai->spdif = true;
14096eb17d70SOlivier Moysan 		sai->master = true;
14106eb17d70SOlivier Moysan 	}
14116eb17d70SOlivier Moysan 
14125914d285SOlivier Moysan 	/* Get synchronization property */
14135914d285SOlivier Moysan 	args.np = NULL;
14145914d285SOlivier Moysan 	ret = of_parse_phandle_with_fixed_args(np, "st,sync", 1, 0, &args);
14155914d285SOlivier Moysan 	if (ret < 0  && ret != -ENOENT) {
14165914d285SOlivier Moysan 		dev_err(&pdev->dev, "Failed to get st,sync property\n");
14175914d285SOlivier Moysan 		return ret;
14185914d285SOlivier Moysan 	}
14195914d285SOlivier Moysan 
14205914d285SOlivier Moysan 	sai->sync = SAI_SYNC_NONE;
14215914d285SOlivier Moysan 	if (args.np) {
14225914d285SOlivier Moysan 		if (args.np == np) {
14235d585e1eSRob Herring 			dev_err(&pdev->dev, "%pOFn sync own reference\n", np);
14245914d285SOlivier Moysan 			of_node_put(args.np);
14255914d285SOlivier Moysan 			return -EINVAL;
14265914d285SOlivier Moysan 		}
14275914d285SOlivier Moysan 
14285914d285SOlivier Moysan 		sai->np_sync_provider  = of_get_parent(args.np);
14295914d285SOlivier Moysan 		if (!sai->np_sync_provider) {
14305d585e1eSRob Herring 			dev_err(&pdev->dev, "%pOFn parent node not found\n",
14315d585e1eSRob Herring 				np);
14325914d285SOlivier Moysan 			of_node_put(args.np);
14335914d285SOlivier Moysan 			return -ENODEV;
14345914d285SOlivier Moysan 		}
14355914d285SOlivier Moysan 
14365914d285SOlivier Moysan 		sai->sync = SAI_SYNC_INTERNAL;
14375914d285SOlivier Moysan 		if (sai->np_sync_provider != sai->pdata->pdev->dev.of_node) {
14385914d285SOlivier Moysan 			if (!STM_SAI_HAS_EXT_SYNC(sai)) {
14395914d285SOlivier Moysan 				dev_err(&pdev->dev,
14405914d285SOlivier Moysan 					"External synchro not supported\n");
14415914d285SOlivier Moysan 				of_node_put(args.np);
14425914d285SOlivier Moysan 				return -EINVAL;
14435914d285SOlivier Moysan 			}
14445914d285SOlivier Moysan 			sai->sync = SAI_SYNC_EXTERNAL;
14455914d285SOlivier Moysan 
14465914d285SOlivier Moysan 			sai->synci = args.args[0];
14475914d285SOlivier Moysan 			if (sai->synci < 1 ||
14485914d285SOlivier Moysan 			    (sai->synci > (SAI_GCR_SYNCIN_MAX + 1))) {
14495914d285SOlivier Moysan 				dev_err(&pdev->dev, "Wrong SAI index\n");
14505914d285SOlivier Moysan 				of_node_put(args.np);
14515914d285SOlivier Moysan 				return -EINVAL;
14525914d285SOlivier Moysan 			}
14535914d285SOlivier Moysan 
14545914d285SOlivier Moysan 			if (of_property_match_string(args.np, "compatible",
14555914d285SOlivier Moysan 						     "st,stm32-sai-sub-a") >= 0)
14565914d285SOlivier Moysan 				sai->synco = STM_SAI_SYNC_OUT_A;
14575914d285SOlivier Moysan 
14585914d285SOlivier Moysan 			if (of_property_match_string(args.np, "compatible",
14595914d285SOlivier Moysan 						     "st,stm32-sai-sub-b") >= 0)
14605914d285SOlivier Moysan 				sai->synco = STM_SAI_SYNC_OUT_B;
14615914d285SOlivier Moysan 
14625914d285SOlivier Moysan 			if (!sai->synco) {
14635914d285SOlivier Moysan 				dev_err(&pdev->dev, "Unknown SAI sub-block\n");
14645914d285SOlivier Moysan 				of_node_put(args.np);
14655914d285SOlivier Moysan 				return -EINVAL;
14665914d285SOlivier Moysan 			}
14675914d285SOlivier Moysan 		}
14685914d285SOlivier Moysan 
14695914d285SOlivier Moysan 		dev_dbg(&pdev->dev, "%s synchronized with %s\n",
14705914d285SOlivier Moysan 			pdev->name, args.np->full_name);
14715914d285SOlivier Moysan 	}
14725914d285SOlivier Moysan 
14735914d285SOlivier Moysan 	of_node_put(args.np);
14743e086edfSolivier moysan 	sai->sai_ck = devm_clk_get(&pdev->dev, "sai_ck");
14753e086edfSolivier moysan 	if (IS_ERR(sai->sai_ck)) {
14765183e854SOlivier Moysan 		if (PTR_ERR(sai->sai_ck) != -EPROBE_DEFER)
14775183e854SOlivier Moysan 			dev_err(&pdev->dev, "Missing kernel clock sai_ck: %ld\n",
14785183e854SOlivier Moysan 				PTR_ERR(sai->sai_ck));
14793e086edfSolivier moysan 		return PTR_ERR(sai->sai_ck);
14803e086edfSolivier moysan 	}
14813e086edfSolivier moysan 
1482a14bf98cSOlivier Moysan 	ret = clk_prepare(sai->pdata->pclk);
1483a14bf98cSOlivier Moysan 	if (ret < 0)
1484a14bf98cSOlivier Moysan 		return ret;
1485a14bf98cSOlivier Moysan 
14868307b2afSOlivier Moysan 	if (STM_SAI_IS_F4(sai->pdata))
14878307b2afSOlivier Moysan 		return 0;
14888307b2afSOlivier Moysan 
14898307b2afSOlivier Moysan 	/* Register mclk provider if requested */
14908307b2afSOlivier Moysan 	if (of_find_property(np, "#clock-cells", NULL)) {
14918307b2afSOlivier Moysan 		ret = stm32_sai_add_mclk_provider(sai);
14928307b2afSOlivier Moysan 		if (ret < 0)
14938307b2afSOlivier Moysan 			return ret;
14948307b2afSOlivier Moysan 	} else {
14958307b2afSOlivier Moysan 		sai->sai_mclk = devm_clk_get(&pdev->dev, "MCLK");
14968307b2afSOlivier Moysan 		if (IS_ERR(sai->sai_mclk)) {
14978307b2afSOlivier Moysan 			if (PTR_ERR(sai->sai_mclk) != -ENOENT)
14988307b2afSOlivier Moysan 				return PTR_ERR(sai->sai_mclk);
14998307b2afSOlivier Moysan 			sai->sai_mclk = NULL;
15008307b2afSOlivier Moysan 		}
15018307b2afSOlivier Moysan 	}
15028307b2afSOlivier Moysan 
15033e086edfSolivier moysan 	return 0;
15043e086edfSolivier moysan }
15053e086edfSolivier moysan 
15063e086edfSolivier moysan static int stm32_sai_sub_probe(struct platform_device *pdev)
15073e086edfSolivier moysan {
15083e086edfSolivier moysan 	struct stm32_sai_sub_data *sai;
15093e086edfSolivier moysan 	const struct of_device_id *of_id;
15106eb17d70SOlivier Moysan 	const struct snd_dmaengine_pcm_config *conf = &stm32_sai_pcm_config;
15113e086edfSolivier moysan 	int ret;
15123e086edfSolivier moysan 
15133e086edfSolivier moysan 	sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
15143e086edfSolivier moysan 	if (!sai)
15153e086edfSolivier moysan 		return -ENOMEM;
15163e086edfSolivier moysan 
15173e086edfSolivier moysan 	of_id = of_match_device(stm32_sai_sub_ids, &pdev->dev);
15183e086edfSolivier moysan 	if (!of_id)
15193e086edfSolivier moysan 		return -EINVAL;
15203e086edfSolivier moysan 	sai->id = (uintptr_t)of_id->data;
15213e086edfSolivier moysan 
15223e086edfSolivier moysan 	sai->pdev = pdev;
1523187e01d0Solivier moysan 	mutex_init(&sai->ctrl_lock);
152426f98e82SOlivier Moysan 	spin_lock_init(&sai->irq_lock);
15253e086edfSolivier moysan 	platform_set_drvdata(pdev, sai);
15263e086edfSolivier moysan 
15273e086edfSolivier moysan 	sai->pdata = dev_get_drvdata(pdev->dev.parent);
15283e086edfSolivier moysan 	if (!sai->pdata) {
15293e086edfSolivier moysan 		dev_err(&pdev->dev, "Parent device data not available\n");
15303e086edfSolivier moysan 		return -EINVAL;
15313e086edfSolivier moysan 	}
15323e086edfSolivier moysan 
15333e086edfSolivier moysan 	ret = stm32_sai_sub_parse_of(pdev, sai);
15343e086edfSolivier moysan 	if (ret)
15353e086edfSolivier moysan 		return ret;
15363e086edfSolivier moysan 
15378f8a5488SArnaud Pouliquen 	if (STM_SAI_IS_PLAYBACK(sai))
15388f8a5488SArnaud Pouliquen 		sai->cpu_dai_drv = stm32_sai_playback_dai;
15398f8a5488SArnaud Pouliquen 	else
15408f8a5488SArnaud Pouliquen 		sai->cpu_dai_drv = stm32_sai_capture_dai;
15418f8a5488SArnaud Pouliquen 	sai->cpu_dai_drv.name = dev_name(&pdev->dev);
15423e086edfSolivier moysan 
15433e086edfSolivier moysan 	ret = devm_request_irq(&pdev->dev, sai->pdata->irq, stm32_sai_isr,
15443e086edfSolivier moysan 			       IRQF_SHARED, dev_name(&pdev->dev), sai);
15453e086edfSolivier moysan 	if (ret) {
1546602fdadcSolivier moysan 		dev_err(&pdev->dev, "IRQ request returned %d\n", ret);
15473e086edfSolivier moysan 		return ret;
15483e086edfSolivier moysan 	}
15493e086edfSolivier moysan 
1550e2bcb657SOlivier Moysan 	if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
1551e2bcb657SOlivier Moysan 		conf = &stm32_sai_pcm_config_spdif;
1552e2bcb657SOlivier Moysan 
15530d6defc7SOlivier Moysan 	ret = snd_dmaengine_pcm_register(&pdev->dev, conf, 0);
15540d6defc7SOlivier Moysan 	if (ret) {
1555cc6eb9b5SMark Brown 		if (ret != -EPROBE_DEFER)
15560d6defc7SOlivier Moysan 			dev_err(&pdev->dev, "Could not register pcm dma\n");
15570d6defc7SOlivier Moysan 		return ret;
15580d6defc7SOlivier Moysan 	}
15590d6defc7SOlivier Moysan 
15600d6defc7SOlivier Moysan 	ret = snd_soc_register_component(&pdev->dev, &stm32_component,
15618f8a5488SArnaud Pouliquen 					 &sai->cpu_dai_drv, 1);
15624e723e75SOlivier Moysan 	if (ret) {
15637506baeeSJulia Lawall 		snd_dmaengine_pcm_unregister(&pdev->dev);
15643e086edfSolivier moysan 		return ret;
15657506baeeSJulia Lawall 	}
15663e086edfSolivier moysan 
15674e723e75SOlivier Moysan 	pm_runtime_enable(&pdev->dev);
15684e723e75SOlivier Moysan 
15694e723e75SOlivier Moysan 	return 0;
15704e723e75SOlivier Moysan }
15714e723e75SOlivier Moysan 
1572a14bf98cSOlivier Moysan static int stm32_sai_sub_remove(struct platform_device *pdev)
1573a14bf98cSOlivier Moysan {
1574a14bf98cSOlivier Moysan 	struct stm32_sai_sub_data *sai = dev_get_drvdata(&pdev->dev);
1575a14bf98cSOlivier Moysan 
1576a14bf98cSOlivier Moysan 	clk_unprepare(sai->pdata->pclk);
15770d6defc7SOlivier Moysan 	snd_dmaengine_pcm_unregister(&pdev->dev);
15780d6defc7SOlivier Moysan 	snd_soc_unregister_component(&pdev->dev);
15794e723e75SOlivier Moysan 	pm_runtime_disable(&pdev->dev);
1580a14bf98cSOlivier Moysan 
1581a14bf98cSOlivier Moysan 	return 0;
1582a14bf98cSOlivier Moysan }
1583a14bf98cSOlivier Moysan 
1584cf881773SOlivier Moysan #ifdef CONFIG_PM_SLEEP
1585cf881773SOlivier Moysan static int stm32_sai_sub_suspend(struct device *dev)
1586cf881773SOlivier Moysan {
1587cf881773SOlivier Moysan 	struct stm32_sai_sub_data *sai = dev_get_drvdata(dev);
1588a14bf98cSOlivier Moysan 	int ret;
1589a14bf98cSOlivier Moysan 
1590a14bf98cSOlivier Moysan 	ret = clk_enable(sai->pdata->pclk);
1591a14bf98cSOlivier Moysan 	if (ret < 0)
1592a14bf98cSOlivier Moysan 		return ret;
1593cf881773SOlivier Moysan 
1594cf881773SOlivier Moysan 	regcache_cache_only(sai->regmap, true);
1595cf881773SOlivier Moysan 	regcache_mark_dirty(sai->regmap);
1596a14bf98cSOlivier Moysan 
1597a14bf98cSOlivier Moysan 	clk_disable(sai->pdata->pclk);
1598a14bf98cSOlivier Moysan 
1599cf881773SOlivier Moysan 	return 0;
1600cf881773SOlivier Moysan }
1601cf881773SOlivier Moysan 
1602cf881773SOlivier Moysan static int stm32_sai_sub_resume(struct device *dev)
1603cf881773SOlivier Moysan {
1604cf881773SOlivier Moysan 	struct stm32_sai_sub_data *sai = dev_get_drvdata(dev);
1605a14bf98cSOlivier Moysan 	int ret;
1606a14bf98cSOlivier Moysan 
1607a14bf98cSOlivier Moysan 	ret = clk_enable(sai->pdata->pclk);
1608a14bf98cSOlivier Moysan 	if (ret < 0)
1609a14bf98cSOlivier Moysan 		return ret;
1610cf881773SOlivier Moysan 
1611cf881773SOlivier Moysan 	regcache_cache_only(sai->regmap, false);
1612a14bf98cSOlivier Moysan 	ret = regcache_sync(sai->regmap);
1613a14bf98cSOlivier Moysan 
1614a14bf98cSOlivier Moysan 	clk_disable(sai->pdata->pclk);
1615a14bf98cSOlivier Moysan 
1616a14bf98cSOlivier Moysan 	return ret;
1617cf881773SOlivier Moysan }
1618cf881773SOlivier Moysan #endif /* CONFIG_PM_SLEEP */
1619cf881773SOlivier Moysan 
1620cf881773SOlivier Moysan static const struct dev_pm_ops stm32_sai_sub_pm_ops = {
1621cf881773SOlivier Moysan 	SET_SYSTEM_SLEEP_PM_OPS(stm32_sai_sub_suspend, stm32_sai_sub_resume)
1622cf881773SOlivier Moysan };
1623cf881773SOlivier Moysan 
16243e086edfSolivier moysan static struct platform_driver stm32_sai_sub_driver = {
16253e086edfSolivier moysan 	.driver = {
16263e086edfSolivier moysan 		.name = "st,stm32-sai-sub",
16273e086edfSolivier moysan 		.of_match_table = stm32_sai_sub_ids,
1628cf881773SOlivier Moysan 		.pm = &stm32_sai_sub_pm_ops,
16293e086edfSolivier moysan 	},
16303e086edfSolivier moysan 	.probe = stm32_sai_sub_probe,
1631a14bf98cSOlivier Moysan 	.remove = stm32_sai_sub_remove,
16323e086edfSolivier moysan };
16333e086edfSolivier moysan 
16343e086edfSolivier moysan module_platform_driver(stm32_sai_sub_driver);
16353e086edfSolivier moysan 
16363e086edfSolivier moysan MODULE_DESCRIPTION("STM32 Soc SAI sub-block Interface");
1637602fdadcSolivier moysan MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>");
16383e086edfSolivier moysan MODULE_ALIAS("platform:st,stm32-sai-sub");
16393e086edfSolivier moysan MODULE_LICENSE("GPL v2");
1640