Blame view

drivers/char/nsc_gpio.c 3.49 KB
1ca5df0a4   Jim Cromie   [PATCH] chardev: ...
1
2
3
4
5
6
7
8
  /* linux/drivers/char/nsc_gpio.c
  
     National Semiconductor common GPIO device-file/VFS methods.
     Allows a user space process to control the GPIO pins.
  
     Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
     Copyright (c) 2005      Jim Cromie <jim.cromie@gmail.com>
  */
1ca5df0a4   Jim Cromie   [PATCH] chardev: ...
9
10
11
12
13
14
  #include <linux/fs.h>
  #include <linux/module.h>
  #include <linux/errno.h>
  #include <linux/kernel.h>
  #include <linux/init.h>
  #include <linux/nsc_gpio.h>
f31000e57   Jim Cromie   [PATCH] chardev: ...
15
  #include <linux/platform_device.h>
1ca5df0a4   Jim Cromie   [PATCH] chardev: ...
16
17
18
19
  #include <asm/uaccess.h>
  #include <asm/io.h>
  
  #define NAME "nsc_gpio"
f31000e57   Jim Cromie   [PATCH] chardev: ...
20
  void nsc_gpio_dump(struct nsc_gpio_ops *amp, unsigned index)
0e41ef3c5   Jim Cromie   [PATCH] chardev: ...
21
  {
f31000e57   Jim Cromie   [PATCH] chardev: ...
22
23
24
25
  	/* retrieve current config w/o changing it */
  	u32 config = amp->gpio_config(index, ~0, 0);
  
  	/* user requested via 'v' command, so its INFO */
23916a8e3   Jim Cromie   [PATCH] chardev: ...
26
27
  	dev_info(amp->dev, "io%02u: 0x%04x %s %s %s %s %s %s %s\tio:%d/%d
  ",
f31000e57   Jim Cromie   [PATCH] chardev: ...
28
29
30
31
32
33
34
  		 index, config,
  		 (config & 1) ? "OE" : "TS",      /* output-enabled/tristate */
  		 (config & 2) ? "PP" : "OD",      /* push pull / open drain */
  		 (config & 4) ? "PUE" : "PUD",    /* pull up enabled/disabled */
  		 (config & 8) ? "LOCKED" : "",    /* locked / unlocked */
  		 (config & 16) ? "LEVEL" : "EDGE",/* level/edge input */
  		 (config & 32) ? "HI" : "LO",     /* trigger on rise/fall edge */
23916a8e3   Jim Cromie   [PATCH] chardev: ...
35
36
37
  		 (config & 64) ? "DEBOUNCE" : "", /* debounce */
  
  		 amp->gpio_get(index), amp->gpio_current(index));
0e41ef3c5   Jim Cromie   [PATCH] chardev: ...
38
  }
1a66fdf08   Jim Cromie   [PATCH] chardev: ...
39
40
41
  ssize_t nsc_gpio_write(struct file *file, const char __user *data,
  		       size_t len, loff_t *ppos)
  {
a7113a966   Josef Sipek   [PATCH] struct pa...
42
  	unsigned m = iminor(file->f_path.dentry->d_inode);
1a66fdf08   Jim Cromie   [PATCH] chardev: ...
43
  	struct nsc_gpio_ops *amp = file->private_data;
f31000e57   Jim Cromie   [PATCH] chardev: ...
44
  	struct device *dev = amp->dev;
1a66fdf08   Jim Cromie   [PATCH] chardev: ...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  	size_t i;
  	int err = 0;
  
  	for (i = 0; i < len; ++i) {
  		char c;
  		if (get_user(c, data + i))
  			return -EFAULT;
  		switch (c) {
  		case '0':
  			amp->gpio_set(m, 0);
  			break;
  		case '1':
  			amp->gpio_set(m, 1);
  			break;
  		case 'O':
f31000e57   Jim Cromie   [PATCH] chardev: ...
60
61
  			dev_dbg(dev, "GPIO%d output enabled
  ", m);
1a66fdf08   Jim Cromie   [PATCH] chardev: ...
62
63
64
  			amp->gpio_config(m, ~1, 1);
  			break;
  		case 'o':
f31000e57   Jim Cromie   [PATCH] chardev: ...
65
66
  			dev_dbg(dev, "GPIO%d output disabled
  ", m);
1a66fdf08   Jim Cromie   [PATCH] chardev: ...
67
68
69
  			amp->gpio_config(m, ~1, 0);
  			break;
  		case 'T':
abecb6da7   Jim Cromie   [PATCH] gpio: cos...
70
71
  			dev_dbg(dev, "GPIO%d output is push pull
  ", m);
1a66fdf08   Jim Cromie   [PATCH] chardev: ...
72
73
74
  			amp->gpio_config(m, ~2, 2);
  			break;
  		case 't':
abecb6da7   Jim Cromie   [PATCH] gpio: cos...
75
76
  			dev_dbg(dev, "GPIO%d output is open drain
  ", m);
1a66fdf08   Jim Cromie   [PATCH] chardev: ...
77
78
79
  			amp->gpio_config(m, ~2, 0);
  			break;
  		case 'P':
f31000e57   Jim Cromie   [PATCH] chardev: ...
80
81
  			dev_dbg(dev, "GPIO%d pull up enabled
  ", m);
1a66fdf08   Jim Cromie   [PATCH] chardev: ...
82
83
84
  			amp->gpio_config(m, ~4, 4);
  			break;
  		case 'p':
f31000e57   Jim Cromie   [PATCH] chardev: ...
85
86
  			dev_dbg(dev, "GPIO%d pull up disabled
  ", m);
1a66fdf08   Jim Cromie   [PATCH] chardev: ...
87
88
  			amp->gpio_config(m, ~4, 0);
  			break;
1a66fdf08   Jim Cromie   [PATCH] chardev: ...
89
90
  		case 'v':
  			/* View Current pin settings */
f31000e57   Jim Cromie   [PATCH] chardev: ...
91
  			amp->gpio_dump(amp, m);
1a66fdf08   Jim Cromie   [PATCH] chardev: ...
92
93
94
95
96
97
  			break;
  		case '
  ':
  			/* end of settings string, do nothing */
  			break;
  		default:
f31000e57   Jim Cromie   [PATCH] chardev: ...
98
99
100
  			dev_err(dev, "io%2d bad setting: chr<0x%2x>
  ",
  				m, (int)c);
1a66fdf08   Jim Cromie   [PATCH] chardev: ...
101
102
103
104
105
106
107
108
109
110
111
112
  			err++;
  		}
  	}
  	if (err)
  		return -EINVAL;	/* full string handled, report error */
  
  	return len;
  }
  
  ssize_t nsc_gpio_read(struct file *file, char __user * buf,
  		      size_t len, loff_t * ppos)
  {
a7113a966   Josef Sipek   [PATCH] struct pa...
113
  	unsigned m = iminor(file->f_path.dentry->d_inode);
1a66fdf08   Jim Cromie   [PATCH] chardev: ...
114
115
116
117
118
119
120
121
122
  	int value;
  	struct nsc_gpio_ops *amp = file->private_data;
  
  	value = amp->gpio_get(m);
  	if (put_user(value ? '1' : '0', buf))
  		return -EFAULT;
  
  	return 1;
  }
0e41ef3c5   Jim Cromie   [PATCH] chardev: ...
123
  /* common file-ops routines for both scx200_gpio and pc87360_gpio */
1a66fdf08   Jim Cromie   [PATCH] chardev: ...
124
125
  EXPORT_SYMBOL(nsc_gpio_write);
  EXPORT_SYMBOL(nsc_gpio_read);
0e41ef3c5   Jim Cromie   [PATCH] chardev: ...
126
  EXPORT_SYMBOL(nsc_gpio_dump);
1ca5df0a4   Jim Cromie   [PATCH] chardev: ...
127
128
129
130
131
132
133
134
135
136
137
138
139
  
  static int __init nsc_gpio_init(void)
  {
  	printk(KERN_DEBUG NAME " initializing
  ");
  	return 0;
  }
  
  static void __exit nsc_gpio_cleanup(void)
  {
  	printk(KERN_DEBUG NAME " cleanup
  ");
  }
1ca5df0a4   Jim Cromie   [PATCH] chardev: ...
140
141
  module_init(nsc_gpio_init);
  module_exit(nsc_gpio_cleanup);
1a66fdf08   Jim Cromie   [PATCH] chardev: ...
142
143
144
145
  
  MODULE_AUTHOR("Jim Cromie <jim.cromie@gmail.com>");
  MODULE_DESCRIPTION("NatSemi GPIO Common Methods");
  MODULE_LICENSE("GPL");