class.c 13 KB

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