class.c 12 KB

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