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