mfp-pxa2xx.c 8.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  linux/arch/arm/mach-pxa/mfp-pxa2xx.c
 *
 *  PXA2xx pin mux configuration support
 *
 *  The GPIOs on PXA2xx can be configured as one of many alternate
 *  functions, this is by concept samilar to the MFP configuration
 *  on PXA3xx,  what's more important, the low power pin state and
 *  wakeup detection are also supported by the same framework.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
19
#include <linux/syscore_ops.h>
20

21
#include <mach/gpio.h>
22 23
#include <mach/pxa2xx-regs.h>
#include <mach/mfp-pxa2xx.h>
24 25 26

#include "generic.h"

27 28 29 30
#define PGSR(x)		__REG2(0x40F00020, (x) << 2)
#define __GAFR(u, x)	__REG2((u) ? 0x40E00058 : 0x40E00054, (x) << 3)
#define GAFR_L(x)	__GAFR(0, x)
#define GAFR_U(x)	__GAFR(1, x)
31 32 33

#define PWER_WE35	(1 << 24)

34
struct gpio_desc {
35 36 37
	unsigned	valid		: 1;
	unsigned	can_wakeup	: 1;
	unsigned	keypad_gpio	: 1;
38
	unsigned	dir_inverted	: 1;
39
	unsigned int	mask; /* bit mask in PWER or PKWR */
40
	unsigned int	mux_mask; /* bit mask of muxed gpio bits, 0 if no mux */
41
	unsigned long	config;
42 43 44
};

static struct gpio_desc gpio_desc[MFP_PIN_GPIO127 + 1];
45

46
static unsigned long gpdr_lpm[4];
47

48
static int __mfp_config_gpio(unsigned gpio, unsigned long c)
49 50
{
	unsigned long gafr, mask = GPIO_bit(gpio);
51 52 53 54
	int bank = gpio_to_bank(gpio);
	int uorl = !!(gpio & 0x10); /* GAFRx_U or GAFRx_L ? */
	int shft = (gpio & 0xf) << 1;
	int fn = MFP_AF(c);
55
	int is_out = (c & MFP_DIR_OUT) ? 1 : 0;
56 57 58 59

	if (fn > 3)
		return -EINVAL;

60 61 62
	/* alternate function and direction at run-time */
	gafr = (uorl == 0) ? GAFR_L(bank) : GAFR_U(bank);
	gafr = (gafr & ~(0x3 << shft)) | (fn << shft);
63

64 65 66 67 68
	if (uorl == 0)
		GAFR_L(bank) = gafr;
	else
		GAFR_U(bank) = gafr;

69
	if (is_out ^ gpio_desc[gpio].dir_inverted)
70 71 72 73
		GPDR(gpio) |= mask;
	else
		GPDR(gpio) &= ~mask;

74 75 76 77
	/* alternate function and direction at low power mode */
	switch (c & MFP_LPM_STATE_MASK) {
	case MFP_LPM_DRIVE_HIGH:
		PGSR(bank) |= mask;
78
		is_out = 1;
79 80 81
		break;
	case MFP_LPM_DRIVE_LOW:
		PGSR(bank) &= ~mask;
82
		is_out = 1;
83
		break;
84
	case MFP_LPM_INPUT:
85 86 87 88 89 90 91 92 93
	case MFP_LPM_DEFAULT:
		break;
	default:
		/* warning and fall through, treat as MFP_LPM_DEFAULT */
		pr_warning("%s: GPIO%d: unsupported low power mode\n",
				__func__, gpio);
		break;
	}

94
	if (is_out ^ gpio_desc[gpio].dir_inverted)
95 96 97
		gpdr_lpm[bank] |= mask;
	else
		gpdr_lpm[bank] &= ~mask;
98

99 100 101 102
	/* give early warning if MFP_LPM_CAN_WAKEUP is set on the
	 * configurations of those pins not able to wakeup
	 */
	if ((c & MFP_LPM_CAN_WAKEUP) && !gpio_desc[gpio].can_wakeup) {
103 104 105 106 107
		pr_warning("%s: GPIO%d unable to wakeup\n",
				__func__, gpio);
		return -EINVAL;
	}

108
	if ((c & MFP_LPM_CAN_WAKEUP) && is_out) {
109 110 111
		pr_warning("%s: output GPIO%d unable to wakeup\n",
				__func__, gpio);
		return -EINVAL;
112 113 114 115 116
	}

	return 0;
}

117 118 119 120 121 122 123 124 125 126 127 128
static inline int __mfp_validate(int mfp)
{
	int gpio = mfp_to_gpio(mfp);

	if ((mfp > MFP_PIN_GPIO127) || !gpio_desc[gpio].valid) {
		pr_warning("%s: GPIO%d is invalid pin\n", __func__, gpio);
		return -1;
	}

	return gpio;
}

129 130 131 132 133 134 135 136
void pxa2xx_mfp_config(unsigned long *mfp_cfgs, int num)
{
	unsigned long flags;
	unsigned long *c;
	int i, gpio;

	for (i = 0, c = mfp_cfgs; i < num; i++, c++) {

137 138
		gpio = __mfp_validate(MFP_PIN(*c));
		if (gpio < 0)
139 140 141 142 143 144 145 146 147 148 149
			continue;

		local_irq_save(flags);

		gpio_desc[gpio].config = *c;
		__mfp_config_gpio(gpio, *c);

		local_irq_restore(flags);
	}
}

150 151
void pxa2xx_mfp_set_lpm(int mfp, unsigned long lpm)
{
152
	unsigned long flags, c;
153 154 155 156 157 158 159
	int gpio;

	gpio = __mfp_validate(mfp);
	if (gpio < 0)
		return;

	local_irq_save(flags);
160 161 162 163 164

	c = gpio_desc[gpio].config;
	c = (c & ~MFP_LPM_STATE_MASK) | lpm;
	__mfp_config_gpio(gpio, c);

165 166 167
	local_irq_restore(flags);
}

168 169 170
int gpio_set_wake(unsigned int gpio, unsigned int on)
{
	struct gpio_desc *d;
171
	unsigned long c, mux_taken;
172 173 174 175 176 177 178 179 180 181

	if (gpio > mfp_to_gpio(MFP_PIN_GPIO127))
		return -EINVAL;

	d = &gpio_desc[gpio];
	c = d->config;

	if (!d->valid)
		return -EINVAL;

182 183 184 185 186 187 188 189 190 191 192
	/* Allow keypad GPIOs to wakeup system when
	 * configured as generic GPIOs.
	 */
	if (d->keypad_gpio && (MFP_AF(d->config) == 0) &&
	    (d->config & MFP_LPM_CAN_WAKEUP)) {
		if (on)
			PKWR |= d->mask;
		else
			PKWR &= ~d->mask;
		return 0;
	}
193

194 195 196 197
	mux_taken = (PWER & d->mux_mask) & (~d->mask);
	if (on && mux_taken)
		return -EBUSY;

198 199
	if (d->can_wakeup && (c & MFP_LPM_CAN_WAKEUP)) {
		if (on) {
200
			PWER = (PWER & ~d->mux_mask) | d->mask;
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

			if (c & MFP_LPM_EDGE_RISE)
				PRER |= d->mask;
			else
				PRER &= ~d->mask;

			if (c & MFP_LPM_EDGE_FALL)
				PFER |= d->mask;
			else
				PFER &= ~d->mask;
		} else {
			PWER &= ~d->mask;
			PRER &= ~d->mask;
			PFER &= ~d->mask;
		}
	}
	return 0;
}

220
#ifdef CONFIG_PXA25x
221
static void __init pxa25x_mfp_init(void)
222 223 224
{
	int i;

225
	for (i = 0; i <= pxa_last_gpio; i++)
226
		gpio_desc[i].valid = 1;
227

228 229 230
	for (i = 0; i <= 15; i++) {
		gpio_desc[i].can_wakeup = 1;
		gpio_desc[i].mask = GPIO_bit(i);
231
	}
232 233 234 235 236 237

	/* PXA26x has additional 4 GPIOs (86/87/88/89) which has the
	 * direction bit inverted in GPDR2. See PXA26x DM 4.1.1.
	 */
	for (i = 86; i <= pxa_last_gpio; i++)
		gpio_desc[i].dir_inverted = 1;
238
}
239 240
#else
static inline void pxa25x_mfp_init(void) {}
241 242 243
#endif /* CONFIG_PXA25x */

#ifdef CONFIG_PXA27x
244
static int pxa27x_pkwr_gpio[] = {
245 246 247 248
	13, 16, 17, 34, 36, 37, 38, 39, 90, 91, 93, 94,
	95, 96, 97, 98, 99, 100, 101, 102
};

249 250 251
int keypad_set_wake(unsigned int on)
{
	unsigned int i, gpio, mask = 0;
252
	struct gpio_desc *d;
253 254 255 256

	for (i = 0; i < ARRAY_SIZE(pxa27x_pkwr_gpio); i++) {

		gpio = pxa27x_pkwr_gpio[i];
257
		d = &gpio_desc[gpio];
258

259 260 261 262 263
		/* skip if configured as generic GPIO */
		if (MFP_AF(d->config) == 0)
			continue;

		if (d->config & MFP_LPM_CAN_WAKEUP)
264 265 266
			mask |= gpio_desc[gpio].mask;
	}

267 268 269 270
	if (on)
		PKWR |= mask;
	else
		PKWR &= ~mask;
271 272 273
	return 0;
}

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
#define PWER_WEMUX2_GPIO38	(1 << 16)
#define PWER_WEMUX2_GPIO53	(2 << 16)
#define PWER_WEMUX2_GPIO40	(3 << 16)
#define PWER_WEMUX2_GPIO36	(4 << 16)
#define PWER_WEMUX2_MASK	(7 << 16)
#define PWER_WEMUX3_GPIO31	(1 << 19)
#define PWER_WEMUX3_GPIO113	(2 << 19)
#define PWER_WEMUX3_MASK	(3 << 19)

#define INIT_GPIO_DESC_MUXED(mux, gpio)				\
do {								\
	gpio_desc[(gpio)].can_wakeup = 1;			\
	gpio_desc[(gpio)].mask = PWER_ ## mux ## _GPIO ##gpio;	\
	gpio_desc[(gpio)].mux_mask = PWER_ ## mux ## _MASK;	\
} while (0)

290
static void __init pxa27x_mfp_init(void)
291 292 293
{
	int i, gpio;

294
	for (i = 0; i <= pxa_last_gpio; i++) {
295 296 297 298 299
		/* skip GPIO2, 5, 6, 7, 8, they are not
		 * valid pins allow configuration
		 */
		if (i == 2 || i == 5 || i == 6 || i == 7 || i == 8)
			continue;
300

301 302
		gpio_desc[i].valid = 1;
	}
303

304 305 306 307 308 309 310
	/* Keypad GPIOs */
	for (i = 0; i < ARRAY_SIZE(pxa27x_pkwr_gpio); i++) {
		gpio = pxa27x_pkwr_gpio[i];
		gpio_desc[gpio].can_wakeup = 1;
		gpio_desc[gpio].keypad_gpio = 1;
		gpio_desc[gpio].mask = 1 << i;
	}
311

312 313 314 315 316
	/* Overwrite GPIO13 as a PWER wakeup source */
	for (i = 0; i <= 15; i++) {
		/* skip GPIO2, 5, 6, 7, 8 */
		if (GPIO_bit(i) & 0x1e4)
			continue;
317

318 319 320 321 322 323 324
		gpio_desc[i].can_wakeup = 1;
		gpio_desc[i].mask = GPIO_bit(i);
	}

	gpio_desc[35].can_wakeup = 1;
	gpio_desc[35].mask = PWER_WE35;

325 326 327 328 329 330
	INIT_GPIO_DESC_MUXED(WEMUX3, 31);
	INIT_GPIO_DESC_MUXED(WEMUX3, 113);
	INIT_GPIO_DESC_MUXED(WEMUX2, 38);
	INIT_GPIO_DESC_MUXED(WEMUX2, 53);
	INIT_GPIO_DESC_MUXED(WEMUX2, 40);
	INIT_GPIO_DESC_MUXED(WEMUX2, 36);
331 332 333 334 335 336 337 338
}
#else
static inline void pxa27x_mfp_init(void) {}
#endif /* CONFIG_PXA27x */

#ifdef CONFIG_PM
static unsigned long saved_gafr[2][4];
static unsigned long saved_gpdr[4];
339
static unsigned long saved_pgsr[4];
340

341
static int pxa2xx_mfp_suspend(void)
342 343 344
{
	int i;

345 346 347 348 349 350 351 352 353 354 355
	/* set corresponding PGSR bit of those marked MFP_LPM_KEEP_OUTPUT */
	for (i = 0; i < pxa_last_gpio; i++) {
		if ((gpio_desc[i].config & MFP_LPM_KEEP_OUTPUT) &&
		    (GPDR(i) & GPIO_bit(i))) {
			if (GPLR(i) & GPIO_bit(i))
				PGSR(i) |= GPIO_bit(i);
			else
				PGSR(i) &= ~GPIO_bit(i);
		}
	}

356
	for (i = 0; i <= gpio_to_bank(pxa_last_gpio); i++) {
357

358 359 360
		saved_gafr[0][i] = GAFR_L(i);
		saved_gafr[1][i] = GAFR_U(i);
		saved_gpdr[i] = GPDR(i * 32);
361
		saved_pgsr[i] = PGSR(i);
362 363

		GPDR(i * 32) = gpdr_lpm[i];
364
	}
365 366
	return 0;
}
367

368
static void pxa2xx_mfp_resume(void)
369 370 371
{
	int i;

372
	for (i = 0; i <= gpio_to_bank(pxa_last_gpio); i++) {
373 374 375
		GAFR_L(i) = saved_gafr[0][i];
		GAFR_U(i) = saved_gafr[1][i];
		GPDR(i * 32) = saved_gpdr[i];
376
		PGSR(i) = saved_pgsr[i];
377 378
	}
	PSSR = PSSR_RDH | PSSR_PH;
379
}
380 381 382 383 384
#else
#define pxa2xx_mfp_suspend	NULL
#define pxa2xx_mfp_resume	NULL
#endif

385
struct syscore_ops pxa2xx_mfp_syscore_ops = {
386 387 388 389 390 391 392 393
	.suspend	= pxa2xx_mfp_suspend,
	.resume		= pxa2xx_mfp_resume,
};

static int __init pxa2xx_mfp_init(void)
{
	int i;

394 395 396
	if (!cpu_is_pxa2xx())
		return 0;

397 398 399 400 401 402
	if (cpu_is_pxa25x())
		pxa25x_mfp_init();

	if (cpu_is_pxa27x())
		pxa27x_mfp_init();

403 404 405
	/* clear RDH bit to enable GPIO receivers after reset/sleep exit */
	PSSR = PSSR_RDH;

406
	/* initialize gafr_run[], pgsr_lpm[] from existing values */
407
	for (i = 0; i <= gpio_to_bank(pxa_last_gpio); i++)
408 409
		gpdr_lpm[i] = GPDR(i * 32);

410
	return 0;
411 412
}
postcore_initcall(pxa2xx_mfp_init);