summaryrefslogtreecommitdiff
path: root/recipes/linux/gumstix-kernel-2.6.21/proc-gpio.patch
blob: 51c5c7242b3b06b8b7ccc85d0537f083c34be587 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
Index: linux-2.6.21gum/arch/arm/Kconfig
===================================================================
--- linux-2.6.21gum.orig/arch/arm/Kconfig
+++ linux-2.6.21gum/arch/arm/Kconfig
@@ -505,6 +505,8 @@ config PCI_HOST_VIA82C505
 	depends on PCI && ARCH_SHARK
 	default y
 
+source "drivers/gpio/Kconfig"
+
 source "drivers/pci/Kconfig"
 
 source "drivers/pcmcia/Kconfig"
Index: linux-2.6.21gum/drivers/Makefile
===================================================================
--- linux-2.6.21gum.orig/drivers/Makefile
+++ linux-2.6.21gum/drivers/Makefile
@@ -81,3 +81,4 @@ obj-$(CONFIG_GENERIC_TIME)	+= clocksourc
 obj-$(CONFIG_DMA_ENGINE)	+= dma/
 obj-$(CONFIG_HID)		+= hid/
 obj-$(CONFIG_PPC_PS3)		+= ps3/
+obj-$(CONFIG_PROC_GPIO)		+= gpio/
Index: linux-2.6.21gum/drivers/gpio/Kconfig
===================================================================
--- /dev/null
+++ linux-2.6.21gum/drivers/gpio/Kconfig
@@ -0,0 +1,12 @@
+config PROC_GPIO
+	tristate "GPIO /proc interface"
+	depends on PXA25x || PXA27x
+	help
+	  This enables an interface under /proc/gpio which allows reading or setting
+	  of any GPIO, and also changing the GPIO alt function mode of any line.
+
+config PROC_GPIO_DEBUG
+	boolean "Enable /proc/gpio debug logging"
+	depends on PROC_GPIO
+	help
+	  This enables printk logging of activity done through /proc/gpio
Index: linux-2.6.21gum/drivers/gpio/Makefile
===================================================================
--- /dev/null
+++ linux-2.6.21gum/drivers/gpio/Makefile
@@ -0,0 +1,3 @@
+# Expose GPIOs under /proc
+obj-$(CONFIG_PROC_GPIO)			+= proc_gpio.o
+
Index: linux-2.6.21gum/drivers/gpio/proc_gpio.c
===================================================================
--- /dev/null
+++ linux-2.6.21gum/drivers/gpio/proc_gpio.c
@@ -0,0 +1,296 @@
+/*
+ *
+ *  PXA25x GPIOs exposed under /proc for reading and writing
+ *  They will show up under /proc/gpio/NN
+ *
+ *  Based on patch 1773/1 in the arm kernel patch repository at arm.linux.co.uk
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/proc_fs.h>
+#include <linux/string.h>
+#include <linux/ctype.h>
+
+#include <asm/hardware.h>
+#include <asm/arch/pxa-regs.h>
+#include <asm/uaccess.h>
+
+static struct proc_dir_entry *proc_gpio_parent;
+static struct proc_dir_entry *proc_gpios[PXA_LAST_GPIO + 1];
+
+typedef struct
+{
+	int	gpio;
+	char    name[32];
+} gpio_summary_type;
+
+static gpio_summary_type gpio_summaries[PXA_LAST_GPIO + 1];
+
+static int proc_gpio_write(struct file *file, const char __user *buf,
+                           unsigned long count, void *data)
+{
+	char *cur, lbuf[count + 1];
+	gpio_summary_type *summary = data;
+	u32 altfn, direction, setclear, gafr;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EACCES;
+
+	memset(lbuf, 0, count + 1);
+
+	if (copy_from_user(lbuf, buf, count))
+		return -EFAULT;
+
+	cur = lbuf;
+
+	// Initialize to current state
+	altfn = ((GAFR(summary->gpio) >> ((summary->gpio & 0x0f) << 0x01)) & 0x03);
+	direction = GPDR(summary->gpio) & GPIO_bit(summary->gpio);
+	setclear = GPLR(summary->gpio) & GPIO_bit(summary->gpio);
+	while(1)
+	{
+		// We accept options: {GPIO|AF1|AF2|AF3}, {set|clear}, {in|out}
+		// Anything else is an error
+		while(cur[0] && (isspace(cur[0]) || ispunct(cur[0]))) cur = &(cur[1]);
+
+		if('\0' == cur[0]) break;
+
+		// Ok, so now we're pointing at the start of something
+		switch(cur[0])
+		{
+			case 'G':
+				// Check that next is "PIO" -- '\0' will cause safe short-circuit if end of buf
+				if(!(cur[1] == 'P' && cur[2] == 'I' && cur[3] == 'O')) goto parse_error;
+				// Ok, so set this GPIO to GPIO (non-ALT) function
+				altfn = 0;
+				cur = &(cur[4]);
+				break;
+			case 'A':
+				if(!(cur[1] == 'F' && cur[2] >= '1' && cur[2] <= '3')) goto parse_error;
+				altfn = cur[2] - '0';
+				cur = &(cur[3]);
+				break;
+			case 's':
+				if(!(cur[1] == 'e' && cur[2] == 't')) goto parse_error;
+				setclear = 1;
+				cur = &(cur[3]);
+				break;
+			case 'c':
+				if(!(cur[1] == 'l' && cur[2] == 'e' && cur[3] == 'a' && cur[4] == 'r')) goto parse_error;
+				setclear = 0;
+				cur = &(cur[5]);
+				break;
+			case 'i':
+				if(!(cur[1] == 'n')) goto parse_error;
+				direction = 0;
+				cur = &(cur[2]);
+				break;
+			case 'o':
+				if(!(cur[1] == 'u' && cur[2] == 't')) goto parse_error;
+				direction = 1;
+				cur = &(cur[3]);
+				break;
+			default: goto parse_error;
+		}
+	}
+	// Ok, now set gpio mode and value
+	if(direction)
+		GPDR(summary->gpio) |= GPIO_bit(summary->gpio);
+	else
+		GPDR(summary->gpio) &= ~GPIO_bit(summary->gpio);
+
+	gafr = GAFR(summary->gpio) & ~(0x3 << (((summary->gpio) & 0xf)*2));
+	GAFR(summary->gpio) = gafr |  (altfn  << (((summary->gpio) & 0xf)*2));
+
+	if(direction && !altfn)
+	{
+		if(setclear) GPSR(summary->gpio) = GPIO_bit(summary->gpio);
+		else GPCR(summary->gpio) = GPIO_bit(summary->gpio);
+	}
+
+#ifdef CONFIG_PROC_GPIO_DEBUG
+	printk(KERN_INFO "Set (%s,%s,%s) via /proc/gpio/%s\n",altfn ? (altfn == 1 ? "AF1" : (altfn == 2 ? "AF2" : "AF3")) : "GPIO",
+				direction ? "out" : "in",
+				setclear ? "set" : "clear",
+				summary->name);
+#endif
+
+	return count;
+
+parse_error:
+	printk(KERN_CRIT "Parse error: Expect \"[GPIO|AF1|AF2|AF3]|[set|clear]|[in|out] ...\"\n");
+	return -EINVAL;
+}
+
+static int proc_gpio_read(char *page, char **start, off_t off,
+			int count, int *eof, void *data)
+{
+	char *p = page;
+	gpio_summary_type *summary = data;
+	int len, i, af;
+	i = summary->gpio;
+
+	p += sprintf(p, "%d\t%s\t%s\t%s\n", i,
+			(af = ((GAFR(i) >> ((i & 0x0f) << 0x01)) & 0x03)) ? (af == 1 ? "AF1" : (af == 2 ? "AF2" : "AF3")) : "GPIO",
+			(GPDR(i) & GPIO_bit(i)) ? "out" : "in",
+			(GPLR(i) & GPIO_bit(i)) ? "set" : "clear");
+
+	len = (p - page) - off;
+
+	if(len < 0)
+	{
+		len = 0;
+	}
+
+	*eof = (len <= count) ? 1 : 0;
+	*start = page + off;
+
+	return len;
+}
+
+
+#ifdef CONFIG_PXA25x
+static const char const *GAFR_DESC[] = { "GAFR0_L", "GAFR0_U", "GAFR1_L", "GAFR1_U", "GAFR2_L", "GAFR2_U" };
+#elif defined(CONFIG_PXA27x)
+static const char const *GAFR_DESC[] = { "GAFR0_L", "GAFR0_U", "GAFR1_L", "GAFR1_U", "GAFR2_L", "GAFR2_U", "GAFR3_L", "GAFR3_U" };
+#endif
+
+static int proc_gafr_read(char *page, char **start, off_t off,
+			int count, int *eof, void *data)
+{
+	char *p = page;
+	int i, len;
+
+	for(i=0; i<ARRAY_SIZE(GAFR_DESC); i++)
+	{
+		p += sprintf(p, "%s: %08x\n", GAFR_DESC[i], GAFR(i*16));
+	}
+
+	len = (p - page) - off;
+
+	if(len < 0)
+	{
+		len = 0;
+	}
+
+	*eof = (len <= count) ? 1 : 0;
+	*start = page + off;
+
+	return len;
+}
+
+static int proc_gpdr_read(char *page, char **start, off_t off,
+			int count, int *eof, void *data)
+{
+	char *p = page;
+	int i, len;
+
+	for(i=0; i<=2; i++)
+	{
+		p += sprintf(p, "GPDR%d: %08x\n", i, GPDR(i * 32));
+	}
+
+	len = (p - page) - off;
+
+	if(len < 0)
+	{
+		len = 0;
+	}
+
+	*eof = (len <= count) ? 1 : 0;
+	*start = page + off;
+
+	return len;
+}
+
+static int proc_gplr_read(char *page, char **start, off_t off,
+			int count, int *eof, void *data)
+{
+	char *p = page;
+	int i, len;
+
+	for(i=0; i<=2; i++)
+	{
+		p += sprintf(p, "GPLR%d: %08x\n", i, GPLR(i * 32));
+	}
+
+	len = (p - page) - off;
+
+	if(len < 0)
+	{
+		len = 0;
+	}
+
+	*eof = (len <= count) ? 1 : 0;
+	*start = page + off;
+
+	return len;
+}
+
+static int __init gpio_init(void)
+{
+	int i;
+
+  proc_gpio_parent = NULL;
+  for (proc_gpio_parent = proc_root.subdir; proc_gpio_parent; proc_gpio_parent = proc_gpio_parent->next) {
+    if (( proc_gpio_parent->namelen == 4 ) && ( memcmp(proc_gpio_parent->name, "gpio", 4 ) == 0 ))
+      break;
+  }
+
+  // proc_gpio_parent will be non-NULL if the directory already exists
+
+  if (!proc_gpio_parent) {
+       proc_gpio_parent = proc_gpio_parent = create_proc_entry("gpio", S_IFDIR | S_IRUGO | S_IXUGO, NULL);
+  }
+
+ 	if(!proc_gpio_parent) return 0;
+
+	for(i=0; i < (PXA_LAST_GPIO+1); i++)
+	{
+		gpio_summaries[i].gpio = i;
+		sprintf(gpio_summaries[i].name, "GPIO%d", i);
+		proc_gpios[i] = create_proc_entry(gpio_summaries[i].name, 0644, proc_gpio_parent);
+		if(proc_gpios[i])
+		{
+			proc_gpios[i]->data = &gpio_summaries[i];
+			proc_gpios[i]->read_proc = proc_gpio_read;
+			proc_gpios[i]->write_proc = proc_gpio_write;
+		}
+	}
+
+	create_proc_read_entry("GAFR", 0444, proc_gpio_parent, proc_gafr_read, NULL);
+	create_proc_read_entry("GPDR", 0444, proc_gpio_parent, proc_gpdr_read, NULL);
+	create_proc_read_entry("GPLR", 0444, proc_gpio_parent, proc_gplr_read, NULL);
+
+	return 0;
+}
+
+static void gpio_exit(void)
+{
+	int i;
+
+	remove_proc_entry("GAFR", proc_gpio_parent);
+	remove_proc_entry("GPDR", proc_gpio_parent);
+	remove_proc_entry("GPLR", proc_gpio_parent);
+
+	for(i=0; i < (PXA_LAST_GPIO+1); i++)
+	{
+		if(proc_gpios[i]) remove_proc_entry(gpio_summaries[i].name, proc_gpio_parent);
+	}
+
+  if (proc_gpio_parent)
+  {
+    if (!proc_gpio_parent->subdir)
+    {
+      // Only remove /proc/gpio if it's empty.
+      remove_proc_entry( "gpio", NULL );
+    }
+  }
+
+}
+
+module_init(gpio_init);
+module_exit(gpio_exit);
+MODULE_LICENSE("GPL");