pm8xxx-irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define pr_fmt(fmt) "%s: " fmt, __func__
  14. #include <linux/export.h>
  15. #include <linux/err.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mfd/pm8xxx/core.h>
  20. #include <linux/mfd/pm8xxx/irq.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. /* PMIC8xxx IRQ */
  24. #define SSBI_REG_ADDR_IRQ_ROOT(base) (base + 0)
  25. #define SSBI_REG_ADDR_IRQ_M_STATUS1(base) (base + 1)
  26. #define SSBI_REG_ADDR_IRQ_M_STATUS2(base) (base + 2)
  27. #define SSBI_REG_ADDR_IRQ_M_STATUS3(base) (base + 3)
  28. #define SSBI_REG_ADDR_IRQ_M_STATUS4(base) (base + 4)
  29. #define SSBI_REG_ADDR_IRQ_BLK_SEL(base) (base + 5)
  30. #define SSBI_REG_ADDR_IRQ_IT_STATUS(base) (base + 6)
  31. #define SSBI_REG_ADDR_IRQ_CONFIG(base) (base + 7)
  32. #define SSBI_REG_ADDR_IRQ_RT_STATUS(base) (base + 8)
  33. #define PM_IRQF_LVL_SEL 0x01 /* level select */
  34. #define PM_IRQF_MASK_FE 0x02 /* mask falling edge */
  35. #define PM_IRQF_MASK_RE 0x04 /* mask rising edge */
  36. #define PM_IRQF_CLR 0x08 /* clear interrupt */
  37. #define PM_IRQF_BITS_MASK 0x70
  38. #define PM_IRQF_BITS_SHIFT 4
  39. #define PM_IRQF_WRITE 0x80
  40. #define PM_IRQF_MASK_ALL (PM_IRQF_MASK_FE | \
  41. PM_IRQF_MASK_RE)
  42. struct pm_irq_chip {
  43. struct device *dev;
  44. spinlock_t pm_irq_lock;
  45. unsigned int base_addr;
  46. unsigned int devirq;
  47. unsigned int irq_base;
  48. unsigned int num_irqs;
  49. unsigned int num_blocks;
  50. unsigned int num_masters;
  51. u8 config[0];
  52. };
  53. static int pm8xxx_read_root_irq(const struct pm_irq_chip *chip, u8 *rp)
  54. {
  55. return pm8xxx_readb(chip->dev,
  56. SSBI_REG_ADDR_IRQ_ROOT(chip->base_addr), rp);
  57. }
  58. static int pm8xxx_read_master_irq(const struct pm_irq_chip *chip, u8 m, u8 *bp)
  59. {
  60. return pm8xxx_readb(chip->dev,
  61. SSBI_REG_ADDR_IRQ_M_STATUS1(chip->base_addr) + m, bp);
  62. }
  63. static int pm8xxx_read_block_irq(struct pm_irq_chip *chip, u8 bp, u8 *ip)
  64. {
  65. int rc;
  66. spin_lock(&chip->pm_irq_lock);
  67. rc = pm8xxx_writeb(chip->dev,
  68. SSBI_REG_ADDR_IRQ_BLK_SEL(chip->base_addr), bp);
  69. if (rc) {
  70. pr_err("Failed Selecting Block %d rc=%d\n", bp, rc);
  71. goto bail;
  72. }
  73. rc = pm8xxx_readb(chip->dev,
  74. SSBI_REG_ADDR_IRQ_IT_STATUS(chip->base_addr), ip);
  75. if (rc)
  76. pr_err("Failed Reading Status rc=%d\n", rc);
  77. bail:
  78. spin_unlock(&chip->pm_irq_lock);
  79. return rc;
  80. }
  81. static int pm8xxx_read_config_irq(struct pm_irq_chip *chip, u8 bp, u8 cp, u8 *r)
  82. {
  83. int rc;
  84. spin_lock(&chip->pm_irq_lock);
  85. rc = pm8xxx_writeb(chip->dev,
  86. SSBI_REG_ADDR_IRQ_BLK_SEL(chip->base_addr), bp);
  87. if (rc) {
  88. pr_err("Failed Selecting Block %d rc=%d\n", bp, rc);
  89. goto bail;
  90. }
  91. cp &= ~PM_IRQF_WRITE;
  92. rc = pm8xxx_writeb(chip->dev,
  93. SSBI_REG_ADDR_IRQ_CONFIG(chip->base_addr), cp);
  94. if (rc)
  95. pr_err("Failed Configuring IRQ rc=%d\n", rc);
  96. rc = pm8xxx_readb(chip->dev,
  97. SSBI_REG_ADDR_IRQ_CONFIG(chip->base_addr), r);
  98. if (rc)
  99. pr_err("Failed reading IRQ rc=%d\n", rc);
  100. bail:
  101. spin_unlock(&chip->pm_irq_lock);
  102. return rc;
  103. }
  104. static int pm8xxx_write_config_irq(struct pm_irq_chip *chip, u8 bp, u8 cp)
  105. {
  106. int rc;
  107. spin_lock(&chip->pm_irq_lock);
  108. rc = pm8xxx_writeb(chip->dev,
  109. SSBI_REG_ADDR_IRQ_BLK_SEL(chip->base_addr), bp);
  110. if (rc) {
  111. pr_err("Failed Selecting Block %d rc=%d\n", bp, rc);
  112. goto bail;
  113. }
  114. /*
  115. * Set the write bit here as this could be a unrequested irq
  116. * whose PM_IRQF_WRITE bit is not set
  117. */
  118. cp |= PM_IRQF_WRITE;
  119. rc = pm8xxx_writeb(chip->dev,
  120. SSBI_REG_ADDR_IRQ_CONFIG(chip->base_addr), cp);
  121. if (rc)
  122. pr_err("Failed Configuring IRQ rc=%d\n", rc);
  123. bail:
  124. spin_unlock(&chip->pm_irq_lock);
  125. return rc;
  126. }
  127. static int pm8xxx_irq_block_handler(struct pm_irq_chip *chip, int block)
  128. {
  129. int pmirq, irq, i, ret = 0;
  130. u8 bits;
  131. ret = pm8xxx_read_block_irq(chip, block, &bits);
  132. if (ret) {
  133. pr_err("Failed reading %d block ret=%d", block, ret);
  134. return ret;
  135. }
  136. if (!bits) {
  137. pr_err("block bit set in master but no irqs: %d", block);
  138. return 0;
  139. }
  140. /* Check IRQ bits */
  141. for (i = 0; i < 8; i++) {
  142. if (bits & (1 << i)) {
  143. pmirq = block * 8 + i;
  144. irq = pmirq + chip->irq_base;
  145. generic_handle_irq(irq);
  146. }
  147. }
  148. return 0;
  149. }
  150. static int pm8xxx_irq_master_handler(struct pm_irq_chip *chip, int master)
  151. {
  152. u8 blockbits;
  153. int block_number, i, ret = 0;
  154. ret = pm8xxx_read_master_irq(chip, master, &blockbits);
  155. if (ret) {
  156. pr_err("Failed to read master %d ret=%d\n", master, ret);
  157. return ret;
  158. }
  159. if (!blockbits) {
  160. pr_err("master bit set in root but no blocks: %d", master);
  161. return 0;
  162. }
  163. for (i = 0; i < 8; i++)
  164. if (blockbits & (1 << i)) {
  165. block_number = master * 8 + i; /* block # */
  166. ret |= pm8xxx_irq_block_handler(chip, block_number);
  167. }
  168. return ret;
  169. }
  170. static irqreturn_t pm8xxx_irq_handler(int irq, void *data)
  171. {
  172. struct pm_irq_chip *chip = data;
  173. u8 root;
  174. int i, ret, masters = 0;
  175. ret = pm8xxx_read_root_irq(chip, &root);
  176. if (ret) {
  177. pr_err("Can't read root status ret=%d\n", ret);
  178. return IRQ_HANDLED;
  179. }
  180. /* on pm8xxx series masters start from bit 1 of the root */
  181. masters = root >> 1;
  182. /* Read allowed masters for blocks. */
  183. for (i = 0; i < chip->num_masters; i++)
  184. if (masters & (1 << i))
  185. pm8xxx_irq_master_handler(chip, i);
  186. return IRQ_HANDLED;
  187. }
  188. static void pm8xxx_irq_mask(struct irq_data *d)
  189. {
  190. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  191. unsigned int pmirq = d->irq - chip->irq_base;
  192. int master, irq_bit;
  193. u8 block, config;
  194. block = pmirq / 8;
  195. master = block / 8;
  196. irq_bit = pmirq % 8;
  197. if (chip->config[pmirq] == 0) {
  198. pr_warn("masking rogue irq=%d pmirq=%d\n", d->irq, pmirq);
  199. chip->config[pmirq] = irq_bit << PM_IRQF_BITS_SHIFT;
  200. }
  201. config = chip->config[pmirq] | PM_IRQF_MASK_ALL;
  202. pm8xxx_write_config_irq(chip, block, config);
  203. }
  204. static void pm8xxx_irq_mask_ack(struct irq_data *d)
  205. {
  206. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  207. unsigned int pmirq = d->irq - chip->irq_base;
  208. int master, irq_bit;
  209. u8 block, config;
  210. block = pmirq / 8;
  211. master = block / 8;
  212. irq_bit = pmirq % 8;
  213. if (chip->config[pmirq] == 0) {
  214. pr_warn("mask acking rogue irq=%d pmirq=%d\n", d->irq, pmirq);
  215. chip->config[pmirq] = irq_bit << PM_IRQF_BITS_SHIFT;
  216. }
  217. config = chip->config[pmirq] | PM_IRQF_MASK_ALL | PM_IRQF_CLR;
  218. pm8xxx_write_config_irq(chip, block, config);
  219. }
  220. static void pm8xxx_irq_unmask(struct irq_data *d)
  221. {
  222. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  223. unsigned int pmirq = d->irq - chip->irq_base;
  224. int master, irq_bit;
  225. u8 block, config, hw_conf;
  226. block = pmirq / 8;
  227. master = block / 8;
  228. irq_bit = pmirq % 8;
  229. config = chip->config[pmirq];
  230. pm8xxx_read_config_irq(chip, block, config, &hw_conf);
  231. /* check if it is masked */
  232. if ((hw_conf & PM_IRQF_MASK_ALL) == PM_IRQF_MASK_ALL)
  233. pm8xxx_write_config_irq(chip, block, config);
  234. }
  235. static int pm8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
  236. {
  237. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  238. unsigned int pmirq = d->irq - chip->irq_base;
  239. int master, irq_bit;
  240. u8 block, config;
  241. block = pmirq / 8;
  242. master = block / 8;
  243. irq_bit = pmirq % 8;
  244. chip->config[pmirq] = (irq_bit << PM_IRQF_BITS_SHIFT)
  245. | PM_IRQF_MASK_ALL;
  246. if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
  247. if (flow_type & IRQF_TRIGGER_RISING)
  248. chip->config[pmirq] &= ~PM_IRQF_MASK_RE;
  249. if (flow_type & IRQF_TRIGGER_FALLING)
  250. chip->config[pmirq] &= ~PM_IRQF_MASK_FE;
  251. } else {
  252. chip->config[pmirq] |= PM_IRQF_LVL_SEL;
  253. if (flow_type & IRQF_TRIGGER_HIGH)
  254. chip->config[pmirq] &= ~PM_IRQF_MASK_RE;
  255. else
  256. chip->config[pmirq] &= ~PM_IRQF_MASK_FE;
  257. }
  258. /*
  259. * The PM_IRQF_WRITE flag serves as an indication that this interrupt
  260. * been requested
  261. */
  262. chip->config[pmirq] |= PM_IRQF_WRITE;
  263. config = chip->config[pmirq] | PM_IRQF_CLR;
  264. return pm8xxx_write_config_irq(chip, block, config);
  265. }
  266. static int pm8xxx_irq_set_wake(struct irq_data *d, unsigned int on)
  267. {
  268. return 0;
  269. }
  270. static int pm8xxx_irq_read_line(struct irq_data *d)
  271. {
  272. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  273. return pm8xxx_get_irq_stat(chip, d->irq);
  274. }
  275. static struct irq_chip pm8xxx_irq_chip = {
  276. .name = "pm8xxx",
  277. .irq_mask = pm8xxx_irq_mask,
  278. .irq_mask_ack = pm8xxx_irq_mask_ack,
  279. .irq_unmask = pm8xxx_irq_unmask,
  280. .irq_set_type = pm8xxx_irq_set_type,
  281. .irq_set_wake = pm8xxx_irq_set_wake,
  282. .irq_read_line = pm8xxx_irq_read_line,
  283. .flags = IRQCHIP_MASK_ON_SUSPEND,
  284. };
  285. /**
  286. * pm8xxx_get_irq_stat - get the status of the irq line
  287. * @chip: pointer to identify a pmic irq controller
  288. * @irq: the irq number
  289. *
  290. * The pm8xxx gpio and mpp rely on the interrupt block to read
  291. * the values on their pins. This function is to facilitate reading
  292. * the status of a gpio or an mpp line. The caller has to convert the
  293. * gpio number to irq number.
  294. *
  295. * RETURNS:
  296. * an int indicating the value read on that line
  297. */
  298. int pm8xxx_get_irq_stat(struct pm_irq_chip *chip, int irq)
  299. {
  300. int pmirq, rc;
  301. u8 block, bits, bit;
  302. unsigned long flags;
  303. if (chip == NULL || irq < chip->irq_base ||
  304. irq >= chip->irq_base + chip->num_irqs)
  305. return -EINVAL;
  306. pmirq = irq - chip->irq_base;
  307. block = pmirq / 8;
  308. bit = pmirq % 8;
  309. spin_lock_irqsave(&chip->pm_irq_lock, flags);
  310. rc = pm8xxx_writeb(chip->dev,
  311. SSBI_REG_ADDR_IRQ_BLK_SEL(chip->base_addr), block);
  312. if (rc) {
  313. pr_err("Failed Selecting block irq=%d pmirq=%d blk=%d rc=%d\n",
  314. irq, pmirq, block, rc);
  315. goto bail_out;
  316. }
  317. rc = pm8xxx_readb(chip->dev,
  318. SSBI_REG_ADDR_IRQ_RT_STATUS(chip->base_addr), &bits);
  319. if (rc) {
  320. pr_err("Failed Configuring irq=%d pmirq=%d blk=%d rc=%d\n",
  321. irq, pmirq, block, rc);
  322. goto bail_out;
  323. }
  324. rc = (bits & (1 << bit)) ? 1 : 0;
  325. bail_out:
  326. spin_unlock_irqrestore(&chip->pm_irq_lock, flags);
  327. return rc;
  328. }
  329. EXPORT_SYMBOL_GPL(pm8xxx_get_irq_stat);
  330. struct pm_irq_chip * __devinit pm8xxx_irq_init(struct device *dev,
  331. const struct pm8xxx_irq_platform_data *pdata)
  332. {
  333. struct pm_irq_chip *chip;
  334. int devirq, rc;
  335. unsigned int pmirq;
  336. if (!pdata) {
  337. pr_err("No platform data\n");
  338. return ERR_PTR(-EINVAL);
  339. }
  340. devirq = pdata->devirq;
  341. if (devirq < 0) {
  342. pr_err("missing devirq\n");
  343. rc = devirq;
  344. return ERR_PTR(-EINVAL);
  345. }
  346. chip = kzalloc(sizeof(struct pm_irq_chip)
  347. + sizeof(u8) * pdata->irq_cdata.nirqs, GFP_KERNEL);
  348. if (!chip) {
  349. pr_err("Cannot alloc pm_irq_chip struct\n");
  350. return ERR_PTR(-EINVAL);
  351. }
  352. chip->dev = dev;
  353. chip->devirq = devirq;
  354. chip->irq_base = pdata->irq_base;
  355. chip->num_irqs = pdata->irq_cdata.nirqs;
  356. chip->base_addr = pdata->irq_cdata.base_addr;
  357. chip->num_blocks = DIV_ROUND_UP(chip->num_irqs, 8);
  358. chip->num_masters = DIV_ROUND_UP(chip->num_blocks, 8);
  359. spin_lock_init(&chip->pm_irq_lock);
  360. for (pmirq = 0; pmirq < chip->num_irqs; pmirq++) {
  361. irq_set_chip_and_handler(chip->irq_base + pmirq,
  362. &pm8xxx_irq_chip,
  363. handle_level_irq);
  364. irq_set_chip_data(chip->irq_base + pmirq, chip);
  365. #ifdef CONFIG_ARM
  366. set_irq_flags(chip->irq_base + pmirq, IRQF_VALID);
  367. #else
  368. irq_set_noprobe(chip->irq_base + pmirq);
  369. #endif
  370. }
  371. if (devirq != 0) {
  372. rc = request_irq(devirq, pm8xxx_irq_handler,
  373. pdata->irq_trigger_flag,
  374. "pm8xxx_usr_irq", chip);
  375. if (rc) {
  376. pr_err("failed to request_irq for %d rc=%d\n",
  377. devirq, rc);
  378. } else {
  379. irq_set_irq_wake(devirq, 1);
  380. }
  381. }
  382. return chip;
  383. }
  384. int pm8xxx_irq_exit(struct pm_irq_chip *chip)
  385. {
  386. irq_set_chained_handler(chip->devirq, NULL);
  387. kfree(chip);
  388. return 0;
  389. }