GNU Linux-libre 4.19.245-gnu1
[releases.git] / sound / soc / stm / stm32_sai_sub.c
1 /*
2  * STM32 ALSA SoC Digital Audio Interface (SAI) driver.
3  *
4  * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
5  * Author(s): Olivier Moysan <olivier.moysan@st.com> for STMicroelectronics.
6  *
7  * License terms: GPL V2.0.
8  *
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.
12  *
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
16  * details.
17  */
18
19 #include <linux/clk.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/of_irq.h>
23 #include <linux/of_platform.h>
24 #include <linux/regmap.h>
25
26 #include <sound/asoundef.h>
27 #include <sound/core.h>
28 #include <sound/dmaengine_pcm.h>
29 #include <sound/pcm_params.h>
30
31 #include "stm32_sai.h"
32
33 #define SAI_FREE_PROTOCOL       0x0
34 #define SAI_SPDIF_PROTOCOL      0x1
35
36 #define SAI_SLOT_SIZE_AUTO      0x0
37 #define SAI_SLOT_SIZE_16        0x1
38 #define SAI_SLOT_SIZE_32        0x2
39
40 #define SAI_DATASIZE_8          0x2
41 #define SAI_DATASIZE_10         0x3
42 #define SAI_DATASIZE_16         0x4
43 #define SAI_DATASIZE_20         0x5
44 #define SAI_DATASIZE_24         0x6
45 #define SAI_DATASIZE_32         0x7
46
47 #define STM_SAI_FIFO_SIZE       8
48 #define STM_SAI_DAI_NAME_SIZE   15
49
50 #define STM_SAI_IS_PLAYBACK(ip) ((ip)->dir == SNDRV_PCM_STREAM_PLAYBACK)
51 #define STM_SAI_IS_CAPTURE(ip)  ((ip)->dir == SNDRV_PCM_STREAM_CAPTURE)
52
53 #define STM_SAI_A_ID            0x0
54 #define STM_SAI_B_ID            0x1
55
56 #define STM_SAI_IS_SUB_A(x)     ((x)->id == STM_SAI_A_ID)
57 #define STM_SAI_IS_SUB_B(x)     ((x)->id == STM_SAI_B_ID)
58 #define STM_SAI_BLOCK_NAME(x)   (((x)->id == STM_SAI_A_ID) ? "A" : "B")
59
60 #define SAI_SYNC_NONE           0x0
61 #define SAI_SYNC_INTERNAL       0x1
62 #define SAI_SYNC_EXTERNAL       0x2
63
64 #define STM_SAI_PROTOCOL_IS_SPDIF(ip)   ((ip)->spdif)
65 #define STM_SAI_HAS_SPDIF(x)    ((x)->pdata->conf->has_spdif)
66 #define STM_SAI_HAS_EXT_SYNC(x) (!STM_SAI_IS_F4(sai->pdata))
67
68 #define SAI_IEC60958_BLOCK_FRAMES       192
69 #define SAI_IEC60958_STATUS_BYTES       24
70
71 /**
72  * struct stm32_sai_sub_data - private data of SAI sub block (block A or B)
73  * @pdev: device data pointer
74  * @regmap: SAI register map pointer
75  * @regmap_config: SAI sub block register map configuration pointer
76  * @dma_params: dma configuration data for rx or tx channel
77  * @cpu_dai_drv: DAI driver data pointer
78  * @cpu_dai: DAI runtime data pointer
79  * @substream: PCM substream data pointer
80  * @pdata: SAI block parent data pointer
81  * @np_sync_provider: synchronization provider node
82  * @sai_ck: kernel clock feeding the SAI clock generator
83  * @phys_addr: SAI registers physical base address
84  * @mclk_rate: SAI block master clock frequency (Hz). set at init
85  * @id: SAI sub block id corresponding to sub-block A or B
86  * @dir: SAI block direction (playback or capture). set at init
87  * @master: SAI block mode flag. (true=master, false=slave) set at init
88  * @spdif: SAI S/PDIF iec60958 mode flag. set at init
89  * @fmt: SAI block format. relevant only for custom protocols. set at init
90  * @sync: SAI block synchronization mode. (none, internal or external)
91  * @synco: SAI block ext sync source (provider setting). (none, sub-block A/B)
92  * @synci: SAI block ext sync source (client setting). (SAI sync provider index)
93  * @fs_length: frame synchronization length. depends on protocol settings
94  * @slots: rx or tx slot number
95  * @slot_width: rx or tx slot width in bits
96  * @slot_mask: rx or tx active slots mask. set at init or at runtime
97  * @data_size: PCM data width. corresponds to PCM substream width.
98  * @spdif_frm_cnt: S/PDIF playback frame counter
99  * @iec958: iec958 data
100  * @ctrl_lock: control lock
101  */
102 struct stm32_sai_sub_data {
103         struct platform_device *pdev;
104         struct regmap *regmap;
105         const struct regmap_config *regmap_config;
106         struct snd_dmaengine_dai_dma_data dma_params;
107         struct snd_soc_dai_driver *cpu_dai_drv;
108         struct snd_soc_dai *cpu_dai;
109         struct snd_pcm_substream *substream;
110         struct stm32_sai_data *pdata;
111         struct device_node *np_sync_provider;
112         struct clk *sai_ck;
113         dma_addr_t phys_addr;
114         unsigned int mclk_rate;
115         unsigned int id;
116         int dir;
117         bool master;
118         bool spdif;
119         int fmt;
120         int sync;
121         int synco;
122         int synci;
123         int fs_length;
124         int slots;
125         int slot_width;
126         int slot_mask;
127         int data_size;
128         unsigned int spdif_frm_cnt;
129         struct snd_aes_iec958 iec958;
130         struct mutex ctrl_lock; /* protect resources accessed by controls */
131 };
132
133 enum stm32_sai_fifo_th {
134         STM_SAI_FIFO_TH_EMPTY,
135         STM_SAI_FIFO_TH_QUARTER,
136         STM_SAI_FIFO_TH_HALF,
137         STM_SAI_FIFO_TH_3_QUARTER,
138         STM_SAI_FIFO_TH_FULL,
139 };
140
141 static bool stm32_sai_sub_readable_reg(struct device *dev, unsigned int reg)
142 {
143         switch (reg) {
144         case STM_SAI_CR1_REGX:
145         case STM_SAI_CR2_REGX:
146         case STM_SAI_FRCR_REGX:
147         case STM_SAI_SLOTR_REGX:
148         case STM_SAI_IMR_REGX:
149         case STM_SAI_SR_REGX:
150         case STM_SAI_CLRFR_REGX:
151         case STM_SAI_DR_REGX:
152         case STM_SAI_PDMCR_REGX:
153         case STM_SAI_PDMLY_REGX:
154                 return true;
155         default:
156                 return false;
157         }
158 }
159
160 static bool stm32_sai_sub_volatile_reg(struct device *dev, unsigned int reg)
161 {
162         switch (reg) {
163         case STM_SAI_DR_REGX:
164                 return true;
165         default:
166                 return false;
167         }
168 }
169
170 static bool stm32_sai_sub_writeable_reg(struct device *dev, unsigned int reg)
171 {
172         switch (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_SR_REGX:
179         case STM_SAI_CLRFR_REGX:
180         case STM_SAI_DR_REGX:
181         case STM_SAI_PDMCR_REGX:
182         case STM_SAI_PDMLY_REGX:
183                 return true;
184         default:
185                 return false;
186         }
187 }
188
189 static const struct regmap_config stm32_sai_sub_regmap_config_f4 = {
190         .reg_bits = 32,
191         .reg_stride = 4,
192         .val_bits = 32,
193         .max_register = STM_SAI_DR_REGX,
194         .readable_reg = stm32_sai_sub_readable_reg,
195         .volatile_reg = stm32_sai_sub_volatile_reg,
196         .writeable_reg = stm32_sai_sub_writeable_reg,
197         .fast_io = true,
198 };
199
200 static const struct regmap_config stm32_sai_sub_regmap_config_h7 = {
201         .reg_bits = 32,
202         .reg_stride = 4,
203         .val_bits = 32,
204         .max_register = STM_SAI_PDMLY_REGX,
205         .readable_reg = stm32_sai_sub_readable_reg,
206         .volatile_reg = stm32_sai_sub_volatile_reg,
207         .writeable_reg = stm32_sai_sub_writeable_reg,
208         .fast_io = true,
209 };
210
211 static int snd_pcm_iec958_info(struct snd_kcontrol *kcontrol,
212                                struct snd_ctl_elem_info *uinfo)
213 {
214         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
215         uinfo->count = 1;
216
217         return 0;
218 }
219
220 static int snd_pcm_iec958_get(struct snd_kcontrol *kcontrol,
221                               struct snd_ctl_elem_value *uctl)
222 {
223         struct stm32_sai_sub_data *sai = snd_kcontrol_chip(kcontrol);
224
225         mutex_lock(&sai->ctrl_lock);
226         memcpy(uctl->value.iec958.status, sai->iec958.status, 4);
227         mutex_unlock(&sai->ctrl_lock);
228
229         return 0;
230 }
231
232 static int snd_pcm_iec958_put(struct snd_kcontrol *kcontrol,
233                               struct snd_ctl_elem_value *uctl)
234 {
235         struct stm32_sai_sub_data *sai = snd_kcontrol_chip(kcontrol);
236
237         mutex_lock(&sai->ctrl_lock);
238         memcpy(sai->iec958.status, uctl->value.iec958.status, 4);
239         mutex_unlock(&sai->ctrl_lock);
240
241         return 0;
242 }
243
244 static const struct snd_kcontrol_new iec958_ctls = {
245         .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
246                         SNDRV_CTL_ELEM_ACCESS_VOLATILE),
247         .iface = SNDRV_CTL_ELEM_IFACE_PCM,
248         .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
249         .info = snd_pcm_iec958_info,
250         .get = snd_pcm_iec958_get,
251         .put = snd_pcm_iec958_put,
252 };
253
254 static irqreturn_t stm32_sai_isr(int irq, void *devid)
255 {
256         struct stm32_sai_sub_data *sai = (struct stm32_sai_sub_data *)devid;
257         struct platform_device *pdev = sai->pdev;
258         unsigned int sr, imr, flags;
259         snd_pcm_state_t status = SNDRV_PCM_STATE_RUNNING;
260
261         regmap_read(sai->regmap, STM_SAI_IMR_REGX, &imr);
262         regmap_read(sai->regmap, STM_SAI_SR_REGX, &sr);
263
264         flags = sr & imr;
265         if (!flags)
266                 return IRQ_NONE;
267
268         regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX, SAI_XCLRFR_MASK,
269                            SAI_XCLRFR_MASK);
270
271         if (!sai->substream) {
272                 dev_err(&pdev->dev, "Device stopped. Spurious IRQ 0x%x\n", sr);
273                 return IRQ_NONE;
274         }
275
276         if (flags & SAI_XIMR_OVRUDRIE) {
277                 dev_err(&pdev->dev, "IRQ %s\n",
278                         STM_SAI_IS_PLAYBACK(sai) ? "underrun" : "overrun");
279                 status = SNDRV_PCM_STATE_XRUN;
280         }
281
282         if (flags & SAI_XIMR_MUTEDETIE)
283                 dev_dbg(&pdev->dev, "IRQ mute detected\n");
284
285         if (flags & SAI_XIMR_WCKCFGIE) {
286                 dev_err(&pdev->dev, "IRQ wrong clock configuration\n");
287                 status = SNDRV_PCM_STATE_DISCONNECTED;
288         }
289
290         if (flags & SAI_XIMR_CNRDYIE)
291                 dev_err(&pdev->dev, "IRQ Codec not ready\n");
292
293         if (flags & SAI_XIMR_AFSDETIE) {
294                 dev_err(&pdev->dev, "IRQ Anticipated frame synchro\n");
295                 status = SNDRV_PCM_STATE_XRUN;
296         }
297
298         if (flags & SAI_XIMR_LFSDETIE) {
299                 dev_err(&pdev->dev, "IRQ Late frame synchro\n");
300                 status = SNDRV_PCM_STATE_XRUN;
301         }
302
303         if (status != SNDRV_PCM_STATE_RUNNING)
304                 snd_pcm_stop_xrun(sai->substream);
305
306         return IRQ_HANDLED;
307 }
308
309 static int stm32_sai_set_sysclk(struct snd_soc_dai *cpu_dai,
310                                 int clk_id, unsigned int freq, int dir)
311 {
312         struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
313         int ret;
314
315         if ((dir == SND_SOC_CLOCK_OUT) && sai->master) {
316                 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
317                                          SAI_XCR1_NODIV,
318                                          (unsigned int)~SAI_XCR1_NODIV);
319                 if (ret < 0)
320                         return ret;
321
322                 sai->mclk_rate = freq;
323                 dev_dbg(cpu_dai->dev, "SAI MCLK frequency is %uHz\n", freq);
324         }
325
326         return 0;
327 }
328
329 static int stm32_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
330                                       u32 rx_mask, int slots, int slot_width)
331 {
332         struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
333         int slotr, slotr_mask, slot_size;
334
335         if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
336                 dev_warn(cpu_dai->dev, "Slot setting relevant only for TDM\n");
337                 return 0;
338         }
339
340         dev_dbg(cpu_dai->dev, "Masks tx/rx:%#x/%#x, slots:%d, width:%d\n",
341                 tx_mask, rx_mask, slots, slot_width);
342
343         switch (slot_width) {
344         case 16:
345                 slot_size = SAI_SLOT_SIZE_16;
346                 break;
347         case 32:
348                 slot_size = SAI_SLOT_SIZE_32;
349                 break;
350         default:
351                 slot_size = SAI_SLOT_SIZE_AUTO;
352                 break;
353         }
354
355         slotr = SAI_XSLOTR_SLOTSZ_SET(slot_size) |
356                 SAI_XSLOTR_NBSLOT_SET(slots - 1);
357         slotr_mask = SAI_XSLOTR_SLOTSZ_MASK | SAI_XSLOTR_NBSLOT_MASK;
358
359         /* tx/rx mask set in machine init, if slot number defined in DT */
360         if (STM_SAI_IS_PLAYBACK(sai)) {
361                 sai->slot_mask = tx_mask;
362                 slotr |= SAI_XSLOTR_SLOTEN_SET(tx_mask);
363         }
364
365         if (STM_SAI_IS_CAPTURE(sai)) {
366                 sai->slot_mask = rx_mask;
367                 slotr |= SAI_XSLOTR_SLOTEN_SET(rx_mask);
368         }
369
370         slotr_mask |= SAI_XSLOTR_SLOTEN_MASK;
371
372         regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, slotr_mask, slotr);
373
374         sai->slot_width = slot_width;
375         sai->slots = slots;
376
377         return 0;
378 }
379
380 static int stm32_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
381 {
382         struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
383         int cr1, frcr = 0;
384         int cr1_mask, frcr_mask = 0;
385         int ret;
386
387         dev_dbg(cpu_dai->dev, "fmt %x\n", fmt);
388
389         /* Do not generate master by default */
390         cr1 = SAI_XCR1_NODIV;
391         cr1_mask = SAI_XCR1_NODIV;
392
393         cr1_mask |= SAI_XCR1_PRTCFG_MASK;
394         if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
395                 cr1 |= SAI_XCR1_PRTCFG_SET(SAI_SPDIF_PROTOCOL);
396                 goto conf_update;
397         }
398
399         cr1 |= SAI_XCR1_PRTCFG_SET(SAI_FREE_PROTOCOL);
400
401         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
402         /* SCK active high for all protocols */
403         case SND_SOC_DAIFMT_I2S:
404                 cr1 |= SAI_XCR1_CKSTR;
405                 frcr |= SAI_XFRCR_FSOFF | SAI_XFRCR_FSDEF;
406                 break;
407         /* Left justified */
408         case SND_SOC_DAIFMT_MSB:
409                 frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF;
410                 break;
411         /* Right justified */
412         case SND_SOC_DAIFMT_LSB:
413                 frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF;
414                 break;
415         case SND_SOC_DAIFMT_DSP_A:
416                 frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF;
417                 break;
418         case SND_SOC_DAIFMT_DSP_B:
419                 frcr |= SAI_XFRCR_FSPOL;
420                 break;
421         default:
422                 dev_err(cpu_dai->dev, "Unsupported protocol %#x\n",
423                         fmt & SND_SOC_DAIFMT_FORMAT_MASK);
424                 return -EINVAL;
425         }
426
427         cr1_mask |= SAI_XCR1_CKSTR;
428         frcr_mask |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF |
429                      SAI_XFRCR_FSDEF;
430
431         /* DAI clock strobing. Invert setting previously set */
432         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
433         case SND_SOC_DAIFMT_NB_NF:
434                 break;
435         case SND_SOC_DAIFMT_IB_NF:
436                 cr1 ^= SAI_XCR1_CKSTR;
437                 break;
438         case SND_SOC_DAIFMT_NB_IF:
439                 frcr ^= SAI_XFRCR_FSPOL;
440                 break;
441         case SND_SOC_DAIFMT_IB_IF:
442                 /* Invert fs & sck */
443                 cr1 ^= SAI_XCR1_CKSTR;
444                 frcr ^= SAI_XFRCR_FSPOL;
445                 break;
446         default:
447                 dev_err(cpu_dai->dev, "Unsupported strobing %#x\n",
448                         fmt & SND_SOC_DAIFMT_INV_MASK);
449                 return -EINVAL;
450         }
451         cr1_mask |= SAI_XCR1_CKSTR;
452         frcr_mask |= SAI_XFRCR_FSPOL;
453
454         regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr);
455
456         /* DAI clock master masks */
457         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
458         case SND_SOC_DAIFMT_CBM_CFM:
459                 /* codec is master */
460                 cr1 |= SAI_XCR1_SLAVE;
461                 sai->master = false;
462                 break;
463         case SND_SOC_DAIFMT_CBS_CFS:
464                 sai->master = true;
465                 break;
466         default:
467                 dev_err(cpu_dai->dev, "Unsupported mode %#x\n",
468                         fmt & SND_SOC_DAIFMT_MASTER_MASK);
469                 return -EINVAL;
470         }
471
472         /* Set slave mode if sub-block is synchronized with another SAI */
473         if (sai->sync) {
474                 dev_dbg(cpu_dai->dev, "Synchronized SAI configured as slave\n");
475                 cr1 |= SAI_XCR1_SLAVE;
476                 sai->master = false;
477         }
478
479         cr1_mask |= SAI_XCR1_SLAVE;
480
481 conf_update:
482         ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1);
483         if (ret < 0) {
484                 dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
485                 return ret;
486         }
487
488         sai->fmt = fmt;
489
490         return 0;
491 }
492
493 static int stm32_sai_startup(struct snd_pcm_substream *substream,
494                              struct snd_soc_dai *cpu_dai)
495 {
496         struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
497         int imr, cr2, ret;
498
499         sai->substream = substream;
500
501         if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
502                 snd_pcm_hw_constraint_mask64(substream->runtime,
503                                              SNDRV_PCM_HW_PARAM_FORMAT,
504                                              SNDRV_PCM_FMTBIT_S32_LE);
505                 snd_pcm_hw_constraint_single(substream->runtime,
506                                              SNDRV_PCM_HW_PARAM_CHANNELS, 2);
507         }
508
509         ret = clk_prepare_enable(sai->sai_ck);
510         if (ret < 0) {
511                 dev_err(cpu_dai->dev, "Failed to enable clock: %d\n", ret);
512                 return ret;
513         }
514
515         /* Enable ITs */
516
517         regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX,
518                            SAI_XCLRFR_MASK, SAI_XCLRFR_MASK);
519
520         imr = SAI_XIMR_OVRUDRIE;
521         if (STM_SAI_IS_CAPTURE(sai)) {
522                 regmap_read(sai->regmap, STM_SAI_CR2_REGX, &cr2);
523                 if (cr2 & SAI_XCR2_MUTECNT_MASK)
524                         imr |= SAI_XIMR_MUTEDETIE;
525         }
526
527         if (sai->master)
528                 imr |= SAI_XIMR_WCKCFGIE;
529         else
530                 imr |= SAI_XIMR_AFSDETIE | SAI_XIMR_LFSDETIE;
531
532         regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX,
533                            SAI_XIMR_MASK, imr);
534
535         return 0;
536 }
537
538 static int stm32_sai_set_config(struct snd_soc_dai *cpu_dai,
539                                 struct snd_pcm_substream *substream,
540                                 struct snd_pcm_hw_params *params)
541 {
542         struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
543         int cr1, cr1_mask, ret;
544
545         /*
546          * DMA bursts increment is set to 4 words.
547          * SAI fifo threshold is set to half fifo, to keep enough space
548          * for DMA incoming bursts.
549          */
550         regmap_update_bits(sai->regmap, STM_SAI_CR2_REGX,
551                            SAI_XCR2_FFLUSH | SAI_XCR2_FTH_MASK,
552                            SAI_XCR2_FFLUSH |
553                            SAI_XCR2_FTH_SET(STM_SAI_FIFO_TH_HALF));
554
555         /* DS bits in CR1 not set for SPDIF (size forced to 24 bits).*/
556         if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
557                 sai->spdif_frm_cnt = 0;
558                 return 0;
559         }
560
561         /* Mode, data format and channel config */
562         cr1_mask = SAI_XCR1_DS_MASK;
563         switch (params_format(params)) {
564         case SNDRV_PCM_FORMAT_S8:
565                 cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_8);
566                 break;
567         case SNDRV_PCM_FORMAT_S16_LE:
568                 cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_16);
569                 break;
570         case SNDRV_PCM_FORMAT_S32_LE:
571                 cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_32);
572                 break;
573         default:
574                 dev_err(cpu_dai->dev, "Data format not supported");
575                 return -EINVAL;
576         }
577
578         cr1_mask |= SAI_XCR1_MONO;
579         if ((sai->slots == 2) && (params_channels(params) == 1))
580                 cr1 |= SAI_XCR1_MONO;
581
582         ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1);
583         if (ret < 0) {
584                 dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
585                 return ret;
586         }
587
588         return 0;
589 }
590
591 static int stm32_sai_set_slots(struct snd_soc_dai *cpu_dai)
592 {
593         struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
594         int slotr, slot_sz;
595
596         regmap_read(sai->regmap, STM_SAI_SLOTR_REGX, &slotr);
597
598         /*
599          * If SLOTSZ is set to auto in SLOTR, align slot width on data size
600          * By default slot width = data size, if not forced from DT
601          */
602         slot_sz = slotr & SAI_XSLOTR_SLOTSZ_MASK;
603         if (slot_sz == SAI_XSLOTR_SLOTSZ_SET(SAI_SLOT_SIZE_AUTO))
604                 sai->slot_width = sai->data_size;
605
606         if (sai->slot_width < sai->data_size) {
607                 dev_err(cpu_dai->dev,
608                         "Data size %d larger than slot width\n",
609                         sai->data_size);
610                 return -EINVAL;
611         }
612
613         /* Slot number is set to 2, if not specified in DT */
614         if (!sai->slots)
615                 sai->slots = 2;
616
617         /* The number of slots in the audio frame is equal to NBSLOT[3:0] + 1*/
618         regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX,
619                            SAI_XSLOTR_NBSLOT_MASK,
620                            SAI_XSLOTR_NBSLOT_SET((sai->slots - 1)));
621
622         /* Set default slots mask if not already set from DT */
623         if (!(slotr & SAI_XSLOTR_SLOTEN_MASK)) {
624                 sai->slot_mask = (1 << sai->slots) - 1;
625                 regmap_update_bits(sai->regmap,
626                                    STM_SAI_SLOTR_REGX, SAI_XSLOTR_SLOTEN_MASK,
627                                    SAI_XSLOTR_SLOTEN_SET(sai->slot_mask));
628         }
629
630         dev_dbg(cpu_dai->dev, "Slots %d, slot width %d\n",
631                 sai->slots, sai->slot_width);
632
633         return 0;
634 }
635
636 static void stm32_sai_set_frame(struct snd_soc_dai *cpu_dai)
637 {
638         struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
639         int fs_active, offset, format;
640         int frcr, frcr_mask;
641
642         format = sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
643         sai->fs_length = sai->slot_width * sai->slots;
644
645         fs_active = sai->fs_length / 2;
646         if ((format == SND_SOC_DAIFMT_DSP_A) ||
647             (format == SND_SOC_DAIFMT_DSP_B))
648                 fs_active = 1;
649
650         frcr = SAI_XFRCR_FRL_SET((sai->fs_length - 1));
651         frcr |= SAI_XFRCR_FSALL_SET((fs_active - 1));
652         frcr_mask = SAI_XFRCR_FRL_MASK | SAI_XFRCR_FSALL_MASK;
653
654         dev_dbg(cpu_dai->dev, "Frame length %d, frame active %d\n",
655                 sai->fs_length, fs_active);
656
657         regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr);
658
659         if ((sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_LSB) {
660                 offset = sai->slot_width - sai->data_size;
661
662                 regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX,
663                                    SAI_XSLOTR_FBOFF_MASK,
664                                    SAI_XSLOTR_FBOFF_SET(offset));
665         }
666 }
667
668 static void stm32_sai_init_iec958_status(struct stm32_sai_sub_data *sai)
669 {
670         unsigned char *cs = sai->iec958.status;
671
672         cs[0] = IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS_NONE;
673         cs[1] = IEC958_AES1_CON_GENERAL;
674         cs[2] = IEC958_AES2_CON_SOURCE_UNSPEC | IEC958_AES2_CON_CHANNEL_UNSPEC;
675         cs[3] = IEC958_AES3_CON_CLOCK_1000PPM | IEC958_AES3_CON_FS_NOTID;
676 }
677
678 static void stm32_sai_set_iec958_status(struct stm32_sai_sub_data *sai,
679                                         struct snd_pcm_runtime *runtime)
680 {
681         if (!runtime)
682                 return;
683
684         /* Force the sample rate according to runtime rate */
685         mutex_lock(&sai->ctrl_lock);
686         switch (runtime->rate) {
687         case 22050:
688                 sai->iec958.status[3] = IEC958_AES3_CON_FS_22050;
689                 break;
690         case 44100:
691                 sai->iec958.status[3] = IEC958_AES3_CON_FS_44100;
692                 break;
693         case 88200:
694                 sai->iec958.status[3] = IEC958_AES3_CON_FS_88200;
695                 break;
696         case 176400:
697                 sai->iec958.status[3] = IEC958_AES3_CON_FS_176400;
698                 break;
699         case 24000:
700                 sai->iec958.status[3] = IEC958_AES3_CON_FS_24000;
701                 break;
702         case 48000:
703                 sai->iec958.status[3] = IEC958_AES3_CON_FS_48000;
704                 break;
705         case 96000:
706                 sai->iec958.status[3] = IEC958_AES3_CON_FS_96000;
707                 break;
708         case 192000:
709                 sai->iec958.status[3] = IEC958_AES3_CON_FS_192000;
710                 break;
711         case 32000:
712                 sai->iec958.status[3] = IEC958_AES3_CON_FS_32000;
713                 break;
714         default:
715                 sai->iec958.status[3] = IEC958_AES3_CON_FS_NOTID;
716                 break;
717         }
718         mutex_unlock(&sai->ctrl_lock);
719 }
720
721 static int stm32_sai_configure_clock(struct snd_soc_dai *cpu_dai,
722                                      struct snd_pcm_hw_params *params)
723 {
724         struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
725         int cr1, mask, div = 0;
726         int sai_clk_rate, mclk_ratio, den, ret;
727         int version = sai->pdata->conf->version;
728         unsigned int rate = params_rate(params);
729
730         if (!sai->mclk_rate) {
731                 dev_err(cpu_dai->dev, "Mclk rate is null\n");
732                 return -EINVAL;
733         }
734
735         if (!(rate % 11025))
736                 clk_set_parent(sai->sai_ck, sai->pdata->clk_x11k);
737         else
738                 clk_set_parent(sai->sai_ck, sai->pdata->clk_x8k);
739         sai_clk_rate = clk_get_rate(sai->sai_ck);
740
741         if (STM_SAI_IS_F4(sai->pdata)) {
742                 /*
743                  * mclk_rate = 256 * fs
744                  * MCKDIV = 0 if sai_ck < 3/2 * mclk_rate
745                  * MCKDIV = sai_ck / (2 * mclk_rate) otherwise
746                  */
747                 if (2 * sai_clk_rate >= 3 * sai->mclk_rate)
748                         div = DIV_ROUND_CLOSEST(sai_clk_rate,
749                                                 2 * sai->mclk_rate);
750         } else {
751                 /*
752                  * TDM mode :
753                  *   mclk on
754                  *      MCKDIV = sai_ck / (ws x 256)    (NOMCK=0. OSR=0)
755                  *      MCKDIV = sai_ck / (ws x 512)    (NOMCK=0. OSR=1)
756                  *   mclk off
757                  *      MCKDIV = sai_ck / (frl x ws)    (NOMCK=1)
758                  * Note: NOMCK/NODIV correspond to same bit.
759                  */
760                 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
761                         div = DIV_ROUND_CLOSEST(sai_clk_rate,
762                                                 (params_rate(params) * 128));
763                 } else {
764                         if (sai->mclk_rate) {
765                                 mclk_ratio = sai->mclk_rate / rate;
766                                 if (mclk_ratio == 512) {
767                                         mask = SAI_XCR1_OSR;
768                                         cr1 = SAI_XCR1_OSR;
769                                 } else if (mclk_ratio != 256) {
770                                         dev_err(cpu_dai->dev,
771                                                 "Wrong mclk ratio %d\n",
772                                                 mclk_ratio);
773                                         return -EINVAL;
774                                 }
775                                 div = DIV_ROUND_CLOSEST(sai_clk_rate,
776                                                         sai->mclk_rate);
777                         } else {
778                                 /* mclk-fs not set, master clock not active */
779                                 den = sai->fs_length * params_rate(params);
780                                 div = DIV_ROUND_CLOSEST(sai_clk_rate, den);
781                         }
782                 }
783         }
784
785         if (div > SAI_XCR1_MCKDIV_MAX(version)) {
786                 dev_err(cpu_dai->dev, "Divider %d out of range\n", div);
787                 return -EINVAL;
788         }
789         dev_dbg(cpu_dai->dev, "SAI clock %d, divider %d\n", sai_clk_rate, div);
790
791         mask = SAI_XCR1_MCKDIV_MASK(SAI_XCR1_MCKDIV_WIDTH(version));
792         cr1 = SAI_XCR1_MCKDIV_SET(div);
793         ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, mask, cr1);
794         if (ret < 0) {
795                 dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
796                 return ret;
797         }
798
799         return 0;
800 }
801
802 static int stm32_sai_hw_params(struct snd_pcm_substream *substream,
803                                struct snd_pcm_hw_params *params,
804                                struct snd_soc_dai *cpu_dai)
805 {
806         struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
807         int ret;
808
809         sai->data_size = params_width(params);
810
811         if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
812                 /* Rate not already set in runtime structure */
813                 substream->runtime->rate = params_rate(params);
814                 stm32_sai_set_iec958_status(sai, substream->runtime);
815         } else {
816                 ret = stm32_sai_set_slots(cpu_dai);
817                 if (ret < 0)
818                         return ret;
819                 stm32_sai_set_frame(cpu_dai);
820         }
821
822         ret = stm32_sai_set_config(cpu_dai, substream, params);
823         if (ret)
824                 return ret;
825
826         if (sai->master)
827                 ret = stm32_sai_configure_clock(cpu_dai, params);
828
829         return ret;
830 }
831
832 static int stm32_sai_trigger(struct snd_pcm_substream *substream, int cmd,
833                              struct snd_soc_dai *cpu_dai)
834 {
835         struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
836         int ret;
837
838         switch (cmd) {
839         case SNDRV_PCM_TRIGGER_START:
840         case SNDRV_PCM_TRIGGER_RESUME:
841         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
842                 dev_dbg(cpu_dai->dev, "Enable DMA and SAI\n");
843
844                 regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
845                                    SAI_XCR1_DMAEN, SAI_XCR1_DMAEN);
846
847                 /* Enable SAI */
848                 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
849                                          SAI_XCR1_SAIEN, SAI_XCR1_SAIEN);
850                 if (ret < 0)
851                         dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
852                 break;
853         case SNDRV_PCM_TRIGGER_SUSPEND:
854         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
855         case SNDRV_PCM_TRIGGER_STOP:
856                 dev_dbg(cpu_dai->dev, "Disable DMA and SAI\n");
857
858                 regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX,
859                                    SAI_XIMR_MASK, 0);
860
861                 regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
862                                    SAI_XCR1_SAIEN,
863                                    (unsigned int)~SAI_XCR1_SAIEN);
864
865                 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
866                                          SAI_XCR1_DMAEN,
867                                          (unsigned int)~SAI_XCR1_DMAEN);
868                 if (ret < 0)
869                         dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
870
871                 if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
872                         sai->spdif_frm_cnt = 0;
873                 break;
874         default:
875                 return -EINVAL;
876         }
877
878         return ret;
879 }
880
881 static void stm32_sai_shutdown(struct snd_pcm_substream *substream,
882                                struct snd_soc_dai *cpu_dai)
883 {
884         struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
885
886         regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, SAI_XIMR_MASK, 0);
887
888         regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, SAI_XCR1_NODIV,
889                            SAI_XCR1_NODIV);
890
891         clk_disable_unprepare(sai->sai_ck);
892         sai->substream = NULL;
893 }
894
895 static int stm32_sai_pcm_new(struct snd_soc_pcm_runtime *rtd,
896                              struct snd_soc_dai *cpu_dai)
897 {
898         struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
899         struct snd_kcontrol_new knew = iec958_ctls;
900
901         if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
902                 dev_dbg(&sai->pdev->dev, "%s: register iec controls", __func__);
903                 knew.device = rtd->pcm->device;
904                 return snd_ctl_add(rtd->pcm->card, snd_ctl_new1(&knew, sai));
905         }
906
907         return 0;
908 }
909
910 static int stm32_sai_dai_probe(struct snd_soc_dai *cpu_dai)
911 {
912         struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
913         int cr1 = 0, cr1_mask;
914
915         sai->dma_params.addr = (dma_addr_t)(sai->phys_addr + STM_SAI_DR_REGX);
916         /*
917          * DMA supports 4, 8 or 16 burst sizes. Burst size 4 is the best choice,
918          * as it allows bytes, half-word and words transfers. (See DMA fifos
919          * constraints).
920          */
921         sai->dma_params.maxburst = 4;
922         /* Buswidth will be set by framework at runtime */
923         sai->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
924
925         if (STM_SAI_IS_PLAYBACK(sai))
926                 snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params, NULL);
927         else
928                 snd_soc_dai_init_dma_data(cpu_dai, NULL, &sai->dma_params);
929
930         /* Next settings are not relevant for spdif mode */
931         if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
932                 return 0;
933
934         cr1_mask = SAI_XCR1_RX_TX;
935         if (STM_SAI_IS_CAPTURE(sai))
936                 cr1 |= SAI_XCR1_RX_TX;
937
938         /* Configure synchronization */
939         if (sai->sync == SAI_SYNC_EXTERNAL) {
940                 /* Configure synchro client and provider */
941                 sai->pdata->set_sync(sai->pdata, sai->np_sync_provider,
942                                      sai->synco, sai->synci);
943         }
944
945         cr1_mask |= SAI_XCR1_SYNCEN_MASK;
946         cr1 |= SAI_XCR1_SYNCEN_SET(sai->sync);
947
948         return regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1);
949 }
950
951 static const struct snd_soc_dai_ops stm32_sai_pcm_dai_ops = {
952         .set_sysclk     = stm32_sai_set_sysclk,
953         .set_fmt        = stm32_sai_set_dai_fmt,
954         .set_tdm_slot   = stm32_sai_set_dai_tdm_slot,
955         .startup        = stm32_sai_startup,
956         .hw_params      = stm32_sai_hw_params,
957         .trigger        = stm32_sai_trigger,
958         .shutdown       = stm32_sai_shutdown,
959 };
960
961 static int stm32_sai_pcm_process_spdif(struct snd_pcm_substream *substream,
962                                        int channel, unsigned long hwoff,
963                                        void *buf, unsigned long bytes)
964 {
965         struct snd_pcm_runtime *runtime = substream->runtime;
966         struct snd_soc_pcm_runtime *rtd = substream->private_data;
967         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
968         struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
969         int *ptr = (int *)(runtime->dma_area + hwoff +
970                            channel * (runtime->dma_bytes / runtime->channels));
971         ssize_t cnt = bytes_to_samples(runtime, bytes);
972         unsigned int frm_cnt = sai->spdif_frm_cnt;
973         unsigned int byte;
974         unsigned int mask;
975
976         do {
977                 *ptr = ((*ptr >> 8) & 0x00ffffff);
978
979                 /* Set channel status bit */
980                 byte = frm_cnt >> 3;
981                 mask = 1 << (frm_cnt - (byte << 3));
982                 if (sai->iec958.status[byte] & mask)
983                         *ptr |= 0x04000000;
984                 ptr++;
985
986                 if (!(cnt % 2))
987                         frm_cnt++;
988
989                 if (frm_cnt == SAI_IEC60958_BLOCK_FRAMES)
990                         frm_cnt = 0;
991         } while (--cnt);
992         sai->spdif_frm_cnt = frm_cnt;
993
994         return 0;
995 }
996
997 /* No support of mmap in S/PDIF mode */
998 static const struct snd_pcm_hardware stm32_sai_pcm_hw_spdif = {
999         .info = SNDRV_PCM_INFO_INTERLEAVED,
1000         .buffer_bytes_max = 8 * PAGE_SIZE,
1001         .period_bytes_min = 1024,
1002         .period_bytes_max = PAGE_SIZE,
1003         .periods_min = 2,
1004         .periods_max = 8,
1005 };
1006
1007 static const struct snd_pcm_hardware stm32_sai_pcm_hw = {
1008         .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP,
1009         .buffer_bytes_max = 8 * PAGE_SIZE,
1010         .period_bytes_min = 1024, /* 5ms at 48kHz */
1011         .period_bytes_max = PAGE_SIZE,
1012         .periods_min = 2,
1013         .periods_max = 8,
1014 };
1015
1016 static struct snd_soc_dai_driver stm32_sai_playback_dai[] = {
1017 {
1018                 .probe = stm32_sai_dai_probe,
1019                 .pcm_new = stm32_sai_pcm_new,
1020                 .id = 1, /* avoid call to fmt_single_name() */
1021                 .playback = {
1022                         .channels_min = 1,
1023                         .channels_max = 2,
1024                         .rate_min = 8000,
1025                         .rate_max = 192000,
1026                         .rates = SNDRV_PCM_RATE_CONTINUOUS,
1027                         /* DMA does not support 24 bits transfers */
1028                         .formats =
1029                                 SNDRV_PCM_FMTBIT_S8 |
1030                                 SNDRV_PCM_FMTBIT_S16_LE |
1031                                 SNDRV_PCM_FMTBIT_S32_LE,
1032                 },
1033                 .ops = &stm32_sai_pcm_dai_ops,
1034         }
1035 };
1036
1037 static struct snd_soc_dai_driver stm32_sai_capture_dai[] = {
1038 {
1039                 .probe = stm32_sai_dai_probe,
1040                 .id = 1, /* avoid call to fmt_single_name() */
1041                 .capture = {
1042                         .channels_min = 1,
1043                         .channels_max = 2,
1044                         .rate_min = 8000,
1045                         .rate_max = 192000,
1046                         .rates = SNDRV_PCM_RATE_CONTINUOUS,
1047                         /* DMA does not support 24 bits transfers */
1048                         .formats =
1049                                 SNDRV_PCM_FMTBIT_S8 |
1050                                 SNDRV_PCM_FMTBIT_S16_LE |
1051                                 SNDRV_PCM_FMTBIT_S32_LE,
1052                 },
1053                 .ops = &stm32_sai_pcm_dai_ops,
1054         }
1055 };
1056
1057 static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config = {
1058         .pcm_hardware = &stm32_sai_pcm_hw,
1059         .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
1060 };
1061
1062 static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config_spdif = {
1063         .pcm_hardware = &stm32_sai_pcm_hw_spdif,
1064         .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
1065         .process = stm32_sai_pcm_process_spdif,
1066 };
1067
1068 static const struct snd_soc_component_driver stm32_component = {
1069         .name = "stm32-sai",
1070 };
1071
1072 static const struct of_device_id stm32_sai_sub_ids[] = {
1073         { .compatible = "st,stm32-sai-sub-a",
1074           .data = (void *)STM_SAI_A_ID},
1075         { .compatible = "st,stm32-sai-sub-b",
1076           .data = (void *)STM_SAI_B_ID},
1077         {}
1078 };
1079 MODULE_DEVICE_TABLE(of, stm32_sai_sub_ids);
1080
1081 static int stm32_sai_sub_parse_of(struct platform_device *pdev,
1082                                   struct stm32_sai_sub_data *sai)
1083 {
1084         struct device_node *np = pdev->dev.of_node;
1085         struct resource *res;
1086         void __iomem *base;
1087         struct of_phandle_args args;
1088         int ret;
1089
1090         if (!np)
1091                 return -ENODEV;
1092
1093         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1094         base = devm_ioremap_resource(&pdev->dev, res);
1095         if (IS_ERR(base))
1096                 return PTR_ERR(base);
1097
1098         sai->phys_addr = res->start;
1099
1100         sai->regmap_config = &stm32_sai_sub_regmap_config_f4;
1101         /* Note: PDM registers not available for H7 sub-block B */
1102         if (STM_SAI_IS_H7(sai->pdata) && STM_SAI_IS_SUB_A(sai))
1103                 sai->regmap_config = &stm32_sai_sub_regmap_config_h7;
1104
1105         sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "sai_ck",
1106                                                 base, sai->regmap_config);
1107         if (IS_ERR(sai->regmap)) {
1108                 dev_err(&pdev->dev, "Failed to initialize MMIO\n");
1109                 return PTR_ERR(sai->regmap);
1110         }
1111
1112         /* Get direction property */
1113         if (of_property_match_string(np, "dma-names", "tx") >= 0) {
1114                 sai->dir = SNDRV_PCM_STREAM_PLAYBACK;
1115         } else if (of_property_match_string(np, "dma-names", "rx") >= 0) {
1116                 sai->dir = SNDRV_PCM_STREAM_CAPTURE;
1117         } else {
1118                 dev_err(&pdev->dev, "Unsupported direction\n");
1119                 return -EINVAL;
1120         }
1121
1122         /* Get spdif iec60958 property */
1123         sai->spdif = false;
1124         if (of_get_property(np, "st,iec60958", NULL)) {
1125                 if (!STM_SAI_HAS_SPDIF(sai) ||
1126                     sai->dir == SNDRV_PCM_STREAM_CAPTURE) {
1127                         dev_err(&pdev->dev, "S/PDIF IEC60958 not supported\n");
1128                         return -EINVAL;
1129                 }
1130                 stm32_sai_init_iec958_status(sai);
1131                 sai->spdif = true;
1132                 sai->master = true;
1133         }
1134
1135         /* Get synchronization property */
1136         args.np = NULL;
1137         ret = of_parse_phandle_with_fixed_args(np, "st,sync", 1, 0, &args);
1138         if (ret < 0  && ret != -ENOENT) {
1139                 dev_err(&pdev->dev, "Failed to get st,sync property\n");
1140                 return ret;
1141         }
1142
1143         sai->sync = SAI_SYNC_NONE;
1144         if (args.np) {
1145                 if (args.np == np) {
1146                         dev_err(&pdev->dev, "%s sync own reference\n",
1147                                 np->name);
1148                         of_node_put(args.np);
1149                         return -EINVAL;
1150                 }
1151
1152                 sai->np_sync_provider  = of_get_parent(args.np);
1153                 if (!sai->np_sync_provider) {
1154                         dev_err(&pdev->dev, "%s parent node not found\n",
1155                                 np->name);
1156                         of_node_put(args.np);
1157                         return -ENODEV;
1158                 }
1159
1160                 sai->sync = SAI_SYNC_INTERNAL;
1161                 if (sai->np_sync_provider != sai->pdata->pdev->dev.of_node) {
1162                         if (!STM_SAI_HAS_EXT_SYNC(sai)) {
1163                                 dev_err(&pdev->dev,
1164                                         "External synchro not supported\n");
1165                                 of_node_put(args.np);
1166                                 return -EINVAL;
1167                         }
1168                         sai->sync = SAI_SYNC_EXTERNAL;
1169
1170                         sai->synci = args.args[0];
1171                         if (sai->synci < 1 ||
1172                             (sai->synci > (SAI_GCR_SYNCIN_MAX + 1))) {
1173                                 dev_err(&pdev->dev, "Wrong SAI index\n");
1174                                 of_node_put(args.np);
1175                                 return -EINVAL;
1176                         }
1177
1178                         if (of_property_match_string(args.np, "compatible",
1179                                                      "st,stm32-sai-sub-a") >= 0)
1180                                 sai->synco = STM_SAI_SYNC_OUT_A;
1181
1182                         if (of_property_match_string(args.np, "compatible",
1183                                                      "st,stm32-sai-sub-b") >= 0)
1184                                 sai->synco = STM_SAI_SYNC_OUT_B;
1185
1186                         if (!sai->synco) {
1187                                 dev_err(&pdev->dev, "Unknown SAI sub-block\n");
1188                                 of_node_put(args.np);
1189                                 return -EINVAL;
1190                         }
1191                 }
1192
1193                 dev_dbg(&pdev->dev, "%s synchronized with %s\n",
1194                         pdev->name, args.np->full_name);
1195         }
1196
1197         of_node_put(args.np);
1198         sai->sai_ck = devm_clk_get(&pdev->dev, "sai_ck");
1199         if (IS_ERR(sai->sai_ck)) {
1200                 dev_err(&pdev->dev, "Missing kernel clock sai_ck\n");
1201                 return PTR_ERR(sai->sai_ck);
1202         }
1203
1204         return 0;
1205 }
1206
1207 static int stm32_sai_sub_dais_init(struct platform_device *pdev,
1208                                    struct stm32_sai_sub_data *sai)
1209 {
1210         sai->cpu_dai_drv = devm_kzalloc(&pdev->dev,
1211                                         sizeof(struct snd_soc_dai_driver),
1212                                         GFP_KERNEL);
1213         if (!sai->cpu_dai_drv)
1214                 return -ENOMEM;
1215
1216         if (STM_SAI_IS_PLAYBACK(sai)) {
1217                 memcpy(sai->cpu_dai_drv, &stm32_sai_playback_dai,
1218                        sizeof(stm32_sai_playback_dai));
1219                 sai->cpu_dai_drv->playback.stream_name = sai->cpu_dai_drv->name;
1220         } else {
1221                 memcpy(sai->cpu_dai_drv, &stm32_sai_capture_dai,
1222                        sizeof(stm32_sai_capture_dai));
1223                 sai->cpu_dai_drv->capture.stream_name = sai->cpu_dai_drv->name;
1224         }
1225         sai->cpu_dai_drv->name = dev_name(&pdev->dev);
1226
1227         return 0;
1228 }
1229
1230 static int stm32_sai_sub_probe(struct platform_device *pdev)
1231 {
1232         struct stm32_sai_sub_data *sai;
1233         const struct of_device_id *of_id;
1234         const struct snd_dmaengine_pcm_config *conf = &stm32_sai_pcm_config;
1235         int ret;
1236
1237         sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
1238         if (!sai)
1239                 return -ENOMEM;
1240
1241         of_id = of_match_device(stm32_sai_sub_ids, &pdev->dev);
1242         if (!of_id)
1243                 return -EINVAL;
1244         sai->id = (uintptr_t)of_id->data;
1245
1246         sai->pdev = pdev;
1247         mutex_init(&sai->ctrl_lock);
1248         platform_set_drvdata(pdev, sai);
1249
1250         sai->pdata = dev_get_drvdata(pdev->dev.parent);
1251         if (!sai->pdata) {
1252                 dev_err(&pdev->dev, "Parent device data not available\n");
1253                 return -EINVAL;
1254         }
1255
1256         ret = stm32_sai_sub_parse_of(pdev, sai);
1257         if (ret)
1258                 return ret;
1259
1260         ret = stm32_sai_sub_dais_init(pdev, sai);
1261         if (ret)
1262                 return ret;
1263
1264         ret = devm_request_irq(&pdev->dev, sai->pdata->irq, stm32_sai_isr,
1265                                IRQF_SHARED, dev_name(&pdev->dev), sai);
1266         if (ret) {
1267                 dev_err(&pdev->dev, "IRQ request returned %d\n", ret);
1268                 return ret;
1269         }
1270
1271         ret = devm_snd_soc_register_component(&pdev->dev, &stm32_component,
1272                                               sai->cpu_dai_drv, 1);
1273         if (ret)
1274                 return ret;
1275
1276         if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
1277                 conf = &stm32_sai_pcm_config_spdif;
1278
1279         ret = devm_snd_dmaengine_pcm_register(&pdev->dev, conf, 0);
1280         if (ret) {
1281                 dev_err(&pdev->dev, "Could not register pcm dma\n");
1282                 return ret;
1283         }
1284
1285         return 0;
1286 }
1287
1288 static struct platform_driver stm32_sai_sub_driver = {
1289         .driver = {
1290                 .name = "st,stm32-sai-sub",
1291                 .of_match_table = stm32_sai_sub_ids,
1292         },
1293         .probe = stm32_sai_sub_probe,
1294 };
1295
1296 module_platform_driver(stm32_sai_sub_driver);
1297
1298 MODULE_DESCRIPTION("STM32 Soc SAI sub-block Interface");
1299 MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>");
1300 MODULE_ALIAS("platform:st,stm32-sai-sub");
1301 MODULE_LICENSE("GPL v2");