PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 1 | /* |
Michael Bestas | 4e1ba2f | 2024-05-12 06:38:39 +0300 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: 2015 The CyanogenMod Project |
| 3 | * SPDX-FileCopyrightText: 2020-2023 The LineageOS Project |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #define LOG_TAG "audio_amplifier" |
| 8 | //#define LOG_NDEBUG 0 |
| 9 | |
| 10 | #include <cutils/str_parms.h> |
| 11 | #include <hardware/audio_amplifier.h> |
| 12 | #include <hardware/hardware.h> |
| 13 | #include <log/log.h> |
| 14 | #include <stdint.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <sys/types.h> |
| 17 | |
| 18 | /* clang-format off */ |
| 19 | #include "audio_hw.h" |
| 20 | #include "platform.h" |
| 21 | #include "platform_api.h" |
| 22 | /* clang-format on */ |
| 23 | |
| 24 | #define UNUSED __attribute__((unused)) |
| 25 | |
| 26 | typedef struct amp_device { |
| 27 | amplifier_device_t amp_dev; |
| 28 | struct audio_device* adev; |
| 29 | struct audio_usecase* usecase_tx; |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 30 | struct pcm* aw882xx_out; |
| 31 | } aw_t; |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 32 | |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 33 | static aw_t* aw_dev = NULL; |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 34 | |
| 35 | static int is_speaker(uint32_t snd_device) { |
| 36 | int speaker = 0; |
| 37 | switch (snd_device) { |
| 38 | case SND_DEVICE_OUT_SPEAKER: |
| 39 | case SND_DEVICE_OUT_SPEAKER_REVERSE: |
| 40 | case SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES: |
Michael Bestas | 2965a90 | 2024-05-27 17:27:54 +0300 | [diff] [blame] | 41 | case SND_DEVICE_OUT_SPEAKER_AND_LINE: |
| 42 | case SND_DEVICE_OUT_SPEAKER_AND_HDMI: |
| 43 | case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT: |
| 44 | case SND_DEVICE_OUT_SPEAKER_AND_ANC_HEADSET: |
| 45 | case SND_DEVICE_OUT_SPEAKER_AND_ANC_FB_HEADSET: |
| 46 | case SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET: |
| 47 | case SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP: |
| 48 | case SND_DEVICE_OUT_SPEAKER_AND_BT_SCO: |
| 49 | case SND_DEVICE_OUT_SPEAKER_AND_BT_SCO_WB: |
| 50 | case SND_DEVICE_OUT_SPEAKER_AND_BT_SCO_SWB: |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 51 | case SND_DEVICE_OUT_VOICE_SPEAKER: |
| 52 | case SND_DEVICE_OUT_VOICE_SPEAKER_2: |
Michael Bestas | 2965a90 | 2024-05-27 17:27:54 +0300 | [diff] [blame] | 53 | case SND_DEVICE_OUT_VOICE_SPEAKER_STEREO: |
| 54 | case SND_DEVICE_OUT_VOICE_SPEAKER_HFP: |
| 55 | case SND_DEVICE_OUT_VOICE_SPEAKER_AND_VOICE_HEADPHONES: |
| 56 | case SND_DEVICE_OUT_VOICE_SPEAKER_AND_VOICE_ANC_HEADSET: |
| 57 | case SND_DEVICE_OUT_VOICE_SPEAKER_AND_VOICE_ANC_FB_HEADSET: |
| 58 | case SND_DEVICE_OUT_VOICE_SPEAKER_STEREO_AND_VOICE_HEADPHONES: |
| 59 | case SND_DEVICE_OUT_VOICE_SPEAKER_STEREO_AND_VOICE_ANC_HEADSET: |
| 60 | case SND_DEVICE_OUT_VOICE_SPEAKER_STEREO_AND_VOICE_ANC_FB_HEADSET: |
| 61 | case SND_DEVICE_OUT_VOIP_SPEAKER: |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 62 | speaker = 1; |
| 63 | break; |
| 64 | } |
| 65 | |
| 66 | return speaker; |
| 67 | } |
| 68 | |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 69 | int aw882xx_start_feedback(void* adev, uint32_t snd_device) { |
| 70 | aw_dev->adev = (struct audio_device*)adev; |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 71 | int pcm_dev_tx_id = 0, rc = 0; |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 72 | struct pcm_config pcm_config_aw882xx = { |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 73 | .channels = 2, |
| 74 | .rate = 48000, |
| 75 | .period_size = 256, |
| 76 | .period_count = 4, |
| 77 | .format = PCM_FORMAT_S16_LE, |
| 78 | .start_threshold = 0, |
| 79 | .stop_threshold = INT_MAX, |
| 80 | .silence_threshold = 0, |
| 81 | }; |
| 82 | |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 83 | if (!aw_dev) { |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 84 | ALOGE("%d: Invalid params", __LINE__); |
| 85 | return -EINVAL; |
| 86 | } |
| 87 | |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 88 | if (aw_dev->aw882xx_out || !is_speaker(snd_device)) return 0; |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 89 | |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 90 | aw_dev->usecase_tx = (struct audio_usecase*)calloc(1, sizeof(struct audio_usecase)); |
| 91 | if (!aw_dev->usecase_tx) { |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 92 | ALOGE("%d: failed to allocate usecase", __LINE__); |
| 93 | return -ENOMEM; |
| 94 | } |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 95 | aw_dev->usecase_tx->id = USECASE_AUDIO_SPKR_CALIB_TX; |
| 96 | aw_dev->usecase_tx->type = PCM_CAPTURE; |
| 97 | aw_dev->usecase_tx->in_snd_device = SND_DEVICE_IN_CAPTURE_VI_FEEDBACK; |
| 98 | list_init(&aw_dev->usecase_tx->device_list); |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 99 | |
Tommaso Fonda | ac2aaf2 | 2023-11-21 15:58:46 +0100 | [diff] [blame] | 100 | list_add_head(&aw_dev->adev->usecase_list, &aw_dev->usecase_tx->list); |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 101 | enable_snd_device(aw_dev->adev, aw_dev->usecase_tx->in_snd_device); |
| 102 | enable_audio_route(aw_dev->adev, aw_dev->usecase_tx); |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 103 | |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 104 | pcm_dev_tx_id = platform_get_pcm_device_id(aw_dev->usecase_tx->id, aw_dev->usecase_tx->type); |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 105 | ALOGD("pcm_dev_tx_id = %d", pcm_dev_tx_id); |
| 106 | if (pcm_dev_tx_id < 0) { |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 107 | ALOGE("%d: Invalid pcm device for usecase (%d)", __LINE__, aw_dev->usecase_tx->id); |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 108 | rc = -ENODEV; |
| 109 | goto error; |
| 110 | } |
| 111 | |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 112 | aw_dev->aw882xx_out = |
| 113 | pcm_open(aw_dev->adev->snd_card, pcm_dev_tx_id, PCM_IN, &pcm_config_aw882xx); |
| 114 | if (!(aw_dev->aw882xx_out || pcm_is_ready(aw_dev->aw882xx_out))) { |
| 115 | ALOGE("%d: %s", __LINE__, pcm_get_error(aw_dev->aw882xx_out)); |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 116 | rc = -EIO; |
| 117 | goto error; |
| 118 | } |
| 119 | |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 120 | rc = pcm_start(aw_dev->aw882xx_out); |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 121 | if (rc < 0) { |
| 122 | ALOGE("%d: pcm start for TX failed", __LINE__); |
| 123 | rc = -EINVAL; |
| 124 | goto error; |
| 125 | } |
| 126 | return 0; |
| 127 | |
| 128 | error: |
| 129 | ALOGE("%s: error case", __func__); |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 130 | if (aw_dev->aw882xx_out != 0) { |
| 131 | pcm_close(aw_dev->aw882xx_out); |
| 132 | aw_dev->aw882xx_out = NULL; |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 133 | } |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 134 | list_remove(&aw_dev->usecase_tx->list); |
| 135 | disable_snd_device(aw_dev->adev, aw_dev->usecase_tx->in_snd_device); |
| 136 | disable_audio_route(aw_dev->adev, aw_dev->usecase_tx); |
| 137 | free(aw_dev->usecase_tx); |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 138 | |
| 139 | return rc; |
| 140 | } |
| 141 | |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 142 | void aw882xx_stop_feedback(void* adev, uint32_t snd_device) { |
| 143 | aw_dev->adev = (struct audio_device*)adev; |
| 144 | if (!aw_dev) { |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 145 | ALOGE("%s: Invalid params", __func__); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | if (!is_speaker(snd_device)) return; |
| 150 | |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 151 | if (aw_dev->aw882xx_out) { |
| 152 | pcm_close(aw_dev->aw882xx_out); |
| 153 | aw_dev->aw882xx_out = NULL; |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 154 | } |
| 155 | |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 156 | disable_snd_device(aw_dev->adev, SND_DEVICE_IN_CAPTURE_VI_FEEDBACK); |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 157 | |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 158 | aw_dev->usecase_tx = get_usecase_from_list(aw_dev->adev, USECASE_AUDIO_SPKR_CALIB_TX); |
| 159 | if (aw_dev->usecase_tx) { |
| 160 | list_remove(&aw_dev->usecase_tx->list); |
| 161 | disable_audio_route(aw_dev->adev, aw_dev->usecase_tx); |
| 162 | free(aw_dev->usecase_tx); |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 163 | } |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | static int amp_set_feedback(UNUSED amplifier_device_t* device, void* adev, uint32_t devices, |
| 168 | bool enable) { |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 169 | aw_dev->adev = (struct audio_device*)adev; |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 170 | if (enable) { |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 171 | aw882xx_start_feedback(aw_dev->adev, devices); |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 172 | } else { |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 173 | aw882xx_stop_feedback(aw_dev->adev, devices); |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static int amp_dev_close(hw_device_t* device) { |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 179 | aw_t* dev = (aw_t*)device; |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 180 | if (dev) free(dev); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int amp_module_open(const hw_module_t* module, const char* name, hw_device_t** device) { |
| 186 | if (strcmp(name, AMPLIFIER_HARDWARE_INTERFACE)) { |
| 187 | ALOGE("%s:%d: %s does not match amplifier hardware interface name\n", __func__, __LINE__, |
| 188 | name); |
| 189 | return -ENODEV; |
| 190 | } |
| 191 | |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 192 | aw_dev = calloc(1, sizeof(aw_t)); |
| 193 | if (!aw_dev) { |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 194 | ALOGE("%s:%d: Unable to allocate memory for amplifier device\n", __func__, __LINE__); |
| 195 | return -ENOMEM; |
| 196 | } |
| 197 | |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 198 | aw_dev->amp_dev.common.tag = HARDWARE_DEVICE_TAG; |
| 199 | aw_dev->amp_dev.common.module = (hw_module_t*)module; |
| 200 | aw_dev->amp_dev.common.version = HARDWARE_DEVICE_API_VERSION(1, 0); |
| 201 | aw_dev->amp_dev.common.close = amp_dev_close; |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 202 | |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 203 | aw_dev->amp_dev.set_input_devices = NULL; |
| 204 | aw_dev->amp_dev.set_output_devices = NULL; |
| 205 | aw_dev->amp_dev.enable_output_devices = NULL; |
| 206 | aw_dev->amp_dev.enable_input_devices = NULL; |
| 207 | aw_dev->amp_dev.set_mode = NULL; |
| 208 | aw_dev->amp_dev.output_stream_start = NULL; |
| 209 | aw_dev->amp_dev.input_stream_start = NULL; |
| 210 | aw_dev->amp_dev.output_stream_standby = NULL; |
| 211 | aw_dev->amp_dev.input_stream_standby = NULL; |
| 212 | aw_dev->amp_dev.set_parameters = NULL; |
| 213 | aw_dev->amp_dev.out_set_parameters = NULL; |
| 214 | aw_dev->amp_dev.in_set_parameters = NULL; |
| 215 | aw_dev->amp_dev.set_feedback = amp_set_feedback; |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 216 | |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 217 | *device = (hw_device_t*)aw_dev; |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static struct hw_module_methods_t hal_module_methods = { |
| 223 | .open = amp_module_open, |
| 224 | }; |
| 225 | |
| 226 | /* clang-format off */ |
| 227 | amplifier_module_t HAL_MODULE_INFO_SYM = { |
| 228 | .common = { |
| 229 | .tag = HARDWARE_MODULE_TAG, |
| 230 | .module_api_version = AMPLIFIER_MODULE_API_VERSION_0_1, |
| 231 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 232 | .id = AMPLIFIER_HARDWARE_MODULE_ID, |
Rohit | 5cbecfa | 2023-03-20 15:19:04 +0530 | [diff] [blame] | 233 | .name = "AW882XX audio amplifier HAL", |
PIPIPIG233666 | 13549c9 | 2020-02-14 22:39:33 -0500 | [diff] [blame] | 234 | .author = "The LineageOS Project", |
| 235 | .methods = &hal_module_methods, |
| 236 | }, |
| 237 | }; |