twl4030-irq.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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/slab.h>
  33. #include <linux/i2c/twl.h>
  34. #include "twl-core.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 sih_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. .set_cor = true,
  131. .bits = 12,
  132. .bytes_ixr = 2,
  133. .edr_offset = TWL4030_INTERRUPTS_BCIEDR1,
  134. /* Note: most of these IRQs default to no-trigger */
  135. .bytes_edr = 3,
  136. .irq_lines = 2,
  137. .mask = { {
  138. .isr_offset = TWL4030_INTERRUPTS_BCIISR1A,
  139. .imr_offset = TWL4030_INTERRUPTS_BCIIMR1A,
  140. }, {
  141. .isr_offset = TWL4030_INTERRUPTS_BCIISR1B,
  142. .imr_offset = TWL4030_INTERRUPTS_BCIIMR1B,
  143. }, },
  144. },
  145. [3] = {
  146. .name = "madc",
  147. SIH_INITIALIZER(MADC, 4)
  148. },
  149. [4] = {
  150. /* USB doesn't use the same SIH organization */
  151. .name = "usb",
  152. },
  153. [5] = {
  154. .name = "power",
  155. .set_cor = true,
  156. SIH_INITIALIZER(INT_PWR, 8)
  157. },
  158. /* there are no SIH modules #6 or #7 ... */
  159. };
  160. static const struct sih sih_modules_twl5031[8] = {
  161. [0] = {
  162. .name = "gpio",
  163. .module = TWL4030_MODULE_GPIO,
  164. .control_offset = REG_GPIO_SIH_CTRL,
  165. .set_cor = true,
  166. .bits = TWL4030_GPIO_MAX,
  167. .bytes_ixr = 3,
  168. /* Note: *all* of these IRQs default to no-trigger */
  169. .edr_offset = REG_GPIO_EDR1,
  170. .bytes_edr = 5,
  171. .irq_lines = 2,
  172. .mask = { {
  173. .isr_offset = REG_GPIO_ISR1A,
  174. .imr_offset = REG_GPIO_IMR1A,
  175. }, {
  176. .isr_offset = REG_GPIO_ISR1B,
  177. .imr_offset = REG_GPIO_IMR1B,
  178. }, },
  179. },
  180. [1] = {
  181. .name = "keypad",
  182. .set_cor = true,
  183. SIH_INITIALIZER(KEYPAD_KEYP, 4)
  184. },
  185. [2] = {
  186. .name = "bci",
  187. .module = TWL5031_MODULE_INTERRUPTS,
  188. .control_offset = TWL5031_INTERRUPTS_BCISIHCTRL,
  189. .bits = 7,
  190. .bytes_ixr = 1,
  191. .edr_offset = TWL5031_INTERRUPTS_BCIEDR1,
  192. /* Note: most of these IRQs default to no-trigger */
  193. .bytes_edr = 2,
  194. .irq_lines = 2,
  195. .mask = { {
  196. .isr_offset = TWL5031_INTERRUPTS_BCIISR1,
  197. .imr_offset = TWL5031_INTERRUPTS_BCIIMR1,
  198. }, {
  199. .isr_offset = TWL5031_INTERRUPTS_BCIISR2,
  200. .imr_offset = TWL5031_INTERRUPTS_BCIIMR2,
  201. }, },
  202. },
  203. [3] = {
  204. .name = "madc",
  205. SIH_INITIALIZER(MADC, 4)
  206. },
  207. [4] = {
  208. /* USB doesn't use the same SIH organization */
  209. .name = "usb",
  210. },
  211. [5] = {
  212. .name = "power",
  213. .set_cor = true,
  214. SIH_INITIALIZER(INT_PWR, 8)
  215. },
  216. [6] = {
  217. /*
  218. * ECI/DBI doesn't use the same SIH organization.
  219. * For example, it supports only one interrupt output line.
  220. * That is, the interrupts are seen on both INT1 and INT2 lines.
  221. */
  222. .name = "eci_dbi",
  223. .module = TWL5031_MODULE_ACCESSORY,
  224. .bits = 9,
  225. .bytes_ixr = 2,
  226. .irq_lines = 1,
  227. .mask = { {
  228. .isr_offset = TWL5031_ACIIDR_LSB,
  229. .imr_offset = TWL5031_ACIIMR_LSB,
  230. }, },
  231. },
  232. [7] = {
  233. /* Audio accessory */
  234. .name = "audio",
  235. .module = TWL5031_MODULE_ACCESSORY,
  236. .control_offset = TWL5031_ACCSIHCTRL,
  237. .bits = 2,
  238. .bytes_ixr = 1,
  239. .edr_offset = TWL5031_ACCEDR1,
  240. /* Note: most of these IRQs default to no-trigger */
  241. .bytes_edr = 1,
  242. .irq_lines = 2,
  243. .mask = { {
  244. .isr_offset = TWL5031_ACCISR1,
  245. .imr_offset = TWL5031_ACCIMR1,
  246. }, {
  247. .isr_offset = TWL5031_ACCISR2,
  248. .imr_offset = TWL5031_ACCIMR2,
  249. }, },
  250. },
  251. };
  252. #undef TWL4030_MODULE_KEYPAD_KEYP
  253. #undef TWL4030_MODULE_INT_PWR
  254. #undef TWL4030_INT_PWR_EDR
  255. /*----------------------------------------------------------------------*/
  256. static unsigned twl4030_irq_base;
  257. /*
  258. * handle_twl4030_pih() is the desc->handle method for the twl4030 interrupt.
  259. * This is a chained interrupt, so there is no desc->action method for it.
  260. * Now we need to query the interrupt controller in the twl4030 to determine
  261. * which module is generating the interrupt request. However, we can't do i2c
  262. * transactions in interrupt context, so we must defer that work to a kernel
  263. * thread. All we do here is acknowledge and mask the interrupt and wakeup
  264. * the kernel thread.
  265. */
  266. static irqreturn_t handle_twl4030_pih(int irq, void *devid)
  267. {
  268. int module_irq;
  269. irqreturn_t ret;
  270. u8 pih_isr;
  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", ret);
  275. return IRQ_NONE;
  276. }
  277. /* these handlers deal with the relevant SIH irq status */
  278. for (module_irq = twl4030_irq_base;
  279. pih_isr;
  280. pih_isr >>= 1, module_irq++) {
  281. if (pih_isr & 0x1)
  282. generic_handle_irq(module_irq);
  283. }
  284. return IRQ_HANDLED;
  285. }
  286. /*----------------------------------------------------------------------*/
  287. /*
  288. * twl4030_init_sih_modules() ... start from a known state where no
  289. * IRQs will be coming in, and where we can quickly enable them then
  290. * handle them as they arrive. Mask all IRQs: maybe init SIH_CTRL.
  291. *
  292. * NOTE: we don't touch EDR registers here; they stay with hardware
  293. * defaults or whatever the last value was. Note that when both EDR
  294. * bits for an IRQ are clear, that's as if its IMR bit is set...
  295. */
  296. static int twl4030_init_sih_modules(unsigned line)
  297. {
  298. const struct sih *sih;
  299. u8 buf[4];
  300. int i;
  301. int status;
  302. /* line 0 == int1_n signal; line 1 == int2_n signal */
  303. if (line > 1)
  304. return -EINVAL;
  305. irq_line = line;
  306. /* disable all interrupts on our line */
  307. memset(buf, 0xff, sizeof buf);
  308. sih = sih_modules;
  309. for (i = 0; i < nr_sih_modules; i++, sih++) {
  310. /* skip USB -- it's funky */
  311. if (!sih->bytes_ixr)
  312. continue;
  313. /* Not all the SIH modules support multiple interrupt lines */
  314. if (sih->irq_lines <= line)
  315. continue;
  316. status = twl_i2c_write(sih->module, buf,
  317. sih->mask[line].imr_offset, sih->bytes_ixr);
  318. if (status < 0)
  319. pr_err("twl4030: err %d initializing %s %s\n",
  320. status, sih->name, "IMR");
  321. /* Maybe disable "exclusive" mode; buffer second pending irq;
  322. * set Clear-On-Read (COR) bit.
  323. *
  324. * NOTE that sometimes COR polarity is documented as being
  325. * inverted: for MADC, COR=1 means "clear on write".
  326. * And for PWR_INT it's not documented...
  327. */
  328. if (sih->set_cor) {
  329. status = twl_i2c_write_u8(sih->module,
  330. TWL4030_SIH_CTRL_COR_MASK,
  331. sih->control_offset);
  332. if (status < 0)
  333. pr_err("twl4030: err %d initializing %s %s\n",
  334. status, sih->name, "SIH_CTRL");
  335. }
  336. }
  337. sih = sih_modules;
  338. for (i = 0; i < nr_sih_modules; i++, sih++) {
  339. u8 rxbuf[4];
  340. int j;
  341. /* skip USB */
  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. /* Clear pending interrupt status. Either the read was
  348. * enough, or we need to write those bits. Repeat, in
  349. * case an IRQ is pending (PENDDIS=0) ... that's not
  350. * uncommon with PWR_INT.PWRON.
  351. */
  352. for (j = 0; j < 2; j++) {
  353. status = twl_i2c_read(sih->module, rxbuf,
  354. sih->mask[line].isr_offset, sih->bytes_ixr);
  355. if (status < 0)
  356. pr_err("twl4030: err %d initializing %s %s\n",
  357. status, sih->name, "ISR");
  358. if (!sih->set_cor)
  359. status = twl_i2c_write(sih->module, buf,
  360. sih->mask[line].isr_offset,
  361. sih->bytes_ixr);
  362. /* else COR=1 means read sufficed.
  363. * (for most SIH modules...)
  364. */
  365. }
  366. }
  367. return 0;
  368. }
  369. static inline void activate_irq(int irq)
  370. {
  371. #ifdef CONFIG_ARM
  372. /* ARM requires an extra step to clear IRQ_NOREQUEST, which it
  373. * sets on behalf of every irq_chip. Also sets IRQ_NOPROBE.
  374. */
  375. set_irq_flags(irq, IRQF_VALID);
  376. #else
  377. /* same effect on other architectures */
  378. irq_set_noprobe(irq);
  379. #endif
  380. }
  381. /*----------------------------------------------------------------------*/
  382. static struct workqueue_struct *wq;
  383. struct sih_agent {
  384. int irq_base;
  385. const struct sih *sih;
  386. u32 imr;
  387. bool imr_change_pending;
  388. struct work_struct mask_work;
  389. u32 edge_change;
  390. struct work_struct edge_work;
  391. struct mutex irq_lock;
  392. };
  393. static void twl4030_sih_do_mask(struct work_struct *work)
  394. {
  395. struct sih_agent *agent;
  396. const struct sih *sih;
  397. union {
  398. u8 bytes[4];
  399. u32 word;
  400. } imr;
  401. int status;
  402. agent = container_of(work, struct sih_agent, mask_work);
  403. /* see what work we have */
  404. if (agent->imr_change_pending) {
  405. sih = agent->sih;
  406. /* byte[0] gets overwritten as we write ... */
  407. imr.word = cpu_to_le32(agent->imr << 8);
  408. agent->imr_change_pending = false;
  409. } else
  410. sih = NULL;
  411. if (!sih)
  412. return;
  413. /* write the whole mask ... simpler than subsetting it */
  414. status = twl_i2c_write(sih->module, imr.bytes,
  415. sih->mask[irq_line].imr_offset, sih->bytes_ixr);
  416. if (status)
  417. pr_err("twl4030: %s, %s --> %d\n", __func__,
  418. "write", status);
  419. }
  420. static void twl4030_sih_do_edge(struct work_struct *work)
  421. {
  422. struct sih_agent *agent;
  423. const struct sih *sih;
  424. u8 bytes[6];
  425. u32 edge_change;
  426. int status;
  427. agent = container_of(work, struct sih_agent, edge_work);
  428. /* see what work we have */
  429. edge_change = agent->edge_change;
  430. agent->edge_change = 0;
  431. sih = edge_change ? agent->sih : NULL;
  432. if (!sih)
  433. return;
  434. /* Read, reserving first byte for write scratch. Yes, this
  435. * could be cached for some speedup ... but be careful about
  436. * any processor on the other IRQ line, EDR registers are
  437. * shared.
  438. */
  439. status = twl_i2c_read(sih->module, bytes + 1,
  440. sih->edr_offset, sih->bytes_edr);
  441. if (status) {
  442. pr_err("twl4030: %s, %s --> %d\n", __func__,
  443. "read", status);
  444. return;
  445. }
  446. /* Modify only the bits we know must change */
  447. while (edge_change) {
  448. int i = fls(edge_change) - 1;
  449. struct irq_data *idata = irq_get_irq_data(i + agent->irq_base);
  450. int byte = 1 + (i >> 2);
  451. int off = (i & 0x3) * 2;
  452. unsigned int type;
  453. bytes[byte] &= ~(0x03 << off);
  454. type = irqd_get_trigger_type(idata);
  455. if (type & IRQ_TYPE_EDGE_RISING)
  456. bytes[byte] |= BIT(off + 1);
  457. if (type & IRQ_TYPE_EDGE_FALLING)
  458. bytes[byte] |= BIT(off + 0);
  459. edge_change &= ~BIT(i);
  460. }
  461. /* Write */
  462. status = twl_i2c_write(sih->module, bytes,
  463. sih->edr_offset, sih->bytes_edr);
  464. if (status)
  465. pr_err("twl4030: %s, %s --> %d\n", __func__,
  466. "write", status);
  467. }
  468. /*----------------------------------------------------------------------*/
  469. /*
  470. * All irq_chip methods get issued from code holding irq_desc[irq].lock,
  471. * which can't perform the underlying I2C operations (because they sleep).
  472. * So we must hand them off to a thread (workqueue) and cope with asynch
  473. * completion, potentially including some re-ordering, of these requests.
  474. */
  475. static void twl4030_sih_mask(struct irq_data *data)
  476. {
  477. struct sih_agent *sih = irq_data_get_irq_chip_data(data);
  478. sih->imr |= BIT(data->irq - sih->irq_base);
  479. sih->imr_change_pending = true;
  480. queue_work(wq, &sih->mask_work);
  481. }
  482. static void twl4030_sih_unmask(struct irq_data *data)
  483. {
  484. struct sih_agent *sih = irq_data_get_irq_chip_data(data);
  485. sih->imr &= ~BIT(data->irq - sih->irq_base);
  486. sih->imr_change_pending = true;
  487. queue_work(wq, &sih->mask_work);
  488. }
  489. static int twl4030_sih_set_type(struct irq_data *data, unsigned trigger)
  490. {
  491. struct sih_agent *sih = irq_data_get_irq_chip_data(data);
  492. if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  493. return -EINVAL;
  494. if (irqd_get_trigger_type(data) != trigger) {
  495. sih->edge_change |= BIT(data->irq - sih->irq_base);
  496. queue_work(wq, &sih->edge_work);
  497. }
  498. return 0;
  499. }
  500. static void twl4030_sih_bus_lock(struct irq_data *data)
  501. {
  502. struct sih_agent *sih = irq_data_get_irq_chip_data(data);
  503. mutex_lock(&sih->irq_lock);
  504. }
  505. static void twl4030_sih_bus_sync_unlock(struct irq_data *data)
  506. {
  507. struct sih_agent *sih = irq_data_get_irq_chip_data(data);
  508. mutex_unlock(&sih->irq_lock);
  509. }
  510. static struct irq_chip twl4030_sih_irq_chip = {
  511. .name = "twl4030",
  512. .irq_mask = twl4030_sih_mask,
  513. .irq_unmask = twl4030_sih_unmask,
  514. .irq_set_type = twl4030_sih_set_type,
  515. .irq_bus_lock = twl4030_sih_bus_lock,
  516. .irq_bus_sync_unlock = twl4030_sih_bus_sync_unlock,
  517. };
  518. /*----------------------------------------------------------------------*/
  519. static inline int sih_read_isr(const struct sih *sih)
  520. {
  521. int status;
  522. union {
  523. u8 bytes[4];
  524. u32 word;
  525. } isr;
  526. /* FIXME need retry-on-error ... */
  527. isr.word = 0;
  528. status = twl_i2c_read(sih->module, isr.bytes,
  529. sih->mask[irq_line].isr_offset, sih->bytes_ixr);
  530. return (status < 0) ? status : le32_to_cpu(isr.word);
  531. }
  532. /*
  533. * Generic handler for SIH interrupts ... we "know" this is called
  534. * in task context, with IRQs enabled.
  535. */
  536. static void handle_twl4030_sih(unsigned irq, struct irq_desc *desc)
  537. {
  538. struct sih_agent *agent = irq_get_handler_data(irq);
  539. const struct sih *sih = agent->sih;
  540. int isr;
  541. /* reading ISR acks the IRQs, using clear-on-read mode */
  542. local_irq_enable();
  543. isr = sih_read_isr(sih);
  544. local_irq_disable();
  545. if (isr < 0) {
  546. pr_err("twl4030: %s SIH, read ISR error %d\n",
  547. sih->name, isr);
  548. /* REVISIT: recover; eventually mask it all, etc */
  549. return;
  550. }
  551. while (isr) {
  552. irq = fls(isr);
  553. irq--;
  554. isr &= ~BIT(irq);
  555. if (irq < sih->bits)
  556. generic_handle_irq(agent->irq_base + irq);
  557. else
  558. pr_err("twl4030: %s SIH, invalid ISR bit %d\n",
  559. sih->name, irq);
  560. }
  561. }
  562. static unsigned twl4030_irq_next;
  563. /* returns the first IRQ used by this SIH bank,
  564. * or negative errno
  565. */
  566. int twl4030_sih_setup(int module)
  567. {
  568. int sih_mod;
  569. const struct sih *sih = NULL;
  570. struct sih_agent *agent;
  571. int i, irq;
  572. int status = -EINVAL;
  573. unsigned irq_base = twl4030_irq_next;
  574. /* only support modules with standard clear-on-read for now */
  575. for (sih_mod = 0, sih = sih_modules;
  576. sih_mod < nr_sih_modules;
  577. sih_mod++, sih++) {
  578. if (sih->module == module && sih->set_cor) {
  579. if (!WARN((irq_base + sih->bits) > NR_IRQS,
  580. "irq %d for %s too big\n",
  581. irq_base + sih->bits,
  582. sih->name))
  583. status = 0;
  584. break;
  585. }
  586. }
  587. if (status < 0)
  588. return status;
  589. agent = kzalloc(sizeof *agent, GFP_KERNEL);
  590. if (!agent)
  591. return -ENOMEM;
  592. status = 0;
  593. agent->irq_base = irq_base;
  594. agent->sih = sih;
  595. agent->imr = ~0;
  596. mutex_init(&agent->irq_lock);
  597. INIT_WORK(&agent->mask_work, twl4030_sih_do_mask);
  598. INIT_WORK(&agent->edge_work, twl4030_sih_do_edge);
  599. for (i = 0; i < sih->bits; i++) {
  600. irq = irq_base + i;
  601. irq_set_chip_data(irq, agent);
  602. irq_set_chip_and_handler(irq, &twl4030_sih_irq_chip,
  603. handle_edge_irq);
  604. activate_irq(irq);
  605. }
  606. status = irq_base;
  607. twl4030_irq_next += i;
  608. /* replace generic PIH handler (handle_simple_irq) */
  609. irq = sih_mod + twl4030_irq_base;
  610. irq_set_handler_data(irq, agent);
  611. irq_set_chained_handler(irq, handle_twl4030_sih);
  612. pr_info("twl4030: %s (irq %d) chaining IRQs %d..%d\n", sih->name,
  613. irq, irq_base, twl4030_irq_next - 1);
  614. return status;
  615. }
  616. /* FIXME need a call to reverse twl4030_sih_setup() ... */
  617. /*----------------------------------------------------------------------*/
  618. /* FIXME pass in which interrupt line we'll use ... */
  619. #define twl_irq_line 0
  620. int twl4030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end)
  621. {
  622. static struct irq_chip twl4030_irq_chip;
  623. int status;
  624. int i;
  625. /*
  626. * Mask and clear all TWL4030 interrupts since initially we do
  627. * not have any TWL4030 module interrupt handlers present
  628. */
  629. status = twl4030_init_sih_modules(twl_irq_line);
  630. if (status < 0)
  631. return status;
  632. wq = create_singlethread_workqueue("twl4030-irqchip");
  633. if (!wq) {
  634. pr_err("twl4030: workqueue FAIL\n");
  635. return -ESRCH;
  636. }
  637. twl4030_irq_base = irq_base;
  638. /* install an irq handler for each of the SIH modules;
  639. * clone dummy irq_chip since PIH can't *do* anything
  640. */
  641. twl4030_irq_chip = dummy_irq_chip;
  642. twl4030_irq_chip.name = "twl4030";
  643. twl4030_sih_irq_chip.irq_ack = dummy_irq_chip.irq_ack;
  644. for (i = irq_base; i < irq_end; i++) {
  645. irq_set_chip_and_handler(i, &twl4030_irq_chip,
  646. handle_simple_irq);
  647. activate_irq(i);
  648. }
  649. twl4030_irq_next = i;
  650. pr_info("twl4030: %s (irq %d) chaining IRQs %d..%d\n", "PIH",
  651. irq_num, irq_base, twl4030_irq_next - 1);
  652. /* ... and the PWR_INT module ... */
  653. status = twl4030_sih_setup(TWL4030_MODULE_INT);
  654. if (status < 0) {
  655. pr_err("twl4030: sih_setup PWR INT --> %d\n", status);
  656. goto fail;
  657. }
  658. /* install an irq handler to demultiplex the TWL4030 interrupt */
  659. status = request_threaded_irq(irq_num, NULL, handle_twl4030_pih,
  660. IRQF_DISABLED, "TWL4030-PIH", NULL);
  661. if (status < 0) {
  662. pr_err("twl4030: could not claim irq%d: %d\n", irq_num, status);
  663. goto fail_rqirq;
  664. }
  665. return status;
  666. fail_rqirq:
  667. /* clean up twl4030_sih_setup */
  668. fail:
  669. for (i = irq_base; i < irq_end; i++)
  670. irq_set_chip_and_handler(i, NULL, NULL);
  671. destroy_workqueue(wq);
  672. wq = NULL;
  673. return status;
  674. }
  675. int twl4030_exit_irq(void)
  676. {
  677. /* FIXME undo twl_init_irq() */
  678. if (twl4030_irq_base) {
  679. pr_err("twl4030: can't yet clean up IRQs?\n");
  680. return -ENOSYS;
  681. }
  682. return 0;
  683. }
  684. int twl4030_init_chip_irq(const char *chip)
  685. {
  686. if (!strcmp(chip, "twl5031")) {
  687. sih_modules = sih_modules_twl5031;
  688. nr_sih_modules = ARRAY_SIZE(sih_modules_twl5031);
  689. } else {
  690. sih_modules = sih_modules_twl4030;
  691. nr_sih_modules = ARRAY_SIZE(sih_modules_twl4030);
  692. }
  693. return 0;
  694. }