]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/gpio/gpio-tegra.c
ASoc: rt5639: Set audio CODEC IRQ as a wake up pin
[sojka/nv-tegra/linux-3.10.git] / drivers / gpio / gpio-tegra.c
1 /*
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  * Copyright (c) 2011-2014, NVIDIA CORPORATION.  All rights reserved.
10  *
11  * This software is licensed under the terms of the GNU General Public
12  * License version 2, as published by the Free Software Foundation, and
13  * may be copied, distributed, and modified under those terms.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  */
21
22 #include <linux/err.h>
23 #include <linux/init.h>
24 #include <linux/irq.h>
25 #include <linux/interrupt.h>
26 #include <linux/io.h>
27 #include <linux/gpio.h>
28 #include <linux/of_device.h>
29 #include <linux/platform_device.h>
30 #include <linux/module.h>
31 #include <linux/delay.h>
32 #include <linux/irqdomain.h>
33 #include <linux/irqchip/chained_irq.h>
34 #include <linux/pinctrl/consumer.h>
35 #include <linux/pm.h>
36 #include <linux/syscore_ops.h>
37 #include <linux/tegra-soc.h>
38 #include <linux/irqchip/tegra.h>
39 #include <linux/tegra-pm.h>
40
41 #define GPIO_BANK(x)            ((x) >> 5)
42 #define GPIO_PORT(x)            (((x) >> 3) & 0x3)
43 #define GPIO_BIT(x)             ((x) & 0x7)
44
45 #define GPIO_REG(x)             (GPIO_BANK(x) * tegra_gpio_bank_stride + \
46                                         GPIO_PORT(x) * 4)
47
48 #define GPIO_CNF(x)             (GPIO_REG(x) + 0x00)
49 #define GPIO_OE(x)              (GPIO_REG(x) + 0x10)
50 #define GPIO_OUT(x)             (GPIO_REG(x) + 0X20)
51 #define GPIO_IN(x)              (GPIO_REG(x) + 0x30)
52 #define GPIO_INT_STA(x)         (GPIO_REG(x) + 0x40)
53 #define GPIO_INT_ENB(x)         (GPIO_REG(x) + 0x50)
54 #define GPIO_INT_LVL(x)         (GPIO_REG(x) + 0x60)
55 #define GPIO_INT_CLR(x)         (GPIO_REG(x) + 0x70)
56
57 #define GPIO_MSK_CNF(x)         (GPIO_REG(x) + tegra_gpio_upper_offset + 0x00)
58 #define GPIO_MSK_OE(x)          (GPIO_REG(x) + tegra_gpio_upper_offset + 0x10)
59 #define GPIO_MSK_OUT(x)         (GPIO_REG(x) + tegra_gpio_upper_offset + 0X20)
60 #define GPIO_MSK_INT_STA(x)     (GPIO_REG(x) + tegra_gpio_upper_offset + 0x40)
61 #define GPIO_MSK_INT_ENB(x)     (GPIO_REG(x) + tegra_gpio_upper_offset + 0x50)
62 #define GPIO_MSK_INT_LVL(x)     (GPIO_REG(x) + tegra_gpio_upper_offset + 0x60)
63
64 #define GPIO_INT_LVL_MASK               0x010101
65 #define GPIO_INT_LVL_EDGE_RISING        0x000101
66 #define GPIO_INT_LVL_EDGE_FALLING       0x000100
67 #define GPIO_INT_LVL_EDGE_BOTH          0x010100
68 #define GPIO_INT_LVL_LEVEL_HIGH         0x000001
69 #define GPIO_INT_LVL_LEVEL_LOW          0x000000
70
71 struct tegra_gpio_bank {
72         int bank;
73         int irq;
74         spinlock_t lvl_lock[4];
75 #ifdef CONFIG_PM_SLEEP
76         u32 cnf[4];
77         u32 out[4];
78         u32 oe[4];
79         u32 int_enb[4];
80         u32 int_lvl[4];
81         u32 wake_enb[4];
82         int wake_depth;
83         int wake_lp0_cap[4];
84 #endif
85 };
86
87 static struct irq_domain *irq_domain;
88 static void __iomem *regs;
89
90 static u32 tegra_gpio_bank_count;
91 static u32 tegra_gpio_bank_stride;
92 static u32 tegra_gpio_upper_offset;
93 static struct tegra_gpio_bank *tegra_gpio_banks;
94
95 static inline void tegra_gpio_writel(u32 val, u32 reg)
96 {
97         __raw_writel(val, regs + reg);
98 }
99
100 static inline u32 tegra_gpio_readl(u32 reg)
101 {
102         return __raw_readl(regs + reg);
103 }
104
105 static int tegra_gpio_compose(int bank, int port, int bit)
106 {
107         return (bank << 5) | ((port & 0x3) << 3) | (bit & 0x7);
108 }
109
110 static void tegra_gpio_mask_write(u32 reg, int gpio, int value)
111 {
112         u32 val;
113
114         val = 0x100 << GPIO_BIT(gpio);
115         if (value)
116                 val |= 1 << GPIO_BIT(gpio);
117         tegra_gpio_writel(val, reg);
118 }
119
120 int tegra_gpio_get_bank_int_nr(int gpio)
121 {
122         int bank;
123         int irq;
124         bank = gpio >> 5;
125         irq = tegra_gpio_banks[bank].irq;
126         return irq;
127 }
128
129 static void tegra_gpio_enable(int gpio)
130 {
131         tegra_gpio_mask_write(GPIO_MSK_CNF(gpio), gpio, 1);
132 }
133
134 int tegra_is_gpio(int gpio)
135 {
136         return (tegra_gpio_readl(GPIO_CNF(gpio)) >> GPIO_BIT(gpio)) & 0x1;
137 }
138 EXPORT_SYMBOL(tegra_is_gpio);
139
140
141 static void tegra_gpio_disable(int gpio)
142 {
143         tegra_gpio_mask_write(GPIO_MSK_CNF(gpio), gpio, 0);
144 }
145
146 void tegra_gpio_init_configure(unsigned gpio, bool is_input, int value)
147 {
148         if (is_input) {
149                 tegra_gpio_mask_write(GPIO_MSK_OE(gpio), gpio, 0);
150         } else {
151                 tegra_gpio_mask_write(GPIO_MSK_OUT(gpio), gpio, value);
152                 tegra_gpio_mask_write(GPIO_MSK_OE(gpio), gpio, 1);
153         }
154         tegra_gpio_mask_write(GPIO_MSK_CNF(gpio), gpio, 1);
155 }
156
157 static int tegra_gpio_request(struct gpio_chip *chip, unsigned offset)
158 {
159 #if 0
160         return pinctrl_request_gpio(offset);
161 #else
162         return 0;
163 #endif
164 }
165
166 static void tegra_gpio_free(struct gpio_chip *chip, unsigned offset)
167 {
168 #if 0
169         pinctrl_free_gpio(offset);
170 #endif
171         tegra_gpio_disable(offset);
172 }
173
174 static void tegra_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
175 {
176         tegra_gpio_mask_write(GPIO_MSK_OUT(offset), offset, value);
177 }
178
179 static int tegra_gpio_get(struct gpio_chip *chip, unsigned offset)
180 {
181         /* If gpio is in output mode then read from the out value */
182         if ((tegra_gpio_readl(GPIO_OE(offset)) >> GPIO_BIT(offset)) & 1)
183                 return (tegra_gpio_readl(GPIO_OUT(offset)) >>
184                                 GPIO_BIT(offset)) & 0x1;
185
186         return (tegra_gpio_readl(GPIO_IN(offset)) >> GPIO_BIT(offset)) & 0x1;
187 }
188
189 static int tegra_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
190 {
191         tegra_gpio_mask_write(GPIO_MSK_OE(offset), offset, 0);
192         tegra_gpio_enable(offset);
193         return 0;
194 }
195
196 static int tegra_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
197                                         int value)
198 {
199         tegra_gpio_set(chip, offset, value);
200         tegra_gpio_mask_write(GPIO_MSK_OE(offset), offset, 1);
201         tegra_gpio_enable(offset);
202         return 0;
203 }
204
205 static int tegra_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
206                                 unsigned debounce)
207 {
208         return -ENOSYS;
209 }
210
211 static int tegra_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
212 {
213         return irq_find_mapping(irq_domain, offset);
214 }
215
216 static struct gpio_chip tegra_gpio_chip = {
217         .label                  = "tegra-gpio",
218         .request                = tegra_gpio_request,
219         .free                   = tegra_gpio_free,
220         .direction_input        = tegra_gpio_direction_input,
221         .get                    = tegra_gpio_get,
222         .direction_output       = tegra_gpio_direction_output,
223         .set                    = tegra_gpio_set,
224         .set_debounce           = tegra_gpio_set_debounce,
225         .to_irq                 = tegra_gpio_to_irq,
226         .base                   = 0,
227 };
228
229 static void tegra_gpio_irq_ack(struct irq_data *d)
230 {
231         int gpio = d->hwirq;
232
233         tegra_gpio_writel(1 << GPIO_BIT(gpio), GPIO_INT_CLR(gpio));
234
235         /* FPGA platforms have a serializer between the GPIO
236            block and interrupt controller. Allow time for
237            clearing of the GPIO interrupt to propagate to the
238            interrupt controller before re-enabling the IRQ
239            to prevent double interrupts. */
240         if (tegra_platform_is_fpga())
241                 udelay(15);
242 }
243
244 static void tegra_gpio_irq_mask(struct irq_data *d)
245 {
246         int gpio = d->hwirq;
247
248         tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio), gpio, 0);
249 }
250
251 static void tegra_gpio_irq_unmask(struct irq_data *d)
252 {
253         int gpio = d->hwirq;
254
255         tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio), gpio, 1);
256 }
257
258 static int tegra_gpio_irq_set_type(struct irq_data *d, unsigned int type)
259 {
260         int gpio = d->hwirq;
261         struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
262         int port = GPIO_PORT(gpio);
263         int lvl_type;
264         int val;
265         unsigned long flags;
266         int wake = tegra_gpio_to_wake(d->hwirq);
267
268         switch (type & IRQ_TYPE_SENSE_MASK) {
269         case IRQ_TYPE_EDGE_RISING:
270                 lvl_type = GPIO_INT_LVL_EDGE_RISING;
271                 break;
272
273         case IRQ_TYPE_EDGE_FALLING:
274                 lvl_type = GPIO_INT_LVL_EDGE_FALLING;
275                 break;
276
277         case IRQ_TYPE_EDGE_BOTH:
278                 lvl_type = GPIO_INT_LVL_EDGE_BOTH;
279                 break;
280
281         case IRQ_TYPE_LEVEL_HIGH:
282                 lvl_type = GPIO_INT_LVL_LEVEL_HIGH;
283                 break;
284
285         case IRQ_TYPE_LEVEL_LOW:
286                 lvl_type = GPIO_INT_LVL_LEVEL_LOW;
287                 break;
288
289         default:
290                 return -EINVAL;
291         }
292
293         spin_lock_irqsave(&bank->lvl_lock[port], flags);
294
295         val = tegra_gpio_readl(GPIO_INT_LVL(gpio));
296         val &= ~(GPIO_INT_LVL_MASK << GPIO_BIT(gpio));
297         val |= lvl_type << GPIO_BIT(gpio);
298         tegra_gpio_writel(val, GPIO_INT_LVL(gpio));
299
300         spin_unlock_irqrestore(&bank->lvl_lock[port], flags);
301
302         tegra_gpio_mask_write(GPIO_MSK_OE(gpio), gpio, 0);
303         tegra_gpio_enable(gpio);
304
305         if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
306                 __irq_set_handler_locked(d->irq, handle_level_irq);
307         else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
308                 __irq_set_handler_locked(d->irq, handle_edge_irq);
309
310         tegra_pm_irq_set_wake_type(wake, type);
311
312         return 0;
313 }
314
315 static void tegra_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
316 {
317         struct tegra_gpio_bank *bank;
318         int port;
319         int pin;
320         struct irq_chip *chip = irq_desc_get_chip(desc);
321
322         chained_irq_enter(chip, desc);
323
324         bank = irq_get_handler_data(irq);
325
326         for (port = 0; port < 4; port++) {
327                 int gpio = tegra_gpio_compose(bank->bank, port, 0);
328                 unsigned long sta = tegra_gpio_readl(GPIO_INT_STA(gpio)) &
329                         tegra_gpio_readl(GPIO_INT_ENB(gpio));
330
331                 for_each_set_bit(pin, &sta, 8)
332                         generic_handle_irq(gpio_to_irq(gpio + pin));
333         }
334
335         chained_irq_exit(chip, desc);
336
337 }
338
339 #ifdef CONFIG_PM_SLEEP
340 static void tegra_gpio_resume(void)
341 {
342         unsigned long flags;
343         int b;
344         int p;
345
346         local_irq_save(flags);
347
348         for (b = 0; b < tegra_gpio_bank_count; b++) {
349                 struct tegra_gpio_bank *bank = &tegra_gpio_banks[b];
350
351                 for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
352                         unsigned int gpio = (b<<5) | (p<<3);
353                         tegra_gpio_writel(bank->cnf[p], GPIO_CNF(gpio));
354                         tegra_gpio_writel(bank->out[p], GPIO_OUT(gpio));
355                         tegra_gpio_writel(bank->oe[p], GPIO_OE(gpio));
356                         tegra_gpio_writel(bank->int_lvl[p], GPIO_INT_LVL(gpio));
357                         tegra_gpio_writel(bank->int_enb[p], GPIO_INT_ENB(gpio));
358                 }
359         }
360
361         local_irq_restore(flags);
362 }
363
364 static int tegra_gpio_suspend(void)
365 {
366         unsigned long flags;
367         int b;
368         int p;
369
370         local_irq_save(flags);
371         for (b = 0; b < tegra_gpio_bank_count; b++) {
372                 struct tegra_gpio_bank *bank = &tegra_gpio_banks[b];
373
374                 for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
375                         unsigned int gpio = (b<<5) | (p<<3);
376                         unsigned int wake_enb;
377                         bank->cnf[p] = tegra_gpio_readl(GPIO_CNF(gpio));
378                         bank->out[p] = tegra_gpio_readl(GPIO_OUT(gpio));
379                         bank->oe[p] = tegra_gpio_readl(GPIO_OE(gpio));
380                         bank->int_enb[p] = tegra_gpio_readl(GPIO_INT_ENB(gpio));
381                         bank->int_lvl[p] = tegra_gpio_readl(GPIO_INT_LVL(gpio));
382
383                         /* disable gpio interrupts that are not wake sources */
384                         wake_enb = (current_suspend_mode == TEGRA_SUSPEND_LP0) ?
385                                 (bank->wake_enb[p] & bank->wake_lp0_cap[p]) :
386                                 bank->wake_enb[p];
387                         tegra_gpio_writel(wake_enb, GPIO_INT_ENB(gpio));
388                 }
389         }
390         local_irq_restore(flags);
391
392         return 0;
393 }
394
395 static int tegra_update_lp1_gpio_wake(struct irq_data *d, bool enable, int wake)
396 {
397 #ifdef CONFIG_PM_SLEEP
398         struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
399         u8 mask;
400         u8 port_index;
401         u8 pin_index_in_bank;
402         u8 pin_in_port;
403         int gpio = d->hwirq;
404
405         if (gpio < 0)
406                 return -EIO;
407         pin_index_in_bank = (gpio & 0x1F);
408         port_index = pin_index_in_bank >> 3;
409         pin_in_port = (pin_index_in_bank & 0x7);
410         mask = BIT(pin_in_port);
411         if (enable)
412                 bank->wake_enb[port_index] |= mask;
413         else
414                 bank->wake_enb[port_index] &= ~mask;
415
416         /* Enable GPIO interrupt in Lp0 when GPIO is a Lp0 wake up source */
417         if (wake >= 0)
418                 bank->wake_lp0_cap[port_index] |= mask;
419 #endif
420
421         return 0;
422 }
423
424 static int tegra_gpio_irq_set_wake(struct irq_data *d, unsigned int enable)
425 {
426         struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
427         int ret = 0;
428         int wake = tegra_gpio_to_wake(d->hwirq);
429
430         /*
431          * update LP1 mask for gpio port/pin interrupt
432          * LP1 enable independent of LP0 wake support
433          */
434         ret = tegra_update_lp1_gpio_wake(d, enable, wake);
435         if (ret) {
436                 pr_err("Failed gpio lp1 %s for irq=%d, error=%d\n",
437                         (enable ? "enable" : "disable"), d->irq, ret);
438                 goto fail;
439         }
440
441         /* LP1 enable for bank interrupt */
442         if (enable) {
443                 if (bank->wake_depth++ == 0) {
444                         ret = tegra_update_lp1_irq_wake(bank->irq, enable);
445                         if (ret)
446                                 bank->wake_depth = 0;
447                 }
448         } else {
449                 if (bank->wake_depth == 0) {
450                         WARN(1, "Unbalanced IRQ %d wake disable\n", bank->irq);
451                 } else if (--bank->wake_depth == 0) {
452                         ret = tegra_update_lp1_irq_wake(bank->irq, enable);
453                         if (ret)
454                                 bank->wake_depth = 1;
455                 }
456         }
457         if (ret)
458                 pr_err("Failed gpio lp1 %s for irq=%d, error=%d\n",
459                         (enable ? "enable" : "disable"), bank->irq, ret);
460
461         if (wake < 0)
462                 pr_err("Warning: enabling a non-LP0 wake source %lu\n",
463                         d->hwirq);
464         else {
465                 ret = tegra_pm_irq_set_wake(wake, enable);
466                 if (ret)
467                         pr_err("Failed gpio lp0 %s for irq=%d, error=%d\n",
468                                 (enable ? "enable" : "disable"), d->irq, ret);
469         }
470
471 fail:
472         return ret;
473 }
474 #else
475 #define tegra_gpio_irq_set_wake NULL
476 #define tegra_update_lp1_gpio_wake NULL
477 #endif
478
479 static struct syscore_ops tegra_gpio_syscore_ops = {
480         .suspend = tegra_gpio_suspend,
481         .resume = tegra_gpio_resume,
482         .save = tegra_gpio_suspend,
483         .restore = tegra_gpio_resume,
484 };
485
486 static struct irq_chip tegra_gpio_irq_chip = {
487         .name           = "GPIO",
488         .irq_ack        = tegra_gpio_irq_ack,
489         .irq_mask       = tegra_gpio_irq_mask,
490         .irq_unmask     = tegra_gpio_irq_unmask,
491         .irq_set_type   = tegra_gpio_irq_set_type,
492         .irq_set_wake   = tegra_gpio_irq_set_wake,
493         .flags          = IRQCHIP_MASK_ON_SUSPEND,
494 };
495
496 struct tegra_gpio_soc_config {
497         u32 bank_stride;
498         u32 upper_offset;
499 };
500
501 static struct tegra_gpio_soc_config tegra20_gpio_config = {
502         .bank_stride = 0x80,
503         .upper_offset = 0x800,
504 };
505
506 static struct tegra_gpio_soc_config tegra30_gpio_config = {
507         .bank_stride = 0x100,
508         .upper_offset = 0x80,
509 };
510
511 static struct of_device_id tegra_gpio_of_match[] = {
512         { .compatible = "nvidia,tegra124-gpio", .data = &tegra30_gpio_config },
513         { .compatible = "nvidia,tegra148-gpio", .data = &tegra30_gpio_config },
514         { .compatible = "nvidia,tegra114-gpio", .data = &tegra30_gpio_config },
515         { .compatible = "nvidia,tegra30-gpio", .data = &tegra30_gpio_config },
516         { .compatible = "nvidia,tegra20-gpio", .data = &tegra20_gpio_config },
517         { },
518 };
519
520 /* This lock class tells lockdep that GPIO irqs are in a different
521  * category than their parents, so it won't report false recursion.
522  */
523 static struct lock_class_key gpio_lock_class;
524
525 static int tegra_gpio_probe(struct platform_device *pdev)
526 {
527         const struct of_device_id *match;
528         struct tegra_gpio_soc_config *config;
529         struct resource *res;
530         struct tegra_gpio_bank *bank;
531         int gpio;
532         int i;
533         int j;
534
535         match = of_match_device(tegra_gpio_of_match, &pdev->dev);
536         if (!match) {
537                 dev_err(&pdev->dev, "Error: No device match found\n");
538                 return -ENODEV;
539         }
540         config = (struct tegra_gpio_soc_config *)match->data;
541
542         tegra_gpio_bank_stride = config->bank_stride;
543         tegra_gpio_upper_offset = config->upper_offset;
544
545         for (;;) {
546                 res = platform_get_resource(pdev, IORESOURCE_IRQ, tegra_gpio_bank_count);
547                 if (!res)
548                         break;
549                 tegra_gpio_bank_count++;
550         }
551         if (!tegra_gpio_bank_count) {
552                 dev_err(&pdev->dev, "Missing IRQ resource\n");
553                 return -ENODEV;
554         }
555
556         tegra_gpio_chip.dev = &pdev->dev;
557         tegra_gpio_chip.ngpio = tegra_gpio_bank_count * 32;
558
559         tegra_gpio_banks = devm_kzalloc(&pdev->dev,
560                         tegra_gpio_bank_count * sizeof(*tegra_gpio_banks),
561                         GFP_KERNEL);
562         if (!tegra_gpio_banks) {
563                 dev_err(&pdev->dev, "Couldn't allocate bank structure\n");
564                 return -ENODEV;
565         }
566
567         irq_domain = irq_domain_add_linear(pdev->dev.of_node,
568                                            tegra_gpio_chip.ngpio,
569                                            &irq_domain_simple_ops, NULL);
570         if (!irq_domain)
571                 return -ENODEV;
572
573         for (i = 0; i < tegra_gpio_bank_count; i++) {
574                 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
575                 if (!res) {
576                         dev_err(&pdev->dev, "Missing IRQ resource\n");
577                         return -ENODEV;
578                 }
579
580                 bank = &tegra_gpio_banks[i];
581                 bank->bank = i;
582                 bank->irq = res->start;
583         }
584
585         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
586         if (!res) {
587                 dev_err(&pdev->dev, "Missing MEM resource\n");
588                 return -ENODEV;
589         }
590
591         regs = devm_ioremap_resource(&pdev->dev, res);
592         if (IS_ERR(regs))
593                 return PTR_ERR(regs);
594
595         for (i = 0; i < tegra_gpio_bank_count; i++) {
596                 for (j = 0; j < 4; j++) {
597                         int gpio = tegra_gpio_compose(i, j, 0);
598                         tegra_gpio_writel(0x00, GPIO_INT_ENB(gpio));
599                         tegra_gpio_writel(0x00, GPIO_INT_STA(gpio));
600                 }
601         }
602
603         tegra_gpio_chip.of_node = pdev->dev.of_node;
604
605         gpiochip_add(&tegra_gpio_chip);
606
607         for (gpio = 0; gpio < tegra_gpio_chip.ngpio; gpio++) {
608                 int irq = irq_create_mapping(irq_domain, gpio);
609                 /* No validity check; all Tegra GPIOs are valid IRQs */
610
611                 bank = &tegra_gpio_banks[GPIO_BANK(gpio)];
612
613                 irq_set_lockdep_class(irq, &gpio_lock_class);
614                 irq_set_chip_data(irq, bank);
615                 irq_set_chip_and_handler(irq, &tegra_gpio_irq_chip,
616                                          handle_simple_irq);
617                 set_irq_flags(irq, IRQF_VALID);
618         }
619
620         for (i = 0; i < tegra_gpio_bank_count; i++) {
621                 bank = &tegra_gpio_banks[i];
622
623                 for (j = 0; j < 4; j++)
624                         spin_lock_init(&bank->lvl_lock[j]);
625
626                 irq_set_handler_data(bank->irq, bank);
627                 irq_set_chained_handler(bank->irq, tegra_gpio_irq_handler);
628
629         }
630
631         return 0;
632 }
633
634 static struct platform_driver tegra_gpio_driver = {
635         .driver         = {
636                 .name   = "tegra-gpio",
637                 .owner  = THIS_MODULE,
638                 .of_match_table = tegra_gpio_of_match,
639         },
640         .probe          = tegra_gpio_probe,
641 };
642
643 static int __init tegra_gpio_init(void)
644 {
645         register_syscore_ops(&tegra_gpio_syscore_ops);
646         return platform_driver_register(&tegra_gpio_driver);
647 }
648 postcore_initcall(tegra_gpio_init);
649
650 #ifdef  CONFIG_DEBUG_FS
651
652 #include <linux/debugfs.h>
653 #include <linux/seq_file.h>
654
655 static int dbg_gpio_show(struct seq_file *s, void *unused)
656 {
657         int i;
658         int j;
659         char x,y;
660
661         x = ' ';
662         y = 'A';
663
664         seq_printf(s, "Name:Bank:Port CNF OE OUT IN INT_STA INT_ENB INT_LVL\n");
665         for (i = 0; i < tegra_gpio_bank_count; i++) {
666                 for (j = 0; j < 4; j++) {
667                         int gpio = tegra_gpio_compose(i, j, 0);
668                         seq_printf(s,
669                                 "%c%c: %d:%d %02x %02x %02x %02x %02x %02x %06x\n",
670                                 x, y, i, j,
671                                 tegra_gpio_readl(GPIO_CNF(gpio)),
672                                 tegra_gpio_readl(GPIO_OE(gpio)),
673                                 tegra_gpio_readl(GPIO_OUT(gpio)),
674                                 tegra_gpio_readl(GPIO_IN(gpio)),
675                                 tegra_gpio_readl(GPIO_INT_STA(gpio)),
676                                 tegra_gpio_readl(GPIO_INT_ENB(gpio)),
677                                 tegra_gpio_readl(GPIO_INT_LVL(gpio)));
678
679                         if (x != ' ')
680                                 x++;
681                         if (y == 'Z') {
682                                 y = 'A';
683                                 x = 'A';
684                         } else {
685                                 y++;
686                         };
687                 }
688         }
689         return 0;
690 }
691
692 static int dbg_gpio_open(struct inode *inode, struct file *file)
693 {
694         return single_open(file, dbg_gpio_show, &inode->i_private);
695 }
696
697 static const struct file_operations debug_fops = {
698         .open           = dbg_gpio_open,
699         .read           = seq_read,
700         .llseek         = seq_lseek,
701         .release        = single_release,
702 };
703
704 static int __init tegra_gpio_debuginit(void)
705 {
706         (void) debugfs_create_file("tegra_gpio", S_IRUGO,
707                                         NULL, NULL, &debug_fops);
708         return 0;
709 }
710 late_initcall(tegra_gpio_debuginit);
711 #endif