xref: /openbmc/linux/drivers/gpio/gpio-davinci.c (revision fac59652993f075d57860769c99045b3ca18780d)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
28338d87fSLinus Walleij /*
38338d87fSLinus Walleij  * TI DaVinci GPIO Support
48338d87fSLinus Walleij  *
58338d87fSLinus Walleij  * Copyright (c) 2006-2007 David Brownell
68338d87fSLinus Walleij  * Copyright (c) 2007, MontaVista Software, Inc. <source@mvista.com>
78338d87fSLinus Walleij  */
879b73ff9SAndrew F. Davis 
97220c43aSLinus Walleij #include <linux/gpio/driver.h>
108338d87fSLinus Walleij #include <linux/errno.h>
118338d87fSLinus Walleij #include <linux/kernel.h>
128338d87fSLinus Walleij #include <linux/clk.h>
138338d87fSLinus Walleij #include <linux/err.h>
148338d87fSLinus Walleij #include <linux/io.h>
15118150f2SKV Sujith #include <linux/irq.h>
169211ff31SLad, Prabhakar #include <linux/irqdomain.h>
17c770844cSKV Sujith #include <linux/module.h>
18c770844cSKV Sujith #include <linux/of.h>
19c770844cSKV Sujith #include <linux/of_device.h>
203c87d7c8SDavid Lechner #include <linux/pinctrl/consumer.h>
21118150f2SKV Sujith #include <linux/platform_device.h>
22118150f2SKV Sujith #include <linux/platform_data/gpio-davinci.h>
230d978eb7SGrygorii Strashko #include <linux/irqchip/chained_irq.h>
2479b73ff9SAndrew F. Davis #include <linux/spinlock.h>
250651a730SDevarsh Thakkar #include <linux/pm_runtime.h>
2679b73ff9SAndrew F. Davis 
2779b73ff9SAndrew F. Davis #define MAX_REGS_BANKS 5
2879b73ff9SAndrew F. Davis #define MAX_INT_PER_BANK 32
298338d87fSLinus Walleij 
308338d87fSLinus Walleij struct davinci_gpio_regs {
318338d87fSLinus Walleij 	u32	dir;
328338d87fSLinus Walleij 	u32	out_data;
338338d87fSLinus Walleij 	u32	set_data;
348338d87fSLinus Walleij 	u32	clr_data;
358338d87fSLinus Walleij 	u32	in_data;
368338d87fSLinus Walleij 	u32	set_rising;
378338d87fSLinus Walleij 	u32	clr_rising;
388338d87fSLinus Walleij 	u32	set_falling;
398338d87fSLinus Walleij 	u32	clr_falling;
408338d87fSLinus Walleij 	u32	intstat;
418338d87fSLinus Walleij };
428338d87fSLinus Walleij 
430c6feb07SGrygorii Strashko typedef struct irq_chip *(*gpio_get_irq_chip_cb_t)(unsigned int irq);
440c6feb07SGrygorii Strashko 
45131a10a3SPhilip Avinash #define BINTEN	0x8 /* GPIO Interrupt Per-Bank Enable Register */
46131a10a3SPhilip Avinash 
478338d87fSLinus Walleij static void __iomem *gpio_base;
488f7cf8c6SKeerthy static unsigned int offset_array[5] = {0x10, 0x38, 0x60, 0x88, 0xb0};
498338d87fSLinus Walleij 
5079b73ff9SAndrew F. Davis struct davinci_gpio_irq_data {
5179b73ff9SAndrew F. Davis 	void __iomem			*regs;
5279b73ff9SAndrew F. Davis 	struct davinci_gpio_controller	*chip;
5379b73ff9SAndrew F. Davis 	int				bank_num;
5479b73ff9SAndrew F. Davis };
5579b73ff9SAndrew F. Davis 
5679b73ff9SAndrew F. Davis struct davinci_gpio_controller {
5779b73ff9SAndrew F. Davis 	struct gpio_chip	chip;
5879b73ff9SAndrew F. Davis 	struct irq_domain	*irq_domain;
5979b73ff9SAndrew F. Davis 	/* Serialize access to GPIO registers */
6079b73ff9SAndrew F. Davis 	spinlock_t		lock;
6179b73ff9SAndrew F. Davis 	void __iomem		*regs[MAX_REGS_BANKS];
6279b73ff9SAndrew F. Davis 	int			gpio_unbanked;
6379b73ff9SAndrew F. Davis 	int			irqs[MAX_INT_PER_BANK];
640651a730SDevarsh Thakkar 	struct davinci_gpio_regs context[MAX_REGS_BANKS];
650651a730SDevarsh Thakkar 	u32			binten_context;
6679b73ff9SAndrew F. Davis };
6779b73ff9SAndrew F. Davis 
__gpio_mask(unsigned gpio)6879b73ff9SAndrew F. Davis static inline u32 __gpio_mask(unsigned gpio)
6979b73ff9SAndrew F. Davis {
7079b73ff9SAndrew F. Davis 	return 1 << (gpio % 32);
7179b73ff9SAndrew F. Davis }
7279b73ff9SAndrew F. Davis 
irq2regs(struct irq_data * d)731765d671SThomas Gleixner static inline struct davinci_gpio_regs __iomem *irq2regs(struct irq_data *d)
748338d87fSLinus Walleij {
758338d87fSLinus Walleij 	struct davinci_gpio_regs __iomem *g;
768338d87fSLinus Walleij 
771765d671SThomas Gleixner 	g = (__force struct davinci_gpio_regs __iomem *)irq_data_get_irq_chip_data(d);
788338d87fSLinus Walleij 
798338d87fSLinus Walleij 	return g;
808338d87fSLinus Walleij }
818338d87fSLinus Walleij 
82eb3744a2SKeerthy static int davinci_gpio_irq_setup(struct platform_device *pdev);
838338d87fSLinus Walleij 
848338d87fSLinus Walleij /*--------------------------------------------------------------------------*/
858338d87fSLinus Walleij 
868338d87fSLinus Walleij /* board setup code *MUST* setup pinmux and enable the GPIO clock. */
__davinci_direction(struct gpio_chip * chip,unsigned offset,bool out,int value)878338d87fSLinus Walleij static inline int __davinci_direction(struct gpio_chip *chip,
888338d87fSLinus Walleij 			unsigned offset, bool out, int value)
898338d87fSLinus Walleij {
9072a1ca2cSLinus Walleij 	struct davinci_gpio_controller *d = gpiochip_get_data(chip);
91b5cf3fd8SKeerthy 	struct davinci_gpio_regs __iomem *g;
928338d87fSLinus Walleij 	unsigned long flags;
938338d87fSLinus Walleij 	u32 temp;
94b5cf3fd8SKeerthy 	int bank = offset / 32;
95b5cf3fd8SKeerthy 	u32 mask = __gpio_mask(offset);
968338d87fSLinus Walleij 
97b5cf3fd8SKeerthy 	g = d->regs[bank];
988338d87fSLinus Walleij 	spin_lock_irqsave(&d->lock, flags);
99388291c3SLad, Prabhakar 	temp = readl_relaxed(&g->dir);
1008338d87fSLinus Walleij 	if (out) {
1018338d87fSLinus Walleij 		temp &= ~mask;
102388291c3SLad, Prabhakar 		writel_relaxed(mask, value ? &g->set_data : &g->clr_data);
1038338d87fSLinus Walleij 	} else {
1048338d87fSLinus Walleij 		temp |= mask;
1058338d87fSLinus Walleij 	}
106388291c3SLad, Prabhakar 	writel_relaxed(temp, &g->dir);
1078338d87fSLinus Walleij 	spin_unlock_irqrestore(&d->lock, flags);
1088338d87fSLinus Walleij 
1098338d87fSLinus Walleij 	return 0;
1108338d87fSLinus Walleij }
1118338d87fSLinus Walleij 
davinci_direction_in(struct gpio_chip * chip,unsigned offset)1128338d87fSLinus Walleij static int davinci_direction_in(struct gpio_chip *chip, unsigned offset)
1138338d87fSLinus Walleij {
1148338d87fSLinus Walleij 	return __davinci_direction(chip, offset, false, 0);
1158338d87fSLinus Walleij }
1168338d87fSLinus Walleij 
1178338d87fSLinus Walleij static int
davinci_direction_out(struct gpio_chip * chip,unsigned offset,int value)1188338d87fSLinus Walleij davinci_direction_out(struct gpio_chip *chip, unsigned offset, int value)
1198338d87fSLinus Walleij {
1208338d87fSLinus Walleij 	return __davinci_direction(chip, offset, true, value);
1218338d87fSLinus Walleij }
1228338d87fSLinus Walleij 
1238338d87fSLinus Walleij /*
1248338d87fSLinus Walleij  * Read the pin's value (works even if it's set up as output);
1258338d87fSLinus Walleij  * returns zero/nonzero.
1268338d87fSLinus Walleij  *
1278338d87fSLinus Walleij  * Note that changes are synched to the GPIO clock, so reading values back
1288338d87fSLinus Walleij  * right after you've set them may give old values.
1298338d87fSLinus Walleij  */
davinci_gpio_get(struct gpio_chip * chip,unsigned offset)1308338d87fSLinus Walleij static int davinci_gpio_get(struct gpio_chip *chip, unsigned offset)
1318338d87fSLinus Walleij {
13272a1ca2cSLinus Walleij 	struct davinci_gpio_controller *d = gpiochip_get_data(chip);
133b5cf3fd8SKeerthy 	struct davinci_gpio_regs __iomem *g;
134b5cf3fd8SKeerthy 	int bank = offset / 32;
1358338d87fSLinus Walleij 
136b5cf3fd8SKeerthy 	g = d->regs[bank];
137b5cf3fd8SKeerthy 
138b5cf3fd8SKeerthy 	return !!(__gpio_mask(offset) & readl_relaxed(&g->in_data));
1398338d87fSLinus Walleij }
1408338d87fSLinus Walleij 
1418338d87fSLinus Walleij /*
1428338d87fSLinus Walleij  * Assuming the pin is muxed as a gpio output, set its output value.
1438338d87fSLinus Walleij  */
1448338d87fSLinus Walleij static void
davinci_gpio_set(struct gpio_chip * chip,unsigned offset,int value)1458338d87fSLinus Walleij davinci_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1468338d87fSLinus Walleij {
14772a1ca2cSLinus Walleij 	struct davinci_gpio_controller *d = gpiochip_get_data(chip);
148b5cf3fd8SKeerthy 	struct davinci_gpio_regs __iomem *g;
149b5cf3fd8SKeerthy 	int bank = offset / 32;
1508338d87fSLinus Walleij 
151b5cf3fd8SKeerthy 	g = d->regs[bank];
152b5cf3fd8SKeerthy 
153b5cf3fd8SKeerthy 	writel_relaxed(__gpio_mask(offset),
154b5cf3fd8SKeerthy 		       value ? &g->set_data : &g->clr_data);
1558338d87fSLinus Walleij }
1568338d87fSLinus Walleij 
157c770844cSKV Sujith static struct davinci_gpio_platform_data *
davinci_gpio_get_pdata(struct platform_device * pdev)158c770844cSKV Sujith davinci_gpio_get_pdata(struct platform_device *pdev)
159c770844cSKV Sujith {
160c770844cSKV Sujith 	struct device_node *dn = pdev->dev.of_node;
161c770844cSKV Sujith 	struct davinci_gpio_platform_data *pdata;
162c770844cSKV Sujith 	int ret;
163c770844cSKV Sujith 	u32 val;
164c770844cSKV Sujith 
165c770844cSKV Sujith 	if (!IS_ENABLED(CONFIG_OF) || !pdev->dev.of_node)
166ab128afcSNizam Haider 		return dev_get_platdata(&pdev->dev);
167c770844cSKV Sujith 
168c770844cSKV Sujith 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
169c770844cSKV Sujith 	if (!pdata)
170c770844cSKV Sujith 		return NULL;
171c770844cSKV Sujith 
172c770844cSKV Sujith 	ret = of_property_read_u32(dn, "ti,ngpio", &val);
173c770844cSKV Sujith 	if (ret)
174c770844cSKV Sujith 		goto of_err;
175c770844cSKV Sujith 
176c770844cSKV Sujith 	pdata->ngpio = val;
177c770844cSKV Sujith 
178c770844cSKV Sujith 	ret = of_property_read_u32(dn, "ti,davinci-gpio-unbanked", &val);
179c770844cSKV Sujith 	if (ret)
180c770844cSKV Sujith 		goto of_err;
181c770844cSKV Sujith 
182c770844cSKV Sujith 	pdata->gpio_unbanked = val;
183c770844cSKV Sujith 
184c770844cSKV Sujith 	return pdata;
185c770844cSKV Sujith 
186c770844cSKV Sujith of_err:
187c770844cSKV Sujith 	dev_err(&pdev->dev, "Populating pdata from DT failed: err %d\n", ret);
188c770844cSKV Sujith 	return NULL;
189c770844cSKV Sujith }
190c770844cSKV Sujith 
davinci_gpio_probe(struct platform_device * pdev)191118150f2SKV Sujith static int davinci_gpio_probe(struct platform_device *pdev)
1928338d87fSLinus Walleij {
193c809e37aSAndrew F. Davis 	int bank, i, ret = 0;
194eb3744a2SKeerthy 	unsigned int ngpio, nbank, nirq;
195118150f2SKV Sujith 	struct davinci_gpio_controller *chips;
196118150f2SKV Sujith 	struct davinci_gpio_platform_data *pdata;
197118150f2SKV Sujith 	struct device *dev = &pdev->dev;
1988338d87fSLinus Walleij 
199c770844cSKV Sujith 	pdata = davinci_gpio_get_pdata(pdev);
200118150f2SKV Sujith 	if (!pdata) {
201118150f2SKV Sujith 		dev_err(dev, "No platform data found\n");
202118150f2SKV Sujith 		return -EINVAL;
203118150f2SKV Sujith 	}
2048338d87fSLinus Walleij 
205c770844cSKV Sujith 	dev->platform_data = pdata;
206c770844cSKV Sujith 
2078338d87fSLinus Walleij 	/*
2088338d87fSLinus Walleij 	 * The gpio banks conceptually expose a segmented bitmap,
2098338d87fSLinus Walleij 	 * and "ngpio" is one more than the largest zero-based
2108338d87fSLinus Walleij 	 * bit index that's valid.
2118338d87fSLinus Walleij 	 */
212118150f2SKV Sujith 	ngpio = pdata->ngpio;
2138338d87fSLinus Walleij 	if (ngpio == 0) {
214118150f2SKV Sujith 		dev_err(dev, "How many GPIOs?\n");
2158338d87fSLinus Walleij 		return -EINVAL;
2168338d87fSLinus Walleij 	}
2178338d87fSLinus Walleij 
218eb3744a2SKeerthy 	/*
219eb3744a2SKeerthy 	 * If there are unbanked interrupts then the number of
220eb3744a2SKeerthy 	 * interrupts is equal to number of gpios else all are banked so
221eb3744a2SKeerthy 	 * number of interrupts is equal to number of banks(each with 16 gpios)
222eb3744a2SKeerthy 	 */
223eb3744a2SKeerthy 	if (pdata->gpio_unbanked)
224eb3744a2SKeerthy 		nirq = pdata->gpio_unbanked;
225eb3744a2SKeerthy 	else
226eb3744a2SKeerthy 		nirq = DIV_ROUND_UP(ngpio, 16);
227eb3744a2SKeerthy 
2282d834922SAleksandr Mishin 	if (nirq > MAX_INT_PER_BANK) {
2292d834922SAleksandr Mishin 		dev_err(dev, "Too many IRQs!\n");
2302d834922SAleksandr Mishin 		return -EINVAL;
2312d834922SAleksandr Mishin 	}
2322d834922SAleksandr Mishin 
233c809e37aSAndrew F. Davis 	chips = devm_kzalloc(dev, sizeof(*chips), GFP_KERNEL);
2349ea9363cSJingoo Han 	if (!chips)
2358338d87fSLinus Walleij 		return -ENOMEM;
236118150f2SKV Sujith 
237fa7569c8SBartosz Golaszewski 	gpio_base = devm_platform_ioremap_resource(pdev, 0);
238118150f2SKV Sujith 	if (IS_ERR(gpio_base))
239118150f2SKV Sujith 		return PTR_ERR(gpio_base);
2408338d87fSLinus Walleij 
241eb3744a2SKeerthy 	for (i = 0; i < nirq; i++) {
242eb3744a2SKeerthy 		chips->irqs[i] = platform_get_irq(pdev, i);
24333b78b5fSKrzysztof Kozlowski 		if (chips->irqs[i] < 0)
24473561d28SRuan Jinjie 			return chips->irqs[i];
245c1d013a7SKeerthy 	}
246c1d013a7SKeerthy 
247587f7a69SAndrew F. Davis 	chips->chip.label = dev_name(dev);
2488338d87fSLinus Walleij 
249b5cf3fd8SKeerthy 	chips->chip.direction_input = davinci_direction_in;
250b5cf3fd8SKeerthy 	chips->chip.get = davinci_gpio_get;
251b5cf3fd8SKeerthy 	chips->chip.direction_output = davinci_direction_out;
252b5cf3fd8SKeerthy 	chips->chip.set = davinci_gpio_set;
2538338d87fSLinus Walleij 
254b5cf3fd8SKeerthy 	chips->chip.ngpio = ngpio;
255786a9ab1SBartosz Golaszewski 	chips->chip.base = pdata->no_auto_base ? pdata->base : -1;
2568338d87fSLinus Walleij 
257c770844cSKV Sujith #ifdef CONFIG_OF_GPIO
258b5cf3fd8SKeerthy 	chips->chip.parent = dev;
2593c87d7c8SDavid Lechner 	chips->chip.request = gpiochip_generic_request;
2603c87d7c8SDavid Lechner 	chips->chip.free = gpiochip_generic_free;
261c770844cSKV Sujith #endif
262b5cf3fd8SKeerthy 	spin_lock_init(&chips->lock);
2638338d87fSLinus Walleij 
264c809e37aSAndrew F. Davis 	nbank = DIV_ROUND_UP(ngpio, 32);
265c809e37aSAndrew F. Davis 	for (bank = 0; bank < nbank; bank++)
266b5cf3fd8SKeerthy 		chips->regs[bank] = gpio_base + offset_array[bank];
2678338d87fSLinus Walleij 
2688327e1baSKeerthy 	ret = devm_gpiochip_add_data(dev, &chips->chip, chips);
2698327e1baSKeerthy 	if (ret)
270587f7a69SAndrew F. Davis 		return ret;
2718327e1baSKeerthy 
272118150f2SKV Sujith 	platform_set_drvdata(pdev, chips);
273eb3744a2SKeerthy 	ret = davinci_gpio_irq_setup(pdev);
2745e7a0ce7SKeerthy 	if (ret)
275587f7a69SAndrew F. Davis 		return ret;
2765e7a0ce7SKeerthy 
2778338d87fSLinus Walleij 	return 0;
2788338d87fSLinus Walleij }
2798338d87fSLinus Walleij 
2808338d87fSLinus Walleij /*--------------------------------------------------------------------------*/
2818338d87fSLinus Walleij /*
2828338d87fSLinus Walleij  * We expect irqs will normally be set up as input pins, but they can also be
2838338d87fSLinus Walleij  * used as output pins ... which is convenient for testing.
2848338d87fSLinus Walleij  *
2858338d87fSLinus Walleij  * NOTE:  The first few GPIOs also have direct INTC hookups in addition
2868338d87fSLinus Walleij  * to their GPIOBNK0 irq, with a bit less overhead.
2878338d87fSLinus Walleij  *
2888338d87fSLinus Walleij  * All those INTC hookups (direct, plus several IRQ banks) can also
2898338d87fSLinus Walleij  * serve as EDMA event triggers.
2908338d87fSLinus Walleij  */
2918338d87fSLinus Walleij 
gpio_irq_mask(struct irq_data * d)292*7c5cd531SEmanuele Ghidoli static void gpio_irq_mask(struct irq_data *d)
2938338d87fSLinus Walleij {
2941765d671SThomas Gleixner 	struct davinci_gpio_regs __iomem *g = irq2regs(d);
29536c05519SKeerthy 	uintptr_t mask = (uintptr_t)irq_data_get_irq_handler_data(d);
2968338d87fSLinus Walleij 
297388291c3SLad, Prabhakar 	writel_relaxed(mask, &g->clr_falling);
298388291c3SLad, Prabhakar 	writel_relaxed(mask, &g->clr_rising);
2998338d87fSLinus Walleij }
3008338d87fSLinus Walleij 
gpio_irq_unmask(struct irq_data * d)301*7c5cd531SEmanuele Ghidoli static void gpio_irq_unmask(struct irq_data *d)
3028338d87fSLinus Walleij {
3031765d671SThomas Gleixner 	struct davinci_gpio_regs __iomem *g = irq2regs(d);
30436c05519SKeerthy 	uintptr_t mask = (uintptr_t)irq_data_get_irq_handler_data(d);
3058338d87fSLinus Walleij 	unsigned status = irqd_get_trigger_type(d);
3068338d87fSLinus Walleij 
3078338d87fSLinus Walleij 	status &= IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING;
3088338d87fSLinus Walleij 	if (!status)
3098338d87fSLinus Walleij 		status = IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING;
3108338d87fSLinus Walleij 
3118338d87fSLinus Walleij 	if (status & IRQ_TYPE_EDGE_FALLING)
312388291c3SLad, Prabhakar 		writel_relaxed(mask, &g->set_falling);
3138338d87fSLinus Walleij 	if (status & IRQ_TYPE_EDGE_RISING)
314388291c3SLad, Prabhakar 		writel_relaxed(mask, &g->set_rising);
3158338d87fSLinus Walleij }
3168338d87fSLinus Walleij 
gpio_irq_type(struct irq_data * d,unsigned trigger)3178338d87fSLinus Walleij static int gpio_irq_type(struct irq_data *d, unsigned trigger)
3188338d87fSLinus Walleij {
3198338d87fSLinus Walleij 	if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
3208338d87fSLinus Walleij 		return -EINVAL;
3218338d87fSLinus Walleij 
3228338d87fSLinus Walleij 	return 0;
3238338d87fSLinus Walleij }
3248338d87fSLinus Walleij 
3258338d87fSLinus Walleij static struct irq_chip gpio_irqchip = {
3268338d87fSLinus Walleij 	.name		= "GPIO",
327*7c5cd531SEmanuele Ghidoli 	.irq_unmask	= gpio_irq_unmask,
328*7c5cd531SEmanuele Ghidoli 	.irq_mask	= gpio_irq_mask,
3298338d87fSLinus Walleij 	.irq_set_type	= gpio_irq_type,
3307b75c470SDhruva Gole 	.flags		= IRQCHIP_SET_TYPE_MASKED | IRQCHIP_SKIP_SET_WAKE,
3318338d87fSLinus Walleij };
3328338d87fSLinus Walleij 
gpio_irq_handler(struct irq_desc * desc)333bd0b9ac4SThomas Gleixner static void gpio_irq_handler(struct irq_desc *desc)
3348338d87fSLinus Walleij {
3358338d87fSLinus Walleij 	struct davinci_gpio_regs __iomem *g;
3368338d87fSLinus Walleij 	u32 mask = 0xffff;
337b5cf3fd8SKeerthy 	int bank_num;
3388338d87fSLinus Walleij 	struct davinci_gpio_controller *d;
339b5cf3fd8SKeerthy 	struct davinci_gpio_irq_data *irqdata;
3408338d87fSLinus Walleij 
341b5cf3fd8SKeerthy 	irqdata = (struct davinci_gpio_irq_data *)irq_desc_get_handler_data(desc);
342b5cf3fd8SKeerthy 	bank_num = irqdata->bank_num;
343b5cf3fd8SKeerthy 	g = irqdata->regs;
344b5cf3fd8SKeerthy 	d = irqdata->chip;
3458338d87fSLinus Walleij 
3468338d87fSLinus Walleij 	/* we only care about one bank */
347b5cf3fd8SKeerthy 	if ((bank_num % 2) == 1)
3488338d87fSLinus Walleij 		mask <<= 16;
3498338d87fSLinus Walleij 
3508338d87fSLinus Walleij 	/* temporarily mask (level sensitive) parent IRQ */
3510d978eb7SGrygorii Strashko 	chained_irq_enter(irq_desc_get_chip(desc), desc);
3528338d87fSLinus Walleij 	while (1) {
3538338d87fSLinus Walleij 		u32		status;
3549211ff31SLad, Prabhakar 		int		bit;
355b5cf3fd8SKeerthy 		irq_hw_number_t hw_irq;
3568338d87fSLinus Walleij 
3578338d87fSLinus Walleij 		/* ack any irqs */
358388291c3SLad, Prabhakar 		status = readl_relaxed(&g->intstat) & mask;
3598338d87fSLinus Walleij 		if (!status)
3608338d87fSLinus Walleij 			break;
361388291c3SLad, Prabhakar 		writel_relaxed(status, &g->intstat);
3628338d87fSLinus Walleij 
3638338d87fSLinus Walleij 		/* now demux them to the right lowlevel handler */
3648338d87fSLinus Walleij 
3658338d87fSLinus Walleij 		while (status) {
3669211ff31SLad, Prabhakar 			bit = __ffs(status);
3679211ff31SLad, Prabhakar 			status &= ~BIT(bit);
368b5cf3fd8SKeerthy 			/* Max number of gpios per controller is 144 so
369b5cf3fd8SKeerthy 			 * hw_irq will be in [0..143]
370b5cf3fd8SKeerthy 			 */
371b5cf3fd8SKeerthy 			hw_irq = (bank_num / 2) * 32 + bit;
372b5cf3fd8SKeerthy 
373dbd1c54fSMarc Zyngier 			generic_handle_domain_irq(d->irq_domain, hw_irq);
3748338d87fSLinus Walleij 		}
3758338d87fSLinus Walleij 	}
3760d978eb7SGrygorii Strashko 	chained_irq_exit(irq_desc_get_chip(desc), desc);
3778338d87fSLinus Walleij 	/* now it may re-trigger */
3788338d87fSLinus Walleij }
3798338d87fSLinus Walleij 
gpio_to_irq_banked(struct gpio_chip * chip,unsigned offset)3808338d87fSLinus Walleij static int gpio_to_irq_banked(struct gpio_chip *chip, unsigned offset)
3818338d87fSLinus Walleij {
38272a1ca2cSLinus Walleij 	struct davinci_gpio_controller *d = gpiochip_get_data(chip);
3838338d87fSLinus Walleij 
3846075a8b2SGrygorii Strashko 	if (d->irq_domain)
385b5cf3fd8SKeerthy 		return irq_create_mapping(d->irq_domain, offset);
3866075a8b2SGrygorii Strashko 	else
3876075a8b2SGrygorii Strashko 		return -ENXIO;
3888338d87fSLinus Walleij }
3898338d87fSLinus Walleij 
gpio_to_irq_unbanked(struct gpio_chip * chip,unsigned offset)3908338d87fSLinus Walleij static int gpio_to_irq_unbanked(struct gpio_chip *chip, unsigned offset)
3918338d87fSLinus Walleij {
39272a1ca2cSLinus Walleij 	struct davinci_gpio_controller *d = gpiochip_get_data(chip);
3938338d87fSLinus Walleij 
394131a10a3SPhilip Avinash 	/*
395131a10a3SPhilip Avinash 	 * NOTE:  we assume for now that only irqs in the first gpio_chip
3968338d87fSLinus Walleij 	 * can provide direct-mapped IRQs to AINTC (up to 32 GPIOs).
3978338d87fSLinus Walleij 	 */
39834af1ab4SLad, Prabhakar 	if (offset < d->gpio_unbanked)
399eb3744a2SKeerthy 		return d->irqs[offset];
4008338d87fSLinus Walleij 	else
4018338d87fSLinus Walleij 		return -ENODEV;
4028338d87fSLinus Walleij }
4038338d87fSLinus Walleij 
gpio_irq_type_unbanked(struct irq_data * data,unsigned trigger)404ab2dde99SSekhar Nori static int gpio_irq_type_unbanked(struct irq_data *data, unsigned trigger)
4058338d87fSLinus Walleij {
406ab2dde99SSekhar Nori 	struct davinci_gpio_controller *d;
407ab2dde99SSekhar Nori 	struct davinci_gpio_regs __iomem *g;
408eb3744a2SKeerthy 	u32 mask, i;
409ab2dde99SSekhar Nori 
410c16edb8bSJiang Liu 	d = (struct davinci_gpio_controller *)irq_data_get_irq_handler_data(data);
4117f8e2a85SKeerthy 	g = (struct davinci_gpio_regs __iomem *)d->regs[0];
412eb3744a2SKeerthy 	for (i = 0; i < MAX_INT_PER_BANK; i++)
413eb3744a2SKeerthy 		if (data->irq == d->irqs[i])
414eb3744a2SKeerthy 			break;
415eb3744a2SKeerthy 
416eb3744a2SKeerthy 	if (i == MAX_INT_PER_BANK)
417eb3744a2SKeerthy 		return -EINVAL;
418eb3744a2SKeerthy 
419eb3744a2SKeerthy 	mask = __gpio_mask(i);
4208338d87fSLinus Walleij 
4218338d87fSLinus Walleij 	if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
4228338d87fSLinus Walleij 		return -EINVAL;
4238338d87fSLinus Walleij 
424388291c3SLad, Prabhakar 	writel_relaxed(mask, (trigger & IRQ_TYPE_EDGE_FALLING)
4258338d87fSLinus Walleij 		     ? &g->set_falling : &g->clr_falling);
426388291c3SLad, Prabhakar 	writel_relaxed(mask, (trigger & IRQ_TYPE_EDGE_RISING)
4278338d87fSLinus Walleij 		     ? &g->set_rising : &g->clr_rising);
4288338d87fSLinus Walleij 
4298338d87fSLinus Walleij 	return 0;
4308338d87fSLinus Walleij }
4318338d87fSLinus Walleij 
4329211ff31SLad, Prabhakar static int
davinci_gpio_irq_map(struct irq_domain * d,unsigned int irq,irq_hw_number_t hw)4339211ff31SLad, Prabhakar davinci_gpio_irq_map(struct irq_domain *d, unsigned int irq,
4349211ff31SLad, Prabhakar 		     irq_hw_number_t hw)
4359211ff31SLad, Prabhakar {
4368f7cf8c6SKeerthy 	struct davinci_gpio_controller *chips =
4378f7cf8c6SKeerthy 				(struct davinci_gpio_controller *)d->host_data;
438b5cf3fd8SKeerthy 	struct davinci_gpio_regs __iomem *g = chips->regs[hw / 32];
4399211ff31SLad, Prabhakar 
4409211ff31SLad, Prabhakar 	irq_set_chip_and_handler_name(irq, &gpio_irqchip, handle_simple_irq,
4419211ff31SLad, Prabhakar 				"davinci_gpio");
4429211ff31SLad, Prabhakar 	irq_set_irq_type(irq, IRQ_TYPE_NONE);
4439211ff31SLad, Prabhakar 	irq_set_chip_data(irq, (__force void *)g);
44436c05519SKeerthy 	irq_set_handler_data(irq, (void *)(uintptr_t)__gpio_mask(hw));
4459211ff31SLad, Prabhakar 
4469211ff31SLad, Prabhakar 	return 0;
4479211ff31SLad, Prabhakar }
4489211ff31SLad, Prabhakar 
4499211ff31SLad, Prabhakar static const struct irq_domain_ops davinci_gpio_irq_ops = {
4509211ff31SLad, Prabhakar 	.map = davinci_gpio_irq_map,
4519211ff31SLad, Prabhakar 	.xlate = irq_domain_xlate_onetwocell,
4529211ff31SLad, Prabhakar };
4539211ff31SLad, Prabhakar 
davinci_gpio_get_irq_chip(unsigned int irq)4540c6feb07SGrygorii Strashko static struct irq_chip *davinci_gpio_get_irq_chip(unsigned int irq)
4550c6feb07SGrygorii Strashko {
4560c6feb07SGrygorii Strashko 	static struct irq_chip_type gpio_unbanked;
4570c6feb07SGrygorii Strashko 
458ccdbddfeSGeliang Tang 	gpio_unbanked = *irq_data_get_chip_type(irq_get_irq_data(irq));
4590c6feb07SGrygorii Strashko 
4600c6feb07SGrygorii Strashko 	return &gpio_unbanked.chip;
4610c6feb07SGrygorii Strashko };
4620c6feb07SGrygorii Strashko 
keystone_gpio_get_irq_chip(unsigned int irq)4630c6feb07SGrygorii Strashko static struct irq_chip *keystone_gpio_get_irq_chip(unsigned int irq)
4640c6feb07SGrygorii Strashko {
4650c6feb07SGrygorii Strashko 	static struct irq_chip gpio_unbanked;
4660c6feb07SGrygorii Strashko 
4670c6feb07SGrygorii Strashko 	gpio_unbanked = *irq_get_chip(irq);
4680c6feb07SGrygorii Strashko 	return &gpio_unbanked;
4690c6feb07SGrygorii Strashko };
4700c6feb07SGrygorii Strashko 
4710c6feb07SGrygorii Strashko static const struct of_device_id davinci_gpio_ids[];
4720c6feb07SGrygorii Strashko 
4738338d87fSLinus Walleij /*
4748338d87fSLinus Walleij  * NOTE:  for suspend/resume, probably best to make a platform_device with
4758338d87fSLinus Walleij  * suspend_late/resume_resume calls hooking into results of the set_wake()
4768338d87fSLinus Walleij  * calls ... so if no gpios are wakeup events the clock can be disabled,
4778338d87fSLinus Walleij  * with outputs left at previously set levels, and so that VDD3P3V.IOPWDN0
4788338d87fSLinus Walleij  * (dm6446) can be set appropriately for GPIOV33 pins.
4798338d87fSLinus Walleij  */
4808338d87fSLinus Walleij 
davinci_gpio_irq_setup(struct platform_device * pdev)481eb3744a2SKeerthy static int davinci_gpio_irq_setup(struct platform_device *pdev)
4828338d87fSLinus Walleij {
48358c0f5aaSAlexander Shiyan 	unsigned	gpio, bank;
48458c0f5aaSAlexander Shiyan 	int		irq;
4856dc0048cSArvind Yadav 	int		ret;
4868338d87fSLinus Walleij 	struct clk	*clk;
4878338d87fSLinus Walleij 	u32		binten = 0;
488c1d013a7SKeerthy 	unsigned	ngpio;
489118150f2SKV Sujith 	struct device *dev = &pdev->dev;
490118150f2SKV Sujith 	struct davinci_gpio_controller *chips = platform_get_drvdata(pdev);
491118150f2SKV Sujith 	struct davinci_gpio_platform_data *pdata = dev->platform_data;
4928338d87fSLinus Walleij 	struct davinci_gpio_regs __iomem *g;
4936075a8b2SGrygorii Strashko 	struct irq_domain	*irq_domain = NULL;
4940c6feb07SGrygorii Strashko 	const struct of_device_id *match;
4950c6feb07SGrygorii Strashko 	struct irq_chip *irq_chip;
496b5cf3fd8SKeerthy 	struct davinci_gpio_irq_data *irqdata;
4970c6feb07SGrygorii Strashko 	gpio_get_irq_chip_cb_t gpio_get_irq_chip;
4980c6feb07SGrygorii Strashko 
4990c6feb07SGrygorii Strashko 	/*
5000c6feb07SGrygorii Strashko 	 * Use davinci_gpio_get_irq_chip by default to handle non DT cases
5010c6feb07SGrygorii Strashko 	 */
5020c6feb07SGrygorii Strashko 	gpio_get_irq_chip = davinci_gpio_get_irq_chip;
5030c6feb07SGrygorii Strashko 	match = of_match_device(of_match_ptr(davinci_gpio_ids),
5040c6feb07SGrygorii Strashko 				dev);
5050c6feb07SGrygorii Strashko 	if (match)
5060c6feb07SGrygorii Strashko 		gpio_get_irq_chip = (gpio_get_irq_chip_cb_t)match->data;
5078338d87fSLinus Walleij 
508118150f2SKV Sujith 	ngpio = pdata->ngpio;
509118150f2SKV Sujith 
510118150f2SKV Sujith 	clk = devm_clk_get(dev, "gpio");
5118338d87fSLinus Walleij 	if (IS_ERR(clk)) {
5121a9ef909SKeerthy 		dev_err(dev, "Error %ld getting gpio clock\n", PTR_ERR(clk));
5138338d87fSLinus Walleij 		return PTR_ERR(clk);
5148338d87fSLinus Walleij 	}
515eb3744a2SKeerthy 
5166dc0048cSArvind Yadav 	ret = clk_prepare_enable(clk);
5176dc0048cSArvind Yadav 	if (ret)
5186dc0048cSArvind Yadav 		return ret;
5198338d87fSLinus Walleij 
5206075a8b2SGrygorii Strashko 	if (!pdata->gpio_unbanked) {
521a1a3c2d5SBartosz Golaszewski 		irq = devm_irq_alloc_descs(dev, -1, 0, ngpio, 0);
5229211ff31SLad, Prabhakar 		if (irq < 0) {
5239211ff31SLad, Prabhakar 			dev_err(dev, "Couldn't allocate IRQ numbers\n");
5246dc0048cSArvind Yadav 			clk_disable_unprepare(clk);
5259211ff31SLad, Prabhakar 			return irq;
5269211ff31SLad, Prabhakar 		}
5279211ff31SLad, Prabhakar 
528310a7e60SKeerthy 		irq_domain = irq_domain_add_legacy(dev->of_node, ngpio, irq, 0,
5299211ff31SLad, Prabhakar 							&davinci_gpio_irq_ops,
5309211ff31SLad, Prabhakar 							chips);
5319211ff31SLad, Prabhakar 		if (!irq_domain) {
5329211ff31SLad, Prabhakar 			dev_err(dev, "Couldn't register an IRQ domain\n");
5336dc0048cSArvind Yadav 			clk_disable_unprepare(clk);
5349211ff31SLad, Prabhakar 			return -ENODEV;
5359211ff31SLad, Prabhakar 		}
5366075a8b2SGrygorii Strashko 	}
5379211ff31SLad, Prabhakar 
538131a10a3SPhilip Avinash 	/*
539029d14e9SAndy Shevchenko 	 * Arrange gpiod_to_irq() support, handling either direct IRQs or
5408338d87fSLinus Walleij 	 * banked IRQs.  Having GPIOs in the first GPIO bank use direct
5418338d87fSLinus Walleij 	 * IRQs, while the others use banked IRQs, would need some setup
5428338d87fSLinus Walleij 	 * tweaks to recognize hardware which can do that.
5438338d87fSLinus Walleij 	 */
544b5cf3fd8SKeerthy 	chips->chip.to_irq = gpio_to_irq_banked;
545b5cf3fd8SKeerthy 	chips->irq_domain = irq_domain;
5468338d87fSLinus Walleij 
5478338d87fSLinus Walleij 	/*
5488338d87fSLinus Walleij 	 * AINTC can handle direct/unbanked IRQs for GPIOs, with the GPIO
5498338d87fSLinus Walleij 	 * controller only handling trigger modes.  We currently assume no
5508338d87fSLinus Walleij 	 * IRQ mux conflicts; gpio_irq_type_unbanked() is only for GPIOs.
5518338d87fSLinus Walleij 	 */
552118150f2SKV Sujith 	if (pdata->gpio_unbanked) {
5538338d87fSLinus Walleij 		/* pass "bank 0" GPIO IRQs to AINTC */
554b5cf3fd8SKeerthy 		chips->chip.to_irq = gpio_to_irq_unbanked;
555b5cf3fd8SKeerthy 		chips->gpio_unbanked = pdata->gpio_unbanked;
5563685bbceSVitaly Andrianov 		binten = GENMASK(pdata->gpio_unbanked / 16, 0);
5578338d87fSLinus Walleij 
5588338d87fSLinus Walleij 		/* AINTC handles mask/unmask; GPIO handles triggering */
559eb3744a2SKeerthy 		irq = chips->irqs[0];
5600c6feb07SGrygorii Strashko 		irq_chip = gpio_get_irq_chip(irq);
5610c6feb07SGrygorii Strashko 		irq_chip->name = "GPIO-AINTC";
5620c6feb07SGrygorii Strashko 		irq_chip->irq_set_type = gpio_irq_type_unbanked;
5638338d87fSLinus Walleij 
5648338d87fSLinus Walleij 		/* default trigger: both edges */
565b5cf3fd8SKeerthy 		g = chips->regs[0];
566388291c3SLad, Prabhakar 		writel_relaxed(~0, &g->set_falling);
567388291c3SLad, Prabhakar 		writel_relaxed(~0, &g->set_rising);
5688338d87fSLinus Walleij 
5698338d87fSLinus Walleij 		/* set the direct IRQs up to use that irqchip */
570eb3744a2SKeerthy 		for (gpio = 0; gpio < pdata->gpio_unbanked; gpio++) {
571eb3744a2SKeerthy 			irq_set_chip(chips->irqs[gpio], irq_chip);
572eb3744a2SKeerthy 			irq_set_handler_data(chips->irqs[gpio], chips);
573eb3744a2SKeerthy 			irq_set_status_flags(chips->irqs[gpio],
574eb3744a2SKeerthy 					     IRQ_TYPE_EDGE_BOTH);
5758338d87fSLinus Walleij 		}
5768338d87fSLinus Walleij 
5778338d87fSLinus Walleij 		goto done;
5788338d87fSLinus Walleij 	}
5798338d87fSLinus Walleij 
5808338d87fSLinus Walleij 	/*
5818338d87fSLinus Walleij 	 * Or, AINTC can handle IRQs for banks of 16 GPIO IRQs, which we
5828338d87fSLinus Walleij 	 * then chain through our own handler.
5838338d87fSLinus Walleij 	 */
584eb3744a2SKeerthy 	for (gpio = 0, bank = 0; gpio < ngpio; bank++, gpio += 16) {
5858f7cf8c6SKeerthy 		/* disabled by default, enabled only as needed
5868f7cf8c6SKeerthy 		 * There are register sets for 32 GPIOs. 2 banks of 16
5878f7cf8c6SKeerthy 		 * GPIOs are covered by each set of registers hence divide by 2
5888f7cf8c6SKeerthy 		 */
589b5cf3fd8SKeerthy 		g = chips->regs[bank / 2];
590388291c3SLad, Prabhakar 		writel_relaxed(~0, &g->clr_falling);
591388291c3SLad, Prabhakar 		writel_relaxed(~0, &g->clr_rising);
5928338d87fSLinus Walleij 
5938338d87fSLinus Walleij 		/*
5948338d87fSLinus Walleij 		 * Each chip handles 32 gpios, and each irq bank consists of 16
5958338d87fSLinus Walleij 		 * gpio irqs. Pass the irq bank's corresponding controller to
5968338d87fSLinus Walleij 		 * the chained irq handler.
5978338d87fSLinus Walleij 		 */
598b5cf3fd8SKeerthy 		irqdata = devm_kzalloc(&pdev->dev,
599b5cf3fd8SKeerthy 				       sizeof(struct
600b5cf3fd8SKeerthy 					      davinci_gpio_irq_data),
601b5cf3fd8SKeerthy 					      GFP_KERNEL);
6026dc0048cSArvind Yadav 		if (!irqdata) {
6036dc0048cSArvind Yadav 			clk_disable_unprepare(clk);
604b5cf3fd8SKeerthy 			return -ENOMEM;
6056dc0048cSArvind Yadav 		}
606b5cf3fd8SKeerthy 
607b5cf3fd8SKeerthy 		irqdata->regs = g;
608b5cf3fd8SKeerthy 		irqdata->bank_num = bank;
609b5cf3fd8SKeerthy 		irqdata->chip = chips;
610b5cf3fd8SKeerthy 
611eb3744a2SKeerthy 		irq_set_chained_handler_and_data(chips->irqs[bank],
612eb3744a2SKeerthy 						 gpio_irq_handler, irqdata);
6138338d87fSLinus Walleij 
6148338d87fSLinus Walleij 		binten |= BIT(bank);
6158338d87fSLinus Walleij 	}
6168338d87fSLinus Walleij 
6178338d87fSLinus Walleij done:
618131a10a3SPhilip Avinash 	/*
619131a10a3SPhilip Avinash 	 * BINTEN -- per-bank interrupt enable. genirq would also let these
6208338d87fSLinus Walleij 	 * bits be set/cleared dynamically.
6218338d87fSLinus Walleij 	 */
622388291c3SLad, Prabhakar 	writel_relaxed(binten, gpio_base + BINTEN);
6238338d87fSLinus Walleij 
6248338d87fSLinus Walleij 	return 0;
6258338d87fSLinus Walleij }
626118150f2SKV Sujith 
davinci_gpio_save_context(struct davinci_gpio_controller * chips,u32 nbank)6270651a730SDevarsh Thakkar static void davinci_gpio_save_context(struct davinci_gpio_controller *chips,
6280651a730SDevarsh Thakkar 				      u32 nbank)
6290651a730SDevarsh Thakkar {
6300651a730SDevarsh Thakkar 	struct davinci_gpio_regs __iomem *g;
6310651a730SDevarsh Thakkar 	struct davinci_gpio_regs *context;
6320651a730SDevarsh Thakkar 	u32 bank;
6330651a730SDevarsh Thakkar 	void __iomem *base;
6340651a730SDevarsh Thakkar 
6350651a730SDevarsh Thakkar 	base = chips->regs[0] - offset_array[0];
6360651a730SDevarsh Thakkar 	chips->binten_context = readl_relaxed(base + BINTEN);
6370651a730SDevarsh Thakkar 
6380651a730SDevarsh Thakkar 	for (bank = 0; bank < nbank; bank++) {
6390651a730SDevarsh Thakkar 		g = chips->regs[bank];
6400651a730SDevarsh Thakkar 		context = &chips->context[bank];
6410651a730SDevarsh Thakkar 		context->dir = readl_relaxed(&g->dir);
6420651a730SDevarsh Thakkar 		context->set_data = readl_relaxed(&g->set_data);
6430651a730SDevarsh Thakkar 		context->set_rising = readl_relaxed(&g->set_rising);
6440651a730SDevarsh Thakkar 		context->set_falling = readl_relaxed(&g->set_falling);
6450651a730SDevarsh Thakkar 	}
6460651a730SDevarsh Thakkar 
6470651a730SDevarsh Thakkar 	/* Clear all interrupt status registers */
6480651a730SDevarsh Thakkar 	writel_relaxed(GENMASK(31, 0), &g->intstat);
6490651a730SDevarsh Thakkar }
6500651a730SDevarsh Thakkar 
davinci_gpio_restore_context(struct davinci_gpio_controller * chips,u32 nbank)6510651a730SDevarsh Thakkar static void davinci_gpio_restore_context(struct davinci_gpio_controller *chips,
6520651a730SDevarsh Thakkar 					 u32 nbank)
6530651a730SDevarsh Thakkar {
6540651a730SDevarsh Thakkar 	struct davinci_gpio_regs __iomem *g;
6550651a730SDevarsh Thakkar 	struct davinci_gpio_regs *context;
6560651a730SDevarsh Thakkar 	u32 bank;
6570651a730SDevarsh Thakkar 	void __iomem *base;
6580651a730SDevarsh Thakkar 
6590651a730SDevarsh Thakkar 	base = chips->regs[0] - offset_array[0];
6600651a730SDevarsh Thakkar 
6610651a730SDevarsh Thakkar 	if (readl_relaxed(base + BINTEN) != chips->binten_context)
6620651a730SDevarsh Thakkar 		writel_relaxed(chips->binten_context, base + BINTEN);
6630651a730SDevarsh Thakkar 
6640651a730SDevarsh Thakkar 	for (bank = 0; bank < nbank; bank++) {
6650651a730SDevarsh Thakkar 		g = chips->regs[bank];
6660651a730SDevarsh Thakkar 		context = &chips->context[bank];
6670651a730SDevarsh Thakkar 		if (readl_relaxed(&g->dir) != context->dir)
6680651a730SDevarsh Thakkar 			writel_relaxed(context->dir, &g->dir);
6690651a730SDevarsh Thakkar 		if (readl_relaxed(&g->set_data) != context->set_data)
6700651a730SDevarsh Thakkar 			writel_relaxed(context->set_data, &g->set_data);
6710651a730SDevarsh Thakkar 		if (readl_relaxed(&g->set_rising) != context->set_rising)
6720651a730SDevarsh Thakkar 			writel_relaxed(context->set_rising, &g->set_rising);
6730651a730SDevarsh Thakkar 		if (readl_relaxed(&g->set_falling) != context->set_falling)
6740651a730SDevarsh Thakkar 			writel_relaxed(context->set_falling, &g->set_falling);
6750651a730SDevarsh Thakkar 	}
6760651a730SDevarsh Thakkar }
6770651a730SDevarsh Thakkar 
davinci_gpio_suspend(struct device * dev)6780651a730SDevarsh Thakkar static int davinci_gpio_suspend(struct device *dev)
6790651a730SDevarsh Thakkar {
6800651a730SDevarsh Thakkar 	struct davinci_gpio_controller *chips = dev_get_drvdata(dev);
6810651a730SDevarsh Thakkar 	struct davinci_gpio_platform_data *pdata = dev_get_platdata(dev);
6820651a730SDevarsh Thakkar 	u32 nbank = DIV_ROUND_UP(pdata->ngpio, 32);
6830651a730SDevarsh Thakkar 
6840651a730SDevarsh Thakkar 	davinci_gpio_save_context(chips, nbank);
6850651a730SDevarsh Thakkar 
6860651a730SDevarsh Thakkar 	return 0;
6870651a730SDevarsh Thakkar }
6880651a730SDevarsh Thakkar 
davinci_gpio_resume(struct device * dev)6890651a730SDevarsh Thakkar static int davinci_gpio_resume(struct device *dev)
6900651a730SDevarsh Thakkar {
6910651a730SDevarsh Thakkar 	struct davinci_gpio_controller *chips = dev_get_drvdata(dev);
6920651a730SDevarsh Thakkar 	struct davinci_gpio_platform_data *pdata = dev_get_platdata(dev);
6930651a730SDevarsh Thakkar 	u32 nbank = DIV_ROUND_UP(pdata->ngpio, 32);
6940651a730SDevarsh Thakkar 
6950651a730SDevarsh Thakkar 	davinci_gpio_restore_context(chips, nbank);
6960651a730SDevarsh Thakkar 
6970651a730SDevarsh Thakkar 	return 0;
6980651a730SDevarsh Thakkar }
6990651a730SDevarsh Thakkar 
7008507f354SMin-Hua Chen static DEFINE_SIMPLE_DEV_PM_OPS(davinci_gpio_dev_pm_ops, davinci_gpio_suspend,
7010651a730SDevarsh Thakkar 			 davinci_gpio_resume);
7020651a730SDevarsh Thakkar 
703c770844cSKV Sujith static const struct of_device_id davinci_gpio_ids[] = {
7040c6feb07SGrygorii Strashko 	{ .compatible = "ti,keystone-gpio", keystone_gpio_get_irq_chip},
7056a4d8b6bSKeerthy 	{ .compatible = "ti,am654-gpio", keystone_gpio_get_irq_chip},
7060c6feb07SGrygorii Strashko 	{ .compatible = "ti,dm6441-gpio", davinci_gpio_get_irq_chip},
707c770844cSKV Sujith 	{ /* sentinel */ },
708c770844cSKV Sujith };
709c770844cSKV Sujith MODULE_DEVICE_TABLE(of, davinci_gpio_ids);
710c770844cSKV Sujith 
711118150f2SKV Sujith static struct platform_driver davinci_gpio_driver = {
712118150f2SKV Sujith 	.probe		= davinci_gpio_probe,
713118150f2SKV Sujith 	.driver		= {
714118150f2SKV Sujith 		.name		= "davinci_gpio",
7150651a730SDevarsh Thakkar 		.pm = pm_sleep_ptr(&davinci_gpio_dev_pm_ops),
716c770844cSKV Sujith 		.of_match_table	= of_match_ptr(davinci_gpio_ids),
717118150f2SKV Sujith 	},
718118150f2SKV Sujith };
719118150f2SKV Sujith 
7208e84a8e6SKrzysztof Kozlowski /*
721118150f2SKV Sujith  * GPIO driver registration needs to be done before machine_init functions
722118150f2SKV Sujith  * access GPIO. Hence davinci_gpio_drv_reg() is a postcore_initcall.
723118150f2SKV Sujith  */
davinci_gpio_drv_reg(void)724118150f2SKV Sujith static int __init davinci_gpio_drv_reg(void)
725118150f2SKV Sujith {
726118150f2SKV Sujith 	return platform_driver_register(&davinci_gpio_driver);
727118150f2SKV Sujith }
728118150f2SKV Sujith postcore_initcall(davinci_gpio_drv_reg);
7298dab99c9SGuillaume La Roque 
davinci_gpio_exit(void)7308dab99c9SGuillaume La Roque static void __exit davinci_gpio_exit(void)
7318dab99c9SGuillaume La Roque {
7328dab99c9SGuillaume La Roque 	platform_driver_unregister(&davinci_gpio_driver);
7338dab99c9SGuillaume La Roque }
7348dab99c9SGuillaume La Roque module_exit(davinci_gpio_exit);
7358dab99c9SGuillaume La Roque 
7368dab99c9SGuillaume La Roque MODULE_AUTHOR("Jan Kotas <jank@cadence.com>");
7378dab99c9SGuillaume La Roque MODULE_DESCRIPTION("DAVINCI GPIO driver");
7388dab99c9SGuillaume La Roque MODULE_LICENSE("GPL");
7398dab99c9SGuillaume La Roque MODULE_ALIAS("platform:gpio-davinci");
740