gpio-rcar.c 14 KB
Newer Older
1 2 3
/*
 * Renesas R-Car GPIO Support
 *
4
 *  Copyright (C) 2014 Renesas Electronics Corporation
5 6 7 8 9 10 11 12 13 14 15 16
 *  Copyright (C) 2013 Magnus Damm
 *
 * 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; either version 2 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

17
#include <linux/clk.h>
18 19 20 21 22 23 24 25
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/ioport.h>
#include <linux/irq.h>
#include <linux/module.h>
26
#include <linux/of.h>
27
#include <linux/of_device.h>
28
#include <linux/pinctrl/consumer.h>
29
#include <linux/platform_device.h>
30
#include <linux/pm_runtime.h>
31 32 33 34 35 36 37 38 39
#include <linux/spinlock.h>
#include <linux/slab.h>

struct gpio_rcar_priv {
	void __iomem *base;
	spinlock_t lock;
	struct platform_device *pdev;
	struct gpio_chip gpio_chip;
	struct irq_chip irq_chip;
40
	struct clk *clk;
41 42
	unsigned int irq_parent;
	bool has_both_edge_trigger;
43
	bool needs_clk;
44 45
};

46 47 48 49 50 51 52 53 54 55 56 57
#define IOINTSEL 0x00	/* General IO/Interrupt Switching Register */
#define INOUTSEL 0x04	/* General Input/Output Switching Register */
#define OUTDT 0x08	/* General Output Register */
#define INDT 0x0c	/* General Input Register */
#define INTDT 0x10	/* Interrupt Display Register */
#define INTCLR 0x14	/* Interrupt Clear Register */
#define INTMSK 0x18	/* Interrupt Mask Register */
#define MSKCLR 0x1c	/* Interrupt Mask Clear Register */
#define POSNEG 0x20	/* Positive/Negative Logic Select Register */
#define EDGLEVEL 0x24	/* Edge/level Select Register */
#define FILONOFF 0x28	/* Chattering Prevention On/Off Register */
#define BOTHEDGE 0x4c	/* One Edge/Both Edge Select Register */
58

59 60
#define RCAR_MAX_GPIO_PER_BANK		32

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
static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs)
{
	return ioread32(p->base + offs);
}

static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs,
				   u32 value)
{
	iowrite32(value, p->base + offs);
}

static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs,
				 int bit, bool value)
{
	u32 tmp = gpio_rcar_read(p, offs);

	if (value)
		tmp |= BIT(bit);
	else
		tmp &= ~BIT(bit);

	gpio_rcar_write(p, offs, tmp);
}

static void gpio_rcar_irq_disable(struct irq_data *d)
{
87
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
88
	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
89 90 91 92 93 94

	gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
}

static void gpio_rcar_irq_enable(struct irq_data *d)
{
95
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
96
	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
97 98 99 100 101 102 103

	gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
}

static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
						  unsigned int hwirq,
						  bool active_high_rising_edge,
104 105
						  bool level_trigger,
						  bool both)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
{
	unsigned long flags;

	/* follow steps in the GPIO documentation for
	 * "Setting Edge-Sensitive Interrupt Input Mode" and
	 * "Setting Level-Sensitive Interrupt Input Mode"
	 */

	spin_lock_irqsave(&p->lock, flags);

	/* Configure postive or negative logic in POSNEG */
	gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);

	/* Configure edge or level trigger in EDGLEVEL */
	gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);

122
	/* Select one edge or both edges in BOTHEDGE */
123
	if (p->has_both_edge_trigger)
124 125
		gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);

126 127 128 129 130 131 132 133 134 135 136 137
	/* Select "Interrupt Input Mode" in IOINTSEL */
	gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);

	/* Write INTCLR in case of edge trigger */
	if (!level_trigger)
		gpio_rcar_write(p, INTCLR, BIT(hwirq));

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

static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
{
138
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
139
	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
140 141 142 143 144 145
	unsigned int hwirq = irqd_to_hwirq(d);

	dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type);

	switch (type & IRQ_TYPE_SENSE_MASK) {
	case IRQ_TYPE_LEVEL_HIGH:
146 147
		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
						      false);
148 149
		break;
	case IRQ_TYPE_LEVEL_LOW:
150 151
		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
						      false);
152 153
		break;
	case IRQ_TYPE_EDGE_RISING:
154 155
		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
						      false);
156 157
		break;
	case IRQ_TYPE_EDGE_FALLING:
158 159 160 161
		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
						      false);
		break;
	case IRQ_TYPE_EDGE_BOTH:
162
		if (!p->has_both_edge_trigger)
163 164 165
			return -EINVAL;
		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
						      true);
166 167 168 169 170 171 172
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

173 174 175
static int gpio_rcar_irq_set_wake(struct irq_data *d, unsigned int on)
{
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
176
	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
177 178 179 180 181 182 183 184 185 186 187
	int error;

	if (p->irq_parent) {
		error = irq_set_irq_wake(p->irq_parent, on);
		if (error) {
			dev_dbg(&p->pdev->dev,
				"irq %u doesn't support irq_set_wake\n",
				p->irq_parent);
			p->irq_parent = 0;
		}
	}
188 189 190 191 192 193 194 195 196 197 198 199

	if (!p->clk)
		return 0;

	if (on)
		clk_enable(p->clk);
	else
		clk_disable(p->clk);

	return 0;
}

200 201 202 203 204 205
static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
{
	struct gpio_rcar_priv *p = dev_id;
	u32 pending;
	unsigned int offset, irqs_handled = 0;

206 207
	while ((pending = gpio_rcar_read(p, INTDT) &
			  gpio_rcar_read(p, INTMSK))) {
208 209
		offset = __ffs(pending);
		gpio_rcar_write(p, INTCLR, BIT(offset));
210
		generic_handle_irq(irq_find_mapping(p->gpio_chip.irq.domain,
211
						    offset));
212 213 214 215 216 217 218 219 220 221
		irqs_handled++;
	}

	return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
}

static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
						       unsigned int gpio,
						       bool output)
{
222
	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
	unsigned long flags;

	/* follow steps in the GPIO documentation for
	 * "Setting General Output Mode" and
	 * "Setting General Input Mode"
	 */

	spin_lock_irqsave(&p->lock, flags);

	/* Configure postive logic in POSNEG */
	gpio_rcar_modify_bit(p, POSNEG, gpio, false);

	/* Select "General Input/Output Mode" in IOINTSEL */
	gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);

	/* Select Input Mode or Output Mode in INOUTSEL */
	gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);

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

244 245
static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
{
246 247 248 249 250 251 252
	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
	int error;

	error = pm_runtime_get_sync(&p->pdev->dev);
	if (error < 0)
		return error;

253
	error = pinctrl_gpio_request(chip->base + offset);
254 255 256 257
	if (error)
		pm_runtime_put(&p->pdev->dev);

	return error;
258 259 260 261
}

static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
{
262 263
	struct gpio_rcar_priv *p = gpiochip_get_data(chip);

264
	pinctrl_gpio_free(chip->base + offset);
265

266 267
	/*
	 * Set the GPIO as an input to ensure that the next GPIO request won't
268 269 270
	 * drive the GPIO pin as an output.
	 */
	gpio_rcar_config_general_input_output_mode(chip, offset, false);
271 272

	pm_runtime_put(&p->pdev->dev);
273 274
}

275 276 277 278 279 280 281 282
static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
{
	gpio_rcar_config_general_input_output_mode(chip, offset, false);
	return 0;
}

static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
{
283 284 285 286
	u32 bit = BIT(offset);

	/* testing on r8a7790 shows that INDT does not show correct pin state
	 * when configured as output, so use OUTDT in case of output pins */
287 288
	if (gpio_rcar_read(gpiochip_get_data(chip), INOUTSEL) & bit)
		return !!(gpio_rcar_read(gpiochip_get_data(chip), OUTDT) & bit);
289
	else
290
		return !!(gpio_rcar_read(gpiochip_get_data(chip), INDT) & bit);
291 292 293 294
}

static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
{
295
	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
296 297 298 299 300 301 302
	unsigned long flags;

	spin_lock_irqsave(&p->lock, flags);
	gpio_rcar_modify_bit(p, OUTDT, offset, value);
	spin_unlock_irqrestore(&p->lock, flags);
}

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
static void gpio_rcar_set_multiple(struct gpio_chip *chip, unsigned long *mask,
				   unsigned long *bits)
{
	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
	unsigned long flags;
	u32 val, bankmask;

	bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0);
	if (!bankmask)
		return;

	spin_lock_irqsave(&p->lock, flags);
	val = gpio_rcar_read(p, OUTDT);
	val &= ~bankmask;
	val |= (bankmask & bits[0]);
	gpio_rcar_write(p, OUTDT, val);
	spin_unlock_irqrestore(&p->lock, flags);
}

322 323 324 325 326 327 328 329 330
static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
				      int value)
{
	/* write GPIO value to output before selecting output mode of pin */
	gpio_rcar_set(chip, offset, value);
	gpio_rcar_config_general_input_output_mode(chip, offset, true);
	return 0;
}

331 332
struct gpio_rcar_info {
	bool has_both_edge_trigger;
333
	bool needs_clk;
334 335
};

336 337
static const struct gpio_rcar_info gpio_rcar_info_gen1 = {
	.has_both_edge_trigger = false,
338
	.needs_clk = false,
339 340 341 342
};

static const struct gpio_rcar_info gpio_rcar_info_gen2 = {
	.has_both_edge_trigger = true,
343
	.needs_clk = true,
344 345
};

346 347
static const struct of_device_id gpio_rcar_of_table[] = {
	{
348 349 350 351
		.compatible = "renesas,gpio-r8a7743",
		/* RZ/G1 GPIO is identical to R-Car Gen2. */
		.data = &gpio_rcar_info_gen2,
	}, {
352
		.compatible = "renesas,gpio-r8a7790",
353
		.data = &gpio_rcar_info_gen2,
354 355
	}, {
		.compatible = "renesas,gpio-r8a7791",
356
		.data = &gpio_rcar_info_gen2,
357 358 359
	}, {
		.compatible = "renesas,gpio-r8a7792",
		.data = &gpio_rcar_info_gen2,
360 361 362 363 364 365
	}, {
		.compatible = "renesas,gpio-r8a7793",
		.data = &gpio_rcar_info_gen2,
	}, {
		.compatible = "renesas,gpio-r8a7794",
		.data = &gpio_rcar_info_gen2,
366 367 368 369
	}, {
		.compatible = "renesas,gpio-r8a7795",
		/* Gen3 GPIO is identical to Gen2. */
		.data = &gpio_rcar_info_gen2,
370 371 372 373
	}, {
		.compatible = "renesas,gpio-r8a7796",
		/* Gen3 GPIO is identical to Gen2. */
		.data = &gpio_rcar_info_gen2,
374 375 376 377 378 379 380 381 382 383
	}, {
		.compatible = "renesas,rcar-gen1-gpio",
		.data = &gpio_rcar_info_gen1,
	}, {
		.compatible = "renesas,rcar-gen2-gpio",
		.data = &gpio_rcar_info_gen2,
	}, {
		.compatible = "renesas,rcar-gen3-gpio",
		/* Gen3 GPIO is identical to Gen2. */
		.data = &gpio_rcar_info_gen2,
384 385
	}, {
		.compatible = "renesas,gpio-rcar",
386
		.data = &gpio_rcar_info_gen1,
387 388 389 390 391 392 393
	}, {
		/* Terminator */
	},
};

MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);

394
static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins)
395 396
{
	struct device_node *np = p->pdev->dev.of_node;
397
	const struct gpio_rcar_info *info;
398 399 400
	struct of_phandle_args args;
	int ret;

401
	info = of_device_get_match_data(&p->pdev->dev);
402

403 404 405
	ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args);
	*npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK;
	p->has_both_edge_trigger = info->has_both_edge_trigger;
406
	p->needs_clk = info->needs_clk;
407

408
	if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) {
409
		dev_warn(&p->pdev->dev,
410 411 412
			 "Invalid number of gpio lines %u, using %u\n", *npins,
			 RCAR_MAX_GPIO_PER_BANK);
		*npins = RCAR_MAX_GPIO_PER_BANK;
413
	}
414 415

	return 0;
416 417
}

418 419 420 421 422 423
static int gpio_rcar_probe(struct platform_device *pdev)
{
	struct gpio_rcar_priv *p;
	struct resource *io, *irq;
	struct gpio_chip *gpio_chip;
	struct irq_chip *irq_chip;
424 425
	struct device *dev = &pdev->dev;
	const char *name = dev_name(dev);
426
	unsigned int npins;
427 428
	int ret;

429
	p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
430 431
	if (!p)
		return -ENOMEM;
432 433 434 435

	p->pdev = pdev;
	spin_lock_init(&p->lock);

436 437
	/* Get device configuration from DT node */
	ret = gpio_rcar_parse_dt(p, &npins);
438 439
	if (ret < 0)
		return ret;
440 441 442

	platform_set_drvdata(pdev, p);

443 444
	p->clk = devm_clk_get(dev, NULL);
	if (IS_ERR(p->clk)) {
445 446 447 448 449
		if (p->needs_clk) {
			dev_err(dev, "unable to get clock\n");
			ret = PTR_ERR(p->clk);
			goto err0;
		}
450 451 452
		p->clk = NULL;
	}

453 454
	pm_runtime_enable(dev);

455
	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
456 457
	if (!irq) {
		dev_err(dev, "missing IRQ\n");
458 459 460 461
		ret = -EINVAL;
		goto err0;
	}

462 463 464 465
	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	p->base = devm_ioremap_resource(dev, io);
	if (IS_ERR(p->base)) {
		ret = PTR_ERR(p->base);
466 467 468 469
		goto err0;
	}

	gpio_chip = &p->gpio_chip;
470 471
	gpio_chip->request = gpio_rcar_request;
	gpio_chip->free = gpio_rcar_free;
472 473 474 475
	gpio_chip->direction_input = gpio_rcar_direction_input;
	gpio_chip->get = gpio_rcar_get;
	gpio_chip->direction_output = gpio_rcar_direction_output;
	gpio_chip->set = gpio_rcar_set;
476
	gpio_chip->set_multiple = gpio_rcar_set_multiple;
477
	gpio_chip->label = name;
478
	gpio_chip->parent = dev;
479
	gpio_chip->owner = THIS_MODULE;
480 481
	gpio_chip->base = -1;
	gpio_chip->ngpio = npins;
482 483 484

	irq_chip = &p->irq_chip;
	irq_chip->name = name;
485
	irq_chip->parent_device = dev;
486 487 488
	irq_chip->irq_mask = gpio_rcar_irq_disable;
	irq_chip->irq_unmask = gpio_rcar_irq_enable;
	irq_chip->irq_set_type = gpio_rcar_irq_set_type;
489 490
	irq_chip->irq_set_wake = gpio_rcar_irq_set_wake;
	irq_chip->flags	= IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
491

492
	ret = gpiochip_add_data(gpio_chip, p);
493 494
	if (ret) {
		dev_err(dev, "failed to add GPIO controller\n");
495
		goto err0;
496 497
	}

498 499
	ret = gpiochip_irqchip_add(gpio_chip, irq_chip, 0, handle_level_irq,
				   IRQ_TYPE_NONE);
500 501 502 503 504
	if (ret) {
		dev_err(dev, "cannot add irqchip\n");
		goto err1;
	}

505
	p->irq_parent = irq->start;
506 507 508
	if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
			     IRQF_SHARED, name, p)) {
		dev_err(dev, "failed to request IRQ\n");
509 510 511 512
		ret = -ENOENT;
		goto err1;
	}

513
	dev_info(dev, "driving %d GPIOs\n", npins);
514

515 516 517
	return 0;

err1:
518
	gpiochip_remove(gpio_chip);
519
err0:
520
	pm_runtime_disable(dev);
521 522 523 524 525 526 527
	return ret;
}

static int gpio_rcar_remove(struct platform_device *pdev)
{
	struct gpio_rcar_priv *p = platform_get_drvdata(pdev);

528
	gpiochip_remove(&p->gpio_chip);
529

530
	pm_runtime_disable(&pdev->dev);
531 532 533 534 535 536 537 538
	return 0;
}

static struct platform_driver gpio_rcar_device_driver = {
	.probe		= gpio_rcar_probe,
	.remove		= gpio_rcar_remove,
	.driver		= {
		.name	= "gpio_rcar",
539
		.of_match_table = of_match_ptr(gpio_rcar_of_table),
540 541 542 543 544 545 546 547
	}
};

module_platform_driver(gpio_rcar_device_driver);

MODULE_AUTHOR("Magnus Damm");
MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
MODULE_LICENSE("GPL v2");