pinctrl-iproc-gpio.c 21.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright (C) 2014-2015 Broadcom Corporation
 *
 * 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 version 2.
 *
 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
 * kind, whether express or implied; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
13 14
 * This file contains the Broadcom Iproc GPIO driver that supports 3
 * GPIO controllers on Iproc including the ASIU GPIO controller, the
15 16 17 18
 * chipCommonG GPIO controller, and the always-on GPIO controller. Basic
 * PINCONF such as bias pull up/down, and drive strength are also supported
 * in this driver.
 *
19 20 21 22
 * It provides the functionality where pins from the GPIO can be
 * individually muxed to GPIO function, if individual pad
 * configuration is supported, through the interaction with respective
 * SoCs IOMUX controller.
23 24 25 26 27 28
 */

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/io.h>
29
#include <linux/gpio/driver.h>
30 31 32 33 34 35 36 37 38
#include <linux/ioport.h>
#include <linux/of_device.h>
#include <linux/of_irq.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/pinctrl/pinconf.h>
#include <linux/pinctrl/pinconf-generic.h>

#include "../pinctrl-utils.h"

39 40 41 42 43 44 45 46 47 48 49 50
#define IPROC_GPIO_DATA_IN_OFFSET   0x00
#define IPROC_GPIO_DATA_OUT_OFFSET  0x04
#define IPROC_GPIO_OUT_EN_OFFSET    0x08
#define IPROC_GPIO_INT_TYPE_OFFSET  0x0c
#define IPROC_GPIO_INT_DE_OFFSET    0x10
#define IPROC_GPIO_INT_EDGE_OFFSET  0x14
#define IPROC_GPIO_INT_MSK_OFFSET   0x18
#define IPROC_GPIO_INT_STAT_OFFSET  0x1c
#define IPROC_GPIO_INT_MSTAT_OFFSET 0x20
#define IPROC_GPIO_INT_CLR_OFFSET   0x24
#define IPROC_GPIO_PAD_RES_OFFSET   0x34
#define IPROC_GPIO_RES_EN_OFFSET    0x38
51 52

/* drive strength control for ASIU GPIO */
53
#define IPROC_GPIO_ASIU_DRV0_CTRL_OFFSET 0x58
54 55

/* drive strength control for CCM/CRMU (AON) GPIO */
56
#define IPROC_GPIO_DRV0_CTRL_OFFSET  0x00
57 58 59 60 61

#define GPIO_BANK_SIZE 0x200
#define NGPIOS_PER_BANK 32
#define GPIO_BANK(pin) ((pin) / NGPIOS_PER_BANK)

62 63
#define IPROC_GPIO_REG(pin, reg) (GPIO_BANK(pin) * GPIO_BANK_SIZE + (reg))
#define IPROC_GPIO_SHIFT(pin) ((pin) % NGPIOS_PER_BANK)
64 65 66 67 68

#define GPIO_DRV_STRENGTH_BIT_SHIFT  20
#define GPIO_DRV_STRENGTH_BITS       3
#define GPIO_DRV_STRENGTH_BIT_MASK   ((1 << GPIO_DRV_STRENGTH_BITS) - 1)

69 70 71 72 73 74 75 76
enum iproc_pinconf_param {
	IPROC_PINCONF_DRIVE_STRENGTH = 0,
	IPROC_PINCONF_BIAS_DISABLE,
	IPROC_PINCONF_BIAS_PULL_UP,
	IPROC_PINCONF_BIAS_PULL_DOWN,
	IPROC_PINCON_MAX,
};

77
/*
78
 * Iproc GPIO core
79 80
 *
 * @dev: pointer to device
81 82
 * @base: I/O register base for Iproc GPIO controller
 * @io_ctrl: I/O register base for certain type of Iproc GPIO controller that
83 84 85 86 87 88
 * has the PINCONF support implemented outside of the GPIO block
 * @lock: lock to protect access to I/O registers
 * @gc: GPIO chip
 * @num_banks: number of GPIO banks, each bank supports up to 32 GPIOs
 * @pinmux_is_supported: flag to indicate this GPIO controller contains pins
 * that can be individually muxed to GPIO
89 90 91 92
 * @pinconf_disable: contains a list of PINCONF parameters that need to be
 * disabled
 * @nr_pinconf_disable: total number of PINCONF parameters that need to be
 * disabled
93 94 95
 * @pctl: pointer to pinctrl_dev
 * @pctldesc: pinctrl descriptor
 */
96
struct iproc_gpio {
97 98 99 100 101 102 103 104 105 106 107 108
	struct device *dev;

	void __iomem *base;
	void __iomem *io_ctrl;

	spinlock_t lock;

	struct gpio_chip gc;
	unsigned num_banks;

	bool pinmux_is_supported;

109 110 111
	enum pin_config_param *pinconf_disable;
	unsigned int nr_pinconf_disable;

112 113 114 115 116 117 118
	struct pinctrl_dev *pctl;
	struct pinctrl_desc pctldesc;
};

/*
 * Mapping from PINCONF pins to GPIO pins is 1-to-1
 */
119
static inline unsigned iproc_pin_to_gpio(unsigned pin)
120 121 122 123 124
{
	return pin;
}

/**
125 126
 *  iproc_set_bit - set or clear one bit (corresponding to the GPIO pin) in a
 *  Iproc GPIO register
127
 *
128
 *  @iproc_gpio: Iproc GPIO device
129 130 131 132
 *  @reg: register offset
 *  @gpio: GPIO pin
 *  @set: set or clear
 */
133
static inline void iproc_set_bit(struct iproc_gpio *chip, unsigned int reg,
134 135
				  unsigned gpio, bool set)
{
136 137
	unsigned int offset = IPROC_GPIO_REG(gpio, reg);
	unsigned int shift = IPROC_GPIO_SHIFT(gpio);
138 139 140 141 142 143 144 145 146 147
	u32 val;

	val = readl(chip->base + offset);
	if (set)
		val |= BIT(shift);
	else
		val &= ~BIT(shift);
	writel(val, chip->base + offset);
}

148
static inline bool iproc_get_bit(struct iproc_gpio *chip, unsigned int reg,
149 150
				  unsigned gpio)
{
151 152
	unsigned int offset = IPROC_GPIO_REG(gpio, reg);
	unsigned int shift = IPROC_GPIO_SHIFT(gpio);
153 154 155 156

	return !!(readl(chip->base + offset) & BIT(shift));
}

157
static void iproc_gpio_irq_handler(struct irq_desc *desc)
158 159
{
	struct gpio_chip *gc = irq_desc_get_handler_data(desc);
160
	struct iproc_gpio *chip = gpiochip_get_data(gc);
161 162 163 164 165 166 167 168
	struct irq_chip *irq_chip = irq_desc_get_chip(desc);
	int i, bit;

	chained_irq_enter(irq_chip, desc);

	/* go through the entire GPIO banks and handle all interrupts */
	for (i = 0; i < chip->num_banks; i++) {
		unsigned long val = readl(chip->base + (i * GPIO_BANK_SIZE) +
169
					  IPROC_GPIO_INT_MSTAT_OFFSET);
170 171 172 173 174 175 176 177 178 179

		for_each_set_bit(bit, &val, NGPIOS_PER_BANK) {
			unsigned pin = NGPIOS_PER_BANK * i + bit;
			int child_irq = irq_find_mapping(gc->irqdomain, pin);

			/*
			 * Clear the interrupt before invoking the
			 * handler, so we do not leave any window
			 */
			writel(BIT(bit), chip->base + (i * GPIO_BANK_SIZE) +
180
			       IPROC_GPIO_INT_CLR_OFFSET);
181 182 183 184 185 186 187 188 189

			generic_handle_irq(child_irq);
		}
	}

	chained_irq_exit(irq_chip, desc);
}


190
static void iproc_gpio_irq_ack(struct irq_data *d)
191 192
{
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
193
	struct iproc_gpio *chip = gpiochip_get_data(gc);
194
	unsigned gpio = d->hwirq;
195 196 197
	unsigned int offset = IPROC_GPIO_REG(gpio,
			IPROC_GPIO_INT_CLR_OFFSET);
	unsigned int shift = IPROC_GPIO_SHIFT(gpio);
198 199 200 201 202 203
	u32 val = BIT(shift);

	writel(val, chip->base + offset);
}

/**
204
 *  iproc_gpio_irq_set_mask - mask/unmask a GPIO interrupt
205 206 207 208
 *
 *  @d: IRQ chip data
 *  @unmask: mask/unmask GPIO interrupt
 */
209
static void iproc_gpio_irq_set_mask(struct irq_data *d, bool unmask)
210 211
{
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
212
	struct iproc_gpio *chip = gpiochip_get_data(gc);
213 214
	unsigned gpio = d->hwirq;

215
	iproc_set_bit(chip, IPROC_GPIO_INT_MSK_OFFSET, gpio, unmask);
216 217
}

218
static void iproc_gpio_irq_mask(struct irq_data *d)
219 220
{
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
221
	struct iproc_gpio *chip = gpiochip_get_data(gc);
222 223 224
	unsigned long flags;

	spin_lock_irqsave(&chip->lock, flags);
225
	iproc_gpio_irq_set_mask(d, false);
226 227 228
	spin_unlock_irqrestore(&chip->lock, flags);
}

229
static void iproc_gpio_irq_unmask(struct irq_data *d)
230 231
{
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
232
	struct iproc_gpio *chip = gpiochip_get_data(gc);
233 234 235
	unsigned long flags;

	spin_lock_irqsave(&chip->lock, flags);
236
	iproc_gpio_irq_set_mask(d, true);
237 238 239
	spin_unlock_irqrestore(&chip->lock, flags);
}

240
static int iproc_gpio_irq_set_type(struct irq_data *d, unsigned int type)
241 242
{
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
243
	struct iproc_gpio *chip = gpiochip_get_data(gc);
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
	unsigned gpio = d->hwirq;
	bool level_triggered = false;
	bool dual_edge = false;
	bool rising_or_high = false;
	unsigned long flags;

	switch (type & IRQ_TYPE_SENSE_MASK) {
	case IRQ_TYPE_EDGE_RISING:
		rising_or_high = true;
		break;

	case IRQ_TYPE_EDGE_FALLING:
		break;

	case IRQ_TYPE_EDGE_BOTH:
		dual_edge = true;
		break;

	case IRQ_TYPE_LEVEL_HIGH:
		level_triggered = true;
		rising_or_high = true;
		break;

	case IRQ_TYPE_LEVEL_LOW:
		level_triggered = true;
		break;

	default:
		dev_err(chip->dev, "invalid GPIO IRQ type 0x%x\n",
			type);
		return -EINVAL;
	}

	spin_lock_irqsave(&chip->lock, flags);
278
	iproc_set_bit(chip, IPROC_GPIO_INT_TYPE_OFFSET, gpio,
279
		       level_triggered);
280 281
	iproc_set_bit(chip, IPROC_GPIO_INT_DE_OFFSET, gpio, dual_edge);
	iproc_set_bit(chip, IPROC_GPIO_INT_EDGE_OFFSET, gpio,
282 283 284 285 286 287 288 289 290 291
		       rising_or_high);
	spin_unlock_irqrestore(&chip->lock, flags);

	dev_dbg(chip->dev,
		"gpio:%u level_triggered:%d dual_edge:%d rising_or_high:%d\n",
		gpio, level_triggered, dual_edge, rising_or_high);

	return 0;
}

292 293 294 295 296 297
static struct irq_chip iproc_gpio_irq_chip = {
	.name = "bcm-iproc-gpio",
	.irq_ack = iproc_gpio_irq_ack,
	.irq_mask = iproc_gpio_irq_mask,
	.irq_unmask = iproc_gpio_irq_unmask,
	.irq_set_type = iproc_gpio_irq_set_type,
298 299 300
};

/*
301
 * Request the Iproc IOMUX pinmux controller to mux individual pins to GPIO
302
 */
303
static int iproc_gpio_request(struct gpio_chip *gc, unsigned offset)
304
{
305
	struct iproc_gpio *chip = gpiochip_get_data(gc);
306 307
	unsigned gpio = gc->base + offset;

308
	/* not all Iproc GPIO pins can be muxed individually */
309 310 311 312 313 314
	if (!chip->pinmux_is_supported)
		return 0;

	return pinctrl_request_gpio(gpio);
}

315
static void iproc_gpio_free(struct gpio_chip *gc, unsigned offset)
316
{
317
	struct iproc_gpio *chip = gpiochip_get_data(gc);
318 319 320 321 322 323 324 325
	unsigned gpio = gc->base + offset;

	if (!chip->pinmux_is_supported)
		return;

	pinctrl_free_gpio(gpio);
}

326
static int iproc_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
327
{
328
	struct iproc_gpio *chip = gpiochip_get_data(gc);
329 330 331
	unsigned long flags;

	spin_lock_irqsave(&chip->lock, flags);
332
	iproc_set_bit(chip, IPROC_GPIO_OUT_EN_OFFSET, gpio, false);
333 334 335 336 337 338 339
	spin_unlock_irqrestore(&chip->lock, flags);

	dev_dbg(chip->dev, "gpio:%u set input\n", gpio);

	return 0;
}

340
static int iproc_gpio_direction_output(struct gpio_chip *gc, unsigned gpio,
341 342
					int val)
{
343
	struct iproc_gpio *chip = gpiochip_get_data(gc);
344 345 346
	unsigned long flags;

	spin_lock_irqsave(&chip->lock, flags);
347 348
	iproc_set_bit(chip, IPROC_GPIO_OUT_EN_OFFSET, gpio, true);
	iproc_set_bit(chip, IPROC_GPIO_DATA_OUT_OFFSET, gpio, !!(val));
349 350 351 352 353 354 355
	spin_unlock_irqrestore(&chip->lock, flags);

	dev_dbg(chip->dev, "gpio:%u set output, value:%d\n", gpio, val);

	return 0;
}

356
static void iproc_gpio_set(struct gpio_chip *gc, unsigned gpio, int val)
357
{
358
	struct iproc_gpio *chip = gpiochip_get_data(gc);
359 360 361
	unsigned long flags;

	spin_lock_irqsave(&chip->lock, flags);
362
	iproc_set_bit(chip, IPROC_GPIO_DATA_OUT_OFFSET, gpio, !!(val));
363 364 365 366 367
	spin_unlock_irqrestore(&chip->lock, flags);

	dev_dbg(chip->dev, "gpio:%u set, value:%d\n", gpio, val);
}

368
static int iproc_gpio_get(struct gpio_chip *gc, unsigned gpio)
369
{
370
	struct iproc_gpio *chip = gpiochip_get_data(gc);
371 372 373
	unsigned int offset = IPROC_GPIO_REG(gpio,
					      IPROC_GPIO_DATA_IN_OFFSET);
	unsigned int shift = IPROC_GPIO_SHIFT(gpio);
374 375 376 377

	return !!(readl(chip->base + offset) & BIT(shift));
}

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 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
/*
 * Mapping of the iProc PINCONF parameters to the generic pin configuration
 * parameters
 */
static const enum pin_config_param iproc_pinconf_disable_map[] = {
	[IPROC_PINCONF_DRIVE_STRENGTH] = PIN_CONFIG_DRIVE_STRENGTH,
	[IPROC_PINCONF_BIAS_DISABLE] = PIN_CONFIG_BIAS_DISABLE,
	[IPROC_PINCONF_BIAS_PULL_UP] = PIN_CONFIG_BIAS_PULL_UP,
	[IPROC_PINCONF_BIAS_PULL_DOWN] = PIN_CONFIG_BIAS_PULL_DOWN,
};

static bool iproc_pinconf_param_is_disabled(struct iproc_gpio *chip,
					    enum pin_config_param param)
{
	unsigned int i;

	if (!chip->nr_pinconf_disable)
		return false;

	for (i = 0; i < chip->nr_pinconf_disable; i++)
		if (chip->pinconf_disable[i] == param)
			return true;

	return false;
}

static int iproc_pinconf_disable_map_create(struct iproc_gpio *chip,
					    unsigned long disable_mask)
{
	unsigned int map_size = ARRAY_SIZE(iproc_pinconf_disable_map);
	unsigned int bit, nbits = 0;

	/* figure out total number of PINCONF parameters to disable */
	for_each_set_bit(bit, &disable_mask, map_size)
		nbits++;

	if (!nbits)
		return 0;

	/*
	 * Allocate an array to store PINCONF parameters that need to be
	 * disabled
	 */
	chip->pinconf_disable = devm_kcalloc(chip->dev, nbits,
					     sizeof(*chip->pinconf_disable),
					     GFP_KERNEL);
	if (!chip->pinconf_disable)
		return -ENOMEM;

	chip->nr_pinconf_disable = nbits;

	/* now store these parameters */
	nbits = 0;
	for_each_set_bit(bit, &disable_mask, map_size)
		chip->pinconf_disable[nbits++] = iproc_pinconf_disable_map[bit];

	return 0;
}

437
static int iproc_get_groups_count(struct pinctrl_dev *pctldev)
438 439 440 441 442 443 444 445
{
	return 1;
}

/*
 * Only one group: "gpio_grp", since this local pinctrl device only performs
 * GPIO specific PINCONF configurations
 */
446
static const char *iproc_get_group_name(struct pinctrl_dev *pctldev,
447 448 449 450 451
					 unsigned selector)
{
	return "gpio_grp";
}

452 453 454
static const struct pinctrl_ops iproc_pctrl_ops = {
	.get_groups_count = iproc_get_groups_count,
	.get_group_name = iproc_get_group_name,
455
	.dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
456
	.dt_free_map = pinctrl_utils_free_map,
457 458
};

459
static int iproc_gpio_set_pull(struct iproc_gpio *chip, unsigned gpio,
460 461 462 463 464 465 466
				bool disable, bool pull_up)
{
	unsigned long flags;

	spin_lock_irqsave(&chip->lock, flags);

	if (disable) {
467
		iproc_set_bit(chip, IPROC_GPIO_RES_EN_OFFSET, gpio, false);
468
	} else {
469
		iproc_set_bit(chip, IPROC_GPIO_PAD_RES_OFFSET, gpio,
470
			       pull_up);
471
		iproc_set_bit(chip, IPROC_GPIO_RES_EN_OFFSET, gpio, true);
472 473 474 475 476 477 478 479 480
	}

	spin_unlock_irqrestore(&chip->lock, flags);

	dev_dbg(chip->dev, "gpio:%u set pullup:%d\n", gpio, pull_up);

	return 0;
}

481
static void iproc_gpio_get_pull(struct iproc_gpio *chip, unsigned gpio,
482 483 484 485 486
				 bool *disable, bool *pull_up)
{
	unsigned long flags;

	spin_lock_irqsave(&chip->lock, flags);
487 488
	*disable = !iproc_get_bit(chip, IPROC_GPIO_RES_EN_OFFSET, gpio);
	*pull_up = iproc_get_bit(chip, IPROC_GPIO_PAD_RES_OFFSET, gpio);
489 490 491
	spin_unlock_irqrestore(&chip->lock, flags);
}

492
static int iproc_gpio_set_strength(struct iproc_gpio *chip, unsigned gpio,
493 494 495 496 497 498 499 500 501 502 503 504 505
				    unsigned strength)
{
	void __iomem *base;
	unsigned int i, offset, shift;
	u32 val;
	unsigned long flags;

	/* make sure drive strength is supported */
	if (strength < 2 ||  strength > 16 || (strength % 2))
		return -ENOTSUPP;

	if (chip->io_ctrl) {
		base = chip->io_ctrl;
506
		offset = IPROC_GPIO_DRV0_CTRL_OFFSET;
507 508
	} else {
		base = chip->base;
509 510
		offset = IPROC_GPIO_REG(gpio,
					 IPROC_GPIO_ASIU_DRV0_CTRL_OFFSET);
511 512
	}

513
	shift = IPROC_GPIO_SHIFT(gpio);
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

	dev_dbg(chip->dev, "gpio:%u set drive strength:%d mA\n", gpio,
		strength);

	spin_lock_irqsave(&chip->lock, flags);
	strength = (strength / 2) - 1;
	for (i = 0; i < GPIO_DRV_STRENGTH_BITS; i++) {
		val = readl(base + offset);
		val &= ~BIT(shift);
		val |= ((strength >> i) & 0x1) << shift;
		writel(val, base + offset);
		offset += 4;
	}
	spin_unlock_irqrestore(&chip->lock, flags);

	return 0;
}

532
static int iproc_gpio_get_strength(struct iproc_gpio *chip, unsigned gpio,
533 534 535 536 537 538 539 540 541
				    u16 *strength)
{
	void __iomem *base;
	unsigned int i, offset, shift;
	u32 val;
	unsigned long flags;

	if (chip->io_ctrl) {
		base = chip->io_ctrl;
542
		offset = IPROC_GPIO_DRV0_CTRL_OFFSET;
543 544
	} else {
		base = chip->base;
545 546
		offset = IPROC_GPIO_REG(gpio,
					 IPROC_GPIO_ASIU_DRV0_CTRL_OFFSET);
547 548
	}

549
	shift = IPROC_GPIO_SHIFT(gpio);
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566

	spin_lock_irqsave(&chip->lock, flags);
	*strength = 0;
	for (i = 0; i < GPIO_DRV_STRENGTH_BITS; i++) {
		val = readl(base + offset) & BIT(shift);
		val >>= shift;
		*strength += (val << i);
		offset += 4;
	}

	/* convert to mA */
	*strength = (*strength + 1) * 2;
	spin_unlock_irqrestore(&chip->lock, flags);

	return 0;
}

567
static int iproc_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
568 569
				 unsigned long *config)
{
570
	struct iproc_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
571
	enum pin_config_param param = pinconf_to_config_param(*config);
572
	unsigned gpio = iproc_pin_to_gpio(pin);
573 574 575 576
	u16 arg;
	bool disable, pull_up;
	int ret;

577 578 579
	if (iproc_pinconf_param_is_disabled(chip, param))
		return -ENOTSUPP;

580 581
	switch (param) {
	case PIN_CONFIG_BIAS_DISABLE:
582
		iproc_gpio_get_pull(chip, gpio, &disable, &pull_up);
583 584 585 586 587 588
		if (disable)
			return 0;
		else
			return -EINVAL;

	case PIN_CONFIG_BIAS_PULL_UP:
589
		iproc_gpio_get_pull(chip, gpio, &disable, &pull_up);
590 591 592 593 594 595
		if (!disable && pull_up)
			return 0;
		else
			return -EINVAL;

	case PIN_CONFIG_BIAS_PULL_DOWN:
596
		iproc_gpio_get_pull(chip, gpio, &disable, &pull_up);
597 598 599 600 601 602
		if (!disable && !pull_up)
			return 0;
		else
			return -EINVAL;

	case PIN_CONFIG_DRIVE_STRENGTH:
603
		ret = iproc_gpio_get_strength(chip, gpio, &arg);
604 605
		if (ret)
			return ret;
606
		*config = pinconf_to_config_packed(param, arg);
607 608 609 610 611 612 613 614 615 616

		return 0;

	default:
		return -ENOTSUPP;
	}

	return -ENOTSUPP;
}

617
static int iproc_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
618 619
				 unsigned long *configs, unsigned num_configs)
{
620
	struct iproc_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
621 622
	enum pin_config_param param;
	u16 arg;
623
	unsigned i, gpio = iproc_pin_to_gpio(pin);
624 625 626 627
	int ret = -ENOTSUPP;

	for (i = 0; i < num_configs; i++) {
		param = pinconf_to_config_param(configs[i]);
628 629 630 631

		if (iproc_pinconf_param_is_disabled(chip, param))
			return -ENOTSUPP;

632 633 634 635
		arg = pinconf_to_config_argument(configs[i]);

		switch (param) {
		case PIN_CONFIG_BIAS_DISABLE:
636
			ret = iproc_gpio_set_pull(chip, gpio, true, false);
637 638 639 640 641
			if (ret < 0)
				goto out;
			break;

		case PIN_CONFIG_BIAS_PULL_UP:
642
			ret = iproc_gpio_set_pull(chip, gpio, false, true);
643 644 645 646 647
			if (ret < 0)
				goto out;
			break;

		case PIN_CONFIG_BIAS_PULL_DOWN:
648
			ret = iproc_gpio_set_pull(chip, gpio, false, false);
649 650 651 652 653
			if (ret < 0)
				goto out;
			break;

		case PIN_CONFIG_DRIVE_STRENGTH:
654
			ret = iproc_gpio_set_strength(chip, gpio, arg);
655 656 657 658 659 660 661 662 663 664 665 666 667 668
			if (ret < 0)
				goto out;
			break;

		default:
			dev_err(chip->dev, "invalid configuration\n");
			return -ENOTSUPP;
		}
	} /* for each config */

out:
	return ret;
}

669
static const struct pinconf_ops iproc_pconf_ops = {
670
	.is_generic = true,
671 672
	.pin_config_get = iproc_pin_config_get,
	.pin_config_set = iproc_pin_config_set,
673 674 675
};

/*
676
 * Iproc GPIO controller supports some PINCONF related configurations such as
677 678 679 680 681
 * pull up, pull down, and drive strength, when the pin is configured to GPIO
 *
 * Here a local pinctrl device is created with simple 1-to-1 pin mapping to the
 * local GPIO pins
 */
682
static int iproc_gpio_register_pinconf(struct iproc_gpio *chip)
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
{
	struct pinctrl_desc *pctldesc = &chip->pctldesc;
	struct pinctrl_pin_desc *pins;
	struct gpio_chip *gc = &chip->gc;
	int i;

	pins = devm_kcalloc(chip->dev, gc->ngpio, sizeof(*pins), GFP_KERNEL);
	if (!pins)
		return -ENOMEM;

	for (i = 0; i < gc->ngpio; i++) {
		pins[i].number = i;
		pins[i].name = devm_kasprintf(chip->dev, GFP_KERNEL,
					      "gpio-%d", i);
		if (!pins[i].name)
			return -ENOMEM;
	}

	pctldesc->name = dev_name(chip->dev);
702
	pctldesc->pctlops = &iproc_pctrl_ops;
703 704
	pctldesc->pins = pins;
	pctldesc->npins = gc->ngpio;
705
	pctldesc->confops = &iproc_pconf_ops;
706

707
	chip->pctl = devm_pinctrl_register(chip->dev, pctldesc, chip);
708
	if (IS_ERR(chip->pctl)) {
709
		dev_err(chip->dev, "unable to register pinctrl device\n");
710
		return PTR_ERR(chip->pctl);
711 712 713 714 715
	}

	return 0;
}

716
static const struct of_device_id iproc_gpio_of_match[] = {
717
	{ .compatible = "brcm,iproc-gpio" },
P
Pramod Kumar 已提交
718 719 720
	{ .compatible = "brcm,cygnus-ccm-gpio" },
	{ .compatible = "brcm,cygnus-asiu-gpio" },
	{ .compatible = "brcm,cygnus-crmu-gpio" },
721 722 723
	{ .compatible = "brcm,iproc-nsp-gpio" },
	{ .compatible = "brcm,iproc-stingray-gpio" },
	{ /* sentinel */ }
724 725
};

726
static int iproc_gpio_probe(struct platform_device *pdev)
727 728 729
{
	struct device *dev = &pdev->dev;
	struct resource *res;
730
	struct iproc_gpio *chip;
731
	struct gpio_chip *gc;
732
	u32 ngpios, pinconf_disable_mask = 0;
733
	int irq, ret;
734 735 736 737 738 739 740 741 742
	bool no_pinconf = false;

	/* NSP does not support drive strength config */
	if (of_device_is_compatible(dev->of_node, "brcm,iproc-nsp-gpio"))
		pinconf_disable_mask = BIT(IPROC_PINCONF_DRIVE_STRENGTH);
	/* Stingray does not support pinconf in this controller */
	else if (of_device_is_compatible(dev->of_node,
					 "brcm,iproc-stingray-gpio"))
		no_pinconf = true;
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766

	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
	if (!chip)
		return -ENOMEM;

	chip->dev = dev;
	platform_set_drvdata(pdev, chip);

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	chip->base = devm_ioremap_resource(dev, res);
	if (IS_ERR(chip->base)) {
		dev_err(dev, "unable to map I/O memory\n");
		return PTR_ERR(chip->base);
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
	if (res) {
		chip->io_ctrl = devm_ioremap_resource(dev, res);
		if (IS_ERR(chip->io_ctrl)) {
			dev_err(dev, "unable to map I/O memory\n");
			return PTR_ERR(chip->io_ctrl);
		}
	}

P
Pramod Kumar 已提交
767 768 769 770 771
	if (of_property_read_u32(dev->of_node, "ngpios", &ngpios)) {
		dev_err(&pdev->dev, "missing ngpios DT property\n");
		return -ENODEV;
	}

772 773 774 775 776 777 778
	spin_lock_init(&chip->lock);

	gc = &chip->gc;
	gc->base = -1;
	gc->ngpio = ngpios;
	chip->num_banks = (ngpios + NGPIOS_PER_BANK - 1) / NGPIOS_PER_BANK;
	gc->label = dev_name(dev);
779
	gc->parent = dev;
780
	gc->of_node = dev->of_node;
781 782 783 784 785 786
	gc->request = iproc_gpio_request;
	gc->free = iproc_gpio_free;
	gc->direction_input = iproc_gpio_direction_input;
	gc->direction_output = iproc_gpio_direction_output;
	gc->set = iproc_gpio_set;
	gc->get = iproc_gpio_get;
787

788 789 790
	chip->pinmux_is_supported = of_property_read_bool(dev->of_node,
							"gpio-ranges");

791
	ret = gpiochip_add_data(gc, chip);
792 793 794 795 796
	if (ret < 0) {
		dev_err(dev, "unable to add GPIO chip\n");
		return ret;
	}

797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
	if (!no_pinconf) {
		ret = iproc_gpio_register_pinconf(chip);
		if (ret) {
			dev_err(dev, "unable to register pinconf\n");
			goto err_rm_gpiochip;
		}

		if (pinconf_disable_mask) {
			ret = iproc_pinconf_disable_map_create(chip,
							 pinconf_disable_mask);
			if (ret) {
				dev_err(dev,
					"unable to create pinconf disable map\n");
				goto err_rm_gpiochip;
			}
		}
813 814 815 816 817
	}

	/* optional GPIO interrupt support */
	irq = platform_get_irq(pdev, 0);
	if (irq) {
818
		ret = gpiochip_irqchip_add(gc, &iproc_gpio_irq_chip, 0,
819 820 821
					   handle_simple_irq, IRQ_TYPE_NONE);
		if (ret) {
			dev_err(dev, "no GPIO irqchip\n");
822
			goto err_rm_gpiochip;
823 824
		}

825 826
		gpiochip_set_chained_irqchip(gc, &iproc_gpio_irq_chip, irq,
					     iproc_gpio_irq_handler);
827 828 829 830 831 832 833 834 835 836
	}

	return 0;

err_rm_gpiochip:
	gpiochip_remove(gc);

	return ret;
}

837
static struct platform_driver iproc_gpio_driver = {
838
	.driver = {
839 840
		.name = "iproc-gpio",
		.of_match_table = iproc_gpio_of_match,
841
	},
842
	.probe = iproc_gpio_probe,
843 844
};

845
static int __init iproc_gpio_init(void)
846
{
847
	return platform_driver_register(&iproc_gpio_driver);
848
}
849
arch_initcall_sync(iproc_gpio_init);