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