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