1 // SPDX-License-Identifier: GPL-2.0
3 * Intel Merrifield SoC GPIO driver
5 * Copyright (c) 2016 Intel Corporation.
6 * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
9 #include <linux/acpi.h>
10 #include <linux/bitops.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/pci.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/string_helpers.h>
19 #define GCCR 0x000 /* controller configuration */
20 #define GPLR 0x004 /* pin level r/o */
21 #define GPDR 0x01c /* pin direction */
22 #define GPSR 0x034 /* pin set w/o */
23 #define GPCR 0x04c /* pin clear w/o */
24 #define GRER 0x064 /* rising edge detect */
25 #define GFER 0x07c /* falling edge detect */
26 #define GFBR 0x094 /* glitch filter bypass */
27 #define GIMR 0x0ac /* interrupt mask */
28 #define GISR 0x0c4 /* interrupt source */
29 #define GITR 0x300 /* input type */
30 #define GLPR 0x318 /* level input polarity */
31 #define GWMR 0x400 /* wake mask */
32 #define GWSR 0x418 /* wake source */
33 #define GSIR 0xc00 /* secure input */
35 /* Intel Merrifield has 192 GPIO pins */
36 #define MRFLD_NGPIO 192
38 struct mrfld_gpio_pinrange {
39 unsigned int gpio_base;
40 unsigned int pin_base;
44 #define GPIO_PINRANGE(gstart, gend, pstart) \
46 .gpio_base = (gstart), \
47 .pin_base = (pstart), \
48 .npins = (gend) - (gstart) + 1, \
52 struct gpio_chip chip;
53 void __iomem *reg_base;
58 static const struct mrfld_gpio_pinrange mrfld_gpio_ranges[] = {
59 GPIO_PINRANGE(0, 11, 146),
60 GPIO_PINRANGE(12, 13, 144),
61 GPIO_PINRANGE(14, 15, 35),
62 GPIO_PINRANGE(16, 16, 164),
63 GPIO_PINRANGE(17, 18, 105),
64 GPIO_PINRANGE(19, 22, 101),
65 GPIO_PINRANGE(23, 30, 107),
66 GPIO_PINRANGE(32, 43, 67),
67 GPIO_PINRANGE(44, 63, 195),
68 GPIO_PINRANGE(64, 67, 140),
69 GPIO_PINRANGE(68, 69, 165),
70 GPIO_PINRANGE(70, 71, 65),
71 GPIO_PINRANGE(72, 76, 228),
72 GPIO_PINRANGE(77, 86, 37),
73 GPIO_PINRANGE(87, 87, 48),
74 GPIO_PINRANGE(88, 88, 47),
75 GPIO_PINRANGE(89, 96, 49),
76 GPIO_PINRANGE(97, 97, 34),
77 GPIO_PINRANGE(102, 119, 83),
78 GPIO_PINRANGE(120, 123, 79),
79 GPIO_PINRANGE(124, 135, 115),
80 GPIO_PINRANGE(137, 142, 158),
81 GPIO_PINRANGE(154, 163, 24),
82 GPIO_PINRANGE(164, 176, 215),
83 GPIO_PINRANGE(177, 189, 127),
84 GPIO_PINRANGE(190, 191, 178),
87 static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned int offset,
88 unsigned int reg_type_offset)
90 struct mrfld_gpio *priv = gpiochip_get_data(chip);
93 return priv->reg_base + reg_type_offset + reg * 4;
96 static int mrfld_gpio_get(struct gpio_chip *chip, unsigned int offset)
98 void __iomem *gplr = gpio_reg(chip, offset, GPLR);
100 return !!(readl(gplr) & BIT(offset % 32));
103 static void mrfld_gpio_set(struct gpio_chip *chip, unsigned int offset,
106 struct mrfld_gpio *priv = gpiochip_get_data(chip);
107 void __iomem *gpsr, *gpcr;
110 raw_spin_lock_irqsave(&priv->lock, flags);
113 gpsr = gpio_reg(chip, offset, GPSR);
114 writel(BIT(offset % 32), gpsr);
116 gpcr = gpio_reg(chip, offset, GPCR);
117 writel(BIT(offset % 32), gpcr);
120 raw_spin_unlock_irqrestore(&priv->lock, flags);
123 static int mrfld_gpio_direction_input(struct gpio_chip *chip,
126 struct mrfld_gpio *priv = gpiochip_get_data(chip);
127 void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
131 raw_spin_lock_irqsave(&priv->lock, flags);
134 value &= ~BIT(offset % 32);
137 raw_spin_unlock_irqrestore(&priv->lock, flags);
142 static int mrfld_gpio_direction_output(struct gpio_chip *chip,
143 unsigned int offset, int value)
145 struct mrfld_gpio *priv = gpiochip_get_data(chip);
146 void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
149 mrfld_gpio_set(chip, offset, value);
151 raw_spin_lock_irqsave(&priv->lock, flags);
154 value |= BIT(offset % 32);
157 raw_spin_unlock_irqrestore(&priv->lock, flags);
162 static int mrfld_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
164 void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
166 if (readl(gpdr) & BIT(offset % 32))
167 return GPIO_LINE_DIRECTION_OUT;
169 return GPIO_LINE_DIRECTION_IN;
172 static int mrfld_gpio_set_debounce(struct gpio_chip *chip, unsigned int offset,
173 unsigned int debounce)
175 struct mrfld_gpio *priv = gpiochip_get_data(chip);
176 void __iomem *gfbr = gpio_reg(chip, offset, GFBR);
180 raw_spin_lock_irqsave(&priv->lock, flags);
183 value = readl(gfbr) & ~BIT(offset % 32);
185 value = readl(gfbr) | BIT(offset % 32);
188 raw_spin_unlock_irqrestore(&priv->lock, flags);
193 static int mrfld_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
194 unsigned long config)
198 if ((pinconf_to_config_param(config) == PIN_CONFIG_BIAS_DISABLE) ||
199 (pinconf_to_config_param(config) == PIN_CONFIG_BIAS_PULL_UP) ||
200 (pinconf_to_config_param(config) == PIN_CONFIG_BIAS_PULL_DOWN))
201 return gpiochip_generic_config(chip, offset, config);
203 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
206 debounce = pinconf_to_config_argument(config);
207 return mrfld_gpio_set_debounce(chip, offset, debounce);
210 static void mrfld_irq_ack(struct irq_data *d)
212 struct mrfld_gpio *priv = irq_data_get_irq_chip_data(d);
213 u32 gpio = irqd_to_hwirq(d);
214 void __iomem *gisr = gpio_reg(&priv->chip, gpio, GISR);
217 raw_spin_lock_irqsave(&priv->lock, flags);
219 writel(BIT(gpio % 32), gisr);
221 raw_spin_unlock_irqrestore(&priv->lock, flags);
224 static void mrfld_irq_unmask_mask(struct mrfld_gpio *priv, u32 gpio, bool unmask)
226 void __iomem *gimr = gpio_reg(&priv->chip, gpio, GIMR);
230 raw_spin_lock_irqsave(&priv->lock, flags);
233 value = readl(gimr) | BIT(gpio % 32);
235 value = readl(gimr) & ~BIT(gpio % 32);
238 raw_spin_unlock_irqrestore(&priv->lock, flags);
241 static void mrfld_irq_mask(struct irq_data *d)
243 struct mrfld_gpio *priv = irq_data_get_irq_chip_data(d);
244 u32 gpio = irqd_to_hwirq(d);
246 mrfld_irq_unmask_mask(priv, gpio, false);
247 gpiochip_disable_irq(&priv->chip, gpio);
250 static void mrfld_irq_unmask(struct irq_data *d)
252 struct mrfld_gpio *priv = irq_data_get_irq_chip_data(d);
253 u32 gpio = irqd_to_hwirq(d);
255 gpiochip_enable_irq(&priv->chip, gpio);
256 mrfld_irq_unmask_mask(priv, gpio, true);
259 static int mrfld_irq_set_type(struct irq_data *d, unsigned int type)
261 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
262 struct mrfld_gpio *priv = gpiochip_get_data(gc);
263 u32 gpio = irqd_to_hwirq(d);
264 void __iomem *grer = gpio_reg(&priv->chip, gpio, GRER);
265 void __iomem *gfer = gpio_reg(&priv->chip, gpio, GFER);
266 void __iomem *gitr = gpio_reg(&priv->chip, gpio, GITR);
267 void __iomem *glpr = gpio_reg(&priv->chip, gpio, GLPR);
271 raw_spin_lock_irqsave(&priv->lock, flags);
273 if (type & IRQ_TYPE_EDGE_RISING)
274 value = readl(grer) | BIT(gpio % 32);
276 value = readl(grer) & ~BIT(gpio % 32);
279 if (type & IRQ_TYPE_EDGE_FALLING)
280 value = readl(gfer) | BIT(gpio % 32);
282 value = readl(gfer) & ~BIT(gpio % 32);
286 * To prevent glitches from triggering an unintended level interrupt,
287 * configure GLPR register first and then configure GITR.
289 if (type & IRQ_TYPE_LEVEL_LOW)
290 value = readl(glpr) | BIT(gpio % 32);
292 value = readl(glpr) & ~BIT(gpio % 32);
295 if (type & IRQ_TYPE_LEVEL_MASK) {
296 value = readl(gitr) | BIT(gpio % 32);
299 irq_set_handler_locked(d, handle_level_irq);
300 } else if (type & IRQ_TYPE_EDGE_BOTH) {
301 value = readl(gitr) & ~BIT(gpio % 32);
304 irq_set_handler_locked(d, handle_edge_irq);
307 raw_spin_unlock_irqrestore(&priv->lock, flags);
312 static int mrfld_irq_set_wake(struct irq_data *d, unsigned int on)
314 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
315 struct mrfld_gpio *priv = gpiochip_get_data(gc);
316 u32 gpio = irqd_to_hwirq(d);
317 void __iomem *gwmr = gpio_reg(&priv->chip, gpio, GWMR);
318 void __iomem *gwsr = gpio_reg(&priv->chip, gpio, GWSR);
322 raw_spin_lock_irqsave(&priv->lock, flags);
324 /* Clear the existing wake status */
325 writel(BIT(gpio % 32), gwsr);
328 value = readl(gwmr) | BIT(gpio % 32);
330 value = readl(gwmr) & ~BIT(gpio % 32);
333 raw_spin_unlock_irqrestore(&priv->lock, flags);
335 dev_dbg(priv->dev, "%s wake for gpio %u\n", str_enable_disable(on), gpio);
339 static const struct irq_chip mrfld_irqchip = {
340 .name = "gpio-merrifield",
341 .irq_ack = mrfld_irq_ack,
342 .irq_mask = mrfld_irq_mask,
343 .irq_unmask = mrfld_irq_unmask,
344 .irq_set_type = mrfld_irq_set_type,
345 .irq_set_wake = mrfld_irq_set_wake,
346 .flags = IRQCHIP_IMMUTABLE,
347 GPIOCHIP_IRQ_RESOURCE_HELPERS,
350 static void mrfld_irq_handler(struct irq_desc *desc)
352 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
353 struct mrfld_gpio *priv = gpiochip_get_data(gc);
354 struct irq_chip *irqchip = irq_desc_get_chip(desc);
355 unsigned long base, gpio;
357 chained_irq_enter(irqchip, desc);
359 /* Check GPIO controller to check which pin triggered the interrupt */
360 for (base = 0; base < priv->chip.ngpio; base += 32) {
361 void __iomem *gisr = gpio_reg(&priv->chip, base, GISR);
362 void __iomem *gimr = gpio_reg(&priv->chip, base, GIMR);
363 unsigned long pending, enabled;
365 pending = readl(gisr);
366 enabled = readl(gimr);
368 /* Only interrupts that are enabled */
371 for_each_set_bit(gpio, &pending, 32)
372 generic_handle_domain_irq(gc->irq.domain, base + gpio);
375 chained_irq_exit(irqchip, desc);
378 static int mrfld_irq_init_hw(struct gpio_chip *chip)
380 struct mrfld_gpio *priv = gpiochip_get_data(chip);
384 for (base = 0; base < priv->chip.ngpio; base += 32) {
385 /* Clear the rising-edge detect register */
386 reg = gpio_reg(&priv->chip, base, GRER);
388 /* Clear the falling-edge detect register */
389 reg = gpio_reg(&priv->chip, base, GFER);
396 static const char *mrfld_gpio_get_pinctrl_dev_name(struct mrfld_gpio *priv)
398 struct acpi_device *adev;
401 adev = acpi_dev_get_first_match_dev("INTC1002", NULL, -1);
403 name = devm_kstrdup(priv->dev, acpi_dev_name(adev), GFP_KERNEL);
406 name = "pinctrl-merrifield";
412 static int mrfld_gpio_add_pin_ranges(struct gpio_chip *chip)
414 struct mrfld_gpio *priv = gpiochip_get_data(chip);
415 const struct mrfld_gpio_pinrange *range;
416 const char *pinctrl_dev_name;
420 pinctrl_dev_name = mrfld_gpio_get_pinctrl_dev_name(priv);
421 if (!pinctrl_dev_name)
424 for (i = 0; i < ARRAY_SIZE(mrfld_gpio_ranges); i++) {
425 range = &mrfld_gpio_ranges[i];
426 retval = gpiochip_add_pin_range(&priv->chip, pinctrl_dev_name,
431 dev_err(priv->dev, "failed to add GPIO pin range\n");
439 static int mrfld_gpio_probe(struct pci_dev *pdev, const struct pci_device_id *id)
441 struct gpio_irq_chip *girq;
442 struct mrfld_gpio *priv;
443 u32 gpio_base, irq_base;
447 retval = pcim_enable_device(pdev);
451 retval = pcim_iomap_regions(pdev, BIT(1) | BIT(0), pci_name(pdev));
453 dev_err(&pdev->dev, "I/O memory mapping error\n");
457 base = pcim_iomap_table(pdev)[1];
459 irq_base = readl(base + 0 * sizeof(u32));
460 gpio_base = readl(base + 1 * sizeof(u32));
462 /* Release the IO mapping, since we already get the info from BAR1 */
463 pcim_iounmap_regions(pdev, BIT(1));
465 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
469 priv->dev = &pdev->dev;
470 priv->reg_base = pcim_iomap_table(pdev)[0];
472 priv->chip.label = dev_name(&pdev->dev);
473 priv->chip.parent = &pdev->dev;
474 priv->chip.request = gpiochip_generic_request;
475 priv->chip.free = gpiochip_generic_free;
476 priv->chip.direction_input = mrfld_gpio_direction_input;
477 priv->chip.direction_output = mrfld_gpio_direction_output;
478 priv->chip.get = mrfld_gpio_get;
479 priv->chip.set = mrfld_gpio_set;
480 priv->chip.get_direction = mrfld_gpio_get_direction;
481 priv->chip.set_config = mrfld_gpio_set_config;
482 priv->chip.base = gpio_base;
483 priv->chip.ngpio = MRFLD_NGPIO;
484 priv->chip.can_sleep = false;
485 priv->chip.add_pin_ranges = mrfld_gpio_add_pin_ranges;
487 raw_spin_lock_init(&priv->lock);
489 retval = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
493 girq = &priv->chip.irq;
494 gpio_irq_chip_set_chip(girq, &mrfld_irqchip);
495 girq->init_hw = mrfld_irq_init_hw;
496 girq->parent_handler = mrfld_irq_handler;
497 girq->num_parents = 1;
498 girq->parents = devm_kcalloc(&pdev->dev, girq->num_parents,
499 sizeof(*girq->parents), GFP_KERNEL);
502 girq->parents[0] = pci_irq_vector(pdev, 0);
503 girq->first = irq_base;
504 girq->default_type = IRQ_TYPE_NONE;
505 girq->handler = handle_bad_irq;
507 retval = devm_gpiochip_add_data(&pdev->dev, &priv->chip, priv);
509 dev_err(&pdev->dev, "gpiochip_add error %d\n", retval);
513 pci_set_drvdata(pdev, priv);
517 static const struct pci_device_id mrfld_gpio_ids[] = {
518 { PCI_VDEVICE(INTEL, 0x1199) },
521 MODULE_DEVICE_TABLE(pci, mrfld_gpio_ids);
523 static struct pci_driver mrfld_gpio_driver = {
524 .name = "gpio-merrifield",
525 .id_table = mrfld_gpio_ids,
526 .probe = mrfld_gpio_probe,
529 module_pci_driver(mrfld_gpio_driver);
531 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
532 MODULE_DESCRIPTION("Intel Merrifield SoC GPIO driver");
533 MODULE_LICENSE("GPL v2");