audio_hw.h revision f99670408844a07cdfabf9a01078ed7ef4c71bbf
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
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
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 */
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,
46    SND_DEVICE_OUT_VOICE_HANDSET_TMUS,
47    SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES,
48    SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES,
49    SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET,
50    SND_DEVICE_OUT_END,
51
52    /* Note: IN_BEGIN should be same as OUT_END because total number of devices
53     * SND_DEVICES_ALL should not exceed MAX_RX + MAX_TX devices.
54     */
55    SND_DEVICE_IN_BEGIN = SND_DEVICE_OUT_END,
56    SND_DEVICE_IN_HANDSET_MIC  = SND_DEVICE_IN_BEGIN,
57    SND_DEVICE_IN_SPEAKER_MIC,
58    SND_DEVICE_IN_HEADSET_MIC,
59    SND_DEVICE_IN_VOICE_SPEAKER_MIC,
60    SND_DEVICE_IN_VOICE_HEADSET_MIC,
61    SND_DEVICE_IN_HDMI_MIC,
62    SND_DEVICE_IN_BT_SCO_MIC ,
63    SND_DEVICE_IN_CAMCORDER_MIC,
64    SND_DEVICE_IN_VOICE_DMIC_EF,
65    SND_DEVICE_IN_VOICE_DMIC_BS,
66    SND_DEVICE_IN_VOICE_DMIC_EF_TMUS,
67    SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF,
68    SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS,
69    SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC,
70    SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC,
71    SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC,
72    SND_DEVICE_IN_VOICE_REC_MIC,
73    SND_DEVICE_IN_VOICE_REC_DMIC_EF,
74    SND_DEVICE_IN_VOICE_REC_DMIC_BS,
75    SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE,
76    SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE,
77    SND_DEVICE_IN_END,
78
79} snd_device_t;
80
81#define NUM_OUT_SND_DEVICES (SND_DEVICE_OUT_END - SND_DEVICE_OUT_BEGIN)
82#define NUM_IN_SND_DEVICES (SND_DEVICE_IN_END - SND_DEVICE_IN_BEGIN)
83#define SND_DEVICE_ALL     (NUM_OUT_SND_DEVICES + NUM_IN_SND_DEVICES)
84#define SND_DEVICE_MAX      MAX(NUM_IN_SND_DEVICES, NUM_IN_SND_DEVICES)
85
86/* These are the supported use cases by the hardware.
87 * Each usecase is mapped to a specific PCM device.
88 * Refer to pcm_device_table[].
89 */
90typedef enum {
91    USECASE_INVALID = -1,
92    /* Playback usecases */
93    USECASE_AUDIO_PLAYBACK_DEEP_BUFFER = 0,
94    USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
95    USECASE_AUDIO_PLAYBACK_MULTI_CH,
96
97    /* Capture usecases */
98    USECASE_AUDIO_RECORD,
99    USECASE_AUDIO_RECORD_LOW_LATENCY,
100
101    USECASE_VOICE_CALL,
102
103    AUDIO_USECASE_MAX
104} audio_usecase_t;
105
106#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
107
108#define SOUND_CARD 0
109
110#define DEFAULT_OUTPUT_SAMPLING_RATE 48000
111
112/*
113 * tinyAlsa library interprets period size as number of frames
114 * one frame = channel_count * sizeof (pcm sample)
115 * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
116 * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
117 * We should take care of returning proper size when AudioFlinger queries for
118 * the buffer size of an input/output stream
119 */
120#define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 1024
121#define DEEP_BUFFER_OUTPUT_PERIOD_COUNT 8
122
123#define LOW_LATENCY_OUTPUT_PERIOD_SIZE 256
124#define LOW_LATENCY_OUTPUT_PERIOD_COUNT 2
125
126#define HDMI_MULTI_PERIOD_SIZE  336
127#define HDMI_MULTI_PERIOD_COUNT 8
128#define HDMI_MULTI_DEFAULT_CHANNEL_COUNT 6
129#define HDMI_MULTI_PERIOD_BYTES (HDMI_MULTI_PERIOD_SIZE * HDMI_MULTI_DEFAULT_CHANNEL_COUNT * 2)
130
131#define AUDIO_CAPTURE_PERIOD_SIZE 320
132#define AUDIO_CAPTURE_PERIOD_COUNT 2
133
134#define MAX_SUPPORTED_CHANNEL_MASKS 2
135
136struct stream_out {
137    struct audio_stream_out stream;
138    pthread_mutex_t lock;
139    struct pcm_config config;
140    struct pcm *pcm;
141    int standby;
142    int pcm_device_id;
143    audio_channel_mask_t channel_mask;
144    audio_devices_t devices;
145    audio_output_flags_t flags;
146    audio_usecase_t usecase;
147    /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
148    audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
149
150    struct audio_device *dev;
151};
152
153struct stream_in {
154    struct audio_stream_in stream;
155    pthread_mutex_t lock;
156    struct pcm_config config;
157    struct pcm *pcm;
158    int standby;
159    int source;
160    int pcm_device_id;
161    int device;
162    audio_channel_mask_t channel_mask;
163    audio_usecase_t usecase;
164
165    struct audio_device *dev;
166};
167
168typedef enum {
169    PCM_PLAYBACK,
170    PCM_CAPTURE,
171    VOICE_CALL
172} usecase_type_t;
173
174// To store active use cases.
175struct audio_usecase {
176    audio_usecase_t id;
177    usecase_type_t  type;
178    audio_devices_t devices;
179    struct audio_usecase *next;
180};
181
182typedef void (*acdb_deallocate_t)();
183typedef int  (*acdb_init_t)();
184typedef void (*acdb_send_audio_cal_t)(int,int);
185typedef void (*acdb_send_voice_cal_t)(int,int);
186
187typedef int (*csd_client_init_t)();
188typedef int (*csd_client_deinit_t)();
189typedef int (*csd_disable_device_t)();
190typedef int (*csd_enable_device_t)(int, int, uint32_t);
191typedef int (*csd_volume_t)(int);
192typedef int (*csd_mic_mute_t)(int);
193typedef int (*csd_start_voice_t)();
194typedef int (*csd_stop_voice_t)();
195
196struct audio_device {
197    struct audio_hw_device device;
198    pthread_mutex_t lock;
199    struct mixer *mixer;
200    audio_mode_t mode;
201    audio_devices_t out_device;
202    struct stream_in *active_input;
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
219    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
225    /* 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
289