blob: ffafe07e8c6227fb9027cd668d443cf444f84f7c [file] [log] [blame]
wei.wang1e45cfd32023-07-10 08:33:46 +00001/*
2 * Copyright (C) 2023 Amlogic Corporation.
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#define LOG_TAG "AML_Audio_Setting"
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <stdint.h>
21#include <string.h>
22#include <unistd.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <ctype.h>
26#include <poll.h>
27#include <sys/ioctl.h>
28#include <pthread.h>
29#include <linux/ioctl.h>
30#define __force
31#define __bitwise
32#define __user
33#include <sound/asound.h>
34#include "AML_Audio_Setting.h"
35#include <cutils/log.h>
36
37#ifndef SNDRV_CTL_ELEM_ID_NAME_MAXLEN
38#define SNDRV_CTL_ELEM_ID_NAME_MAXLEN 44
39#endif
40#define DEFAULT_AML_SOUND_CARD 0
41/* TLV header size*/
42#define TLV_HEADER_SIZE (2 * sizeof(unsigned int))
43
44#define TDMB_GAIN "TDMOUT_B Software Gain"
wei.wang158743372023-08-07 08:07:05 +000045#define TDMA_GAIN "TDMOUT_A Software Gain"
46#define AML_CHIP_ID "AML chip id"
47#define AML_CHIP_ID_S1A 69
wei.wang1e45cfd32023-07-10 08:33:46 +000048#define HDMI_OUT_MUTE "Audio hdmi-out mute"
49#define DAC_DIGITAL_VOLUME "DAC Digital Playback Volume"
haiyang.renfdc144e2023-09-01 02:55:50 +000050#define DIGITAL_MODE "Audio Digital Mode"
haiyang.ren936bfc52023-09-18 05:46:24 +000051#define DRC_CONTROL "Audio DRC Control"
yuliang.huf4792d52023-09-19 10:14:14 +000052#define HDMI_CON "Audio Output Select"
wei.wang1e45cfd32023-07-10 08:33:46 +000053#define DAC_DIGITAl_DEFAULT_VOLUME (251)
54#define HEADPHONE_DAC_CHANNEL_NUM (2)
55
haiyang.ren936bfc52023-09-18 05:46:24 +000056#define DRC_MODE_LINE 2
57#define DRC_MODE_RF 3
58#define DRC_MODE_BIT 0
59#define DRC_HIGH_CUT_BIT 3
60#define DRC_LOW_BST_BIT 16
61
wei.wang1e45cfd32023-07-10 08:33:46 +000062extern "C" {
63
64struct mixer_ctl {
65 struct mixer *mixer;
66 struct snd_ctl_elem_info *info;
67 char **ename;
68 bool info_retrieved;
69};
70
71struct mixer {
72 int fd;
73 struct snd_ctl_card_info card_info;
74 struct snd_ctl_elem_info *elem_info;
75 struct mixer_ctl *ctl;
76 unsigned int count;
77};
78
79/* Mixer control types */
80enum mixer_ctl_type {
81 MIXER_CTL_TYPE_BOOL,
82 MIXER_CTL_TYPE_INT,
83 MIXER_CTL_TYPE_ENUM,
84 MIXER_CTL_TYPE_BYTE,
85 MIXER_CTL_TYPE_IEC958,
86 MIXER_CTL_TYPE_INT64,
87 MIXER_CTL_TYPE_UNKNOWN,
88
89 MIXER_CTL_TYPE_MAX,
90};
91
92static pthread_mutex_t g_volume_lock = PTHREAD_MUTEX_INITIALIZER;
93static pthread_mutex_t g_mute_lock = PTHREAD_MUTEX_INITIALIZER;
haiyang.ren49ae2c82023-11-02 09:31:41 +000094static int reserved_volume = 30;
wei.wang158743372023-08-07 08:07:05 +000095static int chip_id = 0;
wei.wang1e45cfd32023-07-10 08:33:46 +000096
97static void _mixer_close(struct mixer *mixer)
98{
99 unsigned int n,m;
100
101 if (!mixer)
102 return;
103
104 if (mixer->fd >= 0)
105 close(mixer->fd);
106
107 if (mixer->ctl) {
108 for (n = 0; n < mixer->count; n++) {
109 if (mixer->ctl[n].ename) {
110 unsigned int max = mixer->ctl[n].info->value.enumerated.items;
111 for (m = 0; m < max; m++)
112 free(mixer->ctl[n].ename[m]);
113 free(mixer->ctl[n].ename);
114 }
115 }
116 free(mixer->ctl);
117 }
118
119 if (mixer->elem_info)
120 free(mixer->elem_info);
121
122 free(mixer);
123
124 /* TODO: verify frees */
125}
126
127static struct mixer *_mixer_open(unsigned int card)
128{
129 struct snd_ctl_elem_list elist;
130 struct snd_ctl_elem_id *eid = NULL;
131 struct mixer *mixer = NULL;
132 unsigned int n;
133 int fd;
134 char fn[256];
135
136 snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card);
137 fd = open(fn, O_RDWR);
138 if (fd < 0)
139 return 0;
140
141 memset(&elist, 0, sizeof(elist));
142 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
143 goto fail;
144
145 mixer = (struct mixer *)calloc(1, sizeof(*mixer));
146 if (!mixer)
147 goto fail;
148
149 mixer->ctl = (struct mixer_ctl *)calloc(elist.count, sizeof(struct mixer_ctl));
150 mixer->elem_info = (struct snd_ctl_elem_info *)calloc(elist.count, sizeof(struct snd_ctl_elem_info));
151 if (!mixer->ctl || !mixer->elem_info)
152 goto fail;
153
154 if (ioctl(fd, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info) < 0)
155 goto fail;
156
157 eid = (struct snd_ctl_elem_id *)calloc(elist.count, sizeof(struct snd_ctl_elem_id));
158 if (!eid)
159 goto fail;
160
161 mixer->count = elist.count;
162 mixer->fd = fd;
163 elist.space = mixer->count;
164 elist.pids = eid;
165 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
166 goto fail;
167
168 for (n = 0; n < mixer->count; n++) {
169 struct mixer_ctl *ctl = mixer->ctl + n;
170
171 ctl->mixer = mixer;
172 ctl->info = mixer->elem_info + n;
173 ctl->info->id.numid = eid[n].numid;
174 strncpy((char *)ctl->info->id.name, (char *)eid[n].name,
175 SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
176 ctl->info->id.name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN - 1] = 0;
177 }
178
179 free(eid);
180 return mixer;
181
182fail:
183 /* TODO: verify frees in failure case */
184 if (eid)
185 free(eid);
186 if (mixer)
187 _mixer_close(mixer);
188 else if (fd >= 0)
189 close(fd);
190 return 0;
191}
192
193static bool _mixer_ctl_get_elem_info(struct mixer_ctl* ctl)
194{
195 if (!ctl->info_retrieved) {
196 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info) < 0)
197 return false;
198 ctl->info_retrieved = true;
199 }
200
201 if (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED || ctl->ename)
202 return true;
203
204 struct snd_ctl_elem_info tmp;
205 char** enames = (char**)calloc(ctl->info->value.enumerated.items, sizeof(char*));
206 if (!enames)
207 return false;
208
209 for (unsigned int i = 0; i < ctl->info->value.enumerated.items; i++) {
210 memset(&tmp, 0, sizeof(tmp));
211 tmp.id.numid = ctl->info->id.numid;
212 tmp.value.enumerated.item = i;
213 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
214 goto fail;
215 enames[i] = strdup(tmp.value.enumerated.name);
216 if (!enames[i])
217 goto fail;
218 }
219 ctl->ename = enames;
220 return true;
221
222fail:
223 free(enames);
224 return false;
225}
226
227static const char *_mixer_get_name(struct mixer *mixer)
228{
229 return (const char *)mixer->card_info.name;
230}
231
232static unsigned int _mixer_get_num_ctls(struct mixer *mixer)
233{
234 if (!mixer)
235 return 0;
236
237 return mixer->count;
238}
239
240static struct mixer_ctl *_mixer_get_ctl(struct mixer *mixer, unsigned int id)
241{
242 struct mixer_ctl *ctl;
243
244 if (!mixer || (id >= mixer->count))
245 return NULL;
246
247 ctl = mixer->ctl + id;
248 if (!_mixer_ctl_get_elem_info(ctl))
249 return NULL;
250
251 return ctl;
252}
253
254static struct mixer_ctl *_mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
255{
256 unsigned int n;
257
258 if (!mixer)
259 return NULL;
260
261 for (n = 0; n < mixer->count; n++)
262 if (!strcmp(name, (char*) mixer->elem_info[n].id.name))
263 return _mixer_get_ctl(mixer, n);
264
265 return NULL;
266}
267
268static void _mixer_ctl_update(struct mixer_ctl *ctl)
269{
270 ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
271}
272
273static const char *_mixer_ctl_get_name(struct mixer_ctl *ctl)
274{
275 if (!ctl)
276 return NULL;
277
278 return (const char *)ctl->info->id.name;
279}
280
281static enum mixer_ctl_type _mixer_ctl_get_type(struct mixer_ctl *ctl)
282{
283 if (!ctl)
284 return MIXER_CTL_TYPE_UNKNOWN;
285
286 switch (ctl->info->type) {
287 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
288 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
289 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
290 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
291 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
292 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
293 default: return MIXER_CTL_TYPE_UNKNOWN;
294 };
295}
296
297static const char *_mixer_ctl_get_type_string(struct mixer_ctl *ctl)
298{
299 if (!ctl)
300 return "";
301
302 switch (ctl->info->type) {
303 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
304 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
305 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
306 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
307 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
308 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
309 default: return "Unknown";
310 };
311}
312
313static unsigned int _mixer_ctl_get_num_values(struct mixer_ctl *ctl)
314{
315 if (!ctl)
316 return 0;
317
318 return ctl->info->count;
319}
320
321static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
322{
323 int range;
324
325 if (percent > 100)
326 percent = 100;
327 else if (percent < 0)
328 percent = 0;
329
330 range = (ei->value.integer.max - ei->value.integer.min);
331
332 return ei->value.integer.min + (range * percent) / 100;
333}
334
335static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
336{
337 int range = (ei->value.integer.max - ei->value.integer.min);
338
339 if (range == 0)
340 return 0;
341
342 return ((value - ei->value.integer.min) / range) * 100;
343}
344
345static int _mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
346{
347 struct snd_ctl_elem_value ev;
348 int ret;
349
350 if (!ctl || (id >= ctl->info->count))
351 return -EINVAL;
352
353 memset(&ev, 0, sizeof(ev));
354 ev.id.numid = ctl->info->id.numid;
355 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
356 if (ret < 0)
357 return ret;
358
359 switch (ctl->info->type) {
360 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
361 ev.value.integer.value[id] = !!value;
362 break;
363
364 case SNDRV_CTL_ELEM_TYPE_INTEGER:
365 ev.value.integer.value[id] = value;
366 break;
367
368 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
369 ev.value.enumerated.item[id] = value;
370 break;
371
372 case SNDRV_CTL_ELEM_TYPE_BYTES:
373 ev.value.bytes.data[id] = value;
374 break;
375
376 default:
377 return -EINVAL;
378 }
379
380 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
381}
382
383static int _mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
384{
385 struct snd_ctl_elem_value ev;
386 int ret;
387
388 if (!ctl || (id >= ctl->info->count))
389 return -EINVAL;
390
391 memset(&ev, 0, sizeof(ev));
392 ev.id.numid = ctl->info->id.numid;
393 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
394 if (ret < 0)
395 return ret;
396
397 switch (ctl->info->type) {
398 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
399 return !!ev.value.integer.value[id];
400
401 case SNDRV_CTL_ELEM_TYPE_INTEGER:
402 return ev.value.integer.value[id];
403
404 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
405 return ev.value.enumerated.item[id];
406
407 case SNDRV_CTL_ELEM_TYPE_BYTES:
408 return ev.value.bytes.data[id];
409
410 default:
411 return -EINVAL;
412 }
413
414 return 0;
415}
416
417static int _mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
418{
419 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
420 return -EINVAL;
421
422 return int_to_percent(ctl->info, _mixer_ctl_get_value(ctl, id));
423}
424
425static int _mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
426{
427 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
428 return -EINVAL;
429
430 return _mixer_ctl_set_value(ctl, id, percent_to_int(ctl->info, percent));
431}
432
433static int _mixer_ctl_is_access_tlv_rw(struct mixer_ctl *ctl)
434{
435 return (ctl->info->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE);
436}
437
438static int _mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count)
439{
440 struct snd_ctl_elem_value ev;
441 int ret = 0;
442 size_t size;
443 void *source;
444 size_t total_count;
445
446 if ((!ctl) || !count || !array)
447 return -EINVAL;
448
449 total_count = ctl->info->count;
450
451 if ((ctl->info->type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
452 _mixer_ctl_is_access_tlv_rw(ctl)) {
453 /* Additional two words is for the TLV header */
454 total_count += TLV_HEADER_SIZE;
455 }
456
457 if (count > total_count)
458 return -EINVAL;
459
460 memset(&ev, 0, sizeof(ev));
461 ev.id.numid = ctl->info->id.numid;
462
463 switch (ctl->info->type) {
464 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
465 case SNDRV_CTL_ELEM_TYPE_INTEGER:
466 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
467 if (ret < 0)
468 return ret;
469 size = sizeof(ev.value.integer.value[0]);
470 source = ev.value.integer.value;
471 break;
472
473 case SNDRV_CTL_ELEM_TYPE_BYTES:
474 /* check if this is new bytes TLV */
475 if (_mixer_ctl_is_access_tlv_rw(ctl)) {
476 struct snd_ctl_tlv *tlv;
477 int ret;
478
479 if (count > SIZE_MAX - sizeof(*tlv))
480 return -EINVAL;
481 tlv = (struct snd_ctl_tlv *)calloc(1, sizeof(*tlv) + count);
482 if (!tlv)
483 return -ENOMEM;
484 tlv->numid = ctl->info->id.numid;
485 tlv->length = count;
486 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv);
487
488 source = tlv->tlv;
489 memcpy(array, source, count);
490
491 free(tlv);
492
493 return ret;
494 } else {
495 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
496 if (ret < 0)
497 return ret;
498 size = sizeof(ev.value.bytes.data[0]);
499 source = ev.value.bytes.data;
500 break;
501 }
502
503 case SNDRV_CTL_ELEM_TYPE_IEC958:
504 size = sizeof(ev.value.iec958);
505 source = &ev.value.iec958;
506 break;
507
508 default:
509 return -EINVAL;
510 }
511
512 memcpy(array, source, size * count);
513
514 return 0;
515}
516
517static int _mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
518{
519 struct snd_ctl_elem_value ev;
520 size_t size;
521 void *dest;
522 size_t total_count;
523
524 if ((!ctl) || !count || !array)
525 return -EINVAL;
526
527 total_count = ctl->info->count;
528
529 if ((ctl->info->type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
530 _mixer_ctl_is_access_tlv_rw(ctl)) {
531 /* Additional two words is for the TLV header */
532 total_count += TLV_HEADER_SIZE;
533 }
534
535 if (count > total_count)
536 return -EINVAL;
537
538 memset(&ev, 0, sizeof(ev));
539 ev.id.numid = ctl->info->id.numid;
540
541 switch (ctl->info->type) {
542 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
543 case SNDRV_CTL_ELEM_TYPE_INTEGER:
544 size = sizeof(ev.value.integer.value[0]);
545 dest = ev.value.integer.value;
546 break;
547
548 case SNDRV_CTL_ELEM_TYPE_BYTES:
549 /* check if this is new bytes TLV */
550 if (_mixer_ctl_is_access_tlv_rw(ctl)) {
551 struct snd_ctl_tlv *tlv;
552 int ret = 0;
553 if (count > SIZE_MAX - sizeof(*tlv))
554 return -EINVAL;
555 tlv = (struct snd_ctl_tlv *)calloc(1, sizeof(*tlv) + count);
556 if (!tlv)
557 return -ENOMEM;
558 tlv->numid = ctl->info->id.numid;
559 tlv->length = count;
560 memcpy(tlv->tlv, array, count);
561
562 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
563 free(tlv);
564
565 return ret;
566 } else {
567 size = sizeof(ev.value.bytes.data[0]);
568 dest = ev.value.bytes.data;
569 }
570 break;
571
572 case SNDRV_CTL_ELEM_TYPE_IEC958:
573 size = sizeof(ev.value.iec958);
574 dest = &ev.value.iec958;
575 break;
576
577 default:
578 return -EINVAL;
579 }
580
581 memcpy(dest, array, size * count);
582
583 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
584}
585
586static int _mixer_ctl_get_range_min(struct mixer_ctl *ctl)
587{
588 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
589 return -EINVAL;
590
591 return ctl->info->value.integer.min;
592}
593
594static int _mixer_ctl_get_range_max(struct mixer_ctl *ctl)
595{
596 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
597 return -EINVAL;
598
599 return ctl->info->value.integer.max;
600}
601
602static unsigned int _mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
603{
604 if (!ctl)
605 return 0;
606
607 return ctl->info->value.enumerated.items;
608}
609
610static const char *_mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
611 unsigned int enum_id)
612{
613 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
614 (enum_id >= ctl->info->value.enumerated.items))
615 return NULL;
616
617 return (const char *)ctl->ename[enum_id];
618}
619
620static int _mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
621{
622 unsigned int i, num_enums;
623 struct snd_ctl_elem_value ev;
624 int ret;
625
626 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED))
627 return -EINVAL;
628
629 num_enums = ctl->info->value.enumerated.items;
630 for (i = 0; i < num_enums; i++) {
631 if (!strcmp(string, ctl->ename[i])) {
632 memset(&ev, 0, sizeof(ev));
633 ev.value.enumerated.item[0] = i;
634 ev.id.numid = ctl->info->id.numid;
635 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
636 if (ret < 0)
637 return ret;
638 return 0;
639 }
640 }
641
642 return -EINVAL;
643}
644
645static int aml_audio_mixer_int(const char *control, int value, bool set)
646{
647 int ret = -1;
648 struct mixer_ctl *pCtrl = NULL;
649 struct mixer *mixer = NULL;
650 if (control == NULL) {
651 ALOGE("[%s:%d] control is invalid!\n", __FUNCTION__, __LINE__);
652 return ret;
653 }
654 mixer = _mixer_open(DEFAULT_AML_SOUND_CARD);
655
656 if (mixer == NULL) {
657 ALOGE("[%s:%d] mixer is invalid!\n", __FUNCTION__, __LINE__);
658 return ret;
659 }
660
661 pCtrl = _mixer_get_ctl_by_name(mixer, control);
662 if (pCtrl == NULL) {
663 ALOGE("[%s:%d] Failed to find control: %s\n", __FUNCTION__, __LINE__, control);
664 _mixer_close(mixer);
665 return ret;
666 }
667 if (set) {
668 ret = _mixer_ctl_set_value(pCtrl, 0, value);
669 ALOGV("[%s:%d] set %s: %d, ret %d", __FUNCTION__, __LINE__, control, value, ret);
670 } else {
671 ret = _mixer_ctl_get_value(pCtrl, 0);
672 }
673 _mixer_close(mixer);
674
675 return ret;
676}
677
678static int aml_audio_mixer_array(const char *control, void *array, size_t count, bool set)
679{
680 int ret = -1;
681 struct mixer_ctl *pCtrl = NULL;
682 struct mixer *mixer = NULL;
683 if (control == NULL) {
684 ALOGE("[%s:%d] control is invalid!\n", __FUNCTION__, __LINE__);
685 return ret;
686 }
687 mixer = _mixer_open(DEFAULT_AML_SOUND_CARD);
688
689 if (mixer == NULL) {
690 ALOGE("[%s:%d] mixer is invalid!\n", __FUNCTION__, __LINE__);
691 return ret;
692 }
693
694 pCtrl = _mixer_get_ctl_by_name(mixer, control);
695 if (pCtrl == NULL) {
696 ALOGE("[%s:%d] Failed to find control: %s\n", __FUNCTION__, __LINE__, control);
697 _mixer_close(mixer);
698 return ret;
699 }
700 if (set) {
701 ret = _mixer_ctl_set_array(pCtrl, array, count);
702 ALOGV("[%s:%d] set %s, ret %d", __FUNCTION__, __LINE__, control, ret);
703 } else {
704 ret = _mixer_ctl_get_array(pCtrl, array, count);
705 }
706 _mixer_close(mixer);
707
708 return ret;
709}
710
711/* Headphone mute */
712static int set_dac_digital_mute(bool mute)
713{
714 int ret = 0;
715 static bool last_mute_state = false;
716 static int dac_digital_volume = DAC_DIGITAl_DEFAULT_VOLUME; // volume range: 0 ~ 255
717 int headphone_dac_gain[HEADPHONE_DAC_CHANNEL_NUM] = {dac_digital_volume, dac_digital_volume};
718
719 if (last_mute_state == mute) {
720 return ret;
721 }
722 if (mute) {
723 dac_digital_volume = aml_audio_mixer_int(DAC_DIGITAL_VOLUME, 0, false);//get current hp vol
724 headphone_dac_gain[0] = headphone_dac_gain[1] = 0;
725 ret = aml_audio_mixer_array(DAC_DIGITAL_VOLUME, headphone_dac_gain, HEADPHONE_DAC_CHANNEL_NUM, true);
726 } else {
727 //headphone_dac_gain[0] = headphone_dac_gain[1] = dac_digital_volume;
728 ret = aml_audio_mixer_array(DAC_DIGITAL_VOLUME, headphone_dac_gain, HEADPHONE_DAC_CHANNEL_NUM, true);
729 }
730 last_mute_state = mute;
731 ALOGD("[%s:%d] DAC Digital %smute, dac_digital_volume is %d", __func__, __LINE__, mute?" ":"un", dac_digital_volume);
732 return ret;
733}
734
yuliang.huf4792d52023-09-19 10:14:14 +0000735int aml_audio_set_hdmi_param(bool isconnect)
736{
737 int ret = 0;
738 pthread_mutex_lock(&g_volume_lock);
739 chip_id = aml_audio_mixer_int(AML_CHIP_ID, 0, false);
740 /*s1a use this way to set hdmi status*/
741 if (AML_CHIP_ID_S1A == chip_id) {
742 ret = aml_audio_mixer_int(HDMI_CON, isconnect, true);
743 ALOGD("[%s:%d] isconnect: %d, ret: %d", __func__, __LINE__, isconnect, ret);
744 }
745 pthread_mutex_unlock(&g_volume_lock);
746 return ret;
747}
748
wei.wang1e45cfd32023-07-10 08:33:46 +0000749int aml_audio_set_volume(int value)
750{
751 int ret = 0;
752 if (value < 0 || value > 100) {
753 ALOGE("[%s:%d]bad volume: %d", __func__, __LINE__, value);
754 return -1;
755 }
756
757 pthread_mutex_lock(&g_volume_lock);
wei.wang158743372023-08-07 08:07:05 +0000758 chip_id = aml_audio_mixer_int(AML_CHIP_ID, 0, false);
759 /*s1a use TDM-A as cvbs/hdmi_tx samesource*/
760 if (AML_CHIP_ID_S1A == chip_id)
761 ret = aml_audio_mixer_int(TDMA_GAIN, value, true);
762 else
763 ret = aml_audio_mixer_int(TDMB_GAIN, value, true);
764
765 ALOGD("[%s:%d] chip_id: %d, volume: %d, ret: %d", __func__, __LINE__, chip_id, value, ret);
wei.wang1e45cfd32023-07-10 08:33:46 +0000766 pthread_mutex_unlock(&g_volume_lock);
767
768 return ret;
769}
770
771int aml_audio_get_volume()
772{
773 pthread_mutex_lock(&g_volume_lock);
774 int ret = 0;
wei.wang158743372023-08-07 08:07:05 +0000775 if (AML_CHIP_ID_S1A == chip_id)
776 ret = aml_audio_mixer_int(TDMA_GAIN, 0, false);
777 else
778 ret = aml_audio_mixer_int(TDMB_GAIN, 0, false);
779 ALOGD("[%s:%d] chip_id: %d, volume: %d", __func__, __LINE__, chip_id, ret);
wei.wang1e45cfd32023-07-10 08:33:46 +0000780 pthread_mutex_unlock(&g_volume_lock);
781
782 return ret;
783}
784
785int aml_audio_set_mute(int port, bool mute)
786{
787 pthread_mutex_lock(&g_mute_lock);
788 int ret = -1;
789 if (port <= AUDIO_PORT_MIN || port >= AUDIO_PORT_MAX) {
790 ALOGE("[%s:%d]bad port: %d", __func__, __LINE__, port);
791 } else if (port == AUDIO_PORT_HDMI) {
haiyang.ren49ae2c82023-11-02 09:31:41 +0000792 if (mute && !aml_audio_get_mute(port)) {
793 reserved_volume = aml_audio_get_volume();
794 ret = aml_audio_set_volume(0);
795 } else if (!mute) {
796 ret = aml_audio_set_volume(reserved_volume);
797 }
wei.wang1e45cfd32023-07-10 08:33:46 +0000798 } else if (port == AUDIO_PORT_HEADPHONE) {
799 ret = set_dac_digital_mute(mute);
800 }
801 ALOGD("[%s:%d] port: %d, mute: %d, ret: %d", __func__, __LINE__, port, mute, ret);
802 pthread_mutex_unlock(&g_mute_lock);
803 return ret;
804}
805
806bool aml_audio_get_mute(int port)
807{
808 pthread_mutex_lock(&g_mute_lock);
809 bool ret = false;
810 if (port <= AUDIO_PORT_MIN || port >= AUDIO_PORT_MAX) {
811 ALOGE("[%s:%d]bad port: %d", __func__, __LINE__, port);
812 } else if (port == AUDIO_PORT_HDMI) {
haiyang.ren49ae2c82023-11-02 09:31:41 +0000813 ret = (aml_audio_get_volume() == 0) ? true : false;
wei.wang1e45cfd32023-07-10 08:33:46 +0000814 } else if (port == AUDIO_PORT_HEADPHONE) {
815 ret = aml_audio_mixer_int(DAC_DIGITAL_VOLUME, 0, false) ? false : true;
816 }
817 ALOGD("[%s:%d] port: %d, mute: %d", __func__, __LINE__, port, ret);
818 pthread_mutex_unlock(&g_mute_lock);
819 return ret;
820}
821
haiyang.ren936bfc52023-09-18 05:46:24 +0000822int aml_audio_set_digital_mode(enum audio_digital_mode mode)
823{
824 int ret = 0;
825 if ((mode != AML_HAL_PCM) && (mode != AML_HAL_DDP) &&
826 (mode != AML_HAL_AUTO) && (mode != AML_HAL_BYPASS) &&
827 (mode != AML_HAL_DD)) {
828 printf("Invalid mode\n");
829 return false;
haiyang.renfdc144e2023-09-01 02:55:50 +0000830 }
831
haiyang.ren936bfc52023-09-18 05:46:24 +0000832 pthread_mutex_lock(&g_volume_lock);
833 ret = aml_audio_mixer_int(DIGITAL_MODE, mode, true);
834 ALOGD("[%s:%d] mode: %d, ret: %d", __func__, __LINE__, mode, ret);
835 pthread_mutex_unlock(&g_volume_lock);
haiyang.renfdc144e2023-09-01 02:55:50 +0000836
haiyang.ren936bfc52023-09-18 05:46:24 +0000837 return ret;
838}
839
840int aml_audio_get_digital_mode()
841{
842 pthread_mutex_lock(&g_volume_lock);
843 int ret = 0;
844 ret = aml_audio_mixer_int(DIGITAL_MODE, 0, false);
845 ALOGD("[%s:%d] mod: %d", __func__, __LINE__, ret);
846 pthread_mutex_unlock(&g_volume_lock);
847
848 return ret;
849}
850
851int aml_audio_set_drc_mode(enum audio_drc_mode mode)
852{
853 int ret = 0;
854 int drc_control = DRC_MODE_LINE;
855 if ((mode != DRC_RF) && (mode != DRC_LINE) && (mode != DRC_OFF)) {
856 printf("Invalid mode!\n");
857 return false;
haiyang.renfdc144e2023-09-01 02:55:50 +0000858 }
859
haiyang.ren936bfc52023-09-18 05:46:24 +0000860 pthread_mutex_lock(&g_volume_lock);
861 if (mode == DRC_LINE) {
862 drc_control = (DRC_MODE_LINE<<DRC_MODE_BIT)|(100<<DRC_HIGH_CUT_BIT)|(100<<DRC_LOW_BST_BIT);
863 } else if (mode == DRC_RF) {
864 drc_control = (DRC_MODE_RF<<DRC_MODE_BIT)|(100<<DRC_HIGH_CUT_BIT)|(100<<DRC_LOW_BST_BIT);
865 }
866 ret = aml_audio_mixer_int(DRC_CONTROL, drc_control, true);
867 ALOGD("[%s:%d] mode: %d, drc_control: %#x, ret: %d.", __func__, __LINE__, mode, drc_control, ret);
868 pthread_mutex_unlock(&g_volume_lock);
869
870 return ret;
871}
872
873int aml_audio_get_drc_mode()
874{
875 pthread_mutex_lock(&g_volume_lock);
876 int ret = 0, drc_control;
877 drc_control = aml_audio_mixer_int(DRC_CONTROL, 0, false);
878
879 if (drc_control == DRC_MODE_LINE)
880 ret = DRC_OFF;
881 else
882 ret = (drc_control&3 == DRC_MODE_LINE) ? DRC_LINE : DRC_RF;
883 ALOGD("[%s:%d] mode: %d", __func__, __LINE__, ret);
884 pthread_mutex_unlock(&g_volume_lock);
885
886 return ret;
887}
wei.wang1e45cfd32023-07-10 08:33:46 +0000888}//extern c