pm8821-irq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Copyright (c) 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/pm8821-irq.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <mach/mpm.h>
  24. #define PM8821_TOTAL_IRQ_MASTERS 2
  25. #define PM8821_BLOCKS_PER_MASTER 7
  26. #define PM8821_IRQ_MASTER1_SET 0x01
  27. #define PM8821_IRQ_CLEAR_OFFSET 0x01
  28. #define PM8821_IRQ_RT_STATUS_OFFSET 0x0F
  29. #define PM8821_IRQ_MASK_REG_OFFSET 0x08
  30. #define SSBI_REG_ADDR_IRQ_MASTER0 0x30
  31. #define SSBI_REG_ADDR_IRQ_MASTER1 0xB0
  32. #define MPM_PIN_FOR_8821_IRQ 7
  33. #define SSBI_REG_ADDR_IRQ_IT_STATUS(master_base, block) (master_base + block)
  34. /*
  35. * Block 0 does not exist in PM8821 IRQ SSBI address space,
  36. * IRQ0 is assigned to bit0 of block1.
  37. */
  38. #define SSBI_REG_ADDR_IRQ_IT_CLEAR(master_base, block) \
  39. (master_base + PM8821_IRQ_CLEAR_OFFSET + block)
  40. #define SSBI_REG_ADDR_IRQ_RT_STATUS(master_base, block) \
  41. (master_base + PM8821_IRQ_RT_STATUS_OFFSET + block)
  42. #define SSBI_REG_ADDR_IRQ_MASK(master_base, block) \
  43. (master_base + PM8821_IRQ_MASK_REG_OFFSET + block)
  44. struct pm_irq_chip {
  45. struct device *dev;
  46. spinlock_t pm_irq_lock;
  47. unsigned int base_addr;
  48. unsigned int devirq;
  49. unsigned int irq_base;
  50. unsigned int num_irqs;
  51. int masters[PM8821_TOTAL_IRQ_MASTERS];
  52. };
  53. static int pm8821_irq_masked_write(struct pm_irq_chip *chip, u16 addr,
  54. u8 mask, u8 val)
  55. {
  56. int rc;
  57. u8 reg;
  58. rc = pm8xxx_readb(chip->dev, addr, &reg);
  59. if (rc) {
  60. pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
  61. return rc;
  62. }
  63. reg &= ~mask;
  64. reg |= val & mask;
  65. rc = pm8xxx_writeb(chip->dev, addr, reg);
  66. if (rc) {
  67. pr_err("write failed addr = %03X, rc = %d\n", addr, rc);
  68. return rc;
  69. }
  70. return 0;
  71. }
  72. static int pm8821_read_master_irq(const struct pm_irq_chip *chip,
  73. int m, u8 *master)
  74. {
  75. return pm8xxx_readb(chip->dev, chip->masters[m], master);
  76. }
  77. static int pm8821_read_block_irq(struct pm_irq_chip *chip, int master,
  78. u8 block, u8 *bits)
  79. {
  80. int rc;
  81. spin_lock(&chip->pm_irq_lock);
  82. rc = pm8xxx_readb(chip->dev,
  83. SSBI_REG_ADDR_IRQ_IT_STATUS(chip->masters[master], block), bits);
  84. if (rc)
  85. pr_err("Failed Reading Status rc=%d\n", rc);
  86. spin_unlock(&chip->pm_irq_lock);
  87. return rc;
  88. }
  89. static int pm8821_irq_block_handler(struct pm_irq_chip *chip,
  90. int master_number, int block)
  91. {
  92. int pmirq, irq, i, ret;
  93. u8 bits;
  94. ret = pm8821_read_block_irq(chip, master_number, block, &bits);
  95. if (ret) {
  96. pr_err("Failed reading %d block ret=%d", block, ret);
  97. return ret;
  98. }
  99. if (!bits) {
  100. pr_err("block bit set in master but no irqs: %d", block);
  101. return 0;
  102. }
  103. /* Convert block offset to global block number */
  104. block += (master_number * PM8821_BLOCKS_PER_MASTER) - 1;
  105. /* Check IRQ bits */
  106. for (i = 0; i < 8; i++) {
  107. if (bits & BIT(i)) {
  108. pmirq = (block << 3) + i;
  109. irq = pmirq + chip->irq_base;
  110. generic_handle_irq(irq);
  111. }
  112. }
  113. return 0;
  114. }
  115. static int pm8821_irq_read_master(struct pm_irq_chip *chip,
  116. int master_number, u8 master_val)
  117. {
  118. int ret = 0;
  119. int block;
  120. for (block = 1; block < 8; block++) {
  121. if (master_val & BIT(block)) {
  122. ret |= pm8821_irq_block_handler(chip,
  123. master_number, block);
  124. }
  125. }
  126. return ret;
  127. }
  128. static irqreturn_t pm8821_irq_handler(int irq, void *data)
  129. {
  130. struct pm_irq_chip *chip = data;
  131. int ret;
  132. u8 master;
  133. ret = pm8821_read_master_irq(chip, 0, &master);
  134. if (ret) {
  135. pr_err("Failed to read master 0 ret=%d\n", ret);
  136. return ret;
  137. }
  138. if (master & ~PM8821_IRQ_MASTER1_SET)
  139. pm8821_irq_read_master(chip, 0, master);
  140. if (!(master & PM8821_IRQ_MASTER1_SET))
  141. goto done;
  142. ret = pm8821_read_master_irq(chip, 1, &master);
  143. if (ret) {
  144. pr_err("Failed to read master 1 ret=%d\n", ret);
  145. return ret;
  146. }
  147. pm8821_irq_read_master(chip, 1, master);
  148. done:
  149. return IRQ_HANDLED;
  150. }
  151. static void pm8821_irq_mask(struct irq_data *d)
  152. {
  153. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  154. unsigned int pmirq = d->irq - chip->irq_base;
  155. int irq_bit, rc;
  156. u8 block, master;
  157. block = pmirq >> 3;
  158. master = block / PM8821_BLOCKS_PER_MASTER;
  159. irq_bit = pmirq % 8;
  160. block %= PM8821_BLOCKS_PER_MASTER;
  161. spin_lock(&chip->pm_irq_lock);
  162. rc = pm8821_irq_masked_write(chip,
  163. SSBI_REG_ADDR_IRQ_MASK(chip->masters[master], block),
  164. BIT(irq_bit), BIT(irq_bit));
  165. if (rc)
  166. pr_err("Failed to read/write mask IRQ:%d rc=%d\n", pmirq, rc);
  167. spin_unlock(&chip->pm_irq_lock);
  168. }
  169. static void pm8821_irq_mask_ack(struct irq_data *d)
  170. {
  171. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  172. unsigned int pmirq = d->irq - chip->irq_base;
  173. int irq_bit, rc;
  174. u8 block, master;
  175. block = pmirq >> 3;
  176. master = block / PM8821_BLOCKS_PER_MASTER;
  177. irq_bit = pmirq % 8;
  178. block %= PM8821_BLOCKS_PER_MASTER;
  179. spin_lock(&chip->pm_irq_lock);
  180. rc = pm8821_irq_masked_write(chip,
  181. SSBI_REG_ADDR_IRQ_MASK(chip->masters[master], block),
  182. BIT(irq_bit), BIT(irq_bit));
  183. if (rc) {
  184. pr_err("Failed to read/write mask IRQ:%d rc=%d\n", pmirq, rc);
  185. goto fail;
  186. }
  187. rc = pm8821_irq_masked_write(chip,
  188. SSBI_REG_ADDR_IRQ_IT_CLEAR(chip->masters[master], block),
  189. BIT(irq_bit), BIT(irq_bit));
  190. if (rc) {
  191. pr_err("Failed to read/write IT_CLEAR IRQ:%d rc=%d\n",
  192. pmirq, rc);
  193. }
  194. fail:
  195. spin_unlock(&chip->pm_irq_lock);
  196. }
  197. static void pm8821_irq_unmask(struct irq_data *d)
  198. {
  199. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  200. unsigned int pmirq = d->irq - chip->irq_base;
  201. int irq_bit, rc;
  202. u8 block, master;
  203. block = pmirq >> 3;
  204. master = block / PM8821_BLOCKS_PER_MASTER;
  205. irq_bit = pmirq % 8;
  206. block %= PM8821_BLOCKS_PER_MASTER;
  207. spin_lock(&chip->pm_irq_lock);
  208. rc = pm8821_irq_masked_write(chip,
  209. SSBI_REG_ADDR_IRQ_MASK(chip->masters[master], block),
  210. BIT(irq_bit), ~BIT(irq_bit));
  211. if (rc)
  212. pr_err("Failed to read/write unmask IRQ:%d rc=%d\n", pmirq, rc);
  213. spin_unlock(&chip->pm_irq_lock);
  214. }
  215. static int pm8821_irq_set_type(struct irq_data *d, unsigned int flow_type)
  216. {
  217. /*
  218. * PM8821 IRQ controller does not have explicit software support for
  219. * IRQ flow type.
  220. */
  221. return 0;
  222. }
  223. static int pm8821_irq_set_wake(struct irq_data *d, unsigned int on)
  224. {
  225. return 0;
  226. }
  227. static int pm8821_irq_read_line(struct irq_data *d)
  228. {
  229. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  230. return pm8821_get_irq_stat(chip, d->irq);
  231. }
  232. static struct irq_chip pm_irq_chip = {
  233. .name = "pm8821-irq",
  234. .irq_mask = pm8821_irq_mask,
  235. .irq_mask_ack = pm8821_irq_mask_ack,
  236. .irq_unmask = pm8821_irq_unmask,
  237. .irq_set_type = pm8821_irq_set_type,
  238. .irq_set_wake = pm8821_irq_set_wake,
  239. .irq_read_line = pm8821_irq_read_line,
  240. .flags = IRQCHIP_MASK_ON_SUSPEND,
  241. };
  242. /**
  243. * pm8821_get_irq_stat - get the status of the irq line
  244. * @chip: pointer to identify a pmic irq controller
  245. * @irq: the irq number
  246. *
  247. * The pm8821 gpio and mpp rely on the interrupt block to read
  248. * the values on their pins. This function is to facilitate reading
  249. * the status of a gpio or an mpp line. The caller has to convert the
  250. * gpio number to irq number.
  251. *
  252. * RETURNS:
  253. * an int indicating the value read on that line
  254. */
  255. int pm8821_get_irq_stat(struct pm_irq_chip *chip, int irq)
  256. {
  257. int pmirq, rc;
  258. u8 block, bits, bit, master;
  259. unsigned long flags;
  260. if (chip == NULL || irq < chip->irq_base
  261. || irq >= chip->irq_base + chip->num_irqs)
  262. return -EINVAL;
  263. pmirq = irq - chip->irq_base;
  264. block = pmirq >> 3;
  265. master = block / PM8821_BLOCKS_PER_MASTER;
  266. bit = pmirq % 8;
  267. block %= PM8821_BLOCKS_PER_MASTER;
  268. spin_lock_irqsave(&chip->pm_irq_lock, flags);
  269. rc = pm8xxx_readb(chip->dev,
  270. SSBI_REG_ADDR_IRQ_RT_STATUS(chip->masters[master], block),
  271. &bits);
  272. if (rc) {
  273. pr_err("Failed Configuring irq=%d pmirq=%d blk=%d rc=%d\n",
  274. irq, pmirq, block, rc);
  275. goto bail_out;
  276. }
  277. rc = (bits & BIT(bit)) ? 1 : 0;
  278. bail_out:
  279. spin_unlock_irqrestore(&chip->pm_irq_lock, flags);
  280. return rc;
  281. }
  282. EXPORT_SYMBOL_GPL(pm8821_get_irq_stat);
  283. struct pm_irq_chip * __devinit pm8821_irq_init(struct device *dev,
  284. const struct pm8xxx_irq_platform_data *pdata)
  285. {
  286. struct pm_irq_chip *chip;
  287. int devirq, rc, blocks, masters;
  288. unsigned int pmirq;
  289. if (!pdata) {
  290. pr_err("No platform data\n");
  291. return ERR_PTR(-EINVAL);
  292. }
  293. devirq = pdata->devirq;
  294. if (devirq < 0) {
  295. pr_err("missing devirq\n");
  296. rc = devirq;
  297. return ERR_PTR(-EINVAL);
  298. }
  299. chip = kzalloc(sizeof(struct pm_irq_chip)
  300. + sizeof(u8) * pdata->irq_cdata.nirqs, GFP_KERNEL);
  301. if (!chip) {
  302. pr_err("Cannot alloc pm_irq_chip struct\n");
  303. return ERR_PTR(-EINVAL);
  304. }
  305. chip->dev = dev;
  306. chip->devirq = devirq;
  307. chip->irq_base = pdata->irq_base;
  308. chip->num_irqs = pdata->irq_cdata.nirqs;
  309. chip->base_addr = pdata->irq_cdata.base_addr;
  310. blocks = DIV_ROUND_UP(pdata->irq_cdata.nirqs, 8);
  311. masters = DIV_ROUND_UP(blocks, PM8821_BLOCKS_PER_MASTER);
  312. chip->masters[0] = chip->base_addr + SSBI_REG_ADDR_IRQ_MASTER0;
  313. chip->masters[1] = chip->base_addr + SSBI_REG_ADDR_IRQ_MASTER1;
  314. if (masters != PM8821_TOTAL_IRQ_MASTERS) {
  315. pr_err("Unequal number of masters, passed: %d, "
  316. "should have been: %d\n", masters, PM8821_TOTAL_IRQ_MASTERS);
  317. kfree(chip);
  318. return ERR_PTR(-EINVAL);
  319. }
  320. spin_lock_init(&chip->pm_irq_lock);
  321. for (pmirq = 0; pmirq < chip->num_irqs; pmirq++) {
  322. irq_set_chip_and_handler(chip->irq_base + pmirq,
  323. &pm_irq_chip, handle_level_irq);
  324. irq_set_chip_data(chip->irq_base + pmirq, chip);
  325. #ifdef CONFIG_ARM
  326. set_irq_flags(chip->irq_base + pmirq, IRQF_VALID);
  327. #else
  328. irq_set_noprobe(chip->irq_base + pmirq);
  329. #endif
  330. }
  331. if (devirq != 0) {
  332. rc = request_irq(devirq, pm8821_irq_handler,
  333. pdata->irq_trigger_flag, "pm8821_sec_irq", chip);
  334. if (rc) {
  335. pr_err("failed to request_irq for %d rc=%d\n",
  336. devirq, rc);
  337. kfree(chip);
  338. return ERR_PTR(rc);
  339. } else{
  340. irq_set_irq_wake(devirq, 1);
  341. msm_mpm_set_pin_wake(MPM_PIN_FOR_8821_IRQ, 1);
  342. msm_mpm_set_pin_type(MPM_PIN_FOR_8821_IRQ,
  343. pdata->irq_trigger_flag);
  344. }
  345. }
  346. return chip;
  347. }
  348. int pm8821_irq_exit(struct pm_irq_chip *chip)
  349. {
  350. irq_set_chained_handler(chip->devirq, NULL);
  351. kfree(chip);
  352. return 0;
  353. }