blob: b83df351e749f08d53c55afcf2ec78e17da5053f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tom Warrenb3878b82011-06-17 06:27:28 +00002/*
Allen Martin55d98a12012-08-31 08:30:00 +00003 * NVIDIA Tegra20 GPIO handling.
Stephen Warren20a91452015-09-25 10:44:08 -06004 * (C) Copyright 2010-2012,2015
Tom Warrenb3878b82011-06-17 06:27:28 +00005 * NVIDIA Corporation <www.nvidia.com>
Tom Warrenb3878b82011-06-17 06:27:28 +00006 */
7
8/*
9 * Based on (mostly copied from) kw_gpio.c based Linux 2.6 kernel driver.
10 * Tom Warren (twarren@nvidia.com)
11 */
12
Simon Glassb0461042014-09-03 17:37:03 -060013#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glassb0461042014-09-03 17:37:03 -060015#include <malloc.h>
16#include <errno.h>
17#include <fdtdec.h>
Tom Warrenb3878b82011-06-17 06:27:28 +000018#include <asm/io.h>
19#include <asm/bitops.h>
Tom Warrenab371962012-09-19 15:50:56 -070020#include <asm/arch/tegra.h>
Tom Warrenb3878b82011-06-17 06:27:28 +000021#include <asm/gpio.h>
Simon Glassb0461042014-09-03 17:37:03 -060022#include <dm/device-internal.h>
Simon Glassada3b752015-01-05 20:05:33 -070023#include <dt-bindings/gpio/gpio.h>
Simon Glassb0461042014-09-03 17:37:03 -060024
Simon Glass8adc1b62021-08-08 12:20:23 -060025static const int CFG_SFIO = 0;
26static const int CFG_GPIO = 1;
Stephen Warren20a91452015-09-25 10:44:08 -060027static const int DIRECTION_INPUT = 0;
28static const int DIRECTION_OUTPUT = 1;
29
Simon Glassb75b15b2020-12-03 16:55:23 -070030struct tegra_gpio_plat {
Simon Glassb0461042014-09-03 17:37:03 -060031 struct gpio_ctlr_bank *bank;
32 const char *port_name; /* Name of port, e.g. "B" */
33 int base_gpio; /* Port number for this port (0, 1,.., n-1) */
34};
Tom Warrenb3878b82011-06-17 06:27:28 +000035
Simon Glassb0461042014-09-03 17:37:03 -060036/* Information about each port at run-time */
37struct tegra_port_info {
Simon Glassb0461042014-09-03 17:37:03 -060038 struct gpio_ctlr_bank *bank;
39 int base_gpio; /* Port number for this port (0, 1,.., n-1) */
40};
Tom Warrenb3878b82011-06-17 06:27:28 +000041
Stephen Warren20a91452015-09-25 10:44:08 -060042/* Return config of pin 'gpio' as GPIO (1) or SFIO (0) */
Joe Hershbergerf8928f12011-11-11 15:55:36 -060043static int get_config(unsigned gpio)
Tom Warrenb3878b82011-06-17 06:27:28 +000044{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060045 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
46 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +000047 u32 u;
48 int type;
49
Joe Hershbergerf8928f12011-11-11 15:55:36 -060050 u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
Stephen Warren20a91452015-09-25 10:44:08 -060051 type = (u >> GPIO_BIT(gpio)) & 1;
Tom Warrenb3878b82011-06-17 06:27:28 +000052
53 debug("get_config: port = %d, bit = %d is %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -060054 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
Tom Warrenb3878b82011-06-17 06:27:28 +000055
Simon Glass8adc1b62021-08-08 12:20:23 -060056 return type ? CFG_GPIO : CFG_SFIO;
Tom Warrenb3878b82011-06-17 06:27:28 +000057}
58
Stephen Warren20a91452015-09-25 10:44:08 -060059/* Config pin 'gpio' as GPIO or SFIO, based on 'type' */
Joe Hershbergerf8928f12011-11-11 15:55:36 -060060static void set_config(unsigned gpio, int type)
Tom Warrenb3878b82011-06-17 06:27:28 +000061{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060062 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
63 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +000064 u32 u;
65
66 debug("set_config: port = %d, bit = %d, %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -060067 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
Tom Warrenb3878b82011-06-17 06:27:28 +000068
Joe Hershbergerf8928f12011-11-11 15:55:36 -060069 u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
Simon Glass8adc1b62021-08-08 12:20:23 -060070 if (type != CFG_SFIO)
Joe Hershbergerf8928f12011-11-11 15:55:36 -060071 u |= 1 << GPIO_BIT(gpio);
Tom Warrenb3878b82011-06-17 06:27:28 +000072 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -060073 u &= ~(1 << GPIO_BIT(gpio));
74 writel(u, &bank->gpio_config[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +000075}
76
Joe Hershbergerf8928f12011-11-11 15:55:36 -060077/* Return GPIO pin 'gpio' direction - 0 = input or 1 = output */
78static int get_direction(unsigned gpio)
Tom Warrenb3878b82011-06-17 06:27:28 +000079{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060080 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
81 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +000082 u32 u;
83 int dir;
84
Joe Hershbergerf8928f12011-11-11 15:55:36 -060085 u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
86 dir = (u >> GPIO_BIT(gpio)) & 1;
Tom Warrenb3878b82011-06-17 06:27:28 +000087
88 debug("get_direction: port = %d, bit = %d, %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -060089 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), dir ? "OUT" : "IN");
Tom Warrenb3878b82011-06-17 06:27:28 +000090
Stephen Warren20a91452015-09-25 10:44:08 -060091 return dir ? DIRECTION_OUTPUT : DIRECTION_INPUT;
Tom Warrenb3878b82011-06-17 06:27:28 +000092}
93
Joe Hershbergerf8928f12011-11-11 15:55:36 -060094/* Config GPIO pin 'gpio' as input or output (OE) as per 'output' */
95static void set_direction(unsigned gpio, int output)
Tom Warrenb3878b82011-06-17 06:27:28 +000096{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060097 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
98 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +000099 u32 u;
100
101 debug("set_direction: port = %d, bit = %d, %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600102 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), output ? "OUT" : "IN");
Tom Warrenb3878b82011-06-17 06:27:28 +0000103
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600104 u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
Stephen Warren20a91452015-09-25 10:44:08 -0600105 if (output != DIRECTION_INPUT)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600106 u |= 1 << GPIO_BIT(gpio);
Tom Warrenb3878b82011-06-17 06:27:28 +0000107 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600108 u &= ~(1 << GPIO_BIT(gpio));
109 writel(u, &bank->gpio_dir_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000110}
111
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600112/* set GPIO pin 'gpio' output bit as 0 or 1 as per 'high' */
113static void set_level(unsigned gpio, int high)
Tom Warrenb3878b82011-06-17 06:27:28 +0000114{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600115 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
116 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +0000117 u32 u;
118
119 debug("set_level: port = %d, bit %d == %d\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600120 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), high);
Tom Warrenb3878b82011-06-17 06:27:28 +0000121
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600122 u = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000123 if (high)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600124 u |= 1 << GPIO_BIT(gpio);
Tom Warrenb3878b82011-06-17 06:27:28 +0000125 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600126 u &= ~(1 << GPIO_BIT(gpio));
127 writel(u, &bank->gpio_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000128}
129
130/*
131 * Generic_GPIO primitives.
132 */
133
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600134/* set GPIO pin 'gpio' as an input */
Simon Glassb0461042014-09-03 17:37:03 -0600135static int tegra_gpio_direction_input(struct udevice *dev, unsigned offset)
Tom Warrenb3878b82011-06-17 06:27:28 +0000136{
Simon Glassb0461042014-09-03 17:37:03 -0600137 struct tegra_port_info *state = dev_get_priv(dev);
Tom Warrenb3878b82011-06-17 06:27:28 +0000138
139 /* Configure GPIO direction as input. */
Stephen Warren20a91452015-09-25 10:44:08 -0600140 set_direction(state->base_gpio + offset, DIRECTION_INPUT);
Tom Warrenb3878b82011-06-17 06:27:28 +0000141
Stephen Warren6904f6d2015-09-23 12:13:00 -0600142 /* Enable the pin as a GPIO */
143 set_config(state->base_gpio + offset, 1);
144
Tom Warrenb3878b82011-06-17 06:27:28 +0000145 return 0;
146}
147
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600148/* set GPIO pin 'gpio' as an output, with polarity 'value' */
Simon Glassb0461042014-09-03 17:37:03 -0600149static int tegra_gpio_direction_output(struct udevice *dev, unsigned offset,
150 int value)
Tom Warrenb3878b82011-06-17 06:27:28 +0000151{
Simon Glassb0461042014-09-03 17:37:03 -0600152 struct tegra_port_info *state = dev_get_priv(dev);
153 int gpio = state->base_gpio + offset;
Tom Warrenb3878b82011-06-17 06:27:28 +0000154
155 /* Configure GPIO output value. */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600156 set_level(gpio, value);
Tom Warrenb3878b82011-06-17 06:27:28 +0000157
158 /* Configure GPIO direction as output. */
Stephen Warren20a91452015-09-25 10:44:08 -0600159 set_direction(gpio, DIRECTION_OUTPUT);
Tom Warrenb3878b82011-06-17 06:27:28 +0000160
Stephen Warren6904f6d2015-09-23 12:13:00 -0600161 /* Enable the pin as a GPIO */
162 set_config(state->base_gpio + offset, 1);
163
Tom Warrenb3878b82011-06-17 06:27:28 +0000164 return 0;
165}
166
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600167/* read GPIO IN value of pin 'gpio' */
Simon Glassb0461042014-09-03 17:37:03 -0600168static int tegra_gpio_get_value(struct udevice *dev, unsigned offset)
Tom Warrenb3878b82011-06-17 06:27:28 +0000169{
Simon Glassb0461042014-09-03 17:37:03 -0600170 struct tegra_port_info *state = dev_get_priv(dev);
171 int gpio = state->base_gpio + offset;
Tom Warrenb3878b82011-06-17 06:27:28 +0000172 int val;
173
Simon Glassb0461042014-09-03 17:37:03 -0600174 debug("%s: pin = %d (port %d:bit %d)\n", __func__,
175 gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
176
Simon Glass5f4de1f2016-01-30 16:37:45 -0700177 if (get_direction(gpio) == DIRECTION_INPUT)
178 val = readl(&state->bank->gpio_in[GPIO_PORT(gpio)]);
179 else
180 val = readl(&state->bank->gpio_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000181
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600182 return (val >> GPIO_BIT(gpio)) & 1;
Tom Warrenb3878b82011-06-17 06:27:28 +0000183}
184
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600185/* write GPIO OUT value to pin 'gpio' */
Simon Glassb0461042014-09-03 17:37:03 -0600186static int tegra_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Tom Warrenb3878b82011-06-17 06:27:28 +0000187{
Simon Glassb0461042014-09-03 17:37:03 -0600188 struct tegra_port_info *state = dev_get_priv(dev);
189 int gpio = state->base_gpio + offset;
Simon Glassb0461042014-09-03 17:37:03 -0600190
Tom Warrenb3878b82011-06-17 06:27:28 +0000191 debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n",
Simon Glassb0461042014-09-03 17:37:03 -0600192 gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), value);
Tom Warrenb3878b82011-06-17 06:27:28 +0000193
194 /* Configure GPIO output value. */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600195 set_level(gpio, value);
196
197 return 0;
Tom Warrenb3878b82011-06-17 06:27:28 +0000198}
199
Stephen Warren4b27bf12014-04-22 14:37:53 -0600200void 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 Warren20a91452015-09-25 10:44:08 -0600207 set_direction(config[i].gpio, DIRECTION_INPUT);
Stephen Warren4b27bf12014-04-22 14:37:53 -0600208 break;
209 case TEGRA_GPIO_INIT_OUT0:
Stephen Warrenc6f54022015-09-23 12:12:59 -0600210 set_level(config[i].gpio, 0);
Stephen Warren20a91452015-09-25 10:44:08 -0600211 set_direction(config[i].gpio, DIRECTION_OUTPUT);
Stephen Warren4b27bf12014-04-22 14:37:53 -0600212 break;
213 case TEGRA_GPIO_INIT_OUT1:
Stephen Warrenc6f54022015-09-23 12:12:59 -0600214 set_level(config[i].gpio, 1);
Stephen Warren20a91452015-09-25 10:44:08 -0600215 set_direction(config[i].gpio, DIRECTION_OUTPUT);
Stephen Warren4b27bf12014-04-22 14:37:53 -0600216 break;
217 }
Simon Glass8adc1b62021-08-08 12:20:23 -0600218 set_config(config[i].gpio, CFG_GPIO);
Stephen Warren4b27bf12014-04-22 14:37:53 -0600219 }
220}
221
Simon Glassb0461042014-09-03 17:37:03 -0600222static 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 Glassb0461042014-09-03 17:37:03 -0600227 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 Glassada3b752015-01-05 20:05:33 -0700235static int tegra_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600236 struct ofnode_phandle_args *args)
Simon Glassada3b752015-01-05 20:05:33 -0700237{
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 Glassb0461042014-09-03 17:37:03 -0600251static const struct dm_gpio_ops gpio_tegra_ops = {
Simon Glassb0461042014-09-03 17:37:03 -0600252 .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 Glassada3b752015-01-05 20:05:33 -0700257 .xlate = tegra_gpio_xlate,
Simon Glassb0461042014-09-03 17:37:03 -0600258};
259
Svyatoslav Ryhelbd7326a2024-08-09 21:10:31 +0300260/*
261 * SPL GPIO functions.
262 */
263int spl_gpio_output(void *regs, uint gpio, int value)
264{
265 /* Configure GPIO output value. */
266 set_level(gpio, value);
267
268 /* Configure GPIO direction as output. */
269 set_direction(gpio, DIRECTION_OUTPUT);
270
271 /* Enable the pin as a GPIO */
272 set_config(gpio, 1);
273
274 return 0;
275}
276
277int spl_gpio_input(void *regs, uint gpio)
278{
279 /* Configure GPIO direction as input. */
280 set_direction(gpio, DIRECTION_INPUT);
281
282 /* Enable the pin as a GPIO */
283 set_config(gpio, 1);
284
285 return 0;
286}
287
288int spl_gpio_get_value(void *regs, uint gpio)
289{
290 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
291 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
292 int val;
293
294 if (get_direction(gpio) == DIRECTION_INPUT)
295 val = readl(&bank->gpio_in[GPIO_PORT(gpio)]);
296 else
297 val = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
298
299 return (val >> GPIO_BIT(gpio)) & 1;
300}
301
302int spl_gpio_set_value(void *regs, uint gpio, int value)
303{
304 /* Configure GPIO output value. */
305 set_level(gpio, value);
306
307 return 0;
308}
309
Simon Glassb0461042014-09-03 17:37:03 -0600310/**
311 * Returns the name of a GPIO port
312 *
313 * GPIOs are named A, B, C, ..., Z, AA, BB, CC, ...
314 *
315 * @base_port: Base port number (0, 1..n-1)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100316 * Return: allocated string containing the name
Tom Warrenb3878b82011-06-17 06:27:28 +0000317 */
Simon Glassb0461042014-09-03 17:37:03 -0600318static char *gpio_port_name(int base_port)
Tom Warrenb3878b82011-06-17 06:27:28 +0000319{
Simon Glassb0461042014-09-03 17:37:03 -0600320 char *name, *s;
Tom Warrenb3878b82011-06-17 06:27:28 +0000321
Simon Glassb0461042014-09-03 17:37:03 -0600322 name = malloc(3);
323 if (name) {
324 s = name;
325 *s++ = 'A' + (base_port % 26);
326 if (base_port >= 26)
327 *s++ = *name;
328 *s = '\0';
Tom Warrenb3878b82011-06-17 06:27:28 +0000329 }
Simon Glassb0461042014-09-03 17:37:03 -0600330
331 return name;
Tom Warrenb3878b82011-06-17 06:27:28 +0000332}
Simon Glassb0461042014-09-03 17:37:03 -0600333
334static const struct udevice_id tegra_gpio_ids[] = {
335 { .compatible = "nvidia,tegra30-gpio" },
336 { .compatible = "nvidia,tegra20-gpio" },
337 { }
338};
339
340static int gpio_tegra_probe(struct udevice *dev)
341{
Simon Glassde0977b2015-03-05 12:25:20 -0700342 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass95588622020-12-22 19:30:28 -0700343 struct tegra_port_info *priv = dev_get_priv(dev);
344 struct tegra_gpio_plat *plat = dev_get_plat(dev);
Simon Glassb0461042014-09-03 17:37:03 -0600345
346 /* Only child devices have ports */
347 if (!plat)
348 return 0;
349
350 priv->bank = plat->bank;
351 priv->base_gpio = plat->base_gpio;
352
353 uc_priv->gpio_count = TEGRA_GPIOS_PER_PORT;
354 uc_priv->bank_name = plat->port_name;
355
356 return 0;
357}
358
359/**
360 * We have a top-level GPIO device with no actual GPIOs. It has a child
361 * device for each Tegra port.
362 */
363static int gpio_tegra_bind(struct udevice *parent)
364{
Simon Glass95588622020-12-22 19:30:28 -0700365 struct tegra_gpio_plat *plat = dev_get_plat(parent);
Simon Glassb0461042014-09-03 17:37:03 -0600366 struct gpio_ctlr *ctlr;
367 int bank_count;
368 int bank;
369 int ret;
Simon Glassb0461042014-09-03 17:37:03 -0600370
371 /* If this is a child device, there is nothing to do here */
372 if (plat)
373 return 0;
374
Simon Glasse3e5a9f2015-03-03 08:02:59 -0700375 /* TODO(sjg@chromium.org): Remove once SPL supports device tree */
Simon Glass7ec24132024-09-29 19:49:48 -0600376#ifdef CONFIG_XPL_BUILD
Simon Glasse3e5a9f2015-03-03 08:02:59 -0700377 ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
378 bank_count = TEGRA_GPIO_BANKS;
379#else
380 {
381 int len;
382
Simon Glassb0461042014-09-03 17:37:03 -0600383 /*
384 * This driver does not make use of interrupts, other than to figure
385 * out the number of GPIO banks
386 */
Simon Glassb0ca8492017-07-25 08:30:03 -0600387 len = dev_read_size(parent, "interrupts");
388 if (len < 0)
389 return len;
Simon Glassb0461042014-09-03 17:37:03 -0600390 bank_count = len / 3 / sizeof(u32);
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100391 ctlr = dev_read_addr_ptr(parent);
392 if (!ctlr)
Simon Glassb0ca8492017-07-25 08:30:03 -0600393 return -EINVAL;
Simon Glasse3e5a9f2015-03-03 08:02:59 -0700394 }
395#endif
Simon Glassb0461042014-09-03 17:37:03 -0600396 for (bank = 0; bank < bank_count; bank++) {
397 int port;
398
399 for (port = 0; port < TEGRA_PORTS_PER_BANK; port++) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700400 struct tegra_gpio_plat *plat;
Simon Glassb0461042014-09-03 17:37:03 -0600401 struct udevice *dev;
402 int base_port;
403
404 plat = calloc(1, sizeof(*plat));
405 if (!plat)
406 return -ENOMEM;
407 plat->bank = &ctlr->gpio_bank[bank];
408 base_port = bank * TEGRA_PORTS_PER_BANK + port;
409 plat->base_gpio = TEGRA_GPIOS_PER_PORT * base_port;
410 plat->port_name = gpio_port_name(base_port);
411
Simon Glass6996c662020-11-28 17:50:03 -0700412 ret = device_bind(parent, parent->driver,
Simon Glass9030b392020-11-28 17:50:04 -0700413 plat->port_name, plat,
414 dev_ofnode(parent), &dev);
Simon Glassb0461042014-09-03 17:37:03 -0600415 if (ret)
416 return ret;
Simon Glassb0461042014-09-03 17:37:03 -0600417 }
418 }
419
420 return 0;
421}
422
423U_BOOT_DRIVER(gpio_tegra) = {
424 .name = "gpio_tegra",
425 .id = UCLASS_GPIO,
426 .of_match = tegra_gpio_ids,
427 .bind = gpio_tegra_bind,
428 .probe = gpio_tegra_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700429 .priv_auto = sizeof(struct tegra_port_info),
Simon Glassb0461042014-09-03 17:37:03 -0600430 .ops = &gpio_tegra_ops,
431};