blob: 265ae939b0d80cabe75f3fa3add513260c2355ae [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
Dieter Luecking41620832018-11-14 15:11:47 +01002942 if (this_adm.ec_ref_rx && (path != ADM_PATH_PLAYBACK)) {
2943 open_v8.endpoint_id_2 = this_adm.ec_ref_rx;
2944 this_adm.ec_ref_rx = -1;
2945 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302946
Dieter Luecking50c55352018-09-28 14:29:17 +02002947 open_v8.topology_id = topology;
2948 open_v8.reserved = 0;
2949
2950 /* variable endpoint payload */
2951 ep1_payload.dev_num_channel = channel_mode & 0x00FF;
2952 ep1_payload.bit_width = bit_width;
2953 ep1_payload.sample_rate = rate;
2954 ret = adm_arrange_mch_map_v8(&ep1_payload, path,
2955 channel_mode);
2956 if (ret)
2957 return ret;
2958
2959 pr_debug("%s: port_id=0x%x %x %x topology_id=0x%X flags %x ref_ch %x\n",
2960 __func__, open_v8.endpoint_id_1,
2961 open_v8.endpoint_id_2,
2962 open_v8.endpoint_id_3,
2963 open_v8.topology_id,
2964 open_v8.flags,
2965 this_adm.num_ec_ref_rx_chans);
2966
2967 ep1_payload_size = 8 +
2968 roundup(ep1_payload.dev_num_channel, 4);
2969 param_size = sizeof(struct adm_cmd_device_open_v8)
2970 + ep1_payload_size;
2971 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2972
Dieter Luecking27c57722018-10-05 16:45:41 +02002973 if ((this_adm.num_ec_ref_rx_chans != 0)
2974 && (path != ADM_PATH_PLAYBACK)
Dieter Luecking50c55352018-09-28 14:29:17 +02002975 && (open_v8.endpoint_id_2 != 0xFFFF)) {
2976 ep2_payload.dev_num_channel =
2977 this_adm.num_ec_ref_rx_chans;
Dieter Luecking50c55352018-09-28 14:29:17 +02002978
2979 if (this_adm.ec_ref_rx_bit_width != 0) {
2980 ep2_payload.bit_width =
2981 this_adm.ec_ref_rx_bit_width;
2982 this_adm.ec_ref_rx_bit_width = 0;
2983 } else {
2984 ep2_payload.bit_width = bit_width;
2985 }
2986
2987 if (this_adm.ec_ref_rx_sampling_rate != 0) {
2988 ep2_payload.sample_rate =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302989 this_adm.ec_ref_rx_sampling_rate;
Dieter Luecking50c55352018-09-28 14:29:17 +02002990 this_adm.ec_ref_rx_sampling_rate = 0;
2991 } else {
2992 ep2_payload.sample_rate = rate;
2993 }
2994
2995 pr_debug("%s: adm open_v8 eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
2996 __func__,
2997 ep2_payload.dev_num_channel,
2998 ep2_payload.bit_width,
2999 ep2_payload.sample_rate);
3000
3001 ret = adm_arrange_mch_ep2_map_v8(&ep2_payload,
3002 ep2_payload.dev_num_channel);
3003
3004 if (ret)
3005 return ret;
3006 ep2_payload_size = 8 +
3007 roundup(ep2_payload.dev_num_channel, 4);
3008 param_size += ep2_payload_size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303009 }
3010
Dieter Luecking27c57722018-10-05 16:45:41 +02003011 open_v8.hdr.pkt_size = param_size;
Dieter Luecking50c55352018-09-28 14:29:17 +02003012 adm_params = kzalloc(param_size, GFP_KERNEL);
3013 if (!adm_params)
3014 return -ENOMEM;
Dieter Luecking50c55352018-09-28 14:29:17 +02003015 memcpy(adm_params, &open_v8, sizeof(open_v8));
3016 memcpy(adm_params + sizeof(open_v8),
3017 (void *)&ep1_payload,
3018 ep1_payload_size);
Dieter Luecking27c57722018-10-05 16:45:41 +02003019
3020 if ((this_adm.num_ec_ref_rx_chans != 0)
3021 && (path != ADM_PATH_PLAYBACK)
3022 && (open_v8.endpoint_id_2 != 0xFFFF)) {
Dieter Luecking41620832018-11-14 15:11:47 +01003023 this_adm.num_ec_ref_rx_chans = 0;
Dieter Luecking27c57722018-10-05 16:45:41 +02003024 memcpy(adm_params + sizeof(open_v8)
3025 + ep1_payload_size,
3026 (void *)&ep2_payload,
3027 ep2_payload_size);
3028 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303029
Dieter Luecking50c55352018-09-28 14:29:17 +02003030 ret = apr_send_pkt(this_adm.apr,
3031 (uint32_t *)adm_params);
3032 if (ret < 0) {
3033 pr_err("%s: port_id: 0x%x for[0x%x] failed %d for open_v8\n",
3034 __func__, tmp_port, port_id, ret);
3035 return -EINVAL;
3036 }
3037 kfree(adm_params);
3038 } else {
3039
3040 open.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3041 APR_HDR_LEN(APR_HDR_SIZE),
3042 APR_PKT_VER);
3043 open.hdr.pkt_size = sizeof(open);
3044 open.hdr.src_svc = APR_SVC_ADM;
3045 open.hdr.src_domain = APR_DOMAIN_APPS;
3046 open.hdr.src_port = tmp_port;
3047 open.hdr.dest_svc = APR_SVC_ADM;
3048 open.hdr.dest_domain = APR_DOMAIN_ADSP;
3049 open.hdr.dest_port = tmp_port;
3050 open.hdr.token = port_idx << 16 | copp_idx;
3051 open.hdr.opcode = ADM_CMD_DEVICE_OPEN_V5;
3052 open.flags = flags;
3053 open.mode_of_operation = path;
3054 open.endpoint_id_1 = tmp_port;
3055 open.endpoint_id_2 = 0xFFFF;
3056
3057 if (this_adm.ec_ref_rx && (path != 1)) {
3058 open.endpoint_id_2 = this_adm.ec_ref_rx;
3059 this_adm.ec_ref_rx = -1;
3060 }
3061
3062 open.topology_id = topology;
3063
3064 open.dev_num_channel = channel_mode & 0x00FF;
3065 open.bit_width = bit_width;
3066 WARN_ON((perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) &&
3067 (rate != ULL_SUPPORTED_SAMPLE_RATE));
3068 open.sample_rate = rate;
3069
3070 ret = adm_arrange_mch_map(&open, path, channel_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303071
3072 if (ret)
3073 return ret;
3074
Dieter Luecking50c55352018-09-28 14:29:17 +02003075 pr_debug("%s: port_id=0x%x rate=%d topology_id=0x%X\n",
3076 __func__, open.endpoint_id_1, open.sample_rate,
3077 open.topology_id);
3078
3079 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3080
3081 if ((this_adm.num_ec_ref_rx_chans != 0) &&
3082 (path != 1) && (open.endpoint_id_2 != 0xFFFF)) {
3083 memset(&open_v6, 0,
3084 sizeof(struct adm_cmd_device_open_v6));
3085 memcpy(&open_v6, &open,
3086 sizeof(struct adm_cmd_device_open_v5));
3087 open_v6.hdr.opcode = ADM_CMD_DEVICE_OPEN_V6;
3088 open_v6.hdr.pkt_size = sizeof(open_v6);
3089 open_v6.dev_num_channel_eid2 =
3090 this_adm.num_ec_ref_rx_chans;
3091 this_adm.num_ec_ref_rx_chans = 0;
3092
3093 if (this_adm.ec_ref_rx_bit_width != 0) {
3094 open_v6.bit_width_eid2 =
3095 this_adm.ec_ref_rx_bit_width;
3096 this_adm.ec_ref_rx_bit_width = 0;
3097 } else {
3098 open_v6.bit_width_eid2 = bit_width;
3099 }
3100
3101 if (this_adm.ec_ref_rx_sampling_rate != 0) {
3102 open_v6.sample_rate_eid2 =
3103 this_adm.ec_ref_rx_sampling_rate;
3104 this_adm.ec_ref_rx_sampling_rate = 0;
3105 } else {
3106 open_v6.sample_rate_eid2 = rate;
3107 }
3108
3109 pr_debug("%s: eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
3110 __func__, open_v6.dev_num_channel_eid2,
3111 open_v6.bit_width_eid2,
3112 open_v6.sample_rate_eid2);
3113
3114 ret = adm_arrange_mch_ep2_map(&open_v6,
3115 open_v6.dev_num_channel_eid2);
3116
3117 if (ret)
3118 return ret;
3119
3120 ret = apr_send_pkt(this_adm.apr,
3121 (uint32_t *)&open_v6);
3122 } else {
3123 ret = apr_send_pkt(this_adm.apr,
3124 (uint32_t *)&open);
3125 }
3126 if (ret < 0) {
3127 pr_err("%s: port_id: 0x%x for[0x%x] failed %d\n",
3128 __func__, tmp_port, port_id, ret);
3129 return -EINVAL;
3130 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303131 }
Dieter Luecking50c55352018-09-28 14:29:17 +02003132
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303133 /* Wait for the callback with copp id */
3134 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3135 atomic_read(&this_adm.copp.stat
3136 [port_idx][copp_idx]) >= 0,
3137 msecs_to_jiffies(TIMEOUT_MS));
3138 if (!ret) {
3139 pr_err("%s: ADM open timedout for port_id: 0x%x for [0x%x]\n",
3140 __func__, tmp_port, port_id);
3141 return -EINVAL;
3142 } else if (atomic_read(&this_adm.copp.stat
3143 [port_idx][copp_idx]) > 0) {
3144 pr_err("%s: DSP returned error[%s]\n",
3145 __func__, adsp_err_get_err_str(
3146 atomic_read(&this_adm.copp.stat
3147 [port_idx][copp_idx])));
3148 return adsp_err_get_lnx_err_code(
3149 atomic_read(&this_adm.copp.stat
3150 [port_idx][copp_idx]));
3151 }
3152 }
3153 atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
3154 return copp_idx;
3155}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303156EXPORT_SYMBOL(adm_open);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303157
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303158/**
3159 * adm_copp_mfc_cfg -
3160 * command to send ADM MFC config
3161 *
3162 * @port_id: Port ID number
3163 * @copp_idx: copp index assigned
3164 * @dst_sample_rate: sink sample rate
3165 *
3166 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303167void adm_copp_mfc_cfg(int port_id, int copp_idx, int dst_sample_rate)
3168{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003169 struct audproc_mfc_param_media_fmt mfc_cfg;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303170 struct adm_cmd_device_open_v5 open;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003171 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303172 int port_idx;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303173 int rc = 0;
3174 int i = 0;
3175
3176 port_id = q6audio_convert_virtual_to_portid(port_id);
3177 port_idx = adm_validate_and_get_port_index(port_id);
3178
3179 if (port_idx < 0) {
3180 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
3181 goto fail_cmd;
3182 }
3183
3184 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3185 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3186 goto fail_cmd;
3187 }
3188
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003189 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
3190 memset(&open, 0, sizeof(open));
3191 memset(&param_hdr, 0, sizeof(param_hdr));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303192
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003193 param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
3194 param_hdr.instance_id = INSTANCE_ID_0;
3195 param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
3196 param_hdr.param_size = sizeof(mfc_cfg);
3197
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303198 mfc_cfg.sampling_rate = dst_sample_rate;
3199 mfc_cfg.bits_per_sample =
3200 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
3201 open.dev_num_channel = mfc_cfg.num_channels =
3202 atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
3203
3204 rc = adm_arrange_mch_map(&open, ADM_PATH_PLAYBACK,
3205 mfc_cfg.num_channels);
3206 if (rc < 0) {
3207 pr_err("%s: unable to get channal map\n", __func__);
3208 goto fail_cmd;
3209 }
3210
3211 for (i = 0; i < mfc_cfg.num_channels; i++)
3212 mfc_cfg.channel_type[i] =
3213 (uint16_t) open.dev_channel_mapping[i];
3214
3215 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3216
3217 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",
3218 __func__, port_idx, copp_idx,
3219 atomic_read(&this_adm.copp.rate[port_idx][copp_idx]),
3220 mfc_cfg.bits_per_sample, mfc_cfg.num_channels,
3221 mfc_cfg.sampling_rate);
3222
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003223 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
3224 (uint8_t *) &mfc_cfg);
3225 if (rc)
3226 pr_err("%s: Failed to set media format configuration data, err %d\n",
3227 __func__, rc);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303228
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303229fail_cmd:
3230 return;
3231}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303232EXPORT_SYMBOL(adm_copp_mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303233
3234static void route_set_opcode_matrix_id(
3235 struct adm_cmd_matrix_map_routings_v5 **route_addr,
3236 int path, uint32_t passthr_mode)
3237{
3238 struct adm_cmd_matrix_map_routings_v5 *route = *route_addr;
3239
3240 switch (path) {
3241 case ADM_PATH_PLAYBACK:
3242 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
3243 route->matrix_id = ADM_MATRIX_ID_AUDIO_RX;
3244 break;
3245 case ADM_PATH_LIVE_REC:
3246 if (passthr_mode == LISTEN) {
3247 route->hdr.opcode =
3248 ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3249 route->matrix_id = ADM_MATRIX_ID_LISTEN_TX;
3250 break;
3251 }
3252 /* fall through to set matrix id for non-listen case */
3253 case ADM_PATH_NONLIVE_REC:
3254 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
3255 route->matrix_id = ADM_MATRIX_ID_AUDIO_TX;
3256 break;
3257 case ADM_PATH_COMPRESSED_RX:
3258 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3259 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_RX;
3260 break;
3261 case ADM_PATH_COMPRESSED_TX:
3262 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3263 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_TX;
3264 break;
3265 default:
3266 pr_err("%s: Wrong path set[%d]\n", __func__, path);
3267 break;
3268 }
3269 pr_debug("%s: opcode 0x%x, matrix id %d\n",
3270 __func__, route->hdr.opcode, route->matrix_id);
3271}
3272
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303273/**
3274 * adm_matrix_map -
3275 * command to send ADM matrix map for ADM copp list
3276 *
3277 * @path: direction or ADM path type
3278 * @payload_map: have info of session id and associated copp_idx/num_copps
3279 * @perf_mode: performance mode like LL/ULL/..
3280 * @passthr_mode: flag to indicate passthrough mode
3281 *
3282 * Returns 0 on success or error on failure
3283 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303284int adm_matrix_map(int path, struct route_payload payload_map, int perf_mode,
3285 uint32_t passthr_mode)
3286{
3287 struct adm_cmd_matrix_map_routings_v5 *route;
3288 struct adm_session_map_node_v5 *node;
3289 uint16_t *copps_list;
3290 int cmd_size = 0;
3291 int ret = 0, i = 0;
3292 void *payload = NULL;
3293 void *matrix_map = NULL;
3294 int port_idx, copp_idx;
3295
3296 /* Assumes port_ids have already been validated during adm_open */
3297 cmd_size = (sizeof(struct adm_cmd_matrix_map_routings_v5) +
3298 sizeof(struct adm_session_map_node_v5) +
3299 (sizeof(uint32_t) * payload_map.num_copps));
3300 matrix_map = kzalloc(cmd_size, GFP_KERNEL);
3301 if (matrix_map == NULL) {
3302 pr_err("%s: Mem alloc failed\n", __func__);
3303 ret = -EINVAL;
3304 return ret;
3305 }
3306 route = (struct adm_cmd_matrix_map_routings_v5 *)matrix_map;
3307
3308 route->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3309 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
3310 route->hdr.pkt_size = cmd_size;
3311 route->hdr.src_svc = 0;
3312 route->hdr.src_domain = APR_DOMAIN_APPS;
3313 route->hdr.src_port = 0; /* Ignored */;
3314 route->hdr.dest_svc = APR_SVC_ADM;
3315 route->hdr.dest_domain = APR_DOMAIN_ADSP;
3316 route->hdr.dest_port = 0; /* Ignored */;
3317 route->hdr.token = 0;
3318 route->num_sessions = 1;
3319 route_set_opcode_matrix_id(&route, path, passthr_mode);
3320
3321 payload = ((u8 *)matrix_map +
3322 sizeof(struct adm_cmd_matrix_map_routings_v5));
3323 node = (struct adm_session_map_node_v5 *)payload;
3324
3325 node->session_id = payload_map.session_id;
3326 node->num_copps = payload_map.num_copps;
3327 payload = (u8 *)node + sizeof(struct adm_session_map_node_v5);
3328 copps_list = (uint16_t *)payload;
3329 for (i = 0; i < payload_map.num_copps; i++) {
3330 port_idx =
3331 adm_validate_and_get_port_index(payload_map.port_id[i]);
3332 if (port_idx < 0) {
3333 pr_err("%s: Invalid port_id 0x%x\n", __func__,
3334 payload_map.port_id[i]);
3335 ret = -EINVAL;
3336 goto fail_cmd;
3337 }
3338 copp_idx = payload_map.copp_idx[i];
3339 copps_list[i] = atomic_read(&this_adm.copp.id[port_idx]
3340 [copp_idx]);
3341 }
3342 atomic_set(&this_adm.matrix_map_stat, -1);
3343
3344 ret = apr_send_pkt(this_adm.apr, (uint32_t *)matrix_map);
3345 if (ret < 0) {
3346 pr_err("%s: routing for syream %d failed ret %d\n",
3347 __func__, payload_map.session_id, ret);
3348 ret = -EINVAL;
3349 goto fail_cmd;
3350 }
3351 ret = wait_event_timeout(this_adm.matrix_map_wait,
3352 atomic_read(&this_adm.matrix_map_stat) >= 0,
3353 msecs_to_jiffies(TIMEOUT_MS));
3354 if (!ret) {
3355 pr_err("%s: routing for syream %d failed\n", __func__,
3356 payload_map.session_id);
3357 ret = -EINVAL;
3358 goto fail_cmd;
3359 } else if (atomic_read(&this_adm.matrix_map_stat) > 0) {
3360 pr_err("%s: DSP returned error[%s]\n", __func__,
3361 adsp_err_get_err_str(atomic_read(
3362 &this_adm.matrix_map_stat)));
3363 ret = adsp_err_get_lnx_err_code(
3364 atomic_read(&this_adm.matrix_map_stat));
3365 goto fail_cmd;
3366 }
3367
3368 if ((perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) &&
3369 (path != ADM_PATH_COMPRESSED_RX)) {
3370 for (i = 0; i < payload_map.num_copps; i++) {
3371 port_idx = afe_get_port_index(payload_map.port_id[i]);
3372 copp_idx = payload_map.copp_idx[i];
3373 if (port_idx < 0 || copp_idx < 0 ||
3374 (copp_idx > MAX_COPPS_PER_PORT - 1)) {
3375 pr_err("%s: Invalid idx port_idx %d copp_idx %d\n",
3376 __func__, port_idx, copp_idx);
3377 continue;
3378 }
3379 rtac_add_adm_device(payload_map.port_id[i],
3380 atomic_read(&this_adm.copp.id
3381 [port_idx][copp_idx]),
3382 get_cal_path(path),
3383 payload_map.session_id,
3384 payload_map.app_type[i],
3385 payload_map.acdb_dev_id[i]);
3386
3387 if (!test_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3388 (void *)&this_adm.copp.adm_status[port_idx]
3389 [copp_idx])) {
3390 pr_debug("%s: adm copp[0x%x][%d] already sent",
3391 __func__, port_idx, copp_idx);
3392 continue;
3393 }
3394 send_adm_cal(payload_map.port_id[i], copp_idx,
3395 get_cal_path(path), perf_mode,
3396 payload_map.app_type[i],
3397 payload_map.acdb_dev_id[i],
Aditya Bavanari5106b562018-01-08 13:16:32 +05303398 payload_map.sample_rate[i],
3399 passthr_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303400 /* ADM COPP calibration is already sent */
3401 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3402 (void *)&this_adm.copp.
3403 adm_status[port_idx][copp_idx]);
3404 pr_debug("%s: copp_id: %d\n", __func__,
3405 atomic_read(&this_adm.copp.id[port_idx]
3406 [copp_idx]));
3407 }
3408 }
3409
3410fail_cmd:
3411 kfree(matrix_map);
3412 return ret;
3413}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303414EXPORT_SYMBOL(adm_matrix_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303415
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303416/**
3417 * adm_ec_ref_rx_id -
3418 * Update EC ref port ID
3419 *
3420 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303421void adm_ec_ref_rx_id(int port_id)
3422{
3423 this_adm.ec_ref_rx = port_id;
3424 pr_debug("%s: ec_ref_rx:%d\n", __func__, this_adm.ec_ref_rx);
3425}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303426EXPORT_SYMBOL(adm_ec_ref_rx_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303427
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303428/**
3429 * adm_num_ec_ref_rx_chans -
3430 * Update EC ref number of channels
3431 *
3432 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303433void adm_num_ec_ref_rx_chans(int num_chans)
3434{
3435 this_adm.num_ec_ref_rx_chans = num_chans;
3436 pr_debug("%s: num_ec_ref_rx_chans:%d\n",
3437 __func__, this_adm.num_ec_ref_rx_chans);
3438}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303439EXPORT_SYMBOL(adm_num_ec_ref_rx_chans);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303440
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303441/**
3442 * adm_ec_ref_rx_bit_width -
3443 * Update EC ref bit_width
3444 *
3445 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303446void adm_ec_ref_rx_bit_width(int bit_width)
3447{
3448 this_adm.ec_ref_rx_bit_width = bit_width;
3449 pr_debug("%s: ec_ref_rx_bit_width:%d\n",
3450 __func__, this_adm.ec_ref_rx_bit_width);
3451}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303452EXPORT_SYMBOL(adm_ec_ref_rx_bit_width);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303453
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303454/**
3455 * adm_ec_ref_rx_sampling_rate -
3456 * Update EC ref sample rate
3457 *
3458 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303459void adm_ec_ref_rx_sampling_rate(int sampling_rate)
3460{
3461 this_adm.ec_ref_rx_sampling_rate = sampling_rate;
3462 pr_debug("%s: ec_ref_rx_sampling_rate:%d\n",
3463 __func__, this_adm.ec_ref_rx_sampling_rate);
3464}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303465EXPORT_SYMBOL(adm_ec_ref_rx_sampling_rate);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303466
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303467/**
Dieter Luecking50c55352018-09-28 14:29:17 +02003468 * adm_set_native_mode -
3469 * Set adm channel native mode.
3470 * If enabled matrix mixer will be
3471 * running in native mode for channel
3472 * configuration for this device session.
3473 *
3474 */
3475void adm_set_native_mode(int mode)
3476{
3477 this_adm.native_mode = mode;
3478 pr_debug("%s: enable native_mode :%d\n",
3479 __func__, this_adm.native_mode);
3480}
3481EXPORT_SYMBOL(adm_set_native_mode);
3482
3483/**
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303484 * adm_close -
3485 * command to close ADM copp
3486 *
3487 * @port_id: Port ID number
3488 * @perf_mode: performance mode like LL/ULL/..
3489 * @copp_idx: copp index assigned
3490 *
3491 * Returns 0 on success or error on failure
3492 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303493int adm_close(int port_id, int perf_mode, int copp_idx)
3494{
3495 struct apr_hdr close;
3496
3497 int ret = 0, port_idx;
3498 int copp_id = RESET_COPP_ID;
3499
3500 pr_debug("%s: port_id=0x%x perf_mode: %d copp_idx: %d\n", __func__,
3501 port_id, perf_mode, copp_idx);
3502
3503 port_id = q6audio_convert_virtual_to_portid(port_id);
3504 port_idx = adm_validate_and_get_port_index(port_id);
3505 if (port_idx < 0) {
3506 pr_err("%s: Invalid port_id 0x%x\n",
3507 __func__, port_id);
3508 return -EINVAL;
3509 }
3510
3511 if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
3512 pr_err("%s: Invalid copp idx: %d\n", __func__, copp_idx);
3513 return -EINVAL;
3514 }
3515
3516 if (this_adm.copp.adm_delay[port_idx][copp_idx] && perf_mode
3517 == LEGACY_PCM_MODE) {
3518 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
3519 1);
3520 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
3521 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
3522 }
3523
3524 atomic_dec(&this_adm.copp.cnt[port_idx][copp_idx]);
3525 if (!(atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]))) {
3526 copp_id = adm_get_copp_id(port_idx, copp_idx);
3527 pr_debug("%s: Closing ADM port_idx:%d copp_idx:%d copp_id:0x%x\n",
3528 __func__, port_idx, copp_idx, copp_id);
3529 if ((!perf_mode) && (this_adm.outband_memmap.paddr != 0) &&
3530 (atomic_read(&this_adm.copp.topology[port_idx][copp_idx]) ==
3531 SRS_TRUMEDIA_TOPOLOGY_ID)) {
3532 atomic_set(&this_adm.mem_map_index,
3533 ADM_SRS_TRUMEDIA);
3534 ret = adm_memory_unmap_regions();
3535 if (ret < 0) {
3536 pr_err("%s: adm mem unmmap err %d",
3537 __func__, ret);
3538 } else {
3539 atomic_set(&this_adm.mem_map_handles
3540 [ADM_SRS_TRUMEDIA], 0);
3541 }
3542 }
3543
3544
3545 if ((afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX) &&
3546 this_adm.sourceTrackingData.memmap.paddr) {
3547 atomic_set(&this_adm.mem_map_index,
3548 ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
3549 ret = adm_memory_unmap_regions();
3550 if (ret < 0) {
3551 pr_err("%s: adm mem unmmap err %d",
3552 __func__, ret);
3553 }
3554 msm_audio_ion_free(
Banajit Goswami08bb7362017-11-03 22:48:23 -07003555 this_adm.sourceTrackingData.dma_buf);
3556 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303557 this_adm.sourceTrackingData.memmap.size = 0;
3558 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
3559 this_adm.sourceTrackingData.memmap.paddr = 0;
3560 this_adm.sourceTrackingData.apr_cmd_status = -1;
3561 atomic_set(&this_adm.mem_map_handles[
3562 ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
3563 }
3564
3565 close.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3566 APR_HDR_LEN(APR_HDR_SIZE),
3567 APR_PKT_VER);
3568 close.pkt_size = sizeof(close);
3569 close.src_svc = APR_SVC_ADM;
3570 close.src_domain = APR_DOMAIN_APPS;
3571 close.src_port = port_id;
3572 close.dest_svc = APR_SVC_ADM;
3573 close.dest_domain = APR_DOMAIN_ADSP;
3574 close.dest_port = copp_id;
3575 close.token = port_idx << 16 | copp_idx;
3576 close.opcode = ADM_CMD_DEVICE_CLOSE_V5;
3577
3578 atomic_set(&this_adm.copp.id[port_idx][copp_idx],
3579 RESET_COPP_ID);
3580 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
3581 atomic_set(&this_adm.copp.topology[port_idx][copp_idx], 0);
3582 atomic_set(&this_adm.copp.mode[port_idx][copp_idx], 0);
3583 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3584 atomic_set(&this_adm.copp.rate[port_idx][copp_idx], 0);
3585 atomic_set(&this_adm.copp.channels[port_idx][copp_idx], 0);
3586 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx], 0);
3587 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx], 0);
3588
3589 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3590 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
3591
3592 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&close);
3593 if (ret < 0) {
3594 pr_err("%s: ADM close failed %d\n", __func__, ret);
3595 return -EINVAL;
3596 }
3597
3598 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3599 atomic_read(&this_adm.copp.stat
3600 [port_idx][copp_idx]) >= 0,
3601 msecs_to_jiffies(TIMEOUT_MS));
3602 if (!ret) {
3603 pr_err("%s: ADM cmd Route timedout for port 0x%x\n",
3604 __func__, port_id);
3605 return -EINVAL;
3606 } else if (atomic_read(&this_adm.copp.stat
3607 [port_idx][copp_idx]) > 0) {
3608 pr_err("%s: DSP returned error[%s]\n",
3609 __func__, adsp_err_get_err_str(
3610 atomic_read(&this_adm.copp.stat
3611 [port_idx][copp_idx])));
3612 return adsp_err_get_lnx_err_code(
3613 atomic_read(&this_adm.copp.stat
3614 [port_idx][copp_idx]));
3615 }
3616 }
3617
3618 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) {
3619 pr_debug("%s: remove adm device from rtac\n", __func__);
3620 rtac_remove_adm_device(port_id, copp_id);
3621 }
3622 return 0;
3623}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303624EXPORT_SYMBOL(adm_close);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303625
3626int send_rtac_audvol_cal(void)
3627{
3628 int ret = 0;
3629 int ret2 = 0;
3630 int i = 0;
3631 int copp_idx, port_idx, acdb_id, app_id, path;
3632 struct cal_block_data *cal_block = NULL;
3633 struct audio_cal_info_audvol *audvol_cal_info = NULL;
3634 struct rtac_adm rtac_adm_data;
3635
3636 mutex_lock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3637
3638 cal_block = cal_utils_get_only_cal_block(
3639 this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]);
Vikram Panduranga770b8382017-09-27 12:17:36 -07003640 if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303641 pr_err("%s: can't find cal block!\n", __func__);
3642 goto unlock;
3643 }
3644
3645 audvol_cal_info = cal_block->cal_info;
3646 if (audvol_cal_info == NULL) {
3647 pr_err("%s: audvol_cal_info is NULL!\n", __func__);
3648 goto unlock;
3649 }
3650
3651 get_rtac_adm_data(&rtac_adm_data);
3652 for (; i < rtac_adm_data.num_of_dev; i++) {
3653
3654 acdb_id = rtac_adm_data.device[i].acdb_dev_id;
3655 if (acdb_id == 0)
3656 acdb_id = audvol_cal_info->acdb_id;
3657
3658 app_id = rtac_adm_data.device[i].app_type;
3659 if (app_id == 0)
3660 app_id = audvol_cal_info->app_type;
3661
3662 path = afe_get_port_type(rtac_adm_data.device[i].afe_port);
3663 if ((acdb_id == audvol_cal_info->acdb_id) &&
3664 (app_id == audvol_cal_info->app_type) &&
3665 (path == audvol_cal_info->path)) {
3666
3667 if (adm_get_indexes_from_copp_id(rtac_adm_data.
3668 device[i].copp, &copp_idx, &port_idx) != 0) {
3669 pr_debug("%s: Copp Id %d is not active\n",
3670 __func__,
3671 rtac_adm_data.device[i].copp);
3672 continue;
3673 }
3674
3675 ret2 = adm_remap_and_send_cal_block(ADM_RTAC_AUDVOL_CAL,
3676 rtac_adm_data.device[i].afe_port,
3677 copp_idx, cal_block,
3678 atomic_read(&this_adm.copp.
3679 mode[port_idx][copp_idx]),
3680 audvol_cal_info->app_type,
3681 audvol_cal_info->acdb_id,
3682 atomic_read(&this_adm.copp.
3683 rate[port_idx][copp_idx]));
3684 if (ret2 < 0) {
3685 pr_debug("%s: remap and send failed for copp Id %d, acdb id %d, app type %d, path %d\n",
3686 __func__, rtac_adm_data.device[i].copp,
3687 audvol_cal_info->acdb_id,
3688 audvol_cal_info->app_type,
3689 audvol_cal_info->path);
3690 ret = ret2;
3691 }
3692 }
3693 }
3694unlock:
3695 mutex_unlock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3696 return ret;
3697}
3698
3699int adm_map_rtac_block(struct rtac_cal_block_data *cal_block)
3700{
3701 int result = 0;
3702
3703 pr_debug("%s:\n", __func__);
3704
3705 if (cal_block == NULL) {
3706 pr_err("%s: cal_block is NULL!\n",
3707 __func__);
3708 result = -EINVAL;
3709 goto done;
3710 }
3711
3712 if (cal_block->cal_data.paddr == 0) {
3713 pr_debug("%s: No address to map!\n",
3714 __func__);
3715 result = -EINVAL;
3716 goto done;
3717 }
3718
3719 if (cal_block->map_data.map_size == 0) {
3720 pr_debug("%s: map size is 0!\n",
3721 __func__);
3722 result = -EINVAL;
3723 goto done;
3724 }
3725
3726 /* valid port ID needed for callback use primary I2S */
3727 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3728 result = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3729 &cal_block->map_data.map_size, 1);
3730 if (result < 0) {
3731 pr_err("%s: RTAC mmap did not work! size = %d result %d\n",
3732 __func__,
3733 cal_block->map_data.map_size, result);
3734 pr_debug("%s: RTAC mmap did not work! addr = 0x%pK, size = %d\n",
3735 __func__,
3736 &cal_block->cal_data.paddr,
3737 cal_block->map_data.map_size);
3738 goto done;
3739 }
3740
3741 cal_block->map_data.map_handle = atomic_read(
3742 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]);
3743done:
3744 return result;
3745}
3746
3747int adm_unmap_rtac_block(uint32_t *mem_map_handle)
3748{
3749 int result = 0;
3750
3751 pr_debug("%s:\n", __func__);
3752
3753 if (mem_map_handle == NULL) {
3754 pr_debug("%s: Map handle is NULL, nothing to unmap\n",
3755 __func__);
3756 goto done;
3757 }
3758
3759 if (*mem_map_handle == 0) {
3760 pr_debug("%s: Map handle is 0, nothing to unmap\n",
3761 __func__);
3762 goto done;
3763 }
3764
3765 if (*mem_map_handle != atomic_read(
3766 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL])) {
3767 pr_err("%s: Map handles do not match! Unmapping RTAC, RTAC map 0x%x, ADM map 0x%x\n",
3768 __func__, *mem_map_handle, atomic_read(
3769 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]));
3770
3771 /* if mismatch use handle passed in to unmap */
3772 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL],
3773 *mem_map_handle);
3774 }
3775
3776 /* valid port ID needed for callback use primary I2S */
3777 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3778 result = adm_memory_unmap_regions();
3779 if (result < 0) {
3780 pr_debug("%s: adm_memory_unmap_regions failed, error %d\n",
3781 __func__, result);
3782 } else {
3783 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL], 0);
3784 *mem_map_handle = 0;
3785 }
3786done:
3787 return result;
3788}
3789
3790static int get_cal_type_index(int32_t cal_type)
3791{
3792 int ret = -EINVAL;
3793
3794 switch (cal_type) {
3795 case ADM_AUDPROC_CAL_TYPE:
3796 ret = ADM_AUDPROC_CAL;
3797 break;
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05303798 case ADM_LSM_AUDPROC_CAL_TYPE:
3799 ret = ADM_LSM_AUDPROC_CAL;
3800 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303801 case ADM_AUDVOL_CAL_TYPE:
3802 ret = ADM_AUDVOL_CAL;
3803 break;
3804 case ADM_CUST_TOPOLOGY_CAL_TYPE:
3805 ret = ADM_CUSTOM_TOP_CAL;
3806 break;
3807 case ADM_RTAC_INFO_CAL_TYPE:
3808 ret = ADM_RTAC_INFO_CAL;
3809 break;
3810 case ADM_RTAC_APR_CAL_TYPE:
3811 ret = ADM_RTAC_APR_CAL;
3812 break;
3813 case ADM_RTAC_AUDVOL_CAL_TYPE:
3814 ret = ADM_RTAC_AUDVOL_CAL;
3815 break;
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07003816 case ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE:
3817 ret = ADM_LSM_AUDPROC_PERSISTENT_CAL;
3818 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303819 default:
3820 pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
3821 }
3822 return ret;
3823}
3824
3825static int adm_alloc_cal(int32_t cal_type, size_t data_size, void *data)
3826{
3827 int ret = 0;
3828 int cal_index;
3829
3830 pr_debug("%s:\n", __func__);
3831
3832 cal_index = get_cal_type_index(cal_type);
3833 if (cal_index < 0) {
3834 pr_err("%s: could not get cal index %d!\n",
3835 __func__, cal_index);
3836 ret = -EINVAL;
3837 goto done;
3838 }
3839
3840 ret = cal_utils_alloc_cal(data_size, data,
3841 this_adm.cal_data[cal_index], 0, NULL);
3842 if (ret < 0) {
3843 pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
3844 __func__, ret, cal_type);
3845 ret = -EINVAL;
3846 goto done;
3847 }
3848done:
3849 return ret;
3850}
3851
3852static int adm_dealloc_cal(int32_t cal_type, size_t data_size, void *data)
3853{
3854 int ret = 0;
3855 int cal_index;
3856
3857 pr_debug("%s:\n", __func__);
3858
3859 cal_index = get_cal_type_index(cal_type);
3860 if (cal_index < 0) {
3861 pr_err("%s: could not get cal index %d!\n",
3862 __func__, cal_index);
3863 ret = -EINVAL;
3864 goto done;
3865 }
3866
3867 ret = cal_utils_dealloc_cal(data_size, data,
3868 this_adm.cal_data[cal_index]);
3869 if (ret < 0) {
3870 pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
3871 __func__, ret, cal_type);
3872 ret = -EINVAL;
3873 goto done;
3874 }
3875done:
3876 return ret;
3877}
3878
3879static int adm_set_cal(int32_t cal_type, size_t data_size, void *data)
3880{
3881 int ret = 0;
3882 int cal_index;
3883
3884 pr_debug("%s:\n", __func__);
3885
3886 cal_index = get_cal_type_index(cal_type);
3887 if (cal_index < 0) {
3888 pr_err("%s: could not get cal index %d!\n",
3889 __func__, cal_index);
3890 ret = -EINVAL;
3891 goto done;
3892 }
3893
3894 ret = cal_utils_set_cal(data_size, data,
3895 this_adm.cal_data[cal_index], 0, NULL);
3896 if (ret < 0) {
3897 pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
3898 __func__, ret, cal_type);
3899 ret = -EINVAL;
3900 goto done;
3901 }
3902
3903 if (cal_index == ADM_CUSTOM_TOP_CAL) {
3904 mutex_lock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3905 this_adm.set_custom_topology = 1;
3906 mutex_unlock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3907 } else if (cal_index == ADM_RTAC_AUDVOL_CAL) {
3908 send_rtac_audvol_cal();
3909 }
3910done:
3911 return ret;
3912}
3913
3914static int adm_map_cal_data(int32_t cal_type,
3915 struct cal_block_data *cal_block)
3916{
3917 int ret = 0;
3918 int cal_index;
3919
3920 pr_debug("%s:\n", __func__);
3921
3922 cal_index = get_cal_type_index(cal_type);
3923 if (cal_index < 0) {
3924 pr_err("%s: could not get cal index %d!\n",
3925 __func__, cal_index);
3926 ret = -EINVAL;
3927 goto done;
3928 }
3929
3930 atomic_set(&this_adm.mem_map_index, cal_index);
3931 ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3932 (uint32_t *)&cal_block->map_data.map_size, 1);
3933 if (ret < 0) {
3934 pr_err("%s: map did not work! cal_type %i ret %d\n",
3935 __func__, cal_index, ret);
3936 ret = -ENODEV;
3937 goto done;
3938 }
3939 cal_block->map_data.q6map_handle = atomic_read(&this_adm.
3940 mem_map_handles[cal_index]);
3941done:
3942 return ret;
3943}
3944
3945static int adm_unmap_cal_data(int32_t cal_type,
3946 struct cal_block_data *cal_block)
3947{
3948 int ret = 0;
3949 int cal_index;
3950
3951 pr_debug("%s:\n", __func__);
3952
3953 cal_index = get_cal_type_index(cal_type);
3954 if (cal_index < 0) {
3955 pr_err("%s: could not get cal index %d!\n",
3956 __func__, cal_index);
3957 ret = -EINVAL;
3958 goto done;
3959 }
3960
3961 if (cal_block == NULL) {
3962 pr_err("%s: Cal block is NULL!\n",
3963 __func__);
3964 goto done;
3965 }
3966
3967 if (cal_block->map_data.q6map_handle == 0) {
3968 pr_err("%s: Map handle is NULL, nothing to unmap\n",
3969 __func__);
3970 goto done;
3971 }
3972
3973 atomic_set(&this_adm.mem_map_handles[cal_index],
3974 cal_block->map_data.q6map_handle);
3975 atomic_set(&this_adm.mem_map_index, cal_index);
3976 ret = adm_memory_unmap_regions();
3977 if (ret < 0) {
3978 pr_err("%s: unmap did not work! cal_type %i ret %d\n",
3979 __func__, cal_index, ret);
3980 ret = -ENODEV;
3981 goto done;
3982 }
3983 cal_block->map_data.q6map_handle = 0;
3984done:
3985 return ret;
3986}
3987
3988static void adm_delete_cal_data(void)
3989{
3990 pr_debug("%s:\n", __func__);
3991
3992 cal_utils_destroy_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data);
3993}
3994
3995static int adm_init_cal_data(void)
3996{
3997 int ret = 0;
3998 struct cal_type_info cal_type_info[] = {
3999 {{ADM_CUST_TOPOLOGY_CAL_TYPE,
4000 {adm_alloc_cal, adm_dealloc_cal, NULL,
4001 adm_set_cal, NULL, NULL} },
4002 {adm_map_cal_data, adm_unmap_cal_data,
4003 cal_utils_match_buf_num} },
4004
4005 {{ADM_AUDPROC_CAL_TYPE,
4006 {adm_alloc_cal, adm_dealloc_cal, NULL,
4007 adm_set_cal, NULL, NULL} },
4008 {adm_map_cal_data, adm_unmap_cal_data,
4009 cal_utils_match_buf_num} },
4010
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05304011 {{ADM_LSM_AUDPROC_CAL_TYPE,
4012 {adm_alloc_cal, adm_dealloc_cal, NULL,
4013 adm_set_cal, NULL, NULL} },
4014 {adm_map_cal_data, adm_unmap_cal_data,
4015 cal_utils_match_buf_num} },
4016
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304017 {{ADM_AUDVOL_CAL_TYPE,
4018 {adm_alloc_cal, adm_dealloc_cal, NULL,
4019 adm_set_cal, NULL, NULL} },
4020 {adm_map_cal_data, adm_unmap_cal_data,
4021 cal_utils_match_buf_num} },
4022
4023 {{ADM_RTAC_INFO_CAL_TYPE,
4024 {NULL, NULL, NULL, NULL, NULL, NULL} },
4025 {NULL, NULL, cal_utils_match_buf_num} },
4026
4027 {{ADM_RTAC_APR_CAL_TYPE,
4028 {NULL, NULL, NULL, NULL, NULL, NULL} },
4029 {NULL, NULL, cal_utils_match_buf_num} },
4030
4031 {{SRS_TRUMEDIA_CAL_TYPE,
4032 {NULL, NULL, NULL, NULL, NULL, NULL} },
4033 {NULL, NULL, cal_utils_match_buf_num} },
4034
4035 {{ADM_RTAC_AUDVOL_CAL_TYPE,
4036 {adm_alloc_cal, adm_dealloc_cal, NULL,
4037 adm_set_cal, NULL, NULL} },
4038 {adm_map_cal_data, adm_unmap_cal_data,
4039 cal_utils_match_buf_num} },
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07004040
4041 {{ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE,
4042 {adm_alloc_cal, adm_dealloc_cal, NULL,
4043 adm_set_cal, NULL, NULL} },
4044 {adm_map_cal_data, adm_unmap_cal_data,
4045 cal_utils_match_buf_num} },
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304046 };
4047 pr_debug("%s:\n", __func__);
4048
4049 ret = cal_utils_create_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data,
4050 cal_type_info);
4051 if (ret < 0) {
4052 pr_err("%s: could not create cal type! ret %d\n",
4053 __func__, ret);
4054 ret = -EINVAL;
4055 goto err;
4056 }
4057
4058 return ret;
4059err:
4060 adm_delete_cal_data();
4061 return ret;
4062}
4063
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304064/**
4065 * adm_set_volume -
4066 * command to set volume on ADM copp
4067 *
4068 * @port_id: Port ID number
4069 * @copp_idx: copp index assigned
4070 * @volume: gain value to set
4071 *
4072 * Returns 0 on success or error on failure
4073 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304074int adm_set_volume(int port_id, int copp_idx, int volume)
4075{
4076 struct audproc_volume_ctrl_master_gain audproc_vol;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004077 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304078 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304079
4080 pr_debug("%s: port_id %d, volume %d\n", __func__, port_id, volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304081
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004082 memset(&audproc_vol, 0, sizeof(audproc_vol));
4083 memset(&param_hdr, 0, sizeof(param_hdr));
4084 param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
4085 param_hdr.instance_id = INSTANCE_ID_0;
4086 param_hdr.param_id = AUDPROC_PARAM_ID_VOL_CTRL_MASTER_GAIN;
4087 param_hdr.param_size = sizeof(audproc_vol);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304088
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304089 audproc_vol.master_gain = volume;
4090
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004091 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4092 (uint8_t *) &audproc_vol);
4093 if (rc)
4094 pr_err("%s: Failed to set volume, err %d\n", __func__, rc);
4095
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304096 return rc;
4097}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304098EXPORT_SYMBOL(adm_set_volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304099
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304100/**
4101 * adm_set_softvolume -
4102 * command to set softvolume
4103 *
4104 * @port_id: Port ID number
4105 * @copp_idx: copp index assigned
4106 * @softvol_param: Params to set for softvolume
4107 *
4108 * Returns 0 on success or error on failure
4109 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304110int adm_set_softvolume(int port_id, int copp_idx,
4111 struct audproc_softvolume_params *softvol_param)
4112{
4113 struct audproc_soft_step_volume_params audproc_softvol;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004114 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304115 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304116
4117 pr_debug("%s: period %d step %d curve %d\n", __func__,
4118 softvol_param->period, softvol_param->step,
4119 softvol_param->rampingcurve);
4120
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004121 memset(&audproc_softvol, 0, sizeof(audproc_softvol));
4122 memset(&param_hdr, 0, sizeof(param_hdr));
4123 param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
4124 param_hdr.instance_id = INSTANCE_ID_0;
4125 param_hdr.param_id = AUDPROC_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
4126 param_hdr.param_size = sizeof(audproc_softvol);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304127
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304128 audproc_softvol.period = softvol_param->period;
4129 audproc_softvol.step = softvol_param->step;
4130 audproc_softvol.ramping_curve = softvol_param->rampingcurve;
4131
4132 pr_debug("%s: period %d, step %d, curve %d\n", __func__,
4133 audproc_softvol.period, audproc_softvol.step,
4134 audproc_softvol.ramping_curve);
4135
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004136 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4137 (uint8_t *) &audproc_softvol);
4138 if (rc)
4139 pr_err("%s: Failed to set soft volume, err %d\n", __func__, rc);
4140
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304141 return rc;
4142}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304143EXPORT_SYMBOL(adm_set_softvolume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304144
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304145/**
4146 * adm_set_mic_gain -
4147 * command to set MIC gain
4148 *
4149 * @port_id: Port ID number
4150 * @copp_idx: copp index assigned
4151 * @volume: gain value to set
4152 *
4153 * Returns 0 on success or error on failure
4154 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304155int adm_set_mic_gain(int port_id, int copp_idx, int volume)
4156{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004157 struct admx_mic_gain mic_gain_params;
4158 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304159 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304160
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004161 pr_debug("%s: Setting mic gain to %d at port_id 0x%x\n", __func__,
4162 volume, port_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304163
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004164 memset(&mic_gain_params, 0, sizeof(mic_gain_params));
4165 memset(&param_hdr, 0, sizeof(param_hdr));
4166 param_hdr.module_id = ADM_MODULE_IDX_MIC_GAIN_CTRL;
4167 param_hdr.instance_id = INSTANCE_ID_0;
4168 param_hdr.param_id = ADM_PARAM_IDX_MIC_GAIN;
4169 param_hdr.param_size = sizeof(mic_gain_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304170
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004171 mic_gain_params.tx_mic_gain = volume;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304172
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004173 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4174 (uint8_t *) &mic_gain_params);
4175 if (rc)
4176 pr_err("%s: Failed to set mic gain, err %d\n", __func__, rc);
4177
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304178 return rc;
4179}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304180EXPORT_SYMBOL(adm_set_mic_gain);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304181
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304182/**
4183 * adm_send_set_multichannel_ec_primary_mic_ch -
4184 * command to set multi-ch EC primary mic
4185 *
4186 * @port_id: Port ID number
4187 * @copp_idx: copp index assigned
4188 * @primary_mic_ch: channel number of primary mic
4189 *
4190 * Returns 0 on success or error on failure
4191 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304192int adm_send_set_multichannel_ec_primary_mic_ch(int port_id, int copp_idx,
4193 int primary_mic_ch)
4194{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004195 struct admx_sec_primary_mic_ch sec_primary_ch_params;
4196 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304197 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304198
4199 pr_debug("%s port_id 0x%x, copp_idx 0x%x, primary_mic_ch %d\n",
4200 __func__, port_id, copp_idx, primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304201
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004202 memset(&sec_primary_ch_params, 0, sizeof(sec_primary_ch_params));
4203 memset(&param_hdr, 0, sizeof(param_hdr));
4204 param_hdr.module_id = AUDPROC_MODULE_ID_VOICE_TX_SECNS;
4205 param_hdr.instance_id = INSTANCE_ID_0;
4206 param_hdr.param_id = AUDPROC_PARAM_IDX_SEC_PRIMARY_MIC_CH;
4207 param_hdr.param_size = sizeof(sec_primary_ch_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304208
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004209 sec_primary_ch_params.version = 0;
4210 sec_primary_ch_params.sec_primary_mic_ch = primary_mic_ch;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304211
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004212 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4213 (uint8_t *) &sec_primary_ch_params);
4214 if (rc)
4215 pr_err("%s: Failed to set primary mic chanel, err %d\n",
4216 __func__, rc);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304217
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304218 return rc;
4219}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304220EXPORT_SYMBOL(adm_send_set_multichannel_ec_primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304221
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304222/**
4223 * adm_param_enable -
4224 * command to send params to ADM for given module
4225 *
4226 * @port_id: Port ID number
4227 * @copp_idx: copp index assigned
4228 * @module_id: ADM module
4229 * @enable: flag to enable or disable module
4230 *
4231 * Returns 0 on success or error on failure
4232 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304233int adm_param_enable(int port_id, int copp_idx, int module_id, int enable)
4234{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004235 struct module_instance_info mod_inst_info;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304236
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004237 memset(&mod_inst_info, 0, sizeof(mod_inst_info));
4238 mod_inst_info.module_id = module_id;
4239 mod_inst_info.instance_id = INSTANCE_ID_0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304240
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004241 return adm_param_enable_v2(port_id, copp_idx, mod_inst_info, enable);
4242}
Vidyakumar Athota98464a22018-03-15 20:39:37 -07004243EXPORT_SYMBOL(adm_param_enable);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004244
4245/**
4246 * adm_param_enable_v2 -
4247 * command to send params to ADM for given module
4248 *
4249 * @port_id: Port ID number
4250 * @copp_idx: copp index assigned
4251 * @mod_inst_info: module and instance ID info
4252 * @enable: flag to enable or disable module
4253 *
4254 * Returns 0 on success or error on failure
4255 */
4256int adm_param_enable_v2(int port_id, int copp_idx,
4257 struct module_instance_info mod_inst_info, int enable)
4258{
4259 uint32_t enable_param;
4260 struct param_hdr_v3 param_hdr;
4261 int rc = 0;
4262
4263 if (enable < 0 || enable > 1) {
4264 pr_err("%s: Invalid value for enable %d\n", __func__, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304265 return -EINVAL;
4266 }
4267
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004268 pr_debug("%s port_id %d, module_id 0x%x, instance_id 0x%x, enable %d\n",
4269 __func__, port_id, mod_inst_info.module_id,
4270 mod_inst_info.instance_id, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304271
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004272 memset(&param_hdr, 0, sizeof(param_hdr));
4273 param_hdr.module_id = mod_inst_info.module_id;
4274 param_hdr.instance_id = mod_inst_info.instance_id;
4275 param_hdr.param_id = AUDPROC_PARAM_ID_ENABLE;
4276 param_hdr.param_size = sizeof(enable_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304277
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004278 enable_param = enable;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304279
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004280 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4281 (uint8_t *) &enable_param);
4282 if (rc)
4283 pr_err("%s: Failed to set enable of module(%d) instance(%d) to %d, err %d\n",
4284 __func__, mod_inst_info.module_id,
4285 mod_inst_info.instance_id, enable, rc);
4286
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304287 return rc;
4288
4289}
Vidyakumar Athota98464a22018-03-15 20:39:37 -07004290EXPORT_SYMBOL(adm_param_enable_v2);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304291
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304292/**
4293 * adm_send_calibration -
4294 * send ADM calibration to DSP
4295 *
4296 * @port_id: Port ID number
4297 * @copp_idx: copp index assigned
4298 * @path: direction or ADM path type
4299 * @perf_mode: performance mode like LL/ULL/..
4300 * @cal_type: calibration type to use
4301 * @params: pointer with cal data
4302 * @size: cal size
4303 *
4304 * Returns 0 on success or error on failure
4305 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304306int adm_send_calibration(int port_id, int copp_idx, int path, int perf_mode,
4307 int cal_type, char *params, int size)
4308{
4309
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004310 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304311
4312 pr_debug("%s:port_id %d, path %d, perf_mode %d, cal_type %d, size %d\n",
4313 __func__, port_id, path, perf_mode, cal_type, size);
4314
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304315 /* Maps audio_dev_ctrl path definition to ACDB definition */
4316 if (get_cal_path(path) != RX_DEVICE) {
4317 pr_err("%s: acdb_path %d\n", __func__, path);
4318 rc = -EINVAL;
4319 goto end;
4320 }
4321
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004322 rc = adm_set_pp_params(port_id, copp_idx, NULL, (u8 *) params, size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304323
4324end:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304325 return rc;
4326}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304327EXPORT_SYMBOL(adm_send_calibration);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304328
4329/*
4330 * adm_update_wait_parameters must be called with routing driver locks.
4331 * adm_reset_wait_parameters must be called with routing driver locks.
4332 * set and reset parmeters are separated to make sure it is always called
4333 * under routing driver lock.
4334 * adm_wait_timeout is to block until timeout or interrupted. Timeout is
4335 * not a an error.
4336 */
4337int adm_set_wait_parameters(int port_id, int copp_idx)
4338{
4339
4340 int ret = 0, port_idx;
4341
4342 pr_debug("%s: port_id 0x%x, copp_idx %d\n", __func__, port_id,
4343 copp_idx);
4344 port_id = afe_convert_virtual_to_portid(port_id);
4345 port_idx = adm_validate_and_get_port_index(port_id);
4346 if (port_idx < 0) {
4347 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4348 ret = -EINVAL;
4349 goto end;
4350 }
4351
4352 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4353 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4354 return -EINVAL;
4355 }
4356
4357 this_adm.copp.adm_delay[port_idx][copp_idx] = 1;
4358 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 0);
4359
4360end:
4361 return ret;
4362
4363}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304364EXPORT_SYMBOL(adm_set_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304365
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304366/**
4367 * adm_reset_wait_parameters -
4368 * reset wait parameters or ADM delay value
4369 *
4370 * @port_id: Port ID number
4371 * @copp_idx: copp index assigned
4372 *
4373 * Returns 0 on success or error on failure
4374 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304375int adm_reset_wait_parameters(int port_id, int copp_idx)
4376{
4377 int ret = 0, port_idx;
4378
4379 pr_debug("%s: port_id 0x%x copp_idx %d\n", __func__, port_id,
4380 copp_idx);
4381 port_id = afe_convert_virtual_to_portid(port_id);
4382 port_idx = adm_validate_and_get_port_index(port_id);
4383 if (port_idx < 0) {
4384 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4385 ret = -EINVAL;
4386 goto end;
4387 }
4388
4389 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4390 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4391 return -EINVAL;
4392 }
4393
4394 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 1);
4395 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
4396
4397end:
4398 return ret;
4399}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304400EXPORT_SYMBOL(adm_reset_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304401
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304402/**
4403 * adm_wait_timeout -
4404 * ADM wait command after command send to DSP
4405 *
4406 * @port_id: Port ID number
4407 * @copp_idx: copp index assigned
4408 * @wait_time: value in ms for command timeout
4409 *
4410 * Returns 0 on success or error on failure
4411 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304412int adm_wait_timeout(int port_id, int copp_idx, int wait_time)
4413{
4414 int ret = 0, port_idx;
4415
4416 pr_debug("%s: port_id 0x%x, copp_idx %d, wait_time %d\n", __func__,
4417 port_id, copp_idx, wait_time);
4418 port_id = afe_convert_virtual_to_portid(port_id);
4419 port_idx = adm_validate_and_get_port_index(port_id);
4420 if (port_idx < 0) {
4421 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4422 ret = -EINVAL;
4423 goto end;
4424 }
4425
4426 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4427 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4428 return -EINVAL;
4429 }
4430
4431 ret = wait_event_timeout(
4432 this_adm.copp.adm_delay_wait[port_idx][copp_idx],
4433 atomic_read(&this_adm.copp.adm_delay_stat[port_idx][copp_idx]),
4434 msecs_to_jiffies(wait_time));
4435 pr_debug("%s: return %d\n", __func__, ret);
4436 if (ret != 0)
4437 ret = -EINTR;
4438end:
4439 pr_debug("%s: return %d--\n", __func__, ret);
4440 return ret;
4441}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304442EXPORT_SYMBOL(adm_wait_timeout);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304443
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304444/**
4445 * adm_store_cal_data -
4446 * Retrieve calibration data for ADM copp device
4447 *
4448 * @port_id: Port ID number
4449 * @copp_idx: copp index assigned
4450 * @path: direction or copp type
4451 * @perf_mode: performance mode like LL/ULL/..
4452 * @cal_index: calibration index to use
4453 * @params: pointer to store cal data
4454 * @size: pointer to fill with cal size
4455 *
4456 * Returns 0 on success or error on failure
4457 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304458int adm_store_cal_data(int port_id, int copp_idx, int path, int perf_mode,
4459 int cal_index, char *params, int *size)
4460{
4461 int rc = 0;
4462 struct cal_block_data *cal_block = NULL;
4463 int app_type, acdb_id, port_idx, sample_rate;
4464
4465 if (this_adm.cal_data[cal_index] == NULL) {
4466 pr_debug("%s: cal_index %d not allocated!\n",
4467 __func__, cal_index);
4468 goto end;
4469 }
4470
4471 if (get_cal_path(path) != RX_DEVICE) {
4472 pr_debug("%s: Invalid path to store calibration %d\n",
4473 __func__, path);
4474 rc = -EINVAL;
4475 goto end;
4476 }
4477
4478 port_id = afe_convert_virtual_to_portid(port_id);
4479 port_idx = adm_validate_and_get_port_index(port_id);
4480 if (port_idx < 0) {
4481 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
4482 rc = -EINVAL;
4483 goto end;
4484 }
4485
4486 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4487 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4488 return -EINVAL;
4489 }
4490
4491 acdb_id = atomic_read(&this_adm.copp.acdb_id[port_idx][copp_idx]);
4492 app_type = atomic_read(&this_adm.copp.app_type[port_idx][copp_idx]);
4493 sample_rate = atomic_read(&this_adm.copp.rate[port_idx][copp_idx]);
4494
4495 mutex_lock(&this_adm.cal_data[cal_index]->lock);
4496 cal_block = adm_find_cal(cal_index, get_cal_path(path), app_type,
4497 acdb_id, sample_rate);
4498 if (cal_block == NULL)
4499 goto unlock;
4500
4501 if (cal_block->cal_data.size <= 0) {
4502 pr_debug("%s: No ADM cal send for port_id = 0x%x!\n",
4503 __func__, port_id);
4504 rc = -EINVAL;
4505 goto unlock;
4506 }
4507
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05304508 if (cal_index == ADM_AUDPROC_CAL || cal_index == ADM_LSM_AUDPROC_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304509 if (cal_block->cal_data.size > AUD_PROC_BLOCK_SIZE) {
4510 pr_err("%s:audproc:invalid size exp/actual[%zd, %d]\n",
4511 __func__, cal_block->cal_data.size, *size);
4512 rc = -ENOMEM;
4513 goto unlock;
4514 }
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07004515 } else if (cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
4516 if (cal_block->cal_data.size > AUD_PROC_PERSIST_BLOCK_SIZE) {
4517 pr_err("%s:persist invalid size exp/actual[%zd, %d]\n",
4518 __func__, cal_block->cal_data.size, *size);
4519 rc = -ENOMEM;
4520 goto unlock;
4521 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304522 } else if (cal_index == ADM_AUDVOL_CAL) {
4523 if (cal_block->cal_data.size > AUD_VOL_BLOCK_SIZE) {
4524 pr_err("%s:aud_vol:invalid size exp/actual[%zd, %d]\n",
4525 __func__, cal_block->cal_data.size, *size);
4526 rc = -ENOMEM;
4527 goto unlock;
4528 }
4529 } else {
4530 pr_debug("%s: Not valid calibration for dolby topolgy\n",
4531 __func__);
4532 rc = -EINVAL;
4533 goto unlock;
4534 }
4535 memcpy(params, cal_block->cal_data.kvaddr, cal_block->cal_data.size);
4536 *size = cal_block->cal_data.size;
4537
4538 pr_debug("%s:port_id %d, copp_idx %d, path %d",
4539 __func__, port_id, copp_idx, path);
4540 pr_debug("perf_mode %d, cal_type %d, size %d\n",
4541 perf_mode, cal_index, *size);
4542
4543unlock:
4544 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
4545end:
4546 return rc;
4547}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304548EXPORT_SYMBOL(adm_store_cal_data);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304549
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304550/**
4551 * adm_send_compressed_device_mute -
4552 * command to send mute for compressed device
4553 *
4554 * @port_id: Port ID number
4555 * @copp_idx: copp index assigned
4556 * @mute_on: flag to indicate mute or unmute
4557 *
4558 * Returns 0 on success or error on failure
4559 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304560int adm_send_compressed_device_mute(int port_id, int copp_idx, bool mute_on)
4561{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004562 u32 mute_param = mute_on ? 1 : 0;
4563 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304564 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304565
4566 pr_debug("%s port_id: 0x%x, copp_idx %d, mute_on: %d\n",
4567 __func__, port_id, copp_idx, mute_on);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304568
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004569 memset(&param_hdr, 0, sizeof(param_hdr));
4570 param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_MUTE;
4571 param_hdr.instance_id = INSTANCE_ID_0;
4572 param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_MUTE;
4573 param_hdr.param_size = sizeof(mute_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304574
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004575 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4576 (uint8_t *) &mute_param);
4577 if (ret)
4578 pr_err("%s: Failed to set mute, err %d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304579
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304580 return ret;
4581}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304582EXPORT_SYMBOL(adm_send_compressed_device_mute);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304583
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304584/**
4585 * adm_send_compressed_device_latency -
4586 * command to send latency for compressed device
4587 *
4588 * @port_id: Port ID number
4589 * @copp_idx: copp index assigned
4590 * @latency: latency value to pass
4591 *
4592 * Returns 0 on success or error on failure
4593 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304594int adm_send_compressed_device_latency(int port_id, int copp_idx, int latency)
4595{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004596 u32 latency_param;
4597 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304598 int ret = 0;
4599
4600 pr_debug("%s port_id: 0x%x, copp_idx %d latency: %d\n", __func__,
4601 port_id, copp_idx, latency);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004602
4603 if (latency < 0) {
4604 pr_err("%s: Invalid value for latency %d", __func__, latency);
4605 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304606 }
4607
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004608 memset(&param_hdr, 0, sizeof(param_hdr));
4609 param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_LATENCY;
4610 param_hdr.instance_id = INSTANCE_ID_0;
4611 param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_LATENCY;
4612 param_hdr.param_size = sizeof(latency_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304613
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004614 latency_param = latency;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304615
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004616 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4617 (uint8_t *) &latency_param);
4618 if (ret)
4619 pr_err("%s: Failed to set latency, err %d\n", __func__, ret);
4620
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304621 return ret;
4622}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304623EXPORT_SYMBOL(adm_send_compressed_device_latency);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304624
4625/**
4626 * adm_swap_speaker_channels
4627 *
4628 * Receives port_id, copp_idx, sample rate, spk_swap and
4629 * send MFC command to swap speaker channel.
4630 * Return zero on success. On failure returns nonzero.
4631 *
4632 * port_id - Passed value, port_id for which channels swap is wanted
4633 * copp_idx - Passed value, copp_idx for which channels swap is wanted
4634 * sample_rate - Passed value, sample rate used by app type config
4635 * spk_swap - Passed value, spk_swap for check if swap flag is set
4636 */
4637int adm_swap_speaker_channels(int port_id, int copp_idx,
4638 int sample_rate, bool spk_swap)
4639{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004640 struct audproc_mfc_param_media_fmt mfc_cfg;
4641 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304642 uint16_t num_channels;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004643 int port_idx = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304644 int ret = 0;
4645
4646 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4647 __func__, port_id, copp_idx);
4648 port_id = q6audio_convert_virtual_to_portid(port_id);
4649 port_idx = adm_validate_and_get_port_index(port_id);
4650 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
4651 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004652 return -EINVAL;
4653 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4654 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
4655 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304656 }
4657
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004658 num_channels = atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304659 if (num_channels != 2) {
4660 pr_debug("%s: Invalid number of channels: %d\n",
4661 __func__, num_channels);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004662 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304663 }
4664
4665 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004666 memset(&param_hdr, 0, sizeof(param_hdr));
4667
4668 param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
4669 param_hdr.instance_id = INSTANCE_ID_0;
4670 param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
4671 param_hdr.param_size = sizeof(mfc_cfg);
4672
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304673 mfc_cfg.sampling_rate = sample_rate;
4674 mfc_cfg.bits_per_sample =
4675 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
4676 mfc_cfg.num_channels = num_channels;
4677
4678 /* Currently applying speaker swap for only 2 channel use case */
4679 if (spk_swap) {
4680 mfc_cfg.channel_type[0] =
4681 (uint16_t) PCM_CHANNEL_FR;
4682 mfc_cfg.channel_type[1] =
4683 (uint16_t) PCM_CHANNEL_FL;
4684 } else {
4685 mfc_cfg.channel_type[0] =
4686 (uint16_t) PCM_CHANNEL_FL;
4687 mfc_cfg.channel_type[1] =
4688 (uint16_t) PCM_CHANNEL_FR;
4689 }
4690
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004691 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4692 (u8 *) &mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304693 if (ret < 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004694 pr_err("%s: Failed to set swap speaker channels on port[0x%x] failed %d\n",
4695 __func__, port_id, ret);
4696 return ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304697 }
4698
4699 pr_debug("%s: mfc_cfg Set params returned success", __func__);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004700 return 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304701}
4702EXPORT_SYMBOL(adm_swap_speaker_channels);
4703
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304704/**
4705 * adm_set_sound_focus -
4706 * Update sound focus info
4707 *
4708 * @port_id: Port ID number
4709 * @copp_idx: copp index assigned
4710 * @soundFocusData: sound focus data to pass
4711 *
4712 * Returns 0 on success or error on failure
4713 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304714int adm_set_sound_focus(int port_id, int copp_idx,
4715 struct sound_focus_param soundFocusData)
4716{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004717 struct adm_param_fluence_soundfocus_t soundfocus_params;
4718 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304719 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304720 int i;
4721
4722 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4723 __func__, port_id, copp_idx);
4724
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004725 memset(&param_hdr, 0, sizeof(param_hdr));
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07004726 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004727 param_hdr.instance_id = INSTANCE_ID_0;
4728 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
4729 param_hdr.param_size = sizeof(soundfocus_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304730
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004731 memset(&(soundfocus_params), 0xFF, sizeof(soundfocus_params));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304732 for (i = 0; i < MAX_SECTORS; i++) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004733 soundfocus_params.start_angles[i] =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304734 soundFocusData.start_angle[i];
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004735 soundfocus_params.enables[i] = soundFocusData.enable[i];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304736 pr_debug("%s: start_angle[%d] = %d\n",
4737 __func__, i, soundFocusData.start_angle[i]);
4738 pr_debug("%s: enable[%d] = %d\n",
4739 __func__, i, soundFocusData.enable[i]);
4740 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004741 soundfocus_params.gain_step = soundFocusData.gain_step;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304742 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData.gain_step);
4743
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004744 soundfocus_params.reserved = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304745
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004746 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4747 (uint8_t *) &soundfocus_params);
4748 if (ret)
4749 pr_err("%s: Failed to set sound focus params, err %d\n",
4750 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304751
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304752 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
4753
4754 return ret;
4755}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304756EXPORT_SYMBOL(adm_set_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304757
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304758/**
4759 * adm_get_sound_focus -
4760 * Retrieve sound focus info
4761 *
4762 * @port_id: Port ID number
4763 * @copp_idx: copp index assigned
4764 * @soundFocusData: pointer for sound focus data to be updated with
4765 *
4766 * Returns 0 on success or error on failure
4767 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304768int adm_get_sound_focus(int port_id, int copp_idx,
4769 struct sound_focus_param *soundFocusData)
4770{
4771 int ret = 0, i;
4772 char *params_value;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004773 uint32_t max_param_size = 0;
4774 struct adm_param_fluence_soundfocus_t *soundfocus_params = NULL;
4775 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304776
4777 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4778 __func__, port_id, copp_idx);
4779
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004780 max_param_size = sizeof(struct adm_param_fluence_soundfocus_t) +
4781 sizeof(union param_hdrs);
4782 params_value = kzalloc(max_param_size, GFP_KERNEL);
4783 if (!params_value)
4784 return -ENOMEM;
4785
4786 memset(&param_hdr, 0, sizeof(param_hdr));
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07004787 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004788 param_hdr.instance_id = INSTANCE_ID_0;
4789 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
4790 param_hdr.param_size = max_param_size;
4791 ret = adm_get_pp_params(port_id, copp_idx,
4792 ADM_CLIENT_ID_SOURCE_TRACKING, NULL, &param_hdr,
4793 params_value);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304794 if (ret) {
4795 pr_err("%s: get parameters failed ret:%d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304796 ret = -EINVAL;
4797 goto done;
4798 }
4799
4800 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
4801 pr_err("%s - get params returned error [%s]\n",
4802 __func__, adsp_err_get_err_str(
4803 this_adm.sourceTrackingData.apr_cmd_status));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304804 ret = adsp_err_get_lnx_err_code(
4805 this_adm.sourceTrackingData.apr_cmd_status);
4806 goto done;
4807 }
4808
4809 soundfocus_params = (struct adm_param_fluence_soundfocus_t *)
4810 params_value;
4811 for (i = 0; i < MAX_SECTORS; i++) {
4812 soundFocusData->start_angle[i] =
4813 soundfocus_params->start_angles[i];
4814 soundFocusData->enable[i] = soundfocus_params->enables[i];
4815 pr_debug("%s: start_angle[%d] = %d\n",
4816 __func__, i, soundFocusData->start_angle[i]);
4817 pr_debug("%s: enable[%d] = %d\n",
4818 __func__, i, soundFocusData->enable[i]);
4819 }
4820 soundFocusData->gain_step = soundfocus_params->gain_step;
4821 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData->gain_step);
4822
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304823done:
4824 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
4825
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004826 kfree(params_value);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304827 return ret;
4828}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304829EXPORT_SYMBOL(adm_get_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304830
4831static int adm_source_tracking_alloc_map_memory(void)
4832{
4833 int ret;
4834
4835 pr_debug("%s: Enter\n", __func__);
4836
Banajit Goswami08bb7362017-11-03 22:48:23 -07004837 ret = msm_audio_ion_alloc(&this_adm.sourceTrackingData.dma_buf,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304838 AUD_PROC_BLOCK_SIZE,
4839 &this_adm.sourceTrackingData.memmap.paddr,
4840 &this_adm.sourceTrackingData.memmap.size,
4841 &this_adm.sourceTrackingData.memmap.kvaddr);
4842 if (ret) {
4843 pr_err("%s: failed to allocate memory\n", __func__);
4844
4845 ret = -EINVAL;
4846 goto done;
4847 }
4848
4849 atomic_set(&this_adm.mem_map_index, ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
4850 ret = adm_memory_map_regions(&this_adm.sourceTrackingData.memmap.paddr,
4851 0,
4852 (uint32_t *)&this_adm.sourceTrackingData.memmap.size,
4853 1);
4854 if (ret < 0) {
4855 pr_err("%s: failed to map memory, paddr = 0x%pK, size = %d\n",
4856 __func__,
4857 (void *)this_adm.sourceTrackingData.memmap.paddr,
4858 (uint32_t)this_adm.sourceTrackingData.memmap.size);
4859
Banajit Goswami08bb7362017-11-03 22:48:23 -07004860 msm_audio_ion_free(this_adm.sourceTrackingData.dma_buf);
4861 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304862 this_adm.sourceTrackingData.memmap.size = 0;
4863 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
4864 this_adm.sourceTrackingData.memmap.paddr = 0;
4865 this_adm.sourceTrackingData.apr_cmd_status = -1;
4866 atomic_set(&this_adm.mem_map_handles
4867 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
4868
4869 ret = -EINVAL;
4870 goto done;
4871 }
4872 ret = 0;
4873 pr_debug("%s: paddr = 0x%pK, size = %d, mem_map_handle = 0x%x\n",
4874 __func__, (void *)this_adm.sourceTrackingData.memmap.paddr,
4875 (uint32_t)this_adm.sourceTrackingData.memmap.size,
4876 atomic_read(&this_adm.mem_map_handles
4877 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING]));
4878
4879done:
4880 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
4881
4882 return ret;
4883}
4884
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304885/**
4886 * adm_get_source_tracking -
4887 * Retrieve source tracking info
4888 *
4889 * @port_id: Port ID number
4890 * @copp_idx: copp index assigned
4891 * @sourceTrackingData: pointer for source track data to be updated with
4892 *
4893 * Returns 0 on success or error on failure
4894 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304895int adm_get_source_tracking(int port_id, int copp_idx,
4896 struct source_tracking_param *sourceTrackingData)
4897{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004898 struct adm_param_fluence_sourcetracking_t *source_tracking_params =
4899 NULL;
4900 struct mem_mapping_hdr mem_hdr;
4901 struct param_hdr_v3 param_hdr;
4902 int i = 0;
4903 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304904
4905 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4906 __func__, port_id, copp_idx);
4907
4908 if (!this_adm.sourceTrackingData.memmap.paddr) {
4909 /* Allocate and map shared memory for out of band usage */
4910 ret = adm_source_tracking_alloc_map_memory();
4911 if (ret != 0) {
4912 ret = -EINVAL;
4913 goto done;
4914 }
4915 }
4916
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004917 memset(&mem_hdr, 0, sizeof(mem_hdr));
4918 memset(&param_hdr, 0, sizeof(param_hdr));
4919 mem_hdr.data_payload_addr_lsw =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304920 lower_32_bits(this_adm.sourceTrackingData.memmap.paddr);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004921 mem_hdr.data_payload_addr_msw = msm_audio_populate_upper_32_bits(
4922 this_adm.sourceTrackingData.memmap.paddr);
4923 mem_hdr.mem_map_handle = atomic_read(
4924 &this_adm.mem_map_handles[ADM_MEM_MAP_INDEX_SOURCE_TRACKING]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304925
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07004926 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004927 param_hdr.instance_id = INSTANCE_ID_0;
4928 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOURCETRACKING;
4929 /*
4930 * This size should be the max size of the calibration data + header.
4931 * Use the union size to ensure max size is used.
4932 */
4933 param_hdr.param_size =
4934 sizeof(struct adm_param_fluence_sourcetracking_t) +
4935 sizeof(union param_hdrs);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304936
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004937 /*
4938 * Retrieving parameters out of band, so no need to provide a buffer for
4939 * the returned parameter data as it will be at the memory location
4940 * provided.
4941 */
4942 ret = adm_get_pp_params(port_id, copp_idx,
4943 ADM_CLIENT_ID_SOURCE_TRACKING, &mem_hdr,
4944 &param_hdr, NULL);
4945 if (ret) {
4946 pr_err("%s: Failed to get params, error %d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304947 goto done;
4948 }
4949
4950 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
4951 pr_err("%s - get params returned error [%s]\n",
4952 __func__, adsp_err_get_err_str(
4953 this_adm.sourceTrackingData.apr_cmd_status));
4954
4955 ret = adsp_err_get_lnx_err_code(
4956 this_adm.sourceTrackingData.apr_cmd_status);
4957 goto done;
4958 }
4959
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004960 /* How do we know what the param data was retrieved with for hdr size */
4961 source_tracking_params =
4962 (struct adm_param_fluence_sourcetracking_t
4963 *) (this_adm.sourceTrackingData.memmap.kvaddr +
4964 sizeof(struct param_hdr_v1));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304965 for (i = 0; i < MAX_SECTORS; i++) {
4966 sourceTrackingData->vad[i] = source_tracking_params->vad[i];
4967 pr_debug("%s: vad[%d] = %d\n",
4968 __func__, i, sourceTrackingData->vad[i]);
4969 }
4970 sourceTrackingData->doa_speech = source_tracking_params->doa_speech;
4971 pr_debug("%s: doa_speech = %d\n",
4972 __func__, sourceTrackingData->doa_speech);
4973
4974 for (i = 0; i < MAX_NOISE_SOURCE_INDICATORS; i++) {
4975 sourceTrackingData->doa_noise[i] =
4976 source_tracking_params->doa_noise[i];
4977 pr_debug("%s: doa_noise[%d] = %d\n",
4978 __func__, i, sourceTrackingData->doa_noise[i]);
4979 }
4980 for (i = 0; i < MAX_POLAR_ACTIVITY_INDICATORS; i++) {
4981 sourceTrackingData->polar_activity[i] =
4982 source_tracking_params->polar_activity[i];
4983 pr_debug("%s: polar_activity[%d] = %d\n",
4984 __func__, i, sourceTrackingData->polar_activity[i]);
4985 }
4986
4987 ret = 0;
4988
4989done:
4990 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
4991
4992 return ret;
4993}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304994EXPORT_SYMBOL(adm_get_source_tracking);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304995
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304996int __init adm_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304997{
4998 int i = 0, j;
4999
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305000 this_adm.ec_ref_rx = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305001 init_waitqueue_head(&this_adm.matrix_map_wait);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305002 init_waitqueue_head(&this_adm.adm_wait);
5003
5004 for (i = 0; i < AFE_MAX_PORTS; i++) {
5005 for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
5006 atomic_set(&this_adm.copp.id[i][j], RESET_COPP_ID);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305007 init_waitqueue_head(&this_adm.copp.wait[i][j]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305008 init_waitqueue_head(
5009 &this_adm.copp.adm_delay_wait[i][j]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305010 }
5011 }
5012
5013 if (adm_init_cal_data())
5014 pr_err("%s: could not init cal data!\n", __func__);
5015
Banajit Goswami08bb7362017-11-03 22:48:23 -07005016 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305017 this_adm.sourceTrackingData.memmap.size = 0;
5018 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
5019 this_adm.sourceTrackingData.memmap.paddr = 0;
5020 this_adm.sourceTrackingData.apr_cmd_status = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305021
5022 return 0;
5023}
5024
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05305025void adm_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305026{
Laxminath Kasam30ad7512017-11-28 12:40:22 +05305027 if (this_adm.apr)
5028 adm_reset_data();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305029 adm_delete_cal_data();
5030}