gpio-msm-v2.c 12 KB

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