interface.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * RTC subsystem, interface functions
  4. *
  5. * Copyright (C) 2005 Tower Technologies
  6. * Author: Alessandro Zummo <[email protected]>
  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 (!test_bit(RTC_FEATURE_ALARM, rtc->features) || !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 (!test_bit(RTC_FEATURE_ALARM, rtc->features)) {
  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 (!test_bit(RTC_FEATURE_ALARM, rtc->features))
  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. ktime_t alarm_time;
  383. int err;
  384. if (!rtc->ops)
  385. return -ENODEV;
  386. else if (!test_bit(RTC_FEATURE_ALARM, rtc->features))
  387. return -EINVAL;
  388. err = rtc_valid_tm(&alarm->time);
  389. if (err != 0)
  390. return err;
  391. err = rtc_valid_range(rtc, &alarm->time);
  392. if (err)
  393. return err;
  394. err = mutex_lock_interruptible(&rtc->ops_lock);
  395. if (err)
  396. return err;
  397. if (rtc->aie_timer.enabled)
  398. rtc_timer_remove(rtc, &rtc->aie_timer);
  399. alarm_time = rtc_tm_to_ktime(alarm->time);
  400. /*
  401. * Round down so we never miss a deadline, checking for past deadline is
  402. * done in __rtc_set_alarm
  403. */
  404. if (test_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->features))
  405. alarm_time = ktime_sub_ns(alarm_time, (u64)alarm->time.tm_sec * NSEC_PER_SEC);
  406. rtc->aie_timer.node.expires = alarm_time;
  407. rtc->aie_timer.period = 0;
  408. if (alarm->enabled)
  409. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  410. mutex_unlock(&rtc->ops_lock);
  411. return err;
  412. }
  413. EXPORT_SYMBOL_GPL(rtc_set_alarm);
  414. /* Called once per device from rtc_device_register */
  415. int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  416. {
  417. int err;
  418. struct rtc_time now;
  419. err = rtc_valid_tm(&alarm->time);
  420. if (err != 0)
  421. return err;
  422. err = rtc_read_time(rtc, &now);
  423. if (err)
  424. return err;
  425. err = mutex_lock_interruptible(&rtc->ops_lock);
  426. if (err)
  427. return err;
  428. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  429. rtc->aie_timer.period = 0;
  430. /* Alarm has to be enabled & in the future for us to enqueue it */
  431. if (alarm->enabled && (rtc_tm_to_ktime(now) <
  432. rtc->aie_timer.node.expires)) {
  433. rtc->aie_timer.enabled = 1;
  434. timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
  435. trace_rtc_timer_enqueue(&rtc->aie_timer);
  436. }
  437. mutex_unlock(&rtc->ops_lock);
  438. return err;
  439. }
  440. EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
  441. int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  442. {
  443. int err;
  444. err = mutex_lock_interruptible(&rtc->ops_lock);
  445. if (err)
  446. return err;
  447. if (rtc->aie_timer.enabled != enabled) {
  448. if (enabled)
  449. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  450. else
  451. rtc_timer_remove(rtc, &rtc->aie_timer);
  452. }
  453. if (err)
  454. /* nothing */;
  455. else if (!rtc->ops)
  456. err = -ENODEV;
  457. else if (!test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->alarm_irq_enable)
  458. err = -EINVAL;
  459. else
  460. err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
  461. mutex_unlock(&rtc->ops_lock);
  462. trace_rtc_alarm_irq_enable(enabled, err);
  463. return err;
  464. }
  465. EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
  466. int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  467. {
  468. int err;
  469. err = mutex_lock_interruptible(&rtc->ops_lock);
  470. if (err)
  471. return err;
  472. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  473. if (enabled == 0 && rtc->uie_irq_active) {
  474. mutex_unlock(&rtc->ops_lock);
  475. return rtc_dev_update_irq_enable_emul(rtc, 0);
  476. }
  477. #endif
  478. /* make sure we're changing state */
  479. if (rtc->uie_rtctimer.enabled == enabled)
  480. goto out;
  481. if (!test_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features) ||
  482. !test_bit(RTC_FEATURE_ALARM, rtc->features)) {
  483. mutex_unlock(&rtc->ops_lock);
  484. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  485. return rtc_dev_update_irq_enable_emul(rtc, enabled);
  486. #else
  487. return -EINVAL;
  488. #endif
  489. }
  490. if (enabled) {
  491. struct rtc_time tm;
  492. ktime_t now, onesec;
  493. err = __rtc_read_time(rtc, &tm);
  494. if (err)
  495. goto out;
  496. onesec = ktime_set(1, 0);
  497. now = rtc_tm_to_ktime(tm);
  498. rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
  499. rtc->uie_rtctimer.period = ktime_set(1, 0);
  500. err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
  501. } else {
  502. rtc_timer_remove(rtc, &rtc->uie_rtctimer);
  503. }
  504. out:
  505. mutex_unlock(&rtc->ops_lock);
  506. return err;
  507. }
  508. EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
  509. /**
  510. * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
  511. * @rtc: pointer to the rtc device
  512. * @num: number of occurence of the event
  513. * @mode: type of the event, RTC_AF, RTC_UF of RTC_PF
  514. *
  515. * This function is called when an AIE, UIE or PIE mode interrupt
  516. * has occurred (or been emulated).
  517. *
  518. */
  519. void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
  520. {
  521. unsigned long flags;
  522. /* mark one irq of the appropriate mode */
  523. spin_lock_irqsave(&rtc->irq_lock, flags);
  524. rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF | mode);
  525. spin_unlock_irqrestore(&rtc->irq_lock, flags);
  526. wake_up_interruptible(&rtc->irq_queue);
  527. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  528. }
  529. /**
  530. * rtc_aie_update_irq - AIE mode rtctimer hook
  531. * @rtc: pointer to the rtc_device
  532. *
  533. * This functions is called when the aie_timer expires.
  534. */
  535. void rtc_aie_update_irq(struct rtc_device *rtc)
  536. {
  537. rtc_handle_legacy_irq(rtc, 1, RTC_AF);
  538. }
  539. /**
  540. * rtc_uie_update_irq - UIE mode rtctimer hook
  541. * @rtc: pointer to the rtc_device
  542. *
  543. * This functions is called when the uie_timer expires.
  544. */
  545. void rtc_uie_update_irq(struct rtc_device *rtc)
  546. {
  547. rtc_handle_legacy_irq(rtc, 1, RTC_UF);
  548. }
  549. /**
  550. * rtc_pie_update_irq - PIE mode hrtimer hook
  551. * @timer: pointer to the pie mode hrtimer
  552. *
  553. * This function is used to emulate PIE mode interrupts
  554. * using an hrtimer. This function is called when the periodic
  555. * hrtimer expires.
  556. */
  557. enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
  558. {
  559. struct rtc_device *rtc;
  560. ktime_t period;
  561. u64 count;
  562. rtc = container_of(timer, struct rtc_device, pie_timer);
  563. period = NSEC_PER_SEC / rtc->irq_freq;
  564. count = hrtimer_forward_now(timer, period);
  565. rtc_handle_legacy_irq(rtc, count, RTC_PF);
  566. return HRTIMER_RESTART;
  567. }
  568. /**
  569. * rtc_update_irq - Triggered when a RTC interrupt occurs.
  570. * @rtc: the rtc device
  571. * @num: how many irqs are being reported (usually one)
  572. * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
  573. * Context: any
  574. */
  575. void rtc_update_irq(struct rtc_device *rtc,
  576. unsigned long num, unsigned long events)
  577. {
  578. if (IS_ERR_OR_NULL(rtc))
  579. return;
  580. pm_stay_awake(rtc->dev.parent);
  581. schedule_work(&rtc->irqwork);
  582. }
  583. EXPORT_SYMBOL_GPL(rtc_update_irq);
  584. struct rtc_device *rtc_class_open(const char *name)
  585. {
  586. struct device *dev;
  587. struct rtc_device *rtc = NULL;
  588. dev = class_find_device_by_name(rtc_class, name);
  589. if (dev)
  590. rtc = to_rtc_device(dev);
  591. if (rtc) {
  592. if (!try_module_get(rtc->owner)) {
  593. put_device(dev);
  594. rtc = NULL;
  595. }
  596. }
  597. return rtc;
  598. }
  599. EXPORT_SYMBOL_GPL(rtc_class_open);
  600. void rtc_class_close(struct rtc_device *rtc)
  601. {
  602. module_put(rtc->owner);
  603. put_device(&rtc->dev);
  604. }
  605. EXPORT_SYMBOL_GPL(rtc_class_close);
  606. static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
  607. {
  608. /*
  609. * We always cancel the timer here first, because otherwise
  610. * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  611. * when we manage to start the timer before the callback
  612. * returns HRTIMER_RESTART.
  613. *
  614. * We cannot use hrtimer_cancel() here as a running callback
  615. * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
  616. * would spin forever.
  617. */
  618. if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
  619. return -1;
  620. if (enabled) {
  621. ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
  622. hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
  623. }
  624. return 0;
  625. }
  626. /**
  627. * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
  628. * @rtc: the rtc device
  629. * @enabled: true to enable periodic IRQs
  630. * Context: any
  631. *
  632. * Note that rtc_irq_set_freq() should previously have been used to
  633. * specify the desired frequency of periodic IRQ.
  634. */
  635. int rtc_irq_set_state(struct rtc_device *rtc, int enabled)
  636. {
  637. int err = 0;
  638. while (rtc_update_hrtimer(rtc, enabled) < 0)
  639. cpu_relax();
  640. rtc->pie_enabled = enabled;
  641. trace_rtc_irq_set_state(enabled, err);
  642. return err;
  643. }
  644. /**
  645. * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
  646. * @rtc: the rtc device
  647. * @freq: positive frequency
  648. * Context: any
  649. *
  650. * Note that rtc_irq_set_state() is used to enable or disable the
  651. * periodic IRQs.
  652. */
  653. int rtc_irq_set_freq(struct rtc_device *rtc, int freq)
  654. {
  655. int err = 0;
  656. if (freq <= 0 || freq > RTC_MAX_FREQ)
  657. return -EINVAL;
  658. rtc->irq_freq = freq;
  659. while (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0)
  660. cpu_relax();
  661. trace_rtc_irq_set_freq(freq, err);
  662. return err;
  663. }
  664. /**
  665. * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
  666. * @rtc: rtc device
  667. * @timer: timer being added.
  668. *
  669. * Enqueues a timer onto the rtc devices timerqueue and sets
  670. * the next alarm event appropriately.
  671. *
  672. * Sets the enabled bit on the added timer.
  673. *
  674. * Must hold ops_lock for proper serialization of timerqueue
  675. */
  676. static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
  677. {
  678. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  679. struct rtc_time tm;
  680. ktime_t now;
  681. int err;
  682. err = __rtc_read_time(rtc, &tm);
  683. if (err)
  684. return err;
  685. timer->enabled = 1;
  686. now = rtc_tm_to_ktime(tm);
  687. /* Skip over expired timers */
  688. while (next) {
  689. if (next->expires >= now)
  690. break;
  691. next = timerqueue_iterate_next(next);
  692. }
  693. timerqueue_add(&rtc->timerqueue, &timer->node);
  694. trace_rtc_timer_enqueue(timer);
  695. if (!next || ktime_before(timer->node.expires, next->expires)) {
  696. struct rtc_wkalrm alarm;
  697. alarm.time = rtc_ktime_to_tm(timer->node.expires);
  698. alarm.enabled = 1;
  699. err = __rtc_set_alarm(rtc, &alarm);
  700. if (err == -ETIME) {
  701. pm_stay_awake(rtc->dev.parent);
  702. schedule_work(&rtc->irqwork);
  703. } else if (err) {
  704. timerqueue_del(&rtc->timerqueue, &timer->node);
  705. trace_rtc_timer_dequeue(timer);
  706. timer->enabled = 0;
  707. return err;
  708. }
  709. }
  710. return 0;
  711. }
  712. static void rtc_alarm_disable(struct rtc_device *rtc)
  713. {
  714. if (!rtc->ops || !test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->alarm_irq_enable)
  715. return;
  716. rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
  717. trace_rtc_alarm_irq_enable(0, 0);
  718. }
  719. /**
  720. * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
  721. * @rtc: rtc device
  722. * @timer: timer being removed.
  723. *
  724. * Removes a timer onto the rtc devices timerqueue and sets
  725. * the next alarm event appropriately.
  726. *
  727. * Clears the enabled bit on the removed timer.
  728. *
  729. * Must hold ops_lock for proper serialization of timerqueue
  730. */
  731. static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
  732. {
  733. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  734. timerqueue_del(&rtc->timerqueue, &timer->node);
  735. trace_rtc_timer_dequeue(timer);
  736. timer->enabled = 0;
  737. if (next == &timer->node) {
  738. struct rtc_wkalrm alarm;
  739. int err;
  740. next = timerqueue_getnext(&rtc->timerqueue);
  741. if (!next) {
  742. rtc_alarm_disable(rtc);
  743. return;
  744. }
  745. alarm.time = rtc_ktime_to_tm(next->expires);
  746. alarm.enabled = 1;
  747. err = __rtc_set_alarm(rtc, &alarm);
  748. if (err == -ETIME) {
  749. pm_stay_awake(rtc->dev.parent);
  750. schedule_work(&rtc->irqwork);
  751. }
  752. }
  753. }
  754. /**
  755. * rtc_timer_do_work - Expires rtc timers
  756. * @work: work item
  757. *
  758. * Expires rtc timers. Reprograms next alarm event if needed.
  759. * Called via worktask.
  760. *
  761. * Serializes access to timerqueue via ops_lock mutex
  762. */
  763. void rtc_timer_do_work(struct work_struct *work)
  764. {
  765. struct rtc_timer *timer;
  766. struct timerqueue_node *next;
  767. ktime_t now;
  768. struct rtc_time tm;
  769. struct rtc_device *rtc =
  770. container_of(work, struct rtc_device, irqwork);
  771. mutex_lock(&rtc->ops_lock);
  772. again:
  773. __rtc_read_time(rtc, &tm);
  774. now = rtc_tm_to_ktime(tm);
  775. while ((next = timerqueue_getnext(&rtc->timerqueue))) {
  776. if (next->expires > now)
  777. break;
  778. /* expire timer */
  779. timer = container_of(next, struct rtc_timer, node);
  780. timerqueue_del(&rtc->timerqueue, &timer->node);
  781. trace_rtc_timer_dequeue(timer);
  782. timer->enabled = 0;
  783. if (timer->func)
  784. timer->func(timer->rtc);
  785. trace_rtc_timer_fired(timer);
  786. /* Re-add/fwd periodic timers */
  787. if (ktime_to_ns(timer->period)) {
  788. timer->node.expires = ktime_add(timer->node.expires,
  789. timer->period);
  790. timer->enabled = 1;
  791. timerqueue_add(&rtc->timerqueue, &timer->node);
  792. trace_rtc_timer_enqueue(timer);
  793. }
  794. }
  795. /* Set next alarm */
  796. if (next) {
  797. struct rtc_wkalrm alarm;
  798. int err;
  799. int retry = 3;
  800. alarm.time = rtc_ktime_to_tm(next->expires);
  801. alarm.enabled = 1;
  802. reprogram:
  803. err = __rtc_set_alarm(rtc, &alarm);
  804. if (err == -ETIME) {
  805. goto again;
  806. } else if (err) {
  807. if (retry-- > 0)
  808. goto reprogram;
  809. timer = container_of(next, struct rtc_timer, node);
  810. timerqueue_del(&rtc->timerqueue, &timer->node);
  811. trace_rtc_timer_dequeue(timer);
  812. timer->enabled = 0;
  813. dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
  814. goto again;
  815. }
  816. } else {
  817. rtc_alarm_disable(rtc);
  818. }
  819. pm_relax(rtc->dev.parent);
  820. mutex_unlock(&rtc->ops_lock);
  821. }
  822. /* rtc_timer_init - Initializes an rtc_timer
  823. * @timer: timer to be intiialized
  824. * @f: function pointer to be called when timer fires
  825. * @rtc: pointer to the rtc_device
  826. *
  827. * Kernel interface to initializing an rtc_timer.
  828. */
  829. void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
  830. struct rtc_device *rtc)
  831. {
  832. timerqueue_init(&timer->node);
  833. timer->enabled = 0;
  834. timer->func = f;
  835. timer->rtc = rtc;
  836. }
  837. /* rtc_timer_start - Sets an rtc_timer to fire in the future
  838. * @ rtc: rtc device to be used
  839. * @ timer: timer being set
  840. * @ expires: time at which to expire the timer
  841. * @ period: period that the timer will recur
  842. *
  843. * Kernel interface to set an rtc_timer
  844. */
  845. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
  846. ktime_t expires, ktime_t period)
  847. {
  848. int ret = 0;
  849. mutex_lock(&rtc->ops_lock);
  850. if (timer->enabled)
  851. rtc_timer_remove(rtc, timer);
  852. timer->node.expires = expires;
  853. timer->period = period;
  854. ret = rtc_timer_enqueue(rtc, timer);
  855. mutex_unlock(&rtc->ops_lock);
  856. return ret;
  857. }
  858. /* rtc_timer_cancel - Stops an rtc_timer
  859. * @ rtc: rtc device to be used
  860. * @ timer: timer being set
  861. *
  862. * Kernel interface to cancel an rtc_timer
  863. */
  864. void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
  865. {
  866. mutex_lock(&rtc->ops_lock);
  867. if (timer->enabled)
  868. rtc_timer_remove(rtc, timer);
  869. mutex_unlock(&rtc->ops_lock);
  870. }
  871. /**
  872. * rtc_read_offset - Read the amount of rtc offset in parts per billion
  873. * @rtc: rtc device to be used
  874. * @offset: the offset in parts per billion
  875. *
  876. * see below for details.
  877. *
  878. * Kernel interface to read rtc clock offset
  879. * Returns 0 on success, or a negative number on error.
  880. * If read_offset() is not implemented for the rtc, return -EINVAL
  881. */
  882. int rtc_read_offset(struct rtc_device *rtc, long *offset)
  883. {
  884. int ret;
  885. if (!rtc->ops)
  886. return -ENODEV;
  887. if (!rtc->ops->read_offset)
  888. return -EINVAL;
  889. mutex_lock(&rtc->ops_lock);
  890. ret = rtc->ops->read_offset(rtc->dev.parent, offset);
  891. mutex_unlock(&rtc->ops_lock);
  892. trace_rtc_read_offset(*offset, ret);
  893. return ret;
  894. }
  895. /**
  896. * rtc_set_offset - Adjusts the duration of the average second
  897. * @rtc: rtc device to be used
  898. * @offset: the offset in parts per billion
  899. *
  900. * Some rtc's allow an adjustment to the average duration of a second
  901. * to compensate for differences in the actual clock rate due to temperature,
  902. * the crystal, capacitor, etc.
  903. *
  904. * The adjustment applied is as follows:
  905. * t = t0 * (1 + offset * 1e-9)
  906. * where t0 is the measured length of 1 RTC second with offset = 0
  907. *
  908. * Kernel interface to adjust an rtc clock offset.
  909. * Return 0 on success, or a negative number on error.
  910. * If the rtc offset is not setable (or not implemented), return -EINVAL
  911. */
  912. int rtc_set_offset(struct rtc_device *rtc, long offset)
  913. {
  914. int ret;
  915. if (!rtc->ops)
  916. return -ENODEV;
  917. if (!rtc->ops->set_offset)
  918. return -EINVAL;
  919. mutex_lock(&rtc->ops_lock);
  920. ret = rtc->ops->set_offset(rtc->dev.parent, offset);
  921. mutex_unlock(&rtc->ops_lock);
  922. trace_rtc_set_offset(offset, ret);
  923. return ret;
  924. }