Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 1 | /* |
Allen Martin | 55d98a1 | 2012-08-31 08:30:00 +0000 | [diff] [blame] | 2 | * NVIDIA Tegra20 GPIO handling. |
Stephen Warren | 20a9145 | 2015-09-25 10:44:08 -0600 | [diff] [blame] | 3 | * (C) Copyright 2010-2012,2015 |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 4 | * NVIDIA Corporation <www.nvidia.com> |
| 5 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * Based on (mostly copied from) kw_gpio.c based Linux 2.6 kernel driver. |
| 11 | * Tom Warren (twarren@nvidia.com) |
| 12 | */ |
| 13 | |
| 14 | #include <common.h> |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 15 | #include <dm.h> |
| 16 | #include <malloc.h> |
| 17 | #include <errno.h> |
| 18 | #include <fdtdec.h> |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 19 | #include <asm/io.h> |
| 20 | #include <asm/bitops.h> |
Tom Warren | ab37196 | 2012-09-19 15:50:56 -0700 | [diff] [blame] | 21 | #include <asm/arch/tegra.h> |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 22 | #include <asm/gpio.h> |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 23 | #include <dm/device-internal.h> |
Simon Glass | ada3b75 | 2015-01-05 20:05:33 -0700 | [diff] [blame] | 24 | #include <dt-bindings/gpio/gpio.h> |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 25 | |
| 26 | DECLARE_GLOBAL_DATA_PTR; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 27 | |
Stephen Warren | 20a9145 | 2015-09-25 10:44:08 -0600 | [diff] [blame] | 28 | static const int CONFIG_SFIO = 0; |
| 29 | static const int CONFIG_GPIO = 1; |
| 30 | static const int DIRECTION_INPUT = 0; |
| 31 | static const int DIRECTION_OUTPUT = 1; |
| 32 | |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 33 | struct tegra_gpio_platdata { |
| 34 | struct gpio_ctlr_bank *bank; |
| 35 | const char *port_name; /* Name of port, e.g. "B" */ |
| 36 | int base_gpio; /* Port number for this port (0, 1,.., n-1) */ |
| 37 | }; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 38 | |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 39 | /* Information about each port at run-time */ |
| 40 | struct tegra_port_info { |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 41 | struct gpio_ctlr_bank *bank; |
| 42 | int base_gpio; /* Port number for this port (0, 1,.., n-1) */ |
| 43 | }; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 44 | |
Stephen Warren | 20a9145 | 2015-09-25 10:44:08 -0600 | [diff] [blame] | 45 | /* Return config of pin 'gpio' as GPIO (1) or SFIO (0) */ |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 46 | static int get_config(unsigned gpio) |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 47 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 48 | struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
| 49 | struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)]; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 50 | u32 u; |
| 51 | int type; |
| 52 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 53 | u = readl(&bank->gpio_config[GPIO_PORT(gpio)]); |
Stephen Warren | 20a9145 | 2015-09-25 10:44:08 -0600 | [diff] [blame] | 54 | type = (u >> GPIO_BIT(gpio)) & 1; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 55 | |
| 56 | debug("get_config: port = %d, bit = %d is %s\n", |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 57 | GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO"); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 58 | |
Stephen Warren | 20a9145 | 2015-09-25 10:44:08 -0600 | [diff] [blame] | 59 | return type ? CONFIG_GPIO : CONFIG_SFIO; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Stephen Warren | 20a9145 | 2015-09-25 10:44:08 -0600 | [diff] [blame] | 62 | /* Config pin 'gpio' as GPIO or SFIO, based on 'type' */ |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 63 | static void set_config(unsigned gpio, int type) |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 64 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 65 | struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
| 66 | struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)]; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 67 | u32 u; |
| 68 | |
| 69 | debug("set_config: port = %d, bit = %d, %s\n", |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 70 | GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO"); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 71 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 72 | u = readl(&bank->gpio_config[GPIO_PORT(gpio)]); |
Stephen Warren | 20a9145 | 2015-09-25 10:44:08 -0600 | [diff] [blame] | 73 | if (type != CONFIG_SFIO) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 74 | u |= 1 << GPIO_BIT(gpio); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 75 | else |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 76 | u &= ~(1 << GPIO_BIT(gpio)); |
| 77 | writel(u, &bank->gpio_config[GPIO_PORT(gpio)]); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 80 | /* Return GPIO pin 'gpio' direction - 0 = input or 1 = output */ |
| 81 | static int get_direction(unsigned gpio) |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 82 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 83 | struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
| 84 | struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)]; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 85 | u32 u; |
| 86 | int dir; |
| 87 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 88 | u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]); |
| 89 | dir = (u >> GPIO_BIT(gpio)) & 1; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 90 | |
| 91 | debug("get_direction: port = %d, bit = %d, %s\n", |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 92 | GPIO_FULLPORT(gpio), GPIO_BIT(gpio), dir ? "OUT" : "IN"); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 93 | |
Stephen Warren | 20a9145 | 2015-09-25 10:44:08 -0600 | [diff] [blame] | 94 | return dir ? DIRECTION_OUTPUT : DIRECTION_INPUT; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 97 | /* Config GPIO pin 'gpio' as input or output (OE) as per 'output' */ |
| 98 | static void set_direction(unsigned gpio, int output) |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 99 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 100 | struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
| 101 | struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)]; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 102 | u32 u; |
| 103 | |
| 104 | debug("set_direction: port = %d, bit = %d, %s\n", |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 105 | GPIO_FULLPORT(gpio), GPIO_BIT(gpio), output ? "OUT" : "IN"); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 106 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 107 | u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]); |
Stephen Warren | 20a9145 | 2015-09-25 10:44:08 -0600 | [diff] [blame] | 108 | if (output != DIRECTION_INPUT) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 109 | u |= 1 << GPIO_BIT(gpio); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 110 | else |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 111 | u &= ~(1 << GPIO_BIT(gpio)); |
| 112 | writel(u, &bank->gpio_dir_out[GPIO_PORT(gpio)]); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 115 | /* set GPIO pin 'gpio' output bit as 0 or 1 as per 'high' */ |
| 116 | static void set_level(unsigned gpio, int high) |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 117 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 118 | struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
| 119 | struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)]; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 120 | u32 u; |
| 121 | |
| 122 | debug("set_level: port = %d, bit %d == %d\n", |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 123 | GPIO_FULLPORT(gpio), GPIO_BIT(gpio), high); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 124 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 125 | u = readl(&bank->gpio_out[GPIO_PORT(gpio)]); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 126 | if (high) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 127 | u |= 1 << GPIO_BIT(gpio); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 128 | else |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 129 | u &= ~(1 << GPIO_BIT(gpio)); |
| 130 | writel(u, &bank->gpio_out[GPIO_PORT(gpio)]); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Generic_GPIO primitives. |
| 135 | */ |
| 136 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 137 | /* set GPIO pin 'gpio' as an input */ |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 138 | static int tegra_gpio_direction_input(struct udevice *dev, unsigned offset) |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 139 | { |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 140 | struct tegra_port_info *state = dev_get_priv(dev); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 141 | |
| 142 | /* Configure GPIO direction as input. */ |
Stephen Warren | 20a9145 | 2015-09-25 10:44:08 -0600 | [diff] [blame] | 143 | set_direction(state->base_gpio + offset, DIRECTION_INPUT); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 144 | |
Stephen Warren | 6904f6d | 2015-09-23 12:13:00 -0600 | [diff] [blame] | 145 | /* Enable the pin as a GPIO */ |
| 146 | set_config(state->base_gpio + offset, 1); |
| 147 | |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 151 | /* set GPIO pin 'gpio' as an output, with polarity 'value' */ |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 152 | static int tegra_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 153 | int value) |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 154 | { |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 155 | struct tegra_port_info *state = dev_get_priv(dev); |
| 156 | int gpio = state->base_gpio + offset; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 157 | |
| 158 | /* Configure GPIO output value. */ |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 159 | set_level(gpio, value); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 160 | |
| 161 | /* Configure GPIO direction as output. */ |
Stephen Warren | 20a9145 | 2015-09-25 10:44:08 -0600 | [diff] [blame] | 162 | set_direction(gpio, DIRECTION_OUTPUT); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 163 | |
Stephen Warren | 6904f6d | 2015-09-23 12:13:00 -0600 | [diff] [blame] | 164 | /* Enable the pin as a GPIO */ |
| 165 | set_config(state->base_gpio + offset, 1); |
| 166 | |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 170 | /* read GPIO IN value of pin 'gpio' */ |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 171 | static int tegra_gpio_get_value(struct udevice *dev, unsigned offset) |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 172 | { |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 173 | struct tegra_port_info *state = dev_get_priv(dev); |
| 174 | int gpio = state->base_gpio + offset; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 175 | int val; |
| 176 | |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 177 | debug("%s: pin = %d (port %d:bit %d)\n", __func__, |
| 178 | gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio)); |
| 179 | |
| 180 | val = readl(&state->bank->gpio_in[GPIO_PORT(gpio)]); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 181 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 182 | return (val >> GPIO_BIT(gpio)) & 1; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 185 | /* write GPIO OUT value to pin 'gpio' */ |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 186 | static int tegra_gpio_set_value(struct udevice *dev, unsigned offset, int value) |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 187 | { |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 188 | struct tegra_port_info *state = dev_get_priv(dev); |
| 189 | int gpio = state->base_gpio + offset; |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 190 | |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 191 | debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n", |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 192 | gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), value); |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 193 | |
| 194 | /* Configure GPIO output value. */ |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 195 | set_level(gpio, value); |
| 196 | |
| 197 | return 0; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Stephen Warren | 4b27bf1 | 2014-04-22 14:37:53 -0600 | [diff] [blame] | 200 | void gpio_config_table(const struct tegra_gpio_config *config, int len) |
| 201 | { |
| 202 | int i; |
| 203 | |
| 204 | for (i = 0; i < len; i++) { |
| 205 | switch (config[i].init) { |
| 206 | case TEGRA_GPIO_INIT_IN: |
Stephen Warren | 20a9145 | 2015-09-25 10:44:08 -0600 | [diff] [blame] | 207 | set_direction(config[i].gpio, DIRECTION_INPUT); |
Stephen Warren | 4b27bf1 | 2014-04-22 14:37:53 -0600 | [diff] [blame] | 208 | break; |
| 209 | case TEGRA_GPIO_INIT_OUT0: |
Stephen Warren | c6f5402 | 2015-09-23 12:12:59 -0600 | [diff] [blame] | 210 | set_level(config[i].gpio, 0); |
Stephen Warren | 20a9145 | 2015-09-25 10:44:08 -0600 | [diff] [blame] | 211 | set_direction(config[i].gpio, DIRECTION_OUTPUT); |
Stephen Warren | 4b27bf1 | 2014-04-22 14:37:53 -0600 | [diff] [blame] | 212 | break; |
| 213 | case TEGRA_GPIO_INIT_OUT1: |
Stephen Warren | c6f5402 | 2015-09-23 12:12:59 -0600 | [diff] [blame] | 214 | set_level(config[i].gpio, 1); |
Stephen Warren | 20a9145 | 2015-09-25 10:44:08 -0600 | [diff] [blame] | 215 | set_direction(config[i].gpio, DIRECTION_OUTPUT); |
Stephen Warren | 4b27bf1 | 2014-04-22 14:37:53 -0600 | [diff] [blame] | 216 | break; |
| 217 | } |
Stephen Warren | 20a9145 | 2015-09-25 10:44:08 -0600 | [diff] [blame] | 218 | set_config(config[i].gpio, CONFIG_GPIO); |
Stephen Warren | 4b27bf1 | 2014-04-22 14:37:53 -0600 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 222 | static int tegra_gpio_get_function(struct udevice *dev, unsigned offset) |
| 223 | { |
| 224 | struct tegra_port_info *state = dev_get_priv(dev); |
| 225 | int gpio = state->base_gpio + offset; |
| 226 | |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 227 | if (!get_config(gpio)) |
| 228 | return GPIOF_FUNC; |
| 229 | else if (get_direction(gpio)) |
| 230 | return GPIOF_OUTPUT; |
| 231 | else |
| 232 | return GPIOF_INPUT; |
| 233 | } |
| 234 | |
Simon Glass | ada3b75 | 2015-01-05 20:05:33 -0700 | [diff] [blame] | 235 | static int tegra_gpio_xlate(struct udevice *dev, struct gpio_desc *desc, |
| 236 | struct fdtdec_phandle_args *args) |
| 237 | { |
| 238 | int gpio, port, ret; |
| 239 | |
| 240 | gpio = args->args[0]; |
| 241 | port = gpio / TEGRA_GPIOS_PER_PORT; |
| 242 | ret = device_get_child(dev, port, &desc->dev); |
| 243 | if (ret) |
| 244 | return ret; |
| 245 | desc->offset = gpio % TEGRA_GPIOS_PER_PORT; |
| 246 | desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0; |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 251 | static const struct dm_gpio_ops gpio_tegra_ops = { |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 252 | .direction_input = tegra_gpio_direction_input, |
| 253 | .direction_output = tegra_gpio_direction_output, |
| 254 | .get_value = tegra_gpio_get_value, |
| 255 | .set_value = tegra_gpio_set_value, |
| 256 | .get_function = tegra_gpio_get_function, |
Simon Glass | ada3b75 | 2015-01-05 20:05:33 -0700 | [diff] [blame] | 257 | .xlate = tegra_gpio_xlate, |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | /** |
| 261 | * Returns the name of a GPIO port |
| 262 | * |
| 263 | * GPIOs are named A, B, C, ..., Z, AA, BB, CC, ... |
| 264 | * |
| 265 | * @base_port: Base port number (0, 1..n-1) |
| 266 | * @return allocated string containing the name |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 267 | */ |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 268 | static char *gpio_port_name(int base_port) |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 269 | { |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 270 | char *name, *s; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 271 | |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 272 | name = malloc(3); |
| 273 | if (name) { |
| 274 | s = name; |
| 275 | *s++ = 'A' + (base_port % 26); |
| 276 | if (base_port >= 26) |
| 277 | *s++ = *name; |
| 278 | *s = '\0'; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 279 | } |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 280 | |
| 281 | return name; |
Tom Warren | b3878b8 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 282 | } |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 283 | |
| 284 | static const struct udevice_id tegra_gpio_ids[] = { |
| 285 | { .compatible = "nvidia,tegra30-gpio" }, |
| 286 | { .compatible = "nvidia,tegra20-gpio" }, |
| 287 | { } |
| 288 | }; |
| 289 | |
| 290 | static int gpio_tegra_probe(struct udevice *dev) |
| 291 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 292 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 293 | struct tegra_port_info *priv = dev->priv; |
| 294 | struct tegra_gpio_platdata *plat = dev->platdata; |
| 295 | |
| 296 | /* Only child devices have ports */ |
| 297 | if (!plat) |
| 298 | return 0; |
| 299 | |
| 300 | priv->bank = plat->bank; |
| 301 | priv->base_gpio = plat->base_gpio; |
| 302 | |
| 303 | uc_priv->gpio_count = TEGRA_GPIOS_PER_PORT; |
| 304 | uc_priv->bank_name = plat->port_name; |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * We have a top-level GPIO device with no actual GPIOs. It has a child |
| 311 | * device for each Tegra port. |
| 312 | */ |
| 313 | static int gpio_tegra_bind(struct udevice *parent) |
| 314 | { |
| 315 | struct tegra_gpio_platdata *plat = parent->platdata; |
| 316 | struct gpio_ctlr *ctlr; |
| 317 | int bank_count; |
| 318 | int bank; |
| 319 | int ret; |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 320 | |
| 321 | /* If this is a child device, there is nothing to do here */ |
| 322 | if (plat) |
| 323 | return 0; |
| 324 | |
Simon Glass | e3e5a9f | 2015-03-03 08:02:59 -0700 | [diff] [blame] | 325 | /* TODO(sjg@chromium.org): Remove once SPL supports device tree */ |
| 326 | #ifdef CONFIG_SPL_BUILD |
| 327 | ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
| 328 | bank_count = TEGRA_GPIO_BANKS; |
| 329 | #else |
| 330 | { |
| 331 | int len; |
| 332 | |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 333 | /* |
| 334 | * This driver does not make use of interrupts, other than to figure |
| 335 | * out the number of GPIO banks |
| 336 | */ |
| 337 | if (!fdt_getprop(gd->fdt_blob, parent->of_offset, "interrupts", &len)) |
| 338 | return -EINVAL; |
| 339 | bank_count = len / 3 / sizeof(u32); |
Simon Glass | 0971778 | 2015-08-11 08:33:29 -0600 | [diff] [blame] | 340 | ctlr = (struct gpio_ctlr *)dev_get_addr(parent); |
Simon Glass | e3e5a9f | 2015-03-03 08:02:59 -0700 | [diff] [blame] | 341 | } |
| 342 | #endif |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 343 | for (bank = 0; bank < bank_count; bank++) { |
| 344 | int port; |
| 345 | |
| 346 | for (port = 0; port < TEGRA_PORTS_PER_BANK; port++) { |
| 347 | struct tegra_gpio_platdata *plat; |
| 348 | struct udevice *dev; |
| 349 | int base_port; |
| 350 | |
| 351 | plat = calloc(1, sizeof(*plat)); |
| 352 | if (!plat) |
| 353 | return -ENOMEM; |
| 354 | plat->bank = &ctlr->gpio_bank[bank]; |
| 355 | base_port = bank * TEGRA_PORTS_PER_BANK + port; |
| 356 | plat->base_gpio = TEGRA_GPIOS_PER_PORT * base_port; |
| 357 | plat->port_name = gpio_port_name(base_port); |
| 358 | |
| 359 | ret = device_bind(parent, parent->driver, |
| 360 | plat->port_name, plat, -1, &dev); |
| 361 | if (ret) |
| 362 | return ret; |
| 363 | dev->of_offset = parent->of_offset; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | U_BOOT_DRIVER(gpio_tegra) = { |
| 371 | .name = "gpio_tegra", |
| 372 | .id = UCLASS_GPIO, |
| 373 | .of_match = tegra_gpio_ids, |
| 374 | .bind = gpio_tegra_bind, |
| 375 | .probe = gpio_tegra_probe, |
| 376 | .priv_auto_alloc_size = sizeof(struct tegra_port_info), |
| 377 | .ops = &gpio_tegra_ops, |
Simon Glass | e3e5a9f | 2015-03-03 08:02:59 -0700 | [diff] [blame] | 378 | .flags = DM_FLAG_PRE_RELOC, |
Simon Glass | b046104 | 2014-09-03 17:37:03 -0600 | [diff] [blame] | 379 | }; |