pcf50633-irq.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* NXP PCF50633 Power Management Unit (PMU) driver
  2. *
  3. * (C) 2006-2008 by Openmoko, Inc.
  4. * Author: Harald Welte <laforge@openmoko.org>
  5. * Balaji Rao <balajirrao@openmoko.org>
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mutex.h>
  17. #include <linux/export.h>
  18. #include <linux/slab.h>
  19. #include <linux/mfd/pcf50633/core.h>
  20. #include <linux/mfd/pcf50633/mbc.h>
  21. int pcf50633_register_irq(struct pcf50633 *pcf, int irq,
  22. void (*handler) (int, void *), void *data)
  23. {
  24. if (irq < 0 || irq >= PCF50633_NUM_IRQ || !handler)
  25. return -EINVAL;
  26. if (WARN_ON(pcf->irq_handler[irq].handler))
  27. return -EBUSY;
  28. mutex_lock(&pcf->lock);
  29. pcf->irq_handler[irq].handler = handler;
  30. pcf->irq_handler[irq].data = data;
  31. mutex_unlock(&pcf->lock);
  32. return 0;
  33. }
  34. EXPORT_SYMBOL_GPL(pcf50633_register_irq);
  35. int pcf50633_free_irq(struct pcf50633 *pcf, int irq)
  36. {
  37. if (irq < 0 || irq >= PCF50633_NUM_IRQ)
  38. return -EINVAL;
  39. mutex_lock(&pcf->lock);
  40. pcf->irq_handler[irq].handler = NULL;
  41. mutex_unlock(&pcf->lock);
  42. return 0;
  43. }
  44. EXPORT_SYMBOL_GPL(pcf50633_free_irq);
  45. static int __pcf50633_irq_mask_set(struct pcf50633 *pcf, int irq, u8 mask)
  46. {
  47. u8 reg, bit;
  48. int idx;
  49. idx = irq >> 3;
  50. reg = PCF50633_REG_INT1M + idx;
  51. bit = 1 << (irq & 0x07);
  52. pcf50633_reg_set_bit_mask(pcf, reg, bit, mask ? bit : 0);
  53. mutex_lock(&pcf->lock);
  54. if (mask)
  55. pcf->mask_regs[idx] |= bit;
  56. else
  57. pcf->mask_regs[idx] &= ~bit;
  58. mutex_unlock(&pcf->lock);
  59. return 0;
  60. }
  61. int pcf50633_irq_mask(struct pcf50633 *pcf, int irq)
  62. {
  63. dev_dbg(pcf->dev, "Masking IRQ %d\n", irq);
  64. return __pcf50633_irq_mask_set(pcf, irq, 1);
  65. }
  66. EXPORT_SYMBOL_GPL(pcf50633_irq_mask);
  67. int pcf50633_irq_unmask(struct pcf50633 *pcf, int irq)
  68. {
  69. dev_dbg(pcf->dev, "Unmasking IRQ %d\n", irq);
  70. return __pcf50633_irq_mask_set(pcf, irq, 0);
  71. }
  72. EXPORT_SYMBOL_GPL(pcf50633_irq_unmask);
  73. int pcf50633_irq_mask_get(struct pcf50633 *pcf, int irq)
  74. {
  75. u8 reg, bits;
  76. reg = irq >> 3;
  77. bits = 1 << (irq & 0x07);
  78. return pcf->mask_regs[reg] & bits;
  79. }
  80. EXPORT_SYMBOL_GPL(pcf50633_irq_mask_get);
  81. static void pcf50633_irq_call_handler(struct pcf50633 *pcf, int irq)
  82. {
  83. if (pcf->irq_handler[irq].handler)
  84. pcf->irq_handler[irq].handler(irq, pcf->irq_handler[irq].data);
  85. }
  86. /* Maximum amount of time ONKEY is held before emergency action is taken */
  87. #define PCF50633_ONKEY1S_TIMEOUT 8
  88. static irqreturn_t pcf50633_irq(int irq, void *data)
  89. {
  90. struct pcf50633 *pcf = data;
  91. int ret, i, j;
  92. u8 pcf_int[5], chgstat;
  93. /* Read the 5 INT regs in one transaction */
  94. ret = pcf50633_read_block(pcf, PCF50633_REG_INT1,
  95. ARRAY_SIZE(pcf_int), pcf_int);
  96. if (ret != ARRAY_SIZE(pcf_int)) {
  97. dev_err(pcf->dev, "Error reading INT registers\n");
  98. /*
  99. * If this doesn't ACK the interrupt to the chip, we'll be
  100. * called once again as we're level triggered.
  101. */
  102. goto out;
  103. }
  104. /* defeat 8s death from lowsys on A5 */
  105. pcf50633_reg_write(pcf, PCF50633_REG_OOCSHDWN, 0x04);
  106. /* We immediately read the usb and adapter status. We thus make sure
  107. * only of USBINS/USBREM IRQ handlers are called */
  108. if (pcf_int[0] & (PCF50633_INT1_USBINS | PCF50633_INT1_USBREM)) {
  109. chgstat = pcf50633_reg_read(pcf, PCF50633_REG_MBCS2);
  110. if (chgstat & (0x3 << 4))
  111. pcf_int[0] &= ~PCF50633_INT1_USBREM;
  112. else
  113. pcf_int[0] &= ~PCF50633_INT1_USBINS;
  114. }
  115. /* Make sure only one of ADPINS or ADPREM is set */
  116. if (pcf_int[0] & (PCF50633_INT1_ADPINS | PCF50633_INT1_ADPREM)) {
  117. chgstat = pcf50633_reg_read(pcf, PCF50633_REG_MBCS2);
  118. if (chgstat & (0x3 << 4))
  119. pcf_int[0] &= ~PCF50633_INT1_ADPREM;
  120. else
  121. pcf_int[0] &= ~PCF50633_INT1_ADPINS;
  122. }
  123. dev_dbg(pcf->dev, "INT1=0x%02x INT2=0x%02x INT3=0x%02x "
  124. "INT4=0x%02x INT5=0x%02x\n", pcf_int[0],
  125. pcf_int[1], pcf_int[2], pcf_int[3], pcf_int[4]);
  126. /* Some revisions of the chip don't have a 8s standby mode on
  127. * ONKEY1S press. We try to manually do it in such cases. */
  128. if ((pcf_int[0] & PCF50633_INT1_SECOND) && pcf->onkey1s_held) {
  129. dev_info(pcf->dev, "ONKEY1S held for %d secs\n",
  130. pcf->onkey1s_held);
  131. if (pcf->onkey1s_held++ == PCF50633_ONKEY1S_TIMEOUT)
  132. if (pcf->pdata->force_shutdown)
  133. pcf->pdata->force_shutdown(pcf);
  134. }
  135. if (pcf_int[2] & PCF50633_INT3_ONKEY1S) {
  136. dev_info(pcf->dev, "ONKEY1S held\n");
  137. pcf->onkey1s_held = 1 ;
  138. /* Unmask IRQ_SECOND */
  139. pcf50633_reg_clear_bits(pcf, PCF50633_REG_INT1M,
  140. PCF50633_INT1_SECOND);
  141. /* Unmask IRQ_ONKEYR */
  142. pcf50633_reg_clear_bits(pcf, PCF50633_REG_INT2M,
  143. PCF50633_INT2_ONKEYR);
  144. }
  145. if ((pcf_int[1] & PCF50633_INT2_ONKEYR) && pcf->onkey1s_held) {
  146. pcf->onkey1s_held = 0;
  147. /* Mask SECOND and ONKEYR interrupts */
  148. if (pcf->mask_regs[0] & PCF50633_INT1_SECOND)
  149. pcf50633_reg_set_bit_mask(pcf,
  150. PCF50633_REG_INT1M,
  151. PCF50633_INT1_SECOND,
  152. PCF50633_INT1_SECOND);
  153. if (pcf->mask_regs[1] & PCF50633_INT2_ONKEYR)
  154. pcf50633_reg_set_bit_mask(pcf,
  155. PCF50633_REG_INT2M,
  156. PCF50633_INT2_ONKEYR,
  157. PCF50633_INT2_ONKEYR);
  158. }
  159. /* Have we just resumed ? */
  160. if (pcf->is_suspended) {
  161. pcf->is_suspended = 0;
  162. /* Set the resume reason filtering out non resumers */
  163. for (i = 0; i < ARRAY_SIZE(pcf_int); i++)
  164. pcf->resume_reason[i] = pcf_int[i] &
  165. pcf->pdata->resumers[i];
  166. /* Make sure we don't pass on any ONKEY events to
  167. * userspace now */
  168. pcf_int[1] &= ~(PCF50633_INT2_ONKEYR | PCF50633_INT2_ONKEYF);
  169. }
  170. for (i = 0; i < ARRAY_SIZE(pcf_int); i++) {
  171. /* Unset masked interrupts */
  172. pcf_int[i] &= ~pcf->mask_regs[i];
  173. for (j = 0; j < 8 ; j++)
  174. if (pcf_int[i] & (1 << j))
  175. pcf50633_irq_call_handler(pcf, (i * 8) + j);
  176. }
  177. out:
  178. return IRQ_HANDLED;
  179. }
  180. #ifdef CONFIG_PM
  181. int pcf50633_irq_suspend(struct pcf50633 *pcf)
  182. {
  183. int ret;
  184. int i;
  185. u8 res[5];
  186. /* Make sure our interrupt handlers are not called
  187. * henceforth */
  188. disable_irq(pcf->irq);
  189. /* Save the masks */
  190. ret = pcf50633_read_block(pcf, PCF50633_REG_INT1M,
  191. ARRAY_SIZE(pcf->suspend_irq_masks),
  192. pcf->suspend_irq_masks);
  193. if (ret < 0) {
  194. dev_err(pcf->dev, "error saving irq masks\n");
  195. goto out;
  196. }
  197. /* Write wakeup irq masks */
  198. for (i = 0; i < ARRAY_SIZE(res); i++)
  199. res[i] = ~pcf->pdata->resumers[i];
  200. ret = pcf50633_write_block(pcf, PCF50633_REG_INT1M,
  201. ARRAY_SIZE(res), &res[0]);
  202. if (ret < 0) {
  203. dev_err(pcf->dev, "error writing wakeup irq masks\n");
  204. goto out;
  205. }
  206. pcf->is_suspended = 1;
  207. out:
  208. return ret;
  209. }
  210. int pcf50633_irq_resume(struct pcf50633 *pcf)
  211. {
  212. int ret;
  213. /* Write the saved mask registers */
  214. ret = pcf50633_write_block(pcf, PCF50633_REG_INT1M,
  215. ARRAY_SIZE(pcf->suspend_irq_masks),
  216. pcf->suspend_irq_masks);
  217. if (ret < 0)
  218. dev_err(pcf->dev, "Error restoring saved suspend masks\n");
  219. enable_irq(pcf->irq);
  220. return ret;
  221. }
  222. #endif
  223. int pcf50633_irq_init(struct pcf50633 *pcf, int irq)
  224. {
  225. int ret;
  226. pcf->irq = irq;
  227. /* Enable all interrupts except RTC SECOND */
  228. pcf->mask_regs[0] = 0x80;
  229. pcf50633_reg_write(pcf, PCF50633_REG_INT1M, pcf->mask_regs[0]);
  230. pcf50633_reg_write(pcf, PCF50633_REG_INT2M, 0x00);
  231. pcf50633_reg_write(pcf, PCF50633_REG_INT3M, 0x00);
  232. pcf50633_reg_write(pcf, PCF50633_REG_INT4M, 0x00);
  233. pcf50633_reg_write(pcf, PCF50633_REG_INT5M, 0x00);
  234. ret = request_threaded_irq(irq, NULL, pcf50633_irq,
  235. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  236. "pcf50633", pcf);
  237. if (ret)
  238. dev_err(pcf->dev, "Failed to request IRQ %d\n", ret);
  239. if (enable_irq_wake(irq) < 0)
  240. dev_err(pcf->dev, "IRQ %u cannot be enabled as wake-up source"
  241. "in this hardware revision", irq);
  242. return ret;
  243. }
  244. void pcf50633_irq_free(struct pcf50633 *pcf)
  245. {
  246. free_irq(pcf->irq, pcf);
  247. }