class.c 6.8 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. #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_IDR(rtc_idr);
  21. static DEFINE_MUTEX(idr_lock);
  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. mutex_lock(&idr_lock);
  27. idr_remove(&rtc_idr, rtc->id);
  28. mutex_unlock(&idr_lock);
  29. kfree(rtc);
  30. }
  31. #if defined(CONFIG_PM) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
  32. /*
  33. * On suspend(), measure the delta between one RTC and the
  34. * system's wall clock; restore it on resume().
  35. */
  36. static struct timespec old_rtc, old_system, old_delta;
  37. static int rtc_suspend(struct device *dev, pm_message_t mesg)
  38. {
  39. struct rtc_device *rtc = to_rtc_device(dev);
  40. struct rtc_time tm;
  41. struct timespec delta, delta_delta;
  42. if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
  43. return 0;
  44. /* snapshot the current RTC and system time at suspend*/
  45. rtc_read_time(rtc, &tm);
  46. getnstimeofday(&old_system);
  47. rtc_tm_to_time(&tm, &old_rtc.tv_sec);
  48. /*
  49. * To avoid drift caused by repeated suspend/resumes,
  50. * which each can add ~1 second drift error,
  51. * try to compensate so the difference in system time
  52. * and rtc time stays close to constant.
  53. */
  54. delta = timespec_sub(old_system, old_rtc);
  55. delta_delta = timespec_sub(delta, old_delta);
  56. if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) {
  57. /*
  58. * if delta_delta is too large, assume time correction
  59. * has occured and set old_delta to the current delta.
  60. */
  61. old_delta = delta;
  62. } else {
  63. /* Otherwise try to adjust old_system to compensate */
  64. old_system = timespec_sub(old_system, delta_delta);
  65. }
  66. return 0;
  67. }
  68. static int rtc_resume(struct device *dev)
  69. {
  70. struct rtc_device *rtc = to_rtc_device(dev);
  71. struct rtc_time tm;
  72. struct timespec new_system, new_rtc;
  73. struct timespec sleep_time;
  74. if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
  75. return 0;
  76. /* snapshot the current rtc and system time at resume */
  77. getnstimeofday(&new_system);
  78. rtc_read_time(rtc, &tm);
  79. if (rtc_valid_tm(&tm) != 0) {
  80. pr_debug("%s: bogus resume time\n", dev_name(&rtc->dev));
  81. return 0;
  82. }
  83. rtc_tm_to_time(&tm, &new_rtc.tv_sec);
  84. new_rtc.tv_nsec = 0;
  85. if (new_rtc.tv_sec < old_rtc.tv_sec) {
  86. pr_debug("%s: time travel!\n", dev_name(&rtc->dev));
  87. return 0;
  88. }
  89. /* calculate the RTC time delta (sleep time)*/
  90. sleep_time = timespec_sub(new_rtc, old_rtc);
  91. /*
  92. * Since these RTC suspend/resume handlers are not called
  93. * at the very end of suspend or the start of resume,
  94. * some run-time may pass on either sides of the sleep time
  95. * so subtract kernel run-time between rtc_suspend to rtc_resume
  96. * to keep things accurate.
  97. */
  98. sleep_time = timespec_sub(sleep_time,
  99. timespec_sub(new_system, old_system));
  100. if (sleep_time.tv_sec >= 0)
  101. timekeeping_inject_sleeptime(&sleep_time);
  102. return 0;
  103. }
  104. #else
  105. #define rtc_suspend NULL
  106. #define rtc_resume NULL
  107. #endif
  108. /**
  109. * rtc_device_register - register w/ RTC class
  110. * @dev: the device to register
  111. *
  112. * rtc_device_unregister() must be called when the class device is no
  113. * longer needed.
  114. *
  115. * Returns the pointer to the new struct class device.
  116. */
  117. struct rtc_device *rtc_device_register(const char *name, struct device *dev,
  118. const struct rtc_class_ops *ops,
  119. struct module *owner)
  120. {
  121. struct rtc_device *rtc;
  122. struct rtc_wkalrm alrm;
  123. int id, err;
  124. if (idr_pre_get(&rtc_idr, GFP_KERNEL) == 0) {
  125. err = -ENOMEM;
  126. goto exit;
  127. }
  128. mutex_lock(&idr_lock);
  129. err = idr_get_new(&rtc_idr, NULL, &id);
  130. mutex_unlock(&idr_lock);
  131. if (err < 0)
  132. goto exit;
  133. id = id & MAX_ID_MASK;
  134. rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);
  135. if (rtc == NULL) {
  136. err = -ENOMEM;
  137. goto exit_idr;
  138. }
  139. rtc->id = id;
  140. rtc->ops = ops;
  141. rtc->owner = owner;
  142. rtc->irq_freq = 1;
  143. rtc->max_user_freq = 64;
  144. rtc->dev.parent = dev;
  145. rtc->dev.class = rtc_class;
  146. rtc->dev.release = rtc_device_release;
  147. mutex_init(&rtc->ops_lock);
  148. spin_lock_init(&rtc->irq_lock);
  149. spin_lock_init(&rtc->irq_task_lock);
  150. init_waitqueue_head(&rtc->irq_queue);
  151. /* Init timerqueue */
  152. timerqueue_init_head(&rtc->timerqueue);
  153. INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
  154. /* Init aie timer */
  155. rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, (void *)rtc);
  156. /* Init uie timer */
  157. rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, (void *)rtc);
  158. /* Init pie timer */
  159. hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  160. rtc->pie_timer.function = rtc_pie_update_irq;
  161. rtc->pie_enabled = 0;
  162. /* Check to see if there is an ALARM already set in hw */
  163. err = __rtc_read_alarm(rtc, &alrm);
  164. if (!err && !rtc_valid_tm(&alrm.time))
  165. rtc_initialize_alarm(rtc, &alrm);
  166. strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
  167. dev_set_name(&rtc->dev, "rtc%d", id);
  168. rtc_dev_prepare(rtc);
  169. err = device_register(&rtc->dev);
  170. if (err) {
  171. put_device(&rtc->dev);
  172. goto exit_kfree;
  173. }
  174. rtc_dev_add_device(rtc);
  175. rtc_sysfs_add_device(rtc);
  176. rtc_proc_add_device(rtc);
  177. dev_info(dev, "rtc core: registered %s as %s\n",
  178. rtc->name, dev_name(&rtc->dev));
  179. return rtc;
  180. exit_kfree:
  181. kfree(rtc);
  182. exit_idr:
  183. mutex_lock(&idr_lock);
  184. idr_remove(&rtc_idr, id);
  185. mutex_unlock(&idr_lock);
  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. idr_destroy(&rtc_idr);
  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");