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