gpio-em.c 11.4 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 25 26 27 28 29 30 31 32
/*
 * Emma Mobile GPIO Support - GIO
 *
 *  Copyright (C) 2012 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/irqdomain.h>
#include <linux/bitops.h>
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/module.h>
M
Magnus Damm 已提交
33
#include <linux/pinctrl/consumer.h>
M
Magnus Damm 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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
#include <linux/platform_data/gpio-em.h>

struct em_gio_priv {
	void __iomem *base0;
	void __iomem *base1;
	spinlock_t sense_lock;
	struct platform_device *pdev;
	struct gpio_chip gpio_chip;
	struct irq_chip irq_chip;
	struct irq_domain *irq_domain;
};

#define GIO_E1 0x00
#define GIO_E0 0x04
#define GIO_EM 0x04
#define GIO_OL 0x08
#define GIO_OH 0x0c
#define GIO_I 0x10
#define GIO_IIA 0x14
#define GIO_IEN 0x18
#define GIO_IDS 0x1c
#define GIO_IIM 0x1c
#define GIO_RAW 0x20
#define GIO_MST 0x24
#define GIO_IIR 0x28

#define GIO_IDT0 0x40
#define GIO_IDT1 0x44
#define GIO_IDT2 0x48
#define GIO_IDT3 0x4c
#define GIO_RAWBL 0x50
#define GIO_RAWBH 0x54
#define GIO_IRBL 0x58
#define GIO_IRBH 0x5c

#define GIO_IDT(n) (GIO_IDT0 + ((n) * 4))

static inline unsigned long em_gio_read(struct em_gio_priv *p, int offs)
{
	if (offs < GIO_IDT0)
		return ioread32(p->base0 + offs);
	else
		return ioread32(p->base1 + (offs - GIO_IDT0));
}

static inline void em_gio_write(struct em_gio_priv *p, int offs,
				unsigned long value)
{
	if (offs < GIO_IDT0)
		iowrite32(value, p->base0 + offs);
	else
		iowrite32(value, p->base1 + (offs - GIO_IDT0));
}

static void em_gio_irq_disable(struct irq_data *d)
{
90
	struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
M
Magnus Damm 已提交
91 92 93 94 95 96

	em_gio_write(p, GIO_IDS, BIT(irqd_to_hwirq(d)));
}

static void em_gio_irq_enable(struct irq_data *d)
{
97
	struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
M
Magnus Damm 已提交
98 99 100 101

	em_gio_write(p, GIO_IEN, BIT(irqd_to_hwirq(d)));
}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
static unsigned int em_gio_irq_startup(struct irq_data *d)
{
	struct em_gio_priv *p = irq_data_get_irq_chip_data(d);

	if (gpio_lock_as_irq(&p->gpio_chip, irqd_to_hwirq(d)))
		dev_err(p->gpio_chip.dev,
			"unable to lock HW IRQ %lu for IRQ\n",
			irqd_to_hwirq(d));
	em_gio_irq_enable(d);
	return 0;
}

static void em_gio_irq_shutdown(struct irq_data *d)
{
	struct em_gio_priv *p = irq_data_get_irq_chip_data(d);

	em_gio_irq_disable(d);
	gpio_unlock_as_irq(&p->gpio_chip, irqd_to_hwirq(d));
}


M
Magnus Damm 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135
#define GIO_ASYNC(x) (x + 8)

static unsigned char em_gio_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
	[IRQ_TYPE_EDGE_RISING] = GIO_ASYNC(0x00),
	[IRQ_TYPE_EDGE_FALLING] = GIO_ASYNC(0x01),
	[IRQ_TYPE_LEVEL_HIGH] = GIO_ASYNC(0x02),
	[IRQ_TYPE_LEVEL_LOW] = GIO_ASYNC(0x03),
	[IRQ_TYPE_EDGE_BOTH] = GIO_ASYNC(0x04),
};

static int em_gio_irq_set_type(struct irq_data *d, unsigned int type)
{
	unsigned char value = em_gio_sense_table[type & IRQ_TYPE_SENSE_MASK];
136
	struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
M
Magnus Damm 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
	unsigned int reg, offset, shift;
	unsigned long flags;
	unsigned long tmp;

	if (!value)
		return -EINVAL;

	offset = irqd_to_hwirq(d);

	pr_debug("gio: sense irq = %d, mode = %d\n", offset, value);

	/* 8 x 4 bit fields in 4 IDT registers */
	reg = GIO_IDT(offset >> 3);
	shift = (offset & 0x07) << 4;

	spin_lock_irqsave(&p->sense_lock, flags);

	/* disable the interrupt in IIA */
	tmp = em_gio_read(p, GIO_IIA);
	tmp &= ~BIT(offset);
	em_gio_write(p, GIO_IIA, tmp);

	/* change the sense setting in IDT */
	tmp = em_gio_read(p, reg);
	tmp &= ~(0xf << shift);
	tmp |= value << shift;
	em_gio_write(p, reg, tmp);

	/* clear pending interrupts */
	em_gio_write(p, GIO_IIR, BIT(offset));

	/* enable the interrupt in IIA */
	tmp = em_gio_read(p, GIO_IIA);
	tmp |= BIT(offset);
	em_gio_write(p, GIO_IIA, tmp);

	spin_unlock_irqrestore(&p->sense_lock, flags);

	return 0;
}

static irqreturn_t em_gio_irq_handler(int irq, void *dev_id)
{
	struct em_gio_priv *p = dev_id;
	unsigned long pending;
	unsigned int offset, irqs_handled = 0;

	while ((pending = em_gio_read(p, GIO_MST))) {
		offset = __ffs(pending);
		em_gio_write(p, GIO_IIR, BIT(offset));
		generic_handle_irq(irq_find_mapping(p->irq_domain, offset));
		irqs_handled++;
	}

	return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
}

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

static int em_gio_direction_input(struct gpio_chip *chip, unsigned offset)
{
	em_gio_write(gpio_to_priv(chip), GIO_E0, BIT(offset));
	return 0;
}

static int em_gio_get(struct gpio_chip *chip, unsigned offset)
{
	return (int)(em_gio_read(gpio_to_priv(chip), GIO_I) & BIT(offset));
}

static void __em_gio_set(struct gpio_chip *chip, unsigned int reg,
			 unsigned shift, int value)
{
	/* upper 16 bits contains mask and lower 16 actual value */
	em_gio_write(gpio_to_priv(chip), reg,
		     (1 << (shift + 16)) | (value << shift));
}

static void em_gio_set(struct gpio_chip *chip, unsigned offset, int value)
{
	/* output is split into two registers */
	if (offset < 16)
		__em_gio_set(chip, GIO_OL, offset, value);
	else
		__em_gio_set(chip, GIO_OH, offset - 16, value);
}

static int em_gio_direction_output(struct gpio_chip *chip, unsigned offset,
				   int value)
{
	/* write GPIO value to output before selecting output mode of pin */
	em_gio_set(chip, offset, value);
	em_gio_write(gpio_to_priv(chip), GIO_E1, BIT(offset));
	return 0;
}

static int em_gio_to_irq(struct gpio_chip *chip, unsigned offset)
{
238
	return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset);
M
Magnus Damm 已提交
239 240
}

M
Magnus Damm 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
static int em_gio_request(struct gpio_chip *chip, unsigned offset)
{
	return pinctrl_request_gpio(chip->base + offset);
}

static void em_gio_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.
	*/
	em_gio_direction_input(chip, offset);
}

256 257
static int em_gio_irq_domain_map(struct irq_domain *h, unsigned int irq,
				 irq_hw_number_t hwirq)
M
Magnus Damm 已提交
258 259 260
{
	struct em_gio_priv *p = h->host_data;

261
	pr_debug("gio: map hw irq = %d, irq = %d\n", (int)hwirq, irq);
M
Magnus Damm 已提交
262

263 264 265
	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 已提交
266 267 268 269 270
	return 0;
}

static struct irq_domain_ops em_gio_irq_domain_ops = {
	.map	= em_gio_irq_domain_map,
M
Magnus Damm 已提交
271
	.xlate	= irq_domain_xlate_twocell,
M
Magnus Damm 已提交
272 273
};

B
Bill Pemberton 已提交
274
static int em_gio_probe(struct platform_device *pdev)
M
Magnus Damm 已提交
275
{
M
Magnus Damm 已提交
276
	struct gpio_em_config pdata_dt;
J
Jingoo Han 已提交
277
	struct gpio_em_config *pdata = dev_get_platdata(&pdev->dev);
M
Magnus Damm 已提交
278 279 280 281 282 283 284
	struct em_gio_priv *p;
	struct resource *io[2], *irq[2];
	struct gpio_chip *gpio_chip;
	struct irq_chip *irq_chip;
	const char *name = dev_name(&pdev->dev);
	int ret;

285
	p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
M
Magnus Damm 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
	if (!p) {
		dev_err(&pdev->dev, "failed to allocate driver data\n");
		ret = -ENOMEM;
		goto err0;
	}

	p->pdev = pdev;
	platform_set_drvdata(pdev, p);
	spin_lock_init(&p->sense_lock);

	io[0] = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	io[1] = platform_get_resource(pdev, IORESOURCE_MEM, 1);
	irq[0] = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	irq[1] = platform_get_resource(pdev, IORESOURCE_IRQ, 1);

M
Magnus Damm 已提交
301 302
	if (!io[0] || !io[1] || !irq[0] || !irq[1]) {
		dev_err(&pdev->dev, "missing IRQ or IOMEM\n");
M
Magnus Damm 已提交
303
		ret = -EINVAL;
304
		goto err0;
M
Magnus Damm 已提交
305 306
	}

307 308
	p->base0 = devm_ioremap_nocache(&pdev->dev, io[0]->start,
					resource_size(io[0]));
M
Magnus Damm 已提交
309 310 311
	if (!p->base0) {
		dev_err(&pdev->dev, "failed to remap low I/O memory\n");
		ret = -ENXIO;
312
		goto err0;
M
Magnus Damm 已提交
313 314
	}

315 316
	p->base1 = devm_ioremap_nocache(&pdev->dev, io[1]->start,
				   resource_size(io[1]));
M
Magnus Damm 已提交
317 318 319
	if (!p->base1) {
		dev_err(&pdev->dev, "failed to remap high I/O memory\n");
		ret = -ENXIO;
320
		goto err0;
M
Magnus Damm 已提交
321 322
	}

M
Magnus Damm 已提交
323 324 325 326 327 328 329 330
	if (!pdata) {
		memset(&pdata_dt, 0, sizeof(pdata_dt));
		pdata = &pdata_dt;

		if (of_property_read_u32(pdev->dev.of_node, "ngpios",
					 &pdata->number_of_pins)) {
			dev_err(&pdev->dev, "Missing ngpios OF property\n");
			ret = -EINVAL;
331
			goto err0;
M
Magnus Damm 已提交
332 333 334 335 336
		}

		ret = of_alias_get_id(pdev->dev.of_node, "gpio");
		if (ret < 0) {
			dev_err(&pdev->dev, "Couldn't get OF id\n");
337
			goto err0;
M
Magnus Damm 已提交
338 339 340 341
		}
		pdata->gpio_base = ret * 32; /* 32 GPIOs per instance */
	}

M
Magnus Damm 已提交
342
	gpio_chip = &p->gpio_chip;
343
	gpio_chip->of_node = pdev->dev.of_node;
M
Magnus Damm 已提交
344 345 346 347 348
	gpio_chip->direction_input = em_gio_direction_input;
	gpio_chip->get = em_gio_get;
	gpio_chip->direction_output = em_gio_direction_output;
	gpio_chip->set = em_gio_set;
	gpio_chip->to_irq = em_gio_to_irq;
M
Magnus Damm 已提交
349 350
	gpio_chip->request = em_gio_request;
	gpio_chip->free = em_gio_free;
M
Magnus Damm 已提交
351
	gpio_chip->label = name;
M
Magnus Damm 已提交
352
	gpio_chip->dev = &pdev->dev;
M
Magnus Damm 已提交
353 354 355 356 357 358 359 360 361
	gpio_chip->owner = THIS_MODULE;
	gpio_chip->base = pdata->gpio_base;
	gpio_chip->ngpio = pdata->number_of_pins;

	irq_chip = &p->irq_chip;
	irq_chip->name = name;
	irq_chip->irq_mask = em_gio_irq_disable;
	irq_chip->irq_unmask = em_gio_irq_enable;
	irq_chip->irq_set_type = em_gio_irq_set_type;
362 363
	irq_chip->irq_startup = em_gio_irq_startup;
	irq_chip->irq_shutdown = em_gio_irq_shutdown;
M
Magnus Damm 已提交
364
	irq_chip->flags	= IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MASK_ON_SUSPEND;
M
Magnus Damm 已提交
365

366
	p->irq_domain = irq_domain_add_simple(pdev->dev.of_node,
367
					      pdata->number_of_pins,
368
					      pdata->irq_base,
369
					      &em_gio_irq_domain_ops, p);
A
Axel Lin 已提交
370 371
	if (!p->irq_domain) {
		ret = -ENXIO;
M
Magnus Damm 已提交
372
		dev_err(&pdev->dev, "cannot initialize irq domain\n");
373
		goto err0;
M
Magnus Damm 已提交
374 375
	}

376 377
	if (devm_request_irq(&pdev->dev, irq[0]->start,
			     em_gio_irq_handler, 0, name, p)) {
M
Magnus Damm 已提交
378 379
		dev_err(&pdev->dev, "failed to request low IRQ\n");
		ret = -ENOENT;
380
		goto err1;
M
Magnus Damm 已提交
381 382
	}

383 384
	if (devm_request_irq(&pdev->dev, irq[1]->start,
			     em_gio_irq_handler, 0, name, p)) {
M
Magnus Damm 已提交
385 386
		dev_err(&pdev->dev, "failed to request high IRQ\n");
		ret = -ENOENT;
387
		goto err1;
M
Magnus Damm 已提交
388 389 390 391 392
	}

	ret = gpiochip_add(gpio_chip);
	if (ret) {
		dev_err(&pdev->dev, "failed to add GPIO controller\n");
393
		goto err1;
M
Magnus Damm 已提交
394
	}
M
Magnus Damm 已提交
395 396 397 398 399 400 401

	if (pdata->pctl_name) {
		ret = gpiochip_add_pin_range(gpio_chip, pdata->pctl_name, 0,
					     gpio_chip->base, gpio_chip->ngpio);
		if (ret < 0)
			dev_warn(&pdev->dev, "failed to add pin range\n");
	}
M
Magnus Damm 已提交
402 403 404
	return 0;

err1:
405
	irq_domain_remove(p->irq_domain);
M
Magnus Damm 已提交
406 407 408 409
err0:
	return ret;
}

B
Bill Pemberton 已提交
410
static int em_gio_remove(struct platform_device *pdev)
M
Magnus Damm 已提交
411 412 413 414 415 416 417 418
{
	struct em_gio_priv *p = platform_get_drvdata(pdev);
	int ret;

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

A
Axel Lin 已提交
419
	irq_domain_remove(p->irq_domain);
M
Magnus Damm 已提交
420 421 422
	return 0;
}

M
Magnus Damm 已提交
423 424 425 426 427 428
static const struct of_device_id em_gio_dt_ids[] = {
	{ .compatible = "renesas,em-gio", },
	{},
};
MODULE_DEVICE_TABLE(of, em_gio_dt_ids);

M
Magnus Damm 已提交
429 430
static struct platform_driver em_gio_device_driver = {
	.probe		= em_gio_probe,
B
Bill Pemberton 已提交
431
	.remove		= em_gio_remove,
M
Magnus Damm 已提交
432 433
	.driver		= {
		.name	= "em_gio",
M
Magnus Damm 已提交
434 435
		.of_match_table = em_gio_dt_ids,
		.owner		= THIS_MODULE,
M
Magnus Damm 已提交
436 437 438
	}
};

M
Magnus Damm 已提交
439 440 441 442 443 444 445 446 447 448 449
static int __init em_gio_init(void)
{
	return platform_driver_register(&em_gio_device_driver);
}
postcore_initcall(em_gio_init);

static void __exit em_gio_exit(void)
{
	platform_driver_unregister(&em_gio_device_driver);
}
module_exit(em_gio_exit);
M
Magnus Damm 已提交
450 451 452 453

MODULE_AUTHOR("Magnus Damm");
MODULE_DESCRIPTION("Renesas Emma Mobile GIO Driver");
MODULE_LICENSE("GPL v2");