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