blob: 08426de60c70f8a440580c19762a5bfd62a87aae [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <hardware/audio.h>
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -070018#include <hardware/audio_effect.h>
19#include <audio_effects/effect_aec.h>
20#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080021
22#include <tinyalsa/asoundlib.h>
23
24#include <audio_route/audio_route.h>
25
26#define ACDB_DEV_TYPE_OUT 1
27#define ACDB_DEV_TYPE_IN 2
28
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080029#define DUALMIC_CONFIG_NONE 0 /* Target does not contain 2 mics */
30#define DUALMIC_CONFIG_ENDFIRE 1
31#define DUALMIC_CONFIG_BROADSIDE 2
32
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -070033/*
34 * Below are the devices for which is back end is same, SLIMBUS_0_RX.
35 * All these devices are handled by the internal HW codec. We can
36 * enable any one of these devices at any time
37 */
38#define AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND \
39 (AUDIO_DEVICE_OUT_EARPIECE | AUDIO_DEVICE_OUT_SPEAKER | \
40 AUDIO_DEVICE_OUT_WIRED_HEADSET | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)
41
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080042/* Sound devices specific to the platform
43 * The DEVICE_OUT_* and DEVICE_IN_* should be mapped to these sound
44 * devices to enable corresponding mixer paths
45 */
46typedef enum {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080047 SND_DEVICE_NONE = 0,
48
49 /* Playback devices */
50 SND_DEVICE_MIN,
51 SND_DEVICE_OUT_BEGIN = SND_DEVICE_MIN,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080052 SND_DEVICE_OUT_HANDSET = SND_DEVICE_OUT_BEGIN,
53 SND_DEVICE_OUT_SPEAKER,
Jean-Michel Trivic56336b2013-05-24 16:55:17 -070054 SND_DEVICE_OUT_SPEAKER_REVERSE,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080055 SND_DEVICE_OUT_HEADPHONES,
56 SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES,
57 SND_DEVICE_OUT_VOICE_SPEAKER,
58 SND_DEVICE_OUT_VOICE_HEADPHONES,
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080059 SND_DEVICE_OUT_HDMI,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080060 SND_DEVICE_OUT_SPEAKER_AND_HDMI,
61 SND_DEVICE_OUT_BT_SCO,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080062 SND_DEVICE_OUT_VOICE_HANDSET_TMUS,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080063 SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES,
64 SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES,
65 SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080066 SND_DEVICE_OUT_END,
67
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080068 /*
69 * Note: IN_BEGIN should be same as OUT_END because total number of devices
70 * SND_DEVICES_MAX should not exceed MAX_RX + MAX_TX devices.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080071 */
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080072 /* Capture devices */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080073 SND_DEVICE_IN_BEGIN = SND_DEVICE_OUT_END,
74 SND_DEVICE_IN_HANDSET_MIC = SND_DEVICE_IN_BEGIN,
75 SND_DEVICE_IN_SPEAKER_MIC,
76 SND_DEVICE_IN_HEADSET_MIC,
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -070077 SND_DEVICE_IN_HANDSET_MIC_AEC,
78 SND_DEVICE_IN_SPEAKER_MIC_AEC,
79 SND_DEVICE_IN_HEADSET_MIC_AEC,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080080 SND_DEVICE_IN_VOICE_SPEAKER_MIC,
81 SND_DEVICE_IN_VOICE_HEADSET_MIC,
82 SND_DEVICE_IN_HDMI_MIC,
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080083 SND_DEVICE_IN_BT_SCO_MIC,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080084 SND_DEVICE_IN_CAMCORDER_MIC,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080085 SND_DEVICE_IN_VOICE_DMIC_EF,
86 SND_DEVICE_IN_VOICE_DMIC_BS,
87 SND_DEVICE_IN_VOICE_DMIC_EF_TMUS,
88 SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF,
89 SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080090 SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC,
91 SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC,
92 SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080093 SND_DEVICE_IN_VOICE_REC_MIC,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080094 SND_DEVICE_IN_VOICE_REC_DMIC_EF,
95 SND_DEVICE_IN_VOICE_REC_DMIC_BS,
Eric Laurentc8400632013-02-14 19:04:54 -080096 SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE,
97 SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080098 SND_DEVICE_IN_END,
99
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800100 SND_DEVICE_MAX = SND_DEVICE_IN_END,
101
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800102} snd_device_t;
103
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800104
105/* These are the supported use cases by the hardware.
106 * Each usecase is mapped to a specific PCM device.
107 * Refer to pcm_device_table[].
108 */
109typedef enum {
110 USECASE_INVALID = -1,
111 /* Playback usecases */
112 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER = 0,
113 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
114 USECASE_AUDIO_PLAYBACK_MULTI_CH,
115
116 /* Capture usecases */
117 USECASE_AUDIO_RECORD,
118 USECASE_AUDIO_RECORD_LOW_LATENCY,
119
120 USECASE_VOICE_CALL,
121
122 AUDIO_USECASE_MAX
123} audio_usecase_t;
124
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800125#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
126
127#define SOUND_CARD 0
128
129#define DEFAULT_OUTPUT_SAMPLING_RATE 48000
130
131/*
132 * tinyAlsa library interprets period size as number of frames
133 * one frame = channel_count * sizeof (pcm sample)
134 * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
135 * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
136 * We should take care of returning proper size when AudioFlinger queries for
137 * the buffer size of an input/output stream
138 */
Sungmin Choi5195a4b2013-04-03 21:54:22 -0700139#ifdef MSM8974
140#define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 1024
141#else
Ravi Kumar Alamandac3011862013-03-19 16:46:58 -0700142#define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 960
Sungmin Choi5195a4b2013-04-03 21:54:22 -0700143#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800144#define DEEP_BUFFER_OUTPUT_PERIOD_COUNT 8
145
Sungmin Choi5195a4b2013-04-03 21:54:22 -0700146#ifdef MSM8974
147#define LOW_LATENCY_OUTPUT_PERIOD_SIZE 256
148#else
Ravi Kumar Alamandac3011862013-03-19 16:46:58 -0700149#define LOW_LATENCY_OUTPUT_PERIOD_SIZE 240
Sungmin Choi5195a4b2013-04-03 21:54:22 -0700150#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800151#define LOW_LATENCY_OUTPUT_PERIOD_COUNT 2
152
153#define HDMI_MULTI_PERIOD_SIZE 336
154#define HDMI_MULTI_PERIOD_COUNT 8
155#define HDMI_MULTI_DEFAULT_CHANNEL_COUNT 6
156#define HDMI_MULTI_PERIOD_BYTES (HDMI_MULTI_PERIOD_SIZE * HDMI_MULTI_DEFAULT_CHANNEL_COUNT * 2)
157
Sungmin Choi6336b0d2013-04-11 13:04:38 -0700158#ifdef MSM8974
159#define AUDIO_CAPTURE_PERIOD_SIZE 512
160#define AUDIO_CAPTURE_PERIOD_COUNT 16
161#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800162#define AUDIO_CAPTURE_PERIOD_SIZE 320
163#define AUDIO_CAPTURE_PERIOD_COUNT 2
Sungmin Choi6336b0d2013-04-11 13:04:38 -0700164#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800165
166#define MAX_SUPPORTED_CHANNEL_MASKS 2
167
168struct stream_out {
169 struct audio_stream_out stream;
Eric Laurent150dbfe2013-02-27 14:31:02 -0800170 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800171 struct pcm_config config;
172 struct pcm *pcm;
173 int standby;
174 int pcm_device_id;
175 audio_channel_mask_t channel_mask;
176 audio_devices_t devices;
177 audio_output_flags_t flags;
178 audio_usecase_t usecase;
179 /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
180 audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
Eric Laurenta9024de2013-04-04 09:19:12 -0700181 bool muted;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800182
183 struct audio_device *dev;
184};
185
186struct stream_in {
187 struct audio_stream_in stream;
Eric Laurent150dbfe2013-02-27 14:31:02 -0800188 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800189 struct pcm_config config;
190 struct pcm *pcm;
191 int standby;
192 int source;
193 int pcm_device_id;
194 int device;
195 audio_channel_mask_t channel_mask;
196 audio_usecase_t usecase;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700197 bool enable_aec;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800198
199 struct audio_device *dev;
200};
201
202typedef enum {
203 PCM_PLAYBACK,
204 PCM_CAPTURE,
205 VOICE_CALL
206} usecase_type_t;
207
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800208union stream_ptr {
209 struct stream_in *in;
210 struct stream_out *out;
211};
212
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800213struct audio_usecase {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800214 struct listnode list;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800215 audio_usecase_t id;
216 usecase_type_t type;
217 audio_devices_t devices;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700218 snd_device_t out_snd_device;
219 snd_device_t in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800220 union stream_ptr stream;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800221};
222
223typedef void (*acdb_deallocate_t)();
224typedef int (*acdb_init_t)();
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800225typedef void (*acdb_send_audio_cal_t)(int, int);
226typedef void (*acdb_send_voice_cal_t)(int, int);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800227
228typedef int (*csd_client_init_t)();
229typedef int (*csd_client_deinit_t)();
230typedef int (*csd_disable_device_t)();
231typedef int (*csd_enable_device_t)(int, int, uint32_t);
232typedef int (*csd_volume_t)(int);
233typedef int (*csd_mic_mute_t)(int);
234typedef int (*csd_start_voice_t)();
235typedef int (*csd_stop_voice_t)();
236
237struct audio_device {
238 struct audio_hw_device device;
Eric Laurent150dbfe2013-02-27 14:31:02 -0800239 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800240 struct mixer *mixer;
241 audio_mode_t mode;
242 audio_devices_t out_device;
Eric Laurentc8400632013-02-14 19:04:54 -0800243 struct stream_in *active_input;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800244 struct stream_out *primary_output;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800245 int in_call;
246 float voice_volume;
247 bool mic_mute;
248 int tty_mode;
249 bool bluetooth_nrec;
250 bool screen_off;
251 struct pcm *voice_call_rx;
252 struct pcm *voice_call_tx;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700253 int snd_dev_ref_cnt[SND_DEVICE_MAX];
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800254 struct listnode usecase_list;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800255 struct audio_route *audio_route;
256 int acdb_settings;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -0700257 bool speaker_lr_swap;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800258
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800259 bool mic_type_analog;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800260 bool fluence_in_spkr_mode;
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800261 bool fluence_in_voice_call;
262 bool fluence_in_voice_rec;
263 int dualmic_config;
264
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800265 /* Audio calibration related functions */
266 void *acdb_handle;
267 acdb_init_t acdb_init;
268 acdb_deallocate_t acdb_deallocate;
269 acdb_send_audio_cal_t acdb_send_audio_cal;
270 acdb_send_voice_cal_t acdb_send_voice_cal;
271
272 /* CSD Client related functions for voice call */
273 void *csd_client;
274 csd_client_init_t csd_client_init;
275 csd_client_deinit_t csd_client_deinit;
276 csd_disable_device_t csd_disable_device;
277 csd_enable_device_t csd_enable_device;
278 csd_volume_t csd_volume;
279 csd_mic_mute_t csd_mic_mute;
280 csd_start_voice_t csd_start_voice;
281 csd_stop_voice_t csd_stop_voice;
282};
283
Eric Laurent150dbfe2013-02-27 14:31:02 -0800284/*
285 * NOTE: when multiple mutexes have to be acquired, always take the
286 * stream_in or stream_out mutex first, followed by the audio_device mutex.
287 */
288
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800289struct pcm_config pcm_config_deep_buffer = {
290 .channels = 2,
291 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
292 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
293 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
294 .format = PCM_FORMAT_S16_LE,
295 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700296 .stop_threshold = INT_MAX,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800297 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
298};
299
300struct pcm_config pcm_config_low_latency = {
301 .channels = 2,
302 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
303 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
304 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
305 .format = PCM_FORMAT_S16_LE,
306 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700307 .stop_threshold = INT_MAX,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800308 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
309};
310
311struct pcm_config pcm_config_hdmi_multi = {
312 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
313 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
314 .period_size = HDMI_MULTI_PERIOD_SIZE,
315 .period_count = HDMI_MULTI_PERIOD_COUNT,
316 .format = PCM_FORMAT_S16_LE,
317 .start_threshold = 0,
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700318 .stop_threshold = INT_MAX,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800319 .avail_min = 0,
320};
321
322struct pcm_config pcm_config_audio_capture = {
323 .channels = 2,
324 .period_size = AUDIO_CAPTURE_PERIOD_SIZE,
325 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
326 .format = PCM_FORMAT_S16_LE,
327};
328
329struct pcm_config pcm_config_voice_call = {
330 .channels = 1,
331 .rate = 8000,
332 .period_size = 160,
333 .period_count = 2,
334 .format = PCM_FORMAT_S16_LE,
335};
336