interface.c 28 KB

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