blob: fcc1fa13184ddafa464dc9d92052110552b3c6d4 [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
Asish Bhattacharya34504582017-08-08 12:55:01 +05302836 /*
2837 * Routing driver reuses the same adm for streams with the same
2838 * app_type, sample_rate etc.
2839 * This isn't allowed for ULL streams as per the DSP interface
2840 */
2841 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE)
2842 copp_idx = adm_get_idx_if_copp_exists(port_idx, topology,
2843 perf_mode,
2844 rate, bit_width,
2845 app_type);
2846
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302847 if (copp_idx < 0) {
2848 copp_idx = adm_get_next_available_copp(port_idx);
2849 if (copp_idx >= MAX_COPPS_PER_PORT) {
2850 pr_err("%s: exceeded copp id %d\n",
2851 __func__, copp_idx);
2852 return -EINVAL;
2853 }
2854 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
2855 atomic_set(&this_adm.copp.topology[port_idx][copp_idx],
2856 topology);
2857 atomic_set(&this_adm.copp.mode[port_idx][copp_idx],
2858 perf_mode);
2859 atomic_set(&this_adm.copp.rate[port_idx][copp_idx],
2860 rate);
2861 atomic_set(&this_adm.copp.channels[port_idx][copp_idx],
2862 channel_mode);
2863 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx],
2864 bit_width);
2865 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx],
2866 app_type);
2867 atomic_set(&this_adm.copp.acdb_id[port_idx][copp_idx],
2868 acdb_id);
2869 set_bit(ADM_STATUS_CALIBRATION_REQUIRED,
2870 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
2871 if ((path != ADM_PATH_COMPRESSED_RX) &&
2872 (path != ADM_PATH_COMPRESSED_TX))
2873 send_adm_custom_topology();
2874 }
2875
2876 if (this_adm.copp.adm_delay[port_idx][copp_idx] &&
2877 perf_mode == LEGACY_PCM_MODE) {
2878 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
2879 1);
2880 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
2881 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
2882 }
2883
2884 /* Create a COPP if port id are not enabled */
2885 if (atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]) == 0) {
2886 pr_debug("%s: open ADM: port_idx: %d, copp_idx: %d\n", __func__,
2887 port_idx, copp_idx);
Dieter Luecking50c55352018-09-28 14:29:17 +02002888 if ((topology == SRS_TRUMEDIA_TOPOLOGY_ID) &&
2889 perf_mode == LEGACY_PCM_MODE) {
2890 int res;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302891
Dieter Luecking50c55352018-09-28 14:29:17 +02002892 atomic_set(&this_adm.mem_map_index, ADM_SRS_TRUMEDIA);
2893 msm_dts_srs_tm_ion_memmap(&this_adm.outband_memmap);
2894 res = adm_memory_map_regions(
2895 &this_adm.outband_memmap.paddr, 0,
2896 (uint32_t *)&this_adm.outband_memmap.size, 1);
2897 if (res < 0) {
2898 pr_err("%s: SRS adm_memory_map_regions failed! addr = 0x%pK, size = %d\n",
2899 __func__,
2900 (void *)this_adm.outband_memmap.paddr,
2901 (uint32_t)this_adm.outband_memmap.size);
2902 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302903 }
2904
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302905
Mangesh Kunchamwar9f295c72018-10-08 18:20:41 +05302906 if ((q6core_get_avcs_api_version_per_service(
Dieter Luecking50c55352018-09-28 14:29:17 +02002907 APRV2_IDS_SERVICE_ID_ADSP_ADM_V) >=
Mangesh Kunchamwar9f295c72018-10-08 18:20:41 +05302908 ADSP_ADM_API_VERSION_V3) &&
2909 q6core_use_Q6_32ch_support()) {
Dieter Luecking50c55352018-09-28 14:29:17 +02002910 memset(&open_v8, 0, sizeof(open_v8));
2911 memset(&ep1_payload, 0, sizeof(ep1_payload));
2912 memset(&ep2_payload, 0, sizeof(ep2_payload));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302913
Dieter Luecking50c55352018-09-28 14:29:17 +02002914 open_v8.hdr.hdr_field = APR_HDR_FIELD(
2915 APR_MSG_TYPE_SEQ_CMD,
2916 APR_HDR_LEN(APR_HDR_SIZE),
2917 APR_PKT_VER);
2918 open_v8.hdr.src_svc = APR_SVC_ADM;
2919 open_v8.hdr.src_domain = APR_DOMAIN_APPS;
2920 open_v8.hdr.src_port = tmp_port;
2921 open_v8.hdr.dest_svc = APR_SVC_ADM;
2922 open_v8.hdr.dest_domain = APR_DOMAIN_ADSP;
2923 open_v8.hdr.dest_port = tmp_port;
2924 open_v8.hdr.token = port_idx << 16 | copp_idx;
2925 open_v8.hdr.opcode = ADM_CMD_DEVICE_OPEN_V8;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302926
Dieter Luecking50c55352018-09-28 14:29:17 +02002927 if (this_adm.native_mode != 0) {
2928 open_v8.flags = flags |
2929 (this_adm.native_mode << 11);
2930 this_adm.native_mode = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302931 } else {
Dieter Luecking50c55352018-09-28 14:29:17 +02002932 open_v8.flags = flags;
2933 }
2934 open_v8.mode_of_operation = path;
2935 open_v8.endpoint_id_1 = tmp_port;
2936 open_v8.endpoint_id_2 = 0xFFFF;
2937 open_v8.endpoint_id_3 = 0xFFFF;
2938
2939 if (this_adm.ec_ref_rx && (path != 1)) {
2940 open_v8.endpoint_id_2 = this_adm.ec_ref_rx;
2941 this_adm.ec_ref_rx = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302942 }
2943
Dieter Luecking50c55352018-09-28 14:29:17 +02002944 open_v8.topology_id = topology;
2945 open_v8.reserved = 0;
2946
2947 /* variable endpoint payload */
2948 ep1_payload.dev_num_channel = channel_mode & 0x00FF;
2949 ep1_payload.bit_width = bit_width;
2950 ep1_payload.sample_rate = rate;
2951 ret = adm_arrange_mch_map_v8(&ep1_payload, path,
2952 channel_mode);
2953 if (ret)
2954 return ret;
2955
2956 pr_debug("%s: port_id=0x%x %x %x topology_id=0x%X flags %x ref_ch %x\n",
2957 __func__, open_v8.endpoint_id_1,
2958 open_v8.endpoint_id_2,
2959 open_v8.endpoint_id_3,
2960 open_v8.topology_id,
2961 open_v8.flags,
2962 this_adm.num_ec_ref_rx_chans);
2963
2964 ep1_payload_size = 8 +
2965 roundup(ep1_payload.dev_num_channel, 4);
2966 param_size = sizeof(struct adm_cmd_device_open_v8)
2967 + ep1_payload_size;
2968 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2969
2970 if ((this_adm.num_ec_ref_rx_chans != 0) && (path != 1)
2971 && (open_v8.endpoint_id_2 != 0xFFFF)) {
2972 ep2_payload.dev_num_channel =
2973 this_adm.num_ec_ref_rx_chans;
2974 this_adm.num_ec_ref_rx_chans = 0;
2975
2976 if (this_adm.ec_ref_rx_bit_width != 0) {
2977 ep2_payload.bit_width =
2978 this_adm.ec_ref_rx_bit_width;
2979 this_adm.ec_ref_rx_bit_width = 0;
2980 } else {
2981 ep2_payload.bit_width = bit_width;
2982 }
2983
2984 if (this_adm.ec_ref_rx_sampling_rate != 0) {
2985 ep2_payload.sample_rate =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302986 this_adm.ec_ref_rx_sampling_rate;
Dieter Luecking50c55352018-09-28 14:29:17 +02002987 this_adm.ec_ref_rx_sampling_rate = 0;
2988 } else {
2989 ep2_payload.sample_rate = rate;
2990 }
2991
2992 pr_debug("%s: adm open_v8 eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
2993 __func__,
2994 ep2_payload.dev_num_channel,
2995 ep2_payload.bit_width,
2996 ep2_payload.sample_rate);
2997
2998 ret = adm_arrange_mch_ep2_map_v8(&ep2_payload,
2999 ep2_payload.dev_num_channel);
3000
3001 if (ret)
3002 return ret;
3003 ep2_payload_size = 8 +
3004 roundup(ep2_payload.dev_num_channel, 4);
3005 param_size += ep2_payload_size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303006 }
3007
Dieter Luecking50c55352018-09-28 14:29:17 +02003008 adm_params = kzalloc(param_size, GFP_KERNEL);
3009 if (!adm_params)
3010 return -ENOMEM;
3011 open_v8.hdr.pkt_size = param_size;
3012 memcpy(adm_params, &open_v8, sizeof(open_v8));
3013 memcpy(adm_params + sizeof(open_v8),
3014 (void *)&ep1_payload,
3015 ep1_payload_size);
3016 memcpy(adm_params + sizeof(open_v8)
3017 + ep1_payload_size,
3018 (void *)&ep2_payload,
3019 ep2_payload_size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303020
Dieter Luecking50c55352018-09-28 14:29:17 +02003021 ret = apr_send_pkt(this_adm.apr,
3022 (uint32_t *)adm_params);
3023 if (ret < 0) {
3024 pr_err("%s: port_id: 0x%x for[0x%x] failed %d for open_v8\n",
3025 __func__, tmp_port, port_id, ret);
3026 return -EINVAL;
3027 }
3028 kfree(adm_params);
3029 } else {
3030
3031 open.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3032 APR_HDR_LEN(APR_HDR_SIZE),
3033 APR_PKT_VER);
3034 open.hdr.pkt_size = sizeof(open);
3035 open.hdr.src_svc = APR_SVC_ADM;
3036 open.hdr.src_domain = APR_DOMAIN_APPS;
3037 open.hdr.src_port = tmp_port;
3038 open.hdr.dest_svc = APR_SVC_ADM;
3039 open.hdr.dest_domain = APR_DOMAIN_ADSP;
3040 open.hdr.dest_port = tmp_port;
3041 open.hdr.token = port_idx << 16 | copp_idx;
3042 open.hdr.opcode = ADM_CMD_DEVICE_OPEN_V5;
3043 open.flags = flags;
3044 open.mode_of_operation = path;
3045 open.endpoint_id_1 = tmp_port;
3046 open.endpoint_id_2 = 0xFFFF;
3047
3048 if (this_adm.ec_ref_rx && (path != 1)) {
3049 open.endpoint_id_2 = this_adm.ec_ref_rx;
3050 this_adm.ec_ref_rx = -1;
3051 }
3052
3053 open.topology_id = topology;
3054
3055 open.dev_num_channel = channel_mode & 0x00FF;
3056 open.bit_width = bit_width;
3057 WARN_ON((perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) &&
3058 (rate != ULL_SUPPORTED_SAMPLE_RATE));
3059 open.sample_rate = rate;
3060
3061 ret = adm_arrange_mch_map(&open, path, channel_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303062
3063 if (ret)
3064 return ret;
3065
Dieter Luecking50c55352018-09-28 14:29:17 +02003066 pr_debug("%s: port_id=0x%x rate=%d topology_id=0x%X\n",
3067 __func__, open.endpoint_id_1, open.sample_rate,
3068 open.topology_id);
3069
3070 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3071
3072 if ((this_adm.num_ec_ref_rx_chans != 0) &&
3073 (path != 1) && (open.endpoint_id_2 != 0xFFFF)) {
3074 memset(&open_v6, 0,
3075 sizeof(struct adm_cmd_device_open_v6));
3076 memcpy(&open_v6, &open,
3077 sizeof(struct adm_cmd_device_open_v5));
3078 open_v6.hdr.opcode = ADM_CMD_DEVICE_OPEN_V6;
3079 open_v6.hdr.pkt_size = sizeof(open_v6);
3080 open_v6.dev_num_channel_eid2 =
3081 this_adm.num_ec_ref_rx_chans;
3082 this_adm.num_ec_ref_rx_chans = 0;
3083
3084 if (this_adm.ec_ref_rx_bit_width != 0) {
3085 open_v6.bit_width_eid2 =
3086 this_adm.ec_ref_rx_bit_width;
3087 this_adm.ec_ref_rx_bit_width = 0;
3088 } else {
3089 open_v6.bit_width_eid2 = bit_width;
3090 }
3091
3092 if (this_adm.ec_ref_rx_sampling_rate != 0) {
3093 open_v6.sample_rate_eid2 =
3094 this_adm.ec_ref_rx_sampling_rate;
3095 this_adm.ec_ref_rx_sampling_rate = 0;
3096 } else {
3097 open_v6.sample_rate_eid2 = rate;
3098 }
3099
3100 pr_debug("%s: eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
3101 __func__, open_v6.dev_num_channel_eid2,
3102 open_v6.bit_width_eid2,
3103 open_v6.sample_rate_eid2);
3104
3105 ret = adm_arrange_mch_ep2_map(&open_v6,
3106 open_v6.dev_num_channel_eid2);
3107
3108 if (ret)
3109 return ret;
3110
3111 ret = apr_send_pkt(this_adm.apr,
3112 (uint32_t *)&open_v6);
3113 } else {
3114 ret = apr_send_pkt(this_adm.apr,
3115 (uint32_t *)&open);
3116 }
3117 if (ret < 0) {
3118 pr_err("%s: port_id: 0x%x for[0x%x] failed %d\n",
3119 __func__, tmp_port, port_id, ret);
3120 return -EINVAL;
3121 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303122 }
Dieter Luecking50c55352018-09-28 14:29:17 +02003123
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303124 /* Wait for the callback with copp id */
3125 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3126 atomic_read(&this_adm.copp.stat
3127 [port_idx][copp_idx]) >= 0,
3128 msecs_to_jiffies(TIMEOUT_MS));
3129 if (!ret) {
3130 pr_err("%s: ADM open timedout for port_id: 0x%x for [0x%x]\n",
3131 __func__, tmp_port, port_id);
3132 return -EINVAL;
3133 } else if (atomic_read(&this_adm.copp.stat
3134 [port_idx][copp_idx]) > 0) {
3135 pr_err("%s: DSP returned error[%s]\n",
3136 __func__, adsp_err_get_err_str(
3137 atomic_read(&this_adm.copp.stat
3138 [port_idx][copp_idx])));
3139 return adsp_err_get_lnx_err_code(
3140 atomic_read(&this_adm.copp.stat
3141 [port_idx][copp_idx]));
3142 }
3143 }
3144 atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
3145 return copp_idx;
3146}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303147EXPORT_SYMBOL(adm_open);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303148
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303149/**
3150 * adm_copp_mfc_cfg -
3151 * command to send ADM MFC config
3152 *
3153 * @port_id: Port ID number
3154 * @copp_idx: copp index assigned
3155 * @dst_sample_rate: sink sample rate
3156 *
3157 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303158void adm_copp_mfc_cfg(int port_id, int copp_idx, int dst_sample_rate)
3159{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003160 struct audproc_mfc_param_media_fmt mfc_cfg;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303161 struct adm_cmd_device_open_v5 open;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003162 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303163 int port_idx;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303164 int rc = 0;
3165 int i = 0;
3166
3167 port_id = q6audio_convert_virtual_to_portid(port_id);
3168 port_idx = adm_validate_and_get_port_index(port_id);
3169
3170 if (port_idx < 0) {
3171 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
3172 goto fail_cmd;
3173 }
3174
3175 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3176 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3177 goto fail_cmd;
3178 }
3179
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003180 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
3181 memset(&open, 0, sizeof(open));
3182 memset(&param_hdr, 0, sizeof(param_hdr));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303183
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003184 param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
3185 param_hdr.instance_id = INSTANCE_ID_0;
3186 param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
3187 param_hdr.param_size = sizeof(mfc_cfg);
3188
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303189 mfc_cfg.sampling_rate = dst_sample_rate;
3190 mfc_cfg.bits_per_sample =
3191 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
3192 open.dev_num_channel = mfc_cfg.num_channels =
3193 atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
3194
3195 rc = adm_arrange_mch_map(&open, ADM_PATH_PLAYBACK,
3196 mfc_cfg.num_channels);
3197 if (rc < 0) {
3198 pr_err("%s: unable to get channal map\n", __func__);
3199 goto fail_cmd;
3200 }
3201
3202 for (i = 0; i < mfc_cfg.num_channels; i++)
3203 mfc_cfg.channel_type[i] =
3204 (uint16_t) open.dev_channel_mapping[i];
3205
3206 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3207
3208 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",
3209 __func__, port_idx, copp_idx,
3210 atomic_read(&this_adm.copp.rate[port_idx][copp_idx]),
3211 mfc_cfg.bits_per_sample, mfc_cfg.num_channels,
3212 mfc_cfg.sampling_rate);
3213
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003214 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
3215 (uint8_t *) &mfc_cfg);
3216 if (rc)
3217 pr_err("%s: Failed to set media format configuration data, err %d\n",
3218 __func__, rc);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303219
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303220fail_cmd:
3221 return;
3222}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303223EXPORT_SYMBOL(adm_copp_mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303224
3225static void route_set_opcode_matrix_id(
3226 struct adm_cmd_matrix_map_routings_v5 **route_addr,
3227 int path, uint32_t passthr_mode)
3228{
3229 struct adm_cmd_matrix_map_routings_v5 *route = *route_addr;
3230
3231 switch (path) {
3232 case ADM_PATH_PLAYBACK:
3233 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
3234 route->matrix_id = ADM_MATRIX_ID_AUDIO_RX;
3235 break;
3236 case ADM_PATH_LIVE_REC:
3237 if (passthr_mode == LISTEN) {
3238 route->hdr.opcode =
3239 ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3240 route->matrix_id = ADM_MATRIX_ID_LISTEN_TX;
3241 break;
3242 }
3243 /* fall through to set matrix id for non-listen case */
3244 case ADM_PATH_NONLIVE_REC:
3245 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
3246 route->matrix_id = ADM_MATRIX_ID_AUDIO_TX;
3247 break;
3248 case ADM_PATH_COMPRESSED_RX:
3249 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3250 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_RX;
3251 break;
3252 case ADM_PATH_COMPRESSED_TX:
3253 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3254 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_TX;
3255 break;
3256 default:
3257 pr_err("%s: Wrong path set[%d]\n", __func__, path);
3258 break;
3259 }
3260 pr_debug("%s: opcode 0x%x, matrix id %d\n",
3261 __func__, route->hdr.opcode, route->matrix_id);
3262}
3263
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303264/**
3265 * adm_matrix_map -
3266 * command to send ADM matrix map for ADM copp list
3267 *
3268 * @path: direction or ADM path type
3269 * @payload_map: have info of session id and associated copp_idx/num_copps
3270 * @perf_mode: performance mode like LL/ULL/..
3271 * @passthr_mode: flag to indicate passthrough mode
3272 *
3273 * Returns 0 on success or error on failure
3274 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303275int adm_matrix_map(int path, struct route_payload payload_map, int perf_mode,
3276 uint32_t passthr_mode)
3277{
3278 struct adm_cmd_matrix_map_routings_v5 *route;
3279 struct adm_session_map_node_v5 *node;
3280 uint16_t *copps_list;
3281 int cmd_size = 0;
3282 int ret = 0, i = 0;
3283 void *payload = NULL;
3284 void *matrix_map = NULL;
3285 int port_idx, copp_idx;
3286
3287 /* Assumes port_ids have already been validated during adm_open */
3288 cmd_size = (sizeof(struct adm_cmd_matrix_map_routings_v5) +
3289 sizeof(struct adm_session_map_node_v5) +
3290 (sizeof(uint32_t) * payload_map.num_copps));
3291 matrix_map = kzalloc(cmd_size, GFP_KERNEL);
3292 if (matrix_map == NULL) {
3293 pr_err("%s: Mem alloc failed\n", __func__);
3294 ret = -EINVAL;
3295 return ret;
3296 }
3297 route = (struct adm_cmd_matrix_map_routings_v5 *)matrix_map;
3298
3299 route->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3300 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
3301 route->hdr.pkt_size = cmd_size;
3302 route->hdr.src_svc = 0;
3303 route->hdr.src_domain = APR_DOMAIN_APPS;
3304 route->hdr.src_port = 0; /* Ignored */;
3305 route->hdr.dest_svc = APR_SVC_ADM;
3306 route->hdr.dest_domain = APR_DOMAIN_ADSP;
3307 route->hdr.dest_port = 0; /* Ignored */;
3308 route->hdr.token = 0;
3309 route->num_sessions = 1;
3310 route_set_opcode_matrix_id(&route, path, passthr_mode);
3311
3312 payload = ((u8 *)matrix_map +
3313 sizeof(struct adm_cmd_matrix_map_routings_v5));
3314 node = (struct adm_session_map_node_v5 *)payload;
3315
3316 node->session_id = payload_map.session_id;
3317 node->num_copps = payload_map.num_copps;
3318 payload = (u8 *)node + sizeof(struct adm_session_map_node_v5);
3319 copps_list = (uint16_t *)payload;
3320 for (i = 0; i < payload_map.num_copps; i++) {
3321 port_idx =
3322 adm_validate_and_get_port_index(payload_map.port_id[i]);
3323 if (port_idx < 0) {
3324 pr_err("%s: Invalid port_id 0x%x\n", __func__,
3325 payload_map.port_id[i]);
3326 ret = -EINVAL;
3327 goto fail_cmd;
3328 }
3329 copp_idx = payload_map.copp_idx[i];
3330 copps_list[i] = atomic_read(&this_adm.copp.id[port_idx]
3331 [copp_idx]);
3332 }
3333 atomic_set(&this_adm.matrix_map_stat, -1);
3334
3335 ret = apr_send_pkt(this_adm.apr, (uint32_t *)matrix_map);
3336 if (ret < 0) {
3337 pr_err("%s: routing for syream %d failed ret %d\n",
3338 __func__, payload_map.session_id, ret);
3339 ret = -EINVAL;
3340 goto fail_cmd;
3341 }
3342 ret = wait_event_timeout(this_adm.matrix_map_wait,
3343 atomic_read(&this_adm.matrix_map_stat) >= 0,
3344 msecs_to_jiffies(TIMEOUT_MS));
3345 if (!ret) {
3346 pr_err("%s: routing for syream %d failed\n", __func__,
3347 payload_map.session_id);
3348 ret = -EINVAL;
3349 goto fail_cmd;
3350 } else if (atomic_read(&this_adm.matrix_map_stat) > 0) {
3351 pr_err("%s: DSP returned error[%s]\n", __func__,
3352 adsp_err_get_err_str(atomic_read(
3353 &this_adm.matrix_map_stat)));
3354 ret = adsp_err_get_lnx_err_code(
3355 atomic_read(&this_adm.matrix_map_stat));
3356 goto fail_cmd;
3357 }
3358
3359 if ((perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) &&
3360 (path != ADM_PATH_COMPRESSED_RX)) {
3361 for (i = 0; i < payload_map.num_copps; i++) {
3362 port_idx = afe_get_port_index(payload_map.port_id[i]);
3363 copp_idx = payload_map.copp_idx[i];
3364 if (port_idx < 0 || copp_idx < 0 ||
3365 (copp_idx > MAX_COPPS_PER_PORT - 1)) {
3366 pr_err("%s: Invalid idx port_idx %d copp_idx %d\n",
3367 __func__, port_idx, copp_idx);
3368 continue;
3369 }
3370 rtac_add_adm_device(payload_map.port_id[i],
3371 atomic_read(&this_adm.copp.id
3372 [port_idx][copp_idx]),
3373 get_cal_path(path),
3374 payload_map.session_id,
3375 payload_map.app_type[i],
3376 payload_map.acdb_dev_id[i]);
3377
3378 if (!test_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3379 (void *)&this_adm.copp.adm_status[port_idx]
3380 [copp_idx])) {
3381 pr_debug("%s: adm copp[0x%x][%d] already sent",
3382 __func__, port_idx, copp_idx);
3383 continue;
3384 }
3385 send_adm_cal(payload_map.port_id[i], copp_idx,
3386 get_cal_path(path), perf_mode,
3387 payload_map.app_type[i],
3388 payload_map.acdb_dev_id[i],
Aditya Bavanari5106b562018-01-08 13:16:32 +05303389 payload_map.sample_rate[i],
3390 passthr_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303391 /* ADM COPP calibration is already sent */
3392 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3393 (void *)&this_adm.copp.
3394 adm_status[port_idx][copp_idx]);
3395 pr_debug("%s: copp_id: %d\n", __func__,
3396 atomic_read(&this_adm.copp.id[port_idx]
3397 [copp_idx]));
3398 }
3399 }
3400
3401fail_cmd:
3402 kfree(matrix_map);
3403 return ret;
3404}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303405EXPORT_SYMBOL(adm_matrix_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303406
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303407/**
3408 * adm_ec_ref_rx_id -
3409 * Update EC ref port ID
3410 *
3411 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303412void adm_ec_ref_rx_id(int port_id)
3413{
3414 this_adm.ec_ref_rx = port_id;
3415 pr_debug("%s: ec_ref_rx:%d\n", __func__, this_adm.ec_ref_rx);
3416}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303417EXPORT_SYMBOL(adm_ec_ref_rx_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303418
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303419/**
3420 * adm_num_ec_ref_rx_chans -
3421 * Update EC ref number of channels
3422 *
3423 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303424void adm_num_ec_ref_rx_chans(int num_chans)
3425{
3426 this_adm.num_ec_ref_rx_chans = num_chans;
3427 pr_debug("%s: num_ec_ref_rx_chans:%d\n",
3428 __func__, this_adm.num_ec_ref_rx_chans);
3429}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303430EXPORT_SYMBOL(adm_num_ec_ref_rx_chans);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303431
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303432/**
3433 * adm_ec_ref_rx_bit_width -
3434 * Update EC ref bit_width
3435 *
3436 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303437void adm_ec_ref_rx_bit_width(int bit_width)
3438{
3439 this_adm.ec_ref_rx_bit_width = bit_width;
3440 pr_debug("%s: ec_ref_rx_bit_width:%d\n",
3441 __func__, this_adm.ec_ref_rx_bit_width);
3442}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303443EXPORT_SYMBOL(adm_ec_ref_rx_bit_width);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303444
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303445/**
3446 * adm_ec_ref_rx_sampling_rate -
3447 * Update EC ref sample rate
3448 *
3449 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303450void adm_ec_ref_rx_sampling_rate(int sampling_rate)
3451{
3452 this_adm.ec_ref_rx_sampling_rate = sampling_rate;
3453 pr_debug("%s: ec_ref_rx_sampling_rate:%d\n",
3454 __func__, this_adm.ec_ref_rx_sampling_rate);
3455}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303456EXPORT_SYMBOL(adm_ec_ref_rx_sampling_rate);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303457
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303458/**
Dieter Luecking50c55352018-09-28 14:29:17 +02003459 * adm_set_native_mode -
3460 * Set adm channel native mode.
3461 * If enabled matrix mixer will be
3462 * running in native mode for channel
3463 * configuration for this device session.
3464 *
3465 */
3466void adm_set_native_mode(int mode)
3467{
3468 this_adm.native_mode = mode;
3469 pr_debug("%s: enable native_mode :%d\n",
3470 __func__, this_adm.native_mode);
3471}
3472EXPORT_SYMBOL(adm_set_native_mode);
3473
3474/**
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303475 * adm_close -
3476 * command to close ADM copp
3477 *
3478 * @port_id: Port ID number
3479 * @perf_mode: performance mode like LL/ULL/..
3480 * @copp_idx: copp index assigned
3481 *
3482 * Returns 0 on success or error on failure
3483 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303484int adm_close(int port_id, int perf_mode, int copp_idx)
3485{
3486 struct apr_hdr close;
3487
3488 int ret = 0, port_idx;
3489 int copp_id = RESET_COPP_ID;
3490
3491 pr_debug("%s: port_id=0x%x perf_mode: %d copp_idx: %d\n", __func__,
3492 port_id, perf_mode, copp_idx);
3493
3494 port_id = q6audio_convert_virtual_to_portid(port_id);
3495 port_idx = adm_validate_and_get_port_index(port_id);
3496 if (port_idx < 0) {
3497 pr_err("%s: Invalid port_id 0x%x\n",
3498 __func__, port_id);
3499 return -EINVAL;
3500 }
3501
3502 if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
3503 pr_err("%s: Invalid copp idx: %d\n", __func__, copp_idx);
3504 return -EINVAL;
3505 }
3506
3507 if (this_adm.copp.adm_delay[port_idx][copp_idx] && perf_mode
3508 == LEGACY_PCM_MODE) {
3509 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
3510 1);
3511 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
3512 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
3513 }
3514
3515 atomic_dec(&this_adm.copp.cnt[port_idx][copp_idx]);
3516 if (!(atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]))) {
3517 copp_id = adm_get_copp_id(port_idx, copp_idx);
3518 pr_debug("%s: Closing ADM port_idx:%d copp_idx:%d copp_id:0x%x\n",
3519 __func__, port_idx, copp_idx, copp_id);
3520 if ((!perf_mode) && (this_adm.outband_memmap.paddr != 0) &&
3521 (atomic_read(&this_adm.copp.topology[port_idx][copp_idx]) ==
3522 SRS_TRUMEDIA_TOPOLOGY_ID)) {
3523 atomic_set(&this_adm.mem_map_index,
3524 ADM_SRS_TRUMEDIA);
3525 ret = adm_memory_unmap_regions();
3526 if (ret < 0) {
3527 pr_err("%s: adm mem unmmap err %d",
3528 __func__, ret);
3529 } else {
3530 atomic_set(&this_adm.mem_map_handles
3531 [ADM_SRS_TRUMEDIA], 0);
3532 }
3533 }
3534
3535
3536 if ((afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX) &&
3537 this_adm.sourceTrackingData.memmap.paddr) {
3538 atomic_set(&this_adm.mem_map_index,
3539 ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
3540 ret = adm_memory_unmap_regions();
3541 if (ret < 0) {
3542 pr_err("%s: adm mem unmmap err %d",
3543 __func__, ret);
3544 }
3545 msm_audio_ion_free(
Banajit Goswami08bb7362017-11-03 22:48:23 -07003546 this_adm.sourceTrackingData.dma_buf);
3547 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303548 this_adm.sourceTrackingData.memmap.size = 0;
3549 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
3550 this_adm.sourceTrackingData.memmap.paddr = 0;
3551 this_adm.sourceTrackingData.apr_cmd_status = -1;
3552 atomic_set(&this_adm.mem_map_handles[
3553 ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
3554 }
3555
3556 close.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3557 APR_HDR_LEN(APR_HDR_SIZE),
3558 APR_PKT_VER);
3559 close.pkt_size = sizeof(close);
3560 close.src_svc = APR_SVC_ADM;
3561 close.src_domain = APR_DOMAIN_APPS;
3562 close.src_port = port_id;
3563 close.dest_svc = APR_SVC_ADM;
3564 close.dest_domain = APR_DOMAIN_ADSP;
3565 close.dest_port = copp_id;
3566 close.token = port_idx << 16 | copp_idx;
3567 close.opcode = ADM_CMD_DEVICE_CLOSE_V5;
3568
3569 atomic_set(&this_adm.copp.id[port_idx][copp_idx],
3570 RESET_COPP_ID);
3571 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
3572 atomic_set(&this_adm.copp.topology[port_idx][copp_idx], 0);
3573 atomic_set(&this_adm.copp.mode[port_idx][copp_idx], 0);
3574 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3575 atomic_set(&this_adm.copp.rate[port_idx][copp_idx], 0);
3576 atomic_set(&this_adm.copp.channels[port_idx][copp_idx], 0);
3577 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx], 0);
3578 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx], 0);
3579
3580 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3581 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
3582
3583 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&close);
3584 if (ret < 0) {
3585 pr_err("%s: ADM close failed %d\n", __func__, ret);
3586 return -EINVAL;
3587 }
3588
3589 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3590 atomic_read(&this_adm.copp.stat
3591 [port_idx][copp_idx]) >= 0,
3592 msecs_to_jiffies(TIMEOUT_MS));
3593 if (!ret) {
3594 pr_err("%s: ADM cmd Route timedout for port 0x%x\n",
3595 __func__, port_id);
3596 return -EINVAL;
3597 } else if (atomic_read(&this_adm.copp.stat
3598 [port_idx][copp_idx]) > 0) {
3599 pr_err("%s: DSP returned error[%s]\n",
3600 __func__, adsp_err_get_err_str(
3601 atomic_read(&this_adm.copp.stat
3602 [port_idx][copp_idx])));
3603 return adsp_err_get_lnx_err_code(
3604 atomic_read(&this_adm.copp.stat
3605 [port_idx][copp_idx]));
3606 }
3607 }
3608
3609 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) {
3610 pr_debug("%s: remove adm device from rtac\n", __func__);
3611 rtac_remove_adm_device(port_id, copp_id);
3612 }
3613 return 0;
3614}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303615EXPORT_SYMBOL(adm_close);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303616
3617int send_rtac_audvol_cal(void)
3618{
3619 int ret = 0;
3620 int ret2 = 0;
3621 int i = 0;
3622 int copp_idx, port_idx, acdb_id, app_id, path;
3623 struct cal_block_data *cal_block = NULL;
3624 struct audio_cal_info_audvol *audvol_cal_info = NULL;
3625 struct rtac_adm rtac_adm_data;
3626
3627 mutex_lock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3628
3629 cal_block = cal_utils_get_only_cal_block(
3630 this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]);
Vikram Panduranga770b8382017-09-27 12:17:36 -07003631 if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303632 pr_err("%s: can't find cal block!\n", __func__);
3633 goto unlock;
3634 }
3635
3636 audvol_cal_info = cal_block->cal_info;
3637 if (audvol_cal_info == NULL) {
3638 pr_err("%s: audvol_cal_info is NULL!\n", __func__);
3639 goto unlock;
3640 }
3641
3642 get_rtac_adm_data(&rtac_adm_data);
3643 for (; i < rtac_adm_data.num_of_dev; i++) {
3644
3645 acdb_id = rtac_adm_data.device[i].acdb_dev_id;
3646 if (acdb_id == 0)
3647 acdb_id = audvol_cal_info->acdb_id;
3648
3649 app_id = rtac_adm_data.device[i].app_type;
3650 if (app_id == 0)
3651 app_id = audvol_cal_info->app_type;
3652
3653 path = afe_get_port_type(rtac_adm_data.device[i].afe_port);
3654 if ((acdb_id == audvol_cal_info->acdb_id) &&
3655 (app_id == audvol_cal_info->app_type) &&
3656 (path == audvol_cal_info->path)) {
3657
3658 if (adm_get_indexes_from_copp_id(rtac_adm_data.
3659 device[i].copp, &copp_idx, &port_idx) != 0) {
3660 pr_debug("%s: Copp Id %d is not active\n",
3661 __func__,
3662 rtac_adm_data.device[i].copp);
3663 continue;
3664 }
3665
3666 ret2 = adm_remap_and_send_cal_block(ADM_RTAC_AUDVOL_CAL,
3667 rtac_adm_data.device[i].afe_port,
3668 copp_idx, cal_block,
3669 atomic_read(&this_adm.copp.
3670 mode[port_idx][copp_idx]),
3671 audvol_cal_info->app_type,
3672 audvol_cal_info->acdb_id,
3673 atomic_read(&this_adm.copp.
3674 rate[port_idx][copp_idx]));
3675 if (ret2 < 0) {
3676 pr_debug("%s: remap and send failed for copp Id %d, acdb id %d, app type %d, path %d\n",
3677 __func__, rtac_adm_data.device[i].copp,
3678 audvol_cal_info->acdb_id,
3679 audvol_cal_info->app_type,
3680 audvol_cal_info->path);
3681 ret = ret2;
3682 }
3683 }
3684 }
3685unlock:
3686 mutex_unlock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3687 return ret;
3688}
3689
3690int adm_map_rtac_block(struct rtac_cal_block_data *cal_block)
3691{
3692 int result = 0;
3693
3694 pr_debug("%s:\n", __func__);
3695
3696 if (cal_block == NULL) {
3697 pr_err("%s: cal_block is NULL!\n",
3698 __func__);
3699 result = -EINVAL;
3700 goto done;
3701 }
3702
3703 if (cal_block->cal_data.paddr == 0) {
3704 pr_debug("%s: No address to map!\n",
3705 __func__);
3706 result = -EINVAL;
3707 goto done;
3708 }
3709
3710 if (cal_block->map_data.map_size == 0) {
3711 pr_debug("%s: map size is 0!\n",
3712 __func__);
3713 result = -EINVAL;
3714 goto done;
3715 }
3716
3717 /* valid port ID needed for callback use primary I2S */
3718 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3719 result = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3720 &cal_block->map_data.map_size, 1);
3721 if (result < 0) {
3722 pr_err("%s: RTAC mmap did not work! size = %d result %d\n",
3723 __func__,
3724 cal_block->map_data.map_size, result);
3725 pr_debug("%s: RTAC mmap did not work! addr = 0x%pK, size = %d\n",
3726 __func__,
3727 &cal_block->cal_data.paddr,
3728 cal_block->map_data.map_size);
3729 goto done;
3730 }
3731
3732 cal_block->map_data.map_handle = atomic_read(
3733 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]);
3734done:
3735 return result;
3736}
3737
3738int adm_unmap_rtac_block(uint32_t *mem_map_handle)
3739{
3740 int result = 0;
3741
3742 pr_debug("%s:\n", __func__);
3743
3744 if (mem_map_handle == NULL) {
3745 pr_debug("%s: Map handle is NULL, nothing to unmap\n",
3746 __func__);
3747 goto done;
3748 }
3749
3750 if (*mem_map_handle == 0) {
3751 pr_debug("%s: Map handle is 0, nothing to unmap\n",
3752 __func__);
3753 goto done;
3754 }
3755
3756 if (*mem_map_handle != atomic_read(
3757 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL])) {
3758 pr_err("%s: Map handles do not match! Unmapping RTAC, RTAC map 0x%x, ADM map 0x%x\n",
3759 __func__, *mem_map_handle, atomic_read(
3760 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]));
3761
3762 /* if mismatch use handle passed in to unmap */
3763 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL],
3764 *mem_map_handle);
3765 }
3766
3767 /* valid port ID needed for callback use primary I2S */
3768 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3769 result = adm_memory_unmap_regions();
3770 if (result < 0) {
3771 pr_debug("%s: adm_memory_unmap_regions failed, error %d\n",
3772 __func__, result);
3773 } else {
3774 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL], 0);
3775 *mem_map_handle = 0;
3776 }
3777done:
3778 return result;
3779}
3780
3781static int get_cal_type_index(int32_t cal_type)
3782{
3783 int ret = -EINVAL;
3784
3785 switch (cal_type) {
3786 case ADM_AUDPROC_CAL_TYPE:
3787 ret = ADM_AUDPROC_CAL;
3788 break;
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05303789 case ADM_LSM_AUDPROC_CAL_TYPE:
3790 ret = ADM_LSM_AUDPROC_CAL;
3791 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303792 case ADM_AUDVOL_CAL_TYPE:
3793 ret = ADM_AUDVOL_CAL;
3794 break;
3795 case ADM_CUST_TOPOLOGY_CAL_TYPE:
3796 ret = ADM_CUSTOM_TOP_CAL;
3797 break;
3798 case ADM_RTAC_INFO_CAL_TYPE:
3799 ret = ADM_RTAC_INFO_CAL;
3800 break;
3801 case ADM_RTAC_APR_CAL_TYPE:
3802 ret = ADM_RTAC_APR_CAL;
3803 break;
3804 case ADM_RTAC_AUDVOL_CAL_TYPE:
3805 ret = ADM_RTAC_AUDVOL_CAL;
3806 break;
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07003807 case ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE:
3808 ret = ADM_LSM_AUDPROC_PERSISTENT_CAL;
3809 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303810 default:
3811 pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
3812 }
3813 return ret;
3814}
3815
3816static int adm_alloc_cal(int32_t cal_type, size_t data_size, void *data)
3817{
3818 int ret = 0;
3819 int cal_index;
3820
3821 pr_debug("%s:\n", __func__);
3822
3823 cal_index = get_cal_type_index(cal_type);
3824 if (cal_index < 0) {
3825 pr_err("%s: could not get cal index %d!\n",
3826 __func__, cal_index);
3827 ret = -EINVAL;
3828 goto done;
3829 }
3830
3831 ret = cal_utils_alloc_cal(data_size, data,
3832 this_adm.cal_data[cal_index], 0, NULL);
3833 if (ret < 0) {
3834 pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
3835 __func__, ret, cal_type);
3836 ret = -EINVAL;
3837 goto done;
3838 }
3839done:
3840 return ret;
3841}
3842
3843static int adm_dealloc_cal(int32_t cal_type, size_t data_size, void *data)
3844{
3845 int ret = 0;
3846 int cal_index;
3847
3848 pr_debug("%s:\n", __func__);
3849
3850 cal_index = get_cal_type_index(cal_type);
3851 if (cal_index < 0) {
3852 pr_err("%s: could not get cal index %d!\n",
3853 __func__, cal_index);
3854 ret = -EINVAL;
3855 goto done;
3856 }
3857
3858 ret = cal_utils_dealloc_cal(data_size, data,
3859 this_adm.cal_data[cal_index]);
3860 if (ret < 0) {
3861 pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
3862 __func__, ret, cal_type);
3863 ret = -EINVAL;
3864 goto done;
3865 }
3866done:
3867 return ret;
3868}
3869
3870static int adm_set_cal(int32_t cal_type, size_t data_size, void *data)
3871{
3872 int ret = 0;
3873 int cal_index;
3874
3875 pr_debug("%s:\n", __func__);
3876
3877 cal_index = get_cal_type_index(cal_type);
3878 if (cal_index < 0) {
3879 pr_err("%s: could not get cal index %d!\n",
3880 __func__, cal_index);
3881 ret = -EINVAL;
3882 goto done;
3883 }
3884
3885 ret = cal_utils_set_cal(data_size, data,
3886 this_adm.cal_data[cal_index], 0, NULL);
3887 if (ret < 0) {
3888 pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
3889 __func__, ret, cal_type);
3890 ret = -EINVAL;
3891 goto done;
3892 }
3893
3894 if (cal_index == ADM_CUSTOM_TOP_CAL) {
3895 mutex_lock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3896 this_adm.set_custom_topology = 1;
3897 mutex_unlock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3898 } else if (cal_index == ADM_RTAC_AUDVOL_CAL) {
3899 send_rtac_audvol_cal();
3900 }
3901done:
3902 return ret;
3903}
3904
3905static int adm_map_cal_data(int32_t cal_type,
3906 struct cal_block_data *cal_block)
3907{
3908 int ret = 0;
3909 int cal_index;
3910
3911 pr_debug("%s:\n", __func__);
3912
3913 cal_index = get_cal_type_index(cal_type);
3914 if (cal_index < 0) {
3915 pr_err("%s: could not get cal index %d!\n",
3916 __func__, cal_index);
3917 ret = -EINVAL;
3918 goto done;
3919 }
3920
3921 atomic_set(&this_adm.mem_map_index, cal_index);
3922 ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3923 (uint32_t *)&cal_block->map_data.map_size, 1);
3924 if (ret < 0) {
3925 pr_err("%s: map did not work! cal_type %i ret %d\n",
3926 __func__, cal_index, ret);
3927 ret = -ENODEV;
3928 goto done;
3929 }
3930 cal_block->map_data.q6map_handle = atomic_read(&this_adm.
3931 mem_map_handles[cal_index]);
3932done:
3933 return ret;
3934}
3935
3936static int adm_unmap_cal_data(int32_t cal_type,
3937 struct cal_block_data *cal_block)
3938{
3939 int ret = 0;
3940 int cal_index;
3941
3942 pr_debug("%s:\n", __func__);
3943
3944 cal_index = get_cal_type_index(cal_type);
3945 if (cal_index < 0) {
3946 pr_err("%s: could not get cal index %d!\n",
3947 __func__, cal_index);
3948 ret = -EINVAL;
3949 goto done;
3950 }
3951
3952 if (cal_block == NULL) {
3953 pr_err("%s: Cal block is NULL!\n",
3954 __func__);
3955 goto done;
3956 }
3957
3958 if (cal_block->map_data.q6map_handle == 0) {
3959 pr_err("%s: Map handle is NULL, nothing to unmap\n",
3960 __func__);
3961 goto done;
3962 }
3963
3964 atomic_set(&this_adm.mem_map_handles[cal_index],
3965 cal_block->map_data.q6map_handle);
3966 atomic_set(&this_adm.mem_map_index, cal_index);
3967 ret = adm_memory_unmap_regions();
3968 if (ret < 0) {
3969 pr_err("%s: unmap did not work! cal_type %i ret %d\n",
3970 __func__, cal_index, ret);
3971 ret = -ENODEV;
3972 goto done;
3973 }
3974 cal_block->map_data.q6map_handle = 0;
3975done:
3976 return ret;
3977}
3978
3979static void adm_delete_cal_data(void)
3980{
3981 pr_debug("%s:\n", __func__);
3982
3983 cal_utils_destroy_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data);
3984}
3985
3986static int adm_init_cal_data(void)
3987{
3988 int ret = 0;
3989 struct cal_type_info cal_type_info[] = {
3990 {{ADM_CUST_TOPOLOGY_CAL_TYPE,
3991 {adm_alloc_cal, adm_dealloc_cal, NULL,
3992 adm_set_cal, NULL, NULL} },
3993 {adm_map_cal_data, adm_unmap_cal_data,
3994 cal_utils_match_buf_num} },
3995
3996 {{ADM_AUDPROC_CAL_TYPE,
3997 {adm_alloc_cal, adm_dealloc_cal, NULL,
3998 adm_set_cal, NULL, NULL} },
3999 {adm_map_cal_data, adm_unmap_cal_data,
4000 cal_utils_match_buf_num} },
4001
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05304002 {{ADM_LSM_AUDPROC_CAL_TYPE,
4003 {adm_alloc_cal, adm_dealloc_cal, NULL,
4004 adm_set_cal, NULL, NULL} },
4005 {adm_map_cal_data, adm_unmap_cal_data,
4006 cal_utils_match_buf_num} },
4007
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304008 {{ADM_AUDVOL_CAL_TYPE,
4009 {adm_alloc_cal, adm_dealloc_cal, NULL,
4010 adm_set_cal, NULL, NULL} },
4011 {adm_map_cal_data, adm_unmap_cal_data,
4012 cal_utils_match_buf_num} },
4013
4014 {{ADM_RTAC_INFO_CAL_TYPE,
4015 {NULL, NULL, NULL, NULL, NULL, NULL} },
4016 {NULL, NULL, cal_utils_match_buf_num} },
4017
4018 {{ADM_RTAC_APR_CAL_TYPE,
4019 {NULL, NULL, NULL, NULL, NULL, NULL} },
4020 {NULL, NULL, cal_utils_match_buf_num} },
4021
4022 {{SRS_TRUMEDIA_CAL_TYPE,
4023 {NULL, NULL, NULL, NULL, NULL, NULL} },
4024 {NULL, NULL, cal_utils_match_buf_num} },
4025
4026 {{ADM_RTAC_AUDVOL_CAL_TYPE,
4027 {adm_alloc_cal, adm_dealloc_cal, NULL,
4028 adm_set_cal, NULL, NULL} },
4029 {adm_map_cal_data, adm_unmap_cal_data,
4030 cal_utils_match_buf_num} },
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07004031
4032 {{ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE,
4033 {adm_alloc_cal, adm_dealloc_cal, NULL,
4034 adm_set_cal, NULL, NULL} },
4035 {adm_map_cal_data, adm_unmap_cal_data,
4036 cal_utils_match_buf_num} },
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304037 };
4038 pr_debug("%s:\n", __func__);
4039
4040 ret = cal_utils_create_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data,
4041 cal_type_info);
4042 if (ret < 0) {
4043 pr_err("%s: could not create cal type! ret %d\n",
4044 __func__, ret);
4045 ret = -EINVAL;
4046 goto err;
4047 }
4048
4049 return ret;
4050err:
4051 adm_delete_cal_data();
4052 return ret;
4053}
4054
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304055/**
4056 * adm_set_volume -
4057 * command to set volume on ADM copp
4058 *
4059 * @port_id: Port ID number
4060 * @copp_idx: copp index assigned
4061 * @volume: gain value to set
4062 *
4063 * Returns 0 on success or error on failure
4064 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304065int adm_set_volume(int port_id, int copp_idx, int volume)
4066{
4067 struct audproc_volume_ctrl_master_gain audproc_vol;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004068 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304069 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304070
4071 pr_debug("%s: port_id %d, volume %d\n", __func__, port_id, volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304072
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004073 memset(&audproc_vol, 0, sizeof(audproc_vol));
4074 memset(&param_hdr, 0, sizeof(param_hdr));
4075 param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
4076 param_hdr.instance_id = INSTANCE_ID_0;
4077 param_hdr.param_id = AUDPROC_PARAM_ID_VOL_CTRL_MASTER_GAIN;
4078 param_hdr.param_size = sizeof(audproc_vol);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304079
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304080 audproc_vol.master_gain = volume;
4081
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004082 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4083 (uint8_t *) &audproc_vol);
4084 if (rc)
4085 pr_err("%s: Failed to set volume, err %d\n", __func__, rc);
4086
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304087 return rc;
4088}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304089EXPORT_SYMBOL(adm_set_volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304090
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304091/**
4092 * adm_set_softvolume -
4093 * command to set softvolume
4094 *
4095 * @port_id: Port ID number
4096 * @copp_idx: copp index assigned
4097 * @softvol_param: Params to set for softvolume
4098 *
4099 * Returns 0 on success or error on failure
4100 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304101int adm_set_softvolume(int port_id, int copp_idx,
4102 struct audproc_softvolume_params *softvol_param)
4103{
4104 struct audproc_soft_step_volume_params audproc_softvol;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004105 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304106 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304107
4108 pr_debug("%s: period %d step %d curve %d\n", __func__,
4109 softvol_param->period, softvol_param->step,
4110 softvol_param->rampingcurve);
4111
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004112 memset(&audproc_softvol, 0, sizeof(audproc_softvol));
4113 memset(&param_hdr, 0, sizeof(param_hdr));
4114 param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
4115 param_hdr.instance_id = INSTANCE_ID_0;
4116 param_hdr.param_id = AUDPROC_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
4117 param_hdr.param_size = sizeof(audproc_softvol);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304118
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304119 audproc_softvol.period = softvol_param->period;
4120 audproc_softvol.step = softvol_param->step;
4121 audproc_softvol.ramping_curve = softvol_param->rampingcurve;
4122
4123 pr_debug("%s: period %d, step %d, curve %d\n", __func__,
4124 audproc_softvol.period, audproc_softvol.step,
4125 audproc_softvol.ramping_curve);
4126
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004127 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4128 (uint8_t *) &audproc_softvol);
4129 if (rc)
4130 pr_err("%s: Failed to set soft volume, err %d\n", __func__, rc);
4131
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304132 return rc;
4133}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304134EXPORT_SYMBOL(adm_set_softvolume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304135
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304136/**
4137 * adm_set_mic_gain -
4138 * command to set MIC gain
4139 *
4140 * @port_id: Port ID number
4141 * @copp_idx: copp index assigned
4142 * @volume: gain value to set
4143 *
4144 * Returns 0 on success or error on failure
4145 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304146int adm_set_mic_gain(int port_id, int copp_idx, int volume)
4147{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004148 struct admx_mic_gain mic_gain_params;
4149 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304150 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304151
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004152 pr_debug("%s: Setting mic gain to %d at port_id 0x%x\n", __func__,
4153 volume, port_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304154
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004155 memset(&mic_gain_params, 0, sizeof(mic_gain_params));
4156 memset(&param_hdr, 0, sizeof(param_hdr));
4157 param_hdr.module_id = ADM_MODULE_IDX_MIC_GAIN_CTRL;
4158 param_hdr.instance_id = INSTANCE_ID_0;
4159 param_hdr.param_id = ADM_PARAM_IDX_MIC_GAIN;
4160 param_hdr.param_size = sizeof(mic_gain_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304161
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004162 mic_gain_params.tx_mic_gain = volume;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304163
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004164 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4165 (uint8_t *) &mic_gain_params);
4166 if (rc)
4167 pr_err("%s: Failed to set mic gain, err %d\n", __func__, rc);
4168
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304169 return rc;
4170}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304171EXPORT_SYMBOL(adm_set_mic_gain);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304172
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304173/**
4174 * adm_send_set_multichannel_ec_primary_mic_ch -
4175 * command to set multi-ch EC primary mic
4176 *
4177 * @port_id: Port ID number
4178 * @copp_idx: copp index assigned
4179 * @primary_mic_ch: channel number of primary mic
4180 *
4181 * Returns 0 on success or error on failure
4182 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304183int adm_send_set_multichannel_ec_primary_mic_ch(int port_id, int copp_idx,
4184 int primary_mic_ch)
4185{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004186 struct admx_sec_primary_mic_ch sec_primary_ch_params;
4187 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304188 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304189
4190 pr_debug("%s port_id 0x%x, copp_idx 0x%x, primary_mic_ch %d\n",
4191 __func__, port_id, copp_idx, primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304192
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004193 memset(&sec_primary_ch_params, 0, sizeof(sec_primary_ch_params));
4194 memset(&param_hdr, 0, sizeof(param_hdr));
4195 param_hdr.module_id = AUDPROC_MODULE_ID_VOICE_TX_SECNS;
4196 param_hdr.instance_id = INSTANCE_ID_0;
4197 param_hdr.param_id = AUDPROC_PARAM_IDX_SEC_PRIMARY_MIC_CH;
4198 param_hdr.param_size = sizeof(sec_primary_ch_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304199
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004200 sec_primary_ch_params.version = 0;
4201 sec_primary_ch_params.sec_primary_mic_ch = primary_mic_ch;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304202
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004203 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4204 (uint8_t *) &sec_primary_ch_params);
4205 if (rc)
4206 pr_err("%s: Failed to set primary mic chanel, err %d\n",
4207 __func__, rc);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304208
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304209 return rc;
4210}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304211EXPORT_SYMBOL(adm_send_set_multichannel_ec_primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304212
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304213/**
4214 * adm_param_enable -
4215 * command to send params to ADM for given module
4216 *
4217 * @port_id: Port ID number
4218 * @copp_idx: copp index assigned
4219 * @module_id: ADM module
4220 * @enable: flag to enable or disable module
4221 *
4222 * Returns 0 on success or error on failure
4223 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304224int adm_param_enable(int port_id, int copp_idx, int module_id, int enable)
4225{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004226 struct module_instance_info mod_inst_info;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304227
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004228 memset(&mod_inst_info, 0, sizeof(mod_inst_info));
4229 mod_inst_info.module_id = module_id;
4230 mod_inst_info.instance_id = INSTANCE_ID_0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304231
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004232 return adm_param_enable_v2(port_id, copp_idx, mod_inst_info, enable);
4233}
Vidyakumar Athota98464a22018-03-15 20:39:37 -07004234EXPORT_SYMBOL(adm_param_enable);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004235
4236/**
4237 * adm_param_enable_v2 -
4238 * command to send params to ADM for given module
4239 *
4240 * @port_id: Port ID number
4241 * @copp_idx: copp index assigned
4242 * @mod_inst_info: module and instance ID info
4243 * @enable: flag to enable or disable module
4244 *
4245 * Returns 0 on success or error on failure
4246 */
4247int adm_param_enable_v2(int port_id, int copp_idx,
4248 struct module_instance_info mod_inst_info, int enable)
4249{
4250 uint32_t enable_param;
4251 struct param_hdr_v3 param_hdr;
4252 int rc = 0;
4253
4254 if (enable < 0 || enable > 1) {
4255 pr_err("%s: Invalid value for enable %d\n", __func__, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304256 return -EINVAL;
4257 }
4258
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004259 pr_debug("%s port_id %d, module_id 0x%x, instance_id 0x%x, enable %d\n",
4260 __func__, port_id, mod_inst_info.module_id,
4261 mod_inst_info.instance_id, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304262
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004263 memset(&param_hdr, 0, sizeof(param_hdr));
4264 param_hdr.module_id = mod_inst_info.module_id;
4265 param_hdr.instance_id = mod_inst_info.instance_id;
4266 param_hdr.param_id = AUDPROC_PARAM_ID_ENABLE;
4267 param_hdr.param_size = sizeof(enable_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304268
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004269 enable_param = enable;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304270
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004271 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4272 (uint8_t *) &enable_param);
4273 if (rc)
4274 pr_err("%s: Failed to set enable of module(%d) instance(%d) to %d, err %d\n",
4275 __func__, mod_inst_info.module_id,
4276 mod_inst_info.instance_id, enable, rc);
4277
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304278 return rc;
4279
4280}
Vidyakumar Athota98464a22018-03-15 20:39:37 -07004281EXPORT_SYMBOL(adm_param_enable_v2);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304282
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304283/**
4284 * adm_send_calibration -
4285 * send ADM calibration to DSP
4286 *
4287 * @port_id: Port ID number
4288 * @copp_idx: copp index assigned
4289 * @path: direction or ADM path type
4290 * @perf_mode: performance mode like LL/ULL/..
4291 * @cal_type: calibration type to use
4292 * @params: pointer with cal data
4293 * @size: cal size
4294 *
4295 * Returns 0 on success or error on failure
4296 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304297int adm_send_calibration(int port_id, int copp_idx, int path, int perf_mode,
4298 int cal_type, char *params, int size)
4299{
4300
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004301 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304302
4303 pr_debug("%s:port_id %d, path %d, perf_mode %d, cal_type %d, size %d\n",
4304 __func__, port_id, path, perf_mode, cal_type, size);
4305
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304306 /* Maps audio_dev_ctrl path definition to ACDB definition */
4307 if (get_cal_path(path) != RX_DEVICE) {
4308 pr_err("%s: acdb_path %d\n", __func__, path);
4309 rc = -EINVAL;
4310 goto end;
4311 }
4312
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004313 rc = adm_set_pp_params(port_id, copp_idx, NULL, (u8 *) params, size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304314
4315end:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304316 return rc;
4317}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304318EXPORT_SYMBOL(adm_send_calibration);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304319
4320/*
4321 * adm_update_wait_parameters must be called with routing driver locks.
4322 * adm_reset_wait_parameters must be called with routing driver locks.
4323 * set and reset parmeters are separated to make sure it is always called
4324 * under routing driver lock.
4325 * adm_wait_timeout is to block until timeout or interrupted. Timeout is
4326 * not a an error.
4327 */
4328int adm_set_wait_parameters(int port_id, int copp_idx)
4329{
4330
4331 int ret = 0, port_idx;
4332
4333 pr_debug("%s: port_id 0x%x, copp_idx %d\n", __func__, port_id,
4334 copp_idx);
4335 port_id = afe_convert_virtual_to_portid(port_id);
4336 port_idx = adm_validate_and_get_port_index(port_id);
4337 if (port_idx < 0) {
4338 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4339 ret = -EINVAL;
4340 goto end;
4341 }
4342
4343 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4344 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4345 return -EINVAL;
4346 }
4347
4348 this_adm.copp.adm_delay[port_idx][copp_idx] = 1;
4349 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 0);
4350
4351end:
4352 return ret;
4353
4354}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304355EXPORT_SYMBOL(adm_set_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304356
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304357/**
4358 * adm_reset_wait_parameters -
4359 * reset wait parameters or ADM delay value
4360 *
4361 * @port_id: Port ID number
4362 * @copp_idx: copp index assigned
4363 *
4364 * Returns 0 on success or error on failure
4365 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304366int adm_reset_wait_parameters(int port_id, int copp_idx)
4367{
4368 int ret = 0, port_idx;
4369
4370 pr_debug("%s: port_id 0x%x copp_idx %d\n", __func__, port_id,
4371 copp_idx);
4372 port_id = afe_convert_virtual_to_portid(port_id);
4373 port_idx = adm_validate_and_get_port_index(port_id);
4374 if (port_idx < 0) {
4375 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4376 ret = -EINVAL;
4377 goto end;
4378 }
4379
4380 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4381 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4382 return -EINVAL;
4383 }
4384
4385 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 1);
4386 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
4387
4388end:
4389 return ret;
4390}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304391EXPORT_SYMBOL(adm_reset_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304392
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304393/**
4394 * adm_wait_timeout -
4395 * ADM wait command after command send to DSP
4396 *
4397 * @port_id: Port ID number
4398 * @copp_idx: copp index assigned
4399 * @wait_time: value in ms for command timeout
4400 *
4401 * Returns 0 on success or error on failure
4402 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304403int adm_wait_timeout(int port_id, int copp_idx, int wait_time)
4404{
4405 int ret = 0, port_idx;
4406
4407 pr_debug("%s: port_id 0x%x, copp_idx %d, wait_time %d\n", __func__,
4408 port_id, copp_idx, wait_time);
4409 port_id = afe_convert_virtual_to_portid(port_id);
4410 port_idx = adm_validate_and_get_port_index(port_id);
4411 if (port_idx < 0) {
4412 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4413 ret = -EINVAL;
4414 goto end;
4415 }
4416
4417 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4418 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4419 return -EINVAL;
4420 }
4421
4422 ret = wait_event_timeout(
4423 this_adm.copp.adm_delay_wait[port_idx][copp_idx],
4424 atomic_read(&this_adm.copp.adm_delay_stat[port_idx][copp_idx]),
4425 msecs_to_jiffies(wait_time));
4426 pr_debug("%s: return %d\n", __func__, ret);
4427 if (ret != 0)
4428 ret = -EINTR;
4429end:
4430 pr_debug("%s: return %d--\n", __func__, ret);
4431 return ret;
4432}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304433EXPORT_SYMBOL(adm_wait_timeout);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304434
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304435/**
4436 * adm_store_cal_data -
4437 * Retrieve calibration data for ADM copp device
4438 *
4439 * @port_id: Port ID number
4440 * @copp_idx: copp index assigned
4441 * @path: direction or copp type
4442 * @perf_mode: performance mode like LL/ULL/..
4443 * @cal_index: calibration index to use
4444 * @params: pointer to store cal data
4445 * @size: pointer to fill with cal size
4446 *
4447 * Returns 0 on success or error on failure
4448 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304449int adm_store_cal_data(int port_id, int copp_idx, int path, int perf_mode,
4450 int cal_index, char *params, int *size)
4451{
4452 int rc = 0;
4453 struct cal_block_data *cal_block = NULL;
4454 int app_type, acdb_id, port_idx, sample_rate;
4455
4456 if (this_adm.cal_data[cal_index] == NULL) {
4457 pr_debug("%s: cal_index %d not allocated!\n",
4458 __func__, cal_index);
4459 goto end;
4460 }
4461
4462 if (get_cal_path(path) != RX_DEVICE) {
4463 pr_debug("%s: Invalid path to store calibration %d\n",
4464 __func__, path);
4465 rc = -EINVAL;
4466 goto end;
4467 }
4468
4469 port_id = afe_convert_virtual_to_portid(port_id);
4470 port_idx = adm_validate_and_get_port_index(port_id);
4471 if (port_idx < 0) {
4472 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
4473 rc = -EINVAL;
4474 goto end;
4475 }
4476
4477 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4478 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4479 return -EINVAL;
4480 }
4481
4482 acdb_id = atomic_read(&this_adm.copp.acdb_id[port_idx][copp_idx]);
4483 app_type = atomic_read(&this_adm.copp.app_type[port_idx][copp_idx]);
4484 sample_rate = atomic_read(&this_adm.copp.rate[port_idx][copp_idx]);
4485
4486 mutex_lock(&this_adm.cal_data[cal_index]->lock);
4487 cal_block = adm_find_cal(cal_index, get_cal_path(path), app_type,
4488 acdb_id, sample_rate);
4489 if (cal_block == NULL)
4490 goto unlock;
4491
4492 if (cal_block->cal_data.size <= 0) {
4493 pr_debug("%s: No ADM cal send for port_id = 0x%x!\n",
4494 __func__, port_id);
4495 rc = -EINVAL;
4496 goto unlock;
4497 }
4498
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05304499 if (cal_index == ADM_AUDPROC_CAL || cal_index == ADM_LSM_AUDPROC_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304500 if (cal_block->cal_data.size > AUD_PROC_BLOCK_SIZE) {
4501 pr_err("%s:audproc:invalid size exp/actual[%zd, %d]\n",
4502 __func__, cal_block->cal_data.size, *size);
4503 rc = -ENOMEM;
4504 goto unlock;
4505 }
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07004506 } else if (cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
4507 if (cal_block->cal_data.size > AUD_PROC_PERSIST_BLOCK_SIZE) {
4508 pr_err("%s:persist invalid size exp/actual[%zd, %d]\n",
4509 __func__, cal_block->cal_data.size, *size);
4510 rc = -ENOMEM;
4511 goto unlock;
4512 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304513 } else if (cal_index == ADM_AUDVOL_CAL) {
4514 if (cal_block->cal_data.size > AUD_VOL_BLOCK_SIZE) {
4515 pr_err("%s:aud_vol:invalid size exp/actual[%zd, %d]\n",
4516 __func__, cal_block->cal_data.size, *size);
4517 rc = -ENOMEM;
4518 goto unlock;
4519 }
4520 } else {
4521 pr_debug("%s: Not valid calibration for dolby topolgy\n",
4522 __func__);
4523 rc = -EINVAL;
4524 goto unlock;
4525 }
4526 memcpy(params, cal_block->cal_data.kvaddr, cal_block->cal_data.size);
4527 *size = cal_block->cal_data.size;
4528
4529 pr_debug("%s:port_id %d, copp_idx %d, path %d",
4530 __func__, port_id, copp_idx, path);
4531 pr_debug("perf_mode %d, cal_type %d, size %d\n",
4532 perf_mode, cal_index, *size);
4533
4534unlock:
4535 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
4536end:
4537 return rc;
4538}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304539EXPORT_SYMBOL(adm_store_cal_data);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304540
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304541/**
4542 * adm_send_compressed_device_mute -
4543 * command to send mute for compressed device
4544 *
4545 * @port_id: Port ID number
4546 * @copp_idx: copp index assigned
4547 * @mute_on: flag to indicate mute or unmute
4548 *
4549 * Returns 0 on success or error on failure
4550 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304551int adm_send_compressed_device_mute(int port_id, int copp_idx, bool mute_on)
4552{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004553 u32 mute_param = mute_on ? 1 : 0;
4554 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304555 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304556
4557 pr_debug("%s port_id: 0x%x, copp_idx %d, mute_on: %d\n",
4558 __func__, port_id, copp_idx, mute_on);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304559
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004560 memset(&param_hdr, 0, sizeof(param_hdr));
4561 param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_MUTE;
4562 param_hdr.instance_id = INSTANCE_ID_0;
4563 param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_MUTE;
4564 param_hdr.param_size = sizeof(mute_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304565
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004566 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4567 (uint8_t *) &mute_param);
4568 if (ret)
4569 pr_err("%s: Failed to set mute, err %d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304570
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304571 return ret;
4572}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304573EXPORT_SYMBOL(adm_send_compressed_device_mute);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304574
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304575/**
4576 * adm_send_compressed_device_latency -
4577 * command to send latency for compressed device
4578 *
4579 * @port_id: Port ID number
4580 * @copp_idx: copp index assigned
4581 * @latency: latency value to pass
4582 *
4583 * Returns 0 on success or error on failure
4584 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304585int adm_send_compressed_device_latency(int port_id, int copp_idx, int latency)
4586{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004587 u32 latency_param;
4588 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304589 int ret = 0;
4590
4591 pr_debug("%s port_id: 0x%x, copp_idx %d latency: %d\n", __func__,
4592 port_id, copp_idx, latency);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004593
4594 if (latency < 0) {
4595 pr_err("%s: Invalid value for latency %d", __func__, latency);
4596 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304597 }
4598
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004599 memset(&param_hdr, 0, sizeof(param_hdr));
4600 param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_LATENCY;
4601 param_hdr.instance_id = INSTANCE_ID_0;
4602 param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_LATENCY;
4603 param_hdr.param_size = sizeof(latency_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304604
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004605 latency_param = latency;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304606
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004607 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4608 (uint8_t *) &latency_param);
4609 if (ret)
4610 pr_err("%s: Failed to set latency, err %d\n", __func__, ret);
4611
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304612 return ret;
4613}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304614EXPORT_SYMBOL(adm_send_compressed_device_latency);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304615
4616/**
4617 * adm_swap_speaker_channels
4618 *
4619 * Receives port_id, copp_idx, sample rate, spk_swap and
4620 * send MFC command to swap speaker channel.
4621 * Return zero on success. On failure returns nonzero.
4622 *
4623 * port_id - Passed value, port_id for which channels swap is wanted
4624 * copp_idx - Passed value, copp_idx for which channels swap is wanted
4625 * sample_rate - Passed value, sample rate used by app type config
4626 * spk_swap - Passed value, spk_swap for check if swap flag is set
4627 */
4628int adm_swap_speaker_channels(int port_id, int copp_idx,
4629 int sample_rate, bool spk_swap)
4630{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004631 struct audproc_mfc_param_media_fmt mfc_cfg;
4632 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304633 uint16_t num_channels;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004634 int port_idx = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304635 int ret = 0;
4636
4637 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4638 __func__, port_id, copp_idx);
4639 port_id = q6audio_convert_virtual_to_portid(port_id);
4640 port_idx = adm_validate_and_get_port_index(port_id);
4641 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
4642 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004643 return -EINVAL;
4644 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4645 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
4646 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304647 }
4648
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004649 num_channels = atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304650 if (num_channels != 2) {
4651 pr_debug("%s: Invalid number of channels: %d\n",
4652 __func__, num_channels);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004653 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304654 }
4655
4656 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004657 memset(&param_hdr, 0, sizeof(param_hdr));
4658
4659 param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
4660 param_hdr.instance_id = INSTANCE_ID_0;
4661 param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
4662 param_hdr.param_size = sizeof(mfc_cfg);
4663
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304664 mfc_cfg.sampling_rate = sample_rate;
4665 mfc_cfg.bits_per_sample =
4666 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
4667 mfc_cfg.num_channels = num_channels;
4668
4669 /* Currently applying speaker swap for only 2 channel use case */
4670 if (spk_swap) {
4671 mfc_cfg.channel_type[0] =
4672 (uint16_t) PCM_CHANNEL_FR;
4673 mfc_cfg.channel_type[1] =
4674 (uint16_t) PCM_CHANNEL_FL;
4675 } else {
4676 mfc_cfg.channel_type[0] =
4677 (uint16_t) PCM_CHANNEL_FL;
4678 mfc_cfg.channel_type[1] =
4679 (uint16_t) PCM_CHANNEL_FR;
4680 }
4681
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004682 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4683 (u8 *) &mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304684 if (ret < 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004685 pr_err("%s: Failed to set swap speaker channels on port[0x%x] failed %d\n",
4686 __func__, port_id, ret);
4687 return ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304688 }
4689
4690 pr_debug("%s: mfc_cfg Set params returned success", __func__);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004691 return 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304692}
4693EXPORT_SYMBOL(adm_swap_speaker_channels);
4694
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304695/**
4696 * adm_set_sound_focus -
4697 * Update sound focus info
4698 *
4699 * @port_id: Port ID number
4700 * @copp_idx: copp index assigned
4701 * @soundFocusData: sound focus data to pass
4702 *
4703 * Returns 0 on success or error on failure
4704 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304705int adm_set_sound_focus(int port_id, int copp_idx,
4706 struct sound_focus_param soundFocusData)
4707{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004708 struct adm_param_fluence_soundfocus_t soundfocus_params;
4709 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304710 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304711 int i;
4712
4713 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4714 __func__, port_id, copp_idx);
4715
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004716 memset(&param_hdr, 0, sizeof(param_hdr));
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07004717 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004718 param_hdr.instance_id = INSTANCE_ID_0;
4719 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
4720 param_hdr.param_size = sizeof(soundfocus_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304721
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004722 memset(&(soundfocus_params), 0xFF, sizeof(soundfocus_params));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304723 for (i = 0; i < MAX_SECTORS; i++) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004724 soundfocus_params.start_angles[i] =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304725 soundFocusData.start_angle[i];
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004726 soundfocus_params.enables[i] = soundFocusData.enable[i];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304727 pr_debug("%s: start_angle[%d] = %d\n",
4728 __func__, i, soundFocusData.start_angle[i]);
4729 pr_debug("%s: enable[%d] = %d\n",
4730 __func__, i, soundFocusData.enable[i]);
4731 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004732 soundfocus_params.gain_step = soundFocusData.gain_step;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304733 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData.gain_step);
4734
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004735 soundfocus_params.reserved = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304736
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004737 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4738 (uint8_t *) &soundfocus_params);
4739 if (ret)
4740 pr_err("%s: Failed to set sound focus params, err %d\n",
4741 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304742
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304743 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
4744
4745 return ret;
4746}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304747EXPORT_SYMBOL(adm_set_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304748
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304749/**
4750 * adm_get_sound_focus -
4751 * Retrieve sound focus info
4752 *
4753 * @port_id: Port ID number
4754 * @copp_idx: copp index assigned
4755 * @soundFocusData: pointer for sound focus data to be updated with
4756 *
4757 * Returns 0 on success or error on failure
4758 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304759int adm_get_sound_focus(int port_id, int copp_idx,
4760 struct sound_focus_param *soundFocusData)
4761{
4762 int ret = 0, i;
4763 char *params_value;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004764 uint32_t max_param_size = 0;
4765 struct adm_param_fluence_soundfocus_t *soundfocus_params = NULL;
4766 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304767
4768 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4769 __func__, port_id, copp_idx);
4770
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004771 max_param_size = sizeof(struct adm_param_fluence_soundfocus_t) +
4772 sizeof(union param_hdrs);
4773 params_value = kzalloc(max_param_size, GFP_KERNEL);
4774 if (!params_value)
4775 return -ENOMEM;
4776
4777 memset(&param_hdr, 0, sizeof(param_hdr));
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07004778 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004779 param_hdr.instance_id = INSTANCE_ID_0;
4780 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
4781 param_hdr.param_size = max_param_size;
4782 ret = adm_get_pp_params(port_id, copp_idx,
4783 ADM_CLIENT_ID_SOURCE_TRACKING, NULL, &param_hdr,
4784 params_value);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304785 if (ret) {
4786 pr_err("%s: get parameters failed ret:%d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304787 ret = -EINVAL;
4788 goto done;
4789 }
4790
4791 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
4792 pr_err("%s - get params returned error [%s]\n",
4793 __func__, adsp_err_get_err_str(
4794 this_adm.sourceTrackingData.apr_cmd_status));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304795 ret = adsp_err_get_lnx_err_code(
4796 this_adm.sourceTrackingData.apr_cmd_status);
4797 goto done;
4798 }
4799
4800 soundfocus_params = (struct adm_param_fluence_soundfocus_t *)
4801 params_value;
4802 for (i = 0; i < MAX_SECTORS; i++) {
4803 soundFocusData->start_angle[i] =
4804 soundfocus_params->start_angles[i];
4805 soundFocusData->enable[i] = soundfocus_params->enables[i];
4806 pr_debug("%s: start_angle[%d] = %d\n",
4807 __func__, i, soundFocusData->start_angle[i]);
4808 pr_debug("%s: enable[%d] = %d\n",
4809 __func__, i, soundFocusData->enable[i]);
4810 }
4811 soundFocusData->gain_step = soundfocus_params->gain_step;
4812 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData->gain_step);
4813
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304814done:
4815 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
4816
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004817 kfree(params_value);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304818 return ret;
4819}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304820EXPORT_SYMBOL(adm_get_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304821
4822static int adm_source_tracking_alloc_map_memory(void)
4823{
4824 int ret;
4825
4826 pr_debug("%s: Enter\n", __func__);
4827
Banajit Goswami08bb7362017-11-03 22:48:23 -07004828 ret = msm_audio_ion_alloc(&this_adm.sourceTrackingData.dma_buf,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304829 AUD_PROC_BLOCK_SIZE,
4830 &this_adm.sourceTrackingData.memmap.paddr,
4831 &this_adm.sourceTrackingData.memmap.size,
4832 &this_adm.sourceTrackingData.memmap.kvaddr);
4833 if (ret) {
4834 pr_err("%s: failed to allocate memory\n", __func__);
4835
4836 ret = -EINVAL;
4837 goto done;
4838 }
4839
4840 atomic_set(&this_adm.mem_map_index, ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
4841 ret = adm_memory_map_regions(&this_adm.sourceTrackingData.memmap.paddr,
4842 0,
4843 (uint32_t *)&this_adm.sourceTrackingData.memmap.size,
4844 1);
4845 if (ret < 0) {
4846 pr_err("%s: failed to map memory, paddr = 0x%pK, size = %d\n",
4847 __func__,
4848 (void *)this_adm.sourceTrackingData.memmap.paddr,
4849 (uint32_t)this_adm.sourceTrackingData.memmap.size);
4850
Banajit Goswami08bb7362017-11-03 22:48:23 -07004851 msm_audio_ion_free(this_adm.sourceTrackingData.dma_buf);
4852 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304853 this_adm.sourceTrackingData.memmap.size = 0;
4854 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
4855 this_adm.sourceTrackingData.memmap.paddr = 0;
4856 this_adm.sourceTrackingData.apr_cmd_status = -1;
4857 atomic_set(&this_adm.mem_map_handles
4858 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
4859
4860 ret = -EINVAL;
4861 goto done;
4862 }
4863 ret = 0;
4864 pr_debug("%s: paddr = 0x%pK, size = %d, mem_map_handle = 0x%x\n",
4865 __func__, (void *)this_adm.sourceTrackingData.memmap.paddr,
4866 (uint32_t)this_adm.sourceTrackingData.memmap.size,
4867 atomic_read(&this_adm.mem_map_handles
4868 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING]));
4869
4870done:
4871 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
4872
4873 return ret;
4874}
4875
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304876/**
4877 * adm_get_source_tracking -
4878 * Retrieve source tracking info
4879 *
4880 * @port_id: Port ID number
4881 * @copp_idx: copp index assigned
4882 * @sourceTrackingData: pointer for source track data to be updated with
4883 *
4884 * Returns 0 on success or error on failure
4885 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304886int adm_get_source_tracking(int port_id, int copp_idx,
4887 struct source_tracking_param *sourceTrackingData)
4888{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004889 struct adm_param_fluence_sourcetracking_t *source_tracking_params =
4890 NULL;
4891 struct mem_mapping_hdr mem_hdr;
4892 struct param_hdr_v3 param_hdr;
4893 int i = 0;
4894 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304895
4896 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4897 __func__, port_id, copp_idx);
4898
4899 if (!this_adm.sourceTrackingData.memmap.paddr) {
4900 /* Allocate and map shared memory for out of band usage */
4901 ret = adm_source_tracking_alloc_map_memory();
4902 if (ret != 0) {
4903 ret = -EINVAL;
4904 goto done;
4905 }
4906 }
4907
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004908 memset(&mem_hdr, 0, sizeof(mem_hdr));
4909 memset(&param_hdr, 0, sizeof(param_hdr));
4910 mem_hdr.data_payload_addr_lsw =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304911 lower_32_bits(this_adm.sourceTrackingData.memmap.paddr);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004912 mem_hdr.data_payload_addr_msw = msm_audio_populate_upper_32_bits(
4913 this_adm.sourceTrackingData.memmap.paddr);
4914 mem_hdr.mem_map_handle = atomic_read(
4915 &this_adm.mem_map_handles[ADM_MEM_MAP_INDEX_SOURCE_TRACKING]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304916
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07004917 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004918 param_hdr.instance_id = INSTANCE_ID_0;
4919 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOURCETRACKING;
4920 /*
4921 * This size should be the max size of the calibration data + header.
4922 * Use the union size to ensure max size is used.
4923 */
4924 param_hdr.param_size =
4925 sizeof(struct adm_param_fluence_sourcetracking_t) +
4926 sizeof(union param_hdrs);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304927
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004928 /*
4929 * Retrieving parameters out of band, so no need to provide a buffer for
4930 * the returned parameter data as it will be at the memory location
4931 * provided.
4932 */
4933 ret = adm_get_pp_params(port_id, copp_idx,
4934 ADM_CLIENT_ID_SOURCE_TRACKING, &mem_hdr,
4935 &param_hdr, NULL);
4936 if (ret) {
4937 pr_err("%s: Failed to get params, error %d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304938 goto done;
4939 }
4940
4941 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
4942 pr_err("%s - get params returned error [%s]\n",
4943 __func__, adsp_err_get_err_str(
4944 this_adm.sourceTrackingData.apr_cmd_status));
4945
4946 ret = adsp_err_get_lnx_err_code(
4947 this_adm.sourceTrackingData.apr_cmd_status);
4948 goto done;
4949 }
4950
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004951 /* How do we know what the param data was retrieved with for hdr size */
4952 source_tracking_params =
4953 (struct adm_param_fluence_sourcetracking_t
4954 *) (this_adm.sourceTrackingData.memmap.kvaddr +
4955 sizeof(struct param_hdr_v1));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304956 for (i = 0; i < MAX_SECTORS; i++) {
4957 sourceTrackingData->vad[i] = source_tracking_params->vad[i];
4958 pr_debug("%s: vad[%d] = %d\n",
4959 __func__, i, sourceTrackingData->vad[i]);
4960 }
4961 sourceTrackingData->doa_speech = source_tracking_params->doa_speech;
4962 pr_debug("%s: doa_speech = %d\n",
4963 __func__, sourceTrackingData->doa_speech);
4964
4965 for (i = 0; i < MAX_NOISE_SOURCE_INDICATORS; i++) {
4966 sourceTrackingData->doa_noise[i] =
4967 source_tracking_params->doa_noise[i];
4968 pr_debug("%s: doa_noise[%d] = %d\n",
4969 __func__, i, sourceTrackingData->doa_noise[i]);
4970 }
4971 for (i = 0; i < MAX_POLAR_ACTIVITY_INDICATORS; i++) {
4972 sourceTrackingData->polar_activity[i] =
4973 source_tracking_params->polar_activity[i];
4974 pr_debug("%s: polar_activity[%d] = %d\n",
4975 __func__, i, sourceTrackingData->polar_activity[i]);
4976 }
4977
4978 ret = 0;
4979
4980done:
4981 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
4982
4983 return ret;
4984}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304985EXPORT_SYMBOL(adm_get_source_tracking);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304986
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304987int __init adm_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304988{
4989 int i = 0, j;
4990
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304991 this_adm.ec_ref_rx = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304992 init_waitqueue_head(&this_adm.matrix_map_wait);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304993 init_waitqueue_head(&this_adm.adm_wait);
4994
4995 for (i = 0; i < AFE_MAX_PORTS; i++) {
4996 for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
4997 atomic_set(&this_adm.copp.id[i][j], RESET_COPP_ID);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304998 init_waitqueue_head(&this_adm.copp.wait[i][j]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304999 init_waitqueue_head(
5000 &this_adm.copp.adm_delay_wait[i][j]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305001 }
5002 }
5003
5004 if (adm_init_cal_data())
5005 pr_err("%s: could not init cal data!\n", __func__);
5006
Banajit Goswami08bb7362017-11-03 22:48:23 -07005007 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305008 this_adm.sourceTrackingData.memmap.size = 0;
5009 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
5010 this_adm.sourceTrackingData.memmap.paddr = 0;
5011 this_adm.sourceTrackingData.apr_cmd_status = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305012
5013 return 0;
5014}
5015
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05305016void adm_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305017{
Laxminath Kasam30ad7512017-11-28 12:40:22 +05305018 if (this_adm.apr)
5019 adm_reset_data();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305020 adm_delete_cal_data();
5021}