AR7: Cleanups (closes: #2323)
[openwrt.git] / target / linux / ar7 / files / include / asm-mips / ar7 / gpio.h
1 /*
2  * Copyright (C) 2007 OpenWrt.org
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #ifndef __AR7_GPIO_H__
20 #define __AR7_GPIO_H__
21 #include <asm/ar7/ar7.h>
22
23 #define AR7_GPIO_MAX 32
24
25 extern int gpio_request(unsigned gpio, char *label);
26 extern void gpio_free(unsigned gpio);
27
28 /* Common GPIO layer */
29 static inline int gpio_direction_input(unsigned gpio)
30 {
31         void __iomem *gpio_dir =
32                 (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_DIR);
33
34         if (gpio >= AR7_GPIO_MAX)
35                 return -EINVAL;
36
37         writel(readl(gpio_dir) | (1 << gpio), gpio_dir);
38
39         return 0;
40 }
41
42 static inline int gpio_direction_output(unsigned gpio)
43 {
44         void __iomem *gpio_dir =
45                 (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_DIR);
46
47         if (gpio >= AR7_GPIO_MAX)
48                 return -EINVAL;
49
50         writel(readl(gpio_dir) & ~(1 << gpio), gpio_dir);
51
52         return 0;
53 }
54
55 static inline int gpio_get_value(unsigned gpio)
56 {
57         void __iomem *gpio_in =
58                 (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_INPUT);
59
60         if (gpio >= AR7_GPIO_MAX)
61                 return -EINVAL;
62
63         return ((readl(gpio_in) & (1 << gpio)) != 0);
64 }
65
66 static inline void gpio_set_value(unsigned gpio, int value)
67 {
68         void __iomem *gpio_out =
69                 (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_OUTPUT);
70         volatile unsigned tmp;
71
72         if (gpio >= AR7_GPIO_MAX)
73                 return;
74
75         tmp = readl(gpio_out) & ~(1 << gpio);
76         if (value)
77                 tmp |= 1 << gpio;
78         writel(tmp, gpio_out);
79 }
80
81 static inline int gpio_to_irq(unsigned gpio)
82 {
83         return -EINVAL;
84 }
85
86 static inline int irq_to_gpio(unsigned irq)
87 {
88         return -EINVAL;
89 }
90
91 /* Board specific GPIO functions */
92 static inline int ar7_gpio_enable(unsigned gpio)
93 {
94         void __iomem *gpio_en =
95                 (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_ENABLE);
96
97         if (gpio >= AR7_GPIO_MAX)
98                 return -EINVAL;
99
100         writel(readl(gpio_en) | (1 << gpio), gpio_en);
101
102         return 0;
103 }
104
105 static inline int ar7_gpio_disable(unsigned gpio)
106 {
107         void __iomem *gpio_en =
108                 (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_ENABLE);
109
110         if (gpio >= AR7_GPIO_MAX)
111                 return -EINVAL;
112
113         writel(readl(gpio_en) & ~(1 << gpio), gpio_en);
114
115         return 0;
116 }
117
118 #include <asm-generic/gpio.h>
119
120 #endif