2 * Copyright (c) 2009 Wind River Systems, Inc.
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
25 #include <asm/errno.h>
27 #define OMAP_GPIO_DIR_OUT 0
28 #define OMAP_GPIO_DIR_IN 1
32 #define GPIO_PER_BANK 32
36 void *base; /* address of registers in physical memory */
37 enum gpio_method method;
42 static inline int get_gpio_index(int gpio)
47 int gpio_is_valid(int gpio)
49 return (gpio >= 0) && (gpio < OMAP_MAX_GPIO);
52 static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
55 void *reg = bank->base;
58 switch (bank->method) {
59 case METHOD_GPIO_24XX:
74 * Get the direction of the GPIO by reading the GPIO_OE register
75 * corresponding to the specified bank.
77 static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
79 void *reg = bank->base;
82 switch (bank->method) {
83 case METHOD_GPIO_24XX:
93 return OMAP_GPIO_DIR_IN;
95 return OMAP_GPIO_DIR_OUT;
98 static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
101 void *reg = bank->base;
104 switch (bank->method) {
105 case METHOD_GPIO_24XX:
107 reg += OMAP_GPIO_SETDATAOUT;
109 reg += OMAP_GPIO_CLEARDATAOUT;
113 printf("omap3-gpio unknown bank method %s %d\n",
117 __raw_writel(l, reg);
120 static int _get_gpio_value(const struct gpio_bank *bank, int gpio)
122 void *reg = bank->base;
125 switch (bank->method) {
126 case METHOD_GPIO_24XX:
127 input = _get_gpio_direction(bank, gpio);
129 case OMAP_GPIO_DIR_IN:
130 reg += OMAP_GPIO_DATAIN;
132 case OMAP_GPIO_DIR_OUT:
133 reg += OMAP_GPIO_DATAOUT;
143 return (__raw_readl(reg) & (1 << gpio)) != 0;
146 #ifndef CONFIG_DM_GPIO
148 static inline const struct gpio_bank *get_gpio_bank(int gpio)
150 return &omap_gpio_bank[gpio >> 5];
153 static int check_gpio(int gpio)
155 if (!gpio_is_valid(gpio)) {
156 printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
163 * Set value of the specified gpio
165 int gpio_set_value(unsigned gpio, int value)
167 const struct gpio_bank *bank;
169 if (check_gpio(gpio) < 0)
171 bank = get_gpio_bank(gpio);
172 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
178 * Get value of the specified gpio
180 int gpio_get_value(unsigned gpio)
182 const struct gpio_bank *bank;
184 if (check_gpio(gpio) < 0)
186 bank = get_gpio_bank(gpio);
188 return _get_gpio_value(bank, get_gpio_index(gpio));
192 * Set gpio direction as input
194 int gpio_direction_input(unsigned gpio)
196 const struct gpio_bank *bank;
198 if (check_gpio(gpio) < 0)
201 bank = get_gpio_bank(gpio);
202 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
208 * Set gpio direction as output
210 int gpio_direction_output(unsigned gpio, int value)
212 const struct gpio_bank *bank;
214 if (check_gpio(gpio) < 0)
217 bank = get_gpio_bank(gpio);
218 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
219 _set_gpio_direction(bank, get_gpio_index(gpio), 0);
225 * Request a gpio before using it.
227 * NOTE: Argument 'label' is unused.
229 int gpio_request(unsigned gpio, const char *label)
231 if (check_gpio(gpio) < 0)
238 * Reset and free the gpio after using it.
240 int gpio_free(unsigned gpio)
245 #else /* new driver model interface CONFIG_DM_GPIO */
247 /* set GPIO pin 'gpio' as an input */
248 static int omap_gpio_direction_input(struct udevice *dev, unsigned offset)
250 struct gpio_bank *bank = dev_get_priv(dev);
252 /* Configure GPIO direction as input. */
253 _set_gpio_direction(bank, offset, 1);
258 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
259 static int omap_gpio_direction_output(struct udevice *dev, unsigned offset,
262 struct gpio_bank *bank = dev_get_priv(dev);
264 _set_gpio_dataout(bank, offset, value);
265 _set_gpio_direction(bank, offset, 0);
270 /* read GPIO IN value of pin 'gpio' */
271 static int omap_gpio_get_value(struct udevice *dev, unsigned offset)
273 struct gpio_bank *bank = dev_get_priv(dev);
275 return _get_gpio_value(bank, offset);
278 /* write GPIO OUT value to pin 'gpio' */
279 static int omap_gpio_set_value(struct udevice *dev, unsigned offset,
282 struct gpio_bank *bank = dev_get_priv(dev);
284 _set_gpio_dataout(bank, offset, value);
289 static int omap_gpio_get_function(struct udevice *dev, unsigned offset)
291 struct gpio_bank *bank = dev_get_priv(dev);
293 /* GPIOF_FUNC is not implemented yet */
294 if (_get_gpio_direction(bank, offset) == OMAP_GPIO_DIR_OUT)
300 static const struct dm_gpio_ops gpio_omap_ops = {
301 .direction_input = omap_gpio_direction_input,
302 .direction_output = omap_gpio_direction_output,
303 .get_value = omap_gpio_get_value,
304 .set_value = omap_gpio_set_value,
305 .get_function = omap_gpio_get_function,
308 static int omap_gpio_probe(struct udevice *dev)
310 struct gpio_bank *bank = dev_get_priv(dev);
311 struct omap_gpio_platdata *plat = dev_get_platdata(dev);
312 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
315 sprintf(name, "GPIO%d_", plat->bank_index);
319 uc_priv->bank_name = str;
320 uc_priv->gpio_count = GPIO_PER_BANK;
321 bank->base = (void *)plat->base;
322 bank->method = plat->method;
327 U_BOOT_DRIVER(gpio_omap) = {
330 .ops = &gpio_omap_ops,
331 .probe = omap_gpio_probe,
332 .priv_auto_alloc_size = sizeof(struct gpio_bank),
335 #endif /* CONFIG_DM_GPIO */