blob: a317142b642eefafcce5ca50702763c44c2970d4 [file] [log] [blame]
Meng Wang43bbb872018-12-10 12:32:05 +08001// SPDX-License-Identifier: GPL-2.0-only
Meng Wangac147b72017-10-30 16:46:16 +08002/* Copyright (c) 2016-2018, 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;
448
449 if (!substream->runtime) {
450 pr_err("%s substream runtime not found\n", __func__);
451 return -EFAULT;
452 }
453
454 prtd = substream->runtime->private_data;
455 if (!prtd || !prtd->audio_client || !prtd->mmap_flag) {
456 pr_err("%s no audio client or not an mmap session\n", __func__);
457 return -EINVAL;
458 }
459
460 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
461 dir = IN;
462 else
463 dir = OUT;
464
465 apd = prtd->audio_client->port;
466 ab = &(apd[dir].buf[0]);
Banajit Goswami08bb7362017-11-03 22:48:23 -0700467 /*
468 * Passing O_CLOEXEC as flag passed to fd, to be in sync with
469 * previous implimentation.
470 * This was the flag used by previous internal wrapper API, which
471 * used to call dma_buf_fd internally.
472 */
473 mmap_fd->fd = dma_buf_fd(ab->dma_buf, O_CLOEXEC);
Asish Bhattacharya84f7f732017-07-25 16:29:27 +0530474 if (mmap_fd->fd >= 0) {
475 mmap_fd->dir = dir;
476 mmap_fd->actual_size = ab->actual_size;
477 mmap_fd->size = ab->size;
478 }
479 return mmap_fd->fd < 0 ? -EFAULT : 0;
480}
481
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530482static int msm_pcm_ioctl(struct snd_pcm_substream *substream,
483 unsigned int cmd, void *arg)
484{
485 struct snd_pcm_runtime *runtime = substream->runtime;
486 struct msm_audio *prtd = runtime->private_data;
487 int dir = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 0 : 1;
488 struct audio_buffer *buf;
489
490 switch (cmd) {
491 case SNDRV_PCM_IOCTL1_RESET:
492 pr_debug("%s: %s SNDRV_PCM_IOCTL1_RESET\n", __func__,
493 dir == 0 ? "P" : "C");
494 buf = q6asm_shared_io_buf(prtd->audio_client, dir);
495
496 if (buf && buf->data)
497 memset(buf->data, 0, buf->actual_size);
498 break;
499 default:
500 break;
501 }
502
503 return snd_pcm_lib_ioctl(substream, cmd, arg);
504}
505
Asish Bhattacharya84f7f732017-07-25 16:29:27 +0530506#ifdef CONFIG_COMPAT
507static int msm_pcm_compat_ioctl(struct snd_pcm_substream *substream,
508 unsigned int cmd, void *arg)
509{
510 /* we only handle RESET which is common for both modes */
511 return msm_pcm_ioctl(substream, cmd, arg);
512}
513#endif
514
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530515static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
516{
517 struct snd_pcm_runtime *runtime = substream->runtime;
518 uint32_t read_index, wall_clk_msw, wall_clk_lsw;
519 /*these are offsets, unlike ASoC's full values*/
520 snd_pcm_sframes_t hw_ptr;
521 snd_pcm_sframes_t period_size;
522 int ret;
523 int retries = 10;
524 struct msm_audio *prtd = runtime->private_data;
525
526 period_size = runtime->period_size;
527
528 do {
529 ret = q6asm_get_shared_pos(prtd->audio_client,
530 &read_index, &wall_clk_msw,
531 &wall_clk_lsw);
532 } while (ret == -EAGAIN && --retries);
533
534 if (ret || !period_size) {
535 pr_err("get_shared_pos error or zero period size\n");
536 return 0;
537 }
538
539 hw_ptr = bytes_to_frames(substream->runtime,
540 read_index);
541
542 if (runtime->control->appl_ptr == 0) {
543 pr_debug("ptr(%s): appl(0), hw = %lu read_index = %u\n",
544 prtd->substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
545 "P" : "C",
546 hw_ptr, read_index);
547 }
548 return (hw_ptr/period_size) * period_size;
549}
550
551static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
Meng Wangac147b72017-10-30 16:46:16 +0800552 unsigned long hwoff, void __user *buf, unsigned long fbytes)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530553{
554 return -EINVAL;
555}
556
557static int msm_pcm_mmap(struct snd_pcm_substream *substream,
558 struct vm_area_struct *vma)
559{
560 struct snd_pcm_runtime *runtime = substream->runtime;
561 struct msm_audio *prtd = runtime->private_data;
562 struct audio_client *ac = prtd->audio_client;
563 struct audio_port_data *apd = ac->port;
564 struct audio_buffer *ab;
565 int dir = -1;
566 int ret;
567
568 pr_debug("%s: mmap begin\n", __func__);
569 prtd->mmap_flag = 1;
570
571 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
572 dir = IN;
573 else
574 dir = OUT;
575
576 ab = &(apd[dir].buf[0]);
577
578 ret = msm_audio_ion_mmap(ab, vma);
579
580 if (ret)
581 prtd->mmap_flag = 0;
582
583 return ret;
584}
585
586static int msm_pcm_prepare(struct snd_pcm_substream *substream)
587{
588 struct snd_pcm_runtime *runtime = substream->runtime;
589 struct msm_audio *prtd = runtime->private_data;
590
591 if (!prtd || !prtd->mmap_flag)
592 return -EIO;
593
594 return 0;
595}
596
597static int msm_pcm_close(struct snd_pcm_substream *substream)
598{
599 struct snd_pcm_runtime *runtime = substream->runtime;
600 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
601 struct msm_audio *prtd = runtime->private_data;
602 struct audio_client *ac = prtd->audio_client;
603 uint32_t timeout;
604 int dir = 0;
605 int ret = 0;
606
607 if (ac) {
608 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
609 dir = IN;
610 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
611 dir = OUT;
612
613 /* determine timeout length */
614 if (runtime->frame_bits == 0 || runtime->rate == 0) {
615 timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
616 } else {
617 timeout = (runtime->period_size *
618 CMD_EOS_TIMEOUT_MULTIPLIER) /
619 ((runtime->frame_bits / 8) *
620 runtime->rate);
621 if (timeout < CMD_EOS_MIN_TIMEOUT_LENGTH)
622 timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
623 }
624
625 q6asm_cmd(ac, CMD_CLOSE);
626
627 ret = q6asm_shared_io_free(ac, dir);
628
629 if (ret) {
630 pr_err("%s: Failed to close pull mode, ret %d\n",
631 __func__, ret);
632 }
633 q6asm_audio_client_free(ac);
634 }
635 msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
636 dir == IN ?
637 SNDRV_PCM_STREAM_PLAYBACK :
638 SNDRV_PCM_STREAM_CAPTURE);
639 kfree(prtd);
640 runtime->private_data = NULL;
641
642 return 0;
643}
644
645static int msm_pcm_set_volume(struct msm_audio *prtd, uint32_t volume)
646{
647 int rc = 0;
648
649 if (prtd && prtd->audio_client) {
650 pr_debug("%s: channels %d volume 0x%x\n", __func__,
651 prtd->channel_mode, volume);
652 rc = q6asm_set_volume(prtd->audio_client, volume);
653 if (rc < 0) {
654 pr_err("%s: Send Volume command failed rc=%d\n",
655 __func__, rc);
656 }
657 }
658 return rc;
659}
660
661static int msm_pcm_volume_ctl_get(struct snd_kcontrol *kcontrol,
662 struct snd_ctl_elem_value *ucontrol)
663{
664 struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
665 struct snd_pcm_substream *substream =
666 vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
667 struct msm_audio *prtd;
668
669 pr_debug("%s\n", __func__);
670 if (!substream) {
671 pr_err("%s substream not found\n", __func__);
672 return -ENODEV;
673 }
674 if (!substream->runtime) {
Vignesh Kulothungan2ce67842018-09-25 16:40:29 -0700675 pr_debug("%s substream runtime not found\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530676 return 0;
677 }
678 prtd = substream->runtime->private_data;
679 if (prtd)
680 ucontrol->value.integer.value[0] = prtd->volume;
681 return 0;
682}
683
684static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
685 struct snd_ctl_elem_value *ucontrol)
686{
687 int rc = 0;
688 struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
689 struct snd_pcm_substream *substream =
690 vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
691 struct msm_audio *prtd;
692 int volume = ucontrol->value.integer.value[0];
693
694 pr_debug("%s: volume : 0x%x\n", __func__, volume);
695 if (!substream) {
696 pr_err("%s substream not found\n", __func__);
697 return -ENODEV;
698 }
699 if (!substream->runtime) {
700 pr_err("%s substream runtime not found\n", __func__);
701 return 0;
702 }
703 prtd = substream->runtime->private_data;
704 if (prtd) {
705 rc = msm_pcm_set_volume(prtd, volume);
706 prtd->volume = volume;
707 }
708 return rc;
709}
710
711static int msm_pcm_add_volume_control(struct snd_soc_pcm_runtime *rtd)
712{
713 int ret = 0;
714 struct snd_pcm *pcm = rtd->pcm;
715 struct snd_pcm_volume *volume_info;
716 struct snd_kcontrol *kctl;
717
718 dev_dbg(rtd->dev, "%s, Volume control add\n", __func__);
719 ret = snd_pcm_add_volume_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
720 NULL, 1, rtd->dai_link->id,
721 &volume_info);
722 if (ret < 0) {
723 pr_err("%s volume control failed ret %d\n", __func__, ret);
724 return ret;
725 }
726 kctl = volume_info->kctl;
727 kctl->put = msm_pcm_volume_ctl_put;
728 kctl->get = msm_pcm_volume_ctl_get;
729 kctl->tlv.p = msm_pcm_vol_gain;
730 return 0;
731}
732
Surendar Karka5628e8b2018-07-23 10:41:58 +0530733static int msm_pcm_channel_map_put(struct snd_kcontrol *kcontrol,
734 struct snd_ctl_elem_value *ucontrol)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530735{
Surendar Karka5628e8b2018-07-23 10:41:58 +0530736 struct snd_soc_component *pcm = snd_kcontrol_chip(kcontrol);
737 u64 fe_id = kcontrol->private_value;
738 struct msm_plat_data *pdata = (struct msm_plat_data *)
739 snd_soc_component_get_drvdata(pcm);
740 int rc = 0, i = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530741
Surendar Karka5628e8b2018-07-23 10:41:58 +0530742 pr_debug("%s: fe_id- %llu\n", __func__, fe_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530743
Surendar Karka5628e8b2018-07-23 10:41:58 +0530744 if (fe_id >= MSM_FRONTEND_DAI_MAX) {
745 pr_err("%s Received out of bounds fe_id %llu\n",
746 __func__, fe_id);
747 rc = -EINVAL;
748 goto end;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530749 }
Surendar Karka5628e8b2018-07-23 10:41:58 +0530750
751 if (pdata->ch_map[fe_id]) {
752 pdata->ch_map[fe_id]->set_ch_map = true;
753 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
754 pdata->ch_map[fe_id]->channel_map[i] =
755 (char)(ucontrol->value.integer.value[i]);
756 } else {
757 pr_debug("%s: no memory for ch_map, default will be set\n",
758 __func__);
759 }
760end:
761 pr_debug("%s: ret %d\n", __func__, rc);
762 return rc;
763}
764
765static int msm_pcm_channel_map_info(struct snd_kcontrol *kcontrol,
766 struct snd_ctl_elem_info *uinfo)
767{
768 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
769 uinfo->count = 8;
770 uinfo->value.integer.min = 0;
771 uinfo->value.integer.max = 0xFFFFFFFF;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530772 return 0;
773}
774
Surendar Karka5628e8b2018-07-23 10:41:58 +0530775static int msm_pcm_channel_map_get(struct snd_kcontrol *kcontrol,
776 struct snd_ctl_elem_value *ucontrol)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530777{
Surendar Karka5628e8b2018-07-23 10:41:58 +0530778 struct snd_soc_component *pcm = snd_kcontrol_chip(kcontrol);
779 u64 fe_id = kcontrol->private_value;
780 struct msm_plat_data *pdata = (struct msm_plat_data *)
781 snd_soc_component_get_drvdata(pcm);
782 int rc = 0, i = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530783
Surendar Karka5628e8b2018-07-23 10:41:58 +0530784 pr_debug("%s: fe_id- %llu\n", __func__, fe_id);
785 if (fe_id >= MSM_FRONTEND_DAI_MAX) {
786 pr_err("%s: Received out of bounds fe_id %llu\n",
787 __func__, fe_id);
788 rc = -EINVAL;
789 goto end;
790 }
791 if (pdata->ch_map[fe_id]) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530792 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
793 ucontrol->value.integer.value[i] =
Surendar Karka5628e8b2018-07-23 10:41:58 +0530794 pdata->ch_map[fe_id]->channel_map[i];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530795 }
Surendar Karka5628e8b2018-07-23 10:41:58 +0530796end:
797 pr_debug("%s: ret %d\n", __func__, rc);
798 return rc;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530799}
800
Surendar Karka5628e8b2018-07-23 10:41:58 +0530801static int msm_pcm_add_channel_map_control(struct snd_soc_pcm_runtime *rtd)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530802{
Meng Wangee084a02018-09-04 16:11:58 +0800803 struct snd_soc_component *component = NULL;
Surendar Karka5628e8b2018-07-23 10:41:58 +0530804 const char *mixer_ctl_name = "Playback Channel Map";
805 const char *deviceNo = "NN";
806 char *mixer_str = NULL;
807 struct msm_plat_data *pdata = NULL;
808 int ctl_len = 0;
809 struct snd_kcontrol_new fe_channel_map_control[1] = {
810 {
811 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
812 .name = "?",
813 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
814 .info = msm_pcm_channel_map_info,
815 .get = msm_pcm_channel_map_get,
816 .put = msm_pcm_channel_map_put,
817 .private_value = 0,
818 }
819 };
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530820
Surendar Karka5628e8b2018-07-23 10:41:58 +0530821 if (!rtd) {
822 pr_err("%s: NULL rtd\n", __func__);
823 return -EINVAL;
824 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530825
Meng Wangee084a02018-09-04 16:11:58 +0800826 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
827 if (!component) {
828 pr_err("%s: component is NULL\n", __func__);
829 return -EINVAL;
830 }
831
Surendar Karka5628e8b2018-07-23 10:41:58 +0530832 pr_debug("%s: added new pcm FE with name %s, id %d, cpu dai %s, device no %d\n",
833 __func__, rtd->dai_link->name, rtd->dai_link->id,
834 rtd->dai_link->cpu_dai_name, rtd->pcm->device);
835
836 ctl_len = strlen(mixer_ctl_name) + strlen(deviceNo) + 1;
837 mixer_str = kzalloc(ctl_len, GFP_KERNEL);
838 if (!mixer_str)
839 return -ENOMEM;
840
841 snprintf(mixer_str, ctl_len, "%s%d", mixer_ctl_name, rtd->pcm->device);
842
843 fe_channel_map_control[0].name = mixer_str;
844 fe_channel_map_control[0].private_value = rtd->dai_link->id;
845 pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
Meng Wangee084a02018-09-04 16:11:58 +0800846 snd_soc_add_component_controls(component,
Surendar Karka5628e8b2018-07-23 10:41:58 +0530847 fe_channel_map_control,
848 ARRAY_SIZE(fe_channel_map_control));
849
Meng Wangee084a02018-09-04 16:11:58 +0800850 pdata = snd_soc_component_get_drvdata(component);
Surendar Karka5628e8b2018-07-23 10:41:58 +0530851 pdata->ch_map[rtd->dai_link->id] =
852 kzalloc(sizeof(struct msm_pcm_ch_map), GFP_KERNEL);
853 if (!pdata->ch_map[rtd->dai_link->id]) {
854 pr_err("%s: Could not allocate memory for channel map\n",
855 __func__);
856 kfree(mixer_str);
857 return -ENOMEM;
858 }
859 kfree(mixer_str);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530860 return 0;
861}
862
863static int msm_pcm_fe_topology_info(struct snd_kcontrol *kcontrol,
864 struct snd_ctl_elem_info *uinfo)
865{
866 const struct soc_enum *e = &msm_pcm_fe_topology_enum[0];
867
868 return snd_ctl_enum_info(uinfo, 1, e->items, e->texts);
869}
870
871static int msm_pcm_fe_topology_get(struct snd_kcontrol *kcontrol,
872 struct snd_ctl_elem_value *ucontrol)
873{
874 unsigned long fe_id = kcontrol->private_value;
875
876 if (fe_id >= MSM_FRONTEND_DAI_MAX) {
877 pr_err("%s Received out of bound fe_id %lu\n", __func__, fe_id);
878 return -EINVAL;
879 }
880
881 pr_debug("%s: %lu topology %s\n", __func__, fe_id,
882 msm_pcm_fe_topology_text[msm_pcm_fe_topology[fe_id]]);
883 ucontrol->value.enumerated.item[0] = msm_pcm_fe_topology[fe_id];
884 return 0;
885}
886
887static int msm_pcm_fe_topology_put(struct snd_kcontrol *kcontrol,
888 struct snd_ctl_elem_value *ucontrol)
889{
890 unsigned long fe_id = kcontrol->private_value;
891 unsigned int item;
892
893 if (fe_id >= MSM_FRONTEND_DAI_MAX) {
894 pr_err("%s Received out of bound fe_id %lu\n", __func__, fe_id);
895 return -EINVAL;
896 }
897
898 item = ucontrol->value.enumerated.item[0];
899 if (item >= ARRAY_SIZE(msm_pcm_fe_topology_text)) {
900 pr_err("%s Received out of bound topology %lu\n", __func__,
901 fe_id);
902 return -EINVAL;
903 }
904
905 pr_debug("%s: %lu new topology %s\n", __func__, fe_id,
906 msm_pcm_fe_topology_text[item]);
907 msm_pcm_fe_topology[fe_id] = item;
908 return 0;
909}
910
911static int msm_pcm_add_fe_topology_control(struct snd_soc_pcm_runtime *rtd)
912{
Meng Wangee084a02018-09-04 16:11:58 +0800913 struct snd_soc_component *component = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530914 const char *mixer_ctl_name = "PCM_Dev";
915 const char *deviceNo = "NN";
916 const char *topo_text = "Topology";
917 char *mixer_str = NULL;
918 int ctl_len;
919 int ret;
920 struct snd_kcontrol_new topology_control[1] = {
921 {
922 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
923 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
924 .name = "?",
925 .info = msm_pcm_fe_topology_info,
926 .get = msm_pcm_fe_topology_get,
927 .put = msm_pcm_fe_topology_put,
928 .private_value = 0,
929 },
930 };
931
Meng Wangee084a02018-09-04 16:11:58 +0800932 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
933 if (!component) {
934 pr_err("%s: component is NULL\n", __func__);
935 return -EINVAL;
936 }
937
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530938 ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1 +
939 strlen(topo_text) + 1;
940 mixer_str = kzalloc(ctl_len, GFP_KERNEL);
941
942 if (!mixer_str)
943 return -ENOMEM;
944
945 snprintf(mixer_str, ctl_len, "%s %d %s", mixer_ctl_name,
946 rtd->pcm->device, topo_text);
947
948 topology_control[0].name = mixer_str;
949 topology_control[0].private_value = rtd->dai_link->id;
Meng Wangee084a02018-09-04 16:11:58 +0800950 ret = snd_soc_add_component_controls(component, topology_control,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530951 ARRAY_SIZE(topology_control));
952 msm_pcm_fe_topology[rtd->dai_link->id] = 0;
953 kfree(mixer_str);
954 return ret;
955}
956
957static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
958 struct snd_ctl_elem_value *ucontrol)
959{
960 u64 fe_id = kcontrol->private_value;
961 int session_type = SESSION_TYPE_RX;
962 int be_id = ucontrol->value.integer.value[3];
963 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
964 int ret = 0;
965
966 cfg_data.app_type = ucontrol->value.integer.value[0];
967 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
968 if (ucontrol->value.integer.value[2] != 0)
969 cfg_data.sample_rate = ucontrol->value.integer.value[2];
970 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
971 __func__, fe_id, session_type, be_id,
972 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
973 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
974 be_id, &cfg_data);
975 if (ret < 0)
976 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
977 __func__, ret);
978 return ret;
979}
980
981static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
982 struct snd_ctl_elem_value *ucontrol)
983{
984 u64 fe_id = kcontrol->private_value;
985 int session_type = SESSION_TYPE_RX;
986 int be_id = 0;
987 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
988 int ret = 0;
989
990 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
991 &be_id, &cfg_data);
992 if (ret < 0) {
993 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
994 __func__, ret);
995 goto done;
996 }
997
998 ucontrol->value.integer.value[0] = cfg_data.app_type;
999 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
1000 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
1001 ucontrol->value.integer.value[3] = be_id;
1002 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
1003 __func__, fe_id, session_type, be_id,
1004 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1005done:
1006 return ret;
1007}
1008
1009static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
1010 struct snd_ctl_elem_value *ucontrol)
1011{
1012 u64 fe_id = kcontrol->private_value;
1013 int session_type = SESSION_TYPE_TX;
1014 int be_id = ucontrol->value.integer.value[3];
1015 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
1016 int ret = 0;
1017
1018 cfg_data.app_type = ucontrol->value.integer.value[0];
1019 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
1020 if (ucontrol->value.integer.value[2] != 0)
1021 cfg_data.sample_rate = ucontrol->value.integer.value[2];
1022 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
1023 __func__, fe_id, session_type, be_id,
1024 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1025 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
1026 be_id, &cfg_data);
1027 if (ret < 0)
1028 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
1029 __func__, ret);
1030
1031 return ret;
1032}
1033
1034static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
1035 struct snd_ctl_elem_value *ucontrol)
1036{
1037 u64 fe_id = kcontrol->private_value;
1038 int session_type = SESSION_TYPE_TX;
1039 int be_id = 0;
1040 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
1041 int ret = 0;
1042
1043 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
1044 &be_id, &cfg_data);
1045 if (ret < 0) {
1046 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
1047 __func__, ret);
1048 goto done;
1049 }
1050
1051 ucontrol->value.integer.value[0] = cfg_data.app_type;
1052 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
1053 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
1054 ucontrol->value.integer.value[3] = be_id;
1055 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
1056 __func__, fe_id, session_type, be_id,
1057 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
1058done:
1059 return ret;
1060}
1061
1062static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
1063{
1064 struct snd_pcm *pcm = rtd->pcm;
1065 struct snd_pcm_usr *app_type_info;
1066 struct snd_kcontrol *kctl;
1067 const char *playback_mixer_ctl_name = "Audio Stream";
1068 const char *capture_mixer_ctl_name = "Audio Stream Capture";
1069 const char *deviceNo = "NN";
1070 const char *suffix = "App Type Cfg";
1071 int ctl_len, ret = 0;
1072
1073 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
1074 ctl_len = strlen(playback_mixer_ctl_name) + 1 +
1075 strlen(deviceNo) + 1 +
1076 strlen(suffix) + 1;
1077 pr_debug("%s: Playback app type cntrl add\n", __func__);
1078 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1079 NULL, 1, ctl_len, rtd->dai_link->id,
1080 &app_type_info);
1081 if (ret < 0) {
1082 pr_err("%s: playback app type cntrl add failed, err: %d\n",
1083 __func__, ret);
1084 return ret;
1085 }
1086 kctl = app_type_info->kctl;
1087 snprintf(kctl->id.name, ctl_len, "%s %d %s",
1088 playback_mixer_ctl_name, rtd->pcm->device, suffix);
1089 kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
1090 kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
1091 }
1092
1093 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
1094 ctl_len = strlen(capture_mixer_ctl_name) + 1 +
1095 strlen(deviceNo) + 1 + strlen(suffix) + 1;
1096 pr_debug("%s: Capture app type cntrl add\n", __func__);
1097 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
1098 NULL, 1, ctl_len, rtd->dai_link->id,
1099 &app_type_info);
1100 if (ret < 0) {
1101 pr_err("%s: capture app type cntrl add failed, err: %d\n",
1102 __func__, ret);
1103 return ret;
1104 }
1105 kctl = app_type_info->kctl;
1106 snprintf(kctl->id.name, ctl_len, "%s %d %s",
1107 capture_mixer_ctl_name, rtd->pcm->device, suffix);
1108 kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
1109 kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
1110 }
1111
1112 return 0;
1113}
1114
Asish Bhattacharya84f7f732017-07-25 16:29:27 +05301115static int msm_pcm_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
1116 unsigned int cmd, unsigned long arg)
1117{
1118 int ret = 0;
1119 struct snd_pcm *pcm = hw->private_data;
1120 struct snd_pcm_mmap_fd __user *_mmap_fd = NULL;
1121 struct snd_pcm_mmap_fd mmap_fd;
1122 struct snd_pcm_substream *substream = NULL;
1123 int32_t dir = -1;
1124
1125 switch (cmd) {
1126 case SNDRV_PCM_IOCTL_MMAP_DATA_FD:
1127 _mmap_fd = (struct snd_pcm_mmap_fd __user *)arg;
1128 if (get_user(dir, (int32_t __user *)&(_mmap_fd->dir))) {
1129 pr_err("%s: error copying mmap_fd from user\n",
1130 __func__);
1131 ret = -EFAULT;
1132 break;
1133 }
1134 if (dir != OUT && dir != IN) {
1135 pr_err("%s invalid stream dir\n", __func__);
1136 ret = -EINVAL;
1137 break;
1138 }
1139 substream = pcm->streams[dir].substream;
1140 if (!substream) {
1141 pr_err("%s substream not found\n", __func__);
1142 ret = -ENODEV;
1143 break;
1144 }
1145 pr_debug("%s : %s MMAP Data fd\n", __func__,
1146 dir == 0 ? "P" : "C");
1147 if (msm_pcm_mmap_fd(substream, &mmap_fd) < 0) {
1148 pr_err("%s: error getting fd\n",
1149 __func__);
1150 ret = -EFAULT;
1151 break;
1152 }
1153 if (put_user(mmap_fd.fd, &_mmap_fd->fd) ||
1154 put_user(mmap_fd.size, &_mmap_fd->size) ||
1155 put_user(mmap_fd.actual_size, &_mmap_fd->actual_size)) {
1156 pr_err("%s: error copying fd\n", __func__);
1157 return -EFAULT;
1158 }
1159 break;
1160 default:
1161 ret = -EINVAL;
1162 break;
1163 }
1164 return ret;
1165}
1166
1167#ifdef CONFIG_COMPAT
1168static int msm_pcm_hwdep_compat_ioctl(struct snd_hwdep *hw,
1169 struct file *file,
1170 unsigned int cmd,
1171 unsigned long arg)
1172{
1173 /* we only support mmap fd. Handling is common in both modes */
1174 return msm_pcm_hwdep_ioctl(hw, file, cmd, arg);
1175}
1176#else
1177static int msm_pcm_hwdep_compat_ioctl(struct snd_hwdep *hw,
1178 struct file *file,
1179 unsigned int cmd,
1180 unsigned long arg)
1181{
1182 return -EINVAL;
1183}
1184#endif
1185
1186static int msm_pcm_add_hwdep_dev(struct snd_soc_pcm_runtime *runtime)
1187{
1188 struct snd_hwdep *hwdep;
1189 int rc;
1190 char id[] = "NOIRQ_NN";
1191
1192 snprintf(id, sizeof(id), "NOIRQ_%d", runtime->pcm->device);
1193 pr_debug("%s: pcm dev %d\n", __func__, runtime->pcm->device);
1194 rc = snd_hwdep_new(runtime->card->snd_card,
1195 &id[0],
1196 HWDEP_FE_BASE + runtime->pcm->device,
1197 &hwdep);
1198 if (!hwdep || rc < 0) {
1199 pr_err("%s: hwdep intf failed to create %s - hwdep\n", __func__,
1200 id);
1201 return rc;
1202 }
1203
1204 hwdep->iface = SNDRV_HWDEP_IFACE_AUDIO_BE; /* for lack of a FE iface */
1205 hwdep->private_data = runtime->pcm; /* of type struct snd_pcm */
1206 hwdep->ops.ioctl = msm_pcm_hwdep_ioctl;
1207 hwdep->ops.ioctl_compat = msm_pcm_hwdep_compat_ioctl;
1208 return 0;
1209}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301210
1211static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
1212{
1213 struct snd_card *card = rtd->card->snd_card;
1214 struct snd_pcm *pcm = rtd->pcm;
1215 int ret;
1216
1217 pr_debug("%s , register new control\n", __func__);
1218 if (!card->dev->coherent_dma_mask)
1219 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
1220
Surendar Karka5628e8b2018-07-23 10:41:58 +05301221 ret = msm_pcm_add_channel_map_control(rtd);
1222 if (ret)
1223 pr_err("%s: Could not add pcm Channel Map Control\n",
1224 __func__);
1225
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301226 ret = msm_pcm_add_volume_control(rtd);
1227 if (ret) {
1228 pr_err("%s: Could not add pcm Volume Control %d\n",
1229 __func__, ret);
1230 }
1231
1232 ret = msm_pcm_add_fe_topology_control(rtd);
1233 if (ret) {
1234 pr_err("%s: Could not add pcm topology control %d\n",
1235 __func__, ret);
1236 }
1237
1238 ret = msm_pcm_add_app_type_controls(rtd);
1239 if (ret) {
1240 pr_err("%s: Could not add app type controls failed %d\n",
1241 __func__, ret);
1242 }
Asish Bhattacharya84f7f732017-07-25 16:29:27 +05301243 ret = msm_pcm_add_hwdep_dev(rtd);
1244 if (ret)
1245 pr_err("%s: Could not add hw dep node\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301246 pcm->nonatomic = true;
Surendar Karka5628e8b2018-07-23 10:41:58 +05301247
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301248 return ret;
1249}
1250
1251
1252static const struct snd_pcm_ops msm_pcm_ops = {
1253 .open = msm_pcm_open,
1254 .prepare = msm_pcm_prepare,
Meng Wangac147b72017-10-30 16:46:16 +08001255 .copy_user = msm_pcm_copy,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301256 .hw_params = msm_pcm_hw_params,
1257 .ioctl = msm_pcm_ioctl,
Asish Bhattacharya84f7f732017-07-25 16:29:27 +05301258#ifdef CONFIG_COMPAT
1259 .compat_ioctl = msm_pcm_compat_ioctl,
1260#endif
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301261 .trigger = msm_pcm_trigger,
1262 .pointer = msm_pcm_pointer,
1263 .mmap = msm_pcm_mmap,
1264 .close = msm_pcm_close,
1265};
1266
Meng Wangee084a02018-09-04 16:11:58 +08001267static struct snd_soc_component_driver msm_soc_component = {
1268 .name = DRV_NAME,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301269 .ops = &msm_pcm_ops,
1270 .pcm_new = msm_asoc_pcm_new,
1271};
1272
1273static int msm_pcm_probe(struct platform_device *pdev)
1274{
1275 int rc;
1276 struct msm_plat_data *pdata;
1277 const char *latency_level;
1278 int perf_mode = LOW_LATENCY_PCM_MODE;
1279
1280 dev_dbg(&pdev->dev, "Pull mode driver probe\n");
1281
1282 if (of_property_read_bool(pdev->dev.of_node,
1283 "qcom,msm-pcm-low-latency")) {
1284
1285 rc = of_property_read_string(pdev->dev.of_node,
1286 "qcom,latency-level", &latency_level);
1287 if (!rc) {
1288 if (!strcmp(latency_level, "ultra"))
1289 perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
1290 else if (!strcmp(latency_level, "ull-pp"))
1291 perf_mode = ULL_POST_PROCESSING_PCM_MODE;
1292 }
1293 }
1294
1295 pdata = devm_kzalloc(&pdev->dev,
1296 sizeof(struct msm_plat_data), GFP_KERNEL);
1297 if (!pdata)
1298 return -ENOMEM;
1299
1300 pdata->perf_mode = perf_mode;
1301
1302 dev_set_drvdata(&pdev->dev, pdata);
1303
1304 dev_dbg(&pdev->dev, "%s: dev name %s\n",
1305 __func__, dev_name(&pdev->dev));
1306 dev_dbg(&pdev->dev, "Pull mode driver register\n");
Meng Wangee084a02018-09-04 16:11:58 +08001307 rc = snd_soc_register_component(&pdev->dev,
1308 &msm_soc_component,
1309 NULL, 0);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301310
1311 if (rc)
1312 dev_err(&pdev->dev, "Failed to register pull mode driver\n");
1313
1314 return rc;
1315}
1316
1317static int msm_pcm_remove(struct platform_device *pdev)
1318{
1319 struct msm_plat_data *pdata;
1320
1321 dev_dbg(&pdev->dev, "Pull mode remove\n");
1322 pdata = dev_get_drvdata(&pdev->dev);
1323 devm_kfree(&pdev->dev, pdata);
Meng Wangee084a02018-09-04 16:11:58 +08001324 snd_soc_unregister_component(&pdev->dev);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301325 return 0;
1326}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301327static const struct of_device_id msm_pcm_noirq_dt_match[] = {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301328 {.compatible = "qcom,msm-pcm-dsp-noirq"},
1329 {}
1330};
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301331MODULE_DEVICE_TABLE(of, msm_pcm_noirq_dt_match);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301332
1333static struct platform_driver msm_pcm_driver_noirq = {
1334 .driver = {
1335 .name = "msm-pcm-dsp-noirq",
1336 .owner = THIS_MODULE,
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301337 .of_match_table = msm_pcm_noirq_dt_match,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301338 },
1339 .probe = msm_pcm_probe,
1340 .remove = msm_pcm_remove,
1341};
1342
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301343int __init msm_pcm_noirq_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301344{
1345 return platform_driver_register(&msm_pcm_driver_noirq);
1346}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301347
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05301348void msm_pcm_noirq_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301349{
1350 platform_driver_unregister(&msm_pcm_driver_noirq);
1351}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301352
1353MODULE_DESCRIPTION("PCM NOIRQ module platform driver");
1354MODULE_LICENSE("GPL v2");