-
Notifications
You must be signed in to change notification settings - Fork 152
/
pinmux.c
207 lines (183 loc) · 4.02 KB
/
pinmux.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MMAP_PATH "/dev/mem"
enum {
MUX_GPIO = 0,
MUX_SPI_S,
MUX_SPI_CS1,
MUX_I2S,
MUX_UART0,
MUX_I2C,
MUX_UART1,
MUX_UART2,
MUX_PWM0,
MUX_PWM1,
MUX_EPHY,
MUX_WLED,
__MUX_MAX,
};
static struct pinmux {
char *name;
char *func[4];
unsigned int shift;
unsigned int mask;
} mt7688_mux[] = {
{
.name = "i2c",
.func = { "i2c", "gpio", NULL, NULL },
.shift = 20,
.mask = 0x3,
}, {
.name = "uart0",
.func = { "uart", "gpio", NULL, NULL },
.shift = 8,
.mask = 0x3,
}, {
.name = "uart1",
.func = { "uart", "gpio", NULL, NULL },
.shift = 24,
.mask = 0x3,
}, {
.name = "uart2",
.func = { "uart", "gpio", "pwm", NULL },
.shift = 26,
.mask = 0x3,
}, {
.name = "pwm0",
.func = { "pwm", "gpio", NULL, NULL },
.shift = 28,
.mask = 0x3,
}, {
.name = "pwm1",
.func = { "pwm", "gpio", NULL, NULL },
.shift = 30,
.mask = 0x3,
}, {
.name = "refclk",
.func = { "refclk", "gpio", NULL, NULL },
.shift = 18,
.mask = 0x1,
}, {
.name = "spi_s",
.func = { "spi_s", "gpio", NULL, NULL },
.shift = 2,
.mask = 0x3,
}, {
.name = "spi_cs1",
.func = { "spi_cs1", "gpio", NULL, "refclk" },
.shift = 4,
.mask = 0x3,
}, {
.name = "i2s",
.func = { "i2s", "gpio", "pcm", NULL },
.shift = 6,
.mask = 0x3,
}, {
.name = "ephy",
.func = { "ephy", "gpio", NULL, NULL },
.shift = 34,
.mask = 0x3,
}, {
.name = "wled",
.func = { "wled", "gpio", NULL, NULL },
.shift = 32,
.mask = 0x3,
}
};
static uint8_t* gpio_mmap_reg = NULL;
static int gpio_mmap_fd = 0;
static void __gpiomode_set(unsigned int mask, unsigned int shift, unsigned int val)
{
volatile uint32_t *reg = (volatile uint32_t*) (gpio_mmap_reg + 0x60);
unsigned int v;
if (shift > 31) {
shift -= 32;
reg++;
}
v = *reg;
v &= ~(mask << shift);
v |= (val << shift);
*(volatile uint32_t*) (reg) = v;
}
static int gpiomode_set(char *group, char *name)
{
int i;
int id;
for (id = 0; id < __MUX_MAX; id ++)
if (!strcmp(mt7688_mux[id].name, group))
break;
if (id < __MUX_MAX) for (i = 0; i < 4; i++) {
if (!mt7688_mux[id].func[i] || strcmp(mt7688_mux[id].func[i], name))
continue;
__gpiomode_set(mt7688_mux[id].mask, mt7688_mux[id].shift, i);
fprintf(stderr, "set pinmux %s -> %s\n", mt7688_mux[id].name, name);
return 0;
}
fprintf(stderr, "unknown group/function combination\n");
return -1;
}
static int gpiomode_get(void)
{
unsigned int reg = *(volatile uint32_t*) (gpio_mmap_reg + 0x60);
unsigned int reg2 = *(volatile uint32_t*) (gpio_mmap_reg + 0x64);
int id;
for (id = 0; id < __MUX_MAX; id ++) {
unsigned int val;
int i;
if (mt7688_mux[id].shift < 32)
val = (reg >> mt7688_mux[id].shift) & mt7688_mux[id].mask;
else
val = (reg2 >> (mt7688_mux[id].shift - 32)) & mt7688_mux[id].mask;
fprintf(stderr, "Group %s - ", mt7688_mux[id].name);
for (i = 0; i < 4; i++) {
if (!mt7688_mux[id].func[i])
continue;
if (i == val)
fprintf(stderr, "[%s] ", mt7688_mux[id].func[i]);
else
fprintf(stderr, "%s ", mt7688_mux[id].func[i]);
}
fprintf(stderr, "\n");
}
return 0;
}
static int gpiomode_mmap(void)
{
if ((gpio_mmap_fd = open(MMAP_PATH, O_RDWR)) < 0) {
fprintf(stderr, "unable to open mmap file");
return -1;
}
gpio_mmap_reg = (uint8_t*) mmap(NULL, 1024, PROT_READ | PROT_WRITE,
MAP_FILE | MAP_SHARED, gpio_mmap_fd, 0x10000000);
if (gpio_mmap_reg == MAP_FAILED) {
perror("foo");
fprintf(stderr, "failed to mmap");
gpio_mmap_reg = NULL;
close(gpio_mmap_fd);
return -1;
}
return 0;
}
int main(int argc, char **argv)
{
int ret = -1;
if (gpiomode_mmap())
return -1;
if (argc >= 4 && !strcmp(argv[1], "set")) {
ret = gpiomode_set(argv[2], argv[3]);
} else if (argc >= 2 && !strcmp(argv[1], "get")) {
ret = gpiomode_get();
} else {
fprintf(stderr, "%s set <group> <function>\n", *argv);
fprintf(stderr, "%s get\n", *argv);
}
close(gpio_mmap_fd);
return ret;
}