2 * arch/arm/mach-orion/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>
19 #include <asm/arch/orion.h>
22 static DEFINE_SPINLOCK(gpio_lock
);
23 static unsigned long gpio_valid
[BITS_TO_LONGS(GPIO_MAX
)];
24 static const char *gpio_label
[GPIO_MAX
]; /* non null for allocated GPIOs */
26 void __init
orion_gpio_set_valid_pins(u32 pins
)
32 * GENERIC_GPIO primitives
34 int gpio_direction_input(unsigned pin
)
38 if (pin
>= GPIO_MAX
|| !test_bit(pin
, gpio_valid
)) {
39 pr_debug("%s: invalid GPIO %d\n", __FUNCTION__
, pin
);
43 spin_lock_irqsave(&gpio_lock
, flags
);
46 * Some callers might have not used the gpio_request(),
47 * so flag this pin as requested now.
50 gpio_label
[pin
] = "?";
52 orion_setbits(GPIO_IO_CONF
, 1 << pin
);
54 spin_unlock_irqrestore(&gpio_lock
, flags
);
57 EXPORT_SYMBOL(gpio_direction_input
);
59 int gpio_direction_output(unsigned pin
, int value
)
64 if (pin
>= GPIO_MAX
|| !test_bit(pin
, gpio_valid
)) {
65 pr_debug("%s: invalid GPIO %d\n", __FUNCTION__
, pin
);
69 spin_lock_irqsave(&gpio_lock
, flags
);
72 * Some callers might have not used the gpio_request(),
73 * so flag this pin as requested now.
76 gpio_label
[pin
] = "?";
79 orion_clrbits(GPIO_BLINK_EN
, mask
);
81 orion_setbits(GPIO_OUT
, mask
);
83 orion_clrbits(GPIO_OUT
, mask
);
84 orion_clrbits(GPIO_IO_CONF
, mask
);
86 spin_unlock_irqrestore(&gpio_lock
, flags
);
89 EXPORT_SYMBOL(gpio_direction_output
);
91 int gpio_get_value(unsigned pin
)
93 int val
, mask
= 1 << pin
;
95 if (orion_read(GPIO_IO_CONF
) & mask
)
96 val
= orion_read(GPIO_DATA_IN
) ^ orion_read(GPIO_IN_POL
);
98 val
= orion_read(GPIO_OUT
);
102 EXPORT_SYMBOL(gpio_get_value
);
104 void gpio_set_value(unsigned pin
, int value
)
109 spin_lock_irqsave(&gpio_lock
, flags
);
111 orion_clrbits(GPIO_BLINK_EN
, mask
);
113 orion_setbits(GPIO_OUT
, mask
);
115 orion_clrbits(GPIO_OUT
, mask
);
117 spin_unlock_irqrestore(&gpio_lock
, flags
);
119 EXPORT_SYMBOL(gpio_set_value
);
121 void orion_gpio_set_blink(unsigned pin
, int blink
)
126 spin_lock_irqsave(&gpio_lock
, flags
);
128 orion_clrbits(GPIO_OUT
, mask
);
130 orion_setbits(GPIO_BLINK_EN
, mask
);
132 orion_clrbits(GPIO_BLINK_EN
, mask
);
134 spin_unlock_irqrestore(&gpio_lock
, flags
);
136 EXPORT_SYMBOL(orion_gpio_set_blink
);
138 int gpio_request(unsigned pin
, const char *label
)
143 if (pin
>= GPIO_MAX
|| !test_bit(pin
, gpio_valid
)) {
144 pr_debug("%s: invalid GPIO %d\n", __FUNCTION__
, pin
);
148 spin_lock_irqsave(&gpio_lock
, flags
);
150 if (gpio_label
[pin
]) {
151 pr_debug("%s: GPIO %d already used as %s\n",
152 __FUNCTION__
, pin
, gpio_label
[pin
]);
155 gpio_label
[pin
] = label
? label
: "?";
157 spin_unlock_irqrestore(&gpio_lock
, flags
);
160 EXPORT_SYMBOL(gpio_request
);
162 void gpio_free(unsigned pin
)
164 if (pin
>= GPIO_MAX
|| !test_bit(pin
, gpio_valid
)) {
165 pr_debug("%s: invalid GPIO %d\n", __FUNCTION__
, pin
);
169 if (!gpio_label
[pin
])
170 pr_warning("%s: GPIO %d already freed\n", __FUNCTION__
, pin
);
172 gpio_label
[pin
] = NULL
;
174 EXPORT_SYMBOL(gpio_free
);
177 void gpio_display(void)
181 for (i
= 0; i
< GPIO_MAX
; i
++) {
182 printk(KERN_DEBUG
"Pin-%d: ", i
);
184 if (!test_bit(i
, gpio_valid
)) {
185 printk("non-GPIO\n");
186 } else if (!gpio_label
[i
]) {
187 printk("GPIO, free\n");
189 printk("GPIO, used by %s, ", gpio_label
[i
]);
190 if (orion_read(GPIO_IO_CONF
) & (1 << i
)) {
191 printk("input, active %s, level %s, edge %s\n",
192 ((orion_read(GPIO_IN_POL
) >> i
) & 1) ? "low" : "high",
193 ((orion_read(GPIO_LEVEL_MASK
) >> i
) & 1) ? "enabled" : "masked",
194 ((orion_read(GPIO_EDGE_MASK
) >> i
) & 1) ? "enabled" : "masked");
196 printk("output, val=%d\n", (orion_read(GPIO_OUT
) >> i
) & 1);
201 printk(KERN_DEBUG
"MPP_0_7_CTRL (0x%08x) = 0x%08x\n",
202 MPP_0_7_CTRL
, orion_read(MPP_0_7_CTRL
));
203 printk(KERN_DEBUG
"MPP_8_15_CTRL (0x%08x) = 0x%08x\n",
204 MPP_8_15_CTRL
, orion_read(MPP_8_15_CTRL
));
205 printk(KERN_DEBUG
"MPP_16_19_CTRL (0x%08x) = 0x%08x\n",
206 MPP_16_19_CTRL
, orion_read(MPP_16_19_CTRL
));
207 printk(KERN_DEBUG
"MPP_DEV_CTRL (0x%08x) = 0x%08x\n",
208 MPP_DEV_CTRL
, orion_read(MPP_DEV_CTRL
));
209 printk(KERN_DEBUG
"GPIO_OUT (0x%08x) = 0x%08x\n",
210 GPIO_OUT
, orion_read(GPIO_OUT
));
211 printk(KERN_DEBUG
"GPIO_IO_CONF (0x%08x) = 0x%08x\n",
212 GPIO_IO_CONF
, orion_read(GPIO_IO_CONF
));
213 printk(KERN_DEBUG
"GPIO_BLINK_EN (0x%08x) = 0x%08x\n",
214 GPIO_BLINK_EN
, orion_read(GPIO_BLINK_EN
));
215 printk(KERN_DEBUG
"GPIO_IN_POL (0x%08x) = 0x%08x\n",
216 GPIO_IN_POL
, orion_read(GPIO_IN_POL
));
217 printk(KERN_DEBUG
"GPIO_DATA_IN (0x%08x) = 0x%08x\n",
218 GPIO_DATA_IN
, orion_read(GPIO_DATA_IN
));
219 printk(KERN_DEBUG
"GPIO_LEVEL_MASK (0x%08x) = 0x%08x\n",
220 GPIO_LEVEL_MASK
, orion_read(GPIO_LEVEL_MASK
));
221 printk(KERN_DEBUG
"GPIO_EDGE_CAUSE (0x%08x) = 0x%08x\n",
222 GPIO_EDGE_CAUSE
, orion_read(GPIO_EDGE_CAUSE
));
223 printk(KERN_DEBUG
"GPIO_EDGE_MASK (0x%08x) = 0x%08x\n",
224 GPIO_EDGE_MASK
, orion_read(GPIO_EDGE_MASK
));