class.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/module.h>
  15. #include <linux/rtc.h>
  16. #include <linux/kdev_t.h>
  17. #include <linux/idr.h>
  18. #include <linux/slab.h>
  19. #include <linux/workqueue.h>
  20. #include "rtc-core.h"
  21. static DEFINE_IDA(rtc_ida);
  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. ida_simple_remove(&rtc_ida, rtc->id);
  27. kfree(rtc);
  28. }
  29. #ifdef CONFIG_RTC_HCTOSYS_DEVICE
  30. /* Result of the last RTC to system clock attempt. */
  31. int rtc_hctosys_ret = -ENODEV;
  32. #endif
  33. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
  34. /*
  35. * On suspend(), measure the delta between one RTC and the
  36. * system's wall clock; restore it on resume().
  37. */
  38. static struct timespec old_rtc, old_system, old_delta;
  39. static int rtc_suspend(struct device *dev)
  40. {
  41. struct rtc_device *rtc = to_rtc_device(dev);
  42. struct rtc_time tm;
  43. struct timespec delta, delta_delta;
  44. if (has_persistent_clock())
  45. return 0;
  46. if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
  47. return 0;
  48. /* snapshot the current RTC and system time at suspend*/
  49. rtc_read_time(rtc, &tm);
  50. getnstimeofday(&old_system);
  51. rtc_tm_to_time(&tm, &old_rtc.tv_sec);
  52. /*
  53. * To avoid drift caused by repeated suspend/resumes,
  54. * which each can add ~1 second drift error,
  55. * try to compensate so the difference in system time
  56. * and rtc time stays close to constant.
  57. */
  58. delta = timespec_sub(old_system, old_rtc);
  59. delta_delta = timespec_sub(delta, old_delta);
  60. if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) {
  61. /*
  62. * if delta_delta is too large, assume time correction
  63. * has occured and set old_delta to the current delta.
  64. */
  65. old_delta = delta;
  66. } else {
  67. /* Otherwise try to adjust old_system to compensate */
  68. old_system = timespec_sub(old_system, delta_delta);
  69. }
  70. return 0;
  71. }
  72. static int rtc_resume(struct device *dev)
  73. {
  74. struct rtc_device *rtc = to_rtc_device(dev);
  75. struct rtc_time tm;
  76. struct timespec new_system, new_rtc;
  77. struct timespec sleep_time;
  78. if (has_persistent_clock())
  79. return 0;
  80. rtc_hctosys_ret = -ENODEV;
  81. if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
  82. return 0;
  83. /* snapshot the current rtc and system time at resume */
  84. getnstimeofday(&new_system);
  85. rtc_read_time(rtc, &tm);
  86. if (rtc_valid_tm(&tm) != 0) {
  87. pr_debug("%s: bogus resume time\n", dev_name(&rtc->dev));
  88. return 0;
  89. }
  90. rtc_tm_to_time(&tm, &new_rtc.tv_sec);
  91. new_rtc.tv_nsec = 0;
  92. if (new_rtc.tv_sec < old_rtc.tv_sec) {
  93. pr_debug("%s: time travel!\n", dev_name(&rtc->dev));
  94. return 0;
  95. }
  96. /* calculate the RTC time delta (sleep time)*/
  97. sleep_time = timespec_sub(new_rtc, old_rtc);
  98. /*
  99. * Since these RTC suspend/resume handlers are not called
  100. * at the very end of suspend or the start of resume,
  101. * some run-time may pass on either sides of the sleep time
  102. * so subtract kernel run-time between rtc_suspend to rtc_resume
  103. * to keep things accurate.
  104. */
  105. sleep_time = timespec_sub(sleep_time,
  106. timespec_sub(new_system, old_system));
  107. if (sleep_time.tv_sec >= 0)
  108. timekeeping_inject_sleeptime(&sleep_time);
  109. rtc_hctosys_ret = 0;
  110. return 0;
  111. }
  112. static SIMPLE_DEV_PM_OPS(rtc_class_dev_pm_ops, rtc_suspend, rtc_resume);
  113. #define RTC_CLASS_DEV_PM_OPS (&rtc_class_dev_pm_ops)
  114. #else
  115. #define RTC_CLASS_DEV_PM_OPS NULL
  116. #endif
  117. /**
  118. * rtc_device_register - register w/ RTC class
  119. * @dev: the device to register
  120. *
  121. * rtc_device_unregister() must be called when the class device is no
  122. * longer needed.
  123. *
  124. * Returns the pointer to the new struct class device.
  125. */
  126. struct rtc_device *rtc_device_register(const char *name, struct device *dev,
  127. const struct rtc_class_ops *ops,
  128. struct module *owner)
  129. {
  130. struct rtc_device *rtc;
  131. struct rtc_wkalrm alrm;
  132. int id, err;
  133. id = ida_simple_get(&rtc_ida, 0, 0, GFP_KERNEL);
  134. if (id < 0) {
  135. err = id;
  136. goto exit;
  137. }
  138. rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);
  139. if (rtc == NULL) {
  140. err = -ENOMEM;
  141. goto exit_ida;
  142. }
  143. rtc->id = id;
  144. rtc->ops = ops;
  145. rtc->owner = owner;
  146. rtc->irq_freq = 1;
  147. rtc->max_user_freq = 64;
  148. rtc->dev.parent = dev;
  149. rtc->dev.class = rtc_class;
  150. rtc->dev.release = rtc_device_release;
  151. mutex_init(&rtc->ops_lock);
  152. spin_lock_init(&rtc->irq_lock);
  153. spin_lock_init(&rtc->irq_task_lock);
  154. init_waitqueue_head(&rtc->irq_queue);
  155. /* Init timerqueue */
  156. timerqueue_init_head(&rtc->timerqueue);
  157. INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
  158. /* Init aie timer */
  159. rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, (void *)rtc);
  160. /* Init uie timer */
  161. rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, (void *)rtc);
  162. /* Init pie timer */
  163. hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  164. rtc->pie_timer.function = rtc_pie_update_irq;
  165. rtc->pie_enabled = 0;
  166. /* Check to see if there is an ALARM already set in hw */
  167. err = __rtc_read_alarm(rtc, &alrm);
  168. if (!err && !rtc_valid_tm(&alrm.time))
  169. rtc_initialize_alarm(rtc, &alrm);
  170. strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
  171. dev_set_name(&rtc->dev, "rtc%d", id);
  172. rtc_dev_prepare(rtc);
  173. err = device_register(&rtc->dev);
  174. if (err) {
  175. put_device(&rtc->dev);
  176. goto exit_kfree;
  177. }
  178. rtc_dev_add_device(rtc);
  179. rtc_sysfs_add_device(rtc);
  180. rtc_proc_add_device(rtc);
  181. dev_info(dev, "rtc core: registered %s as %s\n",
  182. rtc->name, dev_name(&rtc->dev));
  183. return rtc;
  184. exit_kfree:
  185. kfree(rtc);
  186. exit_ida:
  187. ida_simple_remove(&rtc_ida, id);
  188. exit:
  189. dev_err(dev, "rtc core: unable to register %s, err = %d\n",
  190. name, err);
  191. return ERR_PTR(err);
  192. }
  193. EXPORT_SYMBOL_GPL(rtc_device_register);
  194. /**
  195. * rtc_device_unregister - removes the previously registered RTC class device
  196. *
  197. * @rtc: the RTC class device to destroy
  198. */
  199. void rtc_device_unregister(struct rtc_device *rtc)
  200. {
  201. if (get_device(&rtc->dev) != NULL) {
  202. mutex_lock(&rtc->ops_lock);
  203. /* remove innards of this RTC, then disable it, before
  204. * letting any rtc_class_open() users access it again
  205. */
  206. rtc_sysfs_del_device(rtc);
  207. rtc_dev_del_device(rtc);
  208. rtc_proc_del_device(rtc);
  209. device_unregister(&rtc->dev);
  210. rtc->ops = NULL;
  211. mutex_unlock(&rtc->ops_lock);
  212. put_device(&rtc->dev);
  213. }
  214. }
  215. EXPORT_SYMBOL_GPL(rtc_device_unregister);
  216. static void devm_rtc_device_release(struct device *dev, void *res)
  217. {
  218. struct rtc_device *rtc = *(struct rtc_device **)res;
  219. rtc_device_unregister(rtc);
  220. }
  221. static int devm_rtc_device_match(struct device *dev, void *res, void *data)
  222. {
  223. struct rtc **r = res;
  224. return *r == data;
  225. }
  226. /**
  227. * devm_rtc_device_register - resource managed rtc_device_register()
  228. * @dev: the device to register
  229. * @name: the name of the device
  230. * @ops: the rtc operations structure
  231. * @owner: the module owner
  232. *
  233. * @return a struct rtc on success, or an ERR_PTR on error
  234. *
  235. * Managed rtc_device_register(). The rtc_device returned from this function
  236. * are automatically freed on driver detach. See rtc_device_register()
  237. * for more information.
  238. */
  239. struct rtc_device *devm_rtc_device_register(struct device *dev,
  240. const char *name,
  241. const struct rtc_class_ops *ops,
  242. struct module *owner)
  243. {
  244. struct rtc_device **ptr, *rtc;
  245. ptr = devres_alloc(devm_rtc_device_release, sizeof(*ptr), GFP_KERNEL);
  246. if (!ptr)
  247. return ERR_PTR(-ENOMEM);
  248. rtc = rtc_device_register(name, dev, ops, owner);
  249. if (!IS_ERR(rtc)) {
  250. *ptr = rtc;
  251. devres_add(dev, ptr);
  252. } else {
  253. devres_free(ptr);
  254. }
  255. return rtc;
  256. }
  257. EXPORT_SYMBOL_GPL(devm_rtc_device_register);
  258. /**
  259. * devm_rtc_device_unregister - resource managed devm_rtc_device_unregister()
  260. * @dev: the device to unregister
  261. * @rtc: the RTC class device to unregister
  262. *
  263. * Deallocated a rtc allocated with devm_rtc_device_register(). Normally this
  264. * function will not need to be called and the resource management code will
  265. * ensure that the resource is freed.
  266. */
  267. void devm_rtc_device_unregister(struct device *dev, struct rtc_device *rtc)
  268. {
  269. int rc;
  270. rc = devres_release(dev, devm_rtc_device_release,
  271. devm_rtc_device_match, rtc);
  272. WARN_ON(rc);
  273. }
  274. EXPORT_SYMBOL_GPL(devm_rtc_device_unregister);
  275. static int __init rtc_init(void)
  276. {
  277. rtc_class = class_create(THIS_MODULE, "rtc");
  278. if (IS_ERR(rtc_class)) {
  279. pr_err("couldn't create class\n");
  280. return PTR_ERR(rtc_class);
  281. }
  282. rtc_class->pm = RTC_CLASS_DEV_PM_OPS;
  283. rtc_dev_init();
  284. rtc_sysfs_init(rtc_class);
  285. return 0;
  286. }
  287. static void __exit rtc_exit(void)
  288. {
  289. rtc_dev_exit();
  290. class_destroy(rtc_class);
  291. ida_destroy(&rtc_ida);
  292. }
  293. subsys_initcall(rtc_init);
  294. module_exit(rtc_exit);
  295. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  296. MODULE_DESCRIPTION("RTC class support");
  297. MODULE_LICENSE("GPL");