xref: /openbmc/linux/drivers/gpio/gpio-tegra.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ea5a9607SGrant Likely /*
3ea5a9607SGrant Likely  * arch/arm/mach-tegra/gpio.c
4ea5a9607SGrant Likely  *
5ea5a9607SGrant Likely  * Copyright (c) 2010 Google, Inc
611da9054SLinus Walleij  * Copyright (c) 2011-2016, NVIDIA CORPORATION.  All rights reserved.
7ea5a9607SGrant Likely  *
8ea5a9607SGrant Likely  * Author:
9ea5a9607SGrant Likely  *	Erik Gilling <konkers@google.com>
10ea5a9607SGrant Likely  */
11ea5a9607SGrant Likely 
12641d0342SThierry Reding #include <linux/err.h>
13ea5a9607SGrant Likely #include <linux/init.h>
14ea5a9607SGrant Likely #include <linux/irq.h>
15ea5a9607SGrant Likely #include <linux/interrupt.h>
16ea5a9607SGrant Likely #include <linux/io.h>
1721041dabSLinus Walleij #include <linux/gpio/driver.h>
18*e91d0f05SRob Herring #include <linux/of.h>
1988d8951eSStephen Warren #include <linux/platform_device.h>
2088d8951eSStephen Warren #include <linux/module.h>
217d1aa08aSSvyatoslav Ryhel #include <linux/seq_file.h>
226f74dc9bSStephen Warren #include <linux/irqdomain.h>
23de88cbb7SCatalin Marinas #include <linux/irqchip/chained_irq.h>
243e215d0aSStephen Warren #include <linux/pinctrl/consumer.h>
258939ddc7SLaxman Dewangan #include <linux/pm.h>
26ea5a9607SGrant Likely 
27ea5a9607SGrant Likely #define GPIO_BANK(x)		((x) >> 5)
28ea5a9607SGrant Likely #define GPIO_PORT(x)		(((x) >> 3) & 0x3)
29ea5a9607SGrant Likely #define GPIO_BIT(x)		((x) & 0x7)
30ea5a9607SGrant Likely 
31b546be0dSLaxman Dewangan #define GPIO_REG(tgi, x)	(GPIO_BANK(x) * tgi->soc->bank_stride + \
325c1e2c9dSStephen Warren 					GPIO_PORT(x) * 4)
33ea5a9607SGrant Likely 
34b546be0dSLaxman Dewangan #define GPIO_CNF(t, x)		(GPIO_REG(t, x) + 0x00)
35b546be0dSLaxman Dewangan #define GPIO_OE(t, x)		(GPIO_REG(t, x) + 0x10)
36b546be0dSLaxman Dewangan #define GPIO_OUT(t, x)		(GPIO_REG(t, x) + 0X20)
37b546be0dSLaxman Dewangan #define GPIO_IN(t, x)		(GPIO_REG(t, x) + 0x30)
38b546be0dSLaxman Dewangan #define GPIO_INT_STA(t, x)	(GPIO_REG(t, x) + 0x40)
39b546be0dSLaxman Dewangan #define GPIO_INT_ENB(t, x)	(GPIO_REG(t, x) + 0x50)
40b546be0dSLaxman Dewangan #define GPIO_INT_LVL(t, x)	(GPIO_REG(t, x) + 0x60)
41b546be0dSLaxman Dewangan #define GPIO_INT_CLR(t, x)	(GPIO_REG(t, x) + 0x70)
423737de42SLaxman Dewangan #define GPIO_DBC_CNT(t, x)	(GPIO_REG(t, x) + 0xF0)
433737de42SLaxman Dewangan 
44ea5a9607SGrant Likely 
45b546be0dSLaxman Dewangan #define GPIO_MSK_CNF(t, x)	(GPIO_REG(t, x) + t->soc->upper_offset + 0x00)
46b546be0dSLaxman Dewangan #define GPIO_MSK_OE(t, x)	(GPIO_REG(t, x) + t->soc->upper_offset + 0x10)
47b546be0dSLaxman Dewangan #define GPIO_MSK_OUT(t, x)	(GPIO_REG(t, x) + t->soc->upper_offset + 0X20)
483737de42SLaxman Dewangan #define GPIO_MSK_DBC_EN(t, x)	(GPIO_REG(t, x) + t->soc->upper_offset + 0x30)
49b546be0dSLaxman Dewangan #define GPIO_MSK_INT_STA(t, x)	(GPIO_REG(t, x) + t->soc->upper_offset + 0x40)
50b546be0dSLaxman Dewangan #define GPIO_MSK_INT_ENB(t, x)	(GPIO_REG(t, x) + t->soc->upper_offset + 0x50)
51b546be0dSLaxman Dewangan #define GPIO_MSK_INT_LVL(t, x)	(GPIO_REG(t, x) + t->soc->upper_offset + 0x60)
52ea5a9607SGrant Likely 
53ea5a9607SGrant Likely #define GPIO_INT_LVL_MASK		0x010101
54ea5a9607SGrant Likely #define GPIO_INT_LVL_EDGE_RISING	0x000101
55ea5a9607SGrant Likely #define GPIO_INT_LVL_EDGE_FALLING	0x000100
56ea5a9607SGrant Likely #define GPIO_INT_LVL_EDGE_BOTH		0x010100
57ea5a9607SGrant Likely #define GPIO_INT_LVL_LEVEL_HIGH		0x000001
58ea5a9607SGrant Likely #define GPIO_INT_LVL_LEVEL_LOW		0x000000
59ea5a9607SGrant Likely 
60b546be0dSLaxman Dewangan struct tegra_gpio_info;
61b546be0dSLaxman Dewangan 
62ea5a9607SGrant Likely struct tegra_gpio_bank {
63539b7a39SThierry Reding 	unsigned int bank;
6437174f33SDmitry Osipenko 
6537174f33SDmitry Osipenko 	/*
6637174f33SDmitry Osipenko 	 * IRQ-core code uses raw locking, and thus, nested locking also
6737174f33SDmitry Osipenko 	 * should be raw in order not to trip spinlock debug warnings.
6837174f33SDmitry Osipenko 	 */
6937174f33SDmitry Osipenko 	raw_spinlock_t lvl_lock[4];
7037174f33SDmitry Osipenko 
7137174f33SDmitry Osipenko 	/* Lock for updating debounce count register */
7237174f33SDmitry Osipenko 	spinlock_t dbc_lock[4];
7337174f33SDmitry Osipenko 
748939ddc7SLaxman Dewangan #ifdef CONFIG_PM_SLEEP
75ea5a9607SGrant Likely 	u32 cnf[4];
76ea5a9607SGrant Likely 	u32 out[4];
77ea5a9607SGrant Likely 	u32 oe[4];
78ea5a9607SGrant Likely 	u32 int_enb[4];
79ea5a9607SGrant Likely 	u32 int_lvl[4];
80203f31cbSJoseph Lo 	u32 wake_enb[4];
813737de42SLaxman Dewangan 	u32 dbc_enb[4];
82ea5a9607SGrant Likely #endif
833737de42SLaxman Dewangan 	u32 dbc_cnt[4];
84ea5a9607SGrant Likely };
85ea5a9607SGrant Likely 
86171b92c8SLaxman Dewangan struct tegra_gpio_soc_config {
873737de42SLaxman Dewangan 	bool debounce_supported;
88171b92c8SLaxman Dewangan 	u32 bank_stride;
89171b92c8SLaxman Dewangan 	u32 upper_offset;
90171b92c8SLaxman Dewangan };
91171b92c8SLaxman Dewangan 
92b546be0dSLaxman Dewangan struct tegra_gpio_info {
93b546be0dSLaxman Dewangan 	struct device				*dev;
94b546be0dSLaxman Dewangan 	void __iomem				*regs;
95b546be0dSLaxman Dewangan 	struct tegra_gpio_bank			*bank_info;
96b546be0dSLaxman Dewangan 	const struct tegra_gpio_soc_config	*soc;
97b546be0dSLaxman Dewangan 	struct gpio_chip			gc;
98b546be0dSLaxman Dewangan 	u32					bank_count;
9966fecef5SThierry Reding 	unsigned int				*irqs;
100b546be0dSLaxman Dewangan };
10188d8951eSStephen Warren 
tegra_gpio_writel(struct tegra_gpio_info * tgi,u32 val,u32 reg)102b546be0dSLaxman Dewangan static inline void tegra_gpio_writel(struct tegra_gpio_info *tgi,
103b546be0dSLaxman Dewangan 				     u32 val, u32 reg)
10488d8951eSStephen Warren {
105fc782e47SDmitry Osipenko 	writel_relaxed(val, tgi->regs + reg);
10688d8951eSStephen Warren }
10788d8951eSStephen Warren 
tegra_gpio_readl(struct tegra_gpio_info * tgi,u32 reg)108b546be0dSLaxman Dewangan static inline u32 tegra_gpio_readl(struct tegra_gpio_info *tgi, u32 reg)
10988d8951eSStephen Warren {
110fc782e47SDmitry Osipenko 	return readl_relaxed(tgi->regs + reg);
11188d8951eSStephen Warren }
112ea5a9607SGrant Likely 
tegra_gpio_compose(unsigned int bank,unsigned int port,unsigned int bit)113539b7a39SThierry Reding static unsigned int tegra_gpio_compose(unsigned int bank, unsigned int port,
114539b7a39SThierry Reding 				       unsigned int bit)
115ea5a9607SGrant Likely {
116ea5a9607SGrant Likely 	return (bank << 5) | ((port & 0x3) << 3) | (bit & 0x7);
117ea5a9607SGrant Likely }
118ea5a9607SGrant Likely 
tegra_gpio_mask_write(struct tegra_gpio_info * tgi,u32 reg,unsigned int gpio,u32 value)119b546be0dSLaxman Dewangan static void tegra_gpio_mask_write(struct tegra_gpio_info *tgi, u32 reg,
120539b7a39SThierry Reding 				  unsigned int gpio, u32 value)
121ea5a9607SGrant Likely {
122ea5a9607SGrant Likely 	u32 val;
123ea5a9607SGrant Likely 
124ea5a9607SGrant Likely 	val = 0x100 << GPIO_BIT(gpio);
125ea5a9607SGrant Likely 	if (value)
126ea5a9607SGrant Likely 		val |= 1 << GPIO_BIT(gpio);
127b546be0dSLaxman Dewangan 	tegra_gpio_writel(tgi, val, reg);
128ea5a9607SGrant Likely }
129ea5a9607SGrant Likely 
tegra_gpio_enable(struct tegra_gpio_info * tgi,unsigned int gpio)130539b7a39SThierry Reding static void tegra_gpio_enable(struct tegra_gpio_info *tgi, unsigned int gpio)
131ea5a9607SGrant Likely {
132b546be0dSLaxman Dewangan 	tegra_gpio_mask_write(tgi, GPIO_MSK_CNF(tgi, gpio), gpio, 1);
133ea5a9607SGrant Likely }
134ea5a9607SGrant Likely 
tegra_gpio_disable(struct tegra_gpio_info * tgi,unsigned int gpio)135539b7a39SThierry Reding static void tegra_gpio_disable(struct tegra_gpio_info *tgi, unsigned int gpio)
136ea5a9607SGrant Likely {
137b546be0dSLaxman Dewangan 	tegra_gpio_mask_write(tgi, GPIO_MSK_CNF(tgi, gpio), gpio, 0);
138ea5a9607SGrant Likely }
139ea5a9607SGrant Likely 
tegra_gpio_request(struct gpio_chip * chip,unsigned int offset)1404bc17860SThierry Reding static int tegra_gpio_request(struct gpio_chip *chip, unsigned int offset)
1413e215d0aSStephen Warren {
14211da9054SLinus Walleij 	return pinctrl_gpio_request(chip->base + offset);
1433e215d0aSStephen Warren }
1443e215d0aSStephen Warren 
tegra_gpio_free(struct gpio_chip * chip,unsigned int offset)1454bc17860SThierry Reding static void tegra_gpio_free(struct gpio_chip *chip, unsigned int offset)
1463e215d0aSStephen Warren {
147b546be0dSLaxman Dewangan 	struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
148b546be0dSLaxman Dewangan 
14911da9054SLinus Walleij 	pinctrl_gpio_free(chip->base + offset);
150b546be0dSLaxman Dewangan 	tegra_gpio_disable(tgi, offset);
1513e215d0aSStephen Warren }
1523e215d0aSStephen Warren 
tegra_gpio_set(struct gpio_chip * chip,unsigned int offset,int value)1534bc17860SThierry Reding static void tegra_gpio_set(struct gpio_chip *chip, unsigned int offset,
1544bc17860SThierry Reding 			   int value)
155ea5a9607SGrant Likely {
156b546be0dSLaxman Dewangan 	struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
157b546be0dSLaxman Dewangan 
158b546be0dSLaxman Dewangan 	tegra_gpio_mask_write(tgi, GPIO_MSK_OUT(tgi, offset), offset, value);
159ea5a9607SGrant Likely }
160ea5a9607SGrant Likely 
tegra_gpio_get(struct gpio_chip * chip,unsigned int offset)1614bc17860SThierry Reding static int tegra_gpio_get(struct gpio_chip *chip, unsigned int offset)
162ea5a9607SGrant Likely {
163b546be0dSLaxman Dewangan 	struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
164539b7a39SThierry Reding 	unsigned int bval = BIT(GPIO_BIT(offset));
165195812e4SLaxman Dewangan 
166b546be0dSLaxman Dewangan 	/* If gpio is in output mode then read from the out value */
167b546be0dSLaxman Dewangan 	if (tegra_gpio_readl(tgi, GPIO_OE(tgi, offset)) & bval)
168b546be0dSLaxman Dewangan 		return !!(tegra_gpio_readl(tgi, GPIO_OUT(tgi, offset)) & bval);
169b546be0dSLaxman Dewangan 
170b546be0dSLaxman Dewangan 	return !!(tegra_gpio_readl(tgi, GPIO_IN(tgi, offset)) & bval);
171ea5a9607SGrant Likely }
172ea5a9607SGrant Likely 
tegra_gpio_direction_input(struct gpio_chip * chip,unsigned int offset)1734bc17860SThierry Reding static int tegra_gpio_direction_input(struct gpio_chip *chip,
1744bc17860SThierry Reding 				      unsigned int offset)
175ea5a9607SGrant Likely {
176b546be0dSLaxman Dewangan 	struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
17711da9054SLinus Walleij 	int ret;
178b546be0dSLaxman Dewangan 
179b546be0dSLaxman Dewangan 	tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, offset), offset, 0);
180b546be0dSLaxman Dewangan 	tegra_gpio_enable(tgi, offset);
18111da9054SLinus Walleij 
18211da9054SLinus Walleij 	ret = pinctrl_gpio_direction_input(chip->base + offset);
18311da9054SLinus Walleij 	if (ret < 0)
18411da9054SLinus Walleij 		dev_err(tgi->dev,
18511da9054SLinus Walleij 			"Failed to set pinctrl input direction of GPIO %d: %d",
18611da9054SLinus Walleij 			 chip->base + offset, ret);
18711da9054SLinus Walleij 
18811da9054SLinus Walleij 	return ret;
189ea5a9607SGrant Likely }
190ea5a9607SGrant Likely 
tegra_gpio_direction_output(struct gpio_chip * chip,unsigned int offset,int value)1914bc17860SThierry Reding static int tegra_gpio_direction_output(struct gpio_chip *chip,
1924bc17860SThierry Reding 				       unsigned int offset,
193ea5a9607SGrant Likely 				       int value)
194ea5a9607SGrant Likely {
195b546be0dSLaxman Dewangan 	struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
19611da9054SLinus Walleij 	int ret;
197b546be0dSLaxman Dewangan 
198ea5a9607SGrant Likely 	tegra_gpio_set(chip, offset, value);
199b546be0dSLaxman Dewangan 	tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, offset), offset, 1);
200b546be0dSLaxman Dewangan 	tegra_gpio_enable(tgi, offset);
20111da9054SLinus Walleij 
20211da9054SLinus Walleij 	ret = pinctrl_gpio_direction_output(chip->base + offset);
20311da9054SLinus Walleij 	if (ret < 0)
20411da9054SLinus Walleij 		dev_err(tgi->dev,
20511da9054SLinus Walleij 			"Failed to set pinctrl output direction of GPIO %d: %d",
20611da9054SLinus Walleij 			 chip->base + offset, ret);
20711da9054SLinus Walleij 
20811da9054SLinus Walleij 	return ret;
209ea5a9607SGrant Likely }
210ea5a9607SGrant Likely 
tegra_gpio_get_direction(struct gpio_chip * chip,unsigned int offset)2114bc17860SThierry Reding static int tegra_gpio_get_direction(struct gpio_chip *chip,
2124bc17860SThierry Reding 				    unsigned int offset)
213f002d07cSLaxman Dewangan {
214f002d07cSLaxman Dewangan 	struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
215f002d07cSLaxman Dewangan 	u32 pin_mask = BIT(GPIO_BIT(offset));
216f002d07cSLaxman Dewangan 	u32 cnf, oe;
217f002d07cSLaxman Dewangan 
218f002d07cSLaxman Dewangan 	cnf = tegra_gpio_readl(tgi, GPIO_CNF(tgi, offset));
219f002d07cSLaxman Dewangan 	if (!(cnf & pin_mask))
220f002d07cSLaxman Dewangan 		return -EINVAL;
221f002d07cSLaxman Dewangan 
222f002d07cSLaxman Dewangan 	oe = tegra_gpio_readl(tgi, GPIO_OE(tgi, offset));
223f002d07cSLaxman Dewangan 
224e42615ecSMatti Vaittinen 	if (oe & pin_mask)
225e42615ecSMatti Vaittinen 		return GPIO_LINE_DIRECTION_OUT;
226e42615ecSMatti Vaittinen 
227e42615ecSMatti Vaittinen 	return GPIO_LINE_DIRECTION_IN;
228f002d07cSLaxman Dewangan }
229f002d07cSLaxman Dewangan 
tegra_gpio_set_debounce(struct gpio_chip * chip,unsigned int offset,unsigned int debounce)2303737de42SLaxman Dewangan static int tegra_gpio_set_debounce(struct gpio_chip *chip, unsigned int offset,
2313737de42SLaxman Dewangan 				   unsigned int debounce)
2323737de42SLaxman Dewangan {
2333737de42SLaxman Dewangan 	struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
2343737de42SLaxman Dewangan 	struct tegra_gpio_bank *bank = &tgi->bank_info[GPIO_BANK(offset)];
2353737de42SLaxman Dewangan 	unsigned int debounce_ms = DIV_ROUND_UP(debounce, 1000);
2363737de42SLaxman Dewangan 	unsigned long flags;
237539b7a39SThierry Reding 	unsigned int port;
2383737de42SLaxman Dewangan 
2393737de42SLaxman Dewangan 	if (!debounce_ms) {
2403737de42SLaxman Dewangan 		tegra_gpio_mask_write(tgi, GPIO_MSK_DBC_EN(tgi, offset),
2413737de42SLaxman Dewangan 				      offset, 0);
2423737de42SLaxman Dewangan 		return 0;
2433737de42SLaxman Dewangan 	}
2443737de42SLaxman Dewangan 
2453737de42SLaxman Dewangan 	debounce_ms = min(debounce_ms, 255U);
2463737de42SLaxman Dewangan 	port = GPIO_PORT(offset);
2473737de42SLaxman Dewangan 
2483737de42SLaxman Dewangan 	/* There is only one debounce count register per port and hence
2493737de42SLaxman Dewangan 	 * set the maximum of current and requested debounce time.
2503737de42SLaxman Dewangan 	 */
2513737de42SLaxman Dewangan 	spin_lock_irqsave(&bank->dbc_lock[port], flags);
2523737de42SLaxman Dewangan 	if (bank->dbc_cnt[port] < debounce_ms) {
2533737de42SLaxman Dewangan 		tegra_gpio_writel(tgi, debounce_ms, GPIO_DBC_CNT(tgi, offset));
2543737de42SLaxman Dewangan 		bank->dbc_cnt[port] = debounce_ms;
2553737de42SLaxman Dewangan 	}
2563737de42SLaxman Dewangan 	spin_unlock_irqrestore(&bank->dbc_lock[port], flags);
2573737de42SLaxman Dewangan 
2583737de42SLaxman Dewangan 	tegra_gpio_mask_write(tgi, GPIO_MSK_DBC_EN(tgi, offset), offset, 1);
2593737de42SLaxman Dewangan 
2603737de42SLaxman Dewangan 	return 0;
2613737de42SLaxman Dewangan }
2623737de42SLaxman Dewangan 
tegra_gpio_set_config(struct gpio_chip * chip,unsigned int offset,unsigned long config)2632956b5d9SMika Westerberg static int tegra_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
2642956b5d9SMika Westerberg 				 unsigned long config)
2652956b5d9SMika Westerberg {
2662956b5d9SMika Westerberg 	u32 debounce;
2672956b5d9SMika Westerberg 
2682956b5d9SMika Westerberg 	if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
2692956b5d9SMika Westerberg 		return -ENOTSUPP;
2702956b5d9SMika Westerberg 
2712956b5d9SMika Westerberg 	debounce = pinconf_to_config_argument(config);
2722956b5d9SMika Westerberg 	return tegra_gpio_set_debounce(chip, offset, debounce);
2732956b5d9SMika Westerberg }
2742956b5d9SMika Westerberg 
tegra_gpio_irq_ack(struct irq_data * d)275ea5a9607SGrant Likely static void tegra_gpio_irq_ack(struct irq_data *d)
276ea5a9607SGrant Likely {
27766fecef5SThierry Reding 	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
27866fecef5SThierry Reding 	struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
279539b7a39SThierry Reding 	unsigned int gpio = d->hwirq;
280ea5a9607SGrant Likely 
281b546be0dSLaxman Dewangan 	tegra_gpio_writel(tgi, 1 << GPIO_BIT(gpio), GPIO_INT_CLR(tgi, gpio));
282ea5a9607SGrant Likely }
283ea5a9607SGrant Likely 
tegra_gpio_irq_mask(struct irq_data * d)284ea5a9607SGrant Likely static void tegra_gpio_irq_mask(struct irq_data *d)
285ea5a9607SGrant Likely {
28666fecef5SThierry Reding 	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
28766fecef5SThierry Reding 	struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
288539b7a39SThierry Reding 	unsigned int gpio = d->hwirq;
289ea5a9607SGrant Likely 
290b546be0dSLaxman Dewangan 	tegra_gpio_mask_write(tgi, GPIO_MSK_INT_ENB(tgi, gpio), gpio, 0);
2917d1aa08aSSvyatoslav Ryhel 	gpiochip_disable_irq(chip, gpio);
292ea5a9607SGrant Likely }
293ea5a9607SGrant Likely 
tegra_gpio_irq_unmask(struct irq_data * d)294ea5a9607SGrant Likely static void tegra_gpio_irq_unmask(struct irq_data *d)
295ea5a9607SGrant Likely {
29666fecef5SThierry Reding 	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
29766fecef5SThierry Reding 	struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
298539b7a39SThierry Reding 	unsigned int gpio = d->hwirq;
299ea5a9607SGrant Likely 
3007d1aa08aSSvyatoslav Ryhel 	gpiochip_enable_irq(chip, gpio);
301b546be0dSLaxman Dewangan 	tegra_gpio_mask_write(tgi, GPIO_MSK_INT_ENB(tgi, gpio), gpio, 1);
302ea5a9607SGrant Likely }
303ea5a9607SGrant Likely 
tegra_gpio_irq_set_type(struct irq_data * d,unsigned int type)304ea5a9607SGrant Likely static int tegra_gpio_irq_set_type(struct irq_data *d, unsigned int type)
305ea5a9607SGrant Likely {
306539b7a39SThierry Reding 	unsigned int gpio = d->hwirq, port = GPIO_PORT(gpio), lvl_type;
30766fecef5SThierry Reding 	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
30866fecef5SThierry Reding 	struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
30966fecef5SThierry Reding 	struct tegra_gpio_bank *bank;
310ea5a9607SGrant Likely 	unsigned long flags;
311df231f28SStephen Warren 	int ret;
31266fecef5SThierry Reding 	u32 val;
31366fecef5SThierry Reding 
31466fecef5SThierry Reding 	bank = &tgi->bank_info[GPIO_BANK(d->hwirq)];
315ea5a9607SGrant Likely 
316ea5a9607SGrant Likely 	switch (type & IRQ_TYPE_SENSE_MASK) {
317ea5a9607SGrant Likely 	case IRQ_TYPE_EDGE_RISING:
318ea5a9607SGrant Likely 		lvl_type = GPIO_INT_LVL_EDGE_RISING;
319ea5a9607SGrant Likely 		break;
320ea5a9607SGrant Likely 
321ea5a9607SGrant Likely 	case IRQ_TYPE_EDGE_FALLING:
322ea5a9607SGrant Likely 		lvl_type = GPIO_INT_LVL_EDGE_FALLING;
323ea5a9607SGrant Likely 		break;
324ea5a9607SGrant Likely 
325ea5a9607SGrant Likely 	case IRQ_TYPE_EDGE_BOTH:
326ea5a9607SGrant Likely 		lvl_type = GPIO_INT_LVL_EDGE_BOTH;
327ea5a9607SGrant Likely 		break;
328ea5a9607SGrant Likely 
329ea5a9607SGrant Likely 	case IRQ_TYPE_LEVEL_HIGH:
330ea5a9607SGrant Likely 		lvl_type = GPIO_INT_LVL_LEVEL_HIGH;
331ea5a9607SGrant Likely 		break;
332ea5a9607SGrant Likely 
333ea5a9607SGrant Likely 	case IRQ_TYPE_LEVEL_LOW:
334ea5a9607SGrant Likely 		lvl_type = GPIO_INT_LVL_LEVEL_LOW;
335ea5a9607SGrant Likely 		break;
336ea5a9607SGrant Likely 
337ea5a9607SGrant Likely 	default:
338ea5a9607SGrant Likely 		return -EINVAL;
339ea5a9607SGrant Likely 	}
340ea5a9607SGrant Likely 
34137174f33SDmitry Osipenko 	raw_spin_lock_irqsave(&bank->lvl_lock[port], flags);
342ea5a9607SGrant Likely 
343b546be0dSLaxman Dewangan 	val = tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio));
344ea5a9607SGrant Likely 	val &= ~(GPIO_INT_LVL_MASK << GPIO_BIT(gpio));
345ea5a9607SGrant Likely 	val |= lvl_type << GPIO_BIT(gpio);
346b546be0dSLaxman Dewangan 	tegra_gpio_writel(tgi, val, GPIO_INT_LVL(tgi, gpio));
347ea5a9607SGrant Likely 
34837174f33SDmitry Osipenko 	raw_spin_unlock_irqrestore(&bank->lvl_lock[port], flags);
349ea5a9607SGrant Likely 
350b546be0dSLaxman Dewangan 	tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, gpio), gpio, 0);
351b546be0dSLaxman Dewangan 	tegra_gpio_enable(tgi, gpio);
352d941136fSStephen Warren 
353f78709a5SDmitry Osipenko 	ret = gpiochip_lock_as_irq(&tgi->gc, gpio);
354f78709a5SDmitry Osipenko 	if (ret) {
355f78709a5SDmitry Osipenko 		dev_err(tgi->dev,
356f78709a5SDmitry Osipenko 			"unable to lock Tegra GPIO %u as IRQ\n", gpio);
357f78709a5SDmitry Osipenko 		tegra_gpio_disable(tgi, gpio);
358f78709a5SDmitry Osipenko 		return ret;
359f78709a5SDmitry Osipenko 	}
360f78709a5SDmitry Osipenko 
361ea5a9607SGrant Likely 	if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
362f170d71eSThomas Gleixner 		irq_set_handler_locked(d, handle_level_irq);
363ea5a9607SGrant Likely 	else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
364f170d71eSThomas Gleixner 		irq_set_handler_locked(d, handle_edge_irq);
365ea5a9607SGrant Likely 
36666fecef5SThierry Reding 	if (d->parent_data)
36766fecef5SThierry Reding 		ret = irq_chip_set_type_parent(d, type);
36866fecef5SThierry Reding 
36966fecef5SThierry Reding 	return ret;
370ea5a9607SGrant Likely }
371ea5a9607SGrant Likely 
tegra_gpio_irq_shutdown(struct irq_data * d)372df231f28SStephen Warren static void tegra_gpio_irq_shutdown(struct irq_data *d)
373df231f28SStephen Warren {
37466fecef5SThierry Reding 	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
37566fecef5SThierry Reding 	struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
376539b7a39SThierry Reding 	unsigned int gpio = d->hwirq;
377df231f28SStephen Warren 
3780cf253eeSStephen Warren 	tegra_gpio_irq_mask(d);
379b546be0dSLaxman Dewangan 	gpiochip_unlock_as_irq(&tgi->gc, gpio);
380df231f28SStephen Warren }
381df231f28SStephen Warren 
tegra_gpio_irq_handler(struct irq_desc * desc)382bd0b9ac4SThomas Gleixner static void tegra_gpio_irq_handler(struct irq_desc *desc)
383ea5a9607SGrant Likely {
38466fecef5SThierry Reding 	struct tegra_gpio_info *tgi = irq_desc_get_handler_data(desc);
385ea5a9607SGrant Likely 	struct irq_chip *chip = irq_desc_get_chip(desc);
38666fecef5SThierry Reding 	struct irq_domain *domain = tgi->gc.irq.domain;
38766fecef5SThierry Reding 	unsigned int irq = irq_desc_get_irq(desc);
38866fecef5SThierry Reding 	struct tegra_gpio_bank *bank = NULL;
38966fecef5SThierry Reding 	unsigned int port, pin, gpio, i;
39066fecef5SThierry Reding 	bool unmasked = false;
39166fecef5SThierry Reding 	unsigned long sta;
39266fecef5SThierry Reding 	u32 lvl;
39366fecef5SThierry Reding 
39466fecef5SThierry Reding 	for (i = 0; i < tgi->bank_count; i++) {
39566fecef5SThierry Reding 		if (tgi->irqs[i] == irq) {
39666fecef5SThierry Reding 			bank = &tgi->bank_info[i];
39766fecef5SThierry Reding 			break;
39866fecef5SThierry Reding 		}
39966fecef5SThierry Reding 	}
40066fecef5SThierry Reding 
40166fecef5SThierry Reding 	if (WARN_ON(bank == NULL))
40266fecef5SThierry Reding 		return;
403ea5a9607SGrant Likely 
404ea5a9607SGrant Likely 	chained_irq_enter(chip, desc);
405ea5a9607SGrant Likely 
406ea5a9607SGrant Likely 	for (port = 0; port < 4; port++) {
407b546be0dSLaxman Dewangan 		gpio = tegra_gpio_compose(bank->bank, port, 0);
408b546be0dSLaxman Dewangan 		sta = tegra_gpio_readl(tgi, GPIO_INT_STA(tgi, gpio)) &
409b546be0dSLaxman Dewangan 			tegra_gpio_readl(tgi, GPIO_INT_ENB(tgi, gpio));
410b546be0dSLaxman Dewangan 		lvl = tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio));
411ea5a9607SGrant Likely 
412ea5a9607SGrant Likely 		for_each_set_bit(pin, &sta, 8) {
413dbd1c54fSMarc Zyngier 			int ret;
414dbd1c54fSMarc Zyngier 
415b546be0dSLaxman Dewangan 			tegra_gpio_writel(tgi, 1 << pin,
416b546be0dSLaxman Dewangan 					  GPIO_INT_CLR(tgi, gpio));
417ea5a9607SGrant Likely 
418ea5a9607SGrant Likely 			/* if gpio is edge triggered, clear condition
41920a8a968SColin Cronin 			 * before executing the handler so that we don't
420ea5a9607SGrant Likely 			 * miss edges
421ea5a9607SGrant Likely 			 */
4229e9509e3SMichał Mirosław 			if (!unmasked && lvl & (0x100 << pin)) {
4239e9509e3SMichał Mirosław 				unmasked = true;
424ea5a9607SGrant Likely 				chained_irq_exit(chip, desc);
425ea5a9607SGrant Likely 			}
426ea5a9607SGrant Likely 
427dbd1c54fSMarc Zyngier 			ret = generic_handle_domain_irq(domain, gpio + pin);
428dbd1c54fSMarc Zyngier 			WARN_RATELIMIT(ret, "hwirq = %d", gpio + pin);
429ea5a9607SGrant Likely 		}
430ea5a9607SGrant Likely 	}
431ea5a9607SGrant Likely 
432ea5a9607SGrant Likely 	if (!unmasked)
433ea5a9607SGrant Likely 		chained_irq_exit(chip, desc);
43466fecef5SThierry Reding }
435ea5a9607SGrant Likely 
tegra_gpio_child_to_parent_hwirq(struct gpio_chip * chip,unsigned int hwirq,unsigned int type,unsigned int * parent_hwirq,unsigned int * parent_type)436718ff946SDmitry Osipenko static int tegra_gpio_child_to_parent_hwirq(struct gpio_chip *chip,
437718ff946SDmitry Osipenko 					    unsigned int hwirq,
438718ff946SDmitry Osipenko 					    unsigned int type,
439718ff946SDmitry Osipenko 					    unsigned int *parent_hwirq,
44066fecef5SThierry Reding 					    unsigned int *parent_type)
44166fecef5SThierry Reding {
44266fecef5SThierry Reding 	*parent_hwirq = chip->irq.child_offset_to_irq(chip, hwirq);
44366fecef5SThierry Reding 	*parent_type = type;
44466fecef5SThierry Reding 
44566fecef5SThierry Reding 	return 0;
44666fecef5SThierry Reding }
44766fecef5SThierry Reding 
tegra_gpio_populate_parent_fwspec(struct gpio_chip * chip,union gpio_irq_fwspec * gfwspec,unsigned int parent_hwirq,unsigned int parent_type)44891a29af4SMarc Zyngier static int tegra_gpio_populate_parent_fwspec(struct gpio_chip *chip,
44991a29af4SMarc Zyngier 					     union gpio_irq_fwspec *gfwspec,
450718ff946SDmitry Osipenko 					     unsigned int parent_hwirq,
45166fecef5SThierry Reding 					     unsigned int parent_type)
45266fecef5SThierry Reding {
45391a29af4SMarc Zyngier 	struct irq_fwspec *fwspec = &gfwspec->fwspec;
45466fecef5SThierry Reding 
45566fecef5SThierry Reding 	fwspec->fwnode = chip->irq.parent_domain->fwnode;
45666fecef5SThierry Reding 	fwspec->param_count = 3;
45766fecef5SThierry Reding 	fwspec->param[0] = 0;
45866fecef5SThierry Reding 	fwspec->param[1] = parent_hwirq;
45966fecef5SThierry Reding 	fwspec->param[2] = parent_type;
46066fecef5SThierry Reding 
46191a29af4SMarc Zyngier 	return 0;
462ea5a9607SGrant Likely }
463ea5a9607SGrant Likely 
4648939ddc7SLaxman Dewangan #ifdef CONFIG_PM_SLEEP
tegra_gpio_resume(struct device * dev)4658939ddc7SLaxman Dewangan static int tegra_gpio_resume(struct device *dev)
466ea5a9607SGrant Likely {
4677ddb7dceSWolfram Sang 	struct tegra_gpio_info *tgi = dev_get_drvdata(dev);
468539b7a39SThierry Reding 	unsigned int b, p;
469ea5a9607SGrant Likely 
470b546be0dSLaxman Dewangan 	for (b = 0; b < tgi->bank_count; b++) {
471b546be0dSLaxman Dewangan 		struct tegra_gpio_bank *bank = &tgi->bank_info[b];
472ea5a9607SGrant Likely 
473ea5a9607SGrant Likely 		for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
474ea5a9607SGrant Likely 			unsigned int gpio = (b << 5) | (p << 3);
4754bc17860SThierry Reding 
476b546be0dSLaxman Dewangan 			tegra_gpio_writel(tgi, bank->cnf[p],
477b546be0dSLaxman Dewangan 					  GPIO_CNF(tgi, gpio));
4783737de42SLaxman Dewangan 
4793737de42SLaxman Dewangan 			if (tgi->soc->debounce_supported) {
4803737de42SLaxman Dewangan 				tegra_gpio_writel(tgi, bank->dbc_cnt[p],
4813737de42SLaxman Dewangan 						  GPIO_DBC_CNT(tgi, gpio));
4823737de42SLaxman Dewangan 				tegra_gpio_writel(tgi, bank->dbc_enb[p],
4833737de42SLaxman Dewangan 						  GPIO_MSK_DBC_EN(tgi, gpio));
4843737de42SLaxman Dewangan 			}
4853737de42SLaxman Dewangan 
486b546be0dSLaxman Dewangan 			tegra_gpio_writel(tgi, bank->out[p],
487b546be0dSLaxman Dewangan 					  GPIO_OUT(tgi, gpio));
488b546be0dSLaxman Dewangan 			tegra_gpio_writel(tgi, bank->oe[p],
489b546be0dSLaxman Dewangan 					  GPIO_OE(tgi, gpio));
490b546be0dSLaxman Dewangan 			tegra_gpio_writel(tgi, bank->int_lvl[p],
491b546be0dSLaxman Dewangan 					  GPIO_INT_LVL(tgi, gpio));
492b546be0dSLaxman Dewangan 			tegra_gpio_writel(tgi, bank->int_enb[p],
493b546be0dSLaxman Dewangan 					  GPIO_INT_ENB(tgi, gpio));
494ea5a9607SGrant Likely 		}
495ea5a9607SGrant Likely 	}
496ea5a9607SGrant Likely 
4978939ddc7SLaxman Dewangan 	return 0;
498ea5a9607SGrant Likely }
499ea5a9607SGrant Likely 
tegra_gpio_suspend(struct device * dev)5008939ddc7SLaxman Dewangan static int tegra_gpio_suspend(struct device *dev)
501ea5a9607SGrant Likely {
5027ddb7dceSWolfram Sang 	struct tegra_gpio_info *tgi = dev_get_drvdata(dev);
503539b7a39SThierry Reding 	unsigned int b, p;
504ea5a9607SGrant Likely 
505b546be0dSLaxman Dewangan 	for (b = 0; b < tgi->bank_count; b++) {
506b546be0dSLaxman Dewangan 		struct tegra_gpio_bank *bank = &tgi->bank_info[b];
507ea5a9607SGrant Likely 
508ea5a9607SGrant Likely 		for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
509ea5a9607SGrant Likely 			unsigned int gpio = (b << 5) | (p << 3);
5104bc17860SThierry Reding 
511b546be0dSLaxman Dewangan 			bank->cnf[p] = tegra_gpio_readl(tgi,
512b546be0dSLaxman Dewangan 							GPIO_CNF(tgi, gpio));
513b546be0dSLaxman Dewangan 			bank->out[p] = tegra_gpio_readl(tgi,
514b546be0dSLaxman Dewangan 							GPIO_OUT(tgi, gpio));
515b546be0dSLaxman Dewangan 			bank->oe[p] = tegra_gpio_readl(tgi,
516b546be0dSLaxman Dewangan 						       GPIO_OE(tgi, gpio));
5173737de42SLaxman Dewangan 			if (tgi->soc->debounce_supported) {
5183737de42SLaxman Dewangan 				bank->dbc_enb[p] = tegra_gpio_readl(tgi,
5193737de42SLaxman Dewangan 						GPIO_MSK_DBC_EN(tgi, gpio));
5203737de42SLaxman Dewangan 				bank->dbc_enb[p] = (bank->dbc_enb[p] << 8) |
5213737de42SLaxman Dewangan 							bank->dbc_enb[p];
5223737de42SLaxman Dewangan 			}
5233737de42SLaxman Dewangan 
524b546be0dSLaxman Dewangan 			bank->int_enb[p] = tegra_gpio_readl(tgi,
525b546be0dSLaxman Dewangan 						GPIO_INT_ENB(tgi, gpio));
526b546be0dSLaxman Dewangan 			bank->int_lvl[p] = tegra_gpio_readl(tgi,
527b546be0dSLaxman Dewangan 						GPIO_INT_LVL(tgi, gpio));
528203f31cbSJoseph Lo 
529203f31cbSJoseph Lo 			/* Enable gpio irq for wake up source */
530b546be0dSLaxman Dewangan 			tegra_gpio_writel(tgi, bank->wake_enb[p],
531b546be0dSLaxman Dewangan 					  GPIO_INT_ENB(tgi, gpio));
532ea5a9607SGrant Likely 		}
533ea5a9607SGrant Likely 	}
5349ccaf106SDmitry Osipenko 
5358939ddc7SLaxman Dewangan 	return 0;
536ea5a9607SGrant Likely }
537ea5a9607SGrant Likely 
tegra_gpio_irq_set_wake(struct irq_data * d,unsigned int enable)538203f31cbSJoseph Lo static int tegra_gpio_irq_set_wake(struct irq_data *d, unsigned int enable)
539ea5a9607SGrant Likely {
54066fecef5SThierry Reding 	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
54166fecef5SThierry Reding 	struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
54266fecef5SThierry Reding 	struct tegra_gpio_bank *bank;
543539b7a39SThierry Reding 	unsigned int gpio = d->hwirq;
544203f31cbSJoseph Lo 	u32 port, bit, mask;
54527f8feeaSDmitry Osipenko 	int err;
546f56d979cSDmitry Osipenko 
54766fecef5SThierry Reding 	bank = &tgi->bank_info[GPIO_BANK(d->hwirq)];
548203f31cbSJoseph Lo 
549203f31cbSJoseph Lo 	port = GPIO_PORT(gpio);
550203f31cbSJoseph Lo 	bit = GPIO_BIT(gpio);
551203f31cbSJoseph Lo 	mask = BIT(bit);
552203f31cbSJoseph Lo 
55327f8feeaSDmitry Osipenko 	err = irq_set_irq_wake(tgi->irqs[bank->bank], enable);
55427f8feeaSDmitry Osipenko 	if (err)
55527f8feeaSDmitry Osipenko 		return err;
55627f8feeaSDmitry Osipenko 
55727f8feeaSDmitry Osipenko 	if (d->parent_data) {
55827f8feeaSDmitry Osipenko 		err = irq_chip_set_wake_parent(d, enable);
55927f8feeaSDmitry Osipenko 		if (err) {
56027f8feeaSDmitry Osipenko 			irq_set_irq_wake(tgi->irqs[bank->bank], !enable);
56127f8feeaSDmitry Osipenko 			return err;
56227f8feeaSDmitry Osipenko 		}
56327f8feeaSDmitry Osipenko 	}
56427f8feeaSDmitry Osipenko 
565203f31cbSJoseph Lo 	if (enable)
566203f31cbSJoseph Lo 		bank->wake_enb[port] |= mask;
567203f31cbSJoseph Lo 	else
568203f31cbSJoseph Lo 		bank->wake_enb[port] &= ~mask;
569203f31cbSJoseph Lo 
570f56d979cSDmitry Osipenko 	return 0;
571ea5a9607SGrant Likely }
572ea5a9607SGrant Likely #endif
573ea5a9607SGrant Likely 
tegra_gpio_irq_set_affinity(struct irq_data * data,const struct cpumask * dest,bool force)574718ff946SDmitry Osipenko static int tegra_gpio_irq_set_affinity(struct irq_data *data,
575718ff946SDmitry Osipenko 				       const struct cpumask *dest,
57666fecef5SThierry Reding 				       bool force)
57766fecef5SThierry Reding {
57866fecef5SThierry Reding 	if (data->parent_data)
57966fecef5SThierry Reding 		return irq_chip_set_affinity_parent(data, dest, force);
58066fecef5SThierry Reding 
58166fecef5SThierry Reding 	return -EINVAL;
58266fecef5SThierry Reding }
58366fecef5SThierry Reding 
tegra_gpio_irq_request_resources(struct irq_data * d)58466fecef5SThierry Reding static int tegra_gpio_irq_request_resources(struct irq_data *d)
58566fecef5SThierry Reding {
58666fecef5SThierry Reding 	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
58766fecef5SThierry Reding 	struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
58866fecef5SThierry Reding 
58966fecef5SThierry Reding 	tegra_gpio_enable(tgi, d->hwirq);
59066fecef5SThierry Reding 
59166fecef5SThierry Reding 	return gpiochip_reqres_irq(chip, d->hwirq);
59266fecef5SThierry Reding }
59366fecef5SThierry Reding 
tegra_gpio_irq_release_resources(struct irq_data * d)59466fecef5SThierry Reding static void tegra_gpio_irq_release_resources(struct irq_data *d)
59566fecef5SThierry Reding {
59666fecef5SThierry Reding 	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
59766fecef5SThierry Reding 	struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
59866fecef5SThierry Reding 
59966fecef5SThierry Reding 	gpiochip_relres_irq(chip, d->hwirq);
60066fecef5SThierry Reding 	tegra_gpio_enable(tgi, d->hwirq);
60166fecef5SThierry Reding }
60266fecef5SThierry Reding 
tegra_gpio_irq_print_chip(struct irq_data * d,struct seq_file * s)6037d1aa08aSSvyatoslav Ryhel static void tegra_gpio_irq_print_chip(struct irq_data *d, struct seq_file *s)
6047d1aa08aSSvyatoslav Ryhel {
6057d1aa08aSSvyatoslav Ryhel 	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
6067d1aa08aSSvyatoslav Ryhel 
6077d1aa08aSSvyatoslav Ryhel 	seq_printf(s, dev_name(chip->parent));
6087d1aa08aSSvyatoslav Ryhel }
6097d1aa08aSSvyatoslav Ryhel 
6107d1aa08aSSvyatoslav Ryhel static const struct irq_chip tegra_gpio_irq_chip = {
6117d1aa08aSSvyatoslav Ryhel 	.irq_shutdown		= tegra_gpio_irq_shutdown,
6127d1aa08aSSvyatoslav Ryhel 	.irq_ack		= tegra_gpio_irq_ack,
6137d1aa08aSSvyatoslav Ryhel 	.irq_mask		= tegra_gpio_irq_mask,
6147d1aa08aSSvyatoslav Ryhel 	.irq_unmask		= tegra_gpio_irq_unmask,
6157d1aa08aSSvyatoslav Ryhel 	.irq_set_type		= tegra_gpio_irq_set_type,
6167d1aa08aSSvyatoslav Ryhel #ifdef CONFIG_PM_SLEEP
6177d1aa08aSSvyatoslav Ryhel 	.irq_set_wake		= tegra_gpio_irq_set_wake,
6187d1aa08aSSvyatoslav Ryhel #endif
6197d1aa08aSSvyatoslav Ryhel 	.irq_print_chip		= tegra_gpio_irq_print_chip,
6207d1aa08aSSvyatoslav Ryhel 	.irq_request_resources	= tegra_gpio_irq_request_resources,
6217d1aa08aSSvyatoslav Ryhel 	.irq_release_resources	= tegra_gpio_irq_release_resources,
6227d1aa08aSSvyatoslav Ryhel 	.flags			= IRQCHIP_IMMUTABLE,
6237d1aa08aSSvyatoslav Ryhel };
6247d1aa08aSSvyatoslav Ryhel 
6257d1aa08aSSvyatoslav Ryhel static const struct irq_chip tegra210_gpio_irq_chip = {
6267d1aa08aSSvyatoslav Ryhel 	.irq_shutdown		= tegra_gpio_irq_shutdown,
6277d1aa08aSSvyatoslav Ryhel 	.irq_ack		= tegra_gpio_irq_ack,
6287d1aa08aSSvyatoslav Ryhel 	.irq_mask		= tegra_gpio_irq_mask,
6297d1aa08aSSvyatoslav Ryhel 	.irq_unmask		= tegra_gpio_irq_unmask,
6307d1aa08aSSvyatoslav Ryhel 	.irq_set_affinity	= tegra_gpio_irq_set_affinity,
6317d1aa08aSSvyatoslav Ryhel 	.irq_set_type		= tegra_gpio_irq_set_type,
6327d1aa08aSSvyatoslav Ryhel #ifdef CONFIG_PM_SLEEP
6337d1aa08aSSvyatoslav Ryhel 	.irq_set_wake		= tegra_gpio_irq_set_wake,
6347d1aa08aSSvyatoslav Ryhel #endif
6357d1aa08aSSvyatoslav Ryhel 	.irq_print_chip		= tegra_gpio_irq_print_chip,
6367d1aa08aSSvyatoslav Ryhel 	.irq_request_resources	= tegra_gpio_irq_request_resources,
6377d1aa08aSSvyatoslav Ryhel 	.irq_release_resources	= tegra_gpio_irq_release_resources,
6387d1aa08aSSvyatoslav Ryhel 	.flags			= IRQCHIP_IMMUTABLE,
6397d1aa08aSSvyatoslav Ryhel };
6407d1aa08aSSvyatoslav Ryhel 
641b59d5fb7SSuzuki K. Poulose #ifdef	CONFIG_DEBUG_FS
642b59d5fb7SSuzuki K. Poulose 
643b59d5fb7SSuzuki K. Poulose #include <linux/debugfs.h>
644b59d5fb7SSuzuki K. Poulose 
tegra_dbg_gpio_show(struct seq_file * s,void * unused)6452773eb2fSAxel Lin static int tegra_dbg_gpio_show(struct seq_file *s, void *unused)
646b59d5fb7SSuzuki K. Poulose {
647b2a6115fSDmitry Osipenko 	struct tegra_gpio_info *tgi = dev_get_drvdata(s->private);
648539b7a39SThierry Reding 	unsigned int i, j;
649b59d5fb7SSuzuki K. Poulose 
650b546be0dSLaxman Dewangan 	for (i = 0; i < tgi->bank_count; i++) {
651b59d5fb7SSuzuki K. Poulose 		for (j = 0; j < 4; j++) {
652539b7a39SThierry Reding 			unsigned int gpio = tegra_gpio_compose(i, j, 0);
6534bc17860SThierry Reding 
654b59d5fb7SSuzuki K. Poulose 			seq_printf(s,
655539b7a39SThierry Reding 				"%u:%u %02x %02x %02x %02x %02x %02x %06x\n",
656b59d5fb7SSuzuki K. Poulose 				i, j,
657b546be0dSLaxman Dewangan 				tegra_gpio_readl(tgi, GPIO_CNF(tgi, gpio)),
658b546be0dSLaxman Dewangan 				tegra_gpio_readl(tgi, GPIO_OE(tgi, gpio)),
659b546be0dSLaxman Dewangan 				tegra_gpio_readl(tgi, GPIO_OUT(tgi, gpio)),
660b546be0dSLaxman Dewangan 				tegra_gpio_readl(tgi, GPIO_IN(tgi, gpio)),
661b546be0dSLaxman Dewangan 				tegra_gpio_readl(tgi, GPIO_INT_STA(tgi, gpio)),
662b546be0dSLaxman Dewangan 				tegra_gpio_readl(tgi, GPIO_INT_ENB(tgi, gpio)),
663b546be0dSLaxman Dewangan 				tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio)));
664b59d5fb7SSuzuki K. Poulose 		}
665b59d5fb7SSuzuki K. Poulose 	}
666b59d5fb7SSuzuki K. Poulose 	return 0;
667b59d5fb7SSuzuki K. Poulose }
668b59d5fb7SSuzuki K. Poulose 
tegra_gpio_debuginit(struct tegra_gpio_info * tgi)669b546be0dSLaxman Dewangan static void tegra_gpio_debuginit(struct tegra_gpio_info *tgi)
670b59d5fb7SSuzuki K. Poulose {
671b2a6115fSDmitry Osipenko 	debugfs_create_devm_seqfile(tgi->dev, "tegra_gpio", NULL,
672b2a6115fSDmitry Osipenko 				    tegra_dbg_gpio_show);
673b59d5fb7SSuzuki K. Poulose }
674b59d5fb7SSuzuki K. Poulose 
675b59d5fb7SSuzuki K. Poulose #else
676b59d5fb7SSuzuki K. Poulose 
tegra_gpio_debuginit(struct tegra_gpio_info * tgi)677b546be0dSLaxman Dewangan static inline void tegra_gpio_debuginit(struct tegra_gpio_info *tgi)
678b59d5fb7SSuzuki K. Poulose {
679b59d5fb7SSuzuki K. Poulose }
680b59d5fb7SSuzuki K. Poulose 
681b59d5fb7SSuzuki K. Poulose #endif
682b59d5fb7SSuzuki K. Poulose 
6838939ddc7SLaxman Dewangan static const struct dev_pm_ops tegra_gpio_pm_ops = {
6849ccaf106SDmitry Osipenko 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_gpio_suspend, tegra_gpio_resume)
6858939ddc7SLaxman Dewangan };
6868939ddc7SLaxman Dewangan 
68766fecef5SThierry Reding static const struct of_device_id tegra_pmc_of_match[] = {
68866fecef5SThierry Reding 	{ .compatible = "nvidia,tegra210-pmc", },
68966fecef5SThierry Reding 	{ /* sentinel */ },
69066fecef5SThierry Reding };
6916ea68fc0SDmitry Osipenko 
tegra_gpio_probe(struct platform_device * pdev)6923836309dSBill Pemberton static int tegra_gpio_probe(struct platform_device *pdev)
693ea5a9607SGrant Likely {
694ea5a9607SGrant Likely 	struct tegra_gpio_bank *bank;
69566fecef5SThierry Reding 	struct tegra_gpio_info *tgi;
69666fecef5SThierry Reding 	struct gpio_irq_chip *irq;
69766fecef5SThierry Reding 	struct device_node *np;
69866fecef5SThierry Reding 	unsigned int i, j;
699f57f98a6SStephen Warren 	int ret;
700ea5a9607SGrant Likely 
701b546be0dSLaxman Dewangan 	tgi = devm_kzalloc(&pdev->dev, sizeof(*tgi), GFP_KERNEL);
702b546be0dSLaxman Dewangan 	if (!tgi)
703b546be0dSLaxman Dewangan 		return -ENODEV;
704b546be0dSLaxman Dewangan 
70520133bd5SThierry Reding 	tgi->soc = of_device_get_match_data(&pdev->dev);
706b546be0dSLaxman Dewangan 	tgi->dev = &pdev->dev;
7075c1e2c9dSStephen Warren 
70856420903SThierry Reding 	ret = platform_irq_count(pdev);
70956420903SThierry Reding 	if (ret < 0)
71056420903SThierry Reding 		return ret;
71156420903SThierry Reding 
71256420903SThierry Reding 	tgi->bank_count = ret;
71356420903SThierry Reding 
714b546be0dSLaxman Dewangan 	if (!tgi->bank_count) {
7153391811cSStephen Warren 		dev_err(&pdev->dev, "Missing IRQ resource\n");
7163391811cSStephen Warren 		return -ENODEV;
7173391811cSStephen Warren 	}
7183391811cSStephen Warren 
719b546be0dSLaxman Dewangan 	tgi->gc.label			= "tegra-gpio";
720b546be0dSLaxman Dewangan 	tgi->gc.request			= tegra_gpio_request;
721b546be0dSLaxman Dewangan 	tgi->gc.free			= tegra_gpio_free;
722b546be0dSLaxman Dewangan 	tgi->gc.direction_input		= tegra_gpio_direction_input;
723b546be0dSLaxman Dewangan 	tgi->gc.get			= tegra_gpio_get;
724b546be0dSLaxman Dewangan 	tgi->gc.direction_output	= tegra_gpio_direction_output;
725b546be0dSLaxman Dewangan 	tgi->gc.set			= tegra_gpio_set;
726f002d07cSLaxman Dewangan 	tgi->gc.get_direction		= tegra_gpio_get_direction;
727b546be0dSLaxman Dewangan 	tgi->gc.base			= 0;
728b546be0dSLaxman Dewangan 	tgi->gc.ngpio			= tgi->bank_count * 32;
729b546be0dSLaxman Dewangan 	tgi->gc.parent			= &pdev->dev;
7303391811cSStephen Warren 
731b546be0dSLaxman Dewangan 	platform_set_drvdata(pdev, tgi);
732b546be0dSLaxman Dewangan 
73320133bd5SThierry Reding 	if (tgi->soc->debounce_supported)
7342956b5d9SMika Westerberg 		tgi->gc.set_config = tegra_gpio_set_config;
7353737de42SLaxman Dewangan 
7369b882269SThierry Reding 	tgi->bank_info = devm_kcalloc(&pdev->dev, tgi->bank_count,
737b546be0dSLaxman Dewangan 				      sizeof(*tgi->bank_info), GFP_KERNEL);
738b546be0dSLaxman Dewangan 	if (!tgi->bank_info)
7399b882269SThierry Reding 		return -ENOMEM;
7403391811cSStephen Warren 
741718ff946SDmitry Osipenko 	tgi->irqs = devm_kcalloc(&pdev->dev, tgi->bank_count,
742718ff946SDmitry Osipenko 				 sizeof(*tgi->irqs), GFP_KERNEL);
74366fecef5SThierry Reding 	if (!tgi->irqs)
74466fecef5SThierry Reding 		return -ENOMEM;
7456f74dc9bSStephen Warren 
746b546be0dSLaxman Dewangan 	for (i = 0; i < tgi->bank_count; i++) {
7479c07409cSThierry Reding 		ret = platform_get_irq(pdev, i);
74815bddb7dSStephen Boyd 		if (ret < 0)
7499c07409cSThierry Reding 			return ret;
75088d8951eSStephen Warren 
751b546be0dSLaxman Dewangan 		bank = &tgi->bank_info[i];
75288d8951eSStephen Warren 		bank->bank = i;
75366fecef5SThierry Reding 
75466fecef5SThierry Reding 		tgi->irqs[i] = ret;
75566fecef5SThierry Reding 
75666fecef5SThierry Reding 		for (j = 0; j < 4; j++) {
75766fecef5SThierry Reding 			raw_spin_lock_init(&bank->lvl_lock[j]);
75866fecef5SThierry Reding 			spin_lock_init(&bank->dbc_lock[j]);
75966fecef5SThierry Reding 		}
76066fecef5SThierry Reding 	}
76166fecef5SThierry Reding 
76266fecef5SThierry Reding 	irq = &tgi->gc.irq;
76366fecef5SThierry Reding 	irq->fwnode = of_node_to_fwnode(pdev->dev.of_node);
76466fecef5SThierry Reding 	irq->child_to_parent_hwirq = tegra_gpio_child_to_parent_hwirq;
76566fecef5SThierry Reding 	irq->populate_parent_alloc_arg = tegra_gpio_populate_parent_fwspec;
76666fecef5SThierry Reding 	irq->handler = handle_simple_irq;
76766fecef5SThierry Reding 	irq->default_type = IRQ_TYPE_NONE;
76866fecef5SThierry Reding 	irq->parent_handler = tegra_gpio_irq_handler;
76966fecef5SThierry Reding 	irq->parent_handler_data = tgi;
77066fecef5SThierry Reding 	irq->num_parents = tgi->bank_count;
77166fecef5SThierry Reding 	irq->parents = tgi->irqs;
77266fecef5SThierry Reding 
77366fecef5SThierry Reding 	np = of_find_matching_node(NULL, tegra_pmc_of_match);
77466fecef5SThierry Reding 	if (np) {
77566fecef5SThierry Reding 		irq->parent_domain = irq_find_host(np);
77666fecef5SThierry Reding 		of_node_put(np);
77766fecef5SThierry Reding 
77866fecef5SThierry Reding 		if (!irq->parent_domain)
77966fecef5SThierry Reding 			return -EPROBE_DEFER;
78094de03ccSDmitry Osipenko 
7817d1aa08aSSvyatoslav Ryhel 		gpio_irq_chip_set_chip(irq, &tegra210_gpio_irq_chip);
7827d1aa08aSSvyatoslav Ryhel 	} else {
7837d1aa08aSSvyatoslav Ryhel 		gpio_irq_chip_set_chip(irq, &tegra_gpio_irq_chip);
78488d8951eSStephen Warren 	}
78588d8951eSStephen Warren 
786a0b81f1cSEnrico Weigelt, metux IT consult 	tgi->regs = devm_platform_ioremap_resource(pdev, 0);
787b546be0dSLaxman Dewangan 	if (IS_ERR(tgi->regs))
788b546be0dSLaxman Dewangan 		return PTR_ERR(tgi->regs);
78988d8951eSStephen Warren 
790b546be0dSLaxman Dewangan 	for (i = 0; i < tgi->bank_count; i++) {
791ea5a9607SGrant Likely 		for (j = 0; j < 4; j++) {
792ea5a9607SGrant Likely 			int gpio = tegra_gpio_compose(i, j, 0);
7934bc17860SThierry Reding 
794b546be0dSLaxman Dewangan 			tegra_gpio_writel(tgi, 0x00, GPIO_INT_ENB(tgi, gpio));
795ea5a9607SGrant Likely 		}
796ea5a9607SGrant Likely 	}
797ea5a9607SGrant Likely 
798b546be0dSLaxman Dewangan 	ret = devm_gpiochip_add_data(&pdev->dev, &tgi->gc, tgi);
79966fecef5SThierry Reding 	if (ret < 0)
800f57f98a6SStephen Warren 		return ret;
801ea5a9607SGrant Likely 
802b546be0dSLaxman Dewangan 	tegra_gpio_debuginit(tgi);
803b59d5fb7SSuzuki K. Poulose 
804ea5a9607SGrant Likely 	return 0;
805ea5a9607SGrant Likely }
806ea5a9607SGrant Likely 
807804f5680SLaxman Dewangan static const struct tegra_gpio_soc_config tegra20_gpio_config = {
808171b92c8SLaxman Dewangan 	.bank_stride = 0x80,
809171b92c8SLaxman Dewangan 	.upper_offset = 0x800,
810171b92c8SLaxman Dewangan };
811171b92c8SLaxman Dewangan 
812804f5680SLaxman Dewangan static const struct tegra_gpio_soc_config tegra30_gpio_config = {
813171b92c8SLaxman Dewangan 	.bank_stride = 0x100,
814171b92c8SLaxman Dewangan 	.upper_offset = 0x80,
815171b92c8SLaxman Dewangan };
816171b92c8SLaxman Dewangan 
8173737de42SLaxman Dewangan static const struct tegra_gpio_soc_config tegra210_gpio_config = {
8183737de42SLaxman Dewangan 	.debounce_supported = true,
8193737de42SLaxman Dewangan 	.bank_stride = 0x100,
8203737de42SLaxman Dewangan 	.upper_offset = 0x80,
8213737de42SLaxman Dewangan };
8223737de42SLaxman Dewangan 
823171b92c8SLaxman Dewangan static const struct of_device_id tegra_gpio_of_match[] = {
8243737de42SLaxman Dewangan 	{ .compatible = "nvidia,tegra210-gpio", .data = &tegra210_gpio_config },
825171b92c8SLaxman Dewangan 	{ .compatible = "nvidia,tegra30-gpio", .data = &tegra30_gpio_config },
826171b92c8SLaxman Dewangan 	{ .compatible = "nvidia,tegra20-gpio", .data = &tegra20_gpio_config },
827171b92c8SLaxman Dewangan 	{ },
828171b92c8SLaxman Dewangan };
8294a6eac2bSDmitry Osipenko MODULE_DEVICE_TABLE(of, tegra_gpio_of_match);
830171b92c8SLaxman Dewangan 
83188d8951eSStephen Warren static struct platform_driver tegra_gpio_driver = {
83288d8951eSStephen Warren 	.driver = {
83388d8951eSStephen Warren 		.name = "tegra-gpio",
8348939ddc7SLaxman Dewangan 		.pm = &tegra_gpio_pm_ops,
83588d8951eSStephen Warren 		.of_match_table = tegra_gpio_of_match,
83688d8951eSStephen Warren 	},
83788d8951eSStephen Warren 	.probe = tegra_gpio_probe,
83888d8951eSStephen Warren };
8394a6eac2bSDmitry Osipenko module_platform_driver(tegra_gpio_driver);
84088d8951eSStephen Warren 
8414a6eac2bSDmitry Osipenko MODULE_DESCRIPTION("NVIDIA Tegra GPIO controller driver");
8424a6eac2bSDmitry Osipenko MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
8434a6eac2bSDmitry Osipenko MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
8444a6eac2bSDmitry Osipenko MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
8454a6eac2bSDmitry Osipenko MODULE_AUTHOR("Erik Gilling <konkers@google.com>");
8464a6eac2bSDmitry Osipenko MODULE_LICENSE("GPL v2");
847