blob: 2b60b04f4488710ad01aea2c5fcc627d7e5347a3 [file] [log] [blame]
PIPIPIG23366613549c92020-02-14 22:39:33 -05001/*
Michael Bestas4e1ba2f2024-05-12 06:38:39 +03002 * SPDX-FileCopyrightText: 2015 The CyanogenMod Project
3 * SPDX-FileCopyrightText: 2020-2023 The LineageOS Project
4 * SPDX-License-Identifier: Apache-2.0
PIPIPIG23366613549c92020-02-14 22:39:33 -05005 */
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
26typedef struct amp_device {
27 amplifier_device_t amp_dev;
28 struct audio_device* adev;
29 struct audio_usecase* usecase_tx;
Rohit5cbecfa2023-03-20 15:19:04 +053030 struct pcm* aw882xx_out;
31} aw_t;
PIPIPIG23366613549c92020-02-14 22:39:33 -050032
Rohit5cbecfa2023-03-20 15:19:04 +053033static aw_t* aw_dev = NULL;
PIPIPIG23366613549c92020-02-14 22:39:33 -050034
35static 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 Bestas2965a902024-05-27 17:27:54 +030041 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:
PIPIPIG23366613549c92020-02-14 22:39:33 -050051 case SND_DEVICE_OUT_VOICE_SPEAKER:
52 case SND_DEVICE_OUT_VOICE_SPEAKER_2:
Michael Bestas2965a902024-05-27 17:27:54 +030053 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:
PIPIPIG23366613549c92020-02-14 22:39:33 -050062 speaker = 1;
63 break;
64 }
65
66 return speaker;
67}
68
Rohit5cbecfa2023-03-20 15:19:04 +053069int aw882xx_start_feedback(void* adev, uint32_t snd_device) {
70 aw_dev->adev = (struct audio_device*)adev;
PIPIPIG23366613549c92020-02-14 22:39:33 -050071 int pcm_dev_tx_id = 0, rc = 0;
Rohit5cbecfa2023-03-20 15:19:04 +053072 struct pcm_config pcm_config_aw882xx = {
PIPIPIG23366613549c92020-02-14 22:39:33 -050073 .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
Rohit5cbecfa2023-03-20 15:19:04 +053083 if (!aw_dev) {
PIPIPIG23366613549c92020-02-14 22:39:33 -050084 ALOGE("%d: Invalid params", __LINE__);
85 return -EINVAL;
86 }
87
Rohit5cbecfa2023-03-20 15:19:04 +053088 if (aw_dev->aw882xx_out || !is_speaker(snd_device)) return 0;
PIPIPIG23366613549c92020-02-14 22:39:33 -050089
Rohit5cbecfa2023-03-20 15:19:04 +053090 aw_dev->usecase_tx = (struct audio_usecase*)calloc(1, sizeof(struct audio_usecase));
91 if (!aw_dev->usecase_tx) {
PIPIPIG23366613549c92020-02-14 22:39:33 -050092 ALOGE("%d: failed to allocate usecase", __LINE__);
93 return -ENOMEM;
94 }
Rohit5cbecfa2023-03-20 15:19:04 +053095 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);
PIPIPIG23366613549c92020-02-14 22:39:33 -050099
Tommaso Fondaac2aaf22023-11-21 15:58:46 +0100100 list_add_head(&aw_dev->adev->usecase_list, &aw_dev->usecase_tx->list);
Rohit5cbecfa2023-03-20 15:19:04 +0530101 enable_snd_device(aw_dev->adev, aw_dev->usecase_tx->in_snd_device);
102 enable_audio_route(aw_dev->adev, aw_dev->usecase_tx);
PIPIPIG23366613549c92020-02-14 22:39:33 -0500103
Rohit5cbecfa2023-03-20 15:19:04 +0530104 pcm_dev_tx_id = platform_get_pcm_device_id(aw_dev->usecase_tx->id, aw_dev->usecase_tx->type);
PIPIPIG23366613549c92020-02-14 22:39:33 -0500105 ALOGD("pcm_dev_tx_id = %d", pcm_dev_tx_id);
106 if (pcm_dev_tx_id < 0) {
Rohit5cbecfa2023-03-20 15:19:04 +0530107 ALOGE("%d: Invalid pcm device for usecase (%d)", __LINE__, aw_dev->usecase_tx->id);
PIPIPIG23366613549c92020-02-14 22:39:33 -0500108 rc = -ENODEV;
109 goto error;
110 }
111
Rohit5cbecfa2023-03-20 15:19:04 +0530112 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));
PIPIPIG23366613549c92020-02-14 22:39:33 -0500116 rc = -EIO;
117 goto error;
118 }
119
Rohit5cbecfa2023-03-20 15:19:04 +0530120 rc = pcm_start(aw_dev->aw882xx_out);
PIPIPIG23366613549c92020-02-14 22:39:33 -0500121 if (rc < 0) {
122 ALOGE("%d: pcm start for TX failed", __LINE__);
123 rc = -EINVAL;
124 goto error;
125 }
126 return 0;
127
128error:
129 ALOGE("%s: error case", __func__);
Rohit5cbecfa2023-03-20 15:19:04 +0530130 if (aw_dev->aw882xx_out != 0) {
131 pcm_close(aw_dev->aw882xx_out);
132 aw_dev->aw882xx_out = NULL;
PIPIPIG23366613549c92020-02-14 22:39:33 -0500133 }
Rohit5cbecfa2023-03-20 15:19:04 +0530134 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);
PIPIPIG23366613549c92020-02-14 22:39:33 -0500138
139 return rc;
140}
141
Rohit5cbecfa2023-03-20 15:19:04 +0530142void aw882xx_stop_feedback(void* adev, uint32_t snd_device) {
143 aw_dev->adev = (struct audio_device*)adev;
144 if (!aw_dev) {
PIPIPIG23366613549c92020-02-14 22:39:33 -0500145 ALOGE("%s: Invalid params", __func__);
146 return;
147 }
148
149 if (!is_speaker(snd_device)) return;
150
Rohit5cbecfa2023-03-20 15:19:04 +0530151 if (aw_dev->aw882xx_out) {
152 pcm_close(aw_dev->aw882xx_out);
153 aw_dev->aw882xx_out = NULL;
PIPIPIG23366613549c92020-02-14 22:39:33 -0500154 }
155
Rohit5cbecfa2023-03-20 15:19:04 +0530156 disable_snd_device(aw_dev->adev, SND_DEVICE_IN_CAPTURE_VI_FEEDBACK);
PIPIPIG23366613549c92020-02-14 22:39:33 -0500157
Rohit5cbecfa2023-03-20 15:19:04 +0530158 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);
PIPIPIG23366613549c92020-02-14 22:39:33 -0500163 }
164 return;
165}
166
167static int amp_set_feedback(UNUSED amplifier_device_t* device, void* adev, uint32_t devices,
168 bool enable) {
Rohit5cbecfa2023-03-20 15:19:04 +0530169 aw_dev->adev = (struct audio_device*)adev;
PIPIPIG23366613549c92020-02-14 22:39:33 -0500170 if (enable) {
Rohit5cbecfa2023-03-20 15:19:04 +0530171 aw882xx_start_feedback(aw_dev->adev, devices);
PIPIPIG23366613549c92020-02-14 22:39:33 -0500172 } else {
Rohit5cbecfa2023-03-20 15:19:04 +0530173 aw882xx_stop_feedback(aw_dev->adev, devices);
PIPIPIG23366613549c92020-02-14 22:39:33 -0500174 }
175 return 0;
176}
177
178static int amp_dev_close(hw_device_t* device) {
Rohit5cbecfa2023-03-20 15:19:04 +0530179 aw_t* dev = (aw_t*)device;
PIPIPIG23366613549c92020-02-14 22:39:33 -0500180 if (dev) free(dev);
181
182 return 0;
183}
184
185static 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
Rohit5cbecfa2023-03-20 15:19:04 +0530192 aw_dev = calloc(1, sizeof(aw_t));
193 if (!aw_dev) {
PIPIPIG23366613549c92020-02-14 22:39:33 -0500194 ALOGE("%s:%d: Unable to allocate memory for amplifier device\n", __func__, __LINE__);
195 return -ENOMEM;
196 }
197
Rohit5cbecfa2023-03-20 15:19:04 +0530198 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;
PIPIPIG23366613549c92020-02-14 22:39:33 -0500202
Rohit5cbecfa2023-03-20 15:19:04 +0530203 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;
PIPIPIG23366613549c92020-02-14 22:39:33 -0500216
Rohit5cbecfa2023-03-20 15:19:04 +0530217 *device = (hw_device_t*)aw_dev;
PIPIPIG23366613549c92020-02-14 22:39:33 -0500218
219 return 0;
220}
221
222static struct hw_module_methods_t hal_module_methods = {
223 .open = amp_module_open,
224};
225
226/* clang-format off */
227amplifier_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,
Rohit5cbecfa2023-03-20 15:19:04 +0530233 .name = "AW882XX audio amplifier HAL",
PIPIPIG23366613549c92020-02-14 22:39:33 -0500234 .author = "The LineageOS Project",
235 .methods = &hal_module_methods,
236 },
237};