blob: 63cb643d4b5aef31a9ab2fc29ec0f0569531620a [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
20#include <linux/init.h>
21#include <linux/irq.h>
Colin Cross2e47b8b2010-04-07 12:59:42 -070022#include <linux/interrupt.h>
Erik Gilling3c92db92010-03-15 19:40:06 -070023#include <linux/io.h>
24#include <linux/gpio.h>
Stephen Warren5c1e2c92012-03-16 17:35:08 -060025#include <linux/of_device.h>
Stephen Warren88d89512011-10-11 16:16:14 -060026#include <linux/platform_device.h>
27#include <linux/module.h>
Stephen Warren6f74dc92012-01-04 08:39:37 +000028#include <linux/irqdomain.h>
Stephen Warren3e215d02012-02-18 01:04:55 -070029#include <linux/pinctrl/consumer.h>
Laxman Dewangan8939ddc2012-11-07 20:31:32 +053030#include <linux/pm.h>
Erik Gilling3c92db92010-03-15 19:40:06 -070031
Will Deacon98022942011-02-21 13:58:10 +000032#include <asm/mach/irq.h>
33
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
Stephen Warren5c1e2c92012-03-16 17:35:08 -060038#define GPIO_REG(x) (GPIO_BANK(x) * tegra_gpio_bank_stride + \
39 GPIO_PORT(x) * 4)
Erik Gilling3c92db92010-03-15 19:40:06 -070040
41#define GPIO_CNF(x) (GPIO_REG(x) + 0x00)
42#define GPIO_OE(x) (GPIO_REG(x) + 0x10)
43#define GPIO_OUT(x) (GPIO_REG(x) + 0X20)
44#define GPIO_IN(x) (GPIO_REG(x) + 0x30)
45#define GPIO_INT_STA(x) (GPIO_REG(x) + 0x40)
46#define GPIO_INT_ENB(x) (GPIO_REG(x) + 0x50)
47#define GPIO_INT_LVL(x) (GPIO_REG(x) + 0x60)
48#define GPIO_INT_CLR(x) (GPIO_REG(x) + 0x70)
49
Stephen Warren5c1e2c92012-03-16 17:35:08 -060050#define GPIO_MSK_CNF(x) (GPIO_REG(x) + tegra_gpio_upper_offset + 0x00)
51#define GPIO_MSK_OE(x) (GPIO_REG(x) + tegra_gpio_upper_offset + 0x10)
52#define GPIO_MSK_OUT(x) (GPIO_REG(x) + tegra_gpio_upper_offset + 0X20)
53#define GPIO_MSK_INT_STA(x) (GPIO_REG(x) + tegra_gpio_upper_offset + 0x40)
54#define GPIO_MSK_INT_ENB(x) (GPIO_REG(x) + tegra_gpio_upper_offset + 0x50)
55#define GPIO_MSK_INT_LVL(x) (GPIO_REG(x) + tegra_gpio_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
64struct tegra_gpio_bank {
65 int bank;
66 int irq;
67 spinlock_t lvl_lock[4];
Laxman Dewangan8939ddc2012-11-07 20:31:32 +053068#ifdef CONFIG_PM_SLEEP
Colin Cross2e47b8b2010-04-07 12:59:42 -070069 u32 cnf[4];
70 u32 out[4];
71 u32 oe[4];
72 u32 int_enb[4];
73 u32 int_lvl[4];
74#endif
Erik Gilling3c92db92010-03-15 19:40:06 -070075};
76
Stephen Warrenbdc93a72012-02-13 16:21:15 -070077static struct irq_domain *irq_domain;
Stephen Warren88d89512011-10-11 16:16:14 -060078static void __iomem *regs;
Stephen Warren33918112012-01-19 08:16:35 +000079static u32 tegra_gpio_bank_count;
Stephen Warren5c1e2c92012-03-16 17:35:08 -060080static u32 tegra_gpio_bank_stride;
81static u32 tegra_gpio_upper_offset;
Stephen Warren33918112012-01-19 08:16:35 +000082static struct tegra_gpio_bank *tegra_gpio_banks;
Stephen Warren88d89512011-10-11 16:16:14 -060083
84static inline void tegra_gpio_writel(u32 val, u32 reg)
85{
86 __raw_writel(val, regs + reg);
87}
88
89static inline u32 tegra_gpio_readl(u32 reg)
90{
91 return __raw_readl(regs + reg);
92}
Erik Gilling3c92db92010-03-15 19:40:06 -070093
94static int tegra_gpio_compose(int bank, int port, int bit)
95{
96 return (bank << 5) | ((port & 0x3) << 3) | (bit & 0x7);
97}
98
99static void tegra_gpio_mask_write(u32 reg, int gpio, int value)
100{
101 u32 val;
102
103 val = 0x100 << GPIO_BIT(gpio);
104 if (value)
105 val |= 1 << GPIO_BIT(gpio);
Stephen Warren88d89512011-10-11 16:16:14 -0600106 tegra_gpio_writel(val, reg);
Erik Gilling3c92db92010-03-15 19:40:06 -0700107}
108
Stephen Warren3e215d02012-02-18 01:04:55 -0700109static void tegra_gpio_enable(int gpio)
Erik Gilling3c92db92010-03-15 19:40:06 -0700110{
111 tegra_gpio_mask_write(GPIO_MSK_CNF(gpio), gpio, 1);
112}
113
Stephen Warren3e215d02012-02-18 01:04:55 -0700114static void tegra_gpio_disable(int gpio)
Erik Gilling3c92db92010-03-15 19:40:06 -0700115{
116 tegra_gpio_mask_write(GPIO_MSK_CNF(gpio), gpio, 0);
117}
118
Axel Lin924a0982012-11-08 10:45:24 +0800119static int tegra_gpio_request(struct gpio_chip *chip, unsigned offset)
Stephen Warren3e215d02012-02-18 01:04:55 -0700120{
121 return pinctrl_request_gpio(offset);
122}
123
Axel Lin924a0982012-11-08 10:45:24 +0800124static void tegra_gpio_free(struct gpio_chip *chip, unsigned offset)
Stephen Warren3e215d02012-02-18 01:04:55 -0700125{
126 pinctrl_free_gpio(offset);
127 tegra_gpio_disable(offset);
128}
129
Erik Gilling3c92db92010-03-15 19:40:06 -0700130static void tegra_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
131{
132 tegra_gpio_mask_write(GPIO_MSK_OUT(offset), offset, value);
133}
134
135static int tegra_gpio_get(struct gpio_chip *chip, unsigned offset)
136{
Laxman Dewangan195812e2012-11-09 11:34:20 +0530137 /* If gpio is in output mode then read from the out value */
138 if ((tegra_gpio_readl(GPIO_OE(offset)) >> GPIO_BIT(offset)) & 1)
139 return (tegra_gpio_readl(GPIO_OUT(offset)) >>
140 GPIO_BIT(offset)) & 0x1;
141
Stephen Warren88d89512011-10-11 16:16:14 -0600142 return (tegra_gpio_readl(GPIO_IN(offset)) >> GPIO_BIT(offset)) & 0x1;
Erik Gilling3c92db92010-03-15 19:40:06 -0700143}
144
145static int tegra_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
146{
147 tegra_gpio_mask_write(GPIO_MSK_OE(offset), offset, 0);
Stephen Warren3e215d02012-02-18 01:04:55 -0700148 tegra_gpio_enable(offset);
Erik Gilling3c92db92010-03-15 19:40:06 -0700149 return 0;
150}
151
152static int tegra_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
153 int value)
154{
155 tegra_gpio_set(chip, offset, value);
156 tegra_gpio_mask_write(GPIO_MSK_OE(offset), offset, 1);
Stephen Warren3e215d02012-02-18 01:04:55 -0700157 tegra_gpio_enable(offset);
Erik Gilling3c92db92010-03-15 19:40:06 -0700158 return 0;
159}
160
Stephen Warren438a99c2011-08-23 00:39:56 +0100161static int tegra_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
162{
Stephen Warrenbdc93a72012-02-13 16:21:15 -0700163 return irq_find_mapping(irq_domain, offset);
Stephen Warren438a99c2011-08-23 00:39:56 +0100164}
Erik Gilling3c92db92010-03-15 19:40:06 -0700165
166static struct gpio_chip tegra_gpio_chip = {
167 .label = "tegra-gpio",
Stephen Warren3e215d02012-02-18 01:04:55 -0700168 .request = tegra_gpio_request,
169 .free = tegra_gpio_free,
Erik Gilling3c92db92010-03-15 19:40:06 -0700170 .direction_input = tegra_gpio_direction_input,
171 .get = tegra_gpio_get,
172 .direction_output = tegra_gpio_direction_output,
173 .set = tegra_gpio_set,
Stephen Warren438a99c2011-08-23 00:39:56 +0100174 .to_irq = tegra_gpio_to_irq,
Erik Gilling3c92db92010-03-15 19:40:06 -0700175 .base = 0,
Erik Gilling3c92db92010-03-15 19:40:06 -0700176};
177
Lennert Buytenhek37337a82010-11-29 11:14:46 +0100178static void tegra_gpio_irq_ack(struct irq_data *d)
Erik Gilling3c92db92010-03-15 19:40:06 -0700179{
Stephen Warren6f74dc92012-01-04 08:39:37 +0000180 int gpio = d->hwirq;
Erik Gilling3c92db92010-03-15 19:40:06 -0700181
Stephen Warren88d89512011-10-11 16:16:14 -0600182 tegra_gpio_writel(1 << GPIO_BIT(gpio), GPIO_INT_CLR(gpio));
Erik Gilling3c92db92010-03-15 19:40:06 -0700183}
184
Lennert Buytenhek37337a82010-11-29 11:14:46 +0100185static void tegra_gpio_irq_mask(struct irq_data *d)
Erik Gilling3c92db92010-03-15 19:40:06 -0700186{
Stephen Warren6f74dc92012-01-04 08:39:37 +0000187 int gpio = d->hwirq;
Erik Gilling3c92db92010-03-15 19:40:06 -0700188
189 tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio), gpio, 0);
190}
191
Lennert Buytenhek37337a82010-11-29 11:14:46 +0100192static void tegra_gpio_irq_unmask(struct irq_data *d)
Erik Gilling3c92db92010-03-15 19:40:06 -0700193{
Stephen Warren6f74dc92012-01-04 08:39:37 +0000194 int gpio = d->hwirq;
Erik Gilling3c92db92010-03-15 19:40:06 -0700195
196 tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio), gpio, 1);
197}
198
Lennert Buytenhek37337a82010-11-29 11:14:46 +0100199static int tegra_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Erik Gilling3c92db92010-03-15 19:40:06 -0700200{
Stephen Warren6f74dc92012-01-04 08:39:37 +0000201 int gpio = d->hwirq;
Lennert Buytenhek37337a82010-11-29 11:14:46 +0100202 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
Erik Gilling3c92db92010-03-15 19:40:06 -0700203 int port = GPIO_PORT(gpio);
204 int lvl_type;
205 int val;
206 unsigned long flags;
207
208 switch (type & IRQ_TYPE_SENSE_MASK) {
209 case IRQ_TYPE_EDGE_RISING:
210 lvl_type = GPIO_INT_LVL_EDGE_RISING;
211 break;
212
213 case IRQ_TYPE_EDGE_FALLING:
214 lvl_type = GPIO_INT_LVL_EDGE_FALLING;
215 break;
216
217 case IRQ_TYPE_EDGE_BOTH:
218 lvl_type = GPIO_INT_LVL_EDGE_BOTH;
219 break;
220
221 case IRQ_TYPE_LEVEL_HIGH:
222 lvl_type = GPIO_INT_LVL_LEVEL_HIGH;
223 break;
224
225 case IRQ_TYPE_LEVEL_LOW:
226 lvl_type = GPIO_INT_LVL_LEVEL_LOW;
227 break;
228
229 default:
230 return -EINVAL;
231 }
232
233 spin_lock_irqsave(&bank->lvl_lock[port], flags);
234
Stephen Warren88d89512011-10-11 16:16:14 -0600235 val = tegra_gpio_readl(GPIO_INT_LVL(gpio));
Erik Gilling3c92db92010-03-15 19:40:06 -0700236 val &= ~(GPIO_INT_LVL_MASK << GPIO_BIT(gpio));
237 val |= lvl_type << GPIO_BIT(gpio);
Stephen Warren88d89512011-10-11 16:16:14 -0600238 tegra_gpio_writel(val, GPIO_INT_LVL(gpio));
Erik Gilling3c92db92010-03-15 19:40:06 -0700239
240 spin_unlock_irqrestore(&bank->lvl_lock[port], flags);
241
Stephen Warrend9411362012-03-19 10:31:58 -0600242 tegra_gpio_mask_write(GPIO_MSK_OE(gpio), gpio, 0);
243 tegra_gpio_enable(gpio);
244
Erik Gilling3c92db92010-03-15 19:40:06 -0700245 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100246 __irq_set_handler_locked(d->irq, handle_level_irq);
Erik Gilling3c92db92010-03-15 19:40:06 -0700247 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100248 __irq_set_handler_locked(d->irq, handle_edge_irq);
Erik Gilling3c92db92010-03-15 19:40:06 -0700249
250 return 0;
251}
252
253static void tegra_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
254{
255 struct tegra_gpio_bank *bank;
256 int port;
257 int pin;
258 int unmasked = 0;
Will Deacon98022942011-02-21 13:58:10 +0000259 struct irq_chip *chip = irq_desc_get_chip(desc);
Erik Gilling3c92db92010-03-15 19:40:06 -0700260
Will Deacon98022942011-02-21 13:58:10 +0000261 chained_irq_enter(chip, desc);
Erik Gilling3c92db92010-03-15 19:40:06 -0700262
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100263 bank = irq_get_handler_data(irq);
Erik Gilling3c92db92010-03-15 19:40:06 -0700264
265 for (port = 0; port < 4; port++) {
266 int gpio = tegra_gpio_compose(bank->bank, port, 0);
Stephen Warren88d89512011-10-11 16:16:14 -0600267 unsigned long sta = tegra_gpio_readl(GPIO_INT_STA(gpio)) &
268 tegra_gpio_readl(GPIO_INT_ENB(gpio));
269 u32 lvl = tegra_gpio_readl(GPIO_INT_LVL(gpio));
Erik Gilling3c92db92010-03-15 19:40:06 -0700270
271 for_each_set_bit(pin, &sta, 8) {
Stephen Warren88d89512011-10-11 16:16:14 -0600272 tegra_gpio_writel(1 << pin, GPIO_INT_CLR(gpio));
Erik Gilling3c92db92010-03-15 19:40:06 -0700273
274 /* if gpio is edge triggered, clear condition
275 * before executing the hander so that we don't
276 * miss edges
277 */
278 if (lvl & (0x100 << pin)) {
279 unmasked = 1;
Will Deacon98022942011-02-21 13:58:10 +0000280 chained_irq_exit(chip, desc);
Erik Gilling3c92db92010-03-15 19:40:06 -0700281 }
282
283 generic_handle_irq(gpio_to_irq(gpio + pin));
284 }
285 }
286
287 if (!unmasked)
Will Deacon98022942011-02-21 13:58:10 +0000288 chained_irq_exit(chip, desc);
Erik Gilling3c92db92010-03-15 19:40:06 -0700289
290}
291
Laxman Dewangan8939ddc2012-11-07 20:31:32 +0530292#ifdef CONFIG_PM_SLEEP
293static int tegra_gpio_resume(struct device *dev)
Colin Cross2e47b8b2010-04-07 12:59:42 -0700294{
295 unsigned long flags;
Colin Crossc8309ef2011-03-30 00:24:43 -0700296 int b;
297 int p;
Colin Cross2e47b8b2010-04-07 12:59:42 -0700298
299 local_irq_save(flags);
300
Stephen Warren33918112012-01-19 08:16:35 +0000301 for (b = 0; b < tegra_gpio_bank_count; b++) {
Colin Cross2e47b8b2010-04-07 12:59:42 -0700302 struct tegra_gpio_bank *bank = &tegra_gpio_banks[b];
303
304 for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
305 unsigned int gpio = (b<<5) | (p<<3);
Stephen Warren88d89512011-10-11 16:16:14 -0600306 tegra_gpio_writel(bank->cnf[p], GPIO_CNF(gpio));
307 tegra_gpio_writel(bank->out[p], GPIO_OUT(gpio));
308 tegra_gpio_writel(bank->oe[p], GPIO_OE(gpio));
309 tegra_gpio_writel(bank->int_lvl[p], GPIO_INT_LVL(gpio));
310 tegra_gpio_writel(bank->int_enb[p], GPIO_INT_ENB(gpio));
Colin Cross2e47b8b2010-04-07 12:59:42 -0700311 }
312 }
313
314 local_irq_restore(flags);
Laxman Dewangan8939ddc2012-11-07 20:31:32 +0530315 return 0;
Colin Cross2e47b8b2010-04-07 12:59:42 -0700316}
317
Laxman Dewangan8939ddc2012-11-07 20:31:32 +0530318static int tegra_gpio_suspend(struct device *dev)
Colin Cross2e47b8b2010-04-07 12:59:42 -0700319{
320 unsigned long flags;
Colin Crossc8309ef2011-03-30 00:24:43 -0700321 int b;
322 int p;
Colin Cross2e47b8b2010-04-07 12:59:42 -0700323
Colin Cross2e47b8b2010-04-07 12:59:42 -0700324 local_irq_save(flags);
Stephen Warren33918112012-01-19 08:16:35 +0000325 for (b = 0; b < tegra_gpio_bank_count; b++) {
Colin Cross2e47b8b2010-04-07 12:59:42 -0700326 struct tegra_gpio_bank *bank = &tegra_gpio_banks[b];
327
328 for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
329 unsigned int gpio = (b<<5) | (p<<3);
Stephen Warren88d89512011-10-11 16:16:14 -0600330 bank->cnf[p] = tegra_gpio_readl(GPIO_CNF(gpio));
331 bank->out[p] = tegra_gpio_readl(GPIO_OUT(gpio));
332 bank->oe[p] = tegra_gpio_readl(GPIO_OE(gpio));
333 bank->int_enb[p] = tegra_gpio_readl(GPIO_INT_ENB(gpio));
334 bank->int_lvl[p] = tegra_gpio_readl(GPIO_INT_LVL(gpio));
Colin Cross2e47b8b2010-04-07 12:59:42 -0700335 }
336 }
337 local_irq_restore(flags);
Laxman Dewangan8939ddc2012-11-07 20:31:32 +0530338 return 0;
Colin Cross2e47b8b2010-04-07 12:59:42 -0700339}
340
Lennert Buytenhek37337a82010-11-29 11:14:46 +0100341static int tegra_gpio_wake_enable(struct irq_data *d, unsigned int enable)
Colin Cross2e47b8b2010-04-07 12:59:42 -0700342{
Lennert Buytenhek37337a82010-11-29 11:14:46 +0100343 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100344 return irq_set_irq_wake(bank->irq, enable);
Colin Cross2e47b8b2010-04-07 12:59:42 -0700345}
346#endif
Erik Gilling3c92db92010-03-15 19:40:06 -0700347
348static struct irq_chip tegra_gpio_irq_chip = {
349 .name = "GPIO",
Lennert Buytenhek37337a82010-11-29 11:14:46 +0100350 .irq_ack = tegra_gpio_irq_ack,
351 .irq_mask = tegra_gpio_irq_mask,
352 .irq_unmask = tegra_gpio_irq_unmask,
353 .irq_set_type = tegra_gpio_irq_set_type,
Laxman Dewangan8939ddc2012-11-07 20:31:32 +0530354#ifdef CONFIG_PM_SLEEP
Lennert Buytenhek37337a82010-11-29 11:14:46 +0100355 .irq_set_wake = tegra_gpio_wake_enable,
Colin Cross2e47b8b2010-04-07 12:59:42 -0700356#endif
Erik Gilling3c92db92010-03-15 19:40:06 -0700357};
358
Laxman Dewangan8939ddc2012-11-07 20:31:32 +0530359static const struct dev_pm_ops tegra_gpio_pm_ops = {
360 SET_SYSTEM_SLEEP_PM_OPS(tegra_gpio_suspend, tegra_gpio_resume)
361};
362
Stephen Warren5c1e2c92012-03-16 17:35:08 -0600363struct tegra_gpio_soc_config {
364 u32 bank_stride;
365 u32 upper_offset;
366};
367
368static struct tegra_gpio_soc_config tegra20_gpio_config = {
369 .bank_stride = 0x80,
370 .upper_offset = 0x800,
371};
372
373static struct tegra_gpio_soc_config tegra30_gpio_config = {
374 .bank_stride = 0x100,
375 .upper_offset = 0x80,
376};
377
Bill Pembertonaeca8ad2012-11-19 13:24:14 -0500378static struct of_device_id tegra_gpio_of_match[] = {
Stephen Warren5c1e2c92012-03-16 17:35:08 -0600379 { .compatible = "nvidia,tegra30-gpio", .data = &tegra30_gpio_config },
380 { .compatible = "nvidia,tegra20-gpio", .data = &tegra20_gpio_config },
381 { },
382};
Erik Gilling3c92db92010-03-15 19:40:06 -0700383
384/* This lock class tells lockdep that GPIO irqs are in a different
385 * category than their parents, so it won't report false recursion.
386 */
387static struct lock_class_key gpio_lock_class;
388
Bill Pemberton38363092012-11-19 13:22:34 -0500389static int tegra_gpio_probe(struct platform_device *pdev)
Erik Gilling3c92db92010-03-15 19:40:06 -0700390{
Stephen Warren5c1e2c92012-03-16 17:35:08 -0600391 const struct of_device_id *match;
392 struct tegra_gpio_soc_config *config;
Stephen Warren88d89512011-10-11 16:16:14 -0600393 struct resource *res;
Erik Gilling3c92db92010-03-15 19:40:06 -0700394 struct tegra_gpio_bank *bank;
Stephen Warren47008002011-08-23 00:39:55 +0100395 int gpio;
Erik Gilling3c92db92010-03-15 19:40:06 -0700396 int i;
397 int j;
398
Stephen Warren5c1e2c92012-03-16 17:35:08 -0600399 match = of_match_device(tegra_gpio_of_match, &pdev->dev);
400 if (match)
401 config = (struct tegra_gpio_soc_config *)match->data;
402 else
403 config = &tegra20_gpio_config;
404
405 tegra_gpio_bank_stride = config->bank_stride;
406 tegra_gpio_upper_offset = config->upper_offset;
407
Stephen Warren33918112012-01-19 08:16:35 +0000408 for (;;) {
409 res = platform_get_resource(pdev, IORESOURCE_IRQ, tegra_gpio_bank_count);
410 if (!res)
411 break;
412 tegra_gpio_bank_count++;
413 }
414 if (!tegra_gpio_bank_count) {
415 dev_err(&pdev->dev, "Missing IRQ resource\n");
416 return -ENODEV;
417 }
418
419 tegra_gpio_chip.ngpio = tegra_gpio_bank_count * 32;
420
421 tegra_gpio_banks = devm_kzalloc(&pdev->dev,
422 tegra_gpio_bank_count * sizeof(*tegra_gpio_banks),
423 GFP_KERNEL);
424 if (!tegra_gpio_banks) {
425 dev_err(&pdev->dev, "Couldn't allocate bank structure\n");
426 return -ENODEV;
427 }
428
Linus Walleijd0235672012-10-16 21:00:09 +0200429 irq_domain = irq_domain_add_linear(pdev->dev.of_node,
430 tegra_gpio_chip.ngpio,
Stephen Warrenbdc93a72012-02-13 16:21:15 -0700431 &irq_domain_simple_ops, NULL);
Linus Walleijd0235672012-10-16 21:00:09 +0200432 if (!irq_domain)
433 return -ENODEV;
Stephen Warren6f74dc92012-01-04 08:39:37 +0000434
Stephen Warren33918112012-01-19 08:16:35 +0000435 for (i = 0; i < tegra_gpio_bank_count; i++) {
Stephen Warren88d89512011-10-11 16:16:14 -0600436 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
437 if (!res) {
438 dev_err(&pdev->dev, "Missing IRQ resource\n");
439 return -ENODEV;
440 }
441
442 bank = &tegra_gpio_banks[i];
443 bank->bank = i;
444 bank->irq = res->start;
445 }
446
447 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
448 if (!res) {
449 dev_err(&pdev->dev, "Missing MEM resource\n");
450 return -ENODEV;
451 }
452
Julia Lawallaedd4fd2011-12-27 15:01:26 +0100453 regs = devm_request_and_ioremap(&pdev->dev, res);
Stephen Warren88d89512011-10-11 16:16:14 -0600454 if (!regs) {
455 dev_err(&pdev->dev, "Couldn't ioremap regs\n");
456 return -ENODEV;
457 }
458
Stephen Warren4a3398e2012-03-16 17:37:24 -0600459 for (i = 0; i < tegra_gpio_bank_count; i++) {
Erik Gilling3c92db92010-03-15 19:40:06 -0700460 for (j = 0; j < 4; j++) {
461 int gpio = tegra_gpio_compose(i, j, 0);
Stephen Warren88d89512011-10-11 16:16:14 -0600462 tegra_gpio_writel(0x00, GPIO_INT_ENB(gpio));
Erik Gilling3c92db92010-03-15 19:40:06 -0700463 }
464 }
465
Grant Likelydf221222011-06-15 14:54:14 -0600466#ifdef CONFIG_OF_GPIO
Stephen Warren88d89512011-10-11 16:16:14 -0600467 tegra_gpio_chip.of_node = pdev->dev.of_node;
468#endif
Grant Likelydf221222011-06-15 14:54:14 -0600469
Erik Gilling3c92db92010-03-15 19:40:06 -0700470 gpiochip_add(&tegra_gpio_chip);
471
Stephen Warren33918112012-01-19 08:16:35 +0000472 for (gpio = 0; gpio < tegra_gpio_chip.ngpio; gpio++) {
Linus Walleijd0235672012-10-16 21:00:09 +0200473 int irq = irq_create_mapping(irq_domain, gpio);
Stephen Warren47008002011-08-23 00:39:55 +0100474 /* No validity check; all Tegra GPIOs are valid IRQs */
Erik Gilling3c92db92010-03-15 19:40:06 -0700475
Stephen Warren47008002011-08-23 00:39:55 +0100476 bank = &tegra_gpio_banks[GPIO_BANK(gpio)];
477
478 irq_set_lockdep_class(irq, &gpio_lock_class);
479 irq_set_chip_data(irq, bank);
480 irq_set_chip_and_handler(irq, &tegra_gpio_irq_chip,
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100481 handle_simple_irq);
Stephen Warren47008002011-08-23 00:39:55 +0100482 set_irq_flags(irq, IRQF_VALID);
Erik Gilling3c92db92010-03-15 19:40:06 -0700483 }
484
Stephen Warren33918112012-01-19 08:16:35 +0000485 for (i = 0; i < tegra_gpio_bank_count; i++) {
Erik Gilling3c92db92010-03-15 19:40:06 -0700486 bank = &tegra_gpio_banks[i];
487
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100488 irq_set_chained_handler(bank->irq, tegra_gpio_irq_handler);
489 irq_set_handler_data(bank->irq, bank);
Erik Gilling3c92db92010-03-15 19:40:06 -0700490
491 for (j = 0; j < 4; j++)
492 spin_lock_init(&bank->lvl_lock[j]);
493 }
494
495 return 0;
496}
497
Stephen Warren88d89512011-10-11 16:16:14 -0600498static struct platform_driver tegra_gpio_driver = {
499 .driver = {
500 .name = "tegra-gpio",
501 .owner = THIS_MODULE,
Laxman Dewangan8939ddc2012-11-07 20:31:32 +0530502 .pm = &tegra_gpio_pm_ops,
Stephen Warren88d89512011-10-11 16:16:14 -0600503 .of_match_table = tegra_gpio_of_match,
504 },
505 .probe = tegra_gpio_probe,
506};
507
508static int __init tegra_gpio_init(void)
509{
510 return platform_driver_register(&tegra_gpio_driver);
511}
Erik Gilling3c92db92010-03-15 19:40:06 -0700512postcore_initcall(tegra_gpio_init);
513
514#ifdef CONFIG_DEBUG_FS
515
516#include <linux/debugfs.h>
517#include <linux/seq_file.h>
518
519static int dbg_gpio_show(struct seq_file *s, void *unused)
520{
521 int i;
522 int j;
523
Stephen Warren4a3398e2012-03-16 17:37:24 -0600524 for (i = 0; i < tegra_gpio_bank_count; i++) {
Erik Gilling3c92db92010-03-15 19:40:06 -0700525 for (j = 0; j < 4; j++) {
526 int gpio = tegra_gpio_compose(i, j, 0);
Colin Cross2e47b8b2010-04-07 12:59:42 -0700527 seq_printf(s,
528 "%d:%d %02x %02x %02x %02x %02x %02x %06x\n",
529 i, j,
Stephen Warren88d89512011-10-11 16:16:14 -0600530 tegra_gpio_readl(GPIO_CNF(gpio)),
531 tegra_gpio_readl(GPIO_OE(gpio)),
532 tegra_gpio_readl(GPIO_OUT(gpio)),
533 tegra_gpio_readl(GPIO_IN(gpio)),
534 tegra_gpio_readl(GPIO_INT_STA(gpio)),
535 tegra_gpio_readl(GPIO_INT_ENB(gpio)),
536 tegra_gpio_readl(GPIO_INT_LVL(gpio)));
Erik Gilling3c92db92010-03-15 19:40:06 -0700537 }
538 }
539 return 0;
540}
541
542static int dbg_gpio_open(struct inode *inode, struct file *file)
543{
544 return single_open(file, dbg_gpio_show, &inode->i_private);
545}
546
547static const struct file_operations debug_fops = {
548 .open = dbg_gpio_open,
549 .read = seq_read,
550 .llseek = seq_lseek,
551 .release = single_release,
552};
553
554static int __init tegra_gpio_debuginit(void)
555{
556 (void) debugfs_create_file("tegra_gpio", S_IRUGO,
557 NULL, NULL, &debug_fops);
558 return 0;
559}
560late_initcall(tegra_gpio_debuginit);
561#endif