• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013-2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "msm8960_platform"
18 /*#define LOG_NDEBUG 0*/
19 #define LOG_NDDEBUG 0
20 
21 #include <stdlib.h>
22 #include <dlfcn.h>
23 #include <log/log.h>
24 #include <cutils/properties.h>
25 #include <audio_hw.h>
26 #include <platform_api.h>
27 #include "platform.h"
28 #include "audio_extn.h"
29 
30 #define LIB_ACDB_LOADER "libacdbloader.so"
31 #define LIB_CSD_CLIENT "libcsd-client.so"
32 
33 #define DUALMIC_CONFIG_NONE 0      /* Target does not contain 2 mics */
34 #define DUALMIC_CONFIG_ENDFIRE 1
35 #define DUALMIC_CONFIG_BROADSIDE 2
36 
37 /*
38  * This is the sysfs path for the HDMI audio data block
39  */
40 #define AUDIO_DATA_BLOCK_PATH "/sys/class/graphics/fb1/audio_data_block"
41 #define MIXER_XML_PATH "/system/etc/mixer_paths.xml"
42 
43 /*
44  * This file will have a maximum of 38 bytes:
45  *
46  * 4 bytes: number of audio blocks
47  * 4 bytes: total length of Short Audio Descriptor (SAD) blocks
48  * Maximum 10 * 3 bytes: SAD blocks
49  */
50 #define MAX_SAD_BLOCKS      10
51 #define SAD_BLOCK_SIZE      3
52 
53 /* EDID format ID for LPCM audio */
54 #define EDID_FORMAT_LPCM    1
55 
56 struct audio_block_header
57 {
58     int reserved;
59     int length;
60 };
61 
62 
63 typedef void (*acdb_deallocate_t)();
64 typedef int  (*acdb_init_t)();
65 typedef void (*acdb_send_audio_cal_t)(int, int);
66 typedef void (*acdb_send_voice_cal_t)(int, int);
67 
68 typedef int (*csd_client_init_t)();
69 typedef int (*csd_client_deinit_t)();
70 typedef int (*csd_disable_device_t)();
71 typedef int (*csd_enable_device_t)(int, int, uint32_t);
72 typedef int (*csd_volume_t)(int);
73 typedef int (*csd_mic_mute_t)(int);
74 typedef int (*csd_start_voice_t)();
75 typedef int (*csd_stop_voice_t)();
76 
77 
78 /* Audio calibration related functions */
79 struct platform_data {
80     struct audio_device *adev;
81     bool fluence_in_spkr_mode;
82     bool fluence_in_voice_call;
83     bool fluence_in_voice_rec;
84     int  dualmic_config;
85     bool speaker_lr_swap;
86 
87     void *acdb_handle;
88     acdb_init_t acdb_init;
89     acdb_deallocate_t acdb_deallocate;
90     acdb_send_audio_cal_t acdb_send_audio_cal;
91     acdb_send_voice_cal_t acdb_send_voice_cal;
92 
93     /* CSD Client related functions for voice call */
94     void *csd_client;
95     csd_client_init_t csd_client_init;
96     csd_client_deinit_t csd_client_deinit;
97     csd_disable_device_t csd_disable_device;
98     csd_enable_device_t csd_enable_device;
99     csd_volume_t csd_volume;
100     csd_mic_mute_t csd_mic_mute;
101     csd_start_voice_t csd_start_voice;
102     csd_stop_voice_t csd_stop_voice;
103 };
104 
105 static const int pcm_device_table[AUDIO_USECASE_MAX][2] = {
106     [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
107     [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {14, 14},
108     [USECASE_AUDIO_PLAYBACK_HIFI] = {1, 1},
109     [USECASE_AUDIO_RECORD] = {0, 0},
110     [USECASE_AUDIO_RECORD_LOW_LATENCY] = {14, 14},
111     [USECASE_VOICE_CALL] = {12, 12},
112 };
113 
114 /* Array to store sound devices */
115 static const char * const device_table[SND_DEVICE_MAX] = {
116     [SND_DEVICE_NONE] = "none",
117     /* Playback sound devices */
118     [SND_DEVICE_OUT_HANDSET] = "handset",
119     [SND_DEVICE_OUT_SPEAKER] = "speaker",
120     [SND_DEVICE_OUT_SPEAKER_REVERSE] = "speaker-reverse",
121     [SND_DEVICE_OUT_SPEAKER_SAFE] = "speaker-safe",
122     [SND_DEVICE_OUT_HEADPHONES] = "headphones",
123     [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
124     [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
125     [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
126     [SND_DEVICE_OUT_VOICE_HEADSET] = "voice-headphones",
127     [SND_DEVICE_OUT_HDMI] = "hdmi",
128     [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
129     [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
130     [SND_DEVICE_OUT_BT_SCO_WB] = "bt-sco-headset-wb",
131     [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = "voice-handset-tmus",
132     [SND_DEVICE_OUT_VOICE_HANDSET] = "voice-handset-tmus",
133     [SND_DEVICE_OUT_VOICE_HAC_HANDSET] = "voice-handset-tmus",
134     [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
135     [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
136     [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
137     [SND_DEVICE_OUT_USB_HEADSET] = "usb-headset",
138     [SND_DEVICE_OUT_USB_HEADPHONES] = "usb-headphones",
139     [SND_DEVICE_OUT_VOICE_USB_HEADSET] = "usb-headset",
140     [SND_DEVICE_OUT_VOICE_USB_HEADPHONES] = "usb-headphones",
141 
142 
143     /* Capture sound devices */
144     [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
145     [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
146     [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
147     [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic",
148     [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "voice-speaker-mic",
149     [SND_DEVICE_IN_HEADSET_MIC_AEC] = "headset-mic",
150     [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
151     [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
152     [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
153     [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
154     [SND_DEVICE_IN_BT_SCO_MIC_WB] = "bt-sco-mic-wb",
155     [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
156     [SND_DEVICE_IN_VOICE_DMIC_EF] = "voice-dmic-ef",
157     [SND_DEVICE_IN_VOICE_DMIC_BS] = "voice-dmic-bs",
158     [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = "voice-dmic-ef-tmus",
159     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = "voice-speaker-dmic-ef",
160     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = "voice-speaker-dmic-bs",
161     [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
162     [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
163     [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
164     [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
165     [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = "voice-rec-dmic-ef",
166     [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = "voice-rec-dmic-bs",
167     [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = "voice-rec-dmic-ef-fluence",
168     [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = "voice-rec-dmic-bs-fluence",
169 };
170 
171 /* ACDB IDs (audio DSP path configuration IDs) for each sound device */
172 static const int acdb_device_table[SND_DEVICE_MAX] = {
173     [SND_DEVICE_NONE] = -1,
174     [SND_DEVICE_OUT_HANDSET] = 7,
175     [SND_DEVICE_OUT_SPEAKER] = 14,
176     [SND_DEVICE_OUT_SPEAKER_REVERSE] = 14,
177     [SND_DEVICE_OUT_SPEAKER_SAFE] = 14,
178     [SND_DEVICE_OUT_HEADPHONES] = 10,
179     [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
180     [SND_DEVICE_OUT_VOICE_SPEAKER] = 14,
181     [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
182     [SND_DEVICE_OUT_VOICE_HEADSET] = 10,
183     [SND_DEVICE_OUT_HDMI] = 18,
184     [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 14,
185     [SND_DEVICE_OUT_BT_SCO] = 22,
186     [SND_DEVICE_OUT_BT_SCO_WB] = 39,
187     [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = 81,
188     [SND_DEVICE_OUT_VOICE_HANDSET] = 81,
189     [SND_DEVICE_OUT_VOICE_HAC_HANDSET] = 81,
190     [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
191     [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
192     [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
193     [SND_DEVICE_OUT_USB_HEADSET] = 45,
194     [SND_DEVICE_OUT_USB_HEADPHONES] = 45,
195     [SND_DEVICE_OUT_VOICE_USB_HEADSET] = 45,
196     [SND_DEVICE_OUT_VOICE_USB_HEADPHONES] = 45,
197 
198     [SND_DEVICE_IN_HANDSET_MIC] = 4,
199     [SND_DEVICE_IN_SPEAKER_MIC] = 4,
200     [SND_DEVICE_IN_HEADSET_MIC] = 8,
201     [SND_DEVICE_IN_HANDSET_MIC_AEC] = 40,
202     [SND_DEVICE_IN_SPEAKER_MIC_AEC] = 42,
203     [SND_DEVICE_IN_HEADSET_MIC_AEC] = 47,
204     [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
205     [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8,
206     [SND_DEVICE_IN_HDMI_MIC] = 4,
207     [SND_DEVICE_IN_BT_SCO_MIC] = 21,
208     [SND_DEVICE_IN_BT_SCO_MIC_WB] = 38,
209     [SND_DEVICE_IN_CAMCORDER_MIC] = 61,
210     [SND_DEVICE_IN_VOICE_DMIC_EF] = 6,
211     [SND_DEVICE_IN_VOICE_DMIC_BS] = 5,
212     [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = 91,
213     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = 13,
214     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = 12,
215     [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
216     [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
217     [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
218     [SND_DEVICE_IN_VOICE_REC_MIC] = 62,
219     /* TODO: Update with proper acdb ids */
220     [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = 62,
221     [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = 62,
222     [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = 6,
223     [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = 5,
224 };
225 
226 #define DEEP_BUFFER_PLATFORM_DELAY (29*1000LL)
227 #define LOW_LATENCY_PLATFORM_DELAY (13*1000LL)
228 
229 static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
230 static bool is_tmus = false;
231 
check_operator()232 static void check_operator()
233 {
234     char value[PROPERTY_VALUE_MAX];
235     int mccmnc;
236     property_get("gsm.sim.operator.numeric",value,"0");
237     mccmnc = atoi(value);
238     ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
239     switch(mccmnc) {
240     /* TMUS MCC(310), MNC(490, 260, 026) */
241     case 310490:
242     case 310260:
243     case 310026:
244         is_tmus = true;
245         break;
246     }
247 }
248 
is_operator_tmus()249 bool is_operator_tmus()
250 {
251     pthread_once(&check_op_once_ctl, check_operator);
252     return is_tmus;
253 }
254 
set_echo_reference(struct mixer * mixer,const char * ec_ref)255 static int set_echo_reference(struct mixer *mixer, const char* ec_ref)
256 {
257     struct mixer_ctl *ctl;
258     const char *mixer_ctl_name = "EC_REF_RX";
259 
260     ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
261     if (!ctl) {
262         ALOGE("%s: Could not get ctl for mixer cmd - %s",
263               __func__, mixer_ctl_name);
264         return -EINVAL;
265     }
266     ALOGV("Setting EC Reference: %s", ec_ref);
267     mixer_ctl_set_enum_by_string(ctl, ec_ref);
268     return 0;
269 }
270 
platform_init(struct audio_device * adev)271 void *platform_init(struct audio_device *adev)
272 {
273     char platform[PROPERTY_VALUE_MAX];
274     char baseband[PROPERTY_VALUE_MAX];
275     char value[PROPERTY_VALUE_MAX];
276     struct platform_data *my_data;
277 
278     adev->mixer = mixer_open(MIXER_CARD);
279 
280     if (!adev->mixer) {
281         ALOGE("Unable to open the mixer, aborting.");
282         return NULL;
283     }
284 
285     adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
286     if (!adev->audio_route) {
287         ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
288         return NULL;
289     }
290 
291     my_data = calloc(1, sizeof(struct platform_data));
292 
293     my_data->adev = adev;
294     my_data->dualmic_config = DUALMIC_CONFIG_NONE;
295     my_data->fluence_in_spkr_mode = false;
296     my_data->fluence_in_voice_call = false;
297     my_data->fluence_in_voice_rec = false;
298 
299     property_get("persist.audio.dualmic.config",value,"");
300     if (!strcmp("broadside", value)) {
301         my_data->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
302         adev->acdb_settings |= DMIC_FLAG;
303     } else if (!strcmp("endfire", value)) {
304         my_data->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
305         adev->acdb_settings |= DMIC_FLAG;
306     }
307 
308     if (my_data->dualmic_config != DUALMIC_CONFIG_NONE) {
309         property_get("persist.audio.fluence.voicecall",value,"");
310         if (!strcmp("true", value)) {
311             my_data->fluence_in_voice_call = true;
312         }
313 
314         property_get("persist.audio.fluence.voicerec",value,"");
315         if (!strcmp("true", value)) {
316             my_data->fluence_in_voice_rec = true;
317         }
318 
319         property_get("persist.audio.fluence.speaker",value,"");
320         if (!strcmp("true", value)) {
321             my_data->fluence_in_spkr_mode = true;
322         }
323     }
324 
325     my_data->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
326     if (my_data->acdb_handle == NULL) {
327         ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
328     } else {
329         ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
330         my_data->acdb_deallocate = (acdb_deallocate_t)dlsym(my_data->acdb_handle,
331                                                     "acdb_loader_deallocate_ACDB");
332         my_data->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(my_data->acdb_handle,
333                                                     "acdb_loader_send_audio_cal");
334         if (!my_data->acdb_send_audio_cal)
335             ALOGW("%s: Could not find the symbol acdb_send_audio_cal from %s",
336                   __func__, LIB_ACDB_LOADER);
337         my_data->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(my_data->acdb_handle,
338                                                     "acdb_loader_send_voice_cal");
339         my_data->acdb_init = (acdb_init_t)dlsym(my_data->acdb_handle,
340                                                     "acdb_loader_init_ACDB");
341         if (my_data->acdb_init == NULL)
342             ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
343         else
344             my_data->acdb_init();
345     }
346 
347     /* If platform is Fusion3, load CSD Client specific symbols
348      * Voice call is handled by MDM and apps processor talks to
349      * MDM through CSD Client
350      */
351     property_get("ro.board.platform", platform, "");
352     property_get("ro.baseband", baseband, "");
353     if (!strcmp("msm8960", platform) && !strcmp("mdm", baseband)) {
354         my_data->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
355         if (my_data->csd_client == NULL)
356             ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
357     }
358 
359     if (my_data->csd_client) {
360         ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
361         my_data->csd_client_deinit = (csd_client_deinit_t)dlsym(my_data->csd_client,
362                                                     "csd_client_deinit");
363         my_data->csd_disable_device = (csd_disable_device_t)dlsym(my_data->csd_client,
364                                                     "csd_client_disable_device");
365         my_data->csd_enable_device = (csd_enable_device_t)dlsym(my_data->csd_client,
366                                                     "csd_client_enable_device");
367         my_data->csd_start_voice = (csd_start_voice_t)dlsym(my_data->csd_client,
368                                                     "csd_client_start_voice");
369         my_data->csd_stop_voice = (csd_stop_voice_t)dlsym(my_data->csd_client,
370                                                     "csd_client_stop_voice");
371         my_data->csd_volume = (csd_volume_t)dlsym(my_data->csd_client,
372                                                     "csd_client_volume");
373         my_data->csd_mic_mute = (csd_mic_mute_t)dlsym(my_data->csd_client,
374                                                     "csd_client_mic_mute");
375         my_data->csd_client_init = (csd_client_init_t)dlsym(my_data->csd_client,
376                                                     "csd_client_init");
377 
378         if (my_data->csd_client_init == NULL) {
379             ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror());
380         } else {
381             my_data->csd_client_init();
382         }
383     }
384 
385     return my_data;
386 }
387 
platform_deinit(void * platform)388 void platform_deinit(void *platform)
389 {
390     free(platform);
391 }
392 
platform_get_snd_device_name(snd_device_t snd_device)393 const char *platform_get_snd_device_name(snd_device_t snd_device)
394 {
395     if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX)
396         return device_table[snd_device];
397     else
398         return "none";
399 }
400 
platform_add_backend_name(void * platform __unused,char * mixer_path,snd_device_t snd_device)401 void platform_add_backend_name(void *platform __unused, char *mixer_path,
402                                snd_device_t snd_device)
403 {
404     if (snd_device == SND_DEVICE_IN_BT_SCO_MIC)
405         strcat(mixer_path, " bt-sco");
406     else if(snd_device == SND_DEVICE_OUT_BT_SCO)
407         strcat(mixer_path, " bt-sco");
408     else if (snd_device == SND_DEVICE_OUT_HDMI)
409         strcat(mixer_path, " hdmi");
410     else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HDMI)
411         strcat(mixer_path, " speaker-and-hdmi");
412     else if (snd_device == SND_DEVICE_OUT_BT_SCO_WB ||
413              snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB)
414         strcat(mixer_path, " bt-sco-wb");
415 }
416 
platform_get_pcm_device_id(audio_usecase_t usecase,int device_type)417 int platform_get_pcm_device_id(audio_usecase_t usecase, int device_type)
418 {
419     int device_id;
420     if (device_type == PCM_PLAYBACK)
421         device_id = pcm_device_table[usecase][0];
422     else
423         device_id = pcm_device_table[usecase][1];
424     return device_id;
425 }
426 
platform_get_haptics_pcm_device_id()427 int platform_get_haptics_pcm_device_id()
428 {
429     return -1;
430 }
431 
platform_get_snd_device_index(char * snd_device_index_name __unused)432 int platform_get_snd_device_index(char *snd_device_index_name __unused)
433 {
434     return -ENODEV;
435 }
436 
platform_set_snd_device_acdb_id(snd_device_t snd_device __unused,unsigned int acdb_id __unused)437 int platform_set_snd_device_acdb_id(snd_device_t snd_device __unused,
438                                     unsigned int acdb_id __unused)
439 {
440     return -ENODEV;
441 }
442 
platform_get_default_app_type_v2(void * platform __unused,usecase_type_t type __unused,int * app_type __unused)443 int platform_get_default_app_type_v2(void *platform __unused, usecase_type_t type __unused,
444                                      int *app_type __unused)
445 {
446     ALOGE("%s: Not implemented", __func__);
447     return -ENOSYS;
448 }
449 
platform_get_snd_device_acdb_id(snd_device_t snd_device __unused)450 int platform_get_snd_device_acdb_id(snd_device_t snd_device __unused)
451 {
452     ALOGE("%s: Not implemented", __func__);
453     return -ENOSYS;
454 }
455 
platform_add_external_specific_device(snd_device_t snd_device __unused,const char * name __unused,unsigned int acdb_id __unused)456 void platform_add_external_specific_device(snd_device_t snd_device __unused,
457                                            const char *name __unused,
458                                            unsigned int acdb_id __unused)
459 {
460     return;
461 }
462 
platform_add_operator_specific_device(snd_device_t snd_device __unused,const char * operator __unused,const char * mixer_path __unused,unsigned int acdb_id __unused)463 void platform_add_operator_specific_device(snd_device_t snd_device __unused,
464                                            const char *operator __unused,
465                                            const char *mixer_path __unused,
466                                            unsigned int acdb_id __unused)
467 {
468 }
469 
platform_send_audio_calibration(void * platform,snd_device_t snd_device)470 int platform_send_audio_calibration(void *platform, snd_device_t snd_device)
471 {
472     struct platform_data *my_data = (struct platform_data *)platform;
473     int acdb_dev_id, acdb_dev_type;
474 
475     acdb_dev_id = acdb_device_table[snd_device];
476     if (acdb_dev_id < 0) {
477         ALOGE("%s: Could not find acdb id for device(%d)",
478               __func__, snd_device);
479         return -EINVAL;
480     }
481     if (my_data->acdb_send_audio_cal) {
482         ("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
483               __func__, snd_device, acdb_dev_id);
484         if (snd_device >= SND_DEVICE_OUT_BEGIN &&
485                 snd_device < SND_DEVICE_OUT_END)
486             acdb_dev_type = ACDB_DEV_TYPE_OUT;
487         else
488             acdb_dev_type = ACDB_DEV_TYPE_IN;
489         my_data->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
490     }
491     return 0;
492 }
493 
platform_switch_voice_call_device_pre(void * platform)494 int platform_switch_voice_call_device_pre(void *platform)
495 {
496     struct platform_data *my_data = (struct platform_data *)platform;
497     int ret = 0;
498 
499     if (my_data->csd_client != NULL &&
500         voice_is_in_call(my_data->adev)) {
501         /* This must be called before disabling the mixer controls on APQ side */
502         if (my_data->csd_disable_device == NULL) {
503             ALOGE("%s: dlsym error for csd_disable_device", __func__);
504         } else {
505             ret = my_data->csd_disable_device();
506             if (ret < 0) {
507                 ALOGE("%s: csd_client_disable_device, failed, error %d",
508                       __func__, ret);
509             }
510         }
511     }
512     return ret;
513 }
514 
platform_switch_voice_call_device_post(void * platform,snd_device_t out_snd_device,snd_device_t in_snd_device)515 int platform_switch_voice_call_device_post(void *platform,
516                                            snd_device_t out_snd_device,
517                                            snd_device_t in_snd_device)
518 {
519     struct platform_data *my_data = (struct platform_data *)platform;
520     int acdb_rx_id, acdb_tx_id;
521     int ret = 0;
522 
523     if (my_data->csd_client) {
524         if (my_data->csd_enable_device == NULL) {
525             ALOGE("%s: dlsym error for csd_enable_device",
526                   __func__);
527         } else {
528             acdb_rx_id = acdb_device_table[out_snd_device];
529             acdb_tx_id = acdb_device_table[in_snd_device];
530 
531             if (acdb_rx_id > 0 || acdb_tx_id > 0) {
532                 ret = my_data->csd_enable_device(acdb_rx_id, acdb_tx_id,
533                                                     my_data->adev->acdb_settings);
534                 if (ret < 0) {
535                     ALOGE("%s: csd_enable_device, failed, error %d",
536                           __func__, ret);
537                 }
538             } else {
539                 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
540                       acdb_rx_id, acdb_tx_id);
541             }
542         }
543     }
544 
545     return ret;
546 }
547 
platform_start_voice_call(void * platform,uint32_t vsid __unused)548 int platform_start_voice_call(void *platform, uint32_t vsid __unused)
549 {
550     struct platform_data *my_data = (struct platform_data *)platform;
551     int ret = 0;
552 
553     if (my_data->csd_client) {
554         if (my_data->csd_start_voice == NULL) {
555             ALOGE("dlsym error for csd_client_start_voice");
556             ret = -ENOSYS;
557         } else {
558             ret = my_data->csd_start_voice();
559             if (ret < 0) {
560                 ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
561             }
562         }
563     }
564 
565     return ret;
566 }
567 
platform_stop_voice_call(void * platform,uint32_t vsid __unused)568 int platform_stop_voice_call(void *platform, uint32_t vsid __unused)
569 {
570     struct platform_data *my_data = (struct platform_data *)platform;
571     int ret = 0;
572 
573     if (my_data->csd_client) {
574         if (my_data->csd_stop_voice == NULL) {
575             ALOGE("dlsym error for csd_stop_voice");
576         } else {
577             ret = my_data->csd_stop_voice();
578             if (ret < 0) {
579                 ALOGE("%s: csd_stop_voice error %d\n", __func__, ret);
580             }
581         }
582     }
583 
584     return ret;
585 }
586 
platform_set_speaker_gain_in_combo(struct audio_device * adev __unused,snd_device_t snd_device __unused,bool enable __unused)587 void platform_set_speaker_gain_in_combo(struct audio_device *adev __unused,
588                                         snd_device_t snd_device  __unused,
589                                         bool enable __unused) {
590 }
591 
platform_set_voice_volume(void * platform,int volume)592 int platform_set_voice_volume(void *platform, int volume)
593 {
594     struct platform_data *my_data = (struct platform_data *)platform;
595     int ret = 0;
596 
597     if (my_data->csd_client) {
598         if (my_data->csd_volume == NULL) {
599             ALOGE("%s: dlsym error for csd_volume", __func__);
600         } else {
601             ret = my_data->csd_volume(volume);
602             if (ret < 0) {
603                 ALOGE("%s: csd_volume error %d", __func__, ret);
604             }
605         }
606     } else {
607         ALOGE("%s: No CSD Client present", __func__);
608     }
609 
610     return ret;
611 }
612 
platform_set_mic_mute(void * platform,bool state)613 int platform_set_mic_mute(void *platform, bool state)
614 {
615     struct platform_data *my_data = (struct platform_data *)platform;
616     int ret = 0;
617 
618     if (my_data->adev->mode == AUDIO_MODE_IN_CALL) {
619         if (my_data->csd_client) {
620             if (my_data->csd_mic_mute == NULL) {
621                 ALOGE("%s: dlsym error for csd_mic_mute", __func__);
622             } else {
623                 ret = my_data->csd_mic_mute(state);
624                 if (ret < 0) {
625                     ALOGE("%s: csd_mic_mute error %d", __func__, ret);
626                 }
627             }
628         } else {
629             ALOGE("%s: No CSD Client present", __func__);
630         }
631     }
632 
633     return ret;
634 }
635 
platform_set_device_mute(void * platform __unused,bool state __unused,char * dir __unused)636 int platform_set_device_mute(void *platform __unused, bool state __unused, char *dir __unused)
637 {
638     ALOGE("%s: Not implemented", __func__);
639     return -ENOSYS;
640 }
641 
platform_get_output_snd_device(void * platform,audio_devices_t devices)642 snd_device_t platform_get_output_snd_device(void *platform, audio_devices_t devices)
643 {
644     struct platform_data *my_data = (struct platform_data *)platform;
645     struct audio_device *adev = my_data->adev;
646     audio_mode_t mode = adev->mode;
647     snd_device_t snd_device = SND_DEVICE_NONE;
648 
649     ALOGV("%s: enter: output devices(%#x)", __func__, devices);
650     if (devices == AUDIO_DEVICE_NONE ||
651         devices & AUDIO_DEVICE_BIT_IN) {
652         ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
653         goto exit;
654     }
655 
656     if (voice_is_in_call(adev)) {
657         if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
658             devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
659             if (adev->voice.tty_mode == TTY_MODE_FULL)
660                 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
661             else if (adev->voice.tty_mode == TTY_MODE_VCO)
662                 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
663             else if (adev->voice.tty_mode == TTY_MODE_HCO)
664                 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
665             else if (devices & AUDIO_DEVICE_OUT_WIRED_HEADSET)
666                 snd_device = SND_DEVICE_OUT_VOICE_HEADSET;
667             else
668                 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
669         } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
670             if (adev->bt_wb_speech_enabled) {
671                 snd_device = SND_DEVICE_OUT_BT_SCO_WB;
672             } else {
673                 snd_device = SND_DEVICE_OUT_BT_SCO;
674             }
675         } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
676             snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
677         } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
678             if (is_operator_tmus())
679                 snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
680             else
681                 snd_device = SND_DEVICE_OUT_HANDSET;
682         }
683         if (snd_device != SND_DEVICE_NONE) {
684             goto exit;
685         }
686     }
687 
688     if (popcount(devices) == 2) {
689         if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
690                         AUDIO_DEVICE_OUT_SPEAKER)) {
691             snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
692         } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
693                                AUDIO_DEVICE_OUT_SPEAKER)) {
694             snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
695         } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
696                                AUDIO_DEVICE_OUT_SPEAKER)) {
697             snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
698         } else {
699             ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
700             goto exit;
701         }
702         if (snd_device != SND_DEVICE_NONE) {
703             goto exit;
704         }
705     }
706 
707     if (popcount(devices) != 1) {
708         ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
709         goto exit;
710     }
711 
712     if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
713         devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
714         snd_device = SND_DEVICE_OUT_HEADPHONES;
715     } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
716         /*
717          * Perform device switch only if acdb tuning is different between SPEAKER & SPEAKER_REVERSE,
718          * Or there will be a small pause while performing device switch.
719          */
720         if (my_data->speaker_lr_swap &&
721             (acdb_device_table[SND_DEVICE_OUT_SPEAKER] !=
722             acdb_device_table[SND_DEVICE_OUT_SPEAKER_REVERSE]))
723             snd_device = SND_DEVICE_OUT_SPEAKER_REVERSE;
724         else
725             snd_device = SND_DEVICE_OUT_SPEAKER;
726     } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
727         if (adev->bt_wb_speech_enabled) {
728             snd_device = SND_DEVICE_OUT_BT_SCO_WB;
729         } else {
730             snd_device = SND_DEVICE_OUT_BT_SCO;
731         }
732     } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
733         snd_device = SND_DEVICE_OUT_HDMI ;
734     } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
735         snd_device = SND_DEVICE_OUT_HANDSET;
736     } else {
737         ALOGE("%s: Unknown device(s) %#x", __func__, devices);
738     }
739 exit:
740     ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
741     return snd_device;
742 }
743 
platform_get_input_snd_device(void * platform,struct stream_in * in,audio_devices_t out_device)744 snd_device_t platform_get_input_snd_device(void *platform,
745                                            struct stream_in *in,
746                                            audio_devices_t out_device)
747 {
748     struct platform_data *my_data = (struct platform_data *)platform;
749     struct audio_device *adev = my_data->adev;
750     audio_mode_t mode = adev->mode;
751     snd_device_t snd_device = SND_DEVICE_NONE;
752 
753     if (in == NULL) {
754         in = adev_get_active_input(adev);
755     }
756 
757     audio_source_t source = (in == NULL) ? AUDIO_SOURCE_DEFAULT : in->source;
758     audio_devices_t in_device =
759         ((in == NULL) ? AUDIO_DEVICE_NONE : in->device) & ~AUDIO_DEVICE_BIT_IN;
760     audio_channel_mask_t channel_mask = (in == NULL) ? AUDIO_CHANNEL_IN_MONO : in->channel_mask;
761 
762     ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
763           __func__, out_device, in_device);
764     if ((out_device != AUDIO_DEVICE_NONE) && voice_is_in_call(adev)) {
765         if (adev->voice.tty_mode != TTY_MODE_OFF) {
766             if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
767                 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
768                 switch (adev->voice.tty_mode) {
769                 case TTY_MODE_FULL:
770                     snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
771                     break;
772                 case TTY_MODE_VCO:
773                     snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
774                     break;
775                 case TTY_MODE_HCO:
776                     snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
777                     break;
778                 default:
779                     ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->voice.tty_mode);
780                 }
781                 goto exit;
782             }
783         }
784         if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
785             out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
786             if (my_data->fluence_in_voice_call == false) {
787                 snd_device = SND_DEVICE_IN_HANDSET_MIC;
788             } else {
789                 if (my_data->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
790                     if (is_operator_tmus())
791                         snd_device = SND_DEVICE_IN_VOICE_DMIC_EF_TMUS;
792                     else
793                         snd_device = SND_DEVICE_IN_VOICE_DMIC_EF;
794                 } else if(my_data->dualmic_config == DUALMIC_CONFIG_BROADSIDE)
795                     snd_device = SND_DEVICE_IN_VOICE_DMIC_BS;
796                 else
797                     snd_device = SND_DEVICE_IN_HANDSET_MIC;
798             }
799         } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
800             snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
801         } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
802             if (adev->bt_wb_speech_enabled) {
803                 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
804             } else {
805                 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
806             }
807         } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
808             if (my_data->fluence_in_voice_call && my_data->fluence_in_spkr_mode &&
809                     my_data->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
810                 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF;
811             } else if (my_data->fluence_in_voice_call && my_data->fluence_in_spkr_mode &&
812                     my_data->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
813                 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS;
814             } else {
815                 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
816             }
817         }
818     } else if (source == AUDIO_SOURCE_CAMCORDER) {
819         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
820             in_device & AUDIO_DEVICE_IN_BACK_MIC) {
821             snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
822         }
823     } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
824         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
825             if (my_data->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
826                 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
827                     snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF;
828                 else if (my_data->fluence_in_voice_rec)
829                     snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE;
830             } else if (my_data->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
831                 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
832                     snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS;
833                 else if (my_data->fluence_in_voice_rec)
834                     snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE;
835             }
836 
837             if (snd_device == SND_DEVICE_NONE) {
838                 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
839             }
840         }
841     } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
842             mode == AUDIO_MODE_IN_COMMUNICATION) {
843         if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
844             in_device = AUDIO_DEVICE_IN_BACK_MIC;
845         if (in) {
846             if (in != NULL && in->enable_aec) {
847                 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
848                     snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
849                 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
850                     snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
851                 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
852                     snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
853                 }
854                 set_echo_reference(adev->mixer, "SLIM_RX");
855             } else
856                 set_echo_reference(adev->mixer, "NONE");
857         }
858     } else if (source == AUDIO_SOURCE_DEFAULT) {
859         goto exit;
860     }
861 
862 
863     if (snd_device != SND_DEVICE_NONE) {
864         goto exit;
865     }
866 
867     if (in_device != AUDIO_DEVICE_NONE &&
868             !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
869             !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
870         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
871             snd_device = SND_DEVICE_IN_HANDSET_MIC;
872         } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
873             snd_device = SND_DEVICE_IN_SPEAKER_MIC;
874         } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
875             snd_device = SND_DEVICE_IN_HEADSET_MIC;
876         } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
877             if (adev->bt_wb_speech_enabled) {
878                 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
879             } else {
880                 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
881             }
882         } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
883             snd_device = SND_DEVICE_IN_HDMI_MIC;
884         } else {
885             ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
886             ALOGW("%s: Using default handset-mic", __func__);
887             snd_device = SND_DEVICE_IN_HANDSET_MIC;
888         }
889     } else {
890         if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
891             snd_device = SND_DEVICE_IN_HANDSET_MIC;
892         } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
893             snd_device = SND_DEVICE_IN_HEADSET_MIC;
894         } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
895             snd_device = SND_DEVICE_IN_SPEAKER_MIC;
896         } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
897             snd_device = SND_DEVICE_IN_HANDSET_MIC;
898         } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
899             if (adev->bt_wb_speech_enabled) {
900                 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
901             } else {
902                 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
903             }
904         } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
905             snd_device = SND_DEVICE_IN_HDMI_MIC;
906         } else {
907             ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
908             ALOGW("%s: Using default handset-mic", __func__);
909             snd_device = SND_DEVICE_IN_HANDSET_MIC;
910         }
911     }
912 exit:
913     ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
914     return snd_device;
915 }
916 
platform_set_hdmi_channels(void * platform,int channel_count)917 int platform_set_hdmi_channels(void *platform,  int channel_count)
918 {
919     struct platform_data *my_data = (struct platform_data *)platform;
920     struct audio_device *adev = my_data->adev;
921     struct mixer_ctl *ctl;
922     const char *channel_cnt_str = NULL;
923     const char *mixer_ctl_name = "HDMI_RX Channels";
924     switch (channel_count) {
925     case 8:
926         channel_cnt_str = "Eight"; break;
927     case 7:
928         channel_cnt_str = "Seven"; break;
929     case 6:
930         channel_cnt_str = "Six"; break;
931     case 5:
932         channel_cnt_str = "Five"; break;
933     case 4:
934         channel_cnt_str = "Four"; break;
935     case 3:
936         channel_cnt_str = "Three"; break;
937     default:
938         channel_cnt_str = "Two"; break;
939     }
940     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
941     if (!ctl) {
942         ALOGE("%s: Could not get ctl for mixer cmd - %s",
943               __func__, mixer_ctl_name);
944         return -EINVAL;
945     }
946     ALOGV("HDMI channel count: %s", channel_cnt_str);
947     mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
948     return 0;
949 }
950 
platform_edid_get_max_channels(void * platform __unused)951 int platform_edid_get_max_channels(void *platform __unused)
952 {
953     FILE *file;
954     struct audio_block_header header;
955     char block[MAX_SAD_BLOCKS * SAD_BLOCK_SIZE];
956     char *sad = block;
957     int num_audio_blocks;
958     int channel_count;
959     int max_channels = 0;
960     int i;
961 
962     file = fopen(AUDIO_DATA_BLOCK_PATH, "rb");
963     if (file == NULL) {
964         ALOGE("Unable to open '%s'", AUDIO_DATA_BLOCK_PATH);
965         return 0;
966     }
967 
968     /* Read audio block header */
969     fread(&header, 1, sizeof(header), file);
970 
971     /* Read SAD blocks, clamping the maximum size for safety */
972     if (header.length > (int)sizeof(block))
973         header.length = (int)sizeof(block);
974     fread(&block, header.length, 1, file);
975 
976     fclose(file);
977 
978     /* Calculate the number of SAD blocks */
979     num_audio_blocks = header.length / SAD_BLOCK_SIZE;
980 
981     for (i = 0; i < num_audio_blocks; i++) {
982         /* Only consider LPCM blocks */
983         if ((sad[0] >> 3) != EDID_FORMAT_LPCM)
984             continue;
985 
986         channel_count = (sad[0] & 0x7) + 1;
987         if (channel_count > max_channels)
988             max_channels = channel_count;
989 
990         /* Advance to next block */
991         sad += 3;
992     }
993 
994     return max_channels;
995 }
996 
platform_set_incall_recording_session_id(void * platform __unused,uint32_t session_id __unused,int rec_mode __unused)997 int platform_set_incall_recording_session_id(void *platform __unused,
998                                              uint32_t session_id __unused, int rec_mode __unused)
999 {
1000     ALOGE("%s: Not implemented", __func__);
1001     return -ENOSYS;
1002 }
1003 
platform_stop_incall_recording_usecase(void * platform __unused)1004 int platform_stop_incall_recording_usecase(void *platform __unused)
1005 {
1006     ALOGE("%s: Not implemented", __func__);
1007     return -ENOSYS;
1008 }
1009 
platform_start_incall_music_usecase(void * platform __unused)1010 int platform_start_incall_music_usecase(void *platform __unused)
1011 {
1012     ALOGE("%s: Not implemented", __func__);
1013     return -ENOSYS;
1014 }
1015 
platform_stop_incall_music_usecase(void * platform __unused)1016 int platform_stop_incall_music_usecase(void *platform __unused)
1017 {
1018     ALOGE("%s: Not implemented", __func__);
1019     return -ENOSYS;
1020 }
1021 
platform_set_parameters(void * platform __unused,struct str_parms * parms __unused)1022 int platform_set_parameters(void *platform __unused,
1023                             struct str_parms *parms __unused)
1024 {
1025     ALOGE("%s: Not implemented", __func__);
1026     return -ENOSYS;
1027 }
1028 
1029 /* Delay in Us */
platform_render_latency(audio_usecase_t usecase)1030 int64_t platform_render_latency(audio_usecase_t usecase)
1031 {
1032     switch (usecase) {
1033         case USECASE_AUDIO_PLAYBACK_DEEP_BUFFER:
1034             return DEEP_BUFFER_PLATFORM_DELAY;
1035         case USECASE_AUDIO_PLAYBACK_LOW_LATENCY:
1036             return LOW_LATENCY_PLATFORM_DELAY;
1037         default:
1038             return 0;
1039     }
1040 }
1041 
platform_switch_voice_call_enable_device_config(void * platform __unused,snd_device_t out_snd_device __unused,snd_device_t in_snd_device __unused)1042 int platform_switch_voice_call_enable_device_config(void *platform __unused,
1043                                                     snd_device_t out_snd_device __unused,
1044                                                     snd_device_t in_snd_device __unused)
1045 {
1046     return 0;
1047 }
1048 
platform_switch_voice_call_usecase_route_post(void * platform __unused,snd_device_t out_snd_device __unused,snd_device_t in_snd_device __unused)1049 int platform_switch_voice_call_usecase_route_post(void *platform __unused,
1050                                                   snd_device_t out_snd_device __unused,
1051                                                   snd_device_t in_snd_device __unused)
1052 {
1053     return 0;
1054 }
1055 
platform_get_sample_rate(void * platform __unused,uint32_t * rate __unused)1056 int platform_get_sample_rate(void *platform __unused, uint32_t *rate __unused)
1057 {
1058     return -ENOSYS;
1059 }
1060 
platform_get_usecase_index(const char * usecase __unused)1061 int platform_get_usecase_index(const char * usecase __unused)
1062 {
1063     return -ENOSYS;
1064 }
1065 
platform_set_usecase_pcm_id(audio_usecase_t usecase __unused,int32_t type __unused,int32_t pcm_id __unused)1066 int platform_set_usecase_pcm_id(audio_usecase_t usecase __unused, int32_t type __unused,
1067                                 int32_t pcm_id __unused)
1068 {
1069     return -ENOSYS;
1070 }
1071 
platform_set_snd_device_backend(snd_device_t device __unused,const char * backend __unused,const char * hw_interface __unused)1072 int platform_set_snd_device_backend(snd_device_t device __unused,
1073                                     const char *backend __unused,
1074                                     const char *hw_interface __unused)
1075 {
1076     return -ENOSYS;
1077 }
1078 
platform_set_echo_reference(struct audio_device * adev __unused,bool enable __unused,audio_devices_t out_device __unused)1079 void platform_set_echo_reference(struct audio_device *adev __unused,
1080                                  bool enable __unused,
1081                                  audio_devices_t out_device __unused)
1082 {
1083     return;
1084 }
1085 
1086 #define DEFAULT_NOMINAL_SPEAKER_GAIN 20
ramp_speaker_gain(struct audio_device * adev,bool ramp_up,int target_ramp_up_gain)1087 int ramp_speaker_gain(struct audio_device *adev, bool ramp_up, int target_ramp_up_gain) {
1088     // backup_gain: gain to try to set in case of an error during ramp
1089     int start_gain, end_gain, step, backup_gain, i;
1090     bool error = false;
1091     const struct mixer_ctl *ctl;
1092     const char *mixer_ctl_name_gain_left = "Left Speaker Gain";
1093     const char *mixer_ctl_name_gain_right = "Right Speaker Gain";
1094     struct mixer_ctl *ctl_left = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name_gain_left);
1095     struct mixer_ctl *ctl_right = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name_gain_right);
1096     if (!ctl_left || !ctl_right) {
1097         ALOGE("%s: Could not get ctl for mixer cmd - %s or %s, not applying speaker gain ramp",
1098                       __func__, mixer_ctl_name_gain_left, mixer_ctl_name_gain_right);
1099         return -EINVAL;
1100     } else if ((mixer_ctl_get_num_values(ctl_left) != 1)
1101             || (mixer_ctl_get_num_values(ctl_right) != 1)) {
1102         ALOGE("%s: Unexpected num values for mixer cmd - %s or %s, not applying speaker gain ramp",
1103                               __func__, mixer_ctl_name_gain_left, mixer_ctl_name_gain_right);
1104         return -EINVAL;
1105     }
1106     if (ramp_up) {
1107         start_gain = 0;
1108         end_gain = target_ramp_up_gain > 0 ? target_ramp_up_gain : DEFAULT_NOMINAL_SPEAKER_GAIN;
1109         step = +1;
1110         backup_gain = end_gain;
1111     } else {
1112         // using same gain on left and right
1113         const int left_gain = mixer_ctl_get_value(ctl_left, 0);
1114         start_gain = left_gain > 0 ? left_gain : DEFAULT_NOMINAL_SPEAKER_GAIN;
1115         end_gain = 0;
1116         step = -1;
1117         backup_gain = start_gain;
1118     }
1119     for (i = start_gain ; i != (end_gain + step) ; i += step) {
1120         //ALOGV("setting speaker gain to %d", i);
1121         if (mixer_ctl_set_value(ctl_left, 0, i)) {
1122             ALOGE("%s: error setting %s to %d during gain ramp",
1123                     __func__, mixer_ctl_name_gain_left, i);
1124             error = true;
1125             break;
1126         }
1127         if (mixer_ctl_set_value(ctl_right, 0, i)) {
1128             ALOGE("%s: error setting %s to %d during gain ramp",
1129                     __func__, mixer_ctl_name_gain_right, i);
1130             error = true;
1131             break;
1132         }
1133         usleep(1000);
1134     }
1135     if (error) {
1136         // an error occured during the ramp, let's still try to go back to a safe volume
1137         if (mixer_ctl_set_value(ctl_left, 0, backup_gain)) {
1138             ALOGE("%s: error restoring left gain to %d", __func__, backup_gain);
1139         }
1140         if (mixer_ctl_set_value(ctl_right, 0, backup_gain)) {
1141             ALOGE("%s: error restoring right gain to %d", __func__, backup_gain);
1142         }
1143     }
1144     return start_gain;
1145 }
1146 
platform_set_swap_mixer(struct audio_device * adev,bool swap_channels)1147 int platform_set_swap_mixer(struct audio_device *adev, bool swap_channels)
1148 {
1149     const char *mixer_ctl_name = "Swap channel";
1150     struct mixer_ctl *ctl;
1151     const char *mixer_path;
1152     struct platform_data *my_data = (struct platform_data *)adev->platform;
1153 
1154     // forced to set to swap, but device not rotated ... ignore set
1155     if (swap_channels && !my_data->speaker_lr_swap)
1156         return 0;
1157 
1158     ALOGV("%s:", __func__);
1159 
1160     if (swap_channels) {
1161         mixer_path = platform_get_snd_device_name(SND_DEVICE_OUT_SPEAKER_REVERSE);
1162         audio_route_apply_and_update_path(adev->audio_route, mixer_path);
1163     } else {
1164         mixer_path = platform_get_snd_device_name(SND_DEVICE_OUT_SPEAKER);
1165         audio_route_apply_and_update_path(adev->audio_route, mixer_path);
1166     }
1167 
1168     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1169     if (!ctl) {
1170         ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
1171         return -EINVAL;
1172     }
1173 
1174     if (mixer_ctl_set_value(ctl, 0, swap_channels) < 0) {
1175         ALOGE("%s: Could not set reverse cotrol %d",__func__, swap_channels);
1176         return -EINVAL;
1177     }
1178 
1179     ALOGV("platfor_force_swap_channel :: Channel orientation ( %s ) ",
1180            swap_channels?"R --> L":"L --> R");
1181 
1182     return 0;
1183 }
1184 
platform_check_and_set_swap_lr_channels(struct audio_device * adev,bool swap_channels)1185 int platform_check_and_set_swap_lr_channels(struct audio_device *adev, bool swap_channels)
1186 {
1187     // only update if there is active pcm playback on speaker
1188     struct audio_usecase *usecase;
1189     struct listnode *node;
1190     struct platform_data *my_data = (struct platform_data *)adev->platform;
1191 
1192     my_data->speaker_lr_swap = swap_channels;
1193 
1194     return platform_set_swap_channels(adev, swap_channels);
1195 }
1196 
platform_set_swap_channels(struct audio_device * adev,bool swap_channels)1197 int platform_set_swap_channels(struct audio_device *adev, bool swap_channels)
1198 {
1199     // only update if there is active pcm playback on speaker
1200     struct audio_usecase *usecase;
1201     struct listnode *node;
1202     struct platform_data *my_data = (struct platform_data *)adev->platform;
1203 
1204     // do not swap channels in audio modes with concurrent capture and playback
1205     // as this may break the echo reference
1206     if ((adev->mode == AUDIO_MODE_IN_COMMUNICATION) || (adev->mode == AUDIO_MODE_IN_CALL)) {
1207         ALOGV("%s: will not swap due to audio mode %d", __func__, adev->mode);
1208         return 0;
1209     }
1210 
1211     list_for_each(node, &adev->usecase_list) {
1212         usecase = node_to_item(node, struct audio_usecase, list);
1213         if (usecase->type == PCM_PLAYBACK &&
1214                 usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
1215             /*
1216              * If acdb tuning is different for SPEAKER_REVERSE, it is must
1217              * to perform device switch to disable the current backend to
1218              * enable it with new acdb data.
1219              */
1220             if (acdb_device_table[SND_DEVICE_OUT_SPEAKER] !=
1221                 acdb_device_table[SND_DEVICE_OUT_SPEAKER_REVERSE]) {
1222                 const int initial_skpr_gain = ramp_speaker_gain(adev, false /*ramp_up*/, -1);
1223                 select_devices(adev, usecase->id);
1224                 if (initial_skpr_gain != -EINVAL)
1225                     ramp_speaker_gain(adev, true /*ramp_up*/, initial_skpr_gain);
1226 
1227             } else {
1228                 platform_set_swap_mixer(adev, swap_channels);
1229             }
1230             break;
1231         }
1232     }
1233 
1234     return 0;
1235 }
1236 
platform_send_gain_dep_cal(void * platform __unused,int level __unused)1237 bool platform_send_gain_dep_cal(void *platform __unused,
1238                                 int level __unused)
1239 {
1240     return true;
1241 }
1242 
platform_can_split_snd_device(snd_device_t in_snd_device __unused,int * num_devices __unused,snd_device_t * out_snd_devices __unused)1243 int platform_can_split_snd_device(snd_device_t in_snd_device __unused,
1244                                    int *num_devices __unused,
1245                                    snd_device_t *out_snd_devices __unused)
1246 {
1247     return -ENOSYS;
1248 }
1249 
platform_check_backends_match(snd_device_t snd_device1 __unused,snd_device_t snd_device2 __unused)1250 bool platform_check_backends_match(snd_device_t snd_device1 __unused,
1251                                    snd_device_t snd_device2 __unused)
1252 {
1253     return true;
1254 }
1255 
platform_get_snd_device_name_extn(void * platform __unused,snd_device_t snd_device,char * device_name)1256 int platform_get_snd_device_name_extn(void *platform __unused,
1257                                       snd_device_t snd_device,
1258                                       char *device_name)
1259 {
1260     strlcpy(device_name, platform_get_snd_device_name(snd_device),
1261             DEVICE_NAME_MAX_SIZE);
1262     return 0;
1263 }
1264 
platform_check_and_set_playback_backend_cfg(struct audio_device * adev __unused,struct audio_usecase * usecase __unused,snd_device_t snd_device __unused)1265 bool platform_check_and_set_playback_backend_cfg(struct audio_device* adev __unused,
1266                                               struct audio_usecase *usecase __unused,
1267                                               snd_device_t snd_device __unused)
1268 {
1269     return false;
1270 }
1271 
platform_check_and_set_capture_backend_cfg(struct audio_device * adev __unused,struct audio_usecase * usecase __unused,snd_device_t snd_device __unused)1272 bool platform_check_and_set_capture_backend_cfg(struct audio_device* adev __unused,
1273     struct audio_usecase *usecase __unused, snd_device_t snd_device __unused)
1274 {
1275     return false;
1276 }
1277 
platform_add_gain_level_mapping(struct amp_db_and_gain_table * tbl_entry __unused)1278 bool platform_add_gain_level_mapping(struct amp_db_and_gain_table *tbl_entry __unused)
1279 {
1280     return false;
1281 }
1282 
platform_get_gain_level_mapping(struct amp_db_and_gain_table * mapping_tbl __unused,int table_size __unused)1283 int platform_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl __unused,
1284                                     int table_size __unused)
1285 {
1286     return 0;
1287 }
1288 
platform_snd_card_update(void * platform __unused,card_status_t status __unused)1289 int platform_snd_card_update(void *platform __unused,
1290                              card_status_t status __unused)
1291 {
1292     return -1;
1293 }
1294 
platform_get_snd_device_backend_index(snd_device_t snd_device __unused)1295 int platform_get_snd_device_backend_index(snd_device_t snd_device __unused)
1296 {
1297     return -ENOSYS;
1298 }
1299 
platform_check_and_update_copp_sample_rate(void * platform __unused,snd_device_t snd_device __unused,unsigned int stream_sr __unused,int * sample_rate __unused)1300 void platform_check_and_update_copp_sample_rate(void* platform __unused, snd_device_t snd_device __unused,
1301                                                 unsigned int stream_sr __unused, int* sample_rate __unused)
1302 {
1303 
1304 }
1305 
platform_send_audio_calibration_v2(void * platform __unused,struct audio_usecase * usecase __unused,int app_type __unused,int sample_rate __unused)1306 int platform_send_audio_calibration_v2(void *platform __unused, struct audio_usecase *usecase __unused,
1307                                        int app_type __unused, int sample_rate __unused)
1308 {
1309     return -ENOSYS;
1310 }
1311 
platform_supports_app_type_cfg()1312 bool platform_supports_app_type_cfg() { return false; }
1313 
platform_add_app_type(const char * uc_type __unused,const char * mode __unused,int bw __unused,int app_type __unused,int max_sr __unused)1314 void platform_add_app_type(const char *uc_type __unused,
1315                            const char *mode __unused,
1316                            int bw __unused, int app_type __unused,
1317                            int max_sr __unused) {}
1318 
platform_get_app_type_v2(void * platform __unused,enum usecase_type_t type __unused,const char * mode __unused,int bw __unused,int sr __unused,int * app_type __unused)1319 int platform_get_app_type_v2(void *platform __unused,
1320                              enum usecase_type_t type __unused,
1321                              const char *mode __unused,
1322                              int bw __unused, int sr __unused,
1323                              int *app_type __unused) {
1324     return -ENOSYS;
1325 }
1326 
platform_set_sidetone(struct audio_device * adev,snd_device_t out_snd_device,bool enable,char * str)1327 int platform_set_sidetone(struct audio_device *adev,
1328                           snd_device_t out_snd_device,
1329                           bool enable, char *str)
1330 {
1331     int ret;
1332     if (out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1333         out_snd_device == SND_DEVICE_OUT_VOICE_USB_HEADSET) {
1334             ret = audio_extn_usb_enable_sidetone(out_snd_device, enable);
1335             if (ret)
1336                 ALOGI("%s: usb device %d does not support device sidetone\n",
1337                   __func__, out_snd_device);
1338     } else {
1339         ALOGV("%s: sidetone out device(%d) mixer cmd = %s\n",
1340               __func__, out_snd_device, str);
1341         if (enable)
1342             audio_route_apply_and_update_path(adev->audio_route, str);
1343         else
1344             audio_route_reset_and_update_path(adev->audio_route, str);
1345     }
1346     return 0;
1347 }
1348 
platform_get_mmap_data_fd(void * platform __unused,int fe_dev __unused,int dir __unused,int * fd __unused,uint32_t * size __unused)1349 int platform_get_mmap_data_fd(void *platform __unused, int fe_dev __unused, int dir __unused,
1350                               int *fd __unused, uint32_t *size __unused)
1351 {
1352     return -ENOSYS;
1353 }
1354 
platform_sound_trigger_usecase_needs_event(audio_usecase_t uc_id __unused)1355 bool platform_sound_trigger_usecase_needs_event(audio_usecase_t uc_id __unused)
1356 {
1357     return false;
1358 }
1359 
platform_snd_device_has_speaker(snd_device_t dev __unused)1360 bool platform_snd_device_has_speaker(snd_device_t dev __unused) {
1361     return false;
1362 }
1363 
platform_set_microphone_characteristic(void * platform __unused,struct audio_microphone_characteristic_t mic __unused)1364 bool platform_set_microphone_characteristic(void *platform __unused,
1365                                             struct audio_microphone_characteristic_t mic __unused) {
1366     return -ENOSYS;
1367 }
1368 
platform_get_microphones(void * platform __unused,struct audio_microphone_characteristic_t * mic_array __unused,size_t * mic_count __unused)1369 int platform_get_microphones(void *platform __unused,
1370                              struct audio_microphone_characteristic_t *mic_array __unused,
1371                              size_t *mic_count __unused) {
1372     return -ENOSYS;
1373 }
1374 
platform_get_active_microphones(void * platform __unused,unsigned int channels __unused,audio_usecase_t usecase __unused,struct audio_microphone_characteristic_t * mic_array __unused,size_t * mic_count __unused)1375 int platform_get_active_microphones(void *platform __unused, unsigned int channels __unused,
1376                                     audio_usecase_t usecase __unused,
1377                                     struct audio_microphone_characteristic_t *mic_array __unused,
1378                                     size_t *mic_count __unused) {
1379     return -ENOSYS;
1380 }
1381 
platform_set_usb_service_interval(void * platform __unused,bool playback __unused,unsigned long service_interval __unused,bool * reconfig)1382 int platform_set_usb_service_interval(void *platform __unused,
1383                                       bool playback __unused,
1384                                       unsigned long service_interval __unused,
1385                                       bool *reconfig)
1386 {
1387     *reconfig = false;
1388     return 0;
1389 }
1390 
platform_set_backend_cfg(const struct audio_device * adev __unused,snd_device_t snd_device __unused,const struct audio_backend_cfg * backend_cfg __unused)1391 int platform_set_backend_cfg(const struct audio_device* adev __unused,
1392                              snd_device_t snd_device __unused,
1393                              const struct audio_backend_cfg *backend_cfg __unused)
1394 {
1395     return -1;
1396 }
1397 
platform_set_acdb_metainfo_key(void * platform __unused,char * name __unused,int key __unused)1398 int platform_set_acdb_metainfo_key(void *platform __unused,
1399                                    char *name __unused,
1400                                    int key __unused) {
1401     return -ENOSYS;
1402 }
1403