1/*
2 * Copyright (C) 2013 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 <cutils/log.h>
24#include <cutils/properties.h>
25#include <audio_hw.h>
26#include <platform_api.h>
27#include "platform.h"
28
29#define LIB_ACDB_LOADER "libacdbloader.so"
30#define LIB_CSD_CLIENT "libcsd-client.so"
31
32#define DUALMIC_CONFIG_NONE 0      /* Target does not contain 2 mics */
33#define DUALMIC_CONFIG_ENDFIRE 1
34#define DUALMIC_CONFIG_BROADSIDE 2
35
36/*
37 * This is the sysfs path for the HDMI audio data block
38 */
39#define AUDIO_DATA_BLOCK_PATH "/sys/class/graphics/fb1/audio_data_block"
40#define MIXER_XML_PATH "/system/etc/mixer_paths.xml"
41
42/*
43 * This file will have a maximum of 38 bytes:
44 *
45 * 4 bytes: number of audio blocks
46 * 4 bytes: total length of Short Audio Descriptor (SAD) blocks
47 * Maximum 10 * 3 bytes: SAD blocks
48 */
49#define MAX_SAD_BLOCKS      10
50#define SAD_BLOCK_SIZE      3
51
52/* EDID format ID for LPCM audio */
53#define EDID_FORMAT_LPCM    1
54
55struct audio_block_header
56{
57    int reserved;
58    int length;
59};
60
61
62typedef void (*acdb_deallocate_t)();
63typedef int  (*acdb_init_t)();
64typedef void (*acdb_send_audio_cal_t)(int, int);
65typedef void (*acdb_send_voice_cal_t)(int, int);
66
67typedef int (*csd_client_init_t)();
68typedef int (*csd_client_deinit_t)();
69typedef int (*csd_disable_device_t)();
70typedef int (*csd_enable_device_t)(int, int, uint32_t);
71typedef int (*csd_volume_t)(int);
72typedef int (*csd_mic_mute_t)(int);
73typedef int (*csd_start_voice_t)();
74typedef int (*csd_stop_voice_t)();
75
76
77/* Audio calibration related functions */
78struct platform_data {
79    struct audio_device *adev;
80    bool fluence_in_spkr_mode;
81    bool fluence_in_voice_call;
82    bool fluence_in_voice_rec;
83    int  dualmic_config;
84
85    void *acdb_handle;
86    acdb_init_t acdb_init;
87    acdb_deallocate_t acdb_deallocate;
88    acdb_send_audio_cal_t acdb_send_audio_cal;
89    acdb_send_voice_cal_t acdb_send_voice_cal;
90
91    /* CSD Client related functions for voice call */
92    void *csd_client;
93    csd_client_init_t csd_client_init;
94    csd_client_deinit_t csd_client_deinit;
95    csd_disable_device_t csd_disable_device;
96    csd_enable_device_t csd_enable_device;
97    csd_volume_t csd_volume;
98    csd_mic_mute_t csd_mic_mute;
99    csd_start_voice_t csd_start_voice;
100    csd_stop_voice_t csd_stop_voice;
101};
102
103static const int pcm_device_table[AUDIO_USECASE_MAX][2] = {
104    [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
105    [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {14, 14},
106    [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
107    [USECASE_AUDIO_RECORD] = {0, 0},
108    [USECASE_AUDIO_RECORD_LOW_LATENCY] = {14, 14},
109    [USECASE_VOICE_CALL] = {12, 12},
110};
111
112/* Array to store sound devices */
113static const char * const device_table[SND_DEVICE_MAX] = {
114    [SND_DEVICE_NONE] = "none",
115    /* Playback sound devices */
116    [SND_DEVICE_OUT_HANDSET] = "handset",
117    [SND_DEVICE_OUT_SPEAKER] = "speaker",
118    [SND_DEVICE_OUT_SPEAKER_REVERSE] = "speaker-reverse",
119    [SND_DEVICE_OUT_HEADPHONES] = "headphones",
120    [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
121    [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
122    [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
123    [SND_DEVICE_OUT_HDMI] = "hdmi",
124    [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
125    [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
126    [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = "voice-handset-tmus",
127    [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
128    [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
129    [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
130
131    /* Capture sound devices */
132    [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
133    [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
134    [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
135    [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic",
136    [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "voice-speaker-mic",
137    [SND_DEVICE_IN_HEADSET_MIC_AEC] = "headset-mic",
138    [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
139    [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
140    [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
141    [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
142    [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
143    [SND_DEVICE_IN_VOICE_DMIC_EF] = "voice-dmic-ef",
144    [SND_DEVICE_IN_VOICE_DMIC_BS] = "voice-dmic-bs",
145    [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = "voice-dmic-ef-tmus",
146    [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = "voice-speaker-dmic-ef",
147    [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = "voice-speaker-dmic-bs",
148    [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
149    [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
150    [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
151    [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
152    [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = "voice-rec-dmic-ef",
153    [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = "voice-rec-dmic-bs",
154    [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = "voice-rec-dmic-ef-fluence",
155    [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = "voice-rec-dmic-bs-fluence",
156};
157
158/* ACDB IDs (audio DSP path configuration IDs) for each sound device */
159static const int acdb_device_table[SND_DEVICE_MAX] = {
160    [SND_DEVICE_NONE] = -1,
161    [SND_DEVICE_OUT_HANDSET] = 7,
162    [SND_DEVICE_OUT_SPEAKER] = 14,
163    [SND_DEVICE_OUT_SPEAKER_REVERSE] = 14,
164    [SND_DEVICE_OUT_HEADPHONES] = 10,
165    [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
166    [SND_DEVICE_OUT_VOICE_SPEAKER] = 14,
167    [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
168    [SND_DEVICE_OUT_HDMI] = 18,
169    [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 14,
170    [SND_DEVICE_OUT_BT_SCO] = 22,
171    [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = 81,
172    [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
173    [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
174    [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
175
176    [SND_DEVICE_IN_HANDSET_MIC] = 4,
177    [SND_DEVICE_IN_SPEAKER_MIC] = 4,
178    [SND_DEVICE_IN_HEADSET_MIC] = 8,
179    [SND_DEVICE_IN_HANDSET_MIC_AEC] = 40,
180    [SND_DEVICE_IN_SPEAKER_MIC_AEC] = 42,
181    [SND_DEVICE_IN_HEADSET_MIC_AEC] = 47,
182    [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
183    [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8,
184    [SND_DEVICE_IN_HDMI_MIC] = 4,
185    [SND_DEVICE_IN_BT_SCO_MIC] = 21,
186    [SND_DEVICE_IN_CAMCORDER_MIC] = 61,
187    [SND_DEVICE_IN_VOICE_DMIC_EF] = 6,
188    [SND_DEVICE_IN_VOICE_DMIC_BS] = 5,
189    [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = 91,
190    [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = 13,
191    [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = 12,
192    [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
193    [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
194    [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
195    [SND_DEVICE_IN_VOICE_REC_MIC] = 62,
196    /* TODO: Update with proper acdb ids */
197    [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = 62,
198    [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = 62,
199    [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = 6,
200    [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = 5,
201};
202
203#define DEEP_BUFFER_PLATFORM_DELAY (29*1000LL)
204#define LOW_LATENCY_PLATFORM_DELAY (13*1000LL)
205
206static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
207static bool is_tmus = false;
208
209static void check_operator()
210{
211    char value[PROPERTY_VALUE_MAX];
212    int mccmnc;
213    property_get("gsm.sim.operator.numeric",value,"0");
214    mccmnc = atoi(value);
215    ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
216    switch(mccmnc) {
217    /* TMUS MCC(310), MNC(490, 260, 026) */
218    case 310490:
219    case 310260:
220    case 310026:
221        is_tmus = true;
222        break;
223    }
224}
225
226bool is_operator_tmus()
227{
228    pthread_once(&check_op_once_ctl, check_operator);
229    return is_tmus;
230}
231
232static int set_echo_reference(struct mixer *mixer, const char* ec_ref)
233{
234    struct mixer_ctl *ctl;
235    const char *mixer_ctl_name = "EC_REF_RX";
236
237    ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
238    if (!ctl) {
239        ALOGE("%s: Could not get ctl for mixer cmd - %s",
240              __func__, mixer_ctl_name);
241        return -EINVAL;
242    }
243    ALOGV("Setting EC Reference: %s", ec_ref);
244    mixer_ctl_set_enum_by_string(ctl, ec_ref);
245    return 0;
246}
247
248void *platform_init(struct audio_device *adev)
249{
250    char platform[PROPERTY_VALUE_MAX];
251    char baseband[PROPERTY_VALUE_MAX];
252    char value[PROPERTY_VALUE_MAX];
253    struct platform_data *my_data;
254
255    adev->mixer = mixer_open(MIXER_CARD);
256
257    if (!adev->mixer) {
258        ALOGE("Unable to open the mixer, aborting.");
259        return NULL;
260    }
261
262    adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
263    if (!adev->audio_route) {
264        ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
265        return NULL;
266    }
267
268    my_data = calloc(1, sizeof(struct platform_data));
269
270    my_data->adev = adev;
271    my_data->dualmic_config = DUALMIC_CONFIG_NONE;
272    my_data->fluence_in_spkr_mode = false;
273    my_data->fluence_in_voice_call = false;
274    my_data->fluence_in_voice_rec = false;
275
276    property_get("persist.audio.dualmic.config",value,"");
277    if (!strcmp("broadside", value)) {
278        my_data->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
279        adev->acdb_settings |= DMIC_FLAG;
280    } else if (!strcmp("endfire", value)) {
281        my_data->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
282        adev->acdb_settings |= DMIC_FLAG;
283    }
284
285    if (my_data->dualmic_config != DUALMIC_CONFIG_NONE) {
286        property_get("persist.audio.fluence.voicecall",value,"");
287        if (!strcmp("true", value)) {
288            my_data->fluence_in_voice_call = true;
289        }
290
291        property_get("persist.audio.fluence.voicerec",value,"");
292        if (!strcmp("true", value)) {
293            my_data->fluence_in_voice_rec = true;
294        }
295
296        property_get("persist.audio.fluence.speaker",value,"");
297        if (!strcmp("true", value)) {
298            my_data->fluence_in_spkr_mode = true;
299        }
300    }
301
302    my_data->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
303    if (my_data->acdb_handle == NULL) {
304        ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
305    } else {
306        ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
307        my_data->acdb_deallocate = (acdb_deallocate_t)dlsym(my_data->acdb_handle,
308                                                    "acdb_loader_deallocate_ACDB");
309        my_data->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(my_data->acdb_handle,
310                                                    "acdb_loader_send_audio_cal");
311        if (!my_data->acdb_send_audio_cal)
312            ALOGW("%s: Could not find the symbol acdb_send_audio_cal from %s",
313                  __func__, LIB_ACDB_LOADER);
314        my_data->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(my_data->acdb_handle,
315                                                    "acdb_loader_send_voice_cal");
316        my_data->acdb_init = (acdb_init_t)dlsym(my_data->acdb_handle,
317                                                    "acdb_loader_init_ACDB");
318        if (my_data->acdb_init == NULL)
319            ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
320        else
321            my_data->acdb_init();
322    }
323
324    /* If platform is Fusion3, load CSD Client specific symbols
325     * Voice call is handled by MDM and apps processor talks to
326     * MDM through CSD Client
327     */
328    property_get("ro.board.platform", platform, "");
329    property_get("ro.baseband", baseband, "");
330    if (!strcmp("msm8960", platform) && !strcmp("mdm", baseband)) {
331        my_data->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
332        if (my_data->csd_client == NULL)
333            ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
334    }
335
336    if (my_data->csd_client) {
337        ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
338        my_data->csd_client_deinit = (csd_client_deinit_t)dlsym(my_data->csd_client,
339                                                    "csd_client_deinit");
340        my_data->csd_disable_device = (csd_disable_device_t)dlsym(my_data->csd_client,
341                                                    "csd_client_disable_device");
342        my_data->csd_enable_device = (csd_enable_device_t)dlsym(my_data->csd_client,
343                                                    "csd_client_enable_device");
344        my_data->csd_start_voice = (csd_start_voice_t)dlsym(my_data->csd_client,
345                                                    "csd_client_start_voice");
346        my_data->csd_stop_voice = (csd_stop_voice_t)dlsym(my_data->csd_client,
347                                                    "csd_client_stop_voice");
348        my_data->csd_volume = (csd_volume_t)dlsym(my_data->csd_client,
349                                                    "csd_client_volume");
350        my_data->csd_mic_mute = (csd_mic_mute_t)dlsym(my_data->csd_client,
351                                                    "csd_client_mic_mute");
352        my_data->csd_client_init = (csd_client_init_t)dlsym(my_data->csd_client,
353                                                    "csd_client_init");
354
355        if (my_data->csd_client_init == NULL) {
356            ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror());
357        } else {
358            my_data->csd_client_init();
359        }
360    }
361
362    return my_data;
363}
364
365void platform_deinit(void *platform)
366{
367    free(platform);
368}
369
370const char *platform_get_snd_device_name(snd_device_t snd_device)
371{
372    if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX)
373        return device_table[snd_device];
374    else
375        return "";
376}
377
378void platform_add_backend_name(char *mixer_path, snd_device_t snd_device)
379{
380    if (snd_device == SND_DEVICE_IN_BT_SCO_MIC)
381        strcat(mixer_path, " bt-sco");
382    else if(snd_device == SND_DEVICE_OUT_BT_SCO)
383        strcat(mixer_path, " bt-sco");
384    else if (snd_device == SND_DEVICE_OUT_HDMI)
385        strcat(mixer_path, " hdmi");
386    else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HDMI)
387        strcat(mixer_path, " speaker-and-hdmi");
388}
389
390int platform_get_pcm_device_id(audio_usecase_t usecase, int device_type)
391{
392    int device_id;
393    if (device_type == PCM_PLAYBACK)
394        device_id = pcm_device_table[usecase][0];
395    else
396        device_id = pcm_device_table[usecase][1];
397    return device_id;
398}
399
400int platform_send_audio_calibration(void *platform, snd_device_t snd_device)
401{
402    struct platform_data *my_data = (struct platform_data *)platform;
403    int acdb_dev_id, acdb_dev_type;
404
405    acdb_dev_id = acdb_device_table[snd_device];
406    if (acdb_dev_id < 0) {
407        ALOGE("%s: Could not find acdb id for device(%d)",
408              __func__, snd_device);
409        return -EINVAL;
410    }
411    if (my_data->acdb_send_audio_cal) {
412        ("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
413              __func__, snd_device, acdb_dev_id);
414        if (snd_device >= SND_DEVICE_OUT_BEGIN &&
415                snd_device < SND_DEVICE_OUT_END)
416            acdb_dev_type = ACDB_DEV_TYPE_OUT;
417        else
418            acdb_dev_type = ACDB_DEV_TYPE_IN;
419        my_data->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
420    }
421    return 0;
422}
423
424int platform_switch_voice_call_device_pre(void *platform)
425{
426    struct platform_data *my_data = (struct platform_data *)platform;
427    int ret = 0;
428
429    if (my_data->csd_client != NULL) {
430        /* This must be called before disabling the mixer controls on APQ side */
431        if (my_data->csd_disable_device == NULL) {
432            ALOGE("%s: dlsym error for csd_disable_device", __func__);
433        } else {
434            ret = my_data->csd_disable_device();
435            if (ret < 0) {
436                ALOGE("%s: csd_client_disable_device, failed, error %d",
437                      __func__, ret);
438            }
439        }
440    }
441    return ret;
442}
443
444int platform_switch_voice_call_device_post(void *platform,
445                                           snd_device_t out_snd_device,
446                                           snd_device_t in_snd_device)
447{
448    struct platform_data *my_data = (struct platform_data *)platform;
449    int acdb_rx_id, acdb_tx_id;
450    int ret = 0;
451
452    if (my_data->csd_client) {
453        if (my_data->csd_enable_device == NULL) {
454            ALOGE("%s: dlsym error for csd_enable_device",
455                  __func__);
456        } else {
457            acdb_rx_id = acdb_device_table[out_snd_device];
458            acdb_tx_id = acdb_device_table[in_snd_device];
459
460            if (acdb_rx_id > 0 || acdb_tx_id > 0) {
461                ret = my_data->csd_enable_device(acdb_rx_id, acdb_tx_id,
462                                                    my_data->adev->acdb_settings);
463                if (ret < 0) {
464                    ALOGE("%s: csd_enable_device, failed, error %d",
465                          __func__, ret);
466                }
467            } else {
468                ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
469                      acdb_rx_id, acdb_tx_id);
470            }
471        }
472    }
473
474    return ret;
475}
476
477int platform_start_voice_call(void *platform)
478{
479    struct platform_data *my_data = (struct platform_data *)platform;
480    int ret = 0;
481
482    if (my_data->csd_client) {
483        if (my_data->csd_start_voice == NULL) {
484            ALOGE("dlsym error for csd_client_start_voice");
485            ret = -ENOSYS;
486        } else {
487            ret = my_data->csd_start_voice();
488            if (ret < 0) {
489                ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
490            }
491        }
492    }
493
494    return ret;
495}
496
497int platform_stop_voice_call(void *platform)
498{
499    struct platform_data *my_data = (struct platform_data *)platform;
500    int ret = 0;
501
502    if (my_data->csd_client) {
503        if (my_data->csd_stop_voice == NULL) {
504            ALOGE("dlsym error for csd_stop_voice");
505        } else {
506            ret = my_data->csd_stop_voice();
507            if (ret < 0) {
508                ALOGE("%s: csd_stop_voice error %d\n", __func__, ret);
509            }
510        }
511    }
512
513    return ret;
514}
515
516int platform_set_voice_volume(void *platform, int volume)
517{
518    struct platform_data *my_data = (struct platform_data *)platform;
519    int ret = 0;
520
521    if (my_data->csd_client) {
522        if (my_data->csd_volume == NULL) {
523            ALOGE("%s: dlsym error for csd_volume", __func__);
524        } else {
525            ret = my_data->csd_volume(volume);
526            if (ret < 0) {
527                ALOGE("%s: csd_volume error %d", __func__, ret);
528            }
529        }
530    } else {
531        ALOGE("%s: No CSD Client present", __func__);
532    }
533
534    return ret;
535}
536
537int platform_set_mic_mute(void *platform, bool state)
538{
539    struct platform_data *my_data = (struct platform_data *)platform;
540    int ret = 0;
541
542    if (my_data->adev->mode == AUDIO_MODE_IN_CALL) {
543        if (my_data->csd_client) {
544            if (my_data->csd_mic_mute == NULL) {
545                ALOGE("%s: dlsym error for csd_mic_mute", __func__);
546            } else {
547                ret = my_data->csd_mic_mute(state);
548                if (ret < 0) {
549                    ALOGE("%s: csd_mic_mute error %d", __func__, ret);
550                }
551            }
552        } else {
553            ALOGE("%s: No CSD Client present", __func__);
554        }
555    }
556
557    return ret;
558}
559
560snd_device_t platform_get_output_snd_device(void *platform, audio_devices_t devices)
561{
562    struct platform_data *my_data = (struct platform_data *)platform;
563    struct audio_device *adev = my_data->adev;
564    audio_mode_t mode = adev->mode;
565    snd_device_t snd_device = SND_DEVICE_NONE;
566
567    ALOGV("%s: enter: output devices(%#x)", __func__, devices);
568    if (devices == AUDIO_DEVICE_NONE ||
569        devices & AUDIO_DEVICE_BIT_IN) {
570        ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
571        goto exit;
572    }
573
574    if (mode == AUDIO_MODE_IN_CALL) {
575        if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
576            devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
577            if (adev->tty_mode == TTY_MODE_FULL)
578                snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
579            else if (adev->tty_mode == TTY_MODE_VCO)
580                snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
581            else if (adev->tty_mode == TTY_MODE_HCO)
582                snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
583            else
584                snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
585        } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
586            snd_device = SND_DEVICE_OUT_BT_SCO;
587        } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
588            snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
589        } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
590            if (is_operator_tmus())
591                snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
592            else
593                snd_device = SND_DEVICE_OUT_HANDSET;
594        }
595        if (snd_device != SND_DEVICE_NONE) {
596            goto exit;
597        }
598    }
599
600    if (popcount(devices) == 2) {
601        if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
602                        AUDIO_DEVICE_OUT_SPEAKER)) {
603            snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
604        } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
605                               AUDIO_DEVICE_OUT_SPEAKER)) {
606            snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
607        } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
608                               AUDIO_DEVICE_OUT_SPEAKER)) {
609            snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
610        } else {
611            ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
612            goto exit;
613        }
614        if (snd_device != SND_DEVICE_NONE) {
615            goto exit;
616        }
617    }
618
619    if (popcount(devices) != 1) {
620        ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
621        goto exit;
622    }
623
624    if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
625        devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
626        snd_device = SND_DEVICE_OUT_HEADPHONES;
627    } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
628        if (adev->speaker_lr_swap)
629            snd_device = SND_DEVICE_OUT_SPEAKER_REVERSE;
630        else
631            snd_device = SND_DEVICE_OUT_SPEAKER;
632    } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
633        snd_device = SND_DEVICE_OUT_BT_SCO;
634    } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
635        snd_device = SND_DEVICE_OUT_HDMI ;
636    } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
637        snd_device = SND_DEVICE_OUT_HANDSET;
638    } else {
639        ALOGE("%s: Unknown device(s) %#x", __func__, devices);
640    }
641exit:
642    ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
643    return snd_device;
644}
645
646snd_device_t platform_get_input_snd_device(void *platform, audio_devices_t out_device)
647{
648    struct platform_data *my_data = (struct platform_data *)platform;
649    struct audio_device *adev = my_data->adev;
650    audio_source_t  source = (adev->active_input == NULL) ?
651                                AUDIO_SOURCE_DEFAULT : adev->active_input->source;
652
653    audio_mode_t    mode   = adev->mode;
654    audio_devices_t in_device = ((adev->active_input == NULL) ?
655                                    AUDIO_DEVICE_NONE : adev->active_input->device)
656                                & ~AUDIO_DEVICE_BIT_IN;
657    audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
658                                AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
659    snd_device_t snd_device = SND_DEVICE_NONE;
660
661    ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
662          __func__, out_device, in_device);
663    if (mode == AUDIO_MODE_IN_CALL) {
664        if (out_device == AUDIO_DEVICE_NONE) {
665            ALOGE("%s: No output device set for voice call", __func__);
666            goto exit;
667        }
668        if (adev->tty_mode != TTY_MODE_OFF) {
669            if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
670                out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
671                switch (adev->tty_mode) {
672                case TTY_MODE_FULL:
673                    snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
674                    break;
675                case TTY_MODE_VCO:
676                    snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
677                    break;
678                case TTY_MODE_HCO:
679                    snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
680                    break;
681                default:
682                    ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode);
683                }
684                goto exit;
685            }
686        }
687        if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
688            out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
689            if (my_data->fluence_in_voice_call == false) {
690                snd_device = SND_DEVICE_IN_HANDSET_MIC;
691            } else {
692                if (my_data->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
693                    if (is_operator_tmus())
694                        snd_device = SND_DEVICE_IN_VOICE_DMIC_EF_TMUS;
695                    else
696                        snd_device = SND_DEVICE_IN_VOICE_DMIC_EF;
697                } else if(my_data->dualmic_config == DUALMIC_CONFIG_BROADSIDE)
698                    snd_device = SND_DEVICE_IN_VOICE_DMIC_BS;
699                else
700                    snd_device = SND_DEVICE_IN_HANDSET_MIC;
701            }
702        } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
703            snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
704        } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
705            snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
706        } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
707            if (my_data->fluence_in_voice_call && my_data->fluence_in_spkr_mode &&
708                    my_data->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
709                snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF;
710            } else if (my_data->fluence_in_voice_call && my_data->fluence_in_spkr_mode &&
711                    my_data->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
712                snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS;
713            } else {
714                snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
715            }
716        }
717    } else if (source == AUDIO_SOURCE_CAMCORDER) {
718        if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
719            in_device & AUDIO_DEVICE_IN_BACK_MIC) {
720            snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
721        }
722    } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
723        if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
724            if (my_data->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
725                if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
726                    snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF;
727                else if (my_data->fluence_in_voice_rec)
728                    snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE;
729            } else if (my_data->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
730                if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
731                    snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS;
732                else if (my_data->fluence_in_voice_rec)
733                    snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE;
734            }
735
736            if (snd_device == SND_DEVICE_NONE) {
737                snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
738            }
739        }
740    } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
741        if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
742            in_device = AUDIO_DEVICE_IN_BACK_MIC;
743        if (adev->active_input) {
744            if (adev->active_input->enable_aec) {
745                if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
746                    snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
747                } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
748                    snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
749                } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
750                    snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
751                }
752                set_echo_reference(adev->mixer, "SLIM_RX");
753            } else
754                set_echo_reference(adev->mixer, "NONE");
755        }
756    } else if (source == AUDIO_SOURCE_DEFAULT) {
757        goto exit;
758    }
759
760
761    if (snd_device != SND_DEVICE_NONE) {
762        goto exit;
763    }
764
765    if (in_device != AUDIO_DEVICE_NONE &&
766            !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
767            !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
768        if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
769            snd_device = SND_DEVICE_IN_HANDSET_MIC;
770        } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
771            snd_device = SND_DEVICE_IN_SPEAKER_MIC;
772        } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
773            snd_device = SND_DEVICE_IN_HEADSET_MIC;
774        } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
775            snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
776        } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
777            snd_device = SND_DEVICE_IN_HDMI_MIC;
778        } else {
779            ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
780            ALOGW("%s: Using default handset-mic", __func__);
781            snd_device = SND_DEVICE_IN_HANDSET_MIC;
782        }
783    } else {
784        if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
785            snd_device = SND_DEVICE_IN_HANDSET_MIC;
786        } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
787            snd_device = SND_DEVICE_IN_HEADSET_MIC;
788        } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
789            snd_device = SND_DEVICE_IN_SPEAKER_MIC;
790        } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
791            snd_device = SND_DEVICE_IN_HANDSET_MIC;
792        } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
793            snd_device = SND_DEVICE_IN_BT_SCO_MIC;
794        } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
795            snd_device = SND_DEVICE_IN_HDMI_MIC;
796        } else {
797            ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
798            ALOGW("%s: Using default handset-mic", __func__);
799            snd_device = SND_DEVICE_IN_HANDSET_MIC;
800        }
801    }
802exit:
803    ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
804    return snd_device;
805}
806
807int platform_set_hdmi_channels(void *platform,  int channel_count)
808{
809    struct platform_data *my_data = (struct platform_data *)platform;
810    struct audio_device *adev = my_data->adev;
811    struct mixer_ctl *ctl;
812    const char *channel_cnt_str = NULL;
813    const char *mixer_ctl_name = "HDMI_RX Channels";
814    switch (channel_count) {
815    case 8:
816        channel_cnt_str = "Eight"; break;
817    case 7:
818        channel_cnt_str = "Seven"; break;
819    case 6:
820        channel_cnt_str = "Six"; break;
821    case 5:
822        channel_cnt_str = "Five"; break;
823    case 4:
824        channel_cnt_str = "Four"; break;
825    case 3:
826        channel_cnt_str = "Three"; break;
827    default:
828        channel_cnt_str = "Two"; break;
829    }
830    ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
831    if (!ctl) {
832        ALOGE("%s: Could not get ctl for mixer cmd - %s",
833              __func__, mixer_ctl_name);
834        return -EINVAL;
835    }
836    ALOGV("HDMI channel count: %s", channel_cnt_str);
837    mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
838    return 0;
839}
840
841int platform_edid_get_max_channels(void *platform)
842{
843    FILE *file;
844    struct audio_block_header header;
845    char block[MAX_SAD_BLOCKS * SAD_BLOCK_SIZE];
846    char *sad = block;
847    int num_audio_blocks;
848    int channel_count;
849    int max_channels = 0;
850    int i;
851
852    file = fopen(AUDIO_DATA_BLOCK_PATH, "rb");
853    if (file == NULL) {
854        ALOGE("Unable to open '%s'", AUDIO_DATA_BLOCK_PATH);
855        return 0;
856    }
857
858    /* Read audio block header */
859    fread(&header, 1, sizeof(header), file);
860
861    /* Read SAD blocks, clamping the maximum size for safety */
862    if (header.length > (int)sizeof(block))
863        header.length = (int)sizeof(block);
864    fread(&block, header.length, 1, file);
865
866    fclose(file);
867
868    /* Calculate the number of SAD blocks */
869    num_audio_blocks = header.length / SAD_BLOCK_SIZE;
870
871    for (i = 0; i < num_audio_blocks; i++) {
872        /* Only consider LPCM blocks */
873        if ((sad[0] >> 3) != EDID_FORMAT_LPCM)
874            continue;
875
876        channel_count = (sad[0] & 0x7) + 1;
877        if (channel_count > max_channels)
878            max_channels = channel_count;
879
880        /* Advance to next block */
881        sad += 3;
882    }
883
884    return max_channels;
885}
886
887/* Delay in Us */
888int64_t platform_render_latency(audio_usecase_t usecase)
889{
890    switch (usecase) {
891        case USECASE_AUDIO_PLAYBACK_DEEP_BUFFER:
892            return DEEP_BUFFER_PLATFORM_DELAY;
893        case USECASE_AUDIO_PLAYBACK_LOW_LATENCY:
894            return LOW_LATENCY_PLATFORM_DELAY;
895        default:
896            return 0;
897    }
898}
899