perf intel-pt: Factor out intel_pt_8b_tsc()
[linux/fpc-iii.git] / drivers / gpio / gpio-tegra.c
blob6d9b6906b9d01b6570866e8ee4ab12da3efbc115
1 /*
2 * arch/arm/mach-tegra/gpio.c
4 * Copyright (c) 2010 Google, Inc
5 * Copyright (c) 2011-2016, NVIDIA CORPORATION. All rights reserved.
7 * Author:
8 * Erik Gilling <konkers@google.com>
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 #include <linux/err.h>
22 #include <linux/init.h>
23 #include <linux/irq.h>
24 #include <linux/interrupt.h>
25 #include <linux/io.h>
26 #include <linux/gpio/driver.h>
27 #include <linux/of_device.h>
28 #include <linux/platform_device.h>
29 #include <linux/module.h>
30 #include <linux/irqdomain.h>
31 #include <linux/irqchip/chained_irq.h>
32 #include <linux/pinctrl/consumer.h>
33 #include <linux/pm.h>
35 #define GPIO_BANK(x) ((x) >> 5)
36 #define GPIO_PORT(x) (((x) >> 3) & 0x3)
37 #define GPIO_BIT(x) ((x) & 0x7)
39 #define GPIO_REG(tgi, x) (GPIO_BANK(x) * tgi->soc->bank_stride + \
40 GPIO_PORT(x) * 4)
42 #define GPIO_CNF(t, x) (GPIO_REG(t, x) + 0x00)
43 #define GPIO_OE(t, x) (GPIO_REG(t, x) + 0x10)
44 #define GPIO_OUT(t, x) (GPIO_REG(t, x) + 0X20)
45 #define GPIO_IN(t, x) (GPIO_REG(t, x) + 0x30)
46 #define GPIO_INT_STA(t, x) (GPIO_REG(t, x) + 0x40)
47 #define GPIO_INT_ENB(t, x) (GPIO_REG(t, x) + 0x50)
48 #define GPIO_INT_LVL(t, x) (GPIO_REG(t, x) + 0x60)
49 #define GPIO_INT_CLR(t, x) (GPIO_REG(t, x) + 0x70)
50 #define GPIO_DBC_CNT(t, x) (GPIO_REG(t, x) + 0xF0)
53 #define GPIO_MSK_CNF(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x00)
54 #define GPIO_MSK_OE(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x10)
55 #define GPIO_MSK_OUT(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0X20)
56 #define GPIO_MSK_DBC_EN(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x30)
57 #define GPIO_MSK_INT_STA(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x40)
58 #define GPIO_MSK_INT_ENB(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x50)
59 #define GPIO_MSK_INT_LVL(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x60)
61 #define GPIO_INT_LVL_MASK 0x010101
62 #define GPIO_INT_LVL_EDGE_RISING 0x000101
63 #define GPIO_INT_LVL_EDGE_FALLING 0x000100
64 #define GPIO_INT_LVL_EDGE_BOTH 0x010100
65 #define GPIO_INT_LVL_LEVEL_HIGH 0x000001
66 #define GPIO_INT_LVL_LEVEL_LOW 0x000000
68 struct tegra_gpio_info;
70 struct tegra_gpio_bank {
71 unsigned int bank;
72 unsigned int irq;
73 spinlock_t lvl_lock[4];
74 spinlock_t dbc_lock[4]; /* Lock for updating debounce count register */
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 u32 dbc_enb[4];
83 #endif
84 u32 dbc_cnt[4];
85 struct tegra_gpio_info *tgi;
88 struct tegra_gpio_soc_config {
89 bool debounce_supported;
90 u32 bank_stride;
91 u32 upper_offset;
94 struct tegra_gpio_info {
95 struct device *dev;
96 void __iomem *regs;
97 struct irq_domain *irq_domain;
98 struct tegra_gpio_bank *bank_info;
99 const struct tegra_gpio_soc_config *soc;
100 struct gpio_chip gc;
101 struct irq_chip ic;
102 u32 bank_count;
105 static inline void tegra_gpio_writel(struct tegra_gpio_info *tgi,
106 u32 val, u32 reg)
108 __raw_writel(val, tgi->regs + reg);
111 static inline u32 tegra_gpio_readl(struct tegra_gpio_info *tgi, u32 reg)
113 return __raw_readl(tgi->regs + reg);
116 static unsigned int tegra_gpio_compose(unsigned int bank, unsigned int port,
117 unsigned int bit)
119 return (bank << 5) | ((port & 0x3) << 3) | (bit & 0x7);
122 static void tegra_gpio_mask_write(struct tegra_gpio_info *tgi, u32 reg,
123 unsigned int gpio, u32 value)
125 u32 val;
127 val = 0x100 << GPIO_BIT(gpio);
128 if (value)
129 val |= 1 << GPIO_BIT(gpio);
130 tegra_gpio_writel(tgi, val, reg);
133 static void tegra_gpio_enable(struct tegra_gpio_info *tgi, unsigned int gpio)
135 tegra_gpio_mask_write(tgi, GPIO_MSK_CNF(tgi, gpio), gpio, 1);
138 static void tegra_gpio_disable(struct tegra_gpio_info *tgi, unsigned int gpio)
140 tegra_gpio_mask_write(tgi, GPIO_MSK_CNF(tgi, gpio), gpio, 0);
143 static int tegra_gpio_request(struct gpio_chip *chip, unsigned int offset)
145 return pinctrl_gpio_request(chip->base + offset);
148 static void tegra_gpio_free(struct gpio_chip *chip, unsigned int offset)
150 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
152 pinctrl_gpio_free(chip->base + offset);
153 tegra_gpio_disable(tgi, offset);
156 static void tegra_gpio_set(struct gpio_chip *chip, unsigned int offset,
157 int value)
159 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
161 tegra_gpio_mask_write(tgi, GPIO_MSK_OUT(tgi, offset), offset, value);
164 static int tegra_gpio_get(struct gpio_chip *chip, unsigned int offset)
166 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
167 unsigned int bval = BIT(GPIO_BIT(offset));
169 /* If gpio is in output mode then read from the out value */
170 if (tegra_gpio_readl(tgi, GPIO_OE(tgi, offset)) & bval)
171 return !!(tegra_gpio_readl(tgi, GPIO_OUT(tgi, offset)) & bval);
173 return !!(tegra_gpio_readl(tgi, GPIO_IN(tgi, offset)) & bval);
176 static int tegra_gpio_direction_input(struct gpio_chip *chip,
177 unsigned int offset)
179 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
180 int ret;
182 tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, offset), offset, 0);
183 tegra_gpio_enable(tgi, offset);
185 ret = pinctrl_gpio_direction_input(chip->base + offset);
186 if (ret < 0)
187 dev_err(tgi->dev,
188 "Failed to set pinctrl input direction of GPIO %d: %d",
189 chip->base + offset, ret);
191 return ret;
194 static int tegra_gpio_direction_output(struct gpio_chip *chip,
195 unsigned int offset,
196 int value)
198 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
199 int ret;
201 tegra_gpio_set(chip, offset, value);
202 tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, offset), offset, 1);
203 tegra_gpio_enable(tgi, offset);
205 ret = pinctrl_gpio_direction_output(chip->base + offset);
206 if (ret < 0)
207 dev_err(tgi->dev,
208 "Failed to set pinctrl output direction of GPIO %d: %d",
209 chip->base + offset, ret);
211 return ret;
214 static int tegra_gpio_get_direction(struct gpio_chip *chip,
215 unsigned int offset)
217 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
218 u32 pin_mask = BIT(GPIO_BIT(offset));
219 u32 cnf, oe;
221 cnf = tegra_gpio_readl(tgi, GPIO_CNF(tgi, offset));
222 if (!(cnf & pin_mask))
223 return -EINVAL;
225 oe = tegra_gpio_readl(tgi, GPIO_OE(tgi, offset));
227 return !(oe & pin_mask);
230 static int tegra_gpio_set_debounce(struct gpio_chip *chip, unsigned int offset,
231 unsigned int debounce)
233 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
234 struct tegra_gpio_bank *bank = &tgi->bank_info[GPIO_BANK(offset)];
235 unsigned int debounce_ms = DIV_ROUND_UP(debounce, 1000);
236 unsigned long flags;
237 unsigned int port;
239 if (!debounce_ms) {
240 tegra_gpio_mask_write(tgi, GPIO_MSK_DBC_EN(tgi, offset),
241 offset, 0);
242 return 0;
245 debounce_ms = min(debounce_ms, 255U);
246 port = GPIO_PORT(offset);
248 /* There is only one debounce count register per port and hence
249 * set the maximum of current and requested debounce time.
251 spin_lock_irqsave(&bank->dbc_lock[port], flags);
252 if (bank->dbc_cnt[port] < debounce_ms) {
253 tegra_gpio_writel(tgi, debounce_ms, GPIO_DBC_CNT(tgi, offset));
254 bank->dbc_cnt[port] = debounce_ms;
256 spin_unlock_irqrestore(&bank->dbc_lock[port], flags);
258 tegra_gpio_mask_write(tgi, GPIO_MSK_DBC_EN(tgi, offset), offset, 1);
260 return 0;
263 static int tegra_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
264 unsigned long config)
266 u32 debounce;
268 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
269 return -ENOTSUPP;
271 debounce = pinconf_to_config_argument(config);
272 return tegra_gpio_set_debounce(chip, offset, debounce);
275 static int tegra_gpio_to_irq(struct gpio_chip *chip, unsigned int offset)
277 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
279 return irq_find_mapping(tgi->irq_domain, offset);
282 static void tegra_gpio_irq_ack(struct irq_data *d)
284 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
285 struct tegra_gpio_info *tgi = bank->tgi;
286 unsigned int gpio = d->hwirq;
288 tegra_gpio_writel(tgi, 1 << GPIO_BIT(gpio), GPIO_INT_CLR(tgi, gpio));
291 static void tegra_gpio_irq_mask(struct irq_data *d)
293 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
294 struct tegra_gpio_info *tgi = bank->tgi;
295 unsigned int gpio = d->hwirq;
297 tegra_gpio_mask_write(tgi, GPIO_MSK_INT_ENB(tgi, gpio), gpio, 0);
300 static void tegra_gpio_irq_unmask(struct irq_data *d)
302 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
303 struct tegra_gpio_info *tgi = bank->tgi;
304 unsigned int gpio = d->hwirq;
306 tegra_gpio_mask_write(tgi, GPIO_MSK_INT_ENB(tgi, gpio), gpio, 1);
309 static int tegra_gpio_irq_set_type(struct irq_data *d, unsigned int type)
311 unsigned int gpio = d->hwirq, port = GPIO_PORT(gpio), lvl_type;
312 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
313 struct tegra_gpio_info *tgi = bank->tgi;
314 unsigned long flags;
315 u32 val;
316 int ret;
318 switch (type & IRQ_TYPE_SENSE_MASK) {
319 case IRQ_TYPE_EDGE_RISING:
320 lvl_type = GPIO_INT_LVL_EDGE_RISING;
321 break;
323 case IRQ_TYPE_EDGE_FALLING:
324 lvl_type = GPIO_INT_LVL_EDGE_FALLING;
325 break;
327 case IRQ_TYPE_EDGE_BOTH:
328 lvl_type = GPIO_INT_LVL_EDGE_BOTH;
329 break;
331 case IRQ_TYPE_LEVEL_HIGH:
332 lvl_type = GPIO_INT_LVL_LEVEL_HIGH;
333 break;
335 case IRQ_TYPE_LEVEL_LOW:
336 lvl_type = GPIO_INT_LVL_LEVEL_LOW;
337 break;
339 default:
340 return -EINVAL;
343 spin_lock_irqsave(&bank->lvl_lock[port], flags);
345 val = tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio));
346 val &= ~(GPIO_INT_LVL_MASK << GPIO_BIT(gpio));
347 val |= lvl_type << GPIO_BIT(gpio);
348 tegra_gpio_writel(tgi, val, GPIO_INT_LVL(tgi, gpio));
350 spin_unlock_irqrestore(&bank->lvl_lock[port], flags);
352 tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, gpio), gpio, 0);
353 tegra_gpio_enable(tgi, gpio);
355 ret = gpiochip_lock_as_irq(&tgi->gc, gpio);
356 if (ret) {
357 dev_err(tgi->dev,
358 "unable to lock Tegra GPIO %u as IRQ\n", gpio);
359 tegra_gpio_disable(tgi, gpio);
360 return ret;
363 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
364 irq_set_handler_locked(d, handle_level_irq);
365 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
366 irq_set_handler_locked(d, handle_edge_irq);
368 return 0;
371 static void tegra_gpio_irq_shutdown(struct irq_data *d)
373 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
374 struct tegra_gpio_info *tgi = bank->tgi;
375 unsigned int gpio = d->hwirq;
377 gpiochip_unlock_as_irq(&tgi->gc, gpio);
380 static void tegra_gpio_irq_handler(struct irq_desc *desc)
382 unsigned int port, pin, gpio;
383 bool unmasked = false;
384 u32 lvl;
385 unsigned long sta;
386 struct irq_chip *chip = irq_desc_get_chip(desc);
387 struct tegra_gpio_bank *bank = irq_desc_get_handler_data(desc);
388 struct tegra_gpio_info *tgi = bank->tgi;
390 chained_irq_enter(chip, desc);
392 for (port = 0; port < 4; port++) {
393 gpio = tegra_gpio_compose(bank->bank, port, 0);
394 sta = tegra_gpio_readl(tgi, GPIO_INT_STA(tgi, gpio)) &
395 tegra_gpio_readl(tgi, GPIO_INT_ENB(tgi, gpio));
396 lvl = tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio));
398 for_each_set_bit(pin, &sta, 8) {
399 tegra_gpio_writel(tgi, 1 << pin,
400 GPIO_INT_CLR(tgi, gpio));
402 /* if gpio is edge triggered, clear condition
403 * before executing the handler so that we don't
404 * miss edges
406 if (!unmasked && lvl & (0x100 << pin)) {
407 unmasked = true;
408 chained_irq_exit(chip, desc);
411 generic_handle_irq(irq_find_mapping(tgi->irq_domain,
412 gpio + pin));
416 if (!unmasked)
417 chained_irq_exit(chip, desc);
421 #ifdef CONFIG_PM_SLEEP
422 static int tegra_gpio_resume(struct device *dev)
424 struct tegra_gpio_info *tgi = dev_get_drvdata(dev);
425 unsigned long flags;
426 unsigned int b, p;
428 local_irq_save(flags);
430 for (b = 0; b < tgi->bank_count; b++) {
431 struct tegra_gpio_bank *bank = &tgi->bank_info[b];
433 for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
434 unsigned int gpio = (b << 5) | (p << 3);
436 tegra_gpio_writel(tgi, bank->cnf[p],
437 GPIO_CNF(tgi, gpio));
439 if (tgi->soc->debounce_supported) {
440 tegra_gpio_writel(tgi, bank->dbc_cnt[p],
441 GPIO_DBC_CNT(tgi, gpio));
442 tegra_gpio_writel(tgi, bank->dbc_enb[p],
443 GPIO_MSK_DBC_EN(tgi, gpio));
446 tegra_gpio_writel(tgi, bank->out[p],
447 GPIO_OUT(tgi, gpio));
448 tegra_gpio_writel(tgi, bank->oe[p],
449 GPIO_OE(tgi, gpio));
450 tegra_gpio_writel(tgi, bank->int_lvl[p],
451 GPIO_INT_LVL(tgi, gpio));
452 tegra_gpio_writel(tgi, bank->int_enb[p],
453 GPIO_INT_ENB(tgi, gpio));
457 local_irq_restore(flags);
458 return 0;
461 static int tegra_gpio_suspend(struct device *dev)
463 struct tegra_gpio_info *tgi = dev_get_drvdata(dev);
464 unsigned long flags;
465 unsigned int b, p;
467 local_irq_save(flags);
468 for (b = 0; b < tgi->bank_count; b++) {
469 struct tegra_gpio_bank *bank = &tgi->bank_info[b];
471 for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
472 unsigned int gpio = (b << 5) | (p << 3);
474 bank->cnf[p] = tegra_gpio_readl(tgi,
475 GPIO_CNF(tgi, gpio));
476 bank->out[p] = tegra_gpio_readl(tgi,
477 GPIO_OUT(tgi, gpio));
478 bank->oe[p] = tegra_gpio_readl(tgi,
479 GPIO_OE(tgi, gpio));
480 if (tgi->soc->debounce_supported) {
481 bank->dbc_enb[p] = tegra_gpio_readl(tgi,
482 GPIO_MSK_DBC_EN(tgi, gpio));
483 bank->dbc_enb[p] = (bank->dbc_enb[p] << 8) |
484 bank->dbc_enb[p];
487 bank->int_enb[p] = tegra_gpio_readl(tgi,
488 GPIO_INT_ENB(tgi, gpio));
489 bank->int_lvl[p] = tegra_gpio_readl(tgi,
490 GPIO_INT_LVL(tgi, gpio));
492 /* Enable gpio irq for wake up source */
493 tegra_gpio_writel(tgi, bank->wake_enb[p],
494 GPIO_INT_ENB(tgi, gpio));
497 local_irq_restore(flags);
498 return 0;
501 static int tegra_gpio_irq_set_wake(struct irq_data *d, unsigned int enable)
503 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
504 unsigned int gpio = d->hwirq;
505 u32 port, bit, mask;
507 port = GPIO_PORT(gpio);
508 bit = GPIO_BIT(gpio);
509 mask = BIT(bit);
511 if (enable)
512 bank->wake_enb[port] |= mask;
513 else
514 bank->wake_enb[port] &= ~mask;
516 return irq_set_irq_wake(bank->irq, enable);
518 #endif
520 #ifdef CONFIG_DEBUG_FS
522 #include <linux/debugfs.h>
523 #include <linux/seq_file.h>
525 static int tegra_dbg_gpio_show(struct seq_file *s, void *unused)
527 struct tegra_gpio_info *tgi = s->private;
528 unsigned int i, j;
530 for (i = 0; i < tgi->bank_count; i++) {
531 for (j = 0; j < 4; j++) {
532 unsigned int gpio = tegra_gpio_compose(i, j, 0);
534 seq_printf(s,
535 "%u:%u %02x %02x %02x %02x %02x %02x %06x\n",
536 i, j,
537 tegra_gpio_readl(tgi, GPIO_CNF(tgi, gpio)),
538 tegra_gpio_readl(tgi, GPIO_OE(tgi, gpio)),
539 tegra_gpio_readl(tgi, GPIO_OUT(tgi, gpio)),
540 tegra_gpio_readl(tgi, GPIO_IN(tgi, gpio)),
541 tegra_gpio_readl(tgi, GPIO_INT_STA(tgi, gpio)),
542 tegra_gpio_readl(tgi, GPIO_INT_ENB(tgi, gpio)),
543 tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio)));
546 return 0;
549 DEFINE_SHOW_ATTRIBUTE(tegra_dbg_gpio);
551 static void tegra_gpio_debuginit(struct tegra_gpio_info *tgi)
553 (void) debugfs_create_file("tegra_gpio", 0444,
554 NULL, tgi, &tegra_dbg_gpio_fops);
557 #else
559 static inline void tegra_gpio_debuginit(struct tegra_gpio_info *tgi)
563 #endif
565 static const struct dev_pm_ops tegra_gpio_pm_ops = {
566 SET_SYSTEM_SLEEP_PM_OPS(tegra_gpio_suspend, tegra_gpio_resume)
569 static int tegra_gpio_probe(struct platform_device *pdev)
571 struct tegra_gpio_info *tgi;
572 struct tegra_gpio_bank *bank;
573 unsigned int gpio, i, j;
574 int ret;
576 tgi = devm_kzalloc(&pdev->dev, sizeof(*tgi), GFP_KERNEL);
577 if (!tgi)
578 return -ENODEV;
580 tgi->soc = of_device_get_match_data(&pdev->dev);
581 tgi->dev = &pdev->dev;
583 ret = platform_irq_count(pdev);
584 if (ret < 0)
585 return ret;
587 tgi->bank_count = ret;
589 if (!tgi->bank_count) {
590 dev_err(&pdev->dev, "Missing IRQ resource\n");
591 return -ENODEV;
594 tgi->gc.label = "tegra-gpio";
595 tgi->gc.request = tegra_gpio_request;
596 tgi->gc.free = tegra_gpio_free;
597 tgi->gc.direction_input = tegra_gpio_direction_input;
598 tgi->gc.get = tegra_gpio_get;
599 tgi->gc.direction_output = tegra_gpio_direction_output;
600 tgi->gc.set = tegra_gpio_set;
601 tgi->gc.get_direction = tegra_gpio_get_direction;
602 tgi->gc.to_irq = tegra_gpio_to_irq;
603 tgi->gc.base = 0;
604 tgi->gc.ngpio = tgi->bank_count * 32;
605 tgi->gc.parent = &pdev->dev;
606 tgi->gc.of_node = pdev->dev.of_node;
608 tgi->ic.name = "GPIO";
609 tgi->ic.irq_ack = tegra_gpio_irq_ack;
610 tgi->ic.irq_mask = tegra_gpio_irq_mask;
611 tgi->ic.irq_unmask = tegra_gpio_irq_unmask;
612 tgi->ic.irq_set_type = tegra_gpio_irq_set_type;
613 tgi->ic.irq_shutdown = tegra_gpio_irq_shutdown;
614 #ifdef CONFIG_PM_SLEEP
615 tgi->ic.irq_set_wake = tegra_gpio_irq_set_wake;
616 #endif
618 platform_set_drvdata(pdev, tgi);
620 if (tgi->soc->debounce_supported)
621 tgi->gc.set_config = tegra_gpio_set_config;
623 tgi->bank_info = devm_kcalloc(&pdev->dev, tgi->bank_count,
624 sizeof(*tgi->bank_info), GFP_KERNEL);
625 if (!tgi->bank_info)
626 return -ENOMEM;
628 tgi->irq_domain = irq_domain_add_linear(pdev->dev.of_node,
629 tgi->gc.ngpio,
630 &irq_domain_simple_ops, NULL);
631 if (!tgi->irq_domain)
632 return -ENODEV;
634 for (i = 0; i < tgi->bank_count; i++) {
635 ret = platform_get_irq(pdev, i);
636 if (ret < 0) {
637 dev_err(&pdev->dev, "Missing IRQ resource: %d\n", ret);
638 return ret;
641 bank = &tgi->bank_info[i];
642 bank->bank = i;
643 bank->irq = ret;
644 bank->tgi = tgi;
647 tgi->regs = devm_platform_ioremap_resource(pdev, 0);
648 if (IS_ERR(tgi->regs))
649 return PTR_ERR(tgi->regs);
651 for (i = 0; i < tgi->bank_count; i++) {
652 for (j = 0; j < 4; j++) {
653 int gpio = tegra_gpio_compose(i, j, 0);
655 tegra_gpio_writel(tgi, 0x00, GPIO_INT_ENB(tgi, gpio));
659 ret = devm_gpiochip_add_data(&pdev->dev, &tgi->gc, tgi);
660 if (ret < 0) {
661 irq_domain_remove(tgi->irq_domain);
662 return ret;
665 for (gpio = 0; gpio < tgi->gc.ngpio; gpio++) {
666 int irq = irq_create_mapping(tgi->irq_domain, gpio);
667 /* No validity check; all Tegra GPIOs are valid IRQs */
669 bank = &tgi->bank_info[GPIO_BANK(gpio)];
671 irq_set_chip_data(irq, bank);
672 irq_set_chip_and_handler(irq, &tgi->ic, handle_simple_irq);
675 for (i = 0; i < tgi->bank_count; i++) {
676 bank = &tgi->bank_info[i];
678 irq_set_chained_handler_and_data(bank->irq,
679 tegra_gpio_irq_handler, bank);
681 for (j = 0; j < 4; j++) {
682 spin_lock_init(&bank->lvl_lock[j]);
683 spin_lock_init(&bank->dbc_lock[j]);
687 tegra_gpio_debuginit(tgi);
689 return 0;
692 static const struct tegra_gpio_soc_config tegra20_gpio_config = {
693 .bank_stride = 0x80,
694 .upper_offset = 0x800,
697 static const struct tegra_gpio_soc_config tegra30_gpio_config = {
698 .bank_stride = 0x100,
699 .upper_offset = 0x80,
702 static const struct tegra_gpio_soc_config tegra210_gpio_config = {
703 .debounce_supported = true,
704 .bank_stride = 0x100,
705 .upper_offset = 0x80,
708 static const struct of_device_id tegra_gpio_of_match[] = {
709 { .compatible = "nvidia,tegra210-gpio", .data = &tegra210_gpio_config },
710 { .compatible = "nvidia,tegra30-gpio", .data = &tegra30_gpio_config },
711 { .compatible = "nvidia,tegra20-gpio", .data = &tegra20_gpio_config },
712 { },
715 static struct platform_driver tegra_gpio_driver = {
716 .driver = {
717 .name = "tegra-gpio",
718 .pm = &tegra_gpio_pm_ops,
719 .of_match_table = tegra_gpio_of_match,
721 .probe = tegra_gpio_probe,
724 static int __init tegra_gpio_init(void)
726 return platform_driver_register(&tegra_gpio_driver);
728 subsys_initcall(tegra_gpio_init);