• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "msm8916_platform"
18 /*#define LOG_NDEBUG 0*/
19 #define LOG_NDDEBUG 0
20 
21 #include <stdlib.h>
22 #include <dlfcn.h>
23 #include <fcntl.h>
24 #include <sys/ioctl.h>
25 #include <log/log.h>
26 #include <cutils/properties.h>
27 #include <cutils/str_parms.h>
28 #include <audio_hw.h>
29 #include <platform_api.h>
30 #include "platform.h"
31 #include "audio_extn.h"
32 #include "acdb.h"
33 #include "voice_extn.h"
34 #include "sound/msmcal-hwdep.h"
35 #include "audio_extn/tfa_98xx.h"
36 #include <dirent.h>
37 #define MAX_MIXER_XML_PATH  100
38 #define MIXER_XML_PATH "mixer_paths.xml"
39 #define MIXER_XML_PATH_MTP "mixer_paths_mtp.xml"
40 #define MIXER_XML_PATH_MSM8909_PM8916 "mixer_paths_msm8909_pm8916.xml"
41 #define MIXER_XML_PATH_BG "mixer_paths_bg.xml"
42 #define MIXER_XML_PATH_L9300 "mixer_paths_l9300.xml"
43 
44 #define LIB_ACDB_LOADER "libacdbloader.so"
45 #define AUDIO_DATA_BLOCK_MIXER_CTL "HDMI EDID"
46 #define CVD_VERSION_MIXER_CTL "CVD Version"
47 
48 /*
49  * This file will have a maximum of 38 bytes:
50  *
51  * 4 bytes: number of audio blocks
52  * 4 bytes: total length of Short Audio Descriptor (SAD) blocks
53  * Maximum 10 * 3 bytes: SAD blocks
54  */
55 #define MAX_SAD_BLOCKS      10
56 #define SAD_BLOCK_SIZE      3
57 #define MAX_CVD_VERSION_STRING_SIZE    100
58 
59 /* EDID format ID for LPCM audio */
60 #define EDID_FORMAT_LPCM    1
61 
62 /* fallback app type if the default app type from acdb loader fails */
63 #define DEFAULT_APP_TYPE_RX_PATH  0x11130
64 #define DEFAULT_APP_TYPE_TX_PATH  0x11132
65 #define DEFAULT_RX_BACKEND "SLIMBUS_0_RX"
66 
67 /* Retry for delay in FW loading*/
68 #define RETRY_NUMBER 20
69 #define RETRY_US 500000
70 #define MAX_SND_CARD 8
71 
72 #define SAMPLE_RATE_8KHZ  8000
73 #define SAMPLE_RATE_16KHZ 16000
74 
75 #define MAX_SET_CAL_BYTE_SIZE 65536
76 
77 #define MAX_CAL_NAME 20
78 
79 #define QMIC_FLAG 0x00000004
80 
81 #define TOSTRING_(x) #x
82 #define TOSTRING(x) TOSTRING_(x)
83 
84 #define GET_IN_DEVICE_INDEX(SND_DEVICE) ((SND_DEVICE) - (SND_DEVICE_IN_BEGIN))
85 
86 char cal_name_info[WCD9XXX_MAX_CAL][MAX_CAL_NAME] = {
87         [WCD9XXX_MBHC_CAL] = "mbhc_cal",
88 };
89 
90 struct audio_block_header
91 {
92     int reserved;
93     int length;
94 };
95 
96 typedef struct acdb_audio_cal_cfg {
97     uint32_t             persist;
98     uint32_t             snd_dev_id;
99     audio_devices_t      dev_id;
100     int32_t              acdb_dev_id;
101     uint32_t             app_type;
102     uint32_t             topo_id;
103     uint32_t             sampling_rate;
104     uint32_t             cal_type;
105     uint32_t             module_id;
106     uint32_t             param_id;
107 } acdb_audio_cal_cfg_t;
108 
109 enum {
110     CAL_MODE_SEND           = 0x1,
111     CAL_MODE_PERSIST        = 0x2,
112     CAL_MODE_RTAC           = 0x4
113 };
114 
115 enum {
116     BUFF_IDX_0 = 0,
117     BUFF_IDX_1 = 1,
118 };
119 
120 #define PLATFORM_CONFIG_KEY_OPERATOR_INFO "operator_info"
121 
122 struct operator_info {
123     struct listnode list;
124     char *name;
125     char *mccmnc;
126 };
127 
128 struct operator_specific_device {
129     struct listnode list;
130     char *operator;
131     char *mixer_path;
132     int acdb_id;
133 };
134 
135 static struct listnode operator_info_list;
136 static struct listnode *operator_specific_device_table[SND_DEVICE_MAX];
137 
138 /* Audio calibration related functions */
139 typedef void (*acdb_send_audio_cal_v3_t)(int, int, int , int, int);
140 typedef int (*acdb_loader_get_calibration_t)(char *attr, int size, void *data);
141 acdb_loader_get_calibration_t acdb_loader_get_calibration;
142 static int platform_get_meta_info_key_from_list(void *platform, char *mod_name);
143 
144 struct platform_data {
145     struct audio_device *adev;
146     bool fluence_in_spkr_mode;
147     bool fluence_in_voice_call;
148     bool fluence_in_voice_rec;
149     int  fluence_type;
150     char fluence_cap[PROPERTY_VALUE_MAX];
151     int  fluence_mode;
152     bool ec_ref_enabled;
153     bool gsm_mode_enabled;
154     /* Audio calibration related functions */
155     void                       *acdb_handle;
156     acdb_init_v3_t             acdb_init_v3;
157     acdb_init_v2_cvd_t         acdb_init;
158     acdb_deallocate_t          acdb_deallocate;
159     acdb_send_audio_cal_t      acdb_send_audio_cal;
160     acdb_send_audio_cal_v3_t   acdb_send_audio_cal_v3;
161     acdb_get_audio_cal_t       acdb_get_audio_cal;
162     acdb_send_voice_cal_t      acdb_send_voice_cal;
163     acdb_reload_vocvoltable_t  acdb_reload_vocvoltable;
164     void *hw_info;
165     char ec_ref_mixer_path[64];
166     bool speaker_lr_swap;
167 
168     int max_vol_index;
169     struct listnode acdb_meta_key_list;
170 };
171 
172 int pcm_device_table[AUDIO_USECASE_MAX][2] = {
173     [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {DEEP_BUFFER_PCM_DEVICE,
174                                             DEEP_BUFFER_PCM_DEVICE},
175     [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {LOWLATENCY_PCM_DEVICE,
176                                            LOWLATENCY_PCM_DEVICE},
177     [USECASE_AUDIO_PLAYBACK_HIFI] = {MULTIMEDIA2_PCM_DEVICE,
178                                         MULTIMEDIA2_PCM_DEVICE},
179     [USECASE_AUDIO_PLAYBACK_OFFLOAD] =
180                      {PLAYBACK_OFFLOAD_DEVICE, PLAYBACK_OFFLOAD_DEVICE},
181     [USECASE_AUDIO_PLAYBACK_ULL] = {MULTIMEDIA3_PCM_DEVICE, MULTIMEDIA3_PCM_DEVICE},
182     [USECASE_AUDIO_RECORD] = {AUDIO_RECORD_PCM_DEVICE, AUDIO_RECORD_PCM_DEVICE},
183     [USECASE_AUDIO_RECORD_LOW_LATENCY] = {LOWLATENCY_PCM_DEVICE,
184                                           LOWLATENCY_PCM_DEVICE},
185     [USECASE_AUDIO_HFP_SCO] = {HFP_PCM_RX, HFP_SCO_RX},
186     [USECASE_AUDIO_HFP_SCO_WB] = {HFP_PCM_RX, HFP_SCO_RX},
187     [USECASE_VOICE_CALL] = {VOICE_CALL_PCM_DEVICE, VOICE_CALL_PCM_DEVICE},
188     [USECASE_VOICE2_CALL] = {VOICE2_CALL_PCM_DEVICE, VOICE2_CALL_PCM_DEVICE},
189     [USECASE_VOLTE_CALL] = {VOLTE_CALL_PCM_DEVICE, VOLTE_CALL_PCM_DEVICE},
190     [USECASE_QCHAT_CALL] = {QCHAT_CALL_PCM_DEVICE, QCHAT_CALL_PCM_DEVICE},
191     [USECASE_VOWLAN_CALL] = {VOWLAN_CALL_PCM_DEVICE, VOWLAN_CALL_PCM_DEVICE},
192     [USECASE_VOICEMMODE1_CALL] = {-1, -1}, /* pcm ids updated from platform info file */
193     [USECASE_VOICEMMODE2_CALL] = {-1, -1}, /* pcm ids updated from platform info file */
194     [USECASE_INCALL_REC_UPLINK] = {AUDIO_RECORD_PCM_DEVICE,
195                                    AUDIO_RECORD_PCM_DEVICE},
196     [USECASE_INCALL_REC_DOWNLINK] = {AUDIO_RECORD_PCM_DEVICE,
197                                      AUDIO_RECORD_PCM_DEVICE},
198     [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = {AUDIO_RECORD_PCM_DEVICE,
199                                                 AUDIO_RECORD_PCM_DEVICE},
200     [USECASE_AUDIO_SPKR_CALIB_RX] = {SPKR_PROT_CALIB_RX_PCM_DEVICE, -1},
201     [USECASE_AUDIO_SPKR_CALIB_TX] = {-1, SPKR_PROT_CALIB_TX_PCM_DEVICE},
202     [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = {AFE_PROXY_PLAYBACK_PCM_DEVICE,
203                                           AFE_PROXY_RECORD_PCM_DEVICE},
204     [USECASE_AUDIO_RECORD_AFE_PROXY] = {AFE_PROXY_PLAYBACK_PCM_DEVICE,
205                                         AFE_PROXY_RECORD_PCM_DEVICE},
206 };
207 
208 /* Array to store sound devices */
209 static const char * const device_table[SND_DEVICE_MAX] = {
210     [SND_DEVICE_NONE] = "none",
211     /* Playback sound devices */
212     [SND_DEVICE_OUT_HANDSET] = "handset",
213     [SND_DEVICE_OUT_SPEAKER] = "speaker",
214     [SND_DEVICE_OUT_SPEAKER_REVERSE] = "speaker-reverse",
215     [SND_DEVICE_OUT_SPEAKER_SAFE] = "speaker-safe",
216     [SND_DEVICE_OUT_HEADPHONES] = "headphones",
217     [SND_DEVICE_OUT_LINE] = "line",
218     [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
219     [SND_DEVICE_OUT_SPEAKER_AND_LINE] = "speaker-and-line",
220     [SND_DEVICE_OUT_VOICE_HANDSET] = "voice-handset",
221     [SND_DEVICE_OUT_VOICE_HAC_HANDSET] = "voice-hac-handset",
222     [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
223     [SND_DEVICE_OUT_VOICE_SPEAKER_HFP] = "voice-speaker-hfp",
224     [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
225     [SND_DEVICE_OUT_VOICE_HEADSET] = "voice-headphones",
226     [SND_DEVICE_OUT_VOICE_LINE] = "voice-line",
227     [SND_DEVICE_OUT_HDMI] = "hdmi",
228     [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
229     [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
230     [SND_DEVICE_OUT_BT_SCO_WB] = "bt-sco-headset-wb",
231     [SND_DEVICE_OUT_BT_A2DP] = "bt-a2dp",
232     [SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP] = "speaker-and-bt-a2dp",
233     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP] = "speaker-safe-and-bt-a2dp",
234     [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
235     [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
236     [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
237     [SND_DEVICE_OUT_VOICE_TX] = "voice-tx",
238     [SND_DEVICE_OUT_VOICE_MUSIC_TX] = "voice-music-tx",
239     [SND_DEVICE_OUT_AFE_PROXY] = "afe-proxy",
240     [SND_DEVICE_OUT_USB_HEADSET] = "usb-headphones",
241     [SND_DEVICE_OUT_USB_HEADPHONES] = "usb-headphones",
242     [SND_DEVICE_OUT_VOICE_USB_HEADSET] = "usb-headphones",
243     [SND_DEVICE_OUT_VOICE_USB_HEADPHONES] = "usb-headphones",
244     [SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] = "speaker-and-usb-headphones",
245     [SND_DEVICE_OUT_SPEAKER_PROTECTED] = "speaker-protected",
246     [SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED] = "voice-speaker-protected",
247 
248     /* Capture sound devices */
249     [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
250     [SND_DEVICE_IN_HANDSET_MIC_EXTERNAL] = "handset-mic-ext",
251     [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic",
252     [SND_DEVICE_IN_HANDSET_MIC_NS] = "handset-mic",
253     [SND_DEVICE_IN_HANDSET_MIC_AEC_NS] = "handset-mic",
254     [SND_DEVICE_IN_HANDSET_DMIC] = "dmic-endfire",
255     [SND_DEVICE_IN_HANDSET_DMIC_AEC] = "dmic-endfire",
256     [SND_DEVICE_IN_HANDSET_DMIC_NS] = "dmic-endfire",
257     [SND_DEVICE_IN_HANDSET_DMIC_AEC_NS] = "dmic-endfire",
258     [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
259     [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "speaker-mic",
260     [SND_DEVICE_IN_SPEAKER_MIC_NS] = "speaker-mic",
261     [SND_DEVICE_IN_SPEAKER_MIC_AEC_NS] = "speaker-mic",
262     [SND_DEVICE_IN_SPEAKER_DMIC] = "speaker-dmic-endfire",
263     [SND_DEVICE_IN_SPEAKER_DMIC_AEC] = "speaker-dmic-endfire",
264     [SND_DEVICE_IN_SPEAKER_DMIC_NS] = "speaker-dmic-endfire",
265     [SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS] = "speaker-dmic-endfire",
266     [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
267     [SND_DEVICE_IN_HEADSET_MIC_FLUENCE] = "headset-mic",
268     [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
269     [SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP] = "voice-speaker-mic-hfp",
270     [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
271     [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
272     [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
273     [SND_DEVICE_IN_BT_SCO_MIC_NREC] = "bt-sco-mic",
274     [SND_DEVICE_IN_BT_SCO_MIC_WB] = "bt-sco-mic-wb",
275     [SND_DEVICE_IN_BT_SCO_MIC_WB_NREC] = "bt-sco-mic-wb",
276     [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
277     [SND_DEVICE_IN_VOICE_DMIC] = "voice-dmic-ef",
278     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC] = "voice-speaker-dmic-ef",
279     [SND_DEVICE_IN_VOICE_SPEAKER_QMIC] = "voice-speaker-qmic",
280     [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
281     [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
282     [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
283     [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
284     [SND_DEVICE_IN_VOICE_REC_MIC_NS] = "voice-rec-mic",
285     [SND_DEVICE_IN_VOICE_REC_DMIC_STEREO] = "voice-rec-dmic-ef",
286     [SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE] = "voice-rec-dmic-ef-fluence",
287     [SND_DEVICE_IN_VOICE_RX] = "voice-rx",
288     [SND_DEVICE_IN_USB_HEADSET_MIC] = "usb-headset-mic",
289     [SND_DEVICE_IN_CAPTURE_FM] = "capture-fm",
290     [SND_DEVICE_IN_AANC_HANDSET_MIC] = "aanc-handset-mic",
291     [SND_DEVICE_IN_QUAD_MIC] = "quad-mic",
292     [SND_DEVICE_IN_HANDSET_STEREO_DMIC] = "handset-stereo-dmic-ef",
293     [SND_DEVICE_IN_SPEAKER_STEREO_DMIC] = "speaker-stereo-dmic-ef",
294     [SND_DEVICE_IN_CAPTURE_VI_FEEDBACK] = "vi-feedback",
295     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BROADSIDE] = "voice-speaker-dmic-broadside",
296     [SND_DEVICE_IN_SPEAKER_DMIC_BROADSIDE] = "speaker-dmic-broadside",
297     [SND_DEVICE_IN_SPEAKER_DMIC_AEC_BROADSIDE] = "speaker-dmic-broadside",
298     [SND_DEVICE_IN_SPEAKER_DMIC_NS_BROADSIDE] = "speaker-dmic-broadside",
299     [SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS_BROADSIDE] = "speaker-dmic-broadside",
300     [SND_DEVICE_IN_HANDSET_QMIC] = "quad-mic",
301     [SND_DEVICE_IN_SPEAKER_QMIC_AEC] = "quad-mic",
302     [SND_DEVICE_IN_SPEAKER_QMIC_NS] = "quad-mic",
303     [SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS] = "quad-mic",
304 };
305 
306 static struct audio_effect_config \
307                effect_config_table[GET_IN_DEVICE_INDEX(SND_DEVICE_MAX)][EFFECT_COUNT] = {
308     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS)][EFFECT_AEC] = \
309                                       {TX_VOICE_FLUENCE_PROV2, 0x0, 0x10EAF, 0x01},
310     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS)][EFFECT_NS] = \
311                                       {TX_VOICE_FLUENCE_PROV2,  0x0, 0x10EAF, 0x02},
312     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS_BROADSIDE)][EFFECT_AEC] = \
313                                       {TX_VOICE_DM_FV5_BROADSIDE, 0x0, 0x10EAF, 0x01},
314     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS_BROADSIDE)][EFFECT_NS] = \
315                                       {TX_VOICE_DM_FV5_BROADSIDE, 0x0, 0x10EAF, 0x02},
316     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS)][EFFECT_AEC] = \
317                                       {TX_VOICE_FV5ECNS_DM, 0x0, 0x10EAF, 0x01},
318     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS)][EFFECT_NS] = \
319                                       {TX_VOICE_FV5ECNS_DM, 0x0, 0x10EAF, 0x02},
320     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_SPEAKER_MIC_AEC_NS)][EFFECT_AEC] = \
321                                       {TX_VOICE_FV5ECNS_SM, 0x0, 0x10EAF, 0x01},
322     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_SPEAKER_MIC_AEC_NS)][EFFECT_NS] = \
323                                       {TX_VOICE_FV5ECNS_SM, 0x0, 0x10EAF, 0x02},
324     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_HANDSET_DMIC_AEC_NS)][EFFECT_AEC] = \
325                                       {TX_VOICE_FV5ECNS_DM, 0x0, 0x10EAF, 0x01},
326     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_HANDSET_DMIC_AEC_NS)][EFFECT_NS] = \
327                                       {TX_VOICE_FV5ECNS_DM, 0x0, 0x10EAF, 0x02},
328     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_HANDSET_MIC_AEC_NS)][EFFECT_AEC] = \
329                                       {TX_VOICE_FV5ECNS_SM, 0x0, 0x10EAF, 0x01},
330     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_HANDSET_MIC_AEC_NS)][EFFECT_NS] = \
331                                       {TX_VOICE_FV5ECNS_SM, 0x0, 0x10EAF, 0x02},
332 };
333 
334 /* ACDB IDs (audio DSP path configuration IDs) for each sound device */
335 static int acdb_device_table[SND_DEVICE_MAX] = {
336     [SND_DEVICE_NONE] = -1,
337     [SND_DEVICE_OUT_HANDSET] = 7,
338     [SND_DEVICE_OUT_SPEAKER] = 14,
339     [SND_DEVICE_OUT_SPEAKER_REVERSE] = 14,
340     [SND_DEVICE_OUT_SPEAKER_SAFE] = 14,
341     [SND_DEVICE_OUT_LINE] = 10,
342     [SND_DEVICE_OUT_HEADPHONES] = 10,
343     [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
344     [SND_DEVICE_OUT_SPEAKER_AND_LINE] = 10,
345     [SND_DEVICE_OUT_VOICE_HANDSET] = 7,
346     [SND_DEVICE_OUT_VOICE_HAC_HANDSET] = 53,
347     [SND_DEVICE_OUT_VOICE_LINE] = 10,
348     [SND_DEVICE_OUT_VOICE_SPEAKER] = 14,
349     [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
350     [SND_DEVICE_OUT_VOICE_HEADSET] = 10,
351     [SND_DEVICE_OUT_HDMI] = 18,
352     [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 14,
353     [SND_DEVICE_OUT_BT_SCO] = 22,
354     [SND_DEVICE_OUT_BT_SCO_WB] = 39,
355     [SND_DEVICE_OUT_BT_A2DP] = 20,
356     [SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP] = 14,
357     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP] = 14,
358     [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
359     [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
360     [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
361     [SND_DEVICE_OUT_VOICE_TX] = 45,
362     [SND_DEVICE_OUT_VOICE_MUSIC_TX] = 3,
363     [SND_DEVICE_OUT_AFE_PROXY] = 0,
364     [SND_DEVICE_OUT_USB_HEADSET] = 45,
365     [SND_DEVICE_OUT_VOICE_USB_HEADSET] = 45,
366     [SND_DEVICE_OUT_VOICE_USB_HEADPHONES] = 45,
367     [SND_DEVICE_OUT_USB_HEADPHONES] = 45,
368     [SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] = 14,
369     [SND_DEVICE_OUT_SPEAKER_PROTECTED] = 124,
370     [SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED] = 101,
371     [SND_DEVICE_OUT_VOICE_SPEAKER_HFP] = 140,
372 
373     [SND_DEVICE_IN_HANDSET_MIC] = 4,
374     [SND_DEVICE_IN_HANDSET_MIC_EXTERNAL] = 4,
375     [SND_DEVICE_IN_HANDSET_MIC_AEC] = 106,
376     [SND_DEVICE_IN_HANDSET_MIC_NS] = 107,
377     [SND_DEVICE_IN_HANDSET_MIC_AEC_NS] = 108,
378     [SND_DEVICE_IN_HANDSET_DMIC] = 41,
379     [SND_DEVICE_IN_HANDSET_DMIC_AEC] = 109,
380     [SND_DEVICE_IN_HANDSET_DMIC_NS] = 110,
381     [SND_DEVICE_IN_HANDSET_DMIC_AEC_NS] = 111,
382     [SND_DEVICE_IN_SPEAKER_MIC] = 11,
383     [SND_DEVICE_IN_SPEAKER_MIC_AEC] = 112,
384     [SND_DEVICE_IN_SPEAKER_MIC_NS] = 113,
385     [SND_DEVICE_IN_SPEAKER_MIC_AEC_NS] = 114,
386     [SND_DEVICE_IN_SPEAKER_DMIC] = 43,
387     [SND_DEVICE_IN_SPEAKER_DMIC_AEC] = 115,
388     [SND_DEVICE_IN_SPEAKER_DMIC_NS] = 116,
389     [SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS] = 117,
390     [SND_DEVICE_IN_HEADSET_MIC] = 8,
391     [SND_DEVICE_IN_HEADSET_MIC_FLUENCE] = 47,
392     [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
393     [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8,
394     [SND_DEVICE_IN_HDMI_MIC] = 4,
395     [SND_DEVICE_IN_BT_SCO_MIC] = 21,
396     [SND_DEVICE_IN_BT_SCO_MIC_NREC] = 122,
397     [SND_DEVICE_IN_BT_SCO_MIC_WB] = 38,
398     [SND_DEVICE_IN_BT_SCO_MIC_WB_NREC] = 123,
399     [SND_DEVICE_IN_CAMCORDER_MIC] = 4,
400     [SND_DEVICE_IN_VOICE_DMIC] = 41,
401     [SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP] = 141,
402     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC] = 43,
403     [SND_DEVICE_IN_VOICE_SPEAKER_QMIC] = 19,
404     [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
405     [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
406     [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
407     [SND_DEVICE_IN_VOICE_REC_MIC] = 4,
408     [SND_DEVICE_IN_VOICE_REC_MIC_NS] = 107,
409     [SND_DEVICE_IN_VOICE_REC_DMIC_STEREO] = 34,
410     [SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE] = 41,
411     [SND_DEVICE_IN_VOICE_RX] = 44,
412     [SND_DEVICE_IN_USB_HEADSET_MIC] = 44,
413     [SND_DEVICE_IN_CAPTURE_FM] = 0,
414     [SND_DEVICE_IN_AANC_HANDSET_MIC] = 104,
415     [SND_DEVICE_IN_QUAD_MIC] = 46,
416     [SND_DEVICE_IN_HANDSET_STEREO_DMIC] = 34,
417     [SND_DEVICE_IN_SPEAKER_STEREO_DMIC] = 35,
418     [SND_DEVICE_IN_CAPTURE_VI_FEEDBACK] = 102,
419     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BROADSIDE] = 12,
420     [SND_DEVICE_IN_SPEAKER_DMIC_BROADSIDE] = 12,
421     [SND_DEVICE_IN_SPEAKER_DMIC_AEC_BROADSIDE] = 119,
422     [SND_DEVICE_IN_SPEAKER_DMIC_NS_BROADSIDE] = 121,
423     [SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS_BROADSIDE] = 120,
424     [SND_DEVICE_IN_HANDSET_QMIC] = 125,
425     [SND_DEVICE_IN_SPEAKER_QMIC_AEC] = 126,
426     [SND_DEVICE_IN_SPEAKER_QMIC_NS] = 127,
427     [SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS] = 129,
428 };
429 
430 struct name_to_index {
431     char name[100];
432     unsigned int index;
433 };
434 
435 #define TO_NAME_INDEX(X)   #X, X
436 
437 /* Used to get index from parsed sting */
438 static struct name_to_index snd_device_name_index[SND_DEVICE_MAX] = {
439     {TO_NAME_INDEX(SND_DEVICE_OUT_HANDSET)},
440     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER)},
441     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_REVERSE)},
442     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_SAFE)},
443     {TO_NAME_INDEX(SND_DEVICE_OUT_HEADPHONES)},
444     {TO_NAME_INDEX(SND_DEVICE_OUT_LINE)},
445     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES)},
446     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_LINE)},
447     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_HANDSET)},
448     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_HAC_HANDSET)},
449     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_SPEAKER)},
450     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_SPEAKER_HFP)},
451     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_HEADPHONES)},
452     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_HEADSET)},
453     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_LINE)},
454     {TO_NAME_INDEX(SND_DEVICE_OUT_HDMI)},
455     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_HDMI)},
456     {TO_NAME_INDEX(SND_DEVICE_OUT_BT_SCO)},
457     {TO_NAME_INDEX(SND_DEVICE_OUT_BT_SCO_WB)},
458     {TO_NAME_INDEX(SND_DEVICE_OUT_BT_A2DP)},
459     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP)},
460     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)},
461     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES)},
462     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES)},
463     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET)},
464     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TX)},
465     {TO_NAME_INDEX(SND_DEVICE_OUT_AFE_PROXY)},
466     {TO_NAME_INDEX(SND_DEVICE_OUT_USB_HEADSET)},
467     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_USB_HEADSET)},
468     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_USB_HEADPHONES)},
469     {TO_NAME_INDEX(SND_DEVICE_OUT_USB_HEADPHONES)},
470     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET)},
471     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_PROTECTED)},
472     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED)},
473     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC)},
474     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC_EXTERNAL)},
475     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC_AEC)},
476     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC_NS)},
477     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC_AEC_NS)},
478     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC)},
479     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC_AEC)},
480     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC_NS)},
481     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC_AEC_NS)},
482     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_MIC)},
483     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_MIC_AEC)},
484     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_MIC_NS)},
485     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_MIC_AEC_NS)},
486     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC)},
487     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC)},
488     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_NS)},
489     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS)},
490     {TO_NAME_INDEX(SND_DEVICE_IN_HEADSET_MIC)},
491     {TO_NAME_INDEX(SND_DEVICE_IN_HEADSET_MIC_FLUENCE)},
492     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_SPEAKER_MIC)},
493     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP)},
494     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_HEADSET_MIC)},
495     {TO_NAME_INDEX(SND_DEVICE_IN_HDMI_MIC)},
496     {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC)},
497     {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC_NREC)},
498     {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC_WB)},
499     {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC_WB_NREC)},
500     {TO_NAME_INDEX(SND_DEVICE_IN_CAMCORDER_MIC)},
501     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_DMIC)},
502     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_SPEAKER_DMIC)},
503     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_SPEAKER_QMIC)},
504     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC)},
505     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC)},
506     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC)},
507     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_MIC)},
508     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_MIC_NS)},
509     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_DMIC_STEREO)},
510     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE)},
511     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_RX)},
512     {TO_NAME_INDEX(SND_DEVICE_IN_USB_HEADSET_MIC)},
513     {TO_NAME_INDEX(SND_DEVICE_IN_CAPTURE_FM)},
514     {TO_NAME_INDEX(SND_DEVICE_IN_AANC_HANDSET_MIC)},
515     {TO_NAME_INDEX(SND_DEVICE_IN_QUAD_MIC)},
516     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_STEREO_DMIC)},
517     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_STEREO_DMIC)},
518     {TO_NAME_INDEX(SND_DEVICE_IN_CAPTURE_VI_FEEDBACK)},
519     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BROADSIDE)},
520     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_BROADSIDE)},
521     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC_BROADSIDE)},
522     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_NS_BROADSIDE)},
523     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS_BROADSIDE)},
524     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_QMIC)},
525     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_QMIC_AEC)},
526     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_QMIC_NS)},
527     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS)},
528 };
529 
530 static char * backend_table[SND_DEVICE_MAX] = {0};
531 static char * hw_interface_table[SND_DEVICE_MAX] = {0};
532 
533 static struct name_to_index usecase_name_index[AUDIO_USECASE_MAX] = {
534     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_DEEP_BUFFER)},
535     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_LOW_LATENCY)},
536     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_HIFI)},
537     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_OFFLOAD)},
538     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_ULL)},
539     {TO_NAME_INDEX(USECASE_AUDIO_RECORD)},
540     {TO_NAME_INDEX(USECASE_AUDIO_RECORD_LOW_LATENCY)},
541     {TO_NAME_INDEX(USECASE_VOICE_CALL)},
542     {TO_NAME_INDEX(USECASE_VOICE2_CALL)},
543     {TO_NAME_INDEX(USECASE_VOLTE_CALL)},
544     {TO_NAME_INDEX(USECASE_QCHAT_CALL)},
545     {TO_NAME_INDEX(USECASE_VOWLAN_CALL)},
546     {TO_NAME_INDEX(USECASE_VOICEMMODE1_CALL)},
547     {TO_NAME_INDEX(USECASE_VOICEMMODE2_CALL)},
548     {TO_NAME_INDEX(USECASE_AUDIO_HFP_SCO)},
549     {TO_NAME_INDEX(USECASE_AUDIO_HFP_SCO_WB)},
550     {TO_NAME_INDEX(USECASE_AUDIO_SPKR_CALIB_TX)},
551     {TO_NAME_INDEX(USECASE_AUDIO_A2DP_ABR_FEEDBACK)},
552 };
553 
554 #define DEEP_BUFFER_PLATFORM_DELAY (29*1000LL)
555 #define LOW_LATENCY_PLATFORM_DELAY (13*1000LL)
556 
557 static int audio_usecase_delay_ms[AUDIO_USECASE_MAX] = {0};
558 
559 static int audio_source_delay_ms[AUDIO_SOURCE_CNT] = {0};
560 
561 static struct name_to_index audio_source_index[AUDIO_SOURCE_CNT] = {
562     {TO_NAME_INDEX(AUDIO_SOURCE_DEFAULT)},
563     {TO_NAME_INDEX(AUDIO_SOURCE_MIC)},
564     {TO_NAME_INDEX(AUDIO_SOURCE_VOICE_UPLINK)},
565     {TO_NAME_INDEX(AUDIO_SOURCE_VOICE_DOWNLINK)},
566     {TO_NAME_INDEX(AUDIO_SOURCE_VOICE_CALL)},
567     {TO_NAME_INDEX(AUDIO_SOURCE_CAMCORDER)},
568     {TO_NAME_INDEX(AUDIO_SOURCE_VOICE_RECOGNITION)},
569     {TO_NAME_INDEX(AUDIO_SOURCE_VOICE_COMMUNICATION)},
570     {TO_NAME_INDEX(AUDIO_SOURCE_REMOTE_SUBMIX)},
571     {TO_NAME_INDEX(AUDIO_SOURCE_UNPROCESSED)},
572     {TO_NAME_INDEX(AUDIO_SOURCE_VOICE_PERFORMANCE)},
573 };
574 
query_platform(const char * snd_card_name,char * mixer_xml_path)575 static void query_platform(const char *snd_card_name,
576                                       char *mixer_xml_path)
577 {
578     if (!strncmp(snd_card_name, "msm8x16-snd-card-mtp",
579                  sizeof("msm8x16-snd-card-mtp"))) {
580         strlcpy(mixer_xml_path, MIXER_XML_PATH_MTP,
581                 sizeof(MIXER_XML_PATH_MTP));
582     } else if (!strncmp(snd_card_name, "msm8909-pm8916-snd-card",
583                  sizeof("msm8909-pm8916-snd-card"))) {
584         strlcpy(mixer_xml_path, MIXER_XML_PATH_MSM8909_PM8916,
585                 sizeof(MIXER_XML_PATH_MSM8909_PM8916));
586     } else if (!strncmp(snd_card_name, "msm-bg-snd-card",
587                 sizeof("msm-bg-snd-card"))) {
588         strlcpy(mixer_xml_path, MIXER_XML_PATH_BG,
589                 sizeof(MIXER_XML_PATH_BG));
590     } else if (!strncmp(snd_card_name, "msm8952-snd-card-mtp",
591                  sizeof("msm8952-snd-card-mtp"))) {
592         strlcpy(mixer_xml_path, MIXER_XML_PATH_MTP,
593                 sizeof(MIXER_XML_PATH_MTP));
594     } else if (!strncmp(snd_card_name, "msm8952-l9300-snd-card",
595                  sizeof("msm8952-l9300-snd-card"))) {
596         strlcpy(mixer_xml_path, MIXER_XML_PATH_L9300,
597                 sizeof(MIXER_XML_PATH_L9300));
598     } else {
599         strlcpy(mixer_xml_path, MIXER_XML_PATH,
600                 sizeof(MIXER_XML_PATH));
601     }
602 }
603 
604 static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
605 static bool is_tmus = false;
606 
check_operator()607 static void check_operator()
608 {
609     char value[PROPERTY_VALUE_MAX];
610     int mccmnc;
611     property_get("gsm.sim.operator.numeric",value,"0");
612     mccmnc = atoi(value);
613     ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
614     switch(mccmnc) {
615     /* TMUS MCC(310), MNC(490, 260, 026) */
616     case 310490:
617     case 310260:
618     case 310026:
619     /* Add new TMUS MNC(800, 660, 580, 310, 270, 250, 240, 230, 220, 210, 200, 160) */
620     case 310800:
621     case 310660:
622     case 310580:
623     case 310310:
624     case 310270:
625     case 310250:
626     case 310240:
627     case 310230:
628     case 310220:
629     case 310210:
630     case 310200:
631     case 310160:
632         is_tmus = true;
633         break;
634     }
635 }
636 
is_operator_tmus()637 bool is_operator_tmus()
638 {
639     pthread_once(&check_op_once_ctl, check_operator);
640     return is_tmus;
641 }
642 
get_current_operator()643 static char *get_current_operator()
644 {
645     struct listnode *node;
646     struct operator_info *info_item;
647     char mccmnc[PROPERTY_VALUE_MAX];
648     char *ret = NULL;
649 
650     property_get("gsm.sim.operator.numeric",mccmnc,"00000");
651 
652     list_for_each(node, &operator_info_list) {
653         info_item = node_to_item(node, struct operator_info, list);
654         if (strstr(info_item->mccmnc, mccmnc) != NULL) {
655             ret = info_item->name;
656         }
657     }
658 
659     return ret;
660 }
661 
get_operator_specific_device(snd_device_t snd_device)662 static struct operator_specific_device *get_operator_specific_device(snd_device_t snd_device)
663 {
664     struct listnode *node;
665     struct operator_specific_device *ret = NULL;
666     struct operator_specific_device *device_item;
667     char *operator_name;
668 
669     operator_name = get_current_operator();
670     if (operator_name == NULL)
671         return ret;
672 
673     list_for_each(node, operator_specific_device_table[snd_device]) {
674         device_item = node_to_item(node, struct operator_specific_device, list);
675         if (strcmp(operator_name, device_item->operator) == 0) {
676             ret = device_item;
677         }
678     }
679 
680     return ret;
681 }
682 
get_operator_specific_device_acdb_id(snd_device_t snd_device)683 static int get_operator_specific_device_acdb_id(snd_device_t snd_device)
684 {
685     struct operator_specific_device *device;
686     int ret = acdb_device_table[snd_device];
687 
688     device = get_operator_specific_device(snd_device);
689     if (device != NULL)
690         ret = device->acdb_id;
691 
692     return ret;
693 }
694 
get_operator_specific_device_mixer_path(snd_device_t snd_device)695 static const char *get_operator_specific_device_mixer_path(snd_device_t snd_device)
696 {
697     struct operator_specific_device *device;
698     const char *ret = device_table[snd_device];
699 
700     device = get_operator_specific_device(snd_device);
701     if (device != NULL)
702         ret = device->mixer_path;
703 
704     return ret;
705 }
706 
platform_send_gain_dep_cal(void * platform __unused,int level __unused)707 bool platform_send_gain_dep_cal(void *platform __unused, int level __unused)
708 {
709     return true;
710 }
711 
platform_set_echo_reference(struct audio_device * adev,bool enable,audio_devices_t out_device)712 void platform_set_echo_reference(struct audio_device *adev, bool enable,
713     audio_devices_t out_device)
714 {
715     struct platform_data *my_data = (struct platform_data *)adev->platform;
716     snd_device_t snd_device = SND_DEVICE_NONE;
717 
718     if (strcmp(my_data->ec_ref_mixer_path, "")) {
719         ALOGV("%s: disabling %s", __func__, my_data->ec_ref_mixer_path);
720         audio_route_reset_and_update_path(adev->audio_route,
721             my_data->ec_ref_mixer_path);
722     }
723 
724     if (enable) {
725         if (out_device != AUDIO_DEVICE_NONE) {
726             snd_device = platform_get_output_snd_device(adev->platform, out_device);
727             platform_add_backend_name(adev->platform, my_data->ec_ref_mixer_path, snd_device);
728         }
729 
730         strlcpy(my_data->ec_ref_mixer_path, "echo-reference",
731             sizeof(my_data->ec_ref_mixer_path));
732 
733         ALOGD("%s: enabling %s", __func__, my_data->ec_ref_mixer_path);
734         audio_route_apply_and_update_path(adev->audio_route,
735             my_data->ec_ref_mixer_path);
736     }
737 }
738 
set_platform_defaults()739 static void set_platform_defaults()
740 {
741     int32_t dev;
742     for (dev = 0; dev < SND_DEVICE_MAX; dev++) {
743         backend_table[dev] = NULL;
744         hw_interface_table[dev] = NULL;
745     }
746 
747     // TBD - do these go to the platform-info.xml file.
748     // will help in avoiding strdups here
749     backend_table[SND_DEVICE_IN_BT_SCO_MIC] = strdup("bt-sco");
750     backend_table[SND_DEVICE_IN_BT_SCO_MIC_WB] = strdup("bt-sco-wb");
751     backend_table[SND_DEVICE_IN_BT_SCO_MIC_NREC] = strdup("bt-sco");
752     backend_table[SND_DEVICE_IN_BT_SCO_MIC_WB_NREC] = strdup("bt-sco-wb");
753     backend_table[SND_DEVICE_OUT_BT_SCO] = strdup("bt-sco");
754     backend_table[SND_DEVICE_OUT_BT_SCO_WB] = strdup("bt-sco-wb");
755     backend_table[SND_DEVICE_OUT_BT_A2DP] = strdup("bt-a2dp");
756     backend_table[SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP] = strdup("speaker-and-bt-a2dp");
757     backend_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP] =
758         strdup("speaker-safe-and-bt-a2dp");
759     backend_table[SND_DEVICE_OUT_HDMI] = strdup("hdmi");
760     backend_table[SND_DEVICE_OUT_SPEAKER_AND_HDMI] = strdup("speaker-and-hdmi");
761     backend_table[SND_DEVICE_OUT_VOICE_TX] = strdup("afe-proxy");
762     backend_table[SND_DEVICE_IN_VOICE_RX] = strdup("afe-proxy");
763     backend_table[SND_DEVICE_OUT_AFE_PROXY] = strdup("afe-proxy");
764     backend_table[SND_DEVICE_OUT_USB_HEADSET] = strdup("usb-headphones");
765     backend_table[SND_DEVICE_OUT_VOICE_USB_HEADSET] = strdup("usb-headphones");
766     backend_table[SND_DEVICE_OUT_USB_HEADPHONES] = strdup("usb-headphones");
767     backend_table[SND_DEVICE_OUT_VOICE_USB_HEADPHONES] = strdup("usb-headphones");
768     backend_table[SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] =
769         strdup("speaker-and-usb-headphones");
770     backend_table[SND_DEVICE_IN_USB_HEADSET_MIC] = strdup("usb-headset-mic");
771     backend_table[SND_DEVICE_IN_CAPTURE_FM] = strdup("capture-fm");
772 }
773 
get_cvd_version(char * cvd_version,struct audio_device * adev)774 void get_cvd_version(char *cvd_version, struct audio_device *adev)
775 {
776     struct mixer_ctl *ctl;
777     int count;
778     int ret = 0;
779 
780     ctl = mixer_get_ctl_by_name(adev->mixer, CVD_VERSION_MIXER_CTL);
781     if (!ctl) {
782         ALOGE("%s: Could not get ctl for mixer cmd - %s",  __func__, CVD_VERSION_MIXER_CTL);
783         goto done;
784     }
785     mixer_ctl_update(ctl);
786 
787     count = mixer_ctl_get_num_values(ctl);
788     if (count > MAX_CVD_VERSION_STRING_SIZE)
789         count = MAX_CVD_VERSION_STRING_SIZE -1;
790 
791     ret = mixer_ctl_get_array(ctl, cvd_version, count);
792     if (ret != 0) {
793         ALOGE("%s: ERROR! mixer_ctl_get_array() failed to get CVD Version", __func__);
794         goto done;
795     }
796 
797 done:
798     return;
799 }
800 
hw_util_open(int card_no)801 static int hw_util_open(int card_no)
802 {
803     int fd = -1;
804     char dev_name[256];
805 
806     snprintf(dev_name, sizeof(dev_name), "/dev/snd/hwC%uD%u",
807                                card_no, WCD9XXX_CODEC_HWDEP_NODE);
808     ALOGD("%s Opening device %s\n", __func__, dev_name);
809     fd = open(dev_name, O_WRONLY);
810     if (fd < 0) {
811         ALOGE("%s: cannot open device '%s'\n", __func__, dev_name);
812         return fd;
813     }
814     ALOGD("%s success", __func__);
815     return fd;
816 }
817 
818 struct param_data {
819     int    use_case;
820     int    acdb_id;
821     int    get_size;
822     int    buff_size;
823     int    data_size;
824     void   *buff;
825 };
826 
send_bg_cal(struct platform_data * plat_data,int type,int fd)827 static int send_bg_cal(struct platform_data *plat_data,
828                         int type, int fd)
829 {
830     /*
831      * This is done to avoid compiler failure due to unused varialbes
832      * if both the below #defines are not present
833      */
834     (void)plat_data;
835     (void)type;
836     (void)fd;
837 
838 #ifdef BG_CAL_SUPPORT
839     if ((type == BG_CODEC_MIC_CAL) ||
840         (type == BG_CODEC_SPEAKER_CAL)) {
841 #ifdef BG_CODEC_CAL
842         int ret = 0, key = 0;
843         uint32_t param_len;
844         uint8_t *dptr = NULL;
845         struct wcdcal_ioctl_buffer codec_buffer;
846         acdb_audio_cal_cfg_t cal;
847 
848         memset(&cal, 0, sizeof(cal));
849         cal.persist = 1;
850         cal.cal_type = AUDIO_CORE_METAINFO_CAL_TYPE;
851         param_len = MAX_SET_CAL_BYTE_SIZE;
852         dptr = (unsigned char*) calloc(param_len, sizeof(unsigned char*));
853         if (dptr == NULL) {
854             ALOGE("%s Memory allocation failed for length %d",
855                     __func__, param_len);
856             return 0;
857         }
858         if (type == BG_CODEC_MIC_CAL) {
859             key = platform_get_meta_info_key_from_list(plat_data,
860                                                        "bg_miccal");
861             if (!key) {
862                 ALOGE("%s Failed to fetch mic metakey info", __func__);
863                 goto done;
864             }
865             ALOGV("%s BG mic with key:0x%x", __func__, key);
866             codec_buffer.cal_type = BG_CODEC_MIC_CAL;
867         } else if (type == BG_CODEC_SPEAKER_CAL) {
868             key = platform_get_meta_info_key_from_list(plat_data,
869                                                        "bg_speakercal");
870             if (!key) {
871                 ALOGE("%s Failed to fetch metakey info", __func__);
872                 goto done;
873             }
874             ALOGV("%s BG speaker with key:0x%x", __func__, key);
875             codec_buffer.cal_type = BG_CODEC_SPEAKER_CAL;
876         }
877         cal.acdb_dev_id = key;
878         ret = plat_data->acdb_get_audio_cal((void*)&cal, (void*)dptr,
879                                             &param_len);
880         if (ret) {
881             ALOGE("%s failed to get meta info for key 0x%x error %d",
882                     __func__, key, ret);
883             goto done;
884         }
885         codec_buffer.buffer = dptr;
886         codec_buffer.size = param_len;
887 
888         if (ioctl(fd, SNDRV_CTL_IOCTL_HWDEP_CAL_TYPE, &codec_buffer) < 0)
889             ALOGE("Failed to call ioctl  for mic err=%d calib.size=%d",
890                     errno, codec_buffer.size);
891         else
892             ALOGD("%s cal sent for %d calib.size=%d",
893                     __func__, cal.acdb_dev_id, codec_buffer.size);
894     done:
895         free(dptr);
896 #endif /* #ifdef BG_CODEC_CAL */
897         return 0;
898     } else
899 #endif /* #ifdef BG_CAL_SUPPORT */
900       return -1;
901 }
902 
send_codec_cal(acdb_loader_get_calibration_t acdb_loader_get_calibration,struct platform_data * plat_data,int fd)903 static int send_codec_cal(acdb_loader_get_calibration_t acdb_loader_get_calibration,
904                           struct platform_data *plat_data , int fd)
905 {
906     int ret = 0, type;
907 
908     for (type = WCD9XXX_ANC_CAL; type < WCD9XXX_MAX_CAL; type++) {
909         struct wcdcal_ioctl_buffer codec_buffer;
910         struct param_data calib;
911 
912         if (send_bg_cal(plat_data, type, fd) == 0)
913             continue;
914 
915         if (type != WCD9XXX_MBHC_CAL)
916             continue;
917 
918         calib.get_size = 1;
919         ret = acdb_loader_get_calibration(cal_name_info[type], sizeof(struct param_data),
920                                                                  &calib);
921         if (ret < 0) {
922             ALOGE("%s get_calibration failed\n", __func__);
923             return ret;
924         }
925         calib.get_size = 0;
926         calib.buff = malloc(calib.buff_size);
927         if(calib.buff == NULL) {
928             ALOGE("%s mem allocation for %d bytes for %s failed\n"
929                 , __func__, calib.buff_size, cal_name_info[type]);
930             return -1;
931         }
932         ret = acdb_loader_get_calibration(cal_name_info[type],
933                               sizeof(struct param_data), &calib);
934         if (ret < 0) {
935             ALOGE("%s get_calibration failed type=%s calib.size=%d\n"
936                 , __func__, cal_name_info[type], codec_buffer.size);
937             free(calib.buff);
938             return ret;
939         }
940         codec_buffer.buffer = calib.buff;
941         codec_buffer.size = calib.data_size;
942         codec_buffer.cal_type = type;
943         if (ioctl(fd, SNDRV_CTL_IOCTL_HWDEP_CAL_TYPE, &codec_buffer) < 0)
944             ALOGE("Failed to call ioctl  for %s err=%d calib.size=%d",
945                 cal_name_info[type], errno, codec_buffer.size);
946         ALOGD("%s cal sent for %s calib.size=%d"
947             , __func__, cal_name_info[type], codec_buffer.size);
948         free(calib.buff);
949     }
950     return ret;
951 }
952 
audio_hwdep_send_cal(struct platform_data * plat_data)953 static void audio_hwdep_send_cal(struct platform_data *plat_data)
954 {
955     int fd;
956 
957     fd = hw_util_open(plat_data->adev->snd_card);
958     if (fd == -1) {
959         ALOGE("%s error open\n", __func__);
960         return;
961     }
962 
963     acdb_loader_get_calibration = (acdb_loader_get_calibration_t)
964           dlsym(plat_data->acdb_handle, "acdb_loader_get_calibration");
965 
966     if (acdb_loader_get_calibration == NULL) {
967         ALOGE("%s: ERROR. dlsym Error:%s acdb_loader_get_calibration", __func__,
968            dlerror());
969         return;
970     }
971     if (send_codec_cal(acdb_loader_get_calibration, plat_data, fd) < 0)
972         ALOGE("%s: Could not send anc cal", __FUNCTION__);
973 }
974 
975 
platform_acdb_init(void * platform)976 int platform_acdb_init(void *platform)
977 {
978     struct platform_data *my_data = (struct platform_data *)platform;
979     char *cvd_version = NULL;
980     int key = 0;
981     const char *snd_card_name;
982     int result = 0;
983     char value[PROPERTY_VALUE_MAX];
984     struct listnode *node;
985     struct meta_key_list *key_info;
986 
987     cvd_version = calloc(1, MAX_CVD_VERSION_STRING_SIZE);
988     if (!cvd_version)
989         ALOGE("Failed to allocate cvd version");
990     else
991         get_cvd_version(cvd_version, my_data->adev);
992 
993     property_get("audio.ds1.metainfo.key",value,"0");
994     key = atoi(value);
995     snd_card_name = mixer_get_name(my_data->adev->mixer);
996 
997     if (my_data->acdb_init_v3) {
998         result = my_data->acdb_init_v3(snd_card_name, cvd_version,
999                                            &my_data->acdb_meta_key_list);
1000     } else if (my_data->acdb_init) {
1001         result = my_data->acdb_init((char *)snd_card_name, cvd_version, key);
1002     }
1003 
1004     if (cvd_version)
1005         free(cvd_version);
1006     if (!result) {
1007         ALOGD("ACDB initialized");
1008         audio_hwdep_send_cal(my_data);
1009     } else {
1010         ALOGD("ACDB initialization failed");
1011     }
1012     return result;
1013 }
1014 
1015 // Treblized config files will be located in /odm/etc or /vendor/etc.
1016 static const char *kConfigLocationList[] =
1017         {"/odm/etc", "/vendor/etc", "/system/etc"};
1018 static const int kConfigLocationListSize =
1019         (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
1020 
resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])1021 bool resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH]) {
1022     char full_config_path[MIXER_PATH_MAX_LENGTH];
1023     for (int i = 0; i < kConfigLocationListSize; i++) {
1024         snprintf(full_config_path,
1025                  MIXER_PATH_MAX_LENGTH,
1026                  "%s/%s",
1027                  kConfigLocationList[i],
1028                  file_name);
1029         if (F_OK == access(full_config_path, 0)) {
1030             strcpy(file_name, full_config_path);
1031             return true;
1032         }
1033     }
1034     return false;
1035 }
1036 
platform_init(struct audio_device * adev)1037 void *platform_init(struct audio_device *adev)
1038 {
1039     char platform[PROPERTY_VALUE_MAX] = {0};
1040     char baseband[PROPERTY_VALUE_MAX] = {0};
1041     char value[PROPERTY_VALUE_MAX] = {0};
1042     struct platform_data *my_data = NULL;
1043     int retry_num = 0, snd_card_num = 0, key = 0;
1044     const char *snd_card_name;
1045     char mixer_xml_path[MAX_MIXER_XML_PATH] = {0};
1046     char platform_info_path[MAX_MIXER_XML_PATH] = {0};
1047     char ffspEnable[PROPERTY_VALUE_MAX] = {0};
1048     char *cvd_version = NULL;
1049     int idx;
1050 
1051     my_data = calloc(1, sizeof(struct platform_data));
1052     if (!my_data) {
1053         ALOGE("failed to allocate platform data");
1054         return NULL;
1055     }
1056 
1057     list_init(&operator_info_list);
1058     bool card_verifed[MAX_SND_CARD] = {0};
1059     const int retry_limit = property_get_int32("audio.snd_card.open.retries", RETRY_NUMBER);
1060 
1061     for (;;) {
1062         if (snd_card_num >= MAX_SND_CARD) {
1063             if (retry_num++ >= retry_limit) {
1064                 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
1065                 free(my_data);
1066                 my_data = NULL;
1067                 return NULL;
1068             }
1069 
1070             snd_card_num = 0;
1071             usleep(RETRY_US);
1072             continue;
1073         }
1074 
1075         if (card_verifed[snd_card_num]) {
1076             ++snd_card_num;
1077             continue;
1078         }
1079 
1080         adev->mixer = mixer_open(snd_card_num);
1081 
1082         if (!adev->mixer) {
1083             ALOGE("%s: Unable to open the mixer card: %d", __func__,
1084                snd_card_num);
1085             ++snd_card_num;
1086             continue;
1087         }
1088 
1089         card_verifed[snd_card_num] = true;
1090 
1091         snd_card_name = mixer_get_name(adev->mixer);
1092         ALOGV("%s: snd_card_name: %s", __func__, snd_card_name);
1093 
1094         my_data->hw_info = hw_info_init(snd_card_name);
1095         if (!my_data->hw_info) {
1096             ALOGE("%s: Failed to init hardware info", __func__);
1097         } else {
1098             query_platform(snd_card_name, mixer_xml_path);
1099             if (!resolve_config_file(mixer_xml_path)) {
1100                 memset(mixer_xml_path, 0, sizeof(mixer_xml_path));
1101                 strlcpy(mixer_xml_path, MIXER_XML_PATH, MAX_MIXER_XML_PATH);
1102                 resolve_config_file(mixer_xml_path);
1103             }
1104             ALOGD("%s: mixer path file is %s", __func__,
1105                                     mixer_xml_path);
1106             adev->audio_route = audio_route_init(snd_card_num,
1107                                                  mixer_xml_path);
1108             if (!adev->audio_route) {
1109                 ALOGE("%s: Failed to init audio route controls, aborting.",
1110                        __func__);
1111                 hw_info_deinit(my_data->hw_info);
1112                 my_data->hw_info = NULL;
1113                 free(my_data);
1114                 my_data = NULL;
1115                 mixer_close(adev->mixer);
1116                 adev->mixer = NULL;
1117                 return NULL;
1118             }
1119             adev->snd_card = snd_card_num;
1120             ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
1121             break;
1122         }
1123         ++snd_card_num;
1124         mixer_close(adev->mixer);
1125         adev->mixer = NULL;
1126     }
1127 
1128     //set max volume step for voice call
1129     property_get("ro.config.vc_call_vol_steps", value, TOSTRING(MAX_VOL_INDEX));
1130     my_data->max_vol_index = atoi(value);
1131 
1132     my_data->adev = adev;
1133     my_data->fluence_in_spkr_mode = false;
1134     my_data->fluence_in_voice_call = false;
1135     my_data->fluence_in_voice_rec = false;
1136     my_data->fluence_type = FLUENCE_NONE;
1137     my_data->fluence_mode = FLUENCE_ENDFIRE;
1138 
1139     if (property_get("ro.vendor.audio.sdk.fluencetype", my_data->fluence_cap, NULL) == 0) {
1140         property_get("ro.qc.sdk.audio.fluencetype", my_data->fluence_cap, "");
1141     }
1142     if (!strncmp("fluencepro", my_data->fluence_cap, sizeof("fluencepro"))) {
1143         my_data->fluence_type = FLUENCE_QUAD_MIC | FLUENCE_DUAL_MIC;
1144     } else if (!strncmp("fluence", my_data->fluence_cap, sizeof("fluence"))) {
1145         my_data->fluence_type = FLUENCE_DUAL_MIC;
1146     } else {
1147         my_data->fluence_type = FLUENCE_NONE;
1148     }
1149 
1150     if (my_data->fluence_type != FLUENCE_NONE) {
1151         property_get("persist.audio.fluence.voicecall",value,"");
1152         if (!strncmp("true", value, sizeof("true"))) {
1153             my_data->fluence_in_voice_call = true;
1154         }
1155 
1156         property_get("persist.audio.fluence.voicerec",value,"");
1157         if (!strncmp("true", value, sizeof("true"))) {
1158             my_data->fluence_in_voice_rec = true;
1159         }
1160 
1161         property_get("persist.audio.fluence.speaker",value,"");
1162         if (!strncmp("true", value, sizeof("true"))) {
1163             my_data->fluence_in_spkr_mode = true;
1164         }
1165 
1166         property_get("persist.audio.fluence.mode",value,"");
1167         if (!strncmp("broadside", value, sizeof("broadside"))) {
1168             my_data->fluence_mode = FLUENCE_BROADSIDE;
1169         }
1170     }
1171 
1172     property_get("persist.audio.FFSP.enable", ffspEnable, "");
1173     if (!strncmp("true", ffspEnable, sizeof("true"))) {
1174         acdb_device_table[SND_DEVICE_OUT_SPEAKER] = 131;
1175         acdb_device_table[SND_DEVICE_OUT_SPEAKER_REVERSE] = 131;
1176         acdb_device_table[SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 131;
1177         acdb_device_table[SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] = 131;
1178     }
1179 
1180     list_init(&my_data->acdb_meta_key_list);
1181     set_platform_defaults();
1182     /* Initialize ACDB and PCM ID's */
1183     strlcpy(platform_info_path, PLATFORM_INFO_XML_PATH, MAX_MIXER_XML_PATH);
1184     resolve_config_file(platform_info_path);
1185     platform_info_init(platform_info_path, my_data,
1186                        true, &platform_set_parameters);
1187 
1188     my_data->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
1189     if (my_data->acdb_handle == NULL) {
1190         ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
1191     } else {
1192         ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
1193         my_data->acdb_deallocate = (acdb_deallocate_t)dlsym(my_data->acdb_handle,
1194                                                     "acdb_loader_deallocate_ACDB");
1195         if (!my_data->acdb_deallocate)
1196             ALOGE("%s: Could not find the symbol acdb_loader_deallocate_ACDB from %s",
1197                   __func__, LIB_ACDB_LOADER);
1198 
1199         my_data->acdb_send_audio_cal_v3 = (acdb_send_audio_cal_v3_t)dlsym(my_data->acdb_handle,
1200                                                     "acdb_loader_send_audio_cal_v3");
1201         if (!my_data->acdb_send_audio_cal_v3)
1202             ALOGE("%s: Could not find the symbol acdb_send_audio_cal from %s",
1203                   __func__, LIB_ACDB_LOADER);
1204 
1205         my_data->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(my_data->acdb_handle,
1206                                                     "acdb_loader_send_audio_cal");
1207         if (!my_data->acdb_send_audio_cal)
1208             ALOGE("%s: Could not find the symbol acdb_send_audio_cal from %s",
1209                   __func__, LIB_ACDB_LOADER);
1210 
1211         my_data->acdb_get_audio_cal = (acdb_get_audio_cal_t)dlsym(my_data->acdb_handle,
1212                                                   "acdb_loader_get_audio_cal_v2");
1213         if (!my_data->acdb_get_audio_cal)
1214             ALOGE("%s: Could not find the symbol acdb_get_audio_cal_v2 from %s",
1215                   __func__, LIB_ACDB_LOADER);
1216 
1217         my_data->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(my_data->acdb_handle,
1218                                                     "acdb_loader_send_voice_cal");
1219         if (!my_data->acdb_send_voice_cal)
1220             ALOGE("%s: Could not find the symbol acdb_loader_send_voice_cal from %s",
1221                   __func__, LIB_ACDB_LOADER);
1222 
1223         my_data->acdb_reload_vocvoltable = (acdb_reload_vocvoltable_t)dlsym(my_data->acdb_handle,
1224                                                     "acdb_loader_reload_vocvoltable");
1225         if (!my_data->acdb_reload_vocvoltable)
1226             ALOGE("%s: Could not find the symbol acdb_loader_reload_vocvoltable from %s",
1227                   __func__, LIB_ACDB_LOADER);
1228 
1229         my_data->acdb_init = (acdb_init_v2_cvd_t)dlsym(my_data->acdb_handle,
1230                                                     "acdb_loader_init_v2");
1231         if (my_data->acdb_init == NULL) {
1232             ALOGE("%s: dlsym error %s for acdb_loader_init_v2", __func__, dlerror());
1233             goto acdb_init_fail;
1234         }
1235 
1236         my_data->acdb_init_v3 = (acdb_init_v3_t)dlsym(my_data->acdb_handle,
1237                                                    "acdb_loader_init_v3");
1238         if (my_data->acdb_init_v3 == NULL) {
1239             ALOGI("%s: dlsym error %s for acdb_loader_init_v3", __func__, dlerror());
1240         }
1241         platform_acdb_init(my_data);
1242     }
1243 
1244 acdb_init_fail:
1245 
1246     /*init a2dp*/
1247     audio_extn_a2dp_init(adev);
1248 
1249     /* Read one time ssr property */
1250     audio_extn_spkr_prot_init(adev);
1251 
1252     return my_data;
1253 }
1254 
platform_deinit(void * platform)1255 void platform_deinit(void *platform)
1256 {
1257     struct platform_data *my_data = (struct platform_data *)platform;
1258 
1259     int32_t dev;
1260     struct operator_info *info_item;
1261     struct operator_specific_device *device_item;
1262     struct listnode *node;
1263 
1264     audio_extn_spkr_prot_deinit(my_data->adev);
1265 
1266     hw_info_deinit(my_data->hw_info);
1267 
1268     for (dev = 0; dev < SND_DEVICE_MAX; dev++) {
1269         if (backend_table[dev]) {
1270             free(backend_table[dev]);
1271             backend_table[dev]= NULL;
1272         }
1273         if (operator_specific_device_table[dev]) {
1274             while (!list_empty(operator_specific_device_table[dev])) {
1275                 node = list_head(operator_specific_device_table[dev]);
1276                 list_remove(node);
1277                 device_item = node_to_item(node, struct operator_specific_device, list);
1278                 free(device_item->operator);
1279                 free(device_item->mixer_path);
1280                 free(device_item);
1281             }
1282             free(operator_specific_device_table[dev]);
1283         }
1284     }
1285 
1286     while (!list_empty(&operator_info_list)) {
1287         node = list_head(&operator_info_list);
1288         list_remove(node);
1289         info_item = node_to_item(node, struct operator_info, list);
1290         free(info_item->name);
1291         free(info_item->mccmnc);
1292         free(info_item);
1293     }
1294 
1295     mixer_close(my_data->adev->mixer);
1296     free(platform);
1297 }
1298 
platform_get_snd_device_name(snd_device_t snd_device)1299 const char *platform_get_snd_device_name(snd_device_t snd_device)
1300 {
1301     if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX) {
1302         if (operator_specific_device_table[snd_device] != NULL) {
1303             return get_operator_specific_device_mixer_path(snd_device);
1304         }
1305         return device_table[snd_device];
1306     } else
1307         return "none";
1308 }
1309 
platform_get_snd_device_name_extn(void * platform,snd_device_t snd_device,char * device_name)1310 int platform_get_snd_device_name_extn(void *platform, snd_device_t snd_device,
1311                                       char *device_name)
1312 {
1313     struct platform_data *my_data = (struct platform_data *)platform;
1314 
1315     if (platform == NULL) {
1316         ALOGW("%s: something wrong, use legacy get_snd_device name", __func__);
1317         strlcpy(device_name, platform_get_snd_device_name(snd_device),
1318                  DEVICE_NAME_MAX_SIZE);
1319     } else if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX) {
1320         if (operator_specific_device_table[snd_device] != NULL) {
1321             strlcpy(device_name, get_operator_specific_device_mixer_path(snd_device),
1322                      DEVICE_NAME_MAX_SIZE);
1323         } else {
1324             strlcpy(device_name, device_table[snd_device], DEVICE_NAME_MAX_SIZE);
1325         }
1326         hw_info_append_hw_type(my_data->hw_info, snd_device, device_name);
1327     } else {
1328         strlcpy(device_name, "none", DEVICE_NAME_MAX_SIZE);
1329         return -EINVAL;
1330     }
1331 
1332     return 0;
1333 }
1334 
platform_check_and_set_playback_backend_cfg(struct audio_device * adev __unused,struct audio_usecase * usecase __unused,snd_device_t snd_device __unused)1335 bool platform_check_and_set_playback_backend_cfg(struct audio_device* adev __unused,
1336                                               struct audio_usecase *usecase __unused,
1337                                               snd_device_t snd_device __unused)
1338 {
1339     return false;
1340 }
1341 
platform_check_and_set_capture_backend_cfg(struct audio_device * adev __unused,struct audio_usecase * usecase __unused,snd_device_t snd_device __unused)1342 bool platform_check_and_set_capture_backend_cfg(struct audio_device* adev __unused,
1343                                               struct audio_usecase *usecase __unused,
1344                                               snd_device_t snd_device __unused)
1345 {
1346     return false;
1347 }
1348 
platform_add_gain_level_mapping(struct amp_db_and_gain_table * tbl_entry __unused)1349 bool platform_add_gain_level_mapping(struct amp_db_and_gain_table *tbl_entry __unused)
1350 {
1351     return false;
1352 }
1353 
platform_get_gain_level_mapping(struct amp_db_and_gain_table * mapping_tbl __unused,int table_size __unused)1354 int platform_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl __unused,
1355                                     int table_size __unused)
1356 {
1357     return 0;
1358 }
1359 
platform_add_backend_name(void * platform,char * mixer_path,snd_device_t snd_device)1360 void platform_add_backend_name(void *platform, char *mixer_path,
1361                                snd_device_t snd_device)
1362 {
1363 
1364     struct platform_data *my_data = (struct platform_data *)platform;
1365 
1366     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
1367         ALOGE("%s: Invalid snd_device = %d", __func__, snd_device);
1368         return;
1369     }
1370 
1371     const char * suffix = backend_table[snd_device];
1372 
1373     if (suffix != NULL) {
1374         strlcat(mixer_path, " ", MIXER_PATH_MAX_LENGTH);
1375         strlcat(mixer_path, suffix, MIXER_PATH_MAX_LENGTH);
1376     }
1377 }
1378 
platform_check_backends_match(snd_device_t snd_device1,snd_device_t snd_device2)1379 bool platform_check_backends_match(snd_device_t snd_device1, snd_device_t snd_device2)
1380 {
1381     bool result = true;
1382 
1383     ALOGV("%s: snd_device1 = %s, snd_device2 = %s", __func__,
1384                 platform_get_snd_device_name(snd_device1),
1385                 platform_get_snd_device_name(snd_device2));
1386 
1387     if ((snd_device1 < SND_DEVICE_MIN) || (snd_device1 >= SND_DEVICE_MAX)) {
1388         ALOGE("%s: Invalid snd_device = %s", __func__,
1389                 platform_get_snd_device_name(snd_device1));
1390         return false;
1391     }
1392     if ((snd_device2 < SND_DEVICE_MIN) || (snd_device2 >= SND_DEVICE_MAX)) {
1393         ALOGE("%s: Invalid snd_device = %s", __func__,
1394                 platform_get_snd_device_name(snd_device2));
1395         return false;
1396     }
1397     const char * be_itf1 = hw_interface_table[snd_device1];
1398     const char * be_itf2 = hw_interface_table[snd_device2];
1399 
1400     if (NULL != be_itf1 && NULL != be_itf2) {
1401         if ((NULL == strstr(be_itf2, be_itf1)) && (NULL == strstr(be_itf1, be_itf2)))
1402             result = false;
1403     } else if (NULL != be_itf2 && (NULL == strstr(be_itf2, DEFAULT_RX_BACKEND))) {
1404         result = false;
1405     } else if (NULL != be_itf1 && (NULL == strstr(be_itf1, DEFAULT_RX_BACKEND))) {
1406         result = false;
1407     }
1408 
1409     ALOGV("%s: be_itf1 = %s, be_itf2 = %s, match %d", __func__, be_itf1, be_itf2, result);
1410     return result;
1411 }
1412 
platform_get_pcm_device_id(audio_usecase_t usecase,int device_type)1413 int platform_get_pcm_device_id(audio_usecase_t usecase, int device_type)
1414 {
1415     int device_id = -1;
1416 
1417     if (device_type == PCM_PLAYBACK)
1418         device_id = pcm_device_table[usecase][0];
1419     else
1420         device_id = pcm_device_table[usecase][1];
1421     return device_id;
1422 }
1423 
platform_get_haptics_pcm_device_id()1424 int platform_get_haptics_pcm_device_id()
1425 {
1426     return -1;
1427 }
1428 
find_index(struct name_to_index * table,int32_t len,const char * name)1429 static int find_index(struct name_to_index * table, int32_t len, const char * name)
1430 {
1431     int ret = 0;
1432     int32_t i;
1433 
1434     if (table == NULL) {
1435         ALOGE("%s: table is NULL", __func__);
1436         ret = -ENODEV;
1437         goto done;
1438     }
1439 
1440     if (name == NULL) {
1441         ALOGE("null key");
1442         ret = -ENODEV;
1443         goto done;
1444     }
1445 
1446     for (i=0; i < len; i++) {
1447         const char* tn = table[i].name;
1448         size_t len = strlen(tn);
1449         if (strncmp(tn, name, len) == 0) {
1450             if (strlen(name) != len) {
1451                 continue; // substring
1452             }
1453             ret = table[i].index;
1454             goto done;
1455         }
1456     }
1457     ALOGE("%s: Could not find index for name = %s",
1458             __func__, name);
1459     ret = -ENODEV;
1460 done:
1461     return ret;
1462 }
1463 
platform_get_snd_device_index(char * device_name)1464 int platform_get_snd_device_index(char *device_name)
1465 {
1466     return find_index(snd_device_name_index, SND_DEVICE_MAX, device_name);
1467 }
1468 
platform_get_usecase_index(const char * usecase_name)1469 int platform_get_usecase_index(const char *usecase_name)
1470 {
1471     return find_index(usecase_name_index, AUDIO_USECASE_MAX, usecase_name);
1472 }
1473 
platform_get_audio_source_index(const char * audio_source_name)1474 int platform_get_audio_source_index(const char *audio_source_name)
1475 {
1476     return find_index(audio_source_index, AUDIO_SOURCE_CNT, audio_source_name);
1477 }
1478 
platform_get_effect_config_data(snd_device_t snd_device,struct audio_effect_config * effect_config,effect_type_t effect_type)1479 int platform_get_effect_config_data(snd_device_t snd_device,
1480                                       struct audio_effect_config *effect_config,
1481                                       effect_type_t effect_type)
1482 {
1483     int ret = 0;
1484 
1485     if ((snd_device < SND_DEVICE_IN_BEGIN) || (snd_device >= SND_DEVICE_MAX) ||
1486         (effect_type <= EFFECT_NONE) || (effect_type >= EFFECT_COUNT)) {
1487         ALOGE("%s: Invalid snd_device = %d or effect_type = %d",
1488             __func__, snd_device, effect_type);
1489         ret = -EINVAL;
1490         goto done;
1491     }
1492 
1493     if (effect_config == NULL) {
1494         ALOGE("%s: Invalid effect_config", __func__);
1495         ret = -EINVAL;
1496         goto done;
1497     }
1498 
1499     ALOGV("%s: snd_device = %d module_id = %d",
1500           __func__, snd_device,
1501           effect_config_table[GET_IN_DEVICE_INDEX(snd_device)][effect_type].module_id);
1502     *effect_config = effect_config_table[GET_IN_DEVICE_INDEX(snd_device)][effect_type];
1503 
1504 done:
1505     return ret;
1506 }
1507 
platform_add_external_specific_device(snd_device_t snd_device __unused,const char * name __unused,unsigned int acdb_id __unused)1508 void platform_add_external_specific_device(snd_device_t snd_device __unused,
1509                                            const char *name __unused,
1510                                            unsigned int acdb_id __unused)
1511 {
1512     return;
1513 }
1514 
platform_add_operator_specific_device(snd_device_t snd_device,const char * operator,const char * mixer_path,unsigned int acdb_id)1515 void platform_add_operator_specific_device(snd_device_t snd_device,
1516                                            const char *operator,
1517                                            const char *mixer_path,
1518                                            unsigned int acdb_id)
1519 {
1520     struct operator_specific_device *device;
1521 
1522     if (operator_specific_device_table[snd_device] == NULL) {
1523         operator_specific_device_table[snd_device] =
1524             (struct listnode *)calloc(1, sizeof(struct listnode));
1525         list_init(operator_specific_device_table[snd_device]);
1526     }
1527 
1528     device = (struct operator_specific_device *)calloc(1, sizeof(struct operator_specific_device));
1529 
1530     device->operator = strdup(operator);
1531     device->mixer_path = strdup(mixer_path);
1532     device->acdb_id = acdb_id;
1533 
1534     list_add_tail(operator_specific_device_table[snd_device], &device->list);
1535 
1536     ALOGD("%s: device[%s] -> operator[%s] mixer_path[%s] acdb_id[%d]", __func__,
1537             platform_get_snd_device_name(snd_device), operator, mixer_path, acdb_id);
1538 
1539 }
1540 
platform_set_snd_device_acdb_id(snd_device_t snd_device,unsigned int acdb_id)1541 int platform_set_snd_device_acdb_id(snd_device_t snd_device, unsigned int acdb_id)
1542 {
1543     int ret = 0;
1544 
1545     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
1546         ALOGE("%s: Invalid snd_device = %d",
1547             __func__, snd_device);
1548         ret = -EINVAL;
1549         goto done;
1550     }
1551     ALOGV("%s: acdb_device_table[%s]: old = %d new = %d", __func__,
1552           platform_get_snd_device_name(snd_device), acdb_device_table[snd_device], acdb_id);
1553     acdb_device_table[snd_device] = acdb_id;
1554 done:
1555     return ret;
1556 }
1557 
platform_set_acdb_metainfo_key(void * platform,char * name,int key)1558 int platform_set_acdb_metainfo_key(void *platform, char *name, int key)
1559 {
1560     struct meta_key_list *key_info;
1561     struct platform_data *pdata = (struct platform_data *)platform;
1562 
1563     if (key < 0) {
1564         ALOGE("%s: Incorrect Meta key\n", __func__);
1565         return -EINVAL;
1566     }
1567     key_info = (struct meta_key_list *)calloc(1, sizeof(struct meta_key_list));
1568     if (!key_info) {
1569         ALOGE("%s: Could not allocate memory for key %d", __func__, key);
1570         return -ENOMEM;
1571     }
1572 
1573     key_info->cal_info.nKey = key;
1574     strlcpy(key_info->name, name, sizeof(key_info->name));
1575     list_add_tail(&pdata->acdb_meta_key_list, &key_info->list);
1576     ALOGD("%s: successfully added module %s and key %d to the list", __func__,
1577                    key_info->name, key_info->cal_info.nKey);
1578     return 0;
1579 }
1580 
platform_get_meta_info_key_from_list(void * platform,char * mod_name)1581 static int platform_get_meta_info_key_from_list(void *platform, char *mod_name)
1582 {
1583     struct listnode *node;
1584     struct meta_key_list *key_info;
1585     struct platform_data *pdata = (struct platform_data *)platform;
1586     int key = 0;
1587 
1588     ALOGV("%s: for module %s", __func__, mod_name);
1589     list_for_each(node, &pdata->acdb_meta_key_list) {
1590         key_info = node_to_item(node, struct meta_key_list, list);
1591         if (strcmp(key_info->name, mod_name) == 0) {
1592             key = key_info->cal_info.nKey;
1593             ALOGD("%s: Found key %d for module %s", __func__, key, mod_name);
1594             break;
1595         }
1596     }
1597     return key;
1598 
1599 int platform_set_effect_config_data(snd_device_t snd_device,
1600                                       struct audio_effect_config effect_config,
1601                                       effect_type_t effect_type)
1602 {
1603     int ret = 0;
1604 
1605     if ((snd_device < SND_DEVICE_IN_BEGIN) || (snd_device >= SND_DEVICE_MAX) ||
1606         (effect_type <= EFFECT_NONE) || (effect_type >= EFFECT_COUNT)) {
1607         ALOGE("%s: Invalid snd_device = %d or effect_type = %d",
1608             __func__, snd_device, effect_type);
1609         ret = -EINVAL;
1610         goto done;
1611     }
1612 
1613     ALOGV("%s 0x%x 0x%x 0x%x 0x%x", __func__, effect_config.module_id,
1614            effect_config.instance_id, effect_config.param_id,
1615            effect_config.param_value);
1616     effect_config_table[GET_IN_DEVICE_INDEX(snd_device)][effect_type] = effect_config;
1617 done:
1618     return ret;
1619 }
1620 
1621 int platform_get_default_app_type_v2(void *platform, usecase_type_t type, int *app_type)
1622 {
1623     ALOGV("%s: platform: %p, type: %d", __func__, platform, type);
1624     int rc = 0;
1625     if (type == PCM_CAPTURE) {
1626         *app_type = DEFAULT_APP_TYPE_TX_PATH;
1627     } else if (type == PCM_PLAYBACK) {
1628         *app_type =  DEFAULT_APP_TYPE_RX_PATH;
1629     } else {
1630         rc = -EINVAL;
1631     }
1632     return rc;
1633 }
1634 
1635 int platform_get_snd_device_acdb_id(snd_device_t snd_device)
1636 {
1637     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
1638         ALOGE("%s: Invalid snd_device = %d", __func__, snd_device);
1639         return -EINVAL;
1640     }
1641 
1642     /*
1643      * If speaker protection is enabled, function returns supported
1644      * sound device for speaker. Else same sound device is returned.
1645      */
1646     snd_device = audio_extn_get_spkr_prot_snd_device(snd_device);
1647 
1648     if (operator_specific_device_table[snd_device] != NULL)
1649         return get_operator_specific_device_acdb_id(snd_device);
1650     else
1651         return acdb_device_table[snd_device];
1652 }
1653 
1654 int platform_send_audio_calibration(void *platform, snd_device_t snd_device)
1655 {
1656     struct platform_data *my_data = (struct platform_data *)platform;
1657     int acdb_dev_id, acdb_dev_type;
1658     int sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1659 
1660     acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1661     if (acdb_dev_id < 0) {
1662         ALOGE("%s: Could not find acdb id for device(%d)",
1663               __func__, snd_device);
1664         return -EINVAL;
1665     }
1666         ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
1667               __func__, snd_device, acdb_dev_id);
1668     if (snd_device >= SND_DEVICE_OUT_BEGIN && snd_device < SND_DEVICE_OUT_END)
1669         acdb_dev_type = ACDB_DEV_TYPE_OUT;
1670     else
1671         acdb_dev_type = ACDB_DEV_TYPE_IN;
1672 
1673     if ((my_data->acdb_send_audio_cal_v3) &&
1674         (snd_device == SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP) &&
1675         !audio_extn_tfa_98xx_is_supported() ) {
1676             /* TX path calibration */
1677             my_data->acdb_send_audio_cal_v3(acdb_dev_id, ACDB_DEV_TYPE_IN,
1678                                 DEFAULT_APP_TYPE_TX_PATH, sample_rate, BUFF_IDX_0);
1679             my_data->acdb_send_audio_cal_v3(acdb_dev_id, ACDB_DEV_TYPE_OUT,
1680                                 DEFAULT_APP_TYPE_RX_PATH, sample_rate, BUFF_IDX_0);
1681     } else if ((my_data->acdb_send_audio_cal_v3) &&
1682                (snd_device == SND_DEVICE_OUT_VOICE_SPEAKER_HFP) &&
1683                !audio_extn_tfa_98xx_is_supported()) {
1684             /* RX path calibration */
1685             ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
1686                        __func__, snd_device, acdb_dev_id);
1687             my_data->acdb_send_audio_cal_v3(acdb_dev_id, ACDB_DEV_TYPE_IN,
1688                                 DEFAULT_APP_TYPE_TX_PATH, sample_rate, BUFF_IDX_1);
1689             my_data->acdb_send_audio_cal_v3(acdb_dev_id, ACDB_DEV_TYPE_OUT,
1690                                 DEFAULT_APP_TYPE_RX_PATH, sample_rate, BUFF_IDX_1);
1691     } else if (my_data->acdb_send_audio_cal) {
1692         my_data->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
1693     }
1694     return 0;
1695 }
1696 
1697 int platform_switch_voice_call_device_pre(void *platform __unused)
1698 {
1699     return 0;
1700 }
1701 
1702 int platform_switch_voice_call_enable_device_config(void *platform __unused,
1703                                                     snd_device_t out_snd_device __unused,
1704                                                     snd_device_t in_snd_device __unused)
1705 {
1706     return 0;
1707 }
1708 
1709 int platform_switch_voice_call_device_post(void *platform,
1710                                            snd_device_t out_snd_device,
1711                                            snd_device_t in_snd_device)
1712 {
1713     struct platform_data *my_data = (struct platform_data *)platform;
1714     int acdb_rx_id, acdb_tx_id;
1715 
1716     if (my_data->acdb_send_voice_cal == NULL) {
1717         ALOGE("%s: dlsym error for acdb_send_voice_call", __func__);
1718     } else {
1719         acdb_rx_id = platform_get_snd_device_acdb_id(out_snd_device);
1720         acdb_tx_id = platform_get_snd_device_acdb_id(in_snd_device);
1721 
1722         if (acdb_rx_id > 0 && acdb_tx_id > 0)
1723             my_data->acdb_send_voice_cal(acdb_rx_id, acdb_tx_id);
1724         else
1725             ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
1726                   acdb_rx_id, acdb_tx_id);
1727     }
1728 
1729     return 0;
1730 }
1731 
1732 int platform_switch_voice_call_usecase_route_post(void *platform __unused,
1733                                                   snd_device_t out_snd_device __unused,
1734                                                   snd_device_t in_snd_device __unused)
1735 {
1736     return 0;
1737 }
1738 
1739 int platform_start_voice_call(void *platform __unused, uint32_t vsid __unused)
1740 {
1741     return 0;
1742 }
1743 
1744 int platform_stop_voice_call(void *platform __unused, uint32_t vsid __unused)
1745 {
1746     return 0;
1747 }
1748 
1749 int platform_set_mic_break_det(void *platform __unused, bool enable __unused)
1750 {
1751     return 0;
1752 }
1753 
1754 int platform_get_sample_rate(void *platform __unused, uint32_t *rate __unused)
1755 {
1756     return 0;
1757 }
1758 
1759 void platform_set_speaker_gain_in_combo(struct audio_device *adev __unused,
1760                                         snd_device_t snd_device __unused,
1761                                         bool enable __unused)
1762 {
1763     return;
1764 }
1765 
1766 int platform_set_voice_volume(void *platform, int volume)
1767 {
1768     struct platform_data *my_data = (struct platform_data *)platform;
1769     struct audio_device *adev = my_data->adev;
1770     struct mixer_ctl *ctl;
1771     const char *mixer_ctl_name = "Voice Rx Gain";
1772     const char *mute_mixer_ctl_name = "Voice Rx Device Mute";
1773     int vol_index = 0, ret = 0;
1774     uint32_t set_values[ ] = {0,
1775                               ALL_SESSION_VSID,
1776                               DEFAULT_VOLUME_RAMP_DURATION_MS};
1777 
1778     // Voice volume levels are mapped to adsp volume levels as follows.
1779     // 100 -> 5, 80 -> 4, 60 -> 3, 40 -> 2, 20 -> 1  0 -> 0
1780     // But this values don't changed in kernel. So, below change is need.
1781     vol_index = (int)percent_to_index(volume, MIN_VOL_INDEX, my_data->max_vol_index);
1782     set_values[0] = vol_index;
1783     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1784     if (!ctl) {
1785         ALOGE("%s: Could not get ctl for mixer cmd - %s",
1786               __func__, mixer_ctl_name);
1787         return -EINVAL;
1788     }
1789     ALOGV("Setting voice volume index: %d", set_values[0]);
1790     ret = mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
1791 
1792     // Send mute command in case volume index is max since indexes are inverted
1793     // for mixer controls.
1794     if (vol_index == my_data->max_vol_index) {
1795         set_values[0] = 1;
1796     }
1797     else {
1798         set_values[0] = 0;
1799     }
1800 
1801     ctl = mixer_get_ctl_by_name(adev->mixer, mute_mixer_ctl_name);
1802     if (!ctl) {
1803         ALOGE("%s: Could not get ctl for mixer cmd - %s",
1804               __func__, mute_mixer_ctl_name);
1805         return -EINVAL;
1806     }
1807     ALOGV("%s: Setting RX Device Mute to: %d", __func__, set_values[0]);
1808     mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
1809     return ret;
1810 }
1811 
1812 int platform_set_mic_mute(void *platform, bool state)
1813 {
1814     struct platform_data *my_data = (struct platform_data *)platform;
1815     struct audio_device *adev = my_data->adev;
1816     struct mixer_ctl *ctl;
1817     const char *mixer_ctl_name = "Voice Tx Mute";
1818     int ret = 0;
1819     uint32_t set_values[ ] = {0,
1820                               ALL_SESSION_VSID,
1821                               DEFAULT_MUTE_RAMP_DURATION_MS};
1822 
1823     if (audio_extn_hfp_is_active(adev))
1824         mixer_ctl_name = "HFP TX Mute";
1825 
1826     set_values[0] = state;
1827     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1828     if (!ctl) {
1829         ALOGE("%s: Could not get ctl for mixer cmd - %s",
1830               __func__, mixer_ctl_name);
1831         return -EINVAL;
1832     }
1833     ALOGV("Setting voice mute state: %d", state);
1834     // "HFP TX mute" mixer control has xcount of 1.
1835     if (audio_extn_hfp_is_active(adev))
1836         ret = mixer_ctl_set_array(ctl, set_values, 1 /*count*/);
1837     else
1838         ret = mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
1839     return ret;
1840 }
1841 
1842 int platform_set_device_mute(void *platform, bool state, char *dir)
1843 {
1844     struct platform_data *my_data = (struct platform_data *)platform;
1845     struct audio_device *adev = my_data->adev;
1846     struct mixer_ctl *ctl;
1847     char *mixer_ctl_name = NULL;
1848     int ret = 0;
1849     uint32_t set_values[ ] = {0,
1850                               ALL_SESSION_VSID,
1851                               0};
1852     if(dir == NULL) {
1853         ALOGE("%s: Invalid direction:%s", __func__, dir);
1854         return -EINVAL;
1855     }
1856 
1857     if (!strncmp("rx", dir, sizeof("rx"))) {
1858         mixer_ctl_name = "Voice Rx Device Mute";
1859     } else if (!strncmp("tx", dir, sizeof("tx"))) {
1860         mixer_ctl_name = "Voice Tx Device Mute";
1861     } else {
1862         return -EINVAL;
1863     }
1864 
1865     set_values[0] = state;
1866     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1867     if (!ctl) {
1868         ALOGE("%s: Could not get ctl for mixer cmd - %s",
1869               __func__, mixer_ctl_name);
1870         return -EINVAL;
1871     }
1872 
1873     ALOGV("%s: Setting device mute state: %d, mixer ctrl:%s",
1874           __func__,state, mixer_ctl_name);
1875     mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
1876 
1877     return ret;
1878 }
1879 
1880 int platform_can_split_snd_device(snd_device_t snd_device,
1881                                    int *num_devices,
1882                                    snd_device_t *new_snd_devices)
1883 {
1884     int ret = -EINVAL;
1885 
1886     if (NULL == num_devices || NULL == new_snd_devices) {
1887         ALOGE("%s: NULL pointer ..", __func__);
1888         return -EINVAL;
1889     }
1890 
1891     /*
1892      * If wired headset/headphones/line devices share the same backend
1893      * with speaker/earpiece this routine -EINVAL.
1894      */
1895     if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES &&
1896         !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER, SND_DEVICE_OUT_HEADPHONES)) {
1897         *num_devices = 2;
1898         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
1899         new_snd_devices[1] = SND_DEVICE_OUT_HEADPHONES;
1900         ret = 0;
1901     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_LINE &&
1902                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER, SND_DEVICE_OUT_LINE)) {
1903         *num_devices = 2;
1904         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
1905         new_snd_devices[1] = SND_DEVICE_OUT_LINE;
1906         ret = 0;
1907     }
1908     return ret;
1909 }
1910 
1911 snd_device_t platform_get_output_snd_device(void *platform, audio_devices_t devices)
1912 {
1913     struct platform_data *my_data = (struct platform_data *)platform;
1914     struct audio_device *adev = my_data->adev;
1915     audio_mode_t mode = adev->mode;
1916     snd_device_t snd_device = SND_DEVICE_NONE;
1917 
1918     ALOGV("%s: enter: output devices(%#x)", __func__, devices);
1919     if (devices == AUDIO_DEVICE_NONE ||
1920         devices & AUDIO_DEVICE_BIT_IN) {
1921         ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
1922         goto exit;
1923     }
1924 
1925     if (popcount(devices) == 2) {
1926         if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
1927                         AUDIO_DEVICE_OUT_SPEAKER)) {
1928             snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
1929         } else if (devices == (AUDIO_DEVICE_OUT_LINE |
1930                                AUDIO_DEVICE_OUT_SPEAKER)) {
1931                 snd_device = SND_DEVICE_OUT_SPEAKER_AND_LINE;
1932         } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
1933                                AUDIO_DEVICE_OUT_SPEAKER)) {
1934             snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
1935         } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
1936                                AUDIO_DEVICE_OUT_SPEAKER)) {
1937             snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
1938         } else if (devices == (AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET |
1939                                AUDIO_DEVICE_OUT_SPEAKER)) {
1940             snd_device = SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET;
1941         } else if ((devices & AUDIO_DEVICE_OUT_SPEAKER) &&
1942                    (devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
1943             snd_device = SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP;
1944         } else if ((devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) &&
1945                    (devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
1946             snd_device = SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP;
1947         } else {
1948             ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
1949             goto exit;
1950         }
1951         if (snd_device != SND_DEVICE_NONE) {
1952             goto exit;
1953         }
1954     }
1955 
1956     if (popcount(devices) != 1) {
1957         ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
1958         goto exit;
1959     }
1960 
1961     if (mode == AUDIO_MODE_IN_CALL || audio_extn_hfp_is_active(adev)) {
1962         if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
1963             devices & AUDIO_DEVICE_OUT_WIRED_HEADSET ||
1964             devices & AUDIO_DEVICE_OUT_LINE) {
1965             if (adev->voice.tty_mode != TTY_MODE_OFF) {
1966                 switch (adev->voice.tty_mode) {
1967                 case TTY_MODE_FULL:
1968                     snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
1969                     break;
1970                 case TTY_MODE_VCO:
1971                     snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
1972                     break;
1973                 case TTY_MODE_HCO:
1974                     snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
1975                     break;
1976                 default:
1977                     ALOGE("%s: Invalid TTY mode (%#x)",
1978                           __func__, adev->voice.tty_mode);
1979                 }
1980             } else if (devices & AUDIO_DEVICE_OUT_LINE) {
1981                 snd_device = SND_DEVICE_OUT_VOICE_LINE;
1982             } else if (devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
1983                 snd_device = SND_DEVICE_OUT_VOICE_HEADSET;
1984             } else {
1985                 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
1986             }
1987         } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
1988             if (adev->bt_wb_speech_enabled)
1989                 snd_device = SND_DEVICE_OUT_BT_SCO_WB;
1990             else
1991                 snd_device = SND_DEVICE_OUT_BT_SCO;
1992         } else if (devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
1993                 snd_device = SND_DEVICE_OUT_BT_A2DP;
1994         } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
1995             if (audio_extn_hfp_is_active(adev))
1996                 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER_HFP;
1997             else
1998                 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
1999         } else if (devices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET ||
2000                    devices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) {
2001             snd_device = SND_DEVICE_OUT_USB_HEADSET;
2002         } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
2003                 snd_device = SND_DEVICE_OUT_VOICE_HANDSET;
2004         } else if (devices & AUDIO_DEVICE_OUT_TELEPHONY_TX)
2005             snd_device = SND_DEVICE_OUT_VOICE_TX;
2006 
2007         if (snd_device != SND_DEVICE_NONE) {
2008             goto exit;
2009         }
2010     }
2011 
2012     if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
2013         devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
2014                 snd_device = SND_DEVICE_OUT_HEADPHONES;
2015     } else if (devices & AUDIO_DEVICE_OUT_LINE) {
2016         snd_device = SND_DEVICE_OUT_LINE;
2017     } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
2018         /*
2019          * Perform device switch only if acdb tuning is different between SPEAKER & SPEAKER_REVERSE,
2020          * Or there will be a small pause while performing device switch.
2021          */
2022         if (my_data->speaker_lr_swap &&
2023             (acdb_device_table[SND_DEVICE_OUT_SPEAKER] !=
2024             acdb_device_table[SND_DEVICE_OUT_SPEAKER_REVERSE]))
2025             snd_device = SND_DEVICE_OUT_SPEAKER_REVERSE;
2026         else
2027             snd_device = SND_DEVICE_OUT_SPEAKER;
2028     } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
2029         if (adev->bt_wb_speech_enabled)
2030             snd_device = SND_DEVICE_OUT_BT_SCO_WB;
2031         else
2032             snd_device = SND_DEVICE_OUT_BT_SCO;
2033     } else if (devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
2034         snd_device = SND_DEVICE_OUT_BT_A2DP;
2035     } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
2036         snd_device = SND_DEVICE_OUT_HDMI ;
2037     } else if (devices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET ||
2038                devices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) {
2039         snd_device = SND_DEVICE_OUT_USB_HEADSET;
2040     } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
2041         snd_device = SND_DEVICE_OUT_HANDSET;
2042     } else {
2043         ALOGE("%s: Unknown device(s) %#x", __func__, devices);
2044     }
2045 exit:
2046     ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
2047     return snd_device;
2048 }
2049 
2050 #ifdef DYNAMIC_ECNS_ENABLED
2051 static snd_device_t get_snd_device_for_voice_comm(struct platform_data *my_data,
2052                                                   struct stream_in *in __unused,
2053                                                   audio_devices_t out_device,
2054                                                   audio_devices_t in_device)
2055 {
2056     struct audio_device *adev = my_data->adev;
2057     snd_device_t snd_device = SND_DEVICE_NONE;
2058 
2059     if (my_data->fluence_type != FLUENCE_NONE) {
2060         switch(AUDIO_DEVICE_BIT_IN | in_device) {
2061             case AUDIO_DEVICE_IN_BACK_MIC:
2062                 if (my_data->fluence_in_spkr_mode) {
2063                     if (my_data->fluence_type & FLUENCE_QUAD_MIC) {
2064                         snd_device = SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS;
2065                     } else if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
2066                         if (my_data->fluence_mode == FLUENCE_BROADSIDE)
2067                             snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS_BROADSIDE;
2068                         else
2069                             snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS;
2070                     }
2071                     adev->acdb_settings |= DMIC_FLAG;
2072                 } else
2073                     snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC_NS;
2074                 break;
2075             case AUDIO_DEVICE_IN_BUILTIN_MIC:
2076                 if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
2077                     snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC_NS;
2078                     adev->acdb_settings |= DMIC_FLAG;
2079                 } else
2080                     snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC_NS;
2081                 break;
2082             default:
2083                 ALOGE("%s: Unsupported in_device %#x", __func__, in_device);
2084                 break;
2085         }
2086         platform_set_echo_reference(adev, true, out_device);
2087     }
2088 
2089     return snd_device;
2090 }
2091 #else
2092 static snd_device_t get_snd_device_for_voice_comm(struct platform_data *my_data,
2093                                                   struct stream_in *in,
2094                                                   audio_devices_t out_device,
2095                                                   audio_devices_t in_device)
2096 {
2097     struct audio_device *adev = my_data->adev;
2098     snd_device_t snd_device = SND_DEVICE_NONE;
2099 
2100     if (my_data->fluence_type != FLUENCE_NONE &&
2101         in->enable_aec &&
2102         in->enable_ns) {
2103         if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
2104             if (my_data->fluence_in_spkr_mode) {
2105                 if (my_data->fluence_type & FLUENCE_QUAD_MIC) {
2106                     snd_device = SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS;
2107                 } else if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
2108                     if (my_data->fluence_mode == FLUENCE_BROADSIDE)
2109                         snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS_BROADSIDE;
2110                     else
2111                         snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS;
2112                 }
2113                 adev->acdb_settings |= DMIC_FLAG;
2114             } else
2115                 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC_NS;
2116         } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2117             if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
2118                 snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC_NS;
2119                 adev->acdb_settings |= DMIC_FLAG;
2120             } else
2121                 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC_NS;
2122         } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
2123             snd_device = SND_DEVICE_IN_HEADSET_MIC_FLUENCE;
2124         }
2125         platform_set_echo_reference(adev, true, out_device);
2126     } else if (my_data->fluence_type != FLUENCE_NONE &&
2127                in->enable_aec) {
2128         if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
2129             if (my_data->fluence_in_spkr_mode) {
2130                 if (my_data->fluence_type & FLUENCE_QUAD_MIC) {
2131                     snd_device = SND_DEVICE_IN_SPEAKER_QMIC_AEC;
2132                 } else if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
2133                     if (my_data->fluence_mode == FLUENCE_BROADSIDE)
2134                         snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC_BROADSIDE;
2135                     else
2136                         snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC;
2137                 }
2138                 adev->acdb_settings |= DMIC_FLAG;
2139             } else
2140                 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
2141         } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2142             if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
2143                 snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC;
2144                 adev->acdb_settings |= DMIC_FLAG;
2145             } else
2146                 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
2147         } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
2148             snd_device = SND_DEVICE_IN_HEADSET_MIC_FLUENCE;
2149         }
2150         platform_set_echo_reference(adev, true, out_device);
2151     } else if (my_data->fluence_type != FLUENCE_NONE &&
2152                in->enable_ns) {
2153         if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
2154             if (my_data->fluence_in_spkr_mode) {
2155                 if (my_data->fluence_type & FLUENCE_QUAD_MIC) {
2156                     snd_device = SND_DEVICE_IN_SPEAKER_QMIC_NS;
2157                 } else if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
2158                     if (my_data->fluence_mode == FLUENCE_BROADSIDE)
2159                         snd_device = SND_DEVICE_IN_SPEAKER_DMIC_NS_BROADSIDE;
2160                     else
2161                         snd_device = SND_DEVICE_IN_SPEAKER_DMIC_NS;
2162                 }
2163                 adev->acdb_settings |= DMIC_FLAG;
2164             } else
2165                 snd_device = SND_DEVICE_IN_SPEAKER_MIC_NS;
2166         } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2167             if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
2168                 snd_device = SND_DEVICE_IN_HANDSET_DMIC_NS;
2169                 adev->acdb_settings |= DMIC_FLAG;
2170             } else
2171                 snd_device = SND_DEVICE_IN_HANDSET_MIC_NS;
2172         } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
2173             snd_device = SND_DEVICE_IN_HEADSET_MIC_FLUENCE;
2174         }
2175         platform_set_echo_reference(adev, false, out_device);
2176     } else
2177         platform_set_echo_reference(adev, false, out_device);
2178 
2179     return snd_device;
2180 }
2181 #endif //DYNAMIC_ECNS_ENABLED
2182 
2183 snd_device_t platform_get_input_snd_device(void *platform,
2184                                            struct stream_in *in,
2185                                            audio_devices_t out_device)
2186 {
2187     struct platform_data *my_data = (struct platform_data *)platform;
2188     struct audio_device *adev = my_data->adev;
2189     audio_mode_t mode = adev->mode;
2190     snd_device_t snd_device = SND_DEVICE_NONE;
2191 
2192     if (in == NULL) {
2193         in = adev_get_active_input(adev);
2194     }
2195 
2196     audio_source_t source = (in == NULL) ? AUDIO_SOURCE_DEFAULT : in->source;
2197     audio_devices_t in_device =
2198         ((in == NULL) ? AUDIO_DEVICE_NONE : in->device) & ~AUDIO_DEVICE_BIT_IN;
2199     audio_channel_mask_t channel_mask = (in == NULL) ? AUDIO_CHANNEL_IN_MONO : in->channel_mask;
2200     int channel_count = audio_channel_count_from_in_mask(channel_mask);
2201 
2202     ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
2203           __func__, out_device, in_device);
2204 
2205     if ((out_device != AUDIO_DEVICE_NONE) && ((mode == AUDIO_MODE_IN_CALL) ||
2206         audio_extn_hfp_is_active(adev))) {
2207         if (adev->voice.tty_mode != TTY_MODE_OFF) {
2208             if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
2209                 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET ||
2210                 out_device & AUDIO_DEVICE_OUT_LINE) {
2211                 switch (adev->voice.tty_mode) {
2212                 case TTY_MODE_FULL:
2213                     snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
2214                     break;
2215                 case TTY_MODE_VCO:
2216                     snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
2217                     break;
2218                 case TTY_MODE_HCO:
2219                     snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
2220                     break;
2221                 default:
2222                     ALOGE("%s: Invalid TTY mode (%#x)",
2223                           __func__, adev->voice.tty_mode);
2224                 }
2225                 goto exit;
2226             }
2227         }
2228         if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
2229             out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
2230             out_device & AUDIO_DEVICE_OUT_LINE) {
2231             if (my_data->fluence_type == FLUENCE_NONE ||
2232                 my_data->fluence_in_voice_call == false) {
2233                 snd_device = SND_DEVICE_IN_HANDSET_MIC;
2234                 if (audio_extn_hfp_is_active(adev))
2235                     platform_set_echo_reference(adev, true, out_device);
2236             } else {
2237                 snd_device = SND_DEVICE_IN_VOICE_DMIC;
2238                 adev->acdb_settings |= DMIC_FLAG;
2239             }
2240         } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
2241             snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
2242             if (audio_extn_hfp_is_active(adev))
2243                 platform_set_echo_reference(adev, true, out_device);
2244         } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
2245             if (adev->bt_wb_speech_enabled) {
2246                 if (adev->bluetooth_nrec)
2247                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
2248                 else
2249                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
2250             } else {
2251                 if (adev->bluetooth_nrec)
2252                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
2253                 else
2254                     snd_device = SND_DEVICE_IN_BT_SCO_MIC;
2255             }
2256         } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
2257             if (my_data->fluence_type != FLUENCE_NONE &&
2258                 my_data->fluence_in_voice_call &&
2259                 my_data->fluence_in_spkr_mode) {
2260                     if(my_data->fluence_type & FLUENCE_QUAD_MIC) {
2261                        adev->acdb_settings |= QMIC_FLAG;
2262                        snd_device = SND_DEVICE_IN_VOICE_SPEAKER_QMIC;
2263                     } else {
2264                        adev->acdb_settings |= DMIC_FLAG;
2265                        if (my_data->fluence_mode == FLUENCE_BROADSIDE)
2266                            snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BROADSIDE;
2267                        else
2268                            snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC;
2269                     }
2270             } else {
2271                 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
2272                 if (audio_extn_hfp_is_active(adev)) {
2273                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP;
2274                     platform_set_echo_reference(adev, true, out_device);
2275                 } else {
2276                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
2277                 }
2278             }
2279         } else if (out_device & AUDIO_DEVICE_OUT_TELEPHONY_TX)
2280             snd_device = SND_DEVICE_IN_VOICE_RX;
2281     } else if (source == AUDIO_SOURCE_CAMCORDER) {
2282         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
2283             in_device & AUDIO_DEVICE_IN_BACK_MIC) {
2284             if (my_data->fluence_type & FLUENCE_DUAL_MIC &&
2285                 channel_count == 2)
2286                 snd_device = SND_DEVICE_IN_HANDSET_STEREO_DMIC;
2287             else
2288                 snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
2289         }
2290     } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
2291         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2292             if (channel_count == 2) {
2293                 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_STEREO;
2294                 adev->acdb_settings |= DMIC_FLAG;
2295             } else if (in->enable_ns)
2296                 snd_device = SND_DEVICE_IN_VOICE_REC_MIC_NS;
2297             else if (my_data->fluence_type != FLUENCE_NONE &&
2298                      my_data->fluence_in_voice_rec) {
2299                 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE;
2300                 adev->acdb_settings |= DMIC_FLAG;
2301             } else {
2302                 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
2303             }
2304         }
2305     } else if ((source == AUDIO_SOURCE_VOICE_COMMUNICATION) ||
2306               (mode == AUDIO_MODE_IN_COMMUNICATION)) {
2307         if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
2308             in_device = AUDIO_DEVICE_IN_BACK_MIC;
2309 
2310         if (in) {
2311             snd_device = get_snd_device_for_voice_comm(my_data, in,
2312                                                        out_device, in_device);
2313         }
2314     } else if (source == AUDIO_SOURCE_FM_TUNER) {
2315         snd_device = SND_DEVICE_IN_CAPTURE_FM;
2316     } else if (source == AUDIO_SOURCE_DEFAULT) {
2317         goto exit;
2318     }
2319 
2320 
2321     if (snd_device != SND_DEVICE_NONE) {
2322         goto exit;
2323     }
2324 
2325     if (in_device != AUDIO_DEVICE_NONE &&
2326             !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
2327             !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
2328         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2329             if (my_data->fluence_type & (FLUENCE_DUAL_MIC | FLUENCE_QUAD_MIC) &&
2330                     channel_count == 2)
2331                 snd_device = SND_DEVICE_IN_HANDSET_STEREO_DMIC;
2332             else
2333                 snd_device = SND_DEVICE_IN_HANDSET_MIC;
2334         } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
2335             snd_device = SND_DEVICE_IN_SPEAKER_MIC;
2336         } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
2337             snd_device = SND_DEVICE_IN_HEADSET_MIC;
2338         } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
2339             if (adev->bt_wb_speech_enabled) {
2340                 if (adev->bluetooth_nrec)
2341                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
2342                 else
2343                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
2344             } else {
2345                 if (adev->bluetooth_nrec)
2346                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
2347                 else
2348                     snd_device = SND_DEVICE_IN_BT_SCO_MIC;
2349             }
2350         } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
2351             snd_device = SND_DEVICE_IN_HDMI_MIC;
2352         } else if (in_device & AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET ||
2353                    in_device & AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET) {
2354             snd_device = SND_DEVICE_IN_USB_HEADSET_MIC;
2355         } else if (in_device & AUDIO_DEVICE_IN_FM_TUNER) {
2356             snd_device = SND_DEVICE_IN_CAPTURE_FM;
2357         } else {
2358             ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
2359             ALOGW("%s: Using default handset-mic", __func__);
2360             snd_device = SND_DEVICE_IN_HANDSET_MIC;
2361         }
2362     } else {
2363         if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
2364             snd_device = SND_DEVICE_IN_HANDSET_MIC;
2365         } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
2366             snd_device = SND_DEVICE_IN_HEADSET_MIC;
2367         } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
2368             if (channel_count > 1)
2369                 snd_device = SND_DEVICE_IN_SPEAKER_STEREO_DMIC;
2370             else
2371                 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
2372         } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
2373                        out_device & AUDIO_DEVICE_OUT_LINE) {
2374             snd_device = SND_DEVICE_IN_HANDSET_MIC;
2375         } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
2376             if (adev->bt_wb_speech_enabled) {
2377                 if (adev->bluetooth_nrec)
2378                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
2379                 else
2380                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
2381             } else {
2382                 if (adev->bluetooth_nrec)
2383                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
2384                 else
2385                     snd_device = SND_DEVICE_IN_BT_SCO_MIC;
2386             }
2387         } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
2388             snd_device = SND_DEVICE_IN_HDMI_MIC;
2389         } else if (out_device & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET ||
2390                    out_device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) {
2391             snd_device = SND_DEVICE_IN_USB_HEADSET_MIC;
2392         } else {
2393             ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
2394             ALOGW("%s: Using default handset-mic", __func__);
2395             snd_device = SND_DEVICE_IN_HANDSET_MIC;
2396         }
2397     }
2398 exit:
2399     ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
2400     return snd_device;
2401 }
2402 
2403 int platform_set_hdmi_channels(void *platform,  int channel_count)
2404 {
2405     struct platform_data *my_data = (struct platform_data *)platform;
2406     struct audio_device *adev = my_data->adev;
2407     struct mixer_ctl *ctl;
2408     const char *channel_cnt_str = NULL;
2409     const char *mixer_ctl_name = "HDMI_RX Channels";
2410     switch (channel_count) {
2411     case 8:
2412         channel_cnt_str = "Eight"; break;
2413     case 7:
2414         channel_cnt_str = "Seven"; break;
2415     case 6:
2416         channel_cnt_str = "Six"; break;
2417     case 5:
2418         channel_cnt_str = "Five"; break;
2419     case 4:
2420         channel_cnt_str = "Four"; break;
2421     case 3:
2422         channel_cnt_str = "Three"; break;
2423     default:
2424         channel_cnt_str = "Two"; break;
2425     }
2426     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2427     if (!ctl) {
2428         ALOGE("%s: Could not get ctl for mixer cmd - %s",
2429               __func__, mixer_ctl_name);
2430         return -EINVAL;
2431     }
2432     ALOGV("HDMI channel count: %s", channel_cnt_str);
2433     mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
2434     return 0;
2435 }
2436 
2437 int platform_edid_get_max_channels(void *platform)
2438 {
2439     struct platform_data *my_data = (struct platform_data *)platform;
2440     struct audio_device *adev = my_data->adev;
2441     char block[MAX_SAD_BLOCKS * SAD_BLOCK_SIZE];
2442     char *sad = block;
2443     int num_audio_blocks;
2444     int channel_count;
2445     int max_channels = 0;
2446     int i, ret, count;
2447 
2448     struct mixer_ctl *ctl;
2449 
2450     ctl = mixer_get_ctl_by_name(adev->mixer, AUDIO_DATA_BLOCK_MIXER_CTL);
2451     if (!ctl) {
2452         ALOGE("%s: Could not get ctl for mixer cmd - %s",
2453               __func__, AUDIO_DATA_BLOCK_MIXER_CTL);
2454         return 0;
2455     }
2456 
2457     mixer_ctl_update(ctl);
2458 
2459     count = mixer_ctl_get_num_values(ctl);
2460 
2461     /* Read SAD blocks, clamping the maximum size for safety */
2462     if (count > (int)sizeof(block))
2463         count = (int)sizeof(block);
2464 
2465     ret = mixer_ctl_get_array(ctl, block, count);
2466     if (ret != 0) {
2467         ALOGE("%s: mixer_ctl_get_array() failed to get EDID info", __func__);
2468         return 0;
2469     }
2470 
2471     /* Calculate the number of SAD blocks */
2472     num_audio_blocks = count / SAD_BLOCK_SIZE;
2473 
2474     for (i = 0; i < num_audio_blocks; i++) {
2475         /* Only consider LPCM blocks */
2476         if ((sad[0] >> 3) != EDID_FORMAT_LPCM) {
2477             sad += 3;
2478             continue;
2479         }
2480 
2481         channel_count = (sad[0] & 0x7) + 1;
2482         if (channel_count > max_channels)
2483             max_channels = channel_count;
2484 
2485         /* Advance to next block */
2486         sad += 3;
2487     }
2488 
2489     return max_channels;
2490 }
2491 
2492 int platform_set_incall_recording_session_id(void *platform,
2493                                              uint32_t session_id,
2494                                              int rec_mode __unused)
2495 {
2496     int ret = 0;
2497     struct platform_data *my_data = (struct platform_data *)platform;
2498     struct audio_device *adev = my_data->adev;
2499     struct mixer_ctl *ctl;
2500     const char *mixer_ctl_name = "Voc VSID";
2501     int num_ctl_values;
2502     int i;
2503 
2504     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2505     if (!ctl) {
2506         ALOGE("%s: Could not get ctl for mixer cmd - %s",
2507               __func__, mixer_ctl_name);
2508         ret = -EINVAL;
2509     } else {
2510         num_ctl_values = mixer_ctl_get_num_values(ctl);
2511         for (i = 0; i < num_ctl_values; i++) {
2512             if (mixer_ctl_set_value(ctl, i, session_id)) {
2513                 ALOGV("Error: invalid session_id: %x", session_id);
2514                 ret = -EINVAL;
2515                 break;
2516             }
2517         }
2518     }
2519     return ret;
2520 }
2521 
2522 int platform_set_incall_recording_session_channels(void *platform __unused,
2523                                              uint32_t channel_count __unused)
2524 {
2525     return 0;
2526 }
2527 
2528 
2529 int platform_stop_incall_recording_usecase(void *platform __unused)
2530 {
2531     return 0;
2532 }
2533 
2534 int platform_start_incall_music_usecase(void *platform __unused)
2535 {
2536     return 0;
2537 }
2538 
2539 int platform_stop_incall_music_usecase(void *platform __unused)
2540 {
2541     return 0;
2542 }
2543 
2544 int platform_set_parameters(void *platform, struct str_parms *parms)
2545 {
2546     struct platform_data *my_data = (struct platform_data *)platform;
2547     char value[128];
2548     char *kv_pairs = str_parms_to_str(parms);
2549     int ret = 0, err;
2550 
2551     if (kv_pairs == NULL) {
2552         ret = -EINVAL;
2553         ALOGE("%s: key-value pair is NULL",__func__);
2554         goto done;
2555     }
2556 
2557     ALOGV("%s: enter: %s", __func__, kv_pairs);
2558 
2559     err = str_parms_get_str(parms, PLATFORM_CONFIG_KEY_OPERATOR_INFO,
2560                             value, sizeof(value));
2561     if (err >= 0) {
2562         struct operator_info *info;
2563         char *str = value;
2564         char *name;
2565 
2566         str_parms_del(parms, PLATFORM_CONFIG_KEY_OPERATOR_INFO);
2567         info = (struct operator_info *)calloc(1, sizeof(struct operator_info));
2568         name = strtok(str, ";");
2569         info->name = strdup(name);
2570         info->mccmnc = strdup(str + strlen(name) + 1);
2571 
2572         list_add_tail(&operator_info_list, &info->list);
2573         ALOGV("%s: add operator[%s] mccmnc[%s]", __func__, info->name, info->mccmnc);
2574     }
2575 
2576     audio_extn_hfp_set_parameters(my_data->adev, parms);
2577 done:
2578     ALOGV("%s: exit with code(%d)", __func__, ret);
2579     if (kv_pairs != NULL)
2580         free(kv_pairs);
2581 
2582     return ret;
2583 }
2584 
2585 void platform_set_audio_source_delay(audio_source_t audio_source, int delay_ms)
2586 {
2587     if ((audio_source < AUDIO_SOURCE_DEFAULT) ||
2588            (audio_source > AUDIO_SOURCE_MAX)) {
2589         ALOGE("%s: Invalid audio_source = %d", __func__, audio_source);
2590         return;
2591     }
2592 
2593     audio_source_delay_ms[audio_source] = delay_ms;
2594 }
2595 
2596 /* Delay in Us */
2597 int64_t platform_get_audio_source_delay(audio_source_t audio_source)
2598 {
2599     if ((audio_source < AUDIO_SOURCE_DEFAULT) ||
2600             (audio_source > AUDIO_SOURCE_MAX)) {
2601         ALOGE("%s: Invalid audio_source = %d", __func__, audio_source);
2602         return 0;
2603     }
2604 
2605     return 1000LL * audio_source_delay_ms[audio_source];
2606 }
2607 
2608 void platform_set_audio_usecase_delay(audio_usecase_t usecase, int delay_ms)
2609 {
2610     if ((usecase <= USECASE_INVALID) || (usecase >= AUDIO_USECASE_MAX)) {
2611         ALOGE("%s: invalid usecase case idx %d", __func__, usecase);
2612         return;
2613     }
2614 
2615     audio_usecase_delay_ms[usecase] = delay_ms;
2616 }
2617 
2618 /* Delay in Us */
2619 int64_t platform_get_audio_usecase_delay(audio_usecase_t usecase)
2620 {
2621     if ((usecase <= USECASE_INVALID) || (usecase >= AUDIO_USECASE_MAX)) {
2622         ALOGE("%s: invalid usecase case idx %d", __func__, usecase);
2623         return 0;
2624     }
2625 
2626     return 1000LL *  audio_usecase_delay_ms[usecase] ;
2627 }
2628 
2629 /* Delay in Us */
2630 int64_t platform_render_latency(struct stream_out *out)
2631 {
2632     int64_t delay = 0LL;
2633 
2634     if (!out)
2635         return delay;
2636 
2637     switch (out->usecase) {
2638         case USECASE_AUDIO_PLAYBACK_DEEP_BUFFER:
2639             delay = DEEP_BUFFER_PLATFORM_DELAY;
2640             break;
2641         case USECASE_AUDIO_PLAYBACK_LOW_LATENCY:
2642             delay = LOW_LATENCY_PLATFORM_DELAY;
2643             break;
2644         default:
2645             break;
2646     }
2647 
2648 /* out->usecase could be used to add delay time if it's necessary */
2649     delay += platform_get_audio_usecase_delay(out->usecase);
2650     return delay;
2651 }
2652 
2653 int64_t platform_capture_latency(struct stream_in *in)
2654 {
2655     int64_t delay = 0LL;
2656 
2657     if (!in)
2658         return delay;
2659 
2660     delay = platform_get_audio_source_delay(in->source);
2661 
2662 /* in->device could be used to add delay time if it's necessary */
2663     return delay;
2664 }
2665 
2666 int platform_set_snd_device_backend(snd_device_t device, const char *backend, const char * hw_interface)
2667 {
2668     int ret = 0;
2669 
2670     if ((device < SND_DEVICE_MIN) || (device >= SND_DEVICE_MAX)) {
2671         ALOGE("%s: Invalid snd_device = %d",
2672             __func__, device);
2673         ret = -EINVAL;
2674         goto done;
2675     }
2676 
2677     ALOGV("%s: backend_tag_table[%s]: old = %s new = %s", __func__,
2678           platform_get_snd_device_name(device),
2679           backend_table[device] != NULL ? backend_table[device]: "null", backend);
2680     if (backend_table[device]) {
2681         free(backend_table[device]);
2682     }
2683     backend_table[device] = strdup(backend);
2684 
2685     if (hw_interface != NULL) {
2686         if (hw_interface_table[device])
2687             free(hw_interface_table[device]);
2688         ALOGV("%s: hw_interface_table[%d] = %s", __func__, device, hw_interface);
2689         hw_interface_table[device] = strdup(hw_interface);
2690     }
2691 done:
2692     return ret;
2693 }
2694 
2695 int platform_set_usecase_pcm_id(audio_usecase_t usecase, int32_t type, int32_t pcm_id)
2696 {
2697     int ret = 0;
2698     if ((usecase <= USECASE_INVALID) || (usecase >= AUDIO_USECASE_MAX)) {
2699         ALOGE("%s: invalid usecase case idx %d", __func__, usecase);
2700         ret = -EINVAL;
2701         goto done;
2702     }
2703 
2704     if ((type != 0) && (type != 1)) {
2705         ALOGE("%s: invalid usecase type", __func__);
2706         ret = -EINVAL;
2707     }
2708     pcm_device_table[usecase][type] = pcm_id;
2709 done:
2710     return ret;
2711 }
2712 
2713 #define DEFAULT_NOMINAL_SPEAKER_GAIN 20
2714 int ramp_speaker_gain(struct audio_device *adev, bool ramp_up, int target_ramp_up_gain) {
2715     // backup_gain: gain to try to set in case of an error during ramp
2716     int start_gain, end_gain, step, backup_gain, i;
2717     bool error = false;
2718     const struct mixer_ctl *ctl;
2719     const char *mixer_ctl_name_gain_left = "Left Speaker Gain";
2720     const char *mixer_ctl_name_gain_right = "Right Speaker Gain";
2721     struct mixer_ctl *ctl_left = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name_gain_left);
2722     struct mixer_ctl *ctl_right = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name_gain_right);
2723     if (!ctl_left || !ctl_right) {
2724         ALOGE("%s: Could not get ctl for mixer cmd - %s or %s, not applying speaker gain ramp",
2725                       __func__, mixer_ctl_name_gain_left, mixer_ctl_name_gain_right);
2726         return -EINVAL;
2727     } else if ((mixer_ctl_get_num_values(ctl_left) != 1)
2728             || (mixer_ctl_get_num_values(ctl_right) != 1)) {
2729         ALOGE("%s: Unexpected num values for mixer cmd - %s or %s, not applying speaker gain ramp",
2730                               __func__, mixer_ctl_name_gain_left, mixer_ctl_name_gain_right);
2731         return -EINVAL;
2732     }
2733     if (ramp_up) {
2734         start_gain = 0;
2735         end_gain = target_ramp_up_gain > 0 ? target_ramp_up_gain : DEFAULT_NOMINAL_SPEAKER_GAIN;
2736         step = +1;
2737         backup_gain = end_gain;
2738     } else {
2739         // using same gain on left and right
2740         const int left_gain = mixer_ctl_get_value(ctl_left, 0);
2741         start_gain = left_gain > 0 ? left_gain : DEFAULT_NOMINAL_SPEAKER_GAIN;
2742         end_gain = 0;
2743         step = -1;
2744         backup_gain = start_gain;
2745     }
2746     for (i = start_gain ; i != (end_gain + step) ; i += step) {
2747         //ALOGV("setting speaker gain to %d", i);
2748         if (mixer_ctl_set_value(ctl_left, 0, i)) {
2749             ALOGE("%s: error setting %s to %d during gain ramp",
2750                     __func__, mixer_ctl_name_gain_left, i);
2751             error = true;
2752             break;
2753         }
2754         if (mixer_ctl_set_value(ctl_right, 0, i)) {
2755             ALOGE("%s: error setting %s to %d during gain ramp",
2756                     __func__, mixer_ctl_name_gain_right, i);
2757             error = true;
2758             break;
2759         }
2760         usleep(1000);
2761     }
2762     if (error) {
2763         // an error occured during the ramp, let's still try to go back to a safe volume
2764         if (mixer_ctl_set_value(ctl_left, 0, backup_gain)) {
2765             ALOGE("%s: error restoring left gain to %d", __func__, backup_gain);
2766         }
2767         if (mixer_ctl_set_value(ctl_right, 0, backup_gain)) {
2768             ALOGE("%s: error restoring right gain to %d", __func__, backup_gain);
2769         }
2770     }
2771     return start_gain;
2772 }
2773 
2774 int platform_set_swap_mixer(struct audio_device *adev, bool swap_channels)
2775 {
2776     const char *mixer_ctl_name = "Swap channel";
2777     struct mixer_ctl *ctl;
2778     const char *mixer_path;
2779     struct platform_data *my_data = (struct platform_data *)adev->platform;
2780 
2781     // forced to set to swap, but device not rotated ... ignore set
2782     if (swap_channels && !my_data->speaker_lr_swap)
2783         return 0;
2784 
2785     ALOGV("%s:", __func__);
2786 
2787     if (swap_channels) {
2788         mixer_path = platform_get_snd_device_name(SND_DEVICE_OUT_SPEAKER_REVERSE);
2789         audio_route_apply_and_update_path(adev->audio_route, mixer_path);
2790     } else {
2791         mixer_path = platform_get_snd_device_name(SND_DEVICE_OUT_SPEAKER);
2792         audio_route_apply_and_update_path(adev->audio_route, mixer_path);
2793     }
2794 
2795     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2796     if (!ctl) {
2797         ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
2798         return -EINVAL;
2799     }
2800 
2801     if (mixer_ctl_set_value(ctl, 0, swap_channels) < 0) {
2802         ALOGE("%s: Could not set reverse cotrol %d",__func__, swap_channels);
2803         return -EINVAL;
2804     }
2805 
2806     ALOGV("platfor_force_swap_channel :: Channel orientation ( %s ) ",
2807            swap_channels?"R --> L":"L --> R");
2808 
2809     return 0;
2810 }
2811 
2812 int platform_check_and_set_swap_lr_channels(struct audio_device *adev, bool swap_channels)
2813 {
2814     // only update if there is active pcm playback on speaker
2815     struct audio_usecase *usecase;
2816     struct listnode *node;
2817     struct platform_data *my_data = (struct platform_data *)adev->platform;
2818 
2819     my_data->speaker_lr_swap = swap_channels;
2820 
2821     return platform_set_swap_channels(adev, swap_channels);
2822 }
2823 
2824 int platform_set_swap_channels(struct audio_device *adev, bool swap_channels)
2825 {
2826     // only update if there is active pcm playback on speaker
2827     struct audio_usecase *usecase;
2828     struct listnode *node;
2829     struct platform_data *my_data = (struct platform_data *)adev->platform;
2830 
2831     // do not swap channels in audio modes with concurrent capture and playback
2832     // as this may break the echo reference
2833     if ((adev->mode == AUDIO_MODE_IN_COMMUNICATION) || (adev->mode == AUDIO_MODE_IN_CALL)) {
2834         ALOGV("%s: will not swap due to audio mode %d", __func__, adev->mode);
2835         return 0;
2836     }
2837 
2838     list_for_each(node, &adev->usecase_list) {
2839         usecase = node_to_item(node, struct audio_usecase, list);
2840         if (usecase->type == PCM_PLAYBACK &&
2841                 usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
2842             /*
2843              * If acdb tuning is different for SPEAKER_REVERSE, it is must
2844              * to perform device switch to disable the current backend to
2845              * enable it with new acdb data.
2846              */
2847             if (acdb_device_table[SND_DEVICE_OUT_SPEAKER] !=
2848                 acdb_device_table[SND_DEVICE_OUT_SPEAKER_REVERSE]) {
2849                 const int initial_skpr_gain = ramp_speaker_gain(adev, false /*ramp_up*/, -1);
2850                 select_devices(adev, usecase->id);
2851                 if (initial_skpr_gain != -EINVAL)
2852                     ramp_speaker_gain(adev, true /*ramp_up*/, initial_skpr_gain);
2853 
2854             } else {
2855                 platform_set_swap_mixer(adev, swap_channels);
2856             }
2857             break;
2858         }
2859     }
2860 
2861     return 0;
2862 }
2863 
2864 int platform_snd_card_update(void *platform __unused,
2865                              card_status_t status __unused)
2866 {
2867     return -1;
2868 }
2869 
2870 int platform_send_audio_calibration_v2(void *platform __unused,
2871                                        struct audio_usecase *usecase __unused,
2872                                        int app_type __unused,
2873                                        int sample_rate __unused)
2874 {
2875     return -ENOSYS;
2876 }
2877 
2878 void platform_check_and_update_copp_sample_rate(void* platform __unused,
2879                                                snd_device_t snd_device __unused,
2880                                                 unsigned int stream_sr __unused,
2881                                                 int* sample_rate __unused)
2882 {
2883 
2884 }
2885 
2886 int platform_get_snd_device_backend_index(snd_device_t snd_device __unused)
2887 {
2888     return -ENOSYS;
2889 }
2890 
2891 bool platform_supports_app_type_cfg() { return false; }
2892 
2893 void platform_add_app_type(const char *uc_type __unused,
2894                            const char *mode __unused,
2895                            int bw __unused, int app_type __unused,
2896                            int max_sr __unused) {}
2897 
2898 int platform_get_app_type_v2(void *platform __unused,
2899                              enum usecase_type_t type __unused,
2900                              const char *mode __unused,
2901                              int bw __unused, int sr __unused,
2902                              int *app_type __unused) {
2903     return -ENOSYS;
2904 }
2905 
2906 int platform_set_sidetone(struct audio_device *adev,
2907                           snd_device_t out_snd_device,
2908                           bool enable, char *str)
2909 {
2910     int ret;
2911     if (out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
2912         out_snd_device == SND_DEVICE_OUT_VOICE_USB_HEADSET) {
2913             ret = audio_extn_usb_enable_sidetone(out_snd_device, enable);
2914             if (ret)
2915                 ALOGI("%s: usb device %d does not support device sidetone\n",
2916                   __func__, out_snd_device);
2917     } else {
2918         ALOGV("%s: sidetone out device(%d) mixer cmd = %s\n",
2919               __func__, out_snd_device, str);
2920 
2921         if (enable)
2922             audio_route_apply_and_update_path(adev->audio_route, str);
2923         else
2924             audio_route_reset_and_update_path(adev->audio_route, str);
2925     }
2926     return 0;
2927 }
2928 
2929 int platform_get_mmap_data_fd(void *platform __unused, int fe_dev __unused, int dir __unused,
2930                               int *fd __unused, uint32_t *size __unused)
2931 {
2932     return -ENOSYS;
2933 }
2934 
2935 bool platform_sound_trigger_usecase_needs_event(audio_usecase_t uc_id __unused)
2936 {
2937     return false;
2938 }
2939 
2940 bool platform_snd_device_has_speaker(snd_device_t dev __unused) {
2941     return false;
2942 }
2943 
2944 bool platform_set_microphone_characteristic(void *platform __unused,
2945                                             struct audio_microphone_characteristic_t mic __unused) {
2946     return -ENOSYS;
2947 }
2948 
2949 int platform_get_microphones(void *platform __unused,
2950                              struct audio_microphone_characteristic_t *mic_array __unused,
2951                              size_t *mic_count __unused) {
2952     return -ENOSYS;
2953 }
2954 
2955 bool platform_set_microphone_map(void *platform __unused, snd_device_t in_snd_device __unused,
2956                                  const struct mic_info *info __unused) {
2957     return false;
2958 }
2959 
2960 int platform_get_active_microphones(void *platform __unused, unsigned int channels __unused,
2961                                     audio_usecase_t usecase __unused,
2962                                     struct audio_microphone_characteristic_t *mic_array __unused,
2963                                     size_t *mic_count __unused) {
2964     return -ENOSYS;
2965 }
2966 
2967 int platform_set_usb_service_interval(void *platform __unused,
2968                                       bool playback __unused,
2969                                       unsigned long service_interval __unused,
2970                                       bool *reconfig)
2971 {
2972     *reconfig = false;
2973     return 0;
2974 }
2975 
2976 int platform_set_backend_cfg(const struct audio_device* adev __unused,
2977                              snd_device_t snd_device __unused,
2978                              const struct audio_backend_cfg *backend_cfg __unused)
2979 {
2980     return -1;
2981 }
2982