danube gpio clean up
[openwrt.git] / target / linux / danube / files / drivers / char / danube_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/danube/danube.h>
30 #include <asm/danube/danube_ioctl.h>
31
32 #define MAX_PORTS 2
33 #define PINS_PER_PORT 16
34
35 static unsigned int danube_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 DANUBE_GPIO_SANITY {if (port > MAX_PORTS || pin > PINS_PER_PORT) return -EINVAL; }
42
43 int
44 danube_port_reserve_pin (unsigned int port, unsigned int pin)
45 {
46 DANUBE_GPIO_SANITY;
47 printk("%s : call to obseleted function\n", __func__);
48
49 return 0;
50 }
51 EXPORT_SYMBOL(danube_port_reserve_pin);
52
53 int
54 danube_port_free_pin (unsigned int port, unsigned int pin)
55 {
56 DANUBE_GPIO_SANITY;
57 printk("%s : call to obseleted function\n", __func__);
58
59 return 0;
60 }
61 EXPORT_SYMBOL(danube_port_free_pin);
62
63 int
64 danube_port_set_open_drain (unsigned int port, unsigned int pin)
65 {
66 DANUBE_GPIO_SANITY;
67 writel(readl(DANUBE_GPIO_P0_OD + (port * 0x30)) | (1 << pin), DANUBE_GPIO_P0_OD);
68
69 return 0;
70 }
71 EXPORT_SYMBOL(danube_port_set_open_drain);
72
73 int
74 danube_port_clear_open_drain (unsigned int port, unsigned int pin)
75 {
76 DANUBE_GPIO_SANITY;
77 writel(readl(DANUBE_GPIO_P0_OD + (port * 0x30)) & ~(1 << pin), DANUBE_GPIO_P0_OD);
78
79 return 0;
80 }
81 EXPORT_SYMBOL(danube_port_clear_open_drain);
82
83 int
84 danube_port_set_pudsel (unsigned int port, unsigned int pin)
85 {
86 DANUBE_GPIO_SANITY;
87 writel(readl(DANUBE_GPIO_P0_PUDSEL + (port * 0x30)) | (1 << pin), DANUBE_GPIO_P0_PUDSEL);
88
89 return 0;
90 }
91 EXPORT_SYMBOL(danube_port_set_pudsel);
92
93 int
94 danube_port_clear_pudsel (unsigned int port, unsigned int pin)
95 {
96 DANUBE_GPIO_SANITY;
97 writel(readl(DANUBE_GPIO_P0_PUDSEL + (port * 0x30)) & ~(1 << pin), DANUBE_GPIO_P0_PUDSEL);
98
99 return 0;
100 }
101 EXPORT_SYMBOL(danube_port_clear_pudsel);
102
103 int
104 danube_port_set_puden (unsigned int port, unsigned int pin)
105 {
106 DANUBE_GPIO_SANITY;
107 writel(readl(DANUBE_GPIO_P0_PUDEN + (port * 0x30)) | (1 << pin), DANUBE_GPIO_P0_PUDEN);
108
109 return 0;
110 }
111 EXPORT_SYMBOL(danube_port_set_puden);
112
113 int
114 danube_port_clear_puden (unsigned int port, unsigned int pin)
115 {
116 DANUBE_GPIO_SANITY;
117 writel(readl(DANUBE_GPIO_P0_PUDEN + (port * 0x30)) & ~(1 << pin), DANUBE_GPIO_P0_PUDEN);
118
119 return 0;
120 }
121 EXPORT_SYMBOL(danube_port_clear_puden);
122
123 int
124 danube_port_set_stoff (unsigned int port, unsigned int pin)
125 {
126 DANUBE_GPIO_SANITY;
127 writel(readl(DANUBE_GPIO_P0_STOFF + (port * 0x30)) | (1 << pin), DANUBE_GPIO_P0_STOFF);
128
129 return 0;
130 }
131 EXPORT_SYMBOL(danube_port_set_stoff);
132
133 int
134 danube_port_clear_stoff (unsigned int port, unsigned int pin)
135 {
136 DANUBE_GPIO_SANITY;
137 writel(readl(DANUBE_GPIO_P0_STOFF + (port * 0x30)) & ~(1 << pin), DANUBE_GPIO_P0_STOFF);
138
139 return 0;
140 }
141 EXPORT_SYMBOL(danube_port_clear_stoff);
142
143 int
144 danube_port_set_dir_out (unsigned int port, unsigned int pin)
145 {
146 DANUBE_GPIO_SANITY;
147 writel(readl(DANUBE_GPIO_P0_DIR + (port * 0x30)) | (1 << pin), DANUBE_GPIO_P0_DIR);
148
149 return 0;
150 }
151 EXPORT_SYMBOL(danube_port_set_dir_out);
152
153 int
154 danube_port_set_dir_in (unsigned int port, unsigned int pin)
155 {
156 DANUBE_GPIO_SANITY;
157 writel(readl(DANUBE_GPIO_P0_DIR + (port * 0x30)) & ~(1 << pin), DANUBE_GPIO_P0_DIR);
158
159 return 0;
160 }
161 EXPORT_SYMBOL(danube_port_set_dir_in);
162
163 int
164 danube_port_set_output (unsigned int port, unsigned int pin)
165 {
166 DANUBE_GPIO_SANITY;
167 writel(readl(DANUBE_GPIO_P0_OUT + (port * 0x30)) | (1 << pin), DANUBE_GPIO_P0_OUT);
168
169 return 0;
170 }
171 EXPORT_SYMBOL(danube_port_set_output);
172
173 int
174 danube_port_clear_output (unsigned int port, unsigned int pin)
175 {
176 DANUBE_GPIO_SANITY;
177 writel(readl(DANUBE_GPIO_P0_OUT + (port * 0x30)) & ~(1 << pin), DANUBE_GPIO_P0_OUT);
178
179 return 0;
180 }
181 EXPORT_SYMBOL(danube_port_clear_output);
182
183 int
184 danube_port_get_input (unsigned int port, unsigned int pin)
185 {
186 DANUBE_GPIO_SANITY;
187
188 if (readl(DANUBE_GPIO_P0_IN + (port * 0x30)) & (1 << pin))
189 return 0;
190 else
191 return 1;
192 }
193 EXPORT_SYMBOL(danube_port_get_input);
194
195 int
196 danube_port_set_altsel0 (unsigned int port, unsigned int pin)
197 {
198 DANUBE_GPIO_SANITY;
199 writel(readl(DANUBE_GPIO_P0_ALTSEL0 + (port * 0x30)) | (1 << pin), DANUBE_GPIO_P0_ALTSEL0);
200
201 return 0;
202 }
203 EXPORT_SYMBOL(danube_port_set_altsel0);
204
205 int
206 danube_port_clear_altsel0 (unsigned int port, unsigned int pin)
207 {
208 DANUBE_GPIO_SANITY;
209 writel(readl(DANUBE_GPIO_P0_ALTSEL0 + (port * 0x30)) & ~(1 << pin), DANUBE_GPIO_P0_ALTSEL0);
210
211 return 0;
212 }
213 EXPORT_SYMBOL(danube_port_clear_altsel0);
214
215 int
216 danube_port_set_altsel1 (unsigned int port, unsigned int pin)
217 {
218 DANUBE_GPIO_SANITY;
219 writel(readl(DANUBE_GPIO_P0_ALTSEL1 + (port * 0x30)) | (1 << pin), DANUBE_GPIO_P0_ALTSEL1);
220
221 return 0;
222 }
223 EXPORT_SYMBOL(danube_port_set_altsel1);
224
225 int
226 danube_port_clear_altsel1 (unsigned int port, unsigned int pin)
227 {
228 DANUBE_GPIO_SANITY;
229 writel(readl(DANUBE_GPIO_P0_ALTSEL1 + (port * 0x30)) & ~(1 << pin), DANUBE_GPIO_P0_ALTSEL1);
230
231 return 0;
232 }
233 EXPORT_SYMBOL(danube_port_clear_altsel1);
234
235 long danube_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 danube_port_read_procmem (char *buf, char **start, off_t offset, int count,
256 int *eof, void *data)
257 {
258 long len = sprintf (buf, "\nDanube 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 += danube_port_read_procmem_helper("P0-OUT", DANUBE_GPIO_P0_OUT, &buf[len]);
268 len += danube_port_read_procmem_helper("P1-OUT", DANUBE_GPIO_P1_OUT, &buf[len]);
269 len += danube_port_read_procmem_helper("P0-IN ", DANUBE_GPIO_P0_IN, &buf[len]);
270 len += danube_port_read_procmem_helper("P1-IN ", DANUBE_GPIO_P1_IN, &buf[len]);
271 len += danube_port_read_procmem_helper("P0-DIR", DANUBE_GPIO_P0_DIR, &buf[len]);
272 len += danube_port_read_procmem_helper("P1-DIR", DANUBE_GPIO_P1_DIR, &buf[len]);
273 len += danube_port_read_procmem_helper("P0-STO ", DANUBE_GPIO_P0_STOFF, &buf[len]);
274 len += danube_port_read_procmem_helper("P1-STO ", DANUBE_GPIO_P1_STOFF, &buf[len]);
275 len += danube_port_read_procmem_helper("P0-PUDE", DANUBE_GPIO_P0_PUDEN, &buf[len]);
276 len += danube_port_read_procmem_helper("P1-PUDE", DANUBE_GPIO_P1_PUDEN, &buf[len]);
277 len += danube_port_read_procmem_helper("P0-OD ", DANUBE_GPIO_P0_OD, &buf[len]);
278 len += danube_port_read_procmem_helper("P1-OD ", DANUBE_GPIO_P1_OD, &buf[len]);
279 len += danube_port_read_procmem_helper("P0-PUDS", DANUBE_GPIO_P0_PUDSEL, &buf[len]);
280 len += danube_port_read_procmem_helper("P1-PUDS", DANUBE_GPIO_P1_PUDSEL, &buf[len]);
281 len += danube_port_read_procmem_helper("P0-ALT0", DANUBE_GPIO_P0_ALTSEL0, &buf[len]);
282 len += danube_port_read_procmem_helper("P1-ALT0", DANUBE_GPIO_P1_ALTSEL0, &buf[len]);
283 len += danube_port_read_procmem_helper("P0-ALT1", DANUBE_GPIO_P0_ALTSEL1, &buf[len]);
284 len += danube_port_read_procmem_helper("P1-ALT1", DANUBE_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 danube_port_open (struct inode *inode, struct file *filep)
294 {
295 return 0;
296 }
297
298 static int
299 danube_port_release (struct inode *inode, struct file *filelp)
300 {
301 return 0;
302 }
303
304 static int
305 danube_port_ioctl (struct inode *inode, struct file *filp,
306 unsigned int cmd, unsigned long arg)
307 {
308 int ret = 0;
309 volatile struct danube_port_ioctl_parm parm;
310
311 if (_IOC_TYPE (cmd) != DANUBE_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 danube_port_ioctl_parm)))
318 return -EFAULT;
319 ret = copy_from_user ((void *) &parm, (void *) arg,
320 sizeof (struct danube_port_ioctl_parm));
321 }
322 if (_IOC_DIR (cmd) & _IOC_READ) {
323 if (!access_ok
324 (VERIFY_WRITE, arg,
325 sizeof (struct danube_port_ioctl_parm)))
326 return -EFAULT;
327 }
328
329 if (down_trylock (&port_sem) != 0)
330 return -EBUSY;
331
332 switch (cmd) {
333 case DANUBE_PORT_IOCOD:
334 if (parm.value == 0x00)
335 danube_port_clear_open_drain(parm.port, parm.pin);
336 else
337 danube_port_set_open_drain(parm.port, parm.pin);
338 break;
339
340 case DANUBE_PORT_IOCPUDSEL:
341 if (parm.value == 0x00)
342 danube_port_clear_pudsel(parm.port, parm.pin);
343 else
344 danube_port_set_pudsel(parm.port, parm.pin);
345 break;
346
347 case DANUBE_PORT_IOCPUDEN:
348 if (parm.value == 0x00)
349 danube_port_clear_puden(parm.port, parm.pin);
350 else
351 danube_port_set_puden(parm.port, parm.pin);
352 break;
353
354 case DANUBE_PORT_IOCSTOFF:
355 if (parm.value == 0x00)
356 danube_port_clear_stoff(parm.port, parm.pin);
357 else
358 danube_port_set_stoff(parm.port, parm.pin);
359 break;
360
361 case DANUBE_PORT_IOCDIR:
362 if (parm.value == 0x00)
363 danube_port_set_dir_in(parm.port, parm.pin);
364 else
365 danube_port_set_dir_out(parm.port, parm.pin);
366 break;
367
368 case DANUBE_PORT_IOCOUTPUT:
369 if (parm.value == 0x00)
370 danube_port_clear_output(parm.port, parm.pin);
371 else
372 danube_port_set_output(parm.port, parm.pin);
373 break;
374
375 case DANUBE_PORT_IOCALTSEL0:
376 if (parm.value == 0x00)
377 danube_port_clear_altsel0(parm.port, parm.pin);
378 else
379 danube_port_set_altsel0(parm.port, parm.pin);
380 break;
381
382 case DANUBE_PORT_IOCALTSEL1:
383 if (parm.value == 0x00)
384 danube_port_clear_altsel1(parm.port, parm.pin);
385 else
386 danube_port_set_altsel1(parm.port, parm.pin);
387 break;
388
389 case DANUBE_PORT_IOCINPUT:
390 parm.value = danube_port_get_input(parm.port, parm.pin);
391 copy_to_user((void*)arg, (void*)&parm,
392 sizeof(struct danube_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 = danube_port_open,
406 .release = danube_port_release,
407 .ioctl = danube_port_ioctl
408 };
409
410 int __init
411 danube_gpio_init (void)
412 {
413 int retval = 0;
414
415 sema_init (&port_sem, 1);
416
417 danube_gpio_major = register_chrdev(0, "danube_gpio", &port_fops);
418 if (!danube_gpio_major)
419 {
420 printk("danube-port: Error! Could not register port device. #%d\n", danube_gpio_major);
421 retval = -EINVAL;
422 goto out;
423 }
424
425 create_proc_read_entry("danube_gpio", 0, NULL,
426 danube_port_read_procmem, NULL);
427
428 printk("registered danube gpio driver\n");
429
430 out:
431 return retval;
432 }
433
434 void __exit
435 danube_gpio_exit (void)
436 {
437 unregister_chrdev(danube_gpio_major, "danube_gpio");
438 remove_proc_entry("danube_gpio", NULL);
439 }
440
441 module_init(danube_gpio_init);
442 module_exit(danube_gpio_exit);
This page took 0.060603 seconds and 5 git commands to generate.