blob: 5c326e9a1bc95da1c096fdeaa1d4106c5dadd6f8 [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,
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -070057 SND_DEVICE_OUT_VOICE_HANDSET,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080058 SND_DEVICE_OUT_VOICE_SPEAKER,
59 SND_DEVICE_OUT_VOICE_HEADPHONES,
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080060 SND_DEVICE_OUT_HDMI,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080061 SND_DEVICE_OUT_SPEAKER_AND_HDMI,
62 SND_DEVICE_OUT_BT_SCO,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080063 SND_DEVICE_OUT_VOICE_HANDSET_TMUS,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080064 SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES,
65 SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES,
66 SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080067 SND_DEVICE_OUT_END,
68
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080069 /*
70 * Note: IN_BEGIN should be same as OUT_END because total number of devices
71 * SND_DEVICES_MAX should not exceed MAX_RX + MAX_TX devices.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080072 */
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080073 /* Capture devices */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080074 SND_DEVICE_IN_BEGIN = SND_DEVICE_OUT_END,
75 SND_DEVICE_IN_HANDSET_MIC = SND_DEVICE_IN_BEGIN,
76 SND_DEVICE_IN_SPEAKER_MIC,
77 SND_DEVICE_IN_HEADSET_MIC,
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -070078 SND_DEVICE_IN_HANDSET_MIC_AEC,
79 SND_DEVICE_IN_SPEAKER_MIC_AEC,
80 SND_DEVICE_IN_HEADSET_MIC_AEC,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080081 SND_DEVICE_IN_VOICE_SPEAKER_MIC,
82 SND_DEVICE_IN_VOICE_HEADSET_MIC,
83 SND_DEVICE_IN_HDMI_MIC,
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080084 SND_DEVICE_IN_BT_SCO_MIC,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080085 SND_DEVICE_IN_CAMCORDER_MIC,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080086 SND_DEVICE_IN_VOICE_DMIC_EF,
87 SND_DEVICE_IN_VOICE_DMIC_BS,
88 SND_DEVICE_IN_VOICE_DMIC_EF_TMUS,
89 SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF,
90 SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080091 SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC,
92 SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC,
93 SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080094 SND_DEVICE_IN_VOICE_REC_MIC,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080095 SND_DEVICE_IN_VOICE_REC_DMIC_EF,
96 SND_DEVICE_IN_VOICE_REC_DMIC_BS,
Eric Laurentc8400632013-02-14 19:04:54 -080097 SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE,
98 SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080099 SND_DEVICE_IN_END,
100
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800101 SND_DEVICE_MAX = SND_DEVICE_IN_END,
102
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800103} snd_device_t;
104
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800105
106/* These are the supported use cases by the hardware.
107 * Each usecase is mapped to a specific PCM device.
108 * Refer to pcm_device_table[].
109 */
110typedef enum {
111 USECASE_INVALID = -1,
112 /* Playback usecases */
113 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER = 0,
114 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
115 USECASE_AUDIO_PLAYBACK_MULTI_CH,
116
117 /* Capture usecases */
118 USECASE_AUDIO_RECORD,
119 USECASE_AUDIO_RECORD_LOW_LATENCY,
120
121 USECASE_VOICE_CALL,
122
123 AUDIO_USECASE_MAX
124} audio_usecase_t;
125
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800126#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
127
128#define SOUND_CARD 0
129
130#define DEFAULT_OUTPUT_SAMPLING_RATE 48000
131
132/*
133 * tinyAlsa library interprets period size as number of frames
134 * one frame = channel_count * sizeof (pcm sample)
135 * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
136 * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
137 * We should take care of returning proper size when AudioFlinger queries for
138 * the buffer size of an input/output stream
139 */
Sungmin Choi5195a4b2013-04-03 21:54:22 -0700140#ifdef MSM8974
141#define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 1024
142#else
Ravi Kumar Alamandac3011862013-03-19 16:46:58 -0700143#define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 960
Sungmin Choi5195a4b2013-04-03 21:54:22 -0700144#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800145#define DEEP_BUFFER_OUTPUT_PERIOD_COUNT 8
146
Sungmin Choi5195a4b2013-04-03 21:54:22 -0700147#ifdef MSM8974
148#define LOW_LATENCY_OUTPUT_PERIOD_SIZE 256
149#else
Ravi Kumar Alamandac3011862013-03-19 16:46:58 -0700150#define LOW_LATENCY_OUTPUT_PERIOD_SIZE 240
Sungmin Choi5195a4b2013-04-03 21:54:22 -0700151#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800152#define LOW_LATENCY_OUTPUT_PERIOD_COUNT 2
153
154#define HDMI_MULTI_PERIOD_SIZE 336
155#define HDMI_MULTI_PERIOD_COUNT 8
156#define HDMI_MULTI_DEFAULT_CHANNEL_COUNT 6
157#define HDMI_MULTI_PERIOD_BYTES (HDMI_MULTI_PERIOD_SIZE * HDMI_MULTI_DEFAULT_CHANNEL_COUNT * 2)
158
Sungmin Choi6336b0d2013-04-11 13:04:38 -0700159#ifdef MSM8974
160#define AUDIO_CAPTURE_PERIOD_SIZE 512
161#define AUDIO_CAPTURE_PERIOD_COUNT 16
162#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800163#define AUDIO_CAPTURE_PERIOD_SIZE 320
164#define AUDIO_CAPTURE_PERIOD_COUNT 2
Sungmin Choi6336b0d2013-04-11 13:04:38 -0700165#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800166
167#define MAX_SUPPORTED_CHANNEL_MASKS 2
168
169struct stream_out {
170 struct audio_stream_out stream;
Eric Laurent150dbfe2013-02-27 14:31:02 -0800171 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800172 struct pcm_config config;
173 struct pcm *pcm;
174 int standby;
175 int pcm_device_id;
176 audio_channel_mask_t channel_mask;
177 audio_devices_t devices;
178 audio_output_flags_t flags;
179 audio_usecase_t usecase;
180 /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
181 audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
Eric Laurenta9024de2013-04-04 09:19:12 -0700182 bool muted;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800183
184 struct audio_device *dev;
185};
186
187struct stream_in {
188 struct audio_stream_in stream;
Eric Laurent150dbfe2013-02-27 14:31:02 -0800189 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800190 struct pcm_config config;
191 struct pcm *pcm;
192 int standby;
193 int source;
194 int pcm_device_id;
195 int device;
196 audio_channel_mask_t channel_mask;
197 audio_usecase_t usecase;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700198 bool enable_aec;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800199
200 struct audio_device *dev;
201};
202
203typedef enum {
204 PCM_PLAYBACK,
205 PCM_CAPTURE,
206 VOICE_CALL
207} usecase_type_t;
208
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800209union stream_ptr {
210 struct stream_in *in;
211 struct stream_out *out;
212};
213
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800214struct audio_usecase {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800215 struct listnode list;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800216 audio_usecase_t id;
217 usecase_type_t type;
218 audio_devices_t devices;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700219 snd_device_t out_snd_device;
220 snd_device_t in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800221 union stream_ptr stream;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800222};
223
224typedef void (*acdb_deallocate_t)();
225typedef int (*acdb_init_t)();
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800226typedef void (*acdb_send_audio_cal_t)(int, int);
227typedef void (*acdb_send_voice_cal_t)(int, int);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800228
229typedef int (*csd_client_init_t)();
230typedef int (*csd_client_deinit_t)();
231typedef int (*csd_disable_device_t)();
232typedef int (*csd_enable_device_t)(int, int, uint32_t);
233typedef int (*csd_volume_t)(int);
234typedef int (*csd_mic_mute_t)(int);
235typedef int (*csd_start_voice_t)();
236typedef int (*csd_stop_voice_t)();
237
238struct audio_device {
239 struct audio_hw_device device;
Eric Laurent150dbfe2013-02-27 14:31:02 -0800240 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800241 struct mixer *mixer;
242 audio_mode_t mode;
243 audio_devices_t out_device;
Eric Laurentc8400632013-02-14 19:04:54 -0800244 struct stream_in *active_input;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800245 struct stream_out *primary_output;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800246 int in_call;
247 float voice_volume;
248 bool mic_mute;
249 int tty_mode;
250 bool bluetooth_nrec;
251 bool screen_off;
252 struct pcm *voice_call_rx;
253 struct pcm *voice_call_tx;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700254 int snd_dev_ref_cnt[SND_DEVICE_MAX];
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800255 struct listnode usecase_list;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800256 struct audio_route *audio_route;
257 int acdb_settings;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -0700258 bool speaker_lr_swap;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800259
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800260 bool mic_type_analog;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800261 bool fluence_in_spkr_mode;
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800262 bool fluence_in_voice_call;
263 bool fluence_in_voice_rec;
264 int dualmic_config;
265
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800266 /* Audio calibration related functions */
267 void *acdb_handle;
268 acdb_init_t acdb_init;
269 acdb_deallocate_t acdb_deallocate;
270 acdb_send_audio_cal_t acdb_send_audio_cal;
271 acdb_send_voice_cal_t acdb_send_voice_cal;
272
273 /* CSD Client related functions for voice call */
274 void *csd_client;
275 csd_client_init_t csd_client_init;
276 csd_client_deinit_t csd_client_deinit;
277 csd_disable_device_t csd_disable_device;
278 csd_enable_device_t csd_enable_device;
279 csd_volume_t csd_volume;
280 csd_mic_mute_t csd_mic_mute;
281 csd_start_voice_t csd_start_voice;
282 csd_stop_voice_t csd_stop_voice;
283};
284
Eric Laurent150dbfe2013-02-27 14:31:02 -0800285/*
286 * NOTE: when multiple mutexes have to be acquired, always take the
287 * stream_in or stream_out mutex first, followed by the audio_device mutex.
288 */
289
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800290struct pcm_config pcm_config_deep_buffer = {
291 .channels = 2,
292 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
293 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
294 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
295 .format = PCM_FORMAT_S16_LE,
296 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700297 .stop_threshold = INT_MAX,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800298 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
299};
300
301struct pcm_config pcm_config_low_latency = {
302 .channels = 2,
303 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
304 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
305 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
306 .format = PCM_FORMAT_S16_LE,
307 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700308 .stop_threshold = INT_MAX,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800309 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
310};
311
312struct pcm_config pcm_config_hdmi_multi = {
313 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
314 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
315 .period_size = HDMI_MULTI_PERIOD_SIZE,
316 .period_count = HDMI_MULTI_PERIOD_COUNT,
317 .format = PCM_FORMAT_S16_LE,
318 .start_threshold = 0,
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700319 .stop_threshold = INT_MAX,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800320 .avail_min = 0,
321};
322
323struct pcm_config pcm_config_audio_capture = {
324 .channels = 2,
325 .period_size = AUDIO_CAPTURE_PERIOD_SIZE,
326 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
327 .format = PCM_FORMAT_S16_LE,
328};
329
330struct pcm_config pcm_config_voice_call = {
331 .channels = 1,
332 .rate = 8000,
333 .period_size = 160,
334 .period_count = 2,
335 .format = PCM_FORMAT_S16_LE,
336};
337