twl4030-irq.c 19 KB

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