2 * STM32 ALSA SoC Digital Audio Interface (SAI) driver.
4 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
5 * Author(s): Olivier Moysan <olivier.moysan@st.com> for STMicroelectronics.
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 - 6))) {
411 STM_SAI_IS_SUB_A(sai) ?
412 strncat(p, "a_mclk", 6) : strncat(p, "b_mclk", 6);
414 mclk->hw.init = CLK_HW_INIT(mclk_name, pname, &mclk_ops, 0);
415 mclk->sai_data = sai;
418 dev_dbg(dev, "Register master clock %s\n", mclk_name);
419 ret = devm_clk_hw_register(&sai->pdev->dev, hw);
421 dev_err(dev, "mclk register returned %d\n", ret);
424 sai->sai_mclk = hw->clk;
426 /* register mclk provider */
427 return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, hw);
430 static irqreturn_t stm32_sai_isr(int irq, void *devid)
432 struct stm32_sai_sub_data *sai = (struct stm32_sai_sub_data *)devid;
433 struct platform_device *pdev = sai->pdev;
434 unsigned int sr, imr, flags;
435 snd_pcm_state_t status = SNDRV_PCM_STATE_RUNNING;
437 regmap_read(sai->regmap, STM_SAI_IMR_REGX, &imr);
438 regmap_read(sai->regmap, STM_SAI_SR_REGX, &sr);
444 regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX, SAI_XCLRFR_MASK,
447 if (!sai->substream) {
448 dev_err(&pdev->dev, "Device stopped. Spurious IRQ 0x%x\n", sr);
452 if (flags & SAI_XIMR_OVRUDRIE) {
453 dev_err(&pdev->dev, "IRQ %s\n",
454 STM_SAI_IS_PLAYBACK(sai) ? "underrun" : "overrun");
455 status = SNDRV_PCM_STATE_XRUN;
458 if (flags & SAI_XIMR_MUTEDETIE)
459 dev_dbg(&pdev->dev, "IRQ mute detected\n");
461 if (flags & SAI_XIMR_WCKCFGIE) {
462 dev_err(&pdev->dev, "IRQ wrong clock configuration\n");
463 status = SNDRV_PCM_STATE_DISCONNECTED;
466 if (flags & SAI_XIMR_CNRDYIE)
467 dev_err(&pdev->dev, "IRQ Codec not ready\n");
469 if (flags & SAI_XIMR_AFSDETIE) {
470 dev_err(&pdev->dev, "IRQ Anticipated frame synchro\n");
471 status = SNDRV_PCM_STATE_XRUN;
474 if (flags & SAI_XIMR_LFSDETIE) {
475 dev_err(&pdev->dev, "IRQ Late frame synchro\n");
476 status = SNDRV_PCM_STATE_XRUN;
479 if (status != SNDRV_PCM_STATE_RUNNING)
480 snd_pcm_stop_xrun(sai->substream);
485 static int stm32_sai_set_sysclk(struct snd_soc_dai *cpu_dai,
486 int clk_id, unsigned int freq, int dir)
488 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
491 if (dir == SND_SOC_CLOCK_OUT) {
492 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
494 (unsigned int)~SAI_XCR1_NODIV);
498 dev_dbg(cpu_dai->dev, "SAI MCLK frequency is %uHz\n", freq);
499 sai->mclk_rate = freq;
502 ret = clk_set_rate_exclusive(sai->sai_mclk,
505 dev_err(cpu_dai->dev,
506 "Could not set mclk rate\n");
515 static int stm32_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
516 u32 rx_mask, int slots, int slot_width)
518 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
519 int slotr, slotr_mask, slot_size;
521 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
522 dev_warn(cpu_dai->dev, "Slot setting relevant only for TDM\n");
526 dev_dbg(cpu_dai->dev, "Masks tx/rx:%#x/%#x, slots:%d, width:%d\n",
527 tx_mask, rx_mask, slots, slot_width);
529 switch (slot_width) {
531 slot_size = SAI_SLOT_SIZE_16;
534 slot_size = SAI_SLOT_SIZE_32;
537 slot_size = SAI_SLOT_SIZE_AUTO;
541 slotr = SAI_XSLOTR_SLOTSZ_SET(slot_size) |
542 SAI_XSLOTR_NBSLOT_SET(slots - 1);
543 slotr_mask = SAI_XSLOTR_SLOTSZ_MASK | SAI_XSLOTR_NBSLOT_MASK;
545 /* tx/rx mask set in machine init, if slot number defined in DT */
546 if (STM_SAI_IS_PLAYBACK(sai)) {
547 sai->slot_mask = tx_mask;
548 slotr |= SAI_XSLOTR_SLOTEN_SET(tx_mask);
551 if (STM_SAI_IS_CAPTURE(sai)) {
552 sai->slot_mask = rx_mask;
553 slotr |= SAI_XSLOTR_SLOTEN_SET(rx_mask);
556 slotr_mask |= SAI_XSLOTR_SLOTEN_MASK;
558 regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, slotr_mask, slotr);
560 sai->slot_width = slot_width;
566 static int stm32_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
568 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
570 int cr1_mask, frcr_mask = 0;
573 dev_dbg(cpu_dai->dev, "fmt %x\n", fmt);
575 /* Do not generate master by default */
576 cr1 = SAI_XCR1_NODIV;
577 cr1_mask = SAI_XCR1_NODIV;
579 cr1_mask |= SAI_XCR1_PRTCFG_MASK;
580 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
581 cr1 |= SAI_XCR1_PRTCFG_SET(SAI_SPDIF_PROTOCOL);
585 cr1 |= SAI_XCR1_PRTCFG_SET(SAI_FREE_PROTOCOL);
587 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
588 /* SCK active high for all protocols */
589 case SND_SOC_DAIFMT_I2S:
590 cr1 |= SAI_XCR1_CKSTR;
591 frcr |= SAI_XFRCR_FSOFF | SAI_XFRCR_FSDEF;
594 case SND_SOC_DAIFMT_MSB:
595 frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF;
597 /* Right justified */
598 case SND_SOC_DAIFMT_LSB:
599 frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF;
601 case SND_SOC_DAIFMT_DSP_A:
602 frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF;
604 case SND_SOC_DAIFMT_DSP_B:
605 frcr |= SAI_XFRCR_FSPOL;
608 dev_err(cpu_dai->dev, "Unsupported protocol %#x\n",
609 fmt & SND_SOC_DAIFMT_FORMAT_MASK);
613 cr1_mask |= SAI_XCR1_CKSTR;
614 frcr_mask |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF |
617 /* DAI clock strobing. Invert setting previously set */
618 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
619 case SND_SOC_DAIFMT_NB_NF:
621 case SND_SOC_DAIFMT_IB_NF:
622 cr1 ^= SAI_XCR1_CKSTR;
624 case SND_SOC_DAIFMT_NB_IF:
625 frcr ^= SAI_XFRCR_FSPOL;
627 case SND_SOC_DAIFMT_IB_IF:
628 /* Invert fs & sck */
629 cr1 ^= SAI_XCR1_CKSTR;
630 frcr ^= SAI_XFRCR_FSPOL;
633 dev_err(cpu_dai->dev, "Unsupported strobing %#x\n",
634 fmt & SND_SOC_DAIFMT_INV_MASK);
637 cr1_mask |= SAI_XCR1_CKSTR;
638 frcr_mask |= SAI_XFRCR_FSPOL;
640 regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr);
642 /* DAI clock master masks */
643 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
644 case SND_SOC_DAIFMT_CBM_CFM:
645 /* codec is master */
646 cr1 |= SAI_XCR1_SLAVE;
649 case SND_SOC_DAIFMT_CBS_CFS:
653 dev_err(cpu_dai->dev, "Unsupported mode %#x\n",
654 fmt & SND_SOC_DAIFMT_MASTER_MASK);
658 /* Set slave mode if sub-block is synchronized with another SAI */
660 dev_dbg(cpu_dai->dev, "Synchronized SAI configured as slave\n");
661 cr1 |= SAI_XCR1_SLAVE;
665 cr1_mask |= SAI_XCR1_SLAVE;
668 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1);
670 dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
679 static int stm32_sai_startup(struct snd_pcm_substream *substream,
680 struct snd_soc_dai *cpu_dai)
682 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
685 sai->substream = substream;
687 ret = clk_prepare_enable(sai->sai_ck);
689 dev_err(cpu_dai->dev, "Failed to enable clock: %d\n", ret);
695 regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX,
696 SAI_XCLRFR_MASK, SAI_XCLRFR_MASK);
698 imr = SAI_XIMR_OVRUDRIE;
699 if (STM_SAI_IS_CAPTURE(sai)) {
700 regmap_read(sai->regmap, STM_SAI_CR2_REGX, &cr2);
701 if (cr2 & SAI_XCR2_MUTECNT_MASK)
702 imr |= SAI_XIMR_MUTEDETIE;
706 imr |= SAI_XIMR_WCKCFGIE;
708 imr |= SAI_XIMR_AFSDETIE | SAI_XIMR_LFSDETIE;
710 regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX,
716 static int stm32_sai_set_config(struct snd_soc_dai *cpu_dai,
717 struct snd_pcm_substream *substream,
718 struct snd_pcm_hw_params *params)
720 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
721 int cr1, cr1_mask, ret;
724 * DMA bursts increment is set to 4 words.
725 * SAI fifo threshold is set to half fifo, to keep enough space
726 * for DMA incoming bursts.
728 regmap_update_bits(sai->regmap, STM_SAI_CR2_REGX,
729 SAI_XCR2_FFLUSH | SAI_XCR2_FTH_MASK,
731 SAI_XCR2_FTH_SET(STM_SAI_FIFO_TH_HALF));
733 /* DS bits in CR1 not set for SPDIF (size forced to 24 bits).*/
734 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
735 sai->spdif_frm_cnt = 0;
739 /* Mode, data format and channel config */
740 cr1_mask = SAI_XCR1_DS_MASK;
741 switch (params_format(params)) {
742 case SNDRV_PCM_FORMAT_S8:
743 cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_8);
745 case SNDRV_PCM_FORMAT_S16_LE:
746 cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_16);
748 case SNDRV_PCM_FORMAT_S32_LE:
749 cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_32);
752 dev_err(cpu_dai->dev, "Data format not supported");
756 cr1_mask |= SAI_XCR1_MONO;
757 if ((sai->slots == 2) && (params_channels(params) == 1))
758 cr1 |= SAI_XCR1_MONO;
760 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1);
762 dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
769 static int stm32_sai_set_slots(struct snd_soc_dai *cpu_dai)
771 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
774 regmap_read(sai->regmap, STM_SAI_SLOTR_REGX, &slotr);
777 * If SLOTSZ is set to auto in SLOTR, align slot width on data size
778 * By default slot width = data size, if not forced from DT
780 slot_sz = slotr & SAI_XSLOTR_SLOTSZ_MASK;
781 if (slot_sz == SAI_XSLOTR_SLOTSZ_SET(SAI_SLOT_SIZE_AUTO))
782 sai->slot_width = sai->data_size;
784 if (sai->slot_width < sai->data_size) {
785 dev_err(cpu_dai->dev,
786 "Data size %d larger than slot width\n",
791 /* Slot number is set to 2, if not specified in DT */
795 /* The number of slots in the audio frame is equal to NBSLOT[3:0] + 1*/
796 regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX,
797 SAI_XSLOTR_NBSLOT_MASK,
798 SAI_XSLOTR_NBSLOT_SET((sai->slots - 1)));
800 /* Set default slots mask if not already set from DT */
801 if (!(slotr & SAI_XSLOTR_SLOTEN_MASK)) {
802 sai->slot_mask = (1 << sai->slots) - 1;
803 regmap_update_bits(sai->regmap,
804 STM_SAI_SLOTR_REGX, SAI_XSLOTR_SLOTEN_MASK,
805 SAI_XSLOTR_SLOTEN_SET(sai->slot_mask));
808 dev_dbg(cpu_dai->dev, "Slots %d, slot width %d\n",
809 sai->slots, sai->slot_width);
814 static void stm32_sai_set_frame(struct snd_soc_dai *cpu_dai)
816 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
817 int fs_active, offset, format;
820 format = sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
821 sai->fs_length = sai->slot_width * sai->slots;
823 fs_active = sai->fs_length / 2;
824 if ((format == SND_SOC_DAIFMT_DSP_A) ||
825 (format == SND_SOC_DAIFMT_DSP_B))
828 frcr = SAI_XFRCR_FRL_SET((sai->fs_length - 1));
829 frcr |= SAI_XFRCR_FSALL_SET((fs_active - 1));
830 frcr_mask = SAI_XFRCR_FRL_MASK | SAI_XFRCR_FSALL_MASK;
832 dev_dbg(cpu_dai->dev, "Frame length %d, frame active %d\n",
833 sai->fs_length, fs_active);
835 regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr);
837 if ((sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_LSB) {
838 offset = sai->slot_width - sai->data_size;
840 regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX,
841 SAI_XSLOTR_FBOFF_MASK,
842 SAI_XSLOTR_FBOFF_SET(offset));
846 static void stm32_sai_init_iec958_status(struct stm32_sai_sub_data *sai)
848 unsigned char *cs = sai->iec958.status;
850 cs[0] = IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS_NONE;
851 cs[1] = IEC958_AES1_CON_GENERAL;
852 cs[2] = IEC958_AES2_CON_SOURCE_UNSPEC | IEC958_AES2_CON_CHANNEL_UNSPEC;
853 cs[3] = IEC958_AES3_CON_CLOCK_1000PPM | IEC958_AES3_CON_FS_NOTID;
856 static void stm32_sai_set_iec958_status(struct stm32_sai_sub_data *sai,
857 struct snd_pcm_runtime *runtime)
862 /* Force the sample rate according to runtime rate */
863 mutex_lock(&sai->ctrl_lock);
864 switch (runtime->rate) {
866 sai->iec958.status[3] = IEC958_AES3_CON_FS_22050;
869 sai->iec958.status[3] = IEC958_AES3_CON_FS_44100;
872 sai->iec958.status[3] = IEC958_AES3_CON_FS_88200;
875 sai->iec958.status[3] = IEC958_AES3_CON_FS_176400;
878 sai->iec958.status[3] = IEC958_AES3_CON_FS_24000;
881 sai->iec958.status[3] = IEC958_AES3_CON_FS_48000;
884 sai->iec958.status[3] = IEC958_AES3_CON_FS_96000;
887 sai->iec958.status[3] = IEC958_AES3_CON_FS_192000;
890 sai->iec958.status[3] = IEC958_AES3_CON_FS_32000;
893 sai->iec958.status[3] = IEC958_AES3_CON_FS_NOTID;
896 mutex_unlock(&sai->ctrl_lock);
899 static int stm32_sai_configure_clock(struct snd_soc_dai *cpu_dai,
900 struct snd_pcm_hw_params *params)
902 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
903 int cr1, mask, div = 0;
904 int sai_clk_rate, mclk_ratio, den;
905 unsigned int rate = params_rate(params);
908 clk_set_parent(sai->sai_ck, sai->pdata->clk_x11k);
910 clk_set_parent(sai->sai_ck, sai->pdata->clk_x8k);
911 sai_clk_rate = clk_get_rate(sai->sai_ck);
913 if (STM_SAI_IS_F4(sai->pdata)) {
915 * mclk_rate = 256 * fs
916 * MCKDIV = 0 if sai_ck < 3/2 * mclk_rate
917 * MCKDIV = sai_ck / (2 * mclk_rate) otherwise
919 * MCKDIV ignored. sck = sai_ck
924 if (2 * sai_clk_rate >= 3 * sai->mclk_rate) {
925 div = stm32_sai_get_clk_div(sai, sai_clk_rate,
934 * MCKDIV = sai_ck / (ws x 256) (NOMCK=0. OSR=0)
935 * MCKDIV = sai_ck / (ws x 512) (NOMCK=0. OSR=1)
937 * MCKDIV = sai_ck / (frl x ws) (NOMCK=1)
938 * Note: NOMCK/NODIV correspond to same bit.
940 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
941 div = stm32_sai_get_clk_div(sai, sai_clk_rate,
946 if (sai->mclk_rate) {
947 mclk_ratio = sai->mclk_rate / rate;
948 if (mclk_ratio == 512) {
951 } else if (mclk_ratio != 256) {
952 dev_err(cpu_dai->dev,
953 "Wrong mclk ratio %d\n",
957 div = stm32_sai_get_clk_div(sai, sai_clk_rate,
962 /* mclk-fs not set, master clock not active */
963 den = sai->fs_length * params_rate(params);
964 div = stm32_sai_get_clk_div(sai, sai_clk_rate,
972 return stm32_sai_set_clk_div(sai, div);
975 static int stm32_sai_hw_params(struct snd_pcm_substream *substream,
976 struct snd_pcm_hw_params *params,
977 struct snd_soc_dai *cpu_dai)
979 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
982 sai->data_size = params_width(params);
984 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
985 /* Rate not already set in runtime structure */
986 substream->runtime->rate = params_rate(params);
987 stm32_sai_set_iec958_status(sai, substream->runtime);
989 ret = stm32_sai_set_slots(cpu_dai);
992 stm32_sai_set_frame(cpu_dai);
995 ret = stm32_sai_set_config(cpu_dai, substream, params);
1000 ret = stm32_sai_configure_clock(cpu_dai, params);
1005 static int stm32_sai_trigger(struct snd_pcm_substream *substream, int cmd,
1006 struct snd_soc_dai *cpu_dai)
1008 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
1012 case SNDRV_PCM_TRIGGER_START:
1013 case SNDRV_PCM_TRIGGER_RESUME:
1014 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1015 dev_dbg(cpu_dai->dev, "Enable DMA and SAI\n");
1017 regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
1018 SAI_XCR1_DMAEN, SAI_XCR1_DMAEN);
1021 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
1022 SAI_XCR1_SAIEN, SAI_XCR1_SAIEN);
1024 dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
1026 case SNDRV_PCM_TRIGGER_SUSPEND:
1027 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1028 case SNDRV_PCM_TRIGGER_STOP:
1029 dev_dbg(cpu_dai->dev, "Disable DMA and SAI\n");
1031 regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX,
1034 regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
1036 (unsigned int)~SAI_XCR1_SAIEN);
1038 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
1040 (unsigned int)~SAI_XCR1_DMAEN);
1042 dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
1044 if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
1045 sai->spdif_frm_cnt = 0;
1054 static void stm32_sai_shutdown(struct snd_pcm_substream *substream,
1055 struct snd_soc_dai *cpu_dai)
1057 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
1059 regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, SAI_XIMR_MASK, 0);
1061 regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, SAI_XCR1_NODIV,
1064 clk_disable_unprepare(sai->sai_ck);
1066 clk_rate_exclusive_put(sai->sai_mclk);
1068 sai->substream = NULL;
1071 static int stm32_sai_pcm_new(struct snd_soc_pcm_runtime *rtd,
1072 struct snd_soc_dai *cpu_dai)
1074 struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
1076 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
1077 dev_dbg(&sai->pdev->dev, "%s: register iec controls", __func__);
1078 return snd_ctl_add(rtd->pcm->card,
1079 snd_ctl_new1(&iec958_ctls, sai));
1085 static int stm32_sai_dai_probe(struct snd_soc_dai *cpu_dai)
1087 struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
1088 int cr1 = 0, cr1_mask;
1090 sai->cpu_dai = cpu_dai;
1092 sai->dma_params.addr = (dma_addr_t)(sai->phys_addr + STM_SAI_DR_REGX);
1094 * DMA supports 4, 8 or 16 burst sizes. Burst size 4 is the best choice,
1095 * as it allows bytes, half-word and words transfers. (See DMA fifos
1098 sai->dma_params.maxburst = 4;
1099 /* Buswidth will be set by framework at runtime */
1100 sai->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
1102 if (STM_SAI_IS_PLAYBACK(sai))
1103 snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params, NULL);
1105 snd_soc_dai_init_dma_data(cpu_dai, NULL, &sai->dma_params);
1107 /* Next settings are not relevant for spdif mode */
1108 if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
1111 cr1_mask = SAI_XCR1_RX_TX;
1112 if (STM_SAI_IS_CAPTURE(sai))
1113 cr1 |= SAI_XCR1_RX_TX;
1115 /* Configure synchronization */
1116 if (sai->sync == SAI_SYNC_EXTERNAL) {
1117 /* Configure synchro client and provider */
1118 sai->pdata->set_sync(sai->pdata, sai->np_sync_provider,
1119 sai->synco, sai->synci);
1122 cr1_mask |= SAI_XCR1_SYNCEN_MASK;
1123 cr1 |= SAI_XCR1_SYNCEN_SET(sai->sync);
1125 return regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1);
1128 static const struct snd_soc_dai_ops stm32_sai_pcm_dai_ops = {
1129 .set_sysclk = stm32_sai_set_sysclk,
1130 .set_fmt = stm32_sai_set_dai_fmt,
1131 .set_tdm_slot = stm32_sai_set_dai_tdm_slot,
1132 .startup = stm32_sai_startup,
1133 .hw_params = stm32_sai_hw_params,
1134 .trigger = stm32_sai_trigger,
1135 .shutdown = stm32_sai_shutdown,
1138 static int stm32_sai_pcm_process_spdif(struct snd_pcm_substream *substream,
1139 int channel, unsigned long hwoff,
1140 void *buf, unsigned long bytes)
1142 struct snd_pcm_runtime *runtime = substream->runtime;
1143 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1144 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1145 struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
1146 int *ptr = (int *)(runtime->dma_area + hwoff +
1147 channel * (runtime->dma_bytes / runtime->channels));
1148 ssize_t cnt = bytes_to_samples(runtime, bytes);
1149 unsigned int frm_cnt = sai->spdif_frm_cnt;
1154 *ptr = ((*ptr >> 8) & 0x00ffffff);
1156 /* Set channel status bit */
1157 byte = frm_cnt >> 3;
1158 mask = 1 << (frm_cnt - (byte << 3));
1159 if (sai->iec958.status[byte] & mask)
1166 if (frm_cnt == SAI_IEC60958_BLOCK_FRAMES)
1169 sai->spdif_frm_cnt = frm_cnt;
1174 static const struct snd_pcm_hardware stm32_sai_pcm_hw = {
1175 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP,
1176 .buffer_bytes_max = 8 * PAGE_SIZE,
1177 .period_bytes_min = 1024, /* 5ms at 48kHz */
1178 .period_bytes_max = PAGE_SIZE,
1183 static struct snd_soc_dai_driver stm32_sai_playback_dai[] = {
1185 .probe = stm32_sai_dai_probe,
1186 .pcm_new = stm32_sai_pcm_new,
1187 .id = 1, /* avoid call to fmt_single_name() */
1193 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1194 /* DMA does not support 24 bits transfers */
1196 SNDRV_PCM_FMTBIT_S8 |
1197 SNDRV_PCM_FMTBIT_S16_LE |
1198 SNDRV_PCM_FMTBIT_S32_LE,
1200 .ops = &stm32_sai_pcm_dai_ops,
1204 static struct snd_soc_dai_driver stm32_sai_capture_dai[] = {
1206 .probe = stm32_sai_dai_probe,
1207 .id = 1, /* avoid call to fmt_single_name() */
1213 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1214 /* DMA does not support 24 bits transfers */
1216 SNDRV_PCM_FMTBIT_S8 |
1217 SNDRV_PCM_FMTBIT_S16_LE |
1218 SNDRV_PCM_FMTBIT_S32_LE,
1220 .ops = &stm32_sai_pcm_dai_ops,
1224 static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config = {
1225 .pcm_hardware = &stm32_sai_pcm_hw,
1226 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
1229 static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config_spdif = {
1230 .pcm_hardware = &stm32_sai_pcm_hw,
1231 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
1232 .process = stm32_sai_pcm_process_spdif,
1235 static const struct snd_soc_component_driver stm32_component = {
1236 .name = "stm32-sai",
1239 static const struct of_device_id stm32_sai_sub_ids[] = {
1240 { .compatible = "st,stm32-sai-sub-a",
1241 .data = (void *)STM_SAI_A_ID},
1242 { .compatible = "st,stm32-sai-sub-b",
1243 .data = (void *)STM_SAI_B_ID},
1246 MODULE_DEVICE_TABLE(of, stm32_sai_sub_ids);
1248 static int stm32_sai_sub_parse_of(struct platform_device *pdev,
1249 struct stm32_sai_sub_data *sai)
1251 struct device_node *np = pdev->dev.of_node;
1252 struct resource *res;
1254 struct of_phandle_args args;
1260 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1261 base = devm_ioremap_resource(&pdev->dev, res);
1263 return PTR_ERR(base);
1265 sai->phys_addr = res->start;
1267 sai->regmap_config = &stm32_sai_sub_regmap_config_f4;
1268 /* Note: PDM registers not available for H7 sub-block B */
1269 if (STM_SAI_IS_H7(sai->pdata) && STM_SAI_IS_SUB_A(sai))
1270 sai->regmap_config = &stm32_sai_sub_regmap_config_h7;
1272 sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "sai_ck",
1273 base, sai->regmap_config);
1274 if (IS_ERR(sai->regmap)) {
1275 dev_err(&pdev->dev, "Failed to initialize MMIO\n");
1276 return PTR_ERR(sai->regmap);
1279 /* Get direction property */
1280 if (of_property_match_string(np, "dma-names", "tx") >= 0) {
1281 sai->dir = SNDRV_PCM_STREAM_PLAYBACK;
1282 } else if (of_property_match_string(np, "dma-names", "rx") >= 0) {
1283 sai->dir = SNDRV_PCM_STREAM_CAPTURE;
1285 dev_err(&pdev->dev, "Unsupported direction\n");
1289 /* Get spdif iec60958 property */
1291 if (of_get_property(np, "st,iec60958", NULL)) {
1292 if (!STM_SAI_HAS_SPDIF(sai) ||
1293 sai->dir == SNDRV_PCM_STREAM_CAPTURE) {
1294 dev_err(&pdev->dev, "S/PDIF IEC60958 not supported\n");
1297 stm32_sai_init_iec958_status(sai);
1302 /* Get synchronization property */
1304 ret = of_parse_phandle_with_fixed_args(np, "st,sync", 1, 0, &args);
1305 if (ret < 0 && ret != -ENOENT) {
1306 dev_err(&pdev->dev, "Failed to get st,sync property\n");
1310 sai->sync = SAI_SYNC_NONE;
1312 if (args.np == np) {
1313 dev_err(&pdev->dev, "%pOFn sync own reference\n", np);
1314 of_node_put(args.np);
1318 sai->np_sync_provider = of_get_parent(args.np);
1319 if (!sai->np_sync_provider) {
1320 dev_err(&pdev->dev, "%pOFn parent node not found\n",
1322 of_node_put(args.np);
1326 sai->sync = SAI_SYNC_INTERNAL;
1327 if (sai->np_sync_provider != sai->pdata->pdev->dev.of_node) {
1328 if (!STM_SAI_HAS_EXT_SYNC(sai)) {
1330 "External synchro not supported\n");
1331 of_node_put(args.np);
1334 sai->sync = SAI_SYNC_EXTERNAL;
1336 sai->synci = args.args[0];
1337 if (sai->synci < 1 ||
1338 (sai->synci > (SAI_GCR_SYNCIN_MAX + 1))) {
1339 dev_err(&pdev->dev, "Wrong SAI index\n");
1340 of_node_put(args.np);
1344 if (of_property_match_string(args.np, "compatible",
1345 "st,stm32-sai-sub-a") >= 0)
1346 sai->synco = STM_SAI_SYNC_OUT_A;
1348 if (of_property_match_string(args.np, "compatible",
1349 "st,stm32-sai-sub-b") >= 0)
1350 sai->synco = STM_SAI_SYNC_OUT_B;
1353 dev_err(&pdev->dev, "Unknown SAI sub-block\n");
1354 of_node_put(args.np);
1359 dev_dbg(&pdev->dev, "%s synchronized with %s\n",
1360 pdev->name, args.np->full_name);
1363 of_node_put(args.np);
1364 sai->sai_ck = devm_clk_get(&pdev->dev, "sai_ck");
1365 if (IS_ERR(sai->sai_ck)) {
1366 dev_err(&pdev->dev, "Missing kernel clock sai_ck\n");
1367 return PTR_ERR(sai->sai_ck);
1370 if (STM_SAI_IS_F4(sai->pdata))
1373 /* Register mclk provider if requested */
1374 if (of_find_property(np, "#clock-cells", NULL)) {
1375 ret = stm32_sai_add_mclk_provider(sai);
1379 sai->sai_mclk = devm_clk_get(&pdev->dev, "MCLK");
1380 if (IS_ERR(sai->sai_mclk)) {
1381 if (PTR_ERR(sai->sai_mclk) != -ENOENT)
1382 return PTR_ERR(sai->sai_mclk);
1383 sai->sai_mclk = NULL;
1390 static int stm32_sai_sub_dais_init(struct platform_device *pdev,
1391 struct stm32_sai_sub_data *sai)
1393 sai->cpu_dai_drv = devm_kzalloc(&pdev->dev,
1394 sizeof(struct snd_soc_dai_driver),
1396 if (!sai->cpu_dai_drv)
1399 sai->cpu_dai_drv->name = dev_name(&pdev->dev);
1400 if (STM_SAI_IS_PLAYBACK(sai)) {
1401 memcpy(sai->cpu_dai_drv, &stm32_sai_playback_dai,
1402 sizeof(stm32_sai_playback_dai));
1403 sai->cpu_dai_drv->playback.stream_name = sai->cpu_dai_drv->name;
1405 memcpy(sai->cpu_dai_drv, &stm32_sai_capture_dai,
1406 sizeof(stm32_sai_capture_dai));
1407 sai->cpu_dai_drv->capture.stream_name = sai->cpu_dai_drv->name;
1413 static int stm32_sai_sub_probe(struct platform_device *pdev)
1415 struct stm32_sai_sub_data *sai;
1416 const struct of_device_id *of_id;
1417 const struct snd_dmaengine_pcm_config *conf = &stm32_sai_pcm_config;
1420 sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
1424 of_id = of_match_device(stm32_sai_sub_ids, &pdev->dev);
1427 sai->id = (uintptr_t)of_id->data;
1430 mutex_init(&sai->ctrl_lock);
1431 platform_set_drvdata(pdev, sai);
1433 sai->pdata = dev_get_drvdata(pdev->dev.parent);
1435 dev_err(&pdev->dev, "Parent device data not available\n");
1439 ret = stm32_sai_sub_parse_of(pdev, sai);
1443 ret = stm32_sai_sub_dais_init(pdev, sai);
1447 ret = devm_request_irq(&pdev->dev, sai->pdata->irq, stm32_sai_isr,
1448 IRQF_SHARED, dev_name(&pdev->dev), sai);
1450 dev_err(&pdev->dev, "IRQ request returned %d\n", ret);
1454 ret = devm_snd_soc_register_component(&pdev->dev, &stm32_component,
1455 sai->cpu_dai_drv, 1);
1459 if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
1460 conf = &stm32_sai_pcm_config_spdif;
1462 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, conf, 0);
1464 dev_err(&pdev->dev, "Could not register pcm dma\n");
1471 static struct platform_driver stm32_sai_sub_driver = {
1473 .name = "st,stm32-sai-sub",
1474 .of_match_table = stm32_sai_sub_ids,
1476 .probe = stm32_sai_sub_probe,
1479 module_platform_driver(stm32_sai_sub_driver);
1481 MODULE_DESCRIPTION("STM32 Soc SAI sub-block Interface");
1482 MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>");
1483 MODULE_ALIAS("platform:st,stm32-sai-sub");
1484 MODULE_LICENSE("GPL v2");