blob: 5c1564fcc24ea68808e8f553889b09ac6e5e2d9f [file] [log] [blame]
Vladimir Barinov3d9edf02007-07-10 13:03:43 +01001/*
2 * TI DaVinci GPIO Support
3 *
David Brownelldce11152008-09-07 23:41:04 -07004 * Copyright (c) 2006-2007 David Brownell
Vladimir Barinov3d9edf02007-07-10 13:03:43 +01005 * 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. Davis79b73ff2018-08-31 14:13:26 -050012
Linus Walleij7220c432018-01-14 02:05:38 +010013#include <linux/gpio/driver.h>
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010014#include <linux/errno.h>
15#include <linux/kernel.h>
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010016#include <linux/clk.h>
17#include <linux/err.h>
18#include <linux/io.h>
KV Sujith118150f2013-08-18 10:48:58 +053019#include <linux/irq.h>
Lad, Prabhakar9211ff32013-11-21 23:45:27 +053020#include <linux/irqdomain.h>
KV Sujithc7708442013-11-21 23:45:29 +053021#include <linux/module.h>
22#include <linux/of.h>
23#include <linux/of_device.h>
David Lechner3c87d7c2018-01-21 17:09:40 -060024#include <linux/pinctrl/consumer.h>
KV Sujith118150f2013-08-18 10:48:58 +053025#include <linux/platform_device.h>
26#include <linux/platform_data/gpio-davinci.h>
Grygorii Strashko0d978eb2013-11-26 21:40:09 +020027#include <linux/irqchip/chained_irq.h>
Andrew F. Davis79b73ff2018-08-31 14:13:26 -050028#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 Barinov3d9edf02007-07-10 13:03:43 +010034
Cyril Chemparathyc12f4152010-05-01 18:37:53 -040035struct 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 Strashko0c6feb02014-02-13 17:58:45 +020048typedef struct irq_chip *(*gpio_get_irq_chip_cb_t)(unsigned int irq);
49
Philip Avinash131a10a2013-08-18 10:48:57 +053050#define BINTEN 0x8 /* GPIO Interrupt Per-Bank Enable Register */
51
Cyril Chemparathyb8d44292010-05-07 17:06:32 -040052static void __iomem *gpio_base;
Keerthy8f7cf8c2017-01-17 21:49:11 +053053static unsigned int offset_array[5] = {0x10, 0x38, 0x60, 0x88, 0xb0};
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010054
Andrew F. Davis79b73ff2018-08-31 14:13:26 -050055struct davinci_gpio_irq_data {
56 void __iomem *regs;
57 struct davinci_gpio_controller *chip;
58 int bank_num;
59};
60
61struct 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
71static inline u32 __gpio_mask(unsigned gpio)
72{
73 return 1 << (gpio % 32);
74}
75
Thomas Gleixner1765d672015-07-13 01:18:56 +020076static inline struct davinci_gpio_regs __iomem *irq2regs(struct irq_data *d)
Kevin Hilman21ce8732010-02-25 16:49:56 -080077{
Cyril Chemparathy99e9e522010-05-01 18:37:52 -040078 struct davinci_gpio_regs __iomem *g;
Kevin Hilman21ce8732010-02-25 16:49:56 -080079
Thomas Gleixner1765d672015-07-13 01:18:56 +020080 g = (__force struct davinci_gpio_regs __iomem *)irq_data_get_irq_chip_data(d);
Kevin Hilman21ce8732010-02-25 16:49:56 -080081
82 return g;
83}
84
Keerthyeb3744a2018-06-13 09:10:37 +053085static int davinci_gpio_irq_setup(struct platform_device *pdev);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010086
87/*--------------------------------------------------------------------------*/
88
Cyril Chemparathy5b3a05c2010-05-01 18:38:27 -040089/* board setup code *MUST* setup pinmux and enable the GPIO clock. */
Cyril Chemparathyba4a9842010-05-01 18:37:51 -040090static inline int __davinci_direction(struct gpio_chip *chip,
91 unsigned offset, bool out, int value)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010092{
Linus Walleij72a1ca22015-12-04 16:25:04 +010093 struct davinci_gpio_controller *d = gpiochip_get_data(chip);
Keerthyb5cf3fd2017-01-13 09:50:12 +053094 struct davinci_gpio_regs __iomem *g;
Cyril Chemparathyb27b6d02010-05-01 18:37:55 -040095 unsigned long flags;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010096 u32 temp;
Keerthyb5cf3fd2017-01-13 09:50:12 +053097 int bank = offset / 32;
98 u32 mask = __gpio_mask(offset);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +010099
Keerthyb5cf3fd2017-01-13 09:50:12 +0530100 g = d->regs[bank];
Cyril Chemparathyb27b6d02010-05-01 18:37:55 -0400101 spin_lock_irqsave(&d->lock, flags);
Lad, Prabhakar388291c2013-12-11 23:22:07 +0530102 temp = readl_relaxed(&g->dir);
Cyril Chemparathyba4a9842010-05-01 18:37:51 -0400103 if (out) {
104 temp &= ~mask;
Lad, Prabhakar388291c2013-12-11 23:22:07 +0530105 writel_relaxed(mask, value ? &g->set_data : &g->clr_data);
Cyril Chemparathyba4a9842010-05-01 18:37:51 -0400106 } else {
107 temp |= mask;
108 }
Lad, Prabhakar388291c2013-12-11 23:22:07 +0530109 writel_relaxed(temp, &g->dir);
Cyril Chemparathyb27b6d02010-05-01 18:37:55 -0400110 spin_unlock_irqrestore(&d->lock, flags);
David Brownelldce11152008-09-07 23:41:04 -0700111
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100112 return 0;
113}
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100114
Cyril Chemparathyba4a9842010-05-01 18:37:51 -0400115static int davinci_direction_in(struct gpio_chip *chip, unsigned offset)
116{
117 return __davinci_direction(chip, offset, false, 0);
118}
119
120static int
121davinci_direction_out(struct gpio_chip *chip, unsigned offset, int value)
122{
123 return __davinci_direction(chip, offset, true, value);
124}
125
David Brownelldce11152008-09-07 23:41:04 -0700126/*
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 */
133static int davinci_gpio_get(struct gpio_chip *chip, unsigned offset)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100134{
Linus Walleij72a1ca22015-12-04 16:25:04 +0100135 struct davinci_gpio_controller *d = gpiochip_get_data(chip);
Keerthyb5cf3fd2017-01-13 09:50:12 +0530136 struct davinci_gpio_regs __iomem *g;
137 int bank = offset / 32;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100138
Keerthyb5cf3fd2017-01-13 09:50:12 +0530139 g = d->regs[bank];
140
141 return !!(__gpio_mask(offset) & readl_relaxed(&g->in_data));
David Brownelldce11152008-09-07 23:41:04 -0700142}
143
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100144/*
David Brownelldce11152008-09-07 23:41:04 -0700145 * Assuming the pin is muxed as a gpio output, set its output value.
146 */
147static void
148davinci_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
149{
Linus Walleij72a1ca22015-12-04 16:25:04 +0100150 struct davinci_gpio_controller *d = gpiochip_get_data(chip);
Keerthyb5cf3fd2017-01-13 09:50:12 +0530151 struct davinci_gpio_regs __iomem *g;
152 int bank = offset / 32;
David Brownelldce11152008-09-07 23:41:04 -0700153
Keerthyb5cf3fd2017-01-13 09:50:12 +0530154 g = d->regs[bank];
155
156 writel_relaxed(__gpio_mask(offset),
157 value ? &g->set_data : &g->clr_data);
David Brownelldce11152008-09-07 23:41:04 -0700158}
159
KV Sujithc7708442013-11-21 23:45:29 +0530160static struct davinci_gpio_platform_data *
161davinci_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 Haiderab128af2015-11-23 20:53:18 +0530169 return dev_get_platdata(&pdev->dev);
KV Sujithc7708442013-11-21 23:45:29 +0530170
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
189of_err:
190 dev_err(&pdev->dev, "Populating pdata from DT failed: err %d\n", ret);
191 return NULL;
192}
193
KV Sujith118150f2013-08-18 10:48:58 +0530194static int davinci_gpio_probe(struct platform_device *pdev)
David Brownelldce11152008-09-07 23:41:04 -0700195{
Andrew F. Davisc809e372018-08-31 14:13:24 -0500196 int bank, i, ret = 0;
Keerthyeb3744a2018-06-13 09:10:37 +0530197 unsigned int ngpio, nbank, nirq;
KV Sujith118150f2013-08-18 10:48:58 +0530198 struct davinci_gpio_controller *chips;
199 struct davinci_gpio_platform_data *pdata;
KV Sujith118150f2013-08-18 10:48:58 +0530200 struct device *dev = &pdev->dev;
201 struct resource *res;
David Brownelldce11152008-09-07 23:41:04 -0700202
KV Sujithc7708442013-11-21 23:45:29 +0530203 pdata = davinci_gpio_get_pdata(pdev);
KV Sujith118150f2013-08-18 10:48:58 +0530204 if (!pdata) {
205 dev_err(dev, "No platform data found\n");
206 return -EINVAL;
207 }
Cyril Chemparathy686b6342010-05-01 18:37:54 -0400208
KV Sujithc7708442013-11-21 23:45:29 +0530209 dev->platform_data = pdata;
210
Mark A. Greera9949552009-04-15 12:40:35 -0700211 /*
212 * The gpio banks conceptually expose a segmented bitmap,
David Brownell474dad52008-12-07 11:46:23 -0800213 * and "ngpio" is one more than the largest zero-based
214 * bit index that's valid.
215 */
KV Sujith118150f2013-08-18 10:48:58 +0530216 ngpio = pdata->ngpio;
Mark A. Greera9949552009-04-15 12:40:35 -0700217 if (ngpio == 0) {
KV Sujith118150f2013-08-18 10:48:58 +0530218 dev_err(dev, "How many GPIOs?\n");
David Brownell474dad52008-12-07 11:46:23 -0800219 return -EINVAL;
220 }
221
Grygorii Strashkoc21d5002013-11-21 17:34:35 +0200222 if (WARN_ON(ARCH_NR_GPIOS < ngpio))
223 ngpio = ARCH_NR_GPIOS;
David Brownell474dad52008-12-07 11:46:23 -0800224
Keerthyeb3744a2018-06-13 09:10:37 +0530225 /*
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. Davisc809e372018-08-31 14:13:24 -0500235 chips = devm_kzalloc(dev, sizeof(*chips), GFP_KERNEL);
Jingoo Han9ea9363c2014-04-29 17:33:26 +0900236 if (!chips)
Cyril Chemparathyb8d44292010-05-07 17:06:32 -0400237 return -ENOMEM;
KV Sujith118150f2013-08-18 10:48:58 +0530238
239 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
KV Sujith118150f2013-08-18 10:48:58 +0530240 gpio_base = devm_ioremap_resource(dev, res);
241 if (IS_ERR(gpio_base))
242 return PTR_ERR(gpio_base);
Cyril Chemparathyb8d44292010-05-07 17:06:32 -0400243
Keerthyeb3744a2018-06-13 09:10:37 +0530244 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 }
Keerthyc1d013a2018-06-13 09:10:36 +0530251 }
252
Andrew F. Davis587f7a62018-08-31 14:13:23 -0500253 chips->chip.label = dev_name(dev);
David Brownelldce11152008-09-07 23:41:04 -0700254
Keerthyb5cf3fd2017-01-13 09:50:12 +0530255 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 Brownelldce11152008-09-07 23:41:04 -0700259
Keerthyb5cf3fd2017-01-13 09:50:12 +0530260 chips->chip.ngpio = ngpio;
Andrew F. Davis587f7a62018-08-31 14:13:23 -0500261 chips->chip.base = -1;
David Brownelldce11152008-09-07 23:41:04 -0700262
KV Sujithc7708442013-11-21 23:45:29 +0530263#ifdef CONFIG_OF_GPIO
Keerthyb5cf3fd2017-01-13 09:50:12 +0530264 chips->chip.of_gpio_n_cells = 2;
Keerthyb5cf3fd2017-01-13 09:50:12 +0530265 chips->chip.parent = dev;
266 chips->chip.of_node = dev->of_node;
David Lechner3c87d7c2018-01-21 17:09:40 -0600267
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 Sujithc7708442013-11-21 23:45:29 +0530272#endif
Keerthyb5cf3fd2017-01-13 09:50:12 +0530273 spin_lock_init(&chips->lock);
Cyril Chemparathyb27b6d02010-05-01 18:37:55 -0400274
Andrew F. Davisc809e372018-08-31 14:13:24 -0500275 nbank = DIV_ROUND_UP(ngpio, 32);
276 for (bank = 0; bank < nbank; bank++)
Keerthyb5cf3fd2017-01-13 09:50:12 +0530277 chips->regs[bank] = gpio_base + offset_array[bank];
David Brownelldce11152008-09-07 23:41:04 -0700278
Keerthy8327e1b2017-07-20 15:12:16 +0530279 ret = devm_gpiochip_add_data(dev, &chips->chip, chips);
280 if (ret)
Andrew F. Davis587f7a62018-08-31 14:13:23 -0500281 return ret;
Keerthy8327e1b2017-07-20 15:12:16 +0530282
KV Sujith118150f2013-08-18 10:48:58 +0530283 platform_set_drvdata(pdev, chips);
Keerthyeb3744a2018-06-13 09:10:37 +0530284 ret = davinci_gpio_irq_setup(pdev);
Keerthy5e7a0ce2017-07-20 15:12:17 +0530285 if (ret)
Andrew F. Davis587f7a62018-08-31 14:13:23 -0500286 return ret;
Keerthy5e7a0ce2017-07-20 15:12:17 +0530287
David Brownelldce11152008-09-07 23:41:04 -0700288 return 0;
289}
David Brownelldce11152008-09-07 23:41:04 -0700290
291/*--------------------------------------------------------------------------*/
292/*
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100293 * 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 Brownell474dad52008-12-07 11:46:23 -0800296 * NOTE: The first few GPIOs also have direct INTC hookups in addition
David Brownell7a360712009-06-25 17:01:31 -0700297 * to their GPIOBNK0 irq, with a bit less overhead.
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100298 *
David Brownell474dad52008-12-07 11:46:23 -0800299 * All those INTC hookups (direct, plus several IRQ banks) can also
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100300 * serve as EDMA event triggers.
301 */
302
Lennert Buytenhek23265442010-11-29 10:27:27 +0100303static void gpio_irq_disable(struct irq_data *d)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100304{
Thomas Gleixner1765d672015-07-13 01:18:56 +0200305 struct davinci_gpio_regs __iomem *g = irq2regs(d);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100306 u32 mask = (u32) irq_data_get_irq_handler_data(d);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100307
Lad, Prabhakar388291c2013-12-11 23:22:07 +0530308 writel_relaxed(mask, &g->clr_falling);
309 writel_relaxed(mask, &g->clr_rising);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100310}
311
Lennert Buytenhek23265442010-11-29 10:27:27 +0100312static void gpio_irq_enable(struct irq_data *d)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100313{
Thomas Gleixner1765d672015-07-13 01:18:56 +0200314 struct davinci_gpio_regs __iomem *g = irq2regs(d);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100315 u32 mask = (u32) irq_data_get_irq_handler_data(d);
Thomas Gleixner5093aec2011-03-24 12:47:04 +0100316 unsigned status = irqd_get_trigger_type(d);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100317
David Brownelldf4aab42009-05-04 13:14:27 -0700318 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, Prabhakar388291c2013-12-11 23:22:07 +0530323 writel_relaxed(mask, &g->set_falling);
David Brownelldf4aab42009-05-04 13:14:27 -0700324 if (status & IRQ_TYPE_EDGE_RISING)
Lad, Prabhakar388291c2013-12-11 23:22:07 +0530325 writel_relaxed(mask, &g->set_rising);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100326}
327
Lennert Buytenhek23265442010-11-29 10:27:27 +0100328static int gpio_irq_type(struct irq_data *d, unsigned trigger)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100329{
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100330 if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
331 return -EINVAL;
332
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100333 return 0;
334}
335
336static struct irq_chip gpio_irqchip = {
337 .name = "GPIO",
Lennert Buytenhek23265442010-11-29 10:27:27 +0100338 .irq_enable = gpio_irq_enable,
339 .irq_disable = gpio_irq_disable,
340 .irq_set_type = gpio_irq_type,
Thomas Gleixner5093aec2011-03-24 12:47:04 +0100341 .flags = IRQCHIP_SET_TYPE_MASKED,
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100342};
343
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200344static void gpio_irq_handler(struct irq_desc *desc)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100345{
Thomas Gleixner74164012011-06-06 11:51:43 +0200346 struct davinci_gpio_regs __iomem *g;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100347 u32 mask = 0xffff;
Keerthyb5cf3fd2017-01-13 09:50:12 +0530348 int bank_num;
Ido Yarivf299bb92011-07-12 00:03:11 +0300349 struct davinci_gpio_controller *d;
Keerthyb5cf3fd2017-01-13 09:50:12 +0530350 struct davinci_gpio_irq_data *irqdata;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100351
Keerthyb5cf3fd2017-01-13 09:50:12 +0530352 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 Gleixner74164012011-06-06 11:51:43 +0200356
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100357 /* we only care about one bank */
Keerthyb5cf3fd2017-01-13 09:50:12 +0530358 if ((bank_num % 2) == 1)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100359 mask <<= 16;
360
361 /* temporarily mask (level sensitive) parent IRQ */
Grygorii Strashko0d978eb2013-11-26 21:40:09 +0200362 chained_irq_enter(irq_desc_get_chip(desc), desc);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100363 while (1) {
364 u32 status;
Lad, Prabhakar9211ff32013-11-21 23:45:27 +0530365 int bit;
Keerthyb5cf3fd2017-01-13 09:50:12 +0530366 irq_hw_number_t hw_irq;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100367
368 /* ack any irqs */
Lad, Prabhakar388291c2013-12-11 23:22:07 +0530369 status = readl_relaxed(&g->intstat) & mask;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100370 if (!status)
371 break;
Lad, Prabhakar388291c2013-12-11 23:22:07 +0530372 writel_relaxed(status, &g->intstat);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100373
374 /* now demux them to the right lowlevel handler */
Ido Yarivf299bb92011-07-12 00:03:11 +0300375
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100376 while (status) {
Lad, Prabhakar9211ff32013-11-21 23:45:27 +0530377 bit = __ffs(status);
378 status &= ~BIT(bit);
Keerthyb5cf3fd2017-01-13 09:50:12 +0530379 /* 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, Prabhakar9211ff32013-11-21 23:45:27 +0530384 generic_handle_irq(
Keerthyb5cf3fd2017-01-13 09:50:12 +0530385 irq_find_mapping(d->irq_domain, hw_irq));
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100386 }
387 }
Grygorii Strashko0d978eb2013-11-26 21:40:09 +0200388 chained_irq_exit(irq_desc_get_chip(desc), desc);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100389 /* now it may re-trigger */
390}
391
David Brownell7a360712009-06-25 17:01:31 -0700392static int gpio_to_irq_banked(struct gpio_chip *chip, unsigned offset)
393{
Linus Walleij72a1ca22015-12-04 16:25:04 +0100394 struct davinci_gpio_controller *d = gpiochip_get_data(chip);
David Brownell7a360712009-06-25 17:01:31 -0700395
Grygorii Strashko6075a8b2013-12-18 12:07:51 +0200396 if (d->irq_domain)
Keerthyb5cf3fd2017-01-13 09:50:12 +0530397 return irq_create_mapping(d->irq_domain, offset);
Grygorii Strashko6075a8b2013-12-18 12:07:51 +0200398 else
399 return -ENXIO;
David Brownell7a360712009-06-25 17:01:31 -0700400}
401
402static int gpio_to_irq_unbanked(struct gpio_chip *chip, unsigned offset)
403{
Linus Walleij72a1ca22015-12-04 16:25:04 +0100404 struct davinci_gpio_controller *d = gpiochip_get_data(chip);
David Brownell7a360712009-06-25 17:01:31 -0700405
Philip Avinash131a10a2013-08-18 10:48:57 +0530406 /*
407 * NOTE: we assume for now that only irqs in the first gpio_chip
David Brownell7a360712009-06-25 17:01:31 -0700408 * can provide direct-mapped IRQs to AINTC (up to 32 GPIOs).
409 */
Lad, Prabhakar34af1ab2013-11-08 12:15:55 +0530410 if (offset < d->gpio_unbanked)
Keerthyeb3744a2018-06-13 09:10:37 +0530411 return d->irqs[offset];
David Brownell7a360712009-06-25 17:01:31 -0700412 else
413 return -ENODEV;
414}
415
Sekhar Noriab2dde92012-03-11 18:16:11 +0530416static int gpio_irq_type_unbanked(struct irq_data *data, unsigned trigger)
David Brownell7a360712009-06-25 17:01:31 -0700417{
Sekhar Noriab2dde92012-03-11 18:16:11 +0530418 struct davinci_gpio_controller *d;
419 struct davinci_gpio_regs __iomem *g;
Keerthyeb3744a2018-06-13 09:10:37 +0530420 u32 mask, i;
Sekhar Noriab2dde92012-03-11 18:16:11 +0530421
Jiang Liuc16edb82015-06-01 16:05:19 +0800422 d = (struct davinci_gpio_controller *)irq_data_get_irq_handler_data(data);
Keerthy7f8e2a82017-11-10 16:43:17 +0530423 g = (struct davinci_gpio_regs __iomem *)d->regs[0];
Keerthyeb3744a2018-06-13 09:10:37 +0530424 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 Brownell7a360712009-06-25 17:01:31 -0700432
433 if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
434 return -EINVAL;
435
Lad, Prabhakar388291c2013-12-11 23:22:07 +0530436 writel_relaxed(mask, (trigger & IRQ_TYPE_EDGE_FALLING)
David Brownell7a360712009-06-25 17:01:31 -0700437 ? &g->set_falling : &g->clr_falling);
Lad, Prabhakar388291c2013-12-11 23:22:07 +0530438 writel_relaxed(mask, (trigger & IRQ_TYPE_EDGE_RISING)
David Brownell7a360712009-06-25 17:01:31 -0700439 ? &g->set_rising : &g->clr_rising);
440
441 return 0;
442}
443
Lad, Prabhakar9211ff32013-11-21 23:45:27 +0530444static int
445davinci_gpio_irq_map(struct irq_domain *d, unsigned int irq,
446 irq_hw_number_t hw)
447{
Keerthy8f7cf8c2017-01-17 21:49:11 +0530448 struct davinci_gpio_controller *chips =
449 (struct davinci_gpio_controller *)d->host_data;
Keerthyb5cf3fd2017-01-13 09:50:12 +0530450 struct davinci_gpio_regs __iomem *g = chips->regs[hw / 32];
Lad, Prabhakar9211ff32013-11-21 23:45:27 +0530451
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, Prabhakar9211ff32013-11-21 23:45:27 +0530457
458 return 0;
459}
460
461static const struct irq_domain_ops davinci_gpio_irq_ops = {
462 .map = davinci_gpio_irq_map,
463 .xlate = irq_domain_xlate_onetwocell,
464};
465
Grygorii Strashko0c6feb02014-02-13 17:58:45 +0200466static struct irq_chip *davinci_gpio_get_irq_chip(unsigned int irq)
467{
468 static struct irq_chip_type gpio_unbanked;
469
Geliang Tangccdbddf2015-12-30 22:16:38 +0800470 gpio_unbanked = *irq_data_get_chip_type(irq_get_irq_data(irq));
Grygorii Strashko0c6feb02014-02-13 17:58:45 +0200471
472 return &gpio_unbanked.chip;
473};
474
475static 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
483static const struct of_device_id davinci_gpio_ids[];
484
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100485/*
David Brownell474dad52008-12-07 11:46:23 -0800486 * 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 Barinov3d9edf02007-07-10 13:03:43 +0100488 * 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 Brownell474dad52008-12-07 11:46:23 -0800490 * (dm6446) can be set appropriately for GPIOV33 pins.
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100491 */
492
Keerthyeb3744a2018-06-13 09:10:37 +0530493static int davinci_gpio_irq_setup(struct platform_device *pdev)
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100494{
Alexander Shiyan58c0f5a2014-02-15 17:12:05 +0400495 unsigned gpio, bank;
496 int irq;
Arvind Yadav6dc00482017-05-23 14:48:57 +0530497 int ret;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100498 struct clk *clk;
David Brownell474dad52008-12-07 11:46:23 -0800499 u32 binten = 0;
Keerthyc1d013a2018-06-13 09:10:36 +0530500 unsigned ngpio;
KV Sujith118150f2013-08-18 10:48:58 +0530501 struct device *dev = &pdev->dev;
KV Sujith118150f2013-08-18 10:48:58 +0530502 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 Strashko6075a8b2013-12-18 12:07:51 +0200505 struct irq_domain *irq_domain = NULL;
Grygorii Strashko0c6feb02014-02-13 17:58:45 +0200506 const struct of_device_id *match;
507 struct irq_chip *irq_chip;
Keerthyb5cf3fd2017-01-13 09:50:12 +0530508 struct davinci_gpio_irq_data *irqdata;
Grygorii Strashko0c6feb02014-02-13 17:58:45 +0200509 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 Brownell474dad52008-12-07 11:46:23 -0800519
KV Sujith118150f2013-08-18 10:48:58 +0530520 ngpio = pdata->ngpio;
KV Sujith118150f2013-08-18 10:48:58 +0530521
522 clk = devm_clk_get(dev, "gpio");
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100523 if (IS_ERR(clk)) {
Keerthy1a9ef902017-07-20 15:12:18 +0530524 dev_err(dev, "Error %ld getting gpio clock\n", PTR_ERR(clk));
David Brownell474dad52008-12-07 11:46:23 -0800525 return PTR_ERR(clk);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100526 }
Keerthyeb3744a2018-06-13 09:10:37 +0530527
Arvind Yadav6dc00482017-05-23 14:48:57 +0530528 ret = clk_prepare_enable(clk);
529 if (ret)
530 return ret;
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100531
Grygorii Strashko6075a8b2013-12-18 12:07:51 +0200532 if (!pdata->gpio_unbanked) {
Bartosz Golaszewskia1a3c2d2017-03-04 17:23:36 +0100533 irq = devm_irq_alloc_descs(dev, -1, 0, ngpio, 0);
Grygorii Strashko6075a8b2013-12-18 12:07:51 +0200534 if (irq < 0) {
535 dev_err(dev, "Couldn't allocate IRQ numbers\n");
Arvind Yadav6dc00482017-05-23 14:48:57 +0530536 clk_disable_unprepare(clk);
Grygorii Strashko6075a8b2013-12-18 12:07:51 +0200537 return irq;
538 }
Lad, Prabhakar9211ff32013-11-21 23:45:27 +0530539
Keerthy310a7e62016-01-28 19:08:50 +0530540 irq_domain = irq_domain_add_legacy(dev->of_node, ngpio, irq, 0,
Grygorii Strashko6075a8b2013-12-18 12:07:51 +0200541 &davinci_gpio_irq_ops,
542 chips);
543 if (!irq_domain) {
544 dev_err(dev, "Couldn't register an IRQ domain\n");
Arvind Yadav6dc00482017-05-23 14:48:57 +0530545 clk_disable_unprepare(clk);
Grygorii Strashko6075a8b2013-12-18 12:07:51 +0200546 return -ENODEV;
547 }
Lad, Prabhakar9211ff32013-11-21 23:45:27 +0530548 }
549
Philip Avinash131a10a2013-08-18 10:48:57 +0530550 /*
551 * Arrange gpio_to_irq() support, handling either direct IRQs or
David Brownell7a360712009-06-25 17:01:31 -0700552 * 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 */
Keerthyb5cf3fd2017-01-13 09:50:12 +0530556 chips->chip.to_irq = gpio_to_irq_banked;
557 chips->irq_domain = irq_domain;
David Brownell7a360712009-06-25 17:01:31 -0700558
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 Sujith118150f2013-08-18 10:48:58 +0530564 if (pdata->gpio_unbanked) {
David Brownell7a360712009-06-25 17:01:31 -0700565 /* pass "bank 0" GPIO IRQs to AINTC */
Keerthyb5cf3fd2017-01-13 09:50:12 +0530566 chips->chip.to_irq = gpio_to_irq_unbanked;
Keerthyb5cf3fd2017-01-13 09:50:12 +0530567 chips->gpio_unbanked = pdata->gpio_unbanked;
Vitaly Andrianov3685bbc2015-07-02 14:31:30 -0400568 binten = GENMASK(pdata->gpio_unbanked / 16, 0);
David Brownell7a360712009-06-25 17:01:31 -0700569
570 /* AINTC handles mask/unmask; GPIO handles triggering */
Keerthyeb3744a2018-06-13 09:10:37 +0530571 irq = chips->irqs[0];
Grygorii Strashko0c6feb02014-02-13 17:58:45 +0200572 irq_chip = gpio_get_irq_chip(irq);
573 irq_chip->name = "GPIO-AINTC";
574 irq_chip->irq_set_type = gpio_irq_type_unbanked;
David Brownell7a360712009-06-25 17:01:31 -0700575
576 /* default trigger: both edges */
Keerthyb5cf3fd2017-01-13 09:50:12 +0530577 g = chips->regs[0];
Lad, Prabhakar388291c2013-12-11 23:22:07 +0530578 writel_relaxed(~0, &g->set_falling);
579 writel_relaxed(~0, &g->set_rising);
David Brownell7a360712009-06-25 17:01:31 -0700580
581 /* set the direct IRQs up to use that irqchip */
Keerthyeb3744a2018-06-13 09:10:37 +0530582 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 Brownell7a360712009-06-25 17:01:31 -0700587 }
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 */
Keerthyeb3744a2018-06-13 09:10:37 +0530596 for (gpio = 0, bank = 0; gpio < ngpio; bank++, gpio += 16) {
Keerthy8f7cf8c2017-01-17 21:49:11 +0530597 /* 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 */
Keerthyb5cf3fd2017-01-13 09:50:12 +0530601 g = chips->regs[bank / 2];
Lad, Prabhakar388291c2013-12-11 23:22:07 +0530602 writel_relaxed(~0, &g->clr_falling);
603 writel_relaxed(~0, &g->clr_rising);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100604
Ido Yarivf299bb92011-07-12 00:03:11 +0300605 /*
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 */
Keerthyb5cf3fd2017-01-13 09:50:12 +0530610 irqdata = devm_kzalloc(&pdev->dev,
611 sizeof(struct
612 davinci_gpio_irq_data),
613 GFP_KERNEL);
Arvind Yadav6dc00482017-05-23 14:48:57 +0530614 if (!irqdata) {
615 clk_disable_unprepare(clk);
Keerthyb5cf3fd2017-01-13 09:50:12 +0530616 return -ENOMEM;
Arvind Yadav6dc00482017-05-23 14:48:57 +0530617 }
Keerthyb5cf3fd2017-01-13 09:50:12 +0530618
619 irqdata->regs = g;
620 irqdata->bank_num = bank;
621 irqdata->chip = chips;
622
Keerthyeb3744a2018-06-13 09:10:37 +0530623 irq_set_chained_handler_and_data(chips->irqs[bank],
624 gpio_irq_handler, irqdata);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100625
David Brownell474dad52008-12-07 11:46:23 -0800626 binten |= BIT(bank);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100627 }
628
David Brownell7a360712009-06-25 17:01:31 -0700629done:
Philip Avinash131a10a2013-08-18 10:48:57 +0530630 /*
631 * BINTEN -- per-bank interrupt enable. genirq would also let these
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100632 * bits be set/cleared dynamically.
633 */
Lad, Prabhakar388291c2013-12-11 23:22:07 +0530634 writel_relaxed(binten, gpio_base + BINTEN);
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100635
Vladimir Barinov3d9edf02007-07-10 13:03:43 +0100636 return 0;
637}
KV Sujith118150f2013-08-18 10:48:58 +0530638
KV Sujithc7708442013-11-21 23:45:29 +0530639static const struct of_device_id davinci_gpio_ids[] = {
Grygorii Strashko0c6feb02014-02-13 17:58:45 +0200640 { .compatible = "ti,keystone-gpio", keystone_gpio_get_irq_chip},
641 { .compatible = "ti,dm6441-gpio", davinci_gpio_get_irq_chip},
KV Sujithc7708442013-11-21 23:45:29 +0530642 { /* sentinel */ },
643};
644MODULE_DEVICE_TABLE(of, davinci_gpio_ids);
KV Sujithc7708442013-11-21 23:45:29 +0530645
KV Sujith118150f2013-08-18 10:48:58 +0530646static struct platform_driver davinci_gpio_driver = {
647 .probe = davinci_gpio_probe,
648 .driver = {
KV Sujithc7708442013-11-21 23:45:29 +0530649 .name = "davinci_gpio",
KV Sujithc7708442013-11-21 23:45:29 +0530650 .of_match_table = of_match_ptr(davinci_gpio_ids),
KV Sujith118150f2013-08-18 10:48:58 +0530651 },
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 */
658static int __init davinci_gpio_drv_reg(void)
659{
660 return platform_driver_register(&davinci_gpio_driver);
661}
662postcore_initcall(davinci_gpio_drv_reg);