1 // SPDX-License-Identifier: GPL-2.0
3 // Xilinx ASoC audio formatter support
5 // Copyright (C) 2018 Xilinx, Inc.
7 // Author: Maruthi Srinivas Bayyavarapu <maruthis@xilinx.com>
11 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <linux/of_irq.h>
14 #include <linux/sizes.h>
16 #include <sound/asoundef.h>
17 #include <sound/soc.h>
18 #include <sound/pcm_params.h>
20 #define DRV_NAME "xlnx_formatter_pcm"
22 #define XLNX_S2MM_OFFSET 0
23 #define XLNX_MM2S_OFFSET 0x100
25 #define XLNX_AUD_CORE_CONFIG 0x4
26 #define XLNX_AUD_CTRL 0x10
27 #define XLNX_AUD_STS 0x14
29 #define AUD_CTRL_RESET_MASK BIT(1)
30 #define AUD_CFG_MM2S_MASK BIT(15)
31 #define AUD_CFG_S2MM_MASK BIT(31)
33 #define XLNX_AUD_FS_MULTIPLIER 0x18
34 #define XLNX_AUD_PERIOD_CONFIG 0x1C
35 #define XLNX_AUD_BUFF_ADDR_LSB 0x20
36 #define XLNX_AUD_BUFF_ADDR_MSB 0x24
37 #define XLNX_AUD_XFER_COUNT 0x28
38 #define XLNX_AUD_CH_STS_START 0x2C
39 #define XLNX_BYTES_PER_CH 0x44
40 #define XLNX_AUD_ALIGN_BYTES 64
42 #define AUD_STS_IOC_IRQ_MASK BIT(31)
43 #define AUD_STS_CH_STS_MASK BIT(29)
44 #define AUD_CTRL_IOC_IRQ_MASK BIT(13)
45 #define AUD_CTRL_TOUT_IRQ_MASK BIT(14)
46 #define AUD_CTRL_DMA_EN_MASK BIT(0)
48 #define CFG_MM2S_CH_MASK GENMASK(11, 8)
49 #define CFG_MM2S_CH_SHIFT 8
50 #define CFG_MM2S_XFER_MASK GENMASK(14, 13)
51 #define CFG_MM2S_XFER_SHIFT 13
52 #define CFG_MM2S_PKG_MASK BIT(12)
54 #define CFG_S2MM_CH_MASK GENMASK(27, 24)
55 #define CFG_S2MM_CH_SHIFT 24
56 #define CFG_S2MM_XFER_MASK GENMASK(30, 29)
57 #define CFG_S2MM_XFER_SHIFT 29
58 #define CFG_S2MM_PKG_MASK BIT(28)
60 #define AUD_CTRL_DATA_WIDTH_SHIFT 16
61 #define AUD_CTRL_ACTIVE_CH_SHIFT 19
62 #define PERIOD_CFG_PERIODS_SHIFT 16
66 #define PERIOD_BYTES_MIN 192
67 #define PERIOD_BYTES_MAX (50 * 1024)
68 #define XLNX_PARAM_UNKNOWN 0
78 struct xlnx_pcm_drv_data {
84 struct snd_pcm_substream *play_stream;
85 struct snd_pcm_substream *capture_stream;
90 * struct xlnx_pcm_stream_param - stream configuration
91 * @mmio: base address offset
92 * @interleaved: audio channels arrangement in buffer
93 * @xfer_mode: data formatting mode during transfer
94 * @ch_limit: Maximum channels supported
95 * @buffer_size: stream ring buffer size
97 struct xlnx_pcm_stream_param {
105 static const struct snd_pcm_hardware xlnx_pcm_hardware = {
106 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
107 SNDRV_PCM_INFO_BATCH | SNDRV_PCM_INFO_PAUSE |
108 SNDRV_PCM_INFO_RESUME,
109 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |
110 SNDRV_PCM_FMTBIT_S24_LE,
113 .rates = SNDRV_PCM_RATE_8000_192000,
116 .buffer_bytes_max = PERIODS_MAX * PERIOD_BYTES_MAX,
117 .period_bytes_min = PERIOD_BYTES_MIN,
118 .period_bytes_max = PERIOD_BYTES_MAX,
119 .periods_min = PERIODS_MIN,
120 .periods_max = PERIODS_MAX,
130 static void xlnx_parse_aes_params(u32 chsts_reg1_val, u32 chsts_reg2_val,
133 u32 padded, srate, bit_depth, status[2];
135 if (chsts_reg1_val & IEC958_AES0_PROFESSIONAL) {
136 status[0] = chsts_reg1_val & 0xff;
137 status[1] = (chsts_reg1_val >> 16) & 0xff;
139 switch (status[0] & IEC958_AES0_PRO_FS) {
140 case IEC958_AES0_PRO_FS_44100:
143 case IEC958_AES0_PRO_FS_48000:
146 case IEC958_AES0_PRO_FS_32000:
149 case IEC958_AES0_PRO_FS_NOTID:
151 srate = XLNX_PARAM_UNKNOWN;
155 switch (status[1] & IEC958_AES2_PRO_SBITS) {
156 case IEC958_AES2_PRO_WORDLEN_NOTID:
157 case IEC958_AES2_PRO_SBITS_20:
160 case IEC958_AES2_PRO_SBITS_24:
164 bit_depth = XLNX_PARAM_UNKNOWN;
168 switch (status[1] & IEC958_AES2_PRO_WORDLEN) {
169 case IEC958_AES2_PRO_WORDLEN_20_16:
170 bit_depth = 16 + padded;
172 case IEC958_AES2_PRO_WORDLEN_22_18:
173 bit_depth = 18 + padded;
175 case IEC958_AES2_PRO_WORDLEN_23_19:
176 bit_depth = 19 + padded;
178 case IEC958_AES2_PRO_WORDLEN_24_20:
179 bit_depth = 20 + padded;
181 case IEC958_AES2_PRO_WORDLEN_NOTID:
183 bit_depth = XLNX_PARAM_UNKNOWN;
188 status[0] = (chsts_reg1_val >> 24) & 0xff;
189 status[1] = chsts_reg2_val & 0xff;
191 switch (status[0] & IEC958_AES3_CON_FS) {
192 case IEC958_AES3_CON_FS_44100:
195 case IEC958_AES3_CON_FS_48000:
198 case IEC958_AES3_CON_FS_32000:
202 srate = XLNX_PARAM_UNKNOWN;
206 if (status[1] & IEC958_AES4_CON_MAX_WORDLEN_24)
211 switch (status[1] & IEC958_AES4_CON_WORDLEN) {
212 case IEC958_AES4_CON_WORDLEN_20_16:
213 bit_depth = 16 + padded;
215 case IEC958_AES4_CON_WORDLEN_22_18:
216 bit_depth = 18 + padded;
218 case IEC958_AES4_CON_WORDLEN_23_19:
219 bit_depth = 19 + padded;
221 case IEC958_AES4_CON_WORDLEN_24_20:
222 bit_depth = 20 + padded;
224 case IEC958_AES4_CON_WORDLEN_21_17:
225 bit_depth = 17 + padded;
227 case IEC958_AES4_CON_WORDLEN_NOTID:
229 bit_depth = XLNX_PARAM_UNKNOWN;
235 if (srate != XLNX_PARAM_UNKNOWN)
236 dev_info(dev, "sample rate = %d\n", srate);
238 dev_info(dev, "sample rate = unknown\n");
240 if (bit_depth != XLNX_PARAM_UNKNOWN)
241 dev_info(dev, "bit_depth = %d\n", bit_depth);
243 dev_info(dev, "bit_depth = unknown\n");
246 static int xlnx_formatter_pcm_reset(void __iomem *mmio_base)
248 u32 val, retries = 0;
250 val = readl(mmio_base + XLNX_AUD_CTRL);
251 val |= AUD_CTRL_RESET_MASK;
252 writel(val, mmio_base + XLNX_AUD_CTRL);
254 val = readl(mmio_base + XLNX_AUD_CTRL);
255 /* Poll for maximum timeout of approximately 100ms (1 * 100)*/
256 while ((val & AUD_CTRL_RESET_MASK) && (retries < 100)) {
259 val = readl(mmio_base + XLNX_AUD_CTRL);
261 if (val & AUD_CTRL_RESET_MASK)
267 static void xlnx_formatter_disable_irqs(void __iomem *mmio_base, int stream)
271 val = readl(mmio_base + XLNX_AUD_CTRL);
272 val &= ~AUD_CTRL_IOC_IRQ_MASK;
273 if (stream == SNDRV_PCM_STREAM_CAPTURE)
274 val &= ~AUD_CTRL_TOUT_IRQ_MASK;
276 writel(val, mmio_base + XLNX_AUD_CTRL);
279 static irqreturn_t xlnx_mm2s_irq_handler(int irq, void *arg)
283 struct device *dev = arg;
284 struct xlnx_pcm_drv_data *adata = dev_get_drvdata(dev);
286 reg = adata->mmio + XLNX_MM2S_OFFSET + XLNX_AUD_STS;
288 if (val & AUD_STS_IOC_IRQ_MASK) {
289 writel(val & AUD_STS_IOC_IRQ_MASK, reg);
290 if (adata->play_stream)
291 snd_pcm_period_elapsed(adata->play_stream);
298 static irqreturn_t xlnx_s2mm_irq_handler(int irq, void *arg)
302 struct device *dev = arg;
303 struct xlnx_pcm_drv_data *adata = dev_get_drvdata(dev);
305 reg = adata->mmio + XLNX_S2MM_OFFSET + XLNX_AUD_STS;
307 if (val & AUD_STS_IOC_IRQ_MASK) {
308 writel(val & AUD_STS_IOC_IRQ_MASK, reg);
309 if (adata->capture_stream)
310 snd_pcm_period_elapsed(adata->capture_stream);
317 static int xlnx_formatter_pcm_open(struct snd_pcm_substream *substream)
320 u32 val, data_format_mode;
321 u32 ch_count_mask, ch_count_shift, data_xfer_mode, data_xfer_shift;
322 struct xlnx_pcm_stream_param *stream_data;
323 struct snd_pcm_runtime *runtime = substream->runtime;
324 struct snd_soc_pcm_runtime *prtd = substream->private_data;
325 struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
327 struct xlnx_pcm_drv_data *adata = dev_get_drvdata(component->dev);
329 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
330 !adata->mm2s_presence)
332 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
333 !adata->s2mm_presence)
336 stream_data = kzalloc(sizeof(*stream_data), GFP_KERNEL);
340 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
341 ch_count_mask = CFG_MM2S_CH_MASK;
342 ch_count_shift = CFG_MM2S_CH_SHIFT;
343 data_xfer_mode = CFG_MM2S_XFER_MASK;
344 data_xfer_shift = CFG_MM2S_XFER_SHIFT;
345 data_format_mode = CFG_MM2S_PKG_MASK;
346 stream_data->mmio = adata->mmio + XLNX_MM2S_OFFSET;
347 adata->play_stream = substream;
350 ch_count_mask = CFG_S2MM_CH_MASK;
351 ch_count_shift = CFG_S2MM_CH_SHIFT;
352 data_xfer_mode = CFG_S2MM_XFER_MASK;
353 data_xfer_shift = CFG_S2MM_XFER_SHIFT;
354 data_format_mode = CFG_S2MM_PKG_MASK;
355 stream_data->mmio = adata->mmio + XLNX_S2MM_OFFSET;
356 adata->capture_stream = substream;
359 val = readl(adata->mmio + XLNX_AUD_CORE_CONFIG);
361 if (!(val & data_format_mode))
362 stream_data->interleaved = true;
364 stream_data->xfer_mode = (val & data_xfer_mode) >> data_xfer_shift;
365 stream_data->ch_limit = (val & ch_count_mask) >> ch_count_shift;
366 dev_info(component->dev,
367 "stream %d : format = %d mode = %d ch_limit = %d\n",
368 substream->stream, stream_data->interleaved,
369 stream_data->xfer_mode, stream_data->ch_limit);
371 snd_soc_set_runtime_hwparams(substream, &xlnx_pcm_hardware);
372 runtime->private_data = stream_data;
374 /* Resize the period bytes as divisible by 64 */
375 err = snd_pcm_hw_constraint_step(runtime, 0,
376 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
377 XLNX_AUD_ALIGN_BYTES);
379 dev_err(component->dev,
380 "Unable to set constraint on period bytes\n");
384 /* Resize the buffer bytes as divisible by 64 */
385 err = snd_pcm_hw_constraint_step(runtime, 0,
386 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
387 XLNX_AUD_ALIGN_BYTES);
389 dev_err(component->dev,
390 "Unable to set constraint on buffer bytes\n");
394 /* Set periods as integer multiple */
395 err = snd_pcm_hw_constraint_integer(runtime,
396 SNDRV_PCM_HW_PARAM_PERIODS);
398 dev_err(component->dev,
399 "Unable to set constraint on periods to be integer\n");
403 /* enable DMA IOC irq */
404 val = readl(stream_data->mmio + XLNX_AUD_CTRL);
405 val |= AUD_CTRL_IOC_IRQ_MASK;
406 writel(val, stream_data->mmio + XLNX_AUD_CTRL);
411 static int xlnx_formatter_pcm_close(struct snd_pcm_substream *substream)
414 struct xlnx_pcm_stream_param *stream_data =
415 substream->runtime->private_data;
416 struct snd_soc_pcm_runtime *prtd = substream->private_data;
417 struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
420 ret = xlnx_formatter_pcm_reset(stream_data->mmio);
422 dev_err(component->dev, "audio formatter reset failed\n");
425 xlnx_formatter_disable_irqs(stream_data->mmio, substream->stream);
432 static snd_pcm_uframes_t
433 xlnx_formatter_pcm_pointer(struct snd_pcm_substream *substream)
436 struct snd_pcm_runtime *runtime = substream->runtime;
437 struct xlnx_pcm_stream_param *stream_data = runtime->private_data;
439 pos = readl(stream_data->mmio + XLNX_AUD_XFER_COUNT);
441 if (pos >= stream_data->buffer_size)
444 return bytes_to_frames(runtime, pos);
447 static int xlnx_formatter_pcm_hw_params(struct snd_pcm_substream *substream,
448 struct snd_pcm_hw_params *params)
450 u32 low, high, active_ch, val, bytes_per_ch, bits_per_sample;
451 u32 aes_reg1_val, aes_reg2_val;
454 struct snd_soc_pcm_runtime *prtd = substream->private_data;
455 struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
457 struct snd_pcm_runtime *runtime = substream->runtime;
458 struct xlnx_pcm_stream_param *stream_data = runtime->private_data;
460 active_ch = params_channels(params);
461 if (active_ch > stream_data->ch_limit)
464 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
465 stream_data->xfer_mode == AES_TO_PCM) {
466 val = readl(stream_data->mmio + XLNX_AUD_STS);
467 if (val & AUD_STS_CH_STS_MASK) {
468 aes_reg1_val = readl(stream_data->mmio +
469 XLNX_AUD_CH_STS_START);
470 aes_reg2_val = readl(stream_data->mmio +
471 XLNX_AUD_CH_STS_START + 0x4);
473 xlnx_parse_aes_params(aes_reg1_val, aes_reg2_val,
478 size = params_buffer_bytes(params);
479 status = snd_pcm_lib_malloc_pages(substream, size);
483 stream_data->buffer_size = size;
485 low = lower_32_bits(runtime->dma_addr);
486 high = upper_32_bits(runtime->dma_addr);
487 writel(low, stream_data->mmio + XLNX_AUD_BUFF_ADDR_LSB);
488 writel(high, stream_data->mmio + XLNX_AUD_BUFF_ADDR_MSB);
490 val = readl(stream_data->mmio + XLNX_AUD_CTRL);
491 bits_per_sample = params_width(params);
492 switch (bits_per_sample) {
494 val |= (BIT_DEPTH_8 << AUD_CTRL_DATA_WIDTH_SHIFT);
497 val |= (BIT_DEPTH_16 << AUD_CTRL_DATA_WIDTH_SHIFT);
500 val |= (BIT_DEPTH_20 << AUD_CTRL_DATA_WIDTH_SHIFT);
503 val |= (BIT_DEPTH_24 << AUD_CTRL_DATA_WIDTH_SHIFT);
506 val |= (BIT_DEPTH_32 << AUD_CTRL_DATA_WIDTH_SHIFT);
512 val |= active_ch << AUD_CTRL_ACTIVE_CH_SHIFT;
513 writel(val, stream_data->mmio + XLNX_AUD_CTRL);
515 val = (params_periods(params) << PERIOD_CFG_PERIODS_SHIFT)
516 | params_period_bytes(params);
517 writel(val, stream_data->mmio + XLNX_AUD_PERIOD_CONFIG);
518 bytes_per_ch = DIV_ROUND_UP(params_period_bytes(params), active_ch);
519 writel(bytes_per_ch, stream_data->mmio + XLNX_BYTES_PER_CH);
524 static int xlnx_formatter_pcm_hw_free(struct snd_pcm_substream *substream)
526 return snd_pcm_lib_free_pages(substream);
529 static int xlnx_formatter_pcm_trigger(struct snd_pcm_substream *substream,
533 struct xlnx_pcm_stream_param *stream_data =
534 substream->runtime->private_data;
537 case SNDRV_PCM_TRIGGER_START:
538 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
539 case SNDRV_PCM_TRIGGER_RESUME:
540 val = readl(stream_data->mmio + XLNX_AUD_CTRL);
541 val |= AUD_CTRL_DMA_EN_MASK;
542 writel(val, stream_data->mmio + XLNX_AUD_CTRL);
544 case SNDRV_PCM_TRIGGER_STOP:
545 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
546 case SNDRV_PCM_TRIGGER_SUSPEND:
547 val = readl(stream_data->mmio + XLNX_AUD_CTRL);
548 val &= ~AUD_CTRL_DMA_EN_MASK;
549 writel(val, stream_data->mmio + XLNX_AUD_CTRL);
556 static int xlnx_formatter_pcm_new(struct snd_soc_pcm_runtime *rtd)
558 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd,
560 snd_pcm_lib_preallocate_pages_for_all(rtd->pcm,
561 SNDRV_DMA_TYPE_DEV, component->dev,
562 xlnx_pcm_hardware.buffer_bytes_max,
563 xlnx_pcm_hardware.buffer_bytes_max);
567 static const struct snd_pcm_ops xlnx_formatter_pcm_ops = {
568 .open = xlnx_formatter_pcm_open,
569 .close = xlnx_formatter_pcm_close,
570 .ioctl = snd_pcm_lib_ioctl,
571 .hw_params = xlnx_formatter_pcm_hw_params,
572 .hw_free = xlnx_formatter_pcm_hw_free,
573 .trigger = xlnx_formatter_pcm_trigger,
574 .pointer = xlnx_formatter_pcm_pointer,
577 static const struct snd_soc_component_driver xlnx_asoc_component = {
579 .ops = &xlnx_formatter_pcm_ops,
580 .pcm_new = xlnx_formatter_pcm_new,
583 static int xlnx_formatter_pcm_probe(struct platform_device *pdev)
587 struct xlnx_pcm_drv_data *aud_drv_data;
588 struct resource *res;
589 struct device *dev = &pdev->dev;
591 aud_drv_data = devm_kzalloc(dev, sizeof(*aud_drv_data), GFP_KERNEL);
595 aud_drv_data->axi_clk = devm_clk_get(dev, "s_axi_lite_aclk");
596 if (IS_ERR(aud_drv_data->axi_clk)) {
597 ret = PTR_ERR(aud_drv_data->axi_clk);
598 dev_err(dev, "failed to get s_axi_lite_aclk(%d)\n", ret);
601 ret = clk_prepare_enable(aud_drv_data->axi_clk);
604 "failed to enable s_axi_lite_aclk(%d)\n", ret);
608 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
610 dev_err(dev, "audio formatter node:addr to resource failed\n");
614 aud_drv_data->mmio = devm_ioremap_resource(dev, res);
615 if (IS_ERR(aud_drv_data->mmio)) {
616 dev_err(dev, "audio formatter ioremap failed\n");
617 ret = PTR_ERR(aud_drv_data->mmio);
621 val = readl(aud_drv_data->mmio + XLNX_AUD_CORE_CONFIG);
622 if (val & AUD_CFG_MM2S_MASK) {
623 aud_drv_data->mm2s_presence = true;
624 ret = xlnx_formatter_pcm_reset(aud_drv_data->mmio +
627 dev_err(dev, "audio formatter reset failed\n");
630 xlnx_formatter_disable_irqs(aud_drv_data->mmio +
632 SNDRV_PCM_STREAM_PLAYBACK);
634 aud_drv_data->mm2s_irq = platform_get_irq_byname(pdev,
636 if (aud_drv_data->mm2s_irq < 0) {
637 ret = aud_drv_data->mm2s_irq;
640 ret = devm_request_irq(dev, aud_drv_data->mm2s_irq,
641 xlnx_mm2s_irq_handler, 0,
642 "xlnx_formatter_pcm_mm2s_irq", dev);
644 dev_err(dev, "xlnx audio mm2s irq request failed\n");
648 if (val & AUD_CFG_S2MM_MASK) {
649 aud_drv_data->s2mm_presence = true;
650 ret = xlnx_formatter_pcm_reset(aud_drv_data->mmio +
653 dev_err(dev, "audio formatter reset failed\n");
656 xlnx_formatter_disable_irqs(aud_drv_data->mmio +
658 SNDRV_PCM_STREAM_CAPTURE);
660 aud_drv_data->s2mm_irq = platform_get_irq_byname(pdev,
662 if (aud_drv_data->s2mm_irq < 0) {
663 ret = aud_drv_data->s2mm_irq;
666 ret = devm_request_irq(dev, aud_drv_data->s2mm_irq,
667 xlnx_s2mm_irq_handler, 0,
668 "xlnx_formatter_pcm_s2mm_irq",
671 dev_err(dev, "xlnx audio s2mm irq request failed\n");
676 dev_set_drvdata(dev, aud_drv_data);
678 ret = devm_snd_soc_register_component(dev, &xlnx_asoc_component,
681 dev_err(dev, "pcm platform device register failed\n");
688 clk_disable_unprepare(aud_drv_data->axi_clk);
692 static int xlnx_formatter_pcm_remove(struct platform_device *pdev)
695 struct xlnx_pcm_drv_data *adata = dev_get_drvdata(&pdev->dev);
697 if (adata->s2mm_presence)
698 ret = xlnx_formatter_pcm_reset(adata->mmio + XLNX_S2MM_OFFSET);
700 /* Try MM2S reset, even if S2MM reset fails */
701 if (adata->mm2s_presence)
702 ret = xlnx_formatter_pcm_reset(adata->mmio + XLNX_MM2S_OFFSET);
705 dev_err(&pdev->dev, "audio formatter reset failed\n");
707 clk_disable_unprepare(adata->axi_clk);
711 static const struct of_device_id xlnx_formatter_pcm_of_match[] = {
712 { .compatible = "xlnx,audio-formatter-1.0"},
715 MODULE_DEVICE_TABLE(of, xlnx_formatter_pcm_of_match);
717 static struct platform_driver xlnx_formatter_pcm_driver = {
718 .probe = xlnx_formatter_pcm_probe,
719 .remove = xlnx_formatter_pcm_remove,
722 .of_match_table = xlnx_formatter_pcm_of_match,
726 module_platform_driver(xlnx_formatter_pcm_driver);
727 MODULE_AUTHOR("Maruthi Srinivas Bayyavarapu <maruthis@xilinx.com>");
728 MODULE_LICENSE("GPL v2");