blob: 3d13c7df9dab611772b5fd0e9cba9d997fa31695 [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
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -070029#define MIXER_XML_PATH "/system/etc/mixer_paths.xml"
Eric Laurentb23d5282013-05-14 15:27:20 -070030#define LIB_ACDB_LOADER "libacdbloader.so"
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -070031#define AUDIO_DATA_BLOCK_MIXER_CTL "HDMI EDID"
Eric Laurentb23d5282013-05-14 15:27:20 -070032
Eric Laurentb23d5282013-05-14 15:27:20 -070033/*
Eric Laurentb23d5282013-05-14 15:27:20 -070034 * This file will have a maximum of 38 bytes:
35 *
36 * 4 bytes: number of audio blocks
37 * 4 bytes: total length of Short Audio Descriptor (SAD) blocks
38 * Maximum 10 * 3 bytes: SAD blocks
39 */
40#define MAX_SAD_BLOCKS 10
41#define SAD_BLOCK_SIZE 3
42
43/* EDID format ID for LPCM audio */
44#define EDID_FORMAT_LPCM 1
45
sangwoo1b9f4b32013-06-21 18:22:55 -070046/* Retry for delay in FW loading*/
47#define RETRY_NUMBER 10
48#define RETRY_US 500000
49
sangwoo53b2cf02013-07-25 19:18:44 -070050#define MAX_VOL_INDEX 5
51#define MIN_VOL_INDEX 0
52#define percent_to_index(val, min, max) \
53 ((val) * ((max) - (min)) * 0.01 + (min) + .5)
54
Eric Laurentb23d5282013-05-14 15:27:20 -070055struct audio_block_header
56{
57 int reserved;
58 int length;
59};
60
61typedef void (*acdb_deallocate_t)();
62typedef int (*acdb_init_t)();
63typedef void (*acdb_send_audio_cal_t)(int, int);
64typedef void (*acdb_send_voice_cal_t)(int, int);
65
66/* Audio calibration related functions */
67struct platform_data {
68 struct audio_device *adev;
69 bool fluence_in_spkr_mode;
70 bool fluence_in_voice_call;
71 bool fluence_in_voice_rec;
Mingming Yin8e5a4f62013-10-07 15:23:41 -070072 int fluence_type;
Eric Laurentb23d5282013-05-14 15:27:20 -070073
74 void *acdb_handle;
75 acdb_init_t acdb_init;
76 acdb_deallocate_t acdb_deallocate;
77 acdb_send_audio_cal_t acdb_send_audio_cal;
78 acdb_send_voice_cal_t acdb_send_voice_cal;
79};
80
81static const int pcm_device_table[AUDIO_USECASE_MAX][2] = {
Mingming Yin8e5a4f62013-10-07 15:23:41 -070082 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {DEEP_BUFFER_PCM_DEVICE,
83 DEEP_BUFFER_PCM_DEVICE},
84 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {LOWLATENCY_PCM_DEVICE,
85 LOWLATENCY_PCM_DEVICE},
86 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {MULTI_CHANNEL_PCM_DEVICE,
87 MULTI_CHANNEL_PCM_DEVICE},
88 [USECASE_AUDIO_RECORD] = {DEEP_BUFFER_PCM_DEVICE, DEEP_BUFFER_PCM_DEVICE},
89 [USECASE_AUDIO_RECORD_LOW_LATENCY] = {LOWLATENCY_PCM_DEVICE,
90 LOWLATENCY_PCM_DEVICE},
91 [USECASE_VOICE_CALL] = {VOICE_CALL_PCM_DEVICE, VOICE_CALL_PCM_DEVICE},
Eric Laurentb23d5282013-05-14 15:27:20 -070092};
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",
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700126 [SND_DEVICE_IN_VOICE_DMIC] = "voice-dmic-ef",
127 [SND_DEVICE_IN_VOICE_DMIC_TMUS] = "voice-dmic-ef-tmus",
128 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC] = "voice-speaker-dmic-ef",
Eric Laurentb23d5282013-05-14 15:27:20 -0700129 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
130 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
131 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
132 [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700133 [SND_DEVICE_IN_VOICE_REC_DMIC] = "voice-rec-dmic-ef",
134 [SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE] = "voice-rec-dmic-ef-fluence",
Eric Laurentb23d5282013-05-14 15:27:20 -0700135};
136
137/* ACDB IDs (audio DSP path configuration IDs) for each sound device */
138static const int acdb_device_table[SND_DEVICE_MAX] = {
139 [SND_DEVICE_NONE] = -1,
140 [SND_DEVICE_OUT_HANDSET] = 7,
141 [SND_DEVICE_OUT_SPEAKER] = 15,
142 [SND_DEVICE_OUT_SPEAKER_REVERSE] = 15,
143 [SND_DEVICE_OUT_HEADPHONES] = 10,
144 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
145 [SND_DEVICE_OUT_VOICE_HANDSET] = 7,
146 [SND_DEVICE_OUT_VOICE_SPEAKER] = 15,
147 [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
148 [SND_DEVICE_OUT_HDMI] = 18,
149 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 15,
150 [SND_DEVICE_OUT_BT_SCO] = 22,
sangwooc69476f2013-07-26 16:57:26 -0700151 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = 88,
Eric Laurentb23d5282013-05-14 15:27:20 -0700152 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
153 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
154 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
155
156 [SND_DEVICE_IN_HANDSET_MIC] = 4,
157 [SND_DEVICE_IN_SPEAKER_MIC] = 4, /* ToDo: Check if this needs to changed to 11 */
158 [SND_DEVICE_IN_HEADSET_MIC] = 8,
159 [SND_DEVICE_IN_HANDSET_MIC_AEC] = 40,
160 [SND_DEVICE_IN_SPEAKER_MIC_AEC] = 42,
161 [SND_DEVICE_IN_HEADSET_MIC_AEC] = 47,
162 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
163 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8,
164 [SND_DEVICE_IN_HDMI_MIC] = 4,
165 [SND_DEVICE_IN_BT_SCO_MIC] = 21,
166 [SND_DEVICE_IN_CAMCORDER_MIC] = 61,
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700167 [SND_DEVICE_IN_VOICE_DMIC] = 41,
168 [SND_DEVICE_IN_VOICE_DMIC_TMUS] = 89,
169 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC] = 43,
Eric Laurentb23d5282013-05-14 15:27:20 -0700170 [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 */
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700175 [SND_DEVICE_IN_VOICE_REC_DMIC] = 62,
176 [SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE] = 6,
Eric Laurentb23d5282013-05-14 15:27:20 -0700177};
178
179static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
180static bool is_tmus = false;
181
182static void check_operator()
183{
184 char value[PROPERTY_VALUE_MAX];
185 int mccmnc;
186 property_get("gsm.sim.operator.numeric",value,"0");
187 mccmnc = atoi(value);
188 ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
189 switch(mccmnc) {
190 /* TMUS MCC(310), MNC(490, 260, 026) */
191 case 310490:
192 case 310260:
193 case 310026:
194 is_tmus = true;
195 break;
196 }
197}
198
199bool is_operator_tmus()
200{
201 pthread_once(&check_op_once_ctl, check_operator);
202 return is_tmus;
203}
204
sangwoo53b2cf02013-07-25 19:18:44 -0700205static int set_volume_values(int type, int volume, int* values)
206{
207 values[0] = volume;
208 values[1] = ALL_SESSION_VSID;
209
210 switch(type) {
211 case VOLUME_SET:
212 values[2] = DEFAULT_VOLUME_RAMP_DURATION_MS;
213 break;
214 case MUTE_SET:
215 values[2] = DEFAULT_MUTE_RAMP_DURATION;
216 break;
217 default:
218 return -EINVAL;
219 }
220 return 0;
221}
222
Eric Laurentb23d5282013-05-14 15:27:20 -0700223static int set_echo_reference(struct mixer *mixer, const char* ec_ref)
224{
225 struct mixer_ctl *ctl;
226 const char *mixer_ctl_name = "EC_REF_RX";
227
228 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
229 if (!ctl) {
230 ALOGE("%s: Could not get ctl for mixer cmd - %s",
231 __func__, mixer_ctl_name);
232 return -EINVAL;
233 }
234 ALOGV("Setting EC Reference: %s", ec_ref);
235 mixer_ctl_set_enum_by_string(ctl, ec_ref);
236 return 0;
237}
238
239void *platform_init(struct audio_device *adev)
240{
241 char value[PROPERTY_VALUE_MAX];
242 struct platform_data *my_data;
sangwoo1b9f4b32013-06-21 18:22:55 -0700243 int retry_num = 0;
244
245 adev->mixer = mixer_open(MIXER_CARD);
246
247 while (!adev->mixer && retry_num < RETRY_NUMBER) {
248 usleep(RETRY_US);
249 adev->mixer = mixer_open(MIXER_CARD);
250 retry_num++;
251 }
252
253 if (!adev->mixer) {
254 ALOGE("Unable to open the mixer, aborting.");
255 return NULL;
256 }
257
258 adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
259 if (!adev->audio_route) {
260 ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
261 return NULL;
262 }
Eric Laurentb23d5282013-05-14 15:27:20 -0700263
264 my_data = calloc(1, sizeof(struct platform_data));
265
266 my_data->adev = adev;
Eric Laurentb23d5282013-05-14 15:27:20 -0700267 my_data->fluence_in_spkr_mode = false;
268 my_data->fluence_in_voice_call = false;
269 my_data->fluence_in_voice_rec = false;
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700270 my_data->fluence_type = FLUENCE_NONE;
Eric Laurentb23d5282013-05-14 15:27:20 -0700271
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700272 property_get("ro.qc.sdk.audio.fluencetype", value, "");
273 if (!strncmp("fluencepro", value, sizeof("fluencepro"))) {
274 my_data->fluence_type = FLUENCE_QUAD_MIC;
275 } else if (!strncmp("fluence", value, sizeof("fluence"))) {
276 my_data->fluence_type = FLUENCE_DUAL_MIC;
277 } else {
278 my_data->fluence_type = FLUENCE_NONE;
Eric Laurentb23d5282013-05-14 15:27:20 -0700279 }
280
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700281 if (my_data->fluence_type != FLUENCE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700282 property_get("persist.audio.fluence.voicecall",value,"");
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700283 if (!strncmp("true", value, sizeof("true"))) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700284 my_data->fluence_in_voice_call = true;
285 }
286
287 property_get("persist.audio.fluence.voicerec",value,"");
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700288 if (!strncmp("true", value, sizeof("true"))) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700289 my_data->fluence_in_voice_rec = true;
290 }
291
292 property_get("persist.audio.fluence.speaker",value,"");
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700293 if (!strncmp("true", value, sizeof("true"))) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700294 my_data->fluence_in_spkr_mode = true;
295 }
296 }
297
298 my_data->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
299 if (my_data->acdb_handle == NULL) {
300 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
301 } else {
302 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
303 my_data->acdb_deallocate = (acdb_deallocate_t)dlsym(my_data->acdb_handle,
304 "acdb_loader_deallocate_ACDB");
305 my_data->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(my_data->acdb_handle,
306 "acdb_loader_send_audio_cal");
307 if (!my_data->acdb_send_audio_cal)
308 ALOGW("%s: Could not find the symbol acdb_send_audio_cal from %s",
309 __func__, LIB_ACDB_LOADER);
310 my_data->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(my_data->acdb_handle,
311 "acdb_loader_send_voice_cal");
312 my_data->acdb_init = (acdb_init_t)dlsym(my_data->acdb_handle,
313 "acdb_loader_init_ACDB");
314 if (my_data->acdb_init == NULL)
315 ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
316 else
317 my_data->acdb_init();
318 }
319
320 return my_data;
321}
322
323void platform_deinit(void *platform)
324{
325 free(platform);
326}
327
328const char *platform_get_snd_device_name(snd_device_t snd_device)
329{
330 if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX)
331 return device_table[snd_device];
332 else
333 return "";
334}
335
336void platform_add_backend_name(char *mixer_path, snd_device_t snd_device)
337{
338 if (snd_device == SND_DEVICE_IN_BT_SCO_MIC)
339 strcat(mixer_path, " bt-sco");
340 else if(snd_device == SND_DEVICE_OUT_BT_SCO)
341 strcat(mixer_path, " bt-sco");
342 else if (snd_device == SND_DEVICE_OUT_HDMI)
343 strcat(mixer_path, " hdmi");
344 else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HDMI)
345 strcat(mixer_path, " speaker-and-hdmi");
346}
347
348int platform_get_pcm_device_id(audio_usecase_t usecase, int device_type)
349{
350 int device_id;
351 if (device_type == PCM_PLAYBACK)
352 device_id = pcm_device_table[usecase][0];
353 else
354 device_id = pcm_device_table[usecase][1];
355 return device_id;
356}
357
358int platform_send_audio_calibration(void *platform, snd_device_t snd_device)
359{
360 struct platform_data *my_data = (struct platform_data *)platform;
361 int acdb_dev_id, acdb_dev_type;
362
363 acdb_dev_id = acdb_device_table[snd_device];
364 if (acdb_dev_id < 0) {
365 ALOGE("%s: Could not find acdb id for device(%d)",
366 __func__, snd_device);
367 return -EINVAL;
368 }
369 if (my_data->acdb_send_audio_cal) {
Eric Laurent994a6932013-07-17 11:51:42 -0700370 ("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
Eric Laurentb23d5282013-05-14 15:27:20 -0700371 __func__, snd_device, acdb_dev_id);
372 if (snd_device >= SND_DEVICE_OUT_BEGIN &&
373 snd_device < SND_DEVICE_OUT_END)
374 acdb_dev_type = ACDB_DEV_TYPE_OUT;
375 else
376 acdb_dev_type = ACDB_DEV_TYPE_IN;
377 my_data->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
378 }
379 return 0;
380}
381
382int platform_switch_voice_call_device_pre(void *platform)
383{
384 return 0;
385}
386
387int platform_switch_voice_call_device_post(void *platform,
388 snd_device_t out_snd_device,
389 snd_device_t in_snd_device)
390{
391 struct platform_data *my_data = (struct platform_data *)platform;
392 int acdb_rx_id, acdb_tx_id;
393
394 if (my_data->acdb_send_voice_cal == NULL) {
395 ALOGE("%s: dlsym error for acdb_send_voice_call", __func__);
396 } else {
397 acdb_rx_id = acdb_device_table[out_snd_device];
398 acdb_tx_id = acdb_device_table[in_snd_device];
399
400 if (acdb_rx_id > 0 && acdb_tx_id > 0)
401 my_data->acdb_send_voice_cal(acdb_rx_id, acdb_tx_id);
402 else
403 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
404 acdb_rx_id, acdb_tx_id);
405 }
406
407 return 0;
408}
409
410int platform_start_voice_call(void *platform)
411{
412 return 0;
413}
414
415int platform_stop_voice_call(void *platform)
416{
417 return 0;
418}
419
420int platform_set_voice_volume(void *platform, int volume)
421{
422 struct platform_data *my_data = (struct platform_data *)platform;
423 struct audio_device *adev = my_data->adev;
424 struct mixer_ctl *ctl;
sangwoo53b2cf02013-07-25 19:18:44 -0700425 const char *mixer_ctl_name = "Voice Rx Gain";
426 int values[VOLUME_CTL_PARAM_NUM];
427 int ret = 0;
Eric Laurentb23d5282013-05-14 15:27:20 -0700428
429 // Voice volume levels are mapped to adsp volume levels as follows.
430 // 100 -> 5, 80 -> 4, 60 -> 3, 40 -> 2, 20 -> 1 0 -> 0
431 // But this values don't changed in kernel. So, below change is need.
sangwoo53b2cf02013-07-25 19:18:44 -0700432 volume = (int)percent_to_index(volume, MIN_VOL_INDEX, MAX_VOL_INDEX);
Eric Laurentb23d5282013-05-14 15:27:20 -0700433
434 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
435 if (!ctl) {
436 ALOGE("%s: Could not get ctl for mixer cmd - %s",
437 __func__, mixer_ctl_name);
438 return -EINVAL;
439 }
sangwoo53b2cf02013-07-25 19:18:44 -0700440 ret = set_volume_values(VOLUME_SET, volume, values);
441 if (ret < 0) {
442 ALOGV("%s: failed setting volume by incorrect type", __func__);
443 return -EINVAL;
444 }
445 ret = mixer_ctl_set_array(ctl, values, sizeof(values)/sizeof(int));
446 if (ret < 0) {
447 ALOGV("%s: failed set mixer ctl by %d", __func__, ret);
448 return -EINVAL;
449 }
Eric Laurentb23d5282013-05-14 15:27:20 -0700450
451 return 0;
452}
453
454int platform_set_mic_mute(void *platform, bool state)
455{
456 struct platform_data *my_data = (struct platform_data *)platform;
457 struct audio_device *adev = my_data->adev;
458 struct mixer_ctl *ctl;
459 const char *mixer_ctl_name = "Voice Tx Mute";
sangwoo53b2cf02013-07-25 19:18:44 -0700460 int values[VOLUME_CTL_PARAM_NUM];
461 int ret = 0;
Eric Laurentb23d5282013-05-14 15:27:20 -0700462
463 if (adev->mode == AUDIO_MODE_IN_CALL) {
464 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
465 if (!ctl) {
466 ALOGE("%s: Could not get ctl for mixer cmd - %s",
467 __func__, mixer_ctl_name);
468 return -EINVAL;
469 }
470 ALOGV("Setting mic mute: %d", state);
sangwoo53b2cf02013-07-25 19:18:44 -0700471 ret = set_volume_values(MUTE_SET, state, values);
472 if (ret < 0) {
473 ALOGV("%s: failed setting mute by incorrect type", __func__);
474 return -EINVAL;
475 }
476 ret = mixer_ctl_set_array(ctl, values, sizeof(values)/sizeof(int));
477 if (ret < 0) {
478 ALOGV("%s: failed set mixer ctl by %d", __func__, ret);
479 return -EINVAL;
480 }
Eric Laurentb23d5282013-05-14 15:27:20 -0700481 }
482
483 return 0;
484}
485
486snd_device_t platform_get_output_snd_device(void *platform, audio_devices_t devices)
487{
488 struct platform_data *my_data = (struct platform_data *)platform;
489 struct audio_device *adev = my_data->adev;
490 audio_mode_t mode = adev->mode;
491 snd_device_t snd_device = SND_DEVICE_NONE;
492
493 ALOGV("%s: enter: output devices(%#x)", __func__, devices);
494 if (devices == AUDIO_DEVICE_NONE ||
495 devices & AUDIO_DEVICE_BIT_IN) {
496 ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
497 goto exit;
498 }
499
500 if (mode == AUDIO_MODE_IN_CALL) {
501 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
502 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
503 if (adev->tty_mode == TTY_MODE_FULL)
504 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
505 else if (adev->tty_mode == TTY_MODE_VCO)
506 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
507 else if (adev->tty_mode == TTY_MODE_HCO)
508 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
509 else
510 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
511 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
512 snd_device = SND_DEVICE_OUT_BT_SCO;
513 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
514 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
515 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
516 if (is_operator_tmus())
517 snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
518 else
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700519 snd_device = SND_DEVICE_OUT_VOICE_HANDSET;
Eric Laurentb23d5282013-05-14 15:27:20 -0700520 }
521 if (snd_device != SND_DEVICE_NONE) {
522 goto exit;
523 }
524 }
525
526 if (popcount(devices) == 2) {
527 if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
528 AUDIO_DEVICE_OUT_SPEAKER)) {
529 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
530 } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
531 AUDIO_DEVICE_OUT_SPEAKER)) {
532 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
533 } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
534 AUDIO_DEVICE_OUT_SPEAKER)) {
535 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
536 } else {
537 ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
538 goto exit;
539 }
540 if (snd_device != SND_DEVICE_NONE) {
541 goto exit;
542 }
543 }
544
545 if (popcount(devices) != 1) {
546 ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
547 goto exit;
548 }
549
550 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
551 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
552 snd_device = SND_DEVICE_OUT_HEADPHONES;
553 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
554 if (adev->speaker_lr_swap)
555 snd_device = SND_DEVICE_OUT_SPEAKER_REVERSE;
556 else
557 snd_device = SND_DEVICE_OUT_SPEAKER;
558 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
559 snd_device = SND_DEVICE_OUT_BT_SCO;
560 } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
561 snd_device = SND_DEVICE_OUT_HDMI ;
562 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
563 snd_device = SND_DEVICE_OUT_HANDSET;
564 } else {
565 ALOGE("%s: Unknown device(s) %#x", __func__, devices);
566 }
567exit:
568 ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
569 return snd_device;
570}
571
572snd_device_t platform_get_input_snd_device(void *platform, audio_devices_t out_device)
573{
574 struct platform_data *my_data = (struct platform_data *)platform;
575 struct audio_device *adev = my_data->adev;
576 audio_source_t source = (adev->active_input == NULL) ?
577 AUDIO_SOURCE_DEFAULT : adev->active_input->source;
578
579 audio_mode_t mode = adev->mode;
580 audio_devices_t in_device = ((adev->active_input == NULL) ?
581 AUDIO_DEVICE_NONE : adev->active_input->device)
582 & ~AUDIO_DEVICE_BIT_IN;
583 audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
584 AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
585 snd_device_t snd_device = SND_DEVICE_NONE;
586
587 ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
588 __func__, out_device, in_device);
589 if (mode == AUDIO_MODE_IN_CALL) {
590 if (out_device == AUDIO_DEVICE_NONE) {
591 ALOGE("%s: No output device set for voice call", __func__);
592 goto exit;
593 }
594 if (adev->tty_mode != TTY_MODE_OFF) {
595 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
596 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
597 switch (adev->tty_mode) {
598 case TTY_MODE_FULL:
599 snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
600 break;
601 case TTY_MODE_VCO:
602 snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
603 break;
604 case TTY_MODE_HCO:
605 snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
606 break;
607 default:
608 ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode);
609 }
610 goto exit;
611 }
612 }
613 if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
614 out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700615 if (my_data->fluence_type == FLUENCE_NONE ||
616 my_data->fluence_in_voice_call == false) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700617 snd_device = SND_DEVICE_IN_HANDSET_MIC;
618 } else {
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700619 if (is_operator_tmus())
620 snd_device = SND_DEVICE_IN_VOICE_DMIC_TMUS;
Eric Laurentb23d5282013-05-14 15:27:20 -0700621 else
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700622 snd_device = SND_DEVICE_IN_VOICE_DMIC;
623 adev->acdb_settings |= DMIC_FLAG;
Eric Laurentb23d5282013-05-14 15:27:20 -0700624 }
625 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
626 snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
627 } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
628 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
629 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700630 if (my_data->fluence_type != FLUENCE_NONE &&
631 my_data->fluence_in_voice_call &&
632 my_data->fluence_in_spkr_mode) {
633 if(my_data->fluence_type == FLUENCE_DUAL_MIC) {
634 adev->acdb_settings |= DMIC_FLAG;
635 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC;
636 } else {
637 adev->acdb_settings |= QMIC_FLAG;
638 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_QMIC;
639 }
Eric Laurentb23d5282013-05-14 15:27:20 -0700640 } else {
641 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
642 }
643 }
644 } else if (source == AUDIO_SOURCE_CAMCORDER) {
645 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
646 in_device & AUDIO_DEVICE_IN_BACK_MIC) {
647 snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
648 }
649 } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
650 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700651 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
652 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC;
653 else if (my_data->fluence_in_voice_rec)
654 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE;
Eric Laurentb23d5282013-05-14 15:27:20 -0700655
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700656 if (snd_device == SND_DEVICE_NONE)
Eric Laurentb23d5282013-05-14 15:27:20 -0700657 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
Mingming Yin8e5a4f62013-10-07 15:23:41 -0700658 else
659 adev->acdb_settings |= DMIC_FLAG;
Eric Laurentb23d5282013-05-14 15:27:20 -0700660 }
661 } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
662 if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
663 in_device = AUDIO_DEVICE_IN_BACK_MIC;
664 if (adev->active_input) {
665 if (adev->active_input->enable_aec) {
666 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
667 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
668 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
669 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
670 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
671 snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
672 }
673 set_echo_reference(adev->mixer, "SLIM_RX");
674 } else
675 set_echo_reference(adev->mixer, "NONE");
676 }
677 } else if (source == AUDIO_SOURCE_DEFAULT) {
678 goto exit;
679 }
680
681
682 if (snd_device != SND_DEVICE_NONE) {
683 goto exit;
684 }
685
686 if (in_device != AUDIO_DEVICE_NONE &&
687 !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
688 !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
689 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
690 snd_device = SND_DEVICE_IN_HANDSET_MIC;
691 } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
692 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
693 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
694 snd_device = SND_DEVICE_IN_HEADSET_MIC;
695 } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
696 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
697 } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
698 snd_device = SND_DEVICE_IN_HDMI_MIC;
699 } else {
700 ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
701 ALOGW("%s: Using default handset-mic", __func__);
702 snd_device = SND_DEVICE_IN_HANDSET_MIC;
703 }
704 } else {
705 if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
706 snd_device = SND_DEVICE_IN_HANDSET_MIC;
707 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
708 snd_device = SND_DEVICE_IN_HEADSET_MIC;
709 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
710 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
711 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
712 snd_device = SND_DEVICE_IN_HANDSET_MIC;
713 } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
714 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
715 } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
716 snd_device = SND_DEVICE_IN_HDMI_MIC;
717 } else {
718 ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
719 ALOGW("%s: Using default handset-mic", __func__);
720 snd_device = SND_DEVICE_IN_HANDSET_MIC;
721 }
722 }
723exit:
724 ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
725 return snd_device;
726}
727
728int platform_set_hdmi_channels(void *platform, int channel_count)
729{
730 struct platform_data *my_data = (struct platform_data *)platform;
731 struct audio_device *adev = my_data->adev;
732 struct mixer_ctl *ctl;
733 const char *channel_cnt_str = NULL;
734 const char *mixer_ctl_name = "HDMI_RX Channels";
735 switch (channel_count) {
736 case 8:
737 channel_cnt_str = "Eight"; break;
738 case 7:
739 channel_cnt_str = "Seven"; break;
740 case 6:
741 channel_cnt_str = "Six"; break;
742 case 5:
743 channel_cnt_str = "Five"; break;
744 case 4:
745 channel_cnt_str = "Four"; break;
746 case 3:
747 channel_cnt_str = "Three"; break;
748 default:
749 channel_cnt_str = "Two"; break;
750 }
751 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
752 if (!ctl) {
753 ALOGE("%s: Could not get ctl for mixer cmd - %s",
754 __func__, mixer_ctl_name);
755 return -EINVAL;
756 }
757 ALOGV("HDMI channel count: %s", channel_cnt_str);
758 mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
759 return 0;
760}
761
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700762int platform_edid_get_max_channels(void *platform)
Eric Laurentb23d5282013-05-14 15:27:20 -0700763{
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700764 struct platform_data *my_data = (struct platform_data *)platform;
765 struct audio_device *adev = my_data->adev;
Eric Laurentb23d5282013-05-14 15:27:20 -0700766 char block[MAX_SAD_BLOCKS * SAD_BLOCK_SIZE];
767 char *sad = block;
768 int num_audio_blocks;
769 int channel_count;
770 int max_channels = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700771 int i, ret, count;
Eric Laurentb23d5282013-05-14 15:27:20 -0700772
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700773 struct mixer_ctl *ctl;
774
775 ctl = mixer_get_ctl_by_name(adev->mixer, AUDIO_DATA_BLOCK_MIXER_CTL);
776 if (!ctl) {
777 ALOGE("%s: Could not get ctl for mixer cmd - %s",
778 __func__, AUDIO_DATA_BLOCK_MIXER_CTL);
Eric Laurentb23d5282013-05-14 15:27:20 -0700779 return 0;
780 }
781
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700782 mixer_ctl_update(ctl);
783
784 count = mixer_ctl_get_num_values(ctl);
Eric Laurentb23d5282013-05-14 15:27:20 -0700785
786 /* Read SAD blocks, clamping the maximum size for safety */
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700787 if (count > (int)sizeof(block))
788 count = (int)sizeof(block);
Eric Laurentb23d5282013-05-14 15:27:20 -0700789
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700790 ret = mixer_ctl_get_array(ctl, block, count);
791 if (ret != 0) {
792 ALOGE("%s: mixer_ctl_get_array() failed to get EDID info", __func__);
793 return 0;
794 }
Eric Laurentb23d5282013-05-14 15:27:20 -0700795
796 /* Calculate the number of SAD blocks */
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700797 num_audio_blocks = count / SAD_BLOCK_SIZE;
Eric Laurentb23d5282013-05-14 15:27:20 -0700798
799 for (i = 0; i < num_audio_blocks; i++) {
800 /* Only consider LPCM blocks */
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700801 if ((sad[0] >> 3) != EDID_FORMAT_LPCM) {
802 sad += 3;
Eric Laurentb23d5282013-05-14 15:27:20 -0700803 continue;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700804 }
Eric Laurentb23d5282013-05-14 15:27:20 -0700805
806 channel_count = (sad[0] & 0x7) + 1;
807 if (channel_count > max_channels)
808 max_channels = channel_count;
809
810 /* Advance to next block */
811 sad += 3;
812 }
813
814 return max_channels;
815}