class.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * RTC subsystem, base class
  3. *
  4. * Copyright (C) 2005 Tower Technologies
  5. * Author: Alessandro Zummo <a.zummo@towertech.it>
  6. *
  7. * class skeleton from drivers/hwmon/hwmon.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/rtc.h>
  15. #include <linux/kdev_t.h>
  16. #include <linux/idr.h>
  17. #include <linux/slab.h>
  18. #include <linux/workqueue.h>
  19. #include "rtc-core.h"
  20. static DEFINE_IDA(rtc_ida);
  21. struct class *rtc_class;
  22. static void rtc_device_release(struct device *dev)
  23. {
  24. struct rtc_device *rtc = to_rtc_device(dev);
  25. ida_simple_remove(&rtc_ida, rtc->id);
  26. kfree(rtc);
  27. }
  28. #if defined(CONFIG_PM) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
  29. /*
  30. * On suspend(), measure the delta between one RTC and the
  31. * system's wall clock; restore it on resume().
  32. */
  33. static struct timespec old_rtc, old_system, old_delta;
  34. static int rtc_suspend(struct device *dev, pm_message_t mesg)
  35. {
  36. struct rtc_device *rtc = to_rtc_device(dev);
  37. struct rtc_time tm;
  38. struct timespec delta, delta_delta;
  39. if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
  40. return 0;
  41. /* snapshot the current RTC and system time at suspend*/
  42. rtc_read_time(rtc, &tm);
  43. getnstimeofday(&old_system);
  44. rtc_tm_to_time(&tm, &old_rtc.tv_sec);
  45. /*
  46. * To avoid drift caused by repeated suspend/resumes,
  47. * which each can add ~1 second drift error,
  48. * try to compensate so the difference in system time
  49. * and rtc time stays close to constant.
  50. */
  51. delta = timespec_sub(old_system, old_rtc);
  52. delta_delta = timespec_sub(delta, old_delta);
  53. if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) {
  54. /*
  55. * if delta_delta is too large, assume time correction
  56. * has occured and set old_delta to the current delta.
  57. */
  58. old_delta = delta;
  59. } else {
  60. /* Otherwise try to adjust old_system to compensate */
  61. old_system = timespec_sub(old_system, delta_delta);
  62. }
  63. return 0;
  64. }
  65. static int rtc_resume(struct device *dev)
  66. {
  67. struct rtc_device *rtc = to_rtc_device(dev);
  68. struct rtc_time tm;
  69. struct timespec new_system, new_rtc;
  70. struct timespec sleep_time;
  71. if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
  72. return 0;
  73. /* snapshot the current rtc and system time at resume */
  74. getnstimeofday(&new_system);
  75. rtc_read_time(rtc, &tm);
  76. if (rtc_valid_tm(&tm) != 0) {
  77. pr_debug("%s: bogus resume time\n", dev_name(&rtc->dev));
  78. return 0;
  79. }
  80. rtc_tm_to_time(&tm, &new_rtc.tv_sec);
  81. new_rtc.tv_nsec = 0;
  82. if (new_rtc.tv_sec < old_rtc.tv_sec) {
  83. pr_debug("%s: time travel!\n", dev_name(&rtc->dev));
  84. return 0;
  85. }
  86. /* calculate the RTC time delta (sleep time)*/
  87. sleep_time = timespec_sub(new_rtc, old_rtc);
  88. /*
  89. * Since these RTC suspend/resume handlers are not called
  90. * at the very end of suspend or the start of resume,
  91. * some run-time may pass on either sides of the sleep time
  92. * so subtract kernel run-time between rtc_suspend to rtc_resume
  93. * to keep things accurate.
  94. */
  95. sleep_time = timespec_sub(sleep_time,
  96. timespec_sub(new_system, old_system));
  97. if (sleep_time.tv_sec >= 0)
  98. timekeeping_inject_sleeptime(&sleep_time);
  99. return 0;
  100. }
  101. #else
  102. #define rtc_suspend NULL
  103. #define rtc_resume NULL
  104. #endif
  105. /**
  106. * rtc_device_register - register w/ RTC class
  107. * @dev: the device to register
  108. *
  109. * rtc_device_unregister() must be called when the class device is no
  110. * longer needed.
  111. *
  112. * Returns the pointer to the new struct class device.
  113. */
  114. struct rtc_device *rtc_device_register(const char *name, struct device *dev,
  115. const struct rtc_class_ops *ops,
  116. struct module *owner)
  117. {
  118. struct rtc_device *rtc;
  119. struct rtc_wkalrm alrm;
  120. int id, err;
  121. id = ida_simple_get(&rtc_ida, 0, 0, GFP_KERNEL);
  122. if (id < 0) {
  123. err = id;
  124. goto exit;
  125. }
  126. rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);
  127. if (rtc == NULL) {
  128. err = -ENOMEM;
  129. goto exit_ida;
  130. }
  131. rtc->id = id;
  132. rtc->ops = ops;
  133. rtc->owner = owner;
  134. rtc->irq_freq = 1;
  135. rtc->max_user_freq = 64;
  136. rtc->dev.parent = dev;
  137. rtc->dev.class = rtc_class;
  138. rtc->dev.release = rtc_device_release;
  139. mutex_init(&rtc->ops_lock);
  140. spin_lock_init(&rtc->irq_lock);
  141. spin_lock_init(&rtc->irq_task_lock);
  142. init_waitqueue_head(&rtc->irq_queue);
  143. /* Init timerqueue */
  144. timerqueue_init_head(&rtc->timerqueue);
  145. INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
  146. /* Init aie timer */
  147. rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, (void *)rtc);
  148. /* Init uie timer */
  149. rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, (void *)rtc);
  150. /* Init pie timer */
  151. hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  152. rtc->pie_timer.function = rtc_pie_update_irq;
  153. rtc->pie_enabled = 0;
  154. /* Check to see if there is an ALARM already set in hw */
  155. err = __rtc_read_alarm(rtc, &alrm);
  156. if (!err && !rtc_valid_tm(&alrm.time))
  157. rtc_initialize_alarm(rtc, &alrm);
  158. strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
  159. dev_set_name(&rtc->dev, "rtc%d", id);
  160. rtc_dev_prepare(rtc);
  161. err = device_register(&rtc->dev);
  162. if (err) {
  163. put_device(&rtc->dev);
  164. goto exit_kfree;
  165. }
  166. rtc_dev_add_device(rtc);
  167. rtc_sysfs_add_device(rtc);
  168. rtc_proc_add_device(rtc);
  169. dev_info(dev, "rtc core: registered %s as %s\n",
  170. rtc->name, dev_name(&rtc->dev));
  171. return rtc;
  172. exit_kfree:
  173. kfree(rtc);
  174. exit_ida:
  175. ida_simple_remove(&rtc_ida, id);
  176. exit:
  177. dev_err(dev, "rtc core: unable to register %s, err = %d\n",
  178. name, err);
  179. return ERR_PTR(err);
  180. }
  181. EXPORT_SYMBOL_GPL(rtc_device_register);
  182. /**
  183. * rtc_device_unregister - removes the previously registered RTC class device
  184. *
  185. * @rtc: the RTC class device to destroy
  186. */
  187. void rtc_device_unregister(struct rtc_device *rtc)
  188. {
  189. if (get_device(&rtc->dev) != NULL) {
  190. mutex_lock(&rtc->ops_lock);
  191. /* remove innards of this RTC, then disable it, before
  192. * letting any rtc_class_open() users access it again
  193. */
  194. rtc_sysfs_del_device(rtc);
  195. rtc_dev_del_device(rtc);
  196. rtc_proc_del_device(rtc);
  197. device_unregister(&rtc->dev);
  198. rtc->ops = NULL;
  199. mutex_unlock(&rtc->ops_lock);
  200. put_device(&rtc->dev);
  201. }
  202. }
  203. EXPORT_SYMBOL_GPL(rtc_device_unregister);
  204. static int __init rtc_init(void)
  205. {
  206. rtc_class = class_create(THIS_MODULE, "rtc");
  207. if (IS_ERR(rtc_class)) {
  208. printk(KERN_ERR "%s: couldn't create class\n", __FILE__);
  209. return PTR_ERR(rtc_class);
  210. }
  211. rtc_class->suspend = rtc_suspend;
  212. rtc_class->resume = rtc_resume;
  213. rtc_dev_init();
  214. rtc_sysfs_init(rtc_class);
  215. return 0;
  216. }
  217. static void __exit rtc_exit(void)
  218. {
  219. rtc_dev_exit();
  220. class_destroy(rtc_class);
  221. ida_destroy(&rtc_ida);
  222. }
  223. subsys_initcall(rtc_init);
  224. module_exit(rtc_exit);
  225. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  226. MODULE_DESCRIPTION("RTC class support");
  227. MODULE_LICENSE("GPL");