class.c 9.3 KB

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