interface.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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. /* Can't proceed if alarm is still invalid after replacing
  194. * missing fields.
  195. */
  196. err = rtc_valid_tm(&alarm->time);
  197. if (err)
  198. goto done;
  199. /* with luck, no rollover is needed */
  200. t_now = rtc_tm_to_time64(&now);
  201. t_alm = rtc_tm_to_time64(&alarm->time);
  202. if (t_now < t_alm)
  203. goto done;
  204. switch (missing) {
  205. /* 24 hour rollover ... if it's now 10am Monday, an alarm that
  206. * that will trigger at 5am will do so at 5am Tuesday, which
  207. * could also be in the next month or year. This is a common
  208. * case, especially for PCs.
  209. */
  210. case day:
  211. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
  212. t_alm += 24 * 60 * 60;
  213. rtc_time64_to_tm(t_alm, &alarm->time);
  214. break;
  215. /* Month rollover ... if it's the 31th, an alarm on the 3rd will
  216. * be next month. An alarm matching on the 30th, 29th, or 28th
  217. * may end up in the month after that! Many newer PCs support
  218. * this type of alarm.
  219. */
  220. case month:
  221. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
  222. do {
  223. if (alarm->time.tm_mon < 11)
  224. alarm->time.tm_mon++;
  225. else {
  226. alarm->time.tm_mon = 0;
  227. alarm->time.tm_year++;
  228. }
  229. days = rtc_month_days(alarm->time.tm_mon,
  230. alarm->time.tm_year);
  231. } while (days < alarm->time.tm_mday);
  232. break;
  233. /* Year rollover ... easy except for leap years! */
  234. case year:
  235. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
  236. do {
  237. alarm->time.tm_year++;
  238. } while (!is_leap_year(alarm->time.tm_year + 1900)
  239. && rtc_valid_tm(&alarm->time) != 0);
  240. break;
  241. default:
  242. dev_warn(&rtc->dev, "alarm rollover not handled\n");
  243. }
  244. err = rtc_valid_tm(&alarm->time);
  245. done:
  246. if (err) {
  247. dev_warn(&rtc->dev, "invalid alarm value: %d-%d-%d %d:%d:%d\n",
  248. alarm->time.tm_year + 1900, alarm->time.tm_mon + 1,
  249. alarm->time.tm_mday, alarm->time.tm_hour, alarm->time.tm_min,
  250. alarm->time.tm_sec);
  251. }
  252. return err;
  253. }
  254. int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  255. {
  256. int err;
  257. err = mutex_lock_interruptible(&rtc->ops_lock);
  258. if (err)
  259. return err;
  260. if (rtc->ops == NULL)
  261. err = -ENODEV;
  262. else if (!rtc->ops->read_alarm)
  263. err = -EINVAL;
  264. else {
  265. memset(alarm, 0, sizeof(struct rtc_wkalrm));
  266. alarm->enabled = rtc->aie_timer.enabled;
  267. alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
  268. }
  269. mutex_unlock(&rtc->ops_lock);
  270. return err;
  271. }
  272. EXPORT_SYMBOL_GPL(rtc_read_alarm);
  273. static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  274. {
  275. struct rtc_time tm;
  276. time64_t now, scheduled;
  277. int err;
  278. err = rtc_valid_tm(&alarm->time);
  279. if (err)
  280. return err;
  281. scheduled = rtc_tm_to_time64(&alarm->time);
  282. /* Make sure we're not setting alarms in the past */
  283. err = __rtc_read_time(rtc, &tm);
  284. if (err)
  285. return err;
  286. now = rtc_tm_to_time64(&tm);
  287. if (scheduled <= now)
  288. return -ETIME;
  289. /*
  290. * XXX - We just checked to make sure the alarm time is not
  291. * in the past, but there is still a race window where if
  292. * the is alarm set for the next second and the second ticks
  293. * over right here, before we set the alarm.
  294. */
  295. if (!rtc->ops)
  296. err = -ENODEV;
  297. else if (!rtc->ops->set_alarm)
  298. err = -EINVAL;
  299. else
  300. err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
  301. return err;
  302. }
  303. int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  304. {
  305. int err;
  306. if (!rtc->ops)
  307. return -ENODEV;
  308. else if (!rtc->ops->set_alarm)
  309. return -EINVAL;
  310. err = rtc_valid_tm(&alarm->time);
  311. if (err != 0)
  312. return err;
  313. err = mutex_lock_interruptible(&rtc->ops_lock);
  314. if (err)
  315. return err;
  316. if (rtc->aie_timer.enabled)
  317. rtc_timer_remove(rtc, &rtc->aie_timer);
  318. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  319. rtc->aie_timer.period = ktime_set(0, 0);
  320. if (alarm->enabled)
  321. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  322. mutex_unlock(&rtc->ops_lock);
  323. return err;
  324. }
  325. EXPORT_SYMBOL_GPL(rtc_set_alarm);
  326. /* Called once per device from rtc_device_register */
  327. int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  328. {
  329. int err;
  330. struct rtc_time now;
  331. err = rtc_valid_tm(&alarm->time);
  332. if (err != 0)
  333. return err;
  334. err = rtc_read_time(rtc, &now);
  335. if (err)
  336. return err;
  337. err = mutex_lock_interruptible(&rtc->ops_lock);
  338. if (err)
  339. return err;
  340. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  341. rtc->aie_timer.period = ktime_set(0, 0);
  342. /* Alarm has to be enabled & in the futrure for us to enqueue it */
  343. if (alarm->enabled && (rtc_tm_to_ktime(now).tv64 <
  344. rtc->aie_timer.node.expires.tv64)) {
  345. rtc->aie_timer.enabled = 1;
  346. timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
  347. }
  348. mutex_unlock(&rtc->ops_lock);
  349. return err;
  350. }
  351. EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
  352. int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  353. {
  354. int err = mutex_lock_interruptible(&rtc->ops_lock);
  355. if (err)
  356. return err;
  357. if (rtc->aie_timer.enabled != enabled) {
  358. if (enabled)
  359. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  360. else
  361. rtc_timer_remove(rtc, &rtc->aie_timer);
  362. }
  363. if (err)
  364. /* nothing */;
  365. else if (!rtc->ops)
  366. err = -ENODEV;
  367. else if (!rtc->ops->alarm_irq_enable)
  368. err = -EINVAL;
  369. else
  370. err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
  371. mutex_unlock(&rtc->ops_lock);
  372. return err;
  373. }
  374. EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
  375. int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  376. {
  377. int err = mutex_lock_interruptible(&rtc->ops_lock);
  378. if (err)
  379. return err;
  380. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  381. if (enabled == 0 && rtc->uie_irq_active) {
  382. mutex_unlock(&rtc->ops_lock);
  383. return rtc_dev_update_irq_enable_emul(rtc, 0);
  384. }
  385. #endif
  386. /* make sure we're changing state */
  387. if (rtc->uie_rtctimer.enabled == enabled)
  388. goto out;
  389. if (rtc->uie_unsupported) {
  390. err = -EINVAL;
  391. goto out;
  392. }
  393. if (enabled) {
  394. struct rtc_time tm;
  395. ktime_t now, onesec;
  396. __rtc_read_time(rtc, &tm);
  397. onesec = ktime_set(1, 0);
  398. now = rtc_tm_to_ktime(tm);
  399. rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
  400. rtc->uie_rtctimer.period = ktime_set(1, 0);
  401. err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
  402. } else
  403. rtc_timer_remove(rtc, &rtc->uie_rtctimer);
  404. out:
  405. mutex_unlock(&rtc->ops_lock);
  406. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  407. /*
  408. * Enable emulation if the driver did not provide
  409. * the update_irq_enable function pointer or if returned
  410. * -EINVAL to signal that it has been configured without
  411. * interrupts or that are not available at the moment.
  412. */
  413. if (err == -EINVAL)
  414. err = rtc_dev_update_irq_enable_emul(rtc, enabled);
  415. #endif
  416. return err;
  417. }
  418. EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
  419. /**
  420. * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
  421. * @rtc: pointer to the rtc device
  422. *
  423. * This function is called when an AIE, UIE or PIE mode interrupt
  424. * has occurred (or been emulated).
  425. *
  426. * Triggers the registered irq_task function callback.
  427. */
  428. void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
  429. {
  430. unsigned long flags;
  431. /* mark one irq of the appropriate mode */
  432. spin_lock_irqsave(&rtc->irq_lock, flags);
  433. rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
  434. spin_unlock_irqrestore(&rtc->irq_lock, flags);
  435. /* call the task func */
  436. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  437. if (rtc->irq_task)
  438. rtc->irq_task->func(rtc->irq_task->private_data);
  439. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  440. wake_up_interruptible(&rtc->irq_queue);
  441. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  442. }
  443. /**
  444. * rtc_aie_update_irq - AIE mode rtctimer hook
  445. * @private: pointer to the rtc_device
  446. *
  447. * This functions is called when the aie_timer expires.
  448. */
  449. void rtc_aie_update_irq(void *private)
  450. {
  451. struct rtc_device *rtc = (struct rtc_device *)private;
  452. rtc_handle_legacy_irq(rtc, 1, RTC_AF);
  453. }
  454. /**
  455. * rtc_uie_update_irq - UIE mode rtctimer hook
  456. * @private: pointer to the rtc_device
  457. *
  458. * This functions is called when the uie_timer expires.
  459. */
  460. void rtc_uie_update_irq(void *private)
  461. {
  462. struct rtc_device *rtc = (struct rtc_device *)private;
  463. rtc_handle_legacy_irq(rtc, 1, RTC_UF);
  464. }
  465. /**
  466. * rtc_pie_update_irq - PIE mode hrtimer hook
  467. * @timer: pointer to the pie mode hrtimer
  468. *
  469. * This function is used to emulate PIE mode interrupts
  470. * using an hrtimer. This function is called when the periodic
  471. * hrtimer expires.
  472. */
  473. enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
  474. {
  475. struct rtc_device *rtc;
  476. ktime_t period;
  477. int count;
  478. rtc = container_of(timer, struct rtc_device, pie_timer);
  479. period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
  480. count = hrtimer_forward_now(timer, period);
  481. rtc_handle_legacy_irq(rtc, count, RTC_PF);
  482. return HRTIMER_RESTART;
  483. }
  484. /**
  485. * rtc_update_irq - Triggered when a RTC interrupt occurs.
  486. * @rtc: the rtc device
  487. * @num: how many irqs are being reported (usually one)
  488. * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
  489. * Context: any
  490. */
  491. void rtc_update_irq(struct rtc_device *rtc,
  492. unsigned long num, unsigned long events)
  493. {
  494. if (IS_ERR_OR_NULL(rtc))
  495. return;
  496. pm_stay_awake(rtc->dev.parent);
  497. schedule_work(&rtc->irqwork);
  498. }
  499. EXPORT_SYMBOL_GPL(rtc_update_irq);
  500. static int __rtc_match(struct device *dev, const void *data)
  501. {
  502. const char *name = data;
  503. if (strcmp(dev_name(dev), name) == 0)
  504. return 1;
  505. return 0;
  506. }
  507. struct rtc_device *rtc_class_open(const char *name)
  508. {
  509. struct device *dev;
  510. struct rtc_device *rtc = NULL;
  511. dev = class_find_device(rtc_class, NULL, name, __rtc_match);
  512. if (dev)
  513. rtc = to_rtc_device(dev);
  514. if (rtc) {
  515. if (!try_module_get(rtc->owner)) {
  516. put_device(dev);
  517. rtc = NULL;
  518. }
  519. }
  520. return rtc;
  521. }
  522. EXPORT_SYMBOL_GPL(rtc_class_open);
  523. void rtc_class_close(struct rtc_device *rtc)
  524. {
  525. module_put(rtc->owner);
  526. put_device(&rtc->dev);
  527. }
  528. EXPORT_SYMBOL_GPL(rtc_class_close);
  529. int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
  530. {
  531. int retval = -EBUSY;
  532. if (task == NULL || task->func == NULL)
  533. return -EINVAL;
  534. /* Cannot register while the char dev is in use */
  535. if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
  536. return -EBUSY;
  537. spin_lock_irq(&rtc->irq_task_lock);
  538. if (rtc->irq_task == NULL) {
  539. rtc->irq_task = task;
  540. retval = 0;
  541. }
  542. spin_unlock_irq(&rtc->irq_task_lock);
  543. clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
  544. return retval;
  545. }
  546. EXPORT_SYMBOL_GPL(rtc_irq_register);
  547. void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
  548. {
  549. spin_lock_irq(&rtc->irq_task_lock);
  550. if (rtc->irq_task == task)
  551. rtc->irq_task = NULL;
  552. spin_unlock_irq(&rtc->irq_task_lock);
  553. }
  554. EXPORT_SYMBOL_GPL(rtc_irq_unregister);
  555. static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
  556. {
  557. /*
  558. * We always cancel the timer here first, because otherwise
  559. * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  560. * when we manage to start the timer before the callback
  561. * returns HRTIMER_RESTART.
  562. *
  563. * We cannot use hrtimer_cancel() here as a running callback
  564. * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
  565. * would spin forever.
  566. */
  567. if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
  568. return -1;
  569. if (enabled) {
  570. ktime_t period = ktime_set(0, NSEC_PER_SEC / rtc->irq_freq);
  571. hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
  572. }
  573. return 0;
  574. }
  575. /**
  576. * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
  577. * @rtc: the rtc device
  578. * @task: currently registered with rtc_irq_register()
  579. * @enabled: true to enable periodic IRQs
  580. * Context: any
  581. *
  582. * Note that rtc_irq_set_freq() should previously have been used to
  583. * specify the desired frequency of periodic IRQ task->func() callbacks.
  584. */
  585. int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
  586. {
  587. int err = 0;
  588. unsigned long flags;
  589. retry:
  590. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  591. if (rtc->irq_task != NULL && task == NULL)
  592. err = -EBUSY;
  593. else if (rtc->irq_task != task)
  594. err = -EACCES;
  595. else {
  596. if (rtc_update_hrtimer(rtc, enabled) < 0) {
  597. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  598. cpu_relax();
  599. goto retry;
  600. }
  601. rtc->pie_enabled = enabled;
  602. }
  603. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  604. return err;
  605. }
  606. EXPORT_SYMBOL_GPL(rtc_irq_set_state);
  607. /**
  608. * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
  609. * @rtc: the rtc device
  610. * @task: currently registered with rtc_irq_register()
  611. * @freq: positive frequency with which task->func() will be called
  612. * Context: any
  613. *
  614. * Note that rtc_irq_set_state() is used to enable or disable the
  615. * periodic IRQs.
  616. */
  617. int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
  618. {
  619. int err = 0;
  620. unsigned long flags;
  621. if (freq <= 0 || freq > RTC_MAX_FREQ)
  622. return -EINVAL;
  623. retry:
  624. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  625. if (rtc->irq_task != NULL && task == NULL)
  626. err = -EBUSY;
  627. else if (rtc->irq_task != task)
  628. err = -EACCES;
  629. else {
  630. rtc->irq_freq = freq;
  631. if (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) {
  632. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  633. cpu_relax();
  634. goto retry;
  635. }
  636. }
  637. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  638. return err;
  639. }
  640. EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
  641. /**
  642. * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
  643. * @rtc rtc device
  644. * @timer timer being added.
  645. *
  646. * Enqueues a timer onto the rtc devices timerqueue and sets
  647. * the next alarm event appropriately.
  648. *
  649. * Sets the enabled bit on the added timer.
  650. *
  651. * Must hold ops_lock for proper serialization of timerqueue
  652. */
  653. static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
  654. {
  655. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  656. struct rtc_time tm;
  657. ktime_t now;
  658. timer->enabled = 1;
  659. __rtc_read_time(rtc, &tm);
  660. now = rtc_tm_to_ktime(tm);
  661. /* Skip over expired timers */
  662. while (next) {
  663. if (next->expires.tv64 >= now.tv64)
  664. break;
  665. next = timerqueue_iterate_next(next);
  666. }
  667. timerqueue_add(&rtc->timerqueue, &timer->node);
  668. if (!next || ktime_before(timer->node.expires, next->expires)) {
  669. struct rtc_wkalrm alarm;
  670. int err;
  671. alarm.time = rtc_ktime_to_tm(timer->node.expires);
  672. alarm.enabled = 1;
  673. err = __rtc_set_alarm(rtc, &alarm);
  674. if (err == -ETIME) {
  675. pm_stay_awake(rtc->dev.parent);
  676. schedule_work(&rtc->irqwork);
  677. } else if (err) {
  678. timerqueue_del(&rtc->timerqueue, &timer->node);
  679. timer->enabled = 0;
  680. return err;
  681. }
  682. }
  683. return 0;
  684. }
  685. static void rtc_alarm_disable(struct rtc_device *rtc)
  686. {
  687. if (!rtc->ops || !rtc->ops->alarm_irq_enable)
  688. return;
  689. rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
  690. }
  691. /**
  692. * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
  693. * @rtc rtc device
  694. * @timer timer being removed.
  695. *
  696. * Removes a timer onto the rtc devices timerqueue and sets
  697. * the next alarm event appropriately.
  698. *
  699. * Clears the enabled bit on the removed timer.
  700. *
  701. * Must hold ops_lock for proper serialization of timerqueue
  702. */
  703. static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
  704. {
  705. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  706. timerqueue_del(&rtc->timerqueue, &timer->node);
  707. timer->enabled = 0;
  708. if (next == &timer->node) {
  709. struct rtc_wkalrm alarm;
  710. int err;
  711. next = timerqueue_getnext(&rtc->timerqueue);
  712. if (!next) {
  713. rtc_alarm_disable(rtc);
  714. return;
  715. }
  716. alarm.time = rtc_ktime_to_tm(next->expires);
  717. alarm.enabled = 1;
  718. err = __rtc_set_alarm(rtc, &alarm);
  719. if (err == -ETIME) {
  720. pm_stay_awake(rtc->dev.parent);
  721. schedule_work(&rtc->irqwork);
  722. }
  723. }
  724. }
  725. /**
  726. * rtc_timer_do_work - Expires rtc timers
  727. * @rtc rtc device
  728. * @timer timer being removed.
  729. *
  730. * Expires rtc timers. Reprograms next alarm event if needed.
  731. * Called via worktask.
  732. *
  733. * Serializes access to timerqueue via ops_lock mutex
  734. */
  735. void rtc_timer_do_work(struct work_struct *work)
  736. {
  737. struct rtc_timer *timer;
  738. struct timerqueue_node *next;
  739. ktime_t now;
  740. struct rtc_time tm;
  741. struct rtc_device *rtc =
  742. container_of(work, struct rtc_device, irqwork);
  743. mutex_lock(&rtc->ops_lock);
  744. again:
  745. __rtc_read_time(rtc, &tm);
  746. now = rtc_tm_to_ktime(tm);
  747. while ((next = timerqueue_getnext(&rtc->timerqueue))) {
  748. if (next->expires.tv64 > now.tv64)
  749. break;
  750. /* expire timer */
  751. timer = container_of(next, struct rtc_timer, node);
  752. timerqueue_del(&rtc->timerqueue, &timer->node);
  753. timer->enabled = 0;
  754. if (timer->task.func)
  755. timer->task.func(timer->task.private_data);
  756. /* Re-add/fwd periodic timers */
  757. if (ktime_to_ns(timer->period)) {
  758. timer->node.expires = ktime_add(timer->node.expires,
  759. timer->period);
  760. timer->enabled = 1;
  761. timerqueue_add(&rtc->timerqueue, &timer->node);
  762. }
  763. }
  764. /* Set next alarm */
  765. if (next) {
  766. struct rtc_wkalrm alarm;
  767. int err;
  768. int retry = 3;
  769. alarm.time = rtc_ktime_to_tm(next->expires);
  770. alarm.enabled = 1;
  771. reprogram:
  772. err = __rtc_set_alarm(rtc, &alarm);
  773. if (err == -ETIME)
  774. goto again;
  775. else if (err) {
  776. if (retry-- > 0)
  777. goto reprogram;
  778. timer = container_of(next, struct rtc_timer, node);
  779. timerqueue_del(&rtc->timerqueue, &timer->node);
  780. timer->enabled = 0;
  781. dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
  782. goto again;
  783. }
  784. } else
  785. rtc_alarm_disable(rtc);
  786. pm_relax(rtc->dev.parent);
  787. mutex_unlock(&rtc->ops_lock);
  788. }
  789. /* rtc_timer_init - Initializes an rtc_timer
  790. * @timer: timer to be intiialized
  791. * @f: function pointer to be called when timer fires
  792. * @data: private data passed to function pointer
  793. *
  794. * Kernel interface to initializing an rtc_timer.
  795. */
  796. void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data)
  797. {
  798. timerqueue_init(&timer->node);
  799. timer->enabled = 0;
  800. timer->task.func = f;
  801. timer->task.private_data = data;
  802. }
  803. /* rtc_timer_start - Sets an rtc_timer to fire in the future
  804. * @ rtc: rtc device to be used
  805. * @ timer: timer being set
  806. * @ expires: time at which to expire the timer
  807. * @ period: period that the timer will recur
  808. *
  809. * Kernel interface to set an rtc_timer
  810. */
  811. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
  812. ktime_t expires, ktime_t period)
  813. {
  814. int ret = 0;
  815. mutex_lock(&rtc->ops_lock);
  816. if (timer->enabled)
  817. rtc_timer_remove(rtc, timer);
  818. timer->node.expires = expires;
  819. timer->period = period;
  820. ret = rtc_timer_enqueue(rtc, timer);
  821. mutex_unlock(&rtc->ops_lock);
  822. return ret;
  823. }
  824. /* rtc_timer_cancel - Stops an rtc_timer
  825. * @ rtc: rtc device to be used
  826. * @ timer: timer being set
  827. *
  828. * Kernel interface to cancel an rtc_timer
  829. */
  830. void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
  831. {
  832. mutex_lock(&rtc->ops_lock);
  833. if (timer->enabled)
  834. rtc_timer_remove(rtc, timer);
  835. mutex_unlock(&rtc->ops_lock);
  836. }