2 * Copyright (c) 2009 Wind River Systems, Inc.
3 * Tom Rix <Tom.Rix@windriver.com>
5 * SPDX-License-Identifier: GPL-2.0
7 * This work is derived from the linux 2.6.27 kernel source
8 * To fetch, use the kernel repository
9 * git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
10 * Use the v2.6.27 tag.
12 * Below is the original's header including its copyright
14 * linux/arch/arm/plat-omap/gpio.c
16 * Support functions for OMAP GPIO
18 * Copyright (C) 2003-2005 Nokia Corporation
19 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
24 #include <asm/errno.h>
26 #define OMAP_GPIO_DIR_OUT 0
27 #define OMAP_GPIO_DIR_IN 1
29 static inline int get_gpio_index(int gpio)
34 int gpio_is_valid(int gpio)
36 return (gpio >= 0) && (gpio < OMAP_MAX_GPIO);
39 static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
42 void *reg = bank->base;
45 switch (bank->method) {
46 case METHOD_GPIO_24XX:
61 * Get the direction of the GPIO by reading the GPIO_OE register
62 * corresponding to the specified bank.
64 static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
66 void *reg = bank->base;
69 switch (bank->method) {
70 case METHOD_GPIO_24XX:
80 return OMAP_GPIO_DIR_IN;
82 return OMAP_GPIO_DIR_OUT;
85 static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
88 void *reg = bank->base;
91 switch (bank->method) {
92 case METHOD_GPIO_24XX:
94 reg += OMAP_GPIO_SETDATAOUT;
96 reg += OMAP_GPIO_CLEARDATAOUT;
100 printf("omap3-gpio unknown bank method %s %d\n",
104 __raw_writel(l, reg);
107 static int _get_gpio_value(const struct gpio_bank *bank, int gpio)
109 void *reg = bank->base;
112 switch (bank->method) {
113 case METHOD_GPIO_24XX:
114 input = _get_gpio_direction(bank, gpio);
116 case OMAP_GPIO_DIR_IN:
117 reg += OMAP_GPIO_DATAIN;
119 case OMAP_GPIO_DIR_OUT:
120 reg += OMAP_GPIO_DATAOUT;
130 return (__raw_readl(reg) & (1 << gpio)) != 0;
133 static inline const struct gpio_bank *get_gpio_bank(int gpio)
135 return &omap_gpio_bank[gpio >> 5];
138 static int check_gpio(int gpio)
140 if (!gpio_is_valid(gpio)) {
141 printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
148 * Set value of the specified gpio
150 int gpio_set_value(unsigned gpio, int value)
152 const struct gpio_bank *bank;
154 if (check_gpio(gpio) < 0)
156 bank = get_gpio_bank(gpio);
157 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
163 * Get value of the specified gpio
165 int gpio_get_value(unsigned gpio)
167 const struct gpio_bank *bank;
169 if (check_gpio(gpio) < 0)
171 bank = get_gpio_bank(gpio);
173 return _get_gpio_value(bank, get_gpio_index(gpio));
177 * Set gpio direction as input
179 int gpio_direction_input(unsigned gpio)
181 const struct gpio_bank *bank;
183 if (check_gpio(gpio) < 0)
186 bank = get_gpio_bank(gpio);
187 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
193 * Set gpio direction as output
195 int gpio_direction_output(unsigned gpio, int value)
197 const struct gpio_bank *bank;
199 if (check_gpio(gpio) < 0)
202 bank = get_gpio_bank(gpio);
203 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
204 _set_gpio_direction(bank, get_gpio_index(gpio), 0);
210 * Request a gpio before using it.
212 * NOTE: Argument 'label' is unused.
214 int gpio_request(unsigned gpio, const char *label)
216 if (check_gpio(gpio) < 0)
223 * Reset and free the gpio after using it.
225 int gpio_free(unsigned gpio)