twl4030-irq.c 20 KB

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