blob: e8ae5b7e7ddbecf91991fa179dfbdae025cc8d98 [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
1048void platform_add_backend_name(void *platform, char *mixer_path,
1049 snd_device_t snd_device)
1050{
1051
1052 struct platform_data *my_data = (struct platform_data *)platform;
1053
1054 if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
1055 ALOGE("%s: Invalid snd_device = %d", __func__, snd_device);
1056 return;
1057 }
1058
1059 const char * suffix = backend_table[snd_device];
1060
1061 if (suffix != NULL) {
1062 strlcat(mixer_path, " ", MIXER_PATH_MAX_LENGTH);
1063 strlcat(mixer_path, suffix, MIXER_PATH_MAX_LENGTH);
1064 }
1065}
1066
1067bool platform_check_backends_match(snd_device_t snd_device1, snd_device_t snd_device2)
1068{
1069 bool result = true;
1070
1071 ALOGV("%s: snd_device1 = %s, snd_device2 = %s", __func__,
1072 platform_get_snd_device_name(snd_device1),
1073 platform_get_snd_device_name(snd_device2));
1074
1075 if ((snd_device1 < SND_DEVICE_MIN) || (snd_device1 >= SND_DEVICE_MAX)) {
1076 ALOGE("%s: Invalid snd_device = %s", __func__,
1077 platform_get_snd_device_name(snd_device1));
1078 return false;
1079 }
1080 if ((snd_device2 < SND_DEVICE_MIN) || (snd_device2 >= SND_DEVICE_MAX)) {
1081 ALOGE("%s: Invalid snd_device = %s", __func__,
1082 platform_get_snd_device_name(snd_device2));
1083 return false;
1084 }
1085 const char * be_itf1 = hw_interface_table[snd_device1];
1086 const char * be_itf2 = hw_interface_table[snd_device2];
1087
1088 if (NULL != be_itf1 && NULL != be_itf2) {
1089 if ((NULL == strstr(be_itf2, be_itf1)) && (NULL == strstr(be_itf1, be_itf2)))
1090 result = false;
1091 }
1092
1093 ALOGV("%s: be_itf1 = %s, be_itf2 = %s, match %d", __func__, be_itf1, be_itf2, result);
1094 return result;
1095}
1096
1097int platform_get_pcm_device_id(audio_usecase_t usecase, int device_type)
1098{
1099 int device_id = -1;
1100
1101 if (device_type == PCM_PLAYBACK)
1102 device_id = pcm_device_table[usecase][0];
1103 else
1104 device_id = pcm_device_table[usecase][1];
1105 return device_id;
1106}
1107
1108static int find_index(struct name_to_index * table, int32_t len, const char * name)
1109{
1110 int ret = 0;
1111 int32_t i;
1112
1113 if (table == NULL) {
1114 ALOGE("%s: table is NULL", __func__);
1115 ret = -ENODEV;
1116 goto done;
1117 }
1118
1119 if (name == NULL) {
1120 ALOGE("null key");
1121 ret = -ENODEV;
1122 goto done;
1123 }
1124
1125 for (i=0; i < len; i++) {
1126 const char* tn = table[i].name;
1127 size_t len = strlen(tn);
1128 if (strncmp(tn, name, len) == 0) {
1129 if (strlen(name) != len) {
1130 continue; // substring
1131 }
1132 ret = table[i].index;
1133 goto done;
1134 }
1135 }
1136 ALOGE("%s: Could not find index for name = %s",
1137 __func__, name);
1138 ret = -ENODEV;
1139done:
1140 return ret;
1141}
1142
1143int platform_get_snd_device_index(char *device_name)
1144{
1145 return find_index(snd_device_name_index, SND_DEVICE_MAX, device_name);
1146}
1147
1148int platform_get_usecase_index(const char *usecase_name)
1149{
1150 return find_index(usecase_name_index, AUDIO_USECASE_MAX, usecase_name);
1151}
1152
1153void platform_add_operator_specific_device(snd_device_t snd_device,
1154 const char *operator,
1155 const char *mixer_path,
1156 unsigned int acdb_id)
1157{
1158 struct operator_specific_device *device;
1159
1160 if (operator_specific_device_table[snd_device] == NULL) {
1161 operator_specific_device_table[snd_device] =
1162 (struct listnode *)calloc(1, sizeof(struct listnode));
1163 list_init(operator_specific_device_table[snd_device]);
1164 }
1165
1166 device = (struct operator_specific_device *)calloc(1, sizeof(struct operator_specific_device));
1167
1168 device->operator = strdup(operator);
1169 device->mixer_path = strdup(mixer_path);
1170 device->acdb_id = acdb_id;
1171
1172 list_add_tail(operator_specific_device_table[snd_device], &device->list);
1173
1174 ALOGD("%s: device[%s] -> operator[%s] mixer_path[%s] acdb_id[%d]", __func__,
1175 platform_get_snd_device_name(snd_device), operator, mixer_path, acdb_id);
1176
1177}
1178
1179int platform_set_snd_device_acdb_id(snd_device_t snd_device, unsigned int acdb_id)
1180{
1181 int ret = 0;
1182
1183 if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
1184 ALOGE("%s: Invalid snd_device = %d",
1185 __func__, snd_device);
1186 ret = -EINVAL;
1187 goto done;
1188 }
1189 ALOGV("%s: acdb_device_table[%s]: old = %d new = %d", __func__,
1190 platform_get_snd_device_name(snd_device), acdb_device_table[snd_device], acdb_id);
1191 acdb_device_table[snd_device] = acdb_id;
1192done:
1193 return ret;
1194}
1195
1196int platform_get_snd_device_acdb_id(snd_device_t snd_device)
1197{
1198 if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
1199 ALOGE("%s: Invalid snd_device = %d", __func__, snd_device);
1200 return -EINVAL;
1201 }
1202
1203 if (operator_specific_device_table[snd_device] != NULL)
1204 return get_operator_specific_device_acdb_id(snd_device);
1205 else
1206 return acdb_device_table[snd_device];
1207}
1208
1209int platform_send_audio_calibration(void *platform, snd_device_t snd_device)
1210{
1211 struct platform_data *my_data = (struct platform_data *)platform;
1212 int acdb_dev_id, acdb_dev_type;
1213
1214 acdb_dev_id = acdb_device_table[audio_extn_get_spkr_prot_snd_device(snd_device)];
1215 if (acdb_dev_id < 0) {
1216 ALOGE("%s: Could not find acdb id for device(%d)",
1217 __func__, snd_device);
1218 return -EINVAL;
1219 }
1220 if (my_data->acdb_send_audio_cal) {
1221 ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
1222 __func__, snd_device, acdb_dev_id);
1223 if (snd_device >= SND_DEVICE_OUT_BEGIN &&
1224 snd_device < SND_DEVICE_OUT_END)
1225 acdb_dev_type = ACDB_DEV_TYPE_OUT;
1226 else
1227 acdb_dev_type = ACDB_DEV_TYPE_IN;
1228 my_data->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
1229 }
1230 return 0;
1231}
1232
1233int platform_switch_voice_call_device_pre(void *platform __unused)
1234{
1235 return 0;
1236}
1237
1238int platform_switch_voice_call_enable_device_config(void *platform __unused,
1239 snd_device_t out_snd_device __unused,
1240 snd_device_t in_snd_device __unused)
1241{
1242 return 0;
1243}
1244
1245int platform_switch_voice_call_device_post(void *platform,
1246 snd_device_t out_snd_device,
1247 snd_device_t in_snd_device)
1248{
1249 struct platform_data *my_data = (struct platform_data *)platform;
1250 int acdb_rx_id, acdb_tx_id;
1251
1252 if (my_data->acdb_send_voice_cal == NULL) {
1253 ALOGE("%s: dlsym error for acdb_send_voice_call", __func__);
1254 } else {
1255 if (out_snd_device == SND_DEVICE_OUT_VOICE_SPEAKER &&
1256 audio_extn_spkr_prot_is_enabled())
1257 out_snd_device = SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED;
1258
1259 acdb_rx_id = platform_get_snd_device_acdb_id(out_snd_device);
1260 acdb_tx_id = platform_get_snd_device_acdb_id(in_snd_device);
1261
1262 if (acdb_rx_id > 0 && acdb_tx_id > 0)
1263 my_data->acdb_send_voice_cal(acdb_rx_id, acdb_tx_id);
1264 else
1265 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
1266 acdb_rx_id, acdb_tx_id);
1267 }
1268
1269 return 0;
1270}
1271
1272int platform_switch_voice_call_usecase_route_post(void *platform __unused,
1273 snd_device_t out_snd_device __unused,
1274 snd_device_t in_snd_device __unused)
1275{
1276 return 0;
1277}
1278
1279int platform_start_voice_call(void *platform __unused, uint32_t vsid __unused)
1280{
1281 return 0;
1282}
1283
1284int platform_stop_voice_call(void *platform __unused, uint32_t vsid __unused)
1285{
1286 return 0;
1287}
1288
1289int platform_get_sample_rate(void *platform __unused, uint32_t *rate __unused)
1290{
1291 return 0;
1292}
1293
1294void platform_set_speaker_gain_in_combo(struct audio_device *adev __unused,
1295 snd_device_t snd_device __unused,
1296 bool enable __unused)
1297{
1298 return;
1299}
1300
1301int platform_set_voice_volume(void *platform, int volume)
1302{
1303 struct platform_data *my_data = (struct platform_data *)platform;
1304 struct audio_device *adev = my_data->adev;
1305 struct mixer_ctl *ctl;
1306 const char *mixer_ctl_name = "Voice Rx Gain";
1307 int vol_index = 0, ret = 0;
1308 uint32_t set_values[ ] = {0,
1309 ALL_SESSION_VSID,
1310 DEFAULT_VOLUME_RAMP_DURATION_MS};
1311
1312 // Voice volume levels are mapped to adsp volume levels as follows.
1313 // 100 -> 5, 80 -> 4, 60 -> 3, 40 -> 2, 20 -> 1 0 -> 0
1314 // But this values don't changed in kernel. So, below change is need.
1315 vol_index = (int)percent_to_index(volume, MIN_VOL_INDEX, MAX_VOL_INDEX);
1316 set_values[0] = vol_index;
1317
1318 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1319 if (!ctl) {
1320 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1321 __func__, mixer_ctl_name);
1322 return -EINVAL;
1323 }
1324 ALOGV("Setting voice volume index: %d", set_values[0]);
1325 ret = mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
1326
1327 return ret;
1328}
1329
1330int platform_set_mic_mute(void *platform, bool state)
1331{
1332 struct platform_data *my_data = (struct platform_data *)platform;
1333 struct audio_device *adev = my_data->adev;
1334 struct mixer_ctl *ctl;
1335 const char *mixer_ctl_name = "Voice Tx Mute";
1336 int ret = 0;
1337 uint32_t set_values[ ] = {0,
1338 ALL_SESSION_VSID,
1339 DEFAULT_MUTE_RAMP_DURATION_MS};
1340
1341 set_values[0] = state;
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 mute state: %d", state);
1349 ret = mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
1350 return ret;
1351}
1352
1353int platform_set_device_mute(void *platform, bool state, char *dir)
1354{
1355 struct platform_data *my_data = (struct platform_data *)platform;
1356 struct audio_device *adev = my_data->adev;
1357 struct mixer_ctl *ctl;
1358 char *mixer_ctl_name = NULL;
1359 int ret = 0;
1360 uint32_t set_values[ ] = {0,
1361 ALL_SESSION_VSID,
1362 0};
1363 if(dir == NULL) {
1364 ALOGE("%s: Invalid direction:%s", __func__, dir);
1365 return -EINVAL;
1366 }
1367
1368 if (!strncmp("rx", dir, sizeof("rx"))) {
1369 mixer_ctl_name = "Voice Rx Device Mute";
1370 } else if (!strncmp("tx", dir, sizeof("tx"))) {
1371 mixer_ctl_name = "Voice Tx Device Mute";
1372 } else {
1373 return -EINVAL;
1374 }
1375
1376 set_values[0] = state;
1377 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1378 if (!ctl) {
1379 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1380 __func__, mixer_ctl_name);
1381 return -EINVAL;
1382 }
1383
1384 ALOGV("%s: Setting device mute state: %d, mixer ctrl:%s",
1385 __func__,state, mixer_ctl_name);
1386 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
1387
1388 return ret;
1389}
1390
1391bool platform_can_split_snd_device(snd_device_t snd_device,
1392 int *num_devices,
1393 snd_device_t *new_snd_devices)
1394{
1395 bool status = false;
1396
1397 if (NULL == num_devices || NULL == new_snd_devices) {
1398 ALOGE("%s: NULL pointer ..", __func__);
1399 return false;
1400 }
1401
1402 /*
1403 * If wired headset/headphones/line devices share the same backend
1404 * with speaker/earpiece this routine returns false.
1405 */
1406 if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES &&
1407 !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER, SND_DEVICE_OUT_HEADPHONES)) {
1408 *num_devices = 2;
1409 new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
1410 new_snd_devices[1] = SND_DEVICE_OUT_HEADPHONES;
1411 status = true;
1412 } else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_LINE &&
1413 !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER, SND_DEVICE_OUT_LINE)) {
1414 *num_devices = 2;
1415 new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
1416 new_snd_devices[1] = SND_DEVICE_OUT_LINE;
1417 status = true;
1418 }
1419 return status;
1420}
1421
1422snd_device_t platform_get_output_snd_device(void *platform, audio_devices_t devices)
1423{
1424 struct platform_data *my_data = (struct platform_data *)platform;
1425 struct audio_device *adev = my_data->adev;
1426 audio_mode_t mode = adev->mode;
1427 snd_device_t snd_device = SND_DEVICE_NONE;
1428
1429 audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
1430 AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
1431 int channel_count = popcount(channel_mask);
1432
1433 ALOGV("%s: enter: output devices(%#x)", __func__, devices);
1434 if (devices == AUDIO_DEVICE_NONE ||
1435 devices & AUDIO_DEVICE_BIT_IN) {
1436 ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
1437 goto exit;
1438 }
1439
1440 if (popcount(devices) == 2) {
1441 if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
1442 AUDIO_DEVICE_OUT_SPEAKER)) {
1443 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
1444 } else if (devices == (AUDIO_DEVICE_OUT_LINE |
1445 AUDIO_DEVICE_OUT_SPEAKER)) {
1446 snd_device = SND_DEVICE_OUT_SPEAKER_AND_LINE;
1447 } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
1448 AUDIO_DEVICE_OUT_SPEAKER)) {
1449 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
1450 } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
1451 AUDIO_DEVICE_OUT_SPEAKER)) {
1452 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
1453 } else if (devices == (AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET |
1454 AUDIO_DEVICE_OUT_SPEAKER)) {
1455 snd_device = SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET;
1456 } else {
1457 ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
1458 goto exit;
1459 }
1460 if (snd_device != SND_DEVICE_NONE) {
1461 goto exit;
1462 }
1463 }
1464
1465 if (popcount(devices) != 1) {
1466 ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
1467 goto exit;
1468 }
1469
1470 if (mode == AUDIO_MODE_IN_CALL) {
1471 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
1472 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET ||
1473 devices & AUDIO_DEVICE_OUT_LINE) {
1474 if (adev->voice.tty_mode != TTY_MODE_OFF) {
1475 switch (adev->voice.tty_mode) {
1476 case TTY_MODE_FULL:
1477 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
1478 break;
1479 case TTY_MODE_VCO:
1480 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
1481 break;
1482 case TTY_MODE_HCO:
1483 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
1484 break;
1485 default:
1486 ALOGE("%s: Invalid TTY mode (%#x)",
1487 __func__, adev->voice.tty_mode);
1488 }
1489 } else if (devices & AUDIO_DEVICE_OUT_LINE) {
1490 snd_device = SND_DEVICE_OUT_VOICE_LINE;
1491 } else {
1492 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
1493 }
1494 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
1495 if (adev->bt_wb_speech_enabled)
1496 snd_device = SND_DEVICE_OUT_BT_SCO_WB;
1497 else
1498 snd_device = SND_DEVICE_OUT_BT_SCO;
1499 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
1500 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
1501 } else if (devices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET ||
1502 devices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) {
1503 snd_device = SND_DEVICE_OUT_USB_HEADSET;
1504 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
1505 snd_device = SND_DEVICE_OUT_VOICE_HANDSET;
1506 } else if (devices & AUDIO_DEVICE_OUT_TELEPHONY_TX)
1507 snd_device = SND_DEVICE_OUT_VOICE_TX;
1508
1509 if (snd_device != SND_DEVICE_NONE) {
1510 goto exit;
1511 }
1512 }
1513
1514 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
1515 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
1516 snd_device = SND_DEVICE_OUT_HEADPHONES;
1517 } else if (devices & AUDIO_DEVICE_OUT_LINE) {
1518 snd_device = SND_DEVICE_OUT_LINE;
1519 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
1520 if (my_data->speaker_lr_swap)
1521 snd_device = SND_DEVICE_OUT_SPEAKER_REVERSE;
1522 else
1523 snd_device = SND_DEVICE_OUT_SPEAKER;
1524 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
1525 if (adev->bt_wb_speech_enabled)
1526 snd_device = SND_DEVICE_OUT_BT_SCO_WB;
1527 else
1528 snd_device = SND_DEVICE_OUT_BT_SCO;
1529 } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1530 snd_device = SND_DEVICE_OUT_HDMI ;
1531 } 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_HANDSET;
1536 } else if (devices & AUDIO_DEVICE_OUT_PROXY) {
1537 snd_device = SND_DEVICE_OUT_AFE_PROXY;
1538 } else {
1539 ALOGE("%s: Unknown device(s) %#x", __func__, devices);
1540 }
1541exit:
1542 ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
1543 return snd_device;
1544}
1545
1546snd_device_t platform_get_input_snd_device(void *platform, audio_devices_t out_device)
1547{
1548 struct platform_data *my_data = (struct platform_data *)platform;
1549 struct audio_device *adev = my_data->adev;
1550 audio_source_t source = (adev->active_input == NULL) ?
1551 AUDIO_SOURCE_DEFAULT : adev->active_input->source;
1552
1553 audio_mode_t mode = adev->mode;
1554 audio_devices_t in_device = ((adev->active_input == NULL) ?
1555 AUDIO_DEVICE_NONE : adev->active_input->device)
1556 & ~AUDIO_DEVICE_BIT_IN;
1557 audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
1558 AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
1559 snd_device_t snd_device = SND_DEVICE_NONE;
1560 int channel_count = popcount(channel_mask);
1561
1562 ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
1563 __func__, out_device, in_device);
1564
1565 if ((out_device != AUDIO_DEVICE_NONE) && ((mode == AUDIO_MODE_IN_CALL) ||
1566 audio_extn_hfp_is_active(adev))) {
1567 if (adev->voice.tty_mode != TTY_MODE_OFF) {
1568 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
1569 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET ||
1570 out_device & AUDIO_DEVICE_OUT_LINE) {
1571 switch (adev->voice.tty_mode) {
1572 case TTY_MODE_FULL:
1573 snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
1574 break;
1575 case TTY_MODE_VCO:
1576 snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
1577 break;
1578 case TTY_MODE_HCO:
1579 snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
1580 break;
1581 default:
1582 ALOGE("%s: Invalid TTY mode (%#x)",
1583 __func__, adev->voice.tty_mode);
1584 }
1585 goto exit;
1586 }
1587 }
1588 if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
1589 out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
1590 out_device & AUDIO_DEVICE_OUT_LINE) {
1591 if (my_data->fluence_type == FLUENCE_NONE ||
1592 my_data->fluence_in_voice_call == false) {
1593 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1594 if (audio_extn_hfp_is_active(adev))
1595 platform_set_echo_reference(adev, true, out_device);
1596 } else {
1597 snd_device = SND_DEVICE_IN_VOICE_DMIC;
1598 adev->acdb_settings |= DMIC_FLAG;
1599 }
1600 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
1601 snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
1602 if (audio_extn_hfp_is_active(adev))
1603 platform_set_echo_reference(adev, true, out_device);
1604 } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
1605 if (adev->bt_wb_speech_enabled) {
1606 if (adev->bluetooth_nrec)
1607 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
1608 else
1609 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
1610 } else {
1611 if (adev->bluetooth_nrec)
1612 snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
1613 else
1614 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
1615 }
1616 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
1617 if (my_data->fluence_type != FLUENCE_NONE &&
1618 my_data->fluence_in_voice_call &&
1619 my_data->fluence_in_spkr_mode) {
1620 if(my_data->fluence_type & FLUENCE_QUAD_MIC) {
1621 adev->acdb_settings |= QMIC_FLAG;
1622 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_QMIC;
1623 } else {
1624 adev->acdb_settings |= DMIC_FLAG;
1625 if (my_data->fluence_mode == FLUENCE_BROADSIDE)
1626 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BROADSIDE;
1627 else
1628 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC;
1629 }
1630 } else {
1631 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
1632 if (audio_extn_hfp_is_active(adev))
1633 platform_set_echo_reference(adev, true, out_device);
1634 }
1635 } else if (out_device & AUDIO_DEVICE_OUT_TELEPHONY_TX)
1636 snd_device = SND_DEVICE_IN_VOICE_RX;
1637 } else if (source == AUDIO_SOURCE_CAMCORDER) {
1638 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
1639 in_device & AUDIO_DEVICE_IN_BACK_MIC) {
1640 if (my_data->fluence_type & FLUENCE_DUAL_MIC &&
1641 channel_count == 2)
1642 snd_device = SND_DEVICE_IN_HANDSET_STEREO_DMIC;
1643 else
1644 snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
1645 }
1646 } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
1647 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
1648 if (channel_count == 2) {
1649 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_STEREO;
1650 adev->acdb_settings |= DMIC_FLAG;
1651 } else if (adev->active_input->enable_ns)
1652 snd_device = SND_DEVICE_IN_VOICE_REC_MIC_NS;
1653 else if (my_data->fluence_type != FLUENCE_NONE &&
1654 my_data->fluence_in_voice_rec) {
1655 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE;
1656 adev->acdb_settings |= DMIC_FLAG;
1657 } else {
1658 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
1659 }
1660 }
1661 } else if ((source == AUDIO_SOURCE_VOICE_COMMUNICATION) ||
1662 (mode == AUDIO_MODE_IN_COMMUNICATION)) {
1663 if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
1664 in_device = AUDIO_DEVICE_IN_BACK_MIC;
1665 if (adev->active_input) {
1666 if (my_data->fluence_type != FLUENCE_NONE &&
1667 adev->active_input->enable_aec &&
1668 adev->active_input->enable_ns) {
1669 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
1670 if (my_data->fluence_in_spkr_mode) {
1671 if (my_data->fluence_type & FLUENCE_QUAD_MIC) {
1672 snd_device = SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS;
1673 } else if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
1674 if (my_data->fluence_mode == FLUENCE_BROADSIDE)
1675 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS_BROADSIDE;
1676 else
1677 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS;
1678 }
1679 adev->acdb_settings |= DMIC_FLAG;
1680 } else
1681 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC_NS;
1682 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
1683 if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
1684 snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC_NS;
1685 adev->acdb_settings |= DMIC_FLAG;
1686 } else
1687 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC_NS;
1688 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
1689 snd_device = SND_DEVICE_IN_HEADSET_MIC_FLUENCE;
1690 }
1691 platform_set_echo_reference(adev, true, out_device);
1692 } else if (my_data->fluence_type != FLUENCE_NONE &&
1693 adev->active_input->enable_aec) {
1694 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
1695 if (my_data->fluence_in_spkr_mode) {
1696 if (my_data->fluence_type & FLUENCE_QUAD_MIC) {
1697 snd_device = SND_DEVICE_IN_SPEAKER_QMIC_AEC;
1698 } else if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
1699 if (my_data->fluence_mode == FLUENCE_BROADSIDE)
1700 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC_BROADSIDE;
1701 else
1702 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC;
1703 }
1704 adev->acdb_settings |= DMIC_FLAG;
1705 } else
1706 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
1707 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
1708 if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
1709 snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC;
1710 adev->acdb_settings |= DMIC_FLAG;
1711 } else
1712 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
1713 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
1714 snd_device = SND_DEVICE_IN_HEADSET_MIC_FLUENCE;
1715 }
1716 platform_set_echo_reference(adev, true, out_device);
1717 } else if (my_data->fluence_type != FLUENCE_NONE &&
1718 adev->active_input->enable_ns) {
1719 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
1720 if (my_data->fluence_in_spkr_mode) {
1721 if (my_data->fluence_type & FLUENCE_QUAD_MIC) {
1722 snd_device = SND_DEVICE_IN_SPEAKER_QMIC_NS;
1723 } else if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
1724 if (my_data->fluence_mode == FLUENCE_BROADSIDE)
1725 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_NS_BROADSIDE;
1726 else
1727 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_NS;
1728 }
1729 adev->acdb_settings |= DMIC_FLAG;
1730 } else
1731 snd_device = SND_DEVICE_IN_SPEAKER_MIC_NS;
1732 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
1733 if (my_data->fluence_type & FLUENCE_DUAL_MIC) {
1734 snd_device = SND_DEVICE_IN_HANDSET_DMIC_NS;
1735 adev->acdb_settings |= DMIC_FLAG;
1736 } else
1737 snd_device = SND_DEVICE_IN_HANDSET_MIC_NS;
1738 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
1739 snd_device = SND_DEVICE_IN_HEADSET_MIC_FLUENCE;
1740 }
1741 platform_set_echo_reference(adev, false, out_device);
1742 } else
1743 platform_set_echo_reference(adev, false, out_device);
1744 }
1745 } else if (source == AUDIO_SOURCE_FM_TUNER) {
1746 snd_device = SND_DEVICE_IN_CAPTURE_FM;
1747 } else if (source == AUDIO_SOURCE_DEFAULT) {
1748 goto exit;
1749 }
1750
1751
1752 if (snd_device != SND_DEVICE_NONE) {
1753 goto exit;
1754 }
1755
1756 if (in_device != AUDIO_DEVICE_NONE &&
1757 !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
1758 !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
1759 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
1760 if (my_data->fluence_type & (FLUENCE_DUAL_MIC | FLUENCE_QUAD_MIC) &&
1761 channel_count == 2)
1762 snd_device = SND_DEVICE_IN_HANDSET_STEREO_DMIC;
1763 else
1764 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1765 } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
1766 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
1767 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
1768 snd_device = SND_DEVICE_IN_HEADSET_MIC;
1769 } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
1770 if (adev->bt_wb_speech_enabled) {
1771 if (adev->bluetooth_nrec)
1772 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
1773 else
1774 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
1775 } else {
1776 if (adev->bluetooth_nrec)
1777 snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
1778 else
1779 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
1780 }
1781 } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
1782 snd_device = SND_DEVICE_IN_HDMI_MIC;
1783 } else if (in_device & AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET ||
1784 in_device & AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET) {
1785 snd_device = SND_DEVICE_IN_USB_HEADSET_MIC;
1786 } else if (in_device & AUDIO_DEVICE_IN_FM_TUNER) {
1787 snd_device = SND_DEVICE_IN_CAPTURE_FM;
1788 } else {
1789 ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
1790 ALOGW("%s: Using default handset-mic", __func__);
1791 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1792 }
1793 } else {
1794 if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
1795 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1796 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
1797 snd_device = SND_DEVICE_IN_HEADSET_MIC;
1798 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
1799 if (channel_count > 1)
1800 snd_device = SND_DEVICE_IN_SPEAKER_STEREO_DMIC;
1801 else
1802 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
1803 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
1804 out_device & AUDIO_DEVICE_OUT_LINE) {
1805 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1806 } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
1807 if (adev->bt_wb_speech_enabled) {
1808 if (adev->bluetooth_nrec)
1809 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
1810 else
1811 snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
1812 } else {
1813 if (adev->bluetooth_nrec)
1814 snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
1815 else
1816 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
1817 }
1818 } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1819 snd_device = SND_DEVICE_IN_HDMI_MIC;
1820 } else if (out_device & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET ||
1821 out_device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) {
1822 snd_device = SND_DEVICE_IN_USB_HEADSET_MIC;
1823 } else {
1824 ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
1825 ALOGW("%s: Using default handset-mic", __func__);
1826 snd_device = SND_DEVICE_IN_HANDSET_MIC;
1827 }
1828 }
1829exit:
1830 ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
1831 return snd_device;
1832}
1833
1834int platform_set_hdmi_channels(void *platform, int channel_count)
1835{
1836 struct platform_data *my_data = (struct platform_data *)platform;
1837 struct audio_device *adev = my_data->adev;
1838 struct mixer_ctl *ctl;
1839 const char *channel_cnt_str = NULL;
1840 const char *mixer_ctl_name = "HDMI_RX Channels";
1841 switch (channel_count) {
1842 case 8:
1843 channel_cnt_str = "Eight"; break;
1844 case 7:
1845 channel_cnt_str = "Seven"; break;
1846 case 6:
1847 channel_cnt_str = "Six"; break;
1848 case 5:
1849 channel_cnt_str = "Five"; break;
1850 case 4:
1851 channel_cnt_str = "Four"; break;
1852 case 3:
1853 channel_cnt_str = "Three"; break;
1854 default:
1855 channel_cnt_str = "Two"; break;
1856 }
1857 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1858 if (!ctl) {
1859 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1860 __func__, mixer_ctl_name);
1861 return -EINVAL;
1862 }
1863 ALOGV("HDMI channel count: %s", channel_cnt_str);
1864 mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
1865 return 0;
1866}
1867
1868int platform_edid_get_max_channels(void *platform)
1869{
1870 struct platform_data *my_data = (struct platform_data *)platform;
1871 struct audio_device *adev = my_data->adev;
1872 char block[MAX_SAD_BLOCKS * SAD_BLOCK_SIZE];
1873 char *sad = block;
1874 int num_audio_blocks;
1875 int channel_count;
1876 int max_channels = 0;
1877 int i, ret, count;
1878
1879 struct mixer_ctl *ctl;
1880
1881 ctl = mixer_get_ctl_by_name(adev->mixer, AUDIO_DATA_BLOCK_MIXER_CTL);
1882 if (!ctl) {
1883 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1884 __func__, AUDIO_DATA_BLOCK_MIXER_CTL);
1885 return 0;
1886 }
1887
1888 mixer_ctl_update(ctl);
1889
1890 count = mixer_ctl_get_num_values(ctl);
1891
1892 /* Read SAD blocks, clamping the maximum size for safety */
1893 if (count > (int)sizeof(block))
1894 count = (int)sizeof(block);
1895
1896 ret = mixer_ctl_get_array(ctl, block, count);
1897 if (ret != 0) {
1898 ALOGE("%s: mixer_ctl_get_array() failed to get EDID info", __func__);
1899 return 0;
1900 }
1901
1902 /* Calculate the number of SAD blocks */
1903 num_audio_blocks = count / SAD_BLOCK_SIZE;
1904
1905 for (i = 0; i < num_audio_blocks; i++) {
1906 /* Only consider LPCM blocks */
1907 if ((sad[0] >> 3) != EDID_FORMAT_LPCM) {
1908 sad += 3;
1909 continue;
1910 }
1911
1912 channel_count = (sad[0] & 0x7) + 1;
1913 if (channel_count > max_channels)
1914 max_channels = channel_count;
1915
1916 /* Advance to next block */
1917 sad += 3;
1918 }
1919
1920 return max_channels;
1921}
1922
1923int platform_set_incall_recording_session_id(void *platform,
1924 uint32_t session_id,
1925 int rec_mode __unused)
1926{
1927 int ret = 0;
1928 struct platform_data *my_data = (struct platform_data *)platform;
1929 struct audio_device *adev = my_data->adev;
1930 struct mixer_ctl *ctl;
1931 const char *mixer_ctl_name = "Voc VSID";
1932 int num_ctl_values;
1933 int i;
1934
1935 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1936 if (!ctl) {
1937 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1938 __func__, mixer_ctl_name);
1939 ret = -EINVAL;
1940 } else {
1941 num_ctl_values = mixer_ctl_get_num_values(ctl);
1942 for (i = 0; i < num_ctl_values; i++) {
1943 if (mixer_ctl_set_value(ctl, i, session_id)) {
1944 ALOGV("Error: invalid session_id: %x", session_id);
1945 ret = -EINVAL;
1946 break;
1947 }
1948 }
1949 }
1950 return ret;
1951}
1952
1953int platform_stop_incall_recording_usecase(void *platform __unused)
1954{
1955 return 0;
1956}
1957
1958int platform_start_incall_music_usecase(void *platform __unused)
1959{
1960 return 0;
1961}
1962
1963int platform_stop_incall_music_usecase(void *platform __unused)
1964{
1965 return 0;
1966}
1967
1968int platform_set_parameters(void *platform, struct str_parms *parms)
1969{
1970 struct platform_data *my_data = (struct platform_data *)platform;
1971 char value[128];
1972 char *kv_pairs = str_parms_to_str(parms);
1973 int ret = 0, err;
1974
1975 if (kv_pairs == NULL) {
1976 ret = -EINVAL;
1977 ALOGE("%s: key-value pair is NULL",__func__);
1978 goto done;
1979 }
1980
1981 ALOGV("%s: enter: %s", __func__, kv_pairs);
1982
1983 err = str_parms_get_str(parms, PLATFORM_CONFIG_KEY_OPERATOR_INFO,
1984 value, sizeof(value));
1985 if (err >= 0) {
1986 struct operator_info *info;
1987 char *str = value;
1988 char *name;
1989
1990 str_parms_del(parms, PLATFORM_CONFIG_KEY_OPERATOR_INFO);
1991 info = (struct operator_info *)calloc(1, sizeof(struct operator_info));
1992 name = strtok(str, ";");
1993 info->name = strdup(name);
1994 info->mccmnc = strdup(str + strlen(name) + 1);
1995
1996 list_add_tail(&operator_info_list, &info->list);
1997 ALOGV("%s: add operator[%s] mccmnc[%s]", __func__, info->name, info->mccmnc);
1998 }
1999done:
2000 ALOGV("%s: exit with code(%d)", __func__, ret);
2001 if (kv_pairs != NULL)
2002 free(kv_pairs);
2003
2004 return ret;
2005}
2006
2007/* Delay in Us */
2008int64_t platform_render_latency(audio_usecase_t usecase)
2009{
2010 switch (usecase) {
2011 case USECASE_AUDIO_PLAYBACK_DEEP_BUFFER:
2012 return DEEP_BUFFER_PLATFORM_DELAY;
2013 case USECASE_AUDIO_PLAYBACK_LOW_LATENCY:
2014 return LOW_LATENCY_PLATFORM_DELAY;
2015 default:
2016 return 0;
2017 }
2018}
2019
2020int platform_set_snd_device_backend(snd_device_t device, const char *backend, const char * hw_interface)
2021{
2022 int ret = 0;
2023
2024 if ((device < SND_DEVICE_MIN) || (device >= SND_DEVICE_MAX)) {
2025 ALOGE("%s: Invalid snd_device = %d",
2026 __func__, device);
2027 ret = -EINVAL;
2028 goto done;
2029 }
2030
2031 ALOGV("%s: backend_tag_table[%s]: old = %s new = %s", __func__,
2032 platform_get_snd_device_name(device),
2033 backend_table[device] != NULL ? backend_table[device]: "null", backend);
2034 if (backend_table[device]) {
2035 free(backend_table[device]);
2036 }
2037 backend_table[device] = strdup(backend);
2038
2039 if (hw_interface != NULL) {
2040 if (hw_interface_table[device])
2041 free(hw_interface_table[device]);
2042 ALOGV("%s: hw_interface_table[%d] = %s", __func__, device, hw_interface);
2043 hw_interface_table[device] = strdup(hw_interface);
2044 }
2045done:
2046 return ret;
2047}
2048
2049int platform_set_usecase_pcm_id(audio_usecase_t usecase, int32_t type, int32_t pcm_id)
2050{
2051 int ret = 0;
2052 if ((usecase <= USECASE_INVALID) || (usecase >= AUDIO_USECASE_MAX)) {
2053 ALOGE("%s: invalid usecase case idx %d", __func__, usecase);
2054 ret = -EINVAL;
2055 goto done;
2056 }
2057
2058 if ((type != 0) && (type != 1)) {
2059 ALOGE("%s: invalid usecase type", __func__);
2060 ret = -EINVAL;
2061 }
2062 pcm_device_table[usecase][type] = pcm_id;
2063done:
2064 return ret;
2065}
2066
2067#define DEFAULT_NOMINAL_SPEAKER_GAIN 20
2068int ramp_speaker_gain(struct audio_device *adev, bool ramp_up, int target_ramp_up_gain) {
2069 // backup_gain: gain to try to set in case of an error during ramp
2070 int start_gain, end_gain, step, backup_gain, i;
2071 bool error = false;
2072 const struct mixer_ctl *ctl;
2073 const char *mixer_ctl_name_gain_left = "Left Speaker Gain";
2074 const char *mixer_ctl_name_gain_right = "Right Speaker Gain";
2075 struct mixer_ctl *ctl_left = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name_gain_left);
2076 struct mixer_ctl *ctl_right = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name_gain_right);
2077 if (!ctl_left || !ctl_right) {
2078 ALOGE("%s: Could not get ctl for mixer cmd - %s or %s, not applying speaker gain ramp",
2079 __func__, mixer_ctl_name_gain_left, mixer_ctl_name_gain_right);
2080 return -EINVAL;
2081 } else if ((mixer_ctl_get_num_values(ctl_left) != 1)
2082 || (mixer_ctl_get_num_values(ctl_right) != 1)) {
2083 ALOGE("%s: Unexpected num values for mixer cmd - %s or %s, not applying speaker gain ramp",
2084 __func__, mixer_ctl_name_gain_left, mixer_ctl_name_gain_right);
2085 return -EINVAL;
2086 }
2087 if (ramp_up) {
2088 start_gain = 0;
2089 end_gain = target_ramp_up_gain > 0 ? target_ramp_up_gain : DEFAULT_NOMINAL_SPEAKER_GAIN;
2090 step = +1;
2091 backup_gain = end_gain;
2092 } else {
2093 // using same gain on left and right
2094 const int left_gain = mixer_ctl_get_value(ctl_left, 0);
2095 start_gain = left_gain > 0 ? left_gain : DEFAULT_NOMINAL_SPEAKER_GAIN;
2096 end_gain = 0;
2097 step = -1;
2098 backup_gain = start_gain;
2099 }
2100 for (i = start_gain ; i != (end_gain + step) ; i += step) {
2101 //ALOGV("setting speaker gain to %d", i);
2102 if (mixer_ctl_set_value(ctl_left, 0, i)) {
2103 ALOGE("%s: error setting %s to %d during gain ramp",
2104 __func__, mixer_ctl_name_gain_left, i);
2105 error = true;
2106 break;
2107 }
2108 if (mixer_ctl_set_value(ctl_right, 0, i)) {
2109 ALOGE("%s: error setting %s to %d during gain ramp",
2110 __func__, mixer_ctl_name_gain_right, i);
2111 error = true;
2112 break;
2113 }
2114 usleep(1000);
2115 }
2116 if (error) {
2117 // an error occured during the ramp, let's still try to go back to a safe volume
2118 if (mixer_ctl_set_value(ctl_left, 0, backup_gain)) {
2119 ALOGE("%s: error restoring left gain to %d", __func__, backup_gain);
2120 }
2121 if (mixer_ctl_set_value(ctl_right, 0, backup_gain)) {
2122 ALOGE("%s: error restoring right gain to %d", __func__, backup_gain);
2123 }
2124 }
2125 return start_gain;
2126}
2127
2128int platform_swap_lr_channels(struct audio_device *adev, bool swap_channels)
2129{
2130 // only update if there is active pcm playback on speaker
2131 struct audio_usecase *usecase;
2132 struct listnode *node;
2133 struct platform_data *my_data = (struct platform_data *)adev->platform;
2134
2135 if (my_data->speaker_lr_swap != swap_channels) {
2136 my_data->speaker_lr_swap = swap_channels;
2137
2138 list_for_each(node, &adev->usecase_list) {
2139 usecase = node_to_item(node, struct audio_usecase, list);
2140 if (usecase->type == PCM_PLAYBACK &&
2141 usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
2142 /*
2143 * If acdb tuning is different for SPEAKER_REVERSE, it is must
2144 * to perform device switch to disable the current backend to
2145 * enable it with new acdb data.
2146 */
2147 if (acdb_device_table[SND_DEVICE_OUT_SPEAKER] !=
2148 acdb_device_table[SND_DEVICE_OUT_SPEAKER_REVERSE]) {
2149 const int initial_skpr_gain = ramp_speaker_gain(adev, false /*ramp_up*/, -1);
2150 select_devices(adev, usecase->id);
2151 if (initial_skpr_gain != -EINVAL) {
2152 ramp_speaker_gain(adev, true /*ramp_up*/, initial_skpr_gain);
2153 }
2154 } else {
2155 const char *mixer_path;
2156 if (swap_channels) {
2157 mixer_path = platform_get_snd_device_name(SND_DEVICE_OUT_SPEAKER_REVERSE);
2158 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
2159 } else {
2160 mixer_path = platform_get_snd_device_name(SND_DEVICE_OUT_SPEAKER);
2161 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
2162 }
2163 }
2164 break;
2165 }
2166 }
2167 }
2168 return 0;
2169}