fsl_mpic_timer_wakeup.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. struct timeval interval;
  48. int val = 0;
  49. mutex_lock(&sysfs_lock);
  50. if (fsl_wakeup->timer) {
  51. mpic_get_remain_time(fsl_wakeup->timer, &interval);
  52. val = interval.tv_sec + 1;
  53. }
  54. mutex_unlock(&sysfs_lock);
  55. return sprintf(buf, "%d\n", val);
  56. }
  57. static ssize_t fsl_timer_wakeup_store(struct device *dev,
  58. struct device_attribute *attr,
  59. const char *buf,
  60. size_t count)
  61. {
  62. struct timeval interval;
  63. int ret;
  64. interval.tv_usec = 0;
  65. if (kstrtol(buf, 0, &interval.tv_sec))
  66. return -EINVAL;
  67. mutex_lock(&sysfs_lock);
  68. if (fsl_wakeup->timer) {
  69. disable_irq_wake(fsl_wakeup->timer->irq);
  70. mpic_free_timer(fsl_wakeup->timer);
  71. fsl_wakeup->timer = NULL;
  72. }
  73. if (!interval.tv_sec) {
  74. mutex_unlock(&sysfs_lock);
  75. return count;
  76. }
  77. fsl_wakeup->timer = mpic_request_timer(fsl_mpic_timer_irq,
  78. fsl_wakeup, &interval);
  79. if (!fsl_wakeup->timer) {
  80. mutex_unlock(&sysfs_lock);
  81. return -EINVAL;
  82. }
  83. ret = enable_irq_wake(fsl_wakeup->timer->irq);
  84. if (ret) {
  85. mpic_free_timer(fsl_wakeup->timer);
  86. fsl_wakeup->timer = NULL;
  87. mutex_unlock(&sysfs_lock);
  88. return ret;
  89. }
  90. mpic_start_timer(fsl_wakeup->timer);
  91. mutex_unlock(&sysfs_lock);
  92. return count;
  93. }
  94. static struct device_attribute mpic_attributes = __ATTR(timer_wakeup, 0644,
  95. fsl_timer_wakeup_show, fsl_timer_wakeup_store);
  96. static int __init fsl_wakeup_sys_init(void)
  97. {
  98. int ret;
  99. fsl_wakeup = kzalloc(sizeof(struct fsl_mpic_timer_wakeup), GFP_KERNEL);
  100. if (!fsl_wakeup)
  101. return -ENOMEM;
  102. INIT_WORK(&fsl_wakeup->free_work, fsl_free_resource);
  103. ret = device_create_file(mpic_subsys.dev_root, &mpic_attributes);
  104. if (ret)
  105. kfree(fsl_wakeup);
  106. return ret;
  107. }
  108. static void __exit fsl_wakeup_sys_exit(void)
  109. {
  110. device_remove_file(mpic_subsys.dev_root, &mpic_attributes);
  111. mutex_lock(&sysfs_lock);
  112. if (fsl_wakeup->timer) {
  113. disable_irq_wake(fsl_wakeup->timer->irq);
  114. mpic_free_timer(fsl_wakeup->timer);
  115. }
  116. kfree(fsl_wakeup);
  117. mutex_unlock(&sysfs_lock);
  118. }
  119. module_init(fsl_wakeup_sys_init);
  120. module_exit(fsl_wakeup_sys_exit);
  121. MODULE_DESCRIPTION("Freescale MPIC global timer wakeup driver");
  122. MODULE_LICENSE("GPL v2");
  123. MODULE_AUTHOR("Wang Dongsheng <dongsheng.wang@freescale.com>");