gpio.c 14.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * arch/arm/plat-orion/gpio.c
 *
 * Marvell Orion SoC GPIO handling.
 *
 * This file is licensed under the terms of the GNU General Public
 * License version 2.  This program is licensed "as is" without any
 * warranty of any kind, whether express or implied.
 */

11 12
#define DEBUG

13 14
#include <linux/kernel.h>
#include <linux/init.h>
15
#include <linux/irq.h>
16
#include <linux/irqdomain.h>
17 18 19 20
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/bitops.h>
#include <linux/io.h>
21
#include <linux/gpio.h>
22
#include <linux/leds.h>
23 24 25
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/of_address.h>
26
#include <plat/orion-gpio.h>
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*
 * GPIO unit register offsets.
 */
#define GPIO_OUT_OFF		0x0000
#define GPIO_IO_CONF_OFF	0x0004
#define GPIO_BLINK_EN_OFF	0x0008
#define GPIO_IN_POL_OFF		0x000c
#define GPIO_DATA_IN_OFF	0x0010
#define GPIO_EDGE_CAUSE_OFF	0x0014
#define GPIO_EDGE_MASK_OFF	0x0018
#define GPIO_LEVEL_MASK_OFF	0x001c

struct orion_gpio_chip {
	struct gpio_chip	chip;
	spinlock_t		lock;
	void __iomem		*base;
	unsigned long		valid_input;
	unsigned long		valid_output;
	int			mask_offset;
	int			secondary_irq_base;
48
	struct irq_domain       *domain;
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
};

static void __iomem *GPIO_OUT(struct orion_gpio_chip *ochip)
{
	return ochip->base + GPIO_OUT_OFF;
}

static void __iomem *GPIO_IO_CONF(struct orion_gpio_chip *ochip)
{
	return ochip->base + GPIO_IO_CONF_OFF;
}

static void __iomem *GPIO_BLINK_EN(struct orion_gpio_chip *ochip)
{
	return ochip->base + GPIO_BLINK_EN_OFF;
}

static void __iomem *GPIO_IN_POL(struct orion_gpio_chip *ochip)
{
	return ochip->base + GPIO_IN_POL_OFF;
}

static void __iomem *GPIO_DATA_IN(struct orion_gpio_chip *ochip)
{
	return ochip->base + GPIO_DATA_IN_OFF;
}

static void __iomem *GPIO_EDGE_CAUSE(struct orion_gpio_chip *ochip)
{
	return ochip->base + GPIO_EDGE_CAUSE_OFF;
}

static void __iomem *GPIO_EDGE_MASK(struct orion_gpio_chip *ochip)
{
	return ochip->base + ochip->mask_offset + GPIO_EDGE_MASK_OFF;
}

static void __iomem *GPIO_LEVEL_MASK(struct orion_gpio_chip *ochip)
{
	return ochip->base + ochip->mask_offset + GPIO_LEVEL_MASK_OFF;
}

91

92 93 94 95 96
static struct orion_gpio_chip orion_gpio_chips[2];
static int orion_gpio_chip_count;

static inline void
__set_direction(struct orion_gpio_chip *ochip, unsigned pin, int input)
97 98 99
{
	u32 u;

100
	u = readl(GPIO_IO_CONF(ochip));
101
	if (input)
102
		u |= 1 << pin;
103
	else
104 105
		u &= ~(1 << pin);
	writel(u, GPIO_IO_CONF(ochip));
106 107
}

108
static void __set_level(struct orion_gpio_chip *ochip, unsigned pin, int high)
109 110 111
{
	u32 u;

112
	u = readl(GPIO_OUT(ochip));
113
	if (high)
114
		u |= 1 << pin;
115
	else
116 117
		u &= ~(1 << pin);
	writel(u, GPIO_OUT(ochip));
118 119
}

120 121
static inline void
__set_blinking(struct orion_gpio_chip *ochip, unsigned pin, int blink)
122
{
123
	u32 u;
124

125
	u = readl(GPIO_BLINK_EN(ochip));
126
	if (blink)
127
		u |= 1 << pin;
128
	else
129 130
		u &= ~(1 << pin);
	writel(u, GPIO_BLINK_EN(ochip));
131
}
132

133 134
static inline int
orion_gpio_is_valid(struct orion_gpio_chip *ochip, unsigned pin, int mode)
135
{
136 137 138 139 140 141 142 143 144 145
	if (pin >= ochip->chip.ngpio)
		goto err_out;

	if ((mode & GPIO_INPUT_OK) && !test_bit(pin, &ochip->valid_input))
		goto err_out;

	if ((mode & GPIO_OUTPUT_OK) && !test_bit(pin, &ochip->valid_output))
		goto err_out;

	return 1;
146

147 148 149
err_out:
	pr_debug("%s: invalid GPIO %d\n", __func__, pin);
	return false;
150 151
}

152 153 154
/*
 * GENERIC_GPIO primitives.
 */
155 156 157 158 159 160 161 162 163 164 165 166
static int orion_gpio_request(struct gpio_chip *chip, unsigned pin)
{
	struct orion_gpio_chip *ochip =
		container_of(chip, struct orion_gpio_chip, chip);

	if (orion_gpio_is_valid(ochip, pin, GPIO_INPUT_OK) ||
	    orion_gpio_is_valid(ochip, pin, GPIO_OUTPUT_OK))
		return 0;

	return -EINVAL;
}

167
static int orion_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
168
{
169 170
	struct orion_gpio_chip *ochip =
		container_of(chip, struct orion_gpio_chip, chip);
171 172
	unsigned long flags;

173
	if (!orion_gpio_is_valid(ochip, pin, GPIO_INPUT_OK))
174 175
		return -EINVAL;

176 177 178
	spin_lock_irqsave(&ochip->lock, flags);
	__set_direction(ochip, pin, 1);
	spin_unlock_irqrestore(&ochip->lock, flags);
179 180 181 182

	return 0;
}

183
static int orion_gpio_get(struct gpio_chip *chip, unsigned pin)
184
{
185 186
	struct orion_gpio_chip *ochip =
		container_of(chip, struct orion_gpio_chip, chip);
187 188
	int val;

189 190 191 192 193
	if (readl(GPIO_IO_CONF(ochip)) & (1 << pin)) {
		val = readl(GPIO_DATA_IN(ochip)) ^ readl(GPIO_IN_POL(ochip));
	} else {
		val = readl(GPIO_OUT(ochip));
	}
194

195
	return (val >> pin) & 1;
196 197
}

198 199
static int
orion_gpio_direction_output(struct gpio_chip *chip, unsigned pin, int value)
200
{
201 202
	struct orion_gpio_chip *ochip =
		container_of(chip, struct orion_gpio_chip, chip);
203
	unsigned long flags;
204

205
	if (!orion_gpio_is_valid(ochip, pin, GPIO_OUTPUT_OK))
206
		return -EINVAL;
207

208 209 210 211 212
	spin_lock_irqsave(&ochip->lock, flags);
	__set_blinking(ochip, pin, 0);
	__set_level(ochip, pin, value);
	__set_direction(ochip, pin, 0);
	spin_unlock_irqrestore(&ochip->lock, flags);
213 214

	return 0;
215 216
}

217
static void orion_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
218
{
219 220
	struct orion_gpio_chip *ochip =
		container_of(chip, struct orion_gpio_chip, chip);
221 222
	unsigned long flags;

223 224 225
	spin_lock_irqsave(&ochip->lock, flags);
	__set_level(ochip, pin, value);
	spin_unlock_irqrestore(&ochip->lock, flags);
226 227
}

228
static int orion_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
229
{
230 231
	struct orion_gpio_chip *ochip =
		container_of(chip, struct orion_gpio_chip, chip);
232

233 234
	return irq_create_mapping(ochip->domain,
				  ochip->secondary_irq_base + pin);
235
}
236 237 238 239

/*
 * Orion-specific GPIO API extensions.
 */
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
static struct orion_gpio_chip *orion_gpio_chip_find(int pin)
{
	int i;

	for (i = 0; i < orion_gpio_chip_count; i++) {
		struct orion_gpio_chip *ochip = orion_gpio_chips + i;
		struct gpio_chip *chip = &ochip->chip;

		if (pin >= chip->base && pin < chip->base + chip->ngpio)
			return ochip;
	}

	return NULL;
}

255 256
void __init orion_gpio_set_unused(unsigned pin)
{
257 258 259 260 261 262 263
	struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);

	if (ochip == NULL)
		return;

	pin -= ochip->chip.base;

264
	/* Configure as output, drive low. */
265 266
	__set_level(ochip, pin, 0);
	__set_direction(ochip, pin, 0);
267 268
}

269
void __init orion_gpio_set_valid(unsigned pin, int mode)
270
{
271 272 273 274 275 276 277
	struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);

	if (ochip == NULL)
		return;

	pin -= ochip->chip.base;

278 279
	if (mode == 1)
		mode = GPIO_INPUT_OK | GPIO_OUTPUT_OK;
280

281
	if (mode & GPIO_INPUT_OK)
282
		__set_bit(pin, &ochip->valid_input);
283
	else
284 285
		__clear_bit(pin, &ochip->valid_input);

286
	if (mode & GPIO_OUTPUT_OK)
287
		__set_bit(pin, &ochip->valid_output);
288
	else
289
		__clear_bit(pin, &ochip->valid_output);
290 291 292 293
}

void orion_gpio_set_blink(unsigned pin, int blink)
{
294
	struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
295 296
	unsigned long flags;

297 298
	if (ochip == NULL)
		return;
299

300
	spin_lock_irqsave(&ochip->lock, flags);
301 302
	__set_level(ochip, pin & 31, 0);
	__set_blinking(ochip, pin & 31, blink);
303
	spin_unlock_irqrestore(&ochip->lock, flags);
304 305
}
EXPORT_SYMBOL(orion_gpio_set_blink);
306

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
#define ORION_BLINK_HALF_PERIOD 100 /* ms */

int orion_gpio_led_blink_set(unsigned gpio, int state,
	unsigned long *delay_on, unsigned long *delay_off)
{

	if (delay_on && delay_off && !*delay_on && !*delay_off)
		*delay_on = *delay_off = ORION_BLINK_HALF_PERIOD;

	switch (state) {
	case GPIO_LED_NO_BLINK_LOW:
	case GPIO_LED_NO_BLINK_HIGH:
		orion_gpio_set_blink(gpio, 0);
		gpio_set_value(gpio, state);
		break;
	case GPIO_LED_BLINK:
		orion_gpio_set_blink(gpio, 1);
	}
	return 0;
}
EXPORT_SYMBOL_GPL(orion_gpio_led_blink_set);

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

/*****************************************************************************
 * Orion GPIO IRQ
 *
 * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
 * value of the line or the opposite value.
 *
 * Level IRQ handlers: DATA_IN is used directly as cause register.
 *                     Interrupt are masked by LEVEL_MASK registers.
 * Edge IRQ handlers:  Change in DATA_IN are latched in EDGE_CAUSE.
 *                     Interrupt are masked by EDGE_MASK registers.
 * Both-edge handlers: Similar to regular Edge handlers, but also swaps
 *                     the polarity to catch the next line transaction.
 *                     This is a race condition that might not perfectly
 *                     work on some use cases.
 *
 * Every eight GPIO lines are grouped (OR'ed) before going up to main
 * cause register.
 *
 *                    EDGE  cause    mask
 *        data-in   /--------| |-----| |----\
 *     -----| |-----                         ---- to main cause reg
 *           X      \----------------| |----/
 *        polarity    LEVEL          mask
 *
 ****************************************************************************/

356
static int gpio_irq_set_type(struct irq_data *d, u32 type)
357
{
358 359 360
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
	struct irq_chip_type *ct = irq_data_get_chip_type(d);
	struct orion_gpio_chip *ochip = gc->private;
361
	int pin;
362 363
	u32 u;

364
	pin = d->hwirq - ochip->secondary_irq_base;
365 366

	u = readl(GPIO_IO_CONF(ochip)) & (1 << pin);
367 368 369 370
	if (!u) {
		return -EINVAL;
	}

371 372
	type &= IRQ_TYPE_SENSE_MASK;
	if (type == IRQ_TYPE_NONE)
373
		return -EINVAL;
374 375 376 377 378

	/* Check if we need to change chip and handler */
	if (!(ct->type & type))
		if (irq_setup_alt_chip(d, type))
			return -EINVAL;
379 380 381 382 383

	/*
	 * Configure interrupt polarity.
	 */
	if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH) {
384 385 386
		u = readl(GPIO_IN_POL(ochip));
		u &= ~(1 << pin);
		writel(u, GPIO_IN_POL(ochip));
387
	} else if (type == IRQ_TYPE_EDGE_FALLING || type == IRQ_TYPE_LEVEL_LOW) {
388 389 390
		u = readl(GPIO_IN_POL(ochip));
		u |= 1 << pin;
		writel(u, GPIO_IN_POL(ochip));
391 392 393
	} else if (type == IRQ_TYPE_EDGE_BOTH) {
		u32 v;

394
		v = readl(GPIO_IN_POL(ochip)) ^ readl(GPIO_DATA_IN(ochip));
395 396 397 398

		/*
		 * set initial polarity based on current input level
		 */
399 400 401
		u = readl(GPIO_IN_POL(ochip));
		if (v & (1 << pin))
			u |= 1 << pin;		/* falling */
402
		else
403 404
			u &= ~(1 << pin);	/* rising */
		writel(u, GPIO_IN_POL(ochip));
405 406 407 408
	}
	return 0;
}

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
static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
{
	struct orion_gpio_chip *ochip = irq_get_handler_data(irq);
	u32 cause, type;
	int i;

	if (ochip == NULL)
		return;

	cause = readl(GPIO_DATA_IN(ochip)) & readl(GPIO_LEVEL_MASK(ochip));
	cause |= readl(GPIO_EDGE_CAUSE(ochip)) & readl(GPIO_EDGE_MASK(ochip));

	for (i = 0; i < ochip->chip.ngpio; i++) {
		int irq;

		irq = ochip->secondary_irq_base + i;

		if (!(cause & (1 << i)))
			continue;

		type = irqd_get_trigger_type(irq_get_irq_data(irq));
		if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
			/* Swap polarity (race with GPIO line) */
			u32 polarity;

			polarity = readl(GPIO_IN_POL(ochip));
			polarity ^= 1 << i;
			writel(polarity, GPIO_IN_POL(ochip));
		}
		generic_handle_irq(irq);
	}
}

void __init orion_gpio_init(struct device_node *np,
			    int gpio_base, int ngpio,
			    void __iomem *base, int mask_offset,
			    int secondary_irq_base,
			    int irqs[4])
447 448
{
	struct orion_gpio_chip *ochip;
449 450
	struct irq_chip_generic *gc;
	struct irq_chip_type *ct;
451
	char gc_label[16];
452
	int i;
453 454 455 456

	if (orion_gpio_chip_count == ARRAY_SIZE(orion_gpio_chips))
		return;

457 458 459
	snprintf(gc_label, sizeof(gc_label), "orion_gpio%d",
		orion_gpio_chip_count);

460
	ochip = orion_gpio_chips + orion_gpio_chip_count;
461
	ochip->chip.label = kstrdup(gc_label, GFP_KERNEL);
462 463 464 465 466 467 468 469 470
	ochip->chip.request = orion_gpio_request;
	ochip->chip.direction_input = orion_gpio_direction_input;
	ochip->chip.get = orion_gpio_get;
	ochip->chip.direction_output = orion_gpio_direction_output;
	ochip->chip.set = orion_gpio_set;
	ochip->chip.to_irq = orion_gpio_to_irq;
	ochip->chip.base = gpio_base;
	ochip->chip.ngpio = ngpio;
	ochip->chip.can_sleep = 0;
471 472 473 474
#ifdef CONFIG_OF
	ochip->chip.of_node = np;
#endif

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
	spin_lock_init(&ochip->lock);
	ochip->base = (void __iomem *)base;
	ochip->valid_input = 0;
	ochip->valid_output = 0;
	ochip->mask_offset = mask_offset;
	ochip->secondary_irq_base = secondary_irq_base;

	gpiochip_add(&ochip->chip);

	/*
	 * Mask and clear GPIO interrupts.
	 */
	writel(0, GPIO_EDGE_CAUSE(ochip));
	writel(0, GPIO_EDGE_MASK(ochip));
	writel(0, GPIO_LEVEL_MASK(ochip));

491 492 493 494 495 496 497 498 499 500 501 502 503
	/* Setup the interrupt handlers. Each chip can have up to 4
	 * interrupt handlers, with each handler dealing with 8 GPIO
	 * pins. */

	for (i = 0; i < 4; i++) {
		if (irqs[i]) {
			irq_set_handler_data(irqs[i], ochip);
			irq_set_chained_handler(irqs[i], gpio_irq_handler);
		}
	}

	gc = irq_alloc_generic_chip("orion_gpio_irq", 2,
				    secondary_irq_base,
504 505 506 507 508 509 510 511
				    ochip->base, handle_level_irq);
	gc->private = ochip;
	ct = gc->chip_types;
	ct->regs.mask = ochip->mask_offset + GPIO_LEVEL_MASK_OFF;
	ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
	ct->chip.irq_mask = irq_gc_mask_clr_bit;
	ct->chip.irq_unmask = irq_gc_mask_set_bit;
	ct->chip.irq_set_type = gpio_irq_set_type;
512
	ct->chip.name = ochip->chip.label;
513 514 515 516 517

	ct++;
	ct->regs.mask = ochip->mask_offset + GPIO_EDGE_MASK_OFF;
	ct->regs.ack = GPIO_EDGE_CAUSE_OFF;
	ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
518
	ct->chip.irq_ack = irq_gc_ack_clr_bit;
519 520 521 522
	ct->chip.irq_mask = irq_gc_mask_clr_bit;
	ct->chip.irq_unmask = irq_gc_mask_set_bit;
	ct->chip.irq_set_type = gpio_irq_set_type;
	ct->handler = handle_edge_irq;
523
	ct->chip.name = ochip->chip.label;
524 525 526

	irq_setup_generic_chip(gc, IRQ_MSK(ngpio), IRQ_GC_INIT_MASK_CACHE,
			       IRQ_NOREQUEST, IRQ_LEVEL | IRQ_NOPROBE);
527

528 529 530 531 532 533 534 535 536 537
	/* Setup irq domain on top of the generic chip. */
	ochip->domain = irq_domain_add_legacy(np,
					      ochip->chip.ngpio,
					      ochip->secondary_irq_base,
					      ochip->secondary_irq_base,
					      &irq_domain_simple_ops,
					      ochip);
	if (!ochip->domain)
		panic("%s: couldn't allocate irq domain (DT).\n",
		      ochip->chip.label);
538

539 540
	orion_gpio_chip_count++;
}
541

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
#ifdef CONFIG_OF
static void __init orion_gpio_of_init_one(struct device_node *np,
					  int irq_gpio_base)
{
	int ngpio, gpio_base, mask_offset;
	void __iomem *base;
	int ret, i;
	int irqs[4];
	int secondary_irq_base;

	ret = of_property_read_u32(np, "ngpio", &ngpio);
	if (ret)
		goto out;
	ret = of_property_read_u32(np, "mask-offset", &mask_offset);
	if (ret == -EINVAL)
		mask_offset = 0;
	else
		goto out;
	base = of_iomap(np, 0);
	if (!base)
		goto out;

	secondary_irq_base = irq_gpio_base + (32 * orion_gpio_chip_count);
	gpio_base = 32 * orion_gpio_chip_count;

	/* Get the interrupt numbers. Each chip can have up to 4
	 * interrupt handlers, with each handler dealing with 8 GPIO
	 * pins. */

	for (i = 0; i < 4; i++)
		irqs[i] = irq_of_parse_and_map(np, i);

	orion_gpio_init(np, gpio_base, ngpio, base, mask_offset,
			secondary_irq_base, irqs);
	return;
out:
	pr_err("%s: %s: missing mandatory property\n", __func__, np->name);
}
580

581 582 583
void __init orion_gpio_of_init(int irq_gpio_base)
{
	struct device_node *np;
584

585 586
	for_each_compatible_node(np, NULL, "marvell,orion-gpio")
		orion_gpio_of_init_one(np, irq_gpio_base);
587
}
588
#endif