source: trunk/target/linux/ifxmips/files/arch/mips/ifxmips/gpio.c @ 20418

Last change on this file since 20418 was 20418, checked in by blogic, 6 years ago

[ifxmips] implement gpiolib drivers for core gpio and ebu attached latches

File size: 8.7 KB
Line 
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
53int
54ifxmips_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}
60EXPORT_SYMBOL(ifxmips_port_reserve_pin);
61
62int
63ifxmips_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}
69EXPORT_SYMBOL(ifxmips_port_free_pin);
70
71int
72ifxmips_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}
79EXPORT_SYMBOL(ifxmips_port_set_open_drain);
80
81int
82ifxmips_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}
89EXPORT_SYMBOL(ifxmips_port_clear_open_drain);
90
91int
92ifxmips_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}
99EXPORT_SYMBOL(ifxmips_port_set_pudsel);
100
101int
102ifxmips_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}
109EXPORT_SYMBOL(ifxmips_port_clear_pudsel);
110
111int
112ifxmips_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}
119EXPORT_SYMBOL(ifxmips_port_set_puden);
120
121int
122ifxmips_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}
129EXPORT_SYMBOL(ifxmips_port_clear_puden);
130
131int
132ifxmips_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}
139EXPORT_SYMBOL(ifxmips_port_set_stoff);
140
141int
142ifxmips_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}
149EXPORT_SYMBOL(ifxmips_port_clear_stoff);
150
151int
152ifxmips_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}
159EXPORT_SYMBOL(ifxmips_port_set_dir_out);
160
161int
162ifxmips_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}
169EXPORT_SYMBOL(ifxmips_port_set_dir_in);
170
171int
172ifxmips_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}
179EXPORT_SYMBOL(ifxmips_port_set_output);
180
181int
182ifxmips_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}
189EXPORT_SYMBOL(ifxmips_port_clear_output);
190
191int
192ifxmips_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}
200EXPORT_SYMBOL(ifxmips_port_get_input);
201
202int
203ifxmips_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}
210EXPORT_SYMBOL(ifxmips_port_set_altsel0);
211
212int
213ifxmips_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}
220EXPORT_SYMBOL(ifxmips_port_clear_altsel0);
221
222int
223ifxmips_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}
230EXPORT_SYMBOL(ifxmips_port_set_altsel1);
231
232int
233ifxmips_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}
240EXPORT_SYMBOL(ifxmips_port_clear_altsel1);
241
242static void
243ifxmips_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
253static int
254ifxmips_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
261static int
262ifxmips_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
273static int
274ifxmips_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
286int
287gpio_to_irq(unsigned int gpio)
288{
289        return -EINVAL;
290}
291EXPORT_SYMBOL(gpio_to_irq);
292
293struct gpio_chip
294ifxmips_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
305static int
306ifxmips_gpio_probe(struct platform_device *dev)
307{
308        gpiochip_add(&ifxmips_gpio_chip);
309        return 0;
310}
311
312static int
313ifxmips_gpio_remove(struct platform_device *pdev)
314{
315        gpiochip_remove(&ifxmips_gpio_chip);
316        return 0;
317}
318
319static struct platform_driver
320ifxmips_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
329int __init
330ifxmips_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
338void __exit
339ifxmips_gpio_exit(void)
340{
341        platform_driver_unregister(&ifxmips_gpio_driver);
342}
343
344module_init(ifxmips_gpio_init);
345module_exit(ifxmips_gpio_exit);
Note: See TracBrowser for help on using the repository browser.