blob: 7d59fbbce66a39df7422ce709d6c3d09ef1a6098 [file] [log] [blame]
Aditya Bavanari5106b562018-01-08 13:16:32 +05301/* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/wait.h>
15#include <linux/sched.h>
16#include <linux/jiffies.h>
17#include <linux/uaccess.h>
18#include <linux/atomic.h>
19#include <linux/wait.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053020#include <sound/asound.h>
Laxminath Kasam8b1366a2017-10-05 01:44:16 +053021#include <dsp/msm-dts-srs-tm-config.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053022#include <dsp/apr_audio-v2.h>
23#include <dsp/q6adm-v2.h>
24#include <dsp/q6audio-v2.h>
25#include <dsp/q6afe-v2.h>
26#include <dsp/audio_cal_utils.h>
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -080027#include <dsp/q6common.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053028#include <ipc/apr.h>
29#include "adsp_err.h"
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053030
31#define TIMEOUT_MS 1000
32
33#define RESET_COPP_ID 99
34#define INVALID_COPP_ID 0xFF
35/* Used for inband payload copy, max size is 4k */
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -080036/* 3 is to account for module, instance & param ID in payload */
37#define ADM_GET_PARAMETER_LENGTH (4096 - APR_HDR_SIZE - 3 * sizeof(uint32_t))
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053038
39#define ULL_SUPPORTED_BITS_PER_SAMPLE 16
40#define ULL_SUPPORTED_SAMPLE_RATE 48000
41
42#ifndef CONFIG_DOLBY_DAP
43#undef DOLBY_ADM_COPP_TOPOLOGY_ID
44#define DOLBY_ADM_COPP_TOPOLOGY_ID 0xFFFFFFFE
45#endif
46
47#ifndef CONFIG_DOLBY_DS2
48#undef DS2_ADM_COPP_TOPOLOGY_ID
49#define DS2_ADM_COPP_TOPOLOGY_ID 0xFFFFFFFF
50#endif
51
52/* ENUM for adm_status */
53enum adm_cal_status {
54 ADM_STATUS_CALIBRATION_REQUIRED = 0,
55 ADM_STATUS_MAX,
56};
57
58struct adm_copp {
59
60 atomic_t id[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
61 atomic_t cnt[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
62 atomic_t topology[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
63 atomic_t mode[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
64 atomic_t stat[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
65 atomic_t rate[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
66 atomic_t bit_width[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
67 atomic_t channels[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
68 atomic_t app_type[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
69 atomic_t acdb_id[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
70 wait_queue_head_t wait[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
71 wait_queue_head_t adm_delay_wait[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
72 atomic_t adm_delay_stat[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
73 uint32_t adm_delay[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
74 unsigned long adm_status[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
75};
76
77struct source_tracking_data {
Banajit Goswami08bb7362017-11-03 22:48:23 -070078 struct dma_buf *dma_buf;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053079 struct param_outband memmap;
80 int apr_cmd_status;
81};
82
83struct adm_ctl {
84 void *apr;
85
86 struct adm_copp copp;
87
88 atomic_t matrix_map_stat;
89 wait_queue_head_t matrix_map_wait;
90
91 atomic_t adm_stat;
92 wait_queue_head_t adm_wait;
93
94 struct cal_type_data *cal_data[ADM_MAX_CAL_TYPES];
95
96 atomic_t mem_map_handles[ADM_MEM_MAP_INDEX_MAX];
97 atomic_t mem_map_index;
98
99 struct param_outband outband_memmap;
100 struct source_tracking_data sourceTrackingData;
101
102 int set_custom_topology;
103 int ec_ref_rx;
104 int num_ec_ref_rx_chans;
105 int ec_ref_rx_bit_width;
106 int ec_ref_rx_sampling_rate;
107};
108
109static struct adm_ctl this_adm;
110
111struct adm_multi_ch_map {
112 bool set_channel_map;
113 char channel_mapping[PCM_FORMAT_MAX_NUM_CHANNEL];
114};
115
116#define ADM_MCH_MAP_IDX_PLAYBACK 0
117#define ADM_MCH_MAP_IDX_REC 1
118static struct adm_multi_ch_map multi_ch_maps[2] = {
119 { false,
120 {0, 0, 0, 0, 0, 0, 0, 0}
121 },
122 { false,
123 {0, 0, 0, 0, 0, 0, 0, 0}
124 }
125};
126
127static int adm_get_parameters[MAX_COPPS_PER_PORT * ADM_GET_PARAMETER_LENGTH];
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800128static int adm_module_topo_list[MAX_COPPS_PER_PORT *
129 ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH];
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530130static struct mutex dts_srs_lock;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530131
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530132void msm_dts_srs_acquire_lock(void)
133{
134 mutex_lock(&dts_srs_lock);
135}
136
137void msm_dts_srs_release_lock(void)
138{
139 mutex_unlock(&dts_srs_lock);
140}
141
142/**
143 * adm_validate_and_get_port_index -
144 * validate given port id
145 *
146 * @port_id: Port ID number
147 *
148 * Returns valid index on success or error on failure
149 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530150int adm_validate_and_get_port_index(int port_id)
151{
152 int index;
153 int ret;
154
155 ret = q6audio_validate_port(port_id);
156 if (ret < 0) {
157 pr_err("%s: port validation failed id 0x%x ret %d\n",
158 __func__, port_id, ret);
159 return -EINVAL;
160 }
161
162 index = afe_get_port_index(port_id);
163 if (index < 0 || index >= AFE_MAX_PORTS) {
164 pr_err("%s: Invalid port idx %d port_id 0x%x\n",
165 __func__, index,
166 port_id);
167 return -EINVAL;
168 }
169 pr_debug("%s: port_idx- %d\n", __func__, index);
170 return index;
171}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530172EXPORT_SYMBOL(adm_validate_and_get_port_index);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530173
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530174/**
175 * adm_get_default_copp_idx -
176 * retrieve default copp_idx for given port
177 *
178 * @port_id: Port ID number
179 *
180 * Returns valid value on success or error on failure
181 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530182int adm_get_default_copp_idx(int port_id)
183{
184 int port_idx = adm_validate_and_get_port_index(port_id), idx;
185
186 if (port_idx < 0) {
187 pr_err("%s: Invalid port id: 0x%x", __func__, port_id);
188 return -EINVAL;
189 }
190 pr_debug("%s: port_idx:%d\n", __func__, port_idx);
191 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++) {
192 if (atomic_read(&this_adm.copp.id[port_idx][idx]) !=
193 RESET_COPP_ID)
194 return idx;
195 }
196 return -EINVAL;
197}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530198EXPORT_SYMBOL(adm_get_default_copp_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530199
200int adm_get_topology_for_port_from_copp_id(int port_id, int copp_id)
201{
202 int port_idx = adm_validate_and_get_port_index(port_id), idx;
203
204 if (port_idx < 0) {
205 pr_err("%s: Invalid port id: 0x%x", __func__, port_id);
206 return 0;
207 }
208 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++)
209 if (atomic_read(&this_adm.copp.id[port_idx][idx]) == copp_id)
210 return atomic_read(&this_adm.copp.topology[port_idx]
211 [idx]);
212 pr_err("%s: Invalid copp_id %d port_id 0x%x\n",
213 __func__, copp_id, port_id);
214 return 0;
215}
216
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530217/**
218 * adm_get_topology_for_port_copp_idx -
219 * retrieve topology of given port/copp_idx
220 *
221 * @port_id: Port ID number
222 * @copp_idx: copp index of ADM copp
223 *
224 * Returns valid value on success or 0 on failure
225 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530226int adm_get_topology_for_port_copp_idx(int port_id, int copp_idx)
227{
228 int port_idx = adm_validate_and_get_port_index(port_id);
229
230 if (port_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
231 pr_err("%s: Invalid port: 0x%x copp id: 0x%x",
232 __func__, port_id, copp_idx);
233 return 0;
234 }
235 return atomic_read(&this_adm.copp.topology[port_idx][copp_idx]);
236}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530237EXPORT_SYMBOL(adm_get_topology_for_port_copp_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530238
239int adm_get_indexes_from_copp_id(int copp_id, int *copp_idx, int *port_idx)
240{
241 int p_idx, c_idx;
242
243 for (p_idx = 0; p_idx < AFE_MAX_PORTS; p_idx++) {
244 for (c_idx = 0; c_idx < MAX_COPPS_PER_PORT; c_idx++) {
245 if (atomic_read(&this_adm.copp.id[p_idx][c_idx])
246 == copp_id) {
247 if (copp_idx != NULL)
248 *copp_idx = c_idx;
249 if (port_idx != NULL)
250 *port_idx = p_idx;
251 return 0;
252 }
253 }
254 }
255 return -EINVAL;
256}
257
258static int adm_get_copp_id(int port_idx, int copp_idx)
259{
260 pr_debug("%s: port_idx:%d copp_idx:%d\n", __func__, port_idx, copp_idx);
261
262 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
263 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
264 return -EINVAL;
265 }
266 return atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
267}
268
269static int adm_get_idx_if_copp_exists(int port_idx, int topology, int mode,
270 int rate, int bit_width, int app_type)
271{
272 int idx;
273
274 pr_debug("%s: port_idx-%d, topology-0x%x, mode-%d, rate-%d, bit_width-%d\n",
275 __func__, port_idx, topology, mode, rate, bit_width);
276
277 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++)
278 if ((topology ==
279 atomic_read(&this_adm.copp.topology[port_idx][idx])) &&
280 (mode == atomic_read(&this_adm.copp.mode[port_idx][idx])) &&
281 (rate == atomic_read(&this_adm.copp.rate[port_idx][idx])) &&
282 (bit_width ==
283 atomic_read(&this_adm.copp.bit_width[port_idx][idx])) &&
284 (app_type ==
285 atomic_read(&this_adm.copp.app_type[port_idx][idx])))
286 return idx;
287 return -EINVAL;
288}
289
290static int adm_get_next_available_copp(int port_idx)
291{
292 int idx;
293
294 pr_debug("%s:\n", __func__);
295 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++) {
296 pr_debug("%s: copp_id:0x%x port_idx:%d idx:%d\n", __func__,
297 atomic_read(&this_adm.copp.id[port_idx][idx]),
298 port_idx, idx);
299 if (atomic_read(&this_adm.copp.id[port_idx][idx]) ==
300 RESET_COPP_ID)
301 break;
302 }
303 return idx;
304}
305
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530306/**
307 * srs_trumedia_open -
308 * command to set SRS trumedia open
309 *
310 * @port_id: Port ID number
311 * @copp_idx: copp index of ADM copp
312 * @srs_tech_id: SRS tech index
313 * @srs_params: params pointer
314 *
315 * Returns 0 on success or error on failure
316 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530317int srs_trumedia_open(int port_id, int copp_idx, __s32 srs_tech_id,
318 void *srs_params)
319{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800320 struct param_hdr_v3 param_hdr;
321 struct mem_mapping_hdr mem_hdr;
322 u32 total_param_size = 0;
323 bool outband = false;
324 int port_idx;
325 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530326
327 pr_debug("SRS - %s", __func__);
328
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800329 memset(&param_hdr, 0, sizeof(param_hdr));
330 memset(&mem_hdr, 0, sizeof(mem_hdr));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530331 port_id = afe_convert_virtual_to_portid(port_id);
332 port_idx = adm_validate_and_get_port_index(port_id);
333 if (port_idx < 0) {
334 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
335 return -EINVAL;
336 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800337
338 param_hdr.module_id = SRS_TRUMEDIA_MODULE_ID;
339 param_hdr.instance_id = INSTANCE_ID_0;
340
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530341 switch (srs_tech_id) {
342 case SRS_ID_GLOBAL: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800343 param_hdr.param_id = SRS_TRUMEDIA_PARAMS;
344 param_hdr.param_size =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530345 sizeof(struct srs_trumedia_params_GLOBAL);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530346 break;
347 }
348 case SRS_ID_WOWHD: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800349 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_WOWHD;
350 param_hdr.param_size = sizeof(struct srs_trumedia_params_WOWHD);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530351 break;
352 }
353 case SRS_ID_CSHP: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800354 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_CSHP;
355 param_hdr.param_size = sizeof(struct srs_trumedia_params_CSHP);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530356 break;
357 }
358 case SRS_ID_HPF: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800359 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_HPF;
360 param_hdr.param_size = sizeof(struct srs_trumedia_params_HPF);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530361 break;
362 }
363 case SRS_ID_AEQ: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800364 u8 *update_params_ptr = (u8 *) this_adm.outband_memmap.kvaddr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530365
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800366 outband = true;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530367
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530368 if (update_params_ptr == NULL) {
369 pr_err("ADM_SRS_TRUMEDIA - %s: null memmap for AEQ params\n",
370 __func__);
371 ret = -EINVAL;
372 goto fail_cmd;
373 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530374
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800375 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_AEQ;
376 param_hdr.param_size = sizeof(struct srs_trumedia_params_AEQ);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530377
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800378 ret = q6common_pack_pp_params(update_params_ptr, &param_hdr,
379 srs_params, &total_param_size);
380 if (ret) {
381 pr_err("%s: Failed to pack param header and data, error %d\n",
382 __func__, ret);
383 goto fail_cmd;
384 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530385 break;
386 }
387 case SRS_ID_HL: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800388 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_HL;
389 param_hdr.param_size = sizeof(struct srs_trumedia_params_HL);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530390 break;
391 }
392 case SRS_ID_GEQ: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800393 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_GEQ;
394 param_hdr.param_size = sizeof(struct srs_trumedia_params_GEQ);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530395 break;
396 }
397 default:
398 goto fail_cmd;
399 }
400
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530401 if (outband && this_adm.outband_memmap.paddr) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800402 mem_hdr.data_payload_addr_lsw =
403 lower_32_bits(this_adm.outband_memmap.paddr);
404 mem_hdr.data_payload_addr_msw =
405 msm_audio_populate_upper_32_bits(
406 this_adm.outband_memmap.paddr);
407 mem_hdr.mem_map_handle = atomic_read(
408 &this_adm.mem_map_handles[ADM_SRS_TRUMEDIA]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530409
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800410 ret = adm_set_pp_params(port_id, copp_idx, &mem_hdr, NULL,
411 total_param_size);
412 } else {
413 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx,
414 param_hdr,
415 (u8 *) srs_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530416 }
417
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800418 if (ret < 0)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530419 pr_err("SRS - %s: ADM enable for port %d failed\n", __func__,
420 port_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530421
422fail_cmd:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530423 return ret;
424}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530425EXPORT_SYMBOL(srs_trumedia_open);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530426
427static int adm_populate_channel_weight(u16 *ptr,
428 struct msm_pcm_channel_mixer *ch_mixer,
429 int channel_index)
430{
431 u16 i, j, start_index = 0;
432
433 if (channel_index > ch_mixer->output_channel) {
434 pr_err("%s: channel index %d is larger than output_channel %d\n",
435 __func__, channel_index, ch_mixer->output_channel);
436 return -EINVAL;
437 }
438
439 for (i = 0; i < ch_mixer->output_channel; i++) {
440 pr_debug("%s: weight for output %d:", __func__, i);
441 for (j = 0; j < ADM_MAX_CHANNELS; j++)
442 pr_debug(" %d",
443 ch_mixer->channel_weight[i][j]);
444 pr_debug("\n");
445 }
446
447 for (i = 0; i < channel_index; ++i)
448 start_index += ch_mixer->input_channels[i];
449
450 for (i = 0; i < ch_mixer->output_channel; ++i) {
451 for (j = start_index;
452 j < start_index +
453 ch_mixer->input_channels[channel_index]; j++) {
454 *ptr = ch_mixer->channel_weight[i][j];
455 pr_debug("%s: ptr[%d][%d] = %d\n",
456 __func__, i, j, *ptr);
457 ptr++;
458 }
459 }
460
461 return 0;
462}
463
464/*
465 * adm_programable_channel_mixer
466 *
467 * Receives port_id, copp_idx, session_id, session_type, ch_mixer
468 * and channel_index to send ADM command to mix COPP data.
469 *
470 * port_id - Passed value, port_id for which backend is wanted
471 * copp_idx - Passed value, copp_idx for which COPP is wanted
472 * session_id - Passed value, session_id for which session is needed
473 * session_type - Passed value, session_type for RX or TX
474 * ch_mixer - Passed value, ch_mixer for which channel mixer config is needed
475 * channel_index - Passed value, channel_index for which channel is needed
476 */
477int adm_programable_channel_mixer(int port_id, int copp_idx, int session_id,
478 int session_type,
479 struct msm_pcm_channel_mixer *ch_mixer,
480 int channel_index)
481{
482 struct adm_cmd_set_pspd_mtmx_strtr_params_v5 *adm_params = NULL;
Vignesh Kulothungan2c6dccd2018-03-22 14:18:43 -0700483 struct param_hdr_v1 data_v5;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530484 int ret = 0, port_idx, sz = 0, param_size = 0;
485 u16 *adm_pspd_params;
486 u16 *ptr;
487 int index = 0;
488
489 pr_debug("%s: port_id = %d\n", __func__, port_id);
490 port_id = afe_convert_virtual_to_portid(port_id);
491 port_idx = adm_validate_and_get_port_index(port_id);
492 if (port_idx < 0) {
493 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
494 return -EINVAL;
495 }
496 /*
497 * First 8 bytes are 4 bytes as rule number, 2 bytes as output
498 * channel and 2 bytes as input channel.
499 * 2 * ch_mixer->output_channel means output channel mapping.
500 * 2 * ch_mixer->input_channels[channel_index]) means input
501 * channel mapping.
502 * 2 * ch_mixer->input_channels[channel_index] *
503 * ch_mixer->output_channel) means the channel mixer weighting
504 * coefficients.
505 * param_size needs to be a multiple of 4 bytes.
506 */
507
508 param_size = 2 * (4 + ch_mixer->output_channel +
509 ch_mixer->input_channels[channel_index] +
510 ch_mixer->input_channels[channel_index] *
511 ch_mixer->output_channel);
512 roundup(param_size, 4);
513
514 sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5) +
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800515 sizeof(struct default_chmixer_param_id_coeff) +
Vignesh Kulothungan2c6dccd2018-03-22 14:18:43 -0700516 sizeof(struct param_hdr_v1) + param_size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530517 pr_debug("%s: sz = %d\n", __func__, sz);
518 adm_params = kzalloc(sz, GFP_KERNEL);
519 if (!adm_params)
520 return -ENOMEM;
521
522 adm_params->payload_addr_lsw = 0;
523 adm_params->payload_addr_msw = 0;
524 adm_params->mem_map_handle = 0;
525 adm_params->direction = session_type;
526 adm_params->sessionid = session_id;
527 pr_debug("%s: copp_id = %d, session id %d\n", __func__,
528 atomic_read(&this_adm.copp.id[port_idx][copp_idx]),
529 session_id);
530 adm_params->deviceid = atomic_read(
531 &this_adm.copp.id[port_idx][copp_idx]);
532 adm_params->reserved = 0;
533
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800534 /*
535 * This module is internal to ADSP and cannot be configured with
536 * an instance id
537 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530538 data_v5.module_id = MTMX_MODULE_ID_DEFAULT_CHMIXER;
539 data_v5.param_id = DEFAULT_CHMIXER_PARAM_ID_COEFF;
540 data_v5.reserved = 0;
541 data_v5.param_size = param_size;
542 adm_params->payload_size =
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800543 sizeof(struct default_chmixer_param_id_coeff) +
Vignesh Kulothungan2c6dccd2018-03-22 14:18:43 -0700544 sizeof(struct param_hdr_v1) + data_v5.param_size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530545 adm_pspd_params = (u16 *)((u8 *)adm_params +
546 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5));
547 memcpy(adm_pspd_params, &data_v5, sizeof(data_v5));
548
549 adm_pspd_params = (u16 *)((u8 *)adm_params +
550 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5)
551 + sizeof(data_v5));
552
553 adm_pspd_params[0] = ch_mixer->rule;
554 adm_pspd_params[2] = ch_mixer->output_channel;
555 adm_pspd_params[3] = ch_mixer->input_channels[channel_index];
556 index = 4;
557
558 if (ch_mixer->output_channel == 1) {
559 adm_pspd_params[index] = PCM_CHANNEL_FC;
560 } else if (ch_mixer->output_channel == 2) {
561 adm_pspd_params[index] = PCM_CHANNEL_FL;
562 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
563 } else if (ch_mixer->output_channel == 3) {
564 adm_pspd_params[index] = PCM_CHANNEL_FL;
565 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
566 adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
567 } else if (ch_mixer->output_channel == 4) {
568 adm_pspd_params[index] = PCM_CHANNEL_FL;
569 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
570 adm_pspd_params[index + 2] = PCM_CHANNEL_LS;
571 adm_pspd_params[index + 3] = PCM_CHANNEL_RS;
572 } else if (ch_mixer->output_channel == 5) {
573 adm_pspd_params[index] = PCM_CHANNEL_FL;
574 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
575 adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
576 adm_pspd_params[index + 3] = PCM_CHANNEL_LS;
577 adm_pspd_params[index + 4] = PCM_CHANNEL_RS;
578 } else if (ch_mixer->output_channel == 6) {
579 adm_pspd_params[index] = PCM_CHANNEL_FL;
580 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
581 adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
582 adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
583 adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
584 adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
585 } else if (ch_mixer->output_channel == 8) {
586 adm_pspd_params[index] = PCM_CHANNEL_FL;
587 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
588 adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
589 adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
590 adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
591 adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
592 adm_pspd_params[index + 6] = PCM_CHANNEL_LB;
593 adm_pspd_params[index + 7] = PCM_CHANNEL_RB;
594 }
595
596 index = index + ch_mixer->output_channel;
597 if (ch_mixer->input_channels[channel_index] == 1) {
598 adm_pspd_params[index] = PCM_CHANNEL_FC;
599 } else if (ch_mixer->input_channels[channel_index] == 2) {
600 adm_pspd_params[index] = PCM_CHANNEL_FL;
601 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
602 } else if (ch_mixer->input_channels[channel_index] == 3) {
603 adm_pspd_params[index] = PCM_CHANNEL_FL;
604 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
605 adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
606 } else if (ch_mixer->input_channels[channel_index] == 4) {
607 adm_pspd_params[index] = PCM_CHANNEL_FL;
608 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
609 adm_pspd_params[index + 2] = PCM_CHANNEL_LS;
610 adm_pspd_params[index + 3] = PCM_CHANNEL_RS;
611 } else if (ch_mixer->input_channels[channel_index] == 5) {
612 adm_pspd_params[index] = PCM_CHANNEL_FL;
613 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
614 adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
615 adm_pspd_params[index + 3] = PCM_CHANNEL_LS;
616 adm_pspd_params[index + 4] = PCM_CHANNEL_RS;
617 } else if (ch_mixer->input_channels[channel_index] == 6) {
618 adm_pspd_params[index] = PCM_CHANNEL_FL;
619 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
620 adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
621 adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
622 adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
623 adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
624 } else if (ch_mixer->input_channels[channel_index] == 8) {
625 adm_pspd_params[index] = PCM_CHANNEL_FL;
626 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
627 adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
628 adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
629 adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
630 adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
631 adm_pspd_params[index + 6] = PCM_CHANNEL_LB;
632 adm_pspd_params[index + 7] = PCM_CHANNEL_RB;
633 }
634
635 index = index + ch_mixer->input_channels[channel_index];
636 ret = adm_populate_channel_weight(&adm_pspd_params[index],
637 ch_mixer, channel_index);
638 if (!ret) {
639 pr_err("%s: fail to get channel weight with error %d\n",
640 __func__, ret);
641 goto fail_cmd;
642 }
643
644 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
645 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
646 adm_params->hdr.src_svc = APR_SVC_ADM;
647 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
648 adm_params->hdr.src_port = port_id;
649 adm_params->hdr.dest_svc = APR_SVC_ADM;
650 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
651 adm_params->hdr.dest_port =
652 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
653 adm_params->hdr.token = port_idx << 16 | copp_idx;
654 adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5;
655 adm_params->hdr.pkt_size = sz;
656 adm_params->payload_addr_lsw = 0;
657 adm_params->payload_addr_msw = 0;
658 adm_params->mem_map_handle = 0;
659 adm_params->reserved = 0;
660
661 ptr = (u16 *)adm_params;
662 for (index = 0; index < (sz / 2); index++)
663 pr_debug("%s: adm_params[%d] = 0x%x\n",
664 __func__, index, (unsigned int)ptr[index]);
665
666 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], 0);
667 ret = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
668 if (ret < 0) {
669 pr_err("%s: Set params failed port %d rc %d\n", __func__,
670 port_id, ret);
671 ret = -EINVAL;
672 goto fail_cmd;
673 }
674
675 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
676 atomic_read(
677 &this_adm.copp.stat[port_idx][copp_idx]) >= 0,
678 msecs_to_jiffies(TIMEOUT_MS));
679 if (!ret) {
680 pr_err("%s: set params timed out port = %d\n",
681 __func__, port_id);
682 ret = -ETIMEDOUT;
683 goto fail_cmd;
684 }
685 ret = 0;
686fail_cmd:
687 kfree(adm_params);
688
689 return ret;
690}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530691EXPORT_SYMBOL(adm_programable_channel_mixer);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530692
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530693/**
694 * adm_set_stereo_to_custom_stereo -
695 * command to update custom stereo
696 *
697 * @port_id: Port ID number
698 * @copp_idx: copp index of ADM copp
699 * @session_id: session id to be updated
700 * @params: params pointer
701 * @param_length: length of params
702 *
703 * Returns 0 on success or error on failure
704 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530705int adm_set_stereo_to_custom_stereo(int port_id, int copp_idx,
706 unsigned int session_id, char *params,
707 uint32_t params_length)
708{
709 struct adm_cmd_set_pspd_mtmx_strtr_params_v5 *adm_params = NULL;
710 int sz, rc = 0, port_idx;
711
712 pr_debug("%s:\n", __func__);
713 port_id = afe_convert_virtual_to_portid(port_id);
714 port_idx = adm_validate_and_get_port_index(port_id);
715 if (port_idx < 0) {
716 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
717 return -EINVAL;
718 }
719
720 sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5) +
721 params_length;
722 adm_params = kzalloc(sz, GFP_KERNEL);
723 if (!adm_params) {
724 pr_err("%s, adm params memory alloc failed\n", __func__);
725 return -ENOMEM;
726 }
727
728 memcpy(((u8 *)adm_params +
729 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5)),
730 params, params_length);
731 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
732 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
733 adm_params->hdr.pkt_size = sz;
734 adm_params->hdr.src_svc = APR_SVC_ADM;
735 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
736 adm_params->hdr.src_port = port_id;
737 adm_params->hdr.dest_svc = APR_SVC_ADM;
738 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
739 adm_params->hdr.dest_port = 0; /* Ignored */;
740 adm_params->hdr.token = port_idx << 16 | copp_idx;
741 adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5;
742 adm_params->payload_addr_lsw = 0;
743 adm_params->payload_addr_msw = 0;
744 adm_params->mem_map_handle = 0;
745 adm_params->payload_size = params_length;
746 /* direction RX as 0 */
747 adm_params->direction = ADM_MATRIX_ID_AUDIO_RX;
748 /* session id for this cmd to be applied on */
749 adm_params->sessionid = session_id;
750 adm_params->deviceid =
751 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
752 adm_params->reserved = 0;
753 pr_debug("%s: deviceid %d, session_id %d, src_port %d, dest_port %d\n",
754 __func__, adm_params->deviceid, adm_params->sessionid,
755 adm_params->hdr.src_port, adm_params->hdr.dest_port);
756 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
757 rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
758 if (rc < 0) {
759 pr_err("%s: Set params failed port = 0x%x rc %d\n",
760 __func__, port_id, rc);
761 rc = -EINVAL;
762 goto set_stereo_to_custom_stereo_return;
763 }
764 /* Wait for the callback */
765 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
766 atomic_read(&this_adm.copp.stat
767 [port_idx][copp_idx]) >= 0,
768 msecs_to_jiffies(TIMEOUT_MS));
769 if (!rc) {
770 pr_err("%s: Set params timed out port = 0x%x\n", __func__,
771 port_id);
772 rc = -EINVAL;
773 goto set_stereo_to_custom_stereo_return;
774 } else if (atomic_read(&this_adm.copp.stat
775 [port_idx][copp_idx]) > 0) {
776 pr_err("%s: DSP returned error[%s]\n", __func__,
777 adsp_err_get_err_str(atomic_read(
778 &this_adm.copp.stat
779 [port_idx][copp_idx])));
780 rc = adsp_err_get_lnx_err_code(
781 atomic_read(&this_adm.copp.stat
782 [port_idx][copp_idx]));
783 goto set_stereo_to_custom_stereo_return;
784 }
785 rc = 0;
786set_stereo_to_custom_stereo_return:
787 kfree(adm_params);
788 return rc;
789}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530790EXPORT_SYMBOL(adm_set_stereo_to_custom_stereo);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530791
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800792/*
793 * With pre-packed data, only the opcode differes from V5 and V6.
794 * Use q6common_pack_pp_params to pack the data correctly.
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530795 */
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800796int adm_set_pp_params(int port_id, int copp_idx,
797 struct mem_mapping_hdr *mem_hdr, u8 *param_data,
798 u32 param_size)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530799{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800800 struct adm_cmd_set_pp_params *adm_set_params = NULL;
801 int size = 0;
802 int port_idx = 0;
803 atomic_t *copp_stat = NULL;
804 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530805
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530806 port_id = afe_convert_virtual_to_portid(port_id);
807 port_idx = adm_validate_and_get_port_index(port_id);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800808 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
809 pr_err("%s: Invalid port_idx 0x%x\n", __func__, port_idx);
810 return -EINVAL;
811 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
812 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530813 return -EINVAL;
814 }
815
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800816 /* Only add params_size in inband case */
817 size = sizeof(struct adm_cmd_set_pp_params);
818 if (param_data != NULL)
819 size += param_size;
820 adm_set_params = kzalloc(size, GFP_KERNEL);
821 if (!adm_set_params)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530822 return -ENOMEM;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800823
824 adm_set_params->apr_hdr.hdr_field =
825 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
826 APR_PKT_VER);
827 adm_set_params->apr_hdr.pkt_size = size;
828 adm_set_params->apr_hdr.src_svc = APR_SVC_ADM;
829 adm_set_params->apr_hdr.src_domain = APR_DOMAIN_APPS;
830 adm_set_params->apr_hdr.src_port = port_id;
831 adm_set_params->apr_hdr.dest_svc = APR_SVC_ADM;
832 adm_set_params->apr_hdr.dest_domain = APR_DOMAIN_ADSP;
833 adm_set_params->apr_hdr.dest_port =
834 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
835 adm_set_params->apr_hdr.token = port_idx << 16 | copp_idx;
836
837 if (q6common_is_instance_id_supported())
838 adm_set_params->apr_hdr.opcode = ADM_CMD_SET_PP_PARAMS_V6;
839 else
840 adm_set_params->apr_hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
841
842 adm_set_params->payload_size = param_size;
843
844 if (mem_hdr != NULL) {
845 /* Out of Band Case */
846 adm_set_params->mem_hdr = *mem_hdr;
847 } else if (param_data != NULL) {
848 /*
849 * In band case. Parameter data must be pre-packed with its
850 * header before calling this function. Use
851 * q6common_pack_pp_params to pack parameter data and header
852 * correctly.
853 */
854 memcpy(&adm_set_params->param_data, param_data, param_size);
855 } else {
856 pr_err("%s: Received NULL pointers for both memory header and param data\n",
857 __func__);
858 ret = -EINVAL;
859 goto done;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530860 }
861
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800862 copp_stat = &this_adm.copp.stat[port_idx][copp_idx];
863 atomic_set(copp_stat, -1);
864 ret = apr_send_pkt(this_adm.apr, (uint32_t *) adm_set_params);
865 if (ret < 0) {
866 pr_err("%s: Set params APR send failed port = 0x%x ret %d\n",
867 __func__, port_id, ret);
868 goto done;
869 }
870 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
871 atomic_read(copp_stat) >= 0,
872 msecs_to_jiffies(TIMEOUT_MS));
873 if (!ret) {
874 pr_err("%s: Set params timed out port = 0x%x\n", __func__,
875 port_id);
876 ret = -ETIMEDOUT;
877 goto done;
878 }
879 if (atomic_read(copp_stat) > 0) {
880 pr_err("%s: DSP returned error[%s]\n", __func__,
881 adsp_err_get_err_str(atomic_read(copp_stat)));
882 ret = adsp_err_get_lnx_err_code(atomic_read(copp_stat));
883 goto done;
884 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530885
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800886 ret = 0;
887done:
888 kfree(adm_set_params);
889 return ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530890}
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800891EXPORT_SYMBOL(adm_set_pp_params);
892
893int adm_pack_and_set_one_pp_param(int port_id, int copp_idx,
894 struct param_hdr_v3 param_hdr, u8 *param_data)
895{
896 u8 *packed_data = NULL;
897 u32 total_size = 0;
898 int ret = 0;
899
900 total_size = sizeof(union param_hdrs) + param_hdr.param_size;
901 packed_data = kzalloc(total_size, GFP_KERNEL);
902 if (!packed_data)
903 return -ENOMEM;
904
905 ret = q6common_pack_pp_params(packed_data, &param_hdr, param_data,
906 &total_size);
907 if (ret) {
908 pr_err("%s: Failed to pack parameter data, error %d\n",
909 __func__, ret);
910 goto done;
911 }
912
913 ret = adm_set_pp_params(port_id, copp_idx, NULL, packed_data,
914 total_size);
915 if (ret)
916 pr_err("%s: Failed to set parameter data, error %d\n", __func__,
917 ret);
918done:
919 kfree(packed_data);
920 return ret;
921}
922EXPORT_SYMBOL(adm_pack_and_set_one_pp_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530923
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800924/*
925 * Only one parameter can be requested at a time. Therefore, packing and sending
926 * the request can be handled locally.
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530927 */
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800928int adm_get_pp_params(int port_id, int copp_idx, uint32_t client_id,
929 struct mem_mapping_hdr *mem_hdr,
930 struct param_hdr_v3 *param_hdr, u8 *returned_param_data)
931{
932 struct adm_cmd_get_pp_params adm_get_params;
933 int total_size = 0;
934 int get_param_array_sz = ARRAY_SIZE(adm_get_parameters);
935 int returned_param_size = 0;
936 int returned_param_size_in_bytes = 0;
937 int port_idx = 0;
938 int idx = 0;
939 atomic_t *copp_stat = NULL;
940 int ret = 0;
941
942 if (param_hdr == NULL) {
943 pr_err("%s: Received NULL pointer for parameter header\n",
944 __func__);
945 return -EINVAL;
946 }
947
948 port_id = afe_convert_virtual_to_portid(port_id);
949 port_idx = adm_validate_and_get_port_index(port_id);
950 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
951 pr_err("%s: Invalid port_idx 0x%x\n", __func__, port_idx);
952 return -EINVAL;
953 }
954 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
955 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
956 return -EINVAL;
957 }
958
959 memset(&adm_get_params, 0, sizeof(adm_get_params));
960
961 if (mem_hdr != NULL)
962 adm_get_params.mem_hdr = *mem_hdr;
963
964 q6common_pack_pp_params((u8 *) &adm_get_params.param_hdr, param_hdr,
965 NULL, &total_size);
966
967 /* Pack APR header after filling body so total_size has correct value */
968 adm_get_params.apr_hdr.pkt_size = total_size;
969 adm_get_params.apr_hdr.src_svc = APR_SVC_ADM;
970 adm_get_params.apr_hdr.src_domain = APR_DOMAIN_APPS;
971 adm_get_params.apr_hdr.src_port = port_id;
972 adm_get_params.apr_hdr.dest_svc = APR_SVC_ADM;
973 adm_get_params.apr_hdr.dest_domain = APR_DOMAIN_ADSP;
974 adm_get_params.apr_hdr.dest_port =
975 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
976 adm_get_params.apr_hdr.token =
977 port_idx << 16 | client_id << 8 | copp_idx;
978
979 if (q6common_is_instance_id_supported())
980 adm_get_params.apr_hdr.opcode = ADM_CMD_GET_PP_PARAMS_V6;
981 else
982 adm_get_params.apr_hdr.opcode = ADM_CMD_GET_PP_PARAMS_V5;
983
984 copp_stat = &this_adm.copp.stat[port_idx][copp_idx];
985 atomic_set(copp_stat, -1);
986 ret = apr_send_pkt(this_adm.apr, (uint32_t *) &adm_get_params);
987 if (ret) {
988 pr_err("%s: Get params APR send failed port = 0x%x ret %d\n",
989 __func__, port_id, ret);
990 ret = -EINVAL;
991 goto done;
992 }
993 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
994 atomic_read(copp_stat) >= 0,
995 msecs_to_jiffies(TIMEOUT_MS));
996 if (!ret) {
997 pr_err("%s: Get params timed out port = 0x%x\n", __func__,
998 port_id);
999 ret = -ETIMEDOUT;
1000 goto done;
1001 }
1002 if (atomic_read(copp_stat) > 0) {
1003 pr_err("%s: DSP returned error[%s]\n", __func__,
1004 adsp_err_get_err_str(atomic_read(copp_stat)));
1005 ret = adsp_err_get_lnx_err_code(atomic_read(copp_stat));
1006 goto done;
1007 }
1008
1009 ret = 0;
1010
1011 /* Copy data to caller if sent in band */
1012 if (!returned_param_data) {
1013 pr_debug("%s: Received NULL pointer for param destination, not copying payload\n",
1014 __func__);
1015 return 0;
1016 }
1017
1018 idx = ADM_GET_PARAMETER_LENGTH * copp_idx;
1019 returned_param_size = adm_get_parameters[idx];
1020 if (returned_param_size < 0 ||
1021 returned_param_size + idx + 1 > get_param_array_sz) {
1022 pr_err("%s: Invalid parameter size %d\n", __func__,
1023 returned_param_size);
1024 return -EINVAL;
1025 }
1026
1027 returned_param_size_in_bytes = returned_param_size * sizeof(uint32_t);
1028 if (param_hdr->param_size < returned_param_size_in_bytes) {
1029 pr_err("%s: Provided buffer is not big enough, provided buffer size(%d) size needed(%d)\n",
1030 __func__, param_hdr->param_size,
1031 returned_param_size_in_bytes);
1032 return -EINVAL;
1033 }
1034
1035 memcpy(returned_param_data, &adm_get_parameters[idx + 1],
1036 returned_param_size_in_bytes);
1037done:
1038 return ret;
1039}
1040EXPORT_SYMBOL(adm_get_pp_params);
1041
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001042int adm_get_pp_topo_module_list_v2(int port_id, int copp_idx,
1043 int32_t param_length,
1044 int32_t *returned_params)
1045{
1046 struct adm_cmd_get_pp_topo_module_list adm_get_module_list;
1047 bool iid_supported = q6common_is_instance_id_supported();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301048 int *topo_list;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001049 int num_modules = 0;
1050 int list_size = 0;
1051 int port_idx, idx;
1052 int i = 0;
1053 atomic_t *copp_stat = NULL;
1054 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301055
1056 pr_debug("%s : port_id %x", __func__, port_id);
1057 port_id = afe_convert_virtual_to_portid(port_id);
1058 port_idx = adm_validate_and_get_port_index(port_id);
1059 if (port_idx < 0) {
1060 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
1061 return -EINVAL;
1062 }
1063
1064 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
1065 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
1066 return -EINVAL;
1067 }
1068
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001069 memset(&adm_get_module_list, 0, sizeof(adm_get_module_list));
1070
1071 adm_get_module_list.apr_hdr.pkt_size = sizeof(adm_get_module_list);
1072 adm_get_module_list.apr_hdr.src_svc = APR_SVC_ADM;
1073 adm_get_module_list.apr_hdr.src_domain = APR_DOMAIN_APPS;
1074 adm_get_module_list.apr_hdr.src_port = port_id;
1075 adm_get_module_list.apr_hdr.dest_svc = APR_SVC_ADM;
1076 adm_get_module_list.apr_hdr.dest_domain = APR_DOMAIN_ADSP;
1077 adm_get_module_list.apr_hdr.dest_port =
1078 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
1079 adm_get_module_list.apr_hdr.token = port_idx << 16 | copp_idx;
1080 /*
1081 * Out of band functionality is not currently utilized.
1082 * Assume in band.
1083 */
1084 if (iid_supported) {
1085 adm_get_module_list.apr_hdr.opcode =
1086 ADM_CMD_GET_PP_TOPO_MODULE_LIST_V2;
1087 adm_get_module_list.param_max_size = param_length;
1088 } else {
1089 adm_get_module_list.apr_hdr.opcode =
1090 ADM_CMD_GET_PP_TOPO_MODULE_LIST;
1091
1092 if (param_length > U16_MAX) {
1093 pr_err("%s: Invalid param length for V1 %d\n", __func__,
1094 param_length);
1095 return -EINVAL;
1096 }
1097 adm_get_module_list.param_max_size = param_length << 16;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301098 }
1099
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001100 copp_stat = &this_adm.copp.stat[port_idx][copp_idx];
1101 atomic_set(copp_stat, -1);
1102 ret = apr_send_pkt(this_adm.apr, (uint32_t *) &adm_get_module_list);
1103 if (ret) {
1104 pr_err("%s: APR send pkt failed for port_id: 0x%x failed ret %d\n",
1105 __func__, port_id, ret);
1106 ret = -EINVAL;
1107 goto done;
1108 }
1109 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
1110 atomic_read(copp_stat) >= 0,
1111 msecs_to_jiffies(TIMEOUT_MS));
1112 if (!ret) {
1113 pr_err("%s: Timeout for port_id: 0x%x\n", __func__, port_id);
1114 ret = -ETIMEDOUT;
1115 goto done;
1116 }
1117 if (atomic_read(copp_stat) > 0) {
1118 pr_err("%s: DSP returned error[%s]\n", __func__,
1119 adsp_err_get_err_str(atomic_read(copp_stat)));
1120 ret = adsp_err_get_lnx_err_code(atomic_read(copp_stat));
1121 goto done;
1122 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301123
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001124 ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301125
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001126 if (returned_params) {
1127 /*
1128 * When processing ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST IID is
1129 * added since it is not present. Therefore, there is no need to
1130 * do anything different if IID is not supported here as it is
1131 * already taken care of.
1132 */
1133 idx = ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH * copp_idx;
1134 num_modules = adm_module_topo_list[idx];
1135 if (num_modules < 0 || num_modules > MAX_MODULES_IN_TOPO) {
1136 pr_err("%s: Invalid number of modules returned %d\n",
1137 __func__, num_modules);
1138 return -EINVAL;
1139 }
1140
1141 list_size = num_modules * sizeof(struct module_instance_info);
1142 if (param_length < list_size) {
1143 pr_err("%s: Provided buffer not big enough to hold module-instance list, provided size %d, needed size %d\n",
1144 __func__, param_length, list_size);
1145 return -EINVAL;
1146 }
1147
1148 topo_list = (int32_t *) (&adm_module_topo_list[idx]);
1149 memcpy(returned_params, topo_list, list_size);
1150 for (i = 1; i <= num_modules; i += 2) {
1151 pr_debug("module = 0x%x instance = 0x%x\n",
1152 returned_params[i], returned_params[i + 1]);
1153 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301154 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001155done:
1156 return ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301157}
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001158EXPORT_SYMBOL(adm_get_pp_topo_module_list_v2);
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301159
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301160static void adm_callback_debug_print(struct apr_client_data *data)
1161{
1162 uint32_t *payload;
1163
1164 payload = data->payload;
1165
1166 if (data->payload_size >= 8)
1167 pr_debug("%s: code = 0x%x PL#0[0x%x], PL#1[0x%x], size = %d\n",
1168 __func__, data->opcode, payload[0], payload[1],
1169 data->payload_size);
1170 else if (data->payload_size >= 4)
1171 pr_debug("%s: code = 0x%x PL#0[0x%x], size = %d\n",
1172 __func__, data->opcode, payload[0],
1173 data->payload_size);
1174 else
1175 pr_debug("%s: code = 0x%x, size = %d\n",
1176 __func__, data->opcode, data->payload_size);
1177}
1178
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301179/**
1180 * adm_set_multi_ch_map -
1181 * Update multi channel map info
1182 *
1183 * @channel_map: pointer with channel map info
1184 * @path: direction or ADM path type
1185 *
1186 * Returns 0 on success or error on failure
1187 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301188int adm_set_multi_ch_map(char *channel_map, int path)
1189{
1190 int idx;
1191
1192 if (path == ADM_PATH_PLAYBACK) {
1193 idx = ADM_MCH_MAP_IDX_PLAYBACK;
1194 } else if (path == ADM_PATH_LIVE_REC) {
1195 idx = ADM_MCH_MAP_IDX_REC;
1196 } else {
1197 pr_err("%s: invalid attempt to set path %d\n", __func__, path);
1198 return -EINVAL;
1199 }
1200
1201 memcpy(multi_ch_maps[idx].channel_mapping, channel_map,
1202 PCM_FORMAT_MAX_NUM_CHANNEL);
1203 multi_ch_maps[idx].set_channel_map = true;
1204
1205 return 0;
1206}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301207EXPORT_SYMBOL(adm_set_multi_ch_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301208
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301209/**
1210 * adm_get_multi_ch_map -
1211 * Retrieves multi channel map info
1212 *
1213 * @channel_map: pointer to be updated with channel map
1214 * @path: direction or ADM path type
1215 *
1216 * Returns 0 on success or error on failure
1217 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301218int adm_get_multi_ch_map(char *channel_map, int path)
1219{
1220 int idx;
1221
1222 if (path == ADM_PATH_PLAYBACK) {
1223 idx = ADM_MCH_MAP_IDX_PLAYBACK;
1224 } else if (path == ADM_PATH_LIVE_REC) {
1225 idx = ADM_MCH_MAP_IDX_REC;
1226 } else {
1227 pr_err("%s: invalid attempt to get path %d\n", __func__, path);
1228 return -EINVAL;
1229 }
1230
1231 if (multi_ch_maps[idx].set_channel_map) {
1232 memcpy(channel_map, multi_ch_maps[idx].channel_mapping,
1233 PCM_FORMAT_MAX_NUM_CHANNEL);
1234 }
1235
1236 return 0;
1237}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301238EXPORT_SYMBOL(adm_get_multi_ch_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301239
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001240static int adm_process_get_param_response(u32 opcode, u32 idx, u32 *payload,
1241 u32 payload_size)
1242{
1243 struct adm_cmd_rsp_get_pp_params_v5 *v5_rsp = NULL;
1244 struct adm_cmd_rsp_get_pp_params_v6 *v6_rsp = NULL;
1245 u32 *param_data = NULL;
1246 int data_size = 0;
1247 int struct_size = 0;
1248
1249 if (payload == NULL) {
1250 pr_err("%s: Payload is NULL\n", __func__);
1251 return -EINVAL;
1252 }
1253
1254 switch (opcode) {
1255 case ADM_CMDRSP_GET_PP_PARAMS_V5:
1256 struct_size = sizeof(struct adm_cmd_rsp_get_pp_params_v5);
1257 v5_rsp = (struct adm_cmd_rsp_get_pp_params_v5 *) payload;
1258 data_size = v5_rsp->param_hdr.param_size;
1259 param_data = v5_rsp->param_data;
1260 break;
1261 case ADM_CMDRSP_GET_PP_PARAMS_V6:
1262 struct_size = sizeof(struct adm_cmd_rsp_get_pp_params_v6);
1263 v6_rsp = (struct adm_cmd_rsp_get_pp_params_v6 *) payload;
1264 data_size = v6_rsp->param_hdr.param_size;
1265 param_data = v6_rsp->param_data;
1266 break;
1267 default:
1268 pr_err("%s: Invalid opcode %d\n", __func__, opcode);
1269 return -EINVAL;
1270 }
1271
1272 /*
1273 * Just store the returned parameter data, not the header. The calling
1274 * function is expected to know what it asked for. Therefore, there is
1275 * no difference between V5 and V6.
1276 */
1277 if ((payload_size >= struct_size + data_size) &&
1278 (ARRAY_SIZE(adm_get_parameters) > idx) &&
1279 (ARRAY_SIZE(adm_get_parameters) >= idx + 1 + data_size)) {
1280 /*
1281 * data_size is expressed in number of bytes, store in number of
1282 * ints
1283 */
1284 adm_get_parameters[idx] =
1285 data_size / sizeof(*adm_get_parameters);
1286 pr_debug("%s: GET_PP PARAM: received parameter length: 0x%x\n",
1287 __func__, adm_get_parameters[idx]);
1288 /* store params after param_size */
1289 memcpy(&adm_get_parameters[idx + 1], param_data, data_size);
1290 return 0;
1291 }
1292
1293 pr_err("%s: Invalid parameter combination, payload_size %d, idx %d\n",
1294 __func__, payload_size, idx);
1295 return -EINVAL;
1296}
1297
1298static int adm_process_get_topo_list_response(u32 opcode, int copp_idx,
1299 u32 num_modules, u32 *payload,
1300 u32 payload_size)
1301{
1302 u32 *fill_list = NULL;
1303 int idx = 0;
1304 int i = 0;
1305 int j = 0;
1306
1307 if (payload == NULL) {
1308 pr_err("%s: Payload is NULL\n", __func__);
1309 return -EINVAL;
1310 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
1311 pr_err("%s: Invalid COPP index %d\n", __func__, copp_idx);
1312 return -EINVAL;
1313 }
1314
1315 idx = ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH * copp_idx;
1316 fill_list = adm_module_topo_list + idx;
1317 *fill_list++ = num_modules;
1318 for (i = 0; i < num_modules; i++) {
1319 if (j > payload_size / sizeof(u32)) {
1320 pr_err("%s: Invalid number of modules specified %d\n",
1321 __func__, num_modules);
1322 return -EINVAL;
1323 }
1324
1325 /* store module ID */
1326 *fill_list++ = payload[j];
1327 j++;
1328
1329 switch (opcode) {
1330 case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST_V2:
1331 /* store instance ID */
1332 *fill_list++ = payload[j];
1333 j++;
1334 break;
1335 case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST:
1336 /* Insert IID 0 when repacking */
1337 *fill_list++ = INSTANCE_ID_0;
1338 break;
1339 default:
1340 pr_err("%s: Invalid opcode %d\n", __func__, opcode);
1341 return -EINVAL;
1342 }
1343 }
1344
1345 return 0;
1346}
1347
Laxminath Kasam30ad7512017-11-28 12:40:22 +05301348static void adm_reset_data(void)
1349{
1350 int i, j;
1351
1352 apr_reset(this_adm.apr);
1353 for (i = 0; i < AFE_MAX_PORTS; i++) {
1354 for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
1355 atomic_set(&this_adm.copp.id[i][j],
1356 RESET_COPP_ID);
1357 atomic_set(&this_adm.copp.cnt[i][j], 0);
1358 atomic_set(
1359 &this_adm.copp.topology[i][j], 0);
1360 atomic_set(&this_adm.copp.mode[i][j],
1361 0);
1362 atomic_set(&this_adm.copp.stat[i][j],
1363 0);
1364 atomic_set(&this_adm.copp.rate[i][j],
1365 0);
1366 atomic_set(
1367 &this_adm.copp.channels[i][j],
1368 0);
1369 atomic_set(
1370 &this_adm.copp.bit_width[i][j], 0);
1371 atomic_set(
1372 &this_adm.copp.app_type[i][j], 0);
1373 atomic_set(
1374 &this_adm.copp.acdb_id[i][j], 0);
1375 this_adm.copp.adm_status[i][j] =
1376 ADM_STATUS_CALIBRATION_REQUIRED;
1377 }
1378 }
1379 this_adm.apr = NULL;
1380 cal_utils_clear_cal_block_q6maps(ADM_MAX_CAL_TYPES,
1381 this_adm.cal_data);
1382 mutex_lock(&this_adm.cal_data
1383 [ADM_CUSTOM_TOP_CAL]->lock);
1384 this_adm.set_custom_topology = 1;
1385 mutex_unlock(&this_adm.cal_data[
1386 ADM_CUSTOM_TOP_CAL]->lock);
1387 rtac_clear_mapping(ADM_RTAC_CAL);
1388 /*
1389 * Free the ION memory and clear the map handles
1390 * for Source Tracking
1391 */
1392 if (this_adm.sourceTrackingData.memmap.paddr != 0) {
1393 msm_audio_ion_free(
1394 this_adm.sourceTrackingData.dma_buf);
1395 this_adm.sourceTrackingData.dma_buf = NULL;
1396 this_adm.sourceTrackingData.memmap.size = 0;
1397 this_adm.sourceTrackingData.memmap.kvaddr =
1398 NULL;
1399 this_adm.sourceTrackingData.memmap.paddr = 0;
1400 this_adm.sourceTrackingData.apr_cmd_status = -1;
1401 atomic_set(&this_adm.mem_map_handles[
1402 ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
1403 }
1404}
1405
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301406static int32_t adm_callback(struct apr_client_data *data, void *priv)
1407{
1408 uint32_t *payload;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001409 int port_idx, copp_idx, idx, client_id;
1410 int num_modules;
1411 int ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301412
1413 if (data == NULL) {
1414 pr_err("%s: data parameter is null\n", __func__);
1415 return -EINVAL;
1416 }
1417
1418 payload = data->payload;
1419
1420 if (data->opcode == RESET_EVENTS) {
1421 pr_debug("%s: Reset event is received: %d %d apr[%pK]\n",
1422 __func__,
1423 data->reset_event, data->reset_proc, this_adm.apr);
Laxminath Kasam30ad7512017-11-28 12:40:22 +05301424 if (this_adm.apr)
1425 adm_reset_data();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301426 return 0;
1427 }
1428
1429 adm_callback_debug_print(data);
1430 if (data->payload_size) {
1431 copp_idx = (data->token) & 0XFF;
1432 port_idx = ((data->token) >> 16) & 0xFF;
1433 client_id = ((data->token) >> 8) & 0xFF;
1434 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
1435 pr_err("%s: Invalid port idx %d token %d\n",
1436 __func__, port_idx, data->token);
1437 return 0;
1438 }
1439 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
1440 pr_err("%s: Invalid copp idx %d token %d\n",
1441 __func__, copp_idx, data->token);
1442 return 0;
1443 }
1444 if (client_id < 0 || client_id >= ADM_CLIENT_ID_MAX) {
1445 pr_err("%s: Invalid client id %d\n", __func__,
1446 client_id);
1447 return 0;
1448 }
1449 if (data->opcode == APR_BASIC_RSP_RESULT) {
1450 pr_debug("%s: APR_BASIC_RSP_RESULT id 0x%x\n",
1451 __func__, payload[0]);
1452 if (payload[1] != 0) {
1453 pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
1454 __func__, payload[0], payload[1]);
1455 }
1456 switch (payload[0]) {
1457 case ADM_CMD_SET_PP_PARAMS_V5:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001458 case ADM_CMD_SET_PP_PARAMS_V6:
1459 pr_debug("%s: ADM_CMD_SET_PP_PARAMS\n",
1460 __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301461 if (client_id == ADM_CLIENT_ID_SOURCE_TRACKING)
1462 this_adm.sourceTrackingData.
1463 apr_cmd_status = payload[1];
1464 else if (rtac_make_adm_callback(payload,
1465 data->payload_size))
1466 break;
1467 /*
1468 * if soft volume is called and already
1469 * interrupted break out of the sequence here
1470 */
1471 case ADM_CMD_DEVICE_OPEN_V5:
1472 case ADM_CMD_DEVICE_CLOSE_V5:
1473 case ADM_CMD_DEVICE_OPEN_V6:
1474 pr_debug("%s: Basic callback received, wake up.\n",
1475 __func__);
1476 atomic_set(&this_adm.copp.stat[port_idx]
1477 [copp_idx], payload[1]);
1478 wake_up(
1479 &this_adm.copp.wait[port_idx][copp_idx]);
1480 break;
1481 case ADM_CMD_ADD_TOPOLOGIES:
1482 pr_debug("%s: callback received, ADM_CMD_ADD_TOPOLOGIES.\n",
1483 __func__);
1484 atomic_set(&this_adm.adm_stat, payload[1]);
1485 wake_up(&this_adm.adm_wait);
1486 break;
1487 case ADM_CMD_MATRIX_MAP_ROUTINGS_V5:
1488 case ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5:
1489 pr_debug("%s: Basic callback received, wake up.\n",
1490 __func__);
1491 atomic_set(&this_adm.matrix_map_stat,
1492 payload[1]);
1493 wake_up(&this_adm.matrix_map_wait);
1494 break;
1495 case ADM_CMD_SHARED_MEM_UNMAP_REGIONS:
1496 pr_debug("%s: ADM_CMD_SHARED_MEM_UNMAP_REGIONS\n",
1497 __func__);
1498 atomic_set(&this_adm.adm_stat, payload[1]);
1499 wake_up(&this_adm.adm_wait);
1500 break;
1501 case ADM_CMD_SHARED_MEM_MAP_REGIONS:
1502 pr_debug("%s: ADM_CMD_SHARED_MEM_MAP_REGIONS\n",
1503 __func__);
1504 /* Should only come here if there is an APR */
1505 /* error or malformed APR packet. Otherwise */
1506 /* response will be returned as */
1507 if (payload[1] != 0) {
1508 pr_err("%s: ADM map error, resuming\n",
1509 __func__);
1510 atomic_set(&this_adm.adm_stat,
1511 payload[1]);
1512 wake_up(&this_adm.adm_wait);
1513 }
1514 break;
1515 case ADM_CMD_GET_PP_PARAMS_V5:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001516 case ADM_CMD_GET_PP_PARAMS_V6:
1517 pr_debug("%s: ADM_CMD_GET_PP_PARAMS\n",
1518 __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301519 /* Should only come here if there is an APR */
1520 /* error or malformed APR packet. Otherwise */
1521 /* response will be returned as */
1522 /* ADM_CMDRSP_GET_PP_PARAMS_V5 */
1523 if (client_id ==
1524 ADM_CLIENT_ID_SOURCE_TRACKING) {
1525 this_adm.sourceTrackingData.
1526 apr_cmd_status = payload[1];
1527 if (payload[1] != 0)
1528 pr_err("%s: ADM get param error = %d\n",
1529 __func__, payload[1]);
1530
1531 atomic_set(&this_adm.copp.stat
1532 [port_idx][copp_idx],
1533 payload[1]);
1534 wake_up(&this_adm.copp.wait
1535 [port_idx][copp_idx]);
1536 } else {
1537 if (payload[1] != 0) {
1538 pr_err("%s: ADM get param error = %d, resuming\n",
1539 __func__, payload[1]);
1540
1541 rtac_make_adm_callback(payload,
1542 data->payload_size);
1543 }
1544 }
1545 break;
1546 case ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5:
1547 pr_debug("%s: ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5\n",
1548 __func__);
1549 atomic_set(&this_adm.copp.stat[port_idx]
1550 [copp_idx], payload[1]);
1551 wake_up(
1552 &this_adm.copp.wait[port_idx][copp_idx]);
1553 break;
1554 case ADM_CMD_GET_PP_TOPO_MODULE_LIST:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001555 case ADM_CMD_GET_PP_TOPO_MODULE_LIST_V2:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301556 pr_debug("%s:ADM_CMD_GET_PP_TOPO_MODULE_LIST\n",
1557 __func__);
1558 if (payload[1] != 0)
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001559 pr_err("%s: ADM get topo list error = %d\n",
1560 __func__, payload[1]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301561 break;
1562 default:
1563 pr_err("%s: Unknown Cmd: 0x%x\n", __func__,
1564 payload[0]);
1565 break;
1566 }
1567 return 0;
1568 }
1569
1570 switch (data->opcode) {
1571 case ADM_CMDRSP_DEVICE_OPEN_V5:
1572 case ADM_CMDRSP_DEVICE_OPEN_V6: {
1573 struct adm_cmd_rsp_device_open_v5 *open =
1574 (struct adm_cmd_rsp_device_open_v5 *)data->payload;
1575
1576 if (open->copp_id == INVALID_COPP_ID) {
1577 pr_err("%s: invalid coppid rxed %d\n",
1578 __func__, open->copp_id);
1579 atomic_set(&this_adm.copp.stat[port_idx]
1580 [copp_idx], ADSP_EBADPARAM);
1581 wake_up(
1582 &this_adm.copp.wait[port_idx][copp_idx]);
1583 break;
1584 }
1585 atomic_set(&this_adm.copp.stat
1586 [port_idx][copp_idx], payload[0]);
1587 atomic_set(&this_adm.copp.id[port_idx][copp_idx],
1588 open->copp_id);
1589 pr_debug("%s: coppid rxed=%d\n", __func__,
1590 open->copp_id);
1591 wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
1592 }
1593 break;
1594 case ADM_CMDRSP_GET_PP_PARAMS_V5:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001595 case ADM_CMDRSP_GET_PP_PARAMS_V6:
1596 pr_debug("%s: ADM_CMDRSP_GET_PP_PARAMS\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301597 if (client_id == ADM_CLIENT_ID_SOURCE_TRACKING)
1598 this_adm.sourceTrackingData.apr_cmd_status =
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001599 payload[0];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301600 else if (rtac_make_adm_callback(payload,
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001601 data->payload_size))
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301602 break;
1603
1604 idx = ADM_GET_PARAMETER_LENGTH * copp_idx;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001605 if (payload[0] == 0 && data->payload_size > 0) {
1606 pr_debug("%s: Received parameter data in band\n",
1607 __func__);
1608 ret = adm_process_get_param_response(
1609 data->opcode, idx, payload,
1610 data->payload_size);
1611 if (ret)
1612 pr_err("%s: Failed to process get param response, error %d\n",
1613 __func__, ret);
1614 } else if (payload[0] == 0 && data->payload_size == 0) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301615 adm_get_parameters[idx] = -1;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001616 pr_debug("%s: Out of band case, setting size to %d\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301617 __func__, adm_get_parameters[idx]);
1618 } else {
1619 adm_get_parameters[idx] = -1;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001620 pr_err("%s: ADM_CMDRSP_GET_PP_PARAMS returned error 0x%x\n",
1621 __func__, payload[0]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301622 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001623 atomic_set(&this_adm.copp.stat[port_idx][copp_idx],
1624 payload[0]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301625 wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
1626 break;
1627 case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001628 case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST_V2:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301629 pr_debug("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST\n",
1630 __func__);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001631 num_modules = payload[1];
1632 pr_debug("%s: Num modules %d\n", __func__, num_modules);
1633 if (payload[0]) {
1634 pr_err("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST, error = %d\n",
1635 __func__, payload[0]);
1636 } else if (num_modules > MAX_MODULES_IN_TOPO) {
1637 pr_err("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST invalid num modules received, num modules = %d\n",
1638 __func__, num_modules);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301639 } else {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001640 ret = adm_process_get_topo_list_response(
1641 data->opcode, copp_idx, num_modules,
1642 payload, data->payload_size);
1643 if (ret)
1644 pr_err("%s: Failed to process get topo modules list response, error %d\n",
1645 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301646 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001647 atomic_set(&this_adm.copp.stat[port_idx][copp_idx],
1648 payload[0]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301649 wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
1650 break;
1651 case ADM_CMDRSP_SHARED_MEM_MAP_REGIONS:
1652 pr_debug("%s: ADM_CMDRSP_SHARED_MEM_MAP_REGIONS\n",
1653 __func__);
1654 atomic_set(&this_adm.mem_map_handles[
1655 atomic_read(&this_adm.mem_map_index)],
1656 *payload);
1657 atomic_set(&this_adm.adm_stat, 0);
1658 wake_up(&this_adm.adm_wait);
1659 break;
1660 default:
1661 pr_err("%s: Unknown cmd:0x%x\n", __func__,
1662 data->opcode);
1663 break;
1664 }
1665 }
1666 return 0;
1667}
1668
1669static int adm_memory_map_regions(phys_addr_t *buf_add, uint32_t mempool_id,
1670 uint32_t *bufsz, uint32_t bufcnt)
1671{
1672 struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
1673 struct avs_shared_map_region_payload *mregions = NULL;
1674 void *mmap_region_cmd = NULL;
1675 void *payload = NULL;
1676 int ret = 0;
1677 int i = 0;
1678 int cmd_size = 0;
1679
1680 pr_debug("%s:\n", __func__);
1681 if (this_adm.apr == NULL) {
1682 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
1683 0xFFFFFFFF, &this_adm);
1684 if (this_adm.apr == NULL) {
1685 pr_err("%s: Unable to register ADM\n", __func__);
1686 ret = -ENODEV;
1687 return ret;
1688 }
1689 rtac_set_adm_handle(this_adm.apr);
1690 }
1691
1692 cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
1693 + sizeof(struct avs_shared_map_region_payload)
1694 * bufcnt;
1695
1696 mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
1697 if (!mmap_region_cmd)
1698 return -ENOMEM;
1699
1700 mmap_regions = (struct avs_cmd_shared_mem_map_regions *)mmap_region_cmd;
1701 mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1702 APR_HDR_LEN(APR_HDR_SIZE),
1703 APR_PKT_VER);
1704 mmap_regions->hdr.pkt_size = cmd_size;
1705 mmap_regions->hdr.src_port = 0;
1706
1707 mmap_regions->hdr.dest_port = 0;
1708 mmap_regions->hdr.token = 0;
1709 mmap_regions->hdr.opcode = ADM_CMD_SHARED_MEM_MAP_REGIONS;
1710 mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL & 0x00ff;
1711 mmap_regions->num_regions = bufcnt & 0x00ff;
1712 mmap_regions->property_flag = 0x00;
1713
1714 pr_debug("%s: map_regions->num_regions = %d\n", __func__,
1715 mmap_regions->num_regions);
1716 payload = ((u8 *) mmap_region_cmd +
1717 sizeof(struct avs_cmd_shared_mem_map_regions));
1718 mregions = (struct avs_shared_map_region_payload *)payload;
1719
1720 for (i = 0; i < bufcnt; i++) {
1721 mregions->shm_addr_lsw = lower_32_bits(buf_add[i]);
1722 mregions->shm_addr_msw =
1723 msm_audio_populate_upper_32_bits(buf_add[i]);
1724 mregions->mem_size_bytes = bufsz[i];
1725 ++mregions;
1726 }
1727
1728 atomic_set(&this_adm.adm_stat, -1);
1729 ret = apr_send_pkt(this_adm.apr, (uint32_t *) mmap_region_cmd);
1730 if (ret < 0) {
1731 pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__,
1732 mmap_regions->hdr.opcode, ret);
1733 ret = -EINVAL;
1734 goto fail_cmd;
1735 }
1736
1737 ret = wait_event_timeout(this_adm.adm_wait,
1738 atomic_read(&this_adm.adm_stat) >= 0,
1739 5 * HZ);
1740 if (!ret) {
1741 pr_err("%s: timeout. waited for memory_map\n", __func__);
1742 ret = -EINVAL;
1743 goto fail_cmd;
1744 } else if (atomic_read(&this_adm.adm_stat) > 0) {
1745 pr_err("%s: DSP returned error[%s]\n",
1746 __func__, adsp_err_get_err_str(
1747 atomic_read(&this_adm.adm_stat)));
1748 ret = adsp_err_get_lnx_err_code(
1749 atomic_read(&this_adm.adm_stat));
1750 goto fail_cmd;
1751 }
1752fail_cmd:
1753 kfree(mmap_region_cmd);
1754 return ret;
1755}
1756
1757static int adm_memory_unmap_regions(void)
1758{
1759 struct avs_cmd_shared_mem_unmap_regions unmap_regions;
1760 int ret = 0;
1761
1762 pr_debug("%s:\n", __func__);
1763 if (this_adm.apr == NULL) {
1764 pr_err("%s: APR handle NULL\n", __func__);
1765 return -EINVAL;
1766 }
1767
1768 unmap_regions.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1769 APR_HDR_LEN(APR_HDR_SIZE),
1770 APR_PKT_VER);
1771 unmap_regions.hdr.pkt_size = sizeof(unmap_regions);
1772 unmap_regions.hdr.src_port = 0;
1773 unmap_regions.hdr.dest_port = 0;
1774 unmap_regions.hdr.token = 0;
1775 unmap_regions.hdr.opcode = ADM_CMD_SHARED_MEM_UNMAP_REGIONS;
1776 unmap_regions.mem_map_handle = atomic_read(&this_adm.
1777 mem_map_handles[atomic_read(&this_adm.mem_map_index)]);
1778 atomic_set(&this_adm.adm_stat, -1);
1779 ret = apr_send_pkt(this_adm.apr, (uint32_t *) &unmap_regions);
1780 if (ret < 0) {
1781 pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__,
1782 unmap_regions.hdr.opcode, ret);
1783 ret = -EINVAL;
1784 goto fail_cmd;
1785 }
1786
1787 ret = wait_event_timeout(this_adm.adm_wait,
1788 atomic_read(&this_adm.adm_stat) >= 0,
1789 5 * HZ);
1790 if (!ret) {
1791 pr_err("%s: timeout. waited for memory_unmap\n",
1792 __func__);
1793 ret = -EINVAL;
1794 goto fail_cmd;
1795 } else if (atomic_read(&this_adm.adm_stat) > 0) {
1796 pr_err("%s: DSP returned error[%s]\n",
1797 __func__, adsp_err_get_err_str(
1798 atomic_read(&this_adm.adm_stat)));
1799 ret = adsp_err_get_lnx_err_code(
1800 atomic_read(&this_adm.adm_stat));
1801 goto fail_cmd;
1802 } else {
1803 pr_debug("%s: Unmap handle 0x%x succeeded\n", __func__,
1804 unmap_regions.mem_map_handle);
1805 }
1806fail_cmd:
1807 return ret;
1808}
1809
1810static int remap_cal_data(struct cal_block_data *cal_block, int cal_index)
1811{
1812 int ret = 0;
1813
Banajit Goswami08bb7362017-11-03 22:48:23 -07001814 if (cal_block->map_data.dma_buf == NULL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301815 pr_err("%s: No ION allocation for cal index %d!\n",
1816 __func__, cal_index);
1817 ret = -EINVAL;
1818 goto done;
1819 }
1820
1821 if ((cal_block->map_data.map_size > 0) &&
1822 (cal_block->map_data.q6map_handle == 0)) {
1823 atomic_set(&this_adm.mem_map_index, cal_index);
1824 ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
1825 (uint32_t *)&cal_block->map_data.map_size, 1);
1826 if (ret < 0) {
1827 pr_err("%s: ADM mmap did not work! size = %zd ret %d\n",
1828 __func__,
1829 cal_block->map_data.map_size, ret);
1830 pr_debug("%s: ADM mmap did not work! addr = 0x%pK, size = %zd ret %d\n",
1831 __func__,
1832 &cal_block->cal_data.paddr,
1833 cal_block->map_data.map_size, ret);
1834 goto done;
1835 }
1836 cal_block->map_data.q6map_handle = atomic_read(&this_adm.
1837 mem_map_handles[cal_index]);
1838 }
1839done:
1840 return ret;
1841}
1842
1843static void send_adm_custom_topology(void)
1844{
1845 struct cal_block_data *cal_block = NULL;
1846 struct cmd_set_topologies adm_top;
1847 int cal_index = ADM_CUSTOM_TOP_CAL;
1848 int result;
1849
1850 if (this_adm.cal_data[cal_index] == NULL)
1851 goto done;
1852
1853 mutex_lock(&this_adm.cal_data[cal_index]->lock);
1854 if (!this_adm.set_custom_topology)
1855 goto unlock;
1856 this_adm.set_custom_topology = 0;
1857
1858 cal_block = cal_utils_get_only_cal_block(this_adm.cal_data[cal_index]);
Vikram Panduranga770b8382017-09-27 12:17:36 -07001859 if (cal_block == NULL || cal_utils_is_cal_stale(cal_block))
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301860 goto unlock;
1861
1862 pr_debug("%s: Sending cal_index %d\n", __func__, cal_index);
1863
1864 result = remap_cal_data(cal_block, cal_index);
1865 if (result) {
1866 pr_err("%s: Remap_cal_data failed for cal %d!\n",
1867 __func__, cal_index);
1868 goto unlock;
1869 }
1870 atomic_set(&this_adm.mem_map_index, cal_index);
1871 atomic_set(&this_adm.mem_map_handles[cal_index],
1872 cal_block->map_data.q6map_handle);
1873
1874 if (cal_block->cal_data.size == 0) {
1875 pr_debug("%s: No ADM cal to send\n", __func__);
1876 goto unlock;
1877 }
1878
1879 adm_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1880 APR_HDR_LEN(20), APR_PKT_VER);
1881 adm_top.hdr.pkt_size = sizeof(adm_top);
1882 adm_top.hdr.src_svc = APR_SVC_ADM;
1883 adm_top.hdr.src_domain = APR_DOMAIN_APPS;
1884 adm_top.hdr.src_port = 0;
1885 adm_top.hdr.dest_svc = APR_SVC_ADM;
1886 adm_top.hdr.dest_domain = APR_DOMAIN_ADSP;
1887 adm_top.hdr.dest_port = 0;
1888 adm_top.hdr.token = 0;
1889 adm_top.hdr.opcode = ADM_CMD_ADD_TOPOLOGIES;
1890 adm_top.payload_addr_lsw = lower_32_bits(cal_block->cal_data.paddr);
1891 adm_top.payload_addr_msw = msm_audio_populate_upper_32_bits(
1892 cal_block->cal_data.paddr);
1893 adm_top.mem_map_handle = cal_block->map_data.q6map_handle;
1894 adm_top.payload_size = cal_block->cal_data.size;
1895
1896 atomic_set(&this_adm.adm_stat, -1);
1897 pr_debug("%s: Sending ADM_CMD_ADD_TOPOLOGIES payload = 0x%pK, size = %d\n",
1898 __func__, &cal_block->cal_data.paddr,
1899 adm_top.payload_size);
1900 result = apr_send_pkt(this_adm.apr, (uint32_t *)&adm_top);
1901 if (result < 0) {
1902 pr_err("%s: Set topologies failed payload size = %zd result %d\n",
1903 __func__, cal_block->cal_data.size, result);
1904 goto unlock;
1905 }
1906 /* Wait for the callback */
1907 result = wait_event_timeout(this_adm.adm_wait,
1908 atomic_read(&this_adm.adm_stat) >= 0,
1909 msecs_to_jiffies(TIMEOUT_MS));
1910 if (!result) {
1911 pr_err("%s: Set topologies timed out payload size = %zd\n",
1912 __func__, cal_block->cal_data.size);
1913 goto unlock;
1914 } else if (atomic_read(&this_adm.adm_stat) > 0) {
1915 pr_err("%s: DSP returned error[%s]\n",
1916 __func__, adsp_err_get_err_str(
1917 atomic_read(&this_adm.adm_stat)));
1918 result = adsp_err_get_lnx_err_code(
1919 atomic_read(&this_adm.adm_stat));
1920 goto unlock;
1921 }
1922unlock:
1923 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
1924done:
1925 return;
1926}
1927
1928static int send_adm_cal_block(int port_id, int copp_idx,
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001929 struct cal_block_data *cal_block, int perf_mode)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301930{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001931 struct mem_mapping_hdr mem_hdr;
1932 int payload_size = 0;
1933 int port_idx = 0;
1934 int topology = 0;
1935 int result = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301936
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001937 pr_debug("%s: Port id 0x%x,\n", __func__, port_id);
1938
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301939 if (!cal_block) {
1940 pr_debug("%s: No ADM cal to send for port_id = 0x%x!\n",
1941 __func__, port_id);
1942 result = -EINVAL;
1943 goto done;
1944 }
1945 if (cal_block->cal_data.size <= 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001946 pr_debug("%s: No ADM cal sent for port_id = 0x%x!\n", __func__,
1947 port_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301948 result = -EINVAL;
1949 goto done;
1950 }
1951
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001952 memset(&mem_hdr, 0, sizeof(mem_hdr));
1953 port_id = afe_convert_virtual_to_portid(port_id);
1954 port_idx = adm_validate_and_get_port_index(port_id);
1955 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
1956 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
1957 return -EINVAL;
1958 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
1959 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
1960 return -EINVAL;
1961 }
1962
1963 topology = atomic_read(&this_adm.copp.topology[port_idx][copp_idx]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301964 if (perf_mode == LEGACY_PCM_MODE &&
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001965 topology == DS2_ADM_COPP_TOPOLOGY_ID) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301966 pr_err("%s: perf_mode %d, topology 0x%x\n", __func__, perf_mode,
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001967 topology);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301968 goto done;
1969 }
1970
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001971 mem_hdr.data_payload_addr_lsw =
1972 lower_32_bits(cal_block->cal_data.paddr);
1973 mem_hdr.data_payload_addr_msw =
1974 msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
1975 mem_hdr.mem_map_handle = cal_block->map_data.q6map_handle;
1976 payload_size = cal_block->cal_data.size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301977
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001978 adm_set_pp_params(port_id, copp_idx, &mem_hdr, NULL, payload_size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301979
1980done:
1981 return result;
1982}
1983
1984static struct cal_block_data *adm_find_cal_by_path(int cal_index, int path)
1985{
1986 struct list_head *ptr, *next;
1987 struct cal_block_data *cal_block = NULL;
1988 struct audio_cal_info_audproc *audproc_cal_info = NULL;
1989 struct audio_cal_info_audvol *audvol_cal_info = NULL;
1990
1991 pr_debug("%s:\n", __func__);
1992
1993 list_for_each_safe(ptr, next,
1994 &this_adm.cal_data[cal_index]->cal_blocks) {
1995
1996 cal_block = list_entry(ptr,
1997 struct cal_block_data, list);
1998
Vikram Panduranga770b8382017-09-27 12:17:36 -07001999 if (cal_utils_is_cal_stale(cal_block))
2000 continue;
2001
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302002 if (cal_index == ADM_AUDPROC_CAL ||
Vikram Panduranga770b8382017-09-27 12:17:36 -07002003 cal_index == ADM_LSM_AUDPROC_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302004 audproc_cal_info = cal_block->cal_info;
2005 if ((audproc_cal_info->path == path) &&
2006 (cal_block->cal_data.size > 0))
2007 return cal_block;
2008 } else if (cal_index == ADM_AUDVOL_CAL) {
2009 audvol_cal_info = cal_block->cal_info;
2010 if ((audvol_cal_info->path == path) &&
2011 (cal_block->cal_data.size > 0))
2012 return cal_block;
2013 }
2014 }
2015 pr_debug("%s: Can't find ADM cal for cal_index %d, path %d\n",
2016 __func__, cal_index, path);
2017 return NULL;
2018}
2019
2020static struct cal_block_data *adm_find_cal_by_app_type(int cal_index, int path,
2021 int app_type)
2022{
2023 struct list_head *ptr, *next;
2024 struct cal_block_data *cal_block = NULL;
2025 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2026 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2027
2028 pr_debug("%s\n", __func__);
2029
2030 list_for_each_safe(ptr, next,
2031 &this_adm.cal_data[cal_index]->cal_blocks) {
2032
2033 cal_block = list_entry(ptr,
2034 struct cal_block_data, list);
2035
Vikram Panduranga770b8382017-09-27 12:17:36 -07002036 if (cal_utils_is_cal_stale(cal_block))
2037 continue;
2038
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302039 if (cal_index == ADM_AUDPROC_CAL ||
Vikram Panduranga770b8382017-09-27 12:17:36 -07002040 cal_index == ADM_LSM_AUDPROC_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302041 audproc_cal_info = cal_block->cal_info;
2042 if ((audproc_cal_info->path == path) &&
2043 (audproc_cal_info->app_type == app_type) &&
2044 (cal_block->cal_data.size > 0))
2045 return cal_block;
2046 } else if (cal_index == ADM_AUDVOL_CAL) {
2047 audvol_cal_info = cal_block->cal_info;
2048 if ((audvol_cal_info->path == path) &&
2049 (audvol_cal_info->app_type == app_type) &&
2050 (cal_block->cal_data.size > 0))
2051 return cal_block;
2052 }
2053 }
2054 pr_debug("%s: Can't find ADM cali for cal_index %d, path %d, app %d, defaulting to search by path\n",
2055 __func__, cal_index, path, app_type);
2056 return adm_find_cal_by_path(cal_index, path);
2057}
2058
2059
2060static struct cal_block_data *adm_find_cal(int cal_index, int path,
2061 int app_type, int acdb_id,
2062 int sample_rate)
2063{
2064 struct list_head *ptr, *next;
2065 struct cal_block_data *cal_block = NULL;
2066 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2067 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2068
2069 pr_debug("%s:\n", __func__);
2070
2071 list_for_each_safe(ptr, next,
2072 &this_adm.cal_data[cal_index]->cal_blocks) {
2073
2074 cal_block = list_entry(ptr,
2075 struct cal_block_data, list);
Vikram Panduranga770b8382017-09-27 12:17:36 -07002076 if (cal_utils_is_cal_stale(cal_block))
2077 continue;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302078
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302079 if (cal_index == ADM_AUDPROC_CAL ||
2080 cal_index == ADM_LSM_AUDPROC_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302081 audproc_cal_info = cal_block->cal_info;
2082 if ((audproc_cal_info->path == path) &&
2083 (audproc_cal_info->app_type == app_type) &&
2084 (audproc_cal_info->acdb_id == acdb_id) &&
2085 (audproc_cal_info->sample_rate == sample_rate) &&
2086 (cal_block->cal_data.size > 0))
2087 return cal_block;
2088 } else if (cal_index == ADM_AUDVOL_CAL) {
2089 audvol_cal_info = cal_block->cal_info;
2090 if ((audvol_cal_info->path == path) &&
2091 (audvol_cal_info->app_type == app_type) &&
2092 (audvol_cal_info->acdb_id == acdb_id) &&
2093 (cal_block->cal_data.size > 0))
2094 return cal_block;
2095 }
2096 }
2097 pr_debug("%s: Can't find ADM cal for cal_index %d, path %d, app %d, acdb_id %d sample_rate %d defaulting to search by app type\n",
2098 __func__, cal_index, path, app_type, acdb_id, sample_rate);
2099 return adm_find_cal_by_app_type(cal_index, path, app_type);
2100}
2101
2102static int adm_remap_and_send_cal_block(int cal_index, int port_id,
2103 int copp_idx, struct cal_block_data *cal_block, int perf_mode,
2104 int app_type, int acdb_id, int sample_rate)
2105{
2106 int ret = 0;
2107
2108 pr_debug("%s: Sending cal_index cal %d\n", __func__, cal_index);
2109 ret = remap_cal_data(cal_block, cal_index);
2110 if (ret) {
2111 pr_err("%s: Remap_cal_data failed for cal %d!\n",
2112 __func__, cal_index);
2113 goto done;
2114 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002115 ret = send_adm_cal_block(port_id, copp_idx, cal_block, perf_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302116 if (ret < 0)
2117 pr_debug("%s: No cal sent for cal_index %d, port_id = 0x%x! ret %d sample_rate %d\n",
2118 __func__, cal_index, port_id, ret, sample_rate);
2119done:
2120 return ret;
2121}
2122
2123static void send_adm_cal_type(int cal_index, int path, int port_id,
2124 int copp_idx, int perf_mode, int app_type,
2125 int acdb_id, int sample_rate)
2126{
2127 struct cal_block_data *cal_block = NULL;
2128 int ret;
2129
2130 pr_debug("%s: cal index %d\n", __func__, cal_index);
2131
2132 if (this_adm.cal_data[cal_index] == NULL) {
2133 pr_debug("%s: cal_index %d not allocated!\n",
2134 __func__, cal_index);
2135 goto done;
2136 }
2137
2138 mutex_lock(&this_adm.cal_data[cal_index]->lock);
2139 cal_block = adm_find_cal(cal_index, path, app_type, acdb_id,
2140 sample_rate);
2141 if (cal_block == NULL)
2142 goto unlock;
2143
2144 ret = adm_remap_and_send_cal_block(cal_index, port_id, copp_idx,
2145 cal_block, perf_mode, app_type, acdb_id, sample_rate);
Vikram Panduranga770b8382017-09-27 12:17:36 -07002146
2147 cal_utils_mark_cal_used(cal_block);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302148unlock:
2149 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
2150done:
2151 return;
2152}
2153
2154static int get_cal_path(int path)
2155{
2156 if (path == 0x1)
2157 return RX_DEVICE;
2158 else
2159 return TX_DEVICE;
2160}
2161
2162static void send_adm_cal(int port_id, int copp_idx, int path, int perf_mode,
Aditya Bavanari5106b562018-01-08 13:16:32 +05302163 int app_type, int acdb_id, int sample_rate,
2164 int passthr_mode)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302165{
2166 pr_debug("%s: port id 0x%x copp_idx %d\n", __func__, port_id, copp_idx);
2167
Aditya Bavanari5106b562018-01-08 13:16:32 +05302168 if (passthr_mode != LISTEN)
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302169 send_adm_cal_type(ADM_AUDPROC_CAL, path, port_id, copp_idx,
2170 perf_mode, app_type, acdb_id, sample_rate);
2171 else
2172 send_adm_cal_type(ADM_LSM_AUDPROC_CAL, path, port_id, copp_idx,
2173 perf_mode, app_type, acdb_id, sample_rate);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302174 send_adm_cal_type(ADM_AUDVOL_CAL, path, port_id, copp_idx, perf_mode,
2175 app_type, acdb_id, sample_rate);
2176}
2177
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302178/**
2179 * adm_connect_afe_port -
2180 * command to send ADM connect AFE port
2181 *
2182 * @mode: value of mode for ADM connect AFE
2183 * @session_id: session active to connect
2184 * @port_id: Port ID number
2185 *
2186 * Returns 0 on success or error on failure
2187 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302188int adm_connect_afe_port(int mode, int session_id, int port_id)
2189{
2190 struct adm_cmd_connect_afe_port_v5 cmd;
2191 int ret = 0;
2192 int port_idx, copp_idx = 0;
2193
2194 pr_debug("%s: port_id: 0x%x session id:%d mode:%d\n", __func__,
2195 port_id, session_id, mode);
2196
2197 port_id = afe_convert_virtual_to_portid(port_id);
2198 port_idx = adm_validate_and_get_port_index(port_id);
2199 if (port_idx < 0) {
2200 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2201 return -EINVAL;
2202 }
2203
2204 if (this_adm.apr == NULL) {
2205 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
2206 0xFFFFFFFF, &this_adm);
2207 if (this_adm.apr == NULL) {
2208 pr_err("%s: Unable to register ADM\n", __func__);
2209 ret = -ENODEV;
2210 return ret;
2211 }
2212 rtac_set_adm_handle(this_adm.apr);
2213 }
2214 pr_debug("%s: Port ID 0x%x, index %d\n", __func__, port_id, port_idx);
2215
2216 cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2217 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
2218 cmd.hdr.pkt_size = sizeof(cmd);
2219 cmd.hdr.src_svc = APR_SVC_ADM;
2220 cmd.hdr.src_domain = APR_DOMAIN_APPS;
2221 cmd.hdr.src_port = port_id;
2222 cmd.hdr.dest_svc = APR_SVC_ADM;
2223 cmd.hdr.dest_domain = APR_DOMAIN_ADSP;
2224 cmd.hdr.dest_port = 0; /* Ignored */
2225 cmd.hdr.token = port_idx << 16 | copp_idx;
2226 cmd.hdr.opcode = ADM_CMD_CONNECT_AFE_PORT_V5;
2227
2228 cmd.mode = mode;
2229 cmd.session_id = session_id;
2230 cmd.afe_port_id = port_id;
2231
2232 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2233 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&cmd);
2234 if (ret < 0) {
2235 pr_err("%s: ADM enable for port_id: 0x%x failed ret %d\n",
2236 __func__, port_id, ret);
2237 ret = -EINVAL;
2238 goto fail_cmd;
2239 }
2240 /* Wait for the callback with copp id */
2241 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
2242 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
2243 msecs_to_jiffies(TIMEOUT_MS));
2244 if (!ret) {
2245 pr_err("%s: ADM connect timedout for port_id: 0x%x\n",
2246 __func__, port_id);
2247 ret = -EINVAL;
2248 goto fail_cmd;
2249 } else if (atomic_read(&this_adm.copp.stat
2250 [port_idx][copp_idx]) > 0) {
2251 pr_err("%s: DSP returned error[%s]\n",
2252 __func__, adsp_err_get_err_str(
2253 atomic_read(&this_adm.copp.stat
2254 [port_idx][copp_idx])));
2255 ret = adsp_err_get_lnx_err_code(
2256 atomic_read(&this_adm.copp.stat
2257 [port_idx][copp_idx]));
2258 goto fail_cmd;
2259 }
2260 atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
2261 return 0;
2262
2263fail_cmd:
2264
2265 return ret;
2266}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302267EXPORT_SYMBOL(adm_connect_afe_port);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302268
2269int adm_arrange_mch_map(struct adm_cmd_device_open_v5 *open, int path,
2270 int channel_mode)
2271{
2272 int rc = 0, idx;
2273
2274 memset(open->dev_channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
2275 switch (path) {
2276 case ADM_PATH_PLAYBACK:
2277 idx = ADM_MCH_MAP_IDX_PLAYBACK;
2278 break;
2279 case ADM_PATH_LIVE_REC:
2280 case ADM_PATH_NONLIVE_REC:
2281 idx = ADM_MCH_MAP_IDX_REC;
2282 break;
2283 default:
2284 goto non_mch_path;
2285 };
2286 if ((open->dev_num_channel > 2) && multi_ch_maps[idx].set_channel_map) {
2287 memcpy(open->dev_channel_mapping,
2288 multi_ch_maps[idx].channel_mapping,
2289 PCM_FORMAT_MAX_NUM_CHANNEL);
2290 } else {
2291 if (channel_mode == 1) {
2292 open->dev_channel_mapping[0] = PCM_CHANNEL_FC;
2293 } else if (channel_mode == 2) {
2294 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2295 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2296 } else if (channel_mode == 3) {
2297 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2298 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2299 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2300 } else if (channel_mode == 4) {
2301 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2302 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2303 open->dev_channel_mapping[2] = PCM_CHANNEL_LS;
2304 open->dev_channel_mapping[3] = PCM_CHANNEL_RS;
2305 } else if (channel_mode == 5) {
2306 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2307 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2308 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2309 open->dev_channel_mapping[3] = PCM_CHANNEL_LS;
2310 open->dev_channel_mapping[4] = PCM_CHANNEL_RS;
2311 } else if (channel_mode == 6) {
2312 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2313 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2314 open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2315 open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2316 open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2317 open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2318 } else if (channel_mode == 7) {
2319 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2320 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2321 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2322 open->dev_channel_mapping[3] = PCM_CHANNEL_LFE;
2323 open->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2324 open->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2325 open->dev_channel_mapping[6] = PCM_CHANNEL_CS;
2326 } else if (channel_mode == 8) {
2327 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2328 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2329 open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2330 open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2331 open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2332 open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2333 open->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2334 open->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2335 } else {
2336 pr_err("%s: invalid num_chan %d\n", __func__,
2337 channel_mode);
2338 rc = -EINVAL;
2339 goto inval_ch_mod;
2340 }
2341 }
2342
2343non_mch_path:
2344inval_ch_mod:
2345 return rc;
2346}
2347
2348int adm_arrange_mch_ep2_map(struct adm_cmd_device_open_v6 *open_v6,
2349 int channel_mode)
2350{
2351 int rc = 0;
2352
2353 memset(open_v6->dev_channel_mapping_eid2, 0,
2354 PCM_FORMAT_MAX_NUM_CHANNEL);
2355
2356 if (channel_mode == 1) {
2357 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FC;
2358 } else if (channel_mode == 2) {
2359 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2360 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2361 } else if (channel_mode == 3) {
2362 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2363 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2364 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
2365 } else if (channel_mode == 4) {
2366 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2367 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2368 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LS;
2369 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_RS;
2370 } else if (channel_mode == 5) {
2371 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2372 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2373 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
2374 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_LS;
2375 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_RS;
2376 } else if (channel_mode == 6) {
2377 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2378 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2379 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
2380 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
2381 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
2382 open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
2383 } else if (channel_mode == 8) {
2384 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2385 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2386 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
2387 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
2388 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
2389 open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
2390 open_v6->dev_channel_mapping_eid2[6] = PCM_CHANNEL_LB;
2391 open_v6->dev_channel_mapping_eid2[7] = PCM_CHANNEL_RB;
2392 } else {
2393 pr_err("%s: invalid num_chan %d\n", __func__,
2394 channel_mode);
2395 rc = -EINVAL;
2396 }
2397
2398 return rc;
2399}
2400
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302401/**
2402 * adm_open -
2403 * command to send ADM open
2404 *
2405 * @port_id: port id number
2406 * @path: direction or ADM path type
2407 * @rate: sample rate of session
2408 * @channel_mode: number of channels set
2409 * @topology: topology active for this session
2410 * @perf_mode: performance mode like LL/ULL/..
2411 * @bit_width: bit width to set for copp
2412 * @app_type: App type used for this session
2413 * @acdb_id: ACDB ID of this device
2414 *
2415 * Returns 0 on success or error on failure
2416 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302417int adm_open(int port_id, int path, int rate, int channel_mode, int topology,
2418 int perf_mode, uint16_t bit_width, int app_type, int acdb_id)
2419{
2420 struct adm_cmd_device_open_v5 open;
2421 struct adm_cmd_device_open_v6 open_v6;
2422 int ret = 0;
Asish Bhattacharya34504582017-08-08 12:55:01 +05302423 int port_idx, flags;
2424 int copp_idx = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302425 int tmp_port = q6audio_get_port_id(port_id);
2426
2427 pr_debug("%s:port %#x path:%d rate:%d mode:%d perf_mode:%d,topo_id %d\n",
2428 __func__, port_id, path, rate, channel_mode, perf_mode,
2429 topology);
2430
2431 port_id = q6audio_convert_virtual_to_portid(port_id);
2432 port_idx = adm_validate_and_get_port_index(port_id);
2433 if (port_idx < 0) {
2434 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2435 return -EINVAL;
2436 }
2437
2438 if (this_adm.apr == NULL) {
2439 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
2440 0xFFFFFFFF, &this_adm);
2441 if (this_adm.apr == NULL) {
2442 pr_err("%s: Unable to register ADM\n", __func__);
2443 return -ENODEV;
2444 }
2445 rtac_set_adm_handle(this_adm.apr);
2446 }
2447
2448 if (perf_mode == ULL_POST_PROCESSING_PCM_MODE) {
2449 flags = ADM_ULL_POST_PROCESSING_DEVICE_SESSION;
2450 if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
2451 (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
2452 (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
2453 topology = DEFAULT_COPP_TOPOLOGY;
2454 } else if (perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) {
2455 flags = ADM_ULTRA_LOW_LATENCY_DEVICE_SESSION;
2456 topology = NULL_COPP_TOPOLOGY;
2457 rate = ULL_SUPPORTED_SAMPLE_RATE;
2458 bit_width = ULL_SUPPORTED_BITS_PER_SAMPLE;
2459 } else if (perf_mode == LOW_LATENCY_PCM_MODE) {
2460 flags = ADM_LOW_LATENCY_DEVICE_SESSION;
2461 if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
2462 (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
2463 (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
2464 topology = DEFAULT_COPP_TOPOLOGY;
2465 } else {
2466 if ((path == ADM_PATH_COMPRESSED_RX) ||
2467 (path == ADM_PATH_COMPRESSED_TX))
2468 flags = 0;
2469 else
2470 flags = ADM_LEGACY_DEVICE_SESSION;
2471 }
2472
Laxminath Kasam8f7ccc22017-08-28 17:35:04 +05302473 if ((topology == VPM_TX_SM_ECNS_V2_COPP_TOPOLOGY) ||
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302474 (topology == VPM_TX_DM_FLUENCE_COPP_TOPOLOGY) ||
2475 (topology == VPM_TX_DM_RFECNS_COPP_TOPOLOGY))
2476 rate = 16000;
2477
Asish Bhattacharya34504582017-08-08 12:55:01 +05302478 /*
2479 * Routing driver reuses the same adm for streams with the same
2480 * app_type, sample_rate etc.
2481 * This isn't allowed for ULL streams as per the DSP interface
2482 */
2483 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE)
2484 copp_idx = adm_get_idx_if_copp_exists(port_idx, topology,
2485 perf_mode,
2486 rate, bit_width,
2487 app_type);
2488
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302489 if (copp_idx < 0) {
2490 copp_idx = adm_get_next_available_copp(port_idx);
2491 if (copp_idx >= MAX_COPPS_PER_PORT) {
2492 pr_err("%s: exceeded copp id %d\n",
2493 __func__, copp_idx);
2494 return -EINVAL;
2495 }
2496 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
2497 atomic_set(&this_adm.copp.topology[port_idx][copp_idx],
2498 topology);
2499 atomic_set(&this_adm.copp.mode[port_idx][copp_idx],
2500 perf_mode);
2501 atomic_set(&this_adm.copp.rate[port_idx][copp_idx],
2502 rate);
2503 atomic_set(&this_adm.copp.channels[port_idx][copp_idx],
2504 channel_mode);
2505 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx],
2506 bit_width);
2507 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx],
2508 app_type);
2509 atomic_set(&this_adm.copp.acdb_id[port_idx][copp_idx],
2510 acdb_id);
2511 set_bit(ADM_STATUS_CALIBRATION_REQUIRED,
2512 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
2513 if ((path != ADM_PATH_COMPRESSED_RX) &&
2514 (path != ADM_PATH_COMPRESSED_TX))
2515 send_adm_custom_topology();
2516 }
2517
2518 if (this_adm.copp.adm_delay[port_idx][copp_idx] &&
2519 perf_mode == LEGACY_PCM_MODE) {
2520 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
2521 1);
2522 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
2523 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
2524 }
2525
2526 /* Create a COPP if port id are not enabled */
2527 if (atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]) == 0) {
2528 pr_debug("%s: open ADM: port_idx: %d, copp_idx: %d\n", __func__,
2529 port_idx, copp_idx);
2530 if ((topology == SRS_TRUMEDIA_TOPOLOGY_ID) &&
2531 perf_mode == LEGACY_PCM_MODE) {
2532 int res;
2533
2534 atomic_set(&this_adm.mem_map_index, ADM_SRS_TRUMEDIA);
2535 msm_dts_srs_tm_ion_memmap(&this_adm.outband_memmap);
2536 res = adm_memory_map_regions(&this_adm.outband_memmap.paddr, 0,
2537 (uint32_t *)&this_adm.outband_memmap.size, 1);
2538 if (res < 0) {
2539 pr_err("%s: SRS adm_memory_map_regions failed ! addr = 0x%pK, size = %d\n",
2540 __func__, (void *)this_adm.outband_memmap.paddr,
2541 (uint32_t)this_adm.outband_memmap.size);
2542 }
2543 }
2544 open.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2545 APR_HDR_LEN(APR_HDR_SIZE),
2546 APR_PKT_VER);
2547 open.hdr.pkt_size = sizeof(open);
2548 open.hdr.src_svc = APR_SVC_ADM;
2549 open.hdr.src_domain = APR_DOMAIN_APPS;
2550 open.hdr.src_port = tmp_port;
2551 open.hdr.dest_svc = APR_SVC_ADM;
2552 open.hdr.dest_domain = APR_DOMAIN_ADSP;
2553 open.hdr.dest_port = tmp_port;
2554 open.hdr.token = port_idx << 16 | copp_idx;
2555 open.hdr.opcode = ADM_CMD_DEVICE_OPEN_V5;
2556 open.flags = flags;
2557 open.mode_of_operation = path;
2558 open.endpoint_id_1 = tmp_port;
2559 open.endpoint_id_2 = 0xFFFF;
2560
2561 if (this_adm.ec_ref_rx && (path != 1)) {
2562 open.endpoint_id_2 = this_adm.ec_ref_rx;
2563 this_adm.ec_ref_rx = -1;
2564 }
2565
2566 open.topology_id = topology;
2567
2568 open.dev_num_channel = channel_mode & 0x00FF;
2569 open.bit_width = bit_width;
2570 WARN_ON((perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) &&
2571 (rate != ULL_SUPPORTED_SAMPLE_RATE));
2572 open.sample_rate = rate;
2573
2574 ret = adm_arrange_mch_map(&open, path, channel_mode);
2575
2576 if (ret)
2577 return ret;
2578
2579 pr_debug("%s: port_id=0x%x rate=%d topology_id=0x%X\n",
2580 __func__, open.endpoint_id_1, open.sample_rate,
2581 open.topology_id);
2582
2583 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2584
2585 if ((this_adm.num_ec_ref_rx_chans != 0) && (path != 1) &&
2586 (open.endpoint_id_2 != 0xFFFF)) {
2587 memset(&open_v6, 0,
2588 sizeof(struct adm_cmd_device_open_v6));
2589 memcpy(&open_v6, &open,
2590 sizeof(struct adm_cmd_device_open_v5));
2591 open_v6.hdr.opcode = ADM_CMD_DEVICE_OPEN_V6;
2592 open_v6.hdr.pkt_size = sizeof(open_v6);
2593 open_v6.dev_num_channel_eid2 =
2594 this_adm.num_ec_ref_rx_chans;
2595 this_adm.num_ec_ref_rx_chans = 0;
2596
2597 if (this_adm.ec_ref_rx_bit_width != 0) {
2598 open_v6.bit_width_eid2 =
2599 this_adm.ec_ref_rx_bit_width;
2600 this_adm.ec_ref_rx_bit_width = 0;
2601 } else {
2602 open_v6.bit_width_eid2 = bit_width;
2603 }
2604
2605 if (this_adm.ec_ref_rx_sampling_rate != 0) {
2606 open_v6.sample_rate_eid2 =
2607 this_adm.ec_ref_rx_sampling_rate;
2608 this_adm.ec_ref_rx_sampling_rate = 0;
2609 } else {
2610 open_v6.sample_rate_eid2 = rate;
2611 }
2612
2613 pr_debug("%s: eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
2614 __func__, open_v6.dev_num_channel_eid2,
2615 open_v6.bit_width_eid2,
2616 open_v6.sample_rate_eid2);
2617
2618 ret = adm_arrange_mch_ep2_map(&open_v6,
2619 open_v6.dev_num_channel_eid2);
2620
2621 if (ret)
2622 return ret;
2623
2624 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&open_v6);
2625 } else {
2626 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&open);
2627 }
2628 if (ret < 0) {
2629 pr_err("%s: port_id: 0x%x for[0x%x] failed %d\n",
2630 __func__, tmp_port, port_id, ret);
2631 return -EINVAL;
2632 }
2633 /* Wait for the callback with copp id */
2634 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
2635 atomic_read(&this_adm.copp.stat
2636 [port_idx][copp_idx]) >= 0,
2637 msecs_to_jiffies(TIMEOUT_MS));
2638 if (!ret) {
2639 pr_err("%s: ADM open timedout for port_id: 0x%x for [0x%x]\n",
2640 __func__, tmp_port, port_id);
2641 return -EINVAL;
2642 } else if (atomic_read(&this_adm.copp.stat
2643 [port_idx][copp_idx]) > 0) {
2644 pr_err("%s: DSP returned error[%s]\n",
2645 __func__, adsp_err_get_err_str(
2646 atomic_read(&this_adm.copp.stat
2647 [port_idx][copp_idx])));
2648 return adsp_err_get_lnx_err_code(
2649 atomic_read(&this_adm.copp.stat
2650 [port_idx][copp_idx]));
2651 }
2652 }
2653 atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
2654 return copp_idx;
2655}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302656EXPORT_SYMBOL(adm_open);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302657
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302658/**
2659 * adm_copp_mfc_cfg -
2660 * command to send ADM MFC config
2661 *
2662 * @port_id: Port ID number
2663 * @copp_idx: copp index assigned
2664 * @dst_sample_rate: sink sample rate
2665 *
2666 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302667void adm_copp_mfc_cfg(int port_id, int copp_idx, int dst_sample_rate)
2668{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002669 struct audproc_mfc_param_media_fmt mfc_cfg;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302670 struct adm_cmd_device_open_v5 open;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002671 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302672 int port_idx;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302673 int rc = 0;
2674 int i = 0;
2675
2676 port_id = q6audio_convert_virtual_to_portid(port_id);
2677 port_idx = adm_validate_and_get_port_index(port_id);
2678
2679 if (port_idx < 0) {
2680 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
2681 goto fail_cmd;
2682 }
2683
2684 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
2685 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
2686 goto fail_cmd;
2687 }
2688
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002689 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
2690 memset(&open, 0, sizeof(open));
2691 memset(&param_hdr, 0, sizeof(param_hdr));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302692
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002693 param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
2694 param_hdr.instance_id = INSTANCE_ID_0;
2695 param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
2696 param_hdr.param_size = sizeof(mfc_cfg);
2697
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302698 mfc_cfg.sampling_rate = dst_sample_rate;
2699 mfc_cfg.bits_per_sample =
2700 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
2701 open.dev_num_channel = mfc_cfg.num_channels =
2702 atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
2703
2704 rc = adm_arrange_mch_map(&open, ADM_PATH_PLAYBACK,
2705 mfc_cfg.num_channels);
2706 if (rc < 0) {
2707 pr_err("%s: unable to get channal map\n", __func__);
2708 goto fail_cmd;
2709 }
2710
2711 for (i = 0; i < mfc_cfg.num_channels; i++)
2712 mfc_cfg.channel_type[i] =
2713 (uint16_t) open.dev_channel_mapping[i];
2714
2715 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2716
2717 pr_debug("%s: mfc config: port_idx %d copp_idx %d copp SR %d copp BW %d copp chan %d o/p SR %d\n",
2718 __func__, port_idx, copp_idx,
2719 atomic_read(&this_adm.copp.rate[port_idx][copp_idx]),
2720 mfc_cfg.bits_per_sample, mfc_cfg.num_channels,
2721 mfc_cfg.sampling_rate);
2722
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002723 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
2724 (uint8_t *) &mfc_cfg);
2725 if (rc)
2726 pr_err("%s: Failed to set media format configuration data, err %d\n",
2727 __func__, rc);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302728
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302729fail_cmd:
2730 return;
2731}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302732EXPORT_SYMBOL(adm_copp_mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302733
2734static void route_set_opcode_matrix_id(
2735 struct adm_cmd_matrix_map_routings_v5 **route_addr,
2736 int path, uint32_t passthr_mode)
2737{
2738 struct adm_cmd_matrix_map_routings_v5 *route = *route_addr;
2739
2740 switch (path) {
2741 case ADM_PATH_PLAYBACK:
2742 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
2743 route->matrix_id = ADM_MATRIX_ID_AUDIO_RX;
2744 break;
2745 case ADM_PATH_LIVE_REC:
2746 if (passthr_mode == LISTEN) {
2747 route->hdr.opcode =
2748 ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
2749 route->matrix_id = ADM_MATRIX_ID_LISTEN_TX;
2750 break;
2751 }
2752 /* fall through to set matrix id for non-listen case */
2753 case ADM_PATH_NONLIVE_REC:
2754 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
2755 route->matrix_id = ADM_MATRIX_ID_AUDIO_TX;
2756 break;
2757 case ADM_PATH_COMPRESSED_RX:
2758 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
2759 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_RX;
2760 break;
2761 case ADM_PATH_COMPRESSED_TX:
2762 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
2763 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_TX;
2764 break;
2765 default:
2766 pr_err("%s: Wrong path set[%d]\n", __func__, path);
2767 break;
2768 }
2769 pr_debug("%s: opcode 0x%x, matrix id %d\n",
2770 __func__, route->hdr.opcode, route->matrix_id);
2771}
2772
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302773/**
2774 * adm_matrix_map -
2775 * command to send ADM matrix map for ADM copp list
2776 *
2777 * @path: direction or ADM path type
2778 * @payload_map: have info of session id and associated copp_idx/num_copps
2779 * @perf_mode: performance mode like LL/ULL/..
2780 * @passthr_mode: flag to indicate passthrough mode
2781 *
2782 * Returns 0 on success or error on failure
2783 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302784int adm_matrix_map(int path, struct route_payload payload_map, int perf_mode,
2785 uint32_t passthr_mode)
2786{
2787 struct adm_cmd_matrix_map_routings_v5 *route;
2788 struct adm_session_map_node_v5 *node;
2789 uint16_t *copps_list;
2790 int cmd_size = 0;
2791 int ret = 0, i = 0;
2792 void *payload = NULL;
2793 void *matrix_map = NULL;
2794 int port_idx, copp_idx;
2795
2796 /* Assumes port_ids have already been validated during adm_open */
2797 cmd_size = (sizeof(struct adm_cmd_matrix_map_routings_v5) +
2798 sizeof(struct adm_session_map_node_v5) +
2799 (sizeof(uint32_t) * payload_map.num_copps));
2800 matrix_map = kzalloc(cmd_size, GFP_KERNEL);
2801 if (matrix_map == NULL) {
2802 pr_err("%s: Mem alloc failed\n", __func__);
2803 ret = -EINVAL;
2804 return ret;
2805 }
2806 route = (struct adm_cmd_matrix_map_routings_v5 *)matrix_map;
2807
2808 route->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2809 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
2810 route->hdr.pkt_size = cmd_size;
2811 route->hdr.src_svc = 0;
2812 route->hdr.src_domain = APR_DOMAIN_APPS;
2813 route->hdr.src_port = 0; /* Ignored */;
2814 route->hdr.dest_svc = APR_SVC_ADM;
2815 route->hdr.dest_domain = APR_DOMAIN_ADSP;
2816 route->hdr.dest_port = 0; /* Ignored */;
2817 route->hdr.token = 0;
2818 route->num_sessions = 1;
2819 route_set_opcode_matrix_id(&route, path, passthr_mode);
2820
2821 payload = ((u8 *)matrix_map +
2822 sizeof(struct adm_cmd_matrix_map_routings_v5));
2823 node = (struct adm_session_map_node_v5 *)payload;
2824
2825 node->session_id = payload_map.session_id;
2826 node->num_copps = payload_map.num_copps;
2827 payload = (u8 *)node + sizeof(struct adm_session_map_node_v5);
2828 copps_list = (uint16_t *)payload;
2829 for (i = 0; i < payload_map.num_copps; i++) {
2830 port_idx =
2831 adm_validate_and_get_port_index(payload_map.port_id[i]);
2832 if (port_idx < 0) {
2833 pr_err("%s: Invalid port_id 0x%x\n", __func__,
2834 payload_map.port_id[i]);
2835 ret = -EINVAL;
2836 goto fail_cmd;
2837 }
2838 copp_idx = payload_map.copp_idx[i];
2839 copps_list[i] = atomic_read(&this_adm.copp.id[port_idx]
2840 [copp_idx]);
2841 }
2842 atomic_set(&this_adm.matrix_map_stat, -1);
2843
2844 ret = apr_send_pkt(this_adm.apr, (uint32_t *)matrix_map);
2845 if (ret < 0) {
2846 pr_err("%s: routing for syream %d failed ret %d\n",
2847 __func__, payload_map.session_id, ret);
2848 ret = -EINVAL;
2849 goto fail_cmd;
2850 }
2851 ret = wait_event_timeout(this_adm.matrix_map_wait,
2852 atomic_read(&this_adm.matrix_map_stat) >= 0,
2853 msecs_to_jiffies(TIMEOUT_MS));
2854 if (!ret) {
2855 pr_err("%s: routing for syream %d failed\n", __func__,
2856 payload_map.session_id);
2857 ret = -EINVAL;
2858 goto fail_cmd;
2859 } else if (atomic_read(&this_adm.matrix_map_stat) > 0) {
2860 pr_err("%s: DSP returned error[%s]\n", __func__,
2861 adsp_err_get_err_str(atomic_read(
2862 &this_adm.matrix_map_stat)));
2863 ret = adsp_err_get_lnx_err_code(
2864 atomic_read(&this_adm.matrix_map_stat));
2865 goto fail_cmd;
2866 }
2867
2868 if ((perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) &&
2869 (path != ADM_PATH_COMPRESSED_RX)) {
2870 for (i = 0; i < payload_map.num_copps; i++) {
2871 port_idx = afe_get_port_index(payload_map.port_id[i]);
2872 copp_idx = payload_map.copp_idx[i];
2873 if (port_idx < 0 || copp_idx < 0 ||
2874 (copp_idx > MAX_COPPS_PER_PORT - 1)) {
2875 pr_err("%s: Invalid idx port_idx %d copp_idx %d\n",
2876 __func__, port_idx, copp_idx);
2877 continue;
2878 }
2879 rtac_add_adm_device(payload_map.port_id[i],
2880 atomic_read(&this_adm.copp.id
2881 [port_idx][copp_idx]),
2882 get_cal_path(path),
2883 payload_map.session_id,
2884 payload_map.app_type[i],
2885 payload_map.acdb_dev_id[i]);
2886
2887 if (!test_bit(ADM_STATUS_CALIBRATION_REQUIRED,
2888 (void *)&this_adm.copp.adm_status[port_idx]
2889 [copp_idx])) {
2890 pr_debug("%s: adm copp[0x%x][%d] already sent",
2891 __func__, port_idx, copp_idx);
2892 continue;
2893 }
2894 send_adm_cal(payload_map.port_id[i], copp_idx,
2895 get_cal_path(path), perf_mode,
2896 payload_map.app_type[i],
2897 payload_map.acdb_dev_id[i],
Aditya Bavanari5106b562018-01-08 13:16:32 +05302898 payload_map.sample_rate[i],
2899 passthr_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302900 /* ADM COPP calibration is already sent */
2901 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
2902 (void *)&this_adm.copp.
2903 adm_status[port_idx][copp_idx]);
2904 pr_debug("%s: copp_id: %d\n", __func__,
2905 atomic_read(&this_adm.copp.id[port_idx]
2906 [copp_idx]));
2907 }
2908 }
2909
2910fail_cmd:
2911 kfree(matrix_map);
2912 return ret;
2913}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302914EXPORT_SYMBOL(adm_matrix_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302915
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302916/**
2917 * adm_ec_ref_rx_id -
2918 * Update EC ref port ID
2919 *
2920 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302921void adm_ec_ref_rx_id(int port_id)
2922{
2923 this_adm.ec_ref_rx = port_id;
2924 pr_debug("%s: ec_ref_rx:%d\n", __func__, this_adm.ec_ref_rx);
2925}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302926EXPORT_SYMBOL(adm_ec_ref_rx_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302927
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302928/**
2929 * adm_num_ec_ref_rx_chans -
2930 * Update EC ref number of channels
2931 *
2932 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302933void adm_num_ec_ref_rx_chans(int num_chans)
2934{
2935 this_adm.num_ec_ref_rx_chans = num_chans;
2936 pr_debug("%s: num_ec_ref_rx_chans:%d\n",
2937 __func__, this_adm.num_ec_ref_rx_chans);
2938}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302939EXPORT_SYMBOL(adm_num_ec_ref_rx_chans);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302940
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302941/**
2942 * adm_ec_ref_rx_bit_width -
2943 * Update EC ref bit_width
2944 *
2945 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302946void adm_ec_ref_rx_bit_width(int bit_width)
2947{
2948 this_adm.ec_ref_rx_bit_width = bit_width;
2949 pr_debug("%s: ec_ref_rx_bit_width:%d\n",
2950 __func__, this_adm.ec_ref_rx_bit_width);
2951}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302952EXPORT_SYMBOL(adm_ec_ref_rx_bit_width);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302953
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302954/**
2955 * adm_ec_ref_rx_sampling_rate -
2956 * Update EC ref sample rate
2957 *
2958 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302959void adm_ec_ref_rx_sampling_rate(int sampling_rate)
2960{
2961 this_adm.ec_ref_rx_sampling_rate = sampling_rate;
2962 pr_debug("%s: ec_ref_rx_sampling_rate:%d\n",
2963 __func__, this_adm.ec_ref_rx_sampling_rate);
2964}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302965EXPORT_SYMBOL(adm_ec_ref_rx_sampling_rate);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302966
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302967/**
2968 * adm_close -
2969 * command to close ADM copp
2970 *
2971 * @port_id: Port ID number
2972 * @perf_mode: performance mode like LL/ULL/..
2973 * @copp_idx: copp index assigned
2974 *
2975 * Returns 0 on success or error on failure
2976 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302977int adm_close(int port_id, int perf_mode, int copp_idx)
2978{
2979 struct apr_hdr close;
2980
2981 int ret = 0, port_idx;
2982 int copp_id = RESET_COPP_ID;
2983
2984 pr_debug("%s: port_id=0x%x perf_mode: %d copp_idx: %d\n", __func__,
2985 port_id, perf_mode, copp_idx);
2986
2987 port_id = q6audio_convert_virtual_to_portid(port_id);
2988 port_idx = adm_validate_and_get_port_index(port_id);
2989 if (port_idx < 0) {
2990 pr_err("%s: Invalid port_id 0x%x\n",
2991 __func__, port_id);
2992 return -EINVAL;
2993 }
2994
2995 if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
2996 pr_err("%s: Invalid copp idx: %d\n", __func__, copp_idx);
2997 return -EINVAL;
2998 }
2999
3000 if (this_adm.copp.adm_delay[port_idx][copp_idx] && perf_mode
3001 == LEGACY_PCM_MODE) {
3002 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
3003 1);
3004 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
3005 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
3006 }
3007
3008 atomic_dec(&this_adm.copp.cnt[port_idx][copp_idx]);
3009 if (!(atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]))) {
3010 copp_id = adm_get_copp_id(port_idx, copp_idx);
3011 pr_debug("%s: Closing ADM port_idx:%d copp_idx:%d copp_id:0x%x\n",
3012 __func__, port_idx, copp_idx, copp_id);
3013 if ((!perf_mode) && (this_adm.outband_memmap.paddr != 0) &&
3014 (atomic_read(&this_adm.copp.topology[port_idx][copp_idx]) ==
3015 SRS_TRUMEDIA_TOPOLOGY_ID)) {
3016 atomic_set(&this_adm.mem_map_index,
3017 ADM_SRS_TRUMEDIA);
3018 ret = adm_memory_unmap_regions();
3019 if (ret < 0) {
3020 pr_err("%s: adm mem unmmap err %d",
3021 __func__, ret);
3022 } else {
3023 atomic_set(&this_adm.mem_map_handles
3024 [ADM_SRS_TRUMEDIA], 0);
3025 }
3026 }
3027
3028
3029 if ((afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX) &&
3030 this_adm.sourceTrackingData.memmap.paddr) {
3031 atomic_set(&this_adm.mem_map_index,
3032 ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
3033 ret = adm_memory_unmap_regions();
3034 if (ret < 0) {
3035 pr_err("%s: adm mem unmmap err %d",
3036 __func__, ret);
3037 }
3038 msm_audio_ion_free(
Banajit Goswami08bb7362017-11-03 22:48:23 -07003039 this_adm.sourceTrackingData.dma_buf);
3040 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303041 this_adm.sourceTrackingData.memmap.size = 0;
3042 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
3043 this_adm.sourceTrackingData.memmap.paddr = 0;
3044 this_adm.sourceTrackingData.apr_cmd_status = -1;
3045 atomic_set(&this_adm.mem_map_handles[
3046 ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
3047 }
3048
3049 close.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3050 APR_HDR_LEN(APR_HDR_SIZE),
3051 APR_PKT_VER);
3052 close.pkt_size = sizeof(close);
3053 close.src_svc = APR_SVC_ADM;
3054 close.src_domain = APR_DOMAIN_APPS;
3055 close.src_port = port_id;
3056 close.dest_svc = APR_SVC_ADM;
3057 close.dest_domain = APR_DOMAIN_ADSP;
3058 close.dest_port = copp_id;
3059 close.token = port_idx << 16 | copp_idx;
3060 close.opcode = ADM_CMD_DEVICE_CLOSE_V5;
3061
3062 atomic_set(&this_adm.copp.id[port_idx][copp_idx],
3063 RESET_COPP_ID);
3064 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
3065 atomic_set(&this_adm.copp.topology[port_idx][copp_idx], 0);
3066 atomic_set(&this_adm.copp.mode[port_idx][copp_idx], 0);
3067 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3068 atomic_set(&this_adm.copp.rate[port_idx][copp_idx], 0);
3069 atomic_set(&this_adm.copp.channels[port_idx][copp_idx], 0);
3070 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx], 0);
3071 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx], 0);
3072
3073 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3074 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
3075
3076 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&close);
3077 if (ret < 0) {
3078 pr_err("%s: ADM close failed %d\n", __func__, ret);
3079 return -EINVAL;
3080 }
3081
3082 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3083 atomic_read(&this_adm.copp.stat
3084 [port_idx][copp_idx]) >= 0,
3085 msecs_to_jiffies(TIMEOUT_MS));
3086 if (!ret) {
3087 pr_err("%s: ADM cmd Route timedout for port 0x%x\n",
3088 __func__, port_id);
3089 return -EINVAL;
3090 } else if (atomic_read(&this_adm.copp.stat
3091 [port_idx][copp_idx]) > 0) {
3092 pr_err("%s: DSP returned error[%s]\n",
3093 __func__, adsp_err_get_err_str(
3094 atomic_read(&this_adm.copp.stat
3095 [port_idx][copp_idx])));
3096 return adsp_err_get_lnx_err_code(
3097 atomic_read(&this_adm.copp.stat
3098 [port_idx][copp_idx]));
3099 }
3100 }
3101
3102 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) {
3103 pr_debug("%s: remove adm device from rtac\n", __func__);
3104 rtac_remove_adm_device(port_id, copp_id);
3105 }
3106 return 0;
3107}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303108EXPORT_SYMBOL(adm_close);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303109
3110int send_rtac_audvol_cal(void)
3111{
3112 int ret = 0;
3113 int ret2 = 0;
3114 int i = 0;
3115 int copp_idx, port_idx, acdb_id, app_id, path;
3116 struct cal_block_data *cal_block = NULL;
3117 struct audio_cal_info_audvol *audvol_cal_info = NULL;
3118 struct rtac_adm rtac_adm_data;
3119
3120 mutex_lock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3121
3122 cal_block = cal_utils_get_only_cal_block(
3123 this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]);
Vikram Panduranga770b8382017-09-27 12:17:36 -07003124 if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303125 pr_err("%s: can't find cal block!\n", __func__);
3126 goto unlock;
3127 }
3128
3129 audvol_cal_info = cal_block->cal_info;
3130 if (audvol_cal_info == NULL) {
3131 pr_err("%s: audvol_cal_info is NULL!\n", __func__);
3132 goto unlock;
3133 }
3134
3135 get_rtac_adm_data(&rtac_adm_data);
3136 for (; i < rtac_adm_data.num_of_dev; i++) {
3137
3138 acdb_id = rtac_adm_data.device[i].acdb_dev_id;
3139 if (acdb_id == 0)
3140 acdb_id = audvol_cal_info->acdb_id;
3141
3142 app_id = rtac_adm_data.device[i].app_type;
3143 if (app_id == 0)
3144 app_id = audvol_cal_info->app_type;
3145
3146 path = afe_get_port_type(rtac_adm_data.device[i].afe_port);
3147 if ((acdb_id == audvol_cal_info->acdb_id) &&
3148 (app_id == audvol_cal_info->app_type) &&
3149 (path == audvol_cal_info->path)) {
3150
3151 if (adm_get_indexes_from_copp_id(rtac_adm_data.
3152 device[i].copp, &copp_idx, &port_idx) != 0) {
3153 pr_debug("%s: Copp Id %d is not active\n",
3154 __func__,
3155 rtac_adm_data.device[i].copp);
3156 continue;
3157 }
3158
3159 ret2 = adm_remap_and_send_cal_block(ADM_RTAC_AUDVOL_CAL,
3160 rtac_adm_data.device[i].afe_port,
3161 copp_idx, cal_block,
3162 atomic_read(&this_adm.copp.
3163 mode[port_idx][copp_idx]),
3164 audvol_cal_info->app_type,
3165 audvol_cal_info->acdb_id,
3166 atomic_read(&this_adm.copp.
3167 rate[port_idx][copp_idx]));
3168 if (ret2 < 0) {
3169 pr_debug("%s: remap and send failed for copp Id %d, acdb id %d, app type %d, path %d\n",
3170 __func__, rtac_adm_data.device[i].copp,
3171 audvol_cal_info->acdb_id,
3172 audvol_cal_info->app_type,
3173 audvol_cal_info->path);
3174 ret = ret2;
3175 }
3176 }
3177 }
3178unlock:
3179 mutex_unlock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3180 return ret;
3181}
3182
3183int adm_map_rtac_block(struct rtac_cal_block_data *cal_block)
3184{
3185 int result = 0;
3186
3187 pr_debug("%s:\n", __func__);
3188
3189 if (cal_block == NULL) {
3190 pr_err("%s: cal_block is NULL!\n",
3191 __func__);
3192 result = -EINVAL;
3193 goto done;
3194 }
3195
3196 if (cal_block->cal_data.paddr == 0) {
3197 pr_debug("%s: No address to map!\n",
3198 __func__);
3199 result = -EINVAL;
3200 goto done;
3201 }
3202
3203 if (cal_block->map_data.map_size == 0) {
3204 pr_debug("%s: map size is 0!\n",
3205 __func__);
3206 result = -EINVAL;
3207 goto done;
3208 }
3209
3210 /* valid port ID needed for callback use primary I2S */
3211 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3212 result = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3213 &cal_block->map_data.map_size, 1);
3214 if (result < 0) {
3215 pr_err("%s: RTAC mmap did not work! size = %d result %d\n",
3216 __func__,
3217 cal_block->map_data.map_size, result);
3218 pr_debug("%s: RTAC mmap did not work! addr = 0x%pK, size = %d\n",
3219 __func__,
3220 &cal_block->cal_data.paddr,
3221 cal_block->map_data.map_size);
3222 goto done;
3223 }
3224
3225 cal_block->map_data.map_handle = atomic_read(
3226 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]);
3227done:
3228 return result;
3229}
3230
3231int adm_unmap_rtac_block(uint32_t *mem_map_handle)
3232{
3233 int result = 0;
3234
3235 pr_debug("%s:\n", __func__);
3236
3237 if (mem_map_handle == NULL) {
3238 pr_debug("%s: Map handle is NULL, nothing to unmap\n",
3239 __func__);
3240 goto done;
3241 }
3242
3243 if (*mem_map_handle == 0) {
3244 pr_debug("%s: Map handle is 0, nothing to unmap\n",
3245 __func__);
3246 goto done;
3247 }
3248
3249 if (*mem_map_handle != atomic_read(
3250 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL])) {
3251 pr_err("%s: Map handles do not match! Unmapping RTAC, RTAC map 0x%x, ADM map 0x%x\n",
3252 __func__, *mem_map_handle, atomic_read(
3253 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]));
3254
3255 /* if mismatch use handle passed in to unmap */
3256 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL],
3257 *mem_map_handle);
3258 }
3259
3260 /* valid port ID needed for callback use primary I2S */
3261 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3262 result = adm_memory_unmap_regions();
3263 if (result < 0) {
3264 pr_debug("%s: adm_memory_unmap_regions failed, error %d\n",
3265 __func__, result);
3266 } else {
3267 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL], 0);
3268 *mem_map_handle = 0;
3269 }
3270done:
3271 return result;
3272}
3273
3274static int get_cal_type_index(int32_t cal_type)
3275{
3276 int ret = -EINVAL;
3277
3278 switch (cal_type) {
3279 case ADM_AUDPROC_CAL_TYPE:
3280 ret = ADM_AUDPROC_CAL;
3281 break;
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05303282 case ADM_LSM_AUDPROC_CAL_TYPE:
3283 ret = ADM_LSM_AUDPROC_CAL;
3284 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303285 case ADM_AUDVOL_CAL_TYPE:
3286 ret = ADM_AUDVOL_CAL;
3287 break;
3288 case ADM_CUST_TOPOLOGY_CAL_TYPE:
3289 ret = ADM_CUSTOM_TOP_CAL;
3290 break;
3291 case ADM_RTAC_INFO_CAL_TYPE:
3292 ret = ADM_RTAC_INFO_CAL;
3293 break;
3294 case ADM_RTAC_APR_CAL_TYPE:
3295 ret = ADM_RTAC_APR_CAL;
3296 break;
3297 case ADM_RTAC_AUDVOL_CAL_TYPE:
3298 ret = ADM_RTAC_AUDVOL_CAL;
3299 break;
3300 default:
3301 pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
3302 }
3303 return ret;
3304}
3305
3306static int adm_alloc_cal(int32_t cal_type, size_t data_size, void *data)
3307{
3308 int ret = 0;
3309 int cal_index;
3310
3311 pr_debug("%s:\n", __func__);
3312
3313 cal_index = get_cal_type_index(cal_type);
3314 if (cal_index < 0) {
3315 pr_err("%s: could not get cal index %d!\n",
3316 __func__, cal_index);
3317 ret = -EINVAL;
3318 goto done;
3319 }
3320
3321 ret = cal_utils_alloc_cal(data_size, data,
3322 this_adm.cal_data[cal_index], 0, NULL);
3323 if (ret < 0) {
3324 pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
3325 __func__, ret, cal_type);
3326 ret = -EINVAL;
3327 goto done;
3328 }
3329done:
3330 return ret;
3331}
3332
3333static int adm_dealloc_cal(int32_t cal_type, size_t data_size, void *data)
3334{
3335 int ret = 0;
3336 int cal_index;
3337
3338 pr_debug("%s:\n", __func__);
3339
3340 cal_index = get_cal_type_index(cal_type);
3341 if (cal_index < 0) {
3342 pr_err("%s: could not get cal index %d!\n",
3343 __func__, cal_index);
3344 ret = -EINVAL;
3345 goto done;
3346 }
3347
3348 ret = cal_utils_dealloc_cal(data_size, data,
3349 this_adm.cal_data[cal_index]);
3350 if (ret < 0) {
3351 pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
3352 __func__, ret, cal_type);
3353 ret = -EINVAL;
3354 goto done;
3355 }
3356done:
3357 return ret;
3358}
3359
3360static int adm_set_cal(int32_t cal_type, size_t data_size, void *data)
3361{
3362 int ret = 0;
3363 int cal_index;
3364
3365 pr_debug("%s:\n", __func__);
3366
3367 cal_index = get_cal_type_index(cal_type);
3368 if (cal_index < 0) {
3369 pr_err("%s: could not get cal index %d!\n",
3370 __func__, cal_index);
3371 ret = -EINVAL;
3372 goto done;
3373 }
3374
3375 ret = cal_utils_set_cal(data_size, data,
3376 this_adm.cal_data[cal_index], 0, NULL);
3377 if (ret < 0) {
3378 pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
3379 __func__, ret, cal_type);
3380 ret = -EINVAL;
3381 goto done;
3382 }
3383
3384 if (cal_index == ADM_CUSTOM_TOP_CAL) {
3385 mutex_lock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3386 this_adm.set_custom_topology = 1;
3387 mutex_unlock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3388 } else if (cal_index == ADM_RTAC_AUDVOL_CAL) {
3389 send_rtac_audvol_cal();
3390 }
3391done:
3392 return ret;
3393}
3394
3395static int adm_map_cal_data(int32_t cal_type,
3396 struct cal_block_data *cal_block)
3397{
3398 int ret = 0;
3399 int cal_index;
3400
3401 pr_debug("%s:\n", __func__);
3402
3403 cal_index = get_cal_type_index(cal_type);
3404 if (cal_index < 0) {
3405 pr_err("%s: could not get cal index %d!\n",
3406 __func__, cal_index);
3407 ret = -EINVAL;
3408 goto done;
3409 }
3410
3411 atomic_set(&this_adm.mem_map_index, cal_index);
3412 ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3413 (uint32_t *)&cal_block->map_data.map_size, 1);
3414 if (ret < 0) {
3415 pr_err("%s: map did not work! cal_type %i ret %d\n",
3416 __func__, cal_index, ret);
3417 ret = -ENODEV;
3418 goto done;
3419 }
3420 cal_block->map_data.q6map_handle = atomic_read(&this_adm.
3421 mem_map_handles[cal_index]);
3422done:
3423 return ret;
3424}
3425
3426static int adm_unmap_cal_data(int32_t cal_type,
3427 struct cal_block_data *cal_block)
3428{
3429 int ret = 0;
3430 int cal_index;
3431
3432 pr_debug("%s:\n", __func__);
3433
3434 cal_index = get_cal_type_index(cal_type);
3435 if (cal_index < 0) {
3436 pr_err("%s: could not get cal index %d!\n",
3437 __func__, cal_index);
3438 ret = -EINVAL;
3439 goto done;
3440 }
3441
3442 if (cal_block == NULL) {
3443 pr_err("%s: Cal block is NULL!\n",
3444 __func__);
3445 goto done;
3446 }
3447
3448 if (cal_block->map_data.q6map_handle == 0) {
3449 pr_err("%s: Map handle is NULL, nothing to unmap\n",
3450 __func__);
3451 goto done;
3452 }
3453
3454 atomic_set(&this_adm.mem_map_handles[cal_index],
3455 cal_block->map_data.q6map_handle);
3456 atomic_set(&this_adm.mem_map_index, cal_index);
3457 ret = adm_memory_unmap_regions();
3458 if (ret < 0) {
3459 pr_err("%s: unmap did not work! cal_type %i ret %d\n",
3460 __func__, cal_index, ret);
3461 ret = -ENODEV;
3462 goto done;
3463 }
3464 cal_block->map_data.q6map_handle = 0;
3465done:
3466 return ret;
3467}
3468
3469static void adm_delete_cal_data(void)
3470{
3471 pr_debug("%s:\n", __func__);
3472
3473 cal_utils_destroy_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data);
3474}
3475
3476static int adm_init_cal_data(void)
3477{
3478 int ret = 0;
3479 struct cal_type_info cal_type_info[] = {
3480 {{ADM_CUST_TOPOLOGY_CAL_TYPE,
3481 {adm_alloc_cal, adm_dealloc_cal, NULL,
3482 adm_set_cal, NULL, NULL} },
3483 {adm_map_cal_data, adm_unmap_cal_data,
3484 cal_utils_match_buf_num} },
3485
3486 {{ADM_AUDPROC_CAL_TYPE,
3487 {adm_alloc_cal, adm_dealloc_cal, NULL,
3488 adm_set_cal, NULL, NULL} },
3489 {adm_map_cal_data, adm_unmap_cal_data,
3490 cal_utils_match_buf_num} },
3491
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05303492 {{ADM_LSM_AUDPROC_CAL_TYPE,
3493 {adm_alloc_cal, adm_dealloc_cal, NULL,
3494 adm_set_cal, NULL, NULL} },
3495 {adm_map_cal_data, adm_unmap_cal_data,
3496 cal_utils_match_buf_num} },
3497
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303498 {{ADM_AUDVOL_CAL_TYPE,
3499 {adm_alloc_cal, adm_dealloc_cal, NULL,
3500 adm_set_cal, NULL, NULL} },
3501 {adm_map_cal_data, adm_unmap_cal_data,
3502 cal_utils_match_buf_num} },
3503
3504 {{ADM_RTAC_INFO_CAL_TYPE,
3505 {NULL, NULL, NULL, NULL, NULL, NULL} },
3506 {NULL, NULL, cal_utils_match_buf_num} },
3507
3508 {{ADM_RTAC_APR_CAL_TYPE,
3509 {NULL, NULL, NULL, NULL, NULL, NULL} },
3510 {NULL, NULL, cal_utils_match_buf_num} },
3511
3512 {{SRS_TRUMEDIA_CAL_TYPE,
3513 {NULL, NULL, NULL, NULL, NULL, NULL} },
3514 {NULL, NULL, cal_utils_match_buf_num} },
3515
3516 {{ADM_RTAC_AUDVOL_CAL_TYPE,
3517 {adm_alloc_cal, adm_dealloc_cal, NULL,
3518 adm_set_cal, NULL, NULL} },
3519 {adm_map_cal_data, adm_unmap_cal_data,
3520 cal_utils_match_buf_num} },
3521 };
3522 pr_debug("%s:\n", __func__);
3523
3524 ret = cal_utils_create_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data,
3525 cal_type_info);
3526 if (ret < 0) {
3527 pr_err("%s: could not create cal type! ret %d\n",
3528 __func__, ret);
3529 ret = -EINVAL;
3530 goto err;
3531 }
3532
3533 return ret;
3534err:
3535 adm_delete_cal_data();
3536 return ret;
3537}
3538
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303539/**
3540 * adm_set_volume -
3541 * command to set volume on ADM copp
3542 *
3543 * @port_id: Port ID number
3544 * @copp_idx: copp index assigned
3545 * @volume: gain value to set
3546 *
3547 * Returns 0 on success or error on failure
3548 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303549int adm_set_volume(int port_id, int copp_idx, int volume)
3550{
3551 struct audproc_volume_ctrl_master_gain audproc_vol;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003552 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303553 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303554
3555 pr_debug("%s: port_id %d, volume %d\n", __func__, port_id, volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303556
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003557 memset(&audproc_vol, 0, sizeof(audproc_vol));
3558 memset(&param_hdr, 0, sizeof(param_hdr));
3559 param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
3560 param_hdr.instance_id = INSTANCE_ID_0;
3561 param_hdr.param_id = AUDPROC_PARAM_ID_VOL_CTRL_MASTER_GAIN;
3562 param_hdr.param_size = sizeof(audproc_vol);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303563
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303564 audproc_vol.master_gain = volume;
3565
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003566 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
3567 (uint8_t *) &audproc_vol);
3568 if (rc)
3569 pr_err("%s: Failed to set volume, err %d\n", __func__, rc);
3570
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303571 return rc;
3572}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303573EXPORT_SYMBOL(adm_set_volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303574
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303575/**
3576 * adm_set_softvolume -
3577 * command to set softvolume
3578 *
3579 * @port_id: Port ID number
3580 * @copp_idx: copp index assigned
3581 * @softvol_param: Params to set for softvolume
3582 *
3583 * Returns 0 on success or error on failure
3584 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303585int adm_set_softvolume(int port_id, int copp_idx,
3586 struct audproc_softvolume_params *softvol_param)
3587{
3588 struct audproc_soft_step_volume_params audproc_softvol;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003589 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303590 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303591
3592 pr_debug("%s: period %d step %d curve %d\n", __func__,
3593 softvol_param->period, softvol_param->step,
3594 softvol_param->rampingcurve);
3595
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003596 memset(&audproc_softvol, 0, sizeof(audproc_softvol));
3597 memset(&param_hdr, 0, sizeof(param_hdr));
3598 param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
3599 param_hdr.instance_id = INSTANCE_ID_0;
3600 param_hdr.param_id = AUDPROC_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
3601 param_hdr.param_size = sizeof(audproc_softvol);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303602
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303603 audproc_softvol.period = softvol_param->period;
3604 audproc_softvol.step = softvol_param->step;
3605 audproc_softvol.ramping_curve = softvol_param->rampingcurve;
3606
3607 pr_debug("%s: period %d, step %d, curve %d\n", __func__,
3608 audproc_softvol.period, audproc_softvol.step,
3609 audproc_softvol.ramping_curve);
3610
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003611 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
3612 (uint8_t *) &audproc_softvol);
3613 if (rc)
3614 pr_err("%s: Failed to set soft volume, err %d\n", __func__, rc);
3615
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303616 return rc;
3617}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303618EXPORT_SYMBOL(adm_set_softvolume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303619
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303620/**
3621 * adm_set_mic_gain -
3622 * command to set MIC gain
3623 *
3624 * @port_id: Port ID number
3625 * @copp_idx: copp index assigned
3626 * @volume: gain value to set
3627 *
3628 * Returns 0 on success or error on failure
3629 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303630int adm_set_mic_gain(int port_id, int copp_idx, int volume)
3631{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003632 struct admx_mic_gain mic_gain_params;
3633 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303634 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303635
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003636 pr_debug("%s: Setting mic gain to %d at port_id 0x%x\n", __func__,
3637 volume, port_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303638
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003639 memset(&mic_gain_params, 0, sizeof(mic_gain_params));
3640 memset(&param_hdr, 0, sizeof(param_hdr));
3641 param_hdr.module_id = ADM_MODULE_IDX_MIC_GAIN_CTRL;
3642 param_hdr.instance_id = INSTANCE_ID_0;
3643 param_hdr.param_id = ADM_PARAM_IDX_MIC_GAIN;
3644 param_hdr.param_size = sizeof(mic_gain_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303645
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003646 mic_gain_params.tx_mic_gain = volume;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303647
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003648 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
3649 (uint8_t *) &mic_gain_params);
3650 if (rc)
3651 pr_err("%s: Failed to set mic gain, err %d\n", __func__, rc);
3652
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303653 return rc;
3654}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303655EXPORT_SYMBOL(adm_set_mic_gain);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303656
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303657/**
3658 * adm_send_set_multichannel_ec_primary_mic_ch -
3659 * command to set multi-ch EC primary mic
3660 *
3661 * @port_id: Port ID number
3662 * @copp_idx: copp index assigned
3663 * @primary_mic_ch: channel number of primary mic
3664 *
3665 * Returns 0 on success or error on failure
3666 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303667int adm_send_set_multichannel_ec_primary_mic_ch(int port_id, int copp_idx,
3668 int primary_mic_ch)
3669{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003670 struct admx_sec_primary_mic_ch sec_primary_ch_params;
3671 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303672 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303673
3674 pr_debug("%s port_id 0x%x, copp_idx 0x%x, primary_mic_ch %d\n",
3675 __func__, port_id, copp_idx, primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303676
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003677 memset(&sec_primary_ch_params, 0, sizeof(sec_primary_ch_params));
3678 memset(&param_hdr, 0, sizeof(param_hdr));
3679 param_hdr.module_id = AUDPROC_MODULE_ID_VOICE_TX_SECNS;
3680 param_hdr.instance_id = INSTANCE_ID_0;
3681 param_hdr.param_id = AUDPROC_PARAM_IDX_SEC_PRIMARY_MIC_CH;
3682 param_hdr.param_size = sizeof(sec_primary_ch_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303683
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003684 sec_primary_ch_params.version = 0;
3685 sec_primary_ch_params.sec_primary_mic_ch = primary_mic_ch;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303686
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003687 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
3688 (uint8_t *) &sec_primary_ch_params);
3689 if (rc)
3690 pr_err("%s: Failed to set primary mic chanel, err %d\n",
3691 __func__, rc);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303692
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303693 return rc;
3694}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303695EXPORT_SYMBOL(adm_send_set_multichannel_ec_primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303696
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303697/**
3698 * adm_param_enable -
3699 * command to send params to ADM for given module
3700 *
3701 * @port_id: Port ID number
3702 * @copp_idx: copp index assigned
3703 * @module_id: ADM module
3704 * @enable: flag to enable or disable module
3705 *
3706 * Returns 0 on success or error on failure
3707 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303708int adm_param_enable(int port_id, int copp_idx, int module_id, int enable)
3709{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003710 struct module_instance_info mod_inst_info;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303711
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003712 memset(&mod_inst_info, 0, sizeof(mod_inst_info));
3713 mod_inst_info.module_id = module_id;
3714 mod_inst_info.instance_id = INSTANCE_ID_0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303715
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003716 return adm_param_enable_v2(port_id, copp_idx, mod_inst_info, enable);
3717}
Vidyakumar Athota98464a22018-03-15 20:39:37 -07003718EXPORT_SYMBOL(adm_param_enable);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003719
3720/**
3721 * adm_param_enable_v2 -
3722 * command to send params to ADM for given module
3723 *
3724 * @port_id: Port ID number
3725 * @copp_idx: copp index assigned
3726 * @mod_inst_info: module and instance ID info
3727 * @enable: flag to enable or disable module
3728 *
3729 * Returns 0 on success or error on failure
3730 */
3731int adm_param_enable_v2(int port_id, int copp_idx,
3732 struct module_instance_info mod_inst_info, int enable)
3733{
3734 uint32_t enable_param;
3735 struct param_hdr_v3 param_hdr;
3736 int rc = 0;
3737
3738 if (enable < 0 || enable > 1) {
3739 pr_err("%s: Invalid value for enable %d\n", __func__, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303740 return -EINVAL;
3741 }
3742
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003743 pr_debug("%s port_id %d, module_id 0x%x, instance_id 0x%x, enable %d\n",
3744 __func__, port_id, mod_inst_info.module_id,
3745 mod_inst_info.instance_id, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303746
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003747 memset(&param_hdr, 0, sizeof(param_hdr));
3748 param_hdr.module_id = mod_inst_info.module_id;
3749 param_hdr.instance_id = mod_inst_info.instance_id;
3750 param_hdr.param_id = AUDPROC_PARAM_ID_ENABLE;
3751 param_hdr.param_size = sizeof(enable_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303752
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003753 enable_param = enable;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303754
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003755 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
3756 (uint8_t *) &enable_param);
3757 if (rc)
3758 pr_err("%s: Failed to set enable of module(%d) instance(%d) to %d, err %d\n",
3759 __func__, mod_inst_info.module_id,
3760 mod_inst_info.instance_id, enable, rc);
3761
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303762 return rc;
3763
3764}
Vidyakumar Athota98464a22018-03-15 20:39:37 -07003765EXPORT_SYMBOL(adm_param_enable_v2);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303766
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303767/**
3768 * adm_send_calibration -
3769 * send ADM calibration to DSP
3770 *
3771 * @port_id: Port ID number
3772 * @copp_idx: copp index assigned
3773 * @path: direction or ADM path type
3774 * @perf_mode: performance mode like LL/ULL/..
3775 * @cal_type: calibration type to use
3776 * @params: pointer with cal data
3777 * @size: cal size
3778 *
3779 * Returns 0 on success or error on failure
3780 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303781int adm_send_calibration(int port_id, int copp_idx, int path, int perf_mode,
3782 int cal_type, char *params, int size)
3783{
3784
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003785 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303786
3787 pr_debug("%s:port_id %d, path %d, perf_mode %d, cal_type %d, size %d\n",
3788 __func__, port_id, path, perf_mode, cal_type, size);
3789
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303790 /* Maps audio_dev_ctrl path definition to ACDB definition */
3791 if (get_cal_path(path) != RX_DEVICE) {
3792 pr_err("%s: acdb_path %d\n", __func__, path);
3793 rc = -EINVAL;
3794 goto end;
3795 }
3796
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003797 rc = adm_set_pp_params(port_id, copp_idx, NULL, (u8 *) params, size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303798
3799end:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303800 return rc;
3801}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303802EXPORT_SYMBOL(adm_send_calibration);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303803
3804/*
3805 * adm_update_wait_parameters must be called with routing driver locks.
3806 * adm_reset_wait_parameters must be called with routing driver locks.
3807 * set and reset parmeters are separated to make sure it is always called
3808 * under routing driver lock.
3809 * adm_wait_timeout is to block until timeout or interrupted. Timeout is
3810 * not a an error.
3811 */
3812int adm_set_wait_parameters(int port_id, int copp_idx)
3813{
3814
3815 int ret = 0, port_idx;
3816
3817 pr_debug("%s: port_id 0x%x, copp_idx %d\n", __func__, port_id,
3818 copp_idx);
3819 port_id = afe_convert_virtual_to_portid(port_id);
3820 port_idx = adm_validate_and_get_port_index(port_id);
3821 if (port_idx < 0) {
3822 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
3823 ret = -EINVAL;
3824 goto end;
3825 }
3826
3827 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3828 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3829 return -EINVAL;
3830 }
3831
3832 this_adm.copp.adm_delay[port_idx][copp_idx] = 1;
3833 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 0);
3834
3835end:
3836 return ret;
3837
3838}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303839EXPORT_SYMBOL(adm_set_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303840
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303841/**
3842 * adm_reset_wait_parameters -
3843 * reset wait parameters or ADM delay value
3844 *
3845 * @port_id: Port ID number
3846 * @copp_idx: copp index assigned
3847 *
3848 * Returns 0 on success or error on failure
3849 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303850int adm_reset_wait_parameters(int port_id, int copp_idx)
3851{
3852 int ret = 0, port_idx;
3853
3854 pr_debug("%s: port_id 0x%x copp_idx %d\n", __func__, port_id,
3855 copp_idx);
3856 port_id = afe_convert_virtual_to_portid(port_id);
3857 port_idx = adm_validate_and_get_port_index(port_id);
3858 if (port_idx < 0) {
3859 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
3860 ret = -EINVAL;
3861 goto end;
3862 }
3863
3864 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3865 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3866 return -EINVAL;
3867 }
3868
3869 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 1);
3870 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
3871
3872end:
3873 return ret;
3874}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303875EXPORT_SYMBOL(adm_reset_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303876
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303877/**
3878 * adm_wait_timeout -
3879 * ADM wait command after command send to DSP
3880 *
3881 * @port_id: Port ID number
3882 * @copp_idx: copp index assigned
3883 * @wait_time: value in ms for command timeout
3884 *
3885 * Returns 0 on success or error on failure
3886 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303887int adm_wait_timeout(int port_id, int copp_idx, int wait_time)
3888{
3889 int ret = 0, port_idx;
3890
3891 pr_debug("%s: port_id 0x%x, copp_idx %d, wait_time %d\n", __func__,
3892 port_id, copp_idx, wait_time);
3893 port_id = afe_convert_virtual_to_portid(port_id);
3894 port_idx = adm_validate_and_get_port_index(port_id);
3895 if (port_idx < 0) {
3896 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
3897 ret = -EINVAL;
3898 goto end;
3899 }
3900
3901 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3902 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3903 return -EINVAL;
3904 }
3905
3906 ret = wait_event_timeout(
3907 this_adm.copp.adm_delay_wait[port_idx][copp_idx],
3908 atomic_read(&this_adm.copp.adm_delay_stat[port_idx][copp_idx]),
3909 msecs_to_jiffies(wait_time));
3910 pr_debug("%s: return %d\n", __func__, ret);
3911 if (ret != 0)
3912 ret = -EINTR;
3913end:
3914 pr_debug("%s: return %d--\n", __func__, ret);
3915 return ret;
3916}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303917EXPORT_SYMBOL(adm_wait_timeout);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303918
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303919/**
3920 * adm_store_cal_data -
3921 * Retrieve calibration data for ADM copp device
3922 *
3923 * @port_id: Port ID number
3924 * @copp_idx: copp index assigned
3925 * @path: direction or copp type
3926 * @perf_mode: performance mode like LL/ULL/..
3927 * @cal_index: calibration index to use
3928 * @params: pointer to store cal data
3929 * @size: pointer to fill with cal size
3930 *
3931 * Returns 0 on success or error on failure
3932 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303933int adm_store_cal_data(int port_id, int copp_idx, int path, int perf_mode,
3934 int cal_index, char *params, int *size)
3935{
3936 int rc = 0;
3937 struct cal_block_data *cal_block = NULL;
3938 int app_type, acdb_id, port_idx, sample_rate;
3939
3940 if (this_adm.cal_data[cal_index] == NULL) {
3941 pr_debug("%s: cal_index %d not allocated!\n",
3942 __func__, cal_index);
3943 goto end;
3944 }
3945
3946 if (get_cal_path(path) != RX_DEVICE) {
3947 pr_debug("%s: Invalid path to store calibration %d\n",
3948 __func__, path);
3949 rc = -EINVAL;
3950 goto end;
3951 }
3952
3953 port_id = afe_convert_virtual_to_portid(port_id);
3954 port_idx = adm_validate_and_get_port_index(port_id);
3955 if (port_idx < 0) {
3956 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
3957 rc = -EINVAL;
3958 goto end;
3959 }
3960
3961 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3962 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3963 return -EINVAL;
3964 }
3965
3966 acdb_id = atomic_read(&this_adm.copp.acdb_id[port_idx][copp_idx]);
3967 app_type = atomic_read(&this_adm.copp.app_type[port_idx][copp_idx]);
3968 sample_rate = atomic_read(&this_adm.copp.rate[port_idx][copp_idx]);
3969
3970 mutex_lock(&this_adm.cal_data[cal_index]->lock);
3971 cal_block = adm_find_cal(cal_index, get_cal_path(path), app_type,
3972 acdb_id, sample_rate);
3973 if (cal_block == NULL)
3974 goto unlock;
3975
3976 if (cal_block->cal_data.size <= 0) {
3977 pr_debug("%s: No ADM cal send for port_id = 0x%x!\n",
3978 __func__, port_id);
3979 rc = -EINVAL;
3980 goto unlock;
3981 }
3982
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05303983 if (cal_index == ADM_AUDPROC_CAL || cal_index == ADM_LSM_AUDPROC_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303984 if (cal_block->cal_data.size > AUD_PROC_BLOCK_SIZE) {
3985 pr_err("%s:audproc:invalid size exp/actual[%zd, %d]\n",
3986 __func__, cal_block->cal_data.size, *size);
3987 rc = -ENOMEM;
3988 goto unlock;
3989 }
3990 } else if (cal_index == ADM_AUDVOL_CAL) {
3991 if (cal_block->cal_data.size > AUD_VOL_BLOCK_SIZE) {
3992 pr_err("%s:aud_vol:invalid size exp/actual[%zd, %d]\n",
3993 __func__, cal_block->cal_data.size, *size);
3994 rc = -ENOMEM;
3995 goto unlock;
3996 }
3997 } else {
3998 pr_debug("%s: Not valid calibration for dolby topolgy\n",
3999 __func__);
4000 rc = -EINVAL;
4001 goto unlock;
4002 }
4003 memcpy(params, cal_block->cal_data.kvaddr, cal_block->cal_data.size);
4004 *size = cal_block->cal_data.size;
4005
4006 pr_debug("%s:port_id %d, copp_idx %d, path %d",
4007 __func__, port_id, copp_idx, path);
4008 pr_debug("perf_mode %d, cal_type %d, size %d\n",
4009 perf_mode, cal_index, *size);
4010
4011unlock:
4012 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
4013end:
4014 return rc;
4015}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304016EXPORT_SYMBOL(adm_store_cal_data);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304017
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304018/**
4019 * adm_send_compressed_device_mute -
4020 * command to send mute for compressed device
4021 *
4022 * @port_id: Port ID number
4023 * @copp_idx: copp index assigned
4024 * @mute_on: flag to indicate mute or unmute
4025 *
4026 * Returns 0 on success or error on failure
4027 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304028int adm_send_compressed_device_mute(int port_id, int copp_idx, bool mute_on)
4029{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004030 u32 mute_param = mute_on ? 1 : 0;
4031 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304032 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304033
4034 pr_debug("%s port_id: 0x%x, copp_idx %d, mute_on: %d\n",
4035 __func__, port_id, copp_idx, mute_on);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304036
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004037 memset(&param_hdr, 0, sizeof(param_hdr));
4038 param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_MUTE;
4039 param_hdr.instance_id = INSTANCE_ID_0;
4040 param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_MUTE;
4041 param_hdr.param_size = sizeof(mute_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304042
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004043 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4044 (uint8_t *) &mute_param);
4045 if (ret)
4046 pr_err("%s: Failed to set mute, err %d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304047
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304048 return ret;
4049}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304050EXPORT_SYMBOL(adm_send_compressed_device_mute);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304051
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304052/**
4053 * adm_send_compressed_device_latency -
4054 * command to send latency for compressed device
4055 *
4056 * @port_id: Port ID number
4057 * @copp_idx: copp index assigned
4058 * @latency: latency value to pass
4059 *
4060 * Returns 0 on success or error on failure
4061 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304062int adm_send_compressed_device_latency(int port_id, int copp_idx, int latency)
4063{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004064 u32 latency_param;
4065 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304066 int ret = 0;
4067
4068 pr_debug("%s port_id: 0x%x, copp_idx %d latency: %d\n", __func__,
4069 port_id, copp_idx, latency);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004070
4071 if (latency < 0) {
4072 pr_err("%s: Invalid value for latency %d", __func__, latency);
4073 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304074 }
4075
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004076 memset(&param_hdr, 0, sizeof(param_hdr));
4077 param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_LATENCY;
4078 param_hdr.instance_id = INSTANCE_ID_0;
4079 param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_LATENCY;
4080 param_hdr.param_size = sizeof(latency_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304081
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004082 latency_param = latency;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304083
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004084 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4085 (uint8_t *) &latency_param);
4086 if (ret)
4087 pr_err("%s: Failed to set latency, err %d\n", __func__, ret);
4088
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304089 return ret;
4090}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304091EXPORT_SYMBOL(adm_send_compressed_device_latency);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304092
4093/**
4094 * adm_swap_speaker_channels
4095 *
4096 * Receives port_id, copp_idx, sample rate, spk_swap and
4097 * send MFC command to swap speaker channel.
4098 * Return zero on success. On failure returns nonzero.
4099 *
4100 * port_id - Passed value, port_id for which channels swap is wanted
4101 * copp_idx - Passed value, copp_idx for which channels swap is wanted
4102 * sample_rate - Passed value, sample rate used by app type config
4103 * spk_swap - Passed value, spk_swap for check if swap flag is set
4104 */
4105int adm_swap_speaker_channels(int port_id, int copp_idx,
4106 int sample_rate, bool spk_swap)
4107{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004108 struct audproc_mfc_param_media_fmt mfc_cfg;
4109 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304110 uint16_t num_channels;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004111 int port_idx = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304112 int ret = 0;
4113
4114 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4115 __func__, port_id, copp_idx);
4116 port_id = q6audio_convert_virtual_to_portid(port_id);
4117 port_idx = adm_validate_and_get_port_index(port_id);
4118 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
4119 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004120 return -EINVAL;
4121 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4122 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
4123 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304124 }
4125
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004126 num_channels = atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304127 if (num_channels != 2) {
4128 pr_debug("%s: Invalid number of channels: %d\n",
4129 __func__, num_channels);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004130 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304131 }
4132
4133 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004134 memset(&param_hdr, 0, sizeof(param_hdr));
4135
4136 param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
4137 param_hdr.instance_id = INSTANCE_ID_0;
4138 param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
4139 param_hdr.param_size = sizeof(mfc_cfg);
4140
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304141 mfc_cfg.sampling_rate = sample_rate;
4142 mfc_cfg.bits_per_sample =
4143 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
4144 mfc_cfg.num_channels = num_channels;
4145
4146 /* Currently applying speaker swap for only 2 channel use case */
4147 if (spk_swap) {
4148 mfc_cfg.channel_type[0] =
4149 (uint16_t) PCM_CHANNEL_FR;
4150 mfc_cfg.channel_type[1] =
4151 (uint16_t) PCM_CHANNEL_FL;
4152 } else {
4153 mfc_cfg.channel_type[0] =
4154 (uint16_t) PCM_CHANNEL_FL;
4155 mfc_cfg.channel_type[1] =
4156 (uint16_t) PCM_CHANNEL_FR;
4157 }
4158
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004159 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4160 (u8 *) &mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304161 if (ret < 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004162 pr_err("%s: Failed to set swap speaker channels on port[0x%x] failed %d\n",
4163 __func__, port_id, ret);
4164 return ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304165 }
4166
4167 pr_debug("%s: mfc_cfg Set params returned success", __func__);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004168 return 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304169}
4170EXPORT_SYMBOL(adm_swap_speaker_channels);
4171
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304172/**
4173 * adm_set_sound_focus -
4174 * Update sound focus info
4175 *
4176 * @port_id: Port ID number
4177 * @copp_idx: copp index assigned
4178 * @soundFocusData: sound focus data to pass
4179 *
4180 * Returns 0 on success or error on failure
4181 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304182int adm_set_sound_focus(int port_id, int copp_idx,
4183 struct sound_focus_param soundFocusData)
4184{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004185 struct adm_param_fluence_soundfocus_t soundfocus_params;
4186 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304187 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304188 int i;
4189
4190 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4191 __func__, port_id, copp_idx);
4192
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004193 memset(&param_hdr, 0, sizeof(param_hdr));
4194 param_hdr.module_id = VOICEPROC_MODULE_ID_GENERIC_TX;
4195 param_hdr.instance_id = INSTANCE_ID_0;
4196 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
4197 param_hdr.param_size = sizeof(soundfocus_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304198
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004199 memset(&(soundfocus_params), 0xFF, sizeof(soundfocus_params));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304200 for (i = 0; i < MAX_SECTORS; i++) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004201 soundfocus_params.start_angles[i] =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304202 soundFocusData.start_angle[i];
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004203 soundfocus_params.enables[i] = soundFocusData.enable[i];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304204 pr_debug("%s: start_angle[%d] = %d\n",
4205 __func__, i, soundFocusData.start_angle[i]);
4206 pr_debug("%s: enable[%d] = %d\n",
4207 __func__, i, soundFocusData.enable[i]);
4208 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004209 soundfocus_params.gain_step = soundFocusData.gain_step;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304210 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData.gain_step);
4211
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004212 soundfocus_params.reserved = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304213
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004214 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4215 (uint8_t *) &soundfocus_params);
4216 if (ret)
4217 pr_err("%s: Failed to set sound focus params, err %d\n",
4218 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304219
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304220 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
4221
4222 return ret;
4223}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304224EXPORT_SYMBOL(adm_set_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304225
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304226/**
4227 * adm_get_sound_focus -
4228 * Retrieve sound focus info
4229 *
4230 * @port_id: Port ID number
4231 * @copp_idx: copp index assigned
4232 * @soundFocusData: pointer for sound focus data to be updated with
4233 *
4234 * Returns 0 on success or error on failure
4235 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304236int adm_get_sound_focus(int port_id, int copp_idx,
4237 struct sound_focus_param *soundFocusData)
4238{
4239 int ret = 0, i;
4240 char *params_value;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004241 uint32_t max_param_size = 0;
4242 struct adm_param_fluence_soundfocus_t *soundfocus_params = NULL;
4243 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304244
4245 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4246 __func__, port_id, copp_idx);
4247
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004248 max_param_size = sizeof(struct adm_param_fluence_soundfocus_t) +
4249 sizeof(union param_hdrs);
4250 params_value = kzalloc(max_param_size, GFP_KERNEL);
4251 if (!params_value)
4252 return -ENOMEM;
4253
4254 memset(&param_hdr, 0, sizeof(param_hdr));
4255 param_hdr.module_id = VOICEPROC_MODULE_ID_GENERIC_TX;
4256 param_hdr.instance_id = INSTANCE_ID_0;
4257 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
4258 param_hdr.param_size = max_param_size;
4259 ret = adm_get_pp_params(port_id, copp_idx,
4260 ADM_CLIENT_ID_SOURCE_TRACKING, NULL, &param_hdr,
4261 params_value);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304262 if (ret) {
4263 pr_err("%s: get parameters failed ret:%d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304264 ret = -EINVAL;
4265 goto done;
4266 }
4267
4268 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
4269 pr_err("%s - get params returned error [%s]\n",
4270 __func__, adsp_err_get_err_str(
4271 this_adm.sourceTrackingData.apr_cmd_status));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304272 ret = adsp_err_get_lnx_err_code(
4273 this_adm.sourceTrackingData.apr_cmd_status);
4274 goto done;
4275 }
4276
4277 soundfocus_params = (struct adm_param_fluence_soundfocus_t *)
4278 params_value;
4279 for (i = 0; i < MAX_SECTORS; i++) {
4280 soundFocusData->start_angle[i] =
4281 soundfocus_params->start_angles[i];
4282 soundFocusData->enable[i] = soundfocus_params->enables[i];
4283 pr_debug("%s: start_angle[%d] = %d\n",
4284 __func__, i, soundFocusData->start_angle[i]);
4285 pr_debug("%s: enable[%d] = %d\n",
4286 __func__, i, soundFocusData->enable[i]);
4287 }
4288 soundFocusData->gain_step = soundfocus_params->gain_step;
4289 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData->gain_step);
4290
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304291done:
4292 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
4293
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004294 kfree(params_value);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304295 return ret;
4296}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304297EXPORT_SYMBOL(adm_get_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304298
4299static int adm_source_tracking_alloc_map_memory(void)
4300{
4301 int ret;
4302
4303 pr_debug("%s: Enter\n", __func__);
4304
Banajit Goswami08bb7362017-11-03 22:48:23 -07004305 ret = msm_audio_ion_alloc(&this_adm.sourceTrackingData.dma_buf,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304306 AUD_PROC_BLOCK_SIZE,
4307 &this_adm.sourceTrackingData.memmap.paddr,
4308 &this_adm.sourceTrackingData.memmap.size,
4309 &this_adm.sourceTrackingData.memmap.kvaddr);
4310 if (ret) {
4311 pr_err("%s: failed to allocate memory\n", __func__);
4312
4313 ret = -EINVAL;
4314 goto done;
4315 }
4316
4317 atomic_set(&this_adm.mem_map_index, ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
4318 ret = adm_memory_map_regions(&this_adm.sourceTrackingData.memmap.paddr,
4319 0,
4320 (uint32_t *)&this_adm.sourceTrackingData.memmap.size,
4321 1);
4322 if (ret < 0) {
4323 pr_err("%s: failed to map memory, paddr = 0x%pK, size = %d\n",
4324 __func__,
4325 (void *)this_adm.sourceTrackingData.memmap.paddr,
4326 (uint32_t)this_adm.sourceTrackingData.memmap.size);
4327
Banajit Goswami08bb7362017-11-03 22:48:23 -07004328 msm_audio_ion_free(this_adm.sourceTrackingData.dma_buf);
4329 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304330 this_adm.sourceTrackingData.memmap.size = 0;
4331 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
4332 this_adm.sourceTrackingData.memmap.paddr = 0;
4333 this_adm.sourceTrackingData.apr_cmd_status = -1;
4334 atomic_set(&this_adm.mem_map_handles
4335 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
4336
4337 ret = -EINVAL;
4338 goto done;
4339 }
4340 ret = 0;
4341 pr_debug("%s: paddr = 0x%pK, size = %d, mem_map_handle = 0x%x\n",
4342 __func__, (void *)this_adm.sourceTrackingData.memmap.paddr,
4343 (uint32_t)this_adm.sourceTrackingData.memmap.size,
4344 atomic_read(&this_adm.mem_map_handles
4345 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING]));
4346
4347done:
4348 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
4349
4350 return ret;
4351}
4352
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304353/**
4354 * adm_get_source_tracking -
4355 * Retrieve source tracking info
4356 *
4357 * @port_id: Port ID number
4358 * @copp_idx: copp index assigned
4359 * @sourceTrackingData: pointer for source track data to be updated with
4360 *
4361 * Returns 0 on success or error on failure
4362 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304363int adm_get_source_tracking(int port_id, int copp_idx,
4364 struct source_tracking_param *sourceTrackingData)
4365{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004366 struct adm_param_fluence_sourcetracking_t *source_tracking_params =
4367 NULL;
4368 struct mem_mapping_hdr mem_hdr;
4369 struct param_hdr_v3 param_hdr;
4370 int i = 0;
4371 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304372
4373 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4374 __func__, port_id, copp_idx);
4375
4376 if (!this_adm.sourceTrackingData.memmap.paddr) {
4377 /* Allocate and map shared memory for out of band usage */
4378 ret = adm_source_tracking_alloc_map_memory();
4379 if (ret != 0) {
4380 ret = -EINVAL;
4381 goto done;
4382 }
4383 }
4384
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004385 memset(&mem_hdr, 0, sizeof(mem_hdr));
4386 memset(&param_hdr, 0, sizeof(param_hdr));
4387 mem_hdr.data_payload_addr_lsw =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304388 lower_32_bits(this_adm.sourceTrackingData.memmap.paddr);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004389 mem_hdr.data_payload_addr_msw = msm_audio_populate_upper_32_bits(
4390 this_adm.sourceTrackingData.memmap.paddr);
4391 mem_hdr.mem_map_handle = atomic_read(
4392 &this_adm.mem_map_handles[ADM_MEM_MAP_INDEX_SOURCE_TRACKING]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304393
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004394 param_hdr.module_id = VOICEPROC_MODULE_ID_GENERIC_TX;
4395 param_hdr.instance_id = INSTANCE_ID_0;
4396 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOURCETRACKING;
4397 /*
4398 * This size should be the max size of the calibration data + header.
4399 * Use the union size to ensure max size is used.
4400 */
4401 param_hdr.param_size =
4402 sizeof(struct adm_param_fluence_sourcetracking_t) +
4403 sizeof(union param_hdrs);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304404
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004405 /*
4406 * Retrieving parameters out of band, so no need to provide a buffer for
4407 * the returned parameter data as it will be at the memory location
4408 * provided.
4409 */
4410 ret = adm_get_pp_params(port_id, copp_idx,
4411 ADM_CLIENT_ID_SOURCE_TRACKING, &mem_hdr,
4412 &param_hdr, NULL);
4413 if (ret) {
4414 pr_err("%s: Failed to get params, error %d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304415 goto done;
4416 }
4417
4418 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
4419 pr_err("%s - get params returned error [%s]\n",
4420 __func__, adsp_err_get_err_str(
4421 this_adm.sourceTrackingData.apr_cmd_status));
4422
4423 ret = adsp_err_get_lnx_err_code(
4424 this_adm.sourceTrackingData.apr_cmd_status);
4425 goto done;
4426 }
4427
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004428 /* How do we know what the param data was retrieved with for hdr size */
4429 source_tracking_params =
4430 (struct adm_param_fluence_sourcetracking_t
4431 *) (this_adm.sourceTrackingData.memmap.kvaddr +
4432 sizeof(struct param_hdr_v1));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304433 for (i = 0; i < MAX_SECTORS; i++) {
4434 sourceTrackingData->vad[i] = source_tracking_params->vad[i];
4435 pr_debug("%s: vad[%d] = %d\n",
4436 __func__, i, sourceTrackingData->vad[i]);
4437 }
4438 sourceTrackingData->doa_speech = source_tracking_params->doa_speech;
4439 pr_debug("%s: doa_speech = %d\n",
4440 __func__, sourceTrackingData->doa_speech);
4441
4442 for (i = 0; i < MAX_NOISE_SOURCE_INDICATORS; i++) {
4443 sourceTrackingData->doa_noise[i] =
4444 source_tracking_params->doa_noise[i];
4445 pr_debug("%s: doa_noise[%d] = %d\n",
4446 __func__, i, sourceTrackingData->doa_noise[i]);
4447 }
4448 for (i = 0; i < MAX_POLAR_ACTIVITY_INDICATORS; i++) {
4449 sourceTrackingData->polar_activity[i] =
4450 source_tracking_params->polar_activity[i];
4451 pr_debug("%s: polar_activity[%d] = %d\n",
4452 __func__, i, sourceTrackingData->polar_activity[i]);
4453 }
4454
4455 ret = 0;
4456
4457done:
4458 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
4459
4460 return ret;
4461}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304462EXPORT_SYMBOL(adm_get_source_tracking);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304463
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304464int __init adm_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304465{
4466 int i = 0, j;
4467
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304468 this_adm.ec_ref_rx = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304469 init_waitqueue_head(&this_adm.matrix_map_wait);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304470 init_waitqueue_head(&this_adm.adm_wait);
4471
4472 for (i = 0; i < AFE_MAX_PORTS; i++) {
4473 for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
4474 atomic_set(&this_adm.copp.id[i][j], RESET_COPP_ID);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304475 init_waitqueue_head(&this_adm.copp.wait[i][j]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304476 init_waitqueue_head(
4477 &this_adm.copp.adm_delay_wait[i][j]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304478 }
4479 }
4480
4481 if (adm_init_cal_data())
4482 pr_err("%s: could not init cal data!\n", __func__);
4483
Banajit Goswami08bb7362017-11-03 22:48:23 -07004484 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304485 this_adm.sourceTrackingData.memmap.size = 0;
4486 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
4487 this_adm.sourceTrackingData.memmap.paddr = 0;
4488 this_adm.sourceTrackingData.apr_cmd_status = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304489
4490 return 0;
4491}
4492
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05304493void adm_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304494{
Laxminath Kasam30ad7512017-11-28 12:40:22 +05304495 if (this_adm.apr)
4496 adm_reset_data();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304497 adm_delete_cal_data();
4498}