twl4030-irq.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. generic_handle_irq(module_irq);
  295. }
  296. local_irq_enable();
  297. enable_irq(irq);
  298. }
  299. return 0;
  300. }
  301. /*
  302. * handle_twl4030_pih() is the desc->handle method for the twl4030 interrupt.
  303. * This is a chained interrupt, so there is no desc->action method for it.
  304. * Now we need to query the interrupt controller in the twl4030 to determine
  305. * which module is generating the interrupt request. However, we can't do i2c
  306. * transactions in interrupt context, so we must defer that work to a kernel
  307. * thread. All we do here is acknowledge and mask the interrupt and wakeup
  308. * the kernel thread.
  309. */
  310. static irqreturn_t handle_twl4030_pih(int irq, void *devid)
  311. {
  312. /* Acknowledge, clear *AND* mask the interrupt... */
  313. disable_irq_nosync(irq);
  314. complete(devid);
  315. return IRQ_HANDLED;
  316. }
  317. /*----------------------------------------------------------------------*/
  318. /*
  319. * twl4030_init_sih_modules() ... start from a known state where no
  320. * IRQs will be coming in, and where we can quickly enable them then
  321. * handle them as they arrive. Mask all IRQs: maybe init SIH_CTRL.
  322. *
  323. * NOTE: we don't touch EDR registers here; they stay with hardware
  324. * defaults or whatever the last value was. Note that when both EDR
  325. * bits for an IRQ are clear, that's as if its IMR bit is set...
  326. */
  327. static int twl4030_init_sih_modules(unsigned line)
  328. {
  329. const struct sih *sih;
  330. u8 buf[4];
  331. int i;
  332. int status;
  333. /* line 0 == int1_n signal; line 1 == int2_n signal */
  334. if (line > 1)
  335. return -EINVAL;
  336. irq_line = line;
  337. /* disable all interrupts on our line */
  338. memset(buf, 0xff, sizeof buf);
  339. sih = sih_modules;
  340. for (i = 0; i < nr_sih_modules; i++, sih++) {
  341. /* skip USB -- it's funky */
  342. if (!sih->bytes_ixr)
  343. continue;
  344. /* Not all the SIH modules support multiple interrupt lines */
  345. if (sih->irq_lines <= line)
  346. continue;
  347. status = twl_i2c_write(sih->module, buf,
  348. sih->mask[line].imr_offset, sih->bytes_ixr);
  349. if (status < 0)
  350. pr_err("twl4030: err %d initializing %s %s\n",
  351. status, sih->name, "IMR");
  352. /* Maybe disable "exclusive" mode; buffer second pending irq;
  353. * set Clear-On-Read (COR) bit.
  354. *
  355. * NOTE that sometimes COR polarity is documented as being
  356. * inverted: for MADC, COR=1 means "clear on write".
  357. * And for PWR_INT it's not documented...
  358. */
  359. if (sih->set_cor) {
  360. status = twl_i2c_write_u8(sih->module,
  361. TWL4030_SIH_CTRL_COR_MASK,
  362. sih->control_offset);
  363. if (status < 0)
  364. pr_err("twl4030: err %d initializing %s %s\n",
  365. status, sih->name, "SIH_CTRL");
  366. }
  367. }
  368. sih = sih_modules;
  369. for (i = 0; i < nr_sih_modules; i++, sih++) {
  370. u8 rxbuf[4];
  371. int j;
  372. /* skip USB */
  373. if (!sih->bytes_ixr)
  374. continue;
  375. /* Not all the SIH modules support multiple interrupt lines */
  376. if (sih->irq_lines <= line)
  377. continue;
  378. /* Clear pending interrupt status. Either the read was
  379. * enough, or we need to write those bits. Repeat, in
  380. * case an IRQ is pending (PENDDIS=0) ... that's not
  381. * uncommon with PWR_INT.PWRON.
  382. */
  383. for (j = 0; j < 2; j++) {
  384. status = twl_i2c_read(sih->module, rxbuf,
  385. sih->mask[line].isr_offset, sih->bytes_ixr);
  386. if (status < 0)
  387. pr_err("twl4030: err %d initializing %s %s\n",
  388. status, sih->name, "ISR");
  389. if (!sih->set_cor)
  390. status = twl_i2c_write(sih->module, buf,
  391. sih->mask[line].isr_offset,
  392. sih->bytes_ixr);
  393. /* else COR=1 means read sufficed.
  394. * (for most SIH modules...)
  395. */
  396. }
  397. }
  398. return 0;
  399. }
  400. static inline void activate_irq(int irq)
  401. {
  402. #ifdef CONFIG_ARM
  403. /* ARM requires an extra step to clear IRQ_NOREQUEST, which it
  404. * sets on behalf of every irq_chip. Also sets IRQ_NOPROBE.
  405. */
  406. set_irq_flags(irq, IRQF_VALID);
  407. #else
  408. /* same effect on other architectures */
  409. irq_set_noprobe(irq);
  410. #endif
  411. }
  412. /*----------------------------------------------------------------------*/
  413. static DEFINE_SPINLOCK(sih_agent_lock);
  414. static struct workqueue_struct *wq;
  415. struct sih_agent {
  416. int irq_base;
  417. const struct sih *sih;
  418. u32 imr;
  419. bool imr_change_pending;
  420. struct work_struct mask_work;
  421. u32 edge_change;
  422. struct work_struct edge_work;
  423. };
  424. static void twl4030_sih_do_mask(struct work_struct *work)
  425. {
  426. struct sih_agent *agent;
  427. const struct sih *sih;
  428. union {
  429. u8 bytes[4];
  430. u32 word;
  431. } imr;
  432. int status;
  433. agent = container_of(work, struct sih_agent, mask_work);
  434. /* see what work we have */
  435. spin_lock_irq(&sih_agent_lock);
  436. if (agent->imr_change_pending) {
  437. sih = agent->sih;
  438. /* byte[0] gets overwritten as we write ... */
  439. imr.word = cpu_to_le32(agent->imr << 8);
  440. agent->imr_change_pending = false;
  441. } else
  442. sih = NULL;
  443. spin_unlock_irq(&sih_agent_lock);
  444. if (!sih)
  445. return;
  446. /* write the whole mask ... simpler than subsetting it */
  447. status = twl_i2c_write(sih->module, imr.bytes,
  448. sih->mask[irq_line].imr_offset, sih->bytes_ixr);
  449. if (status)
  450. pr_err("twl4030: %s, %s --> %d\n", __func__,
  451. "write", status);
  452. }
  453. static void twl4030_sih_do_edge(struct work_struct *work)
  454. {
  455. struct sih_agent *agent;
  456. const struct sih *sih;
  457. u8 bytes[6];
  458. u32 edge_change;
  459. int status;
  460. agent = container_of(work, struct sih_agent, edge_work);
  461. /* see what work we have */
  462. spin_lock_irq(&sih_agent_lock);
  463. edge_change = agent->edge_change;
  464. agent->edge_change = 0;
  465. sih = edge_change ? agent->sih : NULL;
  466. spin_unlock_irq(&sih_agent_lock);
  467. if (!sih)
  468. return;
  469. /* Read, reserving first byte for write scratch. Yes, this
  470. * could be cached for some speedup ... but be careful about
  471. * any processor on the other IRQ line, EDR registers are
  472. * shared.
  473. */
  474. status = twl_i2c_read(sih->module, bytes + 1,
  475. sih->edr_offset, sih->bytes_edr);
  476. if (status) {
  477. pr_err("twl4030: %s, %s --> %d\n", __func__,
  478. "read", status);
  479. return;
  480. }
  481. /* Modify only the bits we know must change */
  482. while (edge_change) {
  483. int i = fls(edge_change) - 1;
  484. struct irq_data *idata = irq_get_irq_data(i + agent->irq_base);
  485. int byte = 1 + (i >> 2);
  486. int off = (i & 0x3) * 2;
  487. unsigned int type;
  488. bytes[byte] &= ~(0x03 << off);
  489. type = irqd_get_trigger_type(idata);
  490. if (type & IRQ_TYPE_EDGE_RISING)
  491. bytes[byte] |= BIT(off + 1);
  492. if (type & IRQ_TYPE_EDGE_FALLING)
  493. bytes[byte] |= BIT(off + 0);
  494. edge_change &= ~BIT(i);
  495. }
  496. /* Write */
  497. status = twl_i2c_write(sih->module, bytes,
  498. sih->edr_offset, sih->bytes_edr);
  499. if (status)
  500. pr_err("twl4030: %s, %s --> %d\n", __func__,
  501. "write", status);
  502. }
  503. /*----------------------------------------------------------------------*/
  504. /*
  505. * All irq_chip methods get issued from code holding irq_desc[irq].lock,
  506. * which can't perform the underlying I2C operations (because they sleep).
  507. * So we must hand them off to a thread (workqueue) and cope with asynch
  508. * completion, potentially including some re-ordering, of these requests.
  509. */
  510. static void twl4030_sih_mask(struct irq_data *data)
  511. {
  512. struct sih_agent *sih = irq_data_get_irq_chip_data(data);
  513. unsigned long flags;
  514. spin_lock_irqsave(&sih_agent_lock, flags);
  515. sih->imr |= BIT(data->irq - sih->irq_base);
  516. sih->imr_change_pending = true;
  517. queue_work(wq, &sih->mask_work);
  518. spin_unlock_irqrestore(&sih_agent_lock, flags);
  519. }
  520. static void twl4030_sih_unmask(struct irq_data *data)
  521. {
  522. struct sih_agent *sih = irq_data_get_irq_chip_data(data);
  523. unsigned long flags;
  524. spin_lock_irqsave(&sih_agent_lock, flags);
  525. sih->imr &= ~BIT(data->irq - sih->irq_base);
  526. sih->imr_change_pending = true;
  527. queue_work(wq, &sih->mask_work);
  528. spin_unlock_irqrestore(&sih_agent_lock, flags);
  529. }
  530. static int twl4030_sih_set_type(struct irq_data *data, unsigned trigger)
  531. {
  532. struct sih_agent *sih = irq_data_get_irq_chip_data(data);
  533. unsigned long flags;
  534. if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  535. return -EINVAL;
  536. spin_lock_irqsave(&sih_agent_lock, flags);
  537. if (irqd_get_trigger_type(data) != trigger) {
  538. sih->edge_change |= BIT(data->irq - sih->irq_base);
  539. queue_work(wq, &sih->edge_work);
  540. }
  541. spin_unlock_irqrestore(&sih_agent_lock, flags);
  542. return 0;
  543. }
  544. static struct irq_chip twl4030_sih_irq_chip = {
  545. .name = "twl4030",
  546. .irq_mask = twl4030_sih_mask,
  547. .irq_unmask = twl4030_sih_unmask,
  548. .irq_set_type = twl4030_sih_set_type,
  549. };
  550. /*----------------------------------------------------------------------*/
  551. static inline int sih_read_isr(const struct sih *sih)
  552. {
  553. int status;
  554. union {
  555. u8 bytes[4];
  556. u32 word;
  557. } isr;
  558. /* FIXME need retry-on-error ... */
  559. isr.word = 0;
  560. status = twl_i2c_read(sih->module, isr.bytes,
  561. sih->mask[irq_line].isr_offset, sih->bytes_ixr);
  562. return (status < 0) ? status : le32_to_cpu(isr.word);
  563. }
  564. /*
  565. * Generic handler for SIH interrupts ... we "know" this is called
  566. * in task context, with IRQs enabled.
  567. */
  568. static void handle_twl4030_sih(unsigned irq, struct irq_desc *desc)
  569. {
  570. struct sih_agent *agent = irq_get_handler_data(irq);
  571. const struct sih *sih = agent->sih;
  572. int isr;
  573. /* reading ISR acks the IRQs, using clear-on-read mode */
  574. local_irq_enable();
  575. isr = sih_read_isr(sih);
  576. local_irq_disable();
  577. if (isr < 0) {
  578. pr_err("twl4030: %s SIH, read ISR error %d\n",
  579. sih->name, isr);
  580. /* REVISIT: recover; eventually mask it all, etc */
  581. return;
  582. }
  583. while (isr) {
  584. irq = fls(isr);
  585. irq--;
  586. isr &= ~BIT(irq);
  587. if (irq < sih->bits)
  588. generic_handle_irq(agent->irq_base + irq);
  589. else
  590. pr_err("twl4030: %s SIH, invalid ISR bit %d\n",
  591. sih->name, irq);
  592. }
  593. }
  594. static unsigned twl4030_irq_next;
  595. /* returns the first IRQ used by this SIH bank,
  596. * or negative errno
  597. */
  598. int twl4030_sih_setup(int module)
  599. {
  600. int sih_mod;
  601. const struct sih *sih = NULL;
  602. struct sih_agent *agent;
  603. int i, irq;
  604. int status = -EINVAL;
  605. unsigned irq_base = twl4030_irq_next;
  606. /* only support modules with standard clear-on-read for now */
  607. for (sih_mod = 0, sih = sih_modules;
  608. sih_mod < nr_sih_modules;
  609. sih_mod++, sih++) {
  610. if (sih->module == module && sih->set_cor) {
  611. if (!WARN((irq_base + sih->bits) > NR_IRQS,
  612. "irq %d for %s too big\n",
  613. irq_base + sih->bits,
  614. sih->name))
  615. status = 0;
  616. break;
  617. }
  618. }
  619. if (status < 0)
  620. return status;
  621. agent = kzalloc(sizeof *agent, GFP_KERNEL);
  622. if (!agent)
  623. return -ENOMEM;
  624. status = 0;
  625. agent->irq_base = irq_base;
  626. agent->sih = sih;
  627. agent->imr = ~0;
  628. INIT_WORK(&agent->mask_work, twl4030_sih_do_mask);
  629. INIT_WORK(&agent->edge_work, twl4030_sih_do_edge);
  630. for (i = 0; i < sih->bits; i++) {
  631. irq = irq_base + i;
  632. irq_set_chip_and_handler(irq, &twl4030_sih_irq_chip,
  633. handle_edge_irq);
  634. irq_set_chip_data(irq, agent);
  635. activate_irq(irq);
  636. }
  637. status = irq_base;
  638. twl4030_irq_next += i;
  639. /* replace generic PIH handler (handle_simple_irq) */
  640. irq = sih_mod + twl4030_irq_base;
  641. irq_set_handler_data(irq, agent);
  642. irq_set_chained_handler(irq, handle_twl4030_sih);
  643. pr_info("twl4030: %s (irq %d) chaining IRQs %d..%d\n", sih->name,
  644. irq, irq_base, twl4030_irq_next - 1);
  645. return status;
  646. }
  647. /* FIXME need a call to reverse twl4030_sih_setup() ... */
  648. /*----------------------------------------------------------------------*/
  649. /* FIXME pass in which interrupt line we'll use ... */
  650. #define twl_irq_line 0
  651. int twl4030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end)
  652. {
  653. static struct irq_chip twl4030_irq_chip;
  654. int status;
  655. int i;
  656. struct task_struct *task;
  657. /*
  658. * Mask and clear all TWL4030 interrupts since initially we do
  659. * not have any TWL4030 module interrupt handlers present
  660. */
  661. status = twl4030_init_sih_modules(twl_irq_line);
  662. if (status < 0)
  663. return status;
  664. wq = create_singlethread_workqueue("twl4030-irqchip");
  665. if (!wq) {
  666. pr_err("twl4030: workqueue FAIL\n");
  667. return -ESRCH;
  668. }
  669. twl4030_irq_base = irq_base;
  670. /* install an irq handler for each of the SIH modules;
  671. * clone dummy irq_chip since PIH can't *do* anything
  672. */
  673. twl4030_irq_chip = dummy_irq_chip;
  674. twl4030_irq_chip.name = "twl4030";
  675. twl4030_sih_irq_chip.irq_ack = dummy_irq_chip.irq_ack;
  676. for (i = irq_base; i < irq_end; i++) {
  677. irq_set_chip_and_handler(i, &twl4030_irq_chip,
  678. handle_simple_irq);
  679. activate_irq(i);
  680. }
  681. twl4030_irq_next = i;
  682. pr_info("twl4030: %s (irq %d) chaining IRQs %d..%d\n", "PIH",
  683. irq_num, irq_base, twl4030_irq_next - 1);
  684. /* ... and the PWR_INT module ... */
  685. status = twl4030_sih_setup(TWL4030_MODULE_INT);
  686. if (status < 0) {
  687. pr_err("twl4030: sih_setup PWR INT --> %d\n", status);
  688. goto fail;
  689. }
  690. /* install an irq handler to demultiplex the TWL4030 interrupt */
  691. init_completion(&irq_event);
  692. status = request_irq(irq_num, handle_twl4030_pih, IRQF_DISABLED,
  693. "TWL4030-PIH", &irq_event);
  694. if (status < 0) {
  695. pr_err("twl4030: could not claim irq%d: %d\n", irq_num, status);
  696. goto fail_rqirq;
  697. }
  698. task = kthread_run(twl4030_irq_thread, (void *)(long)irq_num,
  699. "twl4030-irq");
  700. if (IS_ERR(task)) {
  701. pr_err("twl4030: could not create irq %d thread!\n", irq_num);
  702. status = PTR_ERR(task);
  703. goto fail_kthread;
  704. }
  705. return status;
  706. fail_kthread:
  707. free_irq(irq_num, &irq_event);
  708. fail_rqirq:
  709. /* clean up twl4030_sih_setup */
  710. fail:
  711. for (i = irq_base; i < irq_end; i++)
  712. irq_set_chip_and_handler(i, NULL, NULL);
  713. destroy_workqueue(wq);
  714. wq = NULL;
  715. return status;
  716. }
  717. int twl4030_exit_irq(void)
  718. {
  719. /* FIXME undo twl_init_irq() */
  720. if (twl4030_irq_base) {
  721. pr_err("twl4030: can't yet clean up IRQs?\n");
  722. return -ENOSYS;
  723. }
  724. return 0;
  725. }
  726. int twl4030_init_chip_irq(const char *chip)
  727. {
  728. if (!strcmp(chip, "twl5031")) {
  729. sih_modules = sih_modules_twl5031;
  730. nr_sih_modules = ARRAY_SIZE(sih_modules_twl5031);
  731. } else {
  732. sih_modules = sih_modules_twl4030;
  733. nr_sih_modules = ARRAY_SIZE(sih_modules_twl4030);
  734. }
  735. return 0;
  736. }