gpio.h 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * TI DaVinci GPIO Support
 *
 * Copyright (c) 2006 David Brownell
 * Copyright (c) 2007, MontaVista Software, Inc. <source@mvista.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#ifndef	__DAVINCI_GPIO_H
#define	__DAVINCI_GPIO_H

16
#include <linux/io.h>
17
#include <asm-generic/gpio.h>
18

19
#include <mach/irqs.h>
20
#include <mach/common.h>
21

22 23
#define DAVINCI_GPIO_BASE 0x01C67000

24 25 26 27 28 29 30 31
/*
 * basic gpio routines
 *
 * board-specific init should be done by arch/.../.../board-XXX.c (maybe
 * initializing banks together) rather than boot loaders; kexec() won't
 * go through boot loaders.
 *
 * the gpio clock will be turned on when gpios are used, and you may also
D
David Brownell 已提交
32
 * need to pay attention to PINMUX registers to be sure those pins are
33 34
 * used as gpios, not with other peripherals.
 *
35
 * On-chip GPIOs are numbered 0..(DAVINCI_N_GPIO-1).  For documentation,
D
David Brownell 已提交
36 37 38
 * and maybe for later updates, code may write GPIO(N).  These may be
 * all 1.8V signals, all 3.3V ones, or a mix of the two.  A given chip
 * may not support all the GPIOs in that range.
39 40 41
 *
 * GPIOs can also be on external chips, numbered after the ones built-in
 * to the DaVinci chip.  For now, they won't be usable as IRQ sources.
42
 */
D
David Brownell 已提交
43
#define	GPIO(X)		(X)		/* 0 <= X <= (DAVINCI_N_GPIO - 1) */
44

45 46 47
/* Convert GPIO signal to GPIO pin number */
#define GPIO_TO_PIN(bank, gpio)	(16 * (bank) + (gpio))

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
struct gpio_controller {
	u32	dir;
	u32	out_data;
	u32	set_data;
	u32	clr_data;
	u32	in_data;
	u32	set_rising;
	u32	clr_rising;
	u32	set_falling;
	u32	clr_falling;
	u32	intstat;
};

/* The __gpio_to_controller() and __gpio_mask() functions inline to constants
 * with constant parameters; or in outlined code they execute at runtime.
 *
 * You'd access the controller directly when reading or writing more than
 * one gpio value at a time, and to support wired logic where the value
 * being driven by the cpu need not match the value read back.
 *
 * These are NOT part of the cross-platform GPIO interface
 */
static inline struct gpio_controller *__iomem
__gpio_to_controller(unsigned gpio)
{
	void *__iomem ptr;
74
	void __iomem *base = davinci_soc_info.gpio_base;
75

D
David Brownell 已提交
76
	if (gpio < 32 * 1)
77
		ptr = base + 0x10;
D
David Brownell 已提交
78
	else if (gpio < 32 * 2)
79
		ptr = base + 0x38;
D
David Brownell 已提交
80
	else if (gpio < 32 * 3)
81
		ptr = base + 0x60;
D
David Brownell 已提交
82
	else if (gpio < 32 * 4)
83
		ptr = base + 0x88;
84 85
	else if (gpio < 32 * 5)
		ptr = base + 0xb0;
86 87 88 89 90 91 92 93 94 95 96
	else
		ptr = NULL;
	return ptr;
}

static inline u32 __gpio_mask(unsigned gpio)
{
	return 1 << (gpio % 32);
}

/* The get/set/clear functions will inline when called with constant
97
 * parameters referencing built-in GPIOs, for low-overhead bitbanging.
98
 *
99 100
 * Otherwise, calls with variable parameters or referencing external
 * GPIOs (e.g. on GPIO expander chips) use outlined functions.
101 102 103
 */
static inline void gpio_set_value(unsigned gpio, int value)
{
104
	if (__builtin_constant_p(value) && gpio < DAVINCI_N_GPIO) {
105 106 107 108 109 110 111 112 113 114 115 116
		struct gpio_controller	*__iomem g;
		u32			mask;

		g = __gpio_to_controller(gpio);
		mask = __gpio_mask(gpio);
		if (value)
			__raw_writel(mask, &g->set_data);
		else
			__raw_writel(mask, &g->clr_data);
		return;
	}

117
	__gpio_set_value(gpio, value);
118 119 120
}

/* Returns zero or nonzero; works for gpios configured as inputs OR
121
 * as outputs, at least for built-in GPIOs.
122
 *
123 124 125 126 127
 * NOTE: for built-in GPIOs, changes in reported values are synchronized
 * to the GPIO clock.  This is easily seen after calling gpio_set_value()
 * and then immediately gpio_get_value(), where the gpio_get_value() will
 * return the old value until the GPIO clock ticks and the new value gets
 * latched.
128 129 130
 */
static inline int gpio_get_value(unsigned gpio)
{
131
	struct gpio_controller	*__iomem g;
132

133 134
	if (!__builtin_constant_p(gpio) || gpio >= DAVINCI_N_GPIO)
		return __gpio_get_value(gpio);
135 136

	g = __gpio_to_controller(gpio);
137
	return __gpio_mask(gpio) & __raw_readl(&g->in_data);
138 139
}

140 141 142 143 144 145 146
static inline int gpio_cansleep(unsigned gpio)
{
	if (__builtin_constant_p(gpio) && gpio < DAVINCI_N_GPIO)
		return 0;
	else
		return __gpio_cansleep(gpio);
}
147 148 149

static inline int gpio_to_irq(unsigned gpio)
{
D
David Brownell 已提交
150
	return __gpio_to_irq(gpio);
151 152 153 154
}

static inline int irq_to_gpio(unsigned irq)
{
D
David Brownell 已提交
155 156
	/* don't support the reverse mapping */
	return -ENOSYS;
157 158 159
}

#endif				/* __DAVINCI_GPIO_H */