a7xelte: build.prop: Add vendor security patch level
[GitHub/LineageOS/android_device_samsung_a7xelte.git] / amplifier / tfa.c
1 /*
2 * Copyright (C) 2017 Christopher N. Hesse <raymanfx@gmail.com>
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 #define LOG_TAG "audio_hw_amplifier_tfa"
18 #define LOG_NDEBUG 0
19
20 #include <cutils/log.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <errno.h>
24 #include <dlfcn.h>
25 #include <fcntl.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28
29 #include <tinyalsa/asoundlib.h>
30
31 #include "tfa.h"
32
33 static void * write_dummy_data(void *param) {
34 tfa_device_t *t = (tfa_device_t *) param;
35 uint8_t *buffer;
36 int size;
37 struct pcm *pcm;
38 bool signaled = false;
39
40 struct pcm_config config = {
41 .channels = 2,
42 .rate = 48000,
43 .period_size = 256,
44 .period_count = 2,
45 .format = PCM_FORMAT_S16_LE,
46 .start_threshold = config.period_size * config.period_count - 1,
47 .stop_threshold = UINT_MAX,
48 .silence_threshold = 0,
49 .avail_min = 1,
50 };
51
52 pcm = pcm_open(0, 0, PCM_OUT | PCM_MONOTONIC, &config);
53 if (!pcm || !pcm_is_ready(pcm)) {
54 ALOGE("pcm_open failed: %s", pcm_get_error(pcm));
55 if (pcm && errno != EBUSY) {
56 goto err_close_pcm;
57 }
58 goto exit;
59 }
60
61 size = 1024 * 8;
62 buffer = calloc(1, size);
63 if (!buffer) {
64 ALOGE("%s: failed to allocate buffer", __func__);
65 goto err_close_pcm;
66 }
67
68 do {
69 if (pcm_write(pcm, buffer, size)) {
70 ALOGE("%s: pcm_write failed", __func__);
71 }
72 if (!signaled) {
73 pthread_mutex_lock(&t->mutex);
74 t->writing = true;
75 pthread_cond_signal(&t->cond);
76 pthread_mutex_unlock(&t->mutex);
77 signaled = true;
78 }
79 } while (t->initializing);
80
81 t->writing = false;
82
83 err_free:
84 free(buffer);
85 err_close_pcm:
86 pcm_close(pcm);
87 exit:
88 if (!signaled) {
89 pthread_mutex_lock(&t->mutex);
90 t->writing = true;
91 pthread_cond_signal(&t->cond);
92 pthread_mutex_unlock(&t->mutex);
93 }
94
95 return NULL;
96 }
97
98 static int tfa_clock_on(tfa_device_t *tfa_dev)
99 {
100 if (tfa_dev->clock_enabled) {
101 ALOGW("%s: clocks already on", __func__);
102 return -EBUSY;
103 }
104
105 tfa_dev->initializing = true;
106 pthread_create(&tfa_dev->write_thread, NULL, write_dummy_data, tfa_dev);
107 pthread_mutex_lock(&tfa_dev->mutex);
108 while (!tfa_dev->writing) {
109 pthread_cond_wait(&tfa_dev->cond, &tfa_dev->mutex);
110 }
111 pthread_mutex_unlock(&tfa_dev->mutex);
112 tfa_dev->clock_enabled = true;
113
114 ALOGI("%s: clocks enabled", __func__);
115
116 return 0;
117 }
118
119 static int tfa_clock_off(tfa_device_t *tfa_dev)
120 {
121 if (!tfa_dev->clock_enabled) {
122 ALOGW("%s: clocks already off", __func__);
123 return 0;
124 }
125
126 tfa_dev->initializing = false;
127 pthread_join(tfa_dev->write_thread, NULL);
128 tfa_dev->clock_enabled = false;
129
130 ALOGI("%s: clocks disabled", __func__);
131
132 return 0;
133 }
134
135 /*
136 * Loads the vendor amplifier library and grabs the needed functions.
137 *
138 * @param tfa_dev Device handle.
139 *
140 * @return 0 on success, <0 on error.
141 */
142 static int load_tfa_lib(tfa_device_t *tfa_dev) {
143 if (access(TFA_LIBRARY_PATH, R_OK) < 0) {
144 ALOGE("%s: amplifier library %s not found", __func__, TFA_LIBRARY_PATH);
145 return -errno;
146 }
147
148 tfa_dev->lib_handle = dlopen(TFA_LIBRARY_PATH, RTLD_NOW);
149 if (tfa_dev->lib_handle == NULL) {
150 ALOGE("%s: dlopen failed for %s (%s)", __func__, TFA_LIBRARY_PATH, dlerror());
151 return -1;
152 } else {
153 ALOGV("%s: dlopen successful for %s", __func__, TFA_LIBRARY_PATH);
154 }
155
156 tfa_dev->tfa_device_open = (tfa_device_open_t)dlsym(tfa_dev->lib_handle, "tfa_device_open");
157 if (tfa_dev->tfa_device_open == NULL) {
158 ALOGE("%s: dlsym error %s for tfa_device_open", __func__, dlerror());
159 tfa_dev->tfa_device_open = 0;
160 return -1;
161 }
162
163 tfa_dev->tfa_enable = (tfa_enable_t)dlsym(tfa_dev->lib_handle, "tfa_enable");
164 if (tfa_dev->tfa_enable == NULL) {
165 ALOGE("%s: dlsym error %s for tfa_enable", __func__, dlerror());
166 tfa_dev->tfa_enable = 0;
167 return -1;
168 }
169
170 return 0;
171 }
172
173 /*
174 * Hooks into the vendor library and enables/disables the amplifier IC.
175 *
176 * @param tfa_dev Device handle.
177 * @param on true or false for enabling/disabling of the IC.
178 *
179 * @return 0 on success, != 0 on error.
180 */
181 int tfa_power(tfa_device_t *tfa_dev, bool on) {
182 int rc = 0;
183
184 ALOGV("%s: %s amplifier device", __func__, on ? "Enabling" : "Disabling");
185 pthread_mutex_lock(&tfa_dev->tfa_lock);
186 if (on) {
187 if (tfa_dev->tfa_handle->a1 != 0) {
188 tfa_dev->tfa_handle->a1 = 0;
189 }
190 }
191
192 // this implementation requires explicit clock control
193 if (on) {
194 tfa_clock_on(tfa_dev);
195 }
196
197 rc = tfa_dev->tfa_enable(tfa_dev->tfa_handle, on ? 1 : 0);
198 if (rc) {
199 ALOGE("%s: Failed to %s amplifier device", __func__, on ? "enable" : "disable");
200 }
201
202 if (tfa_dev->clock_enabled) {
203 tfa_clock_off(tfa_dev);
204 }
205 pthread_mutex_unlock(&tfa_dev->tfa_lock);
206
207 return rc;
208 }
209
210 /*
211 * Initializes the amplifier device and local class data.
212 *
213 * @return tfa_device_t on success, NULL on error.
214 */
215 tfa_device_t * tfa_device_open() {
216 tfa_device_t *tfa_dev;
217 int rc;
218
219 ALOGV("Opening amplifier device");
220
221 tfa_dev = (tfa_device_t *) malloc(sizeof(tfa_device_t));
222 if (tfa_dev == NULL) {
223 ALOGE("%s: Not enough memory to load the lib handle", __func__);
224 return NULL;
225 }
226
227 // allocate memory for tfa handle
228 tfa_dev->tfa_handle = malloc(sizeof(tfa_handle_t));
229 if (tfa_dev->tfa_handle == NULL) {
230 ALOGE("%s: Not enough memory to load the tfa handle", __func__);
231 return NULL;
232 }
233
234 rc = load_tfa_lib(tfa_dev);
235 if (rc < 0) {
236 ALOGE("%s: Failed to load amplifier library", __func__);
237 return NULL;
238 }
239
240 rc = tfa_dev->tfa_device_open(tfa_dev->tfa_handle, 0);
241 if (rc < 0) {
242 ALOGE("%s: Failed to open amplifier device", __func__);
243 return NULL;
244 }
245
246 pthread_mutex_init(&tfa_dev->tfa_lock, (const pthread_mutexattr_t *) NULL);
247
248 rc = tfa_power(tfa_dev, false);
249 if (rc < 0) {
250 ALOGE("%s: Failed to do initial amplifier powerdown", __func__);
251 return NULL;
252 }
253
254 // do a full powerup - powerdown cycle and initialize clocks
255 tfa_dev->writing = false;
256 tfa_dev->clock_enabled = false;
257 pthread_mutex_init(&tfa_dev->mutex, NULL);
258 pthread_cond_init(&tfa_dev->cond, NULL);
259
260 rc = tfa_power(tfa_dev, true);
261 if (rc < 0) {
262 ALOGE("%s: Failed to do initial amplifier powerup", __func__);
263 return NULL;
264 } else {
265 rc = tfa_power(tfa_dev, false);
266 if (rc < 0) {
267 ALOGE("%s: Failed to do initial amplifier powerdown (2)", __func__);
268 return NULL;
269 }
270 }
271
272 return tfa_dev;
273 }
274
275 /*
276 * De-Initializes the amplifier device.
277 */
278 void tfa_device_close(tfa_device_t *tfa_dev) {
279 ALOGV("%s: Closing amplifier device", __func__);
280 tfa_power(tfa_dev, false);
281
282 pthread_mutex_destroy(&tfa_dev->tfa_lock);
283
284 if (tfa_dev->tfa_handle) {
285 free(tfa_dev->tfa_handle);
286 }
287
288 if (tfa_dev->lib_handle) {
289 dlclose(tfa_dev->lib_handle);
290 }
291
292 if (tfa_dev) {
293 free(tfa_dev);
294 }
295 }