gpio.c 5.67 KB
Newer Older
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
/*
 * Gemini gpiochip and interrupt routines
 *
 * Copyright (C) 2008-2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
 *
 * Based on plat-mxc/gpio.c:
 *  MXC GPIO support. (c) 2008 Daniel Mack <daniel@caiaq.de>
 *  Copyright 2008 Juergen Beisert, kernel@pengutronix.de
 *
 * 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.
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/gpio.h>

#include <mach/hardware.h>
#include <mach/irqs.h>

#define GPIO_BASE(x)		IO_ADDRESS(GEMINI_GPIO_BASE(x))
26
#define irq_to_gpio(x)		((x) - GPIO_IRQ_BASE)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

/* GPIO registers definition */
#define GPIO_DATA_OUT		0x0
#define GPIO_DATA_IN		0x4
#define GPIO_DIR		0x8
#define GPIO_DATA_SET		0x10
#define GPIO_DATA_CLR		0x14
#define GPIO_PULL_EN		0x18
#define GPIO_PULL_TYPE		0x1C
#define GPIO_INT_EN		0x20
#define GPIO_INT_STAT		0x24
#define GPIO_INT_MASK		0x2C
#define GPIO_INT_CLR		0x30
#define GPIO_INT_TYPE		0x34
#define GPIO_INT_BOTH_EDGE	0x38
#define GPIO_INT_LEVEL		0x3C
#define GPIO_DEBOUNCE_EN	0x40
#define GPIO_DEBOUNCE_PRESCALE	0x44

#define GPIO_PORT_NUM		3

48
static void _set_gpio_irqenable(void __iomem *base, unsigned int index,
49 50 51 52 53 54 55 56 57
				int enable)
{
	unsigned int reg;

	reg = __raw_readl(base + GPIO_INT_EN);
	reg = (reg & (~(1 << index))) | (!!enable << index);
	__raw_writel(reg, base + GPIO_INT_EN);
}

58
static void gpio_ack_irq(struct irq_data *d)
59
{
60
	unsigned int gpio = irq_to_gpio(d->irq);
61
	void __iomem *base = GPIO_BASE(gpio / 32);
62 63 64 65

	__raw_writel(1 << (gpio % 32), base + GPIO_INT_CLR);
}

66
static void gpio_mask_irq(struct irq_data *d)
67
{
68
	unsigned int gpio = irq_to_gpio(d->irq);
69
	void __iomem *base = GPIO_BASE(gpio / 32);
70 71 72 73

	_set_gpio_irqenable(base, gpio % 32, 0);
}

74
static void gpio_unmask_irq(struct irq_data *d)
75
{
76
	unsigned int gpio = irq_to_gpio(d->irq);
77
	void __iomem *base = GPIO_BASE(gpio / 32);
78 79 80 81

	_set_gpio_irqenable(base, gpio % 32, 1);
}

82
static int gpio_set_irq_type(struct irq_data *d, unsigned int type)
83
{
84
	unsigned int gpio = irq_to_gpio(d->irq);
85
	unsigned int gpio_mask = 1 << (gpio % 32);
86
	void __iomem *base = GPIO_BASE(gpio / 32);
87 88 89
	unsigned int reg_both, reg_level, reg_type;

	reg_type = __raw_readl(base + GPIO_INT_TYPE);
90
	reg_level = __raw_readl(base + GPIO_INT_LEVEL);
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
	reg_both = __raw_readl(base + GPIO_INT_BOTH_EDGE);

	switch (type) {
	case IRQ_TYPE_EDGE_BOTH:
		reg_type &= ~gpio_mask;
		reg_both |= gpio_mask;
		break;
	case IRQ_TYPE_EDGE_RISING:
		reg_type &= ~gpio_mask;
		reg_both &= ~gpio_mask;
		reg_level &= ~gpio_mask;
		break;
	case IRQ_TYPE_EDGE_FALLING:
		reg_type &= ~gpio_mask;
		reg_both &= ~gpio_mask;
		reg_level |= gpio_mask;
		break;
	case IRQ_TYPE_LEVEL_HIGH:
		reg_type |= gpio_mask;
		reg_level &= ~gpio_mask;
		break;
	case IRQ_TYPE_LEVEL_LOW:
		reg_type |= gpio_mask;
		reg_level |= gpio_mask;
		break;
	default:
		return -EINVAL;
	}

	__raw_writel(reg_type, base + GPIO_INT_TYPE);
121
	__raw_writel(reg_level, base + GPIO_INT_LEVEL);
122 123
	__raw_writel(reg_both, base + GPIO_INT_BOTH_EDGE);

124
	gpio_ack_irq(d);
125 126 127 128

	return 0;
}

129
static void gpio_irq_handler(struct irq_desc *desc)
130
{
131
	unsigned int port = (unsigned int)irq_desc_get_handler_data(desc);
132 133 134 135 136 137 138 139 140 141
	unsigned int gpio_irq_no, irq_stat;

	irq_stat = __raw_readl(GPIO_BASE(port) + GPIO_INT_STAT);

	gpio_irq_no = GPIO_IRQ_BASE + port * 32;
	for (; irq_stat != 0; irq_stat >>= 1, gpio_irq_no++) {

		if ((irq_stat & 1) == 0)
			continue;

142
		generic_handle_irq(gpio_irq_no);
143 144 145 146 147
	}
}

static struct irq_chip gpio_irq_chip = {
	.name = "GPIO",
148 149 150 151
	.irq_ack = gpio_ack_irq,
	.irq_mask = gpio_mask_irq,
	.irq_unmask = gpio_unmask_irq,
	.irq_set_type = gpio_set_irq_type,
152 153 154 155 156
};

static void _set_gpio_direction(struct gpio_chip *chip, unsigned offset,
				int dir)
{
157
	void __iomem *base = GPIO_BASE(offset / 32);
158 159 160 161 162 163 164 165 166 167 168 169
	unsigned int reg;

	reg = __raw_readl(base + GPIO_DIR);
	if (dir)
		reg |= 1 << (offset % 32);
	else
		reg &= ~(1 << (offset % 32));
	__raw_writel(reg, base + GPIO_DIR);
}

static void gemini_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
{
170
	void __iomem *base = GPIO_BASE(offset / 32);
171 172 173 174 175 176 177 178 179

	if (value)
		__raw_writel(1 << (offset % 32), base + GPIO_DATA_SET);
	else
		__raw_writel(1 << (offset % 32), base + GPIO_DATA_CLR);
}

static int gemini_gpio_get(struct gpio_chip *chip, unsigned offset)
{
180
	void __iomem *base = GPIO_BASE(offset / 32);
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

	return (__raw_readl(base + GPIO_DATA_IN) >> (offset % 32)) & 1;
}

static int gemini_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
{
	_set_gpio_direction(chip, offset, 0);
	return 0;
}

static int gemini_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
					int value)
{
	_set_gpio_direction(chip, offset, 1);
	gemini_gpio_set(chip, offset, value);
	return 0;
}

static struct gpio_chip gemini_gpio_chip = {
	.label			= "Gemini",
	.direction_input	= gemini_gpio_direction_input,
	.get			= gemini_gpio_get,
	.direction_output	= gemini_gpio_direction_output,
	.set			= gemini_gpio_set,
	.base			= 0,
	.ngpio			= GPIO_PORT_NUM * 32,
};

void __init gemini_gpio_init(void)
{
	int i, j;

	for (i = 0; i < GPIO_PORT_NUM; i++) {
		/* disable, unmask and clear all interrupts */
		__raw_writel(0x0, GPIO_BASE(i) + GPIO_INT_EN);
		__raw_writel(0x0, GPIO_BASE(i) + GPIO_INT_MASK);
		__raw_writel(~0x0, GPIO_BASE(i) + GPIO_INT_CLR);

		for (j = GPIO_IRQ_BASE + i * 32;
		     j < GPIO_IRQ_BASE + (i + 1) * 32; j++) {
221 222
			irq_set_chip_and_handler(j, &gpio_irq_chip,
						 handle_edge_irq);
223
			irq_clear_status_flags(j, IRQ_NOREQUEST);
224 225
		}

226 227
		irq_set_chained_handler_and_data(IRQ_GPIO(i), gpio_irq_handler,
						 (void *)i);
228 229 230 231
	}

	BUG_ON(gpiochip_add(&gemini_gpio_chip));
}