1 // SPDX-License-Identifier: GPL-2.0
3 * ALSA SoC using the QUICC Multichannel Controller (QMC)
5 * Copyright 2022 CS GROUP France
7 * Author: Herve Codina <herve.codina@bootlin.com>
10 #include <linux/dma-mapping.h>
11 #include <linux/module.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <soc/fsl/qe/qmc.h>
17 #include <sound/pcm_params.h>
18 #include <sound/soc.h>
24 struct qmc_chan *qmc_chan;
25 unsigned int nb_tx_ts;
26 unsigned int nb_rx_ts;
31 unsigned int num_dais;
33 struct snd_soc_dai_driver *dai_drivers;
37 struct qmc_dai *qmc_dai;
38 dma_addr_t dma_buffer_start;
39 dma_addr_t period_ptr_submitted;
40 dma_addr_t period_ptr_ended;
41 dma_addr_t dma_buffer_end;
43 struct snd_pcm_substream *substream;
46 static int qmc_audio_pcm_construct(struct snd_soc_component *component,
47 struct snd_soc_pcm_runtime *rtd)
49 struct snd_card *card = rtd->card->snd_card;
52 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
56 snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV, card->dev,
61 static int qmc_audio_pcm_hw_params(struct snd_soc_component *component,
62 struct snd_pcm_substream *substream,
63 struct snd_pcm_hw_params *params)
65 struct snd_pcm_runtime *runtime = substream->runtime;
66 struct qmc_dai_prtd *prtd = substream->runtime->private_data;
68 prtd->dma_buffer_start = runtime->dma_addr;
69 prtd->dma_buffer_end = runtime->dma_addr + params_buffer_bytes(params);
70 prtd->period_size = params_period_bytes(params);
71 prtd->period_ptr_submitted = prtd->dma_buffer_start;
72 prtd->period_ptr_ended = prtd->dma_buffer_start;
73 prtd->substream = substream;
78 static void qmc_audio_pcm_write_complete(void *context)
80 struct qmc_dai_prtd *prtd = context;
83 prtd->period_ptr_ended += prtd->period_size;
84 if (prtd->period_ptr_ended >= prtd->dma_buffer_end)
85 prtd->period_ptr_ended = prtd->dma_buffer_start;
87 prtd->period_ptr_submitted += prtd->period_size;
88 if (prtd->period_ptr_submitted >= prtd->dma_buffer_end)
89 prtd->period_ptr_submitted = prtd->dma_buffer_start;
91 ret = qmc_chan_write_submit(prtd->qmc_dai->qmc_chan,
92 prtd->period_ptr_submitted, prtd->period_size,
93 qmc_audio_pcm_write_complete, prtd);
95 dev_err(prtd->qmc_dai->dev, "write_submit failed %d\n",
99 snd_pcm_period_elapsed(prtd->substream);
102 static void qmc_audio_pcm_read_complete(void *context, size_t length, unsigned int flags)
104 struct qmc_dai_prtd *prtd = context;
107 if (length != prtd->period_size) {
108 dev_err(prtd->qmc_dai->dev, "read complete length = %zu, exp %zu\n",
109 length, prtd->period_size);
112 prtd->period_ptr_ended += prtd->period_size;
113 if (prtd->period_ptr_ended >= prtd->dma_buffer_end)
114 prtd->period_ptr_ended = prtd->dma_buffer_start;
116 prtd->period_ptr_submitted += prtd->period_size;
117 if (prtd->period_ptr_submitted >= prtd->dma_buffer_end)
118 prtd->period_ptr_submitted = prtd->dma_buffer_start;
120 ret = qmc_chan_read_submit(prtd->qmc_dai->qmc_chan,
121 prtd->period_ptr_submitted, prtd->period_size,
122 qmc_audio_pcm_read_complete, prtd);
124 dev_err(prtd->qmc_dai->dev, "read_submit failed %d\n",
128 snd_pcm_period_elapsed(prtd->substream);
131 static int qmc_audio_pcm_trigger(struct snd_soc_component *component,
132 struct snd_pcm_substream *substream, int cmd)
134 struct qmc_dai_prtd *prtd = substream->runtime->private_data;
137 if (!prtd->qmc_dai) {
138 dev_err(component->dev, "qmc_dai is not set\n");
143 case SNDRV_PCM_TRIGGER_START:
144 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
145 /* Submit first chunk ... */
146 ret = qmc_chan_write_submit(prtd->qmc_dai->qmc_chan,
147 prtd->period_ptr_submitted, prtd->period_size,
148 qmc_audio_pcm_write_complete, prtd);
150 dev_err(component->dev, "write_submit failed %d\n",
155 /* ... prepare next one ... */
156 prtd->period_ptr_submitted += prtd->period_size;
157 if (prtd->period_ptr_submitted >= prtd->dma_buffer_end)
158 prtd->period_ptr_submitted = prtd->dma_buffer_start;
160 /* ... and send it */
161 ret = qmc_chan_write_submit(prtd->qmc_dai->qmc_chan,
162 prtd->period_ptr_submitted, prtd->period_size,
163 qmc_audio_pcm_write_complete, prtd);
165 dev_err(component->dev, "write_submit failed %d\n",
170 /* Submit first chunk ... */
171 ret = qmc_chan_read_submit(prtd->qmc_dai->qmc_chan,
172 prtd->period_ptr_submitted, prtd->period_size,
173 qmc_audio_pcm_read_complete, prtd);
175 dev_err(component->dev, "read_submit failed %d\n",
180 /* ... prepare next one ... */
181 prtd->period_ptr_submitted += prtd->period_size;
182 if (prtd->period_ptr_submitted >= prtd->dma_buffer_end)
183 prtd->period_ptr_submitted = prtd->dma_buffer_start;
185 /* ... and send it */
186 ret = qmc_chan_read_submit(prtd->qmc_dai->qmc_chan,
187 prtd->period_ptr_submitted, prtd->period_size,
188 qmc_audio_pcm_read_complete, prtd);
190 dev_err(component->dev, "write_submit failed %d\n",
197 case SNDRV_PCM_TRIGGER_RESUME:
198 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
201 case SNDRV_PCM_TRIGGER_STOP:
202 case SNDRV_PCM_TRIGGER_SUSPEND:
203 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
213 static snd_pcm_uframes_t qmc_audio_pcm_pointer(struct snd_soc_component *component,
214 struct snd_pcm_substream *substream)
216 struct qmc_dai_prtd *prtd = substream->runtime->private_data;
218 return bytes_to_frames(substream->runtime,
219 prtd->period_ptr_ended - prtd->dma_buffer_start);
222 static int qmc_audio_of_xlate_dai_name(struct snd_soc_component *component,
223 const struct of_phandle_args *args,
224 const char **dai_name)
226 struct qmc_audio *qmc_audio = dev_get_drvdata(component->dev);
227 struct snd_soc_dai_driver *dai_driver;
228 int id = args->args[0];
231 for (i = 0; i < qmc_audio->num_dais; i++) {
232 dai_driver = qmc_audio->dai_drivers + i;
233 if (dai_driver->id == id) {
234 *dai_name = dai_driver->name;
242 static const struct snd_pcm_hardware qmc_audio_pcm_hardware = {
243 .info = SNDRV_PCM_INFO_MMAP |
244 SNDRV_PCM_INFO_MMAP_VALID |
245 SNDRV_PCM_INFO_INTERLEAVED |
246 SNDRV_PCM_INFO_PAUSE,
247 .period_bytes_min = 32,
248 .period_bytes_max = 64*1024,
250 .periods_max = 2*1024,
251 .buffer_bytes_max = 64*1024,
254 static int qmc_audio_pcm_open(struct snd_soc_component *component,
255 struct snd_pcm_substream *substream)
257 struct snd_pcm_runtime *runtime = substream->runtime;
258 struct qmc_dai_prtd *prtd;
261 snd_soc_set_runtime_hwparams(substream, &qmc_audio_pcm_hardware);
263 /* ensure that buffer size is a multiple of period size */
264 ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
268 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
272 runtime->private_data = prtd;
277 static int qmc_audio_pcm_close(struct snd_soc_component *component,
278 struct snd_pcm_substream *substream)
280 struct qmc_dai_prtd *prtd = substream->runtime->private_data;
286 static const struct snd_soc_component_driver qmc_audio_soc_platform = {
287 .open = qmc_audio_pcm_open,
288 .close = qmc_audio_pcm_close,
289 .hw_params = qmc_audio_pcm_hw_params,
290 .trigger = qmc_audio_pcm_trigger,
291 .pointer = qmc_audio_pcm_pointer,
292 .pcm_construct = qmc_audio_pcm_construct,
293 .of_xlate_dai_name = qmc_audio_of_xlate_dai_name,
296 static unsigned int qmc_dai_get_index(struct snd_soc_dai *dai)
298 struct qmc_audio *qmc_audio = snd_soc_dai_get_drvdata(dai);
300 return dai->driver - qmc_audio->dai_drivers;
303 static struct qmc_dai *qmc_dai_get_data(struct snd_soc_dai *dai)
305 struct qmc_audio *qmc_audio = snd_soc_dai_get_drvdata(dai);
308 index = qmc_dai_get_index(dai);
309 if (index > qmc_audio->num_dais)
312 return qmc_audio->dais + index;
316 * The constraints for format/channel is to match with the number of 8bit
317 * time-slots available.
319 static int qmc_dai_hw_rule_channels_by_format(struct qmc_dai *qmc_dai,
320 struct snd_pcm_hw_params *params,
323 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
324 snd_pcm_format_t format = params_format(params);
325 struct snd_interval ch = {0};
327 switch (snd_pcm_format_physical_width(format)) {
341 dev_err(qmc_dai->dev, "format physical width %u not supported\n",
342 snd_pcm_format_physical_width(format));
346 ch.min = ch.max ? 1 : 0;
348 return snd_interval_refine(c, &ch);
351 static int qmc_dai_hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
352 struct snd_pcm_hw_rule *rule)
354 struct qmc_dai *qmc_dai = rule->private;
356 return qmc_dai_hw_rule_channels_by_format(qmc_dai, params, qmc_dai->nb_tx_ts);
359 static int qmc_dai_hw_rule_capture_channels_by_format(
360 struct snd_pcm_hw_params *params,
361 struct snd_pcm_hw_rule *rule)
363 struct qmc_dai *qmc_dai = rule->private;
365 return qmc_dai_hw_rule_channels_by_format(qmc_dai, params, qmc_dai->nb_rx_ts);
368 static int qmc_dai_hw_rule_format_by_channels(struct qmc_dai *qmc_dai,
369 struct snd_pcm_hw_params *params,
372 struct snd_mask *f_old = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
373 unsigned int channels = params_channels(params);
374 unsigned int slot_width;
375 snd_pcm_format_t format;
376 struct snd_mask f_new;
378 if (!channels || channels > nb_ts) {
379 dev_err(qmc_dai->dev, "channels %u not supported\n",
384 slot_width = (nb_ts / channels) * 8;
386 snd_mask_none(&f_new);
387 pcm_for_each_format(format) {
388 if (snd_mask_test_format(f_old, format)) {
389 if (snd_pcm_format_physical_width(format) <= slot_width)
390 snd_mask_set_format(&f_new, format);
394 return snd_mask_refine(f_old, &f_new);
397 static int qmc_dai_hw_rule_playback_format_by_channels(
398 struct snd_pcm_hw_params *params,
399 struct snd_pcm_hw_rule *rule)
401 struct qmc_dai *qmc_dai = rule->private;
403 return qmc_dai_hw_rule_format_by_channels(qmc_dai, params, qmc_dai->nb_tx_ts);
406 static int qmc_dai_hw_rule_capture_format_by_channels(
407 struct snd_pcm_hw_params *params,
408 struct snd_pcm_hw_rule *rule)
410 struct qmc_dai *qmc_dai = rule->private;
412 return qmc_dai_hw_rule_format_by_channels(qmc_dai, params, qmc_dai->nb_rx_ts);
415 static int qmc_dai_startup(struct snd_pcm_substream *substream,
416 struct snd_soc_dai *dai)
418 struct qmc_dai_prtd *prtd = substream->runtime->private_data;
419 snd_pcm_hw_rule_func_t hw_rule_channels_by_format;
420 snd_pcm_hw_rule_func_t hw_rule_format_by_channels;
421 struct qmc_dai *qmc_dai;
422 unsigned int frame_bits;
425 qmc_dai = qmc_dai_get_data(dai);
427 dev_err(dai->dev, "Invalid dai\n");
431 prtd->qmc_dai = qmc_dai;
433 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
434 hw_rule_channels_by_format = qmc_dai_hw_rule_capture_channels_by_format;
435 hw_rule_format_by_channels = qmc_dai_hw_rule_capture_format_by_channels;
436 frame_bits = qmc_dai->nb_rx_ts * 8;
438 hw_rule_channels_by_format = qmc_dai_hw_rule_playback_channels_by_format;
439 hw_rule_format_by_channels = qmc_dai_hw_rule_playback_format_by_channels;
440 frame_bits = qmc_dai->nb_tx_ts * 8;
443 ret = snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
444 hw_rule_channels_by_format, qmc_dai,
445 SNDRV_PCM_HW_PARAM_FORMAT, -1);
447 dev_err(dai->dev, "Failed to add channels rule (%d)\n", ret);
451 ret = snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
452 hw_rule_format_by_channels, qmc_dai,
453 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
455 dev_err(dai->dev, "Failed to add format rule (%d)\n", ret);
459 ret = snd_pcm_hw_constraint_single(substream->runtime,
460 SNDRV_PCM_HW_PARAM_FRAME_BITS,
463 dev_err(dai->dev, "Failed to add frame_bits constraint (%d)\n", ret);
470 static int qmc_dai_hw_params(struct snd_pcm_substream *substream,
471 struct snd_pcm_hw_params *params,
472 struct snd_soc_dai *dai)
474 struct qmc_chan_param chan_param = {0};
475 struct qmc_dai *qmc_dai;
478 qmc_dai = qmc_dai_get_data(dai);
480 dev_err(dai->dev, "Invalid dai\n");
484 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
485 chan_param.mode = QMC_TRANSPARENT;
486 chan_param.transp.max_rx_buf_size = params_period_bytes(params);
487 ret = qmc_chan_set_param(qmc_dai->qmc_chan, &chan_param);
489 dev_err(dai->dev, "set param failed %d\n",
498 static int qmc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
499 struct snd_soc_dai *dai)
501 struct qmc_dai *qmc_dai;
505 qmc_dai = qmc_dai_get_data(dai);
507 dev_err(dai->dev, "Invalid dai\n");
511 direction = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
512 QMC_CHAN_WRITE : QMC_CHAN_READ;
515 case SNDRV_PCM_TRIGGER_START:
516 case SNDRV_PCM_TRIGGER_RESUME:
517 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
518 ret = qmc_chan_start(qmc_dai->qmc_chan, direction);
523 case SNDRV_PCM_TRIGGER_STOP:
524 ret = qmc_chan_stop(qmc_dai->qmc_chan, direction);
527 ret = qmc_chan_reset(qmc_dai->qmc_chan, direction);
532 case SNDRV_PCM_TRIGGER_SUSPEND:
533 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
534 ret = qmc_chan_stop(qmc_dai->qmc_chan, direction);
546 static const struct snd_soc_dai_ops qmc_dai_ops = {
547 .startup = qmc_dai_startup,
548 .trigger = qmc_dai_trigger,
549 .hw_params = qmc_dai_hw_params,
552 static u64 qmc_audio_formats(u8 nb_ts)
554 unsigned int format_width;
555 unsigned int chan_width;
556 snd_pcm_format_t format;
563 chan_width = nb_ts * 8;
564 pcm_for_each_format(format) {
566 * Support format other than little-endian (ie big-endian or
567 * without endianness such as 8bit formats)
569 if (snd_pcm_format_little_endian(format) == 1)
572 /* Support physical width multiple of 8bit */
573 format_width = snd_pcm_format_physical_width(format);
574 if (format_width == 0 || format_width % 8)
578 * And support physical width that can fit N times in the
581 if (format_width > chan_width || chan_width % format_width)
584 formats_mask |= pcm_format_to_bits(format);
589 static int qmc_audio_dai_parse(struct qmc_audio *qmc_audio, struct device_node *np,
590 struct qmc_dai *qmc_dai, struct snd_soc_dai_driver *qmc_soc_dai_driver)
592 struct qmc_chan_info info;
596 qmc_dai->dev = qmc_audio->dev;
598 ret = of_property_read_u32(np, "reg", &val);
600 dev_err(qmc_audio->dev, "%pOF: failed to read reg\n", np);
605 qmc_dai->name = devm_kasprintf(qmc_audio->dev, GFP_KERNEL, "%s.%d",
606 np->parent->name, qmc_dai->id);
608 qmc_dai->qmc_chan = devm_qmc_chan_get_byphandle(qmc_audio->dev, np,
610 if (IS_ERR(qmc_dai->qmc_chan)) {
611 ret = PTR_ERR(qmc_dai->qmc_chan);
612 return dev_err_probe(qmc_audio->dev, ret,
613 "dai %d get QMC channel failed\n", qmc_dai->id);
616 qmc_soc_dai_driver->id = qmc_dai->id;
617 qmc_soc_dai_driver->name = qmc_dai->name;
619 ret = qmc_chan_get_info(qmc_dai->qmc_chan, &info);
621 dev_err(qmc_audio->dev, "dai %d get QMC channel info failed %d\n",
625 dev_info(qmc_audio->dev, "dai %d QMC channel mode %d, nb_tx_ts %u, nb_rx_ts %u\n",
626 qmc_dai->id, info.mode, info.nb_tx_ts, info.nb_rx_ts);
628 if (info.mode != QMC_TRANSPARENT) {
629 dev_err(qmc_audio->dev, "dai %d QMC chan mode %d is not QMC_TRANSPARENT\n",
630 qmc_dai->id, info.mode);
633 qmc_dai->nb_tx_ts = info.nb_tx_ts;
634 qmc_dai->nb_rx_ts = info.nb_rx_ts;
636 qmc_soc_dai_driver->playback.channels_min = 0;
637 qmc_soc_dai_driver->playback.channels_max = 0;
638 if (qmc_dai->nb_tx_ts) {
639 qmc_soc_dai_driver->playback.channels_min = 1;
640 qmc_soc_dai_driver->playback.channels_max = qmc_dai->nb_tx_ts;
642 qmc_soc_dai_driver->playback.formats = qmc_audio_formats(qmc_dai->nb_tx_ts);
644 qmc_soc_dai_driver->capture.channels_min = 0;
645 qmc_soc_dai_driver->capture.channels_max = 0;
646 if (qmc_dai->nb_rx_ts) {
647 qmc_soc_dai_driver->capture.channels_min = 1;
648 qmc_soc_dai_driver->capture.channels_max = qmc_dai->nb_rx_ts;
650 qmc_soc_dai_driver->capture.formats = qmc_audio_formats(qmc_dai->nb_rx_ts);
652 qmc_soc_dai_driver->playback.rates = snd_pcm_rate_to_rate_bit(info.tx_fs_rate);
653 qmc_soc_dai_driver->playback.rate_min = info.tx_fs_rate;
654 qmc_soc_dai_driver->playback.rate_max = info.tx_fs_rate;
655 qmc_soc_dai_driver->capture.rates = snd_pcm_rate_to_rate_bit(info.rx_fs_rate);
656 qmc_soc_dai_driver->capture.rate_min = info.rx_fs_rate;
657 qmc_soc_dai_driver->capture.rate_max = info.rx_fs_rate;
659 qmc_soc_dai_driver->ops = &qmc_dai_ops;
664 static int qmc_audio_probe(struct platform_device *pdev)
666 struct device_node *np = pdev->dev.of_node;
667 struct qmc_audio *qmc_audio;
668 struct device_node *child;
672 qmc_audio = devm_kzalloc(&pdev->dev, sizeof(*qmc_audio), GFP_KERNEL);
676 qmc_audio->dev = &pdev->dev;
678 qmc_audio->num_dais = of_get_available_child_count(np);
679 if (qmc_audio->num_dais) {
680 qmc_audio->dais = devm_kcalloc(&pdev->dev, qmc_audio->num_dais,
681 sizeof(*qmc_audio->dais),
683 if (!qmc_audio->dais)
686 qmc_audio->dai_drivers = devm_kcalloc(&pdev->dev, qmc_audio->num_dais,
687 sizeof(*qmc_audio->dai_drivers),
689 if (!qmc_audio->dai_drivers)
694 for_each_available_child_of_node(np, child) {
695 ret = qmc_audio_dai_parse(qmc_audio, child,
697 qmc_audio->dai_drivers + i);
706 platform_set_drvdata(pdev, qmc_audio);
708 ret = devm_snd_soc_register_component(qmc_audio->dev,
709 &qmc_audio_soc_platform,
710 qmc_audio->dai_drivers,
711 qmc_audio->num_dais);
718 static const struct of_device_id qmc_audio_id_table[] = {
719 { .compatible = "fsl,qmc-audio" },
722 MODULE_DEVICE_TABLE(of, qmc_audio_id_table);
724 static struct platform_driver qmc_audio_driver = {
726 .name = "fsl-qmc-audio",
727 .of_match_table = of_match_ptr(qmc_audio_id_table),
729 .probe = qmc_audio_probe,
731 module_platform_driver(qmc_audio_driver);
733 MODULE_AUTHOR("Herve Codina <herve.codina@bootlin.com>");
734 MODULE_DESCRIPTION("CPM/QE QMC audio driver");
735 MODULE_LICENSE("GPL");