2 * arch/arm/mach-orion5x/gpio.c
4 * GPIO functions for Marvell Orion System On Chip
6 * Maintainer: Tzachi Perelstein <tzachi@marvell.com>
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.
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>
20 #include <asm/arch/orion5x.h>
23 static DEFINE_SPINLOCK(gpio_lock
);
24 static unsigned long gpio_valid
[BITS_TO_LONGS(GPIO_MAX
)];
25 static const char *gpio_label
[GPIO_MAX
]; /* non null for allocated GPIOs */
27 void __init
orion5x_gpio_set_valid_pins(u32 pins
)
33 * GENERIC_GPIO primitives
35 int gpio_direction_input(unsigned pin
)
39 if (pin
>= GPIO_MAX
|| !test_bit(pin
, gpio_valid
)) {
40 pr_debug("%s: invalid GPIO %d\n", __func__
, pin
);
44 spin_lock_irqsave(&gpio_lock
, flags
);
47 * Some callers might have not used the gpio_request(),
48 * so flag this pin as requested now.
51 gpio_label
[pin
] = "?";
53 orion5x_setbits(GPIO_IO_CONF
, 1 << pin
);
55 spin_unlock_irqrestore(&gpio_lock
, flags
);
58 EXPORT_SYMBOL(gpio_direction_input
);
60 int gpio_direction_output(unsigned pin
, int value
)
65 if (pin
>= GPIO_MAX
|| !test_bit(pin
, gpio_valid
)) {
66 pr_debug("%s: invalid GPIO %d\n", __func__
, pin
);
70 spin_lock_irqsave(&gpio_lock
, flags
);
73 * Some callers might have not used the gpio_request(),
74 * so flag this pin as requested now.
77 gpio_label
[pin
] = "?";
80 orion5x_clrbits(GPIO_BLINK_EN
, mask
);
82 orion5x_setbits(GPIO_OUT
, mask
);
84 orion5x_clrbits(GPIO_OUT
, mask
);
85 orion5x_clrbits(GPIO_IO_CONF
, mask
);
87 spin_unlock_irqrestore(&gpio_lock
, flags
);
90 EXPORT_SYMBOL(gpio_direction_output
);
92 int gpio_get_value(unsigned pin
)
94 int val
, mask
= 1 << pin
;
96 if (orion5x_read(GPIO_IO_CONF
) & mask
)
97 val
= orion5x_read(GPIO_DATA_IN
) ^ orion5x_read(GPIO_IN_POL
);
99 val
= orion5x_read(GPIO_OUT
);
103 EXPORT_SYMBOL(gpio_get_value
);
105 void gpio_set_value(unsigned pin
, int value
)
110 spin_lock_irqsave(&gpio_lock
, flags
);
112 orion5x_clrbits(GPIO_BLINK_EN
, mask
);
114 orion5x_setbits(GPIO_OUT
, mask
);
116 orion5x_clrbits(GPIO_OUT
, mask
);
118 spin_unlock_irqrestore(&gpio_lock
, flags
);
120 EXPORT_SYMBOL(gpio_set_value
);
122 void orion5x_gpio_set_blink(unsigned pin
, int blink
)
127 spin_lock_irqsave(&gpio_lock
, flags
);
129 orion5x_clrbits(GPIO_OUT
, mask
);
131 orion5x_setbits(GPIO_BLINK_EN
, mask
);
133 orion5x_clrbits(GPIO_BLINK_EN
, mask
);
135 spin_unlock_irqrestore(&gpio_lock
, flags
);
137 EXPORT_SYMBOL(orion5x_gpio_set_blink
);
139 int gpio_request(unsigned pin
, const char *label
)
144 if (pin
>= GPIO_MAX
|| !test_bit(pin
, gpio_valid
)) {
145 pr_debug("%s: invalid GPIO %d\n", __func__
, pin
);
149 spin_lock_irqsave(&gpio_lock
, flags
);
151 if (gpio_label
[pin
]) {
152 pr_debug("%s: GPIO %d already used as %s\n",
153 __func__
, pin
, gpio_label
[pin
]);
156 gpio_label
[pin
] = label
? label
: "?";
158 spin_unlock_irqrestore(&gpio_lock
, flags
);
161 EXPORT_SYMBOL(gpio_request
);
163 void gpio_free(unsigned pin
)
165 if (pin
>= GPIO_MAX
|| !test_bit(pin
, gpio_valid
)) {
166 pr_debug("%s: invalid GPIO %d\n", __func__
, pin
);
170 if (!gpio_label
[pin
])
171 pr_warning("%s: GPIO %d already freed\n", __func__
, pin
);
173 gpio_label
[pin
] = NULL
;
175 EXPORT_SYMBOL(gpio_free
);
178 void gpio_display(void)
182 for (i
= 0; i
< GPIO_MAX
; i
++) {
183 printk(KERN_DEBUG
"Pin-%d: ", i
);
185 if (!test_bit(i
, gpio_valid
)) {
186 printk("non-GPIO\n");
187 } else if (!gpio_label
[i
]) {
188 printk("GPIO, free\n");
190 printk("GPIO, used by %s, ", gpio_label
[i
]);
191 if (orion5x_read(GPIO_IO_CONF
) & (1 << i
)) {
192 printk("input, active %s, level %s, edge %s\n",
193 ((orion5x_read(GPIO_IN_POL
) >> i
) & 1) ? "low" : "high",
194 ((orion5x_read(GPIO_LEVEL_MASK
) >> i
) & 1) ? "enabled" : "masked",
195 ((orion5x_read(GPIO_EDGE_MASK
) >> i
) & 1) ? "enabled" : "masked");
197 printk("output, val=%d\n", (orion5x_read(GPIO_OUT
) >> i
) & 1);
202 printk(KERN_DEBUG
"MPP_0_7_CTRL (0x%08x) = 0x%08x\n",
203 MPP_0_7_CTRL
, orion5x_read(MPP_0_7_CTRL
));
204 printk(KERN_DEBUG
"MPP_8_15_CTRL (0x%08x) = 0x%08x\n",
205 MPP_8_15_CTRL
, orion5x_read(MPP_8_15_CTRL
));
206 printk(KERN_DEBUG
"MPP_16_19_CTRL (0x%08x) = 0x%08x\n",
207 MPP_16_19_CTRL
, orion5x_read(MPP_16_19_CTRL
));
208 printk(KERN_DEBUG
"MPP_DEV_CTRL (0x%08x) = 0x%08x\n",
209 MPP_DEV_CTRL
, orion5x_read(MPP_DEV_CTRL
));
210 printk(KERN_DEBUG
"GPIO_OUT (0x%08x) = 0x%08x\n",
211 GPIO_OUT
, orion5x_read(GPIO_OUT
));
212 printk(KERN_DEBUG
"GPIO_IO_CONF (0x%08x) = 0x%08x\n",
213 GPIO_IO_CONF
, orion5x_read(GPIO_IO_CONF
));
214 printk(KERN_DEBUG
"GPIO_BLINK_EN (0x%08x) = 0x%08x\n",
215 GPIO_BLINK_EN
, orion5x_read(GPIO_BLINK_EN
));
216 printk(KERN_DEBUG
"GPIO_IN_POL (0x%08x) = 0x%08x\n",
217 GPIO_IN_POL
, orion5x_read(GPIO_IN_POL
));
218 printk(KERN_DEBUG
"GPIO_DATA_IN (0x%08x) = 0x%08x\n",
219 GPIO_DATA_IN
, orion5x_read(GPIO_DATA_IN
));
220 printk(KERN_DEBUG
"GPIO_LEVEL_MASK (0x%08x) = 0x%08x\n",
221 GPIO_LEVEL_MASK
, orion5x_read(GPIO_LEVEL_MASK
));
222 printk(KERN_DEBUG
"GPIO_EDGE_CAUSE (0x%08x) = 0x%08x\n",
223 GPIO_EDGE_CAUSE
, orion5x_read(GPIO_EDGE_CAUSE
));
224 printk(KERN_DEBUG
"GPIO_EDGE_MASK (0x%08x) = 0x%08x\n",
225 GPIO_EDGE_MASK
, orion5x_read(GPIO_EDGE_MASK
));