2 * STM32 ALSA SoC Digital Audio Interface (SAI) driver.
4 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
7 * License terms: GPL V2.0.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 #include <linux/clk.h>
20 #include <linux/clk-provider.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_platform.h>
25 #include <linux/regmap.h>
27 #include <sound/asoundef.h>
28 #include <sound/core.h>
29 #include <sound/dmaengine_pcm.h>
30 #include <sound/pcm_params.h>
32 #include "stm32_sai.h"
34 #define SAI_FREE_PROTOCOL 0x0
35 #define SAI_SPDIF_PROTOCOL 0x1
37 #define SAI_SLOT_SIZE_AUTO 0x0
38 #define SAI_SLOT_SIZE_16 0x1
39 #define SAI_SLOT_SIZE_32 0x2
41 #define SAI_DATASIZE_8 0x2
42 #define SAI_DATASIZE_10 0x3
43 #define SAI_DATASIZE_16 0x4
44 #define SAI_DATASIZE_20 0x5
45 #define SAI_DATASIZE_24 0x6
46 #define SAI_DATASIZE_32 0x7
48 #define STM_SAI_FIFO_SIZE 8
49 #define STM_SAI_DAI_NAME_SIZE 15
51 #define STM_SAI_IS_PLAYBACK(ip) ((ip)->dir == SNDRV_PCM_STREAM_PLAYBACK)
52 #define STM_SAI_IS_CAPTURE(ip) ((ip)->dir == SNDRV_PCM_STREAM_CAPTURE)
54 #define STM_SAI_A_ID 0x0
55 #define STM_SAI_B_ID 0x1
57 #define STM_SAI_IS_SUB_A(x) ((x)->id == STM_SAI_A_ID)
58 #define STM_SAI_IS_SUB_B(x) ((x)->id == STM_SAI_B_ID)
59 #define STM_SAI_BLOCK_NAME(x) (((x)->id == STM_SAI_A_ID) ? "A" : "B")
61 #define SAI_SYNC_NONE 0x0
62 #define SAI_SYNC_INTERNAL 0x1
63 #define SAI_SYNC_EXTERNAL 0x2
65 #define STM_SAI_PROTOCOL_IS_SPDIF(ip) ((ip)->spdif)
66 #define STM_SAI_HAS_SPDIF(x) ((x)->pdata->conf->has_spdif)
67 #define STM_SAI_HAS_EXT_SYNC(x) (!STM_SAI_IS_F4(sai->pdata))
69 #define SAI_IEC60958_BLOCK_FRAMES 192
70 #define SAI_IEC60958_STATUS_BYTES 24
72 #define SAI_MCLK_NAME_LEN 32
75 * struct stm32_sai_sub_data - private data of SAI sub block (block A or B)
76 * @pdev: device data pointer
77 * @regmap: SAI register map pointer
78 * @regmap_config: SAI sub block register map configuration pointer
79 * @dma_params: dma configuration data for rx or tx channel
80 * @cpu_dai_drv: DAI driver data pointer
81 * @cpu_dai: DAI runtime data pointer
82 * @substream: PCM substream data pointer
83 * @pdata: SAI block parent data pointer
84 * @np_sync_provider: synchronization provider node
85 * @sai_ck: kernel clock feeding the SAI clock generator
86 * @sai_mclk: master clock from SAI mclk provider
87 * @phys_addr: SAI registers physical base address
88 * @mclk_rate: SAI block master clock frequency (Hz). set at init
89 * @id: SAI sub block id corresponding to sub-block A or B
90 * @dir: SAI block direction (playback or capture). set at init
91 * @master: SAI block mode flag. (true=master, false=slave) set at init
92 * @spdif: SAI S/PDIF iec60958 mode flag. set at init
93 * @fmt: SAI block format. relevant only for custom protocols. set at init
94 * @sync: SAI block synchronization mode. (none, internal or external)
95 * @synco: SAI block ext sync source (provider setting). (none, sub-block A/B)
96 * @synci: SAI block ext sync source (client setting). (SAI sync provider index)
97 * @fs_length: frame synchronization length. depends on protocol settings
98 * @slots: rx or tx slot number
99 * @slot_width: rx or tx slot width in bits
100 * @slot_mask: rx or tx active slots mask. set at init or at runtime
101 * @data_size: PCM data width. corresponds to PCM substream width.
102 * @spdif_frm_cnt: S/PDIF playback frame counter
103 * @snd_aes_iec958: iec958 data
104 * @ctrl_lock: control lock
106 struct stm32_sai_sub_data {
107 struct platform_device *pdev;
108 struct regmap *regmap;
109 const struct regmap_config *regmap_config;
110 struct snd_dmaengine_dai_dma_data dma_params;
111 struct snd_soc_dai_driver *cpu_dai_drv;
112 struct snd_soc_dai *cpu_dai;
113 struct snd_pcm_substream *substream;
114 struct stm32_sai_data *pdata;
115 struct device_node *np_sync_provider;
117 struct clk *sai_mclk;
118 dma_addr_t phys_addr;
119 unsigned int mclk_rate;
133 unsigned int spdif_frm_cnt;
134 struct snd_aes_iec958 iec958;
135 struct mutex ctrl_lock; /* protect resources accessed by controls */
138 enum stm32_sai_fifo_th {
139 STM_SAI_FIFO_TH_EMPTY,
140 STM_SAI_FIFO_TH_QUARTER,
141 STM_SAI_FIFO_TH_HALF,
142 STM_SAI_FIFO_TH_3_QUARTER,
143 STM_SAI_FIFO_TH_FULL,
146 static bool stm32_sai_sub_readable_reg(struct device *dev, unsigned int reg)
149 case STM_SAI_CR1_REGX:
150 case STM_SAI_CR2_REGX:
151 case STM_SAI_FRCR_REGX:
152 case STM_SAI_SLOTR_REGX:
153 case STM_SAI_IMR_REGX:
154 case STM_SAI_SR_REGX:
155 case STM_SAI_CLRFR_REGX:
156 case STM_SAI_DR_REGX:
157 case STM_SAI_PDMCR_REGX:
158 case STM_SAI_PDMLY_REGX:
165 static bool stm32_sai_sub_volatile_reg(struct device *dev, unsigned int reg)
168 case STM_SAI_DR_REGX:
175 static bool stm32_sai_sub_writeable_reg(struct device *dev, unsigned int reg)
178 case STM_SAI_CR1_REGX:
179 case STM_SAI_CR2_REGX:
180 case STM_SAI_FRCR_REGX:
181 case STM_SAI_SLOTR_REGX:
182 case STM_SAI_IMR_REGX:
183 case STM_SAI_SR_REGX:
184 case STM_SAI_CLRFR_REGX:
185 case STM_SAI_DR_REGX:
186 case STM_SAI_PDMCR_REGX:
187 case STM_SAI_PDMLY_REGX:
194 static const struct regmap_config stm32_sai_sub_regmap_config_f4 = {
198 .max_register = STM_SAI_DR_REGX,
199 .readable_reg = stm32_sai_sub_readable_reg,
200 .volatile_reg = stm32_sai_sub_volatile_reg,
201 .writeable_reg = stm32_sai_sub_writeable_reg,
205 static const struct regmap_config stm32_sai_sub_regmap_config_h7 = {
209 .max_register = STM_SAI_PDMLY_REGX,
210 .readable_reg = stm32_sai_sub_readable_reg,
211 .volatile_reg = stm32_sai_sub_volatile_reg,
212 .writeable_reg = stm32_sai_sub_writeable_reg,
216 static int snd_pcm_iec958_info(struct snd_kcontrol *kcontrol,
217 struct snd_ctl_elem_info *uinfo)
219 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
225 static int snd_pcm_iec958_get(struct snd_kcontrol *kcontrol,
226 struct snd_ctl_elem_value *uctl)
228 struct stm32_sai_sub_data *sai = snd_kcontrol_chip(kcontrol);
230 mutex_lock(&sai->ctrl_lock);
231 memcpy(uctl->value.iec958.status, sai->iec958.status, 4);
232 mutex_unlock(&sai->ctrl_lock);
237 static int snd_pcm_iec958_put(struct snd_kcontrol *kcontrol,
238 struct snd_ctl_elem_value *uctl)
240 struct stm32_sai_sub_data *sai = snd_kcontrol_chip(kcontrol);
242 mutex_lock(&sai->ctrl_lock);
243 memcpy(sai->iec958.status, uctl->value.iec958.status, 4);
244 mutex_unlock(&sai->ctrl_lock);
249 static const struct snd_kcontrol_new iec958_ctls = {
250 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
251 SNDRV_CTL_ELEM_ACCESS_VOLATILE),
252 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
253 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
254 .info = snd_pcm_iec958_info,
255 .get = snd_pcm_iec958_get,
256 .put = snd_pcm_iec958_put,
259 struct stm32_sai_mclk_data {
262 struct stm32_sai_sub_data *sai_data;
265 #define to_mclk_data(_hw) container_of(_hw, struct stm32_sai_mclk_data, hw)
266 #define STM32_SAI_MAX_CLKS 1
268 static int stm32_sai_get_clk_div(struct stm32_sai_sub_data *sai,
269 unsigned long input_rate,
270 unsigned long output_rate)
272 int version = sai->pdata->conf->version;
275 div = DIV_ROUND_CLOSEST(input_rate, output_rate);
276 if (div > SAI_XCR1_MCKDIV_MAX(version)) {
277 dev_err(&sai->pdev->dev, "Divider %d out of range\n", div);
280 dev_dbg(&sai->pdev->dev, "SAI divider %d\n", div);
282 if (input_rate % div)
283 dev_dbg(&sai->pdev->dev,
284 "Rate not accurate. requested (%ld), actual (%ld)\n",
285 output_rate, input_rate / div);
290 static int stm32_sai_set_clk_div(struct stm32_sai_sub_data *sai,
293 int version = sai->pdata->conf->version;
296 if (div > SAI_XCR1_MCKDIV_MAX(version)) {
297 dev_err(&sai->pdev->dev, "Divider %d out of range\n", div);
301 mask = SAI_XCR1_MCKDIV_MASK(SAI_XCR1_MCKDIV_WIDTH(version));
302 cr1 = SAI_XCR1_MCKDIV_SET(div);
303 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, mask, cr1);
305 dev_err(&sai->pdev->dev, "Failed to update CR1 register\n");
310 static long stm32_sai_mclk_round_rate(struct clk_hw *hw, unsigned long rate,
311 unsigned long *prate)
313 struct stm32_sai_mclk_data *mclk = to_mclk_data(hw);
314 struct stm32_sai_sub_data *sai = mclk->sai_data;
317 div = stm32_sai_get_clk_div(sai, *prate, rate);
321 mclk->freq = *prate / div;
326 static unsigned long stm32_sai_mclk_recalc_rate(struct clk_hw *hw,
327 unsigned long parent_rate)
329 struct stm32_sai_mclk_data *mclk = to_mclk_data(hw);
334 static int stm32_sai_mclk_set_rate(struct clk_hw *hw, unsigned long rate,
335 unsigned long parent_rate)
337 struct stm32_sai_mclk_data *mclk = to_mclk_data(hw);
338 struct stm32_sai_sub_data *sai = mclk->sai_data;
342 div = stm32_sai_get_clk_div(sai, parent_rate, rate);
346 ret = stm32_sai_set_clk_div(sai, div);
355 static int stm32_sai_mclk_enable(struct clk_hw *hw)
357 struct stm32_sai_mclk_data *mclk = to_mclk_data(hw);
358 struct stm32_sai_sub_data *sai = mclk->sai_data;
360 dev_dbg(&sai->pdev->dev, "Enable master clock\n");
362 return regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
363 SAI_XCR1_MCKEN, SAI_XCR1_MCKEN);
366 static void stm32_sai_mclk_disable(struct clk_hw *hw)
368 struct stm32_sai_mclk_data *mclk = to_mclk_data(hw);
369 struct stm32_sai_sub_data *sai = mclk->sai_data;
371 dev_dbg(&sai->pdev->dev, "Disable master clock\n");
373 regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, SAI_XCR1_MCKEN, 0);
376 static const struct clk_ops mclk_ops = {
377 .enable = stm32_sai_mclk_enable,
378 .disable = stm32_sai_mclk_disable,
379 .recalc_rate = stm32_sai_mclk_recalc_rate,
380 .round_rate = stm32_sai_mclk_round_rate,
381 .set_rate = stm32_sai_mclk_set_rate,
384 static int stm32_sai_add_mclk_provider(struct stm32_sai_sub_data *sai)
387 struct stm32_sai_mclk_data *mclk;
388 struct device *dev = &sai->pdev->dev;
389 const char *pname = __clk_get_name(sai->sai_ck);
390 char *mclk_name, *p, *s = (char *)pname;
393 mclk = devm_kzalloc(dev, sizeof(mclk), GFP_KERNEL);
397 mclk_name = devm_kcalloc(dev, sizeof(char),
398 SAI_MCLK_NAME_LEN, GFP_KERNEL);
403 * Forge mclk clock name from parent clock name and suffix.
404 * String after "_" char is stripped in parent name.
407 while (*s && *s != '_' && (i < (SAI_MCLK_NAME_LEN - 7))) {
411 STM_SAI_IS_SUB_A(sai) ? strcat(p, "a_mclk") : strcat(p, "b_mclk");
413 mclk->hw.init = CLK_HW_INIT(mclk_name, pname, &mclk_ops, 0);
414 mclk->sai_data = sai;
417 dev_dbg(dev, "Register master clock %s\n", mclk_name);
418 ret = devm_clk_hw_register(&sai->pdev->dev, hw);
420 dev_err(dev, "mclk register returned %d\n", ret);
423 sai->sai_mclk = hw->clk;
425 /* register mclk provider */
426 return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, hw);
429 static irqreturn_t stm32_sai_isr(int irq, void *devid)
431 struct stm32_sai_sub_data *sai = (struct stm32_sai_sub_data *)devid;
432 struct platform_device *pdev = sai->pdev;
433 unsigned int sr, imr, flags;
434 snd_pcm_state_t status = SNDRV_PCM_STATE_RUNNING;
436 regmap_read(sai->regmap, STM_SAI_IMR_REGX, &imr);
437 regmap_read(sai->regmap, STM_SAI_SR_REGX, &sr);
443 regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX, SAI_XCLRFR_MASK,
446 if (!sai->substream) {
447 dev_err(&pdev->dev, "Device stopped. Spurious IRQ 0x%x\n", sr);
451 if (flags & SAI_XIMR_OVRUDRIE) {
452 dev_err(&pdev->dev, "IRQ %s\n",
453 STM_SAI_IS_PLAYBACK(sai) ? "underrun" : "overrun");
454 status = SNDRV_PCM_STATE_XRUN;
457 if (flags & SAI_XIMR_MUTEDETIE)
458 dev_dbg(&pdev->dev, "IRQ mute detected\n");
460 if (flags & SAI_XIMR_WCKCFGIE) {
461 dev_err(&pdev->dev, "IRQ wrong clock configuration\n");
462 status = SNDRV_PCM_STATE_DISCONNECTED;
465 if (flags & SAI_XIMR_CNRDYIE)
466 dev_err(&pdev->dev, "IRQ Codec not ready\n");
468 if (flags & SAI_XIMR_AFSDETIE) {
469 dev_err(&pdev->dev, "IRQ Anticipated frame synchro\n");
470 status = SNDRV_PCM_STATE_XRUN;
473 if (flags & SAI_XIMR_LFSDETIE) {
474 dev_err(&pdev->dev, "IRQ Late frame synchro\n");
475 status = SNDRV_PCM_STATE_XRUN;
478 if (status != SNDRV_PCM_STATE_RUNNING)
479 snd_pcm_stop_xrun(sai->substream);
484 static int stm32_sai_set_sysclk(struct snd_soc_dai *cpu_dai,
485 int clk_id, unsigned int freq, int dir)
487 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
490 if (dir == SND_SOC_CLOCK_OUT) {
491 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
493 (unsigned int)~SAI_XCR1_NODIV);
497 dev_dbg(cpu_dai->dev, "SAI MCLK frequency is %uHz\n", freq);
498 sai->mclk_rate = freq;
501 ret = clk_set_rate_exclusive(sai->sai_mclk,
504 dev_err(cpu_dai->dev,
505 "Could not set mclk rate\n");
514 static int stm32_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
515 u32 rx_mask, int slots, int slot_width)
517 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
518 int slotr, slotr_mask, slot_size;
520 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
521 dev_warn(cpu_dai->dev, "Slot setting relevant only for TDM\n");
525 dev_dbg(cpu_dai->dev, "Masks tx/rx:%#x/%#x, slots:%d, width:%d\n",
526 tx_mask, rx_mask, slots, slot_width);
528 switch (slot_width) {
530 slot_size = SAI_SLOT_SIZE_16;
533 slot_size = SAI_SLOT_SIZE_32;
536 slot_size = SAI_SLOT_SIZE_AUTO;
540 slotr = SAI_XSLOTR_SLOTSZ_SET(slot_size) |
541 SAI_XSLOTR_NBSLOT_SET(slots - 1);
542 slotr_mask = SAI_XSLOTR_SLOTSZ_MASK | SAI_XSLOTR_NBSLOT_MASK;
544 /* tx/rx mask set in machine init, if slot number defined in DT */
545 if (STM_SAI_IS_PLAYBACK(sai)) {
546 sai->slot_mask = tx_mask;
547 slotr |= SAI_XSLOTR_SLOTEN_SET(tx_mask);
550 if (STM_SAI_IS_CAPTURE(sai)) {
551 sai->slot_mask = rx_mask;
552 slotr |= SAI_XSLOTR_SLOTEN_SET(rx_mask);
555 slotr_mask |= SAI_XSLOTR_SLOTEN_MASK;
557 regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, slotr_mask, slotr);
559 sai->slot_width = slot_width;
565 static int stm32_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
567 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
569 int cr1_mask, frcr_mask = 0;
572 dev_dbg(cpu_dai->dev, "fmt %x\n", fmt);
574 /* Do not generate master by default */
575 cr1 = SAI_XCR1_NODIV;
576 cr1_mask = SAI_XCR1_NODIV;
578 cr1_mask |= SAI_XCR1_PRTCFG_MASK;
579 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
580 cr1 |= SAI_XCR1_PRTCFG_SET(SAI_SPDIF_PROTOCOL);
584 cr1 |= SAI_XCR1_PRTCFG_SET(SAI_FREE_PROTOCOL);
586 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
587 /* SCK active high for all protocols */
588 case SND_SOC_DAIFMT_I2S:
589 cr1 |= SAI_XCR1_CKSTR;
590 frcr |= SAI_XFRCR_FSOFF | SAI_XFRCR_FSDEF;
593 case SND_SOC_DAIFMT_MSB:
594 frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF;
596 /* Right justified */
597 case SND_SOC_DAIFMT_LSB:
598 frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF;
600 case SND_SOC_DAIFMT_DSP_A:
601 frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF;
603 case SND_SOC_DAIFMT_DSP_B:
604 frcr |= SAI_XFRCR_FSPOL;
607 dev_err(cpu_dai->dev, "Unsupported protocol %#x\n",
608 fmt & SND_SOC_DAIFMT_FORMAT_MASK);
612 cr1_mask |= SAI_XCR1_CKSTR;
613 frcr_mask |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF |
616 /* DAI clock strobing. Invert setting previously set */
617 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
618 case SND_SOC_DAIFMT_NB_NF:
620 case SND_SOC_DAIFMT_IB_NF:
621 cr1 ^= SAI_XCR1_CKSTR;
623 case SND_SOC_DAIFMT_NB_IF:
624 frcr ^= SAI_XFRCR_FSPOL;
626 case SND_SOC_DAIFMT_IB_IF:
627 /* Invert fs & sck */
628 cr1 ^= SAI_XCR1_CKSTR;
629 frcr ^= SAI_XFRCR_FSPOL;
632 dev_err(cpu_dai->dev, "Unsupported strobing %#x\n",
633 fmt & SND_SOC_DAIFMT_INV_MASK);
636 cr1_mask |= SAI_XCR1_CKSTR;
637 frcr_mask |= SAI_XFRCR_FSPOL;
639 regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr);
641 /* DAI clock master masks */
642 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
643 case SND_SOC_DAIFMT_CBM_CFM:
644 /* codec is master */
645 cr1 |= SAI_XCR1_SLAVE;
648 case SND_SOC_DAIFMT_CBS_CFS:
652 dev_err(cpu_dai->dev, "Unsupported mode %#x\n",
653 fmt & SND_SOC_DAIFMT_MASTER_MASK);
657 /* Set slave mode if sub-block is synchronized with another SAI */
659 dev_dbg(cpu_dai->dev, "Synchronized SAI configured as slave\n");
660 cr1 |= SAI_XCR1_SLAVE;
664 cr1_mask |= SAI_XCR1_SLAVE;
667 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1);
669 dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
678 static int stm32_sai_startup(struct snd_pcm_substream *substream,
679 struct snd_soc_dai *cpu_dai)
681 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
684 sai->substream = substream;
686 ret = clk_prepare_enable(sai->sai_ck);
688 dev_err(cpu_dai->dev, "Failed to enable clock: %d\n", ret);
694 regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX,
695 SAI_XCLRFR_MASK, SAI_XCLRFR_MASK);
697 imr = SAI_XIMR_OVRUDRIE;
698 if (STM_SAI_IS_CAPTURE(sai)) {
699 regmap_read(sai->regmap, STM_SAI_CR2_REGX, &cr2);
700 if (cr2 & SAI_XCR2_MUTECNT_MASK)
701 imr |= SAI_XIMR_MUTEDETIE;
705 imr |= SAI_XIMR_WCKCFGIE;
707 imr |= SAI_XIMR_AFSDETIE | SAI_XIMR_LFSDETIE;
709 regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX,
715 static int stm32_sai_set_config(struct snd_soc_dai *cpu_dai,
716 struct snd_pcm_substream *substream,
717 struct snd_pcm_hw_params *params)
719 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
720 int cr1, cr1_mask, ret;
723 * DMA bursts increment is set to 4 words.
724 * SAI fifo threshold is set to half fifo, to keep enough space
725 * for DMA incoming bursts.
727 regmap_update_bits(sai->regmap, STM_SAI_CR2_REGX,
728 SAI_XCR2_FFLUSH | SAI_XCR2_FTH_MASK,
730 SAI_XCR2_FTH_SET(STM_SAI_FIFO_TH_HALF));
732 /* DS bits in CR1 not set for SPDIF (size forced to 24 bits).*/
733 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
734 sai->spdif_frm_cnt = 0;
738 /* Mode, data format and channel config */
739 cr1_mask = SAI_XCR1_DS_MASK;
740 switch (params_format(params)) {
741 case SNDRV_PCM_FORMAT_S8:
742 cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_8);
744 case SNDRV_PCM_FORMAT_S16_LE:
745 cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_16);
747 case SNDRV_PCM_FORMAT_S32_LE:
748 cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_32);
751 dev_err(cpu_dai->dev, "Data format not supported");
755 cr1_mask |= SAI_XCR1_MONO;
756 if ((sai->slots == 2) && (params_channels(params) == 1))
757 cr1 |= SAI_XCR1_MONO;
759 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1);
761 dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
768 static int stm32_sai_set_slots(struct snd_soc_dai *cpu_dai)
770 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
773 regmap_read(sai->regmap, STM_SAI_SLOTR_REGX, &slotr);
776 * If SLOTSZ is set to auto in SLOTR, align slot width on data size
777 * By default slot width = data size, if not forced from DT
779 slot_sz = slotr & SAI_XSLOTR_SLOTSZ_MASK;
780 if (slot_sz == SAI_XSLOTR_SLOTSZ_SET(SAI_SLOT_SIZE_AUTO))
781 sai->slot_width = sai->data_size;
783 if (sai->slot_width < sai->data_size) {
784 dev_err(cpu_dai->dev,
785 "Data size %d larger than slot width\n",
790 /* Slot number is set to 2, if not specified in DT */
794 /* The number of slots in the audio frame is equal to NBSLOT[3:0] + 1*/
795 regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX,
796 SAI_XSLOTR_NBSLOT_MASK,
797 SAI_XSLOTR_NBSLOT_SET((sai->slots - 1)));
799 /* Set default slots mask if not already set from DT */
800 if (!(slotr & SAI_XSLOTR_SLOTEN_MASK)) {
801 sai->slot_mask = (1 << sai->slots) - 1;
802 regmap_update_bits(sai->regmap,
803 STM_SAI_SLOTR_REGX, SAI_XSLOTR_SLOTEN_MASK,
804 SAI_XSLOTR_SLOTEN_SET(sai->slot_mask));
807 dev_dbg(cpu_dai->dev, "Slots %d, slot width %d\n",
808 sai->slots, sai->slot_width);
813 static void stm32_sai_set_frame(struct snd_soc_dai *cpu_dai)
815 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
816 int fs_active, offset, format;
819 format = sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
820 sai->fs_length = sai->slot_width * sai->slots;
822 fs_active = sai->fs_length / 2;
823 if ((format == SND_SOC_DAIFMT_DSP_A) ||
824 (format == SND_SOC_DAIFMT_DSP_B))
827 frcr = SAI_XFRCR_FRL_SET((sai->fs_length - 1));
828 frcr |= SAI_XFRCR_FSALL_SET((fs_active - 1));
829 frcr_mask = SAI_XFRCR_FRL_MASK | SAI_XFRCR_FSALL_MASK;
831 dev_dbg(cpu_dai->dev, "Frame length %d, frame active %d\n",
832 sai->fs_length, fs_active);
834 regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr);
836 if ((sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_LSB) {
837 offset = sai->slot_width - sai->data_size;
839 regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX,
840 SAI_XSLOTR_FBOFF_MASK,
841 SAI_XSLOTR_FBOFF_SET(offset));
845 static void stm32_sai_init_iec958_status(struct stm32_sai_sub_data *sai)
847 unsigned char *cs = sai->iec958.status;
849 cs[0] = IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS_NONE;
850 cs[1] = IEC958_AES1_CON_GENERAL;
851 cs[2] = IEC958_AES2_CON_SOURCE_UNSPEC | IEC958_AES2_CON_CHANNEL_UNSPEC;
852 cs[3] = IEC958_AES3_CON_CLOCK_1000PPM | IEC958_AES3_CON_FS_NOTID;
855 static void stm32_sai_set_iec958_status(struct stm32_sai_sub_data *sai,
856 struct snd_pcm_runtime *runtime)
861 /* Force the sample rate according to runtime rate */
862 mutex_lock(&sai->ctrl_lock);
863 switch (runtime->rate) {
865 sai->iec958.status[3] = IEC958_AES3_CON_FS_22050;
868 sai->iec958.status[3] = IEC958_AES3_CON_FS_44100;
871 sai->iec958.status[3] = IEC958_AES3_CON_FS_88200;
874 sai->iec958.status[3] = IEC958_AES3_CON_FS_176400;
877 sai->iec958.status[3] = IEC958_AES3_CON_FS_24000;
880 sai->iec958.status[3] = IEC958_AES3_CON_FS_48000;
883 sai->iec958.status[3] = IEC958_AES3_CON_FS_96000;
886 sai->iec958.status[3] = IEC958_AES3_CON_FS_192000;
889 sai->iec958.status[3] = IEC958_AES3_CON_FS_32000;
892 sai->iec958.status[3] = IEC958_AES3_CON_FS_NOTID;
895 mutex_unlock(&sai->ctrl_lock);
898 static int stm32_sai_configure_clock(struct snd_soc_dai *cpu_dai,
899 struct snd_pcm_hw_params *params)
901 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
902 int cr1, mask, div = 0;
903 int sai_clk_rate, mclk_ratio, den;
904 unsigned int rate = params_rate(params);
907 clk_set_parent(sai->sai_ck, sai->pdata->clk_x11k);
909 clk_set_parent(sai->sai_ck, sai->pdata->clk_x8k);
910 sai_clk_rate = clk_get_rate(sai->sai_ck);
912 if (STM_SAI_IS_F4(sai->pdata)) {
914 * mclk_rate = 256 * fs
915 * MCKDIV = 0 if sai_ck < 3/2 * mclk_rate
916 * MCKDIV = sai_ck / (2 * mclk_rate) otherwise
918 * MCKDIV ignored. sck = sai_ck
923 if (2 * sai_clk_rate >= 3 * sai->mclk_rate) {
924 div = stm32_sai_get_clk_div(sai, sai_clk_rate,
933 * MCKDIV = sai_ck / (ws x 256) (NOMCK=0. OSR=0)
934 * MCKDIV = sai_ck / (ws x 512) (NOMCK=0. OSR=1)
936 * MCKDIV = sai_ck / (frl x ws) (NOMCK=1)
937 * Note: NOMCK/NODIV correspond to same bit.
939 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
940 div = stm32_sai_get_clk_div(sai, sai_clk_rate,
945 if (sai->mclk_rate) {
946 mclk_ratio = sai->mclk_rate / rate;
947 if (mclk_ratio == 512) {
950 } else if (mclk_ratio != 256) {
951 dev_err(cpu_dai->dev,
952 "Wrong mclk ratio %d\n",
956 div = stm32_sai_get_clk_div(sai, sai_clk_rate,
961 /* mclk-fs not set, master clock not active */
962 den = sai->fs_length * params_rate(params);
963 div = stm32_sai_get_clk_div(sai, sai_clk_rate,
971 return stm32_sai_set_clk_div(sai, div);
974 static int stm32_sai_hw_params(struct snd_pcm_substream *substream,
975 struct snd_pcm_hw_params *params,
976 struct snd_soc_dai *cpu_dai)
978 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
981 sai->data_size = params_width(params);
983 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
984 /* Rate not already set in runtime structure */
985 substream->runtime->rate = params_rate(params);
986 stm32_sai_set_iec958_status(sai, substream->runtime);
988 ret = stm32_sai_set_slots(cpu_dai);
991 stm32_sai_set_frame(cpu_dai);
994 ret = stm32_sai_set_config(cpu_dai, substream, params);
999 ret = stm32_sai_configure_clock(cpu_dai, params);
1004 static int stm32_sai_trigger(struct snd_pcm_substream *substream, int cmd,
1005 struct snd_soc_dai *cpu_dai)
1007 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
1011 case SNDRV_PCM_TRIGGER_START:
1012 case SNDRV_PCM_TRIGGER_RESUME:
1013 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1014 dev_dbg(cpu_dai->dev, "Enable DMA and SAI\n");
1016 regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
1017 SAI_XCR1_DMAEN, SAI_XCR1_DMAEN);
1020 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
1021 SAI_XCR1_SAIEN, SAI_XCR1_SAIEN);
1023 dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
1025 case SNDRV_PCM_TRIGGER_SUSPEND:
1026 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1027 case SNDRV_PCM_TRIGGER_STOP:
1028 dev_dbg(cpu_dai->dev, "Disable DMA and SAI\n");
1030 regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX,
1033 regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
1035 (unsigned int)~SAI_XCR1_SAIEN);
1037 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
1039 (unsigned int)~SAI_XCR1_DMAEN);
1041 dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
1043 if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
1044 sai->spdif_frm_cnt = 0;
1053 static void stm32_sai_shutdown(struct snd_pcm_substream *substream,
1054 struct snd_soc_dai *cpu_dai)
1056 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
1058 regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, SAI_XIMR_MASK, 0);
1060 regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, SAI_XCR1_NODIV,
1063 clk_disable_unprepare(sai->sai_ck);
1065 clk_rate_exclusive_put(sai->sai_mclk);
1067 sai->substream = NULL;
1070 static int stm32_sai_pcm_new(struct snd_soc_pcm_runtime *rtd,
1071 struct snd_soc_dai *cpu_dai)
1073 struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
1075 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
1076 dev_dbg(&sai->pdev->dev, "%s: register iec controls", __func__);
1077 return snd_ctl_add(rtd->pcm->card,
1078 snd_ctl_new1(&iec958_ctls, sai));
1084 static int stm32_sai_dai_probe(struct snd_soc_dai *cpu_dai)
1086 struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
1087 int cr1 = 0, cr1_mask;
1089 sai->cpu_dai = cpu_dai;
1091 sai->dma_params.addr = (dma_addr_t)(sai->phys_addr + STM_SAI_DR_REGX);
1093 * DMA supports 4, 8 or 16 burst sizes. Burst size 4 is the best choice,
1094 * as it allows bytes, half-word and words transfers. (See DMA fifos
1097 sai->dma_params.maxburst = 4;
1098 /* Buswidth will be set by framework at runtime */
1099 sai->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
1101 if (STM_SAI_IS_PLAYBACK(sai))
1102 snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params, NULL);
1104 snd_soc_dai_init_dma_data(cpu_dai, NULL, &sai->dma_params);
1106 /* Next settings are not relevant for spdif mode */
1107 if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
1110 cr1_mask = SAI_XCR1_RX_TX;
1111 if (STM_SAI_IS_CAPTURE(sai))
1112 cr1 |= SAI_XCR1_RX_TX;
1114 /* Configure synchronization */
1115 if (sai->sync == SAI_SYNC_EXTERNAL) {
1116 /* Configure synchro client and provider */
1117 sai->pdata->set_sync(sai->pdata, sai->np_sync_provider,
1118 sai->synco, sai->synci);
1121 cr1_mask |= SAI_XCR1_SYNCEN_MASK;
1122 cr1 |= SAI_XCR1_SYNCEN_SET(sai->sync);
1124 return regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1);
1127 static const struct snd_soc_dai_ops stm32_sai_pcm_dai_ops = {
1128 .set_sysclk = stm32_sai_set_sysclk,
1129 .set_fmt = stm32_sai_set_dai_fmt,
1130 .set_tdm_slot = stm32_sai_set_dai_tdm_slot,
1131 .startup = stm32_sai_startup,
1132 .hw_params = stm32_sai_hw_params,
1133 .trigger = stm32_sai_trigger,
1134 .shutdown = stm32_sai_shutdown,
1137 static int stm32_sai_pcm_process_spdif(struct snd_pcm_substream *substream,
1138 int channel, unsigned long hwoff,
1139 void *buf, unsigned long bytes)
1141 struct snd_pcm_runtime *runtime = substream->runtime;
1142 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1143 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1144 struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
1145 int *ptr = (int *)(runtime->dma_area + hwoff +
1146 channel * (runtime->dma_bytes / runtime->channels));
1147 ssize_t cnt = bytes_to_samples(runtime, bytes);
1148 unsigned int frm_cnt = sai->spdif_frm_cnt;
1153 *ptr = ((*ptr >> 8) & 0x00ffffff);
1155 /* Set channel status bit */
1156 byte = frm_cnt >> 3;
1157 mask = 1 << (frm_cnt - (byte << 3));
1158 if (sai->iec958.status[byte] & mask)
1165 if (frm_cnt == SAI_IEC60958_BLOCK_FRAMES)
1168 sai->spdif_frm_cnt = frm_cnt;
1173 static const struct snd_pcm_hardware stm32_sai_pcm_hw = {
1174 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP,
1175 .buffer_bytes_max = 8 * PAGE_SIZE,
1176 .period_bytes_min = 1024, /* 5ms at 48kHz */
1177 .period_bytes_max = PAGE_SIZE,
1182 static struct snd_soc_dai_driver stm32_sai_playback_dai[] = {
1184 .probe = stm32_sai_dai_probe,
1185 .pcm_new = stm32_sai_pcm_new,
1186 .id = 1, /* avoid call to fmt_single_name() */
1192 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1193 /* DMA does not support 24 bits transfers */
1195 SNDRV_PCM_FMTBIT_S8 |
1196 SNDRV_PCM_FMTBIT_S16_LE |
1197 SNDRV_PCM_FMTBIT_S32_LE,
1199 .ops = &stm32_sai_pcm_dai_ops,
1203 static struct snd_soc_dai_driver stm32_sai_capture_dai[] = {
1205 .probe = stm32_sai_dai_probe,
1206 .id = 1, /* avoid call to fmt_single_name() */
1212 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1213 /* DMA does not support 24 bits transfers */
1215 SNDRV_PCM_FMTBIT_S8 |
1216 SNDRV_PCM_FMTBIT_S16_LE |
1217 SNDRV_PCM_FMTBIT_S32_LE,
1219 .ops = &stm32_sai_pcm_dai_ops,
1223 static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config = {
1224 .pcm_hardware = &stm32_sai_pcm_hw,
1225 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
1228 static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config_spdif = {
1229 .pcm_hardware = &stm32_sai_pcm_hw,
1230 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
1231 .process = stm32_sai_pcm_process_spdif,
1234 static const struct snd_soc_component_driver stm32_component = {
1235 .name = "stm32-sai",
1238 static const struct of_device_id stm32_sai_sub_ids[] = {
1239 { .compatible = "st,stm32-sai-sub-a",
1240 .data = (void *)STM_SAI_A_ID},
1241 { .compatible = "st,stm32-sai-sub-b",
1242 .data = (void *)STM_SAI_B_ID},
1245 MODULE_DEVICE_TABLE(of, stm32_sai_sub_ids);
1247 static int stm32_sai_sub_parse_of(struct platform_device *pdev,
1248 struct stm32_sai_sub_data *sai)
1250 struct device_node *np = pdev->dev.of_node;
1251 struct resource *res;
1253 struct of_phandle_args args;
1259 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1260 base = devm_ioremap_resource(&pdev->dev, res);
1262 return PTR_ERR(base);
1264 sai->phys_addr = res->start;
1266 sai->regmap_config = &stm32_sai_sub_regmap_config_f4;
1267 /* Note: PDM registers not available for H7 sub-block B */
1268 if (STM_SAI_IS_H7(sai->pdata) && STM_SAI_IS_SUB_A(sai))
1269 sai->regmap_config = &stm32_sai_sub_regmap_config_h7;
1271 sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "sai_ck",
1272 base, sai->regmap_config);
1273 if (IS_ERR(sai->regmap)) {
1274 dev_err(&pdev->dev, "Failed to initialize MMIO\n");
1275 return PTR_ERR(sai->regmap);
1278 /* Get direction property */
1279 if (of_property_match_string(np, "dma-names", "tx") >= 0) {
1280 sai->dir = SNDRV_PCM_STREAM_PLAYBACK;
1281 } else if (of_property_match_string(np, "dma-names", "rx") >= 0) {
1282 sai->dir = SNDRV_PCM_STREAM_CAPTURE;
1284 dev_err(&pdev->dev, "Unsupported direction\n");
1288 /* Get spdif iec60958 property */
1290 if (of_get_property(np, "st,iec60958", NULL)) {
1291 if (!STM_SAI_HAS_SPDIF(sai) ||
1292 sai->dir == SNDRV_PCM_STREAM_CAPTURE) {
1293 dev_err(&pdev->dev, "S/PDIF IEC60958 not supported\n");
1296 stm32_sai_init_iec958_status(sai);
1301 /* Get synchronization property */
1303 ret = of_parse_phandle_with_fixed_args(np, "st,sync", 1, 0, &args);
1304 if (ret < 0 && ret != -ENOENT) {
1305 dev_err(&pdev->dev, "Failed to get st,sync property\n");
1309 sai->sync = SAI_SYNC_NONE;
1311 if (args.np == np) {
1312 dev_err(&pdev->dev, "%pOFn sync own reference\n", np);
1313 of_node_put(args.np);
1317 sai->np_sync_provider = of_get_parent(args.np);
1318 if (!sai->np_sync_provider) {
1319 dev_err(&pdev->dev, "%pOFn parent node not found\n",
1321 of_node_put(args.np);
1325 sai->sync = SAI_SYNC_INTERNAL;
1326 if (sai->np_sync_provider != sai->pdata->pdev->dev.of_node) {
1327 if (!STM_SAI_HAS_EXT_SYNC(sai)) {
1329 "External synchro not supported\n");
1330 of_node_put(args.np);
1333 sai->sync = SAI_SYNC_EXTERNAL;
1335 sai->synci = args.args[0];
1336 if (sai->synci < 1 ||
1337 (sai->synci > (SAI_GCR_SYNCIN_MAX + 1))) {
1338 dev_err(&pdev->dev, "Wrong SAI index\n");
1339 of_node_put(args.np);
1343 if (of_property_match_string(args.np, "compatible",
1344 "st,stm32-sai-sub-a") >= 0)
1345 sai->synco = STM_SAI_SYNC_OUT_A;
1347 if (of_property_match_string(args.np, "compatible",
1348 "st,stm32-sai-sub-b") >= 0)
1349 sai->synco = STM_SAI_SYNC_OUT_B;
1352 dev_err(&pdev->dev, "Unknown SAI sub-block\n");
1353 of_node_put(args.np);
1358 dev_dbg(&pdev->dev, "%s synchronized with %s\n",
1359 pdev->name, args.np->full_name);
1362 of_node_put(args.np);
1363 sai->sai_ck = devm_clk_get(&pdev->dev, "sai_ck");
1364 if (IS_ERR(sai->sai_ck)) {
1365 dev_err(&pdev->dev, "Missing kernel clock sai_ck\n");
1366 return PTR_ERR(sai->sai_ck);
1369 if (STM_SAI_IS_F4(sai->pdata))
1372 /* Register mclk provider if requested */
1373 if (of_find_property(np, "#clock-cells", NULL)) {
1374 ret = stm32_sai_add_mclk_provider(sai);
1378 sai->sai_mclk = devm_clk_get(&pdev->dev, "MCLK");
1379 if (IS_ERR(sai->sai_mclk)) {
1380 if (PTR_ERR(sai->sai_mclk) != -ENOENT)
1381 return PTR_ERR(sai->sai_mclk);
1382 sai->sai_mclk = NULL;
1389 static int stm32_sai_sub_dais_init(struct platform_device *pdev,
1390 struct stm32_sai_sub_data *sai)
1392 sai->cpu_dai_drv = devm_kzalloc(&pdev->dev,
1393 sizeof(struct snd_soc_dai_driver),
1395 if (!sai->cpu_dai_drv)
1398 sai->cpu_dai_drv->name = dev_name(&pdev->dev);
1399 if (STM_SAI_IS_PLAYBACK(sai)) {
1400 memcpy(sai->cpu_dai_drv, &stm32_sai_playback_dai,
1401 sizeof(stm32_sai_playback_dai));
1402 sai->cpu_dai_drv->playback.stream_name = sai->cpu_dai_drv->name;
1404 memcpy(sai->cpu_dai_drv, &stm32_sai_capture_dai,
1405 sizeof(stm32_sai_capture_dai));
1406 sai->cpu_dai_drv->capture.stream_name = sai->cpu_dai_drv->name;
1412 static int stm32_sai_sub_probe(struct platform_device *pdev)
1414 struct stm32_sai_sub_data *sai;
1415 const struct of_device_id *of_id;
1416 const struct snd_dmaengine_pcm_config *conf = &stm32_sai_pcm_config;
1419 sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
1423 of_id = of_match_device(stm32_sai_sub_ids, &pdev->dev);
1426 sai->id = (uintptr_t)of_id->data;
1429 mutex_init(&sai->ctrl_lock);
1430 platform_set_drvdata(pdev, sai);
1432 sai->pdata = dev_get_drvdata(pdev->dev.parent);
1434 dev_err(&pdev->dev, "Parent device data not available\n");
1438 ret = stm32_sai_sub_parse_of(pdev, sai);
1442 ret = stm32_sai_sub_dais_init(pdev, sai);
1446 ret = devm_request_irq(&pdev->dev, sai->pdata->irq, stm32_sai_isr,
1447 IRQF_SHARED, dev_name(&pdev->dev), sai);
1449 dev_err(&pdev->dev, "IRQ request returned %d\n", ret);
1453 ret = devm_snd_soc_register_component(&pdev->dev, &stm32_component,
1454 sai->cpu_dai_drv, 1);
1458 if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
1459 conf = &stm32_sai_pcm_config_spdif;
1461 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, conf, 0);
1463 dev_err(&pdev->dev, "Could not register pcm dma\n");
1470 static struct platform_driver stm32_sai_sub_driver = {
1472 .name = "st,stm32-sai-sub",
1473 .of_match_table = stm32_sai_sub_ids,
1475 .probe = stm32_sai_sub_probe,
1478 module_platform_driver(stm32_sai_sub_driver);
1480 MODULE_DESCRIPTION("STM32 Soc SAI sub-block Interface");
1482 MODULE_ALIAS("platform:st,stm32-sai-sub");
1483 MODULE_LICENSE("GPL v2");