class.c 6.7 KB

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