block_am335x_gpi.c 4.11 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
/*
	Copyright Jeroen Vreeken (jeroen@vreeken.net), 2014

	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 <http://www.gnu.org/licenses/>.

 */

#include "am335x.h"

#include <controller/controller_block.h>
#include <log/log.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <inttypes.h>
#include <unistd.h>
#include <math.h>


struct controller_block_private {
	bool in;

	uint32_t bitmask;

	void *base;
};

static void gpi_calculate(struct controller_block *gpi)
{
	struct controller_block_private *priv = gpi->private;
	bool in;
	uint32_t reg;
	void *base = priv->base;
	
	reg = am335x_read32(base, AM335X_GPIO_REG_DATAIN);
	in = reg & gpi->private->bitmask;

	priv->in = in;
}


static struct controller_block_outterm_list outterms[] = {
	{ "in",  CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, in) },
	{ NULL },
};


struct controller_block * block_am335x_gpi_create(char *name, va_list ap)
{
	struct controller_block *gpi;
	int fd;
	void *base;
	uint32_t reg;
	uint32_t notpin;
	int gpi_nr;
	int pin_nr;
	size_t offset;
	
	gpi_nr = va_arg(ap, int);
	if (gpi_nr < 0 || gpi_nr > 3) {
		log_send(LOG_T_ERROR, "%s: gpio%d is not valid. (valid: 0-3)",
		    name, gpi_nr);
		return NULL;
	}
	switch (gpi_nr) {
		case 0:
			offset = AM335X_GPIO0_BASE;
			break;
		case 1:
			offset = AM335X_GPIO1_BASE;
			break;
		case 2:
			offset = AM335X_GPIO2_BASE;
			break;
		case 3:
			offset = AM335X_GPIO3_BASE;
			break;
	}
	
	pin_nr = va_arg(ap, int);
	if (pin_nr < 0 || pin_nr > 31) {
		log_send(LOG_T_ERROR, "%s: pin%d is not valid. (valid: 0-31)",
		    name, pin_nr);
		return NULL;
	}
	
	if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0 ) {
		log_send(LOG_T_ERROR, "%s: Error opening /dev/mem", name);
		close(fd);
		return NULL;
	}

	base = mmap(NULL, AM335X_GPIO_SIZE, PROT_READ | PROT_WRITE, 
	    MAP_SHARED, fd, offset);
	close(fd);
	if (!base) {
		log_send(LOG_T_ERROR, "Mapping GPIO failed");
		return NULL;
	}
	log_send(LOG_T_DEBUG, "%s: GPIO%d (0x%"PRIx32") mapped @ %p", name, 
	    gpi_nr, offset, base);
	
	reg = am335x_read32(base, AM335X_GPIO_REG_REVISION);
	log_send(LOG_T_DEBUG, 
	    "%s: REVISION: 0x%" PRIx32 ": func: 0x%03x rev: %d.%d",
	    name, reg, AM335X_GPIO_REVISION_FUNC_GET(reg),
	    AM335X_GPIO_REVISION_X_MAJOR_GET(reg),
	    AM335X_GPIO_REVISION_Y_MINOR_GET(reg));
	if (AM335X_GPIO_REVISION_FUNC_GET(reg) != AM335X_GPIO_REVISION_FUNC) {
		log_send(LOG_T_ERROR, "Unexpected functional number");
		goto err_rev;
	}

	if (!(gpi = controller_block_alloc("am355x_gpi", name, sizeof(struct controller_block_private))))
		goto err_alloc;

	if (controller_block_outterm_list_init(gpi, outterms))
		goto err_outterm;

	gpi->private->base = base;
	gpi->private->bitmask = 1 << pin_nr;
	notpin = ~gpi->private->bitmask;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
136
137
	am335x_write32(base, AM335X_GPIO_REG_CTRL, 0);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
	am335x_write32(base, AM335X_GPIO_REG_LEVELDETECT0,
	    am335x_read32(base, AM335X_GPIO_REG_LEVELDETECT0) & notpin);
	am335x_write32(base, AM335X_GPIO_REG_LEVELDETECT1,
	    am335x_read32(base, AM335X_GPIO_REG_LEVELDETECT1) & notpin);
	am335x_write32(base, AM335X_GPIO_REG_RISINGDETECT,
	    am335x_read32(base, AM335X_GPIO_REG_RISINGDETECT) & notpin);
	am335x_write32(base, AM335X_GPIO_REG_FALLINGDETECT,
	    am335x_read32(base, AM335X_GPIO_REG_FALLINGDETECT) & notpin);

	/* configure as output */
	am335x_write32(base, AM335X_GPIO_REG_OE,
	    am335x_read32(base, AM335X_GPIO_REG_OE) | gpi->private->bitmask);

	gpi->calculate = gpi_calculate;

	controller_block_add(gpi);
	return gpi;

err_outterm:
	controller_block_free(gpi);
err_alloc:
err_rev:
	munmap(base, AM335X_GPIO_SIZE);
	return NULL;
}