blob: 653825db4baa6b35660c98f12a6f8026a0cb9d0e [file] [log] [blame]
Erik Gilling3c92db92010-03-15 19:40:06 -07001/*
2 * arch/arm/mach-tegra/gpio.c
3 *
4 * Copyright (c) 2010 Google, Inc
5 *
6 * Author:
7 * Erik Gilling <konkers@google.com>
8 *
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 */
19
Thierry Reding641d0342013-01-21 11:09:01 +010020#include <linux/err.h>
Erik Gilling3c92db92010-03-15 19:40:06 -070021#include <linux/init.h>
22#include <linux/irq.h>
Colin Cross2e47b8b2010-04-07 12:59:42 -070023#include <linux/interrupt.h>
Erik Gilling3c92db92010-03-15 19:40:06 -070024#include <linux/io.h>
25#include <linux/gpio.h>
Stephen Warren5c1e2c92012-03-16 17:35:08 -060026#include <linux/of_device.h>
Stephen Warren88d89512011-10-11 16:16:14 -060027#include <linux/platform_device.h>
28#include <linux/module.h>
Stephen Warren6f74dc92012-01-04 08:39:37 +000029#include <linux/irqdomain.h>
Catalin Marinasde88cbb2013-01-18 15:31:37 +000030#include <linux/irqchip/chained_irq.h>
Stephen Warren3e215d02012-02-18 01:04:55 -070031#include <linux/pinctrl/consumer.h>
Laxman Dewangan8939ddc2012-11-07 20:31:32 +053032#include <linux/pm.h>
Erik Gilling3c92db92010-03-15 19:40:06 -070033
Erik Gilling3c92db92010-03-15 19:40:06 -070034#define GPIO_BANK(x) ((x) >> 5)
35#define GPIO_PORT(x) (((x) >> 3) & 0x3)
36#define GPIO_BIT(x) ((x) & 0x7)
37
Laxman Dewanganb546be02016-04-25 16:08:33 +053038#define GPIO_REG(tgi, x) (GPIO_BANK(x) * tgi->soc->bank_stride + \
Stephen Warren5c1e2c92012-03-16 17:35:08 -060039 GPIO_PORT(x) * 4)
Erik Gilling3c92db92010-03-15 19:40:06 -070040
Laxman Dewanganb546be02016-04-25 16:08:33 +053041#define GPIO_CNF(t, x) (GPIO_REG(t, x) + 0x00)
42#define GPIO_OE(t, x) (GPIO_REG(t, x) + 0x10)
43#define GPIO_OUT(t, x) (GPIO_REG(t, x) + 0X20)
44#define GPIO_IN(t, x) (GPIO_REG(t, x) + 0x30)
45#define GPIO_INT_STA(t, x) (GPIO_REG(t, x) + 0x40)
46#define GPIO_INT_ENB(t, x) (GPIO_REG(t, x) + 0x50)
47#define GPIO_INT_LVL(t, x) (GPIO_REG(t, x) + 0x60)
48#define GPIO_INT_CLR(t, x) (GPIO_REG(t, x) + 0x70)
Erik Gilling3c92db92010-03-15 19:40:06 -070049
Laxman Dewanganb546be02016-04-25 16:08:33 +053050#define GPIO_MSK_CNF(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x00)
51#define GPIO_MSK_OE(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x10)
52#define GPIO_MSK_OUT(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0X20)
53#define GPIO_MSK_INT_STA(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x40)
54#define GPIO_MSK_INT_ENB(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x50)
55#define GPIO_MSK_INT_LVL(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x60)
Erik Gilling3c92db92010-03-15 19:40:06 -070056
57#define GPIO_INT_LVL_MASK 0x010101
58#define GPIO_INT_LVL_EDGE_RISING 0x000101
59#define GPIO_INT_LVL_EDGE_FALLING 0x000100
60#define GPIO_INT_LVL_EDGE_BOTH 0x010100
61#define GPIO_INT_LVL_LEVEL_HIGH 0x000001
62#define GPIO_INT_LVL_LEVEL_LOW 0x000000
63
Laxman Dewanganb546be02016-04-25 16:08:33 +053064struct tegra_gpio_info;
65
Erik Gilling3c92db92010-03-15 19:40:06 -070066struct tegra_gpio_bank {
67 int bank;
68 int irq;
69 spinlock_t lvl_lock[4];
Laxman Dewangan8939ddc2012-11-07 20:31:32 +053070#ifdef CONFIG_PM_SLEEP
Colin Cross2e47b8b2010-04-07 12:59:42 -070071 u32 cnf[4];
72 u32 out[4];
73 u32 oe[4];
74 u32 int_enb[4];
75 u32 int_lvl[4];
Joseph Lo203f31c2013-04-03 19:31:44 +080076 u32 wake_enb[4];
Colin Cross2e47b8b2010-04-07 12:59:42 -070077#endif
Laxman Dewanganb546be02016-04-25 16:08:33 +053078 struct tegra_gpio_info *tgi;
Erik Gilling3c92db92010-03-15 19:40:06 -070079};
80
Laxman Dewangan171b92c2016-04-25 16:08:31 +053081struct tegra_gpio_soc_config {
82 u32 bank_stride;
83 u32 upper_offset;
84};
85
Laxman Dewanganb546be02016-04-25 16:08:33 +053086struct tegra_gpio_info {
87 struct device *dev;
88 void __iomem *regs;
89 struct irq_domain *irq_domain;
90 struct tegra_gpio_bank *bank_info;
91 const struct tegra_gpio_soc_config *soc;
92 struct gpio_chip gc;
93 struct irq_chip ic;
94 struct lock_class_key lock_class;
95 u32 bank_count;
96};
Stephen Warren88d89512011-10-11 16:16:14 -060097
Laxman Dewanganb546be02016-04-25 16:08:33 +053098static inline void tegra_gpio_writel(struct tegra_gpio_info *tgi,
99 u32 val, u32 reg)
Stephen Warren88d89512011-10-11 16:16:14 -0600100{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530101 __raw_writel(val, tgi->regs + reg);
Stephen Warren88d89512011-10-11 16:16:14 -0600102}
103
Laxman Dewanganb546be02016-04-25 16:08:33 +0530104static inline u32 tegra_gpio_readl(struct tegra_gpio_info *tgi, u32 reg)
Stephen Warren88d89512011-10-11 16:16:14 -0600105{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530106 return __raw_readl(tgi->regs + reg);
Stephen Warren88d89512011-10-11 16:16:14 -0600107}
Erik Gilling3c92db92010-03-15 19:40:06 -0700108
109static int tegra_gpio_compose(int bank, int port, int bit)
110{
111 return (bank << 5) | ((port & 0x3) << 3) | (bit & 0x7);
112}
113
Laxman Dewanganb546be02016-04-25 16:08:33 +0530114static void tegra_gpio_mask_write(struct tegra_gpio_info *tgi, u32 reg,
115 int gpio, int value)
Erik Gilling3c92db92010-03-15 19:40:06 -0700116{
117 u32 val;
118
119 val = 0x100 << GPIO_BIT(gpio);
120 if (value)
121 val |= 1 << GPIO_BIT(gpio);
Laxman Dewanganb546be02016-04-25 16:08:33 +0530122 tegra_gpio_writel(tgi, val, reg);
Erik Gilling3c92db92010-03-15 19:40:06 -0700123}
124
Laxman Dewanganb546be02016-04-25 16:08:33 +0530125static void tegra_gpio_enable(struct tegra_gpio_info *tgi, int gpio)
Erik Gilling3c92db92010-03-15 19:40:06 -0700126{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530127 tegra_gpio_mask_write(tgi, GPIO_MSK_CNF(tgi, gpio), gpio, 1);
Erik Gilling3c92db92010-03-15 19:40:06 -0700128}
129
Laxman Dewanganb546be02016-04-25 16:08:33 +0530130static void tegra_gpio_disable(struct tegra_gpio_info *tgi, int gpio)
Erik Gilling3c92db92010-03-15 19:40:06 -0700131{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530132 tegra_gpio_mask_write(tgi, GPIO_MSK_CNF(tgi, gpio), gpio, 0);
Erik Gilling3c92db92010-03-15 19:40:06 -0700133}
134
Axel Lin924a0982012-11-08 10:45:24 +0800135static int tegra_gpio_request(struct gpio_chip *chip, unsigned offset)
Stephen Warren3e215d02012-02-18 01:04:55 -0700136{
137 return pinctrl_request_gpio(offset);
138}
139
Axel Lin924a0982012-11-08 10:45:24 +0800140static void tegra_gpio_free(struct gpio_chip *chip, unsigned offset)
Stephen Warren3e215d02012-02-18 01:04:55 -0700141{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530142 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
143
Stephen Warren3e215d02012-02-18 01:04:55 -0700144 pinctrl_free_gpio(offset);
Laxman Dewanganb546be02016-04-25 16:08:33 +0530145 tegra_gpio_disable(tgi, offset);
Stephen Warren3e215d02012-02-18 01:04:55 -0700146}
147
Erik Gilling3c92db92010-03-15 19:40:06 -0700148static void tegra_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
149{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530150 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
151
152 tegra_gpio_mask_write(tgi, GPIO_MSK_OUT(tgi, offset), offset, value);
Erik Gilling3c92db92010-03-15 19:40:06 -0700153}
154
155static int tegra_gpio_get(struct gpio_chip *chip, unsigned offset)
156{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530157 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
158 int bval = BIT(GPIO_BIT(offset));
Laxman Dewangan195812e2012-11-09 11:34:20 +0530159
Laxman Dewanganb546be02016-04-25 16:08:33 +0530160 /* If gpio is in output mode then read from the out value */
161 if (tegra_gpio_readl(tgi, GPIO_OE(tgi, offset)) & bval)
162 return !!(tegra_gpio_readl(tgi, GPIO_OUT(tgi, offset)) & bval);
163
164 return !!(tegra_gpio_readl(tgi, GPIO_IN(tgi, offset)) & bval);
Erik Gilling3c92db92010-03-15 19:40:06 -0700165}
166
167static int tegra_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
168{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530169 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
170
171 tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, offset), offset, 0);
172 tegra_gpio_enable(tgi, offset);
Erik Gilling3c92db92010-03-15 19:40:06 -0700173 return 0;
174}
175
176static int tegra_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
177 int value)
178{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530179 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
180
Erik Gilling3c92db92010-03-15 19:40:06 -0700181 tegra_gpio_set(chip, offset, value);
Laxman Dewanganb546be02016-04-25 16:08:33 +0530182 tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, offset), offset, 1);
183 tegra_gpio_enable(tgi, offset);
Erik Gilling3c92db92010-03-15 19:40:06 -0700184 return 0;
185}
186
Stephen Warren438a99c2011-08-23 00:39:56 +0100187static int tegra_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
188{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530189 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
Erik Gilling3c92db92010-03-15 19:40:06 -0700190
Laxman Dewanganb546be02016-04-25 16:08:33 +0530191 return irq_find_mapping(tgi->irq_domain, offset);
192}
Erik Gilling3c92db92010-03-15 19:40:06 -0700193
Lennert Buytenhek37337a82010-11-29 11:14:46 +0100194static void tegra_gpio_irq_ack(struct irq_data *d)
Erik Gilling3c92db92010-03-15 19:40:06 -0700195{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530196 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
197 struct tegra_gpio_info *tgi = bank->tgi;
Stephen Warren6f74dc92012-01-04 08:39:37 +0000198 int gpio = d->hwirq;
Erik Gilling3c92db92010-03-15 19:40:06 -0700199
Laxman Dewanganb546be02016-04-25 16:08:33 +0530200 tegra_gpio_writel(tgi, 1 << GPIO_BIT(gpio), GPIO_INT_CLR(tgi, gpio));
Erik Gilling3c92db92010-03-15 19:40:06 -0700201}
202
Lennert Buytenhek37337a82010-11-29 11:14:46 +0100203static void tegra_gpio_irq_mask(struct irq_data *d)
Erik Gilling3c92db92010-03-15 19:40:06 -0700204{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530205 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
206 struct tegra_gpio_info *tgi = bank->tgi;
Stephen Warren6f74dc92012-01-04 08:39:37 +0000207 int gpio = d->hwirq;
Erik Gilling3c92db92010-03-15 19:40:06 -0700208
Laxman Dewanganb546be02016-04-25 16:08:33 +0530209 tegra_gpio_mask_write(tgi, GPIO_MSK_INT_ENB(tgi, gpio), gpio, 0);
Erik Gilling3c92db92010-03-15 19:40:06 -0700210}
211
Lennert Buytenhek37337a82010-11-29 11:14:46 +0100212static void tegra_gpio_irq_unmask(struct irq_data *d)
Erik Gilling3c92db92010-03-15 19:40:06 -0700213{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530214 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
215 struct tegra_gpio_info *tgi = bank->tgi;
Stephen Warren6f74dc92012-01-04 08:39:37 +0000216 int gpio = d->hwirq;
Erik Gilling3c92db92010-03-15 19:40:06 -0700217
Laxman Dewanganb546be02016-04-25 16:08:33 +0530218 tegra_gpio_mask_write(tgi, GPIO_MSK_INT_ENB(tgi, gpio), gpio, 1);
Erik Gilling3c92db92010-03-15 19:40:06 -0700219}
220
Lennert Buytenhek37337a82010-11-29 11:14:46 +0100221static int tegra_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Erik Gilling3c92db92010-03-15 19:40:06 -0700222{
Stephen Warren6f74dc92012-01-04 08:39:37 +0000223 int gpio = d->hwirq;
Lennert Buytenhek37337a82010-11-29 11:14:46 +0100224 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
Laxman Dewanganb546be02016-04-25 16:08:33 +0530225 struct tegra_gpio_info *tgi = bank->tgi;
Erik Gilling3c92db92010-03-15 19:40:06 -0700226 int port = GPIO_PORT(gpio);
227 int lvl_type;
228 int val;
229 unsigned long flags;
Stephen Warrendf231f22013-10-16 13:25:33 -0600230 int ret;
Erik Gilling3c92db92010-03-15 19:40:06 -0700231
232 switch (type & IRQ_TYPE_SENSE_MASK) {
233 case IRQ_TYPE_EDGE_RISING:
234 lvl_type = GPIO_INT_LVL_EDGE_RISING;
235 break;
236
237 case IRQ_TYPE_EDGE_FALLING:
238 lvl_type = GPIO_INT_LVL_EDGE_FALLING;
239 break;
240
241 case IRQ_TYPE_EDGE_BOTH:
242 lvl_type = GPIO_INT_LVL_EDGE_BOTH;
243 break;
244
245 case IRQ_TYPE_LEVEL_HIGH:
246 lvl_type = GPIO_INT_LVL_LEVEL_HIGH;
247 break;
248
249 case IRQ_TYPE_LEVEL_LOW:
250 lvl_type = GPIO_INT_LVL_LEVEL_LOW;
251 break;
252
253 default:
254 return -EINVAL;
255 }
256
Laxman Dewanganb546be02016-04-25 16:08:33 +0530257 ret = gpiochip_lock_as_irq(&tgi->gc, gpio);
Stephen Warrendf231f22013-10-16 13:25:33 -0600258 if (ret) {
Laxman Dewanganb546be02016-04-25 16:08:33 +0530259 dev_err(tgi->dev,
260 "unable to lock Tegra GPIO %d as IRQ\n", gpio);
Stephen Warrendf231f22013-10-16 13:25:33 -0600261 return ret;
262 }
263
Erik Gilling3c92db92010-03-15 19:40:06 -0700264 spin_lock_irqsave(&bank->lvl_lock[port], flags);
265
Laxman Dewanganb546be02016-04-25 16:08:33 +0530266 val = tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio));
Erik Gilling3c92db92010-03-15 19:40:06 -0700267 val &= ~(GPIO_INT_LVL_MASK << GPIO_BIT(gpio));
268 val |= lvl_type << GPIO_BIT(gpio);
Laxman Dewanganb546be02016-04-25 16:08:33 +0530269 tegra_gpio_writel(tgi, val, GPIO_INT_LVL(tgi, gpio));
Erik Gilling3c92db92010-03-15 19:40:06 -0700270
271 spin_unlock_irqrestore(&bank->lvl_lock[port], flags);
272
Laxman Dewanganb546be02016-04-25 16:08:33 +0530273 tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, gpio), gpio, 0);
274 tegra_gpio_enable(tgi, gpio);
Stephen Warrend9411362012-03-19 10:31:58 -0600275
Erik Gilling3c92db92010-03-15 19:40:06 -0700276 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixnerf170d712015-06-23 15:52:40 +0200277 irq_set_handler_locked(d, handle_level_irq);
Erik Gilling3c92db92010-03-15 19:40:06 -0700278 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixnerf170d712015-06-23 15:52:40 +0200279 irq_set_handler_locked(d, handle_edge_irq);
Erik Gilling3c92db92010-03-15 19:40:06 -0700280
281 return 0;
282}
283
Stephen Warrendf231f22013-10-16 13:25:33 -0600284static void tegra_gpio_irq_shutdown(struct irq_data *d)
285{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530286 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
287 struct tegra_gpio_info *tgi = bank->tgi;
Stephen Warrendf231f22013-10-16 13:25:33 -0600288 int gpio = d->hwirq;
289
Laxman Dewanganb546be02016-04-25 16:08:33 +0530290 gpiochip_unlock_as_irq(&tgi->gc, gpio);
Stephen Warrendf231f22013-10-16 13:25:33 -0600291}
292
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200293static void tegra_gpio_irq_handler(struct irq_desc *desc)
Erik Gilling3c92db92010-03-15 19:40:06 -0700294{
Erik Gilling3c92db92010-03-15 19:40:06 -0700295 int port;
296 int pin;
297 int unmasked = 0;
Laxman Dewanganb546be02016-04-25 16:08:33 +0530298 int gpio;
299 u32 lvl;
300 unsigned long sta;
Will Deacon98022942011-02-21 13:58:10 +0000301 struct irq_chip *chip = irq_desc_get_chip(desc);
Jiang Liu476f8b42015-06-04 12:13:15 +0800302 struct tegra_gpio_bank *bank = irq_desc_get_handler_data(desc);
Laxman Dewanganb546be02016-04-25 16:08:33 +0530303 struct tegra_gpio_info *tgi = bank->tgi;
Erik Gilling3c92db92010-03-15 19:40:06 -0700304
Will Deacon98022942011-02-21 13:58:10 +0000305 chained_irq_enter(chip, desc);
Erik Gilling3c92db92010-03-15 19:40:06 -0700306
Erik Gilling3c92db92010-03-15 19:40:06 -0700307 for (port = 0; port < 4; port++) {
Laxman Dewanganb546be02016-04-25 16:08:33 +0530308 gpio = tegra_gpio_compose(bank->bank, port, 0);
309 sta = tegra_gpio_readl(tgi, GPIO_INT_STA(tgi, gpio)) &
310 tegra_gpio_readl(tgi, GPIO_INT_ENB(tgi, gpio));
311 lvl = tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio));
Erik Gilling3c92db92010-03-15 19:40:06 -0700312
313 for_each_set_bit(pin, &sta, 8) {
Laxman Dewanganb546be02016-04-25 16:08:33 +0530314 tegra_gpio_writel(tgi, 1 << pin,
315 GPIO_INT_CLR(tgi, gpio));
Erik Gilling3c92db92010-03-15 19:40:06 -0700316
317 /* if gpio is edge triggered, clear condition
Colin Cronin20a8a962015-05-18 11:41:43 -0700318 * before executing the handler so that we don't
Erik Gilling3c92db92010-03-15 19:40:06 -0700319 * miss edges
320 */
321 if (lvl & (0x100 << pin)) {
322 unmasked = 1;
Will Deacon98022942011-02-21 13:58:10 +0000323 chained_irq_exit(chip, desc);
Erik Gilling3c92db92010-03-15 19:40:06 -0700324 }
325
326 generic_handle_irq(gpio_to_irq(gpio + pin));
327 }
328 }
329
330 if (!unmasked)
Will Deacon98022942011-02-21 13:58:10 +0000331 chained_irq_exit(chip, desc);
Erik Gilling3c92db92010-03-15 19:40:06 -0700332
333}
334
Laxman Dewangan8939ddc2012-11-07 20:31:32 +0530335#ifdef CONFIG_PM_SLEEP
336static int tegra_gpio_resume(struct device *dev)
Colin Cross2e47b8b2010-04-07 12:59:42 -0700337{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530338 struct platform_device *pdev = to_platform_device(dev);
339 struct tegra_gpio_info *tgi = platform_get_drvdata(pdev);
Colin Cross2e47b8b2010-04-07 12:59:42 -0700340 unsigned long flags;
Colin Crossc8309ef2011-03-30 00:24:43 -0700341 int b;
342 int p;
Colin Cross2e47b8b2010-04-07 12:59:42 -0700343
344 local_irq_save(flags);
345
Laxman Dewanganb546be02016-04-25 16:08:33 +0530346 for (b = 0; b < tgi->bank_count; b++) {
347 struct tegra_gpio_bank *bank = &tgi->bank_info[b];
Colin Cross2e47b8b2010-04-07 12:59:42 -0700348
349 for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
350 unsigned int gpio = (b<<5) | (p<<3);
Laxman Dewanganb546be02016-04-25 16:08:33 +0530351 tegra_gpio_writel(tgi, bank->cnf[p],
352 GPIO_CNF(tgi, gpio));
353 tegra_gpio_writel(tgi, bank->out[p],
354 GPIO_OUT(tgi, gpio));
355 tegra_gpio_writel(tgi, bank->oe[p],
356 GPIO_OE(tgi, gpio));
357 tegra_gpio_writel(tgi, bank->int_lvl[p],
358 GPIO_INT_LVL(tgi, gpio));
359 tegra_gpio_writel(tgi, bank->int_enb[p],
360 GPIO_INT_ENB(tgi, gpio));
Colin Cross2e47b8b2010-04-07 12:59:42 -0700361 }
362 }
363
364 local_irq_restore(flags);
Laxman Dewangan8939ddc2012-11-07 20:31:32 +0530365 return 0;
Colin Cross2e47b8b2010-04-07 12:59:42 -0700366}
367
Laxman Dewangan8939ddc2012-11-07 20:31:32 +0530368static int tegra_gpio_suspend(struct device *dev)
Colin Cross2e47b8b2010-04-07 12:59:42 -0700369{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530370 struct platform_device *pdev = to_platform_device(dev);
371 struct tegra_gpio_info *tgi = platform_get_drvdata(pdev);
Colin Cross2e47b8b2010-04-07 12:59:42 -0700372 unsigned long flags;
Colin Crossc8309ef2011-03-30 00:24:43 -0700373 int b;
374 int p;
Colin Cross2e47b8b2010-04-07 12:59:42 -0700375
Colin Cross2e47b8b2010-04-07 12:59:42 -0700376 local_irq_save(flags);
Laxman Dewanganb546be02016-04-25 16:08:33 +0530377 for (b = 0; b < tgi->bank_count; b++) {
378 struct tegra_gpio_bank *bank = &tgi->bank_info[b];
Colin Cross2e47b8b2010-04-07 12:59:42 -0700379
380 for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
381 unsigned int gpio = (b<<5) | (p<<3);
Laxman Dewanganb546be02016-04-25 16:08:33 +0530382 bank->cnf[p] = tegra_gpio_readl(tgi,
383 GPIO_CNF(tgi, gpio));
384 bank->out[p] = tegra_gpio_readl(tgi,
385 GPIO_OUT(tgi, gpio));
386 bank->oe[p] = tegra_gpio_readl(tgi,
387 GPIO_OE(tgi, gpio));
388 bank->int_enb[p] = tegra_gpio_readl(tgi,
389 GPIO_INT_ENB(tgi, gpio));
390 bank->int_lvl[p] = tegra_gpio_readl(tgi,
391 GPIO_INT_LVL(tgi, gpio));
Joseph Lo203f31c2013-04-03 19:31:44 +0800392
393 /* Enable gpio irq for wake up source */
Laxman Dewanganb546be02016-04-25 16:08:33 +0530394 tegra_gpio_writel(tgi, bank->wake_enb[p],
395 GPIO_INT_ENB(tgi, gpio));
Colin Cross2e47b8b2010-04-07 12:59:42 -0700396 }
397 }
398 local_irq_restore(flags);
Laxman Dewangan8939ddc2012-11-07 20:31:32 +0530399 return 0;
Colin Cross2e47b8b2010-04-07 12:59:42 -0700400}
401
Joseph Lo203f31c2013-04-03 19:31:44 +0800402static int tegra_gpio_irq_set_wake(struct irq_data *d, unsigned int enable)
Colin Cross2e47b8b2010-04-07 12:59:42 -0700403{
Lennert Buytenhek37337a82010-11-29 11:14:46 +0100404 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
Joseph Lo203f31c2013-04-03 19:31:44 +0800405 int gpio = d->hwirq;
406 u32 port, bit, mask;
407
408 port = GPIO_PORT(gpio);
409 bit = GPIO_BIT(gpio);
410 mask = BIT(bit);
411
412 if (enable)
413 bank->wake_enb[port] |= mask;
414 else
415 bank->wake_enb[port] &= ~mask;
416
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100417 return irq_set_irq_wake(bank->irq, enable);
Colin Cross2e47b8b2010-04-07 12:59:42 -0700418}
419#endif
Erik Gilling3c92db92010-03-15 19:40:06 -0700420
Suzuki K. Pouloseb59d5fb2015-11-16 16:07:10 +0000421#ifdef CONFIG_DEBUG_FS
422
423#include <linux/debugfs.h>
424#include <linux/seq_file.h>
425
426static int dbg_gpio_show(struct seq_file *s, void *unused)
427{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530428 struct tegra_gpio_info *tgi = s->private;
Suzuki K. Pouloseb59d5fb2015-11-16 16:07:10 +0000429 int i;
430 int j;
431
Laxman Dewanganb546be02016-04-25 16:08:33 +0530432 for (i = 0; i < tgi->bank_count; i++) {
Suzuki K. Pouloseb59d5fb2015-11-16 16:07:10 +0000433 for (j = 0; j < 4; j++) {
434 int gpio = tegra_gpio_compose(i, j, 0);
435 seq_printf(s,
436 "%d:%d %02x %02x %02x %02x %02x %02x %06x\n",
437 i, j,
Laxman Dewanganb546be02016-04-25 16:08:33 +0530438 tegra_gpio_readl(tgi, GPIO_CNF(tgi, gpio)),
439 tegra_gpio_readl(tgi, GPIO_OE(tgi, gpio)),
440 tegra_gpio_readl(tgi, GPIO_OUT(tgi, gpio)),
441 tegra_gpio_readl(tgi, GPIO_IN(tgi, gpio)),
442 tegra_gpio_readl(tgi, GPIO_INT_STA(tgi, gpio)),
443 tegra_gpio_readl(tgi, GPIO_INT_ENB(tgi, gpio)),
444 tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio)));
Suzuki K. Pouloseb59d5fb2015-11-16 16:07:10 +0000445 }
446 }
447 return 0;
448}
449
450static int dbg_gpio_open(struct inode *inode, struct file *file)
451{
Laxman Dewanganb546be02016-04-25 16:08:33 +0530452 return single_open(file, dbg_gpio_show, inode->i_private);
Suzuki K. Pouloseb59d5fb2015-11-16 16:07:10 +0000453}
454
455static const struct file_operations debug_fops = {
456 .open = dbg_gpio_open,
457 .read = seq_read,
458 .llseek = seq_lseek,
459 .release = single_release,
460};
461
Laxman Dewanganb546be02016-04-25 16:08:33 +0530462static void tegra_gpio_debuginit(struct tegra_gpio_info *tgi)
Suzuki K. Pouloseb59d5fb2015-11-16 16:07:10 +0000463{
464 (void) debugfs_create_file("tegra_gpio", S_IRUGO,
Laxman Dewanganb546be02016-04-25 16:08:33 +0530465 NULL, tgi, &debug_fops);
Suzuki K. Pouloseb59d5fb2015-11-16 16:07:10 +0000466}
467
468#else
469
Laxman Dewanganb546be02016-04-25 16:08:33 +0530470static inline void tegra_gpio_debuginit(struct tegra_gpio_info *tgi)
Suzuki K. Pouloseb59d5fb2015-11-16 16:07:10 +0000471{
472}
473
474#endif
475
Laxman Dewangan8939ddc2012-11-07 20:31:32 +0530476static const struct dev_pm_ops tegra_gpio_pm_ops = {
477 SET_SYSTEM_SLEEP_PM_OPS(tegra_gpio_suspend, tegra_gpio_resume)
478};
479
Bill Pemberton38363092012-11-19 13:22:34 -0500480static int tegra_gpio_probe(struct platform_device *pdev)
Erik Gilling3c92db92010-03-15 19:40:06 -0700481{
Laxman Dewangan171b92c2016-04-25 16:08:31 +0530482 const struct tegra_gpio_soc_config *config;
Laxman Dewanganb546be02016-04-25 16:08:33 +0530483 struct tegra_gpio_info *tgi;
Stephen Warren88d89512011-10-11 16:16:14 -0600484 struct resource *res;
Erik Gilling3c92db92010-03-15 19:40:06 -0700485 struct tegra_gpio_bank *bank;
Stephen Warrenf57f98a2013-12-06 13:36:11 -0700486 int ret;
Stephen Warren47008002011-08-23 00:39:55 +0100487 int gpio;
Erik Gilling3c92db92010-03-15 19:40:06 -0700488 int i;
489 int j;
490
Laxman Dewangan171b92c2016-04-25 16:08:31 +0530491 config = of_device_get_match_data(&pdev->dev);
492 if (!config) {
Stephen Warren165b6c22013-02-15 14:54:48 -0700493 dev_err(&pdev->dev, "Error: No device match found\n");
494 return -ENODEV;
495 }
Stephen Warren5c1e2c92012-03-16 17:35:08 -0600496
Laxman Dewanganb546be02016-04-25 16:08:33 +0530497 tgi = devm_kzalloc(&pdev->dev, sizeof(*tgi), GFP_KERNEL);
498 if (!tgi)
499 return -ENODEV;
500
501 tgi->soc = config;
502 tgi->dev = &pdev->dev;
Stephen Warren5c1e2c92012-03-16 17:35:08 -0600503
Stephen Warren33918112012-01-19 08:16:35 +0000504 for (;;) {
Laxman Dewanganb546be02016-04-25 16:08:33 +0530505 res = platform_get_resource(pdev, IORESOURCE_IRQ,
506 tgi->bank_count);
Stephen Warren33918112012-01-19 08:16:35 +0000507 if (!res)
508 break;
Laxman Dewanganb546be02016-04-25 16:08:33 +0530509 tgi->bank_count++;
Stephen Warren33918112012-01-19 08:16:35 +0000510 }
Laxman Dewanganb546be02016-04-25 16:08:33 +0530511 if (!tgi->bank_count) {
Stephen Warren33918112012-01-19 08:16:35 +0000512 dev_err(&pdev->dev, "Missing IRQ resource\n");
513 return -ENODEV;
514 }
515
Laxman Dewanganb546be02016-04-25 16:08:33 +0530516 tgi->gc.label = "tegra-gpio";
517 tgi->gc.request = tegra_gpio_request;
518 tgi->gc.free = tegra_gpio_free;
519 tgi->gc.direction_input = tegra_gpio_direction_input;
520 tgi->gc.get = tegra_gpio_get;
521 tgi->gc.direction_output = tegra_gpio_direction_output;
522 tgi->gc.set = tegra_gpio_set;
523 tgi->gc.to_irq = tegra_gpio_to_irq;
524 tgi->gc.base = 0;
525 tgi->gc.ngpio = tgi->bank_count * 32;
526 tgi->gc.parent = &pdev->dev;
527 tgi->gc.of_node = pdev->dev.of_node;
Stephen Warren33918112012-01-19 08:16:35 +0000528
Laxman Dewanganb546be02016-04-25 16:08:33 +0530529 tgi->ic.name = "GPIO";
530 tgi->ic.irq_ack = tegra_gpio_irq_ack;
531 tgi->ic.irq_mask = tegra_gpio_irq_mask;
532 tgi->ic.irq_unmask = tegra_gpio_irq_unmask;
533 tgi->ic.irq_set_type = tegra_gpio_irq_set_type;
534 tgi->ic.irq_shutdown = tegra_gpio_irq_shutdown;
535#ifdef CONFIG_PM_SLEEP
536 tgi->ic.irq_set_wake = tegra_gpio_irq_set_wake;
537#endif
538
539 platform_set_drvdata(pdev, tgi);
540
541 tgi->bank_info = devm_kzalloc(&pdev->dev, tgi->bank_count *
542 sizeof(*tgi->bank_info), GFP_KERNEL);
543 if (!tgi->bank_info)
Stephen Warren33918112012-01-19 08:16:35 +0000544 return -ENODEV;
Stephen Warren33918112012-01-19 08:16:35 +0000545
Laxman Dewanganb546be02016-04-25 16:08:33 +0530546 tgi->irq_domain = irq_domain_add_linear(pdev->dev.of_node,
547 tgi->gc.ngpio,
548 &irq_domain_simple_ops, NULL);
549 if (!tgi->irq_domain)
Linus Walleijd0235672012-10-16 21:00:09 +0200550 return -ENODEV;
Stephen Warren6f74dc92012-01-04 08:39:37 +0000551
Laxman Dewanganb546be02016-04-25 16:08:33 +0530552 for (i = 0; i < tgi->bank_count; i++) {
Stephen Warren88d89512011-10-11 16:16:14 -0600553 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
554 if (!res) {
555 dev_err(&pdev->dev, "Missing IRQ resource\n");
556 return -ENODEV;
557 }
558
Laxman Dewanganb546be02016-04-25 16:08:33 +0530559 bank = &tgi->bank_info[i];
Stephen Warren88d89512011-10-11 16:16:14 -0600560 bank->bank = i;
561 bank->irq = res->start;
Laxman Dewanganb546be02016-04-25 16:08:33 +0530562 bank->tgi = tgi;
Stephen Warren88d89512011-10-11 16:16:14 -0600563 }
564
565 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Laxman Dewanganb546be02016-04-25 16:08:33 +0530566 tgi->regs = devm_ioremap_resource(&pdev->dev, res);
567 if (IS_ERR(tgi->regs))
568 return PTR_ERR(tgi->regs);
Stephen Warren88d89512011-10-11 16:16:14 -0600569
Laxman Dewanganb546be02016-04-25 16:08:33 +0530570 for (i = 0; i < tgi->bank_count; i++) {
Erik Gilling3c92db92010-03-15 19:40:06 -0700571 for (j = 0; j < 4; j++) {
572 int gpio = tegra_gpio_compose(i, j, 0);
Laxman Dewanganb546be02016-04-25 16:08:33 +0530573 tegra_gpio_writel(tgi, 0x00, GPIO_INT_ENB(tgi, gpio));
Erik Gilling3c92db92010-03-15 19:40:06 -0700574 }
575 }
576
Laxman Dewanganb546be02016-04-25 16:08:33 +0530577 ret = devm_gpiochip_add_data(&pdev->dev, &tgi->gc, tgi);
Stephen Warrenf57f98a2013-12-06 13:36:11 -0700578 if (ret < 0) {
Laxman Dewanganb546be02016-04-25 16:08:33 +0530579 irq_domain_remove(tgi->irq_domain);
Stephen Warrenf57f98a2013-12-06 13:36:11 -0700580 return ret;
581 }
Erik Gilling3c92db92010-03-15 19:40:06 -0700582
Laxman Dewanganb546be02016-04-25 16:08:33 +0530583 for (gpio = 0; gpio < tgi->gc.ngpio; gpio++) {
584 int irq = irq_create_mapping(tgi->irq_domain, gpio);
Stephen Warren47008002011-08-23 00:39:55 +0100585 /* No validity check; all Tegra GPIOs are valid IRQs */
Erik Gilling3c92db92010-03-15 19:40:06 -0700586
Laxman Dewanganb546be02016-04-25 16:08:33 +0530587 bank = &tgi->bank_info[GPIO_BANK(gpio)];
Stephen Warren47008002011-08-23 00:39:55 +0100588
Laxman Dewanganb546be02016-04-25 16:08:33 +0530589 irq_set_lockdep_class(irq, &tgi->lock_class);
Stephen Warren47008002011-08-23 00:39:55 +0100590 irq_set_chip_data(irq, bank);
Laxman Dewanganb546be02016-04-25 16:08:33 +0530591 irq_set_chip_and_handler(irq, &tgi->ic, handle_simple_irq);
Erik Gilling3c92db92010-03-15 19:40:06 -0700592 }
593
Laxman Dewanganb546be02016-04-25 16:08:33 +0530594 for (i = 0; i < tgi->bank_count; i++) {
595 bank = &tgi->bank_info[i];
Erik Gilling3c92db92010-03-15 19:40:06 -0700596
Russell Kinge88d2512015-06-16 23:06:50 +0100597 irq_set_chained_handler_and_data(bank->irq,
598 tegra_gpio_irq_handler, bank);
Erik Gilling3c92db92010-03-15 19:40:06 -0700599
600 for (j = 0; j < 4; j++)
601 spin_lock_init(&bank->lvl_lock[j]);
602 }
603
Laxman Dewanganb546be02016-04-25 16:08:33 +0530604 tegra_gpio_debuginit(tgi);
Suzuki K. Pouloseb59d5fb2015-11-16 16:07:10 +0000605
Erik Gilling3c92db92010-03-15 19:40:06 -0700606 return 0;
607}
608
Laxman Dewangan804f5682016-04-25 16:08:32 +0530609static const struct tegra_gpio_soc_config tegra20_gpio_config = {
Laxman Dewangan171b92c2016-04-25 16:08:31 +0530610 .bank_stride = 0x80,
611 .upper_offset = 0x800,
612};
613
Laxman Dewangan804f5682016-04-25 16:08:32 +0530614static const struct tegra_gpio_soc_config tegra30_gpio_config = {
Laxman Dewangan171b92c2016-04-25 16:08:31 +0530615 .bank_stride = 0x100,
616 .upper_offset = 0x80,
617};
618
619static const struct of_device_id tegra_gpio_of_match[] = {
620 { .compatible = "nvidia,tegra30-gpio", .data = &tegra30_gpio_config },
621 { .compatible = "nvidia,tegra20-gpio", .data = &tegra20_gpio_config },
622 { },
623};
624
Stephen Warren88d89512011-10-11 16:16:14 -0600625static struct platform_driver tegra_gpio_driver = {
626 .driver = {
627 .name = "tegra-gpio",
Laxman Dewangan8939ddc2012-11-07 20:31:32 +0530628 .pm = &tegra_gpio_pm_ops,
Stephen Warren88d89512011-10-11 16:16:14 -0600629 .of_match_table = tegra_gpio_of_match,
630 },
631 .probe = tegra_gpio_probe,
632};
633
634static int __init tegra_gpio_init(void)
635{
636 return platform_driver_register(&tegra_gpio_driver);
637}
Erik Gilling3c92db92010-03-15 19:40:06 -0700638postcore_initcall(tegra_gpio_init);