blob: 06f48bac7411214f551860523cd93e53e26013cf [file] [log] [blame]
Prasad Kumpatlaade35f02019-10-15 15:06:44 +05301/* Copyright (c) 2016-2019, 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>
Prasad Kumpatlaade35f02019-10-15 15:06:44 +053018#include <linux/mutex.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053019#include <linux/wait.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22#include <linux/of_device.h>
23#include <linux/dma-mapping.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;
278
279 pdata = (struct msm_plat_data *)
280 dev_get_drvdata(soc_prtd->platform->dev);
281 if (!pdata) {
282 ret = -EINVAL;
283 pr_err("%s: platform data not populated ret: %d\n", __func__,
284 ret);
285 return ret;
286 }
287
288 topology = msm_pcm_fe_topology[soc_prtd->dai_link->id];
289
290 if (!strcmp(msm_pcm_fe_topology_text[topology], "ULL_PP"))
291 perf_mode = ULL_POST_PROCESSING_PCM_MODE;
292 else if (!strcmp(msm_pcm_fe_topology_text[topology], "ULL"))
293 perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
294 else if (!strcmp(msm_pcm_fe_topology_text[topology], "LL"))
295 perf_mode = LOW_LATENCY_PCM_MODE;
296 else
297 /* use the default from the device tree */
298 perf_mode = pdata->perf_mode;
299
300
301 /* need to set LOW_LATENCY_PCM_MODE for capture since
302 * push mode does not support ULL
303 */
304 prtd->audio_client->perf_mode = (dir == IN) ?
305 perf_mode :
306 LOW_LATENCY_PCM_MODE;
307
308 /* rate and channels are sent to audio driver */
309 prtd->samp_rate = params_rate(params);
310 prtd->channel_mode = params_channels(params);
311 if (prtd->enabled)
312 return 0;
313
314 switch (runtime->format) {
315 case SNDRV_PCM_FORMAT_S24_LE:
316 bits_per_sample = 24;
317 sample_word_size = 32;
318 break;
319 case SNDRV_PCM_FORMAT_S24_3LE:
320 bits_per_sample = 24;
321 sample_word_size = 24;
322 break;
323 case SNDRV_PCM_FORMAT_S16_LE:
324 default:
325 bits_per_sample = 16;
326 sample_word_size = 16;
327 break;
328 }
329
330 config.format = FORMAT_LINEAR_PCM;
331 config.bits_per_sample = bits_per_sample;
332 config.rate = params_rate(params);
333 config.channels = params_channels(params);
334 config.sample_word_size = sample_word_size;
335 config.bufsz = params_buffer_bytes(params) / params_periods(params);
336 config.bufcnt = params_periods(params);
337
338 ret = q6asm_open_shared_io(prtd->audio_client, &config, dir);
339 if (ret) {
340 pr_err("%s: q6asm_open_write_shared_io failed ret: %d\n",
341 __func__, ret);
342 return ret;
343 }
344
345 prtd->pcm_size = params_buffer_bytes(params);
346 prtd->pcm_count = params_buffer_bytes(params);
347 prtd->pcm_irq_pos = 0;
348
349 buf = prtd->audio_client->port[dir].buf;
350 dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
351 dma_buf->dev.dev = substream->pcm->card->dev;
352 dma_buf->private_data = NULL;
353 dma_buf->area = buf->data;
354 dma_buf->addr = buf->phys;
355 dma_buf->bytes = prtd->pcm_size;
356
357 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
358
359 pr_debug("%s: session ID %d, perf %d\n", __func__,
360 prtd->audio_client->session,
361 prtd->audio_client->perf_mode);
362 prtd->session_id = prtd->audio_client->session;
363
364 pr_debug("msm_pcm_routing_reg_phy_stream w/ id %d\n",
365 soc_prtd->dai_link->id);
366 ret = msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->id,
367 prtd->audio_client->perf_mode,
368 prtd->session_id, substream->stream);
369
370 if (ret) {
371 pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
372 return ret;
373 }
374
375 atomic_set(&prtd->out_count, runtime->periods);
376 prtd->enabled = 1;
377 prtd->cmd_pending = 0;
378 prtd->cmd_interrupt = 0;
379
380 return 0;
381}
382
383static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
384{
385 int ret = 0;
386 struct snd_pcm_runtime *runtime = substream->runtime;
387 struct msm_audio *prtd = runtime->private_data;
388 int dir = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 0 : 1;
389 struct audio_buffer *buf;
390
391 switch (cmd) {
392 case SNDRV_PCM_TRIGGER_START:
393 case SNDRV_PCM_TRIGGER_RESUME:
394 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
395 pr_debug("%s: %s Trigger start\n", __func__,
396 dir == 0 ? "P" : "C");
397 ret = q6asm_run(prtd->audio_client, 0, 0, 0);
398 if (ret)
399 break;
400 atomic_set(&prtd->start, 1);
401 break;
402 case SNDRV_PCM_TRIGGER_STOP:
403 pr_debug("%s: SNDRV_PCM_TRIGGER_STOP\n", __func__);
404 atomic_set(&prtd->start, 0);
405 q6asm_cmd(prtd->audio_client, CMD_PAUSE);
406 q6asm_cmd(prtd->audio_client, CMD_FLUSH);
407 buf = q6asm_shared_io_buf(prtd->audio_client, dir);
408 if (buf == NULL) {
409 pr_err("%s: shared IO buffer is null\n", __func__);
410 ret = -EINVAL;
411 break;
412 }
413 memset(buf->data, 0, buf->actual_size);
414 break;
415 case SNDRV_PCM_TRIGGER_SUSPEND:
416 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
417 pr_debug("%s: SNDRV_PCM_TRIGGER_PAUSE\n", __func__);
418 ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
419 atomic_set(&prtd->start, 0);
420 break;
421 default:
422 ret = -EINVAL;
423 break;
424 }
425 return ret;
426}
427
Asish Bhattacharya84f7f732017-07-25 16:29:27 +0530428
429static int msm_pcm_mmap_fd(struct snd_pcm_substream *substream,
430 struct snd_pcm_mmap_fd *mmap_fd)
431{
432 struct msm_audio *prtd;
433 struct audio_port_data *apd;
434 struct audio_buffer *ab;
435 int dir = -1;
436
437 if (!substream->runtime) {
438 pr_err("%s substream runtime not found\n", __func__);
439 return -EFAULT;
440 }
441
442 prtd = substream->runtime->private_data;
443 if (!prtd || !prtd->audio_client || !prtd->mmap_flag) {
444 pr_err("%s no audio client or not an mmap session\n", __func__);
445 return -EINVAL;
446 }
447
448 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
449 dir = IN;
450 else
451 dir = OUT;
452
453 apd = prtd->audio_client->port;
454 ab = &(apd[dir].buf[0]);
455 mmap_fd->fd = ion_share_dma_buf_fd(ab->client, ab->handle);
456 if (mmap_fd->fd >= 0) {
457 mmap_fd->dir = dir;
458 mmap_fd->actual_size = ab->actual_size;
459 mmap_fd->size = ab->size;
460 }
461 return mmap_fd->fd < 0 ? -EFAULT : 0;
462}
463
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530464static int msm_pcm_ioctl(struct snd_pcm_substream *substream,
465 unsigned int cmd, void *arg)
466{
467 struct snd_pcm_runtime *runtime = substream->runtime;
468 struct msm_audio *prtd = runtime->private_data;
469 int dir = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 0 : 1;
470 struct audio_buffer *buf;
471
472 switch (cmd) {
473 case SNDRV_PCM_IOCTL1_RESET:
474 pr_debug("%s: %s SNDRV_PCM_IOCTL1_RESET\n", __func__,
475 dir == 0 ? "P" : "C");
476 buf = q6asm_shared_io_buf(prtd->audio_client, dir);
477
478 if (buf && buf->data)
479 memset(buf->data, 0, buf->actual_size);
480 break;
481 default:
482 break;
483 }
484
485 return snd_pcm_lib_ioctl(substream, cmd, arg);
486}
487
Asish Bhattacharya84f7f732017-07-25 16:29:27 +0530488#ifdef CONFIG_COMPAT
489static int msm_pcm_compat_ioctl(struct snd_pcm_substream *substream,
490 unsigned int cmd, void *arg)
491{
492 /* we only handle RESET which is common for both modes */
493 return msm_pcm_ioctl(substream, cmd, arg);
494}
495#endif
496
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530497static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
498{
499 struct snd_pcm_runtime *runtime = substream->runtime;
500 uint32_t read_index, wall_clk_msw, wall_clk_lsw;
501 /*these are offsets, unlike ASoC's full values*/
502 snd_pcm_sframes_t hw_ptr;
503 snd_pcm_sframes_t period_size;
504 int ret;
505 int retries = 10;
506 struct msm_audio *prtd = runtime->private_data;
507
508 period_size = runtime->period_size;
509
510 do {
511 ret = q6asm_get_shared_pos(prtd->audio_client,
512 &read_index, &wall_clk_msw,
513 &wall_clk_lsw);
514 } while (ret == -EAGAIN && --retries);
515
516 if (ret || !period_size) {
517 pr_err("get_shared_pos error or zero period size\n");
518 return 0;
519 }
520
521 hw_ptr = bytes_to_frames(substream->runtime,
522 read_index);
523
524 if (runtime->control->appl_ptr == 0) {
525 pr_debug("ptr(%s): appl(0), hw = %lu read_index = %u\n",
526 prtd->substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
527 "P" : "C",
528 hw_ptr, read_index);
529 }
530 return (hw_ptr/period_size) * period_size;
531}
532
533static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
534 snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
535{
536 return -EINVAL;
537}
538
539static int msm_pcm_mmap(struct snd_pcm_substream *substream,
540 struct vm_area_struct *vma)
541{
542 struct snd_pcm_runtime *runtime = substream->runtime;
543 struct msm_audio *prtd = runtime->private_data;
544 struct audio_client *ac = prtd->audio_client;
545 struct audio_port_data *apd = ac->port;
546 struct audio_buffer *ab;
547 int dir = -1;
548 int ret;
549
550 pr_debug("%s: mmap begin\n", __func__);
551 prtd->mmap_flag = 1;
552
553 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
554 dir = IN;
555 else
556 dir = OUT;
557
558 ab = &(apd[dir].buf[0]);
559
560 ret = msm_audio_ion_mmap(ab, vma);
561
562 if (ret)
563 prtd->mmap_flag = 0;
564
565 return ret;
566}
567
568static int msm_pcm_prepare(struct snd_pcm_substream *substream)
569{
570 struct snd_pcm_runtime *runtime = substream->runtime;
571 struct msm_audio *prtd = runtime->private_data;
572
573 if (!prtd || !prtd->mmap_flag)
574 return -EIO;
575
576 return 0;
577}
578
579static int msm_pcm_close(struct snd_pcm_substream *substream)
580{
Prasad Kumpatlaade35f02019-10-15 15:06:44 +0530581 struct msm_plat_data *pdata = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530582 struct snd_pcm_runtime *runtime = substream->runtime;
583 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
584 struct msm_audio *prtd = runtime->private_data;
585 struct audio_client *ac = prtd->audio_client;
586 uint32_t timeout;
587 int dir = 0;
588 int ret = 0;
589
Prasad Kumpatlaade35f02019-10-15 15:06:44 +0530590 if (!soc_prtd) {
591 pr_debug("%s private_data not found\n",
592 __func__);
593 return 0;
594 }
595
596 pdata = (struct msm_plat_data *)
597 dev_get_drvdata(soc_prtd->platform->dev);
598 if (!pdata) {
599 pr_err("%s: pdata not found\n", __func__);
600 return -ENODEV;
601 }
602
603 mutex_lock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530604 if (ac) {
605 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
606 dir = IN;
607 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
608 dir = OUT;
609
610 /* determine timeout length */
611 if (runtime->frame_bits == 0 || runtime->rate == 0) {
612 timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
613 } else {
614 timeout = (runtime->period_size *
615 CMD_EOS_TIMEOUT_MULTIPLIER) /
616 ((runtime->frame_bits / 8) *
617 runtime->rate);
618 if (timeout < CMD_EOS_MIN_TIMEOUT_LENGTH)
619 timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
620 }
621
622 q6asm_cmd(ac, CMD_CLOSE);
623
624 ret = q6asm_shared_io_free(ac, dir);
625
626 if (ret) {
627 pr_err("%s: Failed to close pull mode, ret %d\n",
628 __func__, ret);
629 }
630 q6asm_audio_client_free(ac);
631 }
632 msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
633 dir == IN ?
634 SNDRV_PCM_STREAM_PLAYBACK :
635 SNDRV_PCM_STREAM_CAPTURE);
636 kfree(prtd);
637 runtime->private_data = NULL;
Prasad Kumpatlaade35f02019-10-15 15:06:44 +0530638 mutex_unlock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530639
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);
Prasad Kumpatlaade35f02019-10-15 15:06:44 +0530663 struct msm_plat_data *pdata = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530664 struct snd_pcm_substream *substream =
Haynes Mathew Georged00e1062018-04-06 18:18:51 -0700665 vol->pcm->streams[vol->stream].substream;
Prasad Kumpatlaade35f02019-10-15 15:06:44 +0530666 struct snd_soc_pcm_runtime *soc_prtd = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530667 struct msm_audio *prtd;
668
669 pr_debug("%s\n", __func__);
670 if (!substream) {
671 pr_err("%s substream not found\n", __func__);
672 return -ENODEV;
673 }
Prasad Kumpatlaade35f02019-10-15 15:06:44 +0530674 soc_prtd = substream->private_data;
675 if (!substream->runtime || !soc_prtd) {
676 pr_debug("%s substream runtime or private_data not found\n",
677 __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530678 return 0;
679 }
Prasad Kumpatlaade35f02019-10-15 15:06:44 +0530680
681 pdata = (struct msm_plat_data *)
682 dev_get_drvdata(soc_prtd->platform->dev);
683 if (!pdata) {
684 pr_err("%s: pdata not found\n", __func__);
685 return -ENODEV;
686 }
687 mutex_lock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530688 prtd = substream->runtime->private_data;
689 if (prtd)
690 ucontrol->value.integer.value[0] = prtd->volume;
Prasad Kumpatlaade35f02019-10-15 15:06:44 +0530691 mutex_unlock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530692 return 0;
693}
694
695static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
696 struct snd_ctl_elem_value *ucontrol)
697{
698 int rc = 0;
699 struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
Prasad Kumpatlaade35f02019-10-15 15:06:44 +0530700 struct msm_plat_data *pdata = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530701 struct snd_pcm_substream *substream =
Haynes Mathew Georged00e1062018-04-06 18:18:51 -0700702 vol->pcm->streams[vol->stream].substream;
Prasad Kumpatlaade35f02019-10-15 15:06:44 +0530703 struct snd_soc_pcm_runtime *soc_prtd = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530704 struct msm_audio *prtd;
705 int volume = ucontrol->value.integer.value[0];
706
707 pr_debug("%s: volume : 0x%x\n", __func__, volume);
708 if (!substream) {
709 pr_err("%s substream not found\n", __func__);
710 return -ENODEV;
711 }
Prasad Kumpatlaade35f02019-10-15 15:06:44 +0530712 soc_prtd = substream->private_data;
713 if (!substream->runtime || !soc_prtd) {
714 pr_err("%s substream runtime or private_data not found\n",
715 __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530716 return 0;
717 }
Prasad Kumpatlaade35f02019-10-15 15:06:44 +0530718
719 pdata = (struct msm_plat_data *)
720 dev_get_drvdata(soc_prtd->platform->dev);
721 if (!pdata) {
722 pr_err("%s: pdata not found\n", __func__);
723 return -ENODEV;
724 }
725 mutex_lock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530726 prtd = substream->runtime->private_data;
727 if (prtd) {
728 rc = msm_pcm_set_volume(prtd, volume);
729 prtd->volume = volume;
730 }
Prasad Kumpatlaade35f02019-10-15 15:06:44 +0530731 mutex_unlock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530732 return rc;
733}
734
Haynes Mathew Georged00e1062018-04-06 18:18:51 -0700735static int msm_pcm_add_volume_control(struct snd_soc_pcm_runtime *rtd,
736 int stream)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530737{
738 int ret = 0;
739 struct snd_pcm *pcm = rtd->pcm;
740 struct snd_pcm_volume *volume_info;
741 struct snd_kcontrol *kctl;
742
Haynes Mathew Georged00e1062018-04-06 18:18:51 -0700743 dev_dbg(rtd->dev, "%s, volume control add\n", __func__);
744 ret = snd_pcm_add_volume_ctls(pcm, stream,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530745 NULL, 1, rtd->dai_link->id,
746 &volume_info);
747 if (ret < 0) {
748 pr_err("%s volume control failed ret %d\n", __func__, ret);
749 return ret;
750 }
751 kctl = volume_info->kctl;
752 kctl->put = msm_pcm_volume_ctl_put;
753 kctl->get = msm_pcm_volume_ctl_get;
754 kctl->tlv.p = msm_pcm_vol_gain;
755 return 0;
756}
757
758static int msm_pcm_chmap_ctl_put(struct snd_kcontrol *kcontrol,
759 struct snd_ctl_elem_value *ucontrol)
760{
761 int i;
762 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
763 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
764 struct snd_pcm_substream *substream;
765 struct msm_audio *prtd;
766
767 pr_debug("%s", __func__);
768 substream = snd_pcm_chmap_substream(info, idx);
769 if (!substream)
770 return -ENODEV;
771 if (!substream->runtime)
772 return 0;
773
774 prtd = substream->runtime->private_data;
775 if (prtd) {
776 prtd->set_channel_map = true;
777 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
778 prtd->channel_map[i] =
779 (char)(ucontrol->value.integer.value[i]);
780 }
781 return 0;
782}
783
784static int msm_pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
785 struct snd_ctl_elem_value *ucontrol)
786{
787 int i;
788 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
789 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
790 struct snd_pcm_substream *substream;
791 struct msm_audio *prtd;
792
793 pr_debug("%s", __func__);
794 substream = snd_pcm_chmap_substream(info, idx);
795 if (!substream)
796 return -ENODEV;
797 memset(ucontrol->value.integer.value, 0,
798 sizeof(ucontrol->value.integer.value));
799 if (!substream->runtime)
800 return 0; /* no channels set */
801
802 prtd = substream->runtime->private_data;
803
804 if (prtd && prtd->set_channel_map == true) {
805 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
806 ucontrol->value.integer.value[i] =
807 (int)prtd->channel_map[i];
808 } else {
809 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
810 ucontrol->value.integer.value[i] = 0;
811 }
812
813 return 0;
814}
815
816static int msm_pcm_add_chmap_control(struct snd_soc_pcm_runtime *rtd)
817{
818 struct snd_pcm *pcm = rtd->pcm;
819 struct snd_pcm_chmap *chmap_info;
820 struct snd_kcontrol *kctl;
821 char device_num[12];
822 int i, ret;
823
824 pr_debug("%s, Channel map cntrl add\n", __func__);
825 ret = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
826 snd_pcm_std_chmaps,
827 PCM_FORMAT_MAX_NUM_CHANNEL, 0,
828 &chmap_info);
829 if (ret)
830 return ret;
831
832 kctl = chmap_info->kctl;
833 for (i = 0; i < kctl->count; i++)
834 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
835 snprintf(device_num, sizeof(device_num), "%d", pcm->device);
836 strlcat(kctl->id.name, device_num, sizeof(kctl->id.name));
837 pr_debug("%s, Overwriting channel map control name to: %s",
838 __func__, kctl->id.name);
839 kctl->put = msm_pcm_chmap_ctl_put;
840 kctl->get = msm_pcm_chmap_ctl_get;
841 return 0;
842}
843
844static int msm_pcm_fe_topology_info(struct snd_kcontrol *kcontrol,
845 struct snd_ctl_elem_info *uinfo)
846{
847 const struct soc_enum *e = &msm_pcm_fe_topology_enum[0];
848
849 return snd_ctl_enum_info(uinfo, 1, e->items, e->texts);
850}
851
852static int msm_pcm_fe_topology_get(struct snd_kcontrol *kcontrol,
853 struct snd_ctl_elem_value *ucontrol)
854{
855 unsigned long fe_id = kcontrol->private_value;
856
857 if (fe_id >= MSM_FRONTEND_DAI_MAX) {
858 pr_err("%s Received out of bound fe_id %lu\n", __func__, fe_id);
859 return -EINVAL;
860 }
861
862 pr_debug("%s: %lu topology %s\n", __func__, fe_id,
863 msm_pcm_fe_topology_text[msm_pcm_fe_topology[fe_id]]);
864 ucontrol->value.enumerated.item[0] = msm_pcm_fe_topology[fe_id];
865 return 0;
866}
867
868static int msm_pcm_fe_topology_put(struct snd_kcontrol *kcontrol,
869 struct snd_ctl_elem_value *ucontrol)
870{
871 unsigned long fe_id = kcontrol->private_value;
872 unsigned int item;
873
874 if (fe_id >= MSM_FRONTEND_DAI_MAX) {
875 pr_err("%s Received out of bound fe_id %lu\n", __func__, fe_id);
876 return -EINVAL;
877 }
878
879 item = ucontrol->value.enumerated.item[0];
880 if (item >= ARRAY_SIZE(msm_pcm_fe_topology_text)) {
881 pr_err("%s Received out of bound topology %lu\n", __func__,
882 fe_id);
883 return -EINVAL;
884 }
885
886 pr_debug("%s: %lu new topology %s\n", __func__, fe_id,
887 msm_pcm_fe_topology_text[item]);
888 msm_pcm_fe_topology[fe_id] = item;
889 return 0;
890}
891
892static int msm_pcm_add_fe_topology_control(struct snd_soc_pcm_runtime *rtd)
893{
894 const char *mixer_ctl_name = "PCM_Dev";
895 const char *deviceNo = "NN";
896 const char *topo_text = "Topology";
897 char *mixer_str = NULL;
898 int ctl_len;
899 int ret;
900 struct snd_kcontrol_new topology_control[1] = {
901 {
902 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
903 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
904 .name = "?",
905 .info = msm_pcm_fe_topology_info,
906 .get = msm_pcm_fe_topology_get,
907 .put = msm_pcm_fe_topology_put,
908 .private_value = 0,
909 },
910 };
911
912 ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1 +
913 strlen(topo_text) + 1;
914 mixer_str = kzalloc(ctl_len, GFP_KERNEL);
915
916 if (!mixer_str)
917 return -ENOMEM;
918
919 snprintf(mixer_str, ctl_len, "%s %d %s", mixer_ctl_name,
920 rtd->pcm->device, topo_text);
921
922 topology_control[0].name = mixer_str;
923 topology_control[0].private_value = rtd->dai_link->id;
924 ret = snd_soc_add_platform_controls(rtd->platform, topology_control,
925 ARRAY_SIZE(topology_control));
926 msm_pcm_fe_topology[rtd->dai_link->id] = 0;
927 kfree(mixer_str);
928 return ret;
929}
930
931static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
932 struct snd_ctl_elem_value *ucontrol)
933{
934 u64 fe_id = kcontrol->private_value;
935 int session_type = SESSION_TYPE_RX;
936 int be_id = ucontrol->value.integer.value[3];
937 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
938 int ret = 0;
939
940 cfg_data.app_type = ucontrol->value.integer.value[0];
941 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
942 if (ucontrol->value.integer.value[2] != 0)
943 cfg_data.sample_rate = ucontrol->value.integer.value[2];
944 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
945 __func__, fe_id, session_type, be_id,
946 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
947 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
948 be_id, &cfg_data);
949 if (ret < 0)
950 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
951 __func__, ret);
952 return ret;
953}
954
955static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
956 struct snd_ctl_elem_value *ucontrol)
957{
958 u64 fe_id = kcontrol->private_value;
959 int session_type = SESSION_TYPE_RX;
960 int be_id = 0;
961 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
962 int ret = 0;
963
964 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
965 &be_id, &cfg_data);
966 if (ret < 0) {
967 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
968 __func__, ret);
969 goto done;
970 }
971
972 ucontrol->value.integer.value[0] = cfg_data.app_type;
973 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
974 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
975 ucontrol->value.integer.value[3] = be_id;
976 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
977 __func__, fe_id, session_type, be_id,
978 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
979done:
980 return ret;
981}
982
983static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
984 struct snd_ctl_elem_value *ucontrol)
985{
986 u64 fe_id = kcontrol->private_value;
987 int session_type = SESSION_TYPE_TX;
988 int be_id = ucontrol->value.integer.value[3];
989 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
990 int ret = 0;
991
992 cfg_data.app_type = ucontrol->value.integer.value[0];
993 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
994 if (ucontrol->value.integer.value[2] != 0)
995 cfg_data.sample_rate = ucontrol->value.integer.value[2];
996 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
997 __func__, fe_id, session_type, be_id,
998 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
999 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
1000 be_id, &cfg_data);
1001 if (ret < 0)
1002 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
1003 __func__, ret);
1004
1005 return ret;
1006}
1007
1008static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
1009 struct snd_ctl_elem_value *ucontrol)
1010{
1011 u64 fe_id = kcontrol->private_value;
1012 int session_type = SESSION_TYPE_TX;
1013 int be_id = 0;
1014 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
1015 int ret = 0;
1016
1017 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
1018 &be_id, &cfg_data);
1019 if (ret < 0) {
1020 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
1021 __func__, ret);
1022 goto done;
1023 }
1024
1025 ucontrol->value.integer.value[0] = cfg_data.app_type;
1026 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
1027 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
1028 ucontrol->value.integer.value[3] = be_id;
1029 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
1030 __func__, fe_id, session_type, be_id,
1031 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1032done:
1033 return ret;
1034}
1035
1036static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
1037{
1038 struct snd_pcm *pcm = rtd->pcm;
1039 struct snd_pcm_usr *app_type_info;
1040 struct snd_kcontrol *kctl;
1041 const char *playback_mixer_ctl_name = "Audio Stream";
1042 const char *capture_mixer_ctl_name = "Audio Stream Capture";
1043 const char *deviceNo = "NN";
1044 const char *suffix = "App Type Cfg";
1045 int ctl_len, ret = 0;
1046
1047 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
1048 ctl_len = strlen(playback_mixer_ctl_name) + 1 +
1049 strlen(deviceNo) + 1 +
1050 strlen(suffix) + 1;
1051 pr_debug("%s: Playback app type cntrl add\n", __func__);
1052 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1053 NULL, 1, ctl_len, rtd->dai_link->id,
1054 &app_type_info);
1055 if (ret < 0) {
1056 pr_err("%s: playback app type cntrl add failed, err: %d\n",
1057 __func__, ret);
1058 return ret;
1059 }
1060 kctl = app_type_info->kctl;
1061 snprintf(kctl->id.name, ctl_len, "%s %d %s",
1062 playback_mixer_ctl_name, rtd->pcm->device, suffix);
1063 kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
1064 kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
1065 }
1066
1067 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
1068 ctl_len = strlen(capture_mixer_ctl_name) + 1 +
1069 strlen(deviceNo) + 1 + strlen(suffix) + 1;
1070 pr_debug("%s: Capture app type cntrl add\n", __func__);
1071 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
1072 NULL, 1, ctl_len, rtd->dai_link->id,
1073 &app_type_info);
1074 if (ret < 0) {
1075 pr_err("%s: capture app type cntrl add failed, err: %d\n",
1076 __func__, ret);
1077 return ret;
1078 }
1079 kctl = app_type_info->kctl;
1080 snprintf(kctl->id.name, ctl_len, "%s %d %s",
1081 capture_mixer_ctl_name, rtd->pcm->device, suffix);
1082 kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
1083 kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
1084 }
1085
1086 return 0;
1087}
1088
Asish Bhattacharya84f7f732017-07-25 16:29:27 +05301089static int msm_pcm_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
1090 unsigned int cmd, unsigned long arg)
1091{
1092 int ret = 0;
1093 struct snd_pcm *pcm = hw->private_data;
1094 struct snd_pcm_mmap_fd __user *_mmap_fd = NULL;
1095 struct snd_pcm_mmap_fd mmap_fd;
1096 struct snd_pcm_substream *substream = NULL;
1097 int32_t dir = -1;
1098
1099 switch (cmd) {
1100 case SNDRV_PCM_IOCTL_MMAP_DATA_FD:
1101 _mmap_fd = (struct snd_pcm_mmap_fd __user *)arg;
1102 if (get_user(dir, (int32_t __user *)&(_mmap_fd->dir))) {
1103 pr_err("%s: error copying mmap_fd from user\n",
1104 __func__);
1105 ret = -EFAULT;
1106 break;
1107 }
1108 if (dir != OUT && dir != IN) {
1109 pr_err("%s invalid stream dir\n", __func__);
1110 ret = -EINVAL;
1111 break;
1112 }
1113 substream = pcm->streams[dir].substream;
1114 if (!substream) {
1115 pr_err("%s substream not found\n", __func__);
1116 ret = -ENODEV;
1117 break;
1118 }
1119 pr_debug("%s : %s MMAP Data fd\n", __func__,
1120 dir == 0 ? "P" : "C");
1121 if (msm_pcm_mmap_fd(substream, &mmap_fd) < 0) {
1122 pr_err("%s: error getting fd\n",
1123 __func__);
1124 ret = -EFAULT;
1125 break;
1126 }
1127 if (put_user(mmap_fd.fd, &_mmap_fd->fd) ||
1128 put_user(mmap_fd.size, &_mmap_fd->size) ||
1129 put_user(mmap_fd.actual_size, &_mmap_fd->actual_size)) {
1130 pr_err("%s: error copying fd\n", __func__);
1131 return -EFAULT;
1132 }
1133 break;
1134 default:
1135 ret = -EINVAL;
1136 break;
1137 }
1138 return ret;
1139}
1140
1141#ifdef CONFIG_COMPAT
1142static int msm_pcm_hwdep_compat_ioctl(struct snd_hwdep *hw,
1143 struct file *file,
1144 unsigned int cmd,
1145 unsigned long arg)
1146{
1147 /* we only support mmap fd. Handling is common in both modes */
1148 return msm_pcm_hwdep_ioctl(hw, file, cmd, arg);
1149}
1150#else
1151static int msm_pcm_hwdep_compat_ioctl(struct snd_hwdep *hw,
1152 struct file *file,
1153 unsigned int cmd,
1154 unsigned long arg)
1155{
1156 return -EINVAL;
1157}
1158#endif
1159
1160static int msm_pcm_add_hwdep_dev(struct snd_soc_pcm_runtime *runtime)
1161{
1162 struct snd_hwdep *hwdep;
1163 int rc;
1164 char id[] = "NOIRQ_NN";
1165
1166 snprintf(id, sizeof(id), "NOIRQ_%d", runtime->pcm->device);
1167 pr_debug("%s: pcm dev %d\n", __func__, runtime->pcm->device);
1168 rc = snd_hwdep_new(runtime->card->snd_card,
1169 &id[0],
1170 HWDEP_FE_BASE + runtime->pcm->device,
1171 &hwdep);
1172 if (!hwdep || rc < 0) {
1173 pr_err("%s: hwdep intf failed to create %s - hwdep\n", __func__,
1174 id);
1175 return rc;
1176 }
1177
1178 hwdep->iface = SNDRV_HWDEP_IFACE_AUDIO_BE; /* for lack of a FE iface */
1179 hwdep->private_data = runtime->pcm; /* of type struct snd_pcm */
1180 hwdep->ops.ioctl = msm_pcm_hwdep_ioctl;
1181 hwdep->ops.ioctl_compat = msm_pcm_hwdep_compat_ioctl;
1182 return 0;
1183}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301184
1185static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
1186{
1187 struct snd_card *card = rtd->card->snd_card;
1188 struct snd_pcm *pcm = rtd->pcm;
1189 int ret;
1190
1191 pr_debug("%s , register new control\n", __func__);
1192 if (!card->dev->coherent_dma_mask)
1193 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
1194
1195 ret = msm_pcm_add_chmap_control(rtd);
1196 if (ret) {
1197 pr_err("%s failed to add chmap cntls\n", __func__);
1198 goto exit;
1199 }
Haynes Mathew Georged00e1062018-04-06 18:18:51 -07001200 ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_PLAYBACK);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301201 if (ret) {
Haynes Mathew Georged00e1062018-04-06 18:18:51 -07001202 pr_err("%s: Could not add pcm playback volume Control %d\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301203 __func__, ret);
1204 }
Haynes Mathew Georged00e1062018-04-06 18:18:51 -07001205 ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_CAPTURE);
1206 if (ret) {
1207 pr_err("%s: Could not add pcm capture volume Control %d\n",
1208 __func__, ret);
1209 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301210 ret = msm_pcm_add_fe_topology_control(rtd);
1211 if (ret) {
1212 pr_err("%s: Could not add pcm topology control %d\n",
1213 __func__, ret);
1214 }
1215
1216 ret = msm_pcm_add_app_type_controls(rtd);
1217 if (ret) {
1218 pr_err("%s: Could not add app type controls failed %d\n",
1219 __func__, ret);
1220 }
Asish Bhattacharya84f7f732017-07-25 16:29:27 +05301221 ret = msm_pcm_add_hwdep_dev(rtd);
1222 if (ret)
1223 pr_err("%s: Could not add hw dep node\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301224 pcm->nonatomic = true;
1225exit:
1226 return ret;
1227}
1228
1229
1230static const struct snd_pcm_ops msm_pcm_ops = {
1231 .open = msm_pcm_open,
1232 .prepare = msm_pcm_prepare,
1233 .copy = msm_pcm_copy,
1234 .hw_params = msm_pcm_hw_params,
1235 .ioctl = msm_pcm_ioctl,
Asish Bhattacharya84f7f732017-07-25 16:29:27 +05301236#ifdef CONFIG_COMPAT
1237 .compat_ioctl = msm_pcm_compat_ioctl,
1238#endif
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301239 .trigger = msm_pcm_trigger,
1240 .pointer = msm_pcm_pointer,
1241 .mmap = msm_pcm_mmap,
1242 .close = msm_pcm_close,
1243};
1244
1245static struct snd_soc_platform_driver msm_soc_platform = {
1246 .ops = &msm_pcm_ops,
1247 .pcm_new = msm_asoc_pcm_new,
1248};
1249
1250static int msm_pcm_probe(struct platform_device *pdev)
1251{
1252 int rc;
1253 struct msm_plat_data *pdata;
1254 const char *latency_level;
1255 int perf_mode = LOW_LATENCY_PCM_MODE;
1256
1257 dev_dbg(&pdev->dev, "Pull mode driver probe\n");
1258
1259 if (of_property_read_bool(pdev->dev.of_node,
1260 "qcom,msm-pcm-low-latency")) {
1261
1262 rc = of_property_read_string(pdev->dev.of_node,
1263 "qcom,latency-level", &latency_level);
1264 if (!rc) {
1265 if (!strcmp(latency_level, "ultra"))
1266 perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
1267 else if (!strcmp(latency_level, "ull-pp"))
1268 perf_mode = ULL_POST_PROCESSING_PCM_MODE;
1269 }
1270 }
1271
1272 pdata = devm_kzalloc(&pdev->dev,
1273 sizeof(struct msm_plat_data), GFP_KERNEL);
1274 if (!pdata)
1275 return -ENOMEM;
1276
1277 pdata->perf_mode = perf_mode;
1278
Prasad Kumpatlaade35f02019-10-15 15:06:44 +05301279 mutex_init(&pdata->lock);
1280
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301281 dev_set_drvdata(&pdev->dev, pdata);
1282
1283 dev_dbg(&pdev->dev, "%s: dev name %s\n",
1284 __func__, dev_name(&pdev->dev));
1285 dev_dbg(&pdev->dev, "Pull mode driver register\n");
1286 rc = snd_soc_register_platform(&pdev->dev,
1287 &msm_soc_platform);
1288
1289 if (rc)
1290 dev_err(&pdev->dev, "Failed to register pull mode driver\n");
1291
1292 return rc;
1293}
1294
1295static int msm_pcm_remove(struct platform_device *pdev)
1296{
1297 struct msm_plat_data *pdata;
1298
1299 dev_dbg(&pdev->dev, "Pull mode remove\n");
1300 pdata = dev_get_drvdata(&pdev->dev);
Prasad Kumpatlaade35f02019-10-15 15:06:44 +05301301 mutex_destroy(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301302 devm_kfree(&pdev->dev, pdata);
1303 snd_soc_unregister_platform(&pdev->dev);
1304 return 0;
1305}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301306static const struct of_device_id msm_pcm_noirq_dt_match[] = {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301307 {.compatible = "qcom,msm-pcm-dsp-noirq"},
1308 {}
1309};
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301310MODULE_DEVICE_TABLE(of, msm_pcm_noirq_dt_match);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301311
1312static struct platform_driver msm_pcm_driver_noirq = {
1313 .driver = {
1314 .name = "msm-pcm-dsp-noirq",
1315 .owner = THIS_MODULE,
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301316 .of_match_table = msm_pcm_noirq_dt_match,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301317 },
1318 .probe = msm_pcm_probe,
1319 .remove = msm_pcm_remove,
1320};
1321
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301322int __init msm_pcm_noirq_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301323{
1324 return platform_driver_register(&msm_pcm_driver_noirq);
1325}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301326
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05301327void msm_pcm_noirq_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301328{
1329 platform_driver_unregister(&msm_pcm_driver_noirq);
1330}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301331
1332MODULE_DESCRIPTION("PCM NOIRQ module platform driver");
1333MODULE_LICENSE("GPL v2");