blob: 07cf20b6ed820c94c7f509b8c852da555eb927f0 [file] [log] [blame]
Meng Wang43bbb872018-12-10 12:32:05 +08001// SPDX-License-Identifier: GPL-2.0-only
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05302/* Copyright (c) 2013-2019, The Linux Foundation. All rights reserved.
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303 */
4
5#include <linux/init.h>
6#include <linux/err.h>
7#include <linux/module.h>
8#include <linux/platform_device.h>
9#include <linux/slab.h>
10#include <linux/dma-mapping.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053011#include <sound/core.h>
12#include <sound/soc.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053013#include <sound/pcm.h>
14#include <sound/initval.h>
15#include <sound/control.h>
16#include <sound/tlv.h>
17#include <asm/dma.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053018#include <dsp/apr_audio-v2.h>
19#include <dsp/q6audio-v2.h>
20#include <dsp/q6asm-v2.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053021
22#include "msm-pcm-routing-v2.h"
23
Meng Wangee084a02018-09-04 16:11:58 +080024#define DRV_NAME "msm-pcm-loopback-v2"
25
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053026#define LOOPBACK_VOL_MAX_STEPS 0x2000
27#define LOOPBACK_SESSION_MAX 4
28
29static DEFINE_MUTEX(loopback_session_lock);
30static const DECLARE_TLV_DB_LINEAR(loopback_rx_vol_gain, 0,
31 LOOPBACK_VOL_MAX_STEPS);
32
33struct msm_pcm_loopback {
34 struct snd_pcm_substream *playback_substream;
35 struct snd_pcm_substream *capture_substream;
36
37 int instance;
38
39 struct mutex lock;
40
41 uint32_t samp_rate;
42 uint32_t channel_mode;
43
44 int playback_start;
45 int capture_start;
46 int session_id;
47 struct audio_client *audio_client;
48 uint32_t volume;
49};
50
51struct fe_dai_session_map {
52 char stream_name[32];
53 struct msm_pcm_loopback *loopback_priv;
54};
55
56static struct fe_dai_session_map session_map[LOOPBACK_SESSION_MAX] = {
57 { {}, NULL},
58 { {}, NULL},
59 { {}, NULL},
60 { {}, NULL},
61};
62
63static u32 hfp_tx_mute;
64
65struct msm_pcm_pdata {
66 int perf_mode;
Dhananjay Kumar807f7e92018-12-11 18:10:08 +053067 struct snd_pcm *pcm_device[MSM_FRONTEND_DAI_MM_SIZE];
68 struct msm_pcm_channel_mixer *chmixer_pspd[MSM_FRONTEND_DAI_MM_SIZE][2];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053069};
70
71static void stop_pcm(struct msm_pcm_loopback *pcm);
72static int msm_pcm_loopback_get_session(struct snd_soc_pcm_runtime *rtd,
73 struct msm_pcm_loopback **pcm);
74
75static void msm_pcm_route_event_handler(enum msm_pcm_routing_event event,
76 void *priv_data)
77{
78 struct msm_pcm_loopback *pcm = priv_data;
79
80 WARN_ON(!pcm);
81
82 pr_debug("%s: event 0x%x\n", __func__, event);
83
84 switch (event) {
85 case MSM_PCM_RT_EVT_DEVSWITCH:
86 q6asm_cmd(pcm->audio_client, CMD_PAUSE);
87 q6asm_cmd(pcm->audio_client, CMD_FLUSH);
88 q6asm_run(pcm->audio_client, 0, 0, 0);
89 /* fallthrough */
90 default:
91 pr_err("%s: default event 0x%x\n", __func__, event);
92 break;
93 }
94}
95
96static void msm_pcm_loopback_event_handler(uint32_t opcode, uint32_t token,
97 uint32_t *payload, void *priv)
98{
99 pr_debug("%s:\n", __func__);
100 switch (opcode) {
101 case APR_BASIC_RSP_RESULT: {
102 switch (payload[0]) {
103 break;
104 default:
105 break;
106 }
107 }
108 break;
109 default:
110 pr_err("%s: Not Supported Event opcode[0x%x]\n",
111 __func__, opcode);
112 break;
113 }
114}
115
116static int msm_loopback_session_mute_get(struct snd_kcontrol *kcontrol,
117 struct snd_ctl_elem_value *ucontrol)
118{
119 ucontrol->value.integer.value[0] = hfp_tx_mute;
120 return 0;
121}
122
123static int msm_loopback_session_mute_put(struct snd_kcontrol *kcontrol,
124 struct snd_ctl_elem_value *ucontrol)
125{
126 int ret = 0, n = 0;
127 int mute = ucontrol->value.integer.value[0];
128 struct msm_pcm_loopback *pcm = NULL;
129
130 if ((mute < 0) || (mute > 1)) {
131 pr_err(" %s Invalid arguments", __func__);
132 ret = -EINVAL;
133 goto done;
134 }
135
136 pr_debug("%s: mute=%d\n", __func__, mute);
137 hfp_tx_mute = mute;
138 for (n = 0; n < LOOPBACK_SESSION_MAX; n++) {
139 if (!strcmp(session_map[n].stream_name, "MultiMedia6"))
140 pcm = session_map[n].loopback_priv;
141 }
142 if (pcm && pcm->audio_client) {
143 ret = q6asm_set_mute(pcm->audio_client, mute);
144 if (ret < 0)
145 pr_err("%s: Send mute command failed rc=%d\n",
146 __func__, ret);
147 }
148done:
149 return ret;
150}
151
152static struct snd_kcontrol_new msm_loopback_controls[] = {
153 SOC_SINGLE_EXT("HFP TX Mute", SND_SOC_NOPM, 0, 1, 0,
154 msm_loopback_session_mute_get,
155 msm_loopback_session_mute_put),
156};
157
Meng Wangee084a02018-09-04 16:11:58 +0800158static int msm_pcm_loopback_probe(struct snd_soc_component *component)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530159{
Meng Wangee084a02018-09-04 16:11:58 +0800160 snd_soc_add_component_controls(component, msm_loopback_controls,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530161 ARRAY_SIZE(msm_loopback_controls));
162
163 return 0;
164}
165static int pcm_loopback_set_volume(struct msm_pcm_loopback *prtd,
166 uint32_t volume)
167{
168 int rc = -EINVAL;
169
170 pr_debug("%s: Setting volume 0x%x\n", __func__, volume);
171
172 if (prtd && prtd->audio_client) {
173 rc = q6asm_set_volume(prtd->audio_client, volume);
174 if (rc < 0) {
175 pr_err("%s: Send Volume command failed rc = %d\n",
176 __func__, rc);
177 return rc;
178 }
179 prtd->volume = volume;
180 }
181 return rc;
182}
183
184static int msm_pcm_loopback_get_session(struct snd_soc_pcm_runtime *rtd,
185 struct msm_pcm_loopback **pcm)
186{
Meng Wangee084a02018-09-04 16:11:58 +0800187 struct snd_soc_component *component =
188 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530189 int ret = 0;
190 int n, index = -1;
191
Meng Wangee084a02018-09-04 16:11:58 +0800192 if (!component) {
193 pr_err("%s: component is NULL\n", __func__);
194 return -EINVAL;
195 }
196
197 dev_dbg(component->dev, "%s: stream %s\n", __func__,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530198 rtd->dai_link->stream_name);
199
200 mutex_lock(&loopback_session_lock);
201 for (n = 0; n < LOOPBACK_SESSION_MAX; n++) {
202 if (!strcmp(rtd->dai_link->stream_name,
203 session_map[n].stream_name)) {
204 *pcm = session_map[n].loopback_priv;
205 goto exit;
206 }
207 /*
208 * Store the min index value for allocating a new session.
209 * Here, if session stream name is not found in the
210 * existing entries after the loop iteration, then this
211 * index will be used to allocate the new session.
212 * This index variable is expected to point to the topmost
213 * available free session.
214 */
215 if (!(session_map[n].stream_name[0]) && (index < 0))
216 index = n;
217 }
218
219 if (index < 0) {
Meng Wangee084a02018-09-04 16:11:58 +0800220 dev_err(component->dev, "%s: Max Sessions allocated\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530221 __func__);
222 ret = -EAGAIN;
223 goto exit;
224 }
225
226 session_map[index].loopback_priv = kzalloc(
227 sizeof(struct msm_pcm_loopback), GFP_KERNEL);
228 if (!session_map[index].loopback_priv) {
229 ret = -ENOMEM;
230 goto exit;
231 }
232
233 strlcpy(session_map[index].stream_name,
234 rtd->dai_link->stream_name,
235 sizeof(session_map[index].stream_name));
Meng Wangee084a02018-09-04 16:11:58 +0800236 dev_dbg(component->dev, "%s: stream %s index %d\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530237 __func__, session_map[index].stream_name, index);
238
239 mutex_init(&session_map[index].loopback_priv->lock);
240 *pcm = session_map[index].loopback_priv;
241exit:
242 mutex_unlock(&loopback_session_lock);
243 return ret;
244}
245
246static int msm_pcm_open(struct snd_pcm_substream *substream)
247{
248 struct snd_pcm_runtime *runtime = substream->runtime;
249 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
Meng Wangee084a02018-09-04 16:11:58 +0800250 struct snd_soc_component *component =
251 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530252 struct msm_pcm_loopback *pcm = NULL;
253 int ret = 0;
254 uint16_t bits_per_sample = 16;
255 struct msm_pcm_routing_evt event;
256 struct asm_session_mtmx_strtr_param_window_v2_t asm_mtmx_strtr_window;
257 uint32_t param_id;
258 struct msm_pcm_pdata *pdata;
259
Meng Wangee084a02018-09-04 16:11:58 +0800260 if (!component) {
261 pr_err("%s: component is NULL\n", __func__);
262 return -EINVAL;
263 }
264
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530265 ret = msm_pcm_loopback_get_session(rtd, &pcm);
266 if (ret)
267 return ret;
268
269 mutex_lock(&pcm->lock);
270
271 pcm->volume = 0x2000;
272
273 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
274 pcm->playback_substream = substream;
275 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
276 pcm->capture_substream = substream;
277
278 pcm->instance++;
Meng Wangee084a02018-09-04 16:11:58 +0800279 dev_dbg(component->dev, "%s: pcm out open: %d,%d\n", __func__,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530280 pcm->instance, substream->stream);
281 if (pcm->instance == 2) {
282 struct snd_soc_pcm_runtime *soc_pcm_rx =
283 pcm->playback_substream->private_data;
284 struct snd_soc_pcm_runtime *soc_pcm_tx =
285 pcm->capture_substream->private_data;
286 if (pcm->audio_client != NULL)
287 stop_pcm(pcm);
288
289 pdata = (struct msm_pcm_pdata *)
Meng Wangee084a02018-09-04 16:11:58 +0800290 dev_get_drvdata(component->dev);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530291 if (!pdata) {
Meng Wangee084a02018-09-04 16:11:58 +0800292 dev_err(component->dev,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530293 "%s: platform data not populated\n", __func__);
294 mutex_unlock(&pcm->lock);
295 return -EINVAL;
296 }
297
298 pcm->audio_client = q6asm_audio_client_alloc(
299 (app_cb)msm_pcm_loopback_event_handler, pcm);
300 if (!pcm->audio_client) {
Meng Wangee084a02018-09-04 16:11:58 +0800301 dev_err(component->dev,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530302 "%s: Could not allocate memory\n", __func__);
303 mutex_unlock(&pcm->lock);
304 return -ENOMEM;
305 }
306 pcm->session_id = pcm->audio_client->session;
307 pcm->audio_client->perf_mode = pdata->perf_mode;
308 ret = q6asm_open_loopback_v2(pcm->audio_client,
309 bits_per_sample);
310 if (ret < 0) {
Meng Wangee084a02018-09-04 16:11:58 +0800311 dev_err(component->dev,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530312 "%s: pcm out open failed\n", __func__);
313 q6asm_audio_client_free(pcm->audio_client);
314 mutex_unlock(&pcm->lock);
315 return -ENOMEM;
316 }
317 event.event_func = msm_pcm_route_event_handler;
318 event.priv_data = (void *) pcm;
319 msm_pcm_routing_reg_phy_stream(soc_pcm_tx->dai_link->id,
320 pcm->audio_client->perf_mode,
321 pcm->session_id, pcm->capture_substream->stream);
322 msm_pcm_routing_reg_phy_stream_v2(soc_pcm_rx->dai_link->id,
323 pcm->audio_client->perf_mode,
324 pcm->session_id, pcm->playback_substream->stream,
325 event);
326 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
327 pcm->playback_substream = substream;
328 ret = pcm_loopback_set_volume(pcm, pcm->volume);
329 if (ret < 0)
Meng Wangee084a02018-09-04 16:11:58 +0800330 dev_err(component->dev,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530331 "Error %d setting volume", ret);
332 }
333 /* Set to largest negative value */
334 asm_mtmx_strtr_window.window_lsw = 0x00000000;
335 asm_mtmx_strtr_window.window_msw = 0x80000000;
336 param_id = ASM_SESSION_MTMX_STRTR_PARAM_RENDER_WINDOW_START_V2;
337 q6asm_send_mtmx_strtr_window(pcm->audio_client,
338 &asm_mtmx_strtr_window,
339 param_id);
340 /* Set to largest positive value */
341 asm_mtmx_strtr_window.window_lsw = 0xffffffff;
342 asm_mtmx_strtr_window.window_msw = 0x7fffffff;
343 param_id = ASM_SESSION_MTMX_STRTR_PARAM_RENDER_WINDOW_END_V2;
344 q6asm_send_mtmx_strtr_window(pcm->audio_client,
345 &asm_mtmx_strtr_window,
346 param_id);
347 }
Meng Wangee084a02018-09-04 16:11:58 +0800348 dev_info(component->dev, "%s: Instance = %d, Stream ID = %s\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530349 __func__, pcm->instance, substream->pcm->id);
350 runtime->private_data = pcm;
351
352 mutex_unlock(&pcm->lock);
353
354 return 0;
355}
356
357static void stop_pcm(struct msm_pcm_loopback *pcm)
358{
359 struct snd_soc_pcm_runtime *soc_pcm_rx;
360 struct snd_soc_pcm_runtime *soc_pcm_tx;
361
362 if (pcm->audio_client == NULL)
363 return;
364 q6asm_cmd(pcm->audio_client, CMD_CLOSE);
365
366 if (pcm->playback_substream != NULL) {
367 soc_pcm_rx = pcm->playback_substream->private_data;
368 msm_pcm_routing_dereg_phy_stream(soc_pcm_rx->dai_link->id,
369 SNDRV_PCM_STREAM_PLAYBACK);
370 }
371 if (pcm->capture_substream != NULL) {
372 soc_pcm_tx = pcm->capture_substream->private_data;
373 msm_pcm_routing_dereg_phy_stream(soc_pcm_tx->dai_link->id,
374 SNDRV_PCM_STREAM_CAPTURE);
375 }
376 q6asm_audio_client_free(pcm->audio_client);
377 pcm->audio_client = NULL;
378}
379
380static int msm_pcm_close(struct snd_pcm_substream *substream)
381{
382 struct snd_pcm_runtime *runtime = substream->runtime;
383 struct msm_pcm_loopback *pcm = runtime->private_data;
384 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
Meng Wangee084a02018-09-04 16:11:58 +0800385 struct snd_soc_component *component =
386 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530387 int ret = 0, n;
388 bool found = false;
389
Meng Wangee084a02018-09-04 16:11:58 +0800390 if (!component) {
391 pr_err("%s: component is NULL\n", __func__);
392 return -EINVAL;
393 }
394
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530395 mutex_lock(&pcm->lock);
396
Meng Wangee084a02018-09-04 16:11:58 +0800397 dev_dbg(component->dev, "%s: end pcm call:%d\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530398 __func__, substream->stream);
399 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
400 pcm->playback_start = 0;
401 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
402 pcm->capture_start = 0;
403
404 pcm->instance--;
405 if (!pcm->playback_start || !pcm->capture_start) {
Meng Wangee084a02018-09-04 16:11:58 +0800406 dev_dbg(component->dev, "%s: end pcm call\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530407 stop_pcm(pcm);
408 }
409
410 if (!pcm->instance) {
411 mutex_lock(&loopback_session_lock);
412 for (n = 0; n < LOOPBACK_SESSION_MAX; n++) {
413 if (!strcmp(rtd->dai_link->stream_name,
414 session_map[n].stream_name)) {
415 found = true;
416 break;
417 }
418 }
419 if (found) {
420 memset(session_map[n].stream_name, 0,
421 sizeof(session_map[n].stream_name));
422 mutex_unlock(&pcm->lock);
423 mutex_destroy(&session_map[n].loopback_priv->lock);
424 session_map[n].loopback_priv = NULL;
425 kfree(pcm);
Meng Wangee084a02018-09-04 16:11:58 +0800426 dev_dbg(component->dev, "%s: stream freed %s\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530427 __func__, rtd->dai_link->stream_name);
428 mutex_unlock(&loopback_session_lock);
429 return 0;
430 }
431 mutex_unlock(&loopback_session_lock);
432 }
433 mutex_unlock(&pcm->lock);
434 return ret;
435}
436
437static int msm_pcm_prepare(struct snd_pcm_substream *substream)
438{
439 int ret = 0;
440 struct snd_pcm_runtime *runtime = substream->runtime;
441 struct msm_pcm_loopback *pcm = runtime->private_data;
442 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
Meng Wangee084a02018-09-04 16:11:58 +0800443 struct snd_soc_component *component =
444 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
445
446 if (!component) {
447 pr_err("%s: component is NULL\n", __func__);
448 return -EINVAL;
449 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530450
451 mutex_lock(&pcm->lock);
452
Meng Wangee084a02018-09-04 16:11:58 +0800453 dev_dbg(component->dev, "%s: ASM loopback stream:%d\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530454 __func__, substream->stream);
455 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
456 if (!pcm->playback_start)
457 pcm->playback_start = 1;
458 } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
459 if (!pcm->capture_start)
460 pcm->capture_start = 1;
461 }
462 mutex_unlock(&pcm->lock);
463
464 return ret;
465}
466
467static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
468{
469 struct snd_pcm_runtime *runtime = substream->runtime;
470 struct msm_pcm_loopback *pcm = runtime->private_data;
471 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
Meng Wangee084a02018-09-04 16:11:58 +0800472 struct snd_soc_component *component =
473 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
474
475 if (!component) {
476 pr_err("%s: component is NULL\n", __func__);
477 return -EINVAL;
478 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530479
480 switch (cmd) {
481 case SNDRV_PCM_TRIGGER_START:
482 case SNDRV_PCM_TRIGGER_RESUME:
483 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Meng Wangee084a02018-09-04 16:11:58 +0800484 dev_dbg(component->dev,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530485 "%s: playback_start:%d,capture_start:%d\n", __func__,
486 pcm->playback_start, pcm->capture_start);
487 if (pcm->playback_start && pcm->capture_start)
488 q6asm_run_nowait(pcm->audio_client, 0, 0, 0);
489 break;
490 case SNDRV_PCM_TRIGGER_SUSPEND:
491 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
492 case SNDRV_PCM_TRIGGER_STOP:
Meng Wangee084a02018-09-04 16:11:58 +0800493 dev_dbg(component->dev,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530494 "%s:Pause/Stop - playback_start:%d,capture_start:%d\n",
495 __func__, pcm->playback_start, pcm->capture_start);
496 if (pcm->playback_start && pcm->capture_start)
497 q6asm_cmd_nowait(pcm->audio_client, CMD_PAUSE);
498 break;
499 default:
500 pr_err("%s: default cmd %d\n", __func__, cmd);
501 break;
502 }
503
504 return 0;
505}
506
507static const struct snd_pcm_ops msm_pcm_ops = {
508 .open = msm_pcm_open,
509 .close = msm_pcm_close,
510 .prepare = msm_pcm_prepare,
511 .trigger = msm_pcm_trigger,
512};
513
514static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
515 struct snd_ctl_elem_value *ucontrol)
516{
517 int rc = 0;
518 struct snd_pcm_volume *vol = kcontrol->private_data;
519 struct snd_pcm_substream *substream = vol->pcm->streams[0].substream;
520 struct msm_pcm_loopback *prtd;
521 int volume = ucontrol->value.integer.value[0];
522
523 pr_debug("%s: volume : 0x%x\n", __func__, volume);
524 if ((!substream) || (!substream->runtime)) {
525 pr_err("%s substream or runtime not found\n", __func__);
526 rc = -ENODEV;
527 goto exit;
528 }
529 prtd = substream->runtime->private_data;
530 if (!prtd) {
531 rc = -ENODEV;
532 goto exit;
533 }
534 rc = pcm_loopback_set_volume(prtd, volume);
535
536exit:
537 return rc;
538}
539
540static int msm_pcm_volume_ctl_get(struct snd_kcontrol *kcontrol,
541 struct snd_ctl_elem_value *ucontrol)
542{
543 int rc = 0;
544 struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
545 struct snd_pcm_substream *substream =
546 vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
547 struct msm_pcm_loopback *prtd;
548
549 pr_debug("%s\n", __func__);
550 if ((!substream) || (!substream->runtime)) {
Vignesh Kulothungan2ce67842018-09-25 16:40:29 -0700551 pr_debug("%s substream or runtime not found\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530552 rc = -ENODEV;
553 goto exit;
554 }
555 prtd = substream->runtime->private_data;
556 if (!prtd) {
557 rc = -ENODEV;
558 goto exit;
559 }
560 ucontrol->value.integer.value[0] = prtd->volume;
561
562exit:
563 return rc;
564}
565
566static int msm_pcm_add_volume_controls(struct snd_soc_pcm_runtime *rtd)
567{
568 struct snd_pcm *pcm = rtd->pcm->streams[0].pcm;
569 struct snd_pcm_volume *volume_info;
570 struct snd_kcontrol *kctl;
571 int ret = 0;
572
573 dev_dbg(rtd->dev, "%s, Volume cntrl add\n", __func__);
574 ret = snd_pcm_add_volume_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
575 NULL, 1,
576 rtd->dai_link->id,
577 &volume_info);
578 if (ret < 0)
579 return ret;
580 kctl = volume_info->kctl;
581 kctl->put = msm_pcm_volume_ctl_put;
582 kctl->get = msm_pcm_volume_ctl_get;
583 kctl->tlv.p = loopback_rx_vol_gain;
584 return 0;
585}
586
587static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
588 struct snd_ctl_elem_value *ucontrol)
589{
590 u64 fe_id = kcontrol->private_value;
591 int session_type = SESSION_TYPE_RX;
592 int be_id = ucontrol->value.integer.value[3];
593 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
594 int ret = 0;
595
596 cfg_data.app_type = ucontrol->value.integer.value[0];
597 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
598 if (ucontrol->value.integer.value[2] != 0)
599 cfg_data.sample_rate = ucontrol->value.integer.value[2];
600 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
601 __func__, fe_id, session_type, be_id,
602 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
603 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
604 be_id, &cfg_data);
605 if (ret < 0)
606 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
607 __func__, ret);
608
609 return ret;
610}
611
612static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
613 struct snd_ctl_elem_value *ucontrol)
614{
615 u64 fe_id = kcontrol->private_value;
616 int session_type = SESSION_TYPE_RX;
617 int be_id = 0;
618 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
619 int ret = 0;
620
621 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
622 &be_id, &cfg_data);
623 if (ret < 0) {
624 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
625 __func__, ret);
626 goto done;
627 }
628
629 ucontrol->value.integer.value[0] = cfg_data.app_type;
630 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
631 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
632 ucontrol->value.integer.value[3] = be_id;
633 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
634 __func__, fe_id, session_type, be_id,
635 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
636done:
637 return ret;
638}
639
640static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
641 struct snd_ctl_elem_value *ucontrol)
642{
643 u64 fe_id = kcontrol->private_value;
644 int session_type = SESSION_TYPE_TX;
645 int be_id = ucontrol->value.integer.value[3];
646 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
647 int ret = 0;
648
649 cfg_data.app_type = ucontrol->value.integer.value[0];
650 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
651 if (ucontrol->value.integer.value[2] != 0)
652 cfg_data.sample_rate = ucontrol->value.integer.value[2];
653 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
654 __func__, fe_id, session_type, be_id,
655 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
656 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
657 be_id, &cfg_data);
658 if (ret < 0)
659 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
660 __func__, ret);
661
662 return ret;
663}
664
665static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
666 struct snd_ctl_elem_value *ucontrol)
667{
668 u64 fe_id = kcontrol->private_value;
669 int session_type = SESSION_TYPE_TX;
670 int be_id = 0;
671 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
672 int ret = 0;
673
674 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
675 &be_id, &cfg_data);
676 if (ret < 0) {
677 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
678 __func__, ret);
679 goto done;
680 }
681
682 ucontrol->value.integer.value[0] = cfg_data.app_type;
683 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
684 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
685 ucontrol->value.integer.value[3] = be_id;
686 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
687 __func__, fe_id, session_type, be_id,
688 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
689done:
690 return ret;
691}
692
693static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
694{
695 struct snd_pcm *pcm = rtd->pcm->streams[0].pcm;
696 struct snd_pcm_usr *app_type_info;
697 struct snd_kcontrol *kctl;
698 const char *playback_mixer_ctl_name = "Audio Stream";
699 const char *capture_mixer_ctl_name = "Audio Stream Capture";
700 const char *deviceNo = "NN";
701 const char *suffix = "App Type Cfg";
702 int ctl_len, ret = 0;
703
704 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
705 ctl_len = strlen(playback_mixer_ctl_name) + 1 +
706 strlen(deviceNo) + 1 + strlen(suffix) + 1;
707 pr_debug("%s: Playback app type cntrl add\n", __func__);
708 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
709 NULL, 1, ctl_len, rtd->dai_link->id,
710 &app_type_info);
711 if (ret < 0)
712 return ret;
713 kctl = app_type_info->kctl;
714 snprintf(kctl->id.name, ctl_len, "%s %d %s",
715 playback_mixer_ctl_name, rtd->pcm->device, suffix);
716 kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
717 kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
718 }
719
720 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
721 ctl_len = strlen(capture_mixer_ctl_name) + 1 +
722 strlen(deviceNo) + 1 + strlen(suffix) + 1;
723 pr_debug("%s: Capture app type cntrl add\n", __func__);
724 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
725 NULL, 1, ctl_len, rtd->dai_link->id,
726 &app_type_info);
727 if (ret < 0)
728 return ret;
729 kctl = app_type_info->kctl;
730 snprintf(kctl->id.name, ctl_len, "%s %d %s",
731 capture_mixer_ctl_name, rtd->pcm->device, suffix);
732 kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
733 kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
734 }
735
736 return 0;
737}
738
Dhananjay Kumar807f7e92018-12-11 18:10:08 +0530739static struct msm_pcm_channel_mixer *msm_pcm_get_chmixer(
740 struct msm_pcm_pdata *pdata,
741 u64 fe_id, int session_type)
742{
743 if (!pdata) {
744 pr_err("%s: missing pdata\n", __func__);
745 return NULL;
746 }
747
748 if (fe_id >= MSM_FRONTEND_DAI_MM_SIZE) {
749 pr_err("%s: invalid FE %llu\n", __func__, fe_id);
750 return NULL;
751 }
752
753 if ((session_type != SESSION_TYPE_TX) &&
754 (session_type != SESSION_TYPE_RX)) {
755 pr_err("%s: invalid session type %d\n", __func__, session_type);
756 return NULL;
757 }
758
759 return pdata->chmixer_pspd[fe_id][session_type];
760}
761
762static int msm_pcm_channel_mixer_cfg_ctl_put(struct snd_kcontrol *kcontrol,
763 struct snd_ctl_elem_value *ucontrol)
764{
765 u64 fe_id = kcontrol->private_value & 0xFF;
766 int session_type = (kcontrol->private_value >> 8) & 0xFF;
767 int ret = 0;
768 int stream_id = 0;
769 int be_id = 0, i = 0;
770 struct msm_pcm_loopback *prtd = NULL;
771 struct snd_soc_component *component =
772 snd_soc_kcontrol_component(kcontrol);
773 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
774 struct snd_pcm *pcm = NULL;
775 struct snd_pcm_substream *substream = NULL;
776 struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
777 u8 asm_ch_map[PCM_FORMAT_MAX_NUM_CHANNEL_V8] = {0};
778 bool reset_override_out_ch_map = false;
779 bool reset_override_in_ch_map = false;
780
781 pcm = pdata->pcm_device[fe_id];
782 if (!pcm) {
783 pr_err("%s invalid pcm handle for fe_id %llu\n",
784 __func__, fe_id);
785 return -EINVAL;
786 }
787
788 if (session_type == SESSION_TYPE_RX)
789 substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
790 else
791 substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
792 if (!substream) {
793 pr_err("%s substream not found\n", __func__);
794 return -EINVAL;
795 }
796
797 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
798 if (!chmixer_pspd) {
799 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
800 return -EINVAL;
801 }
802
803 chmixer_pspd->enable = ucontrol->value.integer.value[0];
804 chmixer_pspd->rule = ucontrol->value.integer.value[1];
805 chmixer_pspd->input_channel = ucontrol->value.integer.value[2];
806 chmixer_pspd->output_channel = ucontrol->value.integer.value[3];
807 chmixer_pspd->port_idx = ucontrol->value.integer.value[4];
808
809 if (chmixer_pspd->enable) {
810 if (session_type == SESSION_TYPE_RX &&
811 !chmixer_pspd->override_in_ch_map) {
Rohit kumar2a64e522019-02-04 11:22:49 +0530812 if (chmixer_pspd->input_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
813 pr_err("%s: Invalid channel count %d\n",
814 __func__, chmixer_pspd->input_channel);
815 return -EINVAL;
816 }
Dhananjay Kumar807f7e92018-12-11 18:10:08 +0530817 q6asm_map_channels(asm_ch_map,
818 chmixer_pspd->input_channel, false);
819 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
820 chmixer_pspd->in_ch_map[i] = asm_ch_map[i];
821 chmixer_pspd->override_in_ch_map = true;
822 reset_override_in_ch_map = true;
823 } else if (session_type == SESSION_TYPE_TX &&
824 !chmixer_pspd->override_out_ch_map) {
Rohit kumar2a64e522019-02-04 11:22:49 +0530825 if (chmixer_pspd->output_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
826 pr_err("%s: Invalid channel count %d\n",
827 __func__, chmixer_pspd->output_channel);
828 return -EINVAL;
829 }
Dhananjay Kumar807f7e92018-12-11 18:10:08 +0530830 q6asm_map_channels(asm_ch_map,
831 chmixer_pspd->output_channel, false);
832 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
833 chmixer_pspd->out_ch_map[i] = asm_ch_map[i];
834 chmixer_pspd->override_out_ch_map = true;
835 reset_override_out_ch_map = true;
836 }
837 } else {
838 chmixer_pspd->override_out_ch_map = false;
839 chmixer_pspd->override_in_ch_map = false;
840 }
841
842 /* cache value and take effect during adm_open stage */
843 msm_pcm_routing_set_channel_mixer_cfg(fe_id,
844 session_type,
845 chmixer_pspd);
846
847 if (chmixer_pspd->enable && substream->runtime) {
848 prtd = substream->runtime->private_data;
849 if (!prtd) {
850 pr_err("%s find invalid prtd fail\n", __func__);
851 ret = -EINVAL;
852 goto done;
853 }
854
855 if (prtd->audio_client) {
856 stream_id = prtd->audio_client->session;
857 be_id = chmixer_pspd->port_idx;
858 msm_pcm_routing_set_channel_mixer_runtime(be_id,
859 stream_id,
860 session_type,
861 chmixer_pspd);
862 }
863 }
864
865 if (reset_override_out_ch_map)
866 chmixer_pspd->override_out_ch_map = false;
867 if (reset_override_in_ch_map)
868 chmixer_pspd->override_in_ch_map = false;
869
870done:
871 return ret;
872}
873
874static int msm_pcm_channel_mixer_cfg_ctl_get(struct snd_kcontrol *kcontrol,
875 struct snd_ctl_elem_value *ucontrol)
876{
877 u64 fe_id = kcontrol->private_value & 0xFF;
878 int session_type = (kcontrol->private_value >> 8) & 0xFF;
879 struct snd_soc_component *component =
880 snd_soc_kcontrol_component(kcontrol);
881 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
882 struct msm_pcm_channel_mixer *chmixer_pspd;
883
884 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
885 if (!chmixer_pspd) {
886 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
887 return -EINVAL;
888 }
889
890 ucontrol->value.integer.value[0] = chmixer_pspd->enable;
891 ucontrol->value.integer.value[1] = chmixer_pspd->rule;
892 ucontrol->value.integer.value[2] = chmixer_pspd->input_channel;
893 ucontrol->value.integer.value[3] = chmixer_pspd->output_channel;
894 ucontrol->value.integer.value[4] = chmixer_pspd->port_idx;
895 return 0;
896}
897
898static int msm_pcm_channel_mixer_output_map_ctl_put(
899 struct snd_kcontrol *kcontrol,
900 struct snd_ctl_elem_value *ucontrol)
901{
902 u64 fe_id = kcontrol->private_value & 0xFF;
903 int session_type = (kcontrol->private_value >> 8) & 0xFF;
904 int i = 0;
905 struct snd_soc_component *component =
906 snd_soc_kcontrol_component(kcontrol);
907 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
908 struct msm_pcm_channel_mixer *chmixer_pspd;
909
910 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
911 if (!chmixer_pspd) {
912 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
913 return -EINVAL;
914 }
915
916 chmixer_pspd->override_out_ch_map = true;
917 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
918 chmixer_pspd->out_ch_map[i] =
919 ucontrol->value.integer.value[i];
920
921 return 0;
922}
923
924static int msm_pcm_channel_mixer_output_map_ctl_get(
925 struct snd_kcontrol *kcontrol,
926 struct snd_ctl_elem_value *ucontrol)
927{
928 u64 fe_id = kcontrol->private_value & 0xFF;
929 int session_type = (kcontrol->private_value >> 8) & 0xFF;
930 int i = 0;
931 struct snd_soc_component *component =
932 snd_soc_kcontrol_component(kcontrol);
933 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
934 struct msm_pcm_channel_mixer *chmixer_pspd;
935
936 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
937 if (!chmixer_pspd) {
938 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
939 return -EINVAL;
940 }
941
942 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
943 ucontrol->value.integer.value[i] =
944 chmixer_pspd->out_ch_map[i];
945 return 0;
946}
947
948static int msm_pcm_channel_mixer_input_map_ctl_put(
949 struct snd_kcontrol *kcontrol,
950 struct snd_ctl_elem_value *ucontrol)
951{
952 u64 fe_id = kcontrol->private_value & 0xFF;
953 int session_type = (kcontrol->private_value >> 8) & 0xFF;
954 int i = 0;
955 struct snd_soc_component *component =
956 snd_soc_kcontrol_component(kcontrol);
957 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
958 struct msm_pcm_channel_mixer *chmixer_pspd;
959
960 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
961 if (!chmixer_pspd) {
962 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
963 return -EINVAL;
964 }
965
966 chmixer_pspd->override_in_ch_map = true;
967 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
968 chmixer_pspd->in_ch_map[i] = ucontrol->value.integer.value[i];
969
970 return 0;
971}
972
973static int msm_pcm_channel_mixer_input_map_ctl_get(
974 struct snd_kcontrol *kcontrol,
975 struct snd_ctl_elem_value *ucontrol)
976{
977 u64 fe_id = kcontrol->private_value & 0xFF;
978 int session_type = (kcontrol->private_value >> 8) & 0xFF;
979 int i = 0;
980 struct snd_soc_component *component =
981 snd_soc_kcontrol_component(kcontrol);
982 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
983 struct msm_pcm_channel_mixer *chmixer_pspd;
984
985 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
986 if (!chmixer_pspd) {
987 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
988 return -EINVAL;
989 }
990
991 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
992 ucontrol->value.integer.value[i] =
993 chmixer_pspd->in_ch_map[i];
994 return 0;
995}
996
997static int msm_pcm_channel_mixer_weight_ctl_put(
998 struct snd_kcontrol *kcontrol,
999 struct snd_ctl_elem_value *ucontrol)
1000{
1001 u64 fe_id = kcontrol->private_value & 0xFF;
1002 int session_type = (kcontrol->private_value >> 8) & 0xFF;
1003 int channel = (kcontrol->private_value >> 16) & 0xFF;
1004 int i = 0;
1005 struct snd_soc_component *component =
1006 snd_soc_kcontrol_component(kcontrol);
1007 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
1008 struct msm_pcm_channel_mixer *chmixer_pspd;
1009
1010 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
1011 if (!chmixer_pspd) {
1012 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
1013 return -EINVAL;
1014 }
1015
1016 if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
1017 pr_err("%s: invalid channel number %d\n", __func__, channel);
1018 return -EINVAL;
1019 }
1020 channel--;
1021
1022 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
1023 chmixer_pspd->channel_weight[channel][i] =
1024 ucontrol->value.integer.value[i];
1025 return 0;
1026}
1027
1028static int msm_pcm_channel_mixer_weight_ctl_get(
1029 struct snd_kcontrol *kcontrol,
1030 struct snd_ctl_elem_value *ucontrol)
1031{
1032 u64 fe_id = kcontrol->private_value & 0xFF;
1033 int session_type = (kcontrol->private_value >> 8) & 0xFF;
1034 int channel = (kcontrol->private_value >> 16) & 0xFF;
1035 struct snd_soc_component *component =
1036 snd_soc_kcontrol_component(kcontrol);
1037 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
1038 int i = 0;
1039 struct msm_pcm_channel_mixer *chmixer_pspd;
1040
1041 if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
1042 pr_err("%s: invalid channel number %d\n", __func__, channel);
1043 return -EINVAL;
1044 }
1045 channel--;
1046
1047 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
1048 if (!chmixer_pspd) {
1049 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
1050 return -EINVAL;
1051 }
1052
1053 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
1054 ucontrol->value.integer.value[i] =
1055 chmixer_pspd->channel_weight[channel][i];
1056 return 0;
1057}
1058
1059static int msm_pcm_add_platform_controls(struct snd_kcontrol_new *kctl,
1060 struct snd_soc_pcm_runtime *rtd, const char *name_prefix,
1061 const char *name_suffix, int session_type, int channels)
1062{
1063 int ret = -EINVAL;
1064 char *mixer_name = NULL;
1065 struct snd_pcm *pcm = rtd->pcm;
1066 const char *deviceNo = "NN";
1067 const char *channelNo = "NN";
1068 int ctl_len = 0;
1069 struct snd_soc_component *component = NULL;
1070
1071 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
1072 if (!component) {
1073 pr_err("%s: component is NULL\n", __func__);
1074 return -EINVAL;
1075 }
1076
1077 ctl_len = strlen(name_prefix) + 1 + strlen(deviceNo) + 1 +
1078 strlen(channelNo) + 1 + strlen(name_suffix) + 1;
1079
1080 mixer_name = kzalloc(ctl_len, GFP_KERNEL);
1081 if (mixer_name == NULL)
1082 return -ENOMEM;
1083
1084 if (channels >= 0) {
1085 snprintf(mixer_name, ctl_len, "%s %d %s %d",
1086 name_prefix, pcm->device, name_suffix, channels);
1087 kctl->private_value = (rtd->dai_link->id) | (session_type << 8) |
1088 (channels << 16);
1089 } else {
1090 snprintf(mixer_name, ctl_len, "%s %d %s",
1091 name_prefix, pcm->device, name_suffix);
1092 kctl->private_value = (rtd->dai_link->id) | (session_type << 8);
1093 }
1094
1095 kctl->name = mixer_name;
1096 ret = snd_soc_add_component_controls(component, kctl, 1);
1097 kfree(mixer_name);
1098 return ret;
1099}
1100
1101static int msm_pcm_channel_mixer_output_map_info(struct snd_kcontrol *kcontrol,
1102 struct snd_ctl_elem_info *uinfo)
1103{
1104 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1105 uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
1106 /* Valid channel map value ranges from 1 to 64 */
1107 uinfo->value.integer.min = 1;
1108 uinfo->value.integer.max = 64;
1109 return 0;
1110}
1111
1112static int msm_pcm_add_channel_mixer_output_map_controls(
1113 struct snd_soc_pcm_runtime *rtd)
1114{
1115 struct snd_pcm *pcm = rtd->pcm;
1116 const char *playback_mixer_ctl_name = "AudStr";
1117 const char *capture_mixer_ctl_name = "AudStr Capture";
1118 const char *suffix = "ChMixer Output Map";
1119 int session_type = 0, ret = 0, channel = -1;
1120 struct snd_kcontrol_new channel_mixer_output_map_control = {
1121 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1122 .name = "?",
1123 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1124 .info = msm_pcm_channel_mixer_output_map_info,
1125 .put = msm_pcm_channel_mixer_output_map_ctl_put,
1126 .get = msm_pcm_channel_mixer_output_map_ctl_get,
1127 .private_value = 0,
1128 };
1129
1130 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
1131 session_type = SESSION_TYPE_RX;
1132 ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
1133 rtd, playback_mixer_ctl_name, suffix, session_type, channel);
1134 if (ret < 0)
1135 goto fail;
1136 }
1137
1138 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
1139 session_type = SESSION_TYPE_TX;
1140 ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
1141 rtd, capture_mixer_ctl_name, suffix, session_type, channel);
1142 if (ret < 0)
1143 goto fail;
1144 }
1145 return 0;
1146
1147fail:
1148 pr_err("%s: failed add platform ctl, err = %d\n",
1149 __func__, ret);
1150
1151 return ret;
1152}
1153
1154static int msm_pcm_channel_mixer_input_map_info(struct snd_kcontrol *kcontrol,
1155 struct snd_ctl_elem_info *uinfo)
1156{
1157 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1158 uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
1159 /* Valid channel map value ranges from 1 to 64 */
1160 uinfo->value.integer.min = 1;
1161 uinfo->value.integer.max = 64;
1162 return 0;
1163}
1164
1165static int msm_pcm_add_channel_mixer_input_map_controls(
1166 struct snd_soc_pcm_runtime *rtd)
1167{
1168 struct snd_pcm *pcm = rtd->pcm;
1169 const char *playback_mixer_ctl_name = "AudStr";
1170 const char *capture_mixer_ctl_name = "AudStr Capture";
1171 const char *suffix = "ChMixer Input Map";
1172 int session_type = 0, ret = 0, channel = -1;
1173 struct snd_kcontrol_new channel_mixer_input_map_control = {
1174 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1175 .name = "?",
1176 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1177 .info = msm_pcm_channel_mixer_input_map_info,
1178 .put = msm_pcm_channel_mixer_input_map_ctl_put,
1179 .get = msm_pcm_channel_mixer_input_map_ctl_get,
1180 .private_value = 0,
1181 };
1182
1183 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
1184 session_type = SESSION_TYPE_RX;
1185 ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
1186 rtd, playback_mixer_ctl_name, suffix, session_type, channel);
1187 if (ret < 0)
1188 goto fail;
1189 }
1190
1191 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
1192 session_type = SESSION_TYPE_TX;
1193 ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
1194 rtd, capture_mixer_ctl_name, suffix, session_type, channel);
1195 if (ret < 0)
1196 goto fail;
1197 }
1198 return 0;
1199
1200fail:
1201 pr_err("%s: failed add platform ctl, err = %d\n",
1202 __func__, ret);
1203 return ret;
1204}
1205
1206static int msm_pcm_channel_mixer_cfg_info(struct snd_kcontrol *kcontrol,
1207 struct snd_ctl_elem_info *uinfo)
1208{
1209 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1210 /* five int values: enable, rule, in_channels, out_channels and port_id */
1211 uinfo->count = 5;
1212 /* Valid range is all positive values to support above controls */
1213 uinfo->value.integer.min = 0;
1214 uinfo->value.integer.max = INT_MAX;
1215 return 0;
1216}
1217
1218static int msm_pcm_add_channel_mixer_cfg_controls(
1219 struct snd_soc_pcm_runtime *rtd)
1220{
1221 struct snd_pcm *pcm = rtd->pcm;
1222 const char *playback_mixer_ctl_name = "AudStr";
1223 const char *capture_mixer_ctl_name = "AudStr Capture";
1224 const char *suffix = "ChMixer Cfg";
1225 int session_type = 0, ret = 0, channel = -1;
1226 struct msm_pcm_pdata *pdata = NULL;
1227 struct snd_soc_component *component = NULL;
1228 struct snd_kcontrol_new channel_mixer_cfg_control = {
1229 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1230 .name = "?",
1231 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1232 .info = msm_pcm_channel_mixer_cfg_info,
1233 .put = msm_pcm_channel_mixer_cfg_ctl_put,
1234 .get = msm_pcm_channel_mixer_cfg_ctl_get,
1235 .private_value = 0,
1236 };
1237
1238 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
1239 if (!component) {
1240 pr_err("%s: component is NULL\n", __func__);
1241 return -EINVAL;
1242 }
1243 pdata = (struct msm_pcm_pdata *)
1244 dev_get_drvdata(component->dev);
1245 if (pdata == NULL) {
1246 pr_err("%s: platform data not populated\n", __func__);
1247 return -EINVAL;
1248 }
1249
1250 pdata->pcm_device[rtd->dai_link->id] = rtd->pcm;
1251
1252 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
1253 session_type = SESSION_TYPE_RX;
1254 ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
1255 rtd, playback_mixer_ctl_name, suffix, session_type, channel);
1256 if (ret < 0)
1257 goto fail;
1258 }
1259
1260 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
1261 session_type = SESSION_TYPE_TX;
1262 ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
1263 rtd, capture_mixer_ctl_name, suffix, session_type, channel);
1264 if (ret < 0)
1265 goto fail;
1266 }
1267 return 0;
1268
1269fail:
1270 pr_err("%s: failed add platform ctl, err = %d\n",
1271 __func__, ret);
1272
1273 return ret;
1274}
1275
1276static int msm_pcm_channel_mixer_weight_info(struct snd_kcontrol *kcontrol,
1277 struct snd_ctl_elem_info *uinfo)
1278{
1279 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1280 uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
1281 /* Valid range: 0 to 0x4000(Unity) gain weightage */
1282 uinfo->value.integer.min = 0;
1283 uinfo->value.integer.max = 0x4000;
1284 return 0;
1285}
1286
1287static int msm_pcm_add_channel_mixer_weight_controls(
1288 struct snd_soc_pcm_runtime *rtd,
1289 int channel)
1290{
1291 struct snd_pcm *pcm = rtd->pcm;
1292 const char *playback_mixer_ctl_name = "AudStr";
1293 const char *capture_mixer_ctl_name = "AudStr Capture";
1294 const char *suffix = "ChMixer Weight Ch";
1295 int session_type = 0, ret = 0;
1296 struct snd_kcontrol_new channel_mixer_weight_control = {
1297 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1298 .name = "?",
1299 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1300 .info = msm_pcm_channel_mixer_weight_info,
1301 .put = msm_pcm_channel_mixer_weight_ctl_put,
1302 .get = msm_pcm_channel_mixer_weight_ctl_get,
1303 .private_value = 0,
1304 };
1305
1306 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
1307 session_type = SESSION_TYPE_RX;
1308 ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
1309 rtd, playback_mixer_ctl_name, suffix, session_type, channel);
1310 if (ret < 0)
1311 goto fail;
1312 }
1313
1314 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
1315 session_type = SESSION_TYPE_TX;
1316 ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
1317 rtd, capture_mixer_ctl_name, suffix, session_type, channel);
1318 if (ret < 0)
1319 goto fail;
1320 }
1321 return 0;
1322
1323fail:
1324 pr_err("%s: failed add platform ctl, err = %d\n",
1325 __func__, ret);
1326
1327 return ret;
1328}
1329
1330static int msm_pcm_add_channel_mixer_controls(struct snd_soc_pcm_runtime *rtd)
1331{
1332 int i, ret = 0;
1333 struct snd_pcm *pcm = NULL;
1334 struct msm_pcm_pdata *pdata = NULL;
1335 struct snd_soc_component *component = NULL;
1336
1337 if (!rtd || !rtd->pcm) {
1338 pr_err("%s invalid rtd or pcm\n", __func__);
1339 return -EINVAL;
1340 }
1341 pcm = rtd->pcm;
1342
1343 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
1344 if (!component) {
1345 pr_err("%s: component is NULL\n", __func__);
1346 return -EINVAL;
1347 }
1348
1349 pdata = (struct msm_pcm_pdata *)
1350 dev_get_drvdata(component->dev);
1351 if (!pdata) {
1352 pr_err("%s: platform data not populated\n", __func__);
1353 return -EINVAL;
1354 }
1355
1356 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream &&
1357 !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
1358 pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] =
1359 kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
1360 if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
1361 ret = -ENOMEM;
1362 goto fail;
1363 }
1364 }
1365
1366 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream &&
1367 !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
1368 pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] =
1369 kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
1370 if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
1371 ret = -ENOMEM;
1372 goto fail;
1373 }
1374 }
1375
1376 ret = msm_pcm_add_channel_mixer_cfg_controls(rtd);
1377 if (ret) {
1378 pr_err("%s: pcm add channel mixer cfg controls failed:%d\n",
1379 __func__, ret);
1380 goto fail;
1381 }
1382 ret = msm_pcm_add_channel_mixer_input_map_controls(rtd);
1383 if (ret) {
1384 pr_err("%s: pcm add channel mixer input map controls failed:%d\n",
1385 __func__, ret);
1386 goto fail;
1387 }
1388 ret = msm_pcm_add_channel_mixer_output_map_controls(rtd);
1389 if (ret) {
1390 pr_err("%s: pcm add channel mixer output map controls failed:%d\n",
1391 __func__, ret);
1392 goto fail;
1393 }
1394
1395 for (i = 1; i <= PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++) {
1396 ret = msm_pcm_add_channel_mixer_weight_controls(rtd, i);
1397 if (ret) {
1398 pr_err("%s: pcm add channel mixer weight controls failed:%d\n",
1399 __func__, ret);
1400 goto fail;
1401 }
1402 }
1403 return 0;
1404
1405fail:
1406 kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]);
1407 kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]);
1408 pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] = NULL;
1409 pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] = NULL;
1410
1411 return ret;
1412}
1413
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301414static int msm_pcm_add_controls(struct snd_soc_pcm_runtime *rtd)
1415{
1416 int ret = 0;
1417
1418 pr_debug("%s\n", __func__);
1419 ret = msm_pcm_add_volume_controls(rtd);
1420 if (ret)
1421 pr_err("%s: pcm add volume controls failed:%d\n",
1422 __func__, ret);
1423 ret = msm_pcm_add_app_type_controls(rtd);
1424 if (ret)
1425 pr_err("%s: pcm add app type controls failed:%d\n",
1426 __func__, ret);
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05301427
1428 ret = msm_pcm_add_channel_mixer_controls(rtd);
1429 if (ret)
1430 pr_err("%s: pcm add channel mixer controls failed:%d\n",
1431 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301432 return ret;
1433}
1434
1435static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
1436{
1437 struct snd_card *card = rtd->card->snd_card;
1438 int ret = 0;
1439
1440 if (!card->dev->coherent_dma_mask)
1441 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
1442
1443 ret = msm_pcm_add_controls(rtd);
1444 if (ret)
1445 dev_err(rtd->dev, "%s, kctl add failed\n", __func__);
1446 return ret;
1447}
1448
Meng Wangee084a02018-09-04 16:11:58 +08001449static struct snd_soc_component_driver msm_soc_component = {
1450 .name = DRV_NAME,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301451 .ops = &msm_pcm_ops,
1452 .pcm_new = msm_asoc_pcm_new,
1453 .probe = msm_pcm_loopback_probe,
1454};
1455
1456static int msm_pcm_probe(struct platform_device *pdev)
1457{
1458 struct msm_pcm_pdata *pdata;
1459
1460 dev_dbg(&pdev->dev, "%s: dev name %s\n",
1461 __func__, dev_name(&pdev->dev));
1462
1463 pdata = kzalloc(sizeof(struct msm_pcm_pdata), GFP_KERNEL);
1464 if (!pdata)
1465 return -ENOMEM;
1466
1467 if (of_property_read_bool(pdev->dev.of_node,
1468 "qcom,msm-pcm-loopback-low-latency"))
1469 pdata->perf_mode = LOW_LATENCY_PCM_MODE;
1470 else
1471 pdata->perf_mode = LEGACY_PCM_MODE;
1472
1473 dev_set_drvdata(&pdev->dev, pdata);
1474
Meng Wangee084a02018-09-04 16:11:58 +08001475 return snd_soc_register_component(&pdev->dev,
1476 &msm_soc_component,
1477 NULL, 0);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301478}
1479
1480static int msm_pcm_remove(struct platform_device *pdev)
1481{
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05301482 struct msm_pcm_pdata *pdata;
1483 int i = 0;
1484
1485 pdata = dev_get_drvdata(&pdev->dev);
1486 if (pdata) {
1487 for (i = 0; i < MSM_FRONTEND_DAI_MM_SIZE; i++) {
1488 kfree(pdata->chmixer_pspd[i][SESSION_TYPE_RX]);
1489 kfree(pdata->chmixer_pspd[i][SESSION_TYPE_TX]);
1490 }
1491 }
1492 kfree(pdata);
Meng Wangee084a02018-09-04 16:11:58 +08001493 snd_soc_unregister_component(&pdev->dev);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301494 return 0;
1495}
1496
1497static const struct of_device_id msm_pcm_loopback_dt_match[] = {
1498 {.compatible = "qcom,msm-pcm-loopback"},
1499 {}
1500};
1501
1502static struct platform_driver msm_pcm_driver = {
1503 .driver = {
1504 .name = "msm-pcm-loopback",
1505 .owner = THIS_MODULE,
1506 .of_match_table = msm_pcm_loopback_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08001507 .suppress_bind_attrs = true,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301508 },
1509 .probe = msm_pcm_probe,
1510 .remove = msm_pcm_remove,
1511};
1512
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301513int __init msm_pcm_loopback_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301514{
1515 return platform_driver_register(&msm_pcm_driver);
1516}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301517
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05301518void msm_pcm_loopback_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301519{
1520 platform_driver_unregister(&msm_pcm_driver);
1521}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301522
1523MODULE_DESCRIPTION("PCM loopback platform driver");
1524MODULE_LICENSE("GPL v2");