1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
3 // Copyright (c) 2018, Linaro Limited
5 #include <dt-bindings/sound/qcom,q6asm.h>
6 #include <linux/init.h>
8 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/slab.h>
12 #include <sound/soc.h>
13 #include <sound/soc-dapm.h>
14 #include <sound/pcm.h>
15 #include <linux/spinlock.h>
16 #include <sound/compress_driver.h>
18 #include <linux/dma-mapping.h>
19 #include <sound/pcm_params.h>
21 #include "q6routing.h"
22 #include "q6dsp-errno.h"
24 #define DRV_NAME "q6asm-fe-dai"
26 #define PLAYBACK_MIN_NUM_PERIODS 2
27 #define PLAYBACK_MAX_NUM_PERIODS 8
28 #define PLAYBACK_MAX_PERIOD_SIZE 65536
29 #define PLAYBACK_MIN_PERIOD_SIZE 128
30 #define CAPTURE_MIN_NUM_PERIODS 2
31 #define CAPTURE_MAX_NUM_PERIODS 8
32 #define CAPTURE_MAX_PERIOD_SIZE 4096
33 #define CAPTURE_MIN_PERIOD_SIZE 320
34 #define SID_MASK_DEFAULT 0xF
36 /* Default values used if user space does not set */
37 #define COMPR_PLAYBACK_MIN_FRAGMENT_SIZE (8 * 1024)
38 #define COMPR_PLAYBACK_MAX_FRAGMENT_SIZE (128 * 1024)
39 #define COMPR_PLAYBACK_MIN_NUM_FRAGMENTS (4)
40 #define COMPR_PLAYBACK_MAX_NUM_FRAGMENTS (16 * 4)
42 #define ALAC_CH_LAYOUT_MONO ((101 << 16) | 1)
43 #define ALAC_CH_LAYOUT_STEREO ((101 << 16) | 2)
46 Q6ASM_STREAM_IDLE = 0,
51 struct q6asm_dai_rtd {
52 struct snd_pcm_substream *substream;
53 struct snd_compr_stream *cstream;
54 struct snd_codec codec;
55 struct snd_dma_buffer dma_buffer;
58 unsigned int pcm_size;
59 unsigned int pcm_count;
60 unsigned int pcm_irq_pos; /* IRQ position */
62 unsigned int bytes_sent;
63 unsigned int bytes_received;
64 unsigned int copied_total;
65 uint16_t bits_per_sample;
66 uint16_t source; /* Encoding source bit mask */
67 struct audio_client *audio_client;
68 uint32_t next_track_stream_id;
72 enum stream_state state;
73 uint32_t initial_samples_drop;
74 uint32_t trailing_samples_drop;
78 struct q6asm_dai_data {
79 struct snd_soc_dai_driver *dais;
84 static const struct snd_pcm_hardware q6asm_dai_hardware_capture = {
85 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BATCH |
86 SNDRV_PCM_INFO_BLOCK_TRANSFER |
87 SNDRV_PCM_INFO_MMAP_VALID |
88 SNDRV_PCM_INFO_INTERLEAVED |
89 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
90 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
91 SNDRV_PCM_FMTBIT_S24_LE),
92 .rates = SNDRV_PCM_RATE_8000_48000,
97 .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS *
98 CAPTURE_MAX_PERIOD_SIZE,
99 .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
100 .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
101 .periods_min = CAPTURE_MIN_NUM_PERIODS,
102 .periods_max = CAPTURE_MAX_NUM_PERIODS,
106 static struct snd_pcm_hardware q6asm_dai_hardware_playback = {
107 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BATCH |
108 SNDRV_PCM_INFO_BLOCK_TRANSFER |
109 SNDRV_PCM_INFO_MMAP_VALID |
110 SNDRV_PCM_INFO_INTERLEAVED |
111 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
112 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
113 SNDRV_PCM_FMTBIT_S24_LE),
114 .rates = SNDRV_PCM_RATE_8000_192000,
119 .buffer_bytes_max = (PLAYBACK_MAX_NUM_PERIODS *
120 PLAYBACK_MAX_PERIOD_SIZE),
121 .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
122 .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
123 .periods_min = PLAYBACK_MIN_NUM_PERIODS,
124 .periods_max = PLAYBACK_MAX_NUM_PERIODS,
128 #define Q6ASM_FEDAI_DRIVER(num) { \
130 .stream_name = "MultiMedia"#num" Playback", \
131 .rates = (SNDRV_PCM_RATE_8000_192000| \
132 SNDRV_PCM_RATE_KNOT), \
133 .formats = (SNDRV_PCM_FMTBIT_S16_LE | \
134 SNDRV_PCM_FMTBIT_S24_LE), \
138 .rate_max = 192000, \
141 .stream_name = "MultiMedia"#num" Capture", \
142 .rates = (SNDRV_PCM_RATE_8000_48000| \
143 SNDRV_PCM_RATE_KNOT), \
144 .formats = (SNDRV_PCM_FMTBIT_S16_LE | \
145 SNDRV_PCM_FMTBIT_S24_LE), \
151 .name = "MultiMedia"#num, \
152 .id = MSM_FRONTEND_DAI_MULTIMEDIA##num, \
155 /* Conventional and unconventional sample rate supported */
156 static unsigned int supported_sample_rates[] = {
157 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
158 88200, 96000, 176400, 192000
161 static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
162 .count = ARRAY_SIZE(supported_sample_rates),
163 .list = supported_sample_rates,
167 static const struct snd_compr_codec_caps q6asm_compr_caps = {
168 .num_descriptors = 1,
169 .descriptor[0].max_ch = 2,
170 .descriptor[0].sample_rates = { 8000, 11025, 12000, 16000, 22050,
171 24000, 32000, 44100, 48000, 88200,
172 96000, 176400, 192000 },
173 .descriptor[0].num_sample_rates = 13,
174 .descriptor[0].bit_rate[0] = 320,
175 .descriptor[0].bit_rate[1] = 128,
176 .descriptor[0].num_bitrates = 2,
177 .descriptor[0].profiles = 0,
178 .descriptor[0].modes = SND_AUDIOCHANMODE_MP3_STEREO,
179 .descriptor[0].formats = 0,
182 static void event_handler(uint32_t opcode, uint32_t token,
183 void *payload, void *priv)
185 struct q6asm_dai_rtd *prtd = priv;
186 struct snd_pcm_substream *substream = prtd->substream;
189 case ASM_CLIENT_EVENT_CMD_RUN_DONE:
190 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
191 q6asm_write_async(prtd->audio_client, prtd->stream_id,
192 prtd->pcm_count, 0, 0, 0);
194 case ASM_CLIENT_EVENT_CMD_EOS_DONE:
195 prtd->state = Q6ASM_STREAM_STOPPED;
197 case ASM_CLIENT_EVENT_DATA_WRITE_DONE: {
198 prtd->pcm_irq_pos += prtd->pcm_count;
199 snd_pcm_period_elapsed(substream);
200 if (prtd->state == Q6ASM_STREAM_RUNNING)
201 q6asm_write_async(prtd->audio_client, prtd->stream_id,
202 prtd->pcm_count, 0, 0, 0);
206 case ASM_CLIENT_EVENT_DATA_READ_DONE:
207 prtd->pcm_irq_pos += prtd->pcm_count;
208 snd_pcm_period_elapsed(substream);
209 if (prtd->state == Q6ASM_STREAM_RUNNING)
210 q6asm_read(prtd->audio_client, prtd->stream_id);
218 static int q6asm_dai_prepare(struct snd_soc_component *component,
219 struct snd_pcm_substream *substream)
221 struct snd_pcm_runtime *runtime = substream->runtime;
222 struct snd_soc_pcm_runtime *soc_prtd = snd_soc_substream_to_rtd(substream);
223 struct q6asm_dai_rtd *prtd = runtime->private_data;
224 struct q6asm_dai_data *pdata;
225 struct device *dev = component->dev;
228 pdata = snd_soc_component_get_drvdata(component);
232 if (!prtd || !prtd->audio_client) {
233 dev_err(dev, "%s: private data null or audio client freed\n",
238 prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
239 prtd->pcm_irq_pos = 0;
240 /* rate and channels are sent to audio driver */
242 /* clear the previous setup if any */
243 q6asm_cmd(prtd->audio_client, prtd->stream_id, CMD_CLOSE);
244 q6asm_unmap_memory_regions(substream->stream,
246 q6routing_stream_close(soc_prtd->dai_link->id,
250 ret = q6asm_map_memory_regions(substream->stream, prtd->audio_client,
252 (prtd->pcm_size / prtd->periods),
256 dev_err(dev, "Audio Start: Buffer Allocation failed rc = %d\n",
261 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
262 ret = q6asm_open_write(prtd->audio_client, prtd->stream_id,
264 0, prtd->bits_per_sample, false);
265 } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
266 ret = q6asm_open_read(prtd->audio_client, prtd->stream_id,
268 prtd->bits_per_sample);
272 dev_err(dev, "%s: q6asm_open_write failed\n", __func__);
276 prtd->session_id = q6asm_get_session_id(prtd->audio_client);
277 ret = q6routing_stream_open(soc_prtd->dai_link->id, LEGACY_PCM_MODE,
278 prtd->session_id, substream->stream);
280 dev_err(dev, "%s: stream reg failed ret:%d\n", __func__, ret);
284 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
285 ret = q6asm_media_format_block_multi_ch_pcm(
286 prtd->audio_client, prtd->stream_id,
287 runtime->rate, runtime->channels, NULL,
288 prtd->bits_per_sample);
289 } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
290 ret = q6asm_enc_cfg_blk_pcm_format_support(prtd->audio_client,
294 prtd->bits_per_sample);
296 /* Queue the buffers */
297 for (i = 0; i < runtime->periods; i++)
298 q6asm_read(prtd->audio_client, prtd->stream_id);
302 dev_info(dev, "%s: CMD Format block failed\n", __func__);
304 prtd->state = Q6ASM_STREAM_RUNNING;
309 q6asm_cmd(prtd->audio_client, prtd->stream_id, CMD_CLOSE);
311 q6asm_unmap_memory_regions(substream->stream, prtd->audio_client);
312 q6asm_audio_client_free(prtd->audio_client);
313 prtd->audio_client = NULL;
318 static int q6asm_dai_trigger(struct snd_soc_component *component,
319 struct snd_pcm_substream *substream, int cmd)
322 struct snd_pcm_runtime *runtime = substream->runtime;
323 struct q6asm_dai_rtd *prtd = runtime->private_data;
326 case SNDRV_PCM_TRIGGER_START:
327 case SNDRV_PCM_TRIGGER_RESUME:
328 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
329 ret = q6asm_run_nowait(prtd->audio_client, prtd->stream_id,
332 case SNDRV_PCM_TRIGGER_STOP:
333 prtd->state = Q6ASM_STREAM_STOPPED;
334 ret = q6asm_cmd_nowait(prtd->audio_client, prtd->stream_id,
337 case SNDRV_PCM_TRIGGER_SUSPEND:
338 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
339 ret = q6asm_cmd_nowait(prtd->audio_client, prtd->stream_id,
350 static int q6asm_dai_open(struct snd_soc_component *component,
351 struct snd_pcm_substream *substream)
353 struct snd_pcm_runtime *runtime = substream->runtime;
354 struct snd_soc_pcm_runtime *soc_prtd = snd_soc_substream_to_rtd(substream);
355 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(soc_prtd, 0);
356 struct q6asm_dai_rtd *prtd;
357 struct q6asm_dai_data *pdata;
358 struct device *dev = component->dev;
362 stream_id = cpu_dai->driver->id;
364 pdata = snd_soc_component_get_drvdata(component);
366 dev_err(dev, "Drv data not found ..\n");
370 prtd = kzalloc(sizeof(struct q6asm_dai_rtd), GFP_KERNEL);
374 prtd->substream = substream;
375 prtd->audio_client = q6asm_audio_client_alloc(dev,
376 (q6asm_cb)event_handler, prtd, stream_id,
378 if (IS_ERR(prtd->audio_client)) {
379 dev_info(dev, "%s: Could not allocate memory\n", __func__);
380 ret = PTR_ERR(prtd->audio_client);
385 /* DSP expects stream id from 1 */
388 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
389 runtime->hw = q6asm_dai_hardware_playback;
390 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
391 runtime->hw = q6asm_dai_hardware_capture;
393 ret = snd_pcm_hw_constraint_list(runtime, 0,
394 SNDRV_PCM_HW_PARAM_RATE,
395 &constraints_sample_rates);
397 dev_info(dev, "snd_pcm_hw_constraint_list failed\n");
398 /* Ensure that buffer size is a multiple of period size */
399 ret = snd_pcm_hw_constraint_integer(runtime,
400 SNDRV_PCM_HW_PARAM_PERIODS);
402 dev_info(dev, "snd_pcm_hw_constraint_integer failed\n");
404 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
405 ret = snd_pcm_hw_constraint_minmax(runtime,
406 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
407 PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE,
408 PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE);
410 dev_err(dev, "constraint for buffer bytes min max ret = %d\n",
415 ret = snd_pcm_hw_constraint_step(runtime, 0,
416 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
418 dev_err(dev, "constraint for period bytes step ret = %d\n",
421 ret = snd_pcm_hw_constraint_step(runtime, 0,
422 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
424 dev_err(dev, "constraint for buffer bytes step ret = %d\n",
428 runtime->private_data = prtd;
430 snd_soc_set_runtime_hwparams(substream, &q6asm_dai_hardware_playback);
432 runtime->dma_bytes = q6asm_dai_hardware_playback.buffer_bytes_max;
436 prtd->phys = substream->dma_buffer.addr;
438 prtd->phys = substream->dma_buffer.addr | (pdata->sid << 32);
443 static int q6asm_dai_close(struct snd_soc_component *component,
444 struct snd_pcm_substream *substream)
446 struct snd_pcm_runtime *runtime = substream->runtime;
447 struct snd_soc_pcm_runtime *soc_prtd = snd_soc_substream_to_rtd(substream);
448 struct q6asm_dai_rtd *prtd = runtime->private_data;
450 if (prtd->audio_client) {
452 q6asm_cmd(prtd->audio_client, prtd->stream_id,
455 q6asm_unmap_memory_regions(substream->stream,
457 q6asm_audio_client_free(prtd->audio_client);
458 prtd->audio_client = NULL;
460 q6routing_stream_close(soc_prtd->dai_link->id,
466 static snd_pcm_uframes_t q6asm_dai_pointer(struct snd_soc_component *component,
467 struct snd_pcm_substream *substream)
470 struct snd_pcm_runtime *runtime = substream->runtime;
471 struct q6asm_dai_rtd *prtd = runtime->private_data;
473 if (prtd->pcm_irq_pos >= prtd->pcm_size)
474 prtd->pcm_irq_pos = 0;
476 return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
479 static int q6asm_dai_hw_params(struct snd_soc_component *component,
480 struct snd_pcm_substream *substream,
481 struct snd_pcm_hw_params *params)
483 struct snd_pcm_runtime *runtime = substream->runtime;
484 struct q6asm_dai_rtd *prtd = runtime->private_data;
486 prtd->pcm_size = params_buffer_bytes(params);
487 prtd->periods = params_periods(params);
489 switch (params_format(params)) {
490 case SNDRV_PCM_FORMAT_S16_LE:
491 prtd->bits_per_sample = 16;
493 case SNDRV_PCM_FORMAT_S24_LE:
494 prtd->bits_per_sample = 24;
501 static void compress_event_handler(uint32_t opcode, uint32_t token,
502 void *payload, void *priv)
504 struct q6asm_dai_rtd *prtd = priv;
505 struct snd_compr_stream *substream = prtd->cstream;
509 uint32_t bytes_written, bytes_to_write;
510 bool is_last_buffer = false;
513 case ASM_CLIENT_EVENT_CMD_RUN_DONE:
514 spin_lock_irqsave(&prtd->lock, flags);
515 if (!prtd->bytes_sent) {
516 q6asm_stream_remove_initial_silence(prtd->audio_client,
518 prtd->initial_samples_drop);
520 q6asm_write_async(prtd->audio_client, prtd->stream_id,
521 prtd->pcm_count, 0, 0, 0);
522 prtd->bytes_sent += prtd->pcm_count;
525 spin_unlock_irqrestore(&prtd->lock, flags);
528 case ASM_CLIENT_EVENT_CMD_EOS_DONE:
529 spin_lock_irqsave(&prtd->lock, flags);
530 if (prtd->notify_on_drain) {
531 if (substream->partial_drain) {
533 * Close old stream and make it stale, switch
534 * the active stream now!
536 q6asm_cmd_nowait(prtd->audio_client,
540 * vaild stream ids start from 1, So we are
541 * toggling this between 1 and 2.
543 prtd->stream_id = (prtd->stream_id == 1 ? 2 : 1);
546 snd_compr_drain_notify(prtd->cstream);
547 prtd->notify_on_drain = false;
550 prtd->state = Q6ASM_STREAM_STOPPED;
552 spin_unlock_irqrestore(&prtd->lock, flags);
555 case ASM_CLIENT_EVENT_DATA_WRITE_DONE:
556 spin_lock_irqsave(&prtd->lock, flags);
558 bytes_written = token >> ASM_WRITE_TOKEN_LEN_SHIFT;
559 prtd->copied_total += bytes_written;
560 snd_compr_fragment_elapsed(substream);
562 if (prtd->state != Q6ASM_STREAM_RUNNING) {
563 spin_unlock_irqrestore(&prtd->lock, flags);
567 avail = prtd->bytes_received - prtd->bytes_sent;
568 if (avail > prtd->pcm_count) {
569 bytes_to_write = prtd->pcm_count;
571 if (substream->partial_drain || prtd->notify_on_drain)
572 is_last_buffer = true;
573 bytes_to_write = avail;
576 if (bytes_to_write) {
577 if (substream->partial_drain && is_last_buffer) {
578 wflags |= ASM_LAST_BUFFER_FLAG;
579 q6asm_stream_remove_trailing_silence(prtd->audio_client,
581 prtd->trailing_samples_drop);
584 q6asm_write_async(prtd->audio_client, prtd->stream_id,
585 bytes_to_write, 0, 0, wflags);
587 prtd->bytes_sent += bytes_to_write;
590 if (prtd->notify_on_drain && is_last_buffer)
591 q6asm_cmd_nowait(prtd->audio_client,
592 prtd->stream_id, CMD_EOS);
594 spin_unlock_irqrestore(&prtd->lock, flags);
602 static int q6asm_dai_compr_open(struct snd_soc_component *component,
603 struct snd_compr_stream *stream)
605 struct snd_soc_pcm_runtime *rtd = stream->private_data;
606 struct snd_compr_runtime *runtime = stream->runtime;
607 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
608 struct q6asm_dai_data *pdata;
609 struct device *dev = component->dev;
610 struct q6asm_dai_rtd *prtd;
611 int stream_id, size, ret;
613 stream_id = cpu_dai->driver->id;
614 pdata = snd_soc_component_get_drvdata(component);
616 dev_err(dev, "Drv data not found ..\n");
620 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
624 /* DSP expects stream id from 1 */
627 prtd->cstream = stream;
628 prtd->audio_client = q6asm_audio_client_alloc(dev,
629 (q6asm_cb)compress_event_handler,
630 prtd, stream_id, LEGACY_PCM_MODE);
631 if (IS_ERR(prtd->audio_client)) {
632 dev_err(dev, "Could not allocate memory\n");
633 ret = PTR_ERR(prtd->audio_client);
637 size = COMPR_PLAYBACK_MAX_FRAGMENT_SIZE *
638 COMPR_PLAYBACK_MAX_NUM_FRAGMENTS;
639 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size,
642 dev_err(dev, "Cannot allocate buffer(s)\n");
647 prtd->phys = prtd->dma_buffer.addr;
649 prtd->phys = prtd->dma_buffer.addr | (pdata->sid << 32);
651 snd_compr_set_runtime_buffer(stream, &prtd->dma_buffer);
652 spin_lock_init(&prtd->lock);
653 runtime->private_data = prtd;
658 q6asm_audio_client_free(prtd->audio_client);
665 static int q6asm_dai_compr_free(struct snd_soc_component *component,
666 struct snd_compr_stream *stream)
668 struct snd_compr_runtime *runtime = stream->runtime;
669 struct q6asm_dai_rtd *prtd = runtime->private_data;
670 struct snd_soc_pcm_runtime *rtd = stream->private_data;
672 if (prtd->audio_client) {
674 q6asm_cmd(prtd->audio_client, prtd->stream_id,
676 if (prtd->next_track_stream_id) {
677 q6asm_cmd(prtd->audio_client,
678 prtd->next_track_stream_id,
683 snd_dma_free_pages(&prtd->dma_buffer);
684 q6asm_unmap_memory_regions(stream->direction,
686 q6asm_audio_client_free(prtd->audio_client);
687 prtd->audio_client = NULL;
689 q6routing_stream_close(rtd->dai_link->id, stream->direction);
695 static int __q6asm_dai_compr_set_codec_params(struct snd_soc_component *component,
696 struct snd_compr_stream *stream,
697 struct snd_codec *codec,
700 struct snd_compr_runtime *runtime = stream->runtime;
701 struct q6asm_dai_rtd *prtd = runtime->private_data;
702 struct q6asm_flac_cfg flac_cfg;
703 struct q6asm_wma_cfg wma_cfg;
704 struct q6asm_alac_cfg alac_cfg;
705 struct q6asm_ape_cfg ape_cfg;
706 unsigned int wma_v9 = 0;
707 struct device *dev = component->dev;
709 union snd_codec_options *codec_options;
710 struct snd_dec_flac *flac;
711 struct snd_dec_wma *wma;
712 struct snd_dec_alac *alac;
713 struct snd_dec_ape *ape;
715 codec_options = &(prtd->codec.options);
717 memcpy(&prtd->codec, codec, sizeof(*codec));
720 case SND_AUDIOCODEC_FLAC:
722 memset(&flac_cfg, 0x0, sizeof(struct q6asm_flac_cfg));
723 flac = &codec_options->flac_d;
725 flac_cfg.ch_cfg = codec->ch_in;
726 flac_cfg.sample_rate = codec->sample_rate;
727 flac_cfg.stream_info_present = 1;
728 flac_cfg.sample_size = flac->sample_size;
729 flac_cfg.min_blk_size = flac->min_blk_size;
730 flac_cfg.max_blk_size = flac->max_blk_size;
731 flac_cfg.max_frame_size = flac->max_frame_size;
732 flac_cfg.min_frame_size = flac->min_frame_size;
734 ret = q6asm_stream_media_format_block_flac(prtd->audio_client,
738 dev_err(dev, "FLAC CMD Format block failed:%d\n", ret);
743 case SND_AUDIOCODEC_WMA:
744 wma = &codec_options->wma_d;
746 memset(&wma_cfg, 0x0, sizeof(struct q6asm_wma_cfg));
748 wma_cfg.sample_rate = codec->sample_rate;
749 wma_cfg.num_channels = codec->ch_in;
750 wma_cfg.bytes_per_sec = codec->bit_rate / 8;
751 wma_cfg.block_align = codec->align;
752 wma_cfg.bits_per_sample = prtd->bits_per_sample;
753 wma_cfg.enc_options = wma->encoder_option;
754 wma_cfg.adv_enc_options = wma->adv_encoder_option;
755 wma_cfg.adv_enc_options2 = wma->adv_encoder_option2;
757 if (wma_cfg.num_channels == 1)
758 wma_cfg.channel_mask = 4; /* Mono Center */
759 else if (wma_cfg.num_channels == 2)
760 wma_cfg.channel_mask = 3; /* Stereo FL/FR */
764 /* check the codec profile */
765 switch (codec->profile) {
766 case SND_AUDIOPROFILE_WMA9:
767 wma_cfg.fmtag = 0x161;
771 case SND_AUDIOPROFILE_WMA10:
772 wma_cfg.fmtag = 0x166;
775 case SND_AUDIOPROFILE_WMA9_PRO:
776 wma_cfg.fmtag = 0x162;
779 case SND_AUDIOPROFILE_WMA9_LOSSLESS:
780 wma_cfg.fmtag = 0x163;
783 case SND_AUDIOPROFILE_WMA10_LOSSLESS:
784 wma_cfg.fmtag = 0x167;
788 dev_err(dev, "Unknown WMA profile:%x\n",
794 ret = q6asm_stream_media_format_block_wma_v9(
795 prtd->audio_client, stream_id,
798 ret = q6asm_stream_media_format_block_wma_v10(
799 prtd->audio_client, stream_id,
802 dev_err(dev, "WMA9 CMD failed:%d\n", ret);
807 case SND_AUDIOCODEC_ALAC:
808 memset(&alac_cfg, 0x0, sizeof(alac_cfg));
809 alac = &codec_options->alac_d;
811 alac_cfg.sample_rate = codec->sample_rate;
812 alac_cfg.avg_bit_rate = codec->bit_rate;
813 alac_cfg.bit_depth = prtd->bits_per_sample;
814 alac_cfg.num_channels = codec->ch_in;
816 alac_cfg.frame_length = alac->frame_length;
817 alac_cfg.pb = alac->pb;
818 alac_cfg.mb = alac->mb;
819 alac_cfg.kb = alac->kb;
820 alac_cfg.max_run = alac->max_run;
821 alac_cfg.compatible_version = alac->compatible_version;
822 alac_cfg.max_frame_bytes = alac->max_frame_bytes;
824 switch (codec->ch_in) {
826 alac_cfg.channel_layout_tag = ALAC_CH_LAYOUT_MONO;
829 alac_cfg.channel_layout_tag = ALAC_CH_LAYOUT_STEREO;
832 ret = q6asm_stream_media_format_block_alac(prtd->audio_client,
836 dev_err(dev, "ALAC CMD Format block failed:%d\n", ret);
841 case SND_AUDIOCODEC_APE:
842 memset(&ape_cfg, 0x0, sizeof(ape_cfg));
843 ape = &codec_options->ape_d;
845 ape_cfg.sample_rate = codec->sample_rate;
846 ape_cfg.num_channels = codec->ch_in;
847 ape_cfg.bits_per_sample = prtd->bits_per_sample;
849 ape_cfg.compatible_version = ape->compatible_version;
850 ape_cfg.compression_level = ape->compression_level;
851 ape_cfg.format_flags = ape->format_flags;
852 ape_cfg.blocks_per_frame = ape->blocks_per_frame;
853 ape_cfg.final_frame_blocks = ape->final_frame_blocks;
854 ape_cfg.total_frames = ape->total_frames;
855 ape_cfg.seek_table_present = ape->seek_table_present;
857 ret = q6asm_stream_media_format_block_ape(prtd->audio_client,
861 dev_err(dev, "APE CMD Format block failed:%d\n", ret);
873 static int q6asm_dai_compr_set_params(struct snd_soc_component *component,
874 struct snd_compr_stream *stream,
875 struct snd_compr_params *params)
877 struct snd_compr_runtime *runtime = stream->runtime;
878 struct q6asm_dai_rtd *prtd = runtime->private_data;
879 struct snd_soc_pcm_runtime *rtd = stream->private_data;
880 int dir = stream->direction;
881 struct q6asm_dai_data *pdata;
882 struct device *dev = component->dev;
885 pdata = snd_soc_component_get_drvdata(component);
889 if (!prtd || !prtd->audio_client) {
890 dev_err(dev, "private data null or audio client freed\n");
894 prtd->periods = runtime->fragments;
895 prtd->pcm_count = runtime->fragment_size;
896 prtd->pcm_size = runtime->fragments * runtime->fragment_size;
897 prtd->bits_per_sample = 16;
899 if (dir == SND_COMPRESS_PLAYBACK) {
900 ret = q6asm_open_write(prtd->audio_client, prtd->stream_id, params->codec.id,
901 params->codec.profile, prtd->bits_per_sample,
905 dev_err(dev, "q6asm_open_write failed\n");
906 q6asm_audio_client_free(prtd->audio_client);
907 prtd->audio_client = NULL;
912 prtd->session_id = q6asm_get_session_id(prtd->audio_client);
913 ret = q6routing_stream_open(rtd->dai_link->id, LEGACY_PCM_MODE,
914 prtd->session_id, dir);
916 dev_err(dev, "Stream reg failed ret:%d\n", ret);
920 ret = __q6asm_dai_compr_set_codec_params(component, stream,
924 dev_err(dev, "codec param setup failed ret:%d\n", ret);
928 ret = q6asm_map_memory_regions(dir, prtd->audio_client, prtd->phys,
929 (prtd->pcm_size / prtd->periods),
933 dev_err(dev, "Buffer Mapping failed ret:%d\n", ret);
937 prtd->state = Q6ASM_STREAM_RUNNING;
942 static int q6asm_dai_compr_set_metadata(struct snd_soc_component *component,
943 struct snd_compr_stream *stream,
944 struct snd_compr_metadata *metadata)
946 struct snd_compr_runtime *runtime = stream->runtime;
947 struct q6asm_dai_rtd *prtd = runtime->private_data;
950 switch (metadata->key) {
951 case SNDRV_COMPRESS_ENCODER_PADDING:
952 prtd->trailing_samples_drop = metadata->value[0];
954 case SNDRV_COMPRESS_ENCODER_DELAY:
955 prtd->initial_samples_drop = metadata->value[0];
956 if (prtd->next_track_stream_id) {
957 ret = q6asm_open_write(prtd->audio_client,
958 prtd->next_track_stream_id,
961 prtd->bits_per_sample,
964 dev_err(component->dev, "q6asm_open_write failed\n");
967 ret = __q6asm_dai_compr_set_codec_params(component, stream,
969 prtd->next_track_stream_id);
971 dev_err(component->dev, "q6asm_open_write failed\n");
975 ret = q6asm_stream_remove_initial_silence(prtd->audio_client,
976 prtd->next_track_stream_id,
977 prtd->initial_samples_drop);
978 prtd->next_track_stream_id = 0;
991 static int q6asm_dai_compr_trigger(struct snd_soc_component *component,
992 struct snd_compr_stream *stream, int cmd)
994 struct snd_compr_runtime *runtime = stream->runtime;
995 struct q6asm_dai_rtd *prtd = runtime->private_data;
999 case SNDRV_PCM_TRIGGER_START:
1000 case SNDRV_PCM_TRIGGER_RESUME:
1001 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1002 ret = q6asm_run_nowait(prtd->audio_client, prtd->stream_id,
1005 case SNDRV_PCM_TRIGGER_STOP:
1006 prtd->state = Q6ASM_STREAM_STOPPED;
1007 ret = q6asm_cmd_nowait(prtd->audio_client, prtd->stream_id,
1010 case SNDRV_PCM_TRIGGER_SUSPEND:
1011 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1012 ret = q6asm_cmd_nowait(prtd->audio_client, prtd->stream_id,
1015 case SND_COMPR_TRIGGER_NEXT_TRACK:
1016 prtd->next_track = true;
1017 prtd->next_track_stream_id = (prtd->stream_id == 1 ? 2 : 1);
1019 case SND_COMPR_TRIGGER_DRAIN:
1020 case SND_COMPR_TRIGGER_PARTIAL_DRAIN:
1021 prtd->notify_on_drain = true;
1031 static int q6asm_dai_compr_pointer(struct snd_soc_component *component,
1032 struct snd_compr_stream *stream,
1033 struct snd_compr_tstamp *tstamp)
1035 struct snd_compr_runtime *runtime = stream->runtime;
1036 struct q6asm_dai_rtd *prtd = runtime->private_data;
1037 unsigned long flags;
1039 spin_lock_irqsave(&prtd->lock, flags);
1041 tstamp->copied_total = prtd->copied_total;
1042 tstamp->byte_offset = prtd->copied_total % prtd->pcm_size;
1044 spin_unlock_irqrestore(&prtd->lock, flags);
1049 static int q6asm_compr_copy(struct snd_soc_component *component,
1050 struct snd_compr_stream *stream, char __user *buf,
1053 struct snd_compr_runtime *runtime = stream->runtime;
1054 struct q6asm_dai_rtd *prtd = runtime->private_data;
1055 unsigned long flags;
1057 int avail, bytes_in_flight = 0;
1063 bytes_received = prtd->bytes_received;
1066 * Make sure that next track data pointer is aligned at 32 bit boundary
1067 * This is a Mandatory requirement from DSP data buffers alignment
1069 if (prtd->next_track)
1070 bytes_received = ALIGN(prtd->bytes_received, prtd->pcm_count);
1072 app_pointer = bytes_received/prtd->pcm_size;
1073 app_pointer = bytes_received - (app_pointer * prtd->pcm_size);
1074 dstn = prtd->dma_buffer.area + app_pointer;
1076 if (count < prtd->pcm_size - app_pointer) {
1077 if (copy_from_user(dstn, buf, count))
1080 copy = prtd->pcm_size - app_pointer;
1081 if (copy_from_user(dstn, buf, copy))
1083 if (copy_from_user(prtd->dma_buffer.area, buf + copy,
1088 spin_lock_irqsave(&prtd->lock, flags);
1090 bytes_in_flight = prtd->bytes_received - prtd->copied_total;
1092 if (prtd->next_track) {
1093 prtd->next_track = false;
1094 prtd->copied_total = ALIGN(prtd->copied_total, prtd->pcm_count);
1095 prtd->bytes_sent = ALIGN(prtd->bytes_sent, prtd->pcm_count);
1098 prtd->bytes_received = bytes_received + count;
1100 /* Kick off the data to dsp if its starving!! */
1101 if (prtd->state == Q6ASM_STREAM_RUNNING && (bytes_in_flight == 0)) {
1102 uint32_t bytes_to_write = prtd->pcm_count;
1104 avail = prtd->bytes_received - prtd->bytes_sent;
1106 if (avail < prtd->pcm_count)
1107 bytes_to_write = avail;
1109 q6asm_write_async(prtd->audio_client, prtd->stream_id,
1110 bytes_to_write, 0, 0, wflags);
1111 prtd->bytes_sent += bytes_to_write;
1114 spin_unlock_irqrestore(&prtd->lock, flags);
1119 static int q6asm_dai_compr_mmap(struct snd_soc_component *component,
1120 struct snd_compr_stream *stream,
1121 struct vm_area_struct *vma)
1123 struct snd_compr_runtime *runtime = stream->runtime;
1124 struct q6asm_dai_rtd *prtd = runtime->private_data;
1125 struct device *dev = component->dev;
1127 return dma_mmap_coherent(dev, vma,
1128 prtd->dma_buffer.area, prtd->dma_buffer.addr,
1129 prtd->dma_buffer.bytes);
1132 static int q6asm_dai_compr_get_caps(struct snd_soc_component *component,
1133 struct snd_compr_stream *stream,
1134 struct snd_compr_caps *caps)
1136 caps->direction = SND_COMPRESS_PLAYBACK;
1137 caps->min_fragment_size = COMPR_PLAYBACK_MIN_FRAGMENT_SIZE;
1138 caps->max_fragment_size = COMPR_PLAYBACK_MAX_FRAGMENT_SIZE;
1139 caps->min_fragments = COMPR_PLAYBACK_MIN_NUM_FRAGMENTS;
1140 caps->max_fragments = COMPR_PLAYBACK_MAX_NUM_FRAGMENTS;
1141 caps->num_codecs = 5;
1142 caps->codecs[0] = SND_AUDIOCODEC_MP3;
1143 caps->codecs[1] = SND_AUDIOCODEC_FLAC;
1144 caps->codecs[2] = SND_AUDIOCODEC_WMA;
1145 caps->codecs[3] = SND_AUDIOCODEC_ALAC;
1146 caps->codecs[4] = SND_AUDIOCODEC_APE;
1151 static int q6asm_dai_compr_get_codec_caps(struct snd_soc_component *component,
1152 struct snd_compr_stream *stream,
1153 struct snd_compr_codec_caps *codec)
1155 switch (codec->codec) {
1156 case SND_AUDIOCODEC_MP3:
1157 *codec = q6asm_compr_caps;
1166 static const struct snd_compress_ops q6asm_dai_compress_ops = {
1167 .open = q6asm_dai_compr_open,
1168 .free = q6asm_dai_compr_free,
1169 .set_params = q6asm_dai_compr_set_params,
1170 .set_metadata = q6asm_dai_compr_set_metadata,
1171 .pointer = q6asm_dai_compr_pointer,
1172 .trigger = q6asm_dai_compr_trigger,
1173 .get_caps = q6asm_dai_compr_get_caps,
1174 .get_codec_caps = q6asm_dai_compr_get_codec_caps,
1175 .mmap = q6asm_dai_compr_mmap,
1176 .copy = q6asm_compr_copy,
1179 static int q6asm_dai_pcm_new(struct snd_soc_component *component,
1180 struct snd_soc_pcm_runtime *rtd)
1182 struct snd_pcm *pcm = rtd->pcm;
1183 size_t size = q6asm_dai_hardware_playback.buffer_bytes_max;
1185 return snd_pcm_set_fixed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1186 component->dev, size);
1189 static const struct snd_soc_dapm_widget q6asm_dapm_widgets[] = {
1190 SND_SOC_DAPM_AIF_IN("MM_DL1", "MultiMedia1 Playback", 0, SND_SOC_NOPM, 0, 0),
1191 SND_SOC_DAPM_AIF_IN("MM_DL2", "MultiMedia2 Playback", 0, SND_SOC_NOPM, 0, 0),
1192 SND_SOC_DAPM_AIF_IN("MM_DL3", "MultiMedia3 Playback", 0, SND_SOC_NOPM, 0, 0),
1193 SND_SOC_DAPM_AIF_IN("MM_DL4", "MultiMedia4 Playback", 0, SND_SOC_NOPM, 0, 0),
1194 SND_SOC_DAPM_AIF_IN("MM_DL5", "MultiMedia5 Playback", 0, SND_SOC_NOPM, 0, 0),
1195 SND_SOC_DAPM_AIF_IN("MM_DL6", "MultiMedia6 Playback", 0, SND_SOC_NOPM, 0, 0),
1196 SND_SOC_DAPM_AIF_IN("MM_DL7", "MultiMedia7 Playback", 0, SND_SOC_NOPM, 0, 0),
1197 SND_SOC_DAPM_AIF_IN("MM_DL8", "MultiMedia8 Playback", 0, SND_SOC_NOPM, 0, 0),
1198 SND_SOC_DAPM_AIF_OUT("MM_UL1", "MultiMedia1 Capture", 0, SND_SOC_NOPM, 0, 0),
1199 SND_SOC_DAPM_AIF_OUT("MM_UL2", "MultiMedia2 Capture", 0, SND_SOC_NOPM, 0, 0),
1200 SND_SOC_DAPM_AIF_OUT("MM_UL3", "MultiMedia3 Capture", 0, SND_SOC_NOPM, 0, 0),
1201 SND_SOC_DAPM_AIF_OUT("MM_UL4", "MultiMedia4 Capture", 0, SND_SOC_NOPM, 0, 0),
1202 SND_SOC_DAPM_AIF_OUT("MM_UL5", "MultiMedia5 Capture", 0, SND_SOC_NOPM, 0, 0),
1203 SND_SOC_DAPM_AIF_OUT("MM_UL6", "MultiMedia6 Capture", 0, SND_SOC_NOPM, 0, 0),
1204 SND_SOC_DAPM_AIF_OUT("MM_UL7", "MultiMedia7 Capture", 0, SND_SOC_NOPM, 0, 0),
1205 SND_SOC_DAPM_AIF_OUT("MM_UL8", "MultiMedia8 Capture", 0, SND_SOC_NOPM, 0, 0),
1208 static const struct snd_soc_component_driver q6asm_fe_dai_component = {
1210 .open = q6asm_dai_open,
1211 .hw_params = q6asm_dai_hw_params,
1212 .close = q6asm_dai_close,
1213 .prepare = q6asm_dai_prepare,
1214 .trigger = q6asm_dai_trigger,
1215 .pointer = q6asm_dai_pointer,
1216 .pcm_construct = q6asm_dai_pcm_new,
1217 .compress_ops = &q6asm_dai_compress_ops,
1218 .dapm_widgets = q6asm_dapm_widgets,
1219 .num_dapm_widgets = ARRAY_SIZE(q6asm_dapm_widgets),
1220 .legacy_dai_naming = 1,
1223 static struct snd_soc_dai_driver q6asm_fe_dais_template[] = {
1224 Q6ASM_FEDAI_DRIVER(1),
1225 Q6ASM_FEDAI_DRIVER(2),
1226 Q6ASM_FEDAI_DRIVER(3),
1227 Q6ASM_FEDAI_DRIVER(4),
1228 Q6ASM_FEDAI_DRIVER(5),
1229 Q6ASM_FEDAI_DRIVER(6),
1230 Q6ASM_FEDAI_DRIVER(7),
1231 Q6ASM_FEDAI_DRIVER(8),
1234 static const struct snd_soc_dai_ops q6asm_dai_ops = {
1235 .compress_new = snd_soc_new_compress,
1238 static int of_q6asm_parse_dai_data(struct device *dev,
1239 struct q6asm_dai_data *pdata)
1241 struct snd_soc_dai_driver *dai_drv;
1242 struct snd_soc_pcm_stream empty_stream;
1243 struct device_node *node;
1244 int ret, id, dir, idx = 0;
1247 pdata->num_dais = of_get_child_count(dev->of_node);
1248 if (!pdata->num_dais) {
1249 dev_err(dev, "No dais found in DT\n");
1253 pdata->dais = devm_kcalloc(dev, pdata->num_dais, sizeof(*dai_drv),
1258 memset(&empty_stream, 0, sizeof(empty_stream));
1260 for_each_child_of_node(dev->of_node, node) {
1261 ret = of_property_read_u32(node, "reg", &id);
1262 if (ret || id >= MAX_SESSIONS || id < 0) {
1263 dev_err(dev, "valid dai id not found:%d\n", ret);
1267 dai_drv = &pdata->dais[idx++];
1268 *dai_drv = q6asm_fe_dais_template[id];
1270 ret = of_property_read_u32(node, "direction", &dir);
1274 if (dir == Q6ASM_DAI_RX)
1275 dai_drv->capture = empty_stream;
1276 else if (dir == Q6ASM_DAI_TX)
1277 dai_drv->playback = empty_stream;
1279 if (of_property_read_bool(node, "is-compress-dai"))
1280 dai_drv->ops = &q6asm_dai_ops;
1286 static int q6asm_dai_probe(struct platform_device *pdev)
1288 struct device *dev = &pdev->dev;
1289 struct device_node *node = dev->of_node;
1290 struct of_phandle_args args;
1291 struct q6asm_dai_data *pdata;
1294 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1298 rc = of_parse_phandle_with_fixed_args(node, "iommus", 1, 0, &args);
1302 pdata->sid = args.args[0] & SID_MASK_DEFAULT;
1304 dev_set_drvdata(dev, pdata);
1306 rc = of_q6asm_parse_dai_data(dev, pdata);
1310 return devm_snd_soc_register_component(dev, &q6asm_fe_dai_component,
1311 pdata->dais, pdata->num_dais);
1315 static const struct of_device_id q6asm_dai_device_id[] = {
1316 { .compatible = "qcom,q6asm-dais" },
1319 MODULE_DEVICE_TABLE(of, q6asm_dai_device_id);
1322 static struct platform_driver q6asm_dai_platform_driver = {
1324 .name = "q6asm-dai",
1325 .of_match_table = of_match_ptr(q6asm_dai_device_id),
1327 .probe = q6asm_dai_probe,
1329 module_platform_driver(q6asm_dai_platform_driver);
1331 MODULE_DESCRIPTION("Q6ASM dai driver");
1332 MODULE_LICENSE("GPL v2");