blob: cad415e03b5efb1f338df237b8b0ddfc53ca85b6 [file] [log] [blame]
olivier moysan3e086ed2017-04-10 17:19:56 +02001/*
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>
Olivier Moysan8307b2a2018-10-15 16:03:35 +020020#include <linux/clk-provider.h>
olivier moysan3e086ed2017-04-10 17:19:56 +020021#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>
26
Olivier Moysan6eb17d72018-02-19 16:00:37 +010027#include <sound/asoundef.h>
olivier moysan3e086ed2017-04-10 17:19:56 +020028#include <sound/core.h>
29#include <sound/dmaengine_pcm.h>
30#include <sound/pcm_params.h>
31
32#include "stm32_sai.h"
33
34#define SAI_FREE_PROTOCOL 0x0
Olivier Moysan6eb17d72018-02-19 16:00:37 +010035#define SAI_SPDIF_PROTOCOL 0x1
olivier moysan3e086ed2017-04-10 17:19:56 +020036
37#define SAI_SLOT_SIZE_AUTO 0x0
38#define SAI_SLOT_SIZE_16 0x1
39#define SAI_SLOT_SIZE_32 0x2
40
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
47
48#define STM_SAI_FIFO_SIZE 8
49#define STM_SAI_DAI_NAME_SIZE 15
50
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)
53
54#define STM_SAI_A_ID 0x0
55#define STM_SAI_B_ID 0x1
56
olivier moysan03e78a22017-06-16 14:16:24 +020057#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)
olivier moysan3e086ed2017-04-10 17:19:56 +020059#define STM_SAI_BLOCK_NAME(x) (((x)->id == STM_SAI_A_ID) ? "A" : "B")
60
Olivier Moysan5914d282017-10-19 15:03:23 +020061#define SAI_SYNC_NONE 0x0
62#define SAI_SYNC_INTERNAL 0x1
63#define SAI_SYNC_EXTERNAL 0x2
64
Olivier Moysan6eb17d72018-02-19 16:00:37 +010065#define STM_SAI_PROTOCOL_IS_SPDIF(ip) ((ip)->spdif)
66#define STM_SAI_HAS_SPDIF(x) ((x)->pdata->conf->has_spdif)
Olivier Moysan5914d282017-10-19 15:03:23 +020067#define STM_SAI_HAS_EXT_SYNC(x) (!STM_SAI_IS_F4(sai->pdata))
68
Olivier Moysan6eb17d72018-02-19 16:00:37 +010069#define SAI_IEC60958_BLOCK_FRAMES 192
70#define SAI_IEC60958_STATUS_BYTES 24
71
Olivier Moysan8307b2a2018-10-15 16:03:35 +020072#define SAI_MCLK_NAME_LEN 32
73
olivier moysan3e086ed2017-04-10 17:19:56 +020074/**
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
olivier moysan03e78a22017-06-16 14:16:24 +020078 * @regmap_config: SAI sub block register map configuration pointer
olivier moysan3e086ed2017-04-10 17:19:56 +020079 * @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
Olivier Moysan5914d282017-10-19 15:03:23 +020084 * @np_sync_provider: synchronization provider node
olivier moysan3e086ed2017-04-10 17:19:56 +020085 * @sai_ck: kernel clock feeding the SAI clock generator
Olivier Moysan8307b2a2018-10-15 16:03:35 +020086 * @sai_mclk: master clock from SAI mclk provider
olivier moysan3e086ed2017-04-10 17:19:56 +020087 * @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
Olivier Moysan6eb17d72018-02-19 16:00:37 +010092 * @spdif: SAI S/PDIF iec60958 mode flag. set at init
olivier moysan3e086ed2017-04-10 17:19:56 +020093 * @fmt: SAI block format. relevant only for custom protocols. set at init
94 * @sync: SAI block synchronization mode. (none, internal or external)
Olivier Moysan5914d282017-10-19 15:03:23 +020095 * @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)
olivier moysan3e086ed2017-04-10 17:19:56 +020097 * @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.
Olivier Moysan6eb17d72018-02-19 16:00:37 +0100102 * @spdif_frm_cnt: S/PDIF playback frame counter
Olivier Moysan5f8a1002019-02-28 14:19:21 +0100103 * @iec958: iec958 data
olivier moysan187e01d2018-06-11 17:13:59 +0200104 * @ctrl_lock: control lock
Olivier Moysan26f98e82d2019-02-28 14:19:23 +0100105 * @irq_lock: prevent race condition with IRQ
olivier moysan3e086ed2017-04-10 17:19:56 +0200106 */
107struct stm32_sai_sub_data {
108 struct platform_device *pdev;
109 struct regmap *regmap;
olivier moysan03e78a22017-06-16 14:16:24 +0200110 const struct regmap_config *regmap_config;
olivier moysan3e086ed2017-04-10 17:19:56 +0200111 struct snd_dmaengine_dai_dma_data dma_params;
112 struct snd_soc_dai_driver *cpu_dai_drv;
113 struct snd_soc_dai *cpu_dai;
114 struct snd_pcm_substream *substream;
115 struct stm32_sai_data *pdata;
Olivier Moysan5914d282017-10-19 15:03:23 +0200116 struct device_node *np_sync_provider;
olivier moysan3e086ed2017-04-10 17:19:56 +0200117 struct clk *sai_ck;
Olivier Moysan8307b2a2018-10-15 16:03:35 +0200118 struct clk *sai_mclk;
olivier moysan3e086ed2017-04-10 17:19:56 +0200119 dma_addr_t phys_addr;
120 unsigned int mclk_rate;
121 unsigned int id;
122 int dir;
123 bool master;
Olivier Moysan6eb17d72018-02-19 16:00:37 +0100124 bool spdif;
olivier moysan3e086ed2017-04-10 17:19:56 +0200125 int fmt;
126 int sync;
Olivier Moysan5914d282017-10-19 15:03:23 +0200127 int synco;
128 int synci;
olivier moysan3e086ed2017-04-10 17:19:56 +0200129 int fs_length;
130 int slots;
131 int slot_width;
132 int slot_mask;
133 int data_size;
Olivier Moysan6eb17d72018-02-19 16:00:37 +0100134 unsigned int spdif_frm_cnt;
olivier moysan187e01d2018-06-11 17:13:59 +0200135 struct snd_aes_iec958 iec958;
136 struct mutex ctrl_lock; /* protect resources accessed by controls */
Olivier Moysan26f98e82d2019-02-28 14:19:23 +0100137 spinlock_t irq_lock; /* used to prevent race condition with IRQ */
olivier moysan3e086ed2017-04-10 17:19:56 +0200138};
139
140enum stm32_sai_fifo_th {
141 STM_SAI_FIFO_TH_EMPTY,
142 STM_SAI_FIFO_TH_QUARTER,
143 STM_SAI_FIFO_TH_HALF,
144 STM_SAI_FIFO_TH_3_QUARTER,
145 STM_SAI_FIFO_TH_FULL,
146};
147
148static bool stm32_sai_sub_readable_reg(struct device *dev, unsigned int reg)
149{
150 switch (reg) {
151 case STM_SAI_CR1_REGX:
152 case STM_SAI_CR2_REGX:
153 case STM_SAI_FRCR_REGX:
154 case STM_SAI_SLOTR_REGX:
155 case STM_SAI_IMR_REGX:
156 case STM_SAI_SR_REGX:
157 case STM_SAI_CLRFR_REGX:
158 case STM_SAI_DR_REGX:
olivier moysan03e78a22017-06-16 14:16:24 +0200159 case STM_SAI_PDMCR_REGX:
160 case STM_SAI_PDMLY_REGX:
olivier moysan3e086ed2017-04-10 17:19:56 +0200161 return true;
162 default:
163 return false;
164 }
165}
166
167static bool stm32_sai_sub_volatile_reg(struct device *dev, unsigned int reg)
168{
169 switch (reg) {
170 case STM_SAI_DR_REGX:
171 return true;
172 default:
173 return false;
174 }
175}
176
177static bool stm32_sai_sub_writeable_reg(struct device *dev, unsigned int reg)
178{
179 switch (reg) {
180 case STM_SAI_CR1_REGX:
181 case STM_SAI_CR2_REGX:
182 case STM_SAI_FRCR_REGX:
183 case STM_SAI_SLOTR_REGX:
184 case STM_SAI_IMR_REGX:
185 case STM_SAI_SR_REGX:
186 case STM_SAI_CLRFR_REGX:
187 case STM_SAI_DR_REGX:
olivier moysan03e78a22017-06-16 14:16:24 +0200188 case STM_SAI_PDMCR_REGX:
189 case STM_SAI_PDMLY_REGX:
olivier moysan3e086ed2017-04-10 17:19:56 +0200190 return true;
191 default:
192 return false;
193 }
194}
195
olivier moysan03e78a22017-06-16 14:16:24 +0200196static const struct regmap_config stm32_sai_sub_regmap_config_f4 = {
olivier moysan3e086ed2017-04-10 17:19:56 +0200197 .reg_bits = 32,
198 .reg_stride = 4,
199 .val_bits = 32,
200 .max_register = STM_SAI_DR_REGX,
201 .readable_reg = stm32_sai_sub_readable_reg,
202 .volatile_reg = stm32_sai_sub_volatile_reg,
203 .writeable_reg = stm32_sai_sub_writeable_reg,
204 .fast_io = true,
205};
206
olivier moysan03e78a22017-06-16 14:16:24 +0200207static const struct regmap_config stm32_sai_sub_regmap_config_h7 = {
208 .reg_bits = 32,
209 .reg_stride = 4,
210 .val_bits = 32,
211 .max_register = STM_SAI_PDMLY_REGX,
212 .readable_reg = stm32_sai_sub_readable_reg,
213 .volatile_reg = stm32_sai_sub_volatile_reg,
214 .writeable_reg = stm32_sai_sub_writeable_reg,
215 .fast_io = true,
216};
217
olivier moysan187e01d2018-06-11 17:13:59 +0200218static int snd_pcm_iec958_info(struct snd_kcontrol *kcontrol,
219 struct snd_ctl_elem_info *uinfo)
220{
221 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
222 uinfo->count = 1;
223
224 return 0;
225}
226
227static int snd_pcm_iec958_get(struct snd_kcontrol *kcontrol,
228 struct snd_ctl_elem_value *uctl)
229{
230 struct stm32_sai_sub_data *sai = snd_kcontrol_chip(kcontrol);
231
232 mutex_lock(&sai->ctrl_lock);
233 memcpy(uctl->value.iec958.status, sai->iec958.status, 4);
234 mutex_unlock(&sai->ctrl_lock);
235
236 return 0;
237}
238
239static int snd_pcm_iec958_put(struct snd_kcontrol *kcontrol,
240 struct snd_ctl_elem_value *uctl)
241{
242 struct stm32_sai_sub_data *sai = snd_kcontrol_chip(kcontrol);
243
244 mutex_lock(&sai->ctrl_lock);
245 memcpy(sai->iec958.status, uctl->value.iec958.status, 4);
246 mutex_unlock(&sai->ctrl_lock);
247
248 return 0;
249}
250
251static const struct snd_kcontrol_new iec958_ctls = {
252 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
253 SNDRV_CTL_ELEM_ACCESS_VOLATILE),
254 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
255 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
256 .info = snd_pcm_iec958_info,
257 .get = snd_pcm_iec958_get,
258 .put = snd_pcm_iec958_put,
259};
260
Olivier Moysan8307b2a2018-10-15 16:03:35 +0200261struct stm32_sai_mclk_data {
262 struct clk_hw hw;
263 unsigned long freq;
264 struct stm32_sai_sub_data *sai_data;
265};
266
267#define to_mclk_data(_hw) container_of(_hw, struct stm32_sai_mclk_data, hw)
268#define STM32_SAI_MAX_CLKS 1
269
270static int stm32_sai_get_clk_div(struct stm32_sai_sub_data *sai,
271 unsigned long input_rate,
272 unsigned long output_rate)
273{
274 int version = sai->pdata->conf->version;
275 int div;
276
277 div = DIV_ROUND_CLOSEST(input_rate, output_rate);
278 if (div > SAI_XCR1_MCKDIV_MAX(version)) {
279 dev_err(&sai->pdev->dev, "Divider %d out of range\n", div);
280 return -EINVAL;
281 }
282 dev_dbg(&sai->pdev->dev, "SAI divider %d\n", div);
283
284 if (input_rate % div)
285 dev_dbg(&sai->pdev->dev,
286 "Rate not accurate. requested (%ld), actual (%ld)\n",
287 output_rate, input_rate / div);
288
289 return div;
290}
291
292static int stm32_sai_set_clk_div(struct stm32_sai_sub_data *sai,
293 unsigned int div)
294{
295 int version = sai->pdata->conf->version;
296 int ret, cr1, mask;
297
298 if (div > SAI_XCR1_MCKDIV_MAX(version)) {
299 dev_err(&sai->pdev->dev, "Divider %d out of range\n", div);
300 return -EINVAL;
301 }
302
303 mask = SAI_XCR1_MCKDIV_MASK(SAI_XCR1_MCKDIV_WIDTH(version));
304 cr1 = SAI_XCR1_MCKDIV_SET(div);
305 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, mask, cr1);
306 if (ret < 0)
307 dev_err(&sai->pdev->dev, "Failed to update CR1 register\n");
308
309 return ret;
310}
311
312static long stm32_sai_mclk_round_rate(struct clk_hw *hw, unsigned long rate,
313 unsigned long *prate)
314{
315 struct stm32_sai_mclk_data *mclk = to_mclk_data(hw);
316 struct stm32_sai_sub_data *sai = mclk->sai_data;
317 int div;
318
319 div = stm32_sai_get_clk_div(sai, *prate, rate);
320 if (div < 0)
321 return div;
322
323 mclk->freq = *prate / div;
324
325 return mclk->freq;
326}
327
328static unsigned long stm32_sai_mclk_recalc_rate(struct clk_hw *hw,
329 unsigned long parent_rate)
330{
331 struct stm32_sai_mclk_data *mclk = to_mclk_data(hw);
332
333 return mclk->freq;
334}
335
336static int stm32_sai_mclk_set_rate(struct clk_hw *hw, unsigned long rate,
337 unsigned long parent_rate)
338{
339 struct stm32_sai_mclk_data *mclk = to_mclk_data(hw);
340 struct stm32_sai_sub_data *sai = mclk->sai_data;
Colin Ian King6b27e272018-10-31 19:31:43 +0000341 int div, ret;
Olivier Moysan8307b2a2018-10-15 16:03:35 +0200342
343 div = stm32_sai_get_clk_div(sai, parent_rate, rate);
344 if (div < 0)
345 return div;
346
347 ret = stm32_sai_set_clk_div(sai, div);
348 if (ret)
349 return ret;
350
351 mclk->freq = rate;
352
353 return 0;
354}
355
356static int stm32_sai_mclk_enable(struct clk_hw *hw)
357{
358 struct stm32_sai_mclk_data *mclk = to_mclk_data(hw);
359 struct stm32_sai_sub_data *sai = mclk->sai_data;
360
361 dev_dbg(&sai->pdev->dev, "Enable master clock\n");
362
363 return regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
364 SAI_XCR1_MCKEN, SAI_XCR1_MCKEN);
365}
366
367static void stm32_sai_mclk_disable(struct clk_hw *hw)
368{
369 struct stm32_sai_mclk_data *mclk = to_mclk_data(hw);
370 struct stm32_sai_sub_data *sai = mclk->sai_data;
371
372 dev_dbg(&sai->pdev->dev, "Disable master clock\n");
373
374 regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, SAI_XCR1_MCKEN, 0);
375}
376
377static const struct clk_ops mclk_ops = {
378 .enable = stm32_sai_mclk_enable,
379 .disable = stm32_sai_mclk_disable,
380 .recalc_rate = stm32_sai_mclk_recalc_rate,
381 .round_rate = stm32_sai_mclk_round_rate,
382 .set_rate = stm32_sai_mclk_set_rate,
383};
384
385static int stm32_sai_add_mclk_provider(struct stm32_sai_sub_data *sai)
386{
387 struct clk_hw *hw;
388 struct stm32_sai_mclk_data *mclk;
389 struct device *dev = &sai->pdev->dev;
390 const char *pname = __clk_get_name(sai->sai_ck);
391 char *mclk_name, *p, *s = (char *)pname;
392 int ret, i = 0;
393
Wei Yongjun496fa3b2018-10-27 02:19:59 +0000394 mclk = devm_kzalloc(dev, sizeof(*mclk), GFP_KERNEL);
Olivier Moysan8307b2a2018-10-15 16:03:35 +0200395 if (!mclk)
396 return -ENOMEM;
397
398 mclk_name = devm_kcalloc(dev, sizeof(char),
399 SAI_MCLK_NAME_LEN, GFP_KERNEL);
400 if (!mclk_name)
401 return -ENOMEM;
402
403 /*
404 * Forge mclk clock name from parent clock name and suffix.
405 * String after "_" char is stripped in parent name.
406 */
407 p = mclk_name;
Olivier Moysan6be0f962018-10-22 17:10:46 +0200408 while (*s && *s != '_' && (i < (SAI_MCLK_NAME_LEN - 7))) {
Olivier Moysan8307b2a2018-10-15 16:03:35 +0200409 *p++ = *s++;
410 i++;
411 }
Olivier Moysan6be0f962018-10-22 17:10:46 +0200412 STM_SAI_IS_SUB_A(sai) ? strcat(p, "a_mclk") : strcat(p, "b_mclk");
Olivier Moysan8307b2a2018-10-15 16:03:35 +0200413
414 mclk->hw.init = CLK_HW_INIT(mclk_name, pname, &mclk_ops, 0);
415 mclk->sai_data = sai;
416 hw = &mclk->hw;
417
418 dev_dbg(dev, "Register master clock %s\n", mclk_name);
419 ret = devm_clk_hw_register(&sai->pdev->dev, hw);
420 if (ret) {
421 dev_err(dev, "mclk register returned %d\n", ret);
422 return ret;
423 }
424 sai->sai_mclk = hw->clk;
425
426 /* register mclk provider */
427 return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, hw);
428}
429
olivier moysan3e086ed2017-04-10 17:19:56 +0200430static irqreturn_t stm32_sai_isr(int irq, void *devid)
431{
432 struct stm32_sai_sub_data *sai = (struct stm32_sai_sub_data *)devid;
olivier moysan3e086ed2017-04-10 17:19:56 +0200433 struct platform_device *pdev = sai->pdev;
434 unsigned int sr, imr, flags;
435 snd_pcm_state_t status = SNDRV_PCM_STATE_RUNNING;
436
437 regmap_read(sai->regmap, STM_SAI_IMR_REGX, &imr);
438 regmap_read(sai->regmap, STM_SAI_SR_REGX, &sr);
439
440 flags = sr & imr;
441 if (!flags)
442 return IRQ_NONE;
443
444 regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX, SAI_XCLRFR_MASK,
445 SAI_XCLRFR_MASK);
446
Olivier Moysand807cdf2017-10-19 15:03:20 +0200447 if (!sai->substream) {
448 dev_err(&pdev->dev, "Device stopped. Spurious IRQ 0x%x\n", sr);
449 return IRQ_NONE;
450 }
451
olivier moysan3e086ed2017-04-10 17:19:56 +0200452 if (flags & SAI_XIMR_OVRUDRIE) {
olivier moysan602fdad2017-06-16 14:15:30 +0200453 dev_err(&pdev->dev, "IRQ %s\n",
olivier moysan3e086ed2017-04-10 17:19:56 +0200454 STM_SAI_IS_PLAYBACK(sai) ? "underrun" : "overrun");
455 status = SNDRV_PCM_STATE_XRUN;
456 }
457
458 if (flags & SAI_XIMR_MUTEDETIE)
olivier moysan602fdad2017-06-16 14:15:30 +0200459 dev_dbg(&pdev->dev, "IRQ mute detected\n");
olivier moysan3e086ed2017-04-10 17:19:56 +0200460
461 if (flags & SAI_XIMR_WCKCFGIE) {
olivier moysan602fdad2017-06-16 14:15:30 +0200462 dev_err(&pdev->dev, "IRQ wrong clock configuration\n");
olivier moysan3e086ed2017-04-10 17:19:56 +0200463 status = SNDRV_PCM_STATE_DISCONNECTED;
464 }
465
466 if (flags & SAI_XIMR_CNRDYIE)
olivier moysan602fdad2017-06-16 14:15:30 +0200467 dev_err(&pdev->dev, "IRQ Codec not ready\n");
olivier moysan3e086ed2017-04-10 17:19:56 +0200468
469 if (flags & SAI_XIMR_AFSDETIE) {
olivier moysan602fdad2017-06-16 14:15:30 +0200470 dev_err(&pdev->dev, "IRQ Anticipated frame synchro\n");
olivier moysan3e086ed2017-04-10 17:19:56 +0200471 status = SNDRV_PCM_STATE_XRUN;
472 }
473
474 if (flags & SAI_XIMR_LFSDETIE) {
olivier moysan602fdad2017-06-16 14:15:30 +0200475 dev_err(&pdev->dev, "IRQ Late frame synchro\n");
olivier moysan3e086ed2017-04-10 17:19:56 +0200476 status = SNDRV_PCM_STATE_XRUN;
477 }
478
Olivier Moysan26f98e82d2019-02-28 14:19:23 +0100479 spin_lock(&sai->irq_lock);
480 if (status != SNDRV_PCM_STATE_RUNNING && sai->substream)
Takashi Iwaib1625fb2018-07-04 16:01:46 +0200481 snd_pcm_stop_xrun(sai->substream);
Olivier Moysan26f98e82d2019-02-28 14:19:23 +0100482 spin_unlock(&sai->irq_lock);
olivier moysan3e086ed2017-04-10 17:19:56 +0200483
484 return IRQ_HANDLED;
485}
486
487static int stm32_sai_set_sysclk(struct snd_soc_dai *cpu_dai,
488 int clk_id, unsigned int freq, int dir)
489{
490 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
olivier moysan701a6ec2017-06-16 14:15:34 +0200491 int ret;
olivier moysan3e086ed2017-04-10 17:19:56 +0200492
Olivier Moysan8307b2a2018-10-15 16:03:35 +0200493 if (dir == SND_SOC_CLOCK_OUT) {
olivier moysan701a6ec2017-06-16 14:15:34 +0200494 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
495 SAI_XCR1_NODIV,
496 (unsigned int)~SAI_XCR1_NODIV);
497 if (ret < 0)
498 return ret;
499
olivier moysan3e086ed2017-04-10 17:19:56 +0200500 dev_dbg(cpu_dai->dev, "SAI MCLK frequency is %uHz\n", freq);
Olivier Moysan8307b2a2018-10-15 16:03:35 +0200501 sai->mclk_rate = freq;
502
503 if (sai->sai_mclk) {
504 ret = clk_set_rate_exclusive(sai->sai_mclk,
505 sai->mclk_rate);
506 if (ret) {
507 dev_err(cpu_dai->dev,
508 "Could not set mclk rate\n");
509 return ret;
510 }
511 }
olivier moysan3e086ed2017-04-10 17:19:56 +0200512 }
513
514 return 0;
515}
516
517static int stm32_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
518 u32 rx_mask, int slots, int slot_width)
519{
520 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
521 int slotr, slotr_mask, slot_size;
522
Olivier Moysan6eb17d72018-02-19 16:00:37 +0100523 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
524 dev_warn(cpu_dai->dev, "Slot setting relevant only for TDM\n");
525 return 0;
526 }
527
olivier moysan602fdad2017-06-16 14:15:30 +0200528 dev_dbg(cpu_dai->dev, "Masks tx/rx:%#x/%#x, slots:%d, width:%d\n",
olivier moysan3e086ed2017-04-10 17:19:56 +0200529 tx_mask, rx_mask, slots, slot_width);
530
531 switch (slot_width) {
532 case 16:
533 slot_size = SAI_SLOT_SIZE_16;
534 break;
535 case 32:
536 slot_size = SAI_SLOT_SIZE_32;
537 break;
538 default:
539 slot_size = SAI_SLOT_SIZE_AUTO;
540 break;
541 }
542
543 slotr = SAI_XSLOTR_SLOTSZ_SET(slot_size) |
544 SAI_XSLOTR_NBSLOT_SET(slots - 1);
545 slotr_mask = SAI_XSLOTR_SLOTSZ_MASK | SAI_XSLOTR_NBSLOT_MASK;
546
547 /* tx/rx mask set in machine init, if slot number defined in DT */
548 if (STM_SAI_IS_PLAYBACK(sai)) {
549 sai->slot_mask = tx_mask;
550 slotr |= SAI_XSLOTR_SLOTEN_SET(tx_mask);
551 }
552
553 if (STM_SAI_IS_CAPTURE(sai)) {
554 sai->slot_mask = rx_mask;
555 slotr |= SAI_XSLOTR_SLOTEN_SET(rx_mask);
556 }
557
558 slotr_mask |= SAI_XSLOTR_SLOTEN_MASK;
559
560 regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX, slotr_mask, slotr);
561
562 sai->slot_width = slot_width;
563 sai->slots = slots;
564
565 return 0;
566}
567
568static int stm32_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
569{
570 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
Olivier Moysan61fb4ff2017-10-19 15:03:18 +0200571 int cr1, frcr = 0;
572 int cr1_mask, frcr_mask = 0;
olivier moysan3e086ed2017-04-10 17:19:56 +0200573 int ret;
574
575 dev_dbg(cpu_dai->dev, "fmt %x\n", fmt);
576
Olivier Moysan6eb17d72018-02-19 16:00:37 +0100577 /* Do not generate master by default */
578 cr1 = SAI_XCR1_NODIV;
579 cr1_mask = SAI_XCR1_NODIV;
580
581 cr1_mask |= SAI_XCR1_PRTCFG_MASK;
582 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
583 cr1 |= SAI_XCR1_PRTCFG_SET(SAI_SPDIF_PROTOCOL);
584 goto conf_update;
585 }
586
587 cr1 |= SAI_XCR1_PRTCFG_SET(SAI_FREE_PROTOCOL);
Olivier Moysan61fb4ff2017-10-19 15:03:18 +0200588
olivier moysan3e086ed2017-04-10 17:19:56 +0200589 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
590 /* SCK active high for all protocols */
591 case SND_SOC_DAIFMT_I2S:
592 cr1 |= SAI_XCR1_CKSTR;
593 frcr |= SAI_XFRCR_FSOFF | SAI_XFRCR_FSDEF;
594 break;
595 /* Left justified */
596 case SND_SOC_DAIFMT_MSB:
597 frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF;
598 break;
599 /* Right justified */
600 case SND_SOC_DAIFMT_LSB:
601 frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSDEF;
602 break;
603 case SND_SOC_DAIFMT_DSP_A:
604 frcr |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF;
605 break;
606 case SND_SOC_DAIFMT_DSP_B:
607 frcr |= SAI_XFRCR_FSPOL;
608 break;
609 default:
610 dev_err(cpu_dai->dev, "Unsupported protocol %#x\n",
611 fmt & SND_SOC_DAIFMT_FORMAT_MASK);
612 return -EINVAL;
613 }
614
Olivier Moysan61fb4ff2017-10-19 15:03:18 +0200615 cr1_mask |= SAI_XCR1_CKSTR;
olivier moysan3e086ed2017-04-10 17:19:56 +0200616 frcr_mask |= SAI_XFRCR_FSPOL | SAI_XFRCR_FSOFF |
617 SAI_XFRCR_FSDEF;
618
619 /* DAI clock strobing. Invert setting previously set */
620 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
621 case SND_SOC_DAIFMT_NB_NF:
622 break;
623 case SND_SOC_DAIFMT_IB_NF:
624 cr1 ^= SAI_XCR1_CKSTR;
625 break;
626 case SND_SOC_DAIFMT_NB_IF:
627 frcr ^= SAI_XFRCR_FSPOL;
628 break;
629 case SND_SOC_DAIFMT_IB_IF:
630 /* Invert fs & sck */
631 cr1 ^= SAI_XCR1_CKSTR;
632 frcr ^= SAI_XFRCR_FSPOL;
633 break;
634 default:
635 dev_err(cpu_dai->dev, "Unsupported strobing %#x\n",
636 fmt & SND_SOC_DAIFMT_INV_MASK);
637 return -EINVAL;
638 }
639 cr1_mask |= SAI_XCR1_CKSTR;
640 frcr_mask |= SAI_XFRCR_FSPOL;
641
642 regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr);
643
644 /* DAI clock master masks */
645 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
646 case SND_SOC_DAIFMT_CBM_CFM:
647 /* codec is master */
648 cr1 |= SAI_XCR1_SLAVE;
649 sai->master = false;
650 break;
651 case SND_SOC_DAIFMT_CBS_CFS:
652 sai->master = true;
653 break;
654 default:
655 dev_err(cpu_dai->dev, "Unsupported mode %#x\n",
656 fmt & SND_SOC_DAIFMT_MASTER_MASK);
657 return -EINVAL;
658 }
Olivier Moysan5914d282017-10-19 15:03:23 +0200659
660 /* Set slave mode if sub-block is synchronized with another SAI */
661 if (sai->sync) {
662 dev_dbg(cpu_dai->dev, "Synchronized SAI configured as slave\n");
663 cr1 |= SAI_XCR1_SLAVE;
664 sai->master = false;
665 }
666
olivier moysan3e086ed2017-04-10 17:19:56 +0200667 cr1_mask |= SAI_XCR1_SLAVE;
668
Olivier Moysan6eb17d72018-02-19 16:00:37 +0100669conf_update:
olivier moysan3e086ed2017-04-10 17:19:56 +0200670 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1);
671 if (ret < 0) {
672 dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
673 return ret;
674 }
675
676 sai->fmt = fmt;
677
678 return 0;
679}
680
681static int stm32_sai_startup(struct snd_pcm_substream *substream,
682 struct snd_soc_dai *cpu_dai)
683{
684 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
685 int imr, cr2, ret;
Olivier Moysan26f98e82d2019-02-28 14:19:23 +0100686 unsigned long flags;
olivier moysan3e086ed2017-04-10 17:19:56 +0200687
Olivier Moysan26f98e82d2019-02-28 14:19:23 +0100688 spin_lock_irqsave(&sai->irq_lock, flags);
olivier moysan3e086ed2017-04-10 17:19:56 +0200689 sai->substream = substream;
Olivier Moysan26f98e82d2019-02-28 14:19:23 +0100690 spin_unlock_irqrestore(&sai->irq_lock, flags);
olivier moysan3e086ed2017-04-10 17:19:56 +0200691
Olivier Moysanb84681922019-02-28 14:19:22 +0100692 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
693 snd_pcm_hw_constraint_mask64(substream->runtime,
694 SNDRV_PCM_HW_PARAM_FORMAT,
695 SNDRV_PCM_FMTBIT_S32_LE);
696 snd_pcm_hw_constraint_single(substream->runtime,
697 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
698 }
699
olivier moysan3e086ed2017-04-10 17:19:56 +0200700 ret = clk_prepare_enable(sai->sai_ck);
701 if (ret < 0) {
olivier moysan602fdad2017-06-16 14:15:30 +0200702 dev_err(cpu_dai->dev, "Failed to enable clock: %d\n", ret);
olivier moysan3e086ed2017-04-10 17:19:56 +0200703 return ret;
704 }
705
706 /* Enable ITs */
olivier moysan3e086ed2017-04-10 17:19:56 +0200707
708 regmap_update_bits(sai->regmap, STM_SAI_CLRFR_REGX,
709 SAI_XCLRFR_MASK, SAI_XCLRFR_MASK);
710
711 imr = SAI_XIMR_OVRUDRIE;
712 if (STM_SAI_IS_CAPTURE(sai)) {
713 regmap_read(sai->regmap, STM_SAI_CR2_REGX, &cr2);
714 if (cr2 & SAI_XCR2_MUTECNT_MASK)
715 imr |= SAI_XIMR_MUTEDETIE;
716 }
717
718 if (sai->master)
719 imr |= SAI_XIMR_WCKCFGIE;
720 else
721 imr |= SAI_XIMR_AFSDETIE | SAI_XIMR_LFSDETIE;
722
723 regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX,
724 SAI_XIMR_MASK, imr);
725
726 return 0;
727}
728
729static int stm32_sai_set_config(struct snd_soc_dai *cpu_dai,
730 struct snd_pcm_substream *substream,
731 struct snd_pcm_hw_params *params)
732{
733 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
734 int cr1, cr1_mask, ret;
olivier moysan3e086ed2017-04-10 17:19:56 +0200735
Olivier Moysana4529d22017-10-19 15:03:19 +0200736 /*
737 * DMA bursts increment is set to 4 words.
738 * SAI fifo threshold is set to half fifo, to keep enough space
739 * for DMA incoming bursts.
740 */
olivier moysan3e086ed2017-04-10 17:19:56 +0200741 regmap_update_bits(sai->regmap, STM_SAI_CR2_REGX,
742 SAI_XCR2_FFLUSH | SAI_XCR2_FTH_MASK,
Olivier Moysana4529d22017-10-19 15:03:19 +0200743 SAI_XCR2_FFLUSH |
744 SAI_XCR2_FTH_SET(STM_SAI_FIFO_TH_HALF));
olivier moysan3e086ed2017-04-10 17:19:56 +0200745
Olivier Moysan6eb17d72018-02-19 16:00:37 +0100746 /* DS bits in CR1 not set for SPDIF (size forced to 24 bits).*/
747 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
748 sai->spdif_frm_cnt = 0;
749 return 0;
750 }
751
olivier moysan3e086ed2017-04-10 17:19:56 +0200752 /* Mode, data format and channel config */
Olivier Moysan61fb4ff2017-10-19 15:03:18 +0200753 cr1_mask = SAI_XCR1_DS_MASK;
olivier moysan3e086ed2017-04-10 17:19:56 +0200754 switch (params_format(params)) {
755 case SNDRV_PCM_FORMAT_S8:
olivier moysan7e751e372017-11-02 11:20:09 +0100756 cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_8);
olivier moysan3e086ed2017-04-10 17:19:56 +0200757 break;
758 case SNDRV_PCM_FORMAT_S16_LE:
olivier moysan7e751e372017-11-02 11:20:09 +0100759 cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_16);
olivier moysan3e086ed2017-04-10 17:19:56 +0200760 break;
761 case SNDRV_PCM_FORMAT_S32_LE:
olivier moysan7e751e372017-11-02 11:20:09 +0100762 cr1 = SAI_XCR1_DS_SET(SAI_DATASIZE_32);
olivier moysan3e086ed2017-04-10 17:19:56 +0200763 break;
764 default:
765 dev_err(cpu_dai->dev, "Data format not supported");
766 return -EINVAL;
767 }
olivier moysan3e086ed2017-04-10 17:19:56 +0200768
769 cr1_mask |= SAI_XCR1_MONO;
770 if ((sai->slots == 2) && (params_channels(params) == 1))
771 cr1 |= SAI_XCR1_MONO;
772
773 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1);
774 if (ret < 0) {
775 dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
776 return ret;
777 }
778
olivier moysan3e086ed2017-04-10 17:19:56 +0200779 return 0;
780}
781
782static int stm32_sai_set_slots(struct snd_soc_dai *cpu_dai)
783{
784 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
785 int slotr, slot_sz;
786
787 regmap_read(sai->regmap, STM_SAI_SLOTR_REGX, &slotr);
788
789 /*
790 * If SLOTSZ is set to auto in SLOTR, align slot width on data size
791 * By default slot width = data size, if not forced from DT
792 */
793 slot_sz = slotr & SAI_XSLOTR_SLOTSZ_MASK;
794 if (slot_sz == SAI_XSLOTR_SLOTSZ_SET(SAI_SLOT_SIZE_AUTO))
795 sai->slot_width = sai->data_size;
796
797 if (sai->slot_width < sai->data_size) {
798 dev_err(cpu_dai->dev,
799 "Data size %d larger than slot width\n",
800 sai->data_size);
801 return -EINVAL;
802 }
803
804 /* Slot number is set to 2, if not specified in DT */
805 if (!sai->slots)
806 sai->slots = 2;
807
808 /* The number of slots in the audio frame is equal to NBSLOT[3:0] + 1*/
809 regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX,
810 SAI_XSLOTR_NBSLOT_MASK,
811 SAI_XSLOTR_NBSLOT_SET((sai->slots - 1)));
812
813 /* Set default slots mask if not already set from DT */
814 if (!(slotr & SAI_XSLOTR_SLOTEN_MASK)) {
815 sai->slot_mask = (1 << sai->slots) - 1;
816 regmap_update_bits(sai->regmap,
817 STM_SAI_SLOTR_REGX, SAI_XSLOTR_SLOTEN_MASK,
818 SAI_XSLOTR_SLOTEN_SET(sai->slot_mask));
819 }
820
olivier moysan602fdad2017-06-16 14:15:30 +0200821 dev_dbg(cpu_dai->dev, "Slots %d, slot width %d\n",
olivier moysan3e086ed2017-04-10 17:19:56 +0200822 sai->slots, sai->slot_width);
823
824 return 0;
825}
826
827static void stm32_sai_set_frame(struct snd_soc_dai *cpu_dai)
828{
829 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
830 int fs_active, offset, format;
831 int frcr, frcr_mask;
832
833 format = sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
834 sai->fs_length = sai->slot_width * sai->slots;
835
836 fs_active = sai->fs_length / 2;
837 if ((format == SND_SOC_DAIFMT_DSP_A) ||
838 (format == SND_SOC_DAIFMT_DSP_B))
839 fs_active = 1;
840
841 frcr = SAI_XFRCR_FRL_SET((sai->fs_length - 1));
842 frcr |= SAI_XFRCR_FSALL_SET((fs_active - 1));
843 frcr_mask = SAI_XFRCR_FRL_MASK | SAI_XFRCR_FSALL_MASK;
844
olivier moysan602fdad2017-06-16 14:15:30 +0200845 dev_dbg(cpu_dai->dev, "Frame length %d, frame active %d\n",
olivier moysan3e086ed2017-04-10 17:19:56 +0200846 sai->fs_length, fs_active);
847
848 regmap_update_bits(sai->regmap, STM_SAI_FRCR_REGX, frcr_mask, frcr);
849
850 if ((sai->fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_LSB) {
851 offset = sai->slot_width - sai->data_size;
852
853 regmap_update_bits(sai->regmap, STM_SAI_SLOTR_REGX,
854 SAI_XSLOTR_FBOFF_MASK,
855 SAI_XSLOTR_FBOFF_SET(offset));
856 }
857}
858
olivier moysan187e01d2018-06-11 17:13:59 +0200859static void stm32_sai_init_iec958_status(struct stm32_sai_sub_data *sai)
860{
861 unsigned char *cs = sai->iec958.status;
862
863 cs[0] = IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS_NONE;
864 cs[1] = IEC958_AES1_CON_GENERAL;
865 cs[2] = IEC958_AES2_CON_SOURCE_UNSPEC | IEC958_AES2_CON_CHANNEL_UNSPEC;
866 cs[3] = IEC958_AES3_CON_CLOCK_1000PPM | IEC958_AES3_CON_FS_NOTID;
867}
868
869static void stm32_sai_set_iec958_status(struct stm32_sai_sub_data *sai,
870 struct snd_pcm_runtime *runtime)
871{
872 if (!runtime)
873 return;
874
875 /* Force the sample rate according to runtime rate */
876 mutex_lock(&sai->ctrl_lock);
877 switch (runtime->rate) {
878 case 22050:
879 sai->iec958.status[3] = IEC958_AES3_CON_FS_22050;
880 break;
881 case 44100:
882 sai->iec958.status[3] = IEC958_AES3_CON_FS_44100;
883 break;
884 case 88200:
885 sai->iec958.status[3] = IEC958_AES3_CON_FS_88200;
886 break;
887 case 176400:
888 sai->iec958.status[3] = IEC958_AES3_CON_FS_176400;
889 break;
890 case 24000:
891 sai->iec958.status[3] = IEC958_AES3_CON_FS_24000;
892 break;
893 case 48000:
894 sai->iec958.status[3] = IEC958_AES3_CON_FS_48000;
895 break;
896 case 96000:
897 sai->iec958.status[3] = IEC958_AES3_CON_FS_96000;
898 break;
899 case 192000:
900 sai->iec958.status[3] = IEC958_AES3_CON_FS_192000;
901 break;
902 case 32000:
903 sai->iec958.status[3] = IEC958_AES3_CON_FS_32000;
904 break;
905 default:
906 sai->iec958.status[3] = IEC958_AES3_CON_FS_NOTID;
907 break;
908 }
909 mutex_unlock(&sai->ctrl_lock);
910}
911
olivier moysan3e086ed2017-04-10 17:19:56 +0200912static int stm32_sai_configure_clock(struct snd_soc_dai *cpu_dai,
913 struct snd_pcm_hw_params *params)
914{
915 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
YueHaibing70605452019-02-18 14:50:26 +0000916 int div = 0;
Olivier Moysan8307b2a2018-10-15 16:03:35 +0200917 int sai_clk_rate, mclk_ratio, den;
Olivier Moysan6eb17d72018-02-19 16:00:37 +0100918 unsigned int rate = params_rate(params);
olivier moysan3e086ed2017-04-10 17:19:56 +0200919
Olivier Moysan6eb17d72018-02-19 16:00:37 +0100920 if (!(rate % 11025))
olivier moysan3e086ed2017-04-10 17:19:56 +0200921 clk_set_parent(sai->sai_ck, sai->pdata->clk_x11k);
922 else
923 clk_set_parent(sai->sai_ck, sai->pdata->clk_x8k);
924 sai_clk_rate = clk_get_rate(sai->sai_ck);
925
olivier moysan03e78a22017-06-16 14:16:24 +0200926 if (STM_SAI_IS_F4(sai->pdata)) {
Olivier Moysan8307b2a2018-10-15 16:03:35 +0200927 /* mclk on (NODIV=0)
928 * mclk_rate = 256 * fs
929 * MCKDIV = 0 if sai_ck < 3/2 * mclk_rate
930 * MCKDIV = sai_ck / (2 * mclk_rate) otherwise
931 * mclk off (NODIV=1)
932 * MCKDIV ignored. sck = sai_ck
olivier moysan03e78a22017-06-16 14:16:24 +0200933 */
Olivier Moysan8307b2a2018-10-15 16:03:35 +0200934 if (!sai->mclk_rate)
935 return 0;
936
937 if (2 * sai_clk_rate >= 3 * sai->mclk_rate) {
938 div = stm32_sai_get_clk_div(sai, sai_clk_rate,
939 2 * sai->mclk_rate);
940 if (div < 0)
941 return div;
942 }
olivier moysan03e78a22017-06-16 14:16:24 +0200943 } else {
944 /*
945 * TDM mode :
946 * mclk on
947 * MCKDIV = sai_ck / (ws x 256) (NOMCK=0. OSR=0)
948 * MCKDIV = sai_ck / (ws x 512) (NOMCK=0. OSR=1)
949 * mclk off
950 * MCKDIV = sai_ck / (frl x ws) (NOMCK=1)
951 * Note: NOMCK/NODIV correspond to same bit.
952 */
Olivier Moysan6eb17d72018-02-19 16:00:37 +0100953 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
Olivier Moysan8307b2a2018-10-15 16:03:35 +0200954 div = stm32_sai_get_clk_div(sai, sai_clk_rate,
955 rate * 128);
956 if (div < 0)
957 return div;
Olivier Moysan6eb17d72018-02-19 16:00:37 +0100958 } else {
959 if (sai->mclk_rate) {
960 mclk_ratio = sai->mclk_rate / rate;
YueHaibing70605452019-02-18 14:50:26 +0000961 if ((mclk_ratio != 512) &&
962 (mclk_ratio != 256)) {
olivier moysan03e78a22017-06-16 14:16:24 +0200963 dev_err(cpu_dai->dev,
964 "Wrong mclk ratio %d\n",
965 mclk_ratio);
966 return -EINVAL;
967 }
Olivier Moysan8307b2a2018-10-15 16:03:35 +0200968 div = stm32_sai_get_clk_div(sai, sai_clk_rate,
969 sai->mclk_rate);
970 if (div < 0)
971 return div;
Olivier Moysan6eb17d72018-02-19 16:00:37 +0100972 } else {
973 /* mclk-fs not set, master clock not active */
974 den = sai->fs_length * params_rate(params);
Olivier Moysan8307b2a2018-10-15 16:03:35 +0200975 div = stm32_sai_get_clk_div(sai, sai_clk_rate,
976 den);
977 if (div < 0)
978 return div;
olivier moysan03e78a22017-06-16 14:16:24 +0200979 }
olivier moysan03e78a22017-06-16 14:16:24 +0200980 }
981 }
olivier moysan3e086ed2017-04-10 17:19:56 +0200982
Olivier Moysan8307b2a2018-10-15 16:03:35 +0200983 return stm32_sai_set_clk_div(sai, div);
olivier moysan3e086ed2017-04-10 17:19:56 +0200984}
985
986static int stm32_sai_hw_params(struct snd_pcm_substream *substream,
987 struct snd_pcm_hw_params *params,
988 struct snd_soc_dai *cpu_dai)
989{
990 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
991 int ret;
992
993 sai->data_size = params_width(params);
994
olivier moysan187e01d2018-06-11 17:13:59 +0200995 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
996 /* Rate not already set in runtime structure */
997 substream->runtime->rate = params_rate(params);
998 stm32_sai_set_iec958_status(sai, substream->runtime);
999 } else {
Olivier Moysan6eb17d72018-02-19 16:00:37 +01001000 ret = stm32_sai_set_slots(cpu_dai);
1001 if (ret < 0)
1002 return ret;
1003 stm32_sai_set_frame(cpu_dai);
1004 }
olivier moysan3e086ed2017-04-10 17:19:56 +02001005
1006 ret = stm32_sai_set_config(cpu_dai, substream, params);
1007 if (ret)
1008 return ret;
1009
1010 if (sai->master)
1011 ret = stm32_sai_configure_clock(cpu_dai, params);
1012
1013 return ret;
1014}
1015
1016static int stm32_sai_trigger(struct snd_pcm_substream *substream, int cmd,
1017 struct snd_soc_dai *cpu_dai)
1018{
1019 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
1020 int ret;
1021
1022 switch (cmd) {
1023 case SNDRV_PCM_TRIGGER_START:
1024 case SNDRV_PCM_TRIGGER_RESUME:
1025 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1026 dev_dbg(cpu_dai->dev, "Enable DMA and SAI\n");
1027
1028 regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
1029 SAI_XCR1_DMAEN, SAI_XCR1_DMAEN);
1030
1031 /* Enable SAI */
1032 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
1033 SAI_XCR1_SAIEN, SAI_XCR1_SAIEN);
1034 if (ret < 0)
1035 dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
1036 break;
1037 case SNDRV_PCM_TRIGGER_SUSPEND:
1038 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1039 case SNDRV_PCM_TRIGGER_STOP:
1040 dev_dbg(cpu_dai->dev, "Disable DMA and SAI\n");
1041
Olivier Moysan47a89072017-10-19 15:03:21 +02001042 regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX,
1043 SAI_XIMR_MASK, 0);
1044
olivier moysan3e086ed2017-04-10 17:19:56 +02001045 regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
olivier moysan4fa17932017-06-16 14:15:32 +02001046 SAI_XCR1_SAIEN,
1047 (unsigned int)~SAI_XCR1_SAIEN);
olivier moysan3e086ed2017-04-10 17:19:56 +02001048
1049 ret = regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX,
olivier moysan4fa17932017-06-16 14:15:32 +02001050 SAI_XCR1_DMAEN,
1051 (unsigned int)~SAI_XCR1_DMAEN);
olivier moysan3e086ed2017-04-10 17:19:56 +02001052 if (ret < 0)
1053 dev_err(cpu_dai->dev, "Failed to update CR1 register\n");
Olivier Moysan6eb17d72018-02-19 16:00:37 +01001054
1055 if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
1056 sai->spdif_frm_cnt = 0;
olivier moysan3e086ed2017-04-10 17:19:56 +02001057 break;
1058 default:
1059 return -EINVAL;
1060 }
1061
1062 return ret;
1063}
1064
1065static void stm32_sai_shutdown(struct snd_pcm_substream *substream,
1066 struct snd_soc_dai *cpu_dai)
1067{
1068 struct stm32_sai_sub_data *sai = snd_soc_dai_get_drvdata(cpu_dai);
Olivier Moysan26f98e82d2019-02-28 14:19:23 +01001069 unsigned long flags;
olivier moysan3e086ed2017-04-10 17:19:56 +02001070
1071 regmap_update_bits(sai->regmap, STM_SAI_IMR_REGX, SAI_XIMR_MASK, 0);
1072
olivier moysan701a6ec2017-06-16 14:15:34 +02001073 regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, SAI_XCR1_NODIV,
1074 SAI_XCR1_NODIV);
1075
olivier moysan3e086ed2017-04-10 17:19:56 +02001076 clk_disable_unprepare(sai->sai_ck);
Olivier Moysan8307b2a2018-10-15 16:03:35 +02001077
1078 clk_rate_exclusive_put(sai->sai_mclk);
1079
Olivier Moysan26f98e82d2019-02-28 14:19:23 +01001080 spin_lock_irqsave(&sai->irq_lock, flags);
olivier moysan3e086ed2017-04-10 17:19:56 +02001081 sai->substream = NULL;
Olivier Moysan26f98e82d2019-02-28 14:19:23 +01001082 spin_unlock_irqrestore(&sai->irq_lock, flags);
olivier moysan3e086ed2017-04-10 17:19:56 +02001083}
1084
olivier moysan187e01d2018-06-11 17:13:59 +02001085static int stm32_sai_pcm_new(struct snd_soc_pcm_runtime *rtd,
1086 struct snd_soc_dai *cpu_dai)
1087{
1088 struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
Olivier Moysan5f8a1002019-02-28 14:19:21 +01001089 struct snd_kcontrol_new knew = iec958_ctls;
olivier moysan187e01d2018-06-11 17:13:59 +02001090
1091 if (STM_SAI_PROTOCOL_IS_SPDIF(sai)) {
1092 dev_dbg(&sai->pdev->dev, "%s: register iec controls", __func__);
Olivier Moysan5f8a1002019-02-28 14:19:21 +01001093 knew.device = rtd->pcm->device;
1094 return snd_ctl_add(rtd->pcm->card, snd_ctl_new1(&knew, sai));
olivier moysan187e01d2018-06-11 17:13:59 +02001095 }
1096
1097 return 0;
1098}
1099
olivier moysan3e086ed2017-04-10 17:19:56 +02001100static int stm32_sai_dai_probe(struct snd_soc_dai *cpu_dai)
1101{
1102 struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
Olivier Moysan61fb4ff2017-10-19 15:03:18 +02001103 int cr1 = 0, cr1_mask;
olivier moysan3e086ed2017-04-10 17:19:56 +02001104
Olivier Moysan8307b2a2018-10-15 16:03:35 +02001105 sai->cpu_dai = cpu_dai;
1106
olivier moysan3e086ed2017-04-10 17:19:56 +02001107 sai->dma_params.addr = (dma_addr_t)(sai->phys_addr + STM_SAI_DR_REGX);
Olivier Moysana4529d22017-10-19 15:03:19 +02001108 /*
1109 * DMA supports 4, 8 or 16 burst sizes. Burst size 4 is the best choice,
1110 * as it allows bytes, half-word and words transfers. (See DMA fifos
1111 * constraints).
1112 */
1113 sai->dma_params.maxburst = 4;
olivier moysan3e086ed2017-04-10 17:19:56 +02001114 /* Buswidth will be set by framework at runtime */
1115 sai->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
1116
1117 if (STM_SAI_IS_PLAYBACK(sai))
1118 snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params, NULL);
1119 else
1120 snd_soc_dai_init_dma_data(cpu_dai, NULL, &sai->dma_params);
1121
olivier moysan187e01d2018-06-11 17:13:59 +02001122 /* Next settings are not relevant for spdif mode */
1123 if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
1124 return 0;
1125
Olivier Moysan61fb4ff2017-10-19 15:03:18 +02001126 cr1_mask = SAI_XCR1_RX_TX;
1127 if (STM_SAI_IS_CAPTURE(sai))
1128 cr1 |= SAI_XCR1_RX_TX;
1129
Olivier Moysan5914d282017-10-19 15:03:23 +02001130 /* Configure synchronization */
1131 if (sai->sync == SAI_SYNC_EXTERNAL) {
1132 /* Configure synchro client and provider */
1133 sai->pdata->set_sync(sai->pdata, sai->np_sync_provider,
1134 sai->synco, sai->synci);
1135 }
1136
1137 cr1_mask |= SAI_XCR1_SYNCEN_MASK;
1138 cr1 |= SAI_XCR1_SYNCEN_SET(sai->sync);
1139
Olivier Moysan61fb4ff2017-10-19 15:03:18 +02001140 return regmap_update_bits(sai->regmap, STM_SAI_CR1_REGX, cr1_mask, cr1);
olivier moysan3e086ed2017-04-10 17:19:56 +02001141}
1142
1143static const struct snd_soc_dai_ops stm32_sai_pcm_dai_ops = {
1144 .set_sysclk = stm32_sai_set_sysclk,
1145 .set_fmt = stm32_sai_set_dai_fmt,
1146 .set_tdm_slot = stm32_sai_set_dai_tdm_slot,
1147 .startup = stm32_sai_startup,
1148 .hw_params = stm32_sai_hw_params,
1149 .trigger = stm32_sai_trigger,
1150 .shutdown = stm32_sai_shutdown,
1151};
1152
Olivier Moysan6eb17d72018-02-19 16:00:37 +01001153static int stm32_sai_pcm_process_spdif(struct snd_pcm_substream *substream,
1154 int channel, unsigned long hwoff,
1155 void *buf, unsigned long bytes)
1156{
1157 struct snd_pcm_runtime *runtime = substream->runtime;
1158 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1159 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1160 struct stm32_sai_sub_data *sai = dev_get_drvdata(cpu_dai->dev);
1161 int *ptr = (int *)(runtime->dma_area + hwoff +
1162 channel * (runtime->dma_bytes / runtime->channels));
1163 ssize_t cnt = bytes_to_samples(runtime, bytes);
1164 unsigned int frm_cnt = sai->spdif_frm_cnt;
1165 unsigned int byte;
1166 unsigned int mask;
1167
1168 do {
1169 *ptr = ((*ptr >> 8) & 0x00ffffff);
1170
1171 /* Set channel status bit */
1172 byte = frm_cnt >> 3;
1173 mask = 1 << (frm_cnt - (byte << 3));
olivier moysan187e01d2018-06-11 17:13:59 +02001174 if (sai->iec958.status[byte] & mask)
Olivier Moysan6eb17d72018-02-19 16:00:37 +01001175 *ptr |= 0x04000000;
1176 ptr++;
1177
1178 if (!(cnt % 2))
1179 frm_cnt++;
1180
1181 if (frm_cnt == SAI_IEC60958_BLOCK_FRAMES)
1182 frm_cnt = 0;
1183 } while (--cnt);
1184 sai->spdif_frm_cnt = frm_cnt;
1185
1186 return 0;
1187}
1188
olivier moysan3e086ed2017-04-10 17:19:56 +02001189static const struct snd_pcm_hardware stm32_sai_pcm_hw = {
1190 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP,
1191 .buffer_bytes_max = 8 * PAGE_SIZE,
1192 .period_bytes_min = 1024, /* 5ms at 48kHz */
1193 .period_bytes_max = PAGE_SIZE,
1194 .periods_min = 2,
1195 .periods_max = 8,
1196};
1197
1198static struct snd_soc_dai_driver stm32_sai_playback_dai[] = {
1199{
1200 .probe = stm32_sai_dai_probe,
olivier moysan187e01d2018-06-11 17:13:59 +02001201 .pcm_new = stm32_sai_pcm_new,
olivier moysan3e086ed2017-04-10 17:19:56 +02001202 .id = 1, /* avoid call to fmt_single_name() */
1203 .playback = {
1204 .channels_min = 1,
1205 .channels_max = 2,
1206 .rate_min = 8000,
1207 .rate_max = 192000,
1208 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1209 /* DMA does not support 24 bits transfers */
1210 .formats =
1211 SNDRV_PCM_FMTBIT_S8 |
1212 SNDRV_PCM_FMTBIT_S16_LE |
1213 SNDRV_PCM_FMTBIT_S32_LE,
1214 },
1215 .ops = &stm32_sai_pcm_dai_ops,
1216 }
1217};
1218
1219static struct snd_soc_dai_driver stm32_sai_capture_dai[] = {
1220{
1221 .probe = stm32_sai_dai_probe,
1222 .id = 1, /* avoid call to fmt_single_name() */
1223 .capture = {
1224 .channels_min = 1,
1225 .channels_max = 2,
1226 .rate_min = 8000,
1227 .rate_max = 192000,
1228 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1229 /* DMA does not support 24 bits transfers */
1230 .formats =
1231 SNDRV_PCM_FMTBIT_S8 |
1232 SNDRV_PCM_FMTBIT_S16_LE |
1233 SNDRV_PCM_FMTBIT_S32_LE,
1234 },
1235 .ops = &stm32_sai_pcm_dai_ops,
1236 }
1237};
1238
1239static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config = {
Olivier Moysan6eb17d72018-02-19 16:00:37 +01001240 .pcm_hardware = &stm32_sai_pcm_hw,
1241 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
1242};
1243
1244static const struct snd_dmaengine_pcm_config stm32_sai_pcm_config_spdif = {
1245 .pcm_hardware = &stm32_sai_pcm_hw,
1246 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
1247 .process = stm32_sai_pcm_process_spdif,
olivier moysan3e086ed2017-04-10 17:19:56 +02001248};
1249
1250static const struct snd_soc_component_driver stm32_component = {
1251 .name = "stm32-sai",
1252};
1253
1254static const struct of_device_id stm32_sai_sub_ids[] = {
1255 { .compatible = "st,stm32-sai-sub-a",
1256 .data = (void *)STM_SAI_A_ID},
1257 { .compatible = "st,stm32-sai-sub-b",
1258 .data = (void *)STM_SAI_B_ID},
1259 {}
1260};
1261MODULE_DEVICE_TABLE(of, stm32_sai_sub_ids);
1262
1263static int stm32_sai_sub_parse_of(struct platform_device *pdev,
1264 struct stm32_sai_sub_data *sai)
1265{
1266 struct device_node *np = pdev->dev.of_node;
1267 struct resource *res;
1268 void __iomem *base;
Olivier Moysan5914d282017-10-19 15:03:23 +02001269 struct of_phandle_args args;
1270 int ret;
olivier moysan3e086ed2017-04-10 17:19:56 +02001271
1272 if (!np)
1273 return -ENODEV;
1274
1275 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
olivier moysan3e086ed2017-04-10 17:19:56 +02001276 base = devm_ioremap_resource(&pdev->dev, res);
1277 if (IS_ERR(base))
1278 return PTR_ERR(base);
1279
1280 sai->phys_addr = res->start;
olivier moysan03e78a22017-06-16 14:16:24 +02001281
1282 sai->regmap_config = &stm32_sai_sub_regmap_config_f4;
1283 /* Note: PDM registers not available for H7 sub-block B */
1284 if (STM_SAI_IS_H7(sai->pdata) && STM_SAI_IS_SUB_A(sai))
1285 sai->regmap_config = &stm32_sai_sub_regmap_config_h7;
1286
1287 sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "sai_ck",
1288 base, sai->regmap_config);
1289 if (IS_ERR(sai->regmap)) {
1290 dev_err(&pdev->dev, "Failed to initialize MMIO\n");
1291 return PTR_ERR(sai->regmap);
1292 }
olivier moysan3e086ed2017-04-10 17:19:56 +02001293
1294 /* Get direction property */
1295 if (of_property_match_string(np, "dma-names", "tx") >= 0) {
1296 sai->dir = SNDRV_PCM_STREAM_PLAYBACK;
1297 } else if (of_property_match_string(np, "dma-names", "rx") >= 0) {
1298 sai->dir = SNDRV_PCM_STREAM_CAPTURE;
1299 } else {
1300 dev_err(&pdev->dev, "Unsupported direction\n");
1301 return -EINVAL;
1302 }
1303
Olivier Moysan6eb17d72018-02-19 16:00:37 +01001304 /* Get spdif iec60958 property */
1305 sai->spdif = false;
1306 if (of_get_property(np, "st,iec60958", NULL)) {
1307 if (!STM_SAI_HAS_SPDIF(sai) ||
1308 sai->dir == SNDRV_PCM_STREAM_CAPTURE) {
1309 dev_err(&pdev->dev, "S/PDIF IEC60958 not supported\n");
1310 return -EINVAL;
1311 }
olivier moysan187e01d2018-06-11 17:13:59 +02001312 stm32_sai_init_iec958_status(sai);
Olivier Moysan6eb17d72018-02-19 16:00:37 +01001313 sai->spdif = true;
1314 sai->master = true;
1315 }
1316
Olivier Moysan5914d282017-10-19 15:03:23 +02001317 /* Get synchronization property */
1318 args.np = NULL;
1319 ret = of_parse_phandle_with_fixed_args(np, "st,sync", 1, 0, &args);
1320 if (ret < 0 && ret != -ENOENT) {
1321 dev_err(&pdev->dev, "Failed to get st,sync property\n");
1322 return ret;
1323 }
1324
1325 sai->sync = SAI_SYNC_NONE;
1326 if (args.np) {
1327 if (args.np == np) {
Rob Herring5d585e12018-08-28 10:44:28 -05001328 dev_err(&pdev->dev, "%pOFn sync own reference\n", np);
Olivier Moysan5914d282017-10-19 15:03:23 +02001329 of_node_put(args.np);
1330 return -EINVAL;
1331 }
1332
1333 sai->np_sync_provider = of_get_parent(args.np);
1334 if (!sai->np_sync_provider) {
Rob Herring5d585e12018-08-28 10:44:28 -05001335 dev_err(&pdev->dev, "%pOFn parent node not found\n",
1336 np);
Olivier Moysan5914d282017-10-19 15:03:23 +02001337 of_node_put(args.np);
1338 return -ENODEV;
1339 }
1340
1341 sai->sync = SAI_SYNC_INTERNAL;
1342 if (sai->np_sync_provider != sai->pdata->pdev->dev.of_node) {
1343 if (!STM_SAI_HAS_EXT_SYNC(sai)) {
1344 dev_err(&pdev->dev,
1345 "External synchro not supported\n");
1346 of_node_put(args.np);
1347 return -EINVAL;
1348 }
1349 sai->sync = SAI_SYNC_EXTERNAL;
1350
1351 sai->synci = args.args[0];
1352 if (sai->synci < 1 ||
1353 (sai->synci > (SAI_GCR_SYNCIN_MAX + 1))) {
1354 dev_err(&pdev->dev, "Wrong SAI index\n");
1355 of_node_put(args.np);
1356 return -EINVAL;
1357 }
1358
1359 if (of_property_match_string(args.np, "compatible",
1360 "st,stm32-sai-sub-a") >= 0)
1361 sai->synco = STM_SAI_SYNC_OUT_A;
1362
1363 if (of_property_match_string(args.np, "compatible",
1364 "st,stm32-sai-sub-b") >= 0)
1365 sai->synco = STM_SAI_SYNC_OUT_B;
1366
1367 if (!sai->synco) {
1368 dev_err(&pdev->dev, "Unknown SAI sub-block\n");
1369 of_node_put(args.np);
1370 return -EINVAL;
1371 }
1372 }
1373
1374 dev_dbg(&pdev->dev, "%s synchronized with %s\n",
1375 pdev->name, args.np->full_name);
1376 }
1377
1378 of_node_put(args.np);
olivier moysan3e086ed2017-04-10 17:19:56 +02001379 sai->sai_ck = devm_clk_get(&pdev->dev, "sai_ck");
1380 if (IS_ERR(sai->sai_ck)) {
olivier moysan602fdad2017-06-16 14:15:30 +02001381 dev_err(&pdev->dev, "Missing kernel clock sai_ck\n");
olivier moysan3e086ed2017-04-10 17:19:56 +02001382 return PTR_ERR(sai->sai_ck);
1383 }
1384
Olivier Moysan8307b2a2018-10-15 16:03:35 +02001385 if (STM_SAI_IS_F4(sai->pdata))
1386 return 0;
1387
1388 /* Register mclk provider if requested */
1389 if (of_find_property(np, "#clock-cells", NULL)) {
1390 ret = stm32_sai_add_mclk_provider(sai);
1391 if (ret < 0)
1392 return ret;
1393 } else {
1394 sai->sai_mclk = devm_clk_get(&pdev->dev, "MCLK");
1395 if (IS_ERR(sai->sai_mclk)) {
1396 if (PTR_ERR(sai->sai_mclk) != -ENOENT)
1397 return PTR_ERR(sai->sai_mclk);
1398 sai->sai_mclk = NULL;
1399 }
1400 }
1401
olivier moysan3e086ed2017-04-10 17:19:56 +02001402 return 0;
1403}
1404
1405static int stm32_sai_sub_dais_init(struct platform_device *pdev,
1406 struct stm32_sai_sub_data *sai)
1407{
1408 sai->cpu_dai_drv = devm_kzalloc(&pdev->dev,
1409 sizeof(struct snd_soc_dai_driver),
1410 GFP_KERNEL);
1411 if (!sai->cpu_dai_drv)
1412 return -ENOMEM;
1413
1414 sai->cpu_dai_drv->name = dev_name(&pdev->dev);
1415 if (STM_SAI_IS_PLAYBACK(sai)) {
1416 memcpy(sai->cpu_dai_drv, &stm32_sai_playback_dai,
1417 sizeof(stm32_sai_playback_dai));
1418 sai->cpu_dai_drv->playback.stream_name = sai->cpu_dai_drv->name;
1419 } else {
1420 memcpy(sai->cpu_dai_drv, &stm32_sai_capture_dai,
1421 sizeof(stm32_sai_capture_dai));
1422 sai->cpu_dai_drv->capture.stream_name = sai->cpu_dai_drv->name;
1423 }
1424
1425 return 0;
1426}
1427
1428static int stm32_sai_sub_probe(struct platform_device *pdev)
1429{
1430 struct stm32_sai_sub_data *sai;
1431 const struct of_device_id *of_id;
Olivier Moysan6eb17d72018-02-19 16:00:37 +01001432 const struct snd_dmaengine_pcm_config *conf = &stm32_sai_pcm_config;
olivier moysan3e086ed2017-04-10 17:19:56 +02001433 int ret;
1434
1435 sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
1436 if (!sai)
1437 return -ENOMEM;
1438
1439 of_id = of_match_device(stm32_sai_sub_ids, &pdev->dev);
1440 if (!of_id)
1441 return -EINVAL;
1442 sai->id = (uintptr_t)of_id->data;
1443
1444 sai->pdev = pdev;
olivier moysan187e01d2018-06-11 17:13:59 +02001445 mutex_init(&sai->ctrl_lock);
Olivier Moysan26f98e82d2019-02-28 14:19:23 +01001446 spin_lock_init(&sai->irq_lock);
olivier moysan3e086ed2017-04-10 17:19:56 +02001447 platform_set_drvdata(pdev, sai);
1448
1449 sai->pdata = dev_get_drvdata(pdev->dev.parent);
1450 if (!sai->pdata) {
1451 dev_err(&pdev->dev, "Parent device data not available\n");
1452 return -EINVAL;
1453 }
1454
1455 ret = stm32_sai_sub_parse_of(pdev, sai);
1456 if (ret)
1457 return ret;
1458
1459 ret = stm32_sai_sub_dais_init(pdev, sai);
1460 if (ret)
1461 return ret;
1462
1463 ret = devm_request_irq(&pdev->dev, sai->pdata->irq, stm32_sai_isr,
1464 IRQF_SHARED, dev_name(&pdev->dev), sai);
1465 if (ret) {
olivier moysan602fdad2017-06-16 14:15:30 +02001466 dev_err(&pdev->dev, "IRQ request returned %d\n", ret);
olivier moysan3e086ed2017-04-10 17:19:56 +02001467 return ret;
1468 }
1469
1470 ret = devm_snd_soc_register_component(&pdev->dev, &stm32_component,
1471 sai->cpu_dai_drv, 1);
1472 if (ret)
1473 return ret;
1474
Olivier Moysan6eb17d72018-02-19 16:00:37 +01001475 if (STM_SAI_PROTOCOL_IS_SPDIF(sai))
1476 conf = &stm32_sai_pcm_config_spdif;
1477
1478 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, conf, 0);
olivier moysan3e086ed2017-04-10 17:19:56 +02001479 if (ret) {
olivier moysan602fdad2017-06-16 14:15:30 +02001480 dev_err(&pdev->dev, "Could not register pcm dma\n");
olivier moysan3e086ed2017-04-10 17:19:56 +02001481 return ret;
1482 }
1483
1484 return 0;
1485}
1486
1487static struct platform_driver stm32_sai_sub_driver = {
1488 .driver = {
1489 .name = "st,stm32-sai-sub",
1490 .of_match_table = stm32_sai_sub_ids,
1491 },
1492 .probe = stm32_sai_sub_probe,
1493};
1494
1495module_platform_driver(stm32_sai_sub_driver);
1496
1497MODULE_DESCRIPTION("STM32 Soc SAI sub-block Interface");
olivier moysan602fdad2017-06-16 14:15:30 +02001498MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>");
olivier moysan3e086ed2017-04-10 17:19:56 +02001499MODULE_ALIAS("platform:st,stm32-sai-sub");
1500MODULE_LICENSE("GPL v2");