class.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 timespec64 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 timespec64 delta, delta_delta;
  45. int err;
  46. if (timekeeping_rtc_skipsuspend())
  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. ktime_get_real_ts64(&old_system);
  57. old_rtc.tv_sec = rtc_tm_to_time64(&tm);
  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 = timespec64_sub(old_system, old_rtc);
  65. delta_delta = timespec64_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 = timespec64_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 timespec64 new_system, new_rtc;
  83. struct timespec64 sleep_time;
  84. int err;
  85. if (timekeeping_rtc_skipresume())
  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. ktime_get_real_ts64(&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. new_rtc.tv_sec = rtc_tm_to_time64(&tm);
  98. new_rtc.tv_nsec = 0;
  99. if (new_rtc.tv_sec < old_rtc.tv_sec) {
  100. pr_debug("%s: time travel!\n", dev_name(&rtc->dev));
  101. return 0;
  102. }
  103. /* calculate the RTC time delta (sleep time)*/
  104. sleep_time = timespec64_sub(new_rtc, old_rtc);
  105. /*
  106. * Since these RTC suspend/resume handlers are not called
  107. * at the very end of suspend or the start of resume,
  108. * some run-time may pass on either sides of the sleep time
  109. * so subtract kernel run-time between rtc_suspend to rtc_resume
  110. * to keep things accurate.
  111. */
  112. sleep_time = timespec64_sub(sleep_time,
  113. timespec64_sub(new_system, old_system));
  114. if (sleep_time.tv_sec >= 0)
  115. timekeeping_inject_sleeptime64(&sleep_time);
  116. rtc_hctosys_ret = 0;
  117. return 0;
  118. }
  119. static SIMPLE_DEV_PM_OPS(rtc_class_dev_pm_ops, rtc_suspend, rtc_resume);
  120. #define RTC_CLASS_DEV_PM_OPS (&rtc_class_dev_pm_ops)
  121. #else
  122. #define RTC_CLASS_DEV_PM_OPS NULL
  123. #endif
  124. /* Ensure the caller will set the id before releasing the device */
  125. static struct rtc_device *rtc_allocate_device(void)
  126. {
  127. struct rtc_device *rtc;
  128. rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
  129. if (!rtc)
  130. return NULL;
  131. device_initialize(&rtc->dev);
  132. /* Drivers can revise this default after allocating the device. */
  133. rtc->set_offset_nsec = NSEC_PER_SEC / 2;
  134. rtc->irq_freq = 1;
  135. rtc->max_user_freq = 64;
  136. rtc->dev.class = rtc_class;
  137. rtc->dev.groups = rtc_get_dev_attribute_groups();
  138. rtc->dev.release = rtc_device_release;
  139. mutex_init(&rtc->ops_lock);
  140. spin_lock_init(&rtc->irq_lock);
  141. init_waitqueue_head(&rtc->irq_queue);
  142. /* Init timerqueue */
  143. timerqueue_init_head(&rtc->timerqueue);
  144. INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
  145. /* Init aie timer */
  146. rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, (void *)rtc);
  147. /* Init uie timer */
  148. rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, (void *)rtc);
  149. /* Init pie timer */
  150. hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  151. rtc->pie_timer.function = rtc_pie_update_irq;
  152. rtc->pie_enabled = 0;
  153. return rtc;
  154. }
  155. static int rtc_device_get_id(struct device *dev)
  156. {
  157. int of_id = -1, id = -1;
  158. if (dev->of_node)
  159. of_id = of_alias_get_id(dev->of_node, "rtc");
  160. else if (dev->parent && dev->parent->of_node)
  161. of_id = of_alias_get_id(dev->parent->of_node, "rtc");
  162. if (of_id >= 0) {
  163. id = ida_simple_get(&rtc_ida, of_id, of_id + 1, GFP_KERNEL);
  164. if (id < 0)
  165. dev_warn(dev, "/aliases ID %d not available\n", of_id);
  166. }
  167. if (id < 0)
  168. id = ida_simple_get(&rtc_ida, 0, 0, GFP_KERNEL);
  169. return id;
  170. }
  171. static void rtc_device_get_offset(struct rtc_device *rtc)
  172. {
  173. time64_t range_secs;
  174. u32 start_year;
  175. int ret;
  176. /*
  177. * If RTC driver did not implement the range of RTC hardware device,
  178. * then we can not expand the RTC range by adding or subtracting one
  179. * offset.
  180. */
  181. if (rtc->range_min == rtc->range_max)
  182. return;
  183. ret = device_property_read_u32(rtc->dev.parent, "start-year",
  184. &start_year);
  185. if (!ret) {
  186. rtc->start_secs = mktime64(start_year, 1, 1, 0, 0, 0);
  187. rtc->set_start_time = true;
  188. }
  189. /*
  190. * If user did not implement the start time for RTC driver, then no
  191. * need to expand the RTC range.
  192. */
  193. if (!rtc->set_start_time)
  194. return;
  195. range_secs = rtc->range_max - rtc->range_min + 1;
  196. /*
  197. * If the start_secs is larger than the maximum seconds (rtc->range_max)
  198. * supported by RTC hardware or the maximum seconds of new expanded
  199. * range (start_secs + rtc->range_max - rtc->range_min) is less than
  200. * rtc->range_min, which means the minimum seconds (rtc->range_min) of
  201. * RTC hardware will be mapped to start_secs by adding one offset, so
  202. * the offset seconds calculation formula should be:
  203. * rtc->offset_secs = rtc->start_secs - rtc->range_min;
  204. *
  205. * If the start_secs is larger than the minimum seconds (rtc->range_min)
  206. * supported by RTC hardware, then there is one region is overlapped
  207. * between the original RTC hardware range and the new expanded range,
  208. * and this overlapped region do not need to be mapped into the new
  209. * expanded range due to it is valid for RTC device. So the minimum
  210. * seconds of RTC hardware (rtc->range_min) should be mapped to
  211. * rtc->range_max + 1, then the offset seconds formula should be:
  212. * rtc->offset_secs = rtc->range_max - rtc->range_min + 1;
  213. *
  214. * If the start_secs is less than the minimum seconds (rtc->range_min),
  215. * which is similar to case 2. So the start_secs should be mapped to
  216. * start_secs + rtc->range_max - rtc->range_min + 1, then the
  217. * offset seconds formula should be:
  218. * rtc->offset_secs = -(rtc->range_max - rtc->range_min + 1);
  219. *
  220. * Otherwise the offset seconds should be 0.
  221. */
  222. if (rtc->start_secs > rtc->range_max ||
  223. rtc->start_secs + range_secs - 1 < rtc->range_min)
  224. rtc->offset_secs = rtc->start_secs - rtc->range_min;
  225. else if (rtc->start_secs > rtc->range_min)
  226. rtc->offset_secs = range_secs;
  227. else if (rtc->start_secs < rtc->range_min)
  228. rtc->offset_secs = -range_secs;
  229. else
  230. rtc->offset_secs = 0;
  231. }
  232. /**
  233. * rtc_device_register - register w/ RTC class
  234. * @dev: the device to register
  235. *
  236. * rtc_device_unregister() must be called when the class device is no
  237. * longer needed.
  238. *
  239. * Returns the pointer to the new struct class device.
  240. */
  241. struct rtc_device *rtc_device_register(const char *name, struct device *dev,
  242. const struct rtc_class_ops *ops,
  243. struct module *owner)
  244. {
  245. struct rtc_device *rtc;
  246. struct rtc_wkalrm alrm;
  247. int id, err;
  248. id = rtc_device_get_id(dev);
  249. if (id < 0) {
  250. err = id;
  251. goto exit;
  252. }
  253. rtc = rtc_allocate_device();
  254. if (!rtc) {
  255. err = -ENOMEM;
  256. goto exit_ida;
  257. }
  258. rtc->id = id;
  259. rtc->ops = ops;
  260. rtc->owner = owner;
  261. rtc->dev.parent = dev;
  262. dev_set_name(&rtc->dev, "rtc%d", id);
  263. rtc_device_get_offset(rtc);
  264. /* Check to see if there is an ALARM already set in hw */
  265. err = __rtc_read_alarm(rtc, &alrm);
  266. if (!err && !rtc_valid_tm(&alrm.time))
  267. rtc_initialize_alarm(rtc, &alrm);
  268. rtc_dev_prepare(rtc);
  269. err = cdev_device_add(&rtc->char_dev, &rtc->dev);
  270. if (err) {
  271. dev_warn(&rtc->dev, "%s: failed to add char device %d:%d\n",
  272. name, MAJOR(rtc->dev.devt), rtc->id);
  273. /* This will free both memory and the ID */
  274. put_device(&rtc->dev);
  275. goto exit;
  276. } else {
  277. dev_dbg(&rtc->dev, "%s: dev (%d:%d)\n", name,
  278. MAJOR(rtc->dev.devt), rtc->id);
  279. }
  280. rtc_proc_add_device(rtc);
  281. dev_info(dev, "rtc core: registered %s as %s\n",
  282. name, dev_name(&rtc->dev));
  283. return rtc;
  284. exit_ida:
  285. ida_simple_remove(&rtc_ida, id);
  286. exit:
  287. dev_err(dev, "rtc core: unable to register %s, err = %d\n",
  288. name, err);
  289. return ERR_PTR(err);
  290. }
  291. EXPORT_SYMBOL_GPL(rtc_device_register);
  292. /**
  293. * rtc_device_unregister - removes the previously registered RTC class device
  294. *
  295. * @rtc: the RTC class device to destroy
  296. */
  297. void rtc_device_unregister(struct rtc_device *rtc)
  298. {
  299. mutex_lock(&rtc->ops_lock);
  300. /*
  301. * Remove innards of this RTC, then disable it, before
  302. * letting any rtc_class_open() users access it again
  303. */
  304. rtc_proc_del_device(rtc);
  305. cdev_device_del(&rtc->char_dev, &rtc->dev);
  306. rtc->ops = NULL;
  307. mutex_unlock(&rtc->ops_lock);
  308. put_device(&rtc->dev);
  309. }
  310. EXPORT_SYMBOL_GPL(rtc_device_unregister);
  311. static void devm_rtc_device_release(struct device *dev, void *res)
  312. {
  313. struct rtc_device *rtc = *(struct rtc_device **)res;
  314. rtc_nvmem_unregister(rtc);
  315. rtc_device_unregister(rtc);
  316. }
  317. static int devm_rtc_device_match(struct device *dev, void *res, void *data)
  318. {
  319. struct rtc **r = res;
  320. return *r == data;
  321. }
  322. /**
  323. * devm_rtc_device_register - resource managed rtc_device_register()
  324. * @dev: the device to register
  325. * @name: the name of the device
  326. * @ops: the rtc operations structure
  327. * @owner: the module owner
  328. *
  329. * @return a struct rtc on success, or an ERR_PTR on error
  330. *
  331. * Managed rtc_device_register(). The rtc_device returned from this function
  332. * are automatically freed on driver detach. See rtc_device_register()
  333. * for more information.
  334. */
  335. struct rtc_device *devm_rtc_device_register(struct device *dev,
  336. const char *name,
  337. const struct rtc_class_ops *ops,
  338. struct module *owner)
  339. {
  340. struct rtc_device **ptr, *rtc;
  341. ptr = devres_alloc(devm_rtc_device_release, sizeof(*ptr), GFP_KERNEL);
  342. if (!ptr)
  343. return ERR_PTR(-ENOMEM);
  344. rtc = rtc_device_register(name, dev, ops, owner);
  345. if (!IS_ERR(rtc)) {
  346. *ptr = rtc;
  347. devres_add(dev, ptr);
  348. } else {
  349. devres_free(ptr);
  350. }
  351. return rtc;
  352. }
  353. EXPORT_SYMBOL_GPL(devm_rtc_device_register);
  354. /**
  355. * devm_rtc_device_unregister - resource managed devm_rtc_device_unregister()
  356. * @dev: the device to unregister
  357. * @rtc: the RTC class device to unregister
  358. *
  359. * Deallocated a rtc allocated with devm_rtc_device_register(). Normally this
  360. * function will not need to be called and the resource management code will
  361. * ensure that the resource is freed.
  362. */
  363. void devm_rtc_device_unregister(struct device *dev, struct rtc_device *rtc)
  364. {
  365. int rc;
  366. rc = devres_release(dev, devm_rtc_device_release,
  367. devm_rtc_device_match, rtc);
  368. WARN_ON(rc);
  369. }
  370. EXPORT_SYMBOL_GPL(devm_rtc_device_unregister);
  371. static void devm_rtc_release_device(struct device *dev, void *res)
  372. {
  373. struct rtc_device *rtc = *(struct rtc_device **)res;
  374. rtc_nvmem_unregister(rtc);
  375. if (rtc->registered)
  376. rtc_device_unregister(rtc);
  377. else
  378. put_device(&rtc->dev);
  379. }
  380. struct rtc_device *devm_rtc_allocate_device(struct device *dev)
  381. {
  382. struct rtc_device **ptr, *rtc;
  383. int id, err;
  384. id = rtc_device_get_id(dev);
  385. if (id < 0)
  386. return ERR_PTR(id);
  387. ptr = devres_alloc(devm_rtc_release_device, sizeof(*ptr), GFP_KERNEL);
  388. if (!ptr) {
  389. err = -ENOMEM;
  390. goto exit_ida;
  391. }
  392. rtc = rtc_allocate_device();
  393. if (!rtc) {
  394. err = -ENOMEM;
  395. goto exit_devres;
  396. }
  397. *ptr = rtc;
  398. devres_add(dev, ptr);
  399. rtc->id = id;
  400. rtc->dev.parent = dev;
  401. dev_set_name(&rtc->dev, "rtc%d", id);
  402. return rtc;
  403. exit_devres:
  404. devres_free(ptr);
  405. exit_ida:
  406. ida_simple_remove(&rtc_ida, id);
  407. return ERR_PTR(err);
  408. }
  409. EXPORT_SYMBOL_GPL(devm_rtc_allocate_device);
  410. int __rtc_register_device(struct module *owner, struct rtc_device *rtc)
  411. {
  412. struct rtc_wkalrm alrm;
  413. int err;
  414. if (!rtc->ops)
  415. return -EINVAL;
  416. rtc->owner = owner;
  417. rtc_device_get_offset(rtc);
  418. /* Check to see if there is an ALARM already set in hw */
  419. err = __rtc_read_alarm(rtc, &alrm);
  420. if (!err && !rtc_valid_tm(&alrm.time))
  421. rtc_initialize_alarm(rtc, &alrm);
  422. rtc_dev_prepare(rtc);
  423. err = cdev_device_add(&rtc->char_dev, &rtc->dev);
  424. if (err)
  425. dev_warn(rtc->dev.parent, "failed to add char device %d:%d\n",
  426. MAJOR(rtc->dev.devt), rtc->id);
  427. else
  428. dev_dbg(rtc->dev.parent, "char device (%d:%d)\n",
  429. MAJOR(rtc->dev.devt), rtc->id);
  430. rtc_proc_add_device(rtc);
  431. rtc->registered = true;
  432. dev_info(rtc->dev.parent, "registered as %s\n",
  433. dev_name(&rtc->dev));
  434. return 0;
  435. }
  436. EXPORT_SYMBOL_GPL(__rtc_register_device);
  437. static int __init rtc_init(void)
  438. {
  439. rtc_class = class_create(THIS_MODULE, "rtc");
  440. if (IS_ERR(rtc_class)) {
  441. pr_err("couldn't create class\n");
  442. return PTR_ERR(rtc_class);
  443. }
  444. rtc_class->pm = RTC_CLASS_DEV_PM_OPS;
  445. rtc_dev_init();
  446. return 0;
  447. }
  448. subsys_initcall(rtc_init);