twl4030-irq.c 22 KB

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