class.c 9.1 KB

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