twl4030-irq.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * twl4030-irq.c - TWL4030/TPS659x0 irq support
  4. *
  5. * Copyright (C) 2005-2006 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. #include <linux/device.h>
  17. #include <linux/export.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/irq.h>
  20. #include <linux/slab.h>
  21. #include <linux/of.h>
  22. #include <linux/irqdomain.h>
  23. #include <linux/mfd/twl.h>
  24. #include "twl-core.h"
  25. /*
  26. * TWL4030 IRQ handling has two stages in hardware, and thus in software.
  27. * The Primary Interrupt Handler (PIH) stage exposes status bits saying
  28. * which Secondary Interrupt Handler (SIH) stage is raising an interrupt.
  29. * SIH modules are more traditional IRQ components, which support per-IRQ
  30. * enable/disable and trigger controls; they do most of the work.
  31. *
  32. * These chips are designed to support IRQ handling from two different
  33. * I2C masters. Each has a dedicated IRQ line, and dedicated IRQ status
  34. * and mask registers in the PIH and SIH modules.
  35. *
  36. * We set up IRQs starting at a platform-specified base, always starting
  37. * with PIH and the SIH for PWR_INT and then usually adding GPIO:
  38. * base + 0 .. base + 7 PIH
  39. * base + 8 .. base + 15 SIH for PWR_INT
  40. * base + 16 .. base + 33 SIH for GPIO
  41. */
  42. #define TWL4030_CORE_NR_IRQS 8
  43. #define TWL4030_PWR_NR_IRQS 8
  44. /* PIH register offsets */
  45. #define REG_PIH_ISR_P1 0x01
  46. #define REG_PIH_ISR_P2 0x02
  47. #define REG_PIH_SIR 0x03 /* for testing */
  48. /* Linux could (eventually) use either IRQ line */
  49. static int irq_line;
  50. struct sih {
  51. char name[8];
  52. u8 module; /* module id */
  53. u8 control_offset; /* for SIH_CTRL */
  54. bool set_cor;
  55. u8 bits; /* valid in isr/imr */
  56. u8 bytes_ixr; /* bytelen of ISR/IMR/SIR */
  57. u8 edr_offset;
  58. u8 bytes_edr; /* bytelen of EDR */
  59. u8 irq_lines; /* number of supported irq lines */
  60. /* SIR ignored -- set interrupt, for testing only */
  61. struct sih_irq_data {
  62. u8 isr_offset;
  63. u8 imr_offset;
  64. } mask[2];
  65. /* + 2 bytes padding */
  66. };
  67. static const struct sih *sih_modules;
  68. static int nr_sih_modules;
  69. #define SIH_INITIALIZER(modname, nbits) \
  70. .module = TWL4030_MODULE_ ## modname, \
  71. .control_offset = TWL4030_ ## modname ## _SIH_CTRL, \
  72. .bits = nbits, \
  73. .bytes_ixr = DIV_ROUND_UP(nbits, 8), \
  74. .edr_offset = TWL4030_ ## modname ## _EDR, \
  75. .bytes_edr = DIV_ROUND_UP((2*(nbits)), 8), \
  76. .irq_lines = 2, \
  77. .mask = { { \
  78. .isr_offset = TWL4030_ ## modname ## _ISR1, \
  79. .imr_offset = TWL4030_ ## modname ## _IMR1, \
  80. }, \
  81. { \
  82. .isr_offset = TWL4030_ ## modname ## _ISR2, \
  83. .imr_offset = TWL4030_ ## modname ## _IMR2, \
  84. }, },
  85. /* register naming policies are inconsistent ... */
  86. #define TWL4030_INT_PWR_EDR TWL4030_INT_PWR_EDR1
  87. #define TWL4030_MODULE_KEYPAD_KEYP TWL4030_MODULE_KEYPAD
  88. #define TWL4030_MODULE_INT_PWR TWL4030_MODULE_INT
  89. /*
  90. * Order in this table matches order in PIH_ISR. That is,
  91. * BIT(n) in PIH_ISR is sih_modules[n].
  92. */
  93. /* sih_modules_twl4030 is used both in twl4030 and twl5030 */
  94. static const struct sih sih_modules_twl4030[6] = {
  95. [0] = {
  96. .name = "gpio",
  97. .module = TWL4030_MODULE_GPIO,
  98. .control_offset = REG_GPIO_SIH_CTRL,
  99. .set_cor = true,
  100. .bits = TWL4030_GPIO_MAX,
  101. .bytes_ixr = 3,
  102. /* Note: *all* of these IRQs default to no-trigger */
  103. .edr_offset = REG_GPIO_EDR1,
  104. .bytes_edr = 5,
  105. .irq_lines = 2,
  106. .mask = { {
  107. .isr_offset = REG_GPIO_ISR1A,
  108. .imr_offset = REG_GPIO_IMR1A,
  109. }, {
  110. .isr_offset = REG_GPIO_ISR1B,
  111. .imr_offset = REG_GPIO_IMR1B,
  112. }, },
  113. },
  114. [1] = {
  115. .name = "keypad",
  116. .set_cor = true,
  117. SIH_INITIALIZER(KEYPAD_KEYP, 4)
  118. },
  119. [2] = {
  120. .name = "bci",
  121. .module = TWL4030_MODULE_INTERRUPTS,
  122. .control_offset = TWL4030_INTERRUPTS_BCISIHCTRL,
  123. .set_cor = true,
  124. .bits = 12,
  125. .bytes_ixr = 2,
  126. .edr_offset = TWL4030_INTERRUPTS_BCIEDR1,
  127. /* Note: most of these IRQs default to no-trigger */
  128. .bytes_edr = 3,
  129. .irq_lines = 2,
  130. .mask = { {
  131. .isr_offset = TWL4030_INTERRUPTS_BCIISR1A,
  132. .imr_offset = TWL4030_INTERRUPTS_BCIIMR1A,
  133. }, {
  134. .isr_offset = TWL4030_INTERRUPTS_BCIISR1B,
  135. .imr_offset = TWL4030_INTERRUPTS_BCIIMR1B,
  136. }, },
  137. },
  138. [3] = {
  139. .name = "madc",
  140. SIH_INITIALIZER(MADC, 4)
  141. },
  142. [4] = {
  143. /* USB doesn't use the same SIH organization */
  144. .name = "usb",
  145. },
  146. [5] = {
  147. .name = "power",
  148. .set_cor = true,
  149. SIH_INITIALIZER(INT_PWR, 8)
  150. },
  151. /* there are no SIH modules #6 or #7 ... */
  152. };
  153. static const struct sih sih_modules_twl5031[8] = {
  154. [0] = {
  155. .name = "gpio",
  156. .module = TWL4030_MODULE_GPIO,
  157. .control_offset = REG_GPIO_SIH_CTRL,
  158. .set_cor = true,
  159. .bits = TWL4030_GPIO_MAX,
  160. .bytes_ixr = 3,
  161. /* Note: *all* of these IRQs default to no-trigger */
  162. .edr_offset = REG_GPIO_EDR1,
  163. .bytes_edr = 5,
  164. .irq_lines = 2,
  165. .mask = { {
  166. .isr_offset = REG_GPIO_ISR1A,
  167. .imr_offset = REG_GPIO_IMR1A,
  168. }, {
  169. .isr_offset = REG_GPIO_ISR1B,
  170. .imr_offset = REG_GPIO_IMR1B,
  171. }, },
  172. },
  173. [1] = {
  174. .name = "keypad",
  175. .set_cor = true,
  176. SIH_INITIALIZER(KEYPAD_KEYP, 4)
  177. },
  178. [2] = {
  179. .name = "bci",
  180. .module = TWL5031_MODULE_INTERRUPTS,
  181. .control_offset = TWL5031_INTERRUPTS_BCISIHCTRL,
  182. .bits = 7,
  183. .bytes_ixr = 1,
  184. .edr_offset = TWL5031_INTERRUPTS_BCIEDR1,
  185. /* Note: most of these IRQs default to no-trigger */
  186. .bytes_edr = 2,
  187. .irq_lines = 2,
  188. .mask = { {
  189. .isr_offset = TWL5031_INTERRUPTS_BCIISR1,
  190. .imr_offset = TWL5031_INTERRUPTS_BCIIMR1,
  191. }, {
  192. .isr_offset = TWL5031_INTERRUPTS_BCIISR2,
  193. .imr_offset = TWL5031_INTERRUPTS_BCIIMR2,
  194. }, },
  195. },
  196. [3] = {
  197. .name = "madc",
  198. SIH_INITIALIZER(MADC, 4)
  199. },
  200. [4] = {
  201. /* USB doesn't use the same SIH organization */
  202. .name = "usb",
  203. },
  204. [5] = {
  205. .name = "power",
  206. .set_cor = true,
  207. SIH_INITIALIZER(INT_PWR, 8)
  208. },
  209. [6] = {
  210. /*
  211. * ECI/DBI doesn't use the same SIH organization.
  212. * For example, it supports only one interrupt output line.
  213. * That is, the interrupts are seen on both INT1 and INT2 lines.
  214. */
  215. .name = "eci_dbi",
  216. .module = TWL5031_MODULE_ACCESSORY,
  217. .bits = 9,
  218. .bytes_ixr = 2,
  219. .irq_lines = 1,
  220. .mask = { {
  221. .isr_offset = TWL5031_ACIIDR_LSB,
  222. .imr_offset = TWL5031_ACIIMR_LSB,
  223. }, },
  224. },
  225. [7] = {
  226. /* Audio accessory */
  227. .name = "audio",
  228. .module = TWL5031_MODULE_ACCESSORY,
  229. .control_offset = TWL5031_ACCSIHCTRL,
  230. .bits = 2,
  231. .bytes_ixr = 1,
  232. .edr_offset = TWL5031_ACCEDR1,
  233. /* Note: most of these IRQs default to no-trigger */
  234. .bytes_edr = 1,
  235. .irq_lines = 2,
  236. .mask = { {
  237. .isr_offset = TWL5031_ACCISR1,
  238. .imr_offset = TWL5031_ACCIMR1,
  239. }, {
  240. .isr_offset = TWL5031_ACCISR2,
  241. .imr_offset = TWL5031_ACCIMR2,
  242. }, },
  243. },
  244. };
  245. #undef TWL4030_MODULE_KEYPAD_KEYP
  246. #undef TWL4030_MODULE_INT_PWR
  247. #undef TWL4030_INT_PWR_EDR
  248. /*----------------------------------------------------------------------*/
  249. static unsigned twl4030_irq_base;
  250. /*
  251. * handle_twl4030_pih() is the desc->handle method for the twl4030 interrupt.
  252. * This is a chained interrupt, so there is no desc->action method for it.
  253. * Now we need to query the interrupt controller in the twl4030 to determine
  254. * which module is generating the interrupt request. However, we can't do i2c
  255. * transactions in interrupt context, so we must defer that work to a kernel
  256. * thread. All we do here is acknowledge and mask the interrupt and wakeup
  257. * the kernel thread.
  258. */
  259. static irqreturn_t handle_twl4030_pih(int irq, void *devid)
  260. {
  261. irqreturn_t ret;
  262. u8 pih_isr;
  263. ret = twl_i2c_read_u8(TWL_MODULE_PIH, &pih_isr,
  264. REG_PIH_ISR_P1);
  265. if (ret) {
  266. pr_warn("twl4030: I2C error %d reading PIH ISR\n", ret);
  267. return IRQ_NONE;
  268. }
  269. while (pih_isr) {
  270. unsigned long pending = __ffs(pih_isr);
  271. unsigned int irq;
  272. pih_isr &= ~BIT(pending);
  273. irq = pending + twl4030_irq_base;
  274. handle_nested_irq(irq);
  275. }
  276. return IRQ_HANDLED;
  277. }
  278. /*----------------------------------------------------------------------*/
  279. /*
  280. * twl4030_init_sih_modules() ... start from a known state where no
  281. * IRQs will be coming in, and where we can quickly enable them then
  282. * handle them as they arrive. Mask all IRQs: maybe init SIH_CTRL.
  283. *
  284. * NOTE: we don't touch EDR registers here; they stay with hardware
  285. * defaults or whatever the last value was. Note that when both EDR
  286. * bits for an IRQ are clear, that's as if its IMR bit is set...
  287. */
  288. static int twl4030_init_sih_modules(unsigned line)
  289. {
  290. const struct sih *sih;
  291. u8 buf[4];
  292. int i;
  293. int status;
  294. /* line 0 == int1_n signal; line 1 == int2_n signal */
  295. if (line > 1)
  296. return -EINVAL;
  297. irq_line = line;
  298. /* disable all interrupts on our line */
  299. memset(buf, 0xff, sizeof(buf));
  300. sih = sih_modules;
  301. for (i = 0; i < nr_sih_modules; i++, sih++) {
  302. /* skip USB -- it's funky */
  303. if (!sih->bytes_ixr)
  304. continue;
  305. /* Not all the SIH modules support multiple interrupt lines */
  306. if (sih->irq_lines <= line)
  307. continue;
  308. status = twl_i2c_write(sih->module, buf,
  309. sih->mask[line].imr_offset, sih->bytes_ixr);
  310. if (status < 0)
  311. pr_err("twl4030: err %d initializing %s %s\n",
  312. status, sih->name, "IMR");
  313. /*
  314. * Maybe disable "exclusive" mode; buffer second pending irq;
  315. * set Clear-On-Read (COR) bit.
  316. *
  317. * NOTE that sometimes COR polarity is documented as being
  318. * inverted: for MADC, COR=1 means "clear on write".
  319. * And for PWR_INT it's not documented...
  320. */
  321. if (sih->set_cor) {
  322. status = twl_i2c_write_u8(sih->module,
  323. TWL4030_SIH_CTRL_COR_MASK,
  324. sih->control_offset);
  325. if (status < 0)
  326. pr_err("twl4030: err %d initializing %s %s\n",
  327. status, sih->name, "SIH_CTRL");
  328. }
  329. }
  330. sih = sih_modules;
  331. for (i = 0; i < nr_sih_modules; i++, sih++) {
  332. u8 rxbuf[4];
  333. int j;
  334. /* skip USB */
  335. if (!sih->bytes_ixr)
  336. continue;
  337. /* Not all the SIH modules support multiple interrupt lines */
  338. if (sih->irq_lines <= line)
  339. continue;
  340. /*
  341. * Clear pending interrupt status. Either the read was
  342. * enough, or we need to write those bits. Repeat, in
  343. * case an IRQ is pending (PENDDIS=0) ... that's not
  344. * uncommon with PWR_INT.PWRON.
  345. */
  346. for (j = 0; j < 2; j++) {
  347. status = twl_i2c_read(sih->module, rxbuf,
  348. sih->mask[line].isr_offset, sih->bytes_ixr);
  349. if (status < 0)
  350. pr_warn("twl4030: err %d initializing %s %s\n",
  351. status, sih->name, "ISR");
  352. if (!sih->set_cor) {
  353. status = twl_i2c_write(sih->module, buf,
  354. sih->mask[line].isr_offset,
  355. sih->bytes_ixr);
  356. if (status < 0)
  357. pr_warn("twl4030: write failed: %d\n",
  358. status);
  359. }
  360. /*
  361. * else COR=1 means read sufficed.
  362. * (for most SIH modules...)
  363. */
  364. }
  365. }
  366. return 0;
  367. }
  368. static inline void activate_irq(int irq)
  369. {
  370. irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
  371. }
  372. /*----------------------------------------------------------------------*/
  373. struct sih_agent {
  374. int irq_base;
  375. const struct sih *sih;
  376. u32 imr;
  377. bool imr_change_pending;
  378. u32 edge_change;
  379. struct mutex irq_lock;
  380. char *irq_name;
  381. };
  382. /*----------------------------------------------------------------------*/
  383. /*
  384. * All irq_chip methods get issued from code holding irq_desc[irq].lock,
  385. * which can't perform the underlying I2C operations (because they sleep).
  386. * So we must hand them off to a thread (workqueue) and cope with asynch
  387. * completion, potentially including some re-ordering, of these requests.
  388. */
  389. static void twl4030_sih_mask(struct irq_data *data)
  390. {
  391. struct sih_agent *agent = irq_data_get_irq_chip_data(data);
  392. agent->imr |= BIT(data->irq - agent->irq_base);
  393. agent->imr_change_pending = true;
  394. }
  395. static void twl4030_sih_unmask(struct irq_data *data)
  396. {
  397. struct sih_agent *agent = irq_data_get_irq_chip_data(data);
  398. agent->imr &= ~BIT(data->irq - agent->irq_base);
  399. agent->imr_change_pending = true;
  400. }
  401. static int twl4030_sih_set_type(struct irq_data *data, unsigned trigger)
  402. {
  403. struct sih_agent *agent = irq_data_get_irq_chip_data(data);
  404. if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  405. return -EINVAL;
  406. if (irqd_get_trigger_type(data) != trigger)
  407. agent->edge_change |= BIT(data->irq - agent->irq_base);
  408. return 0;
  409. }
  410. static void twl4030_sih_bus_lock(struct irq_data *data)
  411. {
  412. struct sih_agent *agent = irq_data_get_irq_chip_data(data);
  413. mutex_lock(&agent->irq_lock);
  414. }
  415. static void twl4030_sih_bus_sync_unlock(struct irq_data *data)
  416. {
  417. struct sih_agent *agent = irq_data_get_irq_chip_data(data);
  418. const struct sih *sih = agent->sih;
  419. int status;
  420. if (agent->imr_change_pending) {
  421. union {
  422. __le32 word;
  423. u8 bytes[4];
  424. } imr;
  425. /* byte[0] gets overwritten as we write ... */
  426. imr.word = cpu_to_le32(agent->imr);
  427. agent->imr_change_pending = false;
  428. /* write the whole mask ... simpler than subsetting it */
  429. status = twl_i2c_write(sih->module, imr.bytes,
  430. sih->mask[irq_line].imr_offset,
  431. sih->bytes_ixr);
  432. if (status)
  433. pr_err("twl4030: %s, %s --> %d\n", __func__,
  434. "write", status);
  435. }
  436. if (agent->edge_change) {
  437. u32 edge_change;
  438. u8 bytes[6];
  439. edge_change = agent->edge_change;
  440. agent->edge_change = 0;
  441. /*
  442. * Read, reserving first byte for write scratch. Yes, this
  443. * could be cached for some speedup ... but be careful about
  444. * any processor on the other IRQ line, EDR registers are
  445. * shared.
  446. */
  447. status = twl_i2c_read(sih->module, bytes,
  448. sih->edr_offset, sih->bytes_edr);
  449. if (status) {
  450. pr_err("twl4030: %s, %s --> %d\n", __func__,
  451. "read", status);
  452. return;
  453. }
  454. /* Modify only the bits we know must change */
  455. while (edge_change) {
  456. int i = fls(edge_change) - 1;
  457. int byte = i >> 2;
  458. int off = (i & 0x3) * 2;
  459. unsigned int type;
  460. bytes[byte] &= ~(0x03 << off);
  461. type = irq_get_trigger_type(i + agent->irq_base);
  462. if (type & IRQ_TYPE_EDGE_RISING)
  463. bytes[byte] |= BIT(off + 1);
  464. if (type & IRQ_TYPE_EDGE_FALLING)
  465. bytes[byte] |= BIT(off + 0);
  466. edge_change &= ~BIT(i);
  467. }
  468. /* Write */
  469. status = twl_i2c_write(sih->module, bytes,
  470. sih->edr_offset, sih->bytes_edr);
  471. if (status)
  472. pr_err("twl4030: %s, %s --> %d\n", __func__,
  473. "write", status);
  474. }
  475. mutex_unlock(&agent->irq_lock);
  476. }
  477. static struct irq_chip twl4030_sih_irq_chip = {
  478. .name = "twl4030",
  479. .irq_mask = twl4030_sih_mask,
  480. .irq_unmask = twl4030_sih_unmask,
  481. .irq_set_type = twl4030_sih_set_type,
  482. .irq_bus_lock = twl4030_sih_bus_lock,
  483. .irq_bus_sync_unlock = twl4030_sih_bus_sync_unlock,
  484. .flags = IRQCHIP_SKIP_SET_WAKE,
  485. };
  486. /*----------------------------------------------------------------------*/
  487. static inline int sih_read_isr(const struct sih *sih)
  488. {
  489. int status;
  490. union {
  491. u8 bytes[4];
  492. __le32 word;
  493. } isr;
  494. /* FIXME need retry-on-error ... */
  495. isr.word = 0;
  496. status = twl_i2c_read(sih->module, isr.bytes,
  497. sih->mask[irq_line].isr_offset, sih->bytes_ixr);
  498. return (status < 0) ? status : le32_to_cpu(isr.word);
  499. }
  500. /*
  501. * Generic handler for SIH interrupts ... we "know" this is called
  502. * in task context, with IRQs enabled.
  503. */
  504. static irqreturn_t handle_twl4030_sih(int irq, void *data)
  505. {
  506. struct sih_agent *agent = irq_get_handler_data(irq);
  507. const struct sih *sih = agent->sih;
  508. int isr;
  509. /* reading ISR acks the IRQs, using clear-on-read mode */
  510. isr = sih_read_isr(sih);
  511. if (isr < 0) {
  512. pr_err("twl4030: %s SIH, read ISR error %d\n",
  513. sih->name, isr);
  514. /* REVISIT: recover; eventually mask it all, etc */
  515. return IRQ_HANDLED;
  516. }
  517. while (isr) {
  518. irq = fls(isr);
  519. irq--;
  520. isr &= ~BIT(irq);
  521. if (irq < sih->bits)
  522. handle_nested_irq(agent->irq_base + irq);
  523. else
  524. pr_err("twl4030: %s SIH, invalid ISR bit %d\n",
  525. sih->name, irq);
  526. }
  527. return IRQ_HANDLED;
  528. }
  529. /* returns the first IRQ used by this SIH bank, or negative errno */
  530. int twl4030_sih_setup(struct device *dev, int module, int irq_base)
  531. {
  532. int sih_mod;
  533. const struct sih *sih = NULL;
  534. struct sih_agent *agent;
  535. int i, irq;
  536. int status = -EINVAL;
  537. /* only support modules with standard clear-on-read for now */
  538. for (sih_mod = 0, sih = sih_modules; sih_mod < nr_sih_modules;
  539. sih_mod++, sih++) {
  540. if (sih->module == module && sih->set_cor) {
  541. status = 0;
  542. break;
  543. }
  544. }
  545. if (status < 0) {
  546. dev_err(dev, "module to setup SIH for not found\n");
  547. return status;
  548. }
  549. agent = kzalloc(sizeof(*agent), GFP_KERNEL);
  550. if (!agent)
  551. return -ENOMEM;
  552. agent->irq_base = irq_base;
  553. agent->sih = sih;
  554. agent->imr = ~0;
  555. mutex_init(&agent->irq_lock);
  556. for (i = 0; i < sih->bits; i++) {
  557. irq = irq_base + i;
  558. irq_set_chip_data(irq, agent);
  559. irq_set_chip_and_handler(irq, &twl4030_sih_irq_chip,
  560. handle_edge_irq);
  561. irq_set_nested_thread(irq, 1);
  562. activate_irq(irq);
  563. }
  564. /* replace generic PIH handler (handle_simple_irq) */
  565. irq = sih_mod + twl4030_irq_base;
  566. irq_set_handler_data(irq, agent);
  567. agent->irq_name = kasprintf(GFP_KERNEL, "twl4030_%s", sih->name);
  568. status = request_threaded_irq(irq, NULL, handle_twl4030_sih,
  569. IRQF_EARLY_RESUME | IRQF_ONESHOT,
  570. agent->irq_name ?: sih->name, NULL);
  571. dev_info(dev, "%s (irq %d) chaining IRQs %d..%d\n", sih->name,
  572. irq, irq_base, irq_base + i - 1);
  573. return status < 0 ? status : irq_base;
  574. }
  575. /* FIXME need a call to reverse twl4030_sih_setup() ... */
  576. /*----------------------------------------------------------------------*/
  577. /* FIXME pass in which interrupt line we'll use ... */
  578. #define twl_irq_line 0
  579. int twl4030_init_irq(struct device *dev, int irq_num)
  580. {
  581. static struct irq_chip twl4030_irq_chip;
  582. int status, i;
  583. int irq_base, irq_end, nr_irqs;
  584. struct device_node *node = dev->of_node;
  585. /*
  586. * TWL core and pwr interrupts must be contiguous because
  587. * the hwirqs numbers are defined contiguously from 1 to 15.
  588. * Create only one domain for both.
  589. */
  590. nr_irqs = TWL4030_PWR_NR_IRQS + TWL4030_CORE_NR_IRQS;
  591. irq_base = irq_alloc_descs(-1, 0, nr_irqs, 0);
  592. if (irq_base < 0) {
  593. dev_err(dev, "Fail to allocate IRQ descs\n");
  594. return irq_base;
  595. }
  596. irq_domain_add_legacy(node, nr_irqs, irq_base, 0,
  597. &irq_domain_simple_ops, NULL);
  598. irq_end = irq_base + TWL4030_CORE_NR_IRQS;
  599. /*
  600. * Mask and clear all TWL4030 interrupts since initially we do
  601. * not have any TWL4030 module interrupt handlers present
  602. */
  603. status = twl4030_init_sih_modules(twl_irq_line);
  604. if (status < 0)
  605. return status;
  606. twl4030_irq_base = irq_base;
  607. /*
  608. * Install an irq handler for each of the SIH modules;
  609. * clone dummy irq_chip since PIH can't *do* anything
  610. */
  611. twl4030_irq_chip = dummy_irq_chip;
  612. twl4030_irq_chip.name = "twl4030";
  613. twl4030_sih_irq_chip.irq_ack = dummy_irq_chip.irq_ack;
  614. for (i = irq_base; i < irq_end; i++) {
  615. irq_set_chip_and_handler(i, &twl4030_irq_chip,
  616. handle_simple_irq);
  617. irq_set_nested_thread(i, 1);
  618. activate_irq(i);
  619. }
  620. dev_info(dev, "%s (irq %d) chaining IRQs %d..%d\n", "PIH",
  621. irq_num, irq_base, irq_end);
  622. /* ... and the PWR_INT module ... */
  623. status = twl4030_sih_setup(dev, TWL4030_MODULE_INT, irq_end);
  624. if (status < 0) {
  625. dev_err(dev, "sih_setup PWR INT --> %d\n", status);
  626. goto fail;
  627. }
  628. /* install an irq handler to demultiplex the TWL4030 interrupt */
  629. status = request_threaded_irq(irq_num, NULL, handle_twl4030_pih,
  630. IRQF_ONESHOT,
  631. "TWL4030-PIH", NULL);
  632. if (status < 0) {
  633. dev_err(dev, "could not claim irq%d: %d\n", irq_num, status);
  634. goto fail_rqirq;
  635. }
  636. enable_irq_wake(irq_num);
  637. return irq_base;
  638. fail_rqirq:
  639. /* clean up twl4030_sih_setup */
  640. fail:
  641. for (i = irq_base; i < irq_end; i++) {
  642. irq_set_nested_thread(i, 0);
  643. irq_set_chip_and_handler(i, NULL, NULL);
  644. }
  645. return status;
  646. }
  647. void twl4030_exit_irq(void)
  648. {
  649. /* FIXME undo twl_init_irq() */
  650. if (twl4030_irq_base)
  651. pr_err("twl4030: can't yet clean up IRQs?\n");
  652. }
  653. int twl4030_init_chip_irq(const char *chip)
  654. {
  655. if (!strcmp(chip, "twl5031")) {
  656. sih_modules = sih_modules_twl5031;
  657. nr_sih_modules = ARRAY_SIZE(sih_modules_twl5031);
  658. } else {
  659. sih_modules = sih_modules_twl4030;
  660. nr_sih_modules = ARRAY_SIZE(sih_modules_twl4030);
  661. }
  662. return 0;
  663. }