twl6030-irq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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/export.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 <linux/suspend.h>
  40. #include <linux/of.h>
  41. #include <linux/irqdomain.h>
  42. #include <linux/of_device.h>
  43. #include "twl-core.h"
  44. /*
  45. * TWL6030 (unlike its predecessors, which had two level interrupt handling)
  46. * three interrupt registers INT_STS_A, INT_STS_B and INT_STS_C.
  47. * It exposes status bits saying who has raised an interrupt. There are
  48. * three mask registers that corresponds to these status registers, that
  49. * enables/disables these interrupts.
  50. *
  51. * We set up IRQs starting at a platform-specified base. An interrupt map table,
  52. * specifies mapping between interrupt number and the associated module.
  53. */
  54. #define TWL6030_NR_IRQS 20
  55. static int twl6030_interrupt_mapping[24] = {
  56. PWR_INTR_OFFSET, /* Bit 0 PWRON */
  57. PWR_INTR_OFFSET, /* Bit 1 RPWRON */
  58. PWR_INTR_OFFSET, /* Bit 2 BAT_VLOW */
  59. RTC_INTR_OFFSET, /* Bit 3 RTC_ALARM */
  60. RTC_INTR_OFFSET, /* Bit 4 RTC_PERIOD */
  61. HOTDIE_INTR_OFFSET, /* Bit 5 HOT_DIE */
  62. SMPSLDO_INTR_OFFSET, /* Bit 6 VXXX_SHORT */
  63. SMPSLDO_INTR_OFFSET, /* Bit 7 VMMC_SHORT */
  64. SMPSLDO_INTR_OFFSET, /* Bit 8 VUSIM_SHORT */
  65. BATDETECT_INTR_OFFSET, /* Bit 9 BAT */
  66. SIMDETECT_INTR_OFFSET, /* Bit 10 SIM */
  67. MMCDETECT_INTR_OFFSET, /* Bit 11 MMC */
  68. RSV_INTR_OFFSET, /* Bit 12 Reserved */
  69. MADC_INTR_OFFSET, /* Bit 13 GPADC_RT_EOC */
  70. MADC_INTR_OFFSET, /* Bit 14 GPADC_SW_EOC */
  71. GASGAUGE_INTR_OFFSET, /* Bit 15 CC_AUTOCAL */
  72. USBOTG_INTR_OFFSET, /* Bit 16 ID_WKUP */
  73. USBOTG_INTR_OFFSET, /* Bit 17 VBUS_WKUP */
  74. USBOTG_INTR_OFFSET, /* Bit 18 ID */
  75. USB_PRES_INTR_OFFSET, /* Bit 19 VBUS */
  76. CHARGER_INTR_OFFSET, /* Bit 20 CHRG_CTRL */
  77. CHARGERFAULT_INTR_OFFSET, /* Bit 21 EXT_CHRG */
  78. CHARGERFAULT_INTR_OFFSET, /* Bit 22 INT_CHRG */
  79. RSV_INTR_OFFSET, /* Bit 23 Reserved */
  80. };
  81. static int twl6032_interrupt_mapping[24] = {
  82. PWR_INTR_OFFSET, /* Bit 0 PWRON */
  83. PWR_INTR_OFFSET, /* Bit 1 RPWRON */
  84. PWR_INTR_OFFSET, /* Bit 2 SYS_VLOW */
  85. RTC_INTR_OFFSET, /* Bit 3 RTC_ALARM */
  86. RTC_INTR_OFFSET, /* Bit 4 RTC_PERIOD */
  87. HOTDIE_INTR_OFFSET, /* Bit 5 HOT_DIE */
  88. SMPSLDO_INTR_OFFSET, /* Bit 6 VXXX_SHORT */
  89. PWR_INTR_OFFSET, /* Bit 7 SPDURATION */
  90. PWR_INTR_OFFSET, /* Bit 8 WATCHDOG */
  91. BATDETECT_INTR_OFFSET, /* Bit 9 BAT */
  92. SIMDETECT_INTR_OFFSET, /* Bit 10 SIM */
  93. MMCDETECT_INTR_OFFSET, /* Bit 11 MMC */
  94. MADC_INTR_OFFSET, /* Bit 12 GPADC_RT_EOC */
  95. MADC_INTR_OFFSET, /* Bit 13 GPADC_SW_EOC */
  96. GASGAUGE_INTR_OFFSET, /* Bit 14 CC_EOC */
  97. GASGAUGE_INTR_OFFSET, /* Bit 15 CC_AUTOCAL */
  98. USBOTG_INTR_OFFSET, /* Bit 16 ID_WKUP */
  99. USBOTG_INTR_OFFSET, /* Bit 17 VBUS_WKUP */
  100. USBOTG_INTR_OFFSET, /* Bit 18 ID */
  101. USB_PRES_INTR_OFFSET, /* Bit 19 VBUS */
  102. CHARGER_INTR_OFFSET, /* Bit 20 CHRG_CTRL */
  103. CHARGERFAULT_INTR_OFFSET, /* Bit 21 EXT_CHRG */
  104. CHARGERFAULT_INTR_OFFSET, /* Bit 22 INT_CHRG */
  105. RSV_INTR_OFFSET, /* Bit 23 Reserved */
  106. };
  107. /*----------------------------------------------------------------------*/
  108. struct twl6030_irq {
  109. unsigned int irq_base;
  110. int twl_irq;
  111. bool irq_wake_enabled;
  112. atomic_t wakeirqs;
  113. struct notifier_block pm_nb;
  114. struct irq_chip irq_chip;
  115. struct irq_domain *irq_domain;
  116. const int *irq_mapping_tbl;
  117. };
  118. static struct twl6030_irq *twl6030_irq;
  119. static int twl6030_irq_pm_notifier(struct notifier_block *notifier,
  120. unsigned long pm_event, void *unused)
  121. {
  122. int chained_wakeups;
  123. struct twl6030_irq *pdata = container_of(notifier, struct twl6030_irq,
  124. pm_nb);
  125. switch (pm_event) {
  126. case PM_SUSPEND_PREPARE:
  127. chained_wakeups = atomic_read(&pdata->wakeirqs);
  128. if (chained_wakeups && !pdata->irq_wake_enabled) {
  129. if (enable_irq_wake(pdata->twl_irq))
  130. pr_err("twl6030 IRQ wake enable failed\n");
  131. else
  132. pdata->irq_wake_enabled = true;
  133. } else if (!chained_wakeups && pdata->irq_wake_enabled) {
  134. disable_irq_wake(pdata->twl_irq);
  135. pdata->irq_wake_enabled = false;
  136. }
  137. disable_irq(pdata->twl_irq);
  138. break;
  139. case PM_POST_SUSPEND:
  140. enable_irq(pdata->twl_irq);
  141. break;
  142. default:
  143. break;
  144. }
  145. return NOTIFY_DONE;
  146. }
  147. /*
  148. * Threaded irq handler for the twl6030 interrupt.
  149. * We query the interrupt controller in the twl6030 to determine
  150. * which module is generating the interrupt request and call
  151. * handle_nested_irq for that module.
  152. */
  153. static irqreturn_t twl6030_irq_thread(int irq, void *data)
  154. {
  155. int i, ret;
  156. union {
  157. u8 bytes[4];
  158. __le32 int_sts;
  159. } sts;
  160. u32 int_sts; /* sts.int_sts converted to CPU endianness */
  161. struct twl6030_irq *pdata = data;
  162. /* read INT_STS_A, B and C in one shot using a burst read */
  163. ret = twl_i2c_read(TWL_MODULE_PIH, sts.bytes, REG_INT_STS_A, 3);
  164. if (ret) {
  165. pr_warn("twl6030_irq: I2C error %d reading PIH ISR\n", ret);
  166. return IRQ_HANDLED;
  167. }
  168. sts.bytes[3] = 0; /* Only 24 bits are valid*/
  169. /*
  170. * Since VBUS status bit is not reliable for VBUS disconnect
  171. * use CHARGER VBUS detection status bit instead.
  172. */
  173. if (sts.bytes[2] & 0x10)
  174. sts.bytes[2] |= 0x08;
  175. int_sts = le32_to_cpu(sts.int_sts);
  176. for (i = 0; int_sts; int_sts >>= 1, i++)
  177. if (int_sts & 0x1) {
  178. int module_irq =
  179. irq_find_mapping(pdata->irq_domain,
  180. pdata->irq_mapping_tbl[i]);
  181. if (module_irq)
  182. handle_nested_irq(module_irq);
  183. else
  184. pr_err("twl6030_irq: Unmapped PIH ISR %u detected\n",
  185. i);
  186. pr_debug("twl6030_irq: PIH ISR %u, virq%u\n",
  187. i, module_irq);
  188. }
  189. /*
  190. * NOTE:
  191. * Simulation confirms that documentation is wrong w.r.t the
  192. * interrupt status clear operation. A single *byte* write to
  193. * any one of STS_A to STS_C register results in all three
  194. * STS registers being reset. Since it does not matter which
  195. * value is written, all three registers are cleared on a
  196. * single byte write, so we just use 0x0 to clear.
  197. */
  198. ret = twl_i2c_write_u8(TWL_MODULE_PIH, 0x00, REG_INT_STS_A);
  199. if (ret)
  200. pr_warn("twl6030_irq: I2C error in clearing PIH ISR\n");
  201. return IRQ_HANDLED;
  202. }
  203. /*----------------------------------------------------------------------*/
  204. static int twl6030_irq_set_wake(struct irq_data *d, unsigned int on)
  205. {
  206. struct twl6030_irq *pdata = irq_data_get_irq_chip_data(d);
  207. if (on)
  208. atomic_inc(&pdata->wakeirqs);
  209. else
  210. atomic_dec(&pdata->wakeirqs);
  211. return 0;
  212. }
  213. int twl6030_interrupt_unmask(u8 bit_mask, u8 offset)
  214. {
  215. int ret;
  216. u8 unmask_value;
  217. ret = twl_i2c_read_u8(TWL_MODULE_PIH, &unmask_value,
  218. REG_INT_STS_A + offset);
  219. unmask_value &= (~(bit_mask));
  220. ret |= twl_i2c_write_u8(TWL_MODULE_PIH, unmask_value,
  221. REG_INT_STS_A + offset); /* unmask INT_MSK_A/B/C */
  222. return ret;
  223. }
  224. EXPORT_SYMBOL(twl6030_interrupt_unmask);
  225. int twl6030_interrupt_mask(u8 bit_mask, u8 offset)
  226. {
  227. int ret;
  228. u8 mask_value;
  229. ret = twl_i2c_read_u8(TWL_MODULE_PIH, &mask_value,
  230. REG_INT_STS_A + offset);
  231. mask_value |= (bit_mask);
  232. ret |= twl_i2c_write_u8(TWL_MODULE_PIH, mask_value,
  233. REG_INT_STS_A + offset); /* mask INT_MSK_A/B/C */
  234. return ret;
  235. }
  236. EXPORT_SYMBOL(twl6030_interrupt_mask);
  237. int twl6030_mmc_card_detect_config(void)
  238. {
  239. int ret;
  240. u8 reg_val = 0;
  241. /* Unmasking the Card detect Interrupt line for MMC1 from Phoenix */
  242. twl6030_interrupt_unmask(TWL6030_MMCDETECT_INT_MASK,
  243. REG_INT_MSK_LINE_B);
  244. twl6030_interrupt_unmask(TWL6030_MMCDETECT_INT_MASK,
  245. REG_INT_MSK_STS_B);
  246. /*
  247. * Initially Configuring MMC_CTRL for receiving interrupts &
  248. * Card status on TWL6030 for MMC1
  249. */
  250. ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &reg_val, TWL6030_MMCCTRL);
  251. if (ret < 0) {
  252. pr_err("twl6030: Failed to read MMCCTRL, error %d\n", ret);
  253. return ret;
  254. }
  255. reg_val &= ~VMMC_AUTO_OFF;
  256. reg_val |= SW_FC;
  257. ret = twl_i2c_write_u8(TWL6030_MODULE_ID0, reg_val, TWL6030_MMCCTRL);
  258. if (ret < 0) {
  259. pr_err("twl6030: Failed to write MMCCTRL, error %d\n", ret);
  260. return ret;
  261. }
  262. /* Configuring PullUp-PullDown register */
  263. ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &reg_val,
  264. TWL6030_CFG_INPUT_PUPD3);
  265. if (ret < 0) {
  266. pr_err("twl6030: Failed to read CFG_INPUT_PUPD3, error %d\n",
  267. ret);
  268. return ret;
  269. }
  270. reg_val &= ~(MMC_PU | MMC_PD);
  271. ret = twl_i2c_write_u8(TWL6030_MODULE_ID0, reg_val,
  272. TWL6030_CFG_INPUT_PUPD3);
  273. if (ret < 0) {
  274. pr_err("twl6030: Failed to write CFG_INPUT_PUPD3, error %d\n",
  275. ret);
  276. return ret;
  277. }
  278. return irq_find_mapping(twl6030_irq->irq_domain,
  279. MMCDETECT_INTR_OFFSET);
  280. }
  281. EXPORT_SYMBOL(twl6030_mmc_card_detect_config);
  282. int twl6030_mmc_card_detect(struct device *dev, int slot)
  283. {
  284. int ret = -EIO;
  285. u8 read_reg = 0;
  286. struct platform_device *pdev = to_platform_device(dev);
  287. if (pdev->id) {
  288. /* TWL6030 provide's Card detect support for
  289. * only MMC1 controller.
  290. */
  291. pr_err("Unknown MMC controller %d in %s\n", pdev->id, __func__);
  292. return ret;
  293. }
  294. /*
  295. * BIT0 of MMC_CTRL on TWL6030 provides card status for MMC1
  296. * 0 - Card not present ,1 - Card present
  297. */
  298. ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &read_reg,
  299. TWL6030_MMCCTRL);
  300. if (ret >= 0)
  301. ret = read_reg & STS_MMC;
  302. return ret;
  303. }
  304. EXPORT_SYMBOL(twl6030_mmc_card_detect);
  305. static int twl6030_irq_map(struct irq_domain *d, unsigned int virq,
  306. irq_hw_number_t hwirq)
  307. {
  308. struct twl6030_irq *pdata = d->host_data;
  309. irq_set_chip_data(virq, pdata);
  310. irq_set_chip_and_handler(virq, &pdata->irq_chip, handle_simple_irq);
  311. irq_set_nested_thread(virq, true);
  312. irq_set_parent(virq, pdata->twl_irq);
  313. irq_set_noprobe(virq);
  314. return 0;
  315. }
  316. static void twl6030_irq_unmap(struct irq_domain *d, unsigned int virq)
  317. {
  318. irq_set_chip_and_handler(virq, NULL, NULL);
  319. irq_set_chip_data(virq, NULL);
  320. }
  321. static const struct irq_domain_ops twl6030_irq_domain_ops = {
  322. .map = twl6030_irq_map,
  323. .unmap = twl6030_irq_unmap,
  324. .xlate = irq_domain_xlate_onetwocell,
  325. };
  326. static const struct of_device_id twl6030_of_match[] = {
  327. {.compatible = "ti,twl6030", &twl6030_interrupt_mapping},
  328. {.compatible = "ti,twl6032", &twl6032_interrupt_mapping},
  329. { },
  330. };
  331. int twl6030_init_irq(struct device *dev, int irq_num)
  332. {
  333. struct device_node *node = dev->of_node;
  334. int nr_irqs;
  335. int status;
  336. u8 mask[3];
  337. const struct of_device_id *of_id;
  338. of_id = of_match_device(twl6030_of_match, dev);
  339. if (!of_id || !of_id->data) {
  340. dev_err(dev, "Unknown TWL device model\n");
  341. return -EINVAL;
  342. }
  343. nr_irqs = TWL6030_NR_IRQS;
  344. twl6030_irq = devm_kzalloc(dev, sizeof(*twl6030_irq), GFP_KERNEL);
  345. if (!twl6030_irq) {
  346. dev_err(dev, "twl6030_irq: Memory allocation failed\n");
  347. return -ENOMEM;
  348. }
  349. mask[0] = 0xFF;
  350. mask[1] = 0xFF;
  351. mask[2] = 0xFF;
  352. /* mask all int lines */
  353. status = twl_i2c_write(TWL_MODULE_PIH, &mask[0], REG_INT_MSK_LINE_A, 3);
  354. /* mask all int sts */
  355. status |= twl_i2c_write(TWL_MODULE_PIH, &mask[0], REG_INT_MSK_STS_A, 3);
  356. /* clear INT_STS_A,B,C */
  357. status |= twl_i2c_write(TWL_MODULE_PIH, &mask[0], REG_INT_STS_A, 3);
  358. if (status < 0) {
  359. dev_err(dev, "I2C err writing TWL_MODULE_PIH: %d\n", status);
  360. return status;
  361. }
  362. /*
  363. * install an irq handler for each of the modules;
  364. * clone dummy irq_chip since PIH can't *do* anything
  365. */
  366. twl6030_irq->irq_chip = dummy_irq_chip;
  367. twl6030_irq->irq_chip.name = "twl6030";
  368. twl6030_irq->irq_chip.irq_set_type = NULL;
  369. twl6030_irq->irq_chip.irq_set_wake = twl6030_irq_set_wake;
  370. twl6030_irq->pm_nb.notifier_call = twl6030_irq_pm_notifier;
  371. atomic_set(&twl6030_irq->wakeirqs, 0);
  372. twl6030_irq->irq_mapping_tbl = of_id->data;
  373. twl6030_irq->irq_domain =
  374. irq_domain_add_linear(node, nr_irqs,
  375. &twl6030_irq_domain_ops, twl6030_irq);
  376. if (!twl6030_irq->irq_domain) {
  377. dev_err(dev, "Can't add irq_domain\n");
  378. return -ENOMEM;
  379. }
  380. dev_info(dev, "PIH (irq %d) nested IRQs\n", irq_num);
  381. /* install an irq handler to demultiplex the TWL6030 interrupt */
  382. status = request_threaded_irq(irq_num, NULL, twl6030_irq_thread,
  383. IRQF_ONESHOT, "TWL6030-PIH", twl6030_irq);
  384. if (status < 0) {
  385. dev_err(dev, "could not claim irq %d: %d\n", irq_num, status);
  386. goto fail_irq;
  387. }
  388. twl6030_irq->twl_irq = irq_num;
  389. register_pm_notifier(&twl6030_irq->pm_nb);
  390. return 0;
  391. fail_irq:
  392. irq_domain_remove(twl6030_irq->irq_domain);
  393. return status;
  394. }
  395. int twl6030_exit_irq(void)
  396. {
  397. if (twl6030_irq && twl6030_irq->twl_irq) {
  398. unregister_pm_notifier(&twl6030_irq->pm_nb);
  399. free_irq(twl6030_irq->twl_irq, NULL);
  400. /*
  401. * TODO: IRQ domain and allocated nested IRQ descriptors
  402. * should be freed somehow here. Now It can't be done, because
  403. * child devices will not be deleted during removing of
  404. * TWL Core driver and they will still contain allocated
  405. * virt IRQs in their Resources tables.
  406. * The same prevents us from using devm_request_threaded_irq()
  407. * in this module.
  408. */
  409. }
  410. return 0;
  411. }