gpio.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * TI DaVinci GPIO Support
  3. *
  4. * Copyright (c) 2006 David Brownell
  5. * Copyright (c) 2007, MontaVista Software, Inc. <source@mvista.com>
  6. *
  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.
  11. */
  12. #ifndef __DAVINCI_GPIO_H
  13. #define __DAVINCI_GPIO_H
  14. #include <linux/io.h>
  15. #include <asm-generic/gpio.h>
  16. #include <mach/irqs.h>
  17. #include <mach/common.h>
  18. #define DAVINCI_GPIO_BASE 0x01C67000
  19. /*
  20. * basic gpio routines
  21. *
  22. * board-specific init should be done by arch/.../.../board-XXX.c (maybe
  23. * initializing banks together) rather than boot loaders; kexec() won't
  24. * go through boot loaders.
  25. *
  26. * the gpio clock will be turned on when gpios are used, and you may also
  27. * need to pay attention to PINMUX registers to be sure those pins are
  28. * used as gpios, not with other peripherals.
  29. *
  30. * On-chip GPIOs are numbered 0..(DAVINCI_N_GPIO-1). For documentation,
  31. * and maybe for later updates, code may write GPIO(N). These may be
  32. * all 1.8V signals, all 3.3V ones, or a mix of the two. A given chip
  33. * may not support all the GPIOs in that range.
  34. *
  35. * GPIOs can also be on external chips, numbered after the ones built-in
  36. * to the DaVinci chip. For now, they won't be usable as IRQ sources.
  37. */
  38. #define GPIO(X) (X) /* 0 <= X <= (DAVINCI_N_GPIO - 1) */
  39. /* Convert GPIO signal to GPIO pin number */
  40. #define GPIO_TO_PIN(bank, gpio) (16 * (bank) + (gpio))
  41. struct davinci_gpio_regs {
  42. u32 dir;
  43. u32 out_data;
  44. u32 set_data;
  45. u32 clr_data;
  46. u32 in_data;
  47. u32 set_rising;
  48. u32 clr_rising;
  49. u32 set_falling;
  50. u32 clr_falling;
  51. u32 intstat;
  52. };
  53. struct davinci_gpio_controller {
  54. struct davinci_gpio_regs __iomem *regs;
  55. struct gpio_chip chip;
  56. int irq_base;
  57. };
  58. /* The __gpio_to_controller() and __gpio_mask() functions inline to constants
  59. * with constant parameters; or in outlined code they execute at runtime.
  60. *
  61. * You'd access the controller directly when reading or writing more than
  62. * one gpio value at a time, and to support wired logic where the value
  63. * being driven by the cpu need not match the value read back.
  64. *
  65. * These are NOT part of the cross-platform GPIO interface
  66. */
  67. static inline struct davinci_gpio_regs __iomem *
  68. __gpio_to_controller(unsigned gpio)
  69. {
  70. void __iomem *ptr;
  71. void __iomem *base = davinci_soc_info.gpio_base;
  72. if (gpio < 32 * 1)
  73. ptr = base + 0x10;
  74. else if (gpio < 32 * 2)
  75. ptr = base + 0x38;
  76. else if (gpio < 32 * 3)
  77. ptr = base + 0x60;
  78. else if (gpio < 32 * 4)
  79. ptr = base + 0x88;
  80. else if (gpio < 32 * 5)
  81. ptr = base + 0xb0;
  82. else
  83. ptr = NULL;
  84. return ptr;
  85. }
  86. static inline u32 __gpio_mask(unsigned gpio)
  87. {
  88. return 1 << (gpio % 32);
  89. }
  90. /* The get/set/clear functions will inline when called with constant
  91. * parameters referencing built-in GPIOs, for low-overhead bitbanging.
  92. *
  93. * Otherwise, calls with variable parameters or referencing external
  94. * GPIOs (e.g. on GPIO expander chips) use outlined functions.
  95. */
  96. static inline void gpio_set_value(unsigned gpio, int value)
  97. {
  98. if (__builtin_constant_p(value) && gpio < DAVINCI_N_GPIO) {
  99. struct davinci_gpio_regs __iomem *g;
  100. u32 mask;
  101. g = __gpio_to_controller(gpio);
  102. mask = __gpio_mask(gpio);
  103. if (value)
  104. __raw_writel(mask, &g->set_data);
  105. else
  106. __raw_writel(mask, &g->clr_data);
  107. return;
  108. }
  109. __gpio_set_value(gpio, value);
  110. }
  111. /* Returns zero or nonzero; works for gpios configured as inputs OR
  112. * as outputs, at least for built-in GPIOs.
  113. *
  114. * NOTE: for built-in GPIOs, changes in reported values are synchronized
  115. * to the GPIO clock. This is easily seen after calling gpio_set_value()
  116. * and then immediately gpio_get_value(), where the gpio_get_value() will
  117. * return the old value until the GPIO clock ticks and the new value gets
  118. * latched.
  119. */
  120. static inline int gpio_get_value(unsigned gpio)
  121. {
  122. struct davinci_gpio_regs __iomem *g;
  123. if (!__builtin_constant_p(gpio) || gpio >= DAVINCI_N_GPIO)
  124. return __gpio_get_value(gpio);
  125. g = __gpio_to_controller(gpio);
  126. return __gpio_mask(gpio) & __raw_readl(&g->in_data);
  127. }
  128. static inline int gpio_cansleep(unsigned gpio)
  129. {
  130. if (__builtin_constant_p(gpio) && gpio < DAVINCI_N_GPIO)
  131. return 0;
  132. else
  133. return __gpio_cansleep(gpio);
  134. }
  135. static inline int gpio_to_irq(unsigned gpio)
  136. {
  137. return __gpio_to_irq(gpio);
  138. }
  139. static inline int irq_to_gpio(unsigned irq)
  140. {
  141. /* don't support the reverse mapping */
  142. return -ENOSYS;
  143. }
  144. #endif /* __DAVINCI_GPIO_H */