gpio: tegra: use named constants
[platform/kernel/u-boot.git] / drivers / gpio / tegra_gpio.c
1 /*
2  * NVIDIA Tegra20 GPIO handling.
3  *  (C) Copyright 2010-2012,2015
4  *  NVIDIA Corporation <www.nvidia.com>
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
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>
15 #include <dm.h>
16 #include <malloc.h>
17 #include <errno.h>
18 #include <fdtdec.h>
19 #include <asm/io.h>
20 #include <asm/bitops.h>
21 #include <asm/arch/tegra.h>
22 #include <asm/gpio.h>
23 #include <dm/device-internal.h>
24 #include <dt-bindings/gpio/gpio.h>
25
26 DECLARE_GLOBAL_DATA_PTR;
27
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
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 };
38
39 /* Information about each port at run-time */
40 struct tegra_port_info {
41         struct gpio_ctlr_bank *bank;
42         int base_gpio;          /* Port number for this port (0, 1,.., n-1) */
43 };
44
45 /* Return config of pin 'gpio' as GPIO (1) or SFIO (0) */
46 static int get_config(unsigned gpio)
47 {
48         struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
49         struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
50         u32 u;
51         int type;
52
53         u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
54         type = (u >> GPIO_BIT(gpio)) & 1;
55
56         debug("get_config: port = %d, bit = %d is %s\n",
57                 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
58
59         return type ? CONFIG_GPIO : CONFIG_SFIO;
60 }
61
62 /* Config pin 'gpio' as GPIO or SFIO, based on 'type' */
63 static void set_config(unsigned gpio, int type)
64 {
65         struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
66         struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
67         u32 u;
68
69         debug("set_config: port = %d, bit = %d, %s\n",
70                 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
71
72         u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
73         if (type != CONFIG_SFIO)
74                 u |= 1 << GPIO_BIT(gpio);
75         else
76                 u &= ~(1 << GPIO_BIT(gpio));
77         writel(u, &bank->gpio_config[GPIO_PORT(gpio)]);
78 }
79
80 /* Return GPIO pin 'gpio' direction - 0 = input or 1 = output */
81 static int get_direction(unsigned gpio)
82 {
83         struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
84         struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
85         u32 u;
86         int dir;
87
88         u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
89         dir =  (u >> GPIO_BIT(gpio)) & 1;
90
91         debug("get_direction: port = %d, bit = %d, %s\n",
92                 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), dir ? "OUT" : "IN");
93
94         return dir ? DIRECTION_OUTPUT : DIRECTION_INPUT;
95 }
96
97 /* Config GPIO pin 'gpio' as input or output (OE) as per 'output' */
98 static void set_direction(unsigned gpio, int output)
99 {
100         struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
101         struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
102         u32 u;
103
104         debug("set_direction: port = %d, bit = %d, %s\n",
105                 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), output ? "OUT" : "IN");
106
107         u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
108         if (output != DIRECTION_INPUT)
109                 u |= 1 << GPIO_BIT(gpio);
110         else
111                 u &= ~(1 << GPIO_BIT(gpio));
112         writel(u, &bank->gpio_dir_out[GPIO_PORT(gpio)]);
113 }
114
115 /* set GPIO pin 'gpio' output bit as 0 or 1 as per 'high' */
116 static void set_level(unsigned gpio, int high)
117 {
118         struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
119         struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
120         u32 u;
121
122         debug("set_level: port = %d, bit %d == %d\n",
123                 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), high);
124
125         u = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
126         if (high)
127                 u |= 1 << GPIO_BIT(gpio);
128         else
129                 u &= ~(1 << GPIO_BIT(gpio));
130         writel(u, &bank->gpio_out[GPIO_PORT(gpio)]);
131 }
132
133 /*
134  * Generic_GPIO primitives.
135  */
136
137 /* set GPIO pin 'gpio' as an input */
138 static int tegra_gpio_direction_input(struct udevice *dev, unsigned offset)
139 {
140         struct tegra_port_info *state = dev_get_priv(dev);
141
142         /* Configure GPIO direction as input. */
143         set_direction(state->base_gpio + offset, DIRECTION_INPUT);
144
145         /* Enable the pin as a GPIO */
146         set_config(state->base_gpio + offset, 1);
147
148         return 0;
149 }
150
151 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
152 static int tegra_gpio_direction_output(struct udevice *dev, unsigned offset,
153                                        int value)
154 {
155         struct tegra_port_info *state = dev_get_priv(dev);
156         int gpio = state->base_gpio + offset;
157
158         /* Configure GPIO output value. */
159         set_level(gpio, value);
160
161         /* Configure GPIO direction as output. */
162         set_direction(gpio, DIRECTION_OUTPUT);
163
164         /* Enable the pin as a GPIO */
165         set_config(state->base_gpio + offset, 1);
166
167         return 0;
168 }
169
170 /* read GPIO IN value of pin 'gpio' */
171 static int tegra_gpio_get_value(struct udevice *dev, unsigned offset)
172 {
173         struct tegra_port_info *state = dev_get_priv(dev);
174         int gpio = state->base_gpio + offset;
175         int val;
176
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)]);
181
182         return (val >> GPIO_BIT(gpio)) & 1;
183 }
184
185 /* write GPIO OUT value to pin 'gpio' */
186 static int tegra_gpio_set_value(struct udevice *dev, unsigned offset, int value)
187 {
188         struct tegra_port_info *state = dev_get_priv(dev);
189         int gpio = state->base_gpio + offset;
190
191         debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n",
192               gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), value);
193
194         /* Configure GPIO output value. */
195         set_level(gpio, value);
196
197         return 0;
198 }
199
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:
207                         set_direction(config[i].gpio, DIRECTION_INPUT);
208                         break;
209                 case TEGRA_GPIO_INIT_OUT0:
210                         set_level(config[i].gpio, 0);
211                         set_direction(config[i].gpio, DIRECTION_OUTPUT);
212                         break;
213                 case TEGRA_GPIO_INIT_OUT1:
214                         set_level(config[i].gpio, 1);
215                         set_direction(config[i].gpio, DIRECTION_OUTPUT);
216                         break;
217                 }
218                 set_config(config[i].gpio, CONFIG_GPIO);
219         }
220 }
221
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
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
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
251 static const struct dm_gpio_ops gpio_tegra_ops = {
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,
257         .xlate                  = tegra_gpio_xlate,
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
267  */
268 static char *gpio_port_name(int base_port)
269 {
270         char *name, *s;
271
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';
279         }
280
281         return name;
282 }
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 {
292         struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
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;
320
321         /* If this is a child device, there is nothing to do here */
322         if (plat)
323                 return 0;
324
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
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);
340         ctlr = (struct gpio_ctlr *)dev_get_addr(parent);
341         }
342 #endif
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,
378         .flags  = DM_FLAG_PRE_RELOC,
379 };