interface.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * RTC subsystem, interface functions
  4. *
  5. * Copyright (C) 2005 Tower Technologies
  6. * Author: Alessandro Zummo <a.zummo@towertech.it>
  7. *
  8. * based on arch/arm/common/rtctime.c
  9. */
  10. #include <linux/rtc.h>
  11. #include <linux/sched.h>
  12. #include <linux/module.h>
  13. #include <linux/log2.h>
  14. #include <linux/workqueue.h>
  15. #define CREATE_TRACE_POINTS
  16. #include <trace/events/rtc.h>
  17. static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
  18. static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
  19. static void rtc_add_offset(struct rtc_device *rtc, struct rtc_time *tm)
  20. {
  21. time64_t secs;
  22. if (!rtc->offset_secs)
  23. return;
  24. secs = rtc_tm_to_time64(tm);
  25. /*
  26. * Since the reading time values from RTC device are always in the RTC
  27. * original valid range, but we need to skip the overlapped region
  28. * between expanded range and original range, which is no need to add
  29. * the offset.
  30. */
  31. if ((rtc->start_secs > rtc->range_min && secs >= rtc->start_secs) ||
  32. (rtc->start_secs < rtc->range_min &&
  33. secs <= (rtc->start_secs + rtc->range_max - rtc->range_min)))
  34. return;
  35. rtc_time64_to_tm(secs + rtc->offset_secs, tm);
  36. }
  37. static void rtc_subtract_offset(struct rtc_device *rtc, struct rtc_time *tm)
  38. {
  39. time64_t secs;
  40. if (!rtc->offset_secs)
  41. return;
  42. secs = rtc_tm_to_time64(tm);
  43. /*
  44. * If the setting time values are in the valid range of RTC hardware
  45. * device, then no need to subtract the offset when setting time to RTC
  46. * device. Otherwise we need to subtract the offset to make the time
  47. * values are valid for RTC hardware device.
  48. */
  49. if (secs >= rtc->range_min && secs <= rtc->range_max)
  50. return;
  51. rtc_time64_to_tm(secs - rtc->offset_secs, tm);
  52. }
  53. static int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm)
  54. {
  55. if (rtc->range_min != rtc->range_max) {
  56. time64_t time = rtc_tm_to_time64(tm);
  57. time64_t range_min = rtc->set_start_time ? rtc->start_secs :
  58. rtc->range_min;
  59. timeu64_t range_max = rtc->set_start_time ?
  60. (rtc->start_secs + rtc->range_max - rtc->range_min) :
  61. rtc->range_max;
  62. if (time < range_min || time > range_max)
  63. return -ERANGE;
  64. }
  65. return 0;
  66. }
  67. static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
  68. {
  69. int err;
  70. if (!rtc->ops) {
  71. err = -ENODEV;
  72. } else if (!rtc->ops->read_time) {
  73. err = -EINVAL;
  74. } else {
  75. memset(tm, 0, sizeof(struct rtc_time));
  76. err = rtc->ops->read_time(rtc->dev.parent, tm);
  77. if (err < 0) {
  78. dev_dbg(&rtc->dev, "read_time: fail to read: %d\n",
  79. err);
  80. return err;
  81. }
  82. rtc_add_offset(rtc, tm);
  83. err = rtc_valid_tm(tm);
  84. if (err < 0)
  85. dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n");
  86. }
  87. return err;
  88. }
  89. int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
  90. {
  91. int err;
  92. err = mutex_lock_interruptible(&rtc->ops_lock);
  93. if (err)
  94. return err;
  95. err = __rtc_read_time(rtc, tm);
  96. mutex_unlock(&rtc->ops_lock);
  97. trace_rtc_read_time(rtc_tm_to_time64(tm), err);
  98. return err;
  99. }
  100. EXPORT_SYMBOL_GPL(rtc_read_time);
  101. int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
  102. {
  103. int err, uie;
  104. err = rtc_valid_tm(tm);
  105. if (err != 0)
  106. return err;
  107. err = rtc_valid_range(rtc, tm);
  108. if (err)
  109. return err;
  110. rtc_subtract_offset(rtc, tm);
  111. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  112. uie = rtc->uie_rtctimer.enabled || rtc->uie_irq_active;
  113. #else
  114. uie = rtc->uie_rtctimer.enabled;
  115. #endif
  116. if (uie) {
  117. err = rtc_update_irq_enable(rtc, 0);
  118. if (err)
  119. return err;
  120. }
  121. err = mutex_lock_interruptible(&rtc->ops_lock);
  122. if (err)
  123. return err;
  124. if (!rtc->ops)
  125. err = -ENODEV;
  126. else if (rtc->ops->set_time)
  127. err = rtc->ops->set_time(rtc->dev.parent, tm);
  128. else
  129. err = -EINVAL;
  130. pm_stay_awake(rtc->dev.parent);
  131. mutex_unlock(&rtc->ops_lock);
  132. /* A timer might have just expired */
  133. schedule_work(&rtc->irqwork);
  134. if (uie) {
  135. err = rtc_update_irq_enable(rtc, 1);
  136. if (err)
  137. return err;
  138. }
  139. trace_rtc_set_time(rtc_tm_to_time64(tm), err);
  140. return err;
  141. }
  142. EXPORT_SYMBOL_GPL(rtc_set_time);
  143. static int rtc_read_alarm_internal(struct rtc_device *rtc,
  144. struct rtc_wkalrm *alarm)
  145. {
  146. int err;
  147. err = mutex_lock_interruptible(&rtc->ops_lock);
  148. if (err)
  149. return err;
  150. if (!rtc->ops) {
  151. err = -ENODEV;
  152. } else if (!rtc->ops->read_alarm) {
  153. err = -EINVAL;
  154. } else {
  155. alarm->enabled = 0;
  156. alarm->pending = 0;
  157. alarm->time.tm_sec = -1;
  158. alarm->time.tm_min = -1;
  159. alarm->time.tm_hour = -1;
  160. alarm->time.tm_mday = -1;
  161. alarm->time.tm_mon = -1;
  162. alarm->time.tm_year = -1;
  163. alarm->time.tm_wday = -1;
  164. alarm->time.tm_yday = -1;
  165. alarm->time.tm_isdst = -1;
  166. err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
  167. }
  168. mutex_unlock(&rtc->ops_lock);
  169. trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
  170. return err;
  171. }
  172. int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  173. {
  174. int err;
  175. struct rtc_time before, now;
  176. int first_time = 1;
  177. time64_t t_now, t_alm;
  178. enum { none, day, month, year } missing = none;
  179. unsigned int days;
  180. /* The lower level RTC driver may return -1 in some fields,
  181. * creating invalid alarm->time values, for reasons like:
  182. *
  183. * - The hardware may not be capable of filling them in;
  184. * many alarms match only on time-of-day fields, not
  185. * day/month/year calendar data.
  186. *
  187. * - Some hardware uses illegal values as "wildcard" match
  188. * values, which non-Linux firmware (like a BIOS) may try
  189. * to set up as e.g. "alarm 15 minutes after each hour".
  190. * Linux uses only oneshot alarms.
  191. *
  192. * When we see that here, we deal with it by using values from
  193. * a current RTC timestamp for any missing (-1) values. The
  194. * RTC driver prevents "periodic alarm" modes.
  195. *
  196. * But this can be racey, because some fields of the RTC timestamp
  197. * may have wrapped in the interval since we read the RTC alarm,
  198. * which would lead to us inserting inconsistent values in place
  199. * of the -1 fields.
  200. *
  201. * Reading the alarm and timestamp in the reverse sequence
  202. * would have the same race condition, and not solve the issue.
  203. *
  204. * So, we must first read the RTC timestamp,
  205. * then read the RTC alarm value,
  206. * and then read a second RTC timestamp.
  207. *
  208. * If any fields of the second timestamp have changed
  209. * when compared with the first timestamp, then we know
  210. * our timestamp may be inconsistent with that used by
  211. * the low-level rtc_read_alarm_internal() function.
  212. *
  213. * So, when the two timestamps disagree, we just loop and do
  214. * the process again to get a fully consistent set of values.
  215. *
  216. * This could all instead be done in the lower level driver,
  217. * but since more than one lower level RTC implementation needs it,
  218. * then it's probably best best to do it here instead of there..
  219. */
  220. /* Get the "before" timestamp */
  221. err = rtc_read_time(rtc, &before);
  222. if (err < 0)
  223. return err;
  224. do {
  225. if (!first_time)
  226. memcpy(&before, &now, sizeof(struct rtc_time));
  227. first_time = 0;
  228. /* get the RTC alarm values, which may be incomplete */
  229. err = rtc_read_alarm_internal(rtc, alarm);
  230. if (err)
  231. return err;
  232. /* full-function RTCs won't have such missing fields */
  233. if (rtc_valid_tm(&alarm->time) == 0) {
  234. rtc_add_offset(rtc, &alarm->time);
  235. return 0;
  236. }
  237. /* get the "after" timestamp, to detect wrapped fields */
  238. err = rtc_read_time(rtc, &now);
  239. if (err < 0)
  240. return err;
  241. /* note that tm_sec is a "don't care" value here: */
  242. } while (before.tm_min != now.tm_min ||
  243. before.tm_hour != now.tm_hour ||
  244. before.tm_mon != now.tm_mon ||
  245. before.tm_year != now.tm_year);
  246. /* Fill in the missing alarm fields using the timestamp; we
  247. * know there's at least one since alarm->time is invalid.
  248. */
  249. if (alarm->time.tm_sec == -1)
  250. alarm->time.tm_sec = now.tm_sec;
  251. if (alarm->time.tm_min == -1)
  252. alarm->time.tm_min = now.tm_min;
  253. if (alarm->time.tm_hour == -1)
  254. alarm->time.tm_hour = now.tm_hour;
  255. /* For simplicity, only support date rollover for now */
  256. if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
  257. alarm->time.tm_mday = now.tm_mday;
  258. missing = day;
  259. }
  260. if ((unsigned int)alarm->time.tm_mon >= 12) {
  261. alarm->time.tm_mon = now.tm_mon;
  262. if (missing == none)
  263. missing = month;
  264. }
  265. if (alarm->time.tm_year == -1) {
  266. alarm->time.tm_year = now.tm_year;
  267. if (missing == none)
  268. missing = year;
  269. }
  270. /* Can't proceed if alarm is still invalid after replacing
  271. * missing fields.
  272. */
  273. err = rtc_valid_tm(&alarm->time);
  274. if (err)
  275. goto done;
  276. /* with luck, no rollover is needed */
  277. t_now = rtc_tm_to_time64(&now);
  278. t_alm = rtc_tm_to_time64(&alarm->time);
  279. if (t_now < t_alm)
  280. goto done;
  281. switch (missing) {
  282. /* 24 hour rollover ... if it's now 10am Monday, an alarm that
  283. * that will trigger at 5am will do so at 5am Tuesday, which
  284. * could also be in the next month or year. This is a common
  285. * case, especially for PCs.
  286. */
  287. case day:
  288. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
  289. t_alm += 24 * 60 * 60;
  290. rtc_time64_to_tm(t_alm, &alarm->time);
  291. break;
  292. /* Month rollover ... if it's the 31th, an alarm on the 3rd will
  293. * be next month. An alarm matching on the 30th, 29th, or 28th
  294. * may end up in the month after that! Many newer PCs support
  295. * this type of alarm.
  296. */
  297. case month:
  298. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
  299. do {
  300. if (alarm->time.tm_mon < 11) {
  301. alarm->time.tm_mon++;
  302. } else {
  303. alarm->time.tm_mon = 0;
  304. alarm->time.tm_year++;
  305. }
  306. days = rtc_month_days(alarm->time.tm_mon,
  307. alarm->time.tm_year);
  308. } while (days < alarm->time.tm_mday);
  309. break;
  310. /* Year rollover ... easy except for leap years! */
  311. case year:
  312. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
  313. do {
  314. alarm->time.tm_year++;
  315. } while (!is_leap_year(alarm->time.tm_year + 1900) &&
  316. rtc_valid_tm(&alarm->time) != 0);
  317. break;
  318. default:
  319. dev_warn(&rtc->dev, "alarm rollover not handled\n");
  320. }
  321. err = rtc_valid_tm(&alarm->time);
  322. done:
  323. if (err)
  324. dev_warn(&rtc->dev, "invalid alarm value: %ptR\n",
  325. &alarm->time);
  326. return err;
  327. }
  328. int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  329. {
  330. int err;
  331. err = mutex_lock_interruptible(&rtc->ops_lock);
  332. if (err)
  333. return err;
  334. if (!rtc->ops) {
  335. err = -ENODEV;
  336. } else if (!rtc->ops->read_alarm) {
  337. err = -EINVAL;
  338. } else {
  339. memset(alarm, 0, sizeof(struct rtc_wkalrm));
  340. alarm->enabled = rtc->aie_timer.enabled;
  341. alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
  342. }
  343. mutex_unlock(&rtc->ops_lock);
  344. trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
  345. return err;
  346. }
  347. EXPORT_SYMBOL_GPL(rtc_read_alarm);
  348. static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  349. {
  350. struct rtc_time tm;
  351. time64_t now, scheduled;
  352. int err;
  353. err = rtc_valid_tm(&alarm->time);
  354. if (err)
  355. return err;
  356. scheduled = rtc_tm_to_time64(&alarm->time);
  357. /* Make sure we're not setting alarms in the past */
  358. err = __rtc_read_time(rtc, &tm);
  359. if (err)
  360. return err;
  361. now = rtc_tm_to_time64(&tm);
  362. if (scheduled <= now)
  363. return -ETIME;
  364. /*
  365. * XXX - We just checked to make sure the alarm time is not
  366. * in the past, but there is still a race window where if
  367. * the is alarm set for the next second and the second ticks
  368. * over right here, before we set the alarm.
  369. */
  370. rtc_subtract_offset(rtc, &alarm->time);
  371. if (!rtc->ops)
  372. err = -ENODEV;
  373. else if (!rtc->ops->set_alarm)
  374. err = -EINVAL;
  375. else
  376. err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
  377. trace_rtc_set_alarm(rtc_tm_to_time64(&alarm->time), err);
  378. return err;
  379. }
  380. int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  381. {
  382. int err;
  383. if (!rtc->ops)
  384. return -ENODEV;
  385. else if (!rtc->ops->set_alarm)
  386. return -EINVAL;
  387. err = rtc_valid_tm(&alarm->time);
  388. if (err != 0)
  389. return err;
  390. err = rtc_valid_range(rtc, &alarm->time);
  391. if (err)
  392. return err;
  393. err = mutex_lock_interruptible(&rtc->ops_lock);
  394. if (err)
  395. return err;
  396. if (rtc->aie_timer.enabled)
  397. rtc_timer_remove(rtc, &rtc->aie_timer);
  398. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  399. rtc->aie_timer.period = 0;
  400. if (alarm->enabled)
  401. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  402. mutex_unlock(&rtc->ops_lock);
  403. return err;
  404. }
  405. EXPORT_SYMBOL_GPL(rtc_set_alarm);
  406. /* Called once per device from rtc_device_register */
  407. int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  408. {
  409. int err;
  410. struct rtc_time now;
  411. err = rtc_valid_tm(&alarm->time);
  412. if (err != 0)
  413. return err;
  414. err = rtc_read_time(rtc, &now);
  415. if (err)
  416. return err;
  417. err = mutex_lock_interruptible(&rtc->ops_lock);
  418. if (err)
  419. return err;
  420. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  421. rtc->aie_timer.period = 0;
  422. /* Alarm has to be enabled & in the future for us to enqueue it */
  423. if (alarm->enabled && (rtc_tm_to_ktime(now) <
  424. rtc->aie_timer.node.expires)) {
  425. rtc->aie_timer.enabled = 1;
  426. timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
  427. trace_rtc_timer_enqueue(&rtc->aie_timer);
  428. }
  429. mutex_unlock(&rtc->ops_lock);
  430. return err;
  431. }
  432. EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
  433. int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  434. {
  435. int err;
  436. err = mutex_lock_interruptible(&rtc->ops_lock);
  437. if (err)
  438. return err;
  439. if (rtc->aie_timer.enabled != enabled) {
  440. if (enabled)
  441. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  442. else
  443. rtc_timer_remove(rtc, &rtc->aie_timer);
  444. }
  445. if (err)
  446. /* nothing */;
  447. else if (!rtc->ops)
  448. err = -ENODEV;
  449. else if (!rtc->ops->alarm_irq_enable)
  450. err = -EINVAL;
  451. else
  452. err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
  453. mutex_unlock(&rtc->ops_lock);
  454. trace_rtc_alarm_irq_enable(enabled, err);
  455. return err;
  456. }
  457. EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
  458. int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  459. {
  460. int rc = 0, err;
  461. err = mutex_lock_interruptible(&rtc->ops_lock);
  462. if (err)
  463. return err;
  464. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  465. if (enabled == 0 && rtc->uie_irq_active) {
  466. mutex_unlock(&rtc->ops_lock);
  467. return rtc_dev_update_irq_enable_emul(rtc, 0);
  468. }
  469. #endif
  470. /* make sure we're changing state */
  471. if (rtc->uie_rtctimer.enabled == enabled)
  472. goto out;
  473. if (rtc->uie_unsupported) {
  474. err = -EINVAL;
  475. goto out;
  476. }
  477. if (enabled) {
  478. struct rtc_time tm;
  479. ktime_t now, onesec;
  480. rc = __rtc_read_time(rtc, &tm);
  481. if (rc)
  482. goto out;
  483. onesec = ktime_set(1, 0);
  484. now = rtc_tm_to_ktime(tm);
  485. rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
  486. rtc->uie_rtctimer.period = ktime_set(1, 0);
  487. err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
  488. } else {
  489. rtc_timer_remove(rtc, &rtc->uie_rtctimer);
  490. }
  491. out:
  492. mutex_unlock(&rtc->ops_lock);
  493. /*
  494. * __rtc_read_time() failed, this probably means that the RTC time has
  495. * never been set or less probably there is a transient error on the
  496. * bus. In any case, avoid enabling emulation has this will fail when
  497. * reading the time too.
  498. */
  499. if (rc)
  500. return rc;
  501. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  502. /*
  503. * Enable emulation if the driver returned -EINVAL to signal that it has
  504. * been configured without interrupts or they are not available at the
  505. * moment.
  506. */
  507. if (err == -EINVAL)
  508. err = rtc_dev_update_irq_enable_emul(rtc, enabled);
  509. #endif
  510. return err;
  511. }
  512. EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
  513. /**
  514. * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
  515. * @rtc: pointer to the rtc device
  516. * @num: number of occurence of the event
  517. * @mode: type of the event, RTC_AF, RTC_UF of RTC_PF
  518. *
  519. * This function is called when an AIE, UIE or PIE mode interrupt
  520. * has occurred (or been emulated).
  521. *
  522. */
  523. void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
  524. {
  525. unsigned long flags;
  526. /* mark one irq of the appropriate mode */
  527. spin_lock_irqsave(&rtc->irq_lock, flags);
  528. rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF | mode);
  529. spin_unlock_irqrestore(&rtc->irq_lock, flags);
  530. wake_up_interruptible(&rtc->irq_queue);
  531. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  532. }
  533. /**
  534. * rtc_aie_update_irq - AIE mode rtctimer hook
  535. * @rtc: pointer to the rtc_device
  536. *
  537. * This functions is called when the aie_timer expires.
  538. */
  539. void rtc_aie_update_irq(struct rtc_device *rtc)
  540. {
  541. rtc_handle_legacy_irq(rtc, 1, RTC_AF);
  542. }
  543. /**
  544. * rtc_uie_update_irq - UIE mode rtctimer hook
  545. * @rtc: pointer to the rtc_device
  546. *
  547. * This functions is called when the uie_timer expires.
  548. */
  549. void rtc_uie_update_irq(struct rtc_device *rtc)
  550. {
  551. rtc_handle_legacy_irq(rtc, 1, RTC_UF);
  552. }
  553. /**
  554. * rtc_pie_update_irq - PIE mode hrtimer hook
  555. * @timer: pointer to the pie mode hrtimer
  556. *
  557. * This function is used to emulate PIE mode interrupts
  558. * using an hrtimer. This function is called when the periodic
  559. * hrtimer expires.
  560. */
  561. enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
  562. {
  563. struct rtc_device *rtc;
  564. ktime_t period;
  565. u64 count;
  566. rtc = container_of(timer, struct rtc_device, pie_timer);
  567. period = NSEC_PER_SEC / rtc->irq_freq;
  568. count = hrtimer_forward_now(timer, period);
  569. rtc_handle_legacy_irq(rtc, count, RTC_PF);
  570. return HRTIMER_RESTART;
  571. }
  572. /**
  573. * rtc_update_irq - Triggered when a RTC interrupt occurs.
  574. * @rtc: the rtc device
  575. * @num: how many irqs are being reported (usually one)
  576. * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
  577. * Context: any
  578. */
  579. void rtc_update_irq(struct rtc_device *rtc,
  580. unsigned long num, unsigned long events)
  581. {
  582. if (IS_ERR_OR_NULL(rtc))
  583. return;
  584. pm_stay_awake(rtc->dev.parent);
  585. schedule_work(&rtc->irqwork);
  586. }
  587. EXPORT_SYMBOL_GPL(rtc_update_irq);
  588. struct rtc_device *rtc_class_open(const char *name)
  589. {
  590. struct device *dev;
  591. struct rtc_device *rtc = NULL;
  592. dev = class_find_device_by_name(rtc_class, name);
  593. if (dev)
  594. rtc = to_rtc_device(dev);
  595. if (rtc) {
  596. if (!try_module_get(rtc->owner)) {
  597. put_device(dev);
  598. rtc = NULL;
  599. }
  600. }
  601. return rtc;
  602. }
  603. EXPORT_SYMBOL_GPL(rtc_class_open);
  604. void rtc_class_close(struct rtc_device *rtc)
  605. {
  606. module_put(rtc->owner);
  607. put_device(&rtc->dev);
  608. }
  609. EXPORT_SYMBOL_GPL(rtc_class_close);
  610. static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
  611. {
  612. /*
  613. * We always cancel the timer here first, because otherwise
  614. * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  615. * when we manage to start the timer before the callback
  616. * returns HRTIMER_RESTART.
  617. *
  618. * We cannot use hrtimer_cancel() here as a running callback
  619. * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
  620. * would spin forever.
  621. */
  622. if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
  623. return -1;
  624. if (enabled) {
  625. ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
  626. hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
  627. }
  628. return 0;
  629. }
  630. /**
  631. * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
  632. * @rtc: the rtc device
  633. * @enabled: true to enable periodic IRQs
  634. * Context: any
  635. *
  636. * Note that rtc_irq_set_freq() should previously have been used to
  637. * specify the desired frequency of periodic IRQ.
  638. */
  639. int rtc_irq_set_state(struct rtc_device *rtc, int enabled)
  640. {
  641. int err = 0;
  642. while (rtc_update_hrtimer(rtc, enabled) < 0)
  643. cpu_relax();
  644. rtc->pie_enabled = enabled;
  645. trace_rtc_irq_set_state(enabled, err);
  646. return err;
  647. }
  648. /**
  649. * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
  650. * @rtc: the rtc device
  651. * @freq: positive frequency
  652. * Context: any
  653. *
  654. * Note that rtc_irq_set_state() is used to enable or disable the
  655. * periodic IRQs.
  656. */
  657. int rtc_irq_set_freq(struct rtc_device *rtc, int freq)
  658. {
  659. int err = 0;
  660. if (freq <= 0 || freq > RTC_MAX_FREQ)
  661. return -EINVAL;
  662. rtc->irq_freq = freq;
  663. while (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0)
  664. cpu_relax();
  665. trace_rtc_irq_set_freq(freq, err);
  666. return err;
  667. }
  668. /**
  669. * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
  670. * @rtc: rtc device
  671. * @timer: timer being added.
  672. *
  673. * Enqueues a timer onto the rtc devices timerqueue and sets
  674. * the next alarm event appropriately.
  675. *
  676. * Sets the enabled bit on the added timer.
  677. *
  678. * Must hold ops_lock for proper serialization of timerqueue
  679. */
  680. static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
  681. {
  682. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  683. struct rtc_time tm;
  684. ktime_t now;
  685. int err;
  686. err = __rtc_read_time(rtc, &tm);
  687. if (err)
  688. return err;
  689. timer->enabled = 1;
  690. now = rtc_tm_to_ktime(tm);
  691. /* Skip over expired timers */
  692. while (next) {
  693. if (next->expires >= now)
  694. break;
  695. next = timerqueue_iterate_next(next);
  696. }
  697. timerqueue_add(&rtc->timerqueue, &timer->node);
  698. trace_rtc_timer_enqueue(timer);
  699. if (!next || ktime_before(timer->node.expires, next->expires)) {
  700. struct rtc_wkalrm alarm;
  701. alarm.time = rtc_ktime_to_tm(timer->node.expires);
  702. alarm.enabled = 1;
  703. err = __rtc_set_alarm(rtc, &alarm);
  704. if (err == -ETIME) {
  705. pm_stay_awake(rtc->dev.parent);
  706. schedule_work(&rtc->irqwork);
  707. } else if (err) {
  708. timerqueue_del(&rtc->timerqueue, &timer->node);
  709. trace_rtc_timer_dequeue(timer);
  710. timer->enabled = 0;
  711. return err;
  712. }
  713. }
  714. return 0;
  715. }
  716. static void rtc_alarm_disable(struct rtc_device *rtc)
  717. {
  718. if (!rtc->ops || !rtc->ops->alarm_irq_enable)
  719. return;
  720. rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
  721. trace_rtc_alarm_irq_enable(0, 0);
  722. }
  723. /**
  724. * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
  725. * @rtc: rtc device
  726. * @timer: timer being removed.
  727. *
  728. * Removes a timer onto the rtc devices timerqueue and sets
  729. * the next alarm event appropriately.
  730. *
  731. * Clears the enabled bit on the removed timer.
  732. *
  733. * Must hold ops_lock for proper serialization of timerqueue
  734. */
  735. static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
  736. {
  737. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  738. timerqueue_del(&rtc->timerqueue, &timer->node);
  739. trace_rtc_timer_dequeue(timer);
  740. timer->enabled = 0;
  741. if (next == &timer->node) {
  742. struct rtc_wkalrm alarm;
  743. int err;
  744. next = timerqueue_getnext(&rtc->timerqueue);
  745. if (!next) {
  746. rtc_alarm_disable(rtc);
  747. return;
  748. }
  749. alarm.time = rtc_ktime_to_tm(next->expires);
  750. alarm.enabled = 1;
  751. err = __rtc_set_alarm(rtc, &alarm);
  752. if (err == -ETIME) {
  753. pm_stay_awake(rtc->dev.parent);
  754. schedule_work(&rtc->irqwork);
  755. }
  756. }
  757. }
  758. /**
  759. * rtc_timer_do_work - Expires rtc timers
  760. * @work: work item
  761. *
  762. * Expires rtc timers. Reprograms next alarm event if needed.
  763. * Called via worktask.
  764. *
  765. * Serializes access to timerqueue via ops_lock mutex
  766. */
  767. void rtc_timer_do_work(struct work_struct *work)
  768. {
  769. struct rtc_timer *timer;
  770. struct timerqueue_node *next;
  771. ktime_t now;
  772. struct rtc_time tm;
  773. struct rtc_device *rtc =
  774. container_of(work, struct rtc_device, irqwork);
  775. mutex_lock(&rtc->ops_lock);
  776. again:
  777. __rtc_read_time(rtc, &tm);
  778. now = rtc_tm_to_ktime(tm);
  779. while ((next = timerqueue_getnext(&rtc->timerqueue))) {
  780. if (next->expires > now)
  781. break;
  782. /* expire timer */
  783. timer = container_of(next, struct rtc_timer, node);
  784. timerqueue_del(&rtc->timerqueue, &timer->node);
  785. trace_rtc_timer_dequeue(timer);
  786. timer->enabled = 0;
  787. if (timer->func)
  788. timer->func(timer->rtc);
  789. trace_rtc_timer_fired(timer);
  790. /* Re-add/fwd periodic timers */
  791. if (ktime_to_ns(timer->period)) {
  792. timer->node.expires = ktime_add(timer->node.expires,
  793. timer->period);
  794. timer->enabled = 1;
  795. timerqueue_add(&rtc->timerqueue, &timer->node);
  796. trace_rtc_timer_enqueue(timer);
  797. }
  798. }
  799. /* Set next alarm */
  800. if (next) {
  801. struct rtc_wkalrm alarm;
  802. int err;
  803. int retry = 3;
  804. alarm.time = rtc_ktime_to_tm(next->expires);
  805. alarm.enabled = 1;
  806. reprogram:
  807. err = __rtc_set_alarm(rtc, &alarm);
  808. if (err == -ETIME) {
  809. goto again;
  810. } else if (err) {
  811. if (retry-- > 0)
  812. goto reprogram;
  813. timer = container_of(next, struct rtc_timer, node);
  814. timerqueue_del(&rtc->timerqueue, &timer->node);
  815. trace_rtc_timer_dequeue(timer);
  816. timer->enabled = 0;
  817. dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
  818. goto again;
  819. }
  820. } else {
  821. rtc_alarm_disable(rtc);
  822. }
  823. pm_relax(rtc->dev.parent);
  824. mutex_unlock(&rtc->ops_lock);
  825. }
  826. /* rtc_timer_init - Initializes an rtc_timer
  827. * @timer: timer to be intiialized
  828. * @f: function pointer to be called when timer fires
  829. * @rtc: pointer to the rtc_device
  830. *
  831. * Kernel interface to initializing an rtc_timer.
  832. */
  833. void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
  834. struct rtc_device *rtc)
  835. {
  836. timerqueue_init(&timer->node);
  837. timer->enabled = 0;
  838. timer->func = f;
  839. timer->rtc = rtc;
  840. }
  841. /* rtc_timer_start - Sets an rtc_timer to fire in the future
  842. * @ rtc: rtc device to be used
  843. * @ timer: timer being set
  844. * @ expires: time at which to expire the timer
  845. * @ period: period that the timer will recur
  846. *
  847. * Kernel interface to set an rtc_timer
  848. */
  849. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
  850. ktime_t expires, ktime_t period)
  851. {
  852. int ret = 0;
  853. mutex_lock(&rtc->ops_lock);
  854. if (timer->enabled)
  855. rtc_timer_remove(rtc, timer);
  856. timer->node.expires = expires;
  857. timer->period = period;
  858. ret = rtc_timer_enqueue(rtc, timer);
  859. mutex_unlock(&rtc->ops_lock);
  860. return ret;
  861. }
  862. /* rtc_timer_cancel - Stops an rtc_timer
  863. * @ rtc: rtc device to be used
  864. * @ timer: timer being set
  865. *
  866. * Kernel interface to cancel an rtc_timer
  867. */
  868. void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
  869. {
  870. mutex_lock(&rtc->ops_lock);
  871. if (timer->enabled)
  872. rtc_timer_remove(rtc, timer);
  873. mutex_unlock(&rtc->ops_lock);
  874. }
  875. /**
  876. * rtc_read_offset - Read the amount of rtc offset in parts per billion
  877. * @rtc: rtc device to be used
  878. * @offset: the offset in parts per billion
  879. *
  880. * see below for details.
  881. *
  882. * Kernel interface to read rtc clock offset
  883. * Returns 0 on success, or a negative number on error.
  884. * If read_offset() is not implemented for the rtc, return -EINVAL
  885. */
  886. int rtc_read_offset(struct rtc_device *rtc, long *offset)
  887. {
  888. int ret;
  889. if (!rtc->ops)
  890. return -ENODEV;
  891. if (!rtc->ops->read_offset)
  892. return -EINVAL;
  893. mutex_lock(&rtc->ops_lock);
  894. ret = rtc->ops->read_offset(rtc->dev.parent, offset);
  895. mutex_unlock(&rtc->ops_lock);
  896. trace_rtc_read_offset(*offset, ret);
  897. return ret;
  898. }
  899. /**
  900. * rtc_set_offset - Adjusts the duration of the average second
  901. * @rtc: rtc device to be used
  902. * @offset: the offset in parts per billion
  903. *
  904. * Some rtc's allow an adjustment to the average duration of a second
  905. * to compensate for differences in the actual clock rate due to temperature,
  906. * the crystal, capacitor, etc.
  907. *
  908. * The adjustment applied is as follows:
  909. * t = t0 * (1 + offset * 1e-9)
  910. * where t0 is the measured length of 1 RTC second with offset = 0
  911. *
  912. * Kernel interface to adjust an rtc clock offset.
  913. * Return 0 on success, or a negative number on error.
  914. * If the rtc offset is not setable (or not implemented), return -EINVAL
  915. */
  916. int rtc_set_offset(struct rtc_device *rtc, long offset)
  917. {
  918. int ret;
  919. if (!rtc->ops)
  920. return -ENODEV;
  921. if (!rtc->ops->set_offset)
  922. return -EINVAL;
  923. mutex_lock(&rtc->ops_lock);
  924. ret = rtc->ops->set_offset(rtc->dev.parent, offset);
  925. mutex_unlock(&rtc->ops_lock);
  926. trace_rtc_set_offset(offset, ret);
  927. return ret;
  928. }