diff options
Diffstat (limited to 'firmware/drivers/audio')
-rw-r--r-- | firmware/drivers/audio/fiiolinux_codec.c | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/firmware/drivers/audio/fiiolinux_codec.c b/firmware/drivers/audio/fiiolinux_codec.c new file mode 100644 index 0000000000..d8024e3c32 --- /dev/null +++ b/firmware/drivers/audio/fiiolinux_codec.c | |||
@@ -0,0 +1,154 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * | ||
11 | * Copyright (c) 2018 Marcin Bukat | ||
12 | * Copyright (c) 2019 Roman Stolyarov | ||
13 | * | ||
14 | * This program is free software; you can redistribute it and/or | ||
15 | * modify it under the terms of the GNU General Public License | ||
16 | * as published by the Free Software Foundation; either version 2 | ||
17 | * of the License, or (at your option) any later version. | ||
18 | * | ||
19 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
20 | * KIND, either express or implied. | ||
21 | * | ||
22 | ****************************************************************************/ | ||
23 | |||
24 | #include "config.h" | ||
25 | #include "audio.h" | ||
26 | #include "audiohw.h" | ||
27 | #include "system.h" | ||
28 | #include "kernel.h" | ||
29 | #include "panic.h" | ||
30 | #include "sysfs.h" | ||
31 | #include "alsa-controls.h" | ||
32 | #include "pcm-alsa.h" | ||
33 | #include <sys/ioctl.h> | ||
34 | |||
35 | static int fd_hw; | ||
36 | static int ak_hw; | ||
37 | |||
38 | static int vol_sw[2] = {0}; | ||
39 | static long int vol_hw[2] = {0}; | ||
40 | |||
41 | static void hw_open(void) | ||
42 | { | ||
43 | fd_hw = open("/dev/snd/controlC0", O_RDWR); | ||
44 | if(fd_hw < 0) | ||
45 | panicf("Cannot open '/dev/snd/controlC0'"); | ||
46 | |||
47 | ak_hw = open("/dev/ak4376", O_RDWR); | ||
48 | if(ak_hw < 0) | ||
49 | panicf("Cannot open '/dev/ak4376'"); | ||
50 | |||
51 | if(ioctl(ak_hw, 0x20003424, 0) < 0) | ||
52 | { | ||
53 | panicf("Call cmd AK4376_POWER_ON fail"); | ||
54 | } | ||
55 | } | ||
56 | |||
57 | static void hw_close(void) | ||
58 | { | ||
59 | if(ioctl(ak_hw, 0x20003425, 0) < 0) | ||
60 | { | ||
61 | panicf("Call cmd AK4376_POWER_OFF fail"); | ||
62 | } | ||
63 | close(ak_hw); | ||
64 | |||
65 | close(fd_hw); | ||
66 | } | ||
67 | |||
68 | void audiohw_preinit(void) | ||
69 | { | ||
70 | alsa_controls_init(); | ||
71 | hw_open(); | ||
72 | } | ||
73 | |||
74 | void audiohw_postinit(void) | ||
75 | { | ||
76 | } | ||
77 | |||
78 | void audiohw_close(void) | ||
79 | { | ||
80 | hw_close(); | ||
81 | alsa_controls_close(); | ||
82 | } | ||
83 | |||
84 | void audiohw_set_frequency(int fsel) | ||
85 | { | ||
86 | (void)fsel; | ||
87 | } | ||
88 | |||
89 | void audiohw_set_volume(int vol_l, int vol_r) | ||
90 | { | ||
91 | int vol[2]; | ||
92 | |||
93 | vol[0] = vol_l / 20; | ||
94 | vol[1] = vol_r / 20; | ||
95 | |||
96 | for (int i = 0; i < 2; i++) | ||
97 | { | ||
98 | if (vol[i] > -56) | ||
99 | { | ||
100 | if (vol[i] < -12) | ||
101 | { | ||
102 | vol_hw[i] = 1; | ||
103 | vol_sw[i] = vol[i] + 12; | ||
104 | } | ||
105 | else | ||
106 | { | ||
107 | vol_hw[i] = 25 - (-vol[i] * 2); | ||
108 | vol_sw[i] = 0; | ||
109 | } | ||
110 | } | ||
111 | else | ||
112 | { | ||
113 | // Mute | ||
114 | vol_hw[i] = 0; | ||
115 | vol_sw[i] = 0; | ||
116 | } | ||
117 | } | ||
118 | |||
119 | alsa_controls_set_ints("DACL Playback Volume", 1, &vol_hw[0]); | ||
120 | alsa_controls_set_ints("DACR Playback Volume", 1, &vol_hw[1]); | ||
121 | pcm_alsa_set_digital_volume(vol_sw[0], vol_sw[1]); | ||
122 | } | ||
123 | |||
124 | void audiohw_mute(int mute) | ||
125 | { | ||
126 | long int vol0 = 0; | ||
127 | |||
128 | if(mute) | ||
129 | { | ||
130 | alsa_controls_set_ints("DACL Playback Volume", 1, &vol0); | ||
131 | alsa_controls_set_ints("DACR Playback Volume", 1, &vol0); | ||
132 | pcm_alsa_set_digital_volume(0, 0); | ||
133 | } | ||
134 | else | ||
135 | { | ||
136 | alsa_controls_set_ints("DACL Playback Volume", 1, &vol_hw[0]); | ||
137 | alsa_controls_set_ints("DACR Playback Volume", 1, &vol_hw[1]); | ||
138 | pcm_alsa_set_digital_volume(vol_sw[0], vol_sw[1]); | ||
139 | } | ||
140 | } | ||
141 | |||
142 | void audiohw_set_filter_roll_off(int value) | ||
143 | { | ||
144 | /* 0 = Sharp; | ||
145 | 1 = Slow; | ||
146 | 2 = Short Sharp | ||
147 | 3 = Short Slow */ | ||
148 | #if defined(FIIO_M3K) | ||
149 | long int value_hw = value; | ||
150 | alsa_controls_set_ints("AK4376 Digital Filter", 1, &value_hw); | ||
151 | #else | ||
152 | (void)value; | ||
153 | #endif | ||
154 | } | ||