twl4030-irq.c 22 KB

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