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