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