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