class.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 delta;
  37. static time_t oldtime;
  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 ts = current_kernel_time();
  43. if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
  44. return 0;
  45. rtc_read_time(rtc, &tm);
  46. rtc_tm_to_time(&tm, &oldtime);
  47. /* RTC precision is 1 second; adjust delta for avg 1/2 sec err */
  48. set_normalized_timespec(&delta,
  49. ts.tv_sec - oldtime,
  50. ts.tv_nsec - (NSEC_PER_SEC >> 1));
  51. return 0;
  52. }
  53. static int rtc_resume(struct device *dev)
  54. {
  55. struct rtc_device *rtc = to_rtc_device(dev);
  56. struct rtc_time tm;
  57. time_t newtime;
  58. struct timespec time;
  59. if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
  60. return 0;
  61. rtc_read_time(rtc, &tm);
  62. if (rtc_valid_tm(&tm) != 0) {
  63. pr_debug("%s: bogus resume time\n", dev_name(&rtc->dev));
  64. return 0;
  65. }
  66. rtc_tm_to_time(&tm, &newtime);
  67. if (newtime <= oldtime) {
  68. if (newtime < oldtime)
  69. pr_debug("%s: time travel!\n", dev_name(&rtc->dev));
  70. return 0;
  71. }
  72. /* restore wall clock using delta against this RTC;
  73. * adjust again for avg 1/2 second RTC sampling error
  74. */
  75. set_normalized_timespec(&time,
  76. newtime + delta.tv_sec,
  77. (NSEC_PER_SEC >> 1) + delta.tv_nsec);
  78. do_settimeofday(&time);
  79. return 0;
  80. }
  81. #else
  82. #define rtc_suspend NULL
  83. #define rtc_resume NULL
  84. #endif
  85. /**
  86. * rtc_device_register - register w/ RTC class
  87. * @dev: the device to register
  88. *
  89. * rtc_device_unregister() must be called when the class device is no
  90. * longer needed.
  91. *
  92. * Returns the pointer to the new struct class device.
  93. */
  94. struct rtc_device *rtc_device_register(const char *name, struct device *dev,
  95. const struct rtc_class_ops *ops,
  96. struct module *owner)
  97. {
  98. struct rtc_device *rtc;
  99. int id, err;
  100. if (idr_pre_get(&rtc_idr, GFP_KERNEL) == 0) {
  101. err = -ENOMEM;
  102. goto exit;
  103. }
  104. mutex_lock(&idr_lock);
  105. err = idr_get_new(&rtc_idr, NULL, &id);
  106. mutex_unlock(&idr_lock);
  107. if (err < 0)
  108. goto exit;
  109. id = id & MAX_ID_MASK;
  110. rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);
  111. if (rtc == NULL) {
  112. err = -ENOMEM;
  113. goto exit_idr;
  114. }
  115. rtc->id = id;
  116. rtc->ops = ops;
  117. rtc->owner = owner;
  118. rtc->max_user_freq = 64;
  119. rtc->dev.parent = dev;
  120. rtc->dev.class = rtc_class;
  121. rtc->dev.release = rtc_device_release;
  122. mutex_init(&rtc->ops_lock);
  123. spin_lock_init(&rtc->irq_lock);
  124. spin_lock_init(&rtc->irq_task_lock);
  125. init_waitqueue_head(&rtc->irq_queue);
  126. /* Init timerqueue */
  127. timerqueue_init_head(&rtc->timerqueue);
  128. INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
  129. /* Init aie timer */
  130. rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, (void *)rtc);
  131. /* Init uie timer */
  132. rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, (void *)rtc);
  133. /* Init pie timer */
  134. hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  135. rtc->pie_timer.function = rtc_pie_update_irq;
  136. rtc->pie_enabled = 0;
  137. strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
  138. dev_set_name(&rtc->dev, "rtc%d", id);
  139. rtc_dev_prepare(rtc);
  140. err = device_register(&rtc->dev);
  141. if (err)
  142. goto exit_kfree;
  143. rtc_dev_add_device(rtc);
  144. rtc_sysfs_add_device(rtc);
  145. rtc_proc_add_device(rtc);
  146. dev_info(dev, "rtc core: registered %s as %s\n",
  147. rtc->name, dev_name(&rtc->dev));
  148. return rtc;
  149. exit_kfree:
  150. kfree(rtc);
  151. exit_idr:
  152. mutex_lock(&idr_lock);
  153. idr_remove(&rtc_idr, id);
  154. mutex_unlock(&idr_lock);
  155. exit:
  156. dev_err(dev, "rtc core: unable to register %s, err = %d\n",
  157. name, err);
  158. return ERR_PTR(err);
  159. }
  160. EXPORT_SYMBOL_GPL(rtc_device_register);
  161. /**
  162. * rtc_device_unregister - removes the previously registered RTC class device
  163. *
  164. * @rtc: the RTC class device to destroy
  165. */
  166. void rtc_device_unregister(struct rtc_device *rtc)
  167. {
  168. if (get_device(&rtc->dev) != NULL) {
  169. mutex_lock(&rtc->ops_lock);
  170. /* remove innards of this RTC, then disable it, before
  171. * letting any rtc_class_open() users access it again
  172. */
  173. rtc_sysfs_del_device(rtc);
  174. rtc_dev_del_device(rtc);
  175. rtc_proc_del_device(rtc);
  176. device_unregister(&rtc->dev);
  177. rtc->ops = NULL;
  178. mutex_unlock(&rtc->ops_lock);
  179. put_device(&rtc->dev);
  180. }
  181. }
  182. EXPORT_SYMBOL_GPL(rtc_device_unregister);
  183. static int __init rtc_init(void)
  184. {
  185. rtc_class = class_create(THIS_MODULE, "rtc");
  186. if (IS_ERR(rtc_class)) {
  187. printk(KERN_ERR "%s: couldn't create class\n", __FILE__);
  188. return PTR_ERR(rtc_class);
  189. }
  190. rtc_class->suspend = rtc_suspend;
  191. rtc_class->resume = rtc_resume;
  192. rtc_dev_init();
  193. rtc_sysfs_init(rtc_class);
  194. return 0;
  195. }
  196. static void __exit rtc_exit(void)
  197. {
  198. rtc_dev_exit();
  199. class_destroy(rtc_class);
  200. idr_destroy(&rtc_idr);
  201. }
  202. subsys_initcall(rtc_init);
  203. module_exit(rtc_exit);
  204. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  205. MODULE_DESCRIPTION("RTC class support");
  206. MODULE_LICENSE("GPL");