gpio-intel-mid.c 12.5 KB
Newer Older
G
Grant Likely 已提交
1 2 3
/*
 * Moorestown platform Langwell chip GPIO driver
 *
4
 * Copyright (c) 2008, 2009, 2013, Intel Corporation.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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.
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* Supports:
 * Moorestown platform Langwell chip.
A
Alek Du 已提交
22
 * Medfield platform Penwell chip.
23 24
 * Clovertrail platform Cloverview chip.
 * Merrifield platform Tangier chip.
25 26 27 28
 */

#include <linux/module.h>
#include <linux/pci.h>
29
#include <linux/platform_device.h>
30 31 32 33 34 35 36 37
#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>
38
#include <linux/slab.h>
39
#include <linux/pm_runtime.h>
40
#include <linux/irqdomain.h>
41

42 43
#define INTEL_MID_IRQ_TYPE_EDGE		(1 << 0)
#define INTEL_MID_IRQ_TYPE_LEVEL	(1 << 1)
44

A
Alek Du 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/*
 * 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 */
67
	GAFR,		/* alt function */
68 69
};

70 71
/* intel_mid gpio driver data */
struct intel_mid_gpio_ddata {
72 73 74 75 76 77 78 79
	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 */
};

80
struct intel_mid_gpio {
81
	struct gpio_chip		chip;
82
	void __iomem			*reg_base;
83
	spinlock_t			lock;
84
	struct pci_dev			*pdev;
85
	struct irq_domain		*domain;
86 87
};

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

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

97
	return priv->reg_base + reg_type * nreg * 4 + reg * 4;
A
Alek Du 已提交
98 99
}

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

107
	return priv->reg_base + reg_type * nreg * 4 + reg * 4;
108 109
}

110
static int intel_gpio_request(struct gpio_chip *chip, unsigned offset)
111 112 113 114 115 116 117 118 119 120 121 122
{
	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;
}

123
static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
A
Alek Du 已提交
124 125
{
	void __iomem *gplr = gpio_reg(chip, offset, GPLR);
126 127 128 129

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

130
static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
131 132 133 134
{
	void __iomem *gpsr, *gpcr;

	if (value) {
A
Alek Du 已提交
135
		gpsr = gpio_reg(chip, offset, GPSR);
136 137
		writel(BIT(offset % 32), gpsr);
	} else {
A
Alek Du 已提交
138
		gpcr = gpio_reg(chip, offset, GPCR);
139 140 141 142
		writel(BIT(offset % 32), gpcr);
	}
}

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

150 151
	if (priv->pdev)
		pm_runtime_get(&priv->pdev->dev);
152

153
	spin_lock_irqsave(&priv->lock, flags);
154 155 156
	value = readl(gpdr);
	value &= ~BIT(offset % 32);
	writel(value, gpdr);
157
	spin_unlock_irqrestore(&priv->lock, flags);
158

159 160
	if (priv->pdev)
		pm_runtime_put(&priv->pdev->dev);
161

162 163 164
	return 0;
}

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

172
	intel_gpio_set(chip, offset, value);
173

174 175
	if (priv->pdev)
		pm_runtime_get(&priv->pdev->dev);
176

177
	spin_lock_irqsave(&priv->lock, flags);
178
	value = readl(gpdr);
179
	value |= BIT(offset % 32);
180
	writel(value, gpdr);
181
	spin_unlock_irqrestore(&priv->lock, flags);
182

183 184
	if (priv->pdev)
		pm_runtime_put(&priv->pdev->dev);
185

186 187 188
	return 0;
}

189
static int intel_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
190
{
191 192
	struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
	return irq_create_mapping(priv->domain, offset);
193 194
}

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

204
	if (gpio >= priv->chip.ngpio)
205
		return -EINVAL;
206

207 208
	if (priv->pdev)
		pm_runtime_get(&priv->pdev->dev);
209

210
	spin_lock_irqsave(&priv->lock, flags);
211 212 213 214 215 216 217 218 219 220 221
	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);
222
	spin_unlock_irqrestore(&priv->lock, flags);
223

224 225
	if (priv->pdev)
		pm_runtime_put(&priv->pdev->dev);
226

227
	return 0;
228
}
229

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

234
static void intel_mid_irq_mask(struct irq_data *d)
235
{
236
}
237

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
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));
}

258 259 260 261 262
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,
263 264
	.irq_startup	= intel_mid_irq_startup,
	.irq_shutdown	= intel_mid_irq_shutdown,
265 266
};

267
static const struct intel_mid_gpio_ddata gpio_lincroft = {
268 269 270
	.ngpio = 64,
};

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

276
static const struct intel_mid_gpio_ddata gpio_penwell_core = {
277
	.ngpio = 96,
278
	.chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
279 280
};

281
static const struct intel_mid_gpio_ddata gpio_cloverview_aon = {
282
	.ngpio = 96,
283
	.chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE | INTEL_MID_IRQ_TYPE_LEVEL,
284 285
};

286
static const struct intel_mid_gpio_ddata gpio_cloverview_core = {
287
	.ngpio = 96,
288
	.chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
289 290
};

291
static const struct intel_mid_gpio_ddata gpio_tangier = {
292 293 294 295 296
	.ngpio = 192,
	.gplr_offset = 4,
	.flis_base = 0xff0c0000,
	.flis_len = 0x8000,
	.get_flis_offset = NULL,
297
	.chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
298 299
};

300
static const struct pci_device_id intel_gpio_ids[] = {
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 331
	{
		/* 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 }
332
};
333
MODULE_DEVICE_TABLE(pci, intel_gpio_ids);
334

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

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

357
	chip->irq_eoi(data);
358 359
}

360
static void intel_mid_irq_init_hw(struct intel_mid_gpio *priv)
361 362 363 364
{
	void __iomem *reg;
	unsigned base;

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

378 379
static int intel_gpio_irq_map(struct irq_domain *d, unsigned int irq,
			    irq_hw_number_t hwirq)
380
{
381
	struct intel_mid_gpio *priv = d->host_data;
382

383
	irq_set_chip_and_handler(irq, &intel_mid_irqchip, handle_simple_irq);
384 385
	irq_set_chip_data(irq, priv);
	irq_set_irq_type(irq, IRQ_TYPE_NONE);
386 387 388 389

	return 0;
}

390 391
static const struct irq_domain_ops intel_gpio_irq_ops = {
	.map = intel_gpio_irq_map,
392 393 394
	.xlate = irq_domain_xlate_twocell,
};

395
static int intel_gpio_runtime_idle(struct device *dev)
396
{
397
	pm_schedule_suspend(dev, 500);
398 399 400
	return -EBUSY;
}

401 402
static const struct dev_pm_ops intel_gpio_pm_ops = {
	SET_RUNTIME_PM_OPS(NULL, NULL, intel_gpio_runtime_idle)
403 404
};

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

416
	retval = pcim_enable_device(pdev);
417
	if (retval)
418
		return retval;
419

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

426 427
	base = pcim_iomap_table(pdev)[1];

428 429 430
	irq_base = readl(base);
	gpio_base = readl(sizeof(u32) + base);

431
	/* release the IO mapping, since we already get the info from bar1 */
432
	pcim_iounmap_regions(pdev, 1 << 1);
433

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

440 441
	priv->reg_base = pcim_iomap_table(pdev)[0];
	priv->chip.label = dev_name(&pdev->dev);
442
	priv->chip.dev = &pdev->dev;
443 444 445 446 447 448 449 450
	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;
451
	priv->chip.can_sleep = false;
452 453 454 455 456 457 458
	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)
459
		return -ENOMEM;
460

461 462
	pci_set_drvdata(pdev, priv);
	retval = gpiochip_add(&priv->chip);
463
	if (retval) {
464
		dev_err(&pdev->dev, "gpiochip_add error %d\n", retval);
465
		return retval;
466
	}
467

468
	intel_mid_irq_init_hw(priv);
469

470 471
	irq_set_handler_data(pdev->irq, priv);
	irq_set_chained_handler(pdev->irq, intel_mid_irq_handler);
472

473 474 475
	pm_runtime_put_noidle(&pdev->dev);
	pm_runtime_allow(&pdev->dev);

476
	return 0;
477 478
}

479 480 481 482
static struct pci_driver intel_gpio_driver = {
	.name		= "intel_mid_gpio",
	.id_table	= intel_gpio_ids,
	.probe		= intel_gpio_probe,
483
	.driver		= {
484
		.pm	= &intel_gpio_pm_ops,
485
	},
486 487
};

488
static int __init intel_gpio_init(void)
489
{
490
	return pci_register_driver(&intel_gpio_driver);
491 492
}

493
device_initcall(intel_gpio_init);