Flatten brcm63xx patches, should make our life easier to patch files now ;)
[openwrt/openwrt.git] / target / linux / brcm63xx / files / arch / mips / bcm63xx / gpio.c
1 /*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
7 */
8
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/spinlock.h>
12 #include <bcm63xx_cpu.h>
13 #include <bcm63xx_gpio.h>
14 #include <bcm63xx_io.h>
15 #include <bcm63xx_regs.h>
16
17 static DEFINE_SPINLOCK(bcm63xx_gpio_lock);
18
19 void bcm63xx_gpio_set_dataout(int gpio, int val)
20 {
21 u32 reg;
22 u32 mask;
23 u32 tmp;
24 unsigned long flags;
25
26 if (gpio >= BCM63XX_GPIO_COUNT)
27 BUG();
28
29 if (gpio < 32) {
30 reg = GPIO_DATA_LO_REG;
31 mask = 1 << gpio;
32 } else {
33 reg = GPIO_DATA_HI_REG;
34 mask = 1 << (gpio - 32);
35 }
36
37 spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
38 tmp = bcm_gpio_readl(reg);
39 if (val)
40 tmp |= mask;
41 else
42 tmp &= ~mask;
43 bcm_gpio_writel(tmp, reg);
44 spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
45 }
46
47 EXPORT_SYMBOL(bcm63xx_gpio_set_dataout);
48
49 int bcm63xx_gpio_get_datain(int gpio)
50 {
51 u32 reg;
52 u32 mask;
53
54 if (gpio >= BCM63XX_GPIO_COUNT)
55 BUG();
56
57 if (gpio < 32) {
58 reg = GPIO_DATA_LO_REG;
59 mask = 1 << gpio;
60 } else {
61 reg = GPIO_DATA_HI_REG;
62 mask = 1 << (gpio - 32);
63 }
64
65 return !!(bcm_gpio_readl(reg) & mask);
66 }
67
68 EXPORT_SYMBOL(bcm63xx_gpio_get_datain);
69
70 void bcm63xx_gpio_set_direction(int gpio, int dir)
71 {
72 u32 reg;
73 u32 mask;
74 u32 tmp;
75 unsigned long flags;
76
77 if (gpio >= BCM63XX_GPIO_COUNT)
78 BUG();
79
80 if (gpio < 32) {
81 reg = GPIO_CTL_LO_REG;
82 mask = 1 << gpio;
83 } else {
84 reg = GPIO_CTL_HI_REG;
85 mask = 1 << (gpio - 32);
86 }
87
88 spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
89 tmp = bcm_gpio_readl(reg);
90 if (dir == GPIO_DIR_IN)
91 tmp &= ~mask;
92 else
93 tmp |= mask;
94 bcm_gpio_writel(tmp, reg);
95 spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
96 }
97
98 EXPORT_SYMBOL(bcm63xx_gpio_set_direction);