gpio.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/hardware.h>
  16. /*
  17. * basic gpio routines
  18. *
  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.
  22. *
  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.
  26. *
  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)
  31. *
  32. * For GPIO IRQs use gpio_to_irq(GPIO(N)) or gpio_to_irq(GPIOV33(N)) etc
  33. * for now, that's != GPIO(N)
  34. */
  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 */
  38. struct gpio_controller {
  39. u32 dir;
  40. u32 out_data;
  41. u32 set_data;
  42. u32 clr_data;
  43. u32 in_data;
  44. u32 set_rising;
  45. u32 clr_rising;
  46. u32 set_falling;
  47. u32 clr_falling;
  48. u32 intstat;
  49. };
  50. /* The __gpio_to_controller() and __gpio_mask() functions inline to constants
  51. * with constant parameters; or in outlined code they execute at runtime.
  52. *
  53. * You'd access the controller directly when reading or writing more than
  54. * one gpio value at a time, and to support wired logic where the value
  55. * being driven by the cpu need not match the value read back.
  56. *
  57. * These are NOT part of the cross-platform GPIO interface
  58. */
  59. static inline struct gpio_controller *__iomem
  60. __gpio_to_controller(unsigned gpio)
  61. {
  62. void *__iomem ptr;
  63. if (gpio < 32)
  64. ptr = (void *__iomem)IO_ADDRESS(DAVINCI_GPIO_BASE + 0x10);
  65. else if (gpio < 64)
  66. ptr = (void *__iomem)IO_ADDRESS(DAVINCI_GPIO_BASE + 0x38);
  67. else if (gpio < DAVINCI_N_GPIO)
  68. ptr = (void *__iomem)IO_ADDRESS(DAVINCI_GPIO_BASE + 0x60);
  69. else
  70. ptr = NULL;
  71. return ptr;
  72. }
  73. static inline u32 __gpio_mask(unsigned gpio)
  74. {
  75. return 1 << (gpio % 32);
  76. }
  77. /* The get/set/clear functions will inline when called with constant
  78. * parameters, for low-overhead bitbanging. Illegal constant parameters
  79. * cause link-time errors.
  80. *
  81. * Otherwise, calls with variable parameters use outlined functions.
  82. */
  83. extern int __error_inval_gpio(void);
  84. extern void __gpio_set(unsigned gpio, int value);
  85. extern int __gpio_get(unsigned gpio);
  86. static inline void gpio_set_value(unsigned gpio, int value)
  87. {
  88. if (__builtin_constant_p(value)) {
  89. struct gpio_controller *__iomem g;
  90. u32 mask;
  91. if (gpio >= DAVINCI_N_GPIO)
  92. __error_inval_gpio();
  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(gpio, value);
  102. }
  103. /* Returns zero or nonzero; works for gpios configured as inputs OR
  104. * as outputs.
  105. *
  106. * NOTE: changes in reported values are synchronized to the GPIO clock.
  107. * This is most easily seen after calling gpio_set_value() and then immediatly
  108. * gpio_get_value(), where the gpio_get_value() would return the old value
  109. * until the GPIO clock ticks and the new value gets latched.
  110. */
  111. static inline int gpio_get_value(unsigned gpio)
  112. {
  113. struct gpio_controller *__iomem g;
  114. if (!__builtin_constant_p(gpio))
  115. return __gpio_get(gpio);
  116. if (gpio >= DAVINCI_N_GPIO)
  117. return __error_inval_gpio();
  118. g = __gpio_to_controller(gpio);
  119. return !!(__gpio_mask(gpio) & __raw_readl(&g->in_data));
  120. }
  121. /* powerup default direction is IN */
  122. extern int gpio_direction_input(unsigned gpio);
  123. extern int gpio_direction_output(unsigned gpio, int value);
  124. #include <asm-generic/gpio.h> /* cansleep wrappers */
  125. extern int gpio_request(unsigned gpio, const char *tag);
  126. extern void gpio_free(unsigned gpio);
  127. static inline int gpio_to_irq(unsigned gpio)
  128. {
  129. return DAVINCI_N_AINTC_IRQ + gpio;
  130. }
  131. static inline int irq_to_gpio(unsigned irq)
  132. {
  133. return irq - DAVINCI_N_AINTC_IRQ;
  134. }
  135. #endif /* __DAVINCI_GPIO_H */