gpio-timberdale.c 8.8 KB
Newer Older
1
/*
G
Grant Likely 已提交
2
 * Timberdale FPGA GPIO driver
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * Copyright (c) 2009 Intel Corporation
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* Supports:
 * Timberdale FPGA GPIO
 */

#include <linux/module.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
D
David Miller 已提交
26
#include <linux/irq.h>
27 28 29
#include <linux/io.h>
#include <linux/timb_gpio.h>
#include <linux/interrupt.h>
30
#include <linux/slab.h>
31 32 33 34 35 36 37 38 39 40 41

#define DRIVER_NAME "timb-gpio"

#define TGPIOVAL	0x00
#define TGPIODIR	0x04
#define TGPIO_IER	0x08
#define TGPIO_ISR	0x0c
#define TGPIO_IPR	0x10
#define TGPIO_ICR	0x14
#define TGPIO_FLR	0x18
#define TGPIO_LVR	0x1c
42 43
#define TGPIO_VER	0x20
#define TGPIO_BFLR	0x24
44 45 46 47 48 49

struct timbgpio {
	void __iomem		*membase;
	spinlock_t		lock; /* mutual exclusion */
	struct gpio_chip	gpio;
	int			irq_base;
50
	unsigned long		last_ier;
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
};

static int timbgpio_update_bit(struct gpio_chip *gpio, unsigned index,
	unsigned offset, bool enabled)
{
	struct timbgpio *tgpio = container_of(gpio, struct timbgpio, gpio);
	u32 reg;

	spin_lock(&tgpio->lock);
	reg = ioread32(tgpio->membase + offset);

	if (enabled)
		reg |= (1 << index);
	else
		reg &= ~(1 << index);

	iowrite32(reg, tgpio->membase + offset);
	spin_unlock(&tgpio->lock);

	return 0;
}

static int timbgpio_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
{
	return timbgpio_update_bit(gpio, nr, TGPIODIR, true);
}

static int timbgpio_gpio_get(struct gpio_chip *gpio, unsigned nr)
{
	struct timbgpio *tgpio = container_of(gpio, struct timbgpio, gpio);
	u32 value;

	value = ioread32(tgpio->membase + TGPIOVAL);
	return (value & (1 << nr)) ? 1 : 0;
}

static int timbgpio_gpio_direction_output(struct gpio_chip *gpio,
						unsigned nr, int val)
{
	return timbgpio_update_bit(gpio, nr, TGPIODIR, false);
}

static void timbgpio_gpio_set(struct gpio_chip *gpio,
				unsigned nr, int val)
{
	timbgpio_update_bit(gpio, nr, TGPIOVAL, val != 0);
}

static int timbgpio_to_irq(struct gpio_chip *gpio, unsigned offset)
{
	struct timbgpio *tgpio = container_of(gpio, struct timbgpio, gpio);

	if (tgpio->irq_base <= 0)
		return -EINVAL;

	return tgpio->irq_base + offset;
}

/*
 * GPIO IRQ
 */
112
static void timbgpio_irq_disable(struct irq_data *d)
113
{
114 115
	struct timbgpio *tgpio = irq_data_get_irq_chip_data(d);
	int offset = d->irq - tgpio->irq_base;
116
	unsigned long flags;
117

118
	spin_lock_irqsave(&tgpio->lock, flags);
119
	tgpio->last_ier &= ~(1UL << offset);
120 121
	iowrite32(tgpio->last_ier, tgpio->membase + TGPIO_IER);
	spin_unlock_irqrestore(&tgpio->lock, flags);
122 123
}

124
static void timbgpio_irq_enable(struct irq_data *d)
125
{
126 127
	struct timbgpio *tgpio = irq_data_get_irq_chip_data(d);
	int offset = d->irq - tgpio->irq_base;
128
	unsigned long flags;
129

130
	spin_lock_irqsave(&tgpio->lock, flags);
131
	tgpio->last_ier |= 1UL << offset;
132 133
	iowrite32(tgpio->last_ier, tgpio->membase + TGPIO_IER);
	spin_unlock_irqrestore(&tgpio->lock, flags);
134 135
}

136
static int timbgpio_irq_type(struct irq_data *d, unsigned trigger)
137
{
138 139
	struct timbgpio *tgpio = irq_data_get_irq_chip_data(d);
	int offset = d->irq - tgpio->irq_base;
140
	unsigned long flags;
141 142
	u32 lvr, flr, bflr = 0;
	u32 ver;
143
	int ret = 0;
144 145 146 147

	if (offset < 0 || offset > tgpio->gpio.ngpio)
		return -EINVAL;

148 149
	ver = ioread32(tgpio->membase + TGPIO_VER);

150 151 152 153
	spin_lock_irqsave(&tgpio->lock, flags);

	lvr = ioread32(tgpio->membase + TGPIO_LVR);
	flr = ioread32(tgpio->membase + TGPIO_FLR);
154 155
	if (ver > 2)
		bflr = ioread32(tgpio->membase + TGPIO_BFLR);
156 157

	if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
158
		bflr &= ~(1 << offset);
159 160 161 162 163 164 165
		flr &= ~(1 << offset);
		if (trigger & IRQ_TYPE_LEVEL_HIGH)
			lvr |= 1 << offset;
		else
			lvr &= ~(1 << offset);
	}

166
	if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
167 168 169 170
		if (ver < 3) {
			ret = -EINVAL;
			goto out;
		}
171 172 173 174 175 176
		else {
			flr |= 1 << offset;
			bflr |= 1 << offset;
		}
	} else {
		bflr &= ~(1 << offset);
177 178 179
		flr |= 1 << offset;
		if (trigger & IRQ_TYPE_EDGE_FALLING)
			lvr &= ~(1 << offset);
180 181
		else
			lvr |= 1 << offset;
182 183 184 185
	}

	iowrite32(lvr, tgpio->membase + TGPIO_LVR);
	iowrite32(flr, tgpio->membase + TGPIO_FLR);
186 187 188
	if (ver > 2)
		iowrite32(bflr, tgpio->membase + TGPIO_BFLR);

189 190
	iowrite32(1 << offset, tgpio->membase + TGPIO_ICR);

191 192 193
out:
	spin_unlock_irqrestore(&tgpio->lock, flags);
	return ret;
194 195
}

196
static void timbgpio_irq(unsigned int irq, struct irq_desc *desc)
197
{
T
Thomas Gleixner 已提交
198
	struct timbgpio *tgpio = irq_get_handler_data(irq);
199 200 201
	unsigned long ipr;
	int offset;

202
	desc->irq_data.chip->irq_ack(irq_get_irq_data(irq));
203 204 205
	ipr = ioread32(tgpio->membase + TGPIO_IPR);
	iowrite32(ipr, tgpio->membase + TGPIO_ICR);

206 207 208 209 210 211
	/*
	 * Some versions of the hardware trash the IER register if more than
	 * one interrupt is received simultaneously.
	 */
	iowrite32(0, tgpio->membase + TGPIO_IER);

212
	for_each_set_bit(offset, &ipr, tgpio->gpio.ngpio)
213
		generic_handle_irq(timbgpio_to_irq(&tgpio->gpio, offset));
214 215

	iowrite32(tgpio->last_ier, tgpio->membase + TGPIO_IER);
216 217 218 219
}

static struct irq_chip timbgpio_irqchip = {
	.name		= "GPIO",
220 221 222
	.irq_enable	= timbgpio_irq_enable,
	.irq_disable	= timbgpio_irq_disable,
	.irq_set_type	= timbgpio_irq_type,
223 224
};

B
Bill Pemberton 已提交
225
static int timbgpio_probe(struct platform_device *pdev)
226 227 228 229 230
{
	int err, i;
	struct gpio_chip *gc;
	struct timbgpio *tgpio;
	struct resource *iomem;
231
	struct timbgpio_platform_data *pdata = pdev->dev.platform_data;
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
	int irq = platform_get_irq(pdev, 0);

	if (!pdata || pdata->nr_pins > 32) {
		err = -EINVAL;
		goto err_mem;
	}

	iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!iomem) {
		err = -EINVAL;
		goto err_mem;
	}

	tgpio = kzalloc(sizeof(*tgpio), GFP_KERNEL);
	if (!tgpio) {
		err = -EINVAL;
		goto err_mem;
	}
	tgpio->irq_base = pdata->irq_base;

	spin_lock_init(&tgpio->lock);

	if (!request_mem_region(iomem->start, resource_size(iomem),
		DRIVER_NAME)) {
		err = -EBUSY;
		goto err_request;
	}

	tgpio->membase = ioremap(iomem->start, resource_size(iomem));
	if (!tgpio->membase) {
		err = -ENOMEM;
		goto err_ioremap;
	}

	gc = &tgpio->gpio;

	gc->label = dev_name(&pdev->dev);
	gc->owner = THIS_MODULE;
	gc->dev = &pdev->dev;
	gc->direction_input = timbgpio_gpio_direction_input;
	gc->get = timbgpio_gpio_get;
	gc->direction_output = timbgpio_gpio_direction_output;
	gc->set = timbgpio_gpio_set;
	gc->to_irq = (irq >= 0 && tgpio->irq_base > 0) ? timbgpio_to_irq : NULL;
	gc->dbg_show = NULL;
	gc->base = pdata->gpio_base;
	gc->ngpio = pdata->nr_pins;
	gc->can_sleep = 0;

	err = gpiochip_add(gc);
	if (err)
		goto err_chipadd;

	platform_set_drvdata(pdev, tgpio);

	/* make sure to disable interrupts */
	iowrite32(0x0, tgpio->membase + TGPIO_IER);

	if (irq < 0 || tgpio->irq_base <= 0)
		return 0;

	for (i = 0; i < pdata->nr_pins; i++) {
T
Thomas Gleixner 已提交
294
		irq_set_chip_and_handler_name(tgpio->irq_base + i,
295
			&timbgpio_irqchip, handle_simple_irq, "mux");
T
Thomas Gleixner 已提交
296
		irq_set_chip_data(tgpio->irq_base + i, tgpio);
297 298 299 300 301
#ifdef CONFIG_ARM
		set_irq_flags(tgpio->irq_base + i, IRQF_VALID | IRQF_PROBE);
#endif
	}

T
Thomas Gleixner 已提交
302 303
	irq_set_handler_data(irq, tgpio);
	irq_set_chained_handler(irq, timbgpio_irq);
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

	return 0;

err_chipadd:
	iounmap(tgpio->membase);
err_ioremap:
	release_mem_region(iomem->start, resource_size(iomem));
err_request:
	kfree(tgpio);
err_mem:
	printk(KERN_ERR DRIVER_NAME": Failed to register GPIOs: %d\n", err);

	return err;
}

static int __devexit timbgpio_remove(struct platform_device *pdev)
{
	int err;
322
	struct timbgpio_platform_data *pdata = pdev->dev.platform_data;
323 324 325 326 327 328
	struct timbgpio *tgpio = platform_get_drvdata(pdev);
	struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	int irq = platform_get_irq(pdev, 0);

	if (irq >= 0 && tgpio->irq_base > 0) {
		int i;
329
		for (i = 0; i < pdata->nr_pins; i++) {
T
Thomas Gleixner 已提交
330 331
			irq_set_chip(tgpio->irq_base + i, NULL);
			irq_set_chip_data(tgpio->irq_base + i, NULL);
332 333
		}

T
Thomas Gleixner 已提交
334 335
		irq_set_handler(irq, NULL);
		irq_set_handler_data(irq, NULL);
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
	}

	err = gpiochip_remove(&tgpio->gpio);
	if (err)
		printk(KERN_ERR DRIVER_NAME": failed to remove gpio_chip\n");

	iounmap(tgpio->membase);
	release_mem_region(iomem->start, resource_size(iomem));
	kfree(tgpio);

	platform_set_drvdata(pdev, NULL);

	return 0;
}

static struct platform_driver timbgpio_platform_driver = {
	.driver = {
		.name	= DRIVER_NAME,
		.owner	= THIS_MODULE,
	},
	.probe		= timbgpio_probe,
	.remove		= timbgpio_remove,
};

/*--------------------------------------------------------------------------*/

362
module_platform_driver(timbgpio_platform_driver);
363 364 365 366 367 368

MODULE_DESCRIPTION("Timberdale GPIO driver");
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Mocean Laboratories");
MODULE_ALIAS("platform:"DRIVER_NAME);