class.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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_obj(*rtc);
  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_setup(&rtc->pie_timer, rtc_pie_update_irq, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  200. rtc->pie_enabled = 0;
  201. set_bit(RTC_FEATURE_ALARM, rtc->features);
  202. set_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);
  203. return rtc;
  204. }
  205. static int rtc_device_get_id(struct device *dev)
  206. {
  207. int of_id = -1, id = -1;
  208. if (dev->of_node)
  209. of_id = of_alias_get_id(dev->of_node, "rtc");
  210. else if (dev->parent && dev->parent->of_node)
  211. of_id = of_alias_get_id(dev->parent->of_node, "rtc");
  212. if (of_id >= 0) {
  213. id = ida_alloc_range(&rtc_ida, of_id, of_id, GFP_KERNEL);
  214. if (id < 0)
  215. dev_warn(dev, "/aliases ID %d not available\n", of_id);
  216. }
  217. if (id < 0)
  218. id = ida_alloc(&rtc_ida, GFP_KERNEL);
  219. return id;
  220. }
  221. static void rtc_device_get_offset(struct rtc_device *rtc)
  222. {
  223. time64_t range_secs;
  224. u32 start_year;
  225. int ret;
  226. /*
  227. * If RTC driver did not implement the range of RTC hardware device,
  228. * then we can not expand the RTC range by adding or subtracting one
  229. * offset.
  230. */
  231. if (rtc->range_min == rtc->range_max)
  232. return;
  233. ret = device_property_read_u32(rtc->dev.parent, "start-year",
  234. &start_year);
  235. if (!ret) {
  236. rtc->start_secs = mktime64(start_year, 1, 1, 0, 0, 0);
  237. rtc->set_start_time = true;
  238. }
  239. /*
  240. * If user did not implement the start time for RTC driver, then no
  241. * need to expand the RTC range.
  242. */
  243. if (!rtc->set_start_time)
  244. return;
  245. range_secs = rtc->range_max - rtc->range_min + 1;
  246. /*
  247. * If the start_secs is larger than the maximum seconds (rtc->range_max)
  248. * supported by RTC hardware or the maximum seconds of new expanded
  249. * range (start_secs + rtc->range_max - rtc->range_min) is less than
  250. * rtc->range_min, which means the minimum seconds (rtc->range_min) of
  251. * RTC hardware will be mapped to start_secs by adding one offset, so
  252. * the offset seconds calculation formula should be:
  253. * rtc->offset_secs = rtc->start_secs - rtc->range_min;
  254. *
  255. * If the start_secs is larger than the minimum seconds (rtc->range_min)
  256. * supported by RTC hardware, then there is one region is overlapped
  257. * between the original RTC hardware range and the new expanded range,
  258. * and this overlapped region do not need to be mapped into the new
  259. * expanded range due to it is valid for RTC device. So the minimum
  260. * seconds of RTC hardware (rtc->range_min) should be mapped to
  261. * rtc->range_max + 1, then the offset seconds formula should be:
  262. * rtc->offset_secs = rtc->range_max - rtc->range_min + 1;
  263. *
  264. * If the start_secs is less than the minimum seconds (rtc->range_min),
  265. * which is similar to case 2. So the start_secs should be mapped to
  266. * start_secs + rtc->range_max - rtc->range_min + 1, then the
  267. * offset seconds formula should be:
  268. * rtc->offset_secs = -(rtc->range_max - rtc->range_min + 1);
  269. *
  270. * Otherwise the offset seconds should be 0.
  271. */
  272. if ((rtc->start_secs >= 0 && rtc->start_secs > rtc->range_max) ||
  273. rtc->start_secs + range_secs - 1 < rtc->range_min)
  274. rtc->offset_secs = rtc->start_secs - rtc->range_min;
  275. else if (rtc->start_secs > rtc->range_min)
  276. rtc->offset_secs = range_secs;
  277. else if (rtc->start_secs < rtc->range_min)
  278. rtc->offset_secs = -range_secs;
  279. else
  280. rtc->offset_secs = 0;
  281. }
  282. static void devm_rtc_unregister_device(void *data)
  283. {
  284. struct rtc_device *rtc = data;
  285. mutex_lock(&rtc->ops_lock);
  286. /*
  287. * Remove innards of this RTC, then disable it, before
  288. * letting any rtc_class_open() users access it again
  289. */
  290. rtc_proc_del_device(rtc);
  291. if (!test_bit(RTC_NO_CDEV, &rtc->flags))
  292. cdev_device_del(&rtc->char_dev, &rtc->dev);
  293. rtc->ops = NULL;
  294. mutex_unlock(&rtc->ops_lock);
  295. }
  296. static void devm_rtc_release_device(void *res)
  297. {
  298. struct rtc_device *rtc = res;
  299. put_device(&rtc->dev);
  300. }
  301. struct rtc_device *devm_rtc_allocate_device(struct device *dev)
  302. {
  303. struct rtc_device *rtc;
  304. int id, err;
  305. id = rtc_device_get_id(dev);
  306. if (id < 0)
  307. return ERR_PTR(id);
  308. rtc = rtc_allocate_device();
  309. if (!rtc) {
  310. ida_free(&rtc_ida, id);
  311. return ERR_PTR(-ENOMEM);
  312. }
  313. rtc->id = id;
  314. rtc->dev.parent = dev;
  315. err = devm_add_action_or_reset(dev, devm_rtc_release_device, rtc);
  316. if (err)
  317. return ERR_PTR(err);
  318. err = dev_set_name(&rtc->dev, "rtc%d", id);
  319. if (err)
  320. return ERR_PTR(err);
  321. return rtc;
  322. }
  323. EXPORT_SYMBOL_GPL(devm_rtc_allocate_device);
  324. int __devm_rtc_register_device(struct module *owner, struct rtc_device *rtc)
  325. {
  326. struct rtc_wkalrm alrm;
  327. int err;
  328. if (!rtc->ops) {
  329. dev_dbg(&rtc->dev, "no ops set\n");
  330. return -EINVAL;
  331. }
  332. if (!rtc->ops->set_alarm)
  333. clear_bit(RTC_FEATURE_ALARM, rtc->features);
  334. if (rtc->ops->set_offset)
  335. set_bit(RTC_FEATURE_CORRECTION, rtc->features);
  336. rtc->owner = owner;
  337. rtc_device_get_offset(rtc);
  338. /* Check to see if there is an ALARM already set in hw */
  339. err = __rtc_read_alarm(rtc, &alrm);
  340. if (!err)
  341. rtc_initialize_alarm(rtc, &alrm);
  342. rtc_dev_prepare(rtc);
  343. err = cdev_device_add(&rtc->char_dev, &rtc->dev);
  344. if (err) {
  345. set_bit(RTC_NO_CDEV, &rtc->flags);
  346. dev_warn(rtc->dev.parent, "failed to add char device %d:%d\n",
  347. MAJOR(rtc->dev.devt), rtc->id);
  348. } else {
  349. dev_dbg(rtc->dev.parent, "char device (%d:%d)\n",
  350. MAJOR(rtc->dev.devt), rtc->id);
  351. }
  352. rtc_proc_add_device(rtc);
  353. dev_info(rtc->dev.parent, "registered as %s\n",
  354. dev_name(&rtc->dev));
  355. #ifdef CONFIG_RTC_HCTOSYS_DEVICE
  356. if (!strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE))
  357. rtc_hctosys(rtc);
  358. #endif
  359. return devm_add_action_or_reset(rtc->dev.parent,
  360. devm_rtc_unregister_device, rtc);
  361. }
  362. EXPORT_SYMBOL_GPL(__devm_rtc_register_device);
  363. /**
  364. * devm_rtc_device_register - resource managed rtc_device_register()
  365. * @dev: the device to register
  366. * @name: the name of the device (unused)
  367. * @ops: the rtc operations structure
  368. * @owner: the module owner
  369. *
  370. * @return a struct rtc on success, or an ERR_PTR on error
  371. *
  372. * Managed rtc_device_register(). The rtc_device returned from this function
  373. * are automatically freed on driver detach.
  374. * This function is deprecated, use devm_rtc_allocate_device and
  375. * rtc_register_device instead
  376. */
  377. struct rtc_device *devm_rtc_device_register(struct device *dev,
  378. const char *name,
  379. const struct rtc_class_ops *ops,
  380. struct module *owner)
  381. {
  382. struct rtc_device *rtc;
  383. int err;
  384. rtc = devm_rtc_allocate_device(dev);
  385. if (IS_ERR(rtc))
  386. return rtc;
  387. rtc->ops = ops;
  388. err = __devm_rtc_register_device(owner, rtc);
  389. if (err)
  390. return ERR_PTR(err);
  391. return rtc;
  392. }
  393. EXPORT_SYMBOL_GPL(devm_rtc_device_register);
  394. static int __init rtc_init(void)
  395. {
  396. int err;
  397. err = class_register(&rtc_class);
  398. if (err)
  399. return err;
  400. rtc_dev_init();
  401. return 0;
  402. }
  403. subsys_initcall(rtc_init);