twl4030-irq.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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. irq_desc_t *desc = irq_desc + irq;
  164. static unsigned i2c_errors;
  165. const static unsigned max_i2c_errors = 100;
  166. current->flags |= PF_NOFREEZE;
  167. while (!kthread_should_stop()) {
  168. int ret;
  169. int module_irq;
  170. u8 pih_isr;
  171. /* Wait for IRQ, then read PIH irq status (also blocking) */
  172. wait_for_completion_interruptible(&irq_event);
  173. ret = twl4030_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
  174. REG_PIH_ISR_P1);
  175. if (ret) {
  176. pr_warning("twl4030: I2C error %d reading PIH ISR\n",
  177. ret);
  178. if (++i2c_errors >= max_i2c_errors) {
  179. printk(KERN_ERR "Maximum I2C error count"
  180. " exceeded. Terminating %s.\n",
  181. __func__);
  182. break;
  183. }
  184. complete(&irq_event);
  185. continue;
  186. }
  187. /* these handlers deal with the relevant SIH irq status */
  188. local_irq_disable();
  189. for (module_irq = twl4030_irq_base;
  190. pih_isr;
  191. pih_isr >>= 1, module_irq++) {
  192. if (pih_isr & 0x1) {
  193. irq_desc_t *d = irq_desc + module_irq;
  194. /* These can't be masked ... always warn
  195. * if we get any surprises.
  196. */
  197. if (d->status & IRQ_DISABLED)
  198. note_interrupt(module_irq, d,
  199. IRQ_NONE);
  200. else
  201. d->handle_irq(module_irq, d);
  202. }
  203. }
  204. local_irq_enable();
  205. desc->chip->unmask(irq);
  206. }
  207. return 0;
  208. }
  209. /*
  210. * handle_twl4030_pih() is the desc->handle method for the twl4030 interrupt.
  211. * This is a chained interrupt, so there is no desc->action method for it.
  212. * Now we need to query the interrupt controller in the twl4030 to determine
  213. * which module is generating the interrupt request. However, we can't do i2c
  214. * transactions in interrupt context, so we must defer that work to a kernel
  215. * thread. All we do here is acknowledge and mask the interrupt and wakeup
  216. * the kernel thread.
  217. */
  218. static void handle_twl4030_pih(unsigned int irq, irq_desc_t *desc)
  219. {
  220. /* Acknowledge, clear *AND* mask the interrupt... */
  221. desc->chip->ack(irq);
  222. complete(&irq_event);
  223. }
  224. static struct task_struct *start_twl4030_irq_thread(long irq)
  225. {
  226. struct task_struct *thread;
  227. init_completion(&irq_event);
  228. thread = kthread_run(twl4030_irq_thread, (void *)irq, "twl4030-irq");
  229. if (!thread)
  230. pr_err("twl4030: could not create irq %ld thread!\n", irq);
  231. return thread;
  232. }
  233. /*----------------------------------------------------------------------*/
  234. /*
  235. * twl4030_init_sih_modules() ... start from a known state where no
  236. * IRQs will be coming in, and where we can quickly enable them then
  237. * handle them as they arrive. Mask all IRQs: maybe init SIH_CTRL.
  238. *
  239. * NOTE: we don't touch EDR registers here; they stay with hardware
  240. * defaults or whatever the last value was. Note that when both EDR
  241. * bits for an IRQ are clear, that's as if its IMR bit is set...
  242. */
  243. static int twl4030_init_sih_modules(unsigned line)
  244. {
  245. const struct sih *sih;
  246. u8 buf[4];
  247. int i;
  248. int status;
  249. /* line 0 == int1_n signal; line 1 == int2_n signal */
  250. if (line > 1)
  251. return -EINVAL;
  252. irq_line = line;
  253. /* disable all interrupts on our line */
  254. memset(buf, 0xff, sizeof buf);
  255. sih = sih_modules;
  256. for (i = 0; i < ARRAY_SIZE(sih_modules); i++, sih++) {
  257. /* skip USB -- it's funky */
  258. if (!sih->bytes_ixr)
  259. continue;
  260. status = twl4030_i2c_write(sih->module, buf,
  261. sih->mask[line].imr_offset, sih->bytes_ixr);
  262. if (status < 0)
  263. pr_err("twl4030: err %d initializing %s %s\n",
  264. status, sih->name, "IMR");
  265. /* Maybe disable "exclusive" mode; buffer second pending irq;
  266. * set Clear-On-Read (COR) bit.
  267. *
  268. * NOTE that sometimes COR polarity is documented as being
  269. * inverted: for MADC and BCI, COR=1 means "clear on write".
  270. * And for PWR_INT it's not documented...
  271. */
  272. if (sih->set_cor) {
  273. status = twl4030_i2c_write_u8(sih->module,
  274. TWL4030_SIH_CTRL_COR_MASK,
  275. sih->control_offset);
  276. if (status < 0)
  277. pr_err("twl4030: err %d initializing %s %s\n",
  278. status, sih->name, "SIH_CTRL");
  279. }
  280. }
  281. sih = sih_modules;
  282. for (i = 0; i < ARRAY_SIZE(sih_modules); i++, sih++) {
  283. u8 rxbuf[4];
  284. int j;
  285. /* skip USB */
  286. if (!sih->bytes_ixr)
  287. continue;
  288. /* Clear pending interrupt status. Either the read was
  289. * enough, or we need to write those bits. Repeat, in
  290. * case an IRQ is pending (PENDDIS=0) ... that's not
  291. * uncommon with PWR_INT.PWRON.
  292. */
  293. for (j = 0; j < 2; j++) {
  294. status = twl4030_i2c_read(sih->module, rxbuf,
  295. sih->mask[line].isr_offset, sih->bytes_ixr);
  296. if (status < 0)
  297. pr_err("twl4030: err %d initializing %s %s\n",
  298. status, sih->name, "ISR");
  299. if (!sih->set_cor)
  300. status = twl4030_i2c_write(sih->module, buf,
  301. sih->mask[line].isr_offset,
  302. sih->bytes_ixr);
  303. /* else COR=1 means read sufficed.
  304. * (for most SIH modules...)
  305. */
  306. }
  307. }
  308. return 0;
  309. }
  310. static inline void activate_irq(int irq)
  311. {
  312. #ifdef CONFIG_ARM
  313. /* ARM requires an extra step to clear IRQ_NOREQUEST, which it
  314. * sets on behalf of every irq_chip. Also sets IRQ_NOPROBE.
  315. */
  316. set_irq_flags(irq, IRQF_VALID);
  317. #else
  318. /* same effect on other architectures */
  319. set_irq_noprobe(irq);
  320. #endif
  321. }
  322. /*----------------------------------------------------------------------*/
  323. static DEFINE_SPINLOCK(sih_agent_lock);
  324. static struct workqueue_struct *wq;
  325. struct sih_agent {
  326. int irq_base;
  327. const struct sih *sih;
  328. u32 imr;
  329. bool imr_change_pending;
  330. struct work_struct mask_work;
  331. u32 edge_change;
  332. struct work_struct edge_work;
  333. };
  334. static void twl4030_sih_do_mask(struct work_struct *work)
  335. {
  336. struct sih_agent *agent;
  337. const struct sih *sih;
  338. union {
  339. u8 bytes[4];
  340. u32 word;
  341. } imr;
  342. int status;
  343. agent = container_of(work, struct sih_agent, mask_work);
  344. /* see what work we have */
  345. spin_lock_irq(&sih_agent_lock);
  346. if (agent->imr_change_pending) {
  347. sih = agent->sih;
  348. /* byte[0] gets overwritten as we write ... */
  349. imr.word = cpu_to_le32(agent->imr << 8);
  350. agent->imr_change_pending = false;
  351. } else
  352. sih = NULL;
  353. spin_unlock_irq(&sih_agent_lock);
  354. if (!sih)
  355. return;
  356. /* write the whole mask ... simpler than subsetting it */
  357. status = twl4030_i2c_write(sih->module, imr.bytes,
  358. sih->mask[irq_line].imr_offset, sih->bytes_ixr);
  359. if (status)
  360. pr_err("twl4030: %s, %s --> %d\n", __func__,
  361. "write", status);
  362. }
  363. static void twl4030_sih_do_edge(struct work_struct *work)
  364. {
  365. struct sih_agent *agent;
  366. const struct sih *sih;
  367. u8 bytes[6];
  368. u32 edge_change;
  369. int status;
  370. agent = container_of(work, struct sih_agent, edge_work);
  371. /* see what work we have */
  372. spin_lock_irq(&sih_agent_lock);
  373. edge_change = agent->edge_change;
  374. agent->edge_change = 0;;
  375. sih = edge_change ? agent->sih : NULL;
  376. spin_unlock_irq(&sih_agent_lock);
  377. if (!sih)
  378. return;
  379. /* Read, reserving first byte for write scratch. Yes, this
  380. * could be cached for some speedup ... but be careful about
  381. * any processor on the other IRQ line, EDR registers are
  382. * shared.
  383. */
  384. status = twl4030_i2c_read(sih->module, bytes + 1,
  385. sih->edr_offset, sih->bytes_edr);
  386. if (status) {
  387. pr_err("twl4030: %s, %s --> %d\n", __func__,
  388. "read", status);
  389. return;
  390. }
  391. /* Modify only the bits we know must change */
  392. while (edge_change) {
  393. int i = fls(edge_change) - 1;
  394. struct irq_desc *d = irq_desc + i + agent->irq_base;
  395. int byte = 1 + (i >> 2);
  396. int off = (i & 0x3) * 2;
  397. bytes[byte] &= ~(0x03 << off);
  398. spin_lock_irq(&d->lock);
  399. if (d->status & IRQ_TYPE_EDGE_RISING)
  400. bytes[byte] |= BIT(off + 1);
  401. if (d->status & IRQ_TYPE_EDGE_FALLING)
  402. bytes[byte] |= BIT(off + 0);
  403. spin_unlock_irq(&d->lock);
  404. edge_change &= ~BIT(i);
  405. }
  406. /* Write */
  407. status = twl4030_i2c_write(sih->module, bytes,
  408. sih->edr_offset, sih->bytes_edr);
  409. if (status)
  410. pr_err("twl4030: %s, %s --> %d\n", __func__,
  411. "write", status);
  412. }
  413. /*----------------------------------------------------------------------*/
  414. /*
  415. * All irq_chip methods get issued from code holding irq_desc[irq].lock,
  416. * which can't perform the underlying I2C operations (because they sleep).
  417. * So we must hand them off to a thread (workqueue) and cope with asynch
  418. * completion, potentially including some re-ordering, of these requests.
  419. */
  420. static void twl4030_sih_mask(unsigned irq)
  421. {
  422. struct sih_agent *sih = get_irq_chip_data(irq);
  423. unsigned long flags;
  424. spin_lock_irqsave(&sih_agent_lock, flags);
  425. sih->imr |= BIT(irq - sih->irq_base);
  426. sih->imr_change_pending = true;
  427. queue_work(wq, &sih->mask_work);
  428. spin_unlock_irqrestore(&sih_agent_lock, flags);
  429. }
  430. static void twl4030_sih_unmask(unsigned irq)
  431. {
  432. struct sih_agent *sih = get_irq_chip_data(irq);
  433. unsigned long flags;
  434. spin_lock_irqsave(&sih_agent_lock, flags);
  435. sih->imr &= ~BIT(irq - sih->irq_base);
  436. sih->imr_change_pending = true;
  437. queue_work(wq, &sih->mask_work);
  438. spin_unlock_irqrestore(&sih_agent_lock, flags);
  439. }
  440. static int twl4030_sih_set_type(unsigned irq, unsigned trigger)
  441. {
  442. struct sih_agent *sih = get_irq_chip_data(irq);
  443. struct irq_desc *desc = irq_desc + irq;
  444. unsigned long flags;
  445. if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  446. return -EINVAL;
  447. spin_lock_irqsave(&sih_agent_lock, flags);
  448. if ((desc->status & IRQ_TYPE_SENSE_MASK) != trigger) {
  449. desc->status &= ~IRQ_TYPE_SENSE_MASK;
  450. desc->status |= trigger;
  451. sih->edge_change |= BIT(irq - sih->irq_base);
  452. queue_work(wq, &sih->edge_work);
  453. }
  454. spin_unlock_irqrestore(&sih_agent_lock, flags);
  455. return 0;
  456. }
  457. static struct irq_chip twl4030_sih_irq_chip = {
  458. .name = "twl4030",
  459. .mask = twl4030_sih_mask,
  460. .unmask = twl4030_sih_unmask,
  461. .set_type = twl4030_sih_set_type,
  462. };
  463. /*----------------------------------------------------------------------*/
  464. static inline int sih_read_isr(const struct sih *sih)
  465. {
  466. int status;
  467. union {
  468. u8 bytes[4];
  469. u32 word;
  470. } isr;
  471. /* FIXME need retry-on-error ... */
  472. isr.word = 0;
  473. status = twl4030_i2c_read(sih->module, isr.bytes,
  474. sih->mask[irq_line].isr_offset, sih->bytes_ixr);
  475. return (status < 0) ? status : le32_to_cpu(isr.word);
  476. }
  477. /*
  478. * Generic handler for SIH interrupts ... we "know" this is called
  479. * in task context, with IRQs enabled.
  480. */
  481. static void handle_twl4030_sih(unsigned irq, struct irq_desc *desc)
  482. {
  483. struct sih_agent *agent = get_irq_data(irq);
  484. const struct sih *sih = agent->sih;
  485. int isr;
  486. /* reading ISR acks the IRQs, using clear-on-read mode */
  487. local_irq_enable();
  488. isr = sih_read_isr(sih);
  489. local_irq_disable();
  490. if (isr < 0) {
  491. pr_err("twl4030: %s SIH, read ISR error %d\n",
  492. sih->name, isr);
  493. /* REVISIT: recover; eventually mask it all, etc */
  494. return;
  495. }
  496. while (isr) {
  497. irq = fls(isr);
  498. irq--;
  499. isr &= ~BIT(irq);
  500. if (irq < sih->bits)
  501. generic_handle_irq(agent->irq_base + irq);
  502. else
  503. pr_err("twl4030: %s SIH, invalid ISR bit %d\n",
  504. sih->name, irq);
  505. }
  506. }
  507. static unsigned twl4030_irq_next;
  508. /* returns the first IRQ used by this SIH bank,
  509. * or negative errno
  510. */
  511. int twl4030_sih_setup(int module)
  512. {
  513. int sih_mod;
  514. const struct sih *sih = NULL;
  515. struct sih_agent *agent;
  516. int i, irq;
  517. int status = -EINVAL;
  518. unsigned irq_base = twl4030_irq_next;
  519. /* only support modules with standard clear-on-read for now */
  520. for (sih_mod = 0, sih = sih_modules;
  521. sih_mod < ARRAY_SIZE(sih_modules);
  522. sih_mod++, sih++) {
  523. if (sih->module == module && sih->set_cor) {
  524. if (!WARN((irq_base + sih->bits) > NR_IRQS,
  525. "irq %d for %s too big\n",
  526. irq_base + sih->bits,
  527. sih->name))
  528. status = 0;
  529. break;
  530. }
  531. }
  532. if (status < 0)
  533. return status;
  534. agent = kzalloc(sizeof *agent, GFP_KERNEL);
  535. if (!agent)
  536. return -ENOMEM;
  537. status = 0;
  538. agent->irq_base = irq_base;
  539. agent->sih = sih;
  540. agent->imr = ~0;
  541. INIT_WORK(&agent->mask_work, twl4030_sih_do_mask);
  542. INIT_WORK(&agent->edge_work, twl4030_sih_do_edge);
  543. for (i = 0; i < sih->bits; i++) {
  544. irq = irq_base + i;
  545. set_irq_chip_and_handler(irq, &twl4030_sih_irq_chip,
  546. handle_edge_irq);
  547. set_irq_chip_data(irq, agent);
  548. activate_irq(irq);
  549. }
  550. status = irq_base;
  551. twl4030_irq_next += i;
  552. /* replace generic PIH handler (handle_simple_irq) */
  553. irq = sih_mod + twl4030_irq_base;
  554. set_irq_data(irq, agent);
  555. set_irq_chained_handler(irq, handle_twl4030_sih);
  556. pr_info("twl4030: %s (irq %d) chaining IRQs %d..%d\n", sih->name,
  557. irq, irq_base, twl4030_irq_next - 1);
  558. return status;
  559. }
  560. /* FIXME need a call to reverse twl4030_sih_setup() ... */
  561. /*----------------------------------------------------------------------*/
  562. /* FIXME pass in which interrupt line we'll use ... */
  563. #define twl_irq_line 0
  564. int twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end)
  565. {
  566. static struct irq_chip twl4030_irq_chip;
  567. int status;
  568. int i;
  569. struct task_struct *task;
  570. /*
  571. * Mask and clear all TWL4030 interrupts since initially we do
  572. * not have any TWL4030 module interrupt handlers present
  573. */
  574. status = twl4030_init_sih_modules(twl_irq_line);
  575. if (status < 0)
  576. return status;
  577. wq = create_singlethread_workqueue("twl4030-irqchip");
  578. if (!wq) {
  579. pr_err("twl4030: workqueue FAIL\n");
  580. return -ESRCH;
  581. }
  582. twl4030_irq_base = irq_base;
  583. /* install an irq handler for each of the SIH modules;
  584. * clone dummy irq_chip since PIH can't *do* anything
  585. */
  586. twl4030_irq_chip = dummy_irq_chip;
  587. twl4030_irq_chip.name = "twl4030";
  588. twl4030_sih_irq_chip.ack = dummy_irq_chip.ack;
  589. for (i = irq_base; i < irq_end; i++) {
  590. set_irq_chip_and_handler(i, &twl4030_irq_chip,
  591. handle_simple_irq);
  592. activate_irq(i);
  593. }
  594. twl4030_irq_next = i;
  595. pr_info("twl4030: %s (irq %d) chaining IRQs %d..%d\n", "PIH",
  596. irq_num, irq_base, twl4030_irq_next - 1);
  597. /* ... and the PWR_INT module ... */
  598. status = twl4030_sih_setup(TWL4030_MODULE_INT);
  599. if (status < 0) {
  600. pr_err("twl4030: sih_setup PWR INT --> %d\n", status);
  601. goto fail;
  602. }
  603. /* install an irq handler to demultiplex the TWL4030 interrupt */
  604. task = start_twl4030_irq_thread(irq_num);
  605. if (!task) {
  606. pr_err("twl4030: irq thread FAIL\n");
  607. status = -ESRCH;
  608. goto fail;
  609. }
  610. set_irq_data(irq_num, task);
  611. set_irq_chained_handler(irq_num, handle_twl4030_pih);
  612. return status;
  613. fail:
  614. for (i = irq_base; i < irq_end; i++)
  615. set_irq_chip_and_handler(i, NULL, NULL);
  616. destroy_workqueue(wq);
  617. wq = NULL;
  618. return status;
  619. }
  620. int twl_exit_irq(void)
  621. {
  622. /* FIXME undo twl_init_irq() */
  623. if (twl4030_irq_base) {
  624. pr_err("twl4030: can't yet clean up IRQs?\n");
  625. return -ENOSYS;
  626. }
  627. return 0;
  628. }