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