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