class.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * RTC subsystem, base class
  4. *
  5. * Copyright (C) 2005 Tower Technologies
  6. * Author: Alessandro Zummo <[email protected]>
  7. *
  8. * class skeleton from drivers/hwmon/hwmon.c
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/rtc.h>
  14. #include <linux/kdev_t.h>
  15. #include <linux/idr.h>
  16. #include <linux/slab.h>
  17. #include <linux/workqueue.h>
  18. #include "rtc-core.h"
  19. static DEFINE_IDA(rtc_ida);
  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. struct timerqueue_head *head = &rtc->timerqueue;
  25. struct timerqueue_node *node;
  26. mutex_lock(&rtc->ops_lock);
  27. while ((node = timerqueue_getnext(head)))
  28. timerqueue_del(head, node);
  29. mutex_unlock(&rtc->ops_lock);
  30. cancel_work_sync(&rtc->irqwork);
  31. ida_free(&rtc_ida, rtc->id);
  32. mutex_destroy(&rtc->ops_lock);
  33. kfree(rtc);
  34. }
  35. #ifdef CONFIG_RTC_HCTOSYS_DEVICE
  36. /* Result of the last RTC to system clock attempt. */
  37. int rtc_hctosys_ret = -ENODEV;
  38. /* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
  39. * whether it stores the most close value or the value with partial
  40. * seconds truncated. However, it is important that we use it to store
  41. * the truncated value. This is because otherwise it is necessary,
  42. * in an rtc sync function, to read both xtime.tv_sec and
  43. * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
  44. * of >32bits is not possible. So storing the most close value would
  45. * slow down the sync API. So here we have the truncated value and
  46. * the best guess is to add 0.5s.
  47. */
  48. static void rtc_hctosys(struct rtc_device *rtc)
  49. {
  50. int err;
  51. struct rtc_time tm;
  52. struct timespec64 tv64 = {
  53. .tv_nsec = NSEC_PER_SEC >> 1,
  54. };
  55. err = rtc_read_time(rtc, &tm);
  56. if (err) {
  57. dev_err(rtc->dev.parent,
  58. "hctosys: unable to read the hardware clock\n");
  59. goto err_read;
  60. }
  61. tv64.tv_sec = rtc_tm_to_time64(&tm);
  62. #if BITS_PER_LONG == 32
  63. if (tv64.tv_sec > INT_MAX) {
  64. err = -ERANGE;
  65. goto err_read;
  66. }
  67. #endif
  68. err = do_settimeofday64(&tv64);
  69. dev_info(rtc->dev.parent, "setting system clock to %ptR UTC (%lld)\n",
  70. &tm, (long long)tv64.tv_sec);
  71. err_read:
  72. rtc_hctosys_ret = err;
  73. }
  74. #endif
  75. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
  76. /*
  77. * On suspend(), measure the delta between one RTC and the
  78. * system's wall clock; restore it on resume().
  79. */
  80. static struct timespec64 old_rtc, old_system, old_delta;
  81. static int rtc_suspend(struct device *dev)
  82. {
  83. struct rtc_device *rtc = to_rtc_device(dev);
  84. struct rtc_time tm;
  85. struct timespec64 delta, delta_delta;
  86. int err;
  87. if (timekeeping_rtc_skipsuspend())
  88. return 0;
  89. if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
  90. return 0;
  91. /* snapshot the current RTC and system time at suspend*/
  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. ktime_get_real_ts64(&old_system);
  98. old_rtc.tv_sec = rtc_tm_to_time64(&tm);
  99. /*
  100. * To avoid drift caused by repeated suspend/resumes,
  101. * which each can add ~1 second drift error,
  102. * try to compensate so the difference in system time
  103. * and rtc time stays close to constant.
  104. */
  105. delta = timespec64_sub(old_system, old_rtc);
  106. delta_delta = timespec64_sub(delta, old_delta);
  107. if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) {
  108. /*
  109. * if delta_delta is too large, assume time correction
  110. * has occurred and set old_delta to the current delta.
  111. */
  112. old_delta = delta;
  113. } else {
  114. /* Otherwise try to adjust old_system to compensate */
  115. old_system = timespec64_sub(old_system, delta_delta);
  116. }
  117. return 0;
  118. }
  119. static int rtc_resume(struct device *dev)
  120. {
  121. struct rtc_device *rtc = to_rtc_device(dev);
  122. struct rtc_time tm;
  123. struct timespec64 new_system, new_rtc;
  124. struct timespec64 sleep_time;
  125. int err;
  126. if (timekeeping_rtc_skipresume())
  127. return 0;
  128. rtc_hctosys_ret = -ENODEV;
  129. if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
  130. return 0;
  131. /* snapshot the current rtc and system time at resume */
  132. ktime_get_real_ts64(&new_system);
  133. err = rtc_read_time(rtc, &tm);
  134. if (err < 0) {
  135. pr_debug("%s: fail to read rtc time\n", dev_name(&rtc->dev));
  136. return 0;
  137. }
  138. new_rtc.tv_sec = rtc_tm_to_time64(&tm);
  139. new_rtc.tv_nsec = 0;
  140. if (new_rtc.tv_sec < old_rtc.tv_sec) {
  141. pr_debug("%s: time travel!\n", dev_name(&rtc->dev));
  142. return 0;
  143. }
  144. /* calculate the RTC time delta (sleep time)*/
  145. sleep_time = timespec64_sub(new_rtc, old_rtc);
  146. /*
  147. * Since these RTC suspend/resume handlers are not called
  148. * at the very end of suspend or the start of resume,
  149. * some run-time may pass on either sides of the sleep time
  150. * so subtract kernel run-time between rtc_suspend to rtc_resume
  151. * to keep things accurate.
  152. */
  153. sleep_time = timespec64_sub(sleep_time,
  154. timespec64_sub(new_system, old_system));
  155. if (sleep_time.tv_sec >= 0)
  156. timekeeping_inject_sleeptime64(&sleep_time);
  157. rtc_hctosys_ret = 0;
  158. return 0;
  159. }
  160. static SIMPLE_DEV_PM_OPS(rtc_class_dev_pm_ops, rtc_suspend, rtc_resume);
  161. #define RTC_CLASS_DEV_PM_OPS (&rtc_class_dev_pm_ops)
  162. #else
  163. #define RTC_CLASS_DEV_PM_OPS NULL
  164. #endif
  165. /* Ensure the caller will set the id before releasing the device */
  166. static struct rtc_device *rtc_allocate_device(void)
  167. {
  168. struct rtc_device *rtc;
  169. rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
  170. if (!rtc)
  171. return NULL;
  172. device_initialize(&rtc->dev);
  173. /*
  174. * Drivers can revise this default after allocating the device.
  175. * The default is what most RTCs do: Increment seconds exactly one
  176. * second after the write happened. This adds a default transport
  177. * time of 5ms which is at least halfways close to reality.
  178. */
  179. rtc->set_offset_nsec = NSEC_PER_SEC + 5 * NSEC_PER_MSEC;
  180. rtc->irq_freq = 1;
  181. rtc->max_user_freq = 64;
  182. rtc->dev.class = rtc_class;
  183. rtc->dev.groups = rtc_get_dev_attribute_groups();
  184. rtc->dev.release = rtc_device_release;
  185. mutex_init(&rtc->ops_lock);
  186. spin_lock_init(&rtc->irq_lock);
  187. init_waitqueue_head(&rtc->irq_queue);
  188. /* Init timerqueue */
  189. timerqueue_init_head(&rtc->timerqueue);
  190. INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
  191. /* Init aie timer */
  192. rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, rtc);
  193. /* Init uie timer */
  194. rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, rtc);
  195. /* Init pie timer */
  196. hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  197. rtc->pie_timer.function = rtc_pie_update_irq;
  198. rtc->pie_enabled = 0;
  199. set_bit(RTC_FEATURE_ALARM, rtc->features);
  200. set_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);
  201. return rtc;
  202. }
  203. static int rtc_device_get_id(struct device *dev)
  204. {
  205. int of_id = -1, id = -1;
  206. if (dev->of_node)
  207. of_id = of_alias_get_id(dev->of_node, "rtc");
  208. else if (dev->parent && dev->parent->of_node)
  209. of_id = of_alias_get_id(dev->parent->of_node, "rtc");
  210. if (of_id >= 0) {
  211. id = ida_simple_get(&rtc_ida, of_id, of_id + 1, GFP_KERNEL);
  212. if (id < 0)
  213. dev_warn(dev, "/aliases ID %d not available\n", of_id);
  214. }
  215. if (id < 0)
  216. id = ida_alloc(&rtc_ida, GFP_KERNEL);
  217. return id;
  218. }
  219. static void rtc_device_get_offset(struct rtc_device *rtc)
  220. {
  221. time64_t range_secs;
  222. u32 start_year;
  223. int ret;
  224. /*
  225. * If RTC driver did not implement the range of RTC hardware device,
  226. * then we can not expand the RTC range by adding or subtracting one
  227. * offset.
  228. */
  229. if (rtc->range_min == rtc->range_max)
  230. return;
  231. ret = device_property_read_u32(rtc->dev.parent, "start-year",
  232. &start_year);
  233. if (!ret) {
  234. rtc->start_secs = mktime64(start_year, 1, 1, 0, 0, 0);
  235. rtc->set_start_time = true;
  236. }
  237. /*
  238. * If user did not implement the start time for RTC driver, then no
  239. * need to expand the RTC range.
  240. */
  241. if (!rtc->set_start_time)
  242. return;
  243. range_secs = rtc->range_max - rtc->range_min + 1;
  244. /*
  245. * If the start_secs is larger than the maximum seconds (rtc->range_max)
  246. * supported by RTC hardware or the maximum seconds of new expanded
  247. * range (start_secs + rtc->range_max - rtc->range_min) is less than
  248. * rtc->range_min, which means the minimum seconds (rtc->range_min) of
  249. * RTC hardware will be mapped to start_secs by adding one offset, so
  250. * the offset seconds calculation formula should be:
  251. * rtc->offset_secs = rtc->start_secs - rtc->range_min;
  252. *
  253. * If the start_secs is larger than the minimum seconds (rtc->range_min)
  254. * supported by RTC hardware, then there is one region is overlapped
  255. * between the original RTC hardware range and the new expanded range,
  256. * and this overlapped region do not need to be mapped into the new
  257. * expanded range due to it is valid for RTC device. So the minimum
  258. * seconds of RTC hardware (rtc->range_min) should be mapped to
  259. * rtc->range_max + 1, then the offset seconds formula should be:
  260. * rtc->offset_secs = rtc->range_max - rtc->range_min + 1;
  261. *
  262. * If the start_secs is less than the minimum seconds (rtc->range_min),
  263. * which is similar to case 2. So the start_secs should be mapped to
  264. * start_secs + rtc->range_max - rtc->range_min + 1, then the
  265. * offset seconds formula should be:
  266. * rtc->offset_secs = -(rtc->range_max - rtc->range_min + 1);
  267. *
  268. * Otherwise the offset seconds should be 0.
  269. */
  270. if (rtc->start_secs > rtc->range_max ||
  271. rtc->start_secs + range_secs - 1 < rtc->range_min)
  272. rtc->offset_secs = rtc->start_secs - rtc->range_min;
  273. else if (rtc->start_secs > rtc->range_min)
  274. rtc->offset_secs = range_secs;
  275. else if (rtc->start_secs < rtc->range_min)
  276. rtc->offset_secs = -range_secs;
  277. else
  278. rtc->offset_secs = 0;
  279. }
  280. static void devm_rtc_unregister_device(void *data)
  281. {
  282. struct rtc_device *rtc = data;
  283. mutex_lock(&rtc->ops_lock);
  284. /*
  285. * Remove innards of this RTC, then disable it, before
  286. * letting any rtc_class_open() users access it again
  287. */
  288. rtc_proc_del_device(rtc);
  289. if (!test_bit(RTC_NO_CDEV, &rtc->flags))
  290. cdev_device_del(&rtc->char_dev, &rtc->dev);
  291. rtc->ops = NULL;
  292. mutex_unlock(&rtc->ops_lock);
  293. }
  294. static void devm_rtc_release_device(void *res)
  295. {
  296. struct rtc_device *rtc = res;
  297. put_device(&rtc->dev);
  298. }
  299. struct rtc_device *devm_rtc_allocate_device(struct device *dev)
  300. {
  301. struct rtc_device *rtc;
  302. int id, err;
  303. id = rtc_device_get_id(dev);
  304. if (id < 0)
  305. return ERR_PTR(id);
  306. rtc = rtc_allocate_device();
  307. if (!rtc) {
  308. ida_free(&rtc_ida, id);
  309. return ERR_PTR(-ENOMEM);
  310. }
  311. rtc->id = id;
  312. rtc->dev.parent = dev;
  313. err = devm_add_action_or_reset(dev, devm_rtc_release_device, rtc);
  314. if (err)
  315. return ERR_PTR(err);
  316. err = dev_set_name(&rtc->dev, "rtc%d", id);
  317. if (err)
  318. return ERR_PTR(err);
  319. return rtc;
  320. }
  321. EXPORT_SYMBOL_GPL(devm_rtc_allocate_device);
  322. int __devm_rtc_register_device(struct module *owner, struct rtc_device *rtc)
  323. {
  324. struct rtc_wkalrm alrm;
  325. int err;
  326. if (!rtc->ops) {
  327. dev_dbg(&rtc->dev, "no ops set\n");
  328. return -EINVAL;
  329. }
  330. if (!rtc->ops->set_alarm)
  331. clear_bit(RTC_FEATURE_ALARM, rtc->features);
  332. if (rtc->ops->set_offset)
  333. set_bit(RTC_FEATURE_CORRECTION, rtc->features);
  334. rtc->owner = owner;
  335. rtc_device_get_offset(rtc);
  336. /* Check to see if there is an ALARM already set in hw */
  337. err = __rtc_read_alarm(rtc, &alrm);
  338. if (!err && !rtc_valid_tm(&alrm.time))
  339. rtc_initialize_alarm(rtc, &alrm);
  340. rtc_dev_prepare(rtc);
  341. err = cdev_device_add(&rtc->char_dev, &rtc->dev);
  342. if (err) {
  343. set_bit(RTC_NO_CDEV, &rtc->flags);
  344. dev_warn(rtc->dev.parent, "failed to add char device %d:%d\n",
  345. MAJOR(rtc->dev.devt), rtc->id);
  346. } else {
  347. dev_dbg(rtc->dev.parent, "char device (%d:%d)\n",
  348. MAJOR(rtc->dev.devt), rtc->id);
  349. }
  350. rtc_proc_add_device(rtc);
  351. dev_info(rtc->dev.parent, "registered as %s\n",
  352. dev_name(&rtc->dev));
  353. #ifdef CONFIG_RTC_HCTOSYS_DEVICE
  354. if (!strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE))
  355. rtc_hctosys(rtc);
  356. #endif
  357. return devm_add_action_or_reset(rtc->dev.parent,
  358. devm_rtc_unregister_device, rtc);
  359. }
  360. EXPORT_SYMBOL_GPL(__devm_rtc_register_device);
  361. /**
  362. * devm_rtc_device_register - resource managed rtc_device_register()
  363. * @dev: the device to register
  364. * @name: the name of the device (unused)
  365. * @ops: the rtc operations structure
  366. * @owner: the module owner
  367. *
  368. * @return a struct rtc on success, or an ERR_PTR on error
  369. *
  370. * Managed rtc_device_register(). The rtc_device returned from this function
  371. * are automatically freed on driver detach.
  372. * This function is deprecated, use devm_rtc_allocate_device and
  373. * rtc_register_device instead
  374. */
  375. struct rtc_device *devm_rtc_device_register(struct device *dev,
  376. const char *name,
  377. const struct rtc_class_ops *ops,
  378. struct module *owner)
  379. {
  380. struct rtc_device *rtc;
  381. int err;
  382. rtc = devm_rtc_allocate_device(dev);
  383. if (IS_ERR(rtc))
  384. return rtc;
  385. rtc->ops = ops;
  386. err = __devm_rtc_register_device(owner, rtc);
  387. if (err)
  388. return ERR_PTR(err);
  389. return rtc;
  390. }
  391. EXPORT_SYMBOL_GPL(devm_rtc_device_register);
  392. static int __init rtc_init(void)
  393. {
  394. rtc_class = class_create(THIS_MODULE, "rtc");
  395. if (IS_ERR(rtc_class)) {
  396. pr_err("couldn't create class\n");
  397. return PTR_ERR(rtc_class);
  398. }
  399. rtc_class->pm = RTC_CLASS_DEV_PM_OPS;
  400. rtc_dev_init();
  401. return 0;
  402. }
  403. subsys_initcall(rtc_init);