blob: dec6ef206829a6e9eeccdc21e625d0072438e0c1 [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>
18
19#include <tinyalsa/asoundlib.h>
20
21#include <audio_route/audio_route.h>
22
23#define ACDB_DEV_TYPE_OUT 1
24#define ACDB_DEV_TYPE_IN 2
25
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080026#define DUALMIC_CONFIG_NONE 0 /* Target does not contain 2 mics */
27#define DUALMIC_CONFIG_ENDFIRE 1
28#define DUALMIC_CONFIG_BROADSIDE 2
29
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -070030/*
31 * Below are the devices for which is back end is same, SLIMBUS_0_RX.
32 * All these devices are handled by the internal HW codec. We can
33 * enable any one of these devices at any time
34 */
35#define AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND \
36 (AUDIO_DEVICE_OUT_EARPIECE | AUDIO_DEVICE_OUT_SPEAKER | \
37 AUDIO_DEVICE_OUT_WIRED_HEADSET | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)
38
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080039/* Sound devices specific to the platform
40 * The DEVICE_OUT_* and DEVICE_IN_* should be mapped to these sound
41 * devices to enable corresponding mixer paths
42 */
43typedef enum {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080044 SND_DEVICE_NONE = 0,
45
46 /* Playback devices */
47 SND_DEVICE_MIN,
48 SND_DEVICE_OUT_BEGIN = SND_DEVICE_MIN,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080049 SND_DEVICE_OUT_HANDSET = SND_DEVICE_OUT_BEGIN,
50 SND_DEVICE_OUT_SPEAKER,
51 SND_DEVICE_OUT_HEADPHONES,
52 SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES,
53 SND_DEVICE_OUT_VOICE_SPEAKER,
54 SND_DEVICE_OUT_VOICE_HEADPHONES,
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080055 SND_DEVICE_OUT_HDMI,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080056 SND_DEVICE_OUT_SPEAKER_AND_HDMI,
57 SND_DEVICE_OUT_BT_SCO,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080058 SND_DEVICE_OUT_VOICE_HANDSET_TMUS,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080059 SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES,
60 SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES,
61 SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080062 SND_DEVICE_OUT_END,
63
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080064 /*
65 * Note: IN_BEGIN should be same as OUT_END because total number of devices
66 * SND_DEVICES_MAX should not exceed MAX_RX + MAX_TX devices.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080067 */
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080068 /* Capture devices */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080069 SND_DEVICE_IN_BEGIN = SND_DEVICE_OUT_END,
70 SND_DEVICE_IN_HANDSET_MIC = SND_DEVICE_IN_BEGIN,
71 SND_DEVICE_IN_SPEAKER_MIC,
72 SND_DEVICE_IN_HEADSET_MIC,
73 SND_DEVICE_IN_VOICE_SPEAKER_MIC,
74 SND_DEVICE_IN_VOICE_HEADSET_MIC,
75 SND_DEVICE_IN_HDMI_MIC,
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080076 SND_DEVICE_IN_BT_SCO_MIC,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080077 SND_DEVICE_IN_CAMCORDER_MIC,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080078 SND_DEVICE_IN_VOICE_DMIC_EF,
79 SND_DEVICE_IN_VOICE_DMIC_BS,
80 SND_DEVICE_IN_VOICE_DMIC_EF_TMUS,
81 SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF,
82 SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080083 SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC,
84 SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC,
85 SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080086 SND_DEVICE_IN_VOICE_REC_MIC,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080087 SND_DEVICE_IN_VOICE_REC_DMIC_EF,
88 SND_DEVICE_IN_VOICE_REC_DMIC_BS,
Eric Laurentc8400632013-02-14 19:04:54 -080089 SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE,
90 SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080091 SND_DEVICE_IN_END,
92
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080093 SND_DEVICE_MAX = SND_DEVICE_IN_END,
94
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080095} snd_device_t;
96
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080097
98/* These are the supported use cases by the hardware.
99 * Each usecase is mapped to a specific PCM device.
100 * Refer to pcm_device_table[].
101 */
102typedef enum {
103 USECASE_INVALID = -1,
104 /* Playback usecases */
105 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER = 0,
106 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
107 USECASE_AUDIO_PLAYBACK_MULTI_CH,
108
109 /* Capture usecases */
110 USECASE_AUDIO_RECORD,
111 USECASE_AUDIO_RECORD_LOW_LATENCY,
112
113 USECASE_VOICE_CALL,
114
115 AUDIO_USECASE_MAX
116} audio_usecase_t;
117
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800118#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
119
120#define SOUND_CARD 0
121
122#define DEFAULT_OUTPUT_SAMPLING_RATE 48000
123
124/*
125 * tinyAlsa library interprets period size as number of frames
126 * one frame = channel_count * sizeof (pcm sample)
127 * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
128 * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
129 * We should take care of returning proper size when AudioFlinger queries for
130 * the buffer size of an input/output stream
131 */
Sungmin Choi5195a4b2013-04-03 21:54:22 -0700132#ifdef MSM8974
133#define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 1024
134#else
Ravi Kumar Alamandac3011862013-03-19 16:46:58 -0700135#define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 960
Sungmin Choi5195a4b2013-04-03 21:54:22 -0700136#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800137#define DEEP_BUFFER_OUTPUT_PERIOD_COUNT 8
138
Sungmin Choi5195a4b2013-04-03 21:54:22 -0700139#ifdef MSM8974
140#define LOW_LATENCY_OUTPUT_PERIOD_SIZE 256
141#else
Ravi Kumar Alamandac3011862013-03-19 16:46:58 -0700142#define LOW_LATENCY_OUTPUT_PERIOD_SIZE 240
Sungmin Choi5195a4b2013-04-03 21:54:22 -0700143#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800144#define LOW_LATENCY_OUTPUT_PERIOD_COUNT 2
145
146#define HDMI_MULTI_PERIOD_SIZE 336
147#define HDMI_MULTI_PERIOD_COUNT 8
148#define HDMI_MULTI_DEFAULT_CHANNEL_COUNT 6
149#define HDMI_MULTI_PERIOD_BYTES (HDMI_MULTI_PERIOD_SIZE * HDMI_MULTI_DEFAULT_CHANNEL_COUNT * 2)
150
Sungmin Choi6336b0d2013-04-11 13:04:38 -0700151#ifdef MSM8974
152#define AUDIO_CAPTURE_PERIOD_SIZE 512
153#define AUDIO_CAPTURE_PERIOD_COUNT 16
154#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800155#define AUDIO_CAPTURE_PERIOD_SIZE 320
156#define AUDIO_CAPTURE_PERIOD_COUNT 2
Sungmin Choi6336b0d2013-04-11 13:04:38 -0700157#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800158
159#define MAX_SUPPORTED_CHANNEL_MASKS 2
160
161struct stream_out {
162 struct audio_stream_out stream;
Eric Laurent150dbfe2013-02-27 14:31:02 -0800163 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800164 struct pcm_config config;
165 struct pcm *pcm;
166 int standby;
167 int pcm_device_id;
168 audio_channel_mask_t channel_mask;
169 audio_devices_t devices;
170 audio_output_flags_t flags;
171 audio_usecase_t usecase;
172 /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
173 audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
Eric Laurenta9024de2013-04-04 09:19:12 -0700174 bool muted;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800175
176 struct audio_device *dev;
177};
178
179struct stream_in {
180 struct audio_stream_in stream;
Eric Laurent150dbfe2013-02-27 14:31:02 -0800181 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800182 struct pcm_config config;
183 struct pcm *pcm;
184 int standby;
185 int source;
186 int pcm_device_id;
187 int device;
188 audio_channel_mask_t channel_mask;
189 audio_usecase_t usecase;
190
191 struct audio_device *dev;
192};
193
194typedef enum {
195 PCM_PLAYBACK,
196 PCM_CAPTURE,
197 VOICE_CALL
198} usecase_type_t;
199
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800200union stream_ptr {
201 struct stream_in *in;
202 struct stream_out *out;
203};
204
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800205struct audio_usecase {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800206 struct listnode list;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800207 audio_usecase_t id;
208 usecase_type_t type;
209 audio_devices_t devices;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700210 snd_device_t out_snd_device;
211 snd_device_t in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800212 union stream_ptr stream;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800213};
214
215typedef void (*acdb_deallocate_t)();
216typedef int (*acdb_init_t)();
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800217typedef void (*acdb_send_audio_cal_t)(int, int);
218typedef void (*acdb_send_voice_cal_t)(int, int);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800219
220typedef int (*csd_client_init_t)();
221typedef int (*csd_client_deinit_t)();
222typedef int (*csd_disable_device_t)();
223typedef int (*csd_enable_device_t)(int, int, uint32_t);
224typedef int (*csd_volume_t)(int);
225typedef int (*csd_mic_mute_t)(int);
226typedef int (*csd_start_voice_t)();
227typedef int (*csd_stop_voice_t)();
228
229struct audio_device {
230 struct audio_hw_device device;
Eric Laurent150dbfe2013-02-27 14:31:02 -0800231 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800232 struct mixer *mixer;
233 audio_mode_t mode;
234 audio_devices_t out_device;
Eric Laurentc8400632013-02-14 19:04:54 -0800235 struct stream_in *active_input;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800236 struct stream_out *primary_output;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800237 int in_call;
238 float voice_volume;
239 bool mic_mute;
240 int tty_mode;
241 bool bluetooth_nrec;
242 bool screen_off;
243 struct pcm *voice_call_rx;
244 struct pcm *voice_call_tx;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700245 int snd_dev_ref_cnt[SND_DEVICE_MAX];
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800246 struct listnode usecase_list;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800247 struct audio_route *audio_route;
248 int acdb_settings;
249
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800250 bool mic_type_analog;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800251 bool fluence_in_spkr_mode;
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800252 bool fluence_in_voice_call;
253 bool fluence_in_voice_rec;
254 int dualmic_config;
255
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800256 /* Audio calibration related functions */
257 void *acdb_handle;
258 acdb_init_t acdb_init;
259 acdb_deallocate_t acdb_deallocate;
260 acdb_send_audio_cal_t acdb_send_audio_cal;
261 acdb_send_voice_cal_t acdb_send_voice_cal;
262
263 /* CSD Client related functions for voice call */
264 void *csd_client;
265 csd_client_init_t csd_client_init;
266 csd_client_deinit_t csd_client_deinit;
267 csd_disable_device_t csd_disable_device;
268 csd_enable_device_t csd_enable_device;
269 csd_volume_t csd_volume;
270 csd_mic_mute_t csd_mic_mute;
271 csd_start_voice_t csd_start_voice;
272 csd_stop_voice_t csd_stop_voice;
273};
274
Eric Laurent150dbfe2013-02-27 14:31:02 -0800275/*
276 * NOTE: when multiple mutexes have to be acquired, always take the
277 * stream_in or stream_out mutex first, followed by the audio_device mutex.
278 */
279
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800280struct pcm_config pcm_config_deep_buffer = {
281 .channels = 2,
282 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
283 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
284 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
285 .format = PCM_FORMAT_S16_LE,
286 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700287 .stop_threshold = INT_MAX,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800288 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
289};
290
291struct pcm_config pcm_config_low_latency = {
292 .channels = 2,
293 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
294 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
295 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
296 .format = PCM_FORMAT_S16_LE,
297 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700298 .stop_threshold = INT_MAX,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800299 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
300};
301
302struct pcm_config pcm_config_hdmi_multi = {
303 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
304 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
305 .period_size = HDMI_MULTI_PERIOD_SIZE,
306 .period_count = HDMI_MULTI_PERIOD_COUNT,
307 .format = PCM_FORMAT_S16_LE,
308 .start_threshold = 0,
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700309 .stop_threshold = INT_MAX,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800310 .avail_min = 0,
311};
312
313struct pcm_config pcm_config_audio_capture = {
314 .channels = 2,
315 .period_size = AUDIO_CAPTURE_PERIOD_SIZE,
316 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
317 .format = PCM_FORMAT_S16_LE,
318};
319
320struct pcm_config pcm_config_voice_call = {
321 .channels = 1,
322 .rate = 8000,
323 .period_size = 160,
324 .period_count = 2,
325 .format = PCM_FORMAT_S16_LE,
326};
327