interface.c 26 KB

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