2 * NVIDIA Tegra2 GPIO handling.
3 * (C) Copyright 2010,2011
4 * NVIDIA Corporation <www.nvidia.com>
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * Based on (mostly copied from) kw_gpio.c based Linux 2.6 kernel driver.
27 * Tom Warren (twarren@nvidia.com)
32 #include <asm/bitops.h>
33 #include <asm/arch/tegra2.h>
43 static struct gpio_names {
44 char name[GPIO_NAME_SIZE];
45 } gpio_names[MAX_NUM_GPIOS];
47 static char *get_name(int i)
49 return *gpio_names[i].name ? gpio_names[i].name : "UNKNOWN";
52 /* Return config of pin 'gp' as GPIO (1) or SFPIO (0) */
53 static int get_config(int gp)
55 struct gpio_ctlr *gpio = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
56 struct gpio_ctlr_bank *bank = &gpio->gpio_bank[GPIO_BANK(gp)];
60 u = readl(&bank->gpio_config[GPIO_PORT(gp)]);
61 type = (u >> GPIO_BIT(gp)) & 1;
63 debug("get_config: port = %d, bit = %d is %s\n",
64 GPIO_FULLPORT(gp), GPIO_BIT(gp), type ? "GPIO" : "SFPIO");
69 /* Config pin 'gp' as GPIO or SFPIO, based on 'type' */
70 static void set_config(int gp, int type)
72 struct gpio_ctlr *gpio = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
73 struct gpio_ctlr_bank *bank = &gpio->gpio_bank[GPIO_BANK(gp)];
76 debug("set_config: port = %d, bit = %d, %s\n",
77 GPIO_FULLPORT(gp), GPIO_BIT(gp), type ? "GPIO" : "SFPIO");
79 u = readl(&bank->gpio_config[GPIO_PORT(gp)]);
81 u |= 1 << GPIO_BIT(gp);
83 u &= ~(1 << GPIO_BIT(gp));
84 writel(u, &bank->gpio_config[GPIO_PORT(gp)]);
87 /* Return GPIO pin 'gp' direction - 0 = input or 1 = output */
88 static int get_direction(int gp)
90 struct gpio_ctlr *gpio = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
91 struct gpio_ctlr_bank *bank = &gpio->gpio_bank[GPIO_BANK(gp)];
95 u = readl(&bank->gpio_dir_out[GPIO_PORT(gp)]);
96 dir = (u >> GPIO_BIT(gp)) & 1;
98 debug("get_direction: port = %d, bit = %d, %s\n",
99 GPIO_FULLPORT(gp), GPIO_BIT(gp), dir ? "OUT" : "IN");
104 /* Config GPIO pin 'gp' as input or output (OE) as per 'output' */
105 static void set_direction(int gp, int output)
107 struct gpio_ctlr *gpio = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
108 struct gpio_ctlr_bank *bank = &gpio->gpio_bank[GPIO_BANK(gp)];
111 debug("set_direction: port = %d, bit = %d, %s\n",
112 GPIO_FULLPORT(gp), GPIO_BIT(gp), output ? "OUT" : "IN");
114 u = readl(&bank->gpio_dir_out[GPIO_PORT(gp)]);
116 u |= 1 << GPIO_BIT(gp);
118 u &= ~(1 << GPIO_BIT(gp));
119 writel(u, &bank->gpio_dir_out[GPIO_PORT(gp)]);
122 /* set GPIO pin 'gp' output bit as 0 or 1 as per 'high' */
123 static void set_level(int gp, int high)
125 struct gpio_ctlr *gpio = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
126 struct gpio_ctlr_bank *bank = &gpio->gpio_bank[GPIO_BANK(gp)];
129 debug("set_level: port = %d, bit %d == %d\n",
130 GPIO_FULLPORT(gp), GPIO_BIT(gp), high);
132 u = readl(&bank->gpio_out[GPIO_PORT(gp)]);
134 u |= 1 << GPIO_BIT(gp);
136 u &= ~(1 << GPIO_BIT(gp));
137 writel(u, &bank->gpio_out[GPIO_PORT(gp)]);
141 * Generic_GPIO primitives.
144 int gpio_request(int gp, const char *label)
146 if (gp >= MAX_NUM_GPIOS)
149 strncpy(gpio_names[gp].name, label, GPIO_NAME_SIZE);
150 gpio_names[gp].name[GPIO_NAME_SIZE - 1] = '\0';
152 /* Configure as a GPIO */
158 void gpio_free(int gp)
162 /* read GPIO OUT value of pin 'gp' */
163 static int gpio_get_output_value(int gp)
165 struct gpio_ctlr *gpio = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
166 struct gpio_ctlr_bank *bank = &gpio->gpio_bank[GPIO_BANK(gp)];
169 debug("gpio_get_output_value: pin = %d (port %d:bit %d)\n",
170 gp, GPIO_FULLPORT(gp), GPIO_BIT(gp));
172 val = readl(&bank->gpio_out[GPIO_PORT(gp)]);
174 return (val >> GPIO_BIT(gp)) & 1;
177 void gpio_toggle_value(int gp)
179 gpio_set_value(gp, !gpio_get_output_value(gp));
182 /* set GPIO pin 'gp' as an input */
183 int gpio_direction_input(int gp)
185 debug("gpio_direction_input: pin = %d (port %d:bit %d)\n",
186 gp, GPIO_FULLPORT(gp), GPIO_BIT(gp));
188 /* Configure GPIO direction as input. */
189 set_direction(gp, 0);
194 /* set GPIO pin 'gp' as an output, with polarity 'value' */
195 int gpio_direction_output(int gp, int value)
197 debug("gpio_direction_output: pin = %d (port %d:bit %d) = %s\n",
198 gp, GPIO_FULLPORT(gp), GPIO_BIT(gp), value ? "HIGH" : "LOW");
200 /* Configure GPIO output value. */
201 set_level(gp, value);
203 /* Configure GPIO direction as output. */
204 set_direction(gp, 1);
209 /* read GPIO IN value of pin 'gp' */
210 int gpio_get_value(int gp)
212 struct gpio_ctlr *gpio = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
213 struct gpio_ctlr_bank *bank = &gpio->gpio_bank[GPIO_BANK(gp)];
216 debug("gpio_get_value: pin = %d (port %d:bit %d)\n",
217 gp, GPIO_FULLPORT(gp), GPIO_BIT(gp));
219 val = readl(&bank->gpio_in[GPIO_PORT(gp)]);
221 return (val >> GPIO_BIT(gp)) & 1;
224 /* write GPIO OUT value to pin 'gp' */
225 void gpio_set_value(int gp, int value)
227 debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n",
228 gp, GPIO_FULLPORT(gp), GPIO_BIT(gp), value);
230 /* Configure GPIO output value. */
231 set_level(gp, value);
235 * Display Tegra GPIO information
241 for (c = 0; c < MAX_NUM_GPIOS; c++) {
242 type = get_config(c); /* GPIO, not SFPIO */
244 printf("GPIO_%d:\t%s is an %s, ", c,
246 get_direction(c) ? "OUTPUT" : "INPUT");
247 if (get_direction(c))
248 printf("value = %d", gpio_get_output_value(c));
250 printf("value = %d", gpio_get_value(c));