Merge branch 'tegra/devel' into next/devel
[pandora-kernel.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  * 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>
22 #include <linux/interrupt.h>
23 #include <linux/io.h>
24 #include <linux/gpio.h>
25 #include <linux/of.h>
26 #include <linux/platform_device.h>
27 #include <linux/module.h>
28
29 #include <asm/mach/irq.h>
30
31 #include <mach/iomap.h>
32 #include <mach/suspend.h>
33
34 #define GPIO_BANK(x)            ((x) >> 5)
35 #define GPIO_PORT(x)            (((x) >> 3) & 0x3)
36 #define GPIO_BIT(x)             ((x) & 0x7)
37
38 #define GPIO_REG(x)             (GPIO_BANK(x) * 0x80 + GPIO_PORT(x) * 4)
39
40 #define GPIO_CNF(x)             (GPIO_REG(x) + 0x00)
41 #define GPIO_OE(x)              (GPIO_REG(x) + 0x10)
42 #define GPIO_OUT(x)             (GPIO_REG(x) + 0X20)
43 #define GPIO_IN(x)              (GPIO_REG(x) + 0x30)
44 #define GPIO_INT_STA(x)         (GPIO_REG(x) + 0x40)
45 #define GPIO_INT_ENB(x)         (GPIO_REG(x) + 0x50)
46 #define GPIO_INT_LVL(x)         (GPIO_REG(x) + 0x60)
47 #define GPIO_INT_CLR(x)         (GPIO_REG(x) + 0x70)
48
49 #define GPIO_MSK_CNF(x)         (GPIO_REG(x) + 0x800)
50 #define GPIO_MSK_OE(x)          (GPIO_REG(x) + 0x810)
51 #define GPIO_MSK_OUT(x)         (GPIO_REG(x) + 0X820)
52 #define GPIO_MSK_INT_STA(x)     (GPIO_REG(x) + 0x840)
53 #define GPIO_MSK_INT_ENB(x)     (GPIO_REG(x) + 0x850)
54 #define GPIO_MSK_INT_LVL(x)     (GPIO_REG(x) + 0x860)
55
56 #define GPIO_INT_LVL_MASK               0x010101
57 #define GPIO_INT_LVL_EDGE_RISING        0x000101
58 #define GPIO_INT_LVL_EDGE_FALLING       0x000100
59 #define GPIO_INT_LVL_EDGE_BOTH          0x010100
60 #define GPIO_INT_LVL_LEVEL_HIGH         0x000001
61 #define GPIO_INT_LVL_LEVEL_LOW          0x000000
62
63 struct tegra_gpio_bank {
64         int bank;
65         int irq;
66         spinlock_t lvl_lock[4];
67 #ifdef CONFIG_PM
68         u32 cnf[4];
69         u32 out[4];
70         u32 oe[4];
71         u32 int_enb[4];
72         u32 int_lvl[4];
73 #endif
74 };
75
76
77 static void __iomem *regs;
78 static struct tegra_gpio_bank tegra_gpio_banks[7];
79
80 static inline void tegra_gpio_writel(u32 val, u32 reg)
81 {
82         __raw_writel(val, regs + reg);
83 }
84
85 static inline u32 tegra_gpio_readl(u32 reg)
86 {
87         return __raw_readl(regs + reg);
88 }
89
90 static int tegra_gpio_compose(int bank, int port, int bit)
91 {
92         return (bank << 5) | ((port & 0x3) << 3) | (bit & 0x7);
93 }
94
95 static void tegra_gpio_mask_write(u32 reg, int gpio, int value)
96 {
97         u32 val;
98
99         val = 0x100 << GPIO_BIT(gpio);
100         if (value)
101                 val |= 1 << GPIO_BIT(gpio);
102         tegra_gpio_writel(val, reg);
103 }
104
105 void tegra_gpio_enable(int gpio)
106 {
107         tegra_gpio_mask_write(GPIO_MSK_CNF(gpio), gpio, 1);
108 }
109
110 void tegra_gpio_disable(int gpio)
111 {
112         tegra_gpio_mask_write(GPIO_MSK_CNF(gpio), gpio, 0);
113 }
114
115 static void tegra_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
116 {
117         tegra_gpio_mask_write(GPIO_MSK_OUT(offset), offset, value);
118 }
119
120 static int tegra_gpio_get(struct gpio_chip *chip, unsigned offset)
121 {
122         return (tegra_gpio_readl(GPIO_IN(offset)) >> GPIO_BIT(offset)) & 0x1;
123 }
124
125 static int tegra_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
126 {
127         tegra_gpio_mask_write(GPIO_MSK_OE(offset), offset, 0);
128         return 0;
129 }
130
131 static int tegra_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
132                                         int value)
133 {
134         tegra_gpio_set(chip, offset, value);
135         tegra_gpio_mask_write(GPIO_MSK_OE(offset), offset, 1);
136         return 0;
137 }
138
139
140
141 static struct gpio_chip tegra_gpio_chip = {
142         .label                  = "tegra-gpio",
143         .direction_input        = tegra_gpio_direction_input,
144         .get                    = tegra_gpio_get,
145         .direction_output       = tegra_gpio_direction_output,
146         .set                    = tegra_gpio_set,
147         .base                   = 0,
148         .ngpio                  = TEGRA_NR_GPIOS,
149 };
150
151 static void tegra_gpio_irq_ack(struct irq_data *d)
152 {
153         int gpio = d->irq - INT_GPIO_BASE;
154
155         tegra_gpio_writel(1 << GPIO_BIT(gpio), GPIO_INT_CLR(gpio));
156 }
157
158 static void tegra_gpio_irq_mask(struct irq_data *d)
159 {
160         int gpio = d->irq - INT_GPIO_BASE;
161
162         tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio), gpio, 0);
163 }
164
165 static void tegra_gpio_irq_unmask(struct irq_data *d)
166 {
167         int gpio = d->irq - INT_GPIO_BASE;
168
169         tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio), gpio, 1);
170 }
171
172 static int tegra_gpio_irq_set_type(struct irq_data *d, unsigned int type)
173 {
174         int gpio = d->irq - INT_GPIO_BASE;
175         struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
176         int port = GPIO_PORT(gpio);
177         int lvl_type;
178         int val;
179         unsigned long flags;
180
181         switch (type & IRQ_TYPE_SENSE_MASK) {
182         case IRQ_TYPE_EDGE_RISING:
183                 lvl_type = GPIO_INT_LVL_EDGE_RISING;
184                 break;
185
186         case IRQ_TYPE_EDGE_FALLING:
187                 lvl_type = GPIO_INT_LVL_EDGE_FALLING;
188                 break;
189
190         case IRQ_TYPE_EDGE_BOTH:
191                 lvl_type = GPIO_INT_LVL_EDGE_BOTH;
192                 break;
193
194         case IRQ_TYPE_LEVEL_HIGH:
195                 lvl_type = GPIO_INT_LVL_LEVEL_HIGH;
196                 break;
197
198         case IRQ_TYPE_LEVEL_LOW:
199                 lvl_type = GPIO_INT_LVL_LEVEL_LOW;
200                 break;
201
202         default:
203                 return -EINVAL;
204         }
205
206         spin_lock_irqsave(&bank->lvl_lock[port], flags);
207
208         val = tegra_gpio_readl(GPIO_INT_LVL(gpio));
209         val &= ~(GPIO_INT_LVL_MASK << GPIO_BIT(gpio));
210         val |= lvl_type << GPIO_BIT(gpio);
211         tegra_gpio_writel(val, GPIO_INT_LVL(gpio));
212
213         spin_unlock_irqrestore(&bank->lvl_lock[port], flags);
214
215         if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
216                 __irq_set_handler_locked(d->irq, handle_level_irq);
217         else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
218                 __irq_set_handler_locked(d->irq, handle_edge_irq);
219
220         return 0;
221 }
222
223 static void tegra_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
224 {
225         struct tegra_gpio_bank *bank;
226         int port;
227         int pin;
228         int unmasked = 0;
229         struct irq_chip *chip = irq_desc_get_chip(desc);
230
231         chained_irq_enter(chip, desc);
232
233         bank = irq_get_handler_data(irq);
234
235         for (port = 0; port < 4; port++) {
236                 int gpio = tegra_gpio_compose(bank->bank, port, 0);
237                 unsigned long sta = tegra_gpio_readl(GPIO_INT_STA(gpio)) &
238                         tegra_gpio_readl(GPIO_INT_ENB(gpio));
239                 u32 lvl = tegra_gpio_readl(GPIO_INT_LVL(gpio));
240
241                 for_each_set_bit(pin, &sta, 8) {
242                         tegra_gpio_writel(1 << pin, GPIO_INT_CLR(gpio));
243
244                         /* if gpio is edge triggered, clear condition
245                          * before executing the hander so that we don't
246                          * miss edges
247                          */
248                         if (lvl & (0x100 << pin)) {
249                                 unmasked = 1;
250                                 chained_irq_exit(chip, desc);
251                         }
252
253                         generic_handle_irq(gpio_to_irq(gpio + pin));
254                 }
255         }
256
257         if (!unmasked)
258                 chained_irq_exit(chip, desc);
259
260 }
261
262 #ifdef CONFIG_PM
263 void tegra_gpio_resume(void)
264 {
265         unsigned long flags;
266         int b;
267         int p;
268
269         local_irq_save(flags);
270
271         for (b = 0; b < ARRAY_SIZE(tegra_gpio_banks); b++) {
272                 struct tegra_gpio_bank *bank = &tegra_gpio_banks[b];
273
274                 for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
275                         unsigned int gpio = (b<<5) | (p<<3);
276                         tegra_gpio_writel(bank->cnf[p], GPIO_CNF(gpio));
277                         tegra_gpio_writel(bank->out[p], GPIO_OUT(gpio));
278                         tegra_gpio_writel(bank->oe[p], GPIO_OE(gpio));
279                         tegra_gpio_writel(bank->int_lvl[p], GPIO_INT_LVL(gpio));
280                         tegra_gpio_writel(bank->int_enb[p], GPIO_INT_ENB(gpio));
281                 }
282         }
283
284         local_irq_restore(flags);
285 }
286
287 void tegra_gpio_suspend(void)
288 {
289         unsigned long flags;
290         int b;
291         int p;
292
293         local_irq_save(flags);
294         for (b = 0; b < ARRAY_SIZE(tegra_gpio_banks); b++) {
295                 struct tegra_gpio_bank *bank = &tegra_gpio_banks[b];
296
297                 for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
298                         unsigned int gpio = (b<<5) | (p<<3);
299                         bank->cnf[p] = tegra_gpio_readl(GPIO_CNF(gpio));
300                         bank->out[p] = tegra_gpio_readl(GPIO_OUT(gpio));
301                         bank->oe[p] = tegra_gpio_readl(GPIO_OE(gpio));
302                         bank->int_enb[p] = tegra_gpio_readl(GPIO_INT_ENB(gpio));
303                         bank->int_lvl[p] = tegra_gpio_readl(GPIO_INT_LVL(gpio));
304                 }
305         }
306         local_irq_restore(flags);
307 }
308
309 static int tegra_gpio_wake_enable(struct irq_data *d, unsigned int enable)
310 {
311         struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
312         return irq_set_irq_wake(bank->irq, enable);
313 }
314 #endif
315
316 static struct irq_chip tegra_gpio_irq_chip = {
317         .name           = "GPIO",
318         .irq_ack        = tegra_gpio_irq_ack,
319         .irq_mask       = tegra_gpio_irq_mask,
320         .irq_unmask     = tegra_gpio_irq_unmask,
321         .irq_set_type   = tegra_gpio_irq_set_type,
322 #ifdef CONFIG_PM
323         .irq_set_wake   = tegra_gpio_wake_enable,
324 #endif
325 };
326
327
328 /* This lock class tells lockdep that GPIO irqs are in a different
329  * category than their parents, so it won't report false recursion.
330  */
331 static struct lock_class_key gpio_lock_class;
332
333 static int __devinit tegra_gpio_probe(struct platform_device *pdev)
334 {
335         struct resource *res;
336         struct tegra_gpio_bank *bank;
337         int i;
338         int j;
339
340         for (i = 0; i < ARRAY_SIZE(tegra_gpio_banks); i++) {
341                 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
342                 if (!res) {
343                         dev_err(&pdev->dev, "Missing IRQ resource\n");
344                         return -ENODEV;
345                 }
346
347                 bank = &tegra_gpio_banks[i];
348                 bank->bank = i;
349                 bank->irq = res->start;
350         }
351
352         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
353         if (!res) {
354                 dev_err(&pdev->dev, "Missing MEM resource\n");
355                 return -ENODEV;
356         }
357
358         if (!devm_request_mem_region(&pdev->dev, res->start,
359                                      resource_size(res),
360                                      dev_name(&pdev->dev))) {
361                 dev_err(&pdev->dev, "Couldn't request MEM resource\n");
362                 return -ENODEV;
363         }
364
365         regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
366         if (!regs) {
367                 dev_err(&pdev->dev, "Couldn't ioremap regs\n");
368                 return -ENODEV;
369         }
370
371         for (i = 0; i < 7; i++) {
372                 for (j = 0; j < 4; j++) {
373                         int gpio = tegra_gpio_compose(i, j, 0);
374                         tegra_gpio_writel(0x00, GPIO_INT_ENB(gpio));
375                 }
376         }
377
378 #ifdef CONFIG_OF_GPIO
379         tegra_gpio_chip.of_node = pdev->dev.of_node;
380 #endif
381
382         gpiochip_add(&tegra_gpio_chip);
383
384         for (i = INT_GPIO_BASE; i < (INT_GPIO_BASE + TEGRA_NR_GPIOS); i++) {
385                 bank = &tegra_gpio_banks[GPIO_BANK(irq_to_gpio(i))];
386
387                 irq_set_lockdep_class(i, &gpio_lock_class);
388                 irq_set_chip_data(i, bank);
389                 irq_set_chip_and_handler(i, &tegra_gpio_irq_chip,
390                                          handle_simple_irq);
391                 set_irq_flags(i, IRQF_VALID);
392         }
393
394         for (i = 0; i < ARRAY_SIZE(tegra_gpio_banks); i++) {
395                 bank = &tegra_gpio_banks[i];
396
397                 irq_set_chained_handler(bank->irq, tegra_gpio_irq_handler);
398                 irq_set_handler_data(bank->irq, bank);
399
400                 for (j = 0; j < 4; j++)
401                         spin_lock_init(&bank->lvl_lock[j]);
402         }
403
404         return 0;
405 }
406
407 static struct of_device_id tegra_gpio_of_match[] __devinitdata = {
408         { .compatible = "nvidia,tegra20-gpio", },
409         { },
410 };
411
412 static struct platform_driver tegra_gpio_driver = {
413         .driver         = {
414                 .name   = "tegra-gpio",
415                 .owner  = THIS_MODULE,
416                 .of_match_table = tegra_gpio_of_match,
417         },
418         .probe          = tegra_gpio_probe,
419 };
420
421 static int __init tegra_gpio_init(void)
422 {
423         return platform_driver_register(&tegra_gpio_driver);
424 }
425 postcore_initcall(tegra_gpio_init);
426
427 void __init tegra_gpio_config(struct tegra_gpio_table *table, int num)
428 {
429         int i;
430
431         for (i = 0; i < num; i++) {
432                 int gpio = table[i].gpio;
433
434                 if (table[i].enable)
435                         tegra_gpio_enable(gpio);
436                 else
437                         tegra_gpio_disable(gpio);
438         }
439 }
440
441 #ifdef  CONFIG_DEBUG_FS
442
443 #include <linux/debugfs.h>
444 #include <linux/seq_file.h>
445
446 static int dbg_gpio_show(struct seq_file *s, void *unused)
447 {
448         int i;
449         int j;
450
451         for (i = 0; i < 7; i++) {
452                 for (j = 0; j < 4; j++) {
453                         int gpio = tegra_gpio_compose(i, j, 0);
454                         seq_printf(s,
455                                 "%d:%d %02x %02x %02x %02x %02x %02x %06x\n",
456                                 i, j,
457                                 tegra_gpio_readl(GPIO_CNF(gpio)),
458                                 tegra_gpio_readl(GPIO_OE(gpio)),
459                                 tegra_gpio_readl(GPIO_OUT(gpio)),
460                                 tegra_gpio_readl(GPIO_IN(gpio)),
461                                 tegra_gpio_readl(GPIO_INT_STA(gpio)),
462                                 tegra_gpio_readl(GPIO_INT_ENB(gpio)),
463                                 tegra_gpio_readl(GPIO_INT_LVL(gpio)));
464                 }
465         }
466         return 0;
467 }
468
469 static int dbg_gpio_open(struct inode *inode, struct file *file)
470 {
471         return single_open(file, dbg_gpio_show, &inode->i_private);
472 }
473
474 static const struct file_operations debug_fops = {
475         .open           = dbg_gpio_open,
476         .read           = seq_read,
477         .llseek         = seq_lseek,
478         .release        = single_release,
479 };
480
481 static int __init tegra_gpio_debuginit(void)
482 {
483         (void) debugfs_create_file("tegra_gpio", S_IRUGO,
484                                         NULL, NULL, &debug_fops);
485         return 0;
486 }
487 late_initcall(tegra_gpio_debuginit);
488 #endif