class.c 5.2 KB

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