-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathIQSetupMix.c
242 lines (193 loc) · 5.79 KB
/
IQSetupMix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Sample code to set the IQaudio mixer settings correctly for 2vRMS output.
// Also sets GPIO22 to unmute the AMP+ or DigiAMP+ if being used.
//
// G.Garrity Jan 2nd 2016 (C) IQaudio Limited
// Edited 16th Oct 2016 to remove GPIO22 mute settings as this is now handled in the overlay itself.
//
// Compile with gcc IQSetupMix.c -oIQSetupMix -lasound
//
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <stdarg.h>
// Needed for Serial number capture
#include <stdlib.h>
#include <string.h>
// Needed for MAC address
#include <ifaddrs.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <unistd.h>
// Needed for GPIO Access
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define IN 0
#define OUT 1
#define LOW 0
#define HIGH 1
// Needed for ALSA mixer settings
#include <alsa/asoundlib.h>
#include <alsa/mixer.h>
// Define DEBUG_PRINT TRUE for output
#define DEBUG_PRINT 0 // 1 debug messages, 0 none
#define TRUE 1
#define FALSE 0
static int
GPIOExport(int pin)
{
#define BUFFER_MAX 3
char buffer[BUFFER_MAX];
ssize_t bytes_written;
int fd;
fd = open("/sys/class/gpio/export", O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open export for writing!\n");
return(-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
write(fd, buffer, bytes_written);
close(fd);
return(0);
}
static int
GPIOUnexport(int pin)
{
char buffer[BUFFER_MAX];
ssize_t bytes_written;
int fd;
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open unexport for writing!\n");
return(-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
write(fd, buffer, bytes_written);
close(fd);
return(0);
}
static int
GPIODirection(int pin, int dir)
{
static const char s_directions_str[] = "in\0out";
#define DIRECTION_MAX 35
char path[DIRECTION_MAX];
int fd;
snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/direction", pin);
fd = open(path, O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open gpio direction for writing!\n");
return(-1);
}
if (-1 == write(fd, &s_directions_str[IN == dir ? 0 : 3], IN == dir ? 2 : 3)) {
fprintf(stderr, "Failed to set direction!\n");
return(-1);
}
close(fd);
return(0);
}
static int
GPIORead(int pin)
{
#define VALUE_MAX 30
char path[VALUE_MAX];
char value_str[3];
int fd;
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_RDONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open gpio value for reading!\n");
return(-1);
}
if (-1 == read(fd, value_str, 3)) {
fprintf(stderr, "Failed to read value!\n");
return(-1);
}
close(fd);
return(atoi(value_str));
}
static int
GPIOWrite(int pin, int value)
{
static const char s_values_str[] = "01";
char path[VALUE_MAX];
int fd;
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open gpio value for writing!\n");
return(-1);
}
if (1 != write(fd, &s_values_str[LOW == value ? 0 : 1], 1)) {
fprintf(stderr, "Failed to write value!\n");
return(-1);
}
close(fd);
return(0);
}
void SetMixerSetting(long volume)
{
long min, max, currentVolume;
int x;
snd_mixer_t *handle;
snd_mixer_selem_id_t *sid;
const char *card = "hw:CARD=IQaudIODAC";
// Previous linux driver's mixer name
// const char *selem_name = "Playback Digital";
// const char *selem_name = "PCM";
const char *selem_name = "Analogue"; // Linux 4.1.6-v7+ #810
const char *selem_name2 = "Analogue Playback Boost"; // Linux 4.1.6-v7+ #810
// Setup ALSA access
snd_mixer_open(&handle, 0);
snd_mixer_attach(handle, card);
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
snd_mixer_selem_id_alloca(&sid);
snd_mixer_selem_id_set_index(sid, 0);
// Make change to Analogue mixer first
snd_mixer_selem_id_set_name(sid, selem_name);
snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid);
snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
if (DEBUG_PRINT) printf("Returned card's MIXER range - min: %ld, max: %ld\n", min, max);
// Get current volume
if (x = snd_mixer_selem_get_playback_volume (elem, SND_MIXER_SCHN_FRONT_LEFT, ¤tVolume))
{
if (DEBUG_PRINT) printf("%d %s\n", x, snd_strerror(x));
}
else if (DEBUG_PRINT) printf("Current ALSA mixer value: %ld\n", currentVolume);
// Set value to max
currentVolume = 1;
if (x = snd_mixer_selem_set_playback_volume_all (elem, currentVolume))
{
if (DEBUG_PRINT) printf("%d %s\n", x, snd_strerror(x));
}
else if (DEBUG_PRINT) printf("Current ALSA mixer value: %ld\n", currentVolume);
// Make changes to Analogue Playback Boost mixer too
snd_mixer_selem_id_set_name(sid, selem_name2);
snd_mixer_elem_t* elem2 = snd_mixer_find_selem(handle, sid);
snd_mixer_selem_get_playback_volume_range(elem2, &min, &max);
if (DEBUG_PRINT) printf("Returned card's MIXER range - min: %ld, max: %ld\n", min, max);
// Get current volume
if (x = snd_mixer_selem_get_playback_volume (elem2, SND_MIXER_SCHN_FRONT_LEFT, ¤tVolume))
{
if (DEBUG_PRINT) printf("%d %s\n", x, snd_strerror(x));
}
else if (DEBUG_PRINT) printf("Current ALSA mixer value: %ld\n", currentVolume);
// Set value to max
currentVolume = 1;
if (x = snd_mixer_selem_set_playback_volume_all (elem2, currentVolume))
{
if (DEBUG_PRINT) printf("%d %s\n", x, snd_strerror(x));
}
else if (DEBUG_PRINT) printf("Current ALSA mixer value: %ld\n", currentVolume);
snd_mixer_close(handle);
}
int main(int argc, char * argv[])
{
printf("IQaudIO Set PCM512x ALSA driver for 2vRMS v1.2 Oct 16th 2016\n\n");
SetMixerSetting(1);
}