deptest: Do not clobber the base build and staging dirs
[openwrt/svn-archive/archive.git] / target / linux / ifxmips / files-2.6.32 / arch / mips / ifxmips / common / 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) 2004 btxu Generate from INCA-IP project
17 * Copyright (C) 2005 Jin-Sze.Sow Comments edited
18 * Copyright (C) 2006 Huang Xiaogang Modification & verification on Danube chip
19 * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
20 */
21
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/errno.h>
25 #include <linux/proc_fs.h>
26 #include <linux/init.h>
27 #include <linux/ioctl.h>
28 #include <linux/timer.h>
29 #include <linux/module.h>
30 #include <linux/timer.h>
31 #include <linux/interrupt.h>
32 #include <linux/kobject.h>
33 #include <linux/workqueue.h>
34 #include <linux/skbuff.h>
35 #include <linux/netlink.h>
36 #include <linux/platform_device.h>
37 #include <linux/uaccess.h>
38 #include <linux/semaphore.h>
39 #include <linux/gpio.h>
40
41 #include <net/sock.h>
42
43 #include <ifxmips.h>
44
45 #define MAX_PORTS 2
46 #define PINS_PER_PORT 16
47
48 #define IFXMIPS_GPIO_SANITY {if (port > MAX_PORTS || pin > PINS_PER_PORT) return -EINVAL; }
49
50 #define GPIO_TO_PORT(x) ((x > 15) ? (1) : (0))
51 #define GPIO_TO_GPIO(x) ((x > 15) ? (x - 16) : (x))
52
53 int
54 ifxmips_port_reserve_pin(unsigned int port, unsigned int pin)
55 {
56 IFXMIPS_GPIO_SANITY;
57 printk(KERN_INFO "%s : call to obseleted function\n", __func__);
58 return 0;
59 }
60 EXPORT_SYMBOL(ifxmips_port_reserve_pin);
61
62 int
63 ifxmips_port_free_pin(unsigned int port, unsigned int pin)
64 {
65 IFXMIPS_GPIO_SANITY;
66 printk(KERN_INFO "%s : call to obseleted function\n", __func__);
67 return 0;
68 }
69 EXPORT_SYMBOL(ifxmips_port_free_pin);
70
71 int
72 ifxmips_port_set_open_drain(unsigned int port, unsigned int pin)
73 {
74 IFXMIPS_GPIO_SANITY;
75 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OD + (port * 0xC)) | (1 << pin),
76 IFXMIPS_GPIO_P0_OD + (port * 0xC));
77 return 0;
78 }
79 EXPORT_SYMBOL(ifxmips_port_set_open_drain);
80
81 int
82 ifxmips_port_clear_open_drain(unsigned int port, unsigned int pin)
83 {
84 IFXMIPS_GPIO_SANITY;
85 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OD + (port * 0xC)) & ~(1 << pin),
86 IFXMIPS_GPIO_P0_OD + (port * 0xC));
87 return 0;
88 }
89 EXPORT_SYMBOL(ifxmips_port_clear_open_drain);
90
91 int
92 ifxmips_port_set_pudsel(unsigned int port, unsigned int pin)
93 {
94 IFXMIPS_GPIO_SANITY;
95 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDSEL + (port * 0xC)) | (1 << pin),
96 IFXMIPS_GPIO_P0_PUDSEL + (port * 0xC));
97 return 0;
98 }
99 EXPORT_SYMBOL(ifxmips_port_set_pudsel);
100
101 int
102 ifxmips_port_clear_pudsel(unsigned int port, unsigned int pin)
103 {
104 IFXMIPS_GPIO_SANITY;
105 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDSEL + (port * 0xC)) & ~(1 << pin),
106 IFXMIPS_GPIO_P0_PUDSEL + (port * 0xC));
107 return 0;
108 }
109 EXPORT_SYMBOL(ifxmips_port_clear_pudsel);
110
111 int
112 ifxmips_port_set_puden(unsigned int port, unsigned int pin)
113 {
114 IFXMIPS_GPIO_SANITY;
115 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDEN + (port * 0xC)) | (1 << pin),
116 IFXMIPS_GPIO_P0_PUDEN + (port * 0xC));
117 return 0;
118 }
119 EXPORT_SYMBOL(ifxmips_port_set_puden);
120
121 int
122 ifxmips_port_clear_puden(unsigned int port, unsigned int pin)
123 {
124 IFXMIPS_GPIO_SANITY;
125 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDEN + (port * 0xC)) & ~(1 << pin),
126 IFXMIPS_GPIO_P0_PUDEN + (port * 0xC));
127 return 0;
128 }
129 EXPORT_SYMBOL(ifxmips_port_clear_puden);
130
131 int
132 ifxmips_port_set_stoff(unsigned int port, unsigned int pin)
133 {
134 IFXMIPS_GPIO_SANITY;
135 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_STOFF + (port * 0xC)) | (1 << pin),
136 IFXMIPS_GPIO_P0_STOFF + (port * 0xC));
137 return 0;
138 }
139 EXPORT_SYMBOL(ifxmips_port_set_stoff);
140
141 int
142 ifxmips_port_clear_stoff(unsigned int port, unsigned int pin)
143 {
144 IFXMIPS_GPIO_SANITY;
145 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_STOFF + (port * 0xC)) & ~(1 << pin),
146 IFXMIPS_GPIO_P0_STOFF + (port * 0xC));
147 return 0;
148 }
149 EXPORT_SYMBOL(ifxmips_port_clear_stoff);
150
151 int
152 ifxmips_port_set_dir_out(unsigned int port, unsigned int pin)
153 {
154 IFXMIPS_GPIO_SANITY;
155 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_DIR + (port * 0xC)) | (1 << pin),
156 IFXMIPS_GPIO_P0_DIR + (port * 0xC));
157 return 0;
158 }
159 EXPORT_SYMBOL(ifxmips_port_set_dir_out);
160
161 int
162 ifxmips_port_set_dir_in(unsigned int port, unsigned int pin)
163 {
164 IFXMIPS_GPIO_SANITY;
165 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_DIR + (port * 0xC)) & ~(1 << pin),
166 IFXMIPS_GPIO_P0_DIR + (port * 0xC));
167 return 0;
168 }
169 EXPORT_SYMBOL(ifxmips_port_set_dir_in);
170
171 int
172 ifxmips_port_set_output(unsigned int port, unsigned int pin)
173 {
174 IFXMIPS_GPIO_SANITY;
175 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OUT + (port * 0xC)) | (1 << pin),
176 IFXMIPS_GPIO_P0_OUT + (port * 0xC));
177 return 0;
178 }
179 EXPORT_SYMBOL(ifxmips_port_set_output);
180
181 int
182 ifxmips_port_clear_output(unsigned int port, unsigned int pin)
183 {
184 IFXMIPS_GPIO_SANITY;
185 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OUT + (port * 0xC)) & ~(1 << pin),
186 IFXMIPS_GPIO_P0_OUT + (port * 0xC));
187 return 0;
188 }
189 EXPORT_SYMBOL(ifxmips_port_clear_output);
190
191 int
192 ifxmips_port_get_input(unsigned int port, unsigned int pin)
193 {
194 IFXMIPS_GPIO_SANITY;
195 if (ifxmips_r32(IFXMIPS_GPIO_P0_IN + (port * 0xC)) & (1 << pin))
196 return 0;
197 else
198 return 1;
199 }
200 EXPORT_SYMBOL(ifxmips_port_get_input);
201
202 int
203 ifxmips_port_set_altsel0(unsigned int port, unsigned int pin)
204 {
205 IFXMIPS_GPIO_SANITY;
206 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0xC)) | (1 << pin),
207 IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0xC));
208 return 0;
209 }
210 EXPORT_SYMBOL(ifxmips_port_set_altsel0);
211
212 int
213 ifxmips_port_clear_altsel0(unsigned int port, unsigned int pin)
214 {
215 IFXMIPS_GPIO_SANITY;
216 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0xC)) & ~(1 << pin),
217 IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0xC));
218 return 0;
219 }
220 EXPORT_SYMBOL(ifxmips_port_clear_altsel0);
221
222 int
223 ifxmips_port_set_altsel1(unsigned int port, unsigned int pin)
224 {
225 IFXMIPS_GPIO_SANITY;
226 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0xC)) | (1 << pin),
227 IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0xC));
228 return 0;
229 }
230 EXPORT_SYMBOL(ifxmips_port_set_altsel1);
231
232 int
233 ifxmips_port_clear_altsel1(unsigned int port, unsigned int pin)
234 {
235 IFXMIPS_GPIO_SANITY;
236 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0xC)) & ~(1 << pin),
237 IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0xC));
238 return 0;
239 }
240 EXPORT_SYMBOL(ifxmips_port_clear_altsel1);
241
242 static void
243 ifxmips_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
244 {
245 int port = GPIO_TO_PORT(offset);
246 int gpio = GPIO_TO_GPIO(offset);
247 if(value)
248 ifxmips_port_set_output(port, gpio);
249 else
250 ifxmips_port_clear_output(port, gpio);
251 }
252
253 static int
254 ifxmips_gpio_get(struct gpio_chip *chip, unsigned int offset)
255 {
256 int port = GPIO_TO_PORT(offset);
257 int gpio = GPIO_TO_GPIO(offset);
258 return ifxmips_port_get_input(port, gpio);
259 }
260
261 static int
262 ifxmips_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
263 {
264 int port = GPIO_TO_PORT(offset);
265 int gpio = GPIO_TO_GPIO(offset);
266 ifxmips_port_set_open_drain(port, gpio);
267 ifxmips_port_clear_altsel0(port, gpio);
268 ifxmips_port_clear_altsel1(port, gpio);
269 ifxmips_port_set_dir_in(port, gpio);
270 return 0;
271 }
272
273 static int
274 ifxmips_gpio_direction_output(struct gpio_chip *chip, unsigned int offset, int value)
275 {
276 int port = GPIO_TO_PORT(offset);
277 int gpio = GPIO_TO_GPIO(offset);
278 ifxmips_port_clear_open_drain(port, gpio);
279 ifxmips_port_clear_altsel0(port, gpio);
280 ifxmips_port_clear_altsel1(port, gpio);
281 ifxmips_port_set_dir_out(port, gpio);
282 ifxmips_gpio_set(chip, offset, value);
283 return 0;
284 }
285
286 int
287 gpio_to_irq(unsigned int gpio)
288 {
289 return -EINVAL;
290 }
291 EXPORT_SYMBOL(gpio_to_irq);
292
293 struct gpio_chip
294 ifxmips_gpio_chip =
295 {
296 .label = "ifxmips-gpio",
297 .direction_input = ifxmips_gpio_direction_input,
298 .direction_output = ifxmips_gpio_direction_output,
299 .get = ifxmips_gpio_get,
300 .set = ifxmips_gpio_set,
301 .base = 0,
302 .ngpio = 32,
303 };
304
305 static int
306 ifxmips_gpio_probe(struct platform_device *dev)
307 {
308 gpiochip_add(&ifxmips_gpio_chip);
309 return 0;
310 }
311
312 static int
313 ifxmips_gpio_remove(struct platform_device *pdev)
314 {
315 gpiochip_remove(&ifxmips_gpio_chip);
316 return 0;
317 }
318
319 static struct platform_driver
320 ifxmips_gpio_driver = {
321 .probe = ifxmips_gpio_probe,
322 .remove = ifxmips_gpio_remove,
323 .driver = {
324 .name = "ifxmips_gpio",
325 .owner = THIS_MODULE,
326 },
327 };
328
329 int __init
330 ifxmips_gpio_init(void)
331 {
332 int ret = platform_driver_register(&ifxmips_gpio_driver);
333 if (ret)
334 printk(KERN_INFO "ifxmips_gpio : Error registering platfom driver!");
335 return ret;
336 }
337
338 void __exit
339 ifxmips_gpio_exit(void)
340 {
341 platform_driver_unregister(&ifxmips_gpio_driver);
342 }
343
344 module_init(ifxmips_gpio_init);
345 module_exit(ifxmips_gpio_exit);