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