twl4030-irq.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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/init.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/irq.h>
  32. #include <linux/kthread.h>
  33. #include <linux/i2c/twl4030.h>
  34. /*
  35. * TWL4030 IRQ handling has two stages in hardware, and thus in software.
  36. * The Primary Interrupt Handler (PIH) stage exposes status bits saying
  37. * which Secondary Interrupt Handler (SIH) stage is raising an interrupt.
  38. * SIH modules are more traditional IRQ components, which support per-IRQ
  39. * enable/disable and trigger controls; they do most of the work.
  40. *
  41. * These chips are designed to support IRQ handling from two different
  42. * I2C masters. Each has a dedicated IRQ line, and dedicated IRQ status
  43. * and mask registers in the PIH and SIH modules.
  44. *
  45. * We set up IRQs starting at a platform-specified base, always starting
  46. * with PIH and the SIH for PWR_INT and then usually adding GPIO:
  47. * base + 0 .. base + 7 PIH
  48. * base + 8 .. base + 15 SIH for PWR_INT
  49. * base + 16 .. base + 33 SIH for GPIO
  50. */
  51. /* PIH register offsets */
  52. #define REG_PIH_ISR_P1 0x01
  53. #define REG_PIH_ISR_P2 0x02
  54. #define REG_PIH_SIR 0x03 /* for testing */
  55. /* Linux could (eventually) use either IRQ line */
  56. static int irq_line;
  57. struct sih {
  58. char name[8];
  59. u8 module; /* module id */
  60. u8 control_offset; /* for SIH_CTRL */
  61. bool set_cor;
  62. u8 bits; /* valid in isr/imr */
  63. u8 bytes_ixr; /* bytelen of ISR/IMR/SIR */
  64. u8 edr_offset;
  65. u8 bytes_edr; /* bytelen of EDR */
  66. /* SIR ignored -- set interrupt, for testing only */
  67. struct irq_data {
  68. u8 isr_offset;
  69. u8 imr_offset;
  70. } mask[2];
  71. /* + 2 bytes padding */
  72. };
  73. #define SIH_INITIALIZER(modname, nbits) \
  74. .module = TWL4030_MODULE_ ## modname, \
  75. .control_offset = TWL4030_ ## modname ## _SIH_CTRL, \
  76. .bits = nbits, \
  77. .bytes_ixr = DIV_ROUND_UP(nbits, 8), \
  78. .edr_offset = TWL4030_ ## modname ## _EDR, \
  79. .bytes_edr = DIV_ROUND_UP((2*(nbits)), 8), \
  80. .mask = { { \
  81. .isr_offset = TWL4030_ ## modname ## _ISR1, \
  82. .imr_offset = TWL4030_ ## modname ## _IMR1, \
  83. }, \
  84. { \
  85. .isr_offset = TWL4030_ ## modname ## _ISR2, \
  86. .imr_offset = TWL4030_ ## modname ## _IMR2, \
  87. }, },
  88. /* register naming policies are inconsistent ... */
  89. #define TWL4030_INT_PWR_EDR TWL4030_INT_PWR_EDR1
  90. #define TWL4030_MODULE_KEYPAD_KEYP TWL4030_MODULE_KEYPAD
  91. #define TWL4030_MODULE_INT_PWR TWL4030_MODULE_INT
  92. /* Order in this table matches order in PIH_ISR. That is,
  93. * BIT(n) in PIH_ISR is sih_modules[n].
  94. */
  95. static const struct sih sih_modules[6] = {
  96. [0] = {
  97. .name = "gpio",
  98. .module = TWL4030_MODULE_GPIO,
  99. .control_offset = REG_GPIO_SIH_CTRL,
  100. .set_cor = true,
  101. .bits = TWL4030_GPIO_MAX,
  102. .bytes_ixr = 3,
  103. /* Note: *all* of these IRQs default to no-trigger */
  104. .edr_offset = REG_GPIO_EDR1,
  105. .bytes_edr = 5,
  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. .bits = 12,
  124. .bytes_ixr = 2,
  125. .edr_offset = TWL4030_INTERRUPTS_BCIEDR1,
  126. /* Note: most of these IRQs default to no-trigger */
  127. .bytes_edr = 3,
  128. .mask = { {
  129. .isr_offset = TWL4030_INTERRUPTS_BCIISR1A,
  130. .imr_offset = TWL4030_INTERRUPTS_BCIIMR1A,
  131. }, {
  132. .isr_offset = TWL4030_INTERRUPTS_BCIISR1B,
  133. .imr_offset = TWL4030_INTERRUPTS_BCIIMR1B,
  134. }, },
  135. },
  136. [3] = {
  137. .name = "madc",
  138. SIH_INITIALIZER(MADC, 4)
  139. },
  140. [4] = {
  141. /* USB doesn't use the same SIH organization */
  142. .name = "usb",
  143. },
  144. [5] = {
  145. .name = "power",
  146. .set_cor = true,
  147. SIH_INITIALIZER(INT_PWR, 8)
  148. },
  149. /* there are no SIH modules #6 or #7 ... */
  150. };
  151. #undef TWL4030_MODULE_KEYPAD_KEYP
  152. #undef TWL4030_MODULE_INT_PWR
  153. #undef TWL4030_INT_PWR_EDR
  154. /*----------------------------------------------------------------------*/
  155. static unsigned twl4030_irq_base;
  156. static struct completion irq_event;
  157. /*
  158. * This thread processes interrupts reported by the Primary Interrupt Handler.
  159. */
  160. static int twl4030_irq_thread(void *data)
  161. {
  162. long irq = (long)data;
  163. struct irq_desc *desc = irq_to_desc(irq);
  164. static unsigned i2c_errors;
  165. static const unsigned max_i2c_errors = 100;
  166. if (!desc) {
  167. pr_err("twl4030: Invalid IRQ: %ld\n", irq);
  168. return -EINVAL;
  169. }
  170. current->flags |= PF_NOFREEZE;
  171. while (!kthread_should_stop()) {
  172. int ret;
  173. int module_irq;
  174. u8 pih_isr;
  175. /* Wait for IRQ, then read PIH irq status (also blocking) */
  176. wait_for_completion_interruptible(&irq_event);
  177. ret = twl4030_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
  178. REG_PIH_ISR_P1);
  179. if (ret) {
  180. pr_warning("twl4030: I2C error %d reading PIH ISR\n",
  181. ret);
  182. if (++i2c_errors >= max_i2c_errors) {
  183. printk(KERN_ERR "Maximum I2C error count"
  184. " exceeded. Terminating %s.\n",
  185. __func__);
  186. break;
  187. }
  188. complete(&irq_event);
  189. continue;
  190. }
  191. /* these handlers deal with the relevant SIH irq status */
  192. local_irq_disable();
  193. for (module_irq = twl4030_irq_base;
  194. pih_isr;
  195. pih_isr >>= 1, module_irq++) {
  196. if (pih_isr & 0x1) {
  197. struct irq_desc *d = irq_to_desc(module_irq);
  198. if (!d) {
  199. pr_err("twl4030: Invalid SIH IRQ: %d\n",
  200. module_irq);
  201. return -EINVAL;
  202. }
  203. /* These can't be masked ... always warn
  204. * if we get any surprises.
  205. */
  206. if (d->status & IRQ_DISABLED)
  207. note_interrupt(module_irq, d,
  208. IRQ_NONE);
  209. else
  210. d->handle_irq(module_irq, d);
  211. }
  212. }
  213. local_irq_enable();
  214. desc->chip->unmask(irq);
  215. }
  216. return 0;
  217. }
  218. /*
  219. * handle_twl4030_pih() is the desc->handle method for the twl4030 interrupt.
  220. * This is a chained interrupt, so there is no desc->action method for it.
  221. * Now we need to query the interrupt controller in the twl4030 to determine
  222. * which module is generating the interrupt request. However, we can't do i2c
  223. * transactions in interrupt context, so we must defer that work to a kernel
  224. * thread. All we do here is acknowledge and mask the interrupt and wakeup
  225. * the kernel thread.
  226. */
  227. static void handle_twl4030_pih(unsigned int irq, irq_desc_t *desc)
  228. {
  229. /* Acknowledge, clear *AND* mask the interrupt... */
  230. desc->chip->ack(irq);
  231. complete(&irq_event);
  232. }
  233. static struct task_struct *start_twl4030_irq_thread(long irq)
  234. {
  235. struct task_struct *thread;
  236. init_completion(&irq_event);
  237. thread = kthread_run(twl4030_irq_thread, (void *)irq, "twl4030-irq");
  238. if (!thread)
  239. pr_err("twl4030: could not create irq %ld thread!\n", irq);
  240. return thread;
  241. }
  242. /*----------------------------------------------------------------------*/
  243. /*
  244. * twl4030_init_sih_modules() ... start from a known state where no
  245. * IRQs will be coming in, and where we can quickly enable them then
  246. * handle them as they arrive. Mask all IRQs: maybe init SIH_CTRL.
  247. *
  248. * NOTE: we don't touch EDR registers here; they stay with hardware
  249. * defaults or whatever the last value was. Note that when both EDR
  250. * bits for an IRQ are clear, that's as if its IMR bit is set...
  251. */
  252. static int twl4030_init_sih_modules(unsigned line)
  253. {
  254. const struct sih *sih;
  255. u8 buf[4];
  256. int i;
  257. int status;
  258. /* line 0 == int1_n signal; line 1 == int2_n signal */
  259. if (line > 1)
  260. return -EINVAL;
  261. irq_line = line;
  262. /* disable all interrupts on our line */
  263. memset(buf, 0xff, sizeof buf);
  264. sih = sih_modules;
  265. for (i = 0; i < ARRAY_SIZE(sih_modules); i++, sih++) {
  266. /* skip USB -- it's funky */
  267. if (!sih->bytes_ixr)
  268. continue;
  269. status = twl4030_i2c_write(sih->module, buf,
  270. sih->mask[line].imr_offset, sih->bytes_ixr);
  271. if (status < 0)
  272. pr_err("twl4030: err %d initializing %s %s\n",
  273. status, sih->name, "IMR");
  274. /* Maybe disable "exclusive" mode; buffer second pending irq;
  275. * set Clear-On-Read (COR) bit.
  276. *
  277. * NOTE that sometimes COR polarity is documented as being
  278. * inverted: for MADC and BCI, COR=1 means "clear on write".
  279. * And for PWR_INT it's not documented...
  280. */
  281. if (sih->set_cor) {
  282. status = twl4030_i2c_write_u8(sih->module,
  283. TWL4030_SIH_CTRL_COR_MASK,
  284. sih->control_offset);
  285. if (status < 0)
  286. pr_err("twl4030: err %d initializing %s %s\n",
  287. status, sih->name, "SIH_CTRL");
  288. }
  289. }
  290. sih = sih_modules;
  291. for (i = 0; i < ARRAY_SIZE(sih_modules); i++, sih++) {
  292. u8 rxbuf[4];
  293. int j;
  294. /* skip USB */
  295. if (!sih->bytes_ixr)
  296. continue;
  297. /* Clear pending interrupt status. Either the read was
  298. * enough, or we need to write those bits. Repeat, in
  299. * case an IRQ is pending (PENDDIS=0) ... that's not
  300. * uncommon with PWR_INT.PWRON.
  301. */
  302. for (j = 0; j < 2; j++) {
  303. status = twl4030_i2c_read(sih->module, rxbuf,
  304. sih->mask[line].isr_offset, sih->bytes_ixr);
  305. if (status < 0)
  306. pr_err("twl4030: err %d initializing %s %s\n",
  307. status, sih->name, "ISR");
  308. if (!sih->set_cor)
  309. status = twl4030_i2c_write(sih->module, buf,
  310. sih->mask[line].isr_offset,
  311. sih->bytes_ixr);
  312. /* else COR=1 means read sufficed.
  313. * (for most SIH modules...)
  314. */
  315. }
  316. }
  317. return 0;
  318. }
  319. static inline void activate_irq(int irq)
  320. {
  321. #ifdef CONFIG_ARM
  322. /* ARM requires an extra step to clear IRQ_NOREQUEST, which it
  323. * sets on behalf of every irq_chip. Also sets IRQ_NOPROBE.
  324. */
  325. set_irq_flags(irq, IRQF_VALID);
  326. #else
  327. /* same effect on other architectures */
  328. set_irq_noprobe(irq);
  329. #endif
  330. }
  331. /*----------------------------------------------------------------------*/
  332. static DEFINE_SPINLOCK(sih_agent_lock);
  333. static struct workqueue_struct *wq;
  334. struct sih_agent {
  335. int irq_base;
  336. const struct sih *sih;
  337. u32 imr;
  338. bool imr_change_pending;
  339. struct work_struct mask_work;
  340. u32 edge_change;
  341. struct work_struct edge_work;
  342. };
  343. static void twl4030_sih_do_mask(struct work_struct *work)
  344. {
  345. struct sih_agent *agent;
  346. const struct sih *sih;
  347. union {
  348. u8 bytes[4];
  349. u32 word;
  350. } imr;
  351. int status;
  352. agent = container_of(work, struct sih_agent, mask_work);
  353. /* see what work we have */
  354. spin_lock_irq(&sih_agent_lock);
  355. if (agent->imr_change_pending) {
  356. sih = agent->sih;
  357. /* byte[0] gets overwritten as we write ... */
  358. imr.word = cpu_to_le32(agent->imr << 8);
  359. agent->imr_change_pending = false;
  360. } else
  361. sih = NULL;
  362. spin_unlock_irq(&sih_agent_lock);
  363. if (!sih)
  364. return;
  365. /* write the whole mask ... simpler than subsetting it */
  366. status = twl4030_i2c_write(sih->module, imr.bytes,
  367. sih->mask[irq_line].imr_offset, sih->bytes_ixr);
  368. if (status)
  369. pr_err("twl4030: %s, %s --> %d\n", __func__,
  370. "write", status);
  371. }
  372. static void twl4030_sih_do_edge(struct work_struct *work)
  373. {
  374. struct sih_agent *agent;
  375. const struct sih *sih;
  376. u8 bytes[6];
  377. u32 edge_change;
  378. int status;
  379. agent = container_of(work, struct sih_agent, edge_work);
  380. /* see what work we have */
  381. spin_lock_irq(&sih_agent_lock);
  382. edge_change = agent->edge_change;
  383. agent->edge_change = 0;;
  384. sih = edge_change ? agent->sih : NULL;
  385. spin_unlock_irq(&sih_agent_lock);
  386. if (!sih)
  387. return;
  388. /* Read, reserving first byte for write scratch. Yes, this
  389. * could be cached for some speedup ... but be careful about
  390. * any processor on the other IRQ line, EDR registers are
  391. * shared.
  392. */
  393. status = twl4030_i2c_read(sih->module, bytes + 1,
  394. sih->edr_offset, sih->bytes_edr);
  395. if (status) {
  396. pr_err("twl4030: %s, %s --> %d\n", __func__,
  397. "read", status);
  398. return;
  399. }
  400. /* Modify only the bits we know must change */
  401. while (edge_change) {
  402. int i = fls(edge_change) - 1;
  403. struct irq_desc *d = irq_to_desc(i + agent->irq_base);
  404. int byte = 1 + (i >> 2);
  405. int off = (i & 0x3) * 2;
  406. if (!d) {
  407. pr_err("twl4030: Invalid IRQ: %d\n",
  408. i + agent->irq_base);
  409. return;
  410. }
  411. bytes[byte] &= ~(0x03 << off);
  412. spin_lock_irq(&d->lock);
  413. if (d->status & IRQ_TYPE_EDGE_RISING)
  414. bytes[byte] |= BIT(off + 1);
  415. if (d->status & IRQ_TYPE_EDGE_FALLING)
  416. bytes[byte] |= BIT(off + 0);
  417. spin_unlock_irq(&d->lock);
  418. edge_change &= ~BIT(i);
  419. }
  420. /* Write */
  421. status = twl4030_i2c_write(sih->module, bytes,
  422. sih->edr_offset, sih->bytes_edr);
  423. if (status)
  424. pr_err("twl4030: %s, %s --> %d\n", __func__,
  425. "write", status);
  426. }
  427. /*----------------------------------------------------------------------*/
  428. /*
  429. * All irq_chip methods get issued from code holding irq_desc[irq].lock,
  430. * which can't perform the underlying I2C operations (because they sleep).
  431. * So we must hand them off to a thread (workqueue) and cope with asynch
  432. * completion, potentially including some re-ordering, of these requests.
  433. */
  434. static void twl4030_sih_mask(unsigned irq)
  435. {
  436. struct sih_agent *sih = get_irq_chip_data(irq);
  437. unsigned long flags;
  438. spin_lock_irqsave(&sih_agent_lock, flags);
  439. sih->imr |= BIT(irq - sih->irq_base);
  440. sih->imr_change_pending = true;
  441. queue_work(wq, &sih->mask_work);
  442. spin_unlock_irqrestore(&sih_agent_lock, flags);
  443. }
  444. static void twl4030_sih_unmask(unsigned irq)
  445. {
  446. struct sih_agent *sih = get_irq_chip_data(irq);
  447. unsigned long flags;
  448. spin_lock_irqsave(&sih_agent_lock, flags);
  449. sih->imr &= ~BIT(irq - sih->irq_base);
  450. sih->imr_change_pending = true;
  451. queue_work(wq, &sih->mask_work);
  452. spin_unlock_irqrestore(&sih_agent_lock, flags);
  453. }
  454. static int twl4030_sih_set_type(unsigned irq, unsigned trigger)
  455. {
  456. struct sih_agent *sih = get_irq_chip_data(irq);
  457. struct irq_desc *desc = irq_to_desc(irq);
  458. unsigned long flags;
  459. if (!desc) {
  460. pr_err("twl4030: Invalid IRQ: %d\n", irq);
  461. return -EINVAL;
  462. }
  463. if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  464. return -EINVAL;
  465. spin_lock_irqsave(&sih_agent_lock, flags);
  466. if ((desc->status & IRQ_TYPE_SENSE_MASK) != trigger) {
  467. desc->status &= ~IRQ_TYPE_SENSE_MASK;
  468. desc->status |= trigger;
  469. sih->edge_change |= BIT(irq - sih->irq_base);
  470. queue_work(wq, &sih->edge_work);
  471. }
  472. spin_unlock_irqrestore(&sih_agent_lock, flags);
  473. return 0;
  474. }
  475. static struct irq_chip twl4030_sih_irq_chip = {
  476. .name = "twl4030",
  477. .mask = twl4030_sih_mask,
  478. .unmask = twl4030_sih_unmask,
  479. .set_type = twl4030_sih_set_type,
  480. };
  481. /*----------------------------------------------------------------------*/
  482. static inline int sih_read_isr(const struct sih *sih)
  483. {
  484. int status;
  485. union {
  486. u8 bytes[4];
  487. u32 word;
  488. } isr;
  489. /* FIXME need retry-on-error ... */
  490. isr.word = 0;
  491. status = twl4030_i2c_read(sih->module, isr.bytes,
  492. sih->mask[irq_line].isr_offset, sih->bytes_ixr);
  493. return (status < 0) ? status : le32_to_cpu(isr.word);
  494. }
  495. /*
  496. * Generic handler for SIH interrupts ... we "know" this is called
  497. * in task context, with IRQs enabled.
  498. */
  499. static void handle_twl4030_sih(unsigned irq, struct irq_desc *desc)
  500. {
  501. struct sih_agent *agent = get_irq_data(irq);
  502. const struct sih *sih = agent->sih;
  503. int isr;
  504. /* reading ISR acks the IRQs, using clear-on-read mode */
  505. local_irq_enable();
  506. isr = sih_read_isr(sih);
  507. local_irq_disable();
  508. if (isr < 0) {
  509. pr_err("twl4030: %s SIH, read ISR error %d\n",
  510. sih->name, isr);
  511. /* REVISIT: recover; eventually mask it all, etc */
  512. return;
  513. }
  514. while (isr) {
  515. irq = fls(isr);
  516. irq--;
  517. isr &= ~BIT(irq);
  518. if (irq < sih->bits)
  519. generic_handle_irq(agent->irq_base + irq);
  520. else
  521. pr_err("twl4030: %s SIH, invalid ISR bit %d\n",
  522. sih->name, irq);
  523. }
  524. }
  525. static unsigned twl4030_irq_next;
  526. /* returns the first IRQ used by this SIH bank,
  527. * or negative errno
  528. */
  529. int twl4030_sih_setup(int module)
  530. {
  531. int sih_mod;
  532. const struct sih *sih = NULL;
  533. struct sih_agent *agent;
  534. int i, irq;
  535. int status = -EINVAL;
  536. unsigned irq_base = twl4030_irq_next;
  537. /* only support modules with standard clear-on-read for now */
  538. for (sih_mod = 0, sih = sih_modules;
  539. sih_mod < ARRAY_SIZE(sih_modules);
  540. sih_mod++, sih++) {
  541. if (sih->module == module && sih->set_cor) {
  542. if (!WARN((irq_base + sih->bits) > NR_IRQS,
  543. "irq %d for %s too big\n",
  544. irq_base + sih->bits,
  545. sih->name))
  546. status = 0;
  547. break;
  548. }
  549. }
  550. if (status < 0)
  551. return status;
  552. agent = kzalloc(sizeof *agent, GFP_KERNEL);
  553. if (!agent)
  554. return -ENOMEM;
  555. status = 0;
  556. agent->irq_base = irq_base;
  557. agent->sih = sih;
  558. agent->imr = ~0;
  559. INIT_WORK(&agent->mask_work, twl4030_sih_do_mask);
  560. INIT_WORK(&agent->edge_work, twl4030_sih_do_edge);
  561. for (i = 0; i < sih->bits; i++) {
  562. irq = irq_base + i;
  563. set_irq_chip_and_handler(irq, &twl4030_sih_irq_chip,
  564. handle_edge_irq);
  565. set_irq_chip_data(irq, agent);
  566. activate_irq(irq);
  567. }
  568. status = irq_base;
  569. twl4030_irq_next += i;
  570. /* replace generic PIH handler (handle_simple_irq) */
  571. irq = sih_mod + twl4030_irq_base;
  572. set_irq_data(irq, agent);
  573. set_irq_chained_handler(irq, handle_twl4030_sih);
  574. pr_info("twl4030: %s (irq %d) chaining IRQs %d..%d\n", sih->name,
  575. irq, irq_base, twl4030_irq_next - 1);
  576. return status;
  577. }
  578. /* FIXME need a call to reverse twl4030_sih_setup() ... */
  579. /*----------------------------------------------------------------------*/
  580. /* FIXME pass in which interrupt line we'll use ... */
  581. #define twl_irq_line 0
  582. int twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end)
  583. {
  584. static struct irq_chip twl4030_irq_chip;
  585. int status;
  586. int i;
  587. struct task_struct *task;
  588. /*
  589. * Mask and clear all TWL4030 interrupts since initially we do
  590. * not have any TWL4030 module interrupt handlers present
  591. */
  592. status = twl4030_init_sih_modules(twl_irq_line);
  593. if (status < 0)
  594. return status;
  595. wq = create_singlethread_workqueue("twl4030-irqchip");
  596. if (!wq) {
  597. pr_err("twl4030: workqueue FAIL\n");
  598. return -ESRCH;
  599. }
  600. twl4030_irq_base = irq_base;
  601. /* install an irq handler for each of the SIH modules;
  602. * clone dummy irq_chip since PIH can't *do* anything
  603. */
  604. twl4030_irq_chip = dummy_irq_chip;
  605. twl4030_irq_chip.name = "twl4030";
  606. twl4030_sih_irq_chip.ack = dummy_irq_chip.ack;
  607. for (i = irq_base; i < irq_end; i++) {
  608. set_irq_chip_and_handler(i, &twl4030_irq_chip,
  609. handle_simple_irq);
  610. activate_irq(i);
  611. }
  612. twl4030_irq_next = i;
  613. pr_info("twl4030: %s (irq %d) chaining IRQs %d..%d\n", "PIH",
  614. irq_num, irq_base, twl4030_irq_next - 1);
  615. /* ... and the PWR_INT module ... */
  616. status = twl4030_sih_setup(TWL4030_MODULE_INT);
  617. if (status < 0) {
  618. pr_err("twl4030: sih_setup PWR INT --> %d\n", status);
  619. goto fail;
  620. }
  621. /* install an irq handler to demultiplex the TWL4030 interrupt */
  622. task = start_twl4030_irq_thread(irq_num);
  623. if (!task) {
  624. pr_err("twl4030: irq thread FAIL\n");
  625. status = -ESRCH;
  626. goto fail;
  627. }
  628. set_irq_data(irq_num, task);
  629. set_irq_chained_handler(irq_num, handle_twl4030_pih);
  630. return status;
  631. fail:
  632. for (i = irq_base; i < irq_end; i++)
  633. set_irq_chip_and_handler(i, NULL, NULL);
  634. destroy_workqueue(wq);
  635. wq = NULL;
  636. return status;
  637. }
  638. int twl_exit_irq(void)
  639. {
  640. /* FIXME undo twl_init_irq() */
  641. if (twl4030_irq_base) {
  642. pr_err("twl4030: can't yet clean up IRQs?\n");
  643. return -ENOSYS;
  644. }
  645. return 0;
  646. }