blob: 5987d39bace170f3a8c16935890a0a728a41040a [file] [log] [blame]
Rohit kumar5d860f42019-02-01 18:01:12 +05301/* Copyright (c) 2012-2019, The Linux Foundation. All rights reserved.
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/wait.h>
15#include <linux/sched.h>
16#include <linux/jiffies.h>
17#include <linux/uaccess.h>
18#include <linux/atomic.h>
19#include <linux/wait.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053020#include <sound/asound.h>
Laxminath Kasam8b1366a2017-10-05 01:44:16 +053021#include <dsp/msm-dts-srs-tm-config.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053022#include <dsp/apr_audio-v2.h>
23#include <dsp/q6adm-v2.h>
24#include <dsp/q6audio-v2.h>
25#include <dsp/q6afe-v2.h>
26#include <dsp/audio_cal_utils.h>
27#include <ipc/apr.h>
28#include "adsp_err.h"
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053029
30#define TIMEOUT_MS 1000
31
32#define RESET_COPP_ID 99
33#define INVALID_COPP_ID 0xFF
34/* Used for inband payload copy, max size is 4k */
35/* 2 is to account for module & param ID in payload */
36#define ADM_GET_PARAMETER_LENGTH (4096 - APR_HDR_SIZE - 2 * sizeof(uint32_t))
37
38#define ULL_SUPPORTED_BITS_PER_SAMPLE 16
39#define ULL_SUPPORTED_SAMPLE_RATE 48000
40
41#ifndef CONFIG_DOLBY_DAP
42#undef DOLBY_ADM_COPP_TOPOLOGY_ID
43#define DOLBY_ADM_COPP_TOPOLOGY_ID 0xFFFFFFFE
44#endif
45
46#ifndef CONFIG_DOLBY_DS2
47#undef DS2_ADM_COPP_TOPOLOGY_ID
48#define DS2_ADM_COPP_TOPOLOGY_ID 0xFFFFFFFF
49#endif
50
51/* ENUM for adm_status */
52enum adm_cal_status {
53 ADM_STATUS_CALIBRATION_REQUIRED = 0,
54 ADM_STATUS_MAX,
55};
56
57struct adm_copp {
58
59 atomic_t id[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
60 atomic_t cnt[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
61 atomic_t topology[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
62 atomic_t mode[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
63 atomic_t stat[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
64 atomic_t rate[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
65 atomic_t bit_width[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
66 atomic_t channels[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
67 atomic_t app_type[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
68 atomic_t acdb_id[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
Sachin Mohan Gadag3c3c5812018-07-20 15:46:54 +053069 atomic_t session_type[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053070 wait_queue_head_t wait[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
71 wait_queue_head_t adm_delay_wait[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
72 atomic_t adm_delay_stat[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
73 uint32_t adm_delay[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
74 unsigned long adm_status[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
75};
76
77struct source_tracking_data {
78 struct ion_client *ion_client;
79 struct ion_handle *ion_handle;
80 struct param_outband memmap;
81 int apr_cmd_status;
82};
83
84struct adm_ctl {
85 void *apr;
86
87 struct adm_copp copp;
88
89 atomic_t matrix_map_stat;
90 wait_queue_head_t matrix_map_wait;
91
92 atomic_t adm_stat;
93 wait_queue_head_t adm_wait;
94
95 struct cal_type_data *cal_data[ADM_MAX_CAL_TYPES];
96
97 atomic_t mem_map_handles[ADM_MEM_MAP_INDEX_MAX];
98 atomic_t mem_map_index;
99
100 struct param_outband outband_memmap;
101 struct source_tracking_data sourceTrackingData;
102
103 int set_custom_topology;
104 int ec_ref_rx;
105 int num_ec_ref_rx_chans;
106 int ec_ref_rx_bit_width;
107 int ec_ref_rx_sampling_rate;
108};
109
110static struct adm_ctl this_adm;
111
112struct adm_multi_ch_map {
113 bool set_channel_map;
114 char channel_mapping[PCM_FORMAT_MAX_NUM_CHANNEL];
115};
116
117#define ADM_MCH_MAP_IDX_PLAYBACK 0
118#define ADM_MCH_MAP_IDX_REC 1
119static struct adm_multi_ch_map multi_ch_maps[2] = {
120 { false,
121 {0, 0, 0, 0, 0, 0, 0, 0}
122 },
123 { false,
124 {0, 0, 0, 0, 0, 0, 0, 0}
125 }
126};
127
Rohit kumar5d860f42019-02-01 18:01:12 +0530128static struct adm_multi_ch_map port_channel_map[AFE_MAX_PORTS];
129
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530130static int adm_get_parameters[MAX_COPPS_PER_PORT * ADM_GET_PARAMETER_LENGTH];
131static int adm_module_topo_list[
132 MAX_COPPS_PER_PORT * ADM_GET_TOPO_MODULE_LIST_LENGTH];
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530133static struct mutex dts_srs_lock;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530134
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530135void msm_dts_srs_acquire_lock(void)
136{
137 mutex_lock(&dts_srs_lock);
138}
139
140void msm_dts_srs_release_lock(void)
141{
142 mutex_unlock(&dts_srs_lock);
143}
144
145/**
146 * adm_validate_and_get_port_index -
147 * validate given port id
148 *
149 * @port_id: Port ID number
150 *
151 * Returns valid index on success or error on failure
152 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530153int adm_validate_and_get_port_index(int port_id)
154{
155 int index;
156 int ret;
157
158 ret = q6audio_validate_port(port_id);
159 if (ret < 0) {
160 pr_err("%s: port validation failed id 0x%x ret %d\n",
161 __func__, port_id, ret);
162 return -EINVAL;
163 }
164
165 index = afe_get_port_index(port_id);
166 if (index < 0 || index >= AFE_MAX_PORTS) {
167 pr_err("%s: Invalid port idx %d port_id 0x%x\n",
168 __func__, index,
169 port_id);
170 return -EINVAL;
171 }
172 pr_debug("%s: port_idx- %d\n", __func__, index);
173 return index;
174}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530175EXPORT_SYMBOL(adm_validate_and_get_port_index);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530176
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530177/**
178 * adm_get_default_copp_idx -
179 * retrieve default copp_idx for given port
180 *
181 * @port_id: Port ID number
182 *
183 * Returns valid value on success or error on failure
184 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530185int adm_get_default_copp_idx(int port_id)
186{
187 int port_idx = adm_validate_and_get_port_index(port_id), idx;
188
189 if (port_idx < 0) {
190 pr_err("%s: Invalid port id: 0x%x", __func__, port_id);
191 return -EINVAL;
192 }
193 pr_debug("%s: port_idx:%d\n", __func__, port_idx);
194 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++) {
195 if (atomic_read(&this_adm.copp.id[port_idx][idx]) !=
196 RESET_COPP_ID)
197 return idx;
198 }
199 return -EINVAL;
200}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530201EXPORT_SYMBOL(adm_get_default_copp_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530202
203int adm_get_topology_for_port_from_copp_id(int port_id, int copp_id)
204{
205 int port_idx = adm_validate_and_get_port_index(port_id), idx;
206
207 if (port_idx < 0) {
208 pr_err("%s: Invalid port id: 0x%x", __func__, port_id);
209 return 0;
210 }
211 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++)
212 if (atomic_read(&this_adm.copp.id[port_idx][idx]) == copp_id)
213 return atomic_read(&this_adm.copp.topology[port_idx]
214 [idx]);
215 pr_err("%s: Invalid copp_id %d port_id 0x%x\n",
216 __func__, copp_id, port_id);
217 return 0;
218}
219
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530220/**
221 * adm_get_topology_for_port_copp_idx -
222 * retrieve topology of given port/copp_idx
223 *
224 * @port_id: Port ID number
225 * @copp_idx: copp index of ADM copp
226 *
227 * Returns valid value on success or 0 on failure
228 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530229int adm_get_topology_for_port_copp_idx(int port_id, int copp_idx)
230{
231 int port_idx = adm_validate_and_get_port_index(port_id);
232
233 if (port_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
234 pr_err("%s: Invalid port: 0x%x copp id: 0x%x",
235 __func__, port_id, copp_idx);
236 return 0;
237 }
238 return atomic_read(&this_adm.copp.topology[port_idx][copp_idx]);
239}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530240EXPORT_SYMBOL(adm_get_topology_for_port_copp_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530241
242int adm_get_indexes_from_copp_id(int copp_id, int *copp_idx, int *port_idx)
243{
244 int p_idx, c_idx;
245
246 for (p_idx = 0; p_idx < AFE_MAX_PORTS; p_idx++) {
247 for (c_idx = 0; c_idx < MAX_COPPS_PER_PORT; c_idx++) {
248 if (atomic_read(&this_adm.copp.id[p_idx][c_idx])
249 == copp_id) {
250 if (copp_idx != NULL)
251 *copp_idx = c_idx;
252 if (port_idx != NULL)
253 *port_idx = p_idx;
254 return 0;
255 }
256 }
257 }
258 return -EINVAL;
259}
260
261static int adm_get_copp_id(int port_idx, int copp_idx)
262{
263 pr_debug("%s: port_idx:%d copp_idx:%d\n", __func__, port_idx, copp_idx);
264
265 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
266 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
267 return -EINVAL;
268 }
269 return atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
270}
271
272static int adm_get_idx_if_copp_exists(int port_idx, int topology, int mode,
Sachin Mohan Gadag3c3c5812018-07-20 15:46:54 +0530273 int rate, int bit_width, int app_type,
274 int session_type)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530275{
276 int idx;
277
278 pr_debug("%s: port_idx-%d, topology-0x%x, mode-%d, rate-%d, bit_width-%d\n",
279 __func__, port_idx, topology, mode, rate, bit_width);
280
281 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++)
282 if ((topology ==
283 atomic_read(&this_adm.copp.topology[port_idx][idx])) &&
284 (mode == atomic_read(&this_adm.copp.mode[port_idx][idx])) &&
285 (rate == atomic_read(&this_adm.copp.rate[port_idx][idx])) &&
286 (bit_width ==
287 atomic_read(&this_adm.copp.bit_width[port_idx][idx])) &&
Sachin Mohan Gadag3c3c5812018-07-20 15:46:54 +0530288 (session_type ==
289 atomic_read(
290 &this_adm.copp.session_type[port_idx][idx])) &&
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530291 (app_type ==
292 atomic_read(&this_adm.copp.app_type[port_idx][idx])))
293 return idx;
294 return -EINVAL;
295}
296
297static int adm_get_next_available_copp(int port_idx)
298{
299 int idx;
300
301 pr_debug("%s:\n", __func__);
302 for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++) {
303 pr_debug("%s: copp_id:0x%x port_idx:%d idx:%d\n", __func__,
304 atomic_read(&this_adm.copp.id[port_idx][idx]),
305 port_idx, idx);
306 if (atomic_read(&this_adm.copp.id[port_idx][idx]) ==
307 RESET_COPP_ID)
308 break;
309 }
310 return idx;
311}
312
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530313/**
314 * srs_trumedia_open -
315 * command to set SRS trumedia open
316 *
317 * @port_id: Port ID number
318 * @copp_idx: copp index of ADM copp
319 * @srs_tech_id: SRS tech index
320 * @srs_params: params pointer
321 *
322 * Returns 0 on success or error on failure
323 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530324int srs_trumedia_open(int port_id, int copp_idx, __s32 srs_tech_id,
325 void *srs_params)
326{
327 struct adm_cmd_set_pp_params_inband_v5 *adm_params = NULL;
328 struct adm_cmd_set_pp_params_v5 *adm_params_ = NULL;
329 __s32 sz = 0, param_id, module_id = SRS_TRUMEDIA_MODULE_ID, outband = 0;
330 int ret = 0, port_idx;
331
332 pr_debug("SRS - %s", __func__);
333
334 port_id = afe_convert_virtual_to_portid(port_id);
335 port_idx = adm_validate_and_get_port_index(port_id);
336 if (port_idx < 0) {
337 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
338 return -EINVAL;
339 }
340 switch (srs_tech_id) {
341 case SRS_ID_GLOBAL: {
342 struct srs_trumedia_params_GLOBAL *glb_params = NULL;
343
344 sz = sizeof(struct adm_cmd_set_pp_params_inband_v5) +
345 sizeof(struct srs_trumedia_params_GLOBAL);
346 adm_params = kzalloc(sz, GFP_KERNEL);
347 if (!adm_params) {
348 pr_err("%s, adm params memory alloc failed\n",
349 __func__);
350 return -ENOMEM;
351 }
352 adm_params->payload_size =
353 sizeof(struct srs_trumedia_params_GLOBAL) +
354 sizeof(struct adm_param_data_v5);
355 param_id = SRS_TRUMEDIA_PARAMS;
356 adm_params->params.param_size =
357 sizeof(struct srs_trumedia_params_GLOBAL);
358 glb_params = (struct srs_trumedia_params_GLOBAL *)
359 ((u8 *)adm_params +
360 sizeof(struct adm_cmd_set_pp_params_inband_v5));
361 memcpy(glb_params, srs_params,
362 sizeof(struct srs_trumedia_params_GLOBAL));
363 break;
364 }
365 case SRS_ID_WOWHD: {
366 struct srs_trumedia_params_WOWHD *whd_params = NULL;
367
368 sz = sizeof(struct adm_cmd_set_pp_params_inband_v5) +
369 sizeof(struct srs_trumedia_params_WOWHD);
370 adm_params = kzalloc(sz, GFP_KERNEL);
371 if (!adm_params) {
372 pr_err("%s, adm params memory alloc failed\n",
373 __func__);
374 return -ENOMEM;
375 }
376 adm_params->payload_size =
377 sizeof(struct srs_trumedia_params_WOWHD) +
378 sizeof(struct adm_param_data_v5);
379 param_id = SRS_TRUMEDIA_PARAMS_WOWHD;
380 adm_params->params.param_size =
381 sizeof(struct srs_trumedia_params_WOWHD);
382 whd_params = (struct srs_trumedia_params_WOWHD *)
383 ((u8 *)adm_params +
384 sizeof(struct adm_cmd_set_pp_params_inband_v5));
385 memcpy(whd_params, srs_params,
386 sizeof(struct srs_trumedia_params_WOWHD));
387 break;
388 }
389 case SRS_ID_CSHP: {
390 struct srs_trumedia_params_CSHP *chp_params = NULL;
391
392 sz = sizeof(struct adm_cmd_set_pp_params_inband_v5) +
393 sizeof(struct srs_trumedia_params_CSHP);
394 adm_params = kzalloc(sz, GFP_KERNEL);
395 if (!adm_params) {
396 pr_err("%s, adm params memory alloc failed\n",
397 __func__);
398 return -ENOMEM;
399 }
400 adm_params->payload_size =
401 sizeof(struct srs_trumedia_params_CSHP) +
402 sizeof(struct adm_param_data_v5);
403 param_id = SRS_TRUMEDIA_PARAMS_CSHP;
404 adm_params->params.param_size =
405 sizeof(struct srs_trumedia_params_CSHP);
406 chp_params = (struct srs_trumedia_params_CSHP *)
407 ((u8 *)adm_params +
408 sizeof(struct adm_cmd_set_pp_params_inband_v5));
409 memcpy(chp_params, srs_params,
410 sizeof(struct srs_trumedia_params_CSHP));
411 break;
412 }
413 case SRS_ID_HPF: {
414 struct srs_trumedia_params_HPF *hpf_params = NULL;
415
416 sz = sizeof(struct adm_cmd_set_pp_params_inband_v5) +
417 sizeof(struct srs_trumedia_params_HPF);
418 adm_params = kzalloc(sz, GFP_KERNEL);
419 if (!adm_params) {
420 pr_err("%s, adm params memory alloc failed\n",
421 __func__);
422 return -ENOMEM;
423 }
424 adm_params->payload_size =
425 sizeof(struct srs_trumedia_params_HPF) +
426 sizeof(struct adm_param_data_v5);
427 param_id = SRS_TRUMEDIA_PARAMS_HPF;
428 adm_params->params.param_size =
429 sizeof(struct srs_trumedia_params_HPF);
430 hpf_params = (struct srs_trumedia_params_HPF *)
431 ((u8 *)adm_params +
432 sizeof(struct adm_cmd_set_pp_params_inband_v5));
433 memcpy(hpf_params, srs_params,
434 sizeof(struct srs_trumedia_params_HPF));
435 break;
436 }
437 case SRS_ID_AEQ: {
438 int *update_params_ptr = (int *)this_adm.outband_memmap.kvaddr;
439
440 outband = 1;
441 adm_params = kzalloc(sizeof(struct adm_cmd_set_pp_params_v5),
442 GFP_KERNEL);
443 adm_params_ = (struct adm_cmd_set_pp_params_v5 *)adm_params;
444 if (!adm_params_) {
445 pr_err("%s, adm params memory alloc failed\n",
446 __func__);
447 return -ENOMEM;
448 }
449
450 sz = sizeof(struct srs_trumedia_params_AEQ);
451 if (update_params_ptr == NULL) {
452 pr_err("ADM_SRS_TRUMEDIA - %s: null memmap for AEQ params\n",
453 __func__);
454 ret = -EINVAL;
455 goto fail_cmd;
456 }
457 param_id = SRS_TRUMEDIA_PARAMS_AEQ;
458 *update_params_ptr++ = module_id;
459 *update_params_ptr++ = param_id;
460 *update_params_ptr++ = sz;
461 memcpy(update_params_ptr, srs_params, sz);
462
463 adm_params_->payload_size = sz + 12;
464
465 break;
466 }
467 case SRS_ID_HL: {
468 struct srs_trumedia_params_HL *hl_params = NULL;
469
470 sz = sizeof(struct adm_cmd_set_pp_params_inband_v5) +
471 sizeof(struct srs_trumedia_params_HL);
472 adm_params = kzalloc(sz, GFP_KERNEL);
473 if (!adm_params) {
474 pr_err("%s, adm params memory alloc failed\n",
475 __func__);
476 return -ENOMEM;
477 }
478 adm_params->payload_size =
479 sizeof(struct srs_trumedia_params_HL) +
480 sizeof(struct adm_param_data_v5);
481 param_id = SRS_TRUMEDIA_PARAMS_HL;
482 adm_params->params.param_size =
483 sizeof(struct srs_trumedia_params_HL);
484 hl_params = (struct srs_trumedia_params_HL *)
485 ((u8 *)adm_params +
486 sizeof(struct adm_cmd_set_pp_params_inband_v5));
487 memcpy(hl_params, srs_params,
488 sizeof(struct srs_trumedia_params_HL));
489 break;
490 }
491 case SRS_ID_GEQ: {
492 struct srs_trumedia_params_GEQ *geq_params = NULL;
493
494 sz = sizeof(struct adm_cmd_set_pp_params_inband_v5) +
495 sizeof(struct srs_trumedia_params_GEQ);
496 adm_params = kzalloc(sz, GFP_KERNEL);
497 if (!adm_params) {
498 pr_err("%s, adm params memory alloc failed\n",
499 __func__);
500 return -ENOMEM;
501 }
502 adm_params->payload_size =
503 sizeof(struct srs_trumedia_params_GEQ) +
504 sizeof(struct adm_param_data_v5);
505 param_id = SRS_TRUMEDIA_PARAMS_GEQ;
506 adm_params->params.param_size =
507 sizeof(struct srs_trumedia_params_GEQ);
508 geq_params = (struct srs_trumedia_params_GEQ *)
509 ((u8 *)adm_params +
510 sizeof(struct adm_cmd_set_pp_params_inband_v5));
511 memcpy(geq_params, srs_params,
512 sizeof(struct srs_trumedia_params_GEQ));
513 pr_debug("SRS - %s: GEQ params prepared\n", __func__);
514 break;
515 }
516 default:
517 goto fail_cmd;
518 }
519
520 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
521 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
522 adm_params->hdr.src_svc = APR_SVC_ADM;
523 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
524 adm_params->hdr.src_port = port_id;
525 adm_params->hdr.dest_svc = APR_SVC_ADM;
526 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
527 adm_params->hdr.dest_port =
528 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
529 adm_params->hdr.token = port_idx << 16 | copp_idx;
530 adm_params->hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
531 if (outband && this_adm.outband_memmap.paddr) {
532 adm_params->hdr.pkt_size =
533 sizeof(struct adm_cmd_set_pp_params_v5);
534 adm_params->payload_addr_lsw = lower_32_bits(
535 this_adm.outband_memmap.paddr);
536 adm_params->payload_addr_msw = msm_audio_populate_upper_32_bits(
537 this_adm.outband_memmap.paddr);
538 adm_params->mem_map_handle = atomic_read(&this_adm.
539 mem_map_handles[ADM_SRS_TRUMEDIA]);
540 } else {
541 adm_params->hdr.pkt_size = sz;
542 adm_params->payload_addr_lsw = 0;
543 adm_params->payload_addr_msw = 0;
544 adm_params->mem_map_handle = 0;
545
546 adm_params->params.module_id = module_id;
547 adm_params->params.param_id = param_id;
548 adm_params->params.reserved = 0;
549 }
550
551 pr_debug("SRS - %s: Command was sent now check Q6 - port id = %d, size %d, module id %x, param id %x.\n",
552 __func__, adm_params->hdr.dest_port,
553 adm_params->payload_size, module_id, param_id);
554
555 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
556 ret = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
557 if (ret < 0) {
558 pr_err("SRS - %s: ADM enable for port %d failed\n", __func__,
559 port_id);
560 ret = -EINVAL;
561 goto fail_cmd;
562 }
563 /* Wait for the callback with copp id */
564 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
565 atomic_read(&this_adm.copp.stat
566 [port_idx][copp_idx]) >= 0,
567 msecs_to_jiffies(TIMEOUT_MS));
568 if (!ret) {
569 pr_err("%s: SRS set params timed out port = %d\n",
570 __func__, port_id);
571 ret = -EINVAL;
572 goto fail_cmd;
573 } else if (atomic_read(&this_adm.copp.stat
574 [port_idx][copp_idx]) > 0) {
575 pr_err("%s: DSP returned error[%s]\n",
576 __func__, adsp_err_get_err_str(
577 atomic_read(&this_adm.copp.stat
578 [port_idx][copp_idx])));
579 ret = adsp_err_get_lnx_err_code(
580 atomic_read(&this_adm.copp.stat
581 [port_idx][copp_idx]));
582 goto fail_cmd;
583 }
584
585fail_cmd:
586 kfree(adm_params);
587 return ret;
588}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530589EXPORT_SYMBOL(srs_trumedia_open);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530590
591static int adm_populate_channel_weight(u16 *ptr,
592 struct msm_pcm_channel_mixer *ch_mixer,
593 int channel_index)
594{
595 u16 i, j, start_index = 0;
596
597 if (channel_index > ch_mixer->output_channel) {
598 pr_err("%s: channel index %d is larger than output_channel %d\n",
599 __func__, channel_index, ch_mixer->output_channel);
600 return -EINVAL;
601 }
602
603 for (i = 0; i < ch_mixer->output_channel; i++) {
604 pr_debug("%s: weight for output %d:", __func__, i);
605 for (j = 0; j < ADM_MAX_CHANNELS; j++)
606 pr_debug(" %d",
607 ch_mixer->channel_weight[i][j]);
608 pr_debug("\n");
609 }
610
611 for (i = 0; i < channel_index; ++i)
612 start_index += ch_mixer->input_channels[i];
613
614 for (i = 0; i < ch_mixer->output_channel; ++i) {
615 for (j = start_index;
616 j < start_index +
617 ch_mixer->input_channels[channel_index]; j++) {
618 *ptr = ch_mixer->channel_weight[i][j];
619 pr_debug("%s: ptr[%d][%d] = %d\n",
620 __func__, i, j, *ptr);
621 ptr++;
622 }
623 }
624
625 return 0;
626}
627
628/*
629 * adm_programable_channel_mixer
630 *
631 * Receives port_id, copp_idx, session_id, session_type, ch_mixer
632 * and channel_index to send ADM command to mix COPP data.
633 *
634 * port_id - Passed value, port_id for which backend is wanted
635 * copp_idx - Passed value, copp_idx for which COPP is wanted
636 * session_id - Passed value, session_id for which session is needed
637 * session_type - Passed value, session_type for RX or TX
638 * ch_mixer - Passed value, ch_mixer for which channel mixer config is needed
639 * channel_index - Passed value, channel_index for which channel is needed
Dhanalakshmi Siddani040e0262018-11-26 23:01:26 +0530640 * use_default_chmap - true if default channel map to be used
641 * ch_map - input/output channel map for playback/capture session respectively
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530642 */
643int adm_programable_channel_mixer(int port_id, int copp_idx, int session_id,
644 int session_type,
645 struct msm_pcm_channel_mixer *ch_mixer,
Dhanalakshmi Siddani040e0262018-11-26 23:01:26 +0530646 int channel_index, bool use_default_chmap,
647 char *ch_map)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530648{
649 struct adm_cmd_set_pspd_mtmx_strtr_params_v5 *adm_params = NULL;
650 struct adm_param_data_v5 data_v5;
651 int ret = 0, port_idx, sz = 0, param_size = 0;
652 u16 *adm_pspd_params;
653 u16 *ptr;
Dhanalakshmi Siddani040e0262018-11-26 23:01:26 +0530654 int index = 0, i;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530655
656 pr_debug("%s: port_id = %d\n", __func__, port_id);
657 port_id = afe_convert_virtual_to_portid(port_id);
658 port_idx = adm_validate_and_get_port_index(port_id);
659 if (port_idx < 0) {
660 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
661 return -EINVAL;
662 }
663 /*
664 * First 8 bytes are 4 bytes as rule number, 2 bytes as output
665 * channel and 2 bytes as input channel.
666 * 2 * ch_mixer->output_channel means output channel mapping.
667 * 2 * ch_mixer->input_channels[channel_index]) means input
668 * channel mapping.
669 * 2 * ch_mixer->input_channels[channel_index] *
670 * ch_mixer->output_channel) means the channel mixer weighting
671 * coefficients.
672 * param_size needs to be a multiple of 4 bytes.
673 */
674
675 param_size = 2 * (4 + ch_mixer->output_channel +
676 ch_mixer->input_channels[channel_index] +
677 ch_mixer->input_channels[channel_index] *
678 ch_mixer->output_channel);
Dhanalakshmi Siddani51ff6552018-11-26 23:07:52 +0530679 /* Params size should be multiple of 4 bytes i.e 32bit aligned */
680 param_size = round_up(param_size, 4);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530681
682 sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5) +
683 sizeof(struct default_chmixer_param_id_coeff) +
684 sizeof(struct adm_param_data_v5) + param_size;
685 pr_debug("%s: sz = %d\n", __func__, sz);
686 adm_params = kzalloc(sz, GFP_KERNEL);
687 if (!adm_params)
688 return -ENOMEM;
689
690 adm_params->payload_addr_lsw = 0;
691 adm_params->payload_addr_msw = 0;
692 adm_params->mem_map_handle = 0;
693 adm_params->direction = session_type;
694 adm_params->sessionid = session_id;
695 pr_debug("%s: copp_id = %d, session id %d\n", __func__,
696 atomic_read(&this_adm.copp.id[port_idx][copp_idx]),
697 session_id);
698 adm_params->deviceid = atomic_read(
699 &this_adm.copp.id[port_idx][copp_idx]);
700 adm_params->reserved = 0;
701
702 data_v5.module_id = MTMX_MODULE_ID_DEFAULT_CHMIXER;
703 data_v5.param_id = DEFAULT_CHMIXER_PARAM_ID_COEFF;
704 data_v5.reserved = 0;
705 data_v5.param_size = param_size;
706 adm_params->payload_size =
707 sizeof(struct default_chmixer_param_id_coeff) +
708 sizeof(struct adm_param_data_v5) + data_v5.param_size;
709 adm_pspd_params = (u16 *)((u8 *)adm_params +
710 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5));
711 memcpy(adm_pspd_params, &data_v5, sizeof(data_v5));
712
713 adm_pspd_params = (u16 *)((u8 *)adm_params +
714 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5)
715 + sizeof(data_v5));
716
717 adm_pspd_params[0] = ch_mixer->rule;
718 adm_pspd_params[2] = ch_mixer->output_channel;
719 adm_pspd_params[3] = ch_mixer->input_channels[channel_index];
720 index = 4;
721
Dhanalakshmi Siddani040e0262018-11-26 23:01:26 +0530722 if ((session_type == SESSION_TYPE_TX) && !use_default_chmap && ch_map) {
723 for (i = 0; i < ch_mixer->output_channel; i++)
724 adm_pspd_params[index++] = ch_map[i];
725 } else {
726 if (ch_mixer->output_channel == 1) {
727 adm_pspd_params[index] = PCM_CHANNEL_FC;
728 } else if (ch_mixer->output_channel == 2) {
729 adm_pspd_params[index] = PCM_CHANNEL_FL;
730 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
731 } else if (ch_mixer->output_channel == 3) {
732 adm_pspd_params[index] = PCM_CHANNEL_FL;
733 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
734 adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
735 } else if (ch_mixer->output_channel == 4) {
736 adm_pspd_params[index] = PCM_CHANNEL_FL;
737 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
738 adm_pspd_params[index + 2] = PCM_CHANNEL_LS;
739 adm_pspd_params[index + 3] = PCM_CHANNEL_RS;
740 } else if (ch_mixer->output_channel == 5) {
741 adm_pspd_params[index] = PCM_CHANNEL_FL;
742 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
743 adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
744 adm_pspd_params[index + 3] = PCM_CHANNEL_LS;
745 adm_pspd_params[index + 4] = PCM_CHANNEL_RS;
746 } else if (ch_mixer->output_channel == 6) {
747 adm_pspd_params[index] = PCM_CHANNEL_FL;
748 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
749 adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
750 adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
751 adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
752 adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
753 } else if (ch_mixer->output_channel == 8) {
754 adm_pspd_params[index] = PCM_CHANNEL_FL;
755 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
756 adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
757 adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
758 adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
759 adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
760 adm_pspd_params[index + 6] = PCM_CHANNEL_LB;
761 adm_pspd_params[index + 7] = PCM_CHANNEL_RB;
762 }
763 index = index + ch_mixer->output_channel;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530764 }
765
Dhanalakshmi Siddani040e0262018-11-26 23:01:26 +0530766 if ((session_type == SESSION_TYPE_RX) && !use_default_chmap && ch_map) {
767 for (i = 0; i < ch_mixer->input_channels[channel_index]; i++)
768 adm_pspd_params[index++] = ch_map[i];
769 } else {
770 if (ch_mixer->input_channels[channel_index] == 1) {
771 adm_pspd_params[index] = PCM_CHANNEL_FC;
772 } else if (ch_mixer->input_channels[channel_index] == 2) {
773 adm_pspd_params[index] = PCM_CHANNEL_FL;
774 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
775 } else if (ch_mixer->input_channels[channel_index] == 3) {
776 adm_pspd_params[index] = PCM_CHANNEL_FL;
777 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
778 adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
779 } else if (ch_mixer->input_channels[channel_index] == 4) {
780 adm_pspd_params[index] = PCM_CHANNEL_FL;
781 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
782 adm_pspd_params[index + 2] = PCM_CHANNEL_LS;
783 adm_pspd_params[index + 3] = PCM_CHANNEL_RS;
784 } else if (ch_mixer->input_channels[channel_index] == 5) {
785 adm_pspd_params[index] = PCM_CHANNEL_FL;
786 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
787 adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
788 adm_pspd_params[index + 3] = PCM_CHANNEL_LS;
789 adm_pspd_params[index + 4] = PCM_CHANNEL_RS;
790 } else if (ch_mixer->input_channels[channel_index] == 6) {
791 adm_pspd_params[index] = PCM_CHANNEL_FL;
792 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
793 adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
794 adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
795 adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
796 adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
797 } else if (ch_mixer->input_channels[channel_index] == 8) {
798 adm_pspd_params[index] = PCM_CHANNEL_FL;
799 adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
800 adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
801 adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
802 adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
803 adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
804 adm_pspd_params[index + 6] = PCM_CHANNEL_LB;
805 adm_pspd_params[index + 7] = PCM_CHANNEL_RB;
806 }
807 index = index + ch_mixer->input_channels[channel_index];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530808 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530809 ret = adm_populate_channel_weight(&adm_pspd_params[index],
810 ch_mixer, channel_index);
Meng Wang47ea2ca2018-01-23 12:42:52 +0800811 if (ret) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530812 pr_err("%s: fail to get channel weight with error %d\n",
813 __func__, ret);
814 goto fail_cmd;
815 }
816
817 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
818 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
819 adm_params->hdr.src_svc = APR_SVC_ADM;
820 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
821 adm_params->hdr.src_port = port_id;
822 adm_params->hdr.dest_svc = APR_SVC_ADM;
823 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
824 adm_params->hdr.dest_port =
825 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
826 adm_params->hdr.token = port_idx << 16 | copp_idx;
827 adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5;
828 adm_params->hdr.pkt_size = sz;
829 adm_params->payload_addr_lsw = 0;
830 adm_params->payload_addr_msw = 0;
831 adm_params->mem_map_handle = 0;
832 adm_params->reserved = 0;
833
834 ptr = (u16 *)adm_params;
835 for (index = 0; index < (sz / 2); index++)
836 pr_debug("%s: adm_params[%d] = 0x%x\n",
837 __func__, index, (unsigned int)ptr[index]);
838
839 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], 0);
840 ret = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
841 if (ret < 0) {
842 pr_err("%s: Set params failed port %d rc %d\n", __func__,
843 port_id, ret);
844 ret = -EINVAL;
845 goto fail_cmd;
846 }
847
848 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
849 atomic_read(
850 &this_adm.copp.stat[port_idx][copp_idx]) >= 0,
851 msecs_to_jiffies(TIMEOUT_MS));
852 if (!ret) {
853 pr_err("%s: set params timed out port = %d\n",
854 __func__, port_id);
855 ret = -ETIMEDOUT;
856 goto fail_cmd;
857 }
858 ret = 0;
859fail_cmd:
860 kfree(adm_params);
861
862 return ret;
863}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530864EXPORT_SYMBOL(adm_programable_channel_mixer);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530865
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530866/**
867 * adm_set_stereo_to_custom_stereo -
868 * command to update custom stereo
869 *
870 * @port_id: Port ID number
871 * @copp_idx: copp index of ADM copp
872 * @session_id: session id to be updated
873 * @params: params pointer
874 * @param_length: length of params
875 *
876 * Returns 0 on success or error on failure
877 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530878int adm_set_stereo_to_custom_stereo(int port_id, int copp_idx,
879 unsigned int session_id, char *params,
880 uint32_t params_length)
881{
882 struct adm_cmd_set_pspd_mtmx_strtr_params_v5 *adm_params = NULL;
883 int sz, rc = 0, port_idx;
884
885 pr_debug("%s:\n", __func__);
886 port_id = afe_convert_virtual_to_portid(port_id);
887 port_idx = adm_validate_and_get_port_index(port_id);
888 if (port_idx < 0) {
889 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
890 return -EINVAL;
891 }
892
893 sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5) +
894 params_length;
895 adm_params = kzalloc(sz, GFP_KERNEL);
896 if (!adm_params) {
897 pr_err("%s, adm params memory alloc failed\n", __func__);
898 return -ENOMEM;
899 }
900
901 memcpy(((u8 *)adm_params +
902 sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5)),
903 params, params_length);
904 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
905 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
906 adm_params->hdr.pkt_size = sz;
907 adm_params->hdr.src_svc = APR_SVC_ADM;
908 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
909 adm_params->hdr.src_port = port_id;
910 adm_params->hdr.dest_svc = APR_SVC_ADM;
911 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
912 adm_params->hdr.dest_port = 0; /* Ignored */;
913 adm_params->hdr.token = port_idx << 16 | copp_idx;
914 adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5;
915 adm_params->payload_addr_lsw = 0;
916 adm_params->payload_addr_msw = 0;
917 adm_params->mem_map_handle = 0;
918 adm_params->payload_size = params_length;
919 /* direction RX as 0 */
920 adm_params->direction = ADM_MATRIX_ID_AUDIO_RX;
921 /* session id for this cmd to be applied on */
922 adm_params->sessionid = session_id;
923 adm_params->deviceid =
924 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
925 adm_params->reserved = 0;
926 pr_debug("%s: deviceid %d, session_id %d, src_port %d, dest_port %d\n",
927 __func__, adm_params->deviceid, adm_params->sessionid,
928 adm_params->hdr.src_port, adm_params->hdr.dest_port);
929 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
930 rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
931 if (rc < 0) {
932 pr_err("%s: Set params failed port = 0x%x rc %d\n",
933 __func__, port_id, rc);
934 rc = -EINVAL;
935 goto set_stereo_to_custom_stereo_return;
936 }
937 /* Wait for the callback */
938 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
939 atomic_read(&this_adm.copp.stat
940 [port_idx][copp_idx]) >= 0,
941 msecs_to_jiffies(TIMEOUT_MS));
942 if (!rc) {
943 pr_err("%s: Set params timed out port = 0x%x\n", __func__,
944 port_id);
945 rc = -EINVAL;
946 goto set_stereo_to_custom_stereo_return;
947 } else if (atomic_read(&this_adm.copp.stat
948 [port_idx][copp_idx]) > 0) {
949 pr_err("%s: DSP returned error[%s]\n", __func__,
950 adsp_err_get_err_str(atomic_read(
951 &this_adm.copp.stat
952 [port_idx][copp_idx])));
953 rc = adsp_err_get_lnx_err_code(
954 atomic_read(&this_adm.copp.stat
955 [port_idx][copp_idx]));
956 goto set_stereo_to_custom_stereo_return;
957 }
958 rc = 0;
959set_stereo_to_custom_stereo_return:
960 kfree(adm_params);
961 return rc;
962}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530963EXPORT_SYMBOL(adm_set_stereo_to_custom_stereo);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530964
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530965/**
966 * adm_dolby_dap_send_params -
967 * command to send dolby dap params
968 *
969 * @port_id: Port ID number
970 * @copp_idx: copp index of ADM copp
971 * @params: params pointer
972 * @param_length: length of params
973 *
974 * Returns 0 on success or error on failure
975 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530976int adm_dolby_dap_send_params(int port_id, int copp_idx, char *params,
977 uint32_t params_length)
978{
979 struct adm_cmd_set_pp_params_v5 *adm_params = NULL;
980 int sz, rc = 0;
981 int port_idx;
982
983 pr_debug("%s:\n", __func__);
984 port_id = afe_convert_virtual_to_portid(port_id);
985 port_idx = adm_validate_and_get_port_index(port_id);
986 if (port_idx < 0) {
987 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
988 return -EINVAL;
989 }
990
991 sz = sizeof(struct adm_cmd_set_pp_params_v5) + params_length;
992 adm_params = kzalloc(sz, GFP_KERNEL);
993 if (!adm_params) {
994 pr_err("%s, adm params memory alloc failed", __func__);
995 return -ENOMEM;
996 }
997
998 memcpy(((u8 *)adm_params + sizeof(struct adm_cmd_set_pp_params_v5)),
999 params, params_length);
1000 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1001 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
1002 adm_params->hdr.pkt_size = sz;
1003 adm_params->hdr.src_svc = APR_SVC_ADM;
1004 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
1005 adm_params->hdr.src_port = port_id;
1006 adm_params->hdr.dest_svc = APR_SVC_ADM;
1007 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
1008 adm_params->hdr.dest_port =
1009 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
1010 adm_params->hdr.token = port_idx << 16 | copp_idx;
1011 adm_params->hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
1012 adm_params->payload_addr_lsw = 0;
1013 adm_params->payload_addr_msw = 0;
1014 adm_params->mem_map_handle = 0;
1015 adm_params->payload_size = params_length;
1016
1017 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
1018 rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
1019 if (rc < 0) {
1020 pr_err("%s: Set params failed port = 0x%x rc %d\n",
1021 __func__, port_id, rc);
1022 rc = -EINVAL;
1023 goto dolby_dap_send_param_return;
1024 }
1025 /* Wait for the callback */
1026 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
1027 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
1028 msecs_to_jiffies(TIMEOUT_MS));
1029 if (!rc) {
1030 pr_err("%s: Set params timed out port = 0x%x\n",
1031 __func__, port_id);
1032 rc = -EINVAL;
1033 goto dolby_dap_send_param_return;
1034 } else if (atomic_read(&this_adm.copp.stat
1035 [port_idx][copp_idx]) > 0) {
1036 pr_err("%s: DSP returned error[%s]\n",
1037 __func__, adsp_err_get_err_str(
1038 atomic_read(&this_adm.copp.stat
1039 [port_idx][copp_idx])));
1040 rc = adsp_err_get_lnx_err_code(
1041 atomic_read(&this_adm.copp.stat
1042 [port_idx][copp_idx]));
1043 goto dolby_dap_send_param_return;
1044 }
1045 rc = 0;
1046dolby_dap_send_param_return:
1047 kfree(adm_params);
1048 return rc;
1049}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301050EXPORT_SYMBOL(adm_dolby_dap_send_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301051
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301052/**
1053 * adm_get_params_v5 -
1054 * command to retrieve ADM params for given module
1055 *
1056 * @port_id: Port ID number
1057 * @copp_idx: copp index of ADM copp
1058 * @params: params pointer
1059 * @param_length: length of params
1060 *
1061 * Returns 0 on success or error on failure
1062 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301063int adm_send_params_v5(int port_id, int copp_idx, char *params,
1064 uint32_t params_length)
1065{
1066 struct adm_cmd_set_pp_params_v5 *adm_params = NULL;
1067 int rc = 0;
1068 int sz, port_idx;
1069
1070 pr_debug("%s:\n", __func__);
1071 port_id = afe_convert_virtual_to_portid(port_id);
1072 port_idx = adm_validate_and_get_port_index(port_id);
1073 if (port_idx < 0) {
1074 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
1075 return -EINVAL;
1076 }
1077
1078 sz = sizeof(struct adm_cmd_set_pp_params_v5) + params_length;
1079 adm_params = kzalloc(sz, GFP_KERNEL);
1080 if (!adm_params) {
1081 pr_err("%s, adm params memory alloc failed", __func__);
1082 return -ENOMEM;
1083 }
1084
1085 memcpy(((u8 *)adm_params + sizeof(struct adm_cmd_set_pp_params_v5)),
1086 params, params_length);
1087 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1088 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
1089 adm_params->hdr.pkt_size = sz;
1090 adm_params->hdr.src_svc = APR_SVC_ADM;
1091 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
1092 adm_params->hdr.src_port = port_id;
1093 adm_params->hdr.dest_svc = APR_SVC_ADM;
1094 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
1095 adm_params->hdr.dest_port =
1096 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
1097 adm_params->hdr.token = port_idx << 16 | copp_idx;
1098 adm_params->hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
1099 adm_params->payload_addr_lsw = 0;
1100 adm_params->payload_addr_msw = 0;
1101 adm_params->mem_map_handle = 0;
1102 adm_params->payload_size = params_length;
1103
1104 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
1105 rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
1106 if (rc < 0) {
1107 pr_err("%s: Set params failed port = 0x%x rc %d\n",
1108 __func__, port_id, rc);
1109 rc = -EINVAL;
1110 goto send_param_return;
1111 }
1112 /* Wait for the callback */
1113 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
1114 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
1115 msecs_to_jiffies(TIMEOUT_MS));
1116 if (!rc) {
1117 pr_err("%s: Set params timed out port = 0x%x\n",
1118 __func__, port_id);
1119 rc = -EINVAL;
1120 goto send_param_return;
1121 } else if (atomic_read(&this_adm.copp.stat
1122 [port_idx][copp_idx]) > 0) {
1123 pr_err("%s: DSP returned error[%s]\n",
1124 __func__, adsp_err_get_err_str(
1125 atomic_read(&this_adm.copp.stat
1126 [port_idx][copp_idx])));
1127 rc = adsp_err_get_lnx_err_code(
1128 atomic_read(&this_adm.copp.stat
1129 [port_idx][copp_idx]));
1130 goto send_param_return;
1131 }
1132 rc = 0;
1133send_param_return:
1134 kfree(adm_params);
1135 return rc;
1136}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301137EXPORT_SYMBOL(adm_send_params_v5);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301138
1139int adm_get_params_v2(int port_id, int copp_idx, uint32_t module_id,
1140 uint32_t param_id, uint32_t params_length,
1141 char *params, uint32_t client_id)
1142{
1143 struct adm_cmd_get_pp_params_v5 *adm_params = NULL;
1144 int rc = 0, i = 0;
1145 int port_idx, idx;
1146 int *params_data = (int *)params;
1147 uint64_t sz = 0;
1148
1149 port_id = afe_convert_virtual_to_portid(port_id);
1150 port_idx = adm_validate_and_get_port_index(port_id);
1151 if (port_idx < 0) {
1152 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
1153 return -EINVAL;
1154 }
1155
1156 sz = (uint64_t)sizeof(struct adm_cmd_get_pp_params_v5) +
1157 (uint64_t)params_length;
1158 /*
1159 * Check if the value of "sz" (which is ultimately assigned to
1160 * "hdr.pkt_size") crosses U16_MAX.
1161 */
1162 if (sz > U16_MAX) {
1163 pr_err("%s: Invalid params_length\n", __func__);
1164 return -EINVAL;
1165 }
1166 adm_params = kzalloc(sz, GFP_KERNEL);
1167 if (!adm_params) {
1168 pr_err("%s: adm params memory alloc failed", __func__);
1169 return -ENOMEM;
1170 }
1171
1172 memcpy(((u8 *)adm_params + sizeof(struct adm_cmd_get_pp_params_v5)),
1173 params, params_length);
1174 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1175 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
1176 adm_params->hdr.pkt_size = sz;
1177 adm_params->hdr.src_svc = APR_SVC_ADM;
1178 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
1179 adm_params->hdr.src_port = port_id;
1180 adm_params->hdr.dest_svc = APR_SVC_ADM;
1181 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
1182 adm_params->hdr.dest_port =
1183 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
1184 adm_params->hdr.token = port_idx << 16 | client_id << 8 | copp_idx;
1185 adm_params->hdr.opcode = ADM_CMD_GET_PP_PARAMS_V5;
1186 adm_params->data_payload_addr_lsw = 0;
1187 adm_params->data_payload_addr_msw = 0;
1188 adm_params->mem_map_handle = 0;
1189 adm_params->module_id = module_id;
1190 adm_params->param_id = param_id;
1191 adm_params->param_max_size = params_length;
1192 adm_params->reserved = 0;
1193
1194 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
1195 rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
1196 if (rc < 0) {
1197 pr_err("%s: Failed to Get Params on port_id 0x%x %d\n",
1198 __func__, port_id, rc);
1199 rc = -EINVAL;
1200 goto adm_get_param_return;
1201 }
1202 /* Wait for the callback with copp id */
1203 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
1204 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
1205 msecs_to_jiffies(TIMEOUT_MS));
1206 if (!rc) {
1207 pr_err("%s: get params timed out port_id = 0x%x\n", __func__,
1208 port_id);
1209 rc = -EINVAL;
1210 goto adm_get_param_return;
1211 } else if (atomic_read(&this_adm.copp.stat
1212 [port_idx][copp_idx]) > 0) {
1213 pr_err("%s: DSP returned error[%s]\n",
1214 __func__, adsp_err_get_err_str(
1215 atomic_read(&this_adm.copp.stat
1216 [port_idx][copp_idx])));
1217 rc = adsp_err_get_lnx_err_code(
1218 atomic_read(&this_adm.copp.stat
1219 [port_idx][copp_idx]));
1220 goto adm_get_param_return;
1221 }
1222 idx = ADM_GET_PARAMETER_LENGTH * copp_idx;
1223
1224 if (adm_get_parameters[idx] < 0) {
1225 pr_err("%s: Size is invalid %d\n", __func__,
1226 adm_get_parameters[idx]);
1227 rc = -EINVAL;
1228 goto adm_get_param_return;
1229 }
1230 if ((params_data) &&
1231 (ARRAY_SIZE(adm_get_parameters) >
1232 idx) &&
1233 (ARRAY_SIZE(adm_get_parameters) >=
1234 1+adm_get_parameters[idx]+idx) &&
1235 (params_length/sizeof(uint32_t) >=
1236 adm_get_parameters[idx])) {
1237 for (i = 0; i < adm_get_parameters[idx]; i++)
1238 params_data[i] = adm_get_parameters[1+i+idx];
1239
1240 } else {
1241 pr_err("%s: Get param data not copied! get_param array size %zd, index %d, params array size %zd, index %d\n",
1242 __func__, ARRAY_SIZE(adm_get_parameters),
1243 (1+adm_get_parameters[idx]+idx),
1244 params_length/sizeof(int),
1245 adm_get_parameters[idx]);
1246 }
1247 rc = 0;
1248adm_get_param_return:
1249 kfree(adm_params);
1250
1251 return rc;
1252}
1253
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301254/**
1255 * adm_get_params -
1256 * command to retrieve ADM params for given module
1257 *
1258 * @port_id: Port ID number
1259 * @copp_idx: copp index of ADM copp
1260 * @module_id: module ID
1261 * @param_id: Param index
1262 * @param_length: length of params
1263 * @params: params pointer
1264 *
1265 * Returns 0 on success or error on failure
1266 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301267int adm_get_params(int port_id, int copp_idx, uint32_t module_id,
1268 uint32_t param_id, uint32_t params_length, char *params)
1269{
1270 return adm_get_params_v2(port_id, copp_idx, module_id, param_id,
1271 params_length, params, 0);
1272}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301273EXPORT_SYMBOL(adm_get_params);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301274
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301275/**
1276 * adm_get_pp_topo_module_list -
1277 * command to update PP top module list
1278 *
1279 * @port_id: Port ID number
1280 * @copp_idx: copp index of ADM copp
1281 * @param_length: length of params
1282 * @params: pointer with PP top module params
1283 *
1284 * Returns 0 on success or error on failure
1285 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301286int adm_get_pp_topo_module_list(int port_id, int copp_idx, int32_t param_length,
1287 char *params)
1288{
1289 struct adm_cmd_get_pp_topo_module_list_t *adm_pp_module_list = NULL;
1290 int sz, rc = 0, i = 0;
1291 int port_idx, idx;
1292 int32_t *params_data = (int32_t *)params;
1293 int *topo_list;
1294
1295 pr_debug("%s : port_id %x", __func__, port_id);
1296 port_id = afe_convert_virtual_to_portid(port_id);
1297 port_idx = adm_validate_and_get_port_index(port_id);
1298 if (port_idx < 0) {
1299 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
1300 return -EINVAL;
1301 }
1302
1303 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
1304 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
1305 return -EINVAL;
1306 }
1307
1308 sz = sizeof(struct adm_cmd_get_pp_topo_module_list_t) + param_length;
1309 adm_pp_module_list = kzalloc(sz, GFP_KERNEL);
1310 if (!adm_pp_module_list) {
1311 pr_err("%s, adm params memory alloc failed", __func__);
1312 return -ENOMEM;
1313 }
1314
1315 memcpy(((u8 *)adm_pp_module_list +
1316 sizeof(struct adm_cmd_get_pp_topo_module_list_t)),
1317 params, param_length);
1318 adm_pp_module_list->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1319 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
1320 adm_pp_module_list->hdr.pkt_size = sz;
1321 adm_pp_module_list->hdr.src_svc = APR_SVC_ADM;
1322 adm_pp_module_list->hdr.src_domain = APR_DOMAIN_APPS;
1323 adm_pp_module_list->hdr.src_port = port_id;
1324 adm_pp_module_list->hdr.dest_svc = APR_SVC_ADM;
1325 adm_pp_module_list->hdr.dest_domain = APR_DOMAIN_ADSP;
1326 adm_pp_module_list->hdr.dest_port =
1327 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
1328 adm_pp_module_list->hdr.token = port_idx << 16 | copp_idx;
1329 adm_pp_module_list->hdr.opcode = ADM_CMD_GET_PP_TOPO_MODULE_LIST;
1330 adm_pp_module_list->param_max_size = param_length;
1331 /* Payload address and mmap handle set to zero by kzalloc */
1332
1333 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
1334
1335 rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_pp_module_list);
1336 if (rc < 0) {
1337 pr_err("%s: Failed to Get Params on port %d\n", __func__,
1338 port_id);
1339 rc = -EINVAL;
1340 goto adm_pp_module_list_l;
1341 }
1342 /* Wait for the callback with copp id */
1343 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
1344 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
1345 msecs_to_jiffies(TIMEOUT_MS));
1346 if (!rc) {
1347 pr_err("%s: get params timed out port = %d\n", __func__,
1348 port_id);
1349 rc = -EINVAL;
1350 goto adm_pp_module_list_l;
1351 } else if (atomic_read(&this_adm.copp.stat
1352 [port_idx][copp_idx]) > 0) {
1353 pr_err("%s: DSP returned error[%s]\n",
1354 __func__, adsp_err_get_err_str(
1355 atomic_read(&this_adm.copp.stat
1356 [port_idx][copp_idx])));
1357 rc = adsp_err_get_lnx_err_code(
1358 atomic_read(&this_adm.copp.stat
1359 [port_idx][copp_idx]));
1360 goto adm_pp_module_list_l;
1361 }
1362 if (params_data) {
1363 idx = ADM_GET_TOPO_MODULE_LIST_LENGTH * copp_idx;
1364 topo_list = (int *)(adm_module_topo_list + idx);
1365 if (param_length <= ADM_GET_TOPO_MODULE_LIST_LENGTH &&
1366 idx <
1367 (MAX_COPPS_PER_PORT * ADM_GET_TOPO_MODULE_LIST_LENGTH))
1368 memcpy(params_data, topo_list, param_length);
1369 else
1370 pr_debug("%s: i/p size:%d > MAX param size:%d\n",
1371 __func__, param_length,
1372 (int)ADM_GET_TOPO_MODULE_LIST_LENGTH);
1373 for (i = 1; i <= params_data[0]; i++)
1374 pr_debug("module = 0x%x\n", params_data[i]);
1375 }
1376 rc = 0;
1377adm_pp_module_list_l:
1378 kfree(adm_pp_module_list);
1379 pr_debug("%s : rc = %d ", __func__, rc);
1380 return rc;
1381}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301382EXPORT_SYMBOL(adm_get_pp_topo_module_list);
1383
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301384static void adm_callback_debug_print(struct apr_client_data *data)
1385{
1386 uint32_t *payload;
1387
1388 payload = data->payload;
1389
1390 if (data->payload_size >= 8)
1391 pr_debug("%s: code = 0x%x PL#0[0x%x], PL#1[0x%x], size = %d\n",
1392 __func__, data->opcode, payload[0], payload[1],
1393 data->payload_size);
1394 else if (data->payload_size >= 4)
1395 pr_debug("%s: code = 0x%x PL#0[0x%x], size = %d\n",
1396 __func__, data->opcode, payload[0],
1397 data->payload_size);
1398 else
1399 pr_debug("%s: code = 0x%x, size = %d\n",
1400 __func__, data->opcode, data->payload_size);
1401}
1402
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301403/**
1404 * adm_set_multi_ch_map -
1405 * Update multi channel map info
1406 *
1407 * @channel_map: pointer with channel map info
1408 * @path: direction or ADM path type
1409 *
1410 * Returns 0 on success or error on failure
1411 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301412int adm_set_multi_ch_map(char *channel_map, int path)
1413{
1414 int idx;
1415
1416 if (path == ADM_PATH_PLAYBACK) {
1417 idx = ADM_MCH_MAP_IDX_PLAYBACK;
1418 } else if (path == ADM_PATH_LIVE_REC) {
1419 idx = ADM_MCH_MAP_IDX_REC;
1420 } else {
1421 pr_err("%s: invalid attempt to set path %d\n", __func__, path);
1422 return -EINVAL;
1423 }
1424
1425 memcpy(multi_ch_maps[idx].channel_mapping, channel_map,
1426 PCM_FORMAT_MAX_NUM_CHANNEL);
1427 multi_ch_maps[idx].set_channel_map = true;
1428
1429 return 0;
1430}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301431EXPORT_SYMBOL(adm_set_multi_ch_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301432
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301433/**
1434 * adm_get_multi_ch_map -
1435 * Retrieves multi channel map info
1436 *
1437 * @channel_map: pointer to be updated with channel map
1438 * @path: direction or ADM path type
1439 *
1440 * Returns 0 on success or error on failure
1441 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301442int adm_get_multi_ch_map(char *channel_map, int path)
1443{
1444 int idx;
1445
1446 if (path == ADM_PATH_PLAYBACK) {
1447 idx = ADM_MCH_MAP_IDX_PLAYBACK;
1448 } else if (path == ADM_PATH_LIVE_REC) {
1449 idx = ADM_MCH_MAP_IDX_REC;
1450 } else {
1451 pr_err("%s: invalid attempt to get path %d\n", __func__, path);
1452 return -EINVAL;
1453 }
1454
1455 if (multi_ch_maps[idx].set_channel_map) {
1456 memcpy(channel_map, multi_ch_maps[idx].channel_mapping,
1457 PCM_FORMAT_MAX_NUM_CHANNEL);
1458 }
1459
1460 return 0;
1461}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301462EXPORT_SYMBOL(adm_get_multi_ch_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301463
Rohit kumar5d860f42019-02-01 18:01:12 +05301464/**
1465 * adm_set_port_multi_ch_map -
1466 * Update port specific channel map info
1467 *
1468 * @channel_map: pointer with channel map info
1469 * @port_id: port for which chmap is set
1470 */
1471void adm_set_port_multi_ch_map(char *channel_map, int port_id)
1472{
1473 int port_idx;
1474
1475 port_id = q6audio_convert_virtual_to_portid(port_id);
1476 port_idx = adm_validate_and_get_port_index(port_id);
1477
1478 if (port_idx < 0) {
1479 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
1480 return;
1481 }
1482
1483 memcpy(port_channel_map[port_idx].channel_mapping, channel_map,
1484 PCM_FORMAT_MAX_NUM_CHANNEL);
1485 port_channel_map[port_idx].set_channel_map = true;
1486}
1487EXPORT_SYMBOL(adm_set_port_multi_ch_map);
1488
Laxminath Kasam468ece32017-11-28 12:40:22 +05301489static void adm_reset_data(void)
1490{
1491 int i, j;
1492
1493 apr_reset(this_adm.apr);
1494 for (i = 0; i < AFE_MAX_PORTS; i++) {
1495 for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
1496 atomic_set(&this_adm.copp.id[i][j],
1497 RESET_COPP_ID);
1498 atomic_set(&this_adm.copp.cnt[i][j], 0);
1499 atomic_set(
1500 &this_adm.copp.topology[i][j], 0);
1501 atomic_set(&this_adm.copp.mode[i][j],
1502 0);
1503 atomic_set(&this_adm.copp.stat[i][j],
1504 0);
1505 atomic_set(&this_adm.copp.rate[i][j],
1506 0);
1507 atomic_set(
1508 &this_adm.copp.channels[i][j],
1509 0);
1510 atomic_set(
1511 &this_adm.copp.bit_width[i][j], 0);
1512 atomic_set(
1513 &this_adm.copp.app_type[i][j], 0);
1514 atomic_set(
1515 &this_adm.copp.acdb_id[i][j], 0);
Sachin Mohan Gadag3c3c5812018-07-20 15:46:54 +05301516 atomic_set(
1517 &this_adm.copp.session_type[i][j], 0);
Laxminath Kasam468ece32017-11-28 12:40:22 +05301518 this_adm.copp.adm_status[i][j] =
1519 ADM_STATUS_CALIBRATION_REQUIRED;
1520 }
1521 }
1522 this_adm.apr = NULL;
1523 cal_utils_clear_cal_block_q6maps(ADM_MAX_CAL_TYPES,
1524 this_adm.cal_data);
1525 mutex_lock(&this_adm.cal_data
1526 [ADM_CUSTOM_TOP_CAL]->lock);
1527 this_adm.set_custom_topology = 1;
1528 mutex_unlock(&this_adm.cal_data[
1529 ADM_CUSTOM_TOP_CAL]->lock);
1530 rtac_clear_mapping(ADM_RTAC_CAL);
1531 /*
1532 * Free the ION memory and clear the map handles
1533 * for Source Tracking
1534 */
1535 if (this_adm.sourceTrackingData.memmap.paddr != 0) {
1536 msm_audio_ion_free(
1537 this_adm.sourceTrackingData.ion_client,
1538 this_adm.sourceTrackingData.ion_handle);
1539 this_adm.sourceTrackingData.ion_client = NULL;
1540 this_adm.sourceTrackingData.ion_handle = NULL;
1541 this_adm.sourceTrackingData.memmap.size = 0;
1542 this_adm.sourceTrackingData.memmap.kvaddr =
1543 NULL;
1544 this_adm.sourceTrackingData.memmap.paddr = 0;
1545 this_adm.sourceTrackingData.apr_cmd_status = -1;
1546 atomic_set(&this_adm.mem_map_handles[
1547 ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
1548 }
1549}
1550
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301551static int32_t adm_callback(struct apr_client_data *data, void *priv)
1552{
1553 uint32_t *payload;
Laxminath Kasam468ece32017-11-28 12:40:22 +05301554 int i, port_idx, copp_idx, idx, client_id;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301555
1556 if (data == NULL) {
1557 pr_err("%s: data parameter is null\n", __func__);
1558 return -EINVAL;
1559 }
1560
1561 payload = data->payload;
1562
1563 if (data->opcode == RESET_EVENTS) {
1564 pr_debug("%s: Reset event is received: %d %d apr[%pK]\n",
1565 __func__,
1566 data->reset_event, data->reset_proc, this_adm.apr);
Laxminath Kasam468ece32017-11-28 12:40:22 +05301567 if (this_adm.apr)
1568 adm_reset_data();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301569 return 0;
1570 }
1571
1572 adm_callback_debug_print(data);
Vignesh Kulothungan35274962019-01-22 11:13:09 -08001573 if (data->payload_size >= sizeof(uint32_t)) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301574 copp_idx = (data->token) & 0XFF;
1575 port_idx = ((data->token) >> 16) & 0xFF;
1576 client_id = ((data->token) >> 8) & 0xFF;
1577 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
1578 pr_err("%s: Invalid port idx %d token %d\n",
1579 __func__, port_idx, data->token);
1580 return 0;
1581 }
1582 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
1583 pr_err("%s: Invalid copp idx %d token %d\n",
1584 __func__, copp_idx, data->token);
1585 return 0;
1586 }
1587 if (client_id < 0 || client_id >= ADM_CLIENT_ID_MAX) {
1588 pr_err("%s: Invalid client id %d\n", __func__,
1589 client_id);
1590 return 0;
1591 }
1592 if (data->opcode == APR_BASIC_RSP_RESULT) {
1593 pr_debug("%s: APR_BASIC_RSP_RESULT id 0x%x\n",
1594 __func__, payload[0]);
Vignesh Kulothungan35274962019-01-22 11:13:09 -08001595 if (!((client_id != ADM_CLIENT_ID_SOURCE_TRACKING) &&
1596 (payload[0] == ADM_CMD_SET_PP_PARAMS_V5))) {
1597 if (data->payload_size <
1598 (2 * sizeof(uint32_t))) {
1599 pr_err("%s: Invalid payload size %d\n",
1600 __func__, data->payload_size);
1601 return 0;
1602 }
1603 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301604 if (payload[1] != 0) {
1605 pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
1606 __func__, payload[0], payload[1]);
1607 }
1608 switch (payload[0]) {
1609 case ADM_CMD_SET_PP_PARAMS_V5:
1610 pr_debug("%s: ADM_CMD_SET_PP_PARAMS_V5\n",
1611 __func__);
1612 if (client_id == ADM_CLIENT_ID_SOURCE_TRACKING)
1613 this_adm.sourceTrackingData.
1614 apr_cmd_status = payload[1];
1615 else if (rtac_make_adm_callback(payload,
1616 data->payload_size))
1617 break;
1618 /*
1619 * if soft volume is called and already
1620 * interrupted break out of the sequence here
1621 */
1622 case ADM_CMD_DEVICE_OPEN_V5:
1623 case ADM_CMD_DEVICE_CLOSE_V5:
1624 case ADM_CMD_DEVICE_OPEN_V6:
1625 pr_debug("%s: Basic callback received, wake up.\n",
1626 __func__);
1627 atomic_set(&this_adm.copp.stat[port_idx]
1628 [copp_idx], payload[1]);
1629 wake_up(
1630 &this_adm.copp.wait[port_idx][copp_idx]);
1631 break;
1632 case ADM_CMD_ADD_TOPOLOGIES:
1633 pr_debug("%s: callback received, ADM_CMD_ADD_TOPOLOGIES.\n",
1634 __func__);
1635 atomic_set(&this_adm.adm_stat, payload[1]);
1636 wake_up(&this_adm.adm_wait);
1637 break;
1638 case ADM_CMD_MATRIX_MAP_ROUTINGS_V5:
1639 case ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5:
1640 pr_debug("%s: Basic callback received, wake up.\n",
1641 __func__);
1642 atomic_set(&this_adm.matrix_map_stat,
1643 payload[1]);
1644 wake_up(&this_adm.matrix_map_wait);
1645 break;
1646 case ADM_CMD_SHARED_MEM_UNMAP_REGIONS:
1647 pr_debug("%s: ADM_CMD_SHARED_MEM_UNMAP_REGIONS\n",
1648 __func__);
1649 atomic_set(&this_adm.adm_stat, payload[1]);
1650 wake_up(&this_adm.adm_wait);
1651 break;
1652 case ADM_CMD_SHARED_MEM_MAP_REGIONS:
1653 pr_debug("%s: ADM_CMD_SHARED_MEM_MAP_REGIONS\n",
1654 __func__);
1655 /* Should only come here if there is an APR */
1656 /* error or malformed APR packet. Otherwise */
1657 /* response will be returned as */
1658 if (payload[1] != 0) {
1659 pr_err("%s: ADM map error, resuming\n",
1660 __func__);
1661 atomic_set(&this_adm.adm_stat,
1662 payload[1]);
1663 wake_up(&this_adm.adm_wait);
1664 }
1665 break;
1666 case ADM_CMD_GET_PP_PARAMS_V5:
1667 pr_debug("%s: ADM_CMD_GET_PP_PARAMS_V5\n",
1668 __func__);
1669 /* Should only come here if there is an APR */
1670 /* error or malformed APR packet. Otherwise */
1671 /* response will be returned as */
1672 /* ADM_CMDRSP_GET_PP_PARAMS_V5 */
1673 if (client_id ==
1674 ADM_CLIENT_ID_SOURCE_TRACKING) {
1675 this_adm.sourceTrackingData.
1676 apr_cmd_status = payload[1];
1677 if (payload[1] != 0)
1678 pr_err("%s: ADM get param error = %d\n",
1679 __func__, payload[1]);
1680
1681 atomic_set(&this_adm.copp.stat
1682 [port_idx][copp_idx],
1683 payload[1]);
1684 wake_up(&this_adm.copp.wait
1685 [port_idx][copp_idx]);
1686 } else {
1687 if (payload[1] != 0) {
1688 pr_err("%s: ADM get param error = %d, resuming\n",
1689 __func__, payload[1]);
1690
1691 rtac_make_adm_callback(payload,
1692 data->payload_size);
1693 }
1694 }
1695 break;
1696 case ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5:
1697 pr_debug("%s: ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5\n",
1698 __func__);
1699 atomic_set(&this_adm.copp.stat[port_idx]
1700 [copp_idx], payload[1]);
1701 wake_up(
1702 &this_adm.copp.wait[port_idx][copp_idx]);
1703 break;
1704 case ADM_CMD_GET_PP_TOPO_MODULE_LIST:
1705 pr_debug("%s:ADM_CMD_GET_PP_TOPO_MODULE_LIST\n",
1706 __func__);
1707 if (payload[1] != 0)
1708 pr_err("%s: ADM get topo list error = %d,\n",
1709 __func__, payload[1]);
1710 break;
1711 default:
1712 pr_err("%s: Unknown Cmd: 0x%x\n", __func__,
1713 payload[0]);
1714 break;
1715 }
1716 return 0;
1717 }
1718
1719 switch (data->opcode) {
1720 case ADM_CMDRSP_DEVICE_OPEN_V5:
1721 case ADM_CMDRSP_DEVICE_OPEN_V6: {
Vignesh Kulothungan35274962019-01-22 11:13:09 -08001722 struct adm_cmd_rsp_device_open_v5 *open = NULL;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301723
Vignesh Kulothungan35274962019-01-22 11:13:09 -08001724 if (data->payload_size <
1725 sizeof(struct adm_cmd_rsp_device_open_v5)) {
1726 pr_err("%s: Invalid payload size %d\n",
1727 __func__, data->payload_size);
1728 return 0;
1729 }
1730 open =
1731 (struct adm_cmd_rsp_device_open_v5 *)data->payload;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301732 if (open->copp_id == INVALID_COPP_ID) {
1733 pr_err("%s: invalid coppid rxed %d\n",
1734 __func__, open->copp_id);
1735 atomic_set(&this_adm.copp.stat[port_idx]
1736 [copp_idx], ADSP_EBADPARAM);
1737 wake_up(
1738 &this_adm.copp.wait[port_idx][copp_idx]);
1739 break;
1740 }
1741 atomic_set(&this_adm.copp.stat
1742 [port_idx][copp_idx], payload[0]);
1743 atomic_set(&this_adm.copp.id[port_idx][copp_idx],
1744 open->copp_id);
1745 pr_debug("%s: coppid rxed=%d\n", __func__,
1746 open->copp_id);
1747 wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
1748 }
1749 break;
1750 case ADM_CMDRSP_GET_PP_PARAMS_V5:
1751 pr_debug("%s: ADM_CMDRSP_GET_PP_PARAMS_V5\n", __func__);
1752 if (payload[0] != 0)
1753 pr_err("%s: ADM_CMDRSP_GET_PP_PARAMS_V5 returned error = 0x%x\n",
1754 __func__, payload[0]);
1755 if (client_id == ADM_CLIENT_ID_SOURCE_TRACKING)
1756 this_adm.sourceTrackingData.apr_cmd_status =
1757 payload[0];
1758 else if (rtac_make_adm_callback(payload,
1759 data->payload_size))
1760 break;
1761
1762 idx = ADM_GET_PARAMETER_LENGTH * copp_idx;
1763 if ((payload[0] == 0) && (data->payload_size >
1764 (4 * sizeof(*payload))) &&
1765 (data->payload_size - 4 >=
1766 payload[3]) &&
1767 (ARRAY_SIZE(adm_get_parameters) >
1768 idx) &&
1769 (ARRAY_SIZE(adm_get_parameters)-idx-1 >=
1770 payload[3])) {
1771 adm_get_parameters[idx] = payload[3] /
1772 sizeof(uint32_t);
1773 /*
1774 * payload[3] is param_size which is
1775 * expressed in number of bytes
1776 */
1777 pr_debug("%s: GET_PP PARAM:received parameter length: 0x%x\n",
1778 __func__, adm_get_parameters[idx]);
1779 /* storing param size then params */
1780 for (i = 0; i < payload[3] /
1781 sizeof(uint32_t); i++)
1782 adm_get_parameters[idx+1+i] =
1783 payload[4+i];
1784 } else if (payload[0] == 0) {
1785 adm_get_parameters[idx] = -1;
1786 pr_err("%s: Out of band case, setting size to %d\n",
1787 __func__, adm_get_parameters[idx]);
1788 } else {
1789 adm_get_parameters[idx] = -1;
1790 pr_err("%s: GET_PP_PARAMS failed, setting size to %d\n",
1791 __func__, adm_get_parameters[idx]);
1792 }
1793 atomic_set(&this_adm.copp.stat
1794 [port_idx][copp_idx], payload[0]);
1795 wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
1796 break;
1797 case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST:
1798 pr_debug("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST\n",
1799 __func__);
1800 if (payload[0] != 0) {
1801 pr_err("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST",
1802 __func__);
1803 pr_err(":err = 0x%x\n", payload[0]);
Vignesh Kulothungan35274962019-01-22 11:13:09 -08001804 } else if (data->payload_size >=
1805 (2 * sizeof(uint32_t))) {
Soumya Managoli4c223ab2019-08-28 16:47:22 +05301806 if ((payload[1] >
Vignesh Kulothungan35274962019-01-22 11:13:09 -08001807 ((ADM_GET_TOPO_MODULE_LIST_LENGTH /
Soumya Managoli4c223ab2019-08-28 16:47:22 +05301808 sizeof(uint32_t)) - 1)) ||
1809 ((data->payload_size -
1810 (2 * sizeof(uint32_t))) <
1811 (payload[1] * sizeof(uint32_t)))) {
Vignesh Kulothungan35274962019-01-22 11:13:09 -08001812 pr_err("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST",
1813 __func__);
1814 pr_err(":size = %d\n", payload[1]);
1815 } else {
1816 idx = ADM_GET_TOPO_MODULE_LIST_LENGTH *
1817 copp_idx;
1818 pr_debug("%s:Num modules payload[1] %d\n",
1819 __func__, payload[1]);
1820 adm_module_topo_list[idx] = payload[1];
1821 for (i = 1; i <= payload[1]; i++) {
1822 adm_module_topo_list[idx+i] =
1823 payload[1+i];
1824 pr_debug("%s:payload[%d] = %x\n",
Soumya Managoli4c223ab2019-08-28 16:47:22 +05301825 __func__, (i+1),
1826 payload[1+i]);
Vignesh Kulothungan35274962019-01-22 11:13:09 -08001827 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301828 }
Vignesh Kulothungan35274962019-01-22 11:13:09 -08001829 } else
1830 pr_err("%s: Invalid payload size %d\n",
1831 __func__, data->payload_size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301832 atomic_set(&this_adm.copp.stat
1833 [port_idx][copp_idx], payload[0]);
1834 wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
1835 break;
1836 case ADM_CMDRSP_SHARED_MEM_MAP_REGIONS:
1837 pr_debug("%s: ADM_CMDRSP_SHARED_MEM_MAP_REGIONS\n",
1838 __func__);
1839 atomic_set(&this_adm.mem_map_handles[
1840 atomic_read(&this_adm.mem_map_index)],
1841 *payload);
1842 atomic_set(&this_adm.adm_stat, 0);
1843 wake_up(&this_adm.adm_wait);
1844 break;
1845 default:
1846 pr_err("%s: Unknown cmd:0x%x\n", __func__,
1847 data->opcode);
1848 break;
1849 }
1850 }
1851 return 0;
1852}
1853
1854static int adm_memory_map_regions(phys_addr_t *buf_add, uint32_t mempool_id,
1855 uint32_t *bufsz, uint32_t bufcnt)
1856{
1857 struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
1858 struct avs_shared_map_region_payload *mregions = NULL;
1859 void *mmap_region_cmd = NULL;
1860 void *payload = NULL;
1861 int ret = 0;
1862 int i = 0;
1863 int cmd_size = 0;
1864
1865 pr_debug("%s:\n", __func__);
1866 if (this_adm.apr == NULL) {
1867 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
1868 0xFFFFFFFF, &this_adm);
1869 if (this_adm.apr == NULL) {
1870 pr_err("%s: Unable to register ADM\n", __func__);
1871 ret = -ENODEV;
1872 return ret;
1873 }
1874 rtac_set_adm_handle(this_adm.apr);
1875 }
1876
1877 cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
1878 + sizeof(struct avs_shared_map_region_payload)
1879 * bufcnt;
1880
1881 mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
1882 if (!mmap_region_cmd)
1883 return -ENOMEM;
1884
1885 mmap_regions = (struct avs_cmd_shared_mem_map_regions *)mmap_region_cmd;
1886 mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1887 APR_HDR_LEN(APR_HDR_SIZE),
1888 APR_PKT_VER);
1889 mmap_regions->hdr.pkt_size = cmd_size;
1890 mmap_regions->hdr.src_port = 0;
1891
1892 mmap_regions->hdr.dest_port = 0;
1893 mmap_regions->hdr.token = 0;
1894 mmap_regions->hdr.opcode = ADM_CMD_SHARED_MEM_MAP_REGIONS;
1895 mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL & 0x00ff;
1896 mmap_regions->num_regions = bufcnt & 0x00ff;
1897 mmap_regions->property_flag = 0x00;
1898
1899 pr_debug("%s: map_regions->num_regions = %d\n", __func__,
1900 mmap_regions->num_regions);
1901 payload = ((u8 *) mmap_region_cmd +
1902 sizeof(struct avs_cmd_shared_mem_map_regions));
1903 mregions = (struct avs_shared_map_region_payload *)payload;
1904
1905 for (i = 0; i < bufcnt; i++) {
1906 mregions->shm_addr_lsw = lower_32_bits(buf_add[i]);
1907 mregions->shm_addr_msw =
1908 msm_audio_populate_upper_32_bits(buf_add[i]);
1909 mregions->mem_size_bytes = bufsz[i];
1910 ++mregions;
1911 }
1912
1913 atomic_set(&this_adm.adm_stat, -1);
1914 ret = apr_send_pkt(this_adm.apr, (uint32_t *) mmap_region_cmd);
1915 if (ret < 0) {
1916 pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__,
1917 mmap_regions->hdr.opcode, ret);
1918 ret = -EINVAL;
1919 goto fail_cmd;
1920 }
1921
1922 ret = wait_event_timeout(this_adm.adm_wait,
1923 atomic_read(&this_adm.adm_stat) >= 0,
1924 5 * HZ);
1925 if (!ret) {
1926 pr_err("%s: timeout. waited for memory_map\n", __func__);
1927 ret = -EINVAL;
1928 goto fail_cmd;
1929 } else if (atomic_read(&this_adm.adm_stat) > 0) {
1930 pr_err("%s: DSP returned error[%s]\n",
1931 __func__, adsp_err_get_err_str(
1932 atomic_read(&this_adm.adm_stat)));
1933 ret = adsp_err_get_lnx_err_code(
1934 atomic_read(&this_adm.adm_stat));
1935 goto fail_cmd;
1936 }
1937fail_cmd:
1938 kfree(mmap_region_cmd);
1939 return ret;
1940}
1941
1942static int adm_memory_unmap_regions(void)
1943{
1944 struct avs_cmd_shared_mem_unmap_regions unmap_regions;
1945 int ret = 0;
1946
1947 pr_debug("%s:\n", __func__);
1948 if (this_adm.apr == NULL) {
1949 pr_err("%s: APR handle NULL\n", __func__);
1950 return -EINVAL;
1951 }
1952
1953 unmap_regions.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1954 APR_HDR_LEN(APR_HDR_SIZE),
1955 APR_PKT_VER);
1956 unmap_regions.hdr.pkt_size = sizeof(unmap_regions);
1957 unmap_regions.hdr.src_port = 0;
1958 unmap_regions.hdr.dest_port = 0;
1959 unmap_regions.hdr.token = 0;
1960 unmap_regions.hdr.opcode = ADM_CMD_SHARED_MEM_UNMAP_REGIONS;
1961 unmap_regions.mem_map_handle = atomic_read(&this_adm.
1962 mem_map_handles[atomic_read(&this_adm.mem_map_index)]);
1963 atomic_set(&this_adm.adm_stat, -1);
1964 ret = apr_send_pkt(this_adm.apr, (uint32_t *) &unmap_regions);
1965 if (ret < 0) {
1966 pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__,
1967 unmap_regions.hdr.opcode, ret);
1968 ret = -EINVAL;
1969 goto fail_cmd;
1970 }
1971
1972 ret = wait_event_timeout(this_adm.adm_wait,
1973 atomic_read(&this_adm.adm_stat) >= 0,
1974 5 * HZ);
1975 if (!ret) {
1976 pr_err("%s: timeout. waited for memory_unmap\n",
1977 __func__);
1978 ret = -EINVAL;
1979 goto fail_cmd;
1980 } else if (atomic_read(&this_adm.adm_stat) > 0) {
1981 pr_err("%s: DSP returned error[%s]\n",
1982 __func__, adsp_err_get_err_str(
1983 atomic_read(&this_adm.adm_stat)));
1984 ret = adsp_err_get_lnx_err_code(
1985 atomic_read(&this_adm.adm_stat));
1986 goto fail_cmd;
1987 } else {
1988 pr_debug("%s: Unmap handle 0x%x succeeded\n", __func__,
1989 unmap_regions.mem_map_handle);
1990 }
1991fail_cmd:
1992 return ret;
1993}
1994
1995static int remap_cal_data(struct cal_block_data *cal_block, int cal_index)
1996{
1997 int ret = 0;
1998
1999 if (cal_block->map_data.ion_client == NULL) {
2000 pr_err("%s: No ION allocation for cal index %d!\n",
2001 __func__, cal_index);
2002 ret = -EINVAL;
2003 goto done;
2004 }
2005
2006 if ((cal_block->map_data.map_size > 0) &&
2007 (cal_block->map_data.q6map_handle == 0)) {
2008 atomic_set(&this_adm.mem_map_index, cal_index);
2009 ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
2010 (uint32_t *)&cal_block->map_data.map_size, 1);
2011 if (ret < 0) {
2012 pr_err("%s: ADM mmap did not work! size = %zd ret %d\n",
2013 __func__,
2014 cal_block->map_data.map_size, ret);
2015 pr_debug("%s: ADM mmap did not work! addr = 0x%pK, size = %zd ret %d\n",
2016 __func__,
2017 &cal_block->cal_data.paddr,
2018 cal_block->map_data.map_size, ret);
2019 goto done;
2020 }
2021 cal_block->map_data.q6map_handle = atomic_read(&this_adm.
2022 mem_map_handles[cal_index]);
2023 }
2024done:
2025 return ret;
2026}
2027
2028static void send_adm_custom_topology(void)
2029{
2030 struct cal_block_data *cal_block = NULL;
2031 struct cmd_set_topologies adm_top;
2032 int cal_index = ADM_CUSTOM_TOP_CAL;
2033 int result;
2034
2035 if (this_adm.cal_data[cal_index] == NULL)
2036 goto done;
2037
2038 mutex_lock(&this_adm.cal_data[cal_index]->lock);
2039 if (!this_adm.set_custom_topology)
2040 goto unlock;
2041 this_adm.set_custom_topology = 0;
2042
2043 cal_block = cal_utils_get_only_cal_block(this_adm.cal_data[cal_index]);
Vikram Pandurangad3b58cc2017-09-27 12:17:36 -07002044 if (cal_block == NULL || cal_utils_is_cal_stale(cal_block))
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302045 goto unlock;
2046
2047 pr_debug("%s: Sending cal_index %d\n", __func__, cal_index);
2048
2049 result = remap_cal_data(cal_block, cal_index);
2050 if (result) {
2051 pr_err("%s: Remap_cal_data failed for cal %d!\n",
2052 __func__, cal_index);
2053 goto unlock;
2054 }
2055 atomic_set(&this_adm.mem_map_index, cal_index);
2056 atomic_set(&this_adm.mem_map_handles[cal_index],
2057 cal_block->map_data.q6map_handle);
2058
2059 if (cal_block->cal_data.size == 0) {
2060 pr_debug("%s: No ADM cal to send\n", __func__);
2061 goto unlock;
2062 }
2063
2064 adm_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2065 APR_HDR_LEN(20), APR_PKT_VER);
2066 adm_top.hdr.pkt_size = sizeof(adm_top);
2067 adm_top.hdr.src_svc = APR_SVC_ADM;
2068 adm_top.hdr.src_domain = APR_DOMAIN_APPS;
2069 adm_top.hdr.src_port = 0;
2070 adm_top.hdr.dest_svc = APR_SVC_ADM;
2071 adm_top.hdr.dest_domain = APR_DOMAIN_ADSP;
2072 adm_top.hdr.dest_port = 0;
2073 adm_top.hdr.token = 0;
2074 adm_top.hdr.opcode = ADM_CMD_ADD_TOPOLOGIES;
2075 adm_top.payload_addr_lsw = lower_32_bits(cal_block->cal_data.paddr);
2076 adm_top.payload_addr_msw = msm_audio_populate_upper_32_bits(
2077 cal_block->cal_data.paddr);
2078 adm_top.mem_map_handle = cal_block->map_data.q6map_handle;
2079 adm_top.payload_size = cal_block->cal_data.size;
2080
2081 atomic_set(&this_adm.adm_stat, -1);
2082 pr_debug("%s: Sending ADM_CMD_ADD_TOPOLOGIES payload = 0x%pK, size = %d\n",
2083 __func__, &cal_block->cal_data.paddr,
2084 adm_top.payload_size);
2085 result = apr_send_pkt(this_adm.apr, (uint32_t *)&adm_top);
2086 if (result < 0) {
2087 pr_err("%s: Set topologies failed payload size = %zd result %d\n",
2088 __func__, cal_block->cal_data.size, result);
2089 goto unlock;
2090 }
2091 /* Wait for the callback */
2092 result = wait_event_timeout(this_adm.adm_wait,
2093 atomic_read(&this_adm.adm_stat) >= 0,
2094 msecs_to_jiffies(TIMEOUT_MS));
2095 if (!result) {
2096 pr_err("%s: Set topologies timed out payload size = %zd\n",
2097 __func__, cal_block->cal_data.size);
2098 goto unlock;
2099 } else if (atomic_read(&this_adm.adm_stat) > 0) {
2100 pr_err("%s: DSP returned error[%s]\n",
2101 __func__, adsp_err_get_err_str(
2102 atomic_read(&this_adm.adm_stat)));
2103 result = adsp_err_get_lnx_err_code(
2104 atomic_read(&this_adm.adm_stat));
2105 goto unlock;
2106 }
2107unlock:
2108 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
2109done:
2110 return;
2111}
2112
2113static int send_adm_cal_block(int port_id, int copp_idx,
2114 struct cal_block_data *cal_block, int perf_mode,
2115 int app_type, int acdb_id, int sample_rate)
2116{
2117 s32 result = 0;
2118 struct adm_cmd_set_pp_params_v5 adm_params;
2119 int port_idx;
2120
2121 pr_debug("%s: Port id 0x%x sample_rate %d ,\n", __func__,
2122 port_id, sample_rate);
2123 port_id = afe_convert_virtual_to_portid(port_id);
2124 port_idx = adm_validate_and_get_port_index(port_id);
2125 if (port_idx < 0) {
2126 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2127 return -EINVAL;
2128 }
2129 if (!cal_block) {
2130 pr_debug("%s: No ADM cal to send for port_id = 0x%x!\n",
2131 __func__, port_id);
2132 result = -EINVAL;
2133 goto done;
2134 }
2135 if (cal_block->cal_data.size <= 0) {
2136 pr_debug("%s: No ADM cal send for port_id = 0x%x!\n",
2137 __func__, port_id);
2138 result = -EINVAL;
2139 goto done;
2140 }
2141
2142 if (perf_mode == LEGACY_PCM_MODE &&
2143 ((atomic_read(&this_adm.copp.topology[port_idx][copp_idx])) ==
2144 DS2_ADM_COPP_TOPOLOGY_ID)) {
2145 pr_err("%s: perf_mode %d, topology 0x%x\n", __func__, perf_mode,
2146 atomic_read(
2147 &this_adm.copp.topology[port_idx][copp_idx]));
2148 goto done;
2149 }
2150
2151 adm_params.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2152 APR_HDR_LEN(20), APR_PKT_VER);
2153 adm_params.hdr.pkt_size = sizeof(adm_params);
2154 adm_params.hdr.src_svc = APR_SVC_ADM;
2155 adm_params.hdr.src_domain = APR_DOMAIN_APPS;
2156 adm_params.hdr.src_port = port_id;
2157 adm_params.hdr.dest_svc = APR_SVC_ADM;
2158 adm_params.hdr.dest_domain = APR_DOMAIN_ADSP;
2159
2160 adm_params.hdr.token = port_idx << 16 | copp_idx;
2161 adm_params.hdr.dest_port =
2162 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
2163 adm_params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
2164 adm_params.payload_addr_lsw = lower_32_bits(cal_block->cal_data.paddr);
2165 adm_params.payload_addr_msw = msm_audio_populate_upper_32_bits(
2166 cal_block->cal_data.paddr);
2167 adm_params.mem_map_handle = cal_block->map_data.q6map_handle;
2168 adm_params.payload_size = cal_block->cal_data.size;
2169
2170 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2171 pr_debug("%s: Sending SET_PARAMS payload = 0x%pK, size = %d\n",
2172 __func__, &cal_block->cal_data.paddr,
2173 adm_params.payload_size);
2174 result = apr_send_pkt(this_adm.apr, (uint32_t *)&adm_params);
2175 if (result < 0) {
2176 pr_err("%s: Set params failed port 0x%x result %d\n",
2177 __func__, port_id, result);
2178 pr_debug("%s: Set params failed port = 0x%x payload = 0x%pK result %d\n",
2179 __func__, port_id, &cal_block->cal_data.paddr, result);
2180 result = -EINVAL;
2181 goto done;
2182 }
2183 /* Wait for the callback */
2184 result = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
2185 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
2186 msecs_to_jiffies(TIMEOUT_MS));
2187 if (!result) {
2188 pr_err("%s: Set params timed out port = 0x%x\n",
2189 __func__, port_id);
2190 pr_debug("%s: Set params timed out port = 0x%x, payload = 0x%pK\n",
2191 __func__, port_id, &cal_block->cal_data.paddr);
2192 result = -EINVAL;
2193 goto done;
2194 } else if (atomic_read(&this_adm.copp.stat
2195 [port_idx][copp_idx]) > 0) {
2196 pr_err("%s: DSP returned error[%s]\n",
2197 __func__, adsp_err_get_err_str(
2198 atomic_read(&this_adm.copp.stat
2199 [port_idx][copp_idx])));
2200 result = adsp_err_get_lnx_err_code(
2201 atomic_read(&this_adm.copp.stat
2202 [port_idx][copp_idx]));
2203 goto done;
2204 }
2205
2206done:
2207 return result;
2208}
2209
2210static struct cal_block_data *adm_find_cal_by_path(int cal_index, int path)
2211{
2212 struct list_head *ptr, *next;
2213 struct cal_block_data *cal_block = NULL;
2214 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2215 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2216
2217 pr_debug("%s:\n", __func__);
2218
2219 list_for_each_safe(ptr, next,
2220 &this_adm.cal_data[cal_index]->cal_blocks) {
2221
2222 cal_block = list_entry(ptr,
2223 struct cal_block_data, list);
2224
Vikram Pandurangad3b58cc2017-09-27 12:17:36 -07002225 if (cal_utils_is_cal_stale(cal_block))
2226 continue;
2227
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302228 if (cal_index == ADM_AUDPROC_CAL ||
Bhalchandra Gajareface2762018-05-10 14:16:49 -07002229 cal_index == ADM_LSM_AUDPROC_CAL ||
2230 cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302231 audproc_cal_info = cal_block->cal_info;
2232 if ((audproc_cal_info->path == path) &&
2233 (cal_block->cal_data.size > 0))
2234 return cal_block;
2235 } else if (cal_index == ADM_AUDVOL_CAL) {
2236 audvol_cal_info = cal_block->cal_info;
2237 if ((audvol_cal_info->path == path) &&
2238 (cal_block->cal_data.size > 0))
2239 return cal_block;
2240 }
2241 }
2242 pr_debug("%s: Can't find ADM cal for cal_index %d, path %d\n",
2243 __func__, cal_index, path);
2244 return NULL;
2245}
2246
2247static struct cal_block_data *adm_find_cal_by_app_type(int cal_index, int path,
2248 int app_type)
2249{
2250 struct list_head *ptr, *next;
2251 struct cal_block_data *cal_block = NULL;
2252 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2253 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2254
2255 pr_debug("%s\n", __func__);
2256
2257 list_for_each_safe(ptr, next,
2258 &this_adm.cal_data[cal_index]->cal_blocks) {
2259
2260 cal_block = list_entry(ptr,
2261 struct cal_block_data, list);
2262
Vikram Pandurangad3b58cc2017-09-27 12:17:36 -07002263 if (cal_utils_is_cal_stale(cal_block))
2264 continue;
2265
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302266 if (cal_index == ADM_AUDPROC_CAL ||
Bhalchandra Gajareface2762018-05-10 14:16:49 -07002267 cal_index == ADM_LSM_AUDPROC_CAL ||
2268 cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302269 audproc_cal_info = cal_block->cal_info;
2270 if ((audproc_cal_info->path == path) &&
2271 (audproc_cal_info->app_type == app_type) &&
2272 (cal_block->cal_data.size > 0))
2273 return cal_block;
2274 } else if (cal_index == ADM_AUDVOL_CAL) {
2275 audvol_cal_info = cal_block->cal_info;
2276 if ((audvol_cal_info->path == path) &&
2277 (audvol_cal_info->app_type == app_type) &&
2278 (cal_block->cal_data.size > 0))
2279 return cal_block;
2280 }
2281 }
2282 pr_debug("%s: Can't find ADM cali for cal_index %d, path %d, app %d, defaulting to search by path\n",
2283 __func__, cal_index, path, app_type);
2284 return adm_find_cal_by_path(cal_index, path);
2285}
2286
2287
2288static struct cal_block_data *adm_find_cal(int cal_index, int path,
2289 int app_type, int acdb_id,
2290 int sample_rate)
2291{
2292 struct list_head *ptr, *next;
2293 struct cal_block_data *cal_block = NULL;
2294 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2295 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2296
2297 pr_debug("%s:\n", __func__);
2298
2299 list_for_each_safe(ptr, next,
2300 &this_adm.cal_data[cal_index]->cal_blocks) {
2301
2302 cal_block = list_entry(ptr,
2303 struct cal_block_data, list);
Vikram Pandurangad3b58cc2017-09-27 12:17:36 -07002304 if (cal_utils_is_cal_stale(cal_block))
2305 continue;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302306
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302307 if (cal_index == ADM_AUDPROC_CAL ||
Bhalchandra Gajareface2762018-05-10 14:16:49 -07002308 cal_index == ADM_LSM_AUDPROC_CAL ||
2309 cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302310 audproc_cal_info = cal_block->cal_info;
2311 if ((audproc_cal_info->path == path) &&
2312 (audproc_cal_info->app_type == app_type) &&
2313 (audproc_cal_info->acdb_id == acdb_id) &&
2314 (audproc_cal_info->sample_rate == sample_rate) &&
2315 (cal_block->cal_data.size > 0))
2316 return cal_block;
2317 } else if (cal_index == ADM_AUDVOL_CAL) {
2318 audvol_cal_info = cal_block->cal_info;
2319 if ((audvol_cal_info->path == path) &&
2320 (audvol_cal_info->app_type == app_type) &&
2321 (audvol_cal_info->acdb_id == acdb_id) &&
2322 (cal_block->cal_data.size > 0))
2323 return cal_block;
2324 }
2325 }
2326 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",
2327 __func__, cal_index, path, app_type, acdb_id, sample_rate);
2328 return adm_find_cal_by_app_type(cal_index, path, app_type);
2329}
2330
2331static int adm_remap_and_send_cal_block(int cal_index, int port_id,
2332 int copp_idx, struct cal_block_data *cal_block, int perf_mode,
2333 int app_type, int acdb_id, int sample_rate)
2334{
2335 int ret = 0;
2336
2337 pr_debug("%s: Sending cal_index cal %d\n", __func__, cal_index);
2338 ret = remap_cal_data(cal_block, cal_index);
2339 if (ret) {
2340 pr_err("%s: Remap_cal_data failed for cal %d!\n",
2341 __func__, cal_index);
2342 goto done;
2343 }
2344 ret = send_adm_cal_block(port_id, copp_idx, cal_block, perf_mode,
2345 app_type, acdb_id, sample_rate);
2346 if (ret < 0)
2347 pr_debug("%s: No cal sent for cal_index %d, port_id = 0x%x! ret %d sample_rate %d\n",
2348 __func__, cal_index, port_id, ret, sample_rate);
2349done:
2350 return ret;
2351}
2352
2353static void send_adm_cal_type(int cal_index, int path, int port_id,
2354 int copp_idx, int perf_mode, int app_type,
2355 int acdb_id, int sample_rate)
2356{
2357 struct cal_block_data *cal_block = NULL;
2358 int ret;
2359
2360 pr_debug("%s: cal index %d\n", __func__, cal_index);
2361
2362 if (this_adm.cal_data[cal_index] == NULL) {
2363 pr_debug("%s: cal_index %d not allocated!\n",
2364 __func__, cal_index);
2365 goto done;
2366 }
2367
2368 mutex_lock(&this_adm.cal_data[cal_index]->lock);
2369 cal_block = adm_find_cal(cal_index, path, app_type, acdb_id,
2370 sample_rate);
2371 if (cal_block == NULL)
2372 goto unlock;
2373
2374 ret = adm_remap_and_send_cal_block(cal_index, port_id, copp_idx,
2375 cal_block, perf_mode, app_type, acdb_id, sample_rate);
Vikram Pandurangad3b58cc2017-09-27 12:17:36 -07002376
2377 cal_utils_mark_cal_used(cal_block);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302378unlock:
2379 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
2380done:
2381 return;
2382}
2383
2384static int get_cal_path(int path)
2385{
2386 if (path == 0x1)
2387 return RX_DEVICE;
2388 else
2389 return TX_DEVICE;
2390}
2391
2392static void send_adm_cal(int port_id, int copp_idx, int path, int perf_mode,
Aditya Bavanarifcd80ec2018-01-08 13:16:32 +05302393 int app_type, int acdb_id, int sample_rate,
2394 int passthr_mode)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302395{
2396 pr_debug("%s: port id 0x%x copp_idx %d\n", __func__, port_id, copp_idx);
2397
Bhalchandra Gajareface2762018-05-10 14:16:49 -07002398 if (passthr_mode != LISTEN) {
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302399 send_adm_cal_type(ADM_AUDPROC_CAL, path, port_id, copp_idx,
2400 perf_mode, app_type, acdb_id, sample_rate);
Bhalchandra Gajareface2762018-05-10 14:16:49 -07002401 } else {
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302402 send_adm_cal_type(ADM_LSM_AUDPROC_CAL, path, port_id, copp_idx,
2403 perf_mode, app_type, acdb_id, sample_rate);
Bhalchandra Gajareface2762018-05-10 14:16:49 -07002404
2405 send_adm_cal_type(ADM_LSM_AUDPROC_PERSISTENT_CAL, path,
2406 port_id, copp_idx, perf_mode, app_type,
2407 acdb_id, sample_rate);
2408 }
2409
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302410 send_adm_cal_type(ADM_AUDVOL_CAL, path, port_id, copp_idx, perf_mode,
2411 app_type, acdb_id, sample_rate);
2412}
2413
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302414/**
2415 * adm_connect_afe_port -
2416 * command to send ADM connect AFE port
2417 *
2418 * @mode: value of mode for ADM connect AFE
2419 * @session_id: session active to connect
2420 * @port_id: Port ID number
2421 *
2422 * Returns 0 on success or error on failure
2423 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302424int adm_connect_afe_port(int mode, int session_id, int port_id)
2425{
2426 struct adm_cmd_connect_afe_port_v5 cmd;
2427 int ret = 0;
2428 int port_idx, copp_idx = 0;
2429
2430 pr_debug("%s: port_id: 0x%x session id:%d mode:%d\n", __func__,
2431 port_id, session_id, mode);
2432
2433 port_id = afe_convert_virtual_to_portid(port_id);
2434 port_idx = adm_validate_and_get_port_index(port_id);
2435 if (port_idx < 0) {
2436 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2437 return -EINVAL;
2438 }
2439
2440 if (this_adm.apr == NULL) {
2441 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
2442 0xFFFFFFFF, &this_adm);
2443 if (this_adm.apr == NULL) {
2444 pr_err("%s: Unable to register ADM\n", __func__);
2445 ret = -ENODEV;
2446 return ret;
2447 }
2448 rtac_set_adm_handle(this_adm.apr);
2449 }
2450 pr_debug("%s: Port ID 0x%x, index %d\n", __func__, port_id, port_idx);
2451
2452 cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2453 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
2454 cmd.hdr.pkt_size = sizeof(cmd);
2455 cmd.hdr.src_svc = APR_SVC_ADM;
2456 cmd.hdr.src_domain = APR_DOMAIN_APPS;
2457 cmd.hdr.src_port = port_id;
2458 cmd.hdr.dest_svc = APR_SVC_ADM;
2459 cmd.hdr.dest_domain = APR_DOMAIN_ADSP;
2460 cmd.hdr.dest_port = 0; /* Ignored */
2461 cmd.hdr.token = port_idx << 16 | copp_idx;
2462 cmd.hdr.opcode = ADM_CMD_CONNECT_AFE_PORT_V5;
2463
2464 cmd.mode = mode;
2465 cmd.session_id = session_id;
2466 cmd.afe_port_id = port_id;
2467
2468 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2469 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&cmd);
2470 if (ret < 0) {
2471 pr_err("%s: ADM enable for port_id: 0x%x failed ret %d\n",
2472 __func__, port_id, ret);
2473 ret = -EINVAL;
2474 goto fail_cmd;
2475 }
2476 /* Wait for the callback with copp id */
2477 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
2478 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
2479 msecs_to_jiffies(TIMEOUT_MS));
2480 if (!ret) {
2481 pr_err("%s: ADM connect timedout for port_id: 0x%x\n",
2482 __func__, port_id);
2483 ret = -EINVAL;
2484 goto fail_cmd;
2485 } else if (atomic_read(&this_adm.copp.stat
2486 [port_idx][copp_idx]) > 0) {
2487 pr_err("%s: DSP returned error[%s]\n",
2488 __func__, adsp_err_get_err_str(
2489 atomic_read(&this_adm.copp.stat
2490 [port_idx][copp_idx])));
2491 ret = adsp_err_get_lnx_err_code(
2492 atomic_read(&this_adm.copp.stat
2493 [port_idx][copp_idx]));
2494 goto fail_cmd;
2495 }
2496 atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
2497 return 0;
2498
2499fail_cmd:
2500
2501 return ret;
2502}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302503EXPORT_SYMBOL(adm_connect_afe_port);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302504
2505int adm_arrange_mch_map(struct adm_cmd_device_open_v5 *open, int path,
Rohit kumar5d860f42019-02-01 18:01:12 +05302506 int channel_mode, int port_idx)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302507{
2508 int rc = 0, idx;
2509
2510 memset(open->dev_channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
2511 switch (path) {
2512 case ADM_PATH_PLAYBACK:
2513 idx = ADM_MCH_MAP_IDX_PLAYBACK;
2514 break;
2515 case ADM_PATH_LIVE_REC:
2516 case ADM_PATH_NONLIVE_REC:
2517 idx = ADM_MCH_MAP_IDX_REC;
2518 break;
2519 default:
2520 goto non_mch_path;
2521 };
Rohit kumar5d860f42019-02-01 18:01:12 +05302522
2523 if ((open->dev_num_channel > 2) &&
2524 (port_channel_map[port_idx].set_channel_map ||
2525 multi_ch_maps[idx].set_channel_map)) {
2526 if (port_channel_map[port_idx].set_channel_map)
2527 memcpy(open->dev_channel_mapping,
2528 port_channel_map[port_idx].channel_mapping,
2529 PCM_FORMAT_MAX_NUM_CHANNEL);
2530 else
2531 memcpy(open->dev_channel_mapping,
2532 multi_ch_maps[idx].channel_mapping,
2533 PCM_FORMAT_MAX_NUM_CHANNEL);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302534 } else {
2535 if (channel_mode == 1) {
2536 open->dev_channel_mapping[0] = PCM_CHANNEL_FC;
2537 } else if (channel_mode == 2) {
2538 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2539 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2540 } else if (channel_mode == 3) {
2541 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2542 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2543 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2544 } else if (channel_mode == 4) {
2545 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2546 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2547 open->dev_channel_mapping[2] = PCM_CHANNEL_LS;
2548 open->dev_channel_mapping[3] = PCM_CHANNEL_RS;
2549 } else if (channel_mode == 5) {
2550 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2551 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2552 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2553 open->dev_channel_mapping[3] = PCM_CHANNEL_LS;
2554 open->dev_channel_mapping[4] = PCM_CHANNEL_RS;
2555 } else if (channel_mode == 6) {
2556 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2557 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2558 open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2559 open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2560 open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2561 open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2562 } else if (channel_mode == 7) {
2563 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2564 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2565 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2566 open->dev_channel_mapping[3] = PCM_CHANNEL_LFE;
2567 open->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2568 open->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2569 open->dev_channel_mapping[6] = PCM_CHANNEL_CS;
2570 } else if (channel_mode == 8) {
2571 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2572 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2573 open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2574 open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2575 open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2576 open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2577 open->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2578 open->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2579 } else {
2580 pr_err("%s: invalid num_chan %d\n", __func__,
2581 channel_mode);
2582 rc = -EINVAL;
2583 goto inval_ch_mod;
2584 }
2585 }
2586
2587non_mch_path:
2588inval_ch_mod:
2589 return rc;
2590}
2591
2592int adm_arrange_mch_ep2_map(struct adm_cmd_device_open_v6 *open_v6,
2593 int channel_mode)
2594{
2595 int rc = 0;
2596
2597 memset(open_v6->dev_channel_mapping_eid2, 0,
2598 PCM_FORMAT_MAX_NUM_CHANNEL);
2599
2600 if (channel_mode == 1) {
2601 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FC;
2602 } else if (channel_mode == 2) {
2603 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2604 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2605 } else if (channel_mode == 3) {
2606 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2607 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2608 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
2609 } else if (channel_mode == 4) {
2610 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2611 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2612 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LS;
2613 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_RS;
2614 } else if (channel_mode == 5) {
2615 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2616 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2617 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
2618 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_LS;
2619 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_RS;
2620 } else if (channel_mode == 6) {
2621 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2622 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2623 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
2624 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
2625 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
2626 open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
2627 } else if (channel_mode == 8) {
2628 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2629 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2630 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
2631 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
2632 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
2633 open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
2634 open_v6->dev_channel_mapping_eid2[6] = PCM_CHANNEL_LB;
2635 open_v6->dev_channel_mapping_eid2[7] = PCM_CHANNEL_RB;
2636 } else {
2637 pr_err("%s: invalid num_chan %d\n", __func__,
2638 channel_mode);
2639 rc = -EINVAL;
2640 }
2641
2642 return rc;
2643}
2644
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302645/**
2646 * adm_open -
2647 * command to send ADM open
2648 *
2649 * @port_id: port id number
2650 * @path: direction or ADM path type
2651 * @rate: sample rate of session
2652 * @channel_mode: number of channels set
2653 * @topology: topology active for this session
2654 * @perf_mode: performance mode like LL/ULL/..
2655 * @bit_width: bit width to set for copp
2656 * @app_type: App type used for this session
2657 * @acdb_id: ACDB ID of this device
2658 *
2659 * Returns 0 on success or error on failure
2660 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302661int adm_open(int port_id, int path, int rate, int channel_mode, int topology,
Sachin Mohan Gadag3c3c5812018-07-20 15:46:54 +05302662 int perf_mode, uint16_t bit_width, int app_type, int acdb_id,
2663 int session_type)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302664{
2665 struct adm_cmd_device_open_v5 open;
2666 struct adm_cmd_device_open_v6 open_v6;
2667 int ret = 0;
Asish Bhattacharya34504582017-08-08 12:55:01 +05302668 int port_idx, flags;
2669 int copp_idx = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302670 int tmp_port = q6audio_get_port_id(port_id);
2671
2672 pr_debug("%s:port %#x path:%d rate:%d mode:%d perf_mode:%d,topo_id %d\n",
2673 __func__, port_id, path, rate, channel_mode, perf_mode,
2674 topology);
2675
2676 port_id = q6audio_convert_virtual_to_portid(port_id);
2677 port_idx = adm_validate_and_get_port_index(port_id);
2678 if (port_idx < 0) {
2679 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2680 return -EINVAL;
2681 }
2682
2683 if (this_adm.apr == NULL) {
2684 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
2685 0xFFFFFFFF, &this_adm);
2686 if (this_adm.apr == NULL) {
2687 pr_err("%s: Unable to register ADM\n", __func__);
2688 return -ENODEV;
2689 }
2690 rtac_set_adm_handle(this_adm.apr);
2691 }
2692
2693 if (perf_mode == ULL_POST_PROCESSING_PCM_MODE) {
2694 flags = ADM_ULL_POST_PROCESSING_DEVICE_SESSION;
2695 if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
2696 (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
2697 (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
2698 topology = DEFAULT_COPP_TOPOLOGY;
2699 } else if (perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) {
2700 flags = ADM_ULTRA_LOW_LATENCY_DEVICE_SESSION;
2701 topology = NULL_COPP_TOPOLOGY;
2702 rate = ULL_SUPPORTED_SAMPLE_RATE;
2703 bit_width = ULL_SUPPORTED_BITS_PER_SAMPLE;
2704 } else if (perf_mode == LOW_LATENCY_PCM_MODE) {
2705 flags = ADM_LOW_LATENCY_DEVICE_SESSION;
2706 if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
2707 (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
2708 (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
2709 topology = DEFAULT_COPP_TOPOLOGY;
2710 } else {
2711 if ((path == ADM_PATH_COMPRESSED_RX) ||
2712 (path == ADM_PATH_COMPRESSED_TX))
2713 flags = 0;
2714 else
2715 flags = ADM_LEGACY_DEVICE_SESSION;
2716 }
2717
Laxminath Kasam8f7ccc22017-08-28 17:35:04 +05302718 if ((topology == VPM_TX_SM_ECNS_V2_COPP_TOPOLOGY) ||
Bala Kishore Pati798cbf82018-10-22 11:58:41 +05302719 (topology == VPM_TX_SM_ECNS_COPP_TOPOLOGY) ||
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302720 (topology == VPM_TX_DM_FLUENCE_COPP_TOPOLOGY) ||
2721 (topology == VPM_TX_DM_RFECNS_COPP_TOPOLOGY))
2722 rate = 16000;
2723
Asish Bhattacharya34504582017-08-08 12:55:01 +05302724 /*
2725 * Routing driver reuses the same adm for streams with the same
2726 * app_type, sample_rate etc.
2727 * This isn't allowed for ULL streams as per the DSP interface
2728 */
2729 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE)
2730 copp_idx = adm_get_idx_if_copp_exists(port_idx, topology,
2731 perf_mode,
2732 rate, bit_width,
Sachin Mohan Gadag3c3c5812018-07-20 15:46:54 +05302733 app_type, session_type);
Asish Bhattacharya34504582017-08-08 12:55:01 +05302734
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302735 if (copp_idx < 0) {
2736 copp_idx = adm_get_next_available_copp(port_idx);
2737 if (copp_idx >= MAX_COPPS_PER_PORT) {
2738 pr_err("%s: exceeded copp id %d\n",
2739 __func__, copp_idx);
2740 return -EINVAL;
2741 }
2742 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
2743 atomic_set(&this_adm.copp.topology[port_idx][copp_idx],
2744 topology);
2745 atomic_set(&this_adm.copp.mode[port_idx][copp_idx],
2746 perf_mode);
2747 atomic_set(&this_adm.copp.rate[port_idx][copp_idx],
2748 rate);
2749 atomic_set(&this_adm.copp.channels[port_idx][copp_idx],
2750 channel_mode);
2751 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx],
2752 bit_width);
2753 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx],
2754 app_type);
2755 atomic_set(&this_adm.copp.acdb_id[port_idx][copp_idx],
2756 acdb_id);
Sachin Mohan Gadag3c3c5812018-07-20 15:46:54 +05302757 atomic_set(&this_adm.copp.session_type[port_idx][copp_idx],
2758 session_type);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302759 set_bit(ADM_STATUS_CALIBRATION_REQUIRED,
2760 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
2761 if ((path != ADM_PATH_COMPRESSED_RX) &&
2762 (path != ADM_PATH_COMPRESSED_TX))
2763 send_adm_custom_topology();
2764 }
2765
2766 if (this_adm.copp.adm_delay[port_idx][copp_idx] &&
2767 perf_mode == LEGACY_PCM_MODE) {
2768 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
2769 1);
2770 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
2771 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
2772 }
2773
2774 /* Create a COPP if port id are not enabled */
2775 if (atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]) == 0) {
2776 pr_debug("%s: open ADM: port_idx: %d, copp_idx: %d\n", __func__,
2777 port_idx, copp_idx);
2778 if ((topology == SRS_TRUMEDIA_TOPOLOGY_ID) &&
2779 perf_mode == LEGACY_PCM_MODE) {
2780 int res;
2781
2782 atomic_set(&this_adm.mem_map_index, ADM_SRS_TRUMEDIA);
2783 msm_dts_srs_tm_ion_memmap(&this_adm.outband_memmap);
2784 res = adm_memory_map_regions(&this_adm.outband_memmap.paddr, 0,
2785 (uint32_t *)&this_adm.outband_memmap.size, 1);
2786 if (res < 0) {
2787 pr_err("%s: SRS adm_memory_map_regions failed ! addr = 0x%pK, size = %d\n",
2788 __func__, (void *)this_adm.outband_memmap.paddr,
2789 (uint32_t)this_adm.outband_memmap.size);
2790 }
2791 }
2792 open.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2793 APR_HDR_LEN(APR_HDR_SIZE),
2794 APR_PKT_VER);
2795 open.hdr.pkt_size = sizeof(open);
2796 open.hdr.src_svc = APR_SVC_ADM;
2797 open.hdr.src_domain = APR_DOMAIN_APPS;
2798 open.hdr.src_port = tmp_port;
2799 open.hdr.dest_svc = APR_SVC_ADM;
2800 open.hdr.dest_domain = APR_DOMAIN_ADSP;
2801 open.hdr.dest_port = tmp_port;
2802 open.hdr.token = port_idx << 16 | copp_idx;
2803 open.hdr.opcode = ADM_CMD_DEVICE_OPEN_V5;
2804 open.flags = flags;
2805 open.mode_of_operation = path;
2806 open.endpoint_id_1 = tmp_port;
2807 open.endpoint_id_2 = 0xFFFF;
2808
Sachin Mohan Gadag3c3c5812018-07-20 15:46:54 +05302809 if (this_adm.ec_ref_rx && (path != 1) &&
2810 (afe_get_port_type(tmp_port) == MSM_AFE_PORT_TYPE_TX)) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302811 open.endpoint_id_2 = this_adm.ec_ref_rx;
2812 this_adm.ec_ref_rx = -1;
2813 }
2814
2815 open.topology_id = topology;
2816
2817 open.dev_num_channel = channel_mode & 0x00FF;
2818 open.bit_width = bit_width;
2819 WARN_ON((perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) &&
2820 (rate != ULL_SUPPORTED_SAMPLE_RATE));
2821 open.sample_rate = rate;
2822
Rohit kumar5d860f42019-02-01 18:01:12 +05302823 ret = adm_arrange_mch_map(&open, path, channel_mode,
2824 port_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302825
2826 if (ret)
2827 return ret;
2828
2829 pr_debug("%s: port_id=0x%x rate=%d topology_id=0x%X\n",
2830 __func__, open.endpoint_id_1, open.sample_rate,
2831 open.topology_id);
2832
2833 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2834
2835 if ((this_adm.num_ec_ref_rx_chans != 0) && (path != 1) &&
2836 (open.endpoint_id_2 != 0xFFFF)) {
2837 memset(&open_v6, 0,
2838 sizeof(struct adm_cmd_device_open_v6));
2839 memcpy(&open_v6, &open,
2840 sizeof(struct adm_cmd_device_open_v5));
2841 open_v6.hdr.opcode = ADM_CMD_DEVICE_OPEN_V6;
2842 open_v6.hdr.pkt_size = sizeof(open_v6);
2843 open_v6.dev_num_channel_eid2 =
2844 this_adm.num_ec_ref_rx_chans;
2845 this_adm.num_ec_ref_rx_chans = 0;
2846
2847 if (this_adm.ec_ref_rx_bit_width != 0) {
2848 open_v6.bit_width_eid2 =
2849 this_adm.ec_ref_rx_bit_width;
2850 this_adm.ec_ref_rx_bit_width = 0;
2851 } else {
2852 open_v6.bit_width_eid2 = bit_width;
2853 }
2854
2855 if (this_adm.ec_ref_rx_sampling_rate != 0) {
2856 open_v6.sample_rate_eid2 =
2857 this_adm.ec_ref_rx_sampling_rate;
2858 this_adm.ec_ref_rx_sampling_rate = 0;
2859 } else {
2860 open_v6.sample_rate_eid2 = rate;
2861 }
2862
2863 pr_debug("%s: eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
2864 __func__, open_v6.dev_num_channel_eid2,
2865 open_v6.bit_width_eid2,
2866 open_v6.sample_rate_eid2);
2867
2868 ret = adm_arrange_mch_ep2_map(&open_v6,
2869 open_v6.dev_num_channel_eid2);
2870
2871 if (ret)
2872 return ret;
2873
2874 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&open_v6);
2875 } else {
2876 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&open);
2877 }
2878 if (ret < 0) {
2879 pr_err("%s: port_id: 0x%x for[0x%x] failed %d\n",
2880 __func__, tmp_port, port_id, ret);
2881 return -EINVAL;
2882 }
2883 /* Wait for the callback with copp id */
2884 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
2885 atomic_read(&this_adm.copp.stat
2886 [port_idx][copp_idx]) >= 0,
2887 msecs_to_jiffies(TIMEOUT_MS));
2888 if (!ret) {
2889 pr_err("%s: ADM open timedout for port_id: 0x%x for [0x%x]\n",
2890 __func__, tmp_port, port_id);
2891 return -EINVAL;
2892 } else if (atomic_read(&this_adm.copp.stat
2893 [port_idx][copp_idx]) > 0) {
2894 pr_err("%s: DSP returned error[%s]\n",
2895 __func__, adsp_err_get_err_str(
2896 atomic_read(&this_adm.copp.stat
2897 [port_idx][copp_idx])));
2898 return adsp_err_get_lnx_err_code(
2899 atomic_read(&this_adm.copp.stat
2900 [port_idx][copp_idx]));
2901 }
2902 }
2903 atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
2904 return copp_idx;
2905}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302906EXPORT_SYMBOL(adm_open);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302907
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302908/**
2909 * adm_copp_mfc_cfg -
2910 * command to send ADM MFC config
2911 *
2912 * @port_id: Port ID number
2913 * @copp_idx: copp index assigned
2914 * @dst_sample_rate: sink sample rate
2915 *
2916 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302917void adm_copp_mfc_cfg(int port_id, int copp_idx, int dst_sample_rate)
2918{
2919 struct audproc_mfc_output_media_fmt mfc_cfg;
2920 struct adm_cmd_device_open_v5 open;
2921 int port_idx;
2922 int sz = 0;
2923 int rc = 0;
2924 int i = 0;
2925
2926 port_id = q6audio_convert_virtual_to_portid(port_id);
2927 port_idx = adm_validate_and_get_port_index(port_id);
2928
2929 if (port_idx < 0) {
2930 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
2931 goto fail_cmd;
2932 }
2933
2934 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
2935 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
2936 goto fail_cmd;
2937 }
2938
2939 sz = sizeof(struct audproc_mfc_output_media_fmt);
2940
2941 mfc_cfg.params.hdr.hdr_field =
2942 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2943 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
2944 mfc_cfg.params.hdr.pkt_size = sz;
2945 mfc_cfg.params.hdr.src_svc = APR_SVC_ADM;
2946 mfc_cfg.params.hdr.src_domain = APR_DOMAIN_APPS;
2947 mfc_cfg.params.hdr.src_port = port_id;
2948 mfc_cfg.params.hdr.dest_svc = APR_SVC_ADM;
2949 mfc_cfg.params.hdr.dest_domain = APR_DOMAIN_ADSP;
2950 mfc_cfg.params.hdr.dest_port =
2951 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
2952 mfc_cfg.params.hdr.token = port_idx << 16 | copp_idx;
2953 mfc_cfg.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
2954 mfc_cfg.params.payload_addr_lsw = 0;
2955 mfc_cfg.params.payload_addr_msw = 0;
2956 mfc_cfg.params.mem_map_handle = 0;
2957 mfc_cfg.params.payload_size = sizeof(mfc_cfg) -
2958 sizeof(mfc_cfg.params);
2959 mfc_cfg.data.module_id = AUDPROC_MODULE_ID_MFC;
2960 mfc_cfg.data.param_id =
2961 AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
2962 mfc_cfg.data.param_size = mfc_cfg.params.payload_size -
2963 sizeof(mfc_cfg.data);
2964 mfc_cfg.data.reserved = 0;
2965 mfc_cfg.sampling_rate = dst_sample_rate;
2966 mfc_cfg.bits_per_sample =
2967 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
2968 open.dev_num_channel = mfc_cfg.num_channels =
2969 atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
2970
2971 rc = adm_arrange_mch_map(&open, ADM_PATH_PLAYBACK,
Rohit kumar5d860f42019-02-01 18:01:12 +05302972 mfc_cfg.num_channels, port_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302973 if (rc < 0) {
2974 pr_err("%s: unable to get channal map\n", __func__);
2975 goto fail_cmd;
2976 }
2977
2978 for (i = 0; i < mfc_cfg.num_channels; i++)
2979 mfc_cfg.channel_type[i] =
2980 (uint16_t) open.dev_channel_mapping[i];
2981
2982 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2983
2984 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",
2985 __func__, port_idx, copp_idx,
2986 atomic_read(&this_adm.copp.rate[port_idx][copp_idx]),
2987 mfc_cfg.bits_per_sample, mfc_cfg.num_channels,
2988 mfc_cfg.sampling_rate);
2989
2990 rc = apr_send_pkt(this_adm.apr, (uint32_t *)&mfc_cfg);
2991
2992 if (rc < 0) {
2993 pr_err("%s: port_id: for[0x%x] failed %d\n",
2994 __func__, port_id, rc);
2995 goto fail_cmd;
2996 }
2997 /* Wait for the callback with copp id */
2998 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
2999 atomic_read(&this_adm.copp.stat
3000 [port_idx][copp_idx]) >= 0,
3001 msecs_to_jiffies(TIMEOUT_MS));
3002 if (!rc) {
3003 pr_err("%s: mfc_cfg Set params timed out for port_id: for [0x%x]\n",
3004 __func__, port_id);
3005 goto fail_cmd;
3006 } else if (atomic_read(&this_adm.copp.stat
3007 [port_idx][copp_idx]) > 0) {
3008 pr_err("%s: DSP returned error[%s]\n",
3009 __func__, adsp_err_get_err_str(
3010 atomic_read(&this_adm.copp.stat
3011 [port_idx][copp_idx])));
3012 goto fail_cmd;
3013 }
3014 rc = 0;
3015fail_cmd:
3016 return;
3017}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303018EXPORT_SYMBOL(adm_copp_mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303019
3020static void route_set_opcode_matrix_id(
3021 struct adm_cmd_matrix_map_routings_v5 **route_addr,
3022 int path, uint32_t passthr_mode)
3023{
3024 struct adm_cmd_matrix_map_routings_v5 *route = *route_addr;
3025
3026 switch (path) {
3027 case ADM_PATH_PLAYBACK:
3028 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
3029 route->matrix_id = ADM_MATRIX_ID_AUDIO_RX;
3030 break;
3031 case ADM_PATH_LIVE_REC:
3032 if (passthr_mode == LISTEN) {
3033 route->hdr.opcode =
3034 ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3035 route->matrix_id = ADM_MATRIX_ID_LISTEN_TX;
3036 break;
3037 }
3038 /* fall through to set matrix id for non-listen case */
3039 case ADM_PATH_NONLIVE_REC:
3040 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
3041 route->matrix_id = ADM_MATRIX_ID_AUDIO_TX;
3042 break;
3043 case ADM_PATH_COMPRESSED_RX:
3044 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3045 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_RX;
3046 break;
3047 case ADM_PATH_COMPRESSED_TX:
3048 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3049 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_TX;
3050 break;
3051 default:
3052 pr_err("%s: Wrong path set[%d]\n", __func__, path);
3053 break;
3054 }
3055 pr_debug("%s: opcode 0x%x, matrix id %d\n",
3056 __func__, route->hdr.opcode, route->matrix_id);
3057}
3058
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303059/**
3060 * adm_matrix_map -
3061 * command to send ADM matrix map for ADM copp list
3062 *
3063 * @path: direction or ADM path type
3064 * @payload_map: have info of session id and associated copp_idx/num_copps
3065 * @perf_mode: performance mode like LL/ULL/..
3066 * @passthr_mode: flag to indicate passthrough mode
3067 *
3068 * Returns 0 on success or error on failure
3069 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303070int adm_matrix_map(int path, struct route_payload payload_map, int perf_mode,
3071 uint32_t passthr_mode)
3072{
3073 struct adm_cmd_matrix_map_routings_v5 *route;
3074 struct adm_session_map_node_v5 *node;
3075 uint16_t *copps_list;
3076 int cmd_size = 0;
3077 int ret = 0, i = 0;
3078 void *payload = NULL;
3079 void *matrix_map = NULL;
3080 int port_idx, copp_idx;
3081
3082 /* Assumes port_ids have already been validated during adm_open */
3083 cmd_size = (sizeof(struct adm_cmd_matrix_map_routings_v5) +
3084 sizeof(struct adm_session_map_node_v5) +
3085 (sizeof(uint32_t) * payload_map.num_copps));
3086 matrix_map = kzalloc(cmd_size, GFP_KERNEL);
3087 if (matrix_map == NULL) {
3088 pr_err("%s: Mem alloc failed\n", __func__);
3089 ret = -EINVAL;
3090 return ret;
3091 }
3092 route = (struct adm_cmd_matrix_map_routings_v5 *)matrix_map;
3093
3094 route->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3095 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
3096 route->hdr.pkt_size = cmd_size;
3097 route->hdr.src_svc = 0;
3098 route->hdr.src_domain = APR_DOMAIN_APPS;
3099 route->hdr.src_port = 0; /* Ignored */;
3100 route->hdr.dest_svc = APR_SVC_ADM;
3101 route->hdr.dest_domain = APR_DOMAIN_ADSP;
3102 route->hdr.dest_port = 0; /* Ignored */;
3103 route->hdr.token = 0;
3104 route->num_sessions = 1;
3105 route_set_opcode_matrix_id(&route, path, passthr_mode);
3106
3107 payload = ((u8 *)matrix_map +
3108 sizeof(struct adm_cmd_matrix_map_routings_v5));
3109 node = (struct adm_session_map_node_v5 *)payload;
3110
3111 node->session_id = payload_map.session_id;
3112 node->num_copps = payload_map.num_copps;
3113 payload = (u8 *)node + sizeof(struct adm_session_map_node_v5);
3114 copps_list = (uint16_t *)payload;
3115 for (i = 0; i < payload_map.num_copps; i++) {
3116 port_idx =
3117 adm_validate_and_get_port_index(payload_map.port_id[i]);
3118 if (port_idx < 0) {
3119 pr_err("%s: Invalid port_id 0x%x\n", __func__,
3120 payload_map.port_id[i]);
3121 ret = -EINVAL;
3122 goto fail_cmd;
3123 }
3124 copp_idx = payload_map.copp_idx[i];
3125 copps_list[i] = atomic_read(&this_adm.copp.id[port_idx]
3126 [copp_idx]);
3127 }
3128 atomic_set(&this_adm.matrix_map_stat, -1);
3129
3130 ret = apr_send_pkt(this_adm.apr, (uint32_t *)matrix_map);
3131 if (ret < 0) {
3132 pr_err("%s: routing for syream %d failed ret %d\n",
3133 __func__, payload_map.session_id, ret);
3134 ret = -EINVAL;
3135 goto fail_cmd;
3136 }
3137 ret = wait_event_timeout(this_adm.matrix_map_wait,
3138 atomic_read(&this_adm.matrix_map_stat) >= 0,
3139 msecs_to_jiffies(TIMEOUT_MS));
3140 if (!ret) {
3141 pr_err("%s: routing for syream %d failed\n", __func__,
3142 payload_map.session_id);
3143 ret = -EINVAL;
3144 goto fail_cmd;
3145 } else if (atomic_read(&this_adm.matrix_map_stat) > 0) {
3146 pr_err("%s: DSP returned error[%s]\n", __func__,
3147 adsp_err_get_err_str(atomic_read(
3148 &this_adm.matrix_map_stat)));
3149 ret = adsp_err_get_lnx_err_code(
3150 atomic_read(&this_adm.matrix_map_stat));
3151 goto fail_cmd;
3152 }
3153
3154 if ((perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) &&
3155 (path != ADM_PATH_COMPRESSED_RX)) {
3156 for (i = 0; i < payload_map.num_copps; i++) {
3157 port_idx = afe_get_port_index(payload_map.port_id[i]);
3158 copp_idx = payload_map.copp_idx[i];
3159 if (port_idx < 0 || copp_idx < 0 ||
3160 (copp_idx > MAX_COPPS_PER_PORT - 1)) {
3161 pr_err("%s: Invalid idx port_idx %d copp_idx %d\n",
3162 __func__, port_idx, copp_idx);
3163 continue;
3164 }
3165 rtac_add_adm_device(payload_map.port_id[i],
3166 atomic_read(&this_adm.copp.id
3167 [port_idx][copp_idx]),
3168 get_cal_path(path),
3169 payload_map.session_id,
3170 payload_map.app_type[i],
3171 payload_map.acdb_dev_id[i]);
3172
3173 if (!test_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3174 (void *)&this_adm.copp.adm_status[port_idx]
3175 [copp_idx])) {
3176 pr_debug("%s: adm copp[0x%x][%d] already sent",
3177 __func__, port_idx, copp_idx);
3178 continue;
3179 }
3180 send_adm_cal(payload_map.port_id[i], copp_idx,
3181 get_cal_path(path), perf_mode,
3182 payload_map.app_type[i],
3183 payload_map.acdb_dev_id[i],
Aditya Bavanarifcd80ec2018-01-08 13:16:32 +05303184 payload_map.sample_rate[i],
3185 passthr_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303186 /* ADM COPP calibration is already sent */
3187 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3188 (void *)&this_adm.copp.
3189 adm_status[port_idx][copp_idx]);
3190 pr_debug("%s: copp_id: %d\n", __func__,
3191 atomic_read(&this_adm.copp.id[port_idx]
3192 [copp_idx]));
3193 }
3194 }
3195
3196fail_cmd:
3197 kfree(matrix_map);
3198 return ret;
3199}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303200EXPORT_SYMBOL(adm_matrix_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303201
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303202/**
3203 * adm_ec_ref_rx_id -
3204 * Update EC ref port ID
3205 *
3206 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303207void adm_ec_ref_rx_id(int port_id)
3208{
3209 this_adm.ec_ref_rx = port_id;
3210 pr_debug("%s: ec_ref_rx:%d\n", __func__, this_adm.ec_ref_rx);
3211}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303212EXPORT_SYMBOL(adm_ec_ref_rx_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303213
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303214/**
3215 * adm_num_ec_ref_rx_chans -
3216 * Update EC ref number of channels
3217 *
3218 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303219void adm_num_ec_ref_rx_chans(int num_chans)
3220{
3221 this_adm.num_ec_ref_rx_chans = num_chans;
3222 pr_debug("%s: num_ec_ref_rx_chans:%d\n",
3223 __func__, this_adm.num_ec_ref_rx_chans);
3224}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303225EXPORT_SYMBOL(adm_num_ec_ref_rx_chans);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303226
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303227/**
3228 * adm_ec_ref_rx_bit_width -
3229 * Update EC ref bit_width
3230 *
3231 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303232void adm_ec_ref_rx_bit_width(int bit_width)
3233{
3234 this_adm.ec_ref_rx_bit_width = bit_width;
3235 pr_debug("%s: ec_ref_rx_bit_width:%d\n",
3236 __func__, this_adm.ec_ref_rx_bit_width);
3237}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303238EXPORT_SYMBOL(adm_ec_ref_rx_bit_width);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303239
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303240/**
3241 * adm_ec_ref_rx_sampling_rate -
3242 * Update EC ref sample rate
3243 *
3244 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303245void adm_ec_ref_rx_sampling_rate(int sampling_rate)
3246{
3247 this_adm.ec_ref_rx_sampling_rate = sampling_rate;
3248 pr_debug("%s: ec_ref_rx_sampling_rate:%d\n",
3249 __func__, this_adm.ec_ref_rx_sampling_rate);
3250}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303251EXPORT_SYMBOL(adm_ec_ref_rx_sampling_rate);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303252
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303253/**
3254 * adm_close -
3255 * command to close ADM copp
3256 *
3257 * @port_id: Port ID number
3258 * @perf_mode: performance mode like LL/ULL/..
3259 * @copp_idx: copp index assigned
3260 *
3261 * Returns 0 on success or error on failure
3262 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303263int adm_close(int port_id, int perf_mode, int copp_idx)
3264{
3265 struct apr_hdr close;
3266
3267 int ret = 0, port_idx;
3268 int copp_id = RESET_COPP_ID;
3269
3270 pr_debug("%s: port_id=0x%x perf_mode: %d copp_idx: %d\n", __func__,
3271 port_id, perf_mode, copp_idx);
3272
3273 port_id = q6audio_convert_virtual_to_portid(port_id);
3274 port_idx = adm_validate_and_get_port_index(port_id);
3275 if (port_idx < 0) {
3276 pr_err("%s: Invalid port_id 0x%x\n",
3277 __func__, port_id);
3278 return -EINVAL;
3279 }
3280
3281 if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
3282 pr_err("%s: Invalid copp idx: %d\n", __func__, copp_idx);
3283 return -EINVAL;
3284 }
3285
Rohit kumar5d860f42019-02-01 18:01:12 +05303286 port_channel_map[port_idx].set_channel_map = false;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303287 if (this_adm.copp.adm_delay[port_idx][copp_idx] && perf_mode
3288 == LEGACY_PCM_MODE) {
3289 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
3290 1);
3291 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
3292 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
3293 }
3294
3295 atomic_dec(&this_adm.copp.cnt[port_idx][copp_idx]);
3296 if (!(atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]))) {
3297 copp_id = adm_get_copp_id(port_idx, copp_idx);
3298 pr_debug("%s: Closing ADM port_idx:%d copp_idx:%d copp_id:0x%x\n",
3299 __func__, port_idx, copp_idx, copp_id);
3300 if ((!perf_mode) && (this_adm.outband_memmap.paddr != 0) &&
3301 (atomic_read(&this_adm.copp.topology[port_idx][copp_idx]) ==
3302 SRS_TRUMEDIA_TOPOLOGY_ID)) {
3303 atomic_set(&this_adm.mem_map_index,
3304 ADM_SRS_TRUMEDIA);
3305 ret = adm_memory_unmap_regions();
3306 if (ret < 0) {
3307 pr_err("%s: adm mem unmmap err %d",
3308 __func__, ret);
3309 } else {
3310 atomic_set(&this_adm.mem_map_handles
3311 [ADM_SRS_TRUMEDIA], 0);
3312 }
3313 }
3314
3315
3316 if ((afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX) &&
3317 this_adm.sourceTrackingData.memmap.paddr) {
3318 atomic_set(&this_adm.mem_map_index,
3319 ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
3320 ret = adm_memory_unmap_regions();
3321 if (ret < 0) {
3322 pr_err("%s: adm mem unmmap err %d",
3323 __func__, ret);
3324 }
3325 msm_audio_ion_free(
3326 this_adm.sourceTrackingData.ion_client,
3327 this_adm.sourceTrackingData.ion_handle);
3328 this_adm.sourceTrackingData.ion_client = NULL;
3329 this_adm.sourceTrackingData.ion_handle = NULL;
3330 this_adm.sourceTrackingData.memmap.size = 0;
3331 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
3332 this_adm.sourceTrackingData.memmap.paddr = 0;
3333 this_adm.sourceTrackingData.apr_cmd_status = -1;
3334 atomic_set(&this_adm.mem_map_handles[
3335 ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
3336 }
3337
3338 close.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3339 APR_HDR_LEN(APR_HDR_SIZE),
3340 APR_PKT_VER);
3341 close.pkt_size = sizeof(close);
3342 close.src_svc = APR_SVC_ADM;
3343 close.src_domain = APR_DOMAIN_APPS;
3344 close.src_port = port_id;
3345 close.dest_svc = APR_SVC_ADM;
3346 close.dest_domain = APR_DOMAIN_ADSP;
3347 close.dest_port = copp_id;
3348 close.token = port_idx << 16 | copp_idx;
3349 close.opcode = ADM_CMD_DEVICE_CLOSE_V5;
3350
3351 atomic_set(&this_adm.copp.id[port_idx][copp_idx],
3352 RESET_COPP_ID);
3353 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
3354 atomic_set(&this_adm.copp.topology[port_idx][copp_idx], 0);
3355 atomic_set(&this_adm.copp.mode[port_idx][copp_idx], 0);
3356 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3357 atomic_set(&this_adm.copp.rate[port_idx][copp_idx], 0);
3358 atomic_set(&this_adm.copp.channels[port_idx][copp_idx], 0);
3359 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx], 0);
3360 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx], 0);
Sachin Mohan Gadag3c3c5812018-07-20 15:46:54 +05303361 atomic_set(&this_adm.copp.session_type[port_idx][copp_idx], 0);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303362
3363 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3364 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
3365
3366 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&close);
3367 if (ret < 0) {
3368 pr_err("%s: ADM close failed %d\n", __func__, ret);
3369 return -EINVAL;
3370 }
3371
3372 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3373 atomic_read(&this_adm.copp.stat
3374 [port_idx][copp_idx]) >= 0,
3375 msecs_to_jiffies(TIMEOUT_MS));
3376 if (!ret) {
3377 pr_err("%s: ADM cmd Route timedout for port 0x%x\n",
3378 __func__, port_id);
3379 return -EINVAL;
3380 } else if (atomic_read(&this_adm.copp.stat
3381 [port_idx][copp_idx]) > 0) {
3382 pr_err("%s: DSP returned error[%s]\n",
3383 __func__, adsp_err_get_err_str(
3384 atomic_read(&this_adm.copp.stat
3385 [port_idx][copp_idx])));
3386 return adsp_err_get_lnx_err_code(
3387 atomic_read(&this_adm.copp.stat
3388 [port_idx][copp_idx]));
3389 }
3390 }
3391
3392 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) {
3393 pr_debug("%s: remove adm device from rtac\n", __func__);
3394 rtac_remove_adm_device(port_id, copp_id);
3395 }
3396 return 0;
3397}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303398EXPORT_SYMBOL(adm_close);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303399
3400int send_rtac_audvol_cal(void)
3401{
3402 int ret = 0;
3403 int ret2 = 0;
3404 int i = 0;
3405 int copp_idx, port_idx, acdb_id, app_id, path;
3406 struct cal_block_data *cal_block = NULL;
3407 struct audio_cal_info_audvol *audvol_cal_info = NULL;
3408 struct rtac_adm rtac_adm_data;
3409
3410 mutex_lock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3411
3412 cal_block = cal_utils_get_only_cal_block(
3413 this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]);
Vikram Pandurangad3b58cc2017-09-27 12:17:36 -07003414 if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303415 pr_err("%s: can't find cal block!\n", __func__);
3416 goto unlock;
3417 }
3418
3419 audvol_cal_info = cal_block->cal_info;
3420 if (audvol_cal_info == NULL) {
3421 pr_err("%s: audvol_cal_info is NULL!\n", __func__);
3422 goto unlock;
3423 }
3424
3425 get_rtac_adm_data(&rtac_adm_data);
3426 for (; i < rtac_adm_data.num_of_dev; i++) {
3427
3428 acdb_id = rtac_adm_data.device[i].acdb_dev_id;
3429 if (acdb_id == 0)
3430 acdb_id = audvol_cal_info->acdb_id;
3431
3432 app_id = rtac_adm_data.device[i].app_type;
3433 if (app_id == 0)
3434 app_id = audvol_cal_info->app_type;
3435
3436 path = afe_get_port_type(rtac_adm_data.device[i].afe_port);
3437 if ((acdb_id == audvol_cal_info->acdb_id) &&
3438 (app_id == audvol_cal_info->app_type) &&
3439 (path == audvol_cal_info->path)) {
3440
3441 if (adm_get_indexes_from_copp_id(rtac_adm_data.
3442 device[i].copp, &copp_idx, &port_idx) != 0) {
3443 pr_debug("%s: Copp Id %d is not active\n",
3444 __func__,
3445 rtac_adm_data.device[i].copp);
3446 continue;
3447 }
3448
3449 ret2 = adm_remap_and_send_cal_block(ADM_RTAC_AUDVOL_CAL,
3450 rtac_adm_data.device[i].afe_port,
3451 copp_idx, cal_block,
3452 atomic_read(&this_adm.copp.
3453 mode[port_idx][copp_idx]),
3454 audvol_cal_info->app_type,
3455 audvol_cal_info->acdb_id,
3456 atomic_read(&this_adm.copp.
3457 rate[port_idx][copp_idx]));
3458 if (ret2 < 0) {
3459 pr_debug("%s: remap and send failed for copp Id %d, acdb id %d, app type %d, path %d\n",
3460 __func__, rtac_adm_data.device[i].copp,
3461 audvol_cal_info->acdb_id,
3462 audvol_cal_info->app_type,
3463 audvol_cal_info->path);
3464 ret = ret2;
3465 }
3466 }
3467 }
3468unlock:
3469 mutex_unlock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3470 return ret;
3471}
3472
3473int adm_map_rtac_block(struct rtac_cal_block_data *cal_block)
3474{
3475 int result = 0;
3476
3477 pr_debug("%s:\n", __func__);
3478
3479 if (cal_block == NULL) {
3480 pr_err("%s: cal_block is NULL!\n",
3481 __func__);
3482 result = -EINVAL;
3483 goto done;
3484 }
3485
3486 if (cal_block->cal_data.paddr == 0) {
3487 pr_debug("%s: No address to map!\n",
3488 __func__);
3489 result = -EINVAL;
3490 goto done;
3491 }
3492
3493 if (cal_block->map_data.map_size == 0) {
3494 pr_debug("%s: map size is 0!\n",
3495 __func__);
3496 result = -EINVAL;
3497 goto done;
3498 }
3499
3500 /* valid port ID needed for callback use primary I2S */
3501 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3502 result = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3503 &cal_block->map_data.map_size, 1);
3504 if (result < 0) {
3505 pr_err("%s: RTAC mmap did not work! size = %d result %d\n",
3506 __func__,
3507 cal_block->map_data.map_size, result);
3508 pr_debug("%s: RTAC mmap did not work! addr = 0x%pK, size = %d\n",
3509 __func__,
3510 &cal_block->cal_data.paddr,
3511 cal_block->map_data.map_size);
3512 goto done;
3513 }
3514
3515 cal_block->map_data.map_handle = atomic_read(
3516 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]);
3517done:
3518 return result;
3519}
3520
3521int adm_unmap_rtac_block(uint32_t *mem_map_handle)
3522{
3523 int result = 0;
3524
3525 pr_debug("%s:\n", __func__);
3526
3527 if (mem_map_handle == NULL) {
3528 pr_debug("%s: Map handle is NULL, nothing to unmap\n",
3529 __func__);
3530 goto done;
3531 }
3532
3533 if (*mem_map_handle == 0) {
3534 pr_debug("%s: Map handle is 0, nothing to unmap\n",
3535 __func__);
3536 goto done;
3537 }
3538
3539 if (*mem_map_handle != atomic_read(
3540 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL])) {
3541 pr_err("%s: Map handles do not match! Unmapping RTAC, RTAC map 0x%x, ADM map 0x%x\n",
3542 __func__, *mem_map_handle, atomic_read(
3543 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]));
3544
3545 /* if mismatch use handle passed in to unmap */
3546 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL],
3547 *mem_map_handle);
3548 }
3549
3550 /* valid port ID needed for callback use primary I2S */
3551 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3552 result = adm_memory_unmap_regions();
3553 if (result < 0) {
3554 pr_debug("%s: adm_memory_unmap_regions failed, error %d\n",
3555 __func__, result);
3556 } else {
3557 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL], 0);
3558 *mem_map_handle = 0;
3559 }
3560done:
3561 return result;
3562}
3563
3564static int get_cal_type_index(int32_t cal_type)
3565{
3566 int ret = -EINVAL;
3567
3568 switch (cal_type) {
3569 case ADM_AUDPROC_CAL_TYPE:
3570 ret = ADM_AUDPROC_CAL;
3571 break;
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05303572 case ADM_LSM_AUDPROC_CAL_TYPE:
3573 ret = ADM_LSM_AUDPROC_CAL;
3574 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303575 case ADM_AUDVOL_CAL_TYPE:
3576 ret = ADM_AUDVOL_CAL;
3577 break;
3578 case ADM_CUST_TOPOLOGY_CAL_TYPE:
3579 ret = ADM_CUSTOM_TOP_CAL;
3580 break;
3581 case ADM_RTAC_INFO_CAL_TYPE:
3582 ret = ADM_RTAC_INFO_CAL;
3583 break;
3584 case ADM_RTAC_APR_CAL_TYPE:
3585 ret = ADM_RTAC_APR_CAL;
3586 break;
3587 case ADM_RTAC_AUDVOL_CAL_TYPE:
3588 ret = ADM_RTAC_AUDVOL_CAL;
3589 break;
Bhalchandra Gajareface2762018-05-10 14:16:49 -07003590 case ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE:
3591 ret = ADM_LSM_AUDPROC_PERSISTENT_CAL;
3592 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303593 default:
3594 pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
3595 }
3596 return ret;
3597}
3598
3599static int adm_alloc_cal(int32_t cal_type, size_t data_size, void *data)
3600{
3601 int ret = 0;
3602 int cal_index;
3603
3604 pr_debug("%s:\n", __func__);
3605
3606 cal_index = get_cal_type_index(cal_type);
3607 if (cal_index < 0) {
3608 pr_err("%s: could not get cal index %d!\n",
3609 __func__, cal_index);
3610 ret = -EINVAL;
3611 goto done;
3612 }
3613
3614 ret = cal_utils_alloc_cal(data_size, data,
3615 this_adm.cal_data[cal_index], 0, NULL);
3616 if (ret < 0) {
3617 pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
3618 __func__, ret, cal_type);
3619 ret = -EINVAL;
3620 goto done;
3621 }
3622done:
3623 return ret;
3624}
3625
3626static int adm_dealloc_cal(int32_t cal_type, size_t data_size, void *data)
3627{
3628 int ret = 0;
3629 int cal_index;
3630
3631 pr_debug("%s:\n", __func__);
3632
3633 cal_index = get_cal_type_index(cal_type);
3634 if (cal_index < 0) {
3635 pr_err("%s: could not get cal index %d!\n",
3636 __func__, cal_index);
3637 ret = -EINVAL;
3638 goto done;
3639 }
3640
3641 ret = cal_utils_dealloc_cal(data_size, data,
3642 this_adm.cal_data[cal_index]);
3643 if (ret < 0) {
3644 pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
3645 __func__, ret, cal_type);
3646 ret = -EINVAL;
3647 goto done;
3648 }
3649done:
3650 return ret;
3651}
3652
3653static int adm_set_cal(int32_t cal_type, size_t data_size, void *data)
3654{
3655 int ret = 0;
3656 int cal_index;
3657
3658 pr_debug("%s:\n", __func__);
3659
3660 cal_index = get_cal_type_index(cal_type);
3661 if (cal_index < 0) {
3662 pr_err("%s: could not get cal index %d!\n",
3663 __func__, cal_index);
3664 ret = -EINVAL;
3665 goto done;
3666 }
3667
3668 ret = cal_utils_set_cal(data_size, data,
3669 this_adm.cal_data[cal_index], 0, NULL);
3670 if (ret < 0) {
3671 pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
3672 __func__, ret, cal_type);
3673 ret = -EINVAL;
3674 goto done;
3675 }
3676
3677 if (cal_index == ADM_CUSTOM_TOP_CAL) {
3678 mutex_lock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3679 this_adm.set_custom_topology = 1;
3680 mutex_unlock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3681 } else if (cal_index == ADM_RTAC_AUDVOL_CAL) {
3682 send_rtac_audvol_cal();
3683 }
3684done:
3685 return ret;
3686}
3687
3688static int adm_map_cal_data(int32_t cal_type,
3689 struct cal_block_data *cal_block)
3690{
3691 int ret = 0;
3692 int cal_index;
3693
3694 pr_debug("%s:\n", __func__);
3695
3696 cal_index = get_cal_type_index(cal_type);
3697 if (cal_index < 0) {
3698 pr_err("%s: could not get cal index %d!\n",
3699 __func__, cal_index);
3700 ret = -EINVAL;
3701 goto done;
3702 }
3703
3704 atomic_set(&this_adm.mem_map_index, cal_index);
3705 ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3706 (uint32_t *)&cal_block->map_data.map_size, 1);
3707 if (ret < 0) {
3708 pr_err("%s: map did not work! cal_type %i ret %d\n",
3709 __func__, cal_index, ret);
3710 ret = -ENODEV;
3711 goto done;
3712 }
3713 cal_block->map_data.q6map_handle = atomic_read(&this_adm.
3714 mem_map_handles[cal_index]);
3715done:
3716 return ret;
3717}
3718
3719static int adm_unmap_cal_data(int32_t cal_type,
3720 struct cal_block_data *cal_block)
3721{
3722 int ret = 0;
3723 int cal_index;
3724
3725 pr_debug("%s:\n", __func__);
3726
3727 cal_index = get_cal_type_index(cal_type);
3728 if (cal_index < 0) {
3729 pr_err("%s: could not get cal index %d!\n",
3730 __func__, cal_index);
3731 ret = -EINVAL;
3732 goto done;
3733 }
3734
3735 if (cal_block == NULL) {
3736 pr_err("%s: Cal block is NULL!\n",
3737 __func__);
3738 goto done;
3739 }
3740
3741 if (cal_block->map_data.q6map_handle == 0) {
3742 pr_err("%s: Map handle is NULL, nothing to unmap\n",
3743 __func__);
3744 goto done;
3745 }
3746
3747 atomic_set(&this_adm.mem_map_handles[cal_index],
3748 cal_block->map_data.q6map_handle);
3749 atomic_set(&this_adm.mem_map_index, cal_index);
3750 ret = adm_memory_unmap_regions();
3751 if (ret < 0) {
3752 pr_err("%s: unmap did not work! cal_type %i ret %d\n",
3753 __func__, cal_index, ret);
3754 ret = -ENODEV;
3755 goto done;
3756 }
3757 cal_block->map_data.q6map_handle = 0;
3758done:
3759 return ret;
3760}
3761
3762static void adm_delete_cal_data(void)
3763{
3764 pr_debug("%s:\n", __func__);
3765
3766 cal_utils_destroy_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data);
3767}
3768
3769static int adm_init_cal_data(void)
3770{
3771 int ret = 0;
3772 struct cal_type_info cal_type_info[] = {
3773 {{ADM_CUST_TOPOLOGY_CAL_TYPE,
3774 {adm_alloc_cal, adm_dealloc_cal, NULL,
3775 adm_set_cal, NULL, NULL} },
3776 {adm_map_cal_data, adm_unmap_cal_data,
3777 cal_utils_match_buf_num} },
3778
3779 {{ADM_AUDPROC_CAL_TYPE,
3780 {adm_alloc_cal, adm_dealloc_cal, NULL,
3781 adm_set_cal, NULL, NULL} },
3782 {adm_map_cal_data, adm_unmap_cal_data,
3783 cal_utils_match_buf_num} },
3784
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05303785 {{ADM_LSM_AUDPROC_CAL_TYPE,
3786 {adm_alloc_cal, adm_dealloc_cal, NULL,
3787 adm_set_cal, NULL, NULL} },
3788 {adm_map_cal_data, adm_unmap_cal_data,
3789 cal_utils_match_buf_num} },
3790
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303791 {{ADM_AUDVOL_CAL_TYPE,
3792 {adm_alloc_cal, adm_dealloc_cal, NULL,
3793 adm_set_cal, NULL, NULL} },
3794 {adm_map_cal_data, adm_unmap_cal_data,
3795 cal_utils_match_buf_num} },
3796
3797 {{ADM_RTAC_INFO_CAL_TYPE,
3798 {NULL, NULL, NULL, NULL, NULL, NULL} },
3799 {NULL, NULL, cal_utils_match_buf_num} },
3800
3801 {{ADM_RTAC_APR_CAL_TYPE,
3802 {NULL, NULL, NULL, NULL, NULL, NULL} },
3803 {NULL, NULL, cal_utils_match_buf_num} },
3804
3805 {{SRS_TRUMEDIA_CAL_TYPE,
3806 {NULL, NULL, NULL, NULL, NULL, NULL} },
3807 {NULL, NULL, cal_utils_match_buf_num} },
3808
3809 {{ADM_RTAC_AUDVOL_CAL_TYPE,
3810 {adm_alloc_cal, adm_dealloc_cal, NULL,
3811 adm_set_cal, NULL, NULL} },
3812 {adm_map_cal_data, adm_unmap_cal_data,
3813 cal_utils_match_buf_num} },
Bhalchandra Gajareface2762018-05-10 14:16:49 -07003814
3815 {{ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE,
3816 {adm_alloc_cal, adm_dealloc_cal, NULL,
3817 adm_set_cal, NULL, NULL} },
3818 {adm_map_cal_data, adm_unmap_cal_data,
3819 cal_utils_match_buf_num} },
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303820 };
3821 pr_debug("%s:\n", __func__);
3822
3823 ret = cal_utils_create_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data,
3824 cal_type_info);
3825 if (ret < 0) {
3826 pr_err("%s: could not create cal type! ret %d\n",
3827 __func__, ret);
3828 ret = -EINVAL;
3829 goto err;
3830 }
3831
3832 return ret;
3833err:
3834 adm_delete_cal_data();
3835 return ret;
3836}
3837
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303838/**
3839 * adm_set_volume -
3840 * command to set volume on ADM copp
3841 *
3842 * @port_id: Port ID number
3843 * @copp_idx: copp index assigned
3844 * @volume: gain value to set
3845 *
3846 * Returns 0 on success or error on failure
3847 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303848int adm_set_volume(int port_id, int copp_idx, int volume)
3849{
3850 struct audproc_volume_ctrl_master_gain audproc_vol;
3851 int sz = 0;
3852 int rc = 0;
3853 int port_idx;
3854
3855 pr_debug("%s: port_id %d, volume %d\n", __func__, port_id, volume);
3856 port_id = afe_convert_virtual_to_portid(port_id);
3857 port_idx = adm_validate_and_get_port_index(port_id);
3858 if (port_idx < 0) {
3859 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
3860 rc = -EINVAL;
3861 goto fail_cmd;
3862 }
3863
3864 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3865 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3866 return -EINVAL;
3867 }
3868
3869 sz = sizeof(struct audproc_volume_ctrl_master_gain);
3870 audproc_vol.params.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3871 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
3872 audproc_vol.params.hdr.pkt_size = sz;
3873 audproc_vol.params.hdr.src_svc = APR_SVC_ADM;
3874 audproc_vol.params.hdr.src_domain = APR_DOMAIN_APPS;
3875 audproc_vol.params.hdr.src_port = port_id;
3876 audproc_vol.params.hdr.dest_svc = APR_SVC_ADM;
3877 audproc_vol.params.hdr.dest_domain = APR_DOMAIN_ADSP;
3878 audproc_vol.params.hdr.dest_port =
3879 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
3880 audproc_vol.params.hdr.token = port_idx << 16 | copp_idx;
3881 audproc_vol.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
3882 audproc_vol.params.payload_addr_lsw = 0;
3883 audproc_vol.params.payload_addr_msw = 0;
3884 audproc_vol.params.mem_map_handle = 0;
3885 audproc_vol.params.payload_size = sizeof(audproc_vol) -
3886 sizeof(audproc_vol.params);
3887 audproc_vol.data.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
3888 audproc_vol.data.param_id = AUDPROC_PARAM_ID_VOL_CTRL_MASTER_GAIN;
3889 audproc_vol.data.param_size = audproc_vol.params.payload_size -
3890 sizeof(audproc_vol.data);
3891 audproc_vol.data.reserved = 0;
3892 audproc_vol.master_gain = volume;
3893
3894 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3895 rc = apr_send_pkt(this_adm.apr, (uint32_t *)&audproc_vol);
3896 if (rc < 0) {
3897 pr_err("%s: Set params failed port = %#x\n",
3898 __func__, port_id);
3899 rc = -EINVAL;
3900 goto fail_cmd;
3901 }
3902 /* Wait for the callback */
3903 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3904 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
3905 msecs_to_jiffies(TIMEOUT_MS));
3906 if (!rc) {
3907 pr_err("%s: Vol cntrl Set params timed out port = %#x\n",
3908 __func__, port_id);
3909 rc = -EINVAL;
3910 goto fail_cmd;
3911 } else if (atomic_read(&this_adm.copp.stat
3912 [port_idx][copp_idx]) > 0) {
3913 pr_err("%s: DSP returned error[%s]\n",
3914 __func__, adsp_err_get_err_str(
3915 atomic_read(&this_adm.copp.stat
3916 [port_idx][copp_idx])));
3917 rc = adsp_err_get_lnx_err_code(
3918 atomic_read(&this_adm.copp.stat
3919 [port_idx][copp_idx]));
3920 goto fail_cmd;
3921 }
3922 rc = 0;
3923fail_cmd:
3924 return rc;
3925}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303926EXPORT_SYMBOL(adm_set_volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303927
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303928/**
3929 * adm_set_softvolume -
3930 * command to set softvolume
3931 *
3932 * @port_id: Port ID number
3933 * @copp_idx: copp index assigned
3934 * @softvol_param: Params to set for softvolume
3935 *
3936 * Returns 0 on success or error on failure
3937 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303938int adm_set_softvolume(int port_id, int copp_idx,
3939 struct audproc_softvolume_params *softvol_param)
3940{
3941 struct audproc_soft_step_volume_params audproc_softvol;
3942 int sz = 0;
3943 int rc = 0;
3944 int port_idx;
3945
3946 pr_debug("%s: period %d step %d curve %d\n", __func__,
3947 softvol_param->period, softvol_param->step,
3948 softvol_param->rampingcurve);
3949
3950 port_id = afe_convert_virtual_to_portid(port_id);
3951 port_idx = adm_validate_and_get_port_index(port_id);
3952 if (port_idx < 0) {
3953 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
3954 rc = -EINVAL;
3955 goto fail_cmd;
3956 }
3957
3958 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3959 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3960 return -EINVAL;
3961 }
3962
3963 sz = sizeof(struct audproc_soft_step_volume_params);
3964
3965 audproc_softvol.params.hdr.hdr_field =
3966 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3967 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
3968 audproc_softvol.params.hdr.pkt_size = sz;
3969 audproc_softvol.params.hdr.src_svc = APR_SVC_ADM;
3970 audproc_softvol.params.hdr.src_domain = APR_DOMAIN_APPS;
3971 audproc_softvol.params.hdr.src_port = port_id;
3972 audproc_softvol.params.hdr.dest_svc = APR_SVC_ADM;
3973 audproc_softvol.params.hdr.dest_domain = APR_DOMAIN_ADSP;
3974 audproc_softvol.params.hdr.dest_port =
3975 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
3976 audproc_softvol.params.hdr.token = port_idx << 16 | copp_idx;
3977 audproc_softvol.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
3978 audproc_softvol.params.payload_addr_lsw = 0;
3979 audproc_softvol.params.payload_addr_msw = 0;
3980 audproc_softvol.params.mem_map_handle = 0;
3981 audproc_softvol.params.payload_size = sizeof(audproc_softvol) -
3982 sizeof(audproc_softvol.params);
3983 audproc_softvol.data.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
3984 audproc_softvol.data.param_id =
3985 AUDPROC_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
3986 audproc_softvol.data.param_size = audproc_softvol.params.payload_size -
3987 sizeof(audproc_softvol.data);
3988 audproc_softvol.data.reserved = 0;
3989 audproc_softvol.period = softvol_param->period;
3990 audproc_softvol.step = softvol_param->step;
3991 audproc_softvol.ramping_curve = softvol_param->rampingcurve;
3992
3993 pr_debug("%s: period %d, step %d, curve %d\n", __func__,
3994 audproc_softvol.period, audproc_softvol.step,
3995 audproc_softvol.ramping_curve);
3996
3997 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3998 rc = apr_send_pkt(this_adm.apr, (uint32_t *)&audproc_softvol);
3999 if (rc < 0) {
4000 pr_err("%s: Set params failed port = %#x\n",
4001 __func__, port_id);
4002 rc = -EINVAL;
4003 goto fail_cmd;
4004 }
4005 /* Wait for the callback */
4006 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
4007 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
4008 msecs_to_jiffies(TIMEOUT_MS));
4009 if (!rc) {
4010 pr_err("%s: Soft volume Set params timed out port = %#x\n",
4011 __func__, port_id);
4012 rc = -EINVAL;
4013 goto fail_cmd;
4014 } else if (atomic_read(&this_adm.copp.stat
4015 [port_idx][copp_idx]) > 0) {
4016 pr_err("%s: DSP returned error[%s]\n",
4017 __func__, adsp_err_get_err_str(
4018 atomic_read(&this_adm.copp.stat
4019 [port_idx][copp_idx])));
4020 rc = adsp_err_get_lnx_err_code(
4021 atomic_read(&this_adm.copp.stat
4022 [port_idx][copp_idx]));
4023 goto fail_cmd;
4024 }
4025 rc = 0;
4026fail_cmd:
4027 return rc;
4028}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304029EXPORT_SYMBOL(adm_set_softvolume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304030
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304031/**
4032 * adm_set_mic_gain -
4033 * command to set MIC gain
4034 *
4035 * @port_id: Port ID number
4036 * @copp_idx: copp index assigned
4037 * @volume: gain value to set
4038 *
4039 * Returns 0 on success or error on failure
4040 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304041int adm_set_mic_gain(int port_id, int copp_idx, int volume)
4042{
4043 struct adm_set_mic_gain_params mic_gain_params;
4044 int rc = 0;
4045 int sz, port_idx;
4046
4047 pr_debug("%s:\n", __func__);
4048 port_id = afe_convert_virtual_to_portid(port_id);
4049 port_idx = adm_validate_and_get_port_index(port_id);
4050 if (port_idx < 0) {
4051 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
4052 return -EINVAL;
4053 }
4054
4055 sz = sizeof(struct adm_set_mic_gain_params);
4056
4057 mic_gain_params.params.hdr.hdr_field =
4058 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4059 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
4060 mic_gain_params.params.hdr.pkt_size = sz;
4061 mic_gain_params.params.hdr.src_svc = APR_SVC_ADM;
4062 mic_gain_params.params.hdr.src_domain = APR_DOMAIN_APPS;
4063 mic_gain_params.params.hdr.src_port = port_id;
4064 mic_gain_params.params.hdr.dest_svc = APR_SVC_ADM;
4065 mic_gain_params.params.hdr.dest_domain = APR_DOMAIN_ADSP;
4066 mic_gain_params.params.hdr.dest_port =
4067 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
4068 mic_gain_params.params.hdr.token = port_idx << 16 | copp_idx;
4069 mic_gain_params.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
4070 mic_gain_params.params.payload_addr_lsw = 0;
4071 mic_gain_params.params.payload_addr_msw = 0;
4072 mic_gain_params.params.mem_map_handle = 0;
4073 mic_gain_params.params.payload_size =
4074 sizeof(struct adm_param_data_v5) +
4075 sizeof(struct admx_mic_gain);
4076 mic_gain_params.data.module_id = ADM_MODULE_IDX_MIC_GAIN_CTRL;
4077 mic_gain_params.data.param_id = ADM_PARAM_IDX_MIC_GAIN;
4078 mic_gain_params.data.param_size =
4079 sizeof(struct admx_mic_gain);
4080 mic_gain_params.data.reserved = 0;
4081 mic_gain_params.mic_gain_data.tx_mic_gain = volume;
4082 mic_gain_params.mic_gain_data.reserved = 0;
4083 pr_debug("%s: Mic Gain set to %d at port_id 0x%x\n",
4084 __func__, volume, port_id);
4085
4086 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
4087 rc = apr_send_pkt(this_adm.apr, (uint32_t *)&mic_gain_params);
4088 if (rc < 0) {
4089 pr_err("%s: Set params failed port = %#x\n",
4090 __func__, port_id);
4091 rc = -EINVAL;
4092 goto fail_cmd;
4093 }
4094 /* Wait for the callback */
4095 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
4096 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
4097 msecs_to_jiffies(TIMEOUT_MS));
4098 if (!rc) {
4099 pr_err("%s: Mic Gain Set params timed out port = %#x\n",
4100 __func__, port_id);
4101 rc = -EINVAL;
4102 goto fail_cmd;
4103 } else if (atomic_read(&this_adm.copp.stat
4104 [port_idx][copp_idx]) > 0) {
4105 pr_err("%s: DSP returned error[%s]\n",
4106 __func__, adsp_err_get_err_str(
4107 atomic_read(&this_adm.copp.stat
4108 [port_idx][copp_idx])));
4109 rc = adsp_err_get_lnx_err_code(
4110 atomic_read(&this_adm.copp.stat
4111 [port_idx][copp_idx]));
4112 goto fail_cmd;
4113 }
4114 rc = 0;
4115fail_cmd:
4116 return rc;
4117}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304118EXPORT_SYMBOL(adm_set_mic_gain);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304119
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304120/**
4121 * adm_send_set_multichannel_ec_primary_mic_ch -
4122 * command to set multi-ch EC primary mic
4123 *
4124 * @port_id: Port ID number
4125 * @copp_idx: copp index assigned
4126 * @primary_mic_ch: channel number of primary mic
4127 *
4128 * Returns 0 on success or error on failure
4129 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304130int adm_send_set_multichannel_ec_primary_mic_ch(int port_id, int copp_idx,
4131 int primary_mic_ch)
4132{
4133 struct adm_set_sec_primary_ch_params sec_primary_ch_params;
4134 int rc = 0;
4135 int sz, port_idx;
4136
4137 pr_debug("%s port_id 0x%x, copp_idx 0x%x, primary_mic_ch %d\n",
4138 __func__, port_id, copp_idx, primary_mic_ch);
4139 port_id = afe_convert_virtual_to_portid(port_id);
4140 port_idx = adm_validate_and_get_port_index(port_id);
4141 if (port_idx < 0) {
4142 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
4143 return -EINVAL;
4144 }
4145
4146 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4147 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
4148 return -EINVAL;
4149 }
4150
4151 sz = sizeof(struct adm_set_sec_primary_ch_params);
4152
4153 sec_primary_ch_params.params.hdr.hdr_field =
4154 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4155 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
4156 sec_primary_ch_params.params.hdr.pkt_size = sz;
4157 sec_primary_ch_params.params.hdr.src_svc = APR_SVC_ADM;
4158 sec_primary_ch_params.params.hdr.src_domain = APR_DOMAIN_APPS;
4159 sec_primary_ch_params.params.hdr.src_port = port_id;
4160 sec_primary_ch_params.params.hdr.dest_svc = APR_SVC_ADM;
4161 sec_primary_ch_params.params.hdr.dest_domain = APR_DOMAIN_ADSP;
4162 sec_primary_ch_params.params.hdr.dest_port =
4163 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
4164 sec_primary_ch_params.params.hdr.token = port_idx << 16 | copp_idx;
4165 sec_primary_ch_params.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
4166 sec_primary_ch_params.params.payload_addr_lsw = 0;
4167 sec_primary_ch_params.params.payload_addr_msw = 0;
4168 sec_primary_ch_params.params.mem_map_handle = 0;
4169 sec_primary_ch_params.params.payload_size =
4170 sizeof(struct adm_param_data_v5) +
4171 sizeof(struct admx_sec_primary_mic_ch);
4172 sec_primary_ch_params.data.module_id =
4173 AUDPROC_MODULE_ID_VOICE_TX_SECNS;
4174 sec_primary_ch_params.data.param_id =
4175 AUDPROC_PARAM_IDX_SEC_PRIMARY_MIC_CH;
4176 sec_primary_ch_params.data.param_size =
4177 sizeof(struct admx_sec_primary_mic_ch);
4178 sec_primary_ch_params.data.reserved = 0;
4179 sec_primary_ch_params.sec_primary_mic_ch_data.version = 0;
4180 sec_primary_ch_params.sec_primary_mic_ch_data.reserved = 0;
4181 sec_primary_ch_params.sec_primary_mic_ch_data.sec_primary_mic_ch =
4182 primary_mic_ch;
4183 sec_primary_ch_params.sec_primary_mic_ch_data.reserved1 = 0;
4184
4185 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
4186 rc = apr_send_pkt(this_adm.apr, (uint32_t *)&sec_primary_ch_params);
4187 if (rc < 0) {
4188 pr_err("%s: Set params failed port = %#x\n",
4189 __func__, port_id);
4190 rc = -EINVAL;
4191 goto fail_cmd;
4192 }
4193 /* Wait for the callback */
4194 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
4195 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
4196 msecs_to_jiffies(TIMEOUT_MS));
4197 if (!rc) {
4198 pr_err("%s: Mic Set params timed out port = %#x\n",
4199 __func__, port_id);
4200 rc = -EINVAL;
4201 goto fail_cmd;
4202 } else if (atomic_read(&this_adm.copp.stat
4203 [port_idx][copp_idx]) > 0) {
4204 pr_err("%s: DSP returned error[%s]\n",
4205 __func__, adsp_err_get_err_str(
4206 atomic_read(&this_adm.copp.stat
4207 [port_idx][copp_idx])));
4208 rc = adsp_err_get_lnx_err_code(
4209 atomic_read(&this_adm.copp.stat
4210 [port_idx][copp_idx]));
4211 goto fail_cmd;
4212 }
4213 rc = 0;
4214fail_cmd:
4215 return rc;
4216}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304217EXPORT_SYMBOL(adm_send_set_multichannel_ec_primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304218
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304219/**
4220 * adm_param_enable -
4221 * command to send params to ADM for given module
4222 *
4223 * @port_id: Port ID number
4224 * @copp_idx: copp index assigned
4225 * @module_id: ADM module
4226 * @enable: flag to enable or disable module
4227 *
4228 * Returns 0 on success or error on failure
4229 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304230int adm_param_enable(int port_id, int copp_idx, int module_id, int enable)
4231{
4232 struct audproc_enable_param_t adm_mod_enable;
4233 int sz = 0;
4234 int rc = 0;
4235 int port_idx;
4236
4237 pr_debug("%s port_id %d, module_id 0x%x, enable %d\n",
4238 __func__, port_id, module_id, enable);
4239 port_id = afe_convert_virtual_to_portid(port_id);
4240 port_idx = adm_validate_and_get_port_index(port_id);
4241 if (port_idx < 0) {
4242 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4243 rc = -EINVAL;
4244 goto fail_cmd;
4245 }
4246
4247 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4248 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4249 return -EINVAL;
4250 }
4251
4252 sz = sizeof(struct audproc_enable_param_t);
4253
4254 adm_mod_enable.pp_params.hdr.hdr_field =
4255 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4256 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
4257 adm_mod_enable.pp_params.hdr.pkt_size = sz;
4258 adm_mod_enable.pp_params.hdr.src_svc = APR_SVC_ADM;
4259 adm_mod_enable.pp_params.hdr.src_domain = APR_DOMAIN_APPS;
4260 adm_mod_enable.pp_params.hdr.src_port = port_id;
4261 adm_mod_enable.pp_params.hdr.dest_svc = APR_SVC_ADM;
4262 adm_mod_enable.pp_params.hdr.dest_domain = APR_DOMAIN_ADSP;
4263 adm_mod_enable.pp_params.hdr.dest_port =
4264 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
4265 adm_mod_enable.pp_params.hdr.token = port_idx << 16 | copp_idx;
4266 adm_mod_enable.pp_params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
4267 adm_mod_enable.pp_params.payload_addr_lsw = 0;
4268 adm_mod_enable.pp_params.payload_addr_msw = 0;
4269 adm_mod_enable.pp_params.mem_map_handle = 0;
4270 adm_mod_enable.pp_params.payload_size = sizeof(adm_mod_enable) -
4271 sizeof(adm_mod_enable.pp_params) +
4272 sizeof(adm_mod_enable.pp_params.params);
4273 adm_mod_enable.pp_params.params.module_id = module_id;
4274 adm_mod_enable.pp_params.params.param_id = AUDPROC_PARAM_ID_ENABLE;
4275 adm_mod_enable.pp_params.params.param_size =
4276 adm_mod_enable.pp_params.payload_size -
4277 sizeof(adm_mod_enable.pp_params.params);
4278 adm_mod_enable.pp_params.params.reserved = 0;
4279 adm_mod_enable.enable = enable;
4280
4281 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
4282
4283 rc = apr_send_pkt(this_adm.apr, (uint32_t *)&adm_mod_enable);
4284 if (rc < 0) {
4285 pr_err("%s: Set params failed port = %#x\n",
4286 __func__, port_id);
4287 rc = -EINVAL;
4288 goto fail_cmd;
4289 }
4290 /* Wait for the callback */
4291 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
4292 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
4293 msecs_to_jiffies(TIMEOUT_MS));
4294 if (!rc) {
4295 pr_err("%s: module %x enable %d timed out on port = %#x\n",
4296 __func__, module_id, enable, port_id);
4297 rc = -EINVAL;
4298 goto fail_cmd;
4299 } else if (atomic_read(&this_adm.copp.stat
4300 [port_idx][copp_idx]) > 0) {
4301 pr_err("%s: DSP returned error[%s]\n",
4302 __func__, adsp_err_get_err_str(
4303 atomic_read(&this_adm.copp.stat
4304 [port_idx][copp_idx])));
4305 rc = adsp_err_get_lnx_err_code(
4306 atomic_read(&this_adm.copp.stat
4307 [port_idx][copp_idx]));
4308 goto fail_cmd;
4309 }
4310 rc = 0;
4311fail_cmd:
4312 return rc;
4313
4314}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304315EXPORT_SYMBOL(adm_param_enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304316
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304317/**
4318 * adm_send_calibration -
4319 * send ADM calibration to DSP
4320 *
4321 * @port_id: Port ID number
4322 * @copp_idx: copp index assigned
4323 * @path: direction or ADM path type
4324 * @perf_mode: performance mode like LL/ULL/..
4325 * @cal_type: calibration type to use
4326 * @params: pointer with cal data
4327 * @size: cal size
4328 *
4329 * Returns 0 on success or error on failure
4330 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304331int adm_send_calibration(int port_id, int copp_idx, int path, int perf_mode,
4332 int cal_type, char *params, int size)
4333{
4334
4335 struct adm_cmd_set_pp_params_v5 *adm_params = NULL;
4336 int sz, rc = 0;
4337 int port_idx;
4338
4339 pr_debug("%s:port_id %d, path %d, perf_mode %d, cal_type %d, size %d\n",
4340 __func__, port_id, path, perf_mode, cal_type, size);
4341
4342 port_id = afe_convert_virtual_to_portid(port_id);
4343 port_idx = adm_validate_and_get_port_index(port_id);
4344 if (port_idx < 0) {
4345 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4346 rc = -EINVAL;
4347 goto end;
4348 }
4349
4350 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4351 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4352 return -EINVAL;
4353 }
4354
4355 /* Maps audio_dev_ctrl path definition to ACDB definition */
4356 if (get_cal_path(path) != RX_DEVICE) {
4357 pr_err("%s: acdb_path %d\n", __func__, path);
4358 rc = -EINVAL;
4359 goto end;
4360 }
4361
4362 sz = sizeof(struct adm_cmd_set_pp_params_v5) + size;
4363 adm_params = kzalloc(sz, GFP_KERNEL);
4364 if (!adm_params) {
4365 pr_err("%s, adm params memory alloc failed", __func__);
4366 rc = -ENOMEM;
4367 goto end;
4368 }
4369
4370 memcpy(((u8 *)adm_params + sizeof(struct adm_cmd_set_pp_params_v5)),
4371 params, size);
4372
4373 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4374 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
4375 adm_params->hdr.pkt_size = sz;
4376 adm_params->hdr.src_svc = APR_SVC_ADM;
4377 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
4378 adm_params->hdr.src_port = port_id;
4379 adm_params->hdr.dest_svc = APR_SVC_ADM;
4380 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
4381 adm_params->hdr.dest_port =
4382 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
4383 adm_params->hdr.token = port_idx << 16 | copp_idx;
4384 adm_params->hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
4385 /* payload address and mmap handle initialized to zero by kzalloc */
4386 adm_params->payload_size = size;
4387
4388 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
4389 rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
4390 if (rc < 0) {
4391 pr_err("%s: Set params failed port = %#x\n",
4392 __func__, port_id);
4393 rc = -EINVAL;
4394 goto end;
4395 }
4396 /* Wait for the callback */
4397 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
4398 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
4399 msecs_to_jiffies(TIMEOUT_MS));
4400 if (!rc) {
4401 pr_err("%s: Set params timed out port = %#x\n",
4402 __func__, port_id);
4403 rc = -EINVAL;
4404 goto end;
4405 } else if (atomic_read(&this_adm.copp.stat
4406 [port_idx][copp_idx]) > 0) {
4407 pr_err("%s: DSP returned error[%s]\n",
4408 __func__, adsp_err_get_err_str(
4409 atomic_read(&this_adm.copp.stat
4410 [port_idx][copp_idx])));
4411 rc = adsp_err_get_lnx_err_code(
4412 atomic_read(&this_adm.copp.stat
4413 [port_idx][copp_idx]));
4414 goto end;
4415 }
4416 rc = 0;
4417
4418end:
4419 kfree(adm_params);
4420 return rc;
4421}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304422EXPORT_SYMBOL(adm_send_calibration);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304423
4424/*
4425 * adm_update_wait_parameters must be called with routing driver locks.
4426 * adm_reset_wait_parameters must be called with routing driver locks.
4427 * set and reset parmeters are separated to make sure it is always called
4428 * under routing driver lock.
4429 * adm_wait_timeout is to block until timeout or interrupted. Timeout is
4430 * not a an error.
4431 */
4432int adm_set_wait_parameters(int port_id, int copp_idx)
4433{
4434
4435 int ret = 0, port_idx;
4436
4437 pr_debug("%s: port_id 0x%x, copp_idx %d\n", __func__, port_id,
4438 copp_idx);
4439 port_id = afe_convert_virtual_to_portid(port_id);
4440 port_idx = adm_validate_and_get_port_index(port_id);
4441 if (port_idx < 0) {
4442 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4443 ret = -EINVAL;
4444 goto end;
4445 }
4446
4447 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4448 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4449 return -EINVAL;
4450 }
4451
4452 this_adm.copp.adm_delay[port_idx][copp_idx] = 1;
4453 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 0);
4454
4455end:
4456 return ret;
4457
4458}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304459EXPORT_SYMBOL(adm_set_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304460
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304461/**
4462 * adm_reset_wait_parameters -
4463 * reset wait parameters or ADM delay value
4464 *
4465 * @port_id: Port ID number
4466 * @copp_idx: copp index assigned
4467 *
4468 * Returns 0 on success or error on failure
4469 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304470int adm_reset_wait_parameters(int port_id, int copp_idx)
4471{
4472 int ret = 0, port_idx;
4473
4474 pr_debug("%s: port_id 0x%x copp_idx %d\n", __func__, port_id,
4475 copp_idx);
4476 port_id = afe_convert_virtual_to_portid(port_id);
4477 port_idx = adm_validate_and_get_port_index(port_id);
4478 if (port_idx < 0) {
4479 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4480 ret = -EINVAL;
4481 goto end;
4482 }
4483
4484 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4485 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4486 return -EINVAL;
4487 }
4488
4489 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 1);
4490 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
4491
4492end:
4493 return ret;
4494}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304495EXPORT_SYMBOL(adm_reset_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304496
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304497/**
4498 * adm_wait_timeout -
4499 * ADM wait command after command send to DSP
4500 *
4501 * @port_id: Port ID number
4502 * @copp_idx: copp index assigned
4503 * @wait_time: value in ms for command timeout
4504 *
4505 * Returns 0 on success or error on failure
4506 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304507int adm_wait_timeout(int port_id, int copp_idx, int wait_time)
4508{
4509 int ret = 0, port_idx;
4510
4511 pr_debug("%s: port_id 0x%x, copp_idx %d, wait_time %d\n", __func__,
4512 port_id, copp_idx, wait_time);
4513 port_id = afe_convert_virtual_to_portid(port_id);
4514 port_idx = adm_validate_and_get_port_index(port_id);
4515 if (port_idx < 0) {
4516 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4517 ret = -EINVAL;
4518 goto end;
4519 }
4520
4521 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4522 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4523 return -EINVAL;
4524 }
4525
4526 ret = wait_event_timeout(
4527 this_adm.copp.adm_delay_wait[port_idx][copp_idx],
4528 atomic_read(&this_adm.copp.adm_delay_stat[port_idx][copp_idx]),
4529 msecs_to_jiffies(wait_time));
4530 pr_debug("%s: return %d\n", __func__, ret);
4531 if (ret != 0)
4532 ret = -EINTR;
4533end:
4534 pr_debug("%s: return %d--\n", __func__, ret);
4535 return ret;
4536}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304537EXPORT_SYMBOL(adm_wait_timeout);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304538
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304539/**
4540 * adm_store_cal_data -
4541 * Retrieve calibration data for ADM copp device
4542 *
4543 * @port_id: Port ID number
4544 * @copp_idx: copp index assigned
4545 * @path: direction or copp type
4546 * @perf_mode: performance mode like LL/ULL/..
4547 * @cal_index: calibration index to use
4548 * @params: pointer to store cal data
4549 * @size: pointer to fill with cal size
4550 *
4551 * Returns 0 on success or error on failure
4552 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304553int adm_store_cal_data(int port_id, int copp_idx, int path, int perf_mode,
4554 int cal_index, char *params, int *size)
4555{
4556 int rc = 0;
4557 struct cal_block_data *cal_block = NULL;
4558 int app_type, acdb_id, port_idx, sample_rate;
4559
4560 if (this_adm.cal_data[cal_index] == NULL) {
4561 pr_debug("%s: cal_index %d not allocated!\n",
4562 __func__, cal_index);
4563 goto end;
4564 }
4565
4566 if (get_cal_path(path) != RX_DEVICE) {
4567 pr_debug("%s: Invalid path to store calibration %d\n",
4568 __func__, path);
4569 rc = -EINVAL;
4570 goto end;
4571 }
4572
4573 port_id = afe_convert_virtual_to_portid(port_id);
4574 port_idx = adm_validate_and_get_port_index(port_id);
4575 if (port_idx < 0) {
4576 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
4577 rc = -EINVAL;
4578 goto end;
4579 }
4580
4581 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4582 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4583 return -EINVAL;
4584 }
4585
4586 acdb_id = atomic_read(&this_adm.copp.acdb_id[port_idx][copp_idx]);
4587 app_type = atomic_read(&this_adm.copp.app_type[port_idx][copp_idx]);
4588 sample_rate = atomic_read(&this_adm.copp.rate[port_idx][copp_idx]);
4589
4590 mutex_lock(&this_adm.cal_data[cal_index]->lock);
4591 cal_block = adm_find_cal(cal_index, get_cal_path(path), app_type,
4592 acdb_id, sample_rate);
4593 if (cal_block == NULL)
4594 goto unlock;
4595
4596 if (cal_block->cal_data.size <= 0) {
4597 pr_debug("%s: No ADM cal send for port_id = 0x%x!\n",
4598 __func__, port_id);
4599 rc = -EINVAL;
4600 goto unlock;
4601 }
4602
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05304603 if (cal_index == ADM_AUDPROC_CAL || cal_index == ADM_LSM_AUDPROC_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304604 if (cal_block->cal_data.size > AUD_PROC_BLOCK_SIZE) {
4605 pr_err("%s:audproc:invalid size exp/actual[%zd, %d]\n",
4606 __func__, cal_block->cal_data.size, *size);
4607 rc = -ENOMEM;
4608 goto unlock;
4609 }
Bhalchandra Gajareface2762018-05-10 14:16:49 -07004610 } else if (cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
4611 if (cal_block->cal_data.size > AUD_PROC_PERSIST_BLOCK_SIZE) {
4612 pr_err("%s:persist invalid size exp/actual[%zd, %d]\n",
4613 __func__, cal_block->cal_data.size, *size);
4614 rc = -ENOMEM;
4615 goto unlock;
4616 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304617 } else if (cal_index == ADM_AUDVOL_CAL) {
4618 if (cal_block->cal_data.size > AUD_VOL_BLOCK_SIZE) {
4619 pr_err("%s:aud_vol:invalid size exp/actual[%zd, %d]\n",
4620 __func__, cal_block->cal_data.size, *size);
4621 rc = -ENOMEM;
4622 goto unlock;
4623 }
4624 } else {
4625 pr_debug("%s: Not valid calibration for dolby topolgy\n",
4626 __func__);
4627 rc = -EINVAL;
4628 goto unlock;
4629 }
4630 memcpy(params, cal_block->cal_data.kvaddr, cal_block->cal_data.size);
4631 *size = cal_block->cal_data.size;
4632
4633 pr_debug("%s:port_id %d, copp_idx %d, path %d",
4634 __func__, port_id, copp_idx, path);
4635 pr_debug("perf_mode %d, cal_type %d, size %d\n",
4636 perf_mode, cal_index, *size);
4637
4638unlock:
4639 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
4640end:
4641 return rc;
4642}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304643EXPORT_SYMBOL(adm_store_cal_data);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304644
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304645/**
4646 * adm_send_compressed_device_mute -
4647 * command to send mute for compressed device
4648 *
4649 * @port_id: Port ID number
4650 * @copp_idx: copp index assigned
4651 * @mute_on: flag to indicate mute or unmute
4652 *
4653 * Returns 0 on success or error on failure
4654 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304655int adm_send_compressed_device_mute(int port_id, int copp_idx, bool mute_on)
4656{
4657 struct adm_set_compressed_device_mute mute_params;
4658 int ret = 0;
4659 int port_idx;
4660
4661 pr_debug("%s port_id: 0x%x, copp_idx %d, mute_on: %d\n",
4662 __func__, port_id, copp_idx, mute_on);
4663 port_id = afe_convert_virtual_to_portid(port_id);
4664 port_idx = adm_validate_and_get_port_index(port_id);
4665 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
4666 pr_err("%s: Invalid port_id %#x copp_idx %d\n",
4667 __func__, port_id, copp_idx);
4668 ret = -EINVAL;
4669 goto end;
4670 }
4671
4672 mute_params.command.hdr.hdr_field =
4673 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4674 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
4675 mute_params.command.hdr.pkt_size =
4676 sizeof(struct adm_set_compressed_device_mute);
4677 mute_params.command.hdr.src_svc = APR_SVC_ADM;
4678 mute_params.command.hdr.src_domain = APR_DOMAIN_APPS;
4679 mute_params.command.hdr.src_port = port_id;
4680 mute_params.command.hdr.dest_svc = APR_SVC_ADM;
4681 mute_params.command.hdr.dest_domain = APR_DOMAIN_ADSP;
4682 mute_params.command.hdr.dest_port =
4683 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
4684 mute_params.command.hdr.token = port_idx << 16 | copp_idx;
4685 mute_params.command.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
4686 mute_params.command.payload_addr_lsw = 0;
4687 mute_params.command.payload_addr_msw = 0;
4688 mute_params.command.mem_map_handle = 0;
4689 mute_params.command.payload_size = sizeof(mute_params) -
4690 sizeof(mute_params.command);
4691 mute_params.params.module_id = AUDPROC_MODULE_ID_COMPRESSED_MUTE;
4692 mute_params.params.param_id = AUDPROC_PARAM_ID_COMPRESSED_MUTE;
4693 mute_params.params.param_size = mute_params.command.payload_size -
4694 sizeof(mute_params.params);
4695 mute_params.params.reserved = 0;
4696 mute_params.mute_on = mute_on;
4697
4698 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
4699 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&mute_params);
4700 if (ret < 0) {
4701 pr_err("%s: device mute for port %d copp %d failed, ret %d\n",
4702 __func__, port_id, copp_idx, ret);
4703 ret = -EINVAL;
4704 goto end;
4705 }
4706
4707 /* Wait for the callback */
4708 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
4709 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
4710 msecs_to_jiffies(TIMEOUT_MS));
4711 if (!ret) {
4712 pr_err("%s: send device mute for port %d copp %d failed\n",
4713 __func__, port_id, copp_idx);
4714 ret = -EINVAL;
4715 goto end;
4716 } else if (atomic_read(&this_adm.copp.stat
4717 [port_idx][copp_idx]) > 0) {
4718 pr_err("%s: DSP returned error[%s]\n",
4719 __func__, adsp_err_get_err_str(
4720 atomic_read(&this_adm.copp.stat
4721 [port_idx][copp_idx])));
4722 ret = adsp_err_get_lnx_err_code(
4723 atomic_read(&this_adm.copp.stat
4724 [port_idx][copp_idx]));
4725 goto end;
4726 }
4727 ret = 0;
4728end:
4729 return ret;
4730}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304731EXPORT_SYMBOL(adm_send_compressed_device_mute);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304732
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304733/**
4734 * adm_send_compressed_device_latency -
4735 * command to send latency for compressed device
4736 *
4737 * @port_id: Port ID number
4738 * @copp_idx: copp index assigned
4739 * @latency: latency value to pass
4740 *
4741 * Returns 0 on success or error on failure
4742 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304743int adm_send_compressed_device_latency(int port_id, int copp_idx, int latency)
4744{
4745 struct adm_set_compressed_device_latency latency_params;
4746 int port_idx;
4747 int ret = 0;
4748
4749 pr_debug("%s port_id: 0x%x, copp_idx %d latency: %d\n", __func__,
4750 port_id, copp_idx, latency);
4751 port_id = afe_convert_virtual_to_portid(port_id);
4752 port_idx = adm_validate_and_get_port_index(port_id);
4753 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
4754 pr_err("%s: Invalid port_id %#x copp_idx %d\n",
4755 __func__, port_id, copp_idx);
4756 ret = -EINVAL;
4757 goto end;
4758 }
4759
4760 latency_params.command.hdr.hdr_field =
4761 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4762 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
4763 latency_params.command.hdr.pkt_size =
4764 sizeof(struct adm_set_compressed_device_latency);
4765 latency_params.command.hdr.src_svc = APR_SVC_ADM;
4766 latency_params.command.hdr.src_domain = APR_DOMAIN_APPS;
4767 latency_params.command.hdr.src_port = port_id;
4768 latency_params.command.hdr.dest_svc = APR_SVC_ADM;
4769 latency_params.command.hdr.dest_domain = APR_DOMAIN_ADSP;
4770 latency_params.command.hdr.dest_port =
4771 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
4772 latency_params.command.hdr.token = port_idx << 16 | copp_idx;
4773 latency_params.command.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
4774 latency_params.command.payload_addr_lsw = 0;
4775 latency_params.command.payload_addr_msw = 0;
4776 latency_params.command.mem_map_handle = 0;
4777 latency_params.command.payload_size = sizeof(latency_params) -
4778 sizeof(latency_params.command);
4779 latency_params.params.module_id = AUDPROC_MODULE_ID_COMPRESSED_LATENCY;
4780 latency_params.params.param_id = AUDPROC_PARAM_ID_COMPRESSED_LATENCY;
4781 latency_params.params.param_size = latency_params.command.payload_size -
4782 sizeof(latency_params.params);
4783 latency_params.params.reserved = 0;
4784 latency_params.latency = latency;
4785
4786 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
4787 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&latency_params);
4788 if (ret < 0) {
4789 pr_err("%s: send device latency err %d for port %d copp %d\n",
4790 __func__, port_id, copp_idx, ret);
4791 ret = -EINVAL;
4792 goto end;
4793 }
4794
4795 /* Wait for the callback */
4796 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
4797 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
4798 msecs_to_jiffies(TIMEOUT_MS));
4799 if (!ret) {
4800 pr_err("%s: send device latency for port %d failed\n", __func__,
4801 port_id);
4802 ret = -EINVAL;
4803 goto end;
4804 } else if (atomic_read(&this_adm.copp.stat
4805 [port_idx][copp_idx]) > 0) {
4806 pr_err("%s: DSP returned error[%s]\n",
4807 __func__, adsp_err_get_err_str(
4808 atomic_read(&this_adm.copp.stat
4809 [port_idx][copp_idx])));
4810 ret = adsp_err_get_lnx_err_code(
4811 atomic_read(&this_adm.copp.stat
4812 [port_idx][copp_idx]));
4813 goto end;
4814 }
4815 ret = 0;
4816end:
4817 return ret;
4818}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304819EXPORT_SYMBOL(adm_send_compressed_device_latency);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304820
4821/**
4822 * adm_swap_speaker_channels
4823 *
4824 * Receives port_id, copp_idx, sample rate, spk_swap and
4825 * send MFC command to swap speaker channel.
4826 * Return zero on success. On failure returns nonzero.
4827 *
4828 * port_id - Passed value, port_id for which channels swap is wanted
4829 * copp_idx - Passed value, copp_idx for which channels swap is wanted
4830 * sample_rate - Passed value, sample rate used by app type config
4831 * spk_swap - Passed value, spk_swap for check if swap flag is set
4832 */
4833int adm_swap_speaker_channels(int port_id, int copp_idx,
4834 int sample_rate, bool spk_swap)
4835{
4836 struct audproc_mfc_output_media_fmt mfc_cfg;
4837 uint16_t num_channels;
4838 int port_idx;
4839 int ret = 0;
4840
4841 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4842 __func__, port_id, copp_idx);
4843 port_id = q6audio_convert_virtual_to_portid(port_id);
4844 port_idx = adm_validate_and_get_port_index(port_id);
4845 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
4846 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4847 ret = -EINVAL;
4848 goto done;
4849 }
4850
4851 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4852 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4853 ret = -EINVAL;
4854 goto done;
4855 }
4856
4857 num_channels = atomic_read(
4858 &this_adm.copp.channels[port_idx][copp_idx]);
4859 if (num_channels != 2) {
4860 pr_debug("%s: Invalid number of channels: %d\n",
4861 __func__, num_channels);
4862 ret = -EINVAL;
4863 goto done;
4864 }
4865
4866 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
4867 mfc_cfg.params.hdr.hdr_field =
4868 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4869 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
4870 mfc_cfg.params.hdr.pkt_size =
4871 sizeof(mfc_cfg);
4872 mfc_cfg.params.hdr.src_svc = APR_SVC_ADM;
4873 mfc_cfg.params.hdr.src_domain = APR_DOMAIN_APPS;
4874 mfc_cfg.params.hdr.src_port = port_id;
4875 mfc_cfg.params.hdr.dest_svc = APR_SVC_ADM;
4876 mfc_cfg.params.hdr.dest_domain = APR_DOMAIN_ADSP;
4877 mfc_cfg.params.hdr.dest_port =
4878 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
4879 mfc_cfg.params.hdr.token = port_idx << 16 | copp_idx;
4880 mfc_cfg.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
4881 mfc_cfg.params.payload_addr_lsw = 0;
4882 mfc_cfg.params.payload_addr_msw = 0;
4883 mfc_cfg.params.mem_map_handle = 0;
4884 mfc_cfg.params.payload_size = sizeof(mfc_cfg) -
4885 sizeof(mfc_cfg.params);
4886 mfc_cfg.data.module_id = AUDPROC_MODULE_ID_MFC;
4887 mfc_cfg.data.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
4888 mfc_cfg.data.param_size = mfc_cfg.params.payload_size -
4889 sizeof(mfc_cfg.data);
4890 mfc_cfg.data.reserved = 0;
4891 mfc_cfg.sampling_rate = sample_rate;
4892 mfc_cfg.bits_per_sample =
4893 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
4894 mfc_cfg.num_channels = num_channels;
4895
4896 /* Currently applying speaker swap for only 2 channel use case */
4897 if (spk_swap) {
4898 mfc_cfg.channel_type[0] =
4899 (uint16_t) PCM_CHANNEL_FR;
4900 mfc_cfg.channel_type[1] =
4901 (uint16_t) PCM_CHANNEL_FL;
4902 } else {
4903 mfc_cfg.channel_type[0] =
4904 (uint16_t) PCM_CHANNEL_FL;
4905 mfc_cfg.channel_type[1] =
4906 (uint16_t) PCM_CHANNEL_FR;
4907 }
4908
4909 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
4910 pr_debug("%s: mfc config: port_idx %d copp_idx %d copp SR %d copp BW %d copp chan %d\n",
4911 __func__, port_idx, copp_idx, mfc_cfg.sampling_rate,
4912 mfc_cfg.bits_per_sample, mfc_cfg.num_channels);
4913
4914 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&mfc_cfg);
4915 if (ret < 0) {
4916 pr_err("%s: port_id: for[0x%x] failed %d\n",
4917 __func__, port_id, ret);
4918 goto done;
4919 }
4920 /* Wait for the callback with copp id */
4921 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
4922 atomic_read(&this_adm.copp.stat
4923 [port_idx][copp_idx]) >= 0,
4924 msecs_to_jiffies(TIMEOUT_MS));
4925 if (!ret) {
4926 pr_err("%s: mfc_cfg Set params timed out for port_id: for [0x%x]\n",
4927 __func__, port_id);
4928 ret = -ETIMEDOUT;
4929 goto done;
4930 }
4931
4932 if (atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) > 0) {
4933 pr_err("%s: DSP returned error[%s]\n",
4934 __func__, adsp_err_get_err_str(
4935 atomic_read(&this_adm.copp.stat
4936 [port_idx][copp_idx])));
4937 ret = adsp_err_get_lnx_err_code(
4938 atomic_read(&this_adm.copp.stat
4939 [port_idx][copp_idx]));
4940 goto done;
4941 }
4942
4943 pr_debug("%s: mfc_cfg Set params returned success", __func__);
4944 ret = 0;
4945
4946done:
4947 return ret;
4948}
4949EXPORT_SYMBOL(adm_swap_speaker_channels);
4950
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304951/**
4952 * adm_set_sound_focus -
4953 * Update sound focus info
4954 *
4955 * @port_id: Port ID number
4956 * @copp_idx: copp index assigned
4957 * @soundFocusData: sound focus data to pass
4958 *
4959 * Returns 0 on success or error on failure
4960 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304961int adm_set_sound_focus(int port_id, int copp_idx,
4962 struct sound_focus_param soundFocusData)
4963{
4964 struct adm_set_fluence_soundfocus_param soundfocus_params;
4965 int sz = 0;
4966 int ret = 0;
4967 int port_idx;
4968 int i;
4969
4970 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4971 __func__, port_id, copp_idx);
4972
4973 port_id = afe_convert_virtual_to_portid(port_id);
4974 port_idx = adm_validate_and_get_port_index(port_id);
4975 if (port_idx < 0) {
4976 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4977
4978 ret = -EINVAL;
4979 goto done;
4980 }
4981
4982 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4983 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4984
4985 ret = -EINVAL;
4986 goto done;
4987 }
4988
4989 sz = sizeof(struct adm_set_fluence_soundfocus_param);
4990 soundfocus_params.params.hdr.hdr_field =
4991 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
4992 APR_PKT_VER);
4993 soundfocus_params.params.hdr.pkt_size = sz;
4994 soundfocus_params.params.hdr.src_svc = APR_SVC_ADM;
4995 soundfocus_params.params.hdr.src_domain = APR_DOMAIN_APPS;
4996 soundfocus_params.params.hdr.src_port = port_id;
4997 soundfocus_params.params.hdr.dest_svc = APR_SVC_ADM;
4998 soundfocus_params.params.hdr.dest_domain = APR_DOMAIN_ADSP;
4999 soundfocus_params.params.hdr.dest_port =
5000 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
5001 soundfocus_params.params.hdr.token = port_idx << 16 |
5002 ADM_CLIENT_ID_SOURCE_TRACKING << 8 | copp_idx;
5003 soundfocus_params.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
5004 soundfocus_params.params.payload_addr_lsw = 0;
5005 soundfocus_params.params.payload_addr_msw = 0;
5006 soundfocus_params.params.mem_map_handle = 0;
5007 soundfocus_params.params.payload_size = sizeof(soundfocus_params) -
5008 sizeof(soundfocus_params.params);
5009 soundfocus_params.data.module_id = VOICEPROC_MODULE_ID_GENERIC_TX;
5010 soundfocus_params.data.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
5011 soundfocus_params.data.param_size =
5012 soundfocus_params.params.payload_size -
5013 sizeof(soundfocus_params.data);
5014 soundfocus_params.data.reserved = 0;
5015
5016 memset(&(soundfocus_params.soundfocus_data), 0xFF,
5017 sizeof(struct adm_param_fluence_soundfocus_t));
5018 for (i = 0; i < MAX_SECTORS; i++) {
5019 soundfocus_params.soundfocus_data.start_angles[i] =
5020 soundFocusData.start_angle[i];
5021 soundfocus_params.soundfocus_data.enables[i] =
5022 soundFocusData.enable[i];
5023 pr_debug("%s: start_angle[%d] = %d\n",
5024 __func__, i, soundFocusData.start_angle[i]);
5025 pr_debug("%s: enable[%d] = %d\n",
5026 __func__, i, soundFocusData.enable[i]);
5027 }
5028 soundfocus_params.soundfocus_data.gain_step =
5029 soundFocusData.gain_step;
5030 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData.gain_step);
5031
5032 soundfocus_params.soundfocus_data.reserved = 0;
5033
5034 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
5035 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&soundfocus_params);
5036 if (ret < 0) {
5037 pr_err("%s: Set params failed\n", __func__);
5038
5039 ret = -EINVAL;
5040 goto done;
5041 }
5042 /* Wait for the callback */
5043 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
5044 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
5045 msecs_to_jiffies(TIMEOUT_MS));
5046 if (!ret) {
5047 pr_err("%s: Set params timed out\n", __func__);
5048
5049 ret = -EINVAL;
5050 goto done;
5051 }
5052
5053 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
5054 pr_err("%s - set params returned error [%s]\n",
5055 __func__, adsp_err_get_err_str(
5056 this_adm.sourceTrackingData.apr_cmd_status));
5057
5058 ret = adsp_err_get_lnx_err_code(
5059 this_adm.sourceTrackingData.apr_cmd_status);
5060 goto done;
5061 }
5062
5063 ret = 0;
5064
5065done:
5066 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
5067
5068 return ret;
5069}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305070EXPORT_SYMBOL(adm_set_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305071
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305072/**
5073 * adm_get_sound_focus -
5074 * Retrieve sound focus info
5075 *
5076 * @port_id: Port ID number
5077 * @copp_idx: copp index assigned
5078 * @soundFocusData: pointer for sound focus data to be updated with
5079 *
5080 * Returns 0 on success or error on failure
5081 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305082int adm_get_sound_focus(int port_id, int copp_idx,
5083 struct sound_focus_param *soundFocusData)
5084{
5085 int ret = 0, i;
5086 char *params_value;
5087 uint32_t param_payload_len = sizeof(struct adm_param_data_v5) +
5088 sizeof(struct adm_param_fluence_soundfocus_t);
5089 struct adm_param_fluence_soundfocus_t *soundfocus_params;
5090
5091 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
5092 __func__, port_id, copp_idx);
5093
5094 params_value = kzalloc(param_payload_len, GFP_KERNEL);
5095 if (!params_value) {
5096 ret = -ENOMEM;
5097 goto done;
5098 }
5099 ret = adm_get_params_v2(port_id, copp_idx,
5100 VOICEPROC_MODULE_ID_GENERIC_TX,
5101 VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS,
5102 param_payload_len,
5103 params_value,
5104 ADM_CLIENT_ID_SOURCE_TRACKING);
5105 if (ret) {
5106 pr_err("%s: get parameters failed ret:%d\n", __func__, ret);
5107
5108 kfree(params_value);
5109 ret = -EINVAL;
5110 goto done;
5111 }
5112
5113 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
5114 pr_err("%s - get params returned error [%s]\n",
5115 __func__, adsp_err_get_err_str(
5116 this_adm.sourceTrackingData.apr_cmd_status));
5117
5118 kfree(params_value);
5119 ret = adsp_err_get_lnx_err_code(
5120 this_adm.sourceTrackingData.apr_cmd_status);
5121 goto done;
5122 }
5123
5124 soundfocus_params = (struct adm_param_fluence_soundfocus_t *)
5125 params_value;
5126 for (i = 0; i < MAX_SECTORS; i++) {
5127 soundFocusData->start_angle[i] =
5128 soundfocus_params->start_angles[i];
5129 soundFocusData->enable[i] = soundfocus_params->enables[i];
5130 pr_debug("%s: start_angle[%d] = %d\n",
5131 __func__, i, soundFocusData->start_angle[i]);
5132 pr_debug("%s: enable[%d] = %d\n",
5133 __func__, i, soundFocusData->enable[i]);
5134 }
5135 soundFocusData->gain_step = soundfocus_params->gain_step;
5136 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData->gain_step);
5137
5138 kfree(params_value);
5139
5140done:
5141 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
5142
5143 return ret;
5144}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305145EXPORT_SYMBOL(adm_get_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305146
5147static int adm_source_tracking_alloc_map_memory(void)
5148{
5149 int ret;
5150
5151 pr_debug("%s: Enter\n", __func__);
5152
5153 ret = msm_audio_ion_alloc("SOURCE_TRACKING",
5154 &this_adm.sourceTrackingData.ion_client,
5155 &this_adm.sourceTrackingData.ion_handle,
5156 AUD_PROC_BLOCK_SIZE,
5157 &this_adm.sourceTrackingData.memmap.paddr,
5158 &this_adm.sourceTrackingData.memmap.size,
5159 &this_adm.sourceTrackingData.memmap.kvaddr);
5160 if (ret) {
5161 pr_err("%s: failed to allocate memory\n", __func__);
5162
5163 ret = -EINVAL;
5164 goto done;
5165 }
5166
5167 atomic_set(&this_adm.mem_map_index, ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
5168 ret = adm_memory_map_regions(&this_adm.sourceTrackingData.memmap.paddr,
5169 0,
5170 (uint32_t *)&this_adm.sourceTrackingData.memmap.size,
5171 1);
5172 if (ret < 0) {
5173 pr_err("%s: failed to map memory, paddr = 0x%pK, size = %d\n",
5174 __func__,
5175 (void *)this_adm.sourceTrackingData.memmap.paddr,
5176 (uint32_t)this_adm.sourceTrackingData.memmap.size);
5177
5178 msm_audio_ion_free(this_adm.sourceTrackingData.ion_client,
5179 this_adm.sourceTrackingData.ion_handle);
5180 this_adm.sourceTrackingData.ion_client = NULL;
5181 this_adm.sourceTrackingData.ion_handle = NULL;
5182 this_adm.sourceTrackingData.memmap.size = 0;
5183 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
5184 this_adm.sourceTrackingData.memmap.paddr = 0;
5185 this_adm.sourceTrackingData.apr_cmd_status = -1;
5186 atomic_set(&this_adm.mem_map_handles
5187 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
5188
5189 ret = -EINVAL;
5190 goto done;
5191 }
5192 ret = 0;
5193 pr_debug("%s: paddr = 0x%pK, size = %d, mem_map_handle = 0x%x\n",
5194 __func__, (void *)this_adm.sourceTrackingData.memmap.paddr,
5195 (uint32_t)this_adm.sourceTrackingData.memmap.size,
5196 atomic_read(&this_adm.mem_map_handles
5197 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING]));
5198
5199done:
5200 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
5201
5202 return ret;
5203}
5204
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305205/**
5206 * adm_get_source_tracking -
5207 * Retrieve source tracking info
5208 *
5209 * @port_id: Port ID number
5210 * @copp_idx: copp index assigned
5211 * @sourceTrackingData: pointer for source track data to be updated with
5212 *
5213 * Returns 0 on success or error on failure
5214 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305215int adm_get_source_tracking(int port_id, int copp_idx,
5216 struct source_tracking_param *sourceTrackingData)
5217{
5218 struct adm_cmd_get_pp_params_v5 admp;
5219 int p_idx, ret = 0, i;
5220 struct adm_param_fluence_sourcetracking_t *source_tracking_params;
5221
5222 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
5223 __func__, port_id, copp_idx);
5224
5225 if (!this_adm.sourceTrackingData.memmap.paddr) {
5226 /* Allocate and map shared memory for out of band usage */
5227 ret = adm_source_tracking_alloc_map_memory();
5228 if (ret != 0) {
5229 ret = -EINVAL;
5230 goto done;
5231 }
5232 }
5233
5234 port_id = afe_convert_virtual_to_portid(port_id);
5235 p_idx = adm_validate_and_get_port_index(port_id);
5236 if (p_idx < 0) {
5237 pr_err("%s - invalid port index %i, port id %i, copp idx %i\n",
5238 __func__, p_idx, port_id, copp_idx);
5239
5240 ret = -EINVAL;
5241 goto done;
5242 }
5243
5244 admp.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
5245 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
5246 admp.hdr.pkt_size = sizeof(admp);
5247 admp.hdr.src_svc = APR_SVC_ADM;
5248 admp.hdr.src_domain = APR_DOMAIN_APPS;
5249 admp.hdr.src_port = port_id;
5250 admp.hdr.dest_svc = APR_SVC_ADM;
5251 admp.hdr.dest_domain = APR_DOMAIN_ADSP;
5252 admp.hdr.dest_port = atomic_read(&this_adm.copp.id[p_idx][copp_idx]);
5253 admp.hdr.token = p_idx << 16 | ADM_CLIENT_ID_SOURCE_TRACKING << 8 |
5254 copp_idx;
5255 admp.hdr.opcode = ADM_CMD_GET_PP_PARAMS_V5;
5256 admp.data_payload_addr_lsw =
5257 lower_32_bits(this_adm.sourceTrackingData.memmap.paddr);
5258 admp.data_payload_addr_msw =
5259 msm_audio_populate_upper_32_bits(
5260 this_adm.sourceTrackingData.memmap.paddr);
5261 admp.mem_map_handle = atomic_read(&this_adm.mem_map_handles[
5262 ADM_MEM_MAP_INDEX_SOURCE_TRACKING]);
5263 admp.module_id = VOICEPROC_MODULE_ID_GENERIC_TX;
5264 admp.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOURCETRACKING;
5265 admp.param_max_size = sizeof(struct adm_param_fluence_sourcetracking_t)
5266 + sizeof(struct adm_param_data_v5);
5267 admp.reserved = 0;
5268
5269 atomic_set(&this_adm.copp.stat[p_idx][copp_idx], -1);
5270
5271 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&admp);
5272 if (ret < 0) {
5273 pr_err("%s - failed to get Source Tracking Params\n",
5274 __func__);
5275
5276 ret = -EINVAL;
5277 goto done;
5278 }
5279 ret = wait_event_timeout(this_adm.copp.wait[p_idx][copp_idx],
5280 atomic_read(&this_adm.copp.stat[p_idx][copp_idx]) >= 0,
5281 msecs_to_jiffies(TIMEOUT_MS));
5282 if (!ret) {
5283 pr_err("%s - get params timed out\n", __func__);
5284
5285 ret = -EINVAL;
5286 goto done;
5287 } else if (atomic_read(&this_adm.copp.stat
5288 [p_idx][copp_idx]) > 0) {
5289 pr_err("%s: DSP returned error[%s]\n",
5290 __func__, adsp_err_get_err_str(
5291 atomic_read(&this_adm.copp.stat
5292 [p_idx][copp_idx])));
5293 ret = adsp_err_get_lnx_err_code(
5294 atomic_read(&this_adm.copp.stat
5295 [p_idx][copp_idx]));
5296 goto done;
5297 }
5298
5299 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
5300 pr_err("%s - get params returned error [%s]\n",
5301 __func__, adsp_err_get_err_str(
5302 this_adm.sourceTrackingData.apr_cmd_status));
5303
5304 ret = adsp_err_get_lnx_err_code(
5305 this_adm.sourceTrackingData.apr_cmd_status);
5306 goto done;
5307 }
5308
5309 source_tracking_params = (struct adm_param_fluence_sourcetracking_t *)
5310 (this_adm.sourceTrackingData.memmap.kvaddr +
5311 sizeof(struct adm_param_data_v5));
5312 for (i = 0; i < MAX_SECTORS; i++) {
5313 sourceTrackingData->vad[i] = source_tracking_params->vad[i];
5314 pr_debug("%s: vad[%d] = %d\n",
5315 __func__, i, sourceTrackingData->vad[i]);
5316 }
5317 sourceTrackingData->doa_speech = source_tracking_params->doa_speech;
5318 pr_debug("%s: doa_speech = %d\n",
5319 __func__, sourceTrackingData->doa_speech);
5320
5321 for (i = 0; i < MAX_NOISE_SOURCE_INDICATORS; i++) {
5322 sourceTrackingData->doa_noise[i] =
5323 source_tracking_params->doa_noise[i];
5324 pr_debug("%s: doa_noise[%d] = %d\n",
5325 __func__, i, sourceTrackingData->doa_noise[i]);
5326 }
5327 for (i = 0; i < MAX_POLAR_ACTIVITY_INDICATORS; i++) {
5328 sourceTrackingData->polar_activity[i] =
5329 source_tracking_params->polar_activity[i];
5330 pr_debug("%s: polar_activity[%d] = %d\n",
5331 __func__, i, sourceTrackingData->polar_activity[i]);
5332 }
5333
5334 ret = 0;
5335
5336done:
5337 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
5338
5339 return ret;
5340}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305341EXPORT_SYMBOL(adm_get_source_tracking);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305342
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305343int __init adm_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305344{
5345 int i = 0, j;
5346
5347 this_adm.apr = NULL;
5348 this_adm.ec_ref_rx = -1;
5349 this_adm.num_ec_ref_rx_chans = 0;
5350 this_adm.ec_ref_rx_bit_width = 0;
5351 this_adm.ec_ref_rx_sampling_rate = 0;
5352 atomic_set(&this_adm.matrix_map_stat, 0);
5353 init_waitqueue_head(&this_adm.matrix_map_wait);
5354 atomic_set(&this_adm.adm_stat, 0);
5355 init_waitqueue_head(&this_adm.adm_wait);
5356
5357 for (i = 0; i < AFE_MAX_PORTS; i++) {
5358 for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
5359 atomic_set(&this_adm.copp.id[i][j], RESET_COPP_ID);
5360 atomic_set(&this_adm.copp.cnt[i][j], 0);
5361 atomic_set(&this_adm.copp.topology[i][j], 0);
5362 atomic_set(&this_adm.copp.mode[i][j], 0);
5363 atomic_set(&this_adm.copp.stat[i][j], 0);
5364 atomic_set(&this_adm.copp.rate[i][j], 0);
5365 atomic_set(&this_adm.copp.channels[i][j], 0);
5366 atomic_set(&this_adm.copp.bit_width[i][j], 0);
5367 atomic_set(&this_adm.copp.app_type[i][j], 0);
5368 atomic_set(&this_adm.copp.acdb_id[i][j], 0);
Sachin Mohan Gadag3c3c5812018-07-20 15:46:54 +05305369 atomic_set(&this_adm.copp.session_type[i][j], 0);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305370 init_waitqueue_head(&this_adm.copp.wait[i][j]);
5371 atomic_set(&this_adm.copp.adm_delay_stat[i][j], 0);
5372 init_waitqueue_head(
5373 &this_adm.copp.adm_delay_wait[i][j]);
5374 atomic_set(&this_adm.copp.topology[i][j], 0);
5375 this_adm.copp.adm_delay[i][j] = 0;
5376 this_adm.copp.adm_status[i][j] =
5377 ADM_STATUS_CALIBRATION_REQUIRED;
5378 }
5379 }
5380
5381 if (adm_init_cal_data())
5382 pr_err("%s: could not init cal data!\n", __func__);
5383
5384 this_adm.sourceTrackingData.ion_client = NULL;
5385 this_adm.sourceTrackingData.ion_handle = NULL;
5386 this_adm.sourceTrackingData.memmap.size = 0;
5387 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
5388 this_adm.sourceTrackingData.memmap.paddr = 0;
5389 this_adm.sourceTrackingData.apr_cmd_status = -1;
5390 atomic_set(&this_adm.mem_map_handles[ADM_MEM_MAP_INDEX_SOURCE_TRACKING],
5391 0);
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305392 mutex_init(&dts_srs_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305393
5394 return 0;
5395}
5396
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05305397void adm_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305398{
Laxminath Kasam468ece32017-11-28 12:40:22 +05305399 if (this_adm.apr)
5400 adm_reset_data();
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305401 mutex_destroy(&dts_srs_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305402 adm_delete_cal_data();
5403}