twl6030-irq.c 14 KB

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