blob: 645ecf5cfd2d7276ae3df138b957a239ebf0e203 [file] [log] [blame]
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301/*
2 * Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/err.h>
15#include <linux/module.h>
16#include <linux/bitops.h>
17#include <sound/control.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053018#include <dsp/q6adm-v2.h>
19#include <dsp/q6core.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053020
21#include "msm-ds2-dap-config.h"
22#include "msm-pcm-routing-v2.h"
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053023
24
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053025/* ramp up/down for 30ms */
26#define DOLBY_SOFT_VOLUME_PERIOD 40
27/* Step value 0ms or 0us */
28#define DOLBY_SOFT_VOLUME_STEP 1000
29#define DOLBY_ADDITIONAL_RAMP_WAIT 10
30#define SOFT_VOLUME_PARAM_SIZE 3
31#define PARAM_PAYLOAD_SIZE 3
32
33enum {
34 DOLBY_SOFT_VOLUME_CURVE_LINEAR = 0,
35 DOLBY_SOFT_VOLUME_CURVE_EXP,
36 DOLBY_SOFT_VOLUME_CURVE_LOG,
37};
38
39#define VOLUME_ZERO_GAIN 0x0
40#define VOLUME_UNITY_GAIN 0x2000
41/* Wait time for module enable/disble */
42#define DOLBY_MODULE_ENABLE_PERIOD 50
43
44/* DOLBY device definitions end */
45enum {
46 DOLBY_OFF_CACHE = 0,
47 DOLBY_SPEAKER_CACHE,
48 DOLBY_HEADPHONE_CACHE,
49 DOLBY_HDMI_CACHE,
50 DOLBY_WFD_CACHE,
51 DOLBY_FM_CACHE,
52 DOLBY_MAX_CACHE,
53};
54
55enum {
56 DAP_SOFT_BYPASS = 0,
57 DAP_HARD_BYPASS,
58};
59
60enum {
61 MODULE_DISABLE = 0,
62 MODULE_ENABLE,
63};
64/* dolby param ids to/from dsp */
65static uint32_t ds2_dap_params_id[MAX_DS2_PARAMS] = {
66 DOLBY_PARAM_ID_VDHE, DOLBY_PARAM_ID_VSPE, DOLBY_PARAM_ID_DSSF,
67 DOLBY_PARAM_ID_DVLI, DOLBY_PARAM_ID_DVLO, DOLBY_PARAM_ID_DVLE,
68 DOLBY_PARAM_ID_DVMC, DOLBY_PARAM_ID_DVME, DOLBY_PARAM_ID_IENB,
69 DOLBY_PARAM_ID_IEBF, DOLBY_PARAM_ID_IEON, DOLBY_PARAM_ID_DEON,
70 DOLBY_PARAM_ID_NGON, DOLBY_PARAM_ID_GEON, DOLBY_PARAM_ID_GENB,
71 DOLBY_PARAM_ID_GEBF, DOLBY_PARAM_ID_AONB, DOLBY_PARAM_ID_AOBF,
72 DOLBY_PARAM_ID_AOBG, DOLBY_PARAM_ID_AOON, DOLBY_PARAM_ID_ARNB,
73 DOLBY_PARAM_ID_ARBF, DOLBY_PARAM_ID_PLB, DOLBY_PARAM_ID_PLMD,
74 DOLBY_PARAM_ID_DHSB, DOLBY_PARAM_ID_DHRG, DOLBY_PARAM_ID_DSSB,
75 DOLBY_PARAM_ID_DSSA, DOLBY_PARAM_ID_DVLA, DOLBY_PARAM_ID_IEBT,
76 DOLBY_PARAM_ID_IEA, DOLBY_PARAM_ID_DEA, DOLBY_PARAM_ID_DED,
77 DOLBY_PARAM_ID_GEBG, DOLBY_PARAM_ID_AOCC, DOLBY_PARAM_ID_ARBI,
78 DOLBY_PARAM_ID_ARBL, DOLBY_PARAM_ID_ARBH, DOLBY_PARAM_ID_AROD,
79 DOLBY_PARAM_ID_ARTP, DOLBY_PARAM_ID_VMON, DOLBY_PARAM_ID_VMB,
80 DOLBY_PARAM_ID_VCNB, DOLBY_PARAM_ID_VCBF, DOLBY_PARAM_ID_PREG,
81 DOLBY_PARAM_ID_VEN, DOLBY_PARAM_ID_PSTG, DOLBY_PARAM_ID_INIT_ENDP,
82};
83
84/* modifed state: 0x00000000 - Not updated
85 * > 0x00000000 && < 0x00010000
86 * Updated and not committed to DSP
87 * 0x00010001 - Updated and committed to DSP
88 * > 0x00010001 - Modified the committed value
89 */
90/* param offset */
91static uint32_t ds2_dap_params_offset[MAX_DS2_PARAMS] = {
92 DOLBY_PARAM_VDHE_OFFSET, DOLBY_PARAM_VSPE_OFFSET,
93 DOLBY_PARAM_DSSF_OFFSET, DOLBY_PARAM_DVLI_OFFSET,
94 DOLBY_PARAM_DVLO_OFFSET, DOLBY_PARAM_DVLE_OFFSET,
95 DOLBY_PARAM_DVMC_OFFSET, DOLBY_PARAM_DVME_OFFSET,
96 DOLBY_PARAM_IENB_OFFSET, DOLBY_PARAM_IEBF_OFFSET,
97 DOLBY_PARAM_IEON_OFFSET, DOLBY_PARAM_DEON_OFFSET,
98 DOLBY_PARAM_NGON_OFFSET, DOLBY_PARAM_GEON_OFFSET,
99 DOLBY_PARAM_GENB_OFFSET, DOLBY_PARAM_GEBF_OFFSET,
100 DOLBY_PARAM_AONB_OFFSET, DOLBY_PARAM_AOBF_OFFSET,
101 DOLBY_PARAM_AOBG_OFFSET, DOLBY_PARAM_AOON_OFFSET,
102 DOLBY_PARAM_ARNB_OFFSET, DOLBY_PARAM_ARBF_OFFSET,
103 DOLBY_PARAM_PLB_OFFSET, DOLBY_PARAM_PLMD_OFFSET,
104 DOLBY_PARAM_DHSB_OFFSET, DOLBY_PARAM_DHRG_OFFSET,
105 DOLBY_PARAM_DSSB_OFFSET, DOLBY_PARAM_DSSA_OFFSET,
106 DOLBY_PARAM_DVLA_OFFSET, DOLBY_PARAM_IEBT_OFFSET,
107 DOLBY_PARAM_IEA_OFFSET, DOLBY_PARAM_DEA_OFFSET,
108 DOLBY_PARAM_DED_OFFSET, DOLBY_PARAM_GEBG_OFFSET,
109 DOLBY_PARAM_AOCC_OFFSET, DOLBY_PARAM_ARBI_OFFSET,
110 DOLBY_PARAM_ARBL_OFFSET, DOLBY_PARAM_ARBH_OFFSET,
111 DOLBY_PARAM_AROD_OFFSET, DOLBY_PARAM_ARTP_OFFSET,
112 DOLBY_PARAM_VMON_OFFSET, DOLBY_PARAM_VMB_OFFSET,
113 DOLBY_PARAM_VCNB_OFFSET, DOLBY_PARAM_VCBF_OFFSET,
114 DOLBY_PARAM_PREG_OFFSET, DOLBY_PARAM_VEN_OFFSET,
115 DOLBY_PARAM_PSTG_OFFSET, DOLBY_PARAM_INT_ENDP_OFFSET,
116};
117/* param_length */
118static uint32_t ds2_dap_params_length[MAX_DS2_PARAMS] = {
119 DOLBY_PARAM_VDHE_LENGTH, DOLBY_PARAM_VSPE_LENGTH,
120 DOLBY_PARAM_DSSF_LENGTH, DOLBY_PARAM_DVLI_LENGTH,
121 DOLBY_PARAM_DVLO_LENGTH, DOLBY_PARAM_DVLE_LENGTH,
122 DOLBY_PARAM_DVMC_LENGTH, DOLBY_PARAM_DVME_LENGTH,
123 DOLBY_PARAM_IENB_LENGTH, DOLBY_PARAM_IEBF_LENGTH,
124 DOLBY_PARAM_IEON_LENGTH, DOLBY_PARAM_DEON_LENGTH,
125 DOLBY_PARAM_NGON_LENGTH, DOLBY_PARAM_GEON_LENGTH,
126 DOLBY_PARAM_GENB_LENGTH, DOLBY_PARAM_GEBF_LENGTH,
127 DOLBY_PARAM_AONB_LENGTH, DOLBY_PARAM_AOBF_LENGTH,
128 DOLBY_PARAM_AOBG_LENGTH, DOLBY_PARAM_AOON_LENGTH,
129 DOLBY_PARAM_ARNB_LENGTH, DOLBY_PARAM_ARBF_LENGTH,
130 DOLBY_PARAM_PLB_LENGTH, DOLBY_PARAM_PLMD_LENGTH,
131 DOLBY_PARAM_DHSB_LENGTH, DOLBY_PARAM_DHRG_LENGTH,
132 DOLBY_PARAM_DSSB_LENGTH, DOLBY_PARAM_DSSA_LENGTH,
133 DOLBY_PARAM_DVLA_LENGTH, DOLBY_PARAM_IEBT_LENGTH,
134 DOLBY_PARAM_IEA_LENGTH, DOLBY_PARAM_DEA_LENGTH,
135 DOLBY_PARAM_DED_LENGTH, DOLBY_PARAM_GEBG_LENGTH,
136 DOLBY_PARAM_AOCC_LENGTH, DOLBY_PARAM_ARBI_LENGTH,
137 DOLBY_PARAM_ARBL_LENGTH, DOLBY_PARAM_ARBH_LENGTH,
138 DOLBY_PARAM_AROD_LENGTH, DOLBY_PARAM_ARTP_LENGTH,
139 DOLBY_PARAM_VMON_LENGTH, DOLBY_PARAM_VMB_LENGTH,
140 DOLBY_PARAM_VCNB_LENGTH, DOLBY_PARAM_VCBF_LENGTH,
141 DOLBY_PARAM_PREG_LENGTH, DOLBY_PARAM_VEN_LENGTH,
142 DOLBY_PARAM_PSTG_LENGTH, DOLBY_PARAM_INT_ENDP_LENGTH,
143};
144
145struct ds2_dap_params_s {
146 int32_t params_val[TOTAL_LENGTH_DS2_PARAM];
147 int32_t dap_params_modified[MAX_DS2_PARAMS];
148};
149
150struct audio_rx_cal_data {
151 char aud_proc_data[AUD_PROC_BLOCK_SIZE];
152 int32_t aud_proc_size;
153 char aud_vol_data[AUD_VOL_BLOCK_SIZE];
154 int32_t aud_vol_size;
155};
156
157static struct ds2_dap_params_s ds2_dap_params[DOLBY_MAX_CACHE];
158
159struct ds2_device_mapping {
160 int32_t device_id; /* audio_out_... */
161 int port_id; /* afe port. constant for a target variant. routing-v2*/
162 /*Only one Dolby COPP for a specific port*/
163 int copp_idx; /* idx for the copp port on which ds2 is active */
164 int cache_dev; /* idx to a shared parameter array dependent on device*/
165 uint32_t stream_ref_count;
166 bool active;
167 void *cal_data;
168};
169
170static struct ds2_device_mapping dev_map[DS2_DEVICES_ALL];
171
172struct ds2_dap_params_states_s {
173 bool use_cache;
174 bool dap_bypass;
175 bool dap_bypass_type;
176 bool node_opened;
177 int32_t device;
178 bool custom_stereo_onoff;
179};
180
181static struct ds2_dap_params_states_s ds2_dap_params_states = {true, false,
182 false, DEVICE_NONE};
183
184static int all_supported_devices = EARPIECE|SPEAKER|WIRED_HEADSET|
185 WIRED_HEADPHONE|BLUETOOTH_SCO|AUX_DIGITAL|
186 ANLG_DOCK_HEADSET|DGTL_DOCK_HEADSET|
187 REMOTE_SUBMIX|ANC_HEADSET|ANC_HEADPHONE|
188 PROXY|FM|FM_TX|DEVICE_NONE|
189 BLUETOOTH_SCO_HEADSET|BLUETOOTH_SCO_CARKIT;
190
191
192static void msm_ds2_dap_check_and_update_ramp_wait(int port_id, int copp_idx,
193 int *ramp_wait)
194{
195
196 int32_t *update_params_value = NULL;
197 uint32_t params_length = SOFT_VOLUME_PARAM_SIZE * sizeof(uint32_t);
198 uint32_t param_payload_len = PARAM_PAYLOAD_SIZE * sizeof(uint32_t);
199 int rc = 0;
200
201 update_params_value = kzalloc(params_length + param_payload_len,
202 GFP_KERNEL);
203 if (!update_params_value)
204 goto end;
205
206 rc = adm_get_params(port_id, copp_idx,
207 AUDPROC_MODULE_ID_VOL_CTRL,
208 AUDPROC_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS,
209 params_length + param_payload_len,
210 (char *) update_params_value);
211 if (rc == 0) {
212 pr_debug("%s: params_value [0x%x, 0x%x, 0x%x]\n",
213 __func__, update_params_value[0],
214 update_params_value[1],
215 update_params_value[2]);
216 *ramp_wait = update_params_value[0];
217 }
218end:
219 kfree(update_params_value);
220 /*
221 * No error returned as we do not need to error out from dap on/dap
222 * bypass. The default ramp parameter will be used to wait during
223 * ramp down.
224 */
225}
226
227static int msm_ds2_dap_set_vspe_vdhe(int dev_map_idx,
228 bool is_custom_stereo_enabled)
229{
230 int32_t *update_params_value = NULL;
231 int32_t *param_val = NULL;
232 int idx, i, j, rc = 0, cdev;
233 uint32_t params_length = (TOTAL_LENGTH_DOLBY_PARAM +
234 2 * DOLBY_PARAM_PAYLOAD_SIZE) *
235 sizeof(uint32_t);
236
237 if (dev_map_idx < 0 || dev_map_idx >= DS2_DEVICES_ALL) {
238 pr_err("%s: invalid dev map index %d\n", __func__, dev_map_idx);
239 rc = -EINVAL;
240 goto end;
241 }
242
243 if (dev_map[dev_map_idx].port_id == DOLBY_INVALID_PORT_ID) {
244 pr_err("%s: Invalid port id\n", __func__);
245 rc = -EINVAL;
246 goto end;
247 }
248
249 if ((dev_map[dev_map_idx].copp_idx < 0) ||
250 (dev_map[dev_map_idx].copp_idx >= MAX_COPPS_PER_PORT)) {
251 pr_err("%s: Invalid copp_idx\n", __func__);
252 rc = -EINVAL;
253 goto end;
254 }
255
256 if ((dev_map[dev_map_idx].port_id != SLIMBUS_0_RX) &&
257 (dev_map[dev_map_idx].port_id != RT_PROXY_PORT_001_RX)) {
258 pr_debug("%s:No Custom stereo for port:0x%x\n",
259 __func__, dev_map[dev_map_idx].port_id);
260 goto end;
261 }
262
263 update_params_value = kzalloc(params_length, GFP_KERNEL);
264 if (!update_params_value) {
265 rc = -ENOMEM;
266 goto end;
267 }
268 params_length = 0;
269 param_val = update_params_value;
270 cdev = dev_map[dev_map_idx].cache_dev;
271 /* for VDHE and VSPE DAP params at index 0 and 1 in table */
272 for (i = 0; i < 2; i++) {
273 *update_params_value++ = DOLBY_BUNDLE_MODULE_ID;
274 *update_params_value++ = ds2_dap_params_id[i];
275 *update_params_value++ = ds2_dap_params_length[i] *
276 sizeof(uint32_t);
277 idx = ds2_dap_params_offset[i];
278 for (j = 0; j < ds2_dap_params_length[i]; j++) {
279 if (is_custom_stereo_enabled)
280 *update_params_value++ = 0;
281 else
282 *update_params_value++ =
283 ds2_dap_params[cdev].params_val[idx+j];
284 }
285 params_length += (DOLBY_PARAM_PAYLOAD_SIZE +
286 ds2_dap_params_length[i]) *
287 sizeof(uint32_t);
288 }
289
290 pr_debug("%s: valid param length: %d\n", __func__, params_length);
291 if (params_length) {
292 rc = adm_dolby_dap_send_params(dev_map[dev_map_idx].port_id,
293 dev_map[dev_map_idx].copp_idx,
294 (char *)param_val,
295 params_length);
296 if (rc) {
297 pr_err("%s: send vdhe/vspe params failed with rc=%d\n",
298 __func__, rc);
299 rc = -EINVAL;
300 goto end;
301 }
302 }
303end:
304 kfree(param_val);
305 return rc;
306}
307
308int qti_set_custom_stereo_on(int port_id, int copp_idx,
309 bool is_custom_stereo_on)
310{
311
312 uint16_t op_FL_ip_FL_weight;
313 uint16_t op_FL_ip_FR_weight;
314 uint16_t op_FR_ip_FL_weight;
315 uint16_t op_FR_ip_FR_weight;
316
317 int32_t *update_params_value32 = NULL, rc = 0;
318 int32_t *param_val = NULL;
319 int16_t *update_params_value16 = 0;
320 uint32_t params_length_bytes = CUSTOM_STEREO_PAYLOAD_SIZE *
321 sizeof(uint32_t);
322 uint32_t avail_length = params_length_bytes;
323
324 if ((port_id != SLIMBUS_0_RX) &&
325 (port_id != RT_PROXY_PORT_001_RX)) {
326 pr_debug("%s:No Custom stereo for port:0x%x\n",
327 __func__, port_id);
328 goto skip_send_cmd;
329 }
330
331 pr_debug("%s: port 0x%x, copp_idx %d, is_custom_stereo_on %d\n",
332 __func__, port_id, copp_idx, is_custom_stereo_on);
333 if (is_custom_stereo_on) {
334 op_FL_ip_FL_weight =
335 Q14_GAIN_ZERO_POINT_FIVE;
336 op_FL_ip_FR_weight =
337 Q14_GAIN_ZERO_POINT_FIVE;
338 op_FR_ip_FL_weight =
339 Q14_GAIN_ZERO_POINT_FIVE;
340 op_FR_ip_FR_weight =
341 Q14_GAIN_ZERO_POINT_FIVE;
342 } else {
343 op_FL_ip_FL_weight = Q14_GAIN_UNITY;
344 op_FL_ip_FR_weight = 0;
345 op_FR_ip_FL_weight = 0;
346 op_FR_ip_FR_weight = Q14_GAIN_UNITY;
347 }
348
349 update_params_value32 = kzalloc(params_length_bytes, GFP_KERNEL);
350 if (!update_params_value32) {
351 rc = -ENOMEM;
352 goto skip_send_cmd;
353 }
354 param_val = update_params_value32;
355 if (avail_length < 2 * sizeof(uint32_t))
356 goto skip_send_cmd;
357 *update_params_value32++ = MTMX_MODULE_ID_DEFAULT_CHMIXER;
358 *update_params_value32++ = DEFAULT_CHMIXER_PARAM_ID_COEFF;
359 avail_length = avail_length - (2 * sizeof(uint32_t));
360
361 update_params_value16 = (int16_t *)update_params_value32;
362 if (avail_length < 10 * sizeof(uint16_t))
363 goto skip_send_cmd;
364 *update_params_value16++ = CUSTOM_STEREO_CMD_PARAM_SIZE;
365 /* for alignment only*/
366 *update_params_value16++ = 0;
367 /* index is 32-bit param in little endian*/
368 *update_params_value16++ = CUSTOM_STEREO_INDEX_PARAM;
369 *update_params_value16++ = 0;
370 /* for stereo mixing num out ch*/
371 *update_params_value16++ = CUSTOM_STEREO_NUM_OUT_CH;
372 /* for stereo mixing num in ch*/
373 *update_params_value16++ = CUSTOM_STEREO_NUM_IN_CH;
374
375 /* Out ch map FL/FR*/
376 *update_params_value16++ = PCM_CHANNEL_FL;
377 *update_params_value16++ = PCM_CHANNEL_FR;
378
379 /* In ch map FL/FR*/
380 *update_params_value16++ = PCM_CHANNEL_FL;
381 *update_params_value16++ = PCM_CHANNEL_FR;
382 avail_length = avail_length - (10 * sizeof(uint16_t));
383 /* weighting coefficients as name suggests,
384 * mixing will be done according to these coefficients
385 */
386 if (avail_length < 4 * sizeof(uint16_t))
387 goto skip_send_cmd;
388 *update_params_value16++ = op_FL_ip_FL_weight;
389 *update_params_value16++ = op_FL_ip_FR_weight;
390 *update_params_value16++ = op_FR_ip_FL_weight;
391 *update_params_value16++ = op_FR_ip_FR_weight;
392 avail_length = avail_length - (4 * sizeof(uint16_t));
393 if (params_length_bytes != 0) {
394 rc = adm_dolby_dap_send_params(port_id, copp_idx,
395 (char *)param_val,
396 params_length_bytes);
397 if (rc) {
398 pr_err("%s: send params failed rc=%d\n", __func__, rc);
399 rc = -EINVAL;
400 goto skip_send_cmd;
401 }
402 }
403 kfree(param_val);
404 return 0;
405skip_send_cmd:
406 pr_err("%s: insufficient memory, send cmd failed\n",
407 __func__);
408 kfree(param_val);
409 return rc;
410}
411static int dap_set_custom_stereo_onoff(int dev_map_idx,
412 bool is_custom_stereo_enabled)
413{
414
415 int32_t *update_params_value = NULL, rc = 0;
416 int32_t *param_val = NULL;
417 uint32_t params_length_bytes = (TOTAL_LENGTH_DOLBY_PARAM +
418 DOLBY_PARAM_PAYLOAD_SIZE) * sizeof(uint32_t);
419 if ((dev_map[dev_map_idx].port_id != SLIMBUS_0_RX) &&
420 (dev_map[dev_map_idx].port_id != RT_PROXY_PORT_001_RX)) {
421 pr_debug("%s:No Custom stereo for port:0x%x\n",
422 __func__, dev_map[dev_map_idx].port_id);
423 goto end;
424 }
425
426 if ((dev_map[dev_map_idx].copp_idx < 0) ||
427 (dev_map[dev_map_idx].copp_idx >= MAX_COPPS_PER_PORT)) {
428 rc = -EINVAL;
429 goto end;
430 }
431
432 /* DAP custom stereo */
433 msm_ds2_dap_set_vspe_vdhe(dev_map_idx,
434 is_custom_stereo_enabled);
435 update_params_value = kzalloc(params_length_bytes, GFP_KERNEL);
436 if (!update_params_value) {
437 pr_err("%s: params memory alloc failed\n", __func__);
438 rc = -ENOMEM;
439 goto end;
440 }
441 params_length_bytes = 0;
442 param_val = update_params_value;
443 *update_params_value++ = DOLBY_BUNDLE_MODULE_ID;
444 *update_params_value++ = DOLBY_ENABLE_CUSTOM_STEREO;
445 *update_params_value++ = sizeof(uint32_t);
446 if (is_custom_stereo_enabled)
447 *update_params_value++ = 1;
448 else
449 *update_params_value++ = 0;
450 params_length_bytes += (DOLBY_PARAM_PAYLOAD_SIZE + 1) *
451 sizeof(uint32_t);
452 pr_debug("%s: valid param length: %d\n", __func__, params_length_bytes);
453 if (params_length_bytes) {
454 rc = adm_dolby_dap_send_params(dev_map[dev_map_idx].port_id,
455 dev_map[dev_map_idx].copp_idx,
456 (char *)param_val,
457 params_length_bytes);
458 if (rc) {
459 pr_err("%s: custom stereo param failed with rc=%d\n",
460 __func__, rc);
461 rc = -EINVAL;
462 goto end;
463 }
464 }
465end:
466 kfree(param_val);
467 return rc;
468
469}
470
471
472static int set_custom_stereo_onoff(int dev_map_idx,
473 bool is_custom_stereo_enabled)
474{
475 int rc = 0;
476
477 pr_debug("%s: map index %d, custom stereo %d\n", __func__, dev_map_idx,
478 is_custom_stereo_enabled);
479
480 if (dev_map_idx < 0 || dev_map_idx >= DS2_DEVICES_ALL) {
481 pr_err("%s: invalid dev map index %d\n", __func__, dev_map_idx);
482 rc = -EINVAL;
483 goto end;
484 }
485
486 if (dev_map[dev_map_idx].port_id == DOLBY_INVALID_PORT_ID) {
487 pr_err("%s: invalid port id\n", __func__);
488 rc = -EINVAL;
489 goto end;
490 }
491
492 if ((dev_map[dev_map_idx].copp_idx < 0) ||
493 (dev_map[dev_map_idx].copp_idx >= MAX_COPPS_PER_PORT)) {
494 pr_err("%s: invalid copp idx\n", __func__);
495 rc = -EINVAL;
496 goto end;
497 }
498
499 if (ds2_dap_params_states.dap_bypass == true &&
500 ds2_dap_params_states.dap_bypass_type == DAP_HARD_BYPASS) {
501
502 rc = qti_set_custom_stereo_on(dev_map[dev_map_idx].port_id,
503 dev_map[dev_map_idx].copp_idx,
504 is_custom_stereo_enabled);
505 if (rc < 0) {
506 pr_err("%s:qti_set_custom_stereo_on_copp failed C.S %d",
507 __func__, is_custom_stereo_enabled);
508 }
509 goto end;
510
511 }
512
513 if (ds2_dap_params_states.dap_bypass == false) {
514 rc = dap_set_custom_stereo_onoff(dev_map_idx,
515 is_custom_stereo_enabled);
516 if (rc < 0) {
517 pr_err("%s:qti_set_custom_stereo_on_copp failed C.S %d",
518 __func__, is_custom_stereo_enabled);
519 }
520 goto end;
521 }
522end:
523 return rc;
524}
525
526static int msm_ds2_dap_alloc_and_store_cal_data(int dev_map_idx, int path,
527 int perf_mode)
528{
529 int rc = 0;
530 struct audio_rx_cal_data *aud_cal_data;
531
532 pr_debug("%s: path %d, perf_mode %d, dev_map_idx %d\n",
533 __func__, path, perf_mode, dev_map_idx);
534
535 if (dev_map_idx < 0 || dev_map_idx >= DS2_DEVICES_ALL) {
536 pr_err("%s: invalid dev map index %d\n", __func__, dev_map_idx);
537 rc = -EINVAL;
538 goto end;
539 }
540
541 aud_cal_data = kzalloc(sizeof(struct audio_rx_cal_data), GFP_KERNEL);
542 if (!aud_cal_data) {
543 rc = -ENOMEM;
544 goto end;
545 }
546
547 rc = adm_store_cal_data(dev_map[dev_map_idx].port_id,
548 dev_map[dev_map_idx].copp_idx, path, perf_mode,
549 ADM_AUDPROC_CAL, aud_cal_data->aud_proc_data,
550 &aud_cal_data->aud_proc_size);
551 if (rc < 0) {
552 pr_err("%s: store cal data err %d\n", __func__, rc);
553 kfree(aud_cal_data);
554 goto end;
555 }
556
557 rc = adm_store_cal_data(dev_map[dev_map_idx].port_id,
558 dev_map[dev_map_idx].copp_idx, path, perf_mode,
559 ADM_AUDVOL_CAL, aud_cal_data->aud_vol_data,
560 &aud_cal_data->aud_vol_size);
561 if (rc < 0) {
562 pr_err("%s: store cal data err %d\n", __func__, rc);
563 kfree(aud_cal_data);
564 goto end;
565 }
566
567 dev_map[dev_map_idx].cal_data = (void *)aud_cal_data;
568
569end:
570 pr_debug("%s: ret %d\n", __func__, rc);
571 return rc;
572}
573
574static int msm_ds2_dap_free_cal_data(int dev_map_idx)
575{
576 int rc = 0;
577 struct audio_rx_cal_data *aud_cal_data;
578
579 pr_debug("%s: dev_map_idx %d\n", __func__, dev_map_idx);
580 if (dev_map_idx < 0 || dev_map_idx >= DS2_DEVICES_ALL) {
581 pr_err("%s: invalid dev map index %d\n", __func__, dev_map_idx);
582 rc = -EINVAL;
583 goto end;
584 }
585 aud_cal_data = (struct audio_rx_cal_data *)
586 dev_map[dev_map_idx].cal_data;
587 kfree(aud_cal_data);
588 dev_map[dev_map_idx].cal_data = NULL;
589
590end:
591 return rc;
592}
593
594static int msm_ds2_dap_send_cal_data(int dev_map_idx)
595{
596 int rc = 0;
597 struct audio_rx_cal_data *aud_cal_data = NULL;
598
599 pr_debug("%s: devmap index %d\n", __func__, dev_map_idx);
600 if (dev_map_idx < 0 || dev_map_idx >= DS2_DEVICES_ALL) {
601 pr_err("%s: invalid dev map index %d\n", __func__, dev_map_idx);
602 rc = -EINVAL;
603 goto end;
604 }
605
606 if (dev_map[dev_map_idx].cal_data == NULL) {
607 pr_err("%s: No valid calibration data stored for idx %d\n",
608 __func__, dev_map_idx);
609 rc = -EINVAL;
610 goto end;
611 }
612
613 /* send aud proc cal */
614 aud_cal_data = (struct audio_rx_cal_data *)
615 dev_map[dev_map_idx].cal_data;
616 rc = adm_send_calibration(dev_map[dev_map_idx].port_id,
617 dev_map[dev_map_idx].copp_idx,
618 ADM_PATH_PLAYBACK, 0,
619 ADM_AUDPROC_CAL,
620 aud_cal_data->aud_proc_data,
621 aud_cal_data->aud_proc_size);
622 if (rc < 0) {
623 pr_err("%s: adm_send_calibration failed %d\n", __func__, rc);
624 goto end;
625 }
626
627 /* send aud volume cal*/
628 rc = adm_send_calibration(dev_map[dev_map_idx].port_id,
629 dev_map[dev_map_idx].copp_idx,
630 ADM_PATH_PLAYBACK, 0,
631 ADM_AUDVOL_CAL,
632 aud_cal_data->aud_vol_data,
633 aud_cal_data->aud_vol_size);
634 if (rc < 0)
635 pr_err("%s: adm_send_calibration failed %d\n", __func__, rc);
636end:
637 pr_debug("%s: return %d\n", __func__, rc);
638 return rc;
639}
640
641static inline int msm_ds2_dap_can_enable_module(int32_t module_id)
642{
643 if (module_id == MTMX_MODULE_ID_DEFAULT_CHMIXER ||
644 module_id == AUDPROC_MODULE_ID_RESAMPLER ||
645 module_id == AUDPROC_MODULE_ID_VOL_CTRL) {
646 return false;
647 }
648 return true;
649}
650
651static int msm_ds2_dap_init_modules_in_topology(int dev_map_idx)
652{
653 int rc = 0, i = 0, port_id, copp_idx;
654 /* Account for 32 bit integer allocation */
655 int32_t param_sz = (ADM_GET_TOPO_MODULE_LIST_LENGTH / sizeof(uint32_t));
656 int32_t *update_param_val = NULL;
657
658 if (dev_map_idx < 0 || dev_map_idx >= DS2_DEVICES_ALL) {
659 pr_err("%s: invalid dev map index %d\n", __func__, dev_map_idx);
660 rc = -EINVAL;
661 goto end;
662 }
663
664 port_id = dev_map[dev_map_idx].port_id;
665 copp_idx = dev_map[dev_map_idx].copp_idx;
666 pr_debug("%s: port_id 0x%x copp_idx %d\n", __func__, port_id, copp_idx);
667 update_param_val = kzalloc(ADM_GET_TOPO_MODULE_LIST_LENGTH, GFP_KERNEL);
668 if (!update_param_val) {
669 pr_err("%s, param memory alloc failed\n", __func__);
670 rc = -ENOMEM;
671 goto end;
672 }
673
674 if (!ds2_dap_params_states.dap_bypass) {
675 /* get modules from dsp */
676 rc = adm_get_pp_topo_module_list(port_id, copp_idx,
677 ADM_GET_TOPO_MODULE_LIST_LENGTH,
678 (char *)update_param_val);
679 if (rc < 0) {
680 pr_err("%s:topo list port %d, err %d,copp_idx %d\n",
681 __func__, port_id, copp_idx, rc);
682 goto end;
683 }
684
685 if (update_param_val[0] > (param_sz - 1)) {
686 pr_err("%s:max modules exp/ret [%d: %d]\n",
687 __func__, (param_sz - 1),
688 update_param_val[0]);
689 rc = -EINVAL;
690 goto end;
691 }
692 /* Turn off modules */
693 for (i = 1; i < update_param_val[0]; i++) {
694 if (!msm_ds2_dap_can_enable_module(
695 update_param_val[i]) ||
696 (update_param_val[i] == DS2_MODULE_ID)) {
697 pr_debug("%s: Do not enable/disable %d\n",
698 __func__, update_param_val[i]);
699 continue;
700 }
701
702 pr_debug("%s: param disable %d\n",
703 __func__, update_param_val[i]);
704 adm_param_enable(port_id, copp_idx, update_param_val[i],
705 MODULE_DISABLE);
706 }
707 } else {
708 msm_ds2_dap_send_cal_data(dev_map_idx);
709
710 }
711 adm_param_enable(port_id, copp_idx, DS2_MODULE_ID,
712 !ds2_dap_params_states.dap_bypass);
713end:
714 kfree(update_param_val);
715 return rc;
716}
717
718static bool msm_ds2_dap_check_is_param_modified(int32_t *dap_params_modified,
719 int32_t idx, int32_t commit)
720{
721 if ((dap_params_modified[idx] == 0) ||
722 (commit &&
723 ((dap_params_modified[idx] & 0x00010000) &&
724 ((dap_params_modified[idx] & 0x0000FFFF) <= 1)))) {
725 pr_debug("%s: not modified at idx %d\n", __func__, idx);
726 return false;
727 }
728 pr_debug("%s: modified at idx %d\n", __func__, idx);
729 return true;
730}
731
732static int msm_ds2_dap_map_device_to_dolby_cache_devices(int32_t device_id)
733{
734 int32_t cache_dev = -1;
735
736 switch (device_id) {
737 case DEVICE_NONE:
738 cache_dev = DOLBY_OFF_CACHE;
739 break;
740 case EARPIECE:
741 case SPEAKER:
742 cache_dev = DOLBY_SPEAKER_CACHE;
743 break;
744 case WIRED_HEADSET:
745 case WIRED_HEADPHONE:
746 case ANLG_DOCK_HEADSET:
747 case DGTL_DOCK_HEADSET:
748 case ANC_HEADSET:
749 case ANC_HEADPHONE:
750 case BLUETOOTH_SCO:
751 case BLUETOOTH_SCO_HEADSET:
752 case BLUETOOTH_SCO_CARKIT:
753 cache_dev = DOLBY_HEADPHONE_CACHE;
754 break;
755 case FM:
756 case FM_TX:
757 cache_dev = DOLBY_FM_CACHE;
758 break;
759 case AUX_DIGITAL:
760 cache_dev = DOLBY_HDMI_CACHE;
761 break;
762 case PROXY:
763 case REMOTE_SUBMIX:
764 cache_dev = DOLBY_WFD_CACHE;
765 break;
766 default:
767 pr_err("%s: invalid cache device\n", __func__);
768 }
769 pr_debug("%s: cache device %d\n", __func__, cache_dev);
770 return cache_dev;
771}
772
773static int msm_ds2_dap_update_num_devices(struct dolby_param_data *dolby_data,
774 int32_t *num_device, int32_t *dev_arr,
775 int32_t array_size)
776{
777 int32_t idx = 0;
778 int supported_devices = 0;
779
780 if (!array_size) {
781 pr_err("%s: array size zero\n", __func__);
782 return -EINVAL;
783 }
784
785 if (dolby_data->device_id == DEVICE_OUT_ALL ||
786 dolby_data->device_id == DEVICE_OUT_DEFAULT)
787 supported_devices = all_supported_devices;
788 else
789 supported_devices = dolby_data->device_id;
790
791 if ((idx < array_size) && (supported_devices & EARPIECE))
792 dev_arr[idx++] = EARPIECE;
793 if ((idx < array_size) && (supported_devices & SPEAKER))
794 dev_arr[idx++] = SPEAKER;
795 if ((idx < array_size) && (supported_devices & WIRED_HEADSET))
796 dev_arr[idx++] = WIRED_HEADSET;
797 if ((idx < array_size) && (supported_devices & WIRED_HEADPHONE))
798 dev_arr[idx++] = WIRED_HEADPHONE;
799 if ((idx < array_size) && (supported_devices & BLUETOOTH_SCO))
800 dev_arr[idx++] = BLUETOOTH_SCO;
801 if ((idx < array_size) && (supported_devices & BLUETOOTH_SCO_CARKIT))
802 dev_arr[idx++] = BLUETOOTH_SCO_CARKIT;
803 if ((idx < array_size) && (supported_devices & BLUETOOTH_SCO_HEADSET))
804 dev_arr[idx++] = BLUETOOTH_SCO_HEADSET;
805 if ((idx < array_size) && (supported_devices & AUX_DIGITAL))
806 dev_arr[idx++] = AUX_DIGITAL;
807 if ((idx < array_size) && (supported_devices & ANLG_DOCK_HEADSET))
808 dev_arr[idx++] = ANLG_DOCK_HEADSET;
809 if ((idx < array_size) && (supported_devices & DGTL_DOCK_HEADSET))
810 dev_arr[idx++] = DGTL_DOCK_HEADSET;
811 if ((idx < array_size) && (supported_devices & REMOTE_SUBMIX))
812 dev_arr[idx++] = REMOTE_SUBMIX;
813 if ((idx < array_size) && (supported_devices & ANC_HEADSET))
814 dev_arr[idx++] = ANC_HEADSET;
815 if ((idx < array_size) && (supported_devices & ANC_HEADPHONE))
816 dev_arr[idx++] = ANC_HEADPHONE;
817 if ((idx < array_size) && (supported_devices & PROXY))
818 dev_arr[idx++] = PROXY;
819 if ((idx < array_size) && (supported_devices & FM))
820 dev_arr[idx++] = FM;
821 if ((idx < array_size) && (supported_devices & FM_TX))
822 dev_arr[idx++] = FM_TX;
823 /* CHECK device none separately */
824 if ((idx < array_size) && (supported_devices == DEVICE_NONE))
825 dev_arr[idx++] = DEVICE_NONE;
826 pr_debug("%s: dev id 0x%x, idx %d\n", __func__,
827 supported_devices, idx);
828 *num_device = idx;
829 return 0;
830}
831
832static int msm_ds2_dap_get_port_id(
833 int32_t device_id, int32_t be_id)
834{
835 struct msm_pcm_routing_bdai_data bedais;
836 int port_id = DOLBY_INVALID_PORT_ID;
837 int port_type = 0;
838
839 if (be_id < 0) {
840 port_id = -1;
841 goto end;
842 }
843
844 msm_pcm_routing_get_bedai_info(be_id, &bedais);
845 pr_debug("%s: be port_id %d\n", __func__, bedais.port_id);
846 port_id = bedais.port_id;
847 port_type = afe_get_port_type(bedais.port_id);
848 if (port_type != MSM_AFE_PORT_TYPE_RX)
849 port_id = DOLBY_INVALID_PORT_ID;
850end:
851 pr_debug("%s: device_id 0x%x, be_id %d, port_id %d\n",
852 __func__, device_id, be_id, port_id);
853 return port_id;
854}
855
856static int msm_ds2_dap_update_dev_map_port_id(int32_t device_id, int port_id)
857{
858 int i;
859
860 for (i = 0; i < DS2_DEVICES_ALL; i++) {
861 if (dev_map[i].device_id == device_id)
862 dev_map[i].port_id = port_id;
863 }
864 pr_debug("%s: port_id %d, device_id 0x%x\n",
865 __func__, port_id, device_id);
866 return 0;
867}
868
869static int msm_ds2_dap_handle_bypass_wait(int port_id, int copp_idx,
870 int wait_time)
871{
872 int ret = 0;
873
874 adm_set_wait_parameters(port_id, copp_idx);
875 msm_pcm_routing_release_lock();
876 ret = adm_wait_timeout(port_id, copp_idx, wait_time);
877 msm_pcm_routing_acquire_lock();
878 /* Reset the parameters if wait has timed out */
879 if (ret == 0)
880 adm_reset_wait_parameters(port_id, copp_idx);
881 return ret;
882}
883
884static int msm_ds2_dap_handle_bypass(struct dolby_param_data *dolby_data)
885{
886 int rc = 0, i = 0, j = 0;
887 /*Account for 32 bit integer allocation */
888 int32_t param_sz = (ADM_GET_TOPO_MODULE_LIST_LENGTH / sizeof(uint32_t));
889 int32_t *mod_list = NULL;
890 int port_id = 0, copp_idx = -1;
891 bool cs_onoff = ds2_dap_params_states.custom_stereo_onoff;
892 int ramp_wait = DOLBY_SOFT_VOLUME_PERIOD;
893
894 pr_debug("%s: bypass type %d bypass %d custom stereo %d\n", __func__,
895 ds2_dap_params_states.dap_bypass_type,
896 ds2_dap_params_states.dap_bypass,
897 ds2_dap_params_states.custom_stereo_onoff);
898 mod_list = kzalloc(ADM_GET_TOPO_MODULE_LIST_LENGTH, GFP_KERNEL);
899 if (!mod_list) {
900 pr_err("%s: param memory alloc failed\n", __func__);
901 rc = -ENOMEM;
902 goto end;
903 }
904
905 for (i = 0; i < DS2_DEVICES_ALL; i++) {
906 pr_debug("%s: active dev %d\n", __func__, dev_map[i].active);
907 if (dev_map[i].active) {
908 port_id = dev_map[i].port_id;
909 copp_idx = dev_map[i].copp_idx;
910
911 if (port_id == DOLBY_INVALID_PORT_ID) {
912 pr_err("%s: invalid port\n", __func__);
913 rc = 0;
914 goto end;
915 }
916
917 if ((copp_idx < 0) ||
918 (copp_idx >= MAX_COPPS_PER_PORT)) {
919 pr_err("%s: Invalid copp_idx\n", __func__);
920 rc = 0;
921 goto end;
922 }
923
924 /* getmodules from dsp */
925 rc = adm_get_pp_topo_module_list(port_id, copp_idx,
926 ADM_GET_TOPO_MODULE_LIST_LENGTH,
927 (char *)mod_list);
928 if (rc < 0) {
929 pr_err("%s:adm get topo list port %d",
930 __func__, port_id);
931 pr_err("copp_idx %d, err %d\n",
932 copp_idx, rc);
933 goto end;
934 }
935 if (mod_list[0] > (param_sz - 1)) {
936 pr_err("%s:max modules exp/ret [%d: %d]\n",
937 __func__, (param_sz - 1),
938 mod_list[0]);
939 rc = -EINVAL;
940 goto end;
941 }
942 /*
943 * get ramp parameters
944 * check for change in ramp parameters
945 * update ramp wait
946 */
947 msm_ds2_dap_check_and_update_ramp_wait(port_id,
948 copp_idx,
949 &ramp_wait);
950
951 /* Mute before switching modules */
952 rc = adm_set_volume(port_id, copp_idx,
953 VOLUME_ZERO_GAIN);
954 if (rc < 0) {
955 /*
956 * Not Fatal can continue bypass operations.
957 * Do not need to block playback
958 */
959 pr_info("%s :Set volume port_id %d",
960 __func__, port_id);
961 pr_info("copp_idx %d, error %d\n",
962 copp_idx, rc);
963 }
964
965 rc = msm_ds2_dap_handle_bypass_wait(port_id, copp_idx,
966 (ramp_wait +
967 DOLBY_ADDITIONAL_RAMP_WAIT));
968 if (rc == -EINTR) {
969 pr_info("%s:bypass interrupted-ignore,port %d",
970 __func__, port_id);
971 pr_info("copp_idx %d\n", copp_idx);
972 rc = 0;
973 continue;
974 }
975
976 /* if dap bypass is set */
977 if (ds2_dap_params_states.dap_bypass) {
978 /* Turn off dap module */
979 adm_param_enable(port_id, copp_idx,
980 DS2_MODULE_ID, MODULE_DISABLE);
981 /*
982 * If custom stereo is on at the time of bypass,
983 * switch off custom stereo on dap and turn on
984 * custom stereo on qti channel mixer.
985 */
986 if (cs_onoff) {
987 rc = dap_set_custom_stereo_onoff(i,
988 !cs_onoff);
989 if (rc < 0) {
990 pr_info("%s:D_CS i %d,rc %d\n",
991 __func__, i, rc);
992 }
993 rc = qti_set_custom_stereo_on(port_id,
994 copp_idx,
995 cs_onoff);
996 if (rc < 0) {
997 pr_info("%s:Q_CS port id 0x%x",
998 __func__, port_id);
999 pr_info("copp idx %d, rc %d\n",
1000 copp_idx, rc);
1001 }
1002 }
1003 /* Turn on qti modules */
1004 for (j = 1; j < mod_list[0]; j++) {
1005 if (!msm_ds2_dap_can_enable_module(
1006 mod_list[j]) ||
1007 mod_list[j] ==
1008 DS2_MODULE_ID)
1009 continue;
1010 pr_debug("%s: param enable %d\n",
1011 __func__, mod_list[j]);
1012 adm_param_enable(port_id, copp_idx,
1013 mod_list[j],
1014 MODULE_ENABLE);
1015 }
1016
1017 /* Add adm api to resend calibration on port */
1018 rc = msm_ds2_dap_send_cal_data(i);
1019 if (rc < 0) {
1020 /*
1021 * Not fatal,continue bypass operations.
1022 * Do not need to block playback
1023 */
1024 pr_info("%s:send cal err %d index %d\n",
1025 __func__, rc, i);
1026 }
1027 } else {
1028 /* Turn off qti modules */
1029 for (j = 1; j < mod_list[0]; j++) {
1030 if (!msm_ds2_dap_can_enable_module(
1031 mod_list[j]) ||
1032 mod_list[j] ==
1033 DS2_MODULE_ID)
1034 continue;
1035 pr_debug("%s: param disable %d\n",
1036 __func__, mod_list[j]);
1037 adm_param_enable(port_id, copp_idx,
1038 mod_list[j],
1039 MODULE_DISABLE);
1040 }
1041
1042 /* Enable DAP modules */
1043 pr_debug("%s:DS2 param enable\n", __func__);
1044 adm_param_enable(port_id, copp_idx,
1045 DS2_MODULE_ID, MODULE_ENABLE);
1046 /*
1047 * If custom stereo is on at the time of dap on,
1048 * switch off custom stereo on qti channel mixer
1049 * and turn on custom stereo on DAP.
1050 * mixer(qti).
1051 */
1052 if (cs_onoff) {
1053 rc = qti_set_custom_stereo_on(port_id,
1054 copp_idx,
1055 !cs_onoff);
1056 if (rc < 0) {
1057 pr_info("%s:Q_CS port_id 0x%x",
1058 __func__, port_id);
1059 pr_info("copp_idx %d rc %d\n",
1060 copp_idx, rc);
1061 }
1062 rc = dap_set_custom_stereo_onoff(i,
1063 cs_onoff);
1064 if (rc < 0) {
1065 pr_info("%s:D_CS i %d,rc %d\n",
1066 __func__, i, rc);
1067 }
1068 }
1069 }
1070
1071 rc = msm_ds2_dap_handle_bypass_wait(port_id, copp_idx,
1072 DOLBY_MODULE_ENABLE_PERIOD);
1073 if (rc == -EINTR) {
1074 pr_info("%s:bypass interrupted port_id %d copp_idx %d\n",
1075 __func__, port_id, copp_idx);
1076 /* Interrupted ignore bypass */
1077 rc = 0;
1078 continue;
1079 }
1080
1081 /* set volume to unity gain after module on/off */
1082 rc = adm_set_volume(port_id, copp_idx,
1083 VOLUME_UNITY_GAIN);
1084 if (rc < 0) {
1085 /*
1086 * Not Fatal can continue bypass operations.
1087 * Do not need to block playback
1088 */
1089 pr_info("%s: Set vol port %d copp %d, rc %d\n",
1090 __func__, port_id, copp_idx, rc);
1091 rc = 0;
1092 }
1093 }
1094 }
1095
1096end:
1097 kfree(mod_list);
1098 pr_debug("%s:return rc=%d\n", __func__, rc);
1099 return rc;
1100}
1101
1102static int msm_ds2_dap_send_end_point(int dev_map_idx, int endp_idx)
1103{
1104 int rc = 0;
1105 int32_t *update_params_value = NULL, *params_value = NULL;
1106 uint32_t params_length = (DOLBY_PARAM_INT_ENDP_LENGTH +
1107 DOLBY_PARAM_PAYLOAD_SIZE) * sizeof(uint32_t);
1108 int cache_device = 0;
1109 struct ds2_dap_params_s *ds2_ap_params_obj = NULL;
1110 int32_t *modified_param = NULL;
1111
1112 if (dev_map_idx < 0 || dev_map_idx >= DS2_DEVICES_ALL) {
1113 pr_err("%s: invalid dev map index %d\n", __func__, dev_map_idx);
1114 rc = -EINVAL;
1115 goto end;
1116 }
1117 cache_device = dev_map[dev_map_idx].cache_dev;
1118
1119 ds2_ap_params_obj = &ds2_dap_params[cache_device];
1120 pr_debug("%s: cache dev %d, dev_map_idx %d\n", __func__,
1121 cache_device, dev_map_idx);
1122 pr_debug("%s: endp - %pK %pK\n", __func__,
1123 &ds2_dap_params[cache_device], ds2_ap_params_obj);
1124
1125 params_value = kzalloc(params_length, GFP_KERNEL);
1126 if (!params_value) {
1127 rc = -ENOMEM;
1128 goto end;
1129 }
1130
1131 if (dev_map[dev_map_idx].port_id == DOLBY_INVALID_PORT_ID) {
1132 pr_err("%s: invalid port\n", __func__);
1133 rc = -EINVAL;
1134 goto end;
1135 }
1136
1137 if ((dev_map[dev_map_idx].copp_idx < 0) ||
1138 (dev_map[dev_map_idx].copp_idx >= MAX_COPPS_PER_PORT)) {
1139 pr_err("%s: Invalid copp_idx\n", __func__);
1140 rc = -EINVAL;
1141 goto end;
1142 }
1143
1144 update_params_value = params_value;
1145 *update_params_value++ = DOLBY_BUNDLE_MODULE_ID;
1146 *update_params_value++ = DOLBY_PARAM_ID_INIT_ENDP;
1147 *update_params_value++ = DOLBY_PARAM_INT_ENDP_LENGTH * sizeof(uint32_t);
1148 *update_params_value++ = ds2_ap_params_obj->params_val[
1149 ds2_dap_params_offset[endp_idx]];
1150 pr_debug("%s: off %d, length %d\n", __func__,
1151 ds2_dap_params_offset[endp_idx],
1152 ds2_dap_params_length[endp_idx]);
1153 pr_debug("%s: param 0x%x, param val %d\n", __func__,
1154 ds2_dap_params_id[endp_idx], ds2_ap_params_obj->
1155 params_val[ds2_dap_params_offset[endp_idx]]);
1156 rc = adm_dolby_dap_send_params(dev_map[dev_map_idx].port_id,
1157 dev_map[dev_map_idx].copp_idx,
1158 (char *)params_value, params_length);
1159 if (rc) {
1160 pr_err("%s: send dolby params failed rc %d\n", __func__, rc);
1161 rc = -EINVAL;
1162 }
1163 modified_param = ds2_ap_params_obj->dap_params_modified;
1164 if (modified_param == NULL) {
1165 pr_err("%s: modified param structure invalid\n",
1166 __func__);
1167 rc = -EINVAL;
1168 goto end;
1169 }
1170
1171 if (msm_ds2_dap_check_is_param_modified(modified_param, endp_idx, 0))
1172 ds2_ap_params_obj->dap_params_modified[endp_idx] = 0x00010001;
1173
1174end:
1175 kfree(params_value);
1176 return rc;
1177}
1178
1179static int msm_ds2_dap_send_cached_params(int dev_map_idx,
1180 int commit)
1181{
1182 int32_t *update_params_value = NULL, *params_value = NULL;
1183 uint32_t idx, i, j, ret = 0;
1184 uint32_t params_length = (TOTAL_LENGTH_DOLBY_PARAM +
1185 (MAX_DS2_PARAMS - 1) *
1186 DOLBY_PARAM_PAYLOAD_SIZE) *
1187 sizeof(uint32_t);
1188 int cache_device = 0;
1189 struct ds2_dap_params_s *ds2_ap_params_obj = NULL;
1190 int32_t *modified_param = NULL;
1191
1192 if (dev_map_idx < 0 || dev_map_idx >= DS2_DEVICES_ALL) {
1193 pr_err("%s: invalid dev map index %d\n", __func__, dev_map_idx);
1194 ret = -EINVAL;
1195 goto end;
1196 }
1197 cache_device = dev_map[dev_map_idx].cache_dev;
1198
1199 /* Use off profile cache in only for soft bypass */
1200 if (ds2_dap_params_states.dap_bypass_type == DAP_SOFT_BYPASS &&
1201 ds2_dap_params_states.dap_bypass == true) {
1202 pr_debug("%s: use bypass cache 0\n", __func__);
1203 cache_device = dev_map[0].cache_dev;
1204 }
1205
1206 ds2_ap_params_obj = &ds2_dap_params[cache_device];
1207 pr_debug("%s: cached param - %pK %pK, cache_device %d\n", __func__,
1208 &ds2_dap_params[cache_device], ds2_ap_params_obj,
1209 cache_device);
1210 params_value = kzalloc(params_length, GFP_KERNEL);
1211 if (!params_value) {
1212 pr_err("%s: params memory alloc failed\n", __func__);
1213 ret = -ENOMEM;
1214 goto end;
1215 }
1216
1217 if (dev_map[dev_map_idx].port_id == DOLBY_INVALID_PORT_ID) {
1218 pr_err("%s: invalid port id\n", __func__);
1219 ret = -EINVAL;
1220 goto end;
1221 }
1222
1223 if ((dev_map[dev_map_idx].copp_idx < 0) ||
1224 (dev_map[dev_map_idx].copp_idx >= MAX_COPPS_PER_PORT)) {
1225 pr_err("%s: Invalid copp_idx\n", __func__);
1226 ret = -EINVAL;
1227 goto end;
1228 }
1229
1230 update_params_value = params_value;
1231 params_length = 0;
1232 for (i = 0; i < (MAX_DS2_PARAMS-1); i++) {
1233 /*get the pointer to the param modified array in the cache*/
1234 modified_param = ds2_ap_params_obj->dap_params_modified;
1235 if (modified_param == NULL) {
1236 pr_err("%s: modified param structure invalid\n",
1237 __func__);
1238 ret = -EINVAL;
1239 goto end;
1240 }
1241 if (!msm_ds2_dap_check_is_param_modified(modified_param, i,
1242 commit))
1243 continue;
1244 *update_params_value++ = DOLBY_BUNDLE_MODULE_ID;
1245 *update_params_value++ = ds2_dap_params_id[i];
1246 *update_params_value++ = ds2_dap_params_length[i] *
1247 sizeof(uint32_t);
1248 idx = ds2_dap_params_offset[i];
1249 for (j = 0; j < ds2_dap_params_length[i]; j++) {
1250 *update_params_value++ =
1251 ds2_ap_params_obj->params_val[idx+j];
1252 pr_debug("%s: id 0x%x,val %d\n", __func__,
1253 ds2_dap_params_id[i],
1254 ds2_ap_params_obj->params_val[idx+j]);
1255 }
1256 params_length += (DOLBY_PARAM_PAYLOAD_SIZE +
1257 ds2_dap_params_length[i]) * sizeof(uint32_t);
1258 }
1259
1260 pr_debug("%s: valid param length: %d\n", __func__, params_length);
1261 if (params_length) {
1262 ret = adm_dolby_dap_send_params(dev_map[dev_map_idx].port_id,
1263 dev_map[dev_map_idx].copp_idx,
1264 (char *)params_value,
1265 params_length);
1266 if (ret) {
1267 pr_err("%s: send dolby params failed ret %d\n",
1268 __func__, ret);
1269 ret = -EINVAL;
1270 goto end;
1271 }
1272 for (i = 0; i < MAX_DS2_PARAMS-1; i++) {
1273 /*get pointer to the param modified array in the cache*/
1274 modified_param = ds2_ap_params_obj->dap_params_modified;
1275 if (modified_param == NULL) {
1276 pr_err("%s: modified param struct invalid\n",
1277 __func__);
1278 ret = -EINVAL;
1279 goto end;
1280 }
1281 if (!msm_ds2_dap_check_is_param_modified(
1282 modified_param, i, commit))
1283 continue;
1284 ds2_ap_params_obj->dap_params_modified[i] = 0x00010001;
1285 }
1286 }
1287end:
1288 kfree(params_value);
1289 return ret;
1290}
1291
1292static int msm_ds2_dap_commit_params(struct dolby_param_data *dolby_data,
1293 int commit)
1294{
1295 int ret = 0, i, idx;
1296 struct ds2_dap_params_s *ds2_ap_params_obj = NULL;
1297 int32_t *modified_param = NULL;
1298
1299 /* Do not commit params if in hard bypass */
1300 if (ds2_dap_params_states.dap_bypass_type == DAP_HARD_BYPASS &&
1301 ds2_dap_params_states.dap_bypass == true) {
1302 pr_debug("%s: called in bypass", __func__);
1303 ret = -EINVAL;
1304 goto end;
1305 }
1306 for (idx = 0; idx < MAX_DS2_PARAMS; idx++) {
1307 if (ds2_dap_params_id[idx] == DOLBY_PARAM_ID_INIT_ENDP)
1308 break;
1309 }
1310 if (idx >= MAX_DS2_PARAMS || idx < 0) {
1311 pr_err("%s: index of DS2 Param not found idx %d\n",
1312 __func__, idx);
1313 ret = -EINVAL;
1314 goto end;
1315 }
1316 pr_debug("%s: found endp - idx %d 0x%x\n", __func__, idx,
1317 ds2_dap_params_id[idx]);
1318 for (i = 0; i < DS2_DEVICES_ALL; i++) {
1319 pr_debug("%s:dev[0x%x,0x%x],i:%d,active:%d,bypass:%d,type:%d\n",
1320 __func__, dolby_data->device_id, dev_map[i].device_id,
1321 i, dev_map[i].active, ds2_dap_params_states.dap_bypass,
1322 ds2_dap_params_states.dap_bypass_type);
1323
1324 if (((dev_map[i].device_id & ds2_dap_params_states.device) ||
1325 ((ds2_dap_params_states.dap_bypass_type ==
1326 DAP_SOFT_BYPASS) &&
1327 (ds2_dap_params_states.dap_bypass == true))) &&
1328 (dev_map[i].active == true)) {
1329
1330 /*get ptr to the cache storing the params for device*/
1331 if ((ds2_dap_params_states.dap_bypass_type ==
1332 DAP_SOFT_BYPASS) &&
1333 (ds2_dap_params_states.dap_bypass == true))
1334 ds2_ap_params_obj =
1335 &ds2_dap_params[dev_map[0].cache_dev];
1336 else
1337 ds2_ap_params_obj =
1338 &ds2_dap_params[dev_map[i].cache_dev];
1339
1340 /*get the pointer to the param modified array in cache*/
1341 modified_param = ds2_ap_params_obj->dap_params_modified;
1342 if (modified_param == NULL) {
1343 pr_err("%s: modified_param NULL\n", __func__);
1344 ret = -EINVAL;
1345 goto end;
1346 }
1347
1348 /*
1349 * Send the endp param if use cache is set
1350 * or if param is modified
1351 */
1352 if (!commit || msm_ds2_dap_check_is_param_modified(
1353 modified_param, idx, commit)) {
1354 msm_ds2_dap_send_end_point(i, idx);
1355 commit = 0;
1356 }
1357 ret = msm_ds2_dap_send_cached_params(i, commit);
1358 if (ret < 0) {
1359 pr_err("%s: send cached param %d\n",
1360 __func__, ret);
1361 goto end;
1362 }
1363 }
1364 }
1365end:
1366 return ret;
1367}
1368
1369static int msm_ds2_dap_handle_commands(u32 cmd, void *arg)
1370{
1371 int ret = 0, port_id = 0;
1372 int32_t data;
1373 struct dolby_param_data *dolby_data = (struct dolby_param_data *)arg;
1374
1375 if (get_user(data, &dolby_data->data[0])) {
1376 pr_debug("%s error getting data\n", __func__);
1377 ret = -EFAULT;
1378 goto end;
1379 }
1380
1381 pr_debug("%s: param_id %d,be_id %d,device_id 0x%x,length %d,data %d\n",
1382 __func__, dolby_data->param_id, dolby_data->be_id,
1383 dolby_data->device_id, dolby_data->length, data);
1384
1385 switch (dolby_data->param_id) {
1386 case DAP_CMD_COMMIT_ALL:
1387 msm_ds2_dap_commit_params(dolby_data, 0);
1388 break;
1389
1390 case DAP_CMD_COMMIT_CHANGED:
1391 msm_ds2_dap_commit_params(dolby_data, 1);
1392 break;
1393
1394 case DAP_CMD_USE_CACHE_FOR_INIT:
1395 ds2_dap_params_states.use_cache = data;
1396 break;
1397
1398 case DAP_CMD_SET_BYPASS:
1399 pr_debug("%s: bypass %d bypass type %d, data %d\n", __func__,
1400 ds2_dap_params_states.dap_bypass,
1401 ds2_dap_params_states.dap_bypass_type,
1402 data);
1403 /* Do not perform bypass operation if bypass state is same*/
1404 if (ds2_dap_params_states.dap_bypass == data)
1405 break;
1406 ds2_dap_params_states.dap_bypass = data;
1407 /* hard bypass */
1408 if (ds2_dap_params_states.dap_bypass_type == DAP_HARD_BYPASS)
1409 msm_ds2_dap_handle_bypass(dolby_data);
1410 /* soft bypass */
1411 msm_ds2_dap_commit_params(dolby_data, 0);
1412 break;
1413
1414 case DAP_CMD_SET_BYPASS_TYPE:
1415 if (data == true)
1416 ds2_dap_params_states.dap_bypass_type =
1417 DAP_HARD_BYPASS;
1418 else
1419 ds2_dap_params_states.dap_bypass_type =
1420 DAP_SOFT_BYPASS;
1421 pr_debug("%s: bypass type %d", __func__,
1422 ds2_dap_params_states.dap_bypass_type);
1423 break;
1424
1425 case DAP_CMD_SET_ACTIVE_DEVICE:
1426 pr_debug("%s: DAP_CMD_SET_ACTIVE_DEVICE length %d\n",
1427 __func__, dolby_data->length);
1428 /* TODO: need to handle multiple instance*/
1429 ds2_dap_params_states.device |= dolby_data->device_id;
1430 port_id = msm_ds2_dap_get_port_id(
1431 dolby_data->device_id,
1432 dolby_data->be_id);
1433 pr_debug("%s: device id 0x%x all_dev 0x%x port_id %d\n",
1434 __func__, dolby_data->device_id,
1435 ds2_dap_params_states.device, port_id);
1436 msm_ds2_dap_update_dev_map_port_id(dolby_data->device_id,
1437 port_id);
1438 if (port_id == DOLBY_INVALID_PORT_ID) {
1439 pr_err("%s: invalid port id %d\n", __func__, port_id);
1440 ret = -EINVAL;
1441 goto end;
1442 }
1443 break;
1444 }
1445end:
1446 return ret;
1447
1448}
1449
1450static int msm_ds2_dap_set_param(u32 cmd, void *arg)
1451{
1452 int rc = 0, idx, i, j, off, port_id = 0, cdev = 0;
1453 int32_t num_device = 0;
1454 int32_t data = 0;
1455 int32_t dev_arr[DS2_DSP_SUPPORTED_ENDP_DEVICE] = {0};
1456 struct dolby_param_data *dolby_data = (struct dolby_param_data *)arg;
1457
1458 rc = msm_ds2_dap_update_num_devices(dolby_data, &num_device, dev_arr,
1459 DS2_DSP_SUPPORTED_ENDP_DEVICE);
1460 if (num_device == 0 || rc < 0) {
1461 pr_err("%s: num devices 0\n", __func__);
1462 rc = -EINVAL;
1463 goto end;
1464 }
1465 for (i = 0; i < num_device; i++) {
1466 port_id = msm_ds2_dap_get_port_id(dev_arr[i],
1467 dolby_data->be_id);
1468 if (port_id != DOLBY_INVALID_PORT_ID)
1469 msm_ds2_dap_update_dev_map_port_id(dev_arr[i], port_id);
1470
1471 cdev = msm_ds2_dap_map_device_to_dolby_cache_devices(
1472 dev_arr[i]);
1473 if (cdev < 0 || cdev >= DOLBY_MAX_CACHE) {
1474 pr_err("%s: Invalid cache device %d for device 0x%x\n",
1475 __func__, cdev, dev_arr[i]);
1476 rc = -EINVAL;
1477 goto end;
1478 }
1479 pr_debug("%s:port:%d,be:%d,dev:0x%x,cdev:%d,param:0x%x,len:%d\n"
1480 , __func__, port_id, dolby_data->be_id, dev_arr[i],
1481 cdev, dolby_data->param_id, dolby_data->length);
1482 for (idx = 0; idx < MAX_DS2_PARAMS; idx++) {
1483 /*paramid from user space*/
1484 if (dolby_data->param_id == ds2_dap_params_id[idx])
1485 break;
1486 }
1487 if (idx > MAX_DS2_PARAMS-1) {
1488 pr_err("%s: invalid param id 0x%x at idx %d\n",
1489 __func__, dolby_data->param_id, idx);
1490 rc = -EINVAL;
1491 goto end;
1492 }
1493
1494 off = ds2_dap_params_offset[idx];
1495 if ((dolby_data->length <= 0) ||
1496 (dolby_data->length > TOTAL_LENGTH_DS2_PARAM - off)) {
1497 pr_err("%s: invalid length %d at idx %d\n",
1498 __func__, dolby_data->length, idx);
1499 rc = -EINVAL;
1500 goto end;
1501 }
1502
1503 /* cache the parameters */
1504 ds2_dap_params[cdev].dap_params_modified[idx] += 1;
1505 for (j = 0; j < dolby_data->length; j++) {
1506 if (get_user(data, &dolby_data->data[j])) {
1507 pr_debug("%s:error getting data\n", __func__);
1508 rc = -EFAULT;
1509 goto end;
1510 }
1511 ds2_dap_params[cdev].params_val[off + j] = data;
1512 pr_debug("%s:off %d,val[i/p:o/p]-[%d / %d]\n",
1513 __func__, off, data,
1514 ds2_dap_params[cdev].
1515 params_val[off + j]);
1516 }
1517 }
1518end:
1519 return rc;
1520}
1521
1522static int msm_ds2_dap_get_param(u32 cmd, void *arg)
1523{
1524 int rc = 0, i, port_id = 0, copp_idx = -1;
1525 struct dolby_param_data *dolby_data = (struct dolby_param_data *)arg;
1526 int32_t *update_params_value = NULL, *params_value = NULL;
1527 uint32_t params_length = DOLBY_MAX_LENGTH_INDIVIDUAL_PARAM *
1528 sizeof(uint32_t);
1529 uint32_t param_payload_len =
1530 DOLBY_PARAM_PAYLOAD_SIZE * sizeof(uint32_t);
1531
1532 /* Return error on get param in soft or hard bypass */
1533 if (ds2_dap_params_states.dap_bypass == true) {
1534 pr_err("%s: called in bypass_type %d bypass %d\n", __func__,
1535 ds2_dap_params_states.dap_bypass_type,
1536 ds2_dap_params_states.dap_bypass);
1537 rc = -EINVAL;
1538 goto end;
1539 }
1540
1541 /* Return if invalid length */
1542 if ((dolby_data->length >
1543 (DOLBY_MAX_LENGTH_INDIVIDUAL_PARAM - DOLBY_PARAM_PAYLOAD_SIZE)) ||
1544 (dolby_data->length <= 0)) {
1545 pr_err("Invalid length %d", dolby_data->length);
1546 rc = -EINVAL;
1547 goto end;
1548 }
1549
1550 for (i = 0; i < DS2_DEVICES_ALL; i++) {
1551 if ((dev_map[i].active) &&
1552 (dev_map[i].device_id & dolby_data->device_id)) {
1553 port_id = dev_map[i].port_id;
1554 copp_idx = dev_map[i].copp_idx;
1555 break;
1556 }
1557 }
1558
1559 if (port_id == DOLBY_INVALID_PORT_ID) {
1560 pr_err("%s: Invalid port\n", __func__);
1561 rc = -EINVAL;
1562 goto end;
1563 }
1564
1565 if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
1566 pr_err("%s: Invalid copp_idx\n", __func__);
1567 rc = -EINVAL;
1568 goto end;
1569 }
1570
1571 pr_debug("%s: port_id 0x%x, copp_idx %d, dev_map[i].device_id %x\n",
1572 __func__, port_id, copp_idx, dev_map[i].device_id);
1573
1574 params_value = kzalloc(params_length + param_payload_len,
1575 GFP_KERNEL);
1576 if (!params_value) {
1577 rc = -ENOMEM;
1578 goto end;
1579 }
1580
1581 if (dolby_data->param_id == DOLBY_PARAM_ID_VER) {
1582 rc = adm_get_params(port_id, copp_idx,
1583 DOLBY_BUNDLE_MODULE_ID,
1584 DOLBY_PARAM_ID_VER,
1585 params_length + param_payload_len,
1586 (char *)params_value);
1587 } else {
1588 for (i = 0; i < MAX_DS2_PARAMS; i++)
1589 if (ds2_dap_params_id[i] ==
1590 dolby_data->param_id)
1591 break;
1592 if (i > MAX_DS2_PARAMS-1) {
1593 pr_err("%s: invalid param id 0x%x at id %d\n", __func__,
1594 dolby_data->param_id, i);
1595 rc = -EINVAL;
1596 goto end;
1597 } else {
1598 params_length =
1599 ds2_dap_params_length[i] * sizeof(uint32_t);
1600
1601 rc = adm_get_params(port_id, copp_idx,
1602 DOLBY_BUNDLE_MODULE_ID,
1603 ds2_dap_params_id[i],
1604 params_length +
1605 param_payload_len,
1606 (char *)params_value);
1607 }
1608 }
1609 if (rc) {
1610 pr_err("%s: get parameters failed rc %d\n", __func__, rc);
1611 rc = -EINVAL;
1612 goto end;
1613 }
1614 update_params_value = params_value;
1615 if (copy_to_user((void *)dolby_data->data,
1616 &update_params_value[DOLBY_PARAM_PAYLOAD_SIZE],
1617 (dolby_data->length * sizeof(uint32_t)))) {
1618 pr_err("%s: error getting param\n", __func__);
1619 rc = -EFAULT;
1620 goto end;
1621 }
1622end:
1623 kfree(params_value);
1624 return rc;
1625}
1626
1627static int msm_ds2_dap_param_visualizer_control_get(u32 cmd, void *arg)
1628{
1629 int32_t *visualizer_data = NULL;
1630 int i = 0, ret = 0, port_id = -1, cache_dev = -1, copp_idx = -1;
1631 int32_t *update_visualizer_data = NULL;
1632 struct dolby_param_data *dolby_data = (struct dolby_param_data *)arg;
1633 uint32_t offset, length, params_length;
1634 uint32_t param_payload_len =
1635 DOLBY_PARAM_PAYLOAD_SIZE * sizeof(uint32_t);
1636
1637 for (i = 0; i < DS2_DEVICES_ALL; i++) {
1638 if ((dev_map[i].active)) {
1639 port_id = dev_map[i].port_id;
1640 cache_dev = dev_map[i].cache_dev;
1641 copp_idx = dev_map[i].copp_idx;
1642 break;
1643 }
1644 }
1645
1646 if (port_id == DOLBY_INVALID_PORT_ID ||
1647 (copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
1648 ret = 0;
1649 dolby_data->length = 0;
1650 pr_err("%s: no device active\n", __func__);
1651 goto end;
1652 }
1653
1654 length = ds2_dap_params[cache_dev].params_val[DOLBY_PARAM_VCNB_OFFSET];
1655
1656 if (length > DOLBY_PARAM_VCNB_MAX_LENGTH || length <= 0) {
1657 ret = 0;
1658 dolby_data->length = 0;
1659 pr_err("%s Incorrect VCNB length", __func__);
1660 return -EINVAL;
1661 }
1662
1663 params_length = (2*length + DOLBY_VIS_PARAM_HEADER_SIZE) *
1664 sizeof(uint32_t);
1665
1666 visualizer_data = kzalloc(params_length, GFP_KERNEL);
1667 if (!visualizer_data) {
1668 ret = -ENOMEM;
1669 dolby_data->length = 0;
1670 goto end;
1671 }
1672 memset(visualizer_data, 0x0, params_length);
1673
1674 /* Return error on get param in soft or hard bypass */
1675 if (ds2_dap_params_states.dap_bypass == true) {
1676 pr_debug("%s: visualizer called in bypass, return 0\n",
1677 __func__);
1678 ret = 0;
1679 dolby_data->length = 0;
1680 goto end;
1681 }
1682
1683 offset = 0;
1684 params_length = length * sizeof(uint32_t);
1685 ret = adm_get_params(port_id, copp_idx,
1686 DOLBY_BUNDLE_MODULE_ID,
1687 DOLBY_PARAM_ID_VCBG,
1688 params_length + param_payload_len,
1689 (((char *)(visualizer_data)) + offset));
1690 if (ret) {
1691 pr_err("%s: get parameters failed ret %d\n", __func__, ret);
1692 ret = -EINVAL;
1693 dolby_data->length = 0;
1694 goto end;
1695 }
1696 offset = length * sizeof(uint32_t);
1697 ret = adm_get_params(port_id, copp_idx,
1698 DOLBY_BUNDLE_MODULE_ID,
1699 DOLBY_PARAM_ID_VCBE,
1700 params_length + param_payload_len,
1701 (((char *)(visualizer_data)) + offset));
1702 if (ret) {
1703 pr_err("%s: get parameters failed ret %d\n", __func__, ret);
1704 ret = -EINVAL;
1705 dolby_data->length = 0;
1706 goto end;
1707 }
1708 update_visualizer_data = visualizer_data;
1709 dolby_data->length = 2 * length;
1710
1711 if (copy_to_user((void *)dolby_data->data,
1712 (void *)update_visualizer_data,
1713 (dolby_data->length * sizeof(uint32_t)))) {
1714 pr_err("%s: copy to user failed for data\n", __func__);
1715 dolby_data->length = 0;
1716 ret = -EFAULT;
1717 goto end;
1718 }
1719
1720end:
1721 kfree(visualizer_data);
1722 return ret;
1723}
1724
1725int msm_ds2_dap_set_security_control(u32 cmd, void *arg)
1726{
1727 struct dolby_param_license *dolby_license =
1728 ((struct dolby_param_license *)arg);
1729 pr_debug("%s: dmid %d license key %d\n", __func__,
1730 dolby_license->dmid, dolby_license->license_key);
1731 core_set_dolby_manufacturer_id(dolby_license->dmid);
1732 core_set_license(dolby_license->license_key, DOLBY_DS1_LICENSE_ID);
1733 return 0;
1734}
1735
1736int msm_ds2_dap_update_port_parameters(struct snd_hwdep *hw, struct file *file,
1737 bool open)
1738{
1739 int i = 0, dev_id = 0;
1740
1741 pr_debug("%s: open %d\n", __func__, open);
1742 ds2_dap_params_states.node_opened = open;
1743 ds2_dap_params_states.dap_bypass = true;
1744 ds2_dap_params_states.dap_bypass_type = 0;
1745 ds2_dap_params_states.use_cache = 0;
1746 ds2_dap_params_states.device = 0;
1747 ds2_dap_params_states.custom_stereo_onoff = 0;
1748 for (i = 0; i < DS2_DEVICES_ALL; i++) {
1749 if (i == 0)
1750 dev_map[i].device_id = 0;
1751 else {
1752 dev_id = (1 << (i-1));
1753 if (all_supported_devices & dev_id)
1754 dev_map[i].device_id = dev_id;
1755 else
1756 continue;
1757 }
1758 dev_map[i].cache_dev =
1759 msm_ds2_dap_map_device_to_dolby_cache_devices(
1760 dev_map[i].device_id);
1761 if (dev_map[i].cache_dev < 0 ||
1762 dev_map[i].cache_dev >= DOLBY_MAX_CACHE)
1763 pr_err("%s: Invalid cache device %d for device 0x%x\n",
1764 __func__,
1765 dev_map[i].cache_dev,
1766 dev_map[i].device_id);
1767 dev_map[i].port_id = -1;
1768 dev_map[i].active = false;
1769 dev_map[i].stream_ref_count = 0;
1770 dev_map[i].cal_data = NULL;
1771 dev_map[i].copp_idx = -1;
1772 pr_debug("%s: device_id 0x%x, cache_dev %d act %d\n", __func__,
1773 dev_map[i].device_id, dev_map[i].cache_dev,
1774 dev_map[i].active);
1775 }
1776 return 0;
1777
1778}
1779
1780int msm_ds2_dap_ioctl_shared(struct snd_hwdep *hw, struct file *file,
1781 u32 cmd, void *arg)
1782{
1783 int ret = 0;
1784
1785 pr_debug("%s: cmd: 0x%x\n", __func__, cmd);
1786 switch (cmd) {
1787 case SNDRV_DEVDEP_DAP_IOCTL_SET_PARAM:
1788 ret = msm_ds2_dap_set_param(cmd, arg);
1789 break;
1790 case SNDRV_DEVDEP_DAP_IOCTL_GET_PARAM:
1791 ret = msm_ds2_dap_get_param(cmd, arg);
1792 break;
1793 case SNDRV_DEVDEP_DAP_IOCTL_DAP_COMMAND:
1794 ret = msm_ds2_dap_handle_commands(cmd, arg);
1795 break;
1796 case SNDRV_DEVDEP_DAP_IOCTL_DAP_LICENSE:
1797 ret = msm_ds2_dap_set_security_control(cmd, arg);
1798 break;
1799 case SNDRV_DEVDEP_DAP_IOCTL_GET_VISUALIZER:
1800 ret = msm_ds2_dap_param_visualizer_control_get(cmd, arg);
1801 break;
1802 default:
1803 pr_err("%s: called with invalid control 0x%x\n", __func__, cmd);
1804 ret = -EINVAL;
1805 }
1806 return ret;
1807}
1808
1809int msm_ds2_dap_ioctl(struct snd_hwdep *hw, struct file *file,
1810 u32 cmd, void *arg)
1811{
1812
1813 int ret = 0;
1814
1815 pr_debug("%s: cmd: 0x%x\n", __func__, cmd);
1816 if (!arg) {
1817 pr_err("%s: Invalid params event status\n", __func__);
1818 ret = -EINVAL;
1819 goto end;
1820 }
1821 switch (cmd) {
1822 case SNDRV_DEVDEP_DAP_IOCTL_SET_PARAM:
1823 case SNDRV_DEVDEP_DAP_IOCTL_DAP_COMMAND: {
1824 struct dolby_param_data dolby_data;
1825
1826 if (copy_from_user((void *)&dolby_data, (void *)arg,
1827 sizeof(struct dolby_param_data))) {
1828 pr_err("%s: Copy from user failed\n", __func__);
1829 ret = -EFAULT;
1830 goto end;
1831 }
1832 ret = msm_ds2_dap_ioctl_shared(hw, file, cmd, &dolby_data);
1833 break;
1834 }
1835 case SNDRV_DEVDEP_DAP_IOCTL_DAP_LICENSE: {
1836 struct dolby_param_license dolby_license;
1837
1838 if (copy_from_user((void *)&dolby_license, (void *)arg,
1839 sizeof(struct dolby_param_license))) {
1840 pr_err("%s: Copy from user failed\n", __func__);
1841 ret = -EFAULT;
1842 goto end;
1843 }
1844 ret = msm_ds2_dap_ioctl_shared(hw, file, cmd, &dolby_license);
1845 break;
1846 }
1847 case SNDRV_DEVDEP_DAP_IOCTL_GET_PARAM:
1848 case SNDRV_DEVDEP_DAP_IOCTL_GET_VISUALIZER: {
1849 struct dolby_param_data dolby_data;
1850
1851 if (copy_from_user((void *)&dolby_data, (void *)arg,
1852 sizeof(struct dolby_param_data))) {
1853 pr_err("%s: Copy from user failed\n", __func__);
1854 ret = -EFAULT;
1855 goto end;
1856 }
1857 ret = msm_ds2_dap_ioctl_shared(hw, file, cmd, &dolby_data);
1858 if (ret < 0)
1859 pr_err("%s: ioctl cmd %d returned err %d\n",
1860 __func__, cmd, ret);
1861 if (copy_to_user((void *)arg, &dolby_data,
1862 sizeof(struct dolby_param_data))) {
1863 pr_err("%s: Copy to user failed\n", __func__);
1864 ret = -EFAULT;
1865 goto end;
1866 }
1867 break;
1868 }
1869 default:
1870 pr_err("%s: called with invalid control 0x%x\n", __func__, cmd);
1871 ret = -EINVAL;
1872 }
1873end:
1874 return ret;
1875
1876}
1877#ifdef CONFIG_COMPAT
1878int msm_ds2_dap_compat_ioctl(struct snd_hwdep *hw, struct file *file,
1879 u32 cmd, void *arg)
1880{
1881 int ret = 0;
1882
1883 pr_debug("%s: cmd: 0x%x\n", __func__, cmd);
1884 switch (cmd) {
1885 case SNDRV_DEVDEP_DAP_IOCTL_SET_PARAM32:
1886 cmd = SNDRV_DEVDEP_DAP_IOCTL_SET_PARAM;
1887 goto handle_set_ioctl;
1888 case SNDRV_DEVDEP_DAP_IOCTL_DAP_COMMAND32:
1889 cmd = SNDRV_DEVDEP_DAP_IOCTL_DAP_COMMAND;
1890handle_set_ioctl:
1891 {
1892 struct dolby_param_data32 dolby_data32;
1893 struct dolby_param_data dolby_data;
1894
1895 memset(&dolby_data32, 0, sizeof(dolby_data32));
1896 memset(&dolby_data, 0, sizeof(dolby_data));
1897 if (copy_from_user(&dolby_data32, (void *)arg,
1898 sizeof(struct dolby_param_data32))) {
1899 pr_err("%s: Copy from user failed\n", __func__);
1900 ret = -EFAULT;
1901 goto end;
1902 }
1903 dolby_data.version = dolby_data32.version;
1904 dolby_data.device_id = dolby_data32.device_id;
1905 dolby_data.be_id = dolby_data32.be_id;
1906 dolby_data.param_id = dolby_data32.param_id;
1907 dolby_data.length = dolby_data32.length;
1908 dolby_data.data = compat_ptr(dolby_data32.data);
1909
1910 ret = msm_ds2_dap_ioctl_shared(hw, file, cmd, &dolby_data);
1911 break;
1912 }
1913 case SNDRV_DEVDEP_DAP_IOCTL_GET_PARAM32:
1914 cmd = SNDRV_DEVDEP_DAP_IOCTL_GET_PARAM;
1915 goto handle_get_ioctl;
1916 case SNDRV_DEVDEP_DAP_IOCTL_GET_VISUALIZER32:
1917 cmd = SNDRV_DEVDEP_DAP_IOCTL_GET_VISUALIZER;
1918handle_get_ioctl:
1919 {
1920 struct dolby_param_data32 dolby_data32;
1921 struct dolby_param_data dolby_data;
1922
1923 memset(&dolby_data32, 0, sizeof(dolby_data32));
1924 memset(&dolby_data, 0, sizeof(dolby_data));
1925 if (copy_from_user(&dolby_data32, (void *)arg,
1926 sizeof(struct dolby_param_data32))) {
1927 pr_err("%s: Copy from user failed\n", __func__);
1928 ret = -EFAULT;
1929 goto end;
1930 }
1931 dolby_data.version = dolby_data32.version;
1932 dolby_data.device_id = dolby_data32.device_id;
1933 dolby_data.be_id = dolby_data32.be_id;
1934 dolby_data.param_id = dolby_data32.param_id;
1935 dolby_data.length = dolby_data32.length;
1936 dolby_data.data = compat_ptr(dolby_data32.data);
1937
1938 ret = msm_ds2_dap_ioctl_shared(hw, file, cmd, &dolby_data);
1939 if (ret < 0)
1940 pr_err("%s: ioctl cmd %d, returned err %d\n",
1941 __func__, cmd, ret);
1942 dolby_data32.length = dolby_data.length;
1943 if (copy_to_user((void *)arg, &dolby_data32,
1944 sizeof(struct dolby_param_data32))) {
1945 pr_err("%s: Copy to user failed\n", __func__);
1946 ret = -EFAULT;
1947 goto end;
1948 }
1949 break;
1950 }
1951 case SNDRV_DEVDEP_DAP_IOCTL_DAP_LICENSE32: {
1952 struct dolby_param_license32 dolby_license32;
1953 struct dolby_param_license dolby_license;
1954
1955 cmd = SNDRV_DEVDEP_DAP_IOCTL_DAP_LICENSE;
1956 if (copy_from_user((void *)&dolby_license32, (void *)arg,
1957 sizeof(struct dolby_param_license32))) {
1958 pr_err("%s: Copy from user failed\n", __func__);
1959 ret = -EFAULT;
1960 goto end;
1961 }
1962 dolby_license.dmid = dolby_license32.dmid;
1963 dolby_license.license_key = dolby_license32.license_key;
1964 ret = msm_ds2_dap_ioctl_shared(hw, file, cmd, &dolby_license);
1965 break;
1966 }
1967 default:
1968 pr_err("%s: called with invalid control 0x%x\n",
1969 __func__, cmd);
1970 ret = -EINVAL;
1971 }
1972end:
1973 return ret;
1974
1975}
1976#endif
1977
1978int msm_ds2_dap_init(int port_id, int copp_idx, int channels,
1979 bool is_custom_stereo_on)
1980{
1981 int ret = 0, idx = -1, i;
1982 struct dolby_param_data dolby_data;
1983
1984 struct audproc_softvolume_params softvol = {
1985 .period = DOLBY_SOFT_VOLUME_PERIOD,
1986 .step = DOLBY_SOFT_VOLUME_STEP,
1987 .rampingcurve = DOLBY_SOFT_VOLUME_CURVE_EXP,
1988 };
1989
1990 pr_debug("%s: port id %d, copp_idx %d\n", __func__, port_id, copp_idx);
1991
1992 if (port_id != DOLBY_INVALID_PORT_ID) {
1993 for (i = 0; i < DS2_DEVICES_ALL; i++) {
1994 if ((dev_map[i].port_id == port_id) &&
1995 /* device part of active device */
1996 (dev_map[i].device_id &
1997 ds2_dap_params_states.device)) {
1998 idx = i;
1999 /* Give priority to headset in case of
2000 * combo device
2001 */
2002 if (dev_map[i].device_id == SPEAKER)
2003 continue;
2004 else
2005 break;
2006 }
2007 }
2008 if (idx < 0) {
2009 pr_err("%s: invalid index for port %d\n",
2010 __func__, port_id);
2011 ret = -EINVAL;
2012 goto end;
2013 }
2014 pr_debug("%s:index %d, dev[0x%x,0x%x]\n", __func__, idx,
2015 dev_map[idx].device_id, ds2_dap_params_states.device);
2016 dev_map[idx].active = true;
2017 dev_map[idx].copp_idx = copp_idx;
2018 dolby_data.param_id = DOLBY_COMMIT_ALL_TO_DSP;
2019 dolby_data.length = 0;
2020 dolby_data.data = NULL;
2021 dolby_data.device_id = dev_map[idx].device_id;
2022 pr_debug("%s: idx %d, active %d, dev id 0x%x, ref count %d\n",
2023 __func__, idx, dev_map[idx].active,
2024 dev_map[idx].device_id,
2025 dev_map[idx].stream_ref_count);
2026 if (dev_map[idx].stream_ref_count == 0) {
2027 /*perform next 3 func only if hard bypass enabled*/
2028 if (ds2_dap_params_states.dap_bypass_type ==
2029 DAP_HARD_BYPASS) {
2030 ret = msm_ds2_dap_alloc_and_store_cal_data(idx,
2031 ADM_PATH_PLAYBACK, 0);
2032 if (ret < 0) {
2033 pr_err("%s: Failed to alloc and store cal data for idx %d, device %d, copp_idx %d",
2034 __func__,
2035 idx, dev_map[idx].device_id,
2036 dev_map[idx].copp_idx);
2037 dev_map[idx].active = false;
2038 dev_map[idx].copp_idx = -1;
2039 goto end;
2040 }
2041
2042 ret = adm_set_softvolume(port_id, copp_idx,
2043 &softvol);
2044 if (ret < 0) {
2045 pr_err("%s: Soft volume ret error %d\n",
2046 __func__, ret);
2047 dev_map[idx].active = false;
2048 dev_map[idx].copp_idx = -1;
2049 goto end;
2050 }
2051
2052 ret = msm_ds2_dap_init_modules_in_topology(
2053 idx);
2054 if (ret < 0) {
2055 pr_err("%s: Failed to init modules in topolofy for idx %d, device %d, copp_idx %d\n",
2056 __func__, idx,
2057 dev_map[idx].device_id,
2058 dev_map[idx].copp_idx);
2059 dev_map[idx].active = false;
2060 dev_map[idx].copp_idx = -1;
2061 goto end;
2062 }
2063 }
2064
2065 ret = msm_ds2_dap_commit_params(&dolby_data, 0);
2066 if (ret < 0) {
2067 pr_debug("%s: commit params ret %d\n",
2068 __func__, ret);
2069 ret = 0;
2070 }
2071 }
2072 dev_map[idx].stream_ref_count++;
2073 if (is_custom_stereo_on) {
2074 ds2_dap_params_states.custom_stereo_onoff =
2075 is_custom_stereo_on;
2076 set_custom_stereo_onoff(idx,
2077 is_custom_stereo_on);
2078 }
2079 }
2080
2081end:
2082 return ret;
2083}
2084
2085void msm_ds2_dap_deinit(int port_id)
2086{
2087 /*
2088 * Get the active port corrresponding to the active device
2089 * Check if this is same as incoming port
2090 * Set it to invalid
2091 */
2092 int idx = -1, i;
2093
2094 pr_debug("%s: port_id %d\n", __func__, port_id);
2095 if (port_id != DOLBY_INVALID_PORT_ID) {
2096 for (i = 0; i < DS2_DEVICES_ALL; i++) {
2097 /* Active port */
2098 if ((dev_map[i].port_id == port_id) &&
2099 /* device part of active device */
2100 (dev_map[i].device_id &
2101 ds2_dap_params_states.device) &&
2102 /*
2103 * Need this check to avoid race condition of
2104 * active device being set and playback
2105 * instance opened
2106 */
2107 /* active device*/
2108 dev_map[i].active) {
2109 idx = i;
2110 if (dev_map[i].device_id == SPEAKER)
2111 continue;
2112 else
2113 break;
2114 }
2115 }
2116 if (idx < 0) {
2117 pr_err("%s: invalid index for port %d\n",
2118 __func__, port_id);
2119 return;
2120 }
2121 pr_debug("%s:index %d, dev [0x%x, 0x%x]\n", __func__, idx,
2122 dev_map[idx].device_id, ds2_dap_params_states.device);
2123 dev_map[idx].stream_ref_count--;
2124 if (dev_map[idx].stream_ref_count == 0) {
2125 /*perform next func only if hard bypass enabled*/
2126 if (ds2_dap_params_states.dap_bypass_type ==
2127 DAP_HARD_BYPASS) {
2128 msm_ds2_dap_free_cal_data(idx);
2129 }
2130 ds2_dap_params_states.device &= ~dev_map[idx].device_id;
2131 dev_map[idx].active = false;
2132 dev_map[idx].copp_idx = -1;
2133 }
2134 pr_debug("%s:idx %d, active %d, dev id 0x%x ref count %d\n",
2135 __func__, idx, dev_map[idx].active,
2136 dev_map[idx].device_id, dev_map[idx].stream_ref_count);
2137 }
2138}
2139
2140int msm_ds2_dap_set_custom_stereo_onoff(int port_id, int copp_idx,
2141 bool is_custom_stereo_enabled)
2142{
2143 int idx = -1, rc = 0, i;
2144
2145 pr_debug("%s: port_id %d\n", __func__, port_id);
2146 if (port_id != DOLBY_INVALID_PORT_ID) {
2147 for (i = 0; i < DS2_DEVICES_ALL; i++) {
2148 if ((dev_map[i].port_id == port_id) &&
2149 /* device part of active device */
2150 (dev_map[i].device_id &
2151 ds2_dap_params_states.device)) {
2152 idx = i;
2153 if (dev_map[i].device_id == SPEAKER)
2154 continue;
2155 else
2156 break;
2157 }
2158 }
2159 if (idx < 0) {
2160 pr_err("%s: invalid index for port %d\n",
2161 __func__, port_id);
2162 return rc;
2163 }
2164 ds2_dap_params_states.custom_stereo_onoff =
2165 is_custom_stereo_enabled;
2166 rc = set_custom_stereo_onoff(idx,
2167 is_custom_stereo_enabled);
2168 if (rc < 0) {
2169 pr_err("%s: Custom stereo err %d on port %d\n",
2170 __func__, rc, port_id);
2171 }
2172 }
2173 return rc;
2174}