blob: bbcbab35957c1f5c5f0a485bb6c12b471e90b39e [file] [log] [blame]
Meng Wangac147b72017-10-30 16:46:16 +08001/* Copyright (c) 2012-2018, 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,
78 .channels_max = 4,
79 .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);
594 break;
595 }
596 /* pending CMD_EOS isn't expected */
597 WARN_ON_ONCE(test_bit(CMD_EOS, &prtd->cmd_pending));
598 set_bit(CMD_EOS, &prtd->cmd_pending);
599 ret = q6asm_cmd_nowait(prtd->audio_client, CMD_EOS);
600 if (ret)
601 clear_bit(CMD_EOS, &prtd->cmd_pending);
602 break;
603 case SNDRV_PCM_TRIGGER_SUSPEND:
604 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
605 pr_debug("SNDRV_PCM_TRIGGER_PAUSE\n");
606 ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
607 atomic_set(&prtd->start, 0);
608 break;
609 default:
610 ret = -EINVAL;
611 break;
612 }
613
614 return ret;
615}
616
617static int msm_pcm_open(struct snd_pcm_substream *substream)
618{
619 struct snd_pcm_runtime *runtime = substream->runtime;
620 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
621 struct msm_audio *prtd;
Banajit Goswami616e68c2017-10-27 01:31:19 -0700622 struct msm_plat_data *pdata;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530623 int ret = 0;
624
Banajit Goswami616e68c2017-10-27 01:31:19 -0700625 pdata = (struct msm_plat_data *)
626 dev_get_drvdata(soc_prtd->platform->dev);
627 if (!pdata) {
628 pr_err("%s: platform data not populated\n", __func__);
629 return -EINVAL;
630 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530631 prtd = kzalloc(sizeof(struct msm_audio), GFP_KERNEL);
632 if (prtd == NULL)
633 return -ENOMEM;
634
635 prtd->substream = substream;
636 prtd->audio_client = q6asm_audio_client_alloc(
637 (app_cb)event_handler, prtd);
638 if (!prtd->audio_client) {
639 pr_info("%s: Could not allocate memory\n", __func__);
640 kfree(prtd);
641 return -ENOMEM;
642 }
643
644 prtd->audio_client->dev = soc_prtd->platform->dev;
645
646 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
647 runtime->hw = msm_pcm_hardware_playback;
648
649 /* Capture path */
650 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
651 runtime->hw = msm_pcm_hardware_capture;
652 else {
653 pr_err("Invalid Stream type %d\n", substream->stream);
654 return -EINVAL;
655 }
656
657 ret = snd_pcm_hw_constraint_list(runtime, 0,
658 SNDRV_PCM_HW_PARAM_RATE,
659 &constraints_sample_rates);
660 if (ret < 0)
661 pr_info("snd_pcm_hw_constraint_list failed\n");
662 /* Ensure that buffer size is a multiple of period size */
663 ret = snd_pcm_hw_constraint_integer(runtime,
664 SNDRV_PCM_HW_PARAM_PERIODS);
665 if (ret < 0)
666 pr_info("snd_pcm_hw_constraint_integer failed\n");
667
668 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
669 ret = snd_pcm_hw_constraint_minmax(runtime,
670 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
671 PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE,
672 PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE);
673 if (ret < 0) {
674 pr_err("constraint for buffer bytes min max ret = %d\n",
675 ret);
676 }
677 }
678
679 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
680 ret = snd_pcm_hw_constraint_minmax(runtime,
681 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
682 CAPTURE_MIN_NUM_PERIODS * CAPTURE_MIN_PERIOD_SIZE,
683 CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE);
684 if (ret < 0) {
685 pr_err("constraint for buffer bytes min max ret = %d\n",
686 ret);
687 }
688 }
689 ret = snd_pcm_hw_constraint_step(runtime, 0,
690 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
691 if (ret < 0) {
692 pr_err("constraint for period bytes step ret = %d\n",
693 ret);
694 }
695 ret = snd_pcm_hw_constraint_step(runtime, 0,
696 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
697 if (ret < 0) {
698 pr_err("constraint for buffer bytes step ret = %d\n",
699 ret);
700 }
701
702 prtd->enabled = IDLE;
703 prtd->dsp_cnt = 0;
704 prtd->set_channel_map = false;
705 prtd->reset_event = false;
706 runtime->private_data = prtd;
707 msm_adsp_init_mixer_ctl_pp_event_queue(soc_prtd);
Banajit Goswami616e68c2017-10-27 01:31:19 -0700708 /* Vote to update the Rx thread priority to RT Thread for playback */
709 if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) &&
710 (pdata->perf_mode == LOW_LATENCY_PCM_MODE))
711 apr_start_rx_rt(prtd->audio_client->apr);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530712
713 return 0;
714}
715
716static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
Meng Wangac147b72017-10-30 16:46:16 +0800717 unsigned long hwoff, void __user *buf, unsigned long fbytes)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530718{
719 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530720 int xfer = 0;
721 char *bufptr = NULL;
722 void *data = NULL;
723 uint32_t idx = 0;
724 uint32_t size = 0;
725 uint32_t retries = 0;
726
727 struct snd_pcm_runtime *runtime = substream->runtime;
728 struct msm_audio *prtd = runtime->private_data;
729
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530730 pr_debug("%s: prtd->out_count = %d\n",
731 __func__, atomic_read(&prtd->out_count));
732
733 while ((fbytes > 0) && (retries < MAX_PB_COPY_RETRIES)) {
734 if (prtd->reset_event) {
735 pr_err("%s: In SSR return ENETRESET before wait\n",
736 __func__);
737 return -ENETRESET;
738 }
739
740 ret = wait_event_timeout(the_locks.write_wait,
741 (atomic_read(&prtd->out_count)), 5 * HZ);
742 if (!ret) {
743 pr_err("%s: wait_event_timeout failed\n", __func__);
744 ret = -ETIMEDOUT;
745 goto fail;
746 }
747 ret = 0;
748
749 if (prtd->reset_event) {
750 pr_err("%s: In SSR return ENETRESET after wait\n",
751 __func__);
752 return -ENETRESET;
753 }
754
755 if (!atomic_read(&prtd->out_count)) {
756 pr_err("%s: pcm stopped out_count 0\n", __func__);
757 return 0;
758 }
759
760 data = q6asm_is_cpu_buf_avail(IN, prtd->audio_client, &size,
761 &idx);
762 if (data == NULL) {
763 retries++;
764 continue;
765 } else {
766 retries = 0;
767 }
768
769 if (fbytes > size)
770 xfer = size;
771 else
772 xfer = fbytes;
773
774 bufptr = data;
775 if (bufptr) {
Meng Wangac147b72017-10-30 16:46:16 +0800776 pr_debug("%s:fbytes =%lu: xfer=%d size=%d\n",
777 __func__, fbytes, xfer, size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530778 if (copy_from_user(bufptr, buf, xfer)) {
779 ret = -EFAULT;
780 pr_err("%s: copy_from_user failed\n",
781 __func__);
782 q6asm_cpu_buf_release(IN, prtd->audio_client);
783 goto fail;
784 }
785 buf += xfer;
786 fbytes -= xfer;
Meng Wangac147b72017-10-30 16:46:16 +0800787 pr_debug("%s:fbytes = %lu: xfer=%d\n", __func__,
788 fbytes, xfer);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530789 if (atomic_read(&prtd->start)) {
790 pr_debug("%s:writing %d bytes of buffer to dsp\n",
791 __func__, xfer);
792 ret = q6asm_write(prtd->audio_client, xfer,
793 0, 0, NO_TIMESTAMP);
794 if (ret < 0) {
795 ret = -EFAULT;
796 q6asm_cpu_buf_release(IN,
797 prtd->audio_client);
798 goto fail;
799 }
800 } else
801 atomic_inc(&prtd->out_needed);
802 atomic_dec(&prtd->out_count);
803 }
804 }
805fail:
806 if (retries >= MAX_PB_COPY_RETRIES)
807 ret = -ENOMEM;
808
809 return ret;
810}
811
812static int msm_pcm_playback_close(struct snd_pcm_substream *substream)
813{
814 struct snd_pcm_runtime *runtime = substream->runtime;
815 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
816 struct msm_audio *prtd = runtime->private_data;
Banajit Goswami616e68c2017-10-27 01:31:19 -0700817 struct msm_plat_data *pdata;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530818 uint32_t timeout;
819 int dir = 0;
820 int ret = 0;
821
822 pr_debug("%s: cmd_pending 0x%lx\n", __func__, prtd->cmd_pending);
823
824 if (prtd->audio_client) {
825 dir = IN;
826
Banajit Goswami616e68c2017-10-27 01:31:19 -0700827 /*
828 * Unvote to downgrade the Rx thread priority from
829 * RT Thread for Low-Latency use case.
830 */
831 pdata = (struct msm_plat_data *)
832 dev_get_drvdata(soc_prtd->platform->dev);
833 if (pdata) {
834 if (pdata->perf_mode == LOW_LATENCY_PCM_MODE)
835 apr_end_rx_rt(prtd->audio_client->apr);
836 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530837 /* determine timeout length */
838 if (runtime->frame_bits == 0 || runtime->rate == 0) {
839 timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
840 } else {
841 timeout = (runtime->period_size *
842 CMD_EOS_TIMEOUT_MULTIPLIER) /
843 ((runtime->frame_bits / 8) *
844 runtime->rate);
845 if (timeout < CMD_EOS_MIN_TIMEOUT_LENGTH)
846 timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
847 }
848 pr_debug("%s: CMD_EOS timeout is %d\n", __func__, timeout);
849
850 ret = wait_event_timeout(the_locks.eos_wait,
851 !test_bit(CMD_EOS, &prtd->cmd_pending),
852 timeout);
853 if (!ret)
854 pr_err("%s: CMD_EOS failed, cmd_pending 0x%lx\n",
855 __func__, prtd->cmd_pending);
856 q6asm_cmd(prtd->audio_client, CMD_CLOSE);
857 q6asm_audio_client_buf_free_contiguous(dir,
858 prtd->audio_client);
859 q6asm_audio_client_free(prtd->audio_client);
860 }
861 msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
862 SNDRV_PCM_STREAM_PLAYBACK);
863 msm_adsp_clean_mixer_ctl_pp_event_queue(soc_prtd);
864 kfree(prtd);
865 runtime->private_data = NULL;
866
867 return 0;
868}
869
870static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
Meng Wangac147b72017-10-30 16:46:16 +0800871 int channel, unsigned long hwoff, void __user *buf,
872 unsigned long fbytes)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530873{
874 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530875 int xfer;
876 char *bufptr;
877 void *data = NULL;
878 static uint32_t idx;
879 static uint32_t size;
880 uint32_t offset = 0;
881 struct snd_pcm_runtime *runtime = substream->runtime;
882 struct msm_audio *prtd = substream->runtime->private_data;
883
884
885 pr_debug("%s\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530886
887 pr_debug("appl_ptr %d\n", (int)runtime->control->appl_ptr);
888 pr_debug("hw_ptr %d\n", (int)runtime->status->hw_ptr);
889 pr_debug("avail_min %d\n", (int)runtime->control->avail_min);
890
891 if (prtd->reset_event) {
892 pr_err("%s: In SSR return ENETRESET before wait\n", __func__);
893 return -ENETRESET;
894 }
895 ret = wait_event_timeout(the_locks.read_wait,
896 (atomic_read(&prtd->in_count)), 5 * HZ);
897 if (!ret) {
898 pr_debug("%s: wait_event_timeout failed\n", __func__);
899 goto fail;
900 }
901 if (prtd->reset_event) {
902 pr_err("%s: In SSR return ENETRESET after wait\n", __func__);
903 return -ENETRESET;
904 }
905 if (!atomic_read(&prtd->in_count)) {
906 pr_debug("%s: pcm stopped in_count 0\n", __func__);
907 return 0;
908 }
909 pr_debug("Checking if valid buffer is available...%pK\n",
910 data);
911 data = q6asm_is_cpu_buf_avail(OUT, prtd->audio_client, &size, &idx);
912 bufptr = data;
913 pr_debug("Size = %d\n", size);
Meng Wangac147b72017-10-30 16:46:16 +0800914 pr_debug("fbytes = %lu\n", fbytes);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530915 pr_debug("idx = %d\n", idx);
916 if (bufptr) {
917 xfer = fbytes;
918 if (xfer > size)
919 xfer = size;
920 offset = prtd->in_frame_info[idx].offset;
921 pr_debug("Offset value = %d\n", offset);
922 if (copy_to_user(buf, bufptr+offset, xfer)) {
923 pr_err("Failed to copy buf to user\n");
924 ret = -EFAULT;
925 q6asm_cpu_buf_release(OUT, prtd->audio_client);
926 goto fail;
927 }
928 fbytes -= xfer;
929 size -= xfer;
930 prtd->in_frame_info[idx].offset += xfer;
Meng Wangac147b72017-10-30 16:46:16 +0800931 pr_debug("%s:fbytes = %lu: size=%d: xfer=%d\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530932 __func__, fbytes, size, xfer);
933 pr_debug(" Sending next buffer to dsp\n");
934 memset(&prtd->in_frame_info[idx], 0,
935 sizeof(struct msm_audio_in_frame_info));
936 atomic_dec(&prtd->in_count);
937 ret = q6asm_read(prtd->audio_client);
938 if (ret < 0) {
939 pr_err("q6asm read failed\n");
940 ret = -EFAULT;
941 q6asm_cpu_buf_release(OUT, prtd->audio_client);
942 goto fail;
943 }
944 } else
945 pr_err("No valid buffer\n");
946
947 pr_debug("Returning from capture_copy... %d\n", ret);
948fail:
949 return ret;
950}
951
952static int msm_pcm_capture_close(struct snd_pcm_substream *substream)
953{
954 struct snd_pcm_runtime *runtime = substream->runtime;
955 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
956 struct msm_audio *prtd = runtime->private_data;
957 int dir = OUT;
958
959 pr_debug("%s\n", __func__);
960 if (prtd->audio_client) {
961 q6asm_cmd(prtd->audio_client, CMD_CLOSE);
962 q6asm_audio_client_buf_free_contiguous(dir,
963 prtd->audio_client);
964 q6asm_audio_client_free(prtd->audio_client);
965 }
966
967 msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
968 SNDRV_PCM_STREAM_CAPTURE);
969 kfree(prtd);
970 runtime->private_data = NULL;
971
972 return 0;
973}
974
975static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
Meng Wangac147b72017-10-30 16:46:16 +0800976 unsigned long hwoff, void __user *buf, unsigned long fbytes)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530977{
978 int ret = 0;
979
980 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Meng Wangac147b72017-10-30 16:46:16 +0800981 ret = msm_pcm_playback_copy(substream, a, hwoff, buf, fbytes);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530982 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
Meng Wangac147b72017-10-30 16:46:16 +0800983 ret = msm_pcm_capture_copy(substream, a, hwoff, buf, fbytes);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530984 return ret;
985}
986
987static int msm_pcm_close(struct snd_pcm_substream *substream)
988{
989 int ret = 0;
990
991 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
992 ret = msm_pcm_playback_close(substream);
993 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
994 ret = msm_pcm_capture_close(substream);
995 return ret;
996}
997
998static int msm_pcm_prepare(struct snd_pcm_substream *substream)
999{
1000 int ret = 0;
1001
1002 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1003 ret = msm_pcm_playback_prepare(substream);
1004 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1005 ret = msm_pcm_capture_prepare(substream);
1006 return ret;
1007}
1008
1009static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
1010{
1011
1012 struct snd_pcm_runtime *runtime = substream->runtime;
1013 struct msm_audio *prtd = runtime->private_data;
1014
1015 if (prtd->pcm_irq_pos >= prtd->pcm_size)
1016 prtd->pcm_irq_pos = 0;
1017
1018 pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos);
1019 return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
1020}
1021
1022static int msm_pcm_mmap(struct snd_pcm_substream *substream,
1023 struct vm_area_struct *vma)
1024{
1025 struct snd_pcm_runtime *runtime = substream->runtime;
1026 struct msm_audio *prtd = runtime->private_data;
1027 struct audio_client *ac = prtd->audio_client;
1028 struct audio_port_data *apd = ac->port;
1029 struct audio_buffer *ab;
1030 int dir = -1;
1031
1032 prtd->mmap_flag = 1;
1033
1034 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1035 dir = IN;
1036 else
1037 dir = OUT;
1038 ab = &(apd[dir].buf[0]);
1039
1040 return msm_audio_ion_mmap(ab, vma);
1041}
1042
1043static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
1044 struct snd_pcm_hw_params *params)
1045{
1046 struct snd_pcm_runtime *runtime = substream->runtime;
1047 struct msm_audio *prtd = runtime->private_data;
1048 struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
1049 struct audio_buffer *buf;
1050 int dir, ret;
1051
1052 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1053 dir = IN;
1054 else
1055 dir = OUT;
1056 ret = q6asm_audio_client_buf_alloc_contiguous(dir,
1057 prtd->audio_client,
1058 (params_buffer_bytes(params) / params_periods(params)),
1059 params_periods(params));
1060 if (ret < 0) {
1061 pr_err("Audio Start: Buffer Allocation failed rc = %d\n",
1062 ret);
1063 return -ENOMEM;
1064 }
1065 buf = prtd->audio_client->port[dir].buf;
1066 if (buf == NULL || buf[0].data == NULL)
1067 return -ENOMEM;
1068
1069 pr_debug("%s:buf = %pK\n", __func__, buf);
1070 dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
1071 dma_buf->dev.dev = substream->pcm->card->dev;
1072 dma_buf->private_data = NULL;
1073 dma_buf->area = buf[0].data;
1074 dma_buf->addr = buf[0].phys;
1075 dma_buf->bytes = params_buffer_bytes(params);
1076 if (!dma_buf->area)
1077 return -ENOMEM;
1078
1079 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
1080 return 0;
1081}
1082
1083static const struct snd_pcm_ops msm_pcm_ops = {
1084 .open = msm_pcm_open,
Meng Wangac147b72017-10-30 16:46:16 +08001085 .copy_user = msm_pcm_copy,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301086 .hw_params = msm_pcm_hw_params,
1087 .close = msm_pcm_close,
1088 .ioctl = snd_pcm_lib_ioctl,
1089 .prepare = msm_pcm_prepare,
1090 .trigger = msm_pcm_trigger,
1091 .pointer = msm_pcm_pointer,
1092 .mmap = msm_pcm_mmap,
1093};
1094
1095static int msm_pcm_adsp_stream_cmd_put(struct snd_kcontrol *kcontrol,
1096 struct snd_ctl_elem_value *ucontrol)
1097{
1098 struct snd_soc_component *pcm = snd_kcontrol_chip(kcontrol);
1099 struct snd_soc_platform *platform = snd_soc_component_to_platform(pcm);
1100 struct msm_plat_data *pdata = dev_get_drvdata(platform->dev);
1101 struct snd_pcm_substream *substream;
1102 struct msm_audio *prtd;
1103 int ret = 0;
1104 struct msm_adsp_event_data *event_data = NULL;
1105
1106 if (!pdata) {
1107 pr_err("%s pdata is NULL\n", __func__);
1108 ret = -ENODEV;
1109 goto done;
1110 }
1111
1112 substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1113 if (!substream) {
1114 pr_err("%s substream not found\n", __func__);
1115 ret = -EINVAL;
1116 goto done;
1117 }
1118
1119 if (!substream->runtime) {
1120 pr_err("%s substream runtime not found\n", __func__);
1121 ret = -EINVAL;
1122 goto done;
1123 }
1124
1125 prtd = substream->runtime->private_data;
1126 if (prtd->audio_client == NULL) {
1127 pr_err("%s prtd is null.\n", __func__);
1128 ret = -EINVAL;
1129 goto done;
1130 }
1131
1132 event_data = (struct msm_adsp_event_data *)ucontrol->value.bytes.data;
1133 if ((event_data->event_type < ADSP_STREAM_PP_EVENT) ||
1134 (event_data->event_type >= ADSP_STREAM_EVENT_MAX)) {
1135 pr_err("%s: invalid event_type=%d",
1136 __func__, event_data->event_type);
1137 ret = -EINVAL;
1138 goto done;
1139 }
1140
1141 if ((sizeof(struct msm_adsp_event_data) + event_data->payload_len) >=
1142 sizeof(ucontrol->value.bytes.data)) {
1143 pr_err("%s param length=%d exceeds limit",
1144 __func__, event_data->payload_len);
1145 ret = -EINVAL;
1146 goto done;
1147 }
1148
1149 ret = q6asm_send_stream_cmd(prtd->audio_client, event_data);
1150 if (ret < 0)
1151 pr_err("%s: failed to send stream event cmd, err = %d\n",
1152 __func__, ret);
1153done:
1154 return ret;
1155}
1156
1157static int msm_pcm_add_audio_adsp_stream_cmd_control(
1158 struct snd_soc_pcm_runtime *rtd)
1159{
1160 const char *mixer_ctl_name = DSP_STREAM_CMD;
1161 const char *deviceNo = "NN";
1162 char *mixer_str = NULL;
1163 int ctl_len = 0, ret = 0;
1164 struct snd_kcontrol_new fe_audio_adsp_stream_cmd_config_control[1] = {
1165 {
1166 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1167 .name = "?",
1168 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1169 .info = msm_adsp_stream_cmd_info,
1170 .put = msm_pcm_adsp_stream_cmd_put,
1171 .private_value = 0,
1172 }
1173 };
1174
1175 if (!rtd) {
1176 pr_err("%s rtd is NULL\n", __func__);
1177 ret = -EINVAL;
1178 goto done;
1179 }
1180
1181 ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
1182 mixer_str = kzalloc(ctl_len, GFP_KERNEL);
1183 if (!mixer_str) {
1184 ret = -ENOMEM;
1185 goto done;
1186 }
1187
1188 snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
1189 fe_audio_adsp_stream_cmd_config_control[0].name = mixer_str;
1190 fe_audio_adsp_stream_cmd_config_control[0].private_value =
1191 rtd->dai_link->id;
1192 pr_debug("Registering new mixer ctl %s\n", mixer_str);
1193 ret = snd_soc_add_platform_controls(rtd->platform,
1194 fe_audio_adsp_stream_cmd_config_control,
1195 ARRAY_SIZE(fe_audio_adsp_stream_cmd_config_control));
1196 if (ret < 0)
1197 pr_err("%s: failed add ctl %s. err = %d\n",
1198 __func__, mixer_str, ret);
1199
1200 kfree(mixer_str);
1201done:
1202 return ret;
1203}
1204
1205static int msm_pcm_add_audio_adsp_stream_callback_control(
1206 struct snd_soc_pcm_runtime *rtd)
1207{
1208 const char *mixer_ctl_name = DSP_STREAM_CALLBACK;
1209 const char *deviceNo = "NN";
1210 char *mixer_str = NULL;
1211 int ctl_len = 0, ret = 0;
1212 struct snd_kcontrol *kctl;
1213
1214 struct snd_kcontrol_new fe_audio_adsp_callback_config_control[1] = {
1215 {
1216 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1217 .name = "?",
1218 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1219 .info = msm_adsp_stream_callback_info,
1220 .get = msm_adsp_stream_callback_get,
1221 .private_value = 0,
1222 }
1223 };
1224
1225 if (!rtd) {
1226 pr_err("%s NULL rtd\n", __func__);
1227 ret = -EINVAL;
1228 goto done;
1229 }
1230
1231 pr_debug("%s: added new pcm FE with name %s, id %d, cpu dai %s, device no %d\n",
1232 __func__, rtd->dai_link->name, rtd->dai_link->id,
1233 rtd->dai_link->cpu_dai_name, rtd->pcm->device);
1234 ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
1235 mixer_str = kzalloc(ctl_len, GFP_KERNEL);
1236 if (!mixer_str) {
1237 ret = -ENOMEM;
1238 goto done;
1239 }
1240
1241 snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
1242 fe_audio_adsp_callback_config_control[0].name = mixer_str;
1243 fe_audio_adsp_callback_config_control[0].private_value =
1244 rtd->dai_link->id;
1245 pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
1246 ret = snd_soc_add_platform_controls(rtd->platform,
1247 fe_audio_adsp_callback_config_control,
1248 ARRAY_SIZE(fe_audio_adsp_callback_config_control));
1249 if (ret < 0) {
1250 pr_err("%s: failed to add ctl %s. err = %d\n",
1251 __func__, mixer_str, ret);
1252 ret = -EINVAL;
1253 goto free_mixer_str;
1254 }
1255
1256 kctl = snd_soc_card_get_kcontrol(rtd->card, mixer_str);
1257 if (!kctl) {
1258 pr_err("%s: failed to get kctl %s.\n", __func__, mixer_str);
1259 ret = -EINVAL;
1260 goto free_mixer_str;
1261 }
1262
1263 kctl->private_data = NULL;
1264
1265free_mixer_str:
1266 kfree(mixer_str);
1267done:
1268 return ret;
1269}
1270
1271static int msm_pcm_set_volume(struct msm_audio *prtd, uint32_t volume)
1272{
1273 int rc = 0;
1274
1275 if (prtd && prtd->audio_client) {
1276 pr_debug("%s: channels %d volume 0x%x\n", __func__,
1277 prtd->channel_mode, volume);
1278 rc = q6asm_set_volume(prtd->audio_client, volume);
1279 if (rc < 0) {
1280 pr_err("%s: Send Volume command failed rc=%d\n",
1281 __func__, rc);
1282 }
1283 }
1284 return rc;
1285}
1286
1287static int msm_pcm_volume_ctl_get(struct snd_kcontrol *kcontrol,
1288 struct snd_ctl_elem_value *ucontrol)
1289{
1290 struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
1291 struct snd_pcm_substream *substream =
1292 vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1293 struct msm_audio *prtd;
1294
1295 pr_debug("%s\n", __func__);
1296 if (!substream) {
1297 pr_err("%s substream not found\n", __func__);
1298 return -ENODEV;
1299 }
1300 if (!substream->runtime) {
1301 pr_err("%s substream runtime not found\n", __func__);
1302 return 0;
1303 }
1304 prtd = substream->runtime->private_data;
1305 if (prtd)
1306 ucontrol->value.integer.value[0] = prtd->volume;
1307 return 0;
1308}
1309
1310static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
1311 struct snd_ctl_elem_value *ucontrol)
1312{
1313 int rc = 0;
1314 struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
1315 struct snd_pcm_substream *substream =
1316 vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1317 struct msm_audio *prtd;
1318 int volume = ucontrol->value.integer.value[0];
1319
1320 pr_debug("%s: volume : 0x%x\n", __func__, volume);
1321 if (!substream) {
1322 pr_err("%s substream not found\n", __func__);
1323 return -ENODEV;
1324 }
1325 if (!substream->runtime) {
1326 pr_err("%s substream runtime not found\n", __func__);
1327 return 0;
1328 }
1329 prtd = substream->runtime->private_data;
1330 if (prtd) {
1331 rc = msm_pcm_set_volume(prtd, volume);
1332 prtd->volume = volume;
1333 }
1334 return rc;
1335}
1336
1337static int msm_pcm_add_volume_control(struct snd_soc_pcm_runtime *rtd)
1338{
1339 int ret = 0;
1340 struct snd_pcm *pcm = rtd->pcm;
1341 struct snd_pcm_volume *volume_info;
1342 struct snd_kcontrol *kctl;
1343
1344 dev_dbg(rtd->dev, "%s, Volume control add\n", __func__);
1345 ret = snd_pcm_add_volume_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1346 NULL, 1, rtd->dai_link->id,
1347 &volume_info);
1348 if (ret < 0) {
1349 pr_err("%s volume control failed ret %d\n", __func__, ret);
1350 return ret;
1351 }
1352 kctl = volume_info->kctl;
1353 kctl->put = msm_pcm_volume_ctl_put;
1354 kctl->get = msm_pcm_volume_ctl_get;
1355 kctl->tlv.p = msm_pcm_vol_gain;
1356 return 0;
1357}
1358
1359static int msm_pcm_compress_ctl_info(struct snd_kcontrol *kcontrol,
1360 struct snd_ctl_elem_info *uinfo)
1361{
1362 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1363 uinfo->count = 1;
1364 uinfo->value.integer.min = 0;
1365 uinfo->value.integer.max = 0x2000;
1366 return 0;
1367}
1368
1369static int msm_pcm_compress_ctl_get(struct snd_kcontrol *kcontrol,
1370 struct snd_ctl_elem_value *ucontrol)
1371{
1372 struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
1373 struct snd_soc_platform *platform = snd_soc_component_to_platform(comp);
1374 struct msm_plat_data *pdata = dev_get_drvdata(platform->dev);
1375 struct snd_pcm_substream *substream;
1376 struct msm_audio *prtd;
1377
1378 if (!pdata) {
1379 pr_err("%s pdata is NULL\n", __func__);
1380 return -ENODEV;
1381 }
1382 substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1383 if (!substream) {
1384 pr_err("%s substream not found\n", __func__);
1385 return -EINVAL;
1386 }
1387 if (!substream->runtime) {
1388 pr_err("%s substream runtime not found\n", __func__);
1389 return 0;
1390 }
1391 prtd = substream->runtime->private_data;
1392 if (prtd)
1393 ucontrol->value.integer.value[0] = prtd->compress_enable;
1394 return 0;
1395}
1396
1397static int msm_pcm_compress_ctl_put(struct snd_kcontrol *kcontrol,
1398 struct snd_ctl_elem_value *ucontrol)
1399{
1400 int rc = 0;
1401 struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
1402 struct snd_soc_platform *platform = snd_soc_component_to_platform(comp);
1403 struct msm_plat_data *pdata = dev_get_drvdata(platform->dev);
1404 struct snd_pcm_substream *substream;
1405 struct msm_audio *prtd;
1406 int compress = ucontrol->value.integer.value[0];
1407
1408 if (!pdata) {
1409 pr_err("%s pdata is NULL\n", __func__);
1410 return -ENODEV;
1411 }
1412 substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1413 pr_debug("%s: compress : 0x%x\n", __func__, compress);
1414 if (!substream) {
1415 pr_err("%s substream not found\n", __func__);
1416 return -EINVAL;
1417 }
1418 if (!substream->runtime) {
1419 pr_err("%s substream runtime not found\n", __func__);
1420 return 0;
1421 }
1422 prtd = substream->runtime->private_data;
1423 if (prtd) {
1424 pr_debug("%s: setting compress flag to 0x%x\n",
1425 __func__, compress);
1426 prtd->compress_enable = compress;
1427 }
1428 return rc;
1429}
1430
1431static int msm_pcm_add_compress_control(struct snd_soc_pcm_runtime *rtd)
1432{
1433 const char *mixer_ctl_name = "Playback ";
1434 const char *mixer_ctl_end_name = " Compress";
1435 const char *deviceNo = "NN";
1436 char *mixer_str = NULL;
1437 int ctl_len;
1438 int ret = 0;
1439 struct msm_plat_data *pdata;
1440 struct snd_kcontrol_new pcm_compress_control[1] = {
1441 {
1442 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1443 .name = "?",
1444 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1445 .info = msm_pcm_compress_ctl_info,
1446 .get = msm_pcm_compress_ctl_get,
1447 .put = msm_pcm_compress_ctl_put,
1448 .private_value = 0,
1449 }
1450 };
1451
1452 if (!rtd) {
1453 pr_err("%s: NULL rtd\n", __func__);
1454 return -EINVAL;
1455 }
1456
1457 ctl_len = strlen(mixer_ctl_name) + strlen(deviceNo) +
1458 strlen(mixer_ctl_end_name) + 1;
1459 mixer_str = kzalloc(ctl_len, GFP_KERNEL);
1460
1461 if (!mixer_str)
1462 return -ENOMEM;
1463
1464 snprintf(mixer_str, ctl_len, "%s%d%s", mixer_ctl_name,
1465 rtd->pcm->device, mixer_ctl_end_name);
1466
1467 pcm_compress_control[0].name = mixer_str;
1468 pcm_compress_control[0].private_value = rtd->dai_link->id;
1469 pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
1470 pdata = dev_get_drvdata(rtd->platform->dev);
1471 if (pdata) {
1472 if (!pdata->pcm) {
1473 pdata->pcm = rtd->pcm;
1474 snd_soc_add_platform_controls(rtd->platform,
1475 pcm_compress_control,
1476 ARRAY_SIZE
1477 (pcm_compress_control));
1478 pr_debug("%s: add control success plt = %pK\n",
1479 __func__, rtd->platform);
1480 }
1481 } else {
1482 pr_err("%s: NULL pdata\n", __func__);
1483 ret = -EINVAL;
1484 }
1485 kfree(mixer_str);
1486 return ret;
1487}
1488
1489static int msm_pcm_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1490 struct snd_ctl_elem_value *ucontrol)
1491{
1492 int i;
1493 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1494 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1495 struct snd_pcm_substream *substream;
1496 struct msm_audio *prtd;
1497
1498 pr_debug("%s", __func__);
1499 substream = snd_pcm_chmap_substream(info, idx);
1500 if (!substream)
1501 return -ENODEV;
1502 if (!substream->runtime)
1503 return 0;
1504
1505 prtd = substream->runtime->private_data;
1506 if (prtd) {
1507 prtd->set_channel_map = true;
1508 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
1509 prtd->channel_map[i] =
1510 (char)(ucontrol->value.integer.value[i]);
1511 }
1512 return 0;
1513}
1514
1515static int msm_pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1516 struct snd_ctl_elem_value *ucontrol)
1517{
1518 int i;
1519 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1520 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1521 struct snd_pcm_substream *substream;
1522 struct msm_audio *prtd;
1523
1524 pr_debug("%s", __func__);
1525 substream = snd_pcm_chmap_substream(info, idx);
1526 if (!substream)
1527 return -ENODEV;
1528 memset(ucontrol->value.integer.value, 0,
1529 sizeof(ucontrol->value.integer.value));
1530 if (!substream->runtime)
1531 return 0; /* no channels set */
1532
1533 prtd = substream->runtime->private_data;
1534
1535 if (prtd && prtd->set_channel_map == true) {
1536 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
1537 ucontrol->value.integer.value[i] =
1538 (int)prtd->channel_map[i];
1539 } else {
1540 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
1541 ucontrol->value.integer.value[i] = 0;
1542 }
1543
1544 return 0;
1545}
1546
1547static int msm_pcm_add_chmap_controls(struct snd_soc_pcm_runtime *rtd)
1548{
1549 struct snd_pcm *pcm = rtd->pcm;
1550 struct snd_pcm_chmap *chmap_info;
1551 struct snd_kcontrol *kctl;
1552 char device_num[12];
1553 int i, ret = 0;
1554
1555 pr_debug("%s, Channel map cntrl add\n", __func__);
1556 ret = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1557 snd_pcm_std_chmaps,
1558 PCM_FORMAT_MAX_NUM_CHANNEL, 0,
1559 &chmap_info);
1560 if (ret < 0) {
1561 pr_err("%s, channel map cntrl add failed\n", __func__);
1562 return ret;
1563 }
1564 kctl = chmap_info->kctl;
1565 for (i = 0; i < kctl->count; i++)
1566 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1567 snprintf(device_num, sizeof(device_num), "%d", pcm->device);
1568 strlcat(kctl->id.name, device_num, sizeof(kctl->id.name));
1569 pr_debug("%s, Overwriting channel map control name to: %s\n",
1570 __func__, kctl->id.name);
1571 kctl->put = msm_pcm_chmap_ctl_put;
1572 kctl->get = msm_pcm_chmap_ctl_get;
1573 return 0;
1574}
1575
1576static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
1577 struct snd_ctl_elem_value *ucontrol)
1578{
1579 u64 fe_id = kcontrol->private_value;
1580 int session_type = SESSION_TYPE_RX;
1581 int be_id = ucontrol->value.integer.value[3];
1582 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
1583 int ret = 0;
1584
1585 cfg_data.app_type = ucontrol->value.integer.value[0];
1586 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
1587 if (ucontrol->value.integer.value[2] != 0)
1588 cfg_data.sample_rate = ucontrol->value.integer.value[2];
1589 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
1590 __func__, fe_id, session_type, be_id,
1591 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1592 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
1593 be_id, &cfg_data);
1594 if (ret < 0)
1595 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
1596 __func__, ret);
1597
1598 return ret;
1599}
1600
1601static int msm_pcm_playback_app_type_cfg_ctl_get(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 = 0;
1607 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
1608 int ret = 0;
1609
1610 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
1611 &be_id, &cfg_data);
1612 if (ret < 0) {
1613 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
1614 __func__, ret);
1615 goto done;
1616 }
1617
1618 ucontrol->value.integer.value[0] = cfg_data.app_type;
1619 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
1620 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
1621 ucontrol->value.integer.value[3] = be_id;
1622 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
1623 __func__, fe_id, session_type, be_id,
1624 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1625done:
1626 return ret;
1627}
1628
1629static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
1630 struct snd_ctl_elem_value *ucontrol)
1631{
1632 u64 fe_id = kcontrol->private_value;
1633 int session_type = SESSION_TYPE_TX;
1634 int be_id = ucontrol->value.integer.value[3];
1635 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
1636 int ret = 0;
1637
1638 cfg_data.app_type = ucontrol->value.integer.value[0];
1639 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
1640 if (ucontrol->value.integer.value[2] != 0)
1641 cfg_data.sample_rate = ucontrol->value.integer.value[2];
1642 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
1643 __func__, fe_id, session_type, be_id,
1644 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1645 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
1646 be_id, &cfg_data);
1647 if (ret < 0)
1648 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
1649 __func__, ret);
1650
1651 return ret;
1652}
1653
1654static int msm_pcm_capture_app_type_cfg_ctl_get(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 = 0;
1660 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
1661 int ret = 0;
1662
1663 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
1664 &be_id, &cfg_data);
1665 if (ret < 0) {
1666 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
1667 __func__, ret);
1668 goto done;
1669 }
1670
1671 ucontrol->value.integer.value[0] = cfg_data.app_type;
1672 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
1673 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
1674 ucontrol->value.integer.value[3] = be_id;
1675 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
1676 __func__, fe_id, session_type, be_id,
1677 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1678done:
1679 return ret;
1680}
1681
1682static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
1683{
1684 struct snd_pcm *pcm = rtd->pcm;
1685 struct snd_pcm_usr *app_type_info;
1686 struct snd_kcontrol *kctl;
1687 const char *playback_mixer_ctl_name = "Audio Stream";
1688 const char *capture_mixer_ctl_name = "Audio Stream Capture";
1689 const char *deviceNo = "NN";
1690 const char *suffix = "App Type Cfg";
1691 int ctl_len, ret = 0;
1692
1693 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
1694 ctl_len = strlen(playback_mixer_ctl_name) + 1 +
1695 strlen(deviceNo) + 1 + strlen(suffix) + 1;
1696 pr_debug("%s: Playback app type cntrl add\n", __func__);
1697 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1698 NULL, 1, ctl_len, rtd->dai_link->id,
1699 &app_type_info);
1700 if (ret < 0) {
1701 pr_err("%s: playback app type cntrl add failed: %d\n",
1702 __func__, ret);
1703 return ret;
1704 }
1705 kctl = app_type_info->kctl;
1706 snprintf(kctl->id.name, ctl_len, "%s %d %s",
1707 playback_mixer_ctl_name, rtd->pcm->device, suffix);
1708 kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
1709 kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
1710 }
1711
1712 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
1713 ctl_len = strlen(capture_mixer_ctl_name) + 1 +
1714 strlen(deviceNo) + 1 + strlen(suffix) + 1;
1715 pr_debug("%s: Capture app type cntrl add\n", __func__);
1716 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
1717 NULL, 1, ctl_len, rtd->dai_link->id,
1718 &app_type_info);
1719 if (ret < 0) {
1720 pr_err("%s: capture app type cntrl add failed: %d\n",
1721 __func__, ret);
1722 return ret;
1723 }
1724 kctl = app_type_info->kctl;
1725 snprintf(kctl->id.name, ctl_len, "%s %d %s",
1726 capture_mixer_ctl_name, rtd->pcm->device, suffix);
1727 kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
1728 kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
1729 }
1730
1731 return 0;
1732}
1733
1734static int msm_pcm_add_controls(struct snd_soc_pcm_runtime *rtd)
1735{
1736 int ret = 0;
1737
1738 pr_debug("%s\n", __func__);
1739 ret = msm_pcm_add_chmap_controls(rtd);
1740 if (ret)
1741 pr_err("%s: pcm add controls failed:%d\n", __func__, ret);
1742 ret = msm_pcm_add_app_type_controls(rtd);
1743 if (ret)
1744 pr_err("%s: pcm add app type controls failed:%d\n",
1745 __func__, ret);
1746 return ret;
1747}
1748
1749static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
1750{
1751 struct snd_card *card = rtd->card->snd_card;
1752 int ret = 0;
1753
1754 if (!card->dev->coherent_dma_mask)
1755 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
1756
1757 ret = msm_pcm_add_controls(rtd);
1758 if (ret) {
1759 pr_err("%s, kctl add failed:%d\n", __func__, ret);
1760 return ret;
1761 }
1762
1763 ret = msm_pcm_add_volume_control(rtd);
1764 if (ret)
1765 pr_err("%s: Could not add pcm Volume Control %d\n",
1766 __func__, ret);
1767
1768 ret = msm_pcm_add_compress_control(rtd);
1769 if (ret)
1770 pr_err("%s: Could not add pcm Compress Control %d\n",
1771 __func__, ret);
1772
1773 ret = msm_pcm_add_audio_adsp_stream_cmd_control(rtd);
1774 if (ret)
1775 pr_err("%s: Could not add pcm ADSP Stream Cmd Control\n",
1776 __func__);
1777
1778 ret = msm_pcm_add_audio_adsp_stream_callback_control(rtd);
1779 if (ret)
1780 pr_err("%s: Could not add pcm ADSP Stream Callback Control\n",
1781 __func__);
1782
1783 return ret;
1784}
1785
1786static snd_pcm_sframes_t msm_pcm_delay_blk(struct snd_pcm_substream *substream,
1787 struct snd_soc_dai *dai)
1788{
1789 struct snd_pcm_runtime *runtime = substream->runtime;
1790 struct msm_audio *prtd = runtime->private_data;
1791 struct audio_client *ac = prtd->audio_client;
1792 snd_pcm_sframes_t frames;
1793 int ret;
1794
1795 ret = q6asm_get_path_delay(prtd->audio_client);
1796 if (ret) {
1797 pr_err("%s: get_path_delay failed, ret=%d\n", __func__, ret);
1798 return 0;
1799 }
1800
1801 /* convert microseconds to frames */
1802 frames = ac->path_delay / 1000 * runtime->rate / 1000;
1803
1804 /* also convert the remainder from the initial division */
1805 frames += ac->path_delay % 1000 * runtime->rate / 1000000;
1806
1807 /* overcompensate for the loss of precision (empirical) */
1808 frames += 2;
1809
1810 return frames;
1811}
1812
1813static struct snd_soc_platform_driver msm_soc_platform = {
1814 .ops = &msm_pcm_ops,
1815 .pcm_new = msm_asoc_pcm_new,
1816 .delay_blk = msm_pcm_delay_blk,
1817};
1818
1819static int msm_pcm_probe(struct platform_device *pdev)
1820{
1821 int rc;
1822 int id;
1823 struct msm_plat_data *pdata;
1824 const char *latency_level;
1825
1826 rc = of_property_read_u32(pdev->dev.of_node,
1827 "qcom,msm-pcm-dsp-id", &id);
1828 if (rc) {
1829 dev_err(&pdev->dev, "%s: qcom,msm-pcm-dsp-id missing in DT node\n",
1830 __func__);
1831 return rc;
1832 }
1833
1834 pdata = kzalloc(sizeof(struct msm_plat_data), GFP_KERNEL);
1835 if (!pdata)
1836 return -ENOMEM;
1837
1838 if (of_property_read_bool(pdev->dev.of_node,
1839 "qcom,msm-pcm-low-latency")) {
1840
1841 pdata->perf_mode = LOW_LATENCY_PCM_MODE;
1842 rc = of_property_read_string(pdev->dev.of_node,
1843 "qcom,latency-level", &latency_level);
1844 if (!rc) {
1845 if (!strcmp(latency_level, "ultra"))
1846 pdata->perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
1847 else if (!strcmp(latency_level, "ull-pp"))
1848 pdata->perf_mode =
1849 ULL_POST_PROCESSING_PCM_MODE;
1850 }
1851 } else {
1852 pdata->perf_mode = LEGACY_PCM_MODE;
1853 }
1854
1855 dev_set_drvdata(&pdev->dev, pdata);
1856
1857
1858 dev_dbg(&pdev->dev, "%s: dev name %s\n",
1859 __func__, dev_name(&pdev->dev));
1860 return snd_soc_register_platform(&pdev->dev,
1861 &msm_soc_platform);
1862}
1863
1864static int msm_pcm_remove(struct platform_device *pdev)
1865{
1866 struct msm_plat_data *pdata;
1867
1868 pdata = dev_get_drvdata(&pdev->dev);
1869 kfree(pdata);
1870 snd_soc_unregister_platform(&pdev->dev);
1871 return 0;
1872}
1873static const struct of_device_id msm_pcm_dt_match[] = {
1874 {.compatible = "qcom,msm-pcm-dsp"},
1875 {}
1876};
1877MODULE_DEVICE_TABLE(of, msm_pcm_dt_match);
1878
1879static struct platform_driver msm_pcm_driver = {
1880 .driver = {
1881 .name = "msm-pcm-dsp",
1882 .owner = THIS_MODULE,
1883 .of_match_table = msm_pcm_dt_match,
1884 },
1885 .probe = msm_pcm_probe,
1886 .remove = msm_pcm_remove,
1887};
1888
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301889int __init msm_pcm_dsp_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301890{
1891 init_waitqueue_head(&the_locks.enable_wait);
1892 init_waitqueue_head(&the_locks.eos_wait);
1893 init_waitqueue_head(&the_locks.write_wait);
1894 init_waitqueue_head(&the_locks.read_wait);
1895
1896 return platform_driver_register(&msm_pcm_driver);
1897}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301898
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05301899void msm_pcm_dsp_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301900{
1901 platform_driver_unregister(&msm_pcm_driver);
1902}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301903
1904MODULE_DESCRIPTION("PCM module platform driver");
1905MODULE_LICENSE("GPL v2");