twl4030-irq.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. irqreturn_t ret;
  275. u8 pih_isr;
  276. ret = twl_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
  277. REG_PIH_ISR_P1);
  278. if (ret) {
  279. pr_warning("twl4030: I2C error %d reading PIH ISR\n", ret);
  280. return IRQ_NONE;
  281. }
  282. while (pih_isr) {
  283. unsigned long pending = __ffs(pih_isr);
  284. unsigned int irq;
  285. pih_isr &= ~BIT(pending);
  286. irq = pending + twl4030_irq_base;
  287. handle_nested_irq(irq);
  288. }
  289. return IRQ_HANDLED;
  290. }
  291. /*----------------------------------------------------------------------*/
  292. /*
  293. * twl4030_init_sih_modules() ... start from a known state where no
  294. * IRQs will be coming in, and where we can quickly enable them then
  295. * handle them as they arrive. Mask all IRQs: maybe init SIH_CTRL.
  296. *
  297. * NOTE: we don't touch EDR registers here; they stay with hardware
  298. * defaults or whatever the last value was. Note that when both EDR
  299. * bits for an IRQ are clear, that's as if its IMR bit is set...
  300. */
  301. static int twl4030_init_sih_modules(unsigned line)
  302. {
  303. const struct sih *sih;
  304. u8 buf[4];
  305. int i;
  306. int status;
  307. /* line 0 == int1_n signal; line 1 == int2_n signal */
  308. if (line > 1)
  309. return -EINVAL;
  310. irq_line = line;
  311. /* disable all interrupts on our line */
  312. memset(buf, 0xff, sizeof buf);
  313. sih = sih_modules;
  314. for (i = 0; i < nr_sih_modules; i++, sih++) {
  315. /* skip USB -- it's funky */
  316. if (!sih->bytes_ixr)
  317. continue;
  318. /* Not all the SIH modules support multiple interrupt lines */
  319. if (sih->irq_lines <= line)
  320. continue;
  321. status = twl_i2c_write(sih->module, buf,
  322. sih->mask[line].imr_offset, sih->bytes_ixr);
  323. if (status < 0)
  324. pr_err("twl4030: err %d initializing %s %s\n",
  325. status, sih->name, "IMR");
  326. /*
  327. * Maybe disable "exclusive" mode; buffer second pending irq;
  328. * set Clear-On-Read (COR) bit.
  329. *
  330. * NOTE that sometimes COR polarity is documented as being
  331. * inverted: for MADC, COR=1 means "clear on write".
  332. * And for PWR_INT it's not documented...
  333. */
  334. if (sih->set_cor) {
  335. status = twl_i2c_write_u8(sih->module,
  336. TWL4030_SIH_CTRL_COR_MASK,
  337. sih->control_offset);
  338. if (status < 0)
  339. pr_err("twl4030: err %d initializing %s %s\n",
  340. status, sih->name, "SIH_CTRL");
  341. }
  342. }
  343. sih = sih_modules;
  344. for (i = 0; i < nr_sih_modules; i++, sih++) {
  345. u8 rxbuf[4];
  346. int j;
  347. /* skip USB */
  348. if (!sih->bytes_ixr)
  349. continue;
  350. /* Not all the SIH modules support multiple interrupt lines */
  351. if (sih->irq_lines <= line)
  352. continue;
  353. /*
  354. * Clear pending interrupt status. Either the read was
  355. * enough, or we need to write those bits. Repeat, in
  356. * case an IRQ is pending (PENDDIS=0) ... that's not
  357. * uncommon with PWR_INT.PWRON.
  358. */
  359. for (j = 0; j < 2; j++) {
  360. status = twl_i2c_read(sih->module, rxbuf,
  361. sih->mask[line].isr_offset, sih->bytes_ixr);
  362. if (status < 0)
  363. pr_err("twl4030: err %d initializing %s %s\n",
  364. status, sih->name, "ISR");
  365. if (!sih->set_cor)
  366. status = twl_i2c_write(sih->module, buf,
  367. sih->mask[line].isr_offset,
  368. sih->bytes_ixr);
  369. /*
  370. * else COR=1 means read sufficed.
  371. * (for most SIH modules...)
  372. */
  373. }
  374. }
  375. return 0;
  376. }
  377. static inline void activate_irq(int irq)
  378. {
  379. #ifdef CONFIG_ARM
  380. /*
  381. * ARM requires an extra step to clear IRQ_NOREQUEST, which it
  382. * sets on behalf of every irq_chip. Also sets IRQ_NOPROBE.
  383. */
  384. set_irq_flags(irq, IRQF_VALID);
  385. #else
  386. /* same effect on other architectures */
  387. irq_set_noprobe(irq);
  388. #endif
  389. }
  390. /*----------------------------------------------------------------------*/
  391. struct sih_agent {
  392. int irq_base;
  393. const struct sih *sih;
  394. u32 imr;
  395. bool imr_change_pending;
  396. u32 edge_change;
  397. struct mutex irq_lock;
  398. char *irq_name;
  399. };
  400. /*----------------------------------------------------------------------*/
  401. /*
  402. * All irq_chip methods get issued from code holding irq_desc[irq].lock,
  403. * which can't perform the underlying I2C operations (because they sleep).
  404. * So we must hand them off to a thread (workqueue) and cope with asynch
  405. * completion, potentially including some re-ordering, of these requests.
  406. */
  407. static void twl4030_sih_mask(struct irq_data *data)
  408. {
  409. struct sih_agent *agent = irq_data_get_irq_chip_data(data);
  410. agent->imr |= BIT(data->irq - agent->irq_base);
  411. agent->imr_change_pending = true;
  412. }
  413. static void twl4030_sih_unmask(struct irq_data *data)
  414. {
  415. struct sih_agent *agent = irq_data_get_irq_chip_data(data);
  416. agent->imr &= ~BIT(data->irq - agent->irq_base);
  417. agent->imr_change_pending = true;
  418. }
  419. static int twl4030_sih_set_type(struct irq_data *data, unsigned trigger)
  420. {
  421. struct sih_agent *agent = irq_data_get_irq_chip_data(data);
  422. if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  423. return -EINVAL;
  424. if (irqd_get_trigger_type(data) != trigger)
  425. agent->edge_change |= BIT(data->irq - agent->irq_base);
  426. return 0;
  427. }
  428. static void twl4030_sih_bus_lock(struct irq_data *data)
  429. {
  430. struct sih_agent *agent = irq_data_get_irq_chip_data(data);
  431. mutex_lock(&agent->irq_lock);
  432. }
  433. static void twl4030_sih_bus_sync_unlock(struct irq_data *data)
  434. {
  435. struct sih_agent *agent = irq_data_get_irq_chip_data(data);
  436. const struct sih *sih = agent->sih;
  437. int status;
  438. if (agent->imr_change_pending) {
  439. union {
  440. u32 word;
  441. u8 bytes[4];
  442. } imr;
  443. /* byte[0] gets overwritten as we write ... */
  444. imr.word = cpu_to_le32(agent->imr << 8);
  445. agent->imr_change_pending = false;
  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,
  449. sih->bytes_ixr);
  450. if (status)
  451. pr_err("twl4030: %s, %s --> %d\n", __func__,
  452. "write", status);
  453. }
  454. if (agent->edge_change) {
  455. u32 edge_change;
  456. u8 bytes[6];
  457. edge_change = agent->edge_change;
  458. agent->edge_change = 0;
  459. /*
  460. * Read, reserving first byte for write scratch. Yes, this
  461. * could be cached for some speedup ... but be careful about
  462. * any processor on the other IRQ line, EDR registers are
  463. * shared.
  464. */
  465. status = twl_i2c_read(sih->module, bytes + 1,
  466. sih->edr_offset, sih->bytes_edr);
  467. if (status) {
  468. pr_err("twl4030: %s, %s --> %d\n", __func__,
  469. "read", status);
  470. return;
  471. }
  472. /* Modify only the bits we know must change */
  473. while (edge_change) {
  474. int i = fls(edge_change) - 1;
  475. struct irq_data *idata;
  476. int byte = 1 + (i >> 2);
  477. int off = (i & 0x3) * 2;
  478. unsigned int type;
  479. idata = irq_get_irq_data(i + agent->irq_base);
  480. bytes[byte] &= ~(0x03 << off);
  481. type = irqd_get_trigger_type(idata);
  482. if (type & IRQ_TYPE_EDGE_RISING)
  483. bytes[byte] |= BIT(off + 1);
  484. if (type & IRQ_TYPE_EDGE_FALLING)
  485. bytes[byte] |= BIT(off + 0);
  486. edge_change &= ~BIT(i);
  487. }
  488. /* Write */
  489. status = twl_i2c_write(sih->module, bytes,
  490. sih->edr_offset, sih->bytes_edr);
  491. if (status)
  492. pr_err("twl4030: %s, %s --> %d\n", __func__,
  493. "write", status);
  494. }
  495. mutex_unlock(&agent->irq_lock);
  496. }
  497. static struct irq_chip twl4030_sih_irq_chip = {
  498. .name = "twl4030",
  499. .irq_mask = twl4030_sih_mask,
  500. .irq_unmask = twl4030_sih_unmask,
  501. .irq_set_type = twl4030_sih_set_type,
  502. .irq_bus_lock = twl4030_sih_bus_lock,
  503. .irq_bus_sync_unlock = twl4030_sih_bus_sync_unlock,
  504. };
  505. /*----------------------------------------------------------------------*/
  506. static inline int sih_read_isr(const struct sih *sih)
  507. {
  508. int status;
  509. union {
  510. u8 bytes[4];
  511. u32 word;
  512. } isr;
  513. /* FIXME need retry-on-error ... */
  514. isr.word = 0;
  515. status = twl_i2c_read(sih->module, isr.bytes,
  516. sih->mask[irq_line].isr_offset, sih->bytes_ixr);
  517. return (status < 0) ? status : le32_to_cpu(isr.word);
  518. }
  519. /*
  520. * Generic handler for SIH interrupts ... we "know" this is called
  521. * in task context, with IRQs enabled.
  522. */
  523. static irqreturn_t handle_twl4030_sih(int irq, void *data)
  524. {
  525. struct sih_agent *agent = irq_get_handler_data(irq);
  526. const struct sih *sih = agent->sih;
  527. int isr;
  528. /* reading ISR acks the IRQs, using clear-on-read mode */
  529. isr = sih_read_isr(sih);
  530. if (isr < 0) {
  531. pr_err("twl4030: %s SIH, read ISR error %d\n",
  532. sih->name, isr);
  533. /* REVISIT: recover; eventually mask it all, etc */
  534. return IRQ_HANDLED;
  535. }
  536. while (isr) {
  537. irq = fls(isr);
  538. irq--;
  539. isr &= ~BIT(irq);
  540. if (irq < sih->bits)
  541. handle_nested_irq(agent->irq_base + irq);
  542. else
  543. pr_err("twl4030: %s SIH, invalid ISR bit %d\n",
  544. sih->name, irq);
  545. }
  546. return IRQ_HANDLED;
  547. }
  548. /* returns the first IRQ used by this SIH bank, or negative errno */
  549. int twl4030_sih_setup(struct device *dev, int module, int irq_base)
  550. {
  551. int sih_mod;
  552. const struct sih *sih = NULL;
  553. struct sih_agent *agent;
  554. int i, irq;
  555. int status = -EINVAL;
  556. /* only support modules with standard clear-on-read for now */
  557. for (sih_mod = 0, sih = sih_modules; sih_mod < nr_sih_modules;
  558. sih_mod++, sih++) {
  559. if (sih->module == module && sih->set_cor) {
  560. status = 0;
  561. break;
  562. }
  563. }
  564. if (status < 0)
  565. return status;
  566. agent = kzalloc(sizeof *agent, GFP_KERNEL);
  567. if (!agent)
  568. return -ENOMEM;
  569. agent->irq_base = irq_base;
  570. agent->sih = sih;
  571. agent->imr = ~0;
  572. mutex_init(&agent->irq_lock);
  573. for (i = 0; i < sih->bits; i++) {
  574. irq = irq_base + i;
  575. irq_set_chip_data(irq, agent);
  576. irq_set_chip_and_handler(irq, &twl4030_sih_irq_chip,
  577. handle_edge_irq);
  578. irq_set_nested_thread(irq, 1);
  579. activate_irq(irq);
  580. }
  581. /* replace generic PIH handler (handle_simple_irq) */
  582. irq = sih_mod + twl4030_irq_base;
  583. irq_set_handler_data(irq, agent);
  584. agent->irq_name = kasprintf(GFP_KERNEL, "twl4030_%s", sih->name);
  585. status = request_threaded_irq(irq, NULL, handle_twl4030_sih, 0,
  586. agent->irq_name ?: sih->name, NULL);
  587. dev_info(dev, "%s (irq %d) chaining IRQs %d..%d\n", sih->name,
  588. irq, irq_base, irq_base + i - 1);
  589. return status < 0 ? status : irq_base;
  590. }
  591. /* FIXME need a call to reverse twl4030_sih_setup() ... */
  592. /*----------------------------------------------------------------------*/
  593. /* FIXME pass in which interrupt line we'll use ... */
  594. #define twl_irq_line 0
  595. int twl4030_init_irq(struct device *dev, int irq_num)
  596. {
  597. static struct irq_chip twl4030_irq_chip;
  598. int status, i;
  599. int irq_base, irq_end, nr_irqs;
  600. struct device_node *node = dev->of_node;
  601. /*
  602. * TWL core and pwr interrupts must be contiguous because
  603. * the hwirqs numbers are defined contiguously from 1 to 15.
  604. * Create only one domain for both.
  605. */
  606. nr_irqs = TWL4030_PWR_NR_IRQS + TWL4030_CORE_NR_IRQS;
  607. irq_base = irq_alloc_descs(-1, 0, nr_irqs, 0);
  608. if (IS_ERR_VALUE(irq_base)) {
  609. dev_err(dev, "Fail to allocate IRQ descs\n");
  610. return irq_base;
  611. }
  612. irq_domain_add_legacy(node, nr_irqs, irq_base, 0,
  613. &irq_domain_simple_ops, NULL);
  614. irq_end = irq_base + TWL4030_CORE_NR_IRQS;
  615. /*
  616. * Mask and clear all TWL4030 interrupts since initially we do
  617. * not have any TWL4030 module interrupt handlers present
  618. */
  619. status = twl4030_init_sih_modules(twl_irq_line);
  620. if (status < 0)
  621. return status;
  622. twl4030_irq_base = irq_base;
  623. /*
  624. * Install an irq handler for each of the SIH modules;
  625. * clone dummy irq_chip since PIH can't *do* anything
  626. */
  627. twl4030_irq_chip = dummy_irq_chip;
  628. twl4030_irq_chip.name = "twl4030";
  629. twl4030_sih_irq_chip.irq_ack = dummy_irq_chip.irq_ack;
  630. for (i = irq_base; i < irq_end; i++) {
  631. irq_set_chip_and_handler(i, &twl4030_irq_chip,
  632. handle_simple_irq);
  633. irq_set_nested_thread(i, 1);
  634. activate_irq(i);
  635. }
  636. dev_info(dev, "%s (irq %d) chaining IRQs %d..%d\n", "PIH",
  637. irq_num, irq_base, irq_end);
  638. /* ... and the PWR_INT module ... */
  639. status = twl4030_sih_setup(dev, TWL4030_MODULE_INT, irq_end);
  640. if (status < 0) {
  641. dev_err(dev, "sih_setup PWR INT --> %d\n", status);
  642. goto fail;
  643. }
  644. /* install an irq handler to demultiplex the TWL4030 interrupt */
  645. status = request_threaded_irq(irq_num, NULL, handle_twl4030_pih,
  646. IRQF_ONESHOT,
  647. "TWL4030-PIH", NULL);
  648. if (status < 0) {
  649. dev_err(dev, "could not claim irq%d: %d\n", irq_num, status);
  650. goto fail_rqirq;
  651. }
  652. return irq_base;
  653. fail_rqirq:
  654. /* clean up twl4030_sih_setup */
  655. fail:
  656. for (i = irq_base; i < irq_end; i++) {
  657. irq_set_nested_thread(i, 0);
  658. irq_set_chip_and_handler(i, NULL, NULL);
  659. }
  660. return status;
  661. }
  662. int twl4030_exit_irq(void)
  663. {
  664. /* FIXME undo twl_init_irq() */
  665. if (twl4030_irq_base) {
  666. pr_err("twl4030: can't yet clean up IRQs?\n");
  667. return -ENOSYS;
  668. }
  669. return 0;
  670. }
  671. int twl4030_init_chip_irq(const char *chip)
  672. {
  673. if (!strcmp(chip, "twl5031")) {
  674. sih_modules = sih_modules_twl5031;
  675. nr_sih_modules = ARRAY_SIZE(sih_modules_twl5031);
  676. } else {
  677. sih_modules = sih_modules_twl4030;
  678. nr_sih_modules = ARRAY_SIZE(sih_modules_twl4030);
  679. }
  680. return 0;
  681. }