gpio-msm-v3.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* Copyright (c) 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 = 4,
  46. INTR_TARGET_PROC_BIT = 5,
  47. INTR_DIR_CONN_EN_BIT = 8,
  48. };
  49. /*
  50. * There is no 'DC_POLARITY_LO' because the GIC is incapable
  51. * of asserting on falling edge or level-low conditions. Even though
  52. * the registers allow for low-polarity inputs, the case can never arise.
  53. */
  54. enum {
  55. DC_GPIO_SEL_BIT = 0,
  56. DC_POLARITY_BIT = 8,
  57. };
  58. /*
  59. * When a GPIO triggers, two separate decisions are made, controlled
  60. * by two separate flags.
  61. *
  62. * - First, INTR_RAW_STATUS_EN controls whether or not the GPIO_INTR_STATUS
  63. * register for that GPIO will be updated to reflect the triggering of that
  64. * gpio. If this bit is 0, this register will not be updated.
  65. * - Second, INTR_ENABLE controls whether an interrupt is triggered.
  66. *
  67. * If INTR_ENABLE is set and INTR_RAW_STATUS_EN is NOT set, an interrupt
  68. * can be triggered but the status register will not reflect it.
  69. */
  70. #define INTR_RAW_STATUS_EN BIT(INTR_RAW_STATUS_EN_BIT)
  71. #define INTR_ENABLE BIT(INTR_ENABLE_BIT)
  72. #define INTR_POL_CTL_HI BIT(INTR_POL_CTL_BIT)
  73. #define INTR_DIR_CONN_EN BIT(INTR_DIR_CONN_EN_BIT)
  74. #define DC_POLARITY_HI BIT(DC_POLARITY_BIT)
  75. #define INTR_TARGET_PROC_APPS (4 << INTR_TARGET_PROC_BIT)
  76. #define INTR_TARGET_PROC_NONE (7 << INTR_TARGET_PROC_BIT)
  77. #define INTR_DECT_CTL_LEVEL (0 << INTR_DECT_CTL_BIT)
  78. #define INTR_DECT_CTL_POS_EDGE (1 << INTR_DECT_CTL_BIT)
  79. #define INTR_DECT_CTL_NEG_EDGE (2 << INTR_DECT_CTL_BIT)
  80. #define INTR_DECT_CTL_DUAL_EDGE (3 << INTR_DECT_CTL_BIT)
  81. #define INTR_DECT_CTL_MASK (3 << INTR_DECT_CTL_BIT)
  82. #define GPIO_CONFIG(gpio) (MSM_TLMM_BASE + 0x1000 + (0x10 * (gpio)))
  83. #define GPIO_IN_OUT(gpio) (MSM_TLMM_BASE + 0x1004 + (0x10 * (gpio)))
  84. #define GPIO_INTR_CFG(gpio) (MSM_TLMM_BASE + 0x1008 + (0x10 * (gpio)))
  85. #define GPIO_INTR_STATUS(gpio) (MSM_TLMM_BASE + 0x100c + (0x10 * (gpio)))
  86. #define GPIO_DIR_CONN_INTR(intr) (MSM_TLMM_BASE + 0x2800 + (0x04 * (intr)))
  87. static inline void set_gpio_bits(unsigned n, void __iomem *reg)
  88. {
  89. __raw_writel(__raw_readl(reg) | n, reg);
  90. }
  91. static inline void clr_gpio_bits(unsigned n, void __iomem *reg)
  92. {
  93. __raw_writel(__raw_readl(reg) & ~n, reg);
  94. }
  95. unsigned __msm_gpio_get_inout(unsigned gpio)
  96. {
  97. return __raw_readl(GPIO_IN_OUT(gpio)) & BIT(GPIO_IN_BIT);
  98. }
  99. void __msm_gpio_set_inout(unsigned gpio, unsigned val)
  100. {
  101. __raw_writel(val ? BIT(GPIO_OUT_BIT) : 0, GPIO_IN_OUT(gpio));
  102. }
  103. void __msm_gpio_set_config_direction(unsigned gpio, int input, int val)
  104. {
  105. if (input) {
  106. clr_gpio_bits(BIT(GPIO_OE_BIT), GPIO_CONFIG(gpio));
  107. } else {
  108. __msm_gpio_set_inout(gpio, val);
  109. set_gpio_bits(BIT(GPIO_OE_BIT), GPIO_CONFIG(gpio));
  110. }
  111. }
  112. static inline void set_gpio_bits_no_log(unsigned n, void __iomem *reg)
  113. {
  114. __raw_writel_no_log(__raw_readl_no_log(reg) | n, reg);
  115. }
  116. static inline void clr_gpio_bits_no_log(unsigned n, void __iomem *reg)
  117. {
  118. __raw_writel_no_log(__raw_readl_no_log(reg) & ~n, reg);
  119. }
  120. unsigned __msm_gpio_get_inout_no_log(unsigned gpio)
  121. {
  122. return __raw_readl_no_log(GPIO_IN_OUT(gpio)) & BIT(GPIO_IN_BIT);
  123. }
  124. void __msm_gpio_set_inout_no_log(unsigned gpio, unsigned val)
  125. {
  126. __raw_writel_no_log(val ? BIT(GPIO_OUT_BIT) : 0, GPIO_IN_OUT(gpio));
  127. }
  128. void __msm_gpio_set_config_direction_no_log(unsigned gpio, int input, int val)
  129. {
  130. if (input) {
  131. clr_gpio_bits_no_log(BIT(GPIO_OE_BIT), GPIO_CONFIG(gpio));
  132. } else {
  133. __msm_gpio_set_inout_no_log(gpio, val);
  134. set_gpio_bits_no_log(BIT(GPIO_OE_BIT), GPIO_CONFIG(gpio));
  135. }
  136. }
  137. void __msm_gpio_set_polarity(unsigned gpio, unsigned val)
  138. {
  139. if (val)
  140. clr_gpio_bits(INTR_POL_CTL_HI, GPIO_INTR_CFG(gpio));
  141. else
  142. set_gpio_bits(INTR_POL_CTL_HI, GPIO_INTR_CFG(gpio));
  143. }
  144. unsigned __msm_gpio_get_intr_status(unsigned gpio)
  145. {
  146. return __raw_readl(GPIO_INTR_STATUS(gpio)) &
  147. BIT(INTR_STATUS_BIT);
  148. }
  149. void __msm_gpio_set_intr_status(unsigned gpio)
  150. {
  151. __raw_writel(0, GPIO_INTR_STATUS(gpio));
  152. }
  153. unsigned __msm_gpio_get_intr_config(unsigned gpio)
  154. {
  155. return __raw_readl(GPIO_INTR_CFG(gpio));
  156. }
  157. void __msm_gpio_set_intr_cfg_enable(unsigned gpio, unsigned val)
  158. {
  159. unsigned cfg;
  160. cfg = __raw_readl(GPIO_INTR_CFG(gpio));
  161. if (val) {
  162. cfg &= ~INTR_DIR_CONN_EN;
  163. cfg |= INTR_ENABLE;
  164. } else {
  165. cfg &= ~INTR_ENABLE;
  166. }
  167. __raw_writel(cfg, GPIO_INTR_CFG(gpio));
  168. }
  169. unsigned __msm_gpio_get_intr_cfg_enable(unsigned gpio)
  170. {
  171. return __msm_gpio_get_intr_config(gpio) & INTR_ENABLE;
  172. }
  173. void __msm_gpio_set_intr_cfg_type(unsigned gpio, unsigned type)
  174. {
  175. unsigned cfg;
  176. /* RAW_STATUS_EN is left on for all gpio irqs. Due to the
  177. * internal circuitry of TLMM, toggling the RAW_STATUS
  178. * could cause the INTR_STATUS to be set for EDGE interrupts.
  179. */
  180. cfg = INTR_RAW_STATUS_EN | INTR_TARGET_PROC_APPS;
  181. __raw_writel(cfg, GPIO_INTR_CFG(gpio));
  182. cfg &= ~INTR_DECT_CTL_MASK;
  183. if (type == IRQ_TYPE_EDGE_RISING)
  184. cfg |= INTR_DECT_CTL_POS_EDGE;
  185. else if (type == IRQ_TYPE_EDGE_FALLING)
  186. cfg |= INTR_DECT_CTL_NEG_EDGE;
  187. else if (type == IRQ_TYPE_EDGE_BOTH)
  188. cfg |= INTR_DECT_CTL_DUAL_EDGE;
  189. else
  190. cfg |= INTR_DECT_CTL_LEVEL;
  191. if (type & IRQ_TYPE_LEVEL_LOW)
  192. cfg &= ~INTR_POL_CTL_HI;
  193. else
  194. cfg |= INTR_POL_CTL_HI;
  195. __raw_writel(cfg, GPIO_INTR_CFG(gpio));
  196. /* Sometimes it might take a little while to update
  197. * the interrupt status after the RAW_STATUS is enabled
  198. * We clear the interrupt status before enabling the
  199. * interrupt in the unmask call-back.
  200. */
  201. udelay(5);
  202. }
  203. void __gpio_tlmm_config(unsigned config)
  204. {
  205. unsigned flags;
  206. unsigned gpio = GPIO_PIN(config);
  207. flags = ((GPIO_DIR(config) << 9) & (0x1 << 9)) |
  208. ((GPIO_DRVSTR(config) << 6) & (0x7 << 6)) |
  209. ((GPIO_FUNC(config) << 2) & (0xf << 2)) |
  210. ((GPIO_PULL(config) & 0x3));
  211. __raw_writel(flags, GPIO_CONFIG(gpio));
  212. }
  213. void __msm_gpio_install_direct_irq(unsigned gpio, unsigned irq,
  214. unsigned int input_polarity)
  215. {
  216. unsigned cfg;
  217. set_gpio_bits(BIT(GPIO_OE_BIT), GPIO_CONFIG(gpio));
  218. cfg = __raw_readl(GPIO_INTR_CFG(gpio));
  219. cfg &= ~(INTR_TARGET_PROC_NONE | INTR_RAW_STATUS_EN | INTR_ENABLE);
  220. cfg |= INTR_TARGET_PROC_APPS | INTR_DIR_CONN_EN;
  221. __raw_writel(cfg, GPIO_INTR_CFG(gpio));
  222. cfg = gpio;
  223. if (input_polarity)
  224. cfg |= DC_POLARITY_HI;
  225. __raw_writel(cfg, GPIO_DIR_CONN_INTR(irq));
  226. }