gpio.h 4.3 KB

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