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