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