gpio-msm-v2.c 13 KB

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