summaryrefslogtreecommitdiffstats
path: root/exynos4412_gpios.c
blob: b574ff22a95b70450fc1d4f56bc813eb49c6d24e (plain)
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
/*
 * Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include <sys/mman.h>

#include "exynos4412_gpios.h"
#include "exynos4412_gpios_data.h"

void *mmap_gpio_bank(int debug, char *devmem, int fd, size_t page_size,
		     char *bank)
{
	/* TODO: also map GPIO_right
	 * GPIO_right: base_addr: 0x11000000 len: 0x400000
	 * GPIO_left: base_addr:  0x11400000 len: 0x400000
	 */
	uint32_t base_addr = 0x11000000;
	size_t len = 0x800000;

	void *map_base_addr;
	uint32_t *virt_addr;

	int rc;

	map_base_addr = mmap(0, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd,
			     base_addr & ~(page_size -1));
	if (map_base_addr == MAP_FAILED) {
		rc = errno;
		printf("mmap on %s failed with error %d: %s\n",
		       devmem, rc, strerror(rc));
		errno = rc;
		return NULL;
	}

	virt_addr = map_base_addr + (base_addr & 4095);

	if (debug) {
		printf("%s: mapped address 0x%x @ %p\n", __func__, 0,
		       map_base_addr);
		printf("%s: mapped 0x%x bytes of address 0x%x @ %p\n", __func__,
		       len, base_addr, virt_addr);
	}

	return virt_addr;
}

int dump_gpio_infos(int debug, char *devmem, int fd, size_t page_size,
		    char *bank, uint32_t gpio_offset)
{
	uint32_t *addr;
	int rc;
	int i = 0;
	off_t offset = 0;

	char* gpio_registers[] = { "con", "dat", "pud", "drv" };

	offset = get_gpio_register_offset(debug, bank, "con");
	if (offset == -1) {
		rc = errno;
		printf("%s: get_gpio_register_offset failed with error %d: %s\n",
		       __func__, rc, strerror(rc));
		return -1;
	}


	addr = mmap_gpio_bank(debug, devmem, fd, page_size, bank)  + offset;

	if (debug) {
		printf("%s: Mapped at %p\n", __func__, addr);
		print_gpio_banks_data();
	}

	printf("%s: %s[%d]: {\n",  __func__, bank, gpio_offset);
	for (i=0; i< sizeof(gpio_registers) / sizeof (char*); i++) {

		rc = decode_gpio_data(debug, bank, gpio_offset,
				      gpio_registers[i], addr);
		if (rc == -1) {
			rc = errno;
			printf("%s:"
			       " decode_gpio_data(debug=%d,"
			       " bank=%s,"
			       " gpio_offset=%d,"
			       " gpio_register_name=%s,"
			       " addr=%p);"
			       " failed with with error %d: %s\n",
			       __func__,
			       debug,
			       bank,
			       gpio_offset,
			       gpio_registers[i],
			       addr,
			       rc, strerror(rc));
			return -1;
		}

		printf("  %s\n", gpio_data_str(gpio_registers[i], rc));
	}
	printf("}\n");

	return 0;
}

int gpio_get_direction(char *devmem, int fd, size_t page_size, char *bank,
		  uint32_t gpio_offset)
{
	int debug = 0;

	uint32_t *addr;
	int rc;
	off_t offset = 0;

	offset = get_gpio_register_offset(debug, bank, "con");
	if (offset == -1) {
		rc = errno;
		printf("%s: get_gpio_register_offset failed with error %d: %s\n",
		       __func__, rc, strerror(rc));
		return -1;
	}

	addr = mmap_gpio_bank(debug, devmem, fd, page_size, bank) + offset;
	if (addr == NULL) {
		/* mmap_gpio_bank already prints an error */
		return -1;
	}

	if (debug) {
		printf("%s: Mapped at %p\n", __func__, addr);
		print_gpio_banks_data();
	}

	rc = decode_gpio_data(debug, bank, gpio_offset,
				      "con", addr);
	if (rc == -1) {
		rc = errno;
		printf("%s:"
		       " decode_gpio_data(debug=%d,"
		       " bank=%s,"
		       " gpio_offset=%d,"
		       " gpio_register_name=%s,"
		       " addr=%p);"
		       " failed with with error %d: %s\n",
		       __func__,
		       debug,
		       bank,
		       gpio_offset,
		       "con",
		       addr,
		       rc, strerror(rc));
		return -1;
	}

	return rc;
}

int gpio_get_output_value(char *devmem, int fd, size_t page_size, char *bank,
			  uint32_t gpio_offset)
{
	int debug = 0;

	uint32_t *addr;
	int rc;
	off_t offset = 0;

	offset = get_gpio_register_offset(debug, bank, "dat");
	if (offset == -1) {
		rc = errno;
		printf("%s: get_gpio_register_offset failed with error %d: %s\n",
		       __func__, rc, strerror(rc));
		return -1;
	}

	if (debug){
		printf("%s: offset for %s: 0x%lx\n\n",
		       __func__, bank, offset);
	}

	addr = mmap_gpio_bank(debug, devmem, fd, page_size, bank) + offset;
	if (addr == NULL) {
		/* mmap_gpio_bank already prints an error */
		return -1;
	}

	if (debug) {
		printf("%s: Mapped at %p\n", __func__, addr);
		print_gpio_banks_data();
	}

	rc = decode_gpio_data(debug, bank, gpio_offset,
				      "dat", addr);
	if (rc == -1) {
		rc = errno;
		printf("%s:"
		       " decode_gpio_data(debug=%d,"
		       " bank=%s,"
		       " gpio_offset=%d,"
		       " gpio_register_name=%s,"
		       " addr=%p);"
		       " failed with with error %d: %s\n",
		       __func__,
		       debug,
		       bank,
		       gpio_offset,
		       "dat",
		       addr,
		       rc, strerror(rc));
		return -1;
	}

	return rc;
}