interface.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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. */
  505. void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
  506. {
  507. unsigned long flags;
  508. /* mark one irq of the appropriate mode */
  509. spin_lock_irqsave(&rtc->irq_lock, flags);
  510. rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
  511. spin_unlock_irqrestore(&rtc->irq_lock, flags);
  512. wake_up_interruptible(&rtc->irq_queue);
  513. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  514. }
  515. /**
  516. * rtc_aie_update_irq - AIE mode rtctimer hook
  517. * @private: pointer to the rtc_device
  518. *
  519. * This functions is called when the aie_timer expires.
  520. */
  521. void rtc_aie_update_irq(void *private)
  522. {
  523. struct rtc_device *rtc = (struct rtc_device *)private;
  524. rtc_handle_legacy_irq(rtc, 1, RTC_AF);
  525. }
  526. /**
  527. * rtc_uie_update_irq - UIE mode rtctimer hook
  528. * @private: pointer to the rtc_device
  529. *
  530. * This functions is called when the uie_timer expires.
  531. */
  532. void rtc_uie_update_irq(void *private)
  533. {
  534. struct rtc_device *rtc = (struct rtc_device *)private;
  535. rtc_handle_legacy_irq(rtc, 1, RTC_UF);
  536. }
  537. /**
  538. * rtc_pie_update_irq - PIE mode hrtimer hook
  539. * @timer: pointer to the pie mode hrtimer
  540. *
  541. * This function is used to emulate PIE mode interrupts
  542. * using an hrtimer. This function is called when the periodic
  543. * hrtimer expires.
  544. */
  545. enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
  546. {
  547. struct rtc_device *rtc;
  548. ktime_t period;
  549. int count;
  550. rtc = container_of(timer, struct rtc_device, pie_timer);
  551. period = NSEC_PER_SEC / rtc->irq_freq;
  552. count = hrtimer_forward_now(timer, period);
  553. rtc_handle_legacy_irq(rtc, count, RTC_PF);
  554. return HRTIMER_RESTART;
  555. }
  556. /**
  557. * rtc_update_irq - Triggered when a RTC interrupt occurs.
  558. * @rtc: the rtc device
  559. * @num: how many irqs are being reported (usually one)
  560. * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
  561. * Context: any
  562. */
  563. void rtc_update_irq(struct rtc_device *rtc,
  564. unsigned long num, unsigned long events)
  565. {
  566. if (IS_ERR_OR_NULL(rtc))
  567. return;
  568. pm_stay_awake(rtc->dev.parent);
  569. schedule_work(&rtc->irqwork);
  570. }
  571. EXPORT_SYMBOL_GPL(rtc_update_irq);
  572. static int __rtc_match(struct device *dev, const void *data)
  573. {
  574. const char *name = data;
  575. if (strcmp(dev_name(dev), name) == 0)
  576. return 1;
  577. return 0;
  578. }
  579. struct rtc_device *rtc_class_open(const char *name)
  580. {
  581. struct device *dev;
  582. struct rtc_device *rtc = NULL;
  583. dev = class_find_device(rtc_class, NULL, name, __rtc_match);
  584. if (dev)
  585. rtc = to_rtc_device(dev);
  586. if (rtc) {
  587. if (!try_module_get(rtc->owner)) {
  588. put_device(dev);
  589. rtc = NULL;
  590. }
  591. }
  592. return rtc;
  593. }
  594. EXPORT_SYMBOL_GPL(rtc_class_open);
  595. void rtc_class_close(struct rtc_device *rtc)
  596. {
  597. module_put(rtc->owner);
  598. put_device(&rtc->dev);
  599. }
  600. EXPORT_SYMBOL_GPL(rtc_class_close);
  601. static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
  602. {
  603. /*
  604. * We always cancel the timer here first, because otherwise
  605. * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  606. * when we manage to start the timer before the callback
  607. * returns HRTIMER_RESTART.
  608. *
  609. * We cannot use hrtimer_cancel() here as a running callback
  610. * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
  611. * would spin forever.
  612. */
  613. if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
  614. return -1;
  615. if (enabled) {
  616. ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
  617. hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
  618. }
  619. return 0;
  620. }
  621. /**
  622. * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
  623. * @rtc: the rtc device
  624. * @enabled: true to enable periodic IRQs
  625. * Context: any
  626. *
  627. * Note that rtc_irq_set_freq() should previously have been used to
  628. * specify the desired frequency of periodic IRQ.
  629. */
  630. int rtc_irq_set_state(struct rtc_device *rtc, int enabled)
  631. {
  632. int err = 0;
  633. while (rtc_update_hrtimer(rtc, enabled) < 0)
  634. cpu_relax();
  635. rtc->pie_enabled = enabled;
  636. trace_rtc_irq_set_state(enabled, err);
  637. return err;
  638. }
  639. /**
  640. * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
  641. * @rtc: the rtc device
  642. * @freq: positive frequency
  643. * Context: any
  644. *
  645. * Note that rtc_irq_set_state() is used to enable or disable the
  646. * periodic IRQs.
  647. */
  648. int rtc_irq_set_freq(struct rtc_device *rtc, int freq)
  649. {
  650. int err = 0;
  651. if (freq <= 0 || freq > RTC_MAX_FREQ)
  652. return -EINVAL;
  653. rtc->irq_freq = freq;
  654. while (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0)
  655. cpu_relax();
  656. trace_rtc_irq_set_freq(freq, err);
  657. return err;
  658. }
  659. /**
  660. * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
  661. * @rtc rtc device
  662. * @timer timer being added.
  663. *
  664. * Enqueues a timer onto the rtc devices timerqueue and sets
  665. * the next alarm event appropriately.
  666. *
  667. * Sets the enabled bit on the added timer.
  668. *
  669. * Must hold ops_lock for proper serialization of timerqueue
  670. */
  671. static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
  672. {
  673. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  674. struct rtc_time tm;
  675. ktime_t now;
  676. timer->enabled = 1;
  677. __rtc_read_time(rtc, &tm);
  678. now = rtc_tm_to_ktime(tm);
  679. /* Skip over expired timers */
  680. while (next) {
  681. if (next->expires >= now)
  682. break;
  683. next = timerqueue_iterate_next(next);
  684. }
  685. timerqueue_add(&rtc->timerqueue, &timer->node);
  686. trace_rtc_timer_enqueue(timer);
  687. if (!next || ktime_before(timer->node.expires, next->expires)) {
  688. struct rtc_wkalrm alarm;
  689. int err;
  690. alarm.time = rtc_ktime_to_tm(timer->node.expires);
  691. alarm.enabled = 1;
  692. err = __rtc_set_alarm(rtc, &alarm);
  693. if (err == -ETIME) {
  694. pm_stay_awake(rtc->dev.parent);
  695. schedule_work(&rtc->irqwork);
  696. } else if (err) {
  697. timerqueue_del(&rtc->timerqueue, &timer->node);
  698. trace_rtc_timer_dequeue(timer);
  699. timer->enabled = 0;
  700. return err;
  701. }
  702. }
  703. return 0;
  704. }
  705. static void rtc_alarm_disable(struct rtc_device *rtc)
  706. {
  707. if (!rtc->ops || !rtc->ops->alarm_irq_enable)
  708. return;
  709. rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
  710. trace_rtc_alarm_irq_enable(0, 0);
  711. }
  712. /**
  713. * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
  714. * @rtc rtc device
  715. * @timer timer being removed.
  716. *
  717. * Removes a timer onto the rtc devices timerqueue and sets
  718. * the next alarm event appropriately.
  719. *
  720. * Clears the enabled bit on the removed timer.
  721. *
  722. * Must hold ops_lock for proper serialization of timerqueue
  723. */
  724. static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
  725. {
  726. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  727. timerqueue_del(&rtc->timerqueue, &timer->node);
  728. trace_rtc_timer_dequeue(timer);
  729. timer->enabled = 0;
  730. if (next == &timer->node) {
  731. struct rtc_wkalrm alarm;
  732. int err;
  733. next = timerqueue_getnext(&rtc->timerqueue);
  734. if (!next) {
  735. rtc_alarm_disable(rtc);
  736. return;
  737. }
  738. alarm.time = rtc_ktime_to_tm(next->expires);
  739. alarm.enabled = 1;
  740. err = __rtc_set_alarm(rtc, &alarm);
  741. if (err == -ETIME) {
  742. pm_stay_awake(rtc->dev.parent);
  743. schedule_work(&rtc->irqwork);
  744. }
  745. }
  746. }
  747. /**
  748. * rtc_timer_do_work - Expires rtc timers
  749. * @rtc rtc device
  750. * @timer timer being removed.
  751. *
  752. * Expires rtc timers. Reprograms next alarm event if needed.
  753. * Called via worktask.
  754. *
  755. * Serializes access to timerqueue via ops_lock mutex
  756. */
  757. void rtc_timer_do_work(struct work_struct *work)
  758. {
  759. struct rtc_timer *timer;
  760. struct timerqueue_node *next;
  761. ktime_t now;
  762. struct rtc_time tm;
  763. struct rtc_device *rtc =
  764. container_of(work, struct rtc_device, irqwork);
  765. mutex_lock(&rtc->ops_lock);
  766. again:
  767. __rtc_read_time(rtc, &tm);
  768. now = rtc_tm_to_ktime(tm);
  769. while ((next = timerqueue_getnext(&rtc->timerqueue))) {
  770. if (next->expires > now)
  771. break;
  772. /* expire timer */
  773. timer = container_of(next, struct rtc_timer, node);
  774. timerqueue_del(&rtc->timerqueue, &timer->node);
  775. trace_rtc_timer_dequeue(timer);
  776. timer->enabled = 0;
  777. if (timer->func)
  778. timer->func(timer->private_data);
  779. trace_rtc_timer_fired(timer);
  780. /* Re-add/fwd periodic timers */
  781. if (ktime_to_ns(timer->period)) {
  782. timer->node.expires = ktime_add(timer->node.expires,
  783. timer->period);
  784. timer->enabled = 1;
  785. timerqueue_add(&rtc->timerqueue, &timer->node);
  786. trace_rtc_timer_enqueue(timer);
  787. }
  788. }
  789. /* Set next alarm */
  790. if (next) {
  791. struct rtc_wkalrm alarm;
  792. int err;
  793. int retry = 3;
  794. alarm.time = rtc_ktime_to_tm(next->expires);
  795. alarm.enabled = 1;
  796. reprogram:
  797. err = __rtc_set_alarm(rtc, &alarm);
  798. if (err == -ETIME)
  799. goto again;
  800. else if (err) {
  801. if (retry-- > 0)
  802. goto reprogram;
  803. timer = container_of(next, struct rtc_timer, node);
  804. timerqueue_del(&rtc->timerqueue, &timer->node);
  805. trace_rtc_timer_dequeue(timer);
  806. timer->enabled = 0;
  807. dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
  808. goto again;
  809. }
  810. } else
  811. rtc_alarm_disable(rtc);
  812. pm_relax(rtc->dev.parent);
  813. mutex_unlock(&rtc->ops_lock);
  814. }
  815. /* rtc_timer_init - Initializes an rtc_timer
  816. * @timer: timer to be intiialized
  817. * @f: function pointer to be called when timer fires
  818. * @data: private data passed to function pointer
  819. *
  820. * Kernel interface to initializing an rtc_timer.
  821. */
  822. void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data)
  823. {
  824. timerqueue_init(&timer->node);
  825. timer->enabled = 0;
  826. timer->func = f;
  827. timer->private_data = data;
  828. }
  829. /* rtc_timer_start - Sets an rtc_timer to fire in the future
  830. * @ rtc: rtc device to be used
  831. * @ timer: timer being set
  832. * @ expires: time at which to expire the timer
  833. * @ period: period that the timer will recur
  834. *
  835. * Kernel interface to set an rtc_timer
  836. */
  837. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
  838. ktime_t expires, ktime_t period)
  839. {
  840. int ret = 0;
  841. mutex_lock(&rtc->ops_lock);
  842. if (timer->enabled)
  843. rtc_timer_remove(rtc, timer);
  844. timer->node.expires = expires;
  845. timer->period = period;
  846. ret = rtc_timer_enqueue(rtc, timer);
  847. mutex_unlock(&rtc->ops_lock);
  848. return ret;
  849. }
  850. /* rtc_timer_cancel - Stops an rtc_timer
  851. * @ rtc: rtc device to be used
  852. * @ timer: timer being set
  853. *
  854. * Kernel interface to cancel an rtc_timer
  855. */
  856. void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
  857. {
  858. mutex_lock(&rtc->ops_lock);
  859. if (timer->enabled)
  860. rtc_timer_remove(rtc, timer);
  861. mutex_unlock(&rtc->ops_lock);
  862. }
  863. /**
  864. * rtc_read_offset - Read the amount of rtc offset in parts per billion
  865. * @ rtc: rtc device to be used
  866. * @ offset: the offset in parts per billion
  867. *
  868. * see below for details.
  869. *
  870. * Kernel interface to read rtc clock offset
  871. * Returns 0 on success, or a negative number on error.
  872. * If read_offset() is not implemented for the rtc, return -EINVAL
  873. */
  874. int rtc_read_offset(struct rtc_device *rtc, long *offset)
  875. {
  876. int ret;
  877. if (!rtc->ops)
  878. return -ENODEV;
  879. if (!rtc->ops->read_offset)
  880. return -EINVAL;
  881. mutex_lock(&rtc->ops_lock);
  882. ret = rtc->ops->read_offset(rtc->dev.parent, offset);
  883. mutex_unlock(&rtc->ops_lock);
  884. trace_rtc_read_offset(*offset, ret);
  885. return ret;
  886. }
  887. /**
  888. * rtc_set_offset - Adjusts the duration of the average second
  889. * @ rtc: rtc device to be used
  890. * @ offset: the offset in parts per billion
  891. *
  892. * Some rtc's allow an adjustment to the average duration of a second
  893. * to compensate for differences in the actual clock rate due to temperature,
  894. * the crystal, capacitor, etc.
  895. *
  896. * The adjustment applied is as follows:
  897. * t = t0 * (1 + offset * 1e-9)
  898. * where t0 is the measured length of 1 RTC second with offset = 0
  899. *
  900. * Kernel interface to adjust an rtc clock offset.
  901. * Return 0 on success, or a negative number on error.
  902. * If the rtc offset is not setable (or not implemented), return -EINVAL
  903. */
  904. int rtc_set_offset(struct rtc_device *rtc, long offset)
  905. {
  906. int ret;
  907. if (!rtc->ops)
  908. return -ENODEV;
  909. if (!rtc->ops->set_offset)
  910. return -EINVAL;
  911. mutex_lock(&rtc->ops_lock);
  912. ret = rtc->ops->set_offset(rtc->dev.parent, offset);
  913. mutex_unlock(&rtc->ops_lock);
  914. trace_rtc_set_offset(offset, ret);
  915. return ret;
  916. }