gpio-intel-mid.c 12.3 KB
Newer Older
G
Grant Likely 已提交
1
/*
D
David Cohen 已提交
2
 * Intel MID GPIO driver
G
Grant Likely 已提交
3
 *
D
David Cohen 已提交
4
 * Copyright (c) 2008-2014 Intel Corporation.
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 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.
 */

/* Supports:
 * Moorestown platform Langwell chip.
A
Alek Du 已提交
18
 * Medfield platform Penwell chip.
19 20
 * Clovertrail platform Cloverview chip.
 * Merrifield platform Tangier chip.
21 22 23 24
 */

#include <linux/module.h>
#include <linux/pci.h>
25
#include <linux/platform_device.h>
26 27 28 29 30 31 32 33
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/stddef.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/io.h>
#include <linux/gpio.h>
34
#include <linux/slab.h>
35
#include <linux/pm_runtime.h>
36
#include <linux/irqdomain.h>
37

38 39
#define INTEL_MID_IRQ_TYPE_EDGE		(1 << 0)
#define INTEL_MID_IRQ_TYPE_LEVEL	(1 << 1)
40

A
Alek Du 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
/*
 * Langwell chip has 64 pins and thus there are 2 32bit registers to control
 * each feature, while Penwell chip has 96 pins for each block, and need 3 32bit
 * registers to control them, so we only define the order here instead of a
 * structure, to get a bit offset for a pin (use GPDR as an example):
 *
 * nreg = ngpio / 32;
 * reg = offset / 32;
 * bit = offset % 32;
 * reg_addr = reg_base + GPDR * nreg * 4 + reg * 4;
 *
 * so the bit of reg_addr is to control pin offset's GPDR feature
*/

enum GPIO_REG {
	GPLR = 0,	/* pin level read-only */
	GPDR,		/* pin direction */
	GPSR,		/* pin set */
	GPCR,		/* pin clear */
	GRER,		/* rising edge detect */
	GFER,		/* falling edge detect */
	GEDR,		/* edge detect result */
63
	GAFR,		/* alt function */
64 65
};

66 67
/* intel_mid gpio driver data */
struct intel_mid_gpio_ddata {
68 69 70 71 72 73 74 75
	u16 ngpio;		/* number of gpio pins */
	u32 gplr_offset;	/* offset of first GPLR register from base */
	u32 flis_base;		/* base address of FLIS registers */
	u32 flis_len;		/* length of FLIS registers */
	u32 (*get_flis_offset)(int gpio);
	u32 chip_irq_type;	/* chip interrupt type */
};

76
struct intel_mid_gpio {
77
	struct gpio_chip		chip;
78
	void __iomem			*reg_base;
79
	spinlock_t			lock;
80
	struct pci_dev			*pdev;
81
	struct irq_domain		*domain;
82 83
};

84
#define to_intel_gpio_priv(chip) container_of(chip, struct intel_mid_gpio, chip)
D
David Cohen 已提交
85

A
Alek Du 已提交
86
static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned offset,
87
			      enum GPIO_REG reg_type)
88
{
89
	struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
A
Alek Du 已提交
90
	unsigned nreg = chip->ngpio / 32;
91
	u8 reg = offset / 32;
A
Alek Du 已提交
92

93
	return priv->reg_base + reg_type * nreg * 4 + reg * 4;
A
Alek Du 已提交
94 95
}

96 97 98
static void __iomem *gpio_reg_2bit(struct gpio_chip *chip, unsigned offset,
				   enum GPIO_REG reg_type)
{
99
	struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
100 101 102
	unsigned nreg = chip->ngpio / 32;
	u8 reg = offset / 16;

103
	return priv->reg_base + reg_type * nreg * 4 + reg * 4;
104 105
}

106
static int intel_gpio_request(struct gpio_chip *chip, unsigned offset)
107 108 109 110 111 112 113 114 115 116 117 118
{
	void __iomem *gafr = gpio_reg_2bit(chip, offset, GAFR);
	u32 value = readl(gafr);
	int shift = (offset % 16) << 1, af = (value >> shift) & 3;

	if (af) {
		value &= ~(3 << shift);
		writel(value, gafr);
	}
	return 0;
}

119
static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
A
Alek Du 已提交
120 121
{
	void __iomem *gplr = gpio_reg(chip, offset, GPLR);
122 123 124 125

	return readl(gplr) & BIT(offset % 32);
}

126
static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
127 128 129 130
{
	void __iomem *gpsr, *gpcr;

	if (value) {
A
Alek Du 已提交
131
		gpsr = gpio_reg(chip, offset, GPSR);
132 133
		writel(BIT(offset % 32), gpsr);
	} else {
A
Alek Du 已提交
134
		gpcr = gpio_reg(chip, offset, GPCR);
135 136 137 138
		writel(BIT(offset % 32), gpcr);
	}
}

139
static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
140
{
141
	struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
A
Alek Du 已提交
142
	void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
143 144 145
	u32 value;
	unsigned long flags;

146 147
	if (priv->pdev)
		pm_runtime_get(&priv->pdev->dev);
148

149
	spin_lock_irqsave(&priv->lock, flags);
150 151 152
	value = readl(gpdr);
	value &= ~BIT(offset % 32);
	writel(value, gpdr);
153
	spin_unlock_irqrestore(&priv->lock, flags);
154

155 156
	if (priv->pdev)
		pm_runtime_put(&priv->pdev->dev);
157

158 159 160
	return 0;
}

161
static int intel_gpio_direction_output(struct gpio_chip *chip,
162 163
			unsigned offset, int value)
{
164
	struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
A
Alek Du 已提交
165
	void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
166 167
	unsigned long flags;

168
	intel_gpio_set(chip, offset, value);
169

170 171
	if (priv->pdev)
		pm_runtime_get(&priv->pdev->dev);
172

173
	spin_lock_irqsave(&priv->lock, flags);
174
	value = readl(gpdr);
175
	value |= BIT(offset % 32);
176
	writel(value, gpdr);
177
	spin_unlock_irqrestore(&priv->lock, flags);
178

179 180
	if (priv->pdev)
		pm_runtime_put(&priv->pdev->dev);
181

182 183 184
	return 0;
}

185
static int intel_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
186
{
187 188
	struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
	return irq_create_mapping(priv->domain, offset);
189 190
}

191
static int intel_mid_irq_type(struct irq_data *d, unsigned type)
192
{
193
	struct intel_mid_gpio *priv = irq_data_get_irq_chip_data(d);
194
	u32 gpio = irqd_to_hwirq(d);
195 196
	unsigned long flags;
	u32 value;
197 198
	void __iomem *grer = gpio_reg(&priv->chip, gpio, GRER);
	void __iomem *gfer = gpio_reg(&priv->chip, gpio, GFER);
199

200
	if (gpio >= priv->chip.ngpio)
201
		return -EINVAL;
202

203 204
	if (priv->pdev)
		pm_runtime_get(&priv->pdev->dev);
205

206
	spin_lock_irqsave(&priv->lock, flags);
207 208 209 210 211 212 213 214 215 216 217
	if (type & IRQ_TYPE_EDGE_RISING)
		value = readl(grer) | BIT(gpio % 32);
	else
		value = readl(grer) & (~BIT(gpio % 32));
	writel(value, grer);

	if (type & IRQ_TYPE_EDGE_FALLING)
		value = readl(gfer) | BIT(gpio % 32);
	else
		value = readl(gfer) & (~BIT(gpio % 32));
	writel(value, gfer);
218
	spin_unlock_irqrestore(&priv->lock, flags);
219

220 221
	if (priv->pdev)
		pm_runtime_put(&priv->pdev->dev);
222

223
	return 0;
224
}
225

226
static void intel_mid_irq_unmask(struct irq_data *d)
227
{
228
}
229

230
static void intel_mid_irq_mask(struct irq_data *d)
231
{
232
}
233

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
static unsigned int intel_mid_irq_startup(struct irq_data *d)
{
	struct intel_mid_gpio *priv = irq_data_get_irq_chip_data(d);

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

static void intel_mid_irq_shutdown(struct irq_data *d)
{
	struct intel_mid_gpio *priv = irq_data_get_irq_chip_data(d);

	intel_mid_irq_mask(d);
	gpio_unlock_as_irq(&priv->chip, irqd_to_hwirq(d));
}

254 255 256 257 258
static struct irq_chip intel_mid_irqchip = {
	.name		= "INTEL_MID-GPIO",
	.irq_mask	= intel_mid_irq_mask,
	.irq_unmask	= intel_mid_irq_unmask,
	.irq_set_type	= intel_mid_irq_type,
259 260
	.irq_startup	= intel_mid_irq_startup,
	.irq_shutdown	= intel_mid_irq_shutdown,
261 262
};

263
static const struct intel_mid_gpio_ddata gpio_lincroft = {
264 265 266
	.ngpio = 64,
};

267
static const struct intel_mid_gpio_ddata gpio_penwell_aon = {
268
	.ngpio = 96,
269
	.chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
270 271
};

272
static const struct intel_mid_gpio_ddata gpio_penwell_core = {
273
	.ngpio = 96,
274
	.chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
275 276
};

277
static const struct intel_mid_gpio_ddata gpio_cloverview_aon = {
278
	.ngpio = 96,
279
	.chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE | INTEL_MID_IRQ_TYPE_LEVEL,
280 281
};

282
static const struct intel_mid_gpio_ddata gpio_cloverview_core = {
283
	.ngpio = 96,
284
	.chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
285 286
};

287
static const struct intel_mid_gpio_ddata gpio_tangier = {
288 289 290 291 292
	.ngpio = 192,
	.gplr_offset = 4,
	.flis_base = 0xff0c0000,
	.flis_len = 0x8000,
	.get_flis_offset = NULL,
293
	.chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
294 295
};

296
static const struct pci_device_id intel_gpio_ids[] = {
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
	{
		/* Lincroft */
		PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f),
		.driver_data = (kernel_ulong_t)&gpio_lincroft,
	},
	{
		/* Penwell AON */
		PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081f),
		.driver_data = (kernel_ulong_t)&gpio_penwell_aon,
	},
	{
		/* Penwell Core */
		PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081a),
		.driver_data = (kernel_ulong_t)&gpio_penwell_core,
	},
	{
		/* Cloverview Aon */
		PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08eb),
		.driver_data = (kernel_ulong_t)&gpio_cloverview_aon,
	},
	{
		/* Cloverview Core */
		PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08f7),
		.driver_data = (kernel_ulong_t)&gpio_cloverview_core,
	},
	{
		/* Tangier */
		PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x1199),
		.driver_data = (kernel_ulong_t)&gpio_tangier,
	},
	{ 0 }
328
};
329
MODULE_DEVICE_TABLE(pci, intel_gpio_ids);
330

331
static void intel_mid_irq_handler(unsigned irq, struct irq_desc *desc)
332
{
333
	struct irq_data *data = irq_desc_get_irq_data(desc);
334
	struct intel_mid_gpio *priv = irq_data_get_irq_handler_data(data);
335
	struct irq_chip *chip = irq_data_get_irq_chip(data);
336
	u32 base, gpio, mask;
337
	unsigned long pending;
338 339 340
	void __iomem *gedr;

	/* check GPIO controller to check which pin triggered the interrupt */
341 342
	for (base = 0; base < priv->chip.ngpio; base += 32) {
		gedr = gpio_reg(&priv->chip, base, GEDR);
343
		while ((pending = readl(gedr))) {
344
			gpio = __ffs(pending);
345 346 347
			mask = BIT(gpio);
			/* Clear before handling so we can't lose an edge */
			writel(mask, gedr);
348
			generic_handle_irq(irq_find_mapping(priv->domain,
349
							    base + gpio));
350
		}
351
	}
352

353
	chip->irq_eoi(data);
354 355
}

356
static void intel_mid_irq_init_hw(struct intel_mid_gpio *priv)
357 358 359 360
{
	void __iomem *reg;
	unsigned base;

361
	for (base = 0; base < priv->chip.ngpio; base += 32) {
362
		/* Clear the rising-edge detect register */
363
		reg = gpio_reg(&priv->chip, base, GRER);
364 365
		writel(0, reg);
		/* Clear the falling-edge detect register */
366
		reg = gpio_reg(&priv->chip, base, GFER);
367 368
		writel(0, reg);
		/* Clear the edge detect status register */
369
		reg = gpio_reg(&priv->chip, base, GEDR);
370 371 372 373
		writel(~0, reg);
	}
}

374 375
static int intel_gpio_irq_map(struct irq_domain *d, unsigned int irq,
			    irq_hw_number_t hwirq)
376
{
377
	struct intel_mid_gpio *priv = d->host_data;
378

379
	irq_set_chip_and_handler(irq, &intel_mid_irqchip, handle_simple_irq);
380 381
	irq_set_chip_data(irq, priv);
	irq_set_irq_type(irq, IRQ_TYPE_NONE);
382 383 384 385

	return 0;
}

386 387
static const struct irq_domain_ops intel_gpio_irq_ops = {
	.map = intel_gpio_irq_map,
388 389 390
	.xlate = irq_domain_xlate_twocell,
};

391
static int intel_gpio_runtime_idle(struct device *dev)
392
{
393
	pm_schedule_suspend(dev, 500);
394 395 396
	return -EBUSY;
}

397 398
static const struct dev_pm_ops intel_gpio_pm_ops = {
	SET_RUNTIME_PM_OPS(NULL, NULL, intel_gpio_runtime_idle)
399 400
};

401
static int intel_gpio_probe(struct pci_dev *pdev,
402
			  const struct pci_device_id *id)
403
{
404
	void __iomem *base;
405
	struct intel_mid_gpio *priv;
406
	u32 gpio_base;
407
	u32 irq_base;
408
	int retval;
409 410
	struct intel_mid_gpio_ddata *ddata =
				(struct intel_mid_gpio_ddata *)id->driver_data;
411

412
	retval = pcim_enable_device(pdev);
413
	if (retval)
414
		return retval;
415

416
	retval = pcim_iomap_regions(pdev, 1 << 0 | 1 << 1, pci_name(pdev));
417
	if (retval) {
418 419
		dev_err(&pdev->dev, "I/O memory mapping error\n");
		return retval;
420
	}
421

422 423
	base = pcim_iomap_table(pdev)[1];

424 425 426
	irq_base = readl(base);
	gpio_base = readl(sizeof(u32) + base);

427
	/* release the IO mapping, since we already get the info from bar1 */
428
	pcim_iounmap_regions(pdev, 1 << 1);
429

430 431
	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv) {
432
		dev_err(&pdev->dev, "can't allocate chip data\n");
433
		return -ENOMEM;
434
	}
435

436 437
	priv->reg_base = pcim_iomap_table(pdev)[0];
	priv->chip.label = dev_name(&pdev->dev);
438
	priv->chip.dev = &pdev->dev;
439 440 441 442 443 444 445 446
	priv->chip.request = intel_gpio_request;
	priv->chip.direction_input = intel_gpio_direction_input;
	priv->chip.direction_output = intel_gpio_direction_output;
	priv->chip.get = intel_gpio_get;
	priv->chip.set = intel_gpio_set;
	priv->chip.to_irq = intel_gpio_to_irq;
	priv->chip.base = gpio_base;
	priv->chip.ngpio = ddata->ngpio;
447
	priv->chip.can_sleep = false;
448 449 450 451 452 453 454
	priv->pdev = pdev;

	spin_lock_init(&priv->lock);

	priv->domain = irq_domain_add_simple(pdev->dev.of_node, ddata->ngpio,
					irq_base, &intel_gpio_irq_ops, priv);
	if (!priv->domain)
455
		return -ENOMEM;
456

457 458
	pci_set_drvdata(pdev, priv);
	retval = gpiochip_add(&priv->chip);
459
	if (retval) {
460
		dev_err(&pdev->dev, "gpiochip_add error %d\n", retval);
461
		return retval;
462
	}
463

464
	intel_mid_irq_init_hw(priv);
465

466 467
	irq_set_handler_data(pdev->irq, priv);
	irq_set_chained_handler(pdev->irq, intel_mid_irq_handler);
468

469 470 471
	pm_runtime_put_noidle(&pdev->dev);
	pm_runtime_allow(&pdev->dev);

472
	return 0;
473 474
}

475 476 477 478
static struct pci_driver intel_gpio_driver = {
	.name		= "intel_mid_gpio",
	.id_table	= intel_gpio_ids,
	.probe		= intel_gpio_probe,
479
	.driver		= {
480
		.pm	= &intel_gpio_pm_ops,
481
	},
482 483
};

484
static int __init intel_gpio_init(void)
485
{
486
	return pci_register_driver(&intel_gpio_driver);
487 488
}

489
device_initcall(intel_gpio_init);