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