blob: 8d8a473fa17db1922dbe8028c2c4e0790549b640 [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))) {
1806 if (payload[1] >
1807 ((ADM_GET_TOPO_MODULE_LIST_LENGTH /
1808 sizeof(uint32_t)) - 1)) {
1809 pr_err("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST",
1810 __func__);
1811 pr_err(":size = %d\n", payload[1]);
1812 } else {
1813 idx = ADM_GET_TOPO_MODULE_LIST_LENGTH *
1814 copp_idx;
1815 pr_debug("%s:Num modules payload[1] %d\n",
1816 __func__, payload[1]);
1817 adm_module_topo_list[idx] = payload[1];
1818 for (i = 1; i <= payload[1]; i++) {
1819 adm_module_topo_list[idx+i] =
1820 payload[1+i];
1821 pr_debug("%s:payload[%d] = %x\n",
1822 __func__, (i+1),
1823 payload[1+i]);
1824 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301825 }
Vignesh Kulothungan35274962019-01-22 11:13:09 -08001826 } else
1827 pr_err("%s: Invalid payload size %d\n",
1828 __func__, data->payload_size);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301829 atomic_set(&this_adm.copp.stat
1830 [port_idx][copp_idx], payload[0]);
1831 wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
1832 break;
1833 case ADM_CMDRSP_SHARED_MEM_MAP_REGIONS:
1834 pr_debug("%s: ADM_CMDRSP_SHARED_MEM_MAP_REGIONS\n",
1835 __func__);
1836 atomic_set(&this_adm.mem_map_handles[
1837 atomic_read(&this_adm.mem_map_index)],
1838 *payload);
1839 atomic_set(&this_adm.adm_stat, 0);
1840 wake_up(&this_adm.adm_wait);
1841 break;
1842 default:
1843 pr_err("%s: Unknown cmd:0x%x\n", __func__,
1844 data->opcode);
1845 break;
1846 }
1847 }
1848 return 0;
1849}
1850
1851static int adm_memory_map_regions(phys_addr_t *buf_add, uint32_t mempool_id,
1852 uint32_t *bufsz, uint32_t bufcnt)
1853{
1854 struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
1855 struct avs_shared_map_region_payload *mregions = NULL;
1856 void *mmap_region_cmd = NULL;
1857 void *payload = NULL;
1858 int ret = 0;
1859 int i = 0;
1860 int cmd_size = 0;
1861
1862 pr_debug("%s:\n", __func__);
1863 if (this_adm.apr == NULL) {
1864 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
1865 0xFFFFFFFF, &this_adm);
1866 if (this_adm.apr == NULL) {
1867 pr_err("%s: Unable to register ADM\n", __func__);
1868 ret = -ENODEV;
1869 return ret;
1870 }
1871 rtac_set_adm_handle(this_adm.apr);
1872 }
1873
1874 cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
1875 + sizeof(struct avs_shared_map_region_payload)
1876 * bufcnt;
1877
1878 mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
1879 if (!mmap_region_cmd)
1880 return -ENOMEM;
1881
1882 mmap_regions = (struct avs_cmd_shared_mem_map_regions *)mmap_region_cmd;
1883 mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1884 APR_HDR_LEN(APR_HDR_SIZE),
1885 APR_PKT_VER);
1886 mmap_regions->hdr.pkt_size = cmd_size;
1887 mmap_regions->hdr.src_port = 0;
1888
1889 mmap_regions->hdr.dest_port = 0;
1890 mmap_regions->hdr.token = 0;
1891 mmap_regions->hdr.opcode = ADM_CMD_SHARED_MEM_MAP_REGIONS;
1892 mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL & 0x00ff;
1893 mmap_regions->num_regions = bufcnt & 0x00ff;
1894 mmap_regions->property_flag = 0x00;
1895
1896 pr_debug("%s: map_regions->num_regions = %d\n", __func__,
1897 mmap_regions->num_regions);
1898 payload = ((u8 *) mmap_region_cmd +
1899 sizeof(struct avs_cmd_shared_mem_map_regions));
1900 mregions = (struct avs_shared_map_region_payload *)payload;
1901
1902 for (i = 0; i < bufcnt; i++) {
1903 mregions->shm_addr_lsw = lower_32_bits(buf_add[i]);
1904 mregions->shm_addr_msw =
1905 msm_audio_populate_upper_32_bits(buf_add[i]);
1906 mregions->mem_size_bytes = bufsz[i];
1907 ++mregions;
1908 }
1909
1910 atomic_set(&this_adm.adm_stat, -1);
1911 ret = apr_send_pkt(this_adm.apr, (uint32_t *) mmap_region_cmd);
1912 if (ret < 0) {
1913 pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__,
1914 mmap_regions->hdr.opcode, ret);
1915 ret = -EINVAL;
1916 goto fail_cmd;
1917 }
1918
1919 ret = wait_event_timeout(this_adm.adm_wait,
1920 atomic_read(&this_adm.adm_stat) >= 0,
1921 5 * HZ);
1922 if (!ret) {
1923 pr_err("%s: timeout. waited for memory_map\n", __func__);
1924 ret = -EINVAL;
1925 goto fail_cmd;
1926 } else if (atomic_read(&this_adm.adm_stat) > 0) {
1927 pr_err("%s: DSP returned error[%s]\n",
1928 __func__, adsp_err_get_err_str(
1929 atomic_read(&this_adm.adm_stat)));
1930 ret = adsp_err_get_lnx_err_code(
1931 atomic_read(&this_adm.adm_stat));
1932 goto fail_cmd;
1933 }
1934fail_cmd:
1935 kfree(mmap_region_cmd);
1936 return ret;
1937}
1938
1939static int adm_memory_unmap_regions(void)
1940{
1941 struct avs_cmd_shared_mem_unmap_regions unmap_regions;
1942 int ret = 0;
1943
1944 pr_debug("%s:\n", __func__);
1945 if (this_adm.apr == NULL) {
1946 pr_err("%s: APR handle NULL\n", __func__);
1947 return -EINVAL;
1948 }
1949
1950 unmap_regions.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1951 APR_HDR_LEN(APR_HDR_SIZE),
1952 APR_PKT_VER);
1953 unmap_regions.hdr.pkt_size = sizeof(unmap_regions);
1954 unmap_regions.hdr.src_port = 0;
1955 unmap_regions.hdr.dest_port = 0;
1956 unmap_regions.hdr.token = 0;
1957 unmap_regions.hdr.opcode = ADM_CMD_SHARED_MEM_UNMAP_REGIONS;
1958 unmap_regions.mem_map_handle = atomic_read(&this_adm.
1959 mem_map_handles[atomic_read(&this_adm.mem_map_index)]);
1960 atomic_set(&this_adm.adm_stat, -1);
1961 ret = apr_send_pkt(this_adm.apr, (uint32_t *) &unmap_regions);
1962 if (ret < 0) {
1963 pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__,
1964 unmap_regions.hdr.opcode, ret);
1965 ret = -EINVAL;
1966 goto fail_cmd;
1967 }
1968
1969 ret = wait_event_timeout(this_adm.adm_wait,
1970 atomic_read(&this_adm.adm_stat) >= 0,
1971 5 * HZ);
1972 if (!ret) {
1973 pr_err("%s: timeout. waited for memory_unmap\n",
1974 __func__);
1975 ret = -EINVAL;
1976 goto fail_cmd;
1977 } else if (atomic_read(&this_adm.adm_stat) > 0) {
1978 pr_err("%s: DSP returned error[%s]\n",
1979 __func__, adsp_err_get_err_str(
1980 atomic_read(&this_adm.adm_stat)));
1981 ret = adsp_err_get_lnx_err_code(
1982 atomic_read(&this_adm.adm_stat));
1983 goto fail_cmd;
1984 } else {
1985 pr_debug("%s: Unmap handle 0x%x succeeded\n", __func__,
1986 unmap_regions.mem_map_handle);
1987 }
1988fail_cmd:
1989 return ret;
1990}
1991
1992static int remap_cal_data(struct cal_block_data *cal_block, int cal_index)
1993{
1994 int ret = 0;
1995
1996 if (cal_block->map_data.ion_client == NULL) {
1997 pr_err("%s: No ION allocation for cal index %d!\n",
1998 __func__, cal_index);
1999 ret = -EINVAL;
2000 goto done;
2001 }
2002
2003 if ((cal_block->map_data.map_size > 0) &&
2004 (cal_block->map_data.q6map_handle == 0)) {
2005 atomic_set(&this_adm.mem_map_index, cal_index);
2006 ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
2007 (uint32_t *)&cal_block->map_data.map_size, 1);
2008 if (ret < 0) {
2009 pr_err("%s: ADM mmap did not work! size = %zd ret %d\n",
2010 __func__,
2011 cal_block->map_data.map_size, ret);
2012 pr_debug("%s: ADM mmap did not work! addr = 0x%pK, size = %zd ret %d\n",
2013 __func__,
2014 &cal_block->cal_data.paddr,
2015 cal_block->map_data.map_size, ret);
2016 goto done;
2017 }
2018 cal_block->map_data.q6map_handle = atomic_read(&this_adm.
2019 mem_map_handles[cal_index]);
2020 }
2021done:
2022 return ret;
2023}
2024
2025static void send_adm_custom_topology(void)
2026{
2027 struct cal_block_data *cal_block = NULL;
2028 struct cmd_set_topologies adm_top;
2029 int cal_index = ADM_CUSTOM_TOP_CAL;
2030 int result;
2031
2032 if (this_adm.cal_data[cal_index] == NULL)
2033 goto done;
2034
2035 mutex_lock(&this_adm.cal_data[cal_index]->lock);
2036 if (!this_adm.set_custom_topology)
2037 goto unlock;
2038 this_adm.set_custom_topology = 0;
2039
2040 cal_block = cal_utils_get_only_cal_block(this_adm.cal_data[cal_index]);
Vikram Pandurangad3b58cc2017-09-27 12:17:36 -07002041 if (cal_block == NULL || cal_utils_is_cal_stale(cal_block))
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302042 goto unlock;
2043
2044 pr_debug("%s: Sending cal_index %d\n", __func__, cal_index);
2045
2046 result = remap_cal_data(cal_block, cal_index);
2047 if (result) {
2048 pr_err("%s: Remap_cal_data failed for cal %d!\n",
2049 __func__, cal_index);
2050 goto unlock;
2051 }
2052 atomic_set(&this_adm.mem_map_index, cal_index);
2053 atomic_set(&this_adm.mem_map_handles[cal_index],
2054 cal_block->map_data.q6map_handle);
2055
2056 if (cal_block->cal_data.size == 0) {
2057 pr_debug("%s: No ADM cal to send\n", __func__);
2058 goto unlock;
2059 }
2060
2061 adm_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2062 APR_HDR_LEN(20), APR_PKT_VER);
2063 adm_top.hdr.pkt_size = sizeof(adm_top);
2064 adm_top.hdr.src_svc = APR_SVC_ADM;
2065 adm_top.hdr.src_domain = APR_DOMAIN_APPS;
2066 adm_top.hdr.src_port = 0;
2067 adm_top.hdr.dest_svc = APR_SVC_ADM;
2068 adm_top.hdr.dest_domain = APR_DOMAIN_ADSP;
2069 adm_top.hdr.dest_port = 0;
2070 adm_top.hdr.token = 0;
2071 adm_top.hdr.opcode = ADM_CMD_ADD_TOPOLOGIES;
2072 adm_top.payload_addr_lsw = lower_32_bits(cal_block->cal_data.paddr);
2073 adm_top.payload_addr_msw = msm_audio_populate_upper_32_bits(
2074 cal_block->cal_data.paddr);
2075 adm_top.mem_map_handle = cal_block->map_data.q6map_handle;
2076 adm_top.payload_size = cal_block->cal_data.size;
2077
2078 atomic_set(&this_adm.adm_stat, -1);
2079 pr_debug("%s: Sending ADM_CMD_ADD_TOPOLOGIES payload = 0x%pK, size = %d\n",
2080 __func__, &cal_block->cal_data.paddr,
2081 adm_top.payload_size);
2082 result = apr_send_pkt(this_adm.apr, (uint32_t *)&adm_top);
2083 if (result < 0) {
2084 pr_err("%s: Set topologies failed payload size = %zd result %d\n",
2085 __func__, cal_block->cal_data.size, result);
2086 goto unlock;
2087 }
2088 /* Wait for the callback */
2089 result = wait_event_timeout(this_adm.adm_wait,
2090 atomic_read(&this_adm.adm_stat) >= 0,
2091 msecs_to_jiffies(TIMEOUT_MS));
2092 if (!result) {
2093 pr_err("%s: Set topologies timed out payload size = %zd\n",
2094 __func__, cal_block->cal_data.size);
2095 goto unlock;
2096 } else if (atomic_read(&this_adm.adm_stat) > 0) {
2097 pr_err("%s: DSP returned error[%s]\n",
2098 __func__, adsp_err_get_err_str(
2099 atomic_read(&this_adm.adm_stat)));
2100 result = adsp_err_get_lnx_err_code(
2101 atomic_read(&this_adm.adm_stat));
2102 goto unlock;
2103 }
2104unlock:
2105 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
2106done:
2107 return;
2108}
2109
2110static int send_adm_cal_block(int port_id, int copp_idx,
2111 struct cal_block_data *cal_block, int perf_mode,
2112 int app_type, int acdb_id, int sample_rate)
2113{
2114 s32 result = 0;
2115 struct adm_cmd_set_pp_params_v5 adm_params;
2116 int port_idx;
2117
2118 pr_debug("%s: Port id 0x%x sample_rate %d ,\n", __func__,
2119 port_id, sample_rate);
2120 port_id = afe_convert_virtual_to_portid(port_id);
2121 port_idx = adm_validate_and_get_port_index(port_id);
2122 if (port_idx < 0) {
2123 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2124 return -EINVAL;
2125 }
2126 if (!cal_block) {
2127 pr_debug("%s: No ADM cal to send for port_id = 0x%x!\n",
2128 __func__, port_id);
2129 result = -EINVAL;
2130 goto done;
2131 }
2132 if (cal_block->cal_data.size <= 0) {
2133 pr_debug("%s: No ADM cal send for port_id = 0x%x!\n",
2134 __func__, port_id);
2135 result = -EINVAL;
2136 goto done;
2137 }
2138
2139 if (perf_mode == LEGACY_PCM_MODE &&
2140 ((atomic_read(&this_adm.copp.topology[port_idx][copp_idx])) ==
2141 DS2_ADM_COPP_TOPOLOGY_ID)) {
2142 pr_err("%s: perf_mode %d, topology 0x%x\n", __func__, perf_mode,
2143 atomic_read(
2144 &this_adm.copp.topology[port_idx][copp_idx]));
2145 goto done;
2146 }
2147
2148 adm_params.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2149 APR_HDR_LEN(20), APR_PKT_VER);
2150 adm_params.hdr.pkt_size = sizeof(adm_params);
2151 adm_params.hdr.src_svc = APR_SVC_ADM;
2152 adm_params.hdr.src_domain = APR_DOMAIN_APPS;
2153 adm_params.hdr.src_port = port_id;
2154 adm_params.hdr.dest_svc = APR_SVC_ADM;
2155 adm_params.hdr.dest_domain = APR_DOMAIN_ADSP;
2156
2157 adm_params.hdr.token = port_idx << 16 | copp_idx;
2158 adm_params.hdr.dest_port =
2159 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
2160 adm_params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
2161 adm_params.payload_addr_lsw = lower_32_bits(cal_block->cal_data.paddr);
2162 adm_params.payload_addr_msw = msm_audio_populate_upper_32_bits(
2163 cal_block->cal_data.paddr);
2164 adm_params.mem_map_handle = cal_block->map_data.q6map_handle;
2165 adm_params.payload_size = cal_block->cal_data.size;
2166
2167 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2168 pr_debug("%s: Sending SET_PARAMS payload = 0x%pK, size = %d\n",
2169 __func__, &cal_block->cal_data.paddr,
2170 adm_params.payload_size);
2171 result = apr_send_pkt(this_adm.apr, (uint32_t *)&adm_params);
2172 if (result < 0) {
2173 pr_err("%s: Set params failed port 0x%x result %d\n",
2174 __func__, port_id, result);
2175 pr_debug("%s: Set params failed port = 0x%x payload = 0x%pK result %d\n",
2176 __func__, port_id, &cal_block->cal_data.paddr, result);
2177 result = -EINVAL;
2178 goto done;
2179 }
2180 /* Wait for the callback */
2181 result = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
2182 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
2183 msecs_to_jiffies(TIMEOUT_MS));
2184 if (!result) {
2185 pr_err("%s: Set params timed out port = 0x%x\n",
2186 __func__, port_id);
2187 pr_debug("%s: Set params timed out port = 0x%x, payload = 0x%pK\n",
2188 __func__, port_id, &cal_block->cal_data.paddr);
2189 result = -EINVAL;
2190 goto done;
2191 } else if (atomic_read(&this_adm.copp.stat
2192 [port_idx][copp_idx]) > 0) {
2193 pr_err("%s: DSP returned error[%s]\n",
2194 __func__, adsp_err_get_err_str(
2195 atomic_read(&this_adm.copp.stat
2196 [port_idx][copp_idx])));
2197 result = adsp_err_get_lnx_err_code(
2198 atomic_read(&this_adm.copp.stat
2199 [port_idx][copp_idx]));
2200 goto done;
2201 }
2202
2203done:
2204 return result;
2205}
2206
2207static struct cal_block_data *adm_find_cal_by_path(int cal_index, int path)
2208{
2209 struct list_head *ptr, *next;
2210 struct cal_block_data *cal_block = NULL;
2211 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2212 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2213
2214 pr_debug("%s:\n", __func__);
2215
2216 list_for_each_safe(ptr, next,
2217 &this_adm.cal_data[cal_index]->cal_blocks) {
2218
2219 cal_block = list_entry(ptr,
2220 struct cal_block_data, list);
2221
Vikram Pandurangad3b58cc2017-09-27 12:17:36 -07002222 if (cal_utils_is_cal_stale(cal_block))
2223 continue;
2224
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302225 if (cal_index == ADM_AUDPROC_CAL ||
Bhalchandra Gajareface2762018-05-10 14:16:49 -07002226 cal_index == ADM_LSM_AUDPROC_CAL ||
2227 cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302228 audproc_cal_info = cal_block->cal_info;
2229 if ((audproc_cal_info->path == path) &&
2230 (cal_block->cal_data.size > 0))
2231 return cal_block;
2232 } else if (cal_index == ADM_AUDVOL_CAL) {
2233 audvol_cal_info = cal_block->cal_info;
2234 if ((audvol_cal_info->path == path) &&
2235 (cal_block->cal_data.size > 0))
2236 return cal_block;
2237 }
2238 }
2239 pr_debug("%s: Can't find ADM cal for cal_index %d, path %d\n",
2240 __func__, cal_index, path);
2241 return NULL;
2242}
2243
2244static struct cal_block_data *adm_find_cal_by_app_type(int cal_index, int path,
2245 int app_type)
2246{
2247 struct list_head *ptr, *next;
2248 struct cal_block_data *cal_block = NULL;
2249 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2250 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2251
2252 pr_debug("%s\n", __func__);
2253
2254 list_for_each_safe(ptr, next,
2255 &this_adm.cal_data[cal_index]->cal_blocks) {
2256
2257 cal_block = list_entry(ptr,
2258 struct cal_block_data, list);
2259
Vikram Pandurangad3b58cc2017-09-27 12:17:36 -07002260 if (cal_utils_is_cal_stale(cal_block))
2261 continue;
2262
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302263 if (cal_index == ADM_AUDPROC_CAL ||
Bhalchandra Gajareface2762018-05-10 14:16:49 -07002264 cal_index == ADM_LSM_AUDPROC_CAL ||
2265 cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302266 audproc_cal_info = cal_block->cal_info;
2267 if ((audproc_cal_info->path == path) &&
2268 (audproc_cal_info->app_type == app_type) &&
2269 (cal_block->cal_data.size > 0))
2270 return cal_block;
2271 } else if (cal_index == ADM_AUDVOL_CAL) {
2272 audvol_cal_info = cal_block->cal_info;
2273 if ((audvol_cal_info->path == path) &&
2274 (audvol_cal_info->app_type == app_type) &&
2275 (cal_block->cal_data.size > 0))
2276 return cal_block;
2277 }
2278 }
2279 pr_debug("%s: Can't find ADM cali for cal_index %d, path %d, app %d, defaulting to search by path\n",
2280 __func__, cal_index, path, app_type);
2281 return adm_find_cal_by_path(cal_index, path);
2282}
2283
2284
2285static struct cal_block_data *adm_find_cal(int cal_index, int path,
2286 int app_type, int acdb_id,
2287 int sample_rate)
2288{
2289 struct list_head *ptr, *next;
2290 struct cal_block_data *cal_block = NULL;
2291 struct audio_cal_info_audproc *audproc_cal_info = NULL;
2292 struct audio_cal_info_audvol *audvol_cal_info = NULL;
2293
2294 pr_debug("%s:\n", __func__);
2295
2296 list_for_each_safe(ptr, next,
2297 &this_adm.cal_data[cal_index]->cal_blocks) {
2298
2299 cal_block = list_entry(ptr,
2300 struct cal_block_data, list);
Vikram Pandurangad3b58cc2017-09-27 12:17:36 -07002301 if (cal_utils_is_cal_stale(cal_block))
2302 continue;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302303
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302304 if (cal_index == ADM_AUDPROC_CAL ||
Bhalchandra Gajareface2762018-05-10 14:16:49 -07002305 cal_index == ADM_LSM_AUDPROC_CAL ||
2306 cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302307 audproc_cal_info = cal_block->cal_info;
2308 if ((audproc_cal_info->path == path) &&
2309 (audproc_cal_info->app_type == app_type) &&
2310 (audproc_cal_info->acdb_id == acdb_id) &&
2311 (audproc_cal_info->sample_rate == sample_rate) &&
2312 (cal_block->cal_data.size > 0))
2313 return cal_block;
2314 } else if (cal_index == ADM_AUDVOL_CAL) {
2315 audvol_cal_info = cal_block->cal_info;
2316 if ((audvol_cal_info->path == path) &&
2317 (audvol_cal_info->app_type == app_type) &&
2318 (audvol_cal_info->acdb_id == acdb_id) &&
2319 (cal_block->cal_data.size > 0))
2320 return cal_block;
2321 }
2322 }
2323 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",
2324 __func__, cal_index, path, app_type, acdb_id, sample_rate);
2325 return adm_find_cal_by_app_type(cal_index, path, app_type);
2326}
2327
2328static int adm_remap_and_send_cal_block(int cal_index, int port_id,
2329 int copp_idx, struct cal_block_data *cal_block, int perf_mode,
2330 int app_type, int acdb_id, int sample_rate)
2331{
2332 int ret = 0;
2333
2334 pr_debug("%s: Sending cal_index cal %d\n", __func__, cal_index);
2335 ret = remap_cal_data(cal_block, cal_index);
2336 if (ret) {
2337 pr_err("%s: Remap_cal_data failed for cal %d!\n",
2338 __func__, cal_index);
2339 goto done;
2340 }
2341 ret = send_adm_cal_block(port_id, copp_idx, cal_block, perf_mode,
2342 app_type, acdb_id, sample_rate);
2343 if (ret < 0)
2344 pr_debug("%s: No cal sent for cal_index %d, port_id = 0x%x! ret %d sample_rate %d\n",
2345 __func__, cal_index, port_id, ret, sample_rate);
2346done:
2347 return ret;
2348}
2349
2350static void send_adm_cal_type(int cal_index, int path, int port_id,
2351 int copp_idx, int perf_mode, int app_type,
2352 int acdb_id, int sample_rate)
2353{
2354 struct cal_block_data *cal_block = NULL;
2355 int ret;
2356
2357 pr_debug("%s: cal index %d\n", __func__, cal_index);
2358
2359 if (this_adm.cal_data[cal_index] == NULL) {
2360 pr_debug("%s: cal_index %d not allocated!\n",
2361 __func__, cal_index);
2362 goto done;
2363 }
2364
2365 mutex_lock(&this_adm.cal_data[cal_index]->lock);
2366 cal_block = adm_find_cal(cal_index, path, app_type, acdb_id,
2367 sample_rate);
2368 if (cal_block == NULL)
2369 goto unlock;
2370
2371 ret = adm_remap_and_send_cal_block(cal_index, port_id, copp_idx,
2372 cal_block, perf_mode, app_type, acdb_id, sample_rate);
Vikram Pandurangad3b58cc2017-09-27 12:17:36 -07002373
2374 cal_utils_mark_cal_used(cal_block);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302375unlock:
2376 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
2377done:
2378 return;
2379}
2380
2381static int get_cal_path(int path)
2382{
2383 if (path == 0x1)
2384 return RX_DEVICE;
2385 else
2386 return TX_DEVICE;
2387}
2388
2389static void send_adm_cal(int port_id, int copp_idx, int path, int perf_mode,
Aditya Bavanarifcd80ec2018-01-08 13:16:32 +05302390 int app_type, int acdb_id, int sample_rate,
2391 int passthr_mode)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302392{
2393 pr_debug("%s: port id 0x%x copp_idx %d\n", __func__, port_id, copp_idx);
2394
Bhalchandra Gajareface2762018-05-10 14:16:49 -07002395 if (passthr_mode != LISTEN) {
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302396 send_adm_cal_type(ADM_AUDPROC_CAL, path, port_id, copp_idx,
2397 perf_mode, app_type, acdb_id, sample_rate);
Bhalchandra Gajareface2762018-05-10 14:16:49 -07002398 } else {
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05302399 send_adm_cal_type(ADM_LSM_AUDPROC_CAL, path, port_id, copp_idx,
2400 perf_mode, app_type, acdb_id, sample_rate);
Bhalchandra Gajareface2762018-05-10 14:16:49 -07002401
2402 send_adm_cal_type(ADM_LSM_AUDPROC_PERSISTENT_CAL, path,
2403 port_id, copp_idx, perf_mode, app_type,
2404 acdb_id, sample_rate);
2405 }
2406
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302407 send_adm_cal_type(ADM_AUDVOL_CAL, path, port_id, copp_idx, perf_mode,
2408 app_type, acdb_id, sample_rate);
2409}
2410
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302411/**
2412 * adm_connect_afe_port -
2413 * command to send ADM connect AFE port
2414 *
2415 * @mode: value of mode for ADM connect AFE
2416 * @session_id: session active to connect
2417 * @port_id: Port ID number
2418 *
2419 * Returns 0 on success or error on failure
2420 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302421int adm_connect_afe_port(int mode, int session_id, int port_id)
2422{
2423 struct adm_cmd_connect_afe_port_v5 cmd;
2424 int ret = 0;
2425 int port_idx, copp_idx = 0;
2426
2427 pr_debug("%s: port_id: 0x%x session id:%d mode:%d\n", __func__,
2428 port_id, session_id, mode);
2429
2430 port_id = afe_convert_virtual_to_portid(port_id);
2431 port_idx = adm_validate_and_get_port_index(port_id);
2432 if (port_idx < 0) {
2433 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2434 return -EINVAL;
2435 }
2436
2437 if (this_adm.apr == NULL) {
2438 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
2439 0xFFFFFFFF, &this_adm);
2440 if (this_adm.apr == NULL) {
2441 pr_err("%s: Unable to register ADM\n", __func__);
2442 ret = -ENODEV;
2443 return ret;
2444 }
2445 rtac_set_adm_handle(this_adm.apr);
2446 }
2447 pr_debug("%s: Port ID 0x%x, index %d\n", __func__, port_id, port_idx);
2448
2449 cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2450 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
2451 cmd.hdr.pkt_size = sizeof(cmd);
2452 cmd.hdr.src_svc = APR_SVC_ADM;
2453 cmd.hdr.src_domain = APR_DOMAIN_APPS;
2454 cmd.hdr.src_port = port_id;
2455 cmd.hdr.dest_svc = APR_SVC_ADM;
2456 cmd.hdr.dest_domain = APR_DOMAIN_ADSP;
2457 cmd.hdr.dest_port = 0; /* Ignored */
2458 cmd.hdr.token = port_idx << 16 | copp_idx;
2459 cmd.hdr.opcode = ADM_CMD_CONNECT_AFE_PORT_V5;
2460
2461 cmd.mode = mode;
2462 cmd.session_id = session_id;
2463 cmd.afe_port_id = port_id;
2464
2465 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2466 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&cmd);
2467 if (ret < 0) {
2468 pr_err("%s: ADM enable for port_id: 0x%x failed ret %d\n",
2469 __func__, port_id, ret);
2470 ret = -EINVAL;
2471 goto fail_cmd;
2472 }
2473 /* Wait for the callback with copp id */
2474 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
2475 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
2476 msecs_to_jiffies(TIMEOUT_MS));
2477 if (!ret) {
2478 pr_err("%s: ADM connect timedout for port_id: 0x%x\n",
2479 __func__, port_id);
2480 ret = -EINVAL;
2481 goto fail_cmd;
2482 } else if (atomic_read(&this_adm.copp.stat
2483 [port_idx][copp_idx]) > 0) {
2484 pr_err("%s: DSP returned error[%s]\n",
2485 __func__, adsp_err_get_err_str(
2486 atomic_read(&this_adm.copp.stat
2487 [port_idx][copp_idx])));
2488 ret = adsp_err_get_lnx_err_code(
2489 atomic_read(&this_adm.copp.stat
2490 [port_idx][copp_idx]));
2491 goto fail_cmd;
2492 }
2493 atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
2494 return 0;
2495
2496fail_cmd:
2497
2498 return ret;
2499}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302500EXPORT_SYMBOL(adm_connect_afe_port);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302501
2502int adm_arrange_mch_map(struct adm_cmd_device_open_v5 *open, int path,
Rohit kumar5d860f42019-02-01 18:01:12 +05302503 int channel_mode, int port_idx)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302504{
2505 int rc = 0, idx;
2506
2507 memset(open->dev_channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
2508 switch (path) {
2509 case ADM_PATH_PLAYBACK:
2510 idx = ADM_MCH_MAP_IDX_PLAYBACK;
2511 break;
2512 case ADM_PATH_LIVE_REC:
2513 case ADM_PATH_NONLIVE_REC:
2514 idx = ADM_MCH_MAP_IDX_REC;
2515 break;
2516 default:
2517 goto non_mch_path;
2518 };
Rohit kumar5d860f42019-02-01 18:01:12 +05302519
2520 if ((open->dev_num_channel > 2) &&
2521 (port_channel_map[port_idx].set_channel_map ||
2522 multi_ch_maps[idx].set_channel_map)) {
2523 if (port_channel_map[port_idx].set_channel_map)
2524 memcpy(open->dev_channel_mapping,
2525 port_channel_map[port_idx].channel_mapping,
2526 PCM_FORMAT_MAX_NUM_CHANNEL);
2527 else
2528 memcpy(open->dev_channel_mapping,
2529 multi_ch_maps[idx].channel_mapping,
2530 PCM_FORMAT_MAX_NUM_CHANNEL);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302531 } else {
2532 if (channel_mode == 1) {
2533 open->dev_channel_mapping[0] = PCM_CHANNEL_FC;
2534 } else if (channel_mode == 2) {
2535 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2536 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2537 } else if (channel_mode == 3) {
2538 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2539 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2540 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2541 } else if (channel_mode == 4) {
2542 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2543 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2544 open->dev_channel_mapping[2] = PCM_CHANNEL_LS;
2545 open->dev_channel_mapping[3] = PCM_CHANNEL_RS;
2546 } else if (channel_mode == 5) {
2547 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2548 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2549 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2550 open->dev_channel_mapping[3] = PCM_CHANNEL_LS;
2551 open->dev_channel_mapping[4] = PCM_CHANNEL_RS;
2552 } else if (channel_mode == 6) {
2553 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2554 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2555 open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2556 open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2557 open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2558 open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2559 } else if (channel_mode == 7) {
2560 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2561 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2562 open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
2563 open->dev_channel_mapping[3] = PCM_CHANNEL_LFE;
2564 open->dev_channel_mapping[4] = PCM_CHANNEL_LB;
2565 open->dev_channel_mapping[5] = PCM_CHANNEL_RB;
2566 open->dev_channel_mapping[6] = PCM_CHANNEL_CS;
2567 } else if (channel_mode == 8) {
2568 open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
2569 open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
2570 open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
2571 open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
2572 open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
2573 open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
2574 open->dev_channel_mapping[6] = PCM_CHANNEL_LB;
2575 open->dev_channel_mapping[7] = PCM_CHANNEL_RB;
2576 } else {
2577 pr_err("%s: invalid num_chan %d\n", __func__,
2578 channel_mode);
2579 rc = -EINVAL;
2580 goto inval_ch_mod;
2581 }
2582 }
2583
2584non_mch_path:
2585inval_ch_mod:
2586 return rc;
2587}
2588
2589int adm_arrange_mch_ep2_map(struct adm_cmd_device_open_v6 *open_v6,
2590 int channel_mode)
2591{
2592 int rc = 0;
2593
2594 memset(open_v6->dev_channel_mapping_eid2, 0,
2595 PCM_FORMAT_MAX_NUM_CHANNEL);
2596
2597 if (channel_mode == 1) {
2598 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FC;
2599 } else if (channel_mode == 2) {
2600 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2601 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2602 } else if (channel_mode == 3) {
2603 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2604 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2605 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
2606 } else if (channel_mode == 4) {
2607 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2608 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2609 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LS;
2610 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_RS;
2611 } else if (channel_mode == 5) {
2612 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2613 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2614 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
2615 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_LS;
2616 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_RS;
2617 } else if (channel_mode == 6) {
2618 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2619 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2620 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
2621 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
2622 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
2623 open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
2624 } else if (channel_mode == 8) {
2625 open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
2626 open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
2627 open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
2628 open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
2629 open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
2630 open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
2631 open_v6->dev_channel_mapping_eid2[6] = PCM_CHANNEL_LB;
2632 open_v6->dev_channel_mapping_eid2[7] = PCM_CHANNEL_RB;
2633 } else {
2634 pr_err("%s: invalid num_chan %d\n", __func__,
2635 channel_mode);
2636 rc = -EINVAL;
2637 }
2638
2639 return rc;
2640}
2641
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302642/**
2643 * adm_open -
2644 * command to send ADM open
2645 *
2646 * @port_id: port id number
2647 * @path: direction or ADM path type
2648 * @rate: sample rate of session
2649 * @channel_mode: number of channels set
2650 * @topology: topology active for this session
2651 * @perf_mode: performance mode like LL/ULL/..
2652 * @bit_width: bit width to set for copp
2653 * @app_type: App type used for this session
2654 * @acdb_id: ACDB ID of this device
2655 *
2656 * Returns 0 on success or error on failure
2657 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302658int adm_open(int port_id, int path, int rate, int channel_mode, int topology,
Sachin Mohan Gadag3c3c5812018-07-20 15:46:54 +05302659 int perf_mode, uint16_t bit_width, int app_type, int acdb_id,
2660 int session_type)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302661{
2662 struct adm_cmd_device_open_v5 open;
2663 struct adm_cmd_device_open_v6 open_v6;
2664 int ret = 0;
Asish Bhattacharya34504582017-08-08 12:55:01 +05302665 int port_idx, flags;
2666 int copp_idx = -1;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302667 int tmp_port = q6audio_get_port_id(port_id);
2668
2669 pr_debug("%s:port %#x path:%d rate:%d mode:%d perf_mode:%d,topo_id %d\n",
2670 __func__, port_id, path, rate, channel_mode, perf_mode,
2671 topology);
2672
2673 port_id = q6audio_convert_virtual_to_portid(port_id);
2674 port_idx = adm_validate_and_get_port_index(port_id);
2675 if (port_idx < 0) {
2676 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
2677 return -EINVAL;
2678 }
2679
2680 if (this_adm.apr == NULL) {
2681 this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
2682 0xFFFFFFFF, &this_adm);
2683 if (this_adm.apr == NULL) {
2684 pr_err("%s: Unable to register ADM\n", __func__);
2685 return -ENODEV;
2686 }
2687 rtac_set_adm_handle(this_adm.apr);
2688 }
2689
2690 if (perf_mode == ULL_POST_PROCESSING_PCM_MODE) {
2691 flags = ADM_ULL_POST_PROCESSING_DEVICE_SESSION;
2692 if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
2693 (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
2694 (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
2695 topology = DEFAULT_COPP_TOPOLOGY;
2696 } else if (perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) {
2697 flags = ADM_ULTRA_LOW_LATENCY_DEVICE_SESSION;
2698 topology = NULL_COPP_TOPOLOGY;
2699 rate = ULL_SUPPORTED_SAMPLE_RATE;
2700 bit_width = ULL_SUPPORTED_BITS_PER_SAMPLE;
2701 } else if (perf_mode == LOW_LATENCY_PCM_MODE) {
2702 flags = ADM_LOW_LATENCY_DEVICE_SESSION;
2703 if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
2704 (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
2705 (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
2706 topology = DEFAULT_COPP_TOPOLOGY;
2707 } else {
2708 if ((path == ADM_PATH_COMPRESSED_RX) ||
2709 (path == ADM_PATH_COMPRESSED_TX))
2710 flags = 0;
2711 else
2712 flags = ADM_LEGACY_DEVICE_SESSION;
2713 }
2714
Laxminath Kasam8f7ccc22017-08-28 17:35:04 +05302715 if ((topology == VPM_TX_SM_ECNS_V2_COPP_TOPOLOGY) ||
Bala Kishore Pati798cbf82018-10-22 11:58:41 +05302716 (topology == VPM_TX_SM_ECNS_COPP_TOPOLOGY) ||
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302717 (topology == VPM_TX_DM_FLUENCE_COPP_TOPOLOGY) ||
2718 (topology == VPM_TX_DM_RFECNS_COPP_TOPOLOGY))
2719 rate = 16000;
2720
Asish Bhattacharya34504582017-08-08 12:55:01 +05302721 /*
2722 * Routing driver reuses the same adm for streams with the same
2723 * app_type, sample_rate etc.
2724 * This isn't allowed for ULL streams as per the DSP interface
2725 */
2726 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE)
2727 copp_idx = adm_get_idx_if_copp_exists(port_idx, topology,
2728 perf_mode,
2729 rate, bit_width,
Sachin Mohan Gadag3c3c5812018-07-20 15:46:54 +05302730 app_type, session_type);
Asish Bhattacharya34504582017-08-08 12:55:01 +05302731
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302732 if (copp_idx < 0) {
2733 copp_idx = adm_get_next_available_copp(port_idx);
2734 if (copp_idx >= MAX_COPPS_PER_PORT) {
2735 pr_err("%s: exceeded copp id %d\n",
2736 __func__, copp_idx);
2737 return -EINVAL;
2738 }
2739 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
2740 atomic_set(&this_adm.copp.topology[port_idx][copp_idx],
2741 topology);
2742 atomic_set(&this_adm.copp.mode[port_idx][copp_idx],
2743 perf_mode);
2744 atomic_set(&this_adm.copp.rate[port_idx][copp_idx],
2745 rate);
2746 atomic_set(&this_adm.copp.channels[port_idx][copp_idx],
2747 channel_mode);
2748 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx],
2749 bit_width);
2750 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx],
2751 app_type);
2752 atomic_set(&this_adm.copp.acdb_id[port_idx][copp_idx],
2753 acdb_id);
Sachin Mohan Gadag3c3c5812018-07-20 15:46:54 +05302754 atomic_set(&this_adm.copp.session_type[port_idx][copp_idx],
2755 session_type);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302756 set_bit(ADM_STATUS_CALIBRATION_REQUIRED,
2757 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
2758 if ((path != ADM_PATH_COMPRESSED_RX) &&
2759 (path != ADM_PATH_COMPRESSED_TX))
2760 send_adm_custom_topology();
2761 }
2762
2763 if (this_adm.copp.adm_delay[port_idx][copp_idx] &&
2764 perf_mode == LEGACY_PCM_MODE) {
2765 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
2766 1);
2767 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
2768 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
2769 }
2770
2771 /* Create a COPP if port id are not enabled */
2772 if (atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]) == 0) {
2773 pr_debug("%s: open ADM: port_idx: %d, copp_idx: %d\n", __func__,
2774 port_idx, copp_idx);
2775 if ((topology == SRS_TRUMEDIA_TOPOLOGY_ID) &&
2776 perf_mode == LEGACY_PCM_MODE) {
2777 int res;
2778
2779 atomic_set(&this_adm.mem_map_index, ADM_SRS_TRUMEDIA);
2780 msm_dts_srs_tm_ion_memmap(&this_adm.outband_memmap);
2781 res = adm_memory_map_regions(&this_adm.outband_memmap.paddr, 0,
2782 (uint32_t *)&this_adm.outband_memmap.size, 1);
2783 if (res < 0) {
2784 pr_err("%s: SRS adm_memory_map_regions failed ! addr = 0x%pK, size = %d\n",
2785 __func__, (void *)this_adm.outband_memmap.paddr,
2786 (uint32_t)this_adm.outband_memmap.size);
2787 }
2788 }
2789 open.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2790 APR_HDR_LEN(APR_HDR_SIZE),
2791 APR_PKT_VER);
2792 open.hdr.pkt_size = sizeof(open);
2793 open.hdr.src_svc = APR_SVC_ADM;
2794 open.hdr.src_domain = APR_DOMAIN_APPS;
2795 open.hdr.src_port = tmp_port;
2796 open.hdr.dest_svc = APR_SVC_ADM;
2797 open.hdr.dest_domain = APR_DOMAIN_ADSP;
2798 open.hdr.dest_port = tmp_port;
2799 open.hdr.token = port_idx << 16 | copp_idx;
2800 open.hdr.opcode = ADM_CMD_DEVICE_OPEN_V5;
2801 open.flags = flags;
2802 open.mode_of_operation = path;
2803 open.endpoint_id_1 = tmp_port;
2804 open.endpoint_id_2 = 0xFFFF;
2805
Sachin Mohan Gadag3c3c5812018-07-20 15:46:54 +05302806 if (this_adm.ec_ref_rx && (path != 1) &&
2807 (afe_get_port_type(tmp_port) == MSM_AFE_PORT_TYPE_TX)) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302808 open.endpoint_id_2 = this_adm.ec_ref_rx;
2809 this_adm.ec_ref_rx = -1;
2810 }
2811
2812 open.topology_id = topology;
2813
2814 open.dev_num_channel = channel_mode & 0x00FF;
2815 open.bit_width = bit_width;
2816 WARN_ON((perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) &&
2817 (rate != ULL_SUPPORTED_SAMPLE_RATE));
2818 open.sample_rate = rate;
2819
Rohit kumar5d860f42019-02-01 18:01:12 +05302820 ret = adm_arrange_mch_map(&open, path, channel_mode,
2821 port_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302822
2823 if (ret)
2824 return ret;
2825
2826 pr_debug("%s: port_id=0x%x rate=%d topology_id=0x%X\n",
2827 __func__, open.endpoint_id_1, open.sample_rate,
2828 open.topology_id);
2829
2830 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2831
2832 if ((this_adm.num_ec_ref_rx_chans != 0) && (path != 1) &&
2833 (open.endpoint_id_2 != 0xFFFF)) {
2834 memset(&open_v6, 0,
2835 sizeof(struct adm_cmd_device_open_v6));
2836 memcpy(&open_v6, &open,
2837 sizeof(struct adm_cmd_device_open_v5));
2838 open_v6.hdr.opcode = ADM_CMD_DEVICE_OPEN_V6;
2839 open_v6.hdr.pkt_size = sizeof(open_v6);
2840 open_v6.dev_num_channel_eid2 =
2841 this_adm.num_ec_ref_rx_chans;
2842 this_adm.num_ec_ref_rx_chans = 0;
2843
2844 if (this_adm.ec_ref_rx_bit_width != 0) {
2845 open_v6.bit_width_eid2 =
2846 this_adm.ec_ref_rx_bit_width;
2847 this_adm.ec_ref_rx_bit_width = 0;
2848 } else {
2849 open_v6.bit_width_eid2 = bit_width;
2850 }
2851
2852 if (this_adm.ec_ref_rx_sampling_rate != 0) {
2853 open_v6.sample_rate_eid2 =
2854 this_adm.ec_ref_rx_sampling_rate;
2855 this_adm.ec_ref_rx_sampling_rate = 0;
2856 } else {
2857 open_v6.sample_rate_eid2 = rate;
2858 }
2859
2860 pr_debug("%s: eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
2861 __func__, open_v6.dev_num_channel_eid2,
2862 open_v6.bit_width_eid2,
2863 open_v6.sample_rate_eid2);
2864
2865 ret = adm_arrange_mch_ep2_map(&open_v6,
2866 open_v6.dev_num_channel_eid2);
2867
2868 if (ret)
2869 return ret;
2870
2871 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&open_v6);
2872 } else {
2873 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&open);
2874 }
2875 if (ret < 0) {
2876 pr_err("%s: port_id: 0x%x for[0x%x] failed %d\n",
2877 __func__, tmp_port, port_id, ret);
2878 return -EINVAL;
2879 }
2880 /* Wait for the callback with copp id */
2881 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
2882 atomic_read(&this_adm.copp.stat
2883 [port_idx][copp_idx]) >= 0,
2884 msecs_to_jiffies(TIMEOUT_MS));
2885 if (!ret) {
2886 pr_err("%s: ADM open timedout for port_id: 0x%x for [0x%x]\n",
2887 __func__, tmp_port, port_id);
2888 return -EINVAL;
2889 } else if (atomic_read(&this_adm.copp.stat
2890 [port_idx][copp_idx]) > 0) {
2891 pr_err("%s: DSP returned error[%s]\n",
2892 __func__, adsp_err_get_err_str(
2893 atomic_read(&this_adm.copp.stat
2894 [port_idx][copp_idx])));
2895 return adsp_err_get_lnx_err_code(
2896 atomic_read(&this_adm.copp.stat
2897 [port_idx][copp_idx]));
2898 }
2899 }
2900 atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
2901 return copp_idx;
2902}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302903EXPORT_SYMBOL(adm_open);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302904
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302905/**
2906 * adm_copp_mfc_cfg -
2907 * command to send ADM MFC config
2908 *
2909 * @port_id: Port ID number
2910 * @copp_idx: copp index assigned
2911 * @dst_sample_rate: sink sample rate
2912 *
2913 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302914void adm_copp_mfc_cfg(int port_id, int copp_idx, int dst_sample_rate)
2915{
2916 struct audproc_mfc_output_media_fmt mfc_cfg;
2917 struct adm_cmd_device_open_v5 open;
2918 int port_idx;
2919 int sz = 0;
2920 int rc = 0;
2921 int i = 0;
2922
2923 port_id = q6audio_convert_virtual_to_portid(port_id);
2924 port_idx = adm_validate_and_get_port_index(port_id);
2925
2926 if (port_idx < 0) {
2927 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
2928 goto fail_cmd;
2929 }
2930
2931 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
2932 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
2933 goto fail_cmd;
2934 }
2935
2936 sz = sizeof(struct audproc_mfc_output_media_fmt);
2937
2938 mfc_cfg.params.hdr.hdr_field =
2939 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2940 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
2941 mfc_cfg.params.hdr.pkt_size = sz;
2942 mfc_cfg.params.hdr.src_svc = APR_SVC_ADM;
2943 mfc_cfg.params.hdr.src_domain = APR_DOMAIN_APPS;
2944 mfc_cfg.params.hdr.src_port = port_id;
2945 mfc_cfg.params.hdr.dest_svc = APR_SVC_ADM;
2946 mfc_cfg.params.hdr.dest_domain = APR_DOMAIN_ADSP;
2947 mfc_cfg.params.hdr.dest_port =
2948 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
2949 mfc_cfg.params.hdr.token = port_idx << 16 | copp_idx;
2950 mfc_cfg.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
2951 mfc_cfg.params.payload_addr_lsw = 0;
2952 mfc_cfg.params.payload_addr_msw = 0;
2953 mfc_cfg.params.mem_map_handle = 0;
2954 mfc_cfg.params.payload_size = sizeof(mfc_cfg) -
2955 sizeof(mfc_cfg.params);
2956 mfc_cfg.data.module_id = AUDPROC_MODULE_ID_MFC;
2957 mfc_cfg.data.param_id =
2958 AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
2959 mfc_cfg.data.param_size = mfc_cfg.params.payload_size -
2960 sizeof(mfc_cfg.data);
2961 mfc_cfg.data.reserved = 0;
2962 mfc_cfg.sampling_rate = dst_sample_rate;
2963 mfc_cfg.bits_per_sample =
2964 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
2965 open.dev_num_channel = mfc_cfg.num_channels =
2966 atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
2967
2968 rc = adm_arrange_mch_map(&open, ADM_PATH_PLAYBACK,
Rohit kumar5d860f42019-02-01 18:01:12 +05302969 mfc_cfg.num_channels, port_idx);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302970 if (rc < 0) {
2971 pr_err("%s: unable to get channal map\n", __func__);
2972 goto fail_cmd;
2973 }
2974
2975 for (i = 0; i < mfc_cfg.num_channels; i++)
2976 mfc_cfg.channel_type[i] =
2977 (uint16_t) open.dev_channel_mapping[i];
2978
2979 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
2980
2981 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",
2982 __func__, port_idx, copp_idx,
2983 atomic_read(&this_adm.copp.rate[port_idx][copp_idx]),
2984 mfc_cfg.bits_per_sample, mfc_cfg.num_channels,
2985 mfc_cfg.sampling_rate);
2986
2987 rc = apr_send_pkt(this_adm.apr, (uint32_t *)&mfc_cfg);
2988
2989 if (rc < 0) {
2990 pr_err("%s: port_id: for[0x%x] failed %d\n",
2991 __func__, port_id, rc);
2992 goto fail_cmd;
2993 }
2994 /* Wait for the callback with copp id */
2995 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
2996 atomic_read(&this_adm.copp.stat
2997 [port_idx][copp_idx]) >= 0,
2998 msecs_to_jiffies(TIMEOUT_MS));
2999 if (!rc) {
3000 pr_err("%s: mfc_cfg Set params timed out for port_id: for [0x%x]\n",
3001 __func__, port_id);
3002 goto fail_cmd;
3003 } else if (atomic_read(&this_adm.copp.stat
3004 [port_idx][copp_idx]) > 0) {
3005 pr_err("%s: DSP returned error[%s]\n",
3006 __func__, adsp_err_get_err_str(
3007 atomic_read(&this_adm.copp.stat
3008 [port_idx][copp_idx])));
3009 goto fail_cmd;
3010 }
3011 rc = 0;
3012fail_cmd:
3013 return;
3014}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303015EXPORT_SYMBOL(adm_copp_mfc_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303016
3017static void route_set_opcode_matrix_id(
3018 struct adm_cmd_matrix_map_routings_v5 **route_addr,
3019 int path, uint32_t passthr_mode)
3020{
3021 struct adm_cmd_matrix_map_routings_v5 *route = *route_addr;
3022
3023 switch (path) {
3024 case ADM_PATH_PLAYBACK:
3025 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
3026 route->matrix_id = ADM_MATRIX_ID_AUDIO_RX;
3027 break;
3028 case ADM_PATH_LIVE_REC:
3029 if (passthr_mode == LISTEN) {
3030 route->hdr.opcode =
3031 ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3032 route->matrix_id = ADM_MATRIX_ID_LISTEN_TX;
3033 break;
3034 }
3035 /* fall through to set matrix id for non-listen case */
3036 case ADM_PATH_NONLIVE_REC:
3037 route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
3038 route->matrix_id = ADM_MATRIX_ID_AUDIO_TX;
3039 break;
3040 case ADM_PATH_COMPRESSED_RX:
3041 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3042 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_RX;
3043 break;
3044 case ADM_PATH_COMPRESSED_TX:
3045 route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
3046 route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_TX;
3047 break;
3048 default:
3049 pr_err("%s: Wrong path set[%d]\n", __func__, path);
3050 break;
3051 }
3052 pr_debug("%s: opcode 0x%x, matrix id %d\n",
3053 __func__, route->hdr.opcode, route->matrix_id);
3054}
3055
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303056/**
3057 * adm_matrix_map -
3058 * command to send ADM matrix map for ADM copp list
3059 *
3060 * @path: direction or ADM path type
3061 * @payload_map: have info of session id and associated copp_idx/num_copps
3062 * @perf_mode: performance mode like LL/ULL/..
3063 * @passthr_mode: flag to indicate passthrough mode
3064 *
3065 * Returns 0 on success or error on failure
3066 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303067int adm_matrix_map(int path, struct route_payload payload_map, int perf_mode,
3068 uint32_t passthr_mode)
3069{
3070 struct adm_cmd_matrix_map_routings_v5 *route;
3071 struct adm_session_map_node_v5 *node;
3072 uint16_t *copps_list;
3073 int cmd_size = 0;
3074 int ret = 0, i = 0;
3075 void *payload = NULL;
3076 void *matrix_map = NULL;
3077 int port_idx, copp_idx;
3078
3079 /* Assumes port_ids have already been validated during adm_open */
3080 cmd_size = (sizeof(struct adm_cmd_matrix_map_routings_v5) +
3081 sizeof(struct adm_session_map_node_v5) +
3082 (sizeof(uint32_t) * payload_map.num_copps));
3083 matrix_map = kzalloc(cmd_size, GFP_KERNEL);
3084 if (matrix_map == NULL) {
3085 pr_err("%s: Mem alloc failed\n", __func__);
3086 ret = -EINVAL;
3087 return ret;
3088 }
3089 route = (struct adm_cmd_matrix_map_routings_v5 *)matrix_map;
3090
3091 route->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3092 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
3093 route->hdr.pkt_size = cmd_size;
3094 route->hdr.src_svc = 0;
3095 route->hdr.src_domain = APR_DOMAIN_APPS;
3096 route->hdr.src_port = 0; /* Ignored */;
3097 route->hdr.dest_svc = APR_SVC_ADM;
3098 route->hdr.dest_domain = APR_DOMAIN_ADSP;
3099 route->hdr.dest_port = 0; /* Ignored */;
3100 route->hdr.token = 0;
3101 route->num_sessions = 1;
3102 route_set_opcode_matrix_id(&route, path, passthr_mode);
3103
3104 payload = ((u8 *)matrix_map +
3105 sizeof(struct adm_cmd_matrix_map_routings_v5));
3106 node = (struct adm_session_map_node_v5 *)payload;
3107
3108 node->session_id = payload_map.session_id;
3109 node->num_copps = payload_map.num_copps;
3110 payload = (u8 *)node + sizeof(struct adm_session_map_node_v5);
3111 copps_list = (uint16_t *)payload;
3112 for (i = 0; i < payload_map.num_copps; i++) {
3113 port_idx =
3114 adm_validate_and_get_port_index(payload_map.port_id[i]);
3115 if (port_idx < 0) {
3116 pr_err("%s: Invalid port_id 0x%x\n", __func__,
3117 payload_map.port_id[i]);
3118 ret = -EINVAL;
3119 goto fail_cmd;
3120 }
3121 copp_idx = payload_map.copp_idx[i];
3122 copps_list[i] = atomic_read(&this_adm.copp.id[port_idx]
3123 [copp_idx]);
3124 }
3125 atomic_set(&this_adm.matrix_map_stat, -1);
3126
3127 ret = apr_send_pkt(this_adm.apr, (uint32_t *)matrix_map);
3128 if (ret < 0) {
3129 pr_err("%s: routing for syream %d failed ret %d\n",
3130 __func__, payload_map.session_id, ret);
3131 ret = -EINVAL;
3132 goto fail_cmd;
3133 }
3134 ret = wait_event_timeout(this_adm.matrix_map_wait,
3135 atomic_read(&this_adm.matrix_map_stat) >= 0,
3136 msecs_to_jiffies(TIMEOUT_MS));
3137 if (!ret) {
3138 pr_err("%s: routing for syream %d failed\n", __func__,
3139 payload_map.session_id);
3140 ret = -EINVAL;
3141 goto fail_cmd;
3142 } else if (atomic_read(&this_adm.matrix_map_stat) > 0) {
3143 pr_err("%s: DSP returned error[%s]\n", __func__,
3144 adsp_err_get_err_str(atomic_read(
3145 &this_adm.matrix_map_stat)));
3146 ret = adsp_err_get_lnx_err_code(
3147 atomic_read(&this_adm.matrix_map_stat));
3148 goto fail_cmd;
3149 }
3150
3151 if ((perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) &&
3152 (path != ADM_PATH_COMPRESSED_RX)) {
3153 for (i = 0; i < payload_map.num_copps; i++) {
3154 port_idx = afe_get_port_index(payload_map.port_id[i]);
3155 copp_idx = payload_map.copp_idx[i];
3156 if (port_idx < 0 || copp_idx < 0 ||
3157 (copp_idx > MAX_COPPS_PER_PORT - 1)) {
3158 pr_err("%s: Invalid idx port_idx %d copp_idx %d\n",
3159 __func__, port_idx, copp_idx);
3160 continue;
3161 }
3162 rtac_add_adm_device(payload_map.port_id[i],
3163 atomic_read(&this_adm.copp.id
3164 [port_idx][copp_idx]),
3165 get_cal_path(path),
3166 payload_map.session_id,
3167 payload_map.app_type[i],
3168 payload_map.acdb_dev_id[i]);
3169
3170 if (!test_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3171 (void *)&this_adm.copp.adm_status[port_idx]
3172 [copp_idx])) {
3173 pr_debug("%s: adm copp[0x%x][%d] already sent",
3174 __func__, port_idx, copp_idx);
3175 continue;
3176 }
3177 send_adm_cal(payload_map.port_id[i], copp_idx,
3178 get_cal_path(path), perf_mode,
3179 payload_map.app_type[i],
3180 payload_map.acdb_dev_id[i],
Aditya Bavanarifcd80ec2018-01-08 13:16:32 +05303181 payload_map.sample_rate[i],
3182 passthr_mode);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303183 /* ADM COPP calibration is already sent */
3184 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3185 (void *)&this_adm.copp.
3186 adm_status[port_idx][copp_idx]);
3187 pr_debug("%s: copp_id: %d\n", __func__,
3188 atomic_read(&this_adm.copp.id[port_idx]
3189 [copp_idx]));
3190 }
3191 }
3192
3193fail_cmd:
3194 kfree(matrix_map);
3195 return ret;
3196}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303197EXPORT_SYMBOL(adm_matrix_map);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303198
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303199/**
3200 * adm_ec_ref_rx_id -
3201 * Update EC ref port ID
3202 *
3203 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303204void adm_ec_ref_rx_id(int port_id)
3205{
3206 this_adm.ec_ref_rx = port_id;
3207 pr_debug("%s: ec_ref_rx:%d\n", __func__, this_adm.ec_ref_rx);
3208}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303209EXPORT_SYMBOL(adm_ec_ref_rx_id);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303210
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303211/**
3212 * adm_num_ec_ref_rx_chans -
3213 * Update EC ref number of channels
3214 *
3215 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303216void adm_num_ec_ref_rx_chans(int num_chans)
3217{
3218 this_adm.num_ec_ref_rx_chans = num_chans;
3219 pr_debug("%s: num_ec_ref_rx_chans:%d\n",
3220 __func__, this_adm.num_ec_ref_rx_chans);
3221}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303222EXPORT_SYMBOL(adm_num_ec_ref_rx_chans);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303223
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303224/**
3225 * adm_ec_ref_rx_bit_width -
3226 * Update EC ref bit_width
3227 *
3228 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303229void adm_ec_ref_rx_bit_width(int bit_width)
3230{
3231 this_adm.ec_ref_rx_bit_width = bit_width;
3232 pr_debug("%s: ec_ref_rx_bit_width:%d\n",
3233 __func__, this_adm.ec_ref_rx_bit_width);
3234}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303235EXPORT_SYMBOL(adm_ec_ref_rx_bit_width);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303236
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303237/**
3238 * adm_ec_ref_rx_sampling_rate -
3239 * Update EC ref sample rate
3240 *
3241 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303242void adm_ec_ref_rx_sampling_rate(int sampling_rate)
3243{
3244 this_adm.ec_ref_rx_sampling_rate = sampling_rate;
3245 pr_debug("%s: ec_ref_rx_sampling_rate:%d\n",
3246 __func__, this_adm.ec_ref_rx_sampling_rate);
3247}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303248EXPORT_SYMBOL(adm_ec_ref_rx_sampling_rate);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303249
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303250/**
3251 * adm_close -
3252 * command to close ADM copp
3253 *
3254 * @port_id: Port ID number
3255 * @perf_mode: performance mode like LL/ULL/..
3256 * @copp_idx: copp index assigned
3257 *
3258 * Returns 0 on success or error on failure
3259 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303260int adm_close(int port_id, int perf_mode, int copp_idx)
3261{
3262 struct apr_hdr close;
3263
3264 int ret = 0, port_idx;
3265 int copp_id = RESET_COPP_ID;
3266
3267 pr_debug("%s: port_id=0x%x perf_mode: %d copp_idx: %d\n", __func__,
3268 port_id, perf_mode, copp_idx);
3269
3270 port_id = q6audio_convert_virtual_to_portid(port_id);
3271 port_idx = adm_validate_and_get_port_index(port_id);
3272 if (port_idx < 0) {
3273 pr_err("%s: Invalid port_id 0x%x\n",
3274 __func__, port_id);
3275 return -EINVAL;
3276 }
3277
3278 if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
3279 pr_err("%s: Invalid copp idx: %d\n", __func__, copp_idx);
3280 return -EINVAL;
3281 }
3282
Rohit kumar5d860f42019-02-01 18:01:12 +05303283 port_channel_map[port_idx].set_channel_map = false;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303284 if (this_adm.copp.adm_delay[port_idx][copp_idx] && perf_mode
3285 == LEGACY_PCM_MODE) {
3286 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
3287 1);
3288 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
3289 wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
3290 }
3291
3292 atomic_dec(&this_adm.copp.cnt[port_idx][copp_idx]);
3293 if (!(atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]))) {
3294 copp_id = adm_get_copp_id(port_idx, copp_idx);
3295 pr_debug("%s: Closing ADM port_idx:%d copp_idx:%d copp_id:0x%x\n",
3296 __func__, port_idx, copp_idx, copp_id);
3297 if ((!perf_mode) && (this_adm.outband_memmap.paddr != 0) &&
3298 (atomic_read(&this_adm.copp.topology[port_idx][copp_idx]) ==
3299 SRS_TRUMEDIA_TOPOLOGY_ID)) {
3300 atomic_set(&this_adm.mem_map_index,
3301 ADM_SRS_TRUMEDIA);
3302 ret = adm_memory_unmap_regions();
3303 if (ret < 0) {
3304 pr_err("%s: adm mem unmmap err %d",
3305 __func__, ret);
3306 } else {
3307 atomic_set(&this_adm.mem_map_handles
3308 [ADM_SRS_TRUMEDIA], 0);
3309 }
3310 }
3311
3312
3313 if ((afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX) &&
3314 this_adm.sourceTrackingData.memmap.paddr) {
3315 atomic_set(&this_adm.mem_map_index,
3316 ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
3317 ret = adm_memory_unmap_regions();
3318 if (ret < 0) {
3319 pr_err("%s: adm mem unmmap err %d",
3320 __func__, ret);
3321 }
3322 msm_audio_ion_free(
3323 this_adm.sourceTrackingData.ion_client,
3324 this_adm.sourceTrackingData.ion_handle);
3325 this_adm.sourceTrackingData.ion_client = NULL;
3326 this_adm.sourceTrackingData.ion_handle = NULL;
3327 this_adm.sourceTrackingData.memmap.size = 0;
3328 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
3329 this_adm.sourceTrackingData.memmap.paddr = 0;
3330 this_adm.sourceTrackingData.apr_cmd_status = -1;
3331 atomic_set(&this_adm.mem_map_handles[
3332 ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
3333 }
3334
3335 close.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3336 APR_HDR_LEN(APR_HDR_SIZE),
3337 APR_PKT_VER);
3338 close.pkt_size = sizeof(close);
3339 close.src_svc = APR_SVC_ADM;
3340 close.src_domain = APR_DOMAIN_APPS;
3341 close.src_port = port_id;
3342 close.dest_svc = APR_SVC_ADM;
3343 close.dest_domain = APR_DOMAIN_ADSP;
3344 close.dest_port = copp_id;
3345 close.token = port_idx << 16 | copp_idx;
3346 close.opcode = ADM_CMD_DEVICE_CLOSE_V5;
3347
3348 atomic_set(&this_adm.copp.id[port_idx][copp_idx],
3349 RESET_COPP_ID);
3350 atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
3351 atomic_set(&this_adm.copp.topology[port_idx][copp_idx], 0);
3352 atomic_set(&this_adm.copp.mode[port_idx][copp_idx], 0);
3353 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3354 atomic_set(&this_adm.copp.rate[port_idx][copp_idx], 0);
3355 atomic_set(&this_adm.copp.channels[port_idx][copp_idx], 0);
3356 atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx], 0);
3357 atomic_set(&this_adm.copp.app_type[port_idx][copp_idx], 0);
Sachin Mohan Gadag3c3c5812018-07-20 15:46:54 +05303358 atomic_set(&this_adm.copp.session_type[port_idx][copp_idx], 0);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303359
3360 clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
3361 (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
3362
3363 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&close);
3364 if (ret < 0) {
3365 pr_err("%s: ADM close failed %d\n", __func__, ret);
3366 return -EINVAL;
3367 }
3368
3369 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3370 atomic_read(&this_adm.copp.stat
3371 [port_idx][copp_idx]) >= 0,
3372 msecs_to_jiffies(TIMEOUT_MS));
3373 if (!ret) {
3374 pr_err("%s: ADM cmd Route timedout for port 0x%x\n",
3375 __func__, port_id);
3376 return -EINVAL;
3377 } else if (atomic_read(&this_adm.copp.stat
3378 [port_idx][copp_idx]) > 0) {
3379 pr_err("%s: DSP returned error[%s]\n",
3380 __func__, adsp_err_get_err_str(
3381 atomic_read(&this_adm.copp.stat
3382 [port_idx][copp_idx])));
3383 return adsp_err_get_lnx_err_code(
3384 atomic_read(&this_adm.copp.stat
3385 [port_idx][copp_idx]));
3386 }
3387 }
3388
3389 if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) {
3390 pr_debug("%s: remove adm device from rtac\n", __func__);
3391 rtac_remove_adm_device(port_id, copp_id);
3392 }
3393 return 0;
3394}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303395EXPORT_SYMBOL(adm_close);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303396
3397int send_rtac_audvol_cal(void)
3398{
3399 int ret = 0;
3400 int ret2 = 0;
3401 int i = 0;
3402 int copp_idx, port_idx, acdb_id, app_id, path;
3403 struct cal_block_data *cal_block = NULL;
3404 struct audio_cal_info_audvol *audvol_cal_info = NULL;
3405 struct rtac_adm rtac_adm_data;
3406
3407 mutex_lock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3408
3409 cal_block = cal_utils_get_only_cal_block(
3410 this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]);
Vikram Pandurangad3b58cc2017-09-27 12:17:36 -07003411 if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303412 pr_err("%s: can't find cal block!\n", __func__);
3413 goto unlock;
3414 }
3415
3416 audvol_cal_info = cal_block->cal_info;
3417 if (audvol_cal_info == NULL) {
3418 pr_err("%s: audvol_cal_info is NULL!\n", __func__);
3419 goto unlock;
3420 }
3421
3422 get_rtac_adm_data(&rtac_adm_data);
3423 for (; i < rtac_adm_data.num_of_dev; i++) {
3424
3425 acdb_id = rtac_adm_data.device[i].acdb_dev_id;
3426 if (acdb_id == 0)
3427 acdb_id = audvol_cal_info->acdb_id;
3428
3429 app_id = rtac_adm_data.device[i].app_type;
3430 if (app_id == 0)
3431 app_id = audvol_cal_info->app_type;
3432
3433 path = afe_get_port_type(rtac_adm_data.device[i].afe_port);
3434 if ((acdb_id == audvol_cal_info->acdb_id) &&
3435 (app_id == audvol_cal_info->app_type) &&
3436 (path == audvol_cal_info->path)) {
3437
3438 if (adm_get_indexes_from_copp_id(rtac_adm_data.
3439 device[i].copp, &copp_idx, &port_idx) != 0) {
3440 pr_debug("%s: Copp Id %d is not active\n",
3441 __func__,
3442 rtac_adm_data.device[i].copp);
3443 continue;
3444 }
3445
3446 ret2 = adm_remap_and_send_cal_block(ADM_RTAC_AUDVOL_CAL,
3447 rtac_adm_data.device[i].afe_port,
3448 copp_idx, cal_block,
3449 atomic_read(&this_adm.copp.
3450 mode[port_idx][copp_idx]),
3451 audvol_cal_info->app_type,
3452 audvol_cal_info->acdb_id,
3453 atomic_read(&this_adm.copp.
3454 rate[port_idx][copp_idx]));
3455 if (ret2 < 0) {
3456 pr_debug("%s: remap and send failed for copp Id %d, acdb id %d, app type %d, path %d\n",
3457 __func__, rtac_adm_data.device[i].copp,
3458 audvol_cal_info->acdb_id,
3459 audvol_cal_info->app_type,
3460 audvol_cal_info->path);
3461 ret = ret2;
3462 }
3463 }
3464 }
3465unlock:
3466 mutex_unlock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
3467 return ret;
3468}
3469
3470int adm_map_rtac_block(struct rtac_cal_block_data *cal_block)
3471{
3472 int result = 0;
3473
3474 pr_debug("%s:\n", __func__);
3475
3476 if (cal_block == NULL) {
3477 pr_err("%s: cal_block is NULL!\n",
3478 __func__);
3479 result = -EINVAL;
3480 goto done;
3481 }
3482
3483 if (cal_block->cal_data.paddr == 0) {
3484 pr_debug("%s: No address to map!\n",
3485 __func__);
3486 result = -EINVAL;
3487 goto done;
3488 }
3489
3490 if (cal_block->map_data.map_size == 0) {
3491 pr_debug("%s: map size is 0!\n",
3492 __func__);
3493 result = -EINVAL;
3494 goto done;
3495 }
3496
3497 /* valid port ID needed for callback use primary I2S */
3498 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3499 result = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3500 &cal_block->map_data.map_size, 1);
3501 if (result < 0) {
3502 pr_err("%s: RTAC mmap did not work! size = %d result %d\n",
3503 __func__,
3504 cal_block->map_data.map_size, result);
3505 pr_debug("%s: RTAC mmap did not work! addr = 0x%pK, size = %d\n",
3506 __func__,
3507 &cal_block->cal_data.paddr,
3508 cal_block->map_data.map_size);
3509 goto done;
3510 }
3511
3512 cal_block->map_data.map_handle = atomic_read(
3513 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]);
3514done:
3515 return result;
3516}
3517
3518int adm_unmap_rtac_block(uint32_t *mem_map_handle)
3519{
3520 int result = 0;
3521
3522 pr_debug("%s:\n", __func__);
3523
3524 if (mem_map_handle == NULL) {
3525 pr_debug("%s: Map handle is NULL, nothing to unmap\n",
3526 __func__);
3527 goto done;
3528 }
3529
3530 if (*mem_map_handle == 0) {
3531 pr_debug("%s: Map handle is 0, nothing to unmap\n",
3532 __func__);
3533 goto done;
3534 }
3535
3536 if (*mem_map_handle != atomic_read(
3537 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL])) {
3538 pr_err("%s: Map handles do not match! Unmapping RTAC, RTAC map 0x%x, ADM map 0x%x\n",
3539 __func__, *mem_map_handle, atomic_read(
3540 &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]));
3541
3542 /* if mismatch use handle passed in to unmap */
3543 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL],
3544 *mem_map_handle);
3545 }
3546
3547 /* valid port ID needed for callback use primary I2S */
3548 atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
3549 result = adm_memory_unmap_regions();
3550 if (result < 0) {
3551 pr_debug("%s: adm_memory_unmap_regions failed, error %d\n",
3552 __func__, result);
3553 } else {
3554 atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL], 0);
3555 *mem_map_handle = 0;
3556 }
3557done:
3558 return result;
3559}
3560
3561static int get_cal_type_index(int32_t cal_type)
3562{
3563 int ret = -EINVAL;
3564
3565 switch (cal_type) {
3566 case ADM_AUDPROC_CAL_TYPE:
3567 ret = ADM_AUDPROC_CAL;
3568 break;
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05303569 case ADM_LSM_AUDPROC_CAL_TYPE:
3570 ret = ADM_LSM_AUDPROC_CAL;
3571 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303572 case ADM_AUDVOL_CAL_TYPE:
3573 ret = ADM_AUDVOL_CAL;
3574 break;
3575 case ADM_CUST_TOPOLOGY_CAL_TYPE:
3576 ret = ADM_CUSTOM_TOP_CAL;
3577 break;
3578 case ADM_RTAC_INFO_CAL_TYPE:
3579 ret = ADM_RTAC_INFO_CAL;
3580 break;
3581 case ADM_RTAC_APR_CAL_TYPE:
3582 ret = ADM_RTAC_APR_CAL;
3583 break;
3584 case ADM_RTAC_AUDVOL_CAL_TYPE:
3585 ret = ADM_RTAC_AUDVOL_CAL;
3586 break;
Bhalchandra Gajareface2762018-05-10 14:16:49 -07003587 case ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE:
3588 ret = ADM_LSM_AUDPROC_PERSISTENT_CAL;
3589 break;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303590 default:
3591 pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
3592 }
3593 return ret;
3594}
3595
3596static int adm_alloc_cal(int32_t cal_type, size_t data_size, void *data)
3597{
3598 int ret = 0;
3599 int cal_index;
3600
3601 pr_debug("%s:\n", __func__);
3602
3603 cal_index = get_cal_type_index(cal_type);
3604 if (cal_index < 0) {
3605 pr_err("%s: could not get cal index %d!\n",
3606 __func__, cal_index);
3607 ret = -EINVAL;
3608 goto done;
3609 }
3610
3611 ret = cal_utils_alloc_cal(data_size, data,
3612 this_adm.cal_data[cal_index], 0, NULL);
3613 if (ret < 0) {
3614 pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
3615 __func__, ret, cal_type);
3616 ret = -EINVAL;
3617 goto done;
3618 }
3619done:
3620 return ret;
3621}
3622
3623static int adm_dealloc_cal(int32_t cal_type, size_t data_size, void *data)
3624{
3625 int ret = 0;
3626 int cal_index;
3627
3628 pr_debug("%s:\n", __func__);
3629
3630 cal_index = get_cal_type_index(cal_type);
3631 if (cal_index < 0) {
3632 pr_err("%s: could not get cal index %d!\n",
3633 __func__, cal_index);
3634 ret = -EINVAL;
3635 goto done;
3636 }
3637
3638 ret = cal_utils_dealloc_cal(data_size, data,
3639 this_adm.cal_data[cal_index]);
3640 if (ret < 0) {
3641 pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
3642 __func__, ret, cal_type);
3643 ret = -EINVAL;
3644 goto done;
3645 }
3646done:
3647 return ret;
3648}
3649
3650static int adm_set_cal(int32_t cal_type, size_t data_size, void *data)
3651{
3652 int ret = 0;
3653 int cal_index;
3654
3655 pr_debug("%s:\n", __func__);
3656
3657 cal_index = get_cal_type_index(cal_type);
3658 if (cal_index < 0) {
3659 pr_err("%s: could not get cal index %d!\n",
3660 __func__, cal_index);
3661 ret = -EINVAL;
3662 goto done;
3663 }
3664
3665 ret = cal_utils_set_cal(data_size, data,
3666 this_adm.cal_data[cal_index], 0, NULL);
3667 if (ret < 0) {
3668 pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
3669 __func__, ret, cal_type);
3670 ret = -EINVAL;
3671 goto done;
3672 }
3673
3674 if (cal_index == ADM_CUSTOM_TOP_CAL) {
3675 mutex_lock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3676 this_adm.set_custom_topology = 1;
3677 mutex_unlock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
3678 } else if (cal_index == ADM_RTAC_AUDVOL_CAL) {
3679 send_rtac_audvol_cal();
3680 }
3681done:
3682 return ret;
3683}
3684
3685static int adm_map_cal_data(int32_t cal_type,
3686 struct cal_block_data *cal_block)
3687{
3688 int ret = 0;
3689 int cal_index;
3690
3691 pr_debug("%s:\n", __func__);
3692
3693 cal_index = get_cal_type_index(cal_type);
3694 if (cal_index < 0) {
3695 pr_err("%s: could not get cal index %d!\n",
3696 __func__, cal_index);
3697 ret = -EINVAL;
3698 goto done;
3699 }
3700
3701 atomic_set(&this_adm.mem_map_index, cal_index);
3702 ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
3703 (uint32_t *)&cal_block->map_data.map_size, 1);
3704 if (ret < 0) {
3705 pr_err("%s: map did not work! cal_type %i ret %d\n",
3706 __func__, cal_index, ret);
3707 ret = -ENODEV;
3708 goto done;
3709 }
3710 cal_block->map_data.q6map_handle = atomic_read(&this_adm.
3711 mem_map_handles[cal_index]);
3712done:
3713 return ret;
3714}
3715
3716static int adm_unmap_cal_data(int32_t cal_type,
3717 struct cal_block_data *cal_block)
3718{
3719 int ret = 0;
3720 int cal_index;
3721
3722 pr_debug("%s:\n", __func__);
3723
3724 cal_index = get_cal_type_index(cal_type);
3725 if (cal_index < 0) {
3726 pr_err("%s: could not get cal index %d!\n",
3727 __func__, cal_index);
3728 ret = -EINVAL;
3729 goto done;
3730 }
3731
3732 if (cal_block == NULL) {
3733 pr_err("%s: Cal block is NULL!\n",
3734 __func__);
3735 goto done;
3736 }
3737
3738 if (cal_block->map_data.q6map_handle == 0) {
3739 pr_err("%s: Map handle is NULL, nothing to unmap\n",
3740 __func__);
3741 goto done;
3742 }
3743
3744 atomic_set(&this_adm.mem_map_handles[cal_index],
3745 cal_block->map_data.q6map_handle);
3746 atomic_set(&this_adm.mem_map_index, cal_index);
3747 ret = adm_memory_unmap_regions();
3748 if (ret < 0) {
3749 pr_err("%s: unmap did not work! cal_type %i ret %d\n",
3750 __func__, cal_index, ret);
3751 ret = -ENODEV;
3752 goto done;
3753 }
3754 cal_block->map_data.q6map_handle = 0;
3755done:
3756 return ret;
3757}
3758
3759static void adm_delete_cal_data(void)
3760{
3761 pr_debug("%s:\n", __func__);
3762
3763 cal_utils_destroy_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data);
3764}
3765
3766static int adm_init_cal_data(void)
3767{
3768 int ret = 0;
3769 struct cal_type_info cal_type_info[] = {
3770 {{ADM_CUST_TOPOLOGY_CAL_TYPE,
3771 {adm_alloc_cal, adm_dealloc_cal, NULL,
3772 adm_set_cal, NULL, NULL} },
3773 {adm_map_cal_data, adm_unmap_cal_data,
3774 cal_utils_match_buf_num} },
3775
3776 {{ADM_AUDPROC_CAL_TYPE,
3777 {adm_alloc_cal, adm_dealloc_cal, NULL,
3778 adm_set_cal, NULL, NULL} },
3779 {adm_map_cal_data, adm_unmap_cal_data,
3780 cal_utils_match_buf_num} },
3781
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05303782 {{ADM_LSM_AUDPROC_CAL_TYPE,
3783 {adm_alloc_cal, adm_dealloc_cal, NULL,
3784 adm_set_cal, NULL, NULL} },
3785 {adm_map_cal_data, adm_unmap_cal_data,
3786 cal_utils_match_buf_num} },
3787
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303788 {{ADM_AUDVOL_CAL_TYPE,
3789 {adm_alloc_cal, adm_dealloc_cal, NULL,
3790 adm_set_cal, NULL, NULL} },
3791 {adm_map_cal_data, adm_unmap_cal_data,
3792 cal_utils_match_buf_num} },
3793
3794 {{ADM_RTAC_INFO_CAL_TYPE,
3795 {NULL, NULL, NULL, NULL, NULL, NULL} },
3796 {NULL, NULL, cal_utils_match_buf_num} },
3797
3798 {{ADM_RTAC_APR_CAL_TYPE,
3799 {NULL, NULL, NULL, NULL, NULL, NULL} },
3800 {NULL, NULL, cal_utils_match_buf_num} },
3801
3802 {{SRS_TRUMEDIA_CAL_TYPE,
3803 {NULL, NULL, NULL, NULL, NULL, NULL} },
3804 {NULL, NULL, cal_utils_match_buf_num} },
3805
3806 {{ADM_RTAC_AUDVOL_CAL_TYPE,
3807 {adm_alloc_cal, adm_dealloc_cal, NULL,
3808 adm_set_cal, NULL, NULL} },
3809 {adm_map_cal_data, adm_unmap_cal_data,
3810 cal_utils_match_buf_num} },
Bhalchandra Gajareface2762018-05-10 14:16:49 -07003811
3812 {{ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE,
3813 {adm_alloc_cal, adm_dealloc_cal, NULL,
3814 adm_set_cal, NULL, NULL} },
3815 {adm_map_cal_data, adm_unmap_cal_data,
3816 cal_utils_match_buf_num} },
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303817 };
3818 pr_debug("%s:\n", __func__);
3819
3820 ret = cal_utils_create_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data,
3821 cal_type_info);
3822 if (ret < 0) {
3823 pr_err("%s: could not create cal type! ret %d\n",
3824 __func__, ret);
3825 ret = -EINVAL;
3826 goto err;
3827 }
3828
3829 return ret;
3830err:
3831 adm_delete_cal_data();
3832 return ret;
3833}
3834
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303835/**
3836 * adm_set_volume -
3837 * command to set volume on ADM copp
3838 *
3839 * @port_id: Port ID number
3840 * @copp_idx: copp index assigned
3841 * @volume: gain value to set
3842 *
3843 * Returns 0 on success or error on failure
3844 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303845int adm_set_volume(int port_id, int copp_idx, int volume)
3846{
3847 struct audproc_volume_ctrl_master_gain audproc_vol;
3848 int sz = 0;
3849 int rc = 0;
3850 int port_idx;
3851
3852 pr_debug("%s: port_id %d, volume %d\n", __func__, port_id, volume);
3853 port_id = afe_convert_virtual_to_portid(port_id);
3854 port_idx = adm_validate_and_get_port_index(port_id);
3855 if (port_idx < 0) {
3856 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
3857 rc = -EINVAL;
3858 goto fail_cmd;
3859 }
3860
3861 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3862 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3863 return -EINVAL;
3864 }
3865
3866 sz = sizeof(struct audproc_volume_ctrl_master_gain);
3867 audproc_vol.params.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3868 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
3869 audproc_vol.params.hdr.pkt_size = sz;
3870 audproc_vol.params.hdr.src_svc = APR_SVC_ADM;
3871 audproc_vol.params.hdr.src_domain = APR_DOMAIN_APPS;
3872 audproc_vol.params.hdr.src_port = port_id;
3873 audproc_vol.params.hdr.dest_svc = APR_SVC_ADM;
3874 audproc_vol.params.hdr.dest_domain = APR_DOMAIN_ADSP;
3875 audproc_vol.params.hdr.dest_port =
3876 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
3877 audproc_vol.params.hdr.token = port_idx << 16 | copp_idx;
3878 audproc_vol.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
3879 audproc_vol.params.payload_addr_lsw = 0;
3880 audproc_vol.params.payload_addr_msw = 0;
3881 audproc_vol.params.mem_map_handle = 0;
3882 audproc_vol.params.payload_size = sizeof(audproc_vol) -
3883 sizeof(audproc_vol.params);
3884 audproc_vol.data.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
3885 audproc_vol.data.param_id = AUDPROC_PARAM_ID_VOL_CTRL_MASTER_GAIN;
3886 audproc_vol.data.param_size = audproc_vol.params.payload_size -
3887 sizeof(audproc_vol.data);
3888 audproc_vol.data.reserved = 0;
3889 audproc_vol.master_gain = volume;
3890
3891 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3892 rc = apr_send_pkt(this_adm.apr, (uint32_t *)&audproc_vol);
3893 if (rc < 0) {
3894 pr_err("%s: Set params failed port = %#x\n",
3895 __func__, port_id);
3896 rc = -EINVAL;
3897 goto fail_cmd;
3898 }
3899 /* Wait for the callback */
3900 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
3901 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
3902 msecs_to_jiffies(TIMEOUT_MS));
3903 if (!rc) {
3904 pr_err("%s: Vol cntrl Set params timed out port = %#x\n",
3905 __func__, port_id);
3906 rc = -EINVAL;
3907 goto fail_cmd;
3908 } else if (atomic_read(&this_adm.copp.stat
3909 [port_idx][copp_idx]) > 0) {
3910 pr_err("%s: DSP returned error[%s]\n",
3911 __func__, adsp_err_get_err_str(
3912 atomic_read(&this_adm.copp.stat
3913 [port_idx][copp_idx])));
3914 rc = adsp_err_get_lnx_err_code(
3915 atomic_read(&this_adm.copp.stat
3916 [port_idx][copp_idx]));
3917 goto fail_cmd;
3918 }
3919 rc = 0;
3920fail_cmd:
3921 return rc;
3922}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303923EXPORT_SYMBOL(adm_set_volume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303924
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05303925/**
3926 * adm_set_softvolume -
3927 * command to set softvolume
3928 *
3929 * @port_id: Port ID number
3930 * @copp_idx: copp index assigned
3931 * @softvol_param: Params to set for softvolume
3932 *
3933 * Returns 0 on success or error on failure
3934 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303935int adm_set_softvolume(int port_id, int copp_idx,
3936 struct audproc_softvolume_params *softvol_param)
3937{
3938 struct audproc_soft_step_volume_params audproc_softvol;
3939 int sz = 0;
3940 int rc = 0;
3941 int port_idx;
3942
3943 pr_debug("%s: period %d step %d curve %d\n", __func__,
3944 softvol_param->period, softvol_param->step,
3945 softvol_param->rampingcurve);
3946
3947 port_id = afe_convert_virtual_to_portid(port_id);
3948 port_idx = adm_validate_and_get_port_index(port_id);
3949 if (port_idx < 0) {
3950 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
3951 rc = -EINVAL;
3952 goto fail_cmd;
3953 }
3954
3955 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
3956 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
3957 return -EINVAL;
3958 }
3959
3960 sz = sizeof(struct audproc_soft_step_volume_params);
3961
3962 audproc_softvol.params.hdr.hdr_field =
3963 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3964 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
3965 audproc_softvol.params.hdr.pkt_size = sz;
3966 audproc_softvol.params.hdr.src_svc = APR_SVC_ADM;
3967 audproc_softvol.params.hdr.src_domain = APR_DOMAIN_APPS;
3968 audproc_softvol.params.hdr.src_port = port_id;
3969 audproc_softvol.params.hdr.dest_svc = APR_SVC_ADM;
3970 audproc_softvol.params.hdr.dest_domain = APR_DOMAIN_ADSP;
3971 audproc_softvol.params.hdr.dest_port =
3972 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
3973 audproc_softvol.params.hdr.token = port_idx << 16 | copp_idx;
3974 audproc_softvol.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
3975 audproc_softvol.params.payload_addr_lsw = 0;
3976 audproc_softvol.params.payload_addr_msw = 0;
3977 audproc_softvol.params.mem_map_handle = 0;
3978 audproc_softvol.params.payload_size = sizeof(audproc_softvol) -
3979 sizeof(audproc_softvol.params);
3980 audproc_softvol.data.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
3981 audproc_softvol.data.param_id =
3982 AUDPROC_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
3983 audproc_softvol.data.param_size = audproc_softvol.params.payload_size -
3984 sizeof(audproc_softvol.data);
3985 audproc_softvol.data.reserved = 0;
3986 audproc_softvol.period = softvol_param->period;
3987 audproc_softvol.step = softvol_param->step;
3988 audproc_softvol.ramping_curve = softvol_param->rampingcurve;
3989
3990 pr_debug("%s: period %d, step %d, curve %d\n", __func__,
3991 audproc_softvol.period, audproc_softvol.step,
3992 audproc_softvol.ramping_curve);
3993
3994 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
3995 rc = apr_send_pkt(this_adm.apr, (uint32_t *)&audproc_softvol);
3996 if (rc < 0) {
3997 pr_err("%s: Set params failed port = %#x\n",
3998 __func__, port_id);
3999 rc = -EINVAL;
4000 goto fail_cmd;
4001 }
4002 /* Wait for the callback */
4003 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
4004 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
4005 msecs_to_jiffies(TIMEOUT_MS));
4006 if (!rc) {
4007 pr_err("%s: Soft volume Set params timed out port = %#x\n",
4008 __func__, port_id);
4009 rc = -EINVAL;
4010 goto fail_cmd;
4011 } else if (atomic_read(&this_adm.copp.stat
4012 [port_idx][copp_idx]) > 0) {
4013 pr_err("%s: DSP returned error[%s]\n",
4014 __func__, adsp_err_get_err_str(
4015 atomic_read(&this_adm.copp.stat
4016 [port_idx][copp_idx])));
4017 rc = adsp_err_get_lnx_err_code(
4018 atomic_read(&this_adm.copp.stat
4019 [port_idx][copp_idx]));
4020 goto fail_cmd;
4021 }
4022 rc = 0;
4023fail_cmd:
4024 return rc;
4025}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304026EXPORT_SYMBOL(adm_set_softvolume);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304027
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304028/**
4029 * adm_set_mic_gain -
4030 * command to set MIC gain
4031 *
4032 * @port_id: Port ID number
4033 * @copp_idx: copp index assigned
4034 * @volume: gain value to set
4035 *
4036 * Returns 0 on success or error on failure
4037 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304038int adm_set_mic_gain(int port_id, int copp_idx, int volume)
4039{
4040 struct adm_set_mic_gain_params mic_gain_params;
4041 int rc = 0;
4042 int sz, port_idx;
4043
4044 pr_debug("%s:\n", __func__);
4045 port_id = afe_convert_virtual_to_portid(port_id);
4046 port_idx = adm_validate_and_get_port_index(port_id);
4047 if (port_idx < 0) {
4048 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
4049 return -EINVAL;
4050 }
4051
4052 sz = sizeof(struct adm_set_mic_gain_params);
4053
4054 mic_gain_params.params.hdr.hdr_field =
4055 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4056 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
4057 mic_gain_params.params.hdr.pkt_size = sz;
4058 mic_gain_params.params.hdr.src_svc = APR_SVC_ADM;
4059 mic_gain_params.params.hdr.src_domain = APR_DOMAIN_APPS;
4060 mic_gain_params.params.hdr.src_port = port_id;
4061 mic_gain_params.params.hdr.dest_svc = APR_SVC_ADM;
4062 mic_gain_params.params.hdr.dest_domain = APR_DOMAIN_ADSP;
4063 mic_gain_params.params.hdr.dest_port =
4064 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
4065 mic_gain_params.params.hdr.token = port_idx << 16 | copp_idx;
4066 mic_gain_params.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
4067 mic_gain_params.params.payload_addr_lsw = 0;
4068 mic_gain_params.params.payload_addr_msw = 0;
4069 mic_gain_params.params.mem_map_handle = 0;
4070 mic_gain_params.params.payload_size =
4071 sizeof(struct adm_param_data_v5) +
4072 sizeof(struct admx_mic_gain);
4073 mic_gain_params.data.module_id = ADM_MODULE_IDX_MIC_GAIN_CTRL;
4074 mic_gain_params.data.param_id = ADM_PARAM_IDX_MIC_GAIN;
4075 mic_gain_params.data.param_size =
4076 sizeof(struct admx_mic_gain);
4077 mic_gain_params.data.reserved = 0;
4078 mic_gain_params.mic_gain_data.tx_mic_gain = volume;
4079 mic_gain_params.mic_gain_data.reserved = 0;
4080 pr_debug("%s: Mic Gain set to %d at port_id 0x%x\n",
4081 __func__, volume, port_id);
4082
4083 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
4084 rc = apr_send_pkt(this_adm.apr, (uint32_t *)&mic_gain_params);
4085 if (rc < 0) {
4086 pr_err("%s: Set params failed port = %#x\n",
4087 __func__, port_id);
4088 rc = -EINVAL;
4089 goto fail_cmd;
4090 }
4091 /* Wait for the callback */
4092 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
4093 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
4094 msecs_to_jiffies(TIMEOUT_MS));
4095 if (!rc) {
4096 pr_err("%s: Mic Gain Set params timed out port = %#x\n",
4097 __func__, port_id);
4098 rc = -EINVAL;
4099 goto fail_cmd;
4100 } else if (atomic_read(&this_adm.copp.stat
4101 [port_idx][copp_idx]) > 0) {
4102 pr_err("%s: DSP returned error[%s]\n",
4103 __func__, adsp_err_get_err_str(
4104 atomic_read(&this_adm.copp.stat
4105 [port_idx][copp_idx])));
4106 rc = adsp_err_get_lnx_err_code(
4107 atomic_read(&this_adm.copp.stat
4108 [port_idx][copp_idx]));
4109 goto fail_cmd;
4110 }
4111 rc = 0;
4112fail_cmd:
4113 return rc;
4114}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304115EXPORT_SYMBOL(adm_set_mic_gain);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304116
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304117/**
4118 * adm_send_set_multichannel_ec_primary_mic_ch -
4119 * command to set multi-ch EC primary mic
4120 *
4121 * @port_id: Port ID number
4122 * @copp_idx: copp index assigned
4123 * @primary_mic_ch: channel number of primary mic
4124 *
4125 * Returns 0 on success or error on failure
4126 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304127int adm_send_set_multichannel_ec_primary_mic_ch(int port_id, int copp_idx,
4128 int primary_mic_ch)
4129{
4130 struct adm_set_sec_primary_ch_params sec_primary_ch_params;
4131 int rc = 0;
4132 int sz, port_idx;
4133
4134 pr_debug("%s port_id 0x%x, copp_idx 0x%x, primary_mic_ch %d\n",
4135 __func__, port_id, copp_idx, primary_mic_ch);
4136 port_id = afe_convert_virtual_to_portid(port_id);
4137 port_idx = adm_validate_and_get_port_index(port_id);
4138 if (port_idx < 0) {
4139 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
4140 return -EINVAL;
4141 }
4142
4143 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4144 pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
4145 return -EINVAL;
4146 }
4147
4148 sz = sizeof(struct adm_set_sec_primary_ch_params);
4149
4150 sec_primary_ch_params.params.hdr.hdr_field =
4151 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4152 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
4153 sec_primary_ch_params.params.hdr.pkt_size = sz;
4154 sec_primary_ch_params.params.hdr.src_svc = APR_SVC_ADM;
4155 sec_primary_ch_params.params.hdr.src_domain = APR_DOMAIN_APPS;
4156 sec_primary_ch_params.params.hdr.src_port = port_id;
4157 sec_primary_ch_params.params.hdr.dest_svc = APR_SVC_ADM;
4158 sec_primary_ch_params.params.hdr.dest_domain = APR_DOMAIN_ADSP;
4159 sec_primary_ch_params.params.hdr.dest_port =
4160 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
4161 sec_primary_ch_params.params.hdr.token = port_idx << 16 | copp_idx;
4162 sec_primary_ch_params.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
4163 sec_primary_ch_params.params.payload_addr_lsw = 0;
4164 sec_primary_ch_params.params.payload_addr_msw = 0;
4165 sec_primary_ch_params.params.mem_map_handle = 0;
4166 sec_primary_ch_params.params.payload_size =
4167 sizeof(struct adm_param_data_v5) +
4168 sizeof(struct admx_sec_primary_mic_ch);
4169 sec_primary_ch_params.data.module_id =
4170 AUDPROC_MODULE_ID_VOICE_TX_SECNS;
4171 sec_primary_ch_params.data.param_id =
4172 AUDPROC_PARAM_IDX_SEC_PRIMARY_MIC_CH;
4173 sec_primary_ch_params.data.param_size =
4174 sizeof(struct admx_sec_primary_mic_ch);
4175 sec_primary_ch_params.data.reserved = 0;
4176 sec_primary_ch_params.sec_primary_mic_ch_data.version = 0;
4177 sec_primary_ch_params.sec_primary_mic_ch_data.reserved = 0;
4178 sec_primary_ch_params.sec_primary_mic_ch_data.sec_primary_mic_ch =
4179 primary_mic_ch;
4180 sec_primary_ch_params.sec_primary_mic_ch_data.reserved1 = 0;
4181
4182 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
4183 rc = apr_send_pkt(this_adm.apr, (uint32_t *)&sec_primary_ch_params);
4184 if (rc < 0) {
4185 pr_err("%s: Set params failed port = %#x\n",
4186 __func__, port_id);
4187 rc = -EINVAL;
4188 goto fail_cmd;
4189 }
4190 /* Wait for the callback */
4191 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
4192 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
4193 msecs_to_jiffies(TIMEOUT_MS));
4194 if (!rc) {
4195 pr_err("%s: Mic Set params timed out port = %#x\n",
4196 __func__, port_id);
4197 rc = -EINVAL;
4198 goto fail_cmd;
4199 } else if (atomic_read(&this_adm.copp.stat
4200 [port_idx][copp_idx]) > 0) {
4201 pr_err("%s: DSP returned error[%s]\n",
4202 __func__, adsp_err_get_err_str(
4203 atomic_read(&this_adm.copp.stat
4204 [port_idx][copp_idx])));
4205 rc = adsp_err_get_lnx_err_code(
4206 atomic_read(&this_adm.copp.stat
4207 [port_idx][copp_idx]));
4208 goto fail_cmd;
4209 }
4210 rc = 0;
4211fail_cmd:
4212 return rc;
4213}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304214EXPORT_SYMBOL(adm_send_set_multichannel_ec_primary_mic_ch);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304215
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304216/**
4217 * adm_param_enable -
4218 * command to send params to ADM for given module
4219 *
4220 * @port_id: Port ID number
4221 * @copp_idx: copp index assigned
4222 * @module_id: ADM module
4223 * @enable: flag to enable or disable module
4224 *
4225 * Returns 0 on success or error on failure
4226 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304227int adm_param_enable(int port_id, int copp_idx, int module_id, int enable)
4228{
4229 struct audproc_enable_param_t adm_mod_enable;
4230 int sz = 0;
4231 int rc = 0;
4232 int port_idx;
4233
4234 pr_debug("%s port_id %d, module_id 0x%x, enable %d\n",
4235 __func__, port_id, module_id, enable);
4236 port_id = afe_convert_virtual_to_portid(port_id);
4237 port_idx = adm_validate_and_get_port_index(port_id);
4238 if (port_idx < 0) {
4239 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4240 rc = -EINVAL;
4241 goto fail_cmd;
4242 }
4243
4244 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4245 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4246 return -EINVAL;
4247 }
4248
4249 sz = sizeof(struct audproc_enable_param_t);
4250
4251 adm_mod_enable.pp_params.hdr.hdr_field =
4252 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4253 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
4254 adm_mod_enable.pp_params.hdr.pkt_size = sz;
4255 adm_mod_enable.pp_params.hdr.src_svc = APR_SVC_ADM;
4256 adm_mod_enable.pp_params.hdr.src_domain = APR_DOMAIN_APPS;
4257 adm_mod_enable.pp_params.hdr.src_port = port_id;
4258 adm_mod_enable.pp_params.hdr.dest_svc = APR_SVC_ADM;
4259 adm_mod_enable.pp_params.hdr.dest_domain = APR_DOMAIN_ADSP;
4260 adm_mod_enable.pp_params.hdr.dest_port =
4261 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
4262 adm_mod_enable.pp_params.hdr.token = port_idx << 16 | copp_idx;
4263 adm_mod_enable.pp_params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
4264 adm_mod_enable.pp_params.payload_addr_lsw = 0;
4265 adm_mod_enable.pp_params.payload_addr_msw = 0;
4266 adm_mod_enable.pp_params.mem_map_handle = 0;
4267 adm_mod_enable.pp_params.payload_size = sizeof(adm_mod_enable) -
4268 sizeof(adm_mod_enable.pp_params) +
4269 sizeof(adm_mod_enable.pp_params.params);
4270 adm_mod_enable.pp_params.params.module_id = module_id;
4271 adm_mod_enable.pp_params.params.param_id = AUDPROC_PARAM_ID_ENABLE;
4272 adm_mod_enable.pp_params.params.param_size =
4273 adm_mod_enable.pp_params.payload_size -
4274 sizeof(adm_mod_enable.pp_params.params);
4275 adm_mod_enable.pp_params.params.reserved = 0;
4276 adm_mod_enable.enable = enable;
4277
4278 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
4279
4280 rc = apr_send_pkt(this_adm.apr, (uint32_t *)&adm_mod_enable);
4281 if (rc < 0) {
4282 pr_err("%s: Set params failed port = %#x\n",
4283 __func__, port_id);
4284 rc = -EINVAL;
4285 goto fail_cmd;
4286 }
4287 /* Wait for the callback */
4288 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
4289 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
4290 msecs_to_jiffies(TIMEOUT_MS));
4291 if (!rc) {
4292 pr_err("%s: module %x enable %d timed out on port = %#x\n",
4293 __func__, module_id, enable, port_id);
4294 rc = -EINVAL;
4295 goto fail_cmd;
4296 } else if (atomic_read(&this_adm.copp.stat
4297 [port_idx][copp_idx]) > 0) {
4298 pr_err("%s: DSP returned error[%s]\n",
4299 __func__, adsp_err_get_err_str(
4300 atomic_read(&this_adm.copp.stat
4301 [port_idx][copp_idx])));
4302 rc = adsp_err_get_lnx_err_code(
4303 atomic_read(&this_adm.copp.stat
4304 [port_idx][copp_idx]));
4305 goto fail_cmd;
4306 }
4307 rc = 0;
4308fail_cmd:
4309 return rc;
4310
4311}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304312EXPORT_SYMBOL(adm_param_enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304313
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304314/**
4315 * adm_send_calibration -
4316 * send ADM calibration to DSP
4317 *
4318 * @port_id: Port ID number
4319 * @copp_idx: copp index assigned
4320 * @path: direction or ADM path type
4321 * @perf_mode: performance mode like LL/ULL/..
4322 * @cal_type: calibration type to use
4323 * @params: pointer with cal data
4324 * @size: cal size
4325 *
4326 * Returns 0 on success or error on failure
4327 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304328int adm_send_calibration(int port_id, int copp_idx, int path, int perf_mode,
4329 int cal_type, char *params, int size)
4330{
4331
4332 struct adm_cmd_set_pp_params_v5 *adm_params = NULL;
4333 int sz, rc = 0;
4334 int port_idx;
4335
4336 pr_debug("%s:port_id %d, path %d, perf_mode %d, cal_type %d, size %d\n",
4337 __func__, port_id, path, perf_mode, cal_type, size);
4338
4339 port_id = afe_convert_virtual_to_portid(port_id);
4340 port_idx = adm_validate_and_get_port_index(port_id);
4341 if (port_idx < 0) {
4342 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4343 rc = -EINVAL;
4344 goto end;
4345 }
4346
4347 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4348 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4349 return -EINVAL;
4350 }
4351
4352 /* Maps audio_dev_ctrl path definition to ACDB definition */
4353 if (get_cal_path(path) != RX_DEVICE) {
4354 pr_err("%s: acdb_path %d\n", __func__, path);
4355 rc = -EINVAL;
4356 goto end;
4357 }
4358
4359 sz = sizeof(struct adm_cmd_set_pp_params_v5) + size;
4360 adm_params = kzalloc(sz, GFP_KERNEL);
4361 if (!adm_params) {
4362 pr_err("%s, adm params memory alloc failed", __func__);
4363 rc = -ENOMEM;
4364 goto end;
4365 }
4366
4367 memcpy(((u8 *)adm_params + sizeof(struct adm_cmd_set_pp_params_v5)),
4368 params, size);
4369
4370 adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4371 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
4372 adm_params->hdr.pkt_size = sz;
4373 adm_params->hdr.src_svc = APR_SVC_ADM;
4374 adm_params->hdr.src_domain = APR_DOMAIN_APPS;
4375 adm_params->hdr.src_port = port_id;
4376 adm_params->hdr.dest_svc = APR_SVC_ADM;
4377 adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
4378 adm_params->hdr.dest_port =
4379 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
4380 adm_params->hdr.token = port_idx << 16 | copp_idx;
4381 adm_params->hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
4382 /* payload address and mmap handle initialized to zero by kzalloc */
4383 adm_params->payload_size = size;
4384
4385 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
4386 rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
4387 if (rc < 0) {
4388 pr_err("%s: Set params failed port = %#x\n",
4389 __func__, port_id);
4390 rc = -EINVAL;
4391 goto end;
4392 }
4393 /* Wait for the callback */
4394 rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
4395 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
4396 msecs_to_jiffies(TIMEOUT_MS));
4397 if (!rc) {
4398 pr_err("%s: Set params timed out port = %#x\n",
4399 __func__, port_id);
4400 rc = -EINVAL;
4401 goto end;
4402 } else if (atomic_read(&this_adm.copp.stat
4403 [port_idx][copp_idx]) > 0) {
4404 pr_err("%s: DSP returned error[%s]\n",
4405 __func__, adsp_err_get_err_str(
4406 atomic_read(&this_adm.copp.stat
4407 [port_idx][copp_idx])));
4408 rc = adsp_err_get_lnx_err_code(
4409 atomic_read(&this_adm.copp.stat
4410 [port_idx][copp_idx]));
4411 goto end;
4412 }
4413 rc = 0;
4414
4415end:
4416 kfree(adm_params);
4417 return rc;
4418}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304419EXPORT_SYMBOL(adm_send_calibration);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304420
4421/*
4422 * adm_update_wait_parameters must be called with routing driver locks.
4423 * adm_reset_wait_parameters must be called with routing driver locks.
4424 * set and reset parmeters are separated to make sure it is always called
4425 * under routing driver lock.
4426 * adm_wait_timeout is to block until timeout or interrupted. Timeout is
4427 * not a an error.
4428 */
4429int adm_set_wait_parameters(int port_id, int copp_idx)
4430{
4431
4432 int ret = 0, port_idx;
4433
4434 pr_debug("%s: port_id 0x%x, copp_idx %d\n", __func__, port_id,
4435 copp_idx);
4436 port_id = afe_convert_virtual_to_portid(port_id);
4437 port_idx = adm_validate_and_get_port_index(port_id);
4438 if (port_idx < 0) {
4439 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4440 ret = -EINVAL;
4441 goto end;
4442 }
4443
4444 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4445 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4446 return -EINVAL;
4447 }
4448
4449 this_adm.copp.adm_delay[port_idx][copp_idx] = 1;
4450 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 0);
4451
4452end:
4453 return ret;
4454
4455}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304456EXPORT_SYMBOL(adm_set_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304457
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304458/**
4459 * adm_reset_wait_parameters -
4460 * reset wait parameters or ADM delay value
4461 *
4462 * @port_id: Port ID number
4463 * @copp_idx: copp index assigned
4464 *
4465 * Returns 0 on success or error on failure
4466 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304467int adm_reset_wait_parameters(int port_id, int copp_idx)
4468{
4469 int ret = 0, port_idx;
4470
4471 pr_debug("%s: port_id 0x%x copp_idx %d\n", __func__, port_id,
4472 copp_idx);
4473 port_id = afe_convert_virtual_to_portid(port_id);
4474 port_idx = adm_validate_and_get_port_index(port_id);
4475 if (port_idx < 0) {
4476 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4477 ret = -EINVAL;
4478 goto end;
4479 }
4480
4481 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4482 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4483 return -EINVAL;
4484 }
4485
4486 atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 1);
4487 this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
4488
4489end:
4490 return ret;
4491}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304492EXPORT_SYMBOL(adm_reset_wait_parameters);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304493
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304494/**
4495 * adm_wait_timeout -
4496 * ADM wait command after command send to DSP
4497 *
4498 * @port_id: Port ID number
4499 * @copp_idx: copp index assigned
4500 * @wait_time: value in ms for command timeout
4501 *
4502 * Returns 0 on success or error on failure
4503 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304504int adm_wait_timeout(int port_id, int copp_idx, int wait_time)
4505{
4506 int ret = 0, port_idx;
4507
4508 pr_debug("%s: port_id 0x%x, copp_idx %d, wait_time %d\n", __func__,
4509 port_id, copp_idx, wait_time);
4510 port_id = afe_convert_virtual_to_portid(port_id);
4511 port_idx = adm_validate_and_get_port_index(port_id);
4512 if (port_idx < 0) {
4513 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4514 ret = -EINVAL;
4515 goto end;
4516 }
4517
4518 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4519 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4520 return -EINVAL;
4521 }
4522
4523 ret = wait_event_timeout(
4524 this_adm.copp.adm_delay_wait[port_idx][copp_idx],
4525 atomic_read(&this_adm.copp.adm_delay_stat[port_idx][copp_idx]),
4526 msecs_to_jiffies(wait_time));
4527 pr_debug("%s: return %d\n", __func__, ret);
4528 if (ret != 0)
4529 ret = -EINTR;
4530end:
4531 pr_debug("%s: return %d--\n", __func__, ret);
4532 return ret;
4533}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304534EXPORT_SYMBOL(adm_wait_timeout);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304535
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304536/**
4537 * adm_store_cal_data -
4538 * Retrieve calibration data for ADM copp device
4539 *
4540 * @port_id: Port ID number
4541 * @copp_idx: copp index assigned
4542 * @path: direction or copp type
4543 * @perf_mode: performance mode like LL/ULL/..
4544 * @cal_index: calibration index to use
4545 * @params: pointer to store cal data
4546 * @size: pointer to fill with cal size
4547 *
4548 * Returns 0 on success or error on failure
4549 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304550int adm_store_cal_data(int port_id, int copp_idx, int path, int perf_mode,
4551 int cal_index, char *params, int *size)
4552{
4553 int rc = 0;
4554 struct cal_block_data *cal_block = NULL;
4555 int app_type, acdb_id, port_idx, sample_rate;
4556
4557 if (this_adm.cal_data[cal_index] == NULL) {
4558 pr_debug("%s: cal_index %d not allocated!\n",
4559 __func__, cal_index);
4560 goto end;
4561 }
4562
4563 if (get_cal_path(path) != RX_DEVICE) {
4564 pr_debug("%s: Invalid path to store calibration %d\n",
4565 __func__, path);
4566 rc = -EINVAL;
4567 goto end;
4568 }
4569
4570 port_id = afe_convert_virtual_to_portid(port_id);
4571 port_idx = adm_validate_and_get_port_index(port_id);
4572 if (port_idx < 0) {
4573 pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
4574 rc = -EINVAL;
4575 goto end;
4576 }
4577
4578 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4579 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4580 return -EINVAL;
4581 }
4582
4583 acdb_id = atomic_read(&this_adm.copp.acdb_id[port_idx][copp_idx]);
4584 app_type = atomic_read(&this_adm.copp.app_type[port_idx][copp_idx]);
4585 sample_rate = atomic_read(&this_adm.copp.rate[port_idx][copp_idx]);
4586
4587 mutex_lock(&this_adm.cal_data[cal_index]->lock);
4588 cal_block = adm_find_cal(cal_index, get_cal_path(path), app_type,
4589 acdb_id, sample_rate);
4590 if (cal_block == NULL)
4591 goto unlock;
4592
4593 if (cal_block->cal_data.size <= 0) {
4594 pr_debug("%s: No ADM cal send for port_id = 0x%x!\n",
4595 __func__, port_id);
4596 rc = -EINVAL;
4597 goto unlock;
4598 }
4599
Aditya Bavanari2a627ae2017-11-21 20:24:53 +05304600 if (cal_index == ADM_AUDPROC_CAL || cal_index == ADM_LSM_AUDPROC_CAL) {
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304601 if (cal_block->cal_data.size > AUD_PROC_BLOCK_SIZE) {
4602 pr_err("%s:audproc:invalid size exp/actual[%zd, %d]\n",
4603 __func__, cal_block->cal_data.size, *size);
4604 rc = -ENOMEM;
4605 goto unlock;
4606 }
Bhalchandra Gajareface2762018-05-10 14:16:49 -07004607 } else if (cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
4608 if (cal_block->cal_data.size > AUD_PROC_PERSIST_BLOCK_SIZE) {
4609 pr_err("%s:persist invalid size exp/actual[%zd, %d]\n",
4610 __func__, cal_block->cal_data.size, *size);
4611 rc = -ENOMEM;
4612 goto unlock;
4613 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304614 } else if (cal_index == ADM_AUDVOL_CAL) {
4615 if (cal_block->cal_data.size > AUD_VOL_BLOCK_SIZE) {
4616 pr_err("%s:aud_vol:invalid size exp/actual[%zd, %d]\n",
4617 __func__, cal_block->cal_data.size, *size);
4618 rc = -ENOMEM;
4619 goto unlock;
4620 }
4621 } else {
4622 pr_debug("%s: Not valid calibration for dolby topolgy\n",
4623 __func__);
4624 rc = -EINVAL;
4625 goto unlock;
4626 }
4627 memcpy(params, cal_block->cal_data.kvaddr, cal_block->cal_data.size);
4628 *size = cal_block->cal_data.size;
4629
4630 pr_debug("%s:port_id %d, copp_idx %d, path %d",
4631 __func__, port_id, copp_idx, path);
4632 pr_debug("perf_mode %d, cal_type %d, size %d\n",
4633 perf_mode, cal_index, *size);
4634
4635unlock:
4636 mutex_unlock(&this_adm.cal_data[cal_index]->lock);
4637end:
4638 return rc;
4639}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304640EXPORT_SYMBOL(adm_store_cal_data);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304641
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304642/**
4643 * adm_send_compressed_device_mute -
4644 * command to send mute for compressed device
4645 *
4646 * @port_id: Port ID number
4647 * @copp_idx: copp index assigned
4648 * @mute_on: flag to indicate mute or unmute
4649 *
4650 * Returns 0 on success or error on failure
4651 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304652int adm_send_compressed_device_mute(int port_id, int copp_idx, bool mute_on)
4653{
4654 struct adm_set_compressed_device_mute mute_params;
4655 int ret = 0;
4656 int port_idx;
4657
4658 pr_debug("%s port_id: 0x%x, copp_idx %d, mute_on: %d\n",
4659 __func__, port_id, copp_idx, mute_on);
4660 port_id = afe_convert_virtual_to_portid(port_id);
4661 port_idx = adm_validate_and_get_port_index(port_id);
4662 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
4663 pr_err("%s: Invalid port_id %#x copp_idx %d\n",
4664 __func__, port_id, copp_idx);
4665 ret = -EINVAL;
4666 goto end;
4667 }
4668
4669 mute_params.command.hdr.hdr_field =
4670 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4671 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
4672 mute_params.command.hdr.pkt_size =
4673 sizeof(struct adm_set_compressed_device_mute);
4674 mute_params.command.hdr.src_svc = APR_SVC_ADM;
4675 mute_params.command.hdr.src_domain = APR_DOMAIN_APPS;
4676 mute_params.command.hdr.src_port = port_id;
4677 mute_params.command.hdr.dest_svc = APR_SVC_ADM;
4678 mute_params.command.hdr.dest_domain = APR_DOMAIN_ADSP;
4679 mute_params.command.hdr.dest_port =
4680 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
4681 mute_params.command.hdr.token = port_idx << 16 | copp_idx;
4682 mute_params.command.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
4683 mute_params.command.payload_addr_lsw = 0;
4684 mute_params.command.payload_addr_msw = 0;
4685 mute_params.command.mem_map_handle = 0;
4686 mute_params.command.payload_size = sizeof(mute_params) -
4687 sizeof(mute_params.command);
4688 mute_params.params.module_id = AUDPROC_MODULE_ID_COMPRESSED_MUTE;
4689 mute_params.params.param_id = AUDPROC_PARAM_ID_COMPRESSED_MUTE;
4690 mute_params.params.param_size = mute_params.command.payload_size -
4691 sizeof(mute_params.params);
4692 mute_params.params.reserved = 0;
4693 mute_params.mute_on = mute_on;
4694
4695 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
4696 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&mute_params);
4697 if (ret < 0) {
4698 pr_err("%s: device mute for port %d copp %d failed, ret %d\n",
4699 __func__, port_id, copp_idx, ret);
4700 ret = -EINVAL;
4701 goto end;
4702 }
4703
4704 /* Wait for the callback */
4705 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
4706 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
4707 msecs_to_jiffies(TIMEOUT_MS));
4708 if (!ret) {
4709 pr_err("%s: send device mute for port %d copp %d failed\n",
4710 __func__, port_id, copp_idx);
4711 ret = -EINVAL;
4712 goto end;
4713 } else if (atomic_read(&this_adm.copp.stat
4714 [port_idx][copp_idx]) > 0) {
4715 pr_err("%s: DSP returned error[%s]\n",
4716 __func__, adsp_err_get_err_str(
4717 atomic_read(&this_adm.copp.stat
4718 [port_idx][copp_idx])));
4719 ret = adsp_err_get_lnx_err_code(
4720 atomic_read(&this_adm.copp.stat
4721 [port_idx][copp_idx]));
4722 goto end;
4723 }
4724 ret = 0;
4725end:
4726 return ret;
4727}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304728EXPORT_SYMBOL(adm_send_compressed_device_mute);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304729
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304730/**
4731 * adm_send_compressed_device_latency -
4732 * command to send latency for compressed device
4733 *
4734 * @port_id: Port ID number
4735 * @copp_idx: copp index assigned
4736 * @latency: latency value to pass
4737 *
4738 * Returns 0 on success or error on failure
4739 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304740int adm_send_compressed_device_latency(int port_id, int copp_idx, int latency)
4741{
4742 struct adm_set_compressed_device_latency latency_params;
4743 int port_idx;
4744 int ret = 0;
4745
4746 pr_debug("%s port_id: 0x%x, copp_idx %d latency: %d\n", __func__,
4747 port_id, copp_idx, latency);
4748 port_id = afe_convert_virtual_to_portid(port_id);
4749 port_idx = adm_validate_and_get_port_index(port_id);
4750 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
4751 pr_err("%s: Invalid port_id %#x copp_idx %d\n",
4752 __func__, port_id, copp_idx);
4753 ret = -EINVAL;
4754 goto end;
4755 }
4756
4757 latency_params.command.hdr.hdr_field =
4758 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4759 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
4760 latency_params.command.hdr.pkt_size =
4761 sizeof(struct adm_set_compressed_device_latency);
4762 latency_params.command.hdr.src_svc = APR_SVC_ADM;
4763 latency_params.command.hdr.src_domain = APR_DOMAIN_APPS;
4764 latency_params.command.hdr.src_port = port_id;
4765 latency_params.command.hdr.dest_svc = APR_SVC_ADM;
4766 latency_params.command.hdr.dest_domain = APR_DOMAIN_ADSP;
4767 latency_params.command.hdr.dest_port =
4768 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
4769 latency_params.command.hdr.token = port_idx << 16 | copp_idx;
4770 latency_params.command.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
4771 latency_params.command.payload_addr_lsw = 0;
4772 latency_params.command.payload_addr_msw = 0;
4773 latency_params.command.mem_map_handle = 0;
4774 latency_params.command.payload_size = sizeof(latency_params) -
4775 sizeof(latency_params.command);
4776 latency_params.params.module_id = AUDPROC_MODULE_ID_COMPRESSED_LATENCY;
4777 latency_params.params.param_id = AUDPROC_PARAM_ID_COMPRESSED_LATENCY;
4778 latency_params.params.param_size = latency_params.command.payload_size -
4779 sizeof(latency_params.params);
4780 latency_params.params.reserved = 0;
4781 latency_params.latency = latency;
4782
4783 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
4784 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&latency_params);
4785 if (ret < 0) {
4786 pr_err("%s: send device latency err %d for port %d copp %d\n",
4787 __func__, port_id, copp_idx, ret);
4788 ret = -EINVAL;
4789 goto end;
4790 }
4791
4792 /* Wait for the callback */
4793 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
4794 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
4795 msecs_to_jiffies(TIMEOUT_MS));
4796 if (!ret) {
4797 pr_err("%s: send device latency for port %d failed\n", __func__,
4798 port_id);
4799 ret = -EINVAL;
4800 goto end;
4801 } else if (atomic_read(&this_adm.copp.stat
4802 [port_idx][copp_idx]) > 0) {
4803 pr_err("%s: DSP returned error[%s]\n",
4804 __func__, adsp_err_get_err_str(
4805 atomic_read(&this_adm.copp.stat
4806 [port_idx][copp_idx])));
4807 ret = adsp_err_get_lnx_err_code(
4808 atomic_read(&this_adm.copp.stat
4809 [port_idx][copp_idx]));
4810 goto end;
4811 }
4812 ret = 0;
4813end:
4814 return ret;
4815}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304816EXPORT_SYMBOL(adm_send_compressed_device_latency);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304817
4818/**
4819 * adm_swap_speaker_channels
4820 *
4821 * Receives port_id, copp_idx, sample rate, spk_swap and
4822 * send MFC command to swap speaker channel.
4823 * Return zero on success. On failure returns nonzero.
4824 *
4825 * port_id - Passed value, port_id for which channels swap is wanted
4826 * copp_idx - Passed value, copp_idx for which channels swap is wanted
4827 * sample_rate - Passed value, sample rate used by app type config
4828 * spk_swap - Passed value, spk_swap for check if swap flag is set
4829 */
4830int adm_swap_speaker_channels(int port_id, int copp_idx,
4831 int sample_rate, bool spk_swap)
4832{
4833 struct audproc_mfc_output_media_fmt mfc_cfg;
4834 uint16_t num_channels;
4835 int port_idx;
4836 int ret = 0;
4837
4838 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4839 __func__, port_id, copp_idx);
4840 port_id = q6audio_convert_virtual_to_portid(port_id);
4841 port_idx = adm_validate_and_get_port_index(port_id);
4842 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
4843 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4844 ret = -EINVAL;
4845 goto done;
4846 }
4847
4848 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4849 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4850 ret = -EINVAL;
4851 goto done;
4852 }
4853
4854 num_channels = atomic_read(
4855 &this_adm.copp.channels[port_idx][copp_idx]);
4856 if (num_channels != 2) {
4857 pr_debug("%s: Invalid number of channels: %d\n",
4858 __func__, num_channels);
4859 ret = -EINVAL;
4860 goto done;
4861 }
4862
4863 memset(&mfc_cfg, 0, sizeof(mfc_cfg));
4864 mfc_cfg.params.hdr.hdr_field =
4865 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4866 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
4867 mfc_cfg.params.hdr.pkt_size =
4868 sizeof(mfc_cfg);
4869 mfc_cfg.params.hdr.src_svc = APR_SVC_ADM;
4870 mfc_cfg.params.hdr.src_domain = APR_DOMAIN_APPS;
4871 mfc_cfg.params.hdr.src_port = port_id;
4872 mfc_cfg.params.hdr.dest_svc = APR_SVC_ADM;
4873 mfc_cfg.params.hdr.dest_domain = APR_DOMAIN_ADSP;
4874 mfc_cfg.params.hdr.dest_port =
4875 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
4876 mfc_cfg.params.hdr.token = port_idx << 16 | copp_idx;
4877 mfc_cfg.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
4878 mfc_cfg.params.payload_addr_lsw = 0;
4879 mfc_cfg.params.payload_addr_msw = 0;
4880 mfc_cfg.params.mem_map_handle = 0;
4881 mfc_cfg.params.payload_size = sizeof(mfc_cfg) -
4882 sizeof(mfc_cfg.params);
4883 mfc_cfg.data.module_id = AUDPROC_MODULE_ID_MFC;
4884 mfc_cfg.data.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
4885 mfc_cfg.data.param_size = mfc_cfg.params.payload_size -
4886 sizeof(mfc_cfg.data);
4887 mfc_cfg.data.reserved = 0;
4888 mfc_cfg.sampling_rate = sample_rate;
4889 mfc_cfg.bits_per_sample =
4890 atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
4891 mfc_cfg.num_channels = num_channels;
4892
4893 /* Currently applying speaker swap for only 2 channel use case */
4894 if (spk_swap) {
4895 mfc_cfg.channel_type[0] =
4896 (uint16_t) PCM_CHANNEL_FR;
4897 mfc_cfg.channel_type[1] =
4898 (uint16_t) PCM_CHANNEL_FL;
4899 } else {
4900 mfc_cfg.channel_type[0] =
4901 (uint16_t) PCM_CHANNEL_FL;
4902 mfc_cfg.channel_type[1] =
4903 (uint16_t) PCM_CHANNEL_FR;
4904 }
4905
4906 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
4907 pr_debug("%s: mfc config: port_idx %d copp_idx %d copp SR %d copp BW %d copp chan %d\n",
4908 __func__, port_idx, copp_idx, mfc_cfg.sampling_rate,
4909 mfc_cfg.bits_per_sample, mfc_cfg.num_channels);
4910
4911 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&mfc_cfg);
4912 if (ret < 0) {
4913 pr_err("%s: port_id: for[0x%x] failed %d\n",
4914 __func__, port_id, ret);
4915 goto done;
4916 }
4917 /* Wait for the callback with copp id */
4918 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
4919 atomic_read(&this_adm.copp.stat
4920 [port_idx][copp_idx]) >= 0,
4921 msecs_to_jiffies(TIMEOUT_MS));
4922 if (!ret) {
4923 pr_err("%s: mfc_cfg Set params timed out for port_id: for [0x%x]\n",
4924 __func__, port_id);
4925 ret = -ETIMEDOUT;
4926 goto done;
4927 }
4928
4929 if (atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) > 0) {
4930 pr_err("%s: DSP returned error[%s]\n",
4931 __func__, adsp_err_get_err_str(
4932 atomic_read(&this_adm.copp.stat
4933 [port_idx][copp_idx])));
4934 ret = adsp_err_get_lnx_err_code(
4935 atomic_read(&this_adm.copp.stat
4936 [port_idx][copp_idx]));
4937 goto done;
4938 }
4939
4940 pr_debug("%s: mfc_cfg Set params returned success", __func__);
4941 ret = 0;
4942
4943done:
4944 return ret;
4945}
4946EXPORT_SYMBOL(adm_swap_speaker_channels);
4947
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05304948/**
4949 * adm_set_sound_focus -
4950 * Update sound focus info
4951 *
4952 * @port_id: Port ID number
4953 * @copp_idx: copp index assigned
4954 * @soundFocusData: sound focus data to pass
4955 *
4956 * Returns 0 on success or error on failure
4957 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304958int adm_set_sound_focus(int port_id, int copp_idx,
4959 struct sound_focus_param soundFocusData)
4960{
4961 struct adm_set_fluence_soundfocus_param soundfocus_params;
4962 int sz = 0;
4963 int ret = 0;
4964 int port_idx;
4965 int i;
4966
4967 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
4968 __func__, port_id, copp_idx);
4969
4970 port_id = afe_convert_virtual_to_portid(port_id);
4971 port_idx = adm_validate_and_get_port_index(port_id);
4972 if (port_idx < 0) {
4973 pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
4974
4975 ret = -EINVAL;
4976 goto done;
4977 }
4978
4979 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
4980 pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
4981
4982 ret = -EINVAL;
4983 goto done;
4984 }
4985
4986 sz = sizeof(struct adm_set_fluence_soundfocus_param);
4987 soundfocus_params.params.hdr.hdr_field =
4988 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
4989 APR_PKT_VER);
4990 soundfocus_params.params.hdr.pkt_size = sz;
4991 soundfocus_params.params.hdr.src_svc = APR_SVC_ADM;
4992 soundfocus_params.params.hdr.src_domain = APR_DOMAIN_APPS;
4993 soundfocus_params.params.hdr.src_port = port_id;
4994 soundfocus_params.params.hdr.dest_svc = APR_SVC_ADM;
4995 soundfocus_params.params.hdr.dest_domain = APR_DOMAIN_ADSP;
4996 soundfocus_params.params.hdr.dest_port =
4997 atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
4998 soundfocus_params.params.hdr.token = port_idx << 16 |
4999 ADM_CLIENT_ID_SOURCE_TRACKING << 8 | copp_idx;
5000 soundfocus_params.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
5001 soundfocus_params.params.payload_addr_lsw = 0;
5002 soundfocus_params.params.payload_addr_msw = 0;
5003 soundfocus_params.params.mem_map_handle = 0;
5004 soundfocus_params.params.payload_size = sizeof(soundfocus_params) -
5005 sizeof(soundfocus_params.params);
5006 soundfocus_params.data.module_id = VOICEPROC_MODULE_ID_GENERIC_TX;
5007 soundfocus_params.data.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
5008 soundfocus_params.data.param_size =
5009 soundfocus_params.params.payload_size -
5010 sizeof(soundfocus_params.data);
5011 soundfocus_params.data.reserved = 0;
5012
5013 memset(&(soundfocus_params.soundfocus_data), 0xFF,
5014 sizeof(struct adm_param_fluence_soundfocus_t));
5015 for (i = 0; i < MAX_SECTORS; i++) {
5016 soundfocus_params.soundfocus_data.start_angles[i] =
5017 soundFocusData.start_angle[i];
5018 soundfocus_params.soundfocus_data.enables[i] =
5019 soundFocusData.enable[i];
5020 pr_debug("%s: start_angle[%d] = %d\n",
5021 __func__, i, soundFocusData.start_angle[i]);
5022 pr_debug("%s: enable[%d] = %d\n",
5023 __func__, i, soundFocusData.enable[i]);
5024 }
5025 soundfocus_params.soundfocus_data.gain_step =
5026 soundFocusData.gain_step;
5027 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData.gain_step);
5028
5029 soundfocus_params.soundfocus_data.reserved = 0;
5030
5031 atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
5032 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&soundfocus_params);
5033 if (ret < 0) {
5034 pr_err("%s: Set params failed\n", __func__);
5035
5036 ret = -EINVAL;
5037 goto done;
5038 }
5039 /* Wait for the callback */
5040 ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
5041 atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
5042 msecs_to_jiffies(TIMEOUT_MS));
5043 if (!ret) {
5044 pr_err("%s: Set params timed out\n", __func__);
5045
5046 ret = -EINVAL;
5047 goto done;
5048 }
5049
5050 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
5051 pr_err("%s - set params returned error [%s]\n",
5052 __func__, adsp_err_get_err_str(
5053 this_adm.sourceTrackingData.apr_cmd_status));
5054
5055 ret = adsp_err_get_lnx_err_code(
5056 this_adm.sourceTrackingData.apr_cmd_status);
5057 goto done;
5058 }
5059
5060 ret = 0;
5061
5062done:
5063 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
5064
5065 return ret;
5066}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305067EXPORT_SYMBOL(adm_set_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305068
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305069/**
5070 * adm_get_sound_focus -
5071 * Retrieve sound focus info
5072 *
5073 * @port_id: Port ID number
5074 * @copp_idx: copp index assigned
5075 * @soundFocusData: pointer for sound focus data to be updated with
5076 *
5077 * Returns 0 on success or error on failure
5078 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305079int adm_get_sound_focus(int port_id, int copp_idx,
5080 struct sound_focus_param *soundFocusData)
5081{
5082 int ret = 0, i;
5083 char *params_value;
5084 uint32_t param_payload_len = sizeof(struct adm_param_data_v5) +
5085 sizeof(struct adm_param_fluence_soundfocus_t);
5086 struct adm_param_fluence_soundfocus_t *soundfocus_params;
5087
5088 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
5089 __func__, port_id, copp_idx);
5090
5091 params_value = kzalloc(param_payload_len, GFP_KERNEL);
5092 if (!params_value) {
5093 ret = -ENOMEM;
5094 goto done;
5095 }
5096 ret = adm_get_params_v2(port_id, copp_idx,
5097 VOICEPROC_MODULE_ID_GENERIC_TX,
5098 VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS,
5099 param_payload_len,
5100 params_value,
5101 ADM_CLIENT_ID_SOURCE_TRACKING);
5102 if (ret) {
5103 pr_err("%s: get parameters failed ret:%d\n", __func__, ret);
5104
5105 kfree(params_value);
5106 ret = -EINVAL;
5107 goto done;
5108 }
5109
5110 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
5111 pr_err("%s - get params returned error [%s]\n",
5112 __func__, adsp_err_get_err_str(
5113 this_adm.sourceTrackingData.apr_cmd_status));
5114
5115 kfree(params_value);
5116 ret = adsp_err_get_lnx_err_code(
5117 this_adm.sourceTrackingData.apr_cmd_status);
5118 goto done;
5119 }
5120
5121 soundfocus_params = (struct adm_param_fluence_soundfocus_t *)
5122 params_value;
5123 for (i = 0; i < MAX_SECTORS; i++) {
5124 soundFocusData->start_angle[i] =
5125 soundfocus_params->start_angles[i];
5126 soundFocusData->enable[i] = soundfocus_params->enables[i];
5127 pr_debug("%s: start_angle[%d] = %d\n",
5128 __func__, i, soundFocusData->start_angle[i]);
5129 pr_debug("%s: enable[%d] = %d\n",
5130 __func__, i, soundFocusData->enable[i]);
5131 }
5132 soundFocusData->gain_step = soundfocus_params->gain_step;
5133 pr_debug("%s: gain_step = %d\n", __func__, soundFocusData->gain_step);
5134
5135 kfree(params_value);
5136
5137done:
5138 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
5139
5140 return ret;
5141}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305142EXPORT_SYMBOL(adm_get_sound_focus);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305143
5144static int adm_source_tracking_alloc_map_memory(void)
5145{
5146 int ret;
5147
5148 pr_debug("%s: Enter\n", __func__);
5149
5150 ret = msm_audio_ion_alloc("SOURCE_TRACKING",
5151 &this_adm.sourceTrackingData.ion_client,
5152 &this_adm.sourceTrackingData.ion_handle,
5153 AUD_PROC_BLOCK_SIZE,
5154 &this_adm.sourceTrackingData.memmap.paddr,
5155 &this_adm.sourceTrackingData.memmap.size,
5156 &this_adm.sourceTrackingData.memmap.kvaddr);
5157 if (ret) {
5158 pr_err("%s: failed to allocate memory\n", __func__);
5159
5160 ret = -EINVAL;
5161 goto done;
5162 }
5163
5164 atomic_set(&this_adm.mem_map_index, ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
5165 ret = adm_memory_map_regions(&this_adm.sourceTrackingData.memmap.paddr,
5166 0,
5167 (uint32_t *)&this_adm.sourceTrackingData.memmap.size,
5168 1);
5169 if (ret < 0) {
5170 pr_err("%s: failed to map memory, paddr = 0x%pK, size = %d\n",
5171 __func__,
5172 (void *)this_adm.sourceTrackingData.memmap.paddr,
5173 (uint32_t)this_adm.sourceTrackingData.memmap.size);
5174
5175 msm_audio_ion_free(this_adm.sourceTrackingData.ion_client,
5176 this_adm.sourceTrackingData.ion_handle);
5177 this_adm.sourceTrackingData.ion_client = NULL;
5178 this_adm.sourceTrackingData.ion_handle = NULL;
5179 this_adm.sourceTrackingData.memmap.size = 0;
5180 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
5181 this_adm.sourceTrackingData.memmap.paddr = 0;
5182 this_adm.sourceTrackingData.apr_cmd_status = -1;
5183 atomic_set(&this_adm.mem_map_handles
5184 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
5185
5186 ret = -EINVAL;
5187 goto done;
5188 }
5189 ret = 0;
5190 pr_debug("%s: paddr = 0x%pK, size = %d, mem_map_handle = 0x%x\n",
5191 __func__, (void *)this_adm.sourceTrackingData.memmap.paddr,
5192 (uint32_t)this_adm.sourceTrackingData.memmap.size,
5193 atomic_read(&this_adm.mem_map_handles
5194 [ADM_MEM_MAP_INDEX_SOURCE_TRACKING]));
5195
5196done:
5197 pr_debug("%s: Exit, ret = %d\n", __func__, ret);
5198
5199 return ret;
5200}
5201
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305202/**
5203 * adm_get_source_tracking -
5204 * Retrieve source tracking info
5205 *
5206 * @port_id: Port ID number
5207 * @copp_idx: copp index assigned
5208 * @sourceTrackingData: pointer for source track data to be updated with
5209 *
5210 * Returns 0 on success or error on failure
5211 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305212int adm_get_source_tracking(int port_id, int copp_idx,
5213 struct source_tracking_param *sourceTrackingData)
5214{
5215 struct adm_cmd_get_pp_params_v5 admp;
5216 int p_idx, ret = 0, i;
5217 struct adm_param_fluence_sourcetracking_t *source_tracking_params;
5218
5219 pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
5220 __func__, port_id, copp_idx);
5221
5222 if (!this_adm.sourceTrackingData.memmap.paddr) {
5223 /* Allocate and map shared memory for out of band usage */
5224 ret = adm_source_tracking_alloc_map_memory();
5225 if (ret != 0) {
5226 ret = -EINVAL;
5227 goto done;
5228 }
5229 }
5230
5231 port_id = afe_convert_virtual_to_portid(port_id);
5232 p_idx = adm_validate_and_get_port_index(port_id);
5233 if (p_idx < 0) {
5234 pr_err("%s - invalid port index %i, port id %i, copp idx %i\n",
5235 __func__, p_idx, port_id, copp_idx);
5236
5237 ret = -EINVAL;
5238 goto done;
5239 }
5240
5241 admp.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
5242 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
5243 admp.hdr.pkt_size = sizeof(admp);
5244 admp.hdr.src_svc = APR_SVC_ADM;
5245 admp.hdr.src_domain = APR_DOMAIN_APPS;
5246 admp.hdr.src_port = port_id;
5247 admp.hdr.dest_svc = APR_SVC_ADM;
5248 admp.hdr.dest_domain = APR_DOMAIN_ADSP;
5249 admp.hdr.dest_port = atomic_read(&this_adm.copp.id[p_idx][copp_idx]);
5250 admp.hdr.token = p_idx << 16 | ADM_CLIENT_ID_SOURCE_TRACKING << 8 |
5251 copp_idx;
5252 admp.hdr.opcode = ADM_CMD_GET_PP_PARAMS_V5;
5253 admp.data_payload_addr_lsw =
5254 lower_32_bits(this_adm.sourceTrackingData.memmap.paddr);
5255 admp.data_payload_addr_msw =
5256 msm_audio_populate_upper_32_bits(
5257 this_adm.sourceTrackingData.memmap.paddr);
5258 admp.mem_map_handle = atomic_read(&this_adm.mem_map_handles[
5259 ADM_MEM_MAP_INDEX_SOURCE_TRACKING]);
5260 admp.module_id = VOICEPROC_MODULE_ID_GENERIC_TX;
5261 admp.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOURCETRACKING;
5262 admp.param_max_size = sizeof(struct adm_param_fluence_sourcetracking_t)
5263 + sizeof(struct adm_param_data_v5);
5264 admp.reserved = 0;
5265
5266 atomic_set(&this_adm.copp.stat[p_idx][copp_idx], -1);
5267
5268 ret = apr_send_pkt(this_adm.apr, (uint32_t *)&admp);
5269 if (ret < 0) {
5270 pr_err("%s - failed to get Source Tracking Params\n",
5271 __func__);
5272
5273 ret = -EINVAL;
5274 goto done;
5275 }
5276 ret = wait_event_timeout(this_adm.copp.wait[p_idx][copp_idx],
5277 atomic_read(&this_adm.copp.stat[p_idx][copp_idx]) >= 0,
5278 msecs_to_jiffies(TIMEOUT_MS));
5279 if (!ret) {
5280 pr_err("%s - get params timed out\n", __func__);
5281
5282 ret = -EINVAL;
5283 goto done;
5284 } else if (atomic_read(&this_adm.copp.stat
5285 [p_idx][copp_idx]) > 0) {
5286 pr_err("%s: DSP returned error[%s]\n",
5287 __func__, adsp_err_get_err_str(
5288 atomic_read(&this_adm.copp.stat
5289 [p_idx][copp_idx])));
5290 ret = adsp_err_get_lnx_err_code(
5291 atomic_read(&this_adm.copp.stat
5292 [p_idx][copp_idx]));
5293 goto done;
5294 }
5295
5296 if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
5297 pr_err("%s - get params returned error [%s]\n",
5298 __func__, adsp_err_get_err_str(
5299 this_adm.sourceTrackingData.apr_cmd_status));
5300
5301 ret = adsp_err_get_lnx_err_code(
5302 this_adm.sourceTrackingData.apr_cmd_status);
5303 goto done;
5304 }
5305
5306 source_tracking_params = (struct adm_param_fluence_sourcetracking_t *)
5307 (this_adm.sourceTrackingData.memmap.kvaddr +
5308 sizeof(struct adm_param_data_v5));
5309 for (i = 0; i < MAX_SECTORS; i++) {
5310 sourceTrackingData->vad[i] = source_tracking_params->vad[i];
5311 pr_debug("%s: vad[%d] = %d\n",
5312 __func__, i, sourceTrackingData->vad[i]);
5313 }
5314 sourceTrackingData->doa_speech = source_tracking_params->doa_speech;
5315 pr_debug("%s: doa_speech = %d\n",
5316 __func__, sourceTrackingData->doa_speech);
5317
5318 for (i = 0; i < MAX_NOISE_SOURCE_INDICATORS; i++) {
5319 sourceTrackingData->doa_noise[i] =
5320 source_tracking_params->doa_noise[i];
5321 pr_debug("%s: doa_noise[%d] = %d\n",
5322 __func__, i, sourceTrackingData->doa_noise[i]);
5323 }
5324 for (i = 0; i < MAX_POLAR_ACTIVITY_INDICATORS; i++) {
5325 sourceTrackingData->polar_activity[i] =
5326 source_tracking_params->polar_activity[i];
5327 pr_debug("%s: polar_activity[%d] = %d\n",
5328 __func__, i, sourceTrackingData->polar_activity[i]);
5329 }
5330
5331 ret = 0;
5332
5333done:
5334 pr_debug("%s: Exit, ret=%d\n", __func__, ret);
5335
5336 return ret;
5337}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305338EXPORT_SYMBOL(adm_get_source_tracking);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305339
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305340int __init adm_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305341{
5342 int i = 0, j;
5343
5344 this_adm.apr = NULL;
5345 this_adm.ec_ref_rx = -1;
5346 this_adm.num_ec_ref_rx_chans = 0;
5347 this_adm.ec_ref_rx_bit_width = 0;
5348 this_adm.ec_ref_rx_sampling_rate = 0;
5349 atomic_set(&this_adm.matrix_map_stat, 0);
5350 init_waitqueue_head(&this_adm.matrix_map_wait);
5351 atomic_set(&this_adm.adm_stat, 0);
5352 init_waitqueue_head(&this_adm.adm_wait);
5353
5354 for (i = 0; i < AFE_MAX_PORTS; i++) {
5355 for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
5356 atomic_set(&this_adm.copp.id[i][j], RESET_COPP_ID);
5357 atomic_set(&this_adm.copp.cnt[i][j], 0);
5358 atomic_set(&this_adm.copp.topology[i][j], 0);
5359 atomic_set(&this_adm.copp.mode[i][j], 0);
5360 atomic_set(&this_adm.copp.stat[i][j], 0);
5361 atomic_set(&this_adm.copp.rate[i][j], 0);
5362 atomic_set(&this_adm.copp.channels[i][j], 0);
5363 atomic_set(&this_adm.copp.bit_width[i][j], 0);
5364 atomic_set(&this_adm.copp.app_type[i][j], 0);
5365 atomic_set(&this_adm.copp.acdb_id[i][j], 0);
Sachin Mohan Gadag3c3c5812018-07-20 15:46:54 +05305366 atomic_set(&this_adm.copp.session_type[i][j], 0);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305367 init_waitqueue_head(&this_adm.copp.wait[i][j]);
5368 atomic_set(&this_adm.copp.adm_delay_stat[i][j], 0);
5369 init_waitqueue_head(
5370 &this_adm.copp.adm_delay_wait[i][j]);
5371 atomic_set(&this_adm.copp.topology[i][j], 0);
5372 this_adm.copp.adm_delay[i][j] = 0;
5373 this_adm.copp.adm_status[i][j] =
5374 ADM_STATUS_CALIBRATION_REQUIRED;
5375 }
5376 }
5377
5378 if (adm_init_cal_data())
5379 pr_err("%s: could not init cal data!\n", __func__);
5380
5381 this_adm.sourceTrackingData.ion_client = NULL;
5382 this_adm.sourceTrackingData.ion_handle = NULL;
5383 this_adm.sourceTrackingData.memmap.size = 0;
5384 this_adm.sourceTrackingData.memmap.kvaddr = NULL;
5385 this_adm.sourceTrackingData.memmap.paddr = 0;
5386 this_adm.sourceTrackingData.apr_cmd_status = -1;
5387 atomic_set(&this_adm.mem_map_handles[ADM_MEM_MAP_INDEX_SOURCE_TRACKING],
5388 0);
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305389 mutex_init(&dts_srs_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305390
5391 return 0;
5392}
5393
Asish Bhattacharya5faacb32017-12-04 17:23:15 +05305394void adm_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305395{
Laxminath Kasam468ece32017-11-28 12:40:22 +05305396 if (this_adm.apr)
5397 adm_reset_data();
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05305398 mutex_destroy(&dts_srs_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05305399 adm_delete_cal_data();
5400}