Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[pandora-kernel.git] / arch / arm / mach-pxa / mfp-pxa2xx.c
1 /*
2  *  linux/arch/arm/mach-pxa/mfp-pxa2xx.c
3  *
4  *  PXA2xx pin mux configuration support
5  *
6  *  The GPIOs on PXA2xx can be configured as one of many alternate
7  *  functions, this is by concept samilar to the MFP configuration
8  *  on PXA3xx,  what's more important, the low power pin state and
9  *  wakeup detection are also supported by the same framework.
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License version 2 as
13  *  published by the Free Software Foundation.
14  */
15
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/sysdev.h>
20
21 #include <asm/arch/hardware.h>
22 #include <asm/arch/pxa-regs.h>
23 #include <asm/arch/mfp-pxa2xx.h>
24
25 #include "generic.h"
26
27 #define PGSR(x)         __REG2(0x40F00020, ((x) & 0x60) >> 3)
28
29 #define PWER_WE35       (1 << 24)
30
31 struct gpio_desc {
32         unsigned        valid           : 1;
33         unsigned        can_wakeup      : 1;
34         unsigned        keypad_gpio     : 1;
35         unsigned int    mask; /* bit mask in PWER or PKWR */
36         unsigned long   config;
37 };
38
39 static struct gpio_desc gpio_desc[MFP_PIN_GPIO127 + 1];
40
41 static int __mfp_config_gpio(unsigned gpio, unsigned long c)
42 {
43         unsigned long gafr, mask = GPIO_bit(gpio);
44         int fn;
45
46         fn = MFP_AF(c);
47         if (fn > 3)
48                 return -EINVAL;
49
50         /* alternate function and direction */
51         gafr = GAFR(gpio) & ~(0x3 << ((gpio & 0xf) * 2));
52         GAFR(gpio) = gafr |  (fn  << ((gpio & 0xf) * 2));
53
54         if (c & MFP_DIR_OUT)
55                 GPDR(gpio) |= mask;
56         else
57                 GPDR(gpio) &= ~mask;
58
59         /* low power state */
60         switch (c & MFP_LPM_STATE_MASK) {
61         case MFP_LPM_DRIVE_HIGH:
62                 PGSR(gpio) |= mask;
63                 break;
64         case MFP_LPM_DRIVE_LOW:
65                 PGSR(gpio) &= ~mask;
66                 break;
67         case MFP_LPM_INPUT:
68                 break;
69         default:
70                 pr_warning("%s: invalid low power state for GPIO%d\n",
71                                 __func__, gpio);
72                 return -EINVAL;
73         }
74
75         /* give early warning if MFP_LPM_CAN_WAKEUP is set on the
76          * configurations of those pins not able to wakeup
77          */
78         if ((c & MFP_LPM_CAN_WAKEUP) && !gpio_desc[gpio].can_wakeup) {
79                 pr_warning("%s: GPIO%d unable to wakeup\n",
80                                 __func__, gpio);
81                 return -EINVAL;
82         }
83
84         if ((c & MFP_LPM_CAN_WAKEUP) && (c & MFP_DIR_OUT)) {
85                 pr_warning("%s: output GPIO%d unable to wakeup\n",
86                                 __func__, gpio);
87                 return -EINVAL;
88         }
89
90         return 0;
91 }
92
93 void pxa2xx_mfp_config(unsigned long *mfp_cfgs, int num)
94 {
95         unsigned long flags;
96         unsigned long *c;
97         int i, gpio;
98
99         for (i = 0, c = mfp_cfgs; i < num; i++, c++) {
100
101                 gpio = mfp_to_gpio(MFP_PIN(*c));
102
103                 if (!gpio_desc[gpio].valid) {
104                         pr_warning("%s: GPIO%d is invalid pin\n",
105                                 __func__, gpio);
106                         continue;
107                 }
108
109                 local_irq_save(flags);
110
111                 gpio_desc[gpio].config = *c;
112                 __mfp_config_gpio(gpio, *c);
113
114                 local_irq_restore(flags);
115         }
116 }
117
118 int gpio_set_wake(unsigned int gpio, unsigned int on)
119 {
120         struct gpio_desc *d;
121         unsigned long c;
122
123         if (gpio > mfp_to_gpio(MFP_PIN_GPIO127))
124                 return -EINVAL;
125
126         d = &gpio_desc[gpio];
127         c = d->config;
128
129         if (!d->valid)
130                 return -EINVAL;
131
132         if (d->keypad_gpio)
133                 return -EINVAL;
134
135         if (d->can_wakeup && (c & MFP_LPM_CAN_WAKEUP)) {
136                 if (on) {
137                         PWER |= d->mask;
138
139                         if (c & MFP_LPM_EDGE_RISE)
140                                 PRER |= d->mask;
141                         else
142                                 PRER &= ~d->mask;
143
144                         if (c & MFP_LPM_EDGE_FALL)
145                                 PFER |= d->mask;
146                         else
147                                 PFER &= ~d->mask;
148                 } else {
149                         PWER &= ~d->mask;
150                         PRER &= ~d->mask;
151                         PFER &= ~d->mask;
152                 }
153         }
154         return 0;
155 }
156
157 #ifdef CONFIG_PXA25x
158 static int __init pxa25x_mfp_init(void)
159 {
160         int i;
161
162         if (cpu_is_pxa25x()) {
163                 for (i = 0; i <= 84; i++)
164                         gpio_desc[i].valid = 1;
165
166                 for (i = 0; i <= 15; i++) {
167                         gpio_desc[i].can_wakeup = 1;
168                         gpio_desc[i].mask = GPIO_bit(i);
169                 }
170         }
171
172         return 0;
173 }
174 postcore_initcall(pxa25x_mfp_init);
175 #endif /* CONFIG_PXA25x */
176
177 #ifdef CONFIG_PXA27x
178 static int pxa27x_pkwr_gpio[] = {
179         13, 16, 17, 34, 36, 37, 38, 39, 90, 91, 93, 94,
180         95, 96, 97, 98, 99, 100, 101, 102
181 };
182
183 int keypad_set_wake(unsigned int on)
184 {
185         unsigned int i, gpio, mask = 0;
186
187         if (!on) {
188                 PKWR = 0;
189                 return 0;
190         }
191
192         for (i = 0; i < ARRAY_SIZE(pxa27x_pkwr_gpio); i++) {
193
194                 gpio = pxa27x_pkwr_gpio[i];
195
196                 if (gpio_desc[gpio].config & MFP_LPM_CAN_WAKEUP)
197                         mask |= gpio_desc[gpio].mask;
198         }
199
200         PKWR = mask;
201         return 0;
202 }
203
204 static int __init pxa27x_mfp_init(void)
205 {
206         int i, gpio;
207
208         if (cpu_is_pxa27x()) {
209                 for (i = 0; i <= 120; i++) {
210                         /* skip GPIO2, 5, 6, 7, 8, they are not
211                          * valid pins allow configuration
212                          */
213                         if (i == 2 || i == 5 || i == 6 ||
214                             i == 7 || i == 8)
215                                 continue;
216
217                         gpio_desc[i].valid = 1;
218                 }
219
220                 /* Keypad GPIOs */
221                 for (i = 0; i < ARRAY_SIZE(pxa27x_pkwr_gpio); i++) {
222                         gpio = pxa27x_pkwr_gpio[i];
223                         gpio_desc[gpio].can_wakeup = 1;
224                         gpio_desc[gpio].keypad_gpio = 1;
225                         gpio_desc[gpio].mask = 1 << i;
226                 }
227
228                 /* Overwrite GPIO13 as a PWER wakeup source */
229                 for (i = 0; i <= 15; i++) {
230                         /* skip GPIO2, 5, 6, 7, 8 */
231                         if (GPIO_bit(i) & 0x1e4)
232                                 continue;
233
234                         gpio_desc[i].can_wakeup = 1;
235                         gpio_desc[i].mask = GPIO_bit(i);
236                 }
237
238                 gpio_desc[35].can_wakeup = 1;
239                 gpio_desc[35].mask = PWER_WE35;
240         }
241
242         return 0;
243 }
244 postcore_initcall(pxa27x_mfp_init);
245 #endif /* CONFIG_PXA27x */