blob: b39b449222426a85fe88cabcbe90b714d191e9b4 [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
Dieter Luecking50c55352018-09-28 14:29:17 +02002906 if (q6core_get_avcs_api_version_per_service(
2907 APRV2_IDS_SERVICE_ID_ADSP_ADM_V) >=
2908 ADSP_ADM_API_VERSION_V3) {
2909 memset(&open_v8, 0, sizeof(open_v8));
2910 memset(&ep1_payload, 0, sizeof(ep1_payload));
2911 memset(&ep2_payload, 0, sizeof(ep2_payload));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302912
Dieter Luecking50c55352018-09-28 14:29:17 +02002913 open_v8.hdr.hdr_field = APR_HDR_FIELD(
2914 APR_MSG_TYPE_SEQ_CMD,
2915 APR_HDR_LEN(APR_HDR_SIZE),
2916 APR_PKT_VER);
2917 open_v8.hdr.src_svc = APR_SVC_ADM;
2918 open_v8.hdr.src_domain = APR_DOMAIN_APPS;
2919 open_v8.hdr.src_port = tmp_port;
2920 open_v8.hdr.dest_svc = APR_SVC_ADM;
2921 open_v8.hdr.dest_domain = APR_DOMAIN_ADSP;
2922 open_v8.hdr.dest_port = tmp_port;
2923 open_v8.hdr.token = port_idx << 16 | copp_idx;
2924 open_v8.hdr.opcode = ADM_CMD_DEVICE_OPEN_V8;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302925
Dieter Luecking50c55352018-09-28 14:29:17 +02002926 if (this_adm.native_mode != 0) {
2927 open_v8.flags = flags |
2928 (this_adm.native_mode << 11);
2929 this_adm.native_mode = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302930 } else {
Dieter Luecking50c55352018-09-28 14:29:17 +02002931 open_v8.flags = flags;
2932 }
2933 open_v8.mode_of_operation = path;
2934 open_v8.endpoint_id_1 = tmp_port;
2935 open_v8.endpoint_id_2 = 0xFFFF;
2936 open_v8.endpoint_id_3 = 0xFFFF;
2937
2938 if (this_adm.ec_ref_rx && (path != 1)) {
2939 open_v8.endpoint_id_2 = this_adm.ec_ref_rx;
2940 this_adm.ec_ref_rx = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302941 }
2942
Dieter Luecking50c55352018-09-28 14:29:17 +02002943 open_v8.topology_id = topology;
2944 open_v8.reserved = 0;
2945
2946 /* variable endpoint payload */
2947 ep1_payload.dev_num_channel = channel_mode & 0x00FF;
2948 ep1_payload.bit_width = bit_width;
2949 ep1_payload.sample_rate = rate;
2950 ret = adm_arrange_mch_map_v8(&ep1_payload, path,
2951 channel_mode);
2952 if (ret)
2953 return ret;
2954
2955 pr_debug("%s: port_id=0x%x %x %x topology_id=0x%X flags %x ref_ch %x\n",
2956 __func__, open_v8.endpoint_id_1,
2957 open_v8.endpoint_id_2,
2958 open_v8.endpoint_id_3,
2959 open_v8.topology_id,
2960 open_v8.flags,
2961 this_adm.num_ec_ref_rx_chans);
2962
2963 ep1_payload_size = 8 +
2964 roundup(ep1_payload.dev_num_channel, 4);
2965 param_size = sizeof(struct adm_cmd_device_open_v8)
2966 + ep1_payload_size;
2967 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2968
2969 if ((this_adm.num_ec_ref_rx_chans != 0) && (path != 1)
2970 && (open_v8.endpoint_id_2 != 0xFFFF)) {
2971 ep2_payload.dev_num_channel =
2972 this_adm.num_ec_ref_rx_chans;
2973 this_adm.num_ec_ref_rx_chans = 0;
2974
2975 if (this_adm.ec_ref_rx_bit_width != 0) {
2976 ep2_payload.bit_width =
2977 this_adm.ec_ref_rx_bit_width;
2978 this_adm.ec_ref_rx_bit_width = 0;
2979 } else {
2980 ep2_payload.bit_width = bit_width;
2981 }
2982
2983 if (this_adm.ec_ref_rx_sampling_rate != 0) {
2984 ep2_payload.sample_rate =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302985 this_adm.ec_ref_rx_sampling_rate;
Dieter Luecking50c55352018-09-28 14:29:17 +02002986 this_adm.ec_ref_rx_sampling_rate = 0;
2987 } else {
2988 ep2_payload.sample_rate = rate;
2989 }
2990
2991 pr_debug("%s: adm open_v8 eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
2992 __func__,
2993 ep2_payload.dev_num_channel,
2994 ep2_payload.bit_width,
2995 ep2_payload.sample_rate);
2996
2997 ret = adm_arrange_mch_ep2_map_v8(&ep2_payload,
2998 ep2_payload.dev_num_channel);
2999
3000 if (ret)
3001 return ret;
3002 ep2_payload_size = 8 +
3003 roundup(ep2_payload.dev_num_channel, 4);
3004 param_size += ep2_payload_size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303005 }
3006
Dieter Luecking50c55352018-09-28 14:29:17 +02003007 adm_params = kzalloc(param_size, GFP_KERNEL);
3008 if (!adm_params)
3009 return -ENOMEM;
3010 open_v8.hdr.pkt_size = param_size;
3011 memcpy(adm_params, &open_v8, sizeof(open_v8));
3012 memcpy(adm_params + sizeof(open_v8),
3013 (void *)&ep1_payload,
3014 ep1_payload_size);
3015 memcpy(adm_params + sizeof(open_v8)
3016 + ep1_payload_size,
3017 (void *)&ep2_payload,
3018 ep2_payload_size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303019
Dieter Luecking50c55352018-09-28 14:29:17 +02003020 ret = apr_send_pkt(this_adm.apr,
3021 (uint32_t *)adm_params);
3022 if (ret < 0) {
3023 pr_err("%s: port_id: 0x%x for[0x%x] failed %d for open_v8\n",
3024 __func__, tmp_port, port_id, ret);
3025 return -EINVAL;
3026 }
3027 kfree(adm_params);
3028 } else {
3029
3030 open.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3031 APR_HDR_LEN(APR_HDR_SIZE),
3032 APR_PKT_VER);
3033 open.hdr.pkt_size = sizeof(open);
3034 open.hdr.src_svc = APR_SVC_ADM;
3035 open.hdr.src_domain = APR_DOMAIN_APPS;
3036 open.hdr.src_port = tmp_port;
3037 open.hdr.dest_svc = APR_SVC_ADM;
3038 open.hdr.dest_domain = APR_DOMAIN_ADSP;
3039 open.hdr.dest_port = tmp_port;
3040 open.hdr.token = port_idx << 16 | copp_idx;
3041 open.hdr.opcode = ADM_CMD_DEVICE_OPEN_V5;
3042 open.flags = flags;
3043 open.mode_of_operation = path;
3044 open.endpoint_id_1 = tmp_port;
3045 open.endpoint_id_2 = 0xFFFF;
3046
3047 if (this_adm.ec_ref_rx && (path != 1)) {
3048 open.endpoint_id_2 = this_adm.ec_ref_rx;
3049 this_adm.ec_ref_rx = -1;
3050 }
3051
3052 open.topology_id = topology;
3053
3054 open.dev_num_channel = channel_mode & 0x00FF;
3055 open.bit_width = bit_width;
3056 WARN_ON((perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) &&
3057 (rate != ULL_SUPPORTED_SAMPLE_RATE));
3058 open.sample_rate = rate;
3059
3060 ret = adm_arrange_mch_map(&open, path, channel_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303061
3062 if (ret)
3063 return ret;
3064
Dieter Luecking50c55352018-09-28 14:29:17 +02003065 pr_debug("%s: port_id=0x%x rate=%d topology_id=0x%X\n",
3066 __func__, open.endpoint_id_1, open.sample_rate,
3067 open.topology_id);
3068
3069 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3070
3071 if ((this_adm.num_ec_ref_rx_chans != 0) &&
3072 (path != 1) && (open.endpoint_id_2 != 0xFFFF)) {
3073 memset(&open_v6, 0,
3074 sizeof(struct adm_cmd_device_open_v6));
3075 memcpy(&open_v6, &open,
3076 sizeof(struct adm_cmd_device_open_v5));
3077 open_v6.hdr.opcode = ADM_CMD_DEVICE_OPEN_V6;
3078 open_v6.hdr.pkt_size = sizeof(open_v6);
3079 open_v6.dev_num_channel_eid2 =
3080 this_adm.num_ec_ref_rx_chans;
3081 this_adm.num_ec_ref_rx_chans = 0;
3082
3083 if (this_adm.ec_ref_rx_bit_width != 0) {
3084 open_v6.bit_width_eid2 =
3085 this_adm.ec_ref_rx_bit_width;
3086 this_adm.ec_ref_rx_bit_width = 0;
3087 } else {
3088 open_v6.bit_width_eid2 = bit_width;
3089 }
3090
3091 if (this_adm.ec_ref_rx_sampling_rate != 0) {
3092 open_v6.sample_rate_eid2 =
3093 this_adm.ec_ref_rx_sampling_rate;
3094 this_adm.ec_ref_rx_sampling_rate = 0;
3095 } else {
3096 open_v6.sample_rate_eid2 = rate;
3097 }
3098
3099 pr_debug("%s: eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
3100 __func__, open_v6.dev_num_channel_eid2,
3101 open_v6.bit_width_eid2,
3102 open_v6.sample_rate_eid2);
3103
3104 ret = adm_arrange_mch_ep2_map(&open_v6,
3105 open_v6.dev_num_channel_eid2);
3106
3107 if (ret)
3108 return ret;
3109
3110 ret = apr_send_pkt(this_adm.apr,
3111 (uint32_t *)&open_v6);
3112 } else {
3113 ret = apr_send_pkt(this_adm.apr,
3114 (uint32_t *)&open);
3115 }
3116 if (ret < 0) {
3117 pr_err("%s: port_id: 0x%x for[0x%x] failed %d\n",
3118 __func__, tmp_port, port_id, ret);
3119 return -EINVAL;
3120 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303121 }
Dieter Luecking50c55352018-09-28 14:29:17 +02003122
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303123 /* Wait for the callback with copp id */
3124 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3125 atomic_read(&this_adm.copp.stat
3126 [port_idx][copp_idx]) >= 0,
3127 msecs_to_jiffies(TIMEOUT_MS));
3128 if (!ret) {
3129 pr_err("%s: ADM open timedout for port_id: 0x%x for [0x%x]\n",
3130 __func__, tmp_port, port_id);
3131 return -EINVAL;
3132 } else if (atomic_read(&this_adm.copp.stat
3133 [port_idx][copp_idx]) > 0) {
3134 pr_err("%s: DSP returned error[%s]\n",
3135 __func__, adsp_err_get_err_str(
3136 atomic_read(&this_adm.copp.stat
3137 [port_idx][copp_idx])));
3138 return adsp_err_get_lnx_err_code(
3139 atomic_read(&this_adm.copp.stat
3140 [port_idx][copp_idx]));
3141 }
3142 }
3143 atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
3144 return copp_idx;
3145}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303146EXPORT_SYMBOL(adm_open);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303147
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303148/**
3149 * adm_copp_mfc_cfg -
3150 * command to send ADM MFC config
3151 *
3152 * @port_id: Port ID number
3153 * @copp_idx: copp index assigned
3154 * @dst_sample_rate: sink sample rate
3155 *
3156 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303157void adm_copp_mfc_cfg(int port_id, int copp_idx, int dst_sample_rate)
3158{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003159 struct audproc_mfc_param_media_fmt mfc_cfg;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303160 struct adm_cmd_device_open_v5 open;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003161 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303162 int port_idx;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303163 int rc = 0;
3164 int i = 0;
3165
3166 port_id = q6audio_convert_virtual_to_portid(port_id);
3167 port_idx = adm_validate_and_get_port_index(port_id);
3168
3169 if (port_idx < 0) {
3170 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
3171 goto fail_cmd;
3172 }
3173
3174 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3175 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3176 goto fail_cmd;
3177 }
3178
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003179 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
3180 memset(&open, 0, sizeof(open));
3181 memset(&param_hdr, 0, sizeof(param_hdr));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303182
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003183 param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
3184 param_hdr.instance_id = INSTANCE_ID_0;
3185 param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
3186 param_hdr.param_size = sizeof(mfc_cfg);
3187
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303188 mfc_cfg.sampling_rate = dst_sample_rate;
3189 mfc_cfg.bits_per_sample =
3190 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
3191 open.dev_num_channel = mfc_cfg.num_channels =
3192 atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
3193
3194 rc = adm_arrange_mch_map(&open, ADM_PATH_PLAYBACK,
3195 mfc_cfg.num_channels);
3196 if (rc < 0) {
3197 pr_err("%s: unable to get channal map\n", __func__);
3198 goto fail_cmd;
3199 }
3200
3201 for (i = 0; i < mfc_cfg.num_channels; i++)
3202 mfc_cfg.channel_type[i] =
3203 (uint16_t) open.dev_channel_mapping[i];
3204
3205 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3206
3207 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",
3208 __func__, port_idx, copp_idx,
3209 atomic_read(&this_adm.copp.rate[port_idx][copp_idx]),
3210 mfc_cfg.bits_per_sample, mfc_cfg.num_channels,
3211 mfc_cfg.sampling_rate);
3212
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003213 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
3214 (uint8_t *) &mfc_cfg);
3215 if (rc)
3216 pr_err("%s: Failed to set media format configuration data, err %d\n",
3217 __func__, rc);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303218
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303219fail_cmd:
3220 return;
3221}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303222EXPORT_SYMBOL(adm_copp_mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303223
3224static void route_set_opcode_matrix_id(
3225 struct adm_cmd_matrix_map_routings_v5 **route_addr,
3226 int path, uint32_t passthr_mode)
3227{
3228 struct adm_cmd_matrix_map_routings_v5 *route = *route_addr;
3229
3230 switch (path) {
3231 case ADM_PATH_PLAYBACK:
3232 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
3233 route->matrix_id = ADM_MATRIX_ID_AUDIO_RX;
3234 break;
3235 case ADM_PATH_LIVE_REC:
3236 if (passthr_mode == LISTEN) {
3237 route->hdr.opcode =
3238 ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3239 route->matrix_id = ADM_MATRIX_ID_LISTEN_TX;
3240 break;
3241 }
3242 /* fall through to set matrix id for non-listen case */
3243 case ADM_PATH_NONLIVE_REC:
3244 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
3245 route->matrix_id = ADM_MATRIX_ID_AUDIO_TX;
3246 break;
3247 case ADM_PATH_COMPRESSED_RX:
3248 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3249 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_RX;
3250 break;
3251 case ADM_PATH_COMPRESSED_TX:
3252 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3253 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_TX;
3254 break;
3255 default:
3256 pr_err("%s: Wrong path set[%d]\n", __func__, path);
3257 break;
3258 }
3259 pr_debug("%s: opcode 0x%x, matrix id %d\n",
3260 __func__, route->hdr.opcode, route->matrix_id);
3261}
3262
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303263/**
3264 * adm_matrix_map -
3265 * command to send ADM matrix map for ADM copp list
3266 *
3267 * @path: direction or ADM path type
3268 * @payload_map: have info of session id and associated copp_idx/num_copps
3269 * @perf_mode: performance mode like LL/ULL/..
3270 * @passthr_mode: flag to indicate passthrough mode
3271 *
3272 * Returns 0 on success or error on failure
3273 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303274int adm_matrix_map(int path, struct route_payload payload_map, int perf_mode,
3275 uint32_t passthr_mode)
3276{
3277 struct adm_cmd_matrix_map_routings_v5 *route;
3278 struct adm_session_map_node_v5 *node;
3279 uint16_t *copps_list;
3280 int cmd_size = 0;
3281 int ret = 0, i = 0;
3282 void *payload = NULL;
3283 void *matrix_map = NULL;
3284 int port_idx, copp_idx;
3285
3286 /* Assumes port_ids have already been validated during adm_open */
3287 cmd_size = (sizeof(struct adm_cmd_matrix_map_routings_v5) +
3288 sizeof(struct adm_session_map_node_v5) +
3289 (sizeof(uint32_t) * payload_map.num_copps));
3290 matrix_map = kzalloc(cmd_size, GFP_KERNEL);
3291 if (matrix_map == NULL) {
3292 pr_err("%s: Mem alloc failed\n", __func__);
3293 ret = -EINVAL;
3294 return ret;
3295 }
3296 route = (struct adm_cmd_matrix_map_routings_v5 *)matrix_map;
3297
3298 route->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3299 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
3300 route->hdr.pkt_size = cmd_size;
3301 route->hdr.src_svc = 0;
3302 route->hdr.src_domain = APR_DOMAIN_APPS;
3303 route->hdr.src_port = 0; /* Ignored */;
3304 route->hdr.dest_svc = APR_SVC_ADM;
3305 route->hdr.dest_domain = APR_DOMAIN_ADSP;
3306 route->hdr.dest_port = 0; /* Ignored */;
3307 route->hdr.token = 0;
3308 route->num_sessions = 1;
3309 route_set_opcode_matrix_id(&route, path, passthr_mode);
3310
3311 payload = ((u8 *)matrix_map +
3312 sizeof(struct adm_cmd_matrix_map_routings_v5));
3313 node = (struct adm_session_map_node_v5 *)payload;
3314
3315 node->session_id = payload_map.session_id;
3316 node->num_copps = payload_map.num_copps;
3317 payload = (u8 *)node + sizeof(struct adm_session_map_node_v5);
3318 copps_list = (uint16_t *)payload;
3319 for (i = 0; i < payload_map.num_copps; i++) {
3320 port_idx =
3321 adm_validate_and_get_port_index(payload_map.port_id[i]);
3322 if (port_idx < 0) {
3323 pr_err("%s: Invalid port_id 0x%x\n", __func__,
3324 payload_map.port_id[i]);
3325 ret = -EINVAL;
3326 goto fail_cmd;
3327 }
3328 copp_idx = payload_map.copp_idx[i];
3329 copps_list[i] = atomic_read(&this_adm.copp.id[port_idx]
3330 [copp_idx]);
3331 }
3332 atomic_set(&this_adm.matrix_map_stat, -1);
3333
3334 ret = apr_send_pkt(this_adm.apr, (uint32_t *)matrix_map);
3335 if (ret < 0) {
3336 pr_err("%s: routing for syream %d failed ret %d\n",
3337 __func__, payload_map.session_id, ret);
3338 ret = -EINVAL;
3339 goto fail_cmd;
3340 }
3341 ret = wait_event_timeout(this_adm.matrix_map_wait,
3342 atomic_read(&this_adm.matrix_map_stat) >= 0,
3343 msecs_to_jiffies(TIMEOUT_MS));
3344 if (!ret) {
3345 pr_err("%s: routing for syream %d failed\n", __func__,
3346 payload_map.session_id);
3347 ret = -EINVAL;
3348 goto fail_cmd;
3349 } else if (atomic_read(&this_adm.matrix_map_stat) > 0) {
3350 pr_err("%s: DSP returned error[%s]\n", __func__,
3351 adsp_err_get_err_str(atomic_read(
3352 &this_adm.matrix_map_stat)));
3353 ret = adsp_err_get_lnx_err_code(
3354 atomic_read(&this_adm.matrix_map_stat));
3355 goto fail_cmd;
3356 }
3357
3358 if ((perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) &&
3359 (path != ADM_PATH_COMPRESSED_RX)) {
3360 for (i = 0; i < payload_map.num_copps; i++) {
3361 port_idx = afe_get_port_index(payload_map.port_id[i]);
3362 copp_idx = payload_map.copp_idx[i];
3363 if (port_idx < 0 || copp_idx < 0 ||
3364 (copp_idx > MAX_COPPS_PER_PORT - 1)) {
3365 pr_err("%s: Invalid idx port_idx %d copp_idx %d\n",
3366 __func__, port_idx, copp_idx);
3367 continue;
3368 }
3369 rtac_add_adm_device(payload_map.port_id[i],
3370 atomic_read(&this_adm.copp.id
3371 [port_idx][copp_idx]),
3372 get_cal_path(path),
3373 payload_map.session_id,
3374 payload_map.app_type[i],
3375 payload_map.acdb_dev_id[i]);
3376
3377 if (!test_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3378 (void *)&this_adm.copp.adm_status[port_idx]
3379 [copp_idx])) {
3380 pr_debug("%s: adm copp[0x%x][%d] already sent",
3381 __func__, port_idx, copp_idx);
3382 continue;
3383 }
3384 send_adm_cal(payload_map.port_id[i], copp_idx,
3385 get_cal_path(path), perf_mode,
3386 payload_map.app_type[i],
3387 payload_map.acdb_dev_id[i],
Aditya Bavanari5106b562018-01-08 13:16:32 +05303388 payload_map.sample_rate[i],
3389 passthr_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303390 /* ADM COPP calibration is already sent */
3391 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3392 (void *)&this_adm.copp.
3393 adm_status[port_idx][copp_idx]);
3394 pr_debug("%s: copp_id: %d\n", __func__,
3395 atomic_read(&this_adm.copp.id[port_idx]
3396 [copp_idx]));
3397 }
3398 }
3399
3400fail_cmd:
3401 kfree(matrix_map);
3402 return ret;
3403}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303404EXPORT_SYMBOL(adm_matrix_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303405
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303406/**
3407 * adm_ec_ref_rx_id -
3408 * Update EC ref port ID
3409 *
3410 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303411void adm_ec_ref_rx_id(int port_id)
3412{
3413 this_adm.ec_ref_rx = port_id;
3414 pr_debug("%s: ec_ref_rx:%d\n", __func__, this_adm.ec_ref_rx);
3415}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303416EXPORT_SYMBOL(adm_ec_ref_rx_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303417
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303418/**
3419 * adm_num_ec_ref_rx_chans -
3420 * Update EC ref number of channels
3421 *
3422 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303423void adm_num_ec_ref_rx_chans(int num_chans)
3424{
3425 this_adm.num_ec_ref_rx_chans = num_chans;
3426 pr_debug("%s: num_ec_ref_rx_chans:%d\n",
3427 __func__, this_adm.num_ec_ref_rx_chans);
3428}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303429EXPORT_SYMBOL(adm_num_ec_ref_rx_chans);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303430
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303431/**
3432 * adm_ec_ref_rx_bit_width -
3433 * Update EC ref bit_width
3434 *
3435 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303436void adm_ec_ref_rx_bit_width(int bit_width)
3437{
3438 this_adm.ec_ref_rx_bit_width = bit_width;
3439 pr_debug("%s: ec_ref_rx_bit_width:%d\n",
3440 __func__, this_adm.ec_ref_rx_bit_width);
3441}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303442EXPORT_SYMBOL(adm_ec_ref_rx_bit_width);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303443
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303444/**
3445 * adm_ec_ref_rx_sampling_rate -
3446 * Update EC ref sample rate
3447 *
3448 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303449void adm_ec_ref_rx_sampling_rate(int sampling_rate)
3450{
3451 this_adm.ec_ref_rx_sampling_rate = sampling_rate;
3452 pr_debug("%s: ec_ref_rx_sampling_rate:%d\n",
3453 __func__, this_adm.ec_ref_rx_sampling_rate);
3454}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303455EXPORT_SYMBOL(adm_ec_ref_rx_sampling_rate);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303456
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303457/**
Dieter Luecking50c55352018-09-28 14:29:17 +02003458 * adm_set_native_mode -
3459 * Set adm channel native mode.
3460 * If enabled matrix mixer will be
3461 * running in native mode for channel
3462 * configuration for this device session.
3463 *
3464 */
3465void adm_set_native_mode(int mode)
3466{
3467 this_adm.native_mode = mode;
3468 pr_debug("%s: enable native_mode :%d\n",
3469 __func__, this_adm.native_mode);
3470}
3471EXPORT_SYMBOL(adm_set_native_mode);
3472
3473/**
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303474 * adm_close -
3475 * command to close ADM copp
3476 *
3477 * @port_id: Port ID number
3478 * @perf_mode: performance mode like LL/ULL/..
3479 * @copp_idx: copp index assigned
3480 *
3481 * Returns 0 on success or error on failure
3482 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303483int adm_close(int port_id, int perf_mode, int copp_idx)
3484{
3485 struct apr_hdr close;
3486
3487 int ret = 0, port_idx;
3488 int copp_id = RESET_COPP_ID;
3489
3490 pr_debug("%s: port_id=0x%x perf_mode: %d copp_idx: %d\n", __func__,
3491 port_id, perf_mode, copp_idx);
3492
3493 port_id = q6audio_convert_virtual_to_portid(port_id);
3494 port_idx = adm_validate_and_get_port_index(port_id);
3495 if (port_idx < 0) {
3496 pr_err("%s: Invalid port_id 0x%x\n",
3497 __func__, port_id);
3498 return -EINVAL;
3499 }
3500
3501 if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
3502 pr_err("%s: Invalid copp idx: %d\n", __func__, copp_idx);
3503 return -EINVAL;
3504 }
3505
3506 if (this_adm.copp.adm_delay[port_idx][copp_idx] && perf_mode
3507 == LEGACY_PCM_MODE) {
3508 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
3509 1);
3510 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
3511 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
3512 }
3513
3514 atomic_dec(&this_adm.copp.cnt[port_idx][copp_idx]);
3515 if (!(atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]))) {
3516 copp_id = adm_get_copp_id(port_idx, copp_idx);
3517 pr_debug("%s: Closing ADM port_idx:%d copp_idx:%d copp_id:0x%x\n",
3518 __func__, port_idx, copp_idx, copp_id);
3519 if ((!perf_mode) && (this_adm.outband_memmap.paddr != 0) &&
3520 (atomic_read(&this_adm.copp.topology[port_idx][copp_idx]) ==
3521 SRS_TRUMEDIA_TOPOLOGY_ID)) {
3522 atomic_set(&this_adm.mem_map_index,
3523 ADM_SRS_TRUMEDIA);
3524 ret = adm_memory_unmap_regions();
3525 if (ret < 0) {
3526 pr_err("%s: adm mem unmmap err %d",
3527 __func__, ret);
3528 } else {
3529 atomic_set(&this_adm.mem_map_handles
3530 [ADM_SRS_TRUMEDIA], 0);
3531 }
3532 }
3533
3534
3535 if ((afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX) &&
3536 this_adm.sourceTrackingData.memmap.paddr) {
3537 atomic_set(&this_adm.mem_map_index,
3538 ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
3539 ret = adm_memory_unmap_regions();
3540 if (ret < 0) {
3541 pr_err("%s: adm mem unmmap err %d",
3542 __func__, ret);
3543 }
3544 msm_audio_ion_free(
Banajit Goswami08bb7362017-11-03 22:48:23 -07003545 this_adm.sourceTrackingData.dma_buf);
3546 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303547 this_adm.sourceTrackingData.memmap.size = 0;
3548 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
3549 this_adm.sourceTrackingData.memmap.paddr = 0;
3550 this_adm.sourceTrackingData.apr_cmd_status = -1;
3551 atomic_set(&this_adm.mem_map_handles[
3552 ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
3553 }
3554
3555 close.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3556 APR_HDR_LEN(APR_HDR_SIZE),
3557 APR_PKT_VER);
3558 close.pkt_size = sizeof(close);
3559 close.src_svc = APR_SVC_ADM;
3560 close.src_domain = APR_DOMAIN_APPS;
3561 close.src_port = port_id;
3562 close.dest_svc = APR_SVC_ADM;
3563 close.dest_domain = APR_DOMAIN_ADSP;
3564 close.dest_port = copp_id;
3565 close.token = port_idx << 16 | copp_idx;
3566 close.opcode = ADM_CMD_DEVICE_CLOSE_V5;
3567
3568 atomic_set(&this_adm.copp.id[port_idx][copp_idx],
3569 RESET_COPP_ID);
3570 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
3571 atomic_set(&this_adm.copp.topology[port_idx][copp_idx], 0);
3572 atomic_set(&this_adm.copp.mode[port_idx][copp_idx], 0);
3573 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3574 atomic_set(&this_adm.copp.rate[port_idx][copp_idx], 0);
3575 atomic_set(&this_adm.copp.channels[port_idx][copp_idx], 0);
3576 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx], 0);
3577 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx], 0);
3578
3579 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3580 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
3581
3582 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&close);
3583 if (ret < 0) {
3584 pr_err("%s: ADM close failed %d\n", __func__, ret);
3585 return -EINVAL;
3586 }
3587
3588 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3589 atomic_read(&this_adm.copp.stat
3590 [port_idx][copp_idx]) >= 0,
3591 msecs_to_jiffies(TIMEOUT_MS));
3592 if (!ret) {
3593 pr_err("%s: ADM cmd Route timedout for port 0x%x\n",
3594 __func__, port_id);
3595 return -EINVAL;
3596 } else if (atomic_read(&this_adm.copp.stat
3597 [port_idx][copp_idx]) > 0) {
3598 pr_err("%s: DSP returned error[%s]\n",
3599 __func__, adsp_err_get_err_str(
3600 atomic_read(&this_adm.copp.stat
3601 [port_idx][copp_idx])));
3602 return adsp_err_get_lnx_err_code(
3603 atomic_read(&this_adm.copp.stat
3604 [port_idx][copp_idx]));
3605 }
3606 }
3607
3608 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) {
3609 pr_debug("%s: remove adm device from rtac\n", __func__);
3610 rtac_remove_adm_device(port_id, copp_id);
3611 }
3612 return 0;
3613}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303614EXPORT_SYMBOL(adm_close);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303615
3616int send_rtac_audvol_cal(void)
3617{
3618 int ret = 0;
3619 int ret2 = 0;
3620 int i = 0;
3621 int copp_idx, port_idx, acdb_id, app_id, path;
3622 struct cal_block_data *cal_block = NULL;
3623 struct audio_cal_info_audvol *audvol_cal_info = NULL;
3624 struct rtac_adm rtac_adm_data;
3625
3626 mutex_lock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3627
3628 cal_block = cal_utils_get_only_cal_block(
3629 this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]);
Vikram Panduranga770b8382017-09-27 12:17:36 -07003630 if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303631 pr_err("%s: can't find cal block!\n", __func__);
3632 goto unlock;
3633 }
3634
3635 audvol_cal_info = cal_block->cal_info;
3636 if (audvol_cal_info == NULL) {
3637 pr_err("%s: audvol_cal_info is NULL!\n", __func__);
3638 goto unlock;
3639 }
3640
3641 get_rtac_adm_data(&rtac_adm_data);
3642 for (; i < rtac_adm_data.num_of_dev; i++) {
3643
3644 acdb_id = rtac_adm_data.device[i].acdb_dev_id;
3645 if (acdb_id == 0)
3646 acdb_id = audvol_cal_info->acdb_id;
3647
3648 app_id = rtac_adm_data.device[i].app_type;
3649 if (app_id == 0)
3650 app_id = audvol_cal_info->app_type;
3651
3652 path = afe_get_port_type(rtac_adm_data.device[i].afe_port);
3653 if ((acdb_id == audvol_cal_info->acdb_id) &&
3654 (app_id == audvol_cal_info->app_type) &&
3655 (path == audvol_cal_info->path)) {
3656
3657 if (adm_get_indexes_from_copp_id(rtac_adm_data.
3658 device[i].copp, &copp_idx, &port_idx) != 0) {
3659 pr_debug("%s: Copp Id %d is not active\n",
3660 __func__,
3661 rtac_adm_data.device[i].copp);
3662 continue;
3663 }
3664
3665 ret2 = adm_remap_and_send_cal_block(ADM_RTAC_AUDVOL_CAL,
3666 rtac_adm_data.device[i].afe_port,
3667 copp_idx, cal_block,
3668 atomic_read(&this_adm.copp.
3669 mode[port_idx][copp_idx]),
3670 audvol_cal_info->app_type,
3671 audvol_cal_info->acdb_id,
3672 atomic_read(&this_adm.copp.
3673 rate[port_idx][copp_idx]));
3674 if (ret2 < 0) {
3675 pr_debug("%s: remap and send failed for copp Id %d, acdb id %d, app type %d, path %d\n",
3676 __func__, rtac_adm_data.device[i].copp,
3677 audvol_cal_info->acdb_id,
3678 audvol_cal_info->app_type,
3679 audvol_cal_info->path);
3680 ret = ret2;
3681 }
3682 }
3683 }
3684unlock:
3685 mutex_unlock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3686 return ret;
3687}
3688
3689int adm_map_rtac_block(struct rtac_cal_block_data *cal_block)
3690{
3691 int result = 0;
3692
3693 pr_debug("%s:\n", __func__);
3694
3695 if (cal_block == NULL) {
3696 pr_err("%s: cal_block is NULL!\n",
3697 __func__);
3698 result = -EINVAL;
3699 goto done;
3700 }
3701
3702 if (cal_block->cal_data.paddr == 0) {
3703 pr_debug("%s: No address to map!\n",
3704 __func__);
3705 result = -EINVAL;
3706 goto done;
3707 }
3708
3709 if (cal_block->map_data.map_size == 0) {
3710 pr_debug("%s: map size is 0!\n",
3711 __func__);
3712 result = -EINVAL;
3713 goto done;
3714 }
3715
3716 /* valid port ID needed for callback use primary I2S */
3717 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3718 result = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3719 &cal_block->map_data.map_size, 1);
3720 if (result < 0) {
3721 pr_err("%s: RTAC mmap did not work! size = %d result %d\n",
3722 __func__,
3723 cal_block->map_data.map_size, result);
3724 pr_debug("%s: RTAC mmap did not work! addr = 0x%pK, size = %d\n",
3725 __func__,
3726 &cal_block->cal_data.paddr,
3727 cal_block->map_data.map_size);
3728 goto done;
3729 }
3730
3731 cal_block->map_data.map_handle = atomic_read(
3732 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]);
3733done:
3734 return result;
3735}
3736
3737int adm_unmap_rtac_block(uint32_t *mem_map_handle)
3738{
3739 int result = 0;
3740
3741 pr_debug("%s:\n", __func__);
3742
3743 if (mem_map_handle == NULL) {
3744 pr_debug("%s: Map handle is NULL, nothing to unmap\n",
3745 __func__);
3746 goto done;
3747 }
3748
3749 if (*mem_map_handle == 0) {
3750 pr_debug("%s: Map handle is 0, nothing to unmap\n",
3751 __func__);
3752 goto done;
3753 }
3754
3755 if (*mem_map_handle != atomic_read(
3756 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL])) {
3757 pr_err("%s: Map handles do not match! Unmapping RTAC, RTAC map 0x%x, ADM map 0x%x\n",
3758 __func__, *mem_map_handle, atomic_read(
3759 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]));
3760
3761 /* if mismatch use handle passed in to unmap */
3762 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL],
3763 *mem_map_handle);
3764 }
3765
3766 /* valid port ID needed for callback use primary I2S */
3767 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3768 result = adm_memory_unmap_regions();
3769 if (result < 0) {
3770 pr_debug("%s: adm_memory_unmap_regions failed, error %d\n",
3771 __func__, result);
3772 } else {
3773 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL], 0);
3774 *mem_map_handle = 0;
3775 }
3776done:
3777 return result;
3778}
3779
3780static int get_cal_type_index(int32_t cal_type)
3781{
3782 int ret = -EINVAL;
3783
3784 switch (cal_type) {
3785 case ADM_AUDPROC_CAL_TYPE:
3786 ret = ADM_AUDPROC_CAL;
3787 break;
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05303788 case ADM_LSM_AUDPROC_CAL_TYPE:
3789 ret = ADM_LSM_AUDPROC_CAL;
3790 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303791 case ADM_AUDVOL_CAL_TYPE:
3792 ret = ADM_AUDVOL_CAL;
3793 break;
3794 case ADM_CUST_TOPOLOGY_CAL_TYPE:
3795 ret = ADM_CUSTOM_TOP_CAL;
3796 break;
3797 case ADM_RTAC_INFO_CAL_TYPE:
3798 ret = ADM_RTAC_INFO_CAL;
3799 break;
3800 case ADM_RTAC_APR_CAL_TYPE:
3801 ret = ADM_RTAC_APR_CAL;
3802 break;
3803 case ADM_RTAC_AUDVOL_CAL_TYPE:
3804 ret = ADM_RTAC_AUDVOL_CAL;
3805 break;
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07003806 case ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE:
3807 ret = ADM_LSM_AUDPROC_PERSISTENT_CAL;
3808 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303809 default:
3810 pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
3811 }
3812 return ret;
3813}
3814
3815static int adm_alloc_cal(int32_t cal_type, size_t data_size, void *data)
3816{
3817 int ret = 0;
3818 int cal_index;
3819
3820 pr_debug("%s:\n", __func__);
3821
3822 cal_index = get_cal_type_index(cal_type);
3823 if (cal_index < 0) {
3824 pr_err("%s: could not get cal index %d!\n",
3825 __func__, cal_index);
3826 ret = -EINVAL;
3827 goto done;
3828 }
3829
3830 ret = cal_utils_alloc_cal(data_size, data,
3831 this_adm.cal_data[cal_index], 0, NULL);
3832 if (ret < 0) {
3833 pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
3834 __func__, ret, cal_type);
3835 ret = -EINVAL;
3836 goto done;
3837 }
3838done:
3839 return ret;
3840}
3841
3842static int adm_dealloc_cal(int32_t cal_type, size_t data_size, void *data)
3843{
3844 int ret = 0;
3845 int cal_index;
3846
3847 pr_debug("%s:\n", __func__);
3848
3849 cal_index = get_cal_type_index(cal_type);
3850 if (cal_index < 0) {
3851 pr_err("%s: could not get cal index %d!\n",
3852 __func__, cal_index);
3853 ret = -EINVAL;
3854 goto done;
3855 }
3856
3857 ret = cal_utils_dealloc_cal(data_size, data,
3858 this_adm.cal_data[cal_index]);
3859 if (ret < 0) {
3860 pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
3861 __func__, ret, cal_type);
3862 ret = -EINVAL;
3863 goto done;
3864 }
3865done:
3866 return ret;
3867}
3868
3869static int adm_set_cal(int32_t cal_type, size_t data_size, void *data)
3870{
3871 int ret = 0;
3872 int cal_index;
3873
3874 pr_debug("%s:\n", __func__);
3875
3876 cal_index = get_cal_type_index(cal_type);
3877 if (cal_index < 0) {
3878 pr_err("%s: could not get cal index %d!\n",
3879 __func__, cal_index);
3880 ret = -EINVAL;
3881 goto done;
3882 }
3883
3884 ret = cal_utils_set_cal(data_size, data,
3885 this_adm.cal_data[cal_index], 0, NULL);
3886 if (ret < 0) {
3887 pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
3888 __func__, ret, cal_type);
3889 ret = -EINVAL;
3890 goto done;
3891 }
3892
3893 if (cal_index == ADM_CUSTOM_TOP_CAL) {
3894 mutex_lock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3895 this_adm.set_custom_topology = 1;
3896 mutex_unlock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3897 } else if (cal_index == ADM_RTAC_AUDVOL_CAL) {
3898 send_rtac_audvol_cal();
3899 }
3900done:
3901 return ret;
3902}
3903
3904static int adm_map_cal_data(int32_t cal_type,
3905 struct cal_block_data *cal_block)
3906{
3907 int ret = 0;
3908 int cal_index;
3909
3910 pr_debug("%s:\n", __func__);
3911
3912 cal_index = get_cal_type_index(cal_type);
3913 if (cal_index < 0) {
3914 pr_err("%s: could not get cal index %d!\n",
3915 __func__, cal_index);
3916 ret = -EINVAL;
3917 goto done;
3918 }
3919
3920 atomic_set(&this_adm.mem_map_index, cal_index);
3921 ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3922 (uint32_t *)&cal_block->map_data.map_size, 1);
3923 if (ret < 0) {
3924 pr_err("%s: map did not work! cal_type %i ret %d\n",
3925 __func__, cal_index, ret);
3926 ret = -ENODEV;
3927 goto done;
3928 }
3929 cal_block->map_data.q6map_handle = atomic_read(&this_adm.
3930 mem_map_handles[cal_index]);
3931done:
3932 return ret;
3933}
3934
3935static int adm_unmap_cal_data(int32_t cal_type,
3936 struct cal_block_data *cal_block)
3937{
3938 int ret = 0;
3939 int cal_index;
3940
3941 pr_debug("%s:\n", __func__);
3942
3943 cal_index = get_cal_type_index(cal_type);
3944 if (cal_index < 0) {
3945 pr_err("%s: could not get cal index %d!\n",
3946 __func__, cal_index);
3947 ret = -EINVAL;
3948 goto done;
3949 }
3950
3951 if (cal_block == NULL) {
3952 pr_err("%s: Cal block is NULL!\n",
3953 __func__);
3954 goto done;
3955 }
3956
3957 if (cal_block->map_data.q6map_handle == 0) {
3958 pr_err("%s: Map handle is NULL, nothing to unmap\n",
3959 __func__);
3960 goto done;
3961 }
3962
3963 atomic_set(&this_adm.mem_map_handles[cal_index],
3964 cal_block->map_data.q6map_handle);
3965 atomic_set(&this_adm.mem_map_index, cal_index);
3966 ret = adm_memory_unmap_regions();
3967 if (ret < 0) {
3968 pr_err("%s: unmap did not work! cal_type %i ret %d\n",
3969 __func__, cal_index, ret);
3970 ret = -ENODEV;
3971 goto done;
3972 }
3973 cal_block->map_data.q6map_handle = 0;
3974done:
3975 return ret;
3976}
3977
3978static void adm_delete_cal_data(void)
3979{
3980 pr_debug("%s:\n", __func__);
3981
3982 cal_utils_destroy_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data);
3983}
3984
3985static int adm_init_cal_data(void)
3986{
3987 int ret = 0;
3988 struct cal_type_info cal_type_info[] = {
3989 {{ADM_CUST_TOPOLOGY_CAL_TYPE,
3990 {adm_alloc_cal, adm_dealloc_cal, NULL,
3991 adm_set_cal, NULL, NULL} },
3992 {adm_map_cal_data, adm_unmap_cal_data,
3993 cal_utils_match_buf_num} },
3994
3995 {{ADM_AUDPROC_CAL_TYPE,
3996 {adm_alloc_cal, adm_dealloc_cal, NULL,
3997 adm_set_cal, NULL, NULL} },
3998 {adm_map_cal_data, adm_unmap_cal_data,
3999 cal_utils_match_buf_num} },
4000
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05304001 {{ADM_LSM_AUDPROC_CAL_TYPE,
4002 {adm_alloc_cal, adm_dealloc_cal, NULL,
4003 adm_set_cal, NULL, NULL} },
4004 {adm_map_cal_data, adm_unmap_cal_data,
4005 cal_utils_match_buf_num} },
4006
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304007 {{ADM_AUDVOL_CAL_TYPE,
4008 {adm_alloc_cal, adm_dealloc_cal, NULL,
4009 adm_set_cal, NULL, NULL} },
4010 {adm_map_cal_data, adm_unmap_cal_data,
4011 cal_utils_match_buf_num} },
4012
4013 {{ADM_RTAC_INFO_CAL_TYPE,
4014 {NULL, NULL, NULL, NULL, NULL, NULL} },
4015 {NULL, NULL, cal_utils_match_buf_num} },
4016
4017 {{ADM_RTAC_APR_CAL_TYPE,
4018 {NULL, NULL, NULL, NULL, NULL, NULL} },
4019 {NULL, NULL, cal_utils_match_buf_num} },
4020
4021 {{SRS_TRUMEDIA_CAL_TYPE,
4022 {NULL, NULL, NULL, NULL, NULL, NULL} },
4023 {NULL, NULL, cal_utils_match_buf_num} },
4024
4025 {{ADM_RTAC_AUDVOL_CAL_TYPE,
4026 {adm_alloc_cal, adm_dealloc_cal, NULL,
4027 adm_set_cal, NULL, NULL} },
4028 {adm_map_cal_data, adm_unmap_cal_data,
4029 cal_utils_match_buf_num} },
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07004030
4031 {{ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE,
4032 {adm_alloc_cal, adm_dealloc_cal, NULL,
4033 adm_set_cal, NULL, NULL} },
4034 {adm_map_cal_data, adm_unmap_cal_data,
4035 cal_utils_match_buf_num} },
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304036 };
4037 pr_debug("%s:\n", __func__);
4038
4039 ret = cal_utils_create_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data,
4040 cal_type_info);
4041 if (ret < 0) {
4042 pr_err("%s: could not create cal type! ret %d\n",
4043 __func__, ret);
4044 ret = -EINVAL;
4045 goto err;
4046 }
4047
4048 return ret;
4049err:
4050 adm_delete_cal_data();
4051 return ret;
4052}
4053
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304054/**
4055 * adm_set_volume -
4056 * command to set volume on ADM copp
4057 *
4058 * @port_id: Port ID number
4059 * @copp_idx: copp index assigned
4060 * @volume: gain value to set
4061 *
4062 * Returns 0 on success or error on failure
4063 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304064int adm_set_volume(int port_id, int copp_idx, int volume)
4065{
4066 struct audproc_volume_ctrl_master_gain audproc_vol;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004067 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304068 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304069
4070 pr_debug("%s: port_id %d, volume %d\n", __func__, port_id, volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304071
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004072 memset(&audproc_vol, 0, sizeof(audproc_vol));
4073 memset(&param_hdr, 0, sizeof(param_hdr));
4074 param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
4075 param_hdr.instance_id = INSTANCE_ID_0;
4076 param_hdr.param_id = AUDPROC_PARAM_ID_VOL_CTRL_MASTER_GAIN;
4077 param_hdr.param_size = sizeof(audproc_vol);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304078
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304079 audproc_vol.master_gain = volume;
4080
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004081 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4082 (uint8_t *) &audproc_vol);
4083 if (rc)
4084 pr_err("%s: Failed to set volume, err %d\n", __func__, rc);
4085
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304086 return rc;
4087}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304088EXPORT_SYMBOL(adm_set_volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304089
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304090/**
4091 * adm_set_softvolume -
4092 * command to set softvolume
4093 *
4094 * @port_id: Port ID number
4095 * @copp_idx: copp index assigned
4096 * @softvol_param: Params to set for softvolume
4097 *
4098 * Returns 0 on success or error on failure
4099 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304100int adm_set_softvolume(int port_id, int copp_idx,
4101 struct audproc_softvolume_params *softvol_param)
4102{
4103 struct audproc_soft_step_volume_params audproc_softvol;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004104 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304105 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304106
4107 pr_debug("%s: period %d step %d curve %d\n", __func__,
4108 softvol_param->period, softvol_param->step,
4109 softvol_param->rampingcurve);
4110
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004111 memset(&audproc_softvol, 0, sizeof(audproc_softvol));
4112 memset(&param_hdr, 0, sizeof(param_hdr));
4113 param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
4114 param_hdr.instance_id = INSTANCE_ID_0;
4115 param_hdr.param_id = AUDPROC_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
4116 param_hdr.param_size = sizeof(audproc_softvol);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304117
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304118 audproc_softvol.period = softvol_param->period;
4119 audproc_softvol.step = softvol_param->step;
4120 audproc_softvol.ramping_curve = softvol_param->rampingcurve;
4121
4122 pr_debug("%s: period %d, step %d, curve %d\n", __func__,
4123 audproc_softvol.period, audproc_softvol.step,
4124 audproc_softvol.ramping_curve);
4125
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004126 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4127 (uint8_t *) &audproc_softvol);
4128 if (rc)
4129 pr_err("%s: Failed to set soft volume, err %d\n", __func__, rc);
4130
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304131 return rc;
4132}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304133EXPORT_SYMBOL(adm_set_softvolume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304134
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304135/**
4136 * adm_set_mic_gain -
4137 * command to set MIC gain
4138 *
4139 * @port_id: Port ID number
4140 * @copp_idx: copp index assigned
4141 * @volume: gain value to set
4142 *
4143 * Returns 0 on success or error on failure
4144 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304145int adm_set_mic_gain(int port_id, int copp_idx, int volume)
4146{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004147 struct admx_mic_gain mic_gain_params;
4148 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304149 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304150
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004151 pr_debug("%s: Setting mic gain to %d at port_id 0x%x\n", __func__,
4152 volume, port_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304153
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004154 memset(&mic_gain_params, 0, sizeof(mic_gain_params));
4155 memset(&param_hdr, 0, sizeof(param_hdr));
4156 param_hdr.module_id = ADM_MODULE_IDX_MIC_GAIN_CTRL;
4157 param_hdr.instance_id = INSTANCE_ID_0;
4158 param_hdr.param_id = ADM_PARAM_IDX_MIC_GAIN;
4159 param_hdr.param_size = sizeof(mic_gain_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304160
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004161 mic_gain_params.tx_mic_gain = volume;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304162
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004163 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4164 (uint8_t *) &mic_gain_params);
4165 if (rc)
4166 pr_err("%s: Failed to set mic gain, err %d\n", __func__, rc);
4167
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304168 return rc;
4169}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304170EXPORT_SYMBOL(adm_set_mic_gain);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304171
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304172/**
4173 * adm_send_set_multichannel_ec_primary_mic_ch -
4174 * command to set multi-ch EC primary mic
4175 *
4176 * @port_id: Port ID number
4177 * @copp_idx: copp index assigned
4178 * @primary_mic_ch: channel number of primary mic
4179 *
4180 * Returns 0 on success or error on failure
4181 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304182int adm_send_set_multichannel_ec_primary_mic_ch(int port_id, int copp_idx,
4183 int primary_mic_ch)
4184{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004185 struct admx_sec_primary_mic_ch sec_primary_ch_params;
4186 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304187 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304188
4189 pr_debug("%s port_id 0x%x, copp_idx 0x%x, primary_mic_ch %d\n",
4190 __func__, port_id, copp_idx, primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304191
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004192 memset(&sec_primary_ch_params, 0, sizeof(sec_primary_ch_params));
4193 memset(&param_hdr, 0, sizeof(param_hdr));
4194 param_hdr.module_id = AUDPROC_MODULE_ID_VOICE_TX_SECNS;
4195 param_hdr.instance_id = INSTANCE_ID_0;
4196 param_hdr.param_id = AUDPROC_PARAM_IDX_SEC_PRIMARY_MIC_CH;
4197 param_hdr.param_size = sizeof(sec_primary_ch_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304198
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004199 sec_primary_ch_params.version = 0;
4200 sec_primary_ch_params.sec_primary_mic_ch = primary_mic_ch;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304201
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004202 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4203 (uint8_t *) &sec_primary_ch_params);
4204 if (rc)
4205 pr_err("%s: Failed to set primary mic chanel, err %d\n",
4206 __func__, rc);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304207
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304208 return rc;
4209}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304210EXPORT_SYMBOL(adm_send_set_multichannel_ec_primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304211
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304212/**
4213 * adm_param_enable -
4214 * command to send params to ADM for given module
4215 *
4216 * @port_id: Port ID number
4217 * @copp_idx: copp index assigned
4218 * @module_id: ADM module
4219 * @enable: flag to enable or disable module
4220 *
4221 * Returns 0 on success or error on failure
4222 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304223int adm_param_enable(int port_id, int copp_idx, int module_id, int enable)
4224{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004225 struct module_instance_info mod_inst_info;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304226
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004227 memset(&mod_inst_info, 0, sizeof(mod_inst_info));
4228 mod_inst_info.module_id = module_id;
4229 mod_inst_info.instance_id = INSTANCE_ID_0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304230
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004231 return adm_param_enable_v2(port_id, copp_idx, mod_inst_info, enable);
4232}
Vidyakumar Athota98464a22018-03-15 20:39:37 -07004233EXPORT_SYMBOL(adm_param_enable);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004234
4235/**
4236 * adm_param_enable_v2 -
4237 * command to send params to ADM for given module
4238 *
4239 * @port_id: Port ID number
4240 * @copp_idx: copp index assigned
4241 * @mod_inst_info: module and instance ID info
4242 * @enable: flag to enable or disable module
4243 *
4244 * Returns 0 on success or error on failure
4245 */
4246int adm_param_enable_v2(int port_id, int copp_idx,
4247 struct module_instance_info mod_inst_info, int enable)
4248{
4249 uint32_t enable_param;
4250 struct param_hdr_v3 param_hdr;
4251 int rc = 0;
4252
4253 if (enable < 0 || enable > 1) {
4254 pr_err("%s: Invalid value for enable %d\n", __func__, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304255 return -EINVAL;
4256 }
4257
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004258 pr_debug("%s port_id %d, module_id 0x%x, instance_id 0x%x, enable %d\n",
4259 __func__, port_id, mod_inst_info.module_id,
4260 mod_inst_info.instance_id, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304261
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004262 memset(&param_hdr, 0, sizeof(param_hdr));
4263 param_hdr.module_id = mod_inst_info.module_id;
4264 param_hdr.instance_id = mod_inst_info.instance_id;
4265 param_hdr.param_id = AUDPROC_PARAM_ID_ENABLE;
4266 param_hdr.param_size = sizeof(enable_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304267
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004268 enable_param = enable;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304269
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004270 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4271 (uint8_t *) &enable_param);
4272 if (rc)
4273 pr_err("%s: Failed to set enable of module(%d) instance(%d) to %d, err %d\n",
4274 __func__, mod_inst_info.module_id,
4275 mod_inst_info.instance_id, enable, rc);
4276
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304277 return rc;
4278
4279}
Vidyakumar Athota98464a22018-03-15 20:39:37 -07004280EXPORT_SYMBOL(adm_param_enable_v2);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304281
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304282/**
4283 * adm_send_calibration -
4284 * send ADM calibration to DSP
4285 *
4286 * @port_id: Port ID number
4287 * @copp_idx: copp index assigned
4288 * @path: direction or ADM path type
4289 * @perf_mode: performance mode like LL/ULL/..
4290 * @cal_type: calibration type to use
4291 * @params: pointer with cal data
4292 * @size: cal size
4293 *
4294 * Returns 0 on success or error on failure
4295 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304296int adm_send_calibration(int port_id, int copp_idx, int path, int perf_mode,
4297 int cal_type, char *params, int size)
4298{
4299
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004300 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304301
4302 pr_debug("%s:port_id %d, path %d, perf_mode %d, cal_type %d, size %d\n",
4303 __func__, port_id, path, perf_mode, cal_type, size);
4304
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304305 /* Maps audio_dev_ctrl path definition to ACDB definition */
4306 if (get_cal_path(path) != RX_DEVICE) {
4307 pr_err("%s: acdb_path %d\n", __func__, path);
4308 rc = -EINVAL;
4309 goto end;
4310 }
4311
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004312 rc = adm_set_pp_params(port_id, copp_idx, NULL, (u8 *) params, size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304313
4314end:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304315 return rc;
4316}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304317EXPORT_SYMBOL(adm_send_calibration);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304318
4319/*
4320 * adm_update_wait_parameters must be called with routing driver locks.
4321 * adm_reset_wait_parameters must be called with routing driver locks.
4322 * set and reset parmeters are separated to make sure it is always called
4323 * under routing driver lock.
4324 * adm_wait_timeout is to block until timeout or interrupted. Timeout is
4325 * not a an error.
4326 */
4327int adm_set_wait_parameters(int port_id, int copp_idx)
4328{
4329
4330 int ret = 0, port_idx;
4331
4332 pr_debug("%s: port_id 0x%x, copp_idx %d\n", __func__, port_id,
4333 copp_idx);
4334 port_id = afe_convert_virtual_to_portid(port_id);
4335 port_idx = adm_validate_and_get_port_index(port_id);
4336 if (port_idx < 0) {
4337 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4338 ret = -EINVAL;
4339 goto end;
4340 }
4341
4342 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4343 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4344 return -EINVAL;
4345 }
4346
4347 this_adm.copp.adm_delay[port_idx][copp_idx] = 1;
4348 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 0);
4349
4350end:
4351 return ret;
4352
4353}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304354EXPORT_SYMBOL(adm_set_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304355
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304356/**
4357 * adm_reset_wait_parameters -
4358 * reset wait parameters or ADM delay value
4359 *
4360 * @port_id: Port ID number
4361 * @copp_idx: copp index assigned
4362 *
4363 * Returns 0 on success or error on failure
4364 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304365int adm_reset_wait_parameters(int port_id, int copp_idx)
4366{
4367 int ret = 0, port_idx;
4368
4369 pr_debug("%s: port_id 0x%x copp_idx %d\n", __func__, port_id,
4370 copp_idx);
4371 port_id = afe_convert_virtual_to_portid(port_id);
4372 port_idx = adm_validate_and_get_port_index(port_id);
4373 if (port_idx < 0) {
4374 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4375 ret = -EINVAL;
4376 goto end;
4377 }
4378
4379 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4380 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4381 return -EINVAL;
4382 }
4383
4384 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 1);
4385 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
4386
4387end:
4388 return ret;
4389}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304390EXPORT_SYMBOL(adm_reset_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304391
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304392/**
4393 * adm_wait_timeout -
4394 * ADM wait command after command send to DSP
4395 *
4396 * @port_id: Port ID number
4397 * @copp_idx: copp index assigned
4398 * @wait_time: value in ms for command timeout
4399 *
4400 * Returns 0 on success or error on failure
4401 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304402int adm_wait_timeout(int port_id, int copp_idx, int wait_time)
4403{
4404 int ret = 0, port_idx;
4405
4406 pr_debug("%s: port_id 0x%x, copp_idx %d, wait_time %d\n", __func__,
4407 port_id, copp_idx, wait_time);
4408 port_id = afe_convert_virtual_to_portid(port_id);
4409 port_idx = adm_validate_and_get_port_index(port_id);
4410 if (port_idx < 0) {
4411 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4412 ret = -EINVAL;
4413 goto end;
4414 }
4415
4416 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4417 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4418 return -EINVAL;
4419 }
4420
4421 ret = wait_event_timeout(
4422 this_adm.copp.adm_delay_wait[port_idx][copp_idx],
4423 atomic_read(&this_adm.copp.adm_delay_stat[port_idx][copp_idx]),
4424 msecs_to_jiffies(wait_time));
4425 pr_debug("%s: return %d\n", __func__, ret);
4426 if (ret != 0)
4427 ret = -EINTR;
4428end:
4429 pr_debug("%s: return %d--\n", __func__, ret);
4430 return ret;
4431}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304432EXPORT_SYMBOL(adm_wait_timeout);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304433
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304434/**
4435 * adm_store_cal_data -
4436 * Retrieve calibration data for ADM copp device
4437 *
4438 * @port_id: Port ID number
4439 * @copp_idx: copp index assigned
4440 * @path: direction or copp type
4441 * @perf_mode: performance mode like LL/ULL/..
4442 * @cal_index: calibration index to use
4443 * @params: pointer to store cal data
4444 * @size: pointer to fill with cal size
4445 *
4446 * Returns 0 on success or error on failure
4447 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304448int adm_store_cal_data(int port_id, int copp_idx, int path, int perf_mode,
4449 int cal_index, char *params, int *size)
4450{
4451 int rc = 0;
4452 struct cal_block_data *cal_block = NULL;
4453 int app_type, acdb_id, port_idx, sample_rate;
4454
4455 if (this_adm.cal_data[cal_index] == NULL) {
4456 pr_debug("%s: cal_index %d not allocated!\n",
4457 __func__, cal_index);
4458 goto end;
4459 }
4460
4461 if (get_cal_path(path) != RX_DEVICE) {
4462 pr_debug("%s: Invalid path to store calibration %d\n",
4463 __func__, path);
4464 rc = -EINVAL;
4465 goto end;
4466 }
4467
4468 port_id = afe_convert_virtual_to_portid(port_id);
4469 port_idx = adm_validate_and_get_port_index(port_id);
4470 if (port_idx < 0) {
4471 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
4472 rc = -EINVAL;
4473 goto end;
4474 }
4475
4476 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4477 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4478 return -EINVAL;
4479 }
4480
4481 acdb_id = atomic_read(&this_adm.copp.acdb_id[port_idx][copp_idx]);
4482 app_type = atomic_read(&this_adm.copp.app_type[port_idx][copp_idx]);
4483 sample_rate = atomic_read(&this_adm.copp.rate[port_idx][copp_idx]);
4484
4485 mutex_lock(&this_adm.cal_data[cal_index]->lock);
4486 cal_block = adm_find_cal(cal_index, get_cal_path(path), app_type,
4487 acdb_id, sample_rate);
4488 if (cal_block == NULL)
4489 goto unlock;
4490
4491 if (cal_block->cal_data.size <= 0) {
4492 pr_debug("%s: No ADM cal send for port_id = 0x%x!\n",
4493 __func__, port_id);
4494 rc = -EINVAL;
4495 goto unlock;
4496 }
4497
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05304498 if (cal_index == ADM_AUDPROC_CAL || cal_index == ADM_LSM_AUDPROC_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304499 if (cal_block->cal_data.size > AUD_PROC_BLOCK_SIZE) {
4500 pr_err("%s:audproc:invalid size exp/actual[%zd, %d]\n",
4501 __func__, cal_block->cal_data.size, *size);
4502 rc = -ENOMEM;
4503 goto unlock;
4504 }
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07004505 } else if (cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
4506 if (cal_block->cal_data.size > AUD_PROC_PERSIST_BLOCK_SIZE) {
4507 pr_err("%s:persist invalid size exp/actual[%zd, %d]\n",
4508 __func__, cal_block->cal_data.size, *size);
4509 rc = -ENOMEM;
4510 goto unlock;
4511 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304512 } else if (cal_index == ADM_AUDVOL_CAL) {
4513 if (cal_block->cal_data.size > AUD_VOL_BLOCK_SIZE) {
4514 pr_err("%s:aud_vol:invalid size exp/actual[%zd, %d]\n",
4515 __func__, cal_block->cal_data.size, *size);
4516 rc = -ENOMEM;
4517 goto unlock;
4518 }
4519 } else {
4520 pr_debug("%s: Not valid calibration for dolby topolgy\n",
4521 __func__);
4522 rc = -EINVAL;
4523 goto unlock;
4524 }
4525 memcpy(params, cal_block->cal_data.kvaddr, cal_block->cal_data.size);
4526 *size = cal_block->cal_data.size;
4527
4528 pr_debug("%s:port_id %d, copp_idx %d, path %d",
4529 __func__, port_id, copp_idx, path);
4530 pr_debug("perf_mode %d, cal_type %d, size %d\n",
4531 perf_mode, cal_index, *size);
4532
4533unlock:
4534 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
4535end:
4536 return rc;
4537}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304538EXPORT_SYMBOL(adm_store_cal_data);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304539
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304540/**
4541 * adm_send_compressed_device_mute -
4542 * command to send mute for compressed device
4543 *
4544 * @port_id: Port ID number
4545 * @copp_idx: copp index assigned
4546 * @mute_on: flag to indicate mute or unmute
4547 *
4548 * Returns 0 on success or error on failure
4549 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304550int adm_send_compressed_device_mute(int port_id, int copp_idx, bool mute_on)
4551{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004552 u32 mute_param = mute_on ? 1 : 0;
4553 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304554 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304555
4556 pr_debug("%s port_id: 0x%x, copp_idx %d, mute_on: %d\n",
4557 __func__, port_id, copp_idx, mute_on);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304558
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004559 memset(&param_hdr, 0, sizeof(param_hdr));
4560 param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_MUTE;
4561 param_hdr.instance_id = INSTANCE_ID_0;
4562 param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_MUTE;
4563 param_hdr.param_size = sizeof(mute_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304564
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004565 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4566 (uint8_t *) &mute_param);
4567 if (ret)
4568 pr_err("%s: Failed to set mute, err %d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304569
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304570 return ret;
4571}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304572EXPORT_SYMBOL(adm_send_compressed_device_mute);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304573
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304574/**
4575 * adm_send_compressed_device_latency -
4576 * command to send latency for compressed device
4577 *
4578 * @port_id: Port ID number
4579 * @copp_idx: copp index assigned
4580 * @latency: latency value to pass
4581 *
4582 * Returns 0 on success or error on failure
4583 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304584int adm_send_compressed_device_latency(int port_id, int copp_idx, int latency)
4585{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004586 u32 latency_param;
4587 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304588 int ret = 0;
4589
4590 pr_debug("%s port_id: 0x%x, copp_idx %d latency: %d\n", __func__,
4591 port_id, copp_idx, latency);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004592
4593 if (latency < 0) {
4594 pr_err("%s: Invalid value for latency %d", __func__, latency);
4595 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304596 }
4597
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004598 memset(&param_hdr, 0, sizeof(param_hdr));
4599 param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_LATENCY;
4600 param_hdr.instance_id = INSTANCE_ID_0;
4601 param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_LATENCY;
4602 param_hdr.param_size = sizeof(latency_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304603
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004604 latency_param = latency;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304605
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004606 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4607 (uint8_t *) &latency_param);
4608 if (ret)
4609 pr_err("%s: Failed to set latency, err %d\n", __func__, ret);
4610
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304611 return ret;
4612}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304613EXPORT_SYMBOL(adm_send_compressed_device_latency);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304614
4615/**
4616 * adm_swap_speaker_channels
4617 *
4618 * Receives port_id, copp_idx, sample rate, spk_swap and
4619 * send MFC command to swap speaker channel.
4620 * Return zero on success. On failure returns nonzero.
4621 *
4622 * port_id - Passed value, port_id for which channels swap is wanted
4623 * copp_idx - Passed value, copp_idx for which channels swap is wanted
4624 * sample_rate - Passed value, sample rate used by app type config
4625 * spk_swap - Passed value, spk_swap for check if swap flag is set
4626 */
4627int adm_swap_speaker_channels(int port_id, int copp_idx,
4628 int sample_rate, bool spk_swap)
4629{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004630 struct audproc_mfc_param_media_fmt mfc_cfg;
4631 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304632 uint16_t num_channels;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004633 int port_idx = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304634 int ret = 0;
4635
4636 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4637 __func__, port_id, copp_idx);
4638 port_id = q6audio_convert_virtual_to_portid(port_id);
4639 port_idx = adm_validate_and_get_port_index(port_id);
4640 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
4641 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004642 return -EINVAL;
4643 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4644 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
4645 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304646 }
4647
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004648 num_channels = atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304649 if (num_channels != 2) {
4650 pr_debug("%s: Invalid number of channels: %d\n",
4651 __func__, num_channels);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004652 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304653 }
4654
4655 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004656 memset(&param_hdr, 0, sizeof(param_hdr));
4657
4658 param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
4659 param_hdr.instance_id = INSTANCE_ID_0;
4660 param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
4661 param_hdr.param_size = sizeof(mfc_cfg);
4662
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304663 mfc_cfg.sampling_rate = sample_rate;
4664 mfc_cfg.bits_per_sample =
4665 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
4666 mfc_cfg.num_channels = num_channels;
4667
4668 /* Currently applying speaker swap for only 2 channel use case */
4669 if (spk_swap) {
4670 mfc_cfg.channel_type[0] =
4671 (uint16_t) PCM_CHANNEL_FR;
4672 mfc_cfg.channel_type[1] =
4673 (uint16_t) PCM_CHANNEL_FL;
4674 } else {
4675 mfc_cfg.channel_type[0] =
4676 (uint16_t) PCM_CHANNEL_FL;
4677 mfc_cfg.channel_type[1] =
4678 (uint16_t) PCM_CHANNEL_FR;
4679 }
4680
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004681 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4682 (u8 *) &mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304683 if (ret < 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004684 pr_err("%s: Failed to set swap speaker channels on port[0x%x] failed %d\n",
4685 __func__, port_id, ret);
4686 return ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304687 }
4688
4689 pr_debug("%s: mfc_cfg Set params returned success", __func__);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004690 return 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304691}
4692EXPORT_SYMBOL(adm_swap_speaker_channels);
4693
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304694/**
4695 * adm_set_sound_focus -
4696 * Update sound focus info
4697 *
4698 * @port_id: Port ID number
4699 * @copp_idx: copp index assigned
4700 * @soundFocusData: sound focus data to pass
4701 *
4702 * Returns 0 on success or error on failure
4703 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304704int adm_set_sound_focus(int port_id, int copp_idx,
4705 struct sound_focus_param soundFocusData)
4706{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004707 struct adm_param_fluence_soundfocus_t soundfocus_params;
4708 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304709 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304710 int i;
4711
4712 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4713 __func__, port_id, copp_idx);
4714
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004715 memset(&param_hdr, 0, sizeof(param_hdr));
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07004716 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004717 param_hdr.instance_id = INSTANCE_ID_0;
4718 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
4719 param_hdr.param_size = sizeof(soundfocus_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304720
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004721 memset(&(soundfocus_params), 0xFF, sizeof(soundfocus_params));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304722 for (i = 0; i < MAX_SECTORS; i++) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004723 soundfocus_params.start_angles[i] =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304724 soundFocusData.start_angle[i];
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004725 soundfocus_params.enables[i] = soundFocusData.enable[i];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304726 pr_debug("%s: start_angle[%d] = %d\n",
4727 __func__, i, soundFocusData.start_angle[i]);
4728 pr_debug("%s: enable[%d] = %d\n",
4729 __func__, i, soundFocusData.enable[i]);
4730 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004731 soundfocus_params.gain_step = soundFocusData.gain_step;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304732 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData.gain_step);
4733
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004734 soundfocus_params.reserved = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304735
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004736 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4737 (uint8_t *) &soundfocus_params);
4738 if (ret)
4739 pr_err("%s: Failed to set sound focus params, err %d\n",
4740 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304741
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304742 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
4743
4744 return ret;
4745}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304746EXPORT_SYMBOL(adm_set_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304747
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304748/**
4749 * adm_get_sound_focus -
4750 * Retrieve sound focus info
4751 *
4752 * @port_id: Port ID number
4753 * @copp_idx: copp index assigned
4754 * @soundFocusData: pointer for sound focus data to be updated with
4755 *
4756 * Returns 0 on success or error on failure
4757 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304758int adm_get_sound_focus(int port_id, int copp_idx,
4759 struct sound_focus_param *soundFocusData)
4760{
4761 int ret = 0, i;
4762 char *params_value;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004763 uint32_t max_param_size = 0;
4764 struct adm_param_fluence_soundfocus_t *soundfocus_params = NULL;
4765 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304766
4767 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4768 __func__, port_id, copp_idx);
4769
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004770 max_param_size = sizeof(struct adm_param_fluence_soundfocus_t) +
4771 sizeof(union param_hdrs);
4772 params_value = kzalloc(max_param_size, GFP_KERNEL);
4773 if (!params_value)
4774 return -ENOMEM;
4775
4776 memset(&param_hdr, 0, sizeof(param_hdr));
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07004777 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004778 param_hdr.instance_id = INSTANCE_ID_0;
4779 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
4780 param_hdr.param_size = max_param_size;
4781 ret = adm_get_pp_params(port_id, copp_idx,
4782 ADM_CLIENT_ID_SOURCE_TRACKING, NULL, &param_hdr,
4783 params_value);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304784 if (ret) {
4785 pr_err("%s: get parameters failed ret:%d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304786 ret = -EINVAL;
4787 goto done;
4788 }
4789
4790 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
4791 pr_err("%s - get params returned error [%s]\n",
4792 __func__, adsp_err_get_err_str(
4793 this_adm.sourceTrackingData.apr_cmd_status));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304794 ret = adsp_err_get_lnx_err_code(
4795 this_adm.sourceTrackingData.apr_cmd_status);
4796 goto done;
4797 }
4798
4799 soundfocus_params = (struct adm_param_fluence_soundfocus_t *)
4800 params_value;
4801 for (i = 0; i < MAX_SECTORS; i++) {
4802 soundFocusData->start_angle[i] =
4803 soundfocus_params->start_angles[i];
4804 soundFocusData->enable[i] = soundfocus_params->enables[i];
4805 pr_debug("%s: start_angle[%d] = %d\n",
4806 __func__, i, soundFocusData->start_angle[i]);
4807 pr_debug("%s: enable[%d] = %d\n",
4808 __func__, i, soundFocusData->enable[i]);
4809 }
4810 soundFocusData->gain_step = soundfocus_params->gain_step;
4811 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData->gain_step);
4812
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304813done:
4814 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
4815
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004816 kfree(params_value);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304817 return ret;
4818}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304819EXPORT_SYMBOL(adm_get_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304820
4821static int adm_source_tracking_alloc_map_memory(void)
4822{
4823 int ret;
4824
4825 pr_debug("%s: Enter\n", __func__);
4826
Banajit Goswami08bb7362017-11-03 22:48:23 -07004827 ret = msm_audio_ion_alloc(&this_adm.sourceTrackingData.dma_buf,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304828 AUD_PROC_BLOCK_SIZE,
4829 &this_adm.sourceTrackingData.memmap.paddr,
4830 &this_adm.sourceTrackingData.memmap.size,
4831 &this_adm.sourceTrackingData.memmap.kvaddr);
4832 if (ret) {
4833 pr_err("%s: failed to allocate memory\n", __func__);
4834
4835 ret = -EINVAL;
4836 goto done;
4837 }
4838
4839 atomic_set(&this_adm.mem_map_index, ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
4840 ret = adm_memory_map_regions(&this_adm.sourceTrackingData.memmap.paddr,
4841 0,
4842 (uint32_t *)&this_adm.sourceTrackingData.memmap.size,
4843 1);
4844 if (ret < 0) {
4845 pr_err("%s: failed to map memory, paddr = 0x%pK, size = %d\n",
4846 __func__,
4847 (void *)this_adm.sourceTrackingData.memmap.paddr,
4848 (uint32_t)this_adm.sourceTrackingData.memmap.size);
4849
Banajit Goswami08bb7362017-11-03 22:48:23 -07004850 msm_audio_ion_free(this_adm.sourceTrackingData.dma_buf);
4851 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304852 this_adm.sourceTrackingData.memmap.size = 0;
4853 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
4854 this_adm.sourceTrackingData.memmap.paddr = 0;
4855 this_adm.sourceTrackingData.apr_cmd_status = -1;
4856 atomic_set(&this_adm.mem_map_handles
4857 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
4858
4859 ret = -EINVAL;
4860 goto done;
4861 }
4862 ret = 0;
4863 pr_debug("%s: paddr = 0x%pK, size = %d, mem_map_handle = 0x%x\n",
4864 __func__, (void *)this_adm.sourceTrackingData.memmap.paddr,
4865 (uint32_t)this_adm.sourceTrackingData.memmap.size,
4866 atomic_read(&this_adm.mem_map_handles
4867 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING]));
4868
4869done:
4870 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
4871
4872 return ret;
4873}
4874
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304875/**
4876 * adm_get_source_tracking -
4877 * Retrieve source tracking info
4878 *
4879 * @port_id: Port ID number
4880 * @copp_idx: copp index assigned
4881 * @sourceTrackingData: pointer for source track data to be updated with
4882 *
4883 * Returns 0 on success or error on failure
4884 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304885int adm_get_source_tracking(int port_id, int copp_idx,
4886 struct source_tracking_param *sourceTrackingData)
4887{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004888 struct adm_param_fluence_sourcetracking_t *source_tracking_params =
4889 NULL;
4890 struct mem_mapping_hdr mem_hdr;
4891 struct param_hdr_v3 param_hdr;
4892 int i = 0;
4893 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304894
4895 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4896 __func__, port_id, copp_idx);
4897
4898 if (!this_adm.sourceTrackingData.memmap.paddr) {
4899 /* Allocate and map shared memory for out of band usage */
4900 ret = adm_source_tracking_alloc_map_memory();
4901 if (ret != 0) {
4902 ret = -EINVAL;
4903 goto done;
4904 }
4905 }
4906
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004907 memset(&mem_hdr, 0, sizeof(mem_hdr));
4908 memset(&param_hdr, 0, sizeof(param_hdr));
4909 mem_hdr.data_payload_addr_lsw =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304910 lower_32_bits(this_adm.sourceTrackingData.memmap.paddr);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004911 mem_hdr.data_payload_addr_msw = msm_audio_populate_upper_32_bits(
4912 this_adm.sourceTrackingData.memmap.paddr);
4913 mem_hdr.mem_map_handle = atomic_read(
4914 &this_adm.mem_map_handles[ADM_MEM_MAP_INDEX_SOURCE_TRACKING]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304915
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07004916 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004917 param_hdr.instance_id = INSTANCE_ID_0;
4918 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOURCETRACKING;
4919 /*
4920 * This size should be the max size of the calibration data + header.
4921 * Use the union size to ensure max size is used.
4922 */
4923 param_hdr.param_size =
4924 sizeof(struct adm_param_fluence_sourcetracking_t) +
4925 sizeof(union param_hdrs);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304926
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004927 /*
4928 * Retrieving parameters out of band, so no need to provide a buffer for
4929 * the returned parameter data as it will be at the memory location
4930 * provided.
4931 */
4932 ret = adm_get_pp_params(port_id, copp_idx,
4933 ADM_CLIENT_ID_SOURCE_TRACKING, &mem_hdr,
4934 &param_hdr, NULL);
4935 if (ret) {
4936 pr_err("%s: Failed to get params, error %d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304937 goto done;
4938 }
4939
4940 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
4941 pr_err("%s - get params returned error [%s]\n",
4942 __func__, adsp_err_get_err_str(
4943 this_adm.sourceTrackingData.apr_cmd_status));
4944
4945 ret = adsp_err_get_lnx_err_code(
4946 this_adm.sourceTrackingData.apr_cmd_status);
4947 goto done;
4948 }
4949
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004950 /* How do we know what the param data was retrieved with for hdr size */
4951 source_tracking_params =
4952 (struct adm_param_fluence_sourcetracking_t
4953 *) (this_adm.sourceTrackingData.memmap.kvaddr +
4954 sizeof(struct param_hdr_v1));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304955 for (i = 0; i < MAX_SECTORS; i++) {
4956 sourceTrackingData->vad[i] = source_tracking_params->vad[i];
4957 pr_debug("%s: vad[%d] = %d\n",
4958 __func__, i, sourceTrackingData->vad[i]);
4959 }
4960 sourceTrackingData->doa_speech = source_tracking_params->doa_speech;
4961 pr_debug("%s: doa_speech = %d\n",
4962 __func__, sourceTrackingData->doa_speech);
4963
4964 for (i = 0; i < MAX_NOISE_SOURCE_INDICATORS; i++) {
4965 sourceTrackingData->doa_noise[i] =
4966 source_tracking_params->doa_noise[i];
4967 pr_debug("%s: doa_noise[%d] = %d\n",
4968 __func__, i, sourceTrackingData->doa_noise[i]);
4969 }
4970 for (i = 0; i < MAX_POLAR_ACTIVITY_INDICATORS; i++) {
4971 sourceTrackingData->polar_activity[i] =
4972 source_tracking_params->polar_activity[i];
4973 pr_debug("%s: polar_activity[%d] = %d\n",
4974 __func__, i, sourceTrackingData->polar_activity[i]);
4975 }
4976
4977 ret = 0;
4978
4979done:
4980 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
4981
4982 return ret;
4983}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304984EXPORT_SYMBOL(adm_get_source_tracking);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304985
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304986int __init adm_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304987{
4988 int i = 0, j;
4989
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304990 this_adm.ec_ref_rx = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304991 init_waitqueue_head(&this_adm.matrix_map_wait);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304992 init_waitqueue_head(&this_adm.adm_wait);
4993
4994 for (i = 0; i < AFE_MAX_PORTS; i++) {
4995 for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
4996 atomic_set(&this_adm.copp.id[i][j], RESET_COPP_ID);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304997 init_waitqueue_head(&this_adm.copp.wait[i][j]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304998 init_waitqueue_head(
4999 &this_adm.copp.adm_delay_wait[i][j]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305000 }
5001 }
5002
5003 if (adm_init_cal_data())
5004 pr_err("%s: could not init cal data!\n", __func__);
5005
Banajit Goswami08bb7362017-11-03 22:48:23 -07005006 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305007 this_adm.sourceTrackingData.memmap.size = 0;
5008 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
5009 this_adm.sourceTrackingData.memmap.paddr = 0;
5010 this_adm.sourceTrackingData.apr_cmd_status = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305011
5012 return 0;
5013}
5014
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05305015void adm_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305016{
Laxminath Kasam30ad7512017-11-28 12:40:22 +05305017 if (this_adm.apr)
5018 adm_reset_data();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305019 adm_delete_cal_data();
5020}