blob: 01b8245c8d5b4fb73cef907347cda664b4a13422 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warren0c5403f2016-05-25 14:38:51 -06002/*
3 * Copyright (c) 2010-2016, NVIDIA CORPORATION.
4 * (based on tegra_gpio.c)
Stephen Warren0c5403f2016-05-25 14:38:51 -06005 */
6
Stephen Warren0c5403f2016-05-25 14:38:51 -06007#include <dm.h>
8#include <malloc.h>
9#include <errno.h>
10#include <fdtdec.h>
11#include <asm/io.h>
12#include <asm/bitops.h>
13#include <asm/gpio.h>
14#include <dm/device-internal.h>
15#include <dt-bindings/gpio/gpio.h>
16#include "tegra186_gpio_priv.h"
17
Stephen Warren0c5403f2016-05-25 14:38:51 -060018struct tegra186_gpio_port_data {
19 const char *name;
20 uint32_t offset;
21};
22
23struct tegra186_gpio_ctlr_data {
24 const struct tegra186_gpio_port_data *ports;
25 uint32_t port_count;
26};
27
Simon Glassb75b15b2020-12-03 16:55:23 -070028struct tegra186_gpio_plat {
Stephen Warren0c5403f2016-05-25 14:38:51 -060029 const char *name;
30 uint32_t *regs;
31};
32
33static uint32_t *tegra186_gpio_reg(struct udevice *dev, uint32_t reg,
34 uint32_t gpio)
35{
Simon Glass95588622020-12-22 19:30:28 -070036 struct tegra186_gpio_plat *plat = dev_get_plat(dev);
Stephen Warren0c5403f2016-05-25 14:38:51 -060037 uint32_t index = (reg + (gpio * TEGRA186_GPIO_PER_GPIO_STRIDE)) / 4;
38
39 return &(plat->regs[index]);
40}
41
42static int tegra186_gpio_set_out(struct udevice *dev, unsigned offset,
43 bool output)
44{
45 uint32_t *reg;
46 uint32_t rval;
47
48 reg = tegra186_gpio_reg(dev, TEGRA186_GPIO_OUTPUT_CONTROL, offset);
49 rval = readl(reg);
50 if (output)
51 rval &= ~TEGRA186_GPIO_OUTPUT_CONTROL_FLOATED;
52 else
53 rval |= TEGRA186_GPIO_OUTPUT_CONTROL_FLOATED;
54 writel(rval, reg);
55
56 reg = tegra186_gpio_reg(dev, TEGRA186_GPIO_ENABLE_CONFIG, offset);
57 rval = readl(reg);
58 if (output)
59 rval |= TEGRA186_GPIO_ENABLE_CONFIG_OUT;
60 else
61 rval &= ~TEGRA186_GPIO_ENABLE_CONFIG_OUT;
62 rval |= TEGRA186_GPIO_ENABLE_CONFIG_ENABLE;
63 writel(rval, reg);
64
65 return 0;
66}
67
68static int tegra186_gpio_set_val(struct udevice *dev, unsigned offset, bool val)
69{
70 uint32_t *reg;
71 uint32_t rval;
72
73 reg = tegra186_gpio_reg(dev, TEGRA186_GPIO_OUTPUT_VALUE, offset);
74 rval = readl(reg);
75 if (val)
76 rval |= TEGRA186_GPIO_OUTPUT_VALUE_HIGH;
77 else
78 rval &= ~TEGRA186_GPIO_OUTPUT_VALUE_HIGH;
79 writel(rval, reg);
80
81 return 0;
82}
83
84static int tegra186_gpio_direction_input(struct udevice *dev, unsigned offset)
85{
86 return tegra186_gpio_set_out(dev, offset, false);
87}
88
89static int tegra186_gpio_direction_output(struct udevice *dev, unsigned offset,
90 int value)
91{
92 int ret;
93
94 ret = tegra186_gpio_set_val(dev, offset, value != 0);
95 if (ret)
96 return ret;
97 return tegra186_gpio_set_out(dev, offset, true);
98}
99
100static int tegra186_gpio_get_value(struct udevice *dev, unsigned offset)
101{
102 uint32_t *reg;
103 uint32_t rval;
104
105 reg = tegra186_gpio_reg(dev, TEGRA186_GPIO_ENABLE_CONFIG, offset);
106 rval = readl(reg);
107
108 if (rval & TEGRA186_GPIO_ENABLE_CONFIG_OUT)
109 reg = tegra186_gpio_reg(dev, TEGRA186_GPIO_OUTPUT_VALUE,
110 offset);
111 else
112 reg = tegra186_gpio_reg(dev, TEGRA186_GPIO_INPUT, offset);
113
114 rval = readl(reg);
115 return !!rval;
116}
117
118static int tegra186_gpio_set_value(struct udevice *dev, unsigned offset,
119 int value)
120{
121 return tegra186_gpio_set_val(dev, offset, value != 0);
122}
123
124static int tegra186_gpio_get_function(struct udevice *dev, unsigned offset)
125{
126 uint32_t *reg;
127 uint32_t rval;
128
129 reg = tegra186_gpio_reg(dev, TEGRA186_GPIO_ENABLE_CONFIG, offset);
130 rval = readl(reg);
131 if (rval & TEGRA186_GPIO_ENABLE_CONFIG_OUT)
132 return GPIOF_OUTPUT;
133 else
134 return GPIOF_INPUT;
135}
136
137static int tegra186_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600138 struct ofnode_phandle_args *args)
Stephen Warren0c5403f2016-05-25 14:38:51 -0600139{
140 int gpio, port, ret;
141
142 gpio = args->args[0];
143 port = gpio / TEGRA186_GPIO_PER_GPIO_COUNT;
144 ret = device_get_child(dev, port, &desc->dev);
145 if (ret)
146 return ret;
147 desc->offset = gpio % TEGRA186_GPIO_PER_GPIO_COUNT;
148 desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
149
150 return 0;
151}
152
153static const struct dm_gpio_ops tegra186_gpio_ops = {
154 .direction_input = tegra186_gpio_direction_input,
155 .direction_output = tegra186_gpio_direction_output,
156 .get_value = tegra186_gpio_get_value,
157 .set_value = tegra186_gpio_set_value,
158 .get_function = tegra186_gpio_get_function,
159 .xlate = tegra186_gpio_xlate,
160};
161
162/**
163 * We have a top-level GPIO device with no actual GPIOs. It has a child device
164 * for each port within the controller.
165 */
166static int tegra186_gpio_bind(struct udevice *parent)
167{
Simon Glass95588622020-12-22 19:30:28 -0700168 struct tegra186_gpio_plat *parent_plat = dev_get_plat(parent);
Stephen Warren0c5403f2016-05-25 14:38:51 -0600169 struct tegra186_gpio_ctlr_data *ctlr_data =
170 (struct tegra186_gpio_ctlr_data *)dev_get_driver_data(parent);
171 uint32_t *regs;
172 int port, ret;
173
174 /* If this is a child device, there is nothing to do here */
175 if (parent_plat)
176 return 0;
177
Matthias Schiffer47331932023-09-27 15:33:34 +0200178 regs = dev_read_addr_name_ptr(parent, "gpio");
179 if (!regs)
Simon Glassf44b4bf2017-09-17 16:54:53 -0600180 return -EINVAL;
Stephen Warren0c5403f2016-05-25 14:38:51 -0600181
182 for (port = 0; port < ctlr_data->port_count; port++) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700183 struct tegra186_gpio_plat *plat;
Stephen Warren0c5403f2016-05-25 14:38:51 -0600184 struct udevice *dev;
185
186 plat = calloc(1, sizeof(*plat));
187 if (!plat)
188 return -ENOMEM;
189 plat->name = ctlr_data->ports[port].name;
190 plat->regs = &(regs[ctlr_data->ports[port].offset / 4]);
191
Simon Glass6996c662020-11-28 17:50:03 -0700192 ret = device_bind(parent, parent->driver, plat->name, plat,
Simon Glass9030b392020-11-28 17:50:04 -0700193 dev_ofnode(parent), &dev);
Stephen Warren0c5403f2016-05-25 14:38:51 -0600194 if (ret)
195 return ret;
Stephen Warren0c5403f2016-05-25 14:38:51 -0600196 }
197
198 return 0;
199}
200
201static int tegra186_gpio_probe(struct udevice *dev)
202{
Simon Glass95588622020-12-22 19:30:28 -0700203 struct tegra186_gpio_plat *plat = dev_get_plat(dev);
Stephen Warren0c5403f2016-05-25 14:38:51 -0600204 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
205
206 /* Only child devices have ports */
207 if (!plat)
208 return 0;
209
210 uc_priv->gpio_count = TEGRA186_GPIO_PER_GPIO_COUNT;
211 uc_priv->bank_name = plat->name;
212
213 return 0;
214}
215
216static const struct tegra186_gpio_port_data tegra186_gpio_main_ports[] = {
217 {"A", 0x2000},
218 {"B", 0x3000},
219 {"C", 0x3200},
220 {"D", 0x3400},
221 {"E", 0x2200},
222 {"F", 0x2400},
223 {"G", 0x4200},
224 {"H", 0x1000},
225 {"I", 0x0800},
226 {"J", 0x5000},
227 {"K", 0x5200},
228 {"L", 0x1200},
229 {"M", 0x5600},
230 {"N", 0x0000},
231 {"O", 0x0200},
232 {"P", 0x4000},
233 {"Q", 0x0400},
234 {"R", 0x0a00},
235 {"T", 0x0600},
236 {"X", 0x1400},
237 {"Y", 0x1600},
238 {"BB", 0x2600},
239 {"CC", 0x5400},
240};
241
242static const struct tegra186_gpio_ctlr_data tegra186_gpio_main_data = {
243 .ports = tegra186_gpio_main_ports,
244 .port_count = ARRAY_SIZE(tegra186_gpio_main_ports),
245};
246
247static const struct tegra186_gpio_port_data tegra186_gpio_aon_ports[] = {
248 {"S", 0x0200},
249 {"U", 0x0400},
250 {"V", 0x0800},
251 {"W", 0x0a00},
252 {"Z", 0x0e00},
253 {"AA", 0x0c00},
254 {"EE", 0x0600},
255 {"FF", 0x0000},
256};
257
258static const struct tegra186_gpio_ctlr_data tegra186_gpio_aon_data = {
259 .ports = tegra186_gpio_aon_ports,
260 .port_count = ARRAY_SIZE(tegra186_gpio_aon_ports),
261};
262
263static const struct udevice_id tegra186_gpio_ids[] = {
264 {
265 .compatible = "nvidia,tegra186-gpio",
266 .data = (ulong)&tegra186_gpio_main_data,
267 },
268 {
269 .compatible = "nvidia,tegra186-gpio-aon",
270 .data = (ulong)&tegra186_gpio_aon_data,
271 },
272 { }
273};
274
275U_BOOT_DRIVER(tegra186_gpio) = {
276 .name = "tegra186_gpio",
277 .id = UCLASS_GPIO,
278 .of_match = tegra186_gpio_ids,
279 .bind = tegra186_gpio_bind,
280 .probe = tegra186_gpio_probe,
281 .ops = &tegra186_gpio_ops,
Stephen Warren0c5403f2016-05-25 14:38:51 -0600282};