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