1 // SPDX-License-Identifier: GPL-2.0-only
3 * STM32 ALSA SoC Digital Audio Interface (SAI) driver.
5 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
6 * Author(s): Olivier Moysan <olivier.moysan@st.com> for STMicroelectronics.
10 #include <linux/clk-provider.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/of_irq.h>
14 #include <linux/of_platform.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/regmap.h>
18 #include <sound/asoundef.h>
19 #include <sound/core.h>
20 #include <sound/dmaengine_pcm.h>
21 #include <sound/pcm_params.h>
23 #include "stm32_sai.h"
25 #define SAI_FREE_PROTOCOL 0x0
26 #define SAI_SPDIF_PROTOCOL 0x1
28 #define SAI_SLOT_SIZE_AUTO 0x0
29 #define SAI_SLOT_SIZE_16 0x1
30 #define SAI_SLOT_SIZE_32 0x2
32 #define SAI_DATASIZE_8 0x2
33 #define SAI_DATASIZE_10 0x3
34 #define SAI_DATASIZE_16 0x4
35 #define SAI_DATASIZE_20 0x5
36 #define SAI_DATASIZE_24 0x6
37 #define SAI_DATASIZE_32 0x7
39 #define STM_SAI_DAI_NAME_SIZE 15
41 #define STM_SAI_IS_PLAYBACK(ip) ((ip)->dir == SNDRV_PCM_STREAM_PLAYBACK)
42 #define STM_SAI_IS_CAPTURE(ip) ((ip)->dir == SNDRV_PCM_STREAM_CAPTURE)
44 #define STM_SAI_A_ID 0x0
45 #define STM_SAI_B_ID 0x1
47 #define STM_SAI_IS_SUB_A(x) ((x)->id == STM_SAI_A_ID)
48 #define STM_SAI_IS_SUB_B(x) ((x)->id == STM_SAI_B_ID)
49 #define STM_SAI_BLOCK_NAME(x) (((x)->id == STM_SAI_A_ID) ? "A" : "B")
51 #define SAI_SYNC_NONE 0x0
52 #define SAI_SYNC_INTERNAL 0x1
53 #define SAI_SYNC_EXTERNAL 0x2
55 #define STM_SAI_PROTOCOL_IS_SPDIF(ip) ((ip)->spdif)
56 #define STM_SAI_HAS_SPDIF(x) ((x)->pdata->conf.has_spdif_pdm)
57 #define STM_SAI_HAS_PDM(x) ((x)->pdata->conf.has_spdif_pdm)
58 #define STM_SAI_HAS_EXT_SYNC(x) (!STM_SAI_IS_F4(sai->pdata))
60 #define SAI_IEC60958_BLOCK_FRAMES 192
61 #define SAI_IEC60958_STATUS_BYTES 24
63 #define SAI_MCLK_NAME_LEN 32
64 #define SAI_RATE_11K 11025
67 * struct stm32_sai_sub_data - private data of SAI sub block (block A or B)
68 * @pdev: device data pointer
69 * @regmap: SAI register map pointer
70 * @regmap_config: SAI sub block register map configuration pointer
71 * @dma_params: dma configuration data for rx or tx channel
72 * @cpu_dai_drv: DAI driver data pointer
73 * @cpu_dai: DAI runtime data pointer
74 * @substream: PCM substream data pointer
75 * @pdata: SAI block parent data pointer
76 * @np_sync_provider: synchronization provider node
77 * @sai_ck: kernel clock feeding the SAI clock generator
78 * @sai_mclk: master clock from SAI mclk provider
79 * @phys_addr: SAI registers physical base address
80 * @mclk_rate: SAI block master clock frequency (Hz). set at init
81 * @id: SAI sub block id corresponding to sub-block A or B
82 * @dir: SAI block direction (playback or capture). set at init
83 * @master: SAI block mode flag. (true=master, false=slave) set at init
84 * @spdif: SAI S/PDIF iec60958 mode flag. set at init
85 * @fmt: SAI block format. relevant only for custom protocols. set at init
86 * @sync: SAI block synchronization mode. (none, internal or external)
87 * @synco: SAI block ext sync source (provider setting). (none, sub-block A/B)
88 * @synci: SAI block ext sync source (client setting). (SAI sync provider index)
89 * @fs_length: frame synchronization length. depends on protocol settings
90 * @slots: rx or tx slot number
91 * @slot_width: rx or tx slot width in bits
92 * @slot_mask: rx or tx active slots mask. set at init or at runtime
93 * @data_size: PCM data width. corresponds to PCM substream width.
94 * @spdif_frm_cnt: S/PDIF playback frame counter
95 * @iec958: iec958 data
96 * @ctrl_lock: control lock
97 * @irq_lock: prevent race condition with IRQ
99 struct stm32_sai_sub_data
{
100 struct platform_device
*pdev
;
101 struct regmap
*regmap
;
102 const struct regmap_config
*regmap_config
;
103 struct snd_dmaengine_dai_dma_data dma_params
;
104 struct snd_soc_dai_driver cpu_dai_drv
;
105 struct snd_soc_dai
*cpu_dai
;
106 struct snd_pcm_substream
*substream
;
107 struct stm32_sai_data
*pdata
;
108 struct device_node
*np_sync_provider
;
110 struct clk
*sai_mclk
;
111 dma_addr_t phys_addr
;
112 unsigned int mclk_rate
;
126 unsigned int spdif_frm_cnt
;
127 struct snd_aes_iec958 iec958
;
128 struct mutex ctrl_lock
; /* protect resources accessed by controls */
129 spinlock_t irq_lock
; /* used to prevent race condition with IRQ */
132 enum stm32_sai_fifo_th
{
133 STM_SAI_FIFO_TH_EMPTY
,
134 STM_SAI_FIFO_TH_QUARTER
,
135 STM_SAI_FIFO_TH_HALF
,
136 STM_SAI_FIFO_TH_3_QUARTER
,
137 STM_SAI_FIFO_TH_FULL
,
140 static bool stm32_sai_sub_readable_reg(struct device
*dev
, unsigned int reg
)
143 case STM_SAI_CR1_REGX
:
144 case STM_SAI_CR2_REGX
:
145 case STM_SAI_FRCR_REGX
:
146 case STM_SAI_SLOTR_REGX
:
147 case STM_SAI_IMR_REGX
:
148 case STM_SAI_SR_REGX
:
149 case STM_SAI_CLRFR_REGX
:
150 case STM_SAI_DR_REGX
:
151 case STM_SAI_PDMCR_REGX
:
152 case STM_SAI_PDMLY_REGX
:
159 static bool stm32_sai_sub_volatile_reg(struct device
*dev
, unsigned int reg
)
162 case STM_SAI_DR_REGX
:
163 case STM_SAI_SR_REGX
:
170 static bool stm32_sai_sub_writeable_reg(struct device
*dev
, unsigned int reg
)
173 case STM_SAI_CR1_REGX
:
174 case STM_SAI_CR2_REGX
:
175 case STM_SAI_FRCR_REGX
:
176 case STM_SAI_SLOTR_REGX
:
177 case STM_SAI_IMR_REGX
:
178 case STM_SAI_CLRFR_REGX
:
179 case STM_SAI_DR_REGX
:
180 case STM_SAI_PDMCR_REGX
:
181 case STM_SAI_PDMLY_REGX
:
188 static int stm32_sai_sub_reg_up(struct stm32_sai_sub_data
*sai
,
189 unsigned int reg
, unsigned int mask
,
194 ret
= clk_enable(sai
->pdata
->pclk
);
198 ret
= regmap_update_bits(sai
->regmap
, reg
, mask
, val
);
200 clk_disable(sai
->pdata
->pclk
);
205 static int stm32_sai_sub_reg_wr(struct stm32_sai_sub_data
*sai
,
206 unsigned int reg
, unsigned int mask
,
211 ret
= clk_enable(sai
->pdata
->pclk
);
215 ret
= regmap_write_bits(sai
->regmap
, reg
, mask
, val
);
217 clk_disable(sai
->pdata
->pclk
);
222 static int stm32_sai_sub_reg_rd(struct stm32_sai_sub_data
*sai
,
223 unsigned int reg
, unsigned int *val
)
227 ret
= clk_enable(sai
->pdata
->pclk
);
231 ret
= regmap_read(sai
->regmap
, reg
, val
);
233 clk_disable(sai
->pdata
->pclk
);
238 static const struct regmap_config stm32_sai_sub_regmap_config_f4
= {
242 .max_register
= STM_SAI_DR_REGX
,
243 .readable_reg
= stm32_sai_sub_readable_reg
,
244 .volatile_reg
= stm32_sai_sub_volatile_reg
,
245 .writeable_reg
= stm32_sai_sub_writeable_reg
,
247 .cache_type
= REGCACHE_FLAT
,
250 static const struct regmap_config stm32_sai_sub_regmap_config_h7
= {
254 .max_register
= STM_SAI_PDMLY_REGX
,
255 .readable_reg
= stm32_sai_sub_readable_reg
,
256 .volatile_reg
= stm32_sai_sub_volatile_reg
,
257 .writeable_reg
= stm32_sai_sub_writeable_reg
,
259 .cache_type
= REGCACHE_FLAT
,
262 static int snd_pcm_iec958_info(struct snd_kcontrol
*kcontrol
,
263 struct snd_ctl_elem_info
*uinfo
)
265 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
271 static int snd_pcm_iec958_get(struct snd_kcontrol
*kcontrol
,
272 struct snd_ctl_elem_value
*uctl
)
274 struct stm32_sai_sub_data
*sai
= snd_kcontrol_chip(kcontrol
);
276 mutex_lock(&sai
->ctrl_lock
);
277 memcpy(uctl
->value
.iec958
.status
, sai
->iec958
.status
, 4);
278 mutex_unlock(&sai
->ctrl_lock
);
283 static int snd_pcm_iec958_put(struct snd_kcontrol
*kcontrol
,
284 struct snd_ctl_elem_value
*uctl
)
286 struct stm32_sai_sub_data
*sai
= snd_kcontrol_chip(kcontrol
);
288 mutex_lock(&sai
->ctrl_lock
);
289 memcpy(sai
->iec958
.status
, uctl
->value
.iec958
.status
, 4);
290 mutex_unlock(&sai
->ctrl_lock
);
295 static const struct snd_kcontrol_new iec958_ctls
= {
296 .access
= (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
297 SNDRV_CTL_ELEM_ACCESS_VOLATILE
),
298 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
299 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
300 .info
= snd_pcm_iec958_info
,
301 .get
= snd_pcm_iec958_get
,
302 .put
= snd_pcm_iec958_put
,
305 struct stm32_sai_mclk_data
{
308 struct stm32_sai_sub_data
*sai_data
;
311 #define to_mclk_data(_hw) container_of(_hw, struct stm32_sai_mclk_data, hw)
312 #define STM32_SAI_MAX_CLKS 1
314 static int stm32_sai_get_clk_div(struct stm32_sai_sub_data
*sai
,
315 unsigned long input_rate
,
316 unsigned long output_rate
)
318 int version
= sai
->pdata
->conf
.version
;
321 div
= DIV_ROUND_CLOSEST(input_rate
, output_rate
);
322 if (div
> SAI_XCR1_MCKDIV_MAX(version
)) {
323 dev_err(&sai
->pdev
->dev
, "Divider %d out of range\n", div
);
326 dev_dbg(&sai
->pdev
->dev
, "SAI divider %d\n", div
);
328 if (input_rate
% div
)
329 dev_dbg(&sai
->pdev
->dev
,
330 "Rate not accurate. requested (%ld), actual (%ld)\n",
331 output_rate
, input_rate
/ div
);
336 static int stm32_sai_set_clk_div(struct stm32_sai_sub_data
*sai
,
339 int version
= sai
->pdata
->conf
.version
;
342 if (div
> SAI_XCR1_MCKDIV_MAX(version
)) {
343 dev_err(&sai
->pdev
->dev
, "Divider %d out of range\n", div
);
347 mask
= SAI_XCR1_MCKDIV_MASK(SAI_XCR1_MCKDIV_WIDTH(version
));
348 cr1
= SAI_XCR1_MCKDIV_SET(div
);
349 ret
= stm32_sai_sub_reg_up(sai
, STM_SAI_CR1_REGX
, mask
, cr1
);
351 dev_err(&sai
->pdev
->dev
, "Failed to update CR1 register\n");
356 static int stm32_sai_set_parent_clock(struct stm32_sai_sub_data
*sai
,
359 struct platform_device
*pdev
= sai
->pdev
;
360 struct clk
*parent_clk
= sai
->pdata
->clk_x8k
;
363 if (!(rate
% SAI_RATE_11K
))
364 parent_clk
= sai
->pdata
->clk_x11k
;
366 ret
= clk_set_parent(sai
->sai_ck
, parent_clk
);
368 dev_err(&pdev
->dev
, " Error %d setting sai_ck parent clock. %s",
370 "Active stream rates conflict\n" : "\n");
375 static long stm32_sai_mclk_round_rate(struct clk_hw
*hw
, unsigned long rate
,
376 unsigned long *prate
)
378 struct stm32_sai_mclk_data
*mclk
= to_mclk_data(hw
);
379 struct stm32_sai_sub_data
*sai
= mclk
->sai_data
;
382 div
= stm32_sai_get_clk_div(sai
, *prate
, rate
);
386 mclk
->freq
= *prate
/ div
;
391 static unsigned long stm32_sai_mclk_recalc_rate(struct clk_hw
*hw
,
392 unsigned long parent_rate
)
394 struct stm32_sai_mclk_data
*mclk
= to_mclk_data(hw
);
399 static int stm32_sai_mclk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
400 unsigned long parent_rate
)
402 struct stm32_sai_mclk_data
*mclk
= to_mclk_data(hw
);
403 struct stm32_sai_sub_data
*sai
= mclk
->sai_data
;
406 div
= stm32_sai_get_clk_div(sai
, parent_rate
, rate
);
410 ret
= stm32_sai_set_clk_div(sai
, div
);
419 static int stm32_sai_mclk_enable(struct clk_hw
*hw
)
421 struct stm32_sai_mclk_data
*mclk
= to_mclk_data(hw
);
422 struct stm32_sai_sub_data
*sai
= mclk
->sai_data
;
424 dev_dbg(&sai
->pdev
->dev
, "Enable master clock\n");
426 return stm32_sai_sub_reg_up(sai
, STM_SAI_CR1_REGX
,
427 SAI_XCR1_MCKEN
, SAI_XCR1_MCKEN
);
430 static void stm32_sai_mclk_disable(struct clk_hw
*hw
)
432 struct stm32_sai_mclk_data
*mclk
= to_mclk_data(hw
);
433 struct stm32_sai_sub_data
*sai
= mclk
->sai_data
;
435 dev_dbg(&sai
->pdev
->dev
, "Disable master clock\n");
437 stm32_sai_sub_reg_up(sai
, STM_SAI_CR1_REGX
, SAI_XCR1_MCKEN
, 0);
440 static const struct clk_ops mclk_ops
= {
441 .enable
= stm32_sai_mclk_enable
,
442 .disable
= stm32_sai_mclk_disable
,
443 .recalc_rate
= stm32_sai_mclk_recalc_rate
,
444 .round_rate
= stm32_sai_mclk_round_rate
,
445 .set_rate
= stm32_sai_mclk_set_rate
,
448 static int stm32_sai_add_mclk_provider(struct stm32_sai_sub_data
*sai
)
451 struct stm32_sai_mclk_data
*mclk
;
452 struct device
*dev
= &sai
->pdev
->dev
;
453 const char *pname
= __clk_get_name(sai
->sai_ck
);
454 char *mclk_name
, *p
, *s
= (char *)pname
;
457 mclk
= devm_kzalloc(dev
, sizeof(*mclk
), GFP_KERNEL
);
461 mclk_name
= devm_kcalloc(dev
, sizeof(char),
462 SAI_MCLK_NAME_LEN
, GFP_KERNEL
);
467 * Forge mclk clock name from parent clock name and suffix.
468 * String after "_" char is stripped in parent name.
471 while (*s
&& *s
!= '_' && (i
< (SAI_MCLK_NAME_LEN
- 7))) {
475 STM_SAI_IS_SUB_A(sai
) ? strcat(p
, "a_mclk") : strcat(p
, "b_mclk");
477 mclk
->hw
.init
= CLK_HW_INIT(mclk_name
, pname
, &mclk_ops
, 0);
478 mclk
->sai_data
= sai
;
481 dev_dbg(dev
, "Register master clock %s\n", mclk_name
);
482 ret
= devm_clk_hw_register(&sai
->pdev
->dev
, hw
);
484 dev_err(dev
, "mclk register returned %d\n", ret
);
487 sai
->sai_mclk
= hw
->clk
;
489 /* register mclk provider */
490 return devm_of_clk_add_hw_provider(dev
, of_clk_hw_simple_get
, hw
);
493 static irqreturn_t
stm32_sai_isr(int irq
, void *devid
)
495 struct stm32_sai_sub_data
*sai
= (struct stm32_sai_sub_data
*)devid
;
496 struct platform_device
*pdev
= sai
->pdev
;
497 unsigned int sr
, imr
, flags
;
498 snd_pcm_state_t status
= SNDRV_PCM_STATE_RUNNING
;
500 stm32_sai_sub_reg_rd(sai
, STM_SAI_IMR_REGX
, &imr
);
501 stm32_sai_sub_reg_rd(sai
, STM_SAI_SR_REGX
, &sr
);
507 stm32_sai_sub_reg_wr(sai
, STM_SAI_CLRFR_REGX
, SAI_XCLRFR_MASK
,
510 if (!sai
->substream
) {
511 dev_err(&pdev
->dev
, "Device stopped. Spurious IRQ 0x%x\n", sr
);
515 if (flags
& SAI_XIMR_OVRUDRIE
) {
516 dev_err(&pdev
->dev
, "IRQ %s\n",
517 STM_SAI_IS_PLAYBACK(sai
) ? "underrun" : "overrun");
518 status
= SNDRV_PCM_STATE_XRUN
;
521 if (flags
& SAI_XIMR_MUTEDETIE
)
522 dev_dbg(&pdev
->dev
, "IRQ mute detected\n");
524 if (flags
& SAI_XIMR_WCKCFGIE
) {
525 dev_err(&pdev
->dev
, "IRQ wrong clock configuration\n");
526 status
= SNDRV_PCM_STATE_DISCONNECTED
;
529 if (flags
& SAI_XIMR_CNRDYIE
)
530 dev_err(&pdev
->dev
, "IRQ Codec not ready\n");
532 if (flags
& SAI_XIMR_AFSDETIE
) {
533 dev_err(&pdev
->dev
, "IRQ Anticipated frame synchro\n");
534 status
= SNDRV_PCM_STATE_XRUN
;
537 if (flags
& SAI_XIMR_LFSDETIE
) {
538 dev_err(&pdev
->dev
, "IRQ Late frame synchro\n");
539 status
= SNDRV_PCM_STATE_XRUN
;
542 spin_lock(&sai
->irq_lock
);
543 if (status
!= SNDRV_PCM_STATE_RUNNING
&& sai
->substream
)
544 snd_pcm_stop_xrun(sai
->substream
);
545 spin_unlock(&sai
->irq_lock
);
550 static int stm32_sai_set_sysclk(struct snd_soc_dai
*cpu_dai
,
551 int clk_id
, unsigned int freq
, int dir
)
553 struct stm32_sai_sub_data
*sai
= snd_soc_dai_get_drvdata(cpu_dai
);
556 if (dir
== SND_SOC_CLOCK_OUT
&& sai
->sai_mclk
) {
557 ret
= stm32_sai_sub_reg_up(sai
, STM_SAI_CR1_REGX
,
559 freq
? 0 : SAI_XCR1_NODIV
);
563 /* Assume shutdown if requested frequency is 0Hz */
565 /* Release mclk rate only if rate was actually set */
566 if (sai
->mclk_rate
) {
567 clk_rate_exclusive_put(sai
->sai_mclk
);
573 /* If master clock is used, set parent clock now */
574 ret
= stm32_sai_set_parent_clock(sai
, freq
);
578 ret
= clk_set_rate_exclusive(sai
->sai_mclk
, freq
);
580 dev_err(cpu_dai
->dev
,
582 "Active streams have incompatible rates" :
583 "Could not set mclk rate\n");
587 dev_dbg(cpu_dai
->dev
, "SAI MCLK frequency is %uHz\n", freq
);
588 sai
->mclk_rate
= freq
;
594 static int stm32_sai_set_dai_tdm_slot(struct snd_soc_dai
*cpu_dai
, u32 tx_mask
,
595 u32 rx_mask
, int slots
, int slot_width
)
597 struct stm32_sai_sub_data
*sai
= snd_soc_dai_get_drvdata(cpu_dai
);
598 int slotr
, slotr_mask
, slot_size
;
600 if (STM_SAI_PROTOCOL_IS_SPDIF(sai
)) {
601 dev_warn(cpu_dai
->dev
, "Slot setting relevant only for TDM\n");
605 dev_dbg(cpu_dai
->dev
, "Masks tx/rx:%#x/%#x, slots:%d, width:%d\n",
606 tx_mask
, rx_mask
, slots
, slot_width
);
608 switch (slot_width
) {
610 slot_size
= SAI_SLOT_SIZE_16
;
613 slot_size
= SAI_SLOT_SIZE_32
;
616 slot_size
= SAI_SLOT_SIZE_AUTO
;
620 slotr
= SAI_XSLOTR_SLOTSZ_SET(slot_size
) |
621 SAI_XSLOTR_NBSLOT_SET(slots
- 1);
622 slotr_mask
= SAI_XSLOTR_SLOTSZ_MASK
| SAI_XSLOTR_NBSLOT_MASK
;
624 /* tx/rx mask set in machine init, if slot number defined in DT */
625 if (STM_SAI_IS_PLAYBACK(sai
)) {
626 sai
->slot_mask
= tx_mask
;
627 slotr
|= SAI_XSLOTR_SLOTEN_SET(tx_mask
);
630 if (STM_SAI_IS_CAPTURE(sai
)) {
631 sai
->slot_mask
= rx_mask
;
632 slotr
|= SAI_XSLOTR_SLOTEN_SET(rx_mask
);
635 slotr_mask
|= SAI_XSLOTR_SLOTEN_MASK
;
637 stm32_sai_sub_reg_up(sai
, STM_SAI_SLOTR_REGX
, slotr_mask
, slotr
);
639 sai
->slot_width
= slot_width
;
645 static int stm32_sai_set_dai_fmt(struct snd_soc_dai
*cpu_dai
, unsigned int fmt
)
647 struct stm32_sai_sub_data
*sai
= snd_soc_dai_get_drvdata(cpu_dai
);
649 int cr1_mask
, frcr_mask
= 0;
652 dev_dbg(cpu_dai
->dev
, "fmt %x\n", fmt
);
654 /* Do not generate master by default */
655 cr1
= SAI_XCR1_NODIV
;
656 cr1_mask
= SAI_XCR1_NODIV
;
658 cr1_mask
|= SAI_XCR1_PRTCFG_MASK
;
659 if (STM_SAI_PROTOCOL_IS_SPDIF(sai
)) {
660 cr1
|= SAI_XCR1_PRTCFG_SET(SAI_SPDIF_PROTOCOL
);
664 cr1
|= SAI_XCR1_PRTCFG_SET(SAI_FREE_PROTOCOL
);
666 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
667 /* SCK active high for all protocols */
668 case SND_SOC_DAIFMT_I2S
:
669 cr1
|= SAI_XCR1_CKSTR
;
670 frcr
|= SAI_XFRCR_FSOFF
| SAI_XFRCR_FSDEF
;
673 case SND_SOC_DAIFMT_MSB
:
674 frcr
|= SAI_XFRCR_FSPOL
| SAI_XFRCR_FSDEF
;
676 /* Right justified */
677 case SND_SOC_DAIFMT_LSB
:
678 frcr
|= SAI_XFRCR_FSPOL
| SAI_XFRCR_FSDEF
;
680 case SND_SOC_DAIFMT_DSP_A
:
681 frcr
|= SAI_XFRCR_FSPOL
| SAI_XFRCR_FSOFF
;
683 case SND_SOC_DAIFMT_DSP_B
:
684 frcr
|= SAI_XFRCR_FSPOL
;
687 dev_err(cpu_dai
->dev
, "Unsupported protocol %#x\n",
688 fmt
& SND_SOC_DAIFMT_FORMAT_MASK
);
692 cr1_mask
|= SAI_XCR1_CKSTR
;
693 frcr_mask
|= SAI_XFRCR_FSPOL
| SAI_XFRCR_FSOFF
|
696 /* DAI clock strobing. Invert setting previously set */
697 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
698 case SND_SOC_DAIFMT_NB_NF
:
700 case SND_SOC_DAIFMT_IB_NF
:
701 cr1
^= SAI_XCR1_CKSTR
;
703 case SND_SOC_DAIFMT_NB_IF
:
704 frcr
^= SAI_XFRCR_FSPOL
;
706 case SND_SOC_DAIFMT_IB_IF
:
707 /* Invert fs & sck */
708 cr1
^= SAI_XCR1_CKSTR
;
709 frcr
^= SAI_XFRCR_FSPOL
;
712 dev_err(cpu_dai
->dev
, "Unsupported strobing %#x\n",
713 fmt
& SND_SOC_DAIFMT_INV_MASK
);
716 cr1_mask
|= SAI_XCR1_CKSTR
;
717 frcr_mask
|= SAI_XFRCR_FSPOL
;
719 stm32_sai_sub_reg_up(sai
, STM_SAI_FRCR_REGX
, frcr_mask
, frcr
);
721 /* DAI clock master masks */
722 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
723 case SND_SOC_DAIFMT_CBM_CFM
:
724 /* codec is master */
725 cr1
|= SAI_XCR1_SLAVE
;
728 case SND_SOC_DAIFMT_CBS_CFS
:
732 dev_err(cpu_dai
->dev
, "Unsupported mode %#x\n",
733 fmt
& SND_SOC_DAIFMT_MASTER_MASK
);
737 /* Set slave mode if sub-block is synchronized with another SAI */
739 dev_dbg(cpu_dai
->dev
, "Synchronized SAI configured as slave\n");
740 cr1
|= SAI_XCR1_SLAVE
;
744 cr1_mask
|= SAI_XCR1_SLAVE
;
747 ret
= stm32_sai_sub_reg_up(sai
, STM_SAI_CR1_REGX
, cr1_mask
, cr1
);
749 dev_err(cpu_dai
->dev
, "Failed to update CR1 register\n");
758 static int stm32_sai_startup(struct snd_pcm_substream
*substream
,
759 struct snd_soc_dai
*cpu_dai
)
761 struct stm32_sai_sub_data
*sai
= snd_soc_dai_get_drvdata(cpu_dai
);
765 spin_lock_irqsave(&sai
->irq_lock
, flags
);
766 sai
->substream
= substream
;
767 spin_unlock_irqrestore(&sai
->irq_lock
, flags
);
769 if (STM_SAI_PROTOCOL_IS_SPDIF(sai
)) {
770 snd_pcm_hw_constraint_mask64(substream
->runtime
,
771 SNDRV_PCM_HW_PARAM_FORMAT
,
772 SNDRV_PCM_FMTBIT_S32_LE
);
773 snd_pcm_hw_constraint_single(substream
->runtime
,
774 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
777 ret
= clk_prepare_enable(sai
->sai_ck
);
779 dev_err(cpu_dai
->dev
, "Failed to enable clock: %d\n", ret
);
784 stm32_sai_sub_reg_wr(sai
, STM_SAI_CLRFR_REGX
,
785 SAI_XCLRFR_MASK
, SAI_XCLRFR_MASK
);
787 imr
= SAI_XIMR_OVRUDRIE
;
788 if (STM_SAI_IS_CAPTURE(sai
)) {
789 stm32_sai_sub_reg_rd(sai
, STM_SAI_CR2_REGX
, &cr2
);
790 if (cr2
& SAI_XCR2_MUTECNT_MASK
)
791 imr
|= SAI_XIMR_MUTEDETIE
;
795 imr
|= SAI_XIMR_WCKCFGIE
;
797 imr
|= SAI_XIMR_AFSDETIE
| SAI_XIMR_LFSDETIE
;
799 stm32_sai_sub_reg_up(sai
, STM_SAI_IMR_REGX
,
805 static int stm32_sai_set_config(struct snd_soc_dai
*cpu_dai
,
806 struct snd_pcm_substream
*substream
,
807 struct snd_pcm_hw_params
*params
)
809 struct stm32_sai_sub_data
*sai
= snd_soc_dai_get_drvdata(cpu_dai
);
810 int cr1
, cr1_mask
, ret
;
813 * DMA bursts increment is set to 4 words.
814 * SAI fifo threshold is set to half fifo, to keep enough space
815 * for DMA incoming bursts.
817 stm32_sai_sub_reg_wr(sai
, STM_SAI_CR2_REGX
,
818 SAI_XCR2_FFLUSH
| SAI_XCR2_FTH_MASK
,
820 SAI_XCR2_FTH_SET(STM_SAI_FIFO_TH_HALF
));
822 /* DS bits in CR1 not set for SPDIF (size forced to 24 bits).*/
823 if (STM_SAI_PROTOCOL_IS_SPDIF(sai
)) {
824 sai
->spdif_frm_cnt
= 0;
828 /* Mode, data format and channel config */
829 cr1_mask
= SAI_XCR1_DS_MASK
;
830 switch (params_format(params
)) {
831 case SNDRV_PCM_FORMAT_S8
:
832 cr1
= SAI_XCR1_DS_SET(SAI_DATASIZE_8
);
834 case SNDRV_PCM_FORMAT_S16_LE
:
835 cr1
= SAI_XCR1_DS_SET(SAI_DATASIZE_16
);
837 case SNDRV_PCM_FORMAT_S32_LE
:
838 cr1
= SAI_XCR1_DS_SET(SAI_DATASIZE_32
);
841 dev_err(cpu_dai
->dev
, "Data format not supported\n");
845 cr1_mask
|= SAI_XCR1_MONO
;
846 if ((sai
->slots
== 2) && (params_channels(params
) == 1))
847 cr1
|= SAI_XCR1_MONO
;
849 ret
= stm32_sai_sub_reg_up(sai
, STM_SAI_CR1_REGX
, cr1_mask
, cr1
);
851 dev_err(cpu_dai
->dev
, "Failed to update CR1 register\n");
858 static int stm32_sai_set_slots(struct snd_soc_dai
*cpu_dai
)
860 struct stm32_sai_sub_data
*sai
= snd_soc_dai_get_drvdata(cpu_dai
);
863 stm32_sai_sub_reg_rd(sai
, STM_SAI_SLOTR_REGX
, &slotr
);
866 * If SLOTSZ is set to auto in SLOTR, align slot width on data size
867 * By default slot width = data size, if not forced from DT
869 slot_sz
= slotr
& SAI_XSLOTR_SLOTSZ_MASK
;
870 if (slot_sz
== SAI_XSLOTR_SLOTSZ_SET(SAI_SLOT_SIZE_AUTO
))
871 sai
->slot_width
= sai
->data_size
;
873 if (sai
->slot_width
< sai
->data_size
) {
874 dev_err(cpu_dai
->dev
,
875 "Data size %d larger than slot width\n",
880 /* Slot number is set to 2, if not specified in DT */
884 /* The number of slots in the audio frame is equal to NBSLOT[3:0] + 1*/
885 stm32_sai_sub_reg_up(sai
, STM_SAI_SLOTR_REGX
,
886 SAI_XSLOTR_NBSLOT_MASK
,
887 SAI_XSLOTR_NBSLOT_SET((sai
->slots
- 1)));
889 /* Set default slots mask if not already set from DT */
890 if (!(slotr
& SAI_XSLOTR_SLOTEN_MASK
)) {
891 sai
->slot_mask
= (1 << sai
->slots
) - 1;
892 stm32_sai_sub_reg_up(sai
,
893 STM_SAI_SLOTR_REGX
, SAI_XSLOTR_SLOTEN_MASK
,
894 SAI_XSLOTR_SLOTEN_SET(sai
->slot_mask
));
897 dev_dbg(cpu_dai
->dev
, "Slots %d, slot width %d\n",
898 sai
->slots
, sai
->slot_width
);
903 static void stm32_sai_set_frame(struct snd_soc_dai
*cpu_dai
)
905 struct stm32_sai_sub_data
*sai
= snd_soc_dai_get_drvdata(cpu_dai
);
906 int fs_active
, offset
, format
;
909 format
= sai
->fmt
& SND_SOC_DAIFMT_FORMAT_MASK
;
910 sai
->fs_length
= sai
->slot_width
* sai
->slots
;
912 fs_active
= sai
->fs_length
/ 2;
913 if ((format
== SND_SOC_DAIFMT_DSP_A
) ||
914 (format
== SND_SOC_DAIFMT_DSP_B
))
917 frcr
= SAI_XFRCR_FRL_SET((sai
->fs_length
- 1));
918 frcr
|= SAI_XFRCR_FSALL_SET((fs_active
- 1));
919 frcr_mask
= SAI_XFRCR_FRL_MASK
| SAI_XFRCR_FSALL_MASK
;
921 dev_dbg(cpu_dai
->dev
, "Frame length %d, frame active %d\n",
922 sai
->fs_length
, fs_active
);
924 stm32_sai_sub_reg_up(sai
, STM_SAI_FRCR_REGX
, frcr_mask
, frcr
);
926 if ((sai
->fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) == SND_SOC_DAIFMT_LSB
) {
927 offset
= sai
->slot_width
- sai
->data_size
;
929 stm32_sai_sub_reg_up(sai
, STM_SAI_SLOTR_REGX
,
930 SAI_XSLOTR_FBOFF_MASK
,
931 SAI_XSLOTR_FBOFF_SET(offset
));
935 static void stm32_sai_init_iec958_status(struct stm32_sai_sub_data
*sai
)
937 unsigned char *cs
= sai
->iec958
.status
;
939 cs
[0] = IEC958_AES0_CON_NOT_COPYRIGHT
| IEC958_AES0_CON_EMPHASIS_NONE
;
940 cs
[1] = IEC958_AES1_CON_GENERAL
;
941 cs
[2] = IEC958_AES2_CON_SOURCE_UNSPEC
| IEC958_AES2_CON_CHANNEL_UNSPEC
;
942 cs
[3] = IEC958_AES3_CON_CLOCK_1000PPM
| IEC958_AES3_CON_FS_NOTID
;
945 static void stm32_sai_set_iec958_status(struct stm32_sai_sub_data
*sai
,
946 struct snd_pcm_runtime
*runtime
)
951 /* Force the sample rate according to runtime rate */
952 mutex_lock(&sai
->ctrl_lock
);
953 switch (runtime
->rate
) {
955 sai
->iec958
.status
[3] = IEC958_AES3_CON_FS_22050
;
958 sai
->iec958
.status
[3] = IEC958_AES3_CON_FS_44100
;
961 sai
->iec958
.status
[3] = IEC958_AES3_CON_FS_88200
;
964 sai
->iec958
.status
[3] = IEC958_AES3_CON_FS_176400
;
967 sai
->iec958
.status
[3] = IEC958_AES3_CON_FS_24000
;
970 sai
->iec958
.status
[3] = IEC958_AES3_CON_FS_48000
;
973 sai
->iec958
.status
[3] = IEC958_AES3_CON_FS_96000
;
976 sai
->iec958
.status
[3] = IEC958_AES3_CON_FS_192000
;
979 sai
->iec958
.status
[3] = IEC958_AES3_CON_FS_32000
;
982 sai
->iec958
.status
[3] = IEC958_AES3_CON_FS_NOTID
;
985 mutex_unlock(&sai
->ctrl_lock
);
988 static int stm32_sai_configure_clock(struct snd_soc_dai
*cpu_dai
,
989 struct snd_pcm_hw_params
*params
)
991 struct stm32_sai_sub_data
*sai
= snd_soc_dai_get_drvdata(cpu_dai
);
992 int div
= 0, cr1
= 0;
993 int sai_clk_rate
, mclk_ratio
, den
;
994 unsigned int rate
= params_rate(params
);
997 if (!sai
->sai_mclk
) {
998 ret
= stm32_sai_set_parent_clock(sai
, rate
);
1002 sai_clk_rate
= clk_get_rate(sai
->sai_ck
);
1004 if (STM_SAI_IS_F4(sai
->pdata
)) {
1005 /* mclk on (NODIV=0)
1006 * mclk_rate = 256 * fs
1007 * MCKDIV = 0 if sai_ck < 3/2 * mclk_rate
1008 * MCKDIV = sai_ck / (2 * mclk_rate) otherwise
1009 * mclk off (NODIV=1)
1010 * MCKDIV ignored. sck = sai_ck
1012 if (!sai
->mclk_rate
)
1015 if (2 * sai_clk_rate
>= 3 * sai
->mclk_rate
) {
1016 div
= stm32_sai_get_clk_div(sai
, sai_clk_rate
,
1017 2 * sai
->mclk_rate
);
1025 * MCKDIV = sai_ck / (ws x 256) (NOMCK=0. OSR=0)
1026 * MCKDIV = sai_ck / (ws x 512) (NOMCK=0. OSR=1)
1028 * MCKDIV = sai_ck / (frl x ws) (NOMCK=1)
1029 * Note: NOMCK/NODIV correspond to same bit.
1031 if (STM_SAI_PROTOCOL_IS_SPDIF(sai
)) {
1032 div
= stm32_sai_get_clk_div(sai
, sai_clk_rate
,
1037 if (sai
->mclk_rate
) {
1038 mclk_ratio
= sai
->mclk_rate
/ rate
;
1039 if (mclk_ratio
== 512) {
1041 } else if (mclk_ratio
!= 256) {
1042 dev_err(cpu_dai
->dev
,
1043 "Wrong mclk ratio %d\n",
1048 stm32_sai_sub_reg_up(sai
,
1052 div
= stm32_sai_get_clk_div(sai
, sai_clk_rate
,
1057 /* mclk-fs not set, master clock not active */
1058 den
= sai
->fs_length
* params_rate(params
);
1059 div
= stm32_sai_get_clk_div(sai
, sai_clk_rate
,
1067 return stm32_sai_set_clk_div(sai
, div
);
1070 static int stm32_sai_hw_params(struct snd_pcm_substream
*substream
,
1071 struct snd_pcm_hw_params
*params
,
1072 struct snd_soc_dai
*cpu_dai
)
1074 struct stm32_sai_sub_data
*sai
= snd_soc_dai_get_drvdata(cpu_dai
);
1077 sai
->data_size
= params_width(params
);
1079 if (STM_SAI_PROTOCOL_IS_SPDIF(sai
)) {
1080 /* Rate not already set in runtime structure */
1081 substream
->runtime
->rate
= params_rate(params
);
1082 stm32_sai_set_iec958_status(sai
, substream
->runtime
);
1084 ret
= stm32_sai_set_slots(cpu_dai
);
1087 stm32_sai_set_frame(cpu_dai
);
1090 ret
= stm32_sai_set_config(cpu_dai
, substream
, params
);
1095 ret
= stm32_sai_configure_clock(cpu_dai
, params
);
1100 static int stm32_sai_trigger(struct snd_pcm_substream
*substream
, int cmd
,
1101 struct snd_soc_dai
*cpu_dai
)
1103 struct stm32_sai_sub_data
*sai
= snd_soc_dai_get_drvdata(cpu_dai
);
1107 case SNDRV_PCM_TRIGGER_START
:
1108 case SNDRV_PCM_TRIGGER_RESUME
:
1109 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1110 dev_dbg(cpu_dai
->dev
, "Enable DMA and SAI\n");
1112 stm32_sai_sub_reg_up(sai
, STM_SAI_CR1_REGX
,
1113 SAI_XCR1_DMAEN
, SAI_XCR1_DMAEN
);
1116 ret
= stm32_sai_sub_reg_up(sai
, STM_SAI_CR1_REGX
,
1117 SAI_XCR1_SAIEN
, SAI_XCR1_SAIEN
);
1119 dev_err(cpu_dai
->dev
, "Failed to update CR1 register\n");
1121 case SNDRV_PCM_TRIGGER_SUSPEND
:
1122 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1123 case SNDRV_PCM_TRIGGER_STOP
:
1124 dev_dbg(cpu_dai
->dev
, "Disable DMA and SAI\n");
1126 stm32_sai_sub_reg_up(sai
, STM_SAI_IMR_REGX
,
1129 stm32_sai_sub_reg_up(sai
, STM_SAI_CR1_REGX
,
1131 (unsigned int)~SAI_XCR1_SAIEN
);
1133 ret
= stm32_sai_sub_reg_up(sai
, STM_SAI_CR1_REGX
,
1135 (unsigned int)~SAI_XCR1_DMAEN
);
1137 dev_err(cpu_dai
->dev
, "Failed to update CR1 register\n");
1139 if (STM_SAI_PROTOCOL_IS_SPDIF(sai
))
1140 sai
->spdif_frm_cnt
= 0;
1149 static void stm32_sai_shutdown(struct snd_pcm_substream
*substream
,
1150 struct snd_soc_dai
*cpu_dai
)
1152 struct stm32_sai_sub_data
*sai
= snd_soc_dai_get_drvdata(cpu_dai
);
1153 unsigned long flags
;
1155 stm32_sai_sub_reg_up(sai
, STM_SAI_IMR_REGX
, SAI_XIMR_MASK
, 0);
1157 clk_disable_unprepare(sai
->sai_ck
);
1159 spin_lock_irqsave(&sai
->irq_lock
, flags
);
1160 sai
->substream
= NULL
;
1161 spin_unlock_irqrestore(&sai
->irq_lock
, flags
);
1164 static int stm32_sai_pcm_new(struct snd_soc_pcm_runtime
*rtd
,
1165 struct snd_soc_dai
*cpu_dai
)
1167 struct stm32_sai_sub_data
*sai
= dev_get_drvdata(cpu_dai
->dev
);
1168 struct snd_kcontrol_new knew
= iec958_ctls
;
1170 if (STM_SAI_PROTOCOL_IS_SPDIF(sai
)) {
1171 dev_dbg(&sai
->pdev
->dev
, "%s: register iec controls", __func__
);
1172 knew
.device
= rtd
->pcm
->device
;
1173 return snd_ctl_add(rtd
->pcm
->card
, snd_ctl_new1(&knew
, sai
));
1179 static int stm32_sai_dai_probe(struct snd_soc_dai
*cpu_dai
)
1181 struct stm32_sai_sub_data
*sai
= dev_get_drvdata(cpu_dai
->dev
);
1182 int cr1
= 0, cr1_mask
, ret
;
1184 sai
->cpu_dai
= cpu_dai
;
1186 sai
->dma_params
.addr
= (dma_addr_t
)(sai
->phys_addr
+ STM_SAI_DR_REGX
);
1188 * DMA supports 4, 8 or 16 burst sizes. Burst size 4 is the best choice,
1189 * as it allows bytes, half-word and words transfers. (See DMA fifos
1192 sai
->dma_params
.maxburst
= 4;
1193 if (sai
->pdata
->conf
.fifo_size
< 8)
1194 sai
->dma_params
.maxburst
= 1;
1195 /* Buswidth will be set by framework at runtime */
1196 sai
->dma_params
.addr_width
= DMA_SLAVE_BUSWIDTH_UNDEFINED
;
1198 if (STM_SAI_IS_PLAYBACK(sai
))
1199 snd_soc_dai_init_dma_data(cpu_dai
, &sai
->dma_params
, NULL
);
1201 snd_soc_dai_init_dma_data(cpu_dai
, NULL
, &sai
->dma_params
);
1203 /* Next settings are not relevant for spdif mode */
1204 if (STM_SAI_PROTOCOL_IS_SPDIF(sai
))
1207 cr1_mask
= SAI_XCR1_RX_TX
;
1208 if (STM_SAI_IS_CAPTURE(sai
))
1209 cr1
|= SAI_XCR1_RX_TX
;
1211 /* Configure synchronization */
1212 if (sai
->sync
== SAI_SYNC_EXTERNAL
) {
1213 /* Configure synchro client and provider */
1214 ret
= sai
->pdata
->set_sync(sai
->pdata
, sai
->np_sync_provider
,
1215 sai
->synco
, sai
->synci
);
1220 cr1_mask
|= SAI_XCR1_SYNCEN_MASK
;
1221 cr1
|= SAI_XCR1_SYNCEN_SET(sai
->sync
);
1223 return stm32_sai_sub_reg_up(sai
, STM_SAI_CR1_REGX
, cr1_mask
, cr1
);
1226 static const struct snd_soc_dai_ops stm32_sai_pcm_dai_ops
= {
1227 .set_sysclk
= stm32_sai_set_sysclk
,
1228 .set_fmt
= stm32_sai_set_dai_fmt
,
1229 .set_tdm_slot
= stm32_sai_set_dai_tdm_slot
,
1230 .startup
= stm32_sai_startup
,
1231 .hw_params
= stm32_sai_hw_params
,
1232 .trigger
= stm32_sai_trigger
,
1233 .shutdown
= stm32_sai_shutdown
,
1236 static int stm32_sai_pcm_process_spdif(struct snd_pcm_substream
*substream
,
1237 int channel
, unsigned long hwoff
,
1238 void *buf
, unsigned long bytes
)
1240 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1241 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
1242 struct snd_soc_dai
*cpu_dai
= asoc_rtd_to_cpu(rtd
, 0);
1243 struct stm32_sai_sub_data
*sai
= dev_get_drvdata(cpu_dai
->dev
);
1244 int *ptr
= (int *)(runtime
->dma_area
+ hwoff
+
1245 channel
* (runtime
->dma_bytes
/ runtime
->channels
));
1246 ssize_t cnt
= bytes_to_samples(runtime
, bytes
);
1247 unsigned int frm_cnt
= sai
->spdif_frm_cnt
;
1252 *ptr
= ((*ptr
>> 8) & 0x00ffffff);
1254 /* Set channel status bit */
1255 byte
= frm_cnt
>> 3;
1256 mask
= 1 << (frm_cnt
- (byte
<< 3));
1257 if (sai
->iec958
.status
[byte
] & mask
)
1264 if (frm_cnt
== SAI_IEC60958_BLOCK_FRAMES
)
1267 sai
->spdif_frm_cnt
= frm_cnt
;
1272 /* No support of mmap in S/PDIF mode */
1273 static const struct snd_pcm_hardware stm32_sai_pcm_hw_spdif
= {
1274 .info
= SNDRV_PCM_INFO_INTERLEAVED
,
1275 .buffer_bytes_max
= 8 * PAGE_SIZE
,
1276 .period_bytes_min
= 1024,
1277 .period_bytes_max
= PAGE_SIZE
,
1282 static const struct snd_pcm_hardware stm32_sai_pcm_hw
= {
1283 .info
= SNDRV_PCM_INFO_INTERLEAVED
| SNDRV_PCM_INFO_MMAP
,
1284 .buffer_bytes_max
= 8 * PAGE_SIZE
,
1285 .period_bytes_min
= 1024, /* 5ms at 48kHz */
1286 .period_bytes_max
= PAGE_SIZE
,
1291 static struct snd_soc_dai_driver stm32_sai_playback_dai
= {
1292 .probe
= stm32_sai_dai_probe
,
1293 .pcm_new
= stm32_sai_pcm_new
,
1294 .id
= 1, /* avoid call to fmt_single_name() */
1300 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
1301 /* DMA does not support 24 bits transfers */
1303 SNDRV_PCM_FMTBIT_S8
|
1304 SNDRV_PCM_FMTBIT_S16_LE
|
1305 SNDRV_PCM_FMTBIT_S32_LE
,
1307 .ops
= &stm32_sai_pcm_dai_ops
,
1310 static struct snd_soc_dai_driver stm32_sai_capture_dai
= {
1311 .probe
= stm32_sai_dai_probe
,
1312 .id
= 1, /* avoid call to fmt_single_name() */
1318 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
1319 /* DMA does not support 24 bits transfers */
1321 SNDRV_PCM_FMTBIT_S8
|
1322 SNDRV_PCM_FMTBIT_S16_LE
|
1323 SNDRV_PCM_FMTBIT_S32_LE
,
1325 .ops
= &stm32_sai_pcm_dai_ops
,
1328 static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config
= {
1329 .pcm_hardware
= &stm32_sai_pcm_hw
,
1330 .prepare_slave_config
= snd_dmaengine_pcm_prepare_slave_config
,
1333 static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config_spdif
= {
1334 .pcm_hardware
= &stm32_sai_pcm_hw_spdif
,
1335 .prepare_slave_config
= snd_dmaengine_pcm_prepare_slave_config
,
1336 .process
= stm32_sai_pcm_process_spdif
,
1339 static const struct snd_soc_component_driver stm32_component
= {
1340 .name
= "stm32-sai",
1343 static const struct of_device_id stm32_sai_sub_ids
[] = {
1344 { .compatible
= "st,stm32-sai-sub-a",
1345 .data
= (void *)STM_SAI_A_ID
},
1346 { .compatible
= "st,stm32-sai-sub-b",
1347 .data
= (void *)STM_SAI_B_ID
},
1350 MODULE_DEVICE_TABLE(of
, stm32_sai_sub_ids
);
1352 static int stm32_sai_sub_parse_of(struct platform_device
*pdev
,
1353 struct stm32_sai_sub_data
*sai
)
1355 struct device_node
*np
= pdev
->dev
.of_node
;
1356 struct resource
*res
;
1358 struct of_phandle_args args
;
1364 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1365 base
= devm_ioremap_resource(&pdev
->dev
, res
);
1367 return PTR_ERR(base
);
1369 sai
->phys_addr
= res
->start
;
1371 sai
->regmap_config
= &stm32_sai_sub_regmap_config_f4
;
1372 /* Note: PDM registers not available for sub-block B */
1373 if (STM_SAI_HAS_PDM(sai
) && STM_SAI_IS_SUB_A(sai
))
1374 sai
->regmap_config
= &stm32_sai_sub_regmap_config_h7
;
1377 * Do not manage peripheral clock through regmap framework as this
1378 * can lead to circular locking issue with sai master clock provider.
1379 * Manage peripheral clock directly in driver instead.
1381 sai
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, base
,
1382 sai
->regmap_config
);
1383 if (IS_ERR(sai
->regmap
)) {
1384 if (PTR_ERR(sai
->regmap
) != -EPROBE_DEFER
)
1385 dev_err(&pdev
->dev
, "Regmap init error %ld\n",
1386 PTR_ERR(sai
->regmap
));
1387 return PTR_ERR(sai
->regmap
);
1390 /* Get direction property */
1391 if (of_property_match_string(np
, "dma-names", "tx") >= 0) {
1392 sai
->dir
= SNDRV_PCM_STREAM_PLAYBACK
;
1393 } else if (of_property_match_string(np
, "dma-names", "rx") >= 0) {
1394 sai
->dir
= SNDRV_PCM_STREAM_CAPTURE
;
1396 dev_err(&pdev
->dev
, "Unsupported direction\n");
1400 /* Get spdif iec60958 property */
1402 if (of_get_property(np
, "st,iec60958", NULL
)) {
1403 if (!STM_SAI_HAS_SPDIF(sai
) ||
1404 sai
->dir
== SNDRV_PCM_STREAM_CAPTURE
) {
1405 dev_err(&pdev
->dev
, "S/PDIF IEC60958 not supported\n");
1408 stm32_sai_init_iec958_status(sai
);
1413 /* Get synchronization property */
1415 ret
= of_parse_phandle_with_fixed_args(np
, "st,sync", 1, 0, &args
);
1416 if (ret
< 0 && ret
!= -ENOENT
) {
1417 dev_err(&pdev
->dev
, "Failed to get st,sync property\n");
1421 sai
->sync
= SAI_SYNC_NONE
;
1423 if (args
.np
== np
) {
1424 dev_err(&pdev
->dev
, "%pOFn sync own reference\n", np
);
1425 of_node_put(args
.np
);
1429 sai
->np_sync_provider
= of_get_parent(args
.np
);
1430 if (!sai
->np_sync_provider
) {
1431 dev_err(&pdev
->dev
, "%pOFn parent node not found\n",
1433 of_node_put(args
.np
);
1437 sai
->sync
= SAI_SYNC_INTERNAL
;
1438 if (sai
->np_sync_provider
!= sai
->pdata
->pdev
->dev
.of_node
) {
1439 if (!STM_SAI_HAS_EXT_SYNC(sai
)) {
1441 "External synchro not supported\n");
1442 of_node_put(args
.np
);
1445 sai
->sync
= SAI_SYNC_EXTERNAL
;
1447 sai
->synci
= args
.args
[0];
1448 if (sai
->synci
< 1 ||
1449 (sai
->synci
> (SAI_GCR_SYNCIN_MAX
+ 1))) {
1450 dev_err(&pdev
->dev
, "Wrong SAI index\n");
1451 of_node_put(args
.np
);
1455 if (of_property_match_string(args
.np
, "compatible",
1456 "st,stm32-sai-sub-a") >= 0)
1457 sai
->synco
= STM_SAI_SYNC_OUT_A
;
1459 if (of_property_match_string(args
.np
, "compatible",
1460 "st,stm32-sai-sub-b") >= 0)
1461 sai
->synco
= STM_SAI_SYNC_OUT_B
;
1464 dev_err(&pdev
->dev
, "Unknown SAI sub-block\n");
1465 of_node_put(args
.np
);
1470 dev_dbg(&pdev
->dev
, "%s synchronized with %s\n",
1471 pdev
->name
, args
.np
->full_name
);
1474 of_node_put(args
.np
);
1475 sai
->sai_ck
= devm_clk_get(&pdev
->dev
, "sai_ck");
1476 if (IS_ERR(sai
->sai_ck
)) {
1477 if (PTR_ERR(sai
->sai_ck
) != -EPROBE_DEFER
)
1478 dev_err(&pdev
->dev
, "Missing kernel clock sai_ck: %ld\n",
1479 PTR_ERR(sai
->sai_ck
));
1480 return PTR_ERR(sai
->sai_ck
);
1483 ret
= clk_prepare(sai
->pdata
->pclk
);
1487 if (STM_SAI_IS_F4(sai
->pdata
))
1490 /* Register mclk provider if requested */
1491 if (of_find_property(np
, "#clock-cells", NULL
)) {
1492 ret
= stm32_sai_add_mclk_provider(sai
);
1496 sai
->sai_mclk
= devm_clk_get(&pdev
->dev
, "MCLK");
1497 if (IS_ERR(sai
->sai_mclk
)) {
1498 if (PTR_ERR(sai
->sai_mclk
) != -ENOENT
)
1499 return PTR_ERR(sai
->sai_mclk
);
1500 sai
->sai_mclk
= NULL
;
1507 static int stm32_sai_sub_probe(struct platform_device
*pdev
)
1509 struct stm32_sai_sub_data
*sai
;
1510 const struct of_device_id
*of_id
;
1511 const struct snd_dmaengine_pcm_config
*conf
= &stm32_sai_pcm_config
;
1514 sai
= devm_kzalloc(&pdev
->dev
, sizeof(*sai
), GFP_KERNEL
);
1518 of_id
= of_match_device(stm32_sai_sub_ids
, &pdev
->dev
);
1521 sai
->id
= (uintptr_t)of_id
->data
;
1524 mutex_init(&sai
->ctrl_lock
);
1525 spin_lock_init(&sai
->irq_lock
);
1526 platform_set_drvdata(pdev
, sai
);
1528 sai
->pdata
= dev_get_drvdata(pdev
->dev
.parent
);
1530 dev_err(&pdev
->dev
, "Parent device data not available\n");
1534 ret
= stm32_sai_sub_parse_of(pdev
, sai
);
1538 if (STM_SAI_IS_PLAYBACK(sai
))
1539 sai
->cpu_dai_drv
= stm32_sai_playback_dai
;
1541 sai
->cpu_dai_drv
= stm32_sai_capture_dai
;
1542 sai
->cpu_dai_drv
.name
= dev_name(&pdev
->dev
);
1544 ret
= devm_request_irq(&pdev
->dev
, sai
->pdata
->irq
, stm32_sai_isr
,
1545 IRQF_SHARED
, dev_name(&pdev
->dev
), sai
);
1547 dev_err(&pdev
->dev
, "IRQ request returned %d\n", ret
);
1551 if (STM_SAI_PROTOCOL_IS_SPDIF(sai
))
1552 conf
= &stm32_sai_pcm_config_spdif
;
1554 ret
= snd_dmaengine_pcm_register(&pdev
->dev
, conf
, 0);
1556 if (ret
!= -EPROBE_DEFER
)
1557 dev_err(&pdev
->dev
, "Could not register pcm dma\n");
1561 ret
= snd_soc_register_component(&pdev
->dev
, &stm32_component
,
1562 &sai
->cpu_dai_drv
, 1);
1564 snd_dmaengine_pcm_unregister(&pdev
->dev
);
1568 pm_runtime_enable(&pdev
->dev
);
1573 static int stm32_sai_sub_remove(struct platform_device
*pdev
)
1575 struct stm32_sai_sub_data
*sai
= dev_get_drvdata(&pdev
->dev
);
1577 clk_unprepare(sai
->pdata
->pclk
);
1578 snd_dmaengine_pcm_unregister(&pdev
->dev
);
1579 snd_soc_unregister_component(&pdev
->dev
);
1580 pm_runtime_disable(&pdev
->dev
);
1585 #ifdef CONFIG_PM_SLEEP
1586 static int stm32_sai_sub_suspend(struct device
*dev
)
1588 struct stm32_sai_sub_data
*sai
= dev_get_drvdata(dev
);
1591 ret
= clk_enable(sai
->pdata
->pclk
);
1595 regcache_cache_only(sai
->regmap
, true);
1596 regcache_mark_dirty(sai
->regmap
);
1598 clk_disable(sai
->pdata
->pclk
);
1603 static int stm32_sai_sub_resume(struct device
*dev
)
1605 struct stm32_sai_sub_data
*sai
= dev_get_drvdata(dev
);
1608 ret
= clk_enable(sai
->pdata
->pclk
);
1612 regcache_cache_only(sai
->regmap
, false);
1613 ret
= regcache_sync(sai
->regmap
);
1615 clk_disable(sai
->pdata
->pclk
);
1619 #endif /* CONFIG_PM_SLEEP */
1621 static const struct dev_pm_ops stm32_sai_sub_pm_ops
= {
1622 SET_SYSTEM_SLEEP_PM_OPS(stm32_sai_sub_suspend
, stm32_sai_sub_resume
)
1625 static struct platform_driver stm32_sai_sub_driver
= {
1627 .name
= "st,stm32-sai-sub",
1628 .of_match_table
= stm32_sai_sub_ids
,
1629 .pm
= &stm32_sai_sub_pm_ops
,
1631 .probe
= stm32_sai_sub_probe
,
1632 .remove
= stm32_sai_sub_remove
,
1635 module_platform_driver(stm32_sai_sub_driver
);
1637 MODULE_DESCRIPTION("STM32 Soc SAI sub-block Interface");
1638 MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>");
1639 MODULE_ALIAS("platform:st,stm32-sai-sub");
1640 MODULE_LICENSE("GPL v2");