gpio.h 4.4 KB

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