Home | History | Annotate | Download | only in hal
      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 #include <hardware/audio_effect.h>
     19 #include <audio_effects/effect_aec.h>
     20 #include <audio_effects/effect_ns.h>
     21 
     22 #include <tinyalsa/asoundlib.h>
     23 
     24 #include <audio_route/audio_route.h>
     25 
     26 #define ACDB_DEV_TYPE_OUT 1
     27 #define ACDB_DEV_TYPE_IN 2
     28 
     29 #define DUALMIC_CONFIG_NONE 0      /* Target does not contain 2 mics */
     30 #define DUALMIC_CONFIG_ENDFIRE 1
     31 #define DUALMIC_CONFIG_BROADSIDE 2
     32 
     33 /*
     34  * Below are the devices for which is back end is same, SLIMBUS_0_RX.
     35  * All these devices are handled by the internal HW codec. We can
     36  * enable any one of these devices at any time
     37  */
     38 #define AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND \
     39     (AUDIO_DEVICE_OUT_EARPIECE | AUDIO_DEVICE_OUT_SPEAKER | \
     40      AUDIO_DEVICE_OUT_WIRED_HEADSET | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)
     41 
     42 /* Sound devices specific to the platform
     43  * The DEVICE_OUT_* and DEVICE_IN_* should be mapped to these sound
     44  * devices to enable corresponding mixer paths
     45  */
     46 typedef enum {
     47     SND_DEVICE_NONE = 0,
     48 
     49     /* Playback devices */
     50     SND_DEVICE_MIN,
     51     SND_DEVICE_OUT_BEGIN = SND_DEVICE_MIN,
     52     SND_DEVICE_OUT_HANDSET = SND_DEVICE_OUT_BEGIN,
     53     SND_DEVICE_OUT_SPEAKER,
     54     SND_DEVICE_OUT_SPEAKER_REVERSE,
     55     SND_DEVICE_OUT_HEADPHONES,
     56     SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES,
     57     SND_DEVICE_OUT_VOICE_SPEAKER,
     58     SND_DEVICE_OUT_VOICE_HEADPHONES,
     59     SND_DEVICE_OUT_HDMI,
     60     SND_DEVICE_OUT_SPEAKER_AND_HDMI,
     61     SND_DEVICE_OUT_BT_SCO,
     62     SND_DEVICE_OUT_VOICE_HANDSET_TMUS,
     63     SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES,
     64     SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES,
     65     SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET,
     66     SND_DEVICE_OUT_END,
     67 
     68     /*
     69      * Note: IN_BEGIN should be same as OUT_END because total number of devices
     70      * SND_DEVICES_MAX should not exceed MAX_RX + MAX_TX devices.
     71      */
     72     /* Capture devices */
     73     SND_DEVICE_IN_BEGIN = SND_DEVICE_OUT_END,
     74     SND_DEVICE_IN_HANDSET_MIC  = SND_DEVICE_IN_BEGIN,
     75     SND_DEVICE_IN_SPEAKER_MIC,
     76     SND_DEVICE_IN_HEADSET_MIC,
     77     SND_DEVICE_IN_HANDSET_MIC_AEC,
     78     SND_DEVICE_IN_SPEAKER_MIC_AEC,
     79     SND_DEVICE_IN_HEADSET_MIC_AEC,
     80     SND_DEVICE_IN_VOICE_SPEAKER_MIC,
     81     SND_DEVICE_IN_VOICE_HEADSET_MIC,
     82     SND_DEVICE_IN_HDMI_MIC,
     83     SND_DEVICE_IN_BT_SCO_MIC,
     84     SND_DEVICE_IN_CAMCORDER_MIC,
     85     SND_DEVICE_IN_VOICE_DMIC_EF,
     86     SND_DEVICE_IN_VOICE_DMIC_BS,
     87     SND_DEVICE_IN_VOICE_DMIC_EF_TMUS,
     88     SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF,
     89     SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS,
     90     SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC,
     91     SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC,
     92     SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC,
     93     SND_DEVICE_IN_VOICE_REC_MIC,
     94     SND_DEVICE_IN_VOICE_REC_DMIC_EF,
     95     SND_DEVICE_IN_VOICE_REC_DMIC_BS,
     96     SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE,
     97     SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE,
     98     SND_DEVICE_IN_END,
     99 
    100     SND_DEVICE_MAX = SND_DEVICE_IN_END,
    101 
    102 } snd_device_t;
    103 
    104 
    105 /* These are the supported use cases by the hardware.
    106  * Each usecase is mapped to a specific PCM device.
    107  * Refer to pcm_device_table[].
    108  */
    109 typedef enum {
    110     USECASE_INVALID = -1,
    111     /* Playback usecases */
    112     USECASE_AUDIO_PLAYBACK_DEEP_BUFFER = 0,
    113     USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
    114     USECASE_AUDIO_PLAYBACK_MULTI_CH,
    115 
    116     /* Capture usecases */
    117     USECASE_AUDIO_RECORD,
    118     USECASE_AUDIO_RECORD_LOW_LATENCY,
    119 
    120     USECASE_VOICE_CALL,
    121 
    122     AUDIO_USECASE_MAX
    123 } audio_usecase_t;
    124 
    125 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
    126 
    127 #define SOUND_CARD 0
    128 
    129 #define DEFAULT_OUTPUT_SAMPLING_RATE 48000
    130 
    131 /*
    132  * tinyAlsa library interprets period size as number of frames
    133  * one frame = channel_count * sizeof (pcm sample)
    134  * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
    135  * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
    136  * We should take care of returning proper size when AudioFlinger queries for
    137  * the buffer size of an input/output stream
    138  */
    139 #ifdef MSM8974
    140 #define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 1024
    141 #else
    142 #define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 960
    143 #endif
    144 #define DEEP_BUFFER_OUTPUT_PERIOD_COUNT 8
    145 
    146 #ifdef MSM8974
    147 #define LOW_LATENCY_OUTPUT_PERIOD_SIZE 256
    148 #else
    149 #define LOW_LATENCY_OUTPUT_PERIOD_SIZE 240
    150 #endif
    151 #define LOW_LATENCY_OUTPUT_PERIOD_COUNT 2
    152 
    153 #define HDMI_MULTI_PERIOD_SIZE  336
    154 #define HDMI_MULTI_PERIOD_COUNT 8
    155 #define HDMI_MULTI_DEFAULT_CHANNEL_COUNT 6
    156 #define HDMI_MULTI_PERIOD_BYTES (HDMI_MULTI_PERIOD_SIZE * HDMI_MULTI_DEFAULT_CHANNEL_COUNT * 2)
    157 
    158 #ifdef MSM8974
    159 #define AUDIO_CAPTURE_PERIOD_SIZE 512
    160 #define AUDIO_CAPTURE_PERIOD_COUNT 16
    161 #else
    162 #define AUDIO_CAPTURE_PERIOD_SIZE 320
    163 #define AUDIO_CAPTURE_PERIOD_COUNT 2
    164 #endif
    165 
    166 #define MAX_SUPPORTED_CHANNEL_MASKS 2
    167 
    168 struct stream_out {
    169     struct audio_stream_out stream;
    170     pthread_mutex_t lock; /* see note below on mutex acquisition order */
    171     struct pcm_config config;
    172     struct pcm *pcm;
    173     int standby;
    174     int pcm_device_id;
    175     audio_channel_mask_t channel_mask;
    176     audio_devices_t devices;
    177     audio_output_flags_t flags;
    178     audio_usecase_t usecase;
    179     /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
    180     audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
    181     bool muted;
    182 
    183     struct audio_device *dev;
    184 };
    185 
    186 struct stream_in {
    187     struct audio_stream_in stream;
    188     pthread_mutex_t lock; /* see note below on mutex acquisition order */
    189     struct pcm_config config;
    190     struct pcm *pcm;
    191     int standby;
    192     int source;
    193     int pcm_device_id;
    194     int device;
    195     audio_channel_mask_t channel_mask;
    196     audio_usecase_t usecase;
    197     bool enable_aec;
    198 
    199     struct audio_device *dev;
    200 };
    201 
    202 typedef enum {
    203     PCM_PLAYBACK,
    204     PCM_CAPTURE,
    205     VOICE_CALL
    206 } usecase_type_t;
    207 
    208 union stream_ptr {
    209     struct stream_in *in;
    210     struct stream_out *out;
    211 };
    212 
    213 struct audio_usecase {
    214     struct listnode list;
    215     audio_usecase_t id;
    216     usecase_type_t  type;
    217     audio_devices_t devices;
    218     snd_device_t out_snd_device;
    219     snd_device_t in_snd_device;
    220     union stream_ptr stream;
    221 };
    222 
    223 typedef void (*acdb_deallocate_t)();
    224 typedef int  (*acdb_init_t)();
    225 typedef void (*acdb_send_audio_cal_t)(int, int);
    226 typedef void (*acdb_send_voice_cal_t)(int, int);
    227 
    228 typedef int (*csd_client_init_t)();
    229 typedef int (*csd_client_deinit_t)();
    230 typedef int (*csd_disable_device_t)();
    231 typedef int (*csd_enable_device_t)(int, int, uint32_t);
    232 typedef int (*csd_volume_t)(int);
    233 typedef int (*csd_mic_mute_t)(int);
    234 typedef int (*csd_start_voice_t)();
    235 typedef int (*csd_stop_voice_t)();
    236 
    237 struct audio_device {
    238     struct audio_hw_device device;
    239     pthread_mutex_t lock; /* see note below on mutex acquisition order */
    240     struct mixer *mixer;
    241     audio_mode_t mode;
    242     audio_devices_t out_device;
    243     struct stream_in *active_input;
    244     struct stream_out *primary_output;
    245     int in_call;
    246     float voice_volume;
    247     bool mic_mute;
    248     int tty_mode;
    249     bool bluetooth_nrec;
    250     bool screen_off;
    251     struct pcm *voice_call_rx;
    252     struct pcm *voice_call_tx;
    253     int snd_dev_ref_cnt[SND_DEVICE_MAX];
    254     struct listnode usecase_list;
    255     struct audio_route *audio_route;
    256     int acdb_settings;
    257     bool speaker_lr_swap;
    258 
    259     bool mic_type_analog;
    260     bool fluence_in_spkr_mode;
    261     bool fluence_in_voice_call;
    262     bool fluence_in_voice_rec;
    263     int  dualmic_config;
    264 
    265     /* Audio calibration related functions */
    266     void *acdb_handle;
    267     acdb_init_t acdb_init;
    268     acdb_deallocate_t acdb_deallocate;
    269     acdb_send_audio_cal_t acdb_send_audio_cal;
    270     acdb_send_voice_cal_t acdb_send_voice_cal;
    271 
    272     /* CSD Client related functions for voice call */
    273     void *csd_client;
    274     csd_client_init_t csd_client_init;
    275     csd_client_deinit_t csd_client_deinit;
    276     csd_disable_device_t csd_disable_device;
    277     csd_enable_device_t csd_enable_device;
    278     csd_volume_t csd_volume;
    279     csd_mic_mute_t csd_mic_mute;
    280     csd_start_voice_t csd_start_voice;
    281     csd_stop_voice_t csd_stop_voice;
    282 };
    283 
    284 /*
    285  * NOTE: when multiple mutexes have to be acquired, always take the
    286  * stream_in or stream_out mutex first, followed by the audio_device mutex.
    287  */
    288 
    289 struct pcm_config pcm_config_deep_buffer = {
    290     .channels = 2,
    291     .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
    292     .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
    293     .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
    294     .format = PCM_FORMAT_S16_LE,
    295     .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
    296     .stop_threshold = INT_MAX,
    297     .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
    298 };
    299 
    300 struct pcm_config pcm_config_low_latency = {
    301     .channels = 2,
    302     .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
    303     .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
    304     .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
    305     .format = PCM_FORMAT_S16_LE,
    306     .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
    307     .stop_threshold = INT_MAX,
    308     .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
    309 };
    310 
    311 struct pcm_config pcm_config_hdmi_multi = {
    312     .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
    313     .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
    314     .period_size = HDMI_MULTI_PERIOD_SIZE,
    315     .period_count = HDMI_MULTI_PERIOD_COUNT,
    316     .format = PCM_FORMAT_S16_LE,
    317     .start_threshold = 0,
    318     .stop_threshold = INT_MAX,
    319     .avail_min = 0,
    320 };
    321 
    322 struct pcm_config pcm_config_audio_capture = {
    323     .channels = 2,
    324     .period_size = AUDIO_CAPTURE_PERIOD_SIZE,
    325     .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
    326     .format = PCM_FORMAT_S16_LE,
    327 };
    328 
    329 struct pcm_config pcm_config_voice_call = {
    330     .channels = 1,
    331     .rate = 8000,
    332     .period_size = 160,
    333     .period_count = 2,
    334     .format = PCM_FORMAT_S16_LE,
    335 };
    336 
    337