fsl_mpic_timer_wakeup.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * MPIC timer wakeup driver
  3. *
  4. * Copyright 2013 Freescale Semiconductor, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/errno.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/device.h>
  17. #include <asm/mpic_timer.h>
  18. #include <asm/mpic.h>
  19. struct fsl_mpic_timer_wakeup {
  20. struct mpic_timer *timer;
  21. struct work_struct free_work;
  22. };
  23. static struct fsl_mpic_timer_wakeup *fsl_wakeup;
  24. static DEFINE_MUTEX(sysfs_lock);
  25. static void fsl_free_resource(struct work_struct *ws)
  26. {
  27. struct fsl_mpic_timer_wakeup *wakeup =
  28. container_of(ws, struct fsl_mpic_timer_wakeup, free_work);
  29. mutex_lock(&sysfs_lock);
  30. if (wakeup->timer) {
  31. disable_irq_wake(wakeup->timer->irq);
  32. mpic_free_timer(wakeup->timer);
  33. }
  34. wakeup->timer = NULL;
  35. mutex_unlock(&sysfs_lock);
  36. }
  37. static irqreturn_t fsl_mpic_timer_irq(int irq, void *dev_id)
  38. {
  39. struct fsl_mpic_timer_wakeup *wakeup = dev_id;
  40. schedule_work(&wakeup->free_work);
  41. return wakeup->timer ? IRQ_HANDLED : IRQ_NONE;
  42. }
  43. static ssize_t fsl_timer_wakeup_show(struct device *dev,
  44. struct device_attribute *attr,
  45. char *buf)
  46. {
  47. time64_t interval = 0;
  48. mutex_lock(&sysfs_lock);
  49. if (fsl_wakeup->timer) {
  50. mpic_get_remain_time(fsl_wakeup->timer, &interval);
  51. interval++;
  52. }
  53. mutex_unlock(&sysfs_lock);
  54. return sprintf(buf, "%lld\n", interval);
  55. }
  56. static ssize_t fsl_timer_wakeup_store(struct device *dev,
  57. struct device_attribute *attr,
  58. const char *buf,
  59. size_t count)
  60. {
  61. time64_t interval;
  62. int ret;
  63. if (kstrtoll(buf, 0, &interval))
  64. return -EINVAL;
  65. mutex_lock(&sysfs_lock);
  66. if (fsl_wakeup->timer) {
  67. disable_irq_wake(fsl_wakeup->timer->irq);
  68. mpic_free_timer(fsl_wakeup->timer);
  69. fsl_wakeup->timer = NULL;
  70. }
  71. if (!interval) {
  72. mutex_unlock(&sysfs_lock);
  73. return count;
  74. }
  75. fsl_wakeup->timer = mpic_request_timer(fsl_mpic_timer_irq,
  76. fsl_wakeup, interval);
  77. if (!fsl_wakeup->timer) {
  78. mutex_unlock(&sysfs_lock);
  79. return -EINVAL;
  80. }
  81. ret = enable_irq_wake(fsl_wakeup->timer->irq);
  82. if (ret) {
  83. mpic_free_timer(fsl_wakeup->timer);
  84. fsl_wakeup->timer = NULL;
  85. mutex_unlock(&sysfs_lock);
  86. return ret;
  87. }
  88. mpic_start_timer(fsl_wakeup->timer);
  89. mutex_unlock(&sysfs_lock);
  90. return count;
  91. }
  92. static struct device_attribute mpic_attributes = __ATTR(timer_wakeup, 0644,
  93. fsl_timer_wakeup_show, fsl_timer_wakeup_store);
  94. static int __init fsl_wakeup_sys_init(void)
  95. {
  96. int ret;
  97. fsl_wakeup = kzalloc(sizeof(struct fsl_mpic_timer_wakeup), GFP_KERNEL);
  98. if (!fsl_wakeup)
  99. return -ENOMEM;
  100. INIT_WORK(&fsl_wakeup->free_work, fsl_free_resource);
  101. ret = device_create_file(mpic_subsys.dev_root, &mpic_attributes);
  102. if (ret)
  103. kfree(fsl_wakeup);
  104. return ret;
  105. }
  106. static void __exit fsl_wakeup_sys_exit(void)
  107. {
  108. device_remove_file(mpic_subsys.dev_root, &mpic_attributes);
  109. mutex_lock(&sysfs_lock);
  110. if (fsl_wakeup->timer) {
  111. disable_irq_wake(fsl_wakeup->timer->irq);
  112. mpic_free_timer(fsl_wakeup->timer);
  113. }
  114. kfree(fsl_wakeup);
  115. mutex_unlock(&sysfs_lock);
  116. }
  117. module_init(fsl_wakeup_sys_init);
  118. module_exit(fsl_wakeup_sys_exit);
  119. MODULE_DESCRIPTION("Freescale MPIC global timer wakeup driver");
  120. MODULE_LICENSE("GPL v2");
  121. MODULE_AUTHOR("Wang Dongsheng <dongsheng.wang@freescale.com>");