Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 2 | /* |
| 3 | * TI DaVinci GPIO Support |
| 4 | * |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 5 | * Copyright (c) 2006-2007 David Brownell |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 6 | * Copyright (c) 2007, MontaVista Software, Inc. <source@mvista.com> |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 7 | */ |
Andrew F. Davis | 79b73ff | 2018-08-31 14:13:26 -0500 | [diff] [blame] | 8 | |
Linus Walleij | 7220c43 | 2018-01-14 02:05:38 +0100 | [diff] [blame] | 9 | #include <linux/gpio/driver.h> |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 10 | #include <linux/errno.h> |
| 11 | #include <linux/kernel.h> |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 12 | #include <linux/clk.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/io.h> |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 15 | #include <linux/irq.h> |
Lad, Prabhakar | 9211ff3 | 2013-11-21 23:45:27 +0530 | [diff] [blame] | 16 | #include <linux/irqdomain.h> |
KV Sujith | c770844 | 2013-11-21 23:45:29 +0530 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/of.h> |
| 19 | #include <linux/of_device.h> |
David Lechner | 3c87d7c | 2018-01-21 17:09:40 -0600 | [diff] [blame] | 20 | #include <linux/pinctrl/consumer.h> |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/platform_data/gpio-davinci.h> |
Grygorii Strashko | 0d978eb | 2013-11-26 21:40:09 +0200 | [diff] [blame] | 23 | #include <linux/irqchip/chained_irq.h> |
Andrew F. Davis | 79b73ff | 2018-08-31 14:13:26 -0500 | [diff] [blame] | 24 | #include <linux/spinlock.h> |
| 25 | |
| 26 | #include <asm-generic/gpio.h> |
| 27 | |
| 28 | #define MAX_REGS_BANKS 5 |
| 29 | #define MAX_INT_PER_BANK 32 |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 30 | |
Cyril Chemparathy | c12f415 | 2010-05-01 18:37:53 -0400 | [diff] [blame] | 31 | struct davinci_gpio_regs { |
| 32 | u32 dir; |
| 33 | u32 out_data; |
| 34 | u32 set_data; |
| 35 | u32 clr_data; |
| 36 | u32 in_data; |
| 37 | u32 set_rising; |
| 38 | u32 clr_rising; |
| 39 | u32 set_falling; |
| 40 | u32 clr_falling; |
| 41 | u32 intstat; |
| 42 | }; |
| 43 | |
Grygorii Strashko | 0c6feb0 | 2014-02-13 17:58:45 +0200 | [diff] [blame] | 44 | typedef struct irq_chip *(*gpio_get_irq_chip_cb_t)(unsigned int irq); |
| 45 | |
Philip Avinash | 131a10a | 2013-08-18 10:48:57 +0530 | [diff] [blame] | 46 | #define BINTEN 0x8 /* GPIO Interrupt Per-Bank Enable Register */ |
| 47 | |
Cyril Chemparathy | b8d4429 | 2010-05-07 17:06:32 -0400 | [diff] [blame] | 48 | static void __iomem *gpio_base; |
Keerthy | 8f7cf8c | 2017-01-17 21:49:11 +0530 | [diff] [blame] | 49 | static unsigned int offset_array[5] = {0x10, 0x38, 0x60, 0x88, 0xb0}; |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 50 | |
Andrew F. Davis | 79b73ff | 2018-08-31 14:13:26 -0500 | [diff] [blame] | 51 | struct davinci_gpio_irq_data { |
| 52 | void __iomem *regs; |
| 53 | struct davinci_gpio_controller *chip; |
| 54 | int bank_num; |
| 55 | }; |
| 56 | |
| 57 | struct davinci_gpio_controller { |
| 58 | struct gpio_chip chip; |
| 59 | struct irq_domain *irq_domain; |
| 60 | /* Serialize access to GPIO registers */ |
| 61 | spinlock_t lock; |
| 62 | void __iomem *regs[MAX_REGS_BANKS]; |
| 63 | int gpio_unbanked; |
| 64 | int irqs[MAX_INT_PER_BANK]; |
| 65 | }; |
| 66 | |
| 67 | static inline u32 __gpio_mask(unsigned gpio) |
| 68 | { |
| 69 | return 1 << (gpio % 32); |
| 70 | } |
| 71 | |
Thomas Gleixner | 1765d67 | 2015-07-13 01:18:56 +0200 | [diff] [blame] | 72 | static inline struct davinci_gpio_regs __iomem *irq2regs(struct irq_data *d) |
Kevin Hilman | 21ce873 | 2010-02-25 16:49:56 -0800 | [diff] [blame] | 73 | { |
Cyril Chemparathy | 99e9e52 | 2010-05-01 18:37:52 -0400 | [diff] [blame] | 74 | struct davinci_gpio_regs __iomem *g; |
Kevin Hilman | 21ce873 | 2010-02-25 16:49:56 -0800 | [diff] [blame] | 75 | |
Thomas Gleixner | 1765d67 | 2015-07-13 01:18:56 +0200 | [diff] [blame] | 76 | g = (__force struct davinci_gpio_regs __iomem *)irq_data_get_irq_chip_data(d); |
Kevin Hilman | 21ce873 | 2010-02-25 16:49:56 -0800 | [diff] [blame] | 77 | |
| 78 | return g; |
| 79 | } |
| 80 | |
Keerthy | eb3744a | 2018-06-13 09:10:37 +0530 | [diff] [blame] | 81 | static int davinci_gpio_irq_setup(struct platform_device *pdev); |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 82 | |
| 83 | /*--------------------------------------------------------------------------*/ |
| 84 | |
Cyril Chemparathy | 5b3a05c | 2010-05-01 18:38:27 -0400 | [diff] [blame] | 85 | /* board setup code *MUST* setup pinmux and enable the GPIO clock. */ |
Cyril Chemparathy | ba4a984 | 2010-05-01 18:37:51 -0400 | [diff] [blame] | 86 | static inline int __davinci_direction(struct gpio_chip *chip, |
| 87 | unsigned offset, bool out, int value) |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 88 | { |
Linus Walleij | 72a1ca2 | 2015-12-04 16:25:04 +0100 | [diff] [blame] | 89 | struct davinci_gpio_controller *d = gpiochip_get_data(chip); |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 90 | struct davinci_gpio_regs __iomem *g; |
Cyril Chemparathy | b27b6d0 | 2010-05-01 18:37:55 -0400 | [diff] [blame] | 91 | unsigned long flags; |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 92 | u32 temp; |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 93 | int bank = offset / 32; |
| 94 | u32 mask = __gpio_mask(offset); |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 95 | |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 96 | g = d->regs[bank]; |
Cyril Chemparathy | b27b6d0 | 2010-05-01 18:37:55 -0400 | [diff] [blame] | 97 | spin_lock_irqsave(&d->lock, flags); |
Lad, Prabhakar | 388291c | 2013-12-11 23:22:07 +0530 | [diff] [blame] | 98 | temp = readl_relaxed(&g->dir); |
Cyril Chemparathy | ba4a984 | 2010-05-01 18:37:51 -0400 | [diff] [blame] | 99 | if (out) { |
| 100 | temp &= ~mask; |
Lad, Prabhakar | 388291c | 2013-12-11 23:22:07 +0530 | [diff] [blame] | 101 | writel_relaxed(mask, value ? &g->set_data : &g->clr_data); |
Cyril Chemparathy | ba4a984 | 2010-05-01 18:37:51 -0400 | [diff] [blame] | 102 | } else { |
| 103 | temp |= mask; |
| 104 | } |
Lad, Prabhakar | 388291c | 2013-12-11 23:22:07 +0530 | [diff] [blame] | 105 | writel_relaxed(temp, &g->dir); |
Cyril Chemparathy | b27b6d0 | 2010-05-01 18:37:55 -0400 | [diff] [blame] | 106 | spin_unlock_irqrestore(&d->lock, flags); |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 107 | |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 108 | return 0; |
| 109 | } |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 110 | |
Cyril Chemparathy | ba4a984 | 2010-05-01 18:37:51 -0400 | [diff] [blame] | 111 | static int davinci_direction_in(struct gpio_chip *chip, unsigned offset) |
| 112 | { |
| 113 | return __davinci_direction(chip, offset, false, 0); |
| 114 | } |
| 115 | |
| 116 | static int |
| 117 | davinci_direction_out(struct gpio_chip *chip, unsigned offset, int value) |
| 118 | { |
| 119 | return __davinci_direction(chip, offset, true, value); |
| 120 | } |
| 121 | |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 122 | /* |
| 123 | * Read the pin's value (works even if it's set up as output); |
| 124 | * returns zero/nonzero. |
| 125 | * |
| 126 | * Note that changes are synched to the GPIO clock, so reading values back |
| 127 | * right after you've set them may give old values. |
| 128 | */ |
| 129 | static int davinci_gpio_get(struct gpio_chip *chip, unsigned offset) |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 130 | { |
Linus Walleij | 72a1ca2 | 2015-12-04 16:25:04 +0100 | [diff] [blame] | 131 | struct davinci_gpio_controller *d = gpiochip_get_data(chip); |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 132 | struct davinci_gpio_regs __iomem *g; |
| 133 | int bank = offset / 32; |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 134 | |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 135 | g = d->regs[bank]; |
| 136 | |
| 137 | return !!(__gpio_mask(offset) & readl_relaxed(&g->in_data)); |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 140 | /* |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 141 | * Assuming the pin is muxed as a gpio output, set its output value. |
| 142 | */ |
| 143 | static void |
| 144 | davinci_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 145 | { |
Linus Walleij | 72a1ca2 | 2015-12-04 16:25:04 +0100 | [diff] [blame] | 146 | struct davinci_gpio_controller *d = gpiochip_get_data(chip); |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 147 | struct davinci_gpio_regs __iomem *g; |
| 148 | int bank = offset / 32; |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 149 | |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 150 | g = d->regs[bank]; |
| 151 | |
| 152 | writel_relaxed(__gpio_mask(offset), |
| 153 | value ? &g->set_data : &g->clr_data); |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 154 | } |
| 155 | |
KV Sujith | c770844 | 2013-11-21 23:45:29 +0530 | [diff] [blame] | 156 | static struct davinci_gpio_platform_data * |
| 157 | davinci_gpio_get_pdata(struct platform_device *pdev) |
| 158 | { |
| 159 | struct device_node *dn = pdev->dev.of_node; |
| 160 | struct davinci_gpio_platform_data *pdata; |
| 161 | int ret; |
| 162 | u32 val; |
| 163 | |
| 164 | if (!IS_ENABLED(CONFIG_OF) || !pdev->dev.of_node) |
Nizam Haider | ab128af | 2015-11-23 20:53:18 +0530 | [diff] [blame] | 165 | return dev_get_platdata(&pdev->dev); |
KV Sujith | c770844 | 2013-11-21 23:45:29 +0530 | [diff] [blame] | 166 | |
| 167 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 168 | if (!pdata) |
| 169 | return NULL; |
| 170 | |
| 171 | ret = of_property_read_u32(dn, "ti,ngpio", &val); |
| 172 | if (ret) |
| 173 | goto of_err; |
| 174 | |
| 175 | pdata->ngpio = val; |
| 176 | |
| 177 | ret = of_property_read_u32(dn, "ti,davinci-gpio-unbanked", &val); |
| 178 | if (ret) |
| 179 | goto of_err; |
| 180 | |
| 181 | pdata->gpio_unbanked = val; |
| 182 | |
| 183 | return pdata; |
| 184 | |
| 185 | of_err: |
| 186 | dev_err(&pdev->dev, "Populating pdata from DT failed: err %d\n", ret); |
| 187 | return NULL; |
| 188 | } |
| 189 | |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 190 | static int davinci_gpio_probe(struct platform_device *pdev) |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 191 | { |
Andrew F. Davis | c809e37 | 2018-08-31 14:13:24 -0500 | [diff] [blame] | 192 | int bank, i, ret = 0; |
Keerthy | eb3744a | 2018-06-13 09:10:37 +0530 | [diff] [blame] | 193 | unsigned int ngpio, nbank, nirq; |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 194 | struct davinci_gpio_controller *chips; |
| 195 | struct davinci_gpio_platform_data *pdata; |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 196 | struct device *dev = &pdev->dev; |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 197 | |
KV Sujith | c770844 | 2013-11-21 23:45:29 +0530 | [diff] [blame] | 198 | pdata = davinci_gpio_get_pdata(pdev); |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 199 | if (!pdata) { |
| 200 | dev_err(dev, "No platform data found\n"); |
| 201 | return -EINVAL; |
| 202 | } |
Cyril Chemparathy | 686b634 | 2010-05-01 18:37:54 -0400 | [diff] [blame] | 203 | |
KV Sujith | c770844 | 2013-11-21 23:45:29 +0530 | [diff] [blame] | 204 | dev->platform_data = pdata; |
| 205 | |
Mark A. Greer | a994955 | 2009-04-15 12:40:35 -0700 | [diff] [blame] | 206 | /* |
| 207 | * The gpio banks conceptually expose a segmented bitmap, |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 208 | * and "ngpio" is one more than the largest zero-based |
| 209 | * bit index that's valid. |
| 210 | */ |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 211 | ngpio = pdata->ngpio; |
Mark A. Greer | a994955 | 2009-04-15 12:40:35 -0700 | [diff] [blame] | 212 | if (ngpio == 0) { |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 213 | dev_err(dev, "How many GPIOs?\n"); |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 214 | return -EINVAL; |
| 215 | } |
| 216 | |
Grygorii Strashko | c21d500 | 2013-11-21 17:34:35 +0200 | [diff] [blame] | 217 | if (WARN_ON(ARCH_NR_GPIOS < ngpio)) |
| 218 | ngpio = ARCH_NR_GPIOS; |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 219 | |
Keerthy | eb3744a | 2018-06-13 09:10:37 +0530 | [diff] [blame] | 220 | /* |
| 221 | * If there are unbanked interrupts then the number of |
| 222 | * interrupts is equal to number of gpios else all are banked so |
| 223 | * number of interrupts is equal to number of banks(each with 16 gpios) |
| 224 | */ |
| 225 | if (pdata->gpio_unbanked) |
| 226 | nirq = pdata->gpio_unbanked; |
| 227 | else |
| 228 | nirq = DIV_ROUND_UP(ngpio, 16); |
| 229 | |
Andrew F. Davis | c809e37 | 2018-08-31 14:13:24 -0500 | [diff] [blame] | 230 | chips = devm_kzalloc(dev, sizeof(*chips), GFP_KERNEL); |
Jingoo Han | 9ea9363c | 2014-04-29 17:33:26 +0900 | [diff] [blame] | 231 | if (!chips) |
Cyril Chemparathy | b8d4429 | 2010-05-07 17:06:32 -0400 | [diff] [blame] | 232 | return -ENOMEM; |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 233 | |
Bartosz Golaszewski | fa7569c | 2019-02-20 12:12:40 +0100 | [diff] [blame] | 234 | gpio_base = devm_platform_ioremap_resource(pdev, 0); |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 235 | if (IS_ERR(gpio_base)) |
| 236 | return PTR_ERR(gpio_base); |
Cyril Chemparathy | b8d4429 | 2010-05-07 17:06:32 -0400 | [diff] [blame] | 237 | |
Keerthy | eb3744a | 2018-06-13 09:10:37 +0530 | [diff] [blame] | 238 | for (i = 0; i < nirq; i++) { |
| 239 | chips->irqs[i] = platform_get_irq(pdev, i); |
Krzysztof Kozlowski | 33b78b5 | 2020-08-27 22:08:23 +0200 | [diff] [blame] | 240 | if (chips->irqs[i] < 0) |
| 241 | return dev_err_probe(dev, chips->irqs[i], "IRQ not populated\n"); |
Keerthy | c1d013a | 2018-06-13 09:10:36 +0530 | [diff] [blame] | 242 | } |
| 243 | |
Andrew F. Davis | 587f7a6 | 2018-08-31 14:13:23 -0500 | [diff] [blame] | 244 | chips->chip.label = dev_name(dev); |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 245 | |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 246 | chips->chip.direction_input = davinci_direction_in; |
| 247 | chips->chip.get = davinci_gpio_get; |
| 248 | chips->chip.direction_output = davinci_direction_out; |
| 249 | chips->chip.set = davinci_gpio_set; |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 250 | |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 251 | chips->chip.ngpio = ngpio; |
Bartosz Golaszewski | 786a9ab | 2018-11-21 10:35:17 +0100 | [diff] [blame] | 252 | chips->chip.base = pdata->no_auto_base ? pdata->base : -1; |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 253 | |
KV Sujith | c770844 | 2013-11-21 23:45:29 +0530 | [diff] [blame] | 254 | #ifdef CONFIG_OF_GPIO |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 255 | chips->chip.of_gpio_n_cells = 2; |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 256 | chips->chip.parent = dev; |
Thierry Reding | f0254b5 | 2020-04-01 22:05:26 +0200 | [diff] [blame] | 257 | chips->chip.request = gpiochip_generic_request; |
| 258 | chips->chip.free = gpiochip_generic_free; |
KV Sujith | c770844 | 2013-11-21 23:45:29 +0530 | [diff] [blame] | 259 | #endif |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 260 | spin_lock_init(&chips->lock); |
Cyril Chemparathy | b27b6d0 | 2010-05-01 18:37:55 -0400 | [diff] [blame] | 261 | |
Andrew F. Davis | c809e37 | 2018-08-31 14:13:24 -0500 | [diff] [blame] | 262 | nbank = DIV_ROUND_UP(ngpio, 32); |
| 263 | for (bank = 0; bank < nbank; bank++) |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 264 | chips->regs[bank] = gpio_base + offset_array[bank]; |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 265 | |
Keerthy | 8327e1b | 2017-07-20 15:12:16 +0530 | [diff] [blame] | 266 | ret = devm_gpiochip_add_data(dev, &chips->chip, chips); |
| 267 | if (ret) |
Andrew F. Davis | 587f7a6 | 2018-08-31 14:13:23 -0500 | [diff] [blame] | 268 | return ret; |
Keerthy | 8327e1b | 2017-07-20 15:12:16 +0530 | [diff] [blame] | 269 | |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 270 | platform_set_drvdata(pdev, chips); |
Keerthy | eb3744a | 2018-06-13 09:10:37 +0530 | [diff] [blame] | 271 | ret = davinci_gpio_irq_setup(pdev); |
Keerthy | 5e7a0ce | 2017-07-20 15:12:17 +0530 | [diff] [blame] | 272 | if (ret) |
Andrew F. Davis | 587f7a6 | 2018-08-31 14:13:23 -0500 | [diff] [blame] | 273 | return ret; |
Keerthy | 5e7a0ce | 2017-07-20 15:12:17 +0530 | [diff] [blame] | 274 | |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 275 | return 0; |
| 276 | } |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 277 | |
| 278 | /*--------------------------------------------------------------------------*/ |
| 279 | /* |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 280 | * We expect irqs will normally be set up as input pins, but they can also be |
| 281 | * used as output pins ... which is convenient for testing. |
| 282 | * |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 283 | * NOTE: The first few GPIOs also have direct INTC hookups in addition |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 284 | * to their GPIOBNK0 irq, with a bit less overhead. |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 285 | * |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 286 | * All those INTC hookups (direct, plus several IRQ banks) can also |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 287 | * serve as EDMA event triggers. |
| 288 | */ |
| 289 | |
Lennert Buytenhek | 2326544 | 2010-11-29 10:27:27 +0100 | [diff] [blame] | 290 | static void gpio_irq_disable(struct irq_data *d) |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 291 | { |
Thomas Gleixner | 1765d67 | 2015-07-13 01:18:56 +0200 | [diff] [blame] | 292 | struct davinci_gpio_regs __iomem *g = irq2regs(d); |
Keerthy | 36c0551 | 2019-06-05 13:32:57 +0530 | [diff] [blame] | 293 | uintptr_t mask = (uintptr_t)irq_data_get_irq_handler_data(d); |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 294 | |
Lad, Prabhakar | 388291c | 2013-12-11 23:22:07 +0530 | [diff] [blame] | 295 | writel_relaxed(mask, &g->clr_falling); |
| 296 | writel_relaxed(mask, &g->clr_rising); |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 297 | } |
| 298 | |
Lennert Buytenhek | 2326544 | 2010-11-29 10:27:27 +0100 | [diff] [blame] | 299 | static void gpio_irq_enable(struct irq_data *d) |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 300 | { |
Thomas Gleixner | 1765d67 | 2015-07-13 01:18:56 +0200 | [diff] [blame] | 301 | struct davinci_gpio_regs __iomem *g = irq2regs(d); |
Keerthy | 36c0551 | 2019-06-05 13:32:57 +0530 | [diff] [blame] | 302 | uintptr_t mask = (uintptr_t)irq_data_get_irq_handler_data(d); |
Thomas Gleixner | 5093aec | 2011-03-24 12:47:04 +0100 | [diff] [blame] | 303 | unsigned status = irqd_get_trigger_type(d); |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 304 | |
David Brownell | df4aab4 | 2009-05-04 13:14:27 -0700 | [diff] [blame] | 305 | status &= IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING; |
| 306 | if (!status) |
| 307 | status = IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING; |
| 308 | |
| 309 | if (status & IRQ_TYPE_EDGE_FALLING) |
Lad, Prabhakar | 388291c | 2013-12-11 23:22:07 +0530 | [diff] [blame] | 310 | writel_relaxed(mask, &g->set_falling); |
David Brownell | df4aab4 | 2009-05-04 13:14:27 -0700 | [diff] [blame] | 311 | if (status & IRQ_TYPE_EDGE_RISING) |
Lad, Prabhakar | 388291c | 2013-12-11 23:22:07 +0530 | [diff] [blame] | 312 | writel_relaxed(mask, &g->set_rising); |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 313 | } |
| 314 | |
Lennert Buytenhek | 2326544 | 2010-11-29 10:27:27 +0100 | [diff] [blame] | 315 | static int gpio_irq_type(struct irq_data *d, unsigned trigger) |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 316 | { |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 317 | if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING)) |
| 318 | return -EINVAL; |
| 319 | |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | static struct irq_chip gpio_irqchip = { |
| 324 | .name = "GPIO", |
Lennert Buytenhek | 2326544 | 2010-11-29 10:27:27 +0100 | [diff] [blame] | 325 | .irq_enable = gpio_irq_enable, |
| 326 | .irq_disable = gpio_irq_disable, |
| 327 | .irq_set_type = gpio_irq_type, |
Thomas Gleixner | 5093aec | 2011-03-24 12:47:04 +0100 | [diff] [blame] | 328 | .flags = IRQCHIP_SET_TYPE_MASKED, |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 329 | }; |
| 330 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 331 | static void gpio_irq_handler(struct irq_desc *desc) |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 332 | { |
Thomas Gleixner | 7416401 | 2011-06-06 11:51:43 +0200 | [diff] [blame] | 333 | struct davinci_gpio_regs __iomem *g; |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 334 | u32 mask = 0xffff; |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 335 | int bank_num; |
Ido Yariv | f299bb9 | 2011-07-12 00:03:11 +0300 | [diff] [blame] | 336 | struct davinci_gpio_controller *d; |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 337 | struct davinci_gpio_irq_data *irqdata; |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 338 | |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 339 | irqdata = (struct davinci_gpio_irq_data *)irq_desc_get_handler_data(desc); |
| 340 | bank_num = irqdata->bank_num; |
| 341 | g = irqdata->regs; |
| 342 | d = irqdata->chip; |
Thomas Gleixner | 7416401 | 2011-06-06 11:51:43 +0200 | [diff] [blame] | 343 | |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 344 | /* we only care about one bank */ |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 345 | if ((bank_num % 2) == 1) |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 346 | mask <<= 16; |
| 347 | |
| 348 | /* temporarily mask (level sensitive) parent IRQ */ |
Grygorii Strashko | 0d978eb | 2013-11-26 21:40:09 +0200 | [diff] [blame] | 349 | chained_irq_enter(irq_desc_get_chip(desc), desc); |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 350 | while (1) { |
| 351 | u32 status; |
Lad, Prabhakar | 9211ff3 | 2013-11-21 23:45:27 +0530 | [diff] [blame] | 352 | int bit; |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 353 | irq_hw_number_t hw_irq; |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 354 | |
| 355 | /* ack any irqs */ |
Lad, Prabhakar | 388291c | 2013-12-11 23:22:07 +0530 | [diff] [blame] | 356 | status = readl_relaxed(&g->intstat) & mask; |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 357 | if (!status) |
| 358 | break; |
Lad, Prabhakar | 388291c | 2013-12-11 23:22:07 +0530 | [diff] [blame] | 359 | writel_relaxed(status, &g->intstat); |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 360 | |
| 361 | /* now demux them to the right lowlevel handler */ |
Ido Yariv | f299bb9 | 2011-07-12 00:03:11 +0300 | [diff] [blame] | 362 | |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 363 | while (status) { |
Lad, Prabhakar | 9211ff3 | 2013-11-21 23:45:27 +0530 | [diff] [blame] | 364 | bit = __ffs(status); |
| 365 | status &= ~BIT(bit); |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 366 | /* Max number of gpios per controller is 144 so |
| 367 | * hw_irq will be in [0..143] |
| 368 | */ |
| 369 | hw_irq = (bank_num / 2) * 32 + bit; |
| 370 | |
Marc Zyngier | dbd1c54 | 2021-05-04 17:42:18 +0100 | [diff] [blame] | 371 | generic_handle_domain_irq(d->irq_domain, hw_irq); |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 372 | } |
| 373 | } |
Grygorii Strashko | 0d978eb | 2013-11-26 21:40:09 +0200 | [diff] [blame] | 374 | chained_irq_exit(irq_desc_get_chip(desc), desc); |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 375 | /* now it may re-trigger */ |
| 376 | } |
| 377 | |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 378 | static int gpio_to_irq_banked(struct gpio_chip *chip, unsigned offset) |
| 379 | { |
Linus Walleij | 72a1ca2 | 2015-12-04 16:25:04 +0100 | [diff] [blame] | 380 | struct davinci_gpio_controller *d = gpiochip_get_data(chip); |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 381 | |
Grygorii Strashko | 6075a8b | 2013-12-18 12:07:51 +0200 | [diff] [blame] | 382 | if (d->irq_domain) |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 383 | return irq_create_mapping(d->irq_domain, offset); |
Grygorii Strashko | 6075a8b | 2013-12-18 12:07:51 +0200 | [diff] [blame] | 384 | else |
| 385 | return -ENXIO; |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | static int gpio_to_irq_unbanked(struct gpio_chip *chip, unsigned offset) |
| 389 | { |
Linus Walleij | 72a1ca2 | 2015-12-04 16:25:04 +0100 | [diff] [blame] | 390 | struct davinci_gpio_controller *d = gpiochip_get_data(chip); |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 391 | |
Philip Avinash | 131a10a | 2013-08-18 10:48:57 +0530 | [diff] [blame] | 392 | /* |
| 393 | * NOTE: we assume for now that only irqs in the first gpio_chip |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 394 | * can provide direct-mapped IRQs to AINTC (up to 32 GPIOs). |
| 395 | */ |
Lad, Prabhakar | 34af1ab | 2013-11-08 12:15:55 +0530 | [diff] [blame] | 396 | if (offset < d->gpio_unbanked) |
Keerthy | eb3744a | 2018-06-13 09:10:37 +0530 | [diff] [blame] | 397 | return d->irqs[offset]; |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 398 | else |
| 399 | return -ENODEV; |
| 400 | } |
| 401 | |
Sekhar Nori | ab2dde9 | 2012-03-11 18:16:11 +0530 | [diff] [blame] | 402 | static int gpio_irq_type_unbanked(struct irq_data *data, unsigned trigger) |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 403 | { |
Sekhar Nori | ab2dde9 | 2012-03-11 18:16:11 +0530 | [diff] [blame] | 404 | struct davinci_gpio_controller *d; |
| 405 | struct davinci_gpio_regs __iomem *g; |
Keerthy | eb3744a | 2018-06-13 09:10:37 +0530 | [diff] [blame] | 406 | u32 mask, i; |
Sekhar Nori | ab2dde9 | 2012-03-11 18:16:11 +0530 | [diff] [blame] | 407 | |
Jiang Liu | c16edb8 | 2015-06-01 16:05:19 +0800 | [diff] [blame] | 408 | d = (struct davinci_gpio_controller *)irq_data_get_irq_handler_data(data); |
Keerthy | 7f8e2a8 | 2017-11-10 16:43:17 +0530 | [diff] [blame] | 409 | g = (struct davinci_gpio_regs __iomem *)d->regs[0]; |
Keerthy | eb3744a | 2018-06-13 09:10:37 +0530 | [diff] [blame] | 410 | for (i = 0; i < MAX_INT_PER_BANK; i++) |
| 411 | if (data->irq == d->irqs[i]) |
| 412 | break; |
| 413 | |
| 414 | if (i == MAX_INT_PER_BANK) |
| 415 | return -EINVAL; |
| 416 | |
| 417 | mask = __gpio_mask(i); |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 418 | |
| 419 | if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING)) |
| 420 | return -EINVAL; |
| 421 | |
Lad, Prabhakar | 388291c | 2013-12-11 23:22:07 +0530 | [diff] [blame] | 422 | writel_relaxed(mask, (trigger & IRQ_TYPE_EDGE_FALLING) |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 423 | ? &g->set_falling : &g->clr_falling); |
Lad, Prabhakar | 388291c | 2013-12-11 23:22:07 +0530 | [diff] [blame] | 424 | writel_relaxed(mask, (trigger & IRQ_TYPE_EDGE_RISING) |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 425 | ? &g->set_rising : &g->clr_rising); |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
Lad, Prabhakar | 9211ff3 | 2013-11-21 23:45:27 +0530 | [diff] [blame] | 430 | static int |
| 431 | davinci_gpio_irq_map(struct irq_domain *d, unsigned int irq, |
| 432 | irq_hw_number_t hw) |
| 433 | { |
Keerthy | 8f7cf8c | 2017-01-17 21:49:11 +0530 | [diff] [blame] | 434 | struct davinci_gpio_controller *chips = |
| 435 | (struct davinci_gpio_controller *)d->host_data; |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 436 | struct davinci_gpio_regs __iomem *g = chips->regs[hw / 32]; |
Lad, Prabhakar | 9211ff3 | 2013-11-21 23:45:27 +0530 | [diff] [blame] | 437 | |
| 438 | irq_set_chip_and_handler_name(irq, &gpio_irqchip, handle_simple_irq, |
| 439 | "davinci_gpio"); |
| 440 | irq_set_irq_type(irq, IRQ_TYPE_NONE); |
| 441 | irq_set_chip_data(irq, (__force void *)g); |
Keerthy | 36c0551 | 2019-06-05 13:32:57 +0530 | [diff] [blame] | 442 | irq_set_handler_data(irq, (void *)(uintptr_t)__gpio_mask(hw)); |
Lad, Prabhakar | 9211ff3 | 2013-11-21 23:45:27 +0530 | [diff] [blame] | 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static const struct irq_domain_ops davinci_gpio_irq_ops = { |
| 448 | .map = davinci_gpio_irq_map, |
| 449 | .xlate = irq_domain_xlate_onetwocell, |
| 450 | }; |
| 451 | |
Grygorii Strashko | 0c6feb0 | 2014-02-13 17:58:45 +0200 | [diff] [blame] | 452 | static struct irq_chip *davinci_gpio_get_irq_chip(unsigned int irq) |
| 453 | { |
| 454 | static struct irq_chip_type gpio_unbanked; |
| 455 | |
Geliang Tang | ccdbddf | 2015-12-30 22:16:38 +0800 | [diff] [blame] | 456 | gpio_unbanked = *irq_data_get_chip_type(irq_get_irq_data(irq)); |
Grygorii Strashko | 0c6feb0 | 2014-02-13 17:58:45 +0200 | [diff] [blame] | 457 | |
| 458 | return &gpio_unbanked.chip; |
| 459 | }; |
| 460 | |
| 461 | static struct irq_chip *keystone_gpio_get_irq_chip(unsigned int irq) |
| 462 | { |
| 463 | static struct irq_chip gpio_unbanked; |
| 464 | |
| 465 | gpio_unbanked = *irq_get_chip(irq); |
| 466 | return &gpio_unbanked; |
| 467 | }; |
| 468 | |
| 469 | static const struct of_device_id davinci_gpio_ids[]; |
| 470 | |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 471 | /* |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 472 | * NOTE: for suspend/resume, probably best to make a platform_device with |
| 473 | * suspend_late/resume_resume calls hooking into results of the set_wake() |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 474 | * calls ... so if no gpios are wakeup events the clock can be disabled, |
| 475 | * with outputs left at previously set levels, and so that VDD3P3V.IOPWDN0 |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 476 | * (dm6446) can be set appropriately for GPIOV33 pins. |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 477 | */ |
| 478 | |
Keerthy | eb3744a | 2018-06-13 09:10:37 +0530 | [diff] [blame] | 479 | static int davinci_gpio_irq_setup(struct platform_device *pdev) |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 480 | { |
Alexander Shiyan | 58c0f5a | 2014-02-15 17:12:05 +0400 | [diff] [blame] | 481 | unsigned gpio, bank; |
| 482 | int irq; |
Arvind Yadav | 6dc0048 | 2017-05-23 14:48:57 +0530 | [diff] [blame] | 483 | int ret; |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 484 | struct clk *clk; |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 485 | u32 binten = 0; |
Keerthy | c1d013a | 2018-06-13 09:10:36 +0530 | [diff] [blame] | 486 | unsigned ngpio; |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 487 | struct device *dev = &pdev->dev; |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 488 | struct davinci_gpio_controller *chips = platform_get_drvdata(pdev); |
| 489 | struct davinci_gpio_platform_data *pdata = dev->platform_data; |
| 490 | struct davinci_gpio_regs __iomem *g; |
Grygorii Strashko | 6075a8b | 2013-12-18 12:07:51 +0200 | [diff] [blame] | 491 | struct irq_domain *irq_domain = NULL; |
Grygorii Strashko | 0c6feb0 | 2014-02-13 17:58:45 +0200 | [diff] [blame] | 492 | const struct of_device_id *match; |
| 493 | struct irq_chip *irq_chip; |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 494 | struct davinci_gpio_irq_data *irqdata; |
Grygorii Strashko | 0c6feb0 | 2014-02-13 17:58:45 +0200 | [diff] [blame] | 495 | gpio_get_irq_chip_cb_t gpio_get_irq_chip; |
| 496 | |
| 497 | /* |
| 498 | * Use davinci_gpio_get_irq_chip by default to handle non DT cases |
| 499 | */ |
| 500 | gpio_get_irq_chip = davinci_gpio_get_irq_chip; |
| 501 | match = of_match_device(of_match_ptr(davinci_gpio_ids), |
| 502 | dev); |
| 503 | if (match) |
| 504 | gpio_get_irq_chip = (gpio_get_irq_chip_cb_t)match->data; |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 505 | |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 506 | ngpio = pdata->ngpio; |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 507 | |
| 508 | clk = devm_clk_get(dev, "gpio"); |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 509 | if (IS_ERR(clk)) { |
Keerthy | 1a9ef90 | 2017-07-20 15:12:18 +0530 | [diff] [blame] | 510 | dev_err(dev, "Error %ld getting gpio clock\n", PTR_ERR(clk)); |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 511 | return PTR_ERR(clk); |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 512 | } |
Keerthy | eb3744a | 2018-06-13 09:10:37 +0530 | [diff] [blame] | 513 | |
Arvind Yadav | 6dc0048 | 2017-05-23 14:48:57 +0530 | [diff] [blame] | 514 | ret = clk_prepare_enable(clk); |
| 515 | if (ret) |
| 516 | return ret; |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 517 | |
Grygorii Strashko | 6075a8b | 2013-12-18 12:07:51 +0200 | [diff] [blame] | 518 | if (!pdata->gpio_unbanked) { |
Bartosz Golaszewski | a1a3c2d | 2017-03-04 17:23:36 +0100 | [diff] [blame] | 519 | irq = devm_irq_alloc_descs(dev, -1, 0, ngpio, 0); |
Grygorii Strashko | 6075a8b | 2013-12-18 12:07:51 +0200 | [diff] [blame] | 520 | if (irq < 0) { |
| 521 | dev_err(dev, "Couldn't allocate IRQ numbers\n"); |
Arvind Yadav | 6dc0048 | 2017-05-23 14:48:57 +0530 | [diff] [blame] | 522 | clk_disable_unprepare(clk); |
Grygorii Strashko | 6075a8b | 2013-12-18 12:07:51 +0200 | [diff] [blame] | 523 | return irq; |
| 524 | } |
Lad, Prabhakar | 9211ff3 | 2013-11-21 23:45:27 +0530 | [diff] [blame] | 525 | |
Keerthy | 310a7e6 | 2016-01-28 19:08:50 +0530 | [diff] [blame] | 526 | irq_domain = irq_domain_add_legacy(dev->of_node, ngpio, irq, 0, |
Grygorii Strashko | 6075a8b | 2013-12-18 12:07:51 +0200 | [diff] [blame] | 527 | &davinci_gpio_irq_ops, |
| 528 | chips); |
| 529 | if (!irq_domain) { |
| 530 | dev_err(dev, "Couldn't register an IRQ domain\n"); |
Arvind Yadav | 6dc0048 | 2017-05-23 14:48:57 +0530 | [diff] [blame] | 531 | clk_disable_unprepare(clk); |
Grygorii Strashko | 6075a8b | 2013-12-18 12:07:51 +0200 | [diff] [blame] | 532 | return -ENODEV; |
| 533 | } |
Lad, Prabhakar | 9211ff3 | 2013-11-21 23:45:27 +0530 | [diff] [blame] | 534 | } |
| 535 | |
Philip Avinash | 131a10a | 2013-08-18 10:48:57 +0530 | [diff] [blame] | 536 | /* |
| 537 | * Arrange gpio_to_irq() support, handling either direct IRQs or |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 538 | * banked IRQs. Having GPIOs in the first GPIO bank use direct |
| 539 | * IRQs, while the others use banked IRQs, would need some setup |
| 540 | * tweaks to recognize hardware which can do that. |
| 541 | */ |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 542 | chips->chip.to_irq = gpio_to_irq_banked; |
| 543 | chips->irq_domain = irq_domain; |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 544 | |
| 545 | /* |
| 546 | * AINTC can handle direct/unbanked IRQs for GPIOs, with the GPIO |
| 547 | * controller only handling trigger modes. We currently assume no |
| 548 | * IRQ mux conflicts; gpio_irq_type_unbanked() is only for GPIOs. |
| 549 | */ |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 550 | if (pdata->gpio_unbanked) { |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 551 | /* pass "bank 0" GPIO IRQs to AINTC */ |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 552 | chips->chip.to_irq = gpio_to_irq_unbanked; |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 553 | chips->gpio_unbanked = pdata->gpio_unbanked; |
Vitaly Andrianov | 3685bbc | 2015-07-02 14:31:30 -0400 | [diff] [blame] | 554 | binten = GENMASK(pdata->gpio_unbanked / 16, 0); |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 555 | |
| 556 | /* AINTC handles mask/unmask; GPIO handles triggering */ |
Keerthy | eb3744a | 2018-06-13 09:10:37 +0530 | [diff] [blame] | 557 | irq = chips->irqs[0]; |
Grygorii Strashko | 0c6feb0 | 2014-02-13 17:58:45 +0200 | [diff] [blame] | 558 | irq_chip = gpio_get_irq_chip(irq); |
| 559 | irq_chip->name = "GPIO-AINTC"; |
| 560 | irq_chip->irq_set_type = gpio_irq_type_unbanked; |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 561 | |
| 562 | /* default trigger: both edges */ |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 563 | g = chips->regs[0]; |
Lad, Prabhakar | 388291c | 2013-12-11 23:22:07 +0530 | [diff] [blame] | 564 | writel_relaxed(~0, &g->set_falling); |
| 565 | writel_relaxed(~0, &g->set_rising); |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 566 | |
| 567 | /* set the direct IRQs up to use that irqchip */ |
Keerthy | eb3744a | 2018-06-13 09:10:37 +0530 | [diff] [blame] | 568 | for (gpio = 0; gpio < pdata->gpio_unbanked; gpio++) { |
| 569 | irq_set_chip(chips->irqs[gpio], irq_chip); |
| 570 | irq_set_handler_data(chips->irqs[gpio], chips); |
| 571 | irq_set_status_flags(chips->irqs[gpio], |
| 572 | IRQ_TYPE_EDGE_BOTH); |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | goto done; |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * Or, AINTC can handle IRQs for banks of 16 GPIO IRQs, which we |
| 580 | * then chain through our own handler. |
| 581 | */ |
Keerthy | eb3744a | 2018-06-13 09:10:37 +0530 | [diff] [blame] | 582 | for (gpio = 0, bank = 0; gpio < ngpio; bank++, gpio += 16) { |
Keerthy | 8f7cf8c | 2017-01-17 21:49:11 +0530 | [diff] [blame] | 583 | /* disabled by default, enabled only as needed |
| 584 | * There are register sets for 32 GPIOs. 2 banks of 16 |
| 585 | * GPIOs are covered by each set of registers hence divide by 2 |
| 586 | */ |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 587 | g = chips->regs[bank / 2]; |
Lad, Prabhakar | 388291c | 2013-12-11 23:22:07 +0530 | [diff] [blame] | 588 | writel_relaxed(~0, &g->clr_falling); |
| 589 | writel_relaxed(~0, &g->clr_rising); |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 590 | |
Ido Yariv | f299bb9 | 2011-07-12 00:03:11 +0300 | [diff] [blame] | 591 | /* |
| 592 | * Each chip handles 32 gpios, and each irq bank consists of 16 |
| 593 | * gpio irqs. Pass the irq bank's corresponding controller to |
| 594 | * the chained irq handler. |
| 595 | */ |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 596 | irqdata = devm_kzalloc(&pdev->dev, |
| 597 | sizeof(struct |
| 598 | davinci_gpio_irq_data), |
| 599 | GFP_KERNEL); |
Arvind Yadav | 6dc0048 | 2017-05-23 14:48:57 +0530 | [diff] [blame] | 600 | if (!irqdata) { |
| 601 | clk_disable_unprepare(clk); |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 602 | return -ENOMEM; |
Arvind Yadav | 6dc0048 | 2017-05-23 14:48:57 +0530 | [diff] [blame] | 603 | } |
Keerthy | b5cf3fd | 2017-01-13 09:50:12 +0530 | [diff] [blame] | 604 | |
| 605 | irqdata->regs = g; |
| 606 | irqdata->bank_num = bank; |
| 607 | irqdata->chip = chips; |
| 608 | |
Keerthy | eb3744a | 2018-06-13 09:10:37 +0530 | [diff] [blame] | 609 | irq_set_chained_handler_and_data(chips->irqs[bank], |
| 610 | gpio_irq_handler, irqdata); |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 611 | |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 612 | binten |= BIT(bank); |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 613 | } |
| 614 | |
David Brownell | 7a36071 | 2009-06-25 17:01:31 -0700 | [diff] [blame] | 615 | done: |
Philip Avinash | 131a10a | 2013-08-18 10:48:57 +0530 | [diff] [blame] | 616 | /* |
| 617 | * BINTEN -- per-bank interrupt enable. genirq would also let these |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 618 | * bits be set/cleared dynamically. |
| 619 | */ |
Lad, Prabhakar | 388291c | 2013-12-11 23:22:07 +0530 | [diff] [blame] | 620 | writel_relaxed(binten, gpio_base + BINTEN); |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 621 | |
Vladimir Barinov | 3d9edf0 | 2007-07-10 13:03:43 +0100 | [diff] [blame] | 622 | return 0; |
| 623 | } |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 624 | |
KV Sujith | c770844 | 2013-11-21 23:45:29 +0530 | [diff] [blame] | 625 | static const struct of_device_id davinci_gpio_ids[] = { |
Grygorii Strashko | 0c6feb0 | 2014-02-13 17:58:45 +0200 | [diff] [blame] | 626 | { .compatible = "ti,keystone-gpio", keystone_gpio_get_irq_chip}, |
Keerthy | 6a4d8b6 | 2019-06-05 13:32:58 +0530 | [diff] [blame] | 627 | { .compatible = "ti,am654-gpio", keystone_gpio_get_irq_chip}, |
Grygorii Strashko | 0c6feb0 | 2014-02-13 17:58:45 +0200 | [diff] [blame] | 628 | { .compatible = "ti,dm6441-gpio", davinci_gpio_get_irq_chip}, |
KV Sujith | c770844 | 2013-11-21 23:45:29 +0530 | [diff] [blame] | 629 | { /* sentinel */ }, |
| 630 | }; |
| 631 | MODULE_DEVICE_TABLE(of, davinci_gpio_ids); |
KV Sujith | c770844 | 2013-11-21 23:45:29 +0530 | [diff] [blame] | 632 | |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 633 | static struct platform_driver davinci_gpio_driver = { |
| 634 | .probe = davinci_gpio_probe, |
| 635 | .driver = { |
KV Sujith | c770844 | 2013-11-21 23:45:29 +0530 | [diff] [blame] | 636 | .name = "davinci_gpio", |
KV Sujith | c770844 | 2013-11-21 23:45:29 +0530 | [diff] [blame] | 637 | .of_match_table = of_match_ptr(davinci_gpio_ids), |
KV Sujith | 118150f | 2013-08-18 10:48:58 +0530 | [diff] [blame] | 638 | }, |
| 639 | }; |
| 640 | |
| 641 | /** |
| 642 | * GPIO driver registration needs to be done before machine_init functions |
| 643 | * access GPIO. Hence davinci_gpio_drv_reg() is a postcore_initcall. |
| 644 | */ |
| 645 | static int __init davinci_gpio_drv_reg(void) |
| 646 | { |
| 647 | return platform_driver_register(&davinci_gpio_driver); |
| 648 | } |
| 649 | postcore_initcall(davinci_gpio_drv_reg); |