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