diff options
Diffstat (limited to 'firmware/drivers/audio/erosqlinux_codec.c')
-rw-r--r-- | firmware/drivers/audio/erosqlinux_codec.c | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/firmware/drivers/audio/erosqlinux_codec.c b/firmware/drivers/audio/erosqlinux_codec.c new file mode 100644 index 0000000000..deb3bb4b87 --- /dev/null +++ b/firmware/drivers/audio/erosqlinux_codec.c | |||
@@ -0,0 +1,181 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * | ||
11 | * Copyright (c) 2018 Marcin Bukat | ||
12 | * Copyright (c) 2020 Solomon Peachy | ||
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 | //#define LOGF_ENABLE | ||
25 | |||
26 | #include "config.h" | ||
27 | #include "audio.h" | ||
28 | #include "audiohw.h" | ||
29 | #include "button.h" | ||
30 | #include "system.h" | ||
31 | #include "kernel.h" | ||
32 | #include "panic.h" | ||
33 | #include "sysfs.h" | ||
34 | #include "alsa-controls.h" | ||
35 | #include "pcm-alsa.h" | ||
36 | #include "pcm_sw_volume.h" | ||
37 | |||
38 | #include "logf.h" | ||
39 | |||
40 | static int fd_hw; | ||
41 | static int inited = 0; | ||
42 | |||
43 | static long int vol_l_hw = 255; | ||
44 | static long int vol_r_hw = 255; | ||
45 | static long int last_ps = 0; | ||
46 | |||
47 | static void hw_open(void) | ||
48 | { | ||
49 | fd_hw = open("/dev/snd/controlC0", O_RDWR); | ||
50 | if(fd_hw < 0) | ||
51 | panicf("Cannot open '/dev/snd/controlC0'"); | ||
52 | } | ||
53 | |||
54 | static void hw_close(void) | ||
55 | { | ||
56 | close(fd_hw); | ||
57 | } | ||
58 | |||
59 | void audiohw_mute(int mute) | ||
60 | { | ||
61 | logf("mute %d", mute); | ||
62 | |||
63 | if(mute) | ||
64 | { | ||
65 | long int ps0 = 0; | ||
66 | alsa_controls_set_ints("Output Port Switch", 1, &ps0); | ||
67 | } | ||
68 | else | ||
69 | { | ||
70 | last_ps = 0; | ||
71 | erosq_get_outputs(); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | int erosq_get_outputs(void) { | ||
76 | long int ps = 0; // Muted, if nothing is plugged in! | ||
77 | |||
78 | int status = 0; | ||
79 | |||
80 | if (!inited) return ps; | ||
81 | |||
82 | const char * const sysfs_lo_switch = "/sys/class/switch/lineout/state"; | ||
83 | const char * const sysfs_hs_switch = "/sys/class/switch/headset/state"; | ||
84 | |||
85 | sysfs_get_int(sysfs_lo_switch, &status); | ||
86 | if (status) ps = 1; // lineout | ||
87 | |||
88 | sysfs_get_int(sysfs_hs_switch, &status); | ||
89 | if (status) ps = 2; // headset | ||
90 | |||
91 | erosq_set_output(ps); | ||
92 | |||
93 | return ps; | ||
94 | } | ||
95 | |||
96 | void erosq_set_output(int ps) | ||
97 | { | ||
98 | if (!inited) return; | ||
99 | |||
100 | if (last_ps != ps) | ||
101 | { | ||
102 | logf("set out %d/%d", ps, last_ps); | ||
103 | /* Output port switch */ | ||
104 | last_ps = ps; | ||
105 | alsa_controls_set_ints("Output Port Switch", 1, &last_ps); | ||
106 | audiohw_set_volume(vol_l_hw, vol_r_hw); | ||
107 | } | ||
108 | } | ||
109 | |||
110 | void audiohw_preinit(void) | ||
111 | { | ||
112 | logf("hw preinit"); | ||
113 | alsa_controls_init(); | ||
114 | hw_open(); | ||
115 | audiohw_mute(true); /* Start muted to avoid the POP */ | ||
116 | inited = 1; | ||
117 | } | ||
118 | |||
119 | void audiohw_postinit(void) | ||
120 | { | ||
121 | logf("hw postinit"); | ||
122 | erosq_set_output(erosq_get_outputs()); /* Unmute */ | ||
123 | } | ||
124 | |||
125 | void audiohw_close(void) | ||
126 | { | ||
127 | logf("hw close"); | ||
128 | inited = 0; | ||
129 | hw_close(); | ||
130 | alsa_controls_close(); | ||
131 | } | ||
132 | |||
133 | void audiohw_set_frequency(int fsel) | ||
134 | { | ||
135 | (void)fsel; | ||
136 | } | ||
137 | |||
138 | void audiohw_set_volume(int vol_l, int vol_r) | ||
139 | { | ||
140 | logf("hw vol %d %d", vol_l, vol_r); | ||
141 | |||
142 | long l,r; | ||
143 | |||
144 | vol_l_hw = vol_l; | ||
145 | vol_r_hw = vol_r; | ||
146 | |||
147 | if (lineout_inserted()) { | ||
148 | l = 0; | ||
149 | r = 0; | ||
150 | } else { | ||
151 | l = vol_l_hw; | ||
152 | r = vol_r_hw; | ||
153 | } | ||
154 | |||
155 | /* SW volume for <= 1.0 gain, HW at unity, < -740 == MUTE */ | ||
156 | int sw_volume_l = l <= -740 ? PCM_MUTE_LEVEL : MIN(l, 0); | ||
157 | int sw_volume_r = r <= -740 ? PCM_MUTE_LEVEL : MIN(r, 0); | ||
158 | pcm_set_master_volume(sw_volume_l, sw_volume_r); | ||
159 | } | ||
160 | |||
161 | void audiohw_set_lineout_volume(int vol_l, int vol_r) | ||
162 | { | ||
163 | long l,r; | ||
164 | |||
165 | logf("lo vol %d %d", vol_l, vol_r); | ||
166 | |||
167 | (void)vol_l; | ||
168 | (void)vol_r; | ||
169 | |||
170 | if (lineout_inserted()) { | ||
171 | l = 0; | ||
172 | r = 0; | ||
173 | } else { | ||
174 | l = vol_l_hw; | ||
175 | r = vol_r_hw; | ||
176 | } | ||
177 | |||
178 | int sw_volume_l = l <= -740 ? PCM_MUTE_LEVEL : MIN(l, 0); | ||
179 | int sw_volume_r = r <= -740 ? PCM_MUTE_LEVEL : MIN(r, 0); | ||
180 | pcm_set_master_volume(sw_volume_l, sw_volume_r); | ||
181 | } | ||