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