blob: e6a5c1a7a34b6e49a035e2ee1a6df6dda50cefc5 [file] [log] [blame]
Meng Wang43bbb872018-12-10 12:32:05 +08001// SPDX-License-Identifier: GPL-2.0-only
Laxminath Kasam989605a2021-01-20 11:17:22 +05302/* Copyright (c) 2012-2021, 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>
Ajit Pandeya557d962019-09-05 16:26:01 +053011#include <linux/mutex.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053012#include <linux/wait.h>
13#include <linux/platform_device.h>
14#include <linux/slab.h>
15#include <sound/core.h>
16#include <sound/soc.h>
17#include <sound/soc-dapm.h>
18#include <sound/pcm.h>
19#include <sound/initval.h>
20#include <sound/control.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053021#include <sound/timer.h>
22#include <asm/dma.h>
23#include <linux/dma-mapping.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053024#include <linux/msm_audio.h>
25
26#include <linux/of_device.h>
27#include <sound/tlv.h>
28#include <sound/pcm_params.h>
Surendar Karka38d66472019-03-29 18:08:07 +053029#include <sound/devdep_params.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053030#include <dsp/msm_audio_ion.h>
31#include <dsp/q6audio-v2.h>
Dieter Luecking70668fc2018-09-28 15:03:01 +020032#include <dsp/q6core.h>
33#include <dsp/q6asm-v2.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053034
35#include "msm-pcm-q6-v2.h"
36#include "msm-pcm-routing-v2.h"
37#include "msm-qti-pp-config.h"
38
Meng Wangee084a02018-09-04 16:11:58 +080039#define DRV_NAME "msm-pcm-q6-v2"
Vignesh Kulothungan0fcf2af2018-09-20 17:43:49 -070040#define TIMEOUT_MS 1000
Meng Wangee084a02018-09-04 16:11:58 +080041
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053042enum stream_state {
43 IDLE = 0,
44 STOPPED,
45 RUNNING,
46};
47
48static struct audio_locks the_locks;
49
50#define PCM_MASTER_VOL_MAX_STEPS 0x2000
51static const DECLARE_TLV_DB_LINEAR(msm_pcm_vol_gain, 0,
52 PCM_MASTER_VOL_MAX_STEPS);
53
54struct snd_msm {
55 struct snd_card *card;
56 struct snd_pcm *pcm;
57};
58
59#define CMD_EOS_MIN_TIMEOUT_LENGTH 50
60#define CMD_EOS_TIMEOUT_MULTIPLIER (HZ * 50)
61#define MAX_PB_COPY_RETRIES 3
62
63static struct snd_pcm_hardware msm_pcm_hardware_capture = {
64 .info = (SNDRV_PCM_INFO_MMAP |
65 SNDRV_PCM_INFO_BLOCK_TRANSFER |
66 SNDRV_PCM_INFO_MMAP_VALID |
67 SNDRV_PCM_INFO_INTERLEAVED |
68 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
69 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
70 SNDRV_PCM_FMTBIT_S24_LE |
71 SNDRV_PCM_FMTBIT_S24_3LE |
72 SNDRV_PCM_FMTBIT_S32_LE),
73 .rates = SNDRV_PCM_RATE_8000_384000,
74 .rate_min = 8000,
75 .rate_max = 384000,
76 .channels_min = 1,
77 .channels_max = 4,
78 .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS *
79 CAPTURE_MAX_PERIOD_SIZE,
80 .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
81 .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
82 .periods_min = CAPTURE_MIN_NUM_PERIODS,
83 .periods_max = CAPTURE_MAX_NUM_PERIODS,
84 .fifo_size = 0,
85};
86
87static struct snd_pcm_hardware msm_pcm_hardware_playback = {
88 .info = (SNDRV_PCM_INFO_MMAP |
89 SNDRV_PCM_INFO_BLOCK_TRANSFER |
90 SNDRV_PCM_INFO_MMAP_VALID |
91 SNDRV_PCM_INFO_INTERLEAVED |
92 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
93 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
94 SNDRV_PCM_FMTBIT_S24_LE |
95 SNDRV_PCM_FMTBIT_S24_3LE |
96 SNDRV_PCM_FMTBIT_S32_LE),
97 .rates = SNDRV_PCM_RATE_8000_384000,
98 .rate_min = 8000,
99 .rate_max = 384000,
100 .channels_min = 1,
101 .channels_max = 8,
102 .buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS *
103 PLAYBACK_MAX_PERIOD_SIZE,
104 .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
105 .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
106 .periods_min = PLAYBACK_MIN_NUM_PERIODS,
107 .periods_max = PLAYBACK_MAX_NUM_PERIODS,
108 .fifo_size = 0,
109};
110
111/* Conventional and unconventional sample rate supported */
112static unsigned int supported_sample_rates[] = {
113 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
114 88200, 96000, 176400, 192000, 352800, 384000
115};
116
117static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
118 .count = ARRAY_SIZE(supported_sample_rates),
119 .list = supported_sample_rates,
120 .mask = 0,
121};
122
123static void msm_pcm_route_event_handler(enum msm_pcm_routing_event event,
124 void *priv_data)
125{
126 struct msm_audio *prtd = priv_data;
127
128 WARN_ON(!prtd);
129
130 pr_debug("%s: event %x\n", __func__, event);
131
132 switch (event) {
133 case MSM_PCM_RT_EVT_BUF_RECFG:
134 q6asm_cmd(prtd->audio_client, CMD_PAUSE);
135 q6asm_cmd(prtd->audio_client, CMD_FLUSH);
136 q6asm_run(prtd->audio_client, 0, 0, 0);
137 /* fallthrough */
138 default:
139 break;
140 }
141}
142
143static void event_handler(uint32_t opcode,
144 uint32_t token, uint32_t *payload, void *priv)
145{
146 struct msm_audio *prtd = priv;
147 struct snd_pcm_substream *substream = prtd->substream;
148 uint32_t *ptrmem = (uint32_t *)payload;
149 uint32_t idx = 0;
150 uint32_t size = 0;
151 uint8_t buf_index;
152 struct snd_soc_pcm_runtime *rtd;
153 int ret = 0;
154
155 switch (opcode) {
156 case ASM_DATA_EVENT_WRITE_DONE_V2: {
157 pr_debug("ASM_DATA_EVENT_WRITE_DONE_V2\n");
158 pr_debug("Buffer Consumed = 0x%08x\n", *ptrmem);
159 prtd->pcm_irq_pos += prtd->pcm_count;
160 if (atomic_read(&prtd->start))
161 snd_pcm_period_elapsed(substream);
162 atomic_inc(&prtd->out_count);
163 wake_up(&the_locks.write_wait);
164 if (!atomic_read(&prtd->start))
165 break;
166 if (!prtd->mmap_flag || prtd->reset_event)
167 break;
168 if (q6asm_is_cpu_buf_avail_nolock(IN,
169 prtd->audio_client,
170 &size, &idx)) {
171 pr_debug("%s:writing %d bytes of buffer to dsp 2\n",
172 __func__, prtd->pcm_count);
173 q6asm_write_nolock(prtd->audio_client,
174 prtd->pcm_count, 0, 0, NO_TIMESTAMP);
175 }
176 break;
177 }
178 case ASM_DATA_EVENT_RENDERED_EOS:
179 pr_debug("ASM_DATA_EVENT_RENDERED_EOS\n");
180 clear_bit(CMD_EOS, &prtd->cmd_pending);
181 wake_up(&the_locks.eos_wait);
182 break;
183 case ASM_DATA_EVENT_READ_DONE_V2: {
184 pr_debug("ASM_DATA_EVENT_READ_DONE_V2\n");
185 buf_index = q6asm_get_buf_index_from_token(token);
Vignesh Kulothungan3817b182017-12-04 15:56:05 -0800186 if (buf_index >= CAPTURE_MAX_NUM_PERIODS) {
187 pr_err("%s: buffer index %u is out of range.\n",
188 __func__, buf_index);
189 return;
190 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530191 pr_debug("%s: token=0x%08x buf_index=0x%08x\n",
192 __func__, token, buf_index);
193 prtd->in_frame_info[buf_index].size = payload[4];
194 prtd->in_frame_info[buf_index].offset = payload[5];
195 /* assume data size = 0 during flushing */
196 if (prtd->in_frame_info[buf_index].size) {
197 prtd->pcm_irq_pos +=
198 prtd->in_frame_info[buf_index].size;
199 pr_debug("pcm_irq_pos=%d\n", prtd->pcm_irq_pos);
200 if (atomic_read(&prtd->start))
201 snd_pcm_period_elapsed(substream);
202 if (atomic_read(&prtd->in_count) <= prtd->periods)
203 atomic_inc(&prtd->in_count);
204 wake_up(&the_locks.read_wait);
205 if (prtd->mmap_flag &&
206 q6asm_is_cpu_buf_avail_nolock(OUT,
207 prtd->audio_client,
208 &size, &idx) &&
209 (substream->runtime->status->state ==
210 SNDRV_PCM_STATE_RUNNING))
211 q6asm_read_nolock(prtd->audio_client);
212 } else {
213 pr_debug("%s: reclaim flushed buf in_count %x\n",
214 __func__, atomic_read(&prtd->in_count));
215 prtd->pcm_irq_pos += prtd->pcm_count;
216 if (prtd->mmap_flag) {
217 if (q6asm_is_cpu_buf_avail_nolock(OUT,
218 prtd->audio_client,
219 &size, &idx) &&
220 (substream->runtime->status->state ==
221 SNDRV_PCM_STATE_RUNNING))
222 q6asm_read_nolock(prtd->audio_client);
223 } else {
224 atomic_inc(&prtd->in_count);
225 }
226 if (atomic_read(&prtd->in_count) == prtd->periods) {
227 pr_info("%s: reclaimed all bufs\n", __func__);
228 if (atomic_read(&prtd->start))
229 snd_pcm_period_elapsed(substream);
230 wake_up(&the_locks.read_wait);
231 }
232 }
233 break;
234 }
235 case ASM_STREAM_PP_EVENT:
236 case ASM_STREAM_CMD_ENCDEC_EVENTS: {
237 pr_debug("%s: ASM_STREAM_EVENT (0x%x)\n", __func__, opcode);
238 if (!substream) {
239 pr_err("%s: substream is NULL.\n", __func__);
240 return;
241 }
242
243 rtd = substream->private_data;
244 if (!rtd) {
245 pr_err("%s: rtd is NULL\n", __func__);
246 return;
247 }
248
249 ret = msm_adsp_inform_mixer_ctl(rtd, payload);
250 if (ret) {
251 pr_err("%s: failed to inform mixer ctl. err = %d\n",
252 __func__, ret);
253 return;
254 }
255
256 break;
257 }
258 case APR_BASIC_RSP_RESULT: {
259 switch (payload[0]) {
260 case ASM_SESSION_CMD_RUN_V2:
261 if (substream->stream
262 != SNDRV_PCM_STREAM_PLAYBACK) {
263 atomic_set(&prtd->start, 1);
264 break;
265 }
266 if (prtd->mmap_flag) {
267 pr_debug("%s:writing %d bytes of buffer to dsp\n",
268 __func__,
269 prtd->pcm_count);
270 q6asm_write_nolock(prtd->audio_client,
271 prtd->pcm_count,
272 0, 0, NO_TIMESTAMP);
273 } else {
274 while (atomic_read(&prtd->out_needed)) {
275 pr_debug("%s:writing %d bytes of buffer to dsp\n",
276 __func__,
277 prtd->pcm_count);
278 q6asm_write_nolock(prtd->audio_client,
279 prtd->pcm_count,
280 0, 0, NO_TIMESTAMP);
281 atomic_dec(&prtd->out_needed);
282 wake_up(&the_locks.write_wait);
283 };
284 }
285 atomic_set(&prtd->start, 1);
286 break;
287 case ASM_STREAM_CMD_REGISTER_PP_EVENTS:
288 pr_debug("%s: ASM_STREAM_CMD_REGISTER_PP_EVENTS:",
289 __func__);
290 break;
291 default:
292 pr_debug("%s:Payload = [0x%x]stat[0x%x]\n",
293 __func__, payload[0], payload[1]);
294 break;
295 }
296 }
297 break;
298 case RESET_EVENTS:
299 pr_debug("%s RESET_EVENTS\n", __func__);
300 prtd->pcm_irq_pos += prtd->pcm_count;
301 atomic_inc(&prtd->out_count);
302 atomic_inc(&prtd->in_count);
303 prtd->reset_event = true;
304 if (atomic_read(&prtd->start))
305 snd_pcm_period_elapsed(substream);
306 wake_up(&the_locks.eos_wait);
307 wake_up(&the_locks.write_wait);
308 wake_up(&the_locks.read_wait);
309 break;
310 default:
311 pr_debug("Not Supported Event opcode[0x%x]\n", opcode);
312 break;
313 }
314}
315
316static int msm_pcm_playback_prepare(struct snd_pcm_substream *substream)
317{
318 struct snd_pcm_runtime *runtime = substream->runtime;
319 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
Meng Wangee084a02018-09-04 16:11:58 +0800320 struct snd_soc_component *component =
321 snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530322 struct msm_audio *prtd = runtime->private_data;
323 struct msm_plat_data *pdata;
324 struct snd_pcm_hw_params *params;
325 int ret;
326 uint32_t fmt_type = FORMAT_LINEAR_PCM;
327 uint16_t bits_per_sample;
328 uint16_t sample_word_size;
329
Meng Wangee084a02018-09-04 16:11:58 +0800330 if (!component) {
331 pr_err("%s: component is NULL\n", __func__);
332 return -EINVAL;
333 }
334
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530335 pdata = (struct msm_plat_data *)
Meng Wangee084a02018-09-04 16:11:58 +0800336 dev_get_drvdata(component->dev);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530337 if (!pdata) {
338 pr_err("%s: platform data not populated\n", __func__);
339 return -EINVAL;
340 }
341 if (!prtd || !prtd->audio_client) {
342 pr_err("%s: private data null or audio client freed\n",
343 __func__);
344 return -EINVAL;
345 }
346 params = &soc_prtd->dpcm[substream->stream].hw_params;
347
348 pr_debug("%s\n", __func__);
349 prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
350 prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
351 prtd->pcm_irq_pos = 0;
352 /* rate and channels are sent to audio driver */
353 prtd->samp_rate = runtime->rate;
354 prtd->channel_mode = runtime->channels;
355 if (prtd->enabled)
356 return 0;
357
358 prtd->audio_client->perf_mode = pdata->perf_mode;
359 pr_debug("%s: perf: %x\n", __func__, pdata->perf_mode);
360
361 switch (params_format(params)) {
362 case SNDRV_PCM_FORMAT_S32_LE:
363 bits_per_sample = 32;
364 sample_word_size = 32;
365 break;
366 case SNDRV_PCM_FORMAT_S24_LE:
367 bits_per_sample = 24;
368 sample_word_size = 32;
369 break;
370 case SNDRV_PCM_FORMAT_S24_3LE:
371 bits_per_sample = 24;
372 sample_word_size = 24;
373 break;
374 case SNDRV_PCM_FORMAT_S16_LE:
375 default:
376 bits_per_sample = 16;
377 sample_word_size = 16;
378 break;
379 }
380 if (prtd->compress_enable) {
381 fmt_type = FORMAT_GEN_COMPR;
382 pr_debug("%s: Compressed enabled!\n", __func__);
383 ret = q6asm_open_write_compressed(prtd->audio_client, fmt_type,
384 COMPRESSED_PASSTHROUGH_GEN);
385 if (ret < 0) {
386 pr_err("%s: q6asm_open_write_compressed failed (%d)\n",
387 __func__, ret);
388 q6asm_audio_client_free(prtd->audio_client);
389 prtd->audio_client = NULL;
390 return -ENOMEM;
391 }
392 } else {
Mangesh Kunchamwar9f295c72018-10-08 18:20:41 +0530393 if ((q6core_get_avcs_api_version_per_service(
Dieter Luecking70668fc2018-09-28 15:03:01 +0200394 APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
Dieter Luecking9fb7c8c2019-03-06 13:31:09 +0100395 ADSP_ASM_API_VERSION_V2))
Dieter Luecking70668fc2018-09-28 15:03:01 +0200396 ret = q6asm_open_write_v5(prtd->audio_client,
397 fmt_type, bits_per_sample);
398 else
399 ret = q6asm_open_write_v4(prtd->audio_client,
400 fmt_type, bits_per_sample);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530401
402 if (ret < 0) {
Dieter Luecking70668fc2018-09-28 15:03:01 +0200403 pr_err("%s: q6asm_open_write failed (%d)\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530404 __func__, ret);
405 q6asm_audio_client_free(prtd->audio_client);
406 prtd->audio_client = NULL;
407 return -ENOMEM;
408 }
409
410 ret = q6asm_send_cal(prtd->audio_client);
411 if (ret < 0)
412 pr_debug("%s : Send cal failed : %d", __func__, ret);
413 }
414 pr_debug("%s: session ID %d\n", __func__,
415 prtd->audio_client->session);
416 prtd->session_id = prtd->audio_client->session;
417
418 if (prtd->compress_enable) {
419 ret = msm_pcm_routing_reg_phy_compr_stream(
420 soc_prtd->dai_link->id,
421 prtd->audio_client->perf_mode,
422 prtd->session_id,
423 SNDRV_PCM_STREAM_PLAYBACK,
424 COMPRESSED_PASSTHROUGH_GEN);
425 } else {
426 ret = msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->id,
427 prtd->audio_client->perf_mode,
428 prtd->session_id, substream->stream);
429 }
430 if (ret) {
431 pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
432 return ret;
433 }
434 if (prtd->compress_enable) {
435 ret = q6asm_media_format_block_gen_compr(
436 prtd->audio_client, runtime->rate,
437 runtime->channels, !prtd->set_channel_map,
438 prtd->channel_map, bits_per_sample);
439 } else {
Dieter Luecking70668fc2018-09-28 15:03:01 +0200440
Mangesh Kunchamwar9f295c72018-10-08 18:20:41 +0530441 if ((q6core_get_avcs_api_version_per_service(
Dieter Luecking70668fc2018-09-28 15:03:01 +0200442 APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
Dieter Luecking9fb7c8c2019-03-06 13:31:09 +0100443 ADSP_ASM_API_VERSION_V2)) {
Dieter Luecking70668fc2018-09-28 15:03:01 +0200444
445 ret = q6asm_media_format_block_multi_ch_pcm_v5(
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530446 prtd->audio_client, runtime->rate,
447 runtime->channels, !prtd->set_channel_map,
448 prtd->channel_map, bits_per_sample,
449 sample_word_size, ASM_LITTLE_ENDIAN,
450 DEFAULT_QF);
Dieter Luecking70668fc2018-09-28 15:03:01 +0200451 } else {
452 ret = q6asm_media_format_block_multi_ch_pcm_v4(
453 prtd->audio_client, runtime->rate,
454 runtime->channels, !prtd->set_channel_map,
455 prtd->channel_map, bits_per_sample,
456 sample_word_size, ASM_LITTLE_ENDIAN,
457 DEFAULT_QF);
458 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530459 }
460 if (ret < 0)
461 pr_info("%s: CMD Format block failed\n", __func__);
462
463 atomic_set(&prtd->out_count, runtime->periods);
464
465 prtd->enabled = 1;
466 prtd->cmd_pending = 0;
467 prtd->cmd_interrupt = 0;
468
469 return 0;
470}
471
472static int msm_pcm_capture_prepare(struct snd_pcm_substream *substream)
473{
474 struct snd_pcm_runtime *runtime = substream->runtime;
475 struct msm_audio *prtd = runtime->private_data;
476 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
Meng Wangee084a02018-09-04 16:11:58 +0800477 struct snd_soc_component *component =
478 snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530479 struct msm_plat_data *pdata;
480 struct snd_pcm_hw_params *params;
481 struct msm_pcm_routing_evt event;
482 int ret = 0;
483 int i = 0;
484 uint16_t bits_per_sample = 16;
485 uint16_t sample_word_size;
486
Meng Wangee084a02018-09-04 16:11:58 +0800487 if (!component) {
488 pr_err("%s: component is NULL\n", __func__);
489 return -EINVAL;
490 }
491
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530492 pdata = (struct msm_plat_data *)
Meng Wangee084a02018-09-04 16:11:58 +0800493 dev_get_drvdata(component->dev);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530494 if (!pdata) {
495 pr_err("%s: platform data not populated\n", __func__);
496 return -EINVAL;
497 }
498 if (!prtd || !prtd->audio_client) {
499 pr_err("%s: private data null or audio client freed\n",
500 __func__);
501 return -EINVAL;
502 }
503
504 if (prtd->enabled == IDLE) {
505 pr_debug("%s:perf_mode=%d periods=%d\n", __func__,
506 pdata->perf_mode, runtime->periods);
507 params = &soc_prtd->dpcm[substream->stream].hw_params;
508 if ((params_format(params) == SNDRV_PCM_FORMAT_S24_LE) ||
509 (params_format(params) == SNDRV_PCM_FORMAT_S24_3LE))
510 bits_per_sample = 24;
511 else if (params_format(params) == SNDRV_PCM_FORMAT_S32_LE)
512 bits_per_sample = 32;
513
514 /* ULL mode is not supported in capture path */
515 if (pdata->perf_mode == LEGACY_PCM_MODE)
516 prtd->audio_client->perf_mode = LEGACY_PCM_MODE;
517 else
518 prtd->audio_client->perf_mode = LOW_LATENCY_PCM_MODE;
519
520 pr_debug("%s Opening %d-ch PCM read stream, perf_mode %d\n",
521 __func__, params_channels(params),
522 prtd->audio_client->perf_mode);
523
Mangesh Kunchamwar9f295c72018-10-08 18:20:41 +0530524 if ((q6core_get_avcs_api_version_per_service(
Dieter Luecking70668fc2018-09-28 15:03:01 +0200525 APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
Dieter Luecking9fb7c8c2019-03-06 13:31:09 +0100526 ADSP_ASM_API_VERSION_V2))
Dieter Luecking70668fc2018-09-28 15:03:01 +0200527 ret = q6asm_open_read_v5(prtd->audio_client,
528 FORMAT_LINEAR_PCM,
529 bits_per_sample, false, ENC_CFG_ID_NONE);
530 else
531 ret = q6asm_open_read_v4(prtd->audio_client,
532 FORMAT_LINEAR_PCM,
Vikram Pandurangac712c172017-11-17 17:36:49 -0800533 bits_per_sample, false, ENC_CFG_ID_NONE);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530534 if (ret < 0) {
535 pr_err("%s: q6asm_open_read failed\n", __func__);
536 q6asm_audio_client_free(prtd->audio_client);
537 prtd->audio_client = NULL;
538 return -ENOMEM;
539 }
540
541 ret = q6asm_send_cal(prtd->audio_client);
542 if (ret < 0)
543 pr_debug("%s : Send cal failed : %d", __func__, ret);
544
545 pr_debug("%s: session ID %d\n",
546 __func__, prtd->audio_client->session);
547 prtd->session_id = prtd->audio_client->session;
548 event.event_func = msm_pcm_route_event_handler;
549 event.priv_data = (void *) prtd;
550 ret = msm_pcm_routing_reg_phy_stream_v2(
551 soc_prtd->dai_link->id,
552 prtd->audio_client->perf_mode,
553 prtd->session_id, substream->stream,
554 event);
555 if (ret) {
556 pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
557 return ret;
558 }
559 }
560
561 prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
562 prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
563 prtd->pcm_irq_pos = 0;
564 /* rate and channels are sent to audio driver */
565 prtd->samp_rate = runtime->rate;
566 prtd->channel_mode = runtime->channels;
567
568 if (prtd->enabled == IDLE || prtd->enabled == STOPPED) {
569 for (i = 0; i < runtime->periods; i++)
570 q6asm_read(prtd->audio_client);
571 prtd->periods = runtime->periods;
572 }
573
574 if (prtd->enabled != IDLE)
575 return 0;
576
577 switch (runtime->format) {
578 case SNDRV_PCM_FORMAT_S32_LE:
579 bits_per_sample = 32;
580 sample_word_size = 32;
581 break;
582 case SNDRV_PCM_FORMAT_S24_LE:
583 bits_per_sample = 24;
584 sample_word_size = 32;
585 break;
586 case SNDRV_PCM_FORMAT_S24_3LE:
587 bits_per_sample = 24;
588 sample_word_size = 24;
589 break;
590 case SNDRV_PCM_FORMAT_S16_LE:
591 default:
592 bits_per_sample = 16;
593 sample_word_size = 16;
594 break;
595 }
596
597 pr_debug("%s: Samp_rate = %d Channel = %d bit width = %d, word size = %d\n",
598 __func__, prtd->samp_rate, prtd->channel_mode,
599 bits_per_sample, sample_word_size);
Dieter Luecking70668fc2018-09-28 15:03:01 +0200600
Mangesh Kunchamwar9f295c72018-10-08 18:20:41 +0530601 if ((q6core_get_avcs_api_version_per_service(
Dieter Luecking70668fc2018-09-28 15:03:01 +0200602 APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
Dieter Luecking9fb7c8c2019-03-06 13:31:09 +0100603 ADSP_ASM_API_VERSION_V2))
Dieter Luecking70668fc2018-09-28 15:03:01 +0200604 ret = q6asm_enc_cfg_blk_pcm_format_support_v5(
605 prtd->audio_client,
606 prtd->samp_rate,
607 prtd->channel_mode,
608 bits_per_sample,
609 sample_word_size,
610 ASM_LITTLE_ENDIAN,
611 DEFAULT_QF);
612 else
613 ret = q6asm_enc_cfg_blk_pcm_format_support_v4(
614 prtd->audio_client,
615 prtd->samp_rate,
616 prtd->channel_mode,
617 bits_per_sample,
618 sample_word_size,
619 ASM_LITTLE_ENDIAN,
620 DEFAULT_QF);
621
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530622 if (ret < 0)
623 pr_debug("%s: cmd cfg pcm was block failed", __func__);
624
625 prtd->enabled = RUNNING;
626
627 return ret;
628}
629
630static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
631{
632 int ret = 0;
633 struct snd_pcm_runtime *runtime = substream->runtime;
634 struct msm_audio *prtd = runtime->private_data;
635
636 switch (cmd) {
637 case SNDRV_PCM_TRIGGER_START:
638 case SNDRV_PCM_TRIGGER_RESUME:
639 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
640 pr_debug("%s: Trigger start\n", __func__);
641 ret = q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
642 break;
643 case SNDRV_PCM_TRIGGER_STOP:
644 pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
645 atomic_set(&prtd->start, 0);
646 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK) {
647 prtd->enabled = STOPPED;
648 ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
649 break;
650 }
651 /* pending CMD_EOS isn't expected */
652 WARN_ON_ONCE(test_bit(CMD_EOS, &prtd->cmd_pending));
653 set_bit(CMD_EOS, &prtd->cmd_pending);
654 ret = q6asm_cmd_nowait(prtd->audio_client, CMD_EOS);
655 if (ret)
656 clear_bit(CMD_EOS, &prtd->cmd_pending);
657 break;
658 case SNDRV_PCM_TRIGGER_SUSPEND:
659 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
660 pr_debug("SNDRV_PCM_TRIGGER_PAUSE\n");
661 ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
662 atomic_set(&prtd->start, 0);
663 break;
664 default:
665 ret = -EINVAL;
666 break;
667 }
668
669 return ret;
670}
671
672static int msm_pcm_open(struct snd_pcm_substream *substream)
673{
674 struct snd_pcm_runtime *runtime = substream->runtime;
675 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
Meng Wangee084a02018-09-04 16:11:58 +0800676 struct snd_soc_component *component =
677 snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530678 struct msm_audio *prtd;
Banajit Goswami616e68c2017-10-27 01:31:19 -0700679 struct msm_plat_data *pdata;
Ramprasad Katkam17f05162019-04-15 18:37:10 +0530680 enum apr_subsys_state subsys_state;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530681 int ret = 0;
682
Meng Wangee084a02018-09-04 16:11:58 +0800683 if (!component) {
684 pr_err("%s: component is NULL\n", __func__);
685 return -EINVAL;
686 }
687
Banajit Goswami616e68c2017-10-27 01:31:19 -0700688 pdata = (struct msm_plat_data *)
Meng Wangee084a02018-09-04 16:11:58 +0800689 dev_get_drvdata(component->dev);
Banajit Goswami616e68c2017-10-27 01:31:19 -0700690 if (!pdata) {
691 pr_err("%s: platform data not populated\n", __func__);
692 return -EINVAL;
693 }
Ramprasad Katkam17f05162019-04-15 18:37:10 +0530694
695 subsys_state = apr_get_subsys_state();
696 if (subsys_state == APR_SUBSYS_DOWN) {
697 pr_debug("%s: adsp is down\n", __func__);
698 return -ENETRESET;
699 }
700
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530701 prtd = kzalloc(sizeof(struct msm_audio), GFP_KERNEL);
702 if (prtd == NULL)
703 return -ENOMEM;
704
705 prtd->substream = substream;
706 prtd->audio_client = q6asm_audio_client_alloc(
707 (app_cb)event_handler, prtd);
708 if (!prtd->audio_client) {
709 pr_info("%s: Could not allocate memory\n", __func__);
710 kfree(prtd);
Vatsal Bucha2eb2e612018-06-01 12:05:25 +0530711 prtd = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530712 return -ENOMEM;
713 }
714
Meng Wangee084a02018-09-04 16:11:58 +0800715 prtd->audio_client->dev = component->dev;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530716
717 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
718 runtime->hw = msm_pcm_hardware_playback;
719
720 /* Capture path */
721 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
722 runtime->hw = msm_pcm_hardware_capture;
723 else {
724 pr_err("Invalid Stream type %d\n", substream->stream);
725 return -EINVAL;
726 }
727
728 ret = snd_pcm_hw_constraint_list(runtime, 0,
729 SNDRV_PCM_HW_PARAM_RATE,
730 &constraints_sample_rates);
731 if (ret < 0)
732 pr_info("snd_pcm_hw_constraint_list failed\n");
733 /* Ensure that buffer size is a multiple of period size */
734 ret = snd_pcm_hw_constraint_integer(runtime,
735 SNDRV_PCM_HW_PARAM_PERIODS);
736 if (ret < 0)
737 pr_info("snd_pcm_hw_constraint_integer failed\n");
738
739 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
740 ret = snd_pcm_hw_constraint_minmax(runtime,
741 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
742 PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE,
743 PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE);
744 if (ret < 0) {
745 pr_err("constraint for buffer bytes min max ret = %d\n",
746 ret);
747 }
748 }
749
750 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
751 ret = snd_pcm_hw_constraint_minmax(runtime,
752 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
753 CAPTURE_MIN_NUM_PERIODS * CAPTURE_MIN_PERIOD_SIZE,
754 CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE);
755 if (ret < 0) {
756 pr_err("constraint for buffer bytes min max ret = %d\n",
757 ret);
758 }
759 }
760 ret = snd_pcm_hw_constraint_step(runtime, 0,
761 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
762 if (ret < 0) {
763 pr_err("constraint for period bytes step ret = %d\n",
764 ret);
765 }
766 ret = snd_pcm_hw_constraint_step(runtime, 0,
767 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
768 if (ret < 0) {
769 pr_err("constraint for buffer bytes step ret = %d\n",
770 ret);
771 }
772
773 prtd->enabled = IDLE;
774 prtd->dsp_cnt = 0;
775 prtd->set_channel_map = false;
776 prtd->reset_event = false;
777 runtime->private_data = prtd;
Xiaojun Sangf27e3512018-05-23 17:14:31 +0800778
779 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
780 msm_adsp_init_mixer_ctl_pp_event_queue(soc_prtd);
781
Banajit Goswami616e68c2017-10-27 01:31:19 -0700782 /* Vote to update the Rx thread priority to RT Thread for playback */
783 if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) &&
784 (pdata->perf_mode == LOW_LATENCY_PCM_MODE))
785 apr_start_rx_rt(prtd->audio_client->apr);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530786
787 return 0;
788}
789
790static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
Meng Wangac147b72017-10-30 16:46:16 +0800791 unsigned long hwoff, void __user *buf, unsigned long fbytes)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530792{
793 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530794 int xfer = 0;
795 char *bufptr = NULL;
796 void *data = NULL;
797 uint32_t idx = 0;
798 uint32_t size = 0;
799 uint32_t retries = 0;
800
801 struct snd_pcm_runtime *runtime = substream->runtime;
802 struct msm_audio *prtd = runtime->private_data;
803
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530804 pr_debug("%s: prtd->out_count = %d\n",
805 __func__, atomic_read(&prtd->out_count));
806
807 while ((fbytes > 0) && (retries < MAX_PB_COPY_RETRIES)) {
808 if (prtd->reset_event) {
809 pr_err("%s: In SSR return ENETRESET before wait\n",
810 __func__);
811 return -ENETRESET;
812 }
813
814 ret = wait_event_timeout(the_locks.write_wait,
Vignesh Kulothungan0fcf2af2018-09-20 17:43:49 -0700815 (atomic_read(&prtd->out_count)),
816 msecs_to_jiffies(TIMEOUT_MS));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530817 if (!ret) {
818 pr_err("%s: wait_event_timeout failed\n", __func__);
819 ret = -ETIMEDOUT;
820 goto fail;
821 }
822 ret = 0;
823
824 if (prtd->reset_event) {
825 pr_err("%s: In SSR return ENETRESET after wait\n",
826 __func__);
827 return -ENETRESET;
828 }
829
830 if (!atomic_read(&prtd->out_count)) {
831 pr_err("%s: pcm stopped out_count 0\n", __func__);
832 return 0;
833 }
834
835 data = q6asm_is_cpu_buf_avail(IN, prtd->audio_client, &size,
836 &idx);
837 if (data == NULL) {
838 retries++;
839 continue;
840 } else {
841 retries = 0;
842 }
843
844 if (fbytes > size)
845 xfer = size;
846 else
847 xfer = fbytes;
848
849 bufptr = data;
850 if (bufptr) {
Meng Wangac147b72017-10-30 16:46:16 +0800851 pr_debug("%s:fbytes =%lu: xfer=%d size=%d\n",
852 __func__, fbytes, xfer, size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530853 if (copy_from_user(bufptr, buf, xfer)) {
854 ret = -EFAULT;
855 pr_err("%s: copy_from_user failed\n",
856 __func__);
857 q6asm_cpu_buf_release(IN, prtd->audio_client);
858 goto fail;
859 }
860 buf += xfer;
861 fbytes -= xfer;
Meng Wangac147b72017-10-30 16:46:16 +0800862 pr_debug("%s:fbytes = %lu: xfer=%d\n", __func__,
863 fbytes, xfer);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530864 if (atomic_read(&prtd->start)) {
865 pr_debug("%s:writing %d bytes of buffer to dsp\n",
866 __func__, xfer);
867 ret = q6asm_write(prtd->audio_client, xfer,
868 0, 0, NO_TIMESTAMP);
869 if (ret < 0) {
870 ret = -EFAULT;
871 q6asm_cpu_buf_release(IN,
872 prtd->audio_client);
873 goto fail;
874 }
875 } else
876 atomic_inc(&prtd->out_needed);
877 atomic_dec(&prtd->out_count);
878 }
879 }
880fail:
881 if (retries >= MAX_PB_COPY_RETRIES)
882 ret = -ENOMEM;
883
884 return ret;
885}
886
887static int msm_pcm_playback_close(struct snd_pcm_substream *substream)
888{
889 struct snd_pcm_runtime *runtime = substream->runtime;
890 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
891 struct msm_audio *prtd = runtime->private_data;
Meng Wangee084a02018-09-04 16:11:58 +0800892 struct snd_soc_component *component =
893 snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
Banajit Goswami616e68c2017-10-27 01:31:19 -0700894 struct msm_plat_data *pdata;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530895 uint32_t timeout;
896 int dir = 0;
897 int ret = 0;
898
899 pr_debug("%s: cmd_pending 0x%lx\n", __func__, prtd->cmd_pending);
900
Ajit Pandeya557d962019-09-05 16:26:01 +0530901 if (!component) {
902 pr_err("%s: component is NULL\n", __func__);
903 return -EINVAL;
904 }
905
906 pdata = (struct msm_plat_data *) dev_get_drvdata(component->dev);
907 if (!pdata) {
908 pr_err("%s: platform data is NULL\n", __func__);
909 return -EINVAL;
910 }
911
912 mutex_lock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530913 if (prtd->audio_client) {
914 dir = IN;
915
Banajit Goswami616e68c2017-10-27 01:31:19 -0700916 /*
917 * Unvote to downgrade the Rx thread priority from
918 * RT Thread for Low-Latency use case.
919 */
Banajit Goswami616e68c2017-10-27 01:31:19 -0700920 if (pdata) {
921 if (pdata->perf_mode == LOW_LATENCY_PCM_MODE)
922 apr_end_rx_rt(prtd->audio_client->apr);
923 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530924 /* determine timeout length */
925 if (runtime->frame_bits == 0 || runtime->rate == 0) {
926 timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
927 } else {
928 timeout = (runtime->period_size *
929 CMD_EOS_TIMEOUT_MULTIPLIER) /
930 ((runtime->frame_bits / 8) *
931 runtime->rate);
932 if (timeout < CMD_EOS_MIN_TIMEOUT_LENGTH)
933 timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
934 }
935 pr_debug("%s: CMD_EOS timeout is %d\n", __func__, timeout);
936
937 ret = wait_event_timeout(the_locks.eos_wait,
938 !test_bit(CMD_EOS, &prtd->cmd_pending),
939 timeout);
940 if (!ret)
941 pr_err("%s: CMD_EOS failed, cmd_pending 0x%lx\n",
942 __func__, prtd->cmd_pending);
943 q6asm_cmd(prtd->audio_client, CMD_CLOSE);
944 q6asm_audio_client_buf_free_contiguous(dir,
945 prtd->audio_client);
946 q6asm_audio_client_free(prtd->audio_client);
947 }
948 msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
949 SNDRV_PCM_STREAM_PLAYBACK);
950 msm_adsp_clean_mixer_ctl_pp_event_queue(soc_prtd);
951 kfree(prtd);
952 runtime->private_data = NULL;
Ajit Pandeya557d962019-09-05 16:26:01 +0530953 mutex_unlock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530954 return 0;
955}
956
957static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
Meng Wangac147b72017-10-30 16:46:16 +0800958 int channel, unsigned long hwoff, void __user *buf,
959 unsigned long fbytes)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530960{
961 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530962 int xfer;
963 char *bufptr;
964 void *data = NULL;
Vignesh Kulothungan0c5f3462019-05-22 11:53:08 -0700965 uint32_t idx = 0;
966 uint32_t size = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530967 uint32_t offset = 0;
968 struct snd_pcm_runtime *runtime = substream->runtime;
969 struct msm_audio *prtd = substream->runtime->private_data;
970
971
972 pr_debug("%s\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530973
974 pr_debug("appl_ptr %d\n", (int)runtime->control->appl_ptr);
975 pr_debug("hw_ptr %d\n", (int)runtime->status->hw_ptr);
976 pr_debug("avail_min %d\n", (int)runtime->control->avail_min);
977
978 if (prtd->reset_event) {
979 pr_err("%s: In SSR return ENETRESET before wait\n", __func__);
980 return -ENETRESET;
981 }
982 ret = wait_event_timeout(the_locks.read_wait,
Vignesh Kulothungan0fcf2af2018-09-20 17:43:49 -0700983 (atomic_read(&prtd->in_count)),
984 msecs_to_jiffies(TIMEOUT_MS));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530985 if (!ret) {
986 pr_debug("%s: wait_event_timeout failed\n", __func__);
987 goto fail;
988 }
989 if (prtd->reset_event) {
990 pr_err("%s: In SSR return ENETRESET after wait\n", __func__);
991 return -ENETRESET;
992 }
993 if (!atomic_read(&prtd->in_count)) {
994 pr_debug("%s: pcm stopped in_count 0\n", __func__);
995 return 0;
996 }
997 pr_debug("Checking if valid buffer is available...%pK\n",
998 data);
999 data = q6asm_is_cpu_buf_avail(OUT, prtd->audio_client, &size, &idx);
1000 bufptr = data;
1001 pr_debug("Size = %d\n", size);
Meng Wangac147b72017-10-30 16:46:16 +08001002 pr_debug("fbytes = %lu\n", fbytes);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301003 pr_debug("idx = %d\n", idx);
1004 if (bufptr) {
1005 xfer = fbytes;
1006 if (xfer > size)
1007 xfer = size;
1008 offset = prtd->in_frame_info[idx].offset;
1009 pr_debug("Offset value = %d\n", offset);
Laxminath Kasam0b2d06e2020-10-16 17:53:22 +05301010 if (size == 0 || size < prtd->pcm_count) {
1011 memset(bufptr + offset + size, 0, prtd->pcm_count - size);
Laxminath Kasam989605a2021-01-20 11:17:22 +05301012 if (fbytes > prtd->pcm_count)
1013 size = xfer = prtd->pcm_count;
1014 else
1015 size = xfer = fbytes;
Laxminath Kasam5e3924d2020-07-09 19:36:36 +05301016 }
1017
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301018 if (copy_to_user(buf, bufptr+offset, xfer)) {
1019 pr_err("Failed to copy buf to user\n");
1020 ret = -EFAULT;
1021 q6asm_cpu_buf_release(OUT, prtd->audio_client);
1022 goto fail;
1023 }
1024 fbytes -= xfer;
1025 size -= xfer;
1026 prtd->in_frame_info[idx].offset += xfer;
Meng Wangac147b72017-10-30 16:46:16 +08001027 pr_debug("%s:fbytes = %lu: size=%d: xfer=%d\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301028 __func__, fbytes, size, xfer);
1029 pr_debug(" Sending next buffer to dsp\n");
1030 memset(&prtd->in_frame_info[idx], 0,
1031 sizeof(struct msm_audio_in_frame_info));
1032 atomic_dec(&prtd->in_count);
1033 ret = q6asm_read(prtd->audio_client);
1034 if (ret < 0) {
1035 pr_err("q6asm read failed\n");
1036 ret = -EFAULT;
1037 q6asm_cpu_buf_release(OUT, prtd->audio_client);
1038 goto fail;
1039 }
1040 } else
1041 pr_err("No valid buffer\n");
1042
1043 pr_debug("Returning from capture_copy... %d\n", ret);
1044fail:
1045 return ret;
1046}
1047
1048static int msm_pcm_capture_close(struct snd_pcm_substream *substream)
1049{
1050 struct snd_pcm_runtime *runtime = substream->runtime;
1051 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
1052 struct msm_audio *prtd = runtime->private_data;
Ajit Pandeya557d962019-09-05 16:26:01 +05301053 struct snd_soc_component *component =
1054 snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
1055 struct msm_plat_data *pdata;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301056 int dir = OUT;
1057
1058 pr_debug("%s\n", __func__);
Ajit Pandeya557d962019-09-05 16:26:01 +05301059
1060 if (!component) {
1061 pr_err("%s: component is NULL\n", __func__);
1062 return -EINVAL;
1063 }
1064
1065 pdata = (struct msm_plat_data *) dev_get_drvdata(component->dev);
1066 if (!pdata) {
1067 pr_err("%s: platform data is NULL\n", __func__);
1068 return -EINVAL;
1069 }
1070
1071 mutex_lock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301072 if (prtd->audio_client) {
1073 q6asm_cmd(prtd->audio_client, CMD_CLOSE);
1074 q6asm_audio_client_buf_free_contiguous(dir,
1075 prtd->audio_client);
1076 q6asm_audio_client_free(prtd->audio_client);
1077 }
1078
1079 msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
1080 SNDRV_PCM_STREAM_CAPTURE);
1081 kfree(prtd);
1082 runtime->private_data = NULL;
Ajit Pandeya557d962019-09-05 16:26:01 +05301083 mutex_unlock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301084 return 0;
1085}
1086
1087static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
Meng Wangac147b72017-10-30 16:46:16 +08001088 unsigned long hwoff, void __user *buf, unsigned long fbytes)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301089{
1090 int ret = 0;
1091
1092 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Meng Wangac147b72017-10-30 16:46:16 +08001093 ret = msm_pcm_playback_copy(substream, a, hwoff, buf, fbytes);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301094 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
Meng Wangac147b72017-10-30 16:46:16 +08001095 ret = msm_pcm_capture_copy(substream, a, hwoff, buf, fbytes);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301096 return ret;
1097}
1098
1099static int msm_pcm_close(struct snd_pcm_substream *substream)
1100{
1101 int ret = 0;
1102
1103 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1104 ret = msm_pcm_playback_close(substream);
1105 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1106 ret = msm_pcm_capture_close(substream);
1107 return ret;
1108}
1109
1110static int msm_pcm_prepare(struct snd_pcm_substream *substream)
1111{
1112 int ret = 0;
1113
1114 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1115 ret = msm_pcm_playback_prepare(substream);
1116 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1117 ret = msm_pcm_capture_prepare(substream);
1118 return ret;
1119}
1120
1121static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
1122{
1123
1124 struct snd_pcm_runtime *runtime = substream->runtime;
1125 struct msm_audio *prtd = runtime->private_data;
1126
1127 if (prtd->pcm_irq_pos >= prtd->pcm_size)
1128 prtd->pcm_irq_pos = 0;
1129
1130 pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos);
1131 return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
1132}
1133
1134static int msm_pcm_mmap(struct snd_pcm_substream *substream,
1135 struct vm_area_struct *vma)
1136{
1137 struct snd_pcm_runtime *runtime = substream->runtime;
1138 struct msm_audio *prtd = runtime->private_data;
1139 struct audio_client *ac = prtd->audio_client;
1140 struct audio_port_data *apd = ac->port;
1141 struct audio_buffer *ab;
1142 int dir = -1;
1143
1144 prtd->mmap_flag = 1;
1145
1146 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1147 dir = IN;
1148 else
1149 dir = OUT;
1150 ab = &(apd[dir].buf[0]);
1151
1152 return msm_audio_ion_mmap(ab, vma);
1153}
1154
1155static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
1156 struct snd_pcm_hw_params *params)
1157{
1158 struct snd_pcm_runtime *runtime = substream->runtime;
1159 struct msm_audio *prtd = runtime->private_data;
1160 struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
1161 struct audio_buffer *buf;
1162 int dir, ret;
1163
1164 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1165 dir = IN;
1166 else
1167 dir = OUT;
1168 ret = q6asm_audio_client_buf_alloc_contiguous(dir,
1169 prtd->audio_client,
1170 (params_buffer_bytes(params) / params_periods(params)),
1171 params_periods(params));
1172 if (ret < 0) {
1173 pr_err("Audio Start: Buffer Allocation failed rc = %d\n",
1174 ret);
1175 return -ENOMEM;
1176 }
1177 buf = prtd->audio_client->port[dir].buf;
1178 if (buf == NULL || buf[0].data == NULL)
1179 return -ENOMEM;
1180
1181 pr_debug("%s:buf = %pK\n", __func__, buf);
1182 dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
1183 dma_buf->dev.dev = substream->pcm->card->dev;
1184 dma_buf->private_data = NULL;
1185 dma_buf->area = buf[0].data;
1186 dma_buf->addr = buf[0].phys;
1187 dma_buf->bytes = params_buffer_bytes(params);
1188 if (!dma_buf->area)
1189 return -ENOMEM;
1190
1191 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
1192 return 0;
1193}
1194
Surendar Karka38d66472019-03-29 18:08:07 +05301195static int msm_pcm_ioctl(struct snd_pcm_substream *substream,
1196 unsigned int cmd, void __user *arg)
1197{
1198 struct msm_audio *prtd = NULL;
1199 struct snd_soc_pcm_runtime *rtd = NULL;
1200 uint64_t ses_time = 0, abs_time = 0;
1201 int64_t av_offset = 0;
1202 int32_t clock_id = -EINVAL;
1203 int rc = 0;
1204 struct snd_pcm_prsnt_position userarg;
1205
1206 if (!substream || !substream->private_data) {
1207 pr_err("%s: Invalid %s\n", __func__,
1208 (!substream) ? "substream" : "private_data");
1209 return -EINVAL;
1210 }
1211
1212 if (!substream->runtime) {
1213 pr_err("%s substream runtime not found\n", __func__);
1214 return -EINVAL;
1215 }
1216
1217 prtd = substream->runtime->private_data;
1218 if (!prtd) {
1219 pr_err("%s prtd is null.\n", __func__);
1220 return -EINVAL;
1221 }
1222
1223 rtd = substream->private_data;
1224
1225 switch (cmd) {
1226 case SNDRV_PCM_IOCTL_DSP_POSITION:
1227 dev_dbg(rtd->dev, "%s: SNDRV_PCM_DSP_POSITION", __func__);
1228 if (!arg) {
1229 dev_err(rtd->dev, "%s: Invalid params DSP_POSITION\n",
1230 __func__);
1231 rc = -EINVAL;
1232 goto done;
1233 }
1234 memset(&userarg, 0, sizeof(userarg));
1235 if (copy_from_user(&userarg, arg, sizeof(userarg))) {
1236 dev_err(rtd->dev, "%s: err copyuser DSP_POSITION\n",
1237 __func__);
1238 rc = -EFAULT;
1239 goto done;
1240 }
1241 clock_id = userarg.clock_id;
1242 rc = q6asm_get_session_time_v2(prtd->audio_client, &ses_time,
1243 &abs_time);
1244 if (rc) {
1245 pr_err("%s: q6asm_get_session_time_v2 failed, rc=%d\n",
1246 __func__, rc);
1247 goto done;
1248 }
1249 userarg.frames = div64_u64((ses_time * prtd->samp_rate),
1250 1000000);
1251
1252 rc = avcs_core_query_timer_offset(&av_offset, clock_id);
1253 if (rc) {
1254 pr_err("%s: avcs offset query failed, rc=%d\n",
1255 __func__, rc);
1256 goto done;
1257 }
1258
1259 userarg.timestamp = abs_time + av_offset;
1260 if (copy_to_user(arg, &userarg, sizeof(userarg))) {
1261 dev_err(rtd->dev, "%s: err copy to user DSP_POSITION\n",
1262 __func__);
1263 rc = -EFAULT;
1264 goto done;
1265 }
1266 pr_debug("%s, vals f %lld, t %lld, avoff %lld, abst %lld, sess_time %llu sr %d\n",
1267 __func__, userarg.frames, userarg.timestamp,
1268 av_offset, abs_time, ses_time, prtd->samp_rate);
1269 break;
1270 default:
1271 rc = snd_pcm_lib_ioctl(substream, cmd, arg);
1272 break;
1273 }
1274done:
1275 return rc;
1276}
1277
1278#ifdef CONFIG_COMPAT
1279static int msm_pcm_compat_ioctl(struct snd_pcm_substream *substream,
1280 unsigned int cmd, void __user *arg)
1281{
1282 return msm_pcm_ioctl(substream, cmd, arg);
1283}
1284#else
1285#define msm_pcm_compat_ioctl NULL
1286#endif
1287
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301288static const struct snd_pcm_ops msm_pcm_ops = {
1289 .open = msm_pcm_open,
Meng Wangac147b72017-10-30 16:46:16 +08001290 .copy_user = msm_pcm_copy,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301291 .hw_params = msm_pcm_hw_params,
1292 .close = msm_pcm_close,
Surendar Karka38d66472019-03-29 18:08:07 +05301293 .ioctl = msm_pcm_ioctl,
1294 .compat_ioctl = msm_pcm_compat_ioctl,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301295 .prepare = msm_pcm_prepare,
1296 .trigger = msm_pcm_trigger,
1297 .pointer = msm_pcm_pointer,
1298 .mmap = msm_pcm_mmap,
1299};
1300
1301static int msm_pcm_adsp_stream_cmd_put(struct snd_kcontrol *kcontrol,
1302 struct snd_ctl_elem_value *ucontrol)
1303{
Meng Wangee084a02018-09-04 16:11:58 +08001304 struct snd_soc_component *component =
1305 snd_soc_kcontrol_component(kcontrol);
1306 struct msm_plat_data *pdata = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301307 struct snd_pcm_substream *substream;
1308 struct msm_audio *prtd;
1309 int ret = 0;
1310 struct msm_adsp_event_data *event_data = NULL;
1311
Meng Wangee084a02018-09-04 16:11:58 +08001312 if (!component) {
1313 pr_err("%s: component is NULL\n", __func__);
1314 return -EINVAL;
1315 }
1316
1317 pdata = dev_get_drvdata(component->dev);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301318 if (!pdata) {
1319 pr_err("%s pdata is NULL\n", __func__);
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +05301320 return -ENODEV;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301321 }
1322
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +05301323 mutex_lock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301324 substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1325 if (!substream) {
1326 pr_err("%s substream not found\n", __func__);
1327 ret = -EINVAL;
1328 goto done;
1329 }
1330
1331 if (!substream->runtime) {
1332 pr_err("%s substream runtime not found\n", __func__);
1333 ret = -EINVAL;
1334 goto done;
1335 }
Prasad Kumpatlad4e34682020-06-25 19:38:34 +05301336 if (substream->ref_count <= 0) {
1337 pr_err_ratelimited("%s substream ref_count:%d invalid\n",
1338 __func__, substream->ref_count);
1339 ret = -EINVAL;
1340 goto done;
1341 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301342 prtd = substream->runtime->private_data;
Vatsal Bucha2eb2e612018-06-01 12:05:25 +05301343 if (prtd == NULL) {
1344 pr_err("%s prtd is null.\n", __func__);
1345 ret = -EINVAL;
1346 goto done;
1347 }
1348
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301349 if (prtd->audio_client == NULL) {
1350 pr_err("%s prtd is null.\n", __func__);
1351 ret = -EINVAL;
1352 goto done;
1353 }
1354
1355 event_data = (struct msm_adsp_event_data *)ucontrol->value.bytes.data;
1356 if ((event_data->event_type < ADSP_STREAM_PP_EVENT) ||
1357 (event_data->event_type >= ADSP_STREAM_EVENT_MAX)) {
1358 pr_err("%s: invalid event_type=%d",
1359 __func__, event_data->event_type);
1360 ret = -EINVAL;
1361 goto done;
1362 }
1363
Xiaojun Sang75642c32018-03-23 08:57:33 +08001364 if (event_data->payload_len > sizeof(ucontrol->value.bytes.data)
1365 - sizeof(struct msm_adsp_event_data)) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301366 pr_err("%s param length=%d exceeds limit",
1367 __func__, event_data->payload_len);
1368 ret = -EINVAL;
1369 goto done;
1370 }
1371
1372 ret = q6asm_send_stream_cmd(prtd->audio_client, event_data);
1373 if (ret < 0)
1374 pr_err("%s: failed to send stream event cmd, err = %d\n",
1375 __func__, ret);
1376done:
Ajit Pandeya557d962019-09-05 16:26:01 +05301377 mutex_unlock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301378 return ret;
1379}
1380
1381static int msm_pcm_add_audio_adsp_stream_cmd_control(
1382 struct snd_soc_pcm_runtime *rtd)
1383{
Meng Wangee084a02018-09-04 16:11:58 +08001384 struct snd_soc_component *component = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301385 const char *mixer_ctl_name = DSP_STREAM_CMD;
1386 const char *deviceNo = "NN";
1387 char *mixer_str = NULL;
1388 int ctl_len = 0, ret = 0;
1389 struct snd_kcontrol_new fe_audio_adsp_stream_cmd_config_control[1] = {
1390 {
1391 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1392 .name = "?",
1393 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1394 .info = msm_adsp_stream_cmd_info,
1395 .put = msm_pcm_adsp_stream_cmd_put,
1396 .private_value = 0,
1397 }
1398 };
1399
1400 if (!rtd) {
1401 pr_err("%s rtd is NULL\n", __func__);
1402 ret = -EINVAL;
1403 goto done;
1404 }
1405
Meng Wangee084a02018-09-04 16:11:58 +08001406 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
1407 if (!component) {
1408 pr_err("%s: component is NULL\n", __func__);
1409 return -EINVAL;
1410 }
1411
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301412 ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
1413 mixer_str = kzalloc(ctl_len, GFP_KERNEL);
1414 if (!mixer_str) {
1415 ret = -ENOMEM;
1416 goto done;
1417 }
1418
1419 snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
1420 fe_audio_adsp_stream_cmd_config_control[0].name = mixer_str;
1421 fe_audio_adsp_stream_cmd_config_control[0].private_value =
1422 rtd->dai_link->id;
1423 pr_debug("Registering new mixer ctl %s\n", mixer_str);
Meng Wangee084a02018-09-04 16:11:58 +08001424 ret = snd_soc_add_component_controls(component,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301425 fe_audio_adsp_stream_cmd_config_control,
1426 ARRAY_SIZE(fe_audio_adsp_stream_cmd_config_control));
1427 if (ret < 0)
1428 pr_err("%s: failed add ctl %s. err = %d\n",
1429 __func__, mixer_str, ret);
1430
1431 kfree(mixer_str);
1432done:
1433 return ret;
1434}
1435
1436static int msm_pcm_add_audio_adsp_stream_callback_control(
1437 struct snd_soc_pcm_runtime *rtd)
1438{
Meng Wangee084a02018-09-04 16:11:58 +08001439 struct snd_soc_component *component = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301440 const char *mixer_ctl_name = DSP_STREAM_CALLBACK;
1441 const char *deviceNo = "NN";
1442 char *mixer_str = NULL;
1443 int ctl_len = 0, ret = 0;
1444 struct snd_kcontrol *kctl;
1445
1446 struct snd_kcontrol_new fe_audio_adsp_callback_config_control[1] = {
1447 {
1448 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1449 .name = "?",
1450 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1451 .info = msm_adsp_stream_callback_info,
1452 .get = msm_adsp_stream_callback_get,
1453 .private_value = 0,
1454 }
1455 };
1456
1457 if (!rtd) {
1458 pr_err("%s NULL rtd\n", __func__);
1459 ret = -EINVAL;
1460 goto done;
1461 }
1462
Meng Wangee084a02018-09-04 16:11:58 +08001463 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
1464 if (!component) {
1465 pr_err("%s: component is NULL\n", __func__);
1466 return -EINVAL;
1467 }
1468
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301469 pr_debug("%s: added new pcm FE with name %s, id %d, cpu dai %s, device no %d\n",
1470 __func__, rtd->dai_link->name, rtd->dai_link->id,
1471 rtd->dai_link->cpu_dai_name, rtd->pcm->device);
1472 ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
1473 mixer_str = kzalloc(ctl_len, GFP_KERNEL);
1474 if (!mixer_str) {
1475 ret = -ENOMEM;
1476 goto done;
1477 }
1478
1479 snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
1480 fe_audio_adsp_callback_config_control[0].name = mixer_str;
1481 fe_audio_adsp_callback_config_control[0].private_value =
1482 rtd->dai_link->id;
1483 pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
Meng Wangee084a02018-09-04 16:11:58 +08001484 ret = snd_soc_add_component_controls(component,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301485 fe_audio_adsp_callback_config_control,
1486 ARRAY_SIZE(fe_audio_adsp_callback_config_control));
1487 if (ret < 0) {
1488 pr_err("%s: failed to add ctl %s. err = %d\n",
1489 __func__, mixer_str, ret);
1490 ret = -EINVAL;
1491 goto free_mixer_str;
1492 }
1493
1494 kctl = snd_soc_card_get_kcontrol(rtd->card, mixer_str);
1495 if (!kctl) {
1496 pr_err("%s: failed to get kctl %s.\n", __func__, mixer_str);
1497 ret = -EINVAL;
1498 goto free_mixer_str;
1499 }
1500
1501 kctl->private_data = NULL;
1502
1503free_mixer_str:
1504 kfree(mixer_str);
1505done:
1506 return ret;
1507}
1508
1509static int msm_pcm_set_volume(struct msm_audio *prtd, uint32_t volume)
1510{
1511 int rc = 0;
1512
1513 if (prtd && prtd->audio_client) {
1514 pr_debug("%s: channels %d volume 0x%x\n", __func__,
1515 prtd->channel_mode, volume);
1516 rc = q6asm_set_volume(prtd->audio_client, volume);
1517 if (rc < 0) {
1518 pr_err("%s: Send Volume command failed rc=%d\n",
1519 __func__, rc);
1520 }
1521 }
1522 return rc;
1523}
1524
1525static int msm_pcm_volume_ctl_get(struct snd_kcontrol *kcontrol,
1526 struct snd_ctl_elem_value *ucontrol)
1527{
1528 struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +05301529 struct msm_plat_data *pdata = NULL;
Laxminath Kasamd08ab1f2020-01-30 19:07:58 +05301530 struct snd_pcm_substream *substream = NULL;
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +05301531 struct snd_soc_pcm_runtime *soc_prtd = NULL;
1532 struct snd_soc_component *component = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301533 struct msm_audio *prtd;
1534
1535 pr_debug("%s\n", __func__);
Laxminath Kasamd08ab1f2020-01-30 19:07:58 +05301536 if (!vol) {
1537 pr_err("%s: vol is NULL\n", __func__);
1538 return -ENODEV;
1539 }
xsange41f9852020-06-19 19:46:57 +08001540
1541 if (!vol->pcm) {
1542 pr_err("%s: vol->pcm is NULL\n", __func__);
1543 return -ENODEV;
1544 }
1545
Laxminath Kasamd08ab1f2020-01-30 19:07:58 +05301546 substream = vol->pcm->streams[vol->stream].substream;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301547 if (!substream) {
1548 pr_err("%s substream not found\n", __func__);
1549 return -ENODEV;
1550 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +05301551 soc_prtd = substream->private_data;
1552 if (!substream->runtime || !soc_prtd) {
1553 pr_debug("%s substream runtime or private_data not found\n",
1554 __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301555 return 0;
1556 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +05301557
1558 component = snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
1559 if (!component) {
1560 pr_err("%s: component is NULL\n", __func__);
1561 return -EINVAL;
1562 }
1563
1564 pdata = (struct msm_plat_data *) dev_get_drvdata(component->dev);
1565 if (!pdata) {
1566 pr_err("%s: pdata not found\n", __func__);
1567 return -ENODEV;
1568 }
1569
1570 mutex_lock(&pdata->lock);
Prasad Kumpatlad4e34682020-06-25 19:38:34 +05301571 if (substream->ref_count > 0) {
1572 prtd = substream->runtime->private_data;
1573 if (prtd)
1574 ucontrol->value.integer.value[0] = prtd->volume;
1575 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +05301576 mutex_unlock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301577 return 0;
1578}
1579
1580static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
1581 struct snd_ctl_elem_value *ucontrol)
1582{
1583 int rc = 0;
1584 struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
Ajit Pandeya557d962019-09-05 16:26:01 +05301585 struct msm_plat_data *pdata = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301586 struct snd_pcm_substream *substream =
Haynes Mathew George86eb0ce2018-04-06 18:18:51 -07001587 vol->pcm->streams[vol->stream].substream;
Ajit Pandeya557d962019-09-05 16:26:01 +05301588 struct snd_soc_pcm_runtime *soc_prtd = NULL;
1589 struct snd_soc_component *component = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301590 struct msm_audio *prtd;
1591 int volume = ucontrol->value.integer.value[0];
1592
1593 pr_debug("%s: volume : 0x%x\n", __func__, volume);
1594 if (!substream) {
Ajit Pandeya557d962019-09-05 16:26:01 +05301595 pr_err("%s: substream not found\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301596 return -ENODEV;
1597 }
Ajit Pandeya557d962019-09-05 16:26:01 +05301598
1599 soc_prtd = substream->private_data;
1600 if (!substream->runtime || !soc_prtd) {
1601 pr_err("%s: substream runtime or private_data not found\n",
1602 __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301603 return 0;
1604 }
Ajit Pandeya557d962019-09-05 16:26:01 +05301605
1606 component = snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
1607 if (!component) {
1608 pr_err("%s: component is NULL\n", __func__);
1609 return -EINVAL;
1610 }
1611
1612 pdata = (struct msm_plat_data *) dev_get_drvdata(component->dev);
1613 if (!pdata) {
1614 pr_err("%s: pdata not found\n", __func__);
1615 return -ENODEV;
1616 }
1617
1618 mutex_lock(&pdata->lock);
Prasad Kumpatlad4e34682020-06-25 19:38:34 +05301619 if (substream->ref_count > 0) {
1620 prtd = substream->runtime->private_data;
1621 if (prtd) {
1622 rc = msm_pcm_set_volume(prtd, volume);
1623 prtd->volume = volume;
1624 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301625 }
Ajit Pandeya557d962019-09-05 16:26:01 +05301626 mutex_unlock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301627 return rc;
1628}
1629
Haynes Mathew George86eb0ce2018-04-06 18:18:51 -07001630static int msm_pcm_add_volume_control(struct snd_soc_pcm_runtime *rtd,
1631 int stream)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301632{
1633 int ret = 0;
1634 struct snd_pcm *pcm = rtd->pcm;
1635 struct snd_pcm_volume *volume_info;
1636 struct snd_kcontrol *kctl;
1637
Haynes Mathew George86eb0ce2018-04-06 18:18:51 -07001638 dev_dbg(rtd->dev, "%s, volume control add\n", __func__);
1639 ret = snd_pcm_add_volume_ctls(pcm, stream,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301640 NULL, 1, rtd->dai_link->id,
1641 &volume_info);
1642 if (ret < 0) {
1643 pr_err("%s volume control failed ret %d\n", __func__, ret);
1644 return ret;
1645 }
1646 kctl = volume_info->kctl;
1647 kctl->put = msm_pcm_volume_ctl_put;
1648 kctl->get = msm_pcm_volume_ctl_get;
1649 kctl->tlv.p = msm_pcm_vol_gain;
1650 return 0;
1651}
1652
1653static int msm_pcm_compress_ctl_info(struct snd_kcontrol *kcontrol,
1654 struct snd_ctl_elem_info *uinfo)
1655{
1656 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1657 uinfo->count = 1;
1658 uinfo->value.integer.min = 0;
1659 uinfo->value.integer.max = 0x2000;
1660 return 0;
1661}
1662
1663static int msm_pcm_compress_ctl_get(struct snd_kcontrol *kcontrol,
1664 struct snd_ctl_elem_value *ucontrol)
1665{
Meng Wangee084a02018-09-04 16:11:58 +08001666 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
1667 struct msm_plat_data *pdata = dev_get_drvdata(comp->dev);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301668 struct snd_pcm_substream *substream;
1669 struct msm_audio *prtd;
1670
1671 if (!pdata) {
1672 pr_err("%s pdata is NULL\n", __func__);
1673 return -ENODEV;
1674 }
1675 substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1676 if (!substream) {
1677 pr_err("%s substream not found\n", __func__);
1678 return -EINVAL;
1679 }
1680 if (!substream->runtime) {
Vignesh Kulothungan2ce67842018-09-25 16:40:29 -07001681 pr_debug("%s substream runtime not found\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301682 return 0;
1683 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +05301684 mutex_lock(&pdata->lock);
Prasad Kumpatlad4e34682020-06-25 19:38:34 +05301685 if (substream->ref_count > 0) {
1686 prtd = substream->runtime->private_data;
1687 if (prtd)
1688 ucontrol->value.integer.value[0] = prtd->compress_enable;
1689 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +05301690 mutex_unlock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301691 return 0;
1692}
1693
1694static int msm_pcm_compress_ctl_put(struct snd_kcontrol *kcontrol,
1695 struct snd_ctl_elem_value *ucontrol)
1696{
1697 int rc = 0;
Meng Wangee084a02018-09-04 16:11:58 +08001698 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
1699 struct msm_plat_data *pdata = dev_get_drvdata(comp->dev);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301700 struct snd_pcm_substream *substream;
1701 struct msm_audio *prtd;
1702 int compress = ucontrol->value.integer.value[0];
1703
1704 if (!pdata) {
1705 pr_err("%s pdata is NULL\n", __func__);
1706 return -ENODEV;
1707 }
1708 substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1709 pr_debug("%s: compress : 0x%x\n", __func__, compress);
1710 if (!substream) {
1711 pr_err("%s substream not found\n", __func__);
1712 return -EINVAL;
1713 }
1714 if (!substream->runtime) {
1715 pr_err("%s substream runtime not found\n", __func__);
1716 return 0;
1717 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +05301718 mutex_lock(&pdata->lock);
Prasad Kumpatlad4e34682020-06-25 19:38:34 +05301719 if (substream->ref_count > 0) {
1720 prtd = substream->runtime->private_data;
1721 if (prtd) {
1722 pr_debug("%s: setting compress flag to 0x%x\n",
1723 __func__, compress);
1724 prtd->compress_enable = compress;
1725 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301726 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +05301727 mutex_unlock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301728 return rc;
1729}
1730
1731static int msm_pcm_add_compress_control(struct snd_soc_pcm_runtime *rtd)
1732{
Meng Wangee084a02018-09-04 16:11:58 +08001733 struct snd_soc_component *component = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301734 const char *mixer_ctl_name = "Playback ";
1735 const char *mixer_ctl_end_name = " Compress";
1736 const char *deviceNo = "NN";
1737 char *mixer_str = NULL;
1738 int ctl_len;
1739 int ret = 0;
1740 struct msm_plat_data *pdata;
1741 struct snd_kcontrol_new pcm_compress_control[1] = {
1742 {
1743 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1744 .name = "?",
1745 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1746 .info = msm_pcm_compress_ctl_info,
1747 .get = msm_pcm_compress_ctl_get,
1748 .put = msm_pcm_compress_ctl_put,
1749 .private_value = 0,
1750 }
1751 };
1752
1753 if (!rtd) {
1754 pr_err("%s: NULL rtd\n", __func__);
1755 return -EINVAL;
1756 }
1757
Meng Wangee084a02018-09-04 16:11:58 +08001758 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
1759 if (!component) {
1760 pr_err("%s: component is NULL\n", __func__);
1761 return -EINVAL;
1762 }
1763
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301764 ctl_len = strlen(mixer_ctl_name) + strlen(deviceNo) +
1765 strlen(mixer_ctl_end_name) + 1;
1766 mixer_str = kzalloc(ctl_len, GFP_KERNEL);
1767
1768 if (!mixer_str)
1769 return -ENOMEM;
1770
1771 snprintf(mixer_str, ctl_len, "%s%d%s", mixer_ctl_name,
1772 rtd->pcm->device, mixer_ctl_end_name);
1773
1774 pcm_compress_control[0].name = mixer_str;
1775 pcm_compress_control[0].private_value = rtd->dai_link->id;
1776 pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
Meng Wangee084a02018-09-04 16:11:58 +08001777 pdata = dev_get_drvdata(component->dev);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301778 if (pdata) {
1779 if (!pdata->pcm) {
1780 pdata->pcm = rtd->pcm;
Meng Wangee084a02018-09-04 16:11:58 +08001781 snd_soc_add_component_controls(component,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301782 pcm_compress_control,
1783 ARRAY_SIZE
1784 (pcm_compress_control));
1785 pr_debug("%s: add control success plt = %pK\n",
Meng Wangee084a02018-09-04 16:11:58 +08001786 __func__, component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301787 }
1788 } else {
1789 pr_err("%s: NULL pdata\n", __func__);
1790 ret = -EINVAL;
1791 }
1792 kfree(mixer_str);
1793 return ret;
1794}
1795
1796static int msm_pcm_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1797 struct snd_ctl_elem_value *ucontrol)
1798{
1799 int i;
1800 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1801 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1802 struct snd_pcm_substream *substream;
1803 struct msm_audio *prtd;
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05301804 struct snd_soc_pcm_runtime *rtd = NULL;
1805 struct msm_plat_data *pdata = NULL;
1806 struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
Vignesh Kulothungan764b2d22019-03-21 10:54:09 -07001807 struct snd_soc_component *component = NULL;
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05301808 u64 fe_id = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301809
1810 pr_debug("%s", __func__);
1811 substream = snd_pcm_chmap_substream(info, idx);
1812 if (!substream)
1813 return -ENODEV;
Ajit Pandeya557d962019-09-05 16:26:01 +05301814
1815 rtd = substream->private_data;
1816 if (rtd) {
1817 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
1818 if (component) {
1819 pdata = (struct msm_plat_data *)
1820 dev_get_drvdata(component->dev);
1821 if (!pdata) {
1822 pr_err("%s: pdata not found\n", __func__);
1823 return -ENODEV;
1824 }
1825 } else {
1826 pr_err("%s: component is NULL\n", __func__);
1827 return -EINVAL;
1828 }
1829 }
1830
Vikram Panduranga2b0c9952019-10-14 12:44:25 -07001831 if (!rtd)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301832 return 0;
1833
Ajit Pandeya557d962019-09-05 16:26:01 +05301834 mutex_lock(&pdata->lock);
Prasad Kumpatlad4e34682020-06-25 19:38:34 +05301835 if (substream->ref_count <= 0) {
1836 pr_err_ratelimited("%s: substream ref_count:%d invalid\n",
1837 __func__, substream->ref_count);
1838 mutex_unlock(&pdata->lock);
1839 return -EINVAL;
1840 }
Vikram Panduranga2b0c9952019-10-14 12:44:25 -07001841 prtd = substream->runtime ? substream->runtime->private_data : NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301842 if (prtd) {
1843 prtd->set_channel_map = true;
Dieter Luecking70668fc2018-09-28 15:03:01 +02001844 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301845 prtd->channel_map[i] =
1846 (char)(ucontrol->value.integer.value[i]);
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05301847
1848 /* update chmixer_pspd chmap cached with routing driver as well */
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05301849 if (rtd) {
Vignesh Kulothungan764b2d22019-03-21 10:54:09 -07001850 if (component) {
1851 fe_id = rtd->dai_link->id;
Vignesh Kulothungan764b2d22019-03-21 10:54:09 -07001852 chmixer_pspd = pdata ?
1853 pdata->chmixer_pspd[fe_id][SESSION_TYPE_RX] : NULL;
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05301854
Vignesh Kulothungan764b2d22019-03-21 10:54:09 -07001855 if (chmixer_pspd && chmixer_pspd->enable) {
1856 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
1857 chmixer_pspd->in_ch_map[i] = prtd->channel_map[i];
1858 chmixer_pspd->override_in_ch_map = true;
1859 msm_pcm_routing_set_channel_mixer_cfg(fe_id,
1860 SESSION_TYPE_RX, chmixer_pspd);
1861 }
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05301862 }
1863 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301864 }
Ajit Pandeya557d962019-09-05 16:26:01 +05301865 mutex_unlock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301866 return 0;
1867}
1868
1869static int msm_pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1870 struct snd_ctl_elem_value *ucontrol)
1871{
1872 int i;
1873 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1874 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1875 struct snd_pcm_substream *substream;
1876 struct msm_audio *prtd;
Ajit Pandeya557d962019-09-05 16:26:01 +05301877 struct snd_soc_pcm_runtime *rtd = NULL;
1878 struct msm_plat_data *pdata = NULL;
1879 struct snd_soc_component *component = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301880
1881 pr_debug("%s", __func__);
1882 substream = snd_pcm_chmap_substream(info, idx);
1883 if (!substream)
1884 return -ENODEV;
Ajit Pandeya557d962019-09-05 16:26:01 +05301885
1886 rtd = substream->private_data;
1887 if (rtd) {
1888 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
1889 if (component) {
1890 pdata = (struct msm_plat_data *)
1891 dev_get_drvdata(component->dev);
1892 if (!pdata) {
1893 pr_err("%s: pdata not found\n", __func__);
1894 return -ENODEV;
1895 }
1896 } else {
1897 pr_err("%s: component is NULL\n", __func__);
1898 return -EINVAL;
1899 }
1900 }
1901
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301902 memset(ucontrol->value.integer.value, 0,
1903 sizeof(ucontrol->value.integer.value));
Vikram Panduranga2b0c9952019-10-14 12:44:25 -07001904 if (!rtd)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301905 return 0; /* no channels set */
1906
Ajit Pandeya557d962019-09-05 16:26:01 +05301907 mutex_lock(&pdata->lock);
Prasad Kumpatlad4e34682020-06-25 19:38:34 +05301908 if (substream->ref_count <= 0) {
1909 pr_err_ratelimited("%s: substream ref_count:%d invalid\n",
1910 __func__, substream->ref_count);
1911 mutex_unlock(&pdata->lock);
1912 return -EINVAL;
1913 }
Vikram Panduranga2b0c9952019-10-14 12:44:25 -07001914 prtd = substream->runtime ? substream->runtime->private_data : NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301915
1916 if (prtd && prtd->set_channel_map == true) {
Dieter Luecking70668fc2018-09-28 15:03:01 +02001917 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301918 ucontrol->value.integer.value[i] =
1919 (int)prtd->channel_map[i];
1920 } else {
Dieter Luecking70668fc2018-09-28 15:03:01 +02001921 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301922 ucontrol->value.integer.value[i] = 0;
1923 }
1924
Ajit Pandeya557d962019-09-05 16:26:01 +05301925 mutex_unlock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301926 return 0;
1927}
1928
1929static int msm_pcm_add_chmap_controls(struct snd_soc_pcm_runtime *rtd)
1930{
1931 struct snd_pcm *pcm = rtd->pcm;
1932 struct snd_pcm_chmap *chmap_info;
1933 struct snd_kcontrol *kctl;
1934 char device_num[12];
1935 int i, ret = 0;
1936
1937 pr_debug("%s, Channel map cntrl add\n", __func__);
1938 ret = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1939 snd_pcm_std_chmaps,
Dieter Luecking70668fc2018-09-28 15:03:01 +02001940 PCM_FORMAT_MAX_NUM_CHANNEL_V8, 0,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301941 &chmap_info);
1942 if (ret < 0) {
1943 pr_err("%s, channel map cntrl add failed\n", __func__);
1944 return ret;
1945 }
1946 kctl = chmap_info->kctl;
1947 for (i = 0; i < kctl->count; i++)
1948 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1949 snprintf(device_num, sizeof(device_num), "%d", pcm->device);
1950 strlcat(kctl->id.name, device_num, sizeof(kctl->id.name));
1951 pr_debug("%s, Overwriting channel map control name to: %s\n",
1952 __func__, kctl->id.name);
1953 kctl->put = msm_pcm_chmap_ctl_put;
1954 kctl->get = msm_pcm_chmap_ctl_get;
1955 return 0;
1956}
1957
1958static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
1959 struct snd_ctl_elem_value *ucontrol)
1960{
1961 u64 fe_id = kcontrol->private_value;
1962 int session_type = SESSION_TYPE_RX;
1963 int be_id = ucontrol->value.integer.value[3];
1964 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
1965 int ret = 0;
1966
1967 cfg_data.app_type = ucontrol->value.integer.value[0];
1968 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
1969 if (ucontrol->value.integer.value[2] != 0)
1970 cfg_data.sample_rate = ucontrol->value.integer.value[2];
1971 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
1972 __func__, fe_id, session_type, be_id,
1973 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1974 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
1975 be_id, &cfg_data);
1976 if (ret < 0)
1977 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
1978 __func__, ret);
1979
1980 return ret;
1981}
1982
1983static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
1984 struct snd_ctl_elem_value *ucontrol)
1985{
1986 u64 fe_id = kcontrol->private_value;
1987 int session_type = SESSION_TYPE_RX;
1988 int be_id = 0;
1989 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
1990 int ret = 0;
1991
1992 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
1993 &be_id, &cfg_data);
1994 if (ret < 0) {
1995 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
1996 __func__, ret);
1997 goto done;
1998 }
1999
2000 ucontrol->value.integer.value[0] = cfg_data.app_type;
2001 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
2002 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
2003 ucontrol->value.integer.value[3] = be_id;
2004 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
2005 __func__, fe_id, session_type, be_id,
2006 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
2007done:
2008 return ret;
2009}
2010
2011static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
2012 struct snd_ctl_elem_value *ucontrol)
2013{
2014 u64 fe_id = kcontrol->private_value;
2015 int session_type = SESSION_TYPE_TX;
2016 int be_id = ucontrol->value.integer.value[3];
2017 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
2018 int ret = 0;
2019
2020 cfg_data.app_type = ucontrol->value.integer.value[0];
2021 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
2022 if (ucontrol->value.integer.value[2] != 0)
2023 cfg_data.sample_rate = ucontrol->value.integer.value[2];
2024 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
2025 __func__, fe_id, session_type, be_id,
2026 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
2027 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
2028 be_id, &cfg_data);
2029 if (ret < 0)
2030 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
2031 __func__, ret);
2032
2033 return ret;
2034}
2035
2036static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
2037 struct snd_ctl_elem_value *ucontrol)
2038{
2039 u64 fe_id = kcontrol->private_value;
2040 int session_type = SESSION_TYPE_TX;
2041 int be_id = 0;
2042 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
2043 int ret = 0;
2044
2045 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
2046 &be_id, &cfg_data);
2047 if (ret < 0) {
2048 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
2049 __func__, ret);
2050 goto done;
2051 }
2052
2053 ucontrol->value.integer.value[0] = cfg_data.app_type;
2054 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
2055 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
2056 ucontrol->value.integer.value[3] = be_id;
2057 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
2058 __func__, fe_id, session_type, be_id,
2059 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
2060done:
2061 return ret;
2062}
2063
2064static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
2065{
2066 struct snd_pcm *pcm = rtd->pcm;
2067 struct snd_pcm_usr *app_type_info;
2068 struct snd_kcontrol *kctl;
2069 const char *playback_mixer_ctl_name = "Audio Stream";
2070 const char *capture_mixer_ctl_name = "Audio Stream Capture";
2071 const char *deviceNo = "NN";
2072 const char *suffix = "App Type Cfg";
2073 int ctl_len, ret = 0;
2074
2075 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
2076 ctl_len = strlen(playback_mixer_ctl_name) + 1 +
2077 strlen(deviceNo) + 1 + strlen(suffix) + 1;
2078 pr_debug("%s: Playback app type cntrl add\n", __func__);
2079 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2080 NULL, 1, ctl_len, rtd->dai_link->id,
2081 &app_type_info);
2082 if (ret < 0) {
2083 pr_err("%s: playback app type cntrl add failed: %d\n",
2084 __func__, ret);
2085 return ret;
2086 }
2087 kctl = app_type_info->kctl;
2088 snprintf(kctl->id.name, ctl_len, "%s %d %s",
2089 playback_mixer_ctl_name, rtd->pcm->device, suffix);
2090 kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
2091 kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
2092 }
2093
2094 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
2095 ctl_len = strlen(capture_mixer_ctl_name) + 1 +
2096 strlen(deviceNo) + 1 + strlen(suffix) + 1;
2097 pr_debug("%s: Capture app type cntrl add\n", __func__);
2098 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
2099 NULL, 1, ctl_len, rtd->dai_link->id,
2100 &app_type_info);
2101 if (ret < 0) {
2102 pr_err("%s: capture app type cntrl add failed: %d\n",
2103 __func__, ret);
2104 return ret;
2105 }
2106 kctl = app_type_info->kctl;
2107 snprintf(kctl->id.name, ctl_len, "%s %d %s",
2108 capture_mixer_ctl_name, rtd->pcm->device, suffix);
2109 kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
2110 kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
2111 }
2112
2113 return 0;
2114}
2115
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05302116static struct msm_pcm_channel_mixer *msm_pcm_get_chmixer(
2117 struct msm_plat_data *pdata,
2118 u64 fe_id, int session_type)
2119{
2120 if (!pdata) {
2121 pr_err("%s: missing pdata\n", __func__);
2122 return NULL;
2123 }
2124
2125 if (fe_id >= MSM_FRONTEND_DAI_MM_SIZE) {
2126 pr_err("%s: invalid FE %llu\n", __func__, fe_id);
2127 return NULL;
2128 }
2129
2130 if ((session_type != SESSION_TYPE_TX) &&
2131 (session_type != SESSION_TYPE_RX)) {
2132 pr_err("%s: invalid session type %d\n", __func__, session_type);
2133 return NULL;
2134 }
2135
2136 return pdata->chmixer_pspd[fe_id][session_type];
2137}
2138
2139static int msm_pcm_channel_mixer_cfg_ctl_put(struct snd_kcontrol *kcontrol,
2140 struct snd_ctl_elem_value *ucontrol)
2141{
2142 u64 fe_id = kcontrol->private_value & 0xFF;
2143 int session_type = (kcontrol->private_value >> 8) & 0xFF;
2144 int ret = 0;
2145 int stream_id = 0;
2146 int be_id = 0, i = 0;
2147 struct msm_audio *prtd = NULL;
2148 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2149 struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
2150 struct snd_pcm *pcm = NULL;
2151 struct snd_pcm_substream *substream = NULL;
2152 struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
2153 u8 asm_ch_map[PCM_FORMAT_MAX_NUM_CHANNEL_V8] = {0};
2154 bool reset_override_out_ch_map = false;
2155 bool reset_override_in_ch_map = false;
2156
2157 pcm = pdata->pcm_device[fe_id];
2158 if (!pcm) {
2159 pr_err("%s invalid pcm handle for fe_id %llu\n",
2160 __func__, fe_id);
2161 return -EINVAL;
2162 }
2163
2164 if (session_type == SESSION_TYPE_RX)
2165 substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
2166 else
2167 substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
2168 if (!substream) {
2169 pr_err("%s substream not found\n", __func__);
2170 return -EINVAL;
2171 }
2172
2173 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
2174 if (!chmixer_pspd) {
2175 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
2176 return -EINVAL;
2177 }
2178
2179 chmixer_pspd->enable = ucontrol->value.integer.value[0];
2180 chmixer_pspd->rule = ucontrol->value.integer.value[1];
2181 chmixer_pspd->input_channel = ucontrol->value.integer.value[2];
2182 chmixer_pspd->output_channel = ucontrol->value.integer.value[3];
2183 chmixer_pspd->port_idx = ucontrol->value.integer.value[4];
2184
Erin Yan2f81be22019-04-29 13:42:57 +08002185 if (chmixer_pspd->input_channel < 0 ||
Dhananjay Kumarf682acd2019-03-01 17:07:10 +05302186 chmixer_pspd->input_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8 ||
Erin Yan2f81be22019-04-29 13:42:57 +08002187 chmixer_pspd->output_channel < 0 ||
Dhananjay Kumarf682acd2019-03-01 17:07:10 +05302188 chmixer_pspd->output_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
2189 pr_err("%s: Invalid channels, in %d, out %d\n",
2190 __func__, chmixer_pspd->input_channel,
2191 chmixer_pspd->output_channel);
2192 return -EINVAL;
2193 }
2194
Ajit Pandeya557d962019-09-05 16:26:01 +05302195 mutex_lock(&pdata->lock);
Prasad Kumpatlad4e34682020-06-25 19:38:34 +05302196 if (substream->ref_count <= 0) {
2197 pr_err_ratelimited("%s: substream ref_count:%d invalid\n",
2198 __func__, substream->ref_count);
2199 mutex_unlock(&pdata->lock);
2200 return -EINVAL;
2201 }
Dhananjay Kumarf682acd2019-03-01 17:07:10 +05302202 prtd = substream->runtime ? substream->runtime->private_data : NULL;
2203 if (chmixer_pspd->enable && prtd) {
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05302204 if (session_type == SESSION_TYPE_RX &&
2205 !chmixer_pspd->override_in_ch_map) {
Dhananjay Kumarf682acd2019-03-01 17:07:10 +05302206 if (prtd->set_channel_map) {
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05302207 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
Dhananjay Kumarf682acd2019-03-01 17:07:10 +05302208 chmixer_pspd->in_ch_map[i] = prtd->channel_map[i];
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05302209 } else {
2210 q6asm_map_channels(asm_ch_map,
2211 chmixer_pspd->input_channel, false);
2212 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
2213 chmixer_pspd->in_ch_map[i] = asm_ch_map[i];
2214 }
2215 chmixer_pspd->override_in_ch_map = true;
2216 reset_override_in_ch_map = true;
2217 } else if (session_type == SESSION_TYPE_TX &&
2218 !chmixer_pspd->override_out_ch_map) {
Dhananjay Kumarf682acd2019-03-01 17:07:10 +05302219 /*
2220 * Channel map set in prtd is for plyback only,
2221 * hence always use default for capture path.
2222 */
2223 q6asm_map_channels(asm_ch_map,
2224 chmixer_pspd->output_channel, false);
2225 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
2226 chmixer_pspd->out_ch_map[i] = asm_ch_map[i];
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05302227 chmixer_pspd->override_out_ch_map = true;
2228 reset_override_out_ch_map = true;
2229 }
2230 } else {
2231 chmixer_pspd->override_out_ch_map = false;
2232 chmixer_pspd->override_in_ch_map = false;
2233 }
2234
2235 /* cache value and take effect during adm_open stage */
2236 msm_pcm_routing_set_channel_mixer_cfg(fe_id,
2237 session_type,
2238 chmixer_pspd);
2239
Dhananjay Kumarf682acd2019-03-01 17:07:10 +05302240 if (chmixer_pspd->enable && prtd && prtd->audio_client) {
2241 stream_id = prtd->audio_client->session;
2242 be_id = chmixer_pspd->port_idx;
2243 msm_pcm_routing_set_channel_mixer_runtime(be_id,
2244 stream_id,
2245 session_type,
2246 chmixer_pspd);
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05302247 }
2248
2249 if (reset_override_out_ch_map)
2250 chmixer_pspd->override_out_ch_map = false;
2251 if (reset_override_in_ch_map)
2252 chmixer_pspd->override_in_ch_map = false;
2253
Ajit Pandeya557d962019-09-05 16:26:01 +05302254 mutex_unlock(&pdata->lock);
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05302255 return ret;
2256}
2257
2258static int msm_pcm_channel_mixer_cfg_ctl_get(struct snd_kcontrol *kcontrol,
2259 struct snd_ctl_elem_value *ucontrol)
2260{
2261 u64 fe_id = kcontrol->private_value & 0xFF;
2262 int session_type = (kcontrol->private_value >> 8) & 0xFF;
2263 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2264 struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
2265 struct msm_pcm_channel_mixer *chmixer_pspd;
2266
2267 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
2268 if (!chmixer_pspd) {
2269 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
2270 return -EINVAL;
2271 }
2272
2273 ucontrol->value.integer.value[0] = chmixer_pspd->enable;
2274 ucontrol->value.integer.value[1] = chmixer_pspd->rule;
2275 ucontrol->value.integer.value[2] = chmixer_pspd->input_channel;
2276 ucontrol->value.integer.value[3] = chmixer_pspd->output_channel;
2277 ucontrol->value.integer.value[4] = chmixer_pspd->port_idx;
2278 return 0;
2279}
2280
2281static int msm_pcm_channel_mixer_output_map_ctl_put(
2282 struct snd_kcontrol *kcontrol,
2283 struct snd_ctl_elem_value *ucontrol)
2284{
2285 u64 fe_id = kcontrol->private_value & 0xFF;
2286 int session_type = (kcontrol->private_value >> 8) & 0xFF;
2287 int i = 0;
2288 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2289 struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
2290 struct msm_pcm_channel_mixer *chmixer_pspd;
2291
2292 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
2293 if (!chmixer_pspd) {
2294 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
2295 return -EINVAL;
2296 }
2297
2298 chmixer_pspd->override_out_ch_map = true;
2299 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
2300 chmixer_pspd->out_ch_map[i] =
2301 ucontrol->value.integer.value[i];
2302
2303 return 0;
2304}
2305
2306static int msm_pcm_channel_mixer_output_map_ctl_get(
2307 struct snd_kcontrol *kcontrol,
2308 struct snd_ctl_elem_value *ucontrol)
2309{
2310 u64 fe_id = kcontrol->private_value & 0xFF;
2311 int session_type = (kcontrol->private_value >> 8) & 0xFF;
2312 int i = 0;
2313 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2314 struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
2315 struct msm_pcm_channel_mixer *chmixer_pspd;
2316
2317 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
2318 if (!chmixer_pspd) {
2319 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
2320 return -EINVAL;
2321 }
2322
2323 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
2324 ucontrol->value.integer.value[i] =
2325 chmixer_pspd->out_ch_map[i];
2326 return 0;
2327}
2328
2329static int msm_pcm_channel_mixer_input_map_ctl_put(
2330 struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_value *ucontrol)
2332{
2333 u64 fe_id = kcontrol->private_value & 0xFF;
2334 int session_type = (kcontrol->private_value >> 8) & 0xFF;
2335 int i = 0;
2336 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2337 struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
2338 struct msm_pcm_channel_mixer *chmixer_pspd;
2339
2340 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
2341 if (!chmixer_pspd) {
2342 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
2343 return -EINVAL;
2344 }
2345
2346 chmixer_pspd->override_in_ch_map = true;
2347 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
2348 chmixer_pspd->in_ch_map[i] = ucontrol->value.integer.value[i];
2349
2350 return 0;
2351}
2352
2353static int msm_pcm_channel_mixer_input_map_ctl_get(
2354 struct snd_kcontrol *kcontrol,
2355 struct snd_ctl_elem_value *ucontrol)
2356{
2357 u64 fe_id = kcontrol->private_value & 0xFF;
2358 int session_type = (kcontrol->private_value >> 8) & 0xFF;
2359 int i = 0;
2360 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2361 struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
2362 struct msm_pcm_channel_mixer *chmixer_pspd;
2363
2364 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
2365 if (!chmixer_pspd) {
2366 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
2367 return -EINVAL;
2368 }
2369
2370 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
2371 ucontrol->value.integer.value[i] =
2372 chmixer_pspd->in_ch_map[i];
2373 return 0;
2374}
2375
2376static int msm_pcm_channel_mixer_weight_ctl_put(
2377 struct snd_kcontrol *kcontrol,
2378 struct snd_ctl_elem_value *ucontrol)
2379{
2380 u64 fe_id = kcontrol->private_value & 0xFF;
2381 int session_type = (kcontrol->private_value >> 8) & 0xFF;
2382 int channel = (kcontrol->private_value >> 16) & 0xFF;
2383 int i = 0;
2384 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2385 struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
2386 struct msm_pcm_channel_mixer *chmixer_pspd;
2387
2388 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
2389 if (!chmixer_pspd) {
2390 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
2391 return -EINVAL;
2392 }
2393
2394 if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
2395 pr_err("%s: invalid channel number %d\n", __func__, channel);
2396 return -EINVAL;
2397 }
2398 channel--;
2399
2400 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
2401 chmixer_pspd->channel_weight[channel][i] =
2402 ucontrol->value.integer.value[i];
2403 return 0;
2404}
2405
2406static int msm_pcm_channel_mixer_weight_ctl_get(
2407 struct snd_kcontrol *kcontrol,
2408 struct snd_ctl_elem_value *ucontrol)
2409{
2410 u64 fe_id = kcontrol->private_value & 0xFF;
2411 int session_type = (kcontrol->private_value >> 8) & 0xFF;
2412 int channel = (kcontrol->private_value >> 16) & 0xFF;
2413 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2414 struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
2415 int i = 0;
2416 struct msm_pcm_channel_mixer *chmixer_pspd;
2417
2418 if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
2419 pr_err("%s: invalid channel number %d\n", __func__, channel);
2420 return -EINVAL;
2421 }
2422 channel--;
2423
2424 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
2425 if (!chmixer_pspd) {
2426 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
2427 return -EINVAL;
2428 }
2429
2430 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
2431 ucontrol->value.integer.value[i] =
2432 chmixer_pspd->channel_weight[channel][i];
2433 return 0;
2434}
2435
2436static int msm_pcm_add_platform_controls(struct snd_kcontrol_new *kctl,
2437 struct snd_soc_pcm_runtime *rtd, const char *name_prefix,
2438 const char *name_suffix, int session_type, int channels)
2439{
2440 int ret = -EINVAL;
2441 char *mixer_name = NULL;
2442 struct snd_pcm *pcm = rtd->pcm;
2443 const char *deviceNo = "NN";
2444 const char *channelNo = "NN";
2445 int ctl_len = 0;
2446 struct snd_soc_component *component = NULL;
2447
2448 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
2449 if (!component) {
2450 pr_err("%s: component is NULL\n", __func__);
2451 return -EINVAL;
2452 }
2453
2454 ctl_len = strlen(name_prefix) + 1 + strlen(deviceNo) + 1 +
2455 strlen(channelNo) + 1 + strlen(name_suffix) + 1;
2456
2457 mixer_name = kzalloc(ctl_len, GFP_KERNEL);
2458 if (mixer_name == NULL)
2459 return -ENOMEM;
2460
2461 if (channels >= 0) {
2462 snprintf(mixer_name, ctl_len, "%s %d %s %d",
2463 name_prefix, pcm->device, name_suffix, channels);
2464 kctl->private_value = (rtd->dai_link->id) | (session_type << 8) |
2465 (channels << 16);
2466 } else {
2467 snprintf(mixer_name, ctl_len, "%s %d %s",
2468 name_prefix, pcm->device, name_suffix);
2469 kctl->private_value = (rtd->dai_link->id) | (session_type << 8);
2470 }
2471
2472 kctl->name = mixer_name;
2473 ret = snd_soc_add_component_controls(component, kctl, 1);
2474 kfree(mixer_name);
2475 return ret;
2476}
2477
2478static int msm_pcm_channel_mixer_output_map_info(struct snd_kcontrol *kcontrol,
2479 struct snd_ctl_elem_info *uinfo)
2480{
2481 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2482 uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
2483 /* Valid channel map value ranges from 1 to 64 */
2484 uinfo->value.integer.min = 1;
2485 uinfo->value.integer.max = 64;
2486 return 0;
2487}
2488
2489static int msm_pcm_add_channel_mixer_output_map_controls(
2490 struct snd_soc_pcm_runtime *rtd)
2491{
2492 struct snd_pcm *pcm = rtd->pcm;
2493 const char *playback_mixer_ctl_name = "AudStr";
2494 const char *capture_mixer_ctl_name = "AudStr Capture";
2495 const char *suffix = "ChMixer Output Map";
2496 int session_type = 0, ret = 0, channel = -1;
2497 struct snd_kcontrol_new channel_mixer_output_map_control = {
2498 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2499 .name = "?",
2500 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
2501 .info = msm_pcm_channel_mixer_output_map_info,
2502 .put = msm_pcm_channel_mixer_output_map_ctl_put,
2503 .get = msm_pcm_channel_mixer_output_map_ctl_get,
2504 .private_value = 0,
2505 };
2506
2507 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
2508 session_type = SESSION_TYPE_RX;
2509 ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
2510 rtd, playback_mixer_ctl_name, suffix, session_type, channel);
2511 if (ret < 0)
2512 goto fail;
2513 }
2514
2515 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
2516 session_type = SESSION_TYPE_TX;
2517 ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
2518 rtd, capture_mixer_ctl_name, suffix, session_type, channel);
2519 if (ret < 0)
2520 goto fail;
2521 }
2522 return 0;
2523
2524fail:
2525 pr_err("%s: failed add platform ctl, err = %d\n",
2526 __func__, ret);
2527 return ret;
2528}
2529
2530static int msm_pcm_channel_mixer_input_map_info(struct snd_kcontrol *kcontrol,
2531 struct snd_ctl_elem_info *uinfo)
2532{
2533 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2534 uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
2535 /* Valid channel map value ranges from 1 to 64 */
2536 uinfo->value.integer.min = 1;
2537 uinfo->value.integer.max = 64;
2538 return 0;
2539}
2540
2541static int msm_pcm_add_channel_mixer_input_map_controls(
2542 struct snd_soc_pcm_runtime *rtd)
2543{
2544 struct snd_pcm *pcm = rtd->pcm;
2545 const char *playback_mixer_ctl_name = "AudStr";
2546 const char *capture_mixer_ctl_name = "AudStr Capture";
2547 const char *suffix = "ChMixer Input Map";
2548 int session_type = 0, ret = 0, channel = -1;
2549 struct snd_kcontrol_new channel_mixer_input_map_control = {
2550 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2551 .name = "?",
2552 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
2553 .info = msm_pcm_channel_mixer_input_map_info,
2554 .put = msm_pcm_channel_mixer_input_map_ctl_put,
2555 .get = msm_pcm_channel_mixer_input_map_ctl_get,
2556 .private_value = 0,
2557 };
2558
2559 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
2560 session_type = SESSION_TYPE_RX;
2561 ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
2562 rtd, playback_mixer_ctl_name, suffix, session_type, channel);
2563 if (ret < 0)
2564 goto fail;
2565 }
2566
2567 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
2568 session_type = SESSION_TYPE_TX;
2569 ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
2570 rtd, capture_mixer_ctl_name, suffix, session_type, channel);
2571 if (ret < 0)
2572 goto fail;
2573 }
2574 return 0;
2575
2576fail:
2577 pr_err("%s: failed add platform ctl, err = %d\n",
2578 __func__, ret);
2579
2580 return ret;
2581}
2582
2583static int msm_pcm_channel_mixer_cfg_info(struct snd_kcontrol *kcontrol,
2584 struct snd_ctl_elem_info *uinfo)
2585{
2586 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2587 /* five int values: enable, rule, in_channels, out_channels and port_id */
2588 uinfo->count = 5;
2589 /* Valid range is all positive values to support above controls */
2590 uinfo->value.integer.min = 0;
2591 uinfo->value.integer.max = INT_MAX;
2592 return 0;
2593}
2594
2595static int msm_pcm_add_channel_mixer_cfg_controls(
2596 struct snd_soc_pcm_runtime *rtd)
2597{
2598 struct snd_pcm *pcm = rtd->pcm;
2599 const char *playback_mixer_ctl_name = "AudStr";
2600 const char *capture_mixer_ctl_name = "AudStr Capture";
2601 const char *suffix = "ChMixer Cfg";
2602 int session_type = 0, ret = 0, channel = -1;
2603 struct msm_plat_data *pdata = NULL;
2604 struct snd_soc_component *component = NULL;
2605 struct snd_kcontrol_new channel_mixer_cfg_control = {
2606 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2607 .name = "?",
2608 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
2609 .info = msm_pcm_channel_mixer_cfg_info,
2610 .put = msm_pcm_channel_mixer_cfg_ctl_put,
2611 .get = msm_pcm_channel_mixer_cfg_ctl_get,
2612 .private_value = 0,
2613 };
2614
2615 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
2616 if (!component) {
2617 pr_err("%s: component is NULL\n", __func__);
2618 return -EINVAL;
2619 }
2620
2621 pdata = (struct msm_plat_data *)
2622 dev_get_drvdata(component->dev);
2623
2624 pdata->pcm_device[rtd->dai_link->id] = rtd->pcm;
2625
2626 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
2627 session_type = SESSION_TYPE_RX;
2628 ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
2629 rtd, playback_mixer_ctl_name, suffix, session_type, channel);
2630 if (ret < 0)
2631 goto fail;
2632 }
2633
2634 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
2635 session_type = SESSION_TYPE_TX;
2636 ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
2637 rtd, capture_mixer_ctl_name, suffix, session_type, channel);
2638 if (ret < 0)
2639 goto fail;
2640 }
2641 return 0;
2642
2643fail:
2644 pr_err("%s: failed add platform ctl, err = %d\n",
2645 __func__, ret);
2646
2647 return ret;
2648}
2649
2650static int msm_pcm_channel_mixer_weight_info(struct snd_kcontrol *kcontrol,
2651 struct snd_ctl_elem_info *uinfo)
2652{
2653 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2654 uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
2655 /* Valid range: 0 to 0x4000(Unity) gain weightage */
2656 uinfo->value.integer.min = 0;
2657 uinfo->value.integer.max = 0x4000;
2658 return 0;
2659}
2660
2661static int msm_pcm_add_channel_mixer_weight_controls(
2662 struct snd_soc_pcm_runtime *rtd,
2663 int channel)
2664{
2665 struct snd_pcm *pcm = rtd->pcm;
2666 const char *playback_mixer_ctl_name = "AudStr";
2667 const char *capture_mixer_ctl_name = "AudStr Capture";
2668 const char *suffix = "ChMixer Weight Ch";
2669 int session_type = 0, ret = 0;
2670 struct snd_kcontrol_new channel_mixer_weight_control = {
2671 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2672 .name = "?",
2673 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
2674 .info = msm_pcm_channel_mixer_weight_info,
2675 .put = msm_pcm_channel_mixer_weight_ctl_put,
2676 .get = msm_pcm_channel_mixer_weight_ctl_get,
2677 .private_value = 0,
2678 };
2679
2680 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
2681 session_type = SESSION_TYPE_RX;
2682 ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
2683 rtd, playback_mixer_ctl_name, suffix, session_type, channel);
2684 if (ret < 0)
2685 goto fail;
2686 }
2687
2688 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
2689 session_type = SESSION_TYPE_TX;
2690 ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
2691 rtd, capture_mixer_ctl_name, suffix, session_type, channel);
2692 if (ret < 0)
2693 goto fail;
2694 }
2695 return 0;
2696
2697fail:
2698 pr_err("%s: failed add platform ctl, err = %d\n",
2699 __func__, ret);
2700
2701 return ret;
2702}
2703
2704static int msm_pcm_add_channel_mixer_controls(struct snd_soc_pcm_runtime *rtd)
2705{
2706 int i, ret = 0;
2707 struct snd_pcm *pcm = NULL;
2708 struct msm_plat_data *pdata = NULL;
2709 struct snd_soc_component *component = NULL;
2710
2711 if (!rtd || !rtd->pcm) {
2712 pr_err("%s invalid rtd or pcm\n", __func__);
2713 return -EINVAL;
2714 }
2715 pcm = rtd->pcm;
2716
2717 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
2718 if (!component) {
2719 pr_err("%s: component is NULL\n", __func__);
2720 return -EINVAL;
2721 }
2722
2723 pdata = (struct msm_plat_data *)
2724 dev_get_drvdata(component->dev);
2725 if (!pdata) {
2726 pr_err("%s: platform data not populated\n", __func__);
2727 return -EINVAL;
2728 }
2729
2730 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream &&
2731 !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
2732 pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] =
2733 kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
2734 if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
2735 ret = -ENOMEM;
2736 goto fail;
2737 }
2738 }
2739
2740 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream &&
2741 !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
2742 pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] =
2743 kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
2744 if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
2745 ret = -ENOMEM;
2746 goto fail;
2747 }
2748 }
2749
2750 ret = msm_pcm_add_channel_mixer_cfg_controls(rtd);
2751 if (ret) {
2752 pr_err("%s: pcm add channel mixer cfg controls failed:%d\n",
2753 __func__, ret);
2754 goto fail;
2755 }
2756 ret = msm_pcm_add_channel_mixer_input_map_controls(rtd);
2757 if (ret) {
2758 pr_err("%s: pcm add channel mixer input map controls failed:%d\n",
2759 __func__, ret);
2760 goto fail;
2761 }
2762 ret = msm_pcm_add_channel_mixer_output_map_controls(rtd);
2763 if (ret) {
2764 pr_err("%s: pcm add channel mixer output map controls failed:%d\n",
2765 __func__, ret);
2766 goto fail;
2767 }
2768
2769 for (i = 1; i <= PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++) {
2770 ret = msm_pcm_add_channel_mixer_weight_controls(rtd, i);
2771 if (ret) {
2772 pr_err("%s: pcm add channel mixer weight controls failed:%d\n",
2773 __func__, ret);
2774 goto fail;
2775 }
2776 }
2777 return 0;
2778
2779fail:
2780 kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]);
2781 kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]);
2782 pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] = NULL;
2783 pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] = NULL;
2784
2785 return ret;
2786}
2787
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302788static int msm_pcm_add_controls(struct snd_soc_pcm_runtime *rtd)
2789{
2790 int ret = 0;
2791
2792 pr_debug("%s\n", __func__);
2793 ret = msm_pcm_add_chmap_controls(rtd);
2794 if (ret)
2795 pr_err("%s: pcm add controls failed:%d\n", __func__, ret);
2796 ret = msm_pcm_add_app_type_controls(rtd);
2797 if (ret)
2798 pr_err("%s: pcm add app type controls failed:%d\n",
2799 __func__, ret);
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05302800 ret = msm_pcm_add_channel_mixer_controls(rtd);
2801 if (ret)
2802 pr_err("%s: pcm add channel mixer controls failed:%d\n",
2803 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302804 return ret;
2805}
2806
2807static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
2808{
2809 struct snd_card *card = rtd->card->snd_card;
2810 int ret = 0;
2811
2812 if (!card->dev->coherent_dma_mask)
2813 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
2814
2815 ret = msm_pcm_add_controls(rtd);
2816 if (ret) {
2817 pr_err("%s, kctl add failed:%d\n", __func__, ret);
2818 return ret;
2819 }
2820
Haynes Mathew George86eb0ce2018-04-06 18:18:51 -07002821 ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_PLAYBACK);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302822 if (ret)
2823 pr_err("%s: Could not add pcm Volume Control %d\n",
2824 __func__, ret);
Haynes Mathew George86eb0ce2018-04-06 18:18:51 -07002825 ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_CAPTURE);
2826 if (ret)
2827 pr_err("%s: Could not add pcm Volume Control %d\n",
2828 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302829 ret = msm_pcm_add_compress_control(rtd);
2830 if (ret)
2831 pr_err("%s: Could not add pcm Compress Control %d\n",
2832 __func__, ret);
2833
2834 ret = msm_pcm_add_audio_adsp_stream_cmd_control(rtd);
2835 if (ret)
2836 pr_err("%s: Could not add pcm ADSP Stream Cmd Control\n",
2837 __func__);
2838
2839 ret = msm_pcm_add_audio_adsp_stream_callback_control(rtd);
2840 if (ret)
2841 pr_err("%s: Could not add pcm ADSP Stream Callback Control\n",
2842 __func__);
2843
2844 return ret;
2845}
2846
2847static snd_pcm_sframes_t msm_pcm_delay_blk(struct snd_pcm_substream *substream,
2848 struct snd_soc_dai *dai)
2849{
2850 struct snd_pcm_runtime *runtime = substream->runtime;
2851 struct msm_audio *prtd = runtime->private_data;
2852 struct audio_client *ac = prtd->audio_client;
2853 snd_pcm_sframes_t frames;
2854 int ret;
2855
2856 ret = q6asm_get_path_delay(prtd->audio_client);
2857 if (ret) {
2858 pr_err("%s: get_path_delay failed, ret=%d\n", __func__, ret);
2859 return 0;
2860 }
2861
2862 /* convert microseconds to frames */
2863 frames = ac->path_delay / 1000 * runtime->rate / 1000;
2864
2865 /* also convert the remainder from the initial division */
2866 frames += ac->path_delay % 1000 * runtime->rate / 1000000;
2867
2868 /* overcompensate for the loss of precision (empirical) */
2869 frames += 2;
2870
2871 return frames;
2872}
2873
Meng Wangee084a02018-09-04 16:11:58 +08002874static struct snd_soc_component_driver msm_soc_component = {
2875 .name = DRV_NAME,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302876 .ops = &msm_pcm_ops,
2877 .pcm_new = msm_asoc_pcm_new,
2878 .delay_blk = msm_pcm_delay_blk,
2879};
2880
2881static int msm_pcm_probe(struct platform_device *pdev)
2882{
2883 int rc;
2884 int id;
2885 struct msm_plat_data *pdata;
2886 const char *latency_level;
2887
2888 rc = of_property_read_u32(pdev->dev.of_node,
2889 "qcom,msm-pcm-dsp-id", &id);
2890 if (rc) {
2891 dev_err(&pdev->dev, "%s: qcom,msm-pcm-dsp-id missing in DT node\n",
2892 __func__);
2893 return rc;
2894 }
2895
2896 pdata = kzalloc(sizeof(struct msm_plat_data), GFP_KERNEL);
2897 if (!pdata)
2898 return -ENOMEM;
2899
2900 if (of_property_read_bool(pdev->dev.of_node,
2901 "qcom,msm-pcm-low-latency")) {
2902
2903 pdata->perf_mode = LOW_LATENCY_PCM_MODE;
2904 rc = of_property_read_string(pdev->dev.of_node,
2905 "qcom,latency-level", &latency_level);
2906 if (!rc) {
2907 if (!strcmp(latency_level, "ultra"))
2908 pdata->perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
2909 else if (!strcmp(latency_level, "ull-pp"))
2910 pdata->perf_mode =
2911 ULL_POST_PROCESSING_PCM_MODE;
2912 }
2913 } else {
2914 pdata->perf_mode = LEGACY_PCM_MODE;
2915 }
Ajit Pandeya557d962019-09-05 16:26:01 +05302916 mutex_init(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302917 dev_set_drvdata(&pdev->dev, pdata);
2918
2919
2920 dev_dbg(&pdev->dev, "%s: dev name %s\n",
2921 __func__, dev_name(&pdev->dev));
Meng Wangee084a02018-09-04 16:11:58 +08002922 return snd_soc_register_component(&pdev->dev,
2923 &msm_soc_component,
2924 NULL, 0);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302925}
2926
2927static int msm_pcm_remove(struct platform_device *pdev)
2928{
2929 struct msm_plat_data *pdata;
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05302930 int i = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302931
2932 pdata = dev_get_drvdata(&pdev->dev);
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05302933 if (pdata) {
2934 for (i = 0; i < MSM_FRONTEND_DAI_MM_SIZE; i++) {
2935 kfree(pdata->chmixer_pspd[i][SESSION_TYPE_RX]);
2936 kfree(pdata->chmixer_pspd[i][SESSION_TYPE_TX]);
2937 }
2938 }
Ajit Pandeya557d962019-09-05 16:26:01 +05302939 mutex_destroy(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302940 kfree(pdata);
Meng Wangee084a02018-09-04 16:11:58 +08002941 snd_soc_unregister_component(&pdev->dev);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302942 return 0;
2943}
2944static const struct of_device_id msm_pcm_dt_match[] = {
2945 {.compatible = "qcom,msm-pcm-dsp"},
2946 {}
2947};
2948MODULE_DEVICE_TABLE(of, msm_pcm_dt_match);
2949
2950static struct platform_driver msm_pcm_driver = {
2951 .driver = {
2952 .name = "msm-pcm-dsp",
2953 .owner = THIS_MODULE,
2954 .of_match_table = msm_pcm_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08002955 .suppress_bind_attrs = true,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302956 },
2957 .probe = msm_pcm_probe,
2958 .remove = msm_pcm_remove,
2959};
2960
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302961int __init msm_pcm_dsp_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302962{
2963 init_waitqueue_head(&the_locks.enable_wait);
2964 init_waitqueue_head(&the_locks.eos_wait);
2965 init_waitqueue_head(&the_locks.write_wait);
2966 init_waitqueue_head(&the_locks.read_wait);
2967
2968 return platform_driver_register(&msm_pcm_driver);
2969}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302970
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05302971void msm_pcm_dsp_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302972{
2973 platform_driver_unregister(&msm_pcm_driver);
2974}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302975
2976MODULE_DESCRIPTION("PCM module platform driver");
2977MODULE_LICENSE("GPL v2");