cache GPIO addresses to be more efficent
[librecmc/librecmc.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, const char *label);
26 extern void gpio_free(unsigned gpio);
27
28 /* Common GPIO layer */
29 static inline int gpio_get_value(unsigned gpio)
30 {
31         static unsigned addr;
32
33         if (!addr) {
34                 void __iomem *gpio_in = (void __iomem *)
35                                 KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_INPUT);
36                 addr = readl(gpio_in);
37         }
38
39         return addr & (1 << gpio);
40 }
41
42 static inline void gpio_set_value(unsigned gpio, int value)
43 {
44         static unsigned addr;
45         unsigned tmp;
46
47         void __iomem *gpio_out =
48                 (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_OUTPUT);
49         if (!addr)
50                 addr = readl(gpio_out);
51
52         tmp = addr & ~(1 << gpio);
53         if (value)
54                 tmp |= 1 << gpio;
55         writel(tmp, gpio_out);
56 }
57
58 static inline int gpio_direction_input(unsigned gpio)
59 {
60         void __iomem *gpio_dir =
61                 (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_DIR);
62
63         if (gpio >= AR7_GPIO_MAX)
64                 return -EINVAL;
65
66         writel(readl(gpio_dir) | (1 << gpio), gpio_dir);
67
68         return 0;
69 }
70
71 static inline int gpio_direction_output(unsigned gpio, int value)
72 {
73         void __iomem *gpio_dir =
74                 (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_DIR);
75
76         if (gpio >= AR7_GPIO_MAX)
77                 return -EINVAL;
78
79         gpio_set_value(gpio, value);
80         writel(readl(gpio_dir) & ~(1 << gpio), gpio_dir);
81
82         return 0;
83 }
84
85 static inline int gpio_to_irq(unsigned gpio)
86 {
87         return -EINVAL;
88 }
89
90 static inline int irq_to_gpio(unsigned irq)
91 {
92         return -EINVAL;
93 }
94
95 /* Board specific GPIO functions */
96 static inline int ar7_gpio_enable(unsigned gpio)
97 {
98         void __iomem *gpio_en =
99                 (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_ENABLE);
100
101         writel(readl(gpio_en) | (1 << gpio), gpio_en);
102
103         return 0;
104 }
105
106 static inline int ar7_gpio_disable(unsigned gpio)
107 {
108         void __iomem *gpio_en =
109                 (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_ENABLE);
110
111         writel(readl(gpio_en) & ~(1 << gpio), gpio_en);
112
113         return 0;
114 }
115
116 #include <asm-generic/gpio.h>
117
118 #endif