blob: 5a7d9cc9b8004d4219eedb3919bdcc2085ea94ec [file] [log] [blame]
Tzachi Perelstein01af72e2007-10-23 15:14:42 -04001/*
2 * arch/arm/mach-orion/gpio.c
3 *
4 * GPIO functions for Marvell Orion System On Chip
5 *
6 * Maintainer: Tzachi Perelstein <tzachi@marvell.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/spinlock.h>
17#include <linux/bitops.h>
18#include <asm/gpio.h>
Lennert Buytenhekb590bc52008-03-27 14:51:41 -040019#include <asm/io.h>
Tzachi Perelstein01af72e2007-10-23 15:14:42 -040020#include <asm/arch/orion.h>
21#include "common.h"
22
23static DEFINE_SPINLOCK(gpio_lock);
24static unsigned long gpio_valid[BITS_TO_LONGS(GPIO_MAX)];
25static const char *gpio_label[GPIO_MAX]; /* non null for allocated GPIOs */
26
27void __init orion_gpio_set_valid_pins(u32 pins)
28{
29 gpio_valid[0] = pins;
30}
31
32/*
33 * GENERIC_GPIO primitives
34 */
35int gpio_direction_input(unsigned pin)
36{
37 unsigned long flags;
38
39 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
Harvey Harrison8e86f422008-03-04 15:08:02 -080040 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -040041 return -EINVAL;
42 }
43
44 spin_lock_irqsave(&gpio_lock, flags);
45
46 /*
47 * Some callers might have not used the gpio_request(),
48 * so flag this pin as requested now.
49 */
50 if (!gpio_label[pin])
51 gpio_label[pin] = "?";
52
53 orion_setbits(GPIO_IO_CONF, 1 << pin);
54
55 spin_unlock_irqrestore(&gpio_lock, flags);
56 return 0;
57}
58EXPORT_SYMBOL(gpio_direction_input);
59
60int gpio_direction_output(unsigned pin, int value)
61{
62 unsigned long flags;
63 int mask;
64
65 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
Harvey Harrison8e86f422008-03-04 15:08:02 -080066 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -040067 return -EINVAL;
68 }
69
70 spin_lock_irqsave(&gpio_lock, flags);
71
72 /*
73 * Some callers might have not used the gpio_request(),
74 * so flag this pin as requested now.
75 */
76 if (!gpio_label[pin])
77 gpio_label[pin] = "?";
78
79 mask = 1 << pin;
Herbert Valerio Riedelb11e9e02007-11-29 15:19:56 +010080 orion_clrbits(GPIO_BLINK_EN, mask);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -040081 if (value)
82 orion_setbits(GPIO_OUT, mask);
83 else
84 orion_clrbits(GPIO_OUT, mask);
85 orion_clrbits(GPIO_IO_CONF, mask);
86
87 spin_unlock_irqrestore(&gpio_lock, flags);
88 return 0;
89}
90EXPORT_SYMBOL(gpio_direction_output);
91
92int gpio_get_value(unsigned pin)
93{
94 int val, mask = 1 << pin;
95
96 if (orion_read(GPIO_IO_CONF) & mask)
Tzachi Perelsteinf0066612007-11-15 10:57:48 +020097 val = orion_read(GPIO_DATA_IN) ^ orion_read(GPIO_IN_POL);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -040098 else
99 val = orion_read(GPIO_OUT);
100
101 return val & mask;
102}
103EXPORT_SYMBOL(gpio_get_value);
104
105void gpio_set_value(unsigned pin, int value)
106{
107 unsigned long flags;
108 int mask = 1 << pin;
109
110 spin_lock_irqsave(&gpio_lock, flags);
111
Herbert Valerio Riedelb11e9e02007-11-29 15:19:56 +0100112 orion_clrbits(GPIO_BLINK_EN, mask);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400113 if (value)
114 orion_setbits(GPIO_OUT, mask);
115 else
116 orion_clrbits(GPIO_OUT, mask);
117
118 spin_unlock_irqrestore(&gpio_lock, flags);
119}
120EXPORT_SYMBOL(gpio_set_value);
121
Herbert Valerio Riedelb11e9e02007-11-29 15:19:56 +0100122void orion_gpio_set_blink(unsigned pin, int blink)
123{
124 unsigned long flags;
125 int mask = 1 << pin;
126
127 spin_lock_irqsave(&gpio_lock, flags);
128
129 orion_clrbits(GPIO_OUT, mask);
130 if (blink)
131 orion_setbits(GPIO_BLINK_EN, mask);
132 else
133 orion_clrbits(GPIO_BLINK_EN, mask);
134
135 spin_unlock_irqrestore(&gpio_lock, flags);
136}
137EXPORT_SYMBOL(orion_gpio_set_blink);
138
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400139int gpio_request(unsigned pin, const char *label)
140{
141 int ret = 0;
142 unsigned long flags;
143
144 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
Harvey Harrison8e86f422008-03-04 15:08:02 -0800145 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400146 return -EINVAL;
147 }
148
149 spin_lock_irqsave(&gpio_lock, flags);
150
151 if (gpio_label[pin]) {
152 pr_debug("%s: GPIO %d already used as %s\n",
Harvey Harrison8e86f422008-03-04 15:08:02 -0800153 __func__, pin, gpio_label[pin]);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400154 ret = -EBUSY;
155 } else
156 gpio_label[pin] = label ? label : "?";
157
158 spin_unlock_irqrestore(&gpio_lock, flags);
159 return ret;
160}
161EXPORT_SYMBOL(gpio_request);
162
163void gpio_free(unsigned pin)
164{
165 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
Harvey Harrison8e86f422008-03-04 15:08:02 -0800166 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400167 return;
168 }
169
170 if (!gpio_label[pin])
Harvey Harrison8e86f422008-03-04 15:08:02 -0800171 pr_warning("%s: GPIO %d already freed\n", __func__, pin);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400172 else
173 gpio_label[pin] = NULL;
174}
175EXPORT_SYMBOL(gpio_free);
176
177/* Debug helper */
178void gpio_display(void)
179{
180 int i;
181
182 for (i = 0; i < GPIO_MAX; i++) {
183 printk(KERN_DEBUG "Pin-%d: ", i);
184
185 if (!test_bit(i, gpio_valid)) {
186 printk("non-GPIO\n");
187 } else if (!gpio_label[i]) {
188 printk("GPIO, free\n");
189 } else {
190 printk("GPIO, used by %s, ", gpio_label[i]);
191 if (orion_read(GPIO_IO_CONF) & (1 << i)) {
192 printk("input, active %s, level %s, edge %s\n",
193 ((orion_read(GPIO_IN_POL) >> i) & 1) ? "low" : "high",
194 ((orion_read(GPIO_LEVEL_MASK) >> i) & 1) ? "enabled" : "masked",
195 ((orion_read(GPIO_EDGE_MASK) >> i) & 1) ? "enabled" : "masked");
196 } else {
197 printk("output, val=%d\n", (orion_read(GPIO_OUT) >> i) & 1);
198 }
199 }
200 }
201
202 printk(KERN_DEBUG "MPP_0_7_CTRL (0x%08x) = 0x%08x\n",
203 MPP_0_7_CTRL, orion_read(MPP_0_7_CTRL));
204 printk(KERN_DEBUG "MPP_8_15_CTRL (0x%08x) = 0x%08x\n",
205 MPP_8_15_CTRL, orion_read(MPP_8_15_CTRL));
206 printk(KERN_DEBUG "MPP_16_19_CTRL (0x%08x) = 0x%08x\n",
207 MPP_16_19_CTRL, orion_read(MPP_16_19_CTRL));
208 printk(KERN_DEBUG "MPP_DEV_CTRL (0x%08x) = 0x%08x\n",
209 MPP_DEV_CTRL, orion_read(MPP_DEV_CTRL));
210 printk(KERN_DEBUG "GPIO_OUT (0x%08x) = 0x%08x\n",
211 GPIO_OUT, orion_read(GPIO_OUT));
212 printk(KERN_DEBUG "GPIO_IO_CONF (0x%08x) = 0x%08x\n",
213 GPIO_IO_CONF, orion_read(GPIO_IO_CONF));
214 printk(KERN_DEBUG "GPIO_BLINK_EN (0x%08x) = 0x%08x\n",
215 GPIO_BLINK_EN, orion_read(GPIO_BLINK_EN));
216 printk(KERN_DEBUG "GPIO_IN_POL (0x%08x) = 0x%08x\n",
217 GPIO_IN_POL, orion_read(GPIO_IN_POL));
218 printk(KERN_DEBUG "GPIO_DATA_IN (0x%08x) = 0x%08x\n",
219 GPIO_DATA_IN, orion_read(GPIO_DATA_IN));
220 printk(KERN_DEBUG "GPIO_LEVEL_MASK (0x%08x) = 0x%08x\n",
221 GPIO_LEVEL_MASK, orion_read(GPIO_LEVEL_MASK));
222 printk(KERN_DEBUG "GPIO_EDGE_CAUSE (0x%08x) = 0x%08x\n",
223 GPIO_EDGE_CAUSE, orion_read(GPIO_EDGE_CAUSE));
224 printk(KERN_DEBUG "GPIO_EDGE_MASK (0x%08x) = 0x%08x\n",
225 GPIO_EDGE_MASK, orion_read(GPIO_EDGE_MASK));
226}