Andy Shevchenko | aaa2123 | 2018-11-06 14:11:42 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 2 | /* |
| 3 | * GPIO interface for Intel Sodaville SoCs. |
| 4 | * |
| 5 | * Copyright (c) 2010, 2011 Intel Corporation |
| 6 | * |
Paul Gortmaker | 6a5ead9 | 2016-05-09 19:59:55 -0400 | [diff] [blame] | 7 | * Author: Hans J. Koch <hjk@linutronix.de> |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/errno.h> |
Andy Shevchenko | 8700998 | 2018-09-04 14:26:25 +0300 | [diff] [blame] | 11 | #include <linux/gpio/driver.h> |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 12 | #include <linux/init.h> |
Andy Shevchenko | 8700998 | 2018-09-04 14:26:25 +0300 | [diff] [blame] | 13 | #include <linux/interrupt.h> |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 14 | #include <linux/io.h> |
| 15 | #include <linux/irq.h> |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Andy Shevchenko | 8700998 | 2018-09-04 14:26:25 +0300 | [diff] [blame] | 17 | #include <linux/of_irq.h> |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 18 | #include <linux/pci.h> |
| 19 | #include <linux/platform_device.h> |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 20 | |
| 21 | #define DRV_NAME "sdv_gpio" |
| 22 | #define SDV_NUM_PUB_GPIOS 12 |
| 23 | #define PCI_DEVICE_ID_SDV_GPIO 0x2e67 |
| 24 | #define GPIO_BAR 0 |
| 25 | |
| 26 | #define GPOUTR 0x00 |
| 27 | #define GPOER 0x04 |
| 28 | #define GPINR 0x08 |
| 29 | |
| 30 | #define GPSTR 0x0c |
| 31 | #define GPIT1R0 0x10 |
| 32 | #define GPIO_INT 0x14 |
| 33 | #define GPIT1R1 0x18 |
| 34 | |
| 35 | #define GPMUXCTL 0x1c |
| 36 | |
| 37 | struct sdv_gpio_chip_data { |
| 38 | int irq_base; |
| 39 | void __iomem *gpio_pub_base; |
Grant Likely | 3ffc9ce | 2012-03-28 14:55:04 -0600 | [diff] [blame] | 40 | struct irq_domain *id; |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 41 | struct irq_chip_generic *gc; |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 42 | struct gpio_chip chip; |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | static int sdv_gpio_pub_set_type(struct irq_data *d, unsigned int type) |
| 46 | { |
| 47 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 48 | struct sdv_gpio_chip_data *sd = gc->private; |
| 49 | void __iomem *type_reg; |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 50 | u32 reg; |
| 51 | |
Grant Likely | 3ffc9ce | 2012-03-28 14:55:04 -0600 | [diff] [blame] | 52 | if (d->hwirq < 8) |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 53 | type_reg = sd->gpio_pub_base + GPIT1R0; |
| 54 | else |
| 55 | type_reg = sd->gpio_pub_base + GPIT1R1; |
| 56 | |
| 57 | reg = readl(type_reg); |
| 58 | |
| 59 | switch (type) { |
| 60 | case IRQ_TYPE_LEVEL_HIGH: |
Grant Likely | 3ffc9ce | 2012-03-28 14:55:04 -0600 | [diff] [blame] | 61 | reg &= ~BIT(4 * (d->hwirq % 8)); |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 62 | break; |
| 63 | |
| 64 | case IRQ_TYPE_LEVEL_LOW: |
Grant Likely | 3ffc9ce | 2012-03-28 14:55:04 -0600 | [diff] [blame] | 65 | reg |= BIT(4 * (d->hwirq % 8)); |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 66 | break; |
| 67 | |
| 68 | default: |
| 69 | return -EINVAL; |
| 70 | } |
| 71 | |
| 72 | writel(reg, type_reg); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static irqreturn_t sdv_gpio_pub_irq_handler(int irq, void *data) |
| 77 | { |
| 78 | struct sdv_gpio_chip_data *sd = data; |
Andy Shevchenko | f3af44f | 2018-11-06 14:38:55 +0200 | [diff] [blame] | 79 | unsigned long irq_stat = readl(sd->gpio_pub_base + GPSTR); |
| 80 | int irq_bit; |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 81 | |
| 82 | irq_stat &= readl(sd->gpio_pub_base + GPIO_INT); |
| 83 | if (!irq_stat) |
| 84 | return IRQ_NONE; |
| 85 | |
Andy Shevchenko | f3af44f | 2018-11-06 14:38:55 +0200 | [diff] [blame] | 86 | for_each_set_bit(irq_bit, &irq_stat, 32) |
Marc Zyngier | dbd1c54 | 2021-05-04 17:42:18 +0100 | [diff] [blame] | 87 | generic_handle_domain_irq(sd->id, irq_bit); |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 88 | |
| 89 | return IRQ_HANDLED; |
| 90 | } |
| 91 | |
| 92 | static int sdv_xlate(struct irq_domain *h, struct device_node *node, |
| 93 | const u32 *intspec, u32 intsize, irq_hw_number_t *out_hwirq, |
| 94 | u32 *out_type) |
| 95 | { |
| 96 | u32 line, type; |
| 97 | |
Marc Zyngier | 5d4c9bc | 2015-10-13 12:51:29 +0100 | [diff] [blame] | 98 | if (node != irq_domain_get_of_node(h)) |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 99 | return -EINVAL; |
| 100 | |
| 101 | if (intsize < 2) |
| 102 | return -EINVAL; |
| 103 | |
| 104 | line = *intspec; |
| 105 | *out_hwirq = line; |
| 106 | |
| 107 | intspec++; |
| 108 | type = *intspec; |
| 109 | |
| 110 | switch (type) { |
| 111 | case IRQ_TYPE_LEVEL_LOW: |
| 112 | case IRQ_TYPE_LEVEL_HIGH: |
| 113 | *out_type = type; |
| 114 | break; |
| 115 | default: |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | return 0; |
| 119 | } |
| 120 | |
Krzysztof Kozlowski | 0b354dc | 2015-04-27 21:54:07 +0900 | [diff] [blame] | 121 | static const struct irq_domain_ops irq_domain_sdv_ops = { |
Grant Likely | 3ffc9ce | 2012-03-28 14:55:04 -0600 | [diff] [blame] | 122 | .xlate = sdv_xlate, |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 123 | }; |
| 124 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 125 | static int sdv_register_irqsupport(struct sdv_gpio_chip_data *sd, |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 126 | struct pci_dev *pdev) |
| 127 | { |
| 128 | struct irq_chip_type *ct; |
| 129 | int ret; |
| 130 | |
Bartosz Golaszewski | 74dd9eb | 2017-03-04 17:23:37 +0100 | [diff] [blame] | 131 | sd->irq_base = devm_irq_alloc_descs(&pdev->dev, -1, 0, |
| 132 | SDV_NUM_PUB_GPIOS, -1); |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 133 | if (sd->irq_base < 0) |
| 134 | return sd->irq_base; |
| 135 | |
| 136 | /* mask + ACK all interrupt sources */ |
| 137 | writel(0, sd->gpio_pub_base + GPIO_INT); |
| 138 | writel((1 << 11) - 1, sd->gpio_pub_base + GPSTR); |
| 139 | |
Bartosz Golaszewski | 74dd9eb | 2017-03-04 17:23:37 +0100 | [diff] [blame] | 140 | ret = devm_request_irq(&pdev->dev, pdev->irq, |
| 141 | sdv_gpio_pub_irq_handler, IRQF_SHARED, |
| 142 | "sdv_gpio", sd); |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 143 | if (ret) |
Bartosz Golaszewski | 74dd9eb | 2017-03-04 17:23:37 +0100 | [diff] [blame] | 144 | return ret; |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 145 | |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 146 | /* |
| 147 | * This gpio irq controller latches level irqs. Testing shows that if |
| 148 | * we unmask & ACK the IRQ before the source of the interrupt is gone |
| 149 | * then the interrupt is active again. |
| 150 | */ |
Andy Shevchenko | 9381fc5 | 2018-11-07 21:18:04 +0200 | [diff] [blame] | 151 | sd->gc = devm_irq_alloc_generic_chip(&pdev->dev, "sdv-gpio", 1, |
| 152 | sd->irq_base, |
| 153 | sd->gpio_pub_base, |
| 154 | handle_fasteoi_irq); |
Bartosz Golaszewski | 74dd9eb | 2017-03-04 17:23:37 +0100 | [diff] [blame] | 155 | if (!sd->gc) |
| 156 | return -ENOMEM; |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 157 | |
| 158 | sd->gc->private = sd; |
| 159 | ct = sd->gc->chip_types; |
| 160 | ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW; |
| 161 | ct->regs.eoi = GPSTR; |
| 162 | ct->regs.mask = GPIO_INT; |
| 163 | ct->chip.irq_mask = irq_gc_mask_clr_bit; |
| 164 | ct->chip.irq_unmask = irq_gc_mask_set_bit; |
| 165 | ct->chip.irq_eoi = irq_gc_eoi; |
| 166 | ct->chip.irq_set_type = sdv_gpio_pub_set_type; |
| 167 | |
| 168 | irq_setup_generic_chip(sd->gc, IRQ_MSK(SDV_NUM_PUB_GPIOS), |
| 169 | IRQ_GC_INIT_MASK_CACHE, IRQ_NOREQUEST, |
| 170 | IRQ_LEVEL | IRQ_NOPROBE); |
| 171 | |
Grant Likely | 3ffc9ce | 2012-03-28 14:55:04 -0600 | [diff] [blame] | 172 | sd->id = irq_domain_add_legacy(pdev->dev.of_node, SDV_NUM_PUB_GPIOS, |
| 173 | sd->irq_base, 0, &irq_domain_sdv_ops, sd); |
Bartosz Golaszewski | 74dd9eb | 2017-03-04 17:23:37 +0100 | [diff] [blame] | 174 | if (!sd->id) |
| 175 | return -ENODEV; |
| 176 | |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 177 | return 0; |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 180 | static int sdv_gpio_probe(struct pci_dev *pdev, |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 181 | const struct pci_device_id *pci_id) |
| 182 | { |
| 183 | struct sdv_gpio_chip_data *sd; |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 184 | int ret; |
| 185 | u32 mux_val; |
| 186 | |
Andy Shevchenko | 9381fc5 | 2018-11-07 21:18:04 +0200 | [diff] [blame] | 187 | sd = devm_kzalloc(&pdev->dev, sizeof(*sd), GFP_KERNEL); |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 188 | if (!sd) |
| 189 | return -ENOMEM; |
Andy Shevchenko | 9381fc5 | 2018-11-07 21:18:04 +0200 | [diff] [blame] | 190 | |
| 191 | ret = pcim_enable_device(pdev); |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 192 | if (ret) { |
| 193 | dev_err(&pdev->dev, "can't enable device.\n"); |
Andy Shevchenko | 9381fc5 | 2018-11-07 21:18:04 +0200 | [diff] [blame] | 194 | return ret; |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 195 | } |
| 196 | |
Andy Shevchenko | 9381fc5 | 2018-11-07 21:18:04 +0200 | [diff] [blame] | 197 | ret = pcim_iomap_regions(pdev, 1 << GPIO_BAR, DRV_NAME); |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 198 | if (ret) { |
| 199 | dev_err(&pdev->dev, "can't alloc PCI BAR #%d\n", GPIO_BAR); |
Andy Shevchenko | 9381fc5 | 2018-11-07 21:18:04 +0200 | [diff] [blame] | 200 | return ret; |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 201 | } |
| 202 | |
Andy Shevchenko | 9381fc5 | 2018-11-07 21:18:04 +0200 | [diff] [blame] | 203 | sd->gpio_pub_base = pcim_iomap_table(pdev)[GPIO_BAR]; |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 204 | |
Andy Shevchenko | 9381fc5 | 2018-11-07 21:18:04 +0200 | [diff] [blame] | 205 | ret = of_property_read_u32(pdev->dev.of_node, "intel,muxctl", &mux_val); |
| 206 | if (!ret) |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 207 | writel(mux_val, sd->gpio_pub_base + GPMUXCTL); |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 208 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 209 | ret = bgpio_init(&sd->chip, &pdev->dev, 4, |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 210 | sd->gpio_pub_base + GPINR, sd->gpio_pub_base + GPOUTR, |
Shawn Guo | 3e11f7b | 2012-05-19 21:34:58 +0800 | [diff] [blame] | 211 | NULL, sd->gpio_pub_base + GPOER, NULL, 0); |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 212 | if (ret) |
Andy Shevchenko | 9381fc5 | 2018-11-07 21:18:04 +0200 | [diff] [blame] | 213 | return ret; |
| 214 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 215 | sd->chip.ngpio = SDV_NUM_PUB_GPIOS; |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 216 | |
Andy Shevchenko | 9381fc5 | 2018-11-07 21:18:04 +0200 | [diff] [blame] | 217 | ret = devm_gpiochip_add_data(&pdev->dev, &sd->chip, sd); |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 218 | if (ret < 0) { |
| 219 | dev_err(&pdev->dev, "gpiochip_add() failed.\n"); |
Andy Shevchenko | 9381fc5 | 2018-11-07 21:18:04 +0200 | [diff] [blame] | 220 | return ret; |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | ret = sdv_register_irqsupport(sd, pdev); |
| 224 | if (ret) |
Andy Shevchenko | 9381fc5 | 2018-11-07 21:18:04 +0200 | [diff] [blame] | 225 | return ret; |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 226 | |
| 227 | pci_set_drvdata(pdev, sd); |
| 228 | dev_info(&pdev->dev, "Sodaville GPIO driver registered.\n"); |
| 229 | return 0; |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Jingoo Han | 14f4a88 | 2013-12-03 08:08:45 +0900 | [diff] [blame] | 232 | static const struct pci_device_id sdv_gpio_pci_ids[] = { |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 233 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_SDV_GPIO) }, |
| 234 | { 0, }, |
| 235 | }; |
| 236 | |
| 237 | static struct pci_driver sdv_gpio_driver = { |
Paul Gortmaker | 6a5ead9 | 2016-05-09 19:59:55 -0400 | [diff] [blame] | 238 | .driver = { |
| 239 | .suppress_bind_attrs = true, |
| 240 | }, |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 241 | .name = DRV_NAME, |
| 242 | .id_table = sdv_gpio_pci_ids, |
| 243 | .probe = sdv_gpio_probe, |
Sebastian Andrzej Siewior | b43ab90 | 2011-06-27 09:26:23 +0200 | [diff] [blame] | 244 | }; |
Paul Gortmaker | 6a5ead9 | 2016-05-09 19:59:55 -0400 | [diff] [blame] | 245 | builtin_pci_driver(sdv_gpio_driver); |