blob: 7338013c7e7c296f643ae0c6353ac72727d6df04 [file] [log] [blame]
Meng Wang43bbb872018-12-10 12:32:05 +08001// SPDX-License-Identifier: GPL-2.0-only
Laxminath Kasamd08ab1f2020-01-30 19:07:58 +05302/* Copyright (c) 2016-2020, 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>
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +053010#include <linux/mutex.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053011#include <linux/wait.h>
12#include <linux/platform_device.h>
13#include <linux/slab.h>
14#include <linux/of_device.h>
15#include <linux/dma-mapping.h>
Banajit Goswami08bb7362017-11-03 22:48:23 -070016#include <linux/dma-buf.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053017
18#include <sound/core.h>
19#include <sound/soc.h>
20#include <sound/soc-dapm.h>
21#include <sound/pcm.h>
22#include <sound/initval.h>
23#include <sound/control.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053024#include <sound/timer.h>
Asish Bhattacharya84f7f732017-07-25 16:29:27 +053025#include <sound/hwdep.h>
26
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053027#include <asm/dma.h>
28#include <sound/tlv.h>
29#include <sound/pcm_params.h>
Asish Bhattacharya84f7f732017-07-25 16:29:27 +053030#include <sound/devdep_params.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053031#include <dsp/msm_audio_ion.h>
32#include <dsp/q6audio-v2.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053033
34#include "msm-pcm-q6-v2.h"
35#include "msm-pcm-routing-v2.h"
36
Meng Wangee084a02018-09-04 16:11:58 +080037
38#define DRV_NAME "msm-pcm-q6-noirq"
39
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053040#define PCM_MASTER_VOL_MAX_STEPS 0x2000
41static const DECLARE_TLV_DB_LINEAR(msm_pcm_vol_gain, 0,
42 PCM_MASTER_VOL_MAX_STEPS);
43
44struct snd_msm {
45 struct snd_card *card;
46 struct snd_pcm *pcm;
47};
48
49#define CMD_EOS_MIN_TIMEOUT_LENGTH 50
50#define CMD_EOS_TIMEOUT_MULTIPLIER (HZ * 50)
51
52#define ATRACE_END() \
53 trace_printk("tracing_mark_write: E\n")
54#define ATRACE_BEGIN(name) \
55 trace_printk("tracing_mark_write: B|%d|%s\n", current->tgid, name)
56#define ATRACE_FUNC() ATRACE_BEGIN(__func__)
57#define ATRACE_INT(name, value) \
58 trace_printk("tracing_mark_write: C|%d|%s|%d\n", \
59 current->tgid, name, (int)(value))
60
61#define SIO_PLAYBACK_MAX_PERIOD_SIZE PLAYBACK_MAX_PERIOD_SIZE
62#define SIO_PLAYBACK_MIN_PERIOD_SIZE 48
63#define SIO_PLAYBACK_MAX_NUM_PERIODS 512
64#define SIO_PLAYBACK_MIN_NUM_PERIODS PLAYBACK_MIN_NUM_PERIODS
65#define SIO_PLAYBACK_MIN_BYTES (SIO_PLAYBACK_MIN_NUM_PERIODS * \
66 SIO_PLAYBACK_MIN_PERIOD_SIZE)
67
68#define SIO_PLAYBACK_MAX_BYTES ((SIO_PLAYBACK_MAX_NUM_PERIODS) * \
69 (SIO_PLAYBACK_MAX_PERIOD_SIZE))
70
71#define SIO_CAPTURE_MAX_PERIOD_SIZE CAPTURE_MAX_PERIOD_SIZE
72#define SIO_CAPTURE_MIN_PERIOD_SIZE 48
73#define SIO_CAPTURE_MAX_NUM_PERIODS 512
74#define SIO_CAPTURE_MIN_NUM_PERIODS CAPTURE_MIN_NUM_PERIODS
75
76#define SIO_CAPTURE_MIN_BYTES (SIO_CAPTURE_MIN_NUM_PERIODS * \
77 SIO_CAPTURE_MIN_PERIOD_SIZE)
78
79#define SIO_CAPTURE_MAX_BYTES (SIO_CAPTURE_MAX_NUM_PERIODS * \
80 SIO_CAPTURE_MAX_PERIOD_SIZE)
81
82static struct snd_pcm_hardware msm_pcm_hardware_playback = {
83 .info = (SNDRV_PCM_INFO_MMAP |
84 SNDRV_PCM_INFO_BLOCK_TRANSFER |
85 SNDRV_PCM_INFO_MMAP_VALID |
86 SNDRV_PCM_INFO_INTERLEAVED |
87 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP |
88 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
89 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
90 SNDRV_PCM_FMTBIT_S24_LE |
91 SNDRV_PCM_FMTBIT_S24_3LE),
92 .rates = SNDRV_PCM_RATE_8000_192000,
93 .rate_min = 8000,
94 .rate_max = 192000,
95 .channels_min = 1,
96 .channels_max = 8,
97 .buffer_bytes_max = SIO_PLAYBACK_MAX_NUM_PERIODS *
98 SIO_PLAYBACK_MAX_PERIOD_SIZE,
99 .period_bytes_min = SIO_PLAYBACK_MIN_PERIOD_SIZE,
100 .period_bytes_max = SIO_PLAYBACK_MAX_PERIOD_SIZE,
101 .periods_min = SIO_PLAYBACK_MIN_NUM_PERIODS,
102 .periods_max = SIO_PLAYBACK_MAX_NUM_PERIODS,
103 .fifo_size = 0,
104};
105
106static struct snd_pcm_hardware msm_pcm_hardware_capture = {
107 .info = (SNDRV_PCM_INFO_MMAP |
108 SNDRV_PCM_INFO_BLOCK_TRANSFER |
109 SNDRV_PCM_INFO_MMAP_VALID |
110 SNDRV_PCM_INFO_INTERLEAVED |
111 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP |
112 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
113 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
114 SNDRV_PCM_FMTBIT_S24_LE |
115 SNDRV_PCM_FMTBIT_S24_3LE),
116 .rates = SNDRV_PCM_RATE_8000_48000,
117 .rate_min = 8000,
118 .rate_max = 48000,
119 .channels_min = 1,
120 .channels_max = 4,
121 .buffer_bytes_max = SIO_CAPTURE_MAX_NUM_PERIODS *
122 SIO_CAPTURE_MAX_PERIOD_SIZE,
123 .period_bytes_min = SIO_CAPTURE_MIN_PERIOD_SIZE,
124 .period_bytes_max = SIO_CAPTURE_MAX_PERIOD_SIZE,
125 .periods_min = SIO_CAPTURE_MIN_NUM_PERIODS,
126 .periods_max = SIO_CAPTURE_MAX_NUM_PERIODS,
127 .fifo_size = 0,
128};
129
130/* Conventional and unconventional sample rate supported */
131static unsigned int supported_sample_rates[] = {
132 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
133 88200, 96000, 176400, 192000
134};
135
136static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
137 .count = ARRAY_SIZE(supported_sample_rates),
138 .list = supported_sample_rates,
139 .mask = 0,
140};
141
142static unsigned long msm_pcm_fe_topology[MSM_FRONTEND_DAI_MAX];
143
144/* default value is DTS (i.e read from device tree) */
145static char const *msm_pcm_fe_topology_text[] = {
146 "DTS", "ULL", "ULL_PP", "LL" };
147
148static const struct soc_enum msm_pcm_fe_topology_enum[] = {
149 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(msm_pcm_fe_topology_text),
150 msm_pcm_fe_topology_text),
151};
152
153static void event_handler(uint32_t opcode,
154 uint32_t token, uint32_t *payload, void *priv)
155{
156 uint32_t *ptrmem = (uint32_t *)payload;
157
158 switch (opcode) {
159 case ASM_DATA_EVENT_WATERMARK:
160 pr_debug("%s: Watermark level = 0x%08x\n", __func__, *ptrmem);
161 break;
162 case APR_BASIC_RSP_RESULT:
163 pr_debug("%s: Payload = [0x%x]stat[0x%x]\n",
164 __func__, payload[0], payload[1]);
165 switch (payload[0]) {
166 case ASM_SESSION_CMD_RUN_V2:
167 case ASM_SESSION_CMD_PAUSE:
168 case ASM_STREAM_CMD_FLUSH:
169 break;
170 default:
171 break;
172 }
173 break;
174 default:
175 pr_debug("Not Supported Event opcode[0x%x]\n", opcode);
176 break;
177 }
178}
179
180static int msm_pcm_open(struct snd_pcm_substream *substream)
181{
182 struct snd_pcm_runtime *runtime = substream->runtime;
183 struct msm_audio *prtd;
184 int ret = 0;
Ramprasad Katkam17f05162019-04-15 18:37:10 +0530185 enum apr_subsys_state subsys_state;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530186
Ramprasad Katkam17f05162019-04-15 18:37:10 +0530187 subsys_state = apr_get_subsys_state();
188 if (subsys_state == APR_SUBSYS_DOWN) {
189 pr_debug("%s: adsp is down\n", __func__);
190 return -ENETRESET;
191 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530192 prtd = kzalloc(sizeof(struct msm_audio), GFP_KERNEL);
193
194 if (prtd == NULL)
195 return -ENOMEM;
196
197 prtd->substream = substream;
198 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
199 runtime->hw = msm_pcm_hardware_playback;
200 else
201 runtime->hw = msm_pcm_hardware_capture;
202
203 ret = snd_pcm_hw_constraint_list(runtime, 0,
204 SNDRV_PCM_HW_PARAM_RATE,
205 &constraints_sample_rates);
206 if (ret)
207 pr_info("snd_pcm_hw_constraint_list failed\n");
208
209 ret = snd_pcm_hw_constraint_integer(runtime,
210 SNDRV_PCM_HW_PARAM_PERIODS);
211 if (ret)
212 pr_info("snd_pcm_hw_constraint_integer failed\n");
213
214 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
215 ret = snd_pcm_hw_constraint_minmax(runtime,
216 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
217 SIO_PLAYBACK_MIN_BYTES,
218 SIO_PLAYBACK_MAX_BYTES);
219 if (ret) {
220 pr_info("%s: P buffer bytes minmax constraint ret %d\n",
221 __func__, ret);
222 }
223 } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
224 ret = snd_pcm_hw_constraint_minmax(runtime,
225 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
226 SIO_CAPTURE_MIN_BYTES,
227 SIO_CAPTURE_MAX_BYTES);
228 if (ret) {
229 pr_info("%s: C buffer bytes minmax constraint ret %d\n",
230 __func__, ret);
231 }
232 }
233
234 ret = snd_pcm_hw_constraint_step(runtime, 0,
235 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
236 if (ret) {
237 pr_err("%s: Constraint for period bytes step ret = %d\n",
238 __func__, ret);
239 }
240 ret = snd_pcm_hw_constraint_step(runtime, 0,
241 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
242 if (ret) {
243 pr_err("%s: Constraint for buffer bytes step ret = %d\n",
244 __func__, ret);
245 }
246 prtd->audio_client = q6asm_audio_client_alloc(
247 (app_cb)event_handler, prtd);
248 if (!prtd->audio_client) {
249 pr_err("%s: client alloc failed\n", __func__);
250 ret = -ENOMEM;
251 goto fail_cmd;
252 }
253 prtd->dsp_cnt = 0;
254 prtd->set_channel_map = false;
255 runtime->private_data = prtd;
256 return 0;
257
258fail_cmd:
259 kfree(prtd);
260 return ret;
261}
262
263static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
264 struct snd_pcm_hw_params *params)
265
266{
267 struct snd_pcm_runtime *runtime = substream->runtime;
268 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
Meng Wangee084a02018-09-04 16:11:58 +0800269 struct snd_soc_component *component =
270 snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530271 struct msm_audio *prtd = runtime->private_data;
272 struct msm_plat_data *pdata;
273 struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
274 struct audio_buffer *buf;
275 struct shared_io_config config;
276 uint16_t sample_word_size;
277 uint16_t bits_per_sample;
278 int ret;
279 int dir = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? IN : OUT;
280 unsigned long topology;
281 int perf_mode;
Surendar Karka5628e8b2018-07-23 10:41:58 +0530282 bool use_default_chmap = true;
283 char *chmap = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530284
Meng Wangee084a02018-09-04 16:11:58 +0800285 if (!component) {
286 pr_err("%s: component is NULL\n", __func__);
287 return -EINVAL;
288 }
289
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530290 pdata = (struct msm_plat_data *)
Meng Wangee084a02018-09-04 16:11:58 +0800291 dev_get_drvdata(component->dev);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530292 if (!pdata) {
293 ret = -EINVAL;
294 pr_err("%s: platform data not populated ret: %d\n", __func__,
295 ret);
296 return ret;
297 }
298
299 topology = msm_pcm_fe_topology[soc_prtd->dai_link->id];
300
301 if (!strcmp(msm_pcm_fe_topology_text[topology], "ULL_PP"))
302 perf_mode = ULL_POST_PROCESSING_PCM_MODE;
303 else if (!strcmp(msm_pcm_fe_topology_text[topology], "ULL"))
304 perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
305 else if (!strcmp(msm_pcm_fe_topology_text[topology], "LL"))
306 perf_mode = LOW_LATENCY_PCM_MODE;
307 else
308 /* use the default from the device tree */
309 perf_mode = pdata->perf_mode;
310
311
312 /* need to set LOW_LATENCY_PCM_MODE for capture since
313 * push mode does not support ULL
314 */
315 prtd->audio_client->perf_mode = (dir == IN) ?
316 perf_mode :
317 LOW_LATENCY_PCM_MODE;
318
319 /* rate and channels are sent to audio driver */
320 prtd->samp_rate = params_rate(params);
321 prtd->channel_mode = params_channels(params);
322 if (prtd->enabled)
323 return 0;
324
Surendar Karka5628e8b2018-07-23 10:41:58 +0530325 if (pdata->ch_map[soc_prtd->dai_link->id]) {
326 use_default_chmap =
327 !(pdata->ch_map[soc_prtd->dai_link->id]->set_ch_map);
328 chmap =
329 pdata->ch_map[soc_prtd->dai_link->id]->channel_map;
330 }
331
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530332 switch (runtime->format) {
333 case SNDRV_PCM_FORMAT_S24_LE:
334 bits_per_sample = 24;
335 sample_word_size = 32;
336 break;
337 case SNDRV_PCM_FORMAT_S24_3LE:
338 bits_per_sample = 24;
339 sample_word_size = 24;
340 break;
341 case SNDRV_PCM_FORMAT_S16_LE:
342 default:
343 bits_per_sample = 16;
344 sample_word_size = 16;
345 break;
346 }
347
348 config.format = FORMAT_LINEAR_PCM;
349 config.bits_per_sample = bits_per_sample;
350 config.rate = params_rate(params);
351 config.channels = params_channels(params);
352 config.sample_word_size = sample_word_size;
353 config.bufsz = params_buffer_bytes(params) / params_periods(params);
354 config.bufcnt = params_periods(params);
355
Surendar Karka5628e8b2018-07-23 10:41:58 +0530356 ret = q6asm_open_shared_io(prtd->audio_client, &config, dir,
357 use_default_chmap, chmap);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530358 if (ret) {
359 pr_err("%s: q6asm_open_write_shared_io failed ret: %d\n",
360 __func__, ret);
361 return ret;
362 }
363
364 prtd->pcm_size = params_buffer_bytes(params);
365 prtd->pcm_count = params_buffer_bytes(params);
366 prtd->pcm_irq_pos = 0;
367
368 buf = prtd->audio_client->port[dir].buf;
369 dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
370 dma_buf->dev.dev = substream->pcm->card->dev;
371 dma_buf->private_data = NULL;
372 dma_buf->area = buf->data;
373 dma_buf->addr = buf->phys;
374 dma_buf->bytes = prtd->pcm_size;
375
376 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
377
378 pr_debug("%s: session ID %d, perf %d\n", __func__,
379 prtd->audio_client->session,
380 prtd->audio_client->perf_mode);
381 prtd->session_id = prtd->audio_client->session;
382
383 pr_debug("msm_pcm_routing_reg_phy_stream w/ id %d\n",
384 soc_prtd->dai_link->id);
385 ret = msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->id,
386 prtd->audio_client->perf_mode,
387 prtd->session_id, substream->stream);
388
389 if (ret) {
390 pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
391 return ret;
392 }
393
394 atomic_set(&prtd->out_count, runtime->periods);
395 prtd->enabled = 1;
396 prtd->cmd_pending = 0;
397 prtd->cmd_interrupt = 0;
398
399 return 0;
400}
401
402static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
403{
404 int ret = 0;
405 struct snd_pcm_runtime *runtime = substream->runtime;
406 struct msm_audio *prtd = runtime->private_data;
407 int dir = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 0 : 1;
408 struct audio_buffer *buf;
409
410 switch (cmd) {
411 case SNDRV_PCM_TRIGGER_START:
412 case SNDRV_PCM_TRIGGER_RESUME:
413 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
414 pr_debug("%s: %s Trigger start\n", __func__,
415 dir == 0 ? "P" : "C");
416 ret = q6asm_run(prtd->audio_client, 0, 0, 0);
417 if (ret)
418 break;
419 atomic_set(&prtd->start, 1);
420 break;
421 case SNDRV_PCM_TRIGGER_STOP:
422 pr_debug("%s: SNDRV_PCM_TRIGGER_STOP\n", __func__);
423 atomic_set(&prtd->start, 0);
424 q6asm_cmd(prtd->audio_client, CMD_PAUSE);
425 q6asm_cmd(prtd->audio_client, CMD_FLUSH);
426 buf = q6asm_shared_io_buf(prtd->audio_client, dir);
427 if (buf == NULL) {
428 pr_err("%s: shared IO buffer is null\n", __func__);
429 ret = -EINVAL;
430 break;
431 }
432 memset(buf->data, 0, buf->actual_size);
433 break;
434 case SNDRV_PCM_TRIGGER_SUSPEND:
435 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
436 pr_debug("%s: SNDRV_PCM_TRIGGER_PAUSE\n", __func__);
437 ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
438 atomic_set(&prtd->start, 0);
439 break;
440 default:
441 ret = -EINVAL;
442 break;
443 }
444 return ret;
445}
446
Asish Bhattacharya84f7f732017-07-25 16:29:27 +0530447
448static int msm_pcm_mmap_fd(struct snd_pcm_substream *substream,
449 struct snd_pcm_mmap_fd *mmap_fd)
450{
451 struct msm_audio *prtd;
452 struct audio_port_data *apd;
453 struct audio_buffer *ab;
454 int dir = -1;
Aditya Bavanari36a5eb42019-03-27 19:15:03 +0530455 struct dma_buf *buf = NULL;
456 int rc = 0;
Asish Bhattacharya84f7f732017-07-25 16:29:27 +0530457
458 if (!substream->runtime) {
459 pr_err("%s substream runtime not found\n", __func__);
460 return -EFAULT;
461 }
462
463 prtd = substream->runtime->private_data;
464 if (!prtd || !prtd->audio_client || !prtd->mmap_flag) {
465 pr_err("%s no audio client or not an mmap session\n", __func__);
466 return -EINVAL;
467 }
468
469 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
470 dir = IN;
471 else
472 dir = OUT;
473
474 apd = prtd->audio_client->port;
475 ab = &(apd[dir].buf[0]);
Banajit Goswami08bb7362017-11-03 22:48:23 -0700476 /*
477 * Passing O_CLOEXEC as flag passed to fd, to be in sync with
478 * previous implimentation.
479 * This was the flag used by previous internal wrapper API, which
480 * used to call dma_buf_fd internally.
481 */
482 mmap_fd->fd = dma_buf_fd(ab->dma_buf, O_CLOEXEC);
Aditya Bavanari36a5eb42019-03-27 19:15:03 +0530483 if (mmap_fd->fd < 0) {
484 pr_err("%s: dma_buf_fd failed, fd:%d\n",
485 __func__, mmap_fd->fd);
486 rc = -EFAULT;
487 goto buf_fd_fail;
Asish Bhattacharya84f7f732017-07-25 16:29:27 +0530488 }
Aditya Bavanari36a5eb42019-03-27 19:15:03 +0530489 mmap_fd->dir = dir;
490 mmap_fd->actual_size = ab->actual_size;
491 mmap_fd->size = ab->size;
492
493 buf = dma_buf_get(mmap_fd->fd);
494 if (IS_ERR_OR_NULL(buf)) {
495 pr_err("%s: dma_buf_get failed, fd:%d\n",
496 __func__, mmap_fd->fd);
497 rc = -EINVAL;
498 }
499
500buf_fd_fail:
501 return rc;
Asish Bhattacharya84f7f732017-07-25 16:29:27 +0530502}
503
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530504static int msm_pcm_ioctl(struct snd_pcm_substream *substream,
505 unsigned int cmd, void *arg)
506{
507 struct snd_pcm_runtime *runtime = substream->runtime;
508 struct msm_audio *prtd = runtime->private_data;
509 int dir = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 0 : 1;
510 struct audio_buffer *buf;
511
512 switch (cmd) {
513 case SNDRV_PCM_IOCTL1_RESET:
514 pr_debug("%s: %s SNDRV_PCM_IOCTL1_RESET\n", __func__,
515 dir == 0 ? "P" : "C");
516 buf = q6asm_shared_io_buf(prtd->audio_client, dir);
517
518 if (buf && buf->data)
519 memset(buf->data, 0, buf->actual_size);
520 break;
521 default:
522 break;
523 }
524
525 return snd_pcm_lib_ioctl(substream, cmd, arg);
526}
527
Asish Bhattacharya84f7f732017-07-25 16:29:27 +0530528#ifdef CONFIG_COMPAT
529static int msm_pcm_compat_ioctl(struct snd_pcm_substream *substream,
530 unsigned int cmd, void *arg)
531{
532 /* we only handle RESET which is common for both modes */
533 return msm_pcm_ioctl(substream, cmd, arg);
534}
535#endif
536
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530537static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
538{
539 struct snd_pcm_runtime *runtime = substream->runtime;
540 uint32_t read_index, wall_clk_msw, wall_clk_lsw;
541 /*these are offsets, unlike ASoC's full values*/
542 snd_pcm_sframes_t hw_ptr;
543 snd_pcm_sframes_t period_size;
544 int ret;
545 int retries = 10;
546 struct msm_audio *prtd = runtime->private_data;
547
548 period_size = runtime->period_size;
549
550 do {
551 ret = q6asm_get_shared_pos(prtd->audio_client,
552 &read_index, &wall_clk_msw,
553 &wall_clk_lsw);
554 } while (ret == -EAGAIN && --retries);
555
556 if (ret || !period_size) {
557 pr_err("get_shared_pos error or zero period size\n");
558 return 0;
559 }
560
561 hw_ptr = bytes_to_frames(substream->runtime,
562 read_index);
563
564 if (runtime->control->appl_ptr == 0) {
565 pr_debug("ptr(%s): appl(0), hw = %lu read_index = %u\n",
566 prtd->substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
567 "P" : "C",
568 hw_ptr, read_index);
569 }
570 return (hw_ptr/period_size) * period_size;
571}
572
573static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
Meng Wangac147b72017-10-30 16:46:16 +0800574 unsigned long hwoff, void __user *buf, unsigned long fbytes)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530575{
576 return -EINVAL;
577}
578
579static int msm_pcm_mmap(struct snd_pcm_substream *substream,
580 struct vm_area_struct *vma)
581{
582 struct snd_pcm_runtime *runtime = substream->runtime;
583 struct msm_audio *prtd = runtime->private_data;
584 struct audio_client *ac = prtd->audio_client;
585 struct audio_port_data *apd = ac->port;
586 struct audio_buffer *ab;
587 int dir = -1;
588 int ret;
589
590 pr_debug("%s: mmap begin\n", __func__);
591 prtd->mmap_flag = 1;
592
593 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
594 dir = IN;
595 else
596 dir = OUT;
597
598 ab = &(apd[dir].buf[0]);
599
600 ret = msm_audio_ion_mmap(ab, vma);
601
602 if (ret)
603 prtd->mmap_flag = 0;
604
605 return ret;
606}
607
608static int msm_pcm_prepare(struct snd_pcm_substream *substream)
609{
Arun Mirpurieafed6a2019-07-11 16:33:19 -0700610 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530611 struct snd_pcm_runtime *runtime = substream->runtime;
612 struct msm_audio *prtd = runtime->private_data;
Arun Mirpurieafed6a2019-07-11 16:33:19 -0700613 struct asm_softvolume_params softvol = {
614 .period = SOFT_VOLUME_PERIOD,
615 .step = SOFT_VOLUME_STEP,
616 .rampingcurve = SOFT_VOLUME_CURVE_LINEAR,
617 };
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530618
619 if (!prtd || !prtd->mmap_flag)
620 return -EIO;
621
Arun Mirpurieafed6a2019-07-11 16:33:19 -0700622 if (prtd->audio_client) {
623 rc = q6asm_set_softvolume_v2(prtd->audio_client,
624 &softvol, SOFT_VOLUME_INSTANCE_1);
625 if (rc < 0)
626 pr_err("%s: Send SoftVolume command failed rc=%d\n",
627 __func__, rc);
628 }
629 return rc;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530630}
631
632static int msm_pcm_close(struct snd_pcm_substream *substream)
633{
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530634 struct msm_plat_data *pdata = NULL;
635 struct snd_soc_component *component = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530636 struct snd_pcm_runtime *runtime = substream->runtime;
637 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
638 struct msm_audio *prtd = runtime->private_data;
639 struct audio_client *ac = prtd->audio_client;
640 uint32_t timeout;
641 int dir = 0;
642 int ret = 0;
643
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530644 if (!soc_prtd) {
645 pr_debug("%s private_data not found\n",
646 __func__);
647 return 0;
648 }
649
650 component = snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
651 if (!component) {
652 pr_err("%s: component is NULL\n", __func__);
653 return -EINVAL;
654 }
655
656 pdata = (struct msm_plat_data *) dev_get_drvdata(component->dev);
657 if (!pdata) {
658 pr_err("%s: pdata not found\n", __func__);
659 return -ENODEV;
660 }
661
662 mutex_lock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530663 if (ac) {
664 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
665 dir = IN;
666 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
667 dir = OUT;
668
669 /* determine timeout length */
670 if (runtime->frame_bits == 0 || runtime->rate == 0) {
671 timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
672 } else {
673 timeout = (runtime->period_size *
674 CMD_EOS_TIMEOUT_MULTIPLIER) /
675 ((runtime->frame_bits / 8) *
676 runtime->rate);
677 if (timeout < CMD_EOS_MIN_TIMEOUT_LENGTH)
678 timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
679 }
680
681 q6asm_cmd(ac, CMD_CLOSE);
682
683 ret = q6asm_shared_io_free(ac, dir);
684
685 if (ret) {
686 pr_err("%s: Failed to close pull mode, ret %d\n",
687 __func__, ret);
688 }
689 q6asm_audio_client_free(ac);
690 }
691 msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
692 dir == IN ?
693 SNDRV_PCM_STREAM_PLAYBACK :
694 SNDRV_PCM_STREAM_CAPTURE);
695 kfree(prtd);
696 runtime->private_data = NULL;
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530697 mutex_unlock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530698
699 return 0;
700}
701
702static int msm_pcm_set_volume(struct msm_audio *prtd, uint32_t volume)
703{
704 int rc = 0;
705
706 if (prtd && prtd->audio_client) {
707 pr_debug("%s: channels %d volume 0x%x\n", __func__,
708 prtd->channel_mode, volume);
709 rc = q6asm_set_volume(prtd->audio_client, volume);
710 if (rc < 0) {
711 pr_err("%s: Send Volume command failed rc=%d\n",
712 __func__, rc);
713 }
714 }
715 return rc;
716}
717
718static int msm_pcm_volume_ctl_get(struct snd_kcontrol *kcontrol,
719 struct snd_ctl_elem_value *ucontrol)
720{
721 struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530722 struct msm_plat_data *pdata = NULL;
Laxminath Kasamd08ab1f2020-01-30 19:07:58 +0530723 struct snd_pcm_substream *substream = NULL;
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530724 struct snd_soc_pcm_runtime *soc_prtd = NULL;
725 struct snd_soc_component *component = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530726 struct msm_audio *prtd;
727
728 pr_debug("%s\n", __func__);
Laxminath Kasamd08ab1f2020-01-30 19:07:58 +0530729 if (!vol) {
730 pr_err("%s: vol is NULL\n", __func__);
731 return -ENODEV;
732 }
xsang6da283a2020-06-19 19:46:57 +0800733
734 if (!vol->pcm) {
735 pr_err("%s: vol->pcm is NULL\n", __func__);
736 return -ENODEV;
737 }
738
Laxminath Kasamd08ab1f2020-01-30 19:07:58 +0530739 substream = vol->pcm->streams[vol->stream].substream;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530740 if (!substream) {
741 pr_err("%s substream not found\n", __func__);
742 return -ENODEV;
743 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530744 soc_prtd = substream->private_data;
745 if (!substream->runtime || !soc_prtd) {
746 pr_debug("%s substream runtime or private_data not found\n",
747 __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530748 return 0;
749 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530750
751 component = snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
752 if (!component) {
753 pr_err("%s: component is NULL\n", __func__);
754 return -EINVAL;
755 }
756
757 pdata = (struct msm_plat_data *) dev_get_drvdata(component->dev);
758 if (!pdata) {
759 pr_err("%s: pdata not found\n", __func__);
760 return -ENODEV;
761 }
762 mutex_lock(&pdata->lock);
Prasad Kumpatla348bc672020-06-25 19:38:34 +0530763 if (substream->ref_count > 0) {
764 prtd = substream->runtime->private_data;
765 if (prtd)
766 ucontrol->value.integer.value[0] = prtd->volume;
767 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530768 mutex_unlock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530769 return 0;
770}
771
772static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
773 struct snd_ctl_elem_value *ucontrol)
774{
775 int rc = 0;
776 struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530777 struct msm_plat_data *pdata = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530778 struct snd_pcm_substream *substream =
Haynes Mathew George86eb0ce2018-04-06 18:18:51 -0700779 vol->pcm->streams[vol->stream].substream;
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530780 struct snd_soc_pcm_runtime *soc_prtd = NULL;
781 struct snd_soc_component *component = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530782 struct msm_audio *prtd;
783 int volume = ucontrol->value.integer.value[0];
784
785 pr_debug("%s: volume : 0x%x\n", __func__, volume);
786 if (!substream) {
787 pr_err("%s substream not found\n", __func__);
788 return -ENODEV;
789 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530790 soc_prtd = substream->private_data;
791 if (!substream->runtime || !soc_prtd) {
792 pr_err("%s substream runtime or private_data not found\n",
793 __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530794 return 0;
795 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530796
797 component = snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
798 if (!component) {
799 pr_err("%s: component is NULL\n", __func__);
800 return -EINVAL;
801 }
802
803 pdata = (struct msm_plat_data *) dev_get_drvdata(component->dev);
804 if (!pdata) {
805 pr_err("%s: pdata not found\n", __func__);
806 return -ENODEV;
807 }
808
809 mutex_lock(&pdata->lock);
Prasad Kumpatla348bc672020-06-25 19:38:34 +0530810 if (substream->ref_count > 0) {
811 prtd = substream->runtime->private_data;
812 if (prtd) {
813 rc = msm_pcm_set_volume(prtd, volume);
814 prtd->volume = volume;
815 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530816 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530817 mutex_unlock(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530818 return rc;
819}
820
Haynes Mathew George86eb0ce2018-04-06 18:18:51 -0700821static int msm_pcm_add_volume_control(struct snd_soc_pcm_runtime *rtd,
822 int stream)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530823{
824 int ret = 0;
825 struct snd_pcm *pcm = rtd->pcm;
826 struct snd_pcm_volume *volume_info;
827 struct snd_kcontrol *kctl;
828
Haynes Mathew George86eb0ce2018-04-06 18:18:51 -0700829 dev_dbg(rtd->dev, "%s, volume control add\n", __func__);
830 ret = snd_pcm_add_volume_ctls(pcm, stream,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530831 NULL, 1, rtd->dai_link->id,
832 &volume_info);
833 if (ret < 0) {
834 pr_err("%s volume control failed ret %d\n", __func__, ret);
835 return ret;
836 }
837 kctl = volume_info->kctl;
838 kctl->put = msm_pcm_volume_ctl_put;
839 kctl->get = msm_pcm_volume_ctl_get;
840 kctl->tlv.p = msm_pcm_vol_gain;
841 return 0;
842}
843
Surendar Karka5628e8b2018-07-23 10:41:58 +0530844static int msm_pcm_channel_map_put(struct snd_kcontrol *kcontrol,
845 struct snd_ctl_elem_value *ucontrol)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530846{
Surendar Karka5628e8b2018-07-23 10:41:58 +0530847 struct snd_soc_component *pcm = snd_kcontrol_chip(kcontrol);
848 u64 fe_id = kcontrol->private_value;
849 struct msm_plat_data *pdata = (struct msm_plat_data *)
850 snd_soc_component_get_drvdata(pcm);
851 int rc = 0, i = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530852
Surendar Karka5628e8b2018-07-23 10:41:58 +0530853 pr_debug("%s: fe_id- %llu\n", __func__, fe_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530854
Surendar Karka5628e8b2018-07-23 10:41:58 +0530855 if (fe_id >= MSM_FRONTEND_DAI_MAX) {
856 pr_err("%s Received out of bounds fe_id %llu\n",
857 __func__, fe_id);
858 rc = -EINVAL;
859 goto end;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530860 }
Surendar Karka5628e8b2018-07-23 10:41:58 +0530861
862 if (pdata->ch_map[fe_id]) {
863 pdata->ch_map[fe_id]->set_ch_map = true;
864 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
865 pdata->ch_map[fe_id]->channel_map[i] =
866 (char)(ucontrol->value.integer.value[i]);
867 } else {
868 pr_debug("%s: no memory for ch_map, default will be set\n",
869 __func__);
870 }
871end:
872 pr_debug("%s: ret %d\n", __func__, rc);
873 return rc;
874}
875
876static int msm_pcm_channel_map_info(struct snd_kcontrol *kcontrol,
877 struct snd_ctl_elem_info *uinfo)
878{
879 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
880 uinfo->count = 8;
881 uinfo->value.integer.min = 0;
882 uinfo->value.integer.max = 0xFFFFFFFF;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530883 return 0;
884}
885
Surendar Karka5628e8b2018-07-23 10:41:58 +0530886static int msm_pcm_channel_map_get(struct snd_kcontrol *kcontrol,
887 struct snd_ctl_elem_value *ucontrol)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530888{
Surendar Karka5628e8b2018-07-23 10:41:58 +0530889 struct snd_soc_component *pcm = snd_kcontrol_chip(kcontrol);
890 u64 fe_id = kcontrol->private_value;
891 struct msm_plat_data *pdata = (struct msm_plat_data *)
892 snd_soc_component_get_drvdata(pcm);
893 int rc = 0, i = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530894
Surendar Karka5628e8b2018-07-23 10:41:58 +0530895 pr_debug("%s: fe_id- %llu\n", __func__, fe_id);
896 if (fe_id >= MSM_FRONTEND_DAI_MAX) {
897 pr_err("%s: Received out of bounds fe_id %llu\n",
898 __func__, fe_id);
899 rc = -EINVAL;
900 goto end;
901 }
902 if (pdata->ch_map[fe_id]) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530903 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
904 ucontrol->value.integer.value[i] =
Surendar Karka5628e8b2018-07-23 10:41:58 +0530905 pdata->ch_map[fe_id]->channel_map[i];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530906 }
Surendar Karka5628e8b2018-07-23 10:41:58 +0530907end:
908 pr_debug("%s: ret %d\n", __func__, rc);
909 return rc;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530910}
911
Surendar Karka5628e8b2018-07-23 10:41:58 +0530912static int msm_pcm_add_channel_map_control(struct snd_soc_pcm_runtime *rtd)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530913{
Meng Wangee084a02018-09-04 16:11:58 +0800914 struct snd_soc_component *component = NULL;
Surendar Karka5628e8b2018-07-23 10:41:58 +0530915 const char *mixer_ctl_name = "Playback Channel Map";
916 const char *deviceNo = "NN";
917 char *mixer_str = NULL;
918 struct msm_plat_data *pdata = NULL;
919 int ctl_len = 0;
920 struct snd_kcontrol_new fe_channel_map_control[1] = {
921 {
922 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
923 .name = "?",
924 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
925 .info = msm_pcm_channel_map_info,
926 .get = msm_pcm_channel_map_get,
927 .put = msm_pcm_channel_map_put,
928 .private_value = 0,
929 }
930 };
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530931
Surendar Karka5628e8b2018-07-23 10:41:58 +0530932 if (!rtd) {
933 pr_err("%s: NULL rtd\n", __func__);
934 return -EINVAL;
935 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530936
Meng Wangee084a02018-09-04 16:11:58 +0800937 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
938 if (!component) {
939 pr_err("%s: component is NULL\n", __func__);
940 return -EINVAL;
941 }
942
Surendar Karka5628e8b2018-07-23 10:41:58 +0530943 pr_debug("%s: added new pcm FE with name %s, id %d, cpu dai %s, device no %d\n",
944 __func__, rtd->dai_link->name, rtd->dai_link->id,
945 rtd->dai_link->cpu_dai_name, rtd->pcm->device);
946
947 ctl_len = strlen(mixer_ctl_name) + strlen(deviceNo) + 1;
948 mixer_str = kzalloc(ctl_len, GFP_KERNEL);
949 if (!mixer_str)
950 return -ENOMEM;
951
952 snprintf(mixer_str, ctl_len, "%s%d", mixer_ctl_name, rtd->pcm->device);
953
954 fe_channel_map_control[0].name = mixer_str;
955 fe_channel_map_control[0].private_value = rtd->dai_link->id;
956 pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
Meng Wangee084a02018-09-04 16:11:58 +0800957 snd_soc_add_component_controls(component,
Surendar Karka5628e8b2018-07-23 10:41:58 +0530958 fe_channel_map_control,
959 ARRAY_SIZE(fe_channel_map_control));
960
Meng Wangee084a02018-09-04 16:11:58 +0800961 pdata = snd_soc_component_get_drvdata(component);
Surendar Karka5628e8b2018-07-23 10:41:58 +0530962 pdata->ch_map[rtd->dai_link->id] =
963 kzalloc(sizeof(struct msm_pcm_ch_map), GFP_KERNEL);
964 if (!pdata->ch_map[rtd->dai_link->id]) {
965 pr_err("%s: Could not allocate memory for channel map\n",
966 __func__);
967 kfree(mixer_str);
968 return -ENOMEM;
969 }
970 kfree(mixer_str);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530971 return 0;
972}
973
974static int msm_pcm_fe_topology_info(struct snd_kcontrol *kcontrol,
975 struct snd_ctl_elem_info *uinfo)
976{
977 const struct soc_enum *e = &msm_pcm_fe_topology_enum[0];
978
979 return snd_ctl_enum_info(uinfo, 1, e->items, e->texts);
980}
981
982static int msm_pcm_fe_topology_get(struct snd_kcontrol *kcontrol,
983 struct snd_ctl_elem_value *ucontrol)
984{
985 unsigned long fe_id = kcontrol->private_value;
986
987 if (fe_id >= MSM_FRONTEND_DAI_MAX) {
988 pr_err("%s Received out of bound fe_id %lu\n", __func__, fe_id);
989 return -EINVAL;
990 }
991
992 pr_debug("%s: %lu topology %s\n", __func__, fe_id,
993 msm_pcm_fe_topology_text[msm_pcm_fe_topology[fe_id]]);
994 ucontrol->value.enumerated.item[0] = msm_pcm_fe_topology[fe_id];
995 return 0;
996}
997
998static int msm_pcm_fe_topology_put(struct snd_kcontrol *kcontrol,
999 struct snd_ctl_elem_value *ucontrol)
1000{
1001 unsigned long fe_id = kcontrol->private_value;
1002 unsigned int item;
1003
1004 if (fe_id >= MSM_FRONTEND_DAI_MAX) {
1005 pr_err("%s Received out of bound fe_id %lu\n", __func__, fe_id);
1006 return -EINVAL;
1007 }
1008
1009 item = ucontrol->value.enumerated.item[0];
1010 if (item >= ARRAY_SIZE(msm_pcm_fe_topology_text)) {
1011 pr_err("%s Received out of bound topology %lu\n", __func__,
1012 fe_id);
1013 return -EINVAL;
1014 }
1015
1016 pr_debug("%s: %lu new topology %s\n", __func__, fe_id,
1017 msm_pcm_fe_topology_text[item]);
1018 msm_pcm_fe_topology[fe_id] = item;
1019 return 0;
1020}
1021
1022static int msm_pcm_add_fe_topology_control(struct snd_soc_pcm_runtime *rtd)
1023{
Meng Wangee084a02018-09-04 16:11:58 +08001024 struct snd_soc_component *component = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301025 const char *mixer_ctl_name = "PCM_Dev";
1026 const char *deviceNo = "NN";
1027 const char *topo_text = "Topology";
1028 char *mixer_str = NULL;
1029 int ctl_len;
1030 int ret;
1031 struct snd_kcontrol_new topology_control[1] = {
1032 {
1033 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1034 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1035 .name = "?",
1036 .info = msm_pcm_fe_topology_info,
1037 .get = msm_pcm_fe_topology_get,
1038 .put = msm_pcm_fe_topology_put,
1039 .private_value = 0,
1040 },
1041 };
1042
Meng Wangee084a02018-09-04 16:11:58 +08001043 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
1044 if (!component) {
1045 pr_err("%s: component is NULL\n", __func__);
1046 return -EINVAL;
1047 }
1048
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301049 ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1 +
1050 strlen(topo_text) + 1;
1051 mixer_str = kzalloc(ctl_len, GFP_KERNEL);
1052
1053 if (!mixer_str)
1054 return -ENOMEM;
1055
1056 snprintf(mixer_str, ctl_len, "%s %d %s", mixer_ctl_name,
1057 rtd->pcm->device, topo_text);
1058
1059 topology_control[0].name = mixer_str;
1060 topology_control[0].private_value = rtd->dai_link->id;
Meng Wangee084a02018-09-04 16:11:58 +08001061 ret = snd_soc_add_component_controls(component, topology_control,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301062 ARRAY_SIZE(topology_control));
1063 msm_pcm_fe_topology[rtd->dai_link->id] = 0;
1064 kfree(mixer_str);
1065 return ret;
1066}
1067
1068static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
1069 struct snd_ctl_elem_value *ucontrol)
1070{
1071 u64 fe_id = kcontrol->private_value;
1072 int session_type = SESSION_TYPE_RX;
1073 int be_id = ucontrol->value.integer.value[3];
1074 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
1075 int ret = 0;
1076
1077 cfg_data.app_type = ucontrol->value.integer.value[0];
1078 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
1079 if (ucontrol->value.integer.value[2] != 0)
1080 cfg_data.sample_rate = ucontrol->value.integer.value[2];
1081 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
1082 __func__, fe_id, session_type, be_id,
1083 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1084 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
1085 be_id, &cfg_data);
1086 if (ret < 0)
1087 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
1088 __func__, ret);
1089 return ret;
1090}
1091
1092static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
1093 struct snd_ctl_elem_value *ucontrol)
1094{
1095 u64 fe_id = kcontrol->private_value;
1096 int session_type = SESSION_TYPE_RX;
1097 int be_id = 0;
1098 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
1099 int ret = 0;
1100
1101 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
1102 &be_id, &cfg_data);
1103 if (ret < 0) {
1104 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
1105 __func__, ret);
1106 goto done;
1107 }
1108
1109 ucontrol->value.integer.value[0] = cfg_data.app_type;
1110 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
1111 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
1112 ucontrol->value.integer.value[3] = be_id;
1113 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
1114 __func__, fe_id, session_type, be_id,
1115 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1116done:
1117 return ret;
1118}
1119
1120static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
1121 struct snd_ctl_elem_value *ucontrol)
1122{
1123 u64 fe_id = kcontrol->private_value;
1124 int session_type = SESSION_TYPE_TX;
1125 int be_id = ucontrol->value.integer.value[3];
1126 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
1127 int ret = 0;
1128
1129 cfg_data.app_type = ucontrol->value.integer.value[0];
1130 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
1131 if (ucontrol->value.integer.value[2] != 0)
1132 cfg_data.sample_rate = ucontrol->value.integer.value[2];
1133 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
1134 __func__, fe_id, session_type, be_id,
1135 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1136 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
1137 be_id, &cfg_data);
1138 if (ret < 0)
1139 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
1140 __func__, ret);
1141
1142 return ret;
1143}
1144
1145static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
1146 struct snd_ctl_elem_value *ucontrol)
1147{
1148 u64 fe_id = kcontrol->private_value;
1149 int session_type = SESSION_TYPE_TX;
1150 int be_id = 0;
1151 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
1152 int ret = 0;
1153
1154 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
1155 &be_id, &cfg_data);
1156 if (ret < 0) {
1157 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
1158 __func__, ret);
1159 goto done;
1160 }
1161
1162 ucontrol->value.integer.value[0] = cfg_data.app_type;
1163 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
1164 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
1165 ucontrol->value.integer.value[3] = be_id;
1166 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
1167 __func__, fe_id, session_type, be_id,
1168 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1169done:
1170 return ret;
1171}
1172
1173static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
1174{
1175 struct snd_pcm *pcm = rtd->pcm;
1176 struct snd_pcm_usr *app_type_info;
1177 struct snd_kcontrol *kctl;
1178 const char *playback_mixer_ctl_name = "Audio Stream";
1179 const char *capture_mixer_ctl_name = "Audio Stream Capture";
1180 const char *deviceNo = "NN";
1181 const char *suffix = "App Type Cfg";
1182 int ctl_len, ret = 0;
1183
1184 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
1185 ctl_len = strlen(playback_mixer_ctl_name) + 1 +
1186 strlen(deviceNo) + 1 +
1187 strlen(suffix) + 1;
1188 pr_debug("%s: Playback app type cntrl add\n", __func__);
1189 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1190 NULL, 1, ctl_len, rtd->dai_link->id,
1191 &app_type_info);
1192 if (ret < 0) {
1193 pr_err("%s: playback app type cntrl add failed, err: %d\n",
1194 __func__, ret);
1195 return ret;
1196 }
1197 kctl = app_type_info->kctl;
1198 snprintf(kctl->id.name, ctl_len, "%s %d %s",
1199 playback_mixer_ctl_name, rtd->pcm->device, suffix);
1200 kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
1201 kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
1202 }
1203
1204 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
1205 ctl_len = strlen(capture_mixer_ctl_name) + 1 +
1206 strlen(deviceNo) + 1 + strlen(suffix) + 1;
1207 pr_debug("%s: Capture app type cntrl add\n", __func__);
1208 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
1209 NULL, 1, ctl_len, rtd->dai_link->id,
1210 &app_type_info);
1211 if (ret < 0) {
1212 pr_err("%s: capture app type cntrl add failed, err: %d\n",
1213 __func__, ret);
1214 return ret;
1215 }
1216 kctl = app_type_info->kctl;
1217 snprintf(kctl->id.name, ctl_len, "%s %d %s",
1218 capture_mixer_ctl_name, rtd->pcm->device, suffix);
1219 kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
1220 kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
1221 }
1222
1223 return 0;
1224}
1225
Asish Bhattacharya84f7f732017-07-25 16:29:27 +05301226static int msm_pcm_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
1227 unsigned int cmd, unsigned long arg)
1228{
1229 int ret = 0;
1230 struct snd_pcm *pcm = hw->private_data;
1231 struct snd_pcm_mmap_fd __user *_mmap_fd = NULL;
1232 struct snd_pcm_mmap_fd mmap_fd;
1233 struct snd_pcm_substream *substream = NULL;
1234 int32_t dir = -1;
1235
1236 switch (cmd) {
1237 case SNDRV_PCM_IOCTL_MMAP_DATA_FD:
1238 _mmap_fd = (struct snd_pcm_mmap_fd __user *)arg;
1239 if (get_user(dir, (int32_t __user *)&(_mmap_fd->dir))) {
1240 pr_err("%s: error copying mmap_fd from user\n",
1241 __func__);
1242 ret = -EFAULT;
1243 break;
1244 }
1245 if (dir != OUT && dir != IN) {
1246 pr_err("%s invalid stream dir\n", __func__);
1247 ret = -EINVAL;
1248 break;
1249 }
1250 substream = pcm->streams[dir].substream;
1251 if (!substream) {
1252 pr_err("%s substream not found\n", __func__);
1253 ret = -ENODEV;
1254 break;
1255 }
1256 pr_debug("%s : %s MMAP Data fd\n", __func__,
1257 dir == 0 ? "P" : "C");
1258 if (msm_pcm_mmap_fd(substream, &mmap_fd) < 0) {
1259 pr_err("%s: error getting fd\n",
1260 __func__);
1261 ret = -EFAULT;
1262 break;
1263 }
1264 if (put_user(mmap_fd.fd, &_mmap_fd->fd) ||
1265 put_user(mmap_fd.size, &_mmap_fd->size) ||
1266 put_user(mmap_fd.actual_size, &_mmap_fd->actual_size)) {
1267 pr_err("%s: error copying fd\n", __func__);
1268 return -EFAULT;
1269 }
1270 break;
1271 default:
1272 ret = -EINVAL;
1273 break;
1274 }
1275 return ret;
1276}
1277
1278#ifdef CONFIG_COMPAT
1279static int msm_pcm_hwdep_compat_ioctl(struct snd_hwdep *hw,
1280 struct file *file,
1281 unsigned int cmd,
1282 unsigned long arg)
1283{
1284 /* we only support mmap fd. Handling is common in both modes */
1285 return msm_pcm_hwdep_ioctl(hw, file, cmd, arg);
1286}
1287#else
1288static int msm_pcm_hwdep_compat_ioctl(struct snd_hwdep *hw,
1289 struct file *file,
1290 unsigned int cmd,
1291 unsigned long arg)
1292{
1293 return -EINVAL;
1294}
1295#endif
1296
1297static int msm_pcm_add_hwdep_dev(struct snd_soc_pcm_runtime *runtime)
1298{
1299 struct snd_hwdep *hwdep;
1300 int rc;
1301 char id[] = "NOIRQ_NN";
1302
1303 snprintf(id, sizeof(id), "NOIRQ_%d", runtime->pcm->device);
1304 pr_debug("%s: pcm dev %d\n", __func__, runtime->pcm->device);
1305 rc = snd_hwdep_new(runtime->card->snd_card,
1306 &id[0],
1307 HWDEP_FE_BASE + runtime->pcm->device,
1308 &hwdep);
1309 if (!hwdep || rc < 0) {
1310 pr_err("%s: hwdep intf failed to create %s - hwdep\n", __func__,
1311 id);
1312 return rc;
1313 }
1314
1315 hwdep->iface = SNDRV_HWDEP_IFACE_AUDIO_BE; /* for lack of a FE iface */
1316 hwdep->private_data = runtime->pcm; /* of type struct snd_pcm */
1317 hwdep->ops.ioctl = msm_pcm_hwdep_ioctl;
1318 hwdep->ops.ioctl_compat = msm_pcm_hwdep_compat_ioctl;
1319 return 0;
1320}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301321
1322static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
1323{
1324 struct snd_card *card = rtd->card->snd_card;
1325 struct snd_pcm *pcm = rtd->pcm;
1326 int ret;
1327
1328 pr_debug("%s , register new control\n", __func__);
1329 if (!card->dev->coherent_dma_mask)
1330 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
1331
Surendar Karka5628e8b2018-07-23 10:41:58 +05301332 ret = msm_pcm_add_channel_map_control(rtd);
1333 if (ret)
1334 pr_err("%s: Could not add pcm Channel Map Control\n",
1335 __func__);
1336
Haynes Mathew George86eb0ce2018-04-06 18:18:51 -07001337 ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_PLAYBACK);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301338 if (ret) {
Haynes Mathew George86eb0ce2018-04-06 18:18:51 -07001339 pr_err("%s: Could not add pcm playback volume Control %d\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301340 __func__, ret);
1341 }
Haynes Mathew George86eb0ce2018-04-06 18:18:51 -07001342 ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_CAPTURE);
1343 if (ret) {
1344 pr_err("%s: Could not add pcm capture volume Control %d\n",
1345 __func__, ret);
1346 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301347 ret = msm_pcm_add_fe_topology_control(rtd);
1348 if (ret) {
1349 pr_err("%s: Could not add pcm topology control %d\n",
1350 __func__, ret);
1351 }
1352
1353 ret = msm_pcm_add_app_type_controls(rtd);
1354 if (ret) {
1355 pr_err("%s: Could not add app type controls failed %d\n",
1356 __func__, ret);
1357 }
Asish Bhattacharya84f7f732017-07-25 16:29:27 +05301358 ret = msm_pcm_add_hwdep_dev(rtd);
1359 if (ret)
1360 pr_err("%s: Could not add hw dep node\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301361 pcm->nonatomic = true;
Surendar Karka5628e8b2018-07-23 10:41:58 +05301362
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301363 return ret;
1364}
1365
1366
1367static const struct snd_pcm_ops msm_pcm_ops = {
1368 .open = msm_pcm_open,
1369 .prepare = msm_pcm_prepare,
Meng Wangac147b72017-10-30 16:46:16 +08001370 .copy_user = msm_pcm_copy,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301371 .hw_params = msm_pcm_hw_params,
1372 .ioctl = msm_pcm_ioctl,
Asish Bhattacharya84f7f732017-07-25 16:29:27 +05301373#ifdef CONFIG_COMPAT
1374 .compat_ioctl = msm_pcm_compat_ioctl,
1375#endif
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301376 .trigger = msm_pcm_trigger,
1377 .pointer = msm_pcm_pointer,
1378 .mmap = msm_pcm_mmap,
1379 .close = msm_pcm_close,
1380};
1381
Meng Wangee084a02018-09-04 16:11:58 +08001382static struct snd_soc_component_driver msm_soc_component = {
1383 .name = DRV_NAME,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301384 .ops = &msm_pcm_ops,
1385 .pcm_new = msm_asoc_pcm_new,
1386};
1387
1388static int msm_pcm_probe(struct platform_device *pdev)
1389{
1390 int rc;
1391 struct msm_plat_data *pdata;
1392 const char *latency_level;
1393 int perf_mode = LOW_LATENCY_PCM_MODE;
1394
1395 dev_dbg(&pdev->dev, "Pull mode driver probe\n");
1396
1397 if (of_property_read_bool(pdev->dev.of_node,
1398 "qcom,msm-pcm-low-latency")) {
1399
1400 rc = of_property_read_string(pdev->dev.of_node,
1401 "qcom,latency-level", &latency_level);
1402 if (!rc) {
1403 if (!strcmp(latency_level, "ultra"))
1404 perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
1405 else if (!strcmp(latency_level, "ull-pp"))
1406 perf_mode = ULL_POST_PROCESSING_PCM_MODE;
1407 }
1408 }
1409
1410 pdata = devm_kzalloc(&pdev->dev,
1411 sizeof(struct msm_plat_data), GFP_KERNEL);
1412 if (!pdata)
1413 return -ENOMEM;
1414
1415 pdata->perf_mode = perf_mode;
1416
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +05301417 mutex_init(&pdata->lock);
1418
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301419 dev_set_drvdata(&pdev->dev, pdata);
1420
1421 dev_dbg(&pdev->dev, "%s: dev name %s\n",
1422 __func__, dev_name(&pdev->dev));
1423 dev_dbg(&pdev->dev, "Pull mode driver register\n");
Meng Wangee084a02018-09-04 16:11:58 +08001424 rc = snd_soc_register_component(&pdev->dev,
1425 &msm_soc_component,
1426 NULL, 0);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301427
1428 if (rc)
1429 dev_err(&pdev->dev, "Failed to register pull mode driver\n");
1430
1431 return rc;
1432}
1433
1434static int msm_pcm_remove(struct platform_device *pdev)
1435{
1436 struct msm_plat_data *pdata;
1437
1438 dev_dbg(&pdev->dev, "Pull mode remove\n");
1439 pdata = dev_get_drvdata(&pdev->dev);
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +05301440 mutex_destroy(&pdata->lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301441 devm_kfree(&pdev->dev, pdata);
Meng Wangee084a02018-09-04 16:11:58 +08001442 snd_soc_unregister_component(&pdev->dev);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301443 return 0;
1444}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301445static const struct of_device_id msm_pcm_noirq_dt_match[] = {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301446 {.compatible = "qcom,msm-pcm-dsp-noirq"},
1447 {}
1448};
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301449MODULE_DEVICE_TABLE(of, msm_pcm_noirq_dt_match);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301450
1451static struct platform_driver msm_pcm_driver_noirq = {
1452 .driver = {
1453 .name = "msm-pcm-dsp-noirq",
1454 .owner = THIS_MODULE,
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301455 .of_match_table = msm_pcm_noirq_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08001456 .suppress_bind_attrs = true,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301457 },
1458 .probe = msm_pcm_probe,
1459 .remove = msm_pcm_remove,
1460};
1461
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301462int __init msm_pcm_noirq_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301463{
1464 return platform_driver_register(&msm_pcm_driver_noirq);
1465}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301466
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05301467void msm_pcm_noirq_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301468{
1469 platform_driver_unregister(&msm_pcm_driver_noirq);
1470}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301471
1472MODULE_DESCRIPTION("PCM NOIRQ module platform driver");
1473MODULE_LICENSE("GPL v2");