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 const struct gpio_bank *get_gpio_bank(int gpio)
31 return &omap_gpio_bank[gpio >> 5];
34 static inline int get_gpio_index(int gpio)
39 int gpio_is_valid(int gpio)
41 return (gpio >= 0) && (gpio < OMAP_MAX_GPIO);
44 static int check_gpio(int gpio)
46 if (!gpio_is_valid(gpio)) {
47 printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
53 static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
56 void *reg = bank->base;
59 switch (bank->method) {
60 case METHOD_GPIO_24XX:
75 * Get the direction of the GPIO by reading the GPIO_OE register
76 * corresponding to the specified bank.
78 static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
80 void *reg = bank->base;
83 switch (bank->method) {
84 case METHOD_GPIO_24XX:
94 return OMAP_GPIO_DIR_IN;
96 return OMAP_GPIO_DIR_OUT;
99 static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
102 void *reg = bank->base;
105 switch (bank->method) {
106 case METHOD_GPIO_24XX:
108 reg += OMAP_GPIO_SETDATAOUT;
110 reg += OMAP_GPIO_CLEARDATAOUT;
114 printf("omap3-gpio unknown bank method %s %d\n",
118 __raw_writel(l, reg);
122 * Set value of the specified gpio
124 int gpio_set_value(unsigned gpio, int value)
126 const struct gpio_bank *bank;
128 if (check_gpio(gpio) < 0)
130 bank = get_gpio_bank(gpio);
131 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
137 * Get value of the specified gpio
139 int gpio_get_value(unsigned gpio)
141 const struct gpio_bank *bank;
145 if (check_gpio(gpio) < 0)
147 bank = get_gpio_bank(gpio);
149 switch (bank->method) {
150 case METHOD_GPIO_24XX:
151 input = _get_gpio_direction(bank, get_gpio_index(gpio));
153 case OMAP_GPIO_DIR_IN:
154 reg += OMAP_GPIO_DATAIN;
156 case OMAP_GPIO_DIR_OUT:
157 reg += OMAP_GPIO_DATAOUT;
166 return (__raw_readl(reg)
167 & (1 << get_gpio_index(gpio))) != 0;
171 * Set gpio direction as input
173 int gpio_direction_input(unsigned gpio)
175 const struct gpio_bank *bank;
177 if (check_gpio(gpio) < 0)
180 bank = get_gpio_bank(gpio);
181 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
187 * Set gpio direction as output
189 int gpio_direction_output(unsigned gpio, int value)
191 const struct gpio_bank *bank;
193 if (check_gpio(gpio) < 0)
196 bank = get_gpio_bank(gpio);
197 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
198 _set_gpio_direction(bank, get_gpio_index(gpio), 0);
204 * Request a gpio before using it.
206 * NOTE: Argument 'label' is unused.
208 int gpio_request(unsigned gpio, const char *label)
210 if (check_gpio(gpio) < 0)
217 * Reset and free the gpio after using it.
219 int gpio_free(unsigned gpio)