interface.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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 (!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(err?0: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 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. err = rtc_valid_tm(&alarm->time);
  234. if (!err)
  235. goto done;
  236. /* get the "after" timestamp, to detect wrapped fields */
  237. err = rtc_read_time(rtc, &now);
  238. if (err < 0)
  239. return err;
  240. /* note that tm_sec is a "don't care" value here: */
  241. } while (before.tm_min != now.tm_min ||
  242. before.tm_hour != now.tm_hour ||
  243. before.tm_mon != now.tm_mon ||
  244. before.tm_year != now.tm_year);
  245. /* Fill in the missing alarm fields using the timestamp; we
  246. * know there's at least one since alarm->time is invalid.
  247. */
  248. if (alarm->time.tm_sec == -1)
  249. alarm->time.tm_sec = now.tm_sec;
  250. if (alarm->time.tm_min == -1)
  251. alarm->time.tm_min = now.tm_min;
  252. if (alarm->time.tm_hour == -1)
  253. alarm->time.tm_hour = now.tm_hour;
  254. /* For simplicity, only support date rollover for now */
  255. if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
  256. alarm->time.tm_mday = now.tm_mday;
  257. missing = day;
  258. }
  259. if ((unsigned int)alarm->time.tm_mon >= 12) {
  260. alarm->time.tm_mon = now.tm_mon;
  261. if (missing == none)
  262. missing = month;
  263. }
  264. if (alarm->time.tm_year == -1) {
  265. alarm->time.tm_year = now.tm_year;
  266. if (missing == none)
  267. missing = year;
  268. }
  269. /* Can't proceed if alarm is still invalid after replacing
  270. * missing fields.
  271. */
  272. err = rtc_valid_tm(&alarm->time);
  273. if (err)
  274. goto done;
  275. /* with luck, no rollover is needed */
  276. t_now = rtc_tm_to_time64(&now);
  277. t_alm = rtc_tm_to_time64(&alarm->time);
  278. if (t_now < t_alm)
  279. goto done;
  280. switch (missing) {
  281. /* 24 hour rollover ... if it's now 10am Monday, an alarm that
  282. * that will trigger at 5am will do so at 5am Tuesday, which
  283. * could also be in the next month or year. This is a common
  284. * case, especially for PCs.
  285. */
  286. case day:
  287. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
  288. t_alm += 24 * 60 * 60;
  289. rtc_time64_to_tm(t_alm, &alarm->time);
  290. break;
  291. /* Month rollover ... if it's the 31th, an alarm on the 3rd will
  292. * be next month. An alarm matching on the 30th, 29th, or 28th
  293. * may end up in the month after that! Many newer PCs support
  294. * this type of alarm.
  295. */
  296. case month:
  297. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
  298. do {
  299. if (alarm->time.tm_mon < 11) {
  300. alarm->time.tm_mon++;
  301. } else {
  302. alarm->time.tm_mon = 0;
  303. alarm->time.tm_year++;
  304. }
  305. days = rtc_month_days(alarm->time.tm_mon,
  306. alarm->time.tm_year);
  307. } while (days < alarm->time.tm_mday);
  308. break;
  309. /* Year rollover ... easy except for leap years! */
  310. case year:
  311. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
  312. do {
  313. alarm->time.tm_year++;
  314. } while (!is_leap_year(alarm->time.tm_year + 1900) &&
  315. rtc_valid_tm(&alarm->time) != 0);
  316. break;
  317. default:
  318. dev_warn(&rtc->dev, "alarm rollover not handled\n");
  319. }
  320. err = rtc_valid_tm(&alarm->time);
  321. done:
  322. if (err && alarm->enabled)
  323. dev_warn(&rtc->dev, "invalid alarm value: %ptR\n",
  324. &alarm->time);
  325. else
  326. rtc_add_offset(rtc, &alarm->time);
  327. return err;
  328. }
  329. int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  330. {
  331. int err;
  332. err = mutex_lock_interruptible(&rtc->ops_lock);
  333. if (err)
  334. return err;
  335. if (!rtc->ops) {
  336. err = -ENODEV;
  337. } else if (!test_bit(RTC_FEATURE_ALARM, rtc->features)) {
  338. err = -EINVAL;
  339. } else {
  340. memset(alarm, 0, sizeof(struct rtc_wkalrm));
  341. alarm->enabled = rtc->aie_timer.enabled;
  342. alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
  343. }
  344. mutex_unlock(&rtc->ops_lock);
  345. trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
  346. return err;
  347. }
  348. EXPORT_SYMBOL_GPL(rtc_read_alarm);
  349. static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  350. {
  351. struct rtc_time tm;
  352. time64_t now, scheduled;
  353. int err;
  354. err = rtc_valid_tm(&alarm->time);
  355. if (err)
  356. return err;
  357. scheduled = rtc_tm_to_time64(&alarm->time);
  358. /* Make sure we're not setting alarms in the past */
  359. err = __rtc_read_time(rtc, &tm);
  360. if (err)
  361. return err;
  362. now = rtc_tm_to_time64(&tm);
  363. if (scheduled <= now)
  364. return -ETIME;
  365. /*
  366. * XXX - We just checked to make sure the alarm time is not
  367. * in the past, but there is still a race window where if
  368. * the is alarm set for the next second and the second ticks
  369. * over right here, before we set the alarm.
  370. */
  371. rtc_subtract_offset(rtc, &alarm->time);
  372. if (!rtc->ops)
  373. err = -ENODEV;
  374. else if (!test_bit(RTC_FEATURE_ALARM, rtc->features))
  375. err = -EINVAL;
  376. else
  377. err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
  378. /*
  379. * Check for potential race described above. If the waiting for next
  380. * second, and the second just ticked since the check above, either
  381. *
  382. * 1) It ticked after the alarm was set, and an alarm irq should be
  383. * generated.
  384. *
  385. * 2) It ticked before the alarm was set, and alarm irq most likely will
  386. * not be generated.
  387. *
  388. * While we cannot easily check for which of these two scenarios we
  389. * are in, we can return -ETIME to signal that the timer has already
  390. * expired, which is true in both cases.
  391. */
  392. if (!err && (scheduled - now) <= 1) {
  393. err = __rtc_read_time(rtc, &tm);
  394. if (err)
  395. return err;
  396. now = rtc_tm_to_time64(&tm);
  397. if (scheduled <= now)
  398. return -ETIME;
  399. }
  400. trace_rtc_set_alarm(rtc_tm_to_time64(&alarm->time), err);
  401. return err;
  402. }
  403. int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  404. {
  405. ktime_t alarm_time;
  406. int err;
  407. if (!rtc->ops)
  408. return -ENODEV;
  409. else if (!test_bit(RTC_FEATURE_ALARM, rtc->features))
  410. return -EINVAL;
  411. err = rtc_valid_tm(&alarm->time);
  412. if (err != 0)
  413. return err;
  414. err = rtc_valid_range(rtc, &alarm->time);
  415. if (err)
  416. return err;
  417. err = mutex_lock_interruptible(&rtc->ops_lock);
  418. if (err)
  419. return err;
  420. if (rtc->aie_timer.enabled)
  421. rtc_timer_remove(rtc, &rtc->aie_timer);
  422. alarm_time = rtc_tm_to_ktime(alarm->time);
  423. /*
  424. * Round down so we never miss a deadline, checking for past deadline is
  425. * done in __rtc_set_alarm
  426. */
  427. if (test_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->features))
  428. alarm_time = ktime_sub_ns(alarm_time, (u64)alarm->time.tm_sec * NSEC_PER_SEC);
  429. rtc->aie_timer.node.expires = alarm_time;
  430. rtc->aie_timer.period = 0;
  431. if (alarm->enabled)
  432. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  433. mutex_unlock(&rtc->ops_lock);
  434. return err;
  435. }
  436. EXPORT_SYMBOL_GPL(rtc_set_alarm);
  437. /* Called once per device from rtc_device_register */
  438. int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  439. {
  440. int err;
  441. struct rtc_time now;
  442. err = rtc_valid_tm(&alarm->time);
  443. if (err != 0)
  444. return err;
  445. err = rtc_read_time(rtc, &now);
  446. if (err)
  447. return err;
  448. err = mutex_lock_interruptible(&rtc->ops_lock);
  449. if (err)
  450. return err;
  451. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  452. rtc->aie_timer.period = 0;
  453. /* Alarm has to be enabled & in the future for us to enqueue it */
  454. if (alarm->enabled && (rtc_tm_to_ktime(now) <
  455. rtc->aie_timer.node.expires)) {
  456. rtc->aie_timer.enabled = 1;
  457. timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
  458. trace_rtc_timer_enqueue(&rtc->aie_timer);
  459. }
  460. mutex_unlock(&rtc->ops_lock);
  461. return err;
  462. }
  463. EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
  464. int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  465. {
  466. int err;
  467. err = mutex_lock_interruptible(&rtc->ops_lock);
  468. if (err)
  469. return err;
  470. if (rtc->aie_timer.enabled != enabled) {
  471. if (enabled)
  472. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  473. else
  474. rtc_timer_remove(rtc, &rtc->aie_timer);
  475. }
  476. if (err)
  477. /* nothing */;
  478. else if (!rtc->ops)
  479. err = -ENODEV;
  480. else if (!test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->alarm_irq_enable)
  481. err = -EINVAL;
  482. else
  483. err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
  484. mutex_unlock(&rtc->ops_lock);
  485. trace_rtc_alarm_irq_enable(enabled, err);
  486. return err;
  487. }
  488. EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
  489. int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  490. {
  491. int err;
  492. err = mutex_lock_interruptible(&rtc->ops_lock);
  493. if (err)
  494. return err;
  495. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  496. if (enabled == 0 && rtc->uie_irq_active) {
  497. mutex_unlock(&rtc->ops_lock);
  498. return rtc_dev_update_irq_enable_emul(rtc, 0);
  499. }
  500. #endif
  501. /* make sure we're changing state */
  502. if (rtc->uie_rtctimer.enabled == enabled)
  503. goto out;
  504. if (!test_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features) ||
  505. !test_bit(RTC_FEATURE_ALARM, rtc->features)) {
  506. mutex_unlock(&rtc->ops_lock);
  507. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  508. return rtc_dev_update_irq_enable_emul(rtc, enabled);
  509. #else
  510. return -EINVAL;
  511. #endif
  512. }
  513. if (enabled) {
  514. struct rtc_time tm;
  515. ktime_t now, onesec;
  516. err = __rtc_read_time(rtc, &tm);
  517. if (err)
  518. goto out;
  519. onesec = ktime_set(1, 0);
  520. now = rtc_tm_to_ktime(tm);
  521. rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
  522. rtc->uie_rtctimer.period = ktime_set(1, 0);
  523. err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
  524. if (!err && rtc->ops && rtc->ops->alarm_irq_enable)
  525. err = rtc->ops->alarm_irq_enable(rtc->dev.parent, 1);
  526. if (err)
  527. goto out;
  528. } else {
  529. rtc_timer_remove(rtc, &rtc->uie_rtctimer);
  530. }
  531. out:
  532. mutex_unlock(&rtc->ops_lock);
  533. return err;
  534. }
  535. EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
  536. /**
  537. * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
  538. * @rtc: pointer to the rtc device
  539. * @num: number of occurence of the event
  540. * @mode: type of the event, RTC_AF, RTC_UF of RTC_PF
  541. *
  542. * This function is called when an AIE, UIE or PIE mode interrupt
  543. * has occurred (or been emulated).
  544. *
  545. */
  546. void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
  547. {
  548. unsigned long flags;
  549. /* mark one irq of the appropriate mode */
  550. spin_lock_irqsave(&rtc->irq_lock, flags);
  551. rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF | mode);
  552. spin_unlock_irqrestore(&rtc->irq_lock, flags);
  553. wake_up_interruptible(&rtc->irq_queue);
  554. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  555. }
  556. /**
  557. * rtc_aie_update_irq - AIE mode rtctimer hook
  558. * @rtc: pointer to the rtc_device
  559. *
  560. * This functions is called when the aie_timer expires.
  561. */
  562. void rtc_aie_update_irq(struct rtc_device *rtc)
  563. {
  564. rtc_handle_legacy_irq(rtc, 1, RTC_AF);
  565. }
  566. /**
  567. * rtc_uie_update_irq - UIE mode rtctimer hook
  568. * @rtc: pointer to the rtc_device
  569. *
  570. * This functions is called when the uie_timer expires.
  571. */
  572. void rtc_uie_update_irq(struct rtc_device *rtc)
  573. {
  574. rtc_handle_legacy_irq(rtc, 1, RTC_UF);
  575. }
  576. /**
  577. * rtc_pie_update_irq - PIE mode hrtimer hook
  578. * @timer: pointer to the pie mode hrtimer
  579. *
  580. * This function is used to emulate PIE mode interrupts
  581. * using an hrtimer. This function is called when the periodic
  582. * hrtimer expires.
  583. */
  584. enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
  585. {
  586. struct rtc_device *rtc;
  587. ktime_t period;
  588. u64 count;
  589. rtc = container_of(timer, struct rtc_device, pie_timer);
  590. period = NSEC_PER_SEC / rtc->irq_freq;
  591. count = hrtimer_forward_now(timer, period);
  592. rtc_handle_legacy_irq(rtc, count, RTC_PF);
  593. return HRTIMER_RESTART;
  594. }
  595. /**
  596. * rtc_update_irq - Triggered when a RTC interrupt occurs.
  597. * @rtc: the rtc device
  598. * @num: how many irqs are being reported (usually one)
  599. * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
  600. * Context: any
  601. */
  602. void rtc_update_irq(struct rtc_device *rtc,
  603. unsigned long num, unsigned long events)
  604. {
  605. if (IS_ERR_OR_NULL(rtc))
  606. return;
  607. pm_stay_awake(rtc->dev.parent);
  608. schedule_work(&rtc->irqwork);
  609. }
  610. EXPORT_SYMBOL_GPL(rtc_update_irq);
  611. struct rtc_device *rtc_class_open(const char *name)
  612. {
  613. struct device *dev;
  614. struct rtc_device *rtc = NULL;
  615. dev = class_find_device_by_name(&rtc_class, name);
  616. if (dev)
  617. rtc = to_rtc_device(dev);
  618. if (rtc) {
  619. if (!try_module_get(rtc->owner)) {
  620. put_device(dev);
  621. rtc = NULL;
  622. }
  623. }
  624. return rtc;
  625. }
  626. EXPORT_SYMBOL_GPL(rtc_class_open);
  627. void rtc_class_close(struct rtc_device *rtc)
  628. {
  629. module_put(rtc->owner);
  630. put_device(&rtc->dev);
  631. }
  632. EXPORT_SYMBOL_GPL(rtc_class_close);
  633. static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
  634. {
  635. /*
  636. * We always cancel the timer here first, because otherwise
  637. * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  638. * when we manage to start the timer before the callback
  639. * returns HRTIMER_RESTART.
  640. *
  641. * We cannot use hrtimer_cancel() here as a running callback
  642. * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
  643. * would spin forever.
  644. */
  645. if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
  646. return -1;
  647. if (enabled) {
  648. ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
  649. hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
  650. }
  651. return 0;
  652. }
  653. /**
  654. * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
  655. * @rtc: the rtc device
  656. * @enabled: true to enable periodic IRQs
  657. * Context: any
  658. *
  659. * Note that rtc_irq_set_freq() should previously have been used to
  660. * specify the desired frequency of periodic IRQ.
  661. */
  662. int rtc_irq_set_state(struct rtc_device *rtc, int enabled)
  663. {
  664. int err = 0;
  665. while (rtc_update_hrtimer(rtc, enabled) < 0)
  666. cpu_relax();
  667. rtc->pie_enabled = enabled;
  668. trace_rtc_irq_set_state(enabled, err);
  669. return err;
  670. }
  671. /**
  672. * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
  673. * @rtc: the rtc device
  674. * @freq: positive frequency
  675. * Context: any
  676. *
  677. * Note that rtc_irq_set_state() is used to enable or disable the
  678. * periodic IRQs.
  679. */
  680. int rtc_irq_set_freq(struct rtc_device *rtc, int freq)
  681. {
  682. int err = 0;
  683. if (freq <= 0 || freq > RTC_MAX_FREQ)
  684. return -EINVAL;
  685. rtc->irq_freq = freq;
  686. while (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0)
  687. cpu_relax();
  688. trace_rtc_irq_set_freq(freq, err);
  689. return err;
  690. }
  691. /**
  692. * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
  693. * @rtc: rtc device
  694. * @timer: timer being added.
  695. *
  696. * Enqueues a timer onto the rtc devices timerqueue and sets
  697. * the next alarm event appropriately.
  698. *
  699. * Sets the enabled bit on the added timer.
  700. *
  701. * Must hold ops_lock for proper serialization of timerqueue
  702. */
  703. static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
  704. {
  705. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  706. struct rtc_time tm;
  707. ktime_t now;
  708. int err;
  709. err = __rtc_read_time(rtc, &tm);
  710. if (err)
  711. return err;
  712. timer->enabled = 1;
  713. now = rtc_tm_to_ktime(tm);
  714. /* Skip over expired timers */
  715. while (next) {
  716. if (next->expires >= now)
  717. break;
  718. next = timerqueue_iterate_next(next);
  719. }
  720. timerqueue_add(&rtc->timerqueue, &timer->node);
  721. trace_rtc_timer_enqueue(timer);
  722. if (!next || ktime_before(timer->node.expires, next->expires)) {
  723. struct rtc_wkalrm alarm;
  724. alarm.time = rtc_ktime_to_tm(timer->node.expires);
  725. alarm.enabled = 1;
  726. err = __rtc_set_alarm(rtc, &alarm);
  727. if (err == -ETIME) {
  728. pm_stay_awake(rtc->dev.parent);
  729. schedule_work(&rtc->irqwork);
  730. } else if (err) {
  731. timerqueue_del(&rtc->timerqueue, &timer->node);
  732. trace_rtc_timer_dequeue(timer);
  733. timer->enabled = 0;
  734. return err;
  735. }
  736. }
  737. return 0;
  738. }
  739. static void rtc_alarm_disable(struct rtc_device *rtc)
  740. {
  741. if (!rtc->ops || !test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->alarm_irq_enable)
  742. return;
  743. rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
  744. trace_rtc_alarm_irq_enable(0, 0);
  745. }
  746. /**
  747. * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
  748. * @rtc: rtc device
  749. * @timer: timer being removed.
  750. *
  751. * Removes a timer onto the rtc devices timerqueue and sets
  752. * the next alarm event appropriately.
  753. *
  754. * Clears the enabled bit on the removed timer.
  755. *
  756. * Must hold ops_lock for proper serialization of timerqueue
  757. */
  758. static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
  759. {
  760. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  761. timerqueue_del(&rtc->timerqueue, &timer->node);
  762. trace_rtc_timer_dequeue(timer);
  763. timer->enabled = 0;
  764. if (next == &timer->node) {
  765. struct rtc_wkalrm alarm;
  766. int err;
  767. next = timerqueue_getnext(&rtc->timerqueue);
  768. if (!next) {
  769. rtc_alarm_disable(rtc);
  770. return;
  771. }
  772. alarm.time = rtc_ktime_to_tm(next->expires);
  773. alarm.enabled = 1;
  774. err = __rtc_set_alarm(rtc, &alarm);
  775. if (err == -ETIME) {
  776. pm_stay_awake(rtc->dev.parent);
  777. schedule_work(&rtc->irqwork);
  778. }
  779. }
  780. }
  781. /**
  782. * rtc_timer_do_work - Expires rtc timers
  783. * @work: work item
  784. *
  785. * Expires rtc timers. Reprograms next alarm event if needed.
  786. * Called via worktask.
  787. *
  788. * Serializes access to timerqueue via ops_lock mutex
  789. */
  790. void rtc_timer_do_work(struct work_struct *work)
  791. {
  792. struct rtc_timer *timer;
  793. struct timerqueue_node *next;
  794. ktime_t now;
  795. struct rtc_time tm;
  796. int err;
  797. struct rtc_device *rtc =
  798. container_of(work, struct rtc_device, irqwork);
  799. mutex_lock(&rtc->ops_lock);
  800. again:
  801. err = __rtc_read_time(rtc, &tm);
  802. if (err) {
  803. mutex_unlock(&rtc->ops_lock);
  804. return;
  805. }
  806. now = rtc_tm_to_ktime(tm);
  807. while ((next = timerqueue_getnext(&rtc->timerqueue))) {
  808. if (next->expires > now)
  809. break;
  810. /* expire timer */
  811. timer = container_of(next, struct rtc_timer, node);
  812. timerqueue_del(&rtc->timerqueue, &timer->node);
  813. trace_rtc_timer_dequeue(timer);
  814. timer->enabled = 0;
  815. if (timer->func)
  816. timer->func(timer->rtc);
  817. trace_rtc_timer_fired(timer);
  818. /* Re-add/fwd periodic timers */
  819. if (ktime_to_ns(timer->period)) {
  820. timer->node.expires = ktime_add(timer->node.expires,
  821. timer->period);
  822. timer->enabled = 1;
  823. timerqueue_add(&rtc->timerqueue, &timer->node);
  824. trace_rtc_timer_enqueue(timer);
  825. }
  826. }
  827. /* Set next alarm */
  828. if (next) {
  829. struct rtc_wkalrm alarm;
  830. int err;
  831. int retry = 3;
  832. alarm.time = rtc_ktime_to_tm(next->expires);
  833. alarm.enabled = 1;
  834. reprogram:
  835. err = __rtc_set_alarm(rtc, &alarm);
  836. if (err == -ETIME) {
  837. goto again;
  838. } else if (err) {
  839. if (retry-- > 0)
  840. goto reprogram;
  841. timer = container_of(next, struct rtc_timer, node);
  842. timerqueue_del(&rtc->timerqueue, &timer->node);
  843. trace_rtc_timer_dequeue(timer);
  844. timer->enabled = 0;
  845. dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
  846. goto again;
  847. }
  848. } else {
  849. rtc_alarm_disable(rtc);
  850. }
  851. pm_relax(rtc->dev.parent);
  852. mutex_unlock(&rtc->ops_lock);
  853. }
  854. /* rtc_timer_init - Initializes an rtc_timer
  855. * @timer: timer to be intiialized
  856. * @f: function pointer to be called when timer fires
  857. * @rtc: pointer to the rtc_device
  858. *
  859. * Kernel interface to initializing an rtc_timer.
  860. */
  861. void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
  862. struct rtc_device *rtc)
  863. {
  864. timerqueue_init(&timer->node);
  865. timer->enabled = 0;
  866. timer->func = f;
  867. timer->rtc = rtc;
  868. }
  869. /* rtc_timer_start - Sets an rtc_timer to fire in the future
  870. * @ rtc: rtc device to be used
  871. * @ timer: timer being set
  872. * @ expires: time at which to expire the timer
  873. * @ period: period that the timer will recur
  874. *
  875. * Kernel interface to set an rtc_timer
  876. */
  877. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
  878. ktime_t expires, ktime_t period)
  879. {
  880. int ret = 0;
  881. mutex_lock(&rtc->ops_lock);
  882. if (timer->enabled)
  883. rtc_timer_remove(rtc, timer);
  884. timer->node.expires = expires;
  885. timer->period = period;
  886. ret = rtc_timer_enqueue(rtc, timer);
  887. mutex_unlock(&rtc->ops_lock);
  888. return ret;
  889. }
  890. /* rtc_timer_cancel - Stops an rtc_timer
  891. * @ rtc: rtc device to be used
  892. * @ timer: timer being set
  893. *
  894. * Kernel interface to cancel an rtc_timer
  895. */
  896. void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
  897. {
  898. mutex_lock(&rtc->ops_lock);
  899. if (timer->enabled)
  900. rtc_timer_remove(rtc, timer);
  901. mutex_unlock(&rtc->ops_lock);
  902. }
  903. /**
  904. * rtc_read_offset - Read the amount of rtc offset in parts per billion
  905. * @rtc: rtc device to be used
  906. * @offset: the offset in parts per billion
  907. *
  908. * see below for details.
  909. *
  910. * Kernel interface to read rtc clock offset
  911. * Returns 0 on success, or a negative number on error.
  912. * If read_offset() is not implemented for the rtc, return -EINVAL
  913. */
  914. int rtc_read_offset(struct rtc_device *rtc, long *offset)
  915. {
  916. int ret;
  917. if (!rtc->ops)
  918. return -ENODEV;
  919. if (!rtc->ops->read_offset)
  920. return -EINVAL;
  921. mutex_lock(&rtc->ops_lock);
  922. ret = rtc->ops->read_offset(rtc->dev.parent, offset);
  923. mutex_unlock(&rtc->ops_lock);
  924. trace_rtc_read_offset(*offset, ret);
  925. return ret;
  926. }
  927. /**
  928. * rtc_set_offset - Adjusts the duration of the average second
  929. * @rtc: rtc device to be used
  930. * @offset: the offset in parts per billion
  931. *
  932. * Some rtc's allow an adjustment to the average duration of a second
  933. * to compensate for differences in the actual clock rate due to temperature,
  934. * the crystal, capacitor, etc.
  935. *
  936. * The adjustment applied is as follows:
  937. * t = t0 * (1 + offset * 1e-9)
  938. * where t0 is the measured length of 1 RTC second with offset = 0
  939. *
  940. * Kernel interface to adjust an rtc clock offset.
  941. * Return 0 on success, or a negative number on error.
  942. * If the rtc offset is not setable (or not implemented), return -EINVAL
  943. */
  944. int rtc_set_offset(struct rtc_device *rtc, long offset)
  945. {
  946. int ret;
  947. if (!rtc->ops)
  948. return -ENODEV;
  949. if (!rtc->ops->set_offset)
  950. return -EINVAL;
  951. mutex_lock(&rtc->ops_lock);
  952. ret = rtc->ops->set_offset(rtc->dev.parent, offset);
  953. mutex_unlock(&rtc->ops_lock);
  954. trace_rtc_set_offset(offset, ret);
  955. return ret;
  956. }