pinctrl-sirf.c 22.7 KB
Newer Older
1 2 3
/*
 * pinmux driver for CSR SiRFprimaII
 *
4 5
 * Copyright (c) 2011 - 2014 Cambridge Silicon Radio Limited, a CSR plc group
 * company.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 *
 * Licensed under GPLv2 or later.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/irq.h>
#include <linux/platform_device.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/pinctrl/pinmux.h>
#include <linux/pinctrl/consumer.h>
#include <linux/pinctrl/machine.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
#include <linux/bitops.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>

#include "pinctrl-sirf.h"

#define DRIVER_NAME "pinmux-sirf"

struct sirfsoc_gpio_bank {
	int id;
	int parent_irq;
	spinlock_t lock;
37 38 39 40 41
};

struct sirfsoc_gpio_chip {
	struct of_mm_gpio_chip chip;
	struct sirfsoc_gpio_bank sgpio_bank[SIRFSOC_GPIO_NO_OF_BANKS];
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
};

static DEFINE_SPINLOCK(sgpio_lock);

static struct sirfsoc_pin_group *sirfsoc_pin_groups;
static int sirfsoc_pingrp_cnt;

static int sirfsoc_get_groups_count(struct pinctrl_dev *pctldev)
{
	return sirfsoc_pingrp_cnt;
}

static const char *sirfsoc_get_group_name(struct pinctrl_dev *pctldev,
				       unsigned selector)
{
	return sirfsoc_pin_groups[selector].name;
}

60 61 62 63
static int sirfsoc_get_group_pins(struct pinctrl_dev *pctldev,
				unsigned selector,
				const unsigned **pins,
				unsigned *num_pins)
64 65 66 67 68 69
{
	*pins = sirfsoc_pin_groups[selector].pins;
	*num_pins = sirfsoc_pin_groups[selector].num_pins;
	return 0;
}

70 71
static void sirfsoc_pin_dbg_show(struct pinctrl_dev *pctldev,
				struct seq_file *s, unsigned offset)
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
{
	seq_printf(s, " " DRIVER_NAME);
}

static int sirfsoc_dt_node_to_map(struct pinctrl_dev *pctldev,
				 struct device_node *np_config,
				 struct pinctrl_map **map, unsigned *num_maps)
{
	struct sirfsoc_pmx *spmx = pinctrl_dev_get_drvdata(pctldev);
	struct device_node *np;
	struct property *prop;
	const char *function, *group;
	int ret, index = 0, count = 0;

	/* calculate number of maps required */
	for_each_child_of_node(np_config, np) {
		ret = of_property_read_string(np, "sirf,function", &function);
		if (ret < 0)
			return ret;

		ret = of_property_count_strings(np, "sirf,pins");
		if (ret < 0)
			return ret;

		count += ret;
	}

	if (!count) {
		dev_err(spmx->dev, "No child nodes passed via DT\n");
		return -ENODEV;
	}

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

	for_each_child_of_node(np_config, np) {
		of_property_read_string(np, "sirf,function", &function);
		of_property_for_each_string(np, "sirf,pins", prop, group) {
			(*map)[index].type = PIN_MAP_TYPE_MUX_GROUP;
			(*map)[index].data.mux.group = group;
			(*map)[index].data.mux.function = function;
			index++;
		}
	}

	*num_maps = count;

	return 0;
}

static void sirfsoc_dt_free_map(struct pinctrl_dev *pctldev,
		struct pinctrl_map *map, unsigned num_maps)
{
	kfree(map);
}

static struct pinctrl_ops sirfsoc_pctrl_ops = {
	.get_groups_count = sirfsoc_get_groups_count,
	.get_group_name = sirfsoc_get_group_name,
	.get_group_pins = sirfsoc_get_group_pins,
	.pin_dbg_show = sirfsoc_pin_dbg_show,
	.dt_node_to_map = sirfsoc_dt_node_to_map,
	.dt_free_map = sirfsoc_dt_free_map,
};

static struct sirfsoc_pmx_func *sirfsoc_pmx_functions;
static int sirfsoc_pmxfunc_cnt;

141 142
static void sirfsoc_pinmux_endisable(struct sirfsoc_pmx *spmx,
					unsigned selector, bool enable)
143 144
{
	int i;
145 146
	const struct sirfsoc_padmux *mux =
		sirfsoc_pmx_functions[selector].padmux;
147 148 149 150
	const struct sirfsoc_muxmask *mask = mux->muxmask;

	for (i = 0; i < mux->muxmask_counts; i++) {
		u32 muxval;
B
Barry Song 已提交
151 152 153 154 155 156 157 158
		muxval = readl(spmx->gpio_virtbase +
			SIRFSOC_GPIO_PAD_EN(mask[i].group));
		if (enable)
			muxval = muxval & ~mask[i].mask;
		else
			muxval = muxval | mask[i].mask;
		writel(muxval, spmx->gpio_virtbase +
			SIRFSOC_GPIO_PAD_EN(mask[i].group));
159 160 161 162
	}

	if (mux->funcmask && enable) {
		u32 func_en_val;
163

164
		func_en_val =
165
			readl(spmx->rsc_virtbase + mux->ctrlreg);
166
		func_en_val =
167 168
			(func_en_val & ~mux->funcmask) | (mux->funcval);
		writel(func_en_val, spmx->rsc_virtbase + mux->ctrlreg);
169 170 171
	}
}

172 173 174
static int sirfsoc_pinmux_set_mux(struct pinctrl_dev *pmxdev,
				unsigned selector,
				unsigned group)
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
{
	struct sirfsoc_pmx *spmx;

	spmx = pinctrl_dev_get_drvdata(pmxdev);
	sirfsoc_pinmux_endisable(spmx, selector, true);

	return 0;
}

static int sirfsoc_pinmux_get_funcs_count(struct pinctrl_dev *pmxdev)
{
	return sirfsoc_pmxfunc_cnt;
}

static const char *sirfsoc_pinmux_get_func_name(struct pinctrl_dev *pctldev,
					  unsigned selector)
{
	return sirfsoc_pmx_functions[selector].name;
}

195 196 197 198
static int sirfsoc_pinmux_get_groups(struct pinctrl_dev *pctldev,
				unsigned selector,
				const char * const **groups,
				unsigned * const num_groups)
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
{
	*groups = sirfsoc_pmx_functions[selector].groups;
	*num_groups = sirfsoc_pmx_functions[selector].num_groups;
	return 0;
}

static int sirfsoc_pinmux_request_gpio(struct pinctrl_dev *pmxdev,
	struct pinctrl_gpio_range *range, unsigned offset)
{
	struct sirfsoc_pmx *spmx;

	int group = range->id;

	u32 muxval;

	spmx = pinctrl_dev_get_drvdata(pmxdev);

B
Barry Song 已提交
216 217 218 219 220
	muxval = readl(spmx->gpio_virtbase +
		SIRFSOC_GPIO_PAD_EN(group));
	muxval = muxval | (1 << (offset - range->pin_base));
	writel(muxval, spmx->gpio_virtbase +
		SIRFSOC_GPIO_PAD_EN(group));
221 222 223 224 225

	return 0;
}

static struct pinmux_ops sirfsoc_pinmux_ops = {
226
	.set_mux = sirfsoc_pinmux_set_mux,
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	.get_functions_count = sirfsoc_pinmux_get_funcs_count,
	.get_function_name = sirfsoc_pinmux_get_func_name,
	.get_function_groups = sirfsoc_pinmux_get_groups,
	.gpio_request_enable = sirfsoc_pinmux_request_gpio,
};

static struct pinctrl_desc sirfsoc_pinmux_desc = {
	.name = DRIVER_NAME,
	.pctlops = &sirfsoc_pctrl_ops,
	.pmxops = &sirfsoc_pinmux_ops,
	.owner = THIS_MODULE,
};

static void __iomem *sirfsoc_rsc_of_iomap(void)
{
	const struct of_device_id rsc_ids[]  = {
		{ .compatible = "sirf,prima2-rsc" },
		{}
	};
	struct device_node *np;

	np = of_find_matching_node(NULL, rsc_ids);
	if (!np)
		panic("unable to find compatible rsc node in dtb\n");

	return of_iomap(np, 0);
}

static int sirfsoc_gpio_of_xlate(struct gpio_chip *gc,
256 257
	const struct of_phandle_args *gpiospec,
	u32 *flags)
258
{
259
	if (gpiospec->args[0] > SIRFSOC_GPIO_NO_OF_BANKS * SIRFSOC_GPIO_BANK_SIZE)
260
		return -EINVAL;
261

262
	if (flags)
263
		*flags = gpiospec->args[1];
264

265
	return gpiospec->args[0];
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 294 295 296 297 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
}

static const struct of_device_id pinmux_ids[] = {
	{ .compatible = "sirf,prima2-pinctrl", .data = &prima2_pinctrl_data, },
	{ .compatible = "sirf,atlas6-pinctrl", .data = &atlas6_pinctrl_data, },
	{}
};

static int sirfsoc_pinmux_probe(struct platform_device *pdev)
{
	int ret;
	struct sirfsoc_pmx *spmx;
	struct device_node *np = pdev->dev.of_node;
	const struct sirfsoc_pinctrl_data *pdata;

	/* Create state holders etc for this driver */
	spmx = devm_kzalloc(&pdev->dev, sizeof(*spmx), GFP_KERNEL);
	if (!spmx)
		return -ENOMEM;

	spmx->dev = &pdev->dev;

	platform_set_drvdata(pdev, spmx);

	spmx->gpio_virtbase = of_iomap(np, 0);
	if (!spmx->gpio_virtbase) {
		dev_err(&pdev->dev, "can't map gpio registers\n");
		return -ENOMEM;
	}

	spmx->rsc_virtbase = sirfsoc_rsc_of_iomap();
	if (!spmx->rsc_virtbase) {
		ret = -ENOMEM;
		dev_err(&pdev->dev, "can't map rsc registers\n");
		goto out_no_rsc_remap;
	}

	pdata = of_match_node(pinmux_ids, np)->data;
	sirfsoc_pin_groups = pdata->grps;
	sirfsoc_pingrp_cnt = pdata->grps_cnt;
	sirfsoc_pmx_functions = pdata->funcs;
	sirfsoc_pmxfunc_cnt = pdata->funcs_cnt;
	sirfsoc_pinmux_desc.pins = pdata->pads;
	sirfsoc_pinmux_desc.npins = pdata->pads_cnt;


	/* Now register the pin controller and all pins it handles */
	spmx->pmx = pinctrl_register(&sirfsoc_pinmux_desc, &pdev->dev, spmx);
	if (!spmx->pmx) {
		dev_err(&pdev->dev, "could not register SIRFSOC pinmux driver\n");
		ret = -EINVAL;
		goto out_no_pmx;
	}

	dev_info(&pdev->dev, "initialized SIRFSOC pinmux driver\n");

	return 0;

out_no_pmx:
	iounmap(spmx->rsc_virtbase);
out_no_rsc_remap:
	iounmap(spmx->gpio_virtbase);
	return ret;
}

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 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
#ifdef CONFIG_PM_SLEEP
static int sirfsoc_pinmux_suspend_noirq(struct device *dev)
{
	int i, j;
	struct sirfsoc_pmx *spmx = dev_get_drvdata(dev);

	for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
		for (j = 0; j < SIRFSOC_GPIO_BANK_SIZE; j++) {
			spmx->gpio_regs[i][j] = readl(spmx->gpio_virtbase +
				SIRFSOC_GPIO_CTRL(i, j));
		}
		spmx->ints_regs[i] = readl(spmx->gpio_virtbase +
			SIRFSOC_GPIO_INT_STATUS(i));
		spmx->paden_regs[i] = readl(spmx->gpio_virtbase +
			SIRFSOC_GPIO_PAD_EN(i));
	}
	spmx->dspen_regs = readl(spmx->gpio_virtbase + SIRFSOC_GPIO_DSP_EN0);

	for (i = 0; i < 3; i++)
		spmx->rsc_regs[i] = readl(spmx->rsc_virtbase + 4 * i);

	return 0;
}

static int sirfsoc_pinmux_resume_noirq(struct device *dev)
{
	int i, j;
	struct sirfsoc_pmx *spmx = dev_get_drvdata(dev);

	for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
		for (j = 0; j < SIRFSOC_GPIO_BANK_SIZE; j++) {
			writel(spmx->gpio_regs[i][j], spmx->gpio_virtbase +
				SIRFSOC_GPIO_CTRL(i, j));
		}
		writel(spmx->ints_regs[i], spmx->gpio_virtbase +
			SIRFSOC_GPIO_INT_STATUS(i));
		writel(spmx->paden_regs[i], spmx->gpio_virtbase +
			SIRFSOC_GPIO_PAD_EN(i));
	}
	writel(spmx->dspen_regs, spmx->gpio_virtbase + SIRFSOC_GPIO_DSP_EN0);

	for (i = 0; i < 3; i++)
		writel(spmx->rsc_regs[i], spmx->rsc_virtbase + 4 * i);

	return 0;
}

static const struct dev_pm_ops sirfsoc_pinmux_pm_ops = {
	.suspend_noirq = sirfsoc_pinmux_suspend_noirq,
	.resume_noirq = sirfsoc_pinmux_resume_noirq,
381 382
	.freeze_noirq = sirfsoc_pinmux_suspend_noirq,
	.restore_noirq = sirfsoc_pinmux_resume_noirq,
383 384 385
};
#endif

386 387 388 389
static struct platform_driver sirfsoc_pinmux_driver = {
	.driver = {
		.name = DRIVER_NAME,
		.of_match_table = pinmux_ids,
390 391 392
#ifdef CONFIG_PM_SLEEP
		.pm = &sirfsoc_pinmux_pm_ops,
#endif
393 394 395 396 397 398 399 400 401 402
	},
	.probe = sirfsoc_pinmux_probe,
};

static int __init sirfsoc_pinmux_init(void)
{
	return platform_driver_register(&sirfsoc_pinmux_driver);
}
arch_initcall(sirfsoc_pinmux_init);

403 404 405 406 407 408 409
static inline struct sirfsoc_gpio_chip *to_sirfsoc_gpio(struct gpio_chip *gc)
{
	return container_of(gc, struct sirfsoc_gpio_chip, chip.gc);
}

static inline struct sirfsoc_gpio_bank *
sirfsoc_gpio_to_bank(struct sirfsoc_gpio_chip *sgpio, unsigned int offset)
410
{
411
	return &sgpio->sgpio_bank[offset / SIRFSOC_GPIO_BANK_SIZE];
412 413
}

414
static inline int sirfsoc_gpio_to_bankoff(unsigned int offset)
415
{
416
	return offset % SIRFSOC_GPIO_BANK_SIZE;
417
}
418

419 420
static void sirfsoc_gpio_irq_ack(struct irq_data *d)
{
421 422 423 424
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
	struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(gc);
	struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
	int idx = sirfsoc_gpio_to_bankoff(d->hwirq);
425 426 427 428 429 430 431
	u32 val, offset;
	unsigned long flags;

	offset = SIRFSOC_GPIO_CTRL(bank->id, idx);

	spin_lock_irqsave(&sgpio_lock, flags);

432
	val = readl(sgpio->chip.regs + offset);
433

434
	writel(val, sgpio->chip.regs + offset);
435 436 437 438

	spin_unlock_irqrestore(&sgpio_lock, flags);
}

439 440 441
static void __sirfsoc_gpio_irq_mask(struct sirfsoc_gpio_chip *sgpio,
				    struct sirfsoc_gpio_bank *bank,
				    int idx)
442 443 444 445 446 447 448 449
{
	u32 val, offset;
	unsigned long flags;

	offset = SIRFSOC_GPIO_CTRL(bank->id, idx);

	spin_lock_irqsave(&sgpio_lock, flags);

450
	val = readl(sgpio->chip.regs + offset);
451 452
	val &= ~SIRFSOC_GPIO_CTL_INTR_EN_MASK;
	val &= ~SIRFSOC_GPIO_CTL_INTR_STS_MASK;
453
	writel(val, sgpio->chip.regs + offset);
454 455 456 457 458 459

	spin_unlock_irqrestore(&sgpio_lock, flags);
}

static void sirfsoc_gpio_irq_mask(struct irq_data *d)
{
460 461 462
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
	struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(gc);
	struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
463

464
	__sirfsoc_gpio_irq_mask(sgpio, bank, d->hwirq % SIRFSOC_GPIO_BANK_SIZE);
465 466 467 468
}

static void sirfsoc_gpio_irq_unmask(struct irq_data *d)
{
469 470 471 472
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
	struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(gc);
	struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
	int idx = sirfsoc_gpio_to_bankoff(d->hwirq);
473 474 475 476 477 478 479
	u32 val, offset;
	unsigned long flags;

	offset = SIRFSOC_GPIO_CTRL(bank->id, idx);

	spin_lock_irqsave(&sgpio_lock, flags);

480
	val = readl(sgpio->chip.regs + offset);
481 482
	val &= ~SIRFSOC_GPIO_CTL_INTR_STS_MASK;
	val |= SIRFSOC_GPIO_CTL_INTR_EN_MASK;
483
	writel(val, sgpio->chip.regs + offset);
484 485 486 487 488 489

	spin_unlock_irqrestore(&sgpio_lock, flags);
}

static int sirfsoc_gpio_irq_type(struct irq_data *d, unsigned type)
{
490 491 492 493
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
	struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(gc);
	struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
	int idx = sirfsoc_gpio_to_bankoff(d->hwirq);
494 495 496 497 498 499 500
	u32 val, offset;
	unsigned long flags;

	offset = SIRFSOC_GPIO_CTRL(bank->id, idx);

	spin_lock_irqsave(&sgpio_lock, flags);

501
	val = readl(sgpio->chip.regs + offset);
502
	val &= ~(SIRFSOC_GPIO_CTL_INTR_STS_MASK | SIRFSOC_GPIO_CTL_OUT_EN_MASK);
503 504 505 506 507

	switch (type) {
	case IRQ_TYPE_NONE:
		break;
	case IRQ_TYPE_EDGE_RISING:
508 509
		val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK |
			SIRFSOC_GPIO_CTL_INTR_TYPE_MASK;
510 511 512 513
		val &= ~SIRFSOC_GPIO_CTL_INTR_LOW_MASK;
		break;
	case IRQ_TYPE_EDGE_FALLING:
		val &= ~SIRFSOC_GPIO_CTL_INTR_HIGH_MASK;
514 515
		val |= SIRFSOC_GPIO_CTL_INTR_LOW_MASK |
			SIRFSOC_GPIO_CTL_INTR_TYPE_MASK;
516 517
		break;
	case IRQ_TYPE_EDGE_BOTH:
518 519 520
		val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK |
			SIRFSOC_GPIO_CTL_INTR_LOW_MASK |
			SIRFSOC_GPIO_CTL_INTR_TYPE_MASK;
521 522
		break;
	case IRQ_TYPE_LEVEL_LOW:
523 524
		val &= ~(SIRFSOC_GPIO_CTL_INTR_HIGH_MASK |
			SIRFSOC_GPIO_CTL_INTR_TYPE_MASK);
525 526 527 528
		val |= SIRFSOC_GPIO_CTL_INTR_LOW_MASK;
		break;
	case IRQ_TYPE_LEVEL_HIGH:
		val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK;
529 530
		val &= ~(SIRFSOC_GPIO_CTL_INTR_LOW_MASK |
			SIRFSOC_GPIO_CTL_INTR_TYPE_MASK);
531 532 533
		break;
	}

534
	writel(val, sgpio->chip.regs + offset);
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550

	spin_unlock_irqrestore(&sgpio_lock, flags);

	return 0;
}

static struct irq_chip sirfsoc_irq_chip = {
	.name = "sirf-gpio-irq",
	.irq_ack = sirfsoc_gpio_irq_ack,
	.irq_mask = sirfsoc_gpio_irq_mask,
	.irq_unmask = sirfsoc_gpio_irq_unmask,
	.irq_set_type = sirfsoc_gpio_irq_type,
};

static void sirfsoc_gpio_handle_irq(unsigned int irq, struct irq_desc *desc)
{
551 552
	struct gpio_chip *gc = irq_desc_get_handler_data(desc);
	struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(gc);
553
	struct sirfsoc_gpio_bank *bank;
554 555 556
	u32 status, ctrl;
	int idx = 0;
	struct irq_chip *chip = irq_get_chip(irq);
557 558
	int i;

559
	for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
560
		bank = &sgpio->sgpio_bank[i];
561 562 563
		if (bank->parent_irq == irq)
			break;
	}
564
	BUG_ON(i == SIRFSOC_GPIO_NO_OF_BANKS);
565 566 567

	chained_irq_enter(chip, desc);

568
	status = readl(sgpio->chip.regs + SIRFSOC_GPIO_INT_STATUS(bank->id));
569 570
	if (!status) {
		printk(KERN_WARNING
571
			"%s: gpio id %d status %#x no interrupt is flagged\n",
572 573 574 575 576 577
			__func__, bank->id, status);
		handle_bad_irq(irq, desc);
		return;
	}

	while (status) {
578
		ctrl = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, idx));
579 580 581 582 583 584 585 586

		/*
		 * Here we must check whether the corresponding GPIO's interrupt
		 * has been enabled, otherwise just skip it
		 */
		if ((status & 0x1) && (ctrl & SIRFSOC_GPIO_CTL_INTR_EN_MASK)) {
			pr_debug("%s: gpio id %d idx %d happens\n",
				__func__, bank->id, idx);
587
			generic_handle_irq(irq_find_mapping(gc->irqdomain, idx +
588
					bank->id * SIRFSOC_GPIO_BANK_SIZE));
589 590 591 592 593 594 595 596 597
		}

		idx++;
		status = status >> 1;
	}

	chained_irq_exit(chip, desc);
}

598 599
static inline void sirfsoc_gpio_set_input(struct sirfsoc_gpio_chip *sgpio,
					  unsigned ctrl_offset)
600 601 602
{
	u32 val;

603
	val = readl(sgpio->chip.regs + ctrl_offset);
604
	val &= ~SIRFSOC_GPIO_CTL_OUT_EN_MASK;
605
	writel(val, sgpio->chip.regs + ctrl_offset);
606 607 608 609
}

static int sirfsoc_gpio_request(struct gpio_chip *chip, unsigned offset)
{
610 611
	struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
	struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
612 613 614 615 616 617 618 619 620 621 622
	unsigned long flags;

	if (pinctrl_request_gpio(chip->base + offset))
		return -ENODEV;

	spin_lock_irqsave(&bank->lock, flags);

	/*
	 * default status:
	 * set direction as input and mask irq
	 */
623 624
	sirfsoc_gpio_set_input(sgpio, SIRFSOC_GPIO_CTRL(bank->id, offset));
	__sirfsoc_gpio_irq_mask(sgpio, bank, offset);
625 626 627 628 629 630 631 632

	spin_unlock_irqrestore(&bank->lock, flags);

	return 0;
}

static void sirfsoc_gpio_free(struct gpio_chip *chip, unsigned offset)
{
633 634
	struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
	struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
635 636 637 638
	unsigned long flags;

	spin_lock_irqsave(&bank->lock, flags);

639 640
	__sirfsoc_gpio_irq_mask(sgpio, bank, offset);
	sirfsoc_gpio_set_input(sgpio, SIRFSOC_GPIO_CTRL(bank->id, offset));
641 642 643 644 645 646 647 648

	spin_unlock_irqrestore(&bank->lock, flags);

	pinctrl_free_gpio(chip->base + offset);
}

static int sirfsoc_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
{
649 650
	struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
	struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, gpio);
651
	int idx = sirfsoc_gpio_to_bankoff(gpio);
652 653 654 655 656 657 658
	unsigned long flags;
	unsigned offset;

	offset = SIRFSOC_GPIO_CTRL(bank->id, idx);

	spin_lock_irqsave(&bank->lock, flags);

659
	sirfsoc_gpio_set_input(sgpio, offset);
660 661 662 663 664 665

	spin_unlock_irqrestore(&bank->lock, flags);

	return 0;
}

666 667 668 669
static inline void sirfsoc_gpio_set_output(struct sirfsoc_gpio_chip *sgpio,
					   struct sirfsoc_gpio_bank *bank,
					   unsigned offset,
					   int value)
670 671 672 673 674 675
{
	u32 out_ctrl;
	unsigned long flags;

	spin_lock_irqsave(&bank->lock, flags);

676
	out_ctrl = readl(sgpio->chip.regs + offset);
677 678 679 680 681 682 683
	if (value)
		out_ctrl |= SIRFSOC_GPIO_CTL_DATAOUT_MASK;
	else
		out_ctrl &= ~SIRFSOC_GPIO_CTL_DATAOUT_MASK;

	out_ctrl &= ~SIRFSOC_GPIO_CTL_INTR_EN_MASK;
	out_ctrl |= SIRFSOC_GPIO_CTL_OUT_EN_MASK;
684
	writel(out_ctrl, sgpio->chip.regs + offset);
685 686 687 688

	spin_unlock_irqrestore(&bank->lock, flags);
}

689 690
static int sirfsoc_gpio_direction_output(struct gpio_chip *chip,
	unsigned gpio, int value)
691
{
692 693
	struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
	struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, gpio);
694
	int idx = sirfsoc_gpio_to_bankoff(gpio);
695 696 697 698 699 700 701
	u32 offset;
	unsigned long flags;

	offset = SIRFSOC_GPIO_CTRL(bank->id, idx);

	spin_lock_irqsave(&sgpio_lock, flags);

702
	sirfsoc_gpio_set_output(sgpio, bank, offset, value);
703 704 705 706 707 708 709 710

	spin_unlock_irqrestore(&sgpio_lock, flags);

	return 0;
}

static int sirfsoc_gpio_get_value(struct gpio_chip *chip, unsigned offset)
{
711 712
	struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
	struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
713 714 715 716 717
	u32 val;
	unsigned long flags;

	spin_lock_irqsave(&bank->lock, flags);

718
	val = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
719 720 721 722 723 724 725 726 727

	spin_unlock_irqrestore(&bank->lock, flags);

	return !!(val & SIRFSOC_GPIO_CTL_DATAIN_MASK);
}

static void sirfsoc_gpio_set_value(struct gpio_chip *chip, unsigned offset,
	int value)
{
728 729
	struct sirfsoc_gpio_chip *sgpio = to_sirfsoc_gpio(chip);
	struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
730 731 732 733 734
	u32 ctrl;
	unsigned long flags;

	spin_lock_irqsave(&bank->lock, flags);

735
	ctrl = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
736 737 738 739
	if (value)
		ctrl |= SIRFSOC_GPIO_CTL_DATAOUT_MASK;
	else
		ctrl &= ~SIRFSOC_GPIO_CTL_DATAOUT_MASK;
740
	writel(ctrl, sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
741 742 743 744

	spin_unlock_irqrestore(&bank->lock, flags);
}

745 746
static void sirfsoc_gpio_set_pullup(struct sirfsoc_gpio_chip *sgpio,
				    const u32 *pullups)
747 748 749 750 751 752 753
{
	int i, n;
	const unsigned long *p = (const unsigned long *)pullups;

	for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
		for_each_set_bit(n, p + i, BITS_PER_LONG) {
			u32 offset = SIRFSOC_GPIO_CTRL(i, n);
754
			u32 val = readl(sgpio->chip.regs + offset);
755 756
			val |= SIRFSOC_GPIO_CTL_PULL_MASK;
			val |= SIRFSOC_GPIO_CTL_PULL_HIGH;
757
			writel(val, sgpio->chip.regs + offset);
758 759 760 761
		}
	}
}

762 763
static void sirfsoc_gpio_set_pulldown(struct sirfsoc_gpio_chip *sgpio,
				      const u32 *pulldowns)
764 765 766 767 768 769 770
{
	int i, n;
	const unsigned long *p = (const unsigned long *)pulldowns;

	for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
		for_each_set_bit(n, p + i, BITS_PER_LONG) {
			u32 offset = SIRFSOC_GPIO_CTRL(i, n);
771
			u32 val = readl(sgpio->chip.regs + offset);
772 773
			val |= SIRFSOC_GPIO_CTL_PULL_MASK;
			val &= ~SIRFSOC_GPIO_CTL_PULL_HIGH;
774
			writel(val, sgpio->chip.regs + offset);
775 776 777 778 779 780 781
		}
	}
}

static int sirfsoc_gpio_probe(struct device_node *np)
{
	int i, err = 0;
782
	static struct sirfsoc_gpio_chip *sgpio;
783
	struct sirfsoc_gpio_bank *bank;
784
	void __iomem *regs;
785 786 787 788 789 790 791 792
	struct platform_device *pdev;

	u32 pullups[SIRFSOC_GPIO_NO_OF_BANKS], pulldowns[SIRFSOC_GPIO_NO_OF_BANKS];

	pdev = of_find_device_by_node(np);
	if (!pdev)
		return -ENODEV;

793 794 795 796
	sgpio = devm_kzalloc(&pdev->dev, sizeof(*sgpio), GFP_KERNEL);
	if (!sgpio)
		return -ENOMEM;

797 798 799 800
	regs = of_iomap(np, 0);
	if (!regs)
		return -ENOMEM;

801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
	sgpio->chip.gc.request = sirfsoc_gpio_request;
	sgpio->chip.gc.free = sirfsoc_gpio_free;
	sgpio->chip.gc.direction_input = sirfsoc_gpio_direction_input;
	sgpio->chip.gc.get = sirfsoc_gpio_get_value;
	sgpio->chip.gc.direction_output = sirfsoc_gpio_direction_output;
	sgpio->chip.gc.set = sirfsoc_gpio_set_value;
	sgpio->chip.gc.base = 0;
	sgpio->chip.gc.ngpio = SIRFSOC_GPIO_BANK_SIZE * SIRFSOC_GPIO_NO_OF_BANKS;
	sgpio->chip.gc.label = kstrdup(np->full_name, GFP_KERNEL);
	sgpio->chip.gc.of_node = np;
	sgpio->chip.gc.of_xlate = sirfsoc_gpio_of_xlate;
	sgpio->chip.gc.of_gpio_n_cells = 2;
	sgpio->chip.gc.dev = &pdev->dev;
	sgpio->chip.regs = regs;

	err = gpiochip_add(&sgpio->chip.gc);
817
	if (err) {
818
		dev_err(&pdev->dev, "%s: error in probe function with status %d\n",
819 820 821 822
			np->full_name, err);
		goto out;
	}

823
	err =  gpiochip_irqchip_add(&sgpio->chip.gc,
824 825 826 827 828 829
		&sirfsoc_irq_chip,
		0, handle_level_irq,
		IRQ_TYPE_NONE);
	if (err) {
		dev_err(&pdev->dev,
			"could not connect irqchip to gpiochip\n");
830
		goto out_banks;
831 832
	}

833
	for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
834
		bank = &sgpio->sgpio_bank[i];
835 836 837 838
		spin_lock_init(&bank->lock);
		bank->parent_irq = platform_get_irq(pdev, i);
		if (bank->parent_irq < 0) {
			err = bank->parent_irq;
839
			goto out_banks;
840 841
		}

842
		gpiochip_set_chained_irqchip(&sgpio->chip.gc,
843 844 845
			&sirfsoc_irq_chip,
			bank->parent_irq,
			sirfsoc_gpio_handle_irq);
846 847
	}

848 849 850 851 852 853 854 855
	err = gpiochip_add_pin_range(&sgpio->chip.gc, dev_name(&pdev->dev),
		0, 0, SIRFSOC_GPIO_BANK_SIZE * SIRFSOC_GPIO_NO_OF_BANKS);
	if (err) {
		dev_err(&pdev->dev,
			"could not add gpiochip pin range\n");
		goto out_no_range;
	}

856 857
	if (!of_property_read_u32_array(np, "sirf,pullups", pullups,
		SIRFSOC_GPIO_NO_OF_BANKS))
858
		sirfsoc_gpio_set_pullup(sgpio, pullups);
859 860 861

	if (!of_property_read_u32_array(np, "sirf,pulldowns", pulldowns,
		SIRFSOC_GPIO_NO_OF_BANKS))
862
		sirfsoc_gpio_set_pulldown(sgpio, pulldowns);
863 864 865

	return 0;

866 867
out_no_range:
out_banks:
868
	gpiochip_remove(&sgpio->chip.gc);
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
out:
	iounmap(regs);
	return err;
}

static int __init sirfsoc_gpio_init(void)
{

	struct device_node *np;

	np = of_find_matching_node(NULL, pinmux_ids);

	if (!np)
		return -ENODEV;

	return sirfsoc_gpio_probe(np);
}
subsys_initcall(sirfsoc_gpio_init);

888 889 890
MODULE_AUTHOR("Rongjun Ying <rongjun.ying@csr.com>");
MODULE_AUTHOR("Yuping Luo <yuping.luo@csr.com>");
MODULE_AUTHOR("Barry Song <baohua.song@csr.com>");
891 892
MODULE_DESCRIPTION("SIRFSOC pin control driver");
MODULE_LICENSE("GPL");