irq-gpioint.c 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* linux/arch/arm/plat-s5p/irq-gpioint.c
 *
 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
 * Author: Kyungmin Park <kyungmin.park@samsung.com>
 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
 * Author: Marek Szyprowski <m.szyprowski@samsung.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.
 *
 */

#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/io.h>
#include <linux/gpio.h>
20
#include <linux/slab.h>
21 22 23 24 25

#include <mach/map.h>
#include <plat/gpio-core.h>
#include <plat/gpio-cfg.h>

26 27
#include <asm/mach/irq.h>

28
#define GPIO_BASE(chip)		(((unsigned long)(chip)->base) & 0xFFFFF000u)
29

30 31 32 33
#define CON_OFFSET		0x700
#define MASK_OFFSET		0x900
#define PEND_OFFSET		0xA00
#define REG_OFFSET(x)		((x) << 2)
34

35 36 37 38 39 40 41 42 43 44
struct s5p_gpioint_bank {
	struct list_head	list;
	int			start;
	int			nr_groups;
	int			irq;
	struct s3c_gpio_chip	**chips;
	void			(*handler)(unsigned int, struct irq_desc *);
};

LIST_HEAD(banks);
45

46
static int s5p_gpioint_set_type(struct irq_data *d, unsigned int type)
47
{
48 49 50
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
	struct irq_chip_type *ct = gc->chip_types;
	unsigned int shift = (d->irq - gc->irq_base) << 2;
51 52 53

	switch (type) {
	case IRQ_TYPE_EDGE_RISING:
54
		type = S5P_IRQ_TYPE_EDGE_RISING;
55 56
		break;
	case IRQ_TYPE_EDGE_FALLING:
57
		type = S5P_IRQ_TYPE_EDGE_FALLING;
58 59
		break;
	case IRQ_TYPE_EDGE_BOTH:
60
		type = S5P_IRQ_TYPE_EDGE_BOTH;
61 62
		break;
	case IRQ_TYPE_LEVEL_HIGH:
63
		type = S5P_IRQ_TYPE_LEVEL_HIGH;
64 65
		break;
	case IRQ_TYPE_LEVEL_LOW:
66
		type = S5P_IRQ_TYPE_LEVEL_LOW;
67 68 69 70 71 72 73
		break;
	case IRQ_TYPE_NONE:
	default:
		printk(KERN_WARNING "No irq type\n");
		return -EINVAL;
	}

74 75 76
	gc->type_cache &= ~(0x7 << shift);
	gc->type_cache |= type << shift;
	writel(gc->type_cache, gc->reg_base + ct->regs.type);
77 78 79 80 81
	return 0;
}

static void s5p_gpioint_handler(unsigned int irq, struct irq_desc *desc)
{
T
Thomas Gleixner 已提交
82
	struct s5p_gpioint_bank *bank = irq_get_handler_data(irq);
83
	int group, pend_offset, mask_offset;
84 85
	unsigned int pend, mask;

86 87 88
	struct irq_chip *chip = irq_get_chip(irq);
	chained_irq_enter(chip, desc);

89 90
	for (group = 0; group < bank->nr_groups; group++) {
		struct s3c_gpio_chip *chip = bank->chips[group];
91 92 93 94 95
		if (!chip)
			continue;

		pend_offset = REG_OFFSET(group);
		pend = __raw_readl(GPIO_BASE(chip) + PEND_OFFSET + pend_offset);
96 97 98
		if (!pend)
			continue;

99 100
		mask_offset = REG_OFFSET(group);
		mask = __raw_readl(GPIO_BASE(chip) + MASK_OFFSET + mask_offset);
101 102
		pend &= ~mask;

103 104 105 106 107
		while (pend) {
			int offset = fls(pend) - 1;
			int real_irq = chip->irq_base + offset;
			generic_handle_irq(real_irq);
			pend &= ~BIT(offset);
108 109
		}
	}
110
	chained_irq_exit(chip, desc);
111 112 113 114 115
}

static __init int s5p_gpioint_add(struct s3c_gpio_chip *chip)
{
	static int used_gpioint_groups = 0;
116
	int group = chip->group;
117
	struct s5p_gpioint_bank *b, *bank = NULL;
118 119
	struct irq_chip_generic *gc;
	struct irq_chip_type *ct;
120 121 122 123

	if (used_gpioint_groups >= S5P_GPIOINT_GROUP_COUNT)
		return -ENOMEM;

124 125 126
	list_for_each_entry(b, &banks, list) {
		if (group >= b->start && group < b->start + b->nr_groups) {
			bank = b;
127
			break;
128
		}
129 130 131 132 133 134 135 136 137 138
	}
	if (!bank)
		return -EINVAL;

	if (!bank->handler) {
		bank->chips = kzalloc(sizeof(struct s3c_gpio_chip *) *
				      bank->nr_groups, GFP_KERNEL);
		if (!bank->chips)
			return -ENOMEM;

T
Thomas Gleixner 已提交
139 140
		irq_set_chained_handler(bank->irq, s5p_gpioint_handler);
		irq_set_handler_data(bank->irq, bank);
141 142 143 144 145 146
		bank->handler = s5p_gpioint_handler;
		printk(KERN_INFO "Registered chained gpio int handler for interrupt %d.\n",
		       bank->irq);
	}

	/*
L
Lucas De Marchi 已提交
147
	 * chained GPIO irq has been successfully registered, allocate new gpio
148 149
	 * int group and assign irq nubmers
	 */
150 151 152 153
	chip->irq_base = S5P_GPIOINT_BASE +
			 used_gpioint_groups * S5P_GPIOINT_GROUP_SIZE;
	used_gpioint_groups++;

154
	bank->chips[group - bank->start] = chip;
155 156 157 158 159 160 161

	gc = irq_alloc_generic_chip("s5p_gpioint", 1, chip->irq_base,
				    (void __iomem *)GPIO_BASE(chip),
				    handle_level_irq);
	if (!gc)
		return -ENOMEM;
	ct = gc->chip_types;
162
	ct->chip.irq_ack = irq_gc_ack_set_bit;
163 164 165
	ct->chip.irq_mask = irq_gc_mask_set_bit;
	ct->chip.irq_unmask = irq_gc_mask_clr_bit;
	ct->chip.irq_set_type = s5p_gpioint_set_type,
166 167 168
	ct->regs.ack = PEND_OFFSET + REG_OFFSET(group - bank->start);
	ct->regs.mask = MASK_OFFSET + REG_OFFSET(group - bank->start);
	ct->regs.type = CON_OFFSET + REG_OFFSET(group - bank->start);
169 170 171
	irq_setup_generic_chip(gc, IRQ_MSK(chip->chip.ngpio),
			       IRQ_GC_INIT_MASK_CACHE,
			       IRQ_NOREQUEST | IRQ_NOPROBE, 0);
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	return 0;
}

int __init s5p_register_gpio_interrupt(int pin)
{
	struct s3c_gpio_chip *my_chip = s3c_gpiolib_getchip(pin);
	int offset, group;
	int ret;

	if (!my_chip)
		return -EINVAL;

	offset = pin - my_chip->chip.base;
	group = my_chip->group;

	/* check if the group has been already registered */
	if (my_chip->irq_base)
		return my_chip->irq_base + offset;

	/* register gpio group */
	ret = s5p_gpioint_add(my_chip);
	if (ret == 0) {
194
		my_chip->chip.to_irq = samsung_gpiolib_to_irq;
195 196 197 198 199 200
		printk(KERN_INFO "Registered interrupt support for gpio group %d.\n",
		       group);
		return my_chip->irq_base + offset;
	}
	return ret;
}
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

int __init s5p_register_gpioint_bank(int chain_irq, int start, int nr_groups)
{
	struct s5p_gpioint_bank *bank;

	bank = kzalloc(sizeof(*bank), GFP_KERNEL);
	if (!bank)
		return -ENOMEM;

	bank->start = start;
	bank->nr_groups = nr_groups;
	bank->irq = chain_irq;

	list_add_tail(&bank->list, &banks);
	return 0;
}