twl6030-irq.c 13 KB

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