summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/nvrm/io/ap15/nvrm_gpio.c
blob: 905179934d3323f23d25de0123a1d9f2d3249962 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
 * Copyright (c) 2007-2009 NVIDIA Corporation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the NVIDIA Corporation nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <linux/module.h>
#include <linux/gpio.h>
#include <linux/irq.h>
#include <mach/gpio.h>

#include "nvrm_gpio.h"
#include "nvos.h"
#include "nvrm_structure.h"
#include "nvrm_pmu.h"
#include "nvrm_pinmux_utils.h"
#include "ap15/ap15rm_private.h"
#include "ap15/ap15rm_gpio_vi.h"
#include "nvodm_gpio_ext.h"
#include "nvodm_query_discovery.h"
#include "nvrm_hwintf.h"
#include "nvassert.h"

/* Treats GPIO pin handle releases like the pin is completely invalidated:
 * returned to SFIO state and tristated. */
#define RELEASE_IS_INVALIDATE 1
#define NV_ENABLE_GPIO_POWER_RAIL 1

#define TOTAL_GPIO_BANK    7
#define GPIO_PORT_PER_BANK 4
#define GPIO_PIN_PER_PORT  8
#define GPIO_PORT_ID(bank, port)  ((((bank)&0xFF) << 2) | (((port) & 0x3)))
#define GPIO_PIN_ID(bank, port, pin)  ((((bank)&0xFF) << 5) | \
					(((port) & 0x3) <<3) | ((pin) & 0x7))

#define GET_PIN(h)      ((((NvU32)(h))) & 0xFF)
#define GET_PORT(h)     ((((NvU32)(h)) >> 8) & 0xFF)
#define GET_BANK(h)     ((((NvU32)(h)) >> 16) & 0xFF)

#define NVRM_GPIO_CAP_FEAT_EDGE_INTR  0x000000001
#define GPIO_ARCH_FEATURE  (NVRM_GPIO_CAP_FEAT_EDGE_INTR)

extern NvError tegra_gpio_io_power_config(NvRmDeviceHandle hRm, int port,
                                  int pin, unsigned int enable);

typedef struct NvRmGpioPinInfoRec {
	NvBool used;
	NvU32 port;
	NvU32 inst;
	NvU32 pin;
	NvRmGpioPinMode mode;
	/* Sets up a chain of pins associated by one semaphore.
	 * Usefull to parse the pins when an interrupt is received. */
	NvU32 nextPin;
	NvU16 irqNumber;
} NvRmGpioPinInfo;

typedef struct NvRmGpioRec {
	NvU32 RefCount;
	NvRmDeviceHandle hRm;
	NvRmGpioPinInfo *pPinInfo;
} NvRmGpio;

static NvRmGpioHandle s_hGpio = NULL;
static NvOsMutexHandle s_GpioMutex = NULL;

NvError NvRmGpioOpen(NvRmDeviceHandle hRm, NvRmGpioHandle * phGpio)
{
	NvError err = NvSuccess;
	NvU32 total_pins;
	NvU32 i;

	NV_ASSERT(hRm);
	NV_ASSERT(phGpio);

	if (!s_GpioMutex) {
		err = NvOsMutexCreate(&s_GpioMutex);
		if (err != NvSuccess)
			goto fail;
	}

	NvOsMutexLock(s_GpioMutex);
	if (s_hGpio) {
		s_hGpio->RefCount++;
		goto exit;
	}

	s_hGpio = (NvRmGpio *) NvOsAlloc(sizeof(NvRmGpio));
	if (!s_hGpio) {
		err = NvError_InsufficientMemory;
		goto exit;
	}
	NvOsMemset(s_hGpio, 0, sizeof(NvRmGpio));
	s_hGpio->hRm = hRm;

	total_pins = TOTAL_GPIO_BANK * GPIO_PORT_PER_BANK * GPIO_PIN_PER_PORT;
	s_hGpio->pPinInfo = NvOsAlloc(sizeof(NvRmGpioPinInfo) * total_pins);
	if (s_hGpio->pPinInfo == NULL) {
		NvOsFree(s_hGpio);
		err = NvError_InsufficientMemory;
		s_hGpio = NULL;
		goto exit;
	}
	NvOsMemset(s_hGpio->pPinInfo, 0, sizeof(NvRmGpioPinInfo) * total_pins);
	for (i = 0; i < total_pins; i++)
		s_hGpio->pPinInfo[i].irqNumber = NVRM_IRQ_INVALID;
	s_hGpio->RefCount++;

exit:
	*phGpio = s_hGpio;
	NvOsMutexUnlock(s_GpioMutex);

fail:
	return err;
}

void NvRmGpioClose(NvRmGpioHandle hGpio)
{
	if (!hGpio)
		return;

	NV_ASSERT(hGpio->RefCount);

	NvOsMutexLock(s_GpioMutex);
	hGpio->RefCount--;
	if (hGpio->RefCount == 0) {
		NvOsFree(s_hGpio->pPinInfo);
		NvOsFree(s_hGpio);
		s_hGpio = NULL;
	}
	NvOsMutexUnlock(s_GpioMutex);
}

NvError
NvRmGpioAcquirePinHandle(NvRmGpioHandle hGpio,
			 NvU32 port, NvU32 pinNumber, NvRmGpioPinHandle * phPin)
{
	int gpio_nr;
	int ret_status;

	NV_ASSERT(hGpio != NULL);

	if (port == NVRM_GPIO_CAMERA_PORT) {
		NvOsMutexLock(s_GpioMutex);
		/* The Camera has dedicated gpio pins that must be controlled
		 * through a non-standard gpio port control. */
		NvRmPrivGpioViAcquirePinHandle(hGpio->hRm, pinNumber);
		*phPin = GPIO_MAKE_PIN_HANDLE(NVRM_GPIO_CAMERA_INST, port,
		 					pinNumber);
		NvOsMutexUnlock(s_GpioMutex);
	} else if ((port >= NVODM_GPIO_EXT_PORT_0) &&
		   (port <= NVODM_GPIO_EXT_PORT_F)) {
		/* Create a pin handle for GPIOs that are
		 * sourced by external (off-chip) peripherals */
		*phPin = GPIO_MAKE_PIN_HANDLE((port & 0xFF), port, pinNumber);
	} else {
		gpio_nr = GPIO_PIN_ID((port >> 2), (port & 0x3), pinNumber);
		if ((gpio_nr >= ARCH_NR_GPIOS) ||
			 (pinNumber >= GPIO_PIN_PER_PORT)) {
			printk(KERN_ERR "Requested port %d or pin %d  number "
					" is not supported", port, pinNumber);
			return NvError_NotSupported;
		}
		ret_status = gpio_request(gpio_nr, "nvrm_gpio");
		if (unlikely(ret_status != 0)) {
			return NvError_AlreadyAllocated;
		}
		*phPin = GPIO_MAKE_PIN_HANDLE((port >> 2), (port & 0x3),
						 pinNumber);
	}
	return NvSuccess;
}

void NvRmGpioReleasePinHandles(NvRmGpioHandle hGpio,
			       NvRmGpioPinHandle * hPin, NvU32 pinCount)
{
	NvU32 i;
	NvU32 port;
	NvU32 pin;
	NvU32 bank;
	int gpio_nr;
	NvU32 alphaPort;

	if (hPin == NULL)
		return;

	for (i = 0; i < pinCount; i++) {
		bank = GET_BANK(hPin[i]);
		port = GET_PORT(hPin[i]);
		pin = GET_PIN(hPin[i]);
		NvOsMutexLock(s_GpioMutex);
		if (port == NVRM_GPIO_CAMERA_PORT) {
			NvRmPrivGpioViReleasePinHandles(hGpio->hRm, pin);
		} else if ((port >= NVODM_GPIO_EXT_PORT_0) &&
			   (port <= NVODM_GPIO_EXT_PORT_F)) {
			/* Do nothing for now... */
		} else {
			gpio_nr = GPIO_PIN_ID(bank, port & 0x3, pin);
			if (gpio_nr >= ARCH_NR_GPIOS) {
				printk(KERN_ERR "Illegal pin handle at place "
						" %u of the list\n",i);
				NvOsMutexUnlock(s_GpioMutex);
				continue;
			}
			gpio_free(gpio_nr);
			alphaPort = (NvU32) GPIO_PORT_ID(bank, port);
			if (s_hGpio->pPinInfo[gpio_nr].used) {
				NV_DEBUG_PRINTF(("Warning: Releasing in-use "
					"GPIO pin handle GPIO_P%c.%02u "
					"(%c=%u)\n", 'A' + alphaPort, pin,
					'A' + alphaPort, alphaPort));
#if RELEASE_IS_INVALIDATE
				tegra_gpio_disable(gpio_nr);
				NvRmSetGpioTristate(hGpio->hRm,
					alphaPort, pin, NV_TRUE);
				s_hGpio->pPinInfo[gpio_nr].used = NV_FALSE;
#endif
			}
		}
		NvOsMutexUnlock(s_GpioMutex);
	}
	return;
}

void NvRmGpioReadPins(NvRmGpioHandle hGpio,
		      NvRmGpioPinHandle * hPin,
		      NvRmGpioPinState * pPinState, NvU32 pinCount)
{
	NvU32 bank;
	NvU32 port;
	NvU32 pin;
	NvU32 i;
	int gpio_nr;

	NV_ASSERT(hPin != NULL);
	NV_ASSERT(hGpio != NULL);

	for (i = 0; i < pinCount; i++) {
		port = GET_PORT(hPin[i]);
		pin = GET_PIN(hPin[i]);
		bank = GET_BANK(hPin[i]);

		if (port == NVRM_GPIO_CAMERA_PORT) {
			pPinState[i] = NvRmPrivGpioViReadPins(hGpio->hRm, pin);
		} else if ((port >= (NvU32) NVODM_GPIO_EXT_PORT_0) &&
			   (port <= (NvU32) NVODM_GPIO_EXT_PORT_F)) {
			pPinState[i] = NvOdmExternalGpioReadPins(port, pin);
		} else {
			gpio_nr = GPIO_PIN_ID(bank, port, pin);
			if (gpio_nr >= ARCH_NR_GPIOS) {
				printk(KERN_ERR "Illegal pin handle at place "
						" %u of the list\n",i);
				continue;
			}
			pPinState[i] = gpio_get_value(gpio_nr) & 0x1;
		}
	}
}

void NvRmGpioWritePins(NvRmGpioHandle hGpio,
		       NvRmGpioPinHandle * hPin,
		       NvRmGpioPinState * pPinState, NvU32 pinCount)
{
	NvU32 port;
	NvU32 pin;
	NvU32 bank;
	NvU32 i;
	int gpio_nr;

	NV_ASSERT(hPin != NULL);
	NV_ASSERT(hGpio != NULL);

	for (i = 0; i < pinCount; i++) {
		port = GET_PORT(hPin[i]);
		pin = GET_PIN(hPin[i]);
		bank = GET_BANK(hPin[i]);

		if (port == NVRM_GPIO_CAMERA_PORT) {
			NvRmPrivGpioViWritePins(hGpio->hRm, pin, pPinState[i]);
		} else if ((port >= (NvU32) NVODM_GPIO_EXT_PORT_0) &&
			   (port <= (NvU32) NVODM_GPIO_EXT_PORT_F)) {
			NvOdmExternalGpioWritePins(port, pin, pPinState[i]);
		} else {
			gpio_nr = GPIO_PIN_ID(bank, port, pin);
			if (gpio_nr >= ARCH_NR_GPIOS) {
				printk(KERN_ERR "Illegal pin handle at place "
						" %u of the list\n",i);
				continue;
			}
			gpio_set_value(gpio_nr, pPinState[i] & 0x1);
		}
	}

	return;
}

NvError NvRmGpioConfigPins(NvRmGpioHandle hGpio,
			   NvRmGpioPinHandle * hPin,
			   NvU32 pinCount, NvRmGpioPinMode Mode)
{
	NvError err = NvSuccess;
	NvU32 i;
	NvU32 bank;
	NvU32 port;
	NvU32 pin;
	NvU32 alphaPort;
	int ret_status;
	int gpio_nr;
	struct irq_chip *chip;
	int gpio_irq;

	NvOsMutexLock(s_GpioMutex);

	for (i = 0; i < pinCount; i++) {
		bank = GET_BANK(hPin[i]);
		port = GET_PORT(hPin[i]);
		pin = GET_PIN(hPin[i]);

		if (port == NVRM_GPIO_CAMERA_PORT) {
			/* If they are trying to do the wrong thing, assert.
			 * If they are trying to do the only allowed thing,
			 * quietly skip it, as nothing needs to be done. */
			if (Mode != NvOdmGpioPinMode_Output) {
				NV_ASSERT(!"Only output is supported for "
						"camera gpio.\n");
			}
			continue;
		}

		/* Absolute pin number to index into pPinInfo array and
		 * the alphabetic port names. */
		gpio_nr = GPIO_PIN_ID(bank, port, pin);
		gpio_irq = gpio_to_irq(gpio_nr);
		if (gpio_nr >= ARCH_NR_GPIOS) {
			printk(KERN_ERR "Illegal pin handle at place "
					" %u of the list\n",i);
			continue;
		}

		alphaPort = GPIO_PORT_ID(bank, port);

		s_hGpio->pPinInfo[gpio_nr].mode = Mode;
		s_hGpio->pPinInfo[gpio_nr].inst = bank;
		s_hGpio->pPinInfo[gpio_nr].port = port;
		s_hGpio->pPinInfo[gpio_nr].pin = pin;

		/* Don't try to colapse this swtich as the ordering of
		 * the register writes matter. */
		switch (Mode) {
		case NvRmGpioPinMode_Output:
			tegra_gpio_enable(gpio_nr);
			ret_status = gpio_direction_output(gpio_nr, 0);
			if (unlikely(ret_status != 0)) {
				NV_ASSERT(!"Not initialized");
				return NvError_NotInitialized;
			}
			break;

		case NvRmGpioPinMode_InputData:
			tegra_gpio_enable(gpio_nr);
			ret_status = gpio_direction_input(gpio_nr);
			if (unlikely(ret_status != 0)) {
				NV_ASSERT(!"Not initialized");
				return NvError_NotInitialized;
			}
			break;

		case NvRmGpioPinMode_InputInterruptLow:
			gpio_direction_input(gpio_nr);
			tegra_gpio_enable(gpio_nr);
			chip = get_irq_chip(gpio_irq);
			if ((chip) && (chip->set_type))
				chip->set_type(gpio_irq, IRQ_TYPE_LEVEL_LOW);
			break;

		case NvRmGpioPinMode_InputInterruptHigh:
			gpio_direction_input(gpio_nr);
			tegra_gpio_enable(gpio_nr);
			chip = get_irq_chip(gpio_irq);
			if ((chip) && (chip->set_type))
				chip->set_type(gpio_irq, IRQ_TYPE_LEVEL_HIGH);
			break;

		case NvRmGpioPinMode_InputInterruptAny:
			if (GPIO_ARCH_FEATURE & NVRM_GPIO_CAP_FEAT_EDGE_INTR) {
				gpio_direction_input(gpio_nr);
				tegra_gpio_enable(gpio_nr);
				chip = get_irq_chip(gpio_irq);
				if ((chip) && (chip->set_type))
					chip->set_type(gpio_irq,
						       IRQ_TYPE_EDGE_BOTH);
			} else {
				NV_ASSERT(!"Not supported");
			}
			break;

		case NvRmGpioPinMode_Function:
			tegra_gpio_disable(gpio_nr);
			break;

		case NvRmGpioPinMode_Inactive:
			chip = get_irq_chip(gpio_irq);
			if ((chip) && (chip->set_type))
				chip->mask(gpio_irq);
			tegra_gpio_disable(gpio_nr);
			break;

		case NvRmGpioPinMode_InputInterruptRisingEdge:
			if (GPIO_ARCH_FEATURE & NVRM_GPIO_CAP_FEAT_EDGE_INTR) {
				gpio_direction_input(gpio_nr);
				tegra_gpio_enable(gpio_nr);

				chip = get_irq_chip(gpio_irq);
				if ((chip) && (chip->set_type))
					chip->set_type(gpio_irq,
						       IRQ_TYPE_EDGE_RISING);
			} else {
				NV_ASSERT(!"Not supported");
			}
			break;

		case NvRmGpioPinMode_InputInterruptFallingEdge:
			if (GPIO_ARCH_FEATURE & NVRM_GPIO_CAP_FEAT_EDGE_INTR) {
				gpio_direction_input(gpio_nr);
				tegra_gpio_enable(gpio_nr);
				chip = get_irq_chip(gpio_irq);
				if ((chip) && (chip->set_type))
					chip->set_type(gpio_irq,
						       IRQ_TYPE_EDGE_FALLING);
			} else {
				NV_ASSERT(!"Not supported");
			}
			break;

		default:
			NV_ASSERT(!"Invalid gpio mode");
			break;
		}

		/*  Pad group global tristates are only modified when
		 *  the pin transitions from an inactive state to an
		 *  active one.  Active-to-active and inactive-to-inactive
		 * transitions are ignored */
		if ((!s_hGpio->pPinInfo[gpio_nr].used)
		    && (Mode != NvRmGpioPinMode_Inactive)) {
#if NV_ENABLE_GPIO_POWER_RAIL
			err = tegra_gpio_io_power_config(hGpio->hRm,
					alphaPort, pin, true);
#endif
			NvRmSetGpioTristate(hGpio->hRm,
					 alphaPort, pin, NV_FALSE);
		} else if ((s_hGpio->pPinInfo[gpio_nr].used)
			   && (Mode == NvRmGpioPinMode_Inactive)) {
#if NV_ENABLE_GPIO_POWER_RAIL
			err = tegra_gpio_io_power_config(hGpio->hRm,
					 alphaPort, pin, false);
#endif
			NvRmSetGpioTristate(hGpio->hRm,
				 	alphaPort, pin, NV_TRUE);
		}
		if (Mode != NvRmGpioPinMode_Inactive)
			s_hGpio->pPinInfo[gpio_nr].used = NV_TRUE;
		else
			s_hGpio->pPinInfo[gpio_nr].used = NV_FALSE;
	}

	NvOsMutexUnlock(s_GpioMutex);
	return err;
}

NvError NvRmGpioGetIrqs(NvRmDeviceHandle hRmDevice,
			NvRmGpioPinHandle * hPin, NvU32 * Irq, NvU32 pinCount)
{
	NvU32 i;
	int irq_base;
	NvU32 bank;
	NvU32 port;
	NvU32 pin;
	int gpio_nr;

	irq_base = gpio_to_irq(0);
	for (i = 0; i < pinCount; i++) {
		bank = GET_BANK(hPin[i]);
		port = GET_PORT(hPin[i]);
		pin = GET_PIN(hPin[i]);
		gpio_nr = GPIO_PIN_ID(bank, port, pin);
		if (gpio_nr >= ARCH_NR_GPIOS) {
			printk(KERN_ERR "Illegal pin handle at place "
					" %u of the list\n",i);
			continue;
		}
		Irq[i] = irq_base + gpio_nr;
	}
	return NvSuccess;
}