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