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