blob: 65eaff290b00a0a450351a8137e162723660108b [file] [log] [blame]
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +02001/*
2 * GPIO interface for Intel Sodaville SoCs.
3 *
4 * Copyright (c) 2010, 2011 Intel Corporation
5 *
Paul Gortmaker6a5ead92016-05-09 19:59:55 -04006 * Author: Hans J. Koch <hjk@linutronix.de>
7 *
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License 2 as published
10 * by the Free Software Foundation.
11 *
12 */
13
14#include <linux/errno.h>
Andy Shevchenko87009982018-09-04 14:26:25 +030015#include <linux/gpio/driver.h>
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +020016#include <linux/init.h>
Andy Shevchenko87009982018-09-04 14:26:25 +030017#include <linux/interrupt.h>
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +020018#include <linux/io.h>
19#include <linux/irq.h>
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +020020#include <linux/kernel.h>
Andy Shevchenko87009982018-09-04 14:26:25 +030021#include <linux/of_irq.h>
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +020022#include <linux/pci.h>
23#include <linux/platform_device.h>
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +020024
25#define DRV_NAME "sdv_gpio"
26#define SDV_NUM_PUB_GPIOS 12
27#define PCI_DEVICE_ID_SDV_GPIO 0x2e67
28#define GPIO_BAR 0
29
30#define GPOUTR 0x00
31#define GPOER 0x04
32#define GPINR 0x08
33
34#define GPSTR 0x0c
35#define GPIT1R0 0x10
36#define GPIO_INT 0x14
37#define GPIT1R1 0x18
38
39#define GPMUXCTL 0x1c
40
41struct sdv_gpio_chip_data {
42 int irq_base;
43 void __iomem *gpio_pub_base;
Grant Likely3ffc9ce2012-03-28 14:55:04 -060044 struct irq_domain *id;
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +020045 struct irq_chip_generic *gc;
Linus Walleij0f4630f2015-12-04 14:02:58 +010046 struct gpio_chip chip;
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +020047};
48
49static int sdv_gpio_pub_set_type(struct irq_data *d, unsigned int type)
50{
51 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
52 struct sdv_gpio_chip_data *sd = gc->private;
53 void __iomem *type_reg;
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +020054 u32 reg;
55
Grant Likely3ffc9ce2012-03-28 14:55:04 -060056 if (d->hwirq < 8)
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +020057 type_reg = sd->gpio_pub_base + GPIT1R0;
58 else
59 type_reg = sd->gpio_pub_base + GPIT1R1;
60
61 reg = readl(type_reg);
62
63 switch (type) {
64 case IRQ_TYPE_LEVEL_HIGH:
Grant Likely3ffc9ce2012-03-28 14:55:04 -060065 reg &= ~BIT(4 * (d->hwirq % 8));
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +020066 break;
67
68 case IRQ_TYPE_LEVEL_LOW:
Grant Likely3ffc9ce2012-03-28 14:55:04 -060069 reg |= BIT(4 * (d->hwirq % 8));
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +020070 break;
71
72 default:
73 return -EINVAL;
74 }
75
76 writel(reg, type_reg);
77 return 0;
78}
79
80static irqreturn_t sdv_gpio_pub_irq_handler(int irq, void *data)
81{
82 struct sdv_gpio_chip_data *sd = data;
Andy Shevchenkof3af44f2018-11-06 14:38:55 +020083 unsigned long irq_stat = readl(sd->gpio_pub_base + GPSTR);
84 int irq_bit;
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +020085
86 irq_stat &= readl(sd->gpio_pub_base + GPIO_INT);
87 if (!irq_stat)
88 return IRQ_NONE;
89
Andy Shevchenkof3af44f2018-11-06 14:38:55 +020090 for_each_set_bit(irq_bit, &irq_stat, 32)
Grant Likely3ffc9ce2012-03-28 14:55:04 -060091 generic_handle_irq(irq_find_mapping(sd->id, irq_bit));
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +020092
93 return IRQ_HANDLED;
94}
95
96static int sdv_xlate(struct irq_domain *h, struct device_node *node,
97 const u32 *intspec, u32 intsize, irq_hw_number_t *out_hwirq,
98 u32 *out_type)
99{
100 u32 line, type;
101
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100102 if (node != irq_domain_get_of_node(h))
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200103 return -EINVAL;
104
105 if (intsize < 2)
106 return -EINVAL;
107
108 line = *intspec;
109 *out_hwirq = line;
110
111 intspec++;
112 type = *intspec;
113
114 switch (type) {
115 case IRQ_TYPE_LEVEL_LOW:
116 case IRQ_TYPE_LEVEL_HIGH:
117 *out_type = type;
118 break;
119 default:
120 return -EINVAL;
121 }
122 return 0;
123}
124
Krzysztof Kozlowski0b354dc2015-04-27 21:54:07 +0900125static const struct irq_domain_ops irq_domain_sdv_ops = {
Grant Likely3ffc9ce2012-03-28 14:55:04 -0600126 .xlate = sdv_xlate,
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200127};
128
Bill Pemberton38363092012-11-19 13:22:34 -0500129static int sdv_register_irqsupport(struct sdv_gpio_chip_data *sd,
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200130 struct pci_dev *pdev)
131{
132 struct irq_chip_type *ct;
133 int ret;
134
Bartosz Golaszewski74dd9eb2017-03-04 17:23:37 +0100135 sd->irq_base = devm_irq_alloc_descs(&pdev->dev, -1, 0,
136 SDV_NUM_PUB_GPIOS, -1);
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200137 if (sd->irq_base < 0)
138 return sd->irq_base;
139
140 /* mask + ACK all interrupt sources */
141 writel(0, sd->gpio_pub_base + GPIO_INT);
142 writel((1 << 11) - 1, sd->gpio_pub_base + GPSTR);
143
Bartosz Golaszewski74dd9eb2017-03-04 17:23:37 +0100144 ret = devm_request_irq(&pdev->dev, pdev->irq,
145 sdv_gpio_pub_irq_handler, IRQF_SHARED,
146 "sdv_gpio", sd);
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200147 if (ret)
Bartosz Golaszewski74dd9eb2017-03-04 17:23:37 +0100148 return ret;
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200149
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200150 /*
151 * This gpio irq controller latches level irqs. Testing shows that if
152 * we unmask & ACK the IRQ before the source of the interrupt is gone
153 * then the interrupt is active again.
154 */
Andy Shevchenko9381fc52018-11-07 21:18:04 +0200155 sd->gc = devm_irq_alloc_generic_chip(&pdev->dev, "sdv-gpio", 1,
156 sd->irq_base,
157 sd->gpio_pub_base,
158 handle_fasteoi_irq);
Bartosz Golaszewski74dd9eb2017-03-04 17:23:37 +0100159 if (!sd->gc)
160 return -ENOMEM;
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200161
162 sd->gc->private = sd;
163 ct = sd->gc->chip_types;
164 ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
165 ct->regs.eoi = GPSTR;
166 ct->regs.mask = GPIO_INT;
167 ct->chip.irq_mask = irq_gc_mask_clr_bit;
168 ct->chip.irq_unmask = irq_gc_mask_set_bit;
169 ct->chip.irq_eoi = irq_gc_eoi;
170 ct->chip.irq_set_type = sdv_gpio_pub_set_type;
171
172 irq_setup_generic_chip(sd->gc, IRQ_MSK(SDV_NUM_PUB_GPIOS),
173 IRQ_GC_INIT_MASK_CACHE, IRQ_NOREQUEST,
174 IRQ_LEVEL | IRQ_NOPROBE);
175
Grant Likely3ffc9ce2012-03-28 14:55:04 -0600176 sd->id = irq_domain_add_legacy(pdev->dev.of_node, SDV_NUM_PUB_GPIOS,
177 sd->irq_base, 0, &irq_domain_sdv_ops, sd);
Bartosz Golaszewski74dd9eb2017-03-04 17:23:37 +0100178 if (!sd->id)
179 return -ENODEV;
180
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200181 return 0;
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200182}
183
Bill Pemberton38363092012-11-19 13:22:34 -0500184static int sdv_gpio_probe(struct pci_dev *pdev,
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200185 const struct pci_device_id *pci_id)
186{
187 struct sdv_gpio_chip_data *sd;
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200188 int ret;
189 u32 mux_val;
190
Andy Shevchenko9381fc52018-11-07 21:18:04 +0200191 sd = devm_kzalloc(&pdev->dev, sizeof(*sd), GFP_KERNEL);
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200192 if (!sd)
193 return -ENOMEM;
Andy Shevchenko9381fc52018-11-07 21:18:04 +0200194
195 ret = pcim_enable_device(pdev);
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200196 if (ret) {
197 dev_err(&pdev->dev, "can't enable device.\n");
Andy Shevchenko9381fc52018-11-07 21:18:04 +0200198 return ret;
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200199 }
200
Andy Shevchenko9381fc52018-11-07 21:18:04 +0200201 ret = pcim_iomap_regions(pdev, 1 << GPIO_BAR, DRV_NAME);
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200202 if (ret) {
203 dev_err(&pdev->dev, "can't alloc PCI BAR #%d\n", GPIO_BAR);
Andy Shevchenko9381fc52018-11-07 21:18:04 +0200204 return ret;
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200205 }
206
Andy Shevchenko9381fc52018-11-07 21:18:04 +0200207 sd->gpio_pub_base = pcim_iomap_table(pdev)[GPIO_BAR];
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200208
Andy Shevchenko9381fc52018-11-07 21:18:04 +0200209 ret = of_property_read_u32(pdev->dev.of_node, "intel,muxctl", &mux_val);
210 if (!ret)
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200211 writel(mux_val, sd->gpio_pub_base + GPMUXCTL);
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200212
Linus Walleij0f4630f2015-12-04 14:02:58 +0100213 ret = bgpio_init(&sd->chip, &pdev->dev, 4,
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200214 sd->gpio_pub_base + GPINR, sd->gpio_pub_base + GPOUTR,
Shawn Guo3e11f7b2012-05-19 21:34:58 +0800215 NULL, sd->gpio_pub_base + GPOER, NULL, 0);
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200216 if (ret)
Andy Shevchenko9381fc52018-11-07 21:18:04 +0200217 return ret;
218
Linus Walleij0f4630f2015-12-04 14:02:58 +0100219 sd->chip.ngpio = SDV_NUM_PUB_GPIOS;
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200220
Andy Shevchenko9381fc52018-11-07 21:18:04 +0200221 ret = devm_gpiochip_add_data(&pdev->dev, &sd->chip, sd);
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200222 if (ret < 0) {
223 dev_err(&pdev->dev, "gpiochip_add() failed.\n");
Andy Shevchenko9381fc52018-11-07 21:18:04 +0200224 return ret;
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200225 }
226
227 ret = sdv_register_irqsupport(sd, pdev);
228 if (ret)
Andy Shevchenko9381fc52018-11-07 21:18:04 +0200229 return ret;
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200230
231 pci_set_drvdata(pdev, sd);
232 dev_info(&pdev->dev, "Sodaville GPIO driver registered.\n");
233 return 0;
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200234}
235
Jingoo Han14f4a882013-12-03 08:08:45 +0900236static const struct pci_device_id sdv_gpio_pci_ids[] = {
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200237 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_SDV_GPIO) },
238 { 0, },
239};
240
241static struct pci_driver sdv_gpio_driver = {
Paul Gortmaker6a5ead92016-05-09 19:59:55 -0400242 .driver = {
243 .suppress_bind_attrs = true,
244 },
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200245 .name = DRV_NAME,
246 .id_table = sdv_gpio_pci_ids,
247 .probe = sdv_gpio_probe,
Sebastian Andrzej Siewiorb43ab902011-06-27 09:26:23 +0200248};
Paul Gortmaker6a5ead92016-05-09 19:59:55 -0400249builtin_pci_driver(sdv_gpio_driver);