pm8xxx-irq.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (c) 2011, Code Aurora Forum. 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/err.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mfd/pm8xxx/core.h>
  19. #include <linux/mfd/pm8xxx/irq.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. /* PMIC8xxx IRQ */
  23. #define SSBI_REG_ADDR_IRQ_BASE 0x1BB
  24. #define SSBI_REG_ADDR_IRQ_ROOT (SSBI_REG_ADDR_IRQ_BASE + 0)
  25. #define SSBI_REG_ADDR_IRQ_M_STATUS1 (SSBI_REG_ADDR_IRQ_BASE + 1)
  26. #define SSBI_REG_ADDR_IRQ_M_STATUS2 (SSBI_REG_ADDR_IRQ_BASE + 2)
  27. #define SSBI_REG_ADDR_IRQ_M_STATUS3 (SSBI_REG_ADDR_IRQ_BASE + 3)
  28. #define SSBI_REG_ADDR_IRQ_M_STATUS4 (SSBI_REG_ADDR_IRQ_BASE + 4)
  29. #define SSBI_REG_ADDR_IRQ_BLK_SEL (SSBI_REG_ADDR_IRQ_BASE + 5)
  30. #define SSBI_REG_ADDR_IRQ_IT_STATUS (SSBI_REG_ADDR_IRQ_BASE + 6)
  31. #define SSBI_REG_ADDR_IRQ_CONFIG (SSBI_REG_ADDR_IRQ_BASE + 7)
  32. #define SSBI_REG_ADDR_IRQ_RT_STATUS (SSBI_REG_ADDR_IRQ_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 devirq;
  46. unsigned int irq_base;
  47. unsigned int num_irqs;
  48. unsigned int num_blocks;
  49. unsigned int num_masters;
  50. u8 config[0];
  51. };
  52. static int pm8xxx_read_root_irq(const struct pm_irq_chip *chip, u8 *rp)
  53. {
  54. return pm8xxx_readb(chip->dev, SSBI_REG_ADDR_IRQ_ROOT, rp);
  55. }
  56. static int pm8xxx_read_master_irq(const struct pm_irq_chip *chip, u8 m, u8 *bp)
  57. {
  58. return pm8xxx_readb(chip->dev,
  59. SSBI_REG_ADDR_IRQ_M_STATUS1 + m, bp);
  60. }
  61. static int pm8xxx_read_block_irq(struct pm_irq_chip *chip, u8 bp, u8 *ip)
  62. {
  63. int rc;
  64. spin_lock(&chip->pm_irq_lock);
  65. rc = pm8xxx_writeb(chip->dev, SSBI_REG_ADDR_IRQ_BLK_SEL, bp);
  66. if (rc) {
  67. pr_err("Failed Selecting Block %d rc=%d\n", bp, rc);
  68. goto bail;
  69. }
  70. rc = pm8xxx_readb(chip->dev, SSBI_REG_ADDR_IRQ_IT_STATUS, ip);
  71. if (rc)
  72. pr_err("Failed Reading Status rc=%d\n", rc);
  73. bail:
  74. spin_unlock(&chip->pm_irq_lock);
  75. return rc;
  76. }
  77. static int pm8xxx_config_irq(struct pm_irq_chip *chip, u8 bp, u8 cp)
  78. {
  79. int rc;
  80. spin_lock(&chip->pm_irq_lock);
  81. rc = pm8xxx_writeb(chip->dev, SSBI_REG_ADDR_IRQ_BLK_SEL, bp);
  82. if (rc) {
  83. pr_err("Failed Selecting Block %d rc=%d\n", bp, rc);
  84. goto bail;
  85. }
  86. cp |= PM_IRQF_WRITE;
  87. rc = pm8xxx_writeb(chip->dev, SSBI_REG_ADDR_IRQ_CONFIG, cp);
  88. if (rc)
  89. pr_err("Failed Configuring IRQ rc=%d\n", rc);
  90. bail:
  91. spin_unlock(&chip->pm_irq_lock);
  92. return rc;
  93. }
  94. static int pm8xxx_irq_block_handler(struct pm_irq_chip *chip, int block)
  95. {
  96. int pmirq, irq, i, ret = 0;
  97. u8 bits;
  98. ret = pm8xxx_read_block_irq(chip, block, &bits);
  99. if (ret) {
  100. pr_err("Failed reading %d block ret=%d", block, ret);
  101. return ret;
  102. }
  103. if (!bits) {
  104. pr_err("block bit set in master but no irqs: %d", block);
  105. return 0;
  106. }
  107. /* Check IRQ bits */
  108. for (i = 0; i < 8; i++) {
  109. if (bits & (1 << i)) {
  110. pmirq = block * 8 + i;
  111. irq = pmirq + chip->irq_base;
  112. generic_handle_irq(irq);
  113. }
  114. }
  115. return 0;
  116. }
  117. static int pm8xxx_irq_master_handler(struct pm_irq_chip *chip, int master)
  118. {
  119. u8 blockbits;
  120. int block_number, i, ret = 0;
  121. ret = pm8xxx_read_master_irq(chip, master, &blockbits);
  122. if (ret) {
  123. pr_err("Failed to read master %d ret=%d\n", master, ret);
  124. return ret;
  125. }
  126. if (!blockbits) {
  127. pr_err("master bit set in root but no blocks: %d", master);
  128. return 0;
  129. }
  130. for (i = 0; i < 8; i++)
  131. if (blockbits & (1 << i)) {
  132. block_number = master * 8 + i; /* block # */
  133. ret |= pm8xxx_irq_block_handler(chip, block_number);
  134. }
  135. return ret;
  136. }
  137. static void pm8xxx_irq_handler(unsigned int irq, struct irq_desc *desc)
  138. {
  139. struct pm_irq_chip *chip = irq_desc_get_handler_data(desc);
  140. struct irq_chip *irq_chip = irq_desc_get_chip(desc);
  141. u8 root;
  142. int i, ret, masters = 0;
  143. ret = pm8xxx_read_root_irq(chip, &root);
  144. if (ret) {
  145. pr_err("Can't read root status ret=%d\n", ret);
  146. return;
  147. }
  148. /* on pm8xxx series masters start from bit 1 of the root */
  149. masters = root >> 1;
  150. /* Read allowed masters for blocks. */
  151. for (i = 0; i < chip->num_masters; i++)
  152. if (masters & (1 << i))
  153. pm8xxx_irq_master_handler(chip, i);
  154. irq_chip->irq_ack(&desc->irq_data);
  155. }
  156. static void pm8xxx_irq_mask_ack(struct irq_data *d)
  157. {
  158. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  159. unsigned int pmirq = d->irq - chip->irq_base;
  160. int master, irq_bit;
  161. u8 block, config;
  162. block = pmirq / 8;
  163. master = block / 8;
  164. irq_bit = pmirq % 8;
  165. config = chip->config[pmirq] | PM_IRQF_MASK_ALL | PM_IRQF_CLR;
  166. pm8xxx_config_irq(chip, block, config);
  167. }
  168. static void pm8xxx_irq_unmask(struct irq_data *d)
  169. {
  170. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  171. unsigned int pmirq = d->irq - chip->irq_base;
  172. int master, irq_bit;
  173. u8 block, config;
  174. block = pmirq / 8;
  175. master = block / 8;
  176. irq_bit = pmirq % 8;
  177. config = chip->config[pmirq];
  178. pm8xxx_config_irq(chip, block, config);
  179. }
  180. static int pm8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
  181. {
  182. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  183. unsigned int pmirq = d->irq - chip->irq_base;
  184. int master, irq_bit;
  185. u8 block, config;
  186. block = pmirq / 8;
  187. master = block / 8;
  188. irq_bit = pmirq % 8;
  189. chip->config[pmirq] = (irq_bit << PM_IRQF_BITS_SHIFT)
  190. | PM_IRQF_MASK_ALL;
  191. if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
  192. if (flow_type & IRQF_TRIGGER_RISING)
  193. chip->config[pmirq] &= ~PM_IRQF_MASK_RE;
  194. if (flow_type & IRQF_TRIGGER_FALLING)
  195. chip->config[pmirq] &= ~PM_IRQF_MASK_FE;
  196. } else {
  197. chip->config[pmirq] |= PM_IRQF_LVL_SEL;
  198. if (flow_type & IRQF_TRIGGER_HIGH)
  199. chip->config[pmirq] &= ~PM_IRQF_MASK_RE;
  200. else
  201. chip->config[pmirq] &= ~PM_IRQF_MASK_FE;
  202. }
  203. config = chip->config[pmirq] | PM_IRQF_CLR;
  204. return pm8xxx_config_irq(chip, block, config);
  205. }
  206. static int pm8xxx_irq_set_wake(struct irq_data *d, unsigned int on)
  207. {
  208. return 0;
  209. }
  210. static struct irq_chip pm8xxx_irq_chip = {
  211. .name = "pm8xxx",
  212. .irq_mask_ack = pm8xxx_irq_mask_ack,
  213. .irq_unmask = pm8xxx_irq_unmask,
  214. .irq_set_type = pm8xxx_irq_set_type,
  215. .irq_set_wake = pm8xxx_irq_set_wake,
  216. .flags = IRQCHIP_MASK_ON_SUSPEND,
  217. };
  218. /**
  219. * pm8xxx_get_irq_stat - get the status of the irq line
  220. * @chip: pointer to identify a pmic irq controller
  221. * @irq: the irq number
  222. *
  223. * The pm8xxx gpio and mpp rely on the interrupt block to read
  224. * the values on their pins. This function is to facilitate reading
  225. * the status of a gpio or an mpp line. The caller has to convert the
  226. * gpio number to irq number.
  227. *
  228. * RETURNS:
  229. * an int indicating the value read on that line
  230. */
  231. int pm8xxx_get_irq_stat(struct pm_irq_chip *chip, int irq)
  232. {
  233. int pmirq, rc;
  234. u8 block, bits, bit;
  235. unsigned long flags;
  236. if (chip == NULL || irq < chip->irq_base ||
  237. irq >= chip->irq_base + chip->num_irqs)
  238. return -EINVAL;
  239. pmirq = irq - chip->irq_base;
  240. block = pmirq / 8;
  241. bit = pmirq % 8;
  242. spin_lock_irqsave(&chip->pm_irq_lock, flags);
  243. rc = pm8xxx_writeb(chip->dev, SSBI_REG_ADDR_IRQ_BLK_SEL, block);
  244. if (rc) {
  245. pr_err("Failed Selecting block irq=%d pmirq=%d blk=%d rc=%d\n",
  246. irq, pmirq, block, rc);
  247. goto bail_out;
  248. }
  249. rc = pm8xxx_readb(chip->dev, SSBI_REG_ADDR_IRQ_RT_STATUS, &bits);
  250. if (rc) {
  251. pr_err("Failed Configuring irq=%d pmirq=%d blk=%d rc=%d\n",
  252. irq, pmirq, block, rc);
  253. goto bail_out;
  254. }
  255. rc = (bits & (1 << bit)) ? 1 : 0;
  256. bail_out:
  257. spin_unlock_irqrestore(&chip->pm_irq_lock, flags);
  258. return rc;
  259. }
  260. EXPORT_SYMBOL_GPL(pm8xxx_get_irq_stat);
  261. struct pm_irq_chip * __devinit pm8xxx_irq_init(struct device *dev,
  262. const struct pm8xxx_irq_platform_data *pdata)
  263. {
  264. struct pm_irq_chip *chip;
  265. int devirq, rc;
  266. unsigned int pmirq;
  267. if (!pdata) {
  268. pr_err("No platform data\n");
  269. return ERR_PTR(-EINVAL);
  270. }
  271. devirq = pdata->devirq;
  272. if (devirq < 0) {
  273. pr_err("missing devirq\n");
  274. rc = devirq;
  275. return ERR_PTR(-EINVAL);
  276. }
  277. chip = kzalloc(sizeof(struct pm_irq_chip)
  278. + sizeof(u8) * pdata->irq_cdata.nirqs, GFP_KERNEL);
  279. if (!chip) {
  280. pr_err("Cannot alloc pm_irq_chip struct\n");
  281. return ERR_PTR(-EINVAL);
  282. }
  283. chip->dev = dev;
  284. chip->devirq = devirq;
  285. chip->irq_base = pdata->irq_base;
  286. chip->num_irqs = pdata->irq_cdata.nirqs;
  287. chip->num_blocks = DIV_ROUND_UP(chip->num_irqs, 8);
  288. chip->num_masters = DIV_ROUND_UP(chip->num_blocks, 8);
  289. spin_lock_init(&chip->pm_irq_lock);
  290. for (pmirq = 0; pmirq < chip->num_irqs; pmirq++) {
  291. irq_set_chip_and_handler(chip->irq_base + pmirq,
  292. &pm8xxx_irq_chip,
  293. handle_level_irq);
  294. irq_set_chip_data(chip->irq_base + pmirq, chip);
  295. #ifdef CONFIG_ARM
  296. set_irq_flags(chip->irq_base + pmirq, IRQF_VALID);
  297. #else
  298. irq_set_noprobe(chip->irq_base + pmirq);
  299. #endif
  300. }
  301. irq_set_irq_type(devirq, pdata->irq_trigger_flag);
  302. irq_set_handler_data(devirq, chip);
  303. irq_set_chained_handler(devirq, pm8xxx_irq_handler);
  304. set_irq_wake(devirq, 1);
  305. return chip;
  306. }
  307. int __devexit pm8xxx_irq_exit(struct pm_irq_chip *chip)
  308. {
  309. irq_set_chained_handler(chip->devirq, NULL);
  310. kfree(chip);
  311. return 0;
  312. }