2 * Moorestown platform Langwell chip GPIO driver
4 * Copyright (c) 2008, 2009, 2013, Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * Moorestown platform Langwell chip.
22 * Medfield platform Penwell chip.
25 #include <linux/module.h>
26 #include <linux/pci.h>
27 #include <linux/platform_device.h>
28 #include <linux/kernel.h>
29 #include <linux/delay.h>
30 #include <linux/stddef.h>
31 #include <linux/interrupt.h>
32 #include <linux/init.h>
33 #include <linux/irq.h>
35 #include <linux/gpio.h>
36 #include <linux/slab.h>
37 #include <linux/pm_runtime.h>
38 #include <linux/irqdomain.h>
40 #define LNW_IRQ_TYPE_EDGE (1 << 0)
41 #define LNW_IRQ_TYPE_LEVEL (1 << 1)
44 * Langwell chip has 64 pins and thus there are 2 32bit registers to control
45 * each feature, while Penwell chip has 96 pins for each block, and need 3 32bit
46 * registers to control them, so we only define the order here instead of a
47 * structure, to get a bit offset for a pin (use GPDR as an example):
52 * reg_addr = reg_base + GPDR * nreg * 4 + reg * 4;
54 * so the bit of reg_addr is to control pin offset's GPDR feature
58 GPLR = 0, /* pin level read-only */
59 GPDR, /* pin direction */
62 GRER, /* rising edge detect */
63 GFER, /* falling edge detect */
64 GEDR, /* edge detect result */
65 GAFR, /* alt function */
68 /* langwell gpio driver data */
69 struct lnw_gpio_ddata {
70 u16 ngpio; /* number of gpio pins */
71 u32 gplr_offset; /* offset of first GPLR register from base */
72 u32 flis_base; /* base address of FLIS registers */
73 u32 flis_len; /* length of FLIS registers */
74 u32 (*get_flis_offset)(int gpio);
75 u32 chip_irq_type; /* chip interrupt type */
79 struct gpio_chip chip;
80 void __iomem *reg_base;
83 struct irq_domain *domain;
86 #define to_lnw_priv(chip) container_of(chip, struct lnw_gpio, chip)
88 static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned offset,
89 enum GPIO_REG reg_type)
91 struct lnw_gpio *lnw = to_lnw_priv(chip);
92 unsigned nreg = chip->ngpio / 32;
95 return lnw->reg_base + reg_type * nreg * 4 + reg * 4;
98 static void __iomem *gpio_reg_2bit(struct gpio_chip *chip, unsigned offset,
99 enum GPIO_REG reg_type)
101 struct lnw_gpio *lnw = to_lnw_priv(chip);
102 unsigned nreg = chip->ngpio / 32;
103 u8 reg = offset / 16;
105 return lnw->reg_base + reg_type * nreg * 4 + reg * 4;
108 static int lnw_gpio_request(struct gpio_chip *chip, unsigned offset)
110 void __iomem *gafr = gpio_reg_2bit(chip, offset, GAFR);
111 u32 value = readl(gafr);
112 int shift = (offset % 16) << 1, af = (value >> shift) & 3;
115 value &= ~(3 << shift);
121 static int lnw_gpio_get(struct gpio_chip *chip, unsigned offset)
123 void __iomem *gplr = gpio_reg(chip, offset, GPLR);
125 return readl(gplr) & BIT(offset % 32);
128 static void lnw_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
130 void __iomem *gpsr, *gpcr;
133 gpsr = gpio_reg(chip, offset, GPSR);
134 writel(BIT(offset % 32), gpsr);
136 gpcr = gpio_reg(chip, offset, GPCR);
137 writel(BIT(offset % 32), gpcr);
141 static int lnw_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
143 struct lnw_gpio *lnw = to_lnw_priv(chip);
144 void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
149 pm_runtime_get(&lnw->pdev->dev);
151 spin_lock_irqsave(&lnw->lock, flags);
153 value &= ~BIT(offset % 32);
155 spin_unlock_irqrestore(&lnw->lock, flags);
158 pm_runtime_put(&lnw->pdev->dev);
163 static int lnw_gpio_direction_output(struct gpio_chip *chip,
164 unsigned offset, int value)
166 struct lnw_gpio *lnw = to_lnw_priv(chip);
167 void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
170 lnw_gpio_set(chip, offset, value);
173 pm_runtime_get(&lnw->pdev->dev);
175 spin_lock_irqsave(&lnw->lock, flags);
177 value |= BIT(offset % 32);
179 spin_unlock_irqrestore(&lnw->lock, flags);
182 pm_runtime_put(&lnw->pdev->dev);
187 static int lnw_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
189 struct lnw_gpio *lnw = to_lnw_priv(chip);
190 return irq_create_mapping(lnw->domain, offset);
193 static int lnw_irq_type(struct irq_data *d, unsigned type)
195 struct lnw_gpio *lnw = irq_data_get_irq_chip_data(d);
196 u32 gpio = irqd_to_hwirq(d);
199 void __iomem *grer = gpio_reg(&lnw->chip, gpio, GRER);
200 void __iomem *gfer = gpio_reg(&lnw->chip, gpio, GFER);
202 if (gpio >= lnw->chip.ngpio)
206 pm_runtime_get(&lnw->pdev->dev);
208 spin_lock_irqsave(&lnw->lock, flags);
209 if (type & IRQ_TYPE_EDGE_RISING)
210 value = readl(grer) | BIT(gpio % 32);
212 value = readl(grer) & (~BIT(gpio % 32));
215 if (type & IRQ_TYPE_EDGE_FALLING)
216 value = readl(gfer) | BIT(gpio % 32);
218 value = readl(gfer) & (~BIT(gpio % 32));
220 spin_unlock_irqrestore(&lnw->lock, flags);
223 pm_runtime_put(&lnw->pdev->dev);
228 static void lnw_irq_unmask(struct irq_data *d)
232 static void lnw_irq_mask(struct irq_data *d)
236 static struct irq_chip lnw_irqchip = {
238 .irq_mask = lnw_irq_mask,
239 .irq_unmask = lnw_irq_unmask,
240 .irq_set_type = lnw_irq_type,
243 static const struct lnw_gpio_ddata gpio_lincroft = {
247 static const struct lnw_gpio_ddata gpio_penwell_aon = {
249 .chip_irq_type = LNW_IRQ_TYPE_EDGE,
252 static const struct lnw_gpio_ddata gpio_penwell_core = {
254 .chip_irq_type = LNW_IRQ_TYPE_EDGE,
257 static const struct lnw_gpio_ddata gpio_cloverview_aon = {
259 .chip_irq_type = LNW_IRQ_TYPE_EDGE | LNW_IRQ_TYPE_LEVEL,
262 static const struct lnw_gpio_ddata gpio_cloverview_core = {
264 .chip_irq_type = LNW_IRQ_TYPE_EDGE,
267 static const struct lnw_gpio_ddata gpio_tangier = {
270 .flis_base = 0xff0c0000,
272 .get_flis_offset = NULL,
273 .chip_irq_type = LNW_IRQ_TYPE_EDGE,
276 static DEFINE_PCI_DEVICE_TABLE(lnw_gpio_ids) = {
279 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f),
280 .driver_data = (kernel_ulong_t)&gpio_lincroft,
284 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081f),
285 .driver_data = (kernel_ulong_t)&gpio_penwell_aon,
289 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081a),
290 .driver_data = (kernel_ulong_t)&gpio_penwell_core,
294 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08eb),
295 .driver_data = (kernel_ulong_t)&gpio_cloverview_aon,
298 /* Cloverview Core */
299 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08f7),
300 .driver_data = (kernel_ulong_t)&gpio_cloverview_core,
304 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x1199),
305 .driver_data = (kernel_ulong_t)&gpio_tangier,
309 MODULE_DEVICE_TABLE(pci, lnw_gpio_ids);
311 static void lnw_irq_handler(unsigned irq, struct irq_desc *desc)
313 struct irq_data *data = irq_desc_get_irq_data(desc);
314 struct lnw_gpio *lnw = irq_data_get_irq_handler_data(data);
315 struct irq_chip *chip = irq_data_get_irq_chip(data);
316 u32 base, gpio, mask;
317 unsigned long pending;
320 /* check GPIO controller to check which pin triggered the interrupt */
321 for (base = 0; base < lnw->chip.ngpio; base += 32) {
322 gedr = gpio_reg(&lnw->chip, base, GEDR);
323 while ((pending = readl(gedr))) {
324 gpio = __ffs(pending);
326 /* Clear before handling so we can't lose an edge */
328 generic_handle_irq(irq_find_mapping(lnw->domain,
336 static void lnw_irq_init_hw(struct lnw_gpio *lnw)
341 for (base = 0; base < lnw->chip.ngpio; base += 32) {
342 /* Clear the rising-edge detect register */
343 reg = gpio_reg(&lnw->chip, base, GRER);
345 /* Clear the falling-edge detect register */
346 reg = gpio_reg(&lnw->chip, base, GFER);
348 /* Clear the edge detect status register */
349 reg = gpio_reg(&lnw->chip, base, GEDR);
354 static int lnw_gpio_irq_map(struct irq_domain *d, unsigned int virq,
357 struct lnw_gpio *lnw = d->host_data;
359 irq_set_chip_and_handler_name(virq, &lnw_irqchip, handle_simple_irq,
361 irq_set_chip_data(virq, lnw);
362 irq_set_irq_type(virq, IRQ_TYPE_NONE);
367 static const struct irq_domain_ops lnw_gpio_irq_ops = {
368 .map = lnw_gpio_irq_map,
369 .xlate = irq_domain_xlate_twocell,
372 static int lnw_gpio_runtime_idle(struct device *dev)
374 pm_schedule_suspend(dev, 500);
378 static const struct dev_pm_ops lnw_gpio_pm_ops = {
379 SET_RUNTIME_PM_OPS(NULL, NULL, lnw_gpio_runtime_idle)
382 static int lnw_gpio_probe(struct pci_dev *pdev,
383 const struct pci_device_id *id)
386 struct lnw_gpio *lnw;
390 struct lnw_gpio_ddata *ddata = (struct lnw_gpio_ddata *)id->driver_data;
392 retval = pcim_enable_device(pdev);
396 retval = pcim_iomap_regions(pdev, 1 << 0 | 1 << 1, pci_name(pdev));
398 dev_err(&pdev->dev, "I/O memory mapping error\n");
402 base = pcim_iomap_table(pdev)[1];
404 irq_base = readl(base);
405 gpio_base = readl(sizeof(u32) + base);
407 /* release the IO mapping, since we already get the info from bar1 */
408 pcim_iounmap_regions(pdev, 1 << 1);
410 lnw = devm_kzalloc(&pdev->dev, sizeof(*lnw), GFP_KERNEL);
412 dev_err(&pdev->dev, "can't allocate chip data\n");
416 lnw->reg_base = pcim_iomap_table(pdev)[0];
417 lnw->chip.label = dev_name(&pdev->dev);
418 lnw->chip.request = lnw_gpio_request;
419 lnw->chip.direction_input = lnw_gpio_direction_input;
420 lnw->chip.direction_output = lnw_gpio_direction_output;
421 lnw->chip.get = lnw_gpio_get;
422 lnw->chip.set = lnw_gpio_set;
423 lnw->chip.to_irq = lnw_gpio_to_irq;
424 lnw->chip.base = gpio_base;
425 lnw->chip.ngpio = ddata->ngpio;
426 lnw->chip.can_sleep = 0;
429 spin_lock_init(&lnw->lock);
431 lnw->domain = irq_domain_add_simple(pdev->dev.of_node, ddata->ngpio,
432 irq_base, &lnw_gpio_irq_ops, lnw);
436 pci_set_drvdata(pdev, lnw);
437 retval = gpiochip_add(&lnw->chip);
439 dev_err(&pdev->dev, "gpiochip_add error %d\n", retval);
443 lnw_irq_init_hw(lnw);
445 irq_set_handler_data(pdev->irq, lnw);
446 irq_set_chained_handler(pdev->irq, lnw_irq_handler);
448 pm_runtime_put_noidle(&pdev->dev);
449 pm_runtime_allow(&pdev->dev);
454 static struct pci_driver lnw_gpio_driver = {
455 .name = "langwell_gpio",
456 .id_table = lnw_gpio_ids,
457 .probe = lnw_gpio_probe,
459 .pm = &lnw_gpio_pm_ops,
463 static int __init lnw_gpio_init(void)
465 return pci_register_driver(&lnw_gpio_driver);
468 device_initcall(lnw_gpio_init);