gpio-msm-v2.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/bitmap.h>
  14. #include <linux/bitops.h>
  15. #include <linux/delay.h>
  16. #include <linux/gpio.h>
  17. #include <linux/init.h>
  18. #include <linux/io.h>
  19. #include <linux/irq.h>
  20. #include <mach/msm_iomap.h>
  21. #include <mach/gpiomux.h>
  22. #include "gpio-msm-common.h"
  23. /* Bits of interest in the GPIO_IN_OUT register.
  24. */
  25. enum {
  26. GPIO_IN_BIT = 0,
  27. GPIO_OUT_BIT = 1
  28. };
  29. /* Bits of interest in the GPIO_INTR_STATUS register.
  30. */
  31. enum {
  32. INTR_STATUS_BIT = 0,
  33. };
  34. /* Bits of interest in the GPIO_CFG register.
  35. */
  36. enum {
  37. GPIO_OE_BIT = 9,
  38. };
  39. /* Bits of interest in the GPIO_INTR_CFG register.
  40. */
  41. enum {
  42. INTR_ENABLE_BIT = 0,
  43. INTR_POL_CTL_BIT = 1,
  44. INTR_DECT_CTL_BIT = 2,
  45. INTR_RAW_STATUS_EN_BIT = 3,
  46. };
  47. /* Codes of interest in GPIO_INTR_CFG_SU.
  48. */
  49. enum {
  50. TARGET_PROC_SCORPION = 4,
  51. TARGET_PROC_NONE = 7,
  52. };
  53. /*
  54. * There is no 'DC_POLARITY_LO' because the GIC is incapable
  55. * of asserting on falling edge or level-low conditions. Even though
  56. * the registers allow for low-polarity inputs, the case can never arise.
  57. */
  58. enum {
  59. DC_POLARITY_HI = BIT(11),
  60. DC_IRQ_ENABLE = BIT(3),
  61. };
  62. /*
  63. * When a GPIO triggers, two separate decisions are made, controlled
  64. * by two separate flags.
  65. *
  66. * - First, INTR_RAW_STATUS_EN controls whether or not the GPIO_INTR_STATUS
  67. * register for that GPIO will be updated to reflect the triggering of that
  68. * gpio. If this bit is 0, this register will not be updated.
  69. * - Second, INTR_ENABLE controls whether an interrupt is triggered.
  70. *
  71. * If INTR_ENABLE is set and INTR_RAW_STATUS_EN is NOT set, an interrupt
  72. * can be triggered but the status register will not reflect it.
  73. */
  74. #define INTR_RAW_STATUS_EN BIT(INTR_RAW_STATUS_EN_BIT)
  75. #define INTR_ENABLE BIT(INTR_ENABLE_BIT)
  76. #define INTR_DECT_CTL_EDGE BIT(INTR_DECT_CTL_BIT)
  77. #define INTR_POL_CTL_HI BIT(INTR_POL_CTL_BIT)
  78. #define GPIO_INTR_CFG_SU(gpio) (MSM_TLMM_BASE + 0x0400 + (0x04 * (gpio)))
  79. #define DIR_CONN_INTR_CFG_SU(irq) (MSM_TLMM_BASE + 0x0700 + (0x04 * (irq)))
  80. #define GPIO_CONFIG(gpio) (MSM_TLMM_BASE + 0x1000 + (0x10 * (gpio)))
  81. #define GPIO_IN_OUT(gpio) (MSM_TLMM_BASE + 0x1004 + (0x10 * (gpio)))
  82. #define GPIO_INTR_CFG(gpio) (MSM_TLMM_BASE + 0x1008 + (0x10 * (gpio)))
  83. #define GPIO_INTR_STATUS(gpio) (MSM_TLMM_BASE + 0x100c + (0x10 * (gpio)))
  84. static inline void set_gpio_bits(unsigned n, void __iomem *reg)
  85. {
  86. __raw_writel(__raw_readl(reg) | n, reg);
  87. }
  88. static inline void clr_gpio_bits(unsigned n, void __iomem *reg)
  89. {
  90. __raw_writel(__raw_readl(reg) & ~n, reg);
  91. }
  92. unsigned __msm_gpio_get_inout(unsigned gpio)
  93. {
  94. return __raw_readl(GPIO_IN_OUT(gpio)) & BIT(GPIO_IN_BIT);
  95. }
  96. void __msm_gpio_set_inout(unsigned gpio, unsigned val)
  97. {
  98. __raw_writel(val ? BIT(GPIO_OUT_BIT) : 0, GPIO_IN_OUT(gpio));
  99. }
  100. void __msm_gpio_set_config_direction(unsigned gpio, int input, int val)
  101. {
  102. if (input)
  103. clr_gpio_bits(BIT(GPIO_OE_BIT), GPIO_CONFIG(gpio));
  104. else {
  105. __msm_gpio_set_inout(gpio, val);
  106. set_gpio_bits(BIT(GPIO_OE_BIT), GPIO_CONFIG(gpio));
  107. }
  108. }
  109. void __msm_gpio_set_polarity(unsigned gpio, unsigned val)
  110. {
  111. if (val)
  112. clr_gpio_bits(INTR_POL_CTL_HI, GPIO_INTR_CFG(gpio));
  113. else
  114. set_gpio_bits(INTR_POL_CTL_HI, GPIO_INTR_CFG(gpio));
  115. }
  116. unsigned __msm_gpio_get_intr_status(unsigned gpio)
  117. {
  118. return __raw_readl(GPIO_INTR_STATUS(gpio)) &
  119. BIT(INTR_STATUS_BIT);
  120. }
  121. void __msm_gpio_set_intr_status(unsigned gpio)
  122. {
  123. __raw_writel(BIT(INTR_STATUS_BIT), GPIO_INTR_STATUS(gpio));
  124. }
  125. unsigned __msm_gpio_get_intr_config(unsigned gpio)
  126. {
  127. return __raw_readl(GPIO_INTR_CFG(gpio));
  128. }
  129. void __msm_gpio_set_intr_cfg_enable(unsigned gpio, unsigned val)
  130. {
  131. if (val) {
  132. set_gpio_bits(INTR_ENABLE, GPIO_INTR_CFG(gpio));
  133. } else {
  134. clr_gpio_bits(INTR_ENABLE, GPIO_INTR_CFG(gpio));
  135. }
  136. }
  137. unsigned __msm_gpio_get_intr_cfg_enable(unsigned gpio)
  138. {
  139. return __msm_gpio_get_intr_config(gpio) & INTR_ENABLE;
  140. }
  141. void __msm_gpio_set_intr_cfg_type(unsigned gpio, unsigned type)
  142. {
  143. unsigned cfg;
  144. /* RAW_STATUS_EN is left on for all gpio irqs. Due to the
  145. * internal circuitry of TLMM, toggling the RAW_STATUS
  146. * could cause the INTR_STATUS to be set for EDGE interrupts.
  147. */
  148. cfg = __msm_gpio_get_intr_config(gpio);
  149. cfg |= INTR_RAW_STATUS_EN;
  150. __raw_writel(cfg, GPIO_INTR_CFG(gpio));
  151. __raw_writel(TARGET_PROC_SCORPION, GPIO_INTR_CFG_SU(gpio));
  152. cfg = __msm_gpio_get_intr_config(gpio);
  153. if (type & IRQ_TYPE_EDGE_BOTH)
  154. cfg |= INTR_DECT_CTL_EDGE;
  155. else
  156. cfg &= ~INTR_DECT_CTL_EDGE;
  157. if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_HIGH))
  158. cfg |= INTR_POL_CTL_HI;
  159. else
  160. cfg &= ~INTR_POL_CTL_HI;
  161. __raw_writel(cfg, GPIO_INTR_CFG(gpio));
  162. /* Sometimes it might take a little while to update
  163. * the interrupt status after the RAW_STATUS is enabled
  164. * We clear the interrupt status before enabling the
  165. * interrupt in the unmask call-back.
  166. */
  167. udelay(5);
  168. }
  169. void __gpio_tlmm_config(unsigned config)
  170. {
  171. uint32_t flags;
  172. unsigned gpio = GPIO_PIN(config);
  173. flags = ((GPIO_DIR(config) << 9) & (0x1 << 9)) |
  174. ((GPIO_DRVSTR(config) << 6) & (0x7 << 6)) |
  175. ((GPIO_FUNC(config) << 2) & (0xf << 2)) |
  176. ((GPIO_PULL(config) & 0x3));
  177. __raw_writel(flags, GPIO_CONFIG(gpio));
  178. }
  179. void __msm_gpio_install_direct_irq(unsigned gpio, unsigned irq,
  180. unsigned int input_polarity)
  181. {
  182. uint32_t bits;
  183. __raw_writel(__raw_readl(GPIO_CONFIG(gpio)) | BIT(GPIO_OE_BIT),
  184. GPIO_CONFIG(gpio));
  185. __raw_writel(__raw_readl(GPIO_INTR_CFG(gpio)) &
  186. ~(INTR_RAW_STATUS_EN | INTR_ENABLE),
  187. GPIO_INTR_CFG(gpio));
  188. __raw_writel(DC_IRQ_ENABLE | TARGET_PROC_NONE,
  189. GPIO_INTR_CFG_SU(gpio));
  190. bits = TARGET_PROC_SCORPION | (gpio << 3);
  191. if (input_polarity)
  192. bits |= DC_POLARITY_HI;
  193. __raw_writel(bits, DIR_CONN_INTR_CFG_SU(irq));
  194. }