blob: 11521e87478b03ae56497838a69a5ce6255530cf [file] [log] [blame]
Yamit Mehta1d2e4742016-05-18 15:25:12 +05301/*
2 * Copyright (C) 2016 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 "msm8916_platform"
18/*#define LOG_NDEBUG 0*/
19#define LOG_NDDEBUG 0
20
21#include <stdlib.h>
22#include <dlfcn.h>
23#include <fcntl.h>
24#include <sys/ioctl.h>
25#include <cutils/log.h>
26#include <cutils/properties.h>
27#include <cutils/str_parms.h>
28#include <audio_hw.h>
29#include <platform_api.h>
30#include "platform.h"
31#include "audio_extn.h"
32#include "voice_extn.h"
33#include "sound/msmcal-hwdep.h"
34#include <dirent.h>
35#define MAX_MIXER_XML_PATH 100
36#define MIXER_XML_PATH "/system/etc/mixer_paths.xml"
37#define MIXER_XML_PATH_MTP "/system/etc/mixer_paths_mtp.xml"
38#define MIXER_XML_PATH_MSM8909_PM8916 "/system/etc/mixer_paths_msm8909_pm8916.xml"
39#define MIXER_XML_PATH_L9300 "/system/etc/mixer_paths_l9300.xml"
40
41#define LIB_ACDB_LOADER "libacdbloader.so"
42#define AUDIO_DATA_BLOCK_MIXER_CTL "HDMI EDID"
43#define CVD_VERSION_MIXER_CTL "CVD Version"
44
45/*
46 * This file will have a maximum of 38 bytes:
47 *
48 * 4 bytes: number of audio blocks
49 * 4 bytes: total length of Short Audio Descriptor (SAD) blocks
50 * Maximum 10 * 3 bytes: SAD blocks
51 */
52#define MAX_SAD_BLOCKS 10
53#define SAD_BLOCK_SIZE 3
54#define MAX_CVD_VERSION_STRING_SIZE 100
55
56/* EDID format ID for LPCM audio */
57#define EDID_FORMAT_LPCM 1
58
59/* Retry for delay in FW loading*/
60#define RETRY_NUMBER 20
61#define RETRY_US 500000
62#define MAX_SND_CARD 8
63
64#define SAMPLE_RATE_8KHZ 8000
65#define SAMPLE_RATE_16KHZ 16000
66
67#define MAX_SET_CAL_BYTE_SIZE 65536
68
69#define MAX_CAL_NAME 20
70
71#define QMIC_FLAG 0x00000004
72
73char cal_name_info[WCD9XXX_MAX_CAL][MAX_CAL_NAME] = {
74 [WCD9XXX_MBHC_CAL] = "mbhc_cal",
75};
76
77struct audio_block_header
78{
79 int reserved;
80 int length;
81};
82
83enum {
84 CAL_MODE_SEND = 0x1,
85 CAL_MODE_PERSIST = 0x2,
86 CAL_MODE_RTAC = 0x4
87};
88
89#define PLATFORM_CONFIG_KEY_OPERATOR_INFO "operator_info"
90
91struct operator_info {
92 struct listnode list;
93 char *name;
94 char *mccmnc;
95};
96
97struct operator_specific_device {
98 struct listnode list;
99 char *operator;
100 char *mixer_path;
101 int acdb_id;
102};
103
104static struct listnode operator_info_list;
105static struct listnode *operator_specific_device_table[SND_DEVICE_MAX];
106
107/* Audio calibration related functions */
108typedef void (*acdb_deallocate_t)();
109typedef int (*acdb_init_v2_cvd_t)(const char *, char *, int);
110typedef void (*acdb_send_audio_cal_t)(int, int);
111typedef void (*acdb_send_voice_cal_t)(int, int);
112typedef int (*acdb_reload_vocvoltable_t)(int);
113typedef int (*acdb_loader_get_calibration_t)(char *attr, int size, void *data);
114acdb_loader_get_calibration_t acdb_loader_get_calibration;
115
116struct platform_data {
117 struct audio_device *adev;
118 bool fluence_in_spkr_mode;
119 bool fluence_in_voice_call;
120 bool fluence_in_voice_rec;
121 int fluence_type;
122 char fluence_cap[PROPERTY_VALUE_MAX];
123 int fluence_mode;
124 bool ec_ref_enabled;
125 bool gsm_mode_enabled;
126 /* Audio calibration related functions */
127 void *acdb_handle;
128 acdb_init_v2_cvd_t acdb_init;
129 acdb_deallocate_t acdb_deallocate;
130 acdb_send_audio_cal_t acdb_send_audio_cal;
131 acdb_send_voice_cal_t acdb_send_voice_cal;
132 acdb_reload_vocvoltable_t acdb_reload_vocvoltable;
133 void *hw_info;
134 char ec_ref_mixer_path[64];
135 bool speaker_lr_swap;
136};
137
138int pcm_device_table[AUDIO_USECASE_MAX][2] = {
139 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {DEEP_BUFFER_PCM_DEVICE,
140 DEEP_BUFFER_PCM_DEVICE},
141 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {LOWLATENCY_PCM_DEVICE,
142 LOWLATENCY_PCM_DEVICE},
143 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {MULTIMEDIA2_PCM_DEVICE,
144 MULTIMEDIA2_PCM_DEVICE},
145 [USECASE_AUDIO_PLAYBACK_OFFLOAD] =
146 {PLAYBACK_OFFLOAD_DEVICE, PLAYBACK_OFFLOAD_DEVICE},
147 [USECASE_AUDIO_PLAYBACK_ULL] = {MULTIMEDIA3_PCM_DEVICE, MULTIMEDIA3_PCM_DEVICE},
148 [USECASE_AUDIO_RECORD] = {AUDIO_RECORD_PCM_DEVICE, AUDIO_RECORD_PCM_DEVICE},
149 [USECASE_AUDIO_RECORD_LOW_LATENCY] = {LOWLATENCY_PCM_DEVICE,
150 LOWLATENCY_PCM_DEVICE},
151 [USECASE_AUDIO_HFP_SCO] = {HFP_PCM_RX, HFP_SCO_RX},
152 [USECASE_AUDIO_HFP_SCO_WB] = {HFP_PCM_RX, HFP_SCO_RX},
153 [USECASE_VOICE_CALL] = {VOICE_CALL_PCM_DEVICE, VOICE_CALL_PCM_DEVICE},
154 [USECASE_VOICE2_CALL] = {VOICE2_CALL_PCM_DEVICE, VOICE2_CALL_PCM_DEVICE},
155 [USECASE_VOLTE_CALL] = {VOLTE_CALL_PCM_DEVICE, VOLTE_CALL_PCM_DEVICE},
156 [USECASE_QCHAT_CALL] = {QCHAT_CALL_PCM_DEVICE, QCHAT_CALL_PCM_DEVICE},
157 [USECASE_VOWLAN_CALL] = {VOWLAN_CALL_PCM_DEVICE, VOWLAN_CALL_PCM_DEVICE},
158 [USECASE_VOICEMMODE1_CALL] = {-1, -1}, /* pcm ids updated from platform info file */
159 [USECASE_VOICEMMODE2_CALL] = {-1, -1}, /* pcm ids updated from platform info file */
160 [USECASE_INCALL_REC_UPLINK] = {AUDIO_RECORD_PCM_DEVICE,
161 AUDIO_RECORD_PCM_DEVICE},
162 [USECASE_INCALL_REC_DOWNLINK] = {AUDIO_RECORD_PCM_DEVICE,
163 AUDIO_RECORD_PCM_DEVICE},
164 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = {AUDIO_RECORD_PCM_DEVICE,
165 AUDIO_RECORD_PCM_DEVICE},
166 [USECASE_AUDIO_SPKR_CALIB_RX] = {SPKR_PROT_CALIB_RX_PCM_DEVICE, -1},
167 [USECASE_AUDIO_SPKR_CALIB_TX] = {-1, SPKR_PROT_CALIB_TX_PCM_DEVICE},
168 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = {AFE_PROXY_PLAYBACK_PCM_DEVICE,
169 AFE_PROXY_RECORD_PCM_DEVICE},
170 [USECASE_AUDIO_RECORD_AFE_PROXY] = {AFE_PROXY_PLAYBACK_PCM_DEVICE,
171 AFE_PROXY_RECORD_PCM_DEVICE},
172};
173
174/* Array to store sound devices */
175static const char * const device_table[SND_DEVICE_MAX] = {
176 [SND_DEVICE_NONE] = "none",
177 /* Playback sound devices */
178 [SND_DEVICE_OUT_HANDSET] = "handset",
179 [SND_DEVICE_OUT_SPEAKER] = "speaker",
180 [SND_DEVICE_OUT_SPEAKER_REVERSE] = "speaker-reverse",
181 [SND_DEVICE_OUT_HEADPHONES] = "headphones",
182 [SND_DEVICE_OUT_LINE] = "line",
183 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
184 [SND_DEVICE_OUT_SPEAKER_AND_LINE] = "speaker-and-line",
185 [SND_DEVICE_OUT_VOICE_HANDSET] = "voice-handset",
186 [SND_DEVICE_OUT_VOICE_HAC_HANDSET] = "voice-hac-handset",
187 [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
Devin Kim152b09a2016-06-23 13:53:04 -0700188 [SND_DEVICE_OUT_VOICE_SPEAKER_HFP] = "voice-speaker-hfp",
Yamit Mehta1d2e4742016-05-18 15:25:12 +0530189 [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
190 [SND_DEVICE_OUT_VOICE_LINE] = "voice-line",
191 [SND_DEVICE_OUT_HDMI] = "hdmi",
192 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
193 [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
194 [SND_DEVICE_OUT_BT_SCO_WB] = "bt-sco-headset-wb",
195 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
196 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
197 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
198 [SND_DEVICE_OUT_VOICE_TX] = "voice-tx",
199 [SND_DEVICE_OUT_AFE_PROXY] = "afe-proxy",
200 [SND_DEVICE_OUT_USB_HEADSET] = "usb-headphones",
201 [SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] = "speaker-and-usb-headphones",
202 [SND_DEVICE_OUT_SPEAKER_PROTECTED] = "speaker-protected",
203 [SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED] = "voice-speaker-protected",
204
205 /* Capture sound devices */
206 [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
207 [SND_DEVICE_IN_HANDSET_MIC_EXTERNAL] = "handset-mic-ext",
208 [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic",
209 [SND_DEVICE_IN_HANDSET_MIC_NS] = "handset-mic",
210 [SND_DEVICE_IN_HANDSET_MIC_AEC_NS] = "handset-mic",
211 [SND_DEVICE_IN_HANDSET_DMIC] = "dmic-endfire",
212 [SND_DEVICE_IN_HANDSET_DMIC_AEC] = "dmic-endfire",
213 [SND_DEVICE_IN_HANDSET_DMIC_NS] = "dmic-endfire",
214 [SND_DEVICE_IN_HANDSET_DMIC_AEC_NS] = "dmic-endfire",
215 [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
216 [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "speaker-mic",
217 [SND_DEVICE_IN_SPEAKER_MIC_NS] = "speaker-mic",
218 [SND_DEVICE_IN_SPEAKER_MIC_AEC_NS] = "speaker-mic",
219 [SND_DEVICE_IN_SPEAKER_DMIC] = "speaker-dmic-endfire",
220 [SND_DEVICE_IN_SPEAKER_DMIC_AEC] = "speaker-dmic-endfire",
221 [SND_DEVICE_IN_SPEAKER_DMIC_NS] = "speaker-dmic-endfire",
222 [SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS] = "speaker-dmic-endfire",
223 [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
224 [SND_DEVICE_IN_HEADSET_MIC_FLUENCE] = "headset-mic",
225 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
Devin Kim152b09a2016-06-23 13:53:04 -0700226 [SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP] = "voice-speaker-mic-hfp",
Yamit Mehta1d2e4742016-05-18 15:25:12 +0530227 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
228 [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
229 [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
230 [SND_DEVICE_IN_BT_SCO_MIC_NREC] = "bt-sco-mic",
231 [SND_DEVICE_IN_BT_SCO_MIC_WB] = "bt-sco-mic-wb",
232 [SND_DEVICE_IN_BT_SCO_MIC_WB_NREC] = "bt-sco-mic-wb",
233 [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
234 [SND_DEVICE_IN_VOICE_DMIC] = "voice-dmic-ef",
235 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC] = "voice-speaker-dmic-ef",
236 [SND_DEVICE_IN_VOICE_SPEAKER_QMIC] = "voice-speaker-qmic",
237 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
238 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
239 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
240 [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
241 [SND_DEVICE_IN_VOICE_REC_MIC_NS] = "voice-rec-mic",
242 [SND_DEVICE_IN_VOICE_REC_DMIC_STEREO] = "voice-rec-dmic-ef",
243 [SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE] = "voice-rec-dmic-ef-fluence",
244 [SND_DEVICE_IN_VOICE_RX] = "voice-rx",
245 [SND_DEVICE_IN_USB_HEADSET_MIC] = "usb-headset-mic",
246 [SND_DEVICE_IN_CAPTURE_FM] = "capture-fm",
247 [SND_DEVICE_IN_AANC_HANDSET_MIC] = "aanc-handset-mic",
248 [SND_DEVICE_IN_QUAD_MIC] = "quad-mic",
249 [SND_DEVICE_IN_HANDSET_STEREO_DMIC] = "handset-stereo-dmic-ef",
250 [SND_DEVICE_IN_SPEAKER_STEREO_DMIC] = "speaker-stereo-dmic-ef",
251 [SND_DEVICE_IN_CAPTURE_VI_FEEDBACK] = "vi-feedback",
252 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BROADSIDE] = "voice-speaker-dmic-broadside",
253 [SND_DEVICE_IN_SPEAKER_DMIC_BROADSIDE] = "speaker-dmic-broadside",
254 [SND_DEVICE_IN_SPEAKER_DMIC_AEC_BROADSIDE] = "speaker-dmic-broadside",
255 [SND_DEVICE_IN_SPEAKER_DMIC_NS_BROADSIDE] = "speaker-dmic-broadside",
256 [SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS_BROADSIDE] = "speaker-dmic-broadside",
257 [SND_DEVICE_IN_HANDSET_QMIC] = "quad-mic",
258 [SND_DEVICE_IN_SPEAKER_QMIC_AEC] = "quad-mic",
259 [SND_DEVICE_IN_SPEAKER_QMIC_NS] = "quad-mic",
260 [SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS] = "quad-mic",
261};
262
263/* ACDB IDs (audio DSP path configuration IDs) for each sound device */
264static int acdb_device_table[SND_DEVICE_MAX] = {
265 [SND_DEVICE_NONE] = -1,
266 [SND_DEVICE_OUT_HANDSET] = 7,
267 [SND_DEVICE_OUT_SPEAKER] = 14,
268 [SND_DEVICE_OUT_SPEAKER_REVERSE] = 14,
269 [SND_DEVICE_OUT_LINE] = 10,
270 [SND_DEVICE_OUT_HEADPHONES] = 10,
271 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
272 [SND_DEVICE_OUT_SPEAKER_AND_LINE] = 10,
273 [SND_DEVICE_OUT_VOICE_HANDSET] = 7,
274 [SND_DEVICE_OUT_VOICE_HAC_HANDSET] = 53,
275 [SND_DEVICE_OUT_VOICE_LINE] = 10,
276 [SND_DEVICE_OUT_VOICE_SPEAKER] = 14,
277 [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
278 [SND_DEVICE_OUT_HDMI] = 18,
279 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 14,
280 [SND_DEVICE_OUT_BT_SCO] = 22,
281 [SND_DEVICE_OUT_BT_SCO_WB] = 39,
282 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
283 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
284 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
285 [SND_DEVICE_OUT_VOICE_TX] = 45,
286 [SND_DEVICE_OUT_AFE_PROXY] = 0,
287 [SND_DEVICE_OUT_USB_HEADSET] = 45,
288 [SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] = 14,
289 [SND_DEVICE_OUT_SPEAKER_PROTECTED] = 124,
290 [SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED] = 101,
Devin Kim152b09a2016-06-23 13:53:04 -0700291 [SND_DEVICE_OUT_VOICE_SPEAKER_HFP] = 14,
Yamit Mehta1d2e4742016-05-18 15:25:12 +0530292
293 [SND_DEVICE_IN_HANDSET_MIC] = 4,
294 [SND_DEVICE_IN_HANDSET_MIC_EXTERNAL] = 4,
295 [SND_DEVICE_IN_HANDSET_MIC_AEC] = 106,
296 [SND_DEVICE_IN_HANDSET_MIC_NS] = 107,
297 [SND_DEVICE_IN_HANDSET_MIC_AEC_NS] = 108,
298 [SND_DEVICE_IN_HANDSET_DMIC] = 41,
299 [SND_DEVICE_IN_HANDSET_DMIC_AEC] = 109,
300 [SND_DEVICE_IN_HANDSET_DMIC_NS] = 110,
301 [SND_DEVICE_IN_HANDSET_DMIC_AEC_NS] = 111,
302 [SND_DEVICE_IN_SPEAKER_MIC] = 11,
303 [SND_DEVICE_IN_SPEAKER_MIC_AEC] = 112,
304 [SND_DEVICE_IN_SPEAKER_MIC_NS] = 113,
305 [SND_DEVICE_IN_SPEAKER_MIC_AEC_NS] = 114,
306 [SND_DEVICE_IN_SPEAKER_DMIC] = 43,
307 [SND_DEVICE_IN_SPEAKER_DMIC_AEC] = 115,
308 [SND_DEVICE_IN_SPEAKER_DMIC_NS] = 116,
309 [SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS] = 117,
310 [SND_DEVICE_IN_HEADSET_MIC] = 8,
311 [SND_DEVICE_IN_HEADSET_MIC_FLUENCE] = 47,
312 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
313 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8,
314 [SND_DEVICE_IN_HDMI_MIC] = 4,
315 [SND_DEVICE_IN_BT_SCO_MIC] = 21,
316 [SND_DEVICE_IN_BT_SCO_MIC_NREC] = 122,
317 [SND_DEVICE_IN_BT_SCO_MIC_WB] = 38,
318 [SND_DEVICE_IN_BT_SCO_MIC_WB_NREC] = 123,
319 [SND_DEVICE_IN_CAMCORDER_MIC] = 4,
320 [SND_DEVICE_IN_VOICE_DMIC] = 41,
Devin Kim152b09a2016-06-23 13:53:04 -0700321 [SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP] = 11,
Yamit Mehta1d2e4742016-05-18 15:25:12 +0530322 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC] = 43,
323 [SND_DEVICE_IN_VOICE_SPEAKER_QMIC] = 19,
324 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
325 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
326 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
327 [SND_DEVICE_IN_VOICE_REC_MIC] = 4,
328 [SND_DEVICE_IN_VOICE_REC_MIC_NS] = 107,
329 [SND_DEVICE_IN_VOICE_REC_DMIC_STEREO] = 34,
330 [SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE] = 41,
331 [SND_DEVICE_IN_VOICE_RX] = 44,
332 [SND_DEVICE_IN_USB_HEADSET_MIC] = 44,
333 [SND_DEVICE_IN_CAPTURE_FM] = 0,
334 [SND_DEVICE_IN_AANC_HANDSET_MIC] = 104,
335 [SND_DEVICE_IN_QUAD_MIC] = 46,
336 [SND_DEVICE_IN_HANDSET_STEREO_DMIC] = 34,
337 [SND_DEVICE_IN_SPEAKER_STEREO_DMIC] = 35,
338 [SND_DEVICE_IN_CAPTURE_VI_FEEDBACK] = 102,
339 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BROADSIDE] = 12,
340 [SND_DEVICE_IN_SPEAKER_DMIC_BROADSIDE] = 12,
341 [SND_DEVICE_IN_SPEAKER_DMIC_AEC_BROADSIDE] = 119,
342 [SND_DEVICE_IN_SPEAKER_DMIC_NS_BROADSIDE] = 121,
343 [SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS_BROADSIDE] = 120,
344 [SND_DEVICE_IN_HANDSET_QMIC] = 125,
345 [SND_DEVICE_IN_SPEAKER_QMIC_AEC] = 126,
346 [SND_DEVICE_IN_SPEAKER_QMIC_NS] = 127,
347 [SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS] = 129,
348};
349
350struct name_to_index {
351 char name[100];
352 unsigned int index;
353};
354
355#define TO_NAME_INDEX(X) #X, X
356
357/* Used to get index from parsed sting */
358static struct name_to_index snd_device_name_index[SND_DEVICE_MAX] = {
359 {TO_NAME_INDEX(SND_DEVICE_OUT_HANDSET)},
360 {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER)},
361 {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_REVERSE)},
362 {TO_NAME_INDEX(SND_DEVICE_OUT_HEADPHONES)},
363 {TO_NAME_INDEX(SND_DEVICE_OUT_LINE)},
364 {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES)},
365 {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_LINE)},
366 {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_HANDSET)},
367 {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_HAC_HANDSET)},
368 {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_SPEAKER)},
Devin Kim152b09a2016-06-23 13:53:04 -0700369 {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_SPEAKER_HFP)},
Yamit Mehta1d2e4742016-05-18 15:25:12 +0530370 {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_HEADPHONES)},
371 {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_LINE)},
372 {TO_NAME_INDEX(SND_DEVICE_OUT_HDMI)},
373 {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_HDMI)},
374 {TO_NAME_INDEX(SND_DEVICE_OUT_BT_SCO)},
375 {TO_NAME_INDEX(SND_DEVICE_OUT_BT_SCO_WB)},
376 {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES)},
377 {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES)},
378 {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET)},
379 {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TX)},
380 {TO_NAME_INDEX(SND_DEVICE_OUT_AFE_PROXY)},
381 {TO_NAME_INDEX(SND_DEVICE_OUT_USB_HEADSET)},
382 {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET)},
383 {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_PROTECTED)},
384 {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED)},
385 {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC)},
386 {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC_EXTERNAL)},
387 {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC_AEC)},
388 {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC_NS)},
389 {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC_AEC_NS)},
390 {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC)},
391 {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC_AEC)},
392 {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC_NS)},
393 {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC_AEC_NS)},
394 {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_MIC)},
395 {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_MIC_AEC)},
396 {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_MIC_NS)},
397 {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_MIC_AEC_NS)},
398 {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC)},
399 {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC)},
400 {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_NS)},
401 {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS)},
402 {TO_NAME_INDEX(SND_DEVICE_IN_HEADSET_MIC)},
403 {TO_NAME_INDEX(SND_DEVICE_IN_HEADSET_MIC_FLUENCE)},
404 {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_SPEAKER_MIC)},
Devin Kim152b09a2016-06-23 13:53:04 -0700405 {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP)},
Yamit Mehta1d2e4742016-05-18 15:25:12 +0530406 {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_HEADSET_MIC)},
407 {TO_NAME_INDEX(SND_DEVICE_IN_HDMI_MIC)},
408 {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC)},
409 {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC_NREC)},
410 {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC_WB)},
411 {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC_WB_NREC)},
412 {TO_NAME_INDEX(SND_DEVICE_IN_CAMCORDER_MIC)},
413 {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_DMIC)},
414 {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_SPEAKER_DMIC)},
415 {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_SPEAKER_QMIC)},
416 {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC)},
417 {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC)},
418 {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC)},
419 {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_MIC)},
420 {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_MIC_NS)},
421 {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_DMIC_STEREO)},
422 {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE)},
423 {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_RX)},
424 {TO_NAME_INDEX(SND_DEVICE_IN_USB_HEADSET_MIC)},
425 {TO_NAME_INDEX(SND_DEVICE_IN_CAPTURE_FM)},
426 {TO_NAME_INDEX(SND_DEVICE_IN_AANC_HANDSET_MIC)},
427 {TO_NAME_INDEX(SND_DEVICE_IN_QUAD_MIC)},
428 {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_STEREO_DMIC)},
429 {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_STEREO_DMIC)},
430 {TO_NAME_INDEX(SND_DEVICE_IN_CAPTURE_VI_FEEDBACK)},
431 {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BROADSIDE)},
432 {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_BROADSIDE)},
433 {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC_BROADSIDE)},
434 {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_NS_BROADSIDE)},
435 {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS_BROADSIDE)},
436 {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_QMIC)},
437 {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_QMIC_AEC)},
438 {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_QMIC_NS)},
439 {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS)},
440};
441
442static char * backend_table[SND_DEVICE_MAX] = {0};
443static char * hw_interface_table[SND_DEVICE_MAX] = {0};
444
445static struct name_to_index usecase_name_index[AUDIO_USECASE_MAX] = {
446 {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_DEEP_BUFFER)},
447 {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_LOW_LATENCY)},
448 {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_MULTI_CH)},
449 {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_OFFLOAD)},
450 {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_ULL)},
451 {TO_NAME_INDEX(USECASE_AUDIO_RECORD)},
452 {TO_NAME_INDEX(USECASE_AUDIO_RECORD_LOW_LATENCY)},
453 {TO_NAME_INDEX(USECASE_VOICE_CALL)},
454 {TO_NAME_INDEX(USECASE_VOICE2_CALL)},
455 {TO_NAME_INDEX(USECASE_VOLTE_CALL)},
456 {TO_NAME_INDEX(USECASE_QCHAT_CALL)},
457 {TO_NAME_INDEX(USECASE_VOWLAN_CALL)},
458 {TO_NAME_INDEX(USECASE_VOICEMMODE1_CALL)},
459 {TO_NAME_INDEX(USECASE_VOICEMMODE2_CALL)},
460 {TO_NAME_INDEX(USECASE_AUDIO_HFP_SCO)},
461 {TO_NAME_INDEX(USECASE_AUDIO_SPKR_CALIB_TX)},
462};
463
464#define DEEP_BUFFER_PLATFORM_DELAY (29*1000LL)
465#define LOW_LATENCY_PLATFORM_DELAY (13*1000LL)
466
467static void query_platform(const char *snd_card_name,
468 char *mixer_xml_path)
469{
470 if (!strncmp(snd_card_name, "msm8x16-snd-card-mtp",
471 sizeof("msm8x16-snd-card-mtp"))) {
472 strlcpy(mixer_xml_path, MIXER_XML_PATH_MTP,
473 sizeof(MIXER_XML_PATH_MTP));
474 } else if (!strncmp(snd_card_name, "msm8909-pm8916-snd-card",
475 sizeof("msm8909-pm8916-snd-card"))) {
476 strlcpy(mixer_xml_path, MIXER_XML_PATH_MSM8909_PM8916,
477 sizeof(MIXER_XML_PATH_MSM8909_PM8916));
478 } else if (!strncmp(snd_card_name, "msm8952-snd-card-mtp",
479 sizeof("msm8952-snd-card-mtp"))) {
480 strlcpy(mixer_xml_path, MIXER_XML_PATH_MTP,
481 sizeof(MIXER_XML_PATH_MTP));
482 } else if (!strncmp(snd_card_name, "msm8952-l9300-snd-card",
483 sizeof("msm8952-l9300-snd-card"))) {
484 strlcpy(mixer_xml_path, MIXER_XML_PATH_L9300,
485 sizeof(MIXER_XML_PATH_L9300));
486 } else {
487 strlcpy(mixer_xml_path, MIXER_XML_PATH,
488 sizeof(MIXER_XML_PATH));
489 }
490}
491
492static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
493static bool is_tmus = false;
494
495static void check_operator()
496{
497 char value[PROPERTY_VALUE_MAX];
498 int mccmnc;
499 property_get("gsm.sim.operator.numeric",value,"0");
500 mccmnc = atoi(value);
501 ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
502 switch(mccmnc) {
503 /* TMUS MCC(310), MNC(490, 260, 026) */
504 case 310490:
505 case 310260:
506 case 310026:
507 /* Add new TMUS MNC(800, 660, 580, 310, 270, 250, 240, 230, 220, 210, 200, 160) */
508 case 310800:
509 case 310660:
510 case 310580:
511 case 310310:
512 case 310270:
513 case 310250:
514 case 310240:
515 case 310230:
516 case 310220:
517 case 310210:
518 case 310200:
519 case 310160:
520 is_tmus = true;
521 break;
522 }
523}
524
525bool is_operator_tmus()
526{
527 pthread_once(&check_op_once_ctl, check_operator);
528 return is_tmus;
529}
530
531static char *get_current_operator()
532{
533 struct listnode *node;
534 struct operator_info *info_item;
535 char mccmnc[PROPERTY_VALUE_MAX];
536 char *ret = NULL;
537
538 property_get("gsm.sim.operator.numeric",mccmnc,"0");
539
540 list_for_each(node, &operator_info_list) {
541 info_item = node_to_item(node, struct operator_info, list);
542 if (strstr(info_item->mccmnc, mccmnc) != NULL) {
543 ret = info_item->name;
544 }
545 }
546
547 return ret;
548}
549
550static struct operator_specific_device *get_operator_specific_device(snd_device_t snd_device)
551{
552 struct listnode *node;
553 struct operator_specific_device *ret = NULL;
554 struct operator_specific_device *device_item;
555 char *operator_name;
556
557 operator_name = get_current_operator();
558 if (operator_name == NULL)
559 return ret;
560
561 list_for_each(node, operator_specific_device_table[snd_device]) {
562 device_item = node_to_item(node, struct operator_specific_device, list);
563 if (strcmp(operator_name, device_item->operator) == 0) {
564 ret = device_item;
565 }
566 }
567
568 return ret;
569}
570
571static int get_operator_specific_device_acdb_id(snd_device_t snd_device)
572{
573 struct operator_specific_device *device;
574 int ret = acdb_device_table[snd_device];
575
576 device = get_operator_specific_device(snd_device);
577 if (device != NULL)
578 ret = device->acdb_id;
579
580 return ret;
581}
582
583static const char *get_operator_specific_device_mixer_path(snd_device_t snd_device)
584{
585 struct operator_specific_device *device;
586 const char *ret = device_table[snd_device];
587
588 device = get_operator_specific_device(snd_device);
589 if (device != NULL)
590 ret = device->mixer_path;
591
592 return ret;
593}
594
595bool platform_send_gain_dep_cal(void *platform __unused, int level __unused)
596{
597 return 0;
598}
599
600void platform_set_echo_reference(struct audio_device *adev, bool enable,
601 audio_devices_t out_device)
602{
603 struct platform_data *my_data = (struct platform_data *)adev->platform;
604 snd_device_t snd_device = SND_DEVICE_NONE;
605
606 if (strcmp(my_data->ec_ref_mixer_path, "")) {
607 ALOGV("%s: disabling %s", __func__, my_data->ec_ref_mixer_path);
608 audio_route_reset_and_update_path(adev->audio_route,
609 my_data->ec_ref_mixer_path);
610 }
611
612 if (enable) {
613 if (out_device != AUDIO_DEVICE_NONE) {
614 snd_device = platform_get_output_snd_device(adev->platform, out_device);
615 platform_add_backend_name(adev->platform, my_data->ec_ref_mixer_path, snd_device);
616 }
617
618 strlcpy(my_data->ec_ref_mixer_path, "echo-reference",
619 sizeof(my_data->ec_ref_mixer_path));
620
621 ALOGD("%s: enabling %s", __func__, my_data->ec_ref_mixer_path);
622 audio_route_apply_and_update_path(adev->audio_route,
623 my_data->ec_ref_mixer_path);
624 }
625}
626
627static void set_platform_defaults()
628{
629 int32_t dev;
630 for (dev = 0; dev < SND_DEVICE_MAX; dev++) {
631 backend_table[dev] = NULL;
632 hw_interface_table[dev] = NULL;
633 }
634
635 // TBD - do these go to the platform-info.xml file.
636 // will help in avoiding strdups here
637 backend_table[SND_DEVICE_IN_BT_SCO_MIC] = strdup("bt-sco");
638 backend_table[SND_DEVICE_IN_BT_SCO_MIC_WB] = strdup("bt-sco-wb");
639 backend_table[SND_DEVICE_IN_BT_SCO_MIC_NREC] = strdup("bt-sco");
640 backend_table[SND_DEVICE_IN_BT_SCO_MIC_WB_NREC] = strdup("bt-sco-wb");
641 backend_table[SND_DEVICE_OUT_BT_SCO] = strdup("bt-sco");
642 backend_table[SND_DEVICE_OUT_BT_SCO_WB] = strdup("bt-sco-wb");
643 backend_table[SND_DEVICE_OUT_HDMI] = strdup("hdmi");
644 backend_table[SND_DEVICE_OUT_SPEAKER_AND_HDMI] = strdup("speaker-and-hdmi");
645 backend_table[SND_DEVICE_OUT_VOICE_TX] = strdup("afe-proxy");
646 backend_table[SND_DEVICE_IN_VOICE_RX] = strdup("afe-proxy");
647 backend_table[SND_DEVICE_OUT_AFE_PROXY] = strdup("afe-proxy");
648 backend_table[SND_DEVICE_OUT_USB_HEADSET] = strdup("usb-headphones");
649 backend_table[SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] =
650 strdup("speaker-and-usb-headphones");
651 backend_table[SND_DEVICE_IN_USB_HEADSET_MIC] = strdup("usb-headset-mic");
652 backend_table[SND_DEVICE_IN_CAPTURE_FM] = strdup("capture-fm");
653}
654
655void get_cvd_version(char *cvd_version, struct audio_device *adev)
656{
657 struct mixer_ctl *ctl;
658 int count;
659 int ret = 0;
660
661 ctl = mixer_get_ctl_by_name(adev->mixer, CVD_VERSION_MIXER_CTL);
662 if (!ctl) {
663 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__, CVD_VERSION_MIXER_CTL);
664 goto done;
665 }
666 mixer_ctl_update(ctl);
667
668 count = mixer_ctl_get_num_values(ctl);
669 if (count > MAX_CVD_VERSION_STRING_SIZE)
670 count = MAX_CVD_VERSION_STRING_SIZE -1;
671
672 ret = mixer_ctl_get_array(ctl, cvd_version, count);
673 if (ret != 0) {
674 ALOGE("%s: ERROR! mixer_ctl_get_array() failed to get CVD Version", __func__);
675 goto done;
676 }
677
678done:
679 return;
680}
681
682static int hw_util_open(int card_no)
683{
684 int fd = -1;
685 char dev_name[256];
686
687 snprintf(dev_name, sizeof(dev_name), "/dev/snd/hwC%uD%u",
688 card_no, WCD9XXX_CODEC_HWDEP_NODE);
689 ALOGD("%s Opening device %s\n", __func__, dev_name);
690 fd = open(dev_name, O_WRONLY);
691 if (fd < 0) {
692 ALOGE("%s: cannot open device '%s'\n", __func__, dev_name);
693 return fd;
694 }
695 ALOGD("%s success", __func__);
696 return fd;
697}
698
699struct param_data {
700 int use_case;
701 int acdb_id;
702 int get_size;
703 int buff_size;
704 int data_size;
705 void *buff;
706};
707
708static int send_codec_cal(acdb_loader_get_calibration_t acdb_loader_get_calibration,
709 struct platform_data *plat_data __unused, int fd)
710{
711 int ret = 0, type;
712
713 for (type = WCD9XXX_ANC_CAL; type < WCD9XXX_MAX_CAL; type++) {
714 struct wcdcal_ioctl_buffer codec_buffer;
715 struct param_data calib;
716
717 if (type != WCD9XXX_MBHC_CAL)
718 continue;
719
720 calib.get_size = 1;
721 ret = acdb_loader_get_calibration(cal_name_info[type], sizeof(struct param_data),
722 &calib);
723 if (ret < 0) {
724 ALOGE("%s get_calibration failed\n", __func__);
725 return ret;
726 }
727 calib.get_size = 0;
728 calib.buff = malloc(calib.buff_size);
729 if(calib.buff == NULL) {
730 ALOGE("%s mem allocation for %d bytes for %s failed\n"
731 , __func__, calib.buff_size, cal_name_info[type]);
732 return -1;
733 }
734 ret = acdb_loader_get_calibration(cal_name_info[type],
735 sizeof(struct param_data), &calib);
736 if (ret < 0) {
737 ALOGE("%s get_calibration failed type=%s calib.size=%d\n"
738 , __func__, cal_name_info[type], codec_buffer.size);
739 free(calib.buff);
740 return ret;
741 }
742 codec_buffer.buffer = calib.buff;
743 codec_buffer.size = calib.data_size;
744 codec_buffer.cal_type = type;
745 if (ioctl(fd, SNDRV_CTL_IOCTL_HWDEP_CAL_TYPE, &codec_buffer) < 0)
746 ALOGE("Failed to call ioctl for %s err=%d calib.size=%d",
747 cal_name_info[type], errno, codec_buffer.size);
748 ALOGD("%s cal sent for %s calib.size=%d"
749 , __func__, cal_name_info[type], codec_buffer.size);
750 free(calib.buff);
751 }
752 return ret;
753}
754
755static void audio_hwdep_send_cal(struct platform_data *plat_data)
756{
757 int fd;
758
759 fd = hw_util_open(plat_data->adev->snd_card);
760 if (fd == -1) {
761 ALOGE("%s error open\n", __func__);
762 return;
763 }
764
765 acdb_loader_get_calibration = (acdb_loader_get_calibration_t)
766 dlsym(plat_data->acdb_handle, "acdb_loader_get_calibration");
767
768 if (acdb_loader_get_calibration == NULL) {
769 ALOGE("%s: ERROR. dlsym Error:%s acdb_loader_get_calibration", __func__,
770 dlerror());
771 return;
772 }
773 if (send_codec_cal(acdb_loader_get_calibration, plat_data, fd) < 0)
774 ALOGE("%s: Could not send anc cal", __FUNCTION__);
775}
776
777
778int platform_acdb_init(void *platform)
779{
780 struct platform_data *my_data = (struct platform_data *)platform;
781 char *cvd_version = NULL;
782 int key = 0;
783 const char *snd_card_name;
784 int result;
785 char value[PROPERTY_VALUE_MAX];
786 cvd_version = calloc(1, MAX_CVD_VERSION_STRING_SIZE);
787 if (!cvd_version)
788 ALOGE("Failed to allocate cvd version");
789 else
790 get_cvd_version(cvd_version, my_data->adev);
791
792 property_get("audio.ds1.metainfo.key",value,"0");
793 key = atoi(value);
794 snd_card_name = mixer_get_name(my_data->adev->mixer);
795
796 result = my_data->acdb_init(snd_card_name, cvd_version, key);
797
798 if (cvd_version)
799 free(cvd_version);
800 if (!result) {
801 ALOGD("ACDB initialized");
802 audio_hwdep_send_cal(my_data);
803 } else {
804 ALOGD("ACDB initialization failed");
805 }
806 return result;
807}
808
809void *platform_init(struct audio_device *adev)
810{
811 char platform[PROPERTY_VALUE_MAX] = {0};
812 char baseband[PROPERTY_VALUE_MAX] = {0};
813 char value[PROPERTY_VALUE_MAX] = {0};
814 struct platform_data *my_data = NULL;
815 int retry_num = 0, snd_card_num = 0, key = 0;
816 const char *snd_card_name;
817 char mixer_xml_path[MAX_MIXER_XML_PATH] = {0};
818 char ffspEnable[PROPERTY_VALUE_MAX] = {0};
819 char *cvd_version = NULL;
820 int idx;
821
822 my_data = calloc(1, sizeof(struct platform_data));
823 if (!my_data) {
824 ALOGE("failed to allocate platform data");
825 return NULL;
826 }
827
828 list_init(&operator_info_list);
829
830 while (snd_card_num < MAX_SND_CARD) {
831 adev->mixer = mixer_open(snd_card_num);
832
833 while (!adev->mixer && retry_num < RETRY_NUMBER) {
834 usleep(RETRY_US);
835 adev->mixer = mixer_open(snd_card_num);
836 retry_num++;
837 }
838
839 if (!adev->mixer) {
840 ALOGE("%s: Unable to open the mixer card: %d", __func__,
841 snd_card_num);
842 retry_num = 0;
843 snd_card_num++;
844 continue;
845 }
846
847 snd_card_name = mixer_get_name(adev->mixer);
848 ALOGV("%s: snd_card_name: %s", __func__, snd_card_name);
849
850 my_data->hw_info = hw_info_init(snd_card_name);
851 if (!my_data->hw_info) {
852 ALOGE("%s: Failed to init hardware info", __func__);
853 } else {
854 query_platform(snd_card_name, mixer_xml_path);
855 ALOGD("%s: mixer path file is %s", __func__,
856 mixer_xml_path);
857 adev->audio_route = audio_route_init(snd_card_num,
858 mixer_xml_path);
859 if (!adev->audio_route) {
860 ALOGE("%s: Failed to init audio route controls, aborting.",
861 __func__);
862 free(my_data);
863 return NULL;
864 }
865 adev->snd_card = snd_card_num;
866 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
867 break;
868 }
869 retry_num = 0;
870 snd_card_num++;
871 }
872
873 if (snd_card_num >= MAX_SND_CARD) {
874 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
875 free(my_data);
876 return NULL;
877 }
878
879 my_data->adev = adev;
880 my_data->fluence_in_spkr_mode = false;
881 my_data->fluence_in_voice_call = false;
882 my_data->fluence_in_voice_rec = false;
883 my_data->fluence_type = FLUENCE_NONE;
884 my_data->fluence_mode = FLUENCE_ENDFIRE;
885
886 property_get("ro.qc.sdk.audio.fluencetype", my_data->fluence_cap, "");
887 if (!strncmp("fluencepro", my_data->fluence_cap, sizeof("fluencepro"))) {
888 my_data->fluence_type = FLUENCE_QUAD_MIC | FLUENCE_DUAL_MIC;
889 } else if (!strncmp("fluence", my_data->fluence_cap, sizeof("fluence"))) {
890 my_data->fluence_type = FLUENCE_DUAL_MIC;
891 } else {
892 my_data->fluence_type = FLUENCE_NONE;
893 }
894
895 if (my_data->fluence_type != FLUENCE_NONE) {
896 property_get("persist.audio.fluence.voicecall",value,"");
897 if (!strncmp("true", value, sizeof("true"))) {
898 my_data->fluence_in_voice_call = true;
899 }
900
901 property_get("persist.audio.fluence.voicerec",value,"");
902 if (!strncmp("true", value, sizeof("true"))) {
903 my_data->fluence_in_voice_rec = true;
904 }
905
906 property_get("persist.audio.fluence.speaker",value,"");
907 if (!strncmp("true", value, sizeof("true"))) {
908 my_data->fluence_in_spkr_mode = true;
909 }
910
911 property_get("persist.audio.fluence.mode",value,"");
912 if (!strncmp("broadside", value, sizeof("broadside"))) {
913 my_data->fluence_mode = FLUENCE_BROADSIDE;
914 }
915 }
916
917 property_get("persist.audio.FFSP.enable", ffspEnable, "");
918 if (!strncmp("true", ffspEnable, sizeof("true"))) {
919 acdb_device_table[SND_DEVICE_OUT_SPEAKER] = 131;
920 acdb_device_table[SND_DEVICE_OUT_SPEAKER_REVERSE] = 131;
921 acdb_device_table[SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 131;
922 acdb_device_table[SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] = 131;
923 }
924
925 my_data->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
926 if (my_data->acdb_handle == NULL) {
927 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
928 } else {
929 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
930 my_data->acdb_deallocate = (acdb_deallocate_t)dlsym(my_data->acdb_handle,
931 "acdb_loader_deallocate_ACDB");
932 if (!my_data->acdb_deallocate)
933 ALOGE("%s: Could not find the symbol acdb_loader_deallocate_ACDB from %s",
934 __func__, LIB_ACDB_LOADER);
935
936 my_data->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(my_data->acdb_handle,
937 "acdb_loader_send_audio_cal");
938 if (!my_data->acdb_send_audio_cal)
939 ALOGE("%s: Could not find the symbol acdb_send_audio_cal from %s",
940 __func__, LIB_ACDB_LOADER);
941
942 my_data->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(my_data->acdb_handle,
943 "acdb_loader_send_voice_cal");
944 if (!my_data->acdb_send_voice_cal)
945 ALOGE("%s: Could not find the symbol acdb_loader_send_voice_cal from %s",
946 __func__, LIB_ACDB_LOADER);
947
948 my_data->acdb_reload_vocvoltable = (acdb_reload_vocvoltable_t)dlsym(my_data->acdb_handle,
949 "acdb_loader_reload_vocvoltable");
950 if (!my_data->acdb_reload_vocvoltable)
951 ALOGE("%s: Could not find the symbol acdb_loader_reload_vocvoltable from %s",
952 __func__, LIB_ACDB_LOADER);
953
954 my_data->acdb_init = (acdb_init_v2_cvd_t)dlsym(my_data->acdb_handle,
955 "acdb_loader_init_v2");
956 if (my_data->acdb_init == NULL) {
957 ALOGE("%s: dlsym error %s for acdb_loader_init_v2", __func__, dlerror());
958 goto acdb_init_fail;
959 }
960 platform_acdb_init(my_data);
961 }
962
963acdb_init_fail:
964
965 set_platform_defaults();
966
967 /* Initialize ACDB and PCM ID's */
968 platform_info_init(PLATFORM_INFO_XML_PATH, my_data);
969
970
971 /* Read one time ssr property */
972 audio_extn_spkr_prot_init(adev);
973
974 return my_data;
975}
976
977void platform_deinit(void *platform)
978{
979 struct platform_data *my_data = (struct platform_data *)platform;
980
981 int32_t dev;
982 struct operator_info *info_item;
983 struct operator_specific_device *device_item;
984 struct listnode *node;
985
986 hw_info_deinit(my_data->hw_info);
987
988 for (dev = 0; dev < SND_DEVICE_MAX; dev++) {
989 if (backend_table[dev]) {
990 free(backend_table[dev]);
991 backend_table[dev]= NULL;
992 }
993 if (operator_specific_device_table[dev]) {
994 while (!list_empty(operator_specific_device_table[dev])) {
995 node = list_head(operator_specific_device_table[dev]);
996 list_remove(node);
997 device_item = node_to_item(node, struct operator_specific_device, list);
998 free(device_item->operator);
999 free(device_item->mixer_path);
1000 free(device_item);
1001 }
1002 free(operator_specific_device_table[dev]);
1003 }
1004 }
1005
1006 while (!list_empty(&operator_info_list)) {
1007 node = list_head(&operator_info_list);
1008 list_remove(node);
1009 info_item = node_to_item(node, struct operator_info, list);
1010 free(info_item->name);
1011 free(info_item->mccmnc);
1012 free(info_item);
1013 }
1014
1015 free(platform);
1016}
1017
1018const char *platform_get_snd_device_name(snd_device_t snd_device)
1019{
1020 if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX) {
1021 if (operator_specific_device_table[snd_device] != NULL) {
1022 return get_operator_specific_device_mixer_path(snd_device);
1023 }
1024 return device_table[snd_device];
1025 } else
1026 return "none";
1027}
1028
1029int platform_get_snd_device_name_extn(void *platform, snd_device_t snd_device,
1030 char *device_name)
1031{
1032 struct platform_data *my_data = (struct platform_data *)platform;
1033
1034 if (platform == NULL || device_name == NULL) {
1035 ALOGW("%s: something wrong, use legacy get_snd_device name", __func__);
1036 strlcpy(device_name, platform_get_snd_device_name(snd_device),
1037 DEVICE_NAME_MAX_SIZE);
1038 } else if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX) {
1039 if (operator_specific_device_table[snd_device] != NULL) {
1040 strlcpy(device_name, get_operator_specific_device_mixer_path(snd_device),
1041 DEVICE_NAME_MAX_SIZE);
1042 } else {
1043 strlcpy(device_name, device_table[snd_device], DEVICE_NAME_MAX_SIZE);
1044 }
1045 hw_info_append_hw_type(my_data->hw_info, snd_device, device_name);
1046 } else {
1047 strlcpy(device_name, "none", DEVICE_NAME_MAX_SIZE);
1048 return -EINVAL;
1049 }
1050
1051 return 0;
1052}
1053
Yamit Mehta6b4b1272016-06-10 15:12:50 +05301054bool platform_check_and_set_capture_backend_cfg(struct audio_device* adev __unused,
1055 struct audio_usecase *usecase __unused,
1056 snd_device_t snd_device)
1057{
1058 return false;
1059}
1060
1061bool platform_add_gain_level_mapping(struct amp_db_and_gain_table *tbl_entry __unused)
1062{
1063 return false;
1064}
1065
1066int platform_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl __unused,
1067 int table_size __unused)
1068{
1069 return 0;
1070}
1071
Yamit Mehta1d2e4742016-05-18 15:25:12 +05301072void platform_add_backend_name(void *platform, char *mixer_path,
1073 snd_device_t snd_device)
1074{
1075
1076 struct platform_data *my_data = (struct platform_data *)platform;
1077
1078 if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
1079 ALOGE("%s: Invalid snd_device = %d", __func__, snd_device);
1080 return;
1081 }
1082
1083 const char * suffix = backend_table[snd_device];
1084
1085 if (suffix != NULL) {
1086 strlcat(mixer_path, " ", MIXER_PATH_MAX_LENGTH);
1087 strlcat(mixer_path, suffix, MIXER_PATH_MAX_LENGTH);
1088 }
1089}
1090
1091bool platform_check_backends_match(snd_device_t snd_device1, snd_device_t snd_device2)
1092{
1093 bool result = true;
1094
1095 ALOGV("%s: snd_device1 = %s, snd_device2 = %s", __func__,
1096 platform_get_snd_device_name(snd_device1),
1097 platform_get_snd_device_name(snd_device2));
1098
1099 if ((snd_device1 < SND_DEVICE_MIN) || (snd_device1 >= SND_DEVICE_MAX)) {
1100 ALOGE("%s: Invalid snd_device = %s", __func__,
1101 platform_get_snd_device_name(snd_device1));
1102 return false;
1103 }
1104 if ((snd_device2 < SND_DEVICE_MIN) || (snd_device2 >= SND_DEVICE_MAX)) {
1105 ALOGE("%s: Invalid snd_device = %s", __func__,
1106 platform_get_snd_device_name(snd_device2));
1107 return false;
1108 }
1109 const char * be_itf1 = hw_interface_table[snd_device1];
1110 const char * be_itf2 = hw_interface_table[snd_device2];
1111
1112 if (NULL != be_itf1 && NULL != be_itf2) {
1113 if ((NULL == strstr(be_itf2, be_itf1)) && (NULL == strstr(be_itf1, be_itf2)))
1114 result = false;
1115 }
1116
1117 ALOGV("%s: be_itf1 = %s, be_itf2 = %s, match %d", __func__, be_itf1, be_itf2, result);
1118 return result;
1119}
1120
1121int platform_get_pcm_device_id(audio_usecase_t usecase, int device_type)
1122{
1123 int device_id = -1;
1124
1125 if (device_type == PCM_PLAYBACK)
1126 device_id = pcm_device_table[usecase][0];
1127 else
1128 device_id = pcm_device_table[usecase][1];
1129 return device_id;
1130}
1131
1132static int find_index(struct name_to_index * table, int32_t len, const char * name)
1133{
1134 int ret = 0;
1135 int32_t i;
1136
1137 if (table == NULL) {
1138 ALOGE("%s: table is NULL", __func__);
1139 ret = -ENODEV;
1140 goto done;
1141 }
1142
1143 if (name == NULL) {
1144 ALOGE("null key");
1145 ret = -ENODEV;
1146 goto done;
1147 }
1148
1149 for (i=0; i < len; i++) {
1150 const char* tn = table[i].name;
1151 size_t len = strlen(tn);
1152 if (strncmp(tn, name, len) == 0) {
1153 if (strlen(name) != len) {
1154 continue; // substring
1155 }
1156 ret = table[i].index;
1157 goto done;
1158 }
1159 }
1160 ALOGE("%s: Could not find index for name = %s",
1161 __func__, name);
1162 ret = -ENODEV;
1163done:
1164 return ret;
1165}
1166
1167int platform_get_snd_device_index(char *device_name)
1168{
1169 return find_index(snd_device_name_index, SND_DEVICE_MAX, device_name);
1170}
1171
1172int platform_get_usecase_index(const char *usecase_name)
1173{
1174 return find_index(usecase_name_index, AUDIO_USECASE_MAX, usecase_name);
1175}
1176
1177void platform_add_operator_specific_device(snd_device_t snd_device,
1178 const char *operator,
1179 const char *mixer_path,
1180 unsigned int acdb_id)
1181{
1182 struct operator_specific_device *device;
1183
1184 if (operator_specific_device_table[snd_device] == NULL) {
1185 operator_specific_device_table[snd_device] =
1186 (struct listnode *)calloc(1, sizeof(struct listnode));
1187 list_init(operator_specific_device_table[snd_device]);
1188 }
1189
1190 device = (struct operator_specific_device *)calloc(1, sizeof(struct operator_specific_device));
1191
1192 device->operator = strdup(operator);
1193 device->mixer_path = strdup(mixer_path);
1194 device->acdb_id = acdb_id;
1195
1196 list_add_tail(operator_specific_device_table[snd_device], &device->list);
1197
1198 ALOGD("%s: device[%s] -> operator[%s] mixer_path[%s] acdb_id[%d]", __func__,
1199 platform_get_snd_device_name(snd_device), operator, mixer_path, acdb_id);
1200
1201}
1202
1203int platform_set_snd_device_acdb_id(snd_device_t snd_device, unsigned int acdb_id)
1204{
1205 int ret = 0;
1206
1207 if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
1208 ALOGE("%s: Invalid snd_device = %d",
1209 __func__, snd_device);
1210 ret = -EINVAL;
1211 goto done;
1212 }
1213 ALOGV("%s: acdb_device_table[%s]: old = %d new = %d", __func__,
1214 platform_get_snd_device_name(snd_device), acdb_device_table[snd_device], acdb_id);
1215 acdb_device_table[snd_device] = acdb_id;
1216done:
1217 return ret;
1218}
1219
1220int platform_get_snd_device_acdb_id(snd_device_t snd_device)
1221{
1222 if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
1223 ALOGE("%s: Invalid snd_device = %d", __func__, snd_device);
1224 return -EINVAL;
1225 }
1226
1227 if (operator_specific_device_table[snd_device] != NULL)
1228 return get_operator_specific_device_acdb_id(snd_device);
1229 else
1230 return acdb_device_table[snd_device];
1231}
1232
1233int platform_send_audio_calibration(void *platform, snd_device_t snd_device)
1234{
1235 struct platform_data *my_data = (struct platform_data *)platform;
1236 int acdb_dev_id, acdb_dev_type;
1237
1238 acdb_dev_id = acdb_device_table[audio_extn_get_spkr_prot_snd_device(snd_device)];
1239 if (acdb_dev_id < 0) {
1240 ALOGE("%s: Could not find acdb id for device(%d)",
1241 __func__, snd_device);
1242 return -EINVAL;
1243 }
1244 if (my_data->acdb_send_audio_cal) {
1245 ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
1246 __func__, snd_device, acdb_dev_id);
1247 if (snd_device >= SND_DEVICE_OUT_BEGIN &&
1248 snd_device < SND_DEVICE_OUT_END)
1249 acdb_dev_type = ACDB_DEV_TYPE_OUT;
1250 else
1251 acdb_dev_type = ACDB_DEV_TYPE_IN;
1252 my_data->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
1253 }
1254 return 0;
1255}
1256
1257int platform_switch_voice_call_device_pre(void *platform __unused)
1258{
1259 return 0;
1260}
1261
1262int platform_switch_voice_call_enable_device_config(void *platform __unused,
1263 snd_device_t out_snd_device __unused,
1264 snd_device_t in_snd_device __unused)
1265{
1266 return 0;
1267}
1268
1269int platform_switch_voice_call_device_post(void *platform,
1270 snd_device_t out_snd_device,
1271 snd_device_t in_snd_device)
1272{
1273 struct platform_data *my_data = (struct platform_data *)platform;
1274 int acdb_rx_id, acdb_tx_id;
1275
1276 if (my_data->acdb_send_voice_cal == NULL) {
1277 ALOGE("%s: dlsym error for acdb_send_voice_call", __func__);
1278 } else {
1279 if (out_snd_device == SND_DEVICE_OUT_VOICE_SPEAKER &&
1280 audio_extn_spkr_prot_is_enabled())
1281 out_snd_device = SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED;
1282
1283 acdb_rx_id = platform_get_snd_device_acdb_id(out_snd_device);
1284 acdb_tx_id = platform_get_snd_device_acdb_id(in_snd_device);
1285
1286 if (acdb_rx_id > 0 && acdb_tx_id > 0)
1287 my_data->acdb_send_voice_cal(acdb_rx_id, acdb_tx_id);
1288 else
1289 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
1290 acdb_rx_id, acdb_tx_id);
1291 }
1292
1293 return 0;
1294}
1295
1296int platform_switch_voice_call_usecase_route_post(void *platform __unused,
1297 snd_device_t out_snd_device __unused,
1298 snd_device_t in_snd_device __unused)
1299{
1300 return 0;
1301}
1302
1303int platform_start_voice_call(void *platform __unused, uint32_t vsid __unused)
1304{
1305 return 0;
1306}
1307
1308int platform_stop_voice_call(void *platform __unused, uint32_t vsid __unused)
1309{
1310 return 0;
1311}
1312
1313int platform_get_sample_rate(void *platform __unused, uint32_t *rate __unused)
1314{
1315 return 0;
1316}
1317
1318void platform_set_speaker_gain_in_combo(struct audio_device *adev __unused,
1319 snd_device_t snd_device __unused,
1320 bool enable __unused)
1321{
1322 return;
1323}
1324
1325int platform_set_voice_volume(void *platform, int volume)
1326{
1327 struct platform_data *my_data = (struct platform_data *)platform;
1328 struct audio_device *adev = my_data->adev;
1329 struct mixer_ctl *ctl;
1330 const char *mixer_ctl_name = "Voice Rx Gain";
1331 int vol_index = 0, ret = 0;
1332 uint32_t set_values[ ] = {0,
1333 ALL_SESSION_VSID,
1334 DEFAULT_VOLUME_RAMP_DURATION_MS};
1335
1336 // Voice volume levels are mapped to adsp volume levels as follows.
1337 // 100 -> 5, 80 -> 4, 60 -> 3, 40 -> 2, 20 -> 1 0 -> 0
1338 // But this values don't changed in kernel. So, below change is need.
1339 vol_index = (int)percent_to_index(volume, MIN_VOL_INDEX, MAX_VOL_INDEX);
1340 set_values[0] = vol_index;
1341
1342 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1343 if (!ctl) {
1344 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1345 __func__, mixer_ctl_name);
1346 return -EINVAL;
1347 }
1348 ALOGV("Setting voice volume index: %d", set_values[0]);
1349 ret = mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
1350
1351 return ret;
1352}
1353
1354int platform_set_mic_mute(void *platform, bool state)
1355{
1356 struct platform_data *my_data = (struct platform_data *)platform;
1357 struct audio_device *adev = my_data->adev;
1358 struct mixer_ctl *ctl;
1359 const char *mixer_ctl_name = "Voice Tx Mute";
1360 int ret = 0;
1361 uint32_t set_values[ ] = {0,
1362 ALL_SESSION_VSID,
1363 DEFAULT_MUTE_RAMP_DURATION_MS};
1364
Devin Kim152b09a2016-06-23 13:53:04 -07001365 if (audio_extn_hfp_is_active(adev))
1366 mixer_ctl_name = "HFP Tx Mute";
1367
Yamit Mehta1d2e4742016-05-18 15:25:12 +05301368 set_values[0] = state;
1369 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1370 if (!ctl) {
1371 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1372 __func__, mixer_ctl_name);
1373 return -EINVAL;
1374 }
1375 ALOGV("Setting voice mute state: %d", state);
1376 ret = mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
1377 return ret;
1378}
1379
1380int platform_set_device_mute(void *platform, bool state, char *dir)
1381{
1382 struct platform_data *my_data = (struct platform_data *)platform;
1383 struct audio_device *adev = my_data->adev;
1384 struct mixer_ctl *ctl;
1385 char *mixer_ctl_name = NULL;
1386 int ret = 0;
1387 uint32_t set_values[ ] = {0,
1388 ALL_SESSION_VSID,
1389 0};
1390 if(dir == NULL) {
1391 ALOGE("%s: Invalid direction:%s", __func__, dir);
1392 return -EINVAL;
1393 }
1394
1395 if (!strncmp("rx", dir, sizeof("rx"))) {
1396 mixer_ctl_name = "Voice Rx Device Mute";
1397 } else if (!strncmp("tx", dir, sizeof("tx"))) {
1398 mixer_ctl_name = "Voice Tx Device Mute";
1399 } else {
1400 return -EINVAL;
1401 }
1402
1403 set_values[0] = state;
1404 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1405 if (!ctl) {
1406 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1407 __func__, mixer_ctl_name);
1408 return -EINVAL;
1409 }
1410
1411 ALOGV("%s: Setting device mute state: %d, mixer ctrl:%s",
1412 __func__,state, mixer_ctl_name);
1413 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
1414
1415 return ret;
1416}
1417
1418bool platform_can_split_snd_device(snd_device_t snd_device,
1419 int *num_devices,
1420 snd_device_t *new_snd_devices)
1421{
1422 bool status = false;
1423
1424 if (NULL == num_devices || NULL == new_snd_devices) {
1425 ALOGE("%s: NULL pointer ..", __func__);
1426 return false;
1427 }
1428
1429 /*
1430 * If wired headset/headphones/line devices share the same backend
1431 * with speaker/earpiece this routine returns false.
1432 */
1433 if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES &&
1434 !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER, SND_DEVICE_OUT_HEADPHONES)) {
1435 *num_devices = 2;
1436 new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
1437 new_snd_devices[1] = SND_DEVICE_OUT_HEADPHONES;
1438 status = true;
1439 } else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_LINE &&
1440 !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER, SND_DEVICE_OUT_LINE)) {
1441 *num_devices = 2;
1442 new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
1443 new_snd_devices[1] = SND_DEVICE_OUT_LINE;
1444 status = true;
1445 }
1446 return status;
1447}
1448
1449snd_device_t platform_get_output_snd_device(void *platform, audio_devices_t devices)
1450{
1451 struct platform_data *my_data = (struct platform_data *)platform;
1452 struct audio_device *adev = my_data->adev;
1453 audio_mode_t mode = adev->mode;
1454 snd_device_t snd_device = SND_DEVICE_NONE;
1455
1456 audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
1457 AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
1458 int channel_count = popcount(channel_mask);
1459
1460 ALOGV("%s: enter: output devices(%#x)", __func__, devices);
1461 if (devices == AUDIO_DEVICE_NONE ||
1462 devices & AUDIO_DEVICE_BIT_IN) {
1463 ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
1464 goto exit;
1465 }
1466
1467 if (popcount(devices) == 2) {
1468 if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
1469 AUDIO_DEVICE_OUT_SPEAKER)) {
1470 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
1471 } else if (devices == (AUDIO_DEVICE_OUT_LINE |
1472 AUDIO_DEVICE_OUT_SPEAKER)) {
1473 snd_device = SND_DEVICE_OUT_SPEAKER_AND_LINE;
1474 } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
1475 AUDIO_DEVICE_OUT_SPEAKER)) {
1476 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
1477 } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
1478 AUDIO_DEVICE_OUT_SPEAKER)) {
1479 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
1480 } else if (devices == (AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET |
1481 AUDIO_DEVICE_OUT_SPEAKER)) {
1482 snd_device = SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET;
1483 } else {
1484 ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
1485 goto exit;
1486 }
1487 if (snd_device != SND_DEVICE_NONE) {
1488 goto exit;
1489 }
1490 }
1491
1492 if (popcount(devices) != 1) {
1493 ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
1494 goto exit;
1495 }
1496
1497 if (mode == AUDIO_MODE_IN_CALL) {
1498 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
1499 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET ||
1500 devices & AUDIO_DEVICE_OUT_LINE) {
1501 if (adev->voice.tty_mode != TTY_MODE_OFF) {
1502 switch (adev->voice.tty_mode) {
1503 case TTY_MODE_FULL:
1504 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
1505 break;
1506 case TTY_MODE_VCO:
1507 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
1508 break;
1509 case TTY_MODE_HCO:
1510 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
1511 break;
1512 default:
1513 ALOGE("%s: Invalid TTY mode (%#x)",
1514 __func__, adev->voice.tty_mode);
1515 }
1516 } else if (devices & AUDIO_DEVICE_OUT_LINE) {
1517 snd_device = SND_DEVICE_OUT_VOICE_LINE;
1518 } else {
1519 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
1520 }
1521 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
1522 if (adev->bt_wb_speech_enabled)
1523 snd_device = SND_DEVICE_OUT_BT_SCO_WB;
1524 else
1525 snd_device = SND_DEVICE_OUT_BT_SCO;
1526 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
Devin Kim152b09a2016-06-23 13:53:04 -07001527 if (audio_extn_hfp_is_active(adev))
1528 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER_HFP;
1529 else
1530 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
Yamit Mehta1d2e4742016-05-18 15:25:12 +05301531 } else if (devices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET ||
1532 devices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) {
1533 snd_device = SND_DEVICE_OUT_USB_HEADSET;
1534 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
1535 snd_device = SND_DEVICE_OUT_VOICE_HANDSET;
1536 } else if (devices & AUDIO_DEVICE_OUT_TELEPHONY_TX)
1537 snd_device = SND_DEVICE_OUT_VOICE_TX;
1538
1539 if (snd_device != SND_DEVICE_NONE) {
1540 goto exit;
1541 }
1542 }
1543
1544 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
1545 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
1546 snd_device = SND_DEVICE_OUT_HEADPHONES;
1547 } else if (devices & AUDIO_DEVICE_OUT_LINE) {
1548 snd_device = SND_DEVICE_OUT_LINE;
1549 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
1550 if (my_data->speaker_lr_swap)
1551 snd_device = SND_DEVICE_OUT_SPEAKER_REVERSE;
1552 else
1553 snd_device = SND_DEVICE_OUT_SPEAKER;
1554 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
1555 if (adev->bt_wb_speech_enabled)
1556 snd_device = SND_DEVICE_OUT_BT_SCO_WB;
1557 else
1558 snd_device = SND_DEVICE_OUT_BT_SCO;
1559 } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1560 snd_device = SND_DEVICE_OUT_HDMI ;
1561 } else if (devices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET ||
1562 devices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) {
1563 snd_device = SND_DEVICE_OUT_USB_HEADSET;
1564 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
1565 snd_device = SND_DEVICE_OUT_HANDSET;
Yamit Mehta1d2e4742016-05-18 15:25:12 +05301566 } else {
1567 ALOGE("%s: Unknown device(s) %#x", __func__, devices);
1568 }
1569exit:
1570 ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
1571 return snd_device;
1572}
1573
1574snd_device_t platform_get_input_snd_device(void *platform, audio_devices_t out_device)
1575{
1576 struct platform_data *my_data = (struct platform_data *)platform;
1577 struct audio_device *adev = my_data->adev;
1578 audio_source_t source = (adev->active_input == NULL) ?
1579 AUDIO_SOURCE_DEFAULT : adev->active_input->source;
1580
1581 audio_mode_t mode = adev->mode;
1582 audio_devices_t in_device = ((adev->active_input == NULL) ?
1583 AUDIO_DEVICE_NONE : adev->active_input->device)
1584 & ~AUDIO_DEVICE_BIT_IN;
1585 audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
1586 AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
1587 snd_device_t snd_device = SND_DEVICE_NONE;
1588 int channel_count = popcount(channel_mask);
1589
1590 ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
1591 __func__, out_device, in_device);
1592
1593 if ((out_device != AUDIO_DEVICE_NONE) && ((mode == AUDIO_MODE_IN_CALL) ||
1594 audio_extn_hfp_is_active(adev))) {
1595 if (adev->voice.tty_mode != TTY_MODE_OFF) {
1596 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
1597 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET ||
1598 out_device & AUDIO_DEVICE_OUT_LINE) {
1599 switch (adev->voice.tty_mode) {
1600 case TTY_MODE_FULL:
1601 snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
1602 break;
1603 case TTY_MODE_VCO:
1604 snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
1605 break;
1606 case TTY_MODE_HCO:
1607 snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
1608 break;
1609 default:
1610 ALOGE("%s: Invalid TTY mode (%#x)",
1611 __func__, adev->voice.tty_mode);
1612 }
1613 goto exit;
1614 }
1615 }
1616 if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
1617 out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
1618 out_device & AUDIO_DEVICE_OUT_LINE) {
1619 if (my_data->fluence_type == FLUENCE_NONE ||
1620 my_data->fluence_in_voice_call == false) {
1621 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1622 if (audio_extn_hfp_is_active(adev))
1623 platform_set_echo_reference(adev, true, out_device);
1624 } else {
1625 snd_device = SND_DEVICE_IN_VOICE_DMIC;
1626 adev->acdb_settings |= DMIC_FLAG;
1627 }
1628 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
1629 snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
1630 if (audio_extn_hfp_is_active(adev))
1631 platform_set_echo_reference(adev, true, out_device);
1632 } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
1633 if (adev->bt_wb_speech_enabled) {
1634 if (adev->bluetooth_nrec)
1635 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
1636 else
1637 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
1638 } else {
1639 if (adev->bluetooth_nrec)
1640 snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
1641 else
1642 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
1643 }
1644 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
1645 if (my_data->fluence_type != FLUENCE_NONE &&
1646 my_data->fluence_in_voice_call &&
1647 my_data->fluence_in_spkr_mode) {
1648 if(my_data->fluence_type & FLUENCE_QUAD_MIC) {
1649 adev->acdb_settings |= QMIC_FLAG;
1650 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_QMIC;
1651 } else {
1652 adev->acdb_settings |= DMIC_FLAG;
1653 if (my_data->fluence_mode == FLUENCE_BROADSIDE)
1654 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BROADSIDE;
1655 else
1656 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC;
1657 }
1658 } else {
1659 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
Devin Kim152b09a2016-06-23 13:53:04 -07001660 if (audio_extn_hfp_is_active(adev)) {
1661 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP;
Yamit Mehta1d2e4742016-05-18 15:25:12 +05301662 platform_set_echo_reference(adev, true, out_device);
Devin Kim152b09a2016-06-23 13:53:04 -07001663 } else {
1664 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
1665 }
Yamit Mehta1d2e4742016-05-18 15:25:12 +05301666 }
1667 } else if (out_device & AUDIO_DEVICE_OUT_TELEPHONY_TX)
1668 snd_device = SND_DEVICE_IN_VOICE_RX;
1669 } else if (source == AUDIO_SOURCE_CAMCORDER) {
1670 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
1671 in_device & AUDIO_DEVICE_IN_BACK_MIC) {
1672 if (my_data->fluence_type & FLUENCE_DUAL_MIC &&
1673 channel_count == 2)
1674 snd_device = SND_DEVICE_IN_HANDSET_STEREO_DMIC;
1675 else
1676 snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
1677 }
1678 } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
1679 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
1680 if (channel_count == 2) {
1681 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_STEREO;
1682 adev->acdb_settings |= DMIC_FLAG;
1683 } else if (adev->active_input->enable_ns)
1684 snd_device = SND_DEVICE_IN_VOICE_REC_MIC_NS;
1685 else if (my_data->fluence_type != FLUENCE_NONE &&
1686 my_data->fluence_in_voice_rec) {
1687 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE;
1688 adev->acdb_settings |= DMIC_FLAG;
1689 } else {
1690 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
1691 }
1692 }
1693 } else if ((source == AUDIO_SOURCE_VOICE_COMMUNICATION) ||
1694 (mode == AUDIO_MODE_IN_COMMUNICATION)) {
1695 if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
1696 in_device = AUDIO_DEVICE_IN_BACK_MIC;
1697 if (adev->active_input) {
1698 if (my_data->fluence_type != FLUENCE_NONE &&
1699 adev->active_input->enable_aec &&
1700 adev->active_input->enable_ns) {
1701 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
1702 if (my_data->fluence_in_spkr_mode) {
1703 if (my_data->fluence_type & FLUENCE_QUAD_MIC) {
1704 snd_device = SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS;
1705 } else if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
1706 if (my_data->fluence_mode == FLUENCE_BROADSIDE)
1707 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS_BROADSIDE;
1708 else
1709 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS;
1710 }
1711 adev->acdb_settings |= DMIC_FLAG;
1712 } else
1713 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC_NS;
1714 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
1715 if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
1716 snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC_NS;
1717 adev->acdb_settings |= DMIC_FLAG;
1718 } else
1719 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC_NS;
1720 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
1721 snd_device = SND_DEVICE_IN_HEADSET_MIC_FLUENCE;
1722 }
1723 platform_set_echo_reference(adev, true, out_device);
1724 } else if (my_data->fluence_type != FLUENCE_NONE &&
1725 adev->active_input->enable_aec) {
1726 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
1727 if (my_data->fluence_in_spkr_mode) {
1728 if (my_data->fluence_type & FLUENCE_QUAD_MIC) {
1729 snd_device = SND_DEVICE_IN_SPEAKER_QMIC_AEC;
1730 } else if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
1731 if (my_data->fluence_mode == FLUENCE_BROADSIDE)
1732 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC_BROADSIDE;
1733 else
1734 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC;
1735 }
1736 adev->acdb_settings |= DMIC_FLAG;
1737 } else
1738 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
1739 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
1740 if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
1741 snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC;
1742 adev->acdb_settings |= DMIC_FLAG;
1743 } else
1744 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
1745 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
1746 snd_device = SND_DEVICE_IN_HEADSET_MIC_FLUENCE;
1747 }
1748 platform_set_echo_reference(adev, true, out_device);
1749 } else if (my_data->fluence_type != FLUENCE_NONE &&
1750 adev->active_input->enable_ns) {
1751 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
1752 if (my_data->fluence_in_spkr_mode) {
1753 if (my_data->fluence_type & FLUENCE_QUAD_MIC) {
1754 snd_device = SND_DEVICE_IN_SPEAKER_QMIC_NS;
1755 } else if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
1756 if (my_data->fluence_mode == FLUENCE_BROADSIDE)
1757 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_NS_BROADSIDE;
1758 else
1759 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_NS;
1760 }
1761 adev->acdb_settings |= DMIC_FLAG;
1762 } else
1763 snd_device = SND_DEVICE_IN_SPEAKER_MIC_NS;
1764 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
1765 if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
1766 snd_device = SND_DEVICE_IN_HANDSET_DMIC_NS;
1767 adev->acdb_settings |= DMIC_FLAG;
1768 } else
1769 snd_device = SND_DEVICE_IN_HANDSET_MIC_NS;
1770 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
1771 snd_device = SND_DEVICE_IN_HEADSET_MIC_FLUENCE;
1772 }
1773 platform_set_echo_reference(adev, false, out_device);
1774 } else
1775 platform_set_echo_reference(adev, false, out_device);
1776 }
1777 } else if (source == AUDIO_SOURCE_FM_TUNER) {
1778 snd_device = SND_DEVICE_IN_CAPTURE_FM;
1779 } else if (source == AUDIO_SOURCE_DEFAULT) {
1780 goto exit;
1781 }
1782
1783
1784 if (snd_device != SND_DEVICE_NONE) {
1785 goto exit;
1786 }
1787
1788 if (in_device != AUDIO_DEVICE_NONE &&
1789 !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
1790 !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
1791 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
1792 if (my_data->fluence_type & (FLUENCE_DUAL_MIC | FLUENCE_QUAD_MIC) &&
1793 channel_count == 2)
1794 snd_device = SND_DEVICE_IN_HANDSET_STEREO_DMIC;
1795 else
1796 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1797 } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
1798 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
1799 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
1800 snd_device = SND_DEVICE_IN_HEADSET_MIC;
1801 } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
1802 if (adev->bt_wb_speech_enabled) {
1803 if (adev->bluetooth_nrec)
1804 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
1805 else
1806 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
1807 } else {
1808 if (adev->bluetooth_nrec)
1809 snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
1810 else
1811 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
1812 }
1813 } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
1814 snd_device = SND_DEVICE_IN_HDMI_MIC;
1815 } else if (in_device & AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET ||
1816 in_device & AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET) {
1817 snd_device = SND_DEVICE_IN_USB_HEADSET_MIC;
1818 } else if (in_device & AUDIO_DEVICE_IN_FM_TUNER) {
1819 snd_device = SND_DEVICE_IN_CAPTURE_FM;
1820 } else {
1821 ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
1822 ALOGW("%s: Using default handset-mic", __func__);
1823 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1824 }
1825 } else {
1826 if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
1827 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1828 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
1829 snd_device = SND_DEVICE_IN_HEADSET_MIC;
1830 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
1831 if (channel_count > 1)
1832 snd_device = SND_DEVICE_IN_SPEAKER_STEREO_DMIC;
1833 else
1834 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
1835 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
1836 out_device & AUDIO_DEVICE_OUT_LINE) {
1837 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1838 } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
1839 if (adev->bt_wb_speech_enabled) {
1840 if (adev->bluetooth_nrec)
1841 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
1842 else
1843 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
1844 } else {
1845 if (adev->bluetooth_nrec)
1846 snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
1847 else
1848 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
1849 }
1850 } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1851 snd_device = SND_DEVICE_IN_HDMI_MIC;
1852 } else if (out_device & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET ||
1853 out_device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) {
1854 snd_device = SND_DEVICE_IN_USB_HEADSET_MIC;
1855 } else {
1856 ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
1857 ALOGW("%s: Using default handset-mic", __func__);
1858 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1859 }
1860 }
1861exit:
1862 ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
1863 return snd_device;
1864}
1865
1866int platform_set_hdmi_channels(void *platform, int channel_count)
1867{
1868 struct platform_data *my_data = (struct platform_data *)platform;
1869 struct audio_device *adev = my_data->adev;
1870 struct mixer_ctl *ctl;
1871 const char *channel_cnt_str = NULL;
1872 const char *mixer_ctl_name = "HDMI_RX Channels";
1873 switch (channel_count) {
1874 case 8:
1875 channel_cnt_str = "Eight"; break;
1876 case 7:
1877 channel_cnt_str = "Seven"; break;
1878 case 6:
1879 channel_cnt_str = "Six"; break;
1880 case 5:
1881 channel_cnt_str = "Five"; break;
1882 case 4:
1883 channel_cnt_str = "Four"; break;
1884 case 3:
1885 channel_cnt_str = "Three"; break;
1886 default:
1887 channel_cnt_str = "Two"; break;
1888 }
1889 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1890 if (!ctl) {
1891 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1892 __func__, mixer_ctl_name);
1893 return -EINVAL;
1894 }
1895 ALOGV("HDMI channel count: %s", channel_cnt_str);
1896 mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
1897 return 0;
1898}
1899
1900int platform_edid_get_max_channels(void *platform)
1901{
1902 struct platform_data *my_data = (struct platform_data *)platform;
1903 struct audio_device *adev = my_data->adev;
1904 char block[MAX_SAD_BLOCKS * SAD_BLOCK_SIZE];
1905 char *sad = block;
1906 int num_audio_blocks;
1907 int channel_count;
1908 int max_channels = 0;
1909 int i, ret, count;
1910
1911 struct mixer_ctl *ctl;
1912
1913 ctl = mixer_get_ctl_by_name(adev->mixer, AUDIO_DATA_BLOCK_MIXER_CTL);
1914 if (!ctl) {
1915 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1916 __func__, AUDIO_DATA_BLOCK_MIXER_CTL);
1917 return 0;
1918 }
1919
1920 mixer_ctl_update(ctl);
1921
1922 count = mixer_ctl_get_num_values(ctl);
1923
1924 /* Read SAD blocks, clamping the maximum size for safety */
1925 if (count > (int)sizeof(block))
1926 count = (int)sizeof(block);
1927
1928 ret = mixer_ctl_get_array(ctl, block, count);
1929 if (ret != 0) {
1930 ALOGE("%s: mixer_ctl_get_array() failed to get EDID info", __func__);
1931 return 0;
1932 }
1933
1934 /* Calculate the number of SAD blocks */
1935 num_audio_blocks = count / SAD_BLOCK_SIZE;
1936
1937 for (i = 0; i < num_audio_blocks; i++) {
1938 /* Only consider LPCM blocks */
1939 if ((sad[0] >> 3) != EDID_FORMAT_LPCM) {
1940 sad += 3;
1941 continue;
1942 }
1943
1944 channel_count = (sad[0] & 0x7) + 1;
1945 if (channel_count > max_channels)
1946 max_channels = channel_count;
1947
1948 /* Advance to next block */
1949 sad += 3;
1950 }
1951
1952 return max_channels;
1953}
1954
1955int platform_set_incall_recording_session_id(void *platform,
1956 uint32_t session_id,
1957 int rec_mode __unused)
1958{
1959 int ret = 0;
1960 struct platform_data *my_data = (struct platform_data *)platform;
1961 struct audio_device *adev = my_data->adev;
1962 struct mixer_ctl *ctl;
1963 const char *mixer_ctl_name = "Voc VSID";
1964 int num_ctl_values;
1965 int i;
1966
1967 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1968 if (!ctl) {
1969 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1970 __func__, mixer_ctl_name);
1971 ret = -EINVAL;
1972 } else {
1973 num_ctl_values = mixer_ctl_get_num_values(ctl);
1974 for (i = 0; i < num_ctl_values; i++) {
1975 if (mixer_ctl_set_value(ctl, i, session_id)) {
1976 ALOGV("Error: invalid session_id: %x", session_id);
1977 ret = -EINVAL;
1978 break;
1979 }
1980 }
1981 }
1982 return ret;
1983}
1984
1985int platform_stop_incall_recording_usecase(void *platform __unused)
1986{
1987 return 0;
1988}
1989
1990int platform_start_incall_music_usecase(void *platform __unused)
1991{
1992 return 0;
1993}
1994
1995int platform_stop_incall_music_usecase(void *platform __unused)
1996{
1997 return 0;
1998}
1999
2000int platform_set_parameters(void *platform, struct str_parms *parms)
2001{
2002 struct platform_data *my_data = (struct platform_data *)platform;
2003 char value[128];
2004 char *kv_pairs = str_parms_to_str(parms);
2005 int ret = 0, err;
2006
2007 if (kv_pairs == NULL) {
2008 ret = -EINVAL;
2009 ALOGE("%s: key-value pair is NULL",__func__);
2010 goto done;
2011 }
2012
2013 ALOGV("%s: enter: %s", __func__, kv_pairs);
2014
2015 err = str_parms_get_str(parms, PLATFORM_CONFIG_KEY_OPERATOR_INFO,
2016 value, sizeof(value));
2017 if (err >= 0) {
2018 struct operator_info *info;
2019 char *str = value;
2020 char *name;
2021
2022 str_parms_del(parms, PLATFORM_CONFIG_KEY_OPERATOR_INFO);
2023 info = (struct operator_info *)calloc(1, sizeof(struct operator_info));
2024 name = strtok(str, ";");
2025 info->name = strdup(name);
2026 info->mccmnc = strdup(str + strlen(name) + 1);
2027
2028 list_add_tail(&operator_info_list, &info->list);
2029 ALOGV("%s: add operator[%s] mccmnc[%s]", __func__, info->name, info->mccmnc);
2030 }
2031done:
2032 ALOGV("%s: exit with code(%d)", __func__, ret);
2033 if (kv_pairs != NULL)
2034 free(kv_pairs);
2035
2036 return ret;
2037}
2038
2039/* Delay in Us */
2040int64_t platform_render_latency(audio_usecase_t usecase)
2041{
2042 switch (usecase) {
2043 case USECASE_AUDIO_PLAYBACK_DEEP_BUFFER:
2044 return DEEP_BUFFER_PLATFORM_DELAY;
2045 case USECASE_AUDIO_PLAYBACK_LOW_LATENCY:
2046 return LOW_LATENCY_PLATFORM_DELAY;
2047 default:
2048 return 0;
2049 }
2050}
2051
2052int platform_set_snd_device_backend(snd_device_t device, const char *backend, const char * hw_interface)
2053{
2054 int ret = 0;
2055
2056 if ((device < SND_DEVICE_MIN) || (device >= SND_DEVICE_MAX)) {
2057 ALOGE("%s: Invalid snd_device = %d",
2058 __func__, device);
2059 ret = -EINVAL;
2060 goto done;
2061 }
2062
2063 ALOGV("%s: backend_tag_table[%s]: old = %s new = %s", __func__,
2064 platform_get_snd_device_name(device),
2065 backend_table[device] != NULL ? backend_table[device]: "null", backend);
2066 if (backend_table[device]) {
2067 free(backend_table[device]);
2068 }
2069 backend_table[device] = strdup(backend);
2070
2071 if (hw_interface != NULL) {
2072 if (hw_interface_table[device])
2073 free(hw_interface_table[device]);
2074 ALOGV("%s: hw_interface_table[%d] = %s", __func__, device, hw_interface);
2075 hw_interface_table[device] = strdup(hw_interface);
2076 }
2077done:
2078 return ret;
2079}
2080
2081int platform_set_usecase_pcm_id(audio_usecase_t usecase, int32_t type, int32_t pcm_id)
2082{
2083 int ret = 0;
2084 if ((usecase <= USECASE_INVALID) || (usecase >= AUDIO_USECASE_MAX)) {
2085 ALOGE("%s: invalid usecase case idx %d", __func__, usecase);
2086 ret = -EINVAL;
2087 goto done;
2088 }
2089
2090 if ((type != 0) && (type != 1)) {
2091 ALOGE("%s: invalid usecase type", __func__);
2092 ret = -EINVAL;
2093 }
2094 pcm_device_table[usecase][type] = pcm_id;
2095done:
2096 return ret;
2097}
2098
2099#define DEFAULT_NOMINAL_SPEAKER_GAIN 20
2100int ramp_speaker_gain(struct audio_device *adev, bool ramp_up, int target_ramp_up_gain) {
2101 // backup_gain: gain to try to set in case of an error during ramp
2102 int start_gain, end_gain, step, backup_gain, i;
2103 bool error = false;
2104 const struct mixer_ctl *ctl;
2105 const char *mixer_ctl_name_gain_left = "Left Speaker Gain";
2106 const char *mixer_ctl_name_gain_right = "Right Speaker Gain";
2107 struct mixer_ctl *ctl_left = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name_gain_left);
2108 struct mixer_ctl *ctl_right = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name_gain_right);
2109 if (!ctl_left || !ctl_right) {
2110 ALOGE("%s: Could not get ctl for mixer cmd - %s or %s, not applying speaker gain ramp",
2111 __func__, mixer_ctl_name_gain_left, mixer_ctl_name_gain_right);
2112 return -EINVAL;
2113 } else if ((mixer_ctl_get_num_values(ctl_left) != 1)
2114 || (mixer_ctl_get_num_values(ctl_right) != 1)) {
2115 ALOGE("%s: Unexpected num values for mixer cmd - %s or %s, not applying speaker gain ramp",
2116 __func__, mixer_ctl_name_gain_left, mixer_ctl_name_gain_right);
2117 return -EINVAL;
2118 }
2119 if (ramp_up) {
2120 start_gain = 0;
2121 end_gain = target_ramp_up_gain > 0 ? target_ramp_up_gain : DEFAULT_NOMINAL_SPEAKER_GAIN;
2122 step = +1;
2123 backup_gain = end_gain;
2124 } else {
2125 // using same gain on left and right
2126 const int left_gain = mixer_ctl_get_value(ctl_left, 0);
2127 start_gain = left_gain > 0 ? left_gain : DEFAULT_NOMINAL_SPEAKER_GAIN;
2128 end_gain = 0;
2129 step = -1;
2130 backup_gain = start_gain;
2131 }
2132 for (i = start_gain ; i != (end_gain + step) ; i += step) {
2133 //ALOGV("setting speaker gain to %d", i);
2134 if (mixer_ctl_set_value(ctl_left, 0, i)) {
2135 ALOGE("%s: error setting %s to %d during gain ramp",
2136 __func__, mixer_ctl_name_gain_left, i);
2137 error = true;
2138 break;
2139 }
2140 if (mixer_ctl_set_value(ctl_right, 0, i)) {
2141 ALOGE("%s: error setting %s to %d during gain ramp",
2142 __func__, mixer_ctl_name_gain_right, i);
2143 error = true;
2144 break;
2145 }
2146 usleep(1000);
2147 }
2148 if (error) {
2149 // an error occured during the ramp, let's still try to go back to a safe volume
2150 if (mixer_ctl_set_value(ctl_left, 0, backup_gain)) {
2151 ALOGE("%s: error restoring left gain to %d", __func__, backup_gain);
2152 }
2153 if (mixer_ctl_set_value(ctl_right, 0, backup_gain)) {
2154 ALOGE("%s: error restoring right gain to %d", __func__, backup_gain);
2155 }
2156 }
2157 return start_gain;
2158}
2159
2160int platform_swap_lr_channels(struct audio_device *adev, bool swap_channels)
2161{
2162 // only update if there is active pcm playback on speaker
2163 struct audio_usecase *usecase;
2164 struct listnode *node;
2165 struct platform_data *my_data = (struct platform_data *)adev->platform;
2166
2167 if (my_data->speaker_lr_swap != swap_channels) {
2168 my_data->speaker_lr_swap = swap_channels;
2169
2170 list_for_each(node, &adev->usecase_list) {
2171 usecase = node_to_item(node, struct audio_usecase, list);
2172 if (usecase->type == PCM_PLAYBACK &&
2173 usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
2174 /*
2175 * If acdb tuning is different for SPEAKER_REVERSE, it is must
2176 * to perform device switch to disable the current backend to
2177 * enable it with new acdb data.
2178 */
2179 if (acdb_device_table[SND_DEVICE_OUT_SPEAKER] !=
2180 acdb_device_table[SND_DEVICE_OUT_SPEAKER_REVERSE]) {
2181 const int initial_skpr_gain = ramp_speaker_gain(adev, false /*ramp_up*/, -1);
2182 select_devices(adev, usecase->id);
2183 if (initial_skpr_gain != -EINVAL) {
2184 ramp_speaker_gain(adev, true /*ramp_up*/, initial_skpr_gain);
2185 }
2186 } else {
2187 const char *mixer_path;
2188 if (swap_channels) {
2189 mixer_path = platform_get_snd_device_name(SND_DEVICE_OUT_SPEAKER_REVERSE);
2190 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
2191 } else {
2192 mixer_path = platform_get_snd_device_name(SND_DEVICE_OUT_SPEAKER);
2193 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
2194 }
2195 }
2196 break;
2197 }
2198 }
2199 }
2200 return 0;
2201}