twl6030-irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * twl6030-irq.c - TWL6030 irq support
  3. *
  4. * Copyright (C) 2005-2009 Texas Instruments, Inc.
  5. *
  6. * Modifications to defer interrupt handling to a kernel thread:
  7. * Copyright (C) 2006 MontaVista Software, Inc.
  8. *
  9. * Based on tlv320aic23.c:
  10. * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
  11. *
  12. * Code cleanup and modifications to IRQ handler.
  13. * by syed khasim <x0khasim@ti.com>
  14. *
  15. * TWL6030 specific code and IRQ handling changes by
  16. * Jagadeesh Bhaskar Pakaravoor <j-pakaravoor@ti.com>
  17. * Balaji T K <balajitk@ti.com>
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  32. */
  33. #include <linux/init.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/irq.h>
  36. #include <linux/kthread.h>
  37. #include <linux/i2c/twl.h>
  38. #include <linux/platform_device.h>
  39. #include "twl-core.h"
  40. /*
  41. * TWL6030 (unlike its predecessors, which had two level interrupt handling)
  42. * three interrupt registers INT_STS_A, INT_STS_B and INT_STS_C.
  43. * It exposes status bits saying who has raised an interrupt. There are
  44. * three mask registers that corresponds to these status registers, that
  45. * enables/disables these interrupts.
  46. *
  47. * We set up IRQs starting at a platform-specified base. An interrupt map table,
  48. * specifies mapping between interrupt number and the associated module.
  49. *
  50. */
  51. static int twl6030_interrupt_mapping[24] = {
  52. PWR_INTR_OFFSET, /* Bit 0 PWRON */
  53. PWR_INTR_OFFSET, /* Bit 1 RPWRON */
  54. PWR_INTR_OFFSET, /* Bit 2 BAT_VLOW */
  55. RTC_INTR_OFFSET, /* Bit 3 RTC_ALARM */
  56. RTC_INTR_OFFSET, /* Bit 4 RTC_PERIOD */
  57. HOTDIE_INTR_OFFSET, /* Bit 5 HOT_DIE */
  58. SMPSLDO_INTR_OFFSET, /* Bit 6 VXXX_SHORT */
  59. SMPSLDO_INTR_OFFSET, /* Bit 7 VMMC_SHORT */
  60. SMPSLDO_INTR_OFFSET, /* Bit 8 VUSIM_SHORT */
  61. BATDETECT_INTR_OFFSET, /* Bit 9 BAT */
  62. SIMDETECT_INTR_OFFSET, /* Bit 10 SIM */
  63. MMCDETECT_INTR_OFFSET, /* Bit 11 MMC */
  64. RSV_INTR_OFFSET, /* Bit 12 Reserved */
  65. MADC_INTR_OFFSET, /* Bit 13 GPADC_RT_EOC */
  66. MADC_INTR_OFFSET, /* Bit 14 GPADC_SW_EOC */
  67. GASGAUGE_INTR_OFFSET, /* Bit 15 CC_AUTOCAL */
  68. USBOTG_INTR_OFFSET, /* Bit 16 ID_WKUP */
  69. USBOTG_INTR_OFFSET, /* Bit 17 VBUS_WKUP */
  70. USBOTG_INTR_OFFSET, /* Bit 18 ID */
  71. USB_PRES_INTR_OFFSET, /* Bit 19 VBUS */
  72. CHARGER_INTR_OFFSET, /* Bit 20 CHRG_CTRL */
  73. CHARGER_INTR_OFFSET, /* Bit 21 EXT_CHRG */
  74. CHARGER_INTR_OFFSET, /* Bit 22 INT_CHRG */
  75. RSV_INTR_OFFSET, /* Bit 23 Reserved */
  76. };
  77. /*----------------------------------------------------------------------*/
  78. static unsigned twl6030_irq_base;
  79. static struct completion irq_event;
  80. /*
  81. * This thread processes interrupts reported by the Primary Interrupt Handler.
  82. */
  83. static int twl6030_irq_thread(void *data)
  84. {
  85. long irq = (long)data;
  86. static unsigned i2c_errors;
  87. static const unsigned max_i2c_errors = 100;
  88. int ret;
  89. current->flags |= PF_NOFREEZE;
  90. while (!kthread_should_stop()) {
  91. int i;
  92. union {
  93. u8 bytes[4];
  94. u32 int_sts;
  95. } sts;
  96. /* Wait for IRQ, then read PIH irq status (also blocking) */
  97. wait_for_completion_interruptible(&irq_event);
  98. /* read INT_STS_A, B and C in one shot using a burst read */
  99. ret = twl_i2c_read(TWL_MODULE_PIH, sts.bytes,
  100. REG_INT_STS_A, 3);
  101. if (ret) {
  102. pr_warning("twl6030: I2C error %d reading PIH ISR\n",
  103. ret);
  104. if (++i2c_errors >= max_i2c_errors) {
  105. printk(KERN_ERR "Maximum I2C error count"
  106. " exceeded. Terminating %s.\n",
  107. __func__);
  108. break;
  109. }
  110. complete(&irq_event);
  111. continue;
  112. }
  113. sts.bytes[3] = 0; /* Only 24 bits are valid*/
  114. /*
  115. * Since VBUS status bit is not reliable for VBUS disconnect
  116. * use CHARGER VBUS detection status bit instead.
  117. */
  118. if (sts.bytes[2] & 0x10)
  119. sts.bytes[2] |= 0x08;
  120. for (i = 0; sts.int_sts; sts.int_sts >>= 1, i++) {
  121. local_irq_disable();
  122. if (sts.int_sts & 0x1) {
  123. int module_irq = twl6030_irq_base +
  124. twl6030_interrupt_mapping[i];
  125. struct irq_desc *d = irq_to_desc(module_irq);
  126. if (!d) {
  127. pr_err("twl6030: Invalid SIH IRQ: %d\n",
  128. module_irq);
  129. return -EINVAL;
  130. }
  131. /* These can't be masked ... always warn
  132. * if we get any surprises.
  133. */
  134. if (d->status & IRQ_DISABLED)
  135. note_interrupt(module_irq, d,
  136. IRQ_NONE);
  137. else
  138. d->handle_irq(module_irq, d);
  139. }
  140. local_irq_enable();
  141. }
  142. ret = twl_i2c_write(TWL_MODULE_PIH, sts.bytes,
  143. REG_INT_STS_A, 3); /* clear INT_STS_A */
  144. if (ret)
  145. pr_warning("twl6030: I2C error in clearing PIH ISR\n");
  146. enable_irq(irq);
  147. }
  148. return 0;
  149. }
  150. /*
  151. * handle_twl6030_int() is the desc->handle method for the twl6030 interrupt.
  152. * This is a chained interrupt, so there is no desc->action method for it.
  153. * Now we need to query the interrupt controller in the twl6030 to determine
  154. * which module is generating the interrupt request. However, we can't do i2c
  155. * transactions in interrupt context, so we must defer that work to a kernel
  156. * thread. All we do here is acknowledge and mask the interrupt and wakeup
  157. * the kernel thread.
  158. */
  159. static irqreturn_t handle_twl6030_pih(int irq, void *devid)
  160. {
  161. disable_irq_nosync(irq);
  162. complete(devid);
  163. return IRQ_HANDLED;
  164. }
  165. /*----------------------------------------------------------------------*/
  166. static inline void activate_irq(int irq)
  167. {
  168. #ifdef CONFIG_ARM
  169. /* ARM requires an extra step to clear IRQ_NOREQUEST, which it
  170. * sets on behalf of every irq_chip. Also sets IRQ_NOPROBE.
  171. */
  172. set_irq_flags(irq, IRQF_VALID);
  173. #else
  174. /* same effect on other architectures */
  175. set_irq_noprobe(irq);
  176. #endif
  177. }
  178. /*----------------------------------------------------------------------*/
  179. static unsigned twl6030_irq_next;
  180. /*----------------------------------------------------------------------*/
  181. int twl6030_interrupt_unmask(u8 bit_mask, u8 offset)
  182. {
  183. int ret;
  184. u8 unmask_value;
  185. ret = twl_i2c_read_u8(TWL_MODULE_PIH, &unmask_value,
  186. REG_INT_STS_A + offset);
  187. unmask_value &= (~(bit_mask));
  188. ret |= twl_i2c_write_u8(TWL_MODULE_PIH, unmask_value,
  189. REG_INT_STS_A + offset); /* unmask INT_MSK_A/B/C */
  190. return ret;
  191. }
  192. EXPORT_SYMBOL(twl6030_interrupt_unmask);
  193. int twl6030_interrupt_mask(u8 bit_mask, u8 offset)
  194. {
  195. int ret;
  196. u8 mask_value;
  197. ret = twl_i2c_read_u8(TWL_MODULE_PIH, &mask_value,
  198. REG_INT_STS_A + offset);
  199. mask_value |= (bit_mask);
  200. ret |= twl_i2c_write_u8(TWL_MODULE_PIH, mask_value,
  201. REG_INT_STS_A + offset); /* mask INT_MSK_A/B/C */
  202. return ret;
  203. }
  204. EXPORT_SYMBOL(twl6030_interrupt_mask);
  205. int twl6030_mmc_card_detect_config(void)
  206. {
  207. int ret;
  208. u8 reg_val = 0;
  209. /* Unmasking the Card detect Interrupt line for MMC1 from Phoenix */
  210. twl6030_interrupt_unmask(TWL6030_MMCDETECT_INT_MASK,
  211. REG_INT_MSK_LINE_B);
  212. twl6030_interrupt_unmask(TWL6030_MMCDETECT_INT_MASK,
  213. REG_INT_MSK_STS_B);
  214. /*
  215. * Intially Configuring MMC_CTRL for receving interrupts &
  216. * Card status on TWL6030 for MMC1
  217. */
  218. ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &reg_val, TWL6030_MMCCTRL);
  219. if (ret < 0) {
  220. pr_err("twl6030: Failed to read MMCCTRL, error %d\n", ret);
  221. return ret;
  222. }
  223. reg_val &= ~VMMC_AUTO_OFF;
  224. reg_val |= SW_FC;
  225. ret = twl_i2c_write_u8(TWL6030_MODULE_ID0, reg_val, TWL6030_MMCCTRL);
  226. if (ret < 0) {
  227. pr_err("twl6030: Failed to write MMCCTRL, error %d\n", ret);
  228. return ret;
  229. }
  230. /* Configuring PullUp-PullDown register */
  231. ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &reg_val,
  232. TWL6030_CFG_INPUT_PUPD3);
  233. if (ret < 0) {
  234. pr_err("twl6030: Failed to read CFG_INPUT_PUPD3, error %d\n",
  235. ret);
  236. return ret;
  237. }
  238. reg_val &= ~(MMC_PU | MMC_PD);
  239. ret = twl_i2c_write_u8(TWL6030_MODULE_ID0, reg_val,
  240. TWL6030_CFG_INPUT_PUPD3);
  241. if (ret < 0) {
  242. pr_err("twl6030: Failed to write CFG_INPUT_PUPD3, error %d\n",
  243. ret);
  244. return ret;
  245. }
  246. return 0;
  247. }
  248. EXPORT_SYMBOL(twl6030_mmc_card_detect_config);
  249. int twl6030_mmc_card_detect(struct device *dev, int slot)
  250. {
  251. int ret = -EIO;
  252. u8 read_reg = 0;
  253. struct platform_device *pdev = to_platform_device(dev);
  254. if (pdev->id) {
  255. /* TWL6030 provide's Card detect support for
  256. * only MMC1 controller.
  257. */
  258. pr_err("Unkown MMC controller %d in %s\n", pdev->id, __func__);
  259. return ret;
  260. }
  261. /*
  262. * BIT0 of MMC_CTRL on TWL6030 provides card status for MMC1
  263. * 0 - Card not present ,1 - Card present
  264. */
  265. ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &read_reg,
  266. TWL6030_MMCCTRL);
  267. if (ret >= 0)
  268. ret = read_reg & STS_MMC;
  269. return ret;
  270. }
  271. EXPORT_SYMBOL(twl6030_mmc_card_detect);
  272. int twl6030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end)
  273. {
  274. int status = 0;
  275. int i;
  276. struct task_struct *task;
  277. int ret;
  278. u8 mask[4];
  279. static struct irq_chip twl6030_irq_chip;
  280. mask[1] = 0xFF;
  281. mask[2] = 0xFF;
  282. mask[3] = 0xFF;
  283. ret = twl_i2c_write(TWL_MODULE_PIH, &mask[0],
  284. REG_INT_MSK_LINE_A, 3); /* MASK ALL INT LINES */
  285. ret = twl_i2c_write(TWL_MODULE_PIH, &mask[0],
  286. REG_INT_MSK_STS_A, 3); /* MASK ALL INT STS */
  287. ret = twl_i2c_write(TWL_MODULE_PIH, &mask[0],
  288. REG_INT_STS_A, 3); /* clear INT_STS_A,B,C */
  289. twl6030_irq_base = irq_base;
  290. /* install an irq handler for each of the modules;
  291. * clone dummy irq_chip since PIH can't *do* anything
  292. */
  293. twl6030_irq_chip = dummy_irq_chip;
  294. twl6030_irq_chip.name = "twl6030";
  295. twl6030_irq_chip.irq_set_type = NULL;
  296. for (i = irq_base; i < irq_end; i++) {
  297. set_irq_chip_and_handler(i, &twl6030_irq_chip,
  298. handle_simple_irq);
  299. activate_irq(i);
  300. }
  301. twl6030_irq_next = i;
  302. pr_info("twl6030: %s (irq %d) chaining IRQs %d..%d\n", "PIH",
  303. irq_num, irq_base, twl6030_irq_next - 1);
  304. /* install an irq handler to demultiplex the TWL6030 interrupt */
  305. init_completion(&irq_event);
  306. task = kthread_run(twl6030_irq_thread, (void *)irq_num, "twl6030-irq");
  307. if (IS_ERR(task)) {
  308. pr_err("twl6030: could not create irq %d thread!\n", irq_num);
  309. status = PTR_ERR(task);
  310. goto fail_kthread;
  311. }
  312. status = request_irq(irq_num, handle_twl6030_pih, IRQF_DISABLED,
  313. "TWL6030-PIH", &irq_event);
  314. if (status < 0) {
  315. pr_err("twl6030: could not claim irq%d: %d\n", irq_num, status);
  316. goto fail_irq;
  317. }
  318. return status;
  319. fail_irq:
  320. free_irq(irq_num, &irq_event);
  321. fail_kthread:
  322. for (i = irq_base; i < irq_end; i++)
  323. set_irq_chip_and_handler(i, NULL, NULL);
  324. return status;
  325. }
  326. int twl6030_exit_irq(void)
  327. {
  328. if (twl6030_irq_base) {
  329. pr_err("twl6030: can't yet clean up IRQs?\n");
  330. return -ENOSYS;
  331. }
  332. return 0;
  333. }