interface.c 27 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. return 0;
  228. /* get the "after" timestamp, to detect wrapped fields */
  229. err = rtc_read_time(rtc, &now);
  230. if (err < 0)
  231. return err;
  232. /* note that tm_sec is a "don't care" value here: */
  233. } while ( before.tm_min != now.tm_min
  234. || before.tm_hour != now.tm_hour
  235. || before.tm_mon != now.tm_mon
  236. || before.tm_year != now.tm_year);
  237. /* Fill in the missing alarm fields using the timestamp; we
  238. * know there's at least one since alarm->time is invalid.
  239. */
  240. if (alarm->time.tm_sec == -1)
  241. alarm->time.tm_sec = now.tm_sec;
  242. if (alarm->time.tm_min == -1)
  243. alarm->time.tm_min = now.tm_min;
  244. if (alarm->time.tm_hour == -1)
  245. alarm->time.tm_hour = now.tm_hour;
  246. /* For simplicity, only support date rollover for now */
  247. if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
  248. alarm->time.tm_mday = now.tm_mday;
  249. missing = day;
  250. }
  251. if ((unsigned)alarm->time.tm_mon >= 12) {
  252. alarm->time.tm_mon = now.tm_mon;
  253. if (missing == none)
  254. missing = month;
  255. }
  256. if (alarm->time.tm_year == -1) {
  257. alarm->time.tm_year = now.tm_year;
  258. if (missing == none)
  259. missing = year;
  260. }
  261. /* Can't proceed if alarm is still invalid after replacing
  262. * missing fields.
  263. */
  264. err = rtc_valid_tm(&alarm->time);
  265. if (err)
  266. goto done;
  267. /* with luck, no rollover is needed */
  268. t_now = rtc_tm_to_time64(&now);
  269. t_alm = rtc_tm_to_time64(&alarm->time);
  270. if (t_now < t_alm)
  271. goto done;
  272. switch (missing) {
  273. /* 24 hour rollover ... if it's now 10am Monday, an alarm that
  274. * that will trigger at 5am will do so at 5am Tuesday, which
  275. * could also be in the next month or year. This is a common
  276. * case, especially for PCs.
  277. */
  278. case day:
  279. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
  280. t_alm += 24 * 60 * 60;
  281. rtc_time64_to_tm(t_alm, &alarm->time);
  282. break;
  283. /* Month rollover ... if it's the 31th, an alarm on the 3rd will
  284. * be next month. An alarm matching on the 30th, 29th, or 28th
  285. * may end up in the month after that! Many newer PCs support
  286. * this type of alarm.
  287. */
  288. case month:
  289. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
  290. do {
  291. if (alarm->time.tm_mon < 11)
  292. alarm->time.tm_mon++;
  293. else {
  294. alarm->time.tm_mon = 0;
  295. alarm->time.tm_year++;
  296. }
  297. days = rtc_month_days(alarm->time.tm_mon,
  298. alarm->time.tm_year);
  299. } while (days < alarm->time.tm_mday);
  300. break;
  301. /* Year rollover ... easy except for leap years! */
  302. case year:
  303. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
  304. do {
  305. alarm->time.tm_year++;
  306. } while (!is_leap_year(alarm->time.tm_year + 1900)
  307. && rtc_valid_tm(&alarm->time) != 0);
  308. break;
  309. default:
  310. dev_warn(&rtc->dev, "alarm rollover not handled\n");
  311. }
  312. err = rtc_valid_tm(&alarm->time);
  313. done:
  314. if (err) {
  315. dev_warn(&rtc->dev, "invalid alarm value: %d-%d-%d %d:%d:%d\n",
  316. alarm->time.tm_year + 1900, alarm->time.tm_mon + 1,
  317. alarm->time.tm_mday, alarm->time.tm_hour, alarm->time.tm_min,
  318. alarm->time.tm_sec);
  319. }
  320. return err;
  321. }
  322. int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  323. {
  324. int err;
  325. err = mutex_lock_interruptible(&rtc->ops_lock);
  326. if (err)
  327. return err;
  328. if (rtc->ops == NULL)
  329. err = -ENODEV;
  330. else if (!rtc->ops->read_alarm)
  331. err = -EINVAL;
  332. else {
  333. memset(alarm, 0, sizeof(struct rtc_wkalrm));
  334. alarm->enabled = rtc->aie_timer.enabled;
  335. alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
  336. }
  337. mutex_unlock(&rtc->ops_lock);
  338. trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
  339. return err;
  340. }
  341. EXPORT_SYMBOL_GPL(rtc_read_alarm);
  342. static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  343. {
  344. struct rtc_time tm;
  345. time64_t now, scheduled;
  346. int err;
  347. err = rtc_valid_tm(&alarm->time);
  348. if (err)
  349. return err;
  350. rtc_subtract_offset(rtc, &alarm->time);
  351. scheduled = rtc_tm_to_time64(&alarm->time);
  352. /* Make sure we're not setting alarms in the past */
  353. err = __rtc_read_time(rtc, &tm);
  354. if (err)
  355. return err;
  356. now = rtc_tm_to_time64(&tm);
  357. if (scheduled <= now)
  358. return -ETIME;
  359. /*
  360. * XXX - We just checked to make sure the alarm time is not
  361. * in the past, but there is still a race window where if
  362. * the is alarm set for the next second and the second ticks
  363. * over right here, before we set the alarm.
  364. */
  365. if (!rtc->ops)
  366. err = -ENODEV;
  367. else if (!rtc->ops->set_alarm)
  368. err = -EINVAL;
  369. else
  370. err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
  371. trace_rtc_set_alarm(rtc_tm_to_time64(&alarm->time), err);
  372. return err;
  373. }
  374. int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  375. {
  376. int err;
  377. if (!rtc->ops)
  378. return -ENODEV;
  379. else if (!rtc->ops->set_alarm)
  380. return -EINVAL;
  381. err = rtc_valid_tm(&alarm->time);
  382. if (err != 0)
  383. return err;
  384. err = rtc_valid_range(rtc, &alarm->time);
  385. if (err)
  386. return err;
  387. err = mutex_lock_interruptible(&rtc->ops_lock);
  388. if (err)
  389. return err;
  390. if (rtc->aie_timer.enabled)
  391. rtc_timer_remove(rtc, &rtc->aie_timer);
  392. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  393. rtc->aie_timer.period = 0;
  394. if (alarm->enabled)
  395. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  396. mutex_unlock(&rtc->ops_lock);
  397. rtc_add_offset(rtc, &alarm->time);
  398. return err;
  399. }
  400. EXPORT_SYMBOL_GPL(rtc_set_alarm);
  401. /* Called once per device from rtc_device_register */
  402. int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  403. {
  404. int err;
  405. struct rtc_time now;
  406. err = rtc_valid_tm(&alarm->time);
  407. if (err != 0)
  408. return err;
  409. err = rtc_read_time(rtc, &now);
  410. if (err)
  411. return err;
  412. err = mutex_lock_interruptible(&rtc->ops_lock);
  413. if (err)
  414. return err;
  415. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  416. rtc->aie_timer.period = 0;
  417. /* Alarm has to be enabled & in the future for us to enqueue it */
  418. if (alarm->enabled && (rtc_tm_to_ktime(now) <
  419. rtc->aie_timer.node.expires)) {
  420. rtc->aie_timer.enabled = 1;
  421. timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
  422. trace_rtc_timer_enqueue(&rtc->aie_timer);
  423. }
  424. mutex_unlock(&rtc->ops_lock);
  425. return err;
  426. }
  427. EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
  428. int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  429. {
  430. int err = mutex_lock_interruptible(&rtc->ops_lock);
  431. if (err)
  432. return err;
  433. if (rtc->aie_timer.enabled != enabled) {
  434. if (enabled)
  435. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  436. else
  437. rtc_timer_remove(rtc, &rtc->aie_timer);
  438. }
  439. if (err)
  440. /* nothing */;
  441. else if (!rtc->ops)
  442. err = -ENODEV;
  443. else if (!rtc->ops->alarm_irq_enable)
  444. err = -EINVAL;
  445. else
  446. err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
  447. mutex_unlock(&rtc->ops_lock);
  448. trace_rtc_alarm_irq_enable(enabled, err);
  449. return err;
  450. }
  451. EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
  452. int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  453. {
  454. int err = mutex_lock_interruptible(&rtc->ops_lock);
  455. if (err)
  456. return err;
  457. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  458. if (enabled == 0 && rtc->uie_irq_active) {
  459. mutex_unlock(&rtc->ops_lock);
  460. return rtc_dev_update_irq_enable_emul(rtc, 0);
  461. }
  462. #endif
  463. /* make sure we're changing state */
  464. if (rtc->uie_rtctimer.enabled == enabled)
  465. goto out;
  466. if (rtc->uie_unsupported) {
  467. err = -EINVAL;
  468. goto out;
  469. }
  470. if (enabled) {
  471. struct rtc_time tm;
  472. ktime_t now, onesec;
  473. __rtc_read_time(rtc, &tm);
  474. onesec = ktime_set(1, 0);
  475. now = rtc_tm_to_ktime(tm);
  476. rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
  477. rtc->uie_rtctimer.period = ktime_set(1, 0);
  478. err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
  479. } else
  480. rtc_timer_remove(rtc, &rtc->uie_rtctimer);
  481. out:
  482. mutex_unlock(&rtc->ops_lock);
  483. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  484. /*
  485. * Enable emulation if the driver did not provide
  486. * the update_irq_enable function pointer or if returned
  487. * -EINVAL to signal that it has been configured without
  488. * interrupts or that are not available at the moment.
  489. */
  490. if (err == -EINVAL)
  491. err = rtc_dev_update_irq_enable_emul(rtc, enabled);
  492. #endif
  493. return err;
  494. }
  495. EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
  496. /**
  497. * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
  498. * @rtc: pointer to the rtc device
  499. *
  500. * This function is called when an AIE, UIE or PIE mode interrupt
  501. * has occurred (or been emulated).
  502. *
  503. * Triggers the registered irq_task function callback.
  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. * @task: currently registered with rtc_irq_register()
  625. * @enabled: true to enable periodic IRQs
  626. * Context: any
  627. *
  628. * Note that rtc_irq_set_freq() should previously have been used to
  629. * specify the desired frequency of periodic IRQ.
  630. */
  631. int rtc_irq_set_state(struct rtc_device *rtc, int enabled)
  632. {
  633. int err = 0;
  634. while (rtc_update_hrtimer(rtc, enabled) < 0)
  635. cpu_relax();
  636. rtc->pie_enabled = enabled;
  637. trace_rtc_irq_set_state(enabled, err);
  638. return err;
  639. }
  640. EXPORT_SYMBOL_GPL(rtc_irq_set_state);
  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. EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
  663. /**
  664. * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
  665. * @rtc rtc device
  666. * @timer timer being added.
  667. *
  668. * Enqueues a timer onto the rtc devices timerqueue and sets
  669. * the next alarm event appropriately.
  670. *
  671. * Sets the enabled bit on the added timer.
  672. *
  673. * Must hold ops_lock for proper serialization of timerqueue
  674. */
  675. static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
  676. {
  677. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  678. struct rtc_time tm;
  679. ktime_t now;
  680. timer->enabled = 1;
  681. __rtc_read_time(rtc, &tm);
  682. now = rtc_tm_to_ktime(tm);
  683. /* Skip over expired timers */
  684. while (next) {
  685. if (next->expires >= now)
  686. break;
  687. next = timerqueue_iterate_next(next);
  688. }
  689. timerqueue_add(&rtc->timerqueue, &timer->node);
  690. trace_rtc_timer_enqueue(timer);
  691. if (!next || ktime_before(timer->node.expires, next->expires)) {
  692. struct rtc_wkalrm alarm;
  693. int err;
  694. alarm.time = rtc_ktime_to_tm(timer->node.expires);
  695. alarm.enabled = 1;
  696. err = __rtc_set_alarm(rtc, &alarm);
  697. if (err == -ETIME) {
  698. pm_stay_awake(rtc->dev.parent);
  699. schedule_work(&rtc->irqwork);
  700. } else if (err) {
  701. timerqueue_del(&rtc->timerqueue, &timer->node);
  702. trace_rtc_timer_dequeue(timer);
  703. timer->enabled = 0;
  704. return err;
  705. }
  706. }
  707. return 0;
  708. }
  709. static void rtc_alarm_disable(struct rtc_device *rtc)
  710. {
  711. if (!rtc->ops || !rtc->ops->alarm_irq_enable)
  712. return;
  713. rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
  714. trace_rtc_alarm_irq_enable(0, 0);
  715. }
  716. /**
  717. * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
  718. * @rtc rtc device
  719. * @timer timer being removed.
  720. *
  721. * Removes a timer onto the rtc devices timerqueue and sets
  722. * the next alarm event appropriately.
  723. *
  724. * Clears the enabled bit on the removed timer.
  725. *
  726. * Must hold ops_lock for proper serialization of timerqueue
  727. */
  728. static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
  729. {
  730. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  731. timerqueue_del(&rtc->timerqueue, &timer->node);
  732. trace_rtc_timer_dequeue(timer);
  733. timer->enabled = 0;
  734. if (next == &timer->node) {
  735. struct rtc_wkalrm alarm;
  736. int err;
  737. next = timerqueue_getnext(&rtc->timerqueue);
  738. if (!next) {
  739. rtc_alarm_disable(rtc);
  740. return;
  741. }
  742. alarm.time = rtc_ktime_to_tm(next->expires);
  743. alarm.enabled = 1;
  744. err = __rtc_set_alarm(rtc, &alarm);
  745. if (err == -ETIME) {
  746. pm_stay_awake(rtc->dev.parent);
  747. schedule_work(&rtc->irqwork);
  748. }
  749. }
  750. }
  751. /**
  752. * rtc_timer_do_work - Expires rtc timers
  753. * @rtc rtc device
  754. * @timer timer being removed.
  755. *
  756. * Expires rtc timers. Reprograms next alarm event if needed.
  757. * Called via worktask.
  758. *
  759. * Serializes access to timerqueue via ops_lock mutex
  760. */
  761. void rtc_timer_do_work(struct work_struct *work)
  762. {
  763. struct rtc_timer *timer;
  764. struct timerqueue_node *next;
  765. ktime_t now;
  766. struct rtc_time tm;
  767. struct rtc_device *rtc =
  768. container_of(work, struct rtc_device, irqwork);
  769. mutex_lock(&rtc->ops_lock);
  770. again:
  771. __rtc_read_time(rtc, &tm);
  772. now = rtc_tm_to_ktime(tm);
  773. while ((next = timerqueue_getnext(&rtc->timerqueue))) {
  774. if (next->expires > now)
  775. break;
  776. /* expire timer */
  777. timer = container_of(next, struct rtc_timer, node);
  778. timerqueue_del(&rtc->timerqueue, &timer->node);
  779. trace_rtc_timer_dequeue(timer);
  780. timer->enabled = 0;
  781. if (timer->task.func)
  782. timer->task.func(timer->task.private_data);
  783. trace_rtc_timer_fired(timer);
  784. /* Re-add/fwd periodic timers */
  785. if (ktime_to_ns(timer->period)) {
  786. timer->node.expires = ktime_add(timer->node.expires,
  787. timer->period);
  788. timer->enabled = 1;
  789. timerqueue_add(&rtc->timerqueue, &timer->node);
  790. trace_rtc_timer_enqueue(timer);
  791. }
  792. }
  793. /* Set next alarm */
  794. if (next) {
  795. struct rtc_wkalrm alarm;
  796. int err;
  797. int retry = 3;
  798. alarm.time = rtc_ktime_to_tm(next->expires);
  799. alarm.enabled = 1;
  800. reprogram:
  801. err = __rtc_set_alarm(rtc, &alarm);
  802. if (err == -ETIME)
  803. goto again;
  804. else if (err) {
  805. if (retry-- > 0)
  806. goto reprogram;
  807. timer = container_of(next, struct rtc_timer, node);
  808. timerqueue_del(&rtc->timerqueue, &timer->node);
  809. trace_rtc_timer_dequeue(timer);
  810. timer->enabled = 0;
  811. dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
  812. goto again;
  813. }
  814. } else
  815. rtc_alarm_disable(rtc);
  816. pm_relax(rtc->dev.parent);
  817. mutex_unlock(&rtc->ops_lock);
  818. }
  819. /* rtc_timer_init - Initializes an rtc_timer
  820. * @timer: timer to be intiialized
  821. * @f: function pointer to be called when timer fires
  822. * @data: private data passed to function pointer
  823. *
  824. * Kernel interface to initializing an rtc_timer.
  825. */
  826. void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data)
  827. {
  828. timerqueue_init(&timer->node);
  829. timer->enabled = 0;
  830. timer->task.func = f;
  831. timer->task.private_data = data;
  832. }
  833. /* rtc_timer_start - Sets an rtc_timer to fire in the future
  834. * @ rtc: rtc device to be used
  835. * @ timer: timer being set
  836. * @ expires: time at which to expire the timer
  837. * @ period: period that the timer will recur
  838. *
  839. * Kernel interface to set an rtc_timer
  840. */
  841. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
  842. ktime_t expires, ktime_t period)
  843. {
  844. int ret = 0;
  845. mutex_lock(&rtc->ops_lock);
  846. if (timer->enabled)
  847. rtc_timer_remove(rtc, timer);
  848. timer->node.expires = expires;
  849. timer->period = period;
  850. ret = rtc_timer_enqueue(rtc, timer);
  851. mutex_unlock(&rtc->ops_lock);
  852. return ret;
  853. }
  854. /* rtc_timer_cancel - Stops an rtc_timer
  855. * @ rtc: rtc device to be used
  856. * @ timer: timer being set
  857. *
  858. * Kernel interface to cancel an rtc_timer
  859. */
  860. void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
  861. {
  862. mutex_lock(&rtc->ops_lock);
  863. if (timer->enabled)
  864. rtc_timer_remove(rtc, timer);
  865. mutex_unlock(&rtc->ops_lock);
  866. }
  867. /**
  868. * rtc_read_offset - Read the amount of rtc offset in parts per billion
  869. * @ rtc: rtc device to be used
  870. * @ offset: the offset in parts per billion
  871. *
  872. * see below for details.
  873. *
  874. * Kernel interface to read rtc clock offset
  875. * Returns 0 on success, or a negative number on error.
  876. * If read_offset() is not implemented for the rtc, return -EINVAL
  877. */
  878. int rtc_read_offset(struct rtc_device *rtc, long *offset)
  879. {
  880. int ret;
  881. if (!rtc->ops)
  882. return -ENODEV;
  883. if (!rtc->ops->read_offset)
  884. return -EINVAL;
  885. mutex_lock(&rtc->ops_lock);
  886. ret = rtc->ops->read_offset(rtc->dev.parent, offset);
  887. mutex_unlock(&rtc->ops_lock);
  888. trace_rtc_read_offset(*offset, ret);
  889. return ret;
  890. }
  891. /**
  892. * rtc_set_offset - Adjusts the duration of the average second
  893. * @ rtc: rtc device to be used
  894. * @ offset: the offset in parts per billion
  895. *
  896. * Some rtc's allow an adjustment to the average duration of a second
  897. * to compensate for differences in the actual clock rate due to temperature,
  898. * the crystal, capacitor, etc.
  899. *
  900. * The adjustment applied is as follows:
  901. * t = t0 * (1 + offset * 1e-9)
  902. * where t0 is the measured length of 1 RTC second with offset = 0
  903. *
  904. * Kernel interface to adjust an rtc clock offset.
  905. * Return 0 on success, or a negative number on error.
  906. * If the rtc offset is not setable (or not implemented), return -EINVAL
  907. */
  908. int rtc_set_offset(struct rtc_device *rtc, long offset)
  909. {
  910. int ret;
  911. if (!rtc->ops)
  912. return -ENODEV;
  913. if (!rtc->ops->set_offset)
  914. return -EINVAL;
  915. mutex_lock(&rtc->ops_lock);
  916. ret = rtc->ops->set_offset(rtc->dev.parent, offset);
  917. mutex_unlock(&rtc->ops_lock);
  918. trace_rtc_set_offset(offset, ret);
  919. return ret;
  920. }