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