Commit | Line | Data |
---|---|---|
7ca5dc14 FF |
1 | /* |
2 | * Copyright (C) 2007 Felix Fietkau <nbd@openwrt.org> | |
3 | * Copyright (C) 2007 Eugene Konev <ejka@openwrt.org> | |
5f3c9098 | 4 | * Copyright (C) 2009 Florian Fainelli <florian@openwrt.org> |
7ca5dc14 FF |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <linux/module.h> | |
5f3c9098 | 22 | #include <linux/gpio.h> |
7ca5dc14 FF |
23 | |
24 | #include <asm/mach-ar7/gpio.h> | |
25 | ||
5f3c9098 FF |
26 | struct ar7_gpio_chip { |
27 | void __iomem *regs; | |
28 | struct gpio_chip chip; | |
29 | }; | |
7ca5dc14 | 30 | |
5f3c9098 | 31 | static int ar7_gpio_get_value(struct gpio_chip *chip, unsigned gpio) |
7ca5dc14 | 32 | { |
5f3c9098 FF |
33 | struct ar7_gpio_chip *gpch = |
34 | container_of(chip, struct ar7_gpio_chip, chip); | |
35 | void __iomem *gpio_in = gpch->regs + AR7_GPIO_INPUT; | |
7ca5dc14 | 36 | |
5f3c9098 FF |
37 | return readl(gpio_in) & (1 << gpio); |
38 | } | |
39 | ||
40 | static void ar7_gpio_set_value(struct gpio_chip *chip, | |
41 | unsigned gpio, int value) | |
42 | { | |
43 | struct ar7_gpio_chip *gpch = | |
44 | container_of(chip, struct ar7_gpio_chip, chip); | |
45 | void __iomem *gpio_out = gpch->regs + AR7_GPIO_OUTPUT; | |
46 | unsigned tmp; | |
47 | ||
48 | tmp = readl(gpio_out) & ~(1 << gpio); | |
49 | if (value) | |
50 | tmp |= 1 << gpio; | |
51 | writel(tmp, gpio_out); | |
52 | } | |
53 | ||
54 | static int ar7_gpio_direction_input(struct gpio_chip *chip, unsigned gpio) | |
55 | { | |
56 | struct ar7_gpio_chip *gpch = | |
57 | container_of(chip, struct ar7_gpio_chip, chip); | |
58 | void __iomem *gpio_dir = gpch->regs + AR7_GPIO_DIR; | |
7ca5dc14 | 59 | |
5f3c9098 | 60 | writel(readl(gpio_dir) | (1 << gpio), gpio_dir); |
7ca5dc14 FF |
61 | |
62 | return 0; | |
63 | } | |
7ca5dc14 | 64 | |
5f3c9098 FF |
65 | static int ar7_gpio_direction_output(struct gpio_chip *chip, |
66 | unsigned gpio, int value) | |
7ca5dc14 | 67 | { |
5f3c9098 FF |
68 | struct ar7_gpio_chip *gpch = |
69 | container_of(chip, struct ar7_gpio_chip, chip); | |
70 | void __iomem *gpio_dir = gpch->regs + AR7_GPIO_DIR; | |
71 | ||
72 | ar7_gpio_set_value(chip, gpio, value); | |
73 | writel(readl(gpio_dir) & ~(1 << gpio), gpio_dir); | |
74 | ||
75 | return 0; | |
76 | } | |
77 | ||
78 | static struct ar7_gpio_chip ar7_gpio_chip = { | |
79 | .chip = { | |
80 | .label = "ar7-gpio", | |
81 | .direction_input = ar7_gpio_direction_input, | |
82 | .direction_output = ar7_gpio_direction_output, | |
83 | .set = ar7_gpio_set_value, | |
84 | .get = ar7_gpio_get_value, | |
85 | .base = 0, | |
86 | .ngpio = AR7_GPIO_MAX, | |
87 | } | |
88 | }; | |
89 | ||
90 | int ar7_gpio_enable(unsigned gpio) | |
91 | { | |
92 | void __iomem *gpio_en = ar7_gpio_chip.regs + AR7_GPIO_ENABLE; | |
93 | ||
94 | writel(readl(gpio_en) | (1 << gpio), gpio_en); | |
95 | ||
96 | return 0; | |
97 | } | |
98 | EXPORT_SYMBOL(ar7_gpio_enable); | |
99 | ||
100 | int ar7_gpio_disable(unsigned gpio) | |
101 | { | |
102 | void __iomem *gpio_en = ar7_gpio_chip.regs + AR7_GPIO_ENABLE; | |
103 | ||
104 | writel(readl(gpio_en) & ~(1 << gpio), gpio_en); | |
105 | ||
106 | return 0; | |
107 | } | |
108 | EXPORT_SYMBOL(ar7_gpio_disable); | |
109 | ||
110 | static int __init ar7_gpio_init(void) | |
111 | { | |
112 | int ret; | |
113 | ||
114 | ar7_gpio_chip.regs = ioremap_nocache(AR7_REGS_GPIO, | |
115 | AR7_REGS_GPIO + 0x10); | |
116 | ||
117 | if (!ar7_gpio_chip.regs) { | |
118 | printk(KERN_ERR "ar7-gpio: failed to ioremap regs\n"); | |
119 | return -ENOMEM; | |
120 | } | |
121 | ||
122 | ret = gpiochip_add(&ar7_gpio_chip.chip); | |
123 | if (ret) { | |
124 | printk(KERN_ERR "ar7-gpio: failed to add gpiochip\n"); | |
125 | return ret; | |
126 | } | |
127 | printk(KERN_INFO "ar7-gpio: registered %d GPIOs\n", | |
128 | ar7_gpio_chip.chip.ngpio); | |
129 | return ret; | |
7ca5dc14 | 130 | } |
5f3c9098 | 131 | arch_initcall(ar7_gpio_init); |