blob: 62324e2cefbd14e6219026077bd4761b7da40040 [file] [log] [blame]
Sidipotu Ashok5c6855b2019-01-08 15:58:37 +05301/* Copyright (c) 2012-2019, The Linux Foundation. All rights reserved.
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13
14#include <linux/init.h>
15#include <linux/err.h>
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/time.h>
19#include <linux/wait.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22#include <sound/core.h>
23#include <sound/soc.h>
24#include <sound/soc-dapm.h>
25#include <sound/pcm.h>
26#include <sound/initval.h>
27#include <sound/control.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053028#include <sound/timer.h>
29#include <asm/dma.h>
30#include <linux/dma-mapping.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053031#include <linux/msm_audio.h>
32
33#include <linux/of_device.h>
34#include <sound/tlv.h>
35#include <sound/pcm_params.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053036#include <dsp/msm_audio_ion.h>
37#include <dsp/q6audio-v2.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053038
39#include "msm-pcm-q6-v2.h"
40#include "msm-pcm-routing-v2.h"
41#include "msm-qti-pp-config.h"
42
43enum stream_state {
44 IDLE = 0,
45 STOPPED,
46 RUNNING,
47};
48
49static struct audio_locks the_locks;
50
51#define PCM_MASTER_VOL_MAX_STEPS 0x2000
52static const DECLARE_TLV_DB_LINEAR(msm_pcm_vol_gain, 0,
53 PCM_MASTER_VOL_MAX_STEPS);
54
55struct snd_msm {
56 struct snd_card *card;
57 struct snd_pcm *pcm;
58};
59
60#define CMD_EOS_MIN_TIMEOUT_LENGTH 50
61#define CMD_EOS_TIMEOUT_MULTIPLIER (HZ * 50)
62#define MAX_PB_COPY_RETRIES 3
63
64static struct snd_pcm_hardware msm_pcm_hardware_capture = {
65 .info = (SNDRV_PCM_INFO_MMAP |
66 SNDRV_PCM_INFO_BLOCK_TRANSFER |
67 SNDRV_PCM_INFO_MMAP_VALID |
68 SNDRV_PCM_INFO_INTERLEAVED |
69 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
70 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
71 SNDRV_PCM_FMTBIT_S24_LE |
72 SNDRV_PCM_FMTBIT_S24_3LE |
73 SNDRV_PCM_FMTBIT_S32_LE),
74 .rates = SNDRV_PCM_RATE_8000_384000,
75 .rate_min = 8000,
76 .rate_max = 384000,
77 .channels_min = 1,
Soumya Managoli760f1482019-05-14 16:41:53 +053078 .channels_max = 8,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053079 .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS *
80 CAPTURE_MAX_PERIOD_SIZE,
81 .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
82 .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
83 .periods_min = CAPTURE_MIN_NUM_PERIODS,
84 .periods_max = CAPTURE_MAX_NUM_PERIODS,
85 .fifo_size = 0,
86};
87
88static struct snd_pcm_hardware msm_pcm_hardware_playback = {
89 .info = (SNDRV_PCM_INFO_MMAP |
90 SNDRV_PCM_INFO_BLOCK_TRANSFER |
91 SNDRV_PCM_INFO_MMAP_VALID |
92 SNDRV_PCM_INFO_INTERLEAVED |
93 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
94 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
95 SNDRV_PCM_FMTBIT_S24_LE |
96 SNDRV_PCM_FMTBIT_S24_3LE |
97 SNDRV_PCM_FMTBIT_S32_LE),
98 .rates = SNDRV_PCM_RATE_8000_384000,
99 .rate_min = 8000,
100 .rate_max = 384000,
101 .channels_min = 1,
102 .channels_max = 8,
103 .buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS *
104 PLAYBACK_MAX_PERIOD_SIZE,
105 .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
106 .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
107 .periods_min = PLAYBACK_MIN_NUM_PERIODS,
108 .periods_max = PLAYBACK_MAX_NUM_PERIODS,
109 .fifo_size = 0,
110};
111
112/* Conventional and unconventional sample rate supported */
113static unsigned int supported_sample_rates[] = {
114 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
115 88200, 96000, 176400, 192000, 352800, 384000
116};
117
118static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
119 .count = ARRAY_SIZE(supported_sample_rates),
120 .list = supported_sample_rates,
121 .mask = 0,
122};
123
124static void msm_pcm_route_event_handler(enum msm_pcm_routing_event event,
125 void *priv_data)
126{
127 struct msm_audio *prtd = priv_data;
128
129 WARN_ON(!prtd);
130
131 pr_debug("%s: event %x\n", __func__, event);
132
133 switch (event) {
134 case MSM_PCM_RT_EVT_BUF_RECFG:
135 q6asm_cmd(prtd->audio_client, CMD_PAUSE);
136 q6asm_cmd(prtd->audio_client, CMD_FLUSH);
137 q6asm_run(prtd->audio_client, 0, 0, 0);
138 /* fallthrough */
139 default:
140 break;
141 }
142}
143
144static void event_handler(uint32_t opcode,
145 uint32_t token, uint32_t *payload, void *priv)
146{
147 struct msm_audio *prtd = priv;
148 struct snd_pcm_substream *substream = prtd->substream;
149 uint32_t *ptrmem = (uint32_t *)payload;
150 uint32_t idx = 0;
151 uint32_t size = 0;
152 uint8_t buf_index;
153 struct snd_soc_pcm_runtime *rtd;
154 int ret = 0;
155
156 switch (opcode) {
157 case ASM_DATA_EVENT_WRITE_DONE_V2: {
158 pr_debug("ASM_DATA_EVENT_WRITE_DONE_V2\n");
159 pr_debug("Buffer Consumed = 0x%08x\n", *ptrmem);
160 prtd->pcm_irq_pos += prtd->pcm_count;
161 if (atomic_read(&prtd->start))
162 snd_pcm_period_elapsed(substream);
163 atomic_inc(&prtd->out_count);
164 wake_up(&the_locks.write_wait);
165 if (!atomic_read(&prtd->start))
166 break;
167 if (!prtd->mmap_flag || prtd->reset_event)
168 break;
169 if (q6asm_is_cpu_buf_avail_nolock(IN,
170 prtd->audio_client,
171 &size, &idx)) {
172 pr_debug("%s:writing %d bytes of buffer to dsp 2\n",
173 __func__, prtd->pcm_count);
174 q6asm_write_nolock(prtd->audio_client,
175 prtd->pcm_count, 0, 0, NO_TIMESTAMP);
176 }
177 break;
178 }
179 case ASM_DATA_EVENT_RENDERED_EOS:
180 pr_debug("ASM_DATA_EVENT_RENDERED_EOS\n");
181 clear_bit(CMD_EOS, &prtd->cmd_pending);
182 wake_up(&the_locks.eos_wait);
183 break;
184 case ASM_DATA_EVENT_READ_DONE_V2: {
185 pr_debug("ASM_DATA_EVENT_READ_DONE_V2\n");
186 buf_index = q6asm_get_buf_index_from_token(token);
Vignesh Kulothungan3817b182017-12-04 15:56:05 -0800187 if (buf_index >= CAPTURE_MAX_NUM_PERIODS) {
188 pr_err("%s: buffer index %u is out of range.\n",
189 __func__, buf_index);
190 return;
191 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530192 pr_debug("%s: token=0x%08x buf_index=0x%08x\n",
193 __func__, token, buf_index);
194 prtd->in_frame_info[buf_index].size = payload[4];
195 prtd->in_frame_info[buf_index].offset = payload[5];
196 /* assume data size = 0 during flushing */
197 if (prtd->in_frame_info[buf_index].size) {
198 prtd->pcm_irq_pos +=
199 prtd->in_frame_info[buf_index].size;
200 pr_debug("pcm_irq_pos=%d\n", prtd->pcm_irq_pos);
201 if (atomic_read(&prtd->start))
202 snd_pcm_period_elapsed(substream);
203 if (atomic_read(&prtd->in_count) <= prtd->periods)
204 atomic_inc(&prtd->in_count);
205 wake_up(&the_locks.read_wait);
206 if (prtd->mmap_flag &&
207 q6asm_is_cpu_buf_avail_nolock(OUT,
208 prtd->audio_client,
209 &size, &idx) &&
210 (substream->runtime->status->state ==
211 SNDRV_PCM_STATE_RUNNING))
212 q6asm_read_nolock(prtd->audio_client);
213 } else {
214 pr_debug("%s: reclaim flushed buf in_count %x\n",
215 __func__, atomic_read(&prtd->in_count));
216 prtd->pcm_irq_pos += prtd->pcm_count;
217 if (prtd->mmap_flag) {
218 if (q6asm_is_cpu_buf_avail_nolock(OUT,
219 prtd->audio_client,
220 &size, &idx) &&
221 (substream->runtime->status->state ==
222 SNDRV_PCM_STATE_RUNNING))
223 q6asm_read_nolock(prtd->audio_client);
224 } else {
225 atomic_inc(&prtd->in_count);
226 }
227 if (atomic_read(&prtd->in_count) == prtd->periods) {
228 pr_info("%s: reclaimed all bufs\n", __func__);
229 if (atomic_read(&prtd->start))
230 snd_pcm_period_elapsed(substream);
231 wake_up(&the_locks.read_wait);
232 }
233 }
234 break;
235 }
236 case ASM_STREAM_PP_EVENT:
237 case ASM_STREAM_CMD_ENCDEC_EVENTS: {
238 pr_debug("%s: ASM_STREAM_EVENT (0x%x)\n", __func__, opcode);
239 if (!substream) {
240 pr_err("%s: substream is NULL.\n", __func__);
241 return;
242 }
243
244 rtd = substream->private_data;
245 if (!rtd) {
246 pr_err("%s: rtd is NULL\n", __func__);
247 return;
248 }
249
250 ret = msm_adsp_inform_mixer_ctl(rtd, payload);
251 if (ret) {
252 pr_err("%s: failed to inform mixer ctl. err = %d\n",
253 __func__, ret);
254 return;
255 }
256
257 break;
258 }
259 case APR_BASIC_RSP_RESULT: {
260 switch (payload[0]) {
261 case ASM_SESSION_CMD_RUN_V2:
262 if (substream->stream
263 != SNDRV_PCM_STREAM_PLAYBACK) {
264 atomic_set(&prtd->start, 1);
265 break;
266 }
267 if (prtd->mmap_flag) {
268 pr_debug("%s:writing %d bytes of buffer to dsp\n",
269 __func__,
270 prtd->pcm_count);
271 q6asm_write_nolock(prtd->audio_client,
272 prtd->pcm_count,
273 0, 0, NO_TIMESTAMP);
274 } else {
275 while (atomic_read(&prtd->out_needed)) {
276 pr_debug("%s:writing %d bytes of buffer to dsp\n",
277 __func__,
278 prtd->pcm_count);
279 q6asm_write_nolock(prtd->audio_client,
280 prtd->pcm_count,
281 0, 0, NO_TIMESTAMP);
282 atomic_dec(&prtd->out_needed);
283 wake_up(&the_locks.write_wait);
284 };
285 }
286 atomic_set(&prtd->start, 1);
287 break;
288 case ASM_STREAM_CMD_REGISTER_PP_EVENTS:
289 pr_debug("%s: ASM_STREAM_CMD_REGISTER_PP_EVENTS:",
290 __func__);
291 break;
292 default:
293 pr_debug("%s:Payload = [0x%x]stat[0x%x]\n",
294 __func__, payload[0], payload[1]);
295 break;
296 }
297 }
298 break;
299 case RESET_EVENTS:
300 pr_debug("%s RESET_EVENTS\n", __func__);
301 prtd->pcm_irq_pos += prtd->pcm_count;
302 atomic_inc(&prtd->out_count);
303 atomic_inc(&prtd->in_count);
304 prtd->reset_event = true;
305 if (atomic_read(&prtd->start))
306 snd_pcm_period_elapsed(substream);
307 wake_up(&the_locks.eos_wait);
308 wake_up(&the_locks.write_wait);
309 wake_up(&the_locks.read_wait);
310 break;
311 default:
312 pr_debug("Not Supported Event opcode[0x%x]\n", opcode);
313 break;
314 }
315}
316
317static int msm_pcm_playback_prepare(struct snd_pcm_substream *substream)
318{
319 struct snd_pcm_runtime *runtime = substream->runtime;
320 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
321 struct msm_audio *prtd = runtime->private_data;
322 struct msm_plat_data *pdata;
323 struct snd_pcm_hw_params *params;
324 int ret;
325 uint32_t fmt_type = FORMAT_LINEAR_PCM;
326 uint16_t bits_per_sample;
327 uint16_t sample_word_size;
328
329 pdata = (struct msm_plat_data *)
330 dev_get_drvdata(soc_prtd->platform->dev);
331 if (!pdata) {
332 pr_err("%s: platform data not populated\n", __func__);
333 return -EINVAL;
334 }
335 if (!prtd || !prtd->audio_client) {
336 pr_err("%s: private data null or audio client freed\n",
337 __func__);
338 return -EINVAL;
339 }
340 params = &soc_prtd->dpcm[substream->stream].hw_params;
341
342 pr_debug("%s\n", __func__);
343 prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
344 prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
345 prtd->pcm_irq_pos = 0;
346 /* rate and channels are sent to audio driver */
347 prtd->samp_rate = runtime->rate;
348 prtd->channel_mode = runtime->channels;
349 if (prtd->enabled)
350 return 0;
351
352 prtd->audio_client->perf_mode = pdata->perf_mode;
353 pr_debug("%s: perf: %x\n", __func__, pdata->perf_mode);
354
355 switch (params_format(params)) {
356 case SNDRV_PCM_FORMAT_S32_LE:
357 bits_per_sample = 32;
358 sample_word_size = 32;
359 break;
360 case SNDRV_PCM_FORMAT_S24_LE:
361 bits_per_sample = 24;
362 sample_word_size = 32;
363 break;
364 case SNDRV_PCM_FORMAT_S24_3LE:
365 bits_per_sample = 24;
366 sample_word_size = 24;
367 break;
368 case SNDRV_PCM_FORMAT_S16_LE:
369 default:
370 bits_per_sample = 16;
371 sample_word_size = 16;
372 break;
373 }
374 if (prtd->compress_enable) {
375 fmt_type = FORMAT_GEN_COMPR;
376 pr_debug("%s: Compressed enabled!\n", __func__);
377 ret = q6asm_open_write_compressed(prtd->audio_client, fmt_type,
378 COMPRESSED_PASSTHROUGH_GEN);
379 if (ret < 0) {
380 pr_err("%s: q6asm_open_write_compressed failed (%d)\n",
381 __func__, ret);
382 q6asm_audio_client_free(prtd->audio_client);
383 prtd->audio_client = NULL;
384 return -ENOMEM;
385 }
386 } else {
387 ret = q6asm_open_write_v4(prtd->audio_client,
388 fmt_type, bits_per_sample);
389
390 if (ret < 0) {
391 pr_err("%s: q6asm_open_write_v4 failed (%d)\n",
392 __func__, ret);
393 q6asm_audio_client_free(prtd->audio_client);
394 prtd->audio_client = NULL;
395 return -ENOMEM;
396 }
397
398 ret = q6asm_send_cal(prtd->audio_client);
399 if (ret < 0)
400 pr_debug("%s : Send cal failed : %d", __func__, ret);
401 }
402 pr_debug("%s: session ID %d\n", __func__,
403 prtd->audio_client->session);
404 prtd->session_id = prtd->audio_client->session;
405
406 if (prtd->compress_enable) {
407 ret = msm_pcm_routing_reg_phy_compr_stream(
408 soc_prtd->dai_link->id,
409 prtd->audio_client->perf_mode,
410 prtd->session_id,
411 SNDRV_PCM_STREAM_PLAYBACK,
412 COMPRESSED_PASSTHROUGH_GEN);
413 } else {
414 ret = msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->id,
415 prtd->audio_client->perf_mode,
416 prtd->session_id, substream->stream);
417 }
418 if (ret) {
419 pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
420 return ret;
421 }
422 if (prtd->compress_enable) {
423 ret = q6asm_media_format_block_gen_compr(
424 prtd->audio_client, runtime->rate,
425 runtime->channels, !prtd->set_channel_map,
426 prtd->channel_map, bits_per_sample);
427 } else {
428 ret = q6asm_media_format_block_multi_ch_pcm_v4(
429 prtd->audio_client, runtime->rate,
430 runtime->channels, !prtd->set_channel_map,
431 prtd->channel_map, bits_per_sample,
432 sample_word_size, ASM_LITTLE_ENDIAN,
433 DEFAULT_QF);
434 }
435 if (ret < 0)
436 pr_info("%s: CMD Format block failed\n", __func__);
437
438 atomic_set(&prtd->out_count, runtime->periods);
439
440 prtd->enabled = 1;
441 prtd->cmd_pending = 0;
442 prtd->cmd_interrupt = 0;
443
444 return 0;
445}
446
447static int msm_pcm_capture_prepare(struct snd_pcm_substream *substream)
448{
449 struct snd_pcm_runtime *runtime = substream->runtime;
450 struct msm_audio *prtd = runtime->private_data;
451 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
452 struct msm_plat_data *pdata;
453 struct snd_pcm_hw_params *params;
454 struct msm_pcm_routing_evt event;
455 int ret = 0;
456 int i = 0;
457 uint16_t bits_per_sample = 16;
458 uint16_t sample_word_size;
459
460 pdata = (struct msm_plat_data *)
461 dev_get_drvdata(soc_prtd->platform->dev);
462 if (!pdata) {
463 pr_err("%s: platform data not populated\n", __func__);
464 return -EINVAL;
465 }
466 if (!prtd || !prtd->audio_client) {
467 pr_err("%s: private data null or audio client freed\n",
468 __func__);
469 return -EINVAL;
470 }
471
472 if (prtd->enabled == IDLE) {
473 pr_debug("%s:perf_mode=%d periods=%d\n", __func__,
474 pdata->perf_mode, runtime->periods);
475 params = &soc_prtd->dpcm[substream->stream].hw_params;
476 if ((params_format(params) == SNDRV_PCM_FORMAT_S24_LE) ||
477 (params_format(params) == SNDRV_PCM_FORMAT_S24_3LE))
478 bits_per_sample = 24;
479 else if (params_format(params) == SNDRV_PCM_FORMAT_S32_LE)
480 bits_per_sample = 32;
481
482 /* ULL mode is not supported in capture path */
483 if (pdata->perf_mode == LEGACY_PCM_MODE)
484 prtd->audio_client->perf_mode = LEGACY_PCM_MODE;
485 else
486 prtd->audio_client->perf_mode = LOW_LATENCY_PCM_MODE;
487
488 pr_debug("%s Opening %d-ch PCM read stream, perf_mode %d\n",
489 __func__, params_channels(params),
490 prtd->audio_client->perf_mode);
491
492 ret = q6asm_open_read_v4(prtd->audio_client, FORMAT_LINEAR_PCM,
493 bits_per_sample, false);
494 if (ret < 0) {
495 pr_err("%s: q6asm_open_read failed\n", __func__);
496 q6asm_audio_client_free(prtd->audio_client);
497 prtd->audio_client = NULL;
498 return -ENOMEM;
499 }
500
501 ret = q6asm_send_cal(prtd->audio_client);
502 if (ret < 0)
503 pr_debug("%s : Send cal failed : %d", __func__, ret);
504
505 pr_debug("%s: session ID %d\n",
506 __func__, prtd->audio_client->session);
507 prtd->session_id = prtd->audio_client->session;
508 event.event_func = msm_pcm_route_event_handler;
509 event.priv_data = (void *) prtd;
510 ret = msm_pcm_routing_reg_phy_stream_v2(
511 soc_prtd->dai_link->id,
512 prtd->audio_client->perf_mode,
513 prtd->session_id, substream->stream,
514 event);
515 if (ret) {
516 pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
517 return ret;
518 }
519 }
520
521 prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
522 prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
523 prtd->pcm_irq_pos = 0;
524 /* rate and channels are sent to audio driver */
525 prtd->samp_rate = runtime->rate;
526 prtd->channel_mode = runtime->channels;
527
528 if (prtd->enabled == IDLE || prtd->enabled == STOPPED) {
529 for (i = 0; i < runtime->periods; i++)
530 q6asm_read(prtd->audio_client);
531 prtd->periods = runtime->periods;
532 }
533
534 if (prtd->enabled != IDLE)
535 return 0;
536
537 switch (runtime->format) {
538 case SNDRV_PCM_FORMAT_S32_LE:
539 bits_per_sample = 32;
540 sample_word_size = 32;
541 break;
542 case SNDRV_PCM_FORMAT_S24_LE:
543 bits_per_sample = 24;
544 sample_word_size = 32;
545 break;
546 case SNDRV_PCM_FORMAT_S24_3LE:
547 bits_per_sample = 24;
548 sample_word_size = 24;
549 break;
550 case SNDRV_PCM_FORMAT_S16_LE:
551 default:
552 bits_per_sample = 16;
553 sample_word_size = 16;
554 break;
555 }
556
557 pr_debug("%s: Samp_rate = %d Channel = %d bit width = %d, word size = %d\n",
558 __func__, prtd->samp_rate, prtd->channel_mode,
559 bits_per_sample, sample_word_size);
560 ret = q6asm_enc_cfg_blk_pcm_format_support_v4(prtd->audio_client,
561 prtd->samp_rate,
562 prtd->channel_mode,
563 bits_per_sample,
564 sample_word_size,
565 ASM_LITTLE_ENDIAN,
566 DEFAULT_QF);
567 if (ret < 0)
568 pr_debug("%s: cmd cfg pcm was block failed", __func__);
569
570 prtd->enabled = RUNNING;
571
572 return ret;
573}
574
575static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
576{
577 int ret = 0;
578 struct snd_pcm_runtime *runtime = substream->runtime;
579 struct msm_audio *prtd = runtime->private_data;
580
581 switch (cmd) {
582 case SNDRV_PCM_TRIGGER_START:
583 case SNDRV_PCM_TRIGGER_RESUME:
584 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
585 pr_debug("%s: Trigger start\n", __func__);
586 ret = q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
587 break;
588 case SNDRV_PCM_TRIGGER_STOP:
589 pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
590 atomic_set(&prtd->start, 0);
591 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK) {
592 prtd->enabled = STOPPED;
593 ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
Sidipotu Ashok5c6855b2019-01-08 15:58:37 +0530594 if (!ret)
595 ret = q6asm_cmd_nowait(prtd->audio_client,
596 CMD_FLUSH);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530597 break;
598 }
599 /* pending CMD_EOS isn't expected */
600 WARN_ON_ONCE(test_bit(CMD_EOS, &prtd->cmd_pending));
601 set_bit(CMD_EOS, &prtd->cmd_pending);
602 ret = q6asm_cmd_nowait(prtd->audio_client, CMD_EOS);
603 if (ret)
604 clear_bit(CMD_EOS, &prtd->cmd_pending);
605 break;
606 case SNDRV_PCM_TRIGGER_SUSPEND:
607 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
608 pr_debug("SNDRV_PCM_TRIGGER_PAUSE\n");
609 ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
610 atomic_set(&prtd->start, 0);
611 break;
612 default:
613 ret = -EINVAL;
614 break;
615 }
616
617 return ret;
618}
619
620static int msm_pcm_open(struct snd_pcm_substream *substream)
621{
622 struct snd_pcm_runtime *runtime = substream->runtime;
623 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
624 struct msm_audio *prtd;
Banajit Goswami616e68c2017-10-27 01:31:19 -0700625 struct msm_plat_data *pdata;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530626 int ret = 0;
627
Banajit Goswami616e68c2017-10-27 01:31:19 -0700628 pdata = (struct msm_plat_data *)
629 dev_get_drvdata(soc_prtd->platform->dev);
630 if (!pdata) {
631 pr_err("%s: platform data not populated\n", __func__);
632 return -EINVAL;
633 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530634 prtd = kzalloc(sizeof(struct msm_audio), GFP_KERNEL);
635 if (prtd == NULL)
636 return -ENOMEM;
637
638 prtd->substream = substream;
639 prtd->audio_client = q6asm_audio_client_alloc(
640 (app_cb)event_handler, prtd);
641 if (!prtd->audio_client) {
642 pr_info("%s: Could not allocate memory\n", __func__);
643 kfree(prtd);
Vatsal Bucha321a7442018-06-01 12:05:25 +0530644 prtd = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530645 return -ENOMEM;
646 }
647
648 prtd->audio_client->dev = soc_prtd->platform->dev;
649
650 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
651 runtime->hw = msm_pcm_hardware_playback;
652
653 /* Capture path */
654 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
655 runtime->hw = msm_pcm_hardware_capture;
656 else {
657 pr_err("Invalid Stream type %d\n", substream->stream);
658 return -EINVAL;
659 }
660
661 ret = snd_pcm_hw_constraint_list(runtime, 0,
662 SNDRV_PCM_HW_PARAM_RATE,
663 &constraints_sample_rates);
664 if (ret < 0)
665 pr_info("snd_pcm_hw_constraint_list failed\n");
666 /* Ensure that buffer size is a multiple of period size */
667 ret = snd_pcm_hw_constraint_integer(runtime,
668 SNDRV_PCM_HW_PARAM_PERIODS);
669 if (ret < 0)
670 pr_info("snd_pcm_hw_constraint_integer failed\n");
671
672 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
673 ret = snd_pcm_hw_constraint_minmax(runtime,
674 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
675 PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE,
676 PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE);
677 if (ret < 0) {
678 pr_err("constraint for buffer bytes min max ret = %d\n",
679 ret);
680 }
681 }
682
683 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
684 ret = snd_pcm_hw_constraint_minmax(runtime,
685 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
686 CAPTURE_MIN_NUM_PERIODS * CAPTURE_MIN_PERIOD_SIZE,
687 CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE);
688 if (ret < 0) {
689 pr_err("constraint for buffer bytes min max ret = %d\n",
690 ret);
691 }
692 }
693 ret = snd_pcm_hw_constraint_step(runtime, 0,
694 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
695 if (ret < 0) {
696 pr_err("constraint for period bytes step ret = %d\n",
697 ret);
698 }
699 ret = snd_pcm_hw_constraint_step(runtime, 0,
700 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
701 if (ret < 0) {
702 pr_err("constraint for buffer bytes step ret = %d\n",
703 ret);
704 }
705
706 prtd->enabled = IDLE;
707 prtd->dsp_cnt = 0;
708 prtd->set_channel_map = false;
709 prtd->reset_event = false;
710 runtime->private_data = prtd;
711 msm_adsp_init_mixer_ctl_pp_event_queue(soc_prtd);
Banajit Goswami616e68c2017-10-27 01:31:19 -0700712 /* Vote to update the Rx thread priority to RT Thread for playback */
713 if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) &&
714 (pdata->perf_mode == LOW_LATENCY_PCM_MODE))
715 apr_start_rx_rt(prtd->audio_client->apr);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530716
717 return 0;
718}
719
720static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
721 snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
722{
723 int ret = 0;
724 int fbytes = 0;
725 int xfer = 0;
726 char *bufptr = NULL;
727 void *data = NULL;
728 uint32_t idx = 0;
729 uint32_t size = 0;
730 uint32_t retries = 0;
731
732 struct snd_pcm_runtime *runtime = substream->runtime;
733 struct msm_audio *prtd = runtime->private_data;
734
735 fbytes = frames_to_bytes(runtime, frames);
736 pr_debug("%s: prtd->out_count = %d\n",
737 __func__, atomic_read(&prtd->out_count));
738
739 while ((fbytes > 0) && (retries < MAX_PB_COPY_RETRIES)) {
740 if (prtd->reset_event) {
741 pr_err("%s: In SSR return ENETRESET before wait\n",
742 __func__);
743 return -ENETRESET;
744 }
745
746 ret = wait_event_timeout(the_locks.write_wait,
747 (atomic_read(&prtd->out_count)), 5 * HZ);
748 if (!ret) {
749 pr_err("%s: wait_event_timeout failed\n", __func__);
750 ret = -ETIMEDOUT;
751 goto fail;
752 }
753 ret = 0;
754
755 if (prtd->reset_event) {
756 pr_err("%s: In SSR return ENETRESET after wait\n",
757 __func__);
758 return -ENETRESET;
759 }
760
761 if (!atomic_read(&prtd->out_count)) {
762 pr_err("%s: pcm stopped out_count 0\n", __func__);
763 return 0;
764 }
765
766 data = q6asm_is_cpu_buf_avail(IN, prtd->audio_client, &size,
767 &idx);
768 if (data == NULL) {
769 retries++;
770 continue;
771 } else {
772 retries = 0;
773 }
774
775 if (fbytes > size)
776 xfer = size;
777 else
778 xfer = fbytes;
779
780 bufptr = data;
781 if (bufptr) {
782 pr_debug("%s:fbytes =%d: xfer=%d size=%d\n",
783 __func__, fbytes, xfer, size);
784 if (copy_from_user(bufptr, buf, xfer)) {
785 ret = -EFAULT;
786 pr_err("%s: copy_from_user failed\n",
787 __func__);
788 q6asm_cpu_buf_release(IN, prtd->audio_client);
789 goto fail;
790 }
791 buf += xfer;
792 fbytes -= xfer;
793 pr_debug("%s:fbytes = %d: xfer=%d\n", __func__, fbytes,
794 xfer);
795 if (atomic_read(&prtd->start)) {
796 pr_debug("%s:writing %d bytes of buffer to dsp\n",
797 __func__, xfer);
798 ret = q6asm_write(prtd->audio_client, xfer,
799 0, 0, NO_TIMESTAMP);
800 if (ret < 0) {
801 ret = -EFAULT;
802 q6asm_cpu_buf_release(IN,
803 prtd->audio_client);
804 goto fail;
805 }
806 } else
807 atomic_inc(&prtd->out_needed);
808 atomic_dec(&prtd->out_count);
809 }
810 }
811fail:
812 if (retries >= MAX_PB_COPY_RETRIES)
813 ret = -ENOMEM;
814
815 return ret;
816}
817
818static int msm_pcm_playback_close(struct snd_pcm_substream *substream)
819{
820 struct snd_pcm_runtime *runtime = substream->runtime;
821 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
822 struct msm_audio *prtd = runtime->private_data;
Banajit Goswami616e68c2017-10-27 01:31:19 -0700823 struct msm_plat_data *pdata;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530824 uint32_t timeout;
825 int dir = 0;
826 int ret = 0;
827
828 pr_debug("%s: cmd_pending 0x%lx\n", __func__, prtd->cmd_pending);
829
830 if (prtd->audio_client) {
831 dir = IN;
832
Banajit Goswami616e68c2017-10-27 01:31:19 -0700833 /*
834 * Unvote to downgrade the Rx thread priority from
835 * RT Thread for Low-Latency use case.
836 */
837 pdata = (struct msm_plat_data *)
838 dev_get_drvdata(soc_prtd->platform->dev);
839 if (pdata) {
840 if (pdata->perf_mode == LOW_LATENCY_PCM_MODE)
841 apr_end_rx_rt(prtd->audio_client->apr);
842 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530843 /* determine timeout length */
844 if (runtime->frame_bits == 0 || runtime->rate == 0) {
845 timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
846 } else {
847 timeout = (runtime->period_size *
848 CMD_EOS_TIMEOUT_MULTIPLIER) /
849 ((runtime->frame_bits / 8) *
850 runtime->rate);
851 if (timeout < CMD_EOS_MIN_TIMEOUT_LENGTH)
852 timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
853 }
854 pr_debug("%s: CMD_EOS timeout is %d\n", __func__, timeout);
855
856 ret = wait_event_timeout(the_locks.eos_wait,
857 !test_bit(CMD_EOS, &prtd->cmd_pending),
858 timeout);
859 if (!ret)
860 pr_err("%s: CMD_EOS failed, cmd_pending 0x%lx\n",
861 __func__, prtd->cmd_pending);
862 q6asm_cmd(prtd->audio_client, CMD_CLOSE);
863 q6asm_audio_client_buf_free_contiguous(dir,
864 prtd->audio_client);
865 q6asm_audio_client_free(prtd->audio_client);
866 }
867 msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
868 SNDRV_PCM_STREAM_PLAYBACK);
869 msm_adsp_clean_mixer_ctl_pp_event_queue(soc_prtd);
870 kfree(prtd);
871 runtime->private_data = NULL;
872
873 return 0;
874}
875
876static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
877 int channel, snd_pcm_uframes_t hwoff, void __user *buf,
878 snd_pcm_uframes_t frames)
879{
880 int ret = 0;
881 int fbytes = 0;
882 int xfer;
883 char *bufptr;
884 void *data = NULL;
885 static uint32_t idx;
886 static uint32_t size;
887 uint32_t offset = 0;
888 struct snd_pcm_runtime *runtime = substream->runtime;
889 struct msm_audio *prtd = substream->runtime->private_data;
890
891
892 pr_debug("%s\n", __func__);
893 fbytes = frames_to_bytes(runtime, frames);
894
895 pr_debug("appl_ptr %d\n", (int)runtime->control->appl_ptr);
896 pr_debug("hw_ptr %d\n", (int)runtime->status->hw_ptr);
897 pr_debug("avail_min %d\n", (int)runtime->control->avail_min);
898
899 if (prtd->reset_event) {
900 pr_err("%s: In SSR return ENETRESET before wait\n", __func__);
901 return -ENETRESET;
902 }
903 ret = wait_event_timeout(the_locks.read_wait,
904 (atomic_read(&prtd->in_count)), 5 * HZ);
905 if (!ret) {
906 pr_debug("%s: wait_event_timeout failed\n", __func__);
907 goto fail;
908 }
909 if (prtd->reset_event) {
910 pr_err("%s: In SSR return ENETRESET after wait\n", __func__);
911 return -ENETRESET;
912 }
913 if (!atomic_read(&prtd->in_count)) {
914 pr_debug("%s: pcm stopped in_count 0\n", __func__);
915 return 0;
916 }
917 pr_debug("Checking if valid buffer is available...%pK\n",
918 data);
919 data = q6asm_is_cpu_buf_avail(OUT, prtd->audio_client, &size, &idx);
920 bufptr = data;
921 pr_debug("Size = %d\n", size);
922 pr_debug("fbytes = %d\n", fbytes);
923 pr_debug("idx = %d\n", idx);
924 if (bufptr) {
925 xfer = fbytes;
926 if (xfer > size)
927 xfer = size;
928 offset = prtd->in_frame_info[idx].offset;
929 pr_debug("Offset value = %d\n", offset);
930 if (copy_to_user(buf, bufptr+offset, xfer)) {
931 pr_err("Failed to copy buf to user\n");
932 ret = -EFAULT;
933 q6asm_cpu_buf_release(OUT, prtd->audio_client);
934 goto fail;
935 }
936 fbytes -= xfer;
937 size -= xfer;
938 prtd->in_frame_info[idx].offset += xfer;
939 pr_debug("%s:fbytes = %d: size=%d: xfer=%d\n",
940 __func__, fbytes, size, xfer);
941 pr_debug(" Sending next buffer to dsp\n");
942 memset(&prtd->in_frame_info[idx], 0,
943 sizeof(struct msm_audio_in_frame_info));
944 atomic_dec(&prtd->in_count);
945 ret = q6asm_read(prtd->audio_client);
946 if (ret < 0) {
947 pr_err("q6asm read failed\n");
948 ret = -EFAULT;
949 q6asm_cpu_buf_release(OUT, prtd->audio_client);
950 goto fail;
951 }
952 } else
953 pr_err("No valid buffer\n");
954
955 pr_debug("Returning from capture_copy... %d\n", ret);
956fail:
957 return ret;
958}
959
960static int msm_pcm_capture_close(struct snd_pcm_substream *substream)
961{
962 struct snd_pcm_runtime *runtime = substream->runtime;
963 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
964 struct msm_audio *prtd = runtime->private_data;
965 int dir = OUT;
966
967 pr_debug("%s\n", __func__);
968 if (prtd->audio_client) {
969 q6asm_cmd(prtd->audio_client, CMD_CLOSE);
970 q6asm_audio_client_buf_free_contiguous(dir,
971 prtd->audio_client);
972 q6asm_audio_client_free(prtd->audio_client);
973 }
974
975 msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
976 SNDRV_PCM_STREAM_CAPTURE);
977 kfree(prtd);
978 runtime->private_data = NULL;
979
980 return 0;
981}
982
983static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
984 snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
985{
986 int ret = 0;
987
988 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
989 ret = msm_pcm_playback_copy(substream, a, hwoff, buf, frames);
990 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
991 ret = msm_pcm_capture_copy(substream, a, hwoff, buf, frames);
992 return ret;
993}
994
995static int msm_pcm_close(struct snd_pcm_substream *substream)
996{
997 int ret = 0;
998
999 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1000 ret = msm_pcm_playback_close(substream);
1001 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1002 ret = msm_pcm_capture_close(substream);
1003 return ret;
1004}
1005
1006static int msm_pcm_prepare(struct snd_pcm_substream *substream)
1007{
1008 int ret = 0;
1009
1010 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1011 ret = msm_pcm_playback_prepare(substream);
1012 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1013 ret = msm_pcm_capture_prepare(substream);
1014 return ret;
1015}
1016
1017static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
1018{
1019
1020 struct snd_pcm_runtime *runtime = substream->runtime;
1021 struct msm_audio *prtd = runtime->private_data;
1022
1023 if (prtd->pcm_irq_pos >= prtd->pcm_size)
1024 prtd->pcm_irq_pos = 0;
1025
1026 pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos);
1027 return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
1028}
1029
1030static int msm_pcm_mmap(struct snd_pcm_substream *substream,
1031 struct vm_area_struct *vma)
1032{
1033 struct snd_pcm_runtime *runtime = substream->runtime;
1034 struct msm_audio *prtd = runtime->private_data;
1035 struct audio_client *ac = prtd->audio_client;
1036 struct audio_port_data *apd = ac->port;
1037 struct audio_buffer *ab;
1038 int dir = -1;
1039
1040 prtd->mmap_flag = 1;
1041
1042 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1043 dir = IN;
1044 else
1045 dir = OUT;
1046 ab = &(apd[dir].buf[0]);
1047
1048 return msm_audio_ion_mmap(ab, vma);
1049}
1050
1051static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
1052 struct snd_pcm_hw_params *params)
1053{
1054 struct snd_pcm_runtime *runtime = substream->runtime;
1055 struct msm_audio *prtd = runtime->private_data;
1056 struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
1057 struct audio_buffer *buf;
1058 int dir, ret;
1059
1060 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1061 dir = IN;
1062 else
1063 dir = OUT;
1064 ret = q6asm_audio_client_buf_alloc_contiguous(dir,
1065 prtd->audio_client,
1066 (params_buffer_bytes(params) / params_periods(params)),
1067 params_periods(params));
1068 if (ret < 0) {
1069 pr_err("Audio Start: Buffer Allocation failed rc = %d\n",
1070 ret);
1071 return -ENOMEM;
1072 }
1073 buf = prtd->audio_client->port[dir].buf;
1074 if (buf == NULL || buf[0].data == NULL)
1075 return -ENOMEM;
1076
1077 pr_debug("%s:buf = %pK\n", __func__, buf);
1078 dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
1079 dma_buf->dev.dev = substream->pcm->card->dev;
1080 dma_buf->private_data = NULL;
1081 dma_buf->area = buf[0].data;
1082 dma_buf->addr = buf[0].phys;
1083 dma_buf->bytes = params_buffer_bytes(params);
1084 if (!dma_buf->area)
1085 return -ENOMEM;
1086
1087 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
1088 return 0;
1089}
1090
1091static const struct snd_pcm_ops msm_pcm_ops = {
1092 .open = msm_pcm_open,
1093 .copy = msm_pcm_copy,
1094 .hw_params = msm_pcm_hw_params,
1095 .close = msm_pcm_close,
1096 .ioctl = snd_pcm_lib_ioctl,
1097 .prepare = msm_pcm_prepare,
1098 .trigger = msm_pcm_trigger,
1099 .pointer = msm_pcm_pointer,
1100 .mmap = msm_pcm_mmap,
1101};
1102
1103static int msm_pcm_adsp_stream_cmd_put(struct snd_kcontrol *kcontrol,
1104 struct snd_ctl_elem_value *ucontrol)
1105{
1106 struct snd_soc_component *pcm = snd_kcontrol_chip(kcontrol);
1107 struct snd_soc_platform *platform = snd_soc_component_to_platform(pcm);
1108 struct msm_plat_data *pdata = dev_get_drvdata(platform->dev);
1109 struct snd_pcm_substream *substream;
1110 struct msm_audio *prtd;
1111 int ret = 0;
1112 struct msm_adsp_event_data *event_data = NULL;
Aditya Bavanari2e3341d2018-02-23 12:58:57 +05301113 uint64_t actual_payload_len = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301114
1115 if (!pdata) {
1116 pr_err("%s pdata is NULL\n", __func__);
1117 ret = -ENODEV;
1118 goto done;
1119 }
1120
1121 substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1122 if (!substream) {
1123 pr_err("%s substream not found\n", __func__);
1124 ret = -EINVAL;
1125 goto done;
1126 }
1127
1128 if (!substream->runtime) {
1129 pr_err("%s substream runtime not found\n", __func__);
1130 ret = -EINVAL;
1131 goto done;
1132 }
1133
1134 prtd = substream->runtime->private_data;
Vatsal Bucha321a7442018-06-01 12:05:25 +05301135 if (prtd == NULL) {
1136 pr_err("%s prtd is null.\n", __func__);
1137 ret = -EINVAL;
1138 goto done;
1139 }
1140
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301141 if (prtd->audio_client == NULL) {
1142 pr_err("%s prtd is null.\n", __func__);
1143 ret = -EINVAL;
1144 goto done;
1145 }
1146
1147 event_data = (struct msm_adsp_event_data *)ucontrol->value.bytes.data;
1148 if ((event_data->event_type < ADSP_STREAM_PP_EVENT) ||
1149 (event_data->event_type >= ADSP_STREAM_EVENT_MAX)) {
1150 pr_err("%s: invalid event_type=%d",
1151 __func__, event_data->event_type);
1152 ret = -EINVAL;
1153 goto done;
1154 }
1155
Aditya Bavanari2e3341d2018-02-23 12:58:57 +05301156 actual_payload_len = sizeof(struct msm_adsp_event_data) +
1157 event_data->payload_len;
1158 if (actual_payload_len >= U32_MAX) {
1159 pr_err("%s payload length 0x%X exceeds limit",
1160 __func__, event_data->payload_len);
1161 ret = -EINVAL;
1162 goto done;
1163 }
1164
Xiaojun Sangae3d8862018-03-23 08:57:33 +08001165 if (event_data->payload_len > sizeof(ucontrol->value.bytes.data)
1166 - sizeof(struct msm_adsp_event_data)) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301167 pr_err("%s param length=%d exceeds limit",
1168 __func__, event_data->payload_len);
1169 ret = -EINVAL;
1170 goto done;
1171 }
1172
1173 ret = q6asm_send_stream_cmd(prtd->audio_client, event_data);
1174 if (ret < 0)
1175 pr_err("%s: failed to send stream event cmd, err = %d\n",
1176 __func__, ret);
1177done:
1178 return ret;
1179}
1180
1181static int msm_pcm_add_audio_adsp_stream_cmd_control(
1182 struct snd_soc_pcm_runtime *rtd)
1183{
1184 const char *mixer_ctl_name = DSP_STREAM_CMD;
1185 const char *deviceNo = "NN";
1186 char *mixer_str = NULL;
1187 int ctl_len = 0, ret = 0;
1188 struct snd_kcontrol_new fe_audio_adsp_stream_cmd_config_control[1] = {
1189 {
1190 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1191 .name = "?",
1192 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1193 .info = msm_adsp_stream_cmd_info,
1194 .put = msm_pcm_adsp_stream_cmd_put,
1195 .private_value = 0,
1196 }
1197 };
1198
1199 if (!rtd) {
1200 pr_err("%s rtd is NULL\n", __func__);
1201 ret = -EINVAL;
1202 goto done;
1203 }
1204
1205 ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
1206 mixer_str = kzalloc(ctl_len, GFP_KERNEL);
1207 if (!mixer_str) {
1208 ret = -ENOMEM;
1209 goto done;
1210 }
1211
1212 snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
1213 fe_audio_adsp_stream_cmd_config_control[0].name = mixer_str;
1214 fe_audio_adsp_stream_cmd_config_control[0].private_value =
1215 rtd->dai_link->id;
1216 pr_debug("Registering new mixer ctl %s\n", mixer_str);
1217 ret = snd_soc_add_platform_controls(rtd->platform,
1218 fe_audio_adsp_stream_cmd_config_control,
1219 ARRAY_SIZE(fe_audio_adsp_stream_cmd_config_control));
1220 if (ret < 0)
1221 pr_err("%s: failed add ctl %s. err = %d\n",
1222 __func__, mixer_str, ret);
1223
1224 kfree(mixer_str);
1225done:
1226 return ret;
1227}
1228
1229static int msm_pcm_add_audio_adsp_stream_callback_control(
1230 struct snd_soc_pcm_runtime *rtd)
1231{
1232 const char *mixer_ctl_name = DSP_STREAM_CALLBACK;
1233 const char *deviceNo = "NN";
1234 char *mixer_str = NULL;
1235 int ctl_len = 0, ret = 0;
1236 struct snd_kcontrol *kctl;
1237
1238 struct snd_kcontrol_new fe_audio_adsp_callback_config_control[1] = {
1239 {
1240 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1241 .name = "?",
1242 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1243 .info = msm_adsp_stream_callback_info,
1244 .get = msm_adsp_stream_callback_get,
1245 .private_value = 0,
1246 }
1247 };
1248
1249 if (!rtd) {
1250 pr_err("%s NULL rtd\n", __func__);
1251 ret = -EINVAL;
1252 goto done;
1253 }
1254
1255 pr_debug("%s: added new pcm FE with name %s, id %d, cpu dai %s, device no %d\n",
1256 __func__, rtd->dai_link->name, rtd->dai_link->id,
1257 rtd->dai_link->cpu_dai_name, rtd->pcm->device);
1258 ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
1259 mixer_str = kzalloc(ctl_len, GFP_KERNEL);
1260 if (!mixer_str) {
1261 ret = -ENOMEM;
1262 goto done;
1263 }
1264
1265 snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
1266 fe_audio_adsp_callback_config_control[0].name = mixer_str;
1267 fe_audio_adsp_callback_config_control[0].private_value =
1268 rtd->dai_link->id;
1269 pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
1270 ret = snd_soc_add_platform_controls(rtd->platform,
1271 fe_audio_adsp_callback_config_control,
1272 ARRAY_SIZE(fe_audio_adsp_callback_config_control));
1273 if (ret < 0) {
1274 pr_err("%s: failed to add ctl %s. err = %d\n",
1275 __func__, mixer_str, ret);
1276 ret = -EINVAL;
1277 goto free_mixer_str;
1278 }
1279
1280 kctl = snd_soc_card_get_kcontrol(rtd->card, mixer_str);
1281 if (!kctl) {
1282 pr_err("%s: failed to get kctl %s.\n", __func__, mixer_str);
1283 ret = -EINVAL;
1284 goto free_mixer_str;
1285 }
1286
1287 kctl->private_data = NULL;
1288
1289free_mixer_str:
1290 kfree(mixer_str);
1291done:
1292 return ret;
1293}
1294
1295static int msm_pcm_set_volume(struct msm_audio *prtd, uint32_t volume)
1296{
1297 int rc = 0;
1298
1299 if (prtd && prtd->audio_client) {
1300 pr_debug("%s: channels %d volume 0x%x\n", __func__,
1301 prtd->channel_mode, volume);
1302 rc = q6asm_set_volume(prtd->audio_client, volume);
1303 if (rc < 0) {
1304 pr_err("%s: Send Volume command failed rc=%d\n",
1305 __func__, rc);
1306 }
1307 }
1308 return rc;
1309}
1310
1311static int msm_pcm_volume_ctl_get(struct snd_kcontrol *kcontrol,
1312 struct snd_ctl_elem_value *ucontrol)
1313{
1314 struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
1315 struct snd_pcm_substream *substream =
Haynes Mathew Georged00e1062018-04-06 18:18:51 -07001316 vol->pcm->streams[vol->stream].substream;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301317 struct msm_audio *prtd;
1318
1319 pr_debug("%s\n", __func__);
1320 if (!substream) {
1321 pr_err("%s substream not found\n", __func__);
1322 return -ENODEV;
1323 }
1324 if (!substream->runtime) {
1325 pr_err("%s substream runtime not found\n", __func__);
1326 return 0;
1327 }
1328 prtd = substream->runtime->private_data;
1329 if (prtd)
1330 ucontrol->value.integer.value[0] = prtd->volume;
1331 return 0;
1332}
1333
1334static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
1335 struct snd_ctl_elem_value *ucontrol)
1336{
1337 int rc = 0;
1338 struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
1339 struct snd_pcm_substream *substream =
Haynes Mathew Georged00e1062018-04-06 18:18:51 -07001340 vol->pcm->streams[vol->stream].substream;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301341 struct msm_audio *prtd;
1342 int volume = ucontrol->value.integer.value[0];
1343
1344 pr_debug("%s: volume : 0x%x\n", __func__, volume);
1345 if (!substream) {
1346 pr_err("%s substream not found\n", __func__);
1347 return -ENODEV;
1348 }
1349 if (!substream->runtime) {
1350 pr_err("%s substream runtime not found\n", __func__);
1351 return 0;
1352 }
1353 prtd = substream->runtime->private_data;
1354 if (prtd) {
1355 rc = msm_pcm_set_volume(prtd, volume);
1356 prtd->volume = volume;
1357 }
1358 return rc;
1359}
1360
Haynes Mathew Georged00e1062018-04-06 18:18:51 -07001361static int msm_pcm_add_volume_control(struct snd_soc_pcm_runtime *rtd,
1362 int stream)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301363{
1364 int ret = 0;
1365 struct snd_pcm *pcm = rtd->pcm;
1366 struct snd_pcm_volume *volume_info;
1367 struct snd_kcontrol *kctl;
1368
Haynes Mathew Georged00e1062018-04-06 18:18:51 -07001369 dev_dbg(rtd->dev, "%s, volume control add\n", __func__);
1370 ret = snd_pcm_add_volume_ctls(pcm, stream,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301371 NULL, 1, rtd->dai_link->id,
1372 &volume_info);
1373 if (ret < 0) {
1374 pr_err("%s volume control failed ret %d\n", __func__, ret);
1375 return ret;
1376 }
1377 kctl = volume_info->kctl;
1378 kctl->put = msm_pcm_volume_ctl_put;
1379 kctl->get = msm_pcm_volume_ctl_get;
1380 kctl->tlv.p = msm_pcm_vol_gain;
1381 return 0;
1382}
1383
1384static int msm_pcm_compress_ctl_info(struct snd_kcontrol *kcontrol,
1385 struct snd_ctl_elem_info *uinfo)
1386{
1387 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1388 uinfo->count = 1;
1389 uinfo->value.integer.min = 0;
1390 uinfo->value.integer.max = 0x2000;
1391 return 0;
1392}
1393
1394static int msm_pcm_compress_ctl_get(struct snd_kcontrol *kcontrol,
1395 struct snd_ctl_elem_value *ucontrol)
1396{
1397 struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
1398 struct snd_soc_platform *platform = snd_soc_component_to_platform(comp);
1399 struct msm_plat_data *pdata = dev_get_drvdata(platform->dev);
1400 struct snd_pcm_substream *substream;
1401 struct msm_audio *prtd;
1402
1403 if (!pdata) {
1404 pr_err("%s pdata is NULL\n", __func__);
1405 return -ENODEV;
1406 }
1407 substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1408 if (!substream) {
1409 pr_err("%s substream not found\n", __func__);
1410 return -EINVAL;
1411 }
1412 if (!substream->runtime) {
1413 pr_err("%s substream runtime not found\n", __func__);
1414 return 0;
1415 }
1416 prtd = substream->runtime->private_data;
1417 if (prtd)
1418 ucontrol->value.integer.value[0] = prtd->compress_enable;
1419 return 0;
1420}
1421
1422static int msm_pcm_compress_ctl_put(struct snd_kcontrol *kcontrol,
1423 struct snd_ctl_elem_value *ucontrol)
1424{
1425 int rc = 0;
1426 struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
1427 struct snd_soc_platform *platform = snd_soc_component_to_platform(comp);
1428 struct msm_plat_data *pdata = dev_get_drvdata(platform->dev);
1429 struct snd_pcm_substream *substream;
1430 struct msm_audio *prtd;
1431 int compress = ucontrol->value.integer.value[0];
1432
1433 if (!pdata) {
1434 pr_err("%s pdata is NULL\n", __func__);
1435 return -ENODEV;
1436 }
1437 substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1438 pr_debug("%s: compress : 0x%x\n", __func__, compress);
1439 if (!substream) {
1440 pr_err("%s substream not found\n", __func__);
1441 return -EINVAL;
1442 }
1443 if (!substream->runtime) {
1444 pr_err("%s substream runtime not found\n", __func__);
1445 return 0;
1446 }
1447 prtd = substream->runtime->private_data;
1448 if (prtd) {
1449 pr_debug("%s: setting compress flag to 0x%x\n",
1450 __func__, compress);
1451 prtd->compress_enable = compress;
1452 }
1453 return rc;
1454}
1455
1456static int msm_pcm_add_compress_control(struct snd_soc_pcm_runtime *rtd)
1457{
1458 const char *mixer_ctl_name = "Playback ";
1459 const char *mixer_ctl_end_name = " Compress";
1460 const char *deviceNo = "NN";
1461 char *mixer_str = NULL;
1462 int ctl_len;
1463 int ret = 0;
1464 struct msm_plat_data *pdata;
1465 struct snd_kcontrol_new pcm_compress_control[1] = {
1466 {
1467 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1468 .name = "?",
1469 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1470 .info = msm_pcm_compress_ctl_info,
1471 .get = msm_pcm_compress_ctl_get,
1472 .put = msm_pcm_compress_ctl_put,
1473 .private_value = 0,
1474 }
1475 };
1476
1477 if (!rtd) {
1478 pr_err("%s: NULL rtd\n", __func__);
1479 return -EINVAL;
1480 }
1481
1482 ctl_len = strlen(mixer_ctl_name) + strlen(deviceNo) +
1483 strlen(mixer_ctl_end_name) + 1;
1484 mixer_str = kzalloc(ctl_len, GFP_KERNEL);
1485
1486 if (!mixer_str)
1487 return -ENOMEM;
1488
1489 snprintf(mixer_str, ctl_len, "%s%d%s", mixer_ctl_name,
1490 rtd->pcm->device, mixer_ctl_end_name);
1491
1492 pcm_compress_control[0].name = mixer_str;
1493 pcm_compress_control[0].private_value = rtd->dai_link->id;
1494 pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
1495 pdata = dev_get_drvdata(rtd->platform->dev);
1496 if (pdata) {
1497 if (!pdata->pcm) {
1498 pdata->pcm = rtd->pcm;
1499 snd_soc_add_platform_controls(rtd->platform,
1500 pcm_compress_control,
1501 ARRAY_SIZE
1502 (pcm_compress_control));
1503 pr_debug("%s: add control success plt = %pK\n",
1504 __func__, rtd->platform);
1505 }
1506 } else {
1507 pr_err("%s: NULL pdata\n", __func__);
1508 ret = -EINVAL;
1509 }
1510 kfree(mixer_str);
1511 return ret;
1512}
1513
1514static int msm_pcm_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1515 struct snd_ctl_elem_value *ucontrol)
1516{
1517 int i;
1518 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1519 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1520 struct snd_pcm_substream *substream;
1521 struct msm_audio *prtd;
1522
1523 pr_debug("%s", __func__);
1524 substream = snd_pcm_chmap_substream(info, idx);
1525 if (!substream)
1526 return -ENODEV;
1527 if (!substream->runtime)
1528 return 0;
1529
1530 prtd = substream->runtime->private_data;
1531 if (prtd) {
1532 prtd->set_channel_map = true;
1533 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
1534 prtd->channel_map[i] =
1535 (char)(ucontrol->value.integer.value[i]);
1536 }
1537 return 0;
1538}
1539
1540static int msm_pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1541 struct snd_ctl_elem_value *ucontrol)
1542{
1543 int i;
1544 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1545 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1546 struct snd_pcm_substream *substream;
1547 struct msm_audio *prtd;
1548
1549 pr_debug("%s", __func__);
1550 substream = snd_pcm_chmap_substream(info, idx);
1551 if (!substream)
1552 return -ENODEV;
1553 memset(ucontrol->value.integer.value, 0,
1554 sizeof(ucontrol->value.integer.value));
1555 if (!substream->runtime)
1556 return 0; /* no channels set */
1557
1558 prtd = substream->runtime->private_data;
1559
1560 if (prtd && prtd->set_channel_map == true) {
1561 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
1562 ucontrol->value.integer.value[i] =
1563 (int)prtd->channel_map[i];
1564 } else {
1565 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
1566 ucontrol->value.integer.value[i] = 0;
1567 }
1568
1569 return 0;
1570}
1571
1572static int msm_pcm_add_chmap_controls(struct snd_soc_pcm_runtime *rtd)
1573{
1574 struct snd_pcm *pcm = rtd->pcm;
1575 struct snd_pcm_chmap *chmap_info;
1576 struct snd_kcontrol *kctl;
1577 char device_num[12];
1578 int i, ret = 0;
1579
1580 pr_debug("%s, Channel map cntrl add\n", __func__);
1581 ret = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1582 snd_pcm_std_chmaps,
1583 PCM_FORMAT_MAX_NUM_CHANNEL, 0,
1584 &chmap_info);
1585 if (ret < 0) {
1586 pr_err("%s, channel map cntrl add failed\n", __func__);
1587 return ret;
1588 }
1589 kctl = chmap_info->kctl;
1590 for (i = 0; i < kctl->count; i++)
1591 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1592 snprintf(device_num, sizeof(device_num), "%d", pcm->device);
1593 strlcat(kctl->id.name, device_num, sizeof(kctl->id.name));
1594 pr_debug("%s, Overwriting channel map control name to: %s\n",
1595 __func__, kctl->id.name);
1596 kctl->put = msm_pcm_chmap_ctl_put;
1597 kctl->get = msm_pcm_chmap_ctl_get;
1598 return 0;
1599}
1600
1601static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
1602 struct snd_ctl_elem_value *ucontrol)
1603{
1604 u64 fe_id = kcontrol->private_value;
1605 int session_type = SESSION_TYPE_RX;
1606 int be_id = ucontrol->value.integer.value[3];
1607 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
1608 int ret = 0;
1609
1610 cfg_data.app_type = ucontrol->value.integer.value[0];
1611 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
1612 if (ucontrol->value.integer.value[2] != 0)
1613 cfg_data.sample_rate = ucontrol->value.integer.value[2];
1614 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
1615 __func__, fe_id, session_type, be_id,
1616 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1617 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
1618 be_id, &cfg_data);
1619 if (ret < 0)
1620 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
1621 __func__, ret);
1622
1623 return ret;
1624}
1625
1626static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
1627 struct snd_ctl_elem_value *ucontrol)
1628{
1629 u64 fe_id = kcontrol->private_value;
1630 int session_type = SESSION_TYPE_RX;
1631 int be_id = 0;
1632 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
1633 int ret = 0;
1634
1635 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
1636 &be_id, &cfg_data);
1637 if (ret < 0) {
1638 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
1639 __func__, ret);
1640 goto done;
1641 }
1642
1643 ucontrol->value.integer.value[0] = cfg_data.app_type;
1644 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
1645 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
1646 ucontrol->value.integer.value[3] = be_id;
1647 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
1648 __func__, fe_id, session_type, be_id,
1649 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1650done:
1651 return ret;
1652}
1653
1654static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
1655 struct snd_ctl_elem_value *ucontrol)
1656{
1657 u64 fe_id = kcontrol->private_value;
1658 int session_type = SESSION_TYPE_TX;
1659 int be_id = ucontrol->value.integer.value[3];
1660 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
1661 int ret = 0;
1662
1663 cfg_data.app_type = ucontrol->value.integer.value[0];
1664 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
1665 if (ucontrol->value.integer.value[2] != 0)
1666 cfg_data.sample_rate = ucontrol->value.integer.value[2];
1667 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
1668 __func__, fe_id, session_type, be_id,
1669 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1670 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
1671 be_id, &cfg_data);
1672 if (ret < 0)
1673 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
1674 __func__, ret);
1675
1676 return ret;
1677}
1678
1679static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
1680 struct snd_ctl_elem_value *ucontrol)
1681{
1682 u64 fe_id = kcontrol->private_value;
1683 int session_type = SESSION_TYPE_TX;
1684 int be_id = 0;
1685 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
1686 int ret = 0;
1687
1688 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
1689 &be_id, &cfg_data);
1690 if (ret < 0) {
1691 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
1692 __func__, ret);
1693 goto done;
1694 }
1695
1696 ucontrol->value.integer.value[0] = cfg_data.app_type;
1697 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
1698 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
1699 ucontrol->value.integer.value[3] = be_id;
1700 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
1701 __func__, fe_id, session_type, be_id,
1702 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1703done:
1704 return ret;
1705}
1706
1707static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
1708{
1709 struct snd_pcm *pcm = rtd->pcm;
1710 struct snd_pcm_usr *app_type_info;
1711 struct snd_kcontrol *kctl;
1712 const char *playback_mixer_ctl_name = "Audio Stream";
1713 const char *capture_mixer_ctl_name = "Audio Stream Capture";
1714 const char *deviceNo = "NN";
1715 const char *suffix = "App Type Cfg";
1716 int ctl_len, ret = 0;
1717
1718 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
1719 ctl_len = strlen(playback_mixer_ctl_name) + 1 +
1720 strlen(deviceNo) + 1 + strlen(suffix) + 1;
1721 pr_debug("%s: Playback app type cntrl add\n", __func__);
1722 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1723 NULL, 1, ctl_len, rtd->dai_link->id,
1724 &app_type_info);
1725 if (ret < 0) {
1726 pr_err("%s: playback app type cntrl add failed: %d\n",
1727 __func__, ret);
1728 return ret;
1729 }
1730 kctl = app_type_info->kctl;
1731 snprintf(kctl->id.name, ctl_len, "%s %d %s",
1732 playback_mixer_ctl_name, rtd->pcm->device, suffix);
1733 kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
1734 kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
1735 }
1736
1737 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
1738 ctl_len = strlen(capture_mixer_ctl_name) + 1 +
1739 strlen(deviceNo) + 1 + strlen(suffix) + 1;
1740 pr_debug("%s: Capture app type cntrl add\n", __func__);
1741 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
1742 NULL, 1, ctl_len, rtd->dai_link->id,
1743 &app_type_info);
1744 if (ret < 0) {
1745 pr_err("%s: capture app type cntrl add failed: %d\n",
1746 __func__, ret);
1747 return ret;
1748 }
1749 kctl = app_type_info->kctl;
1750 snprintf(kctl->id.name, ctl_len, "%s %d %s",
1751 capture_mixer_ctl_name, rtd->pcm->device, suffix);
1752 kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
1753 kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
1754 }
1755
1756 return 0;
1757}
1758
Dhanalakshmi Siddani040e0262018-11-26 23:01:26 +05301759static int msm_pcm_chmix_cfg_ctl_info(struct snd_kcontrol *kcontrol,
1760 struct snd_ctl_elem_info *uinfo)
1761{
1762 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1763 uinfo->count = 128;
1764 uinfo->value.integer.min = 0;
1765 uinfo->value.integer.max = 0xFFFFFFFF;
1766 return 0;
1767}
1768
1769static int msm_pcm_chmix_cfg_ctl_put(struct snd_kcontrol *kcontrol,
1770 struct snd_ctl_elem_value *ucontrol)
1771{
1772 struct snd_soc_component *usr_info = snd_kcontrol_chip(kcontrol);
1773 struct snd_soc_platform *platform;
1774 struct msm_plat_data *pdata;
1775 struct snd_pcm_substream *substream;
1776 struct msm_audio *prtd;
1777 u64 fe_id = kcontrol->private_value;
1778 int ip_channel_cnt, op_channel_cnt;
1779 int i, index = 0, ret = 0;
1780 int ch_coeff[PCM_FORMAT_MAX_NUM_CHANNEL * PCM_FORMAT_MAX_NUM_CHANNEL];
1781 bool use_default_chmap = true;
1782 char *ch_map = NULL;
1783
1784 pr_debug("%s: fe_id- %llu\n", __func__, fe_id);
1785 if (fe_id >= MSM_FRONTEND_DAI_MAX) {
1786 pr_err("%s: Received out of bounds fe_id %llu\n",
1787 __func__, fe_id);
1788 ret = -EINVAL;
1789 goto done;
1790 }
1791
1792 if (!usr_info) {
1793 pr_err("%s: usr_info is null\n", __func__);
1794 ret = -EINVAL;
1795 goto done;
1796 }
1797 platform = snd_soc_component_to_platform(usr_info);
1798 if (!platform) {
1799 pr_err("%s: platform is null\n", __func__);
1800 ret = -EINVAL;
1801 goto done;
1802 }
1803 pdata = dev_get_drvdata(platform->dev);
1804 if (!pdata) {
1805 pr_err("%s: pdata is null\n", __func__);
1806 ret = -EINVAL;
1807 goto done;
1808 }
1809 substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1810
1811 if (!substream) {
1812 pr_err("%s substream not found\n", __func__);
1813 return -ENODEV;
1814 }
1815 if (!substream->runtime) {
1816 pr_err("%s substream runtime not found\n", __func__);
1817 ret = -EINVAL;
1818 goto done;
1819 }
1820 prtd = substream->runtime->private_data;
1821 if (!prtd) {
1822 pr_err("%s: stream inactive\n", __func__);
1823 ret = -EINVAL;
1824 goto done;
1825 }
1826 use_default_chmap = !prtd->set_channel_map;
1827 ch_map = prtd->channel_map;
1828 ip_channel_cnt = ucontrol->value.integer.value[index++];
1829 op_channel_cnt = ucontrol->value.integer.value[index++];
1830 /*
1831 * wght coeff of first out channel corresponding to each in channel
1832 * are sent followed by second out channel for each in channel etc.
1833 */
1834 memset(ch_coeff, 0, sizeof(ch_coeff));
1835 for (i = 0; i < op_channel_cnt * ip_channel_cnt; i++) {
1836 ch_coeff[i] =
1837 ucontrol->value.integer.value[index++];
1838 }
1839
1840 msm_pcm_routing_send_chmix_cfg(fe_id, ip_channel_cnt, op_channel_cnt,
1841 ch_coeff, SESSION_TYPE_RX, use_default_chmap, ch_map);
1842done:
1843 return ret;
1844}
1845
1846static int msm_pcm_chmix_cfg_ctl_get(struct snd_kcontrol *kcontrol,
1847 struct snd_ctl_elem_value *ucontrol)
1848{
1849 return 0;
1850}
1851
1852static int msm_pcm_add_chmix_cfg_controls(struct snd_soc_pcm_runtime *rtd)
1853{
1854 const char *mixer_ctl_name = "Audio Stream";
1855 const char *deviceNo = "NN";
1856 const char *suffix = "Channel Mix Cfg";
1857 char *mixer_str = NULL;
1858 int ctl_len = 0;
1859 struct msm_plat_data *pdata;
1860 struct snd_kcontrol_new chmix_cfg_controls[1] = {
1861 {
1862 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1863 .name = "?",
1864 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1865 .info = msm_pcm_chmix_cfg_ctl_info,
1866 .get = msm_pcm_chmix_cfg_ctl_get,
1867 .put = msm_pcm_chmix_cfg_ctl_put,
1868 .private_value = 0,
1869 }
1870 };
1871
1872 if (!rtd) {
1873 pr_err("%s: NULL rtd\n", __func__);
1874 return -EINVAL;
1875 }
1876
1877 ctl_len = strlen(mixer_ctl_name) + 1 +
1878 strlen(deviceNo) + 1 + strlen(suffix) + 1;
1879 mixer_str = kzalloc(ctl_len, GFP_KERNEL);
1880 if (!mixer_str)
1881 return -ENOMEM;
1882
1883 snprintf(mixer_str, ctl_len, "%s %d %s",
1884 mixer_ctl_name, rtd->pcm->device, suffix);
1885 chmix_cfg_controls[0].name = mixer_str;
1886 chmix_cfg_controls[0].private_value = rtd->dai_link->id;
1887 pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
1888 pdata = dev_get_drvdata(rtd->platform->dev);
1889 if (pdata) {
1890 if (!pdata->pcm)
1891 pdata->pcm = rtd->pcm;
1892 snd_soc_add_platform_controls(rtd->platform,
1893 chmix_cfg_controls,
1894 ARRAY_SIZE(chmix_cfg_controls));
1895 } else {
1896 pr_err("%s: NULL pdata\n", __func__);
1897 return -EINVAL;
1898 }
1899 kfree(mixer_str);
1900 return 0;
1901}
1902
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301903static int msm_pcm_add_controls(struct snd_soc_pcm_runtime *rtd)
1904{
1905 int ret = 0;
1906
1907 pr_debug("%s\n", __func__);
1908 ret = msm_pcm_add_chmap_controls(rtd);
1909 if (ret)
1910 pr_err("%s: pcm add controls failed:%d\n", __func__, ret);
1911 ret = msm_pcm_add_app_type_controls(rtd);
1912 if (ret)
1913 pr_err("%s: pcm add app type controls failed:%d\n",
1914 __func__, ret);
Dhanalakshmi Siddani040e0262018-11-26 23:01:26 +05301915 ret = msm_pcm_add_chmix_cfg_controls(rtd);
1916 if (ret)
1917 pr_err("%s: add chmix cfg controls failed:%d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301918 return ret;
1919}
1920
1921static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
1922{
1923 struct snd_card *card = rtd->card->snd_card;
1924 int ret = 0;
1925
1926 if (!card->dev->coherent_dma_mask)
1927 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
1928
1929 ret = msm_pcm_add_controls(rtd);
1930 if (ret) {
1931 pr_err("%s, kctl add failed:%d\n", __func__, ret);
1932 return ret;
1933 }
1934
Haynes Mathew Georged00e1062018-04-06 18:18:51 -07001935 ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_PLAYBACK);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301936 if (ret)
1937 pr_err("%s: Could not add pcm Volume Control %d\n",
1938 __func__, ret);
Haynes Mathew Georged00e1062018-04-06 18:18:51 -07001939 ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_CAPTURE);
1940 if (ret)
1941 pr_err("%s: Could not add pcm Volume Control %d\n",
1942 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301943 ret = msm_pcm_add_compress_control(rtd);
1944 if (ret)
1945 pr_err("%s: Could not add pcm Compress Control %d\n",
1946 __func__, ret);
1947
1948 ret = msm_pcm_add_audio_adsp_stream_cmd_control(rtd);
1949 if (ret)
1950 pr_err("%s: Could not add pcm ADSP Stream Cmd Control\n",
1951 __func__);
1952
1953 ret = msm_pcm_add_audio_adsp_stream_callback_control(rtd);
1954 if (ret)
1955 pr_err("%s: Could not add pcm ADSP Stream Callback Control\n",
1956 __func__);
1957
1958 return ret;
1959}
1960
1961static snd_pcm_sframes_t msm_pcm_delay_blk(struct snd_pcm_substream *substream,
1962 struct snd_soc_dai *dai)
1963{
1964 struct snd_pcm_runtime *runtime = substream->runtime;
1965 struct msm_audio *prtd = runtime->private_data;
1966 struct audio_client *ac = prtd->audio_client;
1967 snd_pcm_sframes_t frames;
1968 int ret;
1969
1970 ret = q6asm_get_path_delay(prtd->audio_client);
1971 if (ret) {
1972 pr_err("%s: get_path_delay failed, ret=%d\n", __func__, ret);
1973 return 0;
1974 }
1975
1976 /* convert microseconds to frames */
1977 frames = ac->path_delay / 1000 * runtime->rate / 1000;
1978
1979 /* also convert the remainder from the initial division */
1980 frames += ac->path_delay % 1000 * runtime->rate / 1000000;
1981
1982 /* overcompensate for the loss of precision (empirical) */
1983 frames += 2;
1984
1985 return frames;
1986}
1987
1988static struct snd_soc_platform_driver msm_soc_platform = {
1989 .ops = &msm_pcm_ops,
1990 .pcm_new = msm_asoc_pcm_new,
1991 .delay_blk = msm_pcm_delay_blk,
1992};
1993
1994static int msm_pcm_probe(struct platform_device *pdev)
1995{
1996 int rc;
1997 int id;
1998 struct msm_plat_data *pdata;
1999 const char *latency_level;
2000
2001 rc = of_property_read_u32(pdev->dev.of_node,
2002 "qcom,msm-pcm-dsp-id", &id);
2003 if (rc) {
2004 dev_err(&pdev->dev, "%s: qcom,msm-pcm-dsp-id missing in DT node\n",
2005 __func__);
2006 return rc;
2007 }
2008
2009 pdata = kzalloc(sizeof(struct msm_plat_data), GFP_KERNEL);
2010 if (!pdata)
2011 return -ENOMEM;
2012
2013 if (of_property_read_bool(pdev->dev.of_node,
2014 "qcom,msm-pcm-low-latency")) {
2015
2016 pdata->perf_mode = LOW_LATENCY_PCM_MODE;
2017 rc = of_property_read_string(pdev->dev.of_node,
2018 "qcom,latency-level", &latency_level);
2019 if (!rc) {
2020 if (!strcmp(latency_level, "ultra"))
2021 pdata->perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
2022 else if (!strcmp(latency_level, "ull-pp"))
2023 pdata->perf_mode =
2024 ULL_POST_PROCESSING_PCM_MODE;
2025 }
2026 } else {
2027 pdata->perf_mode = LEGACY_PCM_MODE;
2028 }
2029
2030 dev_set_drvdata(&pdev->dev, pdata);
2031
2032
2033 dev_dbg(&pdev->dev, "%s: dev name %s\n",
2034 __func__, dev_name(&pdev->dev));
2035 return snd_soc_register_platform(&pdev->dev,
2036 &msm_soc_platform);
2037}
2038
2039static int msm_pcm_remove(struct platform_device *pdev)
2040{
2041 struct msm_plat_data *pdata;
2042
2043 pdata = dev_get_drvdata(&pdev->dev);
2044 kfree(pdata);
2045 snd_soc_unregister_platform(&pdev->dev);
2046 return 0;
2047}
2048static const struct of_device_id msm_pcm_dt_match[] = {
2049 {.compatible = "qcom,msm-pcm-dsp"},
2050 {}
2051};
2052MODULE_DEVICE_TABLE(of, msm_pcm_dt_match);
2053
2054static struct platform_driver msm_pcm_driver = {
2055 .driver = {
2056 .name = "msm-pcm-dsp",
2057 .owner = THIS_MODULE,
2058 .of_match_table = msm_pcm_dt_match,
2059 },
2060 .probe = msm_pcm_probe,
2061 .remove = msm_pcm_remove,
2062};
2063
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302064int __init msm_pcm_dsp_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302065{
2066 init_waitqueue_head(&the_locks.enable_wait);
2067 init_waitqueue_head(&the_locks.eos_wait);
2068 init_waitqueue_head(&the_locks.write_wait);
2069 init_waitqueue_head(&the_locks.read_wait);
2070
2071 return platform_driver_register(&msm_pcm_driver);
2072}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302073
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05302074void msm_pcm_dsp_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302075{
2076 platform_driver_unregister(&msm_pcm_driver);
2077}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302078
2079MODULE_DESCRIPTION("PCM module platform driver");
2080MODULE_LICENSE("GPL v2");