fsl_mpic_timer_wakeup.c 3.4 KB

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