2 * arch/arm/plat-orion/gpio.c
4 * Marvell Orion SoC GPIO handling.
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/irq.h>
14 #include <linux/module.h>
15 #include <linux/spinlock.h>
16 #include <linux/bitops.h>
18 #include <linux/gpio.h>
20 static DEFINE_SPINLOCK(gpio_lock
);
21 static unsigned long gpio_valid_input
[BITS_TO_LONGS(GPIO_MAX
)];
22 static unsigned long gpio_valid_output
[BITS_TO_LONGS(GPIO_MAX
)];
24 static inline void __set_direction(unsigned pin
, int input
)
28 u
= readl(GPIO_IO_CONF(pin
));
32 u
&= ~(1 << (pin
& 31));
33 writel(u
, GPIO_IO_CONF(pin
));
36 static void __set_level(unsigned pin
, int high
)
40 u
= readl(GPIO_OUT(pin
));
44 u
&= ~(1 << (pin
& 31));
45 writel(u
, GPIO_OUT(pin
));
48 static inline void __set_blinking(unsigned pin
, int blink
)
52 u
= readl(GPIO_BLINK_EN(pin
));
56 u
&= ~(1 << (pin
& 31));
57 writel(u
, GPIO_BLINK_EN(pin
));
60 static inline int orion_gpio_is_valid(unsigned pin
, int mode
)
63 if ((mode
& GPIO_INPUT_OK
) && !test_bit(pin
, gpio_valid_input
))
65 if ((mode
& GPIO_OUTPUT_OK
) && !test_bit(pin
, gpio_valid_output
))
71 pr_debug("%s: invalid GPIO %d\n", __func__
, pin
);
76 * GENERIC_GPIO primitives.
78 static int orion_gpio_direction_input(struct gpio_chip
*chip
, unsigned pin
)
82 if (!orion_gpio_is_valid(pin
, GPIO_INPUT_OK
))
85 spin_lock_irqsave(&gpio_lock
, flags
);
87 /* Configure GPIO direction. */
88 __set_direction(pin
, 1);
90 spin_unlock_irqrestore(&gpio_lock
, flags
);
95 static int orion_gpio_get_value(struct gpio_chip
*chip
, unsigned pin
)
99 if (readl(GPIO_IO_CONF(pin
)) & (1 << (pin
& 31)))
100 val
= readl(GPIO_DATA_IN(pin
)) ^ readl(GPIO_IN_POL(pin
));
102 val
= readl(GPIO_OUT(pin
));
104 return (val
>> (pin
& 31)) & 1;
107 static int orion_gpio_direction_output(struct gpio_chip
*chip
, unsigned pin
,
112 if (!orion_gpio_is_valid(pin
, GPIO_OUTPUT_OK
))
115 spin_lock_irqsave(&gpio_lock
, flags
);
117 /* Disable blinking. */
118 __set_blinking(pin
, 0);
120 /* Configure GPIO output value. */
121 __set_level(pin
, value
);
123 /* Configure GPIO direction. */
124 __set_direction(pin
, 0);
126 spin_unlock_irqrestore(&gpio_lock
, flags
);
131 static void orion_gpio_set_value(struct gpio_chip
*chip
, unsigned pin
,
136 spin_lock_irqsave(&gpio_lock
, flags
);
138 /* Configure GPIO output value. */
139 __set_level(pin
, value
);
141 spin_unlock_irqrestore(&gpio_lock
, flags
);
144 static int orion_gpio_request(struct gpio_chip
*chip
, unsigned pin
)
146 if (orion_gpio_is_valid(pin
, GPIO_INPUT_OK
) ||
147 orion_gpio_is_valid(pin
, GPIO_OUTPUT_OK
))
152 static struct gpio_chip orion_gpiochip
= {
153 .label
= "orion_gpio",
154 .direction_input
= orion_gpio_direction_input
,
155 .get
= orion_gpio_get_value
,
156 .direction_output
= orion_gpio_direction_output
,
157 .set
= orion_gpio_set_value
,
158 .request
= orion_gpio_request
,
164 void __init
orion_gpio_init(void)
166 gpiochip_add(&orion_gpiochip
);
170 * Orion-specific GPIO API extensions.
172 void __init
orion_gpio_set_unused(unsigned pin
)
174 /* Configure as output, drive low. */
176 __set_direction(pin
, 0);
179 void __init
orion_gpio_set_valid(unsigned pin
, int mode
)
182 mode
= GPIO_INPUT_OK
| GPIO_OUTPUT_OK
;
183 if (mode
& GPIO_INPUT_OK
)
184 __set_bit(pin
, gpio_valid_input
);
186 __clear_bit(pin
, gpio_valid_input
);
187 if (mode
& GPIO_OUTPUT_OK
)
188 __set_bit(pin
, gpio_valid_output
);
190 __clear_bit(pin
, gpio_valid_output
);
193 void orion_gpio_set_blink(unsigned pin
, int blink
)
197 spin_lock_irqsave(&gpio_lock
, flags
);
199 /* Set output value to zero. */
203 __set_blinking(pin
, blink
);
205 spin_unlock_irqrestore(&gpio_lock
, flags
);
207 EXPORT_SYMBOL(orion_gpio_set_blink
);
210 /*****************************************************************************
213 * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
214 * value of the line or the opposite value.
216 * Level IRQ handlers: DATA_IN is used directly as cause register.
217 * Interrupt are masked by LEVEL_MASK registers.
218 * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
219 * Interrupt are masked by EDGE_MASK registers.
220 * Both-edge handlers: Similar to regular Edge handlers, but also swaps
221 * the polarity to catch the next line transaction.
222 * This is a race condition that might not perfectly
223 * work on some use cases.
225 * Every eight GPIO lines are grouped (OR'ed) before going up to main
229 * data-in /--------| |-----| |----\
230 * -----| |----- ---- to main cause reg
231 * X \----------------| |----/
232 * polarity LEVEL mask
234 ****************************************************************************/
236 static void gpio_irq_ack(u32 irq
)
238 int type
= irq_desc
[irq
].status
& IRQ_TYPE_SENSE_MASK
;
239 if (type
& (IRQ_TYPE_EDGE_RISING
| IRQ_TYPE_EDGE_FALLING
)) {
240 int pin
= irq_to_gpio(irq
);
241 writel(~(1 << (pin
& 31)), GPIO_EDGE_CAUSE(pin
));
245 static void gpio_irq_mask(u32 irq
)
247 int pin
= irq_to_gpio(irq
);
248 int type
= irq_desc
[irq
].status
& IRQ_TYPE_SENSE_MASK
;
249 u32 reg
= (type
& (IRQ_TYPE_EDGE_RISING
| IRQ_TYPE_EDGE_FALLING
)) ?
250 GPIO_EDGE_MASK(pin
) : GPIO_LEVEL_MASK(pin
);
252 u
&= ~(1 << (pin
& 31));
256 static void gpio_irq_unmask(u32 irq
)
258 int pin
= irq_to_gpio(irq
);
259 int type
= irq_desc
[irq
].status
& IRQ_TYPE_SENSE_MASK
;
260 u32 reg
= (type
& (IRQ_TYPE_EDGE_RISING
| IRQ_TYPE_EDGE_FALLING
)) ?
261 GPIO_EDGE_MASK(pin
) : GPIO_LEVEL_MASK(pin
);
263 u
|= 1 << (pin
& 31);
267 static int gpio_irq_set_type(u32 irq
, u32 type
)
269 int pin
= irq_to_gpio(irq
);
270 struct irq_desc
*desc
;
273 u
= readl(GPIO_IO_CONF(pin
)) & (1 << (pin
& 31));
275 printk(KERN_ERR
"orion gpio_irq_set_type failed "
276 "(irq %d, pin %d).\n", irq
, pin
);
280 desc
= irq_desc
+ irq
;
283 * Set edge/level type.
285 if (type
& (IRQ_TYPE_EDGE_RISING
| IRQ_TYPE_EDGE_FALLING
)) {
286 desc
->handle_irq
= handle_edge_irq
;
287 } else if (type
& (IRQ_TYPE_LEVEL_HIGH
| IRQ_TYPE_LEVEL_LOW
)) {
288 desc
->handle_irq
= handle_level_irq
;
290 printk(KERN_ERR
"failed to set irq=%d (type=%d)\n", irq
, type
);
295 * Configure interrupt polarity.
297 if (type
== IRQ_TYPE_EDGE_RISING
|| type
== IRQ_TYPE_LEVEL_HIGH
) {
298 u
= readl(GPIO_IN_POL(pin
));
299 u
&= ~(1 << (pin
& 31));
300 writel(u
, GPIO_IN_POL(pin
));
301 } else if (type
== IRQ_TYPE_EDGE_FALLING
|| type
== IRQ_TYPE_LEVEL_LOW
) {
302 u
= readl(GPIO_IN_POL(pin
));
303 u
|= 1 << (pin
& 31);
304 writel(u
, GPIO_IN_POL(pin
));
305 } else if (type
== IRQ_TYPE_EDGE_BOTH
) {
308 v
= readl(GPIO_IN_POL(pin
)) ^ readl(GPIO_DATA_IN(pin
));
311 * set initial polarity based on current input level
313 u
= readl(GPIO_IN_POL(pin
));
314 if (v
& (1 << (pin
& 31)))
315 u
|= 1 << (pin
& 31); /* falling */
317 u
&= ~(1 << (pin
& 31)); /* rising */
318 writel(u
, GPIO_IN_POL(pin
));
321 desc
->status
= (desc
->status
& ~IRQ_TYPE_SENSE_MASK
) | type
;
326 struct irq_chip orion_gpio_irq_chip
= {
327 .name
= "orion_gpio_irq",
329 .mask
= gpio_irq_mask
,
330 .unmask
= gpio_irq_unmask
,
331 .set_type
= gpio_irq_set_type
,
334 void orion_gpio_irq_handler(int pinoff
)
339 cause
= readl(GPIO_DATA_IN(pinoff
)) & readl(GPIO_LEVEL_MASK(pinoff
));
340 cause
|= readl(GPIO_EDGE_CAUSE(pinoff
)) & readl(GPIO_EDGE_MASK(pinoff
));
342 for (pin
= pinoff
; pin
< pinoff
+ 8; pin
++) {
343 int irq
= gpio_to_irq(pin
);
344 struct irq_desc
*desc
= irq_desc
+ irq
;
346 if (!(cause
& (1 << (pin
& 31))))
349 if ((desc
->status
& IRQ_TYPE_SENSE_MASK
) == IRQ_TYPE_EDGE_BOTH
) {
350 /* Swap polarity (race with GPIO line) */
353 polarity
= readl(GPIO_IN_POL(pin
));
354 polarity
^= 1 << (pin
& 31);
355 writel(polarity
, GPIO_IN_POL(pin
));
357 desc_handle_irq(irq
, desc
);