blob: 29bf67fdc0d2722e37efed440c295f893ae09d07 [file] [log] [blame]
Meng Wang688a8672019-01-29 13:43:33 +08001// SPDX-License-Identifier: GPL-2.0-only
Meng Wang61af6842018-09-10 17:47:55 +08002/*
Surendar Karkade79a062018-12-05 14:18:55 +05303 * Copyright (c) 2012-2019, The Linux Foundation. All rights reserved.
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304 */
5#include <linux/module.h>
6#include <linux/slab.h>
7#include <linux/wait.h>
8#include <linux/sched.h>
9#include <linux/jiffies.h>
10#include <linux/uaccess.h>
11#include <linux/atomic.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053012#include <sound/asound.h>
Laxminath Kasam8b1366a2017-10-05 01:44:16 +053013#include <dsp/msm-dts-srs-tm-config.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053014#include <dsp/apr_audio-v2.h>
15#include <dsp/q6adm-v2.h>
16#include <dsp/q6audio-v2.h>
17#include <dsp/q6afe-v2.h>
Dieter Luecking50c55352018-09-28 14:29:17 +020018#include <dsp/q6core.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053019#include <dsp/audio_cal_utils.h>
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -080020#include <dsp/q6common.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053021#include <ipc/apr.h>
22#include "adsp_err.h"
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053023
24#define TIMEOUT_MS 1000
25
26#define RESET_COPP_ID 99
27#define INVALID_COPP_ID 0xFF
28/* Used for inband payload copy, max size is 4k */
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -080029/* 3 is to account for module, instance & param ID in payload */
30#define ADM_GET_PARAMETER_LENGTH (4096 - APR_HDR_SIZE - 3 * sizeof(uint32_t))
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053031
32#define ULL_SUPPORTED_BITS_PER_SAMPLE 16
33#define ULL_SUPPORTED_SAMPLE_RATE 48000
34
35#ifndef CONFIG_DOLBY_DAP
36#undef DOLBY_ADM_COPP_TOPOLOGY_ID
37#define DOLBY_ADM_COPP_TOPOLOGY_ID 0xFFFFFFFE
38#endif
39
40#ifndef CONFIG_DOLBY_DS2
41#undef DS2_ADM_COPP_TOPOLOGY_ID
42#define DS2_ADM_COPP_TOPOLOGY_ID 0xFFFFFFFF
43#endif
44
45/* ENUM for adm_status */
46enum adm_cal_status {
47 ADM_STATUS_CALIBRATION_REQUIRED = 0,
48 ADM_STATUS_MAX,
49};
50
51struct adm_copp {
52
53 atomic_t id[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
54 atomic_t cnt[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
55 atomic_t topology[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
56 atomic_t mode[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
57 atomic_t stat[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
58 atomic_t rate[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
59 atomic_t bit_width[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
60 atomic_t channels[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
61 atomic_t app_type[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
62 atomic_t acdb_id[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
Surendar Karkae7507512018-07-20 15:46:54 +053063 atomic_t session_type[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053064 wait_queue_head_t wait[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
65 wait_queue_head_t adm_delay_wait[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
66 atomic_t adm_delay_stat[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
67 uint32_t adm_delay[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
68 unsigned long adm_status[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
69};
70
71struct source_tracking_data {
Banajit Goswami08bb7362017-11-03 22:48:23 -070072 struct dma_buf *dma_buf;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053073 struct param_outband memmap;
74 int apr_cmd_status;
75};
76
77struct adm_ctl {
78 void *apr;
79
80 struct adm_copp copp;
81
82 atomic_t matrix_map_stat;
83 wait_queue_head_t matrix_map_wait;
84
85 atomic_t adm_stat;
86 wait_queue_head_t adm_wait;
87
88 struct cal_type_data *cal_data[ADM_MAX_CAL_TYPES];
89
90 atomic_t mem_map_handles[ADM_MEM_MAP_INDEX_MAX];
91 atomic_t mem_map_index;
92
93 struct param_outband outband_memmap;
94 struct source_tracking_data sourceTrackingData;
95
96 int set_custom_topology;
97 int ec_ref_rx;
98 int num_ec_ref_rx_chans;
99 int ec_ref_rx_bit_width;
100 int ec_ref_rx_sampling_rate;
Dhananjay Kumar43034392019-01-16 21:57:58 +0530101 int num_ec_ref_rx_chans_downmixed;
102 uint16_t ec_ref_chmixer_weights[PCM_FORMAT_MAX_NUM_CHANNEL_V8]
103 [PCM_FORMAT_MAX_NUM_CHANNEL_V8];
Xiaojun Sangdc9e8c22019-05-13 14:46:15 +0800104 int ffecns_port_id;
Dieter Luecking50c55352018-09-28 14:29:17 +0200105 int native_mode;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530106};
107
108static struct adm_ctl this_adm;
109
110struct adm_multi_ch_map {
111 bool set_channel_map;
Dieter Luecking50c55352018-09-28 14:29:17 +0200112 char channel_mapping[PCM_FORMAT_MAX_NUM_CHANNEL_V8];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530113};
114
115#define ADM_MCH_MAP_IDX_PLAYBACK 0
116#define ADM_MCH_MAP_IDX_REC 1
117static struct adm_multi_ch_map multi_ch_maps[2] = {
Dieter Luecking50c55352018-09-28 14:29:17 +0200118 { false,
119 {0, 0, 0, 0, 0, 0, 0, 0,
120 0, 0, 0, 0, 0, 0, 0, 0,
121 0, 0, 0, 0, 0, 0, 0, 0,
122 0, 0, 0, 0, 0, 0, 0, 0}
123 },
124 { false,
125 {0, 0, 0, 0, 0, 0, 0, 0,
126 0, 0, 0, 0, 0, 0, 0, 0,
127 0, 0, 0, 0, 0, 0, 0, 0,
128 0, 0, 0, 0, 0, 0, 0, 0}
129 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530130};
131
Rohit kumar2054b692019-02-01 18:01:12 +0530132static struct adm_multi_ch_map port_channel_map[AFE_MAX_PORTS];
133
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530134static int adm_get_parameters[MAX_COPPS_PER_PORT * ADM_GET_PARAMETER_LENGTH];
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800135static int adm_module_topo_list[MAX_COPPS_PER_PORT *
136 ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH];
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530137static struct mutex dts_srs_lock;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530138
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530139void msm_dts_srs_acquire_lock(void)
140{
141 mutex_lock(&dts_srs_lock);
142}
143
144void msm_dts_srs_release_lock(void)
145{
146 mutex_unlock(&dts_srs_lock);
147}
148
Dhananjay Kumardefd81b2018-12-11 18:05:59 +0530149static int adm_arrange_mch_map_v8(
150 struct adm_device_endpoint_payload *ep_payload,
151 int path,
Rohit kumar2054b692019-02-01 18:01:12 +0530152 int channel_mode,
153 int port_idx);
Dhananjay Kumardefd81b2018-12-11 18:05:59 +0530154
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530155/**
156 * adm_validate_and_get_port_index -
157 * validate given port id
158 *
159 * @port_id: Port ID number
160 *
161 * Returns valid index on success or error on failure
162 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530163int adm_validate_and_get_port_index(int port_id)
164{
165 int index;
166 int ret;
167
168 ret = q6audio_validate_port(port_id);
169 if (ret < 0) {
170 pr_err("%s: port validation failed id 0x%x ret %d\n",
171 __func__, port_id, ret);
172 return -EINVAL;
173 }
174
175 index = afe_get_port_index(port_id);
176 if (index < 0 || index >= AFE_MAX_PORTS) {
177 pr_err("%s: Invalid port idx %d port_id 0x%x\n",
178 __func__, index,
179 port_id);
180 return -EINVAL;
181 }
182 pr_debug("%s: port_idx- %d\n", __func__, index);
183 return index;
184}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530185EXPORT_SYMBOL(adm_validate_and_get_port_index);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530186
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530187/**
188 * adm_get_default_copp_idx -
189 * retrieve default copp_idx for given port
190 *
191 * @port_id: Port ID number
192 *
193 * Returns valid value on success or error on failure
194 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530195int adm_get_default_copp_idx(int port_id)
196{
197 int port_idx = adm_validate_and_get_port_index(port_id), idx;
198
199 if (port_idx < 0) {
200 pr_err("%s: Invalid port id: 0x%x", __func__, port_id);
201 return -EINVAL;
202 }
203 pr_debug("%s: port_idx:%d\n", __func__, port_idx);
204 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++) {
205 if (atomic_read(&this_adm.copp.id[port_idx][idx]) !=
206 RESET_COPP_ID)
207 return idx;
208 }
209 return -EINVAL;
210}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530211EXPORT_SYMBOL(adm_get_default_copp_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530212
213int adm_get_topology_for_port_from_copp_id(int port_id, int copp_id)
214{
215 int port_idx = adm_validate_and_get_port_index(port_id), idx;
216
217 if (port_idx < 0) {
218 pr_err("%s: Invalid port id: 0x%x", __func__, port_id);
219 return 0;
220 }
221 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++)
222 if (atomic_read(&this_adm.copp.id[port_idx][idx]) == copp_id)
223 return atomic_read(&this_adm.copp.topology[port_idx]
224 [idx]);
225 pr_err("%s: Invalid copp_id %d port_id 0x%x\n",
226 __func__, copp_id, port_id);
227 return 0;
228}
229
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530230/**
231 * adm_get_topology_for_port_copp_idx -
232 * retrieve topology of given port/copp_idx
233 *
234 * @port_id: Port ID number
235 * @copp_idx: copp index of ADM copp
236 *
237 * Returns valid value on success or 0 on failure
238 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530239int adm_get_topology_for_port_copp_idx(int port_id, int copp_idx)
240{
241 int port_idx = adm_validate_and_get_port_index(port_id);
242
243 if (port_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
244 pr_err("%s: Invalid port: 0x%x copp id: 0x%x",
245 __func__, port_id, copp_idx);
246 return 0;
247 }
248 return atomic_read(&this_adm.copp.topology[port_idx][copp_idx]);
249}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530250EXPORT_SYMBOL(adm_get_topology_for_port_copp_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530251
252int adm_get_indexes_from_copp_id(int copp_id, int *copp_idx, int *port_idx)
253{
254 int p_idx, c_idx;
255
256 for (p_idx = 0; p_idx < AFE_MAX_PORTS; p_idx++) {
257 for (c_idx = 0; c_idx < MAX_COPPS_PER_PORT; c_idx++) {
258 if (atomic_read(&this_adm.copp.id[p_idx][c_idx])
259 == copp_id) {
260 if (copp_idx != NULL)
261 *copp_idx = c_idx;
262 if (port_idx != NULL)
263 *port_idx = p_idx;
264 return 0;
265 }
266 }
267 }
268 return -EINVAL;
269}
270
271static int adm_get_copp_id(int port_idx, int copp_idx)
272{
273 pr_debug("%s: port_idx:%d copp_idx:%d\n", __func__, port_idx, copp_idx);
274
275 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
276 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
277 return -EINVAL;
278 }
279 return atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
280}
281
282static int adm_get_idx_if_copp_exists(int port_idx, int topology, int mode,
Surendar Karkae7507512018-07-20 15:46:54 +0530283 int rate, int bit_width, int app_type,
284 int session_type)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530285{
286 int idx;
287
288 pr_debug("%s: port_idx-%d, topology-0x%x, mode-%d, rate-%d, bit_width-%d\n",
289 __func__, port_idx, topology, mode, rate, bit_width);
290
291 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++)
292 if ((topology ==
293 atomic_read(&this_adm.copp.topology[port_idx][idx])) &&
294 (mode == atomic_read(&this_adm.copp.mode[port_idx][idx])) &&
295 (rate == atomic_read(&this_adm.copp.rate[port_idx][idx])) &&
296 (bit_width ==
297 atomic_read(&this_adm.copp.bit_width[port_idx][idx])) &&
Surendar Karkae7507512018-07-20 15:46:54 +0530298 (session_type ==
299 atomic_read(
300 &this_adm.copp.session_type[port_idx][idx])) &&
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530301 (app_type ==
302 atomic_read(&this_adm.copp.app_type[port_idx][idx])))
303 return idx;
304 return -EINVAL;
305}
306
307static int adm_get_next_available_copp(int port_idx)
308{
309 int idx;
310
311 pr_debug("%s:\n", __func__);
312 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++) {
313 pr_debug("%s: copp_id:0x%x port_idx:%d idx:%d\n", __func__,
314 atomic_read(&this_adm.copp.id[port_idx][idx]),
315 port_idx, idx);
316 if (atomic_read(&this_adm.copp.id[port_idx][idx]) ==
317 RESET_COPP_ID)
318 break;
319 }
320 return idx;
321}
322
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530323/**
324 * srs_trumedia_open -
325 * command to set SRS trumedia open
326 *
327 * @port_id: Port ID number
328 * @copp_idx: copp index of ADM copp
329 * @srs_tech_id: SRS tech index
330 * @srs_params: params pointer
331 *
332 * Returns 0 on success or error on failure
333 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530334int srs_trumedia_open(int port_id, int copp_idx, __s32 srs_tech_id,
335 void *srs_params)
336{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800337 struct param_hdr_v3 param_hdr;
338 struct mem_mapping_hdr mem_hdr;
339 u32 total_param_size = 0;
340 bool outband = false;
341 int port_idx;
342 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530343
344 pr_debug("SRS - %s", __func__);
345
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800346 memset(&param_hdr, 0, sizeof(param_hdr));
347 memset(&mem_hdr, 0, sizeof(mem_hdr));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530348 port_id = afe_convert_virtual_to_portid(port_id);
349 port_idx = adm_validate_and_get_port_index(port_id);
350 if (port_idx < 0) {
351 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
352 return -EINVAL;
353 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800354
355 param_hdr.module_id = SRS_TRUMEDIA_MODULE_ID;
356 param_hdr.instance_id = INSTANCE_ID_0;
357
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530358 switch (srs_tech_id) {
359 case SRS_ID_GLOBAL: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800360 param_hdr.param_id = SRS_TRUMEDIA_PARAMS;
361 param_hdr.param_size =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530362 sizeof(struct srs_trumedia_params_GLOBAL);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530363 break;
364 }
365 case SRS_ID_WOWHD: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800366 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_WOWHD;
367 param_hdr.param_size = sizeof(struct srs_trumedia_params_WOWHD);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530368 break;
369 }
370 case SRS_ID_CSHP: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800371 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_CSHP;
372 param_hdr.param_size = sizeof(struct srs_trumedia_params_CSHP);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530373 break;
374 }
375 case SRS_ID_HPF: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800376 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_HPF;
377 param_hdr.param_size = sizeof(struct srs_trumedia_params_HPF);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530378 break;
379 }
380 case SRS_ID_AEQ: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800381 u8 *update_params_ptr = (u8 *) this_adm.outband_memmap.kvaddr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530382
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800383 outband = true;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530384
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530385 if (update_params_ptr == NULL) {
386 pr_err("ADM_SRS_TRUMEDIA - %s: null memmap for AEQ params\n",
387 __func__);
388 ret = -EINVAL;
389 goto fail_cmd;
390 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530391
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800392 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_AEQ;
393 param_hdr.param_size = sizeof(struct srs_trumedia_params_AEQ);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530394
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800395 ret = q6common_pack_pp_params(update_params_ptr, &param_hdr,
396 srs_params, &total_param_size);
397 if (ret) {
398 pr_err("%s: Failed to pack param header and data, error %d\n",
399 __func__, ret);
400 goto fail_cmd;
401 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530402 break;
403 }
404 case SRS_ID_HL: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800405 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_HL;
406 param_hdr.param_size = sizeof(struct srs_trumedia_params_HL);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530407 break;
408 }
409 case SRS_ID_GEQ: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800410 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_GEQ;
411 param_hdr.param_size = sizeof(struct srs_trumedia_params_GEQ);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530412 break;
413 }
414 default:
415 goto fail_cmd;
416 }
417
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530418 if (outband && this_adm.outband_memmap.paddr) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800419 mem_hdr.data_payload_addr_lsw =
420 lower_32_bits(this_adm.outband_memmap.paddr);
421 mem_hdr.data_payload_addr_msw =
422 msm_audio_populate_upper_32_bits(
423 this_adm.outband_memmap.paddr);
424 mem_hdr.mem_map_handle = atomic_read(
425 &this_adm.mem_map_handles[ADM_SRS_TRUMEDIA]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530426
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800427 ret = adm_set_pp_params(port_id, copp_idx, &mem_hdr, NULL,
428 total_param_size);
429 } else {
430 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx,
431 param_hdr,
432 (u8 *) srs_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530433 }
434
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800435 if (ret < 0)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530436 pr_err("SRS - %s: ADM enable for port %d failed\n", __func__,
437 port_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530438
439fail_cmd:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530440 return ret;
441}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530442EXPORT_SYMBOL(srs_trumedia_open);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530443
444static int adm_populate_channel_weight(u16 *ptr,
445 struct msm_pcm_channel_mixer *ch_mixer,
446 int channel_index)
447{
448 u16 i, j, start_index = 0;
449
450 if (channel_index > ch_mixer->output_channel) {
451 pr_err("%s: channel index %d is larger than output_channel %d\n",
452 __func__, channel_index, ch_mixer->output_channel);
453 return -EINVAL;
454 }
455
456 for (i = 0; i < ch_mixer->output_channel; i++) {
457 pr_debug("%s: weight for output %d:", __func__, i);
458 for (j = 0; j < ADM_MAX_CHANNELS; j++)
459 pr_debug(" %d",
460 ch_mixer->channel_weight[i][j]);
461 pr_debug("\n");
462 }
463
464 for (i = 0; i < channel_index; ++i)
465 start_index += ch_mixer->input_channels[i];
466
467 for (i = 0; i < ch_mixer->output_channel; ++i) {
468 for (j = start_index;
469 j < start_index +
470 ch_mixer->input_channels[channel_index]; j++) {
471 *ptr = ch_mixer->channel_weight[i][j];
472 pr_debug("%s: ptr[%d][%d] = %d\n",
473 __func__, i, j, *ptr);
474 ptr++;
475 }
476 }
477
478 return 0;
479}
480
481/*
482 * adm_programable_channel_mixer
483 *
484 * Receives port_id, copp_idx, session_id, session_type, ch_mixer
485 * and channel_index to send ADM command to mix COPP data.
486 *
487 * port_id - Passed value, port_id for which backend is wanted
488 * copp_idx - Passed value, copp_idx for which COPP is wanted
489 * session_id - Passed value, session_id for which session is needed
490 * session_type - Passed value, session_type for RX or TX
491 * ch_mixer - Passed value, ch_mixer for which channel mixer config is needed
492 * channel_index - Passed value, channel_index for which channel is needed
493 */
494int adm_programable_channel_mixer(int port_id, int copp_idx, int session_id,
495 int session_type,
496 struct msm_pcm_channel_mixer *ch_mixer,
497 int channel_index)
498{
499 struct adm_cmd_set_pspd_mtmx_strtr_params_v5 *adm_params = NULL;
Vignesh Kulothungan2c6dccd2018-03-22 14:18:43 -0700500 struct param_hdr_v1 data_v5;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530501 int ret = 0, port_idx, sz = 0, param_size = 0;
Dhananjay Kumardefd81b2018-12-11 18:05:59 +0530502 struct adm_device_endpoint_payload ep_params = {0, 0, 0, {0}};
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530503 u16 *adm_pspd_params;
504 u16 *ptr;
Dhananjay Kumardefd81b2018-12-11 18:05:59 +0530505 int index = 0, i = 0, path_type = ADM_PATH_PLAYBACK;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530506
507 pr_debug("%s: port_id = %d\n", __func__, port_id);
508 port_id = afe_convert_virtual_to_portid(port_id);
509 port_idx = adm_validate_and_get_port_index(port_id);
510 if (port_idx < 0) {
511 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
512 return -EINVAL;
513 }
514 /*
515 * First 8 bytes are 4 bytes as rule number, 2 bytes as output
516 * channel and 2 bytes as input channel.
517 * 2 * ch_mixer->output_channel means output channel mapping.
518 * 2 * ch_mixer->input_channels[channel_index]) means input
519 * channel mapping.
520 * 2 * ch_mixer->input_channels[channel_index] *
521 * ch_mixer->output_channel) means the channel mixer weighting
522 * coefficients.
523 * param_size needs to be a multiple of 4 bytes.
524 */
525
526 param_size = 2 * (4 + ch_mixer->output_channel +
527 ch_mixer->input_channels[channel_index] +
528 ch_mixer->input_channels[channel_index] *
529 ch_mixer->output_channel);
Dhananjay Kumardefd81b2018-12-11 18:05:59 +0530530 param_size = roundup(param_size, 4);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530531
532 sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5) +
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800533 sizeof(struct default_chmixer_param_id_coeff) +
Vignesh Kulothungan2c6dccd2018-03-22 14:18:43 -0700534 sizeof(struct param_hdr_v1) + param_size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530535 pr_debug("%s: sz = %d\n", __func__, sz);
536 adm_params = kzalloc(sz, GFP_KERNEL);
537 if (!adm_params)
538 return -ENOMEM;
539
540 adm_params->payload_addr_lsw = 0;
541 adm_params->payload_addr_msw = 0;
542 adm_params->mem_map_handle = 0;
543 adm_params->direction = session_type;
544 adm_params->sessionid = session_id;
545 pr_debug("%s: copp_id = %d, session id %d\n", __func__,
546 atomic_read(&this_adm.copp.id[port_idx][copp_idx]),
547 session_id);
548 adm_params->deviceid = atomic_read(
549 &this_adm.copp.id[port_idx][copp_idx]);
550 adm_params->reserved = 0;
551
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800552 /*
553 * This module is internal to ADSP and cannot be configured with
554 * an instance id
555 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530556 data_v5.module_id = MTMX_MODULE_ID_DEFAULT_CHMIXER;
557 data_v5.param_id = DEFAULT_CHMIXER_PARAM_ID_COEFF;
558 data_v5.reserved = 0;
559 data_v5.param_size = param_size;
560 adm_params->payload_size =
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800561 sizeof(struct default_chmixer_param_id_coeff) +
Vignesh Kulothungan2c6dccd2018-03-22 14:18:43 -0700562 sizeof(struct param_hdr_v1) + data_v5.param_size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530563 adm_pspd_params = (u16 *)((u8 *)adm_params +
564 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5));
565 memcpy(adm_pspd_params, &data_v5, sizeof(data_v5));
566
567 adm_pspd_params = (u16 *)((u8 *)adm_params +
568 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5)
569 + sizeof(data_v5));
570
571 adm_pspd_params[0] = ch_mixer->rule;
572 adm_pspd_params[2] = ch_mixer->output_channel;
573 adm_pspd_params[3] = ch_mixer->input_channels[channel_index];
574 index = 4;
575
Dhananjay Kumardefd81b2018-12-11 18:05:59 +0530576 path_type = (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_RX) ?
577 ADM_PATH_PLAYBACK : ADM_PATH_LIVE_REC;
578
579 if (ch_mixer->override_out_ch_map) {
580 memcpy(&adm_pspd_params[index], &ch_mixer->out_ch_map,
581 ch_mixer->output_channel * sizeof(uint16_t));
582 index += ch_mixer->output_channel;
583 } else {
584 ep_params.dev_num_channel = ch_mixer->output_channel;
Rohit kumar2054b692019-02-01 18:01:12 +0530585 adm_arrange_mch_map_v8(&ep_params, path_type,
586 ep_params.dev_num_channel, port_idx);
Dhananjay Kumardefd81b2018-12-11 18:05:59 +0530587 for (i = 0; i < ch_mixer->output_channel; i++)
588 adm_pspd_params[index++] = ep_params.dev_channel_mapping[i];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530589 }
590
Dhananjay Kumardefd81b2018-12-11 18:05:59 +0530591 if (ch_mixer->override_in_ch_map) {
592 memcpy(&adm_pspd_params[index], &ch_mixer->in_ch_map,
593 ch_mixer->input_channel * sizeof(uint16_t));
594 index += ch_mixer->input_channel;
595 } else {
596 ep_params.dev_num_channel = ch_mixer->input_channels[channel_index];
Rohit kumar2054b692019-02-01 18:01:12 +0530597 adm_arrange_mch_map_v8(&ep_params, path_type,
598 ep_params.dev_num_channel, port_idx);
Dhananjay Kumardefd81b2018-12-11 18:05:59 +0530599 for (i = 0; i < ch_mixer->input_channels[channel_index]; i++)
600 adm_pspd_params[index++] = ep_params.dev_channel_mapping[i];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530601 }
602
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530603 ret = adm_populate_channel_weight(&adm_pspd_params[index],
604 ch_mixer, channel_index);
Meng Wangd9fa69c2018-01-23 12:42:52 +0800605 if (ret) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530606 pr_err("%s: fail to get channel weight with error %d\n",
607 __func__, ret);
608 goto fail_cmd;
609 }
610
611 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
612 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
613 adm_params->hdr.src_svc = APR_SVC_ADM;
614 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
615 adm_params->hdr.src_port = port_id;
616 adm_params->hdr.dest_svc = APR_SVC_ADM;
617 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
618 adm_params->hdr.dest_port =
619 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
620 adm_params->hdr.token = port_idx << 16 | copp_idx;
621 adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5;
622 adm_params->hdr.pkt_size = sz;
623 adm_params->payload_addr_lsw = 0;
624 adm_params->payload_addr_msw = 0;
625 adm_params->mem_map_handle = 0;
626 adm_params->reserved = 0;
627
628 ptr = (u16 *)adm_params;
629 for (index = 0; index < (sz / 2); index++)
630 pr_debug("%s: adm_params[%d] = 0x%x\n",
631 __func__, index, (unsigned int)ptr[index]);
632
633 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], 0);
634 ret = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
635 if (ret < 0) {
636 pr_err("%s: Set params failed port %d rc %d\n", __func__,
637 port_id, ret);
638 ret = -EINVAL;
639 goto fail_cmd;
640 }
641
642 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
643 atomic_read(
644 &this_adm.copp.stat[port_idx][copp_idx]) >= 0,
645 msecs_to_jiffies(TIMEOUT_MS));
646 if (!ret) {
647 pr_err("%s: set params timed out port = %d\n",
648 __func__, port_id);
649 ret = -ETIMEDOUT;
650 goto fail_cmd;
651 }
652 ret = 0;
653fail_cmd:
654 kfree(adm_params);
655
656 return ret;
657}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530658EXPORT_SYMBOL(adm_programable_channel_mixer);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530659
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530660/**
661 * adm_set_stereo_to_custom_stereo -
662 * command to update custom stereo
663 *
664 * @port_id: Port ID number
665 * @copp_idx: copp index of ADM copp
666 * @session_id: session id to be updated
667 * @params: params pointer
668 * @param_length: length of params
669 *
670 * Returns 0 on success or error on failure
671 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530672int adm_set_stereo_to_custom_stereo(int port_id, int copp_idx,
673 unsigned int session_id, char *params,
674 uint32_t params_length)
675{
676 struct adm_cmd_set_pspd_mtmx_strtr_params_v5 *adm_params = NULL;
677 int sz, rc = 0, port_idx;
678
679 pr_debug("%s:\n", __func__);
680 port_id = afe_convert_virtual_to_portid(port_id);
681 port_idx = adm_validate_and_get_port_index(port_id);
682 if (port_idx < 0) {
683 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
684 return -EINVAL;
685 }
686
687 sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5) +
688 params_length;
689 adm_params = kzalloc(sz, GFP_KERNEL);
690 if (!adm_params) {
691 pr_err("%s, adm params memory alloc failed\n", __func__);
692 return -ENOMEM;
693 }
694
695 memcpy(((u8 *)adm_params +
696 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5)),
697 params, params_length);
698 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
699 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
700 adm_params->hdr.pkt_size = sz;
701 adm_params->hdr.src_svc = APR_SVC_ADM;
702 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
703 adm_params->hdr.src_port = port_id;
704 adm_params->hdr.dest_svc = APR_SVC_ADM;
705 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
706 adm_params->hdr.dest_port = 0; /* Ignored */;
707 adm_params->hdr.token = port_idx << 16 | copp_idx;
708 adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5;
709 adm_params->payload_addr_lsw = 0;
710 adm_params->payload_addr_msw = 0;
711 adm_params->mem_map_handle = 0;
712 adm_params->payload_size = params_length;
713 /* direction RX as 0 */
714 adm_params->direction = ADM_MATRIX_ID_AUDIO_RX;
715 /* session id for this cmd to be applied on */
716 adm_params->sessionid = session_id;
717 adm_params->deviceid =
718 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
719 adm_params->reserved = 0;
720 pr_debug("%s: deviceid %d, session_id %d, src_port %d, dest_port %d\n",
721 __func__, adm_params->deviceid, adm_params->sessionid,
722 adm_params->hdr.src_port, adm_params->hdr.dest_port);
723 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
724 rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
725 if (rc < 0) {
726 pr_err("%s: Set params failed port = 0x%x rc %d\n",
727 __func__, port_id, rc);
728 rc = -EINVAL;
729 goto set_stereo_to_custom_stereo_return;
730 }
731 /* Wait for the callback */
732 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
733 atomic_read(&this_adm.copp.stat
734 [port_idx][copp_idx]) >= 0,
735 msecs_to_jiffies(TIMEOUT_MS));
736 if (!rc) {
737 pr_err("%s: Set params timed out port = 0x%x\n", __func__,
738 port_id);
739 rc = -EINVAL;
740 goto set_stereo_to_custom_stereo_return;
741 } else if (atomic_read(&this_adm.copp.stat
742 [port_idx][copp_idx]) > 0) {
743 pr_err("%s: DSP returned error[%s]\n", __func__,
744 adsp_err_get_err_str(atomic_read(
745 &this_adm.copp.stat
746 [port_idx][copp_idx])));
747 rc = adsp_err_get_lnx_err_code(
748 atomic_read(&this_adm.copp.stat
749 [port_idx][copp_idx]));
750 goto set_stereo_to_custom_stereo_return;
751 }
752 rc = 0;
753set_stereo_to_custom_stereo_return:
754 kfree(adm_params);
755 return rc;
756}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530757EXPORT_SYMBOL(adm_set_stereo_to_custom_stereo);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530758
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800759/*
Bhalchandra Gajareeed46bd2018-05-15 16:48:07 -0700760 * adm_set_custom_chmix_cfg:
761 * Set the custom channel mixer configuration for ADM
762 *
763 * @port_id: Backend port id
764 * @copp_idx: ADM copp index
765 * @session_id: ID of the requesting session
766 * @params: Expected packaged params for channel mixer
767 * @params_length: Length of the params to be set
768 * @direction: RX or TX direction
769 * @stream_type: Audio or Listen stream type
770 */
771int adm_set_custom_chmix_cfg(int port_id, int copp_idx,
772 unsigned int session_id, char *params,
773 uint32_t params_length, int direction,
774 int stream_type)
775{
776 struct adm_cmd_set_pspd_mtmx_strtr_params_v6 *adm_params = NULL;
777 int sz, rc = 0, port_idx;
778
779 port_id = afe_convert_virtual_to_portid(port_id);
780 port_idx = adm_validate_and_get_port_index(port_id);
781 if (port_idx < 0) {
782 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
783 return -EINVAL;
784 }
785
786 sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v6) +
787 params_length;
788 adm_params = kzalloc(sz, GFP_KERNEL);
789 if (!adm_params) {
790 pr_err("%s, adm params memory alloc failed\n", __func__);
791 return -ENOMEM;
792 }
793
794 memcpy(((u8 *)adm_params +
795 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v6)),
796 params, params_length);
797 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
798 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
799 adm_params->hdr.pkt_size = sz;
800 adm_params->hdr.src_svc = APR_SVC_ADM;
801 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
802 adm_params->hdr.src_port = port_id;
803 adm_params->hdr.dest_svc = APR_SVC_ADM;
804 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
805 adm_params->hdr.dest_port = 0; /* Ignored */;
806 adm_params->hdr.token = port_idx << 16 | copp_idx;
807 adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V6;
808 adm_params->payload_addr_lsw = 0;
809 adm_params->payload_addr_msw = 0;
810 adm_params->mem_map_handle = 0;
811 adm_params->payload_size = params_length;
812 adm_params->direction = direction;
813 /* session id for this cmd to be applied on */
814 adm_params->sessionid = session_id;
815 adm_params->deviceid =
816 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
817 /* connecting stream type i.e. lsm or asm */
818 adm_params->stream_type = stream_type;
819 pr_debug("%s: deviceid %d, session_id %d, src_port %d, dest_port %d\n",
820 __func__, adm_params->deviceid, adm_params->sessionid,
821 adm_params->hdr.src_port, adm_params->hdr.dest_port);
822 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
823 rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
824 if (rc < 0) {
825 pr_err("%s: Set params failed port = 0x%x rc %d\n",
826 __func__, port_id, rc);
827 rc = -EINVAL;
828 goto exit;
829 }
830 /* Wait for the callback */
831 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
832 atomic_read(&this_adm.copp.stat
833 [port_idx][copp_idx]),
834 msecs_to_jiffies(TIMEOUT_MS));
835 if (!rc) {
836 pr_err("%s: Set params timed out port = 0x%x\n", __func__,
837 port_id);
838 rc = -EINVAL;
839 goto exit;
840 } else if (atomic_read(&this_adm.copp.stat
841 [port_idx][copp_idx]) > 0) {
842 pr_err("%s: DSP returned error[%s]\n", __func__,
843 adsp_err_get_err_str(atomic_read(
844 &this_adm.copp.stat
845 [port_idx][copp_idx])));
846 rc = adsp_err_get_lnx_err_code(
847 atomic_read(&this_adm.copp.stat
848 [port_idx][copp_idx]));
849 goto exit;
850 }
851
852 rc = 0;
853exit:
854 kfree(adm_params);
855 return rc;
856}
857EXPORT_SYMBOL(adm_set_custom_chmix_cfg);
858
859/*
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800860 * With pre-packed data, only the opcode differes from V5 and V6.
861 * Use q6common_pack_pp_params to pack the data correctly.
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530862 */
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800863int adm_set_pp_params(int port_id, int copp_idx,
864 struct mem_mapping_hdr *mem_hdr, u8 *param_data,
865 u32 param_size)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530866{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800867 struct adm_cmd_set_pp_params *adm_set_params = NULL;
868 int size = 0;
869 int port_idx = 0;
870 atomic_t *copp_stat = NULL;
871 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530872
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530873 port_id = afe_convert_virtual_to_portid(port_id);
874 port_idx = adm_validate_and_get_port_index(port_id);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800875 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
876 pr_err("%s: Invalid port_idx 0x%x\n", __func__, port_idx);
877 return -EINVAL;
878 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
879 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530880 return -EINVAL;
881 }
882
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800883 /* Only add params_size in inband case */
884 size = sizeof(struct adm_cmd_set_pp_params);
885 if (param_data != NULL)
886 size += param_size;
887 adm_set_params = kzalloc(size, GFP_KERNEL);
888 if (!adm_set_params)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530889 return -ENOMEM;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800890
891 adm_set_params->apr_hdr.hdr_field =
892 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
893 APR_PKT_VER);
894 adm_set_params->apr_hdr.pkt_size = size;
895 adm_set_params->apr_hdr.src_svc = APR_SVC_ADM;
896 adm_set_params->apr_hdr.src_domain = APR_DOMAIN_APPS;
897 adm_set_params->apr_hdr.src_port = port_id;
898 adm_set_params->apr_hdr.dest_svc = APR_SVC_ADM;
899 adm_set_params->apr_hdr.dest_domain = APR_DOMAIN_ADSP;
900 adm_set_params->apr_hdr.dest_port =
901 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
902 adm_set_params->apr_hdr.token = port_idx << 16 | copp_idx;
903
904 if (q6common_is_instance_id_supported())
905 adm_set_params->apr_hdr.opcode = ADM_CMD_SET_PP_PARAMS_V6;
906 else
907 adm_set_params->apr_hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
908
909 adm_set_params->payload_size = param_size;
910
911 if (mem_hdr != NULL) {
912 /* Out of Band Case */
913 adm_set_params->mem_hdr = *mem_hdr;
914 } else if (param_data != NULL) {
915 /*
916 * In band case. Parameter data must be pre-packed with its
917 * header before calling this function. Use
918 * q6common_pack_pp_params to pack parameter data and header
919 * correctly.
920 */
921 memcpy(&adm_set_params->param_data, param_data, param_size);
922 } else {
923 pr_err("%s: Received NULL pointers for both memory header and param data\n",
924 __func__);
925 ret = -EINVAL;
926 goto done;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530927 }
928
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800929 copp_stat = &this_adm.copp.stat[port_idx][copp_idx];
930 atomic_set(copp_stat, -1);
931 ret = apr_send_pkt(this_adm.apr, (uint32_t *) adm_set_params);
932 if (ret < 0) {
933 pr_err("%s: Set params APR send failed port = 0x%x ret %d\n",
934 __func__, port_id, ret);
935 goto done;
936 }
937 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
938 atomic_read(copp_stat) >= 0,
939 msecs_to_jiffies(TIMEOUT_MS));
940 if (!ret) {
941 pr_err("%s: Set params timed out port = 0x%x\n", __func__,
942 port_id);
943 ret = -ETIMEDOUT;
944 goto done;
945 }
946 if (atomic_read(copp_stat) > 0) {
947 pr_err("%s: DSP returned error[%s]\n", __func__,
948 adsp_err_get_err_str(atomic_read(copp_stat)));
949 ret = adsp_err_get_lnx_err_code(atomic_read(copp_stat));
950 goto done;
951 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530952
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800953 ret = 0;
954done:
955 kfree(adm_set_params);
956 return ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530957}
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800958EXPORT_SYMBOL(adm_set_pp_params);
959
960int adm_pack_and_set_one_pp_param(int port_id, int copp_idx,
961 struct param_hdr_v3 param_hdr, u8 *param_data)
962{
963 u8 *packed_data = NULL;
964 u32 total_size = 0;
965 int ret = 0;
966
967 total_size = sizeof(union param_hdrs) + param_hdr.param_size;
968 packed_data = kzalloc(total_size, GFP_KERNEL);
969 if (!packed_data)
970 return -ENOMEM;
971
972 ret = q6common_pack_pp_params(packed_data, &param_hdr, param_data,
973 &total_size);
974 if (ret) {
975 pr_err("%s: Failed to pack parameter data, error %d\n",
976 __func__, ret);
977 goto done;
978 }
979
980 ret = adm_set_pp_params(port_id, copp_idx, NULL, packed_data,
981 total_size);
982 if (ret)
983 pr_err("%s: Failed to set parameter data, error %d\n", __func__,
984 ret);
985done:
986 kfree(packed_data);
987 return ret;
988}
989EXPORT_SYMBOL(adm_pack_and_set_one_pp_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530990
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800991/*
992 * Only one parameter can be requested at a time. Therefore, packing and sending
993 * the request can be handled locally.
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530994 */
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800995int adm_get_pp_params(int port_id, int copp_idx, uint32_t client_id,
996 struct mem_mapping_hdr *mem_hdr,
997 struct param_hdr_v3 *param_hdr, u8 *returned_param_data)
998{
999 struct adm_cmd_get_pp_params adm_get_params;
1000 int total_size = 0;
1001 int get_param_array_sz = ARRAY_SIZE(adm_get_parameters);
1002 int returned_param_size = 0;
1003 int returned_param_size_in_bytes = 0;
1004 int port_idx = 0;
1005 int idx = 0;
1006 atomic_t *copp_stat = NULL;
1007 int ret = 0;
1008
1009 if (param_hdr == NULL) {
1010 pr_err("%s: Received NULL pointer for parameter header\n",
1011 __func__);
1012 return -EINVAL;
1013 }
1014
1015 port_id = afe_convert_virtual_to_portid(port_id);
1016 port_idx = adm_validate_and_get_port_index(port_id);
1017 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
1018 pr_err("%s: Invalid port_idx 0x%x\n", __func__, port_idx);
1019 return -EINVAL;
1020 }
1021 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
1022 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
1023 return -EINVAL;
1024 }
1025
1026 memset(&adm_get_params, 0, sizeof(adm_get_params));
1027
1028 if (mem_hdr != NULL)
1029 adm_get_params.mem_hdr = *mem_hdr;
1030
1031 q6common_pack_pp_params((u8 *) &adm_get_params.param_hdr, param_hdr,
1032 NULL, &total_size);
1033
1034 /* Pack APR header after filling body so total_size has correct value */
Vignesh Kulothunganaadecdf2018-06-20 15:08:24 -07001035 adm_get_params.apr_hdr.hdr_field =
1036 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
1037 APR_PKT_VER);
1038 adm_get_params.apr_hdr.pkt_size = sizeof(adm_get_params);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001039 adm_get_params.apr_hdr.src_svc = APR_SVC_ADM;
1040 adm_get_params.apr_hdr.src_domain = APR_DOMAIN_APPS;
1041 adm_get_params.apr_hdr.src_port = port_id;
1042 adm_get_params.apr_hdr.dest_svc = APR_SVC_ADM;
1043 adm_get_params.apr_hdr.dest_domain = APR_DOMAIN_ADSP;
1044 adm_get_params.apr_hdr.dest_port =
1045 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
1046 adm_get_params.apr_hdr.token =
1047 port_idx << 16 | client_id << 8 | copp_idx;
1048
1049 if (q6common_is_instance_id_supported())
1050 adm_get_params.apr_hdr.opcode = ADM_CMD_GET_PP_PARAMS_V6;
1051 else
1052 adm_get_params.apr_hdr.opcode = ADM_CMD_GET_PP_PARAMS_V5;
1053
1054 copp_stat = &this_adm.copp.stat[port_idx][copp_idx];
1055 atomic_set(copp_stat, -1);
Vignesh Kulothunganaadecdf2018-06-20 15:08:24 -07001056
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001057 ret = apr_send_pkt(this_adm.apr, (uint32_t *) &adm_get_params);
Vidyakumar Athotae427c412018-06-16 00:02:46 -07001058 if (ret < 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001059 pr_err("%s: Get params APR send failed port = 0x%x ret %d\n",
1060 __func__, port_id, ret);
1061 ret = -EINVAL;
1062 goto done;
1063 }
1064 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
1065 atomic_read(copp_stat) >= 0,
1066 msecs_to_jiffies(TIMEOUT_MS));
1067 if (!ret) {
1068 pr_err("%s: Get params timed out port = 0x%x\n", __func__,
1069 port_id);
1070 ret = -ETIMEDOUT;
1071 goto done;
1072 }
1073 if (atomic_read(copp_stat) > 0) {
1074 pr_err("%s: DSP returned error[%s]\n", __func__,
1075 adsp_err_get_err_str(atomic_read(copp_stat)));
1076 ret = adsp_err_get_lnx_err_code(atomic_read(copp_stat));
1077 goto done;
1078 }
1079
1080 ret = 0;
1081
1082 /* Copy data to caller if sent in band */
1083 if (!returned_param_data) {
1084 pr_debug("%s: Received NULL pointer for param destination, not copying payload\n",
1085 __func__);
1086 return 0;
1087 }
1088
1089 idx = ADM_GET_PARAMETER_LENGTH * copp_idx;
1090 returned_param_size = adm_get_parameters[idx];
1091 if (returned_param_size < 0 ||
1092 returned_param_size + idx + 1 > get_param_array_sz) {
1093 pr_err("%s: Invalid parameter size %d\n", __func__,
1094 returned_param_size);
1095 return -EINVAL;
1096 }
1097
1098 returned_param_size_in_bytes = returned_param_size * sizeof(uint32_t);
1099 if (param_hdr->param_size < returned_param_size_in_bytes) {
1100 pr_err("%s: Provided buffer is not big enough, provided buffer size(%d) size needed(%d)\n",
1101 __func__, param_hdr->param_size,
1102 returned_param_size_in_bytes);
1103 return -EINVAL;
1104 }
1105
1106 memcpy(returned_param_data, &adm_get_parameters[idx + 1],
1107 returned_param_size_in_bytes);
1108done:
1109 return ret;
1110}
1111EXPORT_SYMBOL(adm_get_pp_params);
1112
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001113int adm_get_pp_topo_module_list_v2(int port_id, int copp_idx,
1114 int32_t param_length,
1115 int32_t *returned_params)
1116{
1117 struct adm_cmd_get_pp_topo_module_list adm_get_module_list;
1118 bool iid_supported = q6common_is_instance_id_supported();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301119 int *topo_list;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001120 int num_modules = 0;
1121 int list_size = 0;
1122 int port_idx, idx;
1123 int i = 0;
1124 atomic_t *copp_stat = NULL;
1125 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301126
1127 pr_debug("%s : port_id %x", __func__, port_id);
1128 port_id = afe_convert_virtual_to_portid(port_id);
1129 port_idx = adm_validate_and_get_port_index(port_id);
1130 if (port_idx < 0) {
1131 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
1132 return -EINVAL;
1133 }
1134
1135 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
1136 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
1137 return -EINVAL;
1138 }
1139
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001140 memset(&adm_get_module_list, 0, sizeof(adm_get_module_list));
1141
1142 adm_get_module_list.apr_hdr.pkt_size = sizeof(adm_get_module_list);
1143 adm_get_module_list.apr_hdr.src_svc = APR_SVC_ADM;
1144 adm_get_module_list.apr_hdr.src_domain = APR_DOMAIN_APPS;
1145 adm_get_module_list.apr_hdr.src_port = port_id;
1146 adm_get_module_list.apr_hdr.dest_svc = APR_SVC_ADM;
1147 adm_get_module_list.apr_hdr.dest_domain = APR_DOMAIN_ADSP;
1148 adm_get_module_list.apr_hdr.dest_port =
1149 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
1150 adm_get_module_list.apr_hdr.token = port_idx << 16 | copp_idx;
1151 /*
1152 * Out of band functionality is not currently utilized.
1153 * Assume in band.
1154 */
1155 if (iid_supported) {
1156 adm_get_module_list.apr_hdr.opcode =
1157 ADM_CMD_GET_PP_TOPO_MODULE_LIST_V2;
1158 adm_get_module_list.param_max_size = param_length;
1159 } else {
1160 adm_get_module_list.apr_hdr.opcode =
1161 ADM_CMD_GET_PP_TOPO_MODULE_LIST;
1162
1163 if (param_length > U16_MAX) {
1164 pr_err("%s: Invalid param length for V1 %d\n", __func__,
1165 param_length);
1166 return -EINVAL;
1167 }
1168 adm_get_module_list.param_max_size = param_length << 16;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301169 }
1170
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001171 copp_stat = &this_adm.copp.stat[port_idx][copp_idx];
1172 atomic_set(copp_stat, -1);
1173 ret = apr_send_pkt(this_adm.apr, (uint32_t *) &adm_get_module_list);
Vidyakumar Athotae427c412018-06-16 00:02:46 -07001174 if (ret < 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001175 pr_err("%s: APR send pkt failed for port_id: 0x%x failed ret %d\n",
1176 __func__, port_id, ret);
1177 ret = -EINVAL;
1178 goto done;
1179 }
1180 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
1181 atomic_read(copp_stat) >= 0,
1182 msecs_to_jiffies(TIMEOUT_MS));
1183 if (!ret) {
1184 pr_err("%s: Timeout for port_id: 0x%x\n", __func__, port_id);
1185 ret = -ETIMEDOUT;
1186 goto done;
1187 }
1188 if (atomic_read(copp_stat) > 0) {
1189 pr_err("%s: DSP returned error[%s]\n", __func__,
1190 adsp_err_get_err_str(atomic_read(copp_stat)));
1191 ret = adsp_err_get_lnx_err_code(atomic_read(copp_stat));
1192 goto done;
1193 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301194
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001195 ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301196
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001197 if (returned_params) {
1198 /*
1199 * When processing ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST IID is
1200 * added since it is not present. Therefore, there is no need to
1201 * do anything different if IID is not supported here as it is
1202 * already taken care of.
1203 */
1204 idx = ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH * copp_idx;
1205 num_modules = adm_module_topo_list[idx];
1206 if (num_modules < 0 || num_modules > MAX_MODULES_IN_TOPO) {
1207 pr_err("%s: Invalid number of modules returned %d\n",
1208 __func__, num_modules);
1209 return -EINVAL;
1210 }
1211
1212 list_size = num_modules * sizeof(struct module_instance_info);
1213 if (param_length < list_size) {
1214 pr_err("%s: Provided buffer not big enough to hold module-instance list, provided size %d, needed size %d\n",
1215 __func__, param_length, list_size);
1216 return -EINVAL;
1217 }
1218
1219 topo_list = (int32_t *) (&adm_module_topo_list[idx]);
1220 memcpy(returned_params, topo_list, list_size);
1221 for (i = 1; i <= num_modules; i += 2) {
1222 pr_debug("module = 0x%x instance = 0x%x\n",
1223 returned_params[i], returned_params[i + 1]);
1224 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301225 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001226done:
1227 return ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301228}
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001229EXPORT_SYMBOL(adm_get_pp_topo_module_list_v2);
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301230
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301231static void adm_callback_debug_print(struct apr_client_data *data)
1232{
1233 uint32_t *payload;
1234
1235 payload = data->payload;
1236
1237 if (data->payload_size >= 8)
1238 pr_debug("%s: code = 0x%x PL#0[0x%x], PL#1[0x%x], size = %d\n",
1239 __func__, data->opcode, payload[0], payload[1],
1240 data->payload_size);
1241 else if (data->payload_size >= 4)
1242 pr_debug("%s: code = 0x%x PL#0[0x%x], size = %d\n",
1243 __func__, data->opcode, payload[0],
1244 data->payload_size);
1245 else
1246 pr_debug("%s: code = 0x%x, size = %d\n",
1247 __func__, data->opcode, data->payload_size);
1248}
1249
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301250/**
1251 * adm_set_multi_ch_map -
1252 * Update multi channel map info
1253 *
1254 * @channel_map: pointer with channel map info
1255 * @path: direction or ADM path type
1256 *
1257 * Returns 0 on success or error on failure
1258 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301259int adm_set_multi_ch_map(char *channel_map, int path)
1260{
1261 int idx;
1262
1263 if (path == ADM_PATH_PLAYBACK) {
1264 idx = ADM_MCH_MAP_IDX_PLAYBACK;
1265 } else if (path == ADM_PATH_LIVE_REC) {
1266 idx = ADM_MCH_MAP_IDX_REC;
1267 } else {
1268 pr_err("%s: invalid attempt to set path %d\n", __func__, path);
1269 return -EINVAL;
1270 }
1271
1272 memcpy(multi_ch_maps[idx].channel_mapping, channel_map,
Dieter Luecking50c55352018-09-28 14:29:17 +02001273 PCM_FORMAT_MAX_NUM_CHANNEL_V8);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301274 multi_ch_maps[idx].set_channel_map = true;
1275
1276 return 0;
1277}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301278EXPORT_SYMBOL(adm_set_multi_ch_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301279
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301280/**
1281 * adm_get_multi_ch_map -
1282 * Retrieves multi channel map info
1283 *
1284 * @channel_map: pointer to be updated with channel map
1285 * @path: direction or ADM path type
1286 *
1287 * Returns 0 on success or error on failure
1288 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301289int adm_get_multi_ch_map(char *channel_map, int path)
1290{
1291 int idx;
1292
1293 if (path == ADM_PATH_PLAYBACK) {
1294 idx = ADM_MCH_MAP_IDX_PLAYBACK;
1295 } else if (path == ADM_PATH_LIVE_REC) {
1296 idx = ADM_MCH_MAP_IDX_REC;
1297 } else {
1298 pr_err("%s: invalid attempt to get path %d\n", __func__, path);
1299 return -EINVAL;
1300 }
1301
1302 if (multi_ch_maps[idx].set_channel_map) {
1303 memcpy(channel_map, multi_ch_maps[idx].channel_mapping,
Dieter Luecking50c55352018-09-28 14:29:17 +02001304 PCM_FORMAT_MAX_NUM_CHANNEL_V8);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301305 }
1306
1307 return 0;
1308}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301309EXPORT_SYMBOL(adm_get_multi_ch_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301310
Rohit kumar2054b692019-02-01 18:01:12 +05301311/**
1312 * adm_set_port_multi_ch_map -
1313 * Update port specific channel map info
1314 *
1315 * @channel_map: pointer with channel map info
1316 * @port_id: port for which chmap is set
1317 */
1318void adm_set_port_multi_ch_map(char *channel_map, int port_id)
1319{
1320 int port_idx;
1321
1322 port_id = q6audio_convert_virtual_to_portid(port_id);
1323 port_idx = adm_validate_and_get_port_index(port_id);
1324
1325 if (port_idx < 0) {
1326 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
1327 return;
1328 }
1329
1330 memcpy(port_channel_map[port_idx].channel_mapping, channel_map,
1331 PCM_FORMAT_MAX_NUM_CHANNEL_V8);
1332 port_channel_map[port_idx].set_channel_map = true;
1333}
1334EXPORT_SYMBOL(adm_set_port_multi_ch_map);
1335
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001336static int adm_process_get_param_response(u32 opcode, u32 idx, u32 *payload,
1337 u32 payload_size)
1338{
1339 struct adm_cmd_rsp_get_pp_params_v5 *v5_rsp = NULL;
1340 struct adm_cmd_rsp_get_pp_params_v6 *v6_rsp = NULL;
1341 u32 *param_data = NULL;
1342 int data_size = 0;
1343 int struct_size = 0;
1344
1345 if (payload == NULL) {
1346 pr_err("%s: Payload is NULL\n", __func__);
1347 return -EINVAL;
1348 }
1349
1350 switch (opcode) {
1351 case ADM_CMDRSP_GET_PP_PARAMS_V5:
1352 struct_size = sizeof(struct adm_cmd_rsp_get_pp_params_v5);
Vignesh Kulothungan35524622019-01-22 11:13:09 -08001353 if (payload_size < struct_size) {
1354 pr_err("%s: payload size %d < expected size %d\n",
1355 __func__, payload_size, struct_size);
1356 break;
1357 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001358 v5_rsp = (struct adm_cmd_rsp_get_pp_params_v5 *) payload;
1359 data_size = v5_rsp->param_hdr.param_size;
1360 param_data = v5_rsp->param_data;
1361 break;
1362 case ADM_CMDRSP_GET_PP_PARAMS_V6:
1363 struct_size = sizeof(struct adm_cmd_rsp_get_pp_params_v6);
Vignesh Kulothungan35524622019-01-22 11:13:09 -08001364 if (payload_size < struct_size) {
1365 pr_err("%s: payload size %d < expected size %d\n",
1366 __func__, payload_size, struct_size);
1367 break;
1368 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001369 v6_rsp = (struct adm_cmd_rsp_get_pp_params_v6 *) payload;
1370 data_size = v6_rsp->param_hdr.param_size;
1371 param_data = v6_rsp->param_data;
1372 break;
1373 default:
1374 pr_err("%s: Invalid opcode %d\n", __func__, opcode);
1375 return -EINVAL;
1376 }
1377
1378 /*
1379 * Just store the returned parameter data, not the header. The calling
1380 * function is expected to know what it asked for. Therefore, there is
1381 * no difference between V5 and V6.
1382 */
1383 if ((payload_size >= struct_size + data_size) &&
1384 (ARRAY_SIZE(adm_get_parameters) > idx) &&
Karthikeyan Mani95be5b12018-11-09 14:57:32 -08001385 (ARRAY_SIZE(adm_get_parameters) > idx + 1 + data_size)) {
Vignesh Kulothunganaadecdf2018-06-20 15:08:24 -07001386 pr_debug("%s: Received parameter data in band\n",
1387 __func__);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001388 /*
1389 * data_size is expressed in number of bytes, store in number of
1390 * ints
1391 */
1392 adm_get_parameters[idx] =
1393 data_size / sizeof(*adm_get_parameters);
1394 pr_debug("%s: GET_PP PARAM: received parameter length: 0x%x\n",
1395 __func__, adm_get_parameters[idx]);
1396 /* store params after param_size */
1397 memcpy(&adm_get_parameters[idx + 1], param_data, data_size);
Vignesh Kulothunganaadecdf2018-06-20 15:08:24 -07001398 } else if (payload_size == sizeof(uint32_t)) {
1399 adm_get_parameters[idx] = -1;
1400 pr_debug("%s: Out of band case, setting size to %d\n",
1401 __func__, adm_get_parameters[idx]);
1402 } else {
1403 pr_err("%s: Invalid parameter combination, payload_size %d, idx %d\n",
1404 __func__, payload_size, idx);
1405 return -EINVAL;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001406 }
Vignesh Kulothunganaadecdf2018-06-20 15:08:24 -07001407 return 0;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001408}
1409
1410static int adm_process_get_topo_list_response(u32 opcode, int copp_idx,
1411 u32 num_modules, u32 *payload,
1412 u32 payload_size)
1413{
1414 u32 *fill_list = NULL;
1415 int idx = 0;
1416 int i = 0;
1417 int j = 0;
1418
1419 if (payload == NULL) {
1420 pr_err("%s: Payload is NULL\n", __func__);
1421 return -EINVAL;
1422 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
1423 pr_err("%s: Invalid COPP index %d\n", __func__, copp_idx);
1424 return -EINVAL;
1425 }
1426
1427 idx = ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH * copp_idx;
1428 fill_list = adm_module_topo_list + idx;
1429 *fill_list++ = num_modules;
1430 for (i = 0; i < num_modules; i++) {
1431 if (j > payload_size / sizeof(u32)) {
1432 pr_err("%s: Invalid number of modules specified %d\n",
1433 __func__, num_modules);
1434 return -EINVAL;
1435 }
1436
1437 /* store module ID */
1438 *fill_list++ = payload[j];
1439 j++;
1440
1441 switch (opcode) {
1442 case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST_V2:
1443 /* store instance ID */
1444 *fill_list++ = payload[j];
1445 j++;
1446 break;
1447 case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST:
1448 /* Insert IID 0 when repacking */
1449 *fill_list++ = INSTANCE_ID_0;
1450 break;
1451 default:
1452 pr_err("%s: Invalid opcode %d\n", __func__, opcode);
1453 return -EINVAL;
1454 }
1455 }
1456
1457 return 0;
1458}
1459
Laxminath Kasam30ad7512017-11-28 12:40:22 +05301460static void adm_reset_data(void)
1461{
1462 int i, j;
1463
1464 apr_reset(this_adm.apr);
1465 for (i = 0; i < AFE_MAX_PORTS; i++) {
1466 for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
1467 atomic_set(&this_adm.copp.id[i][j],
1468 RESET_COPP_ID);
1469 atomic_set(&this_adm.copp.cnt[i][j], 0);
1470 atomic_set(
1471 &this_adm.copp.topology[i][j], 0);
1472 atomic_set(&this_adm.copp.mode[i][j],
1473 0);
1474 atomic_set(&this_adm.copp.stat[i][j],
1475 0);
1476 atomic_set(&this_adm.copp.rate[i][j],
1477 0);
1478 atomic_set(
1479 &this_adm.copp.channels[i][j],
1480 0);
1481 atomic_set(
1482 &this_adm.copp.bit_width[i][j], 0);
1483 atomic_set(
1484 &this_adm.copp.app_type[i][j], 0);
1485 atomic_set(
1486 &this_adm.copp.acdb_id[i][j], 0);
Surendar Karkae7507512018-07-20 15:46:54 +05301487 atomic_set(
1488 &this_adm.copp.session_type[i][j], 0);
Laxminath Kasam30ad7512017-11-28 12:40:22 +05301489 this_adm.copp.adm_status[i][j] =
1490 ADM_STATUS_CALIBRATION_REQUIRED;
1491 }
1492 }
1493 this_adm.apr = NULL;
1494 cal_utils_clear_cal_block_q6maps(ADM_MAX_CAL_TYPES,
1495 this_adm.cal_data);
1496 mutex_lock(&this_adm.cal_data
1497 [ADM_CUSTOM_TOP_CAL]->lock);
1498 this_adm.set_custom_topology = 1;
1499 mutex_unlock(&this_adm.cal_data[
1500 ADM_CUSTOM_TOP_CAL]->lock);
1501 rtac_clear_mapping(ADM_RTAC_CAL);
1502 /*
1503 * Free the ION memory and clear the map handles
1504 * for Source Tracking
1505 */
1506 if (this_adm.sourceTrackingData.memmap.paddr != 0) {
1507 msm_audio_ion_free(
1508 this_adm.sourceTrackingData.dma_buf);
1509 this_adm.sourceTrackingData.dma_buf = NULL;
1510 this_adm.sourceTrackingData.memmap.size = 0;
1511 this_adm.sourceTrackingData.memmap.kvaddr =
1512 NULL;
1513 this_adm.sourceTrackingData.memmap.paddr = 0;
1514 this_adm.sourceTrackingData.apr_cmd_status = -1;
1515 atomic_set(&this_adm.mem_map_handles[
1516 ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
1517 }
1518}
1519
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301520static int32_t adm_callback(struct apr_client_data *data, void *priv)
1521{
1522 uint32_t *payload;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001523 int port_idx, copp_idx, idx, client_id;
1524 int num_modules;
1525 int ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301526
1527 if (data == NULL) {
1528 pr_err("%s: data parameter is null\n", __func__);
1529 return -EINVAL;
1530 }
1531
1532 payload = data->payload;
1533
1534 if (data->opcode == RESET_EVENTS) {
1535 pr_debug("%s: Reset event is received: %d %d apr[%pK]\n",
1536 __func__,
1537 data->reset_event, data->reset_proc, this_adm.apr);
Laxminath Kasam30ad7512017-11-28 12:40:22 +05301538 if (this_adm.apr)
1539 adm_reset_data();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301540 return 0;
1541 }
1542
1543 adm_callback_debug_print(data);
Vignesh Kulothungan4965b8d2019-03-16 18:26:22 -07001544 if (data->payload_size >= sizeof(uint32_t)) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301545 copp_idx = (data->token) & 0XFF;
1546 port_idx = ((data->token) >> 16) & 0xFF;
1547 client_id = ((data->token) >> 8) & 0xFF;
1548 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
1549 pr_err("%s: Invalid port idx %d token %d\n",
1550 __func__, port_idx, data->token);
1551 return 0;
1552 }
1553 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
1554 pr_err("%s: Invalid copp idx %d token %d\n",
1555 __func__, copp_idx, data->token);
1556 return 0;
1557 }
1558 if (client_id < 0 || client_id >= ADM_CLIENT_ID_MAX) {
1559 pr_err("%s: Invalid client id %d\n", __func__,
1560 client_id);
1561 return 0;
1562 }
1563 if (data->opcode == APR_BASIC_RSP_RESULT) {
1564 pr_debug("%s: APR_BASIC_RSP_RESULT id 0x%x\n",
1565 __func__, payload[0]);
Vignesh Kulothungan4965b8d2019-03-16 18:26:22 -07001566
1567 if (!((client_id != ADM_CLIENT_ID_SOURCE_TRACKING) &&
1568 ((payload[0] == ADM_CMD_SET_PP_PARAMS_V5) ||
1569 (payload[0] == ADM_CMD_SET_PP_PARAMS_V6)))) {
1570 if (data->payload_size <
1571 (2 * sizeof(uint32_t))) {
1572 pr_err("%s: Invalid payload size %d\n",
1573 __func__, data->payload_size);
1574 return 0;
1575 }
1576 }
1577
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301578 if (payload[1] != 0) {
1579 pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
1580 __func__, payload[0], payload[1]);
1581 }
1582 switch (payload[0]) {
1583 case ADM_CMD_SET_PP_PARAMS_V5:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001584 case ADM_CMD_SET_PP_PARAMS_V6:
1585 pr_debug("%s: ADM_CMD_SET_PP_PARAMS\n",
1586 __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301587 if (client_id == ADM_CLIENT_ID_SOURCE_TRACKING)
1588 this_adm.sourceTrackingData.
1589 apr_cmd_status = payload[1];
1590 else if (rtac_make_adm_callback(payload,
1591 data->payload_size))
1592 break;
1593 /*
1594 * if soft volume is called and already
1595 * interrupted break out of the sequence here
1596 */
1597 case ADM_CMD_DEVICE_OPEN_V5:
1598 case ADM_CMD_DEVICE_CLOSE_V5:
1599 case ADM_CMD_DEVICE_OPEN_V6:
Dieter Luecking50c55352018-09-28 14:29:17 +02001600 case ADM_CMD_DEVICE_OPEN_V8:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301601 pr_debug("%s: Basic callback received, wake up.\n",
1602 __func__);
1603 atomic_set(&this_adm.copp.stat[port_idx]
1604 [copp_idx], payload[1]);
1605 wake_up(
1606 &this_adm.copp.wait[port_idx][copp_idx]);
1607 break;
1608 case ADM_CMD_ADD_TOPOLOGIES:
1609 pr_debug("%s: callback received, ADM_CMD_ADD_TOPOLOGIES.\n",
1610 __func__);
1611 atomic_set(&this_adm.adm_stat, payload[1]);
1612 wake_up(&this_adm.adm_wait);
1613 break;
1614 case ADM_CMD_MATRIX_MAP_ROUTINGS_V5:
1615 case ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5:
1616 pr_debug("%s: Basic callback received, wake up.\n",
1617 __func__);
1618 atomic_set(&this_adm.matrix_map_stat,
1619 payload[1]);
1620 wake_up(&this_adm.matrix_map_wait);
1621 break;
1622 case ADM_CMD_SHARED_MEM_UNMAP_REGIONS:
1623 pr_debug("%s: ADM_CMD_SHARED_MEM_UNMAP_REGIONS\n",
1624 __func__);
1625 atomic_set(&this_adm.adm_stat, payload[1]);
1626 wake_up(&this_adm.adm_wait);
1627 break;
1628 case ADM_CMD_SHARED_MEM_MAP_REGIONS:
1629 pr_debug("%s: ADM_CMD_SHARED_MEM_MAP_REGIONS\n",
1630 __func__);
1631 /* Should only come here if there is an APR */
1632 /* error or malformed APR packet. Otherwise */
1633 /* response will be returned as */
1634 if (payload[1] != 0) {
1635 pr_err("%s: ADM map error, resuming\n",
1636 __func__);
1637 atomic_set(&this_adm.adm_stat,
1638 payload[1]);
1639 wake_up(&this_adm.adm_wait);
1640 }
1641 break;
1642 case ADM_CMD_GET_PP_PARAMS_V5:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001643 case ADM_CMD_GET_PP_PARAMS_V6:
1644 pr_debug("%s: ADM_CMD_GET_PP_PARAMS\n",
1645 __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301646 /* Should only come here if there is an APR */
1647 /* error or malformed APR packet. Otherwise */
1648 /* response will be returned as */
1649 /* ADM_CMDRSP_GET_PP_PARAMS_V5 */
1650 if (client_id ==
1651 ADM_CLIENT_ID_SOURCE_TRACKING) {
1652 this_adm.sourceTrackingData.
1653 apr_cmd_status = payload[1];
1654 if (payload[1] != 0)
1655 pr_err("%s: ADM get param error = %d\n",
1656 __func__, payload[1]);
1657
1658 atomic_set(&this_adm.copp.stat
1659 [port_idx][copp_idx],
1660 payload[1]);
1661 wake_up(&this_adm.copp.wait
1662 [port_idx][copp_idx]);
1663 } else {
1664 if (payload[1] != 0) {
1665 pr_err("%s: ADM get param error = %d, resuming\n",
1666 __func__, payload[1]);
1667
1668 rtac_make_adm_callback(payload,
1669 data->payload_size);
1670 }
1671 }
1672 break;
1673 case ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5:
Bhalchandra Gajareeed46bd2018-05-15 16:48:07 -07001674 case ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V6:
1675 pr_debug("%s:callback received PSPD MTMX, wake up\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301676 __func__);
1677 atomic_set(&this_adm.copp.stat[port_idx]
1678 [copp_idx], payload[1]);
1679 wake_up(
1680 &this_adm.copp.wait[port_idx][copp_idx]);
1681 break;
1682 case ADM_CMD_GET_PP_TOPO_MODULE_LIST:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001683 case ADM_CMD_GET_PP_TOPO_MODULE_LIST_V2:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301684 pr_debug("%s:ADM_CMD_GET_PP_TOPO_MODULE_LIST\n",
1685 __func__);
1686 if (payload[1] != 0)
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001687 pr_err("%s: ADM get topo list error = %d\n",
1688 __func__, payload[1]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301689 break;
1690 default:
1691 pr_err("%s: Unknown Cmd: 0x%x\n", __func__,
1692 payload[0]);
1693 break;
1694 }
1695 return 0;
1696 }
1697
1698 switch (data->opcode) {
1699 case ADM_CMDRSP_DEVICE_OPEN_V5:
Dieter Luecking50c55352018-09-28 14:29:17 +02001700 case ADM_CMDRSP_DEVICE_OPEN_V6:
1701 case ADM_CMDRSP_DEVICE_OPEN_V8: {
Vignesh Kulothungan35524622019-01-22 11:13:09 -08001702 struct adm_cmd_rsp_device_open_v5 *open = NULL;
1703 if (data->payload_size <
1704 sizeof(struct adm_cmd_rsp_device_open_v5)) {
1705 pr_err("%s: Invalid payload size %d\n", __func__,
1706 data->payload_size);
1707 return 0;
1708 }
1709 open = (struct adm_cmd_rsp_device_open_v5 *)data->payload;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301710 if (open->copp_id == INVALID_COPP_ID) {
1711 pr_err("%s: invalid coppid rxed %d\n",
1712 __func__, open->copp_id);
1713 atomic_set(&this_adm.copp.stat[port_idx]
1714 [copp_idx], ADSP_EBADPARAM);
1715 wake_up(
1716 &this_adm.copp.wait[port_idx][copp_idx]);
1717 break;
1718 }
1719 atomic_set(&this_adm.copp.stat
1720 [port_idx][copp_idx], payload[0]);
1721 atomic_set(&this_adm.copp.id[port_idx][copp_idx],
1722 open->copp_id);
1723 pr_debug("%s: coppid rxed=%d\n", __func__,
1724 open->copp_id);
1725 wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
1726 }
1727 break;
1728 case ADM_CMDRSP_GET_PP_PARAMS_V5:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001729 case ADM_CMDRSP_GET_PP_PARAMS_V6:
1730 pr_debug("%s: ADM_CMDRSP_GET_PP_PARAMS\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301731 if (client_id == ADM_CLIENT_ID_SOURCE_TRACKING)
1732 this_adm.sourceTrackingData.apr_cmd_status =
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001733 payload[0];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301734 else if (rtac_make_adm_callback(payload,
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001735 data->payload_size))
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301736 break;
1737
1738 idx = ADM_GET_PARAMETER_LENGTH * copp_idx;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001739 if (payload[0] == 0 && data->payload_size > 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001740 ret = adm_process_get_param_response(
1741 data->opcode, idx, payload,
1742 data->payload_size);
1743 if (ret)
1744 pr_err("%s: Failed to process get param response, error %d\n",
1745 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301746 } else {
1747 adm_get_parameters[idx] = -1;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001748 pr_err("%s: ADM_CMDRSP_GET_PP_PARAMS returned error 0x%x\n",
1749 __func__, payload[0]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301750 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001751 atomic_set(&this_adm.copp.stat[port_idx][copp_idx],
1752 payload[0]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301753 wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
1754 break;
1755 case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001756 case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST_V2:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301757 pr_debug("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST\n",
1758 __func__);
Vignesh Kulothungan4965b8d2019-03-16 18:26:22 -07001759 if (data->payload_size >= (2 * sizeof(uint32_t))) {
1760 num_modules = payload[1];
1761 pr_debug("%s: Num modules %d\n", __func__,
1762 num_modules);
1763 if (payload[0]) {
1764 pr_err("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST, error = %d\n",
1765 __func__, payload[0]);
1766 } else if (num_modules > MAX_MODULES_IN_TOPO) {
1767 pr_err("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST invalid num modules received, num modules = %d\n",
1768 __func__, num_modules);
1769 } else {
1770 ret = adm_process_get_topo_list_response(
1771 data->opcode, copp_idx,
1772 num_modules, payload,
1773 data->payload_size);
1774 if (ret)
1775 pr_err("%s: Failed to process get topo modules list response, error %d\n",
1776 __func__, ret);
1777 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301778 } else {
Vignesh Kulothungan4965b8d2019-03-16 18:26:22 -07001779 pr_err("%s: Invalid payload size %d\n",
1780 __func__, data->payload_size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301781 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001782 atomic_set(&this_adm.copp.stat[port_idx][copp_idx],
1783 payload[0]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301784 wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
1785 break;
1786 case ADM_CMDRSP_SHARED_MEM_MAP_REGIONS:
1787 pr_debug("%s: ADM_CMDRSP_SHARED_MEM_MAP_REGIONS\n",
1788 __func__);
1789 atomic_set(&this_adm.mem_map_handles[
1790 atomic_read(&this_adm.mem_map_index)],
1791 *payload);
1792 atomic_set(&this_adm.adm_stat, 0);
1793 wake_up(&this_adm.adm_wait);
1794 break;
1795 default:
1796 pr_err("%s: Unknown cmd:0x%x\n", __func__,
1797 data->opcode);
1798 break;
1799 }
1800 }
1801 return 0;
1802}
1803
1804static int adm_memory_map_regions(phys_addr_t *buf_add, uint32_t mempool_id,
1805 uint32_t *bufsz, uint32_t bufcnt)
1806{
1807 struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
1808 struct avs_shared_map_region_payload *mregions = NULL;
1809 void *mmap_region_cmd = NULL;
1810 void *payload = NULL;
1811 int ret = 0;
1812 int i = 0;
1813 int cmd_size = 0;
1814
1815 pr_debug("%s:\n", __func__);
1816 if (this_adm.apr == NULL) {
1817 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
1818 0xFFFFFFFF, &this_adm);
1819 if (this_adm.apr == NULL) {
1820 pr_err("%s: Unable to register ADM\n", __func__);
1821 ret = -ENODEV;
1822 return ret;
1823 }
1824 rtac_set_adm_handle(this_adm.apr);
1825 }
1826
1827 cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
1828 + sizeof(struct avs_shared_map_region_payload)
1829 * bufcnt;
1830
1831 mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
1832 if (!mmap_region_cmd)
1833 return -ENOMEM;
1834
1835 mmap_regions = (struct avs_cmd_shared_mem_map_regions *)mmap_region_cmd;
1836 mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1837 APR_HDR_LEN(APR_HDR_SIZE),
1838 APR_PKT_VER);
1839 mmap_regions->hdr.pkt_size = cmd_size;
1840 mmap_regions->hdr.src_port = 0;
1841
1842 mmap_regions->hdr.dest_port = 0;
1843 mmap_regions->hdr.token = 0;
1844 mmap_regions->hdr.opcode = ADM_CMD_SHARED_MEM_MAP_REGIONS;
1845 mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL & 0x00ff;
1846 mmap_regions->num_regions = bufcnt & 0x00ff;
1847 mmap_regions->property_flag = 0x00;
1848
1849 pr_debug("%s: map_regions->num_regions = %d\n", __func__,
1850 mmap_regions->num_regions);
1851 payload = ((u8 *) mmap_region_cmd +
1852 sizeof(struct avs_cmd_shared_mem_map_regions));
1853 mregions = (struct avs_shared_map_region_payload *)payload;
1854
1855 for (i = 0; i < bufcnt; i++) {
1856 mregions->shm_addr_lsw = lower_32_bits(buf_add[i]);
1857 mregions->shm_addr_msw =
1858 msm_audio_populate_upper_32_bits(buf_add[i]);
1859 mregions->mem_size_bytes = bufsz[i];
1860 ++mregions;
1861 }
1862
1863 atomic_set(&this_adm.adm_stat, -1);
1864 ret = apr_send_pkt(this_adm.apr, (uint32_t *) mmap_region_cmd);
1865 if (ret < 0) {
1866 pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__,
1867 mmap_regions->hdr.opcode, ret);
1868 ret = -EINVAL;
1869 goto fail_cmd;
1870 }
1871
1872 ret = wait_event_timeout(this_adm.adm_wait,
1873 atomic_read(&this_adm.adm_stat) >= 0,
Vignesh Kulothungan0fcf2af2018-09-20 17:43:49 -07001874 msecs_to_jiffies(TIMEOUT_MS));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301875 if (!ret) {
1876 pr_err("%s: timeout. waited for memory_map\n", __func__);
1877 ret = -EINVAL;
1878 goto fail_cmd;
1879 } else if (atomic_read(&this_adm.adm_stat) > 0) {
1880 pr_err("%s: DSP returned error[%s]\n",
1881 __func__, adsp_err_get_err_str(
1882 atomic_read(&this_adm.adm_stat)));
1883 ret = adsp_err_get_lnx_err_code(
1884 atomic_read(&this_adm.adm_stat));
1885 goto fail_cmd;
1886 }
1887fail_cmd:
1888 kfree(mmap_region_cmd);
1889 return ret;
1890}
1891
1892static int adm_memory_unmap_regions(void)
1893{
1894 struct avs_cmd_shared_mem_unmap_regions unmap_regions;
1895 int ret = 0;
1896
1897 pr_debug("%s:\n", __func__);
1898 if (this_adm.apr == NULL) {
1899 pr_err("%s: APR handle NULL\n", __func__);
1900 return -EINVAL;
1901 }
1902
1903 unmap_regions.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1904 APR_HDR_LEN(APR_HDR_SIZE),
1905 APR_PKT_VER);
1906 unmap_regions.hdr.pkt_size = sizeof(unmap_regions);
1907 unmap_regions.hdr.src_port = 0;
1908 unmap_regions.hdr.dest_port = 0;
1909 unmap_regions.hdr.token = 0;
1910 unmap_regions.hdr.opcode = ADM_CMD_SHARED_MEM_UNMAP_REGIONS;
1911 unmap_regions.mem_map_handle = atomic_read(&this_adm.
1912 mem_map_handles[atomic_read(&this_adm.mem_map_index)]);
1913 atomic_set(&this_adm.adm_stat, -1);
1914 ret = apr_send_pkt(this_adm.apr, (uint32_t *) &unmap_regions);
1915 if (ret < 0) {
1916 pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__,
1917 unmap_regions.hdr.opcode, ret);
1918 ret = -EINVAL;
1919 goto fail_cmd;
1920 }
1921
1922 ret = wait_event_timeout(this_adm.adm_wait,
1923 atomic_read(&this_adm.adm_stat) >= 0,
Vignesh Kulothungan0fcf2af2018-09-20 17:43:49 -07001924 msecs_to_jiffies(TIMEOUT_MS));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301925 if (!ret) {
1926 pr_err("%s: timeout. waited for memory_unmap\n",
1927 __func__);
1928 ret = -EINVAL;
1929 goto fail_cmd;
1930 } else if (atomic_read(&this_adm.adm_stat) > 0) {
1931 pr_err("%s: DSP returned error[%s]\n",
1932 __func__, adsp_err_get_err_str(
1933 atomic_read(&this_adm.adm_stat)));
1934 ret = adsp_err_get_lnx_err_code(
1935 atomic_read(&this_adm.adm_stat));
1936 goto fail_cmd;
1937 } else {
1938 pr_debug("%s: Unmap handle 0x%x succeeded\n", __func__,
1939 unmap_regions.mem_map_handle);
1940 }
1941fail_cmd:
1942 return ret;
1943}
1944
1945static int remap_cal_data(struct cal_block_data *cal_block, int cal_index)
1946{
1947 int ret = 0;
1948
Banajit Goswami08bb7362017-11-03 22:48:23 -07001949 if (cal_block->map_data.dma_buf == NULL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301950 pr_err("%s: No ION allocation for cal index %d!\n",
1951 __func__, cal_index);
1952 ret = -EINVAL;
1953 goto done;
1954 }
1955
1956 if ((cal_block->map_data.map_size > 0) &&
1957 (cal_block->map_data.q6map_handle == 0)) {
1958 atomic_set(&this_adm.mem_map_index, cal_index);
1959 ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
1960 (uint32_t *)&cal_block->map_data.map_size, 1);
1961 if (ret < 0) {
1962 pr_err("%s: ADM mmap did not work! size = %zd ret %d\n",
1963 __func__,
1964 cal_block->map_data.map_size, ret);
1965 pr_debug("%s: ADM mmap did not work! addr = 0x%pK, size = %zd ret %d\n",
1966 __func__,
1967 &cal_block->cal_data.paddr,
1968 cal_block->map_data.map_size, ret);
1969 goto done;
1970 }
1971 cal_block->map_data.q6map_handle = atomic_read(&this_adm.
1972 mem_map_handles[cal_index]);
1973 }
1974done:
1975 return ret;
1976}
1977
1978static void send_adm_custom_topology(void)
1979{
1980 struct cal_block_data *cal_block = NULL;
1981 struct cmd_set_topologies adm_top;
1982 int cal_index = ADM_CUSTOM_TOP_CAL;
1983 int result;
1984
1985 if (this_adm.cal_data[cal_index] == NULL)
1986 goto done;
1987
1988 mutex_lock(&this_adm.cal_data[cal_index]->lock);
1989 if (!this_adm.set_custom_topology)
1990 goto unlock;
1991 this_adm.set_custom_topology = 0;
1992
1993 cal_block = cal_utils_get_only_cal_block(this_adm.cal_data[cal_index]);
Vikram Panduranga770b8382017-09-27 12:17:36 -07001994 if (cal_block == NULL || cal_utils_is_cal_stale(cal_block))
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301995 goto unlock;
1996
1997 pr_debug("%s: Sending cal_index %d\n", __func__, cal_index);
1998
1999 result = remap_cal_data(cal_block, cal_index);
2000 if (result) {
2001 pr_err("%s: Remap_cal_data failed for cal %d!\n",
2002 __func__, cal_index);
2003 goto unlock;
2004 }
2005 atomic_set(&this_adm.mem_map_index, cal_index);
2006 atomic_set(&this_adm.mem_map_handles[cal_index],
2007 cal_block->map_data.q6map_handle);
2008
2009 if (cal_block->cal_data.size == 0) {
2010 pr_debug("%s: No ADM cal to send\n", __func__);
2011 goto unlock;
2012 }
2013
2014 adm_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2015 APR_HDR_LEN(20), APR_PKT_VER);
2016 adm_top.hdr.pkt_size = sizeof(adm_top);
2017 adm_top.hdr.src_svc = APR_SVC_ADM;
2018 adm_top.hdr.src_domain = APR_DOMAIN_APPS;
2019 adm_top.hdr.src_port = 0;
2020 adm_top.hdr.dest_svc = APR_SVC_ADM;
2021 adm_top.hdr.dest_domain = APR_DOMAIN_ADSP;
2022 adm_top.hdr.dest_port = 0;
2023 adm_top.hdr.token = 0;
2024 adm_top.hdr.opcode = ADM_CMD_ADD_TOPOLOGIES;
2025 adm_top.payload_addr_lsw = lower_32_bits(cal_block->cal_data.paddr);
2026 adm_top.payload_addr_msw = msm_audio_populate_upper_32_bits(
2027 cal_block->cal_data.paddr);
2028 adm_top.mem_map_handle = cal_block->map_data.q6map_handle;
2029 adm_top.payload_size = cal_block->cal_data.size;
2030
2031 atomic_set(&this_adm.adm_stat, -1);
2032 pr_debug("%s: Sending ADM_CMD_ADD_TOPOLOGIES payload = 0x%pK, size = %d\n",
2033 __func__, &cal_block->cal_data.paddr,
2034 adm_top.payload_size);
2035 result = apr_send_pkt(this_adm.apr, (uint32_t *)&adm_top);
2036 if (result < 0) {
2037 pr_err("%s: Set topologies failed payload size = %zd result %d\n",
2038 __func__, cal_block->cal_data.size, result);
2039 goto unlock;
2040 }
2041 /* Wait for the callback */
2042 result = wait_event_timeout(this_adm.adm_wait,
2043 atomic_read(&this_adm.adm_stat) >= 0,
2044 msecs_to_jiffies(TIMEOUT_MS));
2045 if (!result) {
2046 pr_err("%s: Set topologies timed out payload size = %zd\n",
2047 __func__, cal_block->cal_data.size);
2048 goto unlock;
2049 } else if (atomic_read(&this_adm.adm_stat) > 0) {
2050 pr_err("%s: DSP returned error[%s]\n",
2051 __func__, adsp_err_get_err_str(
2052 atomic_read(&this_adm.adm_stat)));
2053 result = adsp_err_get_lnx_err_code(
2054 atomic_read(&this_adm.adm_stat));
2055 goto unlock;
2056 }
2057unlock:
2058 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
2059done:
2060 return;
2061}
2062
2063static int send_adm_cal_block(int port_id, int copp_idx,
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002064 struct cal_block_data *cal_block, int perf_mode)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302065{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002066 struct mem_mapping_hdr mem_hdr;
2067 int payload_size = 0;
2068 int port_idx = 0;
2069 int topology = 0;
2070 int result = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302071
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002072 pr_debug("%s: Port id 0x%x,\n", __func__, port_id);
2073
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302074 if (!cal_block) {
2075 pr_debug("%s: No ADM cal to send for port_id = 0x%x!\n",
2076 __func__, port_id);
2077 result = -EINVAL;
2078 goto done;
2079 }
2080 if (cal_block->cal_data.size <= 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002081 pr_debug("%s: No ADM cal sent for port_id = 0x%x!\n", __func__,
2082 port_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302083 result = -EINVAL;
2084 goto done;
2085 }
2086
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002087 memset(&mem_hdr, 0, sizeof(mem_hdr));
2088 port_id = afe_convert_virtual_to_portid(port_id);
2089 port_idx = adm_validate_and_get_port_index(port_id);
2090 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
2091 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2092 return -EINVAL;
2093 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
2094 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
2095 return -EINVAL;
2096 }
2097
2098 topology = atomic_read(&this_adm.copp.topology[port_idx][copp_idx]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302099 if (perf_mode == LEGACY_PCM_MODE &&
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002100 topology == DS2_ADM_COPP_TOPOLOGY_ID) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302101 pr_err("%s: perf_mode %d, topology 0x%x\n", __func__, perf_mode,
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002102 topology);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302103 goto done;
2104 }
2105
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002106 mem_hdr.data_payload_addr_lsw =
2107 lower_32_bits(cal_block->cal_data.paddr);
2108 mem_hdr.data_payload_addr_msw =
2109 msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
2110 mem_hdr.mem_map_handle = cal_block->map_data.q6map_handle;
2111 payload_size = cal_block->cal_data.size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302112
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002113 adm_set_pp_params(port_id, copp_idx, &mem_hdr, NULL, payload_size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302114
2115done:
2116 return result;
2117}
2118
2119static struct cal_block_data *adm_find_cal_by_path(int cal_index, int path)
2120{
2121 struct list_head *ptr, *next;
2122 struct cal_block_data *cal_block = NULL;
2123 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2124 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2125
2126 pr_debug("%s:\n", __func__);
2127
2128 list_for_each_safe(ptr, next,
2129 &this_adm.cal_data[cal_index]->cal_blocks) {
2130
2131 cal_block = list_entry(ptr,
2132 struct cal_block_data, list);
2133
Vikram Panduranga770b8382017-09-27 12:17:36 -07002134 if (cal_utils_is_cal_stale(cal_block))
2135 continue;
2136
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302137 if (cal_index == ADM_AUDPROC_CAL ||
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002138 cal_index == ADM_LSM_AUDPROC_CAL ||
2139 cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302140 audproc_cal_info = cal_block->cal_info;
2141 if ((audproc_cal_info->path == path) &&
2142 (cal_block->cal_data.size > 0))
2143 return cal_block;
2144 } else if (cal_index == ADM_AUDVOL_CAL) {
2145 audvol_cal_info = cal_block->cal_info;
2146 if ((audvol_cal_info->path == path) &&
2147 (cal_block->cal_data.size > 0))
2148 return cal_block;
2149 }
2150 }
2151 pr_debug("%s: Can't find ADM cal for cal_index %d, path %d\n",
2152 __func__, cal_index, path);
2153 return NULL;
2154}
2155
2156static struct cal_block_data *adm_find_cal_by_app_type(int cal_index, int path,
2157 int app_type)
2158{
2159 struct list_head *ptr, *next;
2160 struct cal_block_data *cal_block = NULL;
2161 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2162 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2163
2164 pr_debug("%s\n", __func__);
2165
2166 list_for_each_safe(ptr, next,
2167 &this_adm.cal_data[cal_index]->cal_blocks) {
2168
2169 cal_block = list_entry(ptr,
2170 struct cal_block_data, list);
2171
Vikram Panduranga770b8382017-09-27 12:17:36 -07002172 if (cal_utils_is_cal_stale(cal_block))
2173 continue;
2174
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302175 if (cal_index == ADM_AUDPROC_CAL ||
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002176 cal_index == ADM_LSM_AUDPROC_CAL ||
2177 cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302178 audproc_cal_info = cal_block->cal_info;
2179 if ((audproc_cal_info->path == path) &&
2180 (audproc_cal_info->app_type == app_type) &&
2181 (cal_block->cal_data.size > 0))
2182 return cal_block;
2183 } else if (cal_index == ADM_AUDVOL_CAL) {
2184 audvol_cal_info = cal_block->cal_info;
2185 if ((audvol_cal_info->path == path) &&
2186 (audvol_cal_info->app_type == app_type) &&
2187 (cal_block->cal_data.size > 0))
2188 return cal_block;
2189 }
2190 }
2191 pr_debug("%s: Can't find ADM cali for cal_index %d, path %d, app %d, defaulting to search by path\n",
2192 __func__, cal_index, path, app_type);
2193 return adm_find_cal_by_path(cal_index, path);
2194}
2195
2196
2197static struct cal_block_data *adm_find_cal(int cal_index, int path,
2198 int app_type, int acdb_id,
2199 int sample_rate)
2200{
2201 struct list_head *ptr, *next;
2202 struct cal_block_data *cal_block = NULL;
2203 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2204 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2205
2206 pr_debug("%s:\n", __func__);
2207
2208 list_for_each_safe(ptr, next,
2209 &this_adm.cal_data[cal_index]->cal_blocks) {
2210
2211 cal_block = list_entry(ptr,
2212 struct cal_block_data, list);
Vikram Panduranga770b8382017-09-27 12:17:36 -07002213 if (cal_utils_is_cal_stale(cal_block))
2214 continue;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302215
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302216 if (cal_index == ADM_AUDPROC_CAL ||
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002217 cal_index == ADM_LSM_AUDPROC_CAL ||
2218 cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302219 audproc_cal_info = cal_block->cal_info;
2220 if ((audproc_cal_info->path == path) &&
2221 (audproc_cal_info->app_type == app_type) &&
2222 (audproc_cal_info->acdb_id == acdb_id) &&
2223 (audproc_cal_info->sample_rate == sample_rate) &&
2224 (cal_block->cal_data.size > 0))
2225 return cal_block;
2226 } else if (cal_index == ADM_AUDVOL_CAL) {
2227 audvol_cal_info = cal_block->cal_info;
2228 if ((audvol_cal_info->path == path) &&
2229 (audvol_cal_info->app_type == app_type) &&
2230 (audvol_cal_info->acdb_id == acdb_id) &&
2231 (cal_block->cal_data.size > 0))
2232 return cal_block;
2233 }
2234 }
2235 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",
2236 __func__, cal_index, path, app_type, acdb_id, sample_rate);
2237 return adm_find_cal_by_app_type(cal_index, path, app_type);
2238}
2239
2240static int adm_remap_and_send_cal_block(int cal_index, int port_id,
2241 int copp_idx, struct cal_block_data *cal_block, int perf_mode,
2242 int app_type, int acdb_id, int sample_rate)
2243{
2244 int ret = 0;
2245
2246 pr_debug("%s: Sending cal_index cal %d\n", __func__, cal_index);
2247 ret = remap_cal_data(cal_block, cal_index);
2248 if (ret) {
2249 pr_err("%s: Remap_cal_data failed for cal %d!\n",
2250 __func__, cal_index);
2251 goto done;
2252 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002253 ret = send_adm_cal_block(port_id, copp_idx, cal_block, perf_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302254 if (ret < 0)
2255 pr_debug("%s: No cal sent for cal_index %d, port_id = 0x%x! ret %d sample_rate %d\n",
2256 __func__, cal_index, port_id, ret, sample_rate);
2257done:
2258 return ret;
2259}
2260
2261static void send_adm_cal_type(int cal_index, int path, int port_id,
2262 int copp_idx, int perf_mode, int app_type,
2263 int acdb_id, int sample_rate)
2264{
2265 struct cal_block_data *cal_block = NULL;
2266 int ret;
2267
2268 pr_debug("%s: cal index %d\n", __func__, cal_index);
2269
2270 if (this_adm.cal_data[cal_index] == NULL) {
2271 pr_debug("%s: cal_index %d not allocated!\n",
2272 __func__, cal_index);
2273 goto done;
2274 }
2275
2276 mutex_lock(&this_adm.cal_data[cal_index]->lock);
2277 cal_block = adm_find_cal(cal_index, path, app_type, acdb_id,
2278 sample_rate);
2279 if (cal_block == NULL)
2280 goto unlock;
2281
2282 ret = adm_remap_and_send_cal_block(cal_index, port_id, copp_idx,
2283 cal_block, perf_mode, app_type, acdb_id, sample_rate);
Vikram Panduranga770b8382017-09-27 12:17:36 -07002284
2285 cal_utils_mark_cal_used(cal_block);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302286unlock:
2287 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
2288done:
2289 return;
2290}
2291
2292static int get_cal_path(int path)
2293{
2294 if (path == 0x1)
2295 return RX_DEVICE;
2296 else
2297 return TX_DEVICE;
2298}
2299
2300static void send_adm_cal(int port_id, int copp_idx, int path, int perf_mode,
Aditya Bavanari5106b562018-01-08 13:16:32 +05302301 int app_type, int acdb_id, int sample_rate,
2302 int passthr_mode)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302303{
2304 pr_debug("%s: port id 0x%x copp_idx %d\n", __func__, port_id, copp_idx);
2305
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002306 if (passthr_mode != LISTEN) {
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302307 send_adm_cal_type(ADM_AUDPROC_CAL, path, port_id, copp_idx,
2308 perf_mode, app_type, acdb_id, sample_rate);
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002309 } else {
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302310 send_adm_cal_type(ADM_LSM_AUDPROC_CAL, path, port_id, copp_idx,
2311 perf_mode, app_type, acdb_id, sample_rate);
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002312
2313 send_adm_cal_type(ADM_LSM_AUDPROC_PERSISTENT_CAL, path,
2314 port_id, copp_idx, perf_mode, app_type,
2315 acdb_id, sample_rate);
2316 }
2317
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302318 send_adm_cal_type(ADM_AUDVOL_CAL, path, port_id, copp_idx, perf_mode,
2319 app_type, acdb_id, sample_rate);
2320}
2321
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302322/**
2323 * adm_connect_afe_port -
2324 * command to send ADM connect AFE port
2325 *
2326 * @mode: value of mode for ADM connect AFE
2327 * @session_id: session active to connect
2328 * @port_id: Port ID number
2329 *
2330 * Returns 0 on success or error on failure
2331 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302332int adm_connect_afe_port(int mode, int session_id, int port_id)
2333{
2334 struct adm_cmd_connect_afe_port_v5 cmd;
2335 int ret = 0;
2336 int port_idx, copp_idx = 0;
2337
2338 pr_debug("%s: port_id: 0x%x session id:%d mode:%d\n", __func__,
2339 port_id, session_id, mode);
2340
2341 port_id = afe_convert_virtual_to_portid(port_id);
2342 port_idx = adm_validate_and_get_port_index(port_id);
2343 if (port_idx < 0) {
2344 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2345 return -EINVAL;
2346 }
2347
2348 if (this_adm.apr == NULL) {
2349 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
2350 0xFFFFFFFF, &this_adm);
2351 if (this_adm.apr == NULL) {
2352 pr_err("%s: Unable to register ADM\n", __func__);
2353 ret = -ENODEV;
2354 return ret;
2355 }
2356 rtac_set_adm_handle(this_adm.apr);
2357 }
2358 pr_debug("%s: Port ID 0x%x, index %d\n", __func__, port_id, port_idx);
2359
2360 cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2361 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
2362 cmd.hdr.pkt_size = sizeof(cmd);
2363 cmd.hdr.src_svc = APR_SVC_ADM;
2364 cmd.hdr.src_domain = APR_DOMAIN_APPS;
2365 cmd.hdr.src_port = port_id;
2366 cmd.hdr.dest_svc = APR_SVC_ADM;
2367 cmd.hdr.dest_domain = APR_DOMAIN_ADSP;
2368 cmd.hdr.dest_port = 0; /* Ignored */
2369 cmd.hdr.token = port_idx << 16 | copp_idx;
2370 cmd.hdr.opcode = ADM_CMD_CONNECT_AFE_PORT_V5;
2371
2372 cmd.mode = mode;
2373 cmd.session_id = session_id;
2374 cmd.afe_port_id = port_id;
2375
2376 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2377 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&cmd);
2378 if (ret < 0) {
2379 pr_err("%s: ADM enable for port_id: 0x%x failed ret %d\n",
2380 __func__, port_id, ret);
2381 ret = -EINVAL;
2382 goto fail_cmd;
2383 }
2384 /* Wait for the callback with copp id */
2385 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
2386 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
2387 msecs_to_jiffies(TIMEOUT_MS));
2388 if (!ret) {
2389 pr_err("%s: ADM connect timedout for port_id: 0x%x\n",
2390 __func__, port_id);
2391 ret = -EINVAL;
2392 goto fail_cmd;
2393 } else if (atomic_read(&this_adm.copp.stat
2394 [port_idx][copp_idx]) > 0) {
2395 pr_err("%s: DSP returned error[%s]\n",
2396 __func__, adsp_err_get_err_str(
2397 atomic_read(&this_adm.copp.stat
2398 [port_idx][copp_idx])));
2399 ret = adsp_err_get_lnx_err_code(
2400 atomic_read(&this_adm.copp.stat
2401 [port_idx][copp_idx]));
2402 goto fail_cmd;
2403 }
2404 atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
2405 return 0;
2406
2407fail_cmd:
2408
2409 return ret;
2410}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302411EXPORT_SYMBOL(adm_connect_afe_port);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302412
2413int adm_arrange_mch_map(struct adm_cmd_device_open_v5 *open, int path,
Rohit kumar2054b692019-02-01 18:01:12 +05302414 int channel_mode, int port_idx)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302415{
2416 int rc = 0, idx;
2417
Bhalchandra Gajareeed46bd2018-05-15 16:48:07 -07002418 pr_debug("%s: channel mode %d", __func__, channel_mode);
2419
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302420 memset(open->dev_channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
2421 switch (path) {
2422 case ADM_PATH_PLAYBACK:
2423 idx = ADM_MCH_MAP_IDX_PLAYBACK;
2424 break;
2425 case ADM_PATH_LIVE_REC:
2426 case ADM_PATH_NONLIVE_REC:
2427 idx = ADM_MCH_MAP_IDX_REC;
2428 break;
2429 default:
2430 goto non_mch_path;
2431 };
Rohit kumar2054b692019-02-01 18:01:12 +05302432
2433 if ((open->dev_num_channel > 2) &&
2434 (port_channel_map[port_idx].set_channel_map ||
2435 multi_ch_maps[idx].set_channel_map)) {
2436 if (port_channel_map[port_idx].set_channel_map)
2437 memcpy(open->dev_channel_mapping,
2438 port_channel_map[port_idx].channel_mapping,
2439 PCM_FORMAT_MAX_NUM_CHANNEL);
2440 else
2441 memcpy(open->dev_channel_mapping,
2442 multi_ch_maps[idx].channel_mapping,
2443 PCM_FORMAT_MAX_NUM_CHANNEL);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302444 } else {
2445 if (channel_mode == 1) {
2446 open->dev_channel_mapping[0] = PCM_CHANNEL_FC;
2447 } else if (channel_mode == 2) {
2448 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2449 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2450 } else if (channel_mode == 3) {
2451 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2452 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2453 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2454 } else if (channel_mode == 4) {
2455 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2456 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2457 open->dev_channel_mapping[2] = PCM_CHANNEL_LS;
2458 open->dev_channel_mapping[3] = PCM_CHANNEL_RS;
2459 } else if (channel_mode == 5) {
2460 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2461 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2462 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2463 open->dev_channel_mapping[3] = PCM_CHANNEL_LS;
2464 open->dev_channel_mapping[4] = PCM_CHANNEL_RS;
2465 } else if (channel_mode == 6) {
2466 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2467 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2468 open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2469 open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2470 open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2471 open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2472 } else if (channel_mode == 7) {
2473 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2474 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2475 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2476 open->dev_channel_mapping[3] = PCM_CHANNEL_LFE;
2477 open->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2478 open->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2479 open->dev_channel_mapping[6] = PCM_CHANNEL_CS;
2480 } else if (channel_mode == 8) {
2481 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2482 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2483 open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2484 open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2485 open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2486 open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2487 open->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2488 open->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2489 } else {
2490 pr_err("%s: invalid num_chan %d\n", __func__,
2491 channel_mode);
2492 rc = -EINVAL;
2493 goto inval_ch_mod;
2494 }
2495 }
2496
2497non_mch_path:
2498inval_ch_mod:
2499 return rc;
2500}
2501
2502int adm_arrange_mch_ep2_map(struct adm_cmd_device_open_v6 *open_v6,
2503 int channel_mode)
2504{
2505 int rc = 0;
2506
2507 memset(open_v6->dev_channel_mapping_eid2, 0,
2508 PCM_FORMAT_MAX_NUM_CHANNEL);
2509
2510 if (channel_mode == 1) {
2511 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FC;
2512 } else if (channel_mode == 2) {
2513 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2514 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2515 } else if (channel_mode == 3) {
2516 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2517 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2518 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
2519 } else if (channel_mode == 4) {
2520 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2521 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2522 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LS;
2523 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_RS;
2524 } else if (channel_mode == 5) {
2525 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2526 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2527 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
2528 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_LS;
2529 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_RS;
2530 } else if (channel_mode == 6) {
2531 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2532 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2533 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
2534 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
2535 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
2536 open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
2537 } else if (channel_mode == 8) {
2538 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2539 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2540 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
2541 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
2542 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
2543 open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
2544 open_v6->dev_channel_mapping_eid2[6] = PCM_CHANNEL_LB;
2545 open_v6->dev_channel_mapping_eid2[7] = PCM_CHANNEL_RB;
2546 } else {
2547 pr_err("%s: invalid num_chan %d\n", __func__,
2548 channel_mode);
2549 rc = -EINVAL;
2550 }
2551
2552 return rc;
2553}
2554
Dieter Luecking50c55352018-09-28 14:29:17 +02002555static int adm_arrange_mch_map_v8(
2556 struct adm_device_endpoint_payload *ep_payload,
Rohit kumar2054b692019-02-01 18:01:12 +05302557 int path, int channel_mode, int port_idx)
Dieter Luecking50c55352018-09-28 14:29:17 +02002558{
2559 int rc = 0, idx;
2560
2561 memset(ep_payload->dev_channel_mapping,
2562 0, PCM_FORMAT_MAX_NUM_CHANNEL_V8);
2563 switch (path) {
2564 case ADM_PATH_PLAYBACK:
2565 idx = ADM_MCH_MAP_IDX_PLAYBACK;
2566 break;
2567 case ADM_PATH_LIVE_REC:
2568 case ADM_PATH_NONLIVE_REC:
2569 idx = ADM_MCH_MAP_IDX_REC;
2570 break;
2571 default:
2572 goto non_mch_path;
2573 };
2574
2575 if ((ep_payload->dev_num_channel > 2) &&
Rohit kumar2054b692019-02-01 18:01:12 +05302576 (port_channel_map[port_idx].set_channel_map ||
2577 multi_ch_maps[idx].set_channel_map)) {
2578 if (port_channel_map[port_idx].set_channel_map)
2579 memcpy(ep_payload->dev_channel_mapping,
2580 port_channel_map[port_idx].channel_mapping,
2581 PCM_FORMAT_MAX_NUM_CHANNEL_V8);
2582 else
2583 memcpy(ep_payload->dev_channel_mapping,
2584 multi_ch_maps[idx].channel_mapping,
2585 PCM_FORMAT_MAX_NUM_CHANNEL_V8);
Dieter Luecking50c55352018-09-28 14:29:17 +02002586 } else {
2587 if (channel_mode == 1) {
2588 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FC;
2589 } else if (channel_mode == 2) {
2590 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2591 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2592 } else if (channel_mode == 3) {
2593 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2594 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2595 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2596 } else if (channel_mode == 4) {
2597 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2598 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2599 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LS;
2600 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_RS;
2601 } else if (channel_mode == 5) {
2602 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2603 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2604 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2605 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_LS;
2606 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_RS;
2607 } else if (channel_mode == 6) {
2608 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2609 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2610 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2611 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2612 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2613 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2614 } else if (channel_mode == 7) {
2615 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2616 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2617 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2618 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_LFE;
2619 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2620 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2621 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_CS;
2622 } else if (channel_mode == 8) {
2623 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2624 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2625 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2626 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2627 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2628 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2629 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2630 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2631 } else if (channel_mode == 10) {
2632 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2633 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2634 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2635 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2636 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2637 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2638 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LS;
2639 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RS;
2640 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
2641 ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
2642 } else if (channel_mode == 12) {
2643 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2644 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2645 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2646 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2647 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2648 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2649 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LS;
2650 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RS;
2651 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
2652 ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
2653 ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_TSL;
2654 ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_TSR;
Chaithanya Krishna Bacharaju3dd8fdf2019-05-02 12:14:37 +05302655 } else if (channel_mode == 14) {
2656 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2657 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2658 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2659 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2660 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2661 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2662 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LS;
2663 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RS;
2664 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
2665 ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
2666 ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_TSL;
2667 ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_TSR;
2668 ep_payload->dev_channel_mapping[12] = PCM_CHANNEL_FLC;
2669 ep_payload->dev_channel_mapping[13] = PCM_CHANNEL_FRC;
Dieter Luecking50c55352018-09-28 14:29:17 +02002670 } else if (channel_mode == 16) {
2671 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2672 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2673 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2674 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2675 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2676 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2677 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LS;
2678 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RS;
2679 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
2680 ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
2681 ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_TSL;
2682 ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_TSR;
2683 ep_payload->dev_channel_mapping[12] = PCM_CHANNEL_FLC;
2684 ep_payload->dev_channel_mapping[13] = PCM_CHANNEL_FRC;
2685 ep_payload->dev_channel_mapping[14] = PCM_CHANNEL_RLC;
2686 ep_payload->dev_channel_mapping[15] = PCM_CHANNEL_RRC;
2687 } else {
2688 pr_err("%s: invalid num_chan %d\n", __func__,
2689 channel_mode);
2690 rc = -EINVAL;
2691 goto inval_ch_mod;
2692 }
2693 }
2694
2695non_mch_path:
2696inval_ch_mod:
2697 return rc;
2698}
2699
2700static int adm_arrange_mch_ep2_map_v8(
2701 struct adm_device_endpoint_payload *ep_payload,
2702 int channel_mode)
2703{
2704 int rc = 0;
2705
2706 memset(ep_payload->dev_channel_mapping, 0,
2707 PCM_FORMAT_MAX_NUM_CHANNEL_V8);
2708
2709 if (channel_mode == 1) {
2710 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FC;
2711 } else if (channel_mode == 2) {
2712 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2713 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2714 } else if (channel_mode == 3) {
2715 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2716 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2717 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2718 } else if (channel_mode == 4) {
2719 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2720 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2721 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LS;
2722 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_RS;
2723 } else if (channel_mode == 5) {
2724 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2725 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2726 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2727 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_LS;
2728 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_RS;
2729 } else if (channel_mode == 6) {
2730 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2731 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2732 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2733 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2734 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2735 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2736 } else if (channel_mode == 8) {
2737 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2738 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2739 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2740 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2741 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2742 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2743 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2744 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2745 } else if (channel_mode == 10) {
2746 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2747 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2748 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2749 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2750 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2751 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2752 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2753 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2754 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_CS;
Ralf Herzfb81b6e2018-12-12 10:36:11 +01002755 ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TS;
Dieter Luecking50c55352018-09-28 14:29:17 +02002756 } else if (channel_mode == 12) {
2757 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2758 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2759 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2760 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2761 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2762 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2763 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2764 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2765 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
2766 ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
2767 ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_TSL;
2768 ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_TSR;
2769 } else if (channel_mode == 16) {
2770 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2771 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2772 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2773 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2774 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2775 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2776 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2777 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2778 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_CS;
Ralf Herzfb81b6e2018-12-12 10:36:11 +01002779 ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TS;
Dieter Luecking50c55352018-09-28 14:29:17 +02002780 ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_CVH;
2781 ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_MS;
2782 ep_payload->dev_channel_mapping[12] = PCM_CHANNEL_FLC;
2783 ep_payload->dev_channel_mapping[13] = PCM_CHANNEL_FRC;
2784 ep_payload->dev_channel_mapping[14] = PCM_CHANNEL_RLC;
2785 ep_payload->dev_channel_mapping[15] = PCM_CHANNEL_RRC;
2786 } else {
2787 pr_err("%s: invalid num_chan %d\n", __func__,
2788 channel_mode);
2789 rc = -EINVAL;
2790 }
2791
2792 return rc;
2793}
Dhananjay Kumar43034392019-01-16 21:57:58 +05302794
2795static int adm_copp_set_ec_ref_mfc_cfg(int port_id, int copp_idx,
2796 int sample_rate, int bps,
2797 int in_channels, int out_channels)
2798{
2799 struct audproc_mfc_param_media_fmt mfc_cfg;
2800 struct param_hdr_v3 param_hdr;
2801 u16 *chmixer_params = NULL;
2802 int rc = 0, i = 0, j = 0, param_index = 0, param_size = 0;
2803 struct adm_device_endpoint_payload ep_payload = {0, 0, 0, {0}};
2804
2805 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
2806 memset(&ep_payload, 0, sizeof(ep_payload));
2807 memset(&param_hdr, 0, sizeof(param_hdr));
2808
2809 param_hdr.module_id = AUDPROC_MODULE_ID_MFC_EC_REF;
2810 param_hdr.instance_id = INSTANCE_ID_0;
2811
2812 pr_debug("%s: port_id %d copp_idx %d SR %d, BW %d in_ch %d out_ch %d\n",
2813 __func__, port_id, copp_idx, sample_rate,
2814 bps, in_channels, out_channels);
2815
Dhananjay Kumar3d61b362019-03-01 15:47:56 +05302816 if (out_channels <= 0 || out_channels > AUDPROC_MFC_OUT_CHANNELS_MAX) {
2817 pr_err("%s: unsupported out channels=%d\n", __func__, out_channels);
2818 return -EINVAL;
2819 }
2820
Dhananjay Kumar43034392019-01-16 21:57:58 +05302821 /* 1. Update Media Format */
2822 param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
2823 param_hdr.param_size = sizeof(mfc_cfg);
2824
2825 mfc_cfg.sampling_rate = sample_rate;
2826 mfc_cfg.bits_per_sample = bps;
2827 mfc_cfg.num_channels = out_channels;
2828
2829 ep_payload.dev_num_channel = out_channels;
2830 rc = adm_arrange_mch_ep2_map_v8(&ep_payload, out_channels);
2831 if (rc < 0) {
2832 pr_err("%s: unable to get map for out channels=%d\n",
2833 __func__, out_channels);
2834 return -EINVAL;
2835 }
2836
2837 for (i = 0; i < out_channels; i++)
2838 mfc_cfg.channel_type[i] = (uint16_t) ep_payload.dev_channel_mapping[i];
2839
2840
2841 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx,
2842 param_hdr, (uint8_t *) &mfc_cfg);
2843 if (rc) {
2844 pr_err("%s: Failed to set media format, err %d\n", __func__, rc);
2845 return rc;
2846 }
2847
2848 /* 2. Send Channel Mixer params */
2849 param_size = 2 * (4 + out_channels + in_channels + (out_channels * in_channels));
2850 param_size = round_up(param_size, 4);
2851 param_hdr.param_id = DEFAULT_CHMIXER_PARAM_ID_COEFF;
2852 param_hdr.param_size = param_size;
2853
2854 pr_debug("%s: chmixer param sz = %d\n", __func__, param_size);
2855 chmixer_params = kzalloc(param_size, GFP_KERNEL);
2856 if (!chmixer_params) {
2857 return -ENOMEM;
2858 }
2859 param_index = 2; /* param[0] and [1] represents chmixer rule(always 0) */
2860 chmixer_params[param_index++] = out_channels;
2861 chmixer_params[param_index++] = in_channels;
2862
2863 /* output channel map is same as one set in media format */
2864 for (i = 0; i < out_channels; i++)
2865 chmixer_params[param_index++] = ep_payload.dev_channel_mapping[i];
2866
2867 /* input channel map should be same as one set for ep2 during copp open */
2868 ep_payload.dev_num_channel = in_channels;
2869 rc = adm_arrange_mch_ep2_map_v8(&ep_payload, in_channels);
2870 if (rc < 0) {
2871 pr_err("%s: unable to get in channal map\n", __func__);
2872 goto exit;
2873 }
2874 for (i = 0; i < in_channels; i++)
2875 chmixer_params[param_index++] = ep_payload.dev_channel_mapping[i];
2876
2877 for (i = 0; i < out_channels; i++)
2878 for (j = 0; j < in_channels; j++)
2879 chmixer_params[param_index++] = this_adm.ec_ref_chmixer_weights[i][j];
2880
2881 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx,
2882 param_hdr, (uint8_t *) chmixer_params);
2883 if (rc)
2884 pr_err("%s: Failed to set chmixer params, err %d\n", __func__, rc);
2885
2886exit:
2887 kfree(chmixer_params);
2888 return rc;
2889}
2890
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302891/**
2892 * adm_open -
2893 * command to send ADM open
2894 *
2895 * @port_id: port id number
2896 * @path: direction or ADM path type
2897 * @rate: sample rate of session
2898 * @channel_mode: number of channels set
2899 * @topology: topology active for this session
2900 * @perf_mode: performance mode like LL/ULL/..
2901 * @bit_width: bit width to set for copp
2902 * @app_type: App type used for this session
2903 * @acdb_id: ACDB ID of this device
Surendar Karkae7507512018-07-20 15:46:54 +05302904 * @session_type: type of session
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302905 *
2906 * Returns 0 on success or error on failure
2907 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302908int adm_open(int port_id, int path, int rate, int channel_mode, int topology,
Surendar Karkae7507512018-07-20 15:46:54 +05302909 int perf_mode, uint16_t bit_width, int app_type, int acdb_id,
2910 int session_type)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302911{
2912 struct adm_cmd_device_open_v5 open;
2913 struct adm_cmd_device_open_v6 open_v6;
Dieter Luecking50c55352018-09-28 14:29:17 +02002914 struct adm_cmd_device_open_v8 open_v8;
2915 struct adm_device_endpoint_payload ep1_payload;
2916 struct adm_device_endpoint_payload ep2_payload;
2917 int ep1_payload_size = 0;
2918 int ep2_payload_size = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302919 int ret = 0;
Asish Bhattacharya34504582017-08-08 12:55:01 +05302920 int port_idx, flags;
2921 int copp_idx = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302922 int tmp_port = q6audio_get_port_id(port_id);
Dieter Luecking50c55352018-09-28 14:29:17 +02002923 void *adm_params = NULL;
2924 int param_size;
Dhananjay Kumar43034392019-01-16 21:57:58 +05302925 int num_ec_ref_rx_chans = this_adm.num_ec_ref_rx_chans;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302926
2927 pr_debug("%s:port %#x path:%d rate:%d mode:%d perf_mode:%d,topo_id %d\n",
2928 __func__, port_id, path, rate, channel_mode, perf_mode,
2929 topology);
2930
2931 port_id = q6audio_convert_virtual_to_portid(port_id);
2932 port_idx = adm_validate_and_get_port_index(port_id);
2933 if (port_idx < 0) {
2934 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2935 return -EINVAL;
2936 }
Dieter Luecking50c55352018-09-28 14:29:17 +02002937 if (channel_mode < 0 || channel_mode > 32) {
2938 pr_err("%s: Invalid channel number 0x%x\n",
2939 __func__, channel_mode);
2940 return -EINVAL;
2941 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302942
2943 if (this_adm.apr == NULL) {
2944 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
2945 0xFFFFFFFF, &this_adm);
2946 if (this_adm.apr == NULL) {
2947 pr_err("%s: Unable to register ADM\n", __func__);
2948 return -ENODEV;
2949 }
2950 rtac_set_adm_handle(this_adm.apr);
2951 }
2952
2953 if (perf_mode == ULL_POST_PROCESSING_PCM_MODE) {
2954 flags = ADM_ULL_POST_PROCESSING_DEVICE_SESSION;
2955 if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
2956 (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
2957 (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
2958 topology = DEFAULT_COPP_TOPOLOGY;
2959 } else if (perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) {
2960 flags = ADM_ULTRA_LOW_LATENCY_DEVICE_SESSION;
2961 topology = NULL_COPP_TOPOLOGY;
2962 rate = ULL_SUPPORTED_SAMPLE_RATE;
2963 bit_width = ULL_SUPPORTED_BITS_PER_SAMPLE;
2964 } else if (perf_mode == LOW_LATENCY_PCM_MODE) {
2965 flags = ADM_LOW_LATENCY_DEVICE_SESSION;
2966 if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
2967 (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
2968 (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
2969 topology = DEFAULT_COPP_TOPOLOGY;
2970 } else {
2971 if ((path == ADM_PATH_COMPRESSED_RX) ||
2972 (path == ADM_PATH_COMPRESSED_TX))
2973 flags = 0;
2974 else
2975 flags = ADM_LEGACY_DEVICE_SESSION;
2976 }
2977
Laxminath Kasam8f7ccc22017-08-28 17:35:04 +05302978 if ((topology == VPM_TX_SM_ECNS_V2_COPP_TOPOLOGY) ||
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302979 (topology == VPM_TX_DM_FLUENCE_COPP_TOPOLOGY) ||
kunleiz1529b552019-01-24 17:15:49 +08002980 (topology == VPM_TX_DM_RFECNS_COPP_TOPOLOGY)||
2981 (topology == VPM_TX_DM_FLUENCE_EF_COPP_TOPOLOGY)) {
2982 if ((rate != ADM_CMD_COPP_OPEN_SAMPLE_RATE_8K) &&
2983 (rate != ADM_CMD_COPP_OPEN_SAMPLE_RATE_16K) &&
2984 (rate != ADM_CMD_COPP_OPEN_SAMPLE_RATE_32K) &&
2985 (rate != ADM_CMD_COPP_OPEN_SAMPLE_RATE_48K))
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302986 rate = 16000;
kunleiz1529b552019-01-24 17:15:49 +08002987 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302988
Xiaojun Sangdc9e8c22019-05-13 14:46:15 +08002989 if (topology == FFECNS_TOPOLOGY) {
2990 this_adm.ffecns_port_id = port_id;
2991 pr_debug("%s: ffecns port id =%x\n", __func__,
2992 this_adm.ffecns_port_id);
2993 }
2994
kunleizc0cd19e2018-10-18 17:11:35 +08002995 if (topology == VPM_TX_VOICE_SMECNS_V2_COPP_TOPOLOGY)
2996 channel_mode = 1;
2997
Asish Bhattacharya34504582017-08-08 12:55:01 +05302998 /*
2999 * Routing driver reuses the same adm for streams with the same
3000 * app_type, sample_rate etc.
3001 * This isn't allowed for ULL streams as per the DSP interface
3002 */
3003 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE)
3004 copp_idx = adm_get_idx_if_copp_exists(port_idx, topology,
3005 perf_mode,
3006 rate, bit_width,
Surendar Karkae7507512018-07-20 15:46:54 +05303007 app_type, session_type);
Asish Bhattacharya34504582017-08-08 12:55:01 +05303008
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303009 if (copp_idx < 0) {
3010 copp_idx = adm_get_next_available_copp(port_idx);
3011 if (copp_idx >= MAX_COPPS_PER_PORT) {
3012 pr_err("%s: exceeded copp id %d\n",
3013 __func__, copp_idx);
3014 return -EINVAL;
3015 }
3016 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
3017 atomic_set(&this_adm.copp.topology[port_idx][copp_idx],
3018 topology);
3019 atomic_set(&this_adm.copp.mode[port_idx][copp_idx],
3020 perf_mode);
3021 atomic_set(&this_adm.copp.rate[port_idx][copp_idx],
3022 rate);
3023 atomic_set(&this_adm.copp.channels[port_idx][copp_idx],
3024 channel_mode);
3025 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx],
3026 bit_width);
3027 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx],
3028 app_type);
3029 atomic_set(&this_adm.copp.acdb_id[port_idx][copp_idx],
3030 acdb_id);
Surendar Karkae7507512018-07-20 15:46:54 +05303031 atomic_set(&this_adm.copp.session_type[port_idx][copp_idx],
3032 session_type);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303033 set_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3034 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
3035 if ((path != ADM_PATH_COMPRESSED_RX) &&
3036 (path != ADM_PATH_COMPRESSED_TX))
3037 send_adm_custom_topology();
3038 }
3039
3040 if (this_adm.copp.adm_delay[port_idx][copp_idx] &&
3041 perf_mode == LEGACY_PCM_MODE) {
3042 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
3043 1);
3044 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
3045 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
3046 }
3047
3048 /* Create a COPP if port id are not enabled */
3049 if (atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]) == 0) {
3050 pr_debug("%s: open ADM: port_idx: %d, copp_idx: %d\n", __func__,
3051 port_idx, copp_idx);
Dieter Luecking50c55352018-09-28 14:29:17 +02003052 if ((topology == SRS_TRUMEDIA_TOPOLOGY_ID) &&
3053 perf_mode == LEGACY_PCM_MODE) {
3054 int res;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303055
Dieter Luecking50c55352018-09-28 14:29:17 +02003056 atomic_set(&this_adm.mem_map_index, ADM_SRS_TRUMEDIA);
3057 msm_dts_srs_tm_ion_memmap(&this_adm.outband_memmap);
3058 res = adm_memory_map_regions(
3059 &this_adm.outband_memmap.paddr, 0,
3060 (uint32_t *)&this_adm.outband_memmap.size, 1);
3061 if (res < 0) {
3062 pr_err("%s: SRS adm_memory_map_regions failed! addr = 0x%pK, size = %d\n",
3063 __func__,
3064 (void *)this_adm.outband_memmap.paddr,
3065 (uint32_t)this_adm.outband_memmap.size);
3066 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303067 }
3068
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303069
Mangesh Kunchamwar9f295c72018-10-08 18:20:41 +05303070 if ((q6core_get_avcs_api_version_per_service(
Dieter Luecking50c55352018-09-28 14:29:17 +02003071 APRV2_IDS_SERVICE_ID_ADSP_ADM_V) >=
Mangesh Kunchamwar9f295c72018-10-08 18:20:41 +05303072 ADSP_ADM_API_VERSION_V3) &&
3073 q6core_use_Q6_32ch_support()) {
Dieter Luecking50c55352018-09-28 14:29:17 +02003074 memset(&open_v8, 0, sizeof(open_v8));
3075 memset(&ep1_payload, 0, sizeof(ep1_payload));
3076 memset(&ep2_payload, 0, sizeof(ep2_payload));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303077
Dieter Luecking50c55352018-09-28 14:29:17 +02003078 open_v8.hdr.hdr_field = APR_HDR_FIELD(
3079 APR_MSG_TYPE_SEQ_CMD,
3080 APR_HDR_LEN(APR_HDR_SIZE),
3081 APR_PKT_VER);
3082 open_v8.hdr.src_svc = APR_SVC_ADM;
3083 open_v8.hdr.src_domain = APR_DOMAIN_APPS;
3084 open_v8.hdr.src_port = tmp_port;
3085 open_v8.hdr.dest_svc = APR_SVC_ADM;
3086 open_v8.hdr.dest_domain = APR_DOMAIN_ADSP;
3087 open_v8.hdr.dest_port = tmp_port;
3088 open_v8.hdr.token = port_idx << 16 | copp_idx;
3089 open_v8.hdr.opcode = ADM_CMD_DEVICE_OPEN_V8;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303090
Dieter Luecking50c55352018-09-28 14:29:17 +02003091 if (this_adm.native_mode != 0) {
3092 open_v8.flags = flags |
3093 (this_adm.native_mode << 11);
3094 this_adm.native_mode = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303095 } else {
Dieter Luecking50c55352018-09-28 14:29:17 +02003096 open_v8.flags = flags;
3097 }
3098 open_v8.mode_of_operation = path;
3099 open_v8.endpoint_id_1 = tmp_port;
3100 open_v8.endpoint_id_2 = 0xFFFF;
3101 open_v8.endpoint_id_3 = 0xFFFF;
3102
Chaithanya Krishna Bacharaju8b0abba2019-01-11 19:35:54 +05303103 if (((this_adm.ec_ref_rx & AFE_PORT_INVALID) !=
3104 AFE_PORT_INVALID) &&
3105 (path != ADM_PATH_PLAYBACK)) {
Surendar Karkade79a062018-12-05 14:18:55 +05303106 if (this_adm.num_ec_ref_rx_chans != 0) {
3107 open_v8.endpoint_id_2 =
3108 this_adm.ec_ref_rx;
Surendar Karkade79a062018-12-05 14:18:55 +05303109 } else {
3110 pr_err("%s: EC channels not set %d\n",
3111 __func__,
3112 this_adm.num_ec_ref_rx_chans);
3113 return -EINVAL;
3114 }
Dieter Luecking41620832018-11-14 15:11:47 +01003115 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303116
Dieter Luecking50c55352018-09-28 14:29:17 +02003117 open_v8.topology_id = topology;
3118 open_v8.reserved = 0;
3119
3120 /* variable endpoint payload */
3121 ep1_payload.dev_num_channel = channel_mode & 0x00FF;
3122 ep1_payload.bit_width = bit_width;
3123 ep1_payload.sample_rate = rate;
3124 ret = adm_arrange_mch_map_v8(&ep1_payload, path,
Rohit kumar2054b692019-02-01 18:01:12 +05303125 channel_mode, port_idx);
Dieter Luecking50c55352018-09-28 14:29:17 +02003126 if (ret)
3127 return ret;
3128
3129 pr_debug("%s: port_id=0x%x %x %x topology_id=0x%X flags %x ref_ch %x\n",
3130 __func__, open_v8.endpoint_id_1,
3131 open_v8.endpoint_id_2,
3132 open_v8.endpoint_id_3,
3133 open_v8.topology_id,
3134 open_v8.flags,
3135 this_adm.num_ec_ref_rx_chans);
3136
3137 ep1_payload_size = 8 +
3138 roundup(ep1_payload.dev_num_channel, 4);
3139 param_size = sizeof(struct adm_cmd_device_open_v8)
3140 + ep1_payload_size;
3141 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3142
Dieter Luecking27c57722018-10-05 16:45:41 +02003143 if ((this_adm.num_ec_ref_rx_chans != 0)
3144 && (path != ADM_PATH_PLAYBACK)
Dieter Luecking50c55352018-09-28 14:29:17 +02003145 && (open_v8.endpoint_id_2 != 0xFFFF)) {
3146 ep2_payload.dev_num_channel =
3147 this_adm.num_ec_ref_rx_chans;
Dieter Luecking50c55352018-09-28 14:29:17 +02003148
3149 if (this_adm.ec_ref_rx_bit_width != 0) {
3150 ep2_payload.bit_width =
3151 this_adm.ec_ref_rx_bit_width;
Dieter Luecking50c55352018-09-28 14:29:17 +02003152 } else {
3153 ep2_payload.bit_width = bit_width;
3154 }
3155
3156 if (this_adm.ec_ref_rx_sampling_rate != 0) {
3157 ep2_payload.sample_rate =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303158 this_adm.ec_ref_rx_sampling_rate;
Dieter Luecking50c55352018-09-28 14:29:17 +02003159 } else {
3160 ep2_payload.sample_rate = rate;
3161 }
3162
3163 pr_debug("%s: adm open_v8 eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
3164 __func__,
3165 ep2_payload.dev_num_channel,
3166 ep2_payload.bit_width,
3167 ep2_payload.sample_rate);
3168
3169 ret = adm_arrange_mch_ep2_map_v8(&ep2_payload,
3170 ep2_payload.dev_num_channel);
3171
3172 if (ret)
3173 return ret;
3174 ep2_payload_size = 8 +
3175 roundup(ep2_payload.dev_num_channel, 4);
3176 param_size += ep2_payload_size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303177 }
3178
Dieter Luecking27c57722018-10-05 16:45:41 +02003179 open_v8.hdr.pkt_size = param_size;
Dieter Luecking50c55352018-09-28 14:29:17 +02003180 adm_params = kzalloc(param_size, GFP_KERNEL);
3181 if (!adm_params)
3182 return -ENOMEM;
Dieter Luecking50c55352018-09-28 14:29:17 +02003183 memcpy(adm_params, &open_v8, sizeof(open_v8));
3184 memcpy(adm_params + sizeof(open_v8),
3185 (void *)&ep1_payload,
3186 ep1_payload_size);
Dieter Luecking27c57722018-10-05 16:45:41 +02003187
3188 if ((this_adm.num_ec_ref_rx_chans != 0)
3189 && (path != ADM_PATH_PLAYBACK)
3190 && (open_v8.endpoint_id_2 != 0xFFFF)) {
Dieter Luecking41620832018-11-14 15:11:47 +01003191 this_adm.num_ec_ref_rx_chans = 0;
Dieter Luecking27c57722018-10-05 16:45:41 +02003192 memcpy(adm_params + sizeof(open_v8)
3193 + ep1_payload_size,
3194 (void *)&ep2_payload,
3195 ep2_payload_size);
3196 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303197
Dieter Luecking50c55352018-09-28 14:29:17 +02003198 ret = apr_send_pkt(this_adm.apr,
3199 (uint32_t *)adm_params);
3200 if (ret < 0) {
3201 pr_err("%s: port_id: 0x%x for[0x%x] failed %d for open_v8\n",
3202 __func__, tmp_port, port_id, ret);
3203 return -EINVAL;
3204 }
3205 kfree(adm_params);
3206 } else {
3207
3208 open.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3209 APR_HDR_LEN(APR_HDR_SIZE),
3210 APR_PKT_VER);
3211 open.hdr.pkt_size = sizeof(open);
3212 open.hdr.src_svc = APR_SVC_ADM;
3213 open.hdr.src_domain = APR_DOMAIN_APPS;
3214 open.hdr.src_port = tmp_port;
3215 open.hdr.dest_svc = APR_SVC_ADM;
3216 open.hdr.dest_domain = APR_DOMAIN_ADSP;
3217 open.hdr.dest_port = tmp_port;
3218 open.hdr.token = port_idx << 16 | copp_idx;
3219 open.hdr.opcode = ADM_CMD_DEVICE_OPEN_V5;
3220 open.flags = flags;
3221 open.mode_of_operation = path;
3222 open.endpoint_id_1 = tmp_port;
3223 open.endpoint_id_2 = 0xFFFF;
3224
Surendar Karkae7507512018-07-20 15:46:54 +05303225 if (this_adm.ec_ref_rx && (path != 1) &&
3226 (afe_get_port_type(tmp_port) == MSM_AFE_PORT_TYPE_TX)) {
Dieter Luecking50c55352018-09-28 14:29:17 +02003227 open.endpoint_id_2 = this_adm.ec_ref_rx;
Dieter Luecking50c55352018-09-28 14:29:17 +02003228 }
3229
3230 open.topology_id = topology;
3231
3232 open.dev_num_channel = channel_mode & 0x00FF;
3233 open.bit_width = bit_width;
3234 WARN_ON((perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) &&
3235 (rate != ULL_SUPPORTED_SAMPLE_RATE));
3236 open.sample_rate = rate;
3237
Rohit kumar2054b692019-02-01 18:01:12 +05303238 ret = adm_arrange_mch_map(&open, path, channel_mode,
3239 port_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303240 if (ret)
3241 return ret;
3242
Dieter Luecking50c55352018-09-28 14:29:17 +02003243 pr_debug("%s: port_id=0x%x rate=%d topology_id=0x%X\n",
3244 __func__, open.endpoint_id_1, open.sample_rate,
3245 open.topology_id);
3246
3247 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3248
3249 if ((this_adm.num_ec_ref_rx_chans != 0) &&
3250 (path != 1) && (open.endpoint_id_2 != 0xFFFF)) {
3251 memset(&open_v6, 0,
3252 sizeof(struct adm_cmd_device_open_v6));
3253 memcpy(&open_v6, &open,
3254 sizeof(struct adm_cmd_device_open_v5));
3255 open_v6.hdr.opcode = ADM_CMD_DEVICE_OPEN_V6;
3256 open_v6.hdr.pkt_size = sizeof(open_v6);
3257 open_v6.dev_num_channel_eid2 =
3258 this_adm.num_ec_ref_rx_chans;
Dieter Luecking50c55352018-09-28 14:29:17 +02003259
3260 if (this_adm.ec_ref_rx_bit_width != 0) {
3261 open_v6.bit_width_eid2 =
3262 this_adm.ec_ref_rx_bit_width;
Dieter Luecking50c55352018-09-28 14:29:17 +02003263 } else {
3264 open_v6.bit_width_eid2 = bit_width;
3265 }
3266
3267 if (this_adm.ec_ref_rx_sampling_rate != 0) {
3268 open_v6.sample_rate_eid2 =
3269 this_adm.ec_ref_rx_sampling_rate;
Dieter Luecking50c55352018-09-28 14:29:17 +02003270 } else {
3271 open_v6.sample_rate_eid2 = rate;
3272 }
3273
3274 pr_debug("%s: eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
3275 __func__, open_v6.dev_num_channel_eid2,
3276 open_v6.bit_width_eid2,
3277 open_v6.sample_rate_eid2);
3278
3279 ret = adm_arrange_mch_ep2_map(&open_v6,
3280 open_v6.dev_num_channel_eid2);
3281
3282 if (ret)
3283 return ret;
3284
3285 ret = apr_send_pkt(this_adm.apr,
3286 (uint32_t *)&open_v6);
3287 } else {
3288 ret = apr_send_pkt(this_adm.apr,
3289 (uint32_t *)&open);
3290 }
3291 if (ret < 0) {
3292 pr_err("%s: port_id: 0x%x for[0x%x] failed %d\n",
3293 __func__, tmp_port, port_id, ret);
3294 return -EINVAL;
3295 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303296 }
Dieter Luecking50c55352018-09-28 14:29:17 +02003297
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303298 /* Wait for the callback with copp id */
3299 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3300 atomic_read(&this_adm.copp.stat
3301 [port_idx][copp_idx]) >= 0,
3302 msecs_to_jiffies(TIMEOUT_MS));
3303 if (!ret) {
3304 pr_err("%s: ADM open timedout for port_id: 0x%x for [0x%x]\n",
3305 __func__, tmp_port, port_id);
3306 return -EINVAL;
3307 } else if (atomic_read(&this_adm.copp.stat
3308 [port_idx][copp_idx]) > 0) {
3309 pr_err("%s: DSP returned error[%s]\n",
3310 __func__, adsp_err_get_err_str(
3311 atomic_read(&this_adm.copp.stat
3312 [port_idx][copp_idx])));
3313 return adsp_err_get_lnx_err_code(
3314 atomic_read(&this_adm.copp.stat
3315 [port_idx][copp_idx]));
3316 }
3317 }
3318 atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
Dhananjay Kumar43034392019-01-16 21:57:58 +05303319
3320 /*
3321 * Configure MFC(in ec_ref path) if chmixing param is applicable and set.
3322 * Except channels and channel maps the media format config for this module
3323 * should match with the COPP(EP1) config values.
3324 */
3325 if (path != ADM_PATH_PLAYBACK &&
3326 this_adm.num_ec_ref_rx_chans_downmixed != 0 &&
3327 num_ec_ref_rx_chans != this_adm.num_ec_ref_rx_chans_downmixed) {
3328 ret = adm_copp_set_ec_ref_mfc_cfg(port_id, copp_idx,
3329 rate, bit_width, num_ec_ref_rx_chans,
3330 this_adm.num_ec_ref_rx_chans_downmixed);
3331 this_adm.num_ec_ref_rx_chans_downmixed = 0;
3332 if (ret)
3333 pr_err("%s: set EC REF MFC cfg failed, err %d\n", __func__, ret);
3334 }
3335
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303336 return copp_idx;
3337}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303338EXPORT_SYMBOL(adm_open);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303339
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303340/**
3341 * adm_copp_mfc_cfg -
3342 * command to send ADM MFC config
3343 *
3344 * @port_id: Port ID number
3345 * @copp_idx: copp index assigned
3346 * @dst_sample_rate: sink sample rate
3347 *
3348 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303349void adm_copp_mfc_cfg(int port_id, int copp_idx, int dst_sample_rate)
3350{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003351 struct audproc_mfc_param_media_fmt mfc_cfg;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303352 struct adm_cmd_device_open_v5 open;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003353 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303354 int port_idx;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303355 int rc = 0;
3356 int i = 0;
3357
3358 port_id = q6audio_convert_virtual_to_portid(port_id);
3359 port_idx = adm_validate_and_get_port_index(port_id);
3360
3361 if (port_idx < 0) {
3362 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
3363 goto fail_cmd;
3364 }
3365
3366 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3367 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3368 goto fail_cmd;
3369 }
3370
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003371 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
3372 memset(&open, 0, sizeof(open));
3373 memset(&param_hdr, 0, sizeof(param_hdr));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303374
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003375 param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
3376 param_hdr.instance_id = INSTANCE_ID_0;
3377 param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
3378 param_hdr.param_size = sizeof(mfc_cfg);
3379
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303380 mfc_cfg.sampling_rate = dst_sample_rate;
3381 mfc_cfg.bits_per_sample =
3382 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
3383 open.dev_num_channel = mfc_cfg.num_channels =
3384 atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
3385
3386 rc = adm_arrange_mch_map(&open, ADM_PATH_PLAYBACK,
Rohit kumar2054b692019-02-01 18:01:12 +05303387 mfc_cfg.num_channels, port_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303388 if (rc < 0) {
3389 pr_err("%s: unable to get channal map\n", __func__);
3390 goto fail_cmd;
3391 }
3392
3393 for (i = 0; i < mfc_cfg.num_channels; i++)
3394 mfc_cfg.channel_type[i] =
3395 (uint16_t) open.dev_channel_mapping[i];
3396
3397 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3398
3399 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",
3400 __func__, port_idx, copp_idx,
3401 atomic_read(&this_adm.copp.rate[port_idx][copp_idx]),
3402 mfc_cfg.bits_per_sample, mfc_cfg.num_channels,
3403 mfc_cfg.sampling_rate);
3404
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003405 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
3406 (uint8_t *) &mfc_cfg);
3407 if (rc)
3408 pr_err("%s: Failed to set media format configuration data, err %d\n",
3409 __func__, rc);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303410
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303411fail_cmd:
3412 return;
3413}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303414EXPORT_SYMBOL(adm_copp_mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303415
3416static void route_set_opcode_matrix_id(
3417 struct adm_cmd_matrix_map_routings_v5 **route_addr,
3418 int path, uint32_t passthr_mode)
3419{
3420 struct adm_cmd_matrix_map_routings_v5 *route = *route_addr;
3421
3422 switch (path) {
3423 case ADM_PATH_PLAYBACK:
3424 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
3425 route->matrix_id = ADM_MATRIX_ID_AUDIO_RX;
3426 break;
3427 case ADM_PATH_LIVE_REC:
3428 if (passthr_mode == LISTEN) {
3429 route->hdr.opcode =
3430 ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3431 route->matrix_id = ADM_MATRIX_ID_LISTEN_TX;
3432 break;
3433 }
3434 /* fall through to set matrix id for non-listen case */
3435 case ADM_PATH_NONLIVE_REC:
3436 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
3437 route->matrix_id = ADM_MATRIX_ID_AUDIO_TX;
3438 break;
3439 case ADM_PATH_COMPRESSED_RX:
3440 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3441 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_RX;
3442 break;
3443 case ADM_PATH_COMPRESSED_TX:
3444 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3445 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_TX;
3446 break;
3447 default:
3448 pr_err("%s: Wrong path set[%d]\n", __func__, path);
3449 break;
3450 }
3451 pr_debug("%s: opcode 0x%x, matrix id %d\n",
3452 __func__, route->hdr.opcode, route->matrix_id);
3453}
3454
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303455/**
3456 * adm_matrix_map -
3457 * command to send ADM matrix map for ADM copp list
3458 *
3459 * @path: direction or ADM path type
3460 * @payload_map: have info of session id and associated copp_idx/num_copps
3461 * @perf_mode: performance mode like LL/ULL/..
3462 * @passthr_mode: flag to indicate passthrough mode
3463 *
3464 * Returns 0 on success or error on failure
3465 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303466int adm_matrix_map(int path, struct route_payload payload_map, int perf_mode,
3467 uint32_t passthr_mode)
3468{
3469 struct adm_cmd_matrix_map_routings_v5 *route;
3470 struct adm_session_map_node_v5 *node;
3471 uint16_t *copps_list;
3472 int cmd_size = 0;
3473 int ret = 0, i = 0;
3474 void *payload = NULL;
3475 void *matrix_map = NULL;
3476 int port_idx, copp_idx;
3477
3478 /* Assumes port_ids have already been validated during adm_open */
3479 cmd_size = (sizeof(struct adm_cmd_matrix_map_routings_v5) +
3480 sizeof(struct adm_session_map_node_v5) +
3481 (sizeof(uint32_t) * payload_map.num_copps));
3482 matrix_map = kzalloc(cmd_size, GFP_KERNEL);
3483 if (matrix_map == NULL) {
3484 pr_err("%s: Mem alloc failed\n", __func__);
3485 ret = -EINVAL;
3486 return ret;
3487 }
3488 route = (struct adm_cmd_matrix_map_routings_v5 *)matrix_map;
3489
3490 route->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3491 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
3492 route->hdr.pkt_size = cmd_size;
3493 route->hdr.src_svc = 0;
3494 route->hdr.src_domain = APR_DOMAIN_APPS;
3495 route->hdr.src_port = 0; /* Ignored */;
3496 route->hdr.dest_svc = APR_SVC_ADM;
3497 route->hdr.dest_domain = APR_DOMAIN_ADSP;
3498 route->hdr.dest_port = 0; /* Ignored */;
3499 route->hdr.token = 0;
3500 route->num_sessions = 1;
3501 route_set_opcode_matrix_id(&route, path, passthr_mode);
3502
3503 payload = ((u8 *)matrix_map +
3504 sizeof(struct adm_cmd_matrix_map_routings_v5));
3505 node = (struct adm_session_map_node_v5 *)payload;
3506
3507 node->session_id = payload_map.session_id;
3508 node->num_copps = payload_map.num_copps;
3509 payload = (u8 *)node + sizeof(struct adm_session_map_node_v5);
3510 copps_list = (uint16_t *)payload;
3511 for (i = 0; i < payload_map.num_copps; i++) {
3512 port_idx =
3513 adm_validate_and_get_port_index(payload_map.port_id[i]);
3514 if (port_idx < 0) {
3515 pr_err("%s: Invalid port_id 0x%x\n", __func__,
3516 payload_map.port_id[i]);
3517 ret = -EINVAL;
3518 goto fail_cmd;
3519 }
3520 copp_idx = payload_map.copp_idx[i];
3521 copps_list[i] = atomic_read(&this_adm.copp.id[port_idx]
3522 [copp_idx]);
3523 }
3524 atomic_set(&this_adm.matrix_map_stat, -1);
3525
3526 ret = apr_send_pkt(this_adm.apr, (uint32_t *)matrix_map);
3527 if (ret < 0) {
3528 pr_err("%s: routing for syream %d failed ret %d\n",
3529 __func__, payload_map.session_id, ret);
3530 ret = -EINVAL;
3531 goto fail_cmd;
3532 }
3533 ret = wait_event_timeout(this_adm.matrix_map_wait,
3534 atomic_read(&this_adm.matrix_map_stat) >= 0,
3535 msecs_to_jiffies(TIMEOUT_MS));
3536 if (!ret) {
3537 pr_err("%s: routing for syream %d failed\n", __func__,
3538 payload_map.session_id);
3539 ret = -EINVAL;
3540 goto fail_cmd;
3541 } else if (atomic_read(&this_adm.matrix_map_stat) > 0) {
3542 pr_err("%s: DSP returned error[%s]\n", __func__,
3543 adsp_err_get_err_str(atomic_read(
3544 &this_adm.matrix_map_stat)));
3545 ret = adsp_err_get_lnx_err_code(
3546 atomic_read(&this_adm.matrix_map_stat));
3547 goto fail_cmd;
3548 }
3549
3550 if ((perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) &&
3551 (path != ADM_PATH_COMPRESSED_RX)) {
3552 for (i = 0; i < payload_map.num_copps; i++) {
3553 port_idx = afe_get_port_index(payload_map.port_id[i]);
3554 copp_idx = payload_map.copp_idx[i];
3555 if (port_idx < 0 || copp_idx < 0 ||
3556 (copp_idx > MAX_COPPS_PER_PORT - 1)) {
3557 pr_err("%s: Invalid idx port_idx %d copp_idx %d\n",
3558 __func__, port_idx, copp_idx);
3559 continue;
3560 }
3561 rtac_add_adm_device(payload_map.port_id[i],
3562 atomic_read(&this_adm.copp.id
3563 [port_idx][copp_idx]),
3564 get_cal_path(path),
3565 payload_map.session_id,
3566 payload_map.app_type[i],
3567 payload_map.acdb_dev_id[i]);
3568
3569 if (!test_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3570 (void *)&this_adm.copp.adm_status[port_idx]
3571 [copp_idx])) {
3572 pr_debug("%s: adm copp[0x%x][%d] already sent",
3573 __func__, port_idx, copp_idx);
3574 continue;
3575 }
3576 send_adm_cal(payload_map.port_id[i], copp_idx,
3577 get_cal_path(path), perf_mode,
3578 payload_map.app_type[i],
3579 payload_map.acdb_dev_id[i],
Aditya Bavanari5106b562018-01-08 13:16:32 +05303580 payload_map.sample_rate[i],
3581 passthr_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303582 /* ADM COPP calibration is already sent */
3583 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3584 (void *)&this_adm.copp.
3585 adm_status[port_idx][copp_idx]);
3586 pr_debug("%s: copp_id: %d\n", __func__,
3587 atomic_read(&this_adm.copp.id[port_idx]
3588 [copp_idx]));
3589 }
3590 }
3591
3592fail_cmd:
3593 kfree(matrix_map);
3594 return ret;
3595}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303596EXPORT_SYMBOL(adm_matrix_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303597
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303598/**
3599 * adm_ec_ref_rx_id -
3600 * Update EC ref port ID
3601 *
3602 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303603void adm_ec_ref_rx_id(int port_id)
3604{
3605 this_adm.ec_ref_rx = port_id;
3606 pr_debug("%s: ec_ref_rx:%d\n", __func__, this_adm.ec_ref_rx);
3607}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303608EXPORT_SYMBOL(adm_ec_ref_rx_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303609
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303610/**
3611 * adm_num_ec_ref_rx_chans -
3612 * Update EC ref number of channels
3613 *
3614 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303615void adm_num_ec_ref_rx_chans(int num_chans)
3616{
3617 this_adm.num_ec_ref_rx_chans = num_chans;
3618 pr_debug("%s: num_ec_ref_rx_chans:%d\n",
3619 __func__, this_adm.num_ec_ref_rx_chans);
3620}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303621EXPORT_SYMBOL(adm_num_ec_ref_rx_chans);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303622
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303623/**
Dhananjay Kumar43034392019-01-16 21:57:58 +05303624 * adm_num_ec_rx_ref_chans_downmixed -
3625 * Update EC ref num of channels(downmixed) to be fed to EC algo
3626 *
3627 */
3628void adm_num_ec_ref_rx_chans_downmixed(int num_chans)
3629{
3630 this_adm.num_ec_ref_rx_chans_downmixed = num_chans;
3631 pr_debug("%s: num_ec_ref_rx_chans_downmixed:%d\n",
3632 __func__, this_adm.num_ec_ref_rx_chans_downmixed);
3633}
3634EXPORT_SYMBOL(adm_num_ec_ref_rx_chans_downmixed);
3635
3636/**
3637 * adm_ec_ref_chmixer_weights -
3638 * Update MFC(in ec ref) Channel Mixer Weights to be used
3639 * for downmixing rx channels before feeding them to EC algo
3640 * @out_channel_idx: index of output channel to which weightages are applicable
3641 * @weights: pointer to array having input weightages
3642 * @count: array sizeof pointer weights, max supported value is
3643 * PCM_FORMAT_MAX_NUM_CHANNEL_V8
3644 * Returns 0 on success or error on failure
3645 */
3646int adm_ec_ref_chmixer_weights(int out_channel_idx,
3647 uint16_t *weights, int count)
3648{
3649 int i = 0;
3650
3651 if (weights == NULL || count <= 0 || out_channel_idx < 0 ||
3652 count > PCM_FORMAT_MAX_NUM_CHANNEL_V8 ||
3653 out_channel_idx >= PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
3654 pr_err("%s: invalid weightages count(%d) ch_idx(%d)",
3655 __func__, count, out_channel_idx);
3656 return -EINVAL;
3657 }
3658
3659 for (i = 0; i < count; i++) {
3660 this_adm.ec_ref_chmixer_weights[out_channel_idx][i] = weights[i];
3661 pr_debug("%s: out ch idx :%d, weight[%d] = %d\n",
3662 __func__, out_channel_idx, i, weights[i]);
3663 }
3664
3665 return 0;
3666}
3667EXPORT_SYMBOL(adm_ec_ref_chmixer_weights);
3668
3669/**
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303670 * adm_ec_ref_rx_bit_width -
3671 * Update EC ref bit_width
3672 *
3673 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303674void adm_ec_ref_rx_bit_width(int bit_width)
3675{
3676 this_adm.ec_ref_rx_bit_width = bit_width;
3677 pr_debug("%s: ec_ref_rx_bit_width:%d\n",
3678 __func__, this_adm.ec_ref_rx_bit_width);
3679}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303680EXPORT_SYMBOL(adm_ec_ref_rx_bit_width);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303681
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303682/**
3683 * adm_ec_ref_rx_sampling_rate -
3684 * Update EC ref sample rate
3685 *
3686 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303687void adm_ec_ref_rx_sampling_rate(int sampling_rate)
3688{
3689 this_adm.ec_ref_rx_sampling_rate = sampling_rate;
3690 pr_debug("%s: ec_ref_rx_sampling_rate:%d\n",
3691 __func__, this_adm.ec_ref_rx_sampling_rate);
3692}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303693EXPORT_SYMBOL(adm_ec_ref_rx_sampling_rate);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303694
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303695/**
Dieter Luecking50c55352018-09-28 14:29:17 +02003696 * adm_set_native_mode -
3697 * Set adm channel native mode.
3698 * If enabled matrix mixer will be
3699 * running in native mode for channel
3700 * configuration for this device session.
3701 *
3702 */
3703void adm_set_native_mode(int mode)
3704{
3705 this_adm.native_mode = mode;
3706 pr_debug("%s: enable native_mode :%d\n",
3707 __func__, this_adm.native_mode);
3708}
3709EXPORT_SYMBOL(adm_set_native_mode);
3710
3711/**
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303712 * adm_close -
3713 * command to close ADM copp
3714 *
3715 * @port_id: Port ID number
3716 * @perf_mode: performance mode like LL/ULL/..
3717 * @copp_idx: copp index assigned
3718 *
3719 * Returns 0 on success or error on failure
3720 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303721int adm_close(int port_id, int perf_mode, int copp_idx)
3722{
3723 struct apr_hdr close;
3724
3725 int ret = 0, port_idx;
3726 int copp_id = RESET_COPP_ID;
3727
3728 pr_debug("%s: port_id=0x%x perf_mode: %d copp_idx: %d\n", __func__,
3729 port_id, perf_mode, copp_idx);
3730
3731 port_id = q6audio_convert_virtual_to_portid(port_id);
3732 port_idx = adm_validate_and_get_port_index(port_id);
3733 if (port_idx < 0) {
3734 pr_err("%s: Invalid port_id 0x%x\n",
3735 __func__, port_id);
3736 return -EINVAL;
3737 }
3738
3739 if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
3740 pr_err("%s: Invalid copp idx: %d\n", __func__, copp_idx);
3741 return -EINVAL;
3742 }
3743
Rohit kumar2054b692019-02-01 18:01:12 +05303744 port_channel_map[port_idx].set_channel_map = false;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303745 if (this_adm.copp.adm_delay[port_idx][copp_idx] && perf_mode
3746 == LEGACY_PCM_MODE) {
3747 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
3748 1);
3749 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
3750 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
3751 }
3752
3753 atomic_dec(&this_adm.copp.cnt[port_idx][copp_idx]);
3754 if (!(atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]))) {
3755 copp_id = adm_get_copp_id(port_idx, copp_idx);
3756 pr_debug("%s: Closing ADM port_idx:%d copp_idx:%d copp_id:0x%x\n",
3757 __func__, port_idx, copp_idx, copp_id);
3758 if ((!perf_mode) && (this_adm.outband_memmap.paddr != 0) &&
3759 (atomic_read(&this_adm.copp.topology[port_idx][copp_idx]) ==
3760 SRS_TRUMEDIA_TOPOLOGY_ID)) {
3761 atomic_set(&this_adm.mem_map_index,
3762 ADM_SRS_TRUMEDIA);
3763 ret = adm_memory_unmap_regions();
3764 if (ret < 0) {
3765 pr_err("%s: adm mem unmmap err %d",
3766 __func__, ret);
3767 } else {
3768 atomic_set(&this_adm.mem_map_handles
3769 [ADM_SRS_TRUMEDIA], 0);
3770 }
3771 }
3772
3773
3774 if ((afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX) &&
3775 this_adm.sourceTrackingData.memmap.paddr) {
3776 atomic_set(&this_adm.mem_map_index,
3777 ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
3778 ret = adm_memory_unmap_regions();
3779 if (ret < 0) {
3780 pr_err("%s: adm mem unmmap err %d",
3781 __func__, ret);
3782 }
3783 msm_audio_ion_free(
Banajit Goswami08bb7362017-11-03 22:48:23 -07003784 this_adm.sourceTrackingData.dma_buf);
3785 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303786 this_adm.sourceTrackingData.memmap.size = 0;
3787 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
3788 this_adm.sourceTrackingData.memmap.paddr = 0;
3789 this_adm.sourceTrackingData.apr_cmd_status = -1;
3790 atomic_set(&this_adm.mem_map_handles[
3791 ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
3792 }
3793
3794 close.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3795 APR_HDR_LEN(APR_HDR_SIZE),
3796 APR_PKT_VER);
3797 close.pkt_size = sizeof(close);
3798 close.src_svc = APR_SVC_ADM;
3799 close.src_domain = APR_DOMAIN_APPS;
3800 close.src_port = port_id;
3801 close.dest_svc = APR_SVC_ADM;
3802 close.dest_domain = APR_DOMAIN_ADSP;
3803 close.dest_port = copp_id;
3804 close.token = port_idx << 16 | copp_idx;
3805 close.opcode = ADM_CMD_DEVICE_CLOSE_V5;
3806
3807 atomic_set(&this_adm.copp.id[port_idx][copp_idx],
3808 RESET_COPP_ID);
3809 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
3810 atomic_set(&this_adm.copp.topology[port_idx][copp_idx], 0);
3811 atomic_set(&this_adm.copp.mode[port_idx][copp_idx], 0);
3812 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3813 atomic_set(&this_adm.copp.rate[port_idx][copp_idx], 0);
3814 atomic_set(&this_adm.copp.channels[port_idx][copp_idx], 0);
3815 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx], 0);
3816 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx], 0);
Surendar Karkae7507512018-07-20 15:46:54 +05303817 atomic_set(&this_adm.copp.session_type[port_idx][copp_idx], 0);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303818
3819 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3820 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
3821
3822 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&close);
3823 if (ret < 0) {
3824 pr_err("%s: ADM close failed %d\n", __func__, ret);
3825 return -EINVAL;
3826 }
3827
3828 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3829 atomic_read(&this_adm.copp.stat
3830 [port_idx][copp_idx]) >= 0,
3831 msecs_to_jiffies(TIMEOUT_MS));
3832 if (!ret) {
3833 pr_err("%s: ADM cmd Route timedout for port 0x%x\n",
3834 __func__, port_id);
3835 return -EINVAL;
3836 } else if (atomic_read(&this_adm.copp.stat
3837 [port_idx][copp_idx]) > 0) {
3838 pr_err("%s: DSP returned error[%s]\n",
3839 __func__, adsp_err_get_err_str(
3840 atomic_read(&this_adm.copp.stat
3841 [port_idx][copp_idx])));
3842 return adsp_err_get_lnx_err_code(
3843 atomic_read(&this_adm.copp.stat
3844 [port_idx][copp_idx]));
3845 }
3846 }
3847
3848 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) {
3849 pr_debug("%s: remove adm device from rtac\n", __func__);
3850 rtac_remove_adm_device(port_id, copp_id);
3851 }
Xiaojun Sangdc9e8c22019-05-13 14:46:15 +08003852
3853 if (port_id == this_adm.ffecns_port_id)
3854 this_adm.ffecns_port_id = -1;
3855
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303856 return 0;
3857}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303858EXPORT_SYMBOL(adm_close);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303859
3860int send_rtac_audvol_cal(void)
3861{
3862 int ret = 0;
3863 int ret2 = 0;
3864 int i = 0;
3865 int copp_idx, port_idx, acdb_id, app_id, path;
3866 struct cal_block_data *cal_block = NULL;
3867 struct audio_cal_info_audvol *audvol_cal_info = NULL;
3868 struct rtac_adm rtac_adm_data;
3869
3870 mutex_lock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3871
3872 cal_block = cal_utils_get_only_cal_block(
3873 this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]);
Vikram Panduranga770b8382017-09-27 12:17:36 -07003874 if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303875 pr_err("%s: can't find cal block!\n", __func__);
3876 goto unlock;
3877 }
3878
3879 audvol_cal_info = cal_block->cal_info;
3880 if (audvol_cal_info == NULL) {
3881 pr_err("%s: audvol_cal_info is NULL!\n", __func__);
3882 goto unlock;
3883 }
3884
3885 get_rtac_adm_data(&rtac_adm_data);
3886 for (; i < rtac_adm_data.num_of_dev; i++) {
3887
3888 acdb_id = rtac_adm_data.device[i].acdb_dev_id;
3889 if (acdb_id == 0)
3890 acdb_id = audvol_cal_info->acdb_id;
3891
3892 app_id = rtac_adm_data.device[i].app_type;
3893 if (app_id == 0)
3894 app_id = audvol_cal_info->app_type;
3895
3896 path = afe_get_port_type(rtac_adm_data.device[i].afe_port);
3897 if ((acdb_id == audvol_cal_info->acdb_id) &&
3898 (app_id == audvol_cal_info->app_type) &&
3899 (path == audvol_cal_info->path)) {
3900
3901 if (adm_get_indexes_from_copp_id(rtac_adm_data.
3902 device[i].copp, &copp_idx, &port_idx) != 0) {
3903 pr_debug("%s: Copp Id %d is not active\n",
3904 __func__,
3905 rtac_adm_data.device[i].copp);
3906 continue;
3907 }
3908
3909 ret2 = adm_remap_and_send_cal_block(ADM_RTAC_AUDVOL_CAL,
3910 rtac_adm_data.device[i].afe_port,
3911 copp_idx, cal_block,
3912 atomic_read(&this_adm.copp.
3913 mode[port_idx][copp_idx]),
3914 audvol_cal_info->app_type,
3915 audvol_cal_info->acdb_id,
3916 atomic_read(&this_adm.copp.
3917 rate[port_idx][copp_idx]));
3918 if (ret2 < 0) {
3919 pr_debug("%s: remap and send failed for copp Id %d, acdb id %d, app type %d, path %d\n",
3920 __func__, rtac_adm_data.device[i].copp,
3921 audvol_cal_info->acdb_id,
3922 audvol_cal_info->app_type,
3923 audvol_cal_info->path);
3924 ret = ret2;
3925 }
3926 }
3927 }
3928unlock:
3929 mutex_unlock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3930 return ret;
3931}
3932
3933int adm_map_rtac_block(struct rtac_cal_block_data *cal_block)
3934{
3935 int result = 0;
3936
3937 pr_debug("%s:\n", __func__);
3938
3939 if (cal_block == NULL) {
3940 pr_err("%s: cal_block is NULL!\n",
3941 __func__);
3942 result = -EINVAL;
3943 goto done;
3944 }
3945
3946 if (cal_block->cal_data.paddr == 0) {
3947 pr_debug("%s: No address to map!\n",
3948 __func__);
3949 result = -EINVAL;
3950 goto done;
3951 }
3952
3953 if (cal_block->map_data.map_size == 0) {
3954 pr_debug("%s: map size is 0!\n",
3955 __func__);
3956 result = -EINVAL;
3957 goto done;
3958 }
3959
3960 /* valid port ID needed for callback use primary I2S */
3961 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3962 result = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3963 &cal_block->map_data.map_size, 1);
3964 if (result < 0) {
3965 pr_err("%s: RTAC mmap did not work! size = %d result %d\n",
3966 __func__,
3967 cal_block->map_data.map_size, result);
3968 pr_debug("%s: RTAC mmap did not work! addr = 0x%pK, size = %d\n",
3969 __func__,
3970 &cal_block->cal_data.paddr,
3971 cal_block->map_data.map_size);
3972 goto done;
3973 }
3974
3975 cal_block->map_data.map_handle = atomic_read(
3976 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]);
3977done:
3978 return result;
3979}
3980
3981int adm_unmap_rtac_block(uint32_t *mem_map_handle)
3982{
3983 int result = 0;
3984
3985 pr_debug("%s:\n", __func__);
3986
3987 if (mem_map_handle == NULL) {
3988 pr_debug("%s: Map handle is NULL, nothing to unmap\n",
3989 __func__);
3990 goto done;
3991 }
3992
3993 if (*mem_map_handle == 0) {
3994 pr_debug("%s: Map handle is 0, nothing to unmap\n",
3995 __func__);
3996 goto done;
3997 }
3998
3999 if (*mem_map_handle != atomic_read(
4000 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL])) {
4001 pr_err("%s: Map handles do not match! Unmapping RTAC, RTAC map 0x%x, ADM map 0x%x\n",
4002 __func__, *mem_map_handle, atomic_read(
4003 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]));
4004
4005 /* if mismatch use handle passed in to unmap */
4006 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL],
4007 *mem_map_handle);
4008 }
4009
4010 /* valid port ID needed for callback use primary I2S */
4011 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
4012 result = adm_memory_unmap_regions();
4013 if (result < 0) {
4014 pr_debug("%s: adm_memory_unmap_regions failed, error %d\n",
4015 __func__, result);
4016 } else {
4017 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL], 0);
4018 *mem_map_handle = 0;
4019 }
4020done:
4021 return result;
4022}
4023
4024static int get_cal_type_index(int32_t cal_type)
4025{
4026 int ret = -EINVAL;
4027
4028 switch (cal_type) {
4029 case ADM_AUDPROC_CAL_TYPE:
4030 ret = ADM_AUDPROC_CAL;
4031 break;
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05304032 case ADM_LSM_AUDPROC_CAL_TYPE:
4033 ret = ADM_LSM_AUDPROC_CAL;
4034 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304035 case ADM_AUDVOL_CAL_TYPE:
4036 ret = ADM_AUDVOL_CAL;
4037 break;
4038 case ADM_CUST_TOPOLOGY_CAL_TYPE:
4039 ret = ADM_CUSTOM_TOP_CAL;
4040 break;
4041 case ADM_RTAC_INFO_CAL_TYPE:
4042 ret = ADM_RTAC_INFO_CAL;
4043 break;
4044 case ADM_RTAC_APR_CAL_TYPE:
4045 ret = ADM_RTAC_APR_CAL;
4046 break;
4047 case ADM_RTAC_AUDVOL_CAL_TYPE:
4048 ret = ADM_RTAC_AUDVOL_CAL;
4049 break;
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07004050 case ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE:
4051 ret = ADM_LSM_AUDPROC_PERSISTENT_CAL;
4052 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304053 default:
4054 pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
4055 }
4056 return ret;
4057}
4058
4059static int adm_alloc_cal(int32_t cal_type, size_t data_size, void *data)
4060{
4061 int ret = 0;
4062 int cal_index;
4063
4064 pr_debug("%s:\n", __func__);
4065
4066 cal_index = get_cal_type_index(cal_type);
4067 if (cal_index < 0) {
4068 pr_err("%s: could not get cal index %d!\n",
4069 __func__, cal_index);
4070 ret = -EINVAL;
4071 goto done;
4072 }
4073
4074 ret = cal_utils_alloc_cal(data_size, data,
4075 this_adm.cal_data[cal_index], 0, NULL);
4076 if (ret < 0) {
4077 pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
4078 __func__, ret, cal_type);
4079 ret = -EINVAL;
4080 goto done;
4081 }
4082done:
4083 return ret;
4084}
4085
4086static int adm_dealloc_cal(int32_t cal_type, size_t data_size, void *data)
4087{
4088 int ret = 0;
4089 int cal_index;
4090
4091 pr_debug("%s:\n", __func__);
4092
4093 cal_index = get_cal_type_index(cal_type);
4094 if (cal_index < 0) {
4095 pr_err("%s: could not get cal index %d!\n",
4096 __func__, cal_index);
4097 ret = -EINVAL;
4098 goto done;
4099 }
4100
4101 ret = cal_utils_dealloc_cal(data_size, data,
4102 this_adm.cal_data[cal_index]);
4103 if (ret < 0) {
4104 pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
4105 __func__, ret, cal_type);
4106 ret = -EINVAL;
4107 goto done;
4108 }
4109done:
4110 return ret;
4111}
4112
4113static int adm_set_cal(int32_t cal_type, size_t data_size, void *data)
4114{
4115 int ret = 0;
4116 int cal_index;
4117
4118 pr_debug("%s:\n", __func__);
4119
4120 cal_index = get_cal_type_index(cal_type);
4121 if (cal_index < 0) {
4122 pr_err("%s: could not get cal index %d!\n",
4123 __func__, cal_index);
4124 ret = -EINVAL;
4125 goto done;
4126 }
4127
4128 ret = cal_utils_set_cal(data_size, data,
4129 this_adm.cal_data[cal_index], 0, NULL);
4130 if (ret < 0) {
4131 pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
4132 __func__, ret, cal_type);
4133 ret = -EINVAL;
4134 goto done;
4135 }
4136
4137 if (cal_index == ADM_CUSTOM_TOP_CAL) {
4138 mutex_lock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
4139 this_adm.set_custom_topology = 1;
4140 mutex_unlock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
4141 } else if (cal_index == ADM_RTAC_AUDVOL_CAL) {
4142 send_rtac_audvol_cal();
4143 }
4144done:
4145 return ret;
4146}
4147
4148static int adm_map_cal_data(int32_t cal_type,
4149 struct cal_block_data *cal_block)
4150{
4151 int ret = 0;
4152 int cal_index;
4153
4154 pr_debug("%s:\n", __func__);
4155
4156 cal_index = get_cal_type_index(cal_type);
4157 if (cal_index < 0) {
4158 pr_err("%s: could not get cal index %d!\n",
4159 __func__, cal_index);
4160 ret = -EINVAL;
4161 goto done;
4162 }
4163
4164 atomic_set(&this_adm.mem_map_index, cal_index);
4165 ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
4166 (uint32_t *)&cal_block->map_data.map_size, 1);
4167 if (ret < 0) {
4168 pr_err("%s: map did not work! cal_type %i ret %d\n",
4169 __func__, cal_index, ret);
4170 ret = -ENODEV;
4171 goto done;
4172 }
4173 cal_block->map_data.q6map_handle = atomic_read(&this_adm.
4174 mem_map_handles[cal_index]);
4175done:
4176 return ret;
4177}
4178
4179static int adm_unmap_cal_data(int32_t cal_type,
4180 struct cal_block_data *cal_block)
4181{
4182 int ret = 0;
4183 int cal_index;
4184
4185 pr_debug("%s:\n", __func__);
4186
4187 cal_index = get_cal_type_index(cal_type);
4188 if (cal_index < 0) {
4189 pr_err("%s: could not get cal index %d!\n",
4190 __func__, cal_index);
4191 ret = -EINVAL;
4192 goto done;
4193 }
4194
4195 if (cal_block == NULL) {
4196 pr_err("%s: Cal block is NULL!\n",
4197 __func__);
4198 goto done;
4199 }
4200
4201 if (cal_block->map_data.q6map_handle == 0) {
4202 pr_err("%s: Map handle is NULL, nothing to unmap\n",
4203 __func__);
4204 goto done;
4205 }
4206
4207 atomic_set(&this_adm.mem_map_handles[cal_index],
4208 cal_block->map_data.q6map_handle);
4209 atomic_set(&this_adm.mem_map_index, cal_index);
4210 ret = adm_memory_unmap_regions();
4211 if (ret < 0) {
4212 pr_err("%s: unmap did not work! cal_type %i ret %d\n",
4213 __func__, cal_index, ret);
4214 ret = -ENODEV;
4215 goto done;
4216 }
4217 cal_block->map_data.q6map_handle = 0;
4218done:
4219 return ret;
4220}
4221
4222static void adm_delete_cal_data(void)
4223{
4224 pr_debug("%s:\n", __func__);
4225
4226 cal_utils_destroy_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data);
4227}
4228
4229static int adm_init_cal_data(void)
4230{
4231 int ret = 0;
4232 struct cal_type_info cal_type_info[] = {
4233 {{ADM_CUST_TOPOLOGY_CAL_TYPE,
4234 {adm_alloc_cal, adm_dealloc_cal, NULL,
4235 adm_set_cal, NULL, NULL} },
4236 {adm_map_cal_data, adm_unmap_cal_data,
4237 cal_utils_match_buf_num} },
4238
4239 {{ADM_AUDPROC_CAL_TYPE,
4240 {adm_alloc_cal, adm_dealloc_cal, NULL,
4241 adm_set_cal, NULL, NULL} },
4242 {adm_map_cal_data, adm_unmap_cal_data,
4243 cal_utils_match_buf_num} },
4244
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05304245 {{ADM_LSM_AUDPROC_CAL_TYPE,
4246 {adm_alloc_cal, adm_dealloc_cal, NULL,
4247 adm_set_cal, NULL, NULL} },
4248 {adm_map_cal_data, adm_unmap_cal_data,
4249 cal_utils_match_buf_num} },
4250
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304251 {{ADM_AUDVOL_CAL_TYPE,
4252 {adm_alloc_cal, adm_dealloc_cal, NULL,
4253 adm_set_cal, NULL, NULL} },
4254 {adm_map_cal_data, adm_unmap_cal_data,
4255 cal_utils_match_buf_num} },
4256
4257 {{ADM_RTAC_INFO_CAL_TYPE,
4258 {NULL, NULL, NULL, NULL, NULL, NULL} },
4259 {NULL, NULL, cal_utils_match_buf_num} },
4260
4261 {{ADM_RTAC_APR_CAL_TYPE,
4262 {NULL, NULL, NULL, NULL, NULL, NULL} },
4263 {NULL, NULL, cal_utils_match_buf_num} },
4264
4265 {{SRS_TRUMEDIA_CAL_TYPE,
4266 {NULL, NULL, NULL, NULL, NULL, NULL} },
4267 {NULL, NULL, cal_utils_match_buf_num} },
4268
4269 {{ADM_RTAC_AUDVOL_CAL_TYPE,
4270 {adm_alloc_cal, adm_dealloc_cal, NULL,
4271 adm_set_cal, NULL, NULL} },
4272 {adm_map_cal_data, adm_unmap_cal_data,
4273 cal_utils_match_buf_num} },
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07004274
4275 {{ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE,
4276 {adm_alloc_cal, adm_dealloc_cal, NULL,
4277 adm_set_cal, NULL, NULL} },
4278 {adm_map_cal_data, adm_unmap_cal_data,
4279 cal_utils_match_buf_num} },
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304280 };
4281 pr_debug("%s:\n", __func__);
4282
4283 ret = cal_utils_create_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data,
4284 cal_type_info);
4285 if (ret < 0) {
4286 pr_err("%s: could not create cal type! ret %d\n",
4287 __func__, ret);
4288 ret = -EINVAL;
4289 goto err;
4290 }
4291
4292 return ret;
4293err:
4294 adm_delete_cal_data();
4295 return ret;
4296}
4297
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304298/**
4299 * adm_set_volume -
4300 * command to set volume on ADM copp
4301 *
4302 * @port_id: Port ID number
4303 * @copp_idx: copp index assigned
4304 * @volume: gain value to set
4305 *
4306 * Returns 0 on success or error on failure
4307 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304308int adm_set_volume(int port_id, int copp_idx, int volume)
4309{
4310 struct audproc_volume_ctrl_master_gain audproc_vol;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004311 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304312 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304313
4314 pr_debug("%s: port_id %d, volume %d\n", __func__, port_id, volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304315
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004316 memset(&audproc_vol, 0, sizeof(audproc_vol));
4317 memset(&param_hdr, 0, sizeof(param_hdr));
4318 param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
4319 param_hdr.instance_id = INSTANCE_ID_0;
4320 param_hdr.param_id = AUDPROC_PARAM_ID_VOL_CTRL_MASTER_GAIN;
4321 param_hdr.param_size = sizeof(audproc_vol);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304322
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304323 audproc_vol.master_gain = volume;
4324
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004325 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4326 (uint8_t *) &audproc_vol);
4327 if (rc)
4328 pr_err("%s: Failed to set volume, err %d\n", __func__, rc);
4329
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304330 return rc;
4331}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304332EXPORT_SYMBOL(adm_set_volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304333
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304334/**
4335 * adm_set_softvolume -
4336 * command to set softvolume
4337 *
4338 * @port_id: Port ID number
4339 * @copp_idx: copp index assigned
4340 * @softvol_param: Params to set for softvolume
4341 *
4342 * Returns 0 on success or error on failure
4343 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304344int adm_set_softvolume(int port_id, int copp_idx,
4345 struct audproc_softvolume_params *softvol_param)
4346{
4347 struct audproc_soft_step_volume_params audproc_softvol;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004348 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304349 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304350
4351 pr_debug("%s: period %d step %d curve %d\n", __func__,
4352 softvol_param->period, softvol_param->step,
4353 softvol_param->rampingcurve);
4354
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004355 memset(&audproc_softvol, 0, sizeof(audproc_softvol));
4356 memset(&param_hdr, 0, sizeof(param_hdr));
4357 param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
4358 param_hdr.instance_id = INSTANCE_ID_0;
4359 param_hdr.param_id = AUDPROC_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
4360 param_hdr.param_size = sizeof(audproc_softvol);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304361
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304362 audproc_softvol.period = softvol_param->period;
4363 audproc_softvol.step = softvol_param->step;
4364 audproc_softvol.ramping_curve = softvol_param->rampingcurve;
4365
4366 pr_debug("%s: period %d, step %d, curve %d\n", __func__,
4367 audproc_softvol.period, audproc_softvol.step,
4368 audproc_softvol.ramping_curve);
4369
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004370 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4371 (uint8_t *) &audproc_softvol);
4372 if (rc)
4373 pr_err("%s: Failed to set soft volume, err %d\n", __func__, rc);
4374
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304375 return rc;
4376}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304377EXPORT_SYMBOL(adm_set_softvolume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304378
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304379/**
4380 * adm_set_mic_gain -
4381 * command to set MIC gain
4382 *
4383 * @port_id: Port ID number
4384 * @copp_idx: copp index assigned
4385 * @volume: gain value to set
4386 *
4387 * Returns 0 on success or error on failure
4388 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304389int adm_set_mic_gain(int port_id, int copp_idx, int volume)
4390{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004391 struct admx_mic_gain mic_gain_params;
4392 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304393 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304394
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004395 pr_debug("%s: Setting mic gain to %d at port_id 0x%x\n", __func__,
4396 volume, port_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304397
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004398 memset(&mic_gain_params, 0, sizeof(mic_gain_params));
4399 memset(&param_hdr, 0, sizeof(param_hdr));
4400 param_hdr.module_id = ADM_MODULE_IDX_MIC_GAIN_CTRL;
4401 param_hdr.instance_id = INSTANCE_ID_0;
4402 param_hdr.param_id = ADM_PARAM_IDX_MIC_GAIN;
4403 param_hdr.param_size = sizeof(mic_gain_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304404
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004405 mic_gain_params.tx_mic_gain = volume;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304406
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004407 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4408 (uint8_t *) &mic_gain_params);
4409 if (rc)
4410 pr_err("%s: Failed to set mic gain, err %d\n", __func__, rc);
4411
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304412 return rc;
4413}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304414EXPORT_SYMBOL(adm_set_mic_gain);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304415
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304416/**
4417 * adm_send_set_multichannel_ec_primary_mic_ch -
4418 * command to set multi-ch EC primary mic
4419 *
4420 * @port_id: Port ID number
4421 * @copp_idx: copp index assigned
4422 * @primary_mic_ch: channel number of primary mic
4423 *
4424 * Returns 0 on success or error on failure
4425 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304426int adm_send_set_multichannel_ec_primary_mic_ch(int port_id, int copp_idx,
4427 int primary_mic_ch)
4428{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004429 struct admx_sec_primary_mic_ch sec_primary_ch_params;
4430 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304431 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304432
4433 pr_debug("%s port_id 0x%x, copp_idx 0x%x, primary_mic_ch %d\n",
4434 __func__, port_id, copp_idx, primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304435
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004436 memset(&sec_primary_ch_params, 0, sizeof(sec_primary_ch_params));
4437 memset(&param_hdr, 0, sizeof(param_hdr));
4438 param_hdr.module_id = AUDPROC_MODULE_ID_VOICE_TX_SECNS;
4439 param_hdr.instance_id = INSTANCE_ID_0;
4440 param_hdr.param_id = AUDPROC_PARAM_IDX_SEC_PRIMARY_MIC_CH;
4441 param_hdr.param_size = sizeof(sec_primary_ch_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304442
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004443 sec_primary_ch_params.version = 0;
4444 sec_primary_ch_params.sec_primary_mic_ch = primary_mic_ch;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304445
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004446 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4447 (uint8_t *) &sec_primary_ch_params);
4448 if (rc)
4449 pr_err("%s: Failed to set primary mic chanel, err %d\n",
4450 __func__, rc);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304451
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304452 return rc;
4453}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304454EXPORT_SYMBOL(adm_send_set_multichannel_ec_primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304455
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304456/**
Xiaojun Sangdc9e8c22019-05-13 14:46:15 +08004457 * adm_set_ffecns_effect -
4458 * command to set effect for ffecns module
4459 *
4460 * @effect: effect payload
4461 *
4462 * Returns 0 on success or error on failure
4463 */
4464int adm_set_ffecns_effect(int effect)
4465{
4466 struct ffecns_effect ffecns_params;
4467 struct param_hdr_v3 param_hdr;
4468 int rc = 0;
4469 int copp_idx = 0;
4470
4471 copp_idx = adm_get_default_copp_idx(this_adm.ffecns_port_id);
4472 if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
4473 pr_err("%s, no active copp to query rms copp_idx:%d\n",
4474 __func__, copp_idx);
4475 return -EINVAL;
4476 }
4477
4478 memset(&ffecns_params, 0, sizeof(ffecns_params));
4479 memset(&param_hdr, 0, sizeof(param_hdr));
4480
4481 param_hdr.module_id = FFECNS_MODULE_ID;
4482 param_hdr.instance_id = INSTANCE_ID_0;
4483 param_hdr.param_id = FLUENCE_CMN_GLOBAL_EFFECT_PARAM_ID;
4484 param_hdr.param_size = sizeof(ffecns_params);
4485
4486 ffecns_params.payload = effect;
4487
4488 rc = adm_pack_and_set_one_pp_param(this_adm.ffecns_port_id, copp_idx,
4489 param_hdr, (uint8_t *) &ffecns_params);
4490 if (rc)
4491 pr_err("%s: Failed to set ffecns effect, err %d\n",
4492 __func__, rc);
4493
4494 return rc;
4495}
4496EXPORT_SYMBOL(adm_set_ffecns_effect);
4497
4498/**
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304499 * adm_param_enable -
4500 * command to send params to ADM for given module
4501 *
4502 * @port_id: Port ID number
4503 * @copp_idx: copp index assigned
4504 * @module_id: ADM module
4505 * @enable: flag to enable or disable module
4506 *
4507 * Returns 0 on success or error on failure
4508 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304509int adm_param_enable(int port_id, int copp_idx, int module_id, int enable)
4510{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004511 struct module_instance_info mod_inst_info;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304512
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004513 memset(&mod_inst_info, 0, sizeof(mod_inst_info));
4514 mod_inst_info.module_id = module_id;
4515 mod_inst_info.instance_id = INSTANCE_ID_0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304516
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004517 return adm_param_enable_v2(port_id, copp_idx, mod_inst_info, enable);
4518}
Vidyakumar Athota98464a22018-03-15 20:39:37 -07004519EXPORT_SYMBOL(adm_param_enable);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004520
4521/**
4522 * adm_param_enable_v2 -
4523 * command to send params to ADM for given module
4524 *
4525 * @port_id: Port ID number
4526 * @copp_idx: copp index assigned
4527 * @mod_inst_info: module and instance ID info
4528 * @enable: flag to enable or disable module
4529 *
4530 * Returns 0 on success or error on failure
4531 */
4532int adm_param_enable_v2(int port_id, int copp_idx,
4533 struct module_instance_info mod_inst_info, int enable)
4534{
4535 uint32_t enable_param;
4536 struct param_hdr_v3 param_hdr;
4537 int rc = 0;
4538
4539 if (enable < 0 || enable > 1) {
4540 pr_err("%s: Invalid value for enable %d\n", __func__, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304541 return -EINVAL;
4542 }
4543
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004544 pr_debug("%s port_id %d, module_id 0x%x, instance_id 0x%x, enable %d\n",
4545 __func__, port_id, mod_inst_info.module_id,
4546 mod_inst_info.instance_id, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304547
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004548 memset(&param_hdr, 0, sizeof(param_hdr));
4549 param_hdr.module_id = mod_inst_info.module_id;
4550 param_hdr.instance_id = mod_inst_info.instance_id;
4551 param_hdr.param_id = AUDPROC_PARAM_ID_ENABLE;
4552 param_hdr.param_size = sizeof(enable_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304553
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004554 enable_param = enable;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304555
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004556 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4557 (uint8_t *) &enable_param);
4558 if (rc)
4559 pr_err("%s: Failed to set enable of module(%d) instance(%d) to %d, err %d\n",
4560 __func__, mod_inst_info.module_id,
4561 mod_inst_info.instance_id, enable, rc);
4562
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304563 return rc;
4564
4565}
Vidyakumar Athota98464a22018-03-15 20:39:37 -07004566EXPORT_SYMBOL(adm_param_enable_v2);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304567
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304568/**
4569 * adm_send_calibration -
4570 * send ADM calibration to DSP
4571 *
4572 * @port_id: Port ID number
4573 * @copp_idx: copp index assigned
4574 * @path: direction or ADM path type
4575 * @perf_mode: performance mode like LL/ULL/..
4576 * @cal_type: calibration type to use
4577 * @params: pointer with cal data
4578 * @size: cal size
4579 *
4580 * Returns 0 on success or error on failure
4581 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304582int adm_send_calibration(int port_id, int copp_idx, int path, int perf_mode,
4583 int cal_type, char *params, int size)
4584{
4585
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004586 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304587
4588 pr_debug("%s:port_id %d, path %d, perf_mode %d, cal_type %d, size %d\n",
4589 __func__, port_id, path, perf_mode, cal_type, size);
4590
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304591 /* Maps audio_dev_ctrl path definition to ACDB definition */
4592 if (get_cal_path(path) != RX_DEVICE) {
4593 pr_err("%s: acdb_path %d\n", __func__, path);
4594 rc = -EINVAL;
4595 goto end;
4596 }
4597
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004598 rc = adm_set_pp_params(port_id, copp_idx, NULL, (u8 *) params, size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304599
4600end:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304601 return rc;
4602}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304603EXPORT_SYMBOL(adm_send_calibration);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304604
4605/*
4606 * adm_update_wait_parameters must be called with routing driver locks.
4607 * adm_reset_wait_parameters must be called with routing driver locks.
4608 * set and reset parmeters are separated to make sure it is always called
4609 * under routing driver lock.
4610 * adm_wait_timeout is to block until timeout or interrupted. Timeout is
4611 * not a an error.
4612 */
4613int adm_set_wait_parameters(int port_id, int copp_idx)
4614{
4615
4616 int ret = 0, port_idx;
4617
4618 pr_debug("%s: port_id 0x%x, copp_idx %d\n", __func__, port_id,
4619 copp_idx);
4620 port_id = afe_convert_virtual_to_portid(port_id);
4621 port_idx = adm_validate_and_get_port_index(port_id);
4622 if (port_idx < 0) {
4623 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4624 ret = -EINVAL;
4625 goto end;
4626 }
4627
4628 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4629 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4630 return -EINVAL;
4631 }
4632
4633 this_adm.copp.adm_delay[port_idx][copp_idx] = 1;
4634 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 0);
4635
4636end:
4637 return ret;
4638
4639}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304640EXPORT_SYMBOL(adm_set_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304641
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304642/**
4643 * adm_reset_wait_parameters -
4644 * reset wait parameters or ADM delay value
4645 *
4646 * @port_id: Port ID number
4647 * @copp_idx: copp index assigned
4648 *
4649 * Returns 0 on success or error on failure
4650 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304651int adm_reset_wait_parameters(int port_id, int copp_idx)
4652{
4653 int ret = 0, port_idx;
4654
4655 pr_debug("%s: port_id 0x%x copp_idx %d\n", __func__, port_id,
4656 copp_idx);
4657 port_id = afe_convert_virtual_to_portid(port_id);
4658 port_idx = adm_validate_and_get_port_index(port_id);
4659 if (port_idx < 0) {
4660 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4661 ret = -EINVAL;
4662 goto end;
4663 }
4664
4665 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4666 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4667 return -EINVAL;
4668 }
4669
4670 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 1);
4671 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
4672
4673end:
4674 return ret;
4675}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304676EXPORT_SYMBOL(adm_reset_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304677
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304678/**
4679 * adm_wait_timeout -
4680 * ADM wait command after command send to DSP
4681 *
4682 * @port_id: Port ID number
4683 * @copp_idx: copp index assigned
4684 * @wait_time: value in ms for command timeout
4685 *
4686 * Returns 0 on success or error on failure
4687 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304688int adm_wait_timeout(int port_id, int copp_idx, int wait_time)
4689{
4690 int ret = 0, port_idx;
4691
4692 pr_debug("%s: port_id 0x%x, copp_idx %d, wait_time %d\n", __func__,
4693 port_id, copp_idx, wait_time);
4694 port_id = afe_convert_virtual_to_portid(port_id);
4695 port_idx = adm_validate_and_get_port_index(port_id);
4696 if (port_idx < 0) {
4697 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4698 ret = -EINVAL;
4699 goto end;
4700 }
4701
4702 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4703 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4704 return -EINVAL;
4705 }
4706
4707 ret = wait_event_timeout(
4708 this_adm.copp.adm_delay_wait[port_idx][copp_idx],
4709 atomic_read(&this_adm.copp.adm_delay_stat[port_idx][copp_idx]),
4710 msecs_to_jiffies(wait_time));
4711 pr_debug("%s: return %d\n", __func__, ret);
4712 if (ret != 0)
4713 ret = -EINTR;
4714end:
4715 pr_debug("%s: return %d--\n", __func__, ret);
4716 return ret;
4717}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304718EXPORT_SYMBOL(adm_wait_timeout);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304719
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304720/**
4721 * adm_store_cal_data -
4722 * Retrieve calibration data for ADM copp device
4723 *
4724 * @port_id: Port ID number
4725 * @copp_idx: copp index assigned
4726 * @path: direction or copp type
4727 * @perf_mode: performance mode like LL/ULL/..
4728 * @cal_index: calibration index to use
4729 * @params: pointer to store cal data
4730 * @size: pointer to fill with cal size
4731 *
4732 * Returns 0 on success or error on failure
4733 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304734int adm_store_cal_data(int port_id, int copp_idx, int path, int perf_mode,
4735 int cal_index, char *params, int *size)
4736{
4737 int rc = 0;
4738 struct cal_block_data *cal_block = NULL;
4739 int app_type, acdb_id, port_idx, sample_rate;
4740
4741 if (this_adm.cal_data[cal_index] == NULL) {
4742 pr_debug("%s: cal_index %d not allocated!\n",
4743 __func__, cal_index);
4744 goto end;
4745 }
4746
4747 if (get_cal_path(path) != RX_DEVICE) {
4748 pr_debug("%s: Invalid path to store calibration %d\n",
4749 __func__, path);
4750 rc = -EINVAL;
4751 goto end;
4752 }
4753
4754 port_id = afe_convert_virtual_to_portid(port_id);
4755 port_idx = adm_validate_and_get_port_index(port_id);
4756 if (port_idx < 0) {
4757 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
4758 rc = -EINVAL;
4759 goto end;
4760 }
4761
4762 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4763 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4764 return -EINVAL;
4765 }
4766
4767 acdb_id = atomic_read(&this_adm.copp.acdb_id[port_idx][copp_idx]);
4768 app_type = atomic_read(&this_adm.copp.app_type[port_idx][copp_idx]);
4769 sample_rate = atomic_read(&this_adm.copp.rate[port_idx][copp_idx]);
4770
4771 mutex_lock(&this_adm.cal_data[cal_index]->lock);
4772 cal_block = adm_find_cal(cal_index, get_cal_path(path), app_type,
4773 acdb_id, sample_rate);
4774 if (cal_block == NULL)
4775 goto unlock;
4776
4777 if (cal_block->cal_data.size <= 0) {
4778 pr_debug("%s: No ADM cal send for port_id = 0x%x!\n",
4779 __func__, port_id);
4780 rc = -EINVAL;
4781 goto unlock;
4782 }
4783
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05304784 if (cal_index == ADM_AUDPROC_CAL || cal_index == ADM_LSM_AUDPROC_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304785 if (cal_block->cal_data.size > AUD_PROC_BLOCK_SIZE) {
4786 pr_err("%s:audproc:invalid size exp/actual[%zd, %d]\n",
4787 __func__, cal_block->cal_data.size, *size);
4788 rc = -ENOMEM;
4789 goto unlock;
4790 }
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07004791 } else if (cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
4792 if (cal_block->cal_data.size > AUD_PROC_PERSIST_BLOCK_SIZE) {
4793 pr_err("%s:persist invalid size exp/actual[%zd, %d]\n",
4794 __func__, cal_block->cal_data.size, *size);
4795 rc = -ENOMEM;
4796 goto unlock;
4797 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304798 } else if (cal_index == ADM_AUDVOL_CAL) {
4799 if (cal_block->cal_data.size > AUD_VOL_BLOCK_SIZE) {
4800 pr_err("%s:aud_vol:invalid size exp/actual[%zd, %d]\n",
4801 __func__, cal_block->cal_data.size, *size);
4802 rc = -ENOMEM;
4803 goto unlock;
4804 }
4805 } else {
4806 pr_debug("%s: Not valid calibration for dolby topolgy\n",
4807 __func__);
4808 rc = -EINVAL;
4809 goto unlock;
4810 }
4811 memcpy(params, cal_block->cal_data.kvaddr, cal_block->cal_data.size);
4812 *size = cal_block->cal_data.size;
4813
4814 pr_debug("%s:port_id %d, copp_idx %d, path %d",
4815 __func__, port_id, copp_idx, path);
4816 pr_debug("perf_mode %d, cal_type %d, size %d\n",
4817 perf_mode, cal_index, *size);
4818
4819unlock:
4820 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
4821end:
4822 return rc;
4823}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304824EXPORT_SYMBOL(adm_store_cal_data);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304825
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304826/**
4827 * adm_send_compressed_device_mute -
4828 * command to send mute for compressed device
4829 *
4830 * @port_id: Port ID number
4831 * @copp_idx: copp index assigned
4832 * @mute_on: flag to indicate mute or unmute
4833 *
4834 * Returns 0 on success or error on failure
4835 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304836int adm_send_compressed_device_mute(int port_id, int copp_idx, bool mute_on)
4837{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004838 u32 mute_param = mute_on ? 1 : 0;
4839 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304840 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304841
4842 pr_debug("%s port_id: 0x%x, copp_idx %d, mute_on: %d\n",
4843 __func__, port_id, copp_idx, mute_on);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304844
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004845 memset(&param_hdr, 0, sizeof(param_hdr));
4846 param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_MUTE;
4847 param_hdr.instance_id = INSTANCE_ID_0;
4848 param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_MUTE;
4849 param_hdr.param_size = sizeof(mute_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304850
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004851 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4852 (uint8_t *) &mute_param);
4853 if (ret)
4854 pr_err("%s: Failed to set mute, err %d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304855
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304856 return ret;
4857}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304858EXPORT_SYMBOL(adm_send_compressed_device_mute);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304859
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304860/**
4861 * adm_send_compressed_device_latency -
4862 * command to send latency for compressed device
4863 *
4864 * @port_id: Port ID number
4865 * @copp_idx: copp index assigned
4866 * @latency: latency value to pass
4867 *
4868 * Returns 0 on success or error on failure
4869 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304870int adm_send_compressed_device_latency(int port_id, int copp_idx, int latency)
4871{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004872 u32 latency_param;
4873 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304874 int ret = 0;
4875
4876 pr_debug("%s port_id: 0x%x, copp_idx %d latency: %d\n", __func__,
4877 port_id, copp_idx, latency);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004878
4879 if (latency < 0) {
4880 pr_err("%s: Invalid value for latency %d", __func__, latency);
4881 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304882 }
4883
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004884 memset(&param_hdr, 0, sizeof(param_hdr));
4885 param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_LATENCY;
4886 param_hdr.instance_id = INSTANCE_ID_0;
4887 param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_LATENCY;
4888 param_hdr.param_size = sizeof(latency_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304889
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004890 latency_param = latency;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304891
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004892 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4893 (uint8_t *) &latency_param);
4894 if (ret)
4895 pr_err("%s: Failed to set latency, err %d\n", __func__, ret);
4896
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304897 return ret;
4898}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304899EXPORT_SYMBOL(adm_send_compressed_device_latency);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304900
4901/**
4902 * adm_swap_speaker_channels
4903 *
4904 * Receives port_id, copp_idx, sample rate, spk_swap and
4905 * send MFC command to swap speaker channel.
4906 * Return zero on success. On failure returns nonzero.
4907 *
4908 * port_id - Passed value, port_id for which channels swap is wanted
4909 * copp_idx - Passed value, copp_idx for which channels swap is wanted
4910 * sample_rate - Passed value, sample rate used by app type config
4911 * spk_swap - Passed value, spk_swap for check if swap flag is set
4912 */
4913int adm_swap_speaker_channels(int port_id, int copp_idx,
4914 int sample_rate, bool spk_swap)
4915{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004916 struct audproc_mfc_param_media_fmt mfc_cfg;
4917 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304918 uint16_t num_channels;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004919 int port_idx = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304920 int ret = 0;
4921
4922 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4923 __func__, port_id, copp_idx);
4924 port_id = q6audio_convert_virtual_to_portid(port_id);
4925 port_idx = adm_validate_and_get_port_index(port_id);
4926 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
4927 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004928 return -EINVAL;
4929 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4930 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
4931 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304932 }
4933
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004934 num_channels = atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304935 if (num_channels != 2) {
4936 pr_debug("%s: Invalid number of channels: %d\n",
4937 __func__, num_channels);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004938 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304939 }
4940
4941 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004942 memset(&param_hdr, 0, sizeof(param_hdr));
4943
4944 param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
4945 param_hdr.instance_id = INSTANCE_ID_0;
4946 param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
4947 param_hdr.param_size = sizeof(mfc_cfg);
4948
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304949 mfc_cfg.sampling_rate = sample_rate;
4950 mfc_cfg.bits_per_sample =
4951 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
4952 mfc_cfg.num_channels = num_channels;
4953
4954 /* Currently applying speaker swap for only 2 channel use case */
4955 if (spk_swap) {
4956 mfc_cfg.channel_type[0] =
4957 (uint16_t) PCM_CHANNEL_FR;
4958 mfc_cfg.channel_type[1] =
4959 (uint16_t) PCM_CHANNEL_FL;
4960 } else {
4961 mfc_cfg.channel_type[0] =
4962 (uint16_t) PCM_CHANNEL_FL;
4963 mfc_cfg.channel_type[1] =
4964 (uint16_t) PCM_CHANNEL_FR;
4965 }
4966
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004967 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4968 (u8 *) &mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304969 if (ret < 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004970 pr_err("%s: Failed to set swap speaker channels on port[0x%x] failed %d\n",
4971 __func__, port_id, ret);
4972 return ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304973 }
4974
4975 pr_debug("%s: mfc_cfg Set params returned success", __func__);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004976 return 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304977}
4978EXPORT_SYMBOL(adm_swap_speaker_channels);
4979
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304980/**
4981 * adm_set_sound_focus -
4982 * Update sound focus info
4983 *
4984 * @port_id: Port ID number
4985 * @copp_idx: copp index assigned
4986 * @soundFocusData: sound focus data to pass
4987 *
4988 * Returns 0 on success or error on failure
4989 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304990int adm_set_sound_focus(int port_id, int copp_idx,
4991 struct sound_focus_param soundFocusData)
4992{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004993 struct adm_param_fluence_soundfocus_t soundfocus_params;
4994 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304995 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304996 int i;
4997
4998 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4999 __func__, port_id, copp_idx);
5000
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005001 memset(&param_hdr, 0, sizeof(param_hdr));
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07005002 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005003 param_hdr.instance_id = INSTANCE_ID_0;
5004 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
5005 param_hdr.param_size = sizeof(soundfocus_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305006
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005007 memset(&(soundfocus_params), 0xFF, sizeof(soundfocus_params));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305008 for (i = 0; i < MAX_SECTORS; i++) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005009 soundfocus_params.start_angles[i] =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305010 soundFocusData.start_angle[i];
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005011 soundfocus_params.enables[i] = soundFocusData.enable[i];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305012 pr_debug("%s: start_angle[%d] = %d\n",
5013 __func__, i, soundFocusData.start_angle[i]);
5014 pr_debug("%s: enable[%d] = %d\n",
5015 __func__, i, soundFocusData.enable[i]);
5016 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005017 soundfocus_params.gain_step = soundFocusData.gain_step;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305018 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData.gain_step);
5019
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005020 soundfocus_params.reserved = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305021
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005022 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
5023 (uint8_t *) &soundfocus_params);
5024 if (ret)
5025 pr_err("%s: Failed to set sound focus params, err %d\n",
5026 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305027
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305028 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
5029
5030 return ret;
5031}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305032EXPORT_SYMBOL(adm_set_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305033
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305034/**
5035 * adm_get_sound_focus -
5036 * Retrieve sound focus info
5037 *
5038 * @port_id: Port ID number
5039 * @copp_idx: copp index assigned
5040 * @soundFocusData: pointer for sound focus data to be updated with
5041 *
5042 * Returns 0 on success or error on failure
5043 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305044int adm_get_sound_focus(int port_id, int copp_idx,
5045 struct sound_focus_param *soundFocusData)
5046{
5047 int ret = 0, i;
5048 char *params_value;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005049 uint32_t max_param_size = 0;
5050 struct adm_param_fluence_soundfocus_t *soundfocus_params = NULL;
5051 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305052
5053 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
5054 __func__, port_id, copp_idx);
5055
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005056 max_param_size = sizeof(struct adm_param_fluence_soundfocus_t) +
5057 sizeof(union param_hdrs);
5058 params_value = kzalloc(max_param_size, GFP_KERNEL);
5059 if (!params_value)
5060 return -ENOMEM;
5061
5062 memset(&param_hdr, 0, sizeof(param_hdr));
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07005063 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005064 param_hdr.instance_id = INSTANCE_ID_0;
5065 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
5066 param_hdr.param_size = max_param_size;
5067 ret = adm_get_pp_params(port_id, copp_idx,
5068 ADM_CLIENT_ID_SOURCE_TRACKING, NULL, &param_hdr,
5069 params_value);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305070 if (ret) {
5071 pr_err("%s: get parameters failed ret:%d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305072 ret = -EINVAL;
5073 goto done;
5074 }
5075
5076 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
5077 pr_err("%s - get params returned error [%s]\n",
5078 __func__, adsp_err_get_err_str(
5079 this_adm.sourceTrackingData.apr_cmd_status));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305080 ret = adsp_err_get_lnx_err_code(
5081 this_adm.sourceTrackingData.apr_cmd_status);
5082 goto done;
5083 }
5084
5085 soundfocus_params = (struct adm_param_fluence_soundfocus_t *)
5086 params_value;
5087 for (i = 0; i < MAX_SECTORS; i++) {
5088 soundFocusData->start_angle[i] =
5089 soundfocus_params->start_angles[i];
5090 soundFocusData->enable[i] = soundfocus_params->enables[i];
5091 pr_debug("%s: start_angle[%d] = %d\n",
5092 __func__, i, soundFocusData->start_angle[i]);
5093 pr_debug("%s: enable[%d] = %d\n",
5094 __func__, i, soundFocusData->enable[i]);
5095 }
5096 soundFocusData->gain_step = soundfocus_params->gain_step;
5097 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData->gain_step);
5098
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305099done:
5100 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
5101
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005102 kfree(params_value);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305103 return ret;
5104}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305105EXPORT_SYMBOL(adm_get_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305106
5107static int adm_source_tracking_alloc_map_memory(void)
5108{
5109 int ret;
5110
5111 pr_debug("%s: Enter\n", __func__);
5112
Banajit Goswami08bb7362017-11-03 22:48:23 -07005113 ret = msm_audio_ion_alloc(&this_adm.sourceTrackingData.dma_buf,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305114 AUD_PROC_BLOCK_SIZE,
5115 &this_adm.sourceTrackingData.memmap.paddr,
5116 &this_adm.sourceTrackingData.memmap.size,
5117 &this_adm.sourceTrackingData.memmap.kvaddr);
5118 if (ret) {
5119 pr_err("%s: failed to allocate memory\n", __func__);
5120
5121 ret = -EINVAL;
5122 goto done;
5123 }
5124
5125 atomic_set(&this_adm.mem_map_index, ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
5126 ret = adm_memory_map_regions(&this_adm.sourceTrackingData.memmap.paddr,
5127 0,
5128 (uint32_t *)&this_adm.sourceTrackingData.memmap.size,
5129 1);
5130 if (ret < 0) {
5131 pr_err("%s: failed to map memory, paddr = 0x%pK, size = %d\n",
5132 __func__,
5133 (void *)this_adm.sourceTrackingData.memmap.paddr,
5134 (uint32_t)this_adm.sourceTrackingData.memmap.size);
5135
Banajit Goswami08bb7362017-11-03 22:48:23 -07005136 msm_audio_ion_free(this_adm.sourceTrackingData.dma_buf);
5137 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305138 this_adm.sourceTrackingData.memmap.size = 0;
5139 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
5140 this_adm.sourceTrackingData.memmap.paddr = 0;
5141 this_adm.sourceTrackingData.apr_cmd_status = -1;
5142 atomic_set(&this_adm.mem_map_handles
5143 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
5144
5145 ret = -EINVAL;
5146 goto done;
5147 }
5148 ret = 0;
5149 pr_debug("%s: paddr = 0x%pK, size = %d, mem_map_handle = 0x%x\n",
5150 __func__, (void *)this_adm.sourceTrackingData.memmap.paddr,
5151 (uint32_t)this_adm.sourceTrackingData.memmap.size,
5152 atomic_read(&this_adm.mem_map_handles
5153 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING]));
5154
5155done:
5156 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
5157
5158 return ret;
5159}
5160
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305161/**
5162 * adm_get_source_tracking -
5163 * Retrieve source tracking info
5164 *
5165 * @port_id: Port ID number
5166 * @copp_idx: copp index assigned
5167 * @sourceTrackingData: pointer for source track data to be updated with
5168 *
5169 * Returns 0 on success or error on failure
5170 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305171int adm_get_source_tracking(int port_id, int copp_idx,
5172 struct source_tracking_param *sourceTrackingData)
5173{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005174 struct adm_param_fluence_sourcetracking_t *source_tracking_params =
5175 NULL;
5176 struct mem_mapping_hdr mem_hdr;
5177 struct param_hdr_v3 param_hdr;
5178 int i = 0;
5179 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305180
5181 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
5182 __func__, port_id, copp_idx);
5183
5184 if (!this_adm.sourceTrackingData.memmap.paddr) {
5185 /* Allocate and map shared memory for out of band usage */
5186 ret = adm_source_tracking_alloc_map_memory();
5187 if (ret != 0) {
5188 ret = -EINVAL;
5189 goto done;
5190 }
5191 }
5192
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005193 memset(&mem_hdr, 0, sizeof(mem_hdr));
5194 memset(&param_hdr, 0, sizeof(param_hdr));
5195 mem_hdr.data_payload_addr_lsw =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305196 lower_32_bits(this_adm.sourceTrackingData.memmap.paddr);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005197 mem_hdr.data_payload_addr_msw = msm_audio_populate_upper_32_bits(
5198 this_adm.sourceTrackingData.memmap.paddr);
5199 mem_hdr.mem_map_handle = atomic_read(
5200 &this_adm.mem_map_handles[ADM_MEM_MAP_INDEX_SOURCE_TRACKING]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305201
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07005202 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005203 param_hdr.instance_id = INSTANCE_ID_0;
5204 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOURCETRACKING;
5205 /*
5206 * This size should be the max size of the calibration data + header.
5207 * Use the union size to ensure max size is used.
5208 */
5209 param_hdr.param_size =
5210 sizeof(struct adm_param_fluence_sourcetracking_t) +
5211 sizeof(union param_hdrs);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305212
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005213 /*
5214 * Retrieving parameters out of band, so no need to provide a buffer for
5215 * the returned parameter data as it will be at the memory location
5216 * provided.
5217 */
5218 ret = adm_get_pp_params(port_id, copp_idx,
5219 ADM_CLIENT_ID_SOURCE_TRACKING, &mem_hdr,
5220 &param_hdr, NULL);
5221 if (ret) {
5222 pr_err("%s: Failed to get params, error %d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305223 goto done;
5224 }
5225
5226 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
5227 pr_err("%s - get params returned error [%s]\n",
5228 __func__, adsp_err_get_err_str(
5229 this_adm.sourceTrackingData.apr_cmd_status));
5230
5231 ret = adsp_err_get_lnx_err_code(
5232 this_adm.sourceTrackingData.apr_cmd_status);
5233 goto done;
5234 }
5235
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08005236 /* How do we know what the param data was retrieved with for hdr size */
5237 source_tracking_params =
5238 (struct adm_param_fluence_sourcetracking_t
5239 *) (this_adm.sourceTrackingData.memmap.kvaddr +
5240 sizeof(struct param_hdr_v1));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305241 for (i = 0; i < MAX_SECTORS; i++) {
5242 sourceTrackingData->vad[i] = source_tracking_params->vad[i];
5243 pr_debug("%s: vad[%d] = %d\n",
5244 __func__, i, sourceTrackingData->vad[i]);
5245 }
5246 sourceTrackingData->doa_speech = source_tracking_params->doa_speech;
5247 pr_debug("%s: doa_speech = %d\n",
5248 __func__, sourceTrackingData->doa_speech);
5249
5250 for (i = 0; i < MAX_NOISE_SOURCE_INDICATORS; i++) {
5251 sourceTrackingData->doa_noise[i] =
5252 source_tracking_params->doa_noise[i];
5253 pr_debug("%s: doa_noise[%d] = %d\n",
5254 __func__, i, sourceTrackingData->doa_noise[i]);
5255 }
5256 for (i = 0; i < MAX_POLAR_ACTIVITY_INDICATORS; i++) {
5257 sourceTrackingData->polar_activity[i] =
5258 source_tracking_params->polar_activity[i];
5259 pr_debug("%s: polar_activity[%d] = %d\n",
5260 __func__, i, sourceTrackingData->polar_activity[i]);
5261 }
5262
5263 ret = 0;
5264
5265done:
5266 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
5267
5268 return ret;
5269}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305270EXPORT_SYMBOL(adm_get_source_tracking);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305271
Chaithanya Krishna Bacharaju8c6a1842018-11-16 15:02:39 +05305272/**
5273 * adm_get_doa_tracking_mon -
5274 * Retrieve doa tracking monitor info
5275 *
5276 * @port_id: Port ID number
5277 * @copp_idx: copp index assigned
5278 * @doa_tracking_data: pointer for doa data to be updated with
5279 *
5280 * Returns 0 on success or error on failure
5281 */
5282int adm_get_doa_tracking_mon(int port_id, int copp_idx,
5283 struct doa_tracking_mon_param *doa_tracking_data)
5284{
5285 int ret = 0, i;
5286 char *params_value;
5287 uint32_t max_param_size = 0;
5288 struct adm_param_doa_tracking_mon_t *doa_tracking_params = NULL;
5289 struct param_hdr_v3 param_hdr;
5290
5291 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
5292 __func__, port_id, copp_idx);
5293
5294 if (doa_tracking_data == NULL) {
5295 pr_err("%s: Received NULL pointer for doa tracking data\n",
5296 __func__);
5297 return -EINVAL;
5298 }
5299
5300 max_param_size = sizeof(struct adm_param_doa_tracking_mon_t) +
5301 sizeof(union param_hdrs);
5302 params_value = kzalloc(max_param_size, GFP_KERNEL);
5303 if (!params_value)
5304 return -ENOMEM;
5305
5306 memset(&param_hdr, 0, sizeof(param_hdr));
5307 param_hdr.module_id = AUDPROC_MODULE_ID_FFECNS;
5308 param_hdr.instance_id = INSTANCE_ID_0;
5309 param_hdr.param_id = AUDPROC_PARAM_ID_FFV_DOA_TRACKING_MONITOR;
5310 param_hdr.param_size = max_param_size;
5311 ret = adm_get_pp_params(port_id, copp_idx,
5312 ADM_CLIENT_ID_DEFAULT, NULL, &param_hdr,
5313 params_value);
5314 if (ret) {
5315 pr_err("%s: get parameters failed ret:%d\n", __func__, ret);
5316 goto done;
5317 }
5318
5319 doa_tracking_params =
5320 (struct adm_param_doa_tracking_mon_t *)params_value;
5321 for (i = 0; i < MAX_DOA_TRACKING_ANGLES; i++) {
5322 doa_tracking_data->target_angle_L16[i] =
5323 doa_tracking_params->target_angle_L16[i];
5324 pr_debug("%s: target angle[%d] = %d\n",
5325 __func__, i, doa_tracking_data->target_angle_L16[i]);
5326 }
5327
5328 for (i = 0; i < MAX_DOA_TRACKING_ANGLES; i++) {
5329 doa_tracking_data->interf_angle_L16[i] =
5330 doa_tracking_params->interf_angle_L16[i];
5331 pr_debug("%s: interference angle[%d] = %d\n",
5332 __func__, i, doa_tracking_data->interf_angle_L16[i]);
5333 }
5334
5335 for (i = 0; i < MAX_POLAR_ACTIVITY_INDICATORS; i++) {
5336 doa_tracking_data->polar_activity[i] =
5337 doa_tracking_params->polar_activity[i];
5338 }
5339
5340done:
5341 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
5342 kfree(params_value);
5343 return ret;
5344}
5345EXPORT_SYMBOL(adm_get_doa_tracking_mon);
5346
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305347int __init adm_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305348{
5349 int i = 0, j;
5350
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305351 this_adm.ec_ref_rx = -1;
Xiaojun Sangdc9e8c22019-05-13 14:46:15 +08005352 this_adm.ffecns_port_id = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305353 init_waitqueue_head(&this_adm.matrix_map_wait);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305354 init_waitqueue_head(&this_adm.adm_wait);
5355
5356 for (i = 0; i < AFE_MAX_PORTS; i++) {
5357 for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
5358 atomic_set(&this_adm.copp.id[i][j], RESET_COPP_ID);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305359 init_waitqueue_head(&this_adm.copp.wait[i][j]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305360 init_waitqueue_head(
5361 &this_adm.copp.adm_delay_wait[i][j]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305362 }
5363 }
5364
5365 if (adm_init_cal_data())
5366 pr_err("%s: could not init cal data!\n", __func__);
5367
Banajit Goswami08bb7362017-11-03 22:48:23 -07005368 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305369 this_adm.sourceTrackingData.memmap.size = 0;
5370 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
5371 this_adm.sourceTrackingData.memmap.paddr = 0;
5372 this_adm.sourceTrackingData.apr_cmd_status = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305373
5374 return 0;
5375}
5376
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05305377void adm_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305378{
Laxminath Kasam30ad7512017-11-28 12:40:22 +05305379 if (this_adm.apr)
5380 adm_reset_data();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305381 adm_delete_cal_data();
5382}