aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/cns3xxx/patches/104-cns3xxx_gpio.patch
blob: 6dff5b07d751d2e101badd772d2b4462297b2cd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
--- a/arch/arm/mach-cns3xxx/cns3420vb.c
+++ b/arch/arm/mach-cns3xxx/cns3420vb.c
@@ -215,7 +215,7 @@ static struct map_desc cns3420_io_desc[]
 
 static void __init cns3420_map_io(void)
 {
-	cns3xxx_map_io();
+	cns3xxx_common_init();
 	iotable_init(cns3420_io_desc, ARRAY_SIZE(cns3420_io_desc));
 
 	cns3420_early_serial_setup();
--- a/arch/arm/mach-cns3xxx/core.c
+++ b/arch/arm/mach-cns3xxx/core.c
@@ -18,6 +18,7 @@
 #include <asm/hardware/gic.h>
 #include <asm/smp_twd.h>
 #include <asm/hardware/cache-l2x0.h>
+#include <asm/gpio.h>
 #include <mach/cns3xxx.h>
 #include "core.h"
 
@@ -75,12 +76,96 @@ static struct map_desc cns3xxx_io_desc[]
 	},
 };
 
-void __init cns3xxx_map_io(void)
+int gpio_to_irq(int gpio)
+{
+	if (gpio > 63)
+		return -EINVAL;
+
+	if (gpio < 32)
+		return IRQ_CNS3XXX_GPIOA;
+	else
+		return IRQ_CNS3XXX_GPIOB;
+}
+EXPORT_SYMBOL(gpio_to_irq);
+
+int irq2gpio(int irq)
+{
+	if (irq == IRQ_CNS3XXX_GPIOA)
+		return 0;
+	else if (irq == IRQ_CNS3XXX_GPIOB)
+		return 32;
+	else
+		return -EINVAL;
+}
+EXPORT_SYMBOL(irq2gpio);
+
+static inline void gpio_line_config(u8 line, u32 direction)
+{
+	u32 reg;
+	if (direction) {
+		if (line < 32) {
+			reg = __raw_readl(CNS3XXX_GPIOA_BASE_VIRT + CNS3XXX_GPIO_DIR);
+			reg |= (1 << line);
+			__raw_writel(reg, CNS3XXX_GPIOA_BASE_VIRT + CNS3XXX_GPIO_DIR);
+		} else {
+			reg = __raw_readl(CNS3XXX_GPIOB_BASE_VIRT + CNS3XXX_GPIO_DIR);
+			reg |= (1 << (line - 32));
+			__raw_writel(reg, CNS3XXX_GPIOB_BASE_VIRT + CNS3XXX_GPIO_DIR);
+		}
+	} else {
+		if (line < 32) {
+			reg = __raw_readl(CNS3XXX_GPIOA_BASE_VIRT + CNS3XXX_GPIO_DIR);
+			reg &= ~(1 << line);
+			__raw_writel(reg, CNS3XXX_GPIOA_BASE_VIRT + CNS3XXX_GPIO_DIR);
+		} else {
+			reg = __raw_readl(CNS3XXX_GPIOB_BASE_VIRT + CNS3XXX_GPIO_DIR);
+			reg &= ~(1 << (line - 32));
+			__raw_writel(reg, CNS3XXX_GPIOB_BASE_VIRT + CNS3XXX_GPIO_DIR);
+		}
+	}
+}
+
+static int cns3xxx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
+{
+	gpio_line_config(gpio, CNS3XXX_GPIO_IN);
+	return 0;
+}
+
+static int cns3xxx_gpio_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
+{
+	gpio_line_set(gpio, level);
+	gpio_line_config(gpio, CNS3XXX_GPIO_OUT);
+	return 0;
+}
+
+static int cns3xxx_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
+{
+	return gpio_get_value(gpio);
+}
+
+static void cns3xxx_gpio_set_value(struct gpio_chip *chip, unsigned gpio, int value)
+{
+	gpio_set_value(gpio, value);
+}
+
+static struct gpio_chip cns3xxx_gpio_chip = {
+	.label			= "CNS3XXX_GPIO_CHIP",
+	.direction_input	= cns3xxx_gpio_direction_input,
+	.direction_output	= cns3xxx_gpio_direction_output,
+	.get			= cns3xxx_gpio_get_value,
+	.set			= cns3xxx_gpio_set_value,
+	.base			= 0,
+	.ngpio			= 64,
+};
+
+void __init cns3xxx_common_init(void)
 {
 #ifdef CONFIG_LOCAL_TIMERS
 	twd_base = (void __iomem *) CNS3XXX_TC11MP_TWD_BASE_VIRT;
 #endif
 	iotable_init(cns3xxx_io_desc, ARRAY_SIZE(cns3xxx_io_desc));
+
+	gpiochip_add(&cns3xxx_gpio_chip);
 }
 
 /* used by entry-macro.S */
--- a/arch/arm/mach-cns3xxx/core.h
+++ b/arch/arm/mach-cns3xxx/core.h
@@ -20,7 +20,7 @@ void __init cns3xxx_l2x0_init(void);
 static inline void cns3xxx_l2x0_init(void) {}
 #endif /* CONFIG_CACHE_L2X0 */
 
-void __init cns3xxx_map_io(void);
+void __init cns3xxx_common_init(void);
 void __init cns3xxx_init_irq(void);
 void cns3xxx_power_off(void);
 
--- a/arch/arm/mach-cns3xxx/laguna.c
+++ b/arch/arm/mach-cns3xxx/laguna.c
@@ -611,7 +611,7 @@ static struct map_desc laguna_io_desc[]
 
 static void __init laguna_map_io(void)
 {
-	cns3xxx_map_io();
+	cns3xxx_common_init();
 	iotable_init(laguna_io_desc, ARRAY_SIZE(laguna_io_desc));
 	laguna_early_serial_setup();
 }