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