blob: ffde93b7fad633370756ce1e021d8b6d316089ed [file] [log] [blame]
Meng Wang43bbb872018-12-10 12:32:05 +08001// SPDX-License-Identifier: GPL-2.0-only
Laxminath Kasamd08ab1f2020-01-30 19:07:58 +05302/* Copyright (c) 2013-2020, The Linux Foundation. All rights reserved.
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303 */
4
5#include <linux/init.h>
6#include <linux/err.h>
7#include <linux/module.h>
8#include <linux/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 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530135 mutex_lock(&loopback_session_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530136 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 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530148 mutex_unlock(&loopback_session_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530149done:
150 return ret;
151}
152
153static struct snd_kcontrol_new msm_loopback_controls[] = {
154 SOC_SINGLE_EXT("HFP TX Mute", SND_SOC_NOPM, 0, 1, 0,
155 msm_loopback_session_mute_get,
156 msm_loopback_session_mute_put),
157};
158
Meng Wangee084a02018-09-04 16:11:58 +0800159static int msm_pcm_loopback_probe(struct snd_soc_component *component)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530160{
Meng Wangee084a02018-09-04 16:11:58 +0800161 snd_soc_add_component_controls(component, msm_loopback_controls,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530162 ARRAY_SIZE(msm_loopback_controls));
163
164 return 0;
165}
166static int pcm_loopback_set_volume(struct msm_pcm_loopback *prtd,
167 uint32_t volume)
168{
169 int rc = -EINVAL;
170
171 pr_debug("%s: Setting volume 0x%x\n", __func__, volume);
172
173 if (prtd && prtd->audio_client) {
174 rc = q6asm_set_volume(prtd->audio_client, volume);
175 if (rc < 0) {
176 pr_err("%s: Send Volume command failed rc = %d\n",
177 __func__, rc);
178 return rc;
179 }
180 prtd->volume = volume;
181 }
182 return rc;
183}
184
185static int msm_pcm_loopback_get_session(struct snd_soc_pcm_runtime *rtd,
186 struct msm_pcm_loopback **pcm)
187{
Meng Wangee084a02018-09-04 16:11:58 +0800188 struct snd_soc_component *component =
189 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530190 int ret = 0;
191 int n, index = -1;
192
Meng Wangee084a02018-09-04 16:11:58 +0800193 if (!component) {
194 pr_err("%s: component is NULL\n", __func__);
195 return -EINVAL;
196 }
197
198 dev_dbg(component->dev, "%s: stream %s\n", __func__,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530199 rtd->dai_link->stream_name);
200
201 mutex_lock(&loopback_session_lock);
202 for (n = 0; n < LOOPBACK_SESSION_MAX; n++) {
203 if (!strcmp(rtd->dai_link->stream_name,
204 session_map[n].stream_name)) {
205 *pcm = session_map[n].loopback_priv;
206 goto exit;
207 }
208 /*
209 * Store the min index value for allocating a new session.
210 * Here, if session stream name is not found in the
211 * existing entries after the loop iteration, then this
212 * index will be used to allocate the new session.
213 * This index variable is expected to point to the topmost
214 * available free session.
215 */
216 if (!(session_map[n].stream_name[0]) && (index < 0))
217 index = n;
218 }
219
220 if (index < 0) {
Meng Wangee084a02018-09-04 16:11:58 +0800221 dev_err(component->dev, "%s: Max Sessions allocated\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530222 __func__);
223 ret = -EAGAIN;
224 goto exit;
225 }
226
227 session_map[index].loopback_priv = kzalloc(
228 sizeof(struct msm_pcm_loopback), GFP_KERNEL);
229 if (!session_map[index].loopback_priv) {
230 ret = -ENOMEM;
231 goto exit;
232 }
233
234 strlcpy(session_map[index].stream_name,
235 rtd->dai_link->stream_name,
236 sizeof(session_map[index].stream_name));
Meng Wangee084a02018-09-04 16:11:58 +0800237 dev_dbg(component->dev, "%s: stream %s index %d\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530238 __func__, session_map[index].stream_name, index);
239
240 mutex_init(&session_map[index].loopback_priv->lock);
241 *pcm = session_map[index].loopback_priv;
242exit:
243 mutex_unlock(&loopback_session_lock);
244 return ret;
245}
246
247static int msm_pcm_open(struct snd_pcm_substream *substream)
248{
249 struct snd_pcm_runtime *runtime = substream->runtime;
250 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
Meng Wangee084a02018-09-04 16:11:58 +0800251 struct snd_soc_component *component =
252 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530253 struct msm_pcm_loopback *pcm = NULL;
254 int ret = 0;
255 uint16_t bits_per_sample = 16;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530256 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) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530282 if (pcm->audio_client != NULL)
283 stop_pcm(pcm);
284
285 pdata = (struct msm_pcm_pdata *)
Meng Wangee084a02018-09-04 16:11:58 +0800286 dev_get_drvdata(component->dev);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530287 if (!pdata) {
Meng Wangee084a02018-09-04 16:11:58 +0800288 dev_err(component->dev,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530289 "%s: platform data not populated\n", __func__);
290 mutex_unlock(&pcm->lock);
291 return -EINVAL;
292 }
293
294 pcm->audio_client = q6asm_audio_client_alloc(
295 (app_cb)msm_pcm_loopback_event_handler, pcm);
296 if (!pcm->audio_client) {
Meng Wangee084a02018-09-04 16:11:58 +0800297 dev_err(component->dev,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530298 "%s: Could not allocate memory\n", __func__);
299 mutex_unlock(&pcm->lock);
300 return -ENOMEM;
301 }
302 pcm->session_id = pcm->audio_client->session;
303 pcm->audio_client->perf_mode = pdata->perf_mode;
304 ret = q6asm_open_loopback_v2(pcm->audio_client,
305 bits_per_sample);
306 if (ret < 0) {
Meng Wangee084a02018-09-04 16:11:58 +0800307 dev_err(component->dev,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530308 "%s: pcm out open failed\n", __func__);
309 q6asm_audio_client_free(pcm->audio_client);
310 mutex_unlock(&pcm->lock);
311 return -ENOMEM;
312 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530313 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
314 pcm->playback_substream = substream;
315 ret = pcm_loopback_set_volume(pcm, pcm->volume);
316 if (ret < 0)
Meng Wangee084a02018-09-04 16:11:58 +0800317 dev_err(component->dev,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530318 "Error %d setting volume", ret);
319 }
320 /* Set to largest negative value */
321 asm_mtmx_strtr_window.window_lsw = 0x00000000;
322 asm_mtmx_strtr_window.window_msw = 0x80000000;
323 param_id = ASM_SESSION_MTMX_STRTR_PARAM_RENDER_WINDOW_START_V2;
324 q6asm_send_mtmx_strtr_window(pcm->audio_client,
325 &asm_mtmx_strtr_window,
326 param_id);
327 /* Set to largest positive value */
328 asm_mtmx_strtr_window.window_lsw = 0xffffffff;
329 asm_mtmx_strtr_window.window_msw = 0x7fffffff;
330 param_id = ASM_SESSION_MTMX_STRTR_PARAM_RENDER_WINDOW_END_V2;
331 q6asm_send_mtmx_strtr_window(pcm->audio_client,
332 &asm_mtmx_strtr_window,
333 param_id);
334 }
Meng Wangee084a02018-09-04 16:11:58 +0800335 dev_info(component->dev, "%s: Instance = %d, Stream ID = %s\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530336 __func__, pcm->instance, substream->pcm->id);
337 runtime->private_data = pcm;
338
339 mutex_unlock(&pcm->lock);
340
341 return 0;
342}
343
344static void stop_pcm(struct msm_pcm_loopback *pcm)
345{
346 struct snd_soc_pcm_runtime *soc_pcm_rx;
347 struct snd_soc_pcm_runtime *soc_pcm_tx;
348
349 if (pcm->audio_client == NULL)
350 return;
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530351
352 mutex_lock(&loopback_session_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530353 q6asm_cmd(pcm->audio_client, CMD_CLOSE);
354
355 if (pcm->playback_substream != NULL) {
356 soc_pcm_rx = pcm->playback_substream->private_data;
357 msm_pcm_routing_dereg_phy_stream(soc_pcm_rx->dai_link->id,
358 SNDRV_PCM_STREAM_PLAYBACK);
359 }
360 if (pcm->capture_substream != NULL) {
361 soc_pcm_tx = pcm->capture_substream->private_data;
362 msm_pcm_routing_dereg_phy_stream(soc_pcm_tx->dai_link->id,
363 SNDRV_PCM_STREAM_CAPTURE);
364 }
365 q6asm_audio_client_free(pcm->audio_client);
366 pcm->audio_client = NULL;
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530367 mutex_unlock(&loopback_session_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530368}
369
370static int msm_pcm_close(struct snd_pcm_substream *substream)
371{
372 struct snd_pcm_runtime *runtime = substream->runtime;
373 struct msm_pcm_loopback *pcm = runtime->private_data;
374 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
Meng Wangee084a02018-09-04 16:11:58 +0800375 struct snd_soc_component *component =
376 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530377 int ret = 0, n;
378 bool found = false;
379
Meng Wangee084a02018-09-04 16:11:58 +0800380 if (!component) {
381 pr_err("%s: component is NULL\n", __func__);
382 return -EINVAL;
383 }
384
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530385 mutex_lock(&pcm->lock);
386
Meng Wangee084a02018-09-04 16:11:58 +0800387 dev_dbg(component->dev, "%s: end pcm call:%d\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530388 __func__, substream->stream);
389 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
390 pcm->playback_start = 0;
391 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
392 pcm->capture_start = 0;
393
394 pcm->instance--;
395 if (!pcm->playback_start || !pcm->capture_start) {
Meng Wangee084a02018-09-04 16:11:58 +0800396 dev_dbg(component->dev, "%s: end pcm call\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530397 stop_pcm(pcm);
398 }
399
400 if (!pcm->instance) {
401 mutex_lock(&loopback_session_lock);
402 for (n = 0; n < LOOPBACK_SESSION_MAX; n++) {
403 if (!strcmp(rtd->dai_link->stream_name,
404 session_map[n].stream_name)) {
405 found = true;
406 break;
407 }
408 }
409 if (found) {
410 memset(session_map[n].stream_name, 0,
411 sizeof(session_map[n].stream_name));
412 mutex_unlock(&pcm->lock);
413 mutex_destroy(&session_map[n].loopback_priv->lock);
414 session_map[n].loopback_priv = NULL;
415 kfree(pcm);
Meng Wangee084a02018-09-04 16:11:58 +0800416 dev_dbg(component->dev, "%s: stream freed %s\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530417 __func__, rtd->dai_link->stream_name);
418 mutex_unlock(&loopback_session_lock);
419 return 0;
420 }
421 mutex_unlock(&loopback_session_lock);
422 }
423 mutex_unlock(&pcm->lock);
424 return ret;
425}
426
427static int msm_pcm_prepare(struct snd_pcm_substream *substream)
428{
429 int ret = 0;
430 struct snd_pcm_runtime *runtime = substream->runtime;
431 struct msm_pcm_loopback *pcm = runtime->private_data;
432 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
Meng Wangee084a02018-09-04 16:11:58 +0800433 struct snd_soc_component *component =
434 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
Cong Tangd083a832019-08-27 16:21:06 +0530435 struct msm_pcm_routing_evt event;
Meng Wangee084a02018-09-04 16:11:58 +0800436
Cong Tangd083a832019-08-27 16:21:06 +0530437 memset(&event, 0, sizeof(event));
Meng Wangee084a02018-09-04 16:11:58 +0800438 if (!component) {
439 pr_err("%s: component is NULL\n", __func__);
440 return -EINVAL;
441 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530442
443 mutex_lock(&pcm->lock);
444
Meng Wangee084a02018-09-04 16:11:58 +0800445 dev_dbg(component->dev, "%s: ASM loopback stream:%d\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530446 __func__, substream->stream);
Cong Tangd083a832019-08-27 16:21:06 +0530447
448 if (pcm->playback_start && pcm->capture_start) {
449 mutex_unlock(&pcm->lock);
450 return ret;
451 }
452
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530453 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
454 if (!pcm->playback_start)
455 pcm->playback_start = 1;
456 } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
457 if (!pcm->capture_start)
458 pcm->capture_start = 1;
459 }
Cong Tangd083a832019-08-27 16:21:06 +0530460
461 if (pcm->playback_start && pcm->capture_start) {
462 struct snd_soc_pcm_runtime *soc_pcm_rx =
463 pcm->playback_substream->private_data;
464 struct snd_soc_pcm_runtime *soc_pcm_tx =
465 pcm->capture_substream->private_data;
466 event.event_func = msm_pcm_route_event_handler;
467 event.priv_data = (void *) pcm;
468 msm_pcm_routing_reg_phy_stream(soc_pcm_tx->dai_link->id,
469 pcm->audio_client->perf_mode,
470 pcm->session_id, pcm->capture_substream->stream);
471 msm_pcm_routing_reg_phy_stream_v2(soc_pcm_rx->dai_link->id,
472 pcm->audio_client->perf_mode,
473 pcm->session_id, pcm->playback_substream->stream,
474 event);
475 }
476
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530477 mutex_unlock(&pcm->lock);
478
479 return ret;
480}
481
482static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
483{
484 struct snd_pcm_runtime *runtime = substream->runtime;
485 struct msm_pcm_loopback *pcm = runtime->private_data;
486 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
Meng Wangee084a02018-09-04 16:11:58 +0800487 struct snd_soc_component *component =
488 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
489
490 if (!component) {
491 pr_err("%s: component is NULL\n", __func__);
492 return -EINVAL;
493 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530494
495 switch (cmd) {
496 case SNDRV_PCM_TRIGGER_START:
497 case SNDRV_PCM_TRIGGER_RESUME:
498 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Meng Wangee084a02018-09-04 16:11:58 +0800499 dev_dbg(component->dev,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530500 "%s: playback_start:%d,capture_start:%d\n", __func__,
501 pcm->playback_start, pcm->capture_start);
502 if (pcm->playback_start && pcm->capture_start)
503 q6asm_run_nowait(pcm->audio_client, 0, 0, 0);
504 break;
505 case SNDRV_PCM_TRIGGER_SUSPEND:
506 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
507 case SNDRV_PCM_TRIGGER_STOP:
Meng Wangee084a02018-09-04 16:11:58 +0800508 dev_dbg(component->dev,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530509 "%s:Pause/Stop - playback_start:%d,capture_start:%d\n",
510 __func__, pcm->playback_start, pcm->capture_start);
511 if (pcm->playback_start && pcm->capture_start)
512 q6asm_cmd_nowait(pcm->audio_client, CMD_PAUSE);
513 break;
514 default:
515 pr_err("%s: default cmd %d\n", __func__, cmd);
516 break;
517 }
518
519 return 0;
520}
521
522static const struct snd_pcm_ops msm_pcm_ops = {
523 .open = msm_pcm_open,
524 .close = msm_pcm_close,
525 .prepare = msm_pcm_prepare,
526 .trigger = msm_pcm_trigger,
527};
528
529static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
530 struct snd_ctl_elem_value *ucontrol)
531{
532 int rc = 0;
533 struct snd_pcm_volume *vol = kcontrol->private_data;
534 struct snd_pcm_substream *substream = vol->pcm->streams[0].substream;
535 struct msm_pcm_loopback *prtd;
536 int volume = ucontrol->value.integer.value[0];
537
538 pr_debug("%s: volume : 0x%x\n", __func__, volume);
539 if ((!substream) || (!substream->runtime)) {
540 pr_err("%s substream or runtime not found\n", __func__);
541 rc = -ENODEV;
542 goto exit;
543 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530544 mutex_lock(&loopback_session_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530545 prtd = substream->runtime->private_data;
546 if (!prtd) {
547 rc = -ENODEV;
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530548 mutex_unlock(&loopback_session_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530549 goto exit;
550 }
551 rc = pcm_loopback_set_volume(prtd, volume);
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530552 mutex_unlock(&loopback_session_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530553exit:
554 return rc;
555}
556
557static int msm_pcm_volume_ctl_get(struct snd_kcontrol *kcontrol,
558 struct snd_ctl_elem_value *ucontrol)
559{
560 int rc = 0;
561 struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
Laxminath Kasamd08ab1f2020-01-30 19:07:58 +0530562 struct snd_pcm_substream *substream = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530563 struct msm_pcm_loopback *prtd;
564
565 pr_debug("%s\n", __func__);
Laxminath Kasamd08ab1f2020-01-30 19:07:58 +0530566 if (!vol) {
567 pr_err("%s: vol is NULL\n", __func__);
568 return -ENODEV;
569 }
570 substream = vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530571 if ((!substream) || (!substream->runtime)) {
Vignesh Kulothungan2ce67842018-09-25 16:40:29 -0700572 pr_debug("%s substream or runtime not found\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530573 rc = -ENODEV;
574 goto exit;
575 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530576 mutex_lock(&loopback_session_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530577 prtd = substream->runtime->private_data;
578 if (!prtd) {
579 rc = -ENODEV;
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530580 mutex_unlock(&loopback_session_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530581 goto exit;
582 }
583 ucontrol->value.integer.value[0] = prtd->volume;
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530584 mutex_unlock(&loopback_session_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530585exit:
586 return rc;
587}
588
589static int msm_pcm_add_volume_controls(struct snd_soc_pcm_runtime *rtd)
590{
591 struct snd_pcm *pcm = rtd->pcm->streams[0].pcm;
592 struct snd_pcm_volume *volume_info;
593 struct snd_kcontrol *kctl;
594 int ret = 0;
595
596 dev_dbg(rtd->dev, "%s, Volume cntrl add\n", __func__);
597 ret = snd_pcm_add_volume_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
598 NULL, 1,
599 rtd->dai_link->id,
600 &volume_info);
601 if (ret < 0)
602 return ret;
603 kctl = volume_info->kctl;
604 kctl->put = msm_pcm_volume_ctl_put;
605 kctl->get = msm_pcm_volume_ctl_get;
606 kctl->tlv.p = loopback_rx_vol_gain;
607 return 0;
608}
609
610static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
611 struct snd_ctl_elem_value *ucontrol)
612{
613 u64 fe_id = kcontrol->private_value;
614 int session_type = SESSION_TYPE_RX;
615 int be_id = ucontrol->value.integer.value[3];
616 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
617 int ret = 0;
618
619 cfg_data.app_type = ucontrol->value.integer.value[0];
620 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
621 if (ucontrol->value.integer.value[2] != 0)
622 cfg_data.sample_rate = ucontrol->value.integer.value[2];
623 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
624 __func__, fe_id, session_type, be_id,
625 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
626 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
627 be_id, &cfg_data);
628 if (ret < 0)
629 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
630 __func__, ret);
631
632 return ret;
633}
634
635static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
636 struct snd_ctl_elem_value *ucontrol)
637{
638 u64 fe_id = kcontrol->private_value;
639 int session_type = SESSION_TYPE_RX;
640 int be_id = 0;
641 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
642 int ret = 0;
643
644 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
645 &be_id, &cfg_data);
646 if (ret < 0) {
647 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
648 __func__, ret);
649 goto done;
650 }
651
652 ucontrol->value.integer.value[0] = cfg_data.app_type;
653 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
654 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
655 ucontrol->value.integer.value[3] = be_id;
656 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
657 __func__, fe_id, session_type, be_id,
658 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
659done:
660 return ret;
661}
662
663static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
664 struct snd_ctl_elem_value *ucontrol)
665{
666 u64 fe_id = kcontrol->private_value;
667 int session_type = SESSION_TYPE_TX;
668 int be_id = ucontrol->value.integer.value[3];
669 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
670 int ret = 0;
671
672 cfg_data.app_type = ucontrol->value.integer.value[0];
673 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
674 if (ucontrol->value.integer.value[2] != 0)
675 cfg_data.sample_rate = ucontrol->value.integer.value[2];
676 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
677 __func__, fe_id, session_type, be_id,
678 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
679 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
680 be_id, &cfg_data);
681 if (ret < 0)
682 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
683 __func__, ret);
684
685 return ret;
686}
687
688static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
689 struct snd_ctl_elem_value *ucontrol)
690{
691 u64 fe_id = kcontrol->private_value;
692 int session_type = SESSION_TYPE_TX;
693 int be_id = 0;
694 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
695 int ret = 0;
696
697 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
698 &be_id, &cfg_data);
699 if (ret < 0) {
700 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
701 __func__, ret);
702 goto done;
703 }
704
705 ucontrol->value.integer.value[0] = cfg_data.app_type;
706 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
707 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
708 ucontrol->value.integer.value[3] = be_id;
709 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
710 __func__, fe_id, session_type, be_id,
711 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
712done:
713 return ret;
714}
715
716static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
717{
718 struct snd_pcm *pcm = rtd->pcm->streams[0].pcm;
719 struct snd_pcm_usr *app_type_info;
720 struct snd_kcontrol *kctl;
721 const char *playback_mixer_ctl_name = "Audio Stream";
722 const char *capture_mixer_ctl_name = "Audio Stream Capture";
723 const char *deviceNo = "NN";
724 const char *suffix = "App Type Cfg";
725 int ctl_len, ret = 0;
726
727 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
728 ctl_len = strlen(playback_mixer_ctl_name) + 1 +
729 strlen(deviceNo) + 1 + strlen(suffix) + 1;
730 pr_debug("%s: Playback app type cntrl add\n", __func__);
731 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
732 NULL, 1, ctl_len, rtd->dai_link->id,
733 &app_type_info);
734 if (ret < 0)
735 return ret;
736 kctl = app_type_info->kctl;
737 snprintf(kctl->id.name, ctl_len, "%s %d %s",
738 playback_mixer_ctl_name, rtd->pcm->device, suffix);
739 kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
740 kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
741 }
742
743 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
744 ctl_len = strlen(capture_mixer_ctl_name) + 1 +
745 strlen(deviceNo) + 1 + strlen(suffix) + 1;
746 pr_debug("%s: Capture app type cntrl add\n", __func__);
747 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
748 NULL, 1, ctl_len, rtd->dai_link->id,
749 &app_type_info);
750 if (ret < 0)
751 return ret;
752 kctl = app_type_info->kctl;
753 snprintf(kctl->id.name, ctl_len, "%s %d %s",
754 capture_mixer_ctl_name, rtd->pcm->device, suffix);
755 kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
756 kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
757 }
758
759 return 0;
760}
761
Dhananjay Kumar807f7e92018-12-11 18:10:08 +0530762static struct msm_pcm_channel_mixer *msm_pcm_get_chmixer(
763 struct msm_pcm_pdata *pdata,
764 u64 fe_id, int session_type)
765{
766 if (!pdata) {
767 pr_err("%s: missing pdata\n", __func__);
768 return NULL;
769 }
770
771 if (fe_id >= MSM_FRONTEND_DAI_MM_SIZE) {
772 pr_err("%s: invalid FE %llu\n", __func__, fe_id);
773 return NULL;
774 }
775
776 if ((session_type != SESSION_TYPE_TX) &&
777 (session_type != SESSION_TYPE_RX)) {
778 pr_err("%s: invalid session type %d\n", __func__, session_type);
779 return NULL;
780 }
781
782 return pdata->chmixer_pspd[fe_id][session_type];
783}
784
785static int msm_pcm_channel_mixer_cfg_ctl_put(struct snd_kcontrol *kcontrol,
786 struct snd_ctl_elem_value *ucontrol)
787{
788 u64 fe_id = kcontrol->private_value & 0xFF;
789 int session_type = (kcontrol->private_value >> 8) & 0xFF;
790 int ret = 0;
791 int stream_id = 0;
792 int be_id = 0, i = 0;
793 struct msm_pcm_loopback *prtd = NULL;
794 struct snd_soc_component *component =
795 snd_soc_kcontrol_component(kcontrol);
796 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
797 struct snd_pcm *pcm = NULL;
798 struct snd_pcm_substream *substream = NULL;
799 struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
800 u8 asm_ch_map[PCM_FORMAT_MAX_NUM_CHANNEL_V8] = {0};
801 bool reset_override_out_ch_map = false;
802 bool reset_override_in_ch_map = false;
803
804 pcm = pdata->pcm_device[fe_id];
805 if (!pcm) {
806 pr_err("%s invalid pcm handle for fe_id %llu\n",
807 __func__, fe_id);
808 return -EINVAL;
809 }
810
811 if (session_type == SESSION_TYPE_RX)
812 substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
813 else
814 substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
815 if (!substream) {
816 pr_err("%s substream not found\n", __func__);
817 return -EINVAL;
818 }
819
820 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
821 if (!chmixer_pspd) {
822 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
823 return -EINVAL;
824 }
825
826 chmixer_pspd->enable = ucontrol->value.integer.value[0];
827 chmixer_pspd->rule = ucontrol->value.integer.value[1];
828 chmixer_pspd->input_channel = ucontrol->value.integer.value[2];
829 chmixer_pspd->output_channel = ucontrol->value.integer.value[3];
830 chmixer_pspd->port_idx = ucontrol->value.integer.value[4];
831
832 if (chmixer_pspd->enable) {
833 if (session_type == SESSION_TYPE_RX &&
834 !chmixer_pspd->override_in_ch_map) {
Rohit kumar2a64e522019-02-04 11:22:49 +0530835 if (chmixer_pspd->input_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
836 pr_err("%s: Invalid channel count %d\n",
837 __func__, chmixer_pspd->input_channel);
838 return -EINVAL;
839 }
Dhananjay Kumar807f7e92018-12-11 18:10:08 +0530840 q6asm_map_channels(asm_ch_map,
841 chmixer_pspd->input_channel, false);
842 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
843 chmixer_pspd->in_ch_map[i] = asm_ch_map[i];
844 chmixer_pspd->override_in_ch_map = true;
845 reset_override_in_ch_map = true;
846 } else if (session_type == SESSION_TYPE_TX &&
847 !chmixer_pspd->override_out_ch_map) {
Rohit kumar2a64e522019-02-04 11:22:49 +0530848 if (chmixer_pspd->output_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
849 pr_err("%s: Invalid channel count %d\n",
850 __func__, chmixer_pspd->output_channel);
851 return -EINVAL;
852 }
Dhananjay Kumar807f7e92018-12-11 18:10:08 +0530853 q6asm_map_channels(asm_ch_map,
854 chmixer_pspd->output_channel, false);
855 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
856 chmixer_pspd->out_ch_map[i] = asm_ch_map[i];
857 chmixer_pspd->override_out_ch_map = true;
858 reset_override_out_ch_map = true;
859 }
860 } else {
861 chmixer_pspd->override_out_ch_map = false;
862 chmixer_pspd->override_in_ch_map = false;
863 }
864
865 /* cache value and take effect during adm_open stage */
866 msm_pcm_routing_set_channel_mixer_cfg(fe_id,
867 session_type,
868 chmixer_pspd);
869
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530870 mutex_lock(&loopback_session_lock);
Dhananjay Kumar807f7e92018-12-11 18:10:08 +0530871 if (chmixer_pspd->enable && substream->runtime) {
872 prtd = substream->runtime->private_data;
873 if (!prtd) {
874 pr_err("%s find invalid prtd fail\n", __func__);
875 ret = -EINVAL;
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530876 mutex_unlock(&loopback_session_lock);
Dhananjay Kumar807f7e92018-12-11 18:10:08 +0530877 goto done;
878 }
879
880 if (prtd->audio_client) {
881 stream_id = prtd->audio_client->session;
882 be_id = chmixer_pspd->port_idx;
883 msm_pcm_routing_set_channel_mixer_runtime(be_id,
884 stream_id,
885 session_type,
886 chmixer_pspd);
887 }
888 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530889 mutex_unlock(&loopback_session_lock);
Dhananjay Kumar807f7e92018-12-11 18:10:08 +0530890 if (reset_override_out_ch_map)
891 chmixer_pspd->override_out_ch_map = false;
892 if (reset_override_in_ch_map)
893 chmixer_pspd->override_in_ch_map = false;
894
895done:
896 return ret;
897}
898
899static int msm_pcm_channel_mixer_cfg_ctl_get(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 struct snd_soc_component *component =
905 snd_soc_kcontrol_component(kcontrol);
906 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
907 struct msm_pcm_channel_mixer *chmixer_pspd;
908
909 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
910 if (!chmixer_pspd) {
911 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
912 return -EINVAL;
913 }
914
915 ucontrol->value.integer.value[0] = chmixer_pspd->enable;
916 ucontrol->value.integer.value[1] = chmixer_pspd->rule;
917 ucontrol->value.integer.value[2] = chmixer_pspd->input_channel;
918 ucontrol->value.integer.value[3] = chmixer_pspd->output_channel;
919 ucontrol->value.integer.value[4] = chmixer_pspd->port_idx;
920 return 0;
921}
922
923static int msm_pcm_channel_mixer_output_map_ctl_put(
924 struct snd_kcontrol *kcontrol,
925 struct snd_ctl_elem_value *ucontrol)
926{
927 u64 fe_id = kcontrol->private_value & 0xFF;
928 int session_type = (kcontrol->private_value >> 8) & 0xFF;
929 int i = 0;
930 struct snd_soc_component *component =
931 snd_soc_kcontrol_component(kcontrol);
932 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
933 struct msm_pcm_channel_mixer *chmixer_pspd;
934
935 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
936 if (!chmixer_pspd) {
937 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
938 return -EINVAL;
939 }
940
941 chmixer_pspd->override_out_ch_map = true;
942 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
943 chmixer_pspd->out_ch_map[i] =
944 ucontrol->value.integer.value[i];
945
946 return 0;
947}
948
949static int msm_pcm_channel_mixer_output_map_ctl_get(
950 struct snd_kcontrol *kcontrol,
951 struct snd_ctl_elem_value *ucontrol)
952{
953 u64 fe_id = kcontrol->private_value & 0xFF;
954 int session_type = (kcontrol->private_value >> 8) & 0xFF;
955 int i = 0;
956 struct snd_soc_component *component =
957 snd_soc_kcontrol_component(kcontrol);
958 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
959 struct msm_pcm_channel_mixer *chmixer_pspd;
960
961 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
962 if (!chmixer_pspd) {
963 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
964 return -EINVAL;
965 }
966
967 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
968 ucontrol->value.integer.value[i] =
969 chmixer_pspd->out_ch_map[i];
970 return 0;
971}
972
973static int msm_pcm_channel_mixer_input_map_ctl_put(
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 chmixer_pspd->override_in_ch_map = true;
992 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
993 chmixer_pspd->in_ch_map[i] = ucontrol->value.integer.value[i];
994
995 return 0;
996}
997
998static int msm_pcm_channel_mixer_input_map_ctl_get(
999 struct snd_kcontrol *kcontrol,
1000 struct snd_ctl_elem_value *ucontrol)
1001{
1002 u64 fe_id = kcontrol->private_value & 0xFF;
1003 int session_type = (kcontrol->private_value >> 8) & 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 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
1017 ucontrol->value.integer.value[i] =
1018 chmixer_pspd->in_ch_map[i];
1019 return 0;
1020}
1021
1022static int msm_pcm_channel_mixer_weight_ctl_put(
1023 struct snd_kcontrol *kcontrol,
1024 struct snd_ctl_elem_value *ucontrol)
1025{
1026 u64 fe_id = kcontrol->private_value & 0xFF;
1027 int session_type = (kcontrol->private_value >> 8) & 0xFF;
1028 int channel = (kcontrol->private_value >> 16) & 0xFF;
1029 int i = 0;
1030 struct snd_soc_component *component =
1031 snd_soc_kcontrol_component(kcontrol);
1032 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
1033 struct msm_pcm_channel_mixer *chmixer_pspd;
1034
1035 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
1036 if (!chmixer_pspd) {
1037 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
1038 return -EINVAL;
1039 }
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 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
1048 chmixer_pspd->channel_weight[channel][i] =
1049 ucontrol->value.integer.value[i];
1050 return 0;
1051}
1052
1053static int msm_pcm_channel_mixer_weight_ctl_get(
1054 struct snd_kcontrol *kcontrol,
1055 struct snd_ctl_elem_value *ucontrol)
1056{
1057 u64 fe_id = kcontrol->private_value & 0xFF;
1058 int session_type = (kcontrol->private_value >> 8) & 0xFF;
1059 int channel = (kcontrol->private_value >> 16) & 0xFF;
1060 struct snd_soc_component *component =
1061 snd_soc_kcontrol_component(kcontrol);
1062 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
1063 int i = 0;
1064 struct msm_pcm_channel_mixer *chmixer_pspd;
1065
1066 if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
1067 pr_err("%s: invalid channel number %d\n", __func__, channel);
1068 return -EINVAL;
1069 }
1070 channel--;
1071
1072 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
1073 if (!chmixer_pspd) {
1074 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
1075 return -EINVAL;
1076 }
1077
1078 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
1079 ucontrol->value.integer.value[i] =
1080 chmixer_pspd->channel_weight[channel][i];
1081 return 0;
1082}
1083
1084static int msm_pcm_add_platform_controls(struct snd_kcontrol_new *kctl,
1085 struct snd_soc_pcm_runtime *rtd, const char *name_prefix,
1086 const char *name_suffix, int session_type, int channels)
1087{
1088 int ret = -EINVAL;
1089 char *mixer_name = NULL;
1090 struct snd_pcm *pcm = rtd->pcm;
1091 const char *deviceNo = "NN";
1092 const char *channelNo = "NN";
1093 int ctl_len = 0;
1094 struct snd_soc_component *component = NULL;
1095
1096 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
1097 if (!component) {
1098 pr_err("%s: component is NULL\n", __func__);
1099 return -EINVAL;
1100 }
1101
1102 ctl_len = strlen(name_prefix) + 1 + strlen(deviceNo) + 1 +
1103 strlen(channelNo) + 1 + strlen(name_suffix) + 1;
1104
1105 mixer_name = kzalloc(ctl_len, GFP_KERNEL);
1106 if (mixer_name == NULL)
1107 return -ENOMEM;
1108
1109 if (channels >= 0) {
1110 snprintf(mixer_name, ctl_len, "%s %d %s %d",
1111 name_prefix, pcm->device, name_suffix, channels);
1112 kctl->private_value = (rtd->dai_link->id) | (session_type << 8) |
1113 (channels << 16);
1114 } else {
1115 snprintf(mixer_name, ctl_len, "%s %d %s",
1116 name_prefix, pcm->device, name_suffix);
1117 kctl->private_value = (rtd->dai_link->id) | (session_type << 8);
1118 }
1119
1120 kctl->name = mixer_name;
1121 ret = snd_soc_add_component_controls(component, kctl, 1);
1122 kfree(mixer_name);
1123 return ret;
1124}
1125
1126static int msm_pcm_channel_mixer_output_map_info(struct snd_kcontrol *kcontrol,
1127 struct snd_ctl_elem_info *uinfo)
1128{
1129 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1130 uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
1131 /* Valid channel map value ranges from 1 to 64 */
1132 uinfo->value.integer.min = 1;
1133 uinfo->value.integer.max = 64;
1134 return 0;
1135}
1136
1137static int msm_pcm_add_channel_mixer_output_map_controls(
1138 struct snd_soc_pcm_runtime *rtd)
1139{
1140 struct snd_pcm *pcm = rtd->pcm;
1141 const char *playback_mixer_ctl_name = "AudStr";
1142 const char *capture_mixer_ctl_name = "AudStr Capture";
1143 const char *suffix = "ChMixer Output Map";
1144 int session_type = 0, ret = 0, channel = -1;
1145 struct snd_kcontrol_new channel_mixer_output_map_control = {
1146 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1147 .name = "?",
1148 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1149 .info = msm_pcm_channel_mixer_output_map_info,
1150 .put = msm_pcm_channel_mixer_output_map_ctl_put,
1151 .get = msm_pcm_channel_mixer_output_map_ctl_get,
1152 .private_value = 0,
1153 };
1154
1155 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
1156 session_type = SESSION_TYPE_RX;
1157 ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
1158 rtd, playback_mixer_ctl_name, suffix, session_type, channel);
1159 if (ret < 0)
1160 goto fail;
1161 }
1162
1163 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
1164 session_type = SESSION_TYPE_TX;
1165 ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
1166 rtd, capture_mixer_ctl_name, suffix, session_type, channel);
1167 if (ret < 0)
1168 goto fail;
1169 }
1170 return 0;
1171
1172fail:
1173 pr_err("%s: failed add platform ctl, err = %d\n",
1174 __func__, ret);
1175
1176 return ret;
1177}
1178
1179static int msm_pcm_channel_mixer_input_map_info(struct snd_kcontrol *kcontrol,
1180 struct snd_ctl_elem_info *uinfo)
1181{
1182 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1183 uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
1184 /* Valid channel map value ranges from 1 to 64 */
1185 uinfo->value.integer.min = 1;
1186 uinfo->value.integer.max = 64;
1187 return 0;
1188}
1189
1190static int msm_pcm_add_channel_mixer_input_map_controls(
1191 struct snd_soc_pcm_runtime *rtd)
1192{
1193 struct snd_pcm *pcm = rtd->pcm;
1194 const char *playback_mixer_ctl_name = "AudStr";
1195 const char *capture_mixer_ctl_name = "AudStr Capture";
1196 const char *suffix = "ChMixer Input Map";
1197 int session_type = 0, ret = 0, channel = -1;
1198 struct snd_kcontrol_new channel_mixer_input_map_control = {
1199 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1200 .name = "?",
1201 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1202 .info = msm_pcm_channel_mixer_input_map_info,
1203 .put = msm_pcm_channel_mixer_input_map_ctl_put,
1204 .get = msm_pcm_channel_mixer_input_map_ctl_get,
1205 .private_value = 0,
1206 };
1207
1208 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
1209 session_type = SESSION_TYPE_RX;
1210 ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
1211 rtd, playback_mixer_ctl_name, suffix, session_type, channel);
1212 if (ret < 0)
1213 goto fail;
1214 }
1215
1216 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
1217 session_type = SESSION_TYPE_TX;
1218 ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
1219 rtd, capture_mixer_ctl_name, suffix, session_type, channel);
1220 if (ret < 0)
1221 goto fail;
1222 }
1223 return 0;
1224
1225fail:
1226 pr_err("%s: failed add platform ctl, err = %d\n",
1227 __func__, ret);
1228 return ret;
1229}
1230
1231static int msm_pcm_channel_mixer_cfg_info(struct snd_kcontrol *kcontrol,
1232 struct snd_ctl_elem_info *uinfo)
1233{
1234 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1235 /* five int values: enable, rule, in_channels, out_channels and port_id */
1236 uinfo->count = 5;
1237 /* Valid range is all positive values to support above controls */
1238 uinfo->value.integer.min = 0;
1239 uinfo->value.integer.max = INT_MAX;
1240 return 0;
1241}
1242
1243static int msm_pcm_add_channel_mixer_cfg_controls(
1244 struct snd_soc_pcm_runtime *rtd)
1245{
1246 struct snd_pcm *pcm = rtd->pcm;
1247 const char *playback_mixer_ctl_name = "AudStr";
1248 const char *capture_mixer_ctl_name = "AudStr Capture";
1249 const char *suffix = "ChMixer Cfg";
1250 int session_type = 0, ret = 0, channel = -1;
1251 struct msm_pcm_pdata *pdata = NULL;
1252 struct snd_soc_component *component = NULL;
1253 struct snd_kcontrol_new channel_mixer_cfg_control = {
1254 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1255 .name = "?",
1256 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1257 .info = msm_pcm_channel_mixer_cfg_info,
1258 .put = msm_pcm_channel_mixer_cfg_ctl_put,
1259 .get = msm_pcm_channel_mixer_cfg_ctl_get,
1260 .private_value = 0,
1261 };
1262
1263 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
1264 if (!component) {
1265 pr_err("%s: component is NULL\n", __func__);
1266 return -EINVAL;
1267 }
1268 pdata = (struct msm_pcm_pdata *)
1269 dev_get_drvdata(component->dev);
1270 if (pdata == NULL) {
1271 pr_err("%s: platform data not populated\n", __func__);
1272 return -EINVAL;
1273 }
1274
1275 pdata->pcm_device[rtd->dai_link->id] = rtd->pcm;
1276
1277 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
1278 session_type = SESSION_TYPE_RX;
1279 ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
1280 rtd, playback_mixer_ctl_name, suffix, session_type, channel);
1281 if (ret < 0)
1282 goto fail;
1283 }
1284
1285 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
1286 session_type = SESSION_TYPE_TX;
1287 ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
1288 rtd, capture_mixer_ctl_name, suffix, session_type, channel);
1289 if (ret < 0)
1290 goto fail;
1291 }
1292 return 0;
1293
1294fail:
1295 pr_err("%s: failed add platform ctl, err = %d\n",
1296 __func__, ret);
1297
1298 return ret;
1299}
1300
1301static int msm_pcm_channel_mixer_weight_info(struct snd_kcontrol *kcontrol,
1302 struct snd_ctl_elem_info *uinfo)
1303{
1304 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1305 uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
1306 /* Valid range: 0 to 0x4000(Unity) gain weightage */
1307 uinfo->value.integer.min = 0;
1308 uinfo->value.integer.max = 0x4000;
1309 return 0;
1310}
1311
1312static int msm_pcm_add_channel_mixer_weight_controls(
1313 struct snd_soc_pcm_runtime *rtd,
1314 int channel)
1315{
1316 struct snd_pcm *pcm = rtd->pcm;
1317 const char *playback_mixer_ctl_name = "AudStr";
1318 const char *capture_mixer_ctl_name = "AudStr Capture";
1319 const char *suffix = "ChMixer Weight Ch";
1320 int session_type = 0, ret = 0;
1321 struct snd_kcontrol_new channel_mixer_weight_control = {
1322 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1323 .name = "?",
1324 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1325 .info = msm_pcm_channel_mixer_weight_info,
1326 .put = msm_pcm_channel_mixer_weight_ctl_put,
1327 .get = msm_pcm_channel_mixer_weight_ctl_get,
1328 .private_value = 0,
1329 };
1330
1331 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
1332 session_type = SESSION_TYPE_RX;
1333 ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
1334 rtd, playback_mixer_ctl_name, suffix, session_type, channel);
1335 if (ret < 0)
1336 goto fail;
1337 }
1338
1339 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
1340 session_type = SESSION_TYPE_TX;
1341 ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
1342 rtd, capture_mixer_ctl_name, suffix, session_type, channel);
1343 if (ret < 0)
1344 goto fail;
1345 }
1346 return 0;
1347
1348fail:
1349 pr_err("%s: failed add platform ctl, err = %d\n",
1350 __func__, ret);
1351
1352 return ret;
1353}
1354
1355static int msm_pcm_add_channel_mixer_controls(struct snd_soc_pcm_runtime *rtd)
1356{
1357 int i, ret = 0;
1358 struct snd_pcm *pcm = NULL;
1359 struct msm_pcm_pdata *pdata = NULL;
1360 struct snd_soc_component *component = NULL;
1361
1362 if (!rtd || !rtd->pcm) {
1363 pr_err("%s invalid rtd or pcm\n", __func__);
1364 return -EINVAL;
1365 }
1366 pcm = rtd->pcm;
1367
1368 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
1369 if (!component) {
1370 pr_err("%s: component is NULL\n", __func__);
1371 return -EINVAL;
1372 }
1373
1374 pdata = (struct msm_pcm_pdata *)
1375 dev_get_drvdata(component->dev);
1376 if (!pdata) {
1377 pr_err("%s: platform data not populated\n", __func__);
1378 return -EINVAL;
1379 }
1380
1381 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream &&
1382 !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
1383 pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] =
1384 kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
1385 if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
1386 ret = -ENOMEM;
1387 goto fail;
1388 }
1389 }
1390
1391 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream &&
1392 !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
1393 pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] =
1394 kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
1395 if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
1396 ret = -ENOMEM;
1397 goto fail;
1398 }
1399 }
1400
1401 ret = msm_pcm_add_channel_mixer_cfg_controls(rtd);
1402 if (ret) {
1403 pr_err("%s: pcm add channel mixer cfg controls failed:%d\n",
1404 __func__, ret);
1405 goto fail;
1406 }
1407 ret = msm_pcm_add_channel_mixer_input_map_controls(rtd);
1408 if (ret) {
1409 pr_err("%s: pcm add channel mixer input map controls failed:%d\n",
1410 __func__, ret);
1411 goto fail;
1412 }
1413 ret = msm_pcm_add_channel_mixer_output_map_controls(rtd);
1414 if (ret) {
1415 pr_err("%s: pcm add channel mixer output map controls failed:%d\n",
1416 __func__, ret);
1417 goto fail;
1418 }
1419
1420 for (i = 1; i <= PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++) {
1421 ret = msm_pcm_add_channel_mixer_weight_controls(rtd, i);
1422 if (ret) {
1423 pr_err("%s: pcm add channel mixer weight controls failed:%d\n",
1424 __func__, ret);
1425 goto fail;
1426 }
1427 }
1428 return 0;
1429
1430fail:
1431 kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]);
1432 kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]);
1433 pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] = NULL;
1434 pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] = NULL;
1435
1436 return ret;
1437}
1438
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301439static int msm_pcm_add_controls(struct snd_soc_pcm_runtime *rtd)
1440{
1441 int ret = 0;
1442
1443 pr_debug("%s\n", __func__);
1444 ret = msm_pcm_add_volume_controls(rtd);
1445 if (ret)
1446 pr_err("%s: pcm add volume controls failed:%d\n",
1447 __func__, ret);
1448 ret = msm_pcm_add_app_type_controls(rtd);
1449 if (ret)
1450 pr_err("%s: pcm add app type controls failed:%d\n",
1451 __func__, ret);
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05301452
1453 ret = msm_pcm_add_channel_mixer_controls(rtd);
1454 if (ret)
1455 pr_err("%s: pcm add channel mixer controls failed:%d\n",
1456 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301457 return ret;
1458}
1459
1460static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
1461{
1462 struct snd_card *card = rtd->card->snd_card;
1463 int ret = 0;
1464
1465 if (!card->dev->coherent_dma_mask)
1466 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
1467
1468 ret = msm_pcm_add_controls(rtd);
1469 if (ret)
1470 dev_err(rtd->dev, "%s, kctl add failed\n", __func__);
1471 return ret;
1472}
1473
Meng Wangee084a02018-09-04 16:11:58 +08001474static struct snd_soc_component_driver msm_soc_component = {
1475 .name = DRV_NAME,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301476 .ops = &msm_pcm_ops,
1477 .pcm_new = msm_asoc_pcm_new,
1478 .probe = msm_pcm_loopback_probe,
1479};
1480
1481static int msm_pcm_probe(struct platform_device *pdev)
1482{
1483 struct msm_pcm_pdata *pdata;
1484
1485 dev_dbg(&pdev->dev, "%s: dev name %s\n",
1486 __func__, dev_name(&pdev->dev));
1487
1488 pdata = kzalloc(sizeof(struct msm_pcm_pdata), GFP_KERNEL);
1489 if (!pdata)
1490 return -ENOMEM;
1491
1492 if (of_property_read_bool(pdev->dev.of_node,
1493 "qcom,msm-pcm-loopback-low-latency"))
1494 pdata->perf_mode = LOW_LATENCY_PCM_MODE;
1495 else
1496 pdata->perf_mode = LEGACY_PCM_MODE;
1497
1498 dev_set_drvdata(&pdev->dev, pdata);
1499
Meng Wangee084a02018-09-04 16:11:58 +08001500 return snd_soc_register_component(&pdev->dev,
1501 &msm_soc_component,
1502 NULL, 0);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301503}
1504
1505static int msm_pcm_remove(struct platform_device *pdev)
1506{
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05301507 struct msm_pcm_pdata *pdata;
1508 int i = 0;
1509
1510 pdata = dev_get_drvdata(&pdev->dev);
1511 if (pdata) {
1512 for (i = 0; i < MSM_FRONTEND_DAI_MM_SIZE; i++) {
1513 kfree(pdata->chmixer_pspd[i][SESSION_TYPE_RX]);
1514 kfree(pdata->chmixer_pspd[i][SESSION_TYPE_TX]);
1515 }
1516 }
1517 kfree(pdata);
Meng Wangee084a02018-09-04 16:11:58 +08001518 snd_soc_unregister_component(&pdev->dev);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301519 return 0;
1520}
1521
1522static const struct of_device_id msm_pcm_loopback_dt_match[] = {
1523 {.compatible = "qcom,msm-pcm-loopback"},
1524 {}
1525};
1526
1527static struct platform_driver msm_pcm_driver = {
1528 .driver = {
1529 .name = "msm-pcm-loopback",
1530 .owner = THIS_MODULE,
1531 .of_match_table = msm_pcm_loopback_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08001532 .suppress_bind_attrs = true,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301533 },
1534 .probe = msm_pcm_probe,
1535 .remove = msm_pcm_remove,
1536};
1537
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301538int __init msm_pcm_loopback_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301539{
1540 return platform_driver_register(&msm_pcm_driver);
1541}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301542
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05301543void msm_pcm_loopback_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301544{
1545 platform_driver_unregister(&msm_pcm_driver);
1546}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301547
1548MODULE_DESCRIPTION("PCM loopback platform driver");
1549MODULE_LICENSE("GPL v2");