2 * TI DaVinci GPIO Support
4 * Copyright (c) 2006 David Brownell
5 * Copyright (c) 2007, MontaVista Software, Inc. <source@mvista.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #ifndef __DAVINCI_GPIO_H
14 #define __DAVINCI_GPIO_H
19 * board-specific init should be done by arch/.../.../board-XXX.c (maybe
20 * initializing banks together) rather than boot loaders; kexec() won't
21 * go through boot loaders.
23 * the gpio clock will be turned on when gpios are used, and you may also
24 * need to pay attention to PINMUX0 and PINMUX1 to be sure those pins are
25 * used as gpios, not with other peripherals.
27 * GPIOs are numbered 0..(DAVINCI_N_GPIO-1). For documentation, and maybe
28 * for later updates, code should write GPIO(N) or:
29 * - GPIOV18(N) for 1.8V pins, N in 0..53; same as GPIO(0)..GPIO(53)
30 * - GPIOV33(N) for 3.3V pins, N in 0..17; same as GPIO(54)..GPIO(70)
32 * For GPIO IRQs use gpio_to_irq(GPIO(N)) or gpio_to_irq(GPIOV33(N)) etc
33 * for now, that's != GPIO(N)
35 #define GPIO(X) (X) /* 0 <= X <= 70 */
36 #define GPIOV18(X) (X) /* 1.8V i/o; 0 <= X <= 53 */
37 #define GPIOV33(X) ((X)+54) /* 3.3V i/o; 0 <= X <= 17 */
39 struct gpio_controller {
52 /* The __gpio_to_controller() and __gpio_mask() functions inline to constants
53 * with constant parameters; or in outlined code they execute at runtime.
55 * You'd access the controller directly when reading or writing more than
56 * one gpio value at a time, and to support wired logic where the value
57 * being driven by the cpu need not match the value read back.
59 * These are NOT part of the cross-platform GPIO interface
61 static inline struct gpio_controller *__iomem
62 __gpio_to_controller(unsigned gpio)
67 ptr = (void *__iomem)IO_ADDRESS(DAVINCI_GPIO_BASE + 0x10);
69 ptr = (void *__iomem)IO_ADDRESS(DAVINCI_GPIO_BASE + 0x38);
70 else if (gpio < DAVINCI_N_GPIO)
71 ptr = (void *__iomem)IO_ADDRESS(DAVINCI_GPIO_BASE + 0x60);
77 static inline u32 __gpio_mask(unsigned gpio)
79 return 1 << (gpio % 32);
82 /* The get/set/clear functions will inline when called with constant
83 * parameters, for low-overhead bitbanging. Illegal constant parameters
84 * cause link-time errors.
86 * Otherwise, calls with variable parameters use outlined functions.
88 extern int __error_inval_gpio(void);
90 extern void __gpio_set(unsigned gpio, int value);
91 extern int __gpio_get(unsigned gpio);
93 static inline void gpio_set_value(unsigned gpio, int value)
95 if (__builtin_constant_p(value)) {
96 struct gpio_controller *__iomem g;
99 if (gpio >= DAVINCI_N_GPIO)
100 __error_inval_gpio();
102 g = __gpio_to_controller(gpio);
103 mask = __gpio_mask(gpio);
105 __raw_writel(mask, &g->set_data);
107 __raw_writel(mask, &g->clr_data);
111 __gpio_set(gpio, value);
114 /* Returns zero or nonzero; works for gpios configured as inputs OR
117 * NOTE: changes in reported values are synchronized to the GPIO clock.
118 * This is most easily seen after calling gpio_set_value() and then immediatly
119 * gpio_get_value(), where the gpio_get_value() would return the old value
120 * until the GPIO clock ticks and the new value gets latched.
123 static inline int gpio_get_value(unsigned gpio)
125 struct gpio_controller *__iomem g;
127 if (!__builtin_constant_p(gpio))
128 return __gpio_get(gpio);
130 if (gpio >= DAVINCI_N_GPIO)
131 return __error_inval_gpio();
133 g = __gpio_to_controller(gpio);
134 return !!(__gpio_mask(gpio) & __raw_readl(&g->in_data));
137 /* powerup default direction is IN */
138 extern int gpio_direction_input(unsigned gpio);
139 extern int gpio_direction_output(unsigned gpio, int value);
141 #include <asm-generic/gpio.h> /* cansleep wrappers */
143 extern int gpio_request(unsigned gpio, const char *tag);
144 extern void gpio_free(unsigned gpio);
146 static inline int gpio_to_irq(unsigned gpio)
148 return DAVINCI_N_AINTC_IRQ + gpio;
151 static inline int irq_to_gpio(unsigned irq)
153 return irq - DAVINCI_N_AINTC_IRQ;
156 #endif /* __DAVINCI_GPIO_H */