blob: b9c11ac6b4c3625200022e1d65990fb6fd143b87 [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>
Dieter Luecking50c55352018-09-28 14:29:17 +020026#include <dsp/q6core.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053027#include <dsp/audio_cal_utils.h>
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -080028#include <dsp/q6common.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053029#include <ipc/apr.h>
30#include "adsp_err.h"
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053031
32#define TIMEOUT_MS 1000
33
34#define RESET_COPP_ID 99
35#define INVALID_COPP_ID 0xFF
36/* Used for inband payload copy, max size is 4k */
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -080037/* 3 is to account for module, instance & param ID in payload */
38#define ADM_GET_PARAMETER_LENGTH (4096 - APR_HDR_SIZE - 3 * sizeof(uint32_t))
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053039
40#define ULL_SUPPORTED_BITS_PER_SAMPLE 16
41#define ULL_SUPPORTED_SAMPLE_RATE 48000
42
43#ifndef CONFIG_DOLBY_DAP
44#undef DOLBY_ADM_COPP_TOPOLOGY_ID
45#define DOLBY_ADM_COPP_TOPOLOGY_ID 0xFFFFFFFE
46#endif
47
48#ifndef CONFIG_DOLBY_DS2
49#undef DS2_ADM_COPP_TOPOLOGY_ID
50#define DS2_ADM_COPP_TOPOLOGY_ID 0xFFFFFFFF
51#endif
52
53/* ENUM for adm_status */
54enum adm_cal_status {
55 ADM_STATUS_CALIBRATION_REQUIRED = 0,
56 ADM_STATUS_MAX,
57};
58
59struct adm_copp {
60
61 atomic_t id[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
62 atomic_t cnt[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
63 atomic_t topology[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
64 atomic_t mode[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
65 atomic_t stat[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
66 atomic_t rate[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
67 atomic_t bit_width[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
68 atomic_t channels[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
69 atomic_t app_type[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
70 atomic_t acdb_id[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
71 wait_queue_head_t wait[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
72 wait_queue_head_t adm_delay_wait[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
73 atomic_t adm_delay_stat[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
74 uint32_t adm_delay[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
75 unsigned long adm_status[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
76};
77
78struct source_tracking_data {
Banajit Goswami08bb7362017-11-03 22:48:23 -070079 struct dma_buf *dma_buf;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053080 struct param_outband memmap;
81 int apr_cmd_status;
82};
83
84struct adm_ctl {
85 void *apr;
86
87 struct adm_copp copp;
88
89 atomic_t matrix_map_stat;
90 wait_queue_head_t matrix_map_wait;
91
92 atomic_t adm_stat;
93 wait_queue_head_t adm_wait;
94
95 struct cal_type_data *cal_data[ADM_MAX_CAL_TYPES];
96
97 atomic_t mem_map_handles[ADM_MEM_MAP_INDEX_MAX];
98 atomic_t mem_map_index;
99
100 struct param_outband outband_memmap;
101 struct source_tracking_data sourceTrackingData;
102
103 int set_custom_topology;
104 int ec_ref_rx;
105 int num_ec_ref_rx_chans;
106 int ec_ref_rx_bit_width;
107 int ec_ref_rx_sampling_rate;
Dieter Luecking50c55352018-09-28 14:29:17 +0200108
109 int native_mode;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530110};
111
112static struct adm_ctl this_adm;
113
114struct adm_multi_ch_map {
115 bool set_channel_map;
Dieter Luecking50c55352018-09-28 14:29:17 +0200116 char channel_mapping[PCM_FORMAT_MAX_NUM_CHANNEL_V8];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530117};
118
119#define ADM_MCH_MAP_IDX_PLAYBACK 0
120#define ADM_MCH_MAP_IDX_REC 1
121static struct adm_multi_ch_map multi_ch_maps[2] = {
Dieter Luecking50c55352018-09-28 14:29:17 +0200122 { false,
123 {0, 0, 0, 0, 0, 0, 0, 0,
124 0, 0, 0, 0, 0, 0, 0, 0,
125 0, 0, 0, 0, 0, 0, 0, 0,
126 0, 0, 0, 0, 0, 0, 0, 0}
127 },
128 { false,
129 {0, 0, 0, 0, 0, 0, 0, 0,
130 0, 0, 0, 0, 0, 0, 0, 0,
131 0, 0, 0, 0, 0, 0, 0, 0,
132 0, 0, 0, 0, 0, 0, 0, 0}
133 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530134};
135
136static int adm_get_parameters[MAX_COPPS_PER_PORT * ADM_GET_PARAMETER_LENGTH];
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800137static int adm_module_topo_list[MAX_COPPS_PER_PORT *
138 ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH];
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530139static struct mutex dts_srs_lock;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530140
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530141void msm_dts_srs_acquire_lock(void)
142{
143 mutex_lock(&dts_srs_lock);
144}
145
146void msm_dts_srs_release_lock(void)
147{
148 mutex_unlock(&dts_srs_lock);
149}
150
151/**
152 * adm_validate_and_get_port_index -
153 * validate given port id
154 *
155 * @port_id: Port ID number
156 *
157 * Returns valid index on success or error on failure
158 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530159int adm_validate_and_get_port_index(int port_id)
160{
161 int index;
162 int ret;
163
164 ret = q6audio_validate_port(port_id);
165 if (ret < 0) {
166 pr_err("%s: port validation failed id 0x%x ret %d\n",
167 __func__, port_id, ret);
168 return -EINVAL;
169 }
170
171 index = afe_get_port_index(port_id);
172 if (index < 0 || index >= AFE_MAX_PORTS) {
173 pr_err("%s: Invalid port idx %d port_id 0x%x\n",
174 __func__, index,
175 port_id);
176 return -EINVAL;
177 }
178 pr_debug("%s: port_idx- %d\n", __func__, index);
179 return index;
180}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530181EXPORT_SYMBOL(adm_validate_and_get_port_index);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530182
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530183/**
184 * adm_get_default_copp_idx -
185 * retrieve default copp_idx for given port
186 *
187 * @port_id: Port ID number
188 *
189 * Returns valid value on success or error on failure
190 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530191int adm_get_default_copp_idx(int port_id)
192{
193 int port_idx = adm_validate_and_get_port_index(port_id), idx;
194
195 if (port_idx < 0) {
196 pr_err("%s: Invalid port id: 0x%x", __func__, port_id);
197 return -EINVAL;
198 }
199 pr_debug("%s: port_idx:%d\n", __func__, port_idx);
200 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++) {
201 if (atomic_read(&this_adm.copp.id[port_idx][idx]) !=
202 RESET_COPP_ID)
203 return idx;
204 }
205 return -EINVAL;
206}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530207EXPORT_SYMBOL(adm_get_default_copp_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530208
209int adm_get_topology_for_port_from_copp_id(int port_id, int copp_id)
210{
211 int port_idx = adm_validate_and_get_port_index(port_id), idx;
212
213 if (port_idx < 0) {
214 pr_err("%s: Invalid port id: 0x%x", __func__, port_id);
215 return 0;
216 }
217 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++)
218 if (atomic_read(&this_adm.copp.id[port_idx][idx]) == copp_id)
219 return atomic_read(&this_adm.copp.topology[port_idx]
220 [idx]);
221 pr_err("%s: Invalid copp_id %d port_id 0x%x\n",
222 __func__, copp_id, port_id);
223 return 0;
224}
225
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530226/**
227 * adm_get_topology_for_port_copp_idx -
228 * retrieve topology of given port/copp_idx
229 *
230 * @port_id: Port ID number
231 * @copp_idx: copp index of ADM copp
232 *
233 * Returns valid value on success or 0 on failure
234 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530235int adm_get_topology_for_port_copp_idx(int port_id, int copp_idx)
236{
237 int port_idx = adm_validate_and_get_port_index(port_id);
238
239 if (port_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
240 pr_err("%s: Invalid port: 0x%x copp id: 0x%x",
241 __func__, port_id, copp_idx);
242 return 0;
243 }
244 return atomic_read(&this_adm.copp.topology[port_idx][copp_idx]);
245}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530246EXPORT_SYMBOL(adm_get_topology_for_port_copp_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530247
248int adm_get_indexes_from_copp_id(int copp_id, int *copp_idx, int *port_idx)
249{
250 int p_idx, c_idx;
251
252 for (p_idx = 0; p_idx < AFE_MAX_PORTS; p_idx++) {
253 for (c_idx = 0; c_idx < MAX_COPPS_PER_PORT; c_idx++) {
254 if (atomic_read(&this_adm.copp.id[p_idx][c_idx])
255 == copp_id) {
256 if (copp_idx != NULL)
257 *copp_idx = c_idx;
258 if (port_idx != NULL)
259 *port_idx = p_idx;
260 return 0;
261 }
262 }
263 }
264 return -EINVAL;
265}
266
267static int adm_get_copp_id(int port_idx, int copp_idx)
268{
269 pr_debug("%s: port_idx:%d copp_idx:%d\n", __func__, port_idx, copp_idx);
270
271 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
272 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
273 return -EINVAL;
274 }
275 return atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
276}
277
278static int adm_get_idx_if_copp_exists(int port_idx, int topology, int mode,
279 int rate, int bit_width, int app_type)
280{
281 int idx;
282
283 pr_debug("%s: port_idx-%d, topology-0x%x, mode-%d, rate-%d, bit_width-%d\n",
284 __func__, port_idx, topology, mode, rate, bit_width);
285
286 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++)
287 if ((topology ==
288 atomic_read(&this_adm.copp.topology[port_idx][idx])) &&
289 (mode == atomic_read(&this_adm.copp.mode[port_idx][idx])) &&
290 (rate == atomic_read(&this_adm.copp.rate[port_idx][idx])) &&
291 (bit_width ==
292 atomic_read(&this_adm.copp.bit_width[port_idx][idx])) &&
293 (app_type ==
294 atomic_read(&this_adm.copp.app_type[port_idx][idx])))
295 return idx;
296 return -EINVAL;
297}
298
299static int adm_get_next_available_copp(int port_idx)
300{
301 int idx;
302
303 pr_debug("%s:\n", __func__);
304 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++) {
305 pr_debug("%s: copp_id:0x%x port_idx:%d idx:%d\n", __func__,
306 atomic_read(&this_adm.copp.id[port_idx][idx]),
307 port_idx, idx);
308 if (atomic_read(&this_adm.copp.id[port_idx][idx]) ==
309 RESET_COPP_ID)
310 break;
311 }
312 return idx;
313}
314
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530315/**
316 * srs_trumedia_open -
317 * command to set SRS trumedia open
318 *
319 * @port_id: Port ID number
320 * @copp_idx: copp index of ADM copp
321 * @srs_tech_id: SRS tech index
322 * @srs_params: params pointer
323 *
324 * Returns 0 on success or error on failure
325 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530326int srs_trumedia_open(int port_id, int copp_idx, __s32 srs_tech_id,
327 void *srs_params)
328{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800329 struct param_hdr_v3 param_hdr;
330 struct mem_mapping_hdr mem_hdr;
331 u32 total_param_size = 0;
332 bool outband = false;
333 int port_idx;
334 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530335
336 pr_debug("SRS - %s", __func__);
337
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800338 memset(&param_hdr, 0, sizeof(param_hdr));
339 memset(&mem_hdr, 0, sizeof(mem_hdr));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530340 port_id = afe_convert_virtual_to_portid(port_id);
341 port_idx = adm_validate_and_get_port_index(port_id);
342 if (port_idx < 0) {
343 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
344 return -EINVAL;
345 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800346
347 param_hdr.module_id = SRS_TRUMEDIA_MODULE_ID;
348 param_hdr.instance_id = INSTANCE_ID_0;
349
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530350 switch (srs_tech_id) {
351 case SRS_ID_GLOBAL: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800352 param_hdr.param_id = SRS_TRUMEDIA_PARAMS;
353 param_hdr.param_size =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530354 sizeof(struct srs_trumedia_params_GLOBAL);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530355 break;
356 }
357 case SRS_ID_WOWHD: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800358 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_WOWHD;
359 param_hdr.param_size = sizeof(struct srs_trumedia_params_WOWHD);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530360 break;
361 }
362 case SRS_ID_CSHP: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800363 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_CSHP;
364 param_hdr.param_size = sizeof(struct srs_trumedia_params_CSHP);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530365 break;
366 }
367 case SRS_ID_HPF: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800368 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_HPF;
369 param_hdr.param_size = sizeof(struct srs_trumedia_params_HPF);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530370 break;
371 }
372 case SRS_ID_AEQ: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800373 u8 *update_params_ptr = (u8 *) this_adm.outband_memmap.kvaddr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530374
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800375 outband = true;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530376
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530377 if (update_params_ptr == NULL) {
378 pr_err("ADM_SRS_TRUMEDIA - %s: null memmap for AEQ params\n",
379 __func__);
380 ret = -EINVAL;
381 goto fail_cmd;
382 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530383
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800384 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_AEQ;
385 param_hdr.param_size = sizeof(struct srs_trumedia_params_AEQ);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530386
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800387 ret = q6common_pack_pp_params(update_params_ptr, &param_hdr,
388 srs_params, &total_param_size);
389 if (ret) {
390 pr_err("%s: Failed to pack param header and data, error %d\n",
391 __func__, ret);
392 goto fail_cmd;
393 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530394 break;
395 }
396 case SRS_ID_HL: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800397 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_HL;
398 param_hdr.param_size = sizeof(struct srs_trumedia_params_HL);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530399 break;
400 }
401 case SRS_ID_GEQ: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800402 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_GEQ;
403 param_hdr.param_size = sizeof(struct srs_trumedia_params_GEQ);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530404 break;
405 }
406 default:
407 goto fail_cmd;
408 }
409
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530410 if (outband && this_adm.outband_memmap.paddr) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800411 mem_hdr.data_payload_addr_lsw =
412 lower_32_bits(this_adm.outband_memmap.paddr);
413 mem_hdr.data_payload_addr_msw =
414 msm_audio_populate_upper_32_bits(
415 this_adm.outband_memmap.paddr);
416 mem_hdr.mem_map_handle = atomic_read(
417 &this_adm.mem_map_handles[ADM_SRS_TRUMEDIA]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530418
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800419 ret = adm_set_pp_params(port_id, copp_idx, &mem_hdr, NULL,
420 total_param_size);
421 } else {
422 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx,
423 param_hdr,
424 (u8 *) srs_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530425 }
426
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800427 if (ret < 0)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530428 pr_err("SRS - %s: ADM enable for port %d failed\n", __func__,
429 port_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530430
431fail_cmd:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530432 return ret;
433}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530434EXPORT_SYMBOL(srs_trumedia_open);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530435
436static int adm_populate_channel_weight(u16 *ptr,
437 struct msm_pcm_channel_mixer *ch_mixer,
438 int channel_index)
439{
440 u16 i, j, start_index = 0;
441
442 if (channel_index > ch_mixer->output_channel) {
443 pr_err("%s: channel index %d is larger than output_channel %d\n",
444 __func__, channel_index, ch_mixer->output_channel);
445 return -EINVAL;
446 }
447
448 for (i = 0; i < ch_mixer->output_channel; i++) {
449 pr_debug("%s: weight for output %d:", __func__, i);
450 for (j = 0; j < ADM_MAX_CHANNELS; j++)
451 pr_debug(" %d",
452 ch_mixer->channel_weight[i][j]);
453 pr_debug("\n");
454 }
455
456 for (i = 0; i < channel_index; ++i)
457 start_index += ch_mixer->input_channels[i];
458
459 for (i = 0; i < ch_mixer->output_channel; ++i) {
460 for (j = start_index;
461 j < start_index +
462 ch_mixer->input_channels[channel_index]; j++) {
463 *ptr = ch_mixer->channel_weight[i][j];
464 pr_debug("%s: ptr[%d][%d] = %d\n",
465 __func__, i, j, *ptr);
466 ptr++;
467 }
468 }
469
470 return 0;
471}
472
473/*
474 * adm_programable_channel_mixer
475 *
476 * Receives port_id, copp_idx, session_id, session_type, ch_mixer
477 * and channel_index to send ADM command to mix COPP data.
478 *
479 * port_id - Passed value, port_id for which backend is wanted
480 * copp_idx - Passed value, copp_idx for which COPP is wanted
481 * session_id - Passed value, session_id for which session is needed
482 * session_type - Passed value, session_type for RX or TX
483 * ch_mixer - Passed value, ch_mixer for which channel mixer config is needed
484 * channel_index - Passed value, channel_index for which channel is needed
485 */
486int adm_programable_channel_mixer(int port_id, int copp_idx, int session_id,
487 int session_type,
488 struct msm_pcm_channel_mixer *ch_mixer,
489 int channel_index)
490{
491 struct adm_cmd_set_pspd_mtmx_strtr_params_v5 *adm_params = NULL;
Vignesh Kulothungan2c6dccd2018-03-22 14:18:43 -0700492 struct param_hdr_v1 data_v5;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530493 int ret = 0, port_idx, sz = 0, param_size = 0;
494 u16 *adm_pspd_params;
495 u16 *ptr;
496 int index = 0;
497
498 pr_debug("%s: port_id = %d\n", __func__, port_id);
499 port_id = afe_convert_virtual_to_portid(port_id);
500 port_idx = adm_validate_and_get_port_index(port_id);
501 if (port_idx < 0) {
502 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
503 return -EINVAL;
504 }
505 /*
506 * First 8 bytes are 4 bytes as rule number, 2 bytes as output
507 * channel and 2 bytes as input channel.
508 * 2 * ch_mixer->output_channel means output channel mapping.
509 * 2 * ch_mixer->input_channels[channel_index]) means input
510 * channel mapping.
511 * 2 * ch_mixer->input_channels[channel_index] *
512 * ch_mixer->output_channel) means the channel mixer weighting
513 * coefficients.
514 * param_size needs to be a multiple of 4 bytes.
515 */
516
517 param_size = 2 * (4 + ch_mixer->output_channel +
518 ch_mixer->input_channels[channel_index] +
519 ch_mixer->input_channels[channel_index] *
520 ch_mixer->output_channel);
521 roundup(param_size, 4);
522
523 sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5) +
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800524 sizeof(struct default_chmixer_param_id_coeff) +
Vignesh Kulothungan2c6dccd2018-03-22 14:18:43 -0700525 sizeof(struct param_hdr_v1) + param_size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530526 pr_debug("%s: sz = %d\n", __func__, sz);
527 adm_params = kzalloc(sz, GFP_KERNEL);
528 if (!adm_params)
529 return -ENOMEM;
530
531 adm_params->payload_addr_lsw = 0;
532 adm_params->payload_addr_msw = 0;
533 adm_params->mem_map_handle = 0;
534 adm_params->direction = session_type;
535 adm_params->sessionid = session_id;
536 pr_debug("%s: copp_id = %d, session id %d\n", __func__,
537 atomic_read(&this_adm.copp.id[port_idx][copp_idx]),
538 session_id);
539 adm_params->deviceid = atomic_read(
540 &this_adm.copp.id[port_idx][copp_idx]);
541 adm_params->reserved = 0;
542
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800543 /*
544 * This module is internal to ADSP and cannot be configured with
545 * an instance id
546 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530547 data_v5.module_id = MTMX_MODULE_ID_DEFAULT_CHMIXER;
548 data_v5.param_id = DEFAULT_CHMIXER_PARAM_ID_COEFF;
549 data_v5.reserved = 0;
550 data_v5.param_size = param_size;
551 adm_params->payload_size =
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800552 sizeof(struct default_chmixer_param_id_coeff) +
Vignesh Kulothungan2c6dccd2018-03-22 14:18:43 -0700553 sizeof(struct param_hdr_v1) + data_v5.param_size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530554 adm_pspd_params = (u16 *)((u8 *)adm_params +
555 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5));
556 memcpy(adm_pspd_params, &data_v5, sizeof(data_v5));
557
558 adm_pspd_params = (u16 *)((u8 *)adm_params +
559 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5)
560 + sizeof(data_v5));
561
562 adm_pspd_params[0] = ch_mixer->rule;
563 adm_pspd_params[2] = ch_mixer->output_channel;
564 adm_pspd_params[3] = ch_mixer->input_channels[channel_index];
565 index = 4;
566
567 if (ch_mixer->output_channel == 1) {
568 adm_pspd_params[index] = PCM_CHANNEL_FC;
569 } else if (ch_mixer->output_channel == 2) {
570 adm_pspd_params[index] = PCM_CHANNEL_FL;
571 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
572 } else if (ch_mixer->output_channel == 3) {
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 } else if (ch_mixer->output_channel == 4) {
577 adm_pspd_params[index] = PCM_CHANNEL_FL;
578 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
579 adm_pspd_params[index + 2] = PCM_CHANNEL_LS;
580 adm_pspd_params[index + 3] = PCM_CHANNEL_RS;
581 } else if (ch_mixer->output_channel == 5) {
582 adm_pspd_params[index] = PCM_CHANNEL_FL;
583 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
584 adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
585 adm_pspd_params[index + 3] = PCM_CHANNEL_LS;
586 adm_pspd_params[index + 4] = PCM_CHANNEL_RS;
587 } else if (ch_mixer->output_channel == 6) {
588 adm_pspd_params[index] = PCM_CHANNEL_FL;
589 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
590 adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
591 adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
592 adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
593 adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
594 } else if (ch_mixer->output_channel == 8) {
595 adm_pspd_params[index] = PCM_CHANNEL_FL;
596 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
597 adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
598 adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
599 adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
600 adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
601 adm_pspd_params[index + 6] = PCM_CHANNEL_LB;
602 adm_pspd_params[index + 7] = PCM_CHANNEL_RB;
603 }
604
605 index = index + ch_mixer->output_channel;
606 if (ch_mixer->input_channels[channel_index] == 1) {
607 adm_pspd_params[index] = PCM_CHANNEL_FC;
608 } else if (ch_mixer->input_channels[channel_index] == 2) {
609 adm_pspd_params[index] = PCM_CHANNEL_FL;
610 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
611 } else if (ch_mixer->input_channels[channel_index] == 3) {
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 } else if (ch_mixer->input_channels[channel_index] == 4) {
616 adm_pspd_params[index] = PCM_CHANNEL_FL;
617 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
618 adm_pspd_params[index + 2] = PCM_CHANNEL_LS;
619 adm_pspd_params[index + 3] = PCM_CHANNEL_RS;
620 } else if (ch_mixer->input_channels[channel_index] == 5) {
621 adm_pspd_params[index] = PCM_CHANNEL_FL;
622 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
623 adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
624 adm_pspd_params[index + 3] = PCM_CHANNEL_LS;
625 adm_pspd_params[index + 4] = PCM_CHANNEL_RS;
626 } else if (ch_mixer->input_channels[channel_index] == 6) {
627 adm_pspd_params[index] = PCM_CHANNEL_FL;
628 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
629 adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
630 adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
631 adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
632 adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
633 } else if (ch_mixer->input_channels[channel_index] == 8) {
634 adm_pspd_params[index] = PCM_CHANNEL_FL;
635 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
636 adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
637 adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
638 adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
639 adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
640 adm_pspd_params[index + 6] = PCM_CHANNEL_LB;
641 adm_pspd_params[index + 7] = PCM_CHANNEL_RB;
642 }
643
644 index = index + ch_mixer->input_channels[channel_index];
645 ret = adm_populate_channel_weight(&adm_pspd_params[index],
646 ch_mixer, channel_index);
647 if (!ret) {
648 pr_err("%s: fail to get channel weight with error %d\n",
649 __func__, ret);
650 goto fail_cmd;
651 }
652
653 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
654 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
655 adm_params->hdr.src_svc = APR_SVC_ADM;
656 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
657 adm_params->hdr.src_port = port_id;
658 adm_params->hdr.dest_svc = APR_SVC_ADM;
659 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
660 adm_params->hdr.dest_port =
661 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
662 adm_params->hdr.token = port_idx << 16 | copp_idx;
663 adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5;
664 adm_params->hdr.pkt_size = sz;
665 adm_params->payload_addr_lsw = 0;
666 adm_params->payload_addr_msw = 0;
667 adm_params->mem_map_handle = 0;
668 adm_params->reserved = 0;
669
670 ptr = (u16 *)adm_params;
671 for (index = 0; index < (sz / 2); index++)
672 pr_debug("%s: adm_params[%d] = 0x%x\n",
673 __func__, index, (unsigned int)ptr[index]);
674
675 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], 0);
676 ret = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
677 if (ret < 0) {
678 pr_err("%s: Set params failed port %d rc %d\n", __func__,
679 port_id, ret);
680 ret = -EINVAL;
681 goto fail_cmd;
682 }
683
684 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
685 atomic_read(
686 &this_adm.copp.stat[port_idx][copp_idx]) >= 0,
687 msecs_to_jiffies(TIMEOUT_MS));
688 if (!ret) {
689 pr_err("%s: set params timed out port = %d\n",
690 __func__, port_id);
691 ret = -ETIMEDOUT;
692 goto fail_cmd;
693 }
694 ret = 0;
695fail_cmd:
696 kfree(adm_params);
697
698 return ret;
699}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530700EXPORT_SYMBOL(adm_programable_channel_mixer);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530701
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530702/**
703 * adm_set_stereo_to_custom_stereo -
704 * command to update custom stereo
705 *
706 * @port_id: Port ID number
707 * @copp_idx: copp index of ADM copp
708 * @session_id: session id to be updated
709 * @params: params pointer
710 * @param_length: length of params
711 *
712 * Returns 0 on success or error on failure
713 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530714int adm_set_stereo_to_custom_stereo(int port_id, int copp_idx,
715 unsigned int session_id, char *params,
716 uint32_t params_length)
717{
718 struct adm_cmd_set_pspd_mtmx_strtr_params_v5 *adm_params = NULL;
719 int sz, rc = 0, port_idx;
720
721 pr_debug("%s:\n", __func__);
722 port_id = afe_convert_virtual_to_portid(port_id);
723 port_idx = adm_validate_and_get_port_index(port_id);
724 if (port_idx < 0) {
725 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
726 return -EINVAL;
727 }
728
729 sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5) +
730 params_length;
731 adm_params = kzalloc(sz, GFP_KERNEL);
732 if (!adm_params) {
733 pr_err("%s, adm params memory alloc failed\n", __func__);
734 return -ENOMEM;
735 }
736
737 memcpy(((u8 *)adm_params +
738 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5)),
739 params, params_length);
740 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
741 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
742 adm_params->hdr.pkt_size = sz;
743 adm_params->hdr.src_svc = APR_SVC_ADM;
744 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
745 adm_params->hdr.src_port = port_id;
746 adm_params->hdr.dest_svc = APR_SVC_ADM;
747 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
748 adm_params->hdr.dest_port = 0; /* Ignored */;
749 adm_params->hdr.token = port_idx << 16 | copp_idx;
750 adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5;
751 adm_params->payload_addr_lsw = 0;
752 adm_params->payload_addr_msw = 0;
753 adm_params->mem_map_handle = 0;
754 adm_params->payload_size = params_length;
755 /* direction RX as 0 */
756 adm_params->direction = ADM_MATRIX_ID_AUDIO_RX;
757 /* session id for this cmd to be applied on */
758 adm_params->sessionid = session_id;
759 adm_params->deviceid =
760 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
761 adm_params->reserved = 0;
762 pr_debug("%s: deviceid %d, session_id %d, src_port %d, dest_port %d\n",
763 __func__, adm_params->deviceid, adm_params->sessionid,
764 adm_params->hdr.src_port, adm_params->hdr.dest_port);
765 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
766 rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
767 if (rc < 0) {
768 pr_err("%s: Set params failed port = 0x%x rc %d\n",
769 __func__, port_id, rc);
770 rc = -EINVAL;
771 goto set_stereo_to_custom_stereo_return;
772 }
773 /* Wait for the callback */
774 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
775 atomic_read(&this_adm.copp.stat
776 [port_idx][copp_idx]) >= 0,
777 msecs_to_jiffies(TIMEOUT_MS));
778 if (!rc) {
779 pr_err("%s: Set params timed out port = 0x%x\n", __func__,
780 port_id);
781 rc = -EINVAL;
782 goto set_stereo_to_custom_stereo_return;
783 } else if (atomic_read(&this_adm.copp.stat
784 [port_idx][copp_idx]) > 0) {
785 pr_err("%s: DSP returned error[%s]\n", __func__,
786 adsp_err_get_err_str(atomic_read(
787 &this_adm.copp.stat
788 [port_idx][copp_idx])));
789 rc = adsp_err_get_lnx_err_code(
790 atomic_read(&this_adm.copp.stat
791 [port_idx][copp_idx]));
792 goto set_stereo_to_custom_stereo_return;
793 }
794 rc = 0;
795set_stereo_to_custom_stereo_return:
796 kfree(adm_params);
797 return rc;
798}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530799EXPORT_SYMBOL(adm_set_stereo_to_custom_stereo);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530800
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800801/*
Bhalchandra Gajareeed46bd2018-05-15 16:48:07 -0700802 * adm_set_custom_chmix_cfg:
803 * Set the custom channel mixer configuration for ADM
804 *
805 * @port_id: Backend port id
806 * @copp_idx: ADM copp index
807 * @session_id: ID of the requesting session
808 * @params: Expected packaged params for channel mixer
809 * @params_length: Length of the params to be set
810 * @direction: RX or TX direction
811 * @stream_type: Audio or Listen stream type
812 */
813int adm_set_custom_chmix_cfg(int port_id, int copp_idx,
814 unsigned int session_id, char *params,
815 uint32_t params_length, int direction,
816 int stream_type)
817{
818 struct adm_cmd_set_pspd_mtmx_strtr_params_v6 *adm_params = NULL;
819 int sz, rc = 0, port_idx;
820
821 port_id = afe_convert_virtual_to_portid(port_id);
822 port_idx = adm_validate_and_get_port_index(port_id);
823 if (port_idx < 0) {
824 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
825 return -EINVAL;
826 }
827
828 sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v6) +
829 params_length;
830 adm_params = kzalloc(sz, GFP_KERNEL);
831 if (!adm_params) {
832 pr_err("%s, adm params memory alloc failed\n", __func__);
833 return -ENOMEM;
834 }
835
836 memcpy(((u8 *)adm_params +
837 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v6)),
838 params, params_length);
839 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
840 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
841 adm_params->hdr.pkt_size = sz;
842 adm_params->hdr.src_svc = APR_SVC_ADM;
843 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
844 adm_params->hdr.src_port = port_id;
845 adm_params->hdr.dest_svc = APR_SVC_ADM;
846 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
847 adm_params->hdr.dest_port = 0; /* Ignored */;
848 adm_params->hdr.token = port_idx << 16 | copp_idx;
849 adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V6;
850 adm_params->payload_addr_lsw = 0;
851 adm_params->payload_addr_msw = 0;
852 adm_params->mem_map_handle = 0;
853 adm_params->payload_size = params_length;
854 adm_params->direction = direction;
855 /* session id for this cmd to be applied on */
856 adm_params->sessionid = session_id;
857 adm_params->deviceid =
858 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
859 /* connecting stream type i.e. lsm or asm */
860 adm_params->stream_type = stream_type;
861 pr_debug("%s: deviceid %d, session_id %d, src_port %d, dest_port %d\n",
862 __func__, adm_params->deviceid, adm_params->sessionid,
863 adm_params->hdr.src_port, adm_params->hdr.dest_port);
864 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
865 rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
866 if (rc < 0) {
867 pr_err("%s: Set params failed port = 0x%x rc %d\n",
868 __func__, port_id, rc);
869 rc = -EINVAL;
870 goto exit;
871 }
872 /* Wait for the callback */
873 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
874 atomic_read(&this_adm.copp.stat
875 [port_idx][copp_idx]),
876 msecs_to_jiffies(TIMEOUT_MS));
877 if (!rc) {
878 pr_err("%s: Set params timed out port = 0x%x\n", __func__,
879 port_id);
880 rc = -EINVAL;
881 goto exit;
882 } else if (atomic_read(&this_adm.copp.stat
883 [port_idx][copp_idx]) > 0) {
884 pr_err("%s: DSP returned error[%s]\n", __func__,
885 adsp_err_get_err_str(atomic_read(
886 &this_adm.copp.stat
887 [port_idx][copp_idx])));
888 rc = adsp_err_get_lnx_err_code(
889 atomic_read(&this_adm.copp.stat
890 [port_idx][copp_idx]));
891 goto exit;
892 }
893
894 rc = 0;
895exit:
896 kfree(adm_params);
897 return rc;
898}
899EXPORT_SYMBOL(adm_set_custom_chmix_cfg);
900
901/*
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800902 * With pre-packed data, only the opcode differes from V5 and V6.
903 * Use q6common_pack_pp_params to pack the data correctly.
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530904 */
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800905int adm_set_pp_params(int port_id, int copp_idx,
906 struct mem_mapping_hdr *mem_hdr, u8 *param_data,
907 u32 param_size)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530908{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800909 struct adm_cmd_set_pp_params *adm_set_params = NULL;
910 int size = 0;
911 int port_idx = 0;
912 atomic_t *copp_stat = NULL;
913 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530914
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530915 port_id = afe_convert_virtual_to_portid(port_id);
916 port_idx = adm_validate_and_get_port_index(port_id);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800917 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
918 pr_err("%s: Invalid port_idx 0x%x\n", __func__, port_idx);
919 return -EINVAL;
920 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
921 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530922 return -EINVAL;
923 }
924
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800925 /* Only add params_size in inband case */
926 size = sizeof(struct adm_cmd_set_pp_params);
927 if (param_data != NULL)
928 size += param_size;
929 adm_set_params = kzalloc(size, GFP_KERNEL);
930 if (!adm_set_params)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530931 return -ENOMEM;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800932
933 adm_set_params->apr_hdr.hdr_field =
934 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
935 APR_PKT_VER);
936 adm_set_params->apr_hdr.pkt_size = size;
937 adm_set_params->apr_hdr.src_svc = APR_SVC_ADM;
938 adm_set_params->apr_hdr.src_domain = APR_DOMAIN_APPS;
939 adm_set_params->apr_hdr.src_port = port_id;
940 adm_set_params->apr_hdr.dest_svc = APR_SVC_ADM;
941 adm_set_params->apr_hdr.dest_domain = APR_DOMAIN_ADSP;
942 adm_set_params->apr_hdr.dest_port =
943 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
944 adm_set_params->apr_hdr.token = port_idx << 16 | copp_idx;
945
946 if (q6common_is_instance_id_supported())
947 adm_set_params->apr_hdr.opcode = ADM_CMD_SET_PP_PARAMS_V6;
948 else
949 adm_set_params->apr_hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
950
951 adm_set_params->payload_size = param_size;
952
953 if (mem_hdr != NULL) {
954 /* Out of Band Case */
955 adm_set_params->mem_hdr = *mem_hdr;
956 } else if (param_data != NULL) {
957 /*
958 * In band case. Parameter data must be pre-packed with its
959 * header before calling this function. Use
960 * q6common_pack_pp_params to pack parameter data and header
961 * correctly.
962 */
963 memcpy(&adm_set_params->param_data, param_data, param_size);
964 } else {
965 pr_err("%s: Received NULL pointers for both memory header and param data\n",
966 __func__);
967 ret = -EINVAL;
968 goto done;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530969 }
970
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800971 copp_stat = &this_adm.copp.stat[port_idx][copp_idx];
972 atomic_set(copp_stat, -1);
973 ret = apr_send_pkt(this_adm.apr, (uint32_t *) adm_set_params);
974 if (ret < 0) {
975 pr_err("%s: Set params APR send failed port = 0x%x ret %d\n",
976 __func__, port_id, ret);
977 goto done;
978 }
979 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
980 atomic_read(copp_stat) >= 0,
981 msecs_to_jiffies(TIMEOUT_MS));
982 if (!ret) {
983 pr_err("%s: Set params timed out port = 0x%x\n", __func__,
984 port_id);
985 ret = -ETIMEDOUT;
986 goto done;
987 }
988 if (atomic_read(copp_stat) > 0) {
989 pr_err("%s: DSP returned error[%s]\n", __func__,
990 adsp_err_get_err_str(atomic_read(copp_stat)));
991 ret = adsp_err_get_lnx_err_code(atomic_read(copp_stat));
992 goto done;
993 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530994
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800995 ret = 0;
996done:
997 kfree(adm_set_params);
998 return ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530999}
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001000EXPORT_SYMBOL(adm_set_pp_params);
1001
1002int adm_pack_and_set_one_pp_param(int port_id, int copp_idx,
1003 struct param_hdr_v3 param_hdr, u8 *param_data)
1004{
1005 u8 *packed_data = NULL;
1006 u32 total_size = 0;
1007 int ret = 0;
1008
1009 total_size = sizeof(union param_hdrs) + param_hdr.param_size;
1010 packed_data = kzalloc(total_size, GFP_KERNEL);
1011 if (!packed_data)
1012 return -ENOMEM;
1013
1014 ret = q6common_pack_pp_params(packed_data, &param_hdr, param_data,
1015 &total_size);
1016 if (ret) {
1017 pr_err("%s: Failed to pack parameter data, error %d\n",
1018 __func__, ret);
1019 goto done;
1020 }
1021
1022 ret = adm_set_pp_params(port_id, copp_idx, NULL, packed_data,
1023 total_size);
1024 if (ret)
1025 pr_err("%s: Failed to set parameter data, error %d\n", __func__,
1026 ret);
1027done:
1028 kfree(packed_data);
1029 return ret;
1030}
1031EXPORT_SYMBOL(adm_pack_and_set_one_pp_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301032
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001033/*
1034 * Only one parameter can be requested at a time. Therefore, packing and sending
1035 * the request can be handled locally.
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301036 */
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001037int adm_get_pp_params(int port_id, int copp_idx, uint32_t client_id,
1038 struct mem_mapping_hdr *mem_hdr,
1039 struct param_hdr_v3 *param_hdr, u8 *returned_param_data)
1040{
1041 struct adm_cmd_get_pp_params adm_get_params;
1042 int total_size = 0;
1043 int get_param_array_sz = ARRAY_SIZE(adm_get_parameters);
1044 int returned_param_size = 0;
1045 int returned_param_size_in_bytes = 0;
1046 int port_idx = 0;
1047 int idx = 0;
1048 atomic_t *copp_stat = NULL;
1049 int ret = 0;
1050
1051 if (param_hdr == NULL) {
1052 pr_err("%s: Received NULL pointer for parameter header\n",
1053 __func__);
1054 return -EINVAL;
1055 }
1056
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 || port_idx >= AFE_MAX_PORTS) {
1060 pr_err("%s: Invalid port_idx 0x%x\n", __func__, port_idx);
1061 return -EINVAL;
1062 }
1063 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
1064 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
1065 return -EINVAL;
1066 }
1067
1068 memset(&adm_get_params, 0, sizeof(adm_get_params));
1069
1070 if (mem_hdr != NULL)
1071 adm_get_params.mem_hdr = *mem_hdr;
1072
1073 q6common_pack_pp_params((u8 *) &adm_get_params.param_hdr, param_hdr,
1074 NULL, &total_size);
1075
1076 /* Pack APR header after filling body so total_size has correct value */
Vignesh Kulothunganaadecdf2018-06-20 15:08:24 -07001077 adm_get_params.apr_hdr.hdr_field =
1078 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
1079 APR_PKT_VER);
1080 adm_get_params.apr_hdr.pkt_size = sizeof(adm_get_params);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001081 adm_get_params.apr_hdr.src_svc = APR_SVC_ADM;
1082 adm_get_params.apr_hdr.src_domain = APR_DOMAIN_APPS;
1083 adm_get_params.apr_hdr.src_port = port_id;
1084 adm_get_params.apr_hdr.dest_svc = APR_SVC_ADM;
1085 adm_get_params.apr_hdr.dest_domain = APR_DOMAIN_ADSP;
1086 adm_get_params.apr_hdr.dest_port =
1087 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
1088 adm_get_params.apr_hdr.token =
1089 port_idx << 16 | client_id << 8 | copp_idx;
1090
1091 if (q6common_is_instance_id_supported())
1092 adm_get_params.apr_hdr.opcode = ADM_CMD_GET_PP_PARAMS_V6;
1093 else
1094 adm_get_params.apr_hdr.opcode = ADM_CMD_GET_PP_PARAMS_V5;
1095
1096 copp_stat = &this_adm.copp.stat[port_idx][copp_idx];
1097 atomic_set(copp_stat, -1);
Vignesh Kulothunganaadecdf2018-06-20 15:08:24 -07001098
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001099 ret = apr_send_pkt(this_adm.apr, (uint32_t *) &adm_get_params);
Vidyakumar Athotae427c412018-06-16 00:02:46 -07001100 if (ret < 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001101 pr_err("%s: Get params APR send failed port = 0x%x ret %d\n",
1102 __func__, port_id, ret);
1103 ret = -EINVAL;
1104 goto done;
1105 }
1106 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
1107 atomic_read(copp_stat) >= 0,
1108 msecs_to_jiffies(TIMEOUT_MS));
1109 if (!ret) {
1110 pr_err("%s: Get params timed out port = 0x%x\n", __func__,
1111 port_id);
1112 ret = -ETIMEDOUT;
1113 goto done;
1114 }
1115 if (atomic_read(copp_stat) > 0) {
1116 pr_err("%s: DSP returned error[%s]\n", __func__,
1117 adsp_err_get_err_str(atomic_read(copp_stat)));
1118 ret = adsp_err_get_lnx_err_code(atomic_read(copp_stat));
1119 goto done;
1120 }
1121
1122 ret = 0;
1123
1124 /* Copy data to caller if sent in band */
1125 if (!returned_param_data) {
1126 pr_debug("%s: Received NULL pointer for param destination, not copying payload\n",
1127 __func__);
1128 return 0;
1129 }
1130
1131 idx = ADM_GET_PARAMETER_LENGTH * copp_idx;
1132 returned_param_size = adm_get_parameters[idx];
1133 if (returned_param_size < 0 ||
1134 returned_param_size + idx + 1 > get_param_array_sz) {
1135 pr_err("%s: Invalid parameter size %d\n", __func__,
1136 returned_param_size);
1137 return -EINVAL;
1138 }
1139
1140 returned_param_size_in_bytes = returned_param_size * sizeof(uint32_t);
1141 if (param_hdr->param_size < returned_param_size_in_bytes) {
1142 pr_err("%s: Provided buffer is not big enough, provided buffer size(%d) size needed(%d)\n",
1143 __func__, param_hdr->param_size,
1144 returned_param_size_in_bytes);
1145 return -EINVAL;
1146 }
1147
1148 memcpy(returned_param_data, &adm_get_parameters[idx + 1],
1149 returned_param_size_in_bytes);
1150done:
1151 return ret;
1152}
1153EXPORT_SYMBOL(adm_get_pp_params);
1154
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001155int adm_get_pp_topo_module_list_v2(int port_id, int copp_idx,
1156 int32_t param_length,
1157 int32_t *returned_params)
1158{
1159 struct adm_cmd_get_pp_topo_module_list adm_get_module_list;
1160 bool iid_supported = q6common_is_instance_id_supported();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301161 int *topo_list;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001162 int num_modules = 0;
1163 int list_size = 0;
1164 int port_idx, idx;
1165 int i = 0;
1166 atomic_t *copp_stat = NULL;
1167 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301168
1169 pr_debug("%s : port_id %x", __func__, port_id);
1170 port_id = afe_convert_virtual_to_portid(port_id);
1171 port_idx = adm_validate_and_get_port_index(port_id);
1172 if (port_idx < 0) {
1173 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
1174 return -EINVAL;
1175 }
1176
1177 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
1178 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
1179 return -EINVAL;
1180 }
1181
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001182 memset(&adm_get_module_list, 0, sizeof(adm_get_module_list));
1183
1184 adm_get_module_list.apr_hdr.pkt_size = sizeof(adm_get_module_list);
1185 adm_get_module_list.apr_hdr.src_svc = APR_SVC_ADM;
1186 adm_get_module_list.apr_hdr.src_domain = APR_DOMAIN_APPS;
1187 adm_get_module_list.apr_hdr.src_port = port_id;
1188 adm_get_module_list.apr_hdr.dest_svc = APR_SVC_ADM;
1189 adm_get_module_list.apr_hdr.dest_domain = APR_DOMAIN_ADSP;
1190 adm_get_module_list.apr_hdr.dest_port =
1191 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
1192 adm_get_module_list.apr_hdr.token = port_idx << 16 | copp_idx;
1193 /*
1194 * Out of band functionality is not currently utilized.
1195 * Assume in band.
1196 */
1197 if (iid_supported) {
1198 adm_get_module_list.apr_hdr.opcode =
1199 ADM_CMD_GET_PP_TOPO_MODULE_LIST_V2;
1200 adm_get_module_list.param_max_size = param_length;
1201 } else {
1202 adm_get_module_list.apr_hdr.opcode =
1203 ADM_CMD_GET_PP_TOPO_MODULE_LIST;
1204
1205 if (param_length > U16_MAX) {
1206 pr_err("%s: Invalid param length for V1 %d\n", __func__,
1207 param_length);
1208 return -EINVAL;
1209 }
1210 adm_get_module_list.param_max_size = param_length << 16;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301211 }
1212
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001213 copp_stat = &this_adm.copp.stat[port_idx][copp_idx];
1214 atomic_set(copp_stat, -1);
1215 ret = apr_send_pkt(this_adm.apr, (uint32_t *) &adm_get_module_list);
Vidyakumar Athotae427c412018-06-16 00:02:46 -07001216 if (ret < 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001217 pr_err("%s: APR send pkt failed for port_id: 0x%x failed ret %d\n",
1218 __func__, port_id, ret);
1219 ret = -EINVAL;
1220 goto done;
1221 }
1222 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
1223 atomic_read(copp_stat) >= 0,
1224 msecs_to_jiffies(TIMEOUT_MS));
1225 if (!ret) {
1226 pr_err("%s: Timeout for port_id: 0x%x\n", __func__, port_id);
1227 ret = -ETIMEDOUT;
1228 goto done;
1229 }
1230 if (atomic_read(copp_stat) > 0) {
1231 pr_err("%s: DSP returned error[%s]\n", __func__,
1232 adsp_err_get_err_str(atomic_read(copp_stat)));
1233 ret = adsp_err_get_lnx_err_code(atomic_read(copp_stat));
1234 goto done;
1235 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301236
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001237 ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301238
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001239 if (returned_params) {
1240 /*
1241 * When processing ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST IID is
1242 * added since it is not present. Therefore, there is no need to
1243 * do anything different if IID is not supported here as it is
1244 * already taken care of.
1245 */
1246 idx = ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH * copp_idx;
1247 num_modules = adm_module_topo_list[idx];
1248 if (num_modules < 0 || num_modules > MAX_MODULES_IN_TOPO) {
1249 pr_err("%s: Invalid number of modules returned %d\n",
1250 __func__, num_modules);
1251 return -EINVAL;
1252 }
1253
1254 list_size = num_modules * sizeof(struct module_instance_info);
1255 if (param_length < list_size) {
1256 pr_err("%s: Provided buffer not big enough to hold module-instance list, provided size %d, needed size %d\n",
1257 __func__, param_length, list_size);
1258 return -EINVAL;
1259 }
1260
1261 topo_list = (int32_t *) (&adm_module_topo_list[idx]);
1262 memcpy(returned_params, topo_list, list_size);
1263 for (i = 1; i <= num_modules; i += 2) {
1264 pr_debug("module = 0x%x instance = 0x%x\n",
1265 returned_params[i], returned_params[i + 1]);
1266 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301267 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001268done:
1269 return ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301270}
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001271EXPORT_SYMBOL(adm_get_pp_topo_module_list_v2);
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301272
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301273static void adm_callback_debug_print(struct apr_client_data *data)
1274{
1275 uint32_t *payload;
1276
1277 payload = data->payload;
1278
1279 if (data->payload_size >= 8)
1280 pr_debug("%s: code = 0x%x PL#0[0x%x], PL#1[0x%x], size = %d\n",
1281 __func__, data->opcode, payload[0], payload[1],
1282 data->payload_size);
1283 else if (data->payload_size >= 4)
1284 pr_debug("%s: code = 0x%x PL#0[0x%x], size = %d\n",
1285 __func__, data->opcode, payload[0],
1286 data->payload_size);
1287 else
1288 pr_debug("%s: code = 0x%x, size = %d\n",
1289 __func__, data->opcode, data->payload_size);
1290}
1291
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301292/**
1293 * adm_set_multi_ch_map -
1294 * Update multi channel map info
1295 *
1296 * @channel_map: pointer with channel map info
1297 * @path: direction or ADM path type
1298 *
1299 * Returns 0 on success or error on failure
1300 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301301int adm_set_multi_ch_map(char *channel_map, int path)
1302{
1303 int idx;
1304
1305 if (path == ADM_PATH_PLAYBACK) {
1306 idx = ADM_MCH_MAP_IDX_PLAYBACK;
1307 } else if (path == ADM_PATH_LIVE_REC) {
1308 idx = ADM_MCH_MAP_IDX_REC;
1309 } else {
1310 pr_err("%s: invalid attempt to set path %d\n", __func__, path);
1311 return -EINVAL;
1312 }
1313
1314 memcpy(multi_ch_maps[idx].channel_mapping, channel_map,
Dieter Luecking50c55352018-09-28 14:29:17 +02001315 PCM_FORMAT_MAX_NUM_CHANNEL_V8);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301316 multi_ch_maps[idx].set_channel_map = true;
1317
1318 return 0;
1319}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301320EXPORT_SYMBOL(adm_set_multi_ch_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301321
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301322/**
1323 * adm_get_multi_ch_map -
1324 * Retrieves multi channel map info
1325 *
1326 * @channel_map: pointer to be updated with channel map
1327 * @path: direction or ADM path type
1328 *
1329 * Returns 0 on success or error on failure
1330 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301331int adm_get_multi_ch_map(char *channel_map, int path)
1332{
1333 int idx;
1334
1335 if (path == ADM_PATH_PLAYBACK) {
1336 idx = ADM_MCH_MAP_IDX_PLAYBACK;
1337 } else if (path == ADM_PATH_LIVE_REC) {
1338 idx = ADM_MCH_MAP_IDX_REC;
1339 } else {
1340 pr_err("%s: invalid attempt to get path %d\n", __func__, path);
1341 return -EINVAL;
1342 }
1343
1344 if (multi_ch_maps[idx].set_channel_map) {
1345 memcpy(channel_map, multi_ch_maps[idx].channel_mapping,
Dieter Luecking50c55352018-09-28 14:29:17 +02001346 PCM_FORMAT_MAX_NUM_CHANNEL_V8);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301347 }
1348
1349 return 0;
1350}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301351EXPORT_SYMBOL(adm_get_multi_ch_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301352
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001353static int adm_process_get_param_response(u32 opcode, u32 idx, u32 *payload,
1354 u32 payload_size)
1355{
1356 struct adm_cmd_rsp_get_pp_params_v5 *v5_rsp = NULL;
1357 struct adm_cmd_rsp_get_pp_params_v6 *v6_rsp = NULL;
1358 u32 *param_data = NULL;
1359 int data_size = 0;
1360 int struct_size = 0;
1361
1362 if (payload == NULL) {
1363 pr_err("%s: Payload is NULL\n", __func__);
1364 return -EINVAL;
1365 }
1366
1367 switch (opcode) {
1368 case ADM_CMDRSP_GET_PP_PARAMS_V5:
1369 struct_size = sizeof(struct adm_cmd_rsp_get_pp_params_v5);
1370 v5_rsp = (struct adm_cmd_rsp_get_pp_params_v5 *) payload;
1371 data_size = v5_rsp->param_hdr.param_size;
1372 param_data = v5_rsp->param_data;
1373 break;
1374 case ADM_CMDRSP_GET_PP_PARAMS_V6:
1375 struct_size = sizeof(struct adm_cmd_rsp_get_pp_params_v6);
1376 v6_rsp = (struct adm_cmd_rsp_get_pp_params_v6 *) payload;
1377 data_size = v6_rsp->param_hdr.param_size;
1378 param_data = v6_rsp->param_data;
1379 break;
1380 default:
1381 pr_err("%s: Invalid opcode %d\n", __func__, opcode);
1382 return -EINVAL;
1383 }
1384
1385 /*
1386 * Just store the returned parameter data, not the header. The calling
1387 * function is expected to know what it asked for. Therefore, there is
1388 * no difference between V5 and V6.
1389 */
1390 if ((payload_size >= struct_size + data_size) &&
1391 (ARRAY_SIZE(adm_get_parameters) > idx) &&
1392 (ARRAY_SIZE(adm_get_parameters) >= idx + 1 + data_size)) {
Vignesh Kulothunganaadecdf2018-06-20 15:08:24 -07001393 pr_debug("%s: Received parameter data in band\n",
1394 __func__);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001395 /*
1396 * data_size is expressed in number of bytes, store in number of
1397 * ints
1398 */
1399 adm_get_parameters[idx] =
1400 data_size / sizeof(*adm_get_parameters);
1401 pr_debug("%s: GET_PP PARAM: received parameter length: 0x%x\n",
1402 __func__, adm_get_parameters[idx]);
1403 /* store params after param_size */
1404 memcpy(&adm_get_parameters[idx + 1], param_data, data_size);
Vignesh Kulothunganaadecdf2018-06-20 15:08:24 -07001405 } else if (payload_size == sizeof(uint32_t)) {
1406 adm_get_parameters[idx] = -1;
1407 pr_debug("%s: Out of band case, setting size to %d\n",
1408 __func__, adm_get_parameters[idx]);
1409 } else {
1410 pr_err("%s: Invalid parameter combination, payload_size %d, idx %d\n",
1411 __func__, payload_size, idx);
1412 return -EINVAL;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001413 }
Vignesh Kulothunganaadecdf2018-06-20 15:08:24 -07001414 return 0;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001415}
1416
1417static int adm_process_get_topo_list_response(u32 opcode, int copp_idx,
1418 u32 num_modules, u32 *payload,
1419 u32 payload_size)
1420{
1421 u32 *fill_list = NULL;
1422 int idx = 0;
1423 int i = 0;
1424 int j = 0;
1425
1426 if (payload == NULL) {
1427 pr_err("%s: Payload is NULL\n", __func__);
1428 return -EINVAL;
1429 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
1430 pr_err("%s: Invalid COPP index %d\n", __func__, copp_idx);
1431 return -EINVAL;
1432 }
1433
1434 idx = ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH * copp_idx;
1435 fill_list = adm_module_topo_list + idx;
1436 *fill_list++ = num_modules;
1437 for (i = 0; i < num_modules; i++) {
1438 if (j > payload_size / sizeof(u32)) {
1439 pr_err("%s: Invalid number of modules specified %d\n",
1440 __func__, num_modules);
1441 return -EINVAL;
1442 }
1443
1444 /* store module ID */
1445 *fill_list++ = payload[j];
1446 j++;
1447
1448 switch (opcode) {
1449 case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST_V2:
1450 /* store instance ID */
1451 *fill_list++ = payload[j];
1452 j++;
1453 break;
1454 case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST:
1455 /* Insert IID 0 when repacking */
1456 *fill_list++ = INSTANCE_ID_0;
1457 break;
1458 default:
1459 pr_err("%s: Invalid opcode %d\n", __func__, opcode);
1460 return -EINVAL;
1461 }
1462 }
1463
1464 return 0;
1465}
1466
Laxminath Kasam30ad7512017-11-28 12:40:22 +05301467static void adm_reset_data(void)
1468{
1469 int i, j;
1470
1471 apr_reset(this_adm.apr);
1472 for (i = 0; i < AFE_MAX_PORTS; i++) {
1473 for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
1474 atomic_set(&this_adm.copp.id[i][j],
1475 RESET_COPP_ID);
1476 atomic_set(&this_adm.copp.cnt[i][j], 0);
1477 atomic_set(
1478 &this_adm.copp.topology[i][j], 0);
1479 atomic_set(&this_adm.copp.mode[i][j],
1480 0);
1481 atomic_set(&this_adm.copp.stat[i][j],
1482 0);
1483 atomic_set(&this_adm.copp.rate[i][j],
1484 0);
1485 atomic_set(
1486 &this_adm.copp.channels[i][j],
1487 0);
1488 atomic_set(
1489 &this_adm.copp.bit_width[i][j], 0);
1490 atomic_set(
1491 &this_adm.copp.app_type[i][j], 0);
1492 atomic_set(
1493 &this_adm.copp.acdb_id[i][j], 0);
1494 this_adm.copp.adm_status[i][j] =
1495 ADM_STATUS_CALIBRATION_REQUIRED;
1496 }
1497 }
1498 this_adm.apr = NULL;
1499 cal_utils_clear_cal_block_q6maps(ADM_MAX_CAL_TYPES,
1500 this_adm.cal_data);
1501 mutex_lock(&this_adm.cal_data
1502 [ADM_CUSTOM_TOP_CAL]->lock);
1503 this_adm.set_custom_topology = 1;
1504 mutex_unlock(&this_adm.cal_data[
1505 ADM_CUSTOM_TOP_CAL]->lock);
1506 rtac_clear_mapping(ADM_RTAC_CAL);
1507 /*
1508 * Free the ION memory and clear the map handles
1509 * for Source Tracking
1510 */
1511 if (this_adm.sourceTrackingData.memmap.paddr != 0) {
1512 msm_audio_ion_free(
1513 this_adm.sourceTrackingData.dma_buf);
1514 this_adm.sourceTrackingData.dma_buf = NULL;
1515 this_adm.sourceTrackingData.memmap.size = 0;
1516 this_adm.sourceTrackingData.memmap.kvaddr =
1517 NULL;
1518 this_adm.sourceTrackingData.memmap.paddr = 0;
1519 this_adm.sourceTrackingData.apr_cmd_status = -1;
1520 atomic_set(&this_adm.mem_map_handles[
1521 ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
1522 }
1523}
1524
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301525static int32_t adm_callback(struct apr_client_data *data, void *priv)
1526{
1527 uint32_t *payload;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001528 int port_idx, copp_idx, idx, client_id;
1529 int num_modules;
1530 int ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301531
1532 if (data == NULL) {
1533 pr_err("%s: data parameter is null\n", __func__);
1534 return -EINVAL;
1535 }
1536
1537 payload = data->payload;
1538
1539 if (data->opcode == RESET_EVENTS) {
1540 pr_debug("%s: Reset event is received: %d %d apr[%pK]\n",
1541 __func__,
1542 data->reset_event, data->reset_proc, this_adm.apr);
Laxminath Kasam30ad7512017-11-28 12:40:22 +05301543 if (this_adm.apr)
1544 adm_reset_data();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301545 return 0;
1546 }
1547
1548 adm_callback_debug_print(data);
1549 if (data->payload_size) {
1550 copp_idx = (data->token) & 0XFF;
1551 port_idx = ((data->token) >> 16) & 0xFF;
1552 client_id = ((data->token) >> 8) & 0xFF;
1553 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
1554 pr_err("%s: Invalid port idx %d token %d\n",
1555 __func__, port_idx, data->token);
1556 return 0;
1557 }
1558 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
1559 pr_err("%s: Invalid copp idx %d token %d\n",
1560 __func__, copp_idx, data->token);
1561 return 0;
1562 }
1563 if (client_id < 0 || client_id >= ADM_CLIENT_ID_MAX) {
1564 pr_err("%s: Invalid client id %d\n", __func__,
1565 client_id);
1566 return 0;
1567 }
1568 if (data->opcode == APR_BASIC_RSP_RESULT) {
1569 pr_debug("%s: APR_BASIC_RSP_RESULT id 0x%x\n",
1570 __func__, payload[0]);
1571 if (payload[1] != 0) {
1572 pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
1573 __func__, payload[0], payload[1]);
1574 }
1575 switch (payload[0]) {
1576 case ADM_CMD_SET_PP_PARAMS_V5:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001577 case ADM_CMD_SET_PP_PARAMS_V6:
1578 pr_debug("%s: ADM_CMD_SET_PP_PARAMS\n",
1579 __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301580 if (client_id == ADM_CLIENT_ID_SOURCE_TRACKING)
1581 this_adm.sourceTrackingData.
1582 apr_cmd_status = payload[1];
1583 else if (rtac_make_adm_callback(payload,
1584 data->payload_size))
1585 break;
1586 /*
1587 * if soft volume is called and already
1588 * interrupted break out of the sequence here
1589 */
1590 case ADM_CMD_DEVICE_OPEN_V5:
1591 case ADM_CMD_DEVICE_CLOSE_V5:
1592 case ADM_CMD_DEVICE_OPEN_V6:
Dieter Luecking50c55352018-09-28 14:29:17 +02001593 case ADM_CMD_DEVICE_OPEN_V8:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301594 pr_debug("%s: Basic callback received, wake up.\n",
1595 __func__);
1596 atomic_set(&this_adm.copp.stat[port_idx]
1597 [copp_idx], payload[1]);
1598 wake_up(
1599 &this_adm.copp.wait[port_idx][copp_idx]);
1600 break;
1601 case ADM_CMD_ADD_TOPOLOGIES:
1602 pr_debug("%s: callback received, ADM_CMD_ADD_TOPOLOGIES.\n",
1603 __func__);
1604 atomic_set(&this_adm.adm_stat, payload[1]);
1605 wake_up(&this_adm.adm_wait);
1606 break;
1607 case ADM_CMD_MATRIX_MAP_ROUTINGS_V5:
1608 case ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5:
1609 pr_debug("%s: Basic callback received, wake up.\n",
1610 __func__);
1611 atomic_set(&this_adm.matrix_map_stat,
1612 payload[1]);
1613 wake_up(&this_adm.matrix_map_wait);
1614 break;
1615 case ADM_CMD_SHARED_MEM_UNMAP_REGIONS:
1616 pr_debug("%s: ADM_CMD_SHARED_MEM_UNMAP_REGIONS\n",
1617 __func__);
1618 atomic_set(&this_adm.adm_stat, payload[1]);
1619 wake_up(&this_adm.adm_wait);
1620 break;
1621 case ADM_CMD_SHARED_MEM_MAP_REGIONS:
1622 pr_debug("%s: ADM_CMD_SHARED_MEM_MAP_REGIONS\n",
1623 __func__);
1624 /* Should only come here if there is an APR */
1625 /* error or malformed APR packet. Otherwise */
1626 /* response will be returned as */
1627 if (payload[1] != 0) {
1628 pr_err("%s: ADM map error, resuming\n",
1629 __func__);
1630 atomic_set(&this_adm.adm_stat,
1631 payload[1]);
1632 wake_up(&this_adm.adm_wait);
1633 }
1634 break;
1635 case ADM_CMD_GET_PP_PARAMS_V5:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001636 case ADM_CMD_GET_PP_PARAMS_V6:
1637 pr_debug("%s: ADM_CMD_GET_PP_PARAMS\n",
1638 __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301639 /* Should only come here if there is an APR */
1640 /* error or malformed APR packet. Otherwise */
1641 /* response will be returned as */
1642 /* ADM_CMDRSP_GET_PP_PARAMS_V5 */
1643 if (client_id ==
1644 ADM_CLIENT_ID_SOURCE_TRACKING) {
1645 this_adm.sourceTrackingData.
1646 apr_cmd_status = payload[1];
1647 if (payload[1] != 0)
1648 pr_err("%s: ADM get param error = %d\n",
1649 __func__, payload[1]);
1650
1651 atomic_set(&this_adm.copp.stat
1652 [port_idx][copp_idx],
1653 payload[1]);
1654 wake_up(&this_adm.copp.wait
1655 [port_idx][copp_idx]);
1656 } else {
1657 if (payload[1] != 0) {
1658 pr_err("%s: ADM get param error = %d, resuming\n",
1659 __func__, payload[1]);
1660
1661 rtac_make_adm_callback(payload,
1662 data->payload_size);
1663 }
1664 }
1665 break;
1666 case ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5:
Bhalchandra Gajareeed46bd2018-05-15 16:48:07 -07001667 case ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V6:
1668 pr_debug("%s:callback received PSPD MTMX, wake up\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301669 __func__);
1670 atomic_set(&this_adm.copp.stat[port_idx]
1671 [copp_idx], payload[1]);
1672 wake_up(
1673 &this_adm.copp.wait[port_idx][copp_idx]);
1674 break;
1675 case ADM_CMD_GET_PP_TOPO_MODULE_LIST:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001676 case ADM_CMD_GET_PP_TOPO_MODULE_LIST_V2:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301677 pr_debug("%s:ADM_CMD_GET_PP_TOPO_MODULE_LIST\n",
1678 __func__);
1679 if (payload[1] != 0)
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001680 pr_err("%s: ADM get topo list error = %d\n",
1681 __func__, payload[1]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301682 break;
1683 default:
1684 pr_err("%s: Unknown Cmd: 0x%x\n", __func__,
1685 payload[0]);
1686 break;
1687 }
1688 return 0;
1689 }
1690
1691 switch (data->opcode) {
1692 case ADM_CMDRSP_DEVICE_OPEN_V5:
Dieter Luecking50c55352018-09-28 14:29:17 +02001693 case ADM_CMDRSP_DEVICE_OPEN_V6:
1694 case ADM_CMDRSP_DEVICE_OPEN_V8: {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301695 struct adm_cmd_rsp_device_open_v5 *open =
1696 (struct adm_cmd_rsp_device_open_v5 *)data->payload;
1697
1698 if (open->copp_id == INVALID_COPP_ID) {
1699 pr_err("%s: invalid coppid rxed %d\n",
1700 __func__, open->copp_id);
1701 atomic_set(&this_adm.copp.stat[port_idx]
1702 [copp_idx], ADSP_EBADPARAM);
1703 wake_up(
1704 &this_adm.copp.wait[port_idx][copp_idx]);
1705 break;
1706 }
1707 atomic_set(&this_adm.copp.stat
1708 [port_idx][copp_idx], payload[0]);
1709 atomic_set(&this_adm.copp.id[port_idx][copp_idx],
1710 open->copp_id);
1711 pr_debug("%s: coppid rxed=%d\n", __func__,
1712 open->copp_id);
1713 wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
1714 }
1715 break;
1716 case ADM_CMDRSP_GET_PP_PARAMS_V5:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001717 case ADM_CMDRSP_GET_PP_PARAMS_V6:
1718 pr_debug("%s: ADM_CMDRSP_GET_PP_PARAMS\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301719 if (client_id == ADM_CLIENT_ID_SOURCE_TRACKING)
1720 this_adm.sourceTrackingData.apr_cmd_status =
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001721 payload[0];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301722 else if (rtac_make_adm_callback(payload,
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001723 data->payload_size))
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301724 break;
1725
1726 idx = ADM_GET_PARAMETER_LENGTH * copp_idx;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001727 if (payload[0] == 0 && data->payload_size > 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001728 ret = adm_process_get_param_response(
1729 data->opcode, idx, payload,
1730 data->payload_size);
1731 if (ret)
1732 pr_err("%s: Failed to process get param response, error %d\n",
1733 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301734 } else {
1735 adm_get_parameters[idx] = -1;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001736 pr_err("%s: ADM_CMDRSP_GET_PP_PARAMS returned error 0x%x\n",
1737 __func__, payload[0]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301738 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001739 atomic_set(&this_adm.copp.stat[port_idx][copp_idx],
1740 payload[0]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301741 wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
1742 break;
1743 case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001744 case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST_V2:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301745 pr_debug("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST\n",
1746 __func__);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001747 num_modules = payload[1];
1748 pr_debug("%s: Num modules %d\n", __func__, num_modules);
1749 if (payload[0]) {
1750 pr_err("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST, error = %d\n",
1751 __func__, payload[0]);
1752 } else if (num_modules > MAX_MODULES_IN_TOPO) {
1753 pr_err("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST invalid num modules received, num modules = %d\n",
1754 __func__, num_modules);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301755 } else {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001756 ret = adm_process_get_topo_list_response(
1757 data->opcode, copp_idx, num_modules,
1758 payload, data->payload_size);
1759 if (ret)
1760 pr_err("%s: Failed to process get topo modules list response, error %d\n",
1761 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301762 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001763 atomic_set(&this_adm.copp.stat[port_idx][copp_idx],
1764 payload[0]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301765 wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
1766 break;
1767 case ADM_CMDRSP_SHARED_MEM_MAP_REGIONS:
1768 pr_debug("%s: ADM_CMDRSP_SHARED_MEM_MAP_REGIONS\n",
1769 __func__);
1770 atomic_set(&this_adm.mem_map_handles[
1771 atomic_read(&this_adm.mem_map_index)],
1772 *payload);
1773 atomic_set(&this_adm.adm_stat, 0);
1774 wake_up(&this_adm.adm_wait);
1775 break;
1776 default:
1777 pr_err("%s: Unknown cmd:0x%x\n", __func__,
1778 data->opcode);
1779 break;
1780 }
1781 }
1782 return 0;
1783}
1784
1785static int adm_memory_map_regions(phys_addr_t *buf_add, uint32_t mempool_id,
1786 uint32_t *bufsz, uint32_t bufcnt)
1787{
1788 struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
1789 struct avs_shared_map_region_payload *mregions = NULL;
1790 void *mmap_region_cmd = NULL;
1791 void *payload = NULL;
1792 int ret = 0;
1793 int i = 0;
1794 int cmd_size = 0;
1795
1796 pr_debug("%s:\n", __func__);
1797 if (this_adm.apr == NULL) {
1798 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
1799 0xFFFFFFFF, &this_adm);
1800 if (this_adm.apr == NULL) {
1801 pr_err("%s: Unable to register ADM\n", __func__);
1802 ret = -ENODEV;
1803 return ret;
1804 }
1805 rtac_set_adm_handle(this_adm.apr);
1806 }
1807
1808 cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
1809 + sizeof(struct avs_shared_map_region_payload)
1810 * bufcnt;
1811
1812 mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
1813 if (!mmap_region_cmd)
1814 return -ENOMEM;
1815
1816 mmap_regions = (struct avs_cmd_shared_mem_map_regions *)mmap_region_cmd;
1817 mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1818 APR_HDR_LEN(APR_HDR_SIZE),
1819 APR_PKT_VER);
1820 mmap_regions->hdr.pkt_size = cmd_size;
1821 mmap_regions->hdr.src_port = 0;
1822
1823 mmap_regions->hdr.dest_port = 0;
1824 mmap_regions->hdr.token = 0;
1825 mmap_regions->hdr.opcode = ADM_CMD_SHARED_MEM_MAP_REGIONS;
1826 mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL & 0x00ff;
1827 mmap_regions->num_regions = bufcnt & 0x00ff;
1828 mmap_regions->property_flag = 0x00;
1829
1830 pr_debug("%s: map_regions->num_regions = %d\n", __func__,
1831 mmap_regions->num_regions);
1832 payload = ((u8 *) mmap_region_cmd +
1833 sizeof(struct avs_cmd_shared_mem_map_regions));
1834 mregions = (struct avs_shared_map_region_payload *)payload;
1835
1836 for (i = 0; i < bufcnt; i++) {
1837 mregions->shm_addr_lsw = lower_32_bits(buf_add[i]);
1838 mregions->shm_addr_msw =
1839 msm_audio_populate_upper_32_bits(buf_add[i]);
1840 mregions->mem_size_bytes = bufsz[i];
1841 ++mregions;
1842 }
1843
1844 atomic_set(&this_adm.adm_stat, -1);
1845 ret = apr_send_pkt(this_adm.apr, (uint32_t *) mmap_region_cmd);
1846 if (ret < 0) {
1847 pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__,
1848 mmap_regions->hdr.opcode, ret);
1849 ret = -EINVAL;
1850 goto fail_cmd;
1851 }
1852
1853 ret = wait_event_timeout(this_adm.adm_wait,
1854 atomic_read(&this_adm.adm_stat) >= 0,
1855 5 * HZ);
1856 if (!ret) {
1857 pr_err("%s: timeout. waited for memory_map\n", __func__);
1858 ret = -EINVAL;
1859 goto fail_cmd;
1860 } else if (atomic_read(&this_adm.adm_stat) > 0) {
1861 pr_err("%s: DSP returned error[%s]\n",
1862 __func__, adsp_err_get_err_str(
1863 atomic_read(&this_adm.adm_stat)));
1864 ret = adsp_err_get_lnx_err_code(
1865 atomic_read(&this_adm.adm_stat));
1866 goto fail_cmd;
1867 }
1868fail_cmd:
1869 kfree(mmap_region_cmd);
1870 return ret;
1871}
1872
1873static int adm_memory_unmap_regions(void)
1874{
1875 struct avs_cmd_shared_mem_unmap_regions unmap_regions;
1876 int ret = 0;
1877
1878 pr_debug("%s:\n", __func__);
1879 if (this_adm.apr == NULL) {
1880 pr_err("%s: APR handle NULL\n", __func__);
1881 return -EINVAL;
1882 }
1883
1884 unmap_regions.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1885 APR_HDR_LEN(APR_HDR_SIZE),
1886 APR_PKT_VER);
1887 unmap_regions.hdr.pkt_size = sizeof(unmap_regions);
1888 unmap_regions.hdr.src_port = 0;
1889 unmap_regions.hdr.dest_port = 0;
1890 unmap_regions.hdr.token = 0;
1891 unmap_regions.hdr.opcode = ADM_CMD_SHARED_MEM_UNMAP_REGIONS;
1892 unmap_regions.mem_map_handle = atomic_read(&this_adm.
1893 mem_map_handles[atomic_read(&this_adm.mem_map_index)]);
1894 atomic_set(&this_adm.adm_stat, -1);
1895 ret = apr_send_pkt(this_adm.apr, (uint32_t *) &unmap_regions);
1896 if (ret < 0) {
1897 pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__,
1898 unmap_regions.hdr.opcode, ret);
1899 ret = -EINVAL;
1900 goto fail_cmd;
1901 }
1902
1903 ret = wait_event_timeout(this_adm.adm_wait,
1904 atomic_read(&this_adm.adm_stat) >= 0,
1905 5 * HZ);
1906 if (!ret) {
1907 pr_err("%s: timeout. waited for memory_unmap\n",
1908 __func__);
1909 ret = -EINVAL;
1910 goto fail_cmd;
1911 } else if (atomic_read(&this_adm.adm_stat) > 0) {
1912 pr_err("%s: DSP returned error[%s]\n",
1913 __func__, adsp_err_get_err_str(
1914 atomic_read(&this_adm.adm_stat)));
1915 ret = adsp_err_get_lnx_err_code(
1916 atomic_read(&this_adm.adm_stat));
1917 goto fail_cmd;
1918 } else {
1919 pr_debug("%s: Unmap handle 0x%x succeeded\n", __func__,
1920 unmap_regions.mem_map_handle);
1921 }
1922fail_cmd:
1923 return ret;
1924}
1925
1926static int remap_cal_data(struct cal_block_data *cal_block, int cal_index)
1927{
1928 int ret = 0;
1929
Banajit Goswami08bb7362017-11-03 22:48:23 -07001930 if (cal_block->map_data.dma_buf == NULL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301931 pr_err("%s: No ION allocation for cal index %d!\n",
1932 __func__, cal_index);
1933 ret = -EINVAL;
1934 goto done;
1935 }
1936
1937 if ((cal_block->map_data.map_size > 0) &&
1938 (cal_block->map_data.q6map_handle == 0)) {
1939 atomic_set(&this_adm.mem_map_index, cal_index);
1940 ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
1941 (uint32_t *)&cal_block->map_data.map_size, 1);
1942 if (ret < 0) {
1943 pr_err("%s: ADM mmap did not work! size = %zd ret %d\n",
1944 __func__,
1945 cal_block->map_data.map_size, ret);
1946 pr_debug("%s: ADM mmap did not work! addr = 0x%pK, size = %zd ret %d\n",
1947 __func__,
1948 &cal_block->cal_data.paddr,
1949 cal_block->map_data.map_size, ret);
1950 goto done;
1951 }
1952 cal_block->map_data.q6map_handle = atomic_read(&this_adm.
1953 mem_map_handles[cal_index]);
1954 }
1955done:
1956 return ret;
1957}
1958
1959static void send_adm_custom_topology(void)
1960{
1961 struct cal_block_data *cal_block = NULL;
1962 struct cmd_set_topologies adm_top;
1963 int cal_index = ADM_CUSTOM_TOP_CAL;
1964 int result;
1965
1966 if (this_adm.cal_data[cal_index] == NULL)
1967 goto done;
1968
1969 mutex_lock(&this_adm.cal_data[cal_index]->lock);
1970 if (!this_adm.set_custom_topology)
1971 goto unlock;
1972 this_adm.set_custom_topology = 0;
1973
1974 cal_block = cal_utils_get_only_cal_block(this_adm.cal_data[cal_index]);
Vikram Panduranga770b8382017-09-27 12:17:36 -07001975 if (cal_block == NULL || cal_utils_is_cal_stale(cal_block))
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301976 goto unlock;
1977
1978 pr_debug("%s: Sending cal_index %d\n", __func__, cal_index);
1979
1980 result = remap_cal_data(cal_block, cal_index);
1981 if (result) {
1982 pr_err("%s: Remap_cal_data failed for cal %d!\n",
1983 __func__, cal_index);
1984 goto unlock;
1985 }
1986 atomic_set(&this_adm.mem_map_index, cal_index);
1987 atomic_set(&this_adm.mem_map_handles[cal_index],
1988 cal_block->map_data.q6map_handle);
1989
1990 if (cal_block->cal_data.size == 0) {
1991 pr_debug("%s: No ADM cal to send\n", __func__);
1992 goto unlock;
1993 }
1994
1995 adm_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1996 APR_HDR_LEN(20), APR_PKT_VER);
1997 adm_top.hdr.pkt_size = sizeof(adm_top);
1998 adm_top.hdr.src_svc = APR_SVC_ADM;
1999 adm_top.hdr.src_domain = APR_DOMAIN_APPS;
2000 adm_top.hdr.src_port = 0;
2001 adm_top.hdr.dest_svc = APR_SVC_ADM;
2002 adm_top.hdr.dest_domain = APR_DOMAIN_ADSP;
2003 adm_top.hdr.dest_port = 0;
2004 adm_top.hdr.token = 0;
2005 adm_top.hdr.opcode = ADM_CMD_ADD_TOPOLOGIES;
2006 adm_top.payload_addr_lsw = lower_32_bits(cal_block->cal_data.paddr);
2007 adm_top.payload_addr_msw = msm_audio_populate_upper_32_bits(
2008 cal_block->cal_data.paddr);
2009 adm_top.mem_map_handle = cal_block->map_data.q6map_handle;
2010 adm_top.payload_size = cal_block->cal_data.size;
2011
2012 atomic_set(&this_adm.adm_stat, -1);
2013 pr_debug("%s: Sending ADM_CMD_ADD_TOPOLOGIES payload = 0x%pK, size = %d\n",
2014 __func__, &cal_block->cal_data.paddr,
2015 adm_top.payload_size);
2016 result = apr_send_pkt(this_adm.apr, (uint32_t *)&adm_top);
2017 if (result < 0) {
2018 pr_err("%s: Set topologies failed payload size = %zd result %d\n",
2019 __func__, cal_block->cal_data.size, result);
2020 goto unlock;
2021 }
2022 /* Wait for the callback */
2023 result = wait_event_timeout(this_adm.adm_wait,
2024 atomic_read(&this_adm.adm_stat) >= 0,
2025 msecs_to_jiffies(TIMEOUT_MS));
2026 if (!result) {
2027 pr_err("%s: Set topologies timed out payload size = %zd\n",
2028 __func__, cal_block->cal_data.size);
2029 goto unlock;
2030 } else if (atomic_read(&this_adm.adm_stat) > 0) {
2031 pr_err("%s: DSP returned error[%s]\n",
2032 __func__, adsp_err_get_err_str(
2033 atomic_read(&this_adm.adm_stat)));
2034 result = adsp_err_get_lnx_err_code(
2035 atomic_read(&this_adm.adm_stat));
2036 goto unlock;
2037 }
2038unlock:
2039 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
2040done:
2041 return;
2042}
2043
2044static int send_adm_cal_block(int port_id, int copp_idx,
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002045 struct cal_block_data *cal_block, int perf_mode)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302046{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002047 struct mem_mapping_hdr mem_hdr;
2048 int payload_size = 0;
2049 int port_idx = 0;
2050 int topology = 0;
2051 int result = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302052
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002053 pr_debug("%s: Port id 0x%x,\n", __func__, port_id);
2054
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302055 if (!cal_block) {
2056 pr_debug("%s: No ADM cal to send for port_id = 0x%x!\n",
2057 __func__, port_id);
2058 result = -EINVAL;
2059 goto done;
2060 }
2061 if (cal_block->cal_data.size <= 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002062 pr_debug("%s: No ADM cal sent for port_id = 0x%x!\n", __func__,
2063 port_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302064 result = -EINVAL;
2065 goto done;
2066 }
2067
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002068 memset(&mem_hdr, 0, sizeof(mem_hdr));
2069 port_id = afe_convert_virtual_to_portid(port_id);
2070 port_idx = adm_validate_and_get_port_index(port_id);
2071 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
2072 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2073 return -EINVAL;
2074 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
2075 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
2076 return -EINVAL;
2077 }
2078
2079 topology = atomic_read(&this_adm.copp.topology[port_idx][copp_idx]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302080 if (perf_mode == LEGACY_PCM_MODE &&
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002081 topology == DS2_ADM_COPP_TOPOLOGY_ID) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302082 pr_err("%s: perf_mode %d, topology 0x%x\n", __func__, perf_mode,
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002083 topology);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302084 goto done;
2085 }
2086
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002087 mem_hdr.data_payload_addr_lsw =
2088 lower_32_bits(cal_block->cal_data.paddr);
2089 mem_hdr.data_payload_addr_msw =
2090 msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
2091 mem_hdr.mem_map_handle = cal_block->map_data.q6map_handle;
2092 payload_size = cal_block->cal_data.size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302093
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002094 adm_set_pp_params(port_id, copp_idx, &mem_hdr, NULL, payload_size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302095
2096done:
2097 return result;
2098}
2099
2100static struct cal_block_data *adm_find_cal_by_path(int cal_index, int path)
2101{
2102 struct list_head *ptr, *next;
2103 struct cal_block_data *cal_block = NULL;
2104 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2105 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2106
2107 pr_debug("%s:\n", __func__);
2108
2109 list_for_each_safe(ptr, next,
2110 &this_adm.cal_data[cal_index]->cal_blocks) {
2111
2112 cal_block = list_entry(ptr,
2113 struct cal_block_data, list);
2114
Vikram Panduranga770b8382017-09-27 12:17:36 -07002115 if (cal_utils_is_cal_stale(cal_block))
2116 continue;
2117
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302118 if (cal_index == ADM_AUDPROC_CAL ||
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002119 cal_index == ADM_LSM_AUDPROC_CAL ||
2120 cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302121 audproc_cal_info = cal_block->cal_info;
2122 if ((audproc_cal_info->path == path) &&
2123 (cal_block->cal_data.size > 0))
2124 return cal_block;
2125 } else if (cal_index == ADM_AUDVOL_CAL) {
2126 audvol_cal_info = cal_block->cal_info;
2127 if ((audvol_cal_info->path == path) &&
2128 (cal_block->cal_data.size > 0))
2129 return cal_block;
2130 }
2131 }
2132 pr_debug("%s: Can't find ADM cal for cal_index %d, path %d\n",
2133 __func__, cal_index, path);
2134 return NULL;
2135}
2136
2137static struct cal_block_data *adm_find_cal_by_app_type(int cal_index, int path,
2138 int app_type)
2139{
2140 struct list_head *ptr, *next;
2141 struct cal_block_data *cal_block = NULL;
2142 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2143 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2144
2145 pr_debug("%s\n", __func__);
2146
2147 list_for_each_safe(ptr, next,
2148 &this_adm.cal_data[cal_index]->cal_blocks) {
2149
2150 cal_block = list_entry(ptr,
2151 struct cal_block_data, list);
2152
Vikram Panduranga770b8382017-09-27 12:17:36 -07002153 if (cal_utils_is_cal_stale(cal_block))
2154 continue;
2155
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302156 if (cal_index == ADM_AUDPROC_CAL ||
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002157 cal_index == ADM_LSM_AUDPROC_CAL ||
2158 cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302159 audproc_cal_info = cal_block->cal_info;
2160 if ((audproc_cal_info->path == path) &&
2161 (audproc_cal_info->app_type == app_type) &&
2162 (cal_block->cal_data.size > 0))
2163 return cal_block;
2164 } else if (cal_index == ADM_AUDVOL_CAL) {
2165 audvol_cal_info = cal_block->cal_info;
2166 if ((audvol_cal_info->path == path) &&
2167 (audvol_cal_info->app_type == app_type) &&
2168 (cal_block->cal_data.size > 0))
2169 return cal_block;
2170 }
2171 }
2172 pr_debug("%s: Can't find ADM cali for cal_index %d, path %d, app %d, defaulting to search by path\n",
2173 __func__, cal_index, path, app_type);
2174 return adm_find_cal_by_path(cal_index, path);
2175}
2176
2177
2178static struct cal_block_data *adm_find_cal(int cal_index, int path,
2179 int app_type, int acdb_id,
2180 int sample_rate)
2181{
2182 struct list_head *ptr, *next;
2183 struct cal_block_data *cal_block = NULL;
2184 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2185 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2186
2187 pr_debug("%s:\n", __func__);
2188
2189 list_for_each_safe(ptr, next,
2190 &this_adm.cal_data[cal_index]->cal_blocks) {
2191
2192 cal_block = list_entry(ptr,
2193 struct cal_block_data, list);
Vikram Panduranga770b8382017-09-27 12:17:36 -07002194 if (cal_utils_is_cal_stale(cal_block))
2195 continue;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302196
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302197 if (cal_index == ADM_AUDPROC_CAL ||
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002198 cal_index == ADM_LSM_AUDPROC_CAL ||
2199 cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302200 audproc_cal_info = cal_block->cal_info;
2201 if ((audproc_cal_info->path == path) &&
2202 (audproc_cal_info->app_type == app_type) &&
2203 (audproc_cal_info->acdb_id == acdb_id) &&
2204 (audproc_cal_info->sample_rate == sample_rate) &&
2205 (cal_block->cal_data.size > 0))
2206 return cal_block;
2207 } else if (cal_index == ADM_AUDVOL_CAL) {
2208 audvol_cal_info = cal_block->cal_info;
2209 if ((audvol_cal_info->path == path) &&
2210 (audvol_cal_info->app_type == app_type) &&
2211 (audvol_cal_info->acdb_id == acdb_id) &&
2212 (cal_block->cal_data.size > 0))
2213 return cal_block;
2214 }
2215 }
2216 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",
2217 __func__, cal_index, path, app_type, acdb_id, sample_rate);
2218 return adm_find_cal_by_app_type(cal_index, path, app_type);
2219}
2220
2221static int adm_remap_and_send_cal_block(int cal_index, int port_id,
2222 int copp_idx, struct cal_block_data *cal_block, int perf_mode,
2223 int app_type, int acdb_id, int sample_rate)
2224{
2225 int ret = 0;
2226
2227 pr_debug("%s: Sending cal_index cal %d\n", __func__, cal_index);
2228 ret = remap_cal_data(cal_block, cal_index);
2229 if (ret) {
2230 pr_err("%s: Remap_cal_data failed for cal %d!\n",
2231 __func__, cal_index);
2232 goto done;
2233 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002234 ret = send_adm_cal_block(port_id, copp_idx, cal_block, perf_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302235 if (ret < 0)
2236 pr_debug("%s: No cal sent for cal_index %d, port_id = 0x%x! ret %d sample_rate %d\n",
2237 __func__, cal_index, port_id, ret, sample_rate);
2238done:
2239 return ret;
2240}
2241
2242static void send_adm_cal_type(int cal_index, int path, int port_id,
2243 int copp_idx, int perf_mode, int app_type,
2244 int acdb_id, int sample_rate)
2245{
2246 struct cal_block_data *cal_block = NULL;
2247 int ret;
2248
2249 pr_debug("%s: cal index %d\n", __func__, cal_index);
2250
2251 if (this_adm.cal_data[cal_index] == NULL) {
2252 pr_debug("%s: cal_index %d not allocated!\n",
2253 __func__, cal_index);
2254 goto done;
2255 }
2256
2257 mutex_lock(&this_adm.cal_data[cal_index]->lock);
2258 cal_block = adm_find_cal(cal_index, path, app_type, acdb_id,
2259 sample_rate);
2260 if (cal_block == NULL)
2261 goto unlock;
2262
2263 ret = adm_remap_and_send_cal_block(cal_index, port_id, copp_idx,
2264 cal_block, perf_mode, app_type, acdb_id, sample_rate);
Vikram Panduranga770b8382017-09-27 12:17:36 -07002265
2266 cal_utils_mark_cal_used(cal_block);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302267unlock:
2268 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
2269done:
2270 return;
2271}
2272
2273static int get_cal_path(int path)
2274{
2275 if (path == 0x1)
2276 return RX_DEVICE;
2277 else
2278 return TX_DEVICE;
2279}
2280
2281static void send_adm_cal(int port_id, int copp_idx, int path, int perf_mode,
Aditya Bavanari5106b562018-01-08 13:16:32 +05302282 int app_type, int acdb_id, int sample_rate,
2283 int passthr_mode)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302284{
2285 pr_debug("%s: port id 0x%x copp_idx %d\n", __func__, port_id, copp_idx);
2286
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002287 if (passthr_mode != LISTEN) {
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302288 send_adm_cal_type(ADM_AUDPROC_CAL, path, port_id, copp_idx,
2289 perf_mode, app_type, acdb_id, sample_rate);
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002290 } else {
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302291 send_adm_cal_type(ADM_LSM_AUDPROC_CAL, path, port_id, copp_idx,
2292 perf_mode, app_type, acdb_id, sample_rate);
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002293
2294 send_adm_cal_type(ADM_LSM_AUDPROC_PERSISTENT_CAL, path,
2295 port_id, copp_idx, perf_mode, app_type,
2296 acdb_id, sample_rate);
2297 }
2298
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302299 send_adm_cal_type(ADM_AUDVOL_CAL, path, port_id, copp_idx, perf_mode,
2300 app_type, acdb_id, sample_rate);
2301}
2302
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302303/**
2304 * adm_connect_afe_port -
2305 * command to send ADM connect AFE port
2306 *
2307 * @mode: value of mode for ADM connect AFE
2308 * @session_id: session active to connect
2309 * @port_id: Port ID number
2310 *
2311 * Returns 0 on success or error on failure
2312 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302313int adm_connect_afe_port(int mode, int session_id, int port_id)
2314{
2315 struct adm_cmd_connect_afe_port_v5 cmd;
2316 int ret = 0;
2317 int port_idx, copp_idx = 0;
2318
2319 pr_debug("%s: port_id: 0x%x session id:%d mode:%d\n", __func__,
2320 port_id, session_id, mode);
2321
2322 port_id = afe_convert_virtual_to_portid(port_id);
2323 port_idx = adm_validate_and_get_port_index(port_id);
2324 if (port_idx < 0) {
2325 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2326 return -EINVAL;
2327 }
2328
2329 if (this_adm.apr == NULL) {
2330 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
2331 0xFFFFFFFF, &this_adm);
2332 if (this_adm.apr == NULL) {
2333 pr_err("%s: Unable to register ADM\n", __func__);
2334 ret = -ENODEV;
2335 return ret;
2336 }
2337 rtac_set_adm_handle(this_adm.apr);
2338 }
2339 pr_debug("%s: Port ID 0x%x, index %d\n", __func__, port_id, port_idx);
2340
2341 cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2342 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
2343 cmd.hdr.pkt_size = sizeof(cmd);
2344 cmd.hdr.src_svc = APR_SVC_ADM;
2345 cmd.hdr.src_domain = APR_DOMAIN_APPS;
2346 cmd.hdr.src_port = port_id;
2347 cmd.hdr.dest_svc = APR_SVC_ADM;
2348 cmd.hdr.dest_domain = APR_DOMAIN_ADSP;
2349 cmd.hdr.dest_port = 0; /* Ignored */
2350 cmd.hdr.token = port_idx << 16 | copp_idx;
2351 cmd.hdr.opcode = ADM_CMD_CONNECT_AFE_PORT_V5;
2352
2353 cmd.mode = mode;
2354 cmd.session_id = session_id;
2355 cmd.afe_port_id = port_id;
2356
2357 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2358 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&cmd);
2359 if (ret < 0) {
2360 pr_err("%s: ADM enable for port_id: 0x%x failed ret %d\n",
2361 __func__, port_id, ret);
2362 ret = -EINVAL;
2363 goto fail_cmd;
2364 }
2365 /* Wait for the callback with copp id */
2366 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
2367 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
2368 msecs_to_jiffies(TIMEOUT_MS));
2369 if (!ret) {
2370 pr_err("%s: ADM connect timedout for port_id: 0x%x\n",
2371 __func__, port_id);
2372 ret = -EINVAL;
2373 goto fail_cmd;
2374 } else if (atomic_read(&this_adm.copp.stat
2375 [port_idx][copp_idx]) > 0) {
2376 pr_err("%s: DSP returned error[%s]\n",
2377 __func__, adsp_err_get_err_str(
2378 atomic_read(&this_adm.copp.stat
2379 [port_idx][copp_idx])));
2380 ret = adsp_err_get_lnx_err_code(
2381 atomic_read(&this_adm.copp.stat
2382 [port_idx][copp_idx]));
2383 goto fail_cmd;
2384 }
2385 atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
2386 return 0;
2387
2388fail_cmd:
2389
2390 return ret;
2391}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302392EXPORT_SYMBOL(adm_connect_afe_port);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302393
2394int adm_arrange_mch_map(struct adm_cmd_device_open_v5 *open, int path,
2395 int channel_mode)
2396{
2397 int rc = 0, idx;
2398
Bhalchandra Gajareeed46bd2018-05-15 16:48:07 -07002399 pr_debug("%s: channel mode %d", __func__, channel_mode);
2400
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302401 memset(open->dev_channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
2402 switch (path) {
2403 case ADM_PATH_PLAYBACK:
2404 idx = ADM_MCH_MAP_IDX_PLAYBACK;
2405 break;
2406 case ADM_PATH_LIVE_REC:
2407 case ADM_PATH_NONLIVE_REC:
2408 idx = ADM_MCH_MAP_IDX_REC;
2409 break;
2410 default:
2411 goto non_mch_path;
2412 };
2413 if ((open->dev_num_channel > 2) && multi_ch_maps[idx].set_channel_map) {
2414 memcpy(open->dev_channel_mapping,
2415 multi_ch_maps[idx].channel_mapping,
2416 PCM_FORMAT_MAX_NUM_CHANNEL);
2417 } else {
2418 if (channel_mode == 1) {
2419 open->dev_channel_mapping[0] = PCM_CHANNEL_FC;
2420 } else if (channel_mode == 2) {
2421 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2422 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2423 } else if (channel_mode == 3) {
2424 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2425 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2426 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2427 } else if (channel_mode == 4) {
2428 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2429 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2430 open->dev_channel_mapping[2] = PCM_CHANNEL_LS;
2431 open->dev_channel_mapping[3] = PCM_CHANNEL_RS;
2432 } else if (channel_mode == 5) {
2433 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2434 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2435 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2436 open->dev_channel_mapping[3] = PCM_CHANNEL_LS;
2437 open->dev_channel_mapping[4] = PCM_CHANNEL_RS;
2438 } else if (channel_mode == 6) {
2439 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2440 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2441 open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2442 open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2443 open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2444 open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2445 } else if (channel_mode == 7) {
2446 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2447 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2448 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2449 open->dev_channel_mapping[3] = PCM_CHANNEL_LFE;
2450 open->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2451 open->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2452 open->dev_channel_mapping[6] = PCM_CHANNEL_CS;
2453 } else if (channel_mode == 8) {
2454 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2455 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2456 open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2457 open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2458 open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2459 open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2460 open->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2461 open->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2462 } else {
2463 pr_err("%s: invalid num_chan %d\n", __func__,
2464 channel_mode);
2465 rc = -EINVAL;
2466 goto inval_ch_mod;
2467 }
2468 }
2469
2470non_mch_path:
2471inval_ch_mod:
2472 return rc;
2473}
2474
2475int adm_arrange_mch_ep2_map(struct adm_cmd_device_open_v6 *open_v6,
2476 int channel_mode)
2477{
2478 int rc = 0;
2479
2480 memset(open_v6->dev_channel_mapping_eid2, 0,
2481 PCM_FORMAT_MAX_NUM_CHANNEL);
2482
2483 if (channel_mode == 1) {
2484 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FC;
2485 } else if (channel_mode == 2) {
2486 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2487 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2488 } else if (channel_mode == 3) {
2489 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2490 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2491 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
2492 } else if (channel_mode == 4) {
2493 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2494 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2495 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LS;
2496 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_RS;
2497 } else if (channel_mode == 5) {
2498 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2499 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2500 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
2501 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_LS;
2502 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_RS;
2503 } else if (channel_mode == 6) {
2504 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2505 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2506 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
2507 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
2508 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
2509 open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
2510 } else if (channel_mode == 8) {
2511 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2512 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2513 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
2514 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
2515 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
2516 open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
2517 open_v6->dev_channel_mapping_eid2[6] = PCM_CHANNEL_LB;
2518 open_v6->dev_channel_mapping_eid2[7] = PCM_CHANNEL_RB;
2519 } else {
2520 pr_err("%s: invalid num_chan %d\n", __func__,
2521 channel_mode);
2522 rc = -EINVAL;
2523 }
2524
2525 return rc;
2526}
2527
Dieter Luecking50c55352018-09-28 14:29:17 +02002528static int adm_arrange_mch_map_v8(
2529 struct adm_device_endpoint_payload *ep_payload,
2530 int path,
2531 int channel_mode)
2532{
2533 int rc = 0, idx;
2534
2535 memset(ep_payload->dev_channel_mapping,
2536 0, PCM_FORMAT_MAX_NUM_CHANNEL_V8);
2537 switch (path) {
2538 case ADM_PATH_PLAYBACK:
2539 idx = ADM_MCH_MAP_IDX_PLAYBACK;
2540 break;
2541 case ADM_PATH_LIVE_REC:
2542 case ADM_PATH_NONLIVE_REC:
2543 idx = ADM_MCH_MAP_IDX_REC;
2544 break;
2545 default:
2546 goto non_mch_path;
2547 };
2548
2549 if ((ep_payload->dev_num_channel > 2) &&
2550 multi_ch_maps[idx].set_channel_map) {
2551 memcpy(ep_payload->dev_channel_mapping,
2552 multi_ch_maps[idx].channel_mapping,
2553 PCM_FORMAT_MAX_NUM_CHANNEL_V8);
2554 } else {
2555 if (channel_mode == 1) {
2556 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FC;
2557 } else if (channel_mode == 2) {
2558 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2559 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2560 } else if (channel_mode == 3) {
2561 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2562 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2563 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2564 } else if (channel_mode == 4) {
2565 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2566 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2567 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LS;
2568 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_RS;
2569 } else if (channel_mode == 5) {
2570 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2571 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2572 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2573 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_LS;
2574 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_RS;
2575 } else if (channel_mode == 6) {
2576 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2577 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2578 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2579 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2580 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2581 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2582 } else if (channel_mode == 7) {
2583 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2584 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2585 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2586 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_LFE;
2587 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2588 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2589 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_CS;
2590 } else if (channel_mode == 8) {
2591 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2592 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2593 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2594 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2595 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2596 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2597 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2598 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2599 } else if (channel_mode == 10) {
2600 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2601 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2602 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2603 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2604 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2605 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2606 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LS;
2607 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RS;
2608 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
2609 ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
2610 } else if (channel_mode == 12) {
2611 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2612 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2613 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2614 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2615 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2616 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2617 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LS;
2618 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RS;
2619 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
2620 ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
2621 ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_TSL;
2622 ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_TSR;
2623 } else if (channel_mode == 16) {
2624 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2625 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2626 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2627 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2628 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2629 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2630 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LS;
2631 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RS;
2632 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
2633 ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
2634 ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_TSL;
2635 ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_TSR;
2636 ep_payload->dev_channel_mapping[12] = PCM_CHANNEL_FLC;
2637 ep_payload->dev_channel_mapping[13] = PCM_CHANNEL_FRC;
2638 ep_payload->dev_channel_mapping[14] = PCM_CHANNEL_RLC;
2639 ep_payload->dev_channel_mapping[15] = PCM_CHANNEL_RRC;
2640 } else {
2641 pr_err("%s: invalid num_chan %d\n", __func__,
2642 channel_mode);
2643 rc = -EINVAL;
2644 goto inval_ch_mod;
2645 }
2646 }
2647
2648non_mch_path:
2649inval_ch_mod:
2650 return rc;
2651}
2652
2653static int adm_arrange_mch_ep2_map_v8(
2654 struct adm_device_endpoint_payload *ep_payload,
2655 int channel_mode)
2656{
2657 int rc = 0;
2658
2659 memset(ep_payload->dev_channel_mapping, 0,
2660 PCM_FORMAT_MAX_NUM_CHANNEL_V8);
2661
2662 if (channel_mode == 1) {
2663 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FC;
2664 } else if (channel_mode == 2) {
2665 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2666 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2667 } else if (channel_mode == 3) {
2668 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2669 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2670 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2671 } else if (channel_mode == 4) {
2672 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2673 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2674 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LS;
2675 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_RS;
2676 } else if (channel_mode == 5) {
2677 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2678 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2679 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2680 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_LS;
2681 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_RS;
2682 } else if (channel_mode == 6) {
2683 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2684 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2685 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2686 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2687 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2688 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2689 } else if (channel_mode == 8) {
2690 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2691 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2692 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2693 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2694 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2695 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2696 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2697 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2698 } else if (channel_mode == 10) {
2699 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2700 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2701 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2702 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2703 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2704 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2705 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2706 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2707 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_CS;
2708 ep_payload->dev_channel_mapping[9] = PCM_CHANNELS;
2709 } else if (channel_mode == 12) {
2710 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2711 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2712 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2713 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2714 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2715 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2716 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2717 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2718 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
2719 ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
2720 ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_TSL;
2721 ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_TSR;
2722 } else if (channel_mode == 16) {
2723 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2724 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2725 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2726 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2727 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2728 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2729 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2730 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2731 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_CS;
2732 ep_payload->dev_channel_mapping[9] = PCM_CHANNELS;
2733 ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_CVH;
2734 ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_MS;
2735 ep_payload->dev_channel_mapping[12] = PCM_CHANNEL_FLC;
2736 ep_payload->dev_channel_mapping[13] = PCM_CHANNEL_FRC;
2737 ep_payload->dev_channel_mapping[14] = PCM_CHANNEL_RLC;
2738 ep_payload->dev_channel_mapping[15] = PCM_CHANNEL_RRC;
2739 } else {
2740 pr_err("%s: invalid num_chan %d\n", __func__,
2741 channel_mode);
2742 rc = -EINVAL;
2743 }
2744
2745 return rc;
2746}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302747/**
2748 * adm_open -
2749 * command to send ADM open
2750 *
2751 * @port_id: port id number
2752 * @path: direction or ADM path type
2753 * @rate: sample rate of session
2754 * @channel_mode: number of channels set
2755 * @topology: topology active for this session
2756 * @perf_mode: performance mode like LL/ULL/..
2757 * @bit_width: bit width to set for copp
2758 * @app_type: App type used for this session
2759 * @acdb_id: ACDB ID of this device
2760 *
2761 * Returns 0 on success or error on failure
2762 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302763int adm_open(int port_id, int path, int rate, int channel_mode, int topology,
2764 int perf_mode, uint16_t bit_width, int app_type, int acdb_id)
2765{
2766 struct adm_cmd_device_open_v5 open;
2767 struct adm_cmd_device_open_v6 open_v6;
Dieter Luecking50c55352018-09-28 14:29:17 +02002768 struct adm_cmd_device_open_v8 open_v8;
2769 struct adm_device_endpoint_payload ep1_payload;
2770 struct adm_device_endpoint_payload ep2_payload;
2771 int ep1_payload_size = 0;
2772 int ep2_payload_size = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302773 int ret = 0;
Asish Bhattacharya34504582017-08-08 12:55:01 +05302774 int port_idx, flags;
2775 int copp_idx = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302776 int tmp_port = q6audio_get_port_id(port_id);
Dieter Luecking50c55352018-09-28 14:29:17 +02002777 void *adm_params = NULL;
2778 int param_size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302779
2780 pr_debug("%s:port %#x path:%d rate:%d mode:%d perf_mode:%d,topo_id %d\n",
2781 __func__, port_id, path, rate, channel_mode, perf_mode,
2782 topology);
2783
2784 port_id = q6audio_convert_virtual_to_portid(port_id);
2785 port_idx = adm_validate_and_get_port_index(port_id);
2786 if (port_idx < 0) {
2787 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2788 return -EINVAL;
2789 }
Dieter Luecking50c55352018-09-28 14:29:17 +02002790 if (channel_mode < 0 || channel_mode > 32) {
2791 pr_err("%s: Invalid channel number 0x%x\n",
2792 __func__, channel_mode);
2793 return -EINVAL;
2794 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302795
2796 if (this_adm.apr == NULL) {
2797 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
2798 0xFFFFFFFF, &this_adm);
2799 if (this_adm.apr == NULL) {
2800 pr_err("%s: Unable to register ADM\n", __func__);
2801 return -ENODEV;
2802 }
2803 rtac_set_adm_handle(this_adm.apr);
2804 }
2805
2806 if (perf_mode == ULL_POST_PROCESSING_PCM_MODE) {
2807 flags = ADM_ULL_POST_PROCESSING_DEVICE_SESSION;
2808 if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
2809 (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
2810 (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
2811 topology = DEFAULT_COPP_TOPOLOGY;
2812 } else if (perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) {
2813 flags = ADM_ULTRA_LOW_LATENCY_DEVICE_SESSION;
2814 topology = NULL_COPP_TOPOLOGY;
2815 rate = ULL_SUPPORTED_SAMPLE_RATE;
2816 bit_width = ULL_SUPPORTED_BITS_PER_SAMPLE;
2817 } else if (perf_mode == LOW_LATENCY_PCM_MODE) {
2818 flags = ADM_LOW_LATENCY_DEVICE_SESSION;
2819 if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
2820 (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
2821 (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
2822 topology = DEFAULT_COPP_TOPOLOGY;
2823 } else {
2824 if ((path == ADM_PATH_COMPRESSED_RX) ||
2825 (path == ADM_PATH_COMPRESSED_TX))
2826 flags = 0;
2827 else
2828 flags = ADM_LEGACY_DEVICE_SESSION;
2829 }
2830
Laxminath Kasam8f7ccc22017-08-28 17:35:04 +05302831 if ((topology == VPM_TX_SM_ECNS_V2_COPP_TOPOLOGY) ||
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302832 (topology == VPM_TX_DM_FLUENCE_COPP_TOPOLOGY) ||
2833 (topology == VPM_TX_DM_RFECNS_COPP_TOPOLOGY))
2834 rate = 16000;
2835
kunleizc0cd19e2018-10-18 17:11:35 +08002836 if (topology == VPM_TX_VOICE_SMECNS_V2_COPP_TOPOLOGY)
2837 channel_mode = 1;
2838
Asish Bhattacharya34504582017-08-08 12:55:01 +05302839 /*
2840 * Routing driver reuses the same adm for streams with the same
2841 * app_type, sample_rate etc.
2842 * This isn't allowed for ULL streams as per the DSP interface
2843 */
2844 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE)
2845 copp_idx = adm_get_idx_if_copp_exists(port_idx, topology,
2846 perf_mode,
2847 rate, bit_width,
2848 app_type);
2849
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302850 if (copp_idx < 0) {
2851 copp_idx = adm_get_next_available_copp(port_idx);
2852 if (copp_idx >= MAX_COPPS_PER_PORT) {
2853 pr_err("%s: exceeded copp id %d\n",
2854 __func__, copp_idx);
2855 return -EINVAL;
2856 }
2857 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
2858 atomic_set(&this_adm.copp.topology[port_idx][copp_idx],
2859 topology);
2860 atomic_set(&this_adm.copp.mode[port_idx][copp_idx],
2861 perf_mode);
2862 atomic_set(&this_adm.copp.rate[port_idx][copp_idx],
2863 rate);
2864 atomic_set(&this_adm.copp.channels[port_idx][copp_idx],
2865 channel_mode);
2866 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx],
2867 bit_width);
2868 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx],
2869 app_type);
2870 atomic_set(&this_adm.copp.acdb_id[port_idx][copp_idx],
2871 acdb_id);
2872 set_bit(ADM_STATUS_CALIBRATION_REQUIRED,
2873 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
2874 if ((path != ADM_PATH_COMPRESSED_RX) &&
2875 (path != ADM_PATH_COMPRESSED_TX))
2876 send_adm_custom_topology();
2877 }
2878
2879 if (this_adm.copp.adm_delay[port_idx][copp_idx] &&
2880 perf_mode == LEGACY_PCM_MODE) {
2881 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
2882 1);
2883 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
2884 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
2885 }
2886
2887 /* Create a COPP if port id are not enabled */
2888 if (atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]) == 0) {
2889 pr_debug("%s: open ADM: port_idx: %d, copp_idx: %d\n", __func__,
2890 port_idx, copp_idx);
Dieter Luecking50c55352018-09-28 14:29:17 +02002891 if ((topology == SRS_TRUMEDIA_TOPOLOGY_ID) &&
2892 perf_mode == LEGACY_PCM_MODE) {
2893 int res;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302894
Dieter Luecking50c55352018-09-28 14:29:17 +02002895 atomic_set(&this_adm.mem_map_index, ADM_SRS_TRUMEDIA);
2896 msm_dts_srs_tm_ion_memmap(&this_adm.outband_memmap);
2897 res = adm_memory_map_regions(
2898 &this_adm.outband_memmap.paddr, 0,
2899 (uint32_t *)&this_adm.outband_memmap.size, 1);
2900 if (res < 0) {
2901 pr_err("%s: SRS adm_memory_map_regions failed! addr = 0x%pK, size = %d\n",
2902 __func__,
2903 (void *)this_adm.outband_memmap.paddr,
2904 (uint32_t)this_adm.outband_memmap.size);
2905 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302906 }
2907
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302908
Mangesh Kunchamwar9f295c72018-10-08 18:20:41 +05302909 if ((q6core_get_avcs_api_version_per_service(
Dieter Luecking50c55352018-09-28 14:29:17 +02002910 APRV2_IDS_SERVICE_ID_ADSP_ADM_V) >=
Mangesh Kunchamwar9f295c72018-10-08 18:20:41 +05302911 ADSP_ADM_API_VERSION_V3) &&
2912 q6core_use_Q6_32ch_support()) {
Dieter Luecking50c55352018-09-28 14:29:17 +02002913 memset(&open_v8, 0, sizeof(open_v8));
2914 memset(&ep1_payload, 0, sizeof(ep1_payload));
2915 memset(&ep2_payload, 0, sizeof(ep2_payload));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302916
Dieter Luecking50c55352018-09-28 14:29:17 +02002917 open_v8.hdr.hdr_field = APR_HDR_FIELD(
2918 APR_MSG_TYPE_SEQ_CMD,
2919 APR_HDR_LEN(APR_HDR_SIZE),
2920 APR_PKT_VER);
2921 open_v8.hdr.src_svc = APR_SVC_ADM;
2922 open_v8.hdr.src_domain = APR_DOMAIN_APPS;
2923 open_v8.hdr.src_port = tmp_port;
2924 open_v8.hdr.dest_svc = APR_SVC_ADM;
2925 open_v8.hdr.dest_domain = APR_DOMAIN_ADSP;
2926 open_v8.hdr.dest_port = tmp_port;
2927 open_v8.hdr.token = port_idx << 16 | copp_idx;
2928 open_v8.hdr.opcode = ADM_CMD_DEVICE_OPEN_V8;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302929
Dieter Luecking50c55352018-09-28 14:29:17 +02002930 if (this_adm.native_mode != 0) {
2931 open_v8.flags = flags |
2932 (this_adm.native_mode << 11);
2933 this_adm.native_mode = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302934 } else {
Dieter Luecking50c55352018-09-28 14:29:17 +02002935 open_v8.flags = flags;
2936 }
2937 open_v8.mode_of_operation = path;
2938 open_v8.endpoint_id_1 = tmp_port;
2939 open_v8.endpoint_id_2 = 0xFFFF;
2940 open_v8.endpoint_id_3 = 0xFFFF;
2941
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302942
Dieter Luecking50c55352018-09-28 14:29:17 +02002943 open_v8.topology_id = topology;
2944 open_v8.reserved = 0;
2945
2946 /* variable endpoint payload */
2947 ep1_payload.dev_num_channel = channel_mode & 0x00FF;
2948 ep1_payload.bit_width = bit_width;
2949 ep1_payload.sample_rate = rate;
2950 ret = adm_arrange_mch_map_v8(&ep1_payload, path,
2951 channel_mode);
2952 if (ret)
2953 return ret;
2954
2955 pr_debug("%s: port_id=0x%x %x %x topology_id=0x%X flags %x ref_ch %x\n",
2956 __func__, open_v8.endpoint_id_1,
2957 open_v8.endpoint_id_2,
2958 open_v8.endpoint_id_3,
2959 open_v8.topology_id,
2960 open_v8.flags,
2961 this_adm.num_ec_ref_rx_chans);
2962
2963 ep1_payload_size = 8 +
2964 roundup(ep1_payload.dev_num_channel, 4);
2965 param_size = sizeof(struct adm_cmd_device_open_v8)
2966 + ep1_payload_size;
2967 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2968
Dieter Luecking27c57722018-10-05 16:45:41 +02002969 if ((this_adm.num_ec_ref_rx_chans != 0)
2970 && (path != ADM_PATH_PLAYBACK)
Dieter Luecking50c55352018-09-28 14:29:17 +02002971 && (open_v8.endpoint_id_2 != 0xFFFF)) {
Dieter Luecking27c57722018-10-05 16:45:41 +02002972 open_v8.endpoint_id_2 = this_adm.ec_ref_rx;
2973 this_adm.ec_ref_rx = -1;
Dieter Luecking50c55352018-09-28 14:29:17 +02002974 ep2_payload.dev_num_channel =
2975 this_adm.num_ec_ref_rx_chans;
2976 this_adm.num_ec_ref_rx_chans = 0;
2977
2978 if (this_adm.ec_ref_rx_bit_width != 0) {
2979 ep2_payload.bit_width =
2980 this_adm.ec_ref_rx_bit_width;
2981 this_adm.ec_ref_rx_bit_width = 0;
2982 } else {
2983 ep2_payload.bit_width = bit_width;
2984 }
2985
2986 if (this_adm.ec_ref_rx_sampling_rate != 0) {
2987 ep2_payload.sample_rate =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302988 this_adm.ec_ref_rx_sampling_rate;
Dieter Luecking50c55352018-09-28 14:29:17 +02002989 this_adm.ec_ref_rx_sampling_rate = 0;
2990 } else {
2991 ep2_payload.sample_rate = rate;
2992 }
2993
2994 pr_debug("%s: adm open_v8 eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
2995 __func__,
2996 ep2_payload.dev_num_channel,
2997 ep2_payload.bit_width,
2998 ep2_payload.sample_rate);
2999
3000 ret = adm_arrange_mch_ep2_map_v8(&ep2_payload,
3001 ep2_payload.dev_num_channel);
3002
3003 if (ret)
3004 return ret;
3005 ep2_payload_size = 8 +
3006 roundup(ep2_payload.dev_num_channel, 4);
3007 param_size += ep2_payload_size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303008 }
3009
Dieter Luecking27c57722018-10-05 16:45:41 +02003010 open_v8.hdr.pkt_size = param_size;
Dieter Luecking50c55352018-09-28 14:29:17 +02003011 adm_params = kzalloc(param_size, GFP_KERNEL);
3012 if (!adm_params)
3013 return -ENOMEM;
Dieter Luecking50c55352018-09-28 14:29:17 +02003014 memcpy(adm_params, &open_v8, sizeof(open_v8));
3015 memcpy(adm_params + sizeof(open_v8),
3016 (void *)&ep1_payload,
3017 ep1_payload_size);
Dieter Luecking27c57722018-10-05 16:45:41 +02003018
3019 if ((this_adm.num_ec_ref_rx_chans != 0)
3020 && (path != ADM_PATH_PLAYBACK)
3021 && (open_v8.endpoint_id_2 != 0xFFFF)) {
3022 memcpy(adm_params + sizeof(open_v8)
3023 + ep1_payload_size,
3024 (void *)&ep2_payload,
3025 ep2_payload_size);
3026 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303027
Dieter Luecking50c55352018-09-28 14:29:17 +02003028 ret = apr_send_pkt(this_adm.apr,
3029 (uint32_t *)adm_params);
3030 if (ret < 0) {
3031 pr_err("%s: port_id: 0x%x for[0x%x] failed %d for open_v8\n",
3032 __func__, tmp_port, port_id, ret);
3033 return -EINVAL;
3034 }
3035 kfree(adm_params);
3036 } else {
3037
3038 open.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3039 APR_HDR_LEN(APR_HDR_SIZE),
3040 APR_PKT_VER);
3041 open.hdr.pkt_size = sizeof(open);
3042 open.hdr.src_svc = APR_SVC_ADM;
3043 open.hdr.src_domain = APR_DOMAIN_APPS;
3044 open.hdr.src_port = tmp_port;
3045 open.hdr.dest_svc = APR_SVC_ADM;
3046 open.hdr.dest_domain = APR_DOMAIN_ADSP;
3047 open.hdr.dest_port = tmp_port;
3048 open.hdr.token = port_idx << 16 | copp_idx;
3049 open.hdr.opcode = ADM_CMD_DEVICE_OPEN_V5;
3050 open.flags = flags;
3051 open.mode_of_operation = path;
3052 open.endpoint_id_1 = tmp_port;
3053 open.endpoint_id_2 = 0xFFFF;
3054
3055 if (this_adm.ec_ref_rx && (path != 1)) {
3056 open.endpoint_id_2 = this_adm.ec_ref_rx;
3057 this_adm.ec_ref_rx = -1;
3058 }
3059
3060 open.topology_id = topology;
3061
3062 open.dev_num_channel = channel_mode & 0x00FF;
3063 open.bit_width = bit_width;
3064 WARN_ON((perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) &&
3065 (rate != ULL_SUPPORTED_SAMPLE_RATE));
3066 open.sample_rate = rate;
3067
3068 ret = adm_arrange_mch_map(&open, path, channel_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303069
3070 if (ret)
3071 return ret;
3072
Dieter Luecking50c55352018-09-28 14:29:17 +02003073 pr_debug("%s: port_id=0x%x rate=%d topology_id=0x%X\n",
3074 __func__, open.endpoint_id_1, open.sample_rate,
3075 open.topology_id);
3076
3077 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3078
3079 if ((this_adm.num_ec_ref_rx_chans != 0) &&
3080 (path != 1) && (open.endpoint_id_2 != 0xFFFF)) {
3081 memset(&open_v6, 0,
3082 sizeof(struct adm_cmd_device_open_v6));
3083 memcpy(&open_v6, &open,
3084 sizeof(struct adm_cmd_device_open_v5));
3085 open_v6.hdr.opcode = ADM_CMD_DEVICE_OPEN_V6;
3086 open_v6.hdr.pkt_size = sizeof(open_v6);
3087 open_v6.dev_num_channel_eid2 =
3088 this_adm.num_ec_ref_rx_chans;
3089 this_adm.num_ec_ref_rx_chans = 0;
3090
3091 if (this_adm.ec_ref_rx_bit_width != 0) {
3092 open_v6.bit_width_eid2 =
3093 this_adm.ec_ref_rx_bit_width;
3094 this_adm.ec_ref_rx_bit_width = 0;
3095 } else {
3096 open_v6.bit_width_eid2 = bit_width;
3097 }
3098
3099 if (this_adm.ec_ref_rx_sampling_rate != 0) {
3100 open_v6.sample_rate_eid2 =
3101 this_adm.ec_ref_rx_sampling_rate;
3102 this_adm.ec_ref_rx_sampling_rate = 0;
3103 } else {
3104 open_v6.sample_rate_eid2 = rate;
3105 }
3106
3107 pr_debug("%s: eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
3108 __func__, open_v6.dev_num_channel_eid2,
3109 open_v6.bit_width_eid2,
3110 open_v6.sample_rate_eid2);
3111
3112 ret = adm_arrange_mch_ep2_map(&open_v6,
3113 open_v6.dev_num_channel_eid2);
3114
3115 if (ret)
3116 return ret;
3117
3118 ret = apr_send_pkt(this_adm.apr,
3119 (uint32_t *)&open_v6);
3120 } else {
3121 ret = apr_send_pkt(this_adm.apr,
3122 (uint32_t *)&open);
3123 }
3124 if (ret < 0) {
3125 pr_err("%s: port_id: 0x%x for[0x%x] failed %d\n",
3126 __func__, tmp_port, port_id, ret);
3127 return -EINVAL;
3128 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303129 }
Dieter Luecking50c55352018-09-28 14:29:17 +02003130
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303131 /* Wait for the callback with copp id */
3132 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3133 atomic_read(&this_adm.copp.stat
3134 [port_idx][copp_idx]) >= 0,
3135 msecs_to_jiffies(TIMEOUT_MS));
3136 if (!ret) {
3137 pr_err("%s: ADM open timedout for port_id: 0x%x for [0x%x]\n",
3138 __func__, tmp_port, port_id);
3139 return -EINVAL;
3140 } else if (atomic_read(&this_adm.copp.stat
3141 [port_idx][copp_idx]) > 0) {
3142 pr_err("%s: DSP returned error[%s]\n",
3143 __func__, adsp_err_get_err_str(
3144 atomic_read(&this_adm.copp.stat
3145 [port_idx][copp_idx])));
3146 return adsp_err_get_lnx_err_code(
3147 atomic_read(&this_adm.copp.stat
3148 [port_idx][copp_idx]));
3149 }
3150 }
3151 atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
3152 return copp_idx;
3153}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303154EXPORT_SYMBOL(adm_open);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303155
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303156/**
3157 * adm_copp_mfc_cfg -
3158 * command to send ADM MFC config
3159 *
3160 * @port_id: Port ID number
3161 * @copp_idx: copp index assigned
3162 * @dst_sample_rate: sink sample rate
3163 *
3164 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303165void adm_copp_mfc_cfg(int port_id, int copp_idx, int dst_sample_rate)
3166{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003167 struct audproc_mfc_param_media_fmt mfc_cfg;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303168 struct adm_cmd_device_open_v5 open;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003169 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303170 int port_idx;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303171 int rc = 0;
3172 int i = 0;
3173
3174 port_id = q6audio_convert_virtual_to_portid(port_id);
3175 port_idx = adm_validate_and_get_port_index(port_id);
3176
3177 if (port_idx < 0) {
3178 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
3179 goto fail_cmd;
3180 }
3181
3182 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3183 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3184 goto fail_cmd;
3185 }
3186
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003187 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
3188 memset(&open, 0, sizeof(open));
3189 memset(&param_hdr, 0, sizeof(param_hdr));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303190
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003191 param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
3192 param_hdr.instance_id = INSTANCE_ID_0;
3193 param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
3194 param_hdr.param_size = sizeof(mfc_cfg);
3195
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303196 mfc_cfg.sampling_rate = dst_sample_rate;
3197 mfc_cfg.bits_per_sample =
3198 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
3199 open.dev_num_channel = mfc_cfg.num_channels =
3200 atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
3201
3202 rc = adm_arrange_mch_map(&open, ADM_PATH_PLAYBACK,
3203 mfc_cfg.num_channels);
3204 if (rc < 0) {
3205 pr_err("%s: unable to get channal map\n", __func__);
3206 goto fail_cmd;
3207 }
3208
3209 for (i = 0; i < mfc_cfg.num_channels; i++)
3210 mfc_cfg.channel_type[i] =
3211 (uint16_t) open.dev_channel_mapping[i];
3212
3213 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3214
3215 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",
3216 __func__, port_idx, copp_idx,
3217 atomic_read(&this_adm.copp.rate[port_idx][copp_idx]),
3218 mfc_cfg.bits_per_sample, mfc_cfg.num_channels,
3219 mfc_cfg.sampling_rate);
3220
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003221 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
3222 (uint8_t *) &mfc_cfg);
3223 if (rc)
3224 pr_err("%s: Failed to set media format configuration data, err %d\n",
3225 __func__, rc);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303226
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303227fail_cmd:
3228 return;
3229}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303230EXPORT_SYMBOL(adm_copp_mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303231
3232static void route_set_opcode_matrix_id(
3233 struct adm_cmd_matrix_map_routings_v5 **route_addr,
3234 int path, uint32_t passthr_mode)
3235{
3236 struct adm_cmd_matrix_map_routings_v5 *route = *route_addr;
3237
3238 switch (path) {
3239 case ADM_PATH_PLAYBACK:
3240 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
3241 route->matrix_id = ADM_MATRIX_ID_AUDIO_RX;
3242 break;
3243 case ADM_PATH_LIVE_REC:
3244 if (passthr_mode == LISTEN) {
3245 route->hdr.opcode =
3246 ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3247 route->matrix_id = ADM_MATRIX_ID_LISTEN_TX;
3248 break;
3249 }
3250 /* fall through to set matrix id for non-listen case */
3251 case ADM_PATH_NONLIVE_REC:
3252 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
3253 route->matrix_id = ADM_MATRIX_ID_AUDIO_TX;
3254 break;
3255 case ADM_PATH_COMPRESSED_RX:
3256 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3257 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_RX;
3258 break;
3259 case ADM_PATH_COMPRESSED_TX:
3260 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3261 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_TX;
3262 break;
3263 default:
3264 pr_err("%s: Wrong path set[%d]\n", __func__, path);
3265 break;
3266 }
3267 pr_debug("%s: opcode 0x%x, matrix id %d\n",
3268 __func__, route->hdr.opcode, route->matrix_id);
3269}
3270
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303271/**
3272 * adm_matrix_map -
3273 * command to send ADM matrix map for ADM copp list
3274 *
3275 * @path: direction or ADM path type
3276 * @payload_map: have info of session id and associated copp_idx/num_copps
3277 * @perf_mode: performance mode like LL/ULL/..
3278 * @passthr_mode: flag to indicate passthrough mode
3279 *
3280 * Returns 0 on success or error on failure
3281 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303282int adm_matrix_map(int path, struct route_payload payload_map, int perf_mode,
3283 uint32_t passthr_mode)
3284{
3285 struct adm_cmd_matrix_map_routings_v5 *route;
3286 struct adm_session_map_node_v5 *node;
3287 uint16_t *copps_list;
3288 int cmd_size = 0;
3289 int ret = 0, i = 0;
3290 void *payload = NULL;
3291 void *matrix_map = NULL;
3292 int port_idx, copp_idx;
3293
3294 /* Assumes port_ids have already been validated during adm_open */
3295 cmd_size = (sizeof(struct adm_cmd_matrix_map_routings_v5) +
3296 sizeof(struct adm_session_map_node_v5) +
3297 (sizeof(uint32_t) * payload_map.num_copps));
3298 matrix_map = kzalloc(cmd_size, GFP_KERNEL);
3299 if (matrix_map == NULL) {
3300 pr_err("%s: Mem alloc failed\n", __func__);
3301 ret = -EINVAL;
3302 return ret;
3303 }
3304 route = (struct adm_cmd_matrix_map_routings_v5 *)matrix_map;
3305
3306 route->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3307 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
3308 route->hdr.pkt_size = cmd_size;
3309 route->hdr.src_svc = 0;
3310 route->hdr.src_domain = APR_DOMAIN_APPS;
3311 route->hdr.src_port = 0; /* Ignored */;
3312 route->hdr.dest_svc = APR_SVC_ADM;
3313 route->hdr.dest_domain = APR_DOMAIN_ADSP;
3314 route->hdr.dest_port = 0; /* Ignored */;
3315 route->hdr.token = 0;
3316 route->num_sessions = 1;
3317 route_set_opcode_matrix_id(&route, path, passthr_mode);
3318
3319 payload = ((u8 *)matrix_map +
3320 sizeof(struct adm_cmd_matrix_map_routings_v5));
3321 node = (struct adm_session_map_node_v5 *)payload;
3322
3323 node->session_id = payload_map.session_id;
3324 node->num_copps = payload_map.num_copps;
3325 payload = (u8 *)node + sizeof(struct adm_session_map_node_v5);
3326 copps_list = (uint16_t *)payload;
3327 for (i = 0; i < payload_map.num_copps; i++) {
3328 port_idx =
3329 adm_validate_and_get_port_index(payload_map.port_id[i]);
3330 if (port_idx < 0) {
3331 pr_err("%s: Invalid port_id 0x%x\n", __func__,
3332 payload_map.port_id[i]);
3333 ret = -EINVAL;
3334 goto fail_cmd;
3335 }
3336 copp_idx = payload_map.copp_idx[i];
3337 copps_list[i] = atomic_read(&this_adm.copp.id[port_idx]
3338 [copp_idx]);
3339 }
3340 atomic_set(&this_adm.matrix_map_stat, -1);
3341
3342 ret = apr_send_pkt(this_adm.apr, (uint32_t *)matrix_map);
3343 if (ret < 0) {
3344 pr_err("%s: routing for syream %d failed ret %d\n",
3345 __func__, payload_map.session_id, ret);
3346 ret = -EINVAL;
3347 goto fail_cmd;
3348 }
3349 ret = wait_event_timeout(this_adm.matrix_map_wait,
3350 atomic_read(&this_adm.matrix_map_stat) >= 0,
3351 msecs_to_jiffies(TIMEOUT_MS));
3352 if (!ret) {
3353 pr_err("%s: routing for syream %d failed\n", __func__,
3354 payload_map.session_id);
3355 ret = -EINVAL;
3356 goto fail_cmd;
3357 } else if (atomic_read(&this_adm.matrix_map_stat) > 0) {
3358 pr_err("%s: DSP returned error[%s]\n", __func__,
3359 adsp_err_get_err_str(atomic_read(
3360 &this_adm.matrix_map_stat)));
3361 ret = adsp_err_get_lnx_err_code(
3362 atomic_read(&this_adm.matrix_map_stat));
3363 goto fail_cmd;
3364 }
3365
3366 if ((perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) &&
3367 (path != ADM_PATH_COMPRESSED_RX)) {
3368 for (i = 0; i < payload_map.num_copps; i++) {
3369 port_idx = afe_get_port_index(payload_map.port_id[i]);
3370 copp_idx = payload_map.copp_idx[i];
3371 if (port_idx < 0 || copp_idx < 0 ||
3372 (copp_idx > MAX_COPPS_PER_PORT - 1)) {
3373 pr_err("%s: Invalid idx port_idx %d copp_idx %d\n",
3374 __func__, port_idx, copp_idx);
3375 continue;
3376 }
3377 rtac_add_adm_device(payload_map.port_id[i],
3378 atomic_read(&this_adm.copp.id
3379 [port_idx][copp_idx]),
3380 get_cal_path(path),
3381 payload_map.session_id,
3382 payload_map.app_type[i],
3383 payload_map.acdb_dev_id[i]);
3384
3385 if (!test_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3386 (void *)&this_adm.copp.adm_status[port_idx]
3387 [copp_idx])) {
3388 pr_debug("%s: adm copp[0x%x][%d] already sent",
3389 __func__, port_idx, copp_idx);
3390 continue;
3391 }
3392 send_adm_cal(payload_map.port_id[i], copp_idx,
3393 get_cal_path(path), perf_mode,
3394 payload_map.app_type[i],
3395 payload_map.acdb_dev_id[i],
Aditya Bavanari5106b562018-01-08 13:16:32 +05303396 payload_map.sample_rate[i],
3397 passthr_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303398 /* ADM COPP calibration is already sent */
3399 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3400 (void *)&this_adm.copp.
3401 adm_status[port_idx][copp_idx]);
3402 pr_debug("%s: copp_id: %d\n", __func__,
3403 atomic_read(&this_adm.copp.id[port_idx]
3404 [copp_idx]));
3405 }
3406 }
3407
3408fail_cmd:
3409 kfree(matrix_map);
3410 return ret;
3411}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303412EXPORT_SYMBOL(adm_matrix_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303413
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303414/**
3415 * adm_ec_ref_rx_id -
3416 * Update EC ref port ID
3417 *
3418 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303419void adm_ec_ref_rx_id(int port_id)
3420{
3421 this_adm.ec_ref_rx = port_id;
3422 pr_debug("%s: ec_ref_rx:%d\n", __func__, this_adm.ec_ref_rx);
3423}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303424EXPORT_SYMBOL(adm_ec_ref_rx_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303425
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303426/**
3427 * adm_num_ec_ref_rx_chans -
3428 * Update EC ref number of channels
3429 *
3430 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303431void adm_num_ec_ref_rx_chans(int num_chans)
3432{
3433 this_adm.num_ec_ref_rx_chans = num_chans;
3434 pr_debug("%s: num_ec_ref_rx_chans:%d\n",
3435 __func__, this_adm.num_ec_ref_rx_chans);
3436}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303437EXPORT_SYMBOL(adm_num_ec_ref_rx_chans);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303438
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303439/**
3440 * adm_ec_ref_rx_bit_width -
3441 * Update EC ref bit_width
3442 *
3443 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303444void adm_ec_ref_rx_bit_width(int bit_width)
3445{
3446 this_adm.ec_ref_rx_bit_width = bit_width;
3447 pr_debug("%s: ec_ref_rx_bit_width:%d\n",
3448 __func__, this_adm.ec_ref_rx_bit_width);
3449}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303450EXPORT_SYMBOL(adm_ec_ref_rx_bit_width);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303451
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303452/**
3453 * adm_ec_ref_rx_sampling_rate -
3454 * Update EC ref sample rate
3455 *
3456 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303457void adm_ec_ref_rx_sampling_rate(int sampling_rate)
3458{
3459 this_adm.ec_ref_rx_sampling_rate = sampling_rate;
3460 pr_debug("%s: ec_ref_rx_sampling_rate:%d\n",
3461 __func__, this_adm.ec_ref_rx_sampling_rate);
3462}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303463EXPORT_SYMBOL(adm_ec_ref_rx_sampling_rate);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303464
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303465/**
Dieter Luecking50c55352018-09-28 14:29:17 +02003466 * adm_set_native_mode -
3467 * Set adm channel native mode.
3468 * If enabled matrix mixer will be
3469 * running in native mode for channel
3470 * configuration for this device session.
3471 *
3472 */
3473void adm_set_native_mode(int mode)
3474{
3475 this_adm.native_mode = mode;
3476 pr_debug("%s: enable native_mode :%d\n",
3477 __func__, this_adm.native_mode);
3478}
3479EXPORT_SYMBOL(adm_set_native_mode);
3480
3481/**
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303482 * adm_close -
3483 * command to close ADM copp
3484 *
3485 * @port_id: Port ID number
3486 * @perf_mode: performance mode like LL/ULL/..
3487 * @copp_idx: copp index assigned
3488 *
3489 * Returns 0 on success or error on failure
3490 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303491int adm_close(int port_id, int perf_mode, int copp_idx)
3492{
3493 struct apr_hdr close;
3494
3495 int ret = 0, port_idx;
3496 int copp_id = RESET_COPP_ID;
3497
3498 pr_debug("%s: port_id=0x%x perf_mode: %d copp_idx: %d\n", __func__,
3499 port_id, perf_mode, copp_idx);
3500
3501 port_id = q6audio_convert_virtual_to_portid(port_id);
3502 port_idx = adm_validate_and_get_port_index(port_id);
3503 if (port_idx < 0) {
3504 pr_err("%s: Invalid port_id 0x%x\n",
3505 __func__, port_id);
3506 return -EINVAL;
3507 }
3508
3509 if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
3510 pr_err("%s: Invalid copp idx: %d\n", __func__, copp_idx);
3511 return -EINVAL;
3512 }
3513
3514 if (this_adm.copp.adm_delay[port_idx][copp_idx] && perf_mode
3515 == LEGACY_PCM_MODE) {
3516 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
3517 1);
3518 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
3519 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
3520 }
3521
3522 atomic_dec(&this_adm.copp.cnt[port_idx][copp_idx]);
3523 if (!(atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]))) {
3524 copp_id = adm_get_copp_id(port_idx, copp_idx);
3525 pr_debug("%s: Closing ADM port_idx:%d copp_idx:%d copp_id:0x%x\n",
3526 __func__, port_idx, copp_idx, copp_id);
3527 if ((!perf_mode) && (this_adm.outband_memmap.paddr != 0) &&
3528 (atomic_read(&this_adm.copp.topology[port_idx][copp_idx]) ==
3529 SRS_TRUMEDIA_TOPOLOGY_ID)) {
3530 atomic_set(&this_adm.mem_map_index,
3531 ADM_SRS_TRUMEDIA);
3532 ret = adm_memory_unmap_regions();
3533 if (ret < 0) {
3534 pr_err("%s: adm mem unmmap err %d",
3535 __func__, ret);
3536 } else {
3537 atomic_set(&this_adm.mem_map_handles
3538 [ADM_SRS_TRUMEDIA], 0);
3539 }
3540 }
3541
3542
3543 if ((afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX) &&
3544 this_adm.sourceTrackingData.memmap.paddr) {
3545 atomic_set(&this_adm.mem_map_index,
3546 ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
3547 ret = adm_memory_unmap_regions();
3548 if (ret < 0) {
3549 pr_err("%s: adm mem unmmap err %d",
3550 __func__, ret);
3551 }
3552 msm_audio_ion_free(
Banajit Goswami08bb7362017-11-03 22:48:23 -07003553 this_adm.sourceTrackingData.dma_buf);
3554 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303555 this_adm.sourceTrackingData.memmap.size = 0;
3556 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
3557 this_adm.sourceTrackingData.memmap.paddr = 0;
3558 this_adm.sourceTrackingData.apr_cmd_status = -1;
3559 atomic_set(&this_adm.mem_map_handles[
3560 ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
3561 }
3562
3563 close.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3564 APR_HDR_LEN(APR_HDR_SIZE),
3565 APR_PKT_VER);
3566 close.pkt_size = sizeof(close);
3567 close.src_svc = APR_SVC_ADM;
3568 close.src_domain = APR_DOMAIN_APPS;
3569 close.src_port = port_id;
3570 close.dest_svc = APR_SVC_ADM;
3571 close.dest_domain = APR_DOMAIN_ADSP;
3572 close.dest_port = copp_id;
3573 close.token = port_idx << 16 | copp_idx;
3574 close.opcode = ADM_CMD_DEVICE_CLOSE_V5;
3575
3576 atomic_set(&this_adm.copp.id[port_idx][copp_idx],
3577 RESET_COPP_ID);
3578 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
3579 atomic_set(&this_adm.copp.topology[port_idx][copp_idx], 0);
3580 atomic_set(&this_adm.copp.mode[port_idx][copp_idx], 0);
3581 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3582 atomic_set(&this_adm.copp.rate[port_idx][copp_idx], 0);
3583 atomic_set(&this_adm.copp.channels[port_idx][copp_idx], 0);
3584 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx], 0);
3585 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx], 0);
3586
3587 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3588 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
3589
3590 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&close);
3591 if (ret < 0) {
3592 pr_err("%s: ADM close failed %d\n", __func__, ret);
3593 return -EINVAL;
3594 }
3595
3596 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3597 atomic_read(&this_adm.copp.stat
3598 [port_idx][copp_idx]) >= 0,
3599 msecs_to_jiffies(TIMEOUT_MS));
3600 if (!ret) {
3601 pr_err("%s: ADM cmd Route timedout for port 0x%x\n",
3602 __func__, port_id);
3603 return -EINVAL;
3604 } else if (atomic_read(&this_adm.copp.stat
3605 [port_idx][copp_idx]) > 0) {
3606 pr_err("%s: DSP returned error[%s]\n",
3607 __func__, adsp_err_get_err_str(
3608 atomic_read(&this_adm.copp.stat
3609 [port_idx][copp_idx])));
3610 return adsp_err_get_lnx_err_code(
3611 atomic_read(&this_adm.copp.stat
3612 [port_idx][copp_idx]));
3613 }
3614 }
3615
3616 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) {
3617 pr_debug("%s: remove adm device from rtac\n", __func__);
3618 rtac_remove_adm_device(port_id, copp_id);
3619 }
3620 return 0;
3621}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303622EXPORT_SYMBOL(adm_close);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303623
3624int send_rtac_audvol_cal(void)
3625{
3626 int ret = 0;
3627 int ret2 = 0;
3628 int i = 0;
3629 int copp_idx, port_idx, acdb_id, app_id, path;
3630 struct cal_block_data *cal_block = NULL;
3631 struct audio_cal_info_audvol *audvol_cal_info = NULL;
3632 struct rtac_adm rtac_adm_data;
3633
3634 mutex_lock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3635
3636 cal_block = cal_utils_get_only_cal_block(
3637 this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]);
Vikram Panduranga770b8382017-09-27 12:17:36 -07003638 if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303639 pr_err("%s: can't find cal block!\n", __func__);
3640 goto unlock;
3641 }
3642
3643 audvol_cal_info = cal_block->cal_info;
3644 if (audvol_cal_info == NULL) {
3645 pr_err("%s: audvol_cal_info is NULL!\n", __func__);
3646 goto unlock;
3647 }
3648
3649 get_rtac_adm_data(&rtac_adm_data);
3650 for (; i < rtac_adm_data.num_of_dev; i++) {
3651
3652 acdb_id = rtac_adm_data.device[i].acdb_dev_id;
3653 if (acdb_id == 0)
3654 acdb_id = audvol_cal_info->acdb_id;
3655
3656 app_id = rtac_adm_data.device[i].app_type;
3657 if (app_id == 0)
3658 app_id = audvol_cal_info->app_type;
3659
3660 path = afe_get_port_type(rtac_adm_data.device[i].afe_port);
3661 if ((acdb_id == audvol_cal_info->acdb_id) &&
3662 (app_id == audvol_cal_info->app_type) &&
3663 (path == audvol_cal_info->path)) {
3664
3665 if (adm_get_indexes_from_copp_id(rtac_adm_data.
3666 device[i].copp, &copp_idx, &port_idx) != 0) {
3667 pr_debug("%s: Copp Id %d is not active\n",
3668 __func__,
3669 rtac_adm_data.device[i].copp);
3670 continue;
3671 }
3672
3673 ret2 = adm_remap_and_send_cal_block(ADM_RTAC_AUDVOL_CAL,
3674 rtac_adm_data.device[i].afe_port,
3675 copp_idx, cal_block,
3676 atomic_read(&this_adm.copp.
3677 mode[port_idx][copp_idx]),
3678 audvol_cal_info->app_type,
3679 audvol_cal_info->acdb_id,
3680 atomic_read(&this_adm.copp.
3681 rate[port_idx][copp_idx]));
3682 if (ret2 < 0) {
3683 pr_debug("%s: remap and send failed for copp Id %d, acdb id %d, app type %d, path %d\n",
3684 __func__, rtac_adm_data.device[i].copp,
3685 audvol_cal_info->acdb_id,
3686 audvol_cal_info->app_type,
3687 audvol_cal_info->path);
3688 ret = ret2;
3689 }
3690 }
3691 }
3692unlock:
3693 mutex_unlock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3694 return ret;
3695}
3696
3697int adm_map_rtac_block(struct rtac_cal_block_data *cal_block)
3698{
3699 int result = 0;
3700
3701 pr_debug("%s:\n", __func__);
3702
3703 if (cal_block == NULL) {
3704 pr_err("%s: cal_block is NULL!\n",
3705 __func__);
3706 result = -EINVAL;
3707 goto done;
3708 }
3709
3710 if (cal_block->cal_data.paddr == 0) {
3711 pr_debug("%s: No address to map!\n",
3712 __func__);
3713 result = -EINVAL;
3714 goto done;
3715 }
3716
3717 if (cal_block->map_data.map_size == 0) {
3718 pr_debug("%s: map size is 0!\n",
3719 __func__);
3720 result = -EINVAL;
3721 goto done;
3722 }
3723
3724 /* valid port ID needed for callback use primary I2S */
3725 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3726 result = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3727 &cal_block->map_data.map_size, 1);
3728 if (result < 0) {
3729 pr_err("%s: RTAC mmap did not work! size = %d result %d\n",
3730 __func__,
3731 cal_block->map_data.map_size, result);
3732 pr_debug("%s: RTAC mmap did not work! addr = 0x%pK, size = %d\n",
3733 __func__,
3734 &cal_block->cal_data.paddr,
3735 cal_block->map_data.map_size);
3736 goto done;
3737 }
3738
3739 cal_block->map_data.map_handle = atomic_read(
3740 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]);
3741done:
3742 return result;
3743}
3744
3745int adm_unmap_rtac_block(uint32_t *mem_map_handle)
3746{
3747 int result = 0;
3748
3749 pr_debug("%s:\n", __func__);
3750
3751 if (mem_map_handle == NULL) {
3752 pr_debug("%s: Map handle is NULL, nothing to unmap\n",
3753 __func__);
3754 goto done;
3755 }
3756
3757 if (*mem_map_handle == 0) {
3758 pr_debug("%s: Map handle is 0, nothing to unmap\n",
3759 __func__);
3760 goto done;
3761 }
3762
3763 if (*mem_map_handle != atomic_read(
3764 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL])) {
3765 pr_err("%s: Map handles do not match! Unmapping RTAC, RTAC map 0x%x, ADM map 0x%x\n",
3766 __func__, *mem_map_handle, atomic_read(
3767 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]));
3768
3769 /* if mismatch use handle passed in to unmap */
3770 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL],
3771 *mem_map_handle);
3772 }
3773
3774 /* valid port ID needed for callback use primary I2S */
3775 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3776 result = adm_memory_unmap_regions();
3777 if (result < 0) {
3778 pr_debug("%s: adm_memory_unmap_regions failed, error %d\n",
3779 __func__, result);
3780 } else {
3781 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL], 0);
3782 *mem_map_handle = 0;
3783 }
3784done:
3785 return result;
3786}
3787
3788static int get_cal_type_index(int32_t cal_type)
3789{
3790 int ret = -EINVAL;
3791
3792 switch (cal_type) {
3793 case ADM_AUDPROC_CAL_TYPE:
3794 ret = ADM_AUDPROC_CAL;
3795 break;
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05303796 case ADM_LSM_AUDPROC_CAL_TYPE:
3797 ret = ADM_LSM_AUDPROC_CAL;
3798 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303799 case ADM_AUDVOL_CAL_TYPE:
3800 ret = ADM_AUDVOL_CAL;
3801 break;
3802 case ADM_CUST_TOPOLOGY_CAL_TYPE:
3803 ret = ADM_CUSTOM_TOP_CAL;
3804 break;
3805 case ADM_RTAC_INFO_CAL_TYPE:
3806 ret = ADM_RTAC_INFO_CAL;
3807 break;
3808 case ADM_RTAC_APR_CAL_TYPE:
3809 ret = ADM_RTAC_APR_CAL;
3810 break;
3811 case ADM_RTAC_AUDVOL_CAL_TYPE:
3812 ret = ADM_RTAC_AUDVOL_CAL;
3813 break;
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07003814 case ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE:
3815 ret = ADM_LSM_AUDPROC_PERSISTENT_CAL;
3816 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303817 default:
3818 pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
3819 }
3820 return ret;
3821}
3822
3823static int adm_alloc_cal(int32_t cal_type, size_t data_size, void *data)
3824{
3825 int ret = 0;
3826 int cal_index;
3827
3828 pr_debug("%s:\n", __func__);
3829
3830 cal_index = get_cal_type_index(cal_type);
3831 if (cal_index < 0) {
3832 pr_err("%s: could not get cal index %d!\n",
3833 __func__, cal_index);
3834 ret = -EINVAL;
3835 goto done;
3836 }
3837
3838 ret = cal_utils_alloc_cal(data_size, data,
3839 this_adm.cal_data[cal_index], 0, NULL);
3840 if (ret < 0) {
3841 pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
3842 __func__, ret, cal_type);
3843 ret = -EINVAL;
3844 goto done;
3845 }
3846done:
3847 return ret;
3848}
3849
3850static int adm_dealloc_cal(int32_t cal_type, size_t data_size, void *data)
3851{
3852 int ret = 0;
3853 int cal_index;
3854
3855 pr_debug("%s:\n", __func__);
3856
3857 cal_index = get_cal_type_index(cal_type);
3858 if (cal_index < 0) {
3859 pr_err("%s: could not get cal index %d!\n",
3860 __func__, cal_index);
3861 ret = -EINVAL;
3862 goto done;
3863 }
3864
3865 ret = cal_utils_dealloc_cal(data_size, data,
3866 this_adm.cal_data[cal_index]);
3867 if (ret < 0) {
3868 pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
3869 __func__, ret, cal_type);
3870 ret = -EINVAL;
3871 goto done;
3872 }
3873done:
3874 return ret;
3875}
3876
3877static int adm_set_cal(int32_t cal_type, size_t data_size, void *data)
3878{
3879 int ret = 0;
3880 int cal_index;
3881
3882 pr_debug("%s:\n", __func__);
3883
3884 cal_index = get_cal_type_index(cal_type);
3885 if (cal_index < 0) {
3886 pr_err("%s: could not get cal index %d!\n",
3887 __func__, cal_index);
3888 ret = -EINVAL;
3889 goto done;
3890 }
3891
3892 ret = cal_utils_set_cal(data_size, data,
3893 this_adm.cal_data[cal_index], 0, NULL);
3894 if (ret < 0) {
3895 pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
3896 __func__, ret, cal_type);
3897 ret = -EINVAL;
3898 goto done;
3899 }
3900
3901 if (cal_index == ADM_CUSTOM_TOP_CAL) {
3902 mutex_lock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3903 this_adm.set_custom_topology = 1;
3904 mutex_unlock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3905 } else if (cal_index == ADM_RTAC_AUDVOL_CAL) {
3906 send_rtac_audvol_cal();
3907 }
3908done:
3909 return ret;
3910}
3911
3912static int adm_map_cal_data(int32_t cal_type,
3913 struct cal_block_data *cal_block)
3914{
3915 int ret = 0;
3916 int cal_index;
3917
3918 pr_debug("%s:\n", __func__);
3919
3920 cal_index = get_cal_type_index(cal_type);
3921 if (cal_index < 0) {
3922 pr_err("%s: could not get cal index %d!\n",
3923 __func__, cal_index);
3924 ret = -EINVAL;
3925 goto done;
3926 }
3927
3928 atomic_set(&this_adm.mem_map_index, cal_index);
3929 ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3930 (uint32_t *)&cal_block->map_data.map_size, 1);
3931 if (ret < 0) {
3932 pr_err("%s: map did not work! cal_type %i ret %d\n",
3933 __func__, cal_index, ret);
3934 ret = -ENODEV;
3935 goto done;
3936 }
3937 cal_block->map_data.q6map_handle = atomic_read(&this_adm.
3938 mem_map_handles[cal_index]);
3939done:
3940 return ret;
3941}
3942
3943static int adm_unmap_cal_data(int32_t cal_type,
3944 struct cal_block_data *cal_block)
3945{
3946 int ret = 0;
3947 int cal_index;
3948
3949 pr_debug("%s:\n", __func__);
3950
3951 cal_index = get_cal_type_index(cal_type);
3952 if (cal_index < 0) {
3953 pr_err("%s: could not get cal index %d!\n",
3954 __func__, cal_index);
3955 ret = -EINVAL;
3956 goto done;
3957 }
3958
3959 if (cal_block == NULL) {
3960 pr_err("%s: Cal block is NULL!\n",
3961 __func__);
3962 goto done;
3963 }
3964
3965 if (cal_block->map_data.q6map_handle == 0) {
3966 pr_err("%s: Map handle is NULL, nothing to unmap\n",
3967 __func__);
3968 goto done;
3969 }
3970
3971 atomic_set(&this_adm.mem_map_handles[cal_index],
3972 cal_block->map_data.q6map_handle);
3973 atomic_set(&this_adm.mem_map_index, cal_index);
3974 ret = adm_memory_unmap_regions();
3975 if (ret < 0) {
3976 pr_err("%s: unmap did not work! cal_type %i ret %d\n",
3977 __func__, cal_index, ret);
3978 ret = -ENODEV;
3979 goto done;
3980 }
3981 cal_block->map_data.q6map_handle = 0;
3982done:
3983 return ret;
3984}
3985
3986static void adm_delete_cal_data(void)
3987{
3988 pr_debug("%s:\n", __func__);
3989
3990 cal_utils_destroy_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data);
3991}
3992
3993static int adm_init_cal_data(void)
3994{
3995 int ret = 0;
3996 struct cal_type_info cal_type_info[] = {
3997 {{ADM_CUST_TOPOLOGY_CAL_TYPE,
3998 {adm_alloc_cal, adm_dealloc_cal, NULL,
3999 adm_set_cal, NULL, NULL} },
4000 {adm_map_cal_data, adm_unmap_cal_data,
4001 cal_utils_match_buf_num} },
4002
4003 {{ADM_AUDPROC_CAL_TYPE,
4004 {adm_alloc_cal, adm_dealloc_cal, NULL,
4005 adm_set_cal, NULL, NULL} },
4006 {adm_map_cal_data, adm_unmap_cal_data,
4007 cal_utils_match_buf_num} },
4008
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05304009 {{ADM_LSM_AUDPROC_CAL_TYPE,
4010 {adm_alloc_cal, adm_dealloc_cal, NULL,
4011 adm_set_cal, NULL, NULL} },
4012 {adm_map_cal_data, adm_unmap_cal_data,
4013 cal_utils_match_buf_num} },
4014
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304015 {{ADM_AUDVOL_CAL_TYPE,
4016 {adm_alloc_cal, adm_dealloc_cal, NULL,
4017 adm_set_cal, NULL, NULL} },
4018 {adm_map_cal_data, adm_unmap_cal_data,
4019 cal_utils_match_buf_num} },
4020
4021 {{ADM_RTAC_INFO_CAL_TYPE,
4022 {NULL, NULL, NULL, NULL, NULL, NULL} },
4023 {NULL, NULL, cal_utils_match_buf_num} },
4024
4025 {{ADM_RTAC_APR_CAL_TYPE,
4026 {NULL, NULL, NULL, NULL, NULL, NULL} },
4027 {NULL, NULL, cal_utils_match_buf_num} },
4028
4029 {{SRS_TRUMEDIA_CAL_TYPE,
4030 {NULL, NULL, NULL, NULL, NULL, NULL} },
4031 {NULL, NULL, cal_utils_match_buf_num} },
4032
4033 {{ADM_RTAC_AUDVOL_CAL_TYPE,
4034 {adm_alloc_cal, adm_dealloc_cal, NULL,
4035 adm_set_cal, NULL, NULL} },
4036 {adm_map_cal_data, adm_unmap_cal_data,
4037 cal_utils_match_buf_num} },
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07004038
4039 {{ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE,
4040 {adm_alloc_cal, adm_dealloc_cal, NULL,
4041 adm_set_cal, NULL, NULL} },
4042 {adm_map_cal_data, adm_unmap_cal_data,
4043 cal_utils_match_buf_num} },
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304044 };
4045 pr_debug("%s:\n", __func__);
4046
4047 ret = cal_utils_create_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data,
4048 cal_type_info);
4049 if (ret < 0) {
4050 pr_err("%s: could not create cal type! ret %d\n",
4051 __func__, ret);
4052 ret = -EINVAL;
4053 goto err;
4054 }
4055
4056 return ret;
4057err:
4058 adm_delete_cal_data();
4059 return ret;
4060}
4061
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304062/**
4063 * adm_set_volume -
4064 * command to set volume on ADM copp
4065 *
4066 * @port_id: Port ID number
4067 * @copp_idx: copp index assigned
4068 * @volume: gain value to set
4069 *
4070 * Returns 0 on success or error on failure
4071 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304072int adm_set_volume(int port_id, int copp_idx, int volume)
4073{
4074 struct audproc_volume_ctrl_master_gain audproc_vol;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004075 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304076 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304077
4078 pr_debug("%s: port_id %d, volume %d\n", __func__, port_id, volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304079
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004080 memset(&audproc_vol, 0, sizeof(audproc_vol));
4081 memset(&param_hdr, 0, sizeof(param_hdr));
4082 param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
4083 param_hdr.instance_id = INSTANCE_ID_0;
4084 param_hdr.param_id = AUDPROC_PARAM_ID_VOL_CTRL_MASTER_GAIN;
4085 param_hdr.param_size = sizeof(audproc_vol);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304086
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304087 audproc_vol.master_gain = volume;
4088
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004089 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4090 (uint8_t *) &audproc_vol);
4091 if (rc)
4092 pr_err("%s: Failed to set volume, err %d\n", __func__, rc);
4093
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304094 return rc;
4095}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304096EXPORT_SYMBOL(adm_set_volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304097
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304098/**
4099 * adm_set_softvolume -
4100 * command to set softvolume
4101 *
4102 * @port_id: Port ID number
4103 * @copp_idx: copp index assigned
4104 * @softvol_param: Params to set for softvolume
4105 *
4106 * Returns 0 on success or error on failure
4107 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304108int adm_set_softvolume(int port_id, int copp_idx,
4109 struct audproc_softvolume_params *softvol_param)
4110{
4111 struct audproc_soft_step_volume_params audproc_softvol;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004112 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304113 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304114
4115 pr_debug("%s: period %d step %d curve %d\n", __func__,
4116 softvol_param->period, softvol_param->step,
4117 softvol_param->rampingcurve);
4118
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004119 memset(&audproc_softvol, 0, sizeof(audproc_softvol));
4120 memset(&param_hdr, 0, sizeof(param_hdr));
4121 param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
4122 param_hdr.instance_id = INSTANCE_ID_0;
4123 param_hdr.param_id = AUDPROC_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
4124 param_hdr.param_size = sizeof(audproc_softvol);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304125
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304126 audproc_softvol.period = softvol_param->period;
4127 audproc_softvol.step = softvol_param->step;
4128 audproc_softvol.ramping_curve = softvol_param->rampingcurve;
4129
4130 pr_debug("%s: period %d, step %d, curve %d\n", __func__,
4131 audproc_softvol.period, audproc_softvol.step,
4132 audproc_softvol.ramping_curve);
4133
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004134 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4135 (uint8_t *) &audproc_softvol);
4136 if (rc)
4137 pr_err("%s: Failed to set soft volume, err %d\n", __func__, rc);
4138
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304139 return rc;
4140}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304141EXPORT_SYMBOL(adm_set_softvolume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304142
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304143/**
4144 * adm_set_mic_gain -
4145 * command to set MIC gain
4146 *
4147 * @port_id: Port ID number
4148 * @copp_idx: copp index assigned
4149 * @volume: gain value to set
4150 *
4151 * Returns 0 on success or error on failure
4152 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304153int adm_set_mic_gain(int port_id, int copp_idx, int volume)
4154{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004155 struct admx_mic_gain mic_gain_params;
4156 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304157 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304158
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004159 pr_debug("%s: Setting mic gain to %d at port_id 0x%x\n", __func__,
4160 volume, port_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304161
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004162 memset(&mic_gain_params, 0, sizeof(mic_gain_params));
4163 memset(&param_hdr, 0, sizeof(param_hdr));
4164 param_hdr.module_id = ADM_MODULE_IDX_MIC_GAIN_CTRL;
4165 param_hdr.instance_id = INSTANCE_ID_0;
4166 param_hdr.param_id = ADM_PARAM_IDX_MIC_GAIN;
4167 param_hdr.param_size = sizeof(mic_gain_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304168
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004169 mic_gain_params.tx_mic_gain = volume;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304170
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004171 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4172 (uint8_t *) &mic_gain_params);
4173 if (rc)
4174 pr_err("%s: Failed to set mic gain, err %d\n", __func__, rc);
4175
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304176 return rc;
4177}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304178EXPORT_SYMBOL(adm_set_mic_gain);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304179
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304180/**
4181 * adm_send_set_multichannel_ec_primary_mic_ch -
4182 * command to set multi-ch EC primary mic
4183 *
4184 * @port_id: Port ID number
4185 * @copp_idx: copp index assigned
4186 * @primary_mic_ch: channel number of primary mic
4187 *
4188 * Returns 0 on success or error on failure
4189 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304190int adm_send_set_multichannel_ec_primary_mic_ch(int port_id, int copp_idx,
4191 int primary_mic_ch)
4192{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004193 struct admx_sec_primary_mic_ch sec_primary_ch_params;
4194 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304195 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304196
4197 pr_debug("%s port_id 0x%x, copp_idx 0x%x, primary_mic_ch %d\n",
4198 __func__, port_id, copp_idx, primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304199
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004200 memset(&sec_primary_ch_params, 0, sizeof(sec_primary_ch_params));
4201 memset(&param_hdr, 0, sizeof(param_hdr));
4202 param_hdr.module_id = AUDPROC_MODULE_ID_VOICE_TX_SECNS;
4203 param_hdr.instance_id = INSTANCE_ID_0;
4204 param_hdr.param_id = AUDPROC_PARAM_IDX_SEC_PRIMARY_MIC_CH;
4205 param_hdr.param_size = sizeof(sec_primary_ch_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304206
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004207 sec_primary_ch_params.version = 0;
4208 sec_primary_ch_params.sec_primary_mic_ch = primary_mic_ch;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304209
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004210 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4211 (uint8_t *) &sec_primary_ch_params);
4212 if (rc)
4213 pr_err("%s: Failed to set primary mic chanel, err %d\n",
4214 __func__, rc);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304215
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304216 return rc;
4217}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304218EXPORT_SYMBOL(adm_send_set_multichannel_ec_primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304219
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304220/**
4221 * adm_param_enable -
4222 * command to send params to ADM for given module
4223 *
4224 * @port_id: Port ID number
4225 * @copp_idx: copp index assigned
4226 * @module_id: ADM module
4227 * @enable: flag to enable or disable module
4228 *
4229 * Returns 0 on success or error on failure
4230 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304231int adm_param_enable(int port_id, int copp_idx, int module_id, int enable)
4232{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004233 struct module_instance_info mod_inst_info;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304234
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004235 memset(&mod_inst_info, 0, sizeof(mod_inst_info));
4236 mod_inst_info.module_id = module_id;
4237 mod_inst_info.instance_id = INSTANCE_ID_0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304238
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004239 return adm_param_enable_v2(port_id, copp_idx, mod_inst_info, enable);
4240}
Vidyakumar Athota98464a22018-03-15 20:39:37 -07004241EXPORT_SYMBOL(adm_param_enable);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004242
4243/**
4244 * adm_param_enable_v2 -
4245 * command to send params to ADM for given module
4246 *
4247 * @port_id: Port ID number
4248 * @copp_idx: copp index assigned
4249 * @mod_inst_info: module and instance ID info
4250 * @enable: flag to enable or disable module
4251 *
4252 * Returns 0 on success or error on failure
4253 */
4254int adm_param_enable_v2(int port_id, int copp_idx,
4255 struct module_instance_info mod_inst_info, int enable)
4256{
4257 uint32_t enable_param;
4258 struct param_hdr_v3 param_hdr;
4259 int rc = 0;
4260
4261 if (enable < 0 || enable > 1) {
4262 pr_err("%s: Invalid value for enable %d\n", __func__, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304263 return -EINVAL;
4264 }
4265
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004266 pr_debug("%s port_id %d, module_id 0x%x, instance_id 0x%x, enable %d\n",
4267 __func__, port_id, mod_inst_info.module_id,
4268 mod_inst_info.instance_id, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304269
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004270 memset(&param_hdr, 0, sizeof(param_hdr));
4271 param_hdr.module_id = mod_inst_info.module_id;
4272 param_hdr.instance_id = mod_inst_info.instance_id;
4273 param_hdr.param_id = AUDPROC_PARAM_ID_ENABLE;
4274 param_hdr.param_size = sizeof(enable_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304275
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004276 enable_param = enable;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304277
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004278 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4279 (uint8_t *) &enable_param);
4280 if (rc)
4281 pr_err("%s: Failed to set enable of module(%d) instance(%d) to %d, err %d\n",
4282 __func__, mod_inst_info.module_id,
4283 mod_inst_info.instance_id, enable, rc);
4284
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304285 return rc;
4286
4287}
Vidyakumar Athota98464a22018-03-15 20:39:37 -07004288EXPORT_SYMBOL(adm_param_enable_v2);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304289
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304290/**
4291 * adm_send_calibration -
4292 * send ADM calibration to DSP
4293 *
4294 * @port_id: Port ID number
4295 * @copp_idx: copp index assigned
4296 * @path: direction or ADM path type
4297 * @perf_mode: performance mode like LL/ULL/..
4298 * @cal_type: calibration type to use
4299 * @params: pointer with cal data
4300 * @size: cal size
4301 *
4302 * Returns 0 on success or error on failure
4303 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304304int adm_send_calibration(int port_id, int copp_idx, int path, int perf_mode,
4305 int cal_type, char *params, int size)
4306{
4307
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004308 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304309
4310 pr_debug("%s:port_id %d, path %d, perf_mode %d, cal_type %d, size %d\n",
4311 __func__, port_id, path, perf_mode, cal_type, size);
4312
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304313 /* Maps audio_dev_ctrl path definition to ACDB definition */
4314 if (get_cal_path(path) != RX_DEVICE) {
4315 pr_err("%s: acdb_path %d\n", __func__, path);
4316 rc = -EINVAL;
4317 goto end;
4318 }
4319
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004320 rc = adm_set_pp_params(port_id, copp_idx, NULL, (u8 *) params, size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304321
4322end:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304323 return rc;
4324}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304325EXPORT_SYMBOL(adm_send_calibration);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304326
4327/*
4328 * adm_update_wait_parameters must be called with routing driver locks.
4329 * adm_reset_wait_parameters must be called with routing driver locks.
4330 * set and reset parmeters are separated to make sure it is always called
4331 * under routing driver lock.
4332 * adm_wait_timeout is to block until timeout or interrupted. Timeout is
4333 * not a an error.
4334 */
4335int adm_set_wait_parameters(int port_id, int copp_idx)
4336{
4337
4338 int ret = 0, port_idx;
4339
4340 pr_debug("%s: port_id 0x%x, copp_idx %d\n", __func__, port_id,
4341 copp_idx);
4342 port_id = afe_convert_virtual_to_portid(port_id);
4343 port_idx = adm_validate_and_get_port_index(port_id);
4344 if (port_idx < 0) {
4345 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4346 ret = -EINVAL;
4347 goto end;
4348 }
4349
4350 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4351 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4352 return -EINVAL;
4353 }
4354
4355 this_adm.copp.adm_delay[port_idx][copp_idx] = 1;
4356 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 0);
4357
4358end:
4359 return ret;
4360
4361}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304362EXPORT_SYMBOL(adm_set_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304363
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304364/**
4365 * adm_reset_wait_parameters -
4366 * reset wait parameters or ADM delay value
4367 *
4368 * @port_id: Port ID number
4369 * @copp_idx: copp index assigned
4370 *
4371 * Returns 0 on success or error on failure
4372 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304373int adm_reset_wait_parameters(int port_id, int copp_idx)
4374{
4375 int ret = 0, port_idx;
4376
4377 pr_debug("%s: port_id 0x%x copp_idx %d\n", __func__, port_id,
4378 copp_idx);
4379 port_id = afe_convert_virtual_to_portid(port_id);
4380 port_idx = adm_validate_and_get_port_index(port_id);
4381 if (port_idx < 0) {
4382 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4383 ret = -EINVAL;
4384 goto end;
4385 }
4386
4387 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4388 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4389 return -EINVAL;
4390 }
4391
4392 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 1);
4393 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
4394
4395end:
4396 return ret;
4397}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304398EXPORT_SYMBOL(adm_reset_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304399
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304400/**
4401 * adm_wait_timeout -
4402 * ADM wait command after command send to DSP
4403 *
4404 * @port_id: Port ID number
4405 * @copp_idx: copp index assigned
4406 * @wait_time: value in ms for command timeout
4407 *
4408 * Returns 0 on success or error on failure
4409 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304410int adm_wait_timeout(int port_id, int copp_idx, int wait_time)
4411{
4412 int ret = 0, port_idx;
4413
4414 pr_debug("%s: port_id 0x%x, copp_idx %d, wait_time %d\n", __func__,
4415 port_id, copp_idx, wait_time);
4416 port_id = afe_convert_virtual_to_portid(port_id);
4417 port_idx = adm_validate_and_get_port_index(port_id);
4418 if (port_idx < 0) {
4419 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4420 ret = -EINVAL;
4421 goto end;
4422 }
4423
4424 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4425 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4426 return -EINVAL;
4427 }
4428
4429 ret = wait_event_timeout(
4430 this_adm.copp.adm_delay_wait[port_idx][copp_idx],
4431 atomic_read(&this_adm.copp.adm_delay_stat[port_idx][copp_idx]),
4432 msecs_to_jiffies(wait_time));
4433 pr_debug("%s: return %d\n", __func__, ret);
4434 if (ret != 0)
4435 ret = -EINTR;
4436end:
4437 pr_debug("%s: return %d--\n", __func__, ret);
4438 return ret;
4439}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304440EXPORT_SYMBOL(adm_wait_timeout);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304441
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304442/**
4443 * adm_store_cal_data -
4444 * Retrieve calibration data for ADM copp device
4445 *
4446 * @port_id: Port ID number
4447 * @copp_idx: copp index assigned
4448 * @path: direction or copp type
4449 * @perf_mode: performance mode like LL/ULL/..
4450 * @cal_index: calibration index to use
4451 * @params: pointer to store cal data
4452 * @size: pointer to fill with cal size
4453 *
4454 * Returns 0 on success or error on failure
4455 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304456int adm_store_cal_data(int port_id, int copp_idx, int path, int perf_mode,
4457 int cal_index, char *params, int *size)
4458{
4459 int rc = 0;
4460 struct cal_block_data *cal_block = NULL;
4461 int app_type, acdb_id, port_idx, sample_rate;
4462
4463 if (this_adm.cal_data[cal_index] == NULL) {
4464 pr_debug("%s: cal_index %d not allocated!\n",
4465 __func__, cal_index);
4466 goto end;
4467 }
4468
4469 if (get_cal_path(path) != RX_DEVICE) {
4470 pr_debug("%s: Invalid path to store calibration %d\n",
4471 __func__, path);
4472 rc = -EINVAL;
4473 goto end;
4474 }
4475
4476 port_id = afe_convert_virtual_to_portid(port_id);
4477 port_idx = adm_validate_and_get_port_index(port_id);
4478 if (port_idx < 0) {
4479 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
4480 rc = -EINVAL;
4481 goto end;
4482 }
4483
4484 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4485 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4486 return -EINVAL;
4487 }
4488
4489 acdb_id = atomic_read(&this_adm.copp.acdb_id[port_idx][copp_idx]);
4490 app_type = atomic_read(&this_adm.copp.app_type[port_idx][copp_idx]);
4491 sample_rate = atomic_read(&this_adm.copp.rate[port_idx][copp_idx]);
4492
4493 mutex_lock(&this_adm.cal_data[cal_index]->lock);
4494 cal_block = adm_find_cal(cal_index, get_cal_path(path), app_type,
4495 acdb_id, sample_rate);
4496 if (cal_block == NULL)
4497 goto unlock;
4498
4499 if (cal_block->cal_data.size <= 0) {
4500 pr_debug("%s: No ADM cal send for port_id = 0x%x!\n",
4501 __func__, port_id);
4502 rc = -EINVAL;
4503 goto unlock;
4504 }
4505
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05304506 if (cal_index == ADM_AUDPROC_CAL || cal_index == ADM_LSM_AUDPROC_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304507 if (cal_block->cal_data.size > AUD_PROC_BLOCK_SIZE) {
4508 pr_err("%s:audproc:invalid size exp/actual[%zd, %d]\n",
4509 __func__, cal_block->cal_data.size, *size);
4510 rc = -ENOMEM;
4511 goto unlock;
4512 }
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07004513 } else if (cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
4514 if (cal_block->cal_data.size > AUD_PROC_PERSIST_BLOCK_SIZE) {
4515 pr_err("%s:persist invalid size exp/actual[%zd, %d]\n",
4516 __func__, cal_block->cal_data.size, *size);
4517 rc = -ENOMEM;
4518 goto unlock;
4519 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304520 } else if (cal_index == ADM_AUDVOL_CAL) {
4521 if (cal_block->cal_data.size > AUD_VOL_BLOCK_SIZE) {
4522 pr_err("%s:aud_vol:invalid size exp/actual[%zd, %d]\n",
4523 __func__, cal_block->cal_data.size, *size);
4524 rc = -ENOMEM;
4525 goto unlock;
4526 }
4527 } else {
4528 pr_debug("%s: Not valid calibration for dolby topolgy\n",
4529 __func__);
4530 rc = -EINVAL;
4531 goto unlock;
4532 }
4533 memcpy(params, cal_block->cal_data.kvaddr, cal_block->cal_data.size);
4534 *size = cal_block->cal_data.size;
4535
4536 pr_debug("%s:port_id %d, copp_idx %d, path %d",
4537 __func__, port_id, copp_idx, path);
4538 pr_debug("perf_mode %d, cal_type %d, size %d\n",
4539 perf_mode, cal_index, *size);
4540
4541unlock:
4542 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
4543end:
4544 return rc;
4545}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304546EXPORT_SYMBOL(adm_store_cal_data);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304547
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304548/**
4549 * adm_send_compressed_device_mute -
4550 * command to send mute for compressed device
4551 *
4552 * @port_id: Port ID number
4553 * @copp_idx: copp index assigned
4554 * @mute_on: flag to indicate mute or unmute
4555 *
4556 * Returns 0 on success or error on failure
4557 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304558int adm_send_compressed_device_mute(int port_id, int copp_idx, bool mute_on)
4559{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004560 u32 mute_param = mute_on ? 1 : 0;
4561 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304562 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304563
4564 pr_debug("%s port_id: 0x%x, copp_idx %d, mute_on: %d\n",
4565 __func__, port_id, copp_idx, mute_on);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304566
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004567 memset(&param_hdr, 0, sizeof(param_hdr));
4568 param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_MUTE;
4569 param_hdr.instance_id = INSTANCE_ID_0;
4570 param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_MUTE;
4571 param_hdr.param_size = sizeof(mute_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304572
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004573 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4574 (uint8_t *) &mute_param);
4575 if (ret)
4576 pr_err("%s: Failed to set mute, err %d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304577
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304578 return ret;
4579}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304580EXPORT_SYMBOL(adm_send_compressed_device_mute);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304581
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304582/**
4583 * adm_send_compressed_device_latency -
4584 * command to send latency for compressed device
4585 *
4586 * @port_id: Port ID number
4587 * @copp_idx: copp index assigned
4588 * @latency: latency value to pass
4589 *
4590 * Returns 0 on success or error on failure
4591 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304592int adm_send_compressed_device_latency(int port_id, int copp_idx, int latency)
4593{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004594 u32 latency_param;
4595 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304596 int ret = 0;
4597
4598 pr_debug("%s port_id: 0x%x, copp_idx %d latency: %d\n", __func__,
4599 port_id, copp_idx, latency);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004600
4601 if (latency < 0) {
4602 pr_err("%s: Invalid value for latency %d", __func__, latency);
4603 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304604 }
4605
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004606 memset(&param_hdr, 0, sizeof(param_hdr));
4607 param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_LATENCY;
4608 param_hdr.instance_id = INSTANCE_ID_0;
4609 param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_LATENCY;
4610 param_hdr.param_size = sizeof(latency_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304611
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004612 latency_param = latency;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304613
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004614 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4615 (uint8_t *) &latency_param);
4616 if (ret)
4617 pr_err("%s: Failed to set latency, err %d\n", __func__, ret);
4618
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304619 return ret;
4620}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304621EXPORT_SYMBOL(adm_send_compressed_device_latency);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304622
4623/**
4624 * adm_swap_speaker_channels
4625 *
4626 * Receives port_id, copp_idx, sample rate, spk_swap and
4627 * send MFC command to swap speaker channel.
4628 * Return zero on success. On failure returns nonzero.
4629 *
4630 * port_id - Passed value, port_id for which channels swap is wanted
4631 * copp_idx - Passed value, copp_idx for which channels swap is wanted
4632 * sample_rate - Passed value, sample rate used by app type config
4633 * spk_swap - Passed value, spk_swap for check if swap flag is set
4634 */
4635int adm_swap_speaker_channels(int port_id, int copp_idx,
4636 int sample_rate, bool spk_swap)
4637{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004638 struct audproc_mfc_param_media_fmt mfc_cfg;
4639 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304640 uint16_t num_channels;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004641 int port_idx = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304642 int ret = 0;
4643
4644 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4645 __func__, port_id, copp_idx);
4646 port_id = q6audio_convert_virtual_to_portid(port_id);
4647 port_idx = adm_validate_and_get_port_index(port_id);
4648 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
4649 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004650 return -EINVAL;
4651 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4652 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
4653 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304654 }
4655
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004656 num_channels = atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304657 if (num_channels != 2) {
4658 pr_debug("%s: Invalid number of channels: %d\n",
4659 __func__, num_channels);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004660 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304661 }
4662
4663 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004664 memset(&param_hdr, 0, sizeof(param_hdr));
4665
4666 param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
4667 param_hdr.instance_id = INSTANCE_ID_0;
4668 param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
4669 param_hdr.param_size = sizeof(mfc_cfg);
4670
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304671 mfc_cfg.sampling_rate = sample_rate;
4672 mfc_cfg.bits_per_sample =
4673 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
4674 mfc_cfg.num_channels = num_channels;
4675
4676 /* Currently applying speaker swap for only 2 channel use case */
4677 if (spk_swap) {
4678 mfc_cfg.channel_type[0] =
4679 (uint16_t) PCM_CHANNEL_FR;
4680 mfc_cfg.channel_type[1] =
4681 (uint16_t) PCM_CHANNEL_FL;
4682 } else {
4683 mfc_cfg.channel_type[0] =
4684 (uint16_t) PCM_CHANNEL_FL;
4685 mfc_cfg.channel_type[1] =
4686 (uint16_t) PCM_CHANNEL_FR;
4687 }
4688
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004689 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4690 (u8 *) &mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304691 if (ret < 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004692 pr_err("%s: Failed to set swap speaker channels on port[0x%x] failed %d\n",
4693 __func__, port_id, ret);
4694 return ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304695 }
4696
4697 pr_debug("%s: mfc_cfg Set params returned success", __func__);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004698 return 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304699}
4700EXPORT_SYMBOL(adm_swap_speaker_channels);
4701
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304702/**
4703 * adm_set_sound_focus -
4704 * Update sound focus info
4705 *
4706 * @port_id: Port ID number
4707 * @copp_idx: copp index assigned
4708 * @soundFocusData: sound focus data to pass
4709 *
4710 * Returns 0 on success or error on failure
4711 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304712int adm_set_sound_focus(int port_id, int copp_idx,
4713 struct sound_focus_param soundFocusData)
4714{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004715 struct adm_param_fluence_soundfocus_t soundfocus_params;
4716 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304717 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304718 int i;
4719
4720 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4721 __func__, port_id, copp_idx);
4722
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004723 memset(&param_hdr, 0, sizeof(param_hdr));
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07004724 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004725 param_hdr.instance_id = INSTANCE_ID_0;
4726 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
4727 param_hdr.param_size = sizeof(soundfocus_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304728
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004729 memset(&(soundfocus_params), 0xFF, sizeof(soundfocus_params));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304730 for (i = 0; i < MAX_SECTORS; i++) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004731 soundfocus_params.start_angles[i] =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304732 soundFocusData.start_angle[i];
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004733 soundfocus_params.enables[i] = soundFocusData.enable[i];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304734 pr_debug("%s: start_angle[%d] = %d\n",
4735 __func__, i, soundFocusData.start_angle[i]);
4736 pr_debug("%s: enable[%d] = %d\n",
4737 __func__, i, soundFocusData.enable[i]);
4738 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004739 soundfocus_params.gain_step = soundFocusData.gain_step;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304740 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData.gain_step);
4741
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004742 soundfocus_params.reserved = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304743
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004744 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4745 (uint8_t *) &soundfocus_params);
4746 if (ret)
4747 pr_err("%s: Failed to set sound focus params, err %d\n",
4748 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304749
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304750 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
4751
4752 return ret;
4753}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304754EXPORT_SYMBOL(adm_set_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304755
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304756/**
4757 * adm_get_sound_focus -
4758 * Retrieve sound focus info
4759 *
4760 * @port_id: Port ID number
4761 * @copp_idx: copp index assigned
4762 * @soundFocusData: pointer for sound focus data to be updated with
4763 *
4764 * Returns 0 on success or error on failure
4765 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304766int adm_get_sound_focus(int port_id, int copp_idx,
4767 struct sound_focus_param *soundFocusData)
4768{
4769 int ret = 0, i;
4770 char *params_value;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004771 uint32_t max_param_size = 0;
4772 struct adm_param_fluence_soundfocus_t *soundfocus_params = NULL;
4773 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304774
4775 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4776 __func__, port_id, copp_idx);
4777
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004778 max_param_size = sizeof(struct adm_param_fluence_soundfocus_t) +
4779 sizeof(union param_hdrs);
4780 params_value = kzalloc(max_param_size, GFP_KERNEL);
4781 if (!params_value)
4782 return -ENOMEM;
4783
4784 memset(&param_hdr, 0, sizeof(param_hdr));
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07004785 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004786 param_hdr.instance_id = INSTANCE_ID_0;
4787 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
4788 param_hdr.param_size = max_param_size;
4789 ret = adm_get_pp_params(port_id, copp_idx,
4790 ADM_CLIENT_ID_SOURCE_TRACKING, NULL, &param_hdr,
4791 params_value);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304792 if (ret) {
4793 pr_err("%s: get parameters failed ret:%d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304794 ret = -EINVAL;
4795 goto done;
4796 }
4797
4798 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
4799 pr_err("%s - get params returned error [%s]\n",
4800 __func__, adsp_err_get_err_str(
4801 this_adm.sourceTrackingData.apr_cmd_status));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304802 ret = adsp_err_get_lnx_err_code(
4803 this_adm.sourceTrackingData.apr_cmd_status);
4804 goto done;
4805 }
4806
4807 soundfocus_params = (struct adm_param_fluence_soundfocus_t *)
4808 params_value;
4809 for (i = 0; i < MAX_SECTORS; i++) {
4810 soundFocusData->start_angle[i] =
4811 soundfocus_params->start_angles[i];
4812 soundFocusData->enable[i] = soundfocus_params->enables[i];
4813 pr_debug("%s: start_angle[%d] = %d\n",
4814 __func__, i, soundFocusData->start_angle[i]);
4815 pr_debug("%s: enable[%d] = %d\n",
4816 __func__, i, soundFocusData->enable[i]);
4817 }
4818 soundFocusData->gain_step = soundfocus_params->gain_step;
4819 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData->gain_step);
4820
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304821done:
4822 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
4823
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004824 kfree(params_value);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304825 return ret;
4826}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304827EXPORT_SYMBOL(adm_get_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304828
4829static int adm_source_tracking_alloc_map_memory(void)
4830{
4831 int ret;
4832
4833 pr_debug("%s: Enter\n", __func__);
4834
Banajit Goswami08bb7362017-11-03 22:48:23 -07004835 ret = msm_audio_ion_alloc(&this_adm.sourceTrackingData.dma_buf,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304836 AUD_PROC_BLOCK_SIZE,
4837 &this_adm.sourceTrackingData.memmap.paddr,
4838 &this_adm.sourceTrackingData.memmap.size,
4839 &this_adm.sourceTrackingData.memmap.kvaddr);
4840 if (ret) {
4841 pr_err("%s: failed to allocate memory\n", __func__);
4842
4843 ret = -EINVAL;
4844 goto done;
4845 }
4846
4847 atomic_set(&this_adm.mem_map_index, ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
4848 ret = adm_memory_map_regions(&this_adm.sourceTrackingData.memmap.paddr,
4849 0,
4850 (uint32_t *)&this_adm.sourceTrackingData.memmap.size,
4851 1);
4852 if (ret < 0) {
4853 pr_err("%s: failed to map memory, paddr = 0x%pK, size = %d\n",
4854 __func__,
4855 (void *)this_adm.sourceTrackingData.memmap.paddr,
4856 (uint32_t)this_adm.sourceTrackingData.memmap.size);
4857
Banajit Goswami08bb7362017-11-03 22:48:23 -07004858 msm_audio_ion_free(this_adm.sourceTrackingData.dma_buf);
4859 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304860 this_adm.sourceTrackingData.memmap.size = 0;
4861 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
4862 this_adm.sourceTrackingData.memmap.paddr = 0;
4863 this_adm.sourceTrackingData.apr_cmd_status = -1;
4864 atomic_set(&this_adm.mem_map_handles
4865 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
4866
4867 ret = -EINVAL;
4868 goto done;
4869 }
4870 ret = 0;
4871 pr_debug("%s: paddr = 0x%pK, size = %d, mem_map_handle = 0x%x\n",
4872 __func__, (void *)this_adm.sourceTrackingData.memmap.paddr,
4873 (uint32_t)this_adm.sourceTrackingData.memmap.size,
4874 atomic_read(&this_adm.mem_map_handles
4875 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING]));
4876
4877done:
4878 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
4879
4880 return ret;
4881}
4882
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304883/**
4884 * adm_get_source_tracking -
4885 * Retrieve source tracking info
4886 *
4887 * @port_id: Port ID number
4888 * @copp_idx: copp index assigned
4889 * @sourceTrackingData: pointer for source track data to be updated with
4890 *
4891 * Returns 0 on success or error on failure
4892 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304893int adm_get_source_tracking(int port_id, int copp_idx,
4894 struct source_tracking_param *sourceTrackingData)
4895{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004896 struct adm_param_fluence_sourcetracking_t *source_tracking_params =
4897 NULL;
4898 struct mem_mapping_hdr mem_hdr;
4899 struct param_hdr_v3 param_hdr;
4900 int i = 0;
4901 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304902
4903 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4904 __func__, port_id, copp_idx);
4905
4906 if (!this_adm.sourceTrackingData.memmap.paddr) {
4907 /* Allocate and map shared memory for out of band usage */
4908 ret = adm_source_tracking_alloc_map_memory();
4909 if (ret != 0) {
4910 ret = -EINVAL;
4911 goto done;
4912 }
4913 }
4914
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004915 memset(&mem_hdr, 0, sizeof(mem_hdr));
4916 memset(&param_hdr, 0, sizeof(param_hdr));
4917 mem_hdr.data_payload_addr_lsw =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304918 lower_32_bits(this_adm.sourceTrackingData.memmap.paddr);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004919 mem_hdr.data_payload_addr_msw = msm_audio_populate_upper_32_bits(
4920 this_adm.sourceTrackingData.memmap.paddr);
4921 mem_hdr.mem_map_handle = atomic_read(
4922 &this_adm.mem_map_handles[ADM_MEM_MAP_INDEX_SOURCE_TRACKING]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304923
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07004924 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004925 param_hdr.instance_id = INSTANCE_ID_0;
4926 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOURCETRACKING;
4927 /*
4928 * This size should be the max size of the calibration data + header.
4929 * Use the union size to ensure max size is used.
4930 */
4931 param_hdr.param_size =
4932 sizeof(struct adm_param_fluence_sourcetracking_t) +
4933 sizeof(union param_hdrs);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304934
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004935 /*
4936 * Retrieving parameters out of band, so no need to provide a buffer for
4937 * the returned parameter data as it will be at the memory location
4938 * provided.
4939 */
4940 ret = adm_get_pp_params(port_id, copp_idx,
4941 ADM_CLIENT_ID_SOURCE_TRACKING, &mem_hdr,
4942 &param_hdr, NULL);
4943 if (ret) {
4944 pr_err("%s: Failed to get params, error %d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304945 goto done;
4946 }
4947
4948 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
4949 pr_err("%s - get params returned error [%s]\n",
4950 __func__, adsp_err_get_err_str(
4951 this_adm.sourceTrackingData.apr_cmd_status));
4952
4953 ret = adsp_err_get_lnx_err_code(
4954 this_adm.sourceTrackingData.apr_cmd_status);
4955 goto done;
4956 }
4957
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004958 /* How do we know what the param data was retrieved with for hdr size */
4959 source_tracking_params =
4960 (struct adm_param_fluence_sourcetracking_t
4961 *) (this_adm.sourceTrackingData.memmap.kvaddr +
4962 sizeof(struct param_hdr_v1));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304963 for (i = 0; i < MAX_SECTORS; i++) {
4964 sourceTrackingData->vad[i] = source_tracking_params->vad[i];
4965 pr_debug("%s: vad[%d] = %d\n",
4966 __func__, i, sourceTrackingData->vad[i]);
4967 }
4968 sourceTrackingData->doa_speech = source_tracking_params->doa_speech;
4969 pr_debug("%s: doa_speech = %d\n",
4970 __func__, sourceTrackingData->doa_speech);
4971
4972 for (i = 0; i < MAX_NOISE_SOURCE_INDICATORS; i++) {
4973 sourceTrackingData->doa_noise[i] =
4974 source_tracking_params->doa_noise[i];
4975 pr_debug("%s: doa_noise[%d] = %d\n",
4976 __func__, i, sourceTrackingData->doa_noise[i]);
4977 }
4978 for (i = 0; i < MAX_POLAR_ACTIVITY_INDICATORS; i++) {
4979 sourceTrackingData->polar_activity[i] =
4980 source_tracking_params->polar_activity[i];
4981 pr_debug("%s: polar_activity[%d] = %d\n",
4982 __func__, i, sourceTrackingData->polar_activity[i]);
4983 }
4984
4985 ret = 0;
4986
4987done:
4988 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
4989
4990 return ret;
4991}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304992EXPORT_SYMBOL(adm_get_source_tracking);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304993
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304994int __init adm_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304995{
4996 int i = 0, j;
4997
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304998 this_adm.ec_ref_rx = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304999 init_waitqueue_head(&this_adm.matrix_map_wait);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305000 init_waitqueue_head(&this_adm.adm_wait);
5001
5002 for (i = 0; i < AFE_MAX_PORTS; i++) {
5003 for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
5004 atomic_set(&this_adm.copp.id[i][j], RESET_COPP_ID);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305005 init_waitqueue_head(&this_adm.copp.wait[i][j]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305006 init_waitqueue_head(
5007 &this_adm.copp.adm_delay_wait[i][j]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305008 }
5009 }
5010
5011 if (adm_init_cal_data())
5012 pr_err("%s: could not init cal data!\n", __func__);
5013
Banajit Goswami08bb7362017-11-03 22:48:23 -07005014 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305015 this_adm.sourceTrackingData.memmap.size = 0;
5016 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
5017 this_adm.sourceTrackingData.memmap.paddr = 0;
5018 this_adm.sourceTrackingData.apr_cmd_status = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305019
5020 return 0;
5021}
5022
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05305023void adm_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305024{
Laxminath Kasam30ad7512017-11-28 12:40:22 +05305025 if (this_adm.apr)
5026 adm_reset_data();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305027 adm_delete_cal_data();
5028}