twl4030-irq.c 20 KB

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