class.c 6.8 KB

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