blob: d329a143f5ec9480cea3809d8cc918ff91051056 [file] [log] [blame]
Markus Mayer757651e2013-09-10 11:07:01 -07001/*
Paul Gortmaker8f3e19f2016-03-27 11:44:41 -04002 * Broadcom Kona GPIO Driver
3 *
4 * Author: Broadcom Corporation <bcm-kernel-feedback-list@broadcom.com>
Markus Mayer6f587c92014-02-03 13:43:00 -08005 * Copyright (C) 2012-2014 Broadcom Corporation
Markus Mayer757651e2013-09-10 11:07:01 -07006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation version 2.
10 *
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/bitops.h>
18#include <linux/err.h>
19#include <linux/io.h>
Linus Walleij14ec0182018-01-13 22:52:33 +010020#include <linux/gpio/driver.h>
Markus Mayer757651e2013-09-10 11:07:01 -070021#include <linux/of_device.h>
Paul Gortmaker8f3e19f2016-03-27 11:44:41 -040022#include <linux/init.h>
Markus Mayer757651e2013-09-10 11:07:01 -070023#include <linux/irqdomain.h>
24#include <linux/irqchip/chained_irq.h>
25
26#define BCM_GPIO_PASSWD 0x00a5a501
27#define GPIO_PER_BANK 32
28#define GPIO_MAX_BANK_NUM 8
29
30#define GPIO_BANK(gpio) ((gpio) >> 5)
31#define GPIO_BIT(gpio) ((gpio) & (GPIO_PER_BANK - 1))
32
Markus Mayerd762bae2014-01-21 16:10:04 -080033/* There is a GPIO control register for each GPIO */
34#define GPIO_CONTROL(gpio) (0x00000100 + ((gpio) << 2))
35
36/* The remaining registers are per GPIO bank */
Markus Mayer757651e2013-09-10 11:07:01 -070037#define GPIO_OUT_STATUS(bank) (0x00000000 + ((bank) << 2))
38#define GPIO_IN_STATUS(bank) (0x00000020 + ((bank) << 2))
39#define GPIO_OUT_SET(bank) (0x00000040 + ((bank) << 2))
40#define GPIO_OUT_CLEAR(bank) (0x00000060 + ((bank) << 2))
41#define GPIO_INT_STATUS(bank) (0x00000080 + ((bank) << 2))
42#define GPIO_INT_MASK(bank) (0x000000a0 + ((bank) << 2))
43#define GPIO_INT_MSKCLR(bank) (0x000000c0 + ((bank) << 2))
Markus Mayer757651e2013-09-10 11:07:01 -070044#define GPIO_PWD_STATUS(bank) (0x00000500 + ((bank) << 2))
45
46#define GPIO_GPPWR_OFFSET 0x00000520
47
48#define GPIO_GPCTR0_DBR_SHIFT 5
49#define GPIO_GPCTR0_DBR_MASK 0x000001e0
50
51#define GPIO_GPCTR0_ITR_SHIFT 3
52#define GPIO_GPCTR0_ITR_MASK 0x00000018
53#define GPIO_GPCTR0_ITR_CMD_RISING_EDGE 0x00000001
54#define GPIO_GPCTR0_ITR_CMD_FALLING_EDGE 0x00000002
55#define GPIO_GPCTR0_ITR_CMD_BOTH_EDGE 0x00000003
56
57#define GPIO_GPCTR0_IOTR_MASK 0x00000001
58#define GPIO_GPCTR0_IOTR_CMD_0UTPUT 0x00000000
59#define GPIO_GPCTR0_IOTR_CMD_INPUT 0x00000001
60
61#define GPIO_GPCTR0_DB_ENABLE_MASK 0x00000100
62
63#define LOCK_CODE 0xffffffff
64#define UNLOCK_CODE 0x00000000
65
66struct bcm_kona_gpio {
67 void __iomem *reg_base;
68 int num_bank;
Julia Cartwrightc69fcea2017-03-09 10:21:54 -060069 raw_spinlock_t lock;
Markus Mayer757651e2013-09-10 11:07:01 -070070 struct gpio_chip gpio_chip;
71 struct irq_domain *irq_domain;
72 struct bcm_kona_gpio_bank *banks;
73 struct platform_device *pdev;
74};
75
76struct bcm_kona_gpio_bank {
77 int id;
78 int irq;
79 /* Used in the interrupt handler */
80 struct bcm_kona_gpio *kona_gpio;
81};
82
Markus Mayerbdb93c02014-01-21 16:10:41 -080083static inline void bcm_kona_gpio_write_lock_regs(void __iomem *reg_base,
84 int bank_id, u32 lockcode)
Markus Mayer757651e2013-09-10 11:07:01 -070085{
86 writel(BCM_GPIO_PASSWD, reg_base + GPIO_GPPWR_OFFSET);
87 writel(lockcode, reg_base + GPIO_PWD_STATUS(bank_id));
88}
89
Markus Mayerbdb93c02014-01-21 16:10:41 -080090static void bcm_kona_gpio_lock_gpio(struct bcm_kona_gpio *kona_gpio,
91 unsigned gpio)
Markus Mayer757651e2013-09-10 11:07:01 -070092{
Markus Mayerbdb93c02014-01-21 16:10:41 -080093 u32 val;
94 unsigned long flags;
95 int bank_id = GPIO_BANK(gpio);
96
Julia Cartwrightc69fcea2017-03-09 10:21:54 -060097 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
Markus Mayerbdb93c02014-01-21 16:10:41 -080098
99 val = readl(kona_gpio->reg_base + GPIO_PWD_STATUS(bank_id));
100 val |= BIT(gpio);
101 bcm_kona_gpio_write_lock_regs(kona_gpio->reg_base, bank_id, val);
102
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600103 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700104}
105
Markus Mayerbdb93c02014-01-21 16:10:41 -0800106static void bcm_kona_gpio_unlock_gpio(struct bcm_kona_gpio *kona_gpio,
107 unsigned gpio)
Markus Mayer757651e2013-09-10 11:07:01 -0700108{
Markus Mayerbdb93c02014-01-21 16:10:41 -0800109 u32 val;
110 unsigned long flags;
111 int bank_id = GPIO_BANK(gpio);
112
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600113 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
Markus Mayerbdb93c02014-01-21 16:10:41 -0800114
115 val = readl(kona_gpio->reg_base + GPIO_PWD_STATUS(bank_id));
116 val &= ~BIT(gpio);
117 bcm_kona_gpio_write_lock_regs(kona_gpio->reg_base, bank_id, val);
118
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600119 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700120}
121
Axel Lin0218d5a2015-04-13 15:56:00 +0800122static int bcm_kona_gpio_get_dir(struct gpio_chip *chip, unsigned gpio)
123{
Linus Walleijba4a7442015-12-04 15:39:50 +0100124 struct bcm_kona_gpio *kona_gpio = gpiochip_get_data(chip);
Axel Lin0218d5a2015-04-13 15:56:00 +0800125 void __iomem *reg_base = kona_gpio->reg_base;
126 u32 val;
127
128 val = readl(reg_base + GPIO_CONTROL(gpio)) & GPIO_GPCTR0_IOTR_MASK;
Matti Vaittinene42615e2019-11-06 10:54:12 +0200129 return val ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT;
Axel Lin0218d5a2015-04-13 15:56:00 +0800130}
131
Markus Mayer757651e2013-09-10 11:07:01 -0700132static void bcm_kona_gpio_set(struct gpio_chip *chip, unsigned gpio, int value)
133{
134 struct bcm_kona_gpio *kona_gpio;
135 void __iomem *reg_base;
136 int bank_id = GPIO_BANK(gpio);
137 int bit = GPIO_BIT(gpio);
138 u32 val, reg_offset;
139 unsigned long flags;
140
Linus Walleijba4a7442015-12-04 15:39:50 +0100141 kona_gpio = gpiochip_get_data(chip);
Markus Mayer757651e2013-09-10 11:07:01 -0700142 reg_base = kona_gpio->reg_base;
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600143 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700144
Markus Mayer757651e2013-09-10 11:07:01 -0700145 /* this function only applies to output pin */
Matti Vaittinene42615e2019-11-06 10:54:12 +0200146 if (bcm_kona_gpio_get_dir(chip, gpio) == GPIO_LINE_DIRECTION_IN)
Markus Mayer757651e2013-09-10 11:07:01 -0700147 goto out;
148
149 reg_offset = value ? GPIO_OUT_SET(bank_id) : GPIO_OUT_CLEAR(bank_id);
150
151 val = readl(reg_base + reg_offset);
152 val |= BIT(bit);
153 writel(val, reg_base + reg_offset);
154
155out:
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600156 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700157}
158
159static int bcm_kona_gpio_get(struct gpio_chip *chip, unsigned gpio)
160{
161 struct bcm_kona_gpio *kona_gpio;
162 void __iomem *reg_base;
163 int bank_id = GPIO_BANK(gpio);
164 int bit = GPIO_BIT(gpio);
165 u32 val, reg_offset;
166 unsigned long flags;
167
Linus Walleijba4a7442015-12-04 15:39:50 +0100168 kona_gpio = gpiochip_get_data(chip);
Markus Mayer757651e2013-09-10 11:07:01 -0700169 reg_base = kona_gpio->reg_base;
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600170 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700171
Matti Vaittinene42615e2019-11-06 10:54:12 +0200172 if (bcm_kona_gpio_get_dir(chip, gpio) == GPIO_LINE_DIRECTION_IN)
Axel Lin0218d5a2015-04-13 15:56:00 +0800173 reg_offset = GPIO_IN_STATUS(bank_id);
174 else
175 reg_offset = GPIO_OUT_STATUS(bank_id);
Markus Mayer757651e2013-09-10 11:07:01 -0700176
177 /* read the GPIO bank status */
Markus Mayer757651e2013-09-10 11:07:01 -0700178 val = readl(reg_base + reg_offset);
179
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600180 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700181
182 /* return the specified bit status */
Markus Mayere2f0b002013-11-21 15:12:46 -0800183 return !!(val & BIT(bit));
Markus Mayer757651e2013-09-10 11:07:01 -0700184}
185
Markus Mayerbdb93c02014-01-21 16:10:41 -0800186static int bcm_kona_gpio_request(struct gpio_chip *chip, unsigned gpio)
187{
Linus Walleijba4a7442015-12-04 15:39:50 +0100188 struct bcm_kona_gpio *kona_gpio = gpiochip_get_data(chip);
Markus Mayerbdb93c02014-01-21 16:10:41 -0800189
190 bcm_kona_gpio_unlock_gpio(kona_gpio, gpio);
191 return 0;
192}
193
194static void bcm_kona_gpio_free(struct gpio_chip *chip, unsigned gpio)
195{
Linus Walleijba4a7442015-12-04 15:39:50 +0100196 struct bcm_kona_gpio *kona_gpio = gpiochip_get_data(chip);
Markus Mayerbdb93c02014-01-21 16:10:41 -0800197
198 bcm_kona_gpio_lock_gpio(kona_gpio, gpio);
199}
200
Markus Mayer757651e2013-09-10 11:07:01 -0700201static int bcm_kona_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
202{
203 struct bcm_kona_gpio *kona_gpio;
204 void __iomem *reg_base;
205 u32 val;
206 unsigned long flags;
Markus Mayer757651e2013-09-10 11:07:01 -0700207
Linus Walleijba4a7442015-12-04 15:39:50 +0100208 kona_gpio = gpiochip_get_data(chip);
Markus Mayer757651e2013-09-10 11:07:01 -0700209 reg_base = kona_gpio->reg_base;
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600210 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700211
212 val = readl(reg_base + GPIO_CONTROL(gpio));
213 val &= ~GPIO_GPCTR0_IOTR_MASK;
214 val |= GPIO_GPCTR0_IOTR_CMD_INPUT;
215 writel(val, reg_base + GPIO_CONTROL(gpio));
216
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600217 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700218
219 return 0;
220}
221
222static int bcm_kona_gpio_direction_output(struct gpio_chip *chip,
223 unsigned gpio, int value)
224{
225 struct bcm_kona_gpio *kona_gpio;
226 void __iomem *reg_base;
227 int bank_id = GPIO_BANK(gpio);
228 int bit = GPIO_BIT(gpio);
229 u32 val, reg_offset;
230 unsigned long flags;
231
Linus Walleijba4a7442015-12-04 15:39:50 +0100232 kona_gpio = gpiochip_get_data(chip);
Markus Mayer757651e2013-09-10 11:07:01 -0700233 reg_base = kona_gpio->reg_base;
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600234 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700235
236 val = readl(reg_base + GPIO_CONTROL(gpio));
237 val &= ~GPIO_GPCTR0_IOTR_MASK;
238 val |= GPIO_GPCTR0_IOTR_CMD_0UTPUT;
239 writel(val, reg_base + GPIO_CONTROL(gpio));
240 reg_offset = value ? GPIO_OUT_SET(bank_id) : GPIO_OUT_CLEAR(bank_id);
241
242 val = readl(reg_base + reg_offset);
243 val |= BIT(bit);
244 writel(val, reg_base + reg_offset);
245
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600246 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700247
248 return 0;
249}
250
251static int bcm_kona_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
252{
253 struct bcm_kona_gpio *kona_gpio;
254
Linus Walleijba4a7442015-12-04 15:39:50 +0100255 kona_gpio = gpiochip_get_data(chip);
Markus Mayer757651e2013-09-10 11:07:01 -0700256 if (gpio >= kona_gpio->gpio_chip.ngpio)
257 return -ENXIO;
258 return irq_create_mapping(kona_gpio->irq_domain, gpio);
259}
260
261static int bcm_kona_gpio_set_debounce(struct gpio_chip *chip, unsigned gpio,
262 unsigned debounce)
263{
264 struct bcm_kona_gpio *kona_gpio;
265 void __iomem *reg_base;
266 u32 val, res;
267 unsigned long flags;
Markus Mayer757651e2013-09-10 11:07:01 -0700268
Linus Walleijba4a7442015-12-04 15:39:50 +0100269 kona_gpio = gpiochip_get_data(chip);
Markus Mayer757651e2013-09-10 11:07:01 -0700270 reg_base = kona_gpio->reg_base;
271 /* debounce must be 1-128ms (or 0) */
272 if ((debounce > 0 && debounce < 1000) || debounce > 128000) {
Linus Walleij58383c782015-11-04 09:56:26 +0100273 dev_err(chip->parent, "Debounce value %u not in range\n",
Markus Mayer757651e2013-09-10 11:07:01 -0700274 debounce);
275 return -EINVAL;
276 }
277
278 /* calculate debounce bit value */
279 if (debounce != 0) {
280 /* Convert to ms */
281 debounce /= 1000;
282 /* find the MSB */
283 res = fls(debounce) - 1;
284 /* Check if MSB-1 is set (round up or down) */
285 if (res > 0 && (debounce & BIT(res - 1)))
286 res++;
287 }
288
289 /* spin lock for read-modify-write of the GPIO register */
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600290 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700291
292 val = readl(reg_base + GPIO_CONTROL(gpio));
293 val &= ~GPIO_GPCTR0_DBR_MASK;
294
295 if (debounce == 0) {
296 /* disable debounce */
297 val &= ~GPIO_GPCTR0_DB_ENABLE_MASK;
298 } else {
299 val |= GPIO_GPCTR0_DB_ENABLE_MASK |
300 (res << GPIO_GPCTR0_DBR_SHIFT);
301 }
302
303 writel(val, reg_base + GPIO_CONTROL(gpio));
304
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600305 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700306
307 return 0;
308}
309
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300310static int bcm_kona_gpio_set_config(struct gpio_chip *chip, unsigned gpio,
311 unsigned long config)
312{
313 u32 debounce;
314
315 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
316 return -ENOTSUPP;
317
318 debounce = pinconf_to_config_argument(config);
319 return bcm_kona_gpio_set_debounce(chip, gpio, debounce);
320}
321
Julia Lawalle35b5ab2016-09-11 14:14:37 +0200322static const struct gpio_chip template_chip = {
Markus Mayer757651e2013-09-10 11:07:01 -0700323 .label = "bcm-kona-gpio",
Wei Yongjunafb36902013-10-29 11:49:20 +0800324 .owner = THIS_MODULE,
Markus Mayerbdb93c02014-01-21 16:10:41 -0800325 .request = bcm_kona_gpio_request,
326 .free = bcm_kona_gpio_free,
Axel Lin0218d5a2015-04-13 15:56:00 +0800327 .get_direction = bcm_kona_gpio_get_dir,
Markus Mayer757651e2013-09-10 11:07:01 -0700328 .direction_input = bcm_kona_gpio_direction_input,
329 .get = bcm_kona_gpio_get,
330 .direction_output = bcm_kona_gpio_direction_output,
331 .set = bcm_kona_gpio_set,
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300332 .set_config = bcm_kona_gpio_set_config,
Markus Mayer757651e2013-09-10 11:07:01 -0700333 .to_irq = bcm_kona_gpio_to_irq,
334 .base = 0,
335};
336
337static void bcm_kona_gpio_irq_ack(struct irq_data *d)
338{
339 struct bcm_kona_gpio *kona_gpio;
340 void __iomem *reg_base;
Markus Mayerb7ab6972014-01-27 17:32:18 -0800341 unsigned gpio = d->hwirq;
Markus Mayer757651e2013-09-10 11:07:01 -0700342 int bank_id = GPIO_BANK(gpio);
343 int bit = GPIO_BIT(gpio);
344 u32 val;
345 unsigned long flags;
346
347 kona_gpio = irq_data_get_irq_chip_data(d);
348 reg_base = kona_gpio->reg_base;
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600349 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700350
351 val = readl(reg_base + GPIO_INT_STATUS(bank_id));
352 val |= BIT(bit);
353 writel(val, reg_base + GPIO_INT_STATUS(bank_id));
354
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600355 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700356}
357
358static void bcm_kona_gpio_irq_mask(struct irq_data *d)
359{
360 struct bcm_kona_gpio *kona_gpio;
361 void __iomem *reg_base;
Markus Mayerb7ab6972014-01-27 17:32:18 -0800362 unsigned gpio = d->hwirq;
Markus Mayer757651e2013-09-10 11:07:01 -0700363 int bank_id = GPIO_BANK(gpio);
364 int bit = GPIO_BIT(gpio);
365 u32 val;
366 unsigned long flags;
367
368 kona_gpio = irq_data_get_irq_chip_data(d);
369 reg_base = kona_gpio->reg_base;
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600370 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700371
372 val = readl(reg_base + GPIO_INT_MASK(bank_id));
373 val |= BIT(bit);
374 writel(val, reg_base + GPIO_INT_MASK(bank_id));
Hans Verkuil1c939cb2018-09-08 11:23:19 +0200375 gpiochip_disable_irq(&kona_gpio->gpio_chip, gpio);
Markus Mayer757651e2013-09-10 11:07:01 -0700376
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600377 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700378}
379
380static void bcm_kona_gpio_irq_unmask(struct irq_data *d)
381{
382 struct bcm_kona_gpio *kona_gpio;
383 void __iomem *reg_base;
Markus Mayerb7ab6972014-01-27 17:32:18 -0800384 unsigned gpio = d->hwirq;
Markus Mayer757651e2013-09-10 11:07:01 -0700385 int bank_id = GPIO_BANK(gpio);
386 int bit = GPIO_BIT(gpio);
387 u32 val;
388 unsigned long flags;
389
390 kona_gpio = irq_data_get_irq_chip_data(d);
391 reg_base = kona_gpio->reg_base;
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600392 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700393
394 val = readl(reg_base + GPIO_INT_MSKCLR(bank_id));
395 val |= BIT(bit);
396 writel(val, reg_base + GPIO_INT_MSKCLR(bank_id));
Hans Verkuil1c939cb2018-09-08 11:23:19 +0200397 gpiochip_enable_irq(&kona_gpio->gpio_chip, gpio);
Markus Mayer757651e2013-09-10 11:07:01 -0700398
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600399 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700400}
401
402static int bcm_kona_gpio_irq_set_type(struct irq_data *d, unsigned int type)
403{
404 struct bcm_kona_gpio *kona_gpio;
405 void __iomem *reg_base;
Markus Mayerb7ab6972014-01-27 17:32:18 -0800406 unsigned gpio = d->hwirq;
Markus Mayer757651e2013-09-10 11:07:01 -0700407 u32 lvl_type;
408 u32 val;
409 unsigned long flags;
Markus Mayer757651e2013-09-10 11:07:01 -0700410
411 kona_gpio = irq_data_get_irq_chip_data(d);
412 reg_base = kona_gpio->reg_base;
413 switch (type & IRQ_TYPE_SENSE_MASK) {
414 case IRQ_TYPE_EDGE_RISING:
415 lvl_type = GPIO_GPCTR0_ITR_CMD_RISING_EDGE;
416 break;
417
418 case IRQ_TYPE_EDGE_FALLING:
419 lvl_type = GPIO_GPCTR0_ITR_CMD_FALLING_EDGE;
420 break;
421
422 case IRQ_TYPE_EDGE_BOTH:
423 lvl_type = GPIO_GPCTR0_ITR_CMD_BOTH_EDGE;
424 break;
425
426 case IRQ_TYPE_LEVEL_HIGH:
427 case IRQ_TYPE_LEVEL_LOW:
428 /* BCM GPIO doesn't support level triggering */
429 default:
Linus Walleij58383c782015-11-04 09:56:26 +0100430 dev_err(kona_gpio->gpio_chip.parent,
Markus Mayer757651e2013-09-10 11:07:01 -0700431 "Invalid BCM GPIO irq type 0x%x\n", type);
432 return -EINVAL;
433 }
434
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600435 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700436
437 val = readl(reg_base + GPIO_CONTROL(gpio));
438 val &= ~GPIO_GPCTR0_ITR_MASK;
439 val |= lvl_type << GPIO_GPCTR0_ITR_SHIFT;
440 writel(val, reg_base + GPIO_CONTROL(gpio));
441
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600442 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
Markus Mayer757651e2013-09-10 11:07:01 -0700443
444 return 0;
445}
446
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200447static void bcm_kona_gpio_irq_handler(struct irq_desc *desc)
Markus Mayer757651e2013-09-10 11:07:01 -0700448{
449 void __iomem *reg_base;
450 int bit, bank_id;
451 unsigned long sta;
Jiang Liu476f8b42015-06-04 12:13:15 +0800452 struct bcm_kona_gpio_bank *bank = irq_desc_get_handler_data(desc);
Markus Mayer757651e2013-09-10 11:07:01 -0700453 struct irq_chip *chip = irq_desc_get_chip(desc);
454
455 chained_irq_enter(chip, desc);
456
457 /*
458 * For bank interrupts, we can't use chip_data to store the kona_gpio
459 * pointer, since GIC needs it for its own purposes. Therefore, we get
460 * our pointer from the bank structure.
461 */
462 reg_base = bank->kona_gpio->reg_base;
463 bank_id = bank->id;
Markus Mayer757651e2013-09-10 11:07:01 -0700464
465 while ((sta = readl(reg_base + GPIO_INT_STATUS(bank_id)) &
466 (~(readl(reg_base + GPIO_INT_MASK(bank_id)))))) {
467 for_each_set_bit(bit, &sta, 32) {
Linus Walleijd933cc62013-10-11 19:14:50 +0200468 int hwirq = GPIO_PER_BANK * bank_id + bit;
Markus Mayer757651e2013-09-10 11:07:01 -0700469 /*
470 * Clear interrupt before handler is called so we don't
471 * miss any interrupt occurred during executing them.
472 */
473 writel(readl(reg_base + GPIO_INT_STATUS(bank_id)) |
474 BIT(bit), reg_base + GPIO_INT_STATUS(bank_id));
475 /* Invoke interrupt handler */
Marc Zyngierdbd1c542021-05-04 17:42:18 +0100476 generic_handle_domain_irq(bank->kona_gpio->irq_domain,
477 hwirq);
Markus Mayer757651e2013-09-10 11:07:01 -0700478 }
479 }
480
Markus Mayer757651e2013-09-10 11:07:01 -0700481 chained_irq_exit(chip, desc);
482}
483
Linus Walleij57ef0422014-03-14 18:16:20 +0100484static int bcm_kona_gpio_irq_reqres(struct irq_data *d)
Linus Walleijdb6b3ad2013-11-19 14:14:50 +0100485{
486 struct bcm_kona_gpio *kona_gpio = irq_data_get_irq_chip_data(d);
487
Hans Verkuil1c939cb2018-09-08 11:23:19 +0200488 return gpiochip_reqres_irq(&kona_gpio->gpio_chip, d->hwirq);
Linus Walleijdb6b3ad2013-11-19 14:14:50 +0100489}
490
Linus Walleij57ef0422014-03-14 18:16:20 +0100491static void bcm_kona_gpio_irq_relres(struct irq_data *d)
Linus Walleijdb6b3ad2013-11-19 14:14:50 +0100492{
493 struct bcm_kona_gpio *kona_gpio = irq_data_get_irq_chip_data(d);
494
Hans Verkuil1c939cb2018-09-08 11:23:19 +0200495 gpiochip_relres_irq(&kona_gpio->gpio_chip, d->hwirq);
Linus Walleijdb6b3ad2013-11-19 14:14:50 +0100496}
497
Markus Mayer757651e2013-09-10 11:07:01 -0700498static struct irq_chip bcm_gpio_irq_chip = {
499 .name = "bcm-kona-gpio",
500 .irq_ack = bcm_kona_gpio_irq_ack,
501 .irq_mask = bcm_kona_gpio_irq_mask,
502 .irq_unmask = bcm_kona_gpio_irq_unmask,
503 .irq_set_type = bcm_kona_gpio_irq_set_type,
Linus Walleij57ef0422014-03-14 18:16:20 +0100504 .irq_request_resources = bcm_kona_gpio_irq_reqres,
505 .irq_release_resources = bcm_kona_gpio_irq_relres,
Markus Mayer757651e2013-09-10 11:07:01 -0700506};
507
Behan Webster37781292014-09-23 15:55:07 -0700508static struct of_device_id const bcm_kona_gpio_of_match[] = {
Markus Mayer757651e2013-09-10 11:07:01 -0700509 { .compatible = "brcm,kona-gpio" },
510 {}
511};
512
Markus Mayer757651e2013-09-10 11:07:01 -0700513/*
514 * This lock class tells lockdep that GPIO irqs are in a different
515 * category than their parents, so it won't report false recursion.
516 */
517static struct lock_class_key gpio_lock_class;
Andrew Lunn39c3fd52017-12-02 18:11:04 +0100518static struct lock_class_key gpio_request_class;
Markus Mayer757651e2013-09-10 11:07:01 -0700519
Linus Walleij1dc94272013-09-20 23:14:18 +0200520static int bcm_kona_gpio_irq_map(struct irq_domain *d, unsigned int irq,
Markus Mayer757651e2013-09-10 11:07:01 -0700521 irq_hw_number_t hwirq)
522{
523 int ret;
524
Linus Walleij1dc94272013-09-20 23:14:18 +0200525 ret = irq_set_chip_data(irq, d->host_data);
Markus Mayer757651e2013-09-10 11:07:01 -0700526 if (ret < 0)
527 return ret;
Andrew Lunn39c3fd52017-12-02 18:11:04 +0100528 irq_set_lockdep_class(irq, &gpio_lock_class, &gpio_request_class);
Linus Walleij1dc94272013-09-20 23:14:18 +0200529 irq_set_chip_and_handler(irq, &bcm_gpio_irq_chip, handle_simple_irq);
Linus Walleij1dc94272013-09-20 23:14:18 +0200530 irq_set_noprobe(irq);
Markus Mayer757651e2013-09-10 11:07:01 -0700531
532 return 0;
533}
534
Linus Walleijd933cc62013-10-11 19:14:50 +0200535static void bcm_kona_gpio_irq_unmap(struct irq_domain *d, unsigned int irq)
Markus Mayer757651e2013-09-10 11:07:01 -0700536{
Linus Walleijd933cc62013-10-11 19:14:50 +0200537 irq_set_chip_and_handler(irq, NULL, NULL);
538 irq_set_chip_data(irq, NULL);
Markus Mayer757651e2013-09-10 11:07:01 -0700539}
540
Krzysztof Kozlowski0b354dc2015-04-27 21:54:07 +0900541static const struct irq_domain_ops bcm_kona_irq_ops = {
Markus Mayer757651e2013-09-10 11:07:01 -0700542 .map = bcm_kona_gpio_irq_map,
543 .unmap = bcm_kona_gpio_irq_unmap,
544 .xlate = irq_domain_xlate_twocell,
545};
546
547static void bcm_kona_gpio_reset(struct bcm_kona_gpio *kona_gpio)
548{
549 void __iomem *reg_base;
550 int i;
551
552 reg_base = kona_gpio->reg_base;
553 /* disable interrupts and clear status */
554 for (i = 0; i < kona_gpio->num_bank; i++) {
Markus Mayerbdb93c02014-01-21 16:10:41 -0800555 /* Unlock the entire bank first */
Ben Dooksb66b2a02016-06-07 17:22:17 +0100556 bcm_kona_gpio_write_lock_regs(reg_base, i, UNLOCK_CODE);
Markus Mayer757651e2013-09-10 11:07:01 -0700557 writel(0xffffffff, reg_base + GPIO_INT_MASK(i));
558 writel(0xffffffff, reg_base + GPIO_INT_STATUS(i));
Markus Mayerbdb93c02014-01-21 16:10:41 -0800559 /* Now re-lock the bank */
Ben Dooksb66b2a02016-06-07 17:22:17 +0100560 bcm_kona_gpio_write_lock_regs(reg_base, i, LOCK_CODE);
Markus Mayer757651e2013-09-10 11:07:01 -0700561 }
562}
563
564static int bcm_kona_gpio_probe(struct platform_device *pdev)
565{
566 struct device *dev = &pdev->dev;
567 const struct of_device_id *match;
Markus Mayer757651e2013-09-10 11:07:01 -0700568 struct bcm_kona_gpio_bank *bank;
569 struct bcm_kona_gpio *kona_gpio;
570 struct gpio_chip *chip;
571 int ret;
572 int i;
573
574 match = of_match_device(bcm_kona_gpio_of_match, dev);
575 if (!match) {
576 dev_err(dev, "Failed to find gpio controller\n");
577 return -ENODEV;
578 }
579
580 kona_gpio = devm_kzalloc(dev, sizeof(*kona_gpio), GFP_KERNEL);
581 if (!kona_gpio)
582 return -ENOMEM;
583
584 kona_gpio->gpio_chip = template_chip;
585 chip = &kona_gpio->gpio_chip;
Peng Fancfdca142019-12-04 09:24:39 +0000586 ret = platform_irq_count(pdev);
587 if (!ret) {
Markus Mayer757651e2013-09-10 11:07:01 -0700588 dev_err(dev, "Couldn't determine # GPIO banks\n");
589 return -ENOENT;
Peng Fancfdca142019-12-04 09:24:39 +0000590 } else if (ret < 0) {
Krzysztof Kozlowskicff9d732020-08-27 22:08:22 +0200591 return dev_err_probe(dev, ret, "Couldn't determine GPIO banks\n");
Markus Mayer757651e2013-09-10 11:07:01 -0700592 }
Peng Fancfdca142019-12-04 09:24:39 +0000593 kona_gpio->num_bank = ret;
594
Markus Mayer757651e2013-09-10 11:07:01 -0700595 if (kona_gpio->num_bank > GPIO_MAX_BANK_NUM) {
596 dev_err(dev, "Too many GPIO banks configured (max=%d)\n",
597 GPIO_MAX_BANK_NUM);
598 return -ENXIO;
599 }
Kees Cooka86854d2018-06-12 14:07:58 -0700600 kona_gpio->banks = devm_kcalloc(dev,
601 kona_gpio->num_bank,
602 sizeof(*kona_gpio->banks),
603 GFP_KERNEL);
Markus Mayer757651e2013-09-10 11:07:01 -0700604 if (!kona_gpio->banks)
605 return -ENOMEM;
606
607 kona_gpio->pdev = pdev;
608 platform_set_drvdata(pdev, kona_gpio);
609 chip->of_node = dev->of_node;
610 chip->ngpio = kona_gpio->num_bank * GPIO_PER_BANK;
611
612 kona_gpio->irq_domain = irq_domain_add_linear(dev->of_node,
613 chip->ngpio,
614 &bcm_kona_irq_ops,
615 kona_gpio);
616 if (!kona_gpio->irq_domain) {
617 dev_err(dev, "Couldn't allocate IRQ domain\n");
618 return -ENXIO;
619 }
620
Enrico Weigelt, metux IT consult72d8cb72019-03-11 19:54:44 +0100621 kona_gpio->reg_base = devm_platform_ioremap_resource(pdev, 0);
Markus Mayer757651e2013-09-10 11:07:01 -0700622 if (IS_ERR(kona_gpio->reg_base)) {
Tiezhu Yang98f7d1b2020-05-22 12:12:18 +0800623 ret = PTR_ERR(kona_gpio->reg_base);
Markus Mayer757651e2013-09-10 11:07:01 -0700624 goto err_irq_domain;
625 }
626
627 for (i = 0; i < kona_gpio->num_bank; i++) {
628 bank = &kona_gpio->banks[i];
629 bank->id = i;
630 bank->irq = platform_get_irq(pdev, i);
631 bank->kona_gpio = kona_gpio;
632 if (bank->irq < 0) {
633 dev_err(dev, "Couldn't get IRQ for bank %d", i);
634 ret = -ENOENT;
635 goto err_irq_domain;
636 }
637 }
638
Markus Mayer23b4faa2013-10-18 11:50:03 -0700639 dev_info(&pdev->dev, "Setting up Kona GPIO\n");
Markus Mayer757651e2013-09-10 11:07:01 -0700640
641 bcm_kona_gpio_reset(kona_gpio);
642
Laxman Dewangan0b893122016-02-22 17:43:28 +0530643 ret = devm_gpiochip_add_data(dev, chip, kona_gpio);
Markus Mayer757651e2013-09-10 11:07:01 -0700644 if (ret < 0) {
645 dev_err(dev, "Couldn't add GPIO chip -- %d\n", ret);
646 goto err_irq_domain;
647 }
Markus Mayer757651e2013-09-10 11:07:01 -0700648 for (i = 0; i < kona_gpio->num_bank; i++) {
649 bank = &kona_gpio->banks[i];
Thomas Gleixnerb34cc6202015-06-21 20:16:04 +0200650 irq_set_chained_handler_and_data(bank->irq,
651 bcm_kona_gpio_irq_handler,
652 bank);
Markus Mayer757651e2013-09-10 11:07:01 -0700653 }
654
Julia Cartwrightc69fcea2017-03-09 10:21:54 -0600655 raw_spin_lock_init(&kona_gpio->lock);
Markus Mayer757651e2013-09-10 11:07:01 -0700656
657 return 0;
658
659err_irq_domain:
660 irq_domain_remove(kona_gpio->irq_domain);
661
662 return ret;
663}
664
665static struct platform_driver bcm_kona_gpio_driver = {
666 .driver = {
667 .name = "bcm-kona-gpio",
Markus Mayer757651e2013-09-10 11:07:01 -0700668 .of_match_table = bcm_kona_gpio_of_match,
669 },
670 .probe = bcm_kona_gpio_probe,
671};
Paul Gortmaker8f3e19f2016-03-27 11:44:41 -0400672builtin_platform_driver(bcm_kona_gpio_driver);