more danube 2 ifxmips transitions
[openwrt/svn-archive/archive.git] / target / linux / ifxmips / files / drivers / char / ifxmips_gpio.c
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
15 *
16 * Copyright (C) 2005 infineon
17 * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
18 *
19 */
20
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/errno.h>
24 #include <linux/proc_fs.h>
25 #include <linux/init.h>
26 #include <linux/ioctl.h>
27 #include <asm/semaphore.h>
28 #include <asm/uaccess.h>
29 #include <asm/ifxmips/ifxmips.h>
30 #include <asm/ifxmips/ifxmips_ioctl.h>
31
32 #define MAX_PORTS 2
33 #define PINS_PER_PORT 16
34
35 static unsigned int ifxmips_gpio_major = 0;
36
37 /* TODO do we need this ? */
38 static struct semaphore port_sem;
39
40 /* TODO do we really need this ? return in a define is forbidden by coding style */
41 #define IFXMIPS_GPIO_SANITY {if (port > MAX_PORTS || pin > PINS_PER_PORT) return -EINVAL; }
42
43 int
44 ifxmips_port_reserve_pin (unsigned int port, unsigned int pin)
45 {
46 IFXMIPS_GPIO_SANITY;
47 printk("%s : call to obseleted function\n", __func__);
48
49 return 0;
50 }
51 EXPORT_SYMBOL(ifxmips_port_reserve_pin);
52
53 int
54 ifxmips_port_free_pin (unsigned int port, unsigned int pin)
55 {
56 IFXMIPS_GPIO_SANITY;
57 printk("%s : call to obseleted function\n", __func__);
58
59 return 0;
60 }
61 EXPORT_SYMBOL(ifxmips_port_free_pin);
62
63 int
64 ifxmips_port_set_open_drain (unsigned int port, unsigned int pin)
65 {
66 IFXMIPS_GPIO_SANITY;
67 writel(readl(IFXMIPS_GPIO_P0_OD + (port * 0x30)) | (1 << pin), IFXMIPS_GPIO_P0_OD);
68
69 return 0;
70 }
71 EXPORT_SYMBOL(ifxmips_port_set_open_drain);
72
73 int
74 ifxmips_port_clear_open_drain (unsigned int port, unsigned int pin)
75 {
76 IFXMIPS_GPIO_SANITY;
77 writel(readl(IFXMIPS_GPIO_P0_OD + (port * 0x30)) & ~(1 << pin), IFXMIPS_GPIO_P0_OD);
78
79 return 0;
80 }
81 EXPORT_SYMBOL(ifxmips_port_clear_open_drain);
82
83 int
84 ifxmips_port_set_pudsel (unsigned int port, unsigned int pin)
85 {
86 IFXMIPS_GPIO_SANITY;
87 writel(readl(IFXMIPS_GPIO_P0_PUDSEL + (port * 0x30)) | (1 << pin), IFXMIPS_GPIO_P0_PUDSEL);
88
89 return 0;
90 }
91 EXPORT_SYMBOL(ifxmips_port_set_pudsel);
92
93 int
94 ifxmips_port_clear_pudsel (unsigned int port, unsigned int pin)
95 {
96 IFXMIPS_GPIO_SANITY;
97 writel(readl(IFXMIPS_GPIO_P0_PUDSEL + (port * 0x30)) & ~(1 << pin), IFXMIPS_GPIO_P0_PUDSEL);
98
99 return 0;
100 }
101 EXPORT_SYMBOL(ifxmips_port_clear_pudsel);
102
103 int
104 ifxmips_port_set_puden (unsigned int port, unsigned int pin)
105 {
106 IFXMIPS_GPIO_SANITY;
107 writel(readl(IFXMIPS_GPIO_P0_PUDEN + (port * 0x30)) | (1 << pin), IFXMIPS_GPIO_P0_PUDEN);
108
109 return 0;
110 }
111 EXPORT_SYMBOL(ifxmips_port_set_puden);
112
113 int
114 ifxmips_port_clear_puden (unsigned int port, unsigned int pin)
115 {
116 IFXMIPS_GPIO_SANITY;
117 writel(readl(IFXMIPS_GPIO_P0_PUDEN + (port * 0x30)) & ~(1 << pin), IFXMIPS_GPIO_P0_PUDEN);
118
119 return 0;
120 }
121 EXPORT_SYMBOL(ifxmips_port_clear_puden);
122
123 int
124 ifxmips_port_set_stoff (unsigned int port, unsigned int pin)
125 {
126 IFXMIPS_GPIO_SANITY;
127 writel(readl(IFXMIPS_GPIO_P0_STOFF + (port * 0x30)) | (1 << pin), IFXMIPS_GPIO_P0_STOFF);
128
129 return 0;
130 }
131 EXPORT_SYMBOL(ifxmips_port_set_stoff);
132
133 int
134 ifxmips_port_clear_stoff (unsigned int port, unsigned int pin)
135 {
136 IFXMIPS_GPIO_SANITY;
137 writel(readl(IFXMIPS_GPIO_P0_STOFF + (port * 0x30)) & ~(1 << pin), IFXMIPS_GPIO_P0_STOFF);
138
139 return 0;
140 }
141 EXPORT_SYMBOL(ifxmips_port_clear_stoff);
142
143 int
144 ifxmips_port_set_dir_out (unsigned int port, unsigned int pin)
145 {
146 IFXMIPS_GPIO_SANITY;
147 writel(readl(IFXMIPS_GPIO_P0_DIR + (port * 0x30)) | (1 << pin), IFXMIPS_GPIO_P0_DIR);
148
149 return 0;
150 }
151 EXPORT_SYMBOL(ifxmips_port_set_dir_out);
152
153 int
154 ifxmips_port_set_dir_in (unsigned int port, unsigned int pin)
155 {
156 IFXMIPS_GPIO_SANITY;
157 writel(readl(IFXMIPS_GPIO_P0_DIR + (port * 0x30)) & ~(1 << pin), IFXMIPS_GPIO_P0_DIR);
158
159 return 0;
160 }
161 EXPORT_SYMBOL(ifxmips_port_set_dir_in);
162
163 int
164 ifxmips_port_set_output (unsigned int port, unsigned int pin)
165 {
166 IFXMIPS_GPIO_SANITY;
167 writel(readl(IFXMIPS_GPIO_P0_OUT + (port * 0x30)) | (1 << pin), IFXMIPS_GPIO_P0_OUT);
168
169 return 0;
170 }
171 EXPORT_SYMBOL(ifxmips_port_set_output);
172
173 int
174 ifxmips_port_clear_output (unsigned int port, unsigned int pin)
175 {
176 IFXMIPS_GPIO_SANITY;
177 writel(readl(IFXMIPS_GPIO_P0_OUT + (port * 0x30)) & ~(1 << pin), IFXMIPS_GPIO_P0_OUT);
178
179 return 0;
180 }
181 EXPORT_SYMBOL(ifxmips_port_clear_output);
182
183 int
184 ifxmips_port_get_input (unsigned int port, unsigned int pin)
185 {
186 IFXMIPS_GPIO_SANITY;
187
188 if (readl(IFXMIPS_GPIO_P0_IN + (port * 0x30)) & (1 << pin))
189 return 0;
190 else
191 return 1;
192 }
193 EXPORT_SYMBOL(ifxmips_port_get_input);
194
195 int
196 ifxmips_port_set_altsel0 (unsigned int port, unsigned int pin)
197 {
198 IFXMIPS_GPIO_SANITY;
199 writel(readl(IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0x30)) | (1 << pin), IFXMIPS_GPIO_P0_ALTSEL0);
200
201 return 0;
202 }
203 EXPORT_SYMBOL(ifxmips_port_set_altsel0);
204
205 int
206 ifxmips_port_clear_altsel0 (unsigned int port, unsigned int pin)
207 {
208 IFXMIPS_GPIO_SANITY;
209 writel(readl(IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0x30)) & ~(1 << pin), IFXMIPS_GPIO_P0_ALTSEL0);
210
211 return 0;
212 }
213 EXPORT_SYMBOL(ifxmips_port_clear_altsel0);
214
215 int
216 ifxmips_port_set_altsel1 (unsigned int port, unsigned int pin)
217 {
218 IFXMIPS_GPIO_SANITY;
219 writel(readl(IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0x30)) | (1 << pin), IFXMIPS_GPIO_P0_ALTSEL1);
220
221 return 0;
222 }
223 EXPORT_SYMBOL(ifxmips_port_set_altsel1);
224
225 int
226 ifxmips_port_clear_altsel1 (unsigned int port, unsigned int pin)
227 {
228 IFXMIPS_GPIO_SANITY;
229 writel(readl(IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0x30)) & ~(1 << pin), IFXMIPS_GPIO_P0_ALTSEL1);
230
231 return 0;
232 }
233 EXPORT_SYMBOL(ifxmips_port_clear_altsel1);
234
235 long ifxmips_port_read_procmem_helper(char* tag, u32* in_reg, char *buf)
236 {
237 u32 reg, bit = 0;
238 unsigned int len, t;
239
240 len = sprintf(buf, "\n%s: ", tag);
241 reg = readl(in_reg);
242 bit = 0x80000000;
243 for (t = 0; t < 32; t++) {
244 if ((reg & bit) > 0)
245 len = len + sprintf(buf + len, "X");
246 else
247 len = len + sprintf(buf + len, " ");
248 bit = bit >> 1;
249 }
250
251 return len;
252 }
253
254 int
255 ifxmips_port_read_procmem (char *buf, char **start, off_t offset, int count,
256 int *eof, void *data)
257 {
258 long len = sprintf (buf, "\nIFXMips Port Settings\n");
259
260 len += sprintf (buf + len,
261 " 3 2 1 0\n");
262 len += sprintf (buf + len,
263 " 10987654321098765432109876543210\n");
264 len += sprintf (buf + len,
265 "----------------------------------------\n");
266
267 len += ifxmips_port_read_procmem_helper("P0-OUT", IFXMIPS_GPIO_P0_OUT, &buf[len]);
268 len += ifxmips_port_read_procmem_helper("P1-OUT", IFXMIPS_GPIO_P1_OUT, &buf[len]);
269 len += ifxmips_port_read_procmem_helper("P0-IN ", IFXMIPS_GPIO_P0_IN, &buf[len]);
270 len += ifxmips_port_read_procmem_helper("P1-IN ", IFXMIPS_GPIO_P1_IN, &buf[len]);
271 len += ifxmips_port_read_procmem_helper("P0-DIR", IFXMIPS_GPIO_P0_DIR, &buf[len]);
272 len += ifxmips_port_read_procmem_helper("P1-DIR", IFXMIPS_GPIO_P1_DIR, &buf[len]);
273 len += ifxmips_port_read_procmem_helper("P0-STO ", IFXMIPS_GPIO_P0_STOFF, &buf[len]);
274 len += ifxmips_port_read_procmem_helper("P1-STO ", IFXMIPS_GPIO_P1_STOFF, &buf[len]);
275 len += ifxmips_port_read_procmem_helper("P0-PUDE", IFXMIPS_GPIO_P0_PUDEN, &buf[len]);
276 len += ifxmips_port_read_procmem_helper("P1-PUDE", IFXMIPS_GPIO_P1_PUDEN, &buf[len]);
277 len += ifxmips_port_read_procmem_helper("P0-OD ", IFXMIPS_GPIO_P0_OD, &buf[len]);
278 len += ifxmips_port_read_procmem_helper("P1-OD ", IFXMIPS_GPIO_P1_OD, &buf[len]);
279 len += ifxmips_port_read_procmem_helper("P0-PUDS", IFXMIPS_GPIO_P0_PUDSEL, &buf[len]);
280 len += ifxmips_port_read_procmem_helper("P1-PUDS", IFXMIPS_GPIO_P1_PUDSEL, &buf[len]);
281 len += ifxmips_port_read_procmem_helper("P0-ALT0", IFXMIPS_GPIO_P0_ALTSEL0, &buf[len]);
282 len += ifxmips_port_read_procmem_helper("P1-ALT0", IFXMIPS_GPIO_P1_ALTSEL0, &buf[len]);
283 len += ifxmips_port_read_procmem_helper("P0-ALT1", IFXMIPS_GPIO_P0_ALTSEL1, &buf[len]);
284 len += ifxmips_port_read_procmem_helper("P1-ALT1", IFXMIPS_GPIO_P1_ALTSEL1, &buf[len]);
285 len = len + sprintf (buf + len, "\n\n");
286
287 *eof = 1;
288
289 return len;
290 }
291
292 static int
293 ifxmips_port_open (struct inode *inode, struct file *filep)
294 {
295 return 0;
296 }
297
298 static int
299 ifxmips_port_release (struct inode *inode, struct file *filelp)
300 {
301 return 0;
302 }
303
304 static int
305 ifxmips_port_ioctl (struct inode *inode, struct file *filp,
306 unsigned int cmd, unsigned long arg)
307 {
308 int ret = 0;
309 volatile struct ifxmips_port_ioctl_parm parm;
310
311 if (_IOC_TYPE (cmd) != IFXMIPS_PORT_IOC_MAGIC)
312 return -EINVAL;
313
314 if (_IOC_DIR (cmd) & _IOC_WRITE) {
315 if (!access_ok
316 (VERIFY_READ, arg,
317 sizeof (struct ifxmips_port_ioctl_parm)))
318 return -EFAULT;
319 ret = copy_from_user ((void *) &parm, (void *) arg,
320 sizeof (struct ifxmips_port_ioctl_parm));
321 }
322 if (_IOC_DIR (cmd) & _IOC_READ) {
323 if (!access_ok
324 (VERIFY_WRITE, arg,
325 sizeof (struct ifxmips_port_ioctl_parm)))
326 return -EFAULT;
327 }
328
329 if (down_trylock (&port_sem) != 0)
330 return -EBUSY;
331
332 switch (cmd) {
333 case IFXMIPS_PORT_IOCOD:
334 if (parm.value == 0x00)
335 ifxmips_port_clear_open_drain(parm.port, parm.pin);
336 else
337 ifxmips_port_set_open_drain(parm.port, parm.pin);
338 break;
339
340 case IFXMIPS_PORT_IOCPUDSEL:
341 if (parm.value == 0x00)
342 ifxmips_port_clear_pudsel(parm.port, parm.pin);
343 else
344 ifxmips_port_set_pudsel(parm.port, parm.pin);
345 break;
346
347 case IFXMIPS_PORT_IOCPUDEN:
348 if (parm.value == 0x00)
349 ifxmips_port_clear_puden(parm.port, parm.pin);
350 else
351 ifxmips_port_set_puden(parm.port, parm.pin);
352 break;
353
354 case IFXMIPS_PORT_IOCSTOFF:
355 if (parm.value == 0x00)
356 ifxmips_port_clear_stoff(parm.port, parm.pin);
357 else
358 ifxmips_port_set_stoff(parm.port, parm.pin);
359 break;
360
361 case IFXMIPS_PORT_IOCDIR:
362 if (parm.value == 0x00)
363 ifxmips_port_set_dir_in(parm.port, parm.pin);
364 else
365 ifxmips_port_set_dir_out(parm.port, parm.pin);
366 break;
367
368 case IFXMIPS_PORT_IOCOUTPUT:
369 if (parm.value == 0x00)
370 ifxmips_port_clear_output(parm.port, parm.pin);
371 else
372 ifxmips_port_set_output(parm.port, parm.pin);
373 break;
374
375 case IFXMIPS_PORT_IOCALTSEL0:
376 if (parm.value == 0x00)
377 ifxmips_port_clear_altsel0(parm.port, parm.pin);
378 else
379 ifxmips_port_set_altsel0(parm.port, parm.pin);
380 break;
381
382 case IFXMIPS_PORT_IOCALTSEL1:
383 if (parm.value == 0x00)
384 ifxmips_port_clear_altsel1(parm.port, parm.pin);
385 else
386 ifxmips_port_set_altsel1(parm.port, parm.pin);
387 break;
388
389 case IFXMIPS_PORT_IOCINPUT:
390 parm.value = ifxmips_port_get_input(parm.port, parm.pin);
391 copy_to_user((void*)arg, (void*)&parm,
392 sizeof(struct ifxmips_port_ioctl_parm));
393 break;
394
395 default:
396 ret = -EINVAL;
397 }
398
399 up (&port_sem);
400
401 return ret;
402 }
403
404 static struct file_operations port_fops = {
405 .open = ifxmips_port_open,
406 .release = ifxmips_port_release,
407 .ioctl = ifxmips_port_ioctl
408 };
409
410 int __init
411 ifxmips_gpio_init (void)
412 {
413 int retval = 0;
414
415 sema_init (&port_sem, 1);
416
417 ifxmips_gpio_major = register_chrdev(0, "ifxmips_gpio", &port_fops);
418 if (!ifxmips_gpio_major)
419 {
420 printk("ifxmips-port: Error! Could not register port device. #%d\n", ifxmips_gpio_major);
421 retval = -EINVAL;
422 goto out;
423 }
424
425 create_proc_read_entry("ifxmips_gpio", 0, NULL,
426 ifxmips_port_read_procmem, NULL);
427
428 printk("registered ifxmips gpio driver\n");
429
430 out:
431 return retval;
432 }
433
434 void __exit
435 ifxmips_gpio_exit (void)
436 {
437 unregister_chrdev(ifxmips_gpio_major, "ifxmips_gpio");
438 remove_proc_entry("ifxmips_gpio", NULL);
439 }
440
441 module_init(ifxmips_gpio_init);
442 module_exit(ifxmips_gpio_exit);