blob: 0c4f5263d80016b41e629e83464d3cde21e6896e [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 Alamanda2dfba2b2013-01-17 16:50:22 -080030/* Sound devices specific to the platform
31 * The DEVICE_OUT_* and DEVICE_IN_* should be mapped to these sound
32 * devices to enable corresponding mixer paths
33 */
34typedef enum {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080035 SND_DEVICE_NONE = 0,
36
37 /* Playback devices */
38 SND_DEVICE_MIN,
39 SND_DEVICE_OUT_BEGIN = SND_DEVICE_MIN,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080040 SND_DEVICE_OUT_HANDSET = SND_DEVICE_OUT_BEGIN,
41 SND_DEVICE_OUT_SPEAKER,
42 SND_DEVICE_OUT_HEADPHONES,
43 SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES,
44 SND_DEVICE_OUT_VOICE_SPEAKER,
45 SND_DEVICE_OUT_VOICE_HEADPHONES,
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080046 SND_DEVICE_OUT_HDMI,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080047 SND_DEVICE_OUT_SPEAKER_AND_HDMI,
48 SND_DEVICE_OUT_BT_SCO,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080049 SND_DEVICE_OUT_VOICE_HANDSET_TMUS,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080050 SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES,
51 SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES,
52 SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080053 SND_DEVICE_OUT_END,
54
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080055 /*
56 * Note: IN_BEGIN should be same as OUT_END because total number of devices
57 * SND_DEVICES_MAX should not exceed MAX_RX + MAX_TX devices.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080058 */
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080059 /* Capture devices */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080060 SND_DEVICE_IN_BEGIN = SND_DEVICE_OUT_END,
61 SND_DEVICE_IN_HANDSET_MIC = SND_DEVICE_IN_BEGIN,
62 SND_DEVICE_IN_SPEAKER_MIC,
63 SND_DEVICE_IN_HEADSET_MIC,
64 SND_DEVICE_IN_VOICE_SPEAKER_MIC,
65 SND_DEVICE_IN_VOICE_HEADSET_MIC,
66 SND_DEVICE_IN_HDMI_MIC,
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080067 SND_DEVICE_IN_BT_SCO_MIC,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080068 SND_DEVICE_IN_CAMCORDER_MIC,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080069 SND_DEVICE_IN_VOICE_DMIC_EF,
70 SND_DEVICE_IN_VOICE_DMIC_BS,
71 SND_DEVICE_IN_VOICE_DMIC_EF_TMUS,
72 SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF,
73 SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080074 SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC,
75 SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC,
76 SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080077 SND_DEVICE_IN_VOICE_REC_MIC,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080078 SND_DEVICE_IN_VOICE_REC_DMIC_EF,
79 SND_DEVICE_IN_VOICE_REC_DMIC_BS,
Eric Laurentc8400632013-02-14 19:04:54 -080080 SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE,
81 SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080082 SND_DEVICE_IN_END,
83
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080084 SND_DEVICE_MAX = SND_DEVICE_IN_END,
85
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080086} snd_device_t;
87
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080088
89/* These are the supported use cases by the hardware.
90 * Each usecase is mapped to a specific PCM device.
91 * Refer to pcm_device_table[].
92 */
93typedef enum {
94 USECASE_INVALID = -1,
95 /* Playback usecases */
96 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER = 0,
97 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
98 USECASE_AUDIO_PLAYBACK_MULTI_CH,
99
100 /* Capture usecases */
101 USECASE_AUDIO_RECORD,
102 USECASE_AUDIO_RECORD_LOW_LATENCY,
103
104 USECASE_VOICE_CALL,
105
106 AUDIO_USECASE_MAX
107} audio_usecase_t;
108
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800109#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
110
111#define SOUND_CARD 0
112
113#define DEFAULT_OUTPUT_SAMPLING_RATE 48000
114
115/*
116 * tinyAlsa library interprets period size as number of frames
117 * one frame = channel_count * sizeof (pcm sample)
118 * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
119 * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
120 * We should take care of returning proper size when AudioFlinger queries for
121 * the buffer size of an input/output stream
122 */
123#define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 1024
124#define DEEP_BUFFER_OUTPUT_PERIOD_COUNT 8
125
126#define LOW_LATENCY_OUTPUT_PERIOD_SIZE 256
127#define LOW_LATENCY_OUTPUT_PERIOD_COUNT 2
128
129#define HDMI_MULTI_PERIOD_SIZE 336
130#define HDMI_MULTI_PERIOD_COUNT 8
131#define HDMI_MULTI_DEFAULT_CHANNEL_COUNT 6
132#define HDMI_MULTI_PERIOD_BYTES (HDMI_MULTI_PERIOD_SIZE * HDMI_MULTI_DEFAULT_CHANNEL_COUNT * 2)
133
134#define AUDIO_CAPTURE_PERIOD_SIZE 320
135#define AUDIO_CAPTURE_PERIOD_COUNT 2
136
137#define MAX_SUPPORTED_CHANNEL_MASKS 2
138
139struct stream_out {
140 struct audio_stream_out stream;
Eric Laurent150dbfe2013-02-27 14:31:02 -0800141 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800142 struct pcm_config config;
143 struct pcm *pcm;
144 int standby;
145 int pcm_device_id;
146 audio_channel_mask_t channel_mask;
147 audio_devices_t devices;
148 audio_output_flags_t flags;
149 audio_usecase_t usecase;
150 /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
151 audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
152
153 struct audio_device *dev;
154};
155
156struct stream_in {
157 struct audio_stream_in stream;
Eric Laurent150dbfe2013-02-27 14:31:02 -0800158 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800159 struct pcm_config config;
160 struct pcm *pcm;
161 int standby;
162 int source;
163 int pcm_device_id;
164 int device;
165 audio_channel_mask_t channel_mask;
166 audio_usecase_t usecase;
167
168 struct audio_device *dev;
169};
170
171typedef enum {
172 PCM_PLAYBACK,
173 PCM_CAPTURE,
174 VOICE_CALL
175} usecase_type_t;
176
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800177union stream_ptr {
178 struct stream_in *in;
179 struct stream_out *out;
180};
181
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800182struct audio_usecase {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800183 struct listnode list;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800184 audio_usecase_t id;
185 usecase_type_t type;
186 audio_devices_t devices;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800187 union stream_ptr stream;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800188};
189
190typedef void (*acdb_deallocate_t)();
191typedef int (*acdb_init_t)();
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800192typedef void (*acdb_send_audio_cal_t)(int, int);
193typedef void (*acdb_send_voice_cal_t)(int, int);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800194
195typedef int (*csd_client_init_t)();
196typedef int (*csd_client_deinit_t)();
197typedef int (*csd_disable_device_t)();
198typedef int (*csd_enable_device_t)(int, int, uint32_t);
199typedef int (*csd_volume_t)(int);
200typedef int (*csd_mic_mute_t)(int);
201typedef int (*csd_start_voice_t)();
202typedef int (*csd_stop_voice_t)();
203
204struct audio_device {
205 struct audio_hw_device device;
Eric Laurent150dbfe2013-02-27 14:31:02 -0800206 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800207 struct mixer *mixer;
208 audio_mode_t mode;
209 audio_devices_t out_device;
Eric Laurentc8400632013-02-14 19:04:54 -0800210 struct stream_in *active_input;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800211 struct stream_out *primary_output;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800212 int in_call;
213 float voice_volume;
214 bool mic_mute;
215 int tty_mode;
216 bool bluetooth_nrec;
217 bool screen_off;
218 struct pcm *voice_call_rx;
219 struct pcm *voice_call_tx;
220 snd_device_t cur_out_snd_device;
221 snd_device_t cur_in_snd_device;
222 bool out_snd_device_active;
223 bool in_snd_device_active;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800224 struct listnode usecase_list;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800225 struct audio_route *audio_route;
226 int acdb_settings;
227
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800228 bool mic_type_analog;
229 bool fluence_in_voice_call;
230 bool fluence_in_voice_rec;
231 int dualmic_config;
232
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800233 /* Audio calibration related functions */
234 void *acdb_handle;
235 acdb_init_t acdb_init;
236 acdb_deallocate_t acdb_deallocate;
237 acdb_send_audio_cal_t acdb_send_audio_cal;
238 acdb_send_voice_cal_t acdb_send_voice_cal;
239
240 /* CSD Client related functions for voice call */
241 void *csd_client;
242 csd_client_init_t csd_client_init;
243 csd_client_deinit_t csd_client_deinit;
244 csd_disable_device_t csd_disable_device;
245 csd_enable_device_t csd_enable_device;
246 csd_volume_t csd_volume;
247 csd_mic_mute_t csd_mic_mute;
248 csd_start_voice_t csd_start_voice;
249 csd_stop_voice_t csd_stop_voice;
250};
251
Eric Laurent150dbfe2013-02-27 14:31:02 -0800252/*
253 * NOTE: when multiple mutexes have to be acquired, always take the
254 * stream_in or stream_out mutex first, followed by the audio_device mutex.
255 */
256
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800257struct pcm_config pcm_config_deep_buffer = {
258 .channels = 2,
259 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
260 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
261 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
262 .format = PCM_FORMAT_S16_LE,
263 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
264 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
265};
266
267struct pcm_config pcm_config_low_latency = {
268 .channels = 2,
269 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
270 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
271 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
272 .format = PCM_FORMAT_S16_LE,
273 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
274 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
275};
276
277struct pcm_config pcm_config_hdmi_multi = {
278 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
279 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
280 .period_size = HDMI_MULTI_PERIOD_SIZE,
281 .period_count = HDMI_MULTI_PERIOD_COUNT,
282 .format = PCM_FORMAT_S16_LE,
283 .start_threshold = 0,
284 .avail_min = 0,
285};
286
287struct pcm_config pcm_config_audio_capture = {
288 .channels = 2,
289 .period_size = AUDIO_CAPTURE_PERIOD_SIZE,
290 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
291 .format = PCM_FORMAT_S16_LE,
292};
293
294struct pcm_config pcm_config_voice_call = {
295 .channels = 1,
296 .rate = 8000,
297 .period_size = 160,
298 .period_count = 2,
299 .format = PCM_FORMAT_S16_LE,
300};
301