blob: 4534c4cbae1ed38b24869ad78bbf16384f93cfa4 [file] [log] [blame]
Meng Wang61af6842018-09-10 17:47:55 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2012-2018, 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>
12#include <linux/wait.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053013#include <sound/asound.h>
Laxminath Kasam8b1366a2017-10-05 01:44:16 +053014#include <dsp/msm-dts-srs-tm-config.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053015#include <dsp/apr_audio-v2.h>
16#include <dsp/q6adm-v2.h>
17#include <dsp/q6audio-v2.h>
18#include <dsp/q6afe-v2.h>
Dieter Luecking50c55352018-09-28 14:29:17 +020019#include <dsp/q6core.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053020#include <dsp/audio_cal_utils.h>
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -080021#include <dsp/q6common.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053022#include <ipc/apr.h>
23#include "adsp_err.h"
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053024
25#define TIMEOUT_MS 1000
26
27#define RESET_COPP_ID 99
28#define INVALID_COPP_ID 0xFF
29/* Used for inband payload copy, max size is 4k */
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -080030/* 3 is to account for module, instance & param ID in payload */
31#define ADM_GET_PARAMETER_LENGTH (4096 - APR_HDR_SIZE - 3 * sizeof(uint32_t))
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053032
33#define ULL_SUPPORTED_BITS_PER_SAMPLE 16
34#define ULL_SUPPORTED_SAMPLE_RATE 48000
35
36#ifndef CONFIG_DOLBY_DAP
37#undef DOLBY_ADM_COPP_TOPOLOGY_ID
38#define DOLBY_ADM_COPP_TOPOLOGY_ID 0xFFFFFFFE
39#endif
40
41#ifndef CONFIG_DOLBY_DS2
42#undef DS2_ADM_COPP_TOPOLOGY_ID
43#define DS2_ADM_COPP_TOPOLOGY_ID 0xFFFFFFFF
44#endif
45
46/* ENUM for adm_status */
47enum adm_cal_status {
48 ADM_STATUS_CALIBRATION_REQUIRED = 0,
49 ADM_STATUS_MAX,
50};
51
52struct adm_copp {
53
54 atomic_t id[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
55 atomic_t cnt[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
56 atomic_t topology[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
57 atomic_t mode[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
58 atomic_t stat[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
59 atomic_t rate[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
60 atomic_t bit_width[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
61 atomic_t channels[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
62 atomic_t app_type[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
63 atomic_t acdb_id[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
64 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;
Dieter Luecking50c55352018-09-28 14:29:17 +0200101
102 int native_mode;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530103};
104
105static struct adm_ctl this_adm;
106
107struct adm_multi_ch_map {
108 bool set_channel_map;
Dieter Luecking50c55352018-09-28 14:29:17 +0200109 char channel_mapping[PCM_FORMAT_MAX_NUM_CHANNEL_V8];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530110};
111
112#define ADM_MCH_MAP_IDX_PLAYBACK 0
113#define ADM_MCH_MAP_IDX_REC 1
114static struct adm_multi_ch_map multi_ch_maps[2] = {
Dieter Luecking50c55352018-09-28 14:29:17 +0200115 { false,
116 {0, 0, 0, 0, 0, 0, 0, 0,
117 0, 0, 0, 0, 0, 0, 0, 0,
118 0, 0, 0, 0, 0, 0, 0, 0,
119 0, 0, 0, 0, 0, 0, 0, 0}
120 },
121 { false,
122 {0, 0, 0, 0, 0, 0, 0, 0,
123 0, 0, 0, 0, 0, 0, 0, 0,
124 0, 0, 0, 0, 0, 0, 0, 0,
125 0, 0, 0, 0, 0, 0, 0, 0}
126 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530127};
128
129static int adm_get_parameters[MAX_COPPS_PER_PORT * ADM_GET_PARAMETER_LENGTH];
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800130static int adm_module_topo_list[MAX_COPPS_PER_PORT *
131 ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH];
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530132static struct mutex dts_srs_lock;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530133
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530134void msm_dts_srs_acquire_lock(void)
135{
136 mutex_lock(&dts_srs_lock);
137}
138
139void msm_dts_srs_release_lock(void)
140{
141 mutex_unlock(&dts_srs_lock);
142}
143
144/**
145 * adm_validate_and_get_port_index -
146 * validate given port id
147 *
148 * @port_id: Port ID number
149 *
150 * Returns valid index on success or error on failure
151 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530152int adm_validate_and_get_port_index(int port_id)
153{
154 int index;
155 int ret;
156
157 ret = q6audio_validate_port(port_id);
158 if (ret < 0) {
159 pr_err("%s: port validation failed id 0x%x ret %d\n",
160 __func__, port_id, ret);
161 return -EINVAL;
162 }
163
164 index = afe_get_port_index(port_id);
165 if (index < 0 || index >= AFE_MAX_PORTS) {
166 pr_err("%s: Invalid port idx %d port_id 0x%x\n",
167 __func__, index,
168 port_id);
169 return -EINVAL;
170 }
171 pr_debug("%s: port_idx- %d\n", __func__, index);
172 return index;
173}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530174EXPORT_SYMBOL(adm_validate_and_get_port_index);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530175
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530176/**
177 * adm_get_default_copp_idx -
178 * retrieve default copp_idx for given port
179 *
180 * @port_id: Port ID number
181 *
182 * Returns valid value on success or error on failure
183 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530184int adm_get_default_copp_idx(int port_id)
185{
186 int port_idx = adm_validate_and_get_port_index(port_id), idx;
187
188 if (port_idx < 0) {
189 pr_err("%s: Invalid port id: 0x%x", __func__, port_id);
190 return -EINVAL;
191 }
192 pr_debug("%s: port_idx:%d\n", __func__, port_idx);
193 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++) {
194 if (atomic_read(&this_adm.copp.id[port_idx][idx]) !=
195 RESET_COPP_ID)
196 return idx;
197 }
198 return -EINVAL;
199}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530200EXPORT_SYMBOL(adm_get_default_copp_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530201
202int adm_get_topology_for_port_from_copp_id(int port_id, int copp_id)
203{
204 int port_idx = adm_validate_and_get_port_index(port_id), idx;
205
206 if (port_idx < 0) {
207 pr_err("%s: Invalid port id: 0x%x", __func__, port_id);
208 return 0;
209 }
210 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++)
211 if (atomic_read(&this_adm.copp.id[port_idx][idx]) == copp_id)
212 return atomic_read(&this_adm.copp.topology[port_idx]
213 [idx]);
214 pr_err("%s: Invalid copp_id %d port_id 0x%x\n",
215 __func__, copp_id, port_id);
216 return 0;
217}
218
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530219/**
220 * adm_get_topology_for_port_copp_idx -
221 * retrieve topology of given port/copp_idx
222 *
223 * @port_id: Port ID number
224 * @copp_idx: copp index of ADM copp
225 *
226 * Returns valid value on success or 0 on failure
227 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530228int adm_get_topology_for_port_copp_idx(int port_id, int copp_idx)
229{
230 int port_idx = adm_validate_and_get_port_index(port_id);
231
232 if (port_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
233 pr_err("%s: Invalid port: 0x%x copp id: 0x%x",
234 __func__, port_id, copp_idx);
235 return 0;
236 }
237 return atomic_read(&this_adm.copp.topology[port_idx][copp_idx]);
238}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530239EXPORT_SYMBOL(adm_get_topology_for_port_copp_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530240
241int adm_get_indexes_from_copp_id(int copp_id, int *copp_idx, int *port_idx)
242{
243 int p_idx, c_idx;
244
245 for (p_idx = 0; p_idx < AFE_MAX_PORTS; p_idx++) {
246 for (c_idx = 0; c_idx < MAX_COPPS_PER_PORT; c_idx++) {
247 if (atomic_read(&this_adm.copp.id[p_idx][c_idx])
248 == copp_id) {
249 if (copp_idx != NULL)
250 *copp_idx = c_idx;
251 if (port_idx != NULL)
252 *port_idx = p_idx;
253 return 0;
254 }
255 }
256 }
257 return -EINVAL;
258}
259
260static int adm_get_copp_id(int port_idx, int copp_idx)
261{
262 pr_debug("%s: port_idx:%d copp_idx:%d\n", __func__, port_idx, copp_idx);
263
264 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
265 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
266 return -EINVAL;
267 }
268 return atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
269}
270
271static int adm_get_idx_if_copp_exists(int port_idx, int topology, int mode,
272 int rate, int bit_width, int app_type)
273{
274 int idx;
275
276 pr_debug("%s: port_idx-%d, topology-0x%x, mode-%d, rate-%d, bit_width-%d\n",
277 __func__, port_idx, topology, mode, rate, bit_width);
278
279 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++)
280 if ((topology ==
281 atomic_read(&this_adm.copp.topology[port_idx][idx])) &&
282 (mode == atomic_read(&this_adm.copp.mode[port_idx][idx])) &&
283 (rate == atomic_read(&this_adm.copp.rate[port_idx][idx])) &&
284 (bit_width ==
285 atomic_read(&this_adm.copp.bit_width[port_idx][idx])) &&
286 (app_type ==
287 atomic_read(&this_adm.copp.app_type[port_idx][idx])))
288 return idx;
289 return -EINVAL;
290}
291
292static int adm_get_next_available_copp(int port_idx)
293{
294 int idx;
295
296 pr_debug("%s:\n", __func__);
297 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++) {
298 pr_debug("%s: copp_id:0x%x port_idx:%d idx:%d\n", __func__,
299 atomic_read(&this_adm.copp.id[port_idx][idx]),
300 port_idx, idx);
301 if (atomic_read(&this_adm.copp.id[port_idx][idx]) ==
302 RESET_COPP_ID)
303 break;
304 }
305 return idx;
306}
307
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530308/**
309 * srs_trumedia_open -
310 * command to set SRS trumedia open
311 *
312 * @port_id: Port ID number
313 * @copp_idx: copp index of ADM copp
314 * @srs_tech_id: SRS tech index
315 * @srs_params: params pointer
316 *
317 * Returns 0 on success or error on failure
318 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530319int srs_trumedia_open(int port_id, int copp_idx, __s32 srs_tech_id,
320 void *srs_params)
321{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800322 struct param_hdr_v3 param_hdr;
323 struct mem_mapping_hdr mem_hdr;
324 u32 total_param_size = 0;
325 bool outband = false;
326 int port_idx;
327 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530328
329 pr_debug("SRS - %s", __func__);
330
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800331 memset(&param_hdr, 0, sizeof(param_hdr));
332 memset(&mem_hdr, 0, sizeof(mem_hdr));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530333 port_id = afe_convert_virtual_to_portid(port_id);
334 port_idx = adm_validate_and_get_port_index(port_id);
335 if (port_idx < 0) {
336 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
337 return -EINVAL;
338 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800339
340 param_hdr.module_id = SRS_TRUMEDIA_MODULE_ID;
341 param_hdr.instance_id = INSTANCE_ID_0;
342
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530343 switch (srs_tech_id) {
344 case SRS_ID_GLOBAL: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800345 param_hdr.param_id = SRS_TRUMEDIA_PARAMS;
346 param_hdr.param_size =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530347 sizeof(struct srs_trumedia_params_GLOBAL);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530348 break;
349 }
350 case SRS_ID_WOWHD: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800351 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_WOWHD;
352 param_hdr.param_size = sizeof(struct srs_trumedia_params_WOWHD);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530353 break;
354 }
355 case SRS_ID_CSHP: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800356 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_CSHP;
357 param_hdr.param_size = sizeof(struct srs_trumedia_params_CSHP);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530358 break;
359 }
360 case SRS_ID_HPF: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800361 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_HPF;
362 param_hdr.param_size = sizeof(struct srs_trumedia_params_HPF);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530363 break;
364 }
365 case SRS_ID_AEQ: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800366 u8 *update_params_ptr = (u8 *) this_adm.outband_memmap.kvaddr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530367
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800368 outband = true;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530369
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530370 if (update_params_ptr == NULL) {
371 pr_err("ADM_SRS_TRUMEDIA - %s: null memmap for AEQ params\n",
372 __func__);
373 ret = -EINVAL;
374 goto fail_cmd;
375 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530376
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800377 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_AEQ;
378 param_hdr.param_size = sizeof(struct srs_trumedia_params_AEQ);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530379
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800380 ret = q6common_pack_pp_params(update_params_ptr, &param_hdr,
381 srs_params, &total_param_size);
382 if (ret) {
383 pr_err("%s: Failed to pack param header and data, error %d\n",
384 __func__, ret);
385 goto fail_cmd;
386 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530387 break;
388 }
389 case SRS_ID_HL: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800390 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_HL;
391 param_hdr.param_size = sizeof(struct srs_trumedia_params_HL);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530392 break;
393 }
394 case SRS_ID_GEQ: {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800395 param_hdr.param_id = SRS_TRUMEDIA_PARAMS_GEQ;
396 param_hdr.param_size = sizeof(struct srs_trumedia_params_GEQ);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530397 break;
398 }
399 default:
400 goto fail_cmd;
401 }
402
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530403 if (outband && this_adm.outband_memmap.paddr) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800404 mem_hdr.data_payload_addr_lsw =
405 lower_32_bits(this_adm.outband_memmap.paddr);
406 mem_hdr.data_payload_addr_msw =
407 msm_audio_populate_upper_32_bits(
408 this_adm.outband_memmap.paddr);
409 mem_hdr.mem_map_handle = atomic_read(
410 &this_adm.mem_map_handles[ADM_SRS_TRUMEDIA]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530411
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800412 ret = adm_set_pp_params(port_id, copp_idx, &mem_hdr, NULL,
413 total_param_size);
414 } else {
415 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx,
416 param_hdr,
417 (u8 *) srs_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530418 }
419
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800420 if (ret < 0)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530421 pr_err("SRS - %s: ADM enable for port %d failed\n", __func__,
422 port_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530423
424fail_cmd:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530425 return ret;
426}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530427EXPORT_SYMBOL(srs_trumedia_open);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530428
429static int adm_populate_channel_weight(u16 *ptr,
430 struct msm_pcm_channel_mixer *ch_mixer,
431 int channel_index)
432{
433 u16 i, j, start_index = 0;
434
435 if (channel_index > ch_mixer->output_channel) {
436 pr_err("%s: channel index %d is larger than output_channel %d\n",
437 __func__, channel_index, ch_mixer->output_channel);
438 return -EINVAL;
439 }
440
441 for (i = 0; i < ch_mixer->output_channel; i++) {
442 pr_debug("%s: weight for output %d:", __func__, i);
443 for (j = 0; j < ADM_MAX_CHANNELS; j++)
444 pr_debug(" %d",
445 ch_mixer->channel_weight[i][j]);
446 pr_debug("\n");
447 }
448
449 for (i = 0; i < channel_index; ++i)
450 start_index += ch_mixer->input_channels[i];
451
452 for (i = 0; i < ch_mixer->output_channel; ++i) {
453 for (j = start_index;
454 j < start_index +
455 ch_mixer->input_channels[channel_index]; j++) {
456 *ptr = ch_mixer->channel_weight[i][j];
457 pr_debug("%s: ptr[%d][%d] = %d\n",
458 __func__, i, j, *ptr);
459 ptr++;
460 }
461 }
462
463 return 0;
464}
465
466/*
467 * adm_programable_channel_mixer
468 *
469 * Receives port_id, copp_idx, session_id, session_type, ch_mixer
470 * and channel_index to send ADM command to mix COPP data.
471 *
472 * port_id - Passed value, port_id for which backend is wanted
473 * copp_idx - Passed value, copp_idx for which COPP is wanted
474 * session_id - Passed value, session_id for which session is needed
475 * session_type - Passed value, session_type for RX or TX
476 * ch_mixer - Passed value, ch_mixer for which channel mixer config is needed
477 * channel_index - Passed value, channel_index for which channel is needed
478 */
479int adm_programable_channel_mixer(int port_id, int copp_idx, int session_id,
480 int session_type,
481 struct msm_pcm_channel_mixer *ch_mixer,
482 int channel_index)
483{
484 struct adm_cmd_set_pspd_mtmx_strtr_params_v5 *adm_params = NULL;
Vignesh Kulothungan2c6dccd2018-03-22 14:18:43 -0700485 struct param_hdr_v1 data_v5;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530486 int ret = 0, port_idx, sz = 0, param_size = 0;
487 u16 *adm_pspd_params;
488 u16 *ptr;
489 int index = 0;
490
491 pr_debug("%s: port_id = %d\n", __func__, port_id);
492 port_id = afe_convert_virtual_to_portid(port_id);
493 port_idx = adm_validate_and_get_port_index(port_id);
494 if (port_idx < 0) {
495 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
496 return -EINVAL;
497 }
498 /*
499 * First 8 bytes are 4 bytes as rule number, 2 bytes as output
500 * channel and 2 bytes as input channel.
501 * 2 * ch_mixer->output_channel means output channel mapping.
502 * 2 * ch_mixer->input_channels[channel_index]) means input
503 * channel mapping.
504 * 2 * ch_mixer->input_channels[channel_index] *
505 * ch_mixer->output_channel) means the channel mixer weighting
506 * coefficients.
507 * param_size needs to be a multiple of 4 bytes.
508 */
509
510 param_size = 2 * (4 + ch_mixer->output_channel +
511 ch_mixer->input_channels[channel_index] +
512 ch_mixer->input_channels[channel_index] *
513 ch_mixer->output_channel);
514 roundup(param_size, 4);
515
516 sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5) +
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800517 sizeof(struct default_chmixer_param_id_coeff) +
Vignesh Kulothungan2c6dccd2018-03-22 14:18:43 -0700518 sizeof(struct param_hdr_v1) + param_size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530519 pr_debug("%s: sz = %d\n", __func__, sz);
520 adm_params = kzalloc(sz, GFP_KERNEL);
521 if (!adm_params)
522 return -ENOMEM;
523
524 adm_params->payload_addr_lsw = 0;
525 adm_params->payload_addr_msw = 0;
526 adm_params->mem_map_handle = 0;
527 adm_params->direction = session_type;
528 adm_params->sessionid = session_id;
529 pr_debug("%s: copp_id = %d, session id %d\n", __func__,
530 atomic_read(&this_adm.copp.id[port_idx][copp_idx]),
531 session_id);
532 adm_params->deviceid = atomic_read(
533 &this_adm.copp.id[port_idx][copp_idx]);
534 adm_params->reserved = 0;
535
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800536 /*
537 * This module is internal to ADSP and cannot be configured with
538 * an instance id
539 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530540 data_v5.module_id = MTMX_MODULE_ID_DEFAULT_CHMIXER;
541 data_v5.param_id = DEFAULT_CHMIXER_PARAM_ID_COEFF;
542 data_v5.reserved = 0;
543 data_v5.param_size = param_size;
544 adm_params->payload_size =
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800545 sizeof(struct default_chmixer_param_id_coeff) +
Vignesh Kulothungan2c6dccd2018-03-22 14:18:43 -0700546 sizeof(struct param_hdr_v1) + data_v5.param_size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530547 adm_pspd_params = (u16 *)((u8 *)adm_params +
548 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5));
549 memcpy(adm_pspd_params, &data_v5, sizeof(data_v5));
550
551 adm_pspd_params = (u16 *)((u8 *)adm_params +
552 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5)
553 + sizeof(data_v5));
554
555 adm_pspd_params[0] = ch_mixer->rule;
556 adm_pspd_params[2] = ch_mixer->output_channel;
557 adm_pspd_params[3] = ch_mixer->input_channels[channel_index];
558 index = 4;
559
560 if (ch_mixer->output_channel == 1) {
561 adm_pspd_params[index] = PCM_CHANNEL_FC;
562 } else if (ch_mixer->output_channel == 2) {
563 adm_pspd_params[index] = PCM_CHANNEL_FL;
564 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
565 } else if (ch_mixer->output_channel == 3) {
566 adm_pspd_params[index] = PCM_CHANNEL_FL;
567 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
568 adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
569 } else if (ch_mixer->output_channel == 4) {
570 adm_pspd_params[index] = PCM_CHANNEL_FL;
571 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
572 adm_pspd_params[index + 2] = PCM_CHANNEL_LS;
573 adm_pspd_params[index + 3] = PCM_CHANNEL_RS;
574 } else if (ch_mixer->output_channel == 5) {
575 adm_pspd_params[index] = PCM_CHANNEL_FL;
576 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
577 adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
578 adm_pspd_params[index + 3] = PCM_CHANNEL_LS;
579 adm_pspd_params[index + 4] = PCM_CHANNEL_RS;
580 } else if (ch_mixer->output_channel == 6) {
581 adm_pspd_params[index] = PCM_CHANNEL_FL;
582 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
583 adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
584 adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
585 adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
586 adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
587 } else if (ch_mixer->output_channel == 8) {
588 adm_pspd_params[index] = PCM_CHANNEL_FL;
589 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
590 adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
591 adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
592 adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
593 adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
594 adm_pspd_params[index + 6] = PCM_CHANNEL_LB;
595 adm_pspd_params[index + 7] = PCM_CHANNEL_RB;
596 }
597
598 index = index + ch_mixer->output_channel;
599 if (ch_mixer->input_channels[channel_index] == 1) {
600 adm_pspd_params[index] = PCM_CHANNEL_FC;
601 } else if (ch_mixer->input_channels[channel_index] == 2) {
602 adm_pspd_params[index] = PCM_CHANNEL_FL;
603 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
604 } else if (ch_mixer->input_channels[channel_index] == 3) {
605 adm_pspd_params[index] = PCM_CHANNEL_FL;
606 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
607 adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
608 } else if (ch_mixer->input_channels[channel_index] == 4) {
609 adm_pspd_params[index] = PCM_CHANNEL_FL;
610 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
611 adm_pspd_params[index + 2] = PCM_CHANNEL_LS;
612 adm_pspd_params[index + 3] = PCM_CHANNEL_RS;
613 } else if (ch_mixer->input_channels[channel_index] == 5) {
614 adm_pspd_params[index] = PCM_CHANNEL_FL;
615 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
616 adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
617 adm_pspd_params[index + 3] = PCM_CHANNEL_LS;
618 adm_pspd_params[index + 4] = PCM_CHANNEL_RS;
619 } else if (ch_mixer->input_channels[channel_index] == 6) {
620 adm_pspd_params[index] = PCM_CHANNEL_FL;
621 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
622 adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
623 adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
624 adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
625 adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
626 } else if (ch_mixer->input_channels[channel_index] == 8) {
627 adm_pspd_params[index] = PCM_CHANNEL_FL;
628 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
629 adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
630 adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
631 adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
632 adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
633 adm_pspd_params[index + 6] = PCM_CHANNEL_LB;
634 adm_pspd_params[index + 7] = PCM_CHANNEL_RB;
635 }
636
637 index = index + ch_mixer->input_channels[channel_index];
638 ret = adm_populate_channel_weight(&adm_pspd_params[index],
639 ch_mixer, channel_index);
Meng Wangd9fa69c2018-01-23 12:42:52 +0800640 if (ret) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530641 pr_err("%s: fail to get channel weight with error %d\n",
642 __func__, ret);
643 goto fail_cmd;
644 }
645
646 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
647 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
648 adm_params->hdr.src_svc = APR_SVC_ADM;
649 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
650 adm_params->hdr.src_port = port_id;
651 adm_params->hdr.dest_svc = APR_SVC_ADM;
652 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
653 adm_params->hdr.dest_port =
654 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
655 adm_params->hdr.token = port_idx << 16 | copp_idx;
656 adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5;
657 adm_params->hdr.pkt_size = sz;
658 adm_params->payload_addr_lsw = 0;
659 adm_params->payload_addr_msw = 0;
660 adm_params->mem_map_handle = 0;
661 adm_params->reserved = 0;
662
663 ptr = (u16 *)adm_params;
664 for (index = 0; index < (sz / 2); index++)
665 pr_debug("%s: adm_params[%d] = 0x%x\n",
666 __func__, index, (unsigned int)ptr[index]);
667
668 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], 0);
669 ret = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
670 if (ret < 0) {
671 pr_err("%s: Set params failed port %d rc %d\n", __func__,
672 port_id, ret);
673 ret = -EINVAL;
674 goto fail_cmd;
675 }
676
677 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
678 atomic_read(
679 &this_adm.copp.stat[port_idx][copp_idx]) >= 0,
680 msecs_to_jiffies(TIMEOUT_MS));
681 if (!ret) {
682 pr_err("%s: set params timed out port = %d\n",
683 __func__, port_id);
684 ret = -ETIMEDOUT;
685 goto fail_cmd;
686 }
687 ret = 0;
688fail_cmd:
689 kfree(adm_params);
690
691 return ret;
692}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530693EXPORT_SYMBOL(adm_programable_channel_mixer);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530694
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530695/**
696 * adm_set_stereo_to_custom_stereo -
697 * command to update custom stereo
698 *
699 * @port_id: Port ID number
700 * @copp_idx: copp index of ADM copp
701 * @session_id: session id to be updated
702 * @params: params pointer
703 * @param_length: length of params
704 *
705 * Returns 0 on success or error on failure
706 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530707int adm_set_stereo_to_custom_stereo(int port_id, int copp_idx,
708 unsigned int session_id, char *params,
709 uint32_t params_length)
710{
711 struct adm_cmd_set_pspd_mtmx_strtr_params_v5 *adm_params = NULL;
712 int sz, rc = 0, port_idx;
713
714 pr_debug("%s:\n", __func__);
715 port_id = afe_convert_virtual_to_portid(port_id);
716 port_idx = adm_validate_and_get_port_index(port_id);
717 if (port_idx < 0) {
718 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
719 return -EINVAL;
720 }
721
722 sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5) +
723 params_length;
724 adm_params = kzalloc(sz, GFP_KERNEL);
725 if (!adm_params) {
726 pr_err("%s, adm params memory alloc failed\n", __func__);
727 return -ENOMEM;
728 }
729
730 memcpy(((u8 *)adm_params +
731 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5)),
732 params, params_length);
733 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
734 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
735 adm_params->hdr.pkt_size = sz;
736 adm_params->hdr.src_svc = APR_SVC_ADM;
737 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
738 adm_params->hdr.src_port = port_id;
739 adm_params->hdr.dest_svc = APR_SVC_ADM;
740 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
741 adm_params->hdr.dest_port = 0; /* Ignored */;
742 adm_params->hdr.token = port_idx << 16 | copp_idx;
743 adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5;
744 adm_params->payload_addr_lsw = 0;
745 adm_params->payload_addr_msw = 0;
746 adm_params->mem_map_handle = 0;
747 adm_params->payload_size = params_length;
748 /* direction RX as 0 */
749 adm_params->direction = ADM_MATRIX_ID_AUDIO_RX;
750 /* session id for this cmd to be applied on */
751 adm_params->sessionid = session_id;
752 adm_params->deviceid =
753 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
754 adm_params->reserved = 0;
755 pr_debug("%s: deviceid %d, session_id %d, src_port %d, dest_port %d\n",
756 __func__, adm_params->deviceid, adm_params->sessionid,
757 adm_params->hdr.src_port, adm_params->hdr.dest_port);
758 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
759 rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
760 if (rc < 0) {
761 pr_err("%s: Set params failed port = 0x%x rc %d\n",
762 __func__, port_id, rc);
763 rc = -EINVAL;
764 goto set_stereo_to_custom_stereo_return;
765 }
766 /* Wait for the callback */
767 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
768 atomic_read(&this_adm.copp.stat
769 [port_idx][copp_idx]) >= 0,
770 msecs_to_jiffies(TIMEOUT_MS));
771 if (!rc) {
772 pr_err("%s: Set params timed out port = 0x%x\n", __func__,
773 port_id);
774 rc = -EINVAL;
775 goto set_stereo_to_custom_stereo_return;
776 } else if (atomic_read(&this_adm.copp.stat
777 [port_idx][copp_idx]) > 0) {
778 pr_err("%s: DSP returned error[%s]\n", __func__,
779 adsp_err_get_err_str(atomic_read(
780 &this_adm.copp.stat
781 [port_idx][copp_idx])));
782 rc = adsp_err_get_lnx_err_code(
783 atomic_read(&this_adm.copp.stat
784 [port_idx][copp_idx]));
785 goto set_stereo_to_custom_stereo_return;
786 }
787 rc = 0;
788set_stereo_to_custom_stereo_return:
789 kfree(adm_params);
790 return rc;
791}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530792EXPORT_SYMBOL(adm_set_stereo_to_custom_stereo);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530793
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800794/*
Bhalchandra Gajareeed46bd2018-05-15 16:48:07 -0700795 * adm_set_custom_chmix_cfg:
796 * Set the custom channel mixer configuration for ADM
797 *
798 * @port_id: Backend port id
799 * @copp_idx: ADM copp index
800 * @session_id: ID of the requesting session
801 * @params: Expected packaged params for channel mixer
802 * @params_length: Length of the params to be set
803 * @direction: RX or TX direction
804 * @stream_type: Audio or Listen stream type
805 */
806int adm_set_custom_chmix_cfg(int port_id, int copp_idx,
807 unsigned int session_id, char *params,
808 uint32_t params_length, int direction,
809 int stream_type)
810{
811 struct adm_cmd_set_pspd_mtmx_strtr_params_v6 *adm_params = NULL;
812 int sz, rc = 0, port_idx;
813
814 port_id = afe_convert_virtual_to_portid(port_id);
815 port_idx = adm_validate_and_get_port_index(port_id);
816 if (port_idx < 0) {
817 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
818 return -EINVAL;
819 }
820
821 sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v6) +
822 params_length;
823 adm_params = kzalloc(sz, GFP_KERNEL);
824 if (!adm_params) {
825 pr_err("%s, adm params memory alloc failed\n", __func__);
826 return -ENOMEM;
827 }
828
829 memcpy(((u8 *)adm_params +
830 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v6)),
831 params, params_length);
832 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
833 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
834 adm_params->hdr.pkt_size = sz;
835 adm_params->hdr.src_svc = APR_SVC_ADM;
836 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
837 adm_params->hdr.src_port = port_id;
838 adm_params->hdr.dest_svc = APR_SVC_ADM;
839 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
840 adm_params->hdr.dest_port = 0; /* Ignored */;
841 adm_params->hdr.token = port_idx << 16 | copp_idx;
842 adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V6;
843 adm_params->payload_addr_lsw = 0;
844 adm_params->payload_addr_msw = 0;
845 adm_params->mem_map_handle = 0;
846 adm_params->payload_size = params_length;
847 adm_params->direction = direction;
848 /* session id for this cmd to be applied on */
849 adm_params->sessionid = session_id;
850 adm_params->deviceid =
851 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
852 /* connecting stream type i.e. lsm or asm */
853 adm_params->stream_type = stream_type;
854 pr_debug("%s: deviceid %d, session_id %d, src_port %d, dest_port %d\n",
855 __func__, adm_params->deviceid, adm_params->sessionid,
856 adm_params->hdr.src_port, adm_params->hdr.dest_port);
857 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
858 rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
859 if (rc < 0) {
860 pr_err("%s: Set params failed port = 0x%x rc %d\n",
861 __func__, port_id, rc);
862 rc = -EINVAL;
863 goto exit;
864 }
865 /* Wait for the callback */
866 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
867 atomic_read(&this_adm.copp.stat
868 [port_idx][copp_idx]),
869 msecs_to_jiffies(TIMEOUT_MS));
870 if (!rc) {
871 pr_err("%s: Set params timed out port = 0x%x\n", __func__,
872 port_id);
873 rc = -EINVAL;
874 goto exit;
875 } else if (atomic_read(&this_adm.copp.stat
876 [port_idx][copp_idx]) > 0) {
877 pr_err("%s: DSP returned error[%s]\n", __func__,
878 adsp_err_get_err_str(atomic_read(
879 &this_adm.copp.stat
880 [port_idx][copp_idx])));
881 rc = adsp_err_get_lnx_err_code(
882 atomic_read(&this_adm.copp.stat
883 [port_idx][copp_idx]));
884 goto exit;
885 }
886
887 rc = 0;
888exit:
889 kfree(adm_params);
890 return rc;
891}
892EXPORT_SYMBOL(adm_set_custom_chmix_cfg);
893
894/*
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800895 * With pre-packed data, only the opcode differes from V5 and V6.
896 * Use q6common_pack_pp_params to pack the data correctly.
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530897 */
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800898int adm_set_pp_params(int port_id, int copp_idx,
899 struct mem_mapping_hdr *mem_hdr, u8 *param_data,
900 u32 param_size)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530901{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800902 struct adm_cmd_set_pp_params *adm_set_params = NULL;
903 int size = 0;
904 int port_idx = 0;
905 atomic_t *copp_stat = NULL;
906 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530907
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530908 port_id = afe_convert_virtual_to_portid(port_id);
909 port_idx = adm_validate_and_get_port_index(port_id);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800910 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
911 pr_err("%s: Invalid port_idx 0x%x\n", __func__, port_idx);
912 return -EINVAL;
913 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
914 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530915 return -EINVAL;
916 }
917
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800918 /* Only add params_size in inband case */
919 size = sizeof(struct adm_cmd_set_pp_params);
920 if (param_data != NULL)
921 size += param_size;
922 adm_set_params = kzalloc(size, GFP_KERNEL);
923 if (!adm_set_params)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530924 return -ENOMEM;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800925
926 adm_set_params->apr_hdr.hdr_field =
927 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
928 APR_PKT_VER);
929 adm_set_params->apr_hdr.pkt_size = size;
930 adm_set_params->apr_hdr.src_svc = APR_SVC_ADM;
931 adm_set_params->apr_hdr.src_domain = APR_DOMAIN_APPS;
932 adm_set_params->apr_hdr.src_port = port_id;
933 adm_set_params->apr_hdr.dest_svc = APR_SVC_ADM;
934 adm_set_params->apr_hdr.dest_domain = APR_DOMAIN_ADSP;
935 adm_set_params->apr_hdr.dest_port =
936 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
937 adm_set_params->apr_hdr.token = port_idx << 16 | copp_idx;
938
939 if (q6common_is_instance_id_supported())
940 adm_set_params->apr_hdr.opcode = ADM_CMD_SET_PP_PARAMS_V6;
941 else
942 adm_set_params->apr_hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
943
944 adm_set_params->payload_size = param_size;
945
946 if (mem_hdr != NULL) {
947 /* Out of Band Case */
948 adm_set_params->mem_hdr = *mem_hdr;
949 } else if (param_data != NULL) {
950 /*
951 * In band case. Parameter data must be pre-packed with its
952 * header before calling this function. Use
953 * q6common_pack_pp_params to pack parameter data and header
954 * correctly.
955 */
956 memcpy(&adm_set_params->param_data, param_data, param_size);
957 } else {
958 pr_err("%s: Received NULL pointers for both memory header and param data\n",
959 __func__);
960 ret = -EINVAL;
961 goto done;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530962 }
963
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800964 copp_stat = &this_adm.copp.stat[port_idx][copp_idx];
965 atomic_set(copp_stat, -1);
966 ret = apr_send_pkt(this_adm.apr, (uint32_t *) adm_set_params);
967 if (ret < 0) {
968 pr_err("%s: Set params APR send failed port = 0x%x ret %d\n",
969 __func__, port_id, ret);
970 goto done;
971 }
972 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
973 atomic_read(copp_stat) >= 0,
974 msecs_to_jiffies(TIMEOUT_MS));
975 if (!ret) {
976 pr_err("%s: Set params timed out port = 0x%x\n", __func__,
977 port_id);
978 ret = -ETIMEDOUT;
979 goto done;
980 }
981 if (atomic_read(copp_stat) > 0) {
982 pr_err("%s: DSP returned error[%s]\n", __func__,
983 adsp_err_get_err_str(atomic_read(copp_stat)));
984 ret = adsp_err_get_lnx_err_code(atomic_read(copp_stat));
985 goto done;
986 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530987
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800988 ret = 0;
989done:
990 kfree(adm_set_params);
991 return ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530992}
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -0800993EXPORT_SYMBOL(adm_set_pp_params);
994
995int adm_pack_and_set_one_pp_param(int port_id, int copp_idx,
996 struct param_hdr_v3 param_hdr, u8 *param_data)
997{
998 u8 *packed_data = NULL;
999 u32 total_size = 0;
1000 int ret = 0;
1001
1002 total_size = sizeof(union param_hdrs) + param_hdr.param_size;
1003 packed_data = kzalloc(total_size, GFP_KERNEL);
1004 if (!packed_data)
1005 return -ENOMEM;
1006
1007 ret = q6common_pack_pp_params(packed_data, &param_hdr, param_data,
1008 &total_size);
1009 if (ret) {
1010 pr_err("%s: Failed to pack parameter data, error %d\n",
1011 __func__, ret);
1012 goto done;
1013 }
1014
1015 ret = adm_set_pp_params(port_id, copp_idx, NULL, packed_data,
1016 total_size);
1017 if (ret)
1018 pr_err("%s: Failed to set parameter data, error %d\n", __func__,
1019 ret);
1020done:
1021 kfree(packed_data);
1022 return ret;
1023}
1024EXPORT_SYMBOL(adm_pack_and_set_one_pp_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301025
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001026/*
1027 * Only one parameter can be requested at a time. Therefore, packing and sending
1028 * the request can be handled locally.
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301029 */
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001030int adm_get_pp_params(int port_id, int copp_idx, uint32_t client_id,
1031 struct mem_mapping_hdr *mem_hdr,
1032 struct param_hdr_v3 *param_hdr, u8 *returned_param_data)
1033{
1034 struct adm_cmd_get_pp_params adm_get_params;
1035 int total_size = 0;
1036 int get_param_array_sz = ARRAY_SIZE(adm_get_parameters);
1037 int returned_param_size = 0;
1038 int returned_param_size_in_bytes = 0;
1039 int port_idx = 0;
1040 int idx = 0;
1041 atomic_t *copp_stat = NULL;
1042 int ret = 0;
1043
1044 if (param_hdr == NULL) {
1045 pr_err("%s: Received NULL pointer for parameter header\n",
1046 __func__);
1047 return -EINVAL;
1048 }
1049
1050 port_id = afe_convert_virtual_to_portid(port_id);
1051 port_idx = adm_validate_and_get_port_index(port_id);
1052 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
1053 pr_err("%s: Invalid port_idx 0x%x\n", __func__, port_idx);
1054 return -EINVAL;
1055 }
1056 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
1057 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
1058 return -EINVAL;
1059 }
1060
1061 memset(&adm_get_params, 0, sizeof(adm_get_params));
1062
1063 if (mem_hdr != NULL)
1064 adm_get_params.mem_hdr = *mem_hdr;
1065
1066 q6common_pack_pp_params((u8 *) &adm_get_params.param_hdr, param_hdr,
1067 NULL, &total_size);
1068
1069 /* Pack APR header after filling body so total_size has correct value */
Vignesh Kulothunganaadecdf2018-06-20 15:08:24 -07001070 adm_get_params.apr_hdr.hdr_field =
1071 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
1072 APR_PKT_VER);
1073 adm_get_params.apr_hdr.pkt_size = sizeof(adm_get_params);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001074 adm_get_params.apr_hdr.src_svc = APR_SVC_ADM;
1075 adm_get_params.apr_hdr.src_domain = APR_DOMAIN_APPS;
1076 adm_get_params.apr_hdr.src_port = port_id;
1077 adm_get_params.apr_hdr.dest_svc = APR_SVC_ADM;
1078 adm_get_params.apr_hdr.dest_domain = APR_DOMAIN_ADSP;
1079 adm_get_params.apr_hdr.dest_port =
1080 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
1081 adm_get_params.apr_hdr.token =
1082 port_idx << 16 | client_id << 8 | copp_idx;
1083
1084 if (q6common_is_instance_id_supported())
1085 adm_get_params.apr_hdr.opcode = ADM_CMD_GET_PP_PARAMS_V6;
1086 else
1087 adm_get_params.apr_hdr.opcode = ADM_CMD_GET_PP_PARAMS_V5;
1088
1089 copp_stat = &this_adm.copp.stat[port_idx][copp_idx];
1090 atomic_set(copp_stat, -1);
Vignesh Kulothunganaadecdf2018-06-20 15:08:24 -07001091
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001092 ret = apr_send_pkt(this_adm.apr, (uint32_t *) &adm_get_params);
Vidyakumar Athotae427c412018-06-16 00:02:46 -07001093 if (ret < 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001094 pr_err("%s: Get params APR send failed port = 0x%x ret %d\n",
1095 __func__, port_id, ret);
1096 ret = -EINVAL;
1097 goto done;
1098 }
1099 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
1100 atomic_read(copp_stat) >= 0,
1101 msecs_to_jiffies(TIMEOUT_MS));
1102 if (!ret) {
1103 pr_err("%s: Get params timed out port = 0x%x\n", __func__,
1104 port_id);
1105 ret = -ETIMEDOUT;
1106 goto done;
1107 }
1108 if (atomic_read(copp_stat) > 0) {
1109 pr_err("%s: DSP returned error[%s]\n", __func__,
1110 adsp_err_get_err_str(atomic_read(copp_stat)));
1111 ret = adsp_err_get_lnx_err_code(atomic_read(copp_stat));
1112 goto done;
1113 }
1114
1115 ret = 0;
1116
1117 /* Copy data to caller if sent in band */
1118 if (!returned_param_data) {
1119 pr_debug("%s: Received NULL pointer for param destination, not copying payload\n",
1120 __func__);
1121 return 0;
1122 }
1123
1124 idx = ADM_GET_PARAMETER_LENGTH * copp_idx;
1125 returned_param_size = adm_get_parameters[idx];
1126 if (returned_param_size < 0 ||
1127 returned_param_size + idx + 1 > get_param_array_sz) {
1128 pr_err("%s: Invalid parameter size %d\n", __func__,
1129 returned_param_size);
1130 return -EINVAL;
1131 }
1132
1133 returned_param_size_in_bytes = returned_param_size * sizeof(uint32_t);
1134 if (param_hdr->param_size < returned_param_size_in_bytes) {
1135 pr_err("%s: Provided buffer is not big enough, provided buffer size(%d) size needed(%d)\n",
1136 __func__, param_hdr->param_size,
1137 returned_param_size_in_bytes);
1138 return -EINVAL;
1139 }
1140
1141 memcpy(returned_param_data, &adm_get_parameters[idx + 1],
1142 returned_param_size_in_bytes);
1143done:
1144 return ret;
1145}
1146EXPORT_SYMBOL(adm_get_pp_params);
1147
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001148int adm_get_pp_topo_module_list_v2(int port_id, int copp_idx,
1149 int32_t param_length,
1150 int32_t *returned_params)
1151{
1152 struct adm_cmd_get_pp_topo_module_list adm_get_module_list;
1153 bool iid_supported = q6common_is_instance_id_supported();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301154 int *topo_list;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001155 int num_modules = 0;
1156 int list_size = 0;
1157 int port_idx, idx;
1158 int i = 0;
1159 atomic_t *copp_stat = NULL;
1160 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301161
1162 pr_debug("%s : port_id %x", __func__, port_id);
1163 port_id = afe_convert_virtual_to_portid(port_id);
1164 port_idx = adm_validate_and_get_port_index(port_id);
1165 if (port_idx < 0) {
1166 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
1167 return -EINVAL;
1168 }
1169
1170 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
1171 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
1172 return -EINVAL;
1173 }
1174
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001175 memset(&adm_get_module_list, 0, sizeof(adm_get_module_list));
1176
1177 adm_get_module_list.apr_hdr.pkt_size = sizeof(adm_get_module_list);
1178 adm_get_module_list.apr_hdr.src_svc = APR_SVC_ADM;
1179 adm_get_module_list.apr_hdr.src_domain = APR_DOMAIN_APPS;
1180 adm_get_module_list.apr_hdr.src_port = port_id;
1181 adm_get_module_list.apr_hdr.dest_svc = APR_SVC_ADM;
1182 adm_get_module_list.apr_hdr.dest_domain = APR_DOMAIN_ADSP;
1183 adm_get_module_list.apr_hdr.dest_port =
1184 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
1185 adm_get_module_list.apr_hdr.token = port_idx << 16 | copp_idx;
1186 /*
1187 * Out of band functionality is not currently utilized.
1188 * Assume in band.
1189 */
1190 if (iid_supported) {
1191 adm_get_module_list.apr_hdr.opcode =
1192 ADM_CMD_GET_PP_TOPO_MODULE_LIST_V2;
1193 adm_get_module_list.param_max_size = param_length;
1194 } else {
1195 adm_get_module_list.apr_hdr.opcode =
1196 ADM_CMD_GET_PP_TOPO_MODULE_LIST;
1197
1198 if (param_length > U16_MAX) {
1199 pr_err("%s: Invalid param length for V1 %d\n", __func__,
1200 param_length);
1201 return -EINVAL;
1202 }
1203 adm_get_module_list.param_max_size = param_length << 16;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301204 }
1205
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001206 copp_stat = &this_adm.copp.stat[port_idx][copp_idx];
1207 atomic_set(copp_stat, -1);
1208 ret = apr_send_pkt(this_adm.apr, (uint32_t *) &adm_get_module_list);
Vidyakumar Athotae427c412018-06-16 00:02:46 -07001209 if (ret < 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001210 pr_err("%s: APR send pkt failed for port_id: 0x%x failed ret %d\n",
1211 __func__, port_id, ret);
1212 ret = -EINVAL;
1213 goto done;
1214 }
1215 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
1216 atomic_read(copp_stat) >= 0,
1217 msecs_to_jiffies(TIMEOUT_MS));
1218 if (!ret) {
1219 pr_err("%s: Timeout for port_id: 0x%x\n", __func__, port_id);
1220 ret = -ETIMEDOUT;
1221 goto done;
1222 }
1223 if (atomic_read(copp_stat) > 0) {
1224 pr_err("%s: DSP returned error[%s]\n", __func__,
1225 adsp_err_get_err_str(atomic_read(copp_stat)));
1226 ret = adsp_err_get_lnx_err_code(atomic_read(copp_stat));
1227 goto done;
1228 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301229
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001230 ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301231
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001232 if (returned_params) {
1233 /*
1234 * When processing ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST IID is
1235 * added since it is not present. Therefore, there is no need to
1236 * do anything different if IID is not supported here as it is
1237 * already taken care of.
1238 */
1239 idx = ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH * copp_idx;
1240 num_modules = adm_module_topo_list[idx];
1241 if (num_modules < 0 || num_modules > MAX_MODULES_IN_TOPO) {
1242 pr_err("%s: Invalid number of modules returned %d\n",
1243 __func__, num_modules);
1244 return -EINVAL;
1245 }
1246
1247 list_size = num_modules * sizeof(struct module_instance_info);
1248 if (param_length < list_size) {
1249 pr_err("%s: Provided buffer not big enough to hold module-instance list, provided size %d, needed size %d\n",
1250 __func__, param_length, list_size);
1251 return -EINVAL;
1252 }
1253
1254 topo_list = (int32_t *) (&adm_module_topo_list[idx]);
1255 memcpy(returned_params, topo_list, list_size);
1256 for (i = 1; i <= num_modules; i += 2) {
1257 pr_debug("module = 0x%x instance = 0x%x\n",
1258 returned_params[i], returned_params[i + 1]);
1259 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301260 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001261done:
1262 return ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301263}
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001264EXPORT_SYMBOL(adm_get_pp_topo_module_list_v2);
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301265
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301266static void adm_callback_debug_print(struct apr_client_data *data)
1267{
1268 uint32_t *payload;
1269
1270 payload = data->payload;
1271
1272 if (data->payload_size >= 8)
1273 pr_debug("%s: code = 0x%x PL#0[0x%x], PL#1[0x%x], size = %d\n",
1274 __func__, data->opcode, payload[0], payload[1],
1275 data->payload_size);
1276 else if (data->payload_size >= 4)
1277 pr_debug("%s: code = 0x%x PL#0[0x%x], size = %d\n",
1278 __func__, data->opcode, payload[0],
1279 data->payload_size);
1280 else
1281 pr_debug("%s: code = 0x%x, size = %d\n",
1282 __func__, data->opcode, data->payload_size);
1283}
1284
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301285/**
1286 * adm_set_multi_ch_map -
1287 * Update multi channel map info
1288 *
1289 * @channel_map: pointer with channel map info
1290 * @path: direction or ADM path type
1291 *
1292 * Returns 0 on success or error on failure
1293 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301294int adm_set_multi_ch_map(char *channel_map, int path)
1295{
1296 int idx;
1297
1298 if (path == ADM_PATH_PLAYBACK) {
1299 idx = ADM_MCH_MAP_IDX_PLAYBACK;
1300 } else if (path == ADM_PATH_LIVE_REC) {
1301 idx = ADM_MCH_MAP_IDX_REC;
1302 } else {
1303 pr_err("%s: invalid attempt to set path %d\n", __func__, path);
1304 return -EINVAL;
1305 }
1306
1307 memcpy(multi_ch_maps[idx].channel_mapping, channel_map,
Dieter Luecking50c55352018-09-28 14:29:17 +02001308 PCM_FORMAT_MAX_NUM_CHANNEL_V8);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301309 multi_ch_maps[idx].set_channel_map = true;
1310
1311 return 0;
1312}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301313EXPORT_SYMBOL(adm_set_multi_ch_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301314
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301315/**
1316 * adm_get_multi_ch_map -
1317 * Retrieves multi channel map info
1318 *
1319 * @channel_map: pointer to be updated with channel map
1320 * @path: direction or ADM path type
1321 *
1322 * Returns 0 on success or error on failure
1323 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301324int adm_get_multi_ch_map(char *channel_map, int path)
1325{
1326 int idx;
1327
1328 if (path == ADM_PATH_PLAYBACK) {
1329 idx = ADM_MCH_MAP_IDX_PLAYBACK;
1330 } else if (path == ADM_PATH_LIVE_REC) {
1331 idx = ADM_MCH_MAP_IDX_REC;
1332 } else {
1333 pr_err("%s: invalid attempt to get path %d\n", __func__, path);
1334 return -EINVAL;
1335 }
1336
1337 if (multi_ch_maps[idx].set_channel_map) {
1338 memcpy(channel_map, multi_ch_maps[idx].channel_mapping,
Dieter Luecking50c55352018-09-28 14:29:17 +02001339 PCM_FORMAT_MAX_NUM_CHANNEL_V8);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301340 }
1341
1342 return 0;
1343}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301344EXPORT_SYMBOL(adm_get_multi_ch_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301345
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001346static int adm_process_get_param_response(u32 opcode, u32 idx, u32 *payload,
1347 u32 payload_size)
1348{
1349 struct adm_cmd_rsp_get_pp_params_v5 *v5_rsp = NULL;
1350 struct adm_cmd_rsp_get_pp_params_v6 *v6_rsp = NULL;
1351 u32 *param_data = NULL;
1352 int data_size = 0;
1353 int struct_size = 0;
1354
1355 if (payload == NULL) {
1356 pr_err("%s: Payload is NULL\n", __func__);
1357 return -EINVAL;
1358 }
1359
1360 switch (opcode) {
1361 case ADM_CMDRSP_GET_PP_PARAMS_V5:
1362 struct_size = sizeof(struct adm_cmd_rsp_get_pp_params_v5);
1363 v5_rsp = (struct adm_cmd_rsp_get_pp_params_v5 *) payload;
1364 data_size = v5_rsp->param_hdr.param_size;
1365 param_data = v5_rsp->param_data;
1366 break;
1367 case ADM_CMDRSP_GET_PP_PARAMS_V6:
1368 struct_size = sizeof(struct adm_cmd_rsp_get_pp_params_v6);
1369 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);
1487 this_adm.copp.adm_status[i][j] =
1488 ADM_STATUS_CALIBRATION_REQUIRED;
1489 }
1490 }
1491 this_adm.apr = NULL;
1492 cal_utils_clear_cal_block_q6maps(ADM_MAX_CAL_TYPES,
1493 this_adm.cal_data);
1494 mutex_lock(&this_adm.cal_data
1495 [ADM_CUSTOM_TOP_CAL]->lock);
1496 this_adm.set_custom_topology = 1;
1497 mutex_unlock(&this_adm.cal_data[
1498 ADM_CUSTOM_TOP_CAL]->lock);
1499 rtac_clear_mapping(ADM_RTAC_CAL);
1500 /*
1501 * Free the ION memory and clear the map handles
1502 * for Source Tracking
1503 */
1504 if (this_adm.sourceTrackingData.memmap.paddr != 0) {
1505 msm_audio_ion_free(
1506 this_adm.sourceTrackingData.dma_buf);
1507 this_adm.sourceTrackingData.dma_buf = NULL;
1508 this_adm.sourceTrackingData.memmap.size = 0;
1509 this_adm.sourceTrackingData.memmap.kvaddr =
1510 NULL;
1511 this_adm.sourceTrackingData.memmap.paddr = 0;
1512 this_adm.sourceTrackingData.apr_cmd_status = -1;
1513 atomic_set(&this_adm.mem_map_handles[
1514 ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
1515 }
1516}
1517
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301518static int32_t adm_callback(struct apr_client_data *data, void *priv)
1519{
1520 uint32_t *payload;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001521 int port_idx, copp_idx, idx, client_id;
1522 int num_modules;
1523 int ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301524
1525 if (data == NULL) {
1526 pr_err("%s: data parameter is null\n", __func__);
1527 return -EINVAL;
1528 }
1529
1530 payload = data->payload;
1531
1532 if (data->opcode == RESET_EVENTS) {
1533 pr_debug("%s: Reset event is received: %d %d apr[%pK]\n",
1534 __func__,
1535 data->reset_event, data->reset_proc, this_adm.apr);
Laxminath Kasam30ad7512017-11-28 12:40:22 +05301536 if (this_adm.apr)
1537 adm_reset_data();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301538 return 0;
1539 }
1540
1541 adm_callback_debug_print(data);
1542 if (data->payload_size) {
1543 copp_idx = (data->token) & 0XFF;
1544 port_idx = ((data->token) >> 16) & 0xFF;
1545 client_id = ((data->token) >> 8) & 0xFF;
1546 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
1547 pr_err("%s: Invalid port idx %d token %d\n",
1548 __func__, port_idx, data->token);
1549 return 0;
1550 }
1551 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
1552 pr_err("%s: Invalid copp idx %d token %d\n",
1553 __func__, copp_idx, data->token);
1554 return 0;
1555 }
1556 if (client_id < 0 || client_id >= ADM_CLIENT_ID_MAX) {
1557 pr_err("%s: Invalid client id %d\n", __func__,
1558 client_id);
1559 return 0;
1560 }
1561 if (data->opcode == APR_BASIC_RSP_RESULT) {
1562 pr_debug("%s: APR_BASIC_RSP_RESULT id 0x%x\n",
1563 __func__, payload[0]);
1564 if (payload[1] != 0) {
1565 pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
1566 __func__, payload[0], payload[1]);
1567 }
1568 switch (payload[0]) {
1569 case ADM_CMD_SET_PP_PARAMS_V5:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001570 case ADM_CMD_SET_PP_PARAMS_V6:
1571 pr_debug("%s: ADM_CMD_SET_PP_PARAMS\n",
1572 __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301573 if (client_id == ADM_CLIENT_ID_SOURCE_TRACKING)
1574 this_adm.sourceTrackingData.
1575 apr_cmd_status = payload[1];
1576 else if (rtac_make_adm_callback(payload,
1577 data->payload_size))
1578 break;
1579 /*
1580 * if soft volume is called and already
1581 * interrupted break out of the sequence here
1582 */
1583 case ADM_CMD_DEVICE_OPEN_V5:
1584 case ADM_CMD_DEVICE_CLOSE_V5:
1585 case ADM_CMD_DEVICE_OPEN_V6:
Dieter Luecking50c55352018-09-28 14:29:17 +02001586 case ADM_CMD_DEVICE_OPEN_V8:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301587 pr_debug("%s: Basic callback received, wake up.\n",
1588 __func__);
1589 atomic_set(&this_adm.copp.stat[port_idx]
1590 [copp_idx], payload[1]);
1591 wake_up(
1592 &this_adm.copp.wait[port_idx][copp_idx]);
1593 break;
1594 case ADM_CMD_ADD_TOPOLOGIES:
1595 pr_debug("%s: callback received, ADM_CMD_ADD_TOPOLOGIES.\n",
1596 __func__);
1597 atomic_set(&this_adm.adm_stat, payload[1]);
1598 wake_up(&this_adm.adm_wait);
1599 break;
1600 case ADM_CMD_MATRIX_MAP_ROUTINGS_V5:
1601 case ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5:
1602 pr_debug("%s: Basic callback received, wake up.\n",
1603 __func__);
1604 atomic_set(&this_adm.matrix_map_stat,
1605 payload[1]);
1606 wake_up(&this_adm.matrix_map_wait);
1607 break;
1608 case ADM_CMD_SHARED_MEM_UNMAP_REGIONS:
1609 pr_debug("%s: ADM_CMD_SHARED_MEM_UNMAP_REGIONS\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_SHARED_MEM_MAP_REGIONS:
1615 pr_debug("%s: ADM_CMD_SHARED_MEM_MAP_REGIONS\n",
1616 __func__);
1617 /* Should only come here if there is an APR */
1618 /* error or malformed APR packet. Otherwise */
1619 /* response will be returned as */
1620 if (payload[1] != 0) {
1621 pr_err("%s: ADM map error, resuming\n",
1622 __func__);
1623 atomic_set(&this_adm.adm_stat,
1624 payload[1]);
1625 wake_up(&this_adm.adm_wait);
1626 }
1627 break;
1628 case ADM_CMD_GET_PP_PARAMS_V5:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001629 case ADM_CMD_GET_PP_PARAMS_V6:
1630 pr_debug("%s: ADM_CMD_GET_PP_PARAMS\n",
1631 __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301632 /* Should only come here if there is an APR */
1633 /* error or malformed APR packet. Otherwise */
1634 /* response will be returned as */
1635 /* ADM_CMDRSP_GET_PP_PARAMS_V5 */
1636 if (client_id ==
1637 ADM_CLIENT_ID_SOURCE_TRACKING) {
1638 this_adm.sourceTrackingData.
1639 apr_cmd_status = payload[1];
1640 if (payload[1] != 0)
1641 pr_err("%s: ADM get param error = %d\n",
1642 __func__, payload[1]);
1643
1644 atomic_set(&this_adm.copp.stat
1645 [port_idx][copp_idx],
1646 payload[1]);
1647 wake_up(&this_adm.copp.wait
1648 [port_idx][copp_idx]);
1649 } else {
1650 if (payload[1] != 0) {
1651 pr_err("%s: ADM get param error = %d, resuming\n",
1652 __func__, payload[1]);
1653
1654 rtac_make_adm_callback(payload,
1655 data->payload_size);
1656 }
1657 }
1658 break;
1659 case ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5:
Bhalchandra Gajareeed46bd2018-05-15 16:48:07 -07001660 case ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V6:
1661 pr_debug("%s:callback received PSPD MTMX, wake up\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301662 __func__);
1663 atomic_set(&this_adm.copp.stat[port_idx]
1664 [copp_idx], payload[1]);
1665 wake_up(
1666 &this_adm.copp.wait[port_idx][copp_idx]);
1667 break;
1668 case ADM_CMD_GET_PP_TOPO_MODULE_LIST:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001669 case ADM_CMD_GET_PP_TOPO_MODULE_LIST_V2:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301670 pr_debug("%s:ADM_CMD_GET_PP_TOPO_MODULE_LIST\n",
1671 __func__);
1672 if (payload[1] != 0)
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001673 pr_err("%s: ADM get topo list error = %d\n",
1674 __func__, payload[1]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301675 break;
1676 default:
1677 pr_err("%s: Unknown Cmd: 0x%x\n", __func__,
1678 payload[0]);
1679 break;
1680 }
1681 return 0;
1682 }
1683
1684 switch (data->opcode) {
1685 case ADM_CMDRSP_DEVICE_OPEN_V5:
Dieter Luecking50c55352018-09-28 14:29:17 +02001686 case ADM_CMDRSP_DEVICE_OPEN_V6:
1687 case ADM_CMDRSP_DEVICE_OPEN_V8: {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301688 struct adm_cmd_rsp_device_open_v5 *open =
1689 (struct adm_cmd_rsp_device_open_v5 *)data->payload;
1690
1691 if (open->copp_id == INVALID_COPP_ID) {
1692 pr_err("%s: invalid coppid rxed %d\n",
1693 __func__, open->copp_id);
1694 atomic_set(&this_adm.copp.stat[port_idx]
1695 [copp_idx], ADSP_EBADPARAM);
1696 wake_up(
1697 &this_adm.copp.wait[port_idx][copp_idx]);
1698 break;
1699 }
1700 atomic_set(&this_adm.copp.stat
1701 [port_idx][copp_idx], payload[0]);
1702 atomic_set(&this_adm.copp.id[port_idx][copp_idx],
1703 open->copp_id);
1704 pr_debug("%s: coppid rxed=%d\n", __func__,
1705 open->copp_id);
1706 wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
1707 }
1708 break;
1709 case ADM_CMDRSP_GET_PP_PARAMS_V5:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001710 case ADM_CMDRSP_GET_PP_PARAMS_V6:
1711 pr_debug("%s: ADM_CMDRSP_GET_PP_PARAMS\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301712 if (client_id == ADM_CLIENT_ID_SOURCE_TRACKING)
1713 this_adm.sourceTrackingData.apr_cmd_status =
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001714 payload[0];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301715 else if (rtac_make_adm_callback(payload,
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001716 data->payload_size))
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301717 break;
1718
1719 idx = ADM_GET_PARAMETER_LENGTH * copp_idx;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001720 if (payload[0] == 0 && data->payload_size > 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001721 ret = adm_process_get_param_response(
1722 data->opcode, idx, payload,
1723 data->payload_size);
1724 if (ret)
1725 pr_err("%s: Failed to process get param response, error %d\n",
1726 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301727 } else {
1728 adm_get_parameters[idx] = -1;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001729 pr_err("%s: ADM_CMDRSP_GET_PP_PARAMS returned error 0x%x\n",
1730 __func__, payload[0]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301731 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001732 atomic_set(&this_adm.copp.stat[port_idx][copp_idx],
1733 payload[0]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301734 wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
1735 break;
1736 case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST:
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001737 case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST_V2:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301738 pr_debug("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST\n",
1739 __func__);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001740 num_modules = payload[1];
1741 pr_debug("%s: Num modules %d\n", __func__, num_modules);
1742 if (payload[0]) {
1743 pr_err("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST, error = %d\n",
1744 __func__, payload[0]);
1745 } else if (num_modules > MAX_MODULES_IN_TOPO) {
1746 pr_err("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST invalid num modules received, num modules = %d\n",
1747 __func__, num_modules);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301748 } else {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001749 ret = adm_process_get_topo_list_response(
1750 data->opcode, copp_idx, num_modules,
1751 payload, data->payload_size);
1752 if (ret)
1753 pr_err("%s: Failed to process get topo modules list response, error %d\n",
1754 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301755 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08001756 atomic_set(&this_adm.copp.stat[port_idx][copp_idx],
1757 payload[0]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301758 wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
1759 break;
1760 case ADM_CMDRSP_SHARED_MEM_MAP_REGIONS:
1761 pr_debug("%s: ADM_CMDRSP_SHARED_MEM_MAP_REGIONS\n",
1762 __func__);
1763 atomic_set(&this_adm.mem_map_handles[
1764 atomic_read(&this_adm.mem_map_index)],
1765 *payload);
1766 atomic_set(&this_adm.adm_stat, 0);
1767 wake_up(&this_adm.adm_wait);
1768 break;
1769 default:
1770 pr_err("%s: Unknown cmd:0x%x\n", __func__,
1771 data->opcode);
1772 break;
1773 }
1774 }
1775 return 0;
1776}
1777
1778static int adm_memory_map_regions(phys_addr_t *buf_add, uint32_t mempool_id,
1779 uint32_t *bufsz, uint32_t bufcnt)
1780{
1781 struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
1782 struct avs_shared_map_region_payload *mregions = NULL;
1783 void *mmap_region_cmd = NULL;
1784 void *payload = NULL;
1785 int ret = 0;
1786 int i = 0;
1787 int cmd_size = 0;
1788
1789 pr_debug("%s:\n", __func__);
1790 if (this_adm.apr == NULL) {
1791 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
1792 0xFFFFFFFF, &this_adm);
1793 if (this_adm.apr == NULL) {
1794 pr_err("%s: Unable to register ADM\n", __func__);
1795 ret = -ENODEV;
1796 return ret;
1797 }
1798 rtac_set_adm_handle(this_adm.apr);
1799 }
1800
1801 cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
1802 + sizeof(struct avs_shared_map_region_payload)
1803 * bufcnt;
1804
1805 mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
1806 if (!mmap_region_cmd)
1807 return -ENOMEM;
1808
1809 mmap_regions = (struct avs_cmd_shared_mem_map_regions *)mmap_region_cmd;
1810 mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1811 APR_HDR_LEN(APR_HDR_SIZE),
1812 APR_PKT_VER);
1813 mmap_regions->hdr.pkt_size = cmd_size;
1814 mmap_regions->hdr.src_port = 0;
1815
1816 mmap_regions->hdr.dest_port = 0;
1817 mmap_regions->hdr.token = 0;
1818 mmap_regions->hdr.opcode = ADM_CMD_SHARED_MEM_MAP_REGIONS;
1819 mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL & 0x00ff;
1820 mmap_regions->num_regions = bufcnt & 0x00ff;
1821 mmap_regions->property_flag = 0x00;
1822
1823 pr_debug("%s: map_regions->num_regions = %d\n", __func__,
1824 mmap_regions->num_regions);
1825 payload = ((u8 *) mmap_region_cmd +
1826 sizeof(struct avs_cmd_shared_mem_map_regions));
1827 mregions = (struct avs_shared_map_region_payload *)payload;
1828
1829 for (i = 0; i < bufcnt; i++) {
1830 mregions->shm_addr_lsw = lower_32_bits(buf_add[i]);
1831 mregions->shm_addr_msw =
1832 msm_audio_populate_upper_32_bits(buf_add[i]);
1833 mregions->mem_size_bytes = bufsz[i];
1834 ++mregions;
1835 }
1836
1837 atomic_set(&this_adm.adm_stat, -1);
1838 ret = apr_send_pkt(this_adm.apr, (uint32_t *) mmap_region_cmd);
1839 if (ret < 0) {
1840 pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__,
1841 mmap_regions->hdr.opcode, ret);
1842 ret = -EINVAL;
1843 goto fail_cmd;
1844 }
1845
1846 ret = wait_event_timeout(this_adm.adm_wait,
1847 atomic_read(&this_adm.adm_stat) >= 0,
1848 5 * HZ);
1849 if (!ret) {
1850 pr_err("%s: timeout. waited for memory_map\n", __func__);
1851 ret = -EINVAL;
1852 goto fail_cmd;
1853 } else if (atomic_read(&this_adm.adm_stat) > 0) {
1854 pr_err("%s: DSP returned error[%s]\n",
1855 __func__, adsp_err_get_err_str(
1856 atomic_read(&this_adm.adm_stat)));
1857 ret = adsp_err_get_lnx_err_code(
1858 atomic_read(&this_adm.adm_stat));
1859 goto fail_cmd;
1860 }
1861fail_cmd:
1862 kfree(mmap_region_cmd);
1863 return ret;
1864}
1865
1866static int adm_memory_unmap_regions(void)
1867{
1868 struct avs_cmd_shared_mem_unmap_regions unmap_regions;
1869 int ret = 0;
1870
1871 pr_debug("%s:\n", __func__);
1872 if (this_adm.apr == NULL) {
1873 pr_err("%s: APR handle NULL\n", __func__);
1874 return -EINVAL;
1875 }
1876
1877 unmap_regions.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1878 APR_HDR_LEN(APR_HDR_SIZE),
1879 APR_PKT_VER);
1880 unmap_regions.hdr.pkt_size = sizeof(unmap_regions);
1881 unmap_regions.hdr.src_port = 0;
1882 unmap_regions.hdr.dest_port = 0;
1883 unmap_regions.hdr.token = 0;
1884 unmap_regions.hdr.opcode = ADM_CMD_SHARED_MEM_UNMAP_REGIONS;
1885 unmap_regions.mem_map_handle = atomic_read(&this_adm.
1886 mem_map_handles[atomic_read(&this_adm.mem_map_index)]);
1887 atomic_set(&this_adm.adm_stat, -1);
1888 ret = apr_send_pkt(this_adm.apr, (uint32_t *) &unmap_regions);
1889 if (ret < 0) {
1890 pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__,
1891 unmap_regions.hdr.opcode, ret);
1892 ret = -EINVAL;
1893 goto fail_cmd;
1894 }
1895
1896 ret = wait_event_timeout(this_adm.adm_wait,
1897 atomic_read(&this_adm.adm_stat) >= 0,
1898 5 * HZ);
1899 if (!ret) {
1900 pr_err("%s: timeout. waited for memory_unmap\n",
1901 __func__);
1902 ret = -EINVAL;
1903 goto fail_cmd;
1904 } else if (atomic_read(&this_adm.adm_stat) > 0) {
1905 pr_err("%s: DSP returned error[%s]\n",
1906 __func__, adsp_err_get_err_str(
1907 atomic_read(&this_adm.adm_stat)));
1908 ret = adsp_err_get_lnx_err_code(
1909 atomic_read(&this_adm.adm_stat));
1910 goto fail_cmd;
1911 } else {
1912 pr_debug("%s: Unmap handle 0x%x succeeded\n", __func__,
1913 unmap_regions.mem_map_handle);
1914 }
1915fail_cmd:
1916 return ret;
1917}
1918
1919static int remap_cal_data(struct cal_block_data *cal_block, int cal_index)
1920{
1921 int ret = 0;
1922
Banajit Goswami08bb7362017-11-03 22:48:23 -07001923 if (cal_block->map_data.dma_buf == NULL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301924 pr_err("%s: No ION allocation for cal index %d!\n",
1925 __func__, cal_index);
1926 ret = -EINVAL;
1927 goto done;
1928 }
1929
1930 if ((cal_block->map_data.map_size > 0) &&
1931 (cal_block->map_data.q6map_handle == 0)) {
1932 atomic_set(&this_adm.mem_map_index, cal_index);
1933 ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
1934 (uint32_t *)&cal_block->map_data.map_size, 1);
1935 if (ret < 0) {
1936 pr_err("%s: ADM mmap did not work! size = %zd ret %d\n",
1937 __func__,
1938 cal_block->map_data.map_size, ret);
1939 pr_debug("%s: ADM mmap did not work! addr = 0x%pK, size = %zd ret %d\n",
1940 __func__,
1941 &cal_block->cal_data.paddr,
1942 cal_block->map_data.map_size, ret);
1943 goto done;
1944 }
1945 cal_block->map_data.q6map_handle = atomic_read(&this_adm.
1946 mem_map_handles[cal_index]);
1947 }
1948done:
1949 return ret;
1950}
1951
1952static void send_adm_custom_topology(void)
1953{
1954 struct cal_block_data *cal_block = NULL;
1955 struct cmd_set_topologies adm_top;
1956 int cal_index = ADM_CUSTOM_TOP_CAL;
1957 int result;
1958
1959 if (this_adm.cal_data[cal_index] == NULL)
1960 goto done;
1961
1962 mutex_lock(&this_adm.cal_data[cal_index]->lock);
1963 if (!this_adm.set_custom_topology)
1964 goto unlock;
1965 this_adm.set_custom_topology = 0;
1966
1967 cal_block = cal_utils_get_only_cal_block(this_adm.cal_data[cal_index]);
Vikram Panduranga770b8382017-09-27 12:17:36 -07001968 if (cal_block == NULL || cal_utils_is_cal_stale(cal_block))
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301969 goto unlock;
1970
1971 pr_debug("%s: Sending cal_index %d\n", __func__, cal_index);
1972
1973 result = remap_cal_data(cal_block, cal_index);
1974 if (result) {
1975 pr_err("%s: Remap_cal_data failed for cal %d!\n",
1976 __func__, cal_index);
1977 goto unlock;
1978 }
1979 atomic_set(&this_adm.mem_map_index, cal_index);
1980 atomic_set(&this_adm.mem_map_handles[cal_index],
1981 cal_block->map_data.q6map_handle);
1982
1983 if (cal_block->cal_data.size == 0) {
1984 pr_debug("%s: No ADM cal to send\n", __func__);
1985 goto unlock;
1986 }
1987
1988 adm_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1989 APR_HDR_LEN(20), APR_PKT_VER);
1990 adm_top.hdr.pkt_size = sizeof(adm_top);
1991 adm_top.hdr.src_svc = APR_SVC_ADM;
1992 adm_top.hdr.src_domain = APR_DOMAIN_APPS;
1993 adm_top.hdr.src_port = 0;
1994 adm_top.hdr.dest_svc = APR_SVC_ADM;
1995 adm_top.hdr.dest_domain = APR_DOMAIN_ADSP;
1996 adm_top.hdr.dest_port = 0;
1997 adm_top.hdr.token = 0;
1998 adm_top.hdr.opcode = ADM_CMD_ADD_TOPOLOGIES;
1999 adm_top.payload_addr_lsw = lower_32_bits(cal_block->cal_data.paddr);
2000 adm_top.payload_addr_msw = msm_audio_populate_upper_32_bits(
2001 cal_block->cal_data.paddr);
2002 adm_top.mem_map_handle = cal_block->map_data.q6map_handle;
2003 adm_top.payload_size = cal_block->cal_data.size;
2004
2005 atomic_set(&this_adm.adm_stat, -1);
2006 pr_debug("%s: Sending ADM_CMD_ADD_TOPOLOGIES payload = 0x%pK, size = %d\n",
2007 __func__, &cal_block->cal_data.paddr,
2008 adm_top.payload_size);
2009 result = apr_send_pkt(this_adm.apr, (uint32_t *)&adm_top);
2010 if (result < 0) {
2011 pr_err("%s: Set topologies failed payload size = %zd result %d\n",
2012 __func__, cal_block->cal_data.size, result);
2013 goto unlock;
2014 }
2015 /* Wait for the callback */
2016 result = wait_event_timeout(this_adm.adm_wait,
2017 atomic_read(&this_adm.adm_stat) >= 0,
2018 msecs_to_jiffies(TIMEOUT_MS));
2019 if (!result) {
2020 pr_err("%s: Set topologies timed out payload size = %zd\n",
2021 __func__, cal_block->cal_data.size);
2022 goto unlock;
2023 } else if (atomic_read(&this_adm.adm_stat) > 0) {
2024 pr_err("%s: DSP returned error[%s]\n",
2025 __func__, adsp_err_get_err_str(
2026 atomic_read(&this_adm.adm_stat)));
2027 result = adsp_err_get_lnx_err_code(
2028 atomic_read(&this_adm.adm_stat));
2029 goto unlock;
2030 }
2031unlock:
2032 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
2033done:
2034 return;
2035}
2036
2037static int send_adm_cal_block(int port_id, int copp_idx,
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002038 struct cal_block_data *cal_block, int perf_mode)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302039{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002040 struct mem_mapping_hdr mem_hdr;
2041 int payload_size = 0;
2042 int port_idx = 0;
2043 int topology = 0;
2044 int result = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302045
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002046 pr_debug("%s: Port id 0x%x,\n", __func__, port_id);
2047
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302048 if (!cal_block) {
2049 pr_debug("%s: No ADM cal to send for port_id = 0x%x!\n",
2050 __func__, port_id);
2051 result = -EINVAL;
2052 goto done;
2053 }
2054 if (cal_block->cal_data.size <= 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002055 pr_debug("%s: No ADM cal sent for port_id = 0x%x!\n", __func__,
2056 port_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302057 result = -EINVAL;
2058 goto done;
2059 }
2060
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002061 memset(&mem_hdr, 0, sizeof(mem_hdr));
2062 port_id = afe_convert_virtual_to_portid(port_id);
2063 port_idx = adm_validate_and_get_port_index(port_id);
2064 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
2065 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2066 return -EINVAL;
2067 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
2068 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
2069 return -EINVAL;
2070 }
2071
2072 topology = atomic_read(&this_adm.copp.topology[port_idx][copp_idx]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302073 if (perf_mode == LEGACY_PCM_MODE &&
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002074 topology == DS2_ADM_COPP_TOPOLOGY_ID) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302075 pr_err("%s: perf_mode %d, topology 0x%x\n", __func__, perf_mode,
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002076 topology);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302077 goto done;
2078 }
2079
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002080 mem_hdr.data_payload_addr_lsw =
2081 lower_32_bits(cal_block->cal_data.paddr);
2082 mem_hdr.data_payload_addr_msw =
2083 msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
2084 mem_hdr.mem_map_handle = cal_block->map_data.q6map_handle;
2085 payload_size = cal_block->cal_data.size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302086
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002087 adm_set_pp_params(port_id, copp_idx, &mem_hdr, NULL, payload_size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302088
2089done:
2090 return result;
2091}
2092
2093static struct cal_block_data *adm_find_cal_by_path(int cal_index, int path)
2094{
2095 struct list_head *ptr, *next;
2096 struct cal_block_data *cal_block = NULL;
2097 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2098 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2099
2100 pr_debug("%s:\n", __func__);
2101
2102 list_for_each_safe(ptr, next,
2103 &this_adm.cal_data[cal_index]->cal_blocks) {
2104
2105 cal_block = list_entry(ptr,
2106 struct cal_block_data, list);
2107
Vikram Panduranga770b8382017-09-27 12:17:36 -07002108 if (cal_utils_is_cal_stale(cal_block))
2109 continue;
2110
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302111 if (cal_index == ADM_AUDPROC_CAL ||
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002112 cal_index == ADM_LSM_AUDPROC_CAL ||
2113 cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302114 audproc_cal_info = cal_block->cal_info;
2115 if ((audproc_cal_info->path == path) &&
2116 (cal_block->cal_data.size > 0))
2117 return cal_block;
2118 } else if (cal_index == ADM_AUDVOL_CAL) {
2119 audvol_cal_info = cal_block->cal_info;
2120 if ((audvol_cal_info->path == path) &&
2121 (cal_block->cal_data.size > 0))
2122 return cal_block;
2123 }
2124 }
2125 pr_debug("%s: Can't find ADM cal for cal_index %d, path %d\n",
2126 __func__, cal_index, path);
2127 return NULL;
2128}
2129
2130static struct cal_block_data *adm_find_cal_by_app_type(int cal_index, int path,
2131 int app_type)
2132{
2133 struct list_head *ptr, *next;
2134 struct cal_block_data *cal_block = NULL;
2135 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2136 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2137
2138 pr_debug("%s\n", __func__);
2139
2140 list_for_each_safe(ptr, next,
2141 &this_adm.cal_data[cal_index]->cal_blocks) {
2142
2143 cal_block = list_entry(ptr,
2144 struct cal_block_data, list);
2145
Vikram Panduranga770b8382017-09-27 12:17:36 -07002146 if (cal_utils_is_cal_stale(cal_block))
2147 continue;
2148
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302149 if (cal_index == ADM_AUDPROC_CAL ||
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002150 cal_index == ADM_LSM_AUDPROC_CAL ||
2151 cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302152 audproc_cal_info = cal_block->cal_info;
2153 if ((audproc_cal_info->path == path) &&
2154 (audproc_cal_info->app_type == app_type) &&
2155 (cal_block->cal_data.size > 0))
2156 return cal_block;
2157 } else if (cal_index == ADM_AUDVOL_CAL) {
2158 audvol_cal_info = cal_block->cal_info;
2159 if ((audvol_cal_info->path == path) &&
2160 (audvol_cal_info->app_type == app_type) &&
2161 (cal_block->cal_data.size > 0))
2162 return cal_block;
2163 }
2164 }
2165 pr_debug("%s: Can't find ADM cali for cal_index %d, path %d, app %d, defaulting to search by path\n",
2166 __func__, cal_index, path, app_type);
2167 return adm_find_cal_by_path(cal_index, path);
2168}
2169
2170
2171static struct cal_block_data *adm_find_cal(int cal_index, int path,
2172 int app_type, int acdb_id,
2173 int sample_rate)
2174{
2175 struct list_head *ptr, *next;
2176 struct cal_block_data *cal_block = NULL;
2177 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2178 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2179
2180 pr_debug("%s:\n", __func__);
2181
2182 list_for_each_safe(ptr, next,
2183 &this_adm.cal_data[cal_index]->cal_blocks) {
2184
2185 cal_block = list_entry(ptr,
2186 struct cal_block_data, list);
Vikram Panduranga770b8382017-09-27 12:17:36 -07002187 if (cal_utils_is_cal_stale(cal_block))
2188 continue;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302189
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302190 if (cal_index == ADM_AUDPROC_CAL ||
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002191 cal_index == ADM_LSM_AUDPROC_CAL ||
2192 cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302193 audproc_cal_info = cal_block->cal_info;
2194 if ((audproc_cal_info->path == path) &&
2195 (audproc_cal_info->app_type == app_type) &&
2196 (audproc_cal_info->acdb_id == acdb_id) &&
2197 (audproc_cal_info->sample_rate == sample_rate) &&
2198 (cal_block->cal_data.size > 0))
2199 return cal_block;
2200 } else if (cal_index == ADM_AUDVOL_CAL) {
2201 audvol_cal_info = cal_block->cal_info;
2202 if ((audvol_cal_info->path == path) &&
2203 (audvol_cal_info->app_type == app_type) &&
2204 (audvol_cal_info->acdb_id == acdb_id) &&
2205 (cal_block->cal_data.size > 0))
2206 return cal_block;
2207 }
2208 }
2209 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",
2210 __func__, cal_index, path, app_type, acdb_id, sample_rate);
2211 return adm_find_cal_by_app_type(cal_index, path, app_type);
2212}
2213
2214static int adm_remap_and_send_cal_block(int cal_index, int port_id,
2215 int copp_idx, struct cal_block_data *cal_block, int perf_mode,
2216 int app_type, int acdb_id, int sample_rate)
2217{
2218 int ret = 0;
2219
2220 pr_debug("%s: Sending cal_index cal %d\n", __func__, cal_index);
2221 ret = remap_cal_data(cal_block, cal_index);
2222 if (ret) {
2223 pr_err("%s: Remap_cal_data failed for cal %d!\n",
2224 __func__, cal_index);
2225 goto done;
2226 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08002227 ret = send_adm_cal_block(port_id, copp_idx, cal_block, perf_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302228 if (ret < 0)
2229 pr_debug("%s: No cal sent for cal_index %d, port_id = 0x%x! ret %d sample_rate %d\n",
2230 __func__, cal_index, port_id, ret, sample_rate);
2231done:
2232 return ret;
2233}
2234
2235static void send_adm_cal_type(int cal_index, int path, int port_id,
2236 int copp_idx, int perf_mode, int app_type,
2237 int acdb_id, int sample_rate)
2238{
2239 struct cal_block_data *cal_block = NULL;
2240 int ret;
2241
2242 pr_debug("%s: cal index %d\n", __func__, cal_index);
2243
2244 if (this_adm.cal_data[cal_index] == NULL) {
2245 pr_debug("%s: cal_index %d not allocated!\n",
2246 __func__, cal_index);
2247 goto done;
2248 }
2249
2250 mutex_lock(&this_adm.cal_data[cal_index]->lock);
2251 cal_block = adm_find_cal(cal_index, path, app_type, acdb_id,
2252 sample_rate);
2253 if (cal_block == NULL)
2254 goto unlock;
2255
2256 ret = adm_remap_and_send_cal_block(cal_index, port_id, copp_idx,
2257 cal_block, perf_mode, app_type, acdb_id, sample_rate);
Vikram Panduranga770b8382017-09-27 12:17:36 -07002258
2259 cal_utils_mark_cal_used(cal_block);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302260unlock:
2261 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
2262done:
2263 return;
2264}
2265
2266static int get_cal_path(int path)
2267{
2268 if (path == 0x1)
2269 return RX_DEVICE;
2270 else
2271 return TX_DEVICE;
2272}
2273
2274static void send_adm_cal(int port_id, int copp_idx, int path, int perf_mode,
Aditya Bavanari5106b562018-01-08 13:16:32 +05302275 int app_type, int acdb_id, int sample_rate,
2276 int passthr_mode)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302277{
2278 pr_debug("%s: port id 0x%x copp_idx %d\n", __func__, port_id, copp_idx);
2279
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002280 if (passthr_mode != LISTEN) {
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302281 send_adm_cal_type(ADM_AUDPROC_CAL, path, port_id, copp_idx,
2282 perf_mode, app_type, acdb_id, sample_rate);
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002283 } else {
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302284 send_adm_cal_type(ADM_LSM_AUDPROC_CAL, path, port_id, copp_idx,
2285 perf_mode, app_type, acdb_id, sample_rate);
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07002286
2287 send_adm_cal_type(ADM_LSM_AUDPROC_PERSISTENT_CAL, path,
2288 port_id, copp_idx, perf_mode, app_type,
2289 acdb_id, sample_rate);
2290 }
2291
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302292 send_adm_cal_type(ADM_AUDVOL_CAL, path, port_id, copp_idx, perf_mode,
2293 app_type, acdb_id, sample_rate);
2294}
2295
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302296/**
2297 * adm_connect_afe_port -
2298 * command to send ADM connect AFE port
2299 *
2300 * @mode: value of mode for ADM connect AFE
2301 * @session_id: session active to connect
2302 * @port_id: Port ID number
2303 *
2304 * Returns 0 on success or error on failure
2305 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302306int adm_connect_afe_port(int mode, int session_id, int port_id)
2307{
2308 struct adm_cmd_connect_afe_port_v5 cmd;
2309 int ret = 0;
2310 int port_idx, copp_idx = 0;
2311
2312 pr_debug("%s: port_id: 0x%x session id:%d mode:%d\n", __func__,
2313 port_id, session_id, mode);
2314
2315 port_id = afe_convert_virtual_to_portid(port_id);
2316 port_idx = adm_validate_and_get_port_index(port_id);
2317 if (port_idx < 0) {
2318 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2319 return -EINVAL;
2320 }
2321
2322 if (this_adm.apr == NULL) {
2323 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
2324 0xFFFFFFFF, &this_adm);
2325 if (this_adm.apr == NULL) {
2326 pr_err("%s: Unable to register ADM\n", __func__);
2327 ret = -ENODEV;
2328 return ret;
2329 }
2330 rtac_set_adm_handle(this_adm.apr);
2331 }
2332 pr_debug("%s: Port ID 0x%x, index %d\n", __func__, port_id, port_idx);
2333
2334 cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2335 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
2336 cmd.hdr.pkt_size = sizeof(cmd);
2337 cmd.hdr.src_svc = APR_SVC_ADM;
2338 cmd.hdr.src_domain = APR_DOMAIN_APPS;
2339 cmd.hdr.src_port = port_id;
2340 cmd.hdr.dest_svc = APR_SVC_ADM;
2341 cmd.hdr.dest_domain = APR_DOMAIN_ADSP;
2342 cmd.hdr.dest_port = 0; /* Ignored */
2343 cmd.hdr.token = port_idx << 16 | copp_idx;
2344 cmd.hdr.opcode = ADM_CMD_CONNECT_AFE_PORT_V5;
2345
2346 cmd.mode = mode;
2347 cmd.session_id = session_id;
2348 cmd.afe_port_id = port_id;
2349
2350 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2351 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&cmd);
2352 if (ret < 0) {
2353 pr_err("%s: ADM enable for port_id: 0x%x failed ret %d\n",
2354 __func__, port_id, ret);
2355 ret = -EINVAL;
2356 goto fail_cmd;
2357 }
2358 /* Wait for the callback with copp id */
2359 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
2360 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
2361 msecs_to_jiffies(TIMEOUT_MS));
2362 if (!ret) {
2363 pr_err("%s: ADM connect timedout for port_id: 0x%x\n",
2364 __func__, port_id);
2365 ret = -EINVAL;
2366 goto fail_cmd;
2367 } else if (atomic_read(&this_adm.copp.stat
2368 [port_idx][copp_idx]) > 0) {
2369 pr_err("%s: DSP returned error[%s]\n",
2370 __func__, adsp_err_get_err_str(
2371 atomic_read(&this_adm.copp.stat
2372 [port_idx][copp_idx])));
2373 ret = adsp_err_get_lnx_err_code(
2374 atomic_read(&this_adm.copp.stat
2375 [port_idx][copp_idx]));
2376 goto fail_cmd;
2377 }
2378 atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
2379 return 0;
2380
2381fail_cmd:
2382
2383 return ret;
2384}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302385EXPORT_SYMBOL(adm_connect_afe_port);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302386
2387int adm_arrange_mch_map(struct adm_cmd_device_open_v5 *open, int path,
2388 int channel_mode)
2389{
2390 int rc = 0, idx;
2391
Bhalchandra Gajareeed46bd2018-05-15 16:48:07 -07002392 pr_debug("%s: channel mode %d", __func__, channel_mode);
2393
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302394 memset(open->dev_channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
2395 switch (path) {
2396 case ADM_PATH_PLAYBACK:
2397 idx = ADM_MCH_MAP_IDX_PLAYBACK;
2398 break;
2399 case ADM_PATH_LIVE_REC:
2400 case ADM_PATH_NONLIVE_REC:
2401 idx = ADM_MCH_MAP_IDX_REC;
2402 break;
2403 default:
2404 goto non_mch_path;
2405 };
2406 if ((open->dev_num_channel > 2) && multi_ch_maps[idx].set_channel_map) {
2407 memcpy(open->dev_channel_mapping,
2408 multi_ch_maps[idx].channel_mapping,
2409 PCM_FORMAT_MAX_NUM_CHANNEL);
2410 } else {
2411 if (channel_mode == 1) {
2412 open->dev_channel_mapping[0] = PCM_CHANNEL_FC;
2413 } else if (channel_mode == 2) {
2414 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2415 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2416 } else if (channel_mode == 3) {
2417 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2418 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2419 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2420 } else if (channel_mode == 4) {
2421 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2422 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2423 open->dev_channel_mapping[2] = PCM_CHANNEL_LS;
2424 open->dev_channel_mapping[3] = PCM_CHANNEL_RS;
2425 } else if (channel_mode == 5) {
2426 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2427 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2428 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2429 open->dev_channel_mapping[3] = PCM_CHANNEL_LS;
2430 open->dev_channel_mapping[4] = PCM_CHANNEL_RS;
2431 } else if (channel_mode == 6) {
2432 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2433 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2434 open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2435 open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2436 open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2437 open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2438 } else if (channel_mode == 7) {
2439 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2440 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2441 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2442 open->dev_channel_mapping[3] = PCM_CHANNEL_LFE;
2443 open->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2444 open->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2445 open->dev_channel_mapping[6] = PCM_CHANNEL_CS;
2446 } else if (channel_mode == 8) {
2447 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2448 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2449 open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2450 open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2451 open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2452 open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2453 open->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2454 open->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2455 } else {
2456 pr_err("%s: invalid num_chan %d\n", __func__,
2457 channel_mode);
2458 rc = -EINVAL;
2459 goto inval_ch_mod;
2460 }
2461 }
2462
2463non_mch_path:
2464inval_ch_mod:
2465 return rc;
2466}
2467
2468int adm_arrange_mch_ep2_map(struct adm_cmd_device_open_v6 *open_v6,
2469 int channel_mode)
2470{
2471 int rc = 0;
2472
2473 memset(open_v6->dev_channel_mapping_eid2, 0,
2474 PCM_FORMAT_MAX_NUM_CHANNEL);
2475
2476 if (channel_mode == 1) {
2477 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FC;
2478 } else if (channel_mode == 2) {
2479 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2480 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2481 } else if (channel_mode == 3) {
2482 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2483 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2484 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
2485 } else if (channel_mode == 4) {
2486 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2487 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2488 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LS;
2489 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_RS;
2490 } else if (channel_mode == 5) {
2491 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2492 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2493 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
2494 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_LS;
2495 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_RS;
2496 } else if (channel_mode == 6) {
2497 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2498 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2499 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
2500 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
2501 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
2502 open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
2503 } else if (channel_mode == 8) {
2504 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2505 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2506 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
2507 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
2508 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
2509 open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
2510 open_v6->dev_channel_mapping_eid2[6] = PCM_CHANNEL_LB;
2511 open_v6->dev_channel_mapping_eid2[7] = PCM_CHANNEL_RB;
2512 } else {
2513 pr_err("%s: invalid num_chan %d\n", __func__,
2514 channel_mode);
2515 rc = -EINVAL;
2516 }
2517
2518 return rc;
2519}
2520
Dieter Luecking50c55352018-09-28 14:29:17 +02002521static int adm_arrange_mch_map_v8(
2522 struct adm_device_endpoint_payload *ep_payload,
2523 int path,
2524 int channel_mode)
2525{
2526 int rc = 0, idx;
2527
2528 memset(ep_payload->dev_channel_mapping,
2529 0, PCM_FORMAT_MAX_NUM_CHANNEL_V8);
2530 switch (path) {
2531 case ADM_PATH_PLAYBACK:
2532 idx = ADM_MCH_MAP_IDX_PLAYBACK;
2533 break;
2534 case ADM_PATH_LIVE_REC:
2535 case ADM_PATH_NONLIVE_REC:
2536 idx = ADM_MCH_MAP_IDX_REC;
2537 break;
2538 default:
2539 goto non_mch_path;
2540 };
2541
2542 if ((ep_payload->dev_num_channel > 2) &&
2543 multi_ch_maps[idx].set_channel_map) {
2544 memcpy(ep_payload->dev_channel_mapping,
2545 multi_ch_maps[idx].channel_mapping,
2546 PCM_FORMAT_MAX_NUM_CHANNEL_V8);
2547 } else {
2548 if (channel_mode == 1) {
2549 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FC;
2550 } else if (channel_mode == 2) {
2551 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2552 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2553 } else if (channel_mode == 3) {
2554 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2555 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2556 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2557 } else if (channel_mode == 4) {
2558 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2559 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2560 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LS;
2561 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_RS;
2562 } else if (channel_mode == 5) {
2563 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2564 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2565 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2566 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_LS;
2567 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_RS;
2568 } else if (channel_mode == 6) {
2569 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2570 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2571 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2572 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2573 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2574 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2575 } else if (channel_mode == 7) {
2576 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2577 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2578 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2579 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_LFE;
2580 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2581 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2582 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_CS;
2583 } else if (channel_mode == 8) {
2584 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2585 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2586 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2587 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2588 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2589 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2590 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2591 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2592 } else if (channel_mode == 10) {
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_LFE;
2596 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2597 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2598 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2599 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LS;
2600 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RS;
2601 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
2602 ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
2603 } else if (channel_mode == 12) {
2604 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2605 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2606 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2607 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2608 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2609 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2610 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LS;
2611 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RS;
2612 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
2613 ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
2614 ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_TSL;
2615 ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_TSR;
2616 } else if (channel_mode == 16) {
2617 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2618 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2619 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2620 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2621 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2622 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2623 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LS;
2624 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RS;
2625 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
2626 ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
2627 ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_TSL;
2628 ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_TSR;
2629 ep_payload->dev_channel_mapping[12] = PCM_CHANNEL_FLC;
2630 ep_payload->dev_channel_mapping[13] = PCM_CHANNEL_FRC;
2631 ep_payload->dev_channel_mapping[14] = PCM_CHANNEL_RLC;
2632 ep_payload->dev_channel_mapping[15] = PCM_CHANNEL_RRC;
2633 } else {
2634 pr_err("%s: invalid num_chan %d\n", __func__,
2635 channel_mode);
2636 rc = -EINVAL;
2637 goto inval_ch_mod;
2638 }
2639 }
2640
2641non_mch_path:
2642inval_ch_mod:
2643 return rc;
2644}
2645
2646static int adm_arrange_mch_ep2_map_v8(
2647 struct adm_device_endpoint_payload *ep_payload,
2648 int channel_mode)
2649{
2650 int rc = 0;
2651
2652 memset(ep_payload->dev_channel_mapping, 0,
2653 PCM_FORMAT_MAX_NUM_CHANNEL_V8);
2654
2655 if (channel_mode == 1) {
2656 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FC;
2657 } else if (channel_mode == 2) {
2658 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2659 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2660 } else if (channel_mode == 3) {
2661 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2662 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2663 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2664 } else if (channel_mode == 4) {
2665 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2666 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2667 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LS;
2668 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_RS;
2669 } else if (channel_mode == 5) {
2670 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2671 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2672 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2673 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_LS;
2674 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_RS;
2675 } else if (channel_mode == 6) {
2676 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2677 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2678 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2679 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2680 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2681 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2682 } else if (channel_mode == 8) {
2683 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2684 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2685 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2686 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2687 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2688 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2689 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2690 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2691 } else if (channel_mode == 10) {
2692 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2693 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2694 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2695 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2696 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2697 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2698 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2699 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2700 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_CS;
2701 ep_payload->dev_channel_mapping[9] = PCM_CHANNELS;
2702 } else if (channel_mode == 12) {
2703 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2704 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2705 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2706 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2707 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2708 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2709 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2710 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2711 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
2712 ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
2713 ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_TSL;
2714 ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_TSR;
2715 } else if (channel_mode == 16) {
2716 ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2717 ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2718 ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2719 ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2720 ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2721 ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2722 ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2723 ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2724 ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_CS;
2725 ep_payload->dev_channel_mapping[9] = PCM_CHANNELS;
2726 ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_CVH;
2727 ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_MS;
2728 ep_payload->dev_channel_mapping[12] = PCM_CHANNEL_FLC;
2729 ep_payload->dev_channel_mapping[13] = PCM_CHANNEL_FRC;
2730 ep_payload->dev_channel_mapping[14] = PCM_CHANNEL_RLC;
2731 ep_payload->dev_channel_mapping[15] = PCM_CHANNEL_RRC;
2732 } else {
2733 pr_err("%s: invalid num_chan %d\n", __func__,
2734 channel_mode);
2735 rc = -EINVAL;
2736 }
2737
2738 return rc;
2739}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302740/**
2741 * adm_open -
2742 * command to send ADM open
2743 *
2744 * @port_id: port id number
2745 * @path: direction or ADM path type
2746 * @rate: sample rate of session
2747 * @channel_mode: number of channels set
2748 * @topology: topology active for this session
2749 * @perf_mode: performance mode like LL/ULL/..
2750 * @bit_width: bit width to set for copp
2751 * @app_type: App type used for this session
2752 * @acdb_id: ACDB ID of this device
2753 *
2754 * Returns 0 on success or error on failure
2755 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302756int adm_open(int port_id, int path, int rate, int channel_mode, int topology,
2757 int perf_mode, uint16_t bit_width, int app_type, int acdb_id)
2758{
2759 struct adm_cmd_device_open_v5 open;
2760 struct adm_cmd_device_open_v6 open_v6;
Dieter Luecking50c55352018-09-28 14:29:17 +02002761 struct adm_cmd_device_open_v8 open_v8;
2762 struct adm_device_endpoint_payload ep1_payload;
2763 struct adm_device_endpoint_payload ep2_payload;
2764 int ep1_payload_size = 0;
2765 int ep2_payload_size = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302766 int ret = 0;
Asish Bhattacharya34504582017-08-08 12:55:01 +05302767 int port_idx, flags;
2768 int copp_idx = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302769 int tmp_port = q6audio_get_port_id(port_id);
Dieter Luecking50c55352018-09-28 14:29:17 +02002770 void *adm_params = NULL;
2771 int param_size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302772
2773 pr_debug("%s:port %#x path:%d rate:%d mode:%d perf_mode:%d,topo_id %d\n",
2774 __func__, port_id, path, rate, channel_mode, perf_mode,
2775 topology);
2776
2777 port_id = q6audio_convert_virtual_to_portid(port_id);
2778 port_idx = adm_validate_and_get_port_index(port_id);
2779 if (port_idx < 0) {
2780 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2781 return -EINVAL;
2782 }
Dieter Luecking50c55352018-09-28 14:29:17 +02002783 if (channel_mode < 0 || channel_mode > 32) {
2784 pr_err("%s: Invalid channel number 0x%x\n",
2785 __func__, channel_mode);
2786 return -EINVAL;
2787 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302788
2789 if (this_adm.apr == NULL) {
2790 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
2791 0xFFFFFFFF, &this_adm);
2792 if (this_adm.apr == NULL) {
2793 pr_err("%s: Unable to register ADM\n", __func__);
2794 return -ENODEV;
2795 }
2796 rtac_set_adm_handle(this_adm.apr);
2797 }
2798
2799 if (perf_mode == ULL_POST_PROCESSING_PCM_MODE) {
2800 flags = ADM_ULL_POST_PROCESSING_DEVICE_SESSION;
2801 if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
2802 (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
2803 (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
2804 topology = DEFAULT_COPP_TOPOLOGY;
2805 } else if (perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) {
2806 flags = ADM_ULTRA_LOW_LATENCY_DEVICE_SESSION;
2807 topology = NULL_COPP_TOPOLOGY;
2808 rate = ULL_SUPPORTED_SAMPLE_RATE;
2809 bit_width = ULL_SUPPORTED_BITS_PER_SAMPLE;
2810 } else if (perf_mode == LOW_LATENCY_PCM_MODE) {
2811 flags = ADM_LOW_LATENCY_DEVICE_SESSION;
2812 if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
2813 (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
2814 (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
2815 topology = DEFAULT_COPP_TOPOLOGY;
2816 } else {
2817 if ((path == ADM_PATH_COMPRESSED_RX) ||
2818 (path == ADM_PATH_COMPRESSED_TX))
2819 flags = 0;
2820 else
2821 flags = ADM_LEGACY_DEVICE_SESSION;
2822 }
2823
Laxminath Kasam8f7ccc22017-08-28 17:35:04 +05302824 if ((topology == VPM_TX_SM_ECNS_V2_COPP_TOPOLOGY) ||
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302825 (topology == VPM_TX_DM_FLUENCE_COPP_TOPOLOGY) ||
2826 (topology == VPM_TX_DM_RFECNS_COPP_TOPOLOGY))
2827 rate = 16000;
2828
kunleizc0cd19e2018-10-18 17:11:35 +08002829 if (topology == VPM_TX_VOICE_SMECNS_V2_COPP_TOPOLOGY)
2830 channel_mode = 1;
2831
Asish Bhattacharya34504582017-08-08 12:55:01 +05302832 /*
2833 * Routing driver reuses the same adm for streams with the same
2834 * app_type, sample_rate etc.
2835 * This isn't allowed for ULL streams as per the DSP interface
2836 */
2837 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE)
2838 copp_idx = adm_get_idx_if_copp_exists(port_idx, topology,
2839 perf_mode,
2840 rate, bit_width,
2841 app_type);
2842
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302843 if (copp_idx < 0) {
2844 copp_idx = adm_get_next_available_copp(port_idx);
2845 if (copp_idx >= MAX_COPPS_PER_PORT) {
2846 pr_err("%s: exceeded copp id %d\n",
2847 __func__, copp_idx);
2848 return -EINVAL;
2849 }
2850 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
2851 atomic_set(&this_adm.copp.topology[port_idx][copp_idx],
2852 topology);
2853 atomic_set(&this_adm.copp.mode[port_idx][copp_idx],
2854 perf_mode);
2855 atomic_set(&this_adm.copp.rate[port_idx][copp_idx],
2856 rate);
2857 atomic_set(&this_adm.copp.channels[port_idx][copp_idx],
2858 channel_mode);
2859 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx],
2860 bit_width);
2861 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx],
2862 app_type);
2863 atomic_set(&this_adm.copp.acdb_id[port_idx][copp_idx],
2864 acdb_id);
2865 set_bit(ADM_STATUS_CALIBRATION_REQUIRED,
2866 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
2867 if ((path != ADM_PATH_COMPRESSED_RX) &&
2868 (path != ADM_PATH_COMPRESSED_TX))
2869 send_adm_custom_topology();
2870 }
2871
2872 if (this_adm.copp.adm_delay[port_idx][copp_idx] &&
2873 perf_mode == LEGACY_PCM_MODE) {
2874 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
2875 1);
2876 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
2877 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
2878 }
2879
2880 /* Create a COPP if port id are not enabled */
2881 if (atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]) == 0) {
2882 pr_debug("%s: open ADM: port_idx: %d, copp_idx: %d\n", __func__,
2883 port_idx, copp_idx);
Dieter Luecking50c55352018-09-28 14:29:17 +02002884 if ((topology == SRS_TRUMEDIA_TOPOLOGY_ID) &&
2885 perf_mode == LEGACY_PCM_MODE) {
2886 int res;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302887
Dieter Luecking50c55352018-09-28 14:29:17 +02002888 atomic_set(&this_adm.mem_map_index, ADM_SRS_TRUMEDIA);
2889 msm_dts_srs_tm_ion_memmap(&this_adm.outband_memmap);
2890 res = adm_memory_map_regions(
2891 &this_adm.outband_memmap.paddr, 0,
2892 (uint32_t *)&this_adm.outband_memmap.size, 1);
2893 if (res < 0) {
2894 pr_err("%s: SRS adm_memory_map_regions failed! addr = 0x%pK, size = %d\n",
2895 __func__,
2896 (void *)this_adm.outband_memmap.paddr,
2897 (uint32_t)this_adm.outband_memmap.size);
2898 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302899 }
2900
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302901
Mangesh Kunchamwar9f295c72018-10-08 18:20:41 +05302902 if ((q6core_get_avcs_api_version_per_service(
Dieter Luecking50c55352018-09-28 14:29:17 +02002903 APRV2_IDS_SERVICE_ID_ADSP_ADM_V) >=
Mangesh Kunchamwar9f295c72018-10-08 18:20:41 +05302904 ADSP_ADM_API_VERSION_V3) &&
2905 q6core_use_Q6_32ch_support()) {
Dieter Luecking50c55352018-09-28 14:29:17 +02002906 memset(&open_v8, 0, sizeof(open_v8));
2907 memset(&ep1_payload, 0, sizeof(ep1_payload));
2908 memset(&ep2_payload, 0, sizeof(ep2_payload));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302909
Dieter Luecking50c55352018-09-28 14:29:17 +02002910 open_v8.hdr.hdr_field = APR_HDR_FIELD(
2911 APR_MSG_TYPE_SEQ_CMD,
2912 APR_HDR_LEN(APR_HDR_SIZE),
2913 APR_PKT_VER);
2914 open_v8.hdr.src_svc = APR_SVC_ADM;
2915 open_v8.hdr.src_domain = APR_DOMAIN_APPS;
2916 open_v8.hdr.src_port = tmp_port;
2917 open_v8.hdr.dest_svc = APR_SVC_ADM;
2918 open_v8.hdr.dest_domain = APR_DOMAIN_ADSP;
2919 open_v8.hdr.dest_port = tmp_port;
2920 open_v8.hdr.token = port_idx << 16 | copp_idx;
2921 open_v8.hdr.opcode = ADM_CMD_DEVICE_OPEN_V8;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302922
Dieter Luecking50c55352018-09-28 14:29:17 +02002923 if (this_adm.native_mode != 0) {
2924 open_v8.flags = flags |
2925 (this_adm.native_mode << 11);
2926 this_adm.native_mode = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302927 } else {
Dieter Luecking50c55352018-09-28 14:29:17 +02002928 open_v8.flags = flags;
2929 }
2930 open_v8.mode_of_operation = path;
2931 open_v8.endpoint_id_1 = tmp_port;
2932 open_v8.endpoint_id_2 = 0xFFFF;
2933 open_v8.endpoint_id_3 = 0xFFFF;
2934
Dieter Luecking41620832018-11-14 15:11:47 +01002935 if (this_adm.ec_ref_rx && (path != ADM_PATH_PLAYBACK)) {
2936 open_v8.endpoint_id_2 = this_adm.ec_ref_rx;
2937 this_adm.ec_ref_rx = -1;
2938 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302939
Dieter Luecking50c55352018-09-28 14:29:17 +02002940 open_v8.topology_id = topology;
2941 open_v8.reserved = 0;
2942
2943 /* variable endpoint payload */
2944 ep1_payload.dev_num_channel = channel_mode & 0x00FF;
2945 ep1_payload.bit_width = bit_width;
2946 ep1_payload.sample_rate = rate;
2947 ret = adm_arrange_mch_map_v8(&ep1_payload, path,
2948 channel_mode);
2949 if (ret)
2950 return ret;
2951
2952 pr_debug("%s: port_id=0x%x %x %x topology_id=0x%X flags %x ref_ch %x\n",
2953 __func__, open_v8.endpoint_id_1,
2954 open_v8.endpoint_id_2,
2955 open_v8.endpoint_id_3,
2956 open_v8.topology_id,
2957 open_v8.flags,
2958 this_adm.num_ec_ref_rx_chans);
2959
2960 ep1_payload_size = 8 +
2961 roundup(ep1_payload.dev_num_channel, 4);
2962 param_size = sizeof(struct adm_cmd_device_open_v8)
2963 + ep1_payload_size;
2964 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2965
Dieter Luecking27c57722018-10-05 16:45:41 +02002966 if ((this_adm.num_ec_ref_rx_chans != 0)
2967 && (path != ADM_PATH_PLAYBACK)
Dieter Luecking50c55352018-09-28 14:29:17 +02002968 && (open_v8.endpoint_id_2 != 0xFFFF)) {
2969 ep2_payload.dev_num_channel =
2970 this_adm.num_ec_ref_rx_chans;
Dieter Luecking50c55352018-09-28 14:29:17 +02002971
2972 if (this_adm.ec_ref_rx_bit_width != 0) {
2973 ep2_payload.bit_width =
2974 this_adm.ec_ref_rx_bit_width;
2975 this_adm.ec_ref_rx_bit_width = 0;
2976 } else {
2977 ep2_payload.bit_width = bit_width;
2978 }
2979
2980 if (this_adm.ec_ref_rx_sampling_rate != 0) {
2981 ep2_payload.sample_rate =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302982 this_adm.ec_ref_rx_sampling_rate;
Dieter Luecking50c55352018-09-28 14:29:17 +02002983 this_adm.ec_ref_rx_sampling_rate = 0;
2984 } else {
2985 ep2_payload.sample_rate = rate;
2986 }
2987
2988 pr_debug("%s: adm open_v8 eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
2989 __func__,
2990 ep2_payload.dev_num_channel,
2991 ep2_payload.bit_width,
2992 ep2_payload.sample_rate);
2993
2994 ret = adm_arrange_mch_ep2_map_v8(&ep2_payload,
2995 ep2_payload.dev_num_channel);
2996
2997 if (ret)
2998 return ret;
2999 ep2_payload_size = 8 +
3000 roundup(ep2_payload.dev_num_channel, 4);
3001 param_size += ep2_payload_size;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303002 }
3003
Dieter Luecking27c57722018-10-05 16:45:41 +02003004 open_v8.hdr.pkt_size = param_size;
Dieter Luecking50c55352018-09-28 14:29:17 +02003005 adm_params = kzalloc(param_size, GFP_KERNEL);
3006 if (!adm_params)
3007 return -ENOMEM;
Dieter Luecking50c55352018-09-28 14:29:17 +02003008 memcpy(adm_params, &open_v8, sizeof(open_v8));
3009 memcpy(adm_params + sizeof(open_v8),
3010 (void *)&ep1_payload,
3011 ep1_payload_size);
Dieter Luecking27c57722018-10-05 16:45:41 +02003012
3013 if ((this_adm.num_ec_ref_rx_chans != 0)
3014 && (path != ADM_PATH_PLAYBACK)
3015 && (open_v8.endpoint_id_2 != 0xFFFF)) {
Dieter Luecking41620832018-11-14 15:11:47 +01003016 this_adm.num_ec_ref_rx_chans = 0;
Dieter Luecking27c57722018-10-05 16:45:41 +02003017 memcpy(adm_params + sizeof(open_v8)
3018 + ep1_payload_size,
3019 (void *)&ep2_payload,
3020 ep2_payload_size);
3021 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303022
Dieter Luecking50c55352018-09-28 14:29:17 +02003023 ret = apr_send_pkt(this_adm.apr,
3024 (uint32_t *)adm_params);
3025 if (ret < 0) {
3026 pr_err("%s: port_id: 0x%x for[0x%x] failed %d for open_v8\n",
3027 __func__, tmp_port, port_id, ret);
3028 return -EINVAL;
3029 }
3030 kfree(adm_params);
3031 } else {
3032
3033 open.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3034 APR_HDR_LEN(APR_HDR_SIZE),
3035 APR_PKT_VER);
3036 open.hdr.pkt_size = sizeof(open);
3037 open.hdr.src_svc = APR_SVC_ADM;
3038 open.hdr.src_domain = APR_DOMAIN_APPS;
3039 open.hdr.src_port = tmp_port;
3040 open.hdr.dest_svc = APR_SVC_ADM;
3041 open.hdr.dest_domain = APR_DOMAIN_ADSP;
3042 open.hdr.dest_port = tmp_port;
3043 open.hdr.token = port_idx << 16 | copp_idx;
3044 open.hdr.opcode = ADM_CMD_DEVICE_OPEN_V5;
3045 open.flags = flags;
3046 open.mode_of_operation = path;
3047 open.endpoint_id_1 = tmp_port;
3048 open.endpoint_id_2 = 0xFFFF;
3049
3050 if (this_adm.ec_ref_rx && (path != 1)) {
3051 open.endpoint_id_2 = this_adm.ec_ref_rx;
3052 this_adm.ec_ref_rx = -1;
3053 }
3054
3055 open.topology_id = topology;
3056
3057 open.dev_num_channel = channel_mode & 0x00FF;
3058 open.bit_width = bit_width;
3059 WARN_ON((perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) &&
3060 (rate != ULL_SUPPORTED_SAMPLE_RATE));
3061 open.sample_rate = rate;
3062
3063 ret = adm_arrange_mch_map(&open, path, channel_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303064
3065 if (ret)
3066 return ret;
3067
Dieter Luecking50c55352018-09-28 14:29:17 +02003068 pr_debug("%s: port_id=0x%x rate=%d topology_id=0x%X\n",
3069 __func__, open.endpoint_id_1, open.sample_rate,
3070 open.topology_id);
3071
3072 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3073
3074 if ((this_adm.num_ec_ref_rx_chans != 0) &&
3075 (path != 1) && (open.endpoint_id_2 != 0xFFFF)) {
3076 memset(&open_v6, 0,
3077 sizeof(struct adm_cmd_device_open_v6));
3078 memcpy(&open_v6, &open,
3079 sizeof(struct adm_cmd_device_open_v5));
3080 open_v6.hdr.opcode = ADM_CMD_DEVICE_OPEN_V6;
3081 open_v6.hdr.pkt_size = sizeof(open_v6);
3082 open_v6.dev_num_channel_eid2 =
3083 this_adm.num_ec_ref_rx_chans;
3084 this_adm.num_ec_ref_rx_chans = 0;
3085
3086 if (this_adm.ec_ref_rx_bit_width != 0) {
3087 open_v6.bit_width_eid2 =
3088 this_adm.ec_ref_rx_bit_width;
3089 this_adm.ec_ref_rx_bit_width = 0;
3090 } else {
3091 open_v6.bit_width_eid2 = bit_width;
3092 }
3093
3094 if (this_adm.ec_ref_rx_sampling_rate != 0) {
3095 open_v6.sample_rate_eid2 =
3096 this_adm.ec_ref_rx_sampling_rate;
3097 this_adm.ec_ref_rx_sampling_rate = 0;
3098 } else {
3099 open_v6.sample_rate_eid2 = rate;
3100 }
3101
3102 pr_debug("%s: eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
3103 __func__, open_v6.dev_num_channel_eid2,
3104 open_v6.bit_width_eid2,
3105 open_v6.sample_rate_eid2);
3106
3107 ret = adm_arrange_mch_ep2_map(&open_v6,
3108 open_v6.dev_num_channel_eid2);
3109
3110 if (ret)
3111 return ret;
3112
3113 ret = apr_send_pkt(this_adm.apr,
3114 (uint32_t *)&open_v6);
3115 } else {
3116 ret = apr_send_pkt(this_adm.apr,
3117 (uint32_t *)&open);
3118 }
3119 if (ret < 0) {
3120 pr_err("%s: port_id: 0x%x for[0x%x] failed %d\n",
3121 __func__, tmp_port, port_id, ret);
3122 return -EINVAL;
3123 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303124 }
Dieter Luecking50c55352018-09-28 14:29:17 +02003125
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303126 /* Wait for the callback with copp id */
3127 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3128 atomic_read(&this_adm.copp.stat
3129 [port_idx][copp_idx]) >= 0,
3130 msecs_to_jiffies(TIMEOUT_MS));
3131 if (!ret) {
3132 pr_err("%s: ADM open timedout for port_id: 0x%x for [0x%x]\n",
3133 __func__, tmp_port, port_id);
3134 return -EINVAL;
3135 } else if (atomic_read(&this_adm.copp.stat
3136 [port_idx][copp_idx]) > 0) {
3137 pr_err("%s: DSP returned error[%s]\n",
3138 __func__, adsp_err_get_err_str(
3139 atomic_read(&this_adm.copp.stat
3140 [port_idx][copp_idx])));
3141 return adsp_err_get_lnx_err_code(
3142 atomic_read(&this_adm.copp.stat
3143 [port_idx][copp_idx]));
3144 }
3145 }
3146 atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
3147 return copp_idx;
3148}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303149EXPORT_SYMBOL(adm_open);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303150
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303151/**
3152 * adm_copp_mfc_cfg -
3153 * command to send ADM MFC config
3154 *
3155 * @port_id: Port ID number
3156 * @copp_idx: copp index assigned
3157 * @dst_sample_rate: sink sample rate
3158 *
3159 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303160void adm_copp_mfc_cfg(int port_id, int copp_idx, int dst_sample_rate)
3161{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003162 struct audproc_mfc_param_media_fmt mfc_cfg;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303163 struct adm_cmd_device_open_v5 open;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003164 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303165 int port_idx;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303166 int rc = 0;
3167 int i = 0;
3168
3169 port_id = q6audio_convert_virtual_to_portid(port_id);
3170 port_idx = adm_validate_and_get_port_index(port_id);
3171
3172 if (port_idx < 0) {
3173 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
3174 goto fail_cmd;
3175 }
3176
3177 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3178 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3179 goto fail_cmd;
3180 }
3181
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003182 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
3183 memset(&open, 0, sizeof(open));
3184 memset(&param_hdr, 0, sizeof(param_hdr));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303185
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003186 param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
3187 param_hdr.instance_id = INSTANCE_ID_0;
3188 param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
3189 param_hdr.param_size = sizeof(mfc_cfg);
3190
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303191 mfc_cfg.sampling_rate = dst_sample_rate;
3192 mfc_cfg.bits_per_sample =
3193 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
3194 open.dev_num_channel = mfc_cfg.num_channels =
3195 atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
3196
3197 rc = adm_arrange_mch_map(&open, ADM_PATH_PLAYBACK,
3198 mfc_cfg.num_channels);
3199 if (rc < 0) {
3200 pr_err("%s: unable to get channal map\n", __func__);
3201 goto fail_cmd;
3202 }
3203
3204 for (i = 0; i < mfc_cfg.num_channels; i++)
3205 mfc_cfg.channel_type[i] =
3206 (uint16_t) open.dev_channel_mapping[i];
3207
3208 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3209
3210 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",
3211 __func__, port_idx, copp_idx,
3212 atomic_read(&this_adm.copp.rate[port_idx][copp_idx]),
3213 mfc_cfg.bits_per_sample, mfc_cfg.num_channels,
3214 mfc_cfg.sampling_rate);
3215
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08003216 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
3217 (uint8_t *) &mfc_cfg);
3218 if (rc)
3219 pr_err("%s: Failed to set media format configuration data, err %d\n",
3220 __func__, rc);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303221
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303222fail_cmd:
3223 return;
3224}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303225EXPORT_SYMBOL(adm_copp_mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303226
3227static void route_set_opcode_matrix_id(
3228 struct adm_cmd_matrix_map_routings_v5 **route_addr,
3229 int path, uint32_t passthr_mode)
3230{
3231 struct adm_cmd_matrix_map_routings_v5 *route = *route_addr;
3232
3233 switch (path) {
3234 case ADM_PATH_PLAYBACK:
3235 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
3236 route->matrix_id = ADM_MATRIX_ID_AUDIO_RX;
3237 break;
3238 case ADM_PATH_LIVE_REC:
3239 if (passthr_mode == LISTEN) {
3240 route->hdr.opcode =
3241 ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3242 route->matrix_id = ADM_MATRIX_ID_LISTEN_TX;
3243 break;
3244 }
3245 /* fall through to set matrix id for non-listen case */
3246 case ADM_PATH_NONLIVE_REC:
3247 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
3248 route->matrix_id = ADM_MATRIX_ID_AUDIO_TX;
3249 break;
3250 case ADM_PATH_COMPRESSED_RX:
3251 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3252 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_RX;
3253 break;
3254 case ADM_PATH_COMPRESSED_TX:
3255 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3256 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_TX;
3257 break;
3258 default:
3259 pr_err("%s: Wrong path set[%d]\n", __func__, path);
3260 break;
3261 }
3262 pr_debug("%s: opcode 0x%x, matrix id %d\n",
3263 __func__, route->hdr.opcode, route->matrix_id);
3264}
3265
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303266/**
3267 * adm_matrix_map -
3268 * command to send ADM matrix map for ADM copp list
3269 *
3270 * @path: direction or ADM path type
3271 * @payload_map: have info of session id and associated copp_idx/num_copps
3272 * @perf_mode: performance mode like LL/ULL/..
3273 * @passthr_mode: flag to indicate passthrough mode
3274 *
3275 * Returns 0 on success or error on failure
3276 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303277int adm_matrix_map(int path, struct route_payload payload_map, int perf_mode,
3278 uint32_t passthr_mode)
3279{
3280 struct adm_cmd_matrix_map_routings_v5 *route;
3281 struct adm_session_map_node_v5 *node;
3282 uint16_t *copps_list;
3283 int cmd_size = 0;
3284 int ret = 0, i = 0;
3285 void *payload = NULL;
3286 void *matrix_map = NULL;
3287 int port_idx, copp_idx;
3288
3289 /* Assumes port_ids have already been validated during adm_open */
3290 cmd_size = (sizeof(struct adm_cmd_matrix_map_routings_v5) +
3291 sizeof(struct adm_session_map_node_v5) +
3292 (sizeof(uint32_t) * payload_map.num_copps));
3293 matrix_map = kzalloc(cmd_size, GFP_KERNEL);
3294 if (matrix_map == NULL) {
3295 pr_err("%s: Mem alloc failed\n", __func__);
3296 ret = -EINVAL;
3297 return ret;
3298 }
3299 route = (struct adm_cmd_matrix_map_routings_v5 *)matrix_map;
3300
3301 route->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3302 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
3303 route->hdr.pkt_size = cmd_size;
3304 route->hdr.src_svc = 0;
3305 route->hdr.src_domain = APR_DOMAIN_APPS;
3306 route->hdr.src_port = 0; /* Ignored */;
3307 route->hdr.dest_svc = APR_SVC_ADM;
3308 route->hdr.dest_domain = APR_DOMAIN_ADSP;
3309 route->hdr.dest_port = 0; /* Ignored */;
3310 route->hdr.token = 0;
3311 route->num_sessions = 1;
3312 route_set_opcode_matrix_id(&route, path, passthr_mode);
3313
3314 payload = ((u8 *)matrix_map +
3315 sizeof(struct adm_cmd_matrix_map_routings_v5));
3316 node = (struct adm_session_map_node_v5 *)payload;
3317
3318 node->session_id = payload_map.session_id;
3319 node->num_copps = payload_map.num_copps;
3320 payload = (u8 *)node + sizeof(struct adm_session_map_node_v5);
3321 copps_list = (uint16_t *)payload;
3322 for (i = 0; i < payload_map.num_copps; i++) {
3323 port_idx =
3324 adm_validate_and_get_port_index(payload_map.port_id[i]);
3325 if (port_idx < 0) {
3326 pr_err("%s: Invalid port_id 0x%x\n", __func__,
3327 payload_map.port_id[i]);
3328 ret = -EINVAL;
3329 goto fail_cmd;
3330 }
3331 copp_idx = payload_map.copp_idx[i];
3332 copps_list[i] = atomic_read(&this_adm.copp.id[port_idx]
3333 [copp_idx]);
3334 }
3335 atomic_set(&this_adm.matrix_map_stat, -1);
3336
3337 ret = apr_send_pkt(this_adm.apr, (uint32_t *)matrix_map);
3338 if (ret < 0) {
3339 pr_err("%s: routing for syream %d failed ret %d\n",
3340 __func__, payload_map.session_id, ret);
3341 ret = -EINVAL;
3342 goto fail_cmd;
3343 }
3344 ret = wait_event_timeout(this_adm.matrix_map_wait,
3345 atomic_read(&this_adm.matrix_map_stat) >= 0,
3346 msecs_to_jiffies(TIMEOUT_MS));
3347 if (!ret) {
3348 pr_err("%s: routing for syream %d failed\n", __func__,
3349 payload_map.session_id);
3350 ret = -EINVAL;
3351 goto fail_cmd;
3352 } else if (atomic_read(&this_adm.matrix_map_stat) > 0) {
3353 pr_err("%s: DSP returned error[%s]\n", __func__,
3354 adsp_err_get_err_str(atomic_read(
3355 &this_adm.matrix_map_stat)));
3356 ret = adsp_err_get_lnx_err_code(
3357 atomic_read(&this_adm.matrix_map_stat));
3358 goto fail_cmd;
3359 }
3360
3361 if ((perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) &&
3362 (path != ADM_PATH_COMPRESSED_RX)) {
3363 for (i = 0; i < payload_map.num_copps; i++) {
3364 port_idx = afe_get_port_index(payload_map.port_id[i]);
3365 copp_idx = payload_map.copp_idx[i];
3366 if (port_idx < 0 || copp_idx < 0 ||
3367 (copp_idx > MAX_COPPS_PER_PORT - 1)) {
3368 pr_err("%s: Invalid idx port_idx %d copp_idx %d\n",
3369 __func__, port_idx, copp_idx);
3370 continue;
3371 }
3372 rtac_add_adm_device(payload_map.port_id[i],
3373 atomic_read(&this_adm.copp.id
3374 [port_idx][copp_idx]),
3375 get_cal_path(path),
3376 payload_map.session_id,
3377 payload_map.app_type[i],
3378 payload_map.acdb_dev_id[i]);
3379
3380 if (!test_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3381 (void *)&this_adm.copp.adm_status[port_idx]
3382 [copp_idx])) {
3383 pr_debug("%s: adm copp[0x%x][%d] already sent",
3384 __func__, port_idx, copp_idx);
3385 continue;
3386 }
3387 send_adm_cal(payload_map.port_id[i], copp_idx,
3388 get_cal_path(path), perf_mode,
3389 payload_map.app_type[i],
3390 payload_map.acdb_dev_id[i],
Aditya Bavanari5106b562018-01-08 13:16:32 +05303391 payload_map.sample_rate[i],
3392 passthr_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303393 /* ADM COPP calibration is already sent */
3394 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3395 (void *)&this_adm.copp.
3396 adm_status[port_idx][copp_idx]);
3397 pr_debug("%s: copp_id: %d\n", __func__,
3398 atomic_read(&this_adm.copp.id[port_idx]
3399 [copp_idx]));
3400 }
3401 }
3402
3403fail_cmd:
3404 kfree(matrix_map);
3405 return ret;
3406}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303407EXPORT_SYMBOL(adm_matrix_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303408
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303409/**
3410 * adm_ec_ref_rx_id -
3411 * Update EC ref port ID
3412 *
3413 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303414void adm_ec_ref_rx_id(int port_id)
3415{
3416 this_adm.ec_ref_rx = port_id;
3417 pr_debug("%s: ec_ref_rx:%d\n", __func__, this_adm.ec_ref_rx);
3418}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303419EXPORT_SYMBOL(adm_ec_ref_rx_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303420
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303421/**
3422 * adm_num_ec_ref_rx_chans -
3423 * Update EC ref number of channels
3424 *
3425 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303426void adm_num_ec_ref_rx_chans(int num_chans)
3427{
3428 this_adm.num_ec_ref_rx_chans = num_chans;
3429 pr_debug("%s: num_ec_ref_rx_chans:%d\n",
3430 __func__, this_adm.num_ec_ref_rx_chans);
3431}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303432EXPORT_SYMBOL(adm_num_ec_ref_rx_chans);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303433
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303434/**
3435 * adm_ec_ref_rx_bit_width -
3436 * Update EC ref bit_width
3437 *
3438 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303439void adm_ec_ref_rx_bit_width(int bit_width)
3440{
3441 this_adm.ec_ref_rx_bit_width = bit_width;
3442 pr_debug("%s: ec_ref_rx_bit_width:%d\n",
3443 __func__, this_adm.ec_ref_rx_bit_width);
3444}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303445EXPORT_SYMBOL(adm_ec_ref_rx_bit_width);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303446
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303447/**
3448 * adm_ec_ref_rx_sampling_rate -
3449 * Update EC ref sample rate
3450 *
3451 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303452void adm_ec_ref_rx_sampling_rate(int sampling_rate)
3453{
3454 this_adm.ec_ref_rx_sampling_rate = sampling_rate;
3455 pr_debug("%s: ec_ref_rx_sampling_rate:%d\n",
3456 __func__, this_adm.ec_ref_rx_sampling_rate);
3457}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303458EXPORT_SYMBOL(adm_ec_ref_rx_sampling_rate);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303459
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303460/**
Dieter Luecking50c55352018-09-28 14:29:17 +02003461 * adm_set_native_mode -
3462 * Set adm channel native mode.
3463 * If enabled matrix mixer will be
3464 * running in native mode for channel
3465 * configuration for this device session.
3466 *
3467 */
3468void adm_set_native_mode(int mode)
3469{
3470 this_adm.native_mode = mode;
3471 pr_debug("%s: enable native_mode :%d\n",
3472 __func__, this_adm.native_mode);
3473}
3474EXPORT_SYMBOL(adm_set_native_mode);
3475
3476/**
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303477 * adm_close -
3478 * command to close ADM copp
3479 *
3480 * @port_id: Port ID number
3481 * @perf_mode: performance mode like LL/ULL/..
3482 * @copp_idx: copp index assigned
3483 *
3484 * Returns 0 on success or error on failure
3485 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303486int adm_close(int port_id, int perf_mode, int copp_idx)
3487{
3488 struct apr_hdr close;
3489
3490 int ret = 0, port_idx;
3491 int copp_id = RESET_COPP_ID;
3492
3493 pr_debug("%s: port_id=0x%x perf_mode: %d copp_idx: %d\n", __func__,
3494 port_id, perf_mode, copp_idx);
3495
3496 port_id = q6audio_convert_virtual_to_portid(port_id);
3497 port_idx = adm_validate_and_get_port_index(port_id);
3498 if (port_idx < 0) {
3499 pr_err("%s: Invalid port_id 0x%x\n",
3500 __func__, port_id);
3501 return -EINVAL;
3502 }
3503
3504 if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
3505 pr_err("%s: Invalid copp idx: %d\n", __func__, copp_idx);
3506 return -EINVAL;
3507 }
3508
3509 if (this_adm.copp.adm_delay[port_idx][copp_idx] && perf_mode
3510 == LEGACY_PCM_MODE) {
3511 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
3512 1);
3513 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
3514 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
3515 }
3516
3517 atomic_dec(&this_adm.copp.cnt[port_idx][copp_idx]);
3518 if (!(atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]))) {
3519 copp_id = adm_get_copp_id(port_idx, copp_idx);
3520 pr_debug("%s: Closing ADM port_idx:%d copp_idx:%d copp_id:0x%x\n",
3521 __func__, port_idx, copp_idx, copp_id);
3522 if ((!perf_mode) && (this_adm.outband_memmap.paddr != 0) &&
3523 (atomic_read(&this_adm.copp.topology[port_idx][copp_idx]) ==
3524 SRS_TRUMEDIA_TOPOLOGY_ID)) {
3525 atomic_set(&this_adm.mem_map_index,
3526 ADM_SRS_TRUMEDIA);
3527 ret = adm_memory_unmap_regions();
3528 if (ret < 0) {
3529 pr_err("%s: adm mem unmmap err %d",
3530 __func__, ret);
3531 } else {
3532 atomic_set(&this_adm.mem_map_handles
3533 [ADM_SRS_TRUMEDIA], 0);
3534 }
3535 }
3536
3537
3538 if ((afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX) &&
3539 this_adm.sourceTrackingData.memmap.paddr) {
3540 atomic_set(&this_adm.mem_map_index,
3541 ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
3542 ret = adm_memory_unmap_regions();
3543 if (ret < 0) {
3544 pr_err("%s: adm mem unmmap err %d",
3545 __func__, ret);
3546 }
3547 msm_audio_ion_free(
Banajit Goswami08bb7362017-11-03 22:48:23 -07003548 this_adm.sourceTrackingData.dma_buf);
3549 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303550 this_adm.sourceTrackingData.memmap.size = 0;
3551 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
3552 this_adm.sourceTrackingData.memmap.paddr = 0;
3553 this_adm.sourceTrackingData.apr_cmd_status = -1;
3554 atomic_set(&this_adm.mem_map_handles[
3555 ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
3556 }
3557
3558 close.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3559 APR_HDR_LEN(APR_HDR_SIZE),
3560 APR_PKT_VER);
3561 close.pkt_size = sizeof(close);
3562 close.src_svc = APR_SVC_ADM;
3563 close.src_domain = APR_DOMAIN_APPS;
3564 close.src_port = port_id;
3565 close.dest_svc = APR_SVC_ADM;
3566 close.dest_domain = APR_DOMAIN_ADSP;
3567 close.dest_port = copp_id;
3568 close.token = port_idx << 16 | copp_idx;
3569 close.opcode = ADM_CMD_DEVICE_CLOSE_V5;
3570
3571 atomic_set(&this_adm.copp.id[port_idx][copp_idx],
3572 RESET_COPP_ID);
3573 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
3574 atomic_set(&this_adm.copp.topology[port_idx][copp_idx], 0);
3575 atomic_set(&this_adm.copp.mode[port_idx][copp_idx], 0);
3576 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3577 atomic_set(&this_adm.copp.rate[port_idx][copp_idx], 0);
3578 atomic_set(&this_adm.copp.channels[port_idx][copp_idx], 0);
3579 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx], 0);
3580 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx], 0);
3581
3582 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3583 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
3584
3585 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&close);
3586 if (ret < 0) {
3587 pr_err("%s: ADM close failed %d\n", __func__, ret);
3588 return -EINVAL;
3589 }
3590
3591 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3592 atomic_read(&this_adm.copp.stat
3593 [port_idx][copp_idx]) >= 0,
3594 msecs_to_jiffies(TIMEOUT_MS));
3595 if (!ret) {
3596 pr_err("%s: ADM cmd Route timedout for port 0x%x\n",
3597 __func__, port_id);
3598 return -EINVAL;
3599 } else if (atomic_read(&this_adm.copp.stat
3600 [port_idx][copp_idx]) > 0) {
3601 pr_err("%s: DSP returned error[%s]\n",
3602 __func__, adsp_err_get_err_str(
3603 atomic_read(&this_adm.copp.stat
3604 [port_idx][copp_idx])));
3605 return adsp_err_get_lnx_err_code(
3606 atomic_read(&this_adm.copp.stat
3607 [port_idx][copp_idx]));
3608 }
3609 }
3610
3611 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) {
3612 pr_debug("%s: remove adm device from rtac\n", __func__);
3613 rtac_remove_adm_device(port_id, copp_id);
3614 }
3615 return 0;
3616}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303617EXPORT_SYMBOL(adm_close);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303618
3619int send_rtac_audvol_cal(void)
3620{
3621 int ret = 0;
3622 int ret2 = 0;
3623 int i = 0;
3624 int copp_idx, port_idx, acdb_id, app_id, path;
3625 struct cal_block_data *cal_block = NULL;
3626 struct audio_cal_info_audvol *audvol_cal_info = NULL;
3627 struct rtac_adm rtac_adm_data;
3628
3629 mutex_lock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3630
3631 cal_block = cal_utils_get_only_cal_block(
3632 this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]);
Vikram Panduranga770b8382017-09-27 12:17:36 -07003633 if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303634 pr_err("%s: can't find cal block!\n", __func__);
3635 goto unlock;
3636 }
3637
3638 audvol_cal_info = cal_block->cal_info;
3639 if (audvol_cal_info == NULL) {
3640 pr_err("%s: audvol_cal_info is NULL!\n", __func__);
3641 goto unlock;
3642 }
3643
3644 get_rtac_adm_data(&rtac_adm_data);
3645 for (; i < rtac_adm_data.num_of_dev; i++) {
3646
3647 acdb_id = rtac_adm_data.device[i].acdb_dev_id;
3648 if (acdb_id == 0)
3649 acdb_id = audvol_cal_info->acdb_id;
3650
3651 app_id = rtac_adm_data.device[i].app_type;
3652 if (app_id == 0)
3653 app_id = audvol_cal_info->app_type;
3654
3655 path = afe_get_port_type(rtac_adm_data.device[i].afe_port);
3656 if ((acdb_id == audvol_cal_info->acdb_id) &&
3657 (app_id == audvol_cal_info->app_type) &&
3658 (path == audvol_cal_info->path)) {
3659
3660 if (adm_get_indexes_from_copp_id(rtac_adm_data.
3661 device[i].copp, &copp_idx, &port_idx) != 0) {
3662 pr_debug("%s: Copp Id %d is not active\n",
3663 __func__,
3664 rtac_adm_data.device[i].copp);
3665 continue;
3666 }
3667
3668 ret2 = adm_remap_and_send_cal_block(ADM_RTAC_AUDVOL_CAL,
3669 rtac_adm_data.device[i].afe_port,
3670 copp_idx, cal_block,
3671 atomic_read(&this_adm.copp.
3672 mode[port_idx][copp_idx]),
3673 audvol_cal_info->app_type,
3674 audvol_cal_info->acdb_id,
3675 atomic_read(&this_adm.copp.
3676 rate[port_idx][copp_idx]));
3677 if (ret2 < 0) {
3678 pr_debug("%s: remap and send failed for copp Id %d, acdb id %d, app type %d, path %d\n",
3679 __func__, rtac_adm_data.device[i].copp,
3680 audvol_cal_info->acdb_id,
3681 audvol_cal_info->app_type,
3682 audvol_cal_info->path);
3683 ret = ret2;
3684 }
3685 }
3686 }
3687unlock:
3688 mutex_unlock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3689 return ret;
3690}
3691
3692int adm_map_rtac_block(struct rtac_cal_block_data *cal_block)
3693{
3694 int result = 0;
3695
3696 pr_debug("%s:\n", __func__);
3697
3698 if (cal_block == NULL) {
3699 pr_err("%s: cal_block is NULL!\n",
3700 __func__);
3701 result = -EINVAL;
3702 goto done;
3703 }
3704
3705 if (cal_block->cal_data.paddr == 0) {
3706 pr_debug("%s: No address to map!\n",
3707 __func__);
3708 result = -EINVAL;
3709 goto done;
3710 }
3711
3712 if (cal_block->map_data.map_size == 0) {
3713 pr_debug("%s: map size is 0!\n",
3714 __func__);
3715 result = -EINVAL;
3716 goto done;
3717 }
3718
3719 /* valid port ID needed for callback use primary I2S */
3720 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3721 result = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3722 &cal_block->map_data.map_size, 1);
3723 if (result < 0) {
3724 pr_err("%s: RTAC mmap did not work! size = %d result %d\n",
3725 __func__,
3726 cal_block->map_data.map_size, result);
3727 pr_debug("%s: RTAC mmap did not work! addr = 0x%pK, size = %d\n",
3728 __func__,
3729 &cal_block->cal_data.paddr,
3730 cal_block->map_data.map_size);
3731 goto done;
3732 }
3733
3734 cal_block->map_data.map_handle = atomic_read(
3735 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]);
3736done:
3737 return result;
3738}
3739
3740int adm_unmap_rtac_block(uint32_t *mem_map_handle)
3741{
3742 int result = 0;
3743
3744 pr_debug("%s:\n", __func__);
3745
3746 if (mem_map_handle == NULL) {
3747 pr_debug("%s: Map handle is NULL, nothing to unmap\n",
3748 __func__);
3749 goto done;
3750 }
3751
3752 if (*mem_map_handle == 0) {
3753 pr_debug("%s: Map handle is 0, nothing to unmap\n",
3754 __func__);
3755 goto done;
3756 }
3757
3758 if (*mem_map_handle != atomic_read(
3759 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL])) {
3760 pr_err("%s: Map handles do not match! Unmapping RTAC, RTAC map 0x%x, ADM map 0x%x\n",
3761 __func__, *mem_map_handle, atomic_read(
3762 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]));
3763
3764 /* if mismatch use handle passed in to unmap */
3765 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL],
3766 *mem_map_handle);
3767 }
3768
3769 /* valid port ID needed for callback use primary I2S */
3770 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3771 result = adm_memory_unmap_regions();
3772 if (result < 0) {
3773 pr_debug("%s: adm_memory_unmap_regions failed, error %d\n",
3774 __func__, result);
3775 } else {
3776 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL], 0);
3777 *mem_map_handle = 0;
3778 }
3779done:
3780 return result;
3781}
3782
3783static int get_cal_type_index(int32_t cal_type)
3784{
3785 int ret = -EINVAL;
3786
3787 switch (cal_type) {
3788 case ADM_AUDPROC_CAL_TYPE:
3789 ret = ADM_AUDPROC_CAL;
3790 break;
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05303791 case ADM_LSM_AUDPROC_CAL_TYPE:
3792 ret = ADM_LSM_AUDPROC_CAL;
3793 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303794 case ADM_AUDVOL_CAL_TYPE:
3795 ret = ADM_AUDVOL_CAL;
3796 break;
3797 case ADM_CUST_TOPOLOGY_CAL_TYPE:
3798 ret = ADM_CUSTOM_TOP_CAL;
3799 break;
3800 case ADM_RTAC_INFO_CAL_TYPE:
3801 ret = ADM_RTAC_INFO_CAL;
3802 break;
3803 case ADM_RTAC_APR_CAL_TYPE:
3804 ret = ADM_RTAC_APR_CAL;
3805 break;
3806 case ADM_RTAC_AUDVOL_CAL_TYPE:
3807 ret = ADM_RTAC_AUDVOL_CAL;
3808 break;
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07003809 case ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE:
3810 ret = ADM_LSM_AUDPROC_PERSISTENT_CAL;
3811 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303812 default:
3813 pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
3814 }
3815 return ret;
3816}
3817
3818static int adm_alloc_cal(int32_t cal_type, size_t data_size, void *data)
3819{
3820 int ret = 0;
3821 int cal_index;
3822
3823 pr_debug("%s:\n", __func__);
3824
3825 cal_index = get_cal_type_index(cal_type);
3826 if (cal_index < 0) {
3827 pr_err("%s: could not get cal index %d!\n",
3828 __func__, cal_index);
3829 ret = -EINVAL;
3830 goto done;
3831 }
3832
3833 ret = cal_utils_alloc_cal(data_size, data,
3834 this_adm.cal_data[cal_index], 0, NULL);
3835 if (ret < 0) {
3836 pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
3837 __func__, ret, cal_type);
3838 ret = -EINVAL;
3839 goto done;
3840 }
3841done:
3842 return ret;
3843}
3844
3845static int adm_dealloc_cal(int32_t cal_type, size_t data_size, void *data)
3846{
3847 int ret = 0;
3848 int cal_index;
3849
3850 pr_debug("%s:\n", __func__);
3851
3852 cal_index = get_cal_type_index(cal_type);
3853 if (cal_index < 0) {
3854 pr_err("%s: could not get cal index %d!\n",
3855 __func__, cal_index);
3856 ret = -EINVAL;
3857 goto done;
3858 }
3859
3860 ret = cal_utils_dealloc_cal(data_size, data,
3861 this_adm.cal_data[cal_index]);
3862 if (ret < 0) {
3863 pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
3864 __func__, ret, cal_type);
3865 ret = -EINVAL;
3866 goto done;
3867 }
3868done:
3869 return ret;
3870}
3871
3872static int adm_set_cal(int32_t cal_type, size_t data_size, void *data)
3873{
3874 int ret = 0;
3875 int cal_index;
3876
3877 pr_debug("%s:\n", __func__);
3878
3879 cal_index = get_cal_type_index(cal_type);
3880 if (cal_index < 0) {
3881 pr_err("%s: could not get cal index %d!\n",
3882 __func__, cal_index);
3883 ret = -EINVAL;
3884 goto done;
3885 }
3886
3887 ret = cal_utils_set_cal(data_size, data,
3888 this_adm.cal_data[cal_index], 0, NULL);
3889 if (ret < 0) {
3890 pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
3891 __func__, ret, cal_type);
3892 ret = -EINVAL;
3893 goto done;
3894 }
3895
3896 if (cal_index == ADM_CUSTOM_TOP_CAL) {
3897 mutex_lock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3898 this_adm.set_custom_topology = 1;
3899 mutex_unlock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3900 } else if (cal_index == ADM_RTAC_AUDVOL_CAL) {
3901 send_rtac_audvol_cal();
3902 }
3903done:
3904 return ret;
3905}
3906
3907static int adm_map_cal_data(int32_t cal_type,
3908 struct cal_block_data *cal_block)
3909{
3910 int ret = 0;
3911 int cal_index;
3912
3913 pr_debug("%s:\n", __func__);
3914
3915 cal_index = get_cal_type_index(cal_type);
3916 if (cal_index < 0) {
3917 pr_err("%s: could not get cal index %d!\n",
3918 __func__, cal_index);
3919 ret = -EINVAL;
3920 goto done;
3921 }
3922
3923 atomic_set(&this_adm.mem_map_index, cal_index);
3924 ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3925 (uint32_t *)&cal_block->map_data.map_size, 1);
3926 if (ret < 0) {
3927 pr_err("%s: map did not work! cal_type %i ret %d\n",
3928 __func__, cal_index, ret);
3929 ret = -ENODEV;
3930 goto done;
3931 }
3932 cal_block->map_data.q6map_handle = atomic_read(&this_adm.
3933 mem_map_handles[cal_index]);
3934done:
3935 return ret;
3936}
3937
3938static int adm_unmap_cal_data(int32_t cal_type,
3939 struct cal_block_data *cal_block)
3940{
3941 int ret = 0;
3942 int cal_index;
3943
3944 pr_debug("%s:\n", __func__);
3945
3946 cal_index = get_cal_type_index(cal_type);
3947 if (cal_index < 0) {
3948 pr_err("%s: could not get cal index %d!\n",
3949 __func__, cal_index);
3950 ret = -EINVAL;
3951 goto done;
3952 }
3953
3954 if (cal_block == NULL) {
3955 pr_err("%s: Cal block is NULL!\n",
3956 __func__);
3957 goto done;
3958 }
3959
3960 if (cal_block->map_data.q6map_handle == 0) {
3961 pr_err("%s: Map handle is NULL, nothing to unmap\n",
3962 __func__);
3963 goto done;
3964 }
3965
3966 atomic_set(&this_adm.mem_map_handles[cal_index],
3967 cal_block->map_data.q6map_handle);
3968 atomic_set(&this_adm.mem_map_index, cal_index);
3969 ret = adm_memory_unmap_regions();
3970 if (ret < 0) {
3971 pr_err("%s: unmap did not work! cal_type %i ret %d\n",
3972 __func__, cal_index, ret);
3973 ret = -ENODEV;
3974 goto done;
3975 }
3976 cal_block->map_data.q6map_handle = 0;
3977done:
3978 return ret;
3979}
3980
3981static void adm_delete_cal_data(void)
3982{
3983 pr_debug("%s:\n", __func__);
3984
3985 cal_utils_destroy_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data);
3986}
3987
3988static int adm_init_cal_data(void)
3989{
3990 int ret = 0;
3991 struct cal_type_info cal_type_info[] = {
3992 {{ADM_CUST_TOPOLOGY_CAL_TYPE,
3993 {adm_alloc_cal, adm_dealloc_cal, NULL,
3994 adm_set_cal, NULL, NULL} },
3995 {adm_map_cal_data, adm_unmap_cal_data,
3996 cal_utils_match_buf_num} },
3997
3998 {{ADM_AUDPROC_CAL_TYPE,
3999 {adm_alloc_cal, adm_dealloc_cal, NULL,
4000 adm_set_cal, NULL, NULL} },
4001 {adm_map_cal_data, adm_unmap_cal_data,
4002 cal_utils_match_buf_num} },
4003
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05304004 {{ADM_LSM_AUDPROC_CAL_TYPE,
4005 {adm_alloc_cal, adm_dealloc_cal, NULL,
4006 adm_set_cal, NULL, NULL} },
4007 {adm_map_cal_data, adm_unmap_cal_data,
4008 cal_utils_match_buf_num} },
4009
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304010 {{ADM_AUDVOL_CAL_TYPE,
4011 {adm_alloc_cal, adm_dealloc_cal, NULL,
4012 adm_set_cal, NULL, NULL} },
4013 {adm_map_cal_data, adm_unmap_cal_data,
4014 cal_utils_match_buf_num} },
4015
4016 {{ADM_RTAC_INFO_CAL_TYPE,
4017 {NULL, NULL, NULL, NULL, NULL, NULL} },
4018 {NULL, NULL, cal_utils_match_buf_num} },
4019
4020 {{ADM_RTAC_APR_CAL_TYPE,
4021 {NULL, NULL, NULL, NULL, NULL, NULL} },
4022 {NULL, NULL, cal_utils_match_buf_num} },
4023
4024 {{SRS_TRUMEDIA_CAL_TYPE,
4025 {NULL, NULL, NULL, NULL, NULL, NULL} },
4026 {NULL, NULL, cal_utils_match_buf_num} },
4027
4028 {{ADM_RTAC_AUDVOL_CAL_TYPE,
4029 {adm_alloc_cal, adm_dealloc_cal, NULL,
4030 adm_set_cal, NULL, NULL} },
4031 {adm_map_cal_data, adm_unmap_cal_data,
4032 cal_utils_match_buf_num} },
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07004033
4034 {{ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE,
4035 {adm_alloc_cal, adm_dealloc_cal, NULL,
4036 adm_set_cal, NULL, NULL} },
4037 {adm_map_cal_data, adm_unmap_cal_data,
4038 cal_utils_match_buf_num} },
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304039 };
4040 pr_debug("%s:\n", __func__);
4041
4042 ret = cal_utils_create_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data,
4043 cal_type_info);
4044 if (ret < 0) {
4045 pr_err("%s: could not create cal type! ret %d\n",
4046 __func__, ret);
4047 ret = -EINVAL;
4048 goto err;
4049 }
4050
4051 return ret;
4052err:
4053 adm_delete_cal_data();
4054 return ret;
4055}
4056
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304057/**
4058 * adm_set_volume -
4059 * command to set volume on ADM copp
4060 *
4061 * @port_id: Port ID number
4062 * @copp_idx: copp index assigned
4063 * @volume: gain value to set
4064 *
4065 * Returns 0 on success or error on failure
4066 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304067int adm_set_volume(int port_id, int copp_idx, int volume)
4068{
4069 struct audproc_volume_ctrl_master_gain audproc_vol;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004070 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304071 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304072
4073 pr_debug("%s: port_id %d, volume %d\n", __func__, port_id, volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304074
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004075 memset(&audproc_vol, 0, sizeof(audproc_vol));
4076 memset(&param_hdr, 0, sizeof(param_hdr));
4077 param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
4078 param_hdr.instance_id = INSTANCE_ID_0;
4079 param_hdr.param_id = AUDPROC_PARAM_ID_VOL_CTRL_MASTER_GAIN;
4080 param_hdr.param_size = sizeof(audproc_vol);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304081
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304082 audproc_vol.master_gain = volume;
4083
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004084 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4085 (uint8_t *) &audproc_vol);
4086 if (rc)
4087 pr_err("%s: Failed to set volume, err %d\n", __func__, rc);
4088
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304089 return rc;
4090}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304091EXPORT_SYMBOL(adm_set_volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304092
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304093/**
4094 * adm_set_softvolume -
4095 * command to set softvolume
4096 *
4097 * @port_id: Port ID number
4098 * @copp_idx: copp index assigned
4099 * @softvol_param: Params to set for softvolume
4100 *
4101 * Returns 0 on success or error on failure
4102 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304103int adm_set_softvolume(int port_id, int copp_idx,
4104 struct audproc_softvolume_params *softvol_param)
4105{
4106 struct audproc_soft_step_volume_params audproc_softvol;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004107 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304108 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304109
4110 pr_debug("%s: period %d step %d curve %d\n", __func__,
4111 softvol_param->period, softvol_param->step,
4112 softvol_param->rampingcurve);
4113
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004114 memset(&audproc_softvol, 0, sizeof(audproc_softvol));
4115 memset(&param_hdr, 0, sizeof(param_hdr));
4116 param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
4117 param_hdr.instance_id = INSTANCE_ID_0;
4118 param_hdr.param_id = AUDPROC_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
4119 param_hdr.param_size = sizeof(audproc_softvol);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304120
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304121 audproc_softvol.period = softvol_param->period;
4122 audproc_softvol.step = softvol_param->step;
4123 audproc_softvol.ramping_curve = softvol_param->rampingcurve;
4124
4125 pr_debug("%s: period %d, step %d, curve %d\n", __func__,
4126 audproc_softvol.period, audproc_softvol.step,
4127 audproc_softvol.ramping_curve);
4128
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004129 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4130 (uint8_t *) &audproc_softvol);
4131 if (rc)
4132 pr_err("%s: Failed to set soft volume, err %d\n", __func__, rc);
4133
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304134 return rc;
4135}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304136EXPORT_SYMBOL(adm_set_softvolume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304137
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304138/**
4139 * adm_set_mic_gain -
4140 * command to set MIC gain
4141 *
4142 * @port_id: Port ID number
4143 * @copp_idx: copp index assigned
4144 * @volume: gain value to set
4145 *
4146 * Returns 0 on success or error on failure
4147 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304148int adm_set_mic_gain(int port_id, int copp_idx, int volume)
4149{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004150 struct admx_mic_gain mic_gain_params;
4151 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304152 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304153
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004154 pr_debug("%s: Setting mic gain to %d at port_id 0x%x\n", __func__,
4155 volume, port_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304156
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004157 memset(&mic_gain_params, 0, sizeof(mic_gain_params));
4158 memset(&param_hdr, 0, sizeof(param_hdr));
4159 param_hdr.module_id = ADM_MODULE_IDX_MIC_GAIN_CTRL;
4160 param_hdr.instance_id = INSTANCE_ID_0;
4161 param_hdr.param_id = ADM_PARAM_IDX_MIC_GAIN;
4162 param_hdr.param_size = sizeof(mic_gain_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304163
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004164 mic_gain_params.tx_mic_gain = volume;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304165
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004166 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4167 (uint8_t *) &mic_gain_params);
4168 if (rc)
4169 pr_err("%s: Failed to set mic gain, err %d\n", __func__, rc);
4170
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304171 return rc;
4172}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304173EXPORT_SYMBOL(adm_set_mic_gain);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304174
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304175/**
4176 * adm_send_set_multichannel_ec_primary_mic_ch -
4177 * command to set multi-ch EC primary mic
4178 *
4179 * @port_id: Port ID number
4180 * @copp_idx: copp index assigned
4181 * @primary_mic_ch: channel number of primary mic
4182 *
4183 * Returns 0 on success or error on failure
4184 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304185int adm_send_set_multichannel_ec_primary_mic_ch(int port_id, int copp_idx,
4186 int primary_mic_ch)
4187{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004188 struct admx_sec_primary_mic_ch sec_primary_ch_params;
4189 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304190 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304191
4192 pr_debug("%s port_id 0x%x, copp_idx 0x%x, primary_mic_ch %d\n",
4193 __func__, port_id, copp_idx, primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304194
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004195 memset(&sec_primary_ch_params, 0, sizeof(sec_primary_ch_params));
4196 memset(&param_hdr, 0, sizeof(param_hdr));
4197 param_hdr.module_id = AUDPROC_MODULE_ID_VOICE_TX_SECNS;
4198 param_hdr.instance_id = INSTANCE_ID_0;
4199 param_hdr.param_id = AUDPROC_PARAM_IDX_SEC_PRIMARY_MIC_CH;
4200 param_hdr.param_size = sizeof(sec_primary_ch_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304201
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004202 sec_primary_ch_params.version = 0;
4203 sec_primary_ch_params.sec_primary_mic_ch = primary_mic_ch;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304204
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004205 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4206 (uint8_t *) &sec_primary_ch_params);
4207 if (rc)
4208 pr_err("%s: Failed to set primary mic chanel, err %d\n",
4209 __func__, rc);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304210
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304211 return rc;
4212}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304213EXPORT_SYMBOL(adm_send_set_multichannel_ec_primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304214
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304215/**
4216 * adm_param_enable -
4217 * command to send params to ADM for given module
4218 *
4219 * @port_id: Port ID number
4220 * @copp_idx: copp index assigned
4221 * @module_id: ADM module
4222 * @enable: flag to enable or disable module
4223 *
4224 * Returns 0 on success or error on failure
4225 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304226int adm_param_enable(int port_id, int copp_idx, int module_id, int enable)
4227{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004228 struct module_instance_info mod_inst_info;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304229
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004230 memset(&mod_inst_info, 0, sizeof(mod_inst_info));
4231 mod_inst_info.module_id = module_id;
4232 mod_inst_info.instance_id = INSTANCE_ID_0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304233
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004234 return adm_param_enable_v2(port_id, copp_idx, mod_inst_info, enable);
4235}
Vidyakumar Athota98464a22018-03-15 20:39:37 -07004236EXPORT_SYMBOL(adm_param_enable);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004237
4238/**
4239 * adm_param_enable_v2 -
4240 * command to send params to ADM for given module
4241 *
4242 * @port_id: Port ID number
4243 * @copp_idx: copp index assigned
4244 * @mod_inst_info: module and instance ID info
4245 * @enable: flag to enable or disable module
4246 *
4247 * Returns 0 on success or error on failure
4248 */
4249int adm_param_enable_v2(int port_id, int copp_idx,
4250 struct module_instance_info mod_inst_info, int enable)
4251{
4252 uint32_t enable_param;
4253 struct param_hdr_v3 param_hdr;
4254 int rc = 0;
4255
4256 if (enable < 0 || enable > 1) {
4257 pr_err("%s: Invalid value for enable %d\n", __func__, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304258 return -EINVAL;
4259 }
4260
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004261 pr_debug("%s port_id %d, module_id 0x%x, instance_id 0x%x, enable %d\n",
4262 __func__, port_id, mod_inst_info.module_id,
4263 mod_inst_info.instance_id, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304264
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004265 memset(&param_hdr, 0, sizeof(param_hdr));
4266 param_hdr.module_id = mod_inst_info.module_id;
4267 param_hdr.instance_id = mod_inst_info.instance_id;
4268 param_hdr.param_id = AUDPROC_PARAM_ID_ENABLE;
4269 param_hdr.param_size = sizeof(enable_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304270
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004271 enable_param = enable;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304272
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004273 rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4274 (uint8_t *) &enable_param);
4275 if (rc)
4276 pr_err("%s: Failed to set enable of module(%d) instance(%d) to %d, err %d\n",
4277 __func__, mod_inst_info.module_id,
4278 mod_inst_info.instance_id, enable, rc);
4279
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304280 return rc;
4281
4282}
Vidyakumar Athota98464a22018-03-15 20:39:37 -07004283EXPORT_SYMBOL(adm_param_enable_v2);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304284
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304285/**
4286 * adm_send_calibration -
4287 * send ADM calibration to DSP
4288 *
4289 * @port_id: Port ID number
4290 * @copp_idx: copp index assigned
4291 * @path: direction or ADM path type
4292 * @perf_mode: performance mode like LL/ULL/..
4293 * @cal_type: calibration type to use
4294 * @params: pointer with cal data
4295 * @size: cal size
4296 *
4297 * Returns 0 on success or error on failure
4298 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304299int adm_send_calibration(int port_id, int copp_idx, int path, int perf_mode,
4300 int cal_type, char *params, int size)
4301{
4302
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004303 int rc = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304304
4305 pr_debug("%s:port_id %d, path %d, perf_mode %d, cal_type %d, size %d\n",
4306 __func__, port_id, path, perf_mode, cal_type, size);
4307
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304308 /* Maps audio_dev_ctrl path definition to ACDB definition */
4309 if (get_cal_path(path) != RX_DEVICE) {
4310 pr_err("%s: acdb_path %d\n", __func__, path);
4311 rc = -EINVAL;
4312 goto end;
4313 }
4314
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004315 rc = adm_set_pp_params(port_id, copp_idx, NULL, (u8 *) params, size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304316
4317end:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304318 return rc;
4319}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304320EXPORT_SYMBOL(adm_send_calibration);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304321
4322/*
4323 * adm_update_wait_parameters must be called with routing driver locks.
4324 * adm_reset_wait_parameters must be called with routing driver locks.
4325 * set and reset parmeters are separated to make sure it is always called
4326 * under routing driver lock.
4327 * adm_wait_timeout is to block until timeout or interrupted. Timeout is
4328 * not a an error.
4329 */
4330int adm_set_wait_parameters(int port_id, int copp_idx)
4331{
4332
4333 int ret = 0, port_idx;
4334
4335 pr_debug("%s: port_id 0x%x, copp_idx %d\n", __func__, port_id,
4336 copp_idx);
4337 port_id = afe_convert_virtual_to_portid(port_id);
4338 port_idx = adm_validate_and_get_port_index(port_id);
4339 if (port_idx < 0) {
4340 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4341 ret = -EINVAL;
4342 goto end;
4343 }
4344
4345 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4346 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4347 return -EINVAL;
4348 }
4349
4350 this_adm.copp.adm_delay[port_idx][copp_idx] = 1;
4351 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 0);
4352
4353end:
4354 return ret;
4355
4356}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304357EXPORT_SYMBOL(adm_set_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304358
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304359/**
4360 * adm_reset_wait_parameters -
4361 * reset wait parameters or ADM delay value
4362 *
4363 * @port_id: Port ID number
4364 * @copp_idx: copp index assigned
4365 *
4366 * Returns 0 on success or error on failure
4367 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304368int adm_reset_wait_parameters(int port_id, int copp_idx)
4369{
4370 int ret = 0, port_idx;
4371
4372 pr_debug("%s: port_id 0x%x copp_idx %d\n", __func__, port_id,
4373 copp_idx);
4374 port_id = afe_convert_virtual_to_portid(port_id);
4375 port_idx = adm_validate_and_get_port_index(port_id);
4376 if (port_idx < 0) {
4377 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4378 ret = -EINVAL;
4379 goto end;
4380 }
4381
4382 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4383 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4384 return -EINVAL;
4385 }
4386
4387 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 1);
4388 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
4389
4390end:
4391 return ret;
4392}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304393EXPORT_SYMBOL(adm_reset_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304394
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304395/**
4396 * adm_wait_timeout -
4397 * ADM wait command after command send to DSP
4398 *
4399 * @port_id: Port ID number
4400 * @copp_idx: copp index assigned
4401 * @wait_time: value in ms for command timeout
4402 *
4403 * Returns 0 on success or error on failure
4404 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304405int adm_wait_timeout(int port_id, int copp_idx, int wait_time)
4406{
4407 int ret = 0, port_idx;
4408
4409 pr_debug("%s: port_id 0x%x, copp_idx %d, wait_time %d\n", __func__,
4410 port_id, copp_idx, wait_time);
4411 port_id = afe_convert_virtual_to_portid(port_id);
4412 port_idx = adm_validate_and_get_port_index(port_id);
4413 if (port_idx < 0) {
4414 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4415 ret = -EINVAL;
4416 goto end;
4417 }
4418
4419 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4420 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4421 return -EINVAL;
4422 }
4423
4424 ret = wait_event_timeout(
4425 this_adm.copp.adm_delay_wait[port_idx][copp_idx],
4426 atomic_read(&this_adm.copp.adm_delay_stat[port_idx][copp_idx]),
4427 msecs_to_jiffies(wait_time));
4428 pr_debug("%s: return %d\n", __func__, ret);
4429 if (ret != 0)
4430 ret = -EINTR;
4431end:
4432 pr_debug("%s: return %d--\n", __func__, ret);
4433 return ret;
4434}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304435EXPORT_SYMBOL(adm_wait_timeout);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304436
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304437/**
4438 * adm_store_cal_data -
4439 * Retrieve calibration data for ADM copp device
4440 *
4441 * @port_id: Port ID number
4442 * @copp_idx: copp index assigned
4443 * @path: direction or copp type
4444 * @perf_mode: performance mode like LL/ULL/..
4445 * @cal_index: calibration index to use
4446 * @params: pointer to store cal data
4447 * @size: pointer to fill with cal size
4448 *
4449 * Returns 0 on success or error on failure
4450 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304451int adm_store_cal_data(int port_id, int copp_idx, int path, int perf_mode,
4452 int cal_index, char *params, int *size)
4453{
4454 int rc = 0;
4455 struct cal_block_data *cal_block = NULL;
4456 int app_type, acdb_id, port_idx, sample_rate;
4457
4458 if (this_adm.cal_data[cal_index] == NULL) {
4459 pr_debug("%s: cal_index %d not allocated!\n",
4460 __func__, cal_index);
4461 goto end;
4462 }
4463
4464 if (get_cal_path(path) != RX_DEVICE) {
4465 pr_debug("%s: Invalid path to store calibration %d\n",
4466 __func__, path);
4467 rc = -EINVAL;
4468 goto end;
4469 }
4470
4471 port_id = afe_convert_virtual_to_portid(port_id);
4472 port_idx = adm_validate_and_get_port_index(port_id);
4473 if (port_idx < 0) {
4474 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
4475 rc = -EINVAL;
4476 goto end;
4477 }
4478
4479 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4480 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4481 return -EINVAL;
4482 }
4483
4484 acdb_id = atomic_read(&this_adm.copp.acdb_id[port_idx][copp_idx]);
4485 app_type = atomic_read(&this_adm.copp.app_type[port_idx][copp_idx]);
4486 sample_rate = atomic_read(&this_adm.copp.rate[port_idx][copp_idx]);
4487
4488 mutex_lock(&this_adm.cal_data[cal_index]->lock);
4489 cal_block = adm_find_cal(cal_index, get_cal_path(path), app_type,
4490 acdb_id, sample_rate);
4491 if (cal_block == NULL)
4492 goto unlock;
4493
4494 if (cal_block->cal_data.size <= 0) {
4495 pr_debug("%s: No ADM cal send for port_id = 0x%x!\n",
4496 __func__, port_id);
4497 rc = -EINVAL;
4498 goto unlock;
4499 }
4500
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05304501 if (cal_index == ADM_AUDPROC_CAL || cal_index == ADM_LSM_AUDPROC_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304502 if (cal_block->cal_data.size > AUD_PROC_BLOCK_SIZE) {
4503 pr_err("%s:audproc:invalid size exp/actual[%zd, %d]\n",
4504 __func__, cal_block->cal_data.size, *size);
4505 rc = -ENOMEM;
4506 goto unlock;
4507 }
Bhalchandra Gajarebbb64142018-05-10 14:16:49 -07004508 } else if (cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
4509 if (cal_block->cal_data.size > AUD_PROC_PERSIST_BLOCK_SIZE) {
4510 pr_err("%s:persist invalid size exp/actual[%zd, %d]\n",
4511 __func__, cal_block->cal_data.size, *size);
4512 rc = -ENOMEM;
4513 goto unlock;
4514 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304515 } else if (cal_index == ADM_AUDVOL_CAL) {
4516 if (cal_block->cal_data.size > AUD_VOL_BLOCK_SIZE) {
4517 pr_err("%s:aud_vol:invalid size exp/actual[%zd, %d]\n",
4518 __func__, cal_block->cal_data.size, *size);
4519 rc = -ENOMEM;
4520 goto unlock;
4521 }
4522 } else {
4523 pr_debug("%s: Not valid calibration for dolby topolgy\n",
4524 __func__);
4525 rc = -EINVAL;
4526 goto unlock;
4527 }
4528 memcpy(params, cal_block->cal_data.kvaddr, cal_block->cal_data.size);
4529 *size = cal_block->cal_data.size;
4530
4531 pr_debug("%s:port_id %d, copp_idx %d, path %d",
4532 __func__, port_id, copp_idx, path);
4533 pr_debug("perf_mode %d, cal_type %d, size %d\n",
4534 perf_mode, cal_index, *size);
4535
4536unlock:
4537 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
4538end:
4539 return rc;
4540}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304541EXPORT_SYMBOL(adm_store_cal_data);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304542
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304543/**
4544 * adm_send_compressed_device_mute -
4545 * command to send mute for compressed device
4546 *
4547 * @port_id: Port ID number
4548 * @copp_idx: copp index assigned
4549 * @mute_on: flag to indicate mute or unmute
4550 *
4551 * Returns 0 on success or error on failure
4552 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304553int adm_send_compressed_device_mute(int port_id, int copp_idx, bool mute_on)
4554{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004555 u32 mute_param = mute_on ? 1 : 0;
4556 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304557 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304558
4559 pr_debug("%s port_id: 0x%x, copp_idx %d, mute_on: %d\n",
4560 __func__, port_id, copp_idx, mute_on);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304561
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004562 memset(&param_hdr, 0, sizeof(param_hdr));
4563 param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_MUTE;
4564 param_hdr.instance_id = INSTANCE_ID_0;
4565 param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_MUTE;
4566 param_hdr.param_size = sizeof(mute_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304567
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004568 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4569 (uint8_t *) &mute_param);
4570 if (ret)
4571 pr_err("%s: Failed to set mute, err %d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304572
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304573 return ret;
4574}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304575EXPORT_SYMBOL(adm_send_compressed_device_mute);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304576
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304577/**
4578 * adm_send_compressed_device_latency -
4579 * command to send latency for compressed device
4580 *
4581 * @port_id: Port ID number
4582 * @copp_idx: copp index assigned
4583 * @latency: latency value to pass
4584 *
4585 * Returns 0 on success or error on failure
4586 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304587int adm_send_compressed_device_latency(int port_id, int copp_idx, int latency)
4588{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004589 u32 latency_param;
4590 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304591 int ret = 0;
4592
4593 pr_debug("%s port_id: 0x%x, copp_idx %d latency: %d\n", __func__,
4594 port_id, copp_idx, latency);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004595
4596 if (latency < 0) {
4597 pr_err("%s: Invalid value for latency %d", __func__, latency);
4598 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304599 }
4600
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004601 memset(&param_hdr, 0, sizeof(param_hdr));
4602 param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_LATENCY;
4603 param_hdr.instance_id = INSTANCE_ID_0;
4604 param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_LATENCY;
4605 param_hdr.param_size = sizeof(latency_param);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304606
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004607 latency_param = latency;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304608
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004609 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4610 (uint8_t *) &latency_param);
4611 if (ret)
4612 pr_err("%s: Failed to set latency, err %d\n", __func__, ret);
4613
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304614 return ret;
4615}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304616EXPORT_SYMBOL(adm_send_compressed_device_latency);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304617
4618/**
4619 * adm_swap_speaker_channels
4620 *
4621 * Receives port_id, copp_idx, sample rate, spk_swap and
4622 * send MFC command to swap speaker channel.
4623 * Return zero on success. On failure returns nonzero.
4624 *
4625 * port_id - Passed value, port_id for which channels swap is wanted
4626 * copp_idx - Passed value, copp_idx for which channels swap is wanted
4627 * sample_rate - Passed value, sample rate used by app type config
4628 * spk_swap - Passed value, spk_swap for check if swap flag is set
4629 */
4630int adm_swap_speaker_channels(int port_id, int copp_idx,
4631 int sample_rate, bool spk_swap)
4632{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004633 struct audproc_mfc_param_media_fmt mfc_cfg;
4634 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304635 uint16_t num_channels;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004636 int port_idx = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304637 int ret = 0;
4638
4639 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4640 __func__, port_id, copp_idx);
4641 port_id = q6audio_convert_virtual_to_portid(port_id);
4642 port_idx = adm_validate_and_get_port_index(port_id);
4643 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
4644 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004645 return -EINVAL;
4646 } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4647 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
4648 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304649 }
4650
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004651 num_channels = atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304652 if (num_channels != 2) {
4653 pr_debug("%s: Invalid number of channels: %d\n",
4654 __func__, num_channels);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004655 return -EINVAL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304656 }
4657
4658 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004659 memset(&param_hdr, 0, sizeof(param_hdr));
4660
4661 param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
4662 param_hdr.instance_id = INSTANCE_ID_0;
4663 param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
4664 param_hdr.param_size = sizeof(mfc_cfg);
4665
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304666 mfc_cfg.sampling_rate = sample_rate;
4667 mfc_cfg.bits_per_sample =
4668 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
4669 mfc_cfg.num_channels = num_channels;
4670
4671 /* Currently applying speaker swap for only 2 channel use case */
4672 if (spk_swap) {
4673 mfc_cfg.channel_type[0] =
4674 (uint16_t) PCM_CHANNEL_FR;
4675 mfc_cfg.channel_type[1] =
4676 (uint16_t) PCM_CHANNEL_FL;
4677 } else {
4678 mfc_cfg.channel_type[0] =
4679 (uint16_t) PCM_CHANNEL_FL;
4680 mfc_cfg.channel_type[1] =
4681 (uint16_t) PCM_CHANNEL_FR;
4682 }
4683
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004684 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4685 (u8 *) &mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304686 if (ret < 0) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004687 pr_err("%s: Failed to set swap speaker channels on port[0x%x] failed %d\n",
4688 __func__, port_id, ret);
4689 return ret;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304690 }
4691
4692 pr_debug("%s: mfc_cfg Set params returned success", __func__);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004693 return 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304694}
4695EXPORT_SYMBOL(adm_swap_speaker_channels);
4696
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304697/**
4698 * adm_set_sound_focus -
4699 * Update sound focus info
4700 *
4701 * @port_id: Port ID number
4702 * @copp_idx: copp index assigned
4703 * @soundFocusData: sound focus data to pass
4704 *
4705 * Returns 0 on success or error on failure
4706 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304707int adm_set_sound_focus(int port_id, int copp_idx,
4708 struct sound_focus_param soundFocusData)
4709{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004710 struct adm_param_fluence_soundfocus_t soundfocus_params;
4711 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304712 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304713 int i;
4714
4715 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4716 __func__, port_id, copp_idx);
4717
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004718 memset(&param_hdr, 0, sizeof(param_hdr));
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07004719 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004720 param_hdr.instance_id = INSTANCE_ID_0;
4721 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
4722 param_hdr.param_size = sizeof(soundfocus_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304723
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004724 memset(&(soundfocus_params), 0xFF, sizeof(soundfocus_params));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304725 for (i = 0; i < MAX_SECTORS; i++) {
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004726 soundfocus_params.start_angles[i] =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304727 soundFocusData.start_angle[i];
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004728 soundfocus_params.enables[i] = soundFocusData.enable[i];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304729 pr_debug("%s: start_angle[%d] = %d\n",
4730 __func__, i, soundFocusData.start_angle[i]);
4731 pr_debug("%s: enable[%d] = %d\n",
4732 __func__, i, soundFocusData.enable[i]);
4733 }
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004734 soundfocus_params.gain_step = soundFocusData.gain_step;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304735 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData.gain_step);
4736
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004737 soundfocus_params.reserved = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304738
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004739 ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
4740 (uint8_t *) &soundfocus_params);
4741 if (ret)
4742 pr_err("%s: Failed to set sound focus params, err %d\n",
4743 __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304744
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304745 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
4746
4747 return ret;
4748}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304749EXPORT_SYMBOL(adm_set_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304750
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304751/**
4752 * adm_get_sound_focus -
4753 * Retrieve sound focus info
4754 *
4755 * @port_id: Port ID number
4756 * @copp_idx: copp index assigned
4757 * @soundFocusData: pointer for sound focus data to be updated with
4758 *
4759 * Returns 0 on success or error on failure
4760 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304761int adm_get_sound_focus(int port_id, int copp_idx,
4762 struct sound_focus_param *soundFocusData)
4763{
4764 int ret = 0, i;
4765 char *params_value;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004766 uint32_t max_param_size = 0;
4767 struct adm_param_fluence_soundfocus_t *soundfocus_params = NULL;
4768 struct param_hdr_v3 param_hdr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304769
4770 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4771 __func__, port_id, copp_idx);
4772
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004773 max_param_size = sizeof(struct adm_param_fluence_soundfocus_t) +
4774 sizeof(union param_hdrs);
4775 params_value = kzalloc(max_param_size, GFP_KERNEL);
4776 if (!params_value)
4777 return -ENOMEM;
4778
4779 memset(&param_hdr, 0, sizeof(param_hdr));
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07004780 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004781 param_hdr.instance_id = INSTANCE_ID_0;
4782 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
4783 param_hdr.param_size = max_param_size;
4784 ret = adm_get_pp_params(port_id, copp_idx,
4785 ADM_CLIENT_ID_SOURCE_TRACKING, NULL, &param_hdr,
4786 params_value);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304787 if (ret) {
4788 pr_err("%s: get parameters failed ret:%d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304789 ret = -EINVAL;
4790 goto done;
4791 }
4792
4793 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
4794 pr_err("%s - get params returned error [%s]\n",
4795 __func__, adsp_err_get_err_str(
4796 this_adm.sourceTrackingData.apr_cmd_status));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304797 ret = adsp_err_get_lnx_err_code(
4798 this_adm.sourceTrackingData.apr_cmd_status);
4799 goto done;
4800 }
4801
4802 soundfocus_params = (struct adm_param_fluence_soundfocus_t *)
4803 params_value;
4804 for (i = 0; i < MAX_SECTORS; i++) {
4805 soundFocusData->start_angle[i] =
4806 soundfocus_params->start_angles[i];
4807 soundFocusData->enable[i] = soundfocus_params->enables[i];
4808 pr_debug("%s: start_angle[%d] = %d\n",
4809 __func__, i, soundFocusData->start_angle[i]);
4810 pr_debug("%s: enable[%d] = %d\n",
4811 __func__, i, soundFocusData->enable[i]);
4812 }
4813 soundFocusData->gain_step = soundfocus_params->gain_step;
4814 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData->gain_step);
4815
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304816done:
4817 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
4818
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004819 kfree(params_value);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304820 return ret;
4821}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304822EXPORT_SYMBOL(adm_get_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304823
4824static int adm_source_tracking_alloc_map_memory(void)
4825{
4826 int ret;
4827
4828 pr_debug("%s: Enter\n", __func__);
4829
Banajit Goswami08bb7362017-11-03 22:48:23 -07004830 ret = msm_audio_ion_alloc(&this_adm.sourceTrackingData.dma_buf,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304831 AUD_PROC_BLOCK_SIZE,
4832 &this_adm.sourceTrackingData.memmap.paddr,
4833 &this_adm.sourceTrackingData.memmap.size,
4834 &this_adm.sourceTrackingData.memmap.kvaddr);
4835 if (ret) {
4836 pr_err("%s: failed to allocate memory\n", __func__);
4837
4838 ret = -EINVAL;
4839 goto done;
4840 }
4841
4842 atomic_set(&this_adm.mem_map_index, ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
4843 ret = adm_memory_map_regions(&this_adm.sourceTrackingData.memmap.paddr,
4844 0,
4845 (uint32_t *)&this_adm.sourceTrackingData.memmap.size,
4846 1);
4847 if (ret < 0) {
4848 pr_err("%s: failed to map memory, paddr = 0x%pK, size = %d\n",
4849 __func__,
4850 (void *)this_adm.sourceTrackingData.memmap.paddr,
4851 (uint32_t)this_adm.sourceTrackingData.memmap.size);
4852
Banajit Goswami08bb7362017-11-03 22:48:23 -07004853 msm_audio_ion_free(this_adm.sourceTrackingData.dma_buf);
4854 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304855 this_adm.sourceTrackingData.memmap.size = 0;
4856 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
4857 this_adm.sourceTrackingData.memmap.paddr = 0;
4858 this_adm.sourceTrackingData.apr_cmd_status = -1;
4859 atomic_set(&this_adm.mem_map_handles
4860 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
4861
4862 ret = -EINVAL;
4863 goto done;
4864 }
4865 ret = 0;
4866 pr_debug("%s: paddr = 0x%pK, size = %d, mem_map_handle = 0x%x\n",
4867 __func__, (void *)this_adm.sourceTrackingData.memmap.paddr,
4868 (uint32_t)this_adm.sourceTrackingData.memmap.size,
4869 atomic_read(&this_adm.mem_map_handles
4870 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING]));
4871
4872done:
4873 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
4874
4875 return ret;
4876}
4877
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304878/**
4879 * adm_get_source_tracking -
4880 * Retrieve source tracking info
4881 *
4882 * @port_id: Port ID number
4883 * @copp_idx: copp index assigned
4884 * @sourceTrackingData: pointer for source track data to be updated with
4885 *
4886 * Returns 0 on success or error on failure
4887 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304888int adm_get_source_tracking(int port_id, int copp_idx,
4889 struct source_tracking_param *sourceTrackingData)
4890{
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004891 struct adm_param_fluence_sourcetracking_t *source_tracking_params =
4892 NULL;
4893 struct mem_mapping_hdr mem_hdr;
4894 struct param_hdr_v3 param_hdr;
4895 int i = 0;
4896 int ret = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304897
4898 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4899 __func__, port_id, copp_idx);
4900
4901 if (!this_adm.sourceTrackingData.memmap.paddr) {
4902 /* Allocate and map shared memory for out of band usage */
4903 ret = adm_source_tracking_alloc_map_memory();
4904 if (ret != 0) {
4905 ret = -EINVAL;
4906 goto done;
4907 }
4908 }
4909
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004910 memset(&mem_hdr, 0, sizeof(mem_hdr));
4911 memset(&param_hdr, 0, sizeof(param_hdr));
4912 mem_hdr.data_payload_addr_lsw =
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304913 lower_32_bits(this_adm.sourceTrackingData.memmap.paddr);
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004914 mem_hdr.data_payload_addr_msw = msm_audio_populate_upper_32_bits(
4915 this_adm.sourceTrackingData.memmap.paddr);
4916 mem_hdr.mem_map_handle = atomic_read(
4917 &this_adm.mem_map_handles[ADM_MEM_MAP_INDEX_SOURCE_TRACKING]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304918
Vignesh Kulothungan6f75e7b2018-06-21 18:05:44 -07004919 param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004920 param_hdr.instance_id = INSTANCE_ID_0;
4921 param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOURCETRACKING;
4922 /*
4923 * This size should be the max size of the calibration data + header.
4924 * Use the union size to ensure max size is used.
4925 */
4926 param_hdr.param_size =
4927 sizeof(struct adm_param_fluence_sourcetracking_t) +
4928 sizeof(union param_hdrs);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304929
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004930 /*
4931 * Retrieving parameters out of band, so no need to provide a buffer for
4932 * the returned parameter data as it will be at the memory location
4933 * provided.
4934 */
4935 ret = adm_get_pp_params(port_id, copp_idx,
4936 ADM_CLIENT_ID_SOURCE_TRACKING, &mem_hdr,
4937 &param_hdr, NULL);
4938 if (ret) {
4939 pr_err("%s: Failed to get params, error %d\n", __func__, ret);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304940 goto done;
4941 }
4942
4943 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
4944 pr_err("%s - get params returned error [%s]\n",
4945 __func__, adsp_err_get_err_str(
4946 this_adm.sourceTrackingData.apr_cmd_status));
4947
4948 ret = adsp_err_get_lnx_err_code(
4949 this_adm.sourceTrackingData.apr_cmd_status);
4950 goto done;
4951 }
4952
Vignesh Kulothungan60cc0352018-01-29 16:21:22 -08004953 /* How do we know what the param data was retrieved with for hdr size */
4954 source_tracking_params =
4955 (struct adm_param_fluence_sourcetracking_t
4956 *) (this_adm.sourceTrackingData.memmap.kvaddr +
4957 sizeof(struct param_hdr_v1));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304958 for (i = 0; i < MAX_SECTORS; i++) {
4959 sourceTrackingData->vad[i] = source_tracking_params->vad[i];
4960 pr_debug("%s: vad[%d] = %d\n",
4961 __func__, i, sourceTrackingData->vad[i]);
4962 }
4963 sourceTrackingData->doa_speech = source_tracking_params->doa_speech;
4964 pr_debug("%s: doa_speech = %d\n",
4965 __func__, sourceTrackingData->doa_speech);
4966
4967 for (i = 0; i < MAX_NOISE_SOURCE_INDICATORS; i++) {
4968 sourceTrackingData->doa_noise[i] =
4969 source_tracking_params->doa_noise[i];
4970 pr_debug("%s: doa_noise[%d] = %d\n",
4971 __func__, i, sourceTrackingData->doa_noise[i]);
4972 }
4973 for (i = 0; i < MAX_POLAR_ACTIVITY_INDICATORS; i++) {
4974 sourceTrackingData->polar_activity[i] =
4975 source_tracking_params->polar_activity[i];
4976 pr_debug("%s: polar_activity[%d] = %d\n",
4977 __func__, i, sourceTrackingData->polar_activity[i]);
4978 }
4979
4980 ret = 0;
4981
4982done:
4983 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
4984
4985 return ret;
4986}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304987EXPORT_SYMBOL(adm_get_source_tracking);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304988
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304989int __init adm_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304990{
4991 int i = 0, j;
4992
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304993 this_adm.ec_ref_rx = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304994 init_waitqueue_head(&this_adm.matrix_map_wait);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304995 init_waitqueue_head(&this_adm.adm_wait);
4996
4997 for (i = 0; i < AFE_MAX_PORTS; i++) {
4998 for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
4999 atomic_set(&this_adm.copp.id[i][j], RESET_COPP_ID);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305000 init_waitqueue_head(&this_adm.copp.wait[i][j]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305001 init_waitqueue_head(
5002 &this_adm.copp.adm_delay_wait[i][j]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305003 }
5004 }
5005
5006 if (adm_init_cal_data())
5007 pr_err("%s: could not init cal data!\n", __func__);
5008
Banajit Goswami08bb7362017-11-03 22:48:23 -07005009 this_adm.sourceTrackingData.dma_buf = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305010 this_adm.sourceTrackingData.memmap.size = 0;
5011 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
5012 this_adm.sourceTrackingData.memmap.paddr = 0;
5013 this_adm.sourceTrackingData.apr_cmd_status = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305014
5015 return 0;
5016}
5017
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05305018void adm_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305019{
Laxminath Kasam30ad7512017-11-28 12:40:22 +05305020 if (this_adm.apr)
5021 adm_reset_data();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305022 adm_delete_cal_data();
5023}