36cc68896ee286e0bcf7a3a50727f1809e543052
[openwrt/openwrt.git] / target / linux / ar7 / files / include / asm-mips / ar7 / gpio.h
1 /*
2 * Copyright (C) 2007 Florian Fainelli <florian@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 void __iomem *gpio_in =
32 (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_INPUT);
33
34 return readl(gpio_in) & (1 << gpio);
35 }
36
37 static inline void gpio_set_value(unsigned gpio, int value)
38 {
39 void __iomem *gpio_out =
40 (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_OUTPUT);
41 unsigned tmp;
42
43 tmp = readl(gpio_out) & ~(1 << gpio);
44 if (value)
45 tmp |= 1 << gpio;
46 writel(tmp, gpio_out);
47 }
48
49 static inline int gpio_direction_input(unsigned gpio)
50 {
51 void __iomem *gpio_dir =
52 (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_DIR);
53
54 if (gpio >= AR7_GPIO_MAX)
55 return -EINVAL;
56
57 writel(readl(gpio_dir) | (1 << gpio), gpio_dir);
58
59 return 0;
60 }
61
62 static inline int gpio_direction_output(unsigned gpio, int value)
63 {
64 void __iomem *gpio_dir =
65 (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_DIR);
66
67 if (gpio >= AR7_GPIO_MAX)
68 return -EINVAL;
69
70 gpio_set_value(gpio, value);
71 writel(readl(gpio_dir) & ~(1 << gpio), gpio_dir);
72
73 return 0;
74 }
75
76 static inline int gpio_to_irq(unsigned gpio)
77 {
78 return -EINVAL;
79 }
80
81 static inline int irq_to_gpio(unsigned irq)
82 {
83 return -EINVAL;
84 }
85
86 /* Board specific GPIO functions */
87 static inline int ar7_gpio_enable(unsigned gpio)
88 {
89 void __iomem *gpio_en =
90 (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_ENABLE);
91
92 writel(readl(gpio_en) | (1 << gpio), gpio_en);
93
94 return 0;
95 }
96
97 static inline int ar7_gpio_disable(unsigned gpio)
98 {
99 void __iomem *gpio_en =
100 (void __iomem *)KSEG1ADDR(AR7_REGS_GPIO + AR7_GPIO_ENABLE);
101
102 writel(readl(gpio_en) & ~(1 << gpio), gpio_en);
103
104 return 0;
105 }
106
107 #include <asm-generic/gpio.h>
108
109 #endif