interface.c 28 KB

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