gpio-rcar.c 12.8 KB
Newer Older
M
Magnus Damm 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Renesas R-Car GPIO Support
 *
 *  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.
 */

#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/irqdomain.h>
#include <linux/module.h>
25
#include <linux/of.h>
26
#include <linux/pinctrl/consumer.h>
M
Magnus Damm 已提交
27 28
#include <linux/platform_data/gpio-rcar.h>
#include <linux/platform_device.h>
29
#include <linux/pm_runtime.h>
M
Magnus Damm 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <linux/spinlock.h>
#include <linux/slab.h>

struct gpio_rcar_priv {
	void __iomem *base;
	spinlock_t lock;
	struct gpio_rcar_config config;
	struct platform_device *pdev;
	struct gpio_chip gpio_chip;
	struct irq_chip irq_chip;
	struct irq_domain *irq_domain;
};

#define IOINTSEL 0x00
#define INOUTSEL 0x04
#define OUTDT 0x08
#define INDT 0x0c
#define INTDT 0x10
#define INTCLR 0x14
#define INTMSK 0x18
#define MSKCLR 0x1c
#define POSNEG 0x20
#define EDGLEVEL 0x24
#define FILONOFF 0x28
54
#define BOTHEDGE 0x4c
M
Magnus Damm 已提交
55

L
Laurent Pinchart 已提交
56 57
#define RCAR_MAX_GPIO_PER_BANK		32

M
Magnus Damm 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
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)
{
	struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);

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

static void gpio_rcar_irq_enable(struct irq_data *d)
{
	struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);

	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,
99 100
						  bool level_trigger,
						  bool both)
M
Magnus Damm 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
{
	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);

117 118 119 120
	/* Select one edge or both edges in BOTHEDGE */
	if (p->config.has_both_edge_trigger)
		gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);

M
Magnus Damm 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	/* 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)
{
	struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
	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:
140 141
		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
						      false);
M
Magnus Damm 已提交
142 143
		break;
	case IRQ_TYPE_LEVEL_LOW:
144 145
		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
						      false);
M
Magnus Damm 已提交
146 147
		break;
	case IRQ_TYPE_EDGE_RISING:
148 149
		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
						      false);
M
Magnus Damm 已提交
150 151
		break;
	case IRQ_TYPE_EDGE_FALLING:
152 153 154 155 156 157 158 159
		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
						      false);
		break;
	case IRQ_TYPE_EDGE_BOTH:
		if (!p->config.has_both_edge_trigger)
			return -EINVAL;
		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
						      true);
M
Magnus Damm 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

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;

173 174
	while ((pending = gpio_rcar_read(p, INTDT) &
			  gpio_rcar_read(p, INTMSK))) {
M
Magnus Damm 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
		offset = __ffs(pending);
		gpio_rcar_write(p, INTCLR, BIT(offset));
		generic_handle_irq(irq_find_mapping(p->irq_domain, offset));
		irqs_handled++;
	}

	return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
}

static inline struct gpio_rcar_priv *gpio_to_priv(struct gpio_chip *chip)
{
	return container_of(chip, struct gpio_rcar_priv, gpio_chip);
}

static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
						       unsigned int gpio,
						       bool output)
{
	struct gpio_rcar_priv *p = gpio_to_priv(chip);
	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);
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
{
	return pinctrl_request_gpio(chip->base + offset);
}

static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
{
	pinctrl_free_gpio(chip->base + offset);

	/* Set the GPIO as an input to ensure that the next GPIO request won't
	 * drive the GPIO pin as an output.
	 */
	gpio_rcar_config_general_input_output_mode(chip, offset, false);
}

M
Magnus Damm 已提交
230 231 232 233 234 235 236 237
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)
{
238 239 240 241 242
	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 */
	if (gpio_rcar_read(gpio_to_priv(chip), INOUTSEL) & bit)
243
		return !!(gpio_rcar_read(gpio_to_priv(chip), OUTDT) & bit);
244
	else
245
		return !!(gpio_rcar_read(gpio_to_priv(chip), INDT) & bit);
M
Magnus Damm 已提交
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
}

static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
{
	struct gpio_rcar_priv *p = gpio_to_priv(chip);
	unsigned long flags;

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

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;
}

static int gpio_rcar_to_irq(struct gpio_chip *chip, unsigned offset)
{
	return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset);
}

272 273
static int gpio_rcar_irq_domain_map(struct irq_domain *h, unsigned int irq,
				 irq_hw_number_t hwirq)
M
Magnus Damm 已提交
274 275 276
{
	struct gpio_rcar_priv *p = h->host_data;

277
	dev_dbg(&p->pdev->dev, "map hw irq = %d, irq = %d\n", (int)hwirq, irq);
M
Magnus Damm 已提交
278

279 280 281
	irq_set_chip_data(irq, h->host_data);
	irq_set_chip_and_handler(irq, &p->irq_chip, handle_level_irq);
	set_irq_flags(irq, IRQF_VALID); /* kill me now */
M
Magnus Damm 已提交
282 283 284 285 286 287 288
	return 0;
}

static struct irq_domain_ops gpio_rcar_irq_domain_ops = {
	.map	= gpio_rcar_irq_domain_map,
};

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
struct gpio_rcar_info {
	bool has_both_edge_trigger;
};

static const struct of_device_id gpio_rcar_of_table[] = {
	{
		.compatible = "renesas,gpio-r8a7790",
		.data = (void *)&(const struct gpio_rcar_info) {
			.has_both_edge_trigger = true,
		},
	}, {
		.compatible = "renesas,gpio-r8a7791",
		.data = (void *)&(const struct gpio_rcar_info) {
			.has_both_edge_trigger = true,
		},
	}, {
		.compatible = "renesas,gpio-rcar",
		.data = (void *)&(const struct gpio_rcar_info) {
			.has_both_edge_trigger = false,
		},
	}, {
		/* Terminator */
	},
};

MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);

static int gpio_rcar_parse_pdata(struct gpio_rcar_priv *p)
L
Laurent Pinchart 已提交
317
{
J
Jingoo Han 已提交
318
	struct gpio_rcar_config *pdata = dev_get_platdata(&p->pdev->dev);
L
Laurent Pinchart 已提交
319 320 321 322
	struct device_node *np = p->pdev->dev.of_node;
	struct of_phandle_args args;
	int ret;

323
	if (pdata) {
L
Laurent Pinchart 已提交
324
		p->config = *pdata;
325
	} else if (IS_ENABLED(CONFIG_OF) && np) {
326 327 328 329 330 331 332 333 334
		const struct of_device_id *match;
		const struct gpio_rcar_info *info;

		match = of_match_node(gpio_rcar_of_table, np);
		if (!match)
			return -EINVAL;

		info = match->data;

335 336 337
		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0,
						       &args);
		p->config.number_of_pins = ret == 0 ? args.args[2]
L
Laurent Pinchart 已提交
338 339
					 : RCAR_MAX_GPIO_PER_BANK;
		p->config.gpio_base = -1;
340
		p->config.has_both_edge_trigger = info->has_both_edge_trigger;
L
Laurent Pinchart 已提交
341 342 343 344 345 346 347 348 349
	}

	if (p->config.number_of_pins == 0 ||
	    p->config.number_of_pins > RCAR_MAX_GPIO_PER_BANK) {
		dev_warn(&p->pdev->dev,
			 "Invalid number of gpio lines %u, using %u\n",
			 p->config.number_of_pins, RCAR_MAX_GPIO_PER_BANK);
		p->config.number_of_pins = RCAR_MAX_GPIO_PER_BANK;
	}
350 351

	return 0;
L
Laurent Pinchart 已提交
352 353
}

M
Magnus Damm 已提交
354 355 356 357 358 359
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;
360 361
	struct device *dev = &pdev->dev;
	const char *name = dev_name(dev);
M
Magnus Damm 已提交
362 363
	int ret;

364
	p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
M
Magnus Damm 已提交
365 366 367 368 369 370 371 372
	if (!p) {
		ret = -ENOMEM;
		goto err0;
	}

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

L
Laurent Pinchart 已提交
373
	/* Get device configuration from DT node or platform data. */
374 375 376
	ret = gpio_rcar_parse_pdata(p);
	if (ret < 0)
		return ret;
L
Laurent Pinchart 已提交
377 378 379

	platform_set_drvdata(pdev, p);

380 381 382
	pm_runtime_enable(dev);
	pm_runtime_get_sync(dev);

M
Magnus Damm 已提交
383 384 385 386
	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);

	if (!io || !irq) {
387
		dev_err(dev, "missing IRQ or IOMEM\n");
M
Magnus Damm 已提交
388 389 390 391
		ret = -EINVAL;
		goto err0;
	}

392
	p->base = devm_ioremap_nocache(dev, io->start, resource_size(io));
M
Magnus Damm 已提交
393
	if (!p->base) {
394
		dev_err(dev, "failed to remap I/O memory\n");
M
Magnus Damm 已提交
395 396 397 398 399
		ret = -ENXIO;
		goto err0;
	}

	gpio_chip = &p->gpio_chip;
400 401
	gpio_chip->request = gpio_rcar_request;
	gpio_chip->free = gpio_rcar_free;
M
Magnus Damm 已提交
402 403 404 405 406 407
	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;
	gpio_chip->to_irq = gpio_rcar_to_irq;
	gpio_chip->label = name;
408
	gpio_chip->dev = dev;
M
Magnus Damm 已提交
409 410 411 412 413 414 415 416 417
	gpio_chip->owner = THIS_MODULE;
	gpio_chip->base = p->config.gpio_base;
	gpio_chip->ngpio = p->config.number_of_pins;

	irq_chip = &p->irq_chip;
	irq_chip->name = name;
	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;
M
Magnus Damm 已提交
418 419
	irq_chip->flags	= IRQCHIP_SKIP_SET_WAKE | IRQCHIP_SET_TYPE_MASKED
			 | IRQCHIP_MASK_ON_SUSPEND;
M
Magnus Damm 已提交
420 421 422 423 424 425 426

	p->irq_domain = irq_domain_add_simple(pdev->dev.of_node,
					      p->config.number_of_pins,
					      p->config.irq_base,
					      &gpio_rcar_irq_domain_ops, p);
	if (!p->irq_domain) {
		ret = -ENXIO;
427
		dev_err(dev, "cannot initialize irq domain\n");
428
		goto err0;
M
Magnus Damm 已提交
429 430
	}

431 432 433
	if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
			     IRQF_SHARED, name, p)) {
		dev_err(dev, "failed to request IRQ\n");
M
Magnus Damm 已提交
434 435 436 437 438 439
		ret = -ENOENT;
		goto err1;
	}

	ret = gpiochip_add(gpio_chip);
	if (ret) {
440
		dev_err(dev, "failed to add GPIO controller\n");
M
Magnus Damm 已提交
441 442 443
		goto err1;
	}

444
	dev_info(dev, "driving %d GPIOs\n", p->config.number_of_pins);
M
Magnus Damm 已提交
445 446 447 448 449

	/* warn in case of mismatch if irq base is specified */
	if (p->config.irq_base) {
		ret = irq_find_mapping(p->irq_domain, 0);
		if (p->config.irq_base != ret)
450
			dev_warn(dev, "irq base mismatch (%u/%u)\n",
M
Magnus Damm 已提交
451 452 453
				 p->config.irq_base, ret);
	}

L
Laurent Pinchart 已提交
454 455 456 457
	if (p->config.pctl_name) {
		ret = gpiochip_add_pin_range(gpio_chip, p->config.pctl_name, 0,
					     gpio_chip->base, gpio_chip->ngpio);
		if (ret < 0)
458
			dev_warn(dev, "failed to add pin range\n");
L
Laurent Pinchart 已提交
459
	}
460

M
Magnus Damm 已提交
461 462 463 464 465
	return 0;

err1:
	irq_domain_remove(p->irq_domain);
err0:
466 467
	pm_runtime_put(dev);
	pm_runtime_disable(dev);
M
Magnus Damm 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480
	return ret;
}

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

	ret = gpiochip_remove(&p->gpio_chip);
	if (ret)
		return ret;

	irq_domain_remove(p->irq_domain);
481 482
	pm_runtime_put(&pdev->dev);
	pm_runtime_disable(&pdev->dev);
M
Magnus Damm 已提交
483 484 485 486 487 488 489 490
	return 0;
}

static struct platform_driver gpio_rcar_device_driver = {
	.probe		= gpio_rcar_probe,
	.remove		= gpio_rcar_remove,
	.driver		= {
		.name	= "gpio_rcar",
L
Laurent Pinchart 已提交
491
		.of_match_table = of_match_ptr(gpio_rcar_of_table),
M
Magnus Damm 已提交
492 493 494 495 496 497 498 499
	}
};

module_platform_driver(gpio_rcar_device_driver);

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