class.c 5.7 KB

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