interface.c 27 KB

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