blob: f0b7121977fb2815b44b44b65e923e7c89c0f999 [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 Kulothungan60cc0352018-01-29 16:21:22 -0800483 struct param_hdr_v3 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) +
516 sizeof(struct param_hdr_v3) + 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) +
544 sizeof(struct param_hdr_v3) + 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]);
1859 if (cal_block == NULL)
1860 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
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05301999 if (cal_index == ADM_AUDPROC_CAL ||
2000 cal_index == ADM_LSM_AUDPROC_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302001 audproc_cal_info = cal_block->cal_info;
2002 if ((audproc_cal_info->path == path) &&
2003 (cal_block->cal_data.size > 0))
2004 return cal_block;
2005 } else if (cal_index == ADM_AUDVOL_CAL) {
2006 audvol_cal_info = cal_block->cal_info;
2007 if ((audvol_cal_info->path == path) &&
2008 (cal_block->cal_data.size > 0))
2009 return cal_block;
2010 }
2011 }
2012 pr_debug("%s: Can't find ADM cal for cal_index %d, path %d\n",
2013 __func__, cal_index, path);
2014 return NULL;
2015}
2016
2017static struct cal_block_data *adm_find_cal_by_app_type(int cal_index, int path,
2018 int app_type)
2019{
2020 struct list_head *ptr, *next;
2021 struct cal_block_data *cal_block = NULL;
2022 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2023 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2024
2025 pr_debug("%s\n", __func__);
2026
2027 list_for_each_safe(ptr, next,
2028 &this_adm.cal_data[cal_index]->cal_blocks) {
2029
2030 cal_block = list_entry(ptr,
2031 struct cal_block_data, list);
2032
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302033 if (cal_index == ADM_AUDPROC_CAL ||
2034 cal_index == ADM_LSM_AUDPROC_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302035 audproc_cal_info = cal_block->cal_info;
2036 if ((audproc_cal_info->path == path) &&
2037 (audproc_cal_info->app_type == app_type) &&
2038 (cal_block->cal_data.size > 0))
2039 return cal_block;
2040 } else if (cal_index == ADM_AUDVOL_CAL) {
2041 audvol_cal_info = cal_block->cal_info;
2042 if ((audvol_cal_info->path == path) &&
2043 (audvol_cal_info->app_type == app_type) &&
2044 (cal_block->cal_data.size > 0))
2045 return cal_block;
2046 }
2047 }
2048 pr_debug("%s: Can't find ADM cali for cal_index %d, path %d, app %d, defaulting to search by path\n",
2049 __func__, cal_index, path, app_type);
2050 return adm_find_cal_by_path(cal_index, path);
2051}
2052
2053
2054static struct cal_block_data *adm_find_cal(int cal_index, int path,
2055 int app_type, int acdb_id,
2056 int sample_rate)
2057{
2058 struct list_head *ptr, *next;
2059 struct cal_block_data *cal_block = NULL;
2060 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2061 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2062
2063 pr_debug("%s:\n", __func__);
2064
2065 list_for_each_safe(ptr, next,
2066 &this_adm.cal_data[cal_index]->cal_blocks) {
2067
2068 cal_block = list_entry(ptr,
2069 struct cal_block_data, list);
2070
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302071 if (cal_index == ADM_AUDPROC_CAL ||
2072 cal_index == ADM_LSM_AUDPROC_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302073 audproc_cal_info = cal_block->cal_info;
2074 if ((audproc_cal_info->path == path) &&
2075 (audproc_cal_info->app_type == app_type) &&
2076 (audproc_cal_info->acdb_id == acdb_id) &&
2077 (audproc_cal_info->sample_rate == sample_rate) &&
2078 (cal_block->cal_data.size > 0))
2079 return cal_block;
2080 } else if (cal_index == ADM_AUDVOL_CAL) {
2081 audvol_cal_info = cal_block->cal_info;
2082 if ((audvol_cal_info->path == path) &&
2083 (audvol_cal_info->app_type == app_type) &&
2084 (audvol_cal_info->acdb_id == acdb_id) &&
2085 (cal_block->cal_data.size > 0))
2086 return cal_block;
2087 }
2088 }
2089 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",
2090 __func__, cal_index, path, app_type, acdb_id, sample_rate);
2091 return adm_find_cal_by_app_type(cal_index, path, app_type);
2092}
2093
2094static int adm_remap_and_send_cal_block(int cal_index, int port_id,
2095 int copp_idx, struct cal_block_data *cal_block, int perf_mode,
2096 int app_type, int acdb_id, int sample_rate)
2097{
2098 int ret = 0;
2099
2100 pr_debug("%s: Sending cal_index cal %d\n", __func__, cal_index);
2101 ret = remap_cal_data(cal_block, cal_index);
2102 if (ret) {
2103 pr_err("%s: Remap_cal_data failed for cal %d!\n",
2104 __func__, cal_index);
2105 goto done;
2106 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002107 ret = send_adm_cal_block(port_id, copp_idx, cal_block, perf_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302108 if (ret < 0)
2109 pr_debug("%s: No cal sent for cal_index %d, port_id = 0x%x! ret %d sample_rate %d\n",
2110 __func__, cal_index, port_id, ret, sample_rate);
2111done:
2112 return ret;
2113}
2114
2115static void send_adm_cal_type(int cal_index, int path, int port_id,
2116 int copp_idx, int perf_mode, int app_type,
2117 int acdb_id, int sample_rate)
2118{
2119 struct cal_block_data *cal_block = NULL;
2120 int ret;
2121
2122 pr_debug("%s: cal index %d\n", __func__, cal_index);
2123
2124 if (this_adm.cal_data[cal_index] == NULL) {
2125 pr_debug("%s: cal_index %d not allocated!\n",
2126 __func__, cal_index);
2127 goto done;
2128 }
2129
2130 mutex_lock(&this_adm.cal_data[cal_index]->lock);
2131 cal_block = adm_find_cal(cal_index, path, app_type, acdb_id,
2132 sample_rate);
2133 if (cal_block == NULL)
2134 goto unlock;
2135
2136 ret = adm_remap_and_send_cal_block(cal_index, port_id, copp_idx,
2137 cal_block, perf_mode, app_type, acdb_id, sample_rate);
2138unlock:
2139 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
2140done:
2141 return;
2142}
2143
2144static int get_cal_path(int path)
2145{
2146 if (path == 0x1)
2147 return RX_DEVICE;
2148 else
2149 return TX_DEVICE;
2150}
2151
2152static void send_adm_cal(int port_id, int copp_idx, int path, int perf_mode,
Aditya Bavanari5106b562018-01-08 13:16:32 +05302153 int app_type, int acdb_id, int sample_rate,
2154 int passthr_mode)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302155{
2156 pr_debug("%s: port id 0x%x copp_idx %d\n", __func__, port_id, copp_idx);
2157
Aditya Bavanari5106b562018-01-08 13:16:32 +05302158 if (passthr_mode != LISTEN)
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302159 send_adm_cal_type(ADM_AUDPROC_CAL, path, port_id, copp_idx,
2160 perf_mode, app_type, acdb_id, sample_rate);
2161 else
2162 send_adm_cal_type(ADM_LSM_AUDPROC_CAL, path, port_id, copp_idx,
2163 perf_mode, app_type, acdb_id, sample_rate);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302164 send_adm_cal_type(ADM_AUDVOL_CAL, path, port_id, copp_idx, perf_mode,
2165 app_type, acdb_id, sample_rate);
2166}
2167
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302168/**
2169 * adm_connect_afe_port -
2170 * command to send ADM connect AFE port
2171 *
2172 * @mode: value of mode for ADM connect AFE
2173 * @session_id: session active to connect
2174 * @port_id: Port ID number
2175 *
2176 * Returns 0 on success or error on failure
2177 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302178int adm_connect_afe_port(int mode, int session_id, int port_id)
2179{
2180 struct adm_cmd_connect_afe_port_v5 cmd;
2181 int ret = 0;
2182 int port_idx, copp_idx = 0;
2183
2184 pr_debug("%s: port_id: 0x%x session id:%d mode:%d\n", __func__,
2185 port_id, session_id, mode);
2186
2187 port_id = afe_convert_virtual_to_portid(port_id);
2188 port_idx = adm_validate_and_get_port_index(port_id);
2189 if (port_idx < 0) {
2190 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2191 return -EINVAL;
2192 }
2193
2194 if (this_adm.apr == NULL) {
2195 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
2196 0xFFFFFFFF, &this_adm);
2197 if (this_adm.apr == NULL) {
2198 pr_err("%s: Unable to register ADM\n", __func__);
2199 ret = -ENODEV;
2200 return ret;
2201 }
2202 rtac_set_adm_handle(this_adm.apr);
2203 }
2204 pr_debug("%s: Port ID 0x%x, index %d\n", __func__, port_id, port_idx);
2205
2206 cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2207 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
2208 cmd.hdr.pkt_size = sizeof(cmd);
2209 cmd.hdr.src_svc = APR_SVC_ADM;
2210 cmd.hdr.src_domain = APR_DOMAIN_APPS;
2211 cmd.hdr.src_port = port_id;
2212 cmd.hdr.dest_svc = APR_SVC_ADM;
2213 cmd.hdr.dest_domain = APR_DOMAIN_ADSP;
2214 cmd.hdr.dest_port = 0; /* Ignored */
2215 cmd.hdr.token = port_idx << 16 | copp_idx;
2216 cmd.hdr.opcode = ADM_CMD_CONNECT_AFE_PORT_V5;
2217
2218 cmd.mode = mode;
2219 cmd.session_id = session_id;
2220 cmd.afe_port_id = port_id;
2221
2222 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2223 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&cmd);
2224 if (ret < 0) {
2225 pr_err("%s: ADM enable for port_id: 0x%x failed ret %d\n",
2226 __func__, port_id, ret);
2227 ret = -EINVAL;
2228 goto fail_cmd;
2229 }
2230 /* Wait for the callback with copp id */
2231 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
2232 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
2233 msecs_to_jiffies(TIMEOUT_MS));
2234 if (!ret) {
2235 pr_err("%s: ADM connect timedout for port_id: 0x%x\n",
2236 __func__, port_id);
2237 ret = -EINVAL;
2238 goto fail_cmd;
2239 } else if (atomic_read(&this_adm.copp.stat
2240 [port_idx][copp_idx]) > 0) {
2241 pr_err("%s: DSP returned error[%s]\n",
2242 __func__, adsp_err_get_err_str(
2243 atomic_read(&this_adm.copp.stat
2244 [port_idx][copp_idx])));
2245 ret = adsp_err_get_lnx_err_code(
2246 atomic_read(&this_adm.copp.stat
2247 [port_idx][copp_idx]));
2248 goto fail_cmd;
2249 }
2250 atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
2251 return 0;
2252
2253fail_cmd:
2254
2255 return ret;
2256}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302257EXPORT_SYMBOL(adm_connect_afe_port);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302258
2259int adm_arrange_mch_map(struct adm_cmd_device_open_v5 *open, int path,
2260 int channel_mode)
2261{
2262 int rc = 0, idx;
2263
2264 memset(open->dev_channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
2265 switch (path) {
2266 case ADM_PATH_PLAYBACK:
2267 idx = ADM_MCH_MAP_IDX_PLAYBACK;
2268 break;
2269 case ADM_PATH_LIVE_REC:
2270 case ADM_PATH_NONLIVE_REC:
2271 idx = ADM_MCH_MAP_IDX_REC;
2272 break;
2273 default:
2274 goto non_mch_path;
2275 };
2276 if ((open->dev_num_channel > 2) && multi_ch_maps[idx].set_channel_map) {
2277 memcpy(open->dev_channel_mapping,
2278 multi_ch_maps[idx].channel_mapping,
2279 PCM_FORMAT_MAX_NUM_CHANNEL);
2280 } else {
2281 if (channel_mode == 1) {
2282 open->dev_channel_mapping[0] = PCM_CHANNEL_FC;
2283 } else if (channel_mode == 2) {
2284 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2285 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2286 } else if (channel_mode == 3) {
2287 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2288 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2289 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2290 } else if (channel_mode == 4) {
2291 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2292 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2293 open->dev_channel_mapping[2] = PCM_CHANNEL_LS;
2294 open->dev_channel_mapping[3] = PCM_CHANNEL_RS;
2295 } else if (channel_mode == 5) {
2296 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2297 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2298 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2299 open->dev_channel_mapping[3] = PCM_CHANNEL_LS;
2300 open->dev_channel_mapping[4] = PCM_CHANNEL_RS;
2301 } else if (channel_mode == 6) {
2302 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2303 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2304 open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2305 open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2306 open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2307 open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2308 } else if (channel_mode == 7) {
2309 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2310 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2311 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2312 open->dev_channel_mapping[3] = PCM_CHANNEL_LFE;
2313 open->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2314 open->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2315 open->dev_channel_mapping[6] = PCM_CHANNEL_CS;
2316 } else if (channel_mode == 8) {
2317 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2318 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2319 open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2320 open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2321 open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2322 open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2323 open->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2324 open->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2325 } else {
2326 pr_err("%s: invalid num_chan %d\n", __func__,
2327 channel_mode);
2328 rc = -EINVAL;
2329 goto inval_ch_mod;
2330 }
2331 }
2332
2333non_mch_path:
2334inval_ch_mod:
2335 return rc;
2336}
2337
2338int adm_arrange_mch_ep2_map(struct adm_cmd_device_open_v6 *open_v6,
2339 int channel_mode)
2340{
2341 int rc = 0;
2342
2343 memset(open_v6->dev_channel_mapping_eid2, 0,
2344 PCM_FORMAT_MAX_NUM_CHANNEL);
2345
2346 if (channel_mode == 1) {
2347 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FC;
2348 } else if (channel_mode == 2) {
2349 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2350 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2351 } else if (channel_mode == 3) {
2352 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2353 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2354 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
2355 } else if (channel_mode == 4) {
2356 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2357 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2358 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LS;
2359 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_RS;
2360 } else if (channel_mode == 5) {
2361 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2362 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2363 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
2364 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_LS;
2365 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_RS;
2366 } else if (channel_mode == 6) {
2367 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2368 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2369 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
2370 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
2371 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
2372 open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
2373 } else if (channel_mode == 8) {
2374 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2375 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2376 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
2377 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
2378 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
2379 open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
2380 open_v6->dev_channel_mapping_eid2[6] = PCM_CHANNEL_LB;
2381 open_v6->dev_channel_mapping_eid2[7] = PCM_CHANNEL_RB;
2382 } else {
2383 pr_err("%s: invalid num_chan %d\n", __func__,
2384 channel_mode);
2385 rc = -EINVAL;
2386 }
2387
2388 return rc;
2389}
2390
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302391/**
2392 * adm_open -
2393 * command to send ADM open
2394 *
2395 * @port_id: port id number
2396 * @path: direction or ADM path type
2397 * @rate: sample rate of session
2398 * @channel_mode: number of channels set
2399 * @topology: topology active for this session
2400 * @perf_mode: performance mode like LL/ULL/..
2401 * @bit_width: bit width to set for copp
2402 * @app_type: App type used for this session
2403 * @acdb_id: ACDB ID of this device
2404 *
2405 * Returns 0 on success or error on failure
2406 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302407int adm_open(int port_id, int path, int rate, int channel_mode, int topology,
2408 int perf_mode, uint16_t bit_width, int app_type, int acdb_id)
2409{
2410 struct adm_cmd_device_open_v5 open;
2411 struct adm_cmd_device_open_v6 open_v6;
2412 int ret = 0;
Asish Bhattacharya34504582017-08-08 12:55:01 +05302413 int port_idx, flags;
2414 int copp_idx = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302415 int tmp_port = q6audio_get_port_id(port_id);
2416
2417 pr_debug("%s:port %#x path:%d rate:%d mode:%d perf_mode:%d,topo_id %d\n",
2418 __func__, port_id, path, rate, channel_mode, perf_mode,
2419 topology);
2420
2421 port_id = q6audio_convert_virtual_to_portid(port_id);
2422 port_idx = adm_validate_and_get_port_index(port_id);
2423 if (port_idx < 0) {
2424 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2425 return -EINVAL;
2426 }
2427
2428 if (this_adm.apr == NULL) {
2429 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
2430 0xFFFFFFFF, &this_adm);
2431 if (this_adm.apr == NULL) {
2432 pr_err("%s: Unable to register ADM\n", __func__);
2433 return -ENODEV;
2434 }
2435 rtac_set_adm_handle(this_adm.apr);
2436 }
2437
2438 if (perf_mode == ULL_POST_PROCESSING_PCM_MODE) {
2439 flags = ADM_ULL_POST_PROCESSING_DEVICE_SESSION;
2440 if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
2441 (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
2442 (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
2443 topology = DEFAULT_COPP_TOPOLOGY;
2444 } else if (perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) {
2445 flags = ADM_ULTRA_LOW_LATENCY_DEVICE_SESSION;
2446 topology = NULL_COPP_TOPOLOGY;
2447 rate = ULL_SUPPORTED_SAMPLE_RATE;
2448 bit_width = ULL_SUPPORTED_BITS_PER_SAMPLE;
2449 } else if (perf_mode == LOW_LATENCY_PCM_MODE) {
2450 flags = ADM_LOW_LATENCY_DEVICE_SESSION;
2451 if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
2452 (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
2453 (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
2454 topology = DEFAULT_COPP_TOPOLOGY;
2455 } else {
2456 if ((path == ADM_PATH_COMPRESSED_RX) ||
2457 (path == ADM_PATH_COMPRESSED_TX))
2458 flags = 0;
2459 else
2460 flags = ADM_LEGACY_DEVICE_SESSION;
2461 }
2462
Laxminath Kasam8f7ccc22017-08-28 17:35:04 +05302463 if ((topology == VPM_TX_SM_ECNS_V2_COPP_TOPOLOGY) ||
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302464 (topology == VPM_TX_DM_FLUENCE_COPP_TOPOLOGY) ||
2465 (topology == VPM_TX_DM_RFECNS_COPP_TOPOLOGY))
2466 rate = 16000;
2467
Asish Bhattacharya34504582017-08-08 12:55:01 +05302468 /*
2469 * Routing driver reuses the same adm for streams with the same
2470 * app_type, sample_rate etc.
2471 * This isn't allowed for ULL streams as per the DSP interface
2472 */
2473 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE)
2474 copp_idx = adm_get_idx_if_copp_exists(port_idx, topology,
2475 perf_mode,
2476 rate, bit_width,
2477 app_type);
2478
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302479 if (copp_idx < 0) {
2480 copp_idx = adm_get_next_available_copp(port_idx);
2481 if (copp_idx >= MAX_COPPS_PER_PORT) {
2482 pr_err("%s: exceeded copp id %d\n",
2483 __func__, copp_idx);
2484 return -EINVAL;
2485 }
2486 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
2487 atomic_set(&this_adm.copp.topology[port_idx][copp_idx],
2488 topology);
2489 atomic_set(&this_adm.copp.mode[port_idx][copp_idx],
2490 perf_mode);
2491 atomic_set(&this_adm.copp.rate[port_idx][copp_idx],
2492 rate);
2493 atomic_set(&this_adm.copp.channels[port_idx][copp_idx],
2494 channel_mode);
2495 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx],
2496 bit_width);
2497 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx],
2498 app_type);
2499 atomic_set(&this_adm.copp.acdb_id[port_idx][copp_idx],
2500 acdb_id);
2501 set_bit(ADM_STATUS_CALIBRATION_REQUIRED,
2502 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
2503 if ((path != ADM_PATH_COMPRESSED_RX) &&
2504 (path != ADM_PATH_COMPRESSED_TX))
2505 send_adm_custom_topology();
2506 }
2507
2508 if (this_adm.copp.adm_delay[port_idx][copp_idx] &&
2509 perf_mode == LEGACY_PCM_MODE) {
2510 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
2511 1);
2512 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
2513 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
2514 }
2515
2516 /* Create a COPP if port id are not enabled */
2517 if (atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]) == 0) {
2518 pr_debug("%s: open ADM: port_idx: %d, copp_idx: %d\n", __func__,
2519 port_idx, copp_idx);
2520 if ((topology == SRS_TRUMEDIA_TOPOLOGY_ID) &&
2521 perf_mode == LEGACY_PCM_MODE) {
2522 int res;
2523
2524 atomic_set(&this_adm.mem_map_index, ADM_SRS_TRUMEDIA);
2525 msm_dts_srs_tm_ion_memmap(&this_adm.outband_memmap);
2526 res = adm_memory_map_regions(&this_adm.outband_memmap.paddr, 0,
2527 (uint32_t *)&this_adm.outband_memmap.size, 1);
2528 if (res < 0) {
2529 pr_err("%s: SRS adm_memory_map_regions failed ! addr = 0x%pK, size = %d\n",
2530 __func__, (void *)this_adm.outband_memmap.paddr,
2531 (uint32_t)this_adm.outband_memmap.size);
2532 }
2533 }
2534 open.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2535 APR_HDR_LEN(APR_HDR_SIZE),
2536 APR_PKT_VER);
2537 open.hdr.pkt_size = sizeof(open);
2538 open.hdr.src_svc = APR_SVC_ADM;
2539 open.hdr.src_domain = APR_DOMAIN_APPS;
2540 open.hdr.src_port = tmp_port;
2541 open.hdr.dest_svc = APR_SVC_ADM;
2542 open.hdr.dest_domain = APR_DOMAIN_ADSP;
2543 open.hdr.dest_port = tmp_port;
2544 open.hdr.token = port_idx << 16 | copp_idx;
2545 open.hdr.opcode = ADM_CMD_DEVICE_OPEN_V5;
2546 open.flags = flags;
2547 open.mode_of_operation = path;
2548 open.endpoint_id_1 = tmp_port;
2549 open.endpoint_id_2 = 0xFFFF;
2550
2551 if (this_adm.ec_ref_rx && (path != 1)) {
2552 open.endpoint_id_2 = this_adm.ec_ref_rx;
2553 this_adm.ec_ref_rx = -1;
2554 }
2555
2556 open.topology_id = topology;
2557
2558 open.dev_num_channel = channel_mode & 0x00FF;
2559 open.bit_width = bit_width;
2560 WARN_ON((perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) &&
2561 (rate != ULL_SUPPORTED_SAMPLE_RATE));
2562 open.sample_rate = rate;
2563
2564 ret = adm_arrange_mch_map(&open, path, channel_mode);
2565
2566 if (ret)
2567 return ret;
2568
2569 pr_debug("%s: port_id=0x%x rate=%d topology_id=0x%X\n",
2570 __func__, open.endpoint_id_1, open.sample_rate,
2571 open.topology_id);
2572
2573 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2574
2575 if ((this_adm.num_ec_ref_rx_chans != 0) && (path != 1) &&
2576 (open.endpoint_id_2 != 0xFFFF)) {
2577 memset(&open_v6, 0,
2578 sizeof(struct adm_cmd_device_open_v6));
2579 memcpy(&open_v6, &open,
2580 sizeof(struct adm_cmd_device_open_v5));
2581 open_v6.hdr.opcode = ADM_CMD_DEVICE_OPEN_V6;
2582 open_v6.hdr.pkt_size = sizeof(open_v6);
2583 open_v6.dev_num_channel_eid2 =
2584 this_adm.num_ec_ref_rx_chans;
2585 this_adm.num_ec_ref_rx_chans = 0;
2586
2587 if (this_adm.ec_ref_rx_bit_width != 0) {
2588 open_v6.bit_width_eid2 =
2589 this_adm.ec_ref_rx_bit_width;
2590 this_adm.ec_ref_rx_bit_width = 0;
2591 } else {
2592 open_v6.bit_width_eid2 = bit_width;
2593 }
2594
2595 if (this_adm.ec_ref_rx_sampling_rate != 0) {
2596 open_v6.sample_rate_eid2 =
2597 this_adm.ec_ref_rx_sampling_rate;
2598 this_adm.ec_ref_rx_sampling_rate = 0;
2599 } else {
2600 open_v6.sample_rate_eid2 = rate;
2601 }
2602
2603 pr_debug("%s: eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
2604 __func__, open_v6.dev_num_channel_eid2,
2605 open_v6.bit_width_eid2,
2606 open_v6.sample_rate_eid2);
2607
2608 ret = adm_arrange_mch_ep2_map(&open_v6,
2609 open_v6.dev_num_channel_eid2);
2610
2611 if (ret)
2612 return ret;
2613
2614 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&open_v6);
2615 } else {
2616 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&open);
2617 }
2618 if (ret < 0) {
2619 pr_err("%s: port_id: 0x%x for[0x%x] failed %d\n",
2620 __func__, tmp_port, port_id, ret);
2621 return -EINVAL;
2622 }
2623 /* Wait for the callback with copp id */
2624 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
2625 atomic_read(&this_adm.copp.stat
2626 [port_idx][copp_idx]) >= 0,
2627 msecs_to_jiffies(TIMEOUT_MS));
2628 if (!ret) {
2629 pr_err("%s: ADM open timedout for port_id: 0x%x for [0x%x]\n",
2630 __func__, tmp_port, port_id);
2631 return -EINVAL;
2632 } else if (atomic_read(&this_adm.copp.stat
2633 [port_idx][copp_idx]) > 0) {
2634 pr_err("%s: DSP returned error[%s]\n",
2635 __func__, adsp_err_get_err_str(
2636 atomic_read(&this_adm.copp.stat
2637 [port_idx][copp_idx])));
2638 return adsp_err_get_lnx_err_code(
2639 atomic_read(&this_adm.copp.stat
2640 [port_idx][copp_idx]));
2641 }
2642 }
2643 atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
2644 return copp_idx;
2645}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302646EXPORT_SYMBOL(adm_open);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302647
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302648/**
2649 * adm_copp_mfc_cfg -
2650 * command to send ADM MFC config
2651 *
2652 * @port_id: Port ID number
2653 * @copp_idx: copp index assigned
2654 * @dst_sample_rate: sink sample rate
2655 *
2656 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302657void adm_copp_mfc_cfg(int port_id, int copp_idx, int dst_sample_rate)
2658{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002659 struct audproc_mfc_param_media_fmt mfc_cfg;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302660 struct adm_cmd_device_open_v5 open;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002661 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302662 int port_idx;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302663 int rc = 0;
2664 int i = 0;
2665
2666 port_id = q6audio_convert_virtual_to_portid(port_id);
2667 port_idx = adm_validate_and_get_port_index(port_id);
2668
2669 if (port_idx < 0) {
2670 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
2671 goto fail_cmd;
2672 }
2673
2674 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
2675 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
2676 goto fail_cmd;
2677 }
2678
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002679 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
2680 memset(&open, 0, sizeof(open));
2681 memset(&param_hdr, 0, sizeof(param_hdr));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302682
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002683 param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
2684 param_hdr.instance_id = INSTANCE_ID_0;
2685 param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
2686 param_hdr.param_size = sizeof(mfc_cfg);
2687
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302688 mfc_cfg.sampling_rate = dst_sample_rate;
2689 mfc_cfg.bits_per_sample =
2690 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
2691 open.dev_num_channel = mfc_cfg.num_channels =
2692 atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
2693
2694 rc = adm_arrange_mch_map(&open, ADM_PATH_PLAYBACK,
2695 mfc_cfg.num_channels);
2696 if (rc < 0) {
2697 pr_err("%s: unable to get channal map\n", __func__);
2698 goto fail_cmd;
2699 }
2700
2701 for (i = 0; i < mfc_cfg.num_channels; i++)
2702 mfc_cfg.channel_type[i] =
2703 (uint16_t) open.dev_channel_mapping[i];
2704
2705 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2706
2707 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",
2708 __func__, port_idx, copp_idx,
2709 atomic_read(&this_adm.copp.rate[port_idx][copp_idx]),
2710 mfc_cfg.bits_per_sample, mfc_cfg.num_channels,
2711 mfc_cfg.sampling_rate);
2712
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002713 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
2714 (uint8_t *) &mfc_cfg);
2715 if (rc)
2716 pr_err("%s: Failed to set media format configuration data, err %d\n",
2717 __func__, rc);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302718
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302719fail_cmd:
2720 return;
2721}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302722EXPORT_SYMBOL(adm_copp_mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302723
2724static void route_set_opcode_matrix_id(
2725 struct adm_cmd_matrix_map_routings_v5 **route_addr,
2726 int path, uint32_t passthr_mode)
2727{
2728 struct adm_cmd_matrix_map_routings_v5 *route = *route_addr;
2729
2730 switch (path) {
2731 case ADM_PATH_PLAYBACK:
2732 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
2733 route->matrix_id = ADM_MATRIX_ID_AUDIO_RX;
2734 break;
2735 case ADM_PATH_LIVE_REC:
2736 if (passthr_mode == LISTEN) {
2737 route->hdr.opcode =
2738 ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
2739 route->matrix_id = ADM_MATRIX_ID_LISTEN_TX;
2740 break;
2741 }
2742 /* fall through to set matrix id for non-listen case */
2743 case ADM_PATH_NONLIVE_REC:
2744 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
2745 route->matrix_id = ADM_MATRIX_ID_AUDIO_TX;
2746 break;
2747 case ADM_PATH_COMPRESSED_RX:
2748 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
2749 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_RX;
2750 break;
2751 case ADM_PATH_COMPRESSED_TX:
2752 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
2753 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_TX;
2754 break;
2755 default:
2756 pr_err("%s: Wrong path set[%d]\n", __func__, path);
2757 break;
2758 }
2759 pr_debug("%s: opcode 0x%x, matrix id %d\n",
2760 __func__, route->hdr.opcode, route->matrix_id);
2761}
2762
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302763/**
2764 * adm_matrix_map -
2765 * command to send ADM matrix map for ADM copp list
2766 *
2767 * @path: direction or ADM path type
2768 * @payload_map: have info of session id and associated copp_idx/num_copps
2769 * @perf_mode: performance mode like LL/ULL/..
2770 * @passthr_mode: flag to indicate passthrough mode
2771 *
2772 * Returns 0 on success or error on failure
2773 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302774int adm_matrix_map(int path, struct route_payload payload_map, int perf_mode,
2775 uint32_t passthr_mode)
2776{
2777 struct adm_cmd_matrix_map_routings_v5 *route;
2778 struct adm_session_map_node_v5 *node;
2779 uint16_t *copps_list;
2780 int cmd_size = 0;
2781 int ret = 0, i = 0;
2782 void *payload = NULL;
2783 void *matrix_map = NULL;
2784 int port_idx, copp_idx;
2785
2786 /* Assumes port_ids have already been validated during adm_open */
2787 cmd_size = (sizeof(struct adm_cmd_matrix_map_routings_v5) +
2788 sizeof(struct adm_session_map_node_v5) +
2789 (sizeof(uint32_t) * payload_map.num_copps));
2790 matrix_map = kzalloc(cmd_size, GFP_KERNEL);
2791 if (matrix_map == NULL) {
2792 pr_err("%s: Mem alloc failed\n", __func__);
2793 ret = -EINVAL;
2794 return ret;
2795 }
2796 route = (struct adm_cmd_matrix_map_routings_v5 *)matrix_map;
2797
2798 route->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2799 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
2800 route->hdr.pkt_size = cmd_size;
2801 route->hdr.src_svc = 0;
2802 route->hdr.src_domain = APR_DOMAIN_APPS;
2803 route->hdr.src_port = 0; /* Ignored */;
2804 route->hdr.dest_svc = APR_SVC_ADM;
2805 route->hdr.dest_domain = APR_DOMAIN_ADSP;
2806 route->hdr.dest_port = 0; /* Ignored */;
2807 route->hdr.token = 0;
2808 route->num_sessions = 1;
2809 route_set_opcode_matrix_id(&route, path, passthr_mode);
2810
2811 payload = ((u8 *)matrix_map +
2812 sizeof(struct adm_cmd_matrix_map_routings_v5));
2813 node = (struct adm_session_map_node_v5 *)payload;
2814
2815 node->session_id = payload_map.session_id;
2816 node->num_copps = payload_map.num_copps;
2817 payload = (u8 *)node + sizeof(struct adm_session_map_node_v5);
2818 copps_list = (uint16_t *)payload;
2819 for (i = 0; i < payload_map.num_copps; i++) {
2820 port_idx =
2821 adm_validate_and_get_port_index(payload_map.port_id[i]);
2822 if (port_idx < 0) {
2823 pr_err("%s: Invalid port_id 0x%x\n", __func__,
2824 payload_map.port_id[i]);
2825 ret = -EINVAL;
2826 goto fail_cmd;
2827 }
2828 copp_idx = payload_map.copp_idx[i];
2829 copps_list[i] = atomic_read(&this_adm.copp.id[port_idx]
2830 [copp_idx]);
2831 }
2832 atomic_set(&this_adm.matrix_map_stat, -1);
2833
2834 ret = apr_send_pkt(this_adm.apr, (uint32_t *)matrix_map);
2835 if (ret < 0) {
2836 pr_err("%s: routing for syream %d failed ret %d\n",
2837 __func__, payload_map.session_id, ret);
2838 ret = -EINVAL;
2839 goto fail_cmd;
2840 }
2841 ret = wait_event_timeout(this_adm.matrix_map_wait,
2842 atomic_read(&this_adm.matrix_map_stat) >= 0,
2843 msecs_to_jiffies(TIMEOUT_MS));
2844 if (!ret) {
2845 pr_err("%s: routing for syream %d failed\n", __func__,
2846 payload_map.session_id);
2847 ret = -EINVAL;
2848 goto fail_cmd;
2849 } else if (atomic_read(&this_adm.matrix_map_stat) > 0) {
2850 pr_err("%s: DSP returned error[%s]\n", __func__,
2851 adsp_err_get_err_str(atomic_read(
2852 &this_adm.matrix_map_stat)));
2853 ret = adsp_err_get_lnx_err_code(
2854 atomic_read(&this_adm.matrix_map_stat));
2855 goto fail_cmd;
2856 }
2857
2858 if ((perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) &&
2859 (path != ADM_PATH_COMPRESSED_RX)) {
2860 for (i = 0; i < payload_map.num_copps; i++) {
2861 port_idx = afe_get_port_index(payload_map.port_id[i]);
2862 copp_idx = payload_map.copp_idx[i];
2863 if (port_idx < 0 || copp_idx < 0 ||
2864 (copp_idx > MAX_COPPS_PER_PORT - 1)) {
2865 pr_err("%s: Invalid idx port_idx %d copp_idx %d\n",
2866 __func__, port_idx, copp_idx);
2867 continue;
2868 }
2869 rtac_add_adm_device(payload_map.port_id[i],
2870 atomic_read(&this_adm.copp.id
2871 [port_idx][copp_idx]),
2872 get_cal_path(path),
2873 payload_map.session_id,
2874 payload_map.app_type[i],
2875 payload_map.acdb_dev_id[i]);
2876
2877 if (!test_bit(ADM_STATUS_CALIBRATION_REQUIRED,
2878 (void *)&this_adm.copp.adm_status[port_idx]
2879 [copp_idx])) {
2880 pr_debug("%s: adm copp[0x%x][%d] already sent",
2881 __func__, port_idx, copp_idx);
2882 continue;
2883 }
2884 send_adm_cal(payload_map.port_id[i], copp_idx,
2885 get_cal_path(path), perf_mode,
2886 payload_map.app_type[i],
2887 payload_map.acdb_dev_id[i],
Aditya Bavanari5106b562018-01-08 13:16:32 +05302888 payload_map.sample_rate[i],
2889 passthr_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302890 /* ADM COPP calibration is already sent */
2891 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
2892 (void *)&this_adm.copp.
2893 adm_status[port_idx][copp_idx]);
2894 pr_debug("%s: copp_id: %d\n", __func__,
2895 atomic_read(&this_adm.copp.id[port_idx]
2896 [copp_idx]));
2897 }
2898 }
2899
2900fail_cmd:
2901 kfree(matrix_map);
2902 return ret;
2903}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302904EXPORT_SYMBOL(adm_matrix_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302905
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302906/**
2907 * adm_ec_ref_rx_id -
2908 * Update EC ref port ID
2909 *
2910 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302911void adm_ec_ref_rx_id(int port_id)
2912{
2913 this_adm.ec_ref_rx = port_id;
2914 pr_debug("%s: ec_ref_rx:%d\n", __func__, this_adm.ec_ref_rx);
2915}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302916EXPORT_SYMBOL(adm_ec_ref_rx_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302917
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302918/**
2919 * adm_num_ec_ref_rx_chans -
2920 * Update EC ref number of channels
2921 *
2922 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302923void adm_num_ec_ref_rx_chans(int num_chans)
2924{
2925 this_adm.num_ec_ref_rx_chans = num_chans;
2926 pr_debug("%s: num_ec_ref_rx_chans:%d\n",
2927 __func__, this_adm.num_ec_ref_rx_chans);
2928}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302929EXPORT_SYMBOL(adm_num_ec_ref_rx_chans);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302930
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302931/**
2932 * adm_ec_ref_rx_bit_width -
2933 * Update EC ref bit_width
2934 *
2935 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302936void adm_ec_ref_rx_bit_width(int bit_width)
2937{
2938 this_adm.ec_ref_rx_bit_width = bit_width;
2939 pr_debug("%s: ec_ref_rx_bit_width:%d\n",
2940 __func__, this_adm.ec_ref_rx_bit_width);
2941}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302942EXPORT_SYMBOL(adm_ec_ref_rx_bit_width);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302943
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302944/**
2945 * adm_ec_ref_rx_sampling_rate -
2946 * Update EC ref sample rate
2947 *
2948 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302949void adm_ec_ref_rx_sampling_rate(int sampling_rate)
2950{
2951 this_adm.ec_ref_rx_sampling_rate = sampling_rate;
2952 pr_debug("%s: ec_ref_rx_sampling_rate:%d\n",
2953 __func__, this_adm.ec_ref_rx_sampling_rate);
2954}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302955EXPORT_SYMBOL(adm_ec_ref_rx_sampling_rate);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302956
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302957/**
2958 * adm_close -
2959 * command to close ADM copp
2960 *
2961 * @port_id: Port ID number
2962 * @perf_mode: performance mode like LL/ULL/..
2963 * @copp_idx: copp index assigned
2964 *
2965 * Returns 0 on success or error on failure
2966 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302967int adm_close(int port_id, int perf_mode, int copp_idx)
2968{
2969 struct apr_hdr close;
2970
2971 int ret = 0, port_idx;
2972 int copp_id = RESET_COPP_ID;
2973
2974 pr_debug("%s: port_id=0x%x perf_mode: %d copp_idx: %d\n", __func__,
2975 port_id, perf_mode, copp_idx);
2976
2977 port_id = q6audio_convert_virtual_to_portid(port_id);
2978 port_idx = adm_validate_and_get_port_index(port_id);
2979 if (port_idx < 0) {
2980 pr_err("%s: Invalid port_id 0x%x\n",
2981 __func__, port_id);
2982 return -EINVAL;
2983 }
2984
2985 if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
2986 pr_err("%s: Invalid copp idx: %d\n", __func__, copp_idx);
2987 return -EINVAL;
2988 }
2989
2990 if (this_adm.copp.adm_delay[port_idx][copp_idx] && perf_mode
2991 == LEGACY_PCM_MODE) {
2992 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
2993 1);
2994 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
2995 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
2996 }
2997
2998 atomic_dec(&this_adm.copp.cnt[port_idx][copp_idx]);
2999 if (!(atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]))) {
3000 copp_id = adm_get_copp_id(port_idx, copp_idx);
3001 pr_debug("%s: Closing ADM port_idx:%d copp_idx:%d copp_id:0x%x\n",
3002 __func__, port_idx, copp_idx, copp_id);
3003 if ((!perf_mode) && (this_adm.outband_memmap.paddr != 0) &&
3004 (atomic_read(&this_adm.copp.topology[port_idx][copp_idx]) ==
3005 SRS_TRUMEDIA_TOPOLOGY_ID)) {
3006 atomic_set(&this_adm.mem_map_index,
3007 ADM_SRS_TRUMEDIA);
3008 ret = adm_memory_unmap_regions();
3009 if (ret < 0) {
3010 pr_err("%s: adm mem unmmap err %d",
3011 __func__, ret);
3012 } else {
3013 atomic_set(&this_adm.mem_map_handles
3014 [ADM_SRS_TRUMEDIA], 0);
3015 }
3016 }
3017
3018
3019 if ((afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX) &&
3020 this_adm.sourceTrackingData.memmap.paddr) {
3021 atomic_set(&this_adm.mem_map_index,
3022 ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
3023 ret = adm_memory_unmap_regions();
3024 if (ret < 0) {
3025 pr_err("%s: adm mem unmmap err %d",
3026 __func__, ret);
3027 }
3028 msm_audio_ion_free(
Banajit Goswami08bb7362017-11-03 22:48:23 -07003029 this_adm.sourceTrackingData.dma_buf);
3030 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303031 this_adm.sourceTrackingData.memmap.size = 0;
3032 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
3033 this_adm.sourceTrackingData.memmap.paddr = 0;
3034 this_adm.sourceTrackingData.apr_cmd_status = -1;
3035 atomic_set(&this_adm.mem_map_handles[
3036 ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
3037 }
3038
3039 close.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3040 APR_HDR_LEN(APR_HDR_SIZE),
3041 APR_PKT_VER);
3042 close.pkt_size = sizeof(close);
3043 close.src_svc = APR_SVC_ADM;
3044 close.src_domain = APR_DOMAIN_APPS;
3045 close.src_port = port_id;
3046 close.dest_svc = APR_SVC_ADM;
3047 close.dest_domain = APR_DOMAIN_ADSP;
3048 close.dest_port = copp_id;
3049 close.token = port_idx << 16 | copp_idx;
3050 close.opcode = ADM_CMD_DEVICE_CLOSE_V5;
3051
3052 atomic_set(&this_adm.copp.id[port_idx][copp_idx],
3053 RESET_COPP_ID);
3054 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
3055 atomic_set(&this_adm.copp.topology[port_idx][copp_idx], 0);
3056 atomic_set(&this_adm.copp.mode[port_idx][copp_idx], 0);
3057 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3058 atomic_set(&this_adm.copp.rate[port_idx][copp_idx], 0);
3059 atomic_set(&this_adm.copp.channels[port_idx][copp_idx], 0);
3060 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx], 0);
3061 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx], 0);
3062
3063 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3064 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
3065
3066 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&close);
3067 if (ret < 0) {
3068 pr_err("%s: ADM close failed %d\n", __func__, ret);
3069 return -EINVAL;
3070 }
3071
3072 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3073 atomic_read(&this_adm.copp.stat
3074 [port_idx][copp_idx]) >= 0,
3075 msecs_to_jiffies(TIMEOUT_MS));
3076 if (!ret) {
3077 pr_err("%s: ADM cmd Route timedout for port 0x%x\n",
3078 __func__, port_id);
3079 return -EINVAL;
3080 } else if (atomic_read(&this_adm.copp.stat
3081 [port_idx][copp_idx]) > 0) {
3082 pr_err("%s: DSP returned error[%s]\n",
3083 __func__, adsp_err_get_err_str(
3084 atomic_read(&this_adm.copp.stat
3085 [port_idx][copp_idx])));
3086 return adsp_err_get_lnx_err_code(
3087 atomic_read(&this_adm.copp.stat
3088 [port_idx][copp_idx]));
3089 }
3090 }
3091
3092 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) {
3093 pr_debug("%s: remove adm device from rtac\n", __func__);
3094 rtac_remove_adm_device(port_id, copp_id);
3095 }
3096 return 0;
3097}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303098EXPORT_SYMBOL(adm_close);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303099
3100int send_rtac_audvol_cal(void)
3101{
3102 int ret = 0;
3103 int ret2 = 0;
3104 int i = 0;
3105 int copp_idx, port_idx, acdb_id, app_id, path;
3106 struct cal_block_data *cal_block = NULL;
3107 struct audio_cal_info_audvol *audvol_cal_info = NULL;
3108 struct rtac_adm rtac_adm_data;
3109
3110 mutex_lock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3111
3112 cal_block = cal_utils_get_only_cal_block(
3113 this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]);
3114 if (cal_block == NULL) {
3115 pr_err("%s: can't find cal block!\n", __func__);
3116 goto unlock;
3117 }
3118
3119 audvol_cal_info = cal_block->cal_info;
3120 if (audvol_cal_info == NULL) {
3121 pr_err("%s: audvol_cal_info is NULL!\n", __func__);
3122 goto unlock;
3123 }
3124
3125 get_rtac_adm_data(&rtac_adm_data);
3126 for (; i < rtac_adm_data.num_of_dev; i++) {
3127
3128 acdb_id = rtac_adm_data.device[i].acdb_dev_id;
3129 if (acdb_id == 0)
3130 acdb_id = audvol_cal_info->acdb_id;
3131
3132 app_id = rtac_adm_data.device[i].app_type;
3133 if (app_id == 0)
3134 app_id = audvol_cal_info->app_type;
3135
3136 path = afe_get_port_type(rtac_adm_data.device[i].afe_port);
3137 if ((acdb_id == audvol_cal_info->acdb_id) &&
3138 (app_id == audvol_cal_info->app_type) &&
3139 (path == audvol_cal_info->path)) {
3140
3141 if (adm_get_indexes_from_copp_id(rtac_adm_data.
3142 device[i].copp, &copp_idx, &port_idx) != 0) {
3143 pr_debug("%s: Copp Id %d is not active\n",
3144 __func__,
3145 rtac_adm_data.device[i].copp);
3146 continue;
3147 }
3148
3149 ret2 = adm_remap_and_send_cal_block(ADM_RTAC_AUDVOL_CAL,
3150 rtac_adm_data.device[i].afe_port,
3151 copp_idx, cal_block,
3152 atomic_read(&this_adm.copp.
3153 mode[port_idx][copp_idx]),
3154 audvol_cal_info->app_type,
3155 audvol_cal_info->acdb_id,
3156 atomic_read(&this_adm.copp.
3157 rate[port_idx][copp_idx]));
3158 if (ret2 < 0) {
3159 pr_debug("%s: remap and send failed for copp Id %d, acdb id %d, app type %d, path %d\n",
3160 __func__, rtac_adm_data.device[i].copp,
3161 audvol_cal_info->acdb_id,
3162 audvol_cal_info->app_type,
3163 audvol_cal_info->path);
3164 ret = ret2;
3165 }
3166 }
3167 }
3168unlock:
3169 mutex_unlock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3170 return ret;
3171}
3172
3173int adm_map_rtac_block(struct rtac_cal_block_data *cal_block)
3174{
3175 int result = 0;
3176
3177 pr_debug("%s:\n", __func__);
3178
3179 if (cal_block == NULL) {
3180 pr_err("%s: cal_block is NULL!\n",
3181 __func__);
3182 result = -EINVAL;
3183 goto done;
3184 }
3185
3186 if (cal_block->cal_data.paddr == 0) {
3187 pr_debug("%s: No address to map!\n",
3188 __func__);
3189 result = -EINVAL;
3190 goto done;
3191 }
3192
3193 if (cal_block->map_data.map_size == 0) {
3194 pr_debug("%s: map size is 0!\n",
3195 __func__);
3196 result = -EINVAL;
3197 goto done;
3198 }
3199
3200 /* valid port ID needed for callback use primary I2S */
3201 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3202 result = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3203 &cal_block->map_data.map_size, 1);
3204 if (result < 0) {
3205 pr_err("%s: RTAC mmap did not work! size = %d result %d\n",
3206 __func__,
3207 cal_block->map_data.map_size, result);
3208 pr_debug("%s: RTAC mmap did not work! addr = 0x%pK, size = %d\n",
3209 __func__,
3210 &cal_block->cal_data.paddr,
3211 cal_block->map_data.map_size);
3212 goto done;
3213 }
3214
3215 cal_block->map_data.map_handle = atomic_read(
3216 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]);
3217done:
3218 return result;
3219}
3220
3221int adm_unmap_rtac_block(uint32_t *mem_map_handle)
3222{
3223 int result = 0;
3224
3225 pr_debug("%s:\n", __func__);
3226
3227 if (mem_map_handle == NULL) {
3228 pr_debug("%s: Map handle is NULL, nothing to unmap\n",
3229 __func__);
3230 goto done;
3231 }
3232
3233 if (*mem_map_handle == 0) {
3234 pr_debug("%s: Map handle is 0, nothing to unmap\n",
3235 __func__);
3236 goto done;
3237 }
3238
3239 if (*mem_map_handle != atomic_read(
3240 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL])) {
3241 pr_err("%s: Map handles do not match! Unmapping RTAC, RTAC map 0x%x, ADM map 0x%x\n",
3242 __func__, *mem_map_handle, atomic_read(
3243 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]));
3244
3245 /* if mismatch use handle passed in to unmap */
3246 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL],
3247 *mem_map_handle);
3248 }
3249
3250 /* valid port ID needed for callback use primary I2S */
3251 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3252 result = adm_memory_unmap_regions();
3253 if (result < 0) {
3254 pr_debug("%s: adm_memory_unmap_regions failed, error %d\n",
3255 __func__, result);
3256 } else {
3257 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL], 0);
3258 *mem_map_handle = 0;
3259 }
3260done:
3261 return result;
3262}
3263
3264static int get_cal_type_index(int32_t cal_type)
3265{
3266 int ret = -EINVAL;
3267
3268 switch (cal_type) {
3269 case ADM_AUDPROC_CAL_TYPE:
3270 ret = ADM_AUDPROC_CAL;
3271 break;
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05303272 case ADM_LSM_AUDPROC_CAL_TYPE:
3273 ret = ADM_LSM_AUDPROC_CAL;
3274 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303275 case ADM_AUDVOL_CAL_TYPE:
3276 ret = ADM_AUDVOL_CAL;
3277 break;
3278 case ADM_CUST_TOPOLOGY_CAL_TYPE:
3279 ret = ADM_CUSTOM_TOP_CAL;
3280 break;
3281 case ADM_RTAC_INFO_CAL_TYPE:
3282 ret = ADM_RTAC_INFO_CAL;
3283 break;
3284 case ADM_RTAC_APR_CAL_TYPE:
3285 ret = ADM_RTAC_APR_CAL;
3286 break;
3287 case ADM_RTAC_AUDVOL_CAL_TYPE:
3288 ret = ADM_RTAC_AUDVOL_CAL;
3289 break;
3290 default:
3291 pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
3292 }
3293 return ret;
3294}
3295
3296static int adm_alloc_cal(int32_t cal_type, size_t data_size, void *data)
3297{
3298 int ret = 0;
3299 int cal_index;
3300
3301 pr_debug("%s:\n", __func__);
3302
3303 cal_index = get_cal_type_index(cal_type);
3304 if (cal_index < 0) {
3305 pr_err("%s: could not get cal index %d!\n",
3306 __func__, cal_index);
3307 ret = -EINVAL;
3308 goto done;
3309 }
3310
3311 ret = cal_utils_alloc_cal(data_size, data,
3312 this_adm.cal_data[cal_index], 0, NULL);
3313 if (ret < 0) {
3314 pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
3315 __func__, ret, cal_type);
3316 ret = -EINVAL;
3317 goto done;
3318 }
3319done:
3320 return ret;
3321}
3322
3323static int adm_dealloc_cal(int32_t cal_type, size_t data_size, void *data)
3324{
3325 int ret = 0;
3326 int cal_index;
3327
3328 pr_debug("%s:\n", __func__);
3329
3330 cal_index = get_cal_type_index(cal_type);
3331 if (cal_index < 0) {
3332 pr_err("%s: could not get cal index %d!\n",
3333 __func__, cal_index);
3334 ret = -EINVAL;
3335 goto done;
3336 }
3337
3338 ret = cal_utils_dealloc_cal(data_size, data,
3339 this_adm.cal_data[cal_index]);
3340 if (ret < 0) {
3341 pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
3342 __func__, ret, cal_type);
3343 ret = -EINVAL;
3344 goto done;
3345 }
3346done:
3347 return ret;
3348}
3349
3350static int adm_set_cal(int32_t cal_type, size_t data_size, void *data)
3351{
3352 int ret = 0;
3353 int cal_index;
3354
3355 pr_debug("%s:\n", __func__);
3356
3357 cal_index = get_cal_type_index(cal_type);
3358 if (cal_index < 0) {
3359 pr_err("%s: could not get cal index %d!\n",
3360 __func__, cal_index);
3361 ret = -EINVAL;
3362 goto done;
3363 }
3364
3365 ret = cal_utils_set_cal(data_size, data,
3366 this_adm.cal_data[cal_index], 0, NULL);
3367 if (ret < 0) {
3368 pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
3369 __func__, ret, cal_type);
3370 ret = -EINVAL;
3371 goto done;
3372 }
3373
3374 if (cal_index == ADM_CUSTOM_TOP_CAL) {
3375 mutex_lock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3376 this_adm.set_custom_topology = 1;
3377 mutex_unlock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3378 } else if (cal_index == ADM_RTAC_AUDVOL_CAL) {
3379 send_rtac_audvol_cal();
3380 }
3381done:
3382 return ret;
3383}
3384
3385static int adm_map_cal_data(int32_t cal_type,
3386 struct cal_block_data *cal_block)
3387{
3388 int ret = 0;
3389 int cal_index;
3390
3391 pr_debug("%s:\n", __func__);
3392
3393 cal_index = get_cal_type_index(cal_type);
3394 if (cal_index < 0) {
3395 pr_err("%s: could not get cal index %d!\n",
3396 __func__, cal_index);
3397 ret = -EINVAL;
3398 goto done;
3399 }
3400
3401 atomic_set(&this_adm.mem_map_index, cal_index);
3402 ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3403 (uint32_t *)&cal_block->map_data.map_size, 1);
3404 if (ret < 0) {
3405 pr_err("%s: map did not work! cal_type %i ret %d\n",
3406 __func__, cal_index, ret);
3407 ret = -ENODEV;
3408 goto done;
3409 }
3410 cal_block->map_data.q6map_handle = atomic_read(&this_adm.
3411 mem_map_handles[cal_index]);
3412done:
3413 return ret;
3414}
3415
3416static int adm_unmap_cal_data(int32_t cal_type,
3417 struct cal_block_data *cal_block)
3418{
3419 int ret = 0;
3420 int cal_index;
3421
3422 pr_debug("%s:\n", __func__);
3423
3424 cal_index = get_cal_type_index(cal_type);
3425 if (cal_index < 0) {
3426 pr_err("%s: could not get cal index %d!\n",
3427 __func__, cal_index);
3428 ret = -EINVAL;
3429 goto done;
3430 }
3431
3432 if (cal_block == NULL) {
3433 pr_err("%s: Cal block is NULL!\n",
3434 __func__);
3435 goto done;
3436 }
3437
3438 if (cal_block->map_data.q6map_handle == 0) {
3439 pr_err("%s: Map handle is NULL, nothing to unmap\n",
3440 __func__);
3441 goto done;
3442 }
3443
3444 atomic_set(&this_adm.mem_map_handles[cal_index],
3445 cal_block->map_data.q6map_handle);
3446 atomic_set(&this_adm.mem_map_index, cal_index);
3447 ret = adm_memory_unmap_regions();
3448 if (ret < 0) {
3449 pr_err("%s: unmap did not work! cal_type %i ret %d\n",
3450 __func__, cal_index, ret);
3451 ret = -ENODEV;
3452 goto done;
3453 }
3454 cal_block->map_data.q6map_handle = 0;
3455done:
3456 return ret;
3457}
3458
3459static void adm_delete_cal_data(void)
3460{
3461 pr_debug("%s:\n", __func__);
3462
3463 cal_utils_destroy_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data);
3464}
3465
3466static int adm_init_cal_data(void)
3467{
3468 int ret = 0;
3469 struct cal_type_info cal_type_info[] = {
3470 {{ADM_CUST_TOPOLOGY_CAL_TYPE,
3471 {adm_alloc_cal, adm_dealloc_cal, NULL,
3472 adm_set_cal, NULL, NULL} },
3473 {adm_map_cal_data, adm_unmap_cal_data,
3474 cal_utils_match_buf_num} },
3475
3476 {{ADM_AUDPROC_CAL_TYPE,
3477 {adm_alloc_cal, adm_dealloc_cal, NULL,
3478 adm_set_cal, NULL, NULL} },
3479 {adm_map_cal_data, adm_unmap_cal_data,
3480 cal_utils_match_buf_num} },
3481
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05303482 {{ADM_LSM_AUDPROC_CAL_TYPE,
3483 {adm_alloc_cal, adm_dealloc_cal, NULL,
3484 adm_set_cal, NULL, NULL} },
3485 {adm_map_cal_data, adm_unmap_cal_data,
3486 cal_utils_match_buf_num} },
3487
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303488 {{ADM_AUDVOL_CAL_TYPE,
3489 {adm_alloc_cal, adm_dealloc_cal, NULL,
3490 adm_set_cal, NULL, NULL} },
3491 {adm_map_cal_data, adm_unmap_cal_data,
3492 cal_utils_match_buf_num} },
3493
3494 {{ADM_RTAC_INFO_CAL_TYPE,
3495 {NULL, NULL, NULL, NULL, NULL, NULL} },
3496 {NULL, NULL, cal_utils_match_buf_num} },
3497
3498 {{ADM_RTAC_APR_CAL_TYPE,
3499 {NULL, NULL, NULL, NULL, NULL, NULL} },
3500 {NULL, NULL, cal_utils_match_buf_num} },
3501
3502 {{SRS_TRUMEDIA_CAL_TYPE,
3503 {NULL, NULL, NULL, NULL, NULL, NULL} },
3504 {NULL, NULL, cal_utils_match_buf_num} },
3505
3506 {{ADM_RTAC_AUDVOL_CAL_TYPE,
3507 {adm_alloc_cal, adm_dealloc_cal, NULL,
3508 adm_set_cal, NULL, NULL} },
3509 {adm_map_cal_data, adm_unmap_cal_data,
3510 cal_utils_match_buf_num} },
3511 };
3512 pr_debug("%s:\n", __func__);
3513
3514 ret = cal_utils_create_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data,
3515 cal_type_info);
3516 if (ret < 0) {
3517 pr_err("%s: could not create cal type! ret %d\n",
3518 __func__, ret);
3519 ret = -EINVAL;
3520 goto err;
3521 }
3522
3523 return ret;
3524err:
3525 adm_delete_cal_data();
3526 return ret;
3527}
3528
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303529/**
3530 * adm_set_volume -
3531 * command to set volume on ADM copp
3532 *
3533 * @port_id: Port ID number
3534 * @copp_idx: copp index assigned
3535 * @volume: gain value to set
3536 *
3537 * Returns 0 on success or error on failure
3538 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303539int adm_set_volume(int port_id, int copp_idx, int volume)
3540{
3541 struct audproc_volume_ctrl_master_gain audproc_vol;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003542 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303543 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303544
3545 pr_debug("%s: port_id %d, volume %d\n", __func__, port_id, volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303546
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003547 memset(&audproc_vol, 0, sizeof(audproc_vol));
3548 memset(&param_hdr, 0, sizeof(param_hdr));
3549 param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
3550 param_hdr.instance_id = INSTANCE_ID_0;
3551 param_hdr.param_id = AUDPROC_PARAM_ID_VOL_CTRL_MASTER_GAIN;
3552 param_hdr.param_size = sizeof(audproc_vol);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303553
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303554 audproc_vol.master_gain = volume;
3555
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003556 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
3557 (uint8_t *) &audproc_vol);
3558 if (rc)
3559 pr_err("%s: Failed to set volume, err %d\n", __func__, rc);
3560
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303561 return rc;
3562}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303563EXPORT_SYMBOL(adm_set_volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303564
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303565/**
3566 * adm_set_softvolume -
3567 * command to set softvolume
3568 *
3569 * @port_id: Port ID number
3570 * @copp_idx: copp index assigned
3571 * @softvol_param: Params to set for softvolume
3572 *
3573 * Returns 0 on success or error on failure
3574 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303575int adm_set_softvolume(int port_id, int copp_idx,
3576 struct audproc_softvolume_params *softvol_param)
3577{
3578 struct audproc_soft_step_volume_params audproc_softvol;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003579 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303580 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303581
3582 pr_debug("%s: period %d step %d curve %d\n", __func__,
3583 softvol_param->period, softvol_param->step,
3584 softvol_param->rampingcurve);
3585
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003586 memset(&audproc_softvol, 0, sizeof(audproc_softvol));
3587 memset(&param_hdr, 0, sizeof(param_hdr));
3588 param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
3589 param_hdr.instance_id = INSTANCE_ID_0;
3590 param_hdr.param_id = AUDPROC_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
3591 param_hdr.param_size = sizeof(audproc_softvol);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303592
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303593 audproc_softvol.period = softvol_param->period;
3594 audproc_softvol.step = softvol_param->step;
3595 audproc_softvol.ramping_curve = softvol_param->rampingcurve;
3596
3597 pr_debug("%s: period %d, step %d, curve %d\n", __func__,
3598 audproc_softvol.period, audproc_softvol.step,
3599 audproc_softvol.ramping_curve);
3600
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003601 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
3602 (uint8_t *) &audproc_softvol);
3603 if (rc)
3604 pr_err("%s: Failed to set soft volume, err %d\n", __func__, rc);
3605
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303606 return rc;
3607}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303608EXPORT_SYMBOL(adm_set_softvolume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303609
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303610/**
3611 * adm_set_mic_gain -
3612 * command to set MIC gain
3613 *
3614 * @port_id: Port ID number
3615 * @copp_idx: copp index assigned
3616 * @volume: gain value to set
3617 *
3618 * Returns 0 on success or error on failure
3619 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303620int adm_set_mic_gain(int port_id, int copp_idx, int volume)
3621{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003622 struct admx_mic_gain mic_gain_params;
3623 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303624 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303625
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003626 pr_debug("%s: Setting mic gain to %d at port_id 0x%x\n", __func__,
3627 volume, port_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303628
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003629 memset(&mic_gain_params, 0, sizeof(mic_gain_params));
3630 memset(&param_hdr, 0, sizeof(param_hdr));
3631 param_hdr.module_id = ADM_MODULE_IDX_MIC_GAIN_CTRL;
3632 param_hdr.instance_id = INSTANCE_ID_0;
3633 param_hdr.param_id = ADM_PARAM_IDX_MIC_GAIN;
3634 param_hdr.param_size = sizeof(mic_gain_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303635
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003636 mic_gain_params.tx_mic_gain = volume;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303637
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003638 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
3639 (uint8_t *) &mic_gain_params);
3640 if (rc)
3641 pr_err("%s: Failed to set mic gain, err %d\n", __func__, rc);
3642
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303643 return rc;
3644}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303645EXPORT_SYMBOL(adm_set_mic_gain);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303646
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303647/**
3648 * adm_send_set_multichannel_ec_primary_mic_ch -
3649 * command to set multi-ch EC primary mic
3650 *
3651 * @port_id: Port ID number
3652 * @copp_idx: copp index assigned
3653 * @primary_mic_ch: channel number of primary mic
3654 *
3655 * Returns 0 on success or error on failure
3656 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303657int adm_send_set_multichannel_ec_primary_mic_ch(int port_id, int copp_idx,
3658 int primary_mic_ch)
3659{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003660 struct admx_sec_primary_mic_ch sec_primary_ch_params;
3661 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303662 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303663
3664 pr_debug("%s port_id 0x%x, copp_idx 0x%x, primary_mic_ch %d\n",
3665 __func__, port_id, copp_idx, primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303666
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003667 memset(&sec_primary_ch_params, 0, sizeof(sec_primary_ch_params));
3668 memset(&param_hdr, 0, sizeof(param_hdr));
3669 param_hdr.module_id = AUDPROC_MODULE_ID_VOICE_TX_SECNS;
3670 param_hdr.instance_id = INSTANCE_ID_0;
3671 param_hdr.param_id = AUDPROC_PARAM_IDX_SEC_PRIMARY_MIC_CH;
3672 param_hdr.param_size = sizeof(sec_primary_ch_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303673
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003674 sec_primary_ch_params.version = 0;
3675 sec_primary_ch_params.sec_primary_mic_ch = primary_mic_ch;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303676
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003677 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
3678 (uint8_t *) &sec_primary_ch_params);
3679 if (rc)
3680 pr_err("%s: Failed to set primary mic chanel, err %d\n",
3681 __func__, rc);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303682
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303683 return rc;
3684}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303685EXPORT_SYMBOL(adm_send_set_multichannel_ec_primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303686
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303687/**
3688 * adm_param_enable -
3689 * command to send params to ADM for given module
3690 *
3691 * @port_id: Port ID number
3692 * @copp_idx: copp index assigned
3693 * @module_id: ADM module
3694 * @enable: flag to enable or disable module
3695 *
3696 * Returns 0 on success or error on failure
3697 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303698int adm_param_enable(int port_id, int copp_idx, int module_id, int enable)
3699{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003700 struct module_instance_info mod_inst_info;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303701
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003702 memset(&mod_inst_info, 0, sizeof(mod_inst_info));
3703 mod_inst_info.module_id = module_id;
3704 mod_inst_info.instance_id = INSTANCE_ID_0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303705
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003706 return adm_param_enable_v2(port_id, copp_idx, mod_inst_info, enable);
3707}
3708
3709/**
3710 * adm_param_enable_v2 -
3711 * command to send params to ADM for given module
3712 *
3713 * @port_id: Port ID number
3714 * @copp_idx: copp index assigned
3715 * @mod_inst_info: module and instance ID info
3716 * @enable: flag to enable or disable module
3717 *
3718 * Returns 0 on success or error on failure
3719 */
3720int adm_param_enable_v2(int port_id, int copp_idx,
3721 struct module_instance_info mod_inst_info, int enable)
3722{
3723 uint32_t enable_param;
3724 struct param_hdr_v3 param_hdr;
3725 int rc = 0;
3726
3727 if (enable < 0 || enable > 1) {
3728 pr_err("%s: Invalid value for enable %d\n", __func__, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303729 return -EINVAL;
3730 }
3731
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003732 pr_debug("%s port_id %d, module_id 0x%x, instance_id 0x%x, enable %d\n",
3733 __func__, port_id, mod_inst_info.module_id,
3734 mod_inst_info.instance_id, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303735
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003736 memset(&param_hdr, 0, sizeof(param_hdr));
3737 param_hdr.module_id = mod_inst_info.module_id;
3738 param_hdr.instance_id = mod_inst_info.instance_id;
3739 param_hdr.param_id = AUDPROC_PARAM_ID_ENABLE;
3740 param_hdr.param_size = sizeof(enable_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303741
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003742 enable_param = enable;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303743
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003744 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
3745 (uint8_t *) &enable_param);
3746 if (rc)
3747 pr_err("%s: Failed to set enable of module(%d) instance(%d) to %d, err %d\n",
3748 __func__, mod_inst_info.module_id,
3749 mod_inst_info.instance_id, enable, rc);
3750
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303751 return rc;
3752
3753}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303754EXPORT_SYMBOL(adm_param_enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303755
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303756/**
3757 * adm_send_calibration -
3758 * send ADM calibration to DSP
3759 *
3760 * @port_id: Port ID number
3761 * @copp_idx: copp index assigned
3762 * @path: direction or ADM path type
3763 * @perf_mode: performance mode like LL/ULL/..
3764 * @cal_type: calibration type to use
3765 * @params: pointer with cal data
3766 * @size: cal size
3767 *
3768 * Returns 0 on success or error on failure
3769 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303770int adm_send_calibration(int port_id, int copp_idx, int path, int perf_mode,
3771 int cal_type, char *params, int size)
3772{
3773
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003774 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303775
3776 pr_debug("%s:port_id %d, path %d, perf_mode %d, cal_type %d, size %d\n",
3777 __func__, port_id, path, perf_mode, cal_type, size);
3778
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303779 /* Maps audio_dev_ctrl path definition to ACDB definition */
3780 if (get_cal_path(path) != RX_DEVICE) {
3781 pr_err("%s: acdb_path %d\n", __func__, path);
3782 rc = -EINVAL;
3783 goto end;
3784 }
3785
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003786 rc = adm_set_pp_params(port_id, copp_idx, NULL, (u8 *) params, size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303787
3788end:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303789 return rc;
3790}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303791EXPORT_SYMBOL(adm_send_calibration);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303792
3793/*
3794 * adm_update_wait_parameters must be called with routing driver locks.
3795 * adm_reset_wait_parameters must be called with routing driver locks.
3796 * set and reset parmeters are separated to make sure it is always called
3797 * under routing driver lock.
3798 * adm_wait_timeout is to block until timeout or interrupted. Timeout is
3799 * not a an error.
3800 */
3801int adm_set_wait_parameters(int port_id, int copp_idx)
3802{
3803
3804 int ret = 0, port_idx;
3805
3806 pr_debug("%s: port_id 0x%x, copp_idx %d\n", __func__, port_id,
3807 copp_idx);
3808 port_id = afe_convert_virtual_to_portid(port_id);
3809 port_idx = adm_validate_and_get_port_index(port_id);
3810 if (port_idx < 0) {
3811 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
3812 ret = -EINVAL;
3813 goto end;
3814 }
3815
3816 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3817 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3818 return -EINVAL;
3819 }
3820
3821 this_adm.copp.adm_delay[port_idx][copp_idx] = 1;
3822 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 0);
3823
3824end:
3825 return ret;
3826
3827}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303828EXPORT_SYMBOL(adm_set_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303829
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303830/**
3831 * adm_reset_wait_parameters -
3832 * reset wait parameters or ADM delay value
3833 *
3834 * @port_id: Port ID number
3835 * @copp_idx: copp index assigned
3836 *
3837 * Returns 0 on success or error on failure
3838 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303839int adm_reset_wait_parameters(int port_id, int copp_idx)
3840{
3841 int ret = 0, port_idx;
3842
3843 pr_debug("%s: port_id 0x%x copp_idx %d\n", __func__, port_id,
3844 copp_idx);
3845 port_id = afe_convert_virtual_to_portid(port_id);
3846 port_idx = adm_validate_and_get_port_index(port_id);
3847 if (port_idx < 0) {
3848 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
3849 ret = -EINVAL;
3850 goto end;
3851 }
3852
3853 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3854 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3855 return -EINVAL;
3856 }
3857
3858 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 1);
3859 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
3860
3861end:
3862 return ret;
3863}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303864EXPORT_SYMBOL(adm_reset_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303865
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303866/**
3867 * adm_wait_timeout -
3868 * ADM wait command after command send to DSP
3869 *
3870 * @port_id: Port ID number
3871 * @copp_idx: copp index assigned
3872 * @wait_time: value in ms for command timeout
3873 *
3874 * Returns 0 on success or error on failure
3875 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303876int adm_wait_timeout(int port_id, int copp_idx, int wait_time)
3877{
3878 int ret = 0, port_idx;
3879
3880 pr_debug("%s: port_id 0x%x, copp_idx %d, wait_time %d\n", __func__,
3881 port_id, copp_idx, wait_time);
3882 port_id = afe_convert_virtual_to_portid(port_id);
3883 port_idx = adm_validate_and_get_port_index(port_id);
3884 if (port_idx < 0) {
3885 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
3886 ret = -EINVAL;
3887 goto end;
3888 }
3889
3890 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3891 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3892 return -EINVAL;
3893 }
3894
3895 ret = wait_event_timeout(
3896 this_adm.copp.adm_delay_wait[port_idx][copp_idx],
3897 atomic_read(&this_adm.copp.adm_delay_stat[port_idx][copp_idx]),
3898 msecs_to_jiffies(wait_time));
3899 pr_debug("%s: return %d\n", __func__, ret);
3900 if (ret != 0)
3901 ret = -EINTR;
3902end:
3903 pr_debug("%s: return %d--\n", __func__, ret);
3904 return ret;
3905}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303906EXPORT_SYMBOL(adm_wait_timeout);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303907
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303908/**
3909 * adm_store_cal_data -
3910 * Retrieve calibration data for ADM copp device
3911 *
3912 * @port_id: Port ID number
3913 * @copp_idx: copp index assigned
3914 * @path: direction or copp type
3915 * @perf_mode: performance mode like LL/ULL/..
3916 * @cal_index: calibration index to use
3917 * @params: pointer to store cal data
3918 * @size: pointer to fill with cal size
3919 *
3920 * Returns 0 on success or error on failure
3921 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303922int adm_store_cal_data(int port_id, int copp_idx, int path, int perf_mode,
3923 int cal_index, char *params, int *size)
3924{
3925 int rc = 0;
3926 struct cal_block_data *cal_block = NULL;
3927 int app_type, acdb_id, port_idx, sample_rate;
3928
3929 if (this_adm.cal_data[cal_index] == NULL) {
3930 pr_debug("%s: cal_index %d not allocated!\n",
3931 __func__, cal_index);
3932 goto end;
3933 }
3934
3935 if (get_cal_path(path) != RX_DEVICE) {
3936 pr_debug("%s: Invalid path to store calibration %d\n",
3937 __func__, path);
3938 rc = -EINVAL;
3939 goto end;
3940 }
3941
3942 port_id = afe_convert_virtual_to_portid(port_id);
3943 port_idx = adm_validate_and_get_port_index(port_id);
3944 if (port_idx < 0) {
3945 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
3946 rc = -EINVAL;
3947 goto end;
3948 }
3949
3950 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3951 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3952 return -EINVAL;
3953 }
3954
3955 acdb_id = atomic_read(&this_adm.copp.acdb_id[port_idx][copp_idx]);
3956 app_type = atomic_read(&this_adm.copp.app_type[port_idx][copp_idx]);
3957 sample_rate = atomic_read(&this_adm.copp.rate[port_idx][copp_idx]);
3958
3959 mutex_lock(&this_adm.cal_data[cal_index]->lock);
3960 cal_block = adm_find_cal(cal_index, get_cal_path(path), app_type,
3961 acdb_id, sample_rate);
3962 if (cal_block == NULL)
3963 goto unlock;
3964
3965 if (cal_block->cal_data.size <= 0) {
3966 pr_debug("%s: No ADM cal send for port_id = 0x%x!\n",
3967 __func__, port_id);
3968 rc = -EINVAL;
3969 goto unlock;
3970 }
3971
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05303972 if (cal_index == ADM_AUDPROC_CAL || cal_index == ADM_LSM_AUDPROC_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303973 if (cal_block->cal_data.size > AUD_PROC_BLOCK_SIZE) {
3974 pr_err("%s:audproc:invalid size exp/actual[%zd, %d]\n",
3975 __func__, cal_block->cal_data.size, *size);
3976 rc = -ENOMEM;
3977 goto unlock;
3978 }
3979 } else if (cal_index == ADM_AUDVOL_CAL) {
3980 if (cal_block->cal_data.size > AUD_VOL_BLOCK_SIZE) {
3981 pr_err("%s:aud_vol:invalid size exp/actual[%zd, %d]\n",
3982 __func__, cal_block->cal_data.size, *size);
3983 rc = -ENOMEM;
3984 goto unlock;
3985 }
3986 } else {
3987 pr_debug("%s: Not valid calibration for dolby topolgy\n",
3988 __func__);
3989 rc = -EINVAL;
3990 goto unlock;
3991 }
3992 memcpy(params, cal_block->cal_data.kvaddr, cal_block->cal_data.size);
3993 *size = cal_block->cal_data.size;
3994
3995 pr_debug("%s:port_id %d, copp_idx %d, path %d",
3996 __func__, port_id, copp_idx, path);
3997 pr_debug("perf_mode %d, cal_type %d, size %d\n",
3998 perf_mode, cal_index, *size);
3999
4000unlock:
4001 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
4002end:
4003 return rc;
4004}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304005EXPORT_SYMBOL(adm_store_cal_data);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304006
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304007/**
4008 * adm_send_compressed_device_mute -
4009 * command to send mute for compressed device
4010 *
4011 * @port_id: Port ID number
4012 * @copp_idx: copp index assigned
4013 * @mute_on: flag to indicate mute or unmute
4014 *
4015 * Returns 0 on success or error on failure
4016 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304017int adm_send_compressed_device_mute(int port_id, int copp_idx, bool mute_on)
4018{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004019 u32 mute_param = mute_on ? 1 : 0;
4020 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304021 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304022
4023 pr_debug("%s port_id: 0x%x, copp_idx %d, mute_on: %d\n",
4024 __func__, port_id, copp_idx, mute_on);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304025
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004026 memset(&param_hdr, 0, sizeof(param_hdr));
4027 param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_MUTE;
4028 param_hdr.instance_id = INSTANCE_ID_0;
4029 param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_MUTE;
4030 param_hdr.param_size = sizeof(mute_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304031
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004032 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4033 (uint8_t *) &mute_param);
4034 if (ret)
4035 pr_err("%s: Failed to set mute, err %d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304036
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304037 return ret;
4038}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304039EXPORT_SYMBOL(adm_send_compressed_device_mute);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304040
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304041/**
4042 * adm_send_compressed_device_latency -
4043 * command to send latency for compressed device
4044 *
4045 * @port_id: Port ID number
4046 * @copp_idx: copp index assigned
4047 * @latency: latency value to pass
4048 *
4049 * Returns 0 on success or error on failure
4050 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304051int adm_send_compressed_device_latency(int port_id, int copp_idx, int latency)
4052{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004053 u32 latency_param;
4054 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304055 int ret = 0;
4056
4057 pr_debug("%s port_id: 0x%x, copp_idx %d latency: %d\n", __func__,
4058 port_id, copp_idx, latency);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004059
4060 if (latency < 0) {
4061 pr_err("%s: Invalid value for latency %d", __func__, latency);
4062 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304063 }
4064
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004065 memset(&param_hdr, 0, sizeof(param_hdr));
4066 param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_LATENCY;
4067 param_hdr.instance_id = INSTANCE_ID_0;
4068 param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_LATENCY;
4069 param_hdr.param_size = sizeof(latency_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304070
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004071 latency_param = latency;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304072
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004073 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4074 (uint8_t *) &latency_param);
4075 if (ret)
4076 pr_err("%s: Failed to set latency, err %d\n", __func__, ret);
4077
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304078 return ret;
4079}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304080EXPORT_SYMBOL(adm_send_compressed_device_latency);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304081
4082/**
4083 * adm_swap_speaker_channels
4084 *
4085 * Receives port_id, copp_idx, sample rate, spk_swap and
4086 * send MFC command to swap speaker channel.
4087 * Return zero on success. On failure returns nonzero.
4088 *
4089 * port_id - Passed value, port_id for which channels swap is wanted
4090 * copp_idx - Passed value, copp_idx for which channels swap is wanted
4091 * sample_rate - Passed value, sample rate used by app type config
4092 * spk_swap - Passed value, spk_swap for check if swap flag is set
4093 */
4094int adm_swap_speaker_channels(int port_id, int copp_idx,
4095 int sample_rate, bool spk_swap)
4096{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004097 struct audproc_mfc_param_media_fmt mfc_cfg;
4098 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304099 uint16_t num_channels;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004100 int port_idx = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304101 int ret = 0;
4102
4103 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4104 __func__, port_id, copp_idx);
4105 port_id = q6audio_convert_virtual_to_portid(port_id);
4106 port_idx = adm_validate_and_get_port_index(port_id);
4107 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
4108 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004109 return -EINVAL;
4110 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4111 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
4112 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304113 }
4114
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004115 num_channels = atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304116 if (num_channels != 2) {
4117 pr_debug("%s: Invalid number of channels: %d\n",
4118 __func__, num_channels);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004119 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304120 }
4121
4122 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004123 memset(&param_hdr, 0, sizeof(param_hdr));
4124
4125 param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
4126 param_hdr.instance_id = INSTANCE_ID_0;
4127 param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
4128 param_hdr.param_size = sizeof(mfc_cfg);
4129
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304130 mfc_cfg.sampling_rate = sample_rate;
4131 mfc_cfg.bits_per_sample =
4132 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
4133 mfc_cfg.num_channels = num_channels;
4134
4135 /* Currently applying speaker swap for only 2 channel use case */
4136 if (spk_swap) {
4137 mfc_cfg.channel_type[0] =
4138 (uint16_t) PCM_CHANNEL_FR;
4139 mfc_cfg.channel_type[1] =
4140 (uint16_t) PCM_CHANNEL_FL;
4141 } else {
4142 mfc_cfg.channel_type[0] =
4143 (uint16_t) PCM_CHANNEL_FL;
4144 mfc_cfg.channel_type[1] =
4145 (uint16_t) PCM_CHANNEL_FR;
4146 }
4147
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004148 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4149 (u8 *) &mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304150 if (ret < 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004151 pr_err("%s: Failed to set swap speaker channels on port[0x%x] failed %d\n",
4152 __func__, port_id, ret);
4153 return ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304154 }
4155
4156 pr_debug("%s: mfc_cfg Set params returned success", __func__);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004157 return 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304158}
4159EXPORT_SYMBOL(adm_swap_speaker_channels);
4160
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304161/**
4162 * adm_set_sound_focus -
4163 * Update sound focus info
4164 *
4165 * @port_id: Port ID number
4166 * @copp_idx: copp index assigned
4167 * @soundFocusData: sound focus data to pass
4168 *
4169 * Returns 0 on success or error on failure
4170 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304171int adm_set_sound_focus(int port_id, int copp_idx,
4172 struct sound_focus_param soundFocusData)
4173{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004174 struct adm_param_fluence_soundfocus_t soundfocus_params;
4175 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304176 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304177 int i;
4178
4179 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4180 __func__, port_id, copp_idx);
4181
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004182 memset(&param_hdr, 0, sizeof(param_hdr));
4183 param_hdr.module_id = VOICEPROC_MODULE_ID_GENERIC_TX;
4184 param_hdr.instance_id = INSTANCE_ID_0;
4185 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
4186 param_hdr.param_size = sizeof(soundfocus_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304187
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004188 memset(&(soundfocus_params), 0xFF, sizeof(soundfocus_params));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304189 for (i = 0; i < MAX_SECTORS; i++) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004190 soundfocus_params.start_angles[i] =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304191 soundFocusData.start_angle[i];
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004192 soundfocus_params.enables[i] = soundFocusData.enable[i];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304193 pr_debug("%s: start_angle[%d] = %d\n",
4194 __func__, i, soundFocusData.start_angle[i]);
4195 pr_debug("%s: enable[%d] = %d\n",
4196 __func__, i, soundFocusData.enable[i]);
4197 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004198 soundfocus_params.gain_step = soundFocusData.gain_step;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304199 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData.gain_step);
4200
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004201 soundfocus_params.reserved = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304202
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004203 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4204 (uint8_t *) &soundfocus_params);
4205 if (ret)
4206 pr_err("%s: Failed to set sound focus params, err %d\n",
4207 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304208
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304209 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
4210
4211 return ret;
4212}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304213EXPORT_SYMBOL(adm_set_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304214
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304215/**
4216 * adm_get_sound_focus -
4217 * Retrieve sound focus info
4218 *
4219 * @port_id: Port ID number
4220 * @copp_idx: copp index assigned
4221 * @soundFocusData: pointer for sound focus data to be updated with
4222 *
4223 * Returns 0 on success or error on failure
4224 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304225int adm_get_sound_focus(int port_id, int copp_idx,
4226 struct sound_focus_param *soundFocusData)
4227{
4228 int ret = 0, i;
4229 char *params_value;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004230 uint32_t max_param_size = 0;
4231 struct adm_param_fluence_soundfocus_t *soundfocus_params = NULL;
4232 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304233
4234 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4235 __func__, port_id, copp_idx);
4236
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004237 max_param_size = sizeof(struct adm_param_fluence_soundfocus_t) +
4238 sizeof(union param_hdrs);
4239 params_value = kzalloc(max_param_size, GFP_KERNEL);
4240 if (!params_value)
4241 return -ENOMEM;
4242
4243 memset(&param_hdr, 0, sizeof(param_hdr));
4244 param_hdr.module_id = VOICEPROC_MODULE_ID_GENERIC_TX;
4245 param_hdr.instance_id = INSTANCE_ID_0;
4246 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
4247 param_hdr.param_size = max_param_size;
4248 ret = adm_get_pp_params(port_id, copp_idx,
4249 ADM_CLIENT_ID_SOURCE_TRACKING, NULL, &param_hdr,
4250 params_value);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304251 if (ret) {
4252 pr_err("%s: get parameters failed ret:%d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304253 ret = -EINVAL;
4254 goto done;
4255 }
4256
4257 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
4258 pr_err("%s - get params returned error [%s]\n",
4259 __func__, adsp_err_get_err_str(
4260 this_adm.sourceTrackingData.apr_cmd_status));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304261 ret = adsp_err_get_lnx_err_code(
4262 this_adm.sourceTrackingData.apr_cmd_status);
4263 goto done;
4264 }
4265
4266 soundfocus_params = (struct adm_param_fluence_soundfocus_t *)
4267 params_value;
4268 for (i = 0; i < MAX_SECTORS; i++) {
4269 soundFocusData->start_angle[i] =
4270 soundfocus_params->start_angles[i];
4271 soundFocusData->enable[i] = soundfocus_params->enables[i];
4272 pr_debug("%s: start_angle[%d] = %d\n",
4273 __func__, i, soundFocusData->start_angle[i]);
4274 pr_debug("%s: enable[%d] = %d\n",
4275 __func__, i, soundFocusData->enable[i]);
4276 }
4277 soundFocusData->gain_step = soundfocus_params->gain_step;
4278 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData->gain_step);
4279
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304280done:
4281 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
4282
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004283 kfree(params_value);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304284 return ret;
4285}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304286EXPORT_SYMBOL(adm_get_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304287
4288static int adm_source_tracking_alloc_map_memory(void)
4289{
4290 int ret;
4291
4292 pr_debug("%s: Enter\n", __func__);
4293
Banajit Goswami08bb7362017-11-03 22:48:23 -07004294 ret = msm_audio_ion_alloc(&this_adm.sourceTrackingData.dma_buf,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304295 AUD_PROC_BLOCK_SIZE,
4296 &this_adm.sourceTrackingData.memmap.paddr,
4297 &this_adm.sourceTrackingData.memmap.size,
4298 &this_adm.sourceTrackingData.memmap.kvaddr);
4299 if (ret) {
4300 pr_err("%s: failed to allocate memory\n", __func__);
4301
4302 ret = -EINVAL;
4303 goto done;
4304 }
4305
4306 atomic_set(&this_adm.mem_map_index, ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
4307 ret = adm_memory_map_regions(&this_adm.sourceTrackingData.memmap.paddr,
4308 0,
4309 (uint32_t *)&this_adm.sourceTrackingData.memmap.size,
4310 1);
4311 if (ret < 0) {
4312 pr_err("%s: failed to map memory, paddr = 0x%pK, size = %d\n",
4313 __func__,
4314 (void *)this_adm.sourceTrackingData.memmap.paddr,
4315 (uint32_t)this_adm.sourceTrackingData.memmap.size);
4316
Banajit Goswami08bb7362017-11-03 22:48:23 -07004317 msm_audio_ion_free(this_adm.sourceTrackingData.dma_buf);
4318 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304319 this_adm.sourceTrackingData.memmap.size = 0;
4320 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
4321 this_adm.sourceTrackingData.memmap.paddr = 0;
4322 this_adm.sourceTrackingData.apr_cmd_status = -1;
4323 atomic_set(&this_adm.mem_map_handles
4324 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
4325
4326 ret = -EINVAL;
4327 goto done;
4328 }
4329 ret = 0;
4330 pr_debug("%s: paddr = 0x%pK, size = %d, mem_map_handle = 0x%x\n",
4331 __func__, (void *)this_adm.sourceTrackingData.memmap.paddr,
4332 (uint32_t)this_adm.sourceTrackingData.memmap.size,
4333 atomic_read(&this_adm.mem_map_handles
4334 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING]));
4335
4336done:
4337 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
4338
4339 return ret;
4340}
4341
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304342/**
4343 * adm_get_source_tracking -
4344 * Retrieve source tracking info
4345 *
4346 * @port_id: Port ID number
4347 * @copp_idx: copp index assigned
4348 * @sourceTrackingData: pointer for source track data to be updated with
4349 *
4350 * Returns 0 on success or error on failure
4351 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304352int adm_get_source_tracking(int port_id, int copp_idx,
4353 struct source_tracking_param *sourceTrackingData)
4354{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004355 struct adm_param_fluence_sourcetracking_t *source_tracking_params =
4356 NULL;
4357 struct mem_mapping_hdr mem_hdr;
4358 struct param_hdr_v3 param_hdr;
4359 int i = 0;
4360 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304361
4362 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4363 __func__, port_id, copp_idx);
4364
4365 if (!this_adm.sourceTrackingData.memmap.paddr) {
4366 /* Allocate and map shared memory for out of band usage */
4367 ret = adm_source_tracking_alloc_map_memory();
4368 if (ret != 0) {
4369 ret = -EINVAL;
4370 goto done;
4371 }
4372 }
4373
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004374 memset(&mem_hdr, 0, sizeof(mem_hdr));
4375 memset(&param_hdr, 0, sizeof(param_hdr));
4376 mem_hdr.data_payload_addr_lsw =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304377 lower_32_bits(this_adm.sourceTrackingData.memmap.paddr);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004378 mem_hdr.data_payload_addr_msw = msm_audio_populate_upper_32_bits(
4379 this_adm.sourceTrackingData.memmap.paddr);
4380 mem_hdr.mem_map_handle = atomic_read(
4381 &this_adm.mem_map_handles[ADM_MEM_MAP_INDEX_SOURCE_TRACKING]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304382
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004383 param_hdr.module_id = VOICEPROC_MODULE_ID_GENERIC_TX;
4384 param_hdr.instance_id = INSTANCE_ID_0;
4385 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOURCETRACKING;
4386 /*
4387 * This size should be the max size of the calibration data + header.
4388 * Use the union size to ensure max size is used.
4389 */
4390 param_hdr.param_size =
4391 sizeof(struct adm_param_fluence_sourcetracking_t) +
4392 sizeof(union param_hdrs);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304393
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004394 /*
4395 * Retrieving parameters out of band, so no need to provide a buffer for
4396 * the returned parameter data as it will be at the memory location
4397 * provided.
4398 */
4399 ret = adm_get_pp_params(port_id, copp_idx,
4400 ADM_CLIENT_ID_SOURCE_TRACKING, &mem_hdr,
4401 &param_hdr, NULL);
4402 if (ret) {
4403 pr_err("%s: Failed to get params, error %d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304404 goto done;
4405 }
4406
4407 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
4408 pr_err("%s - get params returned error [%s]\n",
4409 __func__, adsp_err_get_err_str(
4410 this_adm.sourceTrackingData.apr_cmd_status));
4411
4412 ret = adsp_err_get_lnx_err_code(
4413 this_adm.sourceTrackingData.apr_cmd_status);
4414 goto done;
4415 }
4416
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004417 /* How do we know what the param data was retrieved with for hdr size */
4418 source_tracking_params =
4419 (struct adm_param_fluence_sourcetracking_t
4420 *) (this_adm.sourceTrackingData.memmap.kvaddr +
4421 sizeof(struct param_hdr_v1));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304422 for (i = 0; i < MAX_SECTORS; i++) {
4423 sourceTrackingData->vad[i] = source_tracking_params->vad[i];
4424 pr_debug("%s: vad[%d] = %d\n",
4425 __func__, i, sourceTrackingData->vad[i]);
4426 }
4427 sourceTrackingData->doa_speech = source_tracking_params->doa_speech;
4428 pr_debug("%s: doa_speech = %d\n",
4429 __func__, sourceTrackingData->doa_speech);
4430
4431 for (i = 0; i < MAX_NOISE_SOURCE_INDICATORS; i++) {
4432 sourceTrackingData->doa_noise[i] =
4433 source_tracking_params->doa_noise[i];
4434 pr_debug("%s: doa_noise[%d] = %d\n",
4435 __func__, i, sourceTrackingData->doa_noise[i]);
4436 }
4437 for (i = 0; i < MAX_POLAR_ACTIVITY_INDICATORS; i++) {
4438 sourceTrackingData->polar_activity[i] =
4439 source_tracking_params->polar_activity[i];
4440 pr_debug("%s: polar_activity[%d] = %d\n",
4441 __func__, i, sourceTrackingData->polar_activity[i]);
4442 }
4443
4444 ret = 0;
4445
4446done:
4447 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
4448
4449 return ret;
4450}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304451EXPORT_SYMBOL(adm_get_source_tracking);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304452
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304453int __init adm_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304454{
4455 int i = 0, j;
4456
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304457 this_adm.ec_ref_rx = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304458 init_waitqueue_head(&this_adm.matrix_map_wait);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304459 init_waitqueue_head(&this_adm.adm_wait);
4460
4461 for (i = 0; i < AFE_MAX_PORTS; i++) {
4462 for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
4463 atomic_set(&this_adm.copp.id[i][j], RESET_COPP_ID);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304464 init_waitqueue_head(&this_adm.copp.wait[i][j]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304465 init_waitqueue_head(
4466 &this_adm.copp.adm_delay_wait[i][j]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304467 }
4468 }
4469
4470 if (adm_init_cal_data())
4471 pr_err("%s: could not init cal data!\n", __func__);
4472
Banajit Goswami08bb7362017-11-03 22:48:23 -07004473 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304474 this_adm.sourceTrackingData.memmap.size = 0;
4475 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
4476 this_adm.sourceTrackingData.memmap.paddr = 0;
4477 this_adm.sourceTrackingData.apr_cmd_status = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304478
4479 return 0;
4480}
4481
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05304482void adm_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304483{
Laxminath Kasam30ad7512017-11-28 12:40:22 +05304484 if (this_adm.apr)
4485 adm_reset_data();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304486 adm_delete_cal_data();
4487}