blob: 471b14360948c8a499bf3d4dab06ed6dcb2a64e8 [file] [log] [blame]
Eric Laurentb23d5282013-05-14 15:27:20 -07001/*
Eric Laurenta609e8e2014-06-18 02:15:17 +00002 * Copyright (C) 2013 The Android Open Source Project
Eric Laurentb23d5282013-05-14 15:27:20 -07003 *
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>
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -070024#include <cutils/str_parms.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070025#include <cutils/properties.h>
26#include <audio_hw.h>
27#include <platform_api.h>
28#include "platform.h"
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -070029#ifdef PLATFORM_MSM8084
30#include "mdm_detect.h"
31#endif
Eric Laurentb23d5282013-05-14 15:27:20 -070032
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -070033#define MIXER_XML_PATH "/system/etc/mixer_paths.xml"
Eric Laurentb23d5282013-05-14 15:27:20 -070034#define LIB_ACDB_LOADER "libacdbloader.so"
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -070035#define AUDIO_DATA_BLOCK_MIXER_CTL "HDMI EDID"
Eric Laurentb23d5282013-05-14 15:27:20 -070036
37#define DUALMIC_CONFIG_NONE 0 /* Target does not contain 2 mics */
38#define DUALMIC_CONFIG_ENDFIRE 1
39#define DUALMIC_CONFIG_BROADSIDE 2
40
41/*
Eric Laurentb23d5282013-05-14 15:27:20 -070042 * 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
Eric Laurenta609e8e2014-06-18 02:15:17 +000057
58#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)
sangwoo53b2cf02013-07-25 19:18:44 -070062
Eric Laurentb23d5282013-05-14 15:27:20 -070063struct audio_block_header
64{
65 int reserved;
66 int length;
67};
68
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -070069/* Audio calibration related functions */
Eric Laurentb23d5282013-05-14 15:27:20 -070070typedef void (*acdb_deallocate_t)();
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -070071#ifdef PLATFORM_MSM8084
72typedef int (*acdb_init_t)(char *);
73#else
Eric Laurentb23d5282013-05-14 15:27:20 -070074typedef int (*acdb_init_t)();
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -070075#endif
Eric Laurentb23d5282013-05-14 15:27:20 -070076typedef void (*acdb_send_audio_cal_t)(int, int);
77typedef void (*acdb_send_voice_cal_t)(int, int);
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -070078typedef int (*acdb_reload_vocvoltable_t)(int);
Eric Laurentb23d5282013-05-14 15:27:20 -070079
80/* Audio calibration related functions */
81struct platform_data {
82 struct audio_device *adev;
83 bool fluence_in_spkr_mode;
84 bool fluence_in_voice_call;
85 bool fluence_in_voice_rec;
86 int dualmic_config;
Eric Laurentb23d5282013-05-14 15:27:20 -070087 void *acdb_handle;
88 acdb_init_t acdb_init;
89 acdb_deallocate_t acdb_deallocate;
90 acdb_send_audio_cal_t acdb_send_audio_cal;
91 acdb_send_voice_cal_t acdb_send_voice_cal;
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -070092 acdb_reload_vocvoltable_t acdb_reload_vocvoltable;
93 struct csd_data *csd;
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -050094 bool ext_speaker;
95 bool ext_earpiece;
Eric Laurentb23d5282013-05-14 15:27:20 -070096};
97
98static const int pcm_device_table[AUDIO_USECASE_MAX][2] = {
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -070099 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {DEEP_BUFFER_PCM_DEVICE,
100 DEEP_BUFFER_PCM_DEVICE},
101 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {LOWLATENCY_PCM_DEVICE,
102 LOWLATENCY_PCM_DEVICE},
103 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {MULTIMEDIA2_PCM_DEVICE,
104 MULTIMEDIA2_PCM_DEVICE},
105 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {PLAYBACK_OFFLOAD_DEVICE,
106 PLAYBACK_OFFLOAD_DEVICE},
107 [USECASE_AUDIO_RECORD] = {AUDIO_RECORD_PCM_DEVICE,
108 AUDIO_RECORD_PCM_DEVICE},
109 [USECASE_AUDIO_RECORD_LOW_LATENCY] = {LOWLATENCY_PCM_DEVICE,
110 LOWLATENCY_PCM_DEVICE},
111 [USECASE_VOICE_CALL] = {VOICE_CALL_PCM_DEVICE,
112 VOICE_CALL_PCM_DEVICE},
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800113 [USECASE_AUDIO_HFP_SCO] = {HFP_PCM_RX, HFP_SCO_RX},
Eric Laurentb23d5282013-05-14 15:27:20 -0700114};
115
116/* Array to store sound devices */
117static const char * const device_table[SND_DEVICE_MAX] = {
118 [SND_DEVICE_NONE] = "none",
119 /* Playback sound devices */
120 [SND_DEVICE_OUT_HANDSET] = "handset",
121 [SND_DEVICE_OUT_SPEAKER] = "speaker",
122 [SND_DEVICE_OUT_SPEAKER_REVERSE] = "speaker-reverse",
123 [SND_DEVICE_OUT_HEADPHONES] = "headphones",
124 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
125 [SND_DEVICE_OUT_VOICE_HANDSET] = "voice-handset",
126 [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
127 [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
128 [SND_DEVICE_OUT_HDMI] = "hdmi",
129 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
130 [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -0700131 [SND_DEVICE_OUT_BT_SCO_WB] = "bt-sco-headset-wb",
Eric Laurentb23d5282013-05-14 15:27:20 -0700132 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = "voice-handset-tmus",
133 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
134 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
135 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
136
137 /* Capture sound devices */
138 [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
139 [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
140 [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
141 [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic",
Eric Laurent34fa7692014-03-07 09:31:06 -0800142 [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "speaker-mic-aec",
Eric Laurentb23d5282013-05-14 15:27:20 -0700143 [SND_DEVICE_IN_HEADSET_MIC_AEC] = "headset-mic",
144 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
145 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
146 [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
147 [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -0700148 [SND_DEVICE_IN_BT_SCO_MIC_WB] = "bt-sco-mic-wb",
Eric Laurentb23d5282013-05-14 15:27:20 -0700149 [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
150 [SND_DEVICE_IN_VOICE_DMIC_EF] = "voice-dmic-ef",
151 [SND_DEVICE_IN_VOICE_DMIC_BS] = "voice-dmic-bs",
152 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = "voice-dmic-ef-tmus",
153 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = "voice-speaker-dmic-ef",
154 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = "voice-speaker-dmic-bs",
155 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
156 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
157 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
158 [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
159 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = "voice-rec-dmic-ef",
160 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = "voice-rec-dmic-bs",
161 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = "voice-rec-dmic-ef-fluence",
162 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = "voice-rec-dmic-bs-fluence",
163};
164
165/* ACDB IDs (audio DSP path configuration IDs) for each sound device */
166static const int acdb_device_table[SND_DEVICE_MAX] = {
167 [SND_DEVICE_NONE] = -1,
168 [SND_DEVICE_OUT_HANDSET] = 7,
169 [SND_DEVICE_OUT_SPEAKER] = 15,
170 [SND_DEVICE_OUT_SPEAKER_REVERSE] = 15,
171 [SND_DEVICE_OUT_HEADPHONES] = 10,
172 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
Ravi Kumar Alamanda1de6e5a2014-06-19 21:55:39 -0500173#ifdef PLATFORM_MSM8084
174 [SND_DEVICE_OUT_VOICE_HANDSET] = 67,
175 [SND_DEVICE_OUT_VOICE_SPEAKER] = 66,
176#else
Eric Laurentb23d5282013-05-14 15:27:20 -0700177 [SND_DEVICE_OUT_VOICE_HANDSET] = 7,
178 [SND_DEVICE_OUT_VOICE_SPEAKER] = 15,
Ravi Kumar Alamanda1de6e5a2014-06-19 21:55:39 -0500179#endif
Eric Laurentb23d5282013-05-14 15:27:20 -0700180 [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
181 [SND_DEVICE_OUT_HDMI] = 18,
182 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 15,
183 [SND_DEVICE_OUT_BT_SCO] = 22,
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -0700184 [SND_DEVICE_OUT_BT_SCO_WB] = 39,
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700185 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = ACDB_ID_VOICE_HANDSET_TMUS,
Eric Laurentb23d5282013-05-14 15:27:20 -0700186 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
187 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
188 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
189
190 [SND_DEVICE_IN_HANDSET_MIC] = 4,
191 [SND_DEVICE_IN_SPEAKER_MIC] = 4, /* ToDo: Check if this needs to changed to 11 */
192 [SND_DEVICE_IN_HEADSET_MIC] = 8,
193 [SND_DEVICE_IN_HANDSET_MIC_AEC] = 40,
194 [SND_DEVICE_IN_SPEAKER_MIC_AEC] = 42,
195 [SND_DEVICE_IN_HEADSET_MIC_AEC] = 47,
196 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
197 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8,
198 [SND_DEVICE_IN_HDMI_MIC] = 4,
199 [SND_DEVICE_IN_BT_SCO_MIC] = 21,
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -0700200 [SND_DEVICE_IN_BT_SCO_MIC_WB] = 38,
Eric Laurentb23d5282013-05-14 15:27:20 -0700201 [SND_DEVICE_IN_CAMCORDER_MIC] = 61,
202 [SND_DEVICE_IN_VOICE_DMIC_EF] = 41,
203 [SND_DEVICE_IN_VOICE_DMIC_BS] = 5,
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700204 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = ACDB_ID_VOICE_DMIC_EF_TMUS,
Eric Laurentb23d5282013-05-14 15:27:20 -0700205 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = 43,
206 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = 12,
207 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
208 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
209 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
210 [SND_DEVICE_IN_VOICE_REC_MIC] = 62,
211 /* TODO: Update with proper acdb ids */
212 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = 62,
213 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = 62,
214 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = 6,
215 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = 5,
216};
217
Haynes Mathew George7ff216f2013-09-11 19:51:41 -0700218#define DEEP_BUFFER_PLATFORM_DELAY (29*1000LL)
219#define LOW_LATENCY_PLATFORM_DELAY (13*1000LL)
220
Eric Laurentb23d5282013-05-14 15:27:20 -0700221static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
222static bool is_tmus = false;
223
224static void check_operator()
225{
226 char value[PROPERTY_VALUE_MAX];
227 int mccmnc;
228 property_get("gsm.sim.operator.numeric",value,"0");
229 mccmnc = atoi(value);
230 ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
231 switch(mccmnc) {
232 /* TMUS MCC(310), MNC(490, 260, 026) */
233 case 310490:
234 case 310260:
235 case 310026:
sangwon.jeonb891db52013-09-14 17:39:15 +0900236 /* Add new TMUS MNC(800, 660, 580, 310, 270, 250, 240, 230, 220, 210, 200, 160) */
237 case 310800:
238 case 310660:
239 case 310580:
240 case 310310:
241 case 310270:
242 case 310250:
243 case 310240:
244 case 310230:
245 case 310220:
246 case 310210:
247 case 310200:
248 case 310160:
Eric Laurentb23d5282013-05-14 15:27:20 -0700249 is_tmus = true;
250 break;
251 }
252}
253
254bool is_operator_tmus()
255{
256 pthread_once(&check_op_once_ctl, check_operator);
257 return is_tmus;
258}
259
Eric Laurenta609e8e2014-06-18 02:15:17 +0000260static int set_volume_values(int type, int volume, int* values)
261{
262 values[0] = volume;
263 values[1] = ALL_SESSION_VSID;
264
265 switch(type) {
266 case VOLUME_SET:
267 values[2] = DEFAULT_VOLUME_RAMP_DURATION_MS;
268 break;
269 case MUTE_SET:
270 values[2] = DEFAULT_MUTE_RAMP_DURATION;
271 break;
272 default:
273 return -EINVAL;
274 }
275 return 0;
276}
277
Eric Laurentb23d5282013-05-14 15:27:20 -0700278static int set_echo_reference(struct mixer *mixer, const char* ec_ref)
279{
280 struct mixer_ctl *ctl;
281 const char *mixer_ctl_name = "EC_REF_RX";
282
283 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
284 if (!ctl) {
285 ALOGE("%s: Could not get ctl for mixer cmd - %s",
286 __func__, mixer_ctl_name);
287 return -EINVAL;
288 }
289 ALOGV("Setting EC Reference: %s", ec_ref);
290 mixer_ctl_set_enum_by_string(ctl, ec_ref);
291 return 0;
292}
293
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700294static struct csd_data *open_csd_client(bool i2s_ext_modem)
295{
296 struct csd_data *csd = calloc(1, sizeof(struct csd_data));
297
298 csd->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
299 if (csd->csd_client == NULL) {
300 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
301 goto error;
302 } else {
303 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
304
305 csd->deinit = (deinit_t)dlsym(csd->csd_client,
306 "csd_client_deinit");
307 if (csd->deinit == NULL) {
308 ALOGE("%s: dlsym error %s for csd_client_deinit", __func__,
309 dlerror());
310 goto error;
311 }
312 csd->disable_device = (disable_device_t)dlsym(csd->csd_client,
313 "csd_client_disable_device");
314 if (csd->disable_device == NULL) {
315 ALOGE("%s: dlsym error %s for csd_client_disable_device",
316 __func__, dlerror());
317 goto error;
318 }
319 csd->enable_device_config = (enable_device_config_t)dlsym(csd->csd_client,
320 "csd_client_enable_device_config");
321 if (csd->enable_device_config == NULL) {
322 ALOGE("%s: dlsym error %s for csd_client_enable_device_config",
323 __func__, dlerror());
324 goto error;
325 }
326 csd->enable_device = (enable_device_t)dlsym(csd->csd_client,
327 "csd_client_enable_device");
328 if (csd->enable_device == NULL) {
329 ALOGE("%s: dlsym error %s for csd_client_enable_device",
330 __func__, dlerror());
331 goto error;
332 }
333 csd->start_voice = (start_voice_t)dlsym(csd->csd_client,
334 "csd_client_start_voice");
335 if (csd->start_voice == NULL) {
336 ALOGE("%s: dlsym error %s for csd_client_start_voice",
337 __func__, dlerror());
338 goto error;
339 }
340 csd->stop_voice = (stop_voice_t)dlsym(csd->csd_client,
341 "csd_client_stop_voice");
342 if (csd->stop_voice == NULL) {
343 ALOGE("%s: dlsym error %s for csd_client_stop_voice",
344 __func__, dlerror());
345 goto error;
346 }
347 csd->volume = (volume_t)dlsym(csd->csd_client,
348 "csd_client_volume");
349 if (csd->volume == NULL) {
350 ALOGE("%s: dlsym error %s for csd_client_volume",
351 __func__, dlerror());
352 goto error;
353 }
354 csd->mic_mute = (mic_mute_t)dlsym(csd->csd_client,
355 "csd_client_mic_mute");
356 if (csd->mic_mute == NULL) {
357 ALOGE("%s: dlsym error %s for csd_client_mic_mute",
358 __func__, dlerror());
359 goto error;
360 }
361 csd->slow_talk = (slow_talk_t)dlsym(csd->csd_client,
362 "csd_client_slow_talk");
363 if (csd->slow_talk == NULL) {
364 ALOGE("%s: dlsym error %s for csd_client_slow_talk",
365 __func__, dlerror());
366 goto error;
367 }
368 csd->start_playback = (start_playback_t)dlsym(csd->csd_client,
369 "csd_client_start_playback");
370 if (csd->start_playback == NULL) {
371 ALOGE("%s: dlsym error %s for csd_client_start_playback",
372 __func__, dlerror());
373 goto error;
374 }
375 csd->stop_playback = (stop_playback_t)dlsym(csd->csd_client,
376 "csd_client_stop_playback");
377 if (csd->stop_playback == NULL) {
378 ALOGE("%s: dlsym error %s for csd_client_stop_playback",
379 __func__, dlerror());
380 goto error;
381 }
382 csd->start_record = (start_record_t)dlsym(csd->csd_client,
383 "csd_client_start_record");
384 if (csd->start_record == NULL) {
385 ALOGE("%s: dlsym error %s for csd_client_start_record",
386 __func__, dlerror());
387 goto error;
388 }
389 csd->stop_record = (stop_record_t)dlsym(csd->csd_client,
390 "csd_client_stop_record");
391 if (csd->stop_record == NULL) {
392 ALOGE("%s: dlsym error %s for csd_client_stop_record",
393 __func__, dlerror());
394 goto error;
395 }
396
397 csd->get_sample_rate = (get_sample_rate_t)dlsym(csd->csd_client,
398 "csd_client_get_sample_rate");
399 if (csd->get_sample_rate == NULL) {
400 ALOGE("%s: dlsym error %s for csd_client_get_sample_rate",
401 __func__, dlerror());
402
403 goto error;
404 }
405
406 csd->init = (init_t)dlsym(csd->csd_client, "csd_client_init");
407
408 if (csd->init == NULL) {
409 ALOGE("%s: dlsym error %s for csd_client_init",
410 __func__, dlerror());
411 goto error;
412 } else {
413 csd->init(i2s_ext_modem);
414 }
415 }
416 return csd;
417
418error:
419 free(csd);
420 csd = NULL;
421 return csd;
422}
423
424void close_csd_client(struct csd_data *csd)
425{
426 if (csd != NULL) {
427 csd->deinit();
428 dlclose(csd->csd_client);
429 free(csd);
430 csd = NULL;
431 }
432}
433
434static void platform_csd_init(struct platform_data *my_data)
435{
436#ifdef PLATFORM_MSM8084
437 struct dev_info mdm_detect_info;
438 int ret = 0;
439
440 /* Call ESOC API to get the number of modems.
441 * If the number of modems is not zero, load CSD Client specific
442 * symbols. Voice call is handled by MDM and apps processor talks to
443 * MDM through CSD Client
444 */
445 ret = get_system_info(&mdm_detect_info);
446 if (ret > 0) {
447 ALOGE("%s: Failed to get system info, ret %d", __func__, ret);
448 }
449 ALOGD("%s: num_modems %d\n", __func__, mdm_detect_info.num_modems);
450
451 if (mdm_detect_info.num_modems > 0)
452 my_data->csd = open_csd_client(false /*is_i2s_ext_modem*/);
453#else
454 my_data->csd = NULL;
455#endif
456}
457
Eric Laurentb23d5282013-05-14 15:27:20 -0700458void *platform_init(struct audio_device *adev)
459{
460 char value[PROPERTY_VALUE_MAX];
461 struct platform_data *my_data;
Eric Laurenta609e8e2014-06-18 02:15:17 +0000462 int retry_num = 0;
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700463 const char *snd_card_name;
sangwoo1b9f4b32013-06-21 18:22:55 -0700464
Eric Laurenta609e8e2014-06-18 02:15:17 +0000465 adev->mixer = mixer_open(MIXER_CARD);
sangwoo1b9f4b32013-06-21 18:22:55 -0700466
Eric Laurenta609e8e2014-06-18 02:15:17 +0000467 while (!adev->mixer && retry_num < RETRY_NUMBER) {
468 usleep(RETRY_US);
469 adev->mixer = mixer_open(MIXER_CARD);
470 retry_num++;
sangwoo1b9f4b32013-06-21 18:22:55 -0700471 }
472
Eric Laurenta609e8e2014-06-18 02:15:17 +0000473 if (!adev->mixer) {
474 ALOGE("Unable to open the mixer, aborting.");
475 return NULL;
476 }
477
478 snd_card_name = mixer_get_name(adev->mixer);
479 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
480
481 adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
482 if (!adev->audio_route) {
483 ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
sangwoo1b9f4b32013-06-21 18:22:55 -0700484 return NULL;
485 }
Eric Laurentb23d5282013-05-14 15:27:20 -0700486
487 my_data = calloc(1, sizeof(struct platform_data));
488
489 my_data->adev = adev;
490 my_data->dualmic_config = DUALMIC_CONFIG_NONE;
491 my_data->fluence_in_spkr_mode = false;
492 my_data->fluence_in_voice_call = false;
493 my_data->fluence_in_voice_rec = false;
494
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500495 /*
496 * The default assumption is that earpiece (handset), speaker and headphones
497 * devices are connected to internal HW codec and communicated through
498 * slimbus backend. If any platform communicates with speaker or earpiece
499 * or headphones through non-slimbus backend such as MI2S or AUXPCM etc.,
500 * the ext_xxxx flags must be set accordingly.
501 */
502 if (strstr(snd_card_name, "tfa9890_stereo")) {
503 my_data->ext_speaker = true;
504 my_data->ext_earpiece = true;
505 } else if (strstr(snd_card_name, "tfa9890")) {
506 my_data->ext_speaker = true;
507 }
508
Eric Laurentb23d5282013-05-14 15:27:20 -0700509 property_get("persist.audio.dualmic.config",value,"");
510 if (!strcmp("broadside", value)) {
511 my_data->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
512 adev->acdb_settings |= DMIC_FLAG;
513 } else if (!strcmp("endfire", value)) {
514 my_data->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
515 adev->acdb_settings |= DMIC_FLAG;
516 }
517
518 if (my_data->dualmic_config != DUALMIC_CONFIG_NONE) {
519 property_get("persist.audio.fluence.voicecall",value,"");
520 if (!strcmp("true", value)) {
521 my_data->fluence_in_voice_call = true;
522 }
523
524 property_get("persist.audio.fluence.voicerec",value,"");
525 if (!strcmp("true", value)) {
526 my_data->fluence_in_voice_rec = true;
527 }
528
529 property_get("persist.audio.fluence.speaker",value,"");
530 if (!strcmp("true", value)) {
531 my_data->fluence_in_spkr_mode = true;
532 }
533 }
534
535 my_data->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
536 if (my_data->acdb_handle == NULL) {
537 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
538 } else {
539 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
540 my_data->acdb_deallocate = (acdb_deallocate_t)dlsym(my_data->acdb_handle,
541 "acdb_loader_deallocate_ACDB");
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700542 if (!my_data->acdb_deallocate)
543 ALOGE("%s: Could not find the symbol acdb_loader_deallocate_ACDB from %s",
544 __func__, LIB_ACDB_LOADER);
545
Eric Laurentb23d5282013-05-14 15:27:20 -0700546 my_data->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(my_data->acdb_handle,
547 "acdb_loader_send_audio_cal");
548 if (!my_data->acdb_send_audio_cal)
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700549 ALOGE("%s: Could not find the symbol acdb_send_audio_cal from %s",
Eric Laurentb23d5282013-05-14 15:27:20 -0700550 __func__, LIB_ACDB_LOADER);
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700551
Eric Laurentb23d5282013-05-14 15:27:20 -0700552 my_data->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(my_data->acdb_handle,
553 "acdb_loader_send_voice_cal");
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700554 if (!my_data->acdb_send_voice_cal)
555 ALOGE("%s: Could not find the symbol acdb_loader_send_voice_cal from %s",
556 __func__, LIB_ACDB_LOADER);
557
558 my_data->acdb_reload_vocvoltable = (acdb_reload_vocvoltable_t)dlsym(my_data->acdb_handle,
559 "acdb_loader_reload_vocvoltable");
560 if (!my_data->acdb_reload_vocvoltable)
561 ALOGE("%s: Could not find the symbol acdb_loader_reload_vocvoltable from %s",
562 __func__, LIB_ACDB_LOADER);
563#ifdef PLATFORM_MSM8084
564 my_data->acdb_init = (acdb_init_t)dlsym(my_data->acdb_handle,
565 "acdb_loader_init_v2");
566 if (my_data->acdb_init == NULL)
567 ALOGE("%s: dlsym error %s for acdb_loader_init_v2", __func__, dlerror());
568 else
569 my_data->acdb_init(snd_card_name);
570#else
Eric Laurentb23d5282013-05-14 15:27:20 -0700571 my_data->acdb_init = (acdb_init_t)dlsym(my_data->acdb_handle,
572 "acdb_loader_init_ACDB");
573 if (my_data->acdb_init == NULL)
574 ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
575 else
576 my_data->acdb_init();
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700577#endif
Eric Laurentb23d5282013-05-14 15:27:20 -0700578 }
579
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700580 /* load csd client */
581 platform_csd_init(my_data);
582
Eric Laurentb23d5282013-05-14 15:27:20 -0700583 return my_data;
584}
585
586void platform_deinit(void *platform)
587{
588 free(platform);
589}
590
591const char *platform_get_snd_device_name(snd_device_t snd_device)
592{
593 if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX)
594 return device_table[snd_device];
595 else
596 return "";
597}
598
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500599void platform_add_backend_name(void *platform, char *mixer_path,
600 snd_device_t snd_device)
Eric Laurentb23d5282013-05-14 15:27:20 -0700601{
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500602 struct platform_data *my_data = (struct platform_data *)platform;
603
Eric Laurentb23d5282013-05-14 15:27:20 -0700604 if (snd_device == SND_DEVICE_IN_BT_SCO_MIC)
605 strcat(mixer_path, " bt-sco");
606 else if(snd_device == SND_DEVICE_OUT_BT_SCO)
607 strcat(mixer_path, " bt-sco");
608 else if (snd_device == SND_DEVICE_OUT_HDMI)
609 strcat(mixer_path, " hdmi");
610 else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HDMI)
611 strcat(mixer_path, " speaker-and-hdmi");
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -0700612 else if (snd_device == SND_DEVICE_OUT_BT_SCO_WB ||
613 snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB)
614 strcat(mixer_path, " bt-sco-wb");
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500615 else if (my_data->ext_speaker) {
616 if (snd_device == SND_DEVICE_OUT_SPEAKER ||
617 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER ||
618 snd_device == SND_DEVICE_OUT_SPEAKER_REVERSE)
619 strcat(mixer_path, " speaker");
620 else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES)
621 strcat(mixer_path, " speaker-and-headphones");
Ravi Kumar Alamanda1de6e5a2014-06-19 21:55:39 -0500622 }
623 if (my_data->ext_earpiece &&
624 (snd_device == SND_DEVICE_OUT_VOICE_HANDSET ||
625 snd_device == SND_DEVICE_OUT_VOICE_HANDSET_TMUS ||
626 snd_device == SND_DEVICE_OUT_HANDSET ||
627 snd_device == SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET)) {
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500628 strcat(mixer_path, " handset");
629 }
Eric Laurentb23d5282013-05-14 15:27:20 -0700630}
631
632int platform_get_pcm_device_id(audio_usecase_t usecase, int device_type)
633{
634 int device_id;
635 if (device_type == PCM_PLAYBACK)
636 device_id = pcm_device_table[usecase][0];
637 else
638 device_id = pcm_device_table[usecase][1];
639 return device_id;
640}
641
642int platform_send_audio_calibration(void *platform, snd_device_t snd_device)
643{
644 struct platform_data *my_data = (struct platform_data *)platform;
645 int acdb_dev_id, acdb_dev_type;
646
647 acdb_dev_id = acdb_device_table[snd_device];
648 if (acdb_dev_id < 0) {
649 ALOGE("%s: Could not find acdb id for device(%d)",
650 __func__, snd_device);
651 return -EINVAL;
652 }
653 if (my_data->acdb_send_audio_cal) {
Eric Laurent994a6932013-07-17 11:51:42 -0700654 ("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
Eric Laurentb23d5282013-05-14 15:27:20 -0700655 __func__, snd_device, acdb_dev_id);
656 if (snd_device >= SND_DEVICE_OUT_BEGIN &&
657 snd_device < SND_DEVICE_OUT_END)
658 acdb_dev_type = ACDB_DEV_TYPE_OUT;
659 else
660 acdb_dev_type = ACDB_DEV_TYPE_IN;
661 my_data->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
662 }
663 return 0;
664}
665
666int platform_switch_voice_call_device_pre(void *platform)
667{
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700668 struct platform_data *my_data = (struct platform_data *)platform;
669 int ret = 0;
670
671 if (my_data->csd != NULL &&
672 my_data->adev->mode == AUDIO_MODE_IN_CALL) {
673 /* This must be called before disabling mixer controls on APQ side */
674 ret = my_data->csd->disable_device();
675 if (ret < 0) {
676 ALOGE("%s: csd_client_disable_device, failed, error %d",
677 __func__, ret);
678 }
679 }
680 return ret;
681}
682
683int platform_switch_voice_call_enable_device_config(void *platform,
684 snd_device_t out_snd_device,
685 snd_device_t in_snd_device)
686{
687 struct platform_data *my_data = (struct platform_data *)platform;
688 int acdb_rx_id, acdb_tx_id;
689 int ret = 0;
690
691 if (my_data->csd == NULL)
692 return ret;
693
694 acdb_rx_id = acdb_device_table[out_snd_device];
695
696 acdb_tx_id = acdb_device_table[in_snd_device];
697
698 if (acdb_rx_id > 0 && acdb_tx_id > 0) {
699 ret = my_data->csd->enable_device_config(acdb_rx_id, acdb_tx_id);
700 if (ret < 0) {
701 ALOGE("%s: csd_enable_device_config, failed, error %d",
702 __func__, ret);
703 }
704 } else {
705 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
706 acdb_rx_id, acdb_tx_id);
707 }
708
709 return ret;
Eric Laurentb23d5282013-05-14 15:27:20 -0700710}
711
712int platform_switch_voice_call_device_post(void *platform,
713 snd_device_t out_snd_device,
714 snd_device_t in_snd_device)
715{
716 struct platform_data *my_data = (struct platform_data *)platform;
717 int acdb_rx_id, acdb_tx_id;
718
719 if (my_data->acdb_send_voice_cal == NULL) {
720 ALOGE("%s: dlsym error for acdb_send_voice_call", __func__);
721 } else {
722 acdb_rx_id = acdb_device_table[out_snd_device];
723 acdb_tx_id = acdb_device_table[in_snd_device];
724
725 if (acdb_rx_id > 0 && acdb_tx_id > 0)
726 my_data->acdb_send_voice_cal(acdb_rx_id, acdb_tx_id);
727 else
728 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
729 acdb_rx_id, acdb_tx_id);
730 }
731
732 return 0;
733}
734
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700735int platform_switch_voice_call_usecase_route_post(void *platform,
736 snd_device_t out_snd_device,
737 snd_device_t in_snd_device)
738{
739 struct platform_data *my_data = (struct platform_data *)platform;
740 int acdb_rx_id, acdb_tx_id;
741 int ret = 0;
742
743 if (my_data->csd == NULL)
744 return ret;
745
746 acdb_rx_id = acdb_device_table[out_snd_device];
747
748 acdb_tx_id = acdb_device_table[in_snd_device];
749
750 if (acdb_rx_id > 0 && acdb_tx_id > 0) {
751 ret = my_data->csd->enable_device(acdb_rx_id, acdb_tx_id,
752 my_data->adev->acdb_settings);
753 if (ret < 0) {
754 ALOGE("%s: csd_enable_device, failed, error %d", __func__, ret);
755 }
756 } else {
757 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
758 acdb_rx_id, acdb_tx_id);
759 }
760
761 return ret;
762}
763
Eric Laurenta609e8e2014-06-18 02:15:17 +0000764int platform_start_voice_call(void *platform)
Eric Laurentb23d5282013-05-14 15:27:20 -0700765{
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700766 struct platform_data *my_data = (struct platform_data *)platform;
767 int ret = 0;
768
769 if (my_data->csd != NULL) {
Eric Laurenta609e8e2014-06-18 02:15:17 +0000770 ret = my_data->csd->start_voice(VOICE_VSID);
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700771 if (ret < 0) {
772 ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
773 }
774 }
775 return ret;
Eric Laurentb23d5282013-05-14 15:27:20 -0700776}
777
Eric Laurenta609e8e2014-06-18 02:15:17 +0000778int platform_stop_voice_call(void *platform)
Eric Laurentb23d5282013-05-14 15:27:20 -0700779{
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700780 struct platform_data *my_data = (struct platform_data *)platform;
781 int ret = 0;
782
783 if (my_data->csd != NULL) {
Eric Laurenta609e8e2014-06-18 02:15:17 +0000784 ret = my_data->csd->stop_voice(VOICE_VSID);
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700785 if (ret < 0) {
786 ALOGE("%s: csd_stop_voice error %d\n", __func__, ret);
787 }
788 }
789 return ret;
Eric Laurentb23d5282013-05-14 15:27:20 -0700790}
791
792int platform_set_voice_volume(void *platform, int volume)
793{
794 struct platform_data *my_data = (struct platform_data *)platform;
795 struct audio_device *adev = my_data->adev;
796 struct mixer_ctl *ctl;
sangwoo53b2cf02013-07-25 19:18:44 -0700797 const char *mixer_ctl_name = "Voice Rx Gain";
Eric Laurenta609e8e2014-06-18 02:15:17 +0000798 int values[VOLUME_CTL_PARAM_NUM];
799 int ret = 0;
Eric Laurentb23d5282013-05-14 15:27:20 -0700800
801 // Voice volume levels are mapped to adsp volume levels as follows.
802 // 100 -> 5, 80 -> 4, 60 -> 3, 40 -> 2, 20 -> 1 0 -> 0
803 // But this values don't changed in kernel. So, below change is need.
Eric Laurenta609e8e2014-06-18 02:15:17 +0000804 volume = (int)percent_to_index(volume, MIN_VOL_INDEX, MAX_VOL_INDEX);
Eric Laurentb23d5282013-05-14 15:27:20 -0700805
806 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
807 if (!ctl) {
808 ALOGE("%s: Could not get ctl for mixer cmd - %s",
809 __func__, mixer_ctl_name);
810 return -EINVAL;
811 }
Eric Laurenta609e8e2014-06-18 02:15:17 +0000812 ret = set_volume_values(VOLUME_SET, volume, values);
813 if (ret < 0) {
814 ALOGV("%s: failed setting volume by incorrect type", __func__);
815 return -EINVAL;
816 }
817 ret = mixer_ctl_set_array(ctl, values, sizeof(values)/sizeof(int));
818 if (ret < 0) {
819 ALOGV("%s: failed set mixer ctl by %d", __func__, ret);
820 return -EINVAL;
821 }
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700822 if (my_data->csd != NULL) {
823 ret = my_data->csd->volume(ALL_SESSION_VSID, volume,
824 DEFAULT_VOLUME_RAMP_DURATION_MS);
825 if (ret < 0) {
826 ALOGE("%s: csd_volume error %d", __func__, ret);
827 }
828 }
829 return ret;
Eric Laurentb23d5282013-05-14 15:27:20 -0700830}
831
832int platform_set_mic_mute(void *platform, bool state)
833{
834 struct platform_data *my_data = (struct platform_data *)platform;
835 struct audio_device *adev = my_data->adev;
836 struct mixer_ctl *ctl;
837 const char *mixer_ctl_name = "Voice Tx Mute";
Eric Laurenta609e8e2014-06-18 02:15:17 +0000838 int values[VOLUME_CTL_PARAM_NUM];
sangwoo53b2cf02013-07-25 19:18:44 -0700839 int ret = 0;
Eric Laurentb23d5282013-05-14 15:27:20 -0700840
Eric Laurenta609e8e2014-06-18 02:15:17 +0000841 if (adev->mode == AUDIO_MODE_IN_CALL) {
842 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
843 if (!ctl) {
844 ALOGE("%s: Could not get ctl for mixer cmd - %s",
845 __func__, mixer_ctl_name);
846 return -EINVAL;
847 }
848 ALOGV("Setting mic mute: %d", state);
849 ret = set_volume_values(MUTE_SET, state, values);
sangwoo53b2cf02013-07-25 19:18:44 -0700850 if (ret < 0) {
Eric Laurenta609e8e2014-06-18 02:15:17 +0000851 ALOGV("%s: failed setting mute by incorrect type", __func__);
852 return -EINVAL;
853 }
854 ret = mixer_ctl_set_array(ctl, values, sizeof(values)/sizeof(int));
855 if (ret < 0) {
856 ALOGV("%s: failed set mixer ctl by %d", __func__, ret);
857 return -EINVAL;
sangwoo53b2cf02013-07-25 19:18:44 -0700858 }
Eric Laurentb23d5282013-05-14 15:27:20 -0700859 }
860
Eric Laurenta609e8e2014-06-18 02:15:17 +0000861 return 0;
Eric Laurentb23d5282013-05-14 15:27:20 -0700862}
863
864snd_device_t platform_get_output_snd_device(void *platform, audio_devices_t devices)
865{
866 struct platform_data *my_data = (struct platform_data *)platform;
867 struct audio_device *adev = my_data->adev;
868 audio_mode_t mode = adev->mode;
869 snd_device_t snd_device = SND_DEVICE_NONE;
870
871 ALOGV("%s: enter: output devices(%#x)", __func__, devices);
872 if (devices == AUDIO_DEVICE_NONE ||
873 devices & AUDIO_DEVICE_BIT_IN) {
874 ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
875 goto exit;
876 }
877
878 if (mode == AUDIO_MODE_IN_CALL) {
879 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
880 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
Eric Laurenta609e8e2014-06-18 02:15:17 +0000881 if (adev->tty_mode == TTY_MODE_FULL)
Eric Laurentb23d5282013-05-14 15:27:20 -0700882 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
Eric Laurenta609e8e2014-06-18 02:15:17 +0000883 else if (adev->tty_mode == TTY_MODE_VCO)
Eric Laurentb23d5282013-05-14 15:27:20 -0700884 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
Eric Laurenta609e8e2014-06-18 02:15:17 +0000885 else if (adev->tty_mode == TTY_MODE_HCO)
Eric Laurentb23d5282013-05-14 15:27:20 -0700886 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
887 else
888 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
889 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -0700890 if (adev->bt_wb_speech_enabled) {
891 snd_device = SND_DEVICE_OUT_BT_SCO_WB;
892 } else {
893 snd_device = SND_DEVICE_OUT_BT_SCO;
894 }
Eric Laurentb23d5282013-05-14 15:27:20 -0700895 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
896 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
897 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
898 if (is_operator_tmus())
899 snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
900 else
901 snd_device = SND_DEVICE_OUT_HANDSET;
902 }
903 if (snd_device != SND_DEVICE_NONE) {
904 goto exit;
905 }
906 }
907
908 if (popcount(devices) == 2) {
909 if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
910 AUDIO_DEVICE_OUT_SPEAKER)) {
911 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
912 } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
913 AUDIO_DEVICE_OUT_SPEAKER)) {
914 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
915 } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
916 AUDIO_DEVICE_OUT_SPEAKER)) {
917 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
918 } else {
919 ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
920 goto exit;
921 }
922 if (snd_device != SND_DEVICE_NONE) {
923 goto exit;
924 }
925 }
926
927 if (popcount(devices) != 1) {
928 ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
929 goto exit;
930 }
931
932 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
933 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
934 snd_device = SND_DEVICE_OUT_HEADPHONES;
935 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
936 if (adev->speaker_lr_swap)
937 snd_device = SND_DEVICE_OUT_SPEAKER_REVERSE;
938 else
939 snd_device = SND_DEVICE_OUT_SPEAKER;
940 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -0700941 if (adev->bt_wb_speech_enabled) {
942 snd_device = SND_DEVICE_OUT_BT_SCO_WB;
943 } else {
944 snd_device = SND_DEVICE_OUT_BT_SCO;
945 }
Eric Laurentb23d5282013-05-14 15:27:20 -0700946 } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
947 snd_device = SND_DEVICE_OUT_HDMI ;
948 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
949 snd_device = SND_DEVICE_OUT_HANDSET;
950 } else {
951 ALOGE("%s: Unknown device(s) %#x", __func__, devices);
952 }
953exit:
954 ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
955 return snd_device;
956}
957
958snd_device_t platform_get_input_snd_device(void *platform, audio_devices_t out_device)
959{
960 struct platform_data *my_data = (struct platform_data *)platform;
961 struct audio_device *adev = my_data->adev;
962 audio_source_t source = (adev->active_input == NULL) ?
963 AUDIO_SOURCE_DEFAULT : adev->active_input->source;
964
965 audio_mode_t mode = adev->mode;
966 audio_devices_t in_device = ((adev->active_input == NULL) ?
967 AUDIO_DEVICE_NONE : adev->active_input->device)
968 & ~AUDIO_DEVICE_BIT_IN;
969 audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
970 AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
971 snd_device_t snd_device = SND_DEVICE_NONE;
972
973 ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
974 __func__, out_device, in_device);
975 if (mode == AUDIO_MODE_IN_CALL) {
976 if (out_device == AUDIO_DEVICE_NONE) {
977 ALOGE("%s: No output device set for voice call", __func__);
978 goto exit;
979 }
Eric Laurenta609e8e2014-06-18 02:15:17 +0000980 if (adev->tty_mode != TTY_MODE_OFF) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700981 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
982 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
Eric Laurenta609e8e2014-06-18 02:15:17 +0000983 switch (adev->tty_mode) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700984 case TTY_MODE_FULL:
985 snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
986 break;
987 case TTY_MODE_VCO:
988 snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
989 break;
990 case TTY_MODE_HCO:
991 snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
992 break;
993 default:
Eric Laurenta609e8e2014-06-18 02:15:17 +0000994 ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode);
Eric Laurentb23d5282013-05-14 15:27:20 -0700995 }
996 goto exit;
997 }
998 }
999 if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
1000 out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
1001 if (my_data->fluence_in_voice_call == false) {
1002 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -08001003 set_echo_reference(adev->mixer, "SLIM_RX");
Eric Laurentb23d5282013-05-14 15:27:20 -07001004 } else {
1005 if (my_data->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
1006 if (is_operator_tmus())
1007 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF_TMUS;
1008 else
1009 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF;
1010 } else if(my_data->dualmic_config == DUALMIC_CONFIG_BROADSIDE)
1011 snd_device = SND_DEVICE_IN_VOICE_DMIC_BS;
1012 else
1013 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1014 }
1015 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
1016 snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
1017 } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07001018 if (adev->bt_wb_speech_enabled) {
1019 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
1020 } else {
1021 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
1022 }
Eric Laurentb23d5282013-05-14 15:27:20 -07001023 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
1024 if (my_data->fluence_in_voice_call && my_data->fluence_in_spkr_mode &&
1025 my_data->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
1026 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF;
1027 } else if (my_data->fluence_in_voice_call && my_data->fluence_in_spkr_mode &&
1028 my_data->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
1029 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS;
1030 } else {
1031 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
1032 }
1033 }
1034 } else if (source == AUDIO_SOURCE_CAMCORDER) {
1035 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
1036 in_device & AUDIO_DEVICE_IN_BACK_MIC) {
1037 snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
1038 }
1039 } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
1040 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
1041 if (my_data->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
1042 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
1043 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF;
1044 else if (my_data->fluence_in_voice_rec)
1045 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE;
1046 } else if (my_data->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
1047 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
1048 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS;
1049 else if (my_data->fluence_in_voice_rec)
1050 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE;
1051 }
1052
1053 if (snd_device == SND_DEVICE_NONE) {
1054 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
1055 }
1056 }
1057 } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1058 if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
1059 in_device = AUDIO_DEVICE_IN_BACK_MIC;
1060 if (adev->active_input) {
1061 if (adev->active_input->enable_aec) {
1062 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
1063 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
1064 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
1065 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
1066 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
1067 snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
1068 }
1069 set_echo_reference(adev->mixer, "SLIM_RX");
1070 } else
1071 set_echo_reference(adev->mixer, "NONE");
1072 }
1073 } else if (source == AUDIO_SOURCE_DEFAULT) {
1074 goto exit;
1075 }
1076
1077
1078 if (snd_device != SND_DEVICE_NONE) {
1079 goto exit;
1080 }
1081
1082 if (in_device != AUDIO_DEVICE_NONE &&
1083 !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
1084 !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
1085 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
1086 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1087 } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
1088 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
1089 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
1090 snd_device = SND_DEVICE_IN_HEADSET_MIC;
1091 } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07001092 if (adev->bt_wb_speech_enabled) {
1093 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
1094 } else {
1095 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
1096 }
Eric Laurentb23d5282013-05-14 15:27:20 -07001097 } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
1098 snd_device = SND_DEVICE_IN_HDMI_MIC;
1099 } else {
1100 ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
1101 ALOGW("%s: Using default handset-mic", __func__);
1102 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1103 }
1104 } else {
1105 if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
1106 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1107 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
1108 snd_device = SND_DEVICE_IN_HEADSET_MIC;
1109 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
1110 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
1111 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
1112 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1113 } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07001114 if (adev->bt_wb_speech_enabled) {
1115 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
1116 } else {
1117 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
1118 }
Eric Laurentb23d5282013-05-14 15:27:20 -07001119 } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1120 snd_device = SND_DEVICE_IN_HDMI_MIC;
1121 } else {
1122 ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
1123 ALOGW("%s: Using default handset-mic", __func__);
1124 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1125 }
1126 }
1127exit:
1128 ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
1129 return snd_device;
1130}
1131
1132int platform_set_hdmi_channels(void *platform, int channel_count)
1133{
1134 struct platform_data *my_data = (struct platform_data *)platform;
1135 struct audio_device *adev = my_data->adev;
1136 struct mixer_ctl *ctl;
1137 const char *channel_cnt_str = NULL;
1138 const char *mixer_ctl_name = "HDMI_RX Channels";
1139 switch (channel_count) {
1140 case 8:
1141 channel_cnt_str = "Eight"; break;
1142 case 7:
1143 channel_cnt_str = "Seven"; break;
1144 case 6:
1145 channel_cnt_str = "Six"; break;
1146 case 5:
1147 channel_cnt_str = "Five"; break;
1148 case 4:
1149 channel_cnt_str = "Four"; break;
1150 case 3:
1151 channel_cnt_str = "Three"; break;
1152 default:
1153 channel_cnt_str = "Two"; break;
1154 }
1155 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1156 if (!ctl) {
1157 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1158 __func__, mixer_ctl_name);
1159 return -EINVAL;
1160 }
1161 ALOGV("HDMI channel count: %s", channel_cnt_str);
1162 mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
1163 return 0;
1164}
1165
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001166int platform_edid_get_max_channels(void *platform)
Eric Laurentb23d5282013-05-14 15:27:20 -07001167{
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001168 struct platform_data *my_data = (struct platform_data *)platform;
1169 struct audio_device *adev = my_data->adev;
Eric Laurentb23d5282013-05-14 15:27:20 -07001170 char block[MAX_SAD_BLOCKS * SAD_BLOCK_SIZE];
1171 char *sad = block;
1172 int num_audio_blocks;
1173 int channel_count;
1174 int max_channels = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001175 int i, ret, count;
Eric Laurentb23d5282013-05-14 15:27:20 -07001176
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001177 struct mixer_ctl *ctl;
1178
1179 ctl = mixer_get_ctl_by_name(adev->mixer, AUDIO_DATA_BLOCK_MIXER_CTL);
1180 if (!ctl) {
1181 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1182 __func__, AUDIO_DATA_BLOCK_MIXER_CTL);
Eric Laurentb23d5282013-05-14 15:27:20 -07001183 return 0;
1184 }
1185
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001186 mixer_ctl_update(ctl);
1187
1188 count = mixer_ctl_get_num_values(ctl);
Eric Laurentb23d5282013-05-14 15:27:20 -07001189
1190 /* Read SAD blocks, clamping the maximum size for safety */
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001191 if (count > (int)sizeof(block))
1192 count = (int)sizeof(block);
Eric Laurentb23d5282013-05-14 15:27:20 -07001193
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001194 ret = mixer_ctl_get_array(ctl, block, count);
1195 if (ret != 0) {
1196 ALOGE("%s: mixer_ctl_get_array() failed to get EDID info", __func__);
1197 return 0;
1198 }
Eric Laurentb23d5282013-05-14 15:27:20 -07001199
1200 /* Calculate the number of SAD blocks */
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001201 num_audio_blocks = count / SAD_BLOCK_SIZE;
Eric Laurentb23d5282013-05-14 15:27:20 -07001202
1203 for (i = 0; i < num_audio_blocks; i++) {
1204 /* Only consider LPCM blocks */
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001205 if ((sad[0] >> 3) != EDID_FORMAT_LPCM) {
1206 sad += 3;
Eric Laurentb23d5282013-05-14 15:27:20 -07001207 continue;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001208 }
Eric Laurentb23d5282013-05-14 15:27:20 -07001209
1210 channel_count = (sad[0] & 0x7) + 1;
1211 if (channel_count > max_channels)
1212 max_channels = channel_count;
1213
1214 /* Advance to next block */
1215 sad += 3;
1216 }
1217
1218 return max_channels;
1219}
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001220
1221/* Delay in Us */
1222int64_t platform_render_latency(audio_usecase_t usecase)
1223{
1224 switch (usecase) {
1225 case USECASE_AUDIO_PLAYBACK_DEEP_BUFFER:
1226 return DEEP_BUFFER_PLATFORM_DELAY;
1227 case USECASE_AUDIO_PLAYBACK_LOW_LATENCY:
1228 return LOW_LATENCY_PLATFORM_DELAY;
1229 default:
1230 return 0;
1231 }
1232}