twl4030-irq.c 22 KB

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