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