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