gpio-v2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* Copyright (c) 2010, Code Aurora Forum. 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. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. * 02110-1301, USA.
  16. *
  17. */
  18. #define pr_fmt(fmt) "%s: " fmt, __func__
  19. #include <linux/bitmap.h>
  20. #include <linux/bitops.h>
  21. #include <linux/gpio.h>
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/irq.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/spinlock.h>
  29. #include <mach/msm_iomap.h>
  30. #include "gpiomux.h"
  31. /* Bits of interest in the GPIO_IN_OUT register.
  32. */
  33. enum {
  34. GPIO_IN = 0,
  35. GPIO_OUT = 1
  36. };
  37. /* Bits of interest in the GPIO_INTR_STATUS register.
  38. */
  39. enum {
  40. INTR_STATUS = 0,
  41. };
  42. /* Bits of interest in the GPIO_CFG register.
  43. */
  44. enum {
  45. GPIO_OE = 9,
  46. };
  47. /* Bits of interest in the GPIO_INTR_CFG register.
  48. * When a GPIO triggers, two separate decisions are made, controlled
  49. * by two separate flags.
  50. *
  51. * - First, INTR_RAW_STATUS_EN controls whether or not the GPIO_INTR_STATUS
  52. * register for that GPIO will be updated to reflect the triggering of that
  53. * gpio. If this bit is 0, this register will not be updated.
  54. * - Second, INTR_ENABLE controls whether an interrupt is triggered.
  55. *
  56. * If INTR_ENABLE is set and INTR_RAW_STATUS_EN is NOT set, an interrupt
  57. * can be triggered but the status register will not reflect it.
  58. */
  59. enum {
  60. INTR_ENABLE = 0,
  61. INTR_POL_CTL = 1,
  62. INTR_DECT_CTL = 2,
  63. INTR_RAW_STATUS_EN = 3,
  64. };
  65. /* Codes of interest in GPIO_INTR_CFG_SU.
  66. */
  67. enum {
  68. TARGET_PROC_SCORPION = 4,
  69. TARGET_PROC_NONE = 7,
  70. };
  71. #define GPIO_INTR_CFG_SU(gpio) (MSM_TLMM_BASE + 0x0400 + (0x04 * (gpio)))
  72. #define GPIO_CONFIG(gpio) (MSM_TLMM_BASE + 0x1000 + (0x10 * (gpio)))
  73. #define GPIO_IN_OUT(gpio) (MSM_TLMM_BASE + 0x1004 + (0x10 * (gpio)))
  74. #define GPIO_INTR_CFG(gpio) (MSM_TLMM_BASE + 0x1008 + (0x10 * (gpio)))
  75. #define GPIO_INTR_STATUS(gpio) (MSM_TLMM_BASE + 0x100c + (0x10 * (gpio)))
  76. /**
  77. * struct msm_gpio_dev: the MSM8660 SoC GPIO device structure
  78. *
  79. * @enabled_irqs: a bitmap used to optimize the summary-irq handler. By
  80. * keeping track of which gpios are unmasked as irq sources, we avoid
  81. * having to do readl calls on hundreds of iomapped registers each time
  82. * the summary interrupt fires in order to locate the active interrupts.
  83. *
  84. * @wake_irqs: a bitmap for tracking which interrupt lines are enabled
  85. * as wakeup sources. When the device is suspended, interrupts which are
  86. * not wakeup sources are disabled.
  87. *
  88. * @dual_edge_irqs: a bitmap used to track which irqs are configured
  89. * as dual-edge, as this is not supported by the hardware and requires
  90. * some special handling in the driver.
  91. */
  92. struct msm_gpio_dev {
  93. struct gpio_chip gpio_chip;
  94. DECLARE_BITMAP(enabled_irqs, NR_GPIO_IRQS);
  95. DECLARE_BITMAP(wake_irqs, NR_GPIO_IRQS);
  96. DECLARE_BITMAP(dual_edge_irqs, NR_GPIO_IRQS);
  97. };
  98. static DEFINE_SPINLOCK(tlmm_lock);
  99. static inline struct msm_gpio_dev *to_msm_gpio_dev(struct gpio_chip *chip)
  100. {
  101. return container_of(chip, struct msm_gpio_dev, gpio_chip);
  102. }
  103. static inline void set_gpio_bits(unsigned n, void __iomem *reg)
  104. {
  105. writel(readl(reg) | n, reg);
  106. }
  107. static inline void clear_gpio_bits(unsigned n, void __iomem *reg)
  108. {
  109. writel(readl(reg) & ~n, reg);
  110. }
  111. static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
  112. {
  113. return readl(GPIO_IN_OUT(offset)) & BIT(GPIO_IN);
  114. }
  115. static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  116. {
  117. writel(val ? BIT(GPIO_OUT) : 0, GPIO_IN_OUT(offset));
  118. }
  119. static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  120. {
  121. unsigned long irq_flags;
  122. spin_lock_irqsave(&tlmm_lock, irq_flags);
  123. clear_gpio_bits(BIT(GPIO_OE), GPIO_CONFIG(offset));
  124. spin_unlock_irqrestore(&tlmm_lock, irq_flags);
  125. return 0;
  126. }
  127. static int msm_gpio_direction_output(struct gpio_chip *chip,
  128. unsigned offset,
  129. int val)
  130. {
  131. unsigned long irq_flags;
  132. spin_lock_irqsave(&tlmm_lock, irq_flags);
  133. msm_gpio_set(chip, offset, val);
  134. set_gpio_bits(BIT(GPIO_OE), GPIO_CONFIG(offset));
  135. spin_unlock_irqrestore(&tlmm_lock, irq_flags);
  136. return 0;
  137. }
  138. static int msm_gpio_request(struct gpio_chip *chip, unsigned offset)
  139. {
  140. return msm_gpiomux_get(chip->base + offset);
  141. }
  142. static void msm_gpio_free(struct gpio_chip *chip, unsigned offset)
  143. {
  144. msm_gpiomux_put(chip->base + offset);
  145. }
  146. static int msm_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  147. {
  148. return MSM_GPIO_TO_INT(chip->base + offset);
  149. }
  150. static inline int msm_irq_to_gpio(struct gpio_chip *chip, unsigned irq)
  151. {
  152. return irq - MSM_GPIO_TO_INT(chip->base);
  153. }
  154. static struct msm_gpio_dev msm_gpio = {
  155. .gpio_chip = {
  156. .base = 0,
  157. .ngpio = NR_GPIO_IRQS,
  158. .direction_input = msm_gpio_direction_input,
  159. .direction_output = msm_gpio_direction_output,
  160. .get = msm_gpio_get,
  161. .set = msm_gpio_set,
  162. .to_irq = msm_gpio_to_irq,
  163. .request = msm_gpio_request,
  164. .free = msm_gpio_free,
  165. },
  166. };
  167. /* For dual-edge interrupts in software, since the hardware has no
  168. * such support:
  169. *
  170. * At appropriate moments, this function may be called to flip the polarity
  171. * settings of both-edge irq lines to try and catch the next edge.
  172. *
  173. * The attempt is considered successful if:
  174. * - the status bit goes high, indicating that an edge was caught, or
  175. * - the input value of the gpio doesn't change during the attempt.
  176. * If the value changes twice during the process, that would cause the first
  177. * test to fail but would force the second, as two opposite
  178. * transitions would cause a detection no matter the polarity setting.
  179. *
  180. * The do-loop tries to sledge-hammer closed the timing hole between
  181. * the initial value-read and the polarity-write - if the line value changes
  182. * during that window, an interrupt is lost, the new polarity setting is
  183. * incorrect, and the first success test will fail, causing a retry.
  184. *
  185. * Algorithm comes from Google's msmgpio driver, see mach-msm/gpio.c.
  186. */
  187. static void msm_gpio_update_dual_edge_pos(unsigned gpio)
  188. {
  189. int loop_limit = 100;
  190. unsigned val, val2, intstat;
  191. do {
  192. val = readl(GPIO_IN_OUT(gpio)) & BIT(GPIO_IN);
  193. if (val)
  194. clear_gpio_bits(BIT(INTR_POL_CTL), GPIO_INTR_CFG(gpio));
  195. else
  196. set_gpio_bits(BIT(INTR_POL_CTL), GPIO_INTR_CFG(gpio));
  197. val2 = readl(GPIO_IN_OUT(gpio)) & BIT(GPIO_IN);
  198. intstat = readl(GPIO_INTR_STATUS(gpio)) & BIT(INTR_STATUS);
  199. if (intstat || val == val2)
  200. return;
  201. } while (loop_limit-- > 0);
  202. pr_err("dual-edge irq failed to stabilize, "
  203. "interrupts dropped. %#08x != %#08x\n",
  204. val, val2);
  205. }
  206. static void msm_gpio_irq_ack(unsigned int irq)
  207. {
  208. int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, irq);
  209. writel(BIT(INTR_STATUS), GPIO_INTR_STATUS(gpio));
  210. if (test_bit(gpio, msm_gpio.dual_edge_irqs))
  211. msm_gpio_update_dual_edge_pos(gpio);
  212. }
  213. static void msm_gpio_irq_mask(unsigned int irq)
  214. {
  215. int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, irq);
  216. unsigned long irq_flags;
  217. spin_lock_irqsave(&tlmm_lock, irq_flags);
  218. writel(TARGET_PROC_NONE, GPIO_INTR_CFG_SU(gpio));
  219. clear_gpio_bits(INTR_RAW_STATUS_EN | INTR_ENABLE, GPIO_INTR_CFG(gpio));
  220. __clear_bit(gpio, msm_gpio.enabled_irqs);
  221. spin_unlock_irqrestore(&tlmm_lock, irq_flags);
  222. }
  223. static void msm_gpio_irq_unmask(unsigned int irq)
  224. {
  225. int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, irq);
  226. unsigned long irq_flags;
  227. spin_lock_irqsave(&tlmm_lock, irq_flags);
  228. __set_bit(gpio, msm_gpio.enabled_irqs);
  229. set_gpio_bits(INTR_RAW_STATUS_EN | INTR_ENABLE, GPIO_INTR_CFG(gpio));
  230. writel(TARGET_PROC_SCORPION, GPIO_INTR_CFG_SU(gpio));
  231. spin_unlock_irqrestore(&tlmm_lock, irq_flags);
  232. }
  233. static int msm_gpio_irq_set_type(unsigned int irq, unsigned int flow_type)
  234. {
  235. int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, irq);
  236. unsigned long irq_flags;
  237. uint32_t bits;
  238. spin_lock_irqsave(&tlmm_lock, irq_flags);
  239. bits = readl(GPIO_INTR_CFG(gpio));
  240. if (flow_type & IRQ_TYPE_EDGE_BOTH) {
  241. bits |= BIT(INTR_DECT_CTL);
  242. irq_desc[irq].handle_irq = handle_edge_irq;
  243. if ((flow_type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
  244. __set_bit(gpio, msm_gpio.dual_edge_irqs);
  245. else
  246. __clear_bit(gpio, msm_gpio.dual_edge_irqs);
  247. } else {
  248. bits &= ~BIT(INTR_DECT_CTL);
  249. irq_desc[irq].handle_irq = handle_level_irq;
  250. __clear_bit(gpio, msm_gpio.dual_edge_irqs);
  251. }
  252. if (flow_type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_HIGH))
  253. bits |= BIT(INTR_POL_CTL);
  254. else
  255. bits &= ~BIT(INTR_POL_CTL);
  256. writel(bits, GPIO_INTR_CFG(gpio));
  257. if ((flow_type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
  258. msm_gpio_update_dual_edge_pos(gpio);
  259. spin_unlock_irqrestore(&tlmm_lock, irq_flags);
  260. return 0;
  261. }
  262. /*
  263. * When the summary IRQ is raised, any number of GPIO lines may be high.
  264. * It is the job of the summary handler to find all those GPIO lines
  265. * which have been set as summary IRQ lines and which are triggered,
  266. * and to call their interrupt handlers.
  267. */
  268. static void msm_summary_irq_handler(unsigned int irq, struct irq_desc *desc)
  269. {
  270. unsigned long i;
  271. for (i = find_first_bit(msm_gpio.enabled_irqs, NR_GPIO_IRQS);
  272. i < NR_GPIO_IRQS;
  273. i = find_next_bit(msm_gpio.enabled_irqs, NR_GPIO_IRQS, i + 1)) {
  274. if (readl(GPIO_INTR_STATUS(i)) & BIT(INTR_STATUS))
  275. generic_handle_irq(msm_gpio_to_irq(&msm_gpio.gpio_chip,
  276. i));
  277. }
  278. desc->chip->ack(irq);
  279. }
  280. static int msm_gpio_irq_set_wake(unsigned int irq, unsigned int on)
  281. {
  282. int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, irq);
  283. if (on) {
  284. if (bitmap_empty(msm_gpio.wake_irqs, NR_GPIO_IRQS))
  285. set_irq_wake(TLMM_SCSS_SUMMARY_IRQ, 1);
  286. set_bit(gpio, msm_gpio.wake_irqs);
  287. } else {
  288. clear_bit(gpio, msm_gpio.wake_irqs);
  289. if (bitmap_empty(msm_gpio.wake_irqs, NR_GPIO_IRQS))
  290. set_irq_wake(TLMM_SCSS_SUMMARY_IRQ, 0);
  291. }
  292. return 0;
  293. }
  294. static struct irq_chip msm_gpio_irq_chip = {
  295. .name = "msmgpio",
  296. .mask = msm_gpio_irq_mask,
  297. .unmask = msm_gpio_irq_unmask,
  298. .ack = msm_gpio_irq_ack,
  299. .set_type = msm_gpio_irq_set_type,
  300. .set_wake = msm_gpio_irq_set_wake,
  301. };
  302. static int __devinit msm_gpio_probe(struct platform_device *dev)
  303. {
  304. int i, irq, ret;
  305. bitmap_zero(msm_gpio.enabled_irqs, NR_GPIO_IRQS);
  306. bitmap_zero(msm_gpio.wake_irqs, NR_GPIO_IRQS);
  307. bitmap_zero(msm_gpio.dual_edge_irqs, NR_GPIO_IRQS);
  308. msm_gpio.gpio_chip.label = dev->name;
  309. ret = gpiochip_add(&msm_gpio.gpio_chip);
  310. if (ret < 0)
  311. return ret;
  312. for (i = 0; i < msm_gpio.gpio_chip.ngpio; ++i) {
  313. irq = msm_gpio_to_irq(&msm_gpio.gpio_chip, i);
  314. set_irq_chip(irq, &msm_gpio_irq_chip);
  315. set_irq_handler(irq, handle_level_irq);
  316. set_irq_flags(irq, IRQF_VALID);
  317. }
  318. set_irq_chained_handler(TLMM_SCSS_SUMMARY_IRQ,
  319. msm_summary_irq_handler);
  320. return 0;
  321. }
  322. static int __devexit msm_gpio_remove(struct platform_device *dev)
  323. {
  324. int ret = gpiochip_remove(&msm_gpio.gpio_chip);
  325. if (ret < 0)
  326. return ret;
  327. set_irq_handler(TLMM_SCSS_SUMMARY_IRQ, NULL);
  328. return 0;
  329. }
  330. static struct platform_driver msm_gpio_driver = {
  331. .probe = msm_gpio_probe,
  332. .remove = __devexit_p(msm_gpio_remove),
  333. .driver = {
  334. .name = "msmgpio",
  335. .owner = THIS_MODULE,
  336. },
  337. };
  338. static struct platform_device msm_device_gpio = {
  339. .name = "msmgpio",
  340. .id = -1,
  341. };
  342. static int __init msm_gpio_init(void)
  343. {
  344. int rc;
  345. rc = platform_driver_register(&msm_gpio_driver);
  346. if (!rc) {
  347. rc = platform_device_register(&msm_device_gpio);
  348. if (rc)
  349. platform_driver_unregister(&msm_gpio_driver);
  350. }
  351. return rc;
  352. }
  353. static void __exit msm_gpio_exit(void)
  354. {
  355. platform_device_unregister(&msm_device_gpio);
  356. platform_driver_unregister(&msm_gpio_driver);
  357. }
  358. postcore_initcall(msm_gpio_init);
  359. module_exit(msm_gpio_exit);
  360. MODULE_AUTHOR("Gregory Bean <gbean@codeaurora.org>");
  361. MODULE_DESCRIPTION("Driver for Qualcomm MSM TLMMv2 SoC GPIOs");
  362. MODULE_LICENSE("GPL v2");
  363. MODULE_ALIAS("platform:msmgpio");