blob: c89efb9773bd29401586e89dd8504fe3b265c1e8 [file] [log] [blame]
Meng Wangac147b72017-10-30 16:46:16 +08001/* Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/init.h>
14#include <linux/err.h>
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/time.h>
18#include <linux/wait.h>
19#include <linux/platform_device.h>
20#include <linux/slab.h>
21#include <linux/of_device.h>
22#include <linux/dma-mapping.h>
Banajit Goswami08bb7362017-11-03 22:48:23 -070023#include <linux/dma-buf.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053024
25#include <sound/core.h>
26#include <sound/soc.h>
27#include <sound/soc-dapm.h>
28#include <sound/pcm.h>
29#include <sound/initval.h>
30#include <sound/control.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053031#include <sound/timer.h>
Asish Bhattacharya84f7f732017-07-25 16:29:27 +053032#include <sound/hwdep.h>
33
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053034#include <asm/dma.h>
35#include <sound/tlv.h>
36#include <sound/pcm_params.h>
Asish Bhattacharya84f7f732017-07-25 16:29:27 +053037#include <sound/devdep_params.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053038#include <dsp/msm_audio_ion.h>
39#include <dsp/q6audio-v2.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053040
41#include "msm-pcm-q6-v2.h"
42#include "msm-pcm-routing-v2.h"
43
44#define PCM_MASTER_VOL_MAX_STEPS 0x2000
45static const DECLARE_TLV_DB_LINEAR(msm_pcm_vol_gain, 0,
46 PCM_MASTER_VOL_MAX_STEPS);
47
48struct snd_msm {
49 struct snd_card *card;
50 struct snd_pcm *pcm;
51};
52
53#define CMD_EOS_MIN_TIMEOUT_LENGTH 50
54#define CMD_EOS_TIMEOUT_MULTIPLIER (HZ * 50)
55
56#define ATRACE_END() \
57 trace_printk("tracing_mark_write: E\n")
58#define ATRACE_BEGIN(name) \
59 trace_printk("tracing_mark_write: B|%d|%s\n", current->tgid, name)
60#define ATRACE_FUNC() ATRACE_BEGIN(__func__)
61#define ATRACE_INT(name, value) \
62 trace_printk("tracing_mark_write: C|%d|%s|%d\n", \
63 current->tgid, name, (int)(value))
64
65#define SIO_PLAYBACK_MAX_PERIOD_SIZE PLAYBACK_MAX_PERIOD_SIZE
66#define SIO_PLAYBACK_MIN_PERIOD_SIZE 48
67#define SIO_PLAYBACK_MAX_NUM_PERIODS 512
68#define SIO_PLAYBACK_MIN_NUM_PERIODS PLAYBACK_MIN_NUM_PERIODS
69#define SIO_PLAYBACK_MIN_BYTES (SIO_PLAYBACK_MIN_NUM_PERIODS * \
70 SIO_PLAYBACK_MIN_PERIOD_SIZE)
71
72#define SIO_PLAYBACK_MAX_BYTES ((SIO_PLAYBACK_MAX_NUM_PERIODS) * \
73 (SIO_PLAYBACK_MAX_PERIOD_SIZE))
74
75#define SIO_CAPTURE_MAX_PERIOD_SIZE CAPTURE_MAX_PERIOD_SIZE
76#define SIO_CAPTURE_MIN_PERIOD_SIZE 48
77#define SIO_CAPTURE_MAX_NUM_PERIODS 512
78#define SIO_CAPTURE_MIN_NUM_PERIODS CAPTURE_MIN_NUM_PERIODS
79
80#define SIO_CAPTURE_MIN_BYTES (SIO_CAPTURE_MIN_NUM_PERIODS * \
81 SIO_CAPTURE_MIN_PERIOD_SIZE)
82
83#define SIO_CAPTURE_MAX_BYTES (SIO_CAPTURE_MAX_NUM_PERIODS * \
84 SIO_CAPTURE_MAX_PERIOD_SIZE)
85
86static struct snd_pcm_hardware msm_pcm_hardware_playback = {
87 .info = (SNDRV_PCM_INFO_MMAP |
88 SNDRV_PCM_INFO_BLOCK_TRANSFER |
89 SNDRV_PCM_INFO_MMAP_VALID |
90 SNDRV_PCM_INFO_INTERLEAVED |
91 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP |
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 .rates = SNDRV_PCM_RATE_8000_192000,
97 .rate_min = 8000,
98 .rate_max = 192000,
99 .channels_min = 1,
100 .channels_max = 8,
101 .buffer_bytes_max = SIO_PLAYBACK_MAX_NUM_PERIODS *
102 SIO_PLAYBACK_MAX_PERIOD_SIZE,
103 .period_bytes_min = SIO_PLAYBACK_MIN_PERIOD_SIZE,
104 .period_bytes_max = SIO_PLAYBACK_MAX_PERIOD_SIZE,
105 .periods_min = SIO_PLAYBACK_MIN_NUM_PERIODS,
106 .periods_max = SIO_PLAYBACK_MAX_NUM_PERIODS,
107 .fifo_size = 0,
108};
109
110static struct snd_pcm_hardware msm_pcm_hardware_capture = {
111 .info = (SNDRV_PCM_INFO_MMAP |
112 SNDRV_PCM_INFO_BLOCK_TRANSFER |
113 SNDRV_PCM_INFO_MMAP_VALID |
114 SNDRV_PCM_INFO_INTERLEAVED |
115 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP |
116 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
117 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
118 SNDRV_PCM_FMTBIT_S24_LE |
119 SNDRV_PCM_FMTBIT_S24_3LE),
120 .rates = SNDRV_PCM_RATE_8000_48000,
121 .rate_min = 8000,
122 .rate_max = 48000,
123 .channels_min = 1,
124 .channels_max = 4,
125 .buffer_bytes_max = SIO_CAPTURE_MAX_NUM_PERIODS *
126 SIO_CAPTURE_MAX_PERIOD_SIZE,
127 .period_bytes_min = SIO_CAPTURE_MIN_PERIOD_SIZE,
128 .period_bytes_max = SIO_CAPTURE_MAX_PERIOD_SIZE,
129 .periods_min = SIO_CAPTURE_MIN_NUM_PERIODS,
130 .periods_max = SIO_CAPTURE_MAX_NUM_PERIODS,
131 .fifo_size = 0,
132};
133
134/* Conventional and unconventional sample rate supported */
135static unsigned int supported_sample_rates[] = {
136 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
137 88200, 96000, 176400, 192000
138};
139
140static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
141 .count = ARRAY_SIZE(supported_sample_rates),
142 .list = supported_sample_rates,
143 .mask = 0,
144};
145
146static unsigned long msm_pcm_fe_topology[MSM_FRONTEND_DAI_MAX];
147
148/* default value is DTS (i.e read from device tree) */
149static char const *msm_pcm_fe_topology_text[] = {
150 "DTS", "ULL", "ULL_PP", "LL" };
151
152static const struct soc_enum msm_pcm_fe_topology_enum[] = {
153 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(msm_pcm_fe_topology_text),
154 msm_pcm_fe_topology_text),
155};
156
157static void event_handler(uint32_t opcode,
158 uint32_t token, uint32_t *payload, void *priv)
159{
160 uint32_t *ptrmem = (uint32_t *)payload;
161
162 switch (opcode) {
163 case ASM_DATA_EVENT_WATERMARK:
164 pr_debug("%s: Watermark level = 0x%08x\n", __func__, *ptrmem);
165 break;
166 case APR_BASIC_RSP_RESULT:
167 pr_debug("%s: Payload = [0x%x]stat[0x%x]\n",
168 __func__, payload[0], payload[1]);
169 switch (payload[0]) {
170 case ASM_SESSION_CMD_RUN_V2:
171 case ASM_SESSION_CMD_PAUSE:
172 case ASM_STREAM_CMD_FLUSH:
173 break;
174 default:
175 break;
176 }
177 break;
178 default:
179 pr_debug("Not Supported Event opcode[0x%x]\n", opcode);
180 break;
181 }
182}
183
184static int msm_pcm_open(struct snd_pcm_substream *substream)
185{
186 struct snd_pcm_runtime *runtime = substream->runtime;
187 struct msm_audio *prtd;
188 int ret = 0;
189
190 prtd = kzalloc(sizeof(struct msm_audio), GFP_KERNEL);
191
192 if (prtd == NULL)
193 return -ENOMEM;
194
195 prtd->substream = substream;
196 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
197 runtime->hw = msm_pcm_hardware_playback;
198 else
199 runtime->hw = msm_pcm_hardware_capture;
200
201 ret = snd_pcm_hw_constraint_list(runtime, 0,
202 SNDRV_PCM_HW_PARAM_RATE,
203 &constraints_sample_rates);
204 if (ret)
205 pr_info("snd_pcm_hw_constraint_list failed\n");
206
207 ret = snd_pcm_hw_constraint_integer(runtime,
208 SNDRV_PCM_HW_PARAM_PERIODS);
209 if (ret)
210 pr_info("snd_pcm_hw_constraint_integer failed\n");
211
212 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
213 ret = snd_pcm_hw_constraint_minmax(runtime,
214 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
215 SIO_PLAYBACK_MIN_BYTES,
216 SIO_PLAYBACK_MAX_BYTES);
217 if (ret) {
218 pr_info("%s: P buffer bytes minmax constraint ret %d\n",
219 __func__, ret);
220 }
221 } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
222 ret = snd_pcm_hw_constraint_minmax(runtime,
223 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
224 SIO_CAPTURE_MIN_BYTES,
225 SIO_CAPTURE_MAX_BYTES);
226 if (ret) {
227 pr_info("%s: C buffer bytes minmax constraint ret %d\n",
228 __func__, ret);
229 }
230 }
231
232 ret = snd_pcm_hw_constraint_step(runtime, 0,
233 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
234 if (ret) {
235 pr_err("%s: Constraint for period bytes step ret = %d\n",
236 __func__, ret);
237 }
238 ret = snd_pcm_hw_constraint_step(runtime, 0,
239 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
240 if (ret) {
241 pr_err("%s: Constraint for buffer bytes step ret = %d\n",
242 __func__, ret);
243 }
244 prtd->audio_client = q6asm_audio_client_alloc(
245 (app_cb)event_handler, prtd);
246 if (!prtd->audio_client) {
247 pr_err("%s: client alloc failed\n", __func__);
248 ret = -ENOMEM;
249 goto fail_cmd;
250 }
251 prtd->dsp_cnt = 0;
252 prtd->set_channel_map = false;
253 runtime->private_data = prtd;
254 return 0;
255
256fail_cmd:
257 kfree(prtd);
258 return ret;
259}
260
261static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
262 struct snd_pcm_hw_params *params)
263
264{
265 struct snd_pcm_runtime *runtime = substream->runtime;
266 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
267 struct msm_audio *prtd = runtime->private_data;
268 struct msm_plat_data *pdata;
269 struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
270 struct audio_buffer *buf;
271 struct shared_io_config config;
272 uint16_t sample_word_size;
273 uint16_t bits_per_sample;
274 int ret;
275 int dir = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? IN : OUT;
276 unsigned long topology;
277 int perf_mode;
Surendar Karka5628e8b2018-07-23 10:41:58 +0530278 bool use_default_chmap = true;
279 char *chmap = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530280
281 pdata = (struct msm_plat_data *)
282 dev_get_drvdata(soc_prtd->platform->dev);
283 if (!pdata) {
284 ret = -EINVAL;
285 pr_err("%s: platform data not populated ret: %d\n", __func__,
286 ret);
287 return ret;
288 }
289
290 topology = msm_pcm_fe_topology[soc_prtd->dai_link->id];
291
292 if (!strcmp(msm_pcm_fe_topology_text[topology], "ULL_PP"))
293 perf_mode = ULL_POST_PROCESSING_PCM_MODE;
294 else if (!strcmp(msm_pcm_fe_topology_text[topology], "ULL"))
295 perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
296 else if (!strcmp(msm_pcm_fe_topology_text[topology], "LL"))
297 perf_mode = LOW_LATENCY_PCM_MODE;
298 else
299 /* use the default from the device tree */
300 perf_mode = pdata->perf_mode;
301
302
303 /* need to set LOW_LATENCY_PCM_MODE for capture since
304 * push mode does not support ULL
305 */
306 prtd->audio_client->perf_mode = (dir == IN) ?
307 perf_mode :
308 LOW_LATENCY_PCM_MODE;
309
310 /* rate and channels are sent to audio driver */
311 prtd->samp_rate = params_rate(params);
312 prtd->channel_mode = params_channels(params);
313 if (prtd->enabled)
314 return 0;
315
Surendar Karka5628e8b2018-07-23 10:41:58 +0530316 if (pdata->ch_map[soc_prtd->dai_link->id]) {
317 use_default_chmap =
318 !(pdata->ch_map[soc_prtd->dai_link->id]->set_ch_map);
319 chmap =
320 pdata->ch_map[soc_prtd->dai_link->id]->channel_map;
321 }
322
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530323 switch (runtime->format) {
324 case SNDRV_PCM_FORMAT_S24_LE:
325 bits_per_sample = 24;
326 sample_word_size = 32;
327 break;
328 case SNDRV_PCM_FORMAT_S24_3LE:
329 bits_per_sample = 24;
330 sample_word_size = 24;
331 break;
332 case SNDRV_PCM_FORMAT_S16_LE:
333 default:
334 bits_per_sample = 16;
335 sample_word_size = 16;
336 break;
337 }
338
339 config.format = FORMAT_LINEAR_PCM;
340 config.bits_per_sample = bits_per_sample;
341 config.rate = params_rate(params);
342 config.channels = params_channels(params);
343 config.sample_word_size = sample_word_size;
344 config.bufsz = params_buffer_bytes(params) / params_periods(params);
345 config.bufcnt = params_periods(params);
346
Surendar Karka5628e8b2018-07-23 10:41:58 +0530347 ret = q6asm_open_shared_io(prtd->audio_client, &config, dir,
348 use_default_chmap, chmap);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530349 if (ret) {
350 pr_err("%s: q6asm_open_write_shared_io failed ret: %d\n",
351 __func__, ret);
352 return ret;
353 }
354
355 prtd->pcm_size = params_buffer_bytes(params);
356 prtd->pcm_count = params_buffer_bytes(params);
357 prtd->pcm_irq_pos = 0;
358
359 buf = prtd->audio_client->port[dir].buf;
360 dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
361 dma_buf->dev.dev = substream->pcm->card->dev;
362 dma_buf->private_data = NULL;
363 dma_buf->area = buf->data;
364 dma_buf->addr = buf->phys;
365 dma_buf->bytes = prtd->pcm_size;
366
367 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
368
369 pr_debug("%s: session ID %d, perf %d\n", __func__,
370 prtd->audio_client->session,
371 prtd->audio_client->perf_mode);
372 prtd->session_id = prtd->audio_client->session;
373
374 pr_debug("msm_pcm_routing_reg_phy_stream w/ id %d\n",
375 soc_prtd->dai_link->id);
376 ret = msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->id,
377 prtd->audio_client->perf_mode,
378 prtd->session_id, substream->stream);
379
380 if (ret) {
381 pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
382 return ret;
383 }
384
385 atomic_set(&prtd->out_count, runtime->periods);
386 prtd->enabled = 1;
387 prtd->cmd_pending = 0;
388 prtd->cmd_interrupt = 0;
389
390 return 0;
391}
392
393static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
394{
395 int ret = 0;
396 struct snd_pcm_runtime *runtime = substream->runtime;
397 struct msm_audio *prtd = runtime->private_data;
398 int dir = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 0 : 1;
399 struct audio_buffer *buf;
400
401 switch (cmd) {
402 case SNDRV_PCM_TRIGGER_START:
403 case SNDRV_PCM_TRIGGER_RESUME:
404 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
405 pr_debug("%s: %s Trigger start\n", __func__,
406 dir == 0 ? "P" : "C");
407 ret = q6asm_run(prtd->audio_client, 0, 0, 0);
408 if (ret)
409 break;
410 atomic_set(&prtd->start, 1);
411 break;
412 case SNDRV_PCM_TRIGGER_STOP:
413 pr_debug("%s: SNDRV_PCM_TRIGGER_STOP\n", __func__);
414 atomic_set(&prtd->start, 0);
415 q6asm_cmd(prtd->audio_client, CMD_PAUSE);
416 q6asm_cmd(prtd->audio_client, CMD_FLUSH);
417 buf = q6asm_shared_io_buf(prtd->audio_client, dir);
418 if (buf == NULL) {
419 pr_err("%s: shared IO buffer is null\n", __func__);
420 ret = -EINVAL;
421 break;
422 }
423 memset(buf->data, 0, buf->actual_size);
424 break;
425 case SNDRV_PCM_TRIGGER_SUSPEND:
426 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
427 pr_debug("%s: SNDRV_PCM_TRIGGER_PAUSE\n", __func__);
428 ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
429 atomic_set(&prtd->start, 0);
430 break;
431 default:
432 ret = -EINVAL;
433 break;
434 }
435 return ret;
436}
437
Asish Bhattacharya84f7f732017-07-25 16:29:27 +0530438
439static int msm_pcm_mmap_fd(struct snd_pcm_substream *substream,
440 struct snd_pcm_mmap_fd *mmap_fd)
441{
442 struct msm_audio *prtd;
443 struct audio_port_data *apd;
444 struct audio_buffer *ab;
445 int dir = -1;
446
447 if (!substream->runtime) {
448 pr_err("%s substream runtime not found\n", __func__);
449 return -EFAULT;
450 }
451
452 prtd = substream->runtime->private_data;
453 if (!prtd || !prtd->audio_client || !prtd->mmap_flag) {
454 pr_err("%s no audio client or not an mmap session\n", __func__);
455 return -EINVAL;
456 }
457
458 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
459 dir = IN;
460 else
461 dir = OUT;
462
463 apd = prtd->audio_client->port;
464 ab = &(apd[dir].buf[0]);
Banajit Goswami08bb7362017-11-03 22:48:23 -0700465 /*
466 * Passing O_CLOEXEC as flag passed to fd, to be in sync with
467 * previous implimentation.
468 * This was the flag used by previous internal wrapper API, which
469 * used to call dma_buf_fd internally.
470 */
471 mmap_fd->fd = dma_buf_fd(ab->dma_buf, O_CLOEXEC);
Asish Bhattacharya84f7f732017-07-25 16:29:27 +0530472 if (mmap_fd->fd >= 0) {
473 mmap_fd->dir = dir;
474 mmap_fd->actual_size = ab->actual_size;
475 mmap_fd->size = ab->size;
476 }
477 return mmap_fd->fd < 0 ? -EFAULT : 0;
478}
479
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530480static int msm_pcm_ioctl(struct snd_pcm_substream *substream,
481 unsigned int cmd, void *arg)
482{
483 struct snd_pcm_runtime *runtime = substream->runtime;
484 struct msm_audio *prtd = runtime->private_data;
485 int dir = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 0 : 1;
486 struct audio_buffer *buf;
487
488 switch (cmd) {
489 case SNDRV_PCM_IOCTL1_RESET:
490 pr_debug("%s: %s SNDRV_PCM_IOCTL1_RESET\n", __func__,
491 dir == 0 ? "P" : "C");
492 buf = q6asm_shared_io_buf(prtd->audio_client, dir);
493
494 if (buf && buf->data)
495 memset(buf->data, 0, buf->actual_size);
496 break;
497 default:
498 break;
499 }
500
501 return snd_pcm_lib_ioctl(substream, cmd, arg);
502}
503
Asish Bhattacharya84f7f732017-07-25 16:29:27 +0530504#ifdef CONFIG_COMPAT
505static int msm_pcm_compat_ioctl(struct snd_pcm_substream *substream,
506 unsigned int cmd, void *arg)
507{
508 /* we only handle RESET which is common for both modes */
509 return msm_pcm_ioctl(substream, cmd, arg);
510}
511#endif
512
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530513static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
514{
515 struct snd_pcm_runtime *runtime = substream->runtime;
516 uint32_t read_index, wall_clk_msw, wall_clk_lsw;
517 /*these are offsets, unlike ASoC's full values*/
518 snd_pcm_sframes_t hw_ptr;
519 snd_pcm_sframes_t period_size;
520 int ret;
521 int retries = 10;
522 struct msm_audio *prtd = runtime->private_data;
523
524 period_size = runtime->period_size;
525
526 do {
527 ret = q6asm_get_shared_pos(prtd->audio_client,
528 &read_index, &wall_clk_msw,
529 &wall_clk_lsw);
530 } while (ret == -EAGAIN && --retries);
531
532 if (ret || !period_size) {
533 pr_err("get_shared_pos error or zero period size\n");
534 return 0;
535 }
536
537 hw_ptr = bytes_to_frames(substream->runtime,
538 read_index);
539
540 if (runtime->control->appl_ptr == 0) {
541 pr_debug("ptr(%s): appl(0), hw = %lu read_index = %u\n",
542 prtd->substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
543 "P" : "C",
544 hw_ptr, read_index);
545 }
546 return (hw_ptr/period_size) * period_size;
547}
548
549static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
Meng Wangac147b72017-10-30 16:46:16 +0800550 unsigned long hwoff, void __user *buf, unsigned long fbytes)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530551{
552 return -EINVAL;
553}
554
555static int msm_pcm_mmap(struct snd_pcm_substream *substream,
556 struct vm_area_struct *vma)
557{
558 struct snd_pcm_runtime *runtime = substream->runtime;
559 struct msm_audio *prtd = runtime->private_data;
560 struct audio_client *ac = prtd->audio_client;
561 struct audio_port_data *apd = ac->port;
562 struct audio_buffer *ab;
563 int dir = -1;
564 int ret;
565
566 pr_debug("%s: mmap begin\n", __func__);
567 prtd->mmap_flag = 1;
568
569 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
570 dir = IN;
571 else
572 dir = OUT;
573
574 ab = &(apd[dir].buf[0]);
575
576 ret = msm_audio_ion_mmap(ab, vma);
577
578 if (ret)
579 prtd->mmap_flag = 0;
580
581 return ret;
582}
583
584static int msm_pcm_prepare(struct snd_pcm_substream *substream)
585{
586 struct snd_pcm_runtime *runtime = substream->runtime;
587 struct msm_audio *prtd = runtime->private_data;
588
589 if (!prtd || !prtd->mmap_flag)
590 return -EIO;
591
592 return 0;
593}
594
595static int msm_pcm_close(struct snd_pcm_substream *substream)
596{
597 struct snd_pcm_runtime *runtime = substream->runtime;
598 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
599 struct msm_audio *prtd = runtime->private_data;
600 struct audio_client *ac = prtd->audio_client;
601 uint32_t timeout;
602 int dir = 0;
603 int ret = 0;
604
605 if (ac) {
606 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
607 dir = IN;
608 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
609 dir = OUT;
610
611 /* determine timeout length */
612 if (runtime->frame_bits == 0 || runtime->rate == 0) {
613 timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
614 } else {
615 timeout = (runtime->period_size *
616 CMD_EOS_TIMEOUT_MULTIPLIER) /
617 ((runtime->frame_bits / 8) *
618 runtime->rate);
619 if (timeout < CMD_EOS_MIN_TIMEOUT_LENGTH)
620 timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
621 }
622
623 q6asm_cmd(ac, CMD_CLOSE);
624
625 ret = q6asm_shared_io_free(ac, dir);
626
627 if (ret) {
628 pr_err("%s: Failed to close pull mode, ret %d\n",
629 __func__, ret);
630 }
631 q6asm_audio_client_free(ac);
632 }
633 msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
634 dir == IN ?
635 SNDRV_PCM_STREAM_PLAYBACK :
636 SNDRV_PCM_STREAM_CAPTURE);
637 kfree(prtd);
638 runtime->private_data = NULL;
639
640 return 0;
641}
642
643static int msm_pcm_set_volume(struct msm_audio *prtd, uint32_t volume)
644{
645 int rc = 0;
646
647 if (prtd && prtd->audio_client) {
648 pr_debug("%s: channels %d volume 0x%x\n", __func__,
649 prtd->channel_mode, volume);
650 rc = q6asm_set_volume(prtd->audio_client, volume);
651 if (rc < 0) {
652 pr_err("%s: Send Volume command failed rc=%d\n",
653 __func__, rc);
654 }
655 }
656 return rc;
657}
658
659static int msm_pcm_volume_ctl_get(struct snd_kcontrol *kcontrol,
660 struct snd_ctl_elem_value *ucontrol)
661{
662 struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
663 struct snd_pcm_substream *substream =
664 vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
665 struct msm_audio *prtd;
666
667 pr_debug("%s\n", __func__);
668 if (!substream) {
669 pr_err("%s substream not found\n", __func__);
670 return -ENODEV;
671 }
672 if (!substream->runtime) {
673 pr_err("%s substream runtime not found\n", __func__);
674 return 0;
675 }
676 prtd = substream->runtime->private_data;
677 if (prtd)
678 ucontrol->value.integer.value[0] = prtd->volume;
679 return 0;
680}
681
682static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
683 struct snd_ctl_elem_value *ucontrol)
684{
685 int rc = 0;
686 struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
687 struct snd_pcm_substream *substream =
688 vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
689 struct msm_audio *prtd;
690 int volume = ucontrol->value.integer.value[0];
691
692 pr_debug("%s: volume : 0x%x\n", __func__, volume);
693 if (!substream) {
694 pr_err("%s substream not found\n", __func__);
695 return -ENODEV;
696 }
697 if (!substream->runtime) {
698 pr_err("%s substream runtime not found\n", __func__);
699 return 0;
700 }
701 prtd = substream->runtime->private_data;
702 if (prtd) {
703 rc = msm_pcm_set_volume(prtd, volume);
704 prtd->volume = volume;
705 }
706 return rc;
707}
708
709static int msm_pcm_add_volume_control(struct snd_soc_pcm_runtime *rtd)
710{
711 int ret = 0;
712 struct snd_pcm *pcm = rtd->pcm;
713 struct snd_pcm_volume *volume_info;
714 struct snd_kcontrol *kctl;
715
716 dev_dbg(rtd->dev, "%s, Volume control add\n", __func__);
717 ret = snd_pcm_add_volume_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
718 NULL, 1, rtd->dai_link->id,
719 &volume_info);
720 if (ret < 0) {
721 pr_err("%s volume control failed ret %d\n", __func__, ret);
722 return ret;
723 }
724 kctl = volume_info->kctl;
725 kctl->put = msm_pcm_volume_ctl_put;
726 kctl->get = msm_pcm_volume_ctl_get;
727 kctl->tlv.p = msm_pcm_vol_gain;
728 return 0;
729}
730
Surendar Karka5628e8b2018-07-23 10:41:58 +0530731static int msm_pcm_channel_map_put(struct snd_kcontrol *kcontrol,
732 struct snd_ctl_elem_value *ucontrol)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530733{
Surendar Karka5628e8b2018-07-23 10:41:58 +0530734 struct snd_soc_component *pcm = snd_kcontrol_chip(kcontrol);
735 u64 fe_id = kcontrol->private_value;
736 struct msm_plat_data *pdata = (struct msm_plat_data *)
737 snd_soc_component_get_drvdata(pcm);
738 int rc = 0, i = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530739
Surendar Karka5628e8b2018-07-23 10:41:58 +0530740 pr_debug("%s: fe_id- %llu\n", __func__, fe_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530741
Surendar Karka5628e8b2018-07-23 10:41:58 +0530742 if (fe_id >= MSM_FRONTEND_DAI_MAX) {
743 pr_err("%s Received out of bounds fe_id %llu\n",
744 __func__, fe_id);
745 rc = -EINVAL;
746 goto end;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530747 }
Surendar Karka5628e8b2018-07-23 10:41:58 +0530748
749 if (pdata->ch_map[fe_id]) {
750 pdata->ch_map[fe_id]->set_ch_map = true;
751 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
752 pdata->ch_map[fe_id]->channel_map[i] =
753 (char)(ucontrol->value.integer.value[i]);
754 } else {
755 pr_debug("%s: no memory for ch_map, default will be set\n",
756 __func__);
757 }
758end:
759 pr_debug("%s: ret %d\n", __func__, rc);
760 return rc;
761}
762
763static int msm_pcm_channel_map_info(struct snd_kcontrol *kcontrol,
764 struct snd_ctl_elem_info *uinfo)
765{
766 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
767 uinfo->count = 8;
768 uinfo->value.integer.min = 0;
769 uinfo->value.integer.max = 0xFFFFFFFF;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530770 return 0;
771}
772
Surendar Karka5628e8b2018-07-23 10:41:58 +0530773static int msm_pcm_channel_map_get(struct snd_kcontrol *kcontrol,
774 struct snd_ctl_elem_value *ucontrol)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530775{
Surendar Karka5628e8b2018-07-23 10:41:58 +0530776 struct snd_soc_component *pcm = snd_kcontrol_chip(kcontrol);
777 u64 fe_id = kcontrol->private_value;
778 struct msm_plat_data *pdata = (struct msm_plat_data *)
779 snd_soc_component_get_drvdata(pcm);
780 int rc = 0, i = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530781
Surendar Karka5628e8b2018-07-23 10:41:58 +0530782 pr_debug("%s: fe_id- %llu\n", __func__, fe_id);
783 if (fe_id >= MSM_FRONTEND_DAI_MAX) {
784 pr_err("%s: Received out of bounds fe_id %llu\n",
785 __func__, fe_id);
786 rc = -EINVAL;
787 goto end;
788 }
789 if (pdata->ch_map[fe_id]) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530790 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
791 ucontrol->value.integer.value[i] =
Surendar Karka5628e8b2018-07-23 10:41:58 +0530792 pdata->ch_map[fe_id]->channel_map[i];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530793 }
Surendar Karka5628e8b2018-07-23 10:41:58 +0530794end:
795 pr_debug("%s: ret %d\n", __func__, rc);
796 return rc;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530797}
798
Surendar Karka5628e8b2018-07-23 10:41:58 +0530799static int msm_pcm_add_channel_map_control(struct snd_soc_pcm_runtime *rtd)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530800{
Surendar Karka5628e8b2018-07-23 10:41:58 +0530801 const char *mixer_ctl_name = "Playback Channel Map";
802 const char *deviceNo = "NN";
803 char *mixer_str = NULL;
804 struct msm_plat_data *pdata = NULL;
805 int ctl_len = 0;
806 struct snd_kcontrol_new fe_channel_map_control[1] = {
807 {
808 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
809 .name = "?",
810 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
811 .info = msm_pcm_channel_map_info,
812 .get = msm_pcm_channel_map_get,
813 .put = msm_pcm_channel_map_put,
814 .private_value = 0,
815 }
816 };
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530817
Surendar Karka5628e8b2018-07-23 10:41:58 +0530818 if (!rtd) {
819 pr_err("%s: NULL rtd\n", __func__);
820 return -EINVAL;
821 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530822
Surendar Karka5628e8b2018-07-23 10:41:58 +0530823 pr_debug("%s: added new pcm FE with name %s, id %d, cpu dai %s, device no %d\n",
824 __func__, rtd->dai_link->name, rtd->dai_link->id,
825 rtd->dai_link->cpu_dai_name, rtd->pcm->device);
826
827 ctl_len = strlen(mixer_ctl_name) + strlen(deviceNo) + 1;
828 mixer_str = kzalloc(ctl_len, GFP_KERNEL);
829 if (!mixer_str)
830 return -ENOMEM;
831
832 snprintf(mixer_str, ctl_len, "%s%d", mixer_ctl_name, rtd->pcm->device);
833
834 fe_channel_map_control[0].name = mixer_str;
835 fe_channel_map_control[0].private_value = rtd->dai_link->id;
836 pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
837 snd_soc_add_platform_controls(rtd->platform,
838 fe_channel_map_control,
839 ARRAY_SIZE(fe_channel_map_control));
840
841 pdata = snd_soc_platform_get_drvdata(rtd->platform);
842 pdata->ch_map[rtd->dai_link->id] =
843 kzalloc(sizeof(struct msm_pcm_ch_map), GFP_KERNEL);
844 if (!pdata->ch_map[rtd->dai_link->id]) {
845 pr_err("%s: Could not allocate memory for channel map\n",
846 __func__);
847 kfree(mixer_str);
848 return -ENOMEM;
849 }
850 kfree(mixer_str);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530851 return 0;
852}
853
854static int msm_pcm_fe_topology_info(struct snd_kcontrol *kcontrol,
855 struct snd_ctl_elem_info *uinfo)
856{
857 const struct soc_enum *e = &msm_pcm_fe_topology_enum[0];
858
859 return snd_ctl_enum_info(uinfo, 1, e->items, e->texts);
860}
861
862static int msm_pcm_fe_topology_get(struct snd_kcontrol *kcontrol,
863 struct snd_ctl_elem_value *ucontrol)
864{
865 unsigned long fe_id = kcontrol->private_value;
866
867 if (fe_id >= MSM_FRONTEND_DAI_MAX) {
868 pr_err("%s Received out of bound fe_id %lu\n", __func__, fe_id);
869 return -EINVAL;
870 }
871
872 pr_debug("%s: %lu topology %s\n", __func__, fe_id,
873 msm_pcm_fe_topology_text[msm_pcm_fe_topology[fe_id]]);
874 ucontrol->value.enumerated.item[0] = msm_pcm_fe_topology[fe_id];
875 return 0;
876}
877
878static int msm_pcm_fe_topology_put(struct snd_kcontrol *kcontrol,
879 struct snd_ctl_elem_value *ucontrol)
880{
881 unsigned long fe_id = kcontrol->private_value;
882 unsigned int item;
883
884 if (fe_id >= MSM_FRONTEND_DAI_MAX) {
885 pr_err("%s Received out of bound fe_id %lu\n", __func__, fe_id);
886 return -EINVAL;
887 }
888
889 item = ucontrol->value.enumerated.item[0];
890 if (item >= ARRAY_SIZE(msm_pcm_fe_topology_text)) {
891 pr_err("%s Received out of bound topology %lu\n", __func__,
892 fe_id);
893 return -EINVAL;
894 }
895
896 pr_debug("%s: %lu new topology %s\n", __func__, fe_id,
897 msm_pcm_fe_topology_text[item]);
898 msm_pcm_fe_topology[fe_id] = item;
899 return 0;
900}
901
902static int msm_pcm_add_fe_topology_control(struct snd_soc_pcm_runtime *rtd)
903{
904 const char *mixer_ctl_name = "PCM_Dev";
905 const char *deviceNo = "NN";
906 const char *topo_text = "Topology";
907 char *mixer_str = NULL;
908 int ctl_len;
909 int ret;
910 struct snd_kcontrol_new topology_control[1] = {
911 {
912 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
913 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
914 .name = "?",
915 .info = msm_pcm_fe_topology_info,
916 .get = msm_pcm_fe_topology_get,
917 .put = msm_pcm_fe_topology_put,
918 .private_value = 0,
919 },
920 };
921
922 ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1 +
923 strlen(topo_text) + 1;
924 mixer_str = kzalloc(ctl_len, GFP_KERNEL);
925
926 if (!mixer_str)
927 return -ENOMEM;
928
929 snprintf(mixer_str, ctl_len, "%s %d %s", mixer_ctl_name,
930 rtd->pcm->device, topo_text);
931
932 topology_control[0].name = mixer_str;
933 topology_control[0].private_value = rtd->dai_link->id;
934 ret = snd_soc_add_platform_controls(rtd->platform, topology_control,
935 ARRAY_SIZE(topology_control));
936 msm_pcm_fe_topology[rtd->dai_link->id] = 0;
937 kfree(mixer_str);
938 return ret;
939}
940
941static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
942 struct snd_ctl_elem_value *ucontrol)
943{
944 u64 fe_id = kcontrol->private_value;
945 int session_type = SESSION_TYPE_RX;
946 int be_id = ucontrol->value.integer.value[3];
947 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
948 int ret = 0;
949
950 cfg_data.app_type = ucontrol->value.integer.value[0];
951 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
952 if (ucontrol->value.integer.value[2] != 0)
953 cfg_data.sample_rate = ucontrol->value.integer.value[2];
954 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
955 __func__, fe_id, session_type, be_id,
956 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
957 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
958 be_id, &cfg_data);
959 if (ret < 0)
960 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
961 __func__, ret);
962 return ret;
963}
964
965static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
966 struct snd_ctl_elem_value *ucontrol)
967{
968 u64 fe_id = kcontrol->private_value;
969 int session_type = SESSION_TYPE_RX;
970 int be_id = 0;
971 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
972 int ret = 0;
973
974 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
975 &be_id, &cfg_data);
976 if (ret < 0) {
977 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
978 __func__, ret);
979 goto done;
980 }
981
982 ucontrol->value.integer.value[0] = cfg_data.app_type;
983 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
984 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
985 ucontrol->value.integer.value[3] = be_id;
986 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
987 __func__, fe_id, session_type, be_id,
988 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
989done:
990 return ret;
991}
992
993static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
994 struct snd_ctl_elem_value *ucontrol)
995{
996 u64 fe_id = kcontrol->private_value;
997 int session_type = SESSION_TYPE_TX;
998 int be_id = ucontrol->value.integer.value[3];
999 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
1000 int ret = 0;
1001
1002 cfg_data.app_type = ucontrol->value.integer.value[0];
1003 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
1004 if (ucontrol->value.integer.value[2] != 0)
1005 cfg_data.sample_rate = ucontrol->value.integer.value[2];
1006 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
1007 __func__, fe_id, session_type, be_id,
1008 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1009 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
1010 be_id, &cfg_data);
1011 if (ret < 0)
1012 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
1013 __func__, ret);
1014
1015 return ret;
1016}
1017
1018static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
1019 struct snd_ctl_elem_value *ucontrol)
1020{
1021 u64 fe_id = kcontrol->private_value;
1022 int session_type = SESSION_TYPE_TX;
1023 int be_id = 0;
1024 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
1025 int ret = 0;
1026
1027 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
1028 &be_id, &cfg_data);
1029 if (ret < 0) {
1030 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
1031 __func__, ret);
1032 goto done;
1033 }
1034
1035 ucontrol->value.integer.value[0] = cfg_data.app_type;
1036 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
1037 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
1038 ucontrol->value.integer.value[3] = be_id;
1039 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
1040 __func__, fe_id, session_type, be_id,
1041 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1042done:
1043 return ret;
1044}
1045
1046static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
1047{
1048 struct snd_pcm *pcm = rtd->pcm;
1049 struct snd_pcm_usr *app_type_info;
1050 struct snd_kcontrol *kctl;
1051 const char *playback_mixer_ctl_name = "Audio Stream";
1052 const char *capture_mixer_ctl_name = "Audio Stream Capture";
1053 const char *deviceNo = "NN";
1054 const char *suffix = "App Type Cfg";
1055 int ctl_len, ret = 0;
1056
1057 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
1058 ctl_len = strlen(playback_mixer_ctl_name) + 1 +
1059 strlen(deviceNo) + 1 +
1060 strlen(suffix) + 1;
1061 pr_debug("%s: Playback app type cntrl add\n", __func__);
1062 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1063 NULL, 1, ctl_len, rtd->dai_link->id,
1064 &app_type_info);
1065 if (ret < 0) {
1066 pr_err("%s: playback app type cntrl add failed, err: %d\n",
1067 __func__, ret);
1068 return ret;
1069 }
1070 kctl = app_type_info->kctl;
1071 snprintf(kctl->id.name, ctl_len, "%s %d %s",
1072 playback_mixer_ctl_name, rtd->pcm->device, suffix);
1073 kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
1074 kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
1075 }
1076
1077 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
1078 ctl_len = strlen(capture_mixer_ctl_name) + 1 +
1079 strlen(deviceNo) + 1 + strlen(suffix) + 1;
1080 pr_debug("%s: Capture app type cntrl add\n", __func__);
1081 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
1082 NULL, 1, ctl_len, rtd->dai_link->id,
1083 &app_type_info);
1084 if (ret < 0) {
1085 pr_err("%s: capture app type cntrl add failed, err: %d\n",
1086 __func__, ret);
1087 return ret;
1088 }
1089 kctl = app_type_info->kctl;
1090 snprintf(kctl->id.name, ctl_len, "%s %d %s",
1091 capture_mixer_ctl_name, rtd->pcm->device, suffix);
1092 kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
1093 kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
1094 }
1095
1096 return 0;
1097}
1098
Asish Bhattacharya84f7f732017-07-25 16:29:27 +05301099static int msm_pcm_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
1100 unsigned int cmd, unsigned long arg)
1101{
1102 int ret = 0;
1103 struct snd_pcm *pcm = hw->private_data;
1104 struct snd_pcm_mmap_fd __user *_mmap_fd = NULL;
1105 struct snd_pcm_mmap_fd mmap_fd;
1106 struct snd_pcm_substream *substream = NULL;
1107 int32_t dir = -1;
1108
1109 switch (cmd) {
1110 case SNDRV_PCM_IOCTL_MMAP_DATA_FD:
1111 _mmap_fd = (struct snd_pcm_mmap_fd __user *)arg;
1112 if (get_user(dir, (int32_t __user *)&(_mmap_fd->dir))) {
1113 pr_err("%s: error copying mmap_fd from user\n",
1114 __func__);
1115 ret = -EFAULT;
1116 break;
1117 }
1118 if (dir != OUT && dir != IN) {
1119 pr_err("%s invalid stream dir\n", __func__);
1120 ret = -EINVAL;
1121 break;
1122 }
1123 substream = pcm->streams[dir].substream;
1124 if (!substream) {
1125 pr_err("%s substream not found\n", __func__);
1126 ret = -ENODEV;
1127 break;
1128 }
1129 pr_debug("%s : %s MMAP Data fd\n", __func__,
1130 dir == 0 ? "P" : "C");
1131 if (msm_pcm_mmap_fd(substream, &mmap_fd) < 0) {
1132 pr_err("%s: error getting fd\n",
1133 __func__);
1134 ret = -EFAULT;
1135 break;
1136 }
1137 if (put_user(mmap_fd.fd, &_mmap_fd->fd) ||
1138 put_user(mmap_fd.size, &_mmap_fd->size) ||
1139 put_user(mmap_fd.actual_size, &_mmap_fd->actual_size)) {
1140 pr_err("%s: error copying fd\n", __func__);
1141 return -EFAULT;
1142 }
1143 break;
1144 default:
1145 ret = -EINVAL;
1146 break;
1147 }
1148 return ret;
1149}
1150
1151#ifdef CONFIG_COMPAT
1152static int msm_pcm_hwdep_compat_ioctl(struct snd_hwdep *hw,
1153 struct file *file,
1154 unsigned int cmd,
1155 unsigned long arg)
1156{
1157 /* we only support mmap fd. Handling is common in both modes */
1158 return msm_pcm_hwdep_ioctl(hw, file, cmd, arg);
1159}
1160#else
1161static int msm_pcm_hwdep_compat_ioctl(struct snd_hwdep *hw,
1162 struct file *file,
1163 unsigned int cmd,
1164 unsigned long arg)
1165{
1166 return -EINVAL;
1167}
1168#endif
1169
1170static int msm_pcm_add_hwdep_dev(struct snd_soc_pcm_runtime *runtime)
1171{
1172 struct snd_hwdep *hwdep;
1173 int rc;
1174 char id[] = "NOIRQ_NN";
1175
1176 snprintf(id, sizeof(id), "NOIRQ_%d", runtime->pcm->device);
1177 pr_debug("%s: pcm dev %d\n", __func__, runtime->pcm->device);
1178 rc = snd_hwdep_new(runtime->card->snd_card,
1179 &id[0],
1180 HWDEP_FE_BASE + runtime->pcm->device,
1181 &hwdep);
1182 if (!hwdep || rc < 0) {
1183 pr_err("%s: hwdep intf failed to create %s - hwdep\n", __func__,
1184 id);
1185 return rc;
1186 }
1187
1188 hwdep->iface = SNDRV_HWDEP_IFACE_AUDIO_BE; /* for lack of a FE iface */
1189 hwdep->private_data = runtime->pcm; /* of type struct snd_pcm */
1190 hwdep->ops.ioctl = msm_pcm_hwdep_ioctl;
1191 hwdep->ops.ioctl_compat = msm_pcm_hwdep_compat_ioctl;
1192 return 0;
1193}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301194
1195static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
1196{
1197 struct snd_card *card = rtd->card->snd_card;
1198 struct snd_pcm *pcm = rtd->pcm;
1199 int ret;
1200
1201 pr_debug("%s , register new control\n", __func__);
1202 if (!card->dev->coherent_dma_mask)
1203 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
1204
Surendar Karka5628e8b2018-07-23 10:41:58 +05301205 ret = msm_pcm_add_channel_map_control(rtd);
1206 if (ret)
1207 pr_err("%s: Could not add pcm Channel Map Control\n",
1208 __func__);
1209
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301210 ret = msm_pcm_add_volume_control(rtd);
1211 if (ret) {
1212 pr_err("%s: Could not add pcm Volume Control %d\n",
1213 __func__, ret);
1214 }
1215
1216 ret = msm_pcm_add_fe_topology_control(rtd);
1217 if (ret) {
1218 pr_err("%s: Could not add pcm topology control %d\n",
1219 __func__, ret);
1220 }
1221
1222 ret = msm_pcm_add_app_type_controls(rtd);
1223 if (ret) {
1224 pr_err("%s: Could not add app type controls failed %d\n",
1225 __func__, ret);
1226 }
Asish Bhattacharya84f7f732017-07-25 16:29:27 +05301227 ret = msm_pcm_add_hwdep_dev(rtd);
1228 if (ret)
1229 pr_err("%s: Could not add hw dep node\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301230 pcm->nonatomic = true;
Surendar Karka5628e8b2018-07-23 10:41:58 +05301231
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301232 return ret;
1233}
1234
1235
1236static const struct snd_pcm_ops msm_pcm_ops = {
1237 .open = msm_pcm_open,
1238 .prepare = msm_pcm_prepare,
Meng Wangac147b72017-10-30 16:46:16 +08001239 .copy_user = msm_pcm_copy,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301240 .hw_params = msm_pcm_hw_params,
1241 .ioctl = msm_pcm_ioctl,
Asish Bhattacharya84f7f732017-07-25 16:29:27 +05301242#ifdef CONFIG_COMPAT
1243 .compat_ioctl = msm_pcm_compat_ioctl,
1244#endif
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301245 .trigger = msm_pcm_trigger,
1246 .pointer = msm_pcm_pointer,
1247 .mmap = msm_pcm_mmap,
1248 .close = msm_pcm_close,
1249};
1250
1251static struct snd_soc_platform_driver msm_soc_platform = {
1252 .ops = &msm_pcm_ops,
1253 .pcm_new = msm_asoc_pcm_new,
1254};
1255
1256static int msm_pcm_probe(struct platform_device *pdev)
1257{
1258 int rc;
1259 struct msm_plat_data *pdata;
1260 const char *latency_level;
1261 int perf_mode = LOW_LATENCY_PCM_MODE;
1262
1263 dev_dbg(&pdev->dev, "Pull mode driver probe\n");
1264
1265 if (of_property_read_bool(pdev->dev.of_node,
1266 "qcom,msm-pcm-low-latency")) {
1267
1268 rc = of_property_read_string(pdev->dev.of_node,
1269 "qcom,latency-level", &latency_level);
1270 if (!rc) {
1271 if (!strcmp(latency_level, "ultra"))
1272 perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
1273 else if (!strcmp(latency_level, "ull-pp"))
1274 perf_mode = ULL_POST_PROCESSING_PCM_MODE;
1275 }
1276 }
1277
1278 pdata = devm_kzalloc(&pdev->dev,
1279 sizeof(struct msm_plat_data), GFP_KERNEL);
1280 if (!pdata)
1281 return -ENOMEM;
1282
1283 pdata->perf_mode = perf_mode;
1284
1285 dev_set_drvdata(&pdev->dev, pdata);
1286
1287 dev_dbg(&pdev->dev, "%s: dev name %s\n",
1288 __func__, dev_name(&pdev->dev));
1289 dev_dbg(&pdev->dev, "Pull mode driver register\n");
1290 rc = snd_soc_register_platform(&pdev->dev,
1291 &msm_soc_platform);
1292
1293 if (rc)
1294 dev_err(&pdev->dev, "Failed to register pull mode driver\n");
1295
1296 return rc;
1297}
1298
1299static int msm_pcm_remove(struct platform_device *pdev)
1300{
1301 struct msm_plat_data *pdata;
1302
1303 dev_dbg(&pdev->dev, "Pull mode remove\n");
1304 pdata = dev_get_drvdata(&pdev->dev);
1305 devm_kfree(&pdev->dev, pdata);
1306 snd_soc_unregister_platform(&pdev->dev);
1307 return 0;
1308}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301309static const struct of_device_id msm_pcm_noirq_dt_match[] = {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301310 {.compatible = "qcom,msm-pcm-dsp-noirq"},
1311 {}
1312};
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301313MODULE_DEVICE_TABLE(of, msm_pcm_noirq_dt_match);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301314
1315static struct platform_driver msm_pcm_driver_noirq = {
1316 .driver = {
1317 .name = "msm-pcm-dsp-noirq",
1318 .owner = THIS_MODULE,
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301319 .of_match_table = msm_pcm_noirq_dt_match,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301320 },
1321 .probe = msm_pcm_probe,
1322 .remove = msm_pcm_remove,
1323};
1324
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301325int __init msm_pcm_noirq_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301326{
1327 return platform_driver_register(&msm_pcm_driver_noirq);
1328}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301329
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05301330void msm_pcm_noirq_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301331{
1332 platform_driver_unregister(&msm_pcm_driver_noirq);
1333}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301334
1335MODULE_DESCRIPTION("PCM NOIRQ module platform driver");
1336MODULE_LICENSE("GPL v2");