blob: 6282dd3c07f8f0604cfed8322e8d65611e356113 [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 {
35 SND_DEVICE_INVALID = -1,
36 SND_DEVICE_OUT_BEGIN = 0,
37 SND_DEVICE_OUT_HANDSET = SND_DEVICE_OUT_BEGIN,
38 SND_DEVICE_OUT_SPEAKER,
39 SND_DEVICE_OUT_HEADPHONES,
40 SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES,
41 SND_DEVICE_OUT_VOICE_SPEAKER,
42 SND_DEVICE_OUT_VOICE_HEADPHONES,
43 SND_DEVICE_OUT_HDMI ,
44 SND_DEVICE_OUT_SPEAKER_AND_HDMI,
45 SND_DEVICE_OUT_BT_SCO,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080046 SND_DEVICE_OUT_VOICE_HANDSET_TMUS,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080047 SND_DEVICE_OUT_END,
48
49 /* Note: IN_BEGIN should be same as OUT_END because total number of devices
50 * SND_DEVICES_ALL should not exceed MAX_RX + MAX_TX devices.
51 */
52 SND_DEVICE_IN_BEGIN = SND_DEVICE_OUT_END,
53 SND_DEVICE_IN_HANDSET_MIC = SND_DEVICE_IN_BEGIN,
54 SND_DEVICE_IN_SPEAKER_MIC,
55 SND_DEVICE_IN_HEADSET_MIC,
56 SND_DEVICE_IN_VOICE_SPEAKER_MIC,
57 SND_DEVICE_IN_VOICE_HEADSET_MIC,
58 SND_DEVICE_IN_HDMI_MIC,
59 SND_DEVICE_IN_BT_SCO_MIC ,
60 SND_DEVICE_IN_CAMCORDER_MIC,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080061 SND_DEVICE_IN_VOICE_DMIC_EF,
62 SND_DEVICE_IN_VOICE_DMIC_BS,
63 SND_DEVICE_IN_VOICE_DMIC_EF_TMUS,
64 SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF,
65 SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080066 SND_DEVICE_IN_VOICE_REC_MIC,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080067 SND_DEVICE_IN_VOICE_REC_DMIC_EF,
68 SND_DEVICE_IN_VOICE_REC_DMIC_BS,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080069 SND_DEVICE_IN_END,
70
71} snd_device_t;
72
73#define NUM_OUT_SND_DEVICES (SND_DEVICE_OUT_END - SND_DEVICE_OUT_BEGIN)
74#define NUM_IN_SND_DEVICES (SND_DEVICE_IN_END - SND_DEVICE_IN_BEGIN)
75#define SND_DEVICE_ALL (NUM_OUT_SND_DEVICES + NUM_IN_SND_DEVICES)
76#define SND_DEVICE_MAX MAX(NUM_IN_SND_DEVICES, NUM_IN_SND_DEVICES)
77
78/* These are the supported use cases by the hardware.
79 * Each usecase is mapped to a specific PCM device.
80 * Refer to pcm_device_table[].
81 */
82typedef enum {
83 USECASE_INVALID = -1,
84 /* Playback usecases */
85 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER = 0,
86 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
87 USECASE_AUDIO_PLAYBACK_MULTI_CH,
88
89 /* Capture usecases */
90 USECASE_AUDIO_RECORD,
91 USECASE_AUDIO_RECORD_LOW_LATENCY,
92
93 USECASE_VOICE_CALL,
94
95 AUDIO_USECASE_MAX
96} audio_usecase_t;
97
98enum tty_modes {
99 TTY_MODE_OFF,
100 TTY_MODE_VCO,
101 TTY_MODE_HCO,
102 TTY_MODE_FULL
103};
104
105#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
106
107#define SOUND_CARD 0
108
109#define DEFAULT_OUTPUT_SAMPLING_RATE 48000
110
111/*
112 * tinyAlsa library interprets period size as number of frames
113 * one frame = channel_count * sizeof (pcm sample)
114 * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
115 * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
116 * We should take care of returning proper size when AudioFlinger queries for
117 * the buffer size of an input/output stream
118 */
119#define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 1024
120#define DEEP_BUFFER_OUTPUT_PERIOD_COUNT 8
121
122#define LOW_LATENCY_OUTPUT_PERIOD_SIZE 256
123#define LOW_LATENCY_OUTPUT_PERIOD_COUNT 2
124
125#define HDMI_MULTI_PERIOD_SIZE 336
126#define HDMI_MULTI_PERIOD_COUNT 8
127#define HDMI_MULTI_DEFAULT_CHANNEL_COUNT 6
128#define HDMI_MULTI_PERIOD_BYTES (HDMI_MULTI_PERIOD_SIZE * HDMI_MULTI_DEFAULT_CHANNEL_COUNT * 2)
129
130#define AUDIO_CAPTURE_PERIOD_SIZE 320
131#define AUDIO_CAPTURE_PERIOD_COUNT 2
132
133#define MAX_SUPPORTED_CHANNEL_MASKS 2
134
135struct stream_out {
136 struct audio_stream_out stream;
137 pthread_mutex_t lock;
138 struct pcm_config config;
139 struct pcm *pcm;
140 int standby;
141 int pcm_device_id;
142 audio_channel_mask_t channel_mask;
143 audio_devices_t devices;
144 audio_output_flags_t flags;
145 audio_usecase_t usecase;
146 /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
147 audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
148
149 struct audio_device *dev;
150};
151
152struct stream_in {
153 struct audio_stream_in stream;
154 pthread_mutex_t lock;
155 struct pcm_config config;
156 struct pcm *pcm;
157 int standby;
158 int source;
159 int pcm_device_id;
160 int device;
161 audio_channel_mask_t channel_mask;
162 audio_usecase_t usecase;
163
164 struct audio_device *dev;
165};
166
167typedef enum {
168 PCM_PLAYBACK,
169 PCM_CAPTURE,
170 VOICE_CALL
171} usecase_type_t;
172
173// To store active use cases.
174struct audio_usecase {
175 audio_usecase_t id;
176 usecase_type_t type;
177 audio_devices_t devices;
178 struct audio_usecase *next;
179};
180
181typedef void (*acdb_deallocate_t)();
182typedef int (*acdb_init_t)();
183typedef void (*acdb_send_audio_cal_t)(int,int);
184typedef void (*acdb_send_voice_cal_t)(int,int);
185
186typedef int (*csd_client_init_t)();
187typedef int (*csd_client_deinit_t)();
188typedef int (*csd_disable_device_t)();
189typedef int (*csd_enable_device_t)(int, int, uint32_t);
190typedef int (*csd_volume_t)(int);
191typedef int (*csd_mic_mute_t)(int);
192typedef int (*csd_start_voice_t)();
193typedef int (*csd_stop_voice_t)();
194
195struct audio_device {
196 struct audio_hw_device device;
197 pthread_mutex_t lock;
198 struct mixer *mixer;
199 audio_mode_t mode;
200 audio_devices_t out_device;
201 audio_devices_t in_device;
202 audio_source_t input_source;
203 int in_call;
204 float voice_volume;
205 bool mic_mute;
206 int tty_mode;
207 bool bluetooth_nrec;
208 bool screen_off;
209 struct pcm *voice_call_rx;
210 struct pcm *voice_call_tx;
211 snd_device_t cur_out_snd_device;
212 snd_device_t cur_in_snd_device;
213 bool out_snd_device_active;
214 bool in_snd_device_active;
215 struct audio_usecase usecase_list;
216 struct audio_route *audio_route;
217 int acdb_settings;
218
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800219 bool is_tmus;
220 bool mic_type_analog;
221 bool fluence_in_voice_call;
222 bool fluence_in_voice_rec;
223 int dualmic_config;
224
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800225 /* Audio calibration related functions */
226 void *acdb_handle;
227 acdb_init_t acdb_init;
228 acdb_deallocate_t acdb_deallocate;
229 acdb_send_audio_cal_t acdb_send_audio_cal;
230 acdb_send_voice_cal_t acdb_send_voice_cal;
231
232 /* CSD Client related functions for voice call */
233 void *csd_client;
234 csd_client_init_t csd_client_init;
235 csd_client_deinit_t csd_client_deinit;
236 csd_disable_device_t csd_disable_device;
237 csd_enable_device_t csd_enable_device;
238 csd_volume_t csd_volume;
239 csd_mic_mute_t csd_mic_mute;
240 csd_start_voice_t csd_start_voice;
241 csd_stop_voice_t csd_stop_voice;
242};
243
244struct pcm_config pcm_config_deep_buffer = {
245 .channels = 2,
246 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
247 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
248 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
249 .format = PCM_FORMAT_S16_LE,
250 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
251 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
252};
253
254struct pcm_config pcm_config_low_latency = {
255 .channels = 2,
256 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
257 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
258 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
259 .format = PCM_FORMAT_S16_LE,
260 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
261 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
262};
263
264struct pcm_config pcm_config_hdmi_multi = {
265 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
266 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
267 .period_size = HDMI_MULTI_PERIOD_SIZE,
268 .period_count = HDMI_MULTI_PERIOD_COUNT,
269 .format = PCM_FORMAT_S16_LE,
270 .start_threshold = 0,
271 .avail_min = 0,
272};
273
274struct pcm_config pcm_config_audio_capture = {
275 .channels = 2,
276 .period_size = AUDIO_CAPTURE_PERIOD_SIZE,
277 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
278 .format = PCM_FORMAT_S16_LE,
279};
280
281struct pcm_config pcm_config_voice_call = {
282 .channels = 1,
283 .rate = 8000,
284 .period_size = 160,
285 .period_count = 2,
286 .format = PCM_FORMAT_S16_LE,
287};
288