Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 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 | |
Thierry Reding | 641d034 | 2013-01-21 11:09:01 +0100 | [diff] [blame] | 20 | #include <linux/err.h> |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 21 | #include <linux/init.h> |
| 22 | #include <linux/irq.h> |
Colin Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 23 | #include <linux/interrupt.h> |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 24 | #include <linux/io.h> |
| 25 | #include <linux/gpio.h> |
Stephen Warren | 5c1e2c9 | 2012-03-16 17:35:08 -0600 | [diff] [blame] | 26 | #include <linux/of_device.h> |
Stephen Warren | 88d8951 | 2011-10-11 16:16:14 -0600 | [diff] [blame] | 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/module.h> |
Stephen Warren | 6f74dc9 | 2012-01-04 08:39:37 +0000 | [diff] [blame] | 29 | #include <linux/irqdomain.h> |
Catalin Marinas | de88cbb | 2013-01-18 15:31:37 +0000 | [diff] [blame] | 30 | #include <linux/irqchip/chained_irq.h> |
Stephen Warren | 3e215d0 | 2012-02-18 01:04:55 -0700 | [diff] [blame] | 31 | #include <linux/pinctrl/consumer.h> |
Laxman Dewangan | 8939ddc | 2012-11-07 20:31:32 +0530 | [diff] [blame] | 32 | #include <linux/pm.h> |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 33 | |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 34 | #define GPIO_BANK(x) ((x) >> 5) |
| 35 | #define GPIO_PORT(x) (((x) >> 3) & 0x3) |
| 36 | #define GPIO_BIT(x) ((x) & 0x7) |
| 37 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 38 | #define GPIO_REG(tgi, x) (GPIO_BANK(x) * tgi->soc->bank_stride + \ |
Stephen Warren | 5c1e2c9 | 2012-03-16 17:35:08 -0600 | [diff] [blame] | 39 | GPIO_PORT(x) * 4) |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 40 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 41 | #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 Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 49 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 50 | #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 Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 56 | |
| 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 Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 64 | struct tegra_gpio_info; |
| 65 | |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 66 | struct tegra_gpio_bank { |
| 67 | int bank; |
| 68 | int irq; |
| 69 | spinlock_t lvl_lock[4]; |
Laxman Dewangan | 8939ddc | 2012-11-07 20:31:32 +0530 | [diff] [blame] | 70 | #ifdef CONFIG_PM_SLEEP |
Colin Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 71 | u32 cnf[4]; |
| 72 | u32 out[4]; |
| 73 | u32 oe[4]; |
| 74 | u32 int_enb[4]; |
| 75 | u32 int_lvl[4]; |
Joseph Lo | 203f31c | 2013-04-03 19:31:44 +0800 | [diff] [blame] | 76 | u32 wake_enb[4]; |
Colin Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 77 | #endif |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 78 | struct tegra_gpio_info *tgi; |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Laxman Dewangan | 171b92c | 2016-04-25 16:08:31 +0530 | [diff] [blame] | 81 | struct tegra_gpio_soc_config { |
| 82 | u32 bank_stride; |
| 83 | u32 upper_offset; |
| 84 | }; |
| 85 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 86 | struct 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 Warren | 88d8951 | 2011-10-11 16:16:14 -0600 | [diff] [blame] | 97 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 98 | static inline void tegra_gpio_writel(struct tegra_gpio_info *tgi, |
| 99 | u32 val, u32 reg) |
Stephen Warren | 88d8951 | 2011-10-11 16:16:14 -0600 | [diff] [blame] | 100 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 101 | __raw_writel(val, tgi->regs + reg); |
Stephen Warren | 88d8951 | 2011-10-11 16:16:14 -0600 | [diff] [blame] | 102 | } |
| 103 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 104 | static inline u32 tegra_gpio_readl(struct tegra_gpio_info *tgi, u32 reg) |
Stephen Warren | 88d8951 | 2011-10-11 16:16:14 -0600 | [diff] [blame] | 105 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 106 | return __raw_readl(tgi->regs + reg); |
Stephen Warren | 88d8951 | 2011-10-11 16:16:14 -0600 | [diff] [blame] | 107 | } |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 108 | |
| 109 | static int tegra_gpio_compose(int bank, int port, int bit) |
| 110 | { |
| 111 | return (bank << 5) | ((port & 0x3) << 3) | (bit & 0x7); |
| 112 | } |
| 113 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 114 | static void tegra_gpio_mask_write(struct tegra_gpio_info *tgi, u32 reg, |
| 115 | int gpio, int value) |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 116 | { |
| 117 | u32 val; |
| 118 | |
| 119 | val = 0x100 << GPIO_BIT(gpio); |
| 120 | if (value) |
| 121 | val |= 1 << GPIO_BIT(gpio); |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 122 | tegra_gpio_writel(tgi, val, reg); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 125 | static void tegra_gpio_enable(struct tegra_gpio_info *tgi, int gpio) |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 126 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 127 | tegra_gpio_mask_write(tgi, GPIO_MSK_CNF(tgi, gpio), gpio, 1); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 130 | static void tegra_gpio_disable(struct tegra_gpio_info *tgi, int gpio) |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 131 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 132 | tegra_gpio_mask_write(tgi, GPIO_MSK_CNF(tgi, gpio), gpio, 0); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Axel Lin | 924a098 | 2012-11-08 10:45:24 +0800 | [diff] [blame] | 135 | static int tegra_gpio_request(struct gpio_chip *chip, unsigned offset) |
Stephen Warren | 3e215d0 | 2012-02-18 01:04:55 -0700 | [diff] [blame] | 136 | { |
| 137 | return pinctrl_request_gpio(offset); |
| 138 | } |
| 139 | |
Axel Lin | 924a098 | 2012-11-08 10:45:24 +0800 | [diff] [blame] | 140 | static void tegra_gpio_free(struct gpio_chip *chip, unsigned offset) |
Stephen Warren | 3e215d0 | 2012-02-18 01:04:55 -0700 | [diff] [blame] | 141 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 142 | struct tegra_gpio_info *tgi = gpiochip_get_data(chip); |
| 143 | |
Stephen Warren | 3e215d0 | 2012-02-18 01:04:55 -0700 | [diff] [blame] | 144 | pinctrl_free_gpio(offset); |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 145 | tegra_gpio_disable(tgi, offset); |
Stephen Warren | 3e215d0 | 2012-02-18 01:04:55 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 148 | static void tegra_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 149 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 150 | struct tegra_gpio_info *tgi = gpiochip_get_data(chip); |
| 151 | |
| 152 | tegra_gpio_mask_write(tgi, GPIO_MSK_OUT(tgi, offset), offset, value); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static int tegra_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 156 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 157 | struct tegra_gpio_info *tgi = gpiochip_get_data(chip); |
| 158 | int bval = BIT(GPIO_BIT(offset)); |
Laxman Dewangan | 195812e | 2012-11-09 11:34:20 +0530 | [diff] [blame] | 159 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 160 | /* 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 Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static int tegra_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 168 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 169 | 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 Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int tegra_gpio_direction_output(struct gpio_chip *chip, unsigned offset, |
| 177 | int value) |
| 178 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 179 | struct tegra_gpio_info *tgi = gpiochip_get_data(chip); |
| 180 | |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 181 | tegra_gpio_set(chip, offset, value); |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 182 | tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, offset), offset, 1); |
| 183 | tegra_gpio_enable(tgi, offset); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
Stephen Warren | 438a99c | 2011-08-23 00:39:56 +0100 | [diff] [blame] | 187 | static int tegra_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 188 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 189 | struct tegra_gpio_info *tgi = gpiochip_get_data(chip); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 190 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 191 | return irq_find_mapping(tgi->irq_domain, offset); |
| 192 | } |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 193 | |
Lennert Buytenhek | 37337a8 | 2010-11-29 11:14:46 +0100 | [diff] [blame] | 194 | static void tegra_gpio_irq_ack(struct irq_data *d) |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 195 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 196 | struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d); |
| 197 | struct tegra_gpio_info *tgi = bank->tgi; |
Stephen Warren | 6f74dc9 | 2012-01-04 08:39:37 +0000 | [diff] [blame] | 198 | int gpio = d->hwirq; |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 199 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 200 | tegra_gpio_writel(tgi, 1 << GPIO_BIT(gpio), GPIO_INT_CLR(tgi, gpio)); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Lennert Buytenhek | 37337a8 | 2010-11-29 11:14:46 +0100 | [diff] [blame] | 203 | static void tegra_gpio_irq_mask(struct irq_data *d) |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 204 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 205 | struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d); |
| 206 | struct tegra_gpio_info *tgi = bank->tgi; |
Stephen Warren | 6f74dc9 | 2012-01-04 08:39:37 +0000 | [diff] [blame] | 207 | int gpio = d->hwirq; |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 208 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 209 | tegra_gpio_mask_write(tgi, GPIO_MSK_INT_ENB(tgi, gpio), gpio, 0); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Lennert Buytenhek | 37337a8 | 2010-11-29 11:14:46 +0100 | [diff] [blame] | 212 | static void tegra_gpio_irq_unmask(struct irq_data *d) |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 213 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 214 | struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d); |
| 215 | struct tegra_gpio_info *tgi = bank->tgi; |
Stephen Warren | 6f74dc9 | 2012-01-04 08:39:37 +0000 | [diff] [blame] | 216 | int gpio = d->hwirq; |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 217 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 218 | tegra_gpio_mask_write(tgi, GPIO_MSK_INT_ENB(tgi, gpio), gpio, 1); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Lennert Buytenhek | 37337a8 | 2010-11-29 11:14:46 +0100 | [diff] [blame] | 221 | static int tegra_gpio_irq_set_type(struct irq_data *d, unsigned int type) |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 222 | { |
Stephen Warren | 6f74dc9 | 2012-01-04 08:39:37 +0000 | [diff] [blame] | 223 | int gpio = d->hwirq; |
Lennert Buytenhek | 37337a8 | 2010-11-29 11:14:46 +0100 | [diff] [blame] | 224 | struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d); |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 225 | struct tegra_gpio_info *tgi = bank->tgi; |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 226 | int port = GPIO_PORT(gpio); |
| 227 | int lvl_type; |
| 228 | int val; |
| 229 | unsigned long flags; |
Stephen Warren | df231f2 | 2013-10-16 13:25:33 -0600 | [diff] [blame] | 230 | int ret; |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 231 | |
| 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 Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 257 | ret = gpiochip_lock_as_irq(&tgi->gc, gpio); |
Stephen Warren | df231f2 | 2013-10-16 13:25:33 -0600 | [diff] [blame] | 258 | if (ret) { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 259 | dev_err(tgi->dev, |
| 260 | "unable to lock Tegra GPIO %d as IRQ\n", gpio); |
Stephen Warren | df231f2 | 2013-10-16 13:25:33 -0600 | [diff] [blame] | 261 | return ret; |
| 262 | } |
| 263 | |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 264 | spin_lock_irqsave(&bank->lvl_lock[port], flags); |
| 265 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 266 | val = tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio)); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 267 | val &= ~(GPIO_INT_LVL_MASK << GPIO_BIT(gpio)); |
| 268 | val |= lvl_type << GPIO_BIT(gpio); |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 269 | tegra_gpio_writel(tgi, val, GPIO_INT_LVL(tgi, gpio)); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 270 | |
| 271 | spin_unlock_irqrestore(&bank->lvl_lock[port], flags); |
| 272 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 273 | tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, gpio), gpio, 0); |
| 274 | tegra_gpio_enable(tgi, gpio); |
Stephen Warren | d941136 | 2012-03-19 10:31:58 -0600 | [diff] [blame] | 275 | |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 276 | if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH)) |
Thomas Gleixner | f170d71 | 2015-06-23 15:52:40 +0200 | [diff] [blame] | 277 | irq_set_handler_locked(d, handle_level_irq); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 278 | else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING)) |
Thomas Gleixner | f170d71 | 2015-06-23 15:52:40 +0200 | [diff] [blame] | 279 | irq_set_handler_locked(d, handle_edge_irq); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
Stephen Warren | df231f2 | 2013-10-16 13:25:33 -0600 | [diff] [blame] | 284 | static void tegra_gpio_irq_shutdown(struct irq_data *d) |
| 285 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 286 | struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d); |
| 287 | struct tegra_gpio_info *tgi = bank->tgi; |
Stephen Warren | df231f2 | 2013-10-16 13:25:33 -0600 | [diff] [blame] | 288 | int gpio = d->hwirq; |
| 289 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 290 | gpiochip_unlock_as_irq(&tgi->gc, gpio); |
Stephen Warren | df231f2 | 2013-10-16 13:25:33 -0600 | [diff] [blame] | 291 | } |
| 292 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 293 | static void tegra_gpio_irq_handler(struct irq_desc *desc) |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 294 | { |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 295 | int port; |
| 296 | int pin; |
| 297 | int unmasked = 0; |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 298 | int gpio; |
| 299 | u32 lvl; |
| 300 | unsigned long sta; |
Will Deacon | 9802294 | 2011-02-21 13:58:10 +0000 | [diff] [blame] | 301 | struct irq_chip *chip = irq_desc_get_chip(desc); |
Jiang Liu | 476f8b4 | 2015-06-04 12:13:15 +0800 | [diff] [blame] | 302 | struct tegra_gpio_bank *bank = irq_desc_get_handler_data(desc); |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 303 | struct tegra_gpio_info *tgi = bank->tgi; |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 304 | |
Will Deacon | 9802294 | 2011-02-21 13:58:10 +0000 | [diff] [blame] | 305 | chained_irq_enter(chip, desc); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 306 | |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 307 | for (port = 0; port < 4; port++) { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 308 | 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 Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 312 | |
| 313 | for_each_set_bit(pin, &sta, 8) { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 314 | tegra_gpio_writel(tgi, 1 << pin, |
| 315 | GPIO_INT_CLR(tgi, gpio)); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 316 | |
| 317 | /* if gpio is edge triggered, clear condition |
Colin Cronin | 20a8a96 | 2015-05-18 11:41:43 -0700 | [diff] [blame] | 318 | * before executing the handler so that we don't |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 319 | * miss edges |
| 320 | */ |
| 321 | if (lvl & (0x100 << pin)) { |
| 322 | unmasked = 1; |
Will Deacon | 9802294 | 2011-02-21 13:58:10 +0000 | [diff] [blame] | 323 | chained_irq_exit(chip, desc); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | generic_handle_irq(gpio_to_irq(gpio + pin)); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | if (!unmasked) |
Will Deacon | 9802294 | 2011-02-21 13:58:10 +0000 | [diff] [blame] | 331 | chained_irq_exit(chip, desc); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 332 | |
| 333 | } |
| 334 | |
Laxman Dewangan | 8939ddc | 2012-11-07 20:31:32 +0530 | [diff] [blame] | 335 | #ifdef CONFIG_PM_SLEEP |
| 336 | static int tegra_gpio_resume(struct device *dev) |
Colin Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 337 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 338 | struct platform_device *pdev = to_platform_device(dev); |
| 339 | struct tegra_gpio_info *tgi = platform_get_drvdata(pdev); |
Colin Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 340 | unsigned long flags; |
Colin Cross | c8309ef | 2011-03-30 00:24:43 -0700 | [diff] [blame] | 341 | int b; |
| 342 | int p; |
Colin Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 343 | |
| 344 | local_irq_save(flags); |
| 345 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 346 | for (b = 0; b < tgi->bank_count; b++) { |
| 347 | struct tegra_gpio_bank *bank = &tgi->bank_info[b]; |
Colin Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 348 | |
| 349 | for (p = 0; p < ARRAY_SIZE(bank->oe); p++) { |
| 350 | unsigned int gpio = (b<<5) | (p<<3); |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 351 | 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 Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
| 364 | local_irq_restore(flags); |
Laxman Dewangan | 8939ddc | 2012-11-07 20:31:32 +0530 | [diff] [blame] | 365 | return 0; |
Colin Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Laxman Dewangan | 8939ddc | 2012-11-07 20:31:32 +0530 | [diff] [blame] | 368 | static int tegra_gpio_suspend(struct device *dev) |
Colin Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 369 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 370 | struct platform_device *pdev = to_platform_device(dev); |
| 371 | struct tegra_gpio_info *tgi = platform_get_drvdata(pdev); |
Colin Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 372 | unsigned long flags; |
Colin Cross | c8309ef | 2011-03-30 00:24:43 -0700 | [diff] [blame] | 373 | int b; |
| 374 | int p; |
Colin Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 375 | |
Colin Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 376 | local_irq_save(flags); |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 377 | for (b = 0; b < tgi->bank_count; b++) { |
| 378 | struct tegra_gpio_bank *bank = &tgi->bank_info[b]; |
Colin Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 379 | |
| 380 | for (p = 0; p < ARRAY_SIZE(bank->oe); p++) { |
| 381 | unsigned int gpio = (b<<5) | (p<<3); |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 382 | 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 Lo | 203f31c | 2013-04-03 19:31:44 +0800 | [diff] [blame] | 392 | |
| 393 | /* Enable gpio irq for wake up source */ |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 394 | tegra_gpio_writel(tgi, bank->wake_enb[p], |
| 395 | GPIO_INT_ENB(tgi, gpio)); |
Colin Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | local_irq_restore(flags); |
Laxman Dewangan | 8939ddc | 2012-11-07 20:31:32 +0530 | [diff] [blame] | 399 | return 0; |
Colin Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Joseph Lo | 203f31c | 2013-04-03 19:31:44 +0800 | [diff] [blame] | 402 | static int tegra_gpio_irq_set_wake(struct irq_data *d, unsigned int enable) |
Colin Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 403 | { |
Lennert Buytenhek | 37337a8 | 2010-11-29 11:14:46 +0100 | [diff] [blame] | 404 | struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d); |
Joseph Lo | 203f31c | 2013-04-03 19:31:44 +0800 | [diff] [blame] | 405 | 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 Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 417 | return irq_set_irq_wake(bank->irq, enable); |
Colin Cross | 2e47b8b | 2010-04-07 12:59:42 -0700 | [diff] [blame] | 418 | } |
| 419 | #endif |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 420 | |
Suzuki K. Poulose | b59d5fb | 2015-11-16 16:07:10 +0000 | [diff] [blame] | 421 | #ifdef CONFIG_DEBUG_FS |
| 422 | |
| 423 | #include <linux/debugfs.h> |
| 424 | #include <linux/seq_file.h> |
| 425 | |
| 426 | static int dbg_gpio_show(struct seq_file *s, void *unused) |
| 427 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 428 | struct tegra_gpio_info *tgi = s->private; |
Suzuki K. Poulose | b59d5fb | 2015-11-16 16:07:10 +0000 | [diff] [blame] | 429 | int i; |
| 430 | int j; |
| 431 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 432 | for (i = 0; i < tgi->bank_count; i++) { |
Suzuki K. Poulose | b59d5fb | 2015-11-16 16:07:10 +0000 | [diff] [blame] | 433 | 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 Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 438 | 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. Poulose | b59d5fb | 2015-11-16 16:07:10 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static int dbg_gpio_open(struct inode *inode, struct file *file) |
| 451 | { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 452 | return single_open(file, dbg_gpio_show, inode->i_private); |
Suzuki K. Poulose | b59d5fb | 2015-11-16 16:07:10 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | static 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 Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 462 | static void tegra_gpio_debuginit(struct tegra_gpio_info *tgi) |
Suzuki K. Poulose | b59d5fb | 2015-11-16 16:07:10 +0000 | [diff] [blame] | 463 | { |
| 464 | (void) debugfs_create_file("tegra_gpio", S_IRUGO, |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 465 | NULL, tgi, &debug_fops); |
Suzuki K. Poulose | b59d5fb | 2015-11-16 16:07:10 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | #else |
| 469 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 470 | static inline void tegra_gpio_debuginit(struct tegra_gpio_info *tgi) |
Suzuki K. Poulose | b59d5fb | 2015-11-16 16:07:10 +0000 | [diff] [blame] | 471 | { |
| 472 | } |
| 473 | |
| 474 | #endif |
| 475 | |
Laxman Dewangan | 8939ddc | 2012-11-07 20:31:32 +0530 | [diff] [blame] | 476 | static const struct dev_pm_ops tegra_gpio_pm_ops = { |
| 477 | SET_SYSTEM_SLEEP_PM_OPS(tegra_gpio_suspend, tegra_gpio_resume) |
| 478 | }; |
| 479 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 480 | static int tegra_gpio_probe(struct platform_device *pdev) |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 481 | { |
Laxman Dewangan | 171b92c | 2016-04-25 16:08:31 +0530 | [diff] [blame] | 482 | const struct tegra_gpio_soc_config *config; |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 483 | struct tegra_gpio_info *tgi; |
Stephen Warren | 88d8951 | 2011-10-11 16:16:14 -0600 | [diff] [blame] | 484 | struct resource *res; |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 485 | struct tegra_gpio_bank *bank; |
Stephen Warren | f57f98a | 2013-12-06 13:36:11 -0700 | [diff] [blame] | 486 | int ret; |
Stephen Warren | 4700800 | 2011-08-23 00:39:55 +0100 | [diff] [blame] | 487 | int gpio; |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 488 | int i; |
| 489 | int j; |
| 490 | |
Laxman Dewangan | 171b92c | 2016-04-25 16:08:31 +0530 | [diff] [blame] | 491 | config = of_device_get_match_data(&pdev->dev); |
| 492 | if (!config) { |
Stephen Warren | 165b6c2 | 2013-02-15 14:54:48 -0700 | [diff] [blame] | 493 | dev_err(&pdev->dev, "Error: No device match found\n"); |
| 494 | return -ENODEV; |
| 495 | } |
Stephen Warren | 5c1e2c9 | 2012-03-16 17:35:08 -0600 | [diff] [blame] | 496 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 497 | 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 Warren | 5c1e2c9 | 2012-03-16 17:35:08 -0600 | [diff] [blame] | 503 | |
Stephen Warren | 3391811 | 2012-01-19 08:16:35 +0000 | [diff] [blame] | 504 | for (;;) { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 505 | res = platform_get_resource(pdev, IORESOURCE_IRQ, |
| 506 | tgi->bank_count); |
Stephen Warren | 3391811 | 2012-01-19 08:16:35 +0000 | [diff] [blame] | 507 | if (!res) |
| 508 | break; |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 509 | tgi->bank_count++; |
Stephen Warren | 3391811 | 2012-01-19 08:16:35 +0000 | [diff] [blame] | 510 | } |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 511 | if (!tgi->bank_count) { |
Stephen Warren | 3391811 | 2012-01-19 08:16:35 +0000 | [diff] [blame] | 512 | dev_err(&pdev->dev, "Missing IRQ resource\n"); |
| 513 | return -ENODEV; |
| 514 | } |
| 515 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 516 | 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 Warren | 3391811 | 2012-01-19 08:16:35 +0000 | [diff] [blame] | 528 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 529 | 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 Warren | 3391811 | 2012-01-19 08:16:35 +0000 | [diff] [blame] | 544 | return -ENODEV; |
Stephen Warren | 3391811 | 2012-01-19 08:16:35 +0000 | [diff] [blame] | 545 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 546 | 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 Walleij | d023567 | 2012-10-16 21:00:09 +0200 | [diff] [blame] | 550 | return -ENODEV; |
Stephen Warren | 6f74dc9 | 2012-01-04 08:39:37 +0000 | [diff] [blame] | 551 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 552 | for (i = 0; i < tgi->bank_count; i++) { |
Stephen Warren | 88d8951 | 2011-10-11 16:16:14 -0600 | [diff] [blame] | 553 | 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 Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 559 | bank = &tgi->bank_info[i]; |
Stephen Warren | 88d8951 | 2011-10-11 16:16:14 -0600 | [diff] [blame] | 560 | bank->bank = i; |
| 561 | bank->irq = res->start; |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 562 | bank->tgi = tgi; |
Stephen Warren | 88d8951 | 2011-10-11 16:16:14 -0600 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 566 | tgi->regs = devm_ioremap_resource(&pdev->dev, res); |
| 567 | if (IS_ERR(tgi->regs)) |
| 568 | return PTR_ERR(tgi->regs); |
Stephen Warren | 88d8951 | 2011-10-11 16:16:14 -0600 | [diff] [blame] | 569 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 570 | for (i = 0; i < tgi->bank_count; i++) { |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 571 | for (j = 0; j < 4; j++) { |
| 572 | int gpio = tegra_gpio_compose(i, j, 0); |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 573 | tegra_gpio_writel(tgi, 0x00, GPIO_INT_ENB(tgi, gpio)); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 577 | ret = devm_gpiochip_add_data(&pdev->dev, &tgi->gc, tgi); |
Stephen Warren | f57f98a | 2013-12-06 13:36:11 -0700 | [diff] [blame] | 578 | if (ret < 0) { |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 579 | irq_domain_remove(tgi->irq_domain); |
Stephen Warren | f57f98a | 2013-12-06 13:36:11 -0700 | [diff] [blame] | 580 | return ret; |
| 581 | } |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 582 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 583 | for (gpio = 0; gpio < tgi->gc.ngpio; gpio++) { |
| 584 | int irq = irq_create_mapping(tgi->irq_domain, gpio); |
Stephen Warren | 4700800 | 2011-08-23 00:39:55 +0100 | [diff] [blame] | 585 | /* No validity check; all Tegra GPIOs are valid IRQs */ |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 586 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 587 | bank = &tgi->bank_info[GPIO_BANK(gpio)]; |
Stephen Warren | 4700800 | 2011-08-23 00:39:55 +0100 | [diff] [blame] | 588 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 589 | irq_set_lockdep_class(irq, &tgi->lock_class); |
Stephen Warren | 4700800 | 2011-08-23 00:39:55 +0100 | [diff] [blame] | 590 | irq_set_chip_data(irq, bank); |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 591 | irq_set_chip_and_handler(irq, &tgi->ic, handle_simple_irq); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 594 | for (i = 0; i < tgi->bank_count; i++) { |
| 595 | bank = &tgi->bank_info[i]; |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 596 | |
Russell King | e88d251 | 2015-06-16 23:06:50 +0100 | [diff] [blame] | 597 | irq_set_chained_handler_and_data(bank->irq, |
| 598 | tegra_gpio_irq_handler, bank); |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 599 | |
| 600 | for (j = 0; j < 4; j++) |
| 601 | spin_lock_init(&bank->lvl_lock[j]); |
| 602 | } |
| 603 | |
Laxman Dewangan | b546be0 | 2016-04-25 16:08:33 +0530 | [diff] [blame^] | 604 | tegra_gpio_debuginit(tgi); |
Suzuki K. Poulose | b59d5fb | 2015-11-16 16:07:10 +0000 | [diff] [blame] | 605 | |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 606 | return 0; |
| 607 | } |
| 608 | |
Laxman Dewangan | 804f568 | 2016-04-25 16:08:32 +0530 | [diff] [blame] | 609 | static const struct tegra_gpio_soc_config tegra20_gpio_config = { |
Laxman Dewangan | 171b92c | 2016-04-25 16:08:31 +0530 | [diff] [blame] | 610 | .bank_stride = 0x80, |
| 611 | .upper_offset = 0x800, |
| 612 | }; |
| 613 | |
Laxman Dewangan | 804f568 | 2016-04-25 16:08:32 +0530 | [diff] [blame] | 614 | static const struct tegra_gpio_soc_config tegra30_gpio_config = { |
Laxman Dewangan | 171b92c | 2016-04-25 16:08:31 +0530 | [diff] [blame] | 615 | .bank_stride = 0x100, |
| 616 | .upper_offset = 0x80, |
| 617 | }; |
| 618 | |
| 619 | static 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 Warren | 88d8951 | 2011-10-11 16:16:14 -0600 | [diff] [blame] | 625 | static struct platform_driver tegra_gpio_driver = { |
| 626 | .driver = { |
| 627 | .name = "tegra-gpio", |
Laxman Dewangan | 8939ddc | 2012-11-07 20:31:32 +0530 | [diff] [blame] | 628 | .pm = &tegra_gpio_pm_ops, |
Stephen Warren | 88d8951 | 2011-10-11 16:16:14 -0600 | [diff] [blame] | 629 | .of_match_table = tegra_gpio_of_match, |
| 630 | }, |
| 631 | .probe = tegra_gpio_probe, |
| 632 | }; |
| 633 | |
| 634 | static int __init tegra_gpio_init(void) |
| 635 | { |
| 636 | return platform_driver_register(&tegra_gpio_driver); |
| 637 | } |
Erik Gilling | 3c92db9 | 2010-03-15 19:40:06 -0700 | [diff] [blame] | 638 | postcore_initcall(tegra_gpio_init); |