pinctrl-coh901.c 20.5 KB
Newer Older
1
/*
Grant Likely's avatar
Grant Likely committed
2
 * U300 GPIO module.
3
 *
4
 * Copyright (C) 2007-2012 ST-Ericsson AB
5 6
 * License terms: GNU General Public License (GPL) version 2
 * COH 901 571/3 - Used in DB3210 (U365 2.0) and DB3350 (U335 1.0)
7
 * Author: Linus Walleij <linus.walleij@linaro.org>
8 9 10 11 12 13 14 15 16 17 18
 * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
 */
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/io.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
19
#include <linux/slab.h>
20
#include <linux/pinctrl/consumer.h>
21 22
#include <linux/pinctrl/pinconf-generic.h>
#include "pinctrl-coh901.h"
23

24
#define U300_GPIO_PORT_STRIDE				(0x30)
25
/*
26 27 28 29
 * Control Register 32bit (R/W)
 * bit 15-9 (mask 0x0000FE00) contains the number of cores. 8*cores
 * gives the number of GPIO pins.
 * bit 8-2  (mask 0x000001FC) contains the core version ID.
30
 */
31 32 33 34 35 36
#define U300_GPIO_CR					(0x00)
#define U300_GPIO_CR_SYNC_SEL_ENABLE			(0x00000002UL)
#define U300_GPIO_CR_BLOCK_CLKRQ_ENABLE			(0x00000001UL)
#define U300_GPIO_PXPDIR				(0x04)
#define U300_GPIO_PXPDOR				(0x08)
#define U300_GPIO_PXPCR					(0x0C)
37 38 39 40 41 42 43
#define U300_GPIO_PXPCR_ALL_PINS_MODE_MASK		(0x0000FFFFUL)
#define U300_GPIO_PXPCR_PIN_MODE_MASK			(0x00000003UL)
#define U300_GPIO_PXPCR_PIN_MODE_SHIFT			(0x00000002UL)
#define U300_GPIO_PXPCR_PIN_MODE_INPUT			(0x00000000UL)
#define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL	(0x00000001UL)
#define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN	(0x00000002UL)
#define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE	(0x00000003UL)
44 45 46 47 48 49 50
#define U300_GPIO_PXPER					(0x10)
#define U300_GPIO_PXPER_ALL_PULL_UP_DISABLE_MASK	(0x000000FFUL)
#define U300_GPIO_PXPER_PULL_UP_DISABLE			(0x00000001UL)
#define U300_GPIO_PXIEV					(0x14)
#define U300_GPIO_PXIEN					(0x18)
#define U300_GPIO_PXIFR					(0x1C)
#define U300_GPIO_PXICR					(0x20)
51 52 53 54 55 56
#define U300_GPIO_PXICR_ALL_IRQ_CONFIG_MASK		(0x000000FFUL)
#define U300_GPIO_PXICR_IRQ_CONFIG_MASK			(0x00000001UL)
#define U300_GPIO_PXICR_IRQ_CONFIG_FALLING_EDGE		(0x00000000UL)
#define U300_GPIO_PXICR_IRQ_CONFIG_RISING_EDGE		(0x00000001UL)

/* 8 bits per port, no version has more than 7 ports */
57
#define U300_GPIO_NUM_PORTS 7
58
#define U300_GPIO_PINS_PER_PORT 8
59
#define U300_GPIO_MAX (U300_GPIO_PINS_PER_PORT * U300_GPIO_NUM_PORTS)
60

61 62 63 64 65 66 67 68
struct u300_gpio_port {
	struct u300_gpio *gpio;
	char name[8];
	int irq;
	int number;
	u8 toggle_edge_mode;
};

69 70
struct u300_gpio {
	struct gpio_chip chip;
71
	struct u300_gpio_port ports[U300_GPIO_NUM_PORTS];
72 73 74 75 76 77 78 79 80 81 82 83 84
	struct clk *clk;
	void __iomem *base;
	struct device *dev;
	u32 stride;
	/* Register offsets */
	u32 pcr;
	u32 dor;
	u32 dir;
	u32 per;
	u32 icr;
	u32 ien;
	u32 iev;
};
85

86 87 88 89 90 91 92 93 94
/*
 * Macro to expand to read a specific register found in the "gpio"
 * struct. It requires the struct u300_gpio *gpio variable to exist in
 * its context. It calculates the port offset from the given pin
 * offset, muliplies by the port stride and adds the register offset
 * so it provides a pointer to the desired register.
 */
#define U300_PIN_REG(pin, reg) \
	(gpio->base + (pin >> 3) * gpio->stride + gpio->reg)
95

96 97 98 99 100 101
/*
 * Provides a bitmask for a specific gpio pin inside an 8-bit GPIO
 * register.
 */
#define U300_PIN_BIT(pin) \
	(1 << (pin & 0x07))
102

103 104 105 106
struct u300_gpio_confdata {
	u16 bias_mode;
	bool output;
	int outval;
107 108
};

109
#define U300_FLOATING_INPUT { \
110
	.bias_mode = PIN_CONFIG_BIAS_HIGH_IMPEDANCE, \
111 112
	.output = false, \
}
113

114
#define U300_PULL_UP_INPUT { \
115
	.bias_mode = PIN_CONFIG_BIAS_PULL_UP, \
116 117
	.output = false, \
}
118

119 120 121 122
#define U300_OUTPUT_LOW { \
	.output = true, \
	.outval = 0, \
}
123

124 125 126 127
#define U300_OUTPUT_HIGH { \
	.output = true, \
	.outval = 1, \
}
128 129

/* Initial configuration */
130
static const struct u300_gpio_confdata __initconst
131
bs335_gpio_config[U300_GPIO_NUM_PORTS][U300_GPIO_PINS_PER_PORT] = {
132 133
	/* Port 0, pins 0-7 */
	{
134 135 136 137 138 139 140 141
		U300_FLOATING_INPUT,
		U300_OUTPUT_HIGH,
		U300_FLOATING_INPUT,
		U300_OUTPUT_LOW,
		U300_OUTPUT_LOW,
		U300_OUTPUT_LOW,
		U300_OUTPUT_LOW,
		U300_OUTPUT_LOW,
142 143 144
	},
	/* Port 1, pins 0-7 */
	{
145 146 147 148 149 150 151 152
		U300_OUTPUT_LOW,
		U300_OUTPUT_LOW,
		U300_OUTPUT_LOW,
		U300_PULL_UP_INPUT,
		U300_FLOATING_INPUT,
		U300_OUTPUT_HIGH,
		U300_OUTPUT_LOW,
		U300_OUTPUT_LOW,
153 154 155
	},
	/* Port 2, pins 0-7 */
	{
156 157 158 159 160 161 162 163
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_OUTPUT_LOW,
		U300_PULL_UP_INPUT,
		U300_OUTPUT_LOW,
		U300_PULL_UP_INPUT,
164 165 166
	},
	/* Port 3, pins 0-7 */
	{
167 168 169 170 171 172 173 174
		U300_PULL_UP_INPUT,
		U300_OUTPUT_LOW,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
175 176 177
	},
	/* Port 4, pins 0-7 */
	{
178 179 180 181 182 183 184 185
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
186 187 188
	},
	/* Port 5, pins 0-7 */
	{
189 190 191 192 193 194 195 196
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
197 198 199
	},
	/* Port 6, pind 0-7 */
	{
200 201 202 203 204 205 206 207
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
		U300_FLOATING_INPUT,
208
	}
209
};
210

211
static int u300_gpio_get(struct gpio_chip *chip, unsigned offset)
212
{
213
	struct u300_gpio *gpio = gpiochip_get_data(chip);
214

215
	return !!(readl(U300_PIN_REG(offset, dir)) & U300_PIN_BIT(offset));
216 217
}

218
static void u300_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
219
{
220
	struct u300_gpio *gpio = gpiochip_get_data(chip);
221 222
	unsigned long flags;
	u32 val;
223

224
	local_irq_save(flags);
225

226 227 228 229 230
	val = readl(U300_PIN_REG(offset, dor));
	if (value)
		writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, dor));
	else
		writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, dor));
231

232
	local_irq_restore(flags);
233 234
}

235
static int u300_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
236
{
237
	struct u300_gpio *gpio = gpiochip_get_data(chip);
238 239
	unsigned long flags;
	u32 val;
240

241 242 243 244 245 246 247
	local_irq_save(flags);
	val = readl(U300_PIN_REG(offset, pcr));
	/* Mask out this pin, note 2 bits per setting */
	val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK << ((offset & 0x07) << 1));
	writel(val, U300_PIN_REG(offset, pcr));
	local_irq_restore(flags);
	return 0;
248 249
}

250 251
static int u300_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
				      int value)
252
{
253
	struct u300_gpio *gpio = gpiochip_get_data(chip);
254
	unsigned long flags;
255 256
	u32 oldmode;
	u32 val;
257 258

	local_irq_save(flags);
259 260 261 262 263 264 265 266 267 268 269 270 271 272
	val = readl(U300_PIN_REG(offset, pcr));
	/*
	 * Drive mode must be set by the special mode set function, set
	 * push/pull mode by default if no mode has been selected.
	 */
	oldmode = val & (U300_GPIO_PXPCR_PIN_MODE_MASK <<
			 ((offset & 0x07) << 1));
	/* mode = 0 means input, else some mode is already set */
	if (oldmode == 0) {
		val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK <<
			 ((offset & 0x07) << 1));
		val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL
			<< ((offset & 0x07) << 1));
		writel(val, U300_PIN_REG(offset, pcr));
273
	}
274
	u300_gpio_set(chip, offset, value);
275
	local_irq_restore(flags);
276
	return 0;
277 278
}

279 280 281 282 283
/* Returning -EINVAL means "supported but not available" */
int u300_gpio_config_get(struct gpio_chip *chip,
			 unsigned offset,
			 unsigned long *config)
{
284
	struct u300_gpio *gpio = gpiochip_get_data(chip);
285 286 287 288 289 290 291 292 293 294 295 296
	enum pin_config_param param = (enum pin_config_param) *config;
	bool biasmode;
	u32 drmode;

	/* One bit per pin, clamp to bool range */
	biasmode = !!(readl(U300_PIN_REG(offset, per)) & U300_PIN_BIT(offset));

	/* Mask out the two bits for this pin and shift to bits 0,1 */
	drmode = readl(U300_PIN_REG(offset, pcr));
	drmode &= (U300_GPIO_PXPCR_PIN_MODE_MASK << ((offset & 0x07) << 1));
	drmode >>= ((offset & 0x07) << 1);

297
	switch (param) {
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
		*config = 0;
		if (biasmode)
			return 0;
		else
			return -EINVAL;
		break;
	case PIN_CONFIG_BIAS_PULL_UP:
		*config = 0;
		if (!biasmode)
			return 0;
		else
			return -EINVAL;
		break;
	case PIN_CONFIG_DRIVE_PUSH_PULL:
		*config = 0;
		if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL)
			return 0;
		else
			return -EINVAL;
		break;
	case PIN_CONFIG_DRIVE_OPEN_DRAIN:
		*config = 0;
		if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN)
			return 0;
		else
			return -EINVAL;
		break;
	case PIN_CONFIG_DRIVE_OPEN_SOURCE:
		*config = 0;
		if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE)
			return 0;
		else
			return -EINVAL;
		break;
	default:
		break;
	}
	return -ENOTSUPP;
}

int u300_gpio_config_set(struct gpio_chip *chip, unsigned offset,
			 enum pin_config_param param)
341
{
342
	struct u300_gpio *gpio = gpiochip_get_data(chip);
343 344 345 346
	unsigned long flags;
	u32 val;

	local_irq_save(flags);
347
	switch (param) {
348 349
	case PIN_CONFIG_BIAS_DISABLE:
	case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
350 351 352
		val = readl(U300_PIN_REG(offset, per));
		writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, per));
		break;
353
	case PIN_CONFIG_BIAS_PULL_UP:
354 355 356
		val = readl(U300_PIN_REG(offset, per));
		writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, per));
		break;
357
	case PIN_CONFIG_DRIVE_PUSH_PULL:
358 359 360 361 362 363 364
		val = readl(U300_PIN_REG(offset, pcr));
		val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
			 << ((offset & 0x07) << 1));
		val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL
			<< ((offset & 0x07) << 1));
		writel(val, U300_PIN_REG(offset, pcr));
		break;
365
	case PIN_CONFIG_DRIVE_OPEN_DRAIN:
366 367 368 369 370 371 372
		val = readl(U300_PIN_REG(offset, pcr));
		val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
			 << ((offset & 0x07) << 1));
		val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN
			<< ((offset & 0x07) << 1));
		writel(val, U300_PIN_REG(offset, pcr));
		break;
373
	case PIN_CONFIG_DRIVE_OPEN_SOURCE:
374 375 376 377 378 379 380 381 382 383 384 385
		val = readl(U300_PIN_REG(offset, pcr));
		val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
			 << ((offset & 0x07) << 1));
		val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE
			<< ((offset & 0x07) << 1));
		writel(val, U300_PIN_REG(offset, pcr));
		break;
	default:
		local_irq_restore(flags);
		dev_err(gpio->dev, "illegal configuration requested\n");
		return -EINVAL;
	}
386 387 388 389
	local_irq_restore(flags);
	return 0;
}

390
static const struct gpio_chip u300_gpio_chip = {
391 392
	.label			= "u300-gpio-chip",
	.owner			= THIS_MODULE,
393 394
	.request		= gpiochip_generic_request,
	.free			= gpiochip_generic_free,
395 396 397 398 399 400 401
	.get			= u300_gpio_get,
	.set			= u300_gpio_set,
	.direction_input	= u300_gpio_direction_input,
	.direction_output	= u300_gpio_direction_output,
};

static void u300_toggle_trigger(struct u300_gpio *gpio, unsigned offset)
402 403 404
{
	u32 val;

405 406 407 408 409 410 411 412 413 414 415 416 417
	val = readl(U300_PIN_REG(offset, icr));
	/* Set mode depending on state */
	if (u300_gpio_get(&gpio->chip, offset)) {
		/* High now, let's trigger on falling edge next then */
		writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
		dev_dbg(gpio->dev, "next IRQ on falling edge on pin %d\n",
			offset);
	} else {
		/* Low now, let's trigger on rising edge next then */
		writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
		dev_dbg(gpio->dev, "next IRQ on rising edge on pin %d\n",
			offset);
	}
418 419
}

420
static int u300_gpio_irq_type(struct irq_data *d, unsigned trigger)
421
{
422
	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
423
	struct u300_gpio *gpio = gpiochip_get_data(chip);
424 425
	struct u300_gpio_port *port = &gpio->ports[d->hwirq >> 3];
	int offset = d->hwirq;
426 427
	u32 val;

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
	if ((trigger & IRQF_TRIGGER_RISING) &&
	    (trigger & IRQF_TRIGGER_FALLING)) {
		/*
		 * The GPIO block can only trigger on falling OR rising edges,
		 * not both. So we need to toggle the mode whenever the pin
		 * goes from one state to the other with a special state flag
		 */
		dev_dbg(gpio->dev,
			"trigger on both rising and falling edge on pin %d\n",
			offset);
		port->toggle_edge_mode |= U300_PIN_BIT(offset);
		u300_toggle_trigger(gpio, offset);
	} else if (trigger & IRQF_TRIGGER_RISING) {
		dev_dbg(gpio->dev, "trigger on rising edge on pin %d\n",
			offset);
		val = readl(U300_PIN_REG(offset, icr));
		writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
		port->toggle_edge_mode &= ~U300_PIN_BIT(offset);
	} else if (trigger & IRQF_TRIGGER_FALLING) {
		dev_dbg(gpio->dev, "trigger on falling edge on pin %d\n",
			offset);
		val = readl(U300_PIN_REG(offset, icr));
		writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
		port->toggle_edge_mode &= ~U300_PIN_BIT(offset);
	}

	return 0;
455 456
}

457
static void u300_gpio_irq_enable(struct irq_data *d)
458
{
459
	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
460
	struct u300_gpio *gpio = gpiochip_get_data(chip);
461 462
	struct u300_gpio_port *port = &gpio->ports[d->hwirq >> 3];
	int offset = d->hwirq;
463 464 465
	u32 val;
	unsigned long flags;

466 467
	dev_dbg(gpio->dev, "enable IRQ for hwirq %lu on port %s, offset %d\n",
		 d->hwirq, port->name, offset);
468
	local_irq_save(flags);
469 470
	val = readl(U300_PIN_REG(offset, ien));
	writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, ien));
471 472 473
	local_irq_restore(flags);
}

474
static void u300_gpio_irq_disable(struct irq_data *d)
475
{
476
	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
477
	struct u300_gpio *gpio = gpiochip_get_data(chip);
478
	int offset = d->hwirq;
479 480 481 482
	u32 val;
	unsigned long flags;

	local_irq_save(flags);
483 484
	val = readl(U300_PIN_REG(offset, ien));
	writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, ien));
485
	local_irq_restore(flags);
486 487
}

488 489 490 491 492 493 494
static struct irq_chip u300_gpio_irqchip = {
	.name			= "u300-gpio-irqchip",
	.irq_enable		= u300_gpio_irq_enable,
	.irq_disable		= u300_gpio_irq_disable,
	.irq_set_type		= u300_gpio_irq_type,
};

495
static void u300_gpio_irq_handler(struct irq_desc *desc)
496
{
497
	unsigned int irq = irq_desc_get_irq(desc);
498 499
	struct irq_chip *parent_chip = irq_desc_get_chip(desc);
	struct gpio_chip *chip = irq_desc_get_handler_data(desc);
500
	struct u300_gpio *gpio = gpiochip_get_data(chip);
501
	struct u300_gpio_port *port = &gpio->ports[irq - chip->base];
502 503
	int pinoffset = port->number << 3; /* get the right stride */
	unsigned long val;
504

505 506
	chained_irq_enter(parent_chip, desc);

507
	/* Read event register */
508
	val = readl(U300_PIN_REG(pinoffset, iev));
509
	/* Mask relevant bits */
510
	val &= 0xFFU; /* 8 bits per port */
511
	/* ACK IRQ (clear event) */
512 513 514 515 516 517 518 519
	writel(val, U300_PIN_REG(pinoffset, iev));

	/* Call IRQ handler */
	if (val != 0) {
		int irqoffset;

		for_each_set_bit(irqoffset, &val, U300_GPIO_PINS_PER_PORT) {
			int offset = pinoffset + irqoffset;
520
			int pin_irq = irq_find_mapping(chip->irq.domain, offset);
521 522 523 524 525 526 527 528 529 530 531

			dev_dbg(gpio->dev, "GPIO IRQ %d on pin %d\n",
				pin_irq, offset);
			generic_handle_irq(pin_irq);
			/*
			 * Triggering IRQ on both rising and falling edge
			 * needs mockery
			 */
			if (port->toggle_edge_mode & U300_PIN_BIT(offset))
				u300_toggle_trigger(gpio, offset);
		}
532
	}
533

534
	chained_irq_exit(parent_chip, desc);
535 536
}

537 538 539
static void __init u300_gpio_init_pin(struct u300_gpio *gpio,
				      int offset,
				      const struct u300_gpio_confdata *conf)
540
{
541 542 543
	/* Set mode: input or output */
	if (conf->output) {
		u300_gpio_direction_output(&gpio->chip, offset, conf->outval);
544

545
		/* Deactivate bias mode for output */
546 547
		u300_gpio_config_set(&gpio->chip, offset,
				     PIN_CONFIG_BIAS_HIGH_IMPEDANCE);
548 549

		/* Set drive mode for output */
550 551
		u300_gpio_config_set(&gpio->chip, offset,
				     PIN_CONFIG_DRIVE_PUSH_PULL);
552 553 554 555 556 557 558 559 560 561

		dev_dbg(gpio->dev, "set up pin %d as output, value: %d\n",
			offset, conf->outval);
	} else {
		u300_gpio_direction_input(&gpio->chip, offset);

		/* Always set output low on input pins */
		u300_gpio_set(&gpio->chip, offset, 0);

		/* Set bias mode for input */
562
		u300_gpio_config_set(&gpio->chip, offset, conf->bias_mode);
563 564 565

		dev_dbg(gpio->dev, "set up pin %d as input, bias: %04x\n",
			offset, conf->bias_mode);
566
	}
567
}
568

569
static void __init u300_gpio_init_coh901571(struct u300_gpio *gpio)
570 571 572 573
{
	int i, j;

	/* Write default config and values to all pins */
574
	for (i = 0; i < U300_GPIO_NUM_PORTS; i++) {
575 576 577 578
		for (j = 0; j < 8; j++) {
			const struct u300_gpio_confdata *conf;
			int offset = (i*8) + j;

579
			conf = &bs335_gpio_config[i][j];
580
			u300_gpio_init_pin(gpio, offset, conf);
581 582
		}
	}
583
}
584

585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
/*
 * Here we map a GPIO in the local gpio_chip pin space to a pin in
 * the local pinctrl pin space. The pin controller used is
 * pinctrl-u300.
 */
struct coh901_pinpair {
	unsigned int offset;
	unsigned int pin_base;
};

#define COH901_PINRANGE(a, b) { .offset = a, .pin_base = b }

static struct coh901_pinpair coh901_pintable[] = {
	COH901_PINRANGE(10, 426),
	COH901_PINRANGE(11, 180),
	COH901_PINRANGE(12, 165), /* MS/MMC card insertion */
	COH901_PINRANGE(13, 179),
	COH901_PINRANGE(14, 178),
	COH901_PINRANGE(16, 194),
	COH901_PINRANGE(17, 193),
	COH901_PINRANGE(18, 192),
	COH901_PINRANGE(19, 191),
	COH901_PINRANGE(20, 186),
	COH901_PINRANGE(21, 185),
	COH901_PINRANGE(22, 184),
	COH901_PINRANGE(23, 183),
	COH901_PINRANGE(24, 182),
	COH901_PINRANGE(25, 181),
};

615
static int __init u300_gpio_probe(struct platform_device *pdev)
616
{
617
	struct u300_gpio *gpio;
618
	struct resource *memres;
619
	int err = 0;
620 621 622
	int portno;
	u32 val;
	u32 ifr;
623 624
	int i;

625 626
	gpio = devm_kzalloc(&pdev->dev, sizeof(struct u300_gpio), GFP_KERNEL);
	if (gpio == NULL)
627 628 629
		return -ENOMEM;

	gpio->chip = u300_gpio_chip;
630
	gpio->chip.ngpio = U300_GPIO_NUM_PORTS * U300_GPIO_PINS_PER_PORT;
631
	gpio->chip.parent = &pdev->dev;
632
	gpio->chip.base = 0;
633
	gpio->dev = &pdev->dev;
634

635
	memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
636 637 638
	gpio->base = devm_ioremap_resource(&pdev->dev, memres);
	if (IS_ERR(gpio->base))
		return PTR_ERR(gpio->base);
639 640

	gpio->clk = devm_clk_get(gpio->dev, NULL);
641 642 643
	if (IS_ERR(gpio->clk)) {
		err = PTR_ERR(gpio->clk);
		dev_err(gpio->dev, "could not get GPIO clock\n");
644
		return err;
645
	}
646

647
	err = clk_prepare_enable(gpio->clk);
648
	if (err) {
649
		dev_err(gpio->dev, "could not enable GPIO clock\n");
650
		return err;
651
	}
652

653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
	dev_info(gpio->dev,
		 "initializing GPIO Controller COH 901 571/3\n");
	gpio->stride = U300_GPIO_PORT_STRIDE;
	gpio->pcr = U300_GPIO_PXPCR;
	gpio->dor = U300_GPIO_PXPDOR;
	gpio->dir = U300_GPIO_PXPDIR;
	gpio->per = U300_GPIO_PXPER;
	gpio->icr = U300_GPIO_PXICR;
	gpio->ien = U300_GPIO_PXIEN;
	gpio->iev = U300_GPIO_PXIEV;
	ifr = U300_GPIO_PXIFR;

	val = readl(gpio->base + U300_GPIO_CR);
	dev_info(gpio->dev, "COH901571/3 block version: %d, " \
		 "number of cores: %d totalling %d pins\n",
		 ((val & 0x000001FC) >> 2),
		 ((val & 0x0000FE00) >> 9),
		 ((val & 0x0000FE00) >> 9) * 8);
	writel(U300_GPIO_CR_BLOCK_CLKRQ_ENABLE,
	       gpio->base + U300_GPIO_CR);
673
	u300_gpio_init_coh901571(gpio);
674

675 676 677
#ifdef CONFIG_OF_GPIO
	gpio->chip.of_node = pdev->dev.of_node;
#endif
678
	err = gpiochip_add_data(&gpio->chip, gpio);
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
	if (err) {
		dev_err(gpio->dev, "unable to add gpiochip: %d\n", err);
		goto err_no_chip;
	}

	err = gpiochip_irqchip_add(&gpio->chip,
				   &u300_gpio_irqchip,
				   0,
				   handle_simple_irq,
				   IRQ_TYPE_EDGE_FALLING);
	if (err) {
		dev_err(gpio->dev, "no GPIO irqchip\n");
		goto err_no_irqchip;
	}

694
	/* Add each port with its IRQ separately */
695
	for (portno = 0 ; portno < U300_GPIO_NUM_PORTS; portno++) {
696
		struct u300_gpio_port *port = &gpio->ports[portno];
697 698 699 700 701

		snprintf(port->name, 8, "gpio%d", portno);
		port->number = portno;
		port->gpio = gpio;

702
		port->irq = platform_get_irq(pdev, portno);
703

704 705 706 707
		gpiochip_set_chained_irqchip(&gpio->chip,
					     &u300_gpio_irqchip,
					     port->irq,
					     u300_gpio_irq_handler);
708 709 710

		/* Turns off irq force (test register) for this port */
		writel(0x0, gpio->base + portno * gpio->stride + ifr);
711
	}
712 713
	dev_dbg(gpio->dev, "initialized %d GPIO ports\n", portno);

714 715 716 717 718 719 720 721 722 723 724 725 726
	/*
	 * Add pinctrl pin ranges, the pin controller must be registered
	 * at this point
	 */
	for (i = 0; i < ARRAY_SIZE(coh901_pintable); i++) {
		struct coh901_pinpair *p = &coh901_pintable[i];

		err = gpiochip_add_pin_range(&gpio->chip, "pinctrl-u300",
					     p->offset, p->pin_base, 1);
		if (err)
			goto err_no_range;
	}

727
	platform_set_drvdata(pdev, gpio);
728 729 730

	return 0;

731
err_no_range:
732
err_no_irqchip:
733
	gpiochip_remove(&gpio->chip);
734
err_no_chip:
735
	clk_disable_unprepare(gpio->clk);
736
	dev_err(&pdev->dev, "module ERROR:%d\n", err);
737 738 739
	return err;
}

740
static int __exit u300_gpio_remove(struct platform_device *pdev)
741
{
742
	struct u300_gpio *gpio = platform_get_drvdata(pdev);
743 744

	/* Turn off the GPIO block */
745
	writel(0x00000000U, gpio->base + U300_GPIO_CR);
746

747
	gpiochip_remove(&gpio->chip);
748
	clk_disable_unprepare(gpio->clk);
749 750 751
	return 0;
}

752 753 754 755 756
static const struct of_device_id u300_gpio_match[] = {
	{ .compatible = "stericsson,gpio-coh901" },
	{},
};

757
static struct platform_driver u300_gpio_driver = {
758 759
	.driver		= {
		.name	= "u300-gpio",
760
		.of_match_table = u300_gpio_match,
761
	},
762
	.remove		= __exit_p(u300_gpio_remove),
763 764 765 766
};

static int __init u300_gpio_init(void)
{
767
	return platform_driver_probe(&u300_gpio_driver, u300_gpio_probe);
768 769 770 771
}

static void __exit u300_gpio_exit(void)
{
772
	platform_driver_unregister(&u300_gpio_driver);
773 774 775 776 777 778
}

arch_initcall(u300_gpio_init);
module_exit(u300_gpio_exit);

MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
779
MODULE_DESCRIPTION("ST-Ericsson AB COH 901 335/COH 901 571/3 GPIO driver");
780
MODULE_LICENSE("GPL");