interface.c 23 KB

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