class.c 6.9 KB

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