blob: f99d08811e5af6487bb31120f2b8f8433ba17c23 [file] [log] [blame]
Tzachi Perelstein01af72e2007-10-23 15:14:42 -04001/*
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -04002 * arch/arm/mach-orion5x/gpio.c
Tzachi Perelstein01af72e2007-10-23 15:14:42 -04003 *
4 * GPIO functions for Marvell Orion System On Chip
5 *
6 * Maintainer: Tzachi Perelstein <tzachi@marvell.com>
7 *
Lennert Buytenhek159ffb32008-03-27 14:51:41 -04008 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
Tzachi Perelstein01af72e2007-10-23 15:14:42 -040010 * 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>
Russell Kingfced80c2008-09-06 12:10:45 +010018#include <linux/io.h>
Tzachi Perelstein01af72e2007-10-23 15:14:42 -040019#include <asm/gpio.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010020#include <mach/orion5x.h>
Tzachi Perelstein01af72e2007-10-23 15:14:42 -040021#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
Lennert Buytenhek19cfd5c2008-05-10 23:25:46 +020027void __init orion5x_gpio_set_valid(unsigned pin, int valid)
Tzachi Perelstein01af72e2007-10-23 15:14:42 -040028{
Lennert Buytenhek19cfd5c2008-05-10 23:25:46 +020029 if (valid)
30 __set_bit(pin, gpio_valid);
31 else
32 __clear_bit(pin, gpio_valid);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -040033}
34
35/*
36 * GENERIC_GPIO primitives
37 */
38int gpio_direction_input(unsigned pin)
39{
40 unsigned long flags;
41
42 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
Harvey Harrison8e86f422008-03-04 15:08:02 -080043 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -040044 return -EINVAL;
45 }
46
47 spin_lock_irqsave(&gpio_lock, flags);
48
49 /*
50 * Some callers might have not used the gpio_request(),
51 * so flag this pin as requested now.
52 */
53 if (!gpio_label[pin])
54 gpio_label[pin] = "?";
55
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -040056 orion5x_setbits(GPIO_IO_CONF, 1 << pin);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -040057
58 spin_unlock_irqrestore(&gpio_lock, flags);
59 return 0;
60}
61EXPORT_SYMBOL(gpio_direction_input);
62
63int gpio_direction_output(unsigned pin, int value)
64{
65 unsigned long flags;
66 int mask;
67
68 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
Harvey Harrison8e86f422008-03-04 15:08:02 -080069 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -040070 return -EINVAL;
71 }
72
73 spin_lock_irqsave(&gpio_lock, flags);
74
75 /*
76 * Some callers might have not used the gpio_request(),
77 * so flag this pin as requested now.
78 */
79 if (!gpio_label[pin])
80 gpio_label[pin] = "?";
81
82 mask = 1 << pin;
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -040083 orion5x_clrbits(GPIO_BLINK_EN, mask);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -040084 if (value)
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -040085 orion5x_setbits(GPIO_OUT, mask);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -040086 else
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -040087 orion5x_clrbits(GPIO_OUT, mask);
88 orion5x_clrbits(GPIO_IO_CONF, mask);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -040089
90 spin_unlock_irqrestore(&gpio_lock, flags);
91 return 0;
92}
93EXPORT_SYMBOL(gpio_direction_output);
94
95int gpio_get_value(unsigned pin)
96{
97 int val, mask = 1 << pin;
98
Lennert Buytenhek79e90dd2008-05-28 16:43:48 +020099 if (readl(GPIO_IO_CONF) & mask)
100 val = readl(GPIO_DATA_IN) ^ readl(GPIO_IN_POL);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400101 else
Lennert Buytenhek79e90dd2008-05-28 16:43:48 +0200102 val = readl(GPIO_OUT);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400103
104 return val & mask;
105}
106EXPORT_SYMBOL(gpio_get_value);
107
108void gpio_set_value(unsigned pin, int value)
109{
110 unsigned long flags;
111 int mask = 1 << pin;
112
113 spin_lock_irqsave(&gpio_lock, flags);
114
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400115 orion5x_clrbits(GPIO_BLINK_EN, mask);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400116 if (value)
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400117 orion5x_setbits(GPIO_OUT, mask);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400118 else
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400119 orion5x_clrbits(GPIO_OUT, mask);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400120
121 spin_unlock_irqrestore(&gpio_lock, flags);
122}
123EXPORT_SYMBOL(gpio_set_value);
124
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400125void orion5x_gpio_set_blink(unsigned pin, int blink)
Herbert Valerio Riedelb11e9e02007-11-29 15:19:56 +0100126{
127 unsigned long flags;
128 int mask = 1 << pin;
129
130 spin_lock_irqsave(&gpio_lock, flags);
131
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400132 orion5x_clrbits(GPIO_OUT, mask);
Herbert Valerio Riedelb11e9e02007-11-29 15:19:56 +0100133 if (blink)
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400134 orion5x_setbits(GPIO_BLINK_EN, mask);
Herbert Valerio Riedelb11e9e02007-11-29 15:19:56 +0100135 else
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400136 orion5x_clrbits(GPIO_BLINK_EN, mask);
Herbert Valerio Riedelb11e9e02007-11-29 15:19:56 +0100137
138 spin_unlock_irqrestore(&gpio_lock, flags);
139}
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400140EXPORT_SYMBOL(orion5x_gpio_set_blink);
Herbert Valerio Riedelb11e9e02007-11-29 15:19:56 +0100141
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400142int gpio_request(unsigned pin, const char *label)
143{
144 int ret = 0;
145 unsigned long flags;
146
147 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
Harvey Harrison8e86f422008-03-04 15:08:02 -0800148 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400149 return -EINVAL;
150 }
151
152 spin_lock_irqsave(&gpio_lock, flags);
153
154 if (gpio_label[pin]) {
155 pr_debug("%s: GPIO %d already used as %s\n",
Harvey Harrison8e86f422008-03-04 15:08:02 -0800156 __func__, pin, gpio_label[pin]);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400157 ret = -EBUSY;
158 } else
159 gpio_label[pin] = label ? label : "?";
160
161 spin_unlock_irqrestore(&gpio_lock, flags);
162 return ret;
163}
164EXPORT_SYMBOL(gpio_request);
165
166void gpio_free(unsigned pin)
167{
Uwe Kleine-König9c656852008-10-29 14:14:54 -0700168 might_sleep();
169
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400170 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
Harvey Harrison8e86f422008-03-04 15:08:02 -0800171 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400172 return;
173 }
174
175 if (!gpio_label[pin])
Harvey Harrison8e86f422008-03-04 15:08:02 -0800176 pr_warning("%s: GPIO %d already freed\n", __func__, pin);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400177 else
178 gpio_label[pin] = NULL;
179}
180EXPORT_SYMBOL(gpio_free);
181
182/* Debug helper */
183void gpio_display(void)
184{
185 int i;
186
187 for (i = 0; i < GPIO_MAX; i++) {
188 printk(KERN_DEBUG "Pin-%d: ", i);
189
190 if (!test_bit(i, gpio_valid)) {
191 printk("non-GPIO\n");
192 } else if (!gpio_label[i]) {
193 printk("GPIO, free\n");
194 } else {
195 printk("GPIO, used by %s, ", gpio_label[i]);
Lennert Buytenhek79e90dd2008-05-28 16:43:48 +0200196 if (readl(GPIO_IO_CONF) & (1 << i)) {
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400197 printk("input, active %s, level %s, edge %s\n",
Lennert Buytenhek79e90dd2008-05-28 16:43:48 +0200198 ((readl(GPIO_IN_POL) >> i) & 1) ? "low" : "high",
199 ((readl(GPIO_LEVEL_MASK) >> i) & 1) ? "enabled" : "masked",
200 ((readl(GPIO_EDGE_MASK) >> i) & 1) ? "enabled" : "masked");
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400201 } else {
Lennert Buytenhek79e90dd2008-05-28 16:43:48 +0200202 printk("output, val=%d\n", (readl(GPIO_OUT) >> i) & 1);
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400203 }
204 }
205 }
206
207 printk(KERN_DEBUG "MPP_0_7_CTRL (0x%08x) = 0x%08x\n",
Lennert Buytenhek79e90dd2008-05-28 16:43:48 +0200208 MPP_0_7_CTRL, readl(MPP_0_7_CTRL));
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400209 printk(KERN_DEBUG "MPP_8_15_CTRL (0x%08x) = 0x%08x\n",
Lennert Buytenhek79e90dd2008-05-28 16:43:48 +0200210 MPP_8_15_CTRL, readl(MPP_8_15_CTRL));
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400211 printk(KERN_DEBUG "MPP_16_19_CTRL (0x%08x) = 0x%08x\n",
Lennert Buytenhek79e90dd2008-05-28 16:43:48 +0200212 MPP_16_19_CTRL, readl(MPP_16_19_CTRL));
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400213 printk(KERN_DEBUG "MPP_DEV_CTRL (0x%08x) = 0x%08x\n",
Lennert Buytenhek79e90dd2008-05-28 16:43:48 +0200214 MPP_DEV_CTRL, readl(MPP_DEV_CTRL));
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400215 printk(KERN_DEBUG "GPIO_OUT (0x%08x) = 0x%08x\n",
Lennert Buytenhek79e90dd2008-05-28 16:43:48 +0200216 GPIO_OUT, readl(GPIO_OUT));
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400217 printk(KERN_DEBUG "GPIO_IO_CONF (0x%08x) = 0x%08x\n",
Lennert Buytenhek79e90dd2008-05-28 16:43:48 +0200218 GPIO_IO_CONF, readl(GPIO_IO_CONF));
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400219 printk(KERN_DEBUG "GPIO_BLINK_EN (0x%08x) = 0x%08x\n",
Lennert Buytenhek79e90dd2008-05-28 16:43:48 +0200220 GPIO_BLINK_EN, readl(GPIO_BLINK_EN));
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400221 printk(KERN_DEBUG "GPIO_IN_POL (0x%08x) = 0x%08x\n",
Lennert Buytenhek79e90dd2008-05-28 16:43:48 +0200222 GPIO_IN_POL, readl(GPIO_IN_POL));
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400223 printk(KERN_DEBUG "GPIO_DATA_IN (0x%08x) = 0x%08x\n",
Lennert Buytenhek79e90dd2008-05-28 16:43:48 +0200224 GPIO_DATA_IN, readl(GPIO_DATA_IN));
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400225 printk(KERN_DEBUG "GPIO_LEVEL_MASK (0x%08x) = 0x%08x\n",
Lennert Buytenhek79e90dd2008-05-28 16:43:48 +0200226 GPIO_LEVEL_MASK, readl(GPIO_LEVEL_MASK));
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400227 printk(KERN_DEBUG "GPIO_EDGE_CAUSE (0x%08x) = 0x%08x\n",
Lennert Buytenhek79e90dd2008-05-28 16:43:48 +0200228 GPIO_EDGE_CAUSE, readl(GPIO_EDGE_CAUSE));
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400229 printk(KERN_DEBUG "GPIO_EDGE_MASK (0x%08x) = 0x%08x\n",
Lennert Buytenhek79e90dd2008-05-28 16:43:48 +0200230 GPIO_EDGE_MASK, readl(GPIO_EDGE_MASK));
Tzachi Perelstein01af72e2007-10-23 15:14:42 -0400231}