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