twl4030-irq.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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/twl.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. u8 irq_lines; /* number of supported irq lines */
  67. /* SIR ignored -- set interrupt, for testing only */
  68. struct irq_data {
  69. u8 isr_offset;
  70. u8 imr_offset;
  71. } mask[2];
  72. /* + 2 bytes padding */
  73. };
  74. static const struct sih *sih_modules;
  75. static int nr_sih_modules;
  76. #define SIH_INITIALIZER(modname, nbits) \
  77. .module = TWL4030_MODULE_ ## modname, \
  78. .control_offset = TWL4030_ ## modname ## _SIH_CTRL, \
  79. .bits = nbits, \
  80. .bytes_ixr = DIV_ROUND_UP(nbits, 8), \
  81. .edr_offset = TWL4030_ ## modname ## _EDR, \
  82. .bytes_edr = DIV_ROUND_UP((2*(nbits)), 8), \
  83. .irq_lines = 2, \
  84. .mask = { { \
  85. .isr_offset = TWL4030_ ## modname ## _ISR1, \
  86. .imr_offset = TWL4030_ ## modname ## _IMR1, \
  87. }, \
  88. { \
  89. .isr_offset = TWL4030_ ## modname ## _ISR2, \
  90. .imr_offset = TWL4030_ ## modname ## _IMR2, \
  91. }, },
  92. /* register naming policies are inconsistent ... */
  93. #define TWL4030_INT_PWR_EDR TWL4030_INT_PWR_EDR1
  94. #define TWL4030_MODULE_KEYPAD_KEYP TWL4030_MODULE_KEYPAD
  95. #define TWL4030_MODULE_INT_PWR TWL4030_MODULE_INT
  96. /* Order in this table matches order in PIH_ISR. That is,
  97. * BIT(n) in PIH_ISR is sih_modules[n].
  98. */
  99. /* sih_modules_twl4030 is used both in twl4030 and twl5030 */
  100. static const struct sih sih_modules_twl4030[6] = {
  101. [0] = {
  102. .name = "gpio",
  103. .module = TWL4030_MODULE_GPIO,
  104. .control_offset = REG_GPIO_SIH_CTRL,
  105. .set_cor = true,
  106. .bits = TWL4030_GPIO_MAX,
  107. .bytes_ixr = 3,
  108. /* Note: *all* of these IRQs default to no-trigger */
  109. .edr_offset = REG_GPIO_EDR1,
  110. .bytes_edr = 5,
  111. .irq_lines = 2,
  112. .mask = { {
  113. .isr_offset = REG_GPIO_ISR1A,
  114. .imr_offset = REG_GPIO_IMR1A,
  115. }, {
  116. .isr_offset = REG_GPIO_ISR1B,
  117. .imr_offset = REG_GPIO_IMR1B,
  118. }, },
  119. },
  120. [1] = {
  121. .name = "keypad",
  122. .set_cor = true,
  123. SIH_INITIALIZER(KEYPAD_KEYP, 4)
  124. },
  125. [2] = {
  126. .name = "bci",
  127. .module = TWL4030_MODULE_INTERRUPTS,
  128. .control_offset = TWL4030_INTERRUPTS_BCISIHCTRL,
  129. .bits = 12,
  130. .bytes_ixr = 2,
  131. .edr_offset = TWL4030_INTERRUPTS_BCIEDR1,
  132. /* Note: most of these IRQs default to no-trigger */
  133. .bytes_edr = 3,
  134. .irq_lines = 2,
  135. .mask = { {
  136. .isr_offset = TWL4030_INTERRUPTS_BCIISR1A,
  137. .imr_offset = TWL4030_INTERRUPTS_BCIIMR1A,
  138. }, {
  139. .isr_offset = TWL4030_INTERRUPTS_BCIISR1B,
  140. .imr_offset = TWL4030_INTERRUPTS_BCIIMR1B,
  141. }, },
  142. },
  143. [3] = {
  144. .name = "madc",
  145. SIH_INITIALIZER(MADC, 4)
  146. },
  147. [4] = {
  148. /* USB doesn't use the same SIH organization */
  149. .name = "usb",
  150. },
  151. [5] = {
  152. .name = "power",
  153. .set_cor = true,
  154. SIH_INITIALIZER(INT_PWR, 8)
  155. },
  156. /* there are no SIH modules #6 or #7 ... */
  157. };
  158. static const struct sih sih_modules_twl5031[8] = {
  159. [0] = {
  160. .name = "gpio",
  161. .module = TWL4030_MODULE_GPIO,
  162. .control_offset = REG_GPIO_SIH_CTRL,
  163. .set_cor = true,
  164. .bits = TWL4030_GPIO_MAX,
  165. .bytes_ixr = 3,
  166. /* Note: *all* of these IRQs default to no-trigger */
  167. .edr_offset = REG_GPIO_EDR1,
  168. .bytes_edr = 5,
  169. .irq_lines = 2,
  170. .mask = { {
  171. .isr_offset = REG_GPIO_ISR1A,
  172. .imr_offset = REG_GPIO_IMR1A,
  173. }, {
  174. .isr_offset = REG_GPIO_ISR1B,
  175. .imr_offset = REG_GPIO_IMR1B,
  176. }, },
  177. },
  178. [1] = {
  179. .name = "keypad",
  180. .set_cor = true,
  181. SIH_INITIALIZER(KEYPAD_KEYP, 4)
  182. },
  183. [2] = {
  184. .name = "bci",
  185. .module = TWL5031_MODULE_INTERRUPTS,
  186. .control_offset = TWL5031_INTERRUPTS_BCISIHCTRL,
  187. .bits = 7,
  188. .bytes_ixr = 1,
  189. .edr_offset = TWL5031_INTERRUPTS_BCIEDR1,
  190. /* Note: most of these IRQs default to no-trigger */
  191. .bytes_edr = 2,
  192. .irq_lines = 2,
  193. .mask = { {
  194. .isr_offset = TWL5031_INTERRUPTS_BCIISR1,
  195. .imr_offset = TWL5031_INTERRUPTS_BCIIMR1,
  196. }, {
  197. .isr_offset = TWL5031_INTERRUPTS_BCIISR2,
  198. .imr_offset = TWL5031_INTERRUPTS_BCIIMR2,
  199. }, },
  200. },
  201. [3] = {
  202. .name = "madc",
  203. SIH_INITIALIZER(MADC, 4)
  204. },
  205. [4] = {
  206. /* USB doesn't use the same SIH organization */
  207. .name = "usb",
  208. },
  209. [5] = {
  210. .name = "power",
  211. .set_cor = true,
  212. SIH_INITIALIZER(INT_PWR, 8)
  213. },
  214. [6] = {
  215. /*
  216. * ACI doesn't use the same SIH organization.
  217. * For example, it supports only one interrupt line
  218. */
  219. .name = "aci",
  220. .module = TWL5031_MODULE_ACCESSORY,
  221. .bits = 9,
  222. .bytes_ixr = 2,
  223. .irq_lines = 1,
  224. .mask = { {
  225. .isr_offset = TWL5031_ACIIDR_LSB,
  226. .imr_offset = TWL5031_ACIIMR_LSB,
  227. }, },
  228. },
  229. [7] = {
  230. /* Accessory */
  231. .name = "acc",
  232. .module = TWL5031_MODULE_ACCESSORY,
  233. .control_offset = TWL5031_ACCSIHCTRL,
  234. .bits = 2,
  235. .bytes_ixr = 1,
  236. .edr_offset = TWL5031_ACCEDR1,
  237. /* Note: most of these IRQs default to no-trigger */
  238. .bytes_edr = 1,
  239. .irq_lines = 2,
  240. .mask = { {
  241. .isr_offset = TWL5031_ACCISR1,
  242. .imr_offset = TWL5031_ACCIMR1,
  243. }, {
  244. .isr_offset = TWL5031_ACCISR2,
  245. .imr_offset = TWL5031_ACCIMR2,
  246. }, },
  247. },
  248. };
  249. #undef TWL4030_MODULE_KEYPAD_KEYP
  250. #undef TWL4030_MODULE_INT_PWR
  251. #undef TWL4030_INT_PWR_EDR
  252. /*----------------------------------------------------------------------*/
  253. static unsigned twl4030_irq_base;
  254. static struct completion irq_event;
  255. /*
  256. * This thread processes interrupts reported by the Primary Interrupt Handler.
  257. */
  258. static int twl4030_irq_thread(void *data)
  259. {
  260. long irq = (long)data;
  261. static unsigned i2c_errors;
  262. static const unsigned max_i2c_errors = 100;
  263. current->flags |= PF_NOFREEZE;
  264. while (!kthread_should_stop()) {
  265. int ret;
  266. int module_irq;
  267. u8 pih_isr;
  268. /* Wait for IRQ, then read PIH irq status (also blocking) */
  269. wait_for_completion_interruptible(&irq_event);
  270. ret = twl_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
  271. REG_PIH_ISR_P1);
  272. if (ret) {
  273. pr_warning("twl4030: I2C error %d reading PIH ISR\n",
  274. ret);
  275. if (++i2c_errors >= max_i2c_errors) {
  276. printk(KERN_ERR "Maximum I2C error count"
  277. " exceeded. Terminating %s.\n",
  278. __func__);
  279. break;
  280. }
  281. complete(&irq_event);
  282. continue;
  283. }
  284. /* these handlers deal with the relevant SIH irq status */
  285. local_irq_disable();
  286. for (module_irq = twl4030_irq_base;
  287. pih_isr;
  288. pih_isr >>= 1, module_irq++) {
  289. if (pih_isr & 0x1) {
  290. struct irq_desc *d = irq_to_desc(module_irq);
  291. if (!d) {
  292. pr_err("twl4030: Invalid SIH IRQ: %d\n",
  293. module_irq);
  294. return -EINVAL;
  295. }
  296. /* These can't be masked ... always warn
  297. * if we get any surprises.
  298. */
  299. if (d->status & IRQ_DISABLED)
  300. note_interrupt(module_irq, d,
  301. IRQ_NONE);
  302. else
  303. d->handle_irq(module_irq, d);
  304. }
  305. }
  306. local_irq_enable();
  307. enable_irq(irq);
  308. }
  309. return 0;
  310. }
  311. /*
  312. * handle_twl4030_pih() is the desc->handle method for the twl4030 interrupt.
  313. * This is a chained interrupt, so there is no desc->action method for it.
  314. * Now we need to query the interrupt controller in the twl4030 to determine
  315. * which module is generating the interrupt request. However, we can't do i2c
  316. * transactions in interrupt context, so we must defer that work to a kernel
  317. * thread. All we do here is acknowledge and mask the interrupt and wakeup
  318. * the kernel thread.
  319. */
  320. static irqreturn_t handle_twl4030_pih(int irq, void *devid)
  321. {
  322. /* Acknowledge, clear *AND* mask the interrupt... */
  323. disable_irq_nosync(irq);
  324. complete(devid);
  325. return IRQ_HANDLED;
  326. }
  327. /*----------------------------------------------------------------------*/
  328. /*
  329. * twl4030_init_sih_modules() ... start from a known state where no
  330. * IRQs will be coming in, and where we can quickly enable them then
  331. * handle them as they arrive. Mask all IRQs: maybe init SIH_CTRL.
  332. *
  333. * NOTE: we don't touch EDR registers here; they stay with hardware
  334. * defaults or whatever the last value was. Note that when both EDR
  335. * bits for an IRQ are clear, that's as if its IMR bit is set...
  336. */
  337. static int twl4030_init_sih_modules(unsigned line)
  338. {
  339. const struct sih *sih;
  340. u8 buf[4];
  341. int i;
  342. int status;
  343. /* line 0 == int1_n signal; line 1 == int2_n signal */
  344. if (line > 1)
  345. return -EINVAL;
  346. irq_line = line;
  347. /* disable all interrupts on our line */
  348. memset(buf, 0xff, sizeof buf);
  349. sih = sih_modules;
  350. for (i = 0; i < nr_sih_modules; i++, sih++) {
  351. /* skip USB -- it's funky */
  352. if (!sih->bytes_ixr)
  353. continue;
  354. /* Not all the SIH modules support multiple interrupt lines */
  355. if (sih->irq_lines <= line)
  356. continue;
  357. status = twl_i2c_write(sih->module, buf,
  358. sih->mask[line].imr_offset, sih->bytes_ixr);
  359. if (status < 0)
  360. pr_err("twl4030: err %d initializing %s %s\n",
  361. status, sih->name, "IMR");
  362. /* Maybe disable "exclusive" mode; buffer second pending irq;
  363. * set Clear-On-Read (COR) bit.
  364. *
  365. * NOTE that sometimes COR polarity is documented as being
  366. * inverted: for MADC and BCI, COR=1 means "clear on write".
  367. * And for PWR_INT it's not documented...
  368. */
  369. if (sih->set_cor) {
  370. status = twl_i2c_write_u8(sih->module,
  371. TWL4030_SIH_CTRL_COR_MASK,
  372. sih->control_offset);
  373. if (status < 0)
  374. pr_err("twl4030: err %d initializing %s %s\n",
  375. status, sih->name, "SIH_CTRL");
  376. }
  377. }
  378. sih = sih_modules;
  379. for (i = 0; i < nr_sih_modules; i++, sih++) {
  380. u8 rxbuf[4];
  381. int j;
  382. /* skip USB */
  383. if (!sih->bytes_ixr)
  384. continue;
  385. /* Not all the SIH modules support multiple interrupt lines */
  386. if (sih->irq_lines <= line)
  387. continue;
  388. /* Clear pending interrupt status. Either the read was
  389. * enough, or we need to write those bits. Repeat, in
  390. * case an IRQ is pending (PENDDIS=0) ... that's not
  391. * uncommon with PWR_INT.PWRON.
  392. */
  393. for (j = 0; j < 2; j++) {
  394. status = twl_i2c_read(sih->module, rxbuf,
  395. sih->mask[line].isr_offset, sih->bytes_ixr);
  396. if (status < 0)
  397. pr_err("twl4030: err %d initializing %s %s\n",
  398. status, sih->name, "ISR");
  399. if (!sih->set_cor)
  400. status = twl_i2c_write(sih->module, buf,
  401. sih->mask[line].isr_offset,
  402. sih->bytes_ixr);
  403. /* else COR=1 means read sufficed.
  404. * (for most SIH modules...)
  405. */
  406. }
  407. }
  408. return 0;
  409. }
  410. static inline void activate_irq(int irq)
  411. {
  412. #ifdef CONFIG_ARM
  413. /* ARM requires an extra step to clear IRQ_NOREQUEST, which it
  414. * sets on behalf of every irq_chip. Also sets IRQ_NOPROBE.
  415. */
  416. set_irq_flags(irq, IRQF_VALID);
  417. #else
  418. /* same effect on other architectures */
  419. set_irq_noprobe(irq);
  420. #endif
  421. }
  422. /*----------------------------------------------------------------------*/
  423. static DEFINE_SPINLOCK(sih_agent_lock);
  424. static struct workqueue_struct *wq;
  425. struct sih_agent {
  426. int irq_base;
  427. const struct sih *sih;
  428. u32 imr;
  429. bool imr_change_pending;
  430. struct work_struct mask_work;
  431. u32 edge_change;
  432. struct work_struct edge_work;
  433. };
  434. static void twl4030_sih_do_mask(struct work_struct *work)
  435. {
  436. struct sih_agent *agent;
  437. const struct sih *sih;
  438. union {
  439. u8 bytes[4];
  440. u32 word;
  441. } imr;
  442. int status;
  443. agent = container_of(work, struct sih_agent, mask_work);
  444. /* see what work we have */
  445. spin_lock_irq(&sih_agent_lock);
  446. if (agent->imr_change_pending) {
  447. sih = agent->sih;
  448. /* byte[0] gets overwritten as we write ... */
  449. imr.word = cpu_to_le32(agent->imr << 8);
  450. agent->imr_change_pending = false;
  451. } else
  452. sih = NULL;
  453. spin_unlock_irq(&sih_agent_lock);
  454. if (!sih)
  455. return;
  456. /* write the whole mask ... simpler than subsetting it */
  457. status = twl_i2c_write(sih->module, imr.bytes,
  458. sih->mask[irq_line].imr_offset, sih->bytes_ixr);
  459. if (status)
  460. pr_err("twl4030: %s, %s --> %d\n", __func__,
  461. "write", status);
  462. }
  463. static void twl4030_sih_do_edge(struct work_struct *work)
  464. {
  465. struct sih_agent *agent;
  466. const struct sih *sih;
  467. u8 bytes[6];
  468. u32 edge_change;
  469. int status;
  470. agent = container_of(work, struct sih_agent, edge_work);
  471. /* see what work we have */
  472. spin_lock_irq(&sih_agent_lock);
  473. edge_change = agent->edge_change;
  474. agent->edge_change = 0;
  475. sih = edge_change ? agent->sih : NULL;
  476. spin_unlock_irq(&sih_agent_lock);
  477. if (!sih)
  478. return;
  479. /* Read, reserving first byte for write scratch. Yes, this
  480. * could be cached for some speedup ... but be careful about
  481. * any processor on the other IRQ line, EDR registers are
  482. * shared.
  483. */
  484. status = twl_i2c_read(sih->module, bytes + 1,
  485. sih->edr_offset, sih->bytes_edr);
  486. if (status) {
  487. pr_err("twl4030: %s, %s --> %d\n", __func__,
  488. "read", status);
  489. return;
  490. }
  491. /* Modify only the bits we know must change */
  492. while (edge_change) {
  493. int i = fls(edge_change) - 1;
  494. struct irq_desc *d = irq_to_desc(i + agent->irq_base);
  495. int byte = 1 + (i >> 2);
  496. int off = (i & 0x3) * 2;
  497. if (!d) {
  498. pr_err("twl4030: Invalid IRQ: %d\n",
  499. i + agent->irq_base);
  500. return;
  501. }
  502. bytes[byte] &= ~(0x03 << off);
  503. raw_spin_lock_irq(&d->lock);
  504. if (d->status & IRQ_TYPE_EDGE_RISING)
  505. bytes[byte] |= BIT(off + 1);
  506. if (d->status & IRQ_TYPE_EDGE_FALLING)
  507. bytes[byte] |= BIT(off + 0);
  508. raw_spin_unlock_irq(&d->lock);
  509. edge_change &= ~BIT(i);
  510. }
  511. /* Write */
  512. status = twl_i2c_write(sih->module, bytes,
  513. sih->edr_offset, sih->bytes_edr);
  514. if (status)
  515. pr_err("twl4030: %s, %s --> %d\n", __func__,
  516. "write", status);
  517. }
  518. /*----------------------------------------------------------------------*/
  519. /*
  520. * All irq_chip methods get issued from code holding irq_desc[irq].lock,
  521. * which can't perform the underlying I2C operations (because they sleep).
  522. * So we must hand them off to a thread (workqueue) and cope with asynch
  523. * completion, potentially including some re-ordering, of these requests.
  524. */
  525. static void twl4030_sih_mask(unsigned irq)
  526. {
  527. struct sih_agent *sih = get_irq_chip_data(irq);
  528. unsigned long flags;
  529. spin_lock_irqsave(&sih_agent_lock, flags);
  530. sih->imr |= BIT(irq - sih->irq_base);
  531. sih->imr_change_pending = true;
  532. queue_work(wq, &sih->mask_work);
  533. spin_unlock_irqrestore(&sih_agent_lock, flags);
  534. }
  535. static void twl4030_sih_unmask(unsigned irq)
  536. {
  537. struct sih_agent *sih = get_irq_chip_data(irq);
  538. unsigned long flags;
  539. spin_lock_irqsave(&sih_agent_lock, flags);
  540. sih->imr &= ~BIT(irq - sih->irq_base);
  541. sih->imr_change_pending = true;
  542. queue_work(wq, &sih->mask_work);
  543. spin_unlock_irqrestore(&sih_agent_lock, flags);
  544. }
  545. static int twl4030_sih_set_type(unsigned irq, unsigned trigger)
  546. {
  547. struct sih_agent *sih = get_irq_chip_data(irq);
  548. struct irq_desc *desc = irq_to_desc(irq);
  549. unsigned long flags;
  550. if (!desc) {
  551. pr_err("twl4030: Invalid IRQ: %d\n", irq);
  552. return -EINVAL;
  553. }
  554. if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  555. return -EINVAL;
  556. spin_lock_irqsave(&sih_agent_lock, flags);
  557. if ((desc->status & IRQ_TYPE_SENSE_MASK) != trigger) {
  558. desc->status &= ~IRQ_TYPE_SENSE_MASK;
  559. desc->status |= trigger;
  560. sih->edge_change |= BIT(irq - sih->irq_base);
  561. queue_work(wq, &sih->edge_work);
  562. }
  563. spin_unlock_irqrestore(&sih_agent_lock, flags);
  564. return 0;
  565. }
  566. static struct irq_chip twl4030_sih_irq_chip = {
  567. .name = "twl4030",
  568. .mask = twl4030_sih_mask,
  569. .unmask = twl4030_sih_unmask,
  570. .set_type = twl4030_sih_set_type,
  571. };
  572. /*----------------------------------------------------------------------*/
  573. static inline int sih_read_isr(const struct sih *sih)
  574. {
  575. int status;
  576. union {
  577. u8 bytes[4];
  578. u32 word;
  579. } isr;
  580. /* FIXME need retry-on-error ... */
  581. isr.word = 0;
  582. status = twl_i2c_read(sih->module, isr.bytes,
  583. sih->mask[irq_line].isr_offset, sih->bytes_ixr);
  584. return (status < 0) ? status : le32_to_cpu(isr.word);
  585. }
  586. /*
  587. * Generic handler for SIH interrupts ... we "know" this is called
  588. * in task context, with IRQs enabled.
  589. */
  590. static void handle_twl4030_sih(unsigned irq, struct irq_desc *desc)
  591. {
  592. struct sih_agent *agent = get_irq_data(irq);
  593. const struct sih *sih = agent->sih;
  594. int isr;
  595. /* reading ISR acks the IRQs, using clear-on-read mode */
  596. local_irq_enable();
  597. isr = sih_read_isr(sih);
  598. local_irq_disable();
  599. if (isr < 0) {
  600. pr_err("twl4030: %s SIH, read ISR error %d\n",
  601. sih->name, isr);
  602. /* REVISIT: recover; eventually mask it all, etc */
  603. return;
  604. }
  605. while (isr) {
  606. irq = fls(isr);
  607. irq--;
  608. isr &= ~BIT(irq);
  609. if (irq < sih->bits)
  610. generic_handle_irq(agent->irq_base + irq);
  611. else
  612. pr_err("twl4030: %s SIH, invalid ISR bit %d\n",
  613. sih->name, irq);
  614. }
  615. }
  616. static unsigned twl4030_irq_next;
  617. /* returns the first IRQ used by this SIH bank,
  618. * or negative errno
  619. */
  620. int twl4030_sih_setup(int module)
  621. {
  622. int sih_mod;
  623. const struct sih *sih = NULL;
  624. struct sih_agent *agent;
  625. int i, irq;
  626. int status = -EINVAL;
  627. unsigned irq_base = twl4030_irq_next;
  628. /* only support modules with standard clear-on-read for now */
  629. for (sih_mod = 0, sih = sih_modules;
  630. sih_mod < nr_sih_modules;
  631. sih_mod++, sih++) {
  632. if (sih->module == module && sih->set_cor) {
  633. if (!WARN((irq_base + sih->bits) > NR_IRQS,
  634. "irq %d for %s too big\n",
  635. irq_base + sih->bits,
  636. sih->name))
  637. status = 0;
  638. break;
  639. }
  640. }
  641. if (status < 0)
  642. return status;
  643. agent = kzalloc(sizeof *agent, GFP_KERNEL);
  644. if (!agent)
  645. return -ENOMEM;
  646. status = 0;
  647. agent->irq_base = irq_base;
  648. agent->sih = sih;
  649. agent->imr = ~0;
  650. INIT_WORK(&agent->mask_work, twl4030_sih_do_mask);
  651. INIT_WORK(&agent->edge_work, twl4030_sih_do_edge);
  652. for (i = 0; i < sih->bits; i++) {
  653. irq = irq_base + i;
  654. set_irq_chip_and_handler(irq, &twl4030_sih_irq_chip,
  655. handle_edge_irq);
  656. set_irq_chip_data(irq, agent);
  657. activate_irq(irq);
  658. }
  659. status = irq_base;
  660. twl4030_irq_next += i;
  661. /* replace generic PIH handler (handle_simple_irq) */
  662. irq = sih_mod + twl4030_irq_base;
  663. set_irq_data(irq, agent);
  664. set_irq_chained_handler(irq, handle_twl4030_sih);
  665. pr_info("twl4030: %s (irq %d) chaining IRQs %d..%d\n", sih->name,
  666. irq, irq_base, twl4030_irq_next - 1);
  667. return status;
  668. }
  669. /* FIXME need a call to reverse twl4030_sih_setup() ... */
  670. /*----------------------------------------------------------------------*/
  671. /* FIXME pass in which interrupt line we'll use ... */
  672. #define twl_irq_line 0
  673. int twl4030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end)
  674. {
  675. static struct irq_chip twl4030_irq_chip;
  676. int status;
  677. int i;
  678. struct task_struct *task;
  679. /*
  680. * Mask and clear all TWL4030 interrupts since initially we do
  681. * not have any TWL4030 module interrupt handlers present
  682. */
  683. status = twl4030_init_sih_modules(twl_irq_line);
  684. if (status < 0)
  685. return status;
  686. wq = create_singlethread_workqueue("twl4030-irqchip");
  687. if (!wq) {
  688. pr_err("twl4030: workqueue FAIL\n");
  689. return -ESRCH;
  690. }
  691. twl4030_irq_base = irq_base;
  692. /* install an irq handler for each of the SIH modules;
  693. * clone dummy irq_chip since PIH can't *do* anything
  694. */
  695. twl4030_irq_chip = dummy_irq_chip;
  696. twl4030_irq_chip.name = "twl4030";
  697. twl4030_sih_irq_chip.ack = dummy_irq_chip.ack;
  698. for (i = irq_base; i < irq_end; i++) {
  699. set_irq_chip_and_handler(i, &twl4030_irq_chip,
  700. handle_simple_irq);
  701. activate_irq(i);
  702. }
  703. twl4030_irq_next = i;
  704. pr_info("twl4030: %s (irq %d) chaining IRQs %d..%d\n", "PIH",
  705. irq_num, irq_base, twl4030_irq_next - 1);
  706. /* ... and the PWR_INT module ... */
  707. status = twl4030_sih_setup(TWL4030_MODULE_INT);
  708. if (status < 0) {
  709. pr_err("twl4030: sih_setup PWR INT --> %d\n", status);
  710. goto fail;
  711. }
  712. /* install an irq handler to demultiplex the TWL4030 interrupt */
  713. init_completion(&irq_event);
  714. status = request_irq(irq_num, handle_twl4030_pih, IRQF_DISABLED,
  715. "TWL4030-PIH", &irq_event);
  716. if (status < 0) {
  717. pr_err("twl4030: could not claim irq%d: %d\n", irq_num, status);
  718. goto fail_rqirq;
  719. }
  720. task = kthread_run(twl4030_irq_thread, (void *)(long)irq_num,
  721. "twl4030-irq");
  722. if (IS_ERR(task)) {
  723. pr_err("twl4030: could not create irq %d thread!\n", irq_num);
  724. status = PTR_ERR(task);
  725. goto fail_kthread;
  726. }
  727. return status;
  728. fail_kthread:
  729. free_irq(irq_num, &irq_event);
  730. fail_rqirq:
  731. /* clean up twl4030_sih_setup */
  732. fail:
  733. for (i = irq_base; i < irq_end; i++)
  734. set_irq_chip_and_handler(i, NULL, NULL);
  735. destroy_workqueue(wq);
  736. wq = NULL;
  737. return status;
  738. }
  739. int twl4030_exit_irq(void)
  740. {
  741. /* FIXME undo twl_init_irq() */
  742. if (twl4030_irq_base) {
  743. pr_err("twl4030: can't yet clean up IRQs?\n");
  744. return -ENOSYS;
  745. }
  746. return 0;
  747. }
  748. int twl4030_init_chip_irq(const char *chip)
  749. {
  750. if (!strcmp(chip, "twl5031")) {
  751. sih_modules = sih_modules_twl5031;
  752. nr_sih_modules = ARRAY_SIZE(sih_modules_twl5031);
  753. } else {
  754. sih_modules = sih_modules_twl4030;
  755. nr_sih_modules = ARRAY_SIZE(sih_modules_twl4030);
  756. }
  757. return 0;
  758. }