blob: 19fe1903881f9673ec7d83995db573980b3fbb2f [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 }
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);
562 struct snd_pcm_substream *substream =
563 vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
564 struct msm_pcm_loopback *prtd;
565
566 pr_debug("%s\n", __func__);
567 if ((!substream) || (!substream->runtime)) {
Vignesh Kulothungan2ce67842018-09-25 16:40:29 -0700568 pr_debug("%s substream or runtime not found\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530569 rc = -ENODEV;
570 goto exit;
571 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530572 mutex_lock(&loopback_session_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530573 prtd = substream->runtime->private_data;
574 if (!prtd) {
575 rc = -ENODEV;
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530576 mutex_unlock(&loopback_session_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530577 goto exit;
578 }
579 ucontrol->value.integer.value[0] = prtd->volume;
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530580 mutex_unlock(&loopback_session_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530581exit:
582 return rc;
583}
584
585static int msm_pcm_add_volume_controls(struct snd_soc_pcm_runtime *rtd)
586{
587 struct snd_pcm *pcm = rtd->pcm->streams[0].pcm;
588 struct snd_pcm_volume *volume_info;
589 struct snd_kcontrol *kctl;
590 int ret = 0;
591
592 dev_dbg(rtd->dev, "%s, Volume cntrl add\n", __func__);
593 ret = snd_pcm_add_volume_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
594 NULL, 1,
595 rtd->dai_link->id,
596 &volume_info);
597 if (ret < 0)
598 return ret;
599 kctl = volume_info->kctl;
600 kctl->put = msm_pcm_volume_ctl_put;
601 kctl->get = msm_pcm_volume_ctl_get;
602 kctl->tlv.p = loopback_rx_vol_gain;
603 return 0;
604}
605
606static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
607 struct snd_ctl_elem_value *ucontrol)
608{
609 u64 fe_id = kcontrol->private_value;
610 int session_type = SESSION_TYPE_RX;
611 int be_id = ucontrol->value.integer.value[3];
612 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
613 int ret = 0;
614
615 cfg_data.app_type = ucontrol->value.integer.value[0];
616 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
617 if (ucontrol->value.integer.value[2] != 0)
618 cfg_data.sample_rate = ucontrol->value.integer.value[2];
619 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
620 __func__, fe_id, session_type, be_id,
621 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
622 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
623 be_id, &cfg_data);
624 if (ret < 0)
625 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
626 __func__, ret);
627
628 return ret;
629}
630
631static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
632 struct snd_ctl_elem_value *ucontrol)
633{
634 u64 fe_id = kcontrol->private_value;
635 int session_type = SESSION_TYPE_RX;
636 int be_id = 0;
637 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
638 int ret = 0;
639
640 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
641 &be_id, &cfg_data);
642 if (ret < 0) {
643 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
644 __func__, ret);
645 goto done;
646 }
647
648 ucontrol->value.integer.value[0] = cfg_data.app_type;
649 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
650 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
651 ucontrol->value.integer.value[3] = be_id;
652 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
653 __func__, fe_id, session_type, be_id,
654 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
655done:
656 return ret;
657}
658
659static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
660 struct snd_ctl_elem_value *ucontrol)
661{
662 u64 fe_id = kcontrol->private_value;
663 int session_type = SESSION_TYPE_TX;
664 int be_id = ucontrol->value.integer.value[3];
665 struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
666 int ret = 0;
667
668 cfg_data.app_type = ucontrol->value.integer.value[0];
669 cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
670 if (ucontrol->value.integer.value[2] != 0)
671 cfg_data.sample_rate = ucontrol->value.integer.value[2];
672 pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
673 __func__, fe_id, session_type, be_id,
674 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
675 ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
676 be_id, &cfg_data);
677 if (ret < 0)
678 pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
679 __func__, ret);
680
681 return ret;
682}
683
684static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
685 struct snd_ctl_elem_value *ucontrol)
686{
687 u64 fe_id = kcontrol->private_value;
688 int session_type = SESSION_TYPE_TX;
689 int be_id = 0;
690 struct msm_pcm_stream_app_type_cfg cfg_data = {0};
691 int ret = 0;
692
693 ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
694 &be_id, &cfg_data);
695 if (ret < 0) {
696 pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
697 __func__, ret);
698 goto done;
699 }
700
701 ucontrol->value.integer.value[0] = cfg_data.app_type;
702 ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
703 ucontrol->value.integer.value[2] = cfg_data.sample_rate;
704 ucontrol->value.integer.value[3] = be_id;
705 pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
706 __func__, fe_id, session_type, be_id,
707 cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
708done:
709 return ret;
710}
711
712static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
713{
714 struct snd_pcm *pcm = rtd->pcm->streams[0].pcm;
715 struct snd_pcm_usr *app_type_info;
716 struct snd_kcontrol *kctl;
717 const char *playback_mixer_ctl_name = "Audio Stream";
718 const char *capture_mixer_ctl_name = "Audio Stream Capture";
719 const char *deviceNo = "NN";
720 const char *suffix = "App Type Cfg";
721 int ctl_len, ret = 0;
722
723 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
724 ctl_len = strlen(playback_mixer_ctl_name) + 1 +
725 strlen(deviceNo) + 1 + strlen(suffix) + 1;
726 pr_debug("%s: Playback app type cntrl add\n", __func__);
727 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
728 NULL, 1, ctl_len, rtd->dai_link->id,
729 &app_type_info);
730 if (ret < 0)
731 return ret;
732 kctl = app_type_info->kctl;
733 snprintf(kctl->id.name, ctl_len, "%s %d %s",
734 playback_mixer_ctl_name, rtd->pcm->device, suffix);
735 kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
736 kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
737 }
738
739 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
740 ctl_len = strlen(capture_mixer_ctl_name) + 1 +
741 strlen(deviceNo) + 1 + strlen(suffix) + 1;
742 pr_debug("%s: Capture app type cntrl add\n", __func__);
743 ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
744 NULL, 1, ctl_len, rtd->dai_link->id,
745 &app_type_info);
746 if (ret < 0)
747 return ret;
748 kctl = app_type_info->kctl;
749 snprintf(kctl->id.name, ctl_len, "%s %d %s",
750 capture_mixer_ctl_name, rtd->pcm->device, suffix);
751 kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
752 kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
753 }
754
755 return 0;
756}
757
Dhananjay Kumar807f7e92018-12-11 18:10:08 +0530758static struct msm_pcm_channel_mixer *msm_pcm_get_chmixer(
759 struct msm_pcm_pdata *pdata,
760 u64 fe_id, int session_type)
761{
762 if (!pdata) {
763 pr_err("%s: missing pdata\n", __func__);
764 return NULL;
765 }
766
767 if (fe_id >= MSM_FRONTEND_DAI_MM_SIZE) {
768 pr_err("%s: invalid FE %llu\n", __func__, fe_id);
769 return NULL;
770 }
771
772 if ((session_type != SESSION_TYPE_TX) &&
773 (session_type != SESSION_TYPE_RX)) {
774 pr_err("%s: invalid session type %d\n", __func__, session_type);
775 return NULL;
776 }
777
778 return pdata->chmixer_pspd[fe_id][session_type];
779}
780
781static int msm_pcm_channel_mixer_cfg_ctl_put(struct snd_kcontrol *kcontrol,
782 struct snd_ctl_elem_value *ucontrol)
783{
784 u64 fe_id = kcontrol->private_value & 0xFF;
785 int session_type = (kcontrol->private_value >> 8) & 0xFF;
786 int ret = 0;
787 int stream_id = 0;
788 int be_id = 0, i = 0;
789 struct msm_pcm_loopback *prtd = NULL;
790 struct snd_soc_component *component =
791 snd_soc_kcontrol_component(kcontrol);
792 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
793 struct snd_pcm *pcm = NULL;
794 struct snd_pcm_substream *substream = NULL;
795 struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
796 u8 asm_ch_map[PCM_FORMAT_MAX_NUM_CHANNEL_V8] = {0};
797 bool reset_override_out_ch_map = false;
798 bool reset_override_in_ch_map = false;
799
800 pcm = pdata->pcm_device[fe_id];
801 if (!pcm) {
802 pr_err("%s invalid pcm handle for fe_id %llu\n",
803 __func__, fe_id);
804 return -EINVAL;
805 }
806
807 if (session_type == SESSION_TYPE_RX)
808 substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
809 else
810 substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
811 if (!substream) {
812 pr_err("%s substream not found\n", __func__);
813 return -EINVAL;
814 }
815
816 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
817 if (!chmixer_pspd) {
818 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
819 return -EINVAL;
820 }
821
822 chmixer_pspd->enable = ucontrol->value.integer.value[0];
823 chmixer_pspd->rule = ucontrol->value.integer.value[1];
824 chmixer_pspd->input_channel = ucontrol->value.integer.value[2];
825 chmixer_pspd->output_channel = ucontrol->value.integer.value[3];
826 chmixer_pspd->port_idx = ucontrol->value.integer.value[4];
827
828 if (chmixer_pspd->enable) {
829 if (session_type == SESSION_TYPE_RX &&
830 !chmixer_pspd->override_in_ch_map) {
Rohit kumar2a64e522019-02-04 11:22:49 +0530831 if (chmixer_pspd->input_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
832 pr_err("%s: Invalid channel count %d\n",
833 __func__, chmixer_pspd->input_channel);
834 return -EINVAL;
835 }
Dhananjay Kumar807f7e92018-12-11 18:10:08 +0530836 q6asm_map_channels(asm_ch_map,
837 chmixer_pspd->input_channel, false);
838 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
839 chmixer_pspd->in_ch_map[i] = asm_ch_map[i];
840 chmixer_pspd->override_in_ch_map = true;
841 reset_override_in_ch_map = true;
842 } else if (session_type == SESSION_TYPE_TX &&
843 !chmixer_pspd->override_out_ch_map) {
Rohit kumar2a64e522019-02-04 11:22:49 +0530844 if (chmixer_pspd->output_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
845 pr_err("%s: Invalid channel count %d\n",
846 __func__, chmixer_pspd->output_channel);
847 return -EINVAL;
848 }
Dhananjay Kumar807f7e92018-12-11 18:10:08 +0530849 q6asm_map_channels(asm_ch_map,
850 chmixer_pspd->output_channel, false);
851 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
852 chmixer_pspd->out_ch_map[i] = asm_ch_map[i];
853 chmixer_pspd->override_out_ch_map = true;
854 reset_override_out_ch_map = true;
855 }
856 } else {
857 chmixer_pspd->override_out_ch_map = false;
858 chmixer_pspd->override_in_ch_map = false;
859 }
860
861 /* cache value and take effect during adm_open stage */
862 msm_pcm_routing_set_channel_mixer_cfg(fe_id,
863 session_type,
864 chmixer_pspd);
865
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530866 mutex_lock(&loopback_session_lock);
Dhananjay Kumar807f7e92018-12-11 18:10:08 +0530867 if (chmixer_pspd->enable && substream->runtime) {
868 prtd = substream->runtime->private_data;
869 if (!prtd) {
870 pr_err("%s find invalid prtd fail\n", __func__);
871 ret = -EINVAL;
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530872 mutex_unlock(&loopback_session_lock);
Dhananjay Kumar807f7e92018-12-11 18:10:08 +0530873 goto done;
874 }
875
876 if (prtd->audio_client) {
877 stream_id = prtd->audio_client->session;
878 be_id = chmixer_pspd->port_idx;
879 msm_pcm_routing_set_channel_mixer_runtime(be_id,
880 stream_id,
881 session_type,
882 chmixer_pspd);
883 }
884 }
Prasad Kumpatla2ef0c882019-10-04 15:11:10 +0530885 mutex_unlock(&loopback_session_lock);
Dhananjay Kumar807f7e92018-12-11 18:10:08 +0530886 if (reset_override_out_ch_map)
887 chmixer_pspd->override_out_ch_map = false;
888 if (reset_override_in_ch_map)
889 chmixer_pspd->override_in_ch_map = false;
890
891done:
892 return ret;
893}
894
895static int msm_pcm_channel_mixer_cfg_ctl_get(struct snd_kcontrol *kcontrol,
896 struct snd_ctl_elem_value *ucontrol)
897{
898 u64 fe_id = kcontrol->private_value & 0xFF;
899 int session_type = (kcontrol->private_value >> 8) & 0xFF;
900 struct snd_soc_component *component =
901 snd_soc_kcontrol_component(kcontrol);
902 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
903 struct msm_pcm_channel_mixer *chmixer_pspd;
904
905 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
906 if (!chmixer_pspd) {
907 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
908 return -EINVAL;
909 }
910
911 ucontrol->value.integer.value[0] = chmixer_pspd->enable;
912 ucontrol->value.integer.value[1] = chmixer_pspd->rule;
913 ucontrol->value.integer.value[2] = chmixer_pspd->input_channel;
914 ucontrol->value.integer.value[3] = chmixer_pspd->output_channel;
915 ucontrol->value.integer.value[4] = chmixer_pspd->port_idx;
916 return 0;
917}
918
919static int msm_pcm_channel_mixer_output_map_ctl_put(
920 struct snd_kcontrol *kcontrol,
921 struct snd_ctl_elem_value *ucontrol)
922{
923 u64 fe_id = kcontrol->private_value & 0xFF;
924 int session_type = (kcontrol->private_value >> 8) & 0xFF;
925 int i = 0;
926 struct snd_soc_component *component =
927 snd_soc_kcontrol_component(kcontrol);
928 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
929 struct msm_pcm_channel_mixer *chmixer_pspd;
930
931 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
932 if (!chmixer_pspd) {
933 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
934 return -EINVAL;
935 }
936
937 chmixer_pspd->override_out_ch_map = true;
938 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
939 chmixer_pspd->out_ch_map[i] =
940 ucontrol->value.integer.value[i];
941
942 return 0;
943}
944
945static int msm_pcm_channel_mixer_output_map_ctl_get(
946 struct snd_kcontrol *kcontrol,
947 struct snd_ctl_elem_value *ucontrol)
948{
949 u64 fe_id = kcontrol->private_value & 0xFF;
950 int session_type = (kcontrol->private_value >> 8) & 0xFF;
951 int i = 0;
952 struct snd_soc_component *component =
953 snd_soc_kcontrol_component(kcontrol);
954 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
955 struct msm_pcm_channel_mixer *chmixer_pspd;
956
957 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
958 if (!chmixer_pspd) {
959 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
960 return -EINVAL;
961 }
962
963 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
964 ucontrol->value.integer.value[i] =
965 chmixer_pspd->out_ch_map[i];
966 return 0;
967}
968
969static int msm_pcm_channel_mixer_input_map_ctl_put(
970 struct snd_kcontrol *kcontrol,
971 struct snd_ctl_elem_value *ucontrol)
972{
973 u64 fe_id = kcontrol->private_value & 0xFF;
974 int session_type = (kcontrol->private_value >> 8) & 0xFF;
975 int i = 0;
976 struct snd_soc_component *component =
977 snd_soc_kcontrol_component(kcontrol);
978 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
979 struct msm_pcm_channel_mixer *chmixer_pspd;
980
981 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
982 if (!chmixer_pspd) {
983 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
984 return -EINVAL;
985 }
986
987 chmixer_pspd->override_in_ch_map = true;
988 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
989 chmixer_pspd->in_ch_map[i] = ucontrol->value.integer.value[i];
990
991 return 0;
992}
993
994static int msm_pcm_channel_mixer_input_map_ctl_get(
995 struct snd_kcontrol *kcontrol,
996 struct snd_ctl_elem_value *ucontrol)
997{
998 u64 fe_id = kcontrol->private_value & 0xFF;
999 int session_type = (kcontrol->private_value >> 8) & 0xFF;
1000 int i = 0;
1001 struct snd_soc_component *component =
1002 snd_soc_kcontrol_component(kcontrol);
1003 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
1004 struct msm_pcm_channel_mixer *chmixer_pspd;
1005
1006 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
1007 if (!chmixer_pspd) {
1008 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
1009 return -EINVAL;
1010 }
1011
1012 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
1013 ucontrol->value.integer.value[i] =
1014 chmixer_pspd->in_ch_map[i];
1015 return 0;
1016}
1017
1018static int msm_pcm_channel_mixer_weight_ctl_put(
1019 struct snd_kcontrol *kcontrol,
1020 struct snd_ctl_elem_value *ucontrol)
1021{
1022 u64 fe_id = kcontrol->private_value & 0xFF;
1023 int session_type = (kcontrol->private_value >> 8) & 0xFF;
1024 int channel = (kcontrol->private_value >> 16) & 0xFF;
1025 int i = 0;
1026 struct snd_soc_component *component =
1027 snd_soc_kcontrol_component(kcontrol);
1028 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
1029 struct msm_pcm_channel_mixer *chmixer_pspd;
1030
1031 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
1032 if (!chmixer_pspd) {
1033 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
1034 return -EINVAL;
1035 }
1036
1037 if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
1038 pr_err("%s: invalid channel number %d\n", __func__, channel);
1039 return -EINVAL;
1040 }
1041 channel--;
1042
1043 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
1044 chmixer_pspd->channel_weight[channel][i] =
1045 ucontrol->value.integer.value[i];
1046 return 0;
1047}
1048
1049static int msm_pcm_channel_mixer_weight_ctl_get(
1050 struct snd_kcontrol *kcontrol,
1051 struct snd_ctl_elem_value *ucontrol)
1052{
1053 u64 fe_id = kcontrol->private_value & 0xFF;
1054 int session_type = (kcontrol->private_value >> 8) & 0xFF;
1055 int channel = (kcontrol->private_value >> 16) & 0xFF;
1056 struct snd_soc_component *component =
1057 snd_soc_kcontrol_component(kcontrol);
1058 struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
1059 int i = 0;
1060 struct msm_pcm_channel_mixer *chmixer_pspd;
1061
1062 if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
1063 pr_err("%s: invalid channel number %d\n", __func__, channel);
1064 return -EINVAL;
1065 }
1066 channel--;
1067
1068 chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
1069 if (!chmixer_pspd) {
1070 pr_err("%s: invalid chmixer_pspd in pdata", __func__);
1071 return -EINVAL;
1072 }
1073
1074 for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
1075 ucontrol->value.integer.value[i] =
1076 chmixer_pspd->channel_weight[channel][i];
1077 return 0;
1078}
1079
1080static int msm_pcm_add_platform_controls(struct snd_kcontrol_new *kctl,
1081 struct snd_soc_pcm_runtime *rtd, const char *name_prefix,
1082 const char *name_suffix, int session_type, int channels)
1083{
1084 int ret = -EINVAL;
1085 char *mixer_name = NULL;
1086 struct snd_pcm *pcm = rtd->pcm;
1087 const char *deviceNo = "NN";
1088 const char *channelNo = "NN";
1089 int ctl_len = 0;
1090 struct snd_soc_component *component = NULL;
1091
1092 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
1093 if (!component) {
1094 pr_err("%s: component is NULL\n", __func__);
1095 return -EINVAL;
1096 }
1097
1098 ctl_len = strlen(name_prefix) + 1 + strlen(deviceNo) + 1 +
1099 strlen(channelNo) + 1 + strlen(name_suffix) + 1;
1100
1101 mixer_name = kzalloc(ctl_len, GFP_KERNEL);
1102 if (mixer_name == NULL)
1103 return -ENOMEM;
1104
1105 if (channels >= 0) {
1106 snprintf(mixer_name, ctl_len, "%s %d %s %d",
1107 name_prefix, pcm->device, name_suffix, channels);
1108 kctl->private_value = (rtd->dai_link->id) | (session_type << 8) |
1109 (channels << 16);
1110 } else {
1111 snprintf(mixer_name, ctl_len, "%s %d %s",
1112 name_prefix, pcm->device, name_suffix);
1113 kctl->private_value = (rtd->dai_link->id) | (session_type << 8);
1114 }
1115
1116 kctl->name = mixer_name;
1117 ret = snd_soc_add_component_controls(component, kctl, 1);
1118 kfree(mixer_name);
1119 return ret;
1120}
1121
1122static int msm_pcm_channel_mixer_output_map_info(struct snd_kcontrol *kcontrol,
1123 struct snd_ctl_elem_info *uinfo)
1124{
1125 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1126 uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
1127 /* Valid channel map value ranges from 1 to 64 */
1128 uinfo->value.integer.min = 1;
1129 uinfo->value.integer.max = 64;
1130 return 0;
1131}
1132
1133static int msm_pcm_add_channel_mixer_output_map_controls(
1134 struct snd_soc_pcm_runtime *rtd)
1135{
1136 struct snd_pcm *pcm = rtd->pcm;
1137 const char *playback_mixer_ctl_name = "AudStr";
1138 const char *capture_mixer_ctl_name = "AudStr Capture";
1139 const char *suffix = "ChMixer Output Map";
1140 int session_type = 0, ret = 0, channel = -1;
1141 struct snd_kcontrol_new channel_mixer_output_map_control = {
1142 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1143 .name = "?",
1144 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1145 .info = msm_pcm_channel_mixer_output_map_info,
1146 .put = msm_pcm_channel_mixer_output_map_ctl_put,
1147 .get = msm_pcm_channel_mixer_output_map_ctl_get,
1148 .private_value = 0,
1149 };
1150
1151 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
1152 session_type = SESSION_TYPE_RX;
1153 ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
1154 rtd, playback_mixer_ctl_name, suffix, session_type, channel);
1155 if (ret < 0)
1156 goto fail;
1157 }
1158
1159 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
1160 session_type = SESSION_TYPE_TX;
1161 ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
1162 rtd, capture_mixer_ctl_name, suffix, session_type, channel);
1163 if (ret < 0)
1164 goto fail;
1165 }
1166 return 0;
1167
1168fail:
1169 pr_err("%s: failed add platform ctl, err = %d\n",
1170 __func__, ret);
1171
1172 return ret;
1173}
1174
1175static int msm_pcm_channel_mixer_input_map_info(struct snd_kcontrol *kcontrol,
1176 struct snd_ctl_elem_info *uinfo)
1177{
1178 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1179 uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
1180 /* Valid channel map value ranges from 1 to 64 */
1181 uinfo->value.integer.min = 1;
1182 uinfo->value.integer.max = 64;
1183 return 0;
1184}
1185
1186static int msm_pcm_add_channel_mixer_input_map_controls(
1187 struct snd_soc_pcm_runtime *rtd)
1188{
1189 struct snd_pcm *pcm = rtd->pcm;
1190 const char *playback_mixer_ctl_name = "AudStr";
1191 const char *capture_mixer_ctl_name = "AudStr Capture";
1192 const char *suffix = "ChMixer Input Map";
1193 int session_type = 0, ret = 0, channel = -1;
1194 struct snd_kcontrol_new channel_mixer_input_map_control = {
1195 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1196 .name = "?",
1197 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1198 .info = msm_pcm_channel_mixer_input_map_info,
1199 .put = msm_pcm_channel_mixer_input_map_ctl_put,
1200 .get = msm_pcm_channel_mixer_input_map_ctl_get,
1201 .private_value = 0,
1202 };
1203
1204 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
1205 session_type = SESSION_TYPE_RX;
1206 ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
1207 rtd, playback_mixer_ctl_name, suffix, session_type, channel);
1208 if (ret < 0)
1209 goto fail;
1210 }
1211
1212 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
1213 session_type = SESSION_TYPE_TX;
1214 ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
1215 rtd, capture_mixer_ctl_name, suffix, session_type, channel);
1216 if (ret < 0)
1217 goto fail;
1218 }
1219 return 0;
1220
1221fail:
1222 pr_err("%s: failed add platform ctl, err = %d\n",
1223 __func__, ret);
1224 return ret;
1225}
1226
1227static int msm_pcm_channel_mixer_cfg_info(struct snd_kcontrol *kcontrol,
1228 struct snd_ctl_elem_info *uinfo)
1229{
1230 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1231 /* five int values: enable, rule, in_channels, out_channels and port_id */
1232 uinfo->count = 5;
1233 /* Valid range is all positive values to support above controls */
1234 uinfo->value.integer.min = 0;
1235 uinfo->value.integer.max = INT_MAX;
1236 return 0;
1237}
1238
1239static int msm_pcm_add_channel_mixer_cfg_controls(
1240 struct snd_soc_pcm_runtime *rtd)
1241{
1242 struct snd_pcm *pcm = rtd->pcm;
1243 const char *playback_mixer_ctl_name = "AudStr";
1244 const char *capture_mixer_ctl_name = "AudStr Capture";
1245 const char *suffix = "ChMixer Cfg";
1246 int session_type = 0, ret = 0, channel = -1;
1247 struct msm_pcm_pdata *pdata = NULL;
1248 struct snd_soc_component *component = NULL;
1249 struct snd_kcontrol_new channel_mixer_cfg_control = {
1250 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1251 .name = "?",
1252 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1253 .info = msm_pcm_channel_mixer_cfg_info,
1254 .put = msm_pcm_channel_mixer_cfg_ctl_put,
1255 .get = msm_pcm_channel_mixer_cfg_ctl_get,
1256 .private_value = 0,
1257 };
1258
1259 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
1260 if (!component) {
1261 pr_err("%s: component is NULL\n", __func__);
1262 return -EINVAL;
1263 }
1264 pdata = (struct msm_pcm_pdata *)
1265 dev_get_drvdata(component->dev);
1266 if (pdata == NULL) {
1267 pr_err("%s: platform data not populated\n", __func__);
1268 return -EINVAL;
1269 }
1270
1271 pdata->pcm_device[rtd->dai_link->id] = rtd->pcm;
1272
1273 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
1274 session_type = SESSION_TYPE_RX;
1275 ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
1276 rtd, playback_mixer_ctl_name, suffix, session_type, channel);
1277 if (ret < 0)
1278 goto fail;
1279 }
1280
1281 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
1282 session_type = SESSION_TYPE_TX;
1283 ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
1284 rtd, capture_mixer_ctl_name, suffix, session_type, channel);
1285 if (ret < 0)
1286 goto fail;
1287 }
1288 return 0;
1289
1290fail:
1291 pr_err("%s: failed add platform ctl, err = %d\n",
1292 __func__, ret);
1293
1294 return ret;
1295}
1296
1297static int msm_pcm_channel_mixer_weight_info(struct snd_kcontrol *kcontrol,
1298 struct snd_ctl_elem_info *uinfo)
1299{
1300 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1301 uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
1302 /* Valid range: 0 to 0x4000(Unity) gain weightage */
1303 uinfo->value.integer.min = 0;
1304 uinfo->value.integer.max = 0x4000;
1305 return 0;
1306}
1307
1308static int msm_pcm_add_channel_mixer_weight_controls(
1309 struct snd_soc_pcm_runtime *rtd,
1310 int channel)
1311{
1312 struct snd_pcm *pcm = rtd->pcm;
1313 const char *playback_mixer_ctl_name = "AudStr";
1314 const char *capture_mixer_ctl_name = "AudStr Capture";
1315 const char *suffix = "ChMixer Weight Ch";
1316 int session_type = 0, ret = 0;
1317 struct snd_kcontrol_new channel_mixer_weight_control = {
1318 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1319 .name = "?",
1320 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1321 .info = msm_pcm_channel_mixer_weight_info,
1322 .put = msm_pcm_channel_mixer_weight_ctl_put,
1323 .get = msm_pcm_channel_mixer_weight_ctl_get,
1324 .private_value = 0,
1325 };
1326
1327 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
1328 session_type = SESSION_TYPE_RX;
1329 ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
1330 rtd, playback_mixer_ctl_name, suffix, session_type, channel);
1331 if (ret < 0)
1332 goto fail;
1333 }
1334
1335 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
1336 session_type = SESSION_TYPE_TX;
1337 ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
1338 rtd, capture_mixer_ctl_name, suffix, session_type, channel);
1339 if (ret < 0)
1340 goto fail;
1341 }
1342 return 0;
1343
1344fail:
1345 pr_err("%s: failed add platform ctl, err = %d\n",
1346 __func__, ret);
1347
1348 return ret;
1349}
1350
1351static int msm_pcm_add_channel_mixer_controls(struct snd_soc_pcm_runtime *rtd)
1352{
1353 int i, ret = 0;
1354 struct snd_pcm *pcm = NULL;
1355 struct msm_pcm_pdata *pdata = NULL;
1356 struct snd_soc_component *component = NULL;
1357
1358 if (!rtd || !rtd->pcm) {
1359 pr_err("%s invalid rtd or pcm\n", __func__);
1360 return -EINVAL;
1361 }
1362 pcm = rtd->pcm;
1363
1364 component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
1365 if (!component) {
1366 pr_err("%s: component is NULL\n", __func__);
1367 return -EINVAL;
1368 }
1369
1370 pdata = (struct msm_pcm_pdata *)
1371 dev_get_drvdata(component->dev);
1372 if (!pdata) {
1373 pr_err("%s: platform data not populated\n", __func__);
1374 return -EINVAL;
1375 }
1376
1377 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream &&
1378 !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
1379 pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] =
1380 kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
1381 if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
1382 ret = -ENOMEM;
1383 goto fail;
1384 }
1385 }
1386
1387 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream &&
1388 !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
1389 pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] =
1390 kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
1391 if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
1392 ret = -ENOMEM;
1393 goto fail;
1394 }
1395 }
1396
1397 ret = msm_pcm_add_channel_mixer_cfg_controls(rtd);
1398 if (ret) {
1399 pr_err("%s: pcm add channel mixer cfg controls failed:%d\n",
1400 __func__, ret);
1401 goto fail;
1402 }
1403 ret = msm_pcm_add_channel_mixer_input_map_controls(rtd);
1404 if (ret) {
1405 pr_err("%s: pcm add channel mixer input map controls failed:%d\n",
1406 __func__, ret);
1407 goto fail;
1408 }
1409 ret = msm_pcm_add_channel_mixer_output_map_controls(rtd);
1410 if (ret) {
1411 pr_err("%s: pcm add channel mixer output map controls failed:%d\n",
1412 __func__, ret);
1413 goto fail;
1414 }
1415
1416 for (i = 1; i <= PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++) {
1417 ret = msm_pcm_add_channel_mixer_weight_controls(rtd, i);
1418 if (ret) {
1419 pr_err("%s: pcm add channel mixer weight controls failed:%d\n",
1420 __func__, ret);
1421 goto fail;
1422 }
1423 }
1424 return 0;
1425
1426fail:
1427 kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]);
1428 kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]);
1429 pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] = NULL;
1430 pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] = NULL;
1431
1432 return ret;
1433}
1434
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301435static int msm_pcm_add_controls(struct snd_soc_pcm_runtime *rtd)
1436{
1437 int ret = 0;
1438
1439 pr_debug("%s\n", __func__);
1440 ret = msm_pcm_add_volume_controls(rtd);
1441 if (ret)
1442 pr_err("%s: pcm add volume controls failed:%d\n",
1443 __func__, ret);
1444 ret = msm_pcm_add_app_type_controls(rtd);
1445 if (ret)
1446 pr_err("%s: pcm add app type controls failed:%d\n",
1447 __func__, ret);
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05301448
1449 ret = msm_pcm_add_channel_mixer_controls(rtd);
1450 if (ret)
1451 pr_err("%s: pcm add channel mixer controls failed:%d\n",
1452 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301453 return ret;
1454}
1455
1456static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
1457{
1458 struct snd_card *card = rtd->card->snd_card;
1459 int ret = 0;
1460
1461 if (!card->dev->coherent_dma_mask)
1462 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
1463
1464 ret = msm_pcm_add_controls(rtd);
1465 if (ret)
1466 dev_err(rtd->dev, "%s, kctl add failed\n", __func__);
1467 return ret;
1468}
1469
Meng Wangee084a02018-09-04 16:11:58 +08001470static struct snd_soc_component_driver msm_soc_component = {
1471 .name = DRV_NAME,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301472 .ops = &msm_pcm_ops,
1473 .pcm_new = msm_asoc_pcm_new,
1474 .probe = msm_pcm_loopback_probe,
1475};
1476
1477static int msm_pcm_probe(struct platform_device *pdev)
1478{
1479 struct msm_pcm_pdata *pdata;
1480
1481 dev_dbg(&pdev->dev, "%s: dev name %s\n",
1482 __func__, dev_name(&pdev->dev));
1483
1484 pdata = kzalloc(sizeof(struct msm_pcm_pdata), GFP_KERNEL);
1485 if (!pdata)
1486 return -ENOMEM;
1487
1488 if (of_property_read_bool(pdev->dev.of_node,
1489 "qcom,msm-pcm-loopback-low-latency"))
1490 pdata->perf_mode = LOW_LATENCY_PCM_MODE;
1491 else
1492 pdata->perf_mode = LEGACY_PCM_MODE;
1493
1494 dev_set_drvdata(&pdev->dev, pdata);
1495
Meng Wangee084a02018-09-04 16:11:58 +08001496 return snd_soc_register_component(&pdev->dev,
1497 &msm_soc_component,
1498 NULL, 0);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301499}
1500
1501static int msm_pcm_remove(struct platform_device *pdev)
1502{
Dhananjay Kumar807f7e92018-12-11 18:10:08 +05301503 struct msm_pcm_pdata *pdata;
1504 int i = 0;
1505
1506 pdata = dev_get_drvdata(&pdev->dev);
1507 if (pdata) {
1508 for (i = 0; i < MSM_FRONTEND_DAI_MM_SIZE; i++) {
1509 kfree(pdata->chmixer_pspd[i][SESSION_TYPE_RX]);
1510 kfree(pdata->chmixer_pspd[i][SESSION_TYPE_TX]);
1511 }
1512 }
1513 kfree(pdata);
Meng Wangee084a02018-09-04 16:11:58 +08001514 snd_soc_unregister_component(&pdev->dev);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301515 return 0;
1516}
1517
1518static const struct of_device_id msm_pcm_loopback_dt_match[] = {
1519 {.compatible = "qcom,msm-pcm-loopback"},
1520 {}
1521};
1522
1523static struct platform_driver msm_pcm_driver = {
1524 .driver = {
1525 .name = "msm-pcm-loopback",
1526 .owner = THIS_MODULE,
1527 .of_match_table = msm_pcm_loopback_dt_match,
Xiaojun Sang53cd13a2018-06-29 15:14:37 +08001528 .suppress_bind_attrs = true,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301529 },
1530 .probe = msm_pcm_probe,
1531 .remove = msm_pcm_remove,
1532};
1533
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301534int __init msm_pcm_loopback_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301535{
1536 return platform_driver_register(&msm_pcm_driver);
1537}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301538
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05301539void msm_pcm_loopback_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301540{
1541 platform_driver_unregister(&msm_pcm_driver);
1542}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301543
1544MODULE_DESCRIPTION("PCM loopback platform driver");
1545MODULE_LICENSE("GPL v2");