blob: c7d47e42fa2b1a7559e5efc431fab958290645cd [file] [log] [blame]
Yash Shah96868dc2019-12-10 16:41:13 +05301// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2019 SiFive
4 */
5
6#include <linux/bitops.h>
7#include <linux/device.h>
8#include <linux/errno.h>
9#include <linux/of_irq.h>
10#include <linux/gpio/driver.h>
11#include <linux/init.h>
12#include <linux/platform_device.h>
13#include <linux/slab.h>
14#include <linux/spinlock.h>
15#include <linux/regmap.h>
16
17#define SIFIVE_GPIO_INPUT_VAL 0x00
18#define SIFIVE_GPIO_INPUT_EN 0x04
19#define SIFIVE_GPIO_OUTPUT_EN 0x08
20#define SIFIVE_GPIO_OUTPUT_VAL 0x0C
21#define SIFIVE_GPIO_RISE_IE 0x18
22#define SIFIVE_GPIO_RISE_IP 0x1C
23#define SIFIVE_GPIO_FALL_IE 0x20
24#define SIFIVE_GPIO_FALL_IP 0x24
25#define SIFIVE_GPIO_HIGH_IE 0x28
26#define SIFIVE_GPIO_HIGH_IP 0x2C
27#define SIFIVE_GPIO_LOW_IE 0x30
28#define SIFIVE_GPIO_LOW_IP 0x34
29#define SIFIVE_GPIO_OUTPUT_XOR 0x40
30
31#define SIFIVE_GPIO_MAX 32
32#define SIFIVE_GPIO_IRQ_OFFSET 7
33
34struct sifive_gpio {
35 void __iomem *base;
36 struct gpio_chip gc;
37 struct regmap *regs;
Yash Shaha924eae2020-01-28 10:54:21 +053038 unsigned long irq_state;
Yash Shah96868dc2019-12-10 16:41:13 +053039 unsigned int trigger[SIFIVE_GPIO_MAX];
40 unsigned int irq_parent[SIFIVE_GPIO_MAX];
41};
42
43static void sifive_gpio_set_ie(struct sifive_gpio *chip, unsigned int offset)
44{
45 unsigned long flags;
46 unsigned int trigger;
47
48 spin_lock_irqsave(&chip->gc.bgpio_lock, flags);
49 trigger = (chip->irq_state & BIT(offset)) ? chip->trigger[offset] : 0;
50 regmap_update_bits(chip->regs, SIFIVE_GPIO_RISE_IE, BIT(offset),
51 (trigger & IRQ_TYPE_EDGE_RISING) ? BIT(offset) : 0);
52 regmap_update_bits(chip->regs, SIFIVE_GPIO_FALL_IE, BIT(offset),
53 (trigger & IRQ_TYPE_EDGE_FALLING) ? BIT(offset) : 0);
54 regmap_update_bits(chip->regs, SIFIVE_GPIO_HIGH_IE, BIT(offset),
55 (trigger & IRQ_TYPE_LEVEL_HIGH) ? BIT(offset) : 0);
56 regmap_update_bits(chip->regs, SIFIVE_GPIO_LOW_IE, BIT(offset),
57 (trigger & IRQ_TYPE_LEVEL_LOW) ? BIT(offset) : 0);
58 spin_unlock_irqrestore(&chip->gc.bgpio_lock, flags);
59}
60
61static int sifive_gpio_irq_set_type(struct irq_data *d, unsigned int trigger)
62{
63 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
64 struct sifive_gpio *chip = gpiochip_get_data(gc);
65 int offset = irqd_to_hwirq(d);
66
67 if (offset < 0 || offset >= gc->ngpio)
68 return -EINVAL;
69
70 chip->trigger[offset] = trigger;
71 sifive_gpio_set_ie(chip, offset);
72 return 0;
73}
74
75static void sifive_gpio_irq_enable(struct irq_data *d)
76{
77 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
78 struct sifive_gpio *chip = gpiochip_get_data(gc);
79 int offset = irqd_to_hwirq(d) % SIFIVE_GPIO_MAX;
80 u32 bit = BIT(offset);
81 unsigned long flags;
82
83 irq_chip_enable_parent(d);
84
85 /* Switch to input */
86 gc->direction_input(gc, offset);
87
88 spin_lock_irqsave(&gc->bgpio_lock, flags);
89 /* Clear any sticky pending interrupts */
90 regmap_write(chip->regs, SIFIVE_GPIO_RISE_IP, bit);
91 regmap_write(chip->regs, SIFIVE_GPIO_FALL_IP, bit);
92 regmap_write(chip->regs, SIFIVE_GPIO_HIGH_IP, bit);
93 regmap_write(chip->regs, SIFIVE_GPIO_LOW_IP, bit);
94 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
95
96 /* Enable interrupts */
Yash Shaha924eae2020-01-28 10:54:21 +053097 assign_bit(offset, &chip->irq_state, 1);
Yash Shah96868dc2019-12-10 16:41:13 +053098 sifive_gpio_set_ie(chip, offset);
99}
100
101static void sifive_gpio_irq_disable(struct irq_data *d)
102{
103 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
104 struct sifive_gpio *chip = gpiochip_get_data(gc);
105 int offset = irqd_to_hwirq(d) % SIFIVE_GPIO_MAX;
106
Yash Shaha924eae2020-01-28 10:54:21 +0530107 assign_bit(offset, &chip->irq_state, 0);
Yash Shah96868dc2019-12-10 16:41:13 +0530108 sifive_gpio_set_ie(chip, offset);
109 irq_chip_disable_parent(d);
110}
111
112static void sifive_gpio_irq_eoi(struct irq_data *d)
113{
114 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
115 struct sifive_gpio *chip = gpiochip_get_data(gc);
116 int offset = irqd_to_hwirq(d) % SIFIVE_GPIO_MAX;
117 u32 bit = BIT(offset);
118 unsigned long flags;
119
120 spin_lock_irqsave(&gc->bgpio_lock, flags);
121 /* Clear all pending interrupts */
122 regmap_write(chip->regs, SIFIVE_GPIO_RISE_IP, bit);
123 regmap_write(chip->regs, SIFIVE_GPIO_FALL_IP, bit);
124 regmap_write(chip->regs, SIFIVE_GPIO_HIGH_IP, bit);
125 regmap_write(chip->regs, SIFIVE_GPIO_LOW_IP, bit);
126 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
127
128 irq_chip_eoi_parent(d);
129}
130
Linus Walleij011a78c2020-11-17 22:33:50 +0100131static int sifive_gpio_irq_set_affinity(struct irq_data *data,
132 const struct cpumask *dest,
133 bool force)
134{
135 if (data->parent_data)
136 return irq_chip_set_affinity_parent(data, dest, force);
137
138 return -EINVAL;
139}
140
Yash Shah96868dc2019-12-10 16:41:13 +0530141static struct irq_chip sifive_gpio_irqchip = {
142 .name = "sifive-gpio",
143 .irq_set_type = sifive_gpio_irq_set_type,
144 .irq_mask = irq_chip_mask_parent,
145 .irq_unmask = irq_chip_unmask_parent,
146 .irq_enable = sifive_gpio_irq_enable,
147 .irq_disable = sifive_gpio_irq_disable,
148 .irq_eoi = sifive_gpio_irq_eoi,
Linus Walleij011a78c2020-11-17 22:33:50 +0100149 .irq_set_affinity = sifive_gpio_irq_set_affinity,
Yash Shah96868dc2019-12-10 16:41:13 +0530150};
151
152static int sifive_gpio_child_to_parent_hwirq(struct gpio_chip *gc,
153 unsigned int child,
154 unsigned int child_type,
155 unsigned int *parent,
156 unsigned int *parent_type)
157{
158 *parent_type = IRQ_TYPE_NONE;
159 *parent = child + SIFIVE_GPIO_IRQ_OFFSET;
160 return 0;
161}
162
163static const struct regmap_config sifive_gpio_regmap_config = {
164 .reg_bits = 32,
165 .reg_stride = 4,
166 .val_bits = 32,
167 .fast_io = true,
168 .disable_locking = true,
169};
170
171static int sifive_gpio_probe(struct platform_device *pdev)
172{
173 struct device *dev = &pdev->dev;
174 struct device_node *node = pdev->dev.of_node;
175 struct device_node *irq_parent;
176 struct irq_domain *parent;
177 struct gpio_irq_chip *girq;
178 struct sifive_gpio *chip;
179 int ret, ngpio;
180
181 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
182 if (!chip)
183 return -ENOMEM;
184
185 chip->base = devm_platform_ioremap_resource(pdev, 0);
186 if (IS_ERR(chip->base)) {
187 dev_err(dev, "failed to allocate device memory\n");
188 return PTR_ERR(chip->base);
189 }
190
191 chip->regs = devm_regmap_init_mmio(dev, chip->base,
192 &sifive_gpio_regmap_config);
193 if (IS_ERR(chip->regs))
194 return PTR_ERR(chip->regs);
195
196 ngpio = of_irq_count(node);
Damien Le Moalb72de3f2020-11-07 17:13:57 +0900197 if (ngpio > SIFIVE_GPIO_MAX) {
Yash Shah96868dc2019-12-10 16:41:13 +0530198 dev_err(dev, "Too many GPIO interrupts (max=%d)\n",
199 SIFIVE_GPIO_MAX);
200 return -ENXIO;
201 }
202
203 irq_parent = of_irq_find_parent(node);
204 if (!irq_parent) {
205 dev_err(dev, "no IRQ parent node\n");
206 return -ENODEV;
207 }
208 parent = irq_find_host(irq_parent);
209 if (!parent) {
210 dev_err(dev, "no IRQ parent domain\n");
211 return -ENODEV;
212 }
213
214 ret = bgpio_init(&chip->gc, dev, 4,
215 chip->base + SIFIVE_GPIO_INPUT_VAL,
216 chip->base + SIFIVE_GPIO_OUTPUT_VAL,
217 NULL,
218 chip->base + SIFIVE_GPIO_OUTPUT_EN,
219 chip->base + SIFIVE_GPIO_INPUT_EN,
220 0);
221 if (ret) {
222 dev_err(dev, "unable to init generic GPIO\n");
223 return ret;
224 }
225
226 /* Disable all GPIO interrupts before enabling parent interrupts */
227 regmap_write(chip->regs, SIFIVE_GPIO_RISE_IE, 0);
228 regmap_write(chip->regs, SIFIVE_GPIO_FALL_IE, 0);
229 regmap_write(chip->regs, SIFIVE_GPIO_HIGH_IE, 0);
230 regmap_write(chip->regs, SIFIVE_GPIO_LOW_IE, 0);
231 chip->irq_state = 0;
232
233 chip->gc.base = -1;
234 chip->gc.ngpio = ngpio;
235 chip->gc.label = dev_name(dev);
236 chip->gc.parent = dev;
237 chip->gc.owner = THIS_MODULE;
238 girq = &chip->gc.irq;
239 girq->chip = &sifive_gpio_irqchip;
240 girq->fwnode = of_node_to_fwnode(node);
241 girq->parent_domain = parent;
242 girq->child_to_parent_hwirq = sifive_gpio_child_to_parent_hwirq;
243 girq->handler = handle_bad_irq;
244 girq->default_type = IRQ_TYPE_NONE;
245
246 platform_set_drvdata(pdev, chip);
247 return gpiochip_add_data(&chip->gc, chip);
248}
249
250static const struct of_device_id sifive_gpio_match[] = {
251 { .compatible = "sifive,gpio0" },
252 { .compatible = "sifive,fu540-c000-gpio" },
253 { },
254};
255
256static struct platform_driver sifive_gpio_driver = {
257 .probe = sifive_gpio_probe,
258 .driver = {
259 .name = "sifive_gpio",
260 .of_match_table = of_match_ptr(sifive_gpio_match),
261 },
262};
263builtin_platform_driver(sifive_gpio_driver)